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 static bool support_inline_data(struct inode *inode)
19 if (f2fs_is_atomic_file(inode))
21 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
23 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
28 bool f2fs_may_inline_data(struct inode *inode)
30 if (!support_inline_data(inode))
33 return !f2fs_post_read_required(inode);
36 bool f2fs_sanity_check_inline_data(struct inode *inode)
38 if (!f2fs_has_inline_data(inode))
41 if (!support_inline_data(inode))
45 * used by sanity_check_inode(), when disk layout fields has not
46 * been synchronized to inmem fields.
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)));
53 bool f2fs_may_inline_dentry(struct inode *inode)
55 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
58 if (!S_ISDIR(inode->i_mode))
64 void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
66 struct inode *inode = page->mapping->host;
68 if (PageUptodate(page))
71 f2fs_bug_on(F2FS_P_SB(page), page->index);
73 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
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);
82 void f2fs_truncate_inline_inode(struct inode *inode,
83 struct page *ipage, u64 from)
87 if (from >= MAX_INLINE_DATA(inode))
90 addr = inline_data_addr(inode, ipage);
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);
97 clear_inode_flag(inode, FI_DATA_EXIST);
100 int f2fs_read_inline_data(struct inode *inode, struct page *page)
104 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
107 return PTR_ERR(ipage);
110 if (!f2fs_has_inline_data(inode)) {
111 f2fs_put_page(ipage, 1);
116 zero_user_segment(page, 0, PAGE_SIZE);
118 f2fs_do_read_inline_data(page, ipage);
120 if (!PageUptodate(page))
121 SetPageUptodate(page);
122 f2fs_put_page(ipage, 1);
127 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
129 struct f2fs_io_info fio = {
130 .sbi = F2FS_I_SB(dn->inode),
131 .ino = dn->inode->i_ino,
134 .op_flags = REQ_SYNC | REQ_PRIO,
136 .encrypted_page = NULL,
137 .io_type = FS_DATA_IO,
142 if (!f2fs_exist_data(dn->inode))
145 err = f2fs_reserve_block(dn, 0);
149 err = f2fs_get_node_info(fio.sbi, dn->nid, &ni, false);
151 f2fs_truncate_data_blocks_range(dn, 1);
156 fio.version = ni.version;
158 if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
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;
167 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
169 f2fs_do_read_inline_data(page, dn->inode_page);
170 set_page_dirty(page);
172 /* clear dirty state */
173 dirty = clear_page_dirty_for_io(page);
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);
182 inode_dec_dirty_pages(dn->inode);
183 f2fs_remove_dirty_inode(dn->inode);
186 /* this converted inline_data should be recovered. */
187 set_inode_flag(dn->inode, FI_APPEND_WRITE);
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);
193 stat_dec_inline_inode(dn->inode);
194 clear_inode_flag(dn->inode, FI_INLINE_DATA);
199 int f2fs_convert_inline_inode(struct inode *inode)
201 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
202 struct dnode_of_data dn;
203 struct page *ipage, *page;
206 if (!f2fs_has_inline_data(inode) ||
207 f2fs_hw_is_readonly(sbi) || f2fs_readonly(sbi->sb))
210 err = f2fs_dquot_initialize(inode);
214 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
220 ipage = f2fs_get_node_page(sbi, inode->i_ino);
222 err = PTR_ERR(ipage);
226 set_new_dnode(&dn, inode, ipage, ipage, 0);
228 if (f2fs_has_inline_data(inode))
229 err = f2fs_convert_inline_page(&dn, page);
235 f2fs_put_page(page, 1);
238 f2fs_balance_fs(sbi, dn.node_changed);
243 int f2fs_write_inline_data(struct inode *inode, struct page *page)
245 struct dnode_of_data dn;
248 set_new_dnode(&dn, inode, NULL, NULL, 0);
249 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
253 if (!f2fs_has_inline_data(inode)) {
258 f2fs_bug_on(F2FS_I_SB(inode), page->index);
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);
265 f2fs_clear_page_cache_dirty_tag(page);
267 set_inode_flag(inode, FI_APPEND_WRITE);
268 set_inode_flag(inode, FI_DATA_EXIST);
270 clear_page_private_inline(dn.inode_page);
275 int f2fs_recover_inline_data(struct inode *inode, struct page *npage)
277 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
278 struct f2fs_inode *ri = NULL;
279 void *src_addr, *dst_addr;
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
291 ri = F2FS_INODE(npage);
293 if (f2fs_has_inline_data(inode) &&
294 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
296 ipage = f2fs_get_node_page(sbi, inode->i_ino);
298 return PTR_ERR(ipage);
300 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
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));
306 set_inode_flag(inode, FI_INLINE_DATA);
307 set_inode_flag(inode, FI_DATA_EXIST);
309 set_page_dirty(ipage);
310 f2fs_put_page(ipage, 1);
314 if (f2fs_has_inline_data(inode)) {
315 ipage = f2fs_get_node_page(sbi, inode->i_ino);
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)) {
325 ret = f2fs_truncate_blocks(inode, 0, false);
328 stat_inc_inline_inode(inode);
334 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
335 const struct f2fs_filename *fname,
336 struct page **res_page)
338 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
339 struct f2fs_dir_entry *de;
340 struct f2fs_dentry_ptr d;
344 ipage = f2fs_get_node_page(sbi, dir->i_ino);
350 inline_dentry = inline_data_addr(dir, ipage);
352 make_dentry_ptr_inline(dir, &d, inline_dentry);
353 de = f2fs_find_target_dentry(&d, fname, NULL);
356 *res_page = ERR_CAST(de);
362 f2fs_put_page(ipage, 0);
367 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
370 struct f2fs_dentry_ptr d;
373 inline_dentry = inline_data_addr(inode, ipage);
375 make_dentry_ptr_inline(inode, &d, inline_dentry);
376 f2fs_do_make_empty_dir(inode, parent, &d);
378 set_page_dirty(ipage);
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));
387 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
388 * release ipage in this function.
390 static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
394 struct dnode_of_data dn;
395 struct f2fs_dentry_block *dentry_blk;
396 struct f2fs_dentry_ptr src, dst;
399 page = f2fs_grab_cache_page(dir->i_mapping, 0, true);
401 f2fs_put_page(ipage, 1);
405 set_new_dnode(&dn, dir, ipage, NULL, 0);
406 err = f2fs_reserve_block(&dn, 0);
410 if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
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);
420 f2fs_wait_on_page_writeback(page, DATA, true, true);
422 dentry_blk = page_address(page);
425 * Start by zeroing the full block, to ensure that all unused space is
426 * zeroed and no uninitialized memory is leaked to disk.
428 memset(dentry_blk, 0, F2FS_BLKSIZE);
430 make_dentry_ptr_inline(dir, &src, inline_dentry);
431 make_dentry_ptr_block(dir, &dst, dentry_blk);
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);
438 if (!PageUptodate(page))
439 SetPageUptodate(page);
440 set_page_dirty(page);
442 /* clear inline dir and flag after data writeback */
443 f2fs_truncate_inline_inode(dir, ipage, 0);
445 stat_dec_inline_dir(dir);
446 clear_inode_flag(dir, FI_INLINE_DENTRY);
449 * should retrieve reserved space which was used to keep
450 * inline_dentry's structure for backward compatibility.
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;
456 f2fs_i_depth_write(dir, 1);
457 if (i_size_read(dir) < PAGE_SIZE)
458 f2fs_i_size_write(dir, PAGE_SIZE);
460 f2fs_put_page(page, 1);
464 static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
466 struct f2fs_dentry_ptr d;
467 unsigned long bit_pos = 0;
470 make_dentry_ptr_inline(dir, &d, inline_dentry);
472 while (bit_pos < d.max) {
473 struct f2fs_dir_entry *de;
474 struct f2fs_filename fname;
478 if (!test_bit_le(bit_pos, d.bitmap)) {
483 de = &d.dentry[bit_pos];
485 if (unlikely(!de->name_len)) {
491 * We only need the disk_name and hash to move the dentry.
492 * We don't need the original or casefolded filenames.
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;
499 ino = le32_to_cpu(de->ino);
500 fake_mode = fs_ftype_to_dtype(de->file_type) << S_DT_SHIFT;
502 err = f2fs_add_regular_entry(dir, &fname, NULL, ino, fake_mode);
504 goto punch_dentry_pages;
506 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
510 truncate_inode_pages(&dir->i_data, 0);
511 f2fs_truncate_blocks(dir, 0, false);
512 f2fs_remove_dirty_inode(dir);
516 static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
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);
529 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
530 f2fs_truncate_inline_inode(dir, ipage, 0);
534 err = f2fs_add_inline_entries(dir, backup_dentry);
540 stat_dec_inline_dir(dir);
541 clear_inode_flag(dir, FI_INLINE_DENTRY);
544 * should retrieve reserved space which was used to keep
545 * inline_dentry's structure for backward compatibility.
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;
551 kfree(backup_dentry);
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);
562 kfree(backup_dentry);
566 static int do_convert_inline_dir(struct inode *dir, struct page *ipage,
569 if (!F2FS_I(dir)->i_dir_level)
570 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
572 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
575 int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry)
577 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
579 struct f2fs_filename fname;
580 void *inline_dentry = NULL;
583 if (!f2fs_has_inline_dentry(dir))
588 err = f2fs_setup_filename(dir, &dentry->d_name, 0, &fname);
592 ipage = f2fs_get_node_page(sbi, dir->i_ino);
594 err = PTR_ERR(ipage);
598 if (f2fs_has_enough_room(dir, ipage, &fname)) {
599 f2fs_put_page(ipage, 1);
603 inline_dentry = inline_data_addr(dir, ipage);
605 err = do_convert_inline_dir(dir, ipage, inline_dentry);
607 f2fs_put_page(ipage, 1);
609 f2fs_free_filename(&fname);
615 int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
616 struct inode *inode, nid_t ino, umode_t mode)
618 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
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;
627 ipage = f2fs_get_node_page(sbi, dir->i_ino);
629 return PTR_ERR(ipage);
631 inline_dentry = inline_data_addr(dir, ipage);
632 make_dentry_ptr_inline(dir, &d, inline_dentry);
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);
644 f2fs_down_write_nested(&F2FS_I(inode)->i_sem,
645 SINGLE_DEPTH_NESTING);
646 page = f2fs_init_inode_metadata(inode, dir, fname, ipage);
653 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
655 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
658 set_page_dirty(ipage);
660 /* we don't need to mark_inode_dirty now */
662 f2fs_i_pino_write(inode, dir->i_ino);
664 /* synchronize inode page's data from inode cache */
665 if (is_inode_flag_set(inode, FI_NEW_INODE))
666 f2fs_update_inode(inode, page);
668 f2fs_put_page(page, 1);
671 f2fs_update_parent_metadata(dir, inode, 0);
674 f2fs_up_write(&F2FS_I(inode)->i_sem);
676 f2fs_put_page(ipage, 1);
680 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
681 struct inode *dir, struct inode *inode)
683 struct f2fs_dentry_ptr d;
685 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
686 unsigned int bit_pos;
690 f2fs_wait_on_page_writeback(page, NODE, true, true);
692 inline_dentry = inline_data_addr(dir, page);
693 make_dentry_ptr_inline(dir, &d, inline_dentry);
695 bit_pos = dentry - d.dentry;
696 for (i = 0; i < slots; i++)
697 __clear_bit_le(bit_pos + i, d.bitmap);
699 set_page_dirty(page);
700 f2fs_put_page(page, 1);
702 dir->i_mtime = inode_set_ctime_current(dir);
703 f2fs_mark_inode_dirty_sync(dir, false);
706 f2fs_drop_nlink(dir, inode);
709 bool f2fs_empty_inline_dir(struct inode *dir)
711 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
713 unsigned int bit_pos = 2;
715 struct f2fs_dentry_ptr d;
717 ipage = f2fs_get_node_page(sbi, dir->i_ino);
721 inline_dentry = inline_data_addr(dir, ipage);
722 make_dentry_ptr_inline(dir, &d, inline_dentry);
724 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
726 f2fs_put_page(ipage, 1);
734 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
735 struct fscrypt_str *fstr)
737 struct inode *inode = file_inode(file);
738 struct page *ipage = NULL;
739 struct f2fs_dentry_ptr d;
740 void *inline_dentry = NULL;
743 make_dentry_ptr_inline(inode, &d, inline_dentry);
745 if (ctx->pos == d.max)
748 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
750 return PTR_ERR(ipage);
753 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
754 * ipage without page's lock held.
758 inline_dentry = inline_data_addr(inode, ipage);
760 make_dentry_ptr_inline(inode, &d, inline_dentry);
762 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
766 f2fs_put_page(ipage, 0);
767 return err < 0 ? err : 0;
770 int f2fs_inline_data_fiemap(struct inode *inode,
771 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
773 __u64 byteaddr, ilen;
774 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
780 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
782 return PTR_ERR(ipage);
784 if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
785 !f2fs_has_inline_data(inode)) {
790 if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
795 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
798 if (start + len < ilen)
802 err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni, false);
806 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
807 byteaddr += (char *)inline_data_addr(inode, ipage) -
808 (char *)F2FS_INODE(ipage);
809 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
810 trace_f2fs_fiemap(inode, start, byteaddr, ilen, flags, err);
812 f2fs_put_page(ipage, 1);