arm64: kgdb: Fix single-step exception handling oops
[linux/fpc-iii.git] / fs / f2fs / dir.c
blobb414892be08b799c3d2dcbe5ca31df86d2affdc1
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);
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);
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 return -ENOSPC;
561 #endif
562 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
563 return -ENOSPC;
565 /* Increase the depth, if required */
566 if (level == current_depth)
567 ++current_depth;
569 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
570 nblock = bucket_blocks(level);
572 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
573 (le32_to_cpu(dentry_hash) % nbucket));
575 for (block = bidx; block <= (bidx + nblock - 1); block++) {
576 dentry_page = get_new_data_page(dir, NULL, block, true);
577 if (IS_ERR(dentry_page))
578 return PTR_ERR(dentry_page);
580 dentry_blk = kmap(dentry_page);
581 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
582 slots, NR_DENTRY_IN_BLOCK);
583 if (bit_pos < NR_DENTRY_IN_BLOCK)
584 goto add_dentry;
586 kunmap(dentry_page);
587 f2fs_put_page(dentry_page, 1);
590 /* Move to next level to find the empty slot for new dentry */
591 ++level;
592 goto start;
593 add_dentry:
594 f2fs_wait_on_page_writeback(dentry_page, DATA, true);
596 if (inode) {
597 down_write(&F2FS_I(inode)->i_sem);
598 page = init_inode_metadata(inode, dir, new_name,
599 orig_name, NULL);
600 if (IS_ERR(page)) {
601 err = PTR_ERR(page);
602 goto fail;
604 if (f2fs_encrypted_inode(dir))
605 file_set_enc_name(inode);
608 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
609 f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
611 set_page_dirty(dentry_page);
613 if (inode) {
614 f2fs_i_pino_write(inode, dir->i_ino);
615 f2fs_put_page(page, 1);
618 update_parent_metadata(dir, inode, current_depth);
619 fail:
620 if (inode)
621 up_write(&F2FS_I(inode)->i_sem);
623 kunmap(dentry_page);
624 f2fs_put_page(dentry_page, 1);
626 return err;
629 int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
630 struct inode *inode, nid_t ino, umode_t mode)
632 struct qstr new_name;
633 int err = -EAGAIN;
635 new_name.name = fname_name(fname);
636 new_name.len = fname_len(fname);
638 if (f2fs_has_inline_dentry(dir))
639 err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
640 inode, ino, mode);
641 if (err == -EAGAIN)
642 err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
643 inode, ino, mode);
645 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
646 return err;
650 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
651 * f2fs_unlock_op().
653 int __f2fs_add_link(struct inode *dir, const struct qstr *name,
654 struct inode *inode, nid_t ino, umode_t mode)
656 struct fscrypt_name fname;
657 struct page *page = NULL;
658 struct f2fs_dir_entry *de = NULL;
659 int err;
661 err = fscrypt_setup_filename(dir, name, 0, &fname);
662 if (err)
663 return err;
666 * An immature stakable filesystem shows a race condition between lookup
667 * and create. If we have same task when doing lookup and create, it's
668 * definitely fine as expected by VFS normally. Otherwise, let's just
669 * verify on-disk dentry one more time, which guarantees filesystem
670 * consistency more.
672 if (current != F2FS_I(dir)->task) {
673 de = __f2fs_find_entry(dir, &fname, &page);
674 F2FS_I(dir)->task = NULL;
676 if (de) {
677 f2fs_dentry_kunmap(dir, page);
678 f2fs_put_page(page, 0);
679 err = -EEXIST;
680 } else if (IS_ERR(page)) {
681 err = PTR_ERR(page);
682 } else {
683 err = __f2fs_do_add_link(dir, &fname, inode, ino, mode);
685 fscrypt_free_filename(&fname);
686 return err;
689 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
691 struct page *page;
692 int err = 0;
694 down_write(&F2FS_I(inode)->i_sem);
695 page = init_inode_metadata(inode, dir, NULL, NULL, NULL);
696 if (IS_ERR(page)) {
697 err = PTR_ERR(page);
698 goto fail;
700 f2fs_put_page(page, 1);
702 clear_inode_flag(inode, FI_NEW_INODE);
703 fail:
704 up_write(&F2FS_I(inode)->i_sem);
705 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
706 return err;
709 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
711 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
713 down_write(&F2FS_I(inode)->i_sem);
715 if (S_ISDIR(inode->i_mode))
716 f2fs_i_links_write(dir, false);
717 inode->i_ctime = current_time(inode);
719 f2fs_i_links_write(inode, false);
720 if (S_ISDIR(inode->i_mode)) {
721 f2fs_i_links_write(inode, false);
722 f2fs_i_size_write(inode, 0);
724 up_write(&F2FS_I(inode)->i_sem);
726 if (inode->i_nlink == 0)
727 add_orphan_inode(inode);
728 else
729 release_orphan_inode(sbi);
733 * It only removes the dentry from the dentry page, corresponding name
734 * entry in name page does not need to be touched during deletion.
736 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
737 struct inode *dir, struct inode *inode)
739 struct f2fs_dentry_block *dentry_blk;
740 unsigned int bit_pos;
741 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
742 int i;
744 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
746 if (f2fs_has_inline_dentry(dir))
747 return f2fs_delete_inline_entry(dentry, page, dir, inode);
749 lock_page(page);
750 f2fs_wait_on_page_writeback(page, DATA, true);
752 dentry_blk = page_address(page);
753 bit_pos = dentry - dentry_blk->dentry;
754 for (i = 0; i < slots; i++)
755 clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
757 /* Let's check and deallocate this dentry page */
758 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
759 NR_DENTRY_IN_BLOCK,
761 kunmap(page); /* kunmap - pair of f2fs_find_entry */
762 set_page_dirty(page);
764 dir->i_ctime = dir->i_mtime = current_time(dir);
765 f2fs_mark_inode_dirty_sync(dir);
767 if (inode)
768 f2fs_drop_nlink(dir, inode);
770 if (bit_pos == NR_DENTRY_IN_BLOCK &&
771 !truncate_hole(dir, page->index, page->index + 1)) {
772 clear_page_dirty_for_io(page);
773 ClearPagePrivate(page);
774 ClearPageUptodate(page);
775 clear_cold_data(page);
776 inode_dec_dirty_pages(dir);
778 f2fs_put_page(page, 1);
781 bool f2fs_empty_dir(struct inode *dir)
783 unsigned long bidx;
784 struct page *dentry_page;
785 unsigned int bit_pos;
786 struct f2fs_dentry_block *dentry_blk;
787 unsigned long nblock = dir_blocks(dir);
789 if (f2fs_has_inline_dentry(dir))
790 return f2fs_empty_inline_dir(dir);
792 for (bidx = 0; bidx < nblock; bidx++) {
793 dentry_page = get_lock_data_page(dir, bidx, false);
794 if (IS_ERR(dentry_page)) {
795 if (PTR_ERR(dentry_page) == -ENOENT)
796 continue;
797 else
798 return false;
801 dentry_blk = kmap_atomic(dentry_page);
802 if (bidx == 0)
803 bit_pos = 2;
804 else
805 bit_pos = 0;
806 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
807 NR_DENTRY_IN_BLOCK,
808 bit_pos);
809 kunmap_atomic(dentry_blk);
811 f2fs_put_page(dentry_page, 1);
813 if (bit_pos < NR_DENTRY_IN_BLOCK)
814 return false;
816 return true;
819 bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
820 unsigned int start_pos, struct fscrypt_str *fstr)
822 unsigned char d_type = DT_UNKNOWN;
823 unsigned int bit_pos;
824 struct f2fs_dir_entry *de = NULL;
825 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
827 bit_pos = ((unsigned long)ctx->pos % d->max);
829 while (bit_pos < d->max) {
830 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
831 if (bit_pos >= d->max)
832 break;
834 de = &d->dentry[bit_pos];
835 if (de->name_len == 0) {
836 bit_pos++;
837 ctx->pos = start_pos + bit_pos;
838 continue;
841 d_type = get_de_type(de);
843 de_name.name = d->filename[bit_pos];
844 de_name.len = le16_to_cpu(de->name_len);
846 if (f2fs_encrypted_inode(d->inode)) {
847 int save_len = fstr->len;
848 int err;
850 err = fscrypt_fname_disk_to_usr(d->inode,
851 (u32)de->hash_code, 0,
852 &de_name, fstr);
853 if (err)
854 return true;
856 de_name = *fstr;
857 fstr->len = save_len;
860 if (!dir_emit(ctx, de_name.name, de_name.len,
861 le32_to_cpu(de->ino), d_type))
862 return true;
864 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
865 ctx->pos = start_pos + bit_pos;
867 return false;
870 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
872 struct inode *inode = file_inode(file);
873 unsigned long npages = dir_blocks(inode);
874 struct f2fs_dentry_block *dentry_blk = NULL;
875 struct page *dentry_page = NULL;
876 struct file_ra_state *ra = &file->f_ra;
877 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
878 struct f2fs_dentry_ptr d;
879 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
880 int err = 0;
882 if (f2fs_encrypted_inode(inode)) {
883 err = fscrypt_get_encryption_info(inode);
884 if (err && err != -ENOKEY)
885 return err;
887 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
888 if (err < 0)
889 return err;
892 if (f2fs_has_inline_dentry(inode)) {
893 err = f2fs_read_inline_dir(file, ctx, &fstr);
894 goto out;
897 /* readahead for multi pages of dir */
898 if (npages - n > 1 && !ra_has_index(ra, n))
899 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
900 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
902 for (; n < npages; n++) {
903 dentry_page = get_lock_data_page(inode, n, false);
904 if (IS_ERR(dentry_page)) {
905 err = PTR_ERR(dentry_page);
906 if (err == -ENOENT)
907 continue;
908 else
909 goto out;
912 dentry_blk = kmap(dentry_page);
914 make_dentry_ptr(inode, &d, (void *)dentry_blk, 1);
916 if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK, &fstr)) {
917 kunmap(dentry_page);
918 f2fs_put_page(dentry_page, 1);
919 break;
922 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
923 kunmap(dentry_page);
924 f2fs_put_page(dentry_page, 1);
926 err = 0;
927 out:
928 fscrypt_fname_free_buffer(&fstr);
929 return err;
932 static int f2fs_dir_open(struct inode *inode, struct file *filp)
934 if (f2fs_encrypted_inode(inode))
935 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
936 return 0;
939 const struct file_operations f2fs_dir_operations = {
940 .llseek = generic_file_llseek,
941 .read = generic_read_dir,
942 .iterate_shared = f2fs_readdir,
943 .fsync = f2fs_sync_file,
944 .open = f2fs_dir_open,
945 .unlocked_ioctl = f2fs_ioctl,
946 #ifdef CONFIG_COMPAT
947 .compat_ioctl = f2fs_compat_ioctl,
948 #endif