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