1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/dir.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/unaligned.h>
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/filelock.h>
12 #include <linux/sched/signal.h>
13 #include <linux/unicode.h>
14 #include <linux/fserror.h>
15 #include "f2fs.h"
16 #include "node.h"
17 #include "acl.h"
18 #include "xattr.h"
19 #include <trace/events/f2fs.h>
20
f2fs_should_fallback_to_linear(struct inode * dir)21 static inline bool f2fs_should_fallback_to_linear(struct inode *dir)
22 {
23 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
24
25 switch (F2FS_OPTION(sbi).lookup_mode) {
26 case LOOKUP_PERF:
27 return false;
28 case LOOKUP_COMPAT:
29 return true;
30 case LOOKUP_AUTO:
31 return !sb_no_casefold_compat_fallback(sbi->sb);
32 }
33 return false;
34 }
35
36 #if IS_ENABLED(CONFIG_UNICODE)
37 extern struct kmem_cache *f2fs_cf_name_slab;
38 #endif
39
dir_blocks(struct inode * inode)40 static unsigned long dir_blocks(struct inode *inode)
41 {
42 return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
43 >> PAGE_SHIFT;
44 }
45
dir_buckets(unsigned int level,int dir_level)46 static unsigned int dir_buckets(unsigned int level, int dir_level)
47 {
48 if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
49 return BIT(level + dir_level);
50 else
51 return MAX_DIR_BUCKETS;
52 }
53
bucket_blocks(unsigned int level)54 static unsigned int bucket_blocks(unsigned int level)
55 {
56 if (level < MAX_DIR_HASH_DEPTH / 2)
57 return 2;
58 else
59 return 4;
60 }
61
62 #if IS_ENABLED(CONFIG_UNICODE)
63 /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
f2fs_init_casefolded_name(const struct inode * dir,struct f2fs_filename * fname)64 int f2fs_init_casefolded_name(const struct inode *dir,
65 struct f2fs_filename *fname)
66 {
67 struct super_block *sb = dir->i_sb;
68 unsigned char *buf;
69 int len;
70
71 if (IS_CASEFOLDED(dir) &&
72 !name_is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) {
73 buf = f2fs_kmem_cache_alloc(f2fs_cf_name_slab,
74 GFP_NOFS, false, F2FS_SB(sb));
75 if (!buf)
76 return -ENOMEM;
77
78 len = utf8_casefold(sb->s_encoding, fname->usr_fname,
79 buf, F2FS_NAME_LEN);
80 if (len <= 0) {
81 kmem_cache_free(f2fs_cf_name_slab, buf);
82 if (sb_has_strict_encoding(sb))
83 return -EINVAL;
84 /* fall back to treating name as opaque byte sequence */
85 return 0;
86 }
87 fname->cf_name.name = buf;
88 fname->cf_name.len = len;
89 }
90
91 return 0;
92 }
93
f2fs_free_casefolded_name(struct f2fs_filename * fname)94 void f2fs_free_casefolded_name(struct f2fs_filename *fname)
95 {
96 unsigned char *buf = (unsigned char *)fname->cf_name.name;
97
98 if (buf) {
99 kmem_cache_free(f2fs_cf_name_slab, buf);
100 fname->cf_name.name = NULL;
101 }
102 }
103 #endif /* CONFIG_UNICODE */
104
__f2fs_setup_filename(const struct inode * dir,const struct fscrypt_name * crypt_name,struct f2fs_filename * fname)105 static int __f2fs_setup_filename(const struct inode *dir,
106 const struct fscrypt_name *crypt_name,
107 struct f2fs_filename *fname)
108 {
109 int err;
110
111 memset(fname, 0, sizeof(*fname));
112
113 fname->usr_fname = crypt_name->usr_fname;
114 fname->disk_name = crypt_name->disk_name;
115 #ifdef CONFIG_FS_ENCRYPTION
116 fname->crypto_buf = crypt_name->crypto_buf;
117 #endif
118 if (crypt_name->is_nokey_name) {
119 /* hash was decoded from the no-key name */
120 fname->hash = cpu_to_le32(crypt_name->hash);
121 } else {
122 err = f2fs_init_casefolded_name(dir, fname);
123 if (err) {
124 f2fs_free_filename(fname);
125 return err;
126 }
127 f2fs_hash_filename(dir, fname);
128 }
129 return 0;
130 }
131
132 /*
133 * Prepare to search for @iname in @dir. This is similar to
134 * fscrypt_setup_filename(), but this also handles computing the casefolded name
135 * and the f2fs dirhash if needed, then packing all the information about this
136 * filename up into a 'struct f2fs_filename'.
137 */
f2fs_setup_filename(struct inode * dir,const struct qstr * iname,int lookup,struct f2fs_filename * fname)138 int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
139 int lookup, struct f2fs_filename *fname)
140 {
141 struct fscrypt_name crypt_name;
142 int err;
143
144 err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
145 if (err)
146 return err;
147
148 return __f2fs_setup_filename(dir, &crypt_name, fname);
149 }
150
151 /*
152 * Prepare to look up @dentry in @dir. This is similar to
153 * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
154 * and the f2fs dirhash if needed, then packing all the information about this
155 * filename up into a 'struct f2fs_filename'.
156 */
f2fs_prepare_lookup(struct inode * dir,struct dentry * dentry,struct f2fs_filename * fname)157 int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
158 struct f2fs_filename *fname)
159 {
160 struct fscrypt_name crypt_name;
161 int err;
162
163 err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
164 if (err)
165 return err;
166
167 return __f2fs_setup_filename(dir, &crypt_name, fname);
168 }
169
f2fs_free_filename(struct f2fs_filename * fname)170 void f2fs_free_filename(struct f2fs_filename *fname)
171 {
172 #ifdef CONFIG_FS_ENCRYPTION
173 kfree(fname->crypto_buf.name);
174 fname->crypto_buf.name = NULL;
175 #endif
176 f2fs_free_casefolded_name(fname);
177 }
178
dir_block_index(unsigned int level,int dir_level,unsigned int idx)179 static unsigned long dir_block_index(unsigned int level,
180 int dir_level, unsigned int idx)
181 {
182 unsigned long i;
183 unsigned long bidx = 0;
184
185 for (i = 0; i < level; i++)
186 bidx += mul_u32_u32(dir_buckets(i, dir_level),
187 bucket_blocks(i));
188 bidx += idx * bucket_blocks(level);
189 return bidx;
190 }
191
find_in_block(struct inode * dir,struct folio * dentry_folio,const struct f2fs_filename * fname,int * max_slots,bool use_hash)192 static struct f2fs_dir_entry *find_in_block(struct inode *dir,
193 struct folio *dentry_folio,
194 const struct f2fs_filename *fname,
195 int *max_slots,
196 bool use_hash)
197 {
198 struct f2fs_dentry_block *dentry_blk;
199 struct f2fs_dentry_ptr d;
200
201 dentry_blk = folio_address(dentry_folio);
202
203 make_dentry_ptr_block(dir, &d, dentry_blk);
204 return f2fs_find_target_dentry(&d, fname, max_slots, use_hash);
205 }
206
f2fs_match_name(const struct inode * dir,const struct f2fs_filename * fname,const u8 * de_name,u32 de_name_len)207 static inline int f2fs_match_name(const struct inode *dir,
208 const struct f2fs_filename *fname,
209 const u8 *de_name, u32 de_name_len)
210 {
211 struct fscrypt_name f;
212
213 #if IS_ENABLED(CONFIG_UNICODE)
214 if (fname->cf_name.name)
215 return generic_ci_match(dir, fname->usr_fname,
216 &fname->cf_name,
217 de_name, de_name_len);
218
219 #endif
220 f.usr_fname = fname->usr_fname;
221 f.disk_name = fname->disk_name;
222 #ifdef CONFIG_FS_ENCRYPTION
223 f.crypto_buf = fname->crypto_buf;
224 #endif
225 return fscrypt_match_name(&f, de_name, de_name_len);
226 }
227
f2fs_find_target_dentry(const struct f2fs_dentry_ptr * d,const struct f2fs_filename * fname,int * max_slots,bool use_hash)228 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
229 const struct f2fs_filename *fname, int *max_slots,
230 bool use_hash)
231 {
232 struct f2fs_dir_entry *de;
233 unsigned long bit_pos = 0;
234 int max_len = 0;
235 int res = 0;
236
237 if (max_slots)
238 *max_slots = 0;
239 while (bit_pos < d->max) {
240 if (!test_bit_le(bit_pos, d->bitmap)) {
241 bit_pos++;
242 max_len++;
243 continue;
244 }
245
246 de = &d->dentry[bit_pos];
247
248 if (unlikely(!de->name_len)) {
249 bit_pos++;
250 continue;
251 }
252
253 if (unlikely(le16_to_cpu(de->name_len) > F2FS_NAME_LEN ||
254 bit_pos + GET_DENTRY_SLOTS(le16_to_cpu(de->name_len)) >
255 d->max))
256 return ERR_PTR(-EFSCORRUPTED);
257
258 if (!use_hash || de->hash_code == fname->hash) {
259 res = f2fs_match_name(d->inode, fname,
260 d->filename[bit_pos],
261 le16_to_cpu(de->name_len));
262 if (res < 0)
263 return ERR_PTR(res);
264 if (res)
265 goto found;
266 }
267
268 if (max_slots && max_len > *max_slots)
269 *max_slots = max_len;
270 max_len = 0;
271
272 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
273 }
274
275 de = NULL;
276 found:
277 if (max_slots && max_len > *max_slots)
278 *max_slots = max_len;
279 return de;
280 }
281
find_in_level(struct inode * dir,unsigned int level,const struct f2fs_filename * fname,struct folio ** res_folio,bool use_hash)282 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
283 unsigned int level,
284 const struct f2fs_filename *fname,
285 struct folio **res_folio,
286 bool use_hash)
287 {
288 int s = GET_DENTRY_SLOTS(fname->disk_name.len);
289 unsigned int nbucket, nblock;
290 unsigned int bidx, end_block, bucket_no;
291 struct f2fs_dir_entry *de = NULL;
292 pgoff_t next_pgofs;
293 bool room = false;
294 int max_slots;
295
296 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
297 nblock = bucket_blocks(level);
298
299 bucket_no = use_hash ? le32_to_cpu(fname->hash) % nbucket : 0;
300
301 start_find_bucket:
302 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
303 bucket_no);
304 end_block = bidx + nblock;
305
306 while (bidx < end_block) {
307 /* no need to allocate new dentry pages to all the indices */
308 struct folio *dentry_folio;
309 dentry_folio = f2fs_find_data_folio(dir, bidx, &next_pgofs);
310 if (IS_ERR(dentry_folio)) {
311 if (PTR_ERR(dentry_folio) == -ENOENT) {
312 room = true;
313 bidx = next_pgofs;
314 continue;
315 } else {
316 *res_folio = dentry_folio;
317 break;
318 }
319 }
320
321 de = find_in_block(dir, dentry_folio, fname, &max_slots, use_hash);
322 if (IS_ERR(de)) {
323 f2fs_folio_put(dentry_folio, false);
324 *res_folio = ERR_CAST(de);
325 de = NULL;
326 break;
327 } else if (de) {
328 *res_folio = dentry_folio;
329 break;
330 }
331
332 if (max_slots >= s)
333 room = true;
334 f2fs_folio_put(dentry_folio, false);
335
336 bidx++;
337 }
338
339 if (de)
340 return de;
341
342 if (likely(use_hash)) {
343 if (room && F2FS_I(dir)->chash != fname->hash) {
344 F2FS_I(dir)->chash = fname->hash;
345 F2FS_I(dir)->clevel = level;
346 }
347 } else if (++bucket_no < nbucket) {
348 goto start_find_bucket;
349 }
350 return NULL;
351 }
352
__f2fs_find_entry(struct inode * dir,const struct f2fs_filename * fname,struct folio ** res_folio)353 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
354 const struct f2fs_filename *fname,
355 struct folio **res_folio)
356 {
357 unsigned long npages = dir_blocks(dir);
358 struct f2fs_dir_entry *de = NULL;
359 unsigned int max_depth;
360 unsigned int level;
361 bool use_hash = true;
362
363 *res_folio = NULL;
364
365 #if IS_ENABLED(CONFIG_UNICODE)
366 start_find_entry:
367 #endif
368 if (f2fs_has_inline_dentry(dir)) {
369 de = f2fs_find_in_inline_dir(dir, fname, res_folio, use_hash);
370 goto out;
371 }
372
373 if (npages == 0)
374 goto out;
375
376 max_depth = F2FS_I(dir)->i_current_depth;
377 if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
378 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %llu: %u",
379 dir->i_ino, max_depth);
380 max_depth = MAX_DIR_HASH_DEPTH;
381 f2fs_i_depth_write(dir, max_depth);
382 }
383
384 for (level = 0; level < max_depth; level++) {
385 de = find_in_level(dir, level, fname, res_folio, use_hash);
386 if (de || IS_ERR(*res_folio))
387 break;
388 }
389
390 out:
391 #if IS_ENABLED(CONFIG_UNICODE)
392 if (f2fs_should_fallback_to_linear(dir) &&
393 IS_CASEFOLDED(dir) && !de && use_hash) {
394 use_hash = false;
395 goto start_find_entry;
396 }
397 #endif
398 /* This is to increase the speed of f2fs_create */
399 if (!de)
400 F2FS_I(dir)->task = current;
401 return de;
402 }
403
404 /*
405 * Find an entry in the specified directory with the wanted name.
406 * It returns the page where the entry was found (as a parameter - res_page),
407 * and the entry itself. Page is returned mapped and unlocked.
408 * Entry is guaranteed to be valid.
409 */
f2fs_find_entry(struct inode * dir,const struct qstr * child,struct folio ** res_folio)410 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
411 const struct qstr *child, struct folio **res_folio)
412 {
413 struct f2fs_dir_entry *de = NULL;
414 struct f2fs_filename fname;
415 int err;
416
417 err = f2fs_setup_filename(dir, child, 1, &fname);
418 if (err) {
419 if (err == -ENOENT)
420 *res_folio = NULL;
421 else
422 *res_folio = ERR_PTR(err);
423 return NULL;
424 }
425
426 de = __f2fs_find_entry(dir, &fname, res_folio);
427
428 f2fs_free_filename(&fname);
429 return de;
430 }
431
f2fs_parent_dir(struct inode * dir,struct folio ** f)432 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct folio **f)
433 {
434 return f2fs_find_entry(dir, &dotdot_name, f);
435 }
436
f2fs_inode_by_name(struct inode * dir,const struct qstr * qstr,struct folio ** folio)437 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
438 struct folio **folio)
439 {
440 ino_t res = 0;
441 struct f2fs_dir_entry *de;
442
443 de = f2fs_find_entry(dir, qstr, folio);
444 if (de) {
445 res = le32_to_cpu(de->ino);
446 f2fs_folio_put(*folio, false);
447 }
448
449 return res;
450 }
451
f2fs_set_link(struct inode * dir,struct f2fs_dir_entry * de,struct folio * folio,struct inode * inode)452 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
453 struct folio *folio, struct inode *inode)
454 {
455 enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
456
457 folio_lock(folio);
458 f2fs_folio_wait_writeback(folio, type, true, true);
459 de->ino = cpu_to_le32(inode->i_ino);
460 de->file_type = fs_umode_to_ftype(inode->i_mode);
461 folio_mark_dirty(folio);
462
463 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
464 f2fs_mark_inode_dirty_sync(dir, true);
465 f2fs_folio_put(folio, true);
466 }
467
init_dent_inode(struct inode * dir,struct inode * inode,const struct f2fs_filename * fname,struct folio * ifolio)468 static void init_dent_inode(struct inode *dir, struct inode *inode,
469 const struct f2fs_filename *fname,
470 struct folio *ifolio)
471 {
472 struct f2fs_inode *ri;
473
474 if (!fname) /* tmpfile case? */
475 return;
476
477 f2fs_folio_wait_writeback(ifolio, NODE, true, true);
478
479 /* copy name info. to this inode folio */
480 ri = F2FS_INODE(ifolio);
481 ri->i_namelen = cpu_to_le32(fname->disk_name.len);
482 memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
483 if (IS_ENCRYPTED(dir)) {
484 file_set_enc_name(inode);
485 /*
486 * Roll-forward recovery doesn't have encryption keys available,
487 * so it can't compute the dirhash for encrypted+casefolded
488 * filenames. Append it to i_name if possible. Else, disable
489 * roll-forward recovery of the dentry (i.e., make fsync'ing the
490 * file force a checkpoint) by setting LOST_PINO.
491 */
492 if (IS_CASEFOLDED(dir)) {
493 if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
494 F2FS_NAME_LEN)
495 put_unaligned(fname->hash, (f2fs_hash_t *)
496 &ri->i_name[fname->disk_name.len]);
497 else
498 file_lost_pino(inode);
499 }
500 }
501 folio_mark_dirty(ifolio);
502 }
503
f2fs_do_make_empty_dir(struct inode * inode,struct inode * parent,struct f2fs_dentry_ptr * d)504 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
505 struct f2fs_dentry_ptr *d)
506 {
507 struct fscrypt_str dot = FSTR_INIT(".", 1);
508 struct fscrypt_str dotdot = FSTR_INIT("..", 2);
509
510 /* update dirent of "." */
511 f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
512
513 /* update dirent of ".." */
514 f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
515 }
516
make_empty_dir(struct inode * inode,struct inode * parent,struct folio * folio)517 static int make_empty_dir(struct inode *inode,
518 struct inode *parent, struct folio *folio)
519 {
520 struct folio *dentry_folio;
521 struct f2fs_dentry_block *dentry_blk;
522 struct f2fs_dentry_ptr d;
523
524 if (f2fs_has_inline_dentry(inode))
525 return f2fs_make_empty_inline_dir(inode, parent, folio);
526
527 dentry_folio = f2fs_get_new_data_folio(inode, folio, 0, true);
528 if (IS_ERR(dentry_folio))
529 return PTR_ERR(dentry_folio);
530
531 dentry_blk = folio_address(dentry_folio);
532
533 make_dentry_ptr_block(NULL, &d, dentry_blk);
534 f2fs_do_make_empty_dir(inode, parent, &d);
535
536 folio_mark_dirty(dentry_folio);
537 f2fs_folio_put(dentry_folio, true);
538 return 0;
539 }
540
f2fs_init_inode_metadata(struct inode * inode,struct inode * dir,const struct f2fs_filename * fname,struct folio * dfolio)541 struct folio *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
542 const struct f2fs_filename *fname, struct folio *dfolio)
543 {
544 struct folio *folio;
545 int err;
546
547 if (is_inode_flag_set(inode, FI_NEW_INODE)) {
548 folio = f2fs_new_inode_folio(inode);
549 if (IS_ERR(folio))
550 return folio;
551
552 if (S_ISDIR(inode->i_mode)) {
553 /* in order to handle error case */
554 folio_get(folio);
555 err = make_empty_dir(inode, dir, folio);
556 if (err) {
557 folio_lock(folio);
558 goto put_error;
559 }
560 folio_put(folio);
561 }
562
563 err = f2fs_init_acl(inode, dir, folio, dfolio);
564 if (err)
565 goto put_error;
566
567 err = f2fs_init_security(inode, dir,
568 fname ? fname->usr_fname : NULL,
569 folio);
570 if (err)
571 goto put_error;
572
573 if (IS_ENCRYPTED(inode)) {
574 err = fscrypt_set_context(inode, folio);
575 if (err)
576 goto put_error;
577 }
578 } else {
579 folio = f2fs_get_inode_folio(F2FS_I_SB(dir), inode->i_ino);
580 if (IS_ERR(folio))
581 return folio;
582 }
583
584 init_dent_inode(dir, inode, fname, folio);
585
586 /*
587 * This file should be checkpointed during fsync.
588 * We lost i_pino from now on.
589 */
590 if (is_inode_flag_set(inode, FI_INC_LINK)) {
591 if (!S_ISDIR(inode->i_mode))
592 file_lost_pino(inode);
593 /*
594 * If link the tmpfile to alias through linkat path,
595 * we should remove this inode from orphan list.
596 */
597 if (inode->i_nlink == 0)
598 f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
599 f2fs_i_links_write(inode, true);
600 }
601 return folio;
602
603 put_error:
604 clear_nlink(inode);
605 f2fs_update_inode(inode, folio);
606 f2fs_folio_put(folio, true);
607 return ERR_PTR(err);
608 }
609
f2fs_update_parent_metadata(struct inode * dir,struct inode * inode,unsigned int current_depth)610 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
611 unsigned int current_depth)
612 {
613 if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
614 if (S_ISDIR(inode->i_mode))
615 f2fs_i_links_write(dir, true);
616 clear_inode_flag(inode, FI_NEW_INODE);
617 }
618 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
619 f2fs_mark_inode_dirty_sync(dir, true);
620
621 if (F2FS_I(dir)->i_current_depth != current_depth)
622 f2fs_i_depth_write(dir, current_depth);
623
624 if (inode && is_inode_flag_set(inode, FI_INC_LINK))
625 clear_inode_flag(inode, FI_INC_LINK);
626 }
627
f2fs_room_for_filename(const void * bitmap,int slots,int max_slots)628 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
629 {
630 int bit_start = 0;
631 int zero_start, zero_end;
632 next:
633 zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
634 if (zero_start >= max_slots)
635 return max_slots;
636
637 zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
638 if (zero_end - zero_start >= slots)
639 return zero_start;
640
641 bit_start = zero_end + 1;
642
643 if (zero_end + 1 >= max_slots)
644 return max_slots;
645 goto next;
646 }
647
f2fs_has_enough_room(struct inode * dir,struct folio * ifolio,const struct f2fs_filename * fname)648 bool f2fs_has_enough_room(struct inode *dir, struct folio *ifolio,
649 const struct f2fs_filename *fname)
650 {
651 struct f2fs_dentry_ptr d;
652 unsigned int bit_pos;
653 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
654
655 make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ifolio));
656
657 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
658
659 return bit_pos < d.max;
660 }
661
f2fs_update_dentry(nid_t ino,umode_t mode,struct f2fs_dentry_ptr * d,const struct fscrypt_str * name,f2fs_hash_t name_hash,unsigned int bit_pos)662 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
663 const struct fscrypt_str *name, f2fs_hash_t name_hash,
664 unsigned int bit_pos)
665 {
666 struct f2fs_dir_entry *de;
667 int slots = GET_DENTRY_SLOTS(name->len);
668 int i;
669
670 de = &d->dentry[bit_pos];
671 de->hash_code = name_hash;
672 de->name_len = cpu_to_le16(name->len);
673 memcpy(d->filename[bit_pos], name->name, name->len);
674 de->ino = cpu_to_le32(ino);
675 de->file_type = fs_umode_to_ftype(mode);
676 for (i = 0; i < slots; i++) {
677 __set_bit_le(bit_pos + i, (void *)d->bitmap);
678 /* avoid wrong garbage data for readdir */
679 if (i)
680 (de + i)->name_len = 0;
681 }
682 }
683
f2fs_add_regular_entry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)684 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
685 struct inode *inode, nid_t ino, umode_t mode)
686 {
687 unsigned int bit_pos;
688 unsigned int level;
689 unsigned int current_depth;
690 unsigned long bidx, block;
691 unsigned int nbucket, nblock;
692 struct folio *dentry_folio = NULL;
693 struct f2fs_dentry_block *dentry_blk = NULL;
694 struct f2fs_dentry_ptr d;
695 struct folio *folio = NULL;
696 int slots, err = 0;
697
698 level = 0;
699 slots = GET_DENTRY_SLOTS(fname->disk_name.len);
700
701 current_depth = F2FS_I(dir)->i_current_depth;
702 if (F2FS_I(dir)->chash == fname->hash) {
703 level = F2FS_I(dir)->clevel;
704 F2FS_I(dir)->chash = 0;
705 }
706
707 start:
708 if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH))
709 return -ENOSPC;
710
711 if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
712 return -ENOSPC;
713
714 /* Increase the depth, if required */
715 if (level == current_depth)
716 ++current_depth;
717
718 nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
719 nblock = bucket_blocks(level);
720
721 bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
722 (le32_to_cpu(fname->hash) % nbucket));
723
724 for (block = bidx; block <= (bidx + nblock - 1); block++) {
725 dentry_folio = f2fs_get_new_data_folio(dir, NULL, block, true);
726 if (IS_ERR(dentry_folio))
727 return PTR_ERR(dentry_folio);
728
729 dentry_blk = folio_address(dentry_folio);
730 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
731 slots, NR_DENTRY_IN_BLOCK);
732 if (bit_pos < NR_DENTRY_IN_BLOCK)
733 goto add_dentry;
734
735 f2fs_folio_put(dentry_folio, true);
736 }
737
738 /* Move to next level to find the empty slot for new dentry */
739 ++level;
740 goto start;
741 add_dentry:
742 f2fs_folio_wait_writeback(dentry_folio, DATA, true, true);
743
744 if (inode) {
745 f2fs_down_write(&F2FS_I(inode)->i_sem);
746 folio = f2fs_init_inode_metadata(inode, dir, fname, NULL);
747 if (IS_ERR(folio)) {
748 err = PTR_ERR(folio);
749 goto fail;
750 }
751 }
752
753 make_dentry_ptr_block(NULL, &d, dentry_blk);
754 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
755 bit_pos);
756
757 folio_mark_dirty(dentry_folio);
758
759 if (inode) {
760 f2fs_i_pino_write(inode, dir->i_ino);
761
762 /* synchronize inode page's data from inode cache */
763 if (is_inode_flag_set(inode, FI_NEW_INODE))
764 f2fs_update_inode(inode, folio);
765
766 f2fs_folio_put(folio, true);
767 }
768
769 f2fs_update_parent_metadata(dir, inode, current_depth);
770 fail:
771 if (inode)
772 f2fs_up_write(&F2FS_I(inode)->i_sem);
773
774 f2fs_folio_put(dentry_folio, true);
775
776 return err;
777 }
778
f2fs_add_dentry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)779 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
780 struct inode *inode, nid_t ino, umode_t mode)
781 {
782 int err = -EAGAIN;
783
784 if (f2fs_has_inline_dentry(dir)) {
785 /*
786 * Should get i_xattr_sem to keep the lock order:
787 * i_xattr_sem -> inode_page lock used by f2fs_setxattr.
788 */
789 f2fs_down_read(&F2FS_I(dir)->i_xattr_sem);
790 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
791 f2fs_up_read(&F2FS_I(dir)->i_xattr_sem);
792 }
793 if (err == -EAGAIN)
794 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
795
796 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
797 return err;
798 }
799
800 /*
801 * Caller should grab and release a rwsem by calling f2fs_lock_op() and
802 * f2fs_unlock_op().
803 */
f2fs_do_add_link(struct inode * dir,const struct qstr * name,struct inode * inode,nid_t ino,umode_t mode)804 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
805 struct inode *inode, nid_t ino, umode_t mode)
806 {
807 struct f2fs_filename fname;
808 struct folio *folio = NULL;
809 struct f2fs_dir_entry *de = NULL;
810 int err;
811
812 err = f2fs_setup_filename(dir, name, 0, &fname);
813 if (err)
814 return err;
815
816 /*
817 * An immature stackable filesystem shows a race condition between lookup
818 * and create. If we have same task when doing lookup and create, it's
819 * definitely fine as expected by VFS normally. Otherwise, let's just
820 * verify on-disk dentry one more time, which guarantees filesystem
821 * consistency more.
822 */
823 if (current != F2FS_I(dir)->task) {
824 de = __f2fs_find_entry(dir, &fname, &folio);
825 F2FS_I(dir)->task = NULL;
826 }
827 if (de) {
828 f2fs_folio_put(folio, false);
829 err = -EEXIST;
830 } else if (IS_ERR(folio)) {
831 err = PTR_ERR(folio);
832 } else {
833 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
834 }
835 f2fs_free_filename(&fname);
836 return err;
837 }
838
f2fs_do_tmpfile(struct inode * inode,struct inode * dir,struct f2fs_filename * fname)839 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir,
840 struct f2fs_filename *fname)
841 {
842 struct folio *folio;
843 int err = 0;
844
845 f2fs_down_write(&F2FS_I(inode)->i_sem);
846 folio = f2fs_init_inode_metadata(inode, dir, fname, NULL);
847 if (IS_ERR(folio)) {
848 err = PTR_ERR(folio);
849 goto fail;
850 }
851 f2fs_folio_put(folio, true);
852
853 clear_inode_flag(inode, FI_NEW_INODE);
854 f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
855 fail:
856 f2fs_up_write(&F2FS_I(inode)->i_sem);
857 return err;
858 }
859
f2fs_drop_nlink(struct inode * dir,struct inode * inode)860 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
861 {
862 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
863
864 f2fs_down_write(&F2FS_I(inode)->i_sem);
865
866 if (S_ISDIR(inode->i_mode))
867 f2fs_i_links_write(dir, false);
868 inode_set_ctime_current(inode);
869
870 f2fs_i_links_write(inode, false);
871 if (S_ISDIR(inode->i_mode)) {
872 f2fs_i_links_write(inode, false);
873 f2fs_i_size_write(inode, 0);
874 }
875 f2fs_up_write(&F2FS_I(inode)->i_sem);
876
877 if (inode->i_nlink == 0)
878 f2fs_add_orphan_inode(inode);
879 else
880 f2fs_release_orphan_inode(sbi);
881 }
882
883 /*
884 * It only removes the dentry from the dentry page, corresponding name
885 * entry in name page does not need to be touched during deletion.
886 */
f2fs_delete_entry(struct f2fs_dir_entry * dentry,struct folio * folio,struct inode * dir,struct inode * inode)887 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct folio *folio,
888 struct inode *dir, struct inode *inode)
889 {
890 struct f2fs_dentry_block *dentry_blk;
891 unsigned int bit_pos;
892 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
893 pgoff_t index = folio->index;
894 int i;
895
896 f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
897
898 if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
899 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
900
901 if (f2fs_has_inline_dentry(dir))
902 return f2fs_delete_inline_entry(dentry, folio, dir, inode);
903
904 folio_lock(folio);
905 f2fs_folio_wait_writeback(folio, DATA, true, true);
906
907 dentry_blk = folio_address(folio);
908 bit_pos = dentry - dentry_blk->dentry;
909 for (i = 0; i < slots; i++)
910 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
911
912 /* Let's check and deallocate this dentry page */
913 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
914 NR_DENTRY_IN_BLOCK,
915 0);
916 folio_mark_dirty(folio);
917
918 if (bit_pos == NR_DENTRY_IN_BLOCK &&
919 !f2fs_truncate_hole(dir, index, index + 1)) {
920 f2fs_clear_page_cache_dirty_tag(folio);
921 folio_clear_dirty_for_io(folio);
922 folio_clear_uptodate(folio);
923 folio_detach_private(folio);
924
925 inode_dec_dirty_pages(dir);
926 f2fs_remove_dirty_inode(dir);
927 }
928 f2fs_folio_put(folio, true);
929
930 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
931 f2fs_mark_inode_dirty_sync(dir, true);
932
933 if (inode)
934 f2fs_drop_nlink(dir, inode);
935 }
936
f2fs_empty_dir(struct inode * dir)937 bool f2fs_empty_dir(struct inode *dir)
938 {
939 unsigned long bidx = 0;
940 unsigned int bit_pos;
941 struct f2fs_dentry_block *dentry_blk;
942 unsigned long nblock = dir_blocks(dir);
943
944 if (f2fs_has_inline_dentry(dir))
945 return f2fs_empty_inline_dir(dir);
946
947 while (bidx < nblock) {
948 pgoff_t next_pgofs;
949 struct folio *dentry_folio;
950
951 dentry_folio = f2fs_find_data_folio(dir, bidx, &next_pgofs);
952 if (IS_ERR(dentry_folio)) {
953 if (PTR_ERR(dentry_folio) == -ENOENT) {
954 bidx = next_pgofs;
955 continue;
956 } else {
957 return false;
958 }
959 }
960
961 dentry_blk = folio_address(dentry_folio);
962 if (bidx == 0)
963 bit_pos = 2;
964 else
965 bit_pos = 0;
966 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
967 NR_DENTRY_IN_BLOCK,
968 bit_pos);
969
970 f2fs_folio_put(dentry_folio, false);
971
972 if (bit_pos < NR_DENTRY_IN_BLOCK)
973 return false;
974
975 bidx++;
976 }
977 return true;
978 }
979
f2fs_fill_dentries(struct dir_context * ctx,struct f2fs_dentry_ptr * d,unsigned int start_pos,struct fscrypt_str * fstr)980 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
981 unsigned int start_pos, struct fscrypt_str *fstr)
982 {
983 unsigned char d_type = DT_UNKNOWN;
984 unsigned int bit_pos;
985 struct f2fs_dir_entry *de = NULL;
986 struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
987 struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
988 struct blk_plug plug;
989 bool readdir_ra = sbi->readdir_ra;
990 bool found_valid_dirent = false;
991 int err = 0;
992
993 bit_pos = ((unsigned long)ctx->pos % d->max);
994
995 if (readdir_ra)
996 blk_start_plug(&plug);
997
998 while (bit_pos < d->max) {
999 bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
1000 if (bit_pos >= d->max)
1001 break;
1002
1003 de = &d->dentry[bit_pos];
1004 if (de->name_len == 0) {
1005 if (found_valid_dirent || !bit_pos) {
1006 f2fs_warn_ratelimited(sbi,
1007 "invalid namelen(0), ino:%u, run fsck to fix.",
1008 le32_to_cpu(de->ino));
1009 set_sbi_flag(sbi, SBI_NEED_FSCK);
1010 }
1011 bit_pos++;
1012 ctx->pos = start_pos + bit_pos;
1013 continue;
1014 }
1015
1016 d_type = fs_ftype_to_dtype(de->file_type);
1017
1018 de_name.name = d->filename[bit_pos];
1019 de_name.len = le16_to_cpu(de->name_len);
1020
1021 /* check memory boundary before moving forward */
1022 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1023 if (unlikely(bit_pos > d->max ||
1024 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1025 f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1026 __func__, le16_to_cpu(de->name_len));
1027 set_sbi_flag(sbi, SBI_NEED_FSCK);
1028 err = -EFSCORRUPTED;
1029 f2fs_handle_error(sbi, ERROR_CORRUPTED_DIRENT);
1030 fserror_report_file_metadata(d->inode, err, GFP_NOFS);
1031 goto out;
1032 }
1033
1034 if (IS_ENCRYPTED(d->inode)) {
1035 int save_len = fstr->len;
1036
1037 err = fscrypt_fname_disk_to_usr(d->inode,
1038 (u32)le32_to_cpu(de->hash_code),
1039 0, &de_name, fstr);
1040 if (err)
1041 goto out;
1042
1043 de_name = *fstr;
1044 fstr->len = save_len;
1045 }
1046
1047 if (!dir_emit(ctx, de_name.name, de_name.len,
1048 le32_to_cpu(de->ino), d_type)) {
1049 err = 1;
1050 goto out;
1051 }
1052
1053 if (readdir_ra)
1054 f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1055
1056 ctx->pos = start_pos + bit_pos;
1057 found_valid_dirent = true;
1058 }
1059 out:
1060 if (readdir_ra)
1061 blk_finish_plug(&plug);
1062 return err;
1063 }
1064
f2fs_readdir(struct file * file,struct dir_context * ctx)1065 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1066 {
1067 struct inode *inode = file_inode(file);
1068 unsigned long npages = dir_blocks(inode);
1069 struct f2fs_dentry_block *dentry_blk = NULL;
1070 struct file_ra_state *ra = &file->f_ra;
1071 loff_t start_pos = ctx->pos;
1072 unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1073 struct f2fs_dentry_ptr d;
1074 struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1075 int err = 0;
1076
1077 if (IS_ENCRYPTED(inode)) {
1078 err = fscrypt_prepare_readdir(inode);
1079 if (err)
1080 goto out;
1081
1082 err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1083 if (err < 0)
1084 goto out;
1085 }
1086
1087 if (f2fs_has_inline_dentry(inode)) {
1088 err = f2fs_read_inline_dir(file, ctx, &fstr);
1089 goto out_free;
1090 }
1091
1092 for (; n < npages; ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1093 struct folio *dentry_folio;
1094 pgoff_t next_pgofs;
1095
1096 /* allow readdir() to be interrupted */
1097 if (fatal_signal_pending(current)) {
1098 err = -ERESTARTSYS;
1099 goto out_free;
1100 }
1101 cond_resched();
1102
1103 /* readahead for multi pages of dir */
1104 if (npages - n > 1 && !ra_has_index(ra, n))
1105 page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1106 min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1107
1108 dentry_folio = f2fs_find_data_folio(inode, n, &next_pgofs);
1109 if (IS_ERR(dentry_folio)) {
1110 err = PTR_ERR(dentry_folio);
1111 if (err == -ENOENT) {
1112 err = 0;
1113 n = next_pgofs;
1114 continue;
1115 } else {
1116 goto out_free;
1117 }
1118 }
1119
1120 dentry_blk = folio_address(dentry_folio);
1121
1122 make_dentry_ptr_block(inode, &d, dentry_blk);
1123
1124 err = f2fs_fill_dentries(ctx, &d,
1125 n * NR_DENTRY_IN_BLOCK, &fstr);
1126 f2fs_folio_put(dentry_folio, false);
1127 if (err)
1128 break;
1129
1130 n++;
1131 }
1132 out_free:
1133 fscrypt_fname_free_buffer(&fstr);
1134 out:
1135 trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1136 return err < 0 ? err : 0;
1137 }
1138
1139 const struct file_operations f2fs_dir_operations = {
1140 .llseek = generic_file_llseek,
1141 .read = generic_read_dir,
1142 .iterate_shared = f2fs_readdir,
1143 .fsync = f2fs_sync_file,
1144 .unlocked_ioctl = f2fs_ioctl,
1145 #ifdef CONFIG_COMPAT
1146 .compat_ioctl = f2fs_compat_ioctl,
1147 #endif
1148 .setlease = generic_setlease,
1149 };
1150