c36b3b22bfffd9403a29d55001620514f0bc11a2
[linux-2.6-block.git] / fs / f2fs / dir.c
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/sched/signal.h>
12 #include <linux/unicode.h>
13 #include "f2fs.h"
14 #include "node.h"
15 #include "acl.h"
16 #include "xattr.h"
17 #include <trace/events/f2fs.h>
18
19 #if IS_ENABLED(CONFIG_UNICODE)
20 extern struct kmem_cache *f2fs_cf_name_slab;
21 #endif
22
23 static unsigned long dir_blocks(struct inode *inode)
24 {
25         return ((unsigned long long) (i_size_read(inode) + PAGE_SIZE - 1))
26                                                         >> PAGE_SHIFT;
27 }
28
29 static unsigned int dir_buckets(unsigned int level, int dir_level)
30 {
31         if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
32                 return BIT(level + dir_level);
33         else
34                 return MAX_DIR_BUCKETS;
35 }
36
37 static 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
45 #if IS_ENABLED(CONFIG_UNICODE)
46 /* If @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
47 int f2fs_init_casefolded_name(const struct inode *dir,
48                               struct f2fs_filename *fname)
49 {
50         struct super_block *sb = dir->i_sb;
51         unsigned char *buf;
52         int len;
53
54         if (IS_CASEFOLDED(dir) &&
55             !is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) {
56                 buf = f2fs_kmem_cache_alloc(f2fs_cf_name_slab,
57                                             GFP_NOFS, false, F2FS_SB(sb));
58                 if (!buf)
59                         return -ENOMEM;
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);
65                         if (sb_has_strict_encoding(sb))
66                                 return -EINVAL;
67                         /* fall back to treating name as opaque byte sequence */
68                         return 0;
69                 }
70                 fname->cf_name.name = buf;
71                 fname->cf_name.len = len;
72         }
73
74         return 0;
75 }
76
77 void 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
88 static 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
101         if (crypt_name->is_nokey_name) {
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  */
121 int 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  */
140 int 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
153 void 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
159         f2fs_free_casefolded_name(fname);
160 }
161
162 static unsigned long dir_block_index(unsigned int level,
163                                 int dir_level, unsigned int idx)
164 {
165         unsigned long i;
166         unsigned long bidx = 0;
167
168         for (i = 0; i < level; i++)
169                 bidx += mul_u32_u32(dir_buckets(i, dir_level),
170                                     bucket_blocks(i));
171         bidx += idx * bucket_blocks(level);
172         return bidx;
173 }
174
175 static struct f2fs_dir_entry *find_in_block(struct inode *dir,
176                                 struct folio *dentry_folio,
177                                 const struct f2fs_filename *fname,
178                                 int *max_slots,
179                                 bool use_hash)
180 {
181         struct f2fs_dentry_block *dentry_blk;
182         struct f2fs_dentry_ptr d;
183
184         dentry_blk = folio_address(dentry_folio);
185
186         make_dentry_ptr_block(dir, &d, dentry_blk);
187         return f2fs_find_target_dentry(&d, fname, max_slots, use_hash);
188 }
189
190 static inline int f2fs_match_name(const struct inode *dir,
191                                    const struct f2fs_filename *fname,
192                                    const u8 *de_name, u32 de_name_len)
193 {
194         struct fscrypt_name f;
195
196 #if IS_ENABLED(CONFIG_UNICODE)
197         if (fname->cf_name.name)
198                 return generic_ci_match(dir, fname->usr_fname,
199                                         &fname->cf_name,
200                                         de_name, de_name_len);
201
202 #endif
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);
209 }
210
211 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
212                         const struct f2fs_filename *fname, int *max_slots,
213                         bool use_hash)
214 {
215         struct f2fs_dir_entry *de;
216         unsigned long bit_pos = 0;
217         int max_len = 0;
218         int res = 0;
219
220         if (max_slots)
221                 *max_slots = 0;
222         while (bit_pos < d->max) {
223                 if (!test_bit_le(bit_pos, d->bitmap)) {
224                         bit_pos++;
225                         max_len++;
226                         continue;
227                 }
228
229                 de = &d->dentry[bit_pos];
230
231                 if (unlikely(!de->name_len)) {
232                         bit_pos++;
233                         continue;
234                 }
235
236                 if (!use_hash || de->hash_code == fname->hash) {
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                 }
245
246                 if (max_slots && max_len > *max_slots)
247                         *max_slots = max_len;
248                 max_len = 0;
249
250                 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
251         }
252
253         de = NULL;
254 found:
255         if (max_slots && max_len > *max_slots)
256                 *max_slots = max_len;
257         return de;
258 }
259
260 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
261                                         unsigned int level,
262                                         const struct f2fs_filename *fname,
263                                         struct folio **res_folio,
264                                         bool use_hash)
265 {
266         int s = GET_DENTRY_SLOTS(fname->disk_name.len);
267         unsigned int nbucket, nblock;
268         unsigned int bidx, end_block, bucket_no;
269         struct f2fs_dir_entry *de = NULL;
270         pgoff_t next_pgofs;
271         bool room = false;
272         int max_slots;
273
274         nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
275         nblock = bucket_blocks(level);
276
277         bucket_no = use_hash ? le32_to_cpu(fname->hash) % nbucket : 0;
278
279 start_find_bucket:
280         bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
281                                bucket_no);
282         end_block = bidx + nblock;
283
284         while (bidx < end_block) {
285                 /* no need to allocate new dentry pages to all the indices */
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) {
290                                 room = true;
291                                 bidx = next_pgofs;
292                                 continue;
293                         } else {
294                                 *res_folio = dentry_folio;
295                                 break;
296                         }
297                 }
298
299                 de = find_in_block(dir, dentry_folio, fname, &max_slots, use_hash);
300                 if (IS_ERR(de)) {
301                         *res_folio = ERR_CAST(de);
302                         de = NULL;
303                         break;
304                 } else if (de) {
305                         *res_folio = dentry_folio;
306                         break;
307                 }
308
309                 if (max_slots >= s)
310                         room = true;
311                 f2fs_folio_put(dentry_folio, false);
312
313                 bidx++;
314         }
315
316         if (de)
317                 return de;
318
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;
328 }
329
330 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
331                                          const struct f2fs_filename *fname,
332                                          struct folio **res_folio)
333 {
334         unsigned long npages = dir_blocks(dir);
335         struct f2fs_dir_entry *de = NULL;
336         unsigned int max_depth;
337         unsigned int level;
338         bool use_hash = true;
339
340         *res_folio = NULL;
341
342 #if IS_ENABLED(CONFIG_UNICODE)
343 start_find_entry:
344 #endif
345         if (f2fs_has_inline_dentry(dir)) {
346                 de = f2fs_find_in_inline_dir(dir, fname, res_folio, use_hash);
347                 goto out;
348         }
349
350         if (npages == 0)
351                 goto out;
352
353         max_depth = F2FS_I(dir)->i_current_depth;
354         if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
355                 f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
356                           dir->i_ino, max_depth);
357                 max_depth = MAX_DIR_HASH_DEPTH;
358                 f2fs_i_depth_write(dir, max_depth);
359         }
360
361         for (level = 0; level < max_depth; level++) {
362                 de = find_in_level(dir, level, fname, res_folio, use_hash);
363                 if (de || IS_ERR(*res_folio))
364                         break;
365         }
366
367 out:
368 #if IS_ENABLED(CONFIG_UNICODE)
369         if (!sb_no_casefold_compat_fallback(dir->i_sb) &&
370                 IS_CASEFOLDED(dir) && !de && use_hash) {
371                 use_hash = false;
372                 goto start_find_entry;
373         }
374 #endif
375         /* This is to increase the speed of f2fs_create */
376         if (!de)
377                 F2FS_I(dir)->task = current;
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  */
387 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
388                         const struct qstr *child, struct folio **res_folio)
389 {
390         struct f2fs_dir_entry *de = NULL;
391         struct f2fs_filename fname;
392         int err;
393
394         err = f2fs_setup_filename(dir, child, 1, &fname);
395         if (err) {
396                 if (err == -ENOENT)
397                         *res_folio = NULL;
398                 else
399                         *res_folio = ERR_PTR(err);
400                 return NULL;
401         }
402
403         de = __f2fs_find_entry(dir, &fname, res_folio);
404
405         f2fs_free_filename(&fname);
406         return de;
407 }
408
409 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct folio **f)
410 {
411         return f2fs_find_entry(dir, &dotdot_name, f);
412 }
413
414 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
415                                                         struct folio **folio)
416 {
417         ino_t res = 0;
418         struct f2fs_dir_entry *de;
419
420         de = f2fs_find_entry(dir, qstr, folio);
421         if (de) {
422                 res = le32_to_cpu(de->ino);
423                 f2fs_folio_put(*folio, false);
424         }
425
426         return res;
427 }
428
429 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
430                 struct folio *folio, struct inode *inode)
431 {
432         enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
433
434         folio_lock(folio);
435         f2fs_folio_wait_writeback(folio, type, true, true);
436         de->ino = cpu_to_le32(inode->i_ino);
437         de->file_type = fs_umode_to_ftype(inode->i_mode);
438         folio_mark_dirty(folio);
439
440         inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
441         f2fs_mark_inode_dirty_sync(dir, false);
442         f2fs_folio_put(folio, true);
443 }
444
445 static void init_dent_inode(struct inode *dir, struct inode *inode,
446                             const struct f2fs_filename *fname,
447                             struct folio *ifolio)
448 {
449         struct f2fs_inode *ri;
450
451         if (!fname) /* tmpfile case? */
452                 return;
453
454         f2fs_folio_wait_writeback(ifolio, NODE, true, true);
455
456         /* copy name info. to this inode folio */
457         ri = F2FS_INODE(&ifolio->page);
458         ri->i_namelen = cpu_to_le32(fname->disk_name.len);
459         memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
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         }
478         folio_mark_dirty(ifolio);
479 }
480
481 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
482                                         struct f2fs_dentry_ptr *d)
483 {
484         struct fscrypt_str dot = FSTR_INIT(".", 1);
485         struct fscrypt_str dotdot = FSTR_INIT("..", 2);
486
487         /* update dirent of "." */
488         f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
489
490         /* update dirent of ".." */
491         f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
492 }
493
494 static int make_empty_dir(struct inode *inode,
495                 struct inode *parent, struct folio *folio)
496 {
497         struct folio *dentry_folio;
498         struct f2fs_dentry_block *dentry_blk;
499         struct f2fs_dentry_ptr d;
500
501         if (f2fs_has_inline_dentry(inode))
502                 return f2fs_make_empty_inline_dir(inode, parent, folio);
503
504         dentry_folio = f2fs_get_new_data_folio(inode, folio, 0, true);
505         if (IS_ERR(dentry_folio))
506                 return PTR_ERR(dentry_folio);
507
508         dentry_blk = folio_address(dentry_folio);
509
510         make_dentry_ptr_block(NULL, &d, dentry_blk);
511         f2fs_do_make_empty_dir(inode, parent, &d);
512
513         folio_mark_dirty(dentry_folio);
514         f2fs_folio_put(dentry_folio, true);
515         return 0;
516 }
517
518 struct folio *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
519                 const struct f2fs_filename *fname, struct folio *dfolio)
520 {
521         struct folio *folio;
522         int err;
523
524         if (is_inode_flag_set(inode, FI_NEW_INODE)) {
525                 folio = f2fs_new_inode_folio(inode);
526                 if (IS_ERR(folio))
527                         return folio;
528
529                 if (S_ISDIR(inode->i_mode)) {
530                         /* in order to handle error case */
531                         folio_get(folio);
532                         err = make_empty_dir(inode, dir, folio);
533                         if (err) {
534                                 folio_lock(folio);
535                                 goto put_error;
536                         }
537                         folio_put(folio);
538                 }
539
540                 err = f2fs_init_acl(inode, dir, folio, dfolio);
541                 if (err)
542                         goto put_error;
543
544                 err = f2fs_init_security(inode, dir,
545                                          fname ? fname->usr_fname : NULL,
546                                          folio);
547                 if (err)
548                         goto put_error;
549
550                 if (IS_ENCRYPTED(inode)) {
551                         err = fscrypt_set_context(inode, folio);
552                         if (err)
553                                 goto put_error;
554                 }
555         } else {
556                 folio = f2fs_get_inode_folio(F2FS_I_SB(dir), inode->i_ino);
557                 if (IS_ERR(folio))
558                         return folio;
559         }
560
561         init_dent_inode(dir, inode, fname, folio);
562
563         /*
564          * This file should be checkpointed during fsync.
565          * We lost i_pino from now on.
566          */
567         if (is_inode_flag_set(inode, FI_INC_LINK)) {
568                 if (!S_ISDIR(inode->i_mode))
569                         file_lost_pino(inode);
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)
575                         f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
576                 f2fs_i_links_write(inode, true);
577         }
578         return folio;
579
580 put_error:
581         clear_nlink(inode);
582         f2fs_update_inode(inode, folio);
583         f2fs_folio_put(folio, true);
584         return ERR_PTR(err);
585 }
586
587 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
588                                                 unsigned int current_depth)
589 {
590         if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
591                 if (S_ISDIR(inode->i_mode))
592                         f2fs_i_links_write(dir, true);
593                 clear_inode_flag(inode, FI_NEW_INODE);
594         }
595         inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
596         f2fs_mark_inode_dirty_sync(dir, false);
597
598         if (F2FS_I(dir)->i_current_depth != current_depth)
599                 f2fs_i_depth_write(dir, current_depth);
600
601         if (inode && is_inode_flag_set(inode, FI_INC_LINK))
602                 clear_inode_flag(inode, FI_INC_LINK);
603 }
604
605 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
606 {
607         int bit_start = 0;
608         int zero_start, zero_end;
609 next:
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);
615         if (zero_end - zero_start >= slots)
616                 return zero_start;
617
618         bit_start = zero_end + 1;
619
620         if (zero_end + 1 >= max_slots)
621                 return max_slots;
622         goto next;
623 }
624
625 bool f2fs_has_enough_room(struct inode *dir, struct folio *ifolio,
626                           const struct f2fs_filename *fname)
627 {
628         struct f2fs_dentry_ptr d;
629         unsigned int bit_pos;
630         int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
631
632         make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ifolio));
633
634         bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
635
636         return bit_pos < d.max;
637 }
638
639 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
640                         const struct fscrypt_str *name, f2fs_hash_t name_hash,
641                         unsigned int bit_pos)
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);
651         de->ino = cpu_to_le32(ino);
652         de->file_type = fs_umode_to_ftype(mode);
653         for (i = 0; i < slots; i++) {
654                 __set_bit_le(bit_pos + i, (void *)d->bitmap);
655                 /* avoid wrong garbage data for readdir */
656                 if (i)
657                         (de + i)->name_len = 0;
658         }
659 }
660
661 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
662                            struct inode *inode, nid_t ino, umode_t mode)
663 {
664         unsigned int bit_pos;
665         unsigned int level;
666         unsigned int current_depth;
667         unsigned long bidx, block;
668         unsigned int nbucket, nblock;
669         struct folio *dentry_folio = NULL;
670         struct f2fs_dentry_block *dentry_blk = NULL;
671         struct f2fs_dentry_ptr d;
672         struct folio *folio = NULL;
673         int slots, err = 0;
674
675         level = 0;
676         slots = GET_DENTRY_SLOTS(fname->disk_name.len);
677
678         current_depth = F2FS_I(dir)->i_current_depth;
679         if (F2FS_I(dir)->chash == fname->hash) {
680                 level = F2FS_I(dir)->clevel;
681                 F2FS_I(dir)->chash = 0;
682         }
683
684 start:
685         if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH))
686                 return -ENOSPC;
687
688         if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
689                 return -ENOSPC;
690
691         /* Increase the depth, if required */
692         if (level == current_depth)
693                 ++current_depth;
694
695         nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
696         nblock = bucket_blocks(level);
697
698         bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
699                                 (le32_to_cpu(fname->hash) % nbucket));
700
701         for (block = bidx; block <= (bidx + nblock - 1); block++) {
702                 dentry_folio = f2fs_get_new_data_folio(dir, NULL, block, true);
703                 if (IS_ERR(dentry_folio))
704                         return PTR_ERR(dentry_folio);
705
706                 dentry_blk = folio_address(dentry_folio);
707                 bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
708                                                 slots, NR_DENTRY_IN_BLOCK);
709                 if (bit_pos < NR_DENTRY_IN_BLOCK)
710                         goto add_dentry;
711
712                 f2fs_folio_put(dentry_folio, true);
713         }
714
715         /* Move to next level to find the empty slot for new dentry */
716         ++level;
717         goto start;
718 add_dentry:
719         f2fs_folio_wait_writeback(dentry_folio, DATA, true, true);
720
721         if (inode) {
722                 f2fs_down_write(&F2FS_I(inode)->i_sem);
723                 folio = f2fs_init_inode_metadata(inode, dir, fname, NULL);
724                 if (IS_ERR(folio)) {
725                         err = PTR_ERR(folio);
726                         goto fail;
727                 }
728         }
729
730         make_dentry_ptr_block(NULL, &d, dentry_blk);
731         f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
732                            bit_pos);
733
734         folio_mark_dirty(dentry_folio);
735
736         if (inode) {
737                 f2fs_i_pino_write(inode, dir->i_ino);
738
739                 /* synchronize inode page's data from inode cache */
740                 if (is_inode_flag_set(inode, FI_NEW_INODE))
741                         f2fs_update_inode(inode, folio);
742
743                 f2fs_folio_put(folio, true);
744         }
745
746         f2fs_update_parent_metadata(dir, inode, current_depth);
747 fail:
748         if (inode)
749                 f2fs_up_write(&F2FS_I(inode)->i_sem);
750
751         f2fs_folio_put(dentry_folio, true);
752
753         return err;
754 }
755
756 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
757                     struct inode *inode, nid_t ino, umode_t mode)
758 {
759         int err = -EAGAIN;
760
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);
767                 err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
768                 f2fs_up_read(&F2FS_I(dir)->i_xattr_sem);
769         }
770         if (err == -EAGAIN)
771                 err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
772
773         f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
774         return err;
775 }
776
777 /*
778  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
779  * f2fs_unlock_op().
780  */
781 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
782                                 struct inode *inode, nid_t ino, umode_t mode)
783 {
784         struct f2fs_filename fname;
785         struct folio *folio = NULL;
786         struct f2fs_dir_entry *de = NULL;
787         int err;
788
789         err = f2fs_setup_filename(dir, name, 0, &fname);
790         if (err)
791                 return err;
792
793         /*
794          * An immature stackable filesystem shows a race condition between lookup
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) {
801                 de = __f2fs_find_entry(dir, &fname, &folio);
802                 F2FS_I(dir)->task = NULL;
803         }
804         if (de) {
805                 f2fs_folio_put(folio, false);
806                 err = -EEXIST;
807         } else if (IS_ERR(folio)) {
808                 err = PTR_ERR(folio);
809         } else {
810                 err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
811         }
812         f2fs_free_filename(&fname);
813         return err;
814 }
815
816 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir,
817                                         struct f2fs_filename *fname)
818 {
819         struct folio *folio;
820         int err = 0;
821
822         f2fs_down_write(&F2FS_I(inode)->i_sem);
823         folio = f2fs_init_inode_metadata(inode, dir, fname, NULL);
824         if (IS_ERR(folio)) {
825                 err = PTR_ERR(folio);
826                 goto fail;
827         }
828         f2fs_folio_put(folio, true);
829
830         clear_inode_flag(inode, FI_NEW_INODE);
831         f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
832 fail:
833         f2fs_up_write(&F2FS_I(inode)->i_sem);
834         return err;
835 }
836
837 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
838 {
839         struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
840
841         f2fs_down_write(&F2FS_I(inode)->i_sem);
842
843         if (S_ISDIR(inode->i_mode))
844                 f2fs_i_links_write(dir, false);
845         inode_set_ctime_current(inode);
846
847         f2fs_i_links_write(inode, false);
848         if (S_ISDIR(inode->i_mode)) {
849                 f2fs_i_links_write(inode, false);
850                 f2fs_i_size_write(inode, 0);
851         }
852         f2fs_up_write(&F2FS_I(inode)->i_sem);
853
854         if (inode->i_nlink == 0)
855                 f2fs_add_orphan_inode(inode);
856         else
857                 f2fs_release_orphan_inode(sbi);
858 }
859
860 /*
861  * It only removes the dentry from the dentry page, corresponding name
862  * entry in name page does not need to be touched during deletion.
863  */
864 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct folio *folio,
865                                         struct inode *dir, struct inode *inode)
866 {
867         struct f2fs_dentry_block *dentry_blk;
868         unsigned int bit_pos;
869         int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
870         pgoff_t index = folio->index;
871         int i;
872
873         f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
874
875         if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
876                 f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
877
878         if (f2fs_has_inline_dentry(dir))
879                 return f2fs_delete_inline_entry(dentry, folio, dir, inode);
880
881         folio_lock(folio);
882         f2fs_folio_wait_writeback(folio, DATA, true, true);
883
884         dentry_blk = folio_address(folio);
885         bit_pos = dentry - dentry_blk->dentry;
886         for (i = 0; i < slots; i++)
887                 __clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
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);
893         folio_mark_dirty(folio);
894
895         if (bit_pos == NR_DENTRY_IN_BLOCK &&
896                 !f2fs_truncate_hole(dir, index, index + 1)) {
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);
901
902                 inode_dec_dirty_pages(dir);
903                 f2fs_remove_dirty_inode(dir);
904         }
905         f2fs_folio_put(folio, true);
906
907         inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
908         f2fs_mark_inode_dirty_sync(dir, false);
909
910         if (inode)
911                 f2fs_drop_nlink(dir, inode);
912 }
913
914 bool f2fs_empty_dir(struct inode *dir)
915 {
916         unsigned long bidx = 0;
917         unsigned int bit_pos;
918         struct f2fs_dentry_block *dentry_blk;
919         unsigned long nblock = dir_blocks(dir);
920
921         if (f2fs_has_inline_dentry(dir))
922                 return f2fs_empty_inline_dir(dir);
923
924         while (bidx < nblock) {
925                 pgoff_t next_pgofs;
926                 struct folio *dentry_folio;
927
928                 dentry_folio = f2fs_find_data_folio(dir, bidx, &next_pgofs);
929                 if (IS_ERR(dentry_folio)) {
930                         if (PTR_ERR(dentry_folio) == -ENOENT) {
931                                 bidx = next_pgofs;
932                                 continue;
933                         } else {
934                                 return false;
935                         }
936                 }
937
938                 dentry_blk = folio_address(dentry_folio);
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);
946
947                 f2fs_folio_put(dentry_folio, false);
948
949                 if (bit_pos < NR_DENTRY_IN_BLOCK)
950                         return false;
951
952                 bidx++;
953         }
954         return true;
955 }
956
957 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
958                         unsigned int start_pos, struct fscrypt_str *fstr)
959 {
960         unsigned char d_type = DT_UNKNOWN;
961         unsigned int bit_pos;
962         struct f2fs_dir_entry *de = NULL;
963         struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
964         struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
965         struct blk_plug plug;
966         bool readdir_ra = sbi->readdir_ra;
967         bool found_valid_dirent = false;
968         int err = 0;
969
970         bit_pos = ((unsigned long)ctx->pos % d->max);
971
972         if (readdir_ra)
973                 blk_start_plug(&plug);
974
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)
978                         break;
979
980                 de = &d->dentry[bit_pos];
981                 if (de->name_len == 0) {
982                         if (found_valid_dirent || !bit_pos) {
983                                 f2fs_warn_ratelimited(sbi,
984                                         "invalid namelen(0), ino:%u, run fsck to fix.",
985                                         le32_to_cpu(de->ino));
986                                 set_sbi_flag(sbi, SBI_NEED_FSCK);
987                         }
988                         bit_pos++;
989                         ctx->pos = start_pos + bit_pos;
990                         continue;
991                 }
992
993                 d_type = fs_ftype_to_dtype(de->file_type);
994
995                 de_name.name = d->filename[bit_pos];
996                 de_name.len = le16_to_cpu(de->name_len);
997
998                 /* check memory boundary before moving forward */
999                 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1000                 if (unlikely(bit_pos > d->max ||
1001                                 le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1002                         f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1003                                   __func__, le16_to_cpu(de->name_len));
1004                         set_sbi_flag(sbi, SBI_NEED_FSCK);
1005                         err = -EFSCORRUPTED;
1006                         f2fs_handle_error(sbi, ERROR_CORRUPTED_DIRENT);
1007                         goto out;
1008                 }
1009
1010                 if (IS_ENCRYPTED(d->inode)) {
1011                         int save_len = fstr->len;
1012
1013                         err = fscrypt_fname_disk_to_usr(d->inode,
1014                                                 (u32)le32_to_cpu(de->hash_code),
1015                                                 0, &de_name, fstr);
1016                         if (err)
1017                                 goto out;
1018
1019                         de_name = *fstr;
1020                         fstr->len = save_len;
1021                 }
1022
1023                 if (!dir_emit(ctx, de_name.name, de_name.len,
1024                                         le32_to_cpu(de->ino), d_type)) {
1025                         err = 1;
1026                         goto out;
1027                 }
1028
1029                 if (readdir_ra)
1030                         f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1031
1032                 ctx->pos = start_pos + bit_pos;
1033                 found_valid_dirent = true;
1034         }
1035 out:
1036         if (readdir_ra)
1037                 blk_finish_plug(&plug);
1038         return err;
1039 }
1040
1041 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1042 {
1043         struct inode *inode = file_inode(file);
1044         unsigned long npages = dir_blocks(inode);
1045         struct f2fs_dentry_block *dentry_blk = NULL;
1046         struct file_ra_state *ra = &file->f_ra;
1047         loff_t start_pos = ctx->pos;
1048         unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1049         struct f2fs_dentry_ptr d;
1050         struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1051         int err = 0;
1052
1053         if (IS_ENCRYPTED(inode)) {
1054                 err = fscrypt_prepare_readdir(inode);
1055                 if (err)
1056                         goto out;
1057
1058                 err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1059                 if (err < 0)
1060                         goto out;
1061         }
1062
1063         if (f2fs_has_inline_dentry(inode)) {
1064                 err = f2fs_read_inline_dir(file, ctx, &fstr);
1065                 goto out_free;
1066         }
1067
1068         for (; n < npages; ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1069                 struct folio *dentry_folio;
1070                 pgoff_t next_pgofs;
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
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
1084                 dentry_folio = f2fs_find_data_folio(inode, n, &next_pgofs);
1085                 if (IS_ERR(dentry_folio)) {
1086                         err = PTR_ERR(dentry_folio);
1087                         if (err == -ENOENT) {
1088                                 err = 0;
1089                                 n = next_pgofs;
1090                                 continue;
1091                         } else {
1092                                 goto out_free;
1093                         }
1094                 }
1095
1096                 dentry_blk = folio_address(dentry_folio);
1097
1098                 make_dentry_ptr_block(inode, &d, dentry_blk);
1099
1100                 err = f2fs_fill_dentries(ctx, &d,
1101                                 n * NR_DENTRY_IN_BLOCK, &fstr);
1102                 f2fs_folio_put(dentry_folio, false);
1103                 if (err)
1104                         break;
1105
1106                 n++;
1107         }
1108 out_free:
1109         fscrypt_fname_free_buffer(&fstr);
1110 out:
1111         trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1112         return err < 0 ? err : 0;
1113 }
1114
1115 const struct file_operations f2fs_dir_operations = {
1116         .llseek         = generic_file_llseek,
1117         .read           = generic_read_dir,
1118         .iterate_shared = f2fs_readdir,
1119         .fsync          = f2fs_sync_file,
1120         .unlocked_ioctl = f2fs_ioctl,
1121 #ifdef CONFIG_COMPAT
1122         .compat_ioctl   = f2fs_compat_ioctl,
1123 #endif
1124 };