xref: /linux/fs/f2fs/dir.c (revision b7df4cc3a088a8ce6973c96731bc792dbf54ce28)
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 <asm/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 @dir is casefolded, initialize @fname->cf_name from @fname->usr_fname. */
46 int f2fs_init_casefolded_name(const struct inode *dir,
47 			      struct f2fs_filename *fname)
48 {
49 #if IS_ENABLED(CONFIG_UNICODE)
50 	struct super_block *sb = dir->i_sb;
51 
52 	if (IS_CASEFOLDED(dir) &&
53 	    !is_dot_dotdot(fname->usr_fname->name, fname->usr_fname->len)) {
54 		fname->cf_name.name = f2fs_kmem_cache_alloc(f2fs_cf_name_slab,
55 					GFP_NOFS, false, F2FS_SB(sb));
56 		if (!fname->cf_name.name)
57 			return -ENOMEM;
58 		fname->cf_name.len = utf8_casefold(sb->s_encoding,
59 						   fname->usr_fname,
60 						   fname->cf_name.name,
61 						   F2FS_NAME_LEN);
62 		if ((int)fname->cf_name.len <= 0) {
63 			kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
64 			fname->cf_name.name = NULL;
65 			if (sb_has_strict_encoding(sb))
66 				return -EINVAL;
67 			/* fall back to treating name as opaque byte sequence */
68 		}
69 	}
70 #endif
71 	return 0;
72 }
73 
74 static int __f2fs_setup_filename(const struct inode *dir,
75 				 const struct fscrypt_name *crypt_name,
76 				 struct f2fs_filename *fname)
77 {
78 	int err;
79 
80 	memset(fname, 0, sizeof(*fname));
81 
82 	fname->usr_fname = crypt_name->usr_fname;
83 	fname->disk_name = crypt_name->disk_name;
84 #ifdef CONFIG_FS_ENCRYPTION
85 	fname->crypto_buf = crypt_name->crypto_buf;
86 #endif
87 	if (crypt_name->is_nokey_name) {
88 		/* hash was decoded from the no-key name */
89 		fname->hash = cpu_to_le32(crypt_name->hash);
90 	} else {
91 		err = f2fs_init_casefolded_name(dir, fname);
92 		if (err) {
93 			f2fs_free_filename(fname);
94 			return err;
95 		}
96 		f2fs_hash_filename(dir, fname);
97 	}
98 	return 0;
99 }
100 
101 /*
102  * Prepare to search for @iname in @dir.  This is similar to
103  * fscrypt_setup_filename(), but this also handles computing the casefolded name
104  * and the f2fs dirhash if needed, then packing all the information about this
105  * filename up into a 'struct f2fs_filename'.
106  */
107 int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
108 			int lookup, struct f2fs_filename *fname)
109 {
110 	struct fscrypt_name crypt_name;
111 	int err;
112 
113 	err = fscrypt_setup_filename(dir, iname, lookup, &crypt_name);
114 	if (err)
115 		return err;
116 
117 	return __f2fs_setup_filename(dir, &crypt_name, fname);
118 }
119 
120 /*
121  * Prepare to look up @dentry in @dir.  This is similar to
122  * fscrypt_prepare_lookup(), but this also handles computing the casefolded name
123  * and the f2fs dirhash if needed, then packing all the information about this
124  * filename up into a 'struct f2fs_filename'.
125  */
126 int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
127 			struct f2fs_filename *fname)
128 {
129 	struct fscrypt_name crypt_name;
130 	int err;
131 
132 	err = fscrypt_prepare_lookup(dir, dentry, &crypt_name);
133 	if (err)
134 		return err;
135 
136 	return __f2fs_setup_filename(dir, &crypt_name, fname);
137 }
138 
139 void f2fs_free_filename(struct f2fs_filename *fname)
140 {
141 #ifdef CONFIG_FS_ENCRYPTION
142 	kfree(fname->crypto_buf.name);
143 	fname->crypto_buf.name = NULL;
144 #endif
145 #if IS_ENABLED(CONFIG_UNICODE)
146 	if (fname->cf_name.name) {
147 		kmem_cache_free(f2fs_cf_name_slab, fname->cf_name.name);
148 		fname->cf_name.name = NULL;
149 	}
150 #endif
151 }
152 
153 static unsigned long dir_block_index(unsigned int level,
154 				int dir_level, unsigned int idx)
155 {
156 	unsigned long i;
157 	unsigned long bidx = 0;
158 
159 	for (i = 0; i < level; i++)
160 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
161 	bidx += idx * bucket_blocks(level);
162 	return bidx;
163 }
164 
165 static struct f2fs_dir_entry *find_in_block(struct inode *dir,
166 				struct page *dentry_page,
167 				const struct f2fs_filename *fname,
168 				int *max_slots)
169 {
170 	struct f2fs_dentry_block *dentry_blk;
171 	struct f2fs_dentry_ptr d;
172 
173 	dentry_blk = (struct f2fs_dentry_block *)page_address(dentry_page);
174 
175 	make_dentry_ptr_block(dir, &d, dentry_blk);
176 	return f2fs_find_target_dentry(&d, fname, max_slots);
177 }
178 
179 #if IS_ENABLED(CONFIG_UNICODE)
180 /*
181  * Test whether a case-insensitive directory entry matches the filename
182  * being searched for.
183  *
184  * Returns 1 for a match, 0 for no match, and -errno on an error.
185  */
186 static int f2fs_match_ci_name(const struct inode *dir, const struct qstr *name,
187 			       const u8 *de_name, u32 de_name_len)
188 {
189 	const struct super_block *sb = dir->i_sb;
190 	const struct unicode_map *um = sb->s_encoding;
191 	struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
192 	struct qstr entry = QSTR_INIT(de_name, de_name_len);
193 	int res;
194 
195 	if (IS_ENCRYPTED(dir)) {
196 		const struct fscrypt_str encrypted_name =
197 			FSTR_INIT((u8 *)de_name, de_name_len);
198 
199 		if (WARN_ON_ONCE(!fscrypt_has_encryption_key(dir)))
200 			return -EINVAL;
201 
202 		decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
203 		if (!decrypted_name.name)
204 			return -ENOMEM;
205 		res = fscrypt_fname_disk_to_usr(dir, 0, 0, &encrypted_name,
206 						&decrypted_name);
207 		if (res < 0)
208 			goto out;
209 		entry.name = decrypted_name.name;
210 		entry.len = decrypted_name.len;
211 	}
212 
213 	res = utf8_strncasecmp_folded(um, name, &entry);
214 	/*
215 	 * In strict mode, ignore invalid names.  In non-strict mode,
216 	 * fall back to treating them as opaque byte sequences.
217 	 */
218 	if (res < 0 && !sb_has_strict_encoding(sb)) {
219 		res = name->len == entry.len &&
220 				memcmp(name->name, entry.name, name->len) == 0;
221 	} else {
222 		/* utf8_strncasecmp_folded returns 0 on match */
223 		res = (res == 0);
224 	}
225 out:
226 	kfree(decrypted_name.name);
227 	return res;
228 }
229 #endif /* CONFIG_UNICODE */
230 
231 static inline int f2fs_match_name(const struct inode *dir,
232 				   const struct f2fs_filename *fname,
233 				   const u8 *de_name, u32 de_name_len)
234 {
235 	struct fscrypt_name f;
236 
237 #if IS_ENABLED(CONFIG_UNICODE)
238 	if (fname->cf_name.name) {
239 		struct qstr cf = FSTR_TO_QSTR(&fname->cf_name);
240 
241 		return f2fs_match_ci_name(dir, &cf, de_name, de_name_len);
242 	}
243 #endif
244 	f.usr_fname = fname->usr_fname;
245 	f.disk_name = fname->disk_name;
246 #ifdef CONFIG_FS_ENCRYPTION
247 	f.crypto_buf = fname->crypto_buf;
248 #endif
249 	return fscrypt_match_name(&f, de_name, de_name_len);
250 }
251 
252 struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
253 			const struct f2fs_filename *fname, int *max_slots)
254 {
255 	struct f2fs_dir_entry *de;
256 	unsigned long bit_pos = 0;
257 	int max_len = 0;
258 	int res = 0;
259 
260 	if (max_slots)
261 		*max_slots = 0;
262 	while (bit_pos < d->max) {
263 		if (!test_bit_le(bit_pos, d->bitmap)) {
264 			bit_pos++;
265 			max_len++;
266 			continue;
267 		}
268 
269 		de = &d->dentry[bit_pos];
270 
271 		if (unlikely(!de->name_len)) {
272 			bit_pos++;
273 			continue;
274 		}
275 
276 		if (de->hash_code == fname->hash) {
277 			res = f2fs_match_name(d->inode, fname,
278 					      d->filename[bit_pos],
279 					      le16_to_cpu(de->name_len));
280 			if (res < 0)
281 				return ERR_PTR(res);
282 			if (res)
283 				goto found;
284 		}
285 
286 		if (max_slots && max_len > *max_slots)
287 			*max_slots = max_len;
288 		max_len = 0;
289 
290 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
291 	}
292 
293 	de = NULL;
294 found:
295 	if (max_slots && max_len > *max_slots)
296 		*max_slots = max_len;
297 	return de;
298 }
299 
300 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
301 					unsigned int level,
302 					const struct f2fs_filename *fname,
303 					struct page **res_page)
304 {
305 	int s = GET_DENTRY_SLOTS(fname->disk_name.len);
306 	unsigned int nbucket, nblock;
307 	unsigned int bidx, end_block;
308 	struct page *dentry_page;
309 	struct f2fs_dir_entry *de = NULL;
310 	pgoff_t next_pgofs;
311 	bool room = false;
312 	int max_slots;
313 
314 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
315 	nblock = bucket_blocks(level);
316 
317 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
318 			       le32_to_cpu(fname->hash) % nbucket);
319 	end_block = bidx + nblock;
320 
321 	while (bidx < end_block) {
322 		/* no need to allocate new dentry pages to all the indices */
323 		dentry_page = f2fs_find_data_page(dir, bidx, &next_pgofs);
324 		if (IS_ERR(dentry_page)) {
325 			if (PTR_ERR(dentry_page) == -ENOENT) {
326 				room = true;
327 				bidx = next_pgofs;
328 				continue;
329 			} else {
330 				*res_page = dentry_page;
331 				break;
332 			}
333 		}
334 
335 		de = find_in_block(dir, dentry_page, fname, &max_slots);
336 		if (IS_ERR(de)) {
337 			*res_page = ERR_CAST(de);
338 			de = NULL;
339 			break;
340 		} else if (de) {
341 			*res_page = dentry_page;
342 			break;
343 		}
344 
345 		if (max_slots >= s)
346 			room = true;
347 		f2fs_put_page(dentry_page, 0);
348 
349 		bidx++;
350 	}
351 
352 	if (!de && room && F2FS_I(dir)->chash != fname->hash) {
353 		F2FS_I(dir)->chash = fname->hash;
354 		F2FS_I(dir)->clevel = level;
355 	}
356 
357 	return de;
358 }
359 
360 struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
361 					 const struct f2fs_filename *fname,
362 					 struct page **res_page)
363 {
364 	unsigned long npages = dir_blocks(dir);
365 	struct f2fs_dir_entry *de = NULL;
366 	unsigned int max_depth;
367 	unsigned int level;
368 
369 	*res_page = NULL;
370 
371 	if (f2fs_has_inline_dentry(dir)) {
372 		de = f2fs_find_in_inline_dir(dir, fname, res_page);
373 		goto out;
374 	}
375 
376 	if (npages == 0)
377 		goto out;
378 
379 	max_depth = F2FS_I(dir)->i_current_depth;
380 	if (unlikely(max_depth > MAX_DIR_HASH_DEPTH)) {
381 		f2fs_warn(F2FS_I_SB(dir), "Corrupted max_depth of %lu: %u",
382 			  dir->i_ino, max_depth);
383 		max_depth = MAX_DIR_HASH_DEPTH;
384 		f2fs_i_depth_write(dir, max_depth);
385 	}
386 
387 	for (level = 0; level < max_depth; level++) {
388 		de = find_in_level(dir, level, fname, res_page);
389 		if (de || IS_ERR(*res_page))
390 			break;
391 	}
392 out:
393 	/* This is to increase the speed of f2fs_create */
394 	if (!de)
395 		F2FS_I(dir)->task = current;
396 	return de;
397 }
398 
399 /*
400  * Find an entry in the specified directory with the wanted name.
401  * It returns the page where the entry was found (as a parameter - res_page),
402  * and the entry itself. Page is returned mapped and unlocked.
403  * Entry is guaranteed to be valid.
404  */
405 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
406 			const struct qstr *child, struct page **res_page)
407 {
408 	struct f2fs_dir_entry *de = NULL;
409 	struct f2fs_filename fname;
410 	int err;
411 
412 	err = f2fs_setup_filename(dir, child, 1, &fname);
413 	if (err) {
414 		if (err == -ENOENT)
415 			*res_page = NULL;
416 		else
417 			*res_page = ERR_PTR(err);
418 		return NULL;
419 	}
420 
421 	de = __f2fs_find_entry(dir, &fname, res_page);
422 
423 	f2fs_free_filename(&fname);
424 	return de;
425 }
426 
427 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
428 {
429 	return f2fs_find_entry(dir, &dotdot_name, p);
430 }
431 
432 ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
433 							struct page **page)
434 {
435 	ino_t res = 0;
436 	struct f2fs_dir_entry *de;
437 
438 	de = f2fs_find_entry(dir, qstr, page);
439 	if (de) {
440 		res = le32_to_cpu(de->ino);
441 		f2fs_put_page(*page, 0);
442 	}
443 
444 	return res;
445 }
446 
447 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
448 		struct page *page, struct inode *inode)
449 {
450 	enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
451 
452 	lock_page(page);
453 	f2fs_wait_on_page_writeback(page, type, true, true);
454 	de->ino = cpu_to_le32(inode->i_ino);
455 	de->file_type = fs_umode_to_ftype(inode->i_mode);
456 	set_page_dirty(page);
457 
458 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
459 	f2fs_mark_inode_dirty_sync(dir, false);
460 	f2fs_put_page(page, 1);
461 }
462 
463 static void init_dent_inode(struct inode *dir, struct inode *inode,
464 			    const struct f2fs_filename *fname,
465 			    struct page *ipage)
466 {
467 	struct f2fs_inode *ri;
468 
469 	if (!fname) /* tmpfile case? */
470 		return;
471 
472 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
473 
474 	/* copy name info. to this inode page */
475 	ri = F2FS_INODE(ipage);
476 	ri->i_namelen = cpu_to_le32(fname->disk_name.len);
477 	memcpy(ri->i_name, fname->disk_name.name, fname->disk_name.len);
478 	if (IS_ENCRYPTED(dir)) {
479 		file_set_enc_name(inode);
480 		/*
481 		 * Roll-forward recovery doesn't have encryption keys available,
482 		 * so it can't compute the dirhash for encrypted+casefolded
483 		 * filenames.  Append it to i_name if possible.  Else, disable
484 		 * roll-forward recovery of the dentry (i.e., make fsync'ing the
485 		 * file force a checkpoint) by setting LOST_PINO.
486 		 */
487 		if (IS_CASEFOLDED(dir)) {
488 			if (fname->disk_name.len + sizeof(f2fs_hash_t) <=
489 			    F2FS_NAME_LEN)
490 				put_unaligned(fname->hash, (f2fs_hash_t *)
491 					&ri->i_name[fname->disk_name.len]);
492 			else
493 				file_lost_pino(inode);
494 		}
495 	}
496 	set_page_dirty(ipage);
497 }
498 
499 void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
500 					struct f2fs_dentry_ptr *d)
501 {
502 	struct fscrypt_str dot = FSTR_INIT(".", 1);
503 	struct fscrypt_str dotdot = FSTR_INIT("..", 2);
504 
505 	/* update dirent of "." */
506 	f2fs_update_dentry(inode->i_ino, inode->i_mode, d, &dot, 0, 0);
507 
508 	/* update dirent of ".." */
509 	f2fs_update_dentry(parent->i_ino, parent->i_mode, d, &dotdot, 0, 1);
510 }
511 
512 static int make_empty_dir(struct inode *inode,
513 		struct inode *parent, struct page *page)
514 {
515 	struct page *dentry_page;
516 	struct f2fs_dentry_block *dentry_blk;
517 	struct f2fs_dentry_ptr d;
518 
519 	if (f2fs_has_inline_dentry(inode))
520 		return f2fs_make_empty_inline_dir(inode, parent, page);
521 
522 	dentry_page = f2fs_get_new_data_page(inode, page, 0, true);
523 	if (IS_ERR(dentry_page))
524 		return PTR_ERR(dentry_page);
525 
526 	dentry_blk = page_address(dentry_page);
527 
528 	make_dentry_ptr_block(NULL, &d, dentry_blk);
529 	f2fs_do_make_empty_dir(inode, parent, &d);
530 
531 	set_page_dirty(dentry_page);
532 	f2fs_put_page(dentry_page, 1);
533 	return 0;
534 }
535 
536 struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
537 			const struct f2fs_filename *fname, struct page *dpage)
538 {
539 	struct page *page;
540 	int err;
541 
542 	if (is_inode_flag_set(inode, FI_NEW_INODE)) {
543 		page = f2fs_new_inode_page(inode);
544 		if (IS_ERR(page))
545 			return page;
546 
547 		if (S_ISDIR(inode->i_mode)) {
548 			/* in order to handle error case */
549 			get_page(page);
550 			err = make_empty_dir(inode, dir, page);
551 			if (err) {
552 				lock_page(page);
553 				goto put_error;
554 			}
555 			put_page(page);
556 		}
557 
558 		err = f2fs_init_acl(inode, dir, page, dpage);
559 		if (err)
560 			goto put_error;
561 
562 		err = f2fs_init_security(inode, dir,
563 					 fname ? fname->usr_fname : NULL, page);
564 		if (err)
565 			goto put_error;
566 
567 		if (IS_ENCRYPTED(inode)) {
568 			err = fscrypt_set_context(inode, page);
569 			if (err)
570 				goto put_error;
571 		}
572 	} else {
573 		page = f2fs_get_node_page(F2FS_I_SB(dir), inode->i_ino);
574 		if (IS_ERR(page))
575 			return page;
576 	}
577 
578 	init_dent_inode(dir, inode, fname, page);
579 
580 	/*
581 	 * This file should be checkpointed during fsync.
582 	 * We lost i_pino from now on.
583 	 */
584 	if (is_inode_flag_set(inode, FI_INC_LINK)) {
585 		if (!S_ISDIR(inode->i_mode))
586 			file_lost_pino(inode);
587 		/*
588 		 * If link the tmpfile to alias through linkat path,
589 		 * we should remove this inode from orphan list.
590 		 */
591 		if (inode->i_nlink == 0)
592 			f2fs_remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
593 		f2fs_i_links_write(inode, true);
594 	}
595 	return page;
596 
597 put_error:
598 	clear_nlink(inode);
599 	f2fs_update_inode(inode, page);
600 	f2fs_put_page(page, 1);
601 	return ERR_PTR(err);
602 }
603 
604 void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
605 						unsigned int current_depth)
606 {
607 	if (inode && is_inode_flag_set(inode, FI_NEW_INODE)) {
608 		if (S_ISDIR(inode->i_mode))
609 			f2fs_i_links_write(dir, true);
610 		clear_inode_flag(inode, FI_NEW_INODE);
611 	}
612 	inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir));
613 	f2fs_mark_inode_dirty_sync(dir, false);
614 
615 	if (F2FS_I(dir)->i_current_depth != current_depth)
616 		f2fs_i_depth_write(dir, current_depth);
617 
618 	if (inode && is_inode_flag_set(inode, FI_INC_LINK))
619 		clear_inode_flag(inode, FI_INC_LINK);
620 }
621 
622 int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots)
623 {
624 	int bit_start = 0;
625 	int zero_start, zero_end;
626 next:
627 	zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
628 	if (zero_start >= max_slots)
629 		return max_slots;
630 
631 	zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
632 	if (zero_end - zero_start >= slots)
633 		return zero_start;
634 
635 	bit_start = zero_end + 1;
636 
637 	if (zero_end + 1 >= max_slots)
638 		return max_slots;
639 	goto next;
640 }
641 
642 bool f2fs_has_enough_room(struct inode *dir, struct page *ipage,
643 			  const struct f2fs_filename *fname)
644 {
645 	struct f2fs_dentry_ptr d;
646 	unsigned int bit_pos;
647 	int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
648 
649 	make_dentry_ptr_inline(dir, &d, inline_data_addr(dir, ipage));
650 
651 	bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
652 
653 	return bit_pos < d.max;
654 }
655 
656 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
657 			const struct fscrypt_str *name, f2fs_hash_t name_hash,
658 			unsigned int bit_pos)
659 {
660 	struct f2fs_dir_entry *de;
661 	int slots = GET_DENTRY_SLOTS(name->len);
662 	int i;
663 
664 	de = &d->dentry[bit_pos];
665 	de->hash_code = name_hash;
666 	de->name_len = cpu_to_le16(name->len);
667 	memcpy(d->filename[bit_pos], name->name, name->len);
668 	de->ino = cpu_to_le32(ino);
669 	de->file_type = fs_umode_to_ftype(mode);
670 	for (i = 0; i < slots; i++) {
671 		__set_bit_le(bit_pos + i, (void *)d->bitmap);
672 		/* avoid wrong garbage data for readdir */
673 		if (i)
674 			(de + i)->name_len = 0;
675 	}
676 }
677 
678 int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
679 			   struct inode *inode, nid_t ino, umode_t mode)
680 {
681 	unsigned int bit_pos;
682 	unsigned int level;
683 	unsigned int current_depth;
684 	unsigned long bidx, block;
685 	unsigned int nbucket, nblock;
686 	struct page *dentry_page = NULL;
687 	struct f2fs_dentry_block *dentry_blk = NULL;
688 	struct f2fs_dentry_ptr d;
689 	struct page *page = NULL;
690 	int slots, err = 0;
691 
692 	level = 0;
693 	slots = GET_DENTRY_SLOTS(fname->disk_name.len);
694 
695 	current_depth = F2FS_I(dir)->i_current_depth;
696 	if (F2FS_I(dir)->chash == fname->hash) {
697 		level = F2FS_I(dir)->clevel;
698 		F2FS_I(dir)->chash = 0;
699 	}
700 
701 start:
702 	if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH))
703 		return -ENOSPC;
704 
705 	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
706 		return -ENOSPC;
707 
708 	/* Increase the depth, if required */
709 	if (level == current_depth)
710 		++current_depth;
711 
712 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
713 	nblock = bucket_blocks(level);
714 
715 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
716 				(le32_to_cpu(fname->hash) % nbucket));
717 
718 	for (block = bidx; block <= (bidx + nblock - 1); block++) {
719 		dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
720 		if (IS_ERR(dentry_page))
721 			return PTR_ERR(dentry_page);
722 
723 		dentry_blk = page_address(dentry_page);
724 		bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
725 						slots, NR_DENTRY_IN_BLOCK);
726 		if (bit_pos < NR_DENTRY_IN_BLOCK)
727 			goto add_dentry;
728 
729 		f2fs_put_page(dentry_page, 1);
730 	}
731 
732 	/* Move to next level to find the empty slot for new dentry */
733 	++level;
734 	goto start;
735 add_dentry:
736 	f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
737 
738 	if (inode) {
739 		f2fs_down_write(&F2FS_I(inode)->i_sem);
740 		page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
741 		if (IS_ERR(page)) {
742 			err = PTR_ERR(page);
743 			goto fail;
744 		}
745 	}
746 
747 	make_dentry_ptr_block(NULL, &d, dentry_blk);
748 	f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
749 			   bit_pos);
750 
751 	set_page_dirty(dentry_page);
752 
753 	if (inode) {
754 		f2fs_i_pino_write(inode, dir->i_ino);
755 
756 		/* synchronize inode page's data from inode cache */
757 		if (is_inode_flag_set(inode, FI_NEW_INODE))
758 			f2fs_update_inode(inode, page);
759 
760 		f2fs_put_page(page, 1);
761 	}
762 
763 	f2fs_update_parent_metadata(dir, inode, current_depth);
764 fail:
765 	if (inode)
766 		f2fs_up_write(&F2FS_I(inode)->i_sem);
767 
768 	f2fs_put_page(dentry_page, 1);
769 
770 	return err;
771 }
772 
773 int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
774 		    struct inode *inode, nid_t ino, umode_t mode)
775 {
776 	int err = -EAGAIN;
777 
778 	if (f2fs_has_inline_dentry(dir)) {
779 		/*
780 		 * Should get i_xattr_sem to keep the lock order:
781 		 * i_xattr_sem -> inode_page lock used by f2fs_setxattr.
782 		 */
783 		f2fs_down_read(&F2FS_I(dir)->i_xattr_sem);
784 		err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
785 		f2fs_up_read(&F2FS_I(dir)->i_xattr_sem);
786 	}
787 	if (err == -EAGAIN)
788 		err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
789 
790 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
791 	return err;
792 }
793 
794 /*
795  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
796  * f2fs_unlock_op().
797  */
798 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
799 				struct inode *inode, nid_t ino, umode_t mode)
800 {
801 	struct f2fs_filename fname;
802 	struct page *page = NULL;
803 	struct f2fs_dir_entry *de = NULL;
804 	int err;
805 
806 	err = f2fs_setup_filename(dir, name, 0, &fname);
807 	if (err)
808 		return err;
809 
810 	/*
811 	 * An immature stackable filesystem shows a race condition between lookup
812 	 * and create. If we have same task when doing lookup and create, it's
813 	 * definitely fine as expected by VFS normally. Otherwise, let's just
814 	 * verify on-disk dentry one more time, which guarantees filesystem
815 	 * consistency more.
816 	 */
817 	if (current != F2FS_I(dir)->task) {
818 		de = __f2fs_find_entry(dir, &fname, &page);
819 		F2FS_I(dir)->task = NULL;
820 	}
821 	if (de) {
822 		f2fs_put_page(page, 0);
823 		err = -EEXIST;
824 	} else if (IS_ERR(page)) {
825 		err = PTR_ERR(page);
826 	} else {
827 		err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
828 	}
829 	f2fs_free_filename(&fname);
830 	return err;
831 }
832 
833 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir,
834 					struct f2fs_filename *fname)
835 {
836 	struct page *page;
837 	int err = 0;
838 
839 	f2fs_down_write(&F2FS_I(inode)->i_sem);
840 	page = f2fs_init_inode_metadata(inode, dir, fname, NULL);
841 	if (IS_ERR(page)) {
842 		err = PTR_ERR(page);
843 		goto fail;
844 	}
845 	f2fs_put_page(page, 1);
846 
847 	clear_inode_flag(inode, FI_NEW_INODE);
848 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
849 fail:
850 	f2fs_up_write(&F2FS_I(inode)->i_sem);
851 	return err;
852 }
853 
854 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
855 {
856 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
857 
858 	f2fs_down_write(&F2FS_I(inode)->i_sem);
859 
860 	if (S_ISDIR(inode->i_mode))
861 		f2fs_i_links_write(dir, false);
862 	inode_set_ctime_current(inode);
863 
864 	f2fs_i_links_write(inode, false);
865 	if (S_ISDIR(inode->i_mode)) {
866 		f2fs_i_links_write(inode, false);
867 		f2fs_i_size_write(inode, 0);
868 	}
869 	f2fs_up_write(&F2FS_I(inode)->i_sem);
870 
871 	if (inode->i_nlink == 0)
872 		f2fs_add_orphan_inode(inode);
873 	else
874 		f2fs_release_orphan_inode(sbi);
875 }
876 
877 /*
878  * It only removes the dentry from the dentry page, corresponding name
879  * entry in name page does not need to be touched during deletion.
880  */
881 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
882 					struct inode *dir, struct inode *inode)
883 {
884 	struct	f2fs_dentry_block *dentry_blk;
885 	unsigned int bit_pos;
886 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
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, page, dir, inode);
896 
897 	lock_page(page);
898 	f2fs_wait_on_page_writeback(page, DATA, true, true);
899 
900 	dentry_blk = page_address(page);
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 	set_page_dirty(page);
910 
911 	if (bit_pos == NR_DENTRY_IN_BLOCK &&
912 		!f2fs_truncate_hole(dir, page->index, page->index + 1)) {
913 		f2fs_clear_page_cache_dirty_tag(page);
914 		clear_page_dirty_for_io(page);
915 		ClearPageUptodate(page);
916 		clear_page_private_all(page);
917 
918 		inode_dec_dirty_pages(dir);
919 		f2fs_remove_dirty_inode(dir);
920 	}
921 	f2fs_put_page(page, 1);
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 	struct page *dentry_page;
934 	unsigned int bit_pos;
935 	struct f2fs_dentry_block *dentry_blk;
936 	unsigned long nblock = dir_blocks(dir);
937 
938 	if (f2fs_has_inline_dentry(dir))
939 		return f2fs_empty_inline_dir(dir);
940 
941 	while (bidx < nblock) {
942 		pgoff_t next_pgofs;
943 
944 		dentry_page = f2fs_find_data_page(dir, bidx, &next_pgofs);
945 		if (IS_ERR(dentry_page)) {
946 			if (PTR_ERR(dentry_page) == -ENOENT) {
947 				bidx = next_pgofs;
948 				continue;
949 			} else {
950 				return false;
951 			}
952 		}
953 
954 		dentry_blk = page_address(dentry_page);
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_put_page(dentry_page, 0);
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 page *dentry_page = NULL;
1063 	struct file_ra_state *ra = &file->f_ra;
1064 	loff_t start_pos = ctx->pos;
1065 	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1066 	struct f2fs_dentry_ptr d;
1067 	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1068 	int err = 0;
1069 
1070 	if (IS_ENCRYPTED(inode)) {
1071 		err = fscrypt_prepare_readdir(inode);
1072 		if (err)
1073 			goto out;
1074 
1075 		err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1076 		if (err < 0)
1077 			goto out;
1078 	}
1079 
1080 	if (f2fs_has_inline_dentry(inode)) {
1081 		err = f2fs_read_inline_dir(file, ctx, &fstr);
1082 		goto out_free;
1083 	}
1084 
1085 	for (; n < npages; ctx->pos = n * NR_DENTRY_IN_BLOCK) {
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_page = f2fs_find_data_page(inode, n, &next_pgofs);
1101 		if (IS_ERR(dentry_page)) {
1102 			err = PTR_ERR(dentry_page);
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 = page_address(dentry_page);
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 		if (err) {
1119 			f2fs_put_page(dentry_page, 0);
1120 			break;
1121 		}
1122 
1123 		f2fs_put_page(dentry_page, 0);
1124 
1125 		n++;
1126 	}
1127 out_free:
1128 	fscrypt_fname_free_buffer(&fstr);
1129 out:
1130 	trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1131 	return err < 0 ? err : 0;
1132 }
1133 
1134 const struct file_operations f2fs_dir_operations = {
1135 	.llseek		= generic_file_llseek,
1136 	.read		= generic_read_dir,
1137 	.iterate_shared	= f2fs_readdir,
1138 	.fsync		= f2fs_sync_file,
1139 	.unlocked_ioctl	= f2fs_ioctl,
1140 #ifdef CONFIG_COMPAT
1141 	.compat_ioctl   = f2fs_compat_ioctl,
1142 #endif
1143 };
1144