Revert "tty: hvc: Fix data abort due to race in hvc_open"
[linux/fpc-iii.git] / fs / f2fs / dir.c
blob54e90dbb09e78ec5db45b8edfcd225d9f352ab27
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/dir.c
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/sched/signal.h>
11 #include <linux/unicode.h>
12 #include "f2fs.h"
13 #include "node.h"
14 #include "acl.h"
15 #include "xattr.h"
16 #include <trace/events/f2fs.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 static 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 f2fs_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 inode *dir,
86 struct page *dentry_page,
87 struct fscrypt_name *fname,
88 f2fs_hash_t namehash,
89 int *max_slots,
90 struct page **res_page)
92 struct f2fs_dentry_block *dentry_blk;
93 struct f2fs_dir_entry *de;
94 struct f2fs_dentry_ptr d;
96 dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
98 make_dentry_ptr_block(dir, &d, dentry_blk);
99 de = f2fs_find_target_dentry(fname, namehash, max_slots, &d);
100 if (de)
101 *res_page = dentry_page;
103 return de;
106 #ifdef CONFIG_UNICODE
108 * Test whether a case-insensitive directory entry matches the filename
109 * being searched for.
111 static bool f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
112 const struct qstr *entry, bool quick)
114 const struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
115 const struct unicode_map *um = sbi->s_encoding;
116 int res;
118 if (quick)
119 res = utf8_strncasecmp_folded(um, name, entry);
120 else
121 res = utf8_strncasecmp(um, name, entry);
122 if (res < 0) {
124 * In strict mode, ignore invalid names. In non-strict mode,
125 * fall back to treating them as opaque byte sequences.
127 if (f2fs_has_strict_mode(sbi) || name->len != entry->len)
128 return false;
129 return !memcmp(name->name, entry->name, name->len);
131 return res == 0;
134 static void f2fs_fname_setup_ci_filename(struct inode *dir,
135 const struct qstr *iname,
136 struct fscrypt_str *cf_name)
138 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
140 if (!IS_CASEFOLDED(dir)) {
141 cf_name->name = NULL;
142 return;
145 cf_name->name = f2fs_kmalloc(sbi, F2FS_NAME_LEN, GFP_NOFS);
146 if (!cf_name->name)
147 return;
149 cf_name->len = utf8_casefold(sbi->s_encoding,
150 iname, cf_name->name,
151 F2FS_NAME_LEN);
152 if ((int)cf_name->len <= 0) {
153 kvfree(cf_name->name);
154 cf_name->name = NULL;
157 #endif
159 static inline bool f2fs_match_name(struct f2fs_dentry_ptr *d,
160 struct f2fs_dir_entry *de,
161 struct fscrypt_name *fname,
162 struct fscrypt_str *cf_str,
163 unsigned long bit_pos,
164 f2fs_hash_t namehash)
166 #ifdef CONFIG_UNICODE
167 struct inode *parent = d->inode;
168 struct f2fs_sb_info *sbi = F2FS_I_SB(parent);
169 struct qstr entry;
170 #endif
172 if (de->hash_code != namehash)
173 return false;
175 #ifdef CONFIG_UNICODE
176 entry.name = d->filename[bit_pos];
177 entry.len = de->name_len;
179 if (sbi->s_encoding && IS_CASEFOLDED(parent)) {
180 if (cf_str->name) {
181 struct qstr cf = {.name = cf_str->name,
182 .len = cf_str->len};
183 return f2fs_match_ci_name(parent, &cf, &entry, true);
185 return f2fs_match_ci_name(parent, fname->usr_fname, &entry,
186 false);
188 #endif
189 if (fscrypt_match_name(fname, d->filename[bit_pos],
190 le16_to_cpu(de->name_len)))
191 return true;
192 return false;
195 struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
196 f2fs_hash_t namehash, int *max_slots,
197 struct f2fs_dentry_ptr *d)
199 struct f2fs_dir_entry *de;
200 struct fscrypt_str cf_str = { .name = NULL, .len = 0 };
201 unsigned long bit_pos = 0;
202 int max_len = 0;
204 #ifdef CONFIG_UNICODE
205 f2fs_fname_setup_ci_filename(d->inode, fname->usr_fname, &cf_str);
206 #endif
208 if (max_slots)
209 *max_slots = 0;
210 while (bit_pos < d->max) {
211 if (!test_bit_le(bit_pos, d->bitmap)) {
212 bit_pos++;
213 max_len++;
214 continue;
217 de = &d->dentry[bit_pos];
219 if (unlikely(!de->name_len)) {
220 bit_pos++;
221 continue;
224 if (f2fs_match_name(d, de, fname, &cf_str, bit_pos, namehash))
225 goto found;
227 if (max_slots && max_len > *max_slots)
228 *max_slots = max_len;
229 max_len = 0;
231 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
234 de = NULL;
235 found:
236 if (max_slots && max_len > *max_slots)
237 *max_slots = max_len;
239 #ifdef CONFIG_UNICODE
240 kvfree(cf_str.name);
241 #endif
242 return de;
245 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
246 unsigned int level,
247 struct fscrypt_name *fname,
248 struct page **res_page)
250 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
251 int s = GET_DENTRY_SLOTS(name.len);
252 unsigned int nbucket, nblock;
253 unsigned int bidx, end_block;
254 struct page *dentry_page;
255 struct f2fs_dir_entry *de = NULL;
256 bool room = false;
257 int max_slots;
258 f2fs_hash_t namehash = f2fs_dentry_hash(dir, &name, fname);
260 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
261 nblock = bucket_blocks(level);
263 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
264 le32_to_cpu(namehash) % nbucket);
265 end_block = bidx + nblock;
267 for (; bidx < end_block; bidx++) {
268 /* no need to allocate new dentry pages to all the indices */
269 dentry_page = f2fs_find_data_page(dir, bidx);
270 if (IS_ERR(dentry_page)) {
271 if (PTR_ERR(dentry_page) == -ENOENT) {
272 room = true;
273 continue;
274 } else {
275 *res_page = dentry_page;
276 break;
280 de = find_in_block(dir, dentry_page, fname, namehash,
281 &max_slots, res_page);
282 if (de)
283 break;
285 if (max_slots >= s)
286 room = true;
287 f2fs_put_page(dentry_page, 0);
290 if (!de && room && F2FS_I(dir)->chash != namehash) {
291 F2FS_I(dir)->chash = namehash;
292 F2FS_I(dir)->clevel = level;
295 return de;
298 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
299 struct fscrypt_name *fname, struct page **res_page)
301 unsigned long npages = dir_blocks(dir);
302 struct f2fs_dir_entry *de = NULL;
303 unsigned int max_depth;
304 unsigned int level;
306 if (f2fs_has_inline_dentry(dir)) {
307 *res_page = NULL;
308 de = f2fs_find_in_inline_dir(dir, fname, res_page);
309 goto out;
312 if (npages == 0) {
313 *res_page = NULL;
314 goto out;
317 max_depth = F2FS_I(dir)->i_current_depth;
318 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
319 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
320 dir->i_ino, max_depth);
321 max_depth = MAX_DIR_HASH_DEPTH;
322 f2fs_i_depth_write(dir, max_depth);
325 for (level = 0; level < max_depth; level++) {
326 *res_page = NULL;
327 de = find_in_level(dir, level, fname, res_page);
328 if (de || IS_ERR(*res_page))
329 break;
331 out:
332 /* This is to increase the speed of f2fs_create */
333 if (!de)
334 F2FS_I(dir)->task = current;
335 return de;
339 * Find an entry in the specified directory with the wanted name.
340 * It returns the page where the entry was found (as a parameter - res_page),
341 * and the entry itself. Page is returned mapped and unlocked.
342 * Entry is guaranteed to be valid.
344 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
345 const struct qstr *child, struct page **res_page)
347 struct f2fs_dir_entry *de = NULL;
348 struct fscrypt_name fname;
349 int err;
351 #ifdef CONFIG_UNICODE
352 if (f2fs_has_strict_mode(F2FS_I_SB(dir)) && IS_CASEFOLDED(dir) &&
353 utf8_validate(F2FS_I_SB(dir)->s_encoding, child)) {
354 *res_page = ERR_PTR(-EINVAL);
355 return NULL;
357 #endif
359 err = fscrypt_setup_filename(dir, child, 1, &fname);
360 if (err) {
361 if (err == -ENOENT)
362 *res_page = NULL;
363 else
364 *res_page = ERR_PTR(err);
365 return NULL;
368 de = __f2fs_find_entry(dir, &fname, res_page);
370 fscrypt_free_filename(&fname);
371 return de;
374 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
376 struct qstr dotdot = QSTR_INIT("..", 2);
378 return f2fs_find_entry(dir, &dotdot, p);
381 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
382 struct page **page)
384 ino_t res = 0;
385 struct f2fs_dir_entry *de;
387 de = f2fs_find_entry(dir, qstr, page);
388 if (de) {
389 res = le32_to_cpu(de->ino);
390 f2fs_put_page(*page, 0);
393 return res;
396 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
397 struct page *page, struct inode *inode)
399 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
400 lock_page(page);
401 f2fs_wait_on_page_writeback(page, type, true, true);
402 de->ino = cpu_to_le32(inode->i_ino);
403 set_de_type(de, inode->i_mode);
404 set_page_dirty(page);
406 dir->i_mtime = dir->i_ctime = current_time(dir);
407 f2fs_mark_inode_dirty_sync(dir, false);
408 f2fs_put_page(page, 1);
411 static void init_dent_inode(const struct qstr *name, struct page *ipage)
413 struct f2fs_inode *ri;
415 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
417 /* copy name info. to this inode page */
418 ri = F2FS_INODE(ipage);
419 ri->i_namelen = cpu_to_le32(name->len);
420 memcpy(ri->i_name, name->name, name->len);
421 set_page_dirty(ipage);
424 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
425 struct f2fs_dentry_ptr *d)
427 struct qstr dot = QSTR_INIT(".", 1);
428 struct qstr dotdot = QSTR_INIT("..", 2);
430 /* update dirent of "." */
431 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
433 /* update dirent of ".." */
434 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
437 static int make_empty_dir(struct inode *inode,
438 struct inode *parent, struct page *page)
440 struct page *dentry_page;
441 struct f2fs_dentry_block *dentry_blk;
442 struct f2fs_dentry_ptr d;
444 if (f2fs_has_inline_dentry(inode))
445 return f2fs_make_empty_inline_dir(inode, parent, page);
447 dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
448 if (IS_ERR(dentry_page))
449 return PTR_ERR(dentry_page);
451 dentry_blk = page_address(dentry_page);
453 make_dentry_ptr_block(NULL, &d, dentry_blk);
454 f2fs_do_make_empty_dir(inode, parent, &d);
456 set_page_dirty(dentry_page);
457 f2fs_put_page(dentry_page, 1);
458 return 0;
461 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
462 const struct qstr *new_name, const struct qstr *orig_name,
463 struct page *dpage)
465 struct page *page;
466 int err;
468 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
469 page = f2fs_new_inode_page(inode);
470 if (IS_ERR(page))
471 return page;
473 if (S_ISDIR(inode->i_mode)) {
474 /* in order to handle error case */
475 get_page(page);
476 err = make_empty_dir(inode, dir, page);
477 if (err) {
478 lock_page(page);
479 goto put_error;
481 put_page(page);
484 err = f2fs_init_acl(inode, dir, page, dpage);
485 if (err)
486 goto put_error;
488 err = f2fs_init_security(inode, dir, orig_name, page);
489 if (err)
490 goto put_error;
492 if (IS_ENCRYPTED(inode)) {
493 err = fscrypt_inherit_context(dir, inode, page, false);
494 if (err)
495 goto put_error;
497 } else {
498 page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
499 if (IS_ERR(page))
500 return page;
503 if (new_name) {
504 init_dent_inode(new_name, page);
505 if (IS_ENCRYPTED(dir))
506 file_set_enc_name(inode);
510 * This file should be checkpointed during fsync.
511 * We lost i_pino from now on.
513 if (is_inode_flag_set(inode, FI_INC_LINK)) {
514 if (!S_ISDIR(inode->i_mode))
515 file_lost_pino(inode);
517 * If link the tmpfile to alias through linkat path,
518 * we should remove this inode from orphan list.
520 if (inode->i_nlink == 0)
521 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
522 f2fs_i_links_write(inode, true);
524 return page;
526 put_error:
527 clear_nlink(inode);
528 f2fs_update_inode(inode, page);
529 f2fs_put_page(page, 1);
530 return ERR_PTR(err);
533 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
534 unsigned int current_depth)
536 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
537 if (S_ISDIR(inode->i_mode))
538 f2fs_i_links_write(dir, true);
539 clear_inode_flag(inode, FI_NEW_INODE);
541 dir->i_mtime = dir->i_ctime = current_time(dir);
542 f2fs_mark_inode_dirty_sync(dir, false);
544 if (F2FS_I(dir)->i_current_depth != current_depth)
545 f2fs_i_depth_write(dir, current_depth);
547 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
548 clear_inode_flag(inode, FI_INC_LINK);
551 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
553 int bit_start = 0;
554 int zero_start, zero_end;
555 next:
556 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
557 if (zero_start >= max_slots)
558 return max_slots;
560 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
561 if (zero_end - zero_start >= slots)
562 return zero_start;
564 bit_start = zero_end + 1;
566 if (zero_end + 1 >= max_slots)
567 return max_slots;
568 goto next;
571 bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
572 struct fscrypt_name *fname)
574 struct f2fs_dentry_ptr d;
575 unsigned int bit_pos;
576 int slots = GET_DENTRY_SLOTS(fname_len(fname));
578 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
580 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
582 return bit_pos < d.max;
585 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
586 const struct qstr *name, f2fs_hash_t name_hash,
587 unsigned int bit_pos)
589 struct f2fs_dir_entry *de;
590 int slots = GET_DENTRY_SLOTS(name->len);
591 int i;
593 de = &d->dentry[bit_pos];
594 de->hash_code = name_hash;
595 de->name_len = cpu_to_le16(name->len);
596 memcpy(d->filename[bit_pos], name->name, name->len);
597 de->ino = cpu_to_le32(ino);
598 set_de_type(de, mode);
599 for (i = 0; i < slots; i++) {
600 __set_bit_le(bit_pos + i, (void *)d->bitmap);
601 /* avoid wrong garbage data for readdir */
602 if (i)
603 (de + i)->name_len = 0;
607 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
608 const struct qstr *orig_name,
609 struct inode *inode, nid_t ino, umode_t mode)
611 unsigned int bit_pos;
612 unsigned int level;
613 unsigned int current_depth;
614 unsigned long bidx, block;
615 f2fs_hash_t dentry_hash;
616 unsigned int nbucket, nblock;
617 struct page *dentry_page = NULL;
618 struct f2fs_dentry_block *dentry_blk = NULL;
619 struct f2fs_dentry_ptr d;
620 struct page *page = NULL;
621 int slots, err = 0;
623 level = 0;
624 slots = GET_DENTRY_SLOTS(new_name->len);
625 dentry_hash = f2fs_dentry_hash(dir, new_name, NULL);
627 current_depth = F2FS_I(dir)->i_current_depth;
628 if (F2FS_I(dir)->chash == dentry_hash) {
629 level = F2FS_I(dir)->clevel;
630 F2FS_I(dir)->chash = 0;
633 start:
634 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
635 f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
636 return -ENOSPC;
639 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
640 return -ENOSPC;
642 /* Increase the depth, if required */
643 if (level == current_depth)
644 ++current_depth;
646 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
647 nblock = bucket_blocks(level);
649 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
650 (le32_to_cpu(dentry_hash) % nbucket));
652 for (block = bidx; block <= (bidx + nblock - 1); block++) {
653 dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
654 if (IS_ERR(dentry_page))
655 return PTR_ERR(dentry_page);
657 dentry_blk = page_address(dentry_page);
658 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
659 slots, NR_DENTRY_IN_BLOCK);
660 if (bit_pos < NR_DENTRY_IN_BLOCK)
661 goto add_dentry;
663 f2fs_put_page(dentry_page, 1);
666 /* Move to next level to find the empty slot for new dentry */
667 ++level;
668 goto start;
669 add_dentry:
670 f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
672 if (inode) {
673 down_write(&F2FS_I(inode)->i_sem);
674 page = f2fs_init_inode_metadata(inode, dir, new_name,
675 orig_name, NULL);
676 if (IS_ERR(page)) {
677 err = PTR_ERR(page);
678 goto fail;
682 make_dentry_ptr_block(NULL, &d, dentry_blk);
683 f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
685 set_page_dirty(dentry_page);
687 if (inode) {
688 f2fs_i_pino_write(inode, dir->i_ino);
690 /* synchronize inode page's data from inode cache */
691 if (is_inode_flag_set(inode, FI_NEW_INODE))
692 f2fs_update_inode(inode, page);
694 f2fs_put_page(page, 1);
697 f2fs_update_parent_metadata(dir, inode, current_depth);
698 fail:
699 if (inode)
700 up_write(&F2FS_I(inode)->i_sem);
702 f2fs_put_page(dentry_page, 1);
704 return err;
707 int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
708 struct inode *inode, nid_t ino, umode_t mode)
710 struct qstr new_name;
711 int err = -EAGAIN;
713 new_name.name = fname_name(fname);
714 new_name.len = fname_len(fname);
716 if (f2fs_has_inline_dentry(dir))
717 err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
718 inode, ino, mode);
719 if (err == -EAGAIN)
720 err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
721 inode, ino, mode);
723 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
724 return err;
728 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
729 * f2fs_unlock_op().
731 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
732 struct inode *inode, nid_t ino, umode_t mode)
734 struct fscrypt_name fname;
735 struct page *page = NULL;
736 struct f2fs_dir_entry *de = NULL;
737 int err;
739 err = fscrypt_setup_filename(dir, name, 0, &fname);
740 if (err)
741 return err;
744 * An immature stakable filesystem shows a race condition between lookup
745 * and create. If we have same task when doing lookup and create, it's
746 * definitely fine as expected by VFS normally. Otherwise, let's just
747 * verify on-disk dentry one more time, which guarantees filesystem
748 * consistency more.
750 if (current != F2FS_I(dir)->task) {
751 de = __f2fs_find_entry(dir, &fname, &page);
752 F2FS_I(dir)->task = NULL;
754 if (de) {
755 f2fs_put_page(page, 0);
756 err = -EEXIST;
757 } else if (IS_ERR(page)) {
758 err = PTR_ERR(page);
759 } else {
760 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
762 fscrypt_free_filename(&fname);
763 return err;
766 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
768 struct page *page;
769 int err = 0;
771 down_write(&F2FS_I(inode)->i_sem);
772 page = f2fs_init_inode_metadata(inode, dir, NULL, NULL, NULL);
773 if (IS_ERR(page)) {
774 err = PTR_ERR(page);
775 goto fail;
777 f2fs_put_page(page, 1);
779 clear_inode_flag(inode, FI_NEW_INODE);
780 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
781 fail:
782 up_write(&F2FS_I(inode)->i_sem);
783 return err;
786 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
788 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
790 down_write(&F2FS_I(inode)->i_sem);
792 if (S_ISDIR(inode->i_mode))
793 f2fs_i_links_write(dir, false);
794 inode->i_ctime = current_time(inode);
796 f2fs_i_links_write(inode, false);
797 if (S_ISDIR(inode->i_mode)) {
798 f2fs_i_links_write(inode, false);
799 f2fs_i_size_write(inode, 0);
801 up_write(&F2FS_I(inode)->i_sem);
803 if (inode->i_nlink == 0)
804 f2fs_add_orphan_inode(inode);
805 else
806 f2fs_release_orphan_inode(sbi);
810 * It only removes the dentry from the dentry page, corresponding name
811 * entry in name page does not need to be touched during deletion.
813 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
814 struct inode *dir, struct inode *inode)
816 struct f2fs_dentry_block *dentry_blk;
817 unsigned int bit_pos;
818 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
819 int i;
821 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
823 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
824 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
826 if (f2fs_has_inline_dentry(dir))
827 return f2fs_delete_inline_entry(dentry, page, dir, inode);
829 lock_page(page);
830 f2fs_wait_on_page_writeback(page, DATA, true, true);
832 dentry_blk = page_address(page);
833 bit_pos = dentry - dentry_blk->dentry;
834 for (i = 0; i < slots; i++)
835 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
837 /* Let's check and deallocate this dentry page */
838 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
839 NR_DENTRY_IN_BLOCK,
841 set_page_dirty(page);
843 if (bit_pos == NR_DENTRY_IN_BLOCK &&
844 !f2fs_truncate_hole(dir, page->index, page->index + 1)) {
845 f2fs_clear_page_cache_dirty_tag(page);
846 clear_page_dirty_for_io(page);
847 f2fs_clear_page_private(page);
848 ClearPageUptodate(page);
849 clear_cold_data(page);
850 inode_dec_dirty_pages(dir);
851 f2fs_remove_dirty_inode(dir);
853 f2fs_put_page(page, 1);
855 dir->i_ctime = dir->i_mtime = current_time(dir);
856 f2fs_mark_inode_dirty_sync(dir, false);
858 if (inode)
859 f2fs_drop_nlink(dir, inode);
862 bool f2fs_empty_dir(struct inode *dir)
864 unsigned long bidx;
865 struct page *dentry_page;
866 unsigned int bit_pos;
867 struct f2fs_dentry_block *dentry_blk;
868 unsigned long nblock = dir_blocks(dir);
870 if (f2fs_has_inline_dentry(dir))
871 return f2fs_empty_inline_dir(dir);
873 for (bidx = 0; bidx < nblock; bidx++) {
874 dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
875 if (IS_ERR(dentry_page)) {
876 if (PTR_ERR(dentry_page) == -ENOENT)
877 continue;
878 else
879 return false;
882 dentry_blk = page_address(dentry_page);
883 if (bidx == 0)
884 bit_pos = 2;
885 else
886 bit_pos = 0;
887 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
888 NR_DENTRY_IN_BLOCK,
889 bit_pos);
891 f2fs_put_page(dentry_page, 1);
893 if (bit_pos < NR_DENTRY_IN_BLOCK)
894 return false;
896 return true;
899 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
900 unsigned int start_pos, struct fscrypt_str *fstr)
902 unsigned char d_type = DT_UNKNOWN;
903 unsigned int bit_pos;
904 struct f2fs_dir_entry *de = NULL;
905 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
906 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
907 struct blk_plug plug;
908 bool readdir_ra = sbi->readdir_ra == 1;
909 int err = 0;
911 bit_pos = ((unsigned long)ctx->pos % d->max);
913 if (readdir_ra)
914 blk_start_plug(&plug);
916 while (bit_pos < d->max) {
917 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
918 if (bit_pos >= d->max)
919 break;
921 de = &d->dentry[bit_pos];
922 if (de->name_len == 0) {
923 bit_pos++;
924 ctx->pos = start_pos + bit_pos;
925 printk_ratelimited(
926 "%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
927 KERN_WARNING, sbi->sb->s_id,
928 le32_to_cpu(de->ino));
929 set_sbi_flag(sbi, SBI_NEED_FSCK);
930 continue;
933 d_type = f2fs_get_de_type(de);
935 de_name.name = d->filename[bit_pos];
936 de_name.len = le16_to_cpu(de->name_len);
938 /* check memory boundary before moving forward */
939 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
940 if (unlikely(bit_pos > d->max ||
941 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
942 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
943 __func__, le16_to_cpu(de->name_len));
944 set_sbi_flag(sbi, SBI_NEED_FSCK);
945 err = -EFSCORRUPTED;
946 goto out;
949 if (IS_ENCRYPTED(d->inode)) {
950 int save_len = fstr->len;
952 err = fscrypt_fname_disk_to_usr(d->inode,
953 (u32)le32_to_cpu(de->hash_code),
954 0, &de_name, fstr);
955 if (err)
956 goto out;
958 de_name = *fstr;
959 fstr->len = save_len;
962 if (!dir_emit(ctx, de_name.name, de_name.len,
963 le32_to_cpu(de->ino), d_type)) {
964 err = 1;
965 goto out;
968 if (readdir_ra)
969 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
971 ctx->pos = start_pos + bit_pos;
973 out:
974 if (readdir_ra)
975 blk_finish_plug(&plug);
976 return err;
979 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
981 struct inode *inode = file_inode(file);
982 unsigned long npages = dir_blocks(inode);
983 struct f2fs_dentry_block *dentry_blk = NULL;
984 struct page *dentry_page = NULL;
985 struct file_ra_state *ra = &file->f_ra;
986 loff_t start_pos = ctx->pos;
987 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
988 struct f2fs_dentry_ptr d;
989 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
990 int err = 0;
992 if (IS_ENCRYPTED(inode)) {
993 err = fscrypt_get_encryption_info(inode);
994 if (err)
995 goto out;
997 err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
998 if (err < 0)
999 goto out;
1002 if (f2fs_has_inline_dentry(inode)) {
1003 err = f2fs_read_inline_dir(file, ctx, &fstr);
1004 goto out_free;
1007 for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1009 /* allow readdir() to be interrupted */
1010 if (fatal_signal_pending(current)) {
1011 err = -ERESTARTSYS;
1012 goto out_free;
1014 cond_resched();
1016 /* readahead for multi pages of dir */
1017 if (npages - n > 1 && !ra_has_index(ra, n))
1018 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1019 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1021 dentry_page = f2fs_find_data_page(inode, n);
1022 if (IS_ERR(dentry_page)) {
1023 err = PTR_ERR(dentry_page);
1024 if (err == -ENOENT) {
1025 err = 0;
1026 continue;
1027 } else {
1028 goto out_free;
1032 dentry_blk = page_address(dentry_page);
1034 make_dentry_ptr_block(inode, &d, dentry_blk);
1036 err = f2fs_fill_dentries(ctx, &d,
1037 n * NR_DENTRY_IN_BLOCK, &fstr);
1038 if (err) {
1039 f2fs_put_page(dentry_page, 0);
1040 break;
1043 f2fs_put_page(dentry_page, 0);
1045 out_free:
1046 fscrypt_fname_free_buffer(&fstr);
1047 out:
1048 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1049 return err < 0 ? err : 0;
1052 static int f2fs_dir_open(struct inode *inode, struct file *filp)
1054 if (IS_ENCRYPTED(inode))
1055 return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
1056 return 0;
1059 const struct file_operations f2fs_dir_operations = {
1060 .llseek = generic_file_llseek,
1061 .read = generic_read_dir,
1062 .iterate_shared = f2fs_readdir,
1063 .fsync = f2fs_sync_file,
1064 .open = f2fs_dir_open,
1065 .unlocked_ioctl = f2fs_ioctl,
1066 #ifdef CONFIG_COMPAT
1067 .compat_ioctl = f2fs_compat_ioctl,
1068 #endif
1071 #ifdef CONFIG_UNICODE
1072 static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
1073 const char *str, const struct qstr *name)
1075 const struct dentry *parent = READ_ONCE(dentry->d_parent);
1076 const struct inode *dir = READ_ONCE(parent->d_inode);
1077 const struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1078 struct qstr entry = QSTR_INIT(str, len);
1079 char strbuf[DNAME_INLINE_LEN];
1080 int res;
1082 if (!dir || !IS_CASEFOLDED(dir))
1083 goto fallback;
1086 * If the dentry name is stored in-line, then it may be concurrently
1087 * modified by a rename. If this happens, the VFS will eventually retry
1088 * the lookup, so it doesn't matter what ->d_compare() returns.
1089 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1090 * string. Therefore, we have to copy the name into a temporary buffer.
1092 if (len <= DNAME_INLINE_LEN - 1) {
1093 memcpy(strbuf, str, len);
1094 strbuf[len] = 0;
1095 entry.name = strbuf;
1096 /* prevent compiler from optimizing out the temporary buffer */
1097 barrier();
1100 res = utf8_strncasecmp(sbi->s_encoding, name, &entry);
1101 if (res >= 0)
1102 return res;
1104 if (f2fs_has_strict_mode(sbi))
1105 return -EINVAL;
1106 fallback:
1107 if (len != name->len)
1108 return 1;
1109 return !!memcmp(str, name->name, len);
1112 static int f2fs_d_hash(const struct dentry *dentry, struct qstr *str)
1114 struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1115 const struct unicode_map *um = sbi->s_encoding;
1116 const struct inode *inode = READ_ONCE(dentry->d_inode);
1117 unsigned char *norm;
1118 int len, ret = 0;
1120 if (!inode || !IS_CASEFOLDED(inode))
1121 return 0;
1123 norm = f2fs_kmalloc(sbi, PATH_MAX, GFP_ATOMIC);
1124 if (!norm)
1125 return -ENOMEM;
1127 len = utf8_casefold(um, str, norm, PATH_MAX);
1128 if (len < 0) {
1129 if (f2fs_has_strict_mode(sbi))
1130 ret = -EINVAL;
1131 goto out;
1133 str->hash = full_name_hash(dentry, norm, len);
1134 out:
1135 kvfree(norm);
1136 return ret;
1139 const struct dentry_operations f2fs_dentry_ops = {
1140 .d_hash = f2fs_d_hash,
1141 .d_compare = f2fs_d_compare,
1143 #endif