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