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