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 static unsigned long dir_blocks(struct inode
*inode
)
21 return ((unsigned long long) (i_size_read(inode
) + PAGE_SIZE
- 1))
25 static unsigned int dir_buckets(unsigned int level
, int dir_level
)
27 if (level
+ dir_level
< MAX_DIR_HASH_DEPTH
/ 2)
28 return 1 << (level
+ dir_level
);
30 return MAX_DIR_BUCKETS
;
33 static unsigned int bucket_blocks(unsigned int level
)
35 if (level
< MAX_DIR_HASH_DEPTH
/ 2)
41 static unsigned char f2fs_filetype_table
[F2FS_FT_MAX
] = {
42 [F2FS_FT_UNKNOWN
] = DT_UNKNOWN
,
43 [F2FS_FT_REG_FILE
] = DT_REG
,
44 [F2FS_FT_DIR
] = DT_DIR
,
45 [F2FS_FT_CHRDEV
] = DT_CHR
,
46 [F2FS_FT_BLKDEV
] = DT_BLK
,
47 [F2FS_FT_FIFO
] = DT_FIFO
,
48 [F2FS_FT_SOCK
] = DT_SOCK
,
49 [F2FS_FT_SYMLINK
] = DT_LNK
,
52 static unsigned char f2fs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
53 [S_IFREG
>> S_SHIFT
] = F2FS_FT_REG_FILE
,
54 [S_IFDIR
>> S_SHIFT
] = F2FS_FT_DIR
,
55 [S_IFCHR
>> S_SHIFT
] = F2FS_FT_CHRDEV
,
56 [S_IFBLK
>> S_SHIFT
] = F2FS_FT_BLKDEV
,
57 [S_IFIFO
>> S_SHIFT
] = F2FS_FT_FIFO
,
58 [S_IFSOCK
>> S_SHIFT
] = F2FS_FT_SOCK
,
59 [S_IFLNK
>> S_SHIFT
] = F2FS_FT_SYMLINK
,
62 static void set_de_type(struct f2fs_dir_entry
*de
, umode_t mode
)
64 de
->file_type
= f2fs_type_by_mode
[(mode
& S_IFMT
) >> S_SHIFT
];
67 unsigned char f2fs_get_de_type(struct f2fs_dir_entry
*de
)
69 if (de
->file_type
< F2FS_FT_MAX
)
70 return f2fs_filetype_table
[de
->file_type
];
74 /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
75 int f2fs_init_casefolded_name(const struct inode
*dir
,
76 struct f2fs_filename
*fname
)
79 struct super_block
*sb
= dir
->i_sb
;
80 struct f2fs_sb_info
*sbi
= F2FS_SB(sb
);
82 if (IS_CASEFOLDED(dir
)) {
83 fname
->cf_name
.name
= f2fs_kmalloc(sbi
, F2FS_NAME_LEN
,
85 if (!fname
->cf_name
.name
)
87 fname
->cf_name
.len
= utf8_casefold(sb
->s_encoding
,
91 if ((int)fname
->cf_name
.len
<= 0) {
92 kfree(fname
->cf_name
.name
);
93 fname
->cf_name
.name
= NULL
;
94 if (sb_has_strict_encoding(sb
))
96 /* fall back to treating name as opaque byte sequence */
103 static int __f2fs_setup_filename(const struct inode
*dir
,
104 const struct fscrypt_name
*crypt_name
,
105 struct f2fs_filename
*fname
)
109 memset(fname
, 0, sizeof(*fname
));
111 fname
->usr_fname
= crypt_name
->usr_fname
;
112 fname
->disk_name
= crypt_name
->disk_name
;
113 #ifdef CONFIG_FS_ENCRYPTION
114 fname
->crypto_buf
= crypt_name
->crypto_buf
;
116 if (crypt_name
->is_nokey_name
) {
117 /* hash was decoded from the no-key name */
118 fname
->hash
= cpu_to_le32(crypt_name
->hash
);
120 err
= f2fs_init_casefolded_name(dir
, fname
);
122 f2fs_free_filename(fname
);
125 f2fs_hash_filename(dir
, fname
);
131 * Prepare to search for @iname in @dir. This is similar to
132 * fscrypt_setup_filename(), but this also handles computing the casefolded name
133 * and the f2fs dirhash if needed, then packing all the information about this
134 * filename up into a 'struct f2fs_filename'.
136 int f2fs_setup_filename(struct inode
*dir
, const struct qstr
*iname
,
137 int lookup
, struct f2fs_filename
*fname
)
139 struct fscrypt_name crypt_name
;
142 err
= fscrypt_setup_filename(dir
, iname
, lookup
, &crypt_name
);
146 return __f2fs_setup_filename(dir
, &crypt_name
, fname
);
150 * Prepare to look up @dentry in @dir. This is similar to
151 * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
152 * and the f2fs dirhash if needed, then packing all the information about this
153 * filename up into a 'struct f2fs_filename'.
155 int f2fs_prepare_lookup(struct inode
*dir
, struct dentry
*dentry
,
156 struct f2fs_filename
*fname
)
158 struct fscrypt_name crypt_name
;
161 err
= fscrypt_prepare_lookup(dir
, dentry
, &crypt_name
);
165 return __f2fs_setup_filename(dir
, &crypt_name
, fname
);
168 void f2fs_free_filename(struct f2fs_filename
*fname
)
170 #ifdef CONFIG_FS_ENCRYPTION
171 kfree(fname
->crypto_buf
.name
);
172 fname
->crypto_buf
.name
= NULL
;
174 #ifdef CONFIG_UNICODE
175 kfree(fname
->cf_name
.name
);
176 fname
->cf_name
.name
= NULL
;
180 static unsigned long dir_block_index(unsigned int level
,
181 int dir_level
, unsigned int idx
)
184 unsigned long bidx
= 0;
186 for (i
= 0; i
< level
; i
++)
187 bidx
+= dir_buckets(i
, dir_level
) * bucket_blocks(i
);
188 bidx
+= idx
* bucket_blocks(level
);
192 static struct f2fs_dir_entry
*find_in_block(struct inode
*dir
,
193 struct page
*dentry_page
,
194 const struct f2fs_filename
*fname
,
197 struct f2fs_dentry_block
*dentry_blk
;
198 struct f2fs_dentry_ptr d
;
200 dentry_blk
= (struct f2fs_dentry_block
*)page_address(dentry_page
);
202 make_dentry_ptr_block(dir
, &d
, dentry_blk
);
203 return f2fs_find_target_dentry(&d
, fname
, max_slots
);
206 #ifdef CONFIG_UNICODE
208 * Test whether a case-insensitive directory entry matches the filename
209 * being searched for.
211 * Returns 1 for a match, 0 for no match, and -errno on an error.
213 static int f2fs_match_ci_name(const struct inode
*dir
, const struct qstr
*name
,
214 const u8
*de_name
, u32 de_name_len
)
216 const struct super_block
*sb
= dir
->i_sb
;
217 const struct unicode_map
*um
= sb
->s_encoding
;
218 struct fscrypt_str decrypted_name
= FSTR_INIT(NULL
, de_name_len
);
219 struct qstr entry
= QSTR_INIT(de_name
, de_name_len
);
222 if (IS_ENCRYPTED(dir
)) {
223 const struct fscrypt_str encrypted_name
=
224 FSTR_INIT((u8
*)de_name
, de_name_len
);
226 if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir
)))
229 decrypted_name
.name
= kmalloc(de_name_len
, GFP_KERNEL
);
230 if (!decrypted_name
.name
)
232 res
= fscrypt_fname_disk_to_usr(dir
, 0, 0, &encrypted_name
,
236 entry
.name
= decrypted_name
.name
;
237 entry
.len
= decrypted_name
.len
;
240 res
= utf8_strncasecmp_folded(um
, name
, &entry
);
242 * In strict mode, ignore invalid names. In non-strict mode,
243 * fall back to treating them as opaque byte sequences.
245 if (res
< 0 && !sb_has_strict_encoding(sb
)) {
246 res
= name
->len
== entry
.len
&&
247 memcmp(name
->name
, entry
.name
, name
->len
) == 0;
249 /* utf8_strncasecmp_folded returns 0 on match */
253 kfree(decrypted_name
.name
);
256 #endif /* CONFIG_UNICODE */
258 static inline int f2fs_match_name(const struct inode
*dir
,
259 const struct f2fs_filename
*fname
,
260 const u8
*de_name
, u32 de_name_len
)
262 struct fscrypt_name f
;
264 #ifdef CONFIG_UNICODE
265 if (fname
->cf_name
.name
) {
266 struct qstr cf
= FSTR_TO_QSTR(&fname
->cf_name
);
268 return f2fs_match_ci_name(dir
, &cf
, de_name
, de_name_len
);
271 f
.usr_fname
= fname
->usr_fname
;
272 f
.disk_name
= fname
->disk_name
;
273 #ifdef CONFIG_FS_ENCRYPTION
274 f
.crypto_buf
= fname
->crypto_buf
;
276 return fscrypt_match_name(&f
, de_name
, de_name_len
);
279 struct f2fs_dir_entry
*f2fs_find_target_dentry(const struct f2fs_dentry_ptr
*d
,
280 const struct f2fs_filename
*fname
, int *max_slots
)
282 struct f2fs_dir_entry
*de
;
283 unsigned long bit_pos
= 0;
289 while (bit_pos
< d
->max
) {
290 if (!test_bit_le(bit_pos
, d
->bitmap
)) {
296 de
= &d
->dentry
[bit_pos
];
298 if (unlikely(!de
->name_len
)) {
303 if (de
->hash_code
== fname
->hash
) {
304 res
= f2fs_match_name(d
->inode
, fname
,
305 d
->filename
[bit_pos
],
306 le16_to_cpu(de
->name_len
));
313 if (max_slots
&& max_len
> *max_slots
)
314 *max_slots
= max_len
;
317 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
322 if (max_slots
&& max_len
> *max_slots
)
323 *max_slots
= max_len
;
327 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
329 const struct f2fs_filename
*fname
,
330 struct page
**res_page
)
332 int s
= GET_DENTRY_SLOTS(fname
->disk_name
.len
);
333 unsigned int nbucket
, nblock
;
334 unsigned int bidx
, end_block
;
335 struct page
*dentry_page
;
336 struct f2fs_dir_entry
*de
= NULL
;
340 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
341 nblock
= bucket_blocks(level
);
343 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
344 le32_to_cpu(fname
->hash
) % nbucket
);
345 end_block
= bidx
+ nblock
;
347 for (; bidx
< end_block
; bidx
++) {
348 /* no need to allocate new dentry pages to all the indices */
349 dentry_page
= f2fs_find_data_page(dir
, bidx
);
350 if (IS_ERR(dentry_page
)) {
351 if (PTR_ERR(dentry_page
) == -ENOENT
) {
355 *res_page
= dentry_page
;
360 de
= find_in_block(dir
, dentry_page
, fname
, &max_slots
);
362 *res_page
= ERR_CAST(de
);
366 *res_page
= dentry_page
;
372 f2fs_put_page(dentry_page
, 0);
375 if (!de
&& room
&& F2FS_I(dir
)->chash
!= fname
->hash
) {
376 F2FS_I(dir
)->chash
= fname
->hash
;
377 F2FS_I(dir
)->clevel
= level
;
383 struct f2fs_dir_entry
*__f2fs_find_entry(struct inode
*dir
,
384 const struct f2fs_filename
*fname
,
385 struct page
**res_page
)
387 unsigned long npages
= dir_blocks(dir
);
388 struct f2fs_dir_entry
*de
= NULL
;
389 unsigned int max_depth
;
394 if (f2fs_has_inline_dentry(dir
)) {
395 de
= f2fs_find_in_inline_dir(dir
, fname
, res_page
);
402 max_depth
= F2FS_I(dir
)->i_current_depth
;
403 if (unlikely(max_depth
> MAX_DIR_HASH_DEPTH
)) {
404 f2fs_warn(F2FS_I_SB(dir
), "Corrupted max_depth of %lu: %u",
405 dir
->i_ino
, max_depth
);
406 max_depth
= MAX_DIR_HASH_DEPTH
;
407 f2fs_i_depth_write(dir
, max_depth
);
410 for (level
= 0; level
< max_depth
; level
++) {
411 de
= find_in_level(dir
, level
, fname
, res_page
);
412 if (de
|| IS_ERR(*res_page
))
416 /* This is to increase the speed of f2fs_create */
418 F2FS_I(dir
)->task
= current
;
423 * Find an entry in the specified directory with the wanted name.
424 * It returns the page where the entry was found (as a parameter - res_page),
425 * and the entry itself. Page is returned mapped and unlocked.
426 * Entry is guaranteed to be valid.
428 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
429 const struct qstr
*child
, struct page
**res_page
)
431 struct f2fs_dir_entry
*de
= NULL
;
432 struct f2fs_filename fname
;
435 err
= f2fs_setup_filename(dir
, child
, 1, &fname
);
440 *res_page
= ERR_PTR(err
);
444 de
= __f2fs_find_entry(dir
, &fname
, res_page
);
446 f2fs_free_filename(&fname
);
450 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
452 struct qstr dotdot
= QSTR_INIT("..", 2);
454 return f2fs_find_entry(dir
, &dotdot
, p
);
457 ino_t
f2fs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
,
461 struct f2fs_dir_entry
*de
;
463 de
= f2fs_find_entry(dir
, qstr
, page
);
465 res
= le32_to_cpu(de
->ino
);
466 f2fs_put_page(*page
, 0);
472 void f2fs_set_link(struct inode
*dir
, struct f2fs_dir_entry
*de
,
473 struct page
*page
, struct inode
*inode
)
475 enum page_type type
= f2fs_has_inline_dentry(dir
) ? NODE
: DATA
;
477 f2fs_wait_on_page_writeback(page
, type
, true, true);
478 de
->ino
= cpu_to_le32(inode
->i_ino
);
479 set_de_type(de
, inode
->i_mode
);
480 set_page_dirty(page
);
482 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
483 f2fs_mark_inode_dirty_sync(dir
, false);
484 f2fs_put_page(page
, 1);
487 static void init_dent_inode(struct inode
*dir
, struct inode
*inode
,
488 const struct f2fs_filename
*fname
,
491 struct f2fs_inode
*ri
;
493 if (!fname
) /* tmpfile case? */
496 f2fs_wait_on_page_writeback(ipage
, NODE
, true, true);
498 /* copy name info. to this inode page */
499 ri
= F2FS_INODE(ipage
);
500 ri
->i_namelen
= cpu_to_le32(fname
->disk_name
.len
);
501 memcpy(ri
->i_name
, fname
->disk_name
.name
, fname
->disk_name
.len
);
502 if (IS_ENCRYPTED(dir
)) {
503 file_set_enc_name(inode
);
505 * Roll-forward recovery doesn't have encryption keys available,
506 * so it can't compute the dirhash for encrypted+casefolded
507 * filenames. Append it to i_name if possible. Else, disable
508 * roll-forward recovery of the dentry (i.e., make fsync'ing the
509 * file force a checkpoint) by setting LOST_PINO.
511 if (IS_CASEFOLDED(dir
)) {
512 if (fname
->disk_name
.len
+ sizeof(f2fs_hash_t
) <=
514 put_unaligned(fname
->hash
, (f2fs_hash_t
*)
515 &ri
->i_name
[fname
->disk_name
.len
]);
517 file_lost_pino(inode
);
520 set_page_dirty(ipage
);
523 void f2fs_do_make_empty_dir(struct inode
*inode
, struct inode
*parent
,
524 struct f2fs_dentry_ptr
*d
)
526 struct fscrypt_str dot
= FSTR_INIT(".", 1);
527 struct fscrypt_str dotdot
= FSTR_INIT("..", 2);
529 /* update dirent of "." */
530 f2fs_update_dentry(inode
->i_ino
, inode
->i_mode
, d
, &dot
, 0, 0);
532 /* update dirent of ".." */
533 f2fs_update_dentry(parent
->i_ino
, parent
->i_mode
, d
, &dotdot
, 0, 1);
536 static int make_empty_dir(struct inode
*inode
,
537 struct inode
*parent
, struct page
*page
)
539 struct page
*dentry_page
;
540 struct f2fs_dentry_block
*dentry_blk
;
541 struct f2fs_dentry_ptr d
;
543 if (f2fs_has_inline_dentry(inode
))
544 return f2fs_make_empty_inline_dir(inode
, parent
, page
);
546 dentry_page
= f2fs_get_new_data_page(inode
, page
, 0, true);
547 if (IS_ERR(dentry_page
))
548 return PTR_ERR(dentry_page
);
550 dentry_blk
= page_address(dentry_page
);
552 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
553 f2fs_do_make_empty_dir(inode
, parent
, &d
);
555 set_page_dirty(dentry_page
);
556 f2fs_put_page(dentry_page
, 1);
560 struct page
*f2fs_init_inode_metadata(struct inode
*inode
, struct inode
*dir
,
561 const struct f2fs_filename
*fname
, struct page
*dpage
)
566 if (is_inode_flag_set(inode
, FI_NEW_INODE
)) {
567 page
= f2fs_new_inode_page(inode
);
571 if (S_ISDIR(inode
->i_mode
)) {
572 /* in order to handle error case */
574 err
= make_empty_dir(inode
, dir
, page
);
582 err
= f2fs_init_acl(inode
, dir
, page
, dpage
);
586 err
= f2fs_init_security(inode
, dir
,
587 fname
? fname
->usr_fname
: NULL
, page
);
591 if (IS_ENCRYPTED(inode
)) {
592 err
= fscrypt_set_context(inode
, page
);
597 page
= f2fs_get_node_page(F2FS_I_SB(dir
), inode
->i_ino
);
602 init_dent_inode(dir
, inode
, fname
, page
);
605 * This file should be checkpointed during fsync.
606 * We lost i_pino from now on.
608 if (is_inode_flag_set(inode
, FI_INC_LINK
)) {
609 if (!S_ISDIR(inode
->i_mode
))
610 file_lost_pino(inode
);
612 * If link the tmpfile to alias through linkat path,
613 * we should remove this inode from orphan list.
615 if (inode
->i_nlink
== 0)
616 f2fs_remove_orphan_inode(F2FS_I_SB(dir
), inode
->i_ino
);
617 f2fs_i_links_write(inode
, true);
623 f2fs_update_inode(inode
, page
);
624 f2fs_put_page(page
, 1);
628 void f2fs_update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
629 unsigned int current_depth
)
631 if (inode
&& is_inode_flag_set(inode
, FI_NEW_INODE
)) {
632 if (S_ISDIR(inode
->i_mode
))
633 f2fs_i_links_write(dir
, true);
634 clear_inode_flag(inode
, FI_NEW_INODE
);
636 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
637 f2fs_mark_inode_dirty_sync(dir
, false);
639 if (F2FS_I(dir
)->i_current_depth
!= current_depth
)
640 f2fs_i_depth_write(dir
, current_depth
);
642 if (inode
&& is_inode_flag_set(inode
, FI_INC_LINK
))
643 clear_inode_flag(inode
, FI_INC_LINK
);
646 int f2fs_room_for_filename(const void *bitmap
, int slots
, int max_slots
)
649 int zero_start
, zero_end
;
651 zero_start
= find_next_zero_bit_le(bitmap
, max_slots
, bit_start
);
652 if (zero_start
>= max_slots
)
655 zero_end
= find_next_bit_le(bitmap
, max_slots
, zero_start
);
656 if (zero_end
- zero_start
>= slots
)
659 bit_start
= zero_end
+ 1;
661 if (zero_end
+ 1 >= max_slots
)
666 bool f2fs_has_enough_room(struct inode
*dir
, struct page
*ipage
,
667 const struct f2fs_filename
*fname
)
669 struct f2fs_dentry_ptr d
;
670 unsigned int bit_pos
;
671 int slots
= GET_DENTRY_SLOTS(fname
->disk_name
.len
);
673 make_dentry_ptr_inline(dir
, &d
, inline_data_addr(dir
, ipage
));
675 bit_pos
= f2fs_room_for_filename(d
.bitmap
, slots
, d
.max
);
677 return bit_pos
< d
.max
;
680 void f2fs_update_dentry(nid_t ino
, umode_t mode
, struct f2fs_dentry_ptr
*d
,
681 const struct fscrypt_str
*name
, f2fs_hash_t name_hash
,
682 unsigned int bit_pos
)
684 struct f2fs_dir_entry
*de
;
685 int slots
= GET_DENTRY_SLOTS(name
->len
);
688 de
= &d
->dentry
[bit_pos
];
689 de
->hash_code
= name_hash
;
690 de
->name_len
= cpu_to_le16(name
->len
);
691 memcpy(d
->filename
[bit_pos
], name
->name
, name
->len
);
692 de
->ino
= cpu_to_le32(ino
);
693 set_de_type(de
, mode
);
694 for (i
= 0; i
< slots
; i
++) {
695 __set_bit_le(bit_pos
+ i
, (void *)d
->bitmap
);
696 /* avoid wrong garbage data for readdir */
698 (de
+ i
)->name_len
= 0;
702 int f2fs_add_regular_entry(struct inode
*dir
, const struct f2fs_filename
*fname
,
703 struct inode
*inode
, nid_t ino
, umode_t mode
)
705 unsigned int bit_pos
;
707 unsigned int current_depth
;
708 unsigned long bidx
, block
;
709 unsigned int nbucket
, nblock
;
710 struct page
*dentry_page
= NULL
;
711 struct f2fs_dentry_block
*dentry_blk
= NULL
;
712 struct f2fs_dentry_ptr d
;
713 struct page
*page
= NULL
;
717 slots
= GET_DENTRY_SLOTS(fname
->disk_name
.len
);
719 current_depth
= F2FS_I(dir
)->i_current_depth
;
720 if (F2FS_I(dir
)->chash
== fname
->hash
) {
721 level
= F2FS_I(dir
)->clevel
;
722 F2FS_I(dir
)->chash
= 0;
726 if (time_to_inject(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
)) {
727 f2fs_show_injection_info(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
);
731 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
734 /* Increase the depth, if required */
735 if (level
== current_depth
)
738 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
739 nblock
= bucket_blocks(level
);
741 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
742 (le32_to_cpu(fname
->hash
) % nbucket
));
744 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
745 dentry_page
= f2fs_get_new_data_page(dir
, NULL
, block
, true);
746 if (IS_ERR(dentry_page
))
747 return PTR_ERR(dentry_page
);
749 dentry_blk
= page_address(dentry_page
);
750 bit_pos
= f2fs_room_for_filename(&dentry_blk
->dentry_bitmap
,
751 slots
, NR_DENTRY_IN_BLOCK
);
752 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
755 f2fs_put_page(dentry_page
, 1);
758 /* Move to next level to find the empty slot for new dentry */
762 f2fs_wait_on_page_writeback(dentry_page
, DATA
, true, true);
765 down_write(&F2FS_I(inode
)->i_sem
);
766 page
= f2fs_init_inode_metadata(inode
, dir
, fname
, NULL
);
773 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
774 f2fs_update_dentry(ino
, mode
, &d
, &fname
->disk_name
, fname
->hash
,
777 set_page_dirty(dentry_page
);
780 f2fs_i_pino_write(inode
, dir
->i_ino
);
782 /* synchronize inode page's data from inode cache */
783 if (is_inode_flag_set(inode
, FI_NEW_INODE
))
784 f2fs_update_inode(inode
, page
);
786 f2fs_put_page(page
, 1);
789 f2fs_update_parent_metadata(dir
, inode
, current_depth
);
792 up_write(&F2FS_I(inode
)->i_sem
);
794 f2fs_put_page(dentry_page
, 1);
799 int f2fs_add_dentry(struct inode
*dir
, const struct f2fs_filename
*fname
,
800 struct inode
*inode
, nid_t ino
, umode_t mode
)
804 if (f2fs_has_inline_dentry(dir
))
805 err
= f2fs_add_inline_entry(dir
, fname
, inode
, ino
, mode
);
807 err
= f2fs_add_regular_entry(dir
, fname
, inode
, ino
, mode
);
809 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
814 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
817 int f2fs_do_add_link(struct inode
*dir
, const struct qstr
*name
,
818 struct inode
*inode
, nid_t ino
, umode_t mode
)
820 struct f2fs_filename fname
;
821 struct page
*page
= NULL
;
822 struct f2fs_dir_entry
*de
= NULL
;
825 err
= f2fs_setup_filename(dir
, name
, 0, &fname
);
830 * An immature stackable filesystem shows a race condition between lookup
831 * and create. If we have same task when doing lookup and create, it's
832 * definitely fine as expected by VFS normally. Otherwise, let's just
833 * verify on-disk dentry one more time, which guarantees filesystem
836 if (current
!= F2FS_I(dir
)->task
) {
837 de
= __f2fs_find_entry(dir
, &fname
, &page
);
838 F2FS_I(dir
)->task
= NULL
;
841 f2fs_put_page(page
, 0);
843 } else if (IS_ERR(page
)) {
846 err
= f2fs_add_dentry(dir
, &fname
, inode
, ino
, mode
);
848 f2fs_free_filename(&fname
);
852 int f2fs_do_tmpfile(struct inode
*inode
, struct inode
*dir
)
857 down_write(&F2FS_I(inode
)->i_sem
);
858 page
= f2fs_init_inode_metadata(inode
, dir
, NULL
, NULL
);
863 f2fs_put_page(page
, 1);
865 clear_inode_flag(inode
, FI_NEW_INODE
);
866 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
868 up_write(&F2FS_I(inode
)->i_sem
);
872 void f2fs_drop_nlink(struct inode
*dir
, struct inode
*inode
)
874 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
876 down_write(&F2FS_I(inode
)->i_sem
);
878 if (S_ISDIR(inode
->i_mode
))
879 f2fs_i_links_write(dir
, false);
880 inode
->i_ctime
= current_time(inode
);
882 f2fs_i_links_write(inode
, false);
883 if (S_ISDIR(inode
->i_mode
)) {
884 f2fs_i_links_write(inode
, false);
885 f2fs_i_size_write(inode
, 0);
887 up_write(&F2FS_I(inode
)->i_sem
);
889 if (inode
->i_nlink
== 0)
890 f2fs_add_orphan_inode(inode
);
892 f2fs_release_orphan_inode(sbi
);
896 * It only removes the dentry from the dentry page, corresponding name
897 * entry in name page does not need to be touched during deletion.
899 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
900 struct inode
*dir
, struct inode
*inode
)
902 struct f2fs_dentry_block
*dentry_blk
;
903 unsigned int bit_pos
;
904 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
907 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
909 if (F2FS_OPTION(F2FS_I_SB(dir
)).fsync_mode
== FSYNC_MODE_STRICT
)
910 f2fs_add_ino_entry(F2FS_I_SB(dir
), dir
->i_ino
, TRANS_DIR_INO
);
912 if (f2fs_has_inline_dentry(dir
))
913 return f2fs_delete_inline_entry(dentry
, page
, dir
, inode
);
916 f2fs_wait_on_page_writeback(page
, DATA
, true, true);
918 dentry_blk
= page_address(page
);
919 bit_pos
= dentry
- dentry_blk
->dentry
;
920 for (i
= 0; i
< slots
; i
++)
921 __clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
923 /* Let's check and deallocate this dentry page */
924 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
927 set_page_dirty(page
);
929 if (bit_pos
== NR_DENTRY_IN_BLOCK
&&
930 !f2fs_truncate_hole(dir
, page
->index
, page
->index
+ 1)) {
931 f2fs_clear_page_cache_dirty_tag(page
);
932 clear_page_dirty_for_io(page
);
933 f2fs_clear_page_private(page
);
934 ClearPageUptodate(page
);
935 clear_cold_data(page
);
936 inode_dec_dirty_pages(dir
);
937 f2fs_remove_dirty_inode(dir
);
939 f2fs_put_page(page
, 1);
941 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
942 f2fs_mark_inode_dirty_sync(dir
, false);
945 f2fs_drop_nlink(dir
, inode
);
948 bool f2fs_empty_dir(struct inode
*dir
)
951 struct page
*dentry_page
;
952 unsigned int bit_pos
;
953 struct f2fs_dentry_block
*dentry_blk
;
954 unsigned long nblock
= dir_blocks(dir
);
956 if (f2fs_has_inline_dentry(dir
))
957 return f2fs_empty_inline_dir(dir
);
959 for (bidx
= 0; bidx
< nblock
; bidx
++) {
960 dentry_page
= f2fs_get_lock_data_page(dir
, bidx
, false);
961 if (IS_ERR(dentry_page
)) {
962 if (PTR_ERR(dentry_page
) == -ENOENT
)
968 dentry_blk
= page_address(dentry_page
);
973 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
977 f2fs_put_page(dentry_page
, 1);
979 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
985 int f2fs_fill_dentries(struct dir_context
*ctx
, struct f2fs_dentry_ptr
*d
,
986 unsigned int start_pos
, struct fscrypt_str
*fstr
)
988 unsigned char d_type
= DT_UNKNOWN
;
989 unsigned int bit_pos
;
990 struct f2fs_dir_entry
*de
= NULL
;
991 struct fscrypt_str de_name
= FSTR_INIT(NULL
, 0);
992 struct f2fs_sb_info
*sbi
= F2FS_I_SB(d
->inode
);
993 struct blk_plug plug
;
994 bool readdir_ra
= sbi
->readdir_ra
== 1;
997 bit_pos
= ((unsigned long)ctx
->pos
% d
->max
);
1000 blk_start_plug(&plug
);
1002 while (bit_pos
< d
->max
) {
1003 bit_pos
= find_next_bit_le(d
->bitmap
, d
->max
, bit_pos
);
1004 if (bit_pos
>= d
->max
)
1007 de
= &d
->dentry
[bit_pos
];
1008 if (de
->name_len
== 0) {
1010 ctx
->pos
= start_pos
+ bit_pos
;
1012 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
1013 KERN_WARNING
, sbi
->sb
->s_id
,
1014 le32_to_cpu(de
->ino
));
1015 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
1019 d_type
= f2fs_get_de_type(de
);
1021 de_name
.name
= d
->filename
[bit_pos
];
1022 de_name
.len
= le16_to_cpu(de
->name_len
);
1024 /* check memory boundary before moving forward */
1025 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
1026 if (unlikely(bit_pos
> d
->max
||
1027 le16_to_cpu(de
->name_len
) > F2FS_NAME_LEN
)) {
1028 f2fs_warn(sbi
, "%s: corrupted namelen=%d, run fsck to fix.",
1029 __func__
, le16_to_cpu(de
->name_len
));
1030 set_sbi_flag(sbi
, SBI_NEED_FSCK
);
1031 err
= -EFSCORRUPTED
;
1035 if (IS_ENCRYPTED(d
->inode
)) {
1036 int save_len
= fstr
->len
;
1038 err
= fscrypt_fname_disk_to_usr(d
->inode
,
1039 (u32
)le32_to_cpu(de
->hash_code
),
1045 fstr
->len
= save_len
;
1048 if (!dir_emit(ctx
, de_name
.name
, de_name
.len
,
1049 le32_to_cpu(de
->ino
), d_type
)) {
1055 f2fs_ra_node_page(sbi
, le32_to_cpu(de
->ino
));
1057 ctx
->pos
= start_pos
+ bit_pos
;
1061 blk_finish_plug(&plug
);
1065 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
1067 struct inode
*inode
= file_inode(file
);
1068 unsigned long npages
= dir_blocks(inode
);
1069 struct f2fs_dentry_block
*dentry_blk
= NULL
;
1070 struct page
*dentry_page
= NULL
;
1071 struct file_ra_state
*ra
= &file
->f_ra
;
1072 loff_t start_pos
= ctx
->pos
;
1073 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
1074 struct f2fs_dentry_ptr d
;
1075 struct fscrypt_str fstr
= FSTR_INIT(NULL
, 0);
1078 if (IS_ENCRYPTED(inode
)) {
1079 err
= fscrypt_prepare_readdir(inode
);
1083 err
= fscrypt_fname_alloc_buffer(F2FS_NAME_LEN
, &fstr
);
1088 if (f2fs_has_inline_dentry(inode
)) {
1089 err
= f2fs_read_inline_dir(file
, ctx
, &fstr
);
1093 for (; n
< npages
; n
++, ctx
->pos
= n
* NR_DENTRY_IN_BLOCK
) {
1095 /* allow readdir() to be interrupted */
1096 if (fatal_signal_pending(current
)) {
1102 /* readahead for multi pages of dir */
1103 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
1104 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
1105 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
1107 dentry_page
= f2fs_find_data_page(inode
, n
);
1108 if (IS_ERR(dentry_page
)) {
1109 err
= PTR_ERR(dentry_page
);
1110 if (err
== -ENOENT
) {
1118 dentry_blk
= page_address(dentry_page
);
1120 make_dentry_ptr_block(inode
, &d
, dentry_blk
);
1122 err
= f2fs_fill_dentries(ctx
, &d
,
1123 n
* NR_DENTRY_IN_BLOCK
, &fstr
);
1125 f2fs_put_page(dentry_page
, 0);
1129 f2fs_put_page(dentry_page
, 0);
1132 fscrypt_fname_free_buffer(&fstr
);
1134 trace_f2fs_readdir(inode
, start_pos
, ctx
->pos
, err
);
1135 return err
< 0 ? err
: 0;
1138 const struct file_operations f2fs_dir_operations
= {
1139 .llseek
= generic_file_llseek
,
1140 .read
= generic_read_dir
,
1141 .iterate_shared
= f2fs_readdir
,
1142 .fsync
= f2fs_sync_file
,
1143 .unlocked_ioctl
= f2fs_ioctl
,
1144 #ifdef CONFIG_COMPAT
1145 .compat_ioctl
= f2fs_compat_ioctl
,