4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
13 #include <linux/sched/signal.h>
18 #include <trace/events/f2fs.h>
20 static unsigned long dir_blocks(struct inode
*inode
)
22 return ((unsigned long long) (i_size_read(inode
) + PAGE_SIZE
- 1))
26 static unsigned int dir_buckets(unsigned int level
, int dir_level
)
28 if (level
+ dir_level
< MAX_DIR_HASH_DEPTH
/ 2)
29 return 1 << (level
+ dir_level
);
31 return MAX_DIR_BUCKETS
;
34 static unsigned int bucket_blocks(unsigned int level
)
36 if (level
< MAX_DIR_HASH_DEPTH
/ 2)
42 static unsigned char f2fs_filetype_table
[F2FS_FT_MAX
] = {
43 [F2FS_FT_UNKNOWN
] = DT_UNKNOWN
,
44 [F2FS_FT_REG_FILE
] = DT_REG
,
45 [F2FS_FT_DIR
] = DT_DIR
,
46 [F2FS_FT_CHRDEV
] = DT_CHR
,
47 [F2FS_FT_BLKDEV
] = DT_BLK
,
48 [F2FS_FT_FIFO
] = DT_FIFO
,
49 [F2FS_FT_SOCK
] = DT_SOCK
,
50 [F2FS_FT_SYMLINK
] = DT_LNK
,
53 static unsigned char f2fs_type_by_mode
[S_IFMT
>> S_SHIFT
] = {
54 [S_IFREG
>> S_SHIFT
] = F2FS_FT_REG_FILE
,
55 [S_IFDIR
>> S_SHIFT
] = F2FS_FT_DIR
,
56 [S_IFCHR
>> S_SHIFT
] = F2FS_FT_CHRDEV
,
57 [S_IFBLK
>> S_SHIFT
] = F2FS_FT_BLKDEV
,
58 [S_IFIFO
>> S_SHIFT
] = F2FS_FT_FIFO
,
59 [S_IFSOCK
>> S_SHIFT
] = F2FS_FT_SOCK
,
60 [S_IFLNK
>> S_SHIFT
] = F2FS_FT_SYMLINK
,
63 void set_de_type(struct f2fs_dir_entry
*de
, umode_t mode
)
65 de
->file_type
= f2fs_type_by_mode
[(mode
& S_IFMT
) >> S_SHIFT
];
68 unsigned char get_de_type(struct f2fs_dir_entry
*de
)
70 if (de
->file_type
< F2FS_FT_MAX
)
71 return f2fs_filetype_table
[de
->file_type
];
75 static unsigned long dir_block_index(unsigned int level
,
76 int dir_level
, unsigned int idx
)
79 unsigned long bidx
= 0;
81 for (i
= 0; i
< level
; i
++)
82 bidx
+= dir_buckets(i
, dir_level
) * bucket_blocks(i
);
83 bidx
+= idx
* bucket_blocks(level
);
87 static struct f2fs_dir_entry
*find_in_block(struct page
*dentry_page
,
88 struct fscrypt_name
*fname
,
91 struct page
**res_page
)
93 struct f2fs_dentry_block
*dentry_blk
;
94 struct f2fs_dir_entry
*de
;
95 struct f2fs_dentry_ptr d
;
97 dentry_blk
= (struct f2fs_dentry_block
*)kmap(dentry_page
);
99 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
100 de
= find_target_dentry(fname
, namehash
, max_slots
, &d
);
102 *res_page
= dentry_page
;
109 struct f2fs_dir_entry
*find_target_dentry(struct fscrypt_name
*fname
,
110 f2fs_hash_t namehash
, int *max_slots
,
111 struct f2fs_dentry_ptr
*d
)
113 struct f2fs_dir_entry
*de
;
114 unsigned long bit_pos
= 0;
119 while (bit_pos
< d
->max
) {
120 if (!test_bit_le(bit_pos
, d
->bitmap
)) {
126 de
= &d
->dentry
[bit_pos
];
128 if (unlikely(!de
->name_len
)) {
133 if (de
->hash_code
== namehash
&&
134 fscrypt_match_name(fname
, d
->filename
[bit_pos
],
135 le16_to_cpu(de
->name_len
)))
138 if (max_slots
&& max_len
> *max_slots
)
139 *max_slots
= max_len
;
142 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
147 if (max_slots
&& max_len
> *max_slots
)
148 *max_slots
= max_len
;
152 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
154 struct fscrypt_name
*fname
,
155 struct page
**res_page
)
157 struct qstr name
= FSTR_TO_QSTR(&fname
->disk_name
);
158 int s
= GET_DENTRY_SLOTS(name
.len
);
159 unsigned int nbucket
, nblock
;
160 unsigned int bidx
, end_block
;
161 struct page
*dentry_page
;
162 struct f2fs_dir_entry
*de
= NULL
;
165 f2fs_hash_t namehash
= f2fs_dentry_hash(&name
, fname
);
167 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
168 nblock
= bucket_blocks(level
);
170 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
171 le32_to_cpu(namehash
) % nbucket
);
172 end_block
= bidx
+ nblock
;
174 for (; bidx
< end_block
; bidx
++) {
175 /* no need to allocate new dentry pages to all the indices */
176 dentry_page
= find_data_page(dir
, bidx
);
177 if (IS_ERR(dentry_page
)) {
178 if (PTR_ERR(dentry_page
) == -ENOENT
) {
182 *res_page
= dentry_page
;
187 de
= find_in_block(dentry_page
, fname
, namehash
, &max_slots
,
194 f2fs_put_page(dentry_page
, 0);
197 if (!de
&& room
&& F2FS_I(dir
)->chash
!= namehash
) {
198 F2FS_I(dir
)->chash
= namehash
;
199 F2FS_I(dir
)->clevel
= level
;
205 struct f2fs_dir_entry
*__f2fs_find_entry(struct inode
*dir
,
206 struct fscrypt_name
*fname
, struct page
**res_page
)
208 unsigned long npages
= dir_blocks(dir
);
209 struct f2fs_dir_entry
*de
= NULL
;
210 unsigned int max_depth
;
213 if (f2fs_has_inline_dentry(dir
)) {
215 de
= find_in_inline_dir(dir
, fname
, res_page
);
224 max_depth
= F2FS_I(dir
)->i_current_depth
;
225 if (unlikely(max_depth
> MAX_DIR_HASH_DEPTH
)) {
226 f2fs_msg(F2FS_I_SB(dir
)->sb
, KERN_WARNING
,
227 "Corrupted max_depth of %lu: %u",
228 dir
->i_ino
, max_depth
);
229 max_depth
= MAX_DIR_HASH_DEPTH
;
230 f2fs_i_depth_write(dir
, max_depth
);
233 for (level
= 0; level
< max_depth
; level
++) {
235 de
= find_in_level(dir
, level
, fname
, res_page
);
236 if (de
|| IS_ERR(*res_page
))
240 /* This is to increase the speed of f2fs_create */
242 F2FS_I(dir
)->task
= current
;
247 * Find an entry in the specified directory with the wanted name.
248 * It returns the page where the entry was found (as a parameter - res_page),
249 * and the entry itself. Page is returned mapped and unlocked.
250 * Entry is guaranteed to be valid.
252 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
253 const struct qstr
*child
, struct page
**res_page
)
255 struct f2fs_dir_entry
*de
= NULL
;
256 struct fscrypt_name fname
;
259 err
= fscrypt_setup_filename(dir
, child
, 1, &fname
);
264 *res_page
= ERR_PTR(err
);
268 de
= __f2fs_find_entry(dir
, &fname
, res_page
);
270 fscrypt_free_filename(&fname
);
274 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
276 struct qstr dotdot
= QSTR_INIT("..", 2);
278 return f2fs_find_entry(dir
, &dotdot
, p
);
281 ino_t
f2fs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
,
285 struct f2fs_dir_entry
*de
;
287 de
= f2fs_find_entry(dir
, qstr
, page
);
289 res
= le32_to_cpu(de
->ino
);
290 f2fs_dentry_kunmap(dir
, *page
);
291 f2fs_put_page(*page
, 0);
297 void f2fs_set_link(struct inode
*dir
, struct f2fs_dir_entry
*de
,
298 struct page
*page
, struct inode
*inode
)
300 enum page_type type
= f2fs_has_inline_dentry(dir
) ? NODE
: DATA
;
302 f2fs_wait_on_page_writeback(page
, type
, true);
303 de
->ino
= cpu_to_le32(inode
->i_ino
);
304 set_de_type(de
, inode
->i_mode
);
305 f2fs_dentry_kunmap(dir
, page
);
306 set_page_dirty(page
);
308 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
309 f2fs_mark_inode_dirty_sync(dir
, false);
310 f2fs_put_page(page
, 1);
313 static void init_dent_inode(const struct qstr
*name
, struct page
*ipage
)
315 struct f2fs_inode
*ri
;
317 f2fs_wait_on_page_writeback(ipage
, NODE
, true);
319 /* copy name info. to this inode page */
320 ri
= F2FS_INODE(ipage
);
321 ri
->i_namelen
= cpu_to_le32(name
->len
);
322 memcpy(ri
->i_name
, name
->name
, name
->len
);
323 set_page_dirty(ipage
);
326 void do_make_empty_dir(struct inode
*inode
, struct inode
*parent
,
327 struct f2fs_dentry_ptr
*d
)
329 struct qstr dot
= QSTR_INIT(".", 1);
330 struct qstr dotdot
= QSTR_INIT("..", 2);
332 /* update dirent of "." */
333 f2fs_update_dentry(inode
->i_ino
, inode
->i_mode
, d
, &dot
, 0, 0);
335 /* update dirent of ".." */
336 f2fs_update_dentry(parent
->i_ino
, parent
->i_mode
, d
, &dotdot
, 0, 1);
339 static int make_empty_dir(struct inode
*inode
,
340 struct inode
*parent
, struct page
*page
)
342 struct page
*dentry_page
;
343 struct f2fs_dentry_block
*dentry_blk
;
344 struct f2fs_dentry_ptr d
;
346 if (f2fs_has_inline_dentry(inode
))
347 return make_empty_inline_dir(inode
, parent
, page
);
349 dentry_page
= get_new_data_page(inode
, page
, 0, true);
350 if (IS_ERR(dentry_page
))
351 return PTR_ERR(dentry_page
);
353 dentry_blk
= kmap_atomic(dentry_page
);
355 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
356 do_make_empty_dir(inode
, parent
, &d
);
358 kunmap_atomic(dentry_blk
);
360 set_page_dirty(dentry_page
);
361 f2fs_put_page(dentry_page
, 1);
365 struct page
*init_inode_metadata(struct inode
*inode
, struct inode
*dir
,
366 const struct qstr
*new_name
, const struct qstr
*orig_name
,
372 if (is_inode_flag_set(inode
, FI_NEW_INODE
)) {
373 page
= new_inode_page(inode
);
377 if (S_ISDIR(inode
->i_mode
)) {
378 /* in order to handle error case */
380 err
= make_empty_dir(inode
, dir
, page
);
388 err
= f2fs_init_acl(inode
, dir
, page
, dpage
);
392 err
= f2fs_init_security(inode
, dir
, orig_name
, page
);
396 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
)) {
397 err
= fscrypt_inherit_context(dir
, inode
, page
, false);
402 page
= get_node_page(F2FS_I_SB(dir
), inode
->i_ino
);
406 set_cold_node(inode
, page
);
410 init_dent_inode(new_name
, page
);
411 if (f2fs_encrypted_inode(dir
))
412 file_set_enc_name(inode
);
416 * This file should be checkpointed during fsync.
417 * We lost i_pino from now on.
419 if (is_inode_flag_set(inode
, FI_INC_LINK
)) {
420 if (!S_ISDIR(inode
->i_mode
))
421 file_lost_pino(inode
);
423 * If link the tmpfile to alias through linkat path,
424 * we should remove this inode from orphan list.
426 if (inode
->i_nlink
== 0)
427 remove_orphan_inode(F2FS_I_SB(dir
), inode
->i_ino
);
428 f2fs_i_links_write(inode
, true);
434 update_inode(inode
, page
);
435 f2fs_put_page(page
, 1);
439 void update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
440 unsigned int current_depth
)
442 if (inode
&& is_inode_flag_set(inode
, FI_NEW_INODE
)) {
443 if (S_ISDIR(inode
->i_mode
))
444 f2fs_i_links_write(dir
, true);
445 clear_inode_flag(inode
, FI_NEW_INODE
);
447 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
448 f2fs_mark_inode_dirty_sync(dir
, false);
450 if (F2FS_I(dir
)->i_current_depth
!= current_depth
)
451 f2fs_i_depth_write(dir
, current_depth
);
453 if (inode
&& is_inode_flag_set(inode
, FI_INC_LINK
))
454 clear_inode_flag(inode
, FI_INC_LINK
);
457 int room_for_filename(const void *bitmap
, int slots
, int max_slots
)
460 int zero_start
, zero_end
;
462 zero_start
= find_next_zero_bit_le(bitmap
, max_slots
, bit_start
);
463 if (zero_start
>= max_slots
)
466 zero_end
= find_next_bit_le(bitmap
, max_slots
, zero_start
);
467 if (zero_end
- zero_start
>= slots
)
470 bit_start
= zero_end
+ 1;
472 if (zero_end
+ 1 >= max_slots
)
477 void f2fs_update_dentry(nid_t ino
, umode_t mode
, struct f2fs_dentry_ptr
*d
,
478 const struct qstr
*name
, f2fs_hash_t name_hash
,
479 unsigned int bit_pos
)
481 struct f2fs_dir_entry
*de
;
482 int slots
= GET_DENTRY_SLOTS(name
->len
);
485 de
= &d
->dentry
[bit_pos
];
486 de
->hash_code
= name_hash
;
487 de
->name_len
= cpu_to_le16(name
->len
);
488 memcpy(d
->filename
[bit_pos
], name
->name
, name
->len
);
489 de
->ino
= cpu_to_le32(ino
);
490 set_de_type(de
, mode
);
491 for (i
= 0; i
< slots
; i
++) {
492 __set_bit_le(bit_pos
+ i
, (void *)d
->bitmap
);
493 /* avoid wrong garbage data for readdir */
495 (de
+ i
)->name_len
= 0;
499 int f2fs_add_regular_entry(struct inode
*dir
, const struct qstr
*new_name
,
500 const struct qstr
*orig_name
,
501 struct inode
*inode
, nid_t ino
, umode_t mode
)
503 unsigned int bit_pos
;
505 unsigned int current_depth
;
506 unsigned long bidx
, block
;
507 f2fs_hash_t dentry_hash
;
508 unsigned int nbucket
, nblock
;
509 struct page
*dentry_page
= NULL
;
510 struct f2fs_dentry_block
*dentry_blk
= NULL
;
511 struct f2fs_dentry_ptr d
;
512 struct page
*page
= NULL
;
516 slots
= GET_DENTRY_SLOTS(new_name
->len
);
517 dentry_hash
= f2fs_dentry_hash(new_name
, NULL
);
519 current_depth
= F2FS_I(dir
)->i_current_depth
;
520 if (F2FS_I(dir
)->chash
== dentry_hash
) {
521 level
= F2FS_I(dir
)->clevel
;
522 F2FS_I(dir
)->chash
= 0;
526 #ifdef CONFIG_F2FS_FAULT_INJECTION
527 if (time_to_inject(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
)) {
528 f2fs_show_injection_info(FAULT_DIR_DEPTH
);
532 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
535 /* Increase the depth, if required */
536 if (level
== current_depth
)
539 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
540 nblock
= bucket_blocks(level
);
542 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
543 (le32_to_cpu(dentry_hash
) % nbucket
));
545 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
546 dentry_page
= get_new_data_page(dir
, NULL
, block
, true);
547 if (IS_ERR(dentry_page
))
548 return PTR_ERR(dentry_page
);
550 dentry_blk
= kmap(dentry_page
);
551 bit_pos
= room_for_filename(&dentry_blk
->dentry_bitmap
,
552 slots
, NR_DENTRY_IN_BLOCK
);
553 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
557 f2fs_put_page(dentry_page
, 1);
560 /* Move to next level to find the empty slot for new dentry */
564 f2fs_wait_on_page_writeback(dentry_page
, DATA
, true);
567 down_write(&F2FS_I(inode
)->i_sem
);
568 page
= init_inode_metadata(inode
, dir
, new_name
,
576 make_dentry_ptr_block(NULL
, &d
, dentry_blk
);
577 f2fs_update_dentry(ino
, mode
, &d
, new_name
, dentry_hash
, bit_pos
);
579 set_page_dirty(dentry_page
);
582 f2fs_i_pino_write(inode
, dir
->i_ino
);
583 f2fs_put_page(page
, 1);
586 update_parent_metadata(dir
, inode
, current_depth
);
589 up_write(&F2FS_I(inode
)->i_sem
);
592 f2fs_put_page(dentry_page
, 1);
597 int __f2fs_do_add_link(struct inode
*dir
, struct fscrypt_name
*fname
,
598 struct inode
*inode
, nid_t ino
, umode_t mode
)
600 struct qstr new_name
;
603 new_name
.name
= fname_name(fname
);
604 new_name
.len
= fname_len(fname
);
606 if (f2fs_has_inline_dentry(dir
))
607 err
= f2fs_add_inline_entry(dir
, &new_name
, fname
->usr_fname
,
610 err
= f2fs_add_regular_entry(dir
, &new_name
, fname
->usr_fname
,
613 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
618 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
621 int __f2fs_add_link(struct inode
*dir
, const struct qstr
*name
,
622 struct inode
*inode
, nid_t ino
, umode_t mode
)
624 struct fscrypt_name fname
;
625 struct page
*page
= NULL
;
626 struct f2fs_dir_entry
*de
= NULL
;
629 err
= fscrypt_setup_filename(dir
, name
, 0, &fname
);
634 * An immature stakable filesystem shows a race condition between lookup
635 * and create. If we have same task when doing lookup and create, it's
636 * definitely fine as expected by VFS normally. Otherwise, let's just
637 * verify on-disk dentry one more time, which guarantees filesystem
640 if (current
!= F2FS_I(dir
)->task
) {
641 de
= __f2fs_find_entry(dir
, &fname
, &page
);
642 F2FS_I(dir
)->task
= NULL
;
645 f2fs_dentry_kunmap(dir
, page
);
646 f2fs_put_page(page
, 0);
648 } else if (IS_ERR(page
)) {
651 err
= __f2fs_do_add_link(dir
, &fname
, inode
, ino
, mode
);
653 fscrypt_free_filename(&fname
);
657 int f2fs_do_tmpfile(struct inode
*inode
, struct inode
*dir
)
662 down_write(&F2FS_I(inode
)->i_sem
);
663 page
= init_inode_metadata(inode
, dir
, NULL
, NULL
, NULL
);
668 f2fs_put_page(page
, 1);
670 clear_inode_flag(inode
, FI_NEW_INODE
);
672 up_write(&F2FS_I(inode
)->i_sem
);
673 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
677 void f2fs_drop_nlink(struct inode
*dir
, struct inode
*inode
)
679 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
681 down_write(&F2FS_I(inode
)->i_sem
);
683 if (S_ISDIR(inode
->i_mode
))
684 f2fs_i_links_write(dir
, false);
685 inode
->i_ctime
= current_time(inode
);
687 f2fs_i_links_write(inode
, false);
688 if (S_ISDIR(inode
->i_mode
)) {
689 f2fs_i_links_write(inode
, false);
690 f2fs_i_size_write(inode
, 0);
692 up_write(&F2FS_I(inode
)->i_sem
);
694 if (inode
->i_nlink
== 0)
695 add_orphan_inode(inode
);
697 release_orphan_inode(sbi
);
701 * It only removes the dentry from the dentry page, corresponding name
702 * entry in name page does not need to be touched during deletion.
704 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
705 struct inode
*dir
, struct inode
*inode
)
707 struct f2fs_dentry_block
*dentry_blk
;
708 unsigned int bit_pos
;
709 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
710 struct address_space
*mapping
= page_mapping(page
);
714 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
716 add_ino_entry(F2FS_I_SB(dir
), dir
->i_ino
, TRANS_DIR_INO
);
718 if (f2fs_has_inline_dentry(dir
))
719 return f2fs_delete_inline_entry(dentry
, page
, dir
, inode
);
722 f2fs_wait_on_page_writeback(page
, DATA
, true);
724 dentry_blk
= page_address(page
);
725 bit_pos
= dentry
- dentry_blk
->dentry
;
726 for (i
= 0; i
< slots
; i
++)
727 __clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
729 /* Let's check and deallocate this dentry page */
730 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
733 kunmap(page
); /* kunmap - pair of f2fs_find_entry */
734 set_page_dirty(page
);
736 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
737 f2fs_mark_inode_dirty_sync(dir
, false);
740 f2fs_drop_nlink(dir
, inode
);
742 if (bit_pos
== NR_DENTRY_IN_BLOCK
&&
743 !truncate_hole(dir
, page
->index
, page
->index
+ 1)) {
744 spin_lock_irqsave(&mapping
->tree_lock
, flags
);
745 radix_tree_tag_clear(&mapping
->page_tree
, page_index(page
),
746 PAGECACHE_TAG_DIRTY
);
747 spin_unlock_irqrestore(&mapping
->tree_lock
, flags
);
749 clear_page_dirty_for_io(page
);
750 ClearPagePrivate(page
);
751 ClearPageUptodate(page
);
752 inode_dec_dirty_pages(dir
);
753 remove_dirty_inode(dir
);
755 f2fs_put_page(page
, 1);
758 bool f2fs_empty_dir(struct inode
*dir
)
761 struct page
*dentry_page
;
762 unsigned int bit_pos
;
763 struct f2fs_dentry_block
*dentry_blk
;
764 unsigned long nblock
= dir_blocks(dir
);
766 if (f2fs_has_inline_dentry(dir
))
767 return f2fs_empty_inline_dir(dir
);
769 for (bidx
= 0; bidx
< nblock
; bidx
++) {
770 dentry_page
= get_lock_data_page(dir
, bidx
, false);
771 if (IS_ERR(dentry_page
)) {
772 if (PTR_ERR(dentry_page
) == -ENOENT
)
778 dentry_blk
= kmap_atomic(dentry_page
);
783 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
786 kunmap_atomic(dentry_blk
);
788 f2fs_put_page(dentry_page
, 1);
790 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
796 int f2fs_fill_dentries(struct dir_context
*ctx
, struct f2fs_dentry_ptr
*d
,
797 unsigned int start_pos
, struct fscrypt_str
*fstr
)
799 unsigned char d_type
= DT_UNKNOWN
;
800 unsigned int bit_pos
;
801 struct f2fs_dir_entry
*de
= NULL
;
802 struct fscrypt_str de_name
= FSTR_INIT(NULL
, 0);
803 struct f2fs_sb_info
*sbi
= F2FS_I_SB(d
->inode
);
805 bit_pos
= ((unsigned long)ctx
->pos
% d
->max
);
807 while (bit_pos
< d
->max
) {
808 bit_pos
= find_next_bit_le(d
->bitmap
, d
->max
, bit_pos
);
809 if (bit_pos
>= d
->max
)
812 de
= &d
->dentry
[bit_pos
];
813 if (de
->name_len
== 0) {
815 ctx
->pos
= start_pos
+ bit_pos
;
819 d_type
= get_de_type(de
);
821 de_name
.name
= d
->filename
[bit_pos
];
822 de_name
.len
= le16_to_cpu(de
->name_len
);
824 if (f2fs_encrypted_inode(d
->inode
)) {
825 int save_len
= fstr
->len
;
828 err
= fscrypt_fname_disk_to_usr(d
->inode
,
829 (u32
)de
->hash_code
, 0,
835 fstr
->len
= save_len
;
838 if (!dir_emit(ctx
, de_name
.name
, de_name
.len
,
839 le32_to_cpu(de
->ino
), d_type
))
842 if (sbi
->readdir_ra
== 1)
843 ra_node_page(sbi
, le32_to_cpu(de
->ino
));
845 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
846 ctx
->pos
= start_pos
+ bit_pos
;
851 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
853 struct inode
*inode
= file_inode(file
);
854 unsigned long npages
= dir_blocks(inode
);
855 struct f2fs_dentry_block
*dentry_blk
= NULL
;
856 struct page
*dentry_page
= NULL
;
857 struct file_ra_state
*ra
= &file
->f_ra
;
858 loff_t start_pos
= ctx
->pos
;
859 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
860 struct f2fs_dentry_ptr d
;
861 struct fscrypt_str fstr
= FSTR_INIT(NULL
, 0);
864 if (f2fs_encrypted_inode(inode
)) {
865 err
= fscrypt_get_encryption_info(inode
);
866 if (err
&& err
!= -ENOKEY
)
869 err
= fscrypt_fname_alloc_buffer(inode
, F2FS_NAME_LEN
, &fstr
);
874 if (f2fs_has_inline_dentry(inode
)) {
875 err
= f2fs_read_inline_dir(file
, ctx
, &fstr
);
879 for (; n
< npages
; n
++, ctx
->pos
= n
* NR_DENTRY_IN_BLOCK
) {
881 /* allow readdir() to be interrupted */
882 if (fatal_signal_pending(current
)) {
888 /* readahead for multi pages of dir */
889 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
890 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
891 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
893 dentry_page
= get_lock_data_page(inode
, n
, false);
894 if (IS_ERR(dentry_page
)) {
895 err
= PTR_ERR(dentry_page
);
896 if (err
== -ENOENT
) {
904 dentry_blk
= kmap(dentry_page
);
906 make_dentry_ptr_block(inode
, &d
, dentry_blk
);
908 err
= f2fs_fill_dentries(ctx
, &d
,
909 n
* NR_DENTRY_IN_BLOCK
, &fstr
);
912 f2fs_put_page(dentry_page
, 1);
917 f2fs_put_page(dentry_page
, 1);
920 fscrypt_fname_free_buffer(&fstr
);
922 trace_f2fs_readdir(inode
, start_pos
, ctx
->pos
, err
);
923 return err
< 0 ? err
: 0;
926 static int f2fs_dir_open(struct inode
*inode
, struct file
*filp
)
928 if (f2fs_encrypted_inode(inode
))
929 return fscrypt_get_encryption_info(inode
) ? -EACCES
: 0;
933 const struct file_operations f2fs_dir_operations
= {
934 .llseek
= generic_file_llseek
,
935 .read
= generic_read_dir
,
936 .iterate_shared
= f2fs_readdir
,
937 .fsync
= f2fs_sync_file
,
938 .open
= f2fs_dir_open
,
939 .unlocked_ioctl
= f2fs_ioctl
,
941 .compat_ioctl
= f2fs_compat_ioctl
,