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_CACHE_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 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 f2fs_filename
*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
;
105 * For the most part, it should be a bug when name_len is zero.
106 * We stop here for figuring out where the bugs has occurred.
108 f2fs_bug_on(F2FS_P_SB(dentry_page
), d
.max
< 0);
112 struct f2fs_dir_entry
*find_target_dentry(struct f2fs_filename
*fname
,
113 f2fs_hash_t namehash
, int *max_slots
,
114 struct f2fs_dentry_ptr
*d
)
116 struct f2fs_dir_entry
*de
;
117 unsigned long bit_pos
= 0;
119 struct f2fs_str de_name
= FSTR_INIT(NULL
, 0);
120 struct f2fs_str
*name
= &fname
->disk_name
;
124 while (bit_pos
< d
->max
) {
125 if (!test_bit_le(bit_pos
, d
->bitmap
)) {
131 de
= &d
->dentry
[bit_pos
];
133 if (de
->hash_code
!= namehash
)
136 de_name
.name
= d
->filename
[bit_pos
];
137 de_name
.len
= le16_to_cpu(de
->name_len
);
139 #ifdef CONFIG_F2FS_FS_ENCRYPTION
140 if (unlikely(!name
->name
)) {
141 if (fname
->usr_fname
->name
[0] == '_') {
142 if (de_name
.len
> 32 &&
143 !memcmp(de_name
.name
+ ((de_name
.len
- 17) & ~15),
144 fname
->crypto_buf
.name
+ 8, 16))
148 name
->name
= fname
->crypto_buf
.name
;
149 name
->len
= fname
->crypto_buf
.len
;
152 if (de_name
.len
== name
->len
&&
153 !memcmp(de_name
.name
, name
->name
, name
->len
))
156 if (max_slots
&& max_len
> *max_slots
)
157 *max_slots
= max_len
;
160 /* remain bug on condition */
161 if (unlikely(!de
->name_len
))
164 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
169 if (max_slots
&& max_len
> *max_slots
)
170 *max_slots
= max_len
;
174 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
176 struct f2fs_filename
*fname
,
177 struct page
**res_page
)
179 struct qstr name
= FSTR_TO_QSTR(&fname
->disk_name
);
180 int s
= GET_DENTRY_SLOTS(name
.len
);
181 unsigned int nbucket
, nblock
;
182 unsigned int bidx
, end_block
;
183 struct page
*dentry_page
;
184 struct f2fs_dir_entry
*de
= NULL
;
187 f2fs_hash_t namehash
;
189 namehash
= f2fs_dentry_hash(&name
, fname
);
191 f2fs_bug_on(F2FS_I_SB(dir
), level
> MAX_DIR_HASH_DEPTH
);
193 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
194 nblock
= bucket_blocks(level
);
196 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
197 le32_to_cpu(namehash
) % nbucket
);
198 end_block
= bidx
+ nblock
;
200 for (; bidx
< end_block
; bidx
++) {
201 /* no need to allocate new dentry pages to all the indices */
202 dentry_page
= find_data_page(dir
, bidx
);
203 if (IS_ERR(dentry_page
)) {
208 de
= find_in_block(dentry_page
, fname
, namehash
, &max_slots
,
215 f2fs_put_page(dentry_page
, 0);
218 if (!de
&& room
&& F2FS_I(dir
)->chash
!= namehash
) {
219 F2FS_I(dir
)->chash
= namehash
;
220 F2FS_I(dir
)->clevel
= level
;
227 * Find an entry in the specified directory with the wanted name.
228 * It returns the page where the entry was found (as a parameter - res_page),
229 * and the entry itself. Page is returned mapped and unlocked.
230 * Entry is guaranteed to be valid.
232 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
233 struct qstr
*child
, struct page
**res_page
)
235 unsigned long npages
= dir_blocks(dir
);
236 struct f2fs_dir_entry
*de
= NULL
;
237 unsigned int max_depth
;
239 struct f2fs_filename fname
;
244 err
= f2fs_fname_setup_filename(dir
, child
, 1, &fname
);
248 if (f2fs_has_inline_dentry(dir
)) {
249 de
= find_in_inline_dir(dir
, &fname
, res_page
);
256 max_depth
= F2FS_I(dir
)->i_current_depth
;
258 for (level
= 0; level
< max_depth
; level
++) {
259 de
= find_in_level(dir
, level
, &fname
, res_page
);
264 f2fs_fname_free_filename(&fname
);
268 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
271 struct f2fs_dir_entry
*de
;
272 struct f2fs_dentry_block
*dentry_blk
;
274 if (f2fs_has_inline_dentry(dir
))
275 return f2fs_parent_inline_dir(dir
, p
);
277 page
= get_lock_data_page(dir
, 0, false);
281 dentry_blk
= kmap(page
);
282 de
= &dentry_blk
->dentry
[1];
288 ino_t
f2fs_inode_by_name(struct inode
*dir
, struct qstr
*qstr
)
291 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
);
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
);
314 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
315 mark_inode_dirty(dir
);
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
);
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 f2fs_dir_entry
*de
;
357 de
->name_len
= cpu_to_le16(1);
359 de
->ino
= cpu_to_le32(inode
->i_ino
);
360 memcpy(d
->filename
[0], ".", 1);
361 set_de_type(de
, inode
->i_mode
);
365 de
->name_len
= cpu_to_le16(2);
366 de
->ino
= cpu_to_le32(parent
->i_ino
);
367 memcpy(d
->filename
[1], "..", 2);
368 set_de_type(de
, parent
->i_mode
);
370 test_and_set_bit_le(0, (void *)d
->bitmap
);
371 test_and_set_bit_le(1, (void *)d
->bitmap
);
374 static int make_empty_dir(struct inode
*inode
,
375 struct inode
*parent
, struct page
*page
)
377 struct page
*dentry_page
;
378 struct f2fs_dentry_block
*dentry_blk
;
379 struct f2fs_dentry_ptr d
;
381 if (f2fs_has_inline_dentry(inode
))
382 return make_empty_inline_dir(inode
, parent
, page
);
384 dentry_page
= get_new_data_page(inode
, page
, 0, true);
385 if (IS_ERR(dentry_page
))
386 return PTR_ERR(dentry_page
);
388 dentry_blk
= kmap_atomic(dentry_page
);
390 make_dentry_ptr(NULL
, &d
, (void *)dentry_blk
, 1);
391 do_make_empty_dir(inode
, parent
, &d
);
393 kunmap_atomic(dentry_blk
);
395 set_page_dirty(dentry_page
);
396 f2fs_put_page(dentry_page
, 1);
400 struct page
*init_inode_metadata(struct inode
*inode
, struct inode
*dir
,
401 const struct qstr
*name
, struct page
*dpage
)
406 if (is_inode_flag_set(F2FS_I(inode
), FI_NEW_INODE
)) {
407 page
= new_inode_page(inode
);
411 if (S_ISDIR(inode
->i_mode
)) {
412 err
= make_empty_dir(inode
, dir
, page
);
417 err
= f2fs_init_acl(inode
, dir
, page
, dpage
);
421 err
= f2fs_init_security(inode
, dir
, name
, page
);
425 if (f2fs_encrypted_inode(dir
) && f2fs_may_encrypt(inode
)) {
426 err
= f2fs_inherit_context(dir
, inode
, page
);
431 page
= get_node_page(F2FS_I_SB(dir
), inode
->i_ino
);
435 set_cold_node(inode
, page
);
439 init_dent_inode(name
, page
);
442 * This file should be checkpointed during fsync.
443 * We lost i_pino from now on.
445 if (is_inode_flag_set(F2FS_I(inode
), FI_INC_LINK
)) {
446 file_lost_pino(inode
);
448 * If link the tmpfile to alias through linkat path,
449 * we should remove this inode from orphan list.
451 if (inode
->i_nlink
== 0)
452 remove_orphan_inode(F2FS_I_SB(dir
), inode
->i_ino
);
458 f2fs_put_page(page
, 1);
460 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
461 truncate_inode_pages(&inode
->i_data
, 0);
462 truncate_blocks(inode
, 0, false);
463 remove_dirty_dir_inode(inode
);
464 remove_inode_page(inode
);
468 void update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
469 unsigned int current_depth
)
471 if (inode
&& is_inode_flag_set(F2FS_I(inode
), FI_NEW_INODE
)) {
472 if (S_ISDIR(inode
->i_mode
)) {
474 set_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
476 clear_inode_flag(F2FS_I(inode
), FI_NEW_INODE
);
478 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
479 mark_inode_dirty(dir
);
481 if (F2FS_I(dir
)->i_current_depth
!= current_depth
) {
482 F2FS_I(dir
)->i_current_depth
= current_depth
;
483 set_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
486 if (inode
&& is_inode_flag_set(F2FS_I(inode
), FI_INC_LINK
))
487 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
490 int room_for_filename(const void *bitmap
, int slots
, int max_slots
)
493 int zero_start
, zero_end
;
495 zero_start
= find_next_zero_bit_le(bitmap
, max_slots
, bit_start
);
496 if (zero_start
>= max_slots
)
499 zero_end
= find_next_bit_le(bitmap
, max_slots
, zero_start
);
500 if (zero_end
- zero_start
>= slots
)
503 bit_start
= zero_end
+ 1;
505 if (zero_end
+ 1 >= max_slots
)
510 void f2fs_update_dentry(nid_t ino
, umode_t mode
, struct f2fs_dentry_ptr
*d
,
511 const struct qstr
*name
, f2fs_hash_t name_hash
,
512 unsigned int bit_pos
)
514 struct f2fs_dir_entry
*de
;
515 int slots
= GET_DENTRY_SLOTS(name
->len
);
518 de
= &d
->dentry
[bit_pos
];
519 de
->hash_code
= name_hash
;
520 de
->name_len
= cpu_to_le16(name
->len
);
521 memcpy(d
->filename
[bit_pos
], name
->name
, name
->len
);
522 de
->ino
= cpu_to_le32(ino
);
523 set_de_type(de
, mode
);
524 for (i
= 0; i
< slots
; i
++)
525 test_and_set_bit_le(bit_pos
+ i
, (void *)d
->bitmap
);
528 int f2fs_add_regular_entry(struct inode
*dir
, const struct qstr
*new_name
,
529 struct inode
*inode
, nid_t ino
, umode_t mode
)
531 unsigned int bit_pos
;
533 unsigned int current_depth
;
534 unsigned long bidx
, block
;
535 f2fs_hash_t dentry_hash
;
536 unsigned int nbucket
, nblock
;
537 struct page
*dentry_page
= NULL
;
538 struct f2fs_dentry_block
*dentry_blk
= NULL
;
539 struct f2fs_dentry_ptr d
;
540 struct page
*page
= NULL
;
544 slots
= GET_DENTRY_SLOTS(new_name
->len
);
545 dentry_hash
= f2fs_dentry_hash(new_name
, NULL
);
547 current_depth
= F2FS_I(dir
)->i_current_depth
;
548 if (F2FS_I(dir
)->chash
== dentry_hash
) {
549 level
= F2FS_I(dir
)->clevel
;
550 F2FS_I(dir
)->chash
= 0;
554 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
557 /* Increase the depth, if required */
558 if (level
== current_depth
)
561 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
562 nblock
= bucket_blocks(level
);
564 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
565 (le32_to_cpu(dentry_hash
) % nbucket
));
567 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
568 dentry_page
= get_new_data_page(dir
, NULL
, block
, true);
569 if (IS_ERR(dentry_page
))
570 return PTR_ERR(dentry_page
);
572 dentry_blk
= kmap(dentry_page
);
573 bit_pos
= room_for_filename(&dentry_blk
->dentry_bitmap
,
574 slots
, NR_DENTRY_IN_BLOCK
);
575 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
579 f2fs_put_page(dentry_page
, 1);
582 /* Move to next level to find the empty slot for new dentry */
586 f2fs_wait_on_page_writeback(dentry_page
, DATA
);
589 down_write(&F2FS_I(inode
)->i_sem
);
590 page
= init_inode_metadata(inode
, dir
, new_name
, NULL
);
595 if (f2fs_encrypted_inode(dir
))
596 file_set_enc_name(inode
);
599 make_dentry_ptr(NULL
, &d
, (void *)dentry_blk
, 1);
600 f2fs_update_dentry(ino
, mode
, &d
, new_name
, dentry_hash
, bit_pos
);
602 set_page_dirty(dentry_page
);
605 /* we don't need to mark_inode_dirty now */
606 F2FS_I(inode
)->i_pino
= dir
->i_ino
;
607 update_inode(inode
, page
);
608 f2fs_put_page(page
, 1);
611 update_parent_metadata(dir
, inode
, current_depth
);
614 up_write(&F2FS_I(inode
)->i_sem
);
616 if (is_inode_flag_set(F2FS_I(dir
), FI_UPDATE_DIR
)) {
617 update_inode_page(dir
);
618 clear_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
621 f2fs_put_page(dentry_page
, 1);
627 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
630 int __f2fs_add_link(struct inode
*dir
, const struct qstr
*name
,
631 struct inode
*inode
, nid_t ino
, umode_t mode
)
633 struct f2fs_filename fname
;
634 struct qstr new_name
;
637 err
= f2fs_fname_setup_filename(dir
, name
, 0, &fname
);
641 new_name
.name
= fname_name(&fname
);
642 new_name
.len
= fname_len(&fname
);
645 if (f2fs_has_inline_dentry(dir
))
646 err
= f2fs_add_inline_entry(dir
, &new_name
, inode
, ino
, mode
);
648 err
= f2fs_add_regular_entry(dir
, &new_name
, inode
, ino
, mode
);
650 f2fs_fname_free_filename(&fname
);
654 int f2fs_do_tmpfile(struct inode
*inode
, struct inode
*dir
)
659 down_write(&F2FS_I(inode
)->i_sem
);
660 page
= init_inode_metadata(inode
, dir
, NULL
, NULL
);
665 /* we don't need to mark_inode_dirty now */
666 update_inode(inode
, page
);
667 f2fs_put_page(page
, 1);
669 clear_inode_flag(F2FS_I(inode
), FI_NEW_INODE
);
671 up_write(&F2FS_I(inode
)->i_sem
);
675 void f2fs_drop_nlink(struct inode
*dir
, struct inode
*inode
, struct page
*page
)
677 struct f2fs_sb_info
*sbi
= F2FS_I_SB(dir
);
679 down_write(&F2FS_I(inode
)->i_sem
);
681 if (S_ISDIR(inode
->i_mode
)) {
684 update_inode(dir
, page
);
686 update_inode_page(dir
);
688 inode
->i_ctime
= CURRENT_TIME
;
691 if (S_ISDIR(inode
->i_mode
)) {
693 i_size_write(inode
, 0);
695 up_write(&F2FS_I(inode
)->i_sem
);
696 update_inode_page(inode
);
698 if (inode
->i_nlink
== 0)
699 add_orphan_inode(sbi
, inode
->i_ino
);
701 release_orphan_inode(sbi
);
705 * It only removes the dentry from the dentry page, corresponding name
706 * entry in name page does not need to be touched during deletion.
708 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
709 struct inode
*dir
, struct inode
*inode
)
711 struct f2fs_dentry_block
*dentry_blk
;
712 unsigned int bit_pos
;
713 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
716 if (f2fs_has_inline_dentry(dir
))
717 return f2fs_delete_inline_entry(dentry
, page
, dir
, inode
);
720 f2fs_wait_on_page_writeback(page
, DATA
);
722 dentry_blk
= page_address(page
);
723 bit_pos
= dentry
- dentry_blk
->dentry
;
724 for (i
= 0; i
< slots
; i
++)
725 clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
727 /* Let's check and deallocate this dentry page */
728 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
731 kunmap(page
); /* kunmap - pair of f2fs_find_entry */
732 set_page_dirty(page
);
734 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
737 f2fs_drop_nlink(dir
, inode
, NULL
);
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
);
746 f2fs_put_page(page
, 1);
749 bool f2fs_empty_dir(struct inode
*dir
)
752 struct page
*dentry_page
;
753 unsigned int bit_pos
;
754 struct f2fs_dentry_block
*dentry_blk
;
755 unsigned long nblock
= dir_blocks(dir
);
757 if (f2fs_has_inline_dentry(dir
))
758 return f2fs_empty_inline_dir(dir
);
760 for (bidx
= 0; bidx
< nblock
; bidx
++) {
761 dentry_page
= get_lock_data_page(dir
, bidx
, false);
762 if (IS_ERR(dentry_page
)) {
763 if (PTR_ERR(dentry_page
) == -ENOENT
)
769 dentry_blk
= kmap_atomic(dentry_page
);
774 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
777 kunmap_atomic(dentry_blk
);
779 f2fs_put_page(dentry_page
, 1);
781 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
787 bool f2fs_fill_dentries(struct dir_context
*ctx
, struct f2fs_dentry_ptr
*d
,
788 unsigned int start_pos
, struct f2fs_str
*fstr
)
790 unsigned char d_type
= DT_UNKNOWN
;
791 unsigned int bit_pos
;
792 struct f2fs_dir_entry
*de
= NULL
;
793 struct f2fs_str de_name
= FSTR_INIT(NULL
, 0);
795 bit_pos
= ((unsigned long)ctx
->pos
% d
->max
);
797 while (bit_pos
< d
->max
) {
798 bit_pos
= find_next_bit_le(d
->bitmap
, d
->max
, bit_pos
);
799 if (bit_pos
>= d
->max
)
802 de
= &d
->dentry
[bit_pos
];
803 d_type
= get_de_type(de
);
805 de_name
.name
= d
->filename
[bit_pos
];
806 de_name
.len
= le16_to_cpu(de
->name_len
);
808 /* check memory boundary before moving forward */
809 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
810 if (unlikely(bit_pos
> d
->max
||
811 le16_to_cpu(de
->name_len
) > F2FS_NAME_LEN
)) {
812 f2fs_msg(F2FS_I_SB(d
->inode
)->sb
, KERN_WARNING
,
813 "%s: corrupted namelen=%d, run fsck to fix.",
814 __func__
, le16_to_cpu(de
->name_len
));
815 set_sbi_flag(F2FS_I_SB(d
->inode
)->sb
->s_fs_info
, SBI_NEED_FSCK
);
819 if (f2fs_encrypted_inode(d
->inode
)) {
820 int save_len
= fstr
->len
;
823 de_name
.name
= kmalloc(de_name
.len
, GFP_NOFS
);
827 memcpy(de_name
.name
, d
->filename
[bit_pos
], de_name
.len
);
829 ret
= f2fs_fname_disk_to_usr(d
->inode
, &de
->hash_code
,
836 fstr
->len
= save_len
;
839 if (!dir_emit(ctx
, de_name
.name
, de_name
.len
,
840 le32_to_cpu(de
->ino
), d_type
))
843 ctx
->pos
= start_pos
+ bit_pos
;
848 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
850 struct inode
*inode
= file_inode(file
);
851 unsigned long npages
= dir_blocks(inode
);
852 struct f2fs_dentry_block
*dentry_blk
= NULL
;
853 struct page
*dentry_page
= NULL
;
854 struct file_ra_state
*ra
= &file
->f_ra
;
855 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
856 struct f2fs_dentry_ptr d
;
857 struct f2fs_str fstr
= FSTR_INIT(NULL
, 0);
860 if (f2fs_encrypted_inode(inode
)) {
861 err
= f2fs_get_encryption_info(inode
);
865 err
= f2fs_fname_crypto_alloc_buffer(inode
, F2FS_NAME_LEN
,
871 if (f2fs_has_inline_dentry(inode
)) {
872 err
= f2fs_read_inline_dir(file
, ctx
, &fstr
);
876 /* readahead for multi pages of dir */
877 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
878 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
879 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
881 for (; n
< npages
; n
++) {
882 dentry_page
= get_lock_data_page(inode
, n
, false);
883 if (IS_ERR(dentry_page
))
886 dentry_blk
= kmap(dentry_page
);
888 make_dentry_ptr(inode
, &d
, (void *)dentry_blk
, 1);
890 if (f2fs_fill_dentries(ctx
, &d
, n
* NR_DENTRY_IN_BLOCK
, &fstr
))
893 ctx
->pos
= (n
+ 1) * NR_DENTRY_IN_BLOCK
;
895 f2fs_put_page(dentry_page
, 1);
899 if (dentry_page
&& !IS_ERR(dentry_page
)) {
901 f2fs_put_page(dentry_page
, 1);
904 f2fs_fname_crypto_free_buffer(&fstr
);
908 const struct file_operations f2fs_dir_operations
= {
909 .llseek
= generic_file_llseek
,
910 .read
= generic_read_dir
,
911 .iterate
= f2fs_readdir
,
912 .fsync
= f2fs_sync_file
,
913 .unlocked_ioctl
= f2fs_ioctl
,
915 .compat_ioctl
= f2fs_compat_ioctl
,