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 1 << (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 static unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
46 [F2FS_FT_UNKNOWN] = DT_UNKNOWN,
47 [F2FS_FT_REG_FILE] = DT_REG,
48 [F2FS_FT_DIR] = DT_DIR,
49 [F2FS_FT_CHRDEV] = DT_CHR,
50 [F2FS_FT_BLKDEV] = DT_BLK,
51 [F2FS_FT_FIFO] = DT_FIFO,
52 [F2FS_FT_SOCK] = DT_SOCK,
53 [F2FS_FT_SYMLINK] = DT_LNK,
56 static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
57 [S_IFREG >> S_SHIFT] = F2FS_FT_REG_FILE,
58 [S_IFDIR >> S_SHIFT] = F2FS_FT_DIR,
59 [S_IFCHR >> S_SHIFT] = F2FS_FT_CHRDEV,
60 [S_IFBLK >> S_SHIFT] = F2FS_FT_BLKDEV,
61 [S_IFIFO >> S_SHIFT] = F2FS_FT_FIFO,
62 [S_IFSOCK >> S_SHIFT] = F2FS_FT_SOCK,
63 [S_IFLNK >> S_SHIFT] = F2FS_FT_SYMLINK,
66 static void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
68 de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
71 unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de)
73 if (de->file_type < F2FS_FT_MAX)
74 return f2fs_filetype_table[de->file_type];
78 /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
79 int f2fs_init_casefolded_name(const struct inode *dir,
80 struct f2fs_filename *fname)
82 #if IS_ENABLED(CONFIG_UNICODE)
83 struct super_block *sb = dir->i_sb;
85 if (IS_CASEFOLDED(dir) &&
86 !is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) {
87 fname->cf_name.name = f2fs_kmem_cache_alloc(f2fs_cf_name_slab,
88 GFP_NOFS, false, F2FS_SB(sb));
89 if (!fname->cf_name.name)
91 fname->cf_name.len = utf8_casefold(sb->s_encoding,
95 if ((int)fname->cf_name.len <= 0) {
96 kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
97 fname->cf_name.name = NULL;
98 if (sb_has_strict_encoding(sb))
100 /* fall back to treating name as opaque byte sequence */
107 static int __f2fs_setup_filename(const struct inode *dir,
108 const struct fscrypt_name *crypt_name,
109 struct f2fs_filename *fname)
113 memset(fname, 0, sizeof(*fname));
115 fname->usr_fname = crypt_name->usr_fname;
116 fname->disk_name = crypt_name->disk_name;
117 #ifdef CONFIG_FS_ENCRYPTION
118 fname->crypto_buf = crypt_name->crypto_buf;
120 if (crypt_name->is_nokey_name) {
121 /* hash was decoded from the no-key name */
122 fname->hash = cpu_to_le32(crypt_name->hash);
124 err = f2fs_init_casefolded_name(dir, fname);
126 f2fs_free_filename(fname);
129 f2fs_hash_filename(dir, fname);
135 * Prepare to search for @iname in @dir. This is similar to
136 * fscrypt_setup_filename(), but this also handles computing the casefolded name
137 * and the f2fs dirhash if needed, then packing all the information about this
138 * filename up into a 'struct f2fs_filename'.
140 int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
141 int lookup, struct f2fs_filename *fname)
143 struct fscrypt_name crypt_name;
146 err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
150 return __f2fs_setup_filename(dir, &crypt_name, fname);
154 * Prepare to look up @dentry in @dir. This is similar to
155 * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
156 * and the f2fs dirhash if needed, then packing all the information about this
157 * filename up into a 'struct f2fs_filename'.
159 int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
160 struct f2fs_filename *fname)
162 struct fscrypt_name crypt_name;
165 err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
169 return __f2fs_setup_filename(dir, &crypt_name, fname);
172 void f2fs_free_filename(struct f2fs_filename *fname)
174 #ifdef CONFIG_FS_ENCRYPTION
175 kfree(fname->crypto_buf.name);
176 fname->crypto_buf.name = NULL;
178 #if IS_ENABLED(CONFIG_UNICODE)
179 if (fname->cf_name.name) {
180 kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
181 fname->cf_name.name = NULL;
186 static unsigned long dir_block_index(unsigned int level,
187 int dir_level, unsigned int idx)
190 unsigned long bidx = 0;
192 for (i = 0; i < level; i++)
193 bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
194 bidx += idx * bucket_blocks(level);
198 static struct f2fs_dir_entry *find_in_block(struct inode *dir,
199 struct page *dentry_page,
200 const struct f2fs_filename *fname,
203 struct f2fs_dentry_block *dentry_blk;
204 struct f2fs_dentry_ptr d;
206 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
208 make_dentry_ptr_block(dir, &d, dentry_blk);
209 return f2fs_find_target_dentry(&d, fname, max_slots);
212 #if IS_ENABLED(CONFIG_UNICODE)
214 * Test whether a case-insensitive directory entry matches the filename
215 * being searched for.
217 * Returns 1 for a match, 0 for no match, and -errno on an error.
219 static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
220 const u8 *de_name, u32 de_name_len)
222 const struct super_block *sb = dir->i_sb;
223 const struct unicode_map *um = sb->s_encoding;
224 struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
225 struct qstr entry = QSTR_INIT(de_name, de_name_len);
228 if (IS_ENCRYPTED(dir)) {
229 const struct fscrypt_str encrypted_name =
230 FSTR_INIT((u8 *)de_name, de_name_len);
232 if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir)))
235 decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
236 if (!decrypted_name.name)
238 res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name,
242 entry.name = decrypted_name.name;
243 entry.len = decrypted_name.len;
246 res = utf8_strncasecmp_folded(um, name, &entry);
248 * In strict mode, ignore invalid names. In non-strict mode,
249 * fall back to treating them as opaque byte sequences.
251 if (res < 0 && !sb_has_strict_encoding(sb)) {
252 res = name->len == entry.len &&
253 memcmp(name->name, entry.name, name->len) == 0;
255 /* utf8_strncasecmp_folded returns 0 on match */
259 kfree(decrypted_name.name);
262 #endif /* CONFIG_UNICODE */
264 static inline int f2fs_match_name(const struct inode *dir,
265 const struct f2fs_filename *fname,
266 const u8 *de_name, u32 de_name_len)
268 struct fscrypt_name f;
270 #if IS_ENABLED(CONFIG_UNICODE)
271 if (fname->cf_name.name) {
272 struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
274 return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
277 f.usr_fname = fname->usr_fname;
278 f.disk_name = fname->disk_name;
279 #ifdef CONFIG_FS_ENCRYPTION
280 f.crypto_buf = fname->crypto_buf;
282 return fscrypt_match_name(&f, de_name, de_name_len);
285 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
286 const struct f2fs_filename *fname, int *max_slots)
288 struct f2fs_dir_entry *de;
289 unsigned long bit_pos = 0;
295 while (bit_pos < d->max) {
296 if (!test_bit_le(bit_pos, d->bitmap)) {
302 de = &d->dentry[bit_pos];
304 if (unlikely(!de->name_len)) {
309 if (de->hash_code == fname->hash) {
310 res = f2fs_match_name(d->inode, fname,
311 d->filename[bit_pos],
312 le16_to_cpu(de->name_len));
319 if (max_slots && max_len > *max_slots)
320 *max_slots = max_len;
323 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
328 if (max_slots && max_len > *max_slots)
329 *max_slots = max_len;
333 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
335 const struct f2fs_filename *fname,
336 struct page **res_page)
338 int s = GET_DENTRY_SLOTS(fname->disk_name.len);
339 unsigned int nbucket, nblock;
340 unsigned int bidx, end_block;
341 struct page *dentry_page;
342 struct f2fs_dir_entry *de = NULL;
346 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
347 nblock = bucket_blocks(level);
349 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
350 le32_to_cpu(fname->hash) % nbucket);
351 end_block = bidx + nblock;
353 for (; bidx < end_block; bidx++) {
354 /* no need to allocate new dentry pages to all the indices */
355 dentry_page = f2fs_find_data_page(dir, bidx);
356 if (IS_ERR(dentry_page)) {
357 if (PTR_ERR(dentry_page) == -ENOENT) {
361 *res_page = dentry_page;
366 de = find_in_block(dir, dentry_page, fname, &max_slots);
368 *res_page = ERR_CAST(de);
372 *res_page = dentry_page;
378 f2fs_put_page(dentry_page, 0);
381 if (!de && room && F2FS_I(dir)->chash != fname->hash) {
382 F2FS_I(dir)->chash = fname->hash;
383 F2FS_I(dir)->clevel = level;
389 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
390 const struct f2fs_filename *fname,
391 struct page **res_page)
393 unsigned long npages = dir_blocks(dir);
394 struct f2fs_dir_entry *de = NULL;
395 unsigned int max_depth;
400 if (f2fs_has_inline_dentry(dir)) {
401 de = f2fs_find_in_inline_dir(dir, fname, res_page);
408 max_depth = F2FS_I(dir)->i_current_depth;
409 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
410 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
411 dir->i_ino, max_depth);
412 max_depth = MAX_DIR_HASH_DEPTH;
413 f2fs_i_depth_write(dir, max_depth);
416 for (level = 0; level < max_depth; level++) {
417 de = find_in_level(dir, level, fname, res_page);
418 if (de || IS_ERR(*res_page))
422 /* This is to increase the speed of f2fs_create */
424 F2FS_I(dir)->task = current;
429 * Find an entry in the specified directory with the wanted name.
430 * It returns the page where the entry was found (as a parameter - res_page),
431 * and the entry itself. Page is returned mapped and unlocked.
432 * Entry is guaranteed to be valid.
434 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
435 const struct qstr *child, struct page **res_page)
437 struct f2fs_dir_entry *de = NULL;
438 struct f2fs_filename fname;
441 err = f2fs_setup_filename(dir, child, 1, &fname);
446 *res_page = ERR_PTR(err);
450 de = __f2fs_find_entry(dir, &fname, res_page);
452 f2fs_free_filename(&fname);
456 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
458 return f2fs_find_entry(dir, &dotdot_name, p);
461 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
465 struct f2fs_dir_entry *de;
467 de = f2fs_find_entry(dir, qstr, page);
469 res = le32_to_cpu(de->ino);
470 f2fs_put_page(*page, 0);
476 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
477 struct page *page, struct inode *inode)
479 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
482 f2fs_wait_on_page_writeback(page, type, true, true);
483 de->ino = cpu_to_le32(inode->i_ino);
484 set_de_type(de, inode->i_mode);
485 set_page_dirty(page);
487 dir->i_mtime = dir->i_ctime = current_time(dir);
488 f2fs_mark_inode_dirty_sync(dir, false);
489 f2fs_put_page(page, 1);
492 static void init_dent_inode(struct inode *dir, struct inode *inode,
493 const struct f2fs_filename *fname,
496 struct f2fs_inode *ri;
498 if (!fname) /* tmpfile case? */
501 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
503 /* copy name info. to this inode page */
504 ri = F2FS_INODE(ipage);
505 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
506 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
507 if (IS_ENCRYPTED(dir)) {
508 file_set_enc_name(inode);
510 * Roll-forward recovery doesn't have encryption keys available,
511 * so it can't compute the dirhash for encrypted+casefolded
512 * filenames. Append it to i_name if possible. Else, disable
513 * roll-forward recovery of the dentry (i.e., make fsync'ing the
514 * file force a checkpoint) by setting LOST_PINO.
516 if (IS_CASEFOLDED(dir)) {
517 if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
519 put_unaligned(fname->hash, (f2fs_hash_t *)
520 &ri->i_name[fname->disk_name.len]);
522 file_lost_pino(inode);
525 set_page_dirty(ipage);
528 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
529 struct f2fs_dentry_ptr *d)
531 struct fscrypt_str dot = FSTR_INIT(".", 1);
532 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
534 /* update dirent of "." */
535 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
537 /* update dirent of ".." */
538 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
541 static int make_empty_dir(struct inode *inode,
542 struct inode *parent, struct page *page)
544 struct page *dentry_page;
545 struct f2fs_dentry_block *dentry_blk;
546 struct f2fs_dentry_ptr d;
548 if (f2fs_has_inline_dentry(inode))
549 return f2fs_make_empty_inline_dir(inode, parent, page);
551 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
552 if (IS_ERR(dentry_page))
553 return PTR_ERR(dentry_page);
555 dentry_blk = page_address(dentry_page);
557 make_dentry_ptr_block(NULL, &d, dentry_blk);
558 f2fs_do_make_empty_dir(inode, parent, &d);
560 set_page_dirty(dentry_page);
561 f2fs_put_page(dentry_page, 1);
565 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
566 const struct f2fs_filename *fname, struct page *dpage)
571 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
572 page = f2fs_new_inode_page(inode);
576 if (S_ISDIR(inode->i_mode)) {
577 /* in order to handle error case */
579 err = make_empty_dir(inode, dir, page);
587 err = f2fs_init_acl(inode, dir, page, dpage);
591 err = f2fs_init_security(inode, dir,
592 fname ? fname->usr_fname : NULL, page);
596 if (IS_ENCRYPTED(inode)) {
597 err = fscrypt_set_context(inode, page);
602 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
607 init_dent_inode(dir, inode, fname, page);
610 * This file should be checkpointed during fsync.
611 * We lost i_pino from now on.
613 if (is_inode_flag_set(inode, FI_INC_LINK)) {
614 if (!S_ISDIR(inode->i_mode))
615 file_lost_pino(inode);
617 * If link the tmpfile to alias through linkat path,
618 * we should remove this inode from orphan list.
620 if (inode->i_nlink == 0)
621 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
622 f2fs_i_links_write(inode, true);
628 f2fs_update_inode(inode, page);
629 f2fs_put_page(page, 1);
633 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
634 unsigned int current_depth)
636 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
637 if (S_ISDIR(inode->i_mode))
638 f2fs_i_links_write(dir, true);
639 clear_inode_flag(inode, FI_NEW_INODE);
641 dir->i_mtime = dir->i_ctime = current_time(dir);
642 f2fs_mark_inode_dirty_sync(dir, false);
644 if (F2FS_I(dir)->i_current_depth != current_depth)
645 f2fs_i_depth_write(dir, current_depth);
647 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
648 clear_inode_flag(inode, FI_INC_LINK);
651 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
654 int zero_start, zero_end;
656 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
657 if (zero_start >= max_slots)
660 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
661 if (zero_end - zero_start >= slots)
664 bit_start = zero_end + 1;
666 if (zero_end + 1 >= max_slots)
671 bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
672 const struct f2fs_filename *fname)
674 struct f2fs_dentry_ptr d;
675 unsigned int bit_pos;
676 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
678 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
680 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
682 return bit_pos < d.max;
685 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
686 const struct fscrypt_str *name, f2fs_hash_t name_hash,
687 unsigned int bit_pos)
689 struct f2fs_dir_entry *de;
690 int slots = GET_DENTRY_SLOTS(name->len);
693 de = &d->dentry[bit_pos];
694 de->hash_code = name_hash;
695 de->name_len = cpu_to_le16(name->len);
696 memcpy(d->filename[bit_pos], name->name, name->len);
697 de->ino = cpu_to_le32(ino);
698 set_de_type(de, mode);
699 for (i = 0; i < slots; i++) {
700 __set_bit_le(bit_pos + i, (void *)d->bitmap);
701 /* avoid wrong garbage data for readdir */
703 (de + i)->name_len = 0;
707 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
708 struct inode *inode, nid_t ino, umode_t mode)
710 unsigned int bit_pos;
712 unsigned int current_depth;
713 unsigned long bidx, block;
714 unsigned int nbucket, nblock;
715 struct page *dentry_page = NULL;
716 struct f2fs_dentry_block *dentry_blk = NULL;
717 struct f2fs_dentry_ptr d;
718 struct page *page = NULL;
722 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
724 current_depth = F2FS_I(dir)->i_current_depth;
725 if (F2FS_I(dir)->chash == fname->hash) {
726 level = F2FS_I(dir)->clevel;
727 F2FS_I(dir)->chash = 0;
731 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
732 f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
736 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
739 /* Increase the depth, if required */
740 if (level == current_depth)
743 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
744 nblock = bucket_blocks(level);
746 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
747 (le32_to_cpu(fname->hash) % nbucket));
749 for (block = bidx; block <= (bidx + nblock - 1); block++) {
750 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
751 if (IS_ERR(dentry_page))
752 return PTR_ERR(dentry_page);
754 dentry_blk = page_address(dentry_page);
755 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
756 slots, NR_DENTRY_IN_BLOCK);
757 if (bit_pos < NR_DENTRY_IN_BLOCK)
760 f2fs_put_page(dentry_page, 1);
763 /* Move to next level to find the empty slot for new dentry */
767 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
770 f2fs_down_write(&F2FS_I(inode)->i_sem);
771 page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
778 make_dentry_ptr_block(NULL, &d, dentry_blk);
779 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
782 set_page_dirty(dentry_page);
785 f2fs_i_pino_write(inode, dir->i_ino);
787 /* synchronize inode page's data from inode cache */
788 if (is_inode_flag_set(inode, FI_NEW_INODE))
789 f2fs_update_inode(inode, page);
791 f2fs_put_page(page, 1);
794 f2fs_update_parent_metadata(dir, inode, current_depth);
797 f2fs_up_write(&F2FS_I(inode)->i_sem);
799 f2fs_put_page(dentry_page, 1);
804 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
805 struct inode *inode, nid_t ino, umode_t mode)
809 if (f2fs_has_inline_dentry(dir))
810 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
812 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
814 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
819 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
822 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
823 struct inode *inode, nid_t ino, umode_t mode)
825 struct f2fs_filename fname;
826 struct page *page = NULL;
827 struct f2fs_dir_entry *de = NULL;
830 err = f2fs_setup_filename(dir, name, 0, &fname);
835 * An immature stackable filesystem shows a race condition between lookup
836 * and create. If we have same task when doing lookup and create, it's
837 * definitely fine as expected by VFS normally. Otherwise, let's just
838 * verify on-disk dentry one more time, which guarantees filesystem
841 if (current != F2FS_I(dir)->task) {
842 de = __f2fs_find_entry(dir, &fname, &page);
843 F2FS_I(dir)->task = NULL;
846 f2fs_put_page(page, 0);
848 } else if (IS_ERR(page)) {
851 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
853 f2fs_free_filename(&fname);
857 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
862 f2fs_down_write(&F2FS_I(inode)->i_sem);
863 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
868 f2fs_put_page(page, 1);
870 clear_inode_flag(inode, FI_NEW_INODE);
871 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
873 f2fs_up_write(&F2FS_I(inode)->i_sem);
877 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
879 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
881 f2fs_down_write(&F2FS_I(inode)->i_sem);
883 if (S_ISDIR(inode->i_mode))
884 f2fs_i_links_write(dir, false);
885 inode->i_ctime = current_time(inode);
887 f2fs_i_links_write(inode, false);
888 if (S_ISDIR(inode->i_mode)) {
889 f2fs_i_links_write(inode, false);
890 f2fs_i_size_write(inode, 0);
892 f2fs_up_write(&F2FS_I(inode)->i_sem);
894 if (inode->i_nlink == 0)
895 f2fs_add_orphan_inode(inode);
897 f2fs_release_orphan_inode(sbi);
901 * It only removes the dentry from the dentry page, corresponding name
902 * entry in name page does not need to be touched during deletion.
904 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
905 struct inode *dir, struct inode *inode)
907 struct f2fs_dentry_block *dentry_blk;
908 unsigned int bit_pos;
909 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
912 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
914 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
915 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
917 if (f2fs_has_inline_dentry(dir))
918 return f2fs_delete_inline_entry(dentry, page, dir, inode);
921 f2fs_wait_on_page_writeback(page, DATA, true, true);
923 dentry_blk = page_address(page);
924 bit_pos = dentry - dentry_blk->dentry;
925 for (i = 0; i < slots; i++)
926 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
928 /* Let's check and deallocate this dentry page */
929 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
932 set_page_dirty(page);
934 if (bit_pos == NR_DENTRY_IN_BLOCK &&
935 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
936 f2fs_clear_page_cache_dirty_tag(page);
937 clear_page_dirty_for_io(page);
938 ClearPageUptodate(page);
940 clear_page_private_gcing(page);
942 inode_dec_dirty_pages(dir);
943 f2fs_remove_dirty_inode(dir);
945 detach_page_private(page);
946 set_page_private(page, 0);
948 f2fs_put_page(page, 1);
950 dir->i_ctime = dir->i_mtime = current_time(dir);
951 f2fs_mark_inode_dirty_sync(dir, false);
954 f2fs_drop_nlink(dir, inode);
957 bool f2fs_empty_dir(struct inode *dir)
960 struct page *dentry_page;
961 unsigned int bit_pos;
962 struct f2fs_dentry_block *dentry_blk;
963 unsigned long nblock = dir_blocks(dir);
965 if (f2fs_has_inline_dentry(dir))
966 return f2fs_empty_inline_dir(dir);
968 for (bidx = 0; bidx < nblock; bidx++) {
969 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
970 if (IS_ERR(dentry_page)) {
971 if (PTR_ERR(dentry_page) == -ENOENT)
977 dentry_blk = page_address(dentry_page);
982 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
986 f2fs_put_page(dentry_page, 1);
988 if (bit_pos < NR_DENTRY_IN_BLOCK)
994 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
995 unsigned int start_pos, struct fscrypt_str *fstr)
997 unsigned char d_type = DT_UNKNOWN;
998 unsigned int bit_pos;
999 struct f2fs_dir_entry *de = NULL;
1000 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
1001 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
1002 struct blk_plug plug;
1003 bool readdir_ra = sbi->readdir_ra == 1;
1004 bool found_valid_dirent = false;
1007 bit_pos = ((unsigned long)ctx->pos % d->max);
1010 blk_start_plug(&plug);
1012 while (bit_pos < d->max) {
1013 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
1014 if (bit_pos >= d->max)
1017 de = &d->dentry[bit_pos];
1018 if (de->name_len == 0) {
1019 if (found_valid_dirent || !bit_pos) {
1021 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
1022 KERN_WARNING, sbi->sb->s_id,
1023 le32_to_cpu(de->ino));
1024 set_sbi_flag(sbi, SBI_NEED_FSCK);
1027 ctx->pos = start_pos + bit_pos;
1031 d_type = f2fs_get_de_type(de);
1033 de_name.name = d->filename[bit_pos];
1034 de_name.len = le16_to_cpu(de->name_len);
1036 /* check memory boundary before moving forward */
1037 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1038 if (unlikely(bit_pos > d->max ||
1039 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1040 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1041 __func__, le16_to_cpu(de->name_len));
1042 set_sbi_flag(sbi, SBI_NEED_FSCK);
1043 err = -EFSCORRUPTED;
1047 if (IS_ENCRYPTED(d->inode)) {
1048 int save_len = fstr->len;
1050 err = fscrypt_fname_disk_to_usr(d->inode,
1051 (u32)le32_to_cpu(de->hash_code),
1057 fstr->len = save_len;
1060 if (!dir_emit(ctx, de_name.name, de_name.len,
1061 le32_to_cpu(de->ino), d_type)) {
1067 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1069 ctx->pos = start_pos + bit_pos;
1070 found_valid_dirent = true;
1074 blk_finish_plug(&plug);
1078 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1080 struct inode *inode = file_inode(file);
1081 unsigned long npages = dir_blocks(inode);
1082 struct f2fs_dentry_block *dentry_blk = NULL;
1083 struct page *dentry_page = NULL;
1084 struct file_ra_state *ra = &file->f_ra;
1085 loff_t start_pos = ctx->pos;
1086 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1087 struct f2fs_dentry_ptr d;
1088 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1091 if (IS_ENCRYPTED(inode)) {
1092 err = fscrypt_prepare_readdir(inode);
1096 err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1101 if (f2fs_has_inline_dentry(inode)) {
1102 err = f2fs_read_inline_dir(file, ctx, &fstr);
1106 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1108 /* allow readdir() to be interrupted */
1109 if (fatal_signal_pending(current)) {
1115 /* readahead for multi pages of dir */
1116 if (npages - n > 1 && !ra_has_index(ra, n))
1117 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1118 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1120 dentry_page = f2fs_find_data_page(inode, n);
1121 if (IS_ERR(dentry_page)) {
1122 err = PTR_ERR(dentry_page);
1123 if (err == -ENOENT) {
1131 dentry_blk = page_address(dentry_page);
1133 make_dentry_ptr_block(inode, &d, dentry_blk);
1135 err = f2fs_fill_dentries(ctx, &d,
1136 n * NR_DENTRY_IN_BLOCK, &fstr);
1138 f2fs_put_page(dentry_page, 0);
1142 f2fs_put_page(dentry_page, 0);
1145 fscrypt_fname_free_buffer(&fstr);
1147 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1148 return err < 0 ? err : 0;
1151 const struct file_operations f2fs_dir_operations = {
1152 .llseek = generic_file_llseek,
1153 .read = generic_read_dir,
1154 .iterate_shared = f2fs_readdir,
1155 .fsync = f2fs_sync_file,
1156 .unlocked_ioctl = f2fs_ioctl,
1157 #ifdef CONFIG_COMPAT
1158 .compat_ioctl = f2fs_compat_ioctl,