1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
9 #include <linux/f2fs_fs.h>
10 #include <linux/sched/signal.h>
11 #include <linux/unicode.h>
16 #include <trace/events/f2fs.h>
18 static unsigned long dir_blocks(struct inode
*inode
)
20 return ((unsigned long long) (i_size_read(inode
) + PAGE_SIZE
- 1))
24 static unsigned int dir_buckets(unsigned int level
, int dir_level
)
26 if (level
+ dir_level
< MAX_DIR_HASH_DEPTH
/ 2)
27 return 1 << (level
+ dir_level
);
29 return MAX_DIR_BUCKETS
;
32 static unsigned int bucket_blocks(unsigned int level
)
34 if (level
< MAX_DIR_HASH_DEPTH
/ 2)
40 static unsigned char f2fs_filetype_table
[F2FS_FT_MAX
] = {
41 [F2FS_FT_UNKNOWN
] = DT_UNKNOWN
,
42 [F2FS_FT_REG_FILE
] = DT_REG
,
43 [F2FS_FT_DIR
] = DT_DIR
,
44 [F2FS_FT_CHRDEV
] = DT_CHR
,
45 [F2FS_FT_BLKDEV
] = DT_BLK
,
46 [F2FS_FT_FIFO
] = DT_FIFO
,
47 [F2FS_FT_SOCK
] = DT_SOCK
,
48 [F2FS_FT_SYMLINK
] = DT_LNK
,
51 static unsigned char f2fs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
52 [S_IFREG
>> S_SHIFT
] = F2FS_FT_REG_FILE
,
53 [S_IFDIR
>> S_SHIFT
] = F2FS_FT_DIR
,
54 [S_IFCHR
>> S_SHIFT
] = F2FS_FT_CHRDEV
,
55 [S_IFBLK
>> S_SHIFT
] = F2FS_FT_BLKDEV
,
56 [S_IFIFO
>> S_SHIFT
] = F2FS_FT_FIFO
,
57 [S_IFSOCK
>> S_SHIFT
] = F2FS_FT_SOCK
,
58 [S_IFLNK
>> S_SHIFT
] = F2FS_FT_SYMLINK
,
61 static void set_de_type(struct f2fs_dir_entry
*de
, umode_t mode
)
63 de
->file_type
= f2fs_type_by_mode
[(mode
& S_IFMT
) >> S_SHIFT
];
66 unsigned char f2fs_get_de_type(struct f2fs_dir_entry
*de
)
68 if (de
->file_type
< F2FS_FT_MAX
)
69 return f2fs_filetype_table
[de
->file_type
];
73 static unsigned long dir_block_index(unsigned int level
,
74 int dir_level
, unsigned int idx
)
77 unsigned long bidx
= 0;
79 for (i
= 0; i
< level
; i
++)
80 bidx
+= dir_buckets(i
, dir_level
) * bucket_blocks(i
);
81 bidx
+= idx
* bucket_blocks(level
);
85 static struct f2fs_dir_entry
*find_in_block(struct inode
*dir
,
86 struct page
*dentry_page
,
87 struct fscrypt_name
*fname
,
90 struct page
**res_page
)
92 struct f2fs_dentry_block
*dentry_blk
;
93 struct f2fs_dir_entry
*de
;
94 struct f2fs_dentry_ptr d
;
96 dentry_blk
= (struct f2fs_dentry_block
*)page_address(dentry_page
);
98 make_dentry_ptr_block(dir
, &d
, dentry_blk
);
99 de
= f2fs_find_target_dentry(fname
, namehash
, max_slots
, &d
);
101 *res_page
= dentry_page
;
106 #ifdef CONFIG_UNICODE
108 * Test whether a case-insensitive directory entry matches the filename
109 * being searched for.
111 static bool f2fs_match_ci_name(const struct inode
*dir
, const struct qstr
*name
,
112 const struct qstr
*entry
, bool quick
)
114 const struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
115 const struct unicode_map
*um
= sbi
->s_encoding
;
119 res
= utf8_strncasecmp_folded(um
, name
, entry
);
121 res
= utf8_strncasecmp(um
, name
, entry
);
124 * In strict mode, ignore invalid names. In non-strict mode,
125 * fall back to treating them as opaque byte sequences.
127 if (f2fs_has_strict_mode(sbi
) || name
->len
!= entry
->len
)
129 return !memcmp(name
->name
, entry
->name
, name
->len
);
134 static void f2fs_fname_setup_ci_filename(struct inode
*dir
,
135 const struct qstr
*iname
,
136 struct fscrypt_str
*cf_name
)
138 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
140 if (!IS_CASEFOLDED(dir
)) {
141 cf_name
->name
= NULL
;
145 cf_name
->name
= f2fs_kmalloc(sbi
, F2FS_NAME_LEN
, GFP_NOFS
);
149 cf_name
->len
= utf8_casefold(sbi
->s_encoding
,
150 iname
, cf_name
->name
,
152 if ((int)cf_name
->len
<= 0) {
153 kvfree(cf_name
->name
);
154 cf_name
->name
= NULL
;
159 static inline bool f2fs_match_name(struct f2fs_dentry_ptr
*d
,
160 struct f2fs_dir_entry
*de
,
161 struct fscrypt_name
*fname
,
162 struct fscrypt_str
*cf_str
,
163 unsigned long bit_pos
,
164 f2fs_hash_t namehash
)
166 #ifdef CONFIG_UNICODE
167 struct inode
*parent
= d
->inode
;
168 struct f2fs_sb_info
*sbi
= F2FS_I_SB(parent
);
172 if (de
->hash_code
!= namehash
)
175 #ifdef CONFIG_UNICODE
176 entry
.name
= d
->filename
[bit_pos
];
177 entry
.len
= de
->name_len
;
179 if (sbi
->s_encoding
&& IS_CASEFOLDED(parent
)) {
181 struct qstr cf
= {.name
= cf_str
->name
,
183 return f2fs_match_ci_name(parent
, &cf
, &entry
, true);
185 return f2fs_match_ci_name(parent
, fname
->usr_fname
, &entry
,
189 if (fscrypt_match_name(fname
, d
->filename
[bit_pos
],
190 le16_to_cpu(de
->name_len
)))
195 struct f2fs_dir_entry
*f2fs_find_target_dentry(struct fscrypt_name
*fname
,
196 f2fs_hash_t namehash
, int *max_slots
,
197 struct f2fs_dentry_ptr
*d
)
199 struct f2fs_dir_entry
*de
;
200 struct fscrypt_str cf_str
= { .name
= NULL
, .len
= 0 };
201 unsigned long bit_pos
= 0;
204 #ifdef CONFIG_UNICODE
205 f2fs_fname_setup_ci_filename(d
->inode
, fname
->usr_fname
, &cf_str
);
210 while (bit_pos
< d
->max
) {
211 if (!test_bit_le(bit_pos
, d
->bitmap
)) {
217 de
= &d
->dentry
[bit_pos
];
219 if (unlikely(!de
->name_len
)) {
224 if (f2fs_match_name(d
, de
, fname
, &cf_str
, bit_pos
, namehash
))
227 if (max_slots
&& max_len
> *max_slots
)
228 *max_slots
= max_len
;
231 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
236 if (max_slots
&& max_len
> *max_slots
)
237 *max_slots
= max_len
;
239 #ifdef CONFIG_UNICODE
245 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
247 struct fscrypt_name
*fname
,
248 struct page
**res_page
)
250 struct qstr name
= FSTR_TO_QSTR(&fname
->disk_name
);
251 int s
= GET_DENTRY_SLOTS(name
.len
);
252 unsigned int nbucket
, nblock
;
253 unsigned int bidx
, end_block
;
254 struct page
*dentry_page
;
255 struct f2fs_dir_entry
*de
= NULL
;
258 f2fs_hash_t namehash
= f2fs_dentry_hash(dir
, &name
, fname
);
260 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
261 nblock
= bucket_blocks(level
);
263 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
264 le32_to_cpu(namehash
) % nbucket
);
265 end_block
= bidx
+ nblock
;
267 for (; bidx
< end_block
; bidx
++) {
268 /* no need to allocate new dentry pages to all the indices */
269 dentry_page
= f2fs_find_data_page(dir
, bidx
);
270 if (IS_ERR(dentry_page
)) {
271 if (PTR_ERR(dentry_page
) == -ENOENT
) {
275 *res_page
= dentry_page
;
280 de
= find_in_block(dir
, dentry_page
, fname
, namehash
,
281 &max_slots
, res_page
);
287 f2fs_put_page(dentry_page
, 0);
290 if (!de
&& room
&& F2FS_I(dir
)->chash
!= namehash
) {
291 F2FS_I(dir
)->chash
= namehash
;
292 F2FS_I(dir
)->clevel
= level
;
298 struct f2fs_dir_entry
*__f2fs_find_entry(struct inode
*dir
,
299 struct fscrypt_name
*fname
, struct page
**res_page
)
301 unsigned long npages
= dir_blocks(dir
);
302 struct f2fs_dir_entry
*de
= NULL
;
303 unsigned int max_depth
;
306 if (f2fs_has_inline_dentry(dir
)) {
308 de
= f2fs_find_in_inline_dir(dir
, fname
, res_page
);
317 max_depth
= F2FS_I(dir
)->i_current_depth
;
318 if (unlikely(max_depth
> MAX_DIR_HASH_DEPTH
)) {
319 f2fs_warn(F2FS_I_SB(dir
), "Corrupted max_depth of %lu: %u",
320 dir
->i_ino
, max_depth
);
321 max_depth
= MAX_DIR_HASH_DEPTH
;
322 f2fs_i_depth_write(dir
, max_depth
);
325 for (level
= 0; level
< max_depth
; level
++) {
327 de
= find_in_level(dir
, level
, fname
, res_page
);
328 if (de
|| IS_ERR(*res_page
))
332 /* This is to increase the speed of f2fs_create */
334 F2FS_I(dir
)->task
= current
;
339 * Find an entry in the specified directory with the wanted name.
340 * It returns the page where the entry was found (as a parameter - res_page),
341 * and the entry itself. Page is returned mapped and unlocked.
342 * Entry is guaranteed to be valid.
344 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
345 const struct qstr
*child
, struct page
**res_page
)
347 struct f2fs_dir_entry
*de
= NULL
;
348 struct fscrypt_name fname
;
351 #ifdef CONFIG_UNICODE
352 if (f2fs_has_strict_mode(F2FS_I_SB(dir
)) && IS_CASEFOLDED(dir
) &&
353 utf8_validate(F2FS_I_SB(dir
)->s_encoding
, child
)) {
354 *res_page
= ERR_PTR(-EINVAL
);
359 err
= fscrypt_setup_filename(dir
, child
, 1, &fname
);
364 *res_page
= ERR_PTR(err
);
368 de
= __f2fs_find_entry(dir
, &fname
, res_page
);
370 fscrypt_free_filename(&fname
);
374 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
376 struct qstr dotdot
= QSTR_INIT("..", 2);
378 return f2fs_find_entry(dir
, &dotdot
, p
);
381 ino_t
f2fs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
,
385 struct f2fs_dir_entry
*de
;
387 de
= f2fs_find_entry(dir
, qstr
, page
);
389 res
= le32_to_cpu(de
->ino
);
390 f2fs_put_page(*page
, 0);
396 void f2fs_set_link(struct inode
*dir
, struct f2fs_dir_entry
*de
,
397 struct page
*page
, struct inode
*inode
)
399 enum page_type type
= f2fs_has_inline_dentry(dir
) ? NODE
: DATA
;
401 f2fs_wait_on_page_writeback(page
, type
, true, true);
402 de
->ino
= cpu_to_le32(inode
->i_ino
);
403 set_de_type(de
, inode
->i_mode
);
404 set_page_dirty(page
);
406 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
407 f2fs_mark_inode_dirty_sync(dir
, false);
408 f2fs_put_page(page
, 1);
411 static void init_dent_inode(const struct qstr
*name
, struct page
*ipage
)
413 struct f2fs_inode
*ri
;
415 f2fs_wait_on_page_writeback(ipage
, NODE
, true, true);
417 /* copy name info. to this inode page */
418 ri
= F2FS_INODE(ipage
);
419 ri
->i_namelen
= cpu_to_le32(name
->len
);
420 memcpy(ri
->i_name
, name
->name
, name
->len
);
421 set_page_dirty(ipage
);
424 void f2fs_do_make_empty_dir(struct inode
*inode
, struct inode
*parent
,
425 struct f2fs_dentry_ptr
*d
)
427 struct qstr dot
= QSTR_INIT(".", 1);
428 struct qstr dotdot
= QSTR_INIT("..", 2);
430 /* update dirent of "." */
431 f2fs_update_dentry(inode
->i_ino
, inode
->i_mode
, d
, &dot
, 0, 0);
433 /* update dirent of ".." */
434 f2fs_update_dentry(parent
->i_ino
, parent
->i_mode
, d
, &dotdot
, 0, 1);
437 static int make_empty_dir(struct inode
*inode
,
438 struct inode
*parent
, struct page
*page
)
440 struct page
*dentry_page
;
441 struct f2fs_dentry_block
*dentry_blk
;
442 struct f2fs_dentry_ptr d
;
444 if (f2fs_has_inline_dentry(inode
))
445 return f2fs_make_empty_inline_dir(inode
, parent
, page
);
447 dentry_page
= f2fs_get_new_data_page(inode
, page
, 0, true);
448 if (IS_ERR(dentry_page
))
449 return PTR_ERR(dentry_page
);
451 dentry_blk
= page_address(dentry_page
);
453 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
454 f2fs_do_make_empty_dir(inode
, parent
, &d
);
456 set_page_dirty(dentry_page
);
457 f2fs_put_page(dentry_page
, 1);
461 struct page
*f2fs_init_inode_metadata(struct inode
*inode
, struct inode
*dir
,
462 const struct qstr
*new_name
, const struct qstr
*orig_name
,
468 if (is_inode_flag_set(inode
, FI_NEW_INODE
)) {
469 page
= f2fs_new_inode_page(inode
);
473 if (S_ISDIR(inode
->i_mode
)) {
474 /* in order to handle error case */
476 err
= make_empty_dir(inode
, dir
, page
);
484 err
= f2fs_init_acl(inode
, dir
, page
, dpage
);
488 err
= f2fs_init_security(inode
, dir
, orig_name
, page
);
492 if (IS_ENCRYPTED(inode
)) {
493 err
= fscrypt_inherit_context(dir
, inode
, page
, false);
498 page
= f2fs_get_node_page(F2FS_I_SB(dir
), inode
->i_ino
);
504 init_dent_inode(new_name
, page
);
505 if (IS_ENCRYPTED(dir
))
506 file_set_enc_name(inode
);
510 * This file should be checkpointed during fsync.
511 * We lost i_pino from now on.
513 if (is_inode_flag_set(inode
, FI_INC_LINK
)) {
514 if (!S_ISDIR(inode
->i_mode
))
515 file_lost_pino(inode
);
517 * If link the tmpfile to alias through linkat path,
518 * we should remove this inode from orphan list.
520 if (inode
->i_nlink
== 0)
521 f2fs_remove_orphan_inode(F2FS_I_SB(dir
), inode
->i_ino
);
522 f2fs_i_links_write(inode
, true);
528 f2fs_update_inode(inode
, page
);
529 f2fs_put_page(page
, 1);
533 void f2fs_update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
534 unsigned int current_depth
)
536 if (inode
&& is_inode_flag_set(inode
, FI_NEW_INODE
)) {
537 if (S_ISDIR(inode
->i_mode
))
538 f2fs_i_links_write(dir
, true);
539 clear_inode_flag(inode
, FI_NEW_INODE
);
541 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
542 f2fs_mark_inode_dirty_sync(dir
, false);
544 if (F2FS_I(dir
)->i_current_depth
!= current_depth
)
545 f2fs_i_depth_write(dir
, current_depth
);
547 if (inode
&& is_inode_flag_set(inode
, FI_INC_LINK
))
548 clear_inode_flag(inode
, FI_INC_LINK
);
551 int f2fs_room_for_filename(const void *bitmap
, int slots
, int max_slots
)
554 int zero_start
, zero_end
;
556 zero_start
= find_next_zero_bit_le(bitmap
, max_slots
, bit_start
);
557 if (zero_start
>= max_slots
)
560 zero_end
= find_next_bit_le(bitmap
, max_slots
, zero_start
);
561 if (zero_end
- zero_start
>= slots
)
564 bit_start
= zero_end
+ 1;
566 if (zero_end
+ 1 >= max_slots
)
571 bool f2fs_has_enough_room(struct inode
*dir
, struct page
*ipage
,
572 struct fscrypt_name
*fname
)
574 struct f2fs_dentry_ptr d
;
575 unsigned int bit_pos
;
576 int slots
= GET_DENTRY_SLOTS(fname_len(fname
));
578 make_dentry_ptr_inline(dir
, &d
, inline_data_addr(dir
, ipage
));
580 bit_pos
= f2fs_room_for_filename(d
.bitmap
, slots
, d
.max
);
582 return bit_pos
< d
.max
;
585 void f2fs_update_dentry(nid_t ino
, umode_t mode
, struct f2fs_dentry_ptr
*d
,
586 const struct qstr
*name
, f2fs_hash_t name_hash
,
587 unsigned int bit_pos
)
589 struct f2fs_dir_entry
*de
;
590 int slots
= GET_DENTRY_SLOTS(name
->len
);
593 de
= &d
->dentry
[bit_pos
];
594 de
->hash_code
= name_hash
;
595 de
->name_len
= cpu_to_le16(name
->len
);
596 memcpy(d
->filename
[bit_pos
], name
->name
, name
->len
);
597 de
->ino
= cpu_to_le32(ino
);
598 set_de_type(de
, mode
);
599 for (i
= 0; i
< slots
; i
++) {
600 __set_bit_le(bit_pos
+ i
, (void *)d
->bitmap
);
601 /* avoid wrong garbage data for readdir */
603 (de
+ i
)->name_len
= 0;
607 int f2fs_add_regular_entry(struct inode
*dir
, const struct qstr
*new_name
,
608 const struct qstr
*orig_name
,
609 struct inode
*inode
, nid_t ino
, umode_t mode
)
611 unsigned int bit_pos
;
613 unsigned int current_depth
;
614 unsigned long bidx
, block
;
615 f2fs_hash_t dentry_hash
;
616 unsigned int nbucket
, nblock
;
617 struct page
*dentry_page
= NULL
;
618 struct f2fs_dentry_block
*dentry_blk
= NULL
;
619 struct f2fs_dentry_ptr d
;
620 struct page
*page
= NULL
;
624 slots
= GET_DENTRY_SLOTS(new_name
->len
);
625 dentry_hash
= f2fs_dentry_hash(dir
, new_name
, NULL
);
627 current_depth
= F2FS_I(dir
)->i_current_depth
;
628 if (F2FS_I(dir
)->chash
== dentry_hash
) {
629 level
= F2FS_I(dir
)->clevel
;
630 F2FS_I(dir
)->chash
= 0;
634 if (time_to_inject(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
)) {
635 f2fs_show_injection_info(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
);
639 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
642 /* Increase the depth, if required */
643 if (level
== current_depth
)
646 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
647 nblock
= bucket_blocks(level
);
649 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
650 (le32_to_cpu(dentry_hash
) % nbucket
));
652 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
653 dentry_page
= f2fs_get_new_data_page(dir
, NULL
, block
, true);
654 if (IS_ERR(dentry_page
))
655 return PTR_ERR(dentry_page
);
657 dentry_blk
= page_address(dentry_page
);
658 bit_pos
= f2fs_room_for_filename(&dentry_blk
->dentry_bitmap
,
659 slots
, NR_DENTRY_IN_BLOCK
);
660 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
663 f2fs_put_page(dentry_page
, 1);
666 /* Move to next level to find the empty slot for new dentry */
670 f2fs_wait_on_page_writeback(dentry_page
, DATA
, true, true);
673 down_write(&F2FS_I(inode
)->i_sem
);
674 page
= f2fs_init_inode_metadata(inode
, dir
, new_name
,
682 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
683 f2fs_update_dentry(ino
, mode
, &d
, new_name
, dentry_hash
, bit_pos
);
685 set_page_dirty(dentry_page
);
688 f2fs_i_pino_write(inode
, dir
->i_ino
);
690 /* synchronize inode page's data from inode cache */
691 if (is_inode_flag_set(inode
, FI_NEW_INODE
))
692 f2fs_update_inode(inode
, page
);
694 f2fs_put_page(page
, 1);
697 f2fs_update_parent_metadata(dir
, inode
, current_depth
);
700 up_write(&F2FS_I(inode
)->i_sem
);
702 f2fs_put_page(dentry_page
, 1);
707 int f2fs_add_dentry(struct inode
*dir
, struct fscrypt_name
*fname
,
708 struct inode
*inode
, nid_t ino
, umode_t mode
)
710 struct qstr new_name
;
713 new_name
.name
= fname_name(fname
);
714 new_name
.len
= fname_len(fname
);
716 if (f2fs_has_inline_dentry(dir
))
717 err
= f2fs_add_inline_entry(dir
, &new_name
, fname
->usr_fname
,
720 err
= f2fs_add_regular_entry(dir
, &new_name
, fname
->usr_fname
,
723 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
728 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
731 int f2fs_do_add_link(struct inode
*dir
, const struct qstr
*name
,
732 struct inode
*inode
, nid_t ino
, umode_t mode
)
734 struct fscrypt_name fname
;
735 struct page
*page
= NULL
;
736 struct f2fs_dir_entry
*de
= NULL
;
739 err
= fscrypt_setup_filename(dir
, name
, 0, &fname
);
744 * An immature stakable filesystem shows a race condition between lookup
745 * and create. If we have same task when doing lookup and create, it's
746 * definitely fine as expected by VFS normally. Otherwise, let's just
747 * verify on-disk dentry one more time, which guarantees filesystem
750 if (current
!= F2FS_I(dir
)->task
) {
751 de
= __f2fs_find_entry(dir
, &fname
, &page
);
752 F2FS_I(dir
)->task
= NULL
;
755 f2fs_put_page(page
, 0);
757 } else if (IS_ERR(page
)) {
760 err
= f2fs_add_dentry(dir
, &fname
, inode
, ino
, mode
);
762 fscrypt_free_filename(&fname
);
766 int f2fs_do_tmpfile(struct inode
*inode
, struct inode
*dir
)
771 down_write(&F2FS_I(inode
)->i_sem
);
772 page
= f2fs_init_inode_metadata(inode
, dir
, NULL
, NULL
, NULL
);
777 f2fs_put_page(page
, 1);
779 clear_inode_flag(inode
, FI_NEW_INODE
);
780 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
782 up_write(&F2FS_I(inode
)->i_sem
);
786 void f2fs_drop_nlink(struct inode
*dir
, struct inode
*inode
)
788 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
790 down_write(&F2FS_I(inode
)->i_sem
);
792 if (S_ISDIR(inode
->i_mode
))
793 f2fs_i_links_write(dir
, false);
794 inode
->i_ctime
= current_time(inode
);
796 f2fs_i_links_write(inode
, false);
797 if (S_ISDIR(inode
->i_mode
)) {
798 f2fs_i_links_write(inode
, false);
799 f2fs_i_size_write(inode
, 0);
801 up_write(&F2FS_I(inode
)->i_sem
);
803 if (inode
->i_nlink
== 0)
804 f2fs_add_orphan_inode(inode
);
806 f2fs_release_orphan_inode(sbi
);
810 * It only removes the dentry from the dentry page, corresponding name
811 * entry in name page does not need to be touched during deletion.
813 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
814 struct inode
*dir
, struct inode
*inode
)
816 struct f2fs_dentry_block
*dentry_blk
;
817 unsigned int bit_pos
;
818 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
821 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
823 if (F2FS_OPTION(F2FS_I_SB(dir
)).fsync_mode
== FSYNC_MODE_STRICT
)
824 f2fs_add_ino_entry(F2FS_I_SB(dir
), dir
->i_ino
, TRANS_DIR_INO
);
826 if (f2fs_has_inline_dentry(dir
))
827 return f2fs_delete_inline_entry(dentry
, page
, dir
, inode
);
830 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
832 dentry_blk
= page_address(page
);
833 bit_pos
= dentry
- dentry_blk
->dentry
;
834 for (i
= 0; i
< slots
; i
++)
835 __clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
837 /* Let's check and deallocate this dentry page */
838 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
841 set_page_dirty(page
);
843 if (bit_pos
== NR_DENTRY_IN_BLOCK
&&
844 !f2fs_truncate_hole(dir
, page
->index
, page
->index
+ 1)) {
845 f2fs_clear_page_cache_dirty_tag(page
);
846 clear_page_dirty_for_io(page
);
847 f2fs_clear_page_private(page
);
848 ClearPageUptodate(page
);
849 clear_cold_data(page
);
850 inode_dec_dirty_pages(dir
);
851 f2fs_remove_dirty_inode(dir
);
853 f2fs_put_page(page
, 1);
855 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
856 f2fs_mark_inode_dirty_sync(dir
, false);
859 f2fs_drop_nlink(dir
, inode
);
862 bool f2fs_empty_dir(struct inode
*dir
)
865 struct page
*dentry_page
;
866 unsigned int bit_pos
;
867 struct f2fs_dentry_block
*dentry_blk
;
868 unsigned long nblock
= dir_blocks(dir
);
870 if (f2fs_has_inline_dentry(dir
))
871 return f2fs_empty_inline_dir(dir
);
873 for (bidx
= 0; bidx
< nblock
; bidx
++) {
874 dentry_page
= f2fs_get_lock_data_page(dir
, bidx
, false);
875 if (IS_ERR(dentry_page
)) {
876 if (PTR_ERR(dentry_page
) == -ENOENT
)
882 dentry_blk
= page_address(dentry_page
);
887 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
891 f2fs_put_page(dentry_page
, 1);
893 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
899 int f2fs_fill_dentries(struct dir_context
*ctx
, struct f2fs_dentry_ptr
*d
,
900 unsigned int start_pos
, struct fscrypt_str
*fstr
)
902 unsigned char d_type
= DT_UNKNOWN
;
903 unsigned int bit_pos
;
904 struct f2fs_dir_entry
*de
= NULL
;
905 struct fscrypt_str de_name
= FSTR_INIT(NULL
, 0);
906 struct f2fs_sb_info
*sbi
= F2FS_I_SB(d
->inode
);
907 struct blk_plug plug
;
908 bool readdir_ra
= sbi
->readdir_ra
== 1;
911 bit_pos
= ((unsigned long)ctx
->pos
% d
->max
);
914 blk_start_plug(&plug
);
916 while (bit_pos
< d
->max
) {
917 bit_pos
= find_next_bit_le(d
->bitmap
, d
->max
, bit_pos
);
918 if (bit_pos
>= d
->max
)
921 de
= &d
->dentry
[bit_pos
];
922 if (de
->name_len
== 0) {
924 ctx
->pos
= start_pos
+ bit_pos
;
926 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
927 KERN_WARNING
, sbi
->sb
->s_id
,
928 le32_to_cpu(de
->ino
));
929 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
933 d_type
= f2fs_get_de_type(de
);
935 de_name
.name
= d
->filename
[bit_pos
];
936 de_name
.len
= le16_to_cpu(de
->name_len
);
938 /* check memory boundary before moving forward */
939 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
940 if (unlikely(bit_pos
> d
->max
||
941 le16_to_cpu(de
->name_len
) > F2FS_NAME_LEN
)) {
942 f2fs_warn(sbi
, "%s: corrupted namelen=%d, run fsck to fix.",
943 __func__
, le16_to_cpu(de
->name_len
));
944 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
949 if (IS_ENCRYPTED(d
->inode
)) {
950 int save_len
= fstr
->len
;
952 err
= fscrypt_fname_disk_to_usr(d
->inode
,
953 (u32
)le32_to_cpu(de
->hash_code
),
959 fstr
->len
= save_len
;
962 if (!dir_emit(ctx
, de_name
.name
, de_name
.len
,
963 le32_to_cpu(de
->ino
), d_type
)) {
969 f2fs_ra_node_page(sbi
, le32_to_cpu(de
->ino
));
971 ctx
->pos
= start_pos
+ bit_pos
;
975 blk_finish_plug(&plug
);
979 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
981 struct inode
*inode
= file_inode(file
);
982 unsigned long npages
= dir_blocks(inode
);
983 struct f2fs_dentry_block
*dentry_blk
= NULL
;
984 struct page
*dentry_page
= NULL
;
985 struct file_ra_state
*ra
= &file
->f_ra
;
986 loff_t start_pos
= ctx
->pos
;
987 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
988 struct f2fs_dentry_ptr d
;
989 struct fscrypt_str fstr
= FSTR_INIT(NULL
, 0);
992 if (IS_ENCRYPTED(inode
)) {
993 err
= fscrypt_get_encryption_info(inode
);
997 err
= fscrypt_fname_alloc_buffer(inode
, F2FS_NAME_LEN
, &fstr
);
1002 if (f2fs_has_inline_dentry(inode
)) {
1003 err
= f2fs_read_inline_dir(file
, ctx
, &fstr
);
1007 for (; n
< npages
; n
++, ctx
->pos
= n
* NR_DENTRY_IN_BLOCK
) {
1009 /* allow readdir() to be interrupted */
1010 if (fatal_signal_pending(current
)) {
1016 /* readahead for multi pages of dir */
1017 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
1018 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
1019 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
1021 dentry_page
= f2fs_find_data_page(inode
, n
);
1022 if (IS_ERR(dentry_page
)) {
1023 err
= PTR_ERR(dentry_page
);
1024 if (err
== -ENOENT
) {
1032 dentry_blk
= page_address(dentry_page
);
1034 make_dentry_ptr_block(inode
, &d
, dentry_blk
);
1036 err
= f2fs_fill_dentries(ctx
, &d
,
1037 n
* NR_DENTRY_IN_BLOCK
, &fstr
);
1039 f2fs_put_page(dentry_page
, 0);
1043 f2fs_put_page(dentry_page
, 0);
1046 fscrypt_fname_free_buffer(&fstr
);
1048 trace_f2fs_readdir(inode
, start_pos
, ctx
->pos
, err
);
1049 return err
< 0 ? err
: 0;
1052 static int f2fs_dir_open(struct inode
*inode
, struct file
*filp
)
1054 if (IS_ENCRYPTED(inode
))
1055 return fscrypt_get_encryption_info(inode
) ? -EACCES
: 0;
1059 const struct file_operations f2fs_dir_operations
= {
1060 .llseek
= generic_file_llseek
,
1061 .read
= generic_read_dir
,
1062 .iterate_shared
= f2fs_readdir
,
1063 .fsync
= f2fs_sync_file
,
1064 .open
= f2fs_dir_open
,
1065 .unlocked_ioctl
= f2fs_ioctl
,
1066 #ifdef CONFIG_COMPAT
1067 .compat_ioctl
= f2fs_compat_ioctl
,
1071 #ifdef CONFIG_UNICODE
1072 static int f2fs_d_compare(const struct dentry
*dentry
, unsigned int len
,
1073 const char *str
, const struct qstr
*name
)
1075 const struct dentry
*parent
= READ_ONCE(dentry
->d_parent
);
1076 const struct inode
*dir
= READ_ONCE(parent
->d_inode
);
1077 const struct f2fs_sb_info
*sbi
= F2FS_SB(dentry
->d_sb
);
1078 struct qstr entry
= QSTR_INIT(str
, len
);
1079 char strbuf
[DNAME_INLINE_LEN
];
1082 if (!dir
|| !IS_CASEFOLDED(dir
))
1086 * If the dentry name is stored in-line, then it may be concurrently
1087 * modified by a rename. If this happens, the VFS will eventually retry
1088 * the lookup, so it doesn't matter what ->d_compare() returns.
1089 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1090 * string. Therefore, we have to copy the name into a temporary buffer.
1092 if (len
<= DNAME_INLINE_LEN
- 1) {
1093 memcpy(strbuf
, str
, len
);
1095 entry
.name
= strbuf
;
1096 /* prevent compiler from optimizing out the temporary buffer */
1100 res
= utf8_strncasecmp(sbi
->s_encoding
, name
, &entry
);
1104 if (f2fs_has_strict_mode(sbi
))
1107 if (len
!= name
->len
)
1109 return !!memcmp(str
, name
->name
, len
);
1112 static int f2fs_d_hash(const struct dentry
*dentry
, struct qstr
*str
)
1114 struct f2fs_sb_info
*sbi
= F2FS_SB(dentry
->d_sb
);
1115 const struct unicode_map
*um
= sbi
->s_encoding
;
1116 const struct inode
*inode
= READ_ONCE(dentry
->d_inode
);
1117 unsigned char *norm
;
1120 if (!inode
|| !IS_CASEFOLDED(inode
))
1123 norm
= f2fs_kmalloc(sbi
, PATH_MAX
, GFP_ATOMIC
);
1127 len
= utf8_casefold(um
, str
, norm
, PATH_MAX
);
1129 if (f2fs_has_strict_mode(sbi
))
1133 str
->hash
= full_name_hash(dentry
, norm
, len
);
1139 const struct dentry_operations f2fs_dentry_ops
= {
1140 .d_hash
= f2fs_d_hash
,
1141 .d_compare
= f2fs_d_compare
,