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