HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / f2fs / dir.c
blobe2ff0eb16f89ce49956032e607cf0b9636ed17cc
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, 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 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 f2fs_filename *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);
105 * For the most part, it should be a bug when name_len is zero.
106 * We stop here for figuring out where the bugs has occurred.
108 f2fs_bug_on(F2FS_P_SB(dentry_page), d.max < 0);
109 return de;
112 struct f2fs_dir_entry *find_target_dentry(struct f2fs_filename *fname,
113 f2fs_hash_t namehash, int *max_slots,
114 struct f2fs_dentry_ptr *d)
116 struct f2fs_dir_entry *de;
117 unsigned long bit_pos = 0;
118 int max_len = 0;
119 struct f2fs_str de_name = FSTR_INIT(NULL, 0);
120 struct f2fs_str *name = &fname->disk_name;
122 if (max_slots)
123 *max_slots = 0;
124 while (bit_pos < d->max) {
125 if (!test_bit_le(bit_pos, d->bitmap)) {
126 bit_pos++;
127 max_len++;
128 continue;
131 de = &d->dentry[bit_pos];
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 /* remain bug on condition */
161 if (unlikely(!de->name_len))
162 d->max = -1;
164 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
167 de = NULL;
168 found:
169 if (max_slots && max_len > *max_slots)
170 *max_slots = max_len;
171 return de;
174 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
175 unsigned int level,
176 struct f2fs_filename *fname,
177 struct page **res_page)
179 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
180 int s = GET_DENTRY_SLOTS(name.len);
181 unsigned int nbucket, nblock;
182 unsigned int bidx, end_block;
183 struct page *dentry_page;
184 struct f2fs_dir_entry *de = NULL;
185 bool room = false;
186 int max_slots;
187 f2fs_hash_t namehash;
189 namehash = f2fs_dentry_hash(&name, fname);
191 f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH);
193 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
194 nblock = bucket_blocks(level);
196 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
197 le32_to_cpu(namehash) % nbucket);
198 end_block = bidx + nblock;
200 for (; bidx < end_block; bidx++) {
201 /* no need to allocate new dentry pages to all the indices */
202 dentry_page = find_data_page(dir, bidx);
203 if (IS_ERR(dentry_page)) {
204 room = true;
205 continue;
208 de = find_in_block(dentry_page, fname, namehash, &max_slots,
209 res_page);
210 if (de)
211 break;
213 if (max_slots >= s)
214 room = true;
215 f2fs_put_page(dentry_page, 0);
218 if (!de && room && F2FS_I(dir)->chash != namehash) {
219 F2FS_I(dir)->chash = namehash;
220 F2FS_I(dir)->clevel = level;
223 return de;
227 * Find an entry in the specified directory with the wanted name.
228 * It returns the page where the entry was found (as a parameter - res_page),
229 * and the entry itself. Page is returned mapped and unlocked.
230 * Entry is guaranteed to be valid.
232 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
233 struct qstr *child, struct page **res_page)
235 unsigned long npages = dir_blocks(dir);
236 struct f2fs_dir_entry *de = NULL;
237 unsigned int max_depth;
238 unsigned int level;
239 struct f2fs_filename fname;
240 int err;
242 *res_page = NULL;
244 err = f2fs_fname_setup_filename(dir, child, 1, &fname);
245 if (err)
246 return NULL;
248 if (f2fs_has_inline_dentry(dir)) {
249 de = find_in_inline_dir(dir, &fname, res_page);
250 goto out;
253 if (npages == 0)
254 goto out;
256 max_depth = F2FS_I(dir)->i_current_depth;
258 for (level = 0; level < max_depth; level++) {
259 de = find_in_level(dir, level, &fname, res_page);
260 if (de)
261 break;
263 out:
264 f2fs_fname_free_filename(&fname);
265 return de;
268 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
270 struct page *page;
271 struct f2fs_dir_entry *de;
272 struct f2fs_dentry_block *dentry_blk;
274 if (f2fs_has_inline_dentry(dir))
275 return f2fs_parent_inline_dir(dir, p);
277 page = get_lock_data_page(dir, 0, false);
278 if (IS_ERR(page))
279 return NULL;
281 dentry_blk = kmap(page);
282 de = &dentry_blk->dentry[1];
283 *p = page;
284 unlock_page(page);
285 return de;
288 ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
290 ino_t res = 0;
291 struct f2fs_dir_entry *de;
292 struct page *page;
294 de = f2fs_find_entry(dir, qstr, &page);
295 if (de) {
296 res = le32_to_cpu(de->ino);
297 f2fs_dentry_kunmap(dir, page);
298 f2fs_put_page(page, 0);
301 return res;
304 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
305 struct page *page, struct inode *inode)
307 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
308 lock_page(page);
309 f2fs_wait_on_page_writeback(page, type);
310 de->ino = cpu_to_le32(inode->i_ino);
311 set_de_type(de, inode->i_mode);
312 f2fs_dentry_kunmap(dir, page);
313 set_page_dirty(page);
314 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
315 mark_inode_dirty(dir);
317 f2fs_put_page(page, 1);
320 static void init_dent_inode(const struct qstr *name, struct page *ipage)
322 struct f2fs_inode *ri;
324 f2fs_wait_on_page_writeback(ipage, NODE);
326 /* copy name info. to this inode page */
327 ri = F2FS_INODE(ipage);
328 ri->i_namelen = cpu_to_le32(name->len);
329 memcpy(ri->i_name, name->name, name->len);
330 set_page_dirty(ipage);
333 int update_dent_inode(struct inode *inode, struct inode *to,
334 const struct qstr *name)
336 struct page *page;
338 if (file_enc_name(to))
339 return 0;
341 page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
342 if (IS_ERR(page))
343 return PTR_ERR(page);
345 init_dent_inode(name, page);
346 f2fs_put_page(page, 1);
348 return 0;
351 void do_make_empty_dir(struct inode *inode, struct inode *parent,
352 struct f2fs_dentry_ptr *d)
354 struct f2fs_dir_entry *de;
356 de = &d->dentry[0];
357 de->name_len = cpu_to_le16(1);
358 de->hash_code = 0;
359 de->ino = cpu_to_le32(inode->i_ino);
360 memcpy(d->filename[0], ".", 1);
361 set_de_type(de, inode->i_mode);
363 de = &d->dentry[1];
364 de->hash_code = 0;
365 de->name_len = cpu_to_le16(2);
366 de->ino = cpu_to_le32(parent->i_ino);
367 memcpy(d->filename[1], "..", 2);
368 set_de_type(de, parent->i_mode);
370 test_and_set_bit_le(0, (void *)d->bitmap);
371 test_and_set_bit_le(1, (void *)d->bitmap);
374 static int make_empty_dir(struct inode *inode,
375 struct inode *parent, struct page *page)
377 struct page *dentry_page;
378 struct f2fs_dentry_block *dentry_blk;
379 struct f2fs_dentry_ptr d;
381 if (f2fs_has_inline_dentry(inode))
382 return make_empty_inline_dir(inode, parent, page);
384 dentry_page = get_new_data_page(inode, page, 0, true);
385 if (IS_ERR(dentry_page))
386 return PTR_ERR(dentry_page);
388 dentry_blk = kmap_atomic(dentry_page);
390 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
391 do_make_empty_dir(inode, parent, &d);
393 kunmap_atomic(dentry_blk);
395 set_page_dirty(dentry_page);
396 f2fs_put_page(dentry_page, 1);
397 return 0;
400 struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
401 const struct qstr *name, struct page *dpage)
403 struct page *page;
404 int err;
406 if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
407 page = new_inode_page(inode);
408 if (IS_ERR(page))
409 return page;
411 if (S_ISDIR(inode->i_mode)) {
412 err = make_empty_dir(inode, dir, page);
413 if (err)
414 goto error;
417 err = f2fs_init_acl(inode, dir, page, dpage);
418 if (err)
419 goto put_error;
421 err = f2fs_init_security(inode, dir, name, page);
422 if (err)
423 goto put_error;
425 if (f2fs_encrypted_inode(dir) && f2fs_may_encrypt(inode)) {
426 err = f2fs_inherit_context(dir, inode, page);
427 if (err)
428 goto put_error;
430 } else {
431 page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
432 if (IS_ERR(page))
433 return page;
435 set_cold_node(inode, page);
438 if (name)
439 init_dent_inode(name, page);
442 * This file should be checkpointed during fsync.
443 * We lost i_pino from now on.
445 if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
446 file_lost_pino(inode);
448 * If link the tmpfile to alias through linkat path,
449 * we should remove this inode from orphan list.
451 if (inode->i_nlink == 0)
452 remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
453 inc_nlink(inode);
455 return page;
457 put_error:
458 f2fs_put_page(page, 1);
459 error:
460 /* once the failed inode becomes a bad inode, i_mode is S_IFREG */
461 truncate_inode_pages(&inode->i_data, 0);
462 truncate_blocks(inode, 0, false);
463 remove_dirty_dir_inode(inode);
464 remove_inode_page(inode);
465 return ERR_PTR(err);
468 void update_parent_metadata(struct inode *dir, struct inode *inode,
469 unsigned int current_depth)
471 if (inode && is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
472 if (S_ISDIR(inode->i_mode)) {
473 inc_nlink(dir);
474 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
476 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
478 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
479 mark_inode_dirty(dir);
481 if (F2FS_I(dir)->i_current_depth != current_depth) {
482 F2FS_I(dir)->i_current_depth = current_depth;
483 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
486 if (inode && is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
487 clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
490 int room_for_filename(const void *bitmap, int slots, int max_slots)
492 int bit_start = 0;
493 int zero_start, zero_end;
494 next:
495 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
496 if (zero_start >= max_slots)
497 return max_slots;
499 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
500 if (zero_end - zero_start >= slots)
501 return zero_start;
503 bit_start = zero_end + 1;
505 if (zero_end + 1 >= max_slots)
506 return max_slots;
507 goto next;
510 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
511 const struct qstr *name, f2fs_hash_t name_hash,
512 unsigned int bit_pos)
514 struct f2fs_dir_entry *de;
515 int slots = GET_DENTRY_SLOTS(name->len);
516 int i;
518 de = &d->dentry[bit_pos];
519 de->hash_code = name_hash;
520 de->name_len = cpu_to_le16(name->len);
521 memcpy(d->filename[bit_pos], name->name, name->len);
522 de->ino = cpu_to_le32(ino);
523 set_de_type(de, mode);
524 for (i = 0; i < slots; i++)
525 test_and_set_bit_le(bit_pos + i, (void *)d->bitmap);
528 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
529 struct inode *inode, nid_t ino, umode_t mode)
531 unsigned int bit_pos;
532 unsigned int level;
533 unsigned int current_depth;
534 unsigned long bidx, block;
535 f2fs_hash_t dentry_hash;
536 unsigned int nbucket, nblock;
537 struct page *dentry_page = NULL;
538 struct f2fs_dentry_block *dentry_blk = NULL;
539 struct f2fs_dentry_ptr d;
540 struct page *page = NULL;
541 int slots, err = 0;
543 level = 0;
544 slots = GET_DENTRY_SLOTS(new_name->len);
545 dentry_hash = f2fs_dentry_hash(new_name, NULL);
547 current_depth = F2FS_I(dir)->i_current_depth;
548 if (F2FS_I(dir)->chash == dentry_hash) {
549 level = F2FS_I(dir)->clevel;
550 F2FS_I(dir)->chash = 0;
553 start:
554 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
555 return -ENOSPC;
557 /* Increase the depth, if required */
558 if (level == current_depth)
559 ++current_depth;
561 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
562 nblock = bucket_blocks(level);
564 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
565 (le32_to_cpu(dentry_hash) % nbucket));
567 for (block = bidx; block <= (bidx + nblock - 1); block++) {
568 dentry_page = get_new_data_page(dir, NULL, block, true);
569 if (IS_ERR(dentry_page))
570 return PTR_ERR(dentry_page);
572 dentry_blk = kmap(dentry_page);
573 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
574 slots, NR_DENTRY_IN_BLOCK);
575 if (bit_pos < NR_DENTRY_IN_BLOCK)
576 goto add_dentry;
578 kunmap(dentry_page);
579 f2fs_put_page(dentry_page, 1);
582 /* Move to next level to find the empty slot for new dentry */
583 ++level;
584 goto start;
585 add_dentry:
586 f2fs_wait_on_page_writeback(dentry_page, DATA);
588 if (inode) {
589 down_write(&F2FS_I(inode)->i_sem);
590 page = init_inode_metadata(inode, dir, new_name, NULL);
591 if (IS_ERR(page)) {
592 err = PTR_ERR(page);
593 goto fail;
595 if (f2fs_encrypted_inode(dir))
596 file_set_enc_name(inode);
599 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 1);
600 f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
602 set_page_dirty(dentry_page);
604 if (inode) {
605 /* we don't need to mark_inode_dirty now */
606 F2FS_I(inode)->i_pino = dir->i_ino;
607 update_inode(inode, page);
608 f2fs_put_page(page, 1);
611 update_parent_metadata(dir, inode, current_depth);
612 fail:
613 if (inode)
614 up_write(&F2FS_I(inode)->i_sem);
616 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
617 update_inode_page(dir);
618 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
620 kunmap(dentry_page);
621 f2fs_put_page(dentry_page, 1);
623 return err;
627 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
628 * f2fs_unlock_op().
630 int __f2fs_add_link(struct inode *dir, const struct qstr *name,
631 struct inode *inode, nid_t ino, umode_t mode)
633 struct f2fs_filename fname;
634 struct qstr new_name;
635 int err;
637 err = f2fs_fname_setup_filename(dir, name, 0, &fname);
638 if (err)
639 return err;
641 new_name.name = fname_name(&fname);
642 new_name.len = fname_len(&fname);
644 err = -EAGAIN;
645 if (f2fs_has_inline_dentry(dir))
646 err = f2fs_add_inline_entry(dir, &new_name, inode, ino, mode);
647 if (err == -EAGAIN)
648 err = f2fs_add_regular_entry(dir, &new_name, inode, ino, mode);
650 f2fs_fname_free_filename(&fname);
651 return err;
654 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
656 struct page *page;
657 int err = 0;
659 down_write(&F2FS_I(inode)->i_sem);
660 page = init_inode_metadata(inode, dir, NULL, NULL);
661 if (IS_ERR(page)) {
662 err = PTR_ERR(page);
663 goto fail;
665 /* we don't need to mark_inode_dirty now */
666 update_inode(inode, page);
667 f2fs_put_page(page, 1);
669 clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
670 fail:
671 up_write(&F2FS_I(inode)->i_sem);
672 return err;
675 void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
677 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
679 down_write(&F2FS_I(inode)->i_sem);
681 if (S_ISDIR(inode->i_mode)) {
682 drop_nlink(dir);
683 if (page)
684 update_inode(dir, page);
685 else
686 update_inode_page(dir);
688 inode->i_ctime = CURRENT_TIME;
690 drop_nlink(inode);
691 if (S_ISDIR(inode->i_mode)) {
692 drop_nlink(inode);
693 i_size_write(inode, 0);
695 up_write(&F2FS_I(inode)->i_sem);
696 update_inode_page(inode);
698 if (inode->i_nlink == 0)
699 add_orphan_inode(sbi, inode->i_ino);
700 else
701 release_orphan_inode(sbi);
705 * It only removes the dentry from the dentry page, corresponding name
706 * entry in name page does not need to be touched during deletion.
708 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
709 struct inode *dir, struct inode *inode)
711 struct f2fs_dentry_block *dentry_blk;
712 unsigned int bit_pos;
713 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
714 int i;
716 if (f2fs_has_inline_dentry(dir))
717 return f2fs_delete_inline_entry(dentry, page, dir, inode);
719 lock_page(page);
720 f2fs_wait_on_page_writeback(page, DATA);
722 dentry_blk = page_address(page);
723 bit_pos = dentry - dentry_blk->dentry;
724 for (i = 0; i < slots; i++)
725 clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
727 /* Let's check and deallocate this dentry page */
728 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
729 NR_DENTRY_IN_BLOCK,
731 kunmap(page); /* kunmap - pair of f2fs_find_entry */
732 set_page_dirty(page);
734 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
736 if (inode)
737 f2fs_drop_nlink(dir, inode, NULL);
739 if (bit_pos == NR_DENTRY_IN_BLOCK &&
740 !truncate_hole(dir, page->index, page->index + 1)) {
741 clear_page_dirty_for_io(page);
742 ClearPagePrivate(page);
743 ClearPageUptodate(page);
744 inode_dec_dirty_pages(dir);
746 f2fs_put_page(page, 1);
749 bool f2fs_empty_dir(struct inode *dir)
751 unsigned long bidx;
752 struct page *dentry_page;
753 unsigned int bit_pos;
754 struct f2fs_dentry_block *dentry_blk;
755 unsigned long nblock = dir_blocks(dir);
757 if (f2fs_has_inline_dentry(dir))
758 return f2fs_empty_inline_dir(dir);
760 for (bidx = 0; bidx < nblock; bidx++) {
761 dentry_page = get_lock_data_page(dir, bidx, false);
762 if (IS_ERR(dentry_page)) {
763 if (PTR_ERR(dentry_page) == -ENOENT)
764 continue;
765 else
766 return false;
769 dentry_blk = kmap_atomic(dentry_page);
770 if (bidx == 0)
771 bit_pos = 2;
772 else
773 bit_pos = 0;
774 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
775 NR_DENTRY_IN_BLOCK,
776 bit_pos);
777 kunmap_atomic(dentry_blk);
779 f2fs_put_page(dentry_page, 1);
781 if (bit_pos < NR_DENTRY_IN_BLOCK)
782 return false;
784 return true;
787 bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
788 unsigned int start_pos, struct f2fs_str *fstr)
790 unsigned char d_type = DT_UNKNOWN;
791 unsigned int bit_pos;
792 struct f2fs_dir_entry *de = NULL;
793 struct f2fs_str de_name = FSTR_INIT(NULL, 0);
795 bit_pos = ((unsigned long)ctx->pos % d->max);
797 while (bit_pos < d->max) {
798 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
799 if (bit_pos >= d->max)
800 break;
802 de = &d->dentry[bit_pos];
803 d_type = get_de_type(de);
805 de_name.name = d->filename[bit_pos];
806 de_name.len = le16_to_cpu(de->name_len);
808 /* check memory boundary before moving forward */
809 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
810 if (unlikely(bit_pos > d->max ||
811 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
812 f2fs_msg(F2FS_I_SB(d->inode)->sb, KERN_WARNING,
813 "%s: corrupted namelen=%d, run fsck to fix.",
814 __func__, le16_to_cpu(de->name_len));
815 set_sbi_flag(F2FS_I_SB(d->inode)->sb->s_fs_info, SBI_NEED_FSCK);
816 return -EINVAL;
819 if (f2fs_encrypted_inode(d->inode)) {
820 int save_len = fstr->len;
821 int ret;
823 de_name.name = kmalloc(de_name.len, GFP_NOFS);
824 if (!de_name.name)
825 return false;
827 memcpy(de_name.name, d->filename[bit_pos], de_name.len);
829 ret = f2fs_fname_disk_to_usr(d->inode, &de->hash_code,
830 &de_name, fstr);
831 kfree(de_name.name);
832 if (ret < 0)
833 return true;
835 de_name = *fstr;
836 fstr->len = save_len;
839 if (!dir_emit(ctx, de_name.name, de_name.len,
840 le32_to_cpu(de->ino), d_type))
841 return true;
843 ctx->pos = start_pos + bit_pos;
845 return false;
848 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
850 struct inode *inode = file_inode(file);
851 unsigned long npages = dir_blocks(inode);
852 struct f2fs_dentry_block *dentry_blk = NULL;
853 struct page *dentry_page = NULL;
854 struct file_ra_state *ra = &file->f_ra;
855 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
856 struct f2fs_dentry_ptr d;
857 struct f2fs_str fstr = FSTR_INIT(NULL, 0);
858 int err = 0;
860 if (f2fs_encrypted_inode(inode)) {
861 err = f2fs_get_encryption_info(inode);
862 if (err)
863 return err;
865 err = f2fs_fname_crypto_alloc_buffer(inode, F2FS_NAME_LEN,
866 &fstr);
867 if (err < 0)
868 return err;
871 if (f2fs_has_inline_dentry(inode)) {
872 err = f2fs_read_inline_dir(file, ctx, &fstr);
873 goto out;
876 /* readahead for multi pages of dir */
877 if (npages - n > 1 && !ra_has_index(ra, n))
878 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
879 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
881 for (; n < npages; n++) {
882 dentry_page = get_lock_data_page(inode, n, false);
883 if (IS_ERR(dentry_page))
884 continue;
886 dentry_blk = kmap(dentry_page);
888 make_dentry_ptr(inode, &d, (void *)dentry_blk, 1);
890 if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK, &fstr))
891 goto stop;
893 ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
894 kunmap(dentry_page);
895 f2fs_put_page(dentry_page, 1);
896 dentry_page = NULL;
898 stop:
899 if (dentry_page && !IS_ERR(dentry_page)) {
900 kunmap(dentry_page);
901 f2fs_put_page(dentry_page, 1);
903 out:
904 f2fs_fname_crypto_free_buffer(&fstr);
905 return err;
908 const struct file_operations f2fs_dir_operations = {
909 .llseek = generic_file_llseek,
910 .read = generic_read_dir,
911 .iterate = f2fs_readdir,
912 .fsync = f2fs_sync_file,
913 .unlocked_ioctl = f2fs_ioctl,
914 #ifdef CONFIG_COMPAT
915 .compat_ioctl = f2fs_compat_ioctl,
916 #endif