xref: /openbmc/linux/fs/f2fs/dir.c (revision f89d416a)
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 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
582 				const struct qstr *name, f2fs_hash_t name_hash,
583 				unsigned int bit_pos)
584 {
585 	struct f2fs_dir_entry *de;
586 	int slots = GET_DENTRY_SLOTS(name->len);
587 	int i;
588 
589 	de = &d->dentry[bit_pos];
590 	de->hash_code = name_hash;
591 	de->name_len = cpu_to_le16(name->len);
592 	memcpy(d->filename[bit_pos], name->name, name->len);
593 	de->ino = cpu_to_le32(ino);
594 	set_de_type(de, mode);
595 	for (i = 0; i < slots; i++) {
596 		__set_bit_le(bit_pos + i, (void *)d->bitmap);
597 		/* avoid wrong garbage data for readdir */
598 		if (i)
599 			(de + i)->name_len = 0;
600 	}
601 }
602 
603 int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
604 				const struct qstr *orig_name,
605 				struct inode *inode, nid_t ino, umode_t mode)
606 {
607 	unsigned int bit_pos;
608 	unsigned int level;
609 	unsigned int current_depth;
610 	unsigned long bidx, block;
611 	f2fs_hash_t dentry_hash;
612 	unsigned int nbucket, nblock;
613 	struct page *dentry_page = NULL;
614 	struct f2fs_dentry_block *dentry_blk = NULL;
615 	struct f2fs_dentry_ptr d;
616 	struct page *page = NULL;
617 	int slots, err = 0;
618 
619 	level = 0;
620 	slots = GET_DENTRY_SLOTS(new_name->len);
621 	dentry_hash = f2fs_dentry_hash(dir, new_name, NULL);
622 
623 	current_depth = F2FS_I(dir)->i_current_depth;
624 	if (F2FS_I(dir)->chash == dentry_hash) {
625 		level = F2FS_I(dir)->clevel;
626 		F2FS_I(dir)->chash = 0;
627 	}
628 
629 start:
630 	if (time_to_inject(F2FS_I_SB(dir), FAULT_DIR_DEPTH)) {
631 		f2fs_show_injection_info(F2FS_I_SB(dir), FAULT_DIR_DEPTH);
632 		return -ENOSPC;
633 	}
634 
635 	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
636 		return -ENOSPC;
637 
638 	/* Increase the depth, if required */
639 	if (level == current_depth)
640 		++current_depth;
641 
642 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
643 	nblock = bucket_blocks(level);
644 
645 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
646 				(le32_to_cpu(dentry_hash) % nbucket));
647 
648 	for (block = bidx; block <= (bidx + nblock - 1); block++) {
649 		dentry_page = f2fs_get_new_data_page(dir, NULL, block, true);
650 		if (IS_ERR(dentry_page))
651 			return PTR_ERR(dentry_page);
652 
653 		dentry_blk = page_address(dentry_page);
654 		bit_pos = f2fs_room_for_filename(&dentry_blk->dentry_bitmap,
655 						slots, NR_DENTRY_IN_BLOCK);
656 		if (bit_pos < NR_DENTRY_IN_BLOCK)
657 			goto add_dentry;
658 
659 		f2fs_put_page(dentry_page, 1);
660 	}
661 
662 	/* Move to next level to find the empty slot for new dentry */
663 	++level;
664 	goto start;
665 add_dentry:
666 	f2fs_wait_on_page_writeback(dentry_page, DATA, true, true);
667 
668 	if (inode) {
669 		down_write(&F2FS_I(inode)->i_sem);
670 		page = f2fs_init_inode_metadata(inode, dir, new_name,
671 						orig_name, NULL);
672 		if (IS_ERR(page)) {
673 			err = PTR_ERR(page);
674 			goto fail;
675 		}
676 	}
677 
678 	make_dentry_ptr_block(NULL, &d, dentry_blk);
679 	f2fs_update_dentry(ino, mode, &d, new_name, dentry_hash, bit_pos);
680 
681 	set_page_dirty(dentry_page);
682 
683 	if (inode) {
684 		f2fs_i_pino_write(inode, dir->i_ino);
685 
686 		/* synchronize inode page's data from inode cache */
687 		if (is_inode_flag_set(inode, FI_NEW_INODE))
688 			f2fs_update_inode(inode, page);
689 
690 		f2fs_put_page(page, 1);
691 	}
692 
693 	f2fs_update_parent_metadata(dir, inode, current_depth);
694 fail:
695 	if (inode)
696 		up_write(&F2FS_I(inode)->i_sem);
697 
698 	f2fs_put_page(dentry_page, 1);
699 
700 	return err;
701 }
702 
703 int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
704 				struct inode *inode, nid_t ino, umode_t mode)
705 {
706 	struct qstr new_name;
707 	int err = -EAGAIN;
708 
709 	new_name.name = fname_name(fname);
710 	new_name.len = fname_len(fname);
711 
712 	if (f2fs_has_inline_dentry(dir))
713 		err = f2fs_add_inline_entry(dir, &new_name, fname->usr_fname,
714 							inode, ino, mode);
715 	if (err == -EAGAIN)
716 		err = f2fs_add_regular_entry(dir, &new_name, fname->usr_fname,
717 							inode, ino, mode);
718 
719 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
720 	return err;
721 }
722 
723 /*
724  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
725  * f2fs_unlock_op().
726  */
727 int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
728 				struct inode *inode, nid_t ino, umode_t mode)
729 {
730 	struct fscrypt_name fname;
731 	struct page *page = NULL;
732 	struct f2fs_dir_entry *de = NULL;
733 	int err;
734 
735 	err = fscrypt_setup_filename(dir, name, 0, &fname);
736 	if (err)
737 		return err;
738 
739 	/*
740 	 * An immature stakable filesystem shows a race condition between lookup
741 	 * and create. If we have same task when doing lookup and create, it's
742 	 * definitely fine as expected by VFS normally. Otherwise, let's just
743 	 * verify on-disk dentry one more time, which guarantees filesystem
744 	 * consistency more.
745 	 */
746 	if (current != F2FS_I(dir)->task) {
747 		de = __f2fs_find_entry(dir, &fname, &page);
748 		F2FS_I(dir)->task = NULL;
749 	}
750 	if (de) {
751 		f2fs_put_page(page, 0);
752 		err = -EEXIST;
753 	} else if (IS_ERR(page)) {
754 		err = PTR_ERR(page);
755 	} else {
756 		err = f2fs_add_dentry(dir, &fname, inode, ino, mode);
757 	}
758 	fscrypt_free_filename(&fname);
759 	return err;
760 }
761 
762 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
763 {
764 	struct page *page;
765 	int err = 0;
766 
767 	down_write(&F2FS_I(inode)->i_sem);
768 	page = f2fs_init_inode_metadata(inode, dir, NULL, NULL, NULL);
769 	if (IS_ERR(page)) {
770 		err = PTR_ERR(page);
771 		goto fail;
772 	}
773 	f2fs_put_page(page, 1);
774 
775 	clear_inode_flag(inode, FI_NEW_INODE);
776 	f2fs_update_time(F2FS_I_SB(inode), REQ_TIME);
777 fail:
778 	up_write(&F2FS_I(inode)->i_sem);
779 	return err;
780 }
781 
782 void f2fs_drop_nlink(struct inode *dir, struct inode *inode)
783 {
784 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
785 
786 	down_write(&F2FS_I(inode)->i_sem);
787 
788 	if (S_ISDIR(inode->i_mode))
789 		f2fs_i_links_write(dir, false);
790 	inode->i_ctime = current_time(inode);
791 
792 	f2fs_i_links_write(inode, false);
793 	if (S_ISDIR(inode->i_mode)) {
794 		f2fs_i_links_write(inode, false);
795 		f2fs_i_size_write(inode, 0);
796 	}
797 	up_write(&F2FS_I(inode)->i_sem);
798 
799 	if (inode->i_nlink == 0)
800 		f2fs_add_orphan_inode(inode);
801 	else
802 		f2fs_release_orphan_inode(sbi);
803 }
804 
805 /*
806  * It only removes the dentry from the dentry page, corresponding name
807  * entry in name page does not need to be touched during deletion.
808  */
809 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
810 					struct inode *dir, struct inode *inode)
811 {
812 	struct	f2fs_dentry_block *dentry_blk;
813 	unsigned int bit_pos;
814 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
815 	int i;
816 
817 	f2fs_update_time(F2FS_I_SB(dir), REQ_TIME);
818 
819 	if (F2FS_OPTION(F2FS_I_SB(dir)).fsync_mode == FSYNC_MODE_STRICT)
820 		f2fs_add_ino_entry(F2FS_I_SB(dir), dir->i_ino, TRANS_DIR_INO);
821 
822 	if (f2fs_has_inline_dentry(dir))
823 		return f2fs_delete_inline_entry(dentry, page, dir, inode);
824 
825 	lock_page(page);
826 	f2fs_wait_on_page_writeback(page, DATA, true, true);
827 
828 	dentry_blk = page_address(page);
829 	bit_pos = dentry - dentry_blk->dentry;
830 	for (i = 0; i < slots; i++)
831 		__clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
832 
833 	/* Let's check and deallocate this dentry page */
834 	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
835 			NR_DENTRY_IN_BLOCK,
836 			0);
837 	set_page_dirty(page);
838 
839 	dir->i_ctime = dir->i_mtime = current_time(dir);
840 	f2fs_mark_inode_dirty_sync(dir, false);
841 
842 	if (inode)
843 		f2fs_drop_nlink(dir, inode);
844 
845 	if (bit_pos == NR_DENTRY_IN_BLOCK &&
846 		!f2fs_truncate_hole(dir, page->index, page->index + 1)) {
847 		f2fs_clear_page_cache_dirty_tag(page);
848 		clear_page_dirty_for_io(page);
849 		f2fs_clear_page_private(page);
850 		ClearPageUptodate(page);
851 		clear_cold_data(page);
852 		inode_dec_dirty_pages(dir);
853 		f2fs_remove_dirty_inode(dir);
854 	}
855 	f2fs_put_page(page, 1);
856 }
857 
858 bool f2fs_empty_dir(struct inode *dir)
859 {
860 	unsigned long bidx;
861 	struct page *dentry_page;
862 	unsigned int bit_pos;
863 	struct f2fs_dentry_block *dentry_blk;
864 	unsigned long nblock = dir_blocks(dir);
865 
866 	if (f2fs_has_inline_dentry(dir))
867 		return f2fs_empty_inline_dir(dir);
868 
869 	for (bidx = 0; bidx < nblock; bidx++) {
870 		dentry_page = f2fs_get_lock_data_page(dir, bidx, false);
871 		if (IS_ERR(dentry_page)) {
872 			if (PTR_ERR(dentry_page) == -ENOENT)
873 				continue;
874 			else
875 				return false;
876 		}
877 
878 		dentry_blk = page_address(dentry_page);
879 		if (bidx == 0)
880 			bit_pos = 2;
881 		else
882 			bit_pos = 0;
883 		bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
884 						NR_DENTRY_IN_BLOCK,
885 						bit_pos);
886 
887 		f2fs_put_page(dentry_page, 1);
888 
889 		if (bit_pos < NR_DENTRY_IN_BLOCK)
890 			return false;
891 	}
892 	return true;
893 }
894 
895 int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
896 			unsigned int start_pos, struct fscrypt_str *fstr)
897 {
898 	unsigned char d_type = DT_UNKNOWN;
899 	unsigned int bit_pos;
900 	struct f2fs_dir_entry *de = NULL;
901 	struct fscrypt_str de_name = FSTR_INIT(NULL, 0);
902 	struct f2fs_sb_info *sbi = F2FS_I_SB(d->inode);
903 	struct blk_plug plug;
904 	bool readdir_ra = sbi->readdir_ra == 1;
905 	int err = 0;
906 
907 	bit_pos = ((unsigned long)ctx->pos % d->max);
908 
909 	if (readdir_ra)
910 		blk_start_plug(&plug);
911 
912 	while (bit_pos < d->max) {
913 		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
914 		if (bit_pos >= d->max)
915 			break;
916 
917 		de = &d->dentry[bit_pos];
918 		if (de->name_len == 0) {
919 			bit_pos++;
920 			ctx->pos = start_pos + bit_pos;
921 			printk_ratelimited(
922 				"%sF2FS-fs (%s): invalid namelen(0), ino:%u, run fsck to fix.",
923 				KERN_WARNING, sbi->sb->s_id,
924 				le32_to_cpu(de->ino));
925 			set_sbi_flag(sbi, SBI_NEED_FSCK);
926 			continue;
927 		}
928 
929 		d_type = f2fs_get_de_type(de);
930 
931 		de_name.name = d->filename[bit_pos];
932 		de_name.len = le16_to_cpu(de->name_len);
933 
934 		/* check memory boundary before moving forward */
935 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
936 		if (unlikely(bit_pos > d->max ||
937 				le16_to_cpu(de->name_len) > F2FS_NAME_LEN)) {
938 			f2fs_warn(sbi, "%s: corrupted namelen=%d, run fsck to fix.",
939 				  __func__, le16_to_cpu(de->name_len));
940 			set_sbi_flag(sbi, SBI_NEED_FSCK);
941 			err = -EFSCORRUPTED;
942 			goto out;
943 		}
944 
945 		if (IS_ENCRYPTED(d->inode)) {
946 			int save_len = fstr->len;
947 
948 			err = fscrypt_fname_disk_to_usr(d->inode,
949 						(u32)le32_to_cpu(de->hash_code),
950 						0, &de_name, fstr);
951 			if (err)
952 				goto out;
953 
954 			de_name = *fstr;
955 			fstr->len = save_len;
956 		}
957 
958 		if (!dir_emit(ctx, de_name.name, de_name.len,
959 					le32_to_cpu(de->ino), d_type)) {
960 			err = 1;
961 			goto out;
962 		}
963 
964 		if (readdir_ra)
965 			f2fs_ra_node_page(sbi, le32_to_cpu(de->ino));
966 
967 		ctx->pos = start_pos + bit_pos;
968 	}
969 out:
970 	if (readdir_ra)
971 		blk_finish_plug(&plug);
972 	return err;
973 }
974 
975 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
976 {
977 	struct inode *inode = file_inode(file);
978 	unsigned long npages = dir_blocks(inode);
979 	struct f2fs_dentry_block *dentry_blk = NULL;
980 	struct page *dentry_page = NULL;
981 	struct file_ra_state *ra = &file->f_ra;
982 	loff_t start_pos = ctx->pos;
983 	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
984 	struct f2fs_dentry_ptr d;
985 	struct fscrypt_str fstr = FSTR_INIT(NULL, 0);
986 	int err = 0;
987 
988 	if (IS_ENCRYPTED(inode)) {
989 		err = fscrypt_get_encryption_info(inode);
990 		if (err && err != -ENOKEY)
991 			goto out;
992 
993 		err = fscrypt_fname_alloc_buffer(inode, F2FS_NAME_LEN, &fstr);
994 		if (err < 0)
995 			goto out;
996 	}
997 
998 	if (f2fs_has_inline_dentry(inode)) {
999 		err = f2fs_read_inline_dir(file, ctx, &fstr);
1000 		goto out_free;
1001 	}
1002 
1003 	for (; n < npages; n++, ctx->pos = n * NR_DENTRY_IN_BLOCK) {
1004 
1005 		/* allow readdir() to be interrupted */
1006 		if (fatal_signal_pending(current)) {
1007 			err = -ERESTARTSYS;
1008 			goto out_free;
1009 		}
1010 		cond_resched();
1011 
1012 		/* readahead for multi pages of dir */
1013 		if (npages - n > 1 && !ra_has_index(ra, n))
1014 			page_cache_sync_readahead(inode->i_mapping, ra, file, n,
1015 				min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
1016 
1017 		dentry_page = f2fs_find_data_page(inode, n);
1018 		if (IS_ERR(dentry_page)) {
1019 			err = PTR_ERR(dentry_page);
1020 			if (err == -ENOENT) {
1021 				err = 0;
1022 				continue;
1023 			} else {
1024 				goto out_free;
1025 			}
1026 		}
1027 
1028 		dentry_blk = page_address(dentry_page);
1029 
1030 		make_dentry_ptr_block(inode, &d, dentry_blk);
1031 
1032 		err = f2fs_fill_dentries(ctx, &d,
1033 				n * NR_DENTRY_IN_BLOCK, &fstr);
1034 		if (err) {
1035 			f2fs_put_page(dentry_page, 0);
1036 			break;
1037 		}
1038 
1039 		f2fs_put_page(dentry_page, 0);
1040 	}
1041 out_free:
1042 	fscrypt_fname_free_buffer(&fstr);
1043 out:
1044 	trace_f2fs_readdir(inode, start_pos, ctx->pos, err);
1045 	return err < 0 ? err : 0;
1046 }
1047 
1048 static int f2fs_dir_open(struct inode *inode, struct file *filp)
1049 {
1050 	if (IS_ENCRYPTED(inode))
1051 		return fscrypt_get_encryption_info(inode) ? -EACCES : 0;
1052 	return 0;
1053 }
1054 
1055 const struct file_operations f2fs_dir_operations = {
1056 	.llseek		= generic_file_llseek,
1057 	.read		= generic_read_dir,
1058 	.iterate_shared	= f2fs_readdir,
1059 	.fsync		= f2fs_sync_file,
1060 	.open		= f2fs_dir_open,
1061 	.unlocked_ioctl	= f2fs_ioctl,
1062 #ifdef CONFIG_COMPAT
1063 	.compat_ioctl   = f2fs_compat_ioctl,
1064 #endif
1065 };
1066 
1067 #ifdef CONFIG_UNICODE
1068 static int f2fs_d_compare(const struct dentry *dentry, unsigned int len,
1069 			  const char *str, const struct qstr *name)
1070 {
1071 	struct qstr qstr = {.name = str, .len = len };
1072 
1073 	if (!IS_CASEFOLDED(dentry->d_parent->d_inode)) {
1074 		if (len != name->len)
1075 			return -1;
1076 		return memcmp(str, name, len);
1077 	}
1078 
1079 	return f2fs_ci_compare(dentry->d_parent->d_inode, name, &qstr, false);
1080 }
1081 
1082 static int f2fs_d_hash(const struct dentry *dentry, struct qstr *str)
1083 {
1084 	struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
1085 	const struct unicode_map *um = sbi->s_encoding;
1086 	unsigned char *norm;
1087 	int len, ret = 0;
1088 
1089 	if (!IS_CASEFOLDED(dentry->d_inode))
1090 		return 0;
1091 
1092 	norm = f2fs_kmalloc(sbi, PATH_MAX, GFP_ATOMIC);
1093 	if (!norm)
1094 		return -ENOMEM;
1095 
1096 	len = utf8_casefold(um, str, norm, PATH_MAX);
1097 	if (len < 0) {
1098 		if (f2fs_has_strict_mode(sbi))
1099 			ret = -EINVAL;
1100 		goto out;
1101 	}
1102 	str->hash = full_name_hash(dentry, norm, len);
1103 out:
1104 	kvfree(norm);
1105 	return ret;
1106 }
1107 
1108 const struct dentry_operations f2fs_dentry_ops = {
1109 	.d_hash = f2fs_d_hash,
1110 	.d_compare = f2fs_d_compare,
1111 };
1112 #endif
1113