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