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 * Returns: 0 if the directory entry matches, more than 0 if it
112 * doesn't match or less than zero on error.
114 int f2fs_ci_compare(const struct inode
*parent
, const struct qstr
*name
,
115 const struct qstr
*entry
, bool quick
)
117 const struct f2fs_sb_info
*sbi
= F2FS_SB(parent
->i_sb
);
118 const struct unicode_map
*um
= sbi
->s_encoding
;
122 ret
= utf8_strncasecmp_folded(um
, name
, entry
);
124 ret
= utf8_strncasecmp(um
, name
, entry
);
127 /* Handle invalid character sequence as either an error
128 * or as an opaque byte sequence.
130 if (f2fs_has_strict_mode(sbi
))
133 if (name
->len
!= entry
->len
)
136 return !!memcmp(name
->name
, entry
->name
, name
->len
);
142 static void f2fs_fname_setup_ci_filename(struct inode
*dir
,
143 const struct qstr
*iname
,
144 struct fscrypt_str
*cf_name
)
146 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
148 if (!IS_CASEFOLDED(dir
)) {
149 cf_name
->name
= NULL
;
153 cf_name
->name
= f2fs_kmalloc(sbi
, F2FS_NAME_LEN
, GFP_NOFS
);
157 cf_name
->len
= utf8_casefold(sbi
->s_encoding
,
158 iname
, cf_name
->name
,
160 if ((int)cf_name
->len
<= 0) {
161 kvfree(cf_name
->name
);
162 cf_name
->name
= NULL
;
167 static inline bool f2fs_match_name(struct f2fs_dentry_ptr
*d
,
168 struct f2fs_dir_entry
*de
,
169 struct fscrypt_name
*fname
,
170 struct fscrypt_str
*cf_str
,
171 unsigned long bit_pos
,
172 f2fs_hash_t namehash
)
174 #ifdef CONFIG_UNICODE
175 struct inode
*parent
= d
->inode
;
176 struct f2fs_sb_info
*sbi
= F2FS_I_SB(parent
);
180 if (de
->hash_code
!= namehash
)
183 #ifdef CONFIG_UNICODE
184 entry
.name
= d
->filename
[bit_pos
];
185 entry
.len
= de
->name_len
;
187 if (sbi
->s_encoding
&& IS_CASEFOLDED(parent
)) {
189 struct qstr cf
= {.name
= cf_str
->name
,
191 return !f2fs_ci_compare(parent
, &cf
, &entry
, true);
193 return !f2fs_ci_compare(parent
, fname
->usr_fname
, &entry
,
197 if (fscrypt_match_name(fname
, d
->filename
[bit_pos
],
198 le16_to_cpu(de
->name_len
)))
203 struct f2fs_dir_entry
*f2fs_find_target_dentry(struct fscrypt_name
*fname
,
204 f2fs_hash_t namehash
, int *max_slots
,
205 struct f2fs_dentry_ptr
*d
)
207 struct f2fs_dir_entry
*de
;
208 struct fscrypt_str cf_str
= { .name
= NULL
, .len
= 0 };
209 unsigned long bit_pos
= 0;
212 #ifdef CONFIG_UNICODE
213 f2fs_fname_setup_ci_filename(d
->inode
, fname
->usr_fname
, &cf_str
);
218 while (bit_pos
< d
->max
) {
219 if (!test_bit_le(bit_pos
, d
->bitmap
)) {
225 de
= &d
->dentry
[bit_pos
];
227 if (unlikely(!de
->name_len
)) {
232 if (f2fs_match_name(d
, de
, fname
, &cf_str
, bit_pos
, namehash
))
235 if (max_slots
&& max_len
> *max_slots
)
236 *max_slots
= max_len
;
239 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
244 if (max_slots
&& max_len
> *max_slots
)
245 *max_slots
= max_len
;
247 #ifdef CONFIG_UNICODE
253 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
255 struct fscrypt_name
*fname
,
256 struct page
**res_page
)
258 struct qstr name
= FSTR_TO_QSTR(&fname
->disk_name
);
259 int s
= GET_DENTRY_SLOTS(name
.len
);
260 unsigned int nbucket
, nblock
;
261 unsigned int bidx
, end_block
;
262 struct page
*dentry_page
;
263 struct f2fs_dir_entry
*de
= NULL
;
266 f2fs_hash_t namehash
= f2fs_dentry_hash(dir
, &name
, fname
);
268 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
269 nblock
= bucket_blocks(level
);
271 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
272 le32_to_cpu(namehash
) % nbucket
);
273 end_block
= bidx
+ nblock
;
275 for (; bidx
< end_block
; bidx
++) {
276 /* no need to allocate new dentry pages to all the indices */
277 dentry_page
= f2fs_find_data_page(dir
, bidx
);
278 if (IS_ERR(dentry_page
)) {
279 if (PTR_ERR(dentry_page
) == -ENOENT
) {
283 *res_page
= dentry_page
;
288 de
= find_in_block(dir
, dentry_page
, fname
, namehash
,
289 &max_slots
, res_page
);
295 f2fs_put_page(dentry_page
, 0);
298 if (!de
&& room
&& F2FS_I(dir
)->chash
!= namehash
) {
299 F2FS_I(dir
)->chash
= namehash
;
300 F2FS_I(dir
)->clevel
= level
;
306 struct f2fs_dir_entry
*__f2fs_find_entry(struct inode
*dir
,
307 struct fscrypt_name
*fname
, struct page
**res_page
)
309 unsigned long npages
= dir_blocks(dir
);
310 struct f2fs_dir_entry
*de
= NULL
;
311 unsigned int max_depth
;
314 if (f2fs_has_inline_dentry(dir
)) {
316 de
= f2fs_find_in_inline_dir(dir
, fname
, res_page
);
325 max_depth
= F2FS_I(dir
)->i_current_depth
;
326 if (unlikely(max_depth
> MAX_DIR_HASH_DEPTH
)) {
327 f2fs_warn(F2FS_I_SB(dir
), "Corrupted max_depth of %lu: %u",
328 dir
->i_ino
, max_depth
);
329 max_depth
= MAX_DIR_HASH_DEPTH
;
330 f2fs_i_depth_write(dir
, max_depth
);
333 for (level
= 0; level
< max_depth
; level
++) {
335 de
= find_in_level(dir
, level
, fname
, res_page
);
336 if (de
|| IS_ERR(*res_page
))
340 /* This is to increase the speed of f2fs_create */
342 F2FS_I(dir
)->task
= current
;
347 * Find an entry in the specified directory with the wanted name.
348 * It returns the page where the entry was found (as a parameter - res_page),
349 * and the entry itself. Page is returned mapped and unlocked.
350 * Entry is guaranteed to be valid.
352 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
353 const struct qstr
*child
, struct page
**res_page
)
355 struct f2fs_dir_entry
*de
= NULL
;
356 struct fscrypt_name fname
;
359 #ifdef CONFIG_UNICODE
360 if (f2fs_has_strict_mode(F2FS_I_SB(dir
)) && IS_CASEFOLDED(dir
) &&
361 utf8_validate(F2FS_I_SB(dir
)->s_encoding
, child
)) {
362 *res_page
= ERR_PTR(-EINVAL
);
367 err
= fscrypt_setup_filename(dir
, child
, 1, &fname
);
372 *res_page
= ERR_PTR(err
);
376 de
= __f2fs_find_entry(dir
, &fname
, res_page
);
378 fscrypt_free_filename(&fname
);
382 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
384 struct qstr dotdot
= QSTR_INIT("..", 2);
386 return f2fs_find_entry(dir
, &dotdot
, p
);
389 ino_t
f2fs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
,
393 struct f2fs_dir_entry
*de
;
395 de
= f2fs_find_entry(dir
, qstr
, page
);
397 res
= le32_to_cpu(de
->ino
);
398 f2fs_put_page(*page
, 0);
404 void f2fs_set_link(struct inode
*dir
, struct f2fs_dir_entry
*de
,
405 struct page
*page
, struct inode
*inode
)
407 enum page_type type
= f2fs_has_inline_dentry(dir
) ? NODE
: DATA
;
409 f2fs_wait_on_page_writeback(page
, type
, true, true);
410 de
->ino
= cpu_to_le32(inode
->i_ino
);
411 set_de_type(de
, inode
->i_mode
);
412 set_page_dirty(page
);
414 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
415 f2fs_mark_inode_dirty_sync(dir
, false);
416 f2fs_put_page(page
, 1);
419 static void init_dent_inode(const struct qstr
*name
, struct page
*ipage
)
421 struct f2fs_inode
*ri
;
423 f2fs_wait_on_page_writeback(ipage
, NODE
, true, true);
425 /* copy name info. to this inode page */
426 ri
= F2FS_INODE(ipage
);
427 ri
->i_namelen
= cpu_to_le32(name
->len
);
428 memcpy(ri
->i_name
, name
->name
, name
->len
);
429 set_page_dirty(ipage
);
432 void f2fs_do_make_empty_dir(struct inode
*inode
, struct inode
*parent
,
433 struct f2fs_dentry_ptr
*d
)
435 struct qstr dot
= QSTR_INIT(".", 1);
436 struct qstr dotdot
= QSTR_INIT("..", 2);
438 /* update dirent of "." */
439 f2fs_update_dentry(inode
->i_ino
, inode
->i_mode
, d
, &dot
, 0, 0);
441 /* update dirent of ".." */
442 f2fs_update_dentry(parent
->i_ino
, parent
->i_mode
, d
, &dotdot
, 0, 1);
445 static int make_empty_dir(struct inode
*inode
,
446 struct inode
*parent
, struct page
*page
)
448 struct page
*dentry_page
;
449 struct f2fs_dentry_block
*dentry_blk
;
450 struct f2fs_dentry_ptr d
;
452 if (f2fs_has_inline_dentry(inode
))
453 return f2fs_make_empty_inline_dir(inode
, parent
, page
);
455 dentry_page
= f2fs_get_new_data_page(inode
, page
, 0, true);
456 if (IS_ERR(dentry_page
))
457 return PTR_ERR(dentry_page
);
459 dentry_blk
= page_address(dentry_page
);
461 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
462 f2fs_do_make_empty_dir(inode
, parent
, &d
);
464 set_page_dirty(dentry_page
);
465 f2fs_put_page(dentry_page
, 1);
469 struct page
*f2fs_init_inode_metadata(struct inode
*inode
, struct inode
*dir
,
470 const struct qstr
*new_name
, const struct qstr
*orig_name
,
474 int dummy_encrypt
= DUMMY_ENCRYPTION_ENABLED(F2FS_I_SB(dir
));
477 if (is_inode_flag_set(inode
, FI_NEW_INODE
)) {
478 page
= f2fs_new_inode_page(inode
);
482 if (S_ISDIR(inode
->i_mode
)) {
483 /* in order to handle error case */
485 err
= make_empty_dir(inode
, dir
, page
);
493 err
= f2fs_init_acl(inode
, dir
, page
, dpage
);
497 err
= f2fs_init_security(inode
, dir
, orig_name
, page
);
501 if ((IS_ENCRYPTED(dir
) || dummy_encrypt
) &&
502 f2fs_may_encrypt(inode
)) {
503 err
= fscrypt_inherit_context(dir
, inode
, page
, false);
508 page
= f2fs_get_node_page(F2FS_I_SB(dir
), inode
->i_ino
);
514 init_dent_inode(new_name
, page
);
515 if (IS_ENCRYPTED(dir
))
516 file_set_enc_name(inode
);
520 * This file should be checkpointed during fsync.
521 * We lost i_pino from now on.
523 if (is_inode_flag_set(inode
, FI_INC_LINK
)) {
524 if (!S_ISDIR(inode
->i_mode
))
525 file_lost_pino(inode
);
527 * If link the tmpfile to alias through linkat path,
528 * we should remove this inode from orphan list.
530 if (inode
->i_nlink
== 0)
531 f2fs_remove_orphan_inode(F2FS_I_SB(dir
), inode
->i_ino
);
532 f2fs_i_links_write(inode
, true);
538 f2fs_update_inode(inode
, page
);
539 f2fs_put_page(page
, 1);
543 void f2fs_update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
544 unsigned int current_depth
)
546 if (inode
&& is_inode_flag_set(inode
, FI_NEW_INODE
)) {
547 if (S_ISDIR(inode
->i_mode
))
548 f2fs_i_links_write(dir
, true);
549 clear_inode_flag(inode
, FI_NEW_INODE
);
551 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
552 f2fs_mark_inode_dirty_sync(dir
, false);
554 if (F2FS_I(dir
)->i_current_depth
!= current_depth
)
555 f2fs_i_depth_write(dir
, current_depth
);
557 if (inode
&& is_inode_flag_set(inode
, FI_INC_LINK
))
558 clear_inode_flag(inode
, FI_INC_LINK
);
561 int f2fs_room_for_filename(const void *bitmap
, int slots
, int max_slots
)
564 int zero_start
, zero_end
;
566 zero_start
= find_next_zero_bit_le(bitmap
, max_slots
, bit_start
);
567 if (zero_start
>= max_slots
)
570 zero_end
= find_next_bit_le(bitmap
, max_slots
, zero_start
);
571 if (zero_end
- zero_start
>= slots
)
574 bit_start
= zero_end
+ 1;
576 if (zero_end
+ 1 >= max_slots
)
581 bool f2fs_has_enough_room(struct inode
*dir
, struct page
*ipage
,
582 struct fscrypt_name
*fname
)
584 struct f2fs_dentry_ptr d
;
585 unsigned int bit_pos
;
586 int slots
= GET_DENTRY_SLOTS(fname_len(fname
));
588 make_dentry_ptr_inline(dir
, &d
, inline_data_addr(dir
, ipage
));
590 bit_pos
= f2fs_room_for_filename(d
.bitmap
, slots
, d
.max
);
592 return bit_pos
< d
.max
;
595 void f2fs_update_dentry(nid_t ino
, umode_t mode
, struct f2fs_dentry_ptr
*d
,
596 const struct qstr
*name
, f2fs_hash_t name_hash
,
597 unsigned int bit_pos
)
599 struct f2fs_dir_entry
*de
;
600 int slots
= GET_DENTRY_SLOTS(name
->len
);
603 de
= &d
->dentry
[bit_pos
];
604 de
->hash_code
= name_hash
;
605 de
->name_len
= cpu_to_le16(name
->len
);
606 memcpy(d
->filename
[bit_pos
], name
->name
, name
->len
);
607 de
->ino
= cpu_to_le32(ino
);
608 set_de_type(de
, mode
);
609 for (i
= 0; i
< slots
; i
++) {
610 __set_bit_le(bit_pos
+ i
, (void *)d
->bitmap
);
611 /* avoid wrong garbage data for readdir */
613 (de
+ i
)->name_len
= 0;
617 int f2fs_add_regular_entry(struct inode
*dir
, const struct qstr
*new_name
,
618 const struct qstr
*orig_name
,
619 struct inode
*inode
, nid_t ino
, umode_t mode
)
621 unsigned int bit_pos
;
623 unsigned int current_depth
;
624 unsigned long bidx
, block
;
625 f2fs_hash_t dentry_hash
;
626 unsigned int nbucket
, nblock
;
627 struct page
*dentry_page
= NULL
;
628 struct f2fs_dentry_block
*dentry_blk
= NULL
;
629 struct f2fs_dentry_ptr d
;
630 struct page
*page
= NULL
;
634 slots
= GET_DENTRY_SLOTS(new_name
->len
);
635 dentry_hash
= f2fs_dentry_hash(dir
, new_name
, NULL
);
637 current_depth
= F2FS_I(dir
)->i_current_depth
;
638 if (F2FS_I(dir
)->chash
== dentry_hash
) {
639 level
= F2FS_I(dir
)->clevel
;
640 F2FS_I(dir
)->chash
= 0;
644 if (time_to_inject(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
)) {
645 f2fs_show_injection_info(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
);
649 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
652 /* Increase the depth, if required */
653 if (level
== current_depth
)
656 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
657 nblock
= bucket_blocks(level
);
659 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
660 (le32_to_cpu(dentry_hash
) % nbucket
));
662 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
663 dentry_page
= f2fs_get_new_data_page(dir
, NULL
, block
, true);
664 if (IS_ERR(dentry_page
))
665 return PTR_ERR(dentry_page
);
667 dentry_blk
= page_address(dentry_page
);
668 bit_pos
= f2fs_room_for_filename(&dentry_blk
->dentry_bitmap
,
669 slots
, NR_DENTRY_IN_BLOCK
);
670 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
673 f2fs_put_page(dentry_page
, 1);
676 /* Move to next level to find the empty slot for new dentry */
680 f2fs_wait_on_page_writeback(dentry_page
, DATA
, true, true);
683 down_write(&F2FS_I(inode
)->i_sem
);
684 page
= f2fs_init_inode_metadata(inode
, dir
, new_name
,
692 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
693 f2fs_update_dentry(ino
, mode
, &d
, new_name
, dentry_hash
, bit_pos
);
695 set_page_dirty(dentry_page
);
698 f2fs_i_pino_write(inode
, dir
->i_ino
);
700 /* synchronize inode page's data from inode cache */
701 if (is_inode_flag_set(inode
, FI_NEW_INODE
))
702 f2fs_update_inode(inode
, page
);
704 f2fs_put_page(page
, 1);
707 f2fs_update_parent_metadata(dir
, inode
, current_depth
);
710 up_write(&F2FS_I(inode
)->i_sem
);
712 f2fs_put_page(dentry_page
, 1);
717 int f2fs_add_dentry(struct inode
*dir
, struct fscrypt_name
*fname
,
718 struct inode
*inode
, nid_t ino
, umode_t mode
)
720 struct qstr new_name
;
723 new_name
.name
= fname_name(fname
);
724 new_name
.len
= fname_len(fname
);
726 if (f2fs_has_inline_dentry(dir
))
727 err
= f2fs_add_inline_entry(dir
, &new_name
, fname
->usr_fname
,
730 err
= f2fs_add_regular_entry(dir
, &new_name
, fname
->usr_fname
,
733 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
738 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
741 int f2fs_do_add_link(struct inode
*dir
, const struct qstr
*name
,
742 struct inode
*inode
, nid_t ino
, umode_t mode
)
744 struct fscrypt_name fname
;
745 struct page
*page
= NULL
;
746 struct f2fs_dir_entry
*de
= NULL
;
749 err
= fscrypt_setup_filename(dir
, name
, 0, &fname
);
754 * An immature stakable filesystem shows a race condition between lookup
755 * and create. If we have same task when doing lookup and create, it's
756 * definitely fine as expected by VFS normally. Otherwise, let's just
757 * verify on-disk dentry one more time, which guarantees filesystem
760 if (current
!= F2FS_I(dir
)->task
) {
761 de
= __f2fs_find_entry(dir
, &fname
, &page
);
762 F2FS_I(dir
)->task
= NULL
;
765 f2fs_put_page(page
, 0);
767 } else if (IS_ERR(page
)) {
770 err
= f2fs_add_dentry(dir
, &fname
, inode
, ino
, mode
);
772 fscrypt_free_filename(&fname
);
776 int f2fs_do_tmpfile(struct inode
*inode
, struct inode
*dir
)
781 down_write(&F2FS_I(inode
)->i_sem
);
782 page
= f2fs_init_inode_metadata(inode
, dir
, NULL
, NULL
, NULL
);
787 f2fs_put_page(page
, 1);
789 clear_inode_flag(inode
, FI_NEW_INODE
);
790 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
792 up_write(&F2FS_I(inode
)->i_sem
);
796 void f2fs_drop_nlink(struct inode
*dir
, struct inode
*inode
)
798 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
800 down_write(&F2FS_I(inode
)->i_sem
);
802 if (S_ISDIR(inode
->i_mode
))
803 f2fs_i_links_write(dir
, false);
804 inode
->i_ctime
= current_time(inode
);
806 f2fs_i_links_write(inode
, false);
807 if (S_ISDIR(inode
->i_mode
)) {
808 f2fs_i_links_write(inode
, false);
809 f2fs_i_size_write(inode
, 0);
811 up_write(&F2FS_I(inode
)->i_sem
);
813 if (inode
->i_nlink
== 0)
814 f2fs_add_orphan_inode(inode
);
816 f2fs_release_orphan_inode(sbi
);
820 * It only removes the dentry from the dentry page, corresponding name
821 * entry in name page does not need to be touched during deletion.
823 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
824 struct inode
*dir
, struct inode
*inode
)
826 struct f2fs_dentry_block
*dentry_blk
;
827 unsigned int bit_pos
;
828 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
831 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
833 if (F2FS_OPTION(F2FS_I_SB(dir
)).fsync_mode
== FSYNC_MODE_STRICT
)
834 f2fs_add_ino_entry(F2FS_I_SB(dir
), dir
->i_ino
, TRANS_DIR_INO
);
836 if (f2fs_has_inline_dentry(dir
))
837 return f2fs_delete_inline_entry(dentry
, page
, dir
, inode
);
840 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
842 dentry_blk
= page_address(page
);
843 bit_pos
= dentry
- dentry_blk
->dentry
;
844 for (i
= 0; i
< slots
; i
++)
845 __clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
847 /* Let's check and deallocate this dentry page */
848 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
851 set_page_dirty(page
);
853 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
854 f2fs_mark_inode_dirty_sync(dir
, false);
857 f2fs_drop_nlink(dir
, inode
);
859 if (bit_pos
== NR_DENTRY_IN_BLOCK
&&
860 !f2fs_truncate_hole(dir
, page
->index
, page
->index
+ 1)) {
861 f2fs_clear_page_cache_dirty_tag(page
);
862 clear_page_dirty_for_io(page
);
863 f2fs_clear_page_private(page
);
864 ClearPageUptodate(page
);
865 clear_cold_data(page
);
866 inode_dec_dirty_pages(dir
);
867 f2fs_remove_dirty_inode(dir
);
869 f2fs_put_page(page
, 1);
872 bool f2fs_empty_dir(struct inode
*dir
)
875 struct page
*dentry_page
;
876 unsigned int bit_pos
;
877 struct f2fs_dentry_block
*dentry_blk
;
878 unsigned long nblock
= dir_blocks(dir
);
880 if (f2fs_has_inline_dentry(dir
))
881 return f2fs_empty_inline_dir(dir
);
883 for (bidx
= 0; bidx
< nblock
; bidx
++) {
884 dentry_page
= f2fs_get_lock_data_page(dir
, bidx
, false);
885 if (IS_ERR(dentry_page
)) {
886 if (PTR_ERR(dentry_page
) == -ENOENT
)
892 dentry_blk
= page_address(dentry_page
);
897 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
901 f2fs_put_page(dentry_page
, 1);
903 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
909 int f2fs_fill_dentries(struct dir_context
*ctx
, struct f2fs_dentry_ptr
*d
,
910 unsigned int start_pos
, struct fscrypt_str
*fstr
)
912 unsigned char d_type
= DT_UNKNOWN
;
913 unsigned int bit_pos
;
914 struct f2fs_dir_entry
*de
= NULL
;
915 struct fscrypt_str de_name
= FSTR_INIT(NULL
, 0);
916 struct f2fs_sb_info
*sbi
= F2FS_I_SB(d
->inode
);
917 struct blk_plug plug
;
918 bool readdir_ra
= sbi
->readdir_ra
== 1;
921 bit_pos
= ((unsigned long)ctx
->pos
% d
->max
);
924 blk_start_plug(&plug
);
926 while (bit_pos
< d
->max
) {
927 bit_pos
= find_next_bit_le(d
->bitmap
, d
->max
, bit_pos
);
928 if (bit_pos
>= d
->max
)
931 de
= &d
->dentry
[bit_pos
];
932 if (de
->name_len
== 0) {
934 ctx
->pos
= start_pos
+ bit_pos
;
936 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
937 KERN_WARNING
, sbi
->sb
->s_id
,
938 le32_to_cpu(de
->ino
));
939 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
943 d_type
= f2fs_get_de_type(de
);
945 de_name
.name
= d
->filename
[bit_pos
];
946 de_name
.len
= le16_to_cpu(de
->name_len
);
948 /* check memory boundary before moving forward */
949 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
950 if (unlikely(bit_pos
> d
->max
||
951 le16_to_cpu(de
->name_len
) > F2FS_NAME_LEN
)) {
952 f2fs_warn(sbi
, "%s: corrupted namelen=%d, run fsck to fix.",
953 __func__
, le16_to_cpu(de
->name_len
));
954 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
959 if (IS_ENCRYPTED(d
->inode
)) {
960 int save_len
= fstr
->len
;
962 err
= fscrypt_fname_disk_to_usr(d
->inode
,
963 (u32
)le32_to_cpu(de
->hash_code
),
969 fstr
->len
= save_len
;
972 if (!dir_emit(ctx
, de_name
.name
, de_name
.len
,
973 le32_to_cpu(de
->ino
), d_type
)) {
979 f2fs_ra_node_page(sbi
, le32_to_cpu(de
->ino
));
981 ctx
->pos
= start_pos
+ bit_pos
;
985 blk_finish_plug(&plug
);
989 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
991 struct inode
*inode
= file_inode(file
);
992 unsigned long npages
= dir_blocks(inode
);
993 struct f2fs_dentry_block
*dentry_blk
= NULL
;
994 struct page
*dentry_page
= NULL
;
995 struct file_ra_state
*ra
= &file
->f_ra
;
996 loff_t start_pos
= ctx
->pos
;
997 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
998 struct f2fs_dentry_ptr d
;
999 struct fscrypt_str fstr
= FSTR_INIT(NULL
, 0);
1002 if (IS_ENCRYPTED(inode
)) {
1003 err
= fscrypt_get_encryption_info(inode
);
1007 err
= fscrypt_fname_alloc_buffer(inode
, F2FS_NAME_LEN
, &fstr
);
1012 if (f2fs_has_inline_dentry(inode
)) {
1013 err
= f2fs_read_inline_dir(file
, ctx
, &fstr
);
1017 for (; n
< npages
; n
++, ctx
->pos
= n
* NR_DENTRY_IN_BLOCK
) {
1019 /* allow readdir() to be interrupted */
1020 if (fatal_signal_pending(current
)) {
1026 /* readahead for multi pages of dir */
1027 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
1028 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
1029 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
1031 dentry_page
= f2fs_find_data_page(inode
, n
);
1032 if (IS_ERR(dentry_page
)) {
1033 err
= PTR_ERR(dentry_page
);
1034 if (err
== -ENOENT
) {
1042 dentry_blk
= page_address(dentry_page
);
1044 make_dentry_ptr_block(inode
, &d
, dentry_blk
);
1046 err
= f2fs_fill_dentries(ctx
, &d
,
1047 n
* NR_DENTRY_IN_BLOCK
, &fstr
);
1049 f2fs_put_page(dentry_page
, 0);
1053 f2fs_put_page(dentry_page
, 0);
1056 fscrypt_fname_free_buffer(&fstr
);
1058 trace_f2fs_readdir(inode
, start_pos
, ctx
->pos
, err
);
1059 return err
< 0 ? err
: 0;
1062 static int f2fs_dir_open(struct inode
*inode
, struct file
*filp
)
1064 if (IS_ENCRYPTED(inode
))
1065 return fscrypt_get_encryption_info(inode
) ? -EACCES
: 0;
1069 const struct file_operations f2fs_dir_operations
= {
1070 .llseek
= generic_file_llseek
,
1071 .read
= generic_read_dir
,
1072 .iterate_shared
= f2fs_readdir
,
1073 .fsync
= f2fs_sync_file
,
1074 .open
= f2fs_dir_open
,
1075 .unlocked_ioctl
= f2fs_ioctl
,
1076 #ifdef CONFIG_COMPAT
1077 .compat_ioctl
= f2fs_compat_ioctl
,
1081 #ifdef CONFIG_UNICODE
1082 static int f2fs_d_compare(const struct dentry
*dentry
, unsigned int len
,
1083 const char *str
, const struct qstr
*name
)
1085 struct qstr qstr
= {.name
= str
, .len
= len
};
1086 const struct dentry
*parent
= READ_ONCE(dentry
->d_parent
);
1087 const struct inode
*inode
= READ_ONCE(parent
->d_inode
);
1089 if (!inode
|| !IS_CASEFOLDED(inode
)) {
1090 if (len
!= name
->len
)
1092 return memcmp(str
, name
->name
, len
);
1095 return f2fs_ci_compare(inode
, name
, &qstr
, false);
1098 static int f2fs_d_hash(const struct dentry
*dentry
, struct qstr
*str
)
1100 struct f2fs_sb_info
*sbi
= F2FS_SB(dentry
->d_sb
);
1101 const struct unicode_map
*um
= sbi
->s_encoding
;
1102 const struct inode
*inode
= READ_ONCE(dentry
->d_inode
);
1103 unsigned char *norm
;
1106 if (!inode
|| !IS_CASEFOLDED(inode
))
1109 norm
= f2fs_kmalloc(sbi
, PATH_MAX
, GFP_ATOMIC
);
1113 len
= utf8_casefold(um
, str
, norm
, PATH_MAX
);
1115 if (f2fs_has_strict_mode(sbi
))
1119 str
->hash
= full_name_hash(dentry
, norm
, len
);
1125 const struct dentry_operations f2fs_dentry_ops
= {
1126 .d_hash
= f2fs_d_hash
,
1127 .d_compare
= f2fs_d_compare
,