1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/inline.c
4 * Copyright (c) 2013, Intel Corporation
5 * Authors: Huajun Li <huajun.li@intel.com>
6 * Haicheng Li <haicheng.li@intel.com>
7 */
8
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/fiemap.h>
12
13 #include "f2fs.h"
14 #include "node.h"
15 #include <trace/events/f2fs.h>
16
support_inline_data(struct inode * inode)17 static bool support_inline_data(struct inode *inode)
18 {
19 if (f2fs_is_atomic_file(inode))
20 return false;
21 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
22 return false;
23 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
24 return false;
25 return true;
26 }
27
f2fs_may_inline_data(struct inode * inode)28 bool f2fs_may_inline_data(struct inode *inode)
29 {
30 if (!support_inline_data(inode))
31 return false;
32
33 return !f2fs_post_read_required(inode);
34 }
35
f2fs_sanity_check_inline_data(struct inode * inode)36 bool f2fs_sanity_check_inline_data(struct inode *inode)
37 {
38 if (!f2fs_has_inline_data(inode))
39 return false;
40
41 if (!support_inline_data(inode))
42 return true;
43
44 /*
45 * used by sanity_check_inode(), when disk layout fields has not
46 * been synchronized to inmem fields.
47 */
48 return (S_ISREG(inode->i_mode) &&
49 (file_is_encrypt(inode) || file_is_verity(inode) ||
50 (F2FS_I(inode)->i_flags & F2FS_COMPR_FL)));
51 }
52
f2fs_may_inline_dentry(struct inode * inode)53 bool f2fs_may_inline_dentry(struct inode *inode)
54 {
55 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
56 return false;
57
58 if (!S_ISDIR(inode->i_mode))
59 return false;
60
61 return true;
62 }
63
f2fs_do_read_inline_data(struct page * page,struct page * ipage)64 void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
65 {
66 struct inode *inode = page->mapping->host;
67
68 if (PageUptodate(page))
69 return;
70
71 f2fs_bug_on(F2FS_P_SB(page), page->index);
72
73 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
74
75 /* Copy the whole inline data block */
76 memcpy_to_page(page, 0, inline_data_addr(inode, ipage),
77 MAX_INLINE_DATA(inode));
78 if (!PageUptodate(page))
79 SetPageUptodate(page);
80 }
81
f2fs_truncate_inline_inode(struct inode * inode,struct page * ipage,u64 from)82 void f2fs_truncate_inline_inode(struct inode *inode,
83 struct page *ipage, u64 from)
84 {
85 void *addr;
86
87 if (from >= MAX_INLINE_DATA(inode))
88 return;
89
90 addr = inline_data_addr(inode, ipage);
91
92 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
93 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
94 set_page_dirty(ipage);
95
96 if (from == 0)
97 clear_inode_flag(inode, FI_DATA_EXIST);
98 }
99
f2fs_read_inline_data(struct inode * inode,struct page * page)100 int f2fs_read_inline_data(struct inode *inode, struct page *page)
101 {
102 struct page *ipage;
103
104 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
105 if (IS_ERR(ipage)) {
106 unlock_page(page);
107 return PTR_ERR(ipage);
108 }
109
110 if (!f2fs_has_inline_data(inode)) {
111 f2fs_put_page(ipage, 1);
112 return -EAGAIN;
113 }
114
115 if (page->index)
116 zero_user_segment(page, 0, PAGE_SIZE);
117 else
118 f2fs_do_read_inline_data(page, ipage);
119
120 if (!PageUptodate(page))
121 SetPageUptodate(page);
122 f2fs_put_page(ipage, 1);
123 unlock_page(page);
124 return 0;
125 }
126
f2fs_convert_inline_page(struct dnode_of_data * dn,struct page * page)127 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
128 {
129 struct f2fs_io_info fio = {
130 .sbi = F2FS_I_SB(dn->inode),
131 .ino = dn->inode->i_ino,
132 .type = DATA,
133 .op = REQ_OP_WRITE,
134 .op_flags = REQ_SYNC | REQ_PRIO,
135 .page = page,
136 .encrypted_page = NULL,
137 .io_type = FS_DATA_IO,
138 };
139 struct node_info ni;
140 int dirty, err;
141
142 if (!f2fs_exist_data(dn->inode))
143 goto clear_out;
144
145 err = f2fs_reserve_block(dn, 0);
146 if (err)
147 return err;
148
149 err = f2fs_get_node_info(fio.sbi, dn->nid, &ni, false);
150 if (err) {
151 f2fs_truncate_data_blocks_range(dn, 1);
152 f2fs_put_dnode(dn);
153 return err;
154 }
155
156 fio.version = ni.version;
157
158 if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
159 f2fs_put_dnode(dn);
160 set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
161 f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
162 __func__, dn->inode->i_ino, dn->data_blkaddr);
163 f2fs_handle_error(fio.sbi, ERROR_INVALID_BLKADDR);
164 return -EFSCORRUPTED;
165 }
166
167 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
168
169 f2fs_do_read_inline_data(page, dn->inode_page);
170 set_page_dirty(page);
171
172 /* clear dirty state */
173 dirty = clear_page_dirty_for_io(page);
174
175 /* write data page to try to make data consistent */
176 set_page_writeback(page);
177 fio.old_blkaddr = dn->data_blkaddr;
178 set_inode_flag(dn->inode, FI_HOT_DATA);
179 f2fs_outplace_write_data(dn, &fio);
180 f2fs_wait_on_page_writeback(page, DATA, true, true);
181 if (dirty) {
182 inode_dec_dirty_pages(dn->inode);
183 f2fs_remove_dirty_inode(dn->inode);
184 }
185
186 /* this converted inline_data should be recovered. */
187 set_inode_flag(dn->inode, FI_APPEND_WRITE);
188
189 /* clear inline data and flag after data writeback */
190 f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
191 clear_page_private_inline(dn->inode_page);
192 clear_out:
193 stat_dec_inline_inode(dn->inode);
194 clear_inode_flag(dn->inode, FI_INLINE_DATA);
195 f2fs_put_dnode(dn);
196 return 0;
197 }
198
f2fs_convert_inline_inode(struct inode * inode)199 int f2fs_convert_inline_inode(struct inode *inode)
200 {
201 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
202 struct dnode_of_data dn;
203 struct page *ipage, *page;
204 int err = 0;
205
206 if (!f2fs_has_inline_data(inode) ||
207 f2fs_hw_is_readonly(sbi) || f2fs_readonly(sbi->sb))
208 return 0;
209
210 err = f2fs_dquot_initialize(inode);
211 if (err)
212 return err;
213
214 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
215 if (!page)
216 return -ENOMEM;
217
218 f2fs_lock_op(sbi);
219
220 ipage = f2fs_get_node_page(sbi, inode->i_ino);
221 if (IS_ERR(ipage)) {
222 err = PTR_ERR(ipage);
223 goto out;
224 }
225
226 set_new_dnode(&dn, inode, ipage, ipage, 0);
227
228 if (f2fs_has_inline_data(inode))
229 err = f2fs_convert_inline_page(&dn, page);
230
231 f2fs_put_dnode(&dn);
232 out:
233 f2fs_unlock_op(sbi);
234
235 f2fs_put_page(page, 1);
236
237 if (!err)
238 f2fs_balance_fs(sbi, dn.node_changed);
239
240 return err;
241 }
242
f2fs_write_inline_data(struct inode * inode,struct page * page)243 int f2fs_write_inline_data(struct inode *inode, struct page *page)
244 {
245 struct dnode_of_data dn;
246 int err;
247
248 set_new_dnode(&dn, inode, NULL, NULL, 0);
249 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
250 if (err)
251 return err;
252
253 if (!f2fs_has_inline_data(inode)) {
254 f2fs_put_dnode(&dn);
255 return -EAGAIN;
256 }
257
258 f2fs_bug_on(F2FS_I_SB(inode), page->index);
259
260 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
261 memcpy_from_page(inline_data_addr(inode, dn.inode_page),
262 page, 0, MAX_INLINE_DATA(inode));
263 set_page_dirty(dn.inode_page);
264
265 f2fs_clear_page_cache_dirty_tag(page);
266
267 set_inode_flag(inode, FI_APPEND_WRITE);
268 set_inode_flag(inode, FI_DATA_EXIST);
269
270 clear_page_private_inline(dn.inode_page);
271 f2fs_put_dnode(&dn);
272 return 0;
273 }
274
f2fs_recover_inline_data(struct inode * inode,struct page * npage)275 int f2fs_recover_inline_data(struct inode *inode, struct page *npage)
276 {
277 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
278 struct f2fs_inode *ri = NULL;
279 void *src_addr, *dst_addr;
280 struct page *ipage;
281
282 /*
283 * The inline_data recovery policy is as follows.
284 * [prev.] [next] of inline_data flag
285 * o o -> recover inline_data
286 * o x -> remove inline_data, and then recover data blocks
287 * x o -> remove data blocks, and then recover inline_data
288 * x x -> recover data blocks
289 */
290 if (IS_INODE(npage))
291 ri = F2FS_INODE(npage);
292
293 if (f2fs_has_inline_data(inode) &&
294 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
295 process_inline:
296 ipage = f2fs_get_node_page(sbi, inode->i_ino);
297 if (IS_ERR(ipage))
298 return PTR_ERR(ipage);
299
300 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
301
302 src_addr = inline_data_addr(inode, npage);
303 dst_addr = inline_data_addr(inode, ipage);
304 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
305
306 set_inode_flag(inode, FI_INLINE_DATA);
307 set_inode_flag(inode, FI_DATA_EXIST);
308
309 set_page_dirty(ipage);
310 f2fs_put_page(ipage, 1);
311 return 1;
312 }
313
314 if (f2fs_has_inline_data(inode)) {
315 ipage = f2fs_get_node_page(sbi, inode->i_ino);
316 if (IS_ERR(ipage))
317 return PTR_ERR(ipage);
318 f2fs_truncate_inline_inode(inode, ipage, 0);
319 stat_dec_inline_inode(inode);
320 clear_inode_flag(inode, FI_INLINE_DATA);
321 f2fs_put_page(ipage, 1);
322 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
323 int ret;
324
325 ret = f2fs_truncate_blocks(inode, 0, false);
326 if (ret)
327 return ret;
328 stat_inc_inline_inode(inode);
329 goto process_inline;
330 }
331 return 0;
332 }
333
f2fs_find_in_inline_dir(struct inode * dir,const struct f2fs_filename * fname,struct page ** res_page)334 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
335 const struct f2fs_filename *fname,
336 struct page **res_page)
337 {
338 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
339 struct f2fs_dir_entry *de;
340 struct f2fs_dentry_ptr d;
341 struct page *ipage;
342 void *inline_dentry;
343
344 ipage = f2fs_get_node_page(sbi, dir->i_ino);
345 if (IS_ERR(ipage)) {
346 *res_page = ipage;
347 return NULL;
348 }
349
350 inline_dentry = inline_data_addr(dir, ipage);
351
352 make_dentry_ptr_inline(dir, &d, inline_dentry);
353 de = f2fs_find_target_dentry(&d, fname, NULL);
354 unlock_page(ipage);
355 if (IS_ERR(de)) {
356 *res_page = ERR_CAST(de);
357 de = NULL;
358 }
359 if (de)
360 *res_page = ipage;
361 else
362 f2fs_put_page(ipage, 0);
363
364 return de;
365 }
366
f2fs_make_empty_inline_dir(struct inode * inode,struct inode * parent,struct page * ipage)367 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
368 struct page *ipage)
369 {
370 struct f2fs_dentry_ptr d;
371 void *inline_dentry;
372
373 inline_dentry = inline_data_addr(inode, ipage);
374
375 make_dentry_ptr_inline(inode, &d, inline_dentry);
376 f2fs_do_make_empty_dir(inode, parent, &d);
377
378 set_page_dirty(ipage);
379
380 /* update i_size to MAX_INLINE_DATA */
381 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
382 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
383 return 0;
384 }
385
386 /*
387 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
388 * release ipage in this function.
389 */
f2fs_move_inline_dirents(struct inode * dir,struct page * ipage,void * inline_dentry)390 static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
391 void *inline_dentry)
392 {
393 struct page *page;
394 struct dnode_of_data dn;
395 struct f2fs_dentry_block *dentry_blk;
396 struct f2fs_dentry_ptr src, dst;
397 int err;
398
399 page = f2fs_grab_cache_page(dir->i_mapping, 0, true);
400 if (!page) {
401 f2fs_put_page(ipage, 1);
402 return -ENOMEM;
403 }
404
405 set_new_dnode(&dn, dir, ipage, NULL, 0);
406 err = f2fs_reserve_block(&dn, 0);
407 if (err)
408 goto out;
409
410 if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
411 f2fs_put_dnode(&dn);
412 set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
413 f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
414 __func__, dir->i_ino, dn.data_blkaddr);
415 f2fs_handle_error(F2FS_P_SB(page), ERROR_INVALID_BLKADDR);
416 err = -EFSCORRUPTED;
417 goto out;
418 }
419
420 f2fs_wait_on_page_writeback(page, DATA, true, true);
421
422 dentry_blk = page_address(page);
423
424 /*
425 * Start by zeroing the full block, to ensure that all unused space is
426 * zeroed and no uninitialized memory is leaked to disk.
427 */
428 memset(dentry_blk, 0, F2FS_BLKSIZE);
429
430 make_dentry_ptr_inline(dir, &src, inline_dentry);
431 make_dentry_ptr_block(dir, &dst, dentry_blk);
432
433 /* copy data from inline dentry block to new dentry block */
434 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
435 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
436 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
437
438 if (!PageUptodate(page))
439 SetPageUptodate(page);
440 set_page_dirty(page);
441
442 /* clear inline dir and flag after data writeback */
443 f2fs_truncate_inline_inode(dir, ipage, 0);
444
445 stat_dec_inline_dir(dir);
446 clear_inode_flag(dir, FI_INLINE_DENTRY);
447
448 /*
449 * should retrieve reserved space which was used to keep
450 * inline_dentry's structure for backward compatibility.
451 */
452 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
453 !f2fs_has_inline_xattr(dir))
454 F2FS_I(dir)->i_inline_xattr_size = 0;
455
456 f2fs_i_depth_write(dir, 1);
457 if (i_size_read(dir) < PAGE_SIZE)
458 f2fs_i_size_write(dir, PAGE_SIZE);
459 out:
460 f2fs_put_page(page, 1);
461 return err;
462 }
463
f2fs_add_inline_entries(struct inode * dir,void * inline_dentry)464 static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
465 {
466 struct f2fs_dentry_ptr d;
467 unsigned long bit_pos = 0;
468 int err = 0;
469
470 make_dentry_ptr_inline(dir, &d, inline_dentry);
471
472 while (bit_pos < d.max) {
473 struct f2fs_dir_entry *de;
474 struct f2fs_filename fname;
475 nid_t ino;
476 umode_t fake_mode;
477
478 if (!test_bit_le(bit_pos, d.bitmap)) {
479 bit_pos++;
480 continue;
481 }
482
483 de = &d.dentry[bit_pos];
484
485 if (unlikely(!de->name_len)) {
486 bit_pos++;
487 continue;
488 }
489
490 /*
491 * We only need the disk_name and hash to move the dentry.
492 * We don't need the original or casefolded filenames.
493 */
494 memset(&fname, 0, sizeof(fname));
495 fname.disk_name.name = d.filename[bit_pos];
496 fname.disk_name.len = le16_to_cpu(de->name_len);
497 fname.hash = de->hash_code;
498
499 ino = le32_to_cpu(de->ino);
500 fake_mode = f2fs_get_de_type(de) << S_SHIFT;
501
502 err = f2fs_add_regular_entry(dir, &fname, NULL, ino, fake_mode);
503 if (err)
504 goto punch_dentry_pages;
505
506 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
507 }
508 return 0;
509 punch_dentry_pages:
510 truncate_inode_pages(&dir->i_data, 0);
511 f2fs_truncate_blocks(dir, 0, false);
512 f2fs_remove_dirty_inode(dir);
513 return err;
514 }
515
f2fs_move_rehashed_dirents(struct inode * dir,struct page * ipage,void * inline_dentry)516 static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
517 void *inline_dentry)
518 {
519 void *backup_dentry;
520 int err;
521
522 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
523 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
524 if (!backup_dentry) {
525 f2fs_put_page(ipage, 1);
526 return -ENOMEM;
527 }
528
529 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
530 f2fs_truncate_inline_inode(dir, ipage, 0);
531
532 unlock_page(ipage);
533
534 err = f2fs_add_inline_entries(dir, backup_dentry);
535 if (err)
536 goto recover;
537
538 lock_page(ipage);
539
540 stat_dec_inline_dir(dir);
541 clear_inode_flag(dir, FI_INLINE_DENTRY);
542
543 /*
544 * should retrieve reserved space which was used to keep
545 * inline_dentry's structure for backward compatibility.
546 */
547 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
548 !f2fs_has_inline_xattr(dir))
549 F2FS_I(dir)->i_inline_xattr_size = 0;
550
551 kfree(backup_dentry);
552 return 0;
553 recover:
554 lock_page(ipage);
555 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
556 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
557 f2fs_i_depth_write(dir, 0);
558 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
559 set_page_dirty(ipage);
560 f2fs_put_page(ipage, 1);
561
562 kfree(backup_dentry);
563 return err;
564 }
565
do_convert_inline_dir(struct inode * dir,struct page * ipage,void * inline_dentry)566 static int do_convert_inline_dir(struct inode *dir, struct page *ipage,
567 void *inline_dentry)
568 {
569 if (!F2FS_I(dir)->i_dir_level)
570 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
571 else
572 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
573 }
574
f2fs_try_convert_inline_dir(struct inode * dir,struct dentry * dentry)575 int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry)
576 {
577 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
578 struct page *ipage;
579 struct f2fs_filename fname;
580 void *inline_dentry = NULL;
581 int err = 0;
582
583 if (!f2fs_has_inline_dentry(dir))
584 return 0;
585
586 f2fs_lock_op(sbi);
587
588 err = f2fs_setup_filename(dir, &dentry->d_name, 0, &fname);
589 if (err)
590 goto out;
591
592 ipage = f2fs_get_node_page(sbi, dir->i_ino);
593 if (IS_ERR(ipage)) {
594 err = PTR_ERR(ipage);
595 goto out_fname;
596 }
597
598 if (f2fs_has_enough_room(dir, ipage, &fname)) {
599 f2fs_put_page(ipage, 1);
600 goto out_fname;
601 }
602
603 inline_dentry = inline_data_addr(dir, ipage);
604
605 err = do_convert_inline_dir(dir, ipage, inline_dentry);
606 if (!err)
607 f2fs_put_page(ipage, 1);
608 out_fname:
609 f2fs_free_filename(&fname);
610 out:
611 f2fs_unlock_op(sbi);
612 return err;
613 }
614
f2fs_add_inline_entry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)615 int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
616 struct inode *inode, nid_t ino, umode_t mode)
617 {
618 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
619 struct page *ipage;
620 unsigned int bit_pos;
621 void *inline_dentry = NULL;
622 struct f2fs_dentry_ptr d;
623 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
624 struct page *page = NULL;
625 int err = 0;
626
627 ipage = f2fs_get_node_page(sbi, dir->i_ino);
628 if (IS_ERR(ipage))
629 return PTR_ERR(ipage);
630
631 inline_dentry = inline_data_addr(dir, ipage);
632 make_dentry_ptr_inline(dir, &d, inline_dentry);
633
634 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
635 if (bit_pos >= d.max) {
636 err = do_convert_inline_dir(dir, ipage, inline_dentry);
637 if (err)
638 return err;
639 err = -EAGAIN;
640 goto out;
641 }
642
643 if (inode) {
644 f2fs_down_write(&F2FS_I(inode)->i_sem);
645 page = f2fs_init_inode_metadata(inode, dir, fname, ipage);
646 if (IS_ERR(page)) {
647 err = PTR_ERR(page);
648 goto fail;
649 }
650 }
651
652 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
653
654 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
655 bit_pos);
656
657 set_page_dirty(ipage);
658
659 /* we don't need to mark_inode_dirty now */
660 if (inode) {
661 f2fs_i_pino_write(inode, dir->i_ino);
662
663 /* synchronize inode page's data from inode cache */
664 if (is_inode_flag_set(inode, FI_NEW_INODE))
665 f2fs_update_inode(inode, page);
666
667 f2fs_put_page(page, 1);
668 }
669
670 f2fs_update_parent_metadata(dir, inode, 0);
671 fail:
672 if (inode)
673 f2fs_up_write(&F2FS_I(inode)->i_sem);
674 out:
675 f2fs_put_page(ipage, 1);
676 return err;
677 }
678
f2fs_delete_inline_entry(struct f2fs_dir_entry * dentry,struct page * page,struct inode * dir,struct inode * inode)679 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
680 struct inode *dir, struct inode *inode)
681 {
682 struct f2fs_dentry_ptr d;
683 void *inline_dentry;
684 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
685 unsigned int bit_pos;
686 int i;
687
688 lock_page(page);
689 f2fs_wait_on_page_writeback(page, NODE, true, true);
690
691 inline_dentry = inline_data_addr(dir, page);
692 make_dentry_ptr_inline(dir, &d, inline_dentry);
693
694 bit_pos = dentry - d.dentry;
695 for (i = 0; i < slots; i++)
696 __clear_bit_le(bit_pos + i, d.bitmap);
697
698 set_page_dirty(page);
699 f2fs_put_page(page, 1);
700
701 dir->i_ctime = dir->i_mtime = current_time(dir);
702 f2fs_mark_inode_dirty_sync(dir, false);
703
704 if (inode)
705 f2fs_drop_nlink(dir, inode);
706 }
707
f2fs_empty_inline_dir(struct inode * dir)708 bool f2fs_empty_inline_dir(struct inode *dir)
709 {
710 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
711 struct page *ipage;
712 unsigned int bit_pos = 2;
713 void *inline_dentry;
714 struct f2fs_dentry_ptr d;
715
716 ipage = f2fs_get_node_page(sbi, dir->i_ino);
717 if (IS_ERR(ipage))
718 return false;
719
720 inline_dentry = inline_data_addr(dir, ipage);
721 make_dentry_ptr_inline(dir, &d, inline_dentry);
722
723 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
724
725 f2fs_put_page(ipage, 1);
726
727 if (bit_pos < d.max)
728 return false;
729
730 return true;
731 }
732
f2fs_read_inline_dir(struct file * file,struct dir_context * ctx,struct fscrypt_str * fstr)733 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
734 struct fscrypt_str *fstr)
735 {
736 struct inode *inode = file_inode(file);
737 struct page *ipage = NULL;
738 struct f2fs_dentry_ptr d;
739 void *inline_dentry = NULL;
740 int err;
741
742 make_dentry_ptr_inline(inode, &d, inline_dentry);
743
744 if (ctx->pos == d.max)
745 return 0;
746
747 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
748 if (IS_ERR(ipage))
749 return PTR_ERR(ipage);
750
751 /*
752 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
753 * ipage without page's lock held.
754 */
755 unlock_page(ipage);
756
757 inline_dentry = inline_data_addr(inode, ipage);
758
759 make_dentry_ptr_inline(inode, &d, inline_dentry);
760
761 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
762 if (!err)
763 ctx->pos = d.max;
764
765 f2fs_put_page(ipage, 0);
766 return err < 0 ? err : 0;
767 }
768
f2fs_inline_data_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,__u64 start,__u64 len)769 int f2fs_inline_data_fiemap(struct inode *inode,
770 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
771 {
772 __u64 byteaddr, ilen;
773 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
774 FIEMAP_EXTENT_LAST;
775 struct node_info ni;
776 struct page *ipage;
777 int err = 0;
778
779 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
780 if (IS_ERR(ipage))
781 return PTR_ERR(ipage);
782
783 if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
784 !f2fs_has_inline_data(inode)) {
785 err = -EAGAIN;
786 goto out;
787 }
788
789 if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
790 err = -EAGAIN;
791 goto out;
792 }
793
794 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
795 if (start >= ilen)
796 goto out;
797 if (start + len < ilen)
798 ilen = start + len;
799 ilen -= start;
800
801 err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni, false);
802 if (err)
803 goto out;
804
805 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
806 byteaddr += (char *)inline_data_addr(inode, ipage) -
807 (char *)F2FS_INODE(ipage);
808 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
809 trace_f2fs_fiemap(inode, start, byteaddr, ilen, flags, err);
810 out:
811 f2fs_put_page(ipage, 1);
812 return err;
813 }
814