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(const char *name
, size_t namelen
,
81 f2fs_hash_t namehash
, 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 const char *name
, size_t namelen
, 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
, namelen
, namehash
, de
)) {
113 if (!memcmp(dentry_blk
->filename
[bit_pos
],
115 *res_page
= dentry_page
;
119 if (max_len
> *max_slots
) {
120 *max_slots
= max_len
;
123 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
129 if (max_len
> *max_slots
)
130 *max_slots
= max_len
;
134 static struct f2fs_dir_entry
*find_in_level(struct inode
*dir
,
135 unsigned int level
, const char *name
, size_t namelen
,
136 f2fs_hash_t namehash
, struct page
**res_page
)
138 int s
= GET_DENTRY_SLOTS(namelen
);
139 unsigned int nbucket
, nblock
;
140 unsigned int bidx
, end_block
;
141 struct page
*dentry_page
;
142 struct f2fs_dir_entry
*de
= NULL
;
146 f2fs_bug_on(level
> MAX_DIR_HASH_DEPTH
);
148 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
149 nblock
= bucket_blocks(level
);
151 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
152 le32_to_cpu(namehash
) % nbucket
);
153 end_block
= bidx
+ nblock
;
155 for (; bidx
< end_block
; bidx
++) {
156 /* no need to allocate new dentry pages to all the indices */
157 dentry_page
= find_data_page(dir
, bidx
, true);
158 if (IS_ERR(dentry_page
)) {
163 de
= find_in_block(dentry_page
, name
, namelen
,
164 &max_slots
, namehash
, res_page
);
170 f2fs_put_page(dentry_page
, 0);
173 if (!de
&& room
&& F2FS_I(dir
)->chash
!= namehash
) {
174 F2FS_I(dir
)->chash
= namehash
;
175 F2FS_I(dir
)->clevel
= level
;
182 * Find an entry in the specified directory with the wanted name.
183 * It returns the page where the entry was found (as a parameter - res_page),
184 * and the entry itself. Page is returned mapped and unlocked.
185 * Entry is guaranteed to be valid.
187 struct f2fs_dir_entry
*f2fs_find_entry(struct inode
*dir
,
188 struct qstr
*child
, struct page
**res_page
)
190 const char *name
= child
->name
;
191 size_t namelen
= child
->len
;
192 unsigned long npages
= dir_blocks(dir
);
193 struct f2fs_dir_entry
*de
= NULL
;
194 f2fs_hash_t name_hash
;
195 unsigned int max_depth
;
203 name_hash
= f2fs_dentry_hash(name
, namelen
);
204 max_depth
= F2FS_I(dir
)->i_current_depth
;
206 for (level
= 0; level
< max_depth
; level
++) {
207 de
= find_in_level(dir
, level
, name
,
208 namelen
, name_hash
, res_page
);
212 if (!de
&& F2FS_I(dir
)->chash
!= name_hash
) {
213 F2FS_I(dir
)->chash
= name_hash
;
214 F2FS_I(dir
)->clevel
= level
- 1;
219 struct f2fs_dir_entry
*f2fs_parent_dir(struct inode
*dir
, struct page
**p
)
222 struct f2fs_dir_entry
*de
;
223 struct f2fs_dentry_block
*dentry_blk
;
225 page
= get_lock_data_page(dir
, 0);
229 dentry_blk
= kmap(page
);
230 de
= &dentry_blk
->dentry
[1];
236 ino_t
f2fs_inode_by_name(struct inode
*dir
, struct qstr
*qstr
)
239 struct f2fs_dir_entry
*de
;
242 de
= f2fs_find_entry(dir
, qstr
, &page
);
244 res
= le32_to_cpu(de
->ino
);
246 f2fs_put_page(page
, 0);
252 void f2fs_set_link(struct inode
*dir
, struct f2fs_dir_entry
*de
,
253 struct page
*page
, struct inode
*inode
)
256 f2fs_wait_on_page_writeback(page
, DATA
);
257 de
->ino
= cpu_to_le32(inode
->i_ino
);
258 set_de_type(de
, inode
);
260 set_page_dirty(page
);
261 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
262 mark_inode_dirty(dir
);
264 f2fs_put_page(page
, 1);
267 static void init_dent_inode(const struct qstr
*name
, struct page
*ipage
)
269 struct f2fs_inode
*ri
;
271 f2fs_wait_on_page_writeback(ipage
, NODE
);
273 /* copy name info. to this inode page */
274 ri
= F2FS_INODE(ipage
);
275 ri
->i_namelen
= cpu_to_le32(name
->len
);
276 memcpy(ri
->i_name
, name
->name
, name
->len
);
277 set_page_dirty(ipage
);
280 int update_dent_inode(struct inode
*inode
, const struct qstr
*name
)
282 struct f2fs_sb_info
*sbi
= F2FS_SB(inode
->i_sb
);
285 page
= get_node_page(sbi
, inode
->i_ino
);
287 return PTR_ERR(page
);
289 init_dent_inode(name
, page
);
290 f2fs_put_page(page
, 1);
295 static int make_empty_dir(struct inode
*inode
,
296 struct inode
*parent
, struct page
*page
)
298 struct page
*dentry_page
;
299 struct f2fs_dentry_block
*dentry_blk
;
300 struct f2fs_dir_entry
*de
;
303 dentry_page
= get_new_data_page(inode
, page
, 0, true);
304 if (IS_ERR(dentry_page
))
305 return PTR_ERR(dentry_page
);
307 kaddr
= kmap_atomic(dentry_page
);
308 dentry_blk
= (struct f2fs_dentry_block
*)kaddr
;
310 de
= &dentry_blk
->dentry
[0];
311 de
->name_len
= cpu_to_le16(1);
313 de
->ino
= cpu_to_le32(inode
->i_ino
);
314 memcpy(dentry_blk
->filename
[0], ".", 1);
315 set_de_type(de
, inode
);
317 de
= &dentry_blk
->dentry
[1];
319 de
->name_len
= cpu_to_le16(2);
320 de
->ino
= cpu_to_le32(parent
->i_ino
);
321 memcpy(dentry_blk
->filename
[1], "..", 2);
322 set_de_type(de
, inode
);
324 test_and_set_bit_le(0, &dentry_blk
->dentry_bitmap
);
325 test_and_set_bit_le(1, &dentry_blk
->dentry_bitmap
);
326 kunmap_atomic(kaddr
);
328 set_page_dirty(dentry_page
);
329 f2fs_put_page(dentry_page
, 1);
333 static struct page
*init_inode_metadata(struct inode
*inode
,
334 struct inode
*dir
, const struct qstr
*name
)
339 if (is_inode_flag_set(F2FS_I(inode
), FI_NEW_INODE
)) {
340 page
= new_inode_page(inode
, name
);
344 if (S_ISDIR(inode
->i_mode
)) {
345 err
= make_empty_dir(inode
, dir
, page
);
350 err
= f2fs_init_acl(inode
, dir
, page
);
354 err
= f2fs_init_security(inode
, dir
, name
, page
);
358 page
= get_node_page(F2FS_SB(dir
->i_sb
), inode
->i_ino
);
362 set_cold_node(inode
, page
);
365 init_dent_inode(name
, page
);
368 * This file should be checkpointed during fsync.
369 * We lost i_pino from now on.
371 if (is_inode_flag_set(F2FS_I(inode
), FI_INC_LINK
)) {
372 file_lost_pino(inode
);
378 f2fs_put_page(page
, 1);
380 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
381 truncate_inode_pages(&inode
->i_data
, 0);
382 truncate_blocks(inode
, 0);
383 remove_dirty_dir_inode(inode
);
384 remove_inode_page(inode
);
388 static void update_parent_metadata(struct inode
*dir
, struct inode
*inode
,
389 unsigned int current_depth
)
391 if (is_inode_flag_set(F2FS_I(inode
), FI_NEW_INODE
)) {
392 if (S_ISDIR(inode
->i_mode
)) {
394 set_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
396 clear_inode_flag(F2FS_I(inode
), FI_NEW_INODE
);
398 dir
->i_mtime
= dir
->i_ctime
= CURRENT_TIME
;
399 mark_inode_dirty(dir
);
401 if (F2FS_I(dir
)->i_current_depth
!= current_depth
) {
402 F2FS_I(dir
)->i_current_depth
= current_depth
;
403 set_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
406 if (is_inode_flag_set(F2FS_I(inode
), FI_INC_LINK
))
407 clear_inode_flag(F2FS_I(inode
), FI_INC_LINK
);
410 static int room_for_filename(struct f2fs_dentry_block
*dentry_blk
, int slots
)
413 int zero_start
, zero_end
;
415 zero_start
= find_next_zero_bit_le(&dentry_blk
->dentry_bitmap
,
418 if (zero_start
>= NR_DENTRY_IN_BLOCK
)
419 return NR_DENTRY_IN_BLOCK
;
421 zero_end
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
424 if (zero_end
- zero_start
>= slots
)
427 bit_start
= zero_end
+ 1;
429 if (zero_end
+ 1 >= NR_DENTRY_IN_BLOCK
)
430 return NR_DENTRY_IN_BLOCK
;
435 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
438 int __f2fs_add_link(struct inode
*dir
, const struct qstr
*name
,
441 unsigned int bit_pos
;
443 unsigned int current_depth
;
444 unsigned long bidx
, block
;
445 f2fs_hash_t dentry_hash
;
446 struct f2fs_dir_entry
*de
;
447 unsigned int nbucket
, nblock
;
448 size_t namelen
= name
->len
;
449 struct page
*dentry_page
= NULL
;
450 struct f2fs_dentry_block
*dentry_blk
= NULL
;
451 int slots
= GET_DENTRY_SLOTS(namelen
);
456 dentry_hash
= f2fs_dentry_hash(name
->name
, name
->len
);
458 current_depth
= F2FS_I(dir
)->i_current_depth
;
459 if (F2FS_I(dir
)->chash
== dentry_hash
) {
460 level
= F2FS_I(dir
)->clevel
;
461 F2FS_I(dir
)->chash
= 0;
465 if (unlikely(current_depth
== MAX_DIR_HASH_DEPTH
))
468 /* Increase the depth, if required */
469 if (level
== current_depth
)
472 nbucket
= dir_buckets(level
, F2FS_I(dir
)->i_dir_level
);
473 nblock
= bucket_blocks(level
);
475 bidx
= dir_block_index(level
, F2FS_I(dir
)->i_dir_level
,
476 (le32_to_cpu(dentry_hash
) % nbucket
));
478 for (block
= bidx
; block
<= (bidx
+ nblock
- 1); block
++) {
479 dentry_page
= get_new_data_page(dir
, NULL
, block
, true);
480 if (IS_ERR(dentry_page
))
481 return PTR_ERR(dentry_page
);
483 dentry_blk
= kmap(dentry_page
);
484 bit_pos
= room_for_filename(dentry_blk
, slots
);
485 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
489 f2fs_put_page(dentry_page
, 1);
492 /* Move to next level to find the empty slot for new dentry */
496 f2fs_wait_on_page_writeback(dentry_page
, DATA
);
498 down_write(&F2FS_I(inode
)->i_sem
);
499 page
= init_inode_metadata(inode
, dir
, name
);
504 de
= &dentry_blk
->dentry
[bit_pos
];
505 de
->hash_code
= dentry_hash
;
506 de
->name_len
= cpu_to_le16(namelen
);
507 memcpy(dentry_blk
->filename
[bit_pos
], name
->name
, name
->len
);
508 de
->ino
= cpu_to_le32(inode
->i_ino
);
509 set_de_type(de
, inode
);
510 for (i
= 0; i
< slots
; i
++)
511 test_and_set_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
512 set_page_dirty(dentry_page
);
514 /* we don't need to mark_inode_dirty now */
515 F2FS_I(inode
)->i_pino
= dir
->i_ino
;
516 update_inode(inode
, page
);
517 f2fs_put_page(page
, 1);
519 update_parent_metadata(dir
, inode
, current_depth
);
521 up_write(&F2FS_I(inode
)->i_sem
);
523 if (is_inode_flag_set(F2FS_I(dir
), FI_UPDATE_DIR
)) {
524 update_inode_page(dir
);
525 clear_inode_flag(F2FS_I(dir
), FI_UPDATE_DIR
);
528 f2fs_put_page(dentry_page
, 1);
533 * It only removes the dentry from the dentry page,corresponding name
534 * entry in name page does not need to be touched during deletion.
536 void f2fs_delete_entry(struct f2fs_dir_entry
*dentry
, struct page
*page
,
539 struct f2fs_dentry_block
*dentry_blk
;
540 unsigned int bit_pos
;
541 struct address_space
*mapping
= page
->mapping
;
542 struct inode
*dir
= mapping
->host
;
543 int slots
= GET_DENTRY_SLOTS(le16_to_cpu(dentry
->name_len
));
544 void *kaddr
= page_address(page
);
548 f2fs_wait_on_page_writeback(page
, DATA
);
550 dentry_blk
= (struct f2fs_dentry_block
*)kaddr
;
551 bit_pos
= dentry
- (struct f2fs_dir_entry
*)dentry_blk
->dentry
;
552 for (i
= 0; i
< slots
; i
++)
553 test_and_clear_bit_le(bit_pos
+ i
, &dentry_blk
->dentry_bitmap
);
555 /* Let's check and deallocate this dentry page */
556 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
559 kunmap(page
); /* kunmap - pair of f2fs_find_entry */
560 set_page_dirty(page
);
562 dir
->i_ctime
= dir
->i_mtime
= CURRENT_TIME
;
565 struct f2fs_sb_info
*sbi
= F2FS_SB(dir
->i_sb
);
567 down_write(&F2FS_I(inode
)->i_sem
);
569 if (S_ISDIR(inode
->i_mode
)) {
571 update_inode_page(dir
);
573 inode
->i_ctime
= CURRENT_TIME
;
575 if (S_ISDIR(inode
->i_mode
)) {
577 i_size_write(inode
, 0);
579 up_write(&F2FS_I(inode
)->i_sem
);
580 update_inode_page(inode
);
582 if (inode
->i_nlink
== 0)
583 add_orphan_inode(sbi
, inode
->i_ino
);
585 release_orphan_inode(sbi
);
588 if (bit_pos
== NR_DENTRY_IN_BLOCK
) {
589 truncate_hole(dir
, page
->index
, page
->index
+ 1);
590 clear_page_dirty_for_io(page
);
591 ClearPageUptodate(page
);
592 inode_dec_dirty_dents(dir
);
594 f2fs_put_page(page
, 1);
597 bool f2fs_empty_dir(struct inode
*dir
)
600 struct page
*dentry_page
;
601 unsigned int bit_pos
;
602 struct f2fs_dentry_block
*dentry_blk
;
603 unsigned long nblock
= dir_blocks(dir
);
605 for (bidx
= 0; bidx
< nblock
; bidx
++) {
607 dentry_page
= get_lock_data_page(dir
, bidx
);
608 if (IS_ERR(dentry_page
)) {
609 if (PTR_ERR(dentry_page
) == -ENOENT
)
615 kaddr
= kmap_atomic(dentry_page
);
616 dentry_blk
= (struct f2fs_dentry_block
*)kaddr
;
621 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
624 kunmap_atomic(kaddr
);
626 f2fs_put_page(dentry_page
, 1);
628 if (bit_pos
< NR_DENTRY_IN_BLOCK
)
634 static int f2fs_readdir(struct file
*file
, struct dir_context
*ctx
)
636 struct inode
*inode
= file_inode(file
);
637 unsigned long npages
= dir_blocks(inode
);
638 unsigned int bit_pos
= 0;
639 struct f2fs_dentry_block
*dentry_blk
= NULL
;
640 struct f2fs_dir_entry
*de
= NULL
;
641 struct page
*dentry_page
= NULL
;
642 struct file_ra_state
*ra
= &file
->f_ra
;
643 unsigned int n
= ((unsigned long)ctx
->pos
/ NR_DENTRY_IN_BLOCK
);
644 unsigned char d_type
= DT_UNKNOWN
;
646 bit_pos
= ((unsigned long)ctx
->pos
% NR_DENTRY_IN_BLOCK
);
648 /* readahead for multi pages of dir */
649 if (npages
- n
> 1 && !ra_has_index(ra
, n
))
650 page_cache_sync_readahead(inode
->i_mapping
, ra
, file
, n
,
651 min(npages
- n
, (pgoff_t
)MAX_DIR_RA_PAGES
));
653 for (; n
< npages
; n
++) {
654 dentry_page
= get_lock_data_page(inode
, n
);
655 if (IS_ERR(dentry_page
))
658 dentry_blk
= kmap(dentry_page
);
659 while (bit_pos
< NR_DENTRY_IN_BLOCK
) {
660 bit_pos
= find_next_bit_le(&dentry_blk
->dentry_bitmap
,
663 if (bit_pos
>= NR_DENTRY_IN_BLOCK
)
666 de
= &dentry_blk
->dentry
[bit_pos
];
667 if (de
->file_type
< F2FS_FT_MAX
)
668 d_type
= f2fs_filetype_table
[de
->file_type
];
672 dentry_blk
->filename
[bit_pos
],
673 le16_to_cpu(de
->name_len
),
674 le32_to_cpu(de
->ino
), d_type
))
677 bit_pos
+= GET_DENTRY_SLOTS(le16_to_cpu(de
->name_len
));
678 ctx
->pos
= n
* NR_DENTRY_IN_BLOCK
+ bit_pos
;
681 ctx
->pos
= (n
+ 1) * NR_DENTRY_IN_BLOCK
;
683 f2fs_put_page(dentry_page
, 1);
687 if (dentry_page
&& !IS_ERR(dentry_page
)) {
689 f2fs_put_page(dentry_page
, 1);
695 const struct file_operations f2fs_dir_operations
= {
696 .llseek
= generic_file_llseek
,
697 .read
= generic_read_dir
,
698 .iterate
= f2fs_readdir
,
699 .fsync
= f2fs_sync_file
,
700 .unlocked_ioctl
= f2fs_ioctl
,