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