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>
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 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 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 page
*dentry_page
,
86 struct fscrypt_name
*fname
,
89 struct page
**res_page
)
91 struct f2fs_dentry_block
*dentry_blk
;
92 struct f2fs_dir_entry
*de
;
93 struct f2fs_dentry_ptr d
;
95 dentry_blk
= (struct f2fs_dentry_block
*)kmap(dentry_page
);
97 make_dentry_ptr(NULL
, &d
, (void *)dentry_blk
, 1);
98 de
= find_target_dentry(fname
, namehash
, max_slots
, &d
);
100 *res_page
= dentry_page
;
107 struct f2fs_dir_entry
*find_target_dentry(struct fscrypt_name
*fname
,
108 f2fs_hash_t namehash
, int *max_slots
,
109 struct f2fs_dentry_ptr
*d
)
111 struct f2fs_dir_entry
*de
;
112 unsigned long bit_pos
= 0;
114 struct fscrypt_str de_name
= FSTR_INIT(NULL
, 0);
115 struct fscrypt_str
*name
= &fname
->disk_name
;
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
)) {
134 de_name
.name
= d
->filename
[bit_pos
];
135 de_name
.len
= le16_to_cpu(de
->name_len
);
137 /* show encrypted name */
139 if (de
->hash_code
== cpu_to_le32(fname
->hash
))
141 } else if (de_name
.len
== name
->len
&&
142 de
->hash_code
== namehash
&&
143 !memcmp(de_name
.name
, name
->name
, name
->len
))
146 if (max_slots
&& max_len
> *max_slots
)
147 *max_slots
= max_len
;
150 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
155 if (max_slots
&& max_len
> *max_slots
)
156 *max_slots
= max_len
;
160 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
162 struct fscrypt_name
*fname
,
163 struct page
**res_page
)
165 struct qstr name
= FSTR_TO_QSTR(&fname
->disk_name
);
166 int s
= GET_DENTRY_SLOTS(name
.len
);
167 unsigned int nbucket
, nblock
;
168 unsigned int bidx
, end_block
;
169 struct page
*dentry_page
;
170 struct f2fs_dir_entry
*de
= NULL
;
173 f2fs_hash_t namehash
;
176 namehash
= cpu_to_le32(fname
->hash
);
178 namehash
= f2fs_dentry_hash(&name
);
180 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
181 nblock
= bucket_blocks(level
);
183 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
184 le32_to_cpu(namehash
) % nbucket
);
185 end_block
= bidx
+ nblock
;
187 for (; bidx
< end_block
; bidx
++) {
188 /* no need to allocate new dentry pages to all the indices */
189 dentry_page
= find_data_page(dir
, bidx
);
190 if (IS_ERR(dentry_page
)) {
191 if (PTR_ERR(dentry_page
) == -ENOENT
) {
195 *res_page
= dentry_page
;
200 de
= find_in_block(dentry_page
, fname
, namehash
, &max_slots
,
207 f2fs_put_page(dentry_page
, 0);
210 if (!de
&& room
&& F2FS_I(dir
)->chash
!= namehash
) {
211 F2FS_I(dir
)->chash
= namehash
;
212 F2FS_I(dir
)->clevel
= level
;
218 struct f2fs_dir_entry
*__f2fs_find_entry(struct inode
*dir
,
219 struct fscrypt_name
*fname
, struct page
**res_page
)
221 unsigned long npages
= dir_blocks(dir
);
222 struct f2fs_dir_entry
*de
= NULL
;
223 unsigned int max_depth
;
226 if (f2fs_has_inline_dentry(dir
)) {
228 de
= find_in_inline_dir(dir
, fname
, res_page
);
237 max_depth
= F2FS_I(dir
)->i_current_depth
;
238 if (unlikely(max_depth
> MAX_DIR_HASH_DEPTH
)) {
239 f2fs_msg(F2FS_I_SB(dir
)->sb
, KERN_WARNING
,
240 "Corrupted max_depth of %lu: %u",
241 dir
->i_ino
, max_depth
);
242 max_depth
= MAX_DIR_HASH_DEPTH
;
243 f2fs_i_depth_write(dir
, max_depth
);
246 for (level
= 0; level
< max_depth
; level
++) {
248 de
= find_in_level(dir
, level
, fname
, res_page
);
249 if (de
|| IS_ERR(*res_page
))
257 * Find an entry in the specified directory with the wanted name.
258 * It returns the page where the entry was found (as a parameter - res_page),
259 * and the entry itself. Page is returned mapped and unlocked.
260 * Entry is guaranteed to be valid.
262 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
263 const struct qstr
*child
, struct page
**res_page
)
265 struct f2fs_dir_entry
*de
= NULL
;
266 struct fscrypt_name fname
;
269 err
= fscrypt_setup_filename(dir
, child
, 1, &fname
);
271 *res_page
= ERR_PTR(err
);
275 de
= __f2fs_find_entry(dir
, &fname
, res_page
);
277 fscrypt_free_filename(&fname
);
281 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
283 struct qstr dotdot
= QSTR_INIT("..", 2);
285 return f2fs_find_entry(dir
, &dotdot
, p
);
288 ino_t
f2fs_inode_by_name(struct inode
*dir
, const struct qstr
*qstr
,
292 struct f2fs_dir_entry
*de
;
294 de
= f2fs_find_entry(dir
, qstr
, page
);
296 res
= le32_to_cpu(de
->ino
);
297 f2fs_dentry_kunmap(dir
, *page
);
298 f2fs_put_page(*page
, 0);
304 void f2fs_set_link(struct inode
*dir
, struct f2fs_dir_entry
*de
,
305 struct page
*page
, struct inode
*inode
)
307 enum page_type type
= f2fs_has_inline_dentry(dir
) ? NODE
: DATA
;
309 f2fs_wait_on_page_writeback(page
, type
, true);
310 de
->ino
= cpu_to_le32(inode
->i_ino
);
311 set_de_type(de
, inode
->i_mode
);
312 f2fs_dentry_kunmap(dir
, page
);
313 set_page_dirty(page
);
315 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
316 f2fs_mark_inode_dirty_sync(dir
, false);
317 f2fs_put_page(page
, 1);
320 static void init_dent_inode(const struct qstr
*name
, struct page
*ipage
)
322 struct f2fs_inode
*ri
;
324 f2fs_wait_on_page_writeback(ipage
, NODE
, true);
326 /* copy name info. to this inode page */
327 ri
= F2FS_INODE(ipage
);
328 ri
->i_namelen
= cpu_to_le32(name
->len
);
329 memcpy(ri
->i_name
, name
->name
, name
->len
);
330 set_page_dirty(ipage
);
333 int update_dent_inode(struct inode
*inode
, struct inode
*to
,
334 const struct qstr
*name
)
338 if (file_enc_name(to
))
341 page
= get_node_page(F2FS_I_SB(inode
), inode
->i_ino
);
343 return PTR_ERR(page
);
345 init_dent_inode(name
, page
);
346 f2fs_put_page(page
, 1);
351 void do_make_empty_dir(struct inode
*inode
, struct inode
*parent
,
352 struct f2fs_dentry_ptr
*d
)
354 struct qstr dot
= QSTR_INIT(".", 1);
355 struct qstr dotdot
= QSTR_INIT("..", 2);
357 /* update dirent of "." */
358 f2fs_update_dentry(inode
->i_ino
, inode
->i_mode
, d
, &dot
, 0, 0);
360 /* update dirent of ".." */
361 f2fs_update_dentry(parent
->i_ino
, parent
->i_mode
, d
, &dotdot
, 0, 1);
364 static int make_empty_dir(struct inode
*inode
,
365 struct inode
*parent
, struct page
*page
)
367 struct page
*dentry_page
;
368 struct f2fs_dentry_block
*dentry_blk
;
369 struct f2fs_dentry_ptr d
;
371 if (f2fs_has_inline_dentry(inode
))
372 return make_empty_inline_dir(inode
, parent
, page
);
374 dentry_page
= get_new_data_page(inode
, page
, 0, true);
375 if (IS_ERR(dentry_page
))
376 return PTR_ERR(dentry_page
);
378 dentry_blk
= kmap_atomic(dentry_page
);
380 make_dentry_ptr(NULL
, &d
, (void *)dentry_blk
, 1);
381 do_make_empty_dir(inode
, parent
, &d
);
383 kunmap_atomic(dentry_blk
);
385 set_page_dirty(dentry_page
);
386 f2fs_put_page(dentry_page
, 1);
390 struct page
*init_inode_metadata(struct inode
*inode
, struct inode
*dir
,
391 const struct qstr
*new_name
, const struct qstr
*orig_name
,
397 if (is_inode_flag_set(inode
, FI_NEW_INODE
)) {
398 page
= new_inode_page(inode
);
402 if (S_ISDIR(inode
->i_mode
)) {
403 /* in order to handle error case */
405 err
= make_empty_dir(inode
, dir
, page
);
413 err
= f2fs_init_acl(inode
, dir
, page
, dpage
);
417 err
= f2fs_init_security(inode
, dir
, orig_name
, page
);
421 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
)) {
422 err
= fscrypt_inherit_context(dir
, inode
, page
, false);
427 page
= get_node_page(F2FS_I_SB(dir
), inode
->i_ino
);
431 set_cold_node(inode
, page
);
435 init_dent_inode(new_name
, page
);
438 * This file should be checkpointed during fsync.
439 * We lost i_pino from now on.
441 if (is_inode_flag_set(inode
, FI_INC_LINK
)) {
442 file_lost_pino(inode
);
444 * If link the tmpfile to alias through linkat path,
445 * we should remove this inode from orphan list.
447 if (inode
->i_nlink
== 0)
448 remove_orphan_inode(F2FS_I_SB(dir
), inode
->i_ino
);
449 f2fs_i_links_write(inode
, true);
455 update_inode(inode
, page
);
456 f2fs_put_page(page
, 1);
460 void update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
461 unsigned int current_depth
)
463 if (inode
&& is_inode_flag_set(inode
, FI_NEW_INODE
)) {
464 if (S_ISDIR(inode
->i_mode
))
465 f2fs_i_links_write(dir
, true);
466 clear_inode_flag(inode
, FI_NEW_INODE
);
468 dir
->i_mtime
= dir
->i_ctime
= current_time(dir
);
469 f2fs_mark_inode_dirty_sync(dir
, false);
471 if (F2FS_I(dir
)->i_current_depth
!= current_depth
)
472 f2fs_i_depth_write(dir
, current_depth
);
474 if (inode
&& is_inode_flag_set(inode
, FI_INC_LINK
))
475 clear_inode_flag(inode
, FI_INC_LINK
);
478 int room_for_filename(const void *bitmap
, int slots
, int max_slots
)
481 int zero_start
, zero_end
;
483 zero_start
= find_next_zero_bit_le(bitmap
, max_slots
, bit_start
);
484 if (zero_start
>= max_slots
)
487 zero_end
= find_next_bit_le(bitmap
, max_slots
, zero_start
);
488 if (zero_end
- zero_start
>= slots
)
491 bit_start
= zero_end
+ 1;
493 if (zero_end
+ 1 >= max_slots
)
498 void f2fs_update_dentry(nid_t ino
, umode_t mode
, struct f2fs_dentry_ptr
*d
,
499 const struct qstr
*name
, f2fs_hash_t name_hash
,
500 unsigned int bit_pos
)
502 struct f2fs_dir_entry
*de
;
503 int slots
= GET_DENTRY_SLOTS(name
->len
);
506 de
= &d
->dentry
[bit_pos
];
507 de
->hash_code
= name_hash
;
508 de
->name_len
= cpu_to_le16(name
->len
);
509 memcpy(d
->filename
[bit_pos
], name
->name
, name
->len
);
510 de
->ino
= cpu_to_le32(ino
);
511 set_de_type(de
, mode
);
512 for (i
= 0; i
< slots
; i
++) {
513 __set_bit_le(bit_pos
+ i
, (void *)d
->bitmap
);
514 /* avoid wrong garbage data for readdir */
516 (de
+ i
)->name_len
= 0;
520 int f2fs_add_regular_entry(struct inode
*dir
, const struct qstr
*new_name
,
521 const struct qstr
*orig_name
,
522 struct inode
*inode
, nid_t ino
, umode_t mode
)
524 unsigned int bit_pos
;
526 unsigned int current_depth
;
527 unsigned long bidx
, block
;
528 f2fs_hash_t dentry_hash
;
529 unsigned int nbucket
, nblock
;
530 struct page
*dentry_page
= NULL
;
531 struct f2fs_dentry_block
*dentry_blk
= NULL
;
532 struct f2fs_dentry_ptr d
;
533 struct page
*page
= NULL
;
537 slots
= GET_DENTRY_SLOTS(new_name
->len
);
538 dentry_hash
= f2fs_dentry_hash(new_name
);
540 current_depth
= F2FS_I(dir
)->i_current_depth
;
541 if (F2FS_I(dir
)->chash
== dentry_hash
) {
542 level
= F2FS_I(dir
)->clevel
;
543 F2FS_I(dir
)->chash
= 0;
547 #ifdef CONFIG_F2FS_FAULT_INJECTION
548 if (time_to_inject(F2FS_I_SB(dir
), FAULT_DIR_DEPTH
))
551 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
554 /* Increase the depth, if required */
555 if (level
== current_depth
)
558 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
559 nblock
= bucket_blocks(level
);
561 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
562 (le32_to_cpu(dentry_hash
) % nbucket
));
564 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
565 dentry_page
= get_new_data_page(dir
, NULL
, block
, true);
566 if (IS_ERR(dentry_page
))
567 return PTR_ERR(dentry_page
);
569 dentry_blk
= kmap(dentry_page
);
570 bit_pos
= room_for_filename(&dentry_blk
->dentry_bitmap
,
571 slots
, NR_DENTRY_IN_BLOCK
);
572 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
576 f2fs_put_page(dentry_page
, 1);
579 /* Move to next level to find the empty slot for new dentry */
583 f2fs_wait_on_page_writeback(dentry_page
, DATA
, true);
586 down_write(&F2FS_I(inode
)->i_sem
);
587 page
= init_inode_metadata(inode
, dir
, new_name
,
593 if (f2fs_encrypted_inode(dir
))
594 file_set_enc_name(inode
);
597 make_dentry_ptr(NULL
, &d
, (void *)dentry_blk
, 1);
598 f2fs_update_dentry(ino
, mode
, &d
, new_name
, dentry_hash
, bit_pos
);
600 set_page_dirty(dentry_page
);
603 f2fs_i_pino_write(inode
, dir
->i_ino
);
604 f2fs_put_page(page
, 1);
607 update_parent_metadata(dir
, inode
, current_depth
);
610 up_write(&F2FS_I(inode
)->i_sem
);
613 f2fs_put_page(dentry_page
, 1);
618 int __f2fs_do_add_link(struct inode
*dir
, struct fscrypt_name
*fname
,
619 struct inode
*inode
, nid_t ino
, umode_t mode
)
621 struct qstr new_name
;
624 new_name
.name
= fname_name(fname
);
625 new_name
.len
= fname_len(fname
);
627 if (f2fs_has_inline_dentry(dir
))
628 err
= f2fs_add_inline_entry(dir
, &new_name
, fname
->usr_fname
,
631 err
= f2fs_add_regular_entry(dir
, &new_name
, fname
->usr_fname
,
634 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
639 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
642 int __f2fs_add_link(struct inode
*dir
, const struct qstr
*name
,
643 struct inode
*inode
, nid_t ino
, umode_t mode
)
645 struct fscrypt_name fname
;
648 err
= fscrypt_setup_filename(dir
, name
, 0, &fname
);
652 err
= __f2fs_do_add_link(dir
, &fname
, inode
, ino
, mode
);
654 fscrypt_free_filename(&fname
);
658 int f2fs_do_tmpfile(struct inode
*inode
, struct inode
*dir
)
663 down_write(&F2FS_I(inode
)->i_sem
);
664 page
= init_inode_metadata(inode
, dir
, NULL
, NULL
, NULL
);
669 f2fs_put_page(page
, 1);
671 clear_inode_flag(inode
, FI_NEW_INODE
);
673 up_write(&F2FS_I(inode
)->i_sem
);
674 f2fs_update_time(F2FS_I_SB(inode
), REQ_TIME
);
678 void f2fs_drop_nlink(struct inode
*dir
, struct inode
*inode
)
680 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
682 down_write(&F2FS_I(inode
)->i_sem
);
684 if (S_ISDIR(inode
->i_mode
))
685 f2fs_i_links_write(dir
, false);
686 inode
->i_ctime
= current_time(inode
);
688 f2fs_i_links_write(inode
, false);
689 if (S_ISDIR(inode
->i_mode
)) {
690 f2fs_i_links_write(inode
, false);
691 f2fs_i_size_write(inode
, 0);
693 up_write(&F2FS_I(inode
)->i_sem
);
695 if (inode
->i_nlink
== 0)
696 add_orphan_inode(inode
);
698 release_orphan_inode(sbi
);
702 * It only removes the dentry from the dentry page, corresponding name
703 * entry in name page does not need to be touched during deletion.
705 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
706 struct inode
*dir
, struct inode
*inode
)
708 struct f2fs_dentry_block
*dentry_blk
;
709 unsigned int bit_pos
;
710 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
713 f2fs_update_time(F2FS_I_SB(dir
), REQ_TIME
);
715 if (f2fs_has_inline_dentry(dir
))
716 return f2fs_delete_inline_entry(dentry
, page
, dir
, inode
);
719 f2fs_wait_on_page_writeback(page
, DATA
, true);
721 dentry_blk
= page_address(page
);
722 bit_pos
= dentry
- dentry_blk
->dentry
;
723 for (i
= 0; i
< slots
; i
++)
724 clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
726 /* Let's check and deallocate this dentry page */
727 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
730 kunmap(page
); /* kunmap - pair of f2fs_find_entry */
731 set_page_dirty(page
);
733 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
734 f2fs_mark_inode_dirty_sync(dir
, false);
737 f2fs_drop_nlink(dir
, inode
);
739 if (bit_pos
== NR_DENTRY_IN_BLOCK
&&
740 !truncate_hole(dir
, page
->index
, page
->index
+ 1)) {
741 clear_page_dirty_for_io(page
);
742 ClearPagePrivate(page
);
743 ClearPageUptodate(page
);
744 inode_dec_dirty_pages(dir
);
745 remove_dirty_inode(dir
);
747 f2fs_put_page(page
, 1);
750 bool f2fs_empty_dir(struct inode
*dir
)
753 struct page
*dentry_page
;
754 unsigned int bit_pos
;
755 struct f2fs_dentry_block
*dentry_blk
;
756 unsigned long nblock
= dir_blocks(dir
);
758 if (f2fs_has_inline_dentry(dir
))
759 return f2fs_empty_inline_dir(dir
);
761 for (bidx
= 0; bidx
< nblock
; bidx
++) {
762 dentry_page
= get_lock_data_page(dir
, bidx
, false);
763 if (IS_ERR(dentry_page
)) {
764 if (PTR_ERR(dentry_page
) == -ENOENT
)
770 dentry_blk
= kmap_atomic(dentry_page
);
775 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
778 kunmap_atomic(dentry_blk
);
780 f2fs_put_page(dentry_page
, 1);
782 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
788 int f2fs_fill_dentries(struct dir_context
*ctx
, struct f2fs_dentry_ptr
*d
,
789 unsigned int start_pos
, struct fscrypt_str
*fstr
)
791 unsigned char d_type
= DT_UNKNOWN
;
792 unsigned int bit_pos
;
793 struct f2fs_dir_entry
*de
= NULL
;
794 struct fscrypt_str de_name
= FSTR_INIT(NULL
, 0);
796 bit_pos
= ((unsigned long)ctx
->pos
% d
->max
);
798 while (bit_pos
< d
->max
) {
799 bit_pos
= find_next_bit_le(d
->bitmap
, d
->max
, bit_pos
);
800 if (bit_pos
>= d
->max
)
803 de
= &d
->dentry
[bit_pos
];
804 if (de
->name_len
== 0) {
806 ctx
->pos
= start_pos
+ bit_pos
;
810 d_type
= get_de_type(de
);
812 de_name
.name
= d
->filename
[bit_pos
];
813 de_name
.len
= le16_to_cpu(de
->name_len
);
815 if (f2fs_encrypted_inode(d
->inode
)) {
816 int save_len
= fstr
->len
;
819 err
= fscrypt_fname_disk_to_usr(d
->inode
,
820 (u32
)de
->hash_code
, 0,
826 fstr
->len
= save_len
;
829 if (!dir_emit(ctx
, de_name
.name
, de_name
.len
,
830 le32_to_cpu(de
->ino
), d_type
))
833 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
834 ctx
->pos
= start_pos
+ bit_pos
;
839 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
841 struct inode
*inode
= file_inode(file
);
842 unsigned long npages
= dir_blocks(inode
);
843 struct f2fs_dentry_block
*dentry_blk
= NULL
;
844 struct page
*dentry_page
= NULL
;
845 struct file_ra_state
*ra
= &file
->f_ra
;
846 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
847 struct f2fs_dentry_ptr d
;
848 struct fscrypt_str fstr
= FSTR_INIT(NULL
, 0);
851 if (f2fs_encrypted_inode(inode
)) {
852 err
= fscrypt_get_encryption_info(inode
);
853 if (err
&& err
!= -ENOKEY
)
856 err
= fscrypt_fname_alloc_buffer(inode
, F2FS_NAME_LEN
, &fstr
);
861 if (f2fs_has_inline_dentry(inode
)) {
862 err
= f2fs_read_inline_dir(file
, ctx
, &fstr
);
866 /* readahead for multi pages of dir */
867 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
868 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
869 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
871 for (; n
< npages
; n
++) {
872 dentry_page
= get_lock_data_page(inode
, n
, false);
873 if (IS_ERR(dentry_page
)) {
874 err
= PTR_ERR(dentry_page
);
875 if (err
== -ENOENT
) {
883 dentry_blk
= kmap(dentry_page
);
885 make_dentry_ptr(inode
, &d
, (void *)dentry_blk
, 1);
887 err
= f2fs_fill_dentries(ctx
, &d
,
888 n
* NR_DENTRY_IN_BLOCK
, &fstr
);
891 f2fs_put_page(dentry_page
, 1);
895 ctx
->pos
= (n
+ 1) * NR_DENTRY_IN_BLOCK
;
897 f2fs_put_page(dentry_page
, 1);
900 fscrypt_fname_free_buffer(&fstr
);
901 return err
< 0 ? err
: 0;
904 static int f2fs_dir_open(struct inode
*inode
, struct file
*filp
)
906 if (f2fs_encrypted_inode(inode
))
907 return fscrypt_get_encryption_info(inode
) ? -EACCES
: 0;
911 const struct file_operations f2fs_dir_operations
= {
912 .llseek
= generic_file_llseek
,
913 .read
= generic_read_dir
,
914 .iterate_shared
= f2fs_readdir
,
915 .fsync
= f2fs_sync_file
,
916 .open
= f2fs_dir_open
,
917 .unlocked_ioctl
= f2fs_ioctl
,
919 .compat_ioctl
= f2fs_compat_ioctl
,