1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
8 #include <asm/unaligned.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/sched/signal.h>
12 #include <linux/unicode.h>
17 #include <trace/events/f2fs.h>
19 #if IS_ENABLED(CONFIG_UNICODE)
20 extern struct kmem_cache *f2fs_cf_name_slab;
23 static unsigned long dir_blocks(struct inode *inode)
25 return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
29 static unsigned int dir_buckets(unsigned int level, int dir_level)
31 if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
32 return BIT(level + dir_level);
34 return MAX_DIR_BUCKETS;
37 static unsigned int bucket_blocks(unsigned int level)
39 if (level < MAX_DIR_HASH_DEPTH / 2)
45 /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
46 int f2fs_init_casefolded_name(const struct inode *dir,
47 struct f2fs_filename *fname)
49 #if IS_ENABLED(CONFIG_UNICODE)
50 struct super_block *sb = dir->i_sb;
52 if (IS_CASEFOLDED(dir) &&
53 !is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) {
54 fname->cf_name.name = f2fs_kmem_cache_alloc(f2fs_cf_name_slab,
55 GFP_NOFS, false, F2FS_SB(sb));
56 if (!fname->cf_name.name)
58 fname->cf_name.len = utf8_casefold(sb->s_encoding,
62 if ((int)fname->cf_name.len <= 0) {
63 kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
64 fname->cf_name.name = NULL;
65 if (sb_has_strict_encoding(sb))
67 /* fall back to treating name as opaque byte sequence */
74 static int __f2fs_setup_filename(const struct inode *dir,
75 const struct fscrypt_name *crypt_name,
76 struct f2fs_filename *fname)
80 memset(fname, 0, sizeof(*fname));
82 fname->usr_fname = crypt_name->usr_fname;
83 fname->disk_name = crypt_name->disk_name;
84 #ifdef CONFIG_FS_ENCRYPTION
85 fname->crypto_buf = crypt_name->crypto_buf;
87 if (crypt_name->is_nokey_name) {
88 /* hash was decoded from the no-key name */
89 fname->hash = cpu_to_le32(crypt_name->hash);
91 err = f2fs_init_casefolded_name(dir, fname);
93 f2fs_free_filename(fname);
96 f2fs_hash_filename(dir, fname);
102 * Prepare to search for @iname in @dir. This is similar to
103 * fscrypt_setup_filename(), but this also handles computing the casefolded name
104 * and the f2fs dirhash if needed, then packing all the information about this
105 * filename up into a 'struct f2fs_filename'.
107 int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
108 int lookup, struct f2fs_filename *fname)
110 struct fscrypt_name crypt_name;
113 err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
117 return __f2fs_setup_filename(dir, &crypt_name, fname);
121 * Prepare to look up @dentry in @dir. This is similar to
122 * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
123 * and the f2fs dirhash if needed, then packing all the information about this
124 * filename up into a 'struct f2fs_filename'.
126 int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
127 struct f2fs_filename *fname)
129 struct fscrypt_name crypt_name;
132 err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
136 return __f2fs_setup_filename(dir, &crypt_name, fname);
139 void f2fs_free_filename(struct f2fs_filename *fname)
141 #ifdef CONFIG_FS_ENCRYPTION
142 kfree(fname->crypto_buf.name);
143 fname->crypto_buf.name = NULL;
145 #if IS_ENABLED(CONFIG_UNICODE)
146 if (fname->cf_name.name) {
147 kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
148 fname->cf_name.name = NULL;
153 static unsigned long dir_block_index(unsigned int level,
154 int dir_level, unsigned int idx)
157 unsigned long bidx = 0;
159 for (i = 0; i < level; i++)
160 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
161 bidx += idx * bucket_blocks(level);
165 static struct f2fs_dir_entry *find_in_block(struct inode *dir,
166 struct page *dentry_page,
167 const struct f2fs_filename *fname,
170 struct f2fs_dentry_block *dentry_blk;
171 struct f2fs_dentry_ptr d;
173 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
175 make_dentry_ptr_block(dir, &d, dentry_blk);
176 return f2fs_find_target_dentry(&d, fname, max_slots);
179 #if IS_ENABLED(CONFIG_UNICODE)
181 * Test whether a case-insensitive directory entry matches the filename
182 * being searched for.
184 * Returns 1 for a match, 0 for no match, and -errno on an error.
186 static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
187 const u8 *de_name, u32 de_name_len)
189 const struct super_block *sb = dir->i_sb;
190 const struct unicode_map *um = sb->s_encoding;
191 struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
192 struct qstr entry = QSTR_INIT(de_name, de_name_len);
195 if (IS_ENCRYPTED(dir)) {
196 const struct fscrypt_str encrypted_name =
197 FSTR_INIT((u8 *)de_name, de_name_len);
199 if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir)))
202 decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
203 if (!decrypted_name.name)
205 res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name,
209 entry.name = decrypted_name.name;
210 entry.len = decrypted_name.len;
213 res = utf8_strncasecmp_folded(um, name, &entry);
215 * In strict mode, ignore invalid names. In non-strict mode,
216 * fall back to treating them as opaque byte sequences.
218 if (res < 0 && !sb_has_strict_encoding(sb)) {
219 res = name->len == entry.len &&
220 memcmp(name->name, entry.name, name->len) == 0;
222 /* utf8_strncasecmp_folded returns 0 on match */
226 kfree(decrypted_name.name);
229 #endif /* CONFIG_UNICODE */
231 static inline int f2fs_match_name(const struct inode *dir,
232 const struct f2fs_filename *fname,
233 const u8 *de_name, u32 de_name_len)
235 struct fscrypt_name f;
237 #if IS_ENABLED(CONFIG_UNICODE)
238 if (fname->cf_name.name) {
239 struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
241 return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
244 f.usr_fname = fname->usr_fname;
245 f.disk_name = fname->disk_name;
246 #ifdef CONFIG_FS_ENCRYPTION
247 f.crypto_buf = fname->crypto_buf;
249 return fscrypt_match_name(&f, de_name, de_name_len);
252 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
253 const struct f2fs_filename *fname, int *max_slots)
255 struct f2fs_dir_entry *de;
256 unsigned long bit_pos = 0;
262 while (bit_pos < d->max) {
263 if (!test_bit_le(bit_pos, d->bitmap)) {
269 de = &d->dentry[bit_pos];
271 if (unlikely(!de->name_len)) {
276 if (de->hash_code == fname->hash) {
277 res = f2fs_match_name(d->inode, fname,
278 d->filename[bit_pos],
279 le16_to_cpu(de->name_len));
286 if (max_slots && max_len > *max_slots)
287 *max_slots = max_len;
290 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
295 if (max_slots && max_len > *max_slots)
296 *max_slots = max_len;
300 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
302 const struct f2fs_filename *fname,
303 struct page **res_page)
305 int s = GET_DENTRY_SLOTS(fname->disk_name.len);
306 unsigned int nbucket, nblock;
307 unsigned int bidx, end_block;
308 struct page *dentry_page;
309 struct f2fs_dir_entry *de = NULL;
314 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
315 nblock = bucket_blocks(level);
317 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
318 le32_to_cpu(fname->hash) % nbucket);
319 end_block = bidx + nblock;
321 while (bidx < end_block) {
322 /* no need to allocate new dentry pages to all the indices */
323 dentry_page = f2fs_find_data_page(dir, bidx, &next_pgofs);
324 if (IS_ERR(dentry_page)) {
325 if (PTR_ERR(dentry_page) == -ENOENT) {
330 *res_page = dentry_page;
335 de = find_in_block(dir, dentry_page, fname, &max_slots);
337 *res_page = ERR_CAST(de);
341 *res_page = dentry_page;
347 f2fs_put_page(dentry_page, 0);
352 if (!de && room && F2FS_I(dir)->chash != fname->hash) {
353 F2FS_I(dir)->chash = fname->hash;
354 F2FS_I(dir)->clevel = level;
360 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
361 const struct f2fs_filename *fname,
362 struct page **res_page)
364 unsigned long npages = dir_blocks(dir);
365 struct f2fs_dir_entry *de = NULL;
366 unsigned int max_depth;
371 if (f2fs_has_inline_dentry(dir)) {
372 de = f2fs_find_in_inline_dir(dir, fname, res_page);
379 max_depth = F2FS_I(dir)->i_current_depth;
380 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
381 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
382 dir->i_ino, max_depth);
383 max_depth = MAX_DIR_HASH_DEPTH;
384 f2fs_i_depth_write(dir, max_depth);
387 for (level = 0; level < max_depth; level++) {
388 de = find_in_level(dir, level, fname, res_page);
389 if (de || IS_ERR(*res_page))
393 /* This is to increase the speed of f2fs_create */
395 F2FS_I(dir)->task = current;
400 * Find an entry in the specified directory with the wanted name.
401 * It returns the page where the entry was found (as a parameter - res_page),
402 * and the entry itself. Page is returned mapped and unlocked.
403 * Entry is guaranteed to be valid.
405 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
406 const struct qstr *child, struct page **res_page)
408 struct f2fs_dir_entry *de = NULL;
409 struct f2fs_filename fname;
412 err = f2fs_setup_filename(dir, child, 1, &fname);
417 *res_page = ERR_PTR(err);
421 de = __f2fs_find_entry(dir, &fname, res_page);
423 f2fs_free_filename(&fname);
427 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
429 return f2fs_find_entry(dir, &dotdot_name, p);
432 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
436 struct f2fs_dir_entry *de;
438 de = f2fs_find_entry(dir, qstr, page);
440 res = le32_to_cpu(de->ino);
441 f2fs_put_page(*page, 0);
447 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
448 struct page *page, struct inode *inode)
450 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
453 f2fs_wait_on_page_writeback(page, type, true, true);
454 de->ino = cpu_to_le32(inode->i_ino);
455 de->file_type = fs_umode_to_ftype(inode->i_mode);
456 set_page_dirty(page);
458 dir->i_mtime = inode_set_ctime_current(dir);
459 f2fs_mark_inode_dirty_sync(dir, false);
460 f2fs_put_page(page, 1);
463 static void init_dent_inode(struct inode *dir, struct inode *inode,
464 const struct f2fs_filename *fname,
467 struct f2fs_inode *ri;
469 if (!fname) /* tmpfile case? */
472 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
474 /* copy name info. to this inode page */
475 ri = F2FS_INODE(ipage);
476 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
477 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
478 if (IS_ENCRYPTED(dir)) {
479 file_set_enc_name(inode);
481 * Roll-forward recovery doesn't have encryption keys available,
482 * so it can't compute the dirhash for encrypted+casefolded
483 * filenames. Append it to i_name if possible. Else, disable
484 * roll-forward recovery of the dentry (i.e., make fsync'ing the
485 * file force a checkpoint) by setting LOST_PINO.
487 if (IS_CASEFOLDED(dir)) {
488 if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
490 put_unaligned(fname->hash, (f2fs_hash_t *)
491 &ri->i_name[fname->disk_name.len]);
493 file_lost_pino(inode);
496 set_page_dirty(ipage);
499 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
500 struct f2fs_dentry_ptr *d)
502 struct fscrypt_str dot = FSTR_INIT(".", 1);
503 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
505 /* update dirent of "." */
506 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
508 /* update dirent of ".." */
509 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
512 static int make_empty_dir(struct inode *inode,
513 struct inode *parent, struct page *page)
515 struct page *dentry_page;
516 struct f2fs_dentry_block *dentry_blk;
517 struct f2fs_dentry_ptr d;
519 if (f2fs_has_inline_dentry(inode))
520 return f2fs_make_empty_inline_dir(inode, parent, page);
522 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
523 if (IS_ERR(dentry_page))
524 return PTR_ERR(dentry_page);
526 dentry_blk = page_address(dentry_page);
528 make_dentry_ptr_block(NULL, &d, dentry_blk);
529 f2fs_do_make_empty_dir(inode, parent, &d);
531 set_page_dirty(dentry_page);
532 f2fs_put_page(dentry_page, 1);
536 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
537 const struct f2fs_filename *fname, struct page *dpage)
542 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
543 page = f2fs_new_inode_page(inode);
547 if (S_ISDIR(inode->i_mode)) {
548 /* in order to handle error case */
550 err = make_empty_dir(inode, dir, page);
558 err = f2fs_init_acl(inode, dir, page, dpage);
562 err = f2fs_init_security(inode, dir,
563 fname ? fname->usr_fname : NULL, page);
567 if (IS_ENCRYPTED(inode)) {
568 err = fscrypt_set_context(inode, page);
573 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
578 init_dent_inode(dir, inode, fname, page);
581 * This file should be checkpointed during fsync.
582 * We lost i_pino from now on.
584 if (is_inode_flag_set(inode, FI_INC_LINK)) {
585 if (!S_ISDIR(inode->i_mode))
586 file_lost_pino(inode);
588 * If link the tmpfile to alias through linkat path,
589 * we should remove this inode from orphan list.
591 if (inode->i_nlink == 0)
592 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
593 f2fs_i_links_write(inode, true);
599 f2fs_update_inode(inode, page);
600 f2fs_put_page(page, 1);
604 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
605 unsigned int current_depth)
607 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
608 if (S_ISDIR(inode->i_mode))
609 f2fs_i_links_write(dir, true);
610 clear_inode_flag(inode, FI_NEW_INODE);
612 dir->i_mtime = inode_set_ctime_current(dir);
613 f2fs_mark_inode_dirty_sync(dir, false);
615 if (F2FS_I(dir)->i_current_depth != current_depth)
616 f2fs_i_depth_write(dir, current_depth);
618 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
619 clear_inode_flag(inode, FI_INC_LINK);
622 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
625 int zero_start, zero_end;
627 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
628 if (zero_start >= max_slots)
631 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
632 if (zero_end - zero_start >= slots)
635 bit_start = zero_end + 1;
637 if (zero_end + 1 >= max_slots)
642 bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
643 const struct f2fs_filename *fname)
645 struct f2fs_dentry_ptr d;
646 unsigned int bit_pos;
647 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
649 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
651 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
653 return bit_pos < d.max;
656 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
657 const struct fscrypt_str *name, f2fs_hash_t name_hash,
658 unsigned int bit_pos)
660 struct f2fs_dir_entry *de;
661 int slots = GET_DENTRY_SLOTS(name->len);
664 de = &d->dentry[bit_pos];
665 de->hash_code = name_hash;
666 de->name_len = cpu_to_le16(name->len);
667 memcpy(d->filename[bit_pos], name->name, name->len);
668 de->ino = cpu_to_le32(ino);
669 de->file_type = fs_umode_to_ftype(mode);
670 for (i = 0; i < slots; i++) {
671 __set_bit_le(bit_pos + i, (void *)d->bitmap);
672 /* avoid wrong garbage data for readdir */
674 (de + i)->name_len = 0;
678 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
679 struct inode *inode, nid_t ino, umode_t mode)
681 unsigned int bit_pos;
683 unsigned int current_depth;
684 unsigned long bidx, block;
685 unsigned int nbucket, nblock;
686 struct page *dentry_page = NULL;
687 struct f2fs_dentry_block *dentry_blk = NULL;
688 struct f2fs_dentry_ptr d;
689 struct page *page = NULL;
693 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
695 current_depth = F2FS_I(dir)->i_current_depth;
696 if (F2FS_I(dir)->chash == fname->hash) {
697 level = F2FS_I(dir)->clevel;
698 F2FS_I(dir)->chash = 0;
702 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH))
705 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
708 /* Increase the depth, if required */
709 if (level == current_depth)
712 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
713 nblock = bucket_blocks(level);
715 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
716 (le32_to_cpu(fname->hash) % nbucket));
718 for (block = bidx; block <= (bidx + nblock - 1); block++) {
719 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
720 if (IS_ERR(dentry_page))
721 return PTR_ERR(dentry_page);
723 dentry_blk = page_address(dentry_page);
724 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
725 slots, NR_DENTRY_IN_BLOCK);
726 if (bit_pos < NR_DENTRY_IN_BLOCK)
729 f2fs_put_page(dentry_page, 1);
732 /* Move to next level to find the empty slot for new dentry */
736 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
739 f2fs_down_write(&F2FS_I(inode)->i_sem);
740 page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
747 make_dentry_ptr_block(NULL, &d, dentry_blk);
748 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
751 set_page_dirty(dentry_page);
754 f2fs_i_pino_write(inode, dir->i_ino);
756 /* synchronize inode page's data from inode cache */
757 if (is_inode_flag_set(inode, FI_NEW_INODE))
758 f2fs_update_inode(inode, page);
760 f2fs_put_page(page, 1);
763 f2fs_update_parent_metadata(dir, inode, current_depth);
766 f2fs_up_write(&F2FS_I(inode)->i_sem);
768 f2fs_put_page(dentry_page, 1);
773 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
774 struct inode *inode, nid_t ino, umode_t mode)
778 if (f2fs_has_inline_dentry(dir)) {
780 * Should get i_xattr_sem to keep the lock order:
781 * i_xattr_sem -> inode_page lock used by f2fs_setxattr.
783 f2fs_down_read(&F2FS_I(dir)->i_xattr_sem);
784 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
785 f2fs_up_read(&F2FS_I(dir)->i_xattr_sem);
788 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
790 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
795 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
798 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
799 struct inode *inode, nid_t ino, umode_t mode)
801 struct f2fs_filename fname;
802 struct page *page = NULL;
803 struct f2fs_dir_entry *de = NULL;
806 err = f2fs_setup_filename(dir, name, 0, &fname);
811 * An immature stackable filesystem shows a race condition between lookup
812 * and create. If we have same task when doing lookup and create, it's
813 * definitely fine as expected by VFS normally. Otherwise, let's just
814 * verify on-disk dentry one more time, which guarantees filesystem
817 if (current != F2FS_I(dir)->task) {
818 de = __f2fs_find_entry(dir, &fname, &page);
819 F2FS_I(dir)->task = NULL;
822 f2fs_put_page(page, 0);
824 } else if (IS_ERR(page)) {
827 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
829 f2fs_free_filename(&fname);
833 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
838 f2fs_down_write(&F2FS_I(inode)->i_sem);
839 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
844 f2fs_put_page(page, 1);
846 clear_inode_flag(inode, FI_NEW_INODE);
847 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
849 f2fs_up_write(&F2FS_I(inode)->i_sem);
853 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
855 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
857 f2fs_down_write(&F2FS_I(inode)->i_sem);
859 if (S_ISDIR(inode->i_mode))
860 f2fs_i_links_write(dir, false);
861 inode_set_ctime_current(inode);
863 f2fs_i_links_write(inode, false);
864 if (S_ISDIR(inode->i_mode)) {
865 f2fs_i_links_write(inode, false);
866 f2fs_i_size_write(inode, 0);
868 f2fs_up_write(&F2FS_I(inode)->i_sem);
870 if (inode->i_nlink == 0)
871 f2fs_add_orphan_inode(inode);
873 f2fs_release_orphan_inode(sbi);
877 * It only removes the dentry from the dentry page, corresponding name
878 * entry in name page does not need to be touched during deletion.
880 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
881 struct inode *dir, struct inode *inode)
883 struct f2fs_dentry_block *dentry_blk;
884 unsigned int bit_pos;
885 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
888 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
890 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
891 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
893 if (f2fs_has_inline_dentry(dir))
894 return f2fs_delete_inline_entry(dentry, page, dir, inode);
897 f2fs_wait_on_page_writeback(page, DATA, true, true);
899 dentry_blk = page_address(page);
900 bit_pos = dentry - dentry_blk->dentry;
901 for (i = 0; i < slots; i++)
902 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
904 /* Let's check and deallocate this dentry page */
905 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
908 set_page_dirty(page);
910 if (bit_pos == NR_DENTRY_IN_BLOCK &&
911 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
912 f2fs_clear_page_cache_dirty_tag(page);
913 clear_page_dirty_for_io(page);
914 ClearPageUptodate(page);
915 clear_page_private_all(page);
917 inode_dec_dirty_pages(dir);
918 f2fs_remove_dirty_inode(dir);
920 f2fs_put_page(page, 1);
922 dir->i_mtime = inode_set_ctime_current(dir);
923 f2fs_mark_inode_dirty_sync(dir, false);
926 f2fs_drop_nlink(dir, inode);
929 bool f2fs_empty_dir(struct inode *dir)
931 unsigned long bidx = 0;
932 struct page *dentry_page;
933 unsigned int bit_pos;
934 struct f2fs_dentry_block *dentry_blk;
935 unsigned long nblock = dir_blocks(dir);
937 if (f2fs_has_inline_dentry(dir))
938 return f2fs_empty_inline_dir(dir);
940 while (bidx < nblock) {
943 dentry_page = f2fs_find_data_page(dir, bidx, &next_pgofs);
944 if (IS_ERR(dentry_page)) {
945 if (PTR_ERR(dentry_page) == -ENOENT) {
953 dentry_blk = page_address(dentry_page);
958 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
962 f2fs_put_page(dentry_page, 0);
964 if (bit_pos < NR_DENTRY_IN_BLOCK)
972 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
973 unsigned int start_pos, struct fscrypt_str *fstr)
975 unsigned char d_type = DT_UNKNOWN;
976 unsigned int bit_pos;
977 struct f2fs_dir_entry *de = NULL;
978 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
979 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
980 struct blk_plug plug;
981 bool readdir_ra = sbi->readdir_ra;
982 bool found_valid_dirent = false;
985 bit_pos = ((unsigned long)ctx->pos % d->max);
988 blk_start_plug(&plug);
990 while (bit_pos < d->max) {
991 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
992 if (bit_pos >= d->max)
995 de = &d->dentry[bit_pos];
996 if (de->name_len == 0) {
997 if (found_valid_dirent || !bit_pos) {
999 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
1000 KERN_WARNING, sbi->sb->s_id,
1001 le32_to_cpu(de->ino));
1002 set_sbi_flag(sbi, SBI_NEED_FSCK);
1005 ctx->pos = start_pos + bit_pos;
1009 d_type = fs_ftype_to_dtype(de->file_type);
1011 de_name.name = d->filename[bit_pos];
1012 de_name.len = le16_to_cpu(de->name_len);
1014 /* check memory boundary before moving forward */
1015 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1016 if (unlikely(bit_pos > d->max ||
1017 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1018 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1019 __func__, le16_to_cpu(de->name_len));
1020 set_sbi_flag(sbi, SBI_NEED_FSCK);
1021 err = -EFSCORRUPTED;
1022 f2fs_handle_error(sbi, ERROR_CORRUPTED_DIRENT);
1026 if (IS_ENCRYPTED(d->inode)) {
1027 int save_len = fstr->len;
1029 err = fscrypt_fname_disk_to_usr(d->inode,
1030 (u32)le32_to_cpu(de->hash_code),
1036 fstr->len = save_len;
1039 if (!dir_emit(ctx, de_name.name, de_name.len,
1040 le32_to_cpu(de->ino), d_type)) {
1046 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1048 ctx->pos = start_pos + bit_pos;
1049 found_valid_dirent = true;
1053 blk_finish_plug(&plug);
1057 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1059 struct inode *inode = file_inode(file);
1060 unsigned long npages = dir_blocks(inode);
1061 struct f2fs_dentry_block *dentry_blk = NULL;
1062 struct page *dentry_page = NULL;
1063 struct file_ra_state *ra = &file->f_ra;
1064 loff_t start_pos = ctx->pos;
1065 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1066 struct f2fs_dentry_ptr d;
1067 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1070 if (IS_ENCRYPTED(inode)) {
1071 err = fscrypt_prepare_readdir(inode);
1075 err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1080 if (f2fs_has_inline_dentry(inode)) {
1081 err = f2fs_read_inline_dir(file, ctx, &fstr);
1085 for (; n < npages; ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1088 /* allow readdir() to be interrupted */
1089 if (fatal_signal_pending(current)) {
1095 /* readahead for multi pages of dir */
1096 if (npages - n > 1 && !ra_has_index(ra, n))
1097 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1098 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1100 dentry_page = f2fs_find_data_page(inode, n, &next_pgofs);
1101 if (IS_ERR(dentry_page)) {
1102 err = PTR_ERR(dentry_page);
1103 if (err == -ENOENT) {
1112 dentry_blk = page_address(dentry_page);
1114 make_dentry_ptr_block(inode, &d, dentry_blk);
1116 err = f2fs_fill_dentries(ctx, &d,
1117 n * NR_DENTRY_IN_BLOCK, &fstr);
1119 f2fs_put_page(dentry_page, 0);
1123 f2fs_put_page(dentry_page, 0);
1128 fscrypt_fname_free_buffer(&fstr);
1130 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1131 return err < 0 ? err : 0;
1134 const struct file_operations f2fs_dir_operations = {
1135 .llseek = generic_file_llseek,
1136 .read = generic_read_dir,
1137 .iterate_shared = f2fs_readdir,
1138 .fsync = f2fs_sync_file,
1139 .unlocked_ioctl = f2fs_ioctl,
1140 #ifdef CONFIG_COMPAT
1141 .compat_ioctl = f2fs_compat_ioctl,