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 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
,
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
, struct inode
*inode
)
64 umode_t mode
= inode
->i_mode
;
65 de
->file_type
= f2fs_type_by_mode
[(mode
& S_IFMT
) >> S_SHIFT
];
68 static unsigned long dir_block_index(unsigned int level
,
69 int dir_level
, unsigned int idx
)
72 unsigned long bidx
= 0;
74 for (i
= 0; i
< level
; i
++)
75 bidx
+= dir_buckets(i
, dir_level
) * bucket_blocks(i
);
76 bidx
+= idx
* bucket_blocks(level
);
80 static bool early_match_name(size_t namelen
, f2fs_hash_t namehash
,
81 struct f2fs_dir_entry
*de
)
83 if (le16_to_cpu(de
->name_len
) != namelen
)
86 if (de
->hash_code
!= namehash
)
92 static struct f2fs_dir_entry
*find_in_block(struct page
*dentry_page
,
93 struct qstr
*name
, int *max_slots
,
94 f2fs_hash_t namehash
, struct page
**res_page
)
96 struct f2fs_dir_entry
*de
;
97 unsigned long bit_pos
= 0;
98 struct f2fs_dentry_block
*dentry_blk
= kmap(dentry_page
);
99 const void *dentry_bits
= &dentry_blk
->dentry_bitmap
;
102 while (bit_pos
< NR_DENTRY_IN_BLOCK
) {
103 if (!test_bit_le(bit_pos
, dentry_bits
)) {
106 else if (!test_bit_le(bit_pos
- 1, dentry_bits
))
111 de
= &dentry_blk
->dentry
[bit_pos
];
112 if (early_match_name(name
->len
, namehash
, de
)) {
113 if (!memcmp(dentry_blk
->filename
[bit_pos
],
116 *res_page
= dentry_page
;
120 if (max_len
> *max_slots
) {
121 *max_slots
= max_len
;
126 * For the most part, it should be a bug when name_len is zero.
127 * We stop here for figuring out where the bugs are occurred.
129 f2fs_bug_on(!de
->name_len
);
131 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
137 if (max_len
> *max_slots
)
138 *max_slots
= max_len
;
142 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
143 unsigned int level
, struct qstr
*name
,
144 f2fs_hash_t namehash
, struct page
**res_page
)
146 int s
= GET_DENTRY_SLOTS(name
->len
);
147 unsigned int nbucket
, nblock
;
148 unsigned int bidx
, end_block
;
149 struct page
*dentry_page
;
150 struct f2fs_dir_entry
*de
= NULL
;
154 f2fs_bug_on(level
> MAX_DIR_HASH_DEPTH
);
156 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
157 nblock
= bucket_blocks(level
);
159 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
160 le32_to_cpu(namehash
) % nbucket
);
161 end_block
= bidx
+ nblock
;
163 for (; bidx
< end_block
; bidx
++) {
164 /* no need to allocate new dentry pages to all the indices */
165 dentry_page
= find_data_page(dir
, bidx
, true);
166 if (IS_ERR(dentry_page
)) {
171 de
= find_in_block(dentry_page
, name
, &max_slots
,
178 f2fs_put_page(dentry_page
, 0);
181 if (!de
&& room
&& F2FS_I(dir
)->chash
!= namehash
) {
182 F2FS_I(dir
)->chash
= namehash
;
183 F2FS_I(dir
)->clevel
= level
;
190 * Find an entry in the specified directory with the wanted name.
191 * It returns the page where the entry was found (as a parameter - res_page),
192 * and the entry itself. Page is returned mapped and unlocked.
193 * Entry is guaranteed to be valid.
195 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
196 struct qstr
*child
, struct page
**res_page
)
198 unsigned long npages
= dir_blocks(dir
);
199 struct f2fs_dir_entry
*de
= NULL
;
200 f2fs_hash_t name_hash
;
201 unsigned int max_depth
;
209 name_hash
= f2fs_dentry_hash(child
);
210 max_depth
= F2FS_I(dir
)->i_current_depth
;
212 for (level
= 0; level
< max_depth
; level
++) {
213 de
= find_in_level(dir
, level
, child
, name_hash
, res_page
);
217 if (!de
&& F2FS_I(dir
)->chash
!= name_hash
) {
218 F2FS_I(dir
)->chash
= name_hash
;
219 F2FS_I(dir
)->clevel
= level
- 1;
224 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
227 struct f2fs_dir_entry
*de
;
228 struct f2fs_dentry_block
*dentry_blk
;
230 page
= get_lock_data_page(dir
, 0);
234 dentry_blk
= kmap(page
);
235 de
= &dentry_blk
->dentry
[1];
241 ino_t
f2fs_inode_by_name(struct inode
*dir
, struct qstr
*qstr
)
244 struct f2fs_dir_entry
*de
;
247 de
= f2fs_find_entry(dir
, qstr
, &page
);
249 res
= le32_to_cpu(de
->ino
);
251 f2fs_put_page(page
, 0);
257 void f2fs_set_link(struct inode
*dir
, struct f2fs_dir_entry
*de
,
258 struct page
*page
, struct inode
*inode
)
261 f2fs_wait_on_page_writeback(page
, DATA
);
262 de
->ino
= cpu_to_le32(inode
->i_ino
);
263 set_de_type(de
, inode
);
265 set_page_dirty(page
);
266 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
267 mark_inode_dirty(dir
);
269 f2fs_put_page(page
, 1);
272 static void init_dent_inode(const struct qstr
*name
, struct page
*ipage
)
274 struct f2fs_inode
*ri
;
276 f2fs_wait_on_page_writeback(ipage
, NODE
);
278 /* copy name info. to this inode page */
279 ri
= F2FS_INODE(ipage
);
280 ri
->i_namelen
= cpu_to_le32(name
->len
);
281 memcpy(ri
->i_name
, name
->name
, name
->len
);
282 set_page_dirty(ipage
);
285 int update_dent_inode(struct inode
*inode
, const struct qstr
*name
)
287 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
290 page
= get_node_page(sbi
, inode
->i_ino
);
292 return PTR_ERR(page
);
294 init_dent_inode(name
, page
);
295 f2fs_put_page(page
, 1);
300 static int make_empty_dir(struct inode
*inode
,
301 struct inode
*parent
, struct page
*page
)
303 struct page
*dentry_page
;
304 struct f2fs_dentry_block
*dentry_blk
;
305 struct f2fs_dir_entry
*de
;
307 dentry_page
= get_new_data_page(inode
, page
, 0, true);
308 if (IS_ERR(dentry_page
))
309 return PTR_ERR(dentry_page
);
312 dentry_blk
= kmap_atomic(dentry_page
);
314 de
= &dentry_blk
->dentry
[0];
315 de
->name_len
= cpu_to_le16(1);
317 de
->ino
= cpu_to_le32(inode
->i_ino
);
318 memcpy(dentry_blk
->filename
[0], ".", 1);
319 set_de_type(de
, inode
);
321 de
= &dentry_blk
->dentry
[1];
323 de
->name_len
= cpu_to_le16(2);
324 de
->ino
= cpu_to_le32(parent
->i_ino
);
325 memcpy(dentry_blk
->filename
[1], "..", 2);
326 set_de_type(de
, inode
);
328 test_and_set_bit_le(0, &dentry_blk
->dentry_bitmap
);
329 test_and_set_bit_le(1, &dentry_blk
->dentry_bitmap
);
330 kunmap_atomic(dentry_blk
);
332 set_page_dirty(dentry_page
);
333 f2fs_put_page(dentry_page
, 1);
337 static struct page
*init_inode_metadata(struct inode
*inode
,
338 struct inode
*dir
, const struct qstr
*name
)
340 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
344 if (is_inode_flag_set(F2FS_I(inode
), FI_NEW_INODE
)) {
345 page
= new_inode_page(inode
);
349 if (S_ISDIR(inode
->i_mode
)) {
350 err
= make_empty_dir(inode
, dir
, page
);
355 err
= f2fs_init_acl(inode
, dir
, page
);
359 err
= f2fs_init_security(inode
, dir
, name
, page
);
363 page
= get_node_page(F2FS_SB(dir
->i_sb
), inode
->i_ino
);
367 set_cold_node(inode
, page
);
371 init_dent_inode(name
, page
);
374 * This file should be checkpointed during fsync.
375 * We lost i_pino from now on.
377 if (is_inode_flag_set(F2FS_I(inode
), FI_INC_LINK
)) {
378 file_lost_pino(inode
);
380 * If link the tmpfile to alias through linkat path,
381 * we should remove this inode from orphan list.
383 if (inode
->i_nlink
== 0)
384 remove_orphan_inode(sbi
, inode
->i_ino
);
390 f2fs_put_page(page
, 1);
392 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
393 truncate_inode_pages(&inode
->i_data
, 0);
394 truncate_blocks(inode
, 0);
395 remove_dirty_dir_inode(inode
);
396 remove_inode_page(inode
);
400 static void update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
401 unsigned int current_depth
)
403 if (is_inode_flag_set(F2FS_I(inode
), FI_NEW_INODE
)) {
404 if (S_ISDIR(inode
->i_mode
)) {
406 set_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
408 clear_inode_flag(F2FS_I(inode
), FI_NEW_INODE
);
410 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
411 mark_inode_dirty(dir
);
413 if (F2FS_I(dir
)->i_current_depth
!= current_depth
) {
414 F2FS_I(dir
)->i_current_depth
= current_depth
;
415 set_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
418 if (is_inode_flag_set(F2FS_I(inode
), FI_INC_LINK
))
419 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
422 static int room_for_filename(struct f2fs_dentry_block
*dentry_blk
, int slots
)
425 int zero_start
, zero_end
;
427 zero_start
= find_next_zero_bit_le(&dentry_blk
->dentry_bitmap
,
430 if (zero_start
>= NR_DENTRY_IN_BLOCK
)
431 return NR_DENTRY_IN_BLOCK
;
433 zero_end
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
436 if (zero_end
- zero_start
>= slots
)
439 bit_start
= zero_end
+ 1;
441 if (zero_end
+ 1 >= NR_DENTRY_IN_BLOCK
)
442 return NR_DENTRY_IN_BLOCK
;
447 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
450 int __f2fs_add_link(struct inode
*dir
, const struct qstr
*name
,
453 unsigned int bit_pos
;
455 unsigned int current_depth
;
456 unsigned long bidx
, block
;
457 f2fs_hash_t dentry_hash
;
458 struct f2fs_dir_entry
*de
;
459 unsigned int nbucket
, nblock
;
460 size_t namelen
= name
->len
;
461 struct page
*dentry_page
= NULL
;
462 struct f2fs_dentry_block
*dentry_blk
= NULL
;
463 int slots
= GET_DENTRY_SLOTS(namelen
);
468 dentry_hash
= f2fs_dentry_hash(name
);
470 current_depth
= F2FS_I(dir
)->i_current_depth
;
471 if (F2FS_I(dir
)->chash
== dentry_hash
) {
472 level
= F2FS_I(dir
)->clevel
;
473 F2FS_I(dir
)->chash
= 0;
477 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
480 /* Increase the depth, if required */
481 if (level
== current_depth
)
484 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
485 nblock
= bucket_blocks(level
);
487 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
488 (le32_to_cpu(dentry_hash
) % nbucket
));
490 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
491 dentry_page
= get_new_data_page(dir
, NULL
, block
, true);
492 if (IS_ERR(dentry_page
))
493 return PTR_ERR(dentry_page
);
495 dentry_blk
= kmap(dentry_page
);
496 bit_pos
= room_for_filename(dentry_blk
, slots
);
497 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
501 f2fs_put_page(dentry_page
, 1);
504 /* Move to next level to find the empty slot for new dentry */
508 f2fs_wait_on_page_writeback(dentry_page
, DATA
);
510 down_write(&F2FS_I(inode
)->i_sem
);
511 page
= init_inode_metadata(inode
, dir
, name
);
516 de
= &dentry_blk
->dentry
[bit_pos
];
517 de
->hash_code
= dentry_hash
;
518 de
->name_len
= cpu_to_le16(namelen
);
519 memcpy(dentry_blk
->filename
[bit_pos
], name
->name
, name
->len
);
520 de
->ino
= cpu_to_le32(inode
->i_ino
);
521 set_de_type(de
, inode
);
522 for (i
= 0; i
< slots
; i
++)
523 test_and_set_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
524 set_page_dirty(dentry_page
);
526 /* we don't need to mark_inode_dirty now */
527 F2FS_I(inode
)->i_pino
= dir
->i_ino
;
528 update_inode(inode
, page
);
529 f2fs_put_page(page
, 1);
531 update_parent_metadata(dir
, inode
, current_depth
);
533 up_write(&F2FS_I(inode
)->i_sem
);
535 if (is_inode_flag_set(F2FS_I(dir
), FI_UPDATE_DIR
)) {
536 update_inode_page(dir
);
537 clear_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
540 f2fs_put_page(dentry_page
, 1);
544 int f2fs_do_tmpfile(struct inode
*inode
, struct inode
*dir
)
549 down_write(&F2FS_I(inode
)->i_sem
);
550 page
= init_inode_metadata(inode
, dir
, NULL
);
555 /* we don't need to mark_inode_dirty now */
556 update_inode(inode
, page
);
557 f2fs_put_page(page
, 1);
559 clear_inode_flag(F2FS_I(inode
), FI_NEW_INODE
);
561 up_write(&F2FS_I(inode
)->i_sem
);
566 * It only removes the dentry from the dentry page,corresponding name
567 * entry in name page does not need to be touched during deletion.
569 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
572 struct f2fs_dentry_block
*dentry_blk
;
573 unsigned int bit_pos
;
574 struct address_space
*mapping
= page
->mapping
;
575 struct inode
*dir
= mapping
->host
;
576 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
580 f2fs_wait_on_page_writeback(page
, DATA
);
582 dentry_blk
= page_address(page
);
583 bit_pos
= dentry
- dentry_blk
->dentry
;
584 for (i
= 0; i
< slots
; i
++)
585 test_and_clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
587 /* Let's check and deallocate this dentry page */
588 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
591 kunmap(page
); /* kunmap - pair of f2fs_find_entry */
592 set_page_dirty(page
);
594 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
597 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
599 down_write(&F2FS_I(inode
)->i_sem
);
601 if (S_ISDIR(inode
->i_mode
)) {
603 update_inode_page(dir
);
605 inode
->i_ctime
= CURRENT_TIME
;
607 if (S_ISDIR(inode
->i_mode
)) {
609 i_size_write(inode
, 0);
611 up_write(&F2FS_I(inode
)->i_sem
);
612 update_inode_page(inode
);
614 if (inode
->i_nlink
== 0)
615 add_orphan_inode(sbi
, inode
->i_ino
);
617 release_orphan_inode(sbi
);
620 if (bit_pos
== NR_DENTRY_IN_BLOCK
) {
621 truncate_hole(dir
, page
->index
, page
->index
+ 1);
622 clear_page_dirty_for_io(page
);
623 ClearPageUptodate(page
);
624 inode_dec_dirty_dents(dir
);
626 f2fs_put_page(page
, 1);
629 bool f2fs_empty_dir(struct inode
*dir
)
632 struct page
*dentry_page
;
633 unsigned int bit_pos
;
634 struct f2fs_dentry_block
*dentry_blk
;
635 unsigned long nblock
= dir_blocks(dir
);
637 for (bidx
= 0; bidx
< nblock
; bidx
++) {
638 dentry_page
= get_lock_data_page(dir
, bidx
);
639 if (IS_ERR(dentry_page
)) {
640 if (PTR_ERR(dentry_page
) == -ENOENT
)
647 dentry_blk
= kmap_atomic(dentry_page
);
652 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
655 kunmap_atomic(dentry_blk
);
657 f2fs_put_page(dentry_page
, 1);
659 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
665 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
667 struct inode
*inode
= file_inode(file
);
668 unsigned long npages
= dir_blocks(inode
);
669 unsigned int bit_pos
= 0;
670 struct f2fs_dentry_block
*dentry_blk
= NULL
;
671 struct f2fs_dir_entry
*de
= NULL
;
672 struct page
*dentry_page
= NULL
;
673 struct file_ra_state
*ra
= &file
->f_ra
;
674 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
675 unsigned char d_type
= DT_UNKNOWN
;
677 bit_pos
= ((unsigned long)ctx
->pos
% NR_DENTRY_IN_BLOCK
);
679 /* readahead for multi pages of dir */
680 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
681 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
682 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
684 for (; n
< npages
; n
++) {
685 dentry_page
= get_lock_data_page(inode
, n
);
686 if (IS_ERR(dentry_page
))
689 dentry_blk
= kmap(dentry_page
);
690 while (bit_pos
< NR_DENTRY_IN_BLOCK
) {
691 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
694 if (bit_pos
>= NR_DENTRY_IN_BLOCK
)
697 de
= &dentry_blk
->dentry
[bit_pos
];
698 if (de
->file_type
< F2FS_FT_MAX
)
699 d_type
= f2fs_filetype_table
[de
->file_type
];
703 dentry_blk
->filename
[bit_pos
],
704 le16_to_cpu(de
->name_len
),
705 le32_to_cpu(de
->ino
), d_type
))
708 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
709 ctx
->pos
= n
* NR_DENTRY_IN_BLOCK
+ bit_pos
;
712 ctx
->pos
= (n
+ 1) * NR_DENTRY_IN_BLOCK
;
714 f2fs_put_page(dentry_page
, 1);
718 if (dentry_page
&& !IS_ERR(dentry_page
)) {
720 f2fs_put_page(dentry_page
, 1);
726 const struct file_operations f2fs_dir_operations
= {
727 .llseek
= generic_file_llseek
,
728 .read
= generic_read_dir
,
729 .iterate
= f2fs_readdir
,
730 .fsync
= f2fs_sync_file
,
731 .unlocked_ioctl
= f2fs_ioctl
,