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