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