Merge tag 'blackfin-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/realm...
[linux-2.6.git] / fs / f2fs / dir.c
blob62f0d5977c64f3526e0720653fe33039c85355e3
1 /*
2 * fs/f2fs/dir.c
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.
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
13 #include "f2fs.h"
14 #include "node.h"
15 #include "acl.h"
16 #include "xattr.h"
18 static unsigned long dir_blocks(struct inode *inode)
20 return ((unsigned long long) (i_size_read(inode) + PAGE_CACHE_SIZE - 1))
21 >> PAGE_CACHE_SHIFT;
24 static unsigned int dir_buckets(unsigned int level)
26 if (level < MAX_DIR_HASH_DEPTH / 2)
27 return 1 << level;
28 else
29 return 1 << ((MAX_DIR_HASH_DEPTH / 2) - 1);
32 static unsigned int bucket_blocks(unsigned int level)
34 if (level < MAX_DIR_HASH_DEPTH / 2)
35 return 2;
36 else
37 return 4;
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 #define S_SHIFT 12
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, unsigned int idx)
70 unsigned long i;
71 unsigned long bidx = 0;
73 for (i = 0; i < level; i++)
74 bidx += dir_buckets(i) * bucket_blocks(i);
75 bidx += idx * bucket_blocks(level);
76 return bidx;
79 static bool early_match_name(const char *name, size_t namelen,
80 f2fs_hash_t namehash, struct f2fs_dir_entry *de)
82 if (le16_to_cpu(de->name_len) != namelen)
83 return false;
85 if (de->hash_code != namehash)
86 return false;
88 return true;
91 static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
92 const char *name, size_t namelen, int *max_slots,
93 f2fs_hash_t namehash, struct page **res_page)
95 struct f2fs_dir_entry *de;
96 unsigned long bit_pos, end_pos, next_pos;
97 struct f2fs_dentry_block *dentry_blk = kmap(dentry_page);
98 int slots;
100 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
101 NR_DENTRY_IN_BLOCK, 0);
102 while (bit_pos < NR_DENTRY_IN_BLOCK) {
103 de = &dentry_blk->dentry[bit_pos];
104 slots = GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
106 if (early_match_name(name, namelen, namehash, de)) {
107 if (!memcmp(dentry_blk->filename[bit_pos],
108 name, namelen)) {
109 *res_page = dentry_page;
110 goto found;
113 next_pos = bit_pos + slots;
114 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
115 NR_DENTRY_IN_BLOCK, next_pos);
116 if (bit_pos >= NR_DENTRY_IN_BLOCK)
117 end_pos = NR_DENTRY_IN_BLOCK;
118 else
119 end_pos = bit_pos;
120 if (*max_slots < end_pos - next_pos)
121 *max_slots = end_pos - next_pos;
124 de = NULL;
125 kunmap(dentry_page);
126 found:
127 return de;
130 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
131 unsigned int level, const char *name, size_t namelen,
132 f2fs_hash_t namehash, struct page **res_page)
134 int s = GET_DENTRY_SLOTS(namelen);
135 unsigned int nbucket, nblock;
136 unsigned int bidx, end_block;
137 struct page *dentry_page;
138 struct f2fs_dir_entry *de = NULL;
139 bool room = false;
140 int max_slots = 0;
142 BUG_ON(level > MAX_DIR_HASH_DEPTH);
144 nbucket = dir_buckets(level);
145 nblock = bucket_blocks(level);
147 bidx = dir_block_index(level, le32_to_cpu(namehash) % nbucket);
148 end_block = bidx + nblock;
150 for (; bidx < end_block; bidx++) {
151 /* no need to allocate new dentry pages to all the indices */
152 dentry_page = find_data_page(dir, bidx, true);
153 if (IS_ERR(dentry_page)) {
154 room = true;
155 continue;
158 de = find_in_block(dentry_page, name, namelen,
159 &max_slots, namehash, res_page);
160 if (de)
161 break;
163 if (max_slots >= s)
164 room = true;
165 f2fs_put_page(dentry_page, 0);
168 if (!de && room && F2FS_I(dir)->chash != namehash) {
169 F2FS_I(dir)->chash = namehash;
170 F2FS_I(dir)->clevel = level;
173 return de;
177 * Find an entry in the specified directory with the wanted name.
178 * It returns the page where the entry was found (as a parameter - res_page),
179 * and the entry itself. Page is returned mapped and unlocked.
180 * Entry is guaranteed to be valid.
182 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
183 struct qstr *child, struct page **res_page)
185 const char *name = child->name;
186 size_t namelen = child->len;
187 unsigned long npages = dir_blocks(dir);
188 struct f2fs_dir_entry *de = NULL;
189 f2fs_hash_t name_hash;
190 unsigned int max_depth;
191 unsigned int level;
193 if (namelen > F2FS_NAME_LEN)
194 return NULL;
196 if (npages == 0)
197 return NULL;
199 *res_page = NULL;
201 name_hash = f2fs_dentry_hash(name, namelen);
202 max_depth = F2FS_I(dir)->i_current_depth;
204 for (level = 0; level < max_depth; level++) {
205 de = find_in_level(dir, level, name,
206 namelen, name_hash, res_page);
207 if (de)
208 break;
210 if (!de && F2FS_I(dir)->chash != name_hash) {
211 F2FS_I(dir)->chash = name_hash;
212 F2FS_I(dir)->clevel = level - 1;
214 return de;
217 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
219 struct page *page;
220 struct f2fs_dir_entry *de;
221 struct f2fs_dentry_block *dentry_blk;
223 page = get_lock_data_page(dir, 0);
224 if (IS_ERR(page))
225 return NULL;
227 dentry_blk = kmap(page);
228 de = &dentry_blk->dentry[1];
229 *p = page;
230 unlock_page(page);
231 return de;
234 ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
236 ino_t res = 0;
237 struct f2fs_dir_entry *de;
238 struct page *page;
240 de = f2fs_find_entry(dir, qstr, &page);
241 if (de) {
242 res = le32_to_cpu(de->ino);
243 kunmap(page);
244 f2fs_put_page(page, 0);
247 return res;
250 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
251 struct page *page, struct inode *inode)
253 lock_page(page);
254 wait_on_page_writeback(page);
255 de->ino = cpu_to_le32(inode->i_ino);
256 set_de_type(de, inode);
257 kunmap(page);
258 set_page_dirty(page);
259 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
260 mark_inode_dirty(dir);
262 /* update parent inode number before releasing dentry page */
263 F2FS_I(inode)->i_pino = dir->i_ino;
265 f2fs_put_page(page, 1);
268 static void init_dent_inode(const struct qstr *name, struct page *ipage)
270 struct f2fs_node *rn;
272 /* copy name info. to this inode page */
273 rn = (struct f2fs_node *)page_address(ipage);
274 rn->i.i_namelen = cpu_to_le32(name->len);
275 memcpy(rn->i.i_name, name->name, name->len);
276 set_page_dirty(ipage);
279 static int make_empty_dir(struct inode *inode,
280 struct inode *parent, struct page *page)
282 struct page *dentry_page;
283 struct f2fs_dentry_block *dentry_blk;
284 struct f2fs_dir_entry *de;
285 void *kaddr;
287 dentry_page = get_new_data_page(inode, page, 0, true);
288 if (IS_ERR(dentry_page))
289 return PTR_ERR(dentry_page);
291 kaddr = kmap_atomic(dentry_page);
292 dentry_blk = (struct f2fs_dentry_block *)kaddr;
294 de = &dentry_blk->dentry[0];
295 de->name_len = cpu_to_le16(1);
296 de->hash_code = 0;
297 de->ino = cpu_to_le32(inode->i_ino);
298 memcpy(dentry_blk->filename[0], ".", 1);
299 set_de_type(de, inode);
301 de = &dentry_blk->dentry[1];
302 de->hash_code = 0;
303 de->name_len = cpu_to_le16(2);
304 de->ino = cpu_to_le32(parent->i_ino);
305 memcpy(dentry_blk->filename[1], "..", 2);
306 set_de_type(de, inode);
308 test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
309 test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
310 kunmap_atomic(kaddr);
312 set_page_dirty(dentry_page);
313 f2fs_put_page(dentry_page, 1);
314 return 0;
317 static struct page *init_inode_metadata(struct inode *inode,
318 struct inode *dir, const struct qstr *name)
320 struct page *page;
321 int err;
323 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
324 page = new_inode_page(inode, name);
325 if (IS_ERR(page))
326 return page;
328 if (S_ISDIR(inode->i_mode)) {
329 err = make_empty_dir(inode, dir, page);
330 if (err)
331 goto error;
334 err = f2fs_init_acl(inode, dir);
335 if (err)
336 goto error;
338 err = f2fs_init_security(inode, dir, name, page);
339 if (err)
340 goto error;
342 wait_on_page_writeback(page);
343 } else {
344 page = get_node_page(F2FS_SB(dir->i_sb), inode->i_ino);
345 if (IS_ERR(page))
346 return page;
348 wait_on_page_writeback(page);
349 set_cold_node(inode, page);
352 init_dent_inode(name, page);
355 * This file should be checkpointed during fsync.
356 * We lost i_pino from now on.
358 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
359 file_lost_pino(inode);
360 inc_nlink(inode);
362 return page;
364 error:
365 f2fs_put_page(page, 1);
366 remove_inode_page(inode);
367 return ERR_PTR(err);
370 static void update_parent_metadata(struct inode *dir, struct inode *inode,
371 unsigned int current_depth)
373 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
374 if (S_ISDIR(inode->i_mode)) {
375 inc_nlink(dir);
376 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
378 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
380 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
381 if (F2FS_I(dir)->i_current_depth != current_depth) {
382 F2FS_I(dir)->i_current_depth = current_depth;
383 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
386 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR))
387 update_inode_page(dir);
388 else
389 mark_inode_dirty(dir);
391 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
392 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
395 static int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots)
397 int bit_start = 0;
398 int zero_start, zero_end;
399 next:
400 zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap,
401 NR_DENTRY_IN_BLOCK,
402 bit_start);
403 if (zero_start >= NR_DENTRY_IN_BLOCK)
404 return NR_DENTRY_IN_BLOCK;
406 zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap,
407 NR_DENTRY_IN_BLOCK,
408 zero_start);
409 if (zero_end - zero_start >= slots)
410 return zero_start;
412 bit_start = zero_end + 1;
414 if (zero_end + 1 >= NR_DENTRY_IN_BLOCK)
415 return NR_DENTRY_IN_BLOCK;
416 goto next;
420 * Caller should grab and release a mutex by calling mutex_lock_op() and
421 * mutex_unlock_op().
423 int __f2fs_add_link(struct inode *dir, const struct qstr *name, struct inode *inode)
425 unsigned int bit_pos;
426 unsigned int level;
427 unsigned int current_depth;
428 unsigned long bidx, block;
429 f2fs_hash_t dentry_hash;
430 struct f2fs_dir_entry *de;
431 unsigned int nbucket, nblock;
432 size_t namelen = name->len;
433 struct page *dentry_page = NULL;
434 struct f2fs_dentry_block *dentry_blk = NULL;
435 int slots = GET_DENTRY_SLOTS(namelen);
436 struct page *page;
437 int err = 0;
438 int i;
440 dentry_hash = f2fs_dentry_hash(name->name, name->len);
441 level = 0;
442 current_depth = F2FS_I(dir)->i_current_depth;
443 if (F2FS_I(dir)->chash == dentry_hash) {
444 level = F2FS_I(dir)->clevel;
445 F2FS_I(dir)->chash = 0;
448 start:
449 if (current_depth == MAX_DIR_HASH_DEPTH)
450 return -ENOSPC;
452 /* Increase the depth, if required */
453 if (level == current_depth)
454 ++current_depth;
456 nbucket = dir_buckets(level);
457 nblock = bucket_blocks(level);
459 bidx = dir_block_index(level, (le32_to_cpu(dentry_hash) % nbucket));
461 for (block = bidx; block <= (bidx + nblock - 1); block++) {
462 dentry_page = get_new_data_page(dir, NULL, block, true);
463 if (IS_ERR(dentry_page))
464 return PTR_ERR(dentry_page);
466 dentry_blk = kmap(dentry_page);
467 bit_pos = room_for_filename(dentry_blk, slots);
468 if (bit_pos < NR_DENTRY_IN_BLOCK)
469 goto add_dentry;
471 kunmap(dentry_page);
472 f2fs_put_page(dentry_page, 1);
475 /* Move to next level to find the empty slot for new dentry */
476 ++level;
477 goto start;
478 add_dentry:
479 wait_on_page_writeback(dentry_page);
481 page = init_inode_metadata(inode, dir, name);
482 if (IS_ERR(page)) {
483 err = PTR_ERR(page);
484 goto fail;
486 de = &dentry_blk->dentry[bit_pos];
487 de->hash_code = dentry_hash;
488 de->name_len = cpu_to_le16(namelen);
489 memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
490 de->ino = cpu_to_le32(inode->i_ino);
491 set_de_type(de, inode);
492 for (i = 0; i < slots; i++)
493 test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
494 set_page_dirty(dentry_page);
496 /* we don't need to mark_inode_dirty now */
497 F2FS_I(inode)->i_pino = dir->i_ino;
498 update_inode(inode, page);
499 f2fs_put_page(page, 1);
501 update_parent_metadata(dir, inode, current_depth);
502 fail:
503 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
504 kunmap(dentry_page);
505 f2fs_put_page(dentry_page, 1);
506 return err;
510 * It only removes the dentry from the dentry page,corresponding name
511 * entry in name page does not need to be touched during deletion.
513 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
514 struct inode *inode)
516 struct f2fs_dentry_block *dentry_blk;
517 unsigned int bit_pos;
518 struct address_space *mapping = page->mapping;
519 struct inode *dir = mapping->host;
520 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
521 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
522 void *kaddr = page_address(page);
523 int i;
525 lock_page(page);
526 wait_on_page_writeback(page);
528 dentry_blk = (struct f2fs_dentry_block *)kaddr;
529 bit_pos = dentry - (struct f2fs_dir_entry *)dentry_blk->dentry;
530 for (i = 0; i < slots; i++)
531 test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
533 /* Let's check and deallocate this dentry page */
534 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
535 NR_DENTRY_IN_BLOCK,
537 kunmap(page); /* kunmap - pair of f2fs_find_entry */
538 set_page_dirty(page);
540 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
542 if (inode && S_ISDIR(inode->i_mode)) {
543 drop_nlink(dir);
544 update_inode_page(dir);
545 } else {
546 mark_inode_dirty(dir);
549 if (inode) {
550 inode->i_ctime = CURRENT_TIME;
551 drop_nlink(inode);
552 if (S_ISDIR(inode->i_mode)) {
553 drop_nlink(inode);
554 i_size_write(inode, 0);
556 update_inode_page(inode);
558 if (inode->i_nlink == 0)
559 add_orphan_inode(sbi, inode->i_ino);
562 if (bit_pos == NR_DENTRY_IN_BLOCK) {
563 truncate_hole(dir, page->index, page->index + 1);
564 clear_page_dirty_for_io(page);
565 ClearPageUptodate(page);
566 dec_page_count(sbi, F2FS_DIRTY_DENTS);
567 inode_dec_dirty_dents(dir);
569 f2fs_put_page(page, 1);
572 bool f2fs_empty_dir(struct inode *dir)
574 unsigned long bidx;
575 struct page *dentry_page;
576 unsigned int bit_pos;
577 struct f2fs_dentry_block *dentry_blk;
578 unsigned long nblock = dir_blocks(dir);
580 for (bidx = 0; bidx < nblock; bidx++) {
581 void *kaddr;
582 dentry_page = get_lock_data_page(dir, bidx);
583 if (IS_ERR(dentry_page)) {
584 if (PTR_ERR(dentry_page) == -ENOENT)
585 continue;
586 else
587 return false;
590 kaddr = kmap_atomic(dentry_page);
591 dentry_blk = (struct f2fs_dentry_block *)kaddr;
592 if (bidx == 0)
593 bit_pos = 2;
594 else
595 bit_pos = 0;
596 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
597 NR_DENTRY_IN_BLOCK,
598 bit_pos);
599 kunmap_atomic(kaddr);
601 f2fs_put_page(dentry_page, 1);
603 if (bit_pos < NR_DENTRY_IN_BLOCK)
604 return false;
606 return true;
609 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
611 struct inode *inode = file_inode(file);
612 unsigned long npages = dir_blocks(inode);
613 unsigned int bit_pos = 0;
614 struct f2fs_dentry_block *dentry_blk = NULL;
615 struct f2fs_dir_entry *de = NULL;
616 struct page *dentry_page = NULL;
617 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
618 unsigned char d_type = DT_UNKNOWN;
620 bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
622 for ( ; n < npages; n++) {
623 dentry_page = get_lock_data_page(inode, n);
624 if (IS_ERR(dentry_page))
625 continue;
627 dentry_blk = kmap(dentry_page);
628 while (bit_pos < NR_DENTRY_IN_BLOCK) {
629 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
630 NR_DENTRY_IN_BLOCK,
631 bit_pos);
632 if (bit_pos >= NR_DENTRY_IN_BLOCK)
633 break;
635 de = &dentry_blk->dentry[bit_pos];
636 if (de->file_type < F2FS_FT_MAX)
637 d_type = f2fs_filetype_table[de->file_type];
638 else
639 d_type = DT_UNKNOWN;
640 if (!dir_emit(ctx,
641 dentry_blk->filename[bit_pos],
642 le16_to_cpu(de->name_len),
643 le32_to_cpu(de->ino), d_type))
644 goto stop;
646 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
647 ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
649 bit_pos = 0;
650 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
651 kunmap(dentry_page);
652 f2fs_put_page(dentry_page, 1);
653 dentry_page = NULL;
655 stop:
656 if (dentry_page && !IS_ERR(dentry_page)) {
657 kunmap(dentry_page);
658 f2fs_put_page(dentry_page, 1);
661 return 0;
664 const struct file_operations f2fs_dir_operations = {
665 .llseek = generic_file_llseek,
666 .read = generic_read_dir,
667 .iterate = f2fs_readdir,
668 .fsync = f2fs_sync_file,
669 .unlocked_ioctl = f2fs_ioctl,