xref: /openbmc/linux/fs/f2fs/dir.c (revision 0545810f)
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 	dir->i_mtime = dir->i_ctime = current_time(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 	dir->i_mtime = dir->i_ctime = current_time(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 		err = f2fs_add_inline_entry(dir, fname, inode, ino, mode);
780 	if (err == -EAGAIN)
781 		err = f2fs_add_regular_entry(dir, fname, inode, ino, mode);
782 
783 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
784 	return err;
785 }
786 
787 /*
788  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
789  * f2fs_unlock_op().
790  */
791 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
792 				struct inode *inode, nid_t ino, umode_t mode)
793 {
794 	struct f2fs_filename fname;
795 	struct page *page = NULL;
796 	struct f2fs_dir_entry *de = NULL;
797 	int err;
798 
799 	err = f2fs_setup_filename(dir, name, 0, &fname);
800 	if (err)
801 		return err;
802 
803 	/*
804 	 * An immature stackable filesystem shows a race condition between lookup
805 	 * and create. If we have same task when doing lookup and create, it's
806 	 * definitely fine as expected by VFS normally. Otherwise, let's just
807 	 * verify on-disk dentry one more time, which guarantees filesystem
808 	 * consistency more.
809 	 */
810 	if (current != F2FS_I(dir)->task) {
811 		de = __f2fs_find_entry(dir, &fname, &page);
812 		F2FS_I(dir)->task = NULL;
813 	}
814 	if (de) {
815 		f2fs_put_page(page, 0);
816 		err = -EEXIST;
817 	} else if (IS_ERR(page)) {
818 		err = PTR_ERR(page);
819 	} else {
820 		err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
821 	}
822 	f2fs_free_filename(&fname);
823 	return err;
824 }
825 
826 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
827 {
828 	struct page *page;
829 	int err = 0;
830 
831 	f2fs_down_write(&F2FS_I(inode)->i_sem);
832 	page = f2fs_init_inode_metadata(inode, dir, NULL, NULL);
833 	if (IS_ERR(page)) {
834 		err = PTR_ERR(page);
835 		goto fail;
836 	}
837 	f2fs_put_page(page, 1);
838 
839 	clear_inode_flag(inode, FI_NEW_INODE);
840 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
841 fail:
842 	f2fs_up_write(&F2FS_I(inode)->i_sem);
843 	return err;
844 }
845 
846 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
847 {
848 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
849 
850 	f2fs_down_write(&F2FS_I(inode)->i_sem);
851 
852 	if (S_ISDIR(inode->i_mode))
853 		f2fs_i_links_write(dir, false);
854 	inode->i_ctime = current_time(inode);
855 
856 	f2fs_i_links_write(inode, false);
857 	if (S_ISDIR(inode->i_mode)) {
858 		f2fs_i_links_write(inode, false);
859 		f2fs_i_size_write(inode, 0);
860 	}
861 	f2fs_up_write(&F2FS_I(inode)->i_sem);
862 
863 	if (inode->i_nlink == 0)
864 		f2fs_add_orphan_inode(inode);
865 	else
866 		f2fs_release_orphan_inode(sbi);
867 }
868 
869 /*
870  * It only removes the dentry from the dentry page, corresponding name
871  * entry in name page does not need to be touched during deletion.
872  */
873 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
874 					struct inode *dir, struct inode *inode)
875 {
876 	struct	f2fs_dentry_block *dentry_blk;
877 	unsigned int bit_pos;
878 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
879 	int i;
880 
881 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
882 
883 	if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
884 		f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
885 
886 	if (f2fs_has_inline_dentry(dir))
887 		return f2fs_delete_inline_entry(dentry, page, dir, inode);
888 
889 	lock_page(page);
890 	f2fs_wait_on_page_writeback(page, DATA, true, true);
891 
892 	dentry_blk = page_address(page);
893 	bit_pos = dentry - dentry_blk->dentry;
894 	for (i = 0; i < slots; i++)
895 		__clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
896 
897 	/* Let's check and deallocate this dentry page */
898 	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
899 			NR_DENTRY_IN_BLOCK,
900 			0);
901 	set_page_dirty(page);
902 
903 	if (bit_pos == NR_DENTRY_IN_BLOCK &&
904 		!f2fs_truncate_hole(dir, page->index, page->index + 1)) {
905 		f2fs_clear_page_cache_dirty_tag(page);
906 		clear_page_dirty_for_io(page);
907 		ClearPageUptodate(page);
908 		clear_page_private_all(page);
909 
910 		inode_dec_dirty_pages(dir);
911 		f2fs_remove_dirty_inode(dir);
912 	}
913 	f2fs_put_page(page, 1);
914 
915 	dir->i_ctime = dir->i_mtime = current_time(dir);
916 	f2fs_mark_inode_dirty_sync(dir, false);
917 
918 	if (inode)
919 		f2fs_drop_nlink(dir, inode);
920 }
921 
922 bool f2fs_empty_dir(struct inode *dir)
923 {
924 	unsigned long bidx = 0;
925 	struct page *dentry_page;
926 	unsigned int bit_pos;
927 	struct f2fs_dentry_block *dentry_blk;
928 	unsigned long nblock = dir_blocks(dir);
929 
930 	if (f2fs_has_inline_dentry(dir))
931 		return f2fs_empty_inline_dir(dir);
932 
933 	while (bidx < nblock) {
934 		pgoff_t next_pgofs;
935 
936 		dentry_page = f2fs_find_data_page(dir, bidx, &next_pgofs);
937 		if (IS_ERR(dentry_page)) {
938 			if (PTR_ERR(dentry_page) == -ENOENT) {
939 				bidx = next_pgofs;
940 				continue;
941 			} else {
942 				return false;
943 			}
944 		}
945 
946 		dentry_blk = page_address(dentry_page);
947 		if (bidx == 0)
948 			bit_pos = 2;
949 		else
950 			bit_pos = 0;
951 		bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
952 						NR_DENTRY_IN_BLOCK,
953 						bit_pos);
954 
955 		f2fs_put_page(dentry_page, 0);
956 
957 		if (bit_pos < NR_DENTRY_IN_BLOCK)
958 			return false;
959 
960 		bidx++;
961 	}
962 	return true;
963 }
964 
965 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
966 			unsigned int start_pos, struct fscrypt_str *fstr)
967 {
968 	unsigned char d_type = DT_UNKNOWN;
969 	unsigned int bit_pos;
970 	struct f2fs_dir_entry *de = NULL;
971 	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
972 	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
973 	struct blk_plug plug;
974 	bool readdir_ra = sbi->readdir_ra;
975 	bool found_valid_dirent = false;
976 	int err = 0;
977 
978 	bit_pos = ((unsigned long)ctx->pos % d->max);
979 
980 	if (readdir_ra)
981 		blk_start_plug(&plug);
982 
983 	while (bit_pos < d->max) {
984 		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
985 		if (bit_pos >= d->max)
986 			break;
987 
988 		de = &d->dentry[bit_pos];
989 		if (de->name_len == 0) {
990 			if (found_valid_dirent || !bit_pos) {
991 				printk_ratelimited(
992 					"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
993 					KERN_WARNING, sbi->sb->s_id,
994 					le32_to_cpu(de->ino));
995 				set_sbi_flag(sbi, SBI_NEED_FSCK);
996 			}
997 			bit_pos++;
998 			ctx->pos = start_pos + bit_pos;
999 			continue;
1000 		}
1001 
1002 		d_type = fs_ftype_to_dtype(de->file_type);
1003 
1004 		de_name.name = d->filename[bit_pos];
1005 		de_name.len = le16_to_cpu(de->name_len);
1006 
1007 		/* check memory boundary before moving forward */
1008 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1009 		if (unlikely(bit_pos > d->max ||
1010 				le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
1011 			f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
1012 				  __func__, le16_to_cpu(de->name_len));
1013 			set_sbi_flag(sbi, SBI_NEED_FSCK);
1014 			err = -EFSCORRUPTED;
1015 			f2fs_handle_error(sbi, ERROR_CORRUPTED_DIRENT);
1016 			goto out;
1017 		}
1018 
1019 		if (IS_ENCRYPTED(d->inode)) {
1020 			int save_len = fstr->len;
1021 
1022 			err = fscrypt_fname_disk_to_usr(d->inode,
1023 						(u32)le32_to_cpu(de->hash_code),
1024 						0, &de_name, fstr);
1025 			if (err)
1026 				goto out;
1027 
1028 			de_name = *fstr;
1029 			fstr->len = save_len;
1030 		}
1031 
1032 		if (!dir_emit(ctx, de_name.name, de_name.len,
1033 					le32_to_cpu(de->ino), d_type)) {
1034 			err = 1;
1035 			goto out;
1036 		}
1037 
1038 		if (readdir_ra)
1039 			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
1040 
1041 		ctx->pos = start_pos + bit_pos;
1042 		found_valid_dirent = true;
1043 	}
1044 out:
1045 	if (readdir_ra)
1046 		blk_finish_plug(&plug);
1047 	return err;
1048 }
1049 
1050 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
1051 {
1052 	struct inode *inode = file_inode(file);
1053 	unsigned long npages = dir_blocks(inode);
1054 	struct f2fs_dentry_block *dentry_blk = NULL;
1055 	struct page *dentry_page = NULL;
1056 	struct file_ra_state *ra = &file->f_ra;
1057 	loff_t start_pos = ctx->pos;
1058 	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
1059 	struct f2fs_dentry_ptr d;
1060 	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
1061 	int err = 0;
1062 
1063 	if (IS_ENCRYPTED(inode)) {
1064 		err = fscrypt_prepare_readdir(inode);
1065 		if (err)
1066 			goto out;
1067 
1068 		err = fscrypt_fname_alloc_buffer(F2FS_NAME_LEN, &fstr);
1069 		if (err < 0)
1070 			goto out;
1071 	}
1072 
1073 	if (f2fs_has_inline_dentry(inode)) {
1074 		err = f2fs_read_inline_dir(file, ctx, &fstr);
1075 		goto out_free;
1076 	}
1077 
1078 	for (; n < npages; ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1079 		pgoff_t next_pgofs;
1080 
1081 		/* allow readdir() to be interrupted */
1082 		if (fatal_signal_pending(current)) {
1083 			err = -ERESTARTSYS;
1084 			goto out_free;
1085 		}
1086 		cond_resched();
1087 
1088 		/* readahead for multi pages of dir */
1089 		if (npages - n > 1 && !ra_has_index(ra, n))
1090 			page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1091 				min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1092 
1093 		dentry_page = f2fs_find_data_page(inode, n, &next_pgofs);
1094 		if (IS_ERR(dentry_page)) {
1095 			err = PTR_ERR(dentry_page);
1096 			if (err == -ENOENT) {
1097 				err = 0;
1098 				n = next_pgofs;
1099 				continue;
1100 			} else {
1101 				goto out_free;
1102 			}
1103 		}
1104 
1105 		dentry_blk = page_address(dentry_page);
1106 
1107 		make_dentry_ptr_block(inode, &d, dentry_blk);
1108 
1109 		err = f2fs_fill_dentries(ctx, &d,
1110 				n * NR_DENTRY_IN_BLOCK, &fstr);
1111 		if (err) {
1112 			f2fs_put_page(dentry_page, 0);
1113 			break;
1114 		}
1115 
1116 		f2fs_put_page(dentry_page, 0);
1117 
1118 		n++;
1119 	}
1120 out_free:
1121 	fscrypt_fname_free_buffer(&fstr);
1122 out:
1123 	trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1124 	return err < 0 ? err : 0;
1125 }
1126 
1127 const struct file_operations f2fs_dir_operations = {
1128 	.llseek		= generic_file_llseek,
1129 	.read		= generic_read_dir,
1130 	.iterate_shared	= f2fs_readdir,
1131 	.fsync		= f2fs_sync_file,
1132 	.unlocked_ioctl	= f2fs_ioctl,
1133 #ifdef CONFIG_COMPAT
1134 	.compat_ioctl   = f2fs_compat_ioctl,
1135 #endif
1136 };
1137