1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright (c) 2013, Intel Corporation
5 * Authors: Huajun Li <huajun.li@intel.com>
6 * Haicheng Li <haicheng.li@intel.com>
10 #include <linux/f2fs_fs.h>
11 #include <linux/fiemap.h>
15 #include <trace/events/f2fs.h>
17 bool f2fs_may_inline_data(struct inode
*inode
)
19 if (f2fs_is_atomic_file(inode
))
22 if (!S_ISREG(inode
->i_mode
) && !S_ISLNK(inode
->i_mode
))
25 if (i_size_read(inode
) > MAX_INLINE_DATA(inode
))
28 if (f2fs_post_read_required(inode
))
34 bool f2fs_may_inline_dentry(struct inode
*inode
)
36 if (!test_opt(F2FS_I_SB(inode
), INLINE_DENTRY
))
39 if (!S_ISDIR(inode
->i_mode
))
45 void f2fs_do_read_inline_data(struct page
*page
, struct page
*ipage
)
47 struct inode
*inode
= page
->mapping
->host
;
48 void *src_addr
, *dst_addr
;
50 if (PageUptodate(page
))
53 f2fs_bug_on(F2FS_P_SB(page
), page
->index
);
55 zero_user_segment(page
, MAX_INLINE_DATA(inode
), PAGE_SIZE
);
57 /* Copy the whole inline data block */
58 src_addr
= inline_data_addr(inode
, ipage
);
59 dst_addr
= kmap_atomic(page
);
60 memcpy(dst_addr
, src_addr
, MAX_INLINE_DATA(inode
));
61 flush_dcache_page(page
);
62 kunmap_atomic(dst_addr
);
63 if (!PageUptodate(page
))
64 SetPageUptodate(page
);
67 void f2fs_truncate_inline_inode(struct inode
*inode
,
68 struct page
*ipage
, u64 from
)
72 if (from
>= MAX_INLINE_DATA(inode
))
75 addr
= inline_data_addr(inode
, ipage
);
77 f2fs_wait_on_page_writeback(ipage
, NODE
, true, true);
78 memset(addr
+ from
, 0, MAX_INLINE_DATA(inode
) - from
);
79 set_page_dirty(ipage
);
82 clear_inode_flag(inode
, FI_DATA_EXIST
);
85 int f2fs_read_inline_data(struct inode
*inode
, struct page
*page
)
89 ipage
= f2fs_get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
92 return PTR_ERR(ipage
);
95 if (!f2fs_has_inline_data(inode
)) {
96 f2fs_put_page(ipage
, 1);
101 zero_user_segment(page
, 0, PAGE_SIZE
);
103 f2fs_do_read_inline_data(page
, ipage
);
105 if (!PageUptodate(page
))
106 SetPageUptodate(page
);
107 f2fs_put_page(ipage
, 1);
112 int f2fs_convert_inline_page(struct dnode_of_data
*dn
, struct page
*page
)
114 struct f2fs_io_info fio
= {
115 .sbi
= F2FS_I_SB(dn
->inode
),
116 .ino
= dn
->inode
->i_ino
,
119 .op_flags
= REQ_SYNC
| REQ_PRIO
,
121 .encrypted_page
= NULL
,
122 .io_type
= FS_DATA_IO
,
127 if (!f2fs_exist_data(dn
->inode
))
130 err
= f2fs_reserve_block(dn
, 0);
134 err
= f2fs_get_node_info(fio
.sbi
, dn
->nid
, &ni
);
136 f2fs_truncate_data_blocks_range(dn
, 1);
141 fio
.version
= ni
.version
;
143 if (unlikely(dn
->data_blkaddr
!= NEW_ADDR
)) {
145 set_sbi_flag(fio
.sbi
, SBI_NEED_FSCK
);
146 f2fs_warn(fio
.sbi
, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
147 __func__
, dn
->inode
->i_ino
, dn
->data_blkaddr
);
148 return -EFSCORRUPTED
;
151 f2fs_bug_on(F2FS_P_SB(page
), PageWriteback(page
));
153 f2fs_do_read_inline_data(page
, dn
->inode_page
);
154 set_page_dirty(page
);
156 /* clear dirty state */
157 dirty
= clear_page_dirty_for_io(page
);
159 /* write data page to try to make data consistent */
160 set_page_writeback(page
);
161 ClearPageError(page
);
162 fio
.old_blkaddr
= dn
->data_blkaddr
;
163 set_inode_flag(dn
->inode
, FI_HOT_DATA
);
164 f2fs_outplace_write_data(dn
, &fio
);
165 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
167 inode_dec_dirty_pages(dn
->inode
);
168 f2fs_remove_dirty_inode(dn
->inode
);
171 /* this converted inline_data should be recovered. */
172 set_inode_flag(dn
->inode
, FI_APPEND_WRITE
);
174 /* clear inline data and flag after data writeback */
175 f2fs_truncate_inline_inode(dn
->inode
, dn
->inode_page
, 0);
176 clear_inline_node(dn
->inode_page
);
178 stat_dec_inline_inode(dn
->inode
);
179 clear_inode_flag(dn
->inode
, FI_INLINE_DATA
);
184 int f2fs_convert_inline_inode(struct inode
*inode
)
186 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
187 struct dnode_of_data dn
;
188 struct page
*ipage
, *page
;
191 if (!f2fs_has_inline_data(inode
) ||
192 f2fs_hw_is_readonly(sbi
) || f2fs_readonly(sbi
->sb
))
195 page
= f2fs_grab_cache_page(inode
->i_mapping
, 0, false);
201 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
203 err
= PTR_ERR(ipage
);
207 set_new_dnode(&dn
, inode
, ipage
, ipage
, 0);
209 if (f2fs_has_inline_data(inode
))
210 err
= f2fs_convert_inline_page(&dn
, page
);
216 f2fs_put_page(page
, 1);
218 f2fs_balance_fs(sbi
, dn
.node_changed
);
223 int f2fs_write_inline_data(struct inode
*inode
, struct page
*page
)
225 void *src_addr
, *dst_addr
;
226 struct dnode_of_data dn
;
229 set_new_dnode(&dn
, inode
, NULL
, NULL
, 0);
230 err
= f2fs_get_dnode_of_data(&dn
, 0, LOOKUP_NODE
);
234 if (!f2fs_has_inline_data(inode
)) {
239 f2fs_bug_on(F2FS_I_SB(inode
), page
->index
);
241 f2fs_wait_on_page_writeback(dn
.inode_page
, NODE
, true, true);
242 src_addr
= kmap_atomic(page
);
243 dst_addr
= inline_data_addr(inode
, dn
.inode_page
);
244 memcpy(dst_addr
, src_addr
, MAX_INLINE_DATA(inode
));
245 kunmap_atomic(src_addr
);
246 set_page_dirty(dn
.inode_page
);
248 f2fs_clear_page_cache_dirty_tag(page
);
250 set_inode_flag(inode
, FI_APPEND_WRITE
);
251 set_inode_flag(inode
, FI_DATA_EXIST
);
253 clear_inline_node(dn
.inode_page
);
258 int f2fs_recover_inline_data(struct inode
*inode
, struct page
*npage
)
260 struct f2fs_sb_info
*sbi
= F2FS_I_SB(inode
);
261 struct f2fs_inode
*ri
= NULL
;
262 void *src_addr
, *dst_addr
;
266 * The inline_data recovery policy is as follows.
267 * [prev.] [next] of inline_data flag
268 * o o -> recover inline_data
269 * o x -> remove inline_data, and then recover data blocks
270 * x o -> remove data blocks, and then recover inline_data
271 * x x -> recover data blocks
274 ri
= F2FS_INODE(npage
);
276 if (f2fs_has_inline_data(inode
) &&
277 ri
&& (ri
->i_inline
& F2FS_INLINE_DATA
)) {
279 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
281 return PTR_ERR(ipage
);
283 f2fs_wait_on_page_writeback(ipage
, NODE
, true, true);
285 src_addr
= inline_data_addr(inode
, npage
);
286 dst_addr
= inline_data_addr(inode
, ipage
);
287 memcpy(dst_addr
, src_addr
, MAX_INLINE_DATA(inode
));
289 set_inode_flag(inode
, FI_INLINE_DATA
);
290 set_inode_flag(inode
, FI_DATA_EXIST
);
292 set_page_dirty(ipage
);
293 f2fs_put_page(ipage
, 1);
297 if (f2fs_has_inline_data(inode
)) {
298 ipage
= f2fs_get_node_page(sbi
, inode
->i_ino
);
300 return PTR_ERR(ipage
);
301 f2fs_truncate_inline_inode(inode
, ipage
, 0);
302 stat_dec_inline_inode(inode
);
303 clear_inode_flag(inode
, FI_INLINE_DATA
);
304 f2fs_put_page(ipage
, 1);
305 } else if (ri
&& (ri
->i_inline
& F2FS_INLINE_DATA
)) {
308 ret
= f2fs_truncate_blocks(inode
, 0, false);
311 stat_inc_inline_inode(inode
);
317 struct f2fs_dir_entry
*f2fs_find_in_inline_dir(struct inode
*dir
,
318 const struct f2fs_filename
*fname
,
319 struct page
**res_page
)
321 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
322 struct f2fs_dir_entry
*de
;
323 struct f2fs_dentry_ptr d
;
327 ipage
= f2fs_get_node_page(sbi
, dir
->i_ino
);
333 inline_dentry
= inline_data_addr(dir
, ipage
);
335 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
336 de
= f2fs_find_target_dentry(&d
, fname
, NULL
);
339 *res_page
= ERR_CAST(de
);
345 f2fs_put_page(ipage
, 0);
350 int f2fs_make_empty_inline_dir(struct inode
*inode
, struct inode
*parent
,
353 struct f2fs_dentry_ptr d
;
356 inline_dentry
= inline_data_addr(inode
, ipage
);
358 make_dentry_ptr_inline(inode
, &d
, inline_dentry
);
359 f2fs_do_make_empty_dir(inode
, parent
, &d
);
361 set_page_dirty(ipage
);
363 /* update i_size to MAX_INLINE_DATA */
364 if (i_size_read(inode
) < MAX_INLINE_DATA(inode
))
365 f2fs_i_size_write(inode
, MAX_INLINE_DATA(inode
));
370 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
371 * release ipage in this function.
373 static int f2fs_move_inline_dirents(struct inode
*dir
, struct page
*ipage
,
377 struct dnode_of_data dn
;
378 struct f2fs_dentry_block
*dentry_blk
;
379 struct f2fs_dentry_ptr src
, dst
;
382 page
= f2fs_grab_cache_page(dir
->i_mapping
, 0, true);
384 f2fs_put_page(ipage
, 1);
388 set_new_dnode(&dn
, dir
, ipage
, NULL
, 0);
389 err
= f2fs_reserve_block(&dn
, 0);
393 if (unlikely(dn
.data_blkaddr
!= NEW_ADDR
)) {
395 set_sbi_flag(F2FS_P_SB(page
), SBI_NEED_FSCK
);
396 f2fs_warn(F2FS_P_SB(page
), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
397 __func__
, dir
->i_ino
, dn
.data_blkaddr
);
402 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
404 dentry_blk
= page_address(page
);
406 make_dentry_ptr_inline(dir
, &src
, inline_dentry
);
407 make_dentry_ptr_block(dir
, &dst
, dentry_blk
);
409 /* copy data from inline dentry block to new dentry block */
410 memcpy(dst
.bitmap
, src
.bitmap
, src
.nr_bitmap
);
411 memset(dst
.bitmap
+ src
.nr_bitmap
, 0, dst
.nr_bitmap
- src
.nr_bitmap
);
413 * we do not need to zero out remainder part of dentry and filename
414 * field, since we have used bitmap for marking the usage status of
415 * them, besides, we can also ignore copying/zeroing reserved space
416 * of dentry block, because them haven't been used so far.
418 memcpy(dst
.dentry
, src
.dentry
, SIZE_OF_DIR_ENTRY
* src
.max
);
419 memcpy(dst
.filename
, src
.filename
, src
.max
* F2FS_SLOT_LEN
);
421 if (!PageUptodate(page
))
422 SetPageUptodate(page
);
423 set_page_dirty(page
);
425 /* clear inline dir and flag after data writeback */
426 f2fs_truncate_inline_inode(dir
, ipage
, 0);
428 stat_dec_inline_dir(dir
);
429 clear_inode_flag(dir
, FI_INLINE_DENTRY
);
432 * should retrieve reserved space which was used to keep
433 * inline_dentry's structure for backward compatibility.
435 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir
)) &&
436 !f2fs_has_inline_xattr(dir
))
437 F2FS_I(dir
)->i_inline_xattr_size
= 0;
439 f2fs_i_depth_write(dir
, 1);
440 if (i_size_read(dir
) < PAGE_SIZE
)
441 f2fs_i_size_write(dir
, PAGE_SIZE
);
443 f2fs_put_page(page
, 1);
447 static int f2fs_add_inline_entries(struct inode
*dir
, void *inline_dentry
)
449 struct f2fs_dentry_ptr d
;
450 unsigned long bit_pos
= 0;
453 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
455 while (bit_pos
< d
.max
) {
456 struct f2fs_dir_entry
*de
;
457 struct f2fs_filename fname
;
461 if (!test_bit_le(bit_pos
, d
.bitmap
)) {
466 de
= &d
.dentry
[bit_pos
];
468 if (unlikely(!de
->name_len
)) {
474 * We only need the disk_name and hash to move the dentry.
475 * We don't need the original or casefolded filenames.
477 memset(&fname
, 0, sizeof(fname
));
478 fname
.disk_name
.name
= d
.filename
[bit_pos
];
479 fname
.disk_name
.len
= le16_to_cpu(de
->name_len
);
480 fname
.hash
= de
->hash_code
;
482 ino
= le32_to_cpu(de
->ino
);
483 fake_mode
= f2fs_get_de_type(de
) << S_SHIFT
;
485 err
= f2fs_add_regular_entry(dir
, &fname
, NULL
, ino
, fake_mode
);
487 goto punch_dentry_pages
;
489 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
493 truncate_inode_pages(&dir
->i_data
, 0);
494 f2fs_truncate_blocks(dir
, 0, false);
495 f2fs_remove_dirty_inode(dir
);
499 static int f2fs_move_rehashed_dirents(struct inode
*dir
, struct page
*ipage
,
505 backup_dentry
= f2fs_kmalloc(F2FS_I_SB(dir
),
506 MAX_INLINE_DATA(dir
), GFP_F2FS_ZERO
);
507 if (!backup_dentry
) {
508 f2fs_put_page(ipage
, 1);
512 memcpy(backup_dentry
, inline_dentry
, MAX_INLINE_DATA(dir
));
513 f2fs_truncate_inline_inode(dir
, ipage
, 0);
517 err
= f2fs_add_inline_entries(dir
, backup_dentry
);
523 stat_dec_inline_dir(dir
);
524 clear_inode_flag(dir
, FI_INLINE_DENTRY
);
527 * should retrieve reserved space which was used to keep
528 * inline_dentry's structure for backward compatibility.
530 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir
)) &&
531 !f2fs_has_inline_xattr(dir
))
532 F2FS_I(dir
)->i_inline_xattr_size
= 0;
534 kfree(backup_dentry
);
538 f2fs_wait_on_page_writeback(ipage
, NODE
, true, true);
539 memcpy(inline_dentry
, backup_dentry
, MAX_INLINE_DATA(dir
));
540 f2fs_i_depth_write(dir
, 0);
541 f2fs_i_size_write(dir
, MAX_INLINE_DATA(dir
));
542 set_page_dirty(ipage
);
543 f2fs_put_page(ipage
, 1);
545 kfree(backup_dentry
);
549 static int do_convert_inline_dir(struct inode
*dir
, struct page
*ipage
,
552 if (!F2FS_I(dir
)->i_dir_level
)
553 return f2fs_move_inline_dirents(dir
, ipage
, inline_dentry
);
555 return f2fs_move_rehashed_dirents(dir
, ipage
, inline_dentry
);
558 int f2fs_try_convert_inline_dir(struct inode
*dir
, struct dentry
*dentry
)
560 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
562 struct f2fs_filename fname
;
563 void *inline_dentry
= NULL
;
566 if (!f2fs_has_inline_dentry(dir
))
571 err
= f2fs_setup_filename(dir
, &dentry
->d_name
, 0, &fname
);
575 ipage
= f2fs_get_node_page(sbi
, dir
->i_ino
);
577 err
= PTR_ERR(ipage
);
581 if (f2fs_has_enough_room(dir
, ipage
, &fname
)) {
582 f2fs_put_page(ipage
, 1);
586 inline_dentry
= inline_data_addr(dir
, ipage
);
588 err
= do_convert_inline_dir(dir
, ipage
, inline_dentry
);
590 f2fs_put_page(ipage
, 1);
592 f2fs_free_filename(&fname
);
598 int f2fs_add_inline_entry(struct inode
*dir
, const struct f2fs_filename
*fname
,
599 struct inode
*inode
, nid_t ino
, umode_t mode
)
601 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
603 unsigned int bit_pos
;
604 void *inline_dentry
= NULL
;
605 struct f2fs_dentry_ptr d
;
606 int slots
= GET_DENTRY_SLOTS(fname
->disk_name
.len
);
607 struct page
*page
= NULL
;
610 ipage
= f2fs_get_node_page(sbi
, dir
->i_ino
);
612 return PTR_ERR(ipage
);
614 inline_dentry
= inline_data_addr(dir
, ipage
);
615 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
617 bit_pos
= f2fs_room_for_filename(d
.bitmap
, slots
, d
.max
);
618 if (bit_pos
>= d
.max
) {
619 err
= do_convert_inline_dir(dir
, ipage
, inline_dentry
);
627 down_write(&F2FS_I(inode
)->i_sem
);
628 page
= f2fs_init_inode_metadata(inode
, dir
, fname
, ipage
);
635 f2fs_wait_on_page_writeback(ipage
, NODE
, true, true);
637 f2fs_update_dentry(ino
, mode
, &d
, &fname
->disk_name
, fname
->hash
,
640 set_page_dirty(ipage
);
642 /* we don't need to mark_inode_dirty now */
644 f2fs_i_pino_write(inode
, dir
->i_ino
);
646 /* synchronize inode page's data from inode cache */
647 if (is_inode_flag_set(inode
, FI_NEW_INODE
))
648 f2fs_update_inode(inode
, page
);
650 f2fs_put_page(page
, 1);
653 f2fs_update_parent_metadata(dir
, inode
, 0);
656 up_write(&F2FS_I(inode
)->i_sem
);
658 f2fs_put_page(ipage
, 1);
662 void f2fs_delete_inline_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
663 struct inode
*dir
, struct inode
*inode
)
665 struct f2fs_dentry_ptr d
;
667 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
668 unsigned int bit_pos
;
672 f2fs_wait_on_page_writeback(page
, NODE
, true, true);
674 inline_dentry
= inline_data_addr(dir
, page
);
675 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
677 bit_pos
= dentry
- d
.dentry
;
678 for (i
= 0; i
< slots
; i
++)
679 __clear_bit_le(bit_pos
+ i
, d
.bitmap
);
681 set_page_dirty(page
);
682 f2fs_put_page(page
, 1);
684 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
685 f2fs_mark_inode_dirty_sync(dir
, false);
688 f2fs_drop_nlink(dir
, inode
);
691 bool f2fs_empty_inline_dir(struct inode
*dir
)
693 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
695 unsigned int bit_pos
= 2;
697 struct f2fs_dentry_ptr d
;
699 ipage
= f2fs_get_node_page(sbi
, dir
->i_ino
);
703 inline_dentry
= inline_data_addr(dir
, ipage
);
704 make_dentry_ptr_inline(dir
, &d
, inline_dentry
);
706 bit_pos
= find_next_bit_le(d
.bitmap
, d
.max
, bit_pos
);
708 f2fs_put_page(ipage
, 1);
716 int f2fs_read_inline_dir(struct file
*file
, struct dir_context
*ctx
,
717 struct fscrypt_str
*fstr
)
719 struct inode
*inode
= file_inode(file
);
720 struct page
*ipage
= NULL
;
721 struct f2fs_dentry_ptr d
;
722 void *inline_dentry
= NULL
;
725 make_dentry_ptr_inline(inode
, &d
, inline_dentry
);
727 if (ctx
->pos
== d
.max
)
730 ipage
= f2fs_get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
732 return PTR_ERR(ipage
);
735 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
736 * ipage without page's lock held.
740 inline_dentry
= inline_data_addr(inode
, ipage
);
742 make_dentry_ptr_inline(inode
, &d
, inline_dentry
);
744 err
= f2fs_fill_dentries(ctx
, &d
, 0, fstr
);
748 f2fs_put_page(ipage
, 0);
749 return err
< 0 ? err
: 0;
752 int f2fs_inline_data_fiemap(struct inode
*inode
,
753 struct fiemap_extent_info
*fieinfo
, __u64 start
, __u64 len
)
755 __u64 byteaddr
, ilen
;
756 __u32 flags
= FIEMAP_EXTENT_DATA_INLINE
| FIEMAP_EXTENT_NOT_ALIGNED
|
762 ipage
= f2fs_get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
764 return PTR_ERR(ipage
);
766 if ((S_ISREG(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) &&
767 !f2fs_has_inline_data(inode
)) {
772 if (S_ISDIR(inode
->i_mode
) && !f2fs_has_inline_dentry(inode
)) {
777 ilen
= min_t(size_t, MAX_INLINE_DATA(inode
), i_size_read(inode
));
780 if (start
+ len
< ilen
)
784 err
= f2fs_get_node_info(F2FS_I_SB(inode
), inode
->i_ino
, &ni
);
788 byteaddr
= (__u64
)ni
.blk_addr
<< inode
->i_sb
->s_blocksize_bits
;
789 byteaddr
+= (char *)inline_data_addr(inode
, ipage
) -
790 (char *)F2FS_INODE(ipage
);
791 err
= fiemap_fill_next_extent(fieinfo
, start
, byteaddr
, ilen
, flags
);
792 trace_f2fs_fiemap(inode
, start
, byteaddr
, ilen
, flags
, err
);
794 f2fs_put_page(ipage
, 1);