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