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 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;
347 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
348 nblock = bucket_blocks(level);
350 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
351 le32_to_cpu(fname->hash) % nbucket);
352 end_block = bidx + nblock;
354 while (bidx < end_block) {
355 /* no need to allocate new dentry pages to all the indices */
356 dentry_page = f2fs_find_data_page(dir, bidx, &next_pgofs);
357 if (IS_ERR(dentry_page)) {
358 if (PTR_ERR(dentry_page) == -ENOENT) {
363 *res_page = dentry_page;
368 de = find_in_block(dir, dentry_page, fname, &max_slots);
370 *res_page = ERR_CAST(de);
374 *res_page = dentry_page;
380 f2fs_put_page(dentry_page, 0);
385 if (!de && room && F2FS_I(dir)->chash != fname->hash) {
386 F2FS_I(dir)->chash = fname->hash;
387 F2FS_I(dir)->clevel = level;
393 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
394 const struct f2fs_filename *fname,
395 struct page **res_page)
397 unsigned long npages = dir_blocks(dir);
398 struct f2fs_dir_entry *de = NULL;
399 unsigned int max_depth;
404 if (f2fs_has_inline_dentry(dir)) {
405 de = f2fs_find_in_inline_dir(dir, fname, res_page);
412 max_depth = F2FS_I(dir)->i_current_depth;
413 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
414 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
415 dir->i_ino, max_depth);
416 max_depth = MAX_DIR_HASH_DEPTH;
417 f2fs_i_depth_write(dir, max_depth);
420 for (level = 0; level < max_depth; level++) {
421 de = find_in_level(dir, level, fname, res_page);
422 if (de || IS_ERR(*res_page))
426 /* This is to increase the speed of f2fs_create */
428 F2FS_I(dir)->task = current;
433 * Find an entry in the specified directory with the wanted name.
434 * It returns the page where the entry was found (as a parameter - res_page),
435 * and the entry itself. Page is returned mapped and unlocked.
436 * Entry is guaranteed to be valid.
438 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
439 const struct qstr *child, struct page **res_page)
441 struct f2fs_dir_entry *de = NULL;
442 struct f2fs_filename fname;
445 err = f2fs_setup_filename(dir, child, 1, &fname);
450 *res_page = ERR_PTR(err);
454 de = __f2fs_find_entry(dir, &fname, res_page);
456 f2fs_free_filename(&fname);
460 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
462 return f2fs_find_entry(dir, &dotdot_name, p);
465 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
469 struct f2fs_dir_entry *de;
471 de = f2fs_find_entry(dir, qstr, page);
473 res = le32_to_cpu(de->ino);
474 f2fs_put_page(*page, 0);
480 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
481 struct page *page, struct inode *inode)
483 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
486 f2fs_wait_on_page_writeback(page, type, true, true);
487 de->ino = cpu_to_le32(inode->i_ino);
488 set_de_type(de, inode->i_mode);
489 set_page_dirty(page);
491 dir->i_mtime = dir->i_ctime = current_time(dir);
492 f2fs_mark_inode_dirty_sync(dir, false);
493 f2fs_put_page(page, 1);
496 static void init_dent_inode(struct inode *dir, struct inode *inode,
497 const struct f2fs_filename *fname,
500 struct f2fs_inode *ri;
502 if (!fname) /* tmpfile case? */
505 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
507 /* copy name info. to this inode page */
508 ri = F2FS_INODE(ipage);
509 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
510 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
511 if (IS_ENCRYPTED(dir)) {
512 file_set_enc_name(inode);
514 * Roll-forward recovery doesn't have encryption keys available,
515 * so it can't compute the dirhash for encrypted+casefolded
516 * filenames. Append it to i_name if possible. Else, disable
517 * roll-forward recovery of the dentry (i.e., make fsync'ing the
518 * file force a checkpoint) by setting LOST_PINO.
520 if (IS_CASEFOLDED(dir)) {
521 if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
523 put_unaligned(fname->hash, (f2fs_hash_t *)
524 &ri->i_name[fname->disk_name.len]);
526 file_lost_pino(inode);
529 set_page_dirty(ipage);
532 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
533 struct f2fs_dentry_ptr *d)
535 struct fscrypt_str dot = FSTR_INIT(".", 1);
536 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
538 /* update dirent of "." */
539 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
541 /* update dirent of ".." */
542 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
545 static int make_empty_dir(struct inode *inode,
546 struct inode *parent, struct page *page)
548 struct page *dentry_page;
549 struct f2fs_dentry_block *dentry_blk;
550 struct f2fs_dentry_ptr d;
552 if (f2fs_has_inline_dentry(inode))
553 return f2fs_make_empty_inline_dir(inode, parent, page);
555 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
556 if (IS_ERR(dentry_page))
557 return PTR_ERR(dentry_page);
559 dentry_blk = page_address(dentry_page);
561 make_dentry_ptr_block(NULL, &d, dentry_blk);
562 f2fs_do_make_empty_dir(inode, parent, &d);
564 set_page_dirty(dentry_page);
565 f2fs_put_page(dentry_page, 1);
569 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
570 const struct f2fs_filename *fname, struct page *dpage)
575 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
576 page = f2fs_new_inode_page(inode);
580 if (S_ISDIR(inode->i_mode)) {
581 /* in order to handle error case */
583 err = make_empty_dir(inode, dir, page);
591 err = f2fs_init_acl(inode, dir, page, dpage);
595 err = f2fs_init_security(inode, dir,
596 fname ? fname->usr_fname : NULL, page);
600 if (IS_ENCRYPTED(inode)) {
601 err = fscrypt_set_context(inode, page);
606 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
611 init_dent_inode(dir, inode, fname, page);
614 * This file should be checkpointed during fsync.
615 * We lost i_pino from now on.
617 if (is_inode_flag_set(inode, FI_INC_LINK)) {
618 if (!S_ISDIR(inode->i_mode))
619 file_lost_pino(inode);
621 * If link the tmpfile to alias through linkat path,
622 * we should remove this inode from orphan list.
624 if (inode->i_nlink == 0)
625 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
626 f2fs_i_links_write(inode, true);
632 f2fs_update_inode(inode, page);
633 f2fs_put_page(page, 1);
637 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
638 unsigned int current_depth)
640 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
641 if (S_ISDIR(inode->i_mode))
642 f2fs_i_links_write(dir, true);
643 clear_inode_flag(inode, FI_NEW_INODE);
645 dir->i_mtime = dir->i_ctime = current_time(dir);
646 f2fs_mark_inode_dirty_sync(dir, false);
648 if (F2FS_I(dir)->i_current_depth != current_depth)
649 f2fs_i_depth_write(dir, current_depth);
651 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
652 clear_inode_flag(inode, FI_INC_LINK);
655 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
658 int zero_start, zero_end;
660 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
661 if (zero_start >= max_slots)
664 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
665 if (zero_end - zero_start >= slots)
668 bit_start = zero_end + 1;
670 if (zero_end + 1 >= max_slots)
675 bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
676 const struct f2fs_filename *fname)
678 struct f2fs_dentry_ptr d;
679 unsigned int bit_pos;
680 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
682 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
684 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
686 return bit_pos < d.max;
689 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
690 const struct fscrypt_str *name, f2fs_hash_t name_hash,
691 unsigned int bit_pos)
693 struct f2fs_dir_entry *de;
694 int slots = GET_DENTRY_SLOTS(name->len);
697 de = &d->dentry[bit_pos];
698 de->hash_code = name_hash;
699 de->name_len = cpu_to_le16(name->len);
700 memcpy(d->filename[bit_pos], name->name, name->len);
701 de->ino = cpu_to_le32(ino);
702 set_de_type(de, mode);
703 for (i = 0; i < slots; i++) {
704 __set_bit_le(bit_pos + i, (void *)d->bitmap);
705 /* avoid wrong garbage data for readdir */
707 (de + i)->name_len = 0;
711 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
712 struct inode *inode, nid_t ino, umode_t mode)
714 unsigned int bit_pos;
716 unsigned int current_depth;
717 unsigned long bidx, block;
718 unsigned int nbucket, nblock;
719 struct page *dentry_page = NULL;
720 struct f2fs_dentry_block *dentry_blk = NULL;
721 struct f2fs_dentry_ptr d;
722 struct page *page = NULL;
726 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
728 current_depth = F2FS_I(dir)->i_current_depth;
729 if (F2FS_I(dir)->chash == fname->hash) {
730 level = F2FS_I(dir)->clevel;
731 F2FS_I(dir)->chash = 0;
735 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
736 f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
740 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
743 /* Increase the depth, if required */
744 if (level == current_depth)
747 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
748 nblock = bucket_blocks(level);
750 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
751 (le32_to_cpu(fname->hash) % nbucket));
753 for (block = bidx; block <= (bidx + nblock - 1); block++) {
754 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
755 if (IS_ERR(dentry_page))
756 return PTR_ERR(dentry_page);
758 dentry_blk = page_address(dentry_page);
759 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
760 slots, NR_DENTRY_IN_BLOCK);
761 if (bit_pos < NR_DENTRY_IN_BLOCK)
764 f2fs_put_page(dentry_page, 1);
767 /* Move to next level to find the empty slot for new dentry */
771 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
774 f2fs_down_write(&F2FS_I(inode)->i_sem);
775 page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
782 make_dentry_ptr_block(NULL, &d, dentry_blk);
783 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
786 set_page_dirty(dentry_page);
789 f2fs_i_pino_write(inode, dir->i_ino);
791 /* synchronize inode page's data from inode cache */
792 if (is_inode_flag_set(inode, FI_NEW_INODE))
793 f2fs_update_inode(inode, page);
795 f2fs_put_page(page, 1);
798 f2fs_update_parent_metadata(dir, inode, current_depth);
801 f2fs_up_write(&F2FS_I(inode)->i_sem);
803 f2fs_put_page(dentry_page, 1);
808 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
809 struct inode *inode, nid_t ino, umode_t mode)
813 if (f2fs_has_inline_dentry(dir)) {
815 * Should get i_xattr_sem to keep the lock order:
816 * i_xattr_sem -> inode_page lock used by f2fs_setxattr.
818 f2fs_down_read(&F2FS_I(dir)->i_xattr_sem);
819 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
820 f2fs_up_read(&F2FS_I(dir)->i_xattr_sem);
823 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
825 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
830 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
833 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
834 struct inode *inode, nid_t ino, umode_t mode)
836 struct f2fs_filename fname;
837 struct page *page = NULL;
838 struct f2fs_dir_entry *de = NULL;
841 err = f2fs_setup_filename(dir, name, 0, &fname);
846 * An immature stackable filesystem shows a race condition between lookup
847 * and create. If we have same task when doing lookup and create, it's
848 * definitely fine as expected by VFS normally. Otherwise, let's just
849 * verify on-disk dentry one more time, which guarantees filesystem
852 if (current != F2FS_I(dir)->task) {
853 de = __f2fs_find_entry(dir, &fname, &page);
854 F2FS_I(dir)->task = NULL;
857 f2fs_put_page(page, 0);
859 } else if (IS_ERR(page)) {
862 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
864 f2fs_free_filename(&fname);
868 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
873 f2fs_down_write(&F2FS_I(inode)->i_sem);
874 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
879 f2fs_put_page(page, 1);
881 clear_inode_flag(inode, FI_NEW_INODE);
882 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
884 f2fs_up_write(&F2FS_I(inode)->i_sem);
888 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
890 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
892 f2fs_down_write(&F2FS_I(inode)->i_sem);
894 if (S_ISDIR(inode->i_mode))
895 f2fs_i_links_write(dir, false);
896 inode->i_ctime = current_time(inode);
898 f2fs_i_links_write(inode, false);
899 if (S_ISDIR(inode->i_mode)) {
900 f2fs_i_links_write(inode, false);
901 f2fs_i_size_write(inode, 0);
903 f2fs_up_write(&F2FS_I(inode)->i_sem);
905 if (inode->i_nlink == 0)
906 f2fs_add_orphan_inode(inode);
908 f2fs_release_orphan_inode(sbi);
912 * It only removes the dentry from the dentry page, corresponding name
913 * entry in name page does not need to be touched during deletion.
915 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
916 struct inode *dir, struct inode *inode)
918 struct f2fs_dentry_block *dentry_blk;
919 unsigned int bit_pos;
920 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
923 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
925 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
926 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
928 if (f2fs_has_inline_dentry(dir))
929 return f2fs_delete_inline_entry(dentry, page, dir, inode);
932 f2fs_wait_on_page_writeback(page, DATA, true, true);
934 dentry_blk = page_address(page);
935 bit_pos = dentry - dentry_blk->dentry;
936 for (i = 0; i < slots; i++)
937 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
939 /* Let's check and deallocate this dentry page */
940 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
943 set_page_dirty(page);
945 if (bit_pos == NR_DENTRY_IN_BLOCK &&
946 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
947 f2fs_clear_page_cache_dirty_tag(page);
948 clear_page_dirty_for_io(page);
949 ClearPageUptodate(page);
951 clear_page_private_gcing(page);
953 inode_dec_dirty_pages(dir);
954 f2fs_remove_dirty_inode(dir);
956 detach_page_private(page);
957 set_page_private(page, 0);
959 f2fs_put_page(page, 1);
961 dir->i_ctime = dir->i_mtime = current_time(dir);
962 f2fs_mark_inode_dirty_sync(dir, false);
965 f2fs_drop_nlink(dir, inode);
968 bool f2fs_empty_dir(struct inode *dir)
970 unsigned long bidx = 0;
971 struct page *dentry_page;
972 unsigned int bit_pos;
973 struct f2fs_dentry_block *dentry_blk;
974 unsigned long nblock = dir_blocks(dir);
976 if (f2fs_has_inline_dentry(dir))
977 return f2fs_empty_inline_dir(dir);
979 while (bidx < nblock) {
982 dentry_page = f2fs_find_data_page(dir, bidx, &next_pgofs);
983 if (IS_ERR(dentry_page)) {
984 if (PTR_ERR(dentry_page) == -ENOENT) {
992 dentry_blk = page_address(dentry_page);
997 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
1001 f2fs_put_page(dentry_page, 0);
1003 if (bit_pos < NR_DENTRY_IN_BLOCK)
1011 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
1012 unsigned int start_pos, struct fscrypt_str *fstr)
1014 unsigned char d_type = DT_UNKNOWN;
1015 unsigned int bit_pos;
1016 struct f2fs_dir_entry *de = NULL;
1017 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
1018 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
1019 struct blk_plug plug;
1020 bool readdir_ra = sbi->readdir_ra == 1;
1021 bool found_valid_dirent = false;
1024 bit_pos = ((unsigned long)ctx->pos % d->max);
1027 blk_start_plug(&plug);
1029 while (bit_pos < d->max) {
1030 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
1031 if (bit_pos >= d->max)
1034 de = &d->dentry[bit_pos];
1035 if (de->name_len == 0) {
1036 if (found_valid_dirent || !bit_pos) {
1038 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
1039 KERN_WARNING, sbi->sb->s_id,
1040 le32_to_cpu(de->ino));
1041 set_sbi_flag(sbi, SBI_NEED_FSCK);
1044 ctx->pos = start_pos + bit_pos;
1048 d_type = f2fs_get_de_type(de);
1050 de_name.name = d->filename[bit_pos];
1051 de_name.len = le16_to_cpu(de->name_len);
1053 /* check memory boundary before moving forward */
1054 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1055 if (unlikely(bit_pos > d->max ||
1056 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1057 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1058 __func__, le16_to_cpu(de->name_len));
1059 set_sbi_flag(sbi, SBI_NEED_FSCK);
1060 err = -EFSCORRUPTED;
1061 f2fs_handle_error(sbi, ERROR_CORRUPTED_DIRENT);
1065 if (IS_ENCRYPTED(d->inode)) {
1066 int save_len = fstr->len;
1068 err = fscrypt_fname_disk_to_usr(d->inode,
1069 (u32)le32_to_cpu(de->hash_code),
1075 fstr->len = save_len;
1078 if (!dir_emit(ctx, de_name.name, de_name.len,
1079 le32_to_cpu(de->ino), d_type)) {
1085 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1087 ctx->pos = start_pos + bit_pos;
1088 found_valid_dirent = true;
1092 blk_finish_plug(&plug);
1096 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1098 struct inode *inode = file_inode(file);
1099 unsigned long npages = dir_blocks(inode);
1100 struct f2fs_dentry_block *dentry_blk = NULL;
1101 struct page *dentry_page = NULL;
1102 struct file_ra_state *ra = &file->f_ra;
1103 loff_t start_pos = ctx->pos;
1104 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1105 struct f2fs_dentry_ptr d;
1106 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1109 if (IS_ENCRYPTED(inode)) {
1110 err = fscrypt_prepare_readdir(inode);
1114 err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1119 if (f2fs_has_inline_dentry(inode)) {
1120 err = f2fs_read_inline_dir(file, ctx, &fstr);
1124 for (; n < npages; ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1127 /* allow readdir() to be interrupted */
1128 if (fatal_signal_pending(current)) {
1134 /* readahead for multi pages of dir */
1135 if (npages - n > 1 && !ra_has_index(ra, n))
1136 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1137 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1139 dentry_page = f2fs_find_data_page(inode, n, &next_pgofs);
1140 if (IS_ERR(dentry_page)) {
1141 err = PTR_ERR(dentry_page);
1142 if (err == -ENOENT) {
1151 dentry_blk = page_address(dentry_page);
1153 make_dentry_ptr_block(inode, &d, dentry_blk);
1155 err = f2fs_fill_dentries(ctx, &d,
1156 n * NR_DENTRY_IN_BLOCK, &fstr);
1158 f2fs_put_page(dentry_page, 0);
1162 f2fs_put_page(dentry_page, 0);
1167 fscrypt_fname_free_buffer(&fstr);
1169 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1170 return err < 0 ? err : 0;
1173 const struct file_operations f2fs_dir_operations = {
1174 .llseek = generic_file_llseek,
1175 .read = generic_read_dir,
1176 .iterate_shared = f2fs_readdir,
1177 .fsync = f2fs_sync_file,
1178 .unlocked_ioctl = f2fs_ioctl,
1179 #ifdef CONFIG_COMPAT
1180 .compat_ioctl = f2fs_compat_ioctl,