x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / fs / f2fs / dir.c
blob96e9e7fa64dde434c478060e9e8e96813b5a5bb7
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_SIZE - 1))
21 >> PAGE_SHIFT;
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);
28 else
29 return MAX_DIR_BUCKETS;
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 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];
70 return DT_UNKNOWN;
73 static unsigned long dir_block_index(unsigned int level,
74 int dir_level, unsigned int idx)
76 unsigned long i;
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);
82 return bidx;
85 static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
86 struct fscrypt_name *fname,
87 f2fs_hash_t namehash,
88 int *max_slots,
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);
99 if (de)
100 *res_page = dentry_page;
101 else
102 kunmap(dentry_page);
104 return de;
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;
113 int max_len = 0;
114 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
115 struct fscrypt_str *name = &fname->disk_name;
117 if (max_slots)
118 *max_slots = 0;
119 while (bit_pos < d->max) {
120 if (!test_bit_le(bit_pos, d->bitmap)) {
121 bit_pos++;
122 max_len++;
123 continue;
126 de = &d->dentry[bit_pos];
128 if (unlikely(!de->name_len)) {
129 bit_pos++;
130 continue;
133 if (de->hash_code != namehash)
134 goto not_match;
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))
145 goto found;
146 goto not_match;
148 name->name = fname->crypto_buf.name;
149 name->len = fname->crypto_buf.len;
151 #endif
152 if (de_name.len == name->len &&
153 !memcmp(de_name.name, name->name, name->len))
154 goto found;
155 not_match:
156 if (max_slots && max_len > *max_slots)
157 *max_slots = max_len;
158 max_len = 0;
160 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
163 de = NULL;
164 found:
165 if (max_slots && max_len > *max_slots)
166 *max_slots = max_len;
167 return de;
170 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
171 unsigned int level,
172 struct fscrypt_name *fname,
173 struct page **res_page)
175 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
176 int s = GET_DENTRY_SLOTS(name.len);
177 unsigned int nbucket, nblock;
178 unsigned int bidx, end_block;
179 struct page *dentry_page;
180 struct f2fs_dir_entry *de = NULL;
181 bool room = false;
182 int max_slots;
183 f2fs_hash_t namehash = f2fs_dentry_hash(&name, fname);
185 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
186 nblock = bucket_blocks(level);
188 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
189 le32_to_cpu(namehash) % nbucket);
190 end_block = bidx + nblock;
192 for (; bidx < end_block; bidx++) {
193 /* no need to allocate new dentry pages to all the indices */
194 dentry_page = find_data_page(dir, bidx);
195 if (IS_ERR(dentry_page)) {
196 if (PTR_ERR(dentry_page) == -ENOENT) {
197 room = true;
198 continue;
199 } else {
200 *res_page = dentry_page;
201 break;
205 de = find_in_block(dentry_page, fname, namehash, &max_slots,
206 res_page);
207 if (de)
208 break;
210 if (max_slots >= s)
211 room = true;
212 f2fs_put_page(dentry_page, 0);
215 if (!de && room && F2FS_I(dir)->chash != namehash) {
216 F2FS_I(dir)->chash = namehash;
217 F2FS_I(dir)->clevel = level;
220 return de;
223 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
224 struct fscrypt_name *fname, struct page **res_page)
226 unsigned long npages = dir_blocks(dir);
227 struct f2fs_dir_entry *de = NULL;
228 unsigned int max_depth;
229 unsigned int level;
231 if (f2fs_has_inline_dentry(dir)) {
232 *res_page = NULL;
233 de = find_in_inline_dir(dir, fname, res_page);
234 goto out;
237 if (npages == 0) {
238 *res_page = NULL;
239 goto out;
242 max_depth = F2FS_I(dir)->i_current_depth;
243 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
244 f2fs_msg(F2FS_I_SB(dir)->sb, KERN_WARNING,
245 "Corrupted max_depth of %lu: %u",
246 dir->i_ino, max_depth);
247 max_depth = MAX_DIR_HASH_DEPTH;
248 f2fs_i_depth_write(dir, max_depth);
251 for (level = 0; level < max_depth; level++) {
252 *res_page = NULL;
253 de = find_in_level(dir, level, fname, res_page);
254 if (de || IS_ERR(*res_page))
255 break;
257 out:
258 /* This is to increase the speed of f2fs_create */
259 if (!de)
260 F2FS_I(dir)->task = current;
261 return de;
265 * Find an entry in the specified directory with the wanted name.
266 * It returns the page where the entry was found (as a parameter - res_page),
267 * and the entry itself. Page is returned mapped and unlocked.
268 * Entry is guaranteed to be valid.
270 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
271 const struct qstr *child, struct page **res_page)
273 struct f2fs_dir_entry *de = NULL;
274 struct fscrypt_name fname;
275 int err;
277 err = fscrypt_setup_filename(dir, child, 1, &fname);
278 if (err) {
279 if (err == -ENOENT)
280 *res_page = NULL;
281 else
282 *res_page = ERR_PTR(err);
283 return NULL;
286 de = __f2fs_find_entry(dir, &fname, res_page);
288 fscrypt_free_filename(&fname);
289 return de;
292 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
294 struct qstr dotdot = QSTR_INIT("..", 2);
296 return f2fs_find_entry(dir, &dotdot, p);
299 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
300 struct page **page)
302 ino_t res = 0;
303 struct f2fs_dir_entry *de;
305 de = f2fs_find_entry(dir, qstr, page);
306 if (de) {
307 res = le32_to_cpu(de->ino);
308 f2fs_dentry_kunmap(dir, *page);
309 f2fs_put_page(*page, 0);
312 return res;
315 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
316 struct page *page, struct inode *inode)
318 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
319 lock_page(page);
320 f2fs_wait_on_page_writeback(page, type, true);
321 de->ino = cpu_to_le32(inode->i_ino);
322 set_de_type(de, inode->i_mode);
323 f2fs_dentry_kunmap(dir, page);
324 set_page_dirty(page);
326 dir->i_mtime = dir->i_ctime = current_time(dir);
327 f2fs_mark_inode_dirty_sync(dir, false);
328 f2fs_put_page(page, 1);
331 static void init_dent_inode(const struct qstr *name, struct page *ipage)
333 struct f2fs_inode *ri;
335 f2fs_wait_on_page_writeback(ipage, NODE, true);
337 /* copy name info. to this inode page */
338 ri = F2FS_INODE(ipage);
339 ri->i_namelen = cpu_to_le32(name->len);
340 memcpy(ri->i_name, name->name, name->len);
341 set_page_dirty(ipage);
344 int update_dent_inode(struct inode *inode, struct inode *to,
345 const struct qstr *name)
347 struct page *page;
349 if (file_enc_name(to))
350 return 0;
352 page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
353 if (IS_ERR(page))
354 return PTR_ERR(page);
356 init_dent_inode(name, page);
357 f2fs_put_page(page, 1);
359 return 0;
362 void do_make_empty_dir(struct inode *inode, struct inode *parent,
363 struct f2fs_dentry_ptr *d)
365 struct qstr dot = QSTR_INIT(".", 1);
366 struct qstr dotdot = QSTR_INIT("..", 2);
368 /* update dirent of "." */
369 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
371 /* update dirent of ".." */
372 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
375 static int make_empty_dir(struct inode *inode,
376 struct inode *parent, struct page *page)
378 struct page *dentry_page;
379 struct f2fs_dentry_block *dentry_blk;
380 struct f2fs_dentry_ptr d;
382 if (f2fs_has_inline_dentry(inode))
383 return make_empty_inline_dir(inode, parent, page);
385 dentry_page = get_new_data_page(inode, page, 0, true);
386 if (IS_ERR(dentry_page))
387 return PTR_ERR(dentry_page);
389 dentry_blk = kmap_atomic(dentry_page);
391 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
392 do_make_empty_dir(inode, parent, &d);
394 kunmap_atomic(dentry_blk);
396 set_page_dirty(dentry_page);
397 f2fs_put_page(dentry_page, 1);
398 return 0;
401 struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
402 const struct qstr *new_name, const struct qstr *orig_name,
403 struct page *dpage)
405 struct page *page;
406 int err;
408 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
409 page = new_inode_page(inode);
410 if (IS_ERR(page))
411 return page;
413 if (S_ISDIR(inode->i_mode)) {
414 /* in order to handle error case */
415 get_page(page);
416 err = make_empty_dir(inode, dir, page);
417 if (err) {
418 lock_page(page);
419 goto put_error;
421 put_page(page);
424 err = f2fs_init_acl(inode, dir, page, dpage);
425 if (err)
426 goto put_error;
428 err = f2fs_init_security(inode, dir, orig_name, page);
429 if (err)
430 goto put_error;
432 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) {
433 err = fscrypt_inherit_context(dir, inode, page, false);
434 if (err)
435 goto put_error;
437 } else {
438 page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
439 if (IS_ERR(page))
440 return page;
442 set_cold_node(inode, page);
445 if (new_name)
446 init_dent_inode(new_name, page);
449 * This file should be checkpointed during fsync.
450 * We lost i_pino from now on.
452 if (is_inode_flag_set(inode, FI_INC_LINK)) {
453 file_lost_pino(inode);
455 * If link the tmpfile to alias through linkat path,
456 * we should remove this inode from orphan list.
458 if (inode->i_nlink == 0)
459 remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
460 f2fs_i_links_write(inode, true);
462 return page;
464 put_error:
465 clear_nlink(inode);
466 update_inode(inode, page);
467 f2fs_put_page(page, 1);
468 return ERR_PTR(err);
471 void update_parent_metadata(struct inode *dir, struct inode *inode,
472 unsigned int current_depth)
474 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
475 if (S_ISDIR(inode->i_mode))
476 f2fs_i_links_write(dir, true);
477 clear_inode_flag(inode, FI_NEW_INODE);
479 dir->i_mtime = dir->i_ctime = current_time(dir);
480 f2fs_mark_inode_dirty_sync(dir, false);
482 if (F2FS_I(dir)->i_current_depth != current_depth)
483 f2fs_i_depth_write(dir, current_depth);
485 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
486 clear_inode_flag(inode, FI_INC_LINK);
489 int room_for_filename(const void *bitmap, int slots, int max_slots)
491 int bit_start = 0;
492 int zero_start, zero_end;
493 next:
494 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
495 if (zero_start >= max_slots)
496 return max_slots;
498 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
499 if (zero_end - zero_start >= slots)
500 return zero_start;
502 bit_start = zero_end + 1;
504 if (zero_end + 1 >= max_slots)
505 return max_slots;
506 goto next;
509 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
510 const struct qstr *name, f2fs_hash_t name_hash,
511 unsigned int bit_pos)
513 struct f2fs_dir_entry *de;
514 int slots = GET_DENTRY_SLOTS(name->len);
515 int i;
517 de = &d->dentry[bit_pos];
518 de->hash_code = name_hash;
519 de->name_len = cpu_to_le16(name->len);
520 memcpy(d->filename[bit_pos], name->name, name->len);
521 de->ino = cpu_to_le32(ino);
522 set_de_type(de, mode);
523 for (i = 0; i < slots; i++) {
524 __set_bit_le(bit_pos + i, (void *)d->bitmap);
525 /* avoid wrong garbage data for readdir */
526 if (i)
527 (de + i)->name_len = 0;
531 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
532 const struct qstr *orig_name,
533 struct inode *inode, nid_t ino, umode_t mode)
535 unsigned int bit_pos;
536 unsigned int level;
537 unsigned int current_depth;
538 unsigned long bidx, block;
539 f2fs_hash_t dentry_hash;
540 unsigned int nbucket, nblock;
541 struct page *dentry_page = NULL;
542 struct f2fs_dentry_block *dentry_blk = NULL;
543 struct f2fs_dentry_ptr d;
544 struct page *page = NULL;
545 int slots, err = 0;
547 level = 0;
548 slots = GET_DENTRY_SLOTS(new_name->len);
549 dentry_hash = f2fs_dentry_hash(new_name, NULL);
551 current_depth = F2FS_I(dir)->i_current_depth;
552 if (F2FS_I(dir)->chash == dentry_hash) {
553 level = F2FS_I(dir)->clevel;
554 F2FS_I(dir)->chash = 0;
557 start:
558 #ifdef CONFIG_F2FS_FAULT_INJECTION
559 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
560 f2fs_show_injection_info(FAULT_DIR_DEPTH);
561 return -ENOSPC;
563 #endif
564 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
565 return -ENOSPC;
567 /* Increase the depth, if required */
568 if (level == current_depth)
569 ++current_depth;
571 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
572 nblock = bucket_blocks(level);
574 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
575 (le32_to_cpu(dentry_hash) % nbucket));
577 for (block = bidx; block <= (bidx + nblock - 1); block++) {
578 dentry_page = get_new_data_page(dir, NULL, block, true);
579 if (IS_ERR(dentry_page))
580 return PTR_ERR(dentry_page);
582 dentry_blk = kmap(dentry_page);
583 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
584 slots, NR_DENTRY_IN_BLOCK);
585 if (bit_pos < NR_DENTRY_IN_BLOCK)
586 goto add_dentry;
588 kunmap(dentry_page);
589 f2fs_put_page(dentry_page, 1);
592 /* Move to next level to find the empty slot for new dentry */
593 ++level;
594 goto start;
595 add_dentry:
596 f2fs_wait_on_page_writeback(dentry_page, DATA, true);
598 if (inode) {
599 down_write(&F2FS_I(inode)->i_sem);
600 page = init_inode_metadata(inode, dir, new_name,
601 orig_name, NULL);
602 if (IS_ERR(page)) {
603 err = PTR_ERR(page);
604 goto fail;
606 if (f2fs_encrypted_inode(dir))
607 file_set_enc_name(inode);
610 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
611 f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
613 set_page_dirty(dentry_page);
615 if (inode) {
616 f2fs_i_pino_write(inode, dir->i_ino);
617 f2fs_put_page(page, 1);
620 update_parent_metadata(dir, inode, current_depth);
621 fail:
622 if (inode)
623 up_write(&F2FS_I(inode)->i_sem);
625 kunmap(dentry_page);
626 f2fs_put_page(dentry_page, 1);
628 return err;
631 int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
632 struct inode *inode, nid_t ino, umode_t mode)
634 struct qstr new_name;
635 int err = -EAGAIN;
637 new_name.name = fname_name(fname);
638 new_name.len = fname_len(fname);
640 if (f2fs_has_inline_dentry(dir))
641 err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
642 inode, ino, mode);
643 if (err == -EAGAIN)
644 err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
645 inode, ino, mode);
647 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
648 return err;
652 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
653 * f2fs_unlock_op().
655 int __f2fs_add_link(struct inode *dir, const struct qstr *name,
656 struct inode *inode, nid_t ino, umode_t mode)
658 struct fscrypt_name fname;
659 struct page *page = NULL;
660 struct f2fs_dir_entry *de = NULL;
661 int err;
663 err = fscrypt_setup_filename(dir, name, 0, &fname);
664 if (err)
665 return err;
668 * An immature stakable filesystem shows a race condition between lookup
669 * and create. If we have same task when doing lookup and create, it's
670 * definitely fine as expected by VFS normally. Otherwise, let's just
671 * verify on-disk dentry one more time, which guarantees filesystem
672 * consistency more.
674 if (current != F2FS_I(dir)->task) {
675 de = __f2fs_find_entry(dir, &fname, &page);
676 F2FS_I(dir)->task = NULL;
678 if (de) {
679 f2fs_dentry_kunmap(dir, page);
680 f2fs_put_page(page, 0);
681 err = -EEXIST;
682 } else if (IS_ERR(page)) {
683 err = PTR_ERR(page);
684 } else {
685 err = __f2fs_do_add_link(dir, &fname, inode, ino, mode);
687 fscrypt_free_filename(&fname);
688 return err;
691 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
693 struct page *page;
694 int err = 0;
696 down_write(&F2FS_I(inode)->i_sem);
697 page = init_inode_metadata(inode, dir, NULL, NULL, NULL);
698 if (IS_ERR(page)) {
699 err = PTR_ERR(page);
700 goto fail;
702 f2fs_put_page(page, 1);
704 clear_inode_flag(inode, FI_NEW_INODE);
705 fail:
706 up_write(&F2FS_I(inode)->i_sem);
707 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
708 return err;
711 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
713 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
715 down_write(&F2FS_I(inode)->i_sem);
717 if (S_ISDIR(inode->i_mode))
718 f2fs_i_links_write(dir, false);
719 inode->i_ctime = current_time(inode);
721 f2fs_i_links_write(inode, false);
722 if (S_ISDIR(inode->i_mode)) {
723 f2fs_i_links_write(inode, false);
724 f2fs_i_size_write(inode, 0);
726 up_write(&F2FS_I(inode)->i_sem);
728 if (inode->i_nlink == 0)
729 add_orphan_inode(inode);
730 else
731 release_orphan_inode(sbi);
735 * It only removes the dentry from the dentry page, corresponding name
736 * entry in name page does not need to be touched during deletion.
738 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
739 struct inode *dir, struct inode *inode)
741 struct f2fs_dentry_block *dentry_blk;
742 unsigned int bit_pos;
743 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
744 int i;
746 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
748 if (f2fs_has_inline_dentry(dir))
749 return f2fs_delete_inline_entry(dentry, page, dir, inode);
751 lock_page(page);
752 f2fs_wait_on_page_writeback(page, DATA, true);
754 dentry_blk = page_address(page);
755 bit_pos = dentry - dentry_blk->dentry;
756 for (i = 0; i < slots; i++)
757 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
759 /* Let's check and deallocate this dentry page */
760 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
761 NR_DENTRY_IN_BLOCK,
763 kunmap(page); /* kunmap - pair of f2fs_find_entry */
764 set_page_dirty(page);
766 dir->i_ctime = dir->i_mtime = current_time(dir);
767 f2fs_mark_inode_dirty_sync(dir, false);
769 if (inode)
770 f2fs_drop_nlink(dir, inode);
772 if (bit_pos == NR_DENTRY_IN_BLOCK &&
773 !truncate_hole(dir, page->index, page->index + 1)) {
774 clear_page_dirty_for_io(page);
775 ClearPagePrivate(page);
776 ClearPageUptodate(page);
777 inode_dec_dirty_pages(dir);
778 remove_dirty_inode(dir);
780 f2fs_put_page(page, 1);
783 bool f2fs_empty_dir(struct inode *dir)
785 unsigned long bidx;
786 struct page *dentry_page;
787 unsigned int bit_pos;
788 struct f2fs_dentry_block *dentry_blk;
789 unsigned long nblock = dir_blocks(dir);
791 if (f2fs_has_inline_dentry(dir))
792 return f2fs_empty_inline_dir(dir);
794 for (bidx = 0; bidx < nblock; bidx++) {
795 dentry_page = get_lock_data_page(dir, bidx, false);
796 if (IS_ERR(dentry_page)) {
797 if (PTR_ERR(dentry_page) == -ENOENT)
798 continue;
799 else
800 return false;
803 dentry_blk = kmap_atomic(dentry_page);
804 if (bidx == 0)
805 bit_pos = 2;
806 else
807 bit_pos = 0;
808 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
809 NR_DENTRY_IN_BLOCK,
810 bit_pos);
811 kunmap_atomic(dentry_blk);
813 f2fs_put_page(dentry_page, 1);
815 if (bit_pos < NR_DENTRY_IN_BLOCK)
816 return false;
818 return true;
821 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
822 unsigned int start_pos, struct fscrypt_str *fstr)
824 unsigned char d_type = DT_UNKNOWN;
825 unsigned int bit_pos;
826 struct f2fs_dir_entry *de = NULL;
827 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
829 bit_pos = ((unsigned long)ctx->pos % d->max);
831 while (bit_pos < d->max) {
832 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
833 if (bit_pos >= d->max)
834 break;
836 de = &d->dentry[bit_pos];
837 if (de->name_len == 0) {
838 bit_pos++;
839 ctx->pos = start_pos + bit_pos;
840 continue;
843 d_type = get_de_type(de);
845 de_name.name = d->filename[bit_pos];
846 de_name.len = le16_to_cpu(de->name_len);
848 if (f2fs_encrypted_inode(d->inode)) {
849 int save_len = fstr->len;
850 int err;
852 err = fscrypt_fname_disk_to_usr(d->inode,
853 (u32)de->hash_code, 0,
854 &de_name, fstr);
855 if (err)
856 return err;
858 de_name = *fstr;
859 fstr->len = save_len;
862 if (!dir_emit(ctx, de_name.name, de_name.len,
863 le32_to_cpu(de->ino), d_type))
864 return 1;
866 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
867 ctx->pos = start_pos + bit_pos;
869 return 0;
872 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
874 struct inode *inode = file_inode(file);
875 unsigned long npages = dir_blocks(inode);
876 struct f2fs_dentry_block *dentry_blk = NULL;
877 struct page *dentry_page = NULL;
878 struct file_ra_state *ra = &file->f_ra;
879 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
880 struct f2fs_dentry_ptr d;
881 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
882 int err = 0;
884 if (f2fs_encrypted_inode(inode)) {
885 err = fscrypt_get_encryption_info(inode);
886 if (err && err != -ENOKEY)
887 return err;
889 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
890 if (err < 0)
891 return err;
894 if (f2fs_has_inline_dentry(inode)) {
895 err = f2fs_read_inline_dir(file, ctx, &fstr);
896 goto out;
899 /* readahead for multi pages of dir */
900 if (npages - n > 1 && !ra_has_index(ra, n))
901 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
902 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
904 for (; n < npages; n++) {
905 dentry_page = get_lock_data_page(inode, n, false);
906 if (IS_ERR(dentry_page)) {
907 err = PTR_ERR(dentry_page);
908 if (err == -ENOENT) {
909 err = 0;
910 continue;
911 } else {
912 goto out;
916 dentry_blk = kmap(dentry_page);
918 make_dentry_ptr(inode, &d, (void *)dentry_blk, 1);
920 err = f2fs_fill_dentries(ctx, &d,
921 n * NR_DENTRY_IN_BLOCK, &fstr);
922 if (err) {
923 kunmap(dentry_page);
924 f2fs_put_page(dentry_page, 1);
925 break;
928 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
929 kunmap(dentry_page);
930 f2fs_put_page(dentry_page, 1);
932 out:
933 fscrypt_fname_free_buffer(&fstr);
934 return err < 0 ? err : 0;
937 static int f2fs_dir_open(struct inode *inode, struct file *filp)
939 if (f2fs_encrypted_inode(inode))
940 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
941 return 0;
944 const struct file_operations f2fs_dir_operations = {
945 .llseek = generic_file_llseek,
946 .read = generic_read_dir,
947 .iterate_shared = f2fs_readdir,
948 .fsync = f2fs_sync_file,
949 .open = f2fs_dir_open,
950 .unlocked_ioctl = f2fs_ioctl,
951 #ifdef CONFIG_COMPAT
952 .compat_ioctl = f2fs_compat_ioctl,
953 #endif