xref: /openbmc/linux/fs/f2fs/dir.c (revision 94cdda6b)
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_CACHE_SIZE - 1))
21 							>> PAGE_CACHE_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 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 #define S_SHIFT 12
52 static unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
53 	[S_IFREG >> S_SHIFT]	= F2FS_FT_REG_FILE,
54 	[S_IFDIR >> S_SHIFT]	= F2FS_FT_DIR,
55 	[S_IFCHR >> S_SHIFT]	= F2FS_FT_CHRDEV,
56 	[S_IFBLK >> S_SHIFT]	= F2FS_FT_BLKDEV,
57 	[S_IFIFO >> S_SHIFT]	= F2FS_FT_FIFO,
58 	[S_IFSOCK >> S_SHIFT]	= F2FS_FT_SOCK,
59 	[S_IFLNK >> S_SHIFT]	= F2FS_FT_SYMLINK,
60 };
61 
62 void set_de_type(struct f2fs_dir_entry *de, umode_t mode)
63 {
64 	de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
65 }
66 
67 static unsigned long dir_block_index(unsigned int level,
68 				int dir_level, unsigned int idx)
69 {
70 	unsigned long i;
71 	unsigned long bidx = 0;
72 
73 	for (i = 0; i < level; i++)
74 		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
75 	bidx += idx * bucket_blocks(level);
76 	return bidx;
77 }
78 
79 static bool early_match_name(size_t namelen, f2fs_hash_t namehash,
80 				struct f2fs_dir_entry *de)
81 {
82 	if (le16_to_cpu(de->name_len) != namelen)
83 		return false;
84 
85 	if (de->hash_code != namehash)
86 		return false;
87 
88 	return true;
89 }
90 
91 static struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
92 				struct qstr *name, int *max_slots,
93 				struct page **res_page)
94 {
95 	struct f2fs_dentry_block *dentry_blk;
96 	struct f2fs_dir_entry *de;
97 	struct f2fs_dentry_ptr d;
98 
99 	dentry_blk = (struct f2fs_dentry_block *)kmap(dentry_page);
100 
101 	make_dentry_ptr(&d, (void *)dentry_blk, 1);
102 	de = find_target_dentry(name, max_slots, &d);
103 
104 	if (de)
105 		*res_page = dentry_page;
106 	else
107 		kunmap(dentry_page);
108 
109 	/*
110 	 * For the most part, it should be a bug when name_len is zero.
111 	 * We stop here for figuring out where the bugs has occurred.
112 	 */
113 	f2fs_bug_on(F2FS_P_SB(dentry_page), d.max < 0);
114 	return de;
115 }
116 
117 struct f2fs_dir_entry *find_target_dentry(struct qstr *name, int *max_slots,
118 						struct f2fs_dentry_ptr *d)
119 {
120 	struct f2fs_dir_entry *de;
121 	unsigned long bit_pos = 0;
122 	f2fs_hash_t namehash = f2fs_dentry_hash(name);
123 	int max_len = 0;
124 
125 	if (max_slots)
126 		*max_slots = 0;
127 	while (bit_pos < d->max) {
128 		if (!test_bit_le(bit_pos, d->bitmap)) {
129 			bit_pos++;
130 			max_len++;
131 			continue;
132 		}
133 
134 		de = &d->dentry[bit_pos];
135 		if (early_match_name(name->len, namehash, de) &&
136 			!memcmp(d->filename[bit_pos], name->name, name->len))
137 			goto found;
138 
139 		if (max_slots && max_len > *max_slots)
140 			*max_slots = max_len;
141 		max_len = 0;
142 
143 		/* remain bug on condition */
144 		if (unlikely(!de->name_len))
145 			d->max = -1;
146 
147 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
148 	}
149 
150 	de = NULL;
151 found:
152 	if (max_slots && max_len > *max_slots)
153 		*max_slots = max_len;
154 	return de;
155 }
156 
157 static struct f2fs_dir_entry *find_in_level(struct inode *dir,
158 			unsigned int level, struct qstr *name,
159 			f2fs_hash_t namehash, struct page **res_page)
160 {
161 	int s = GET_DENTRY_SLOTS(name->len);
162 	unsigned int nbucket, nblock;
163 	unsigned int bidx, end_block;
164 	struct page *dentry_page;
165 	struct f2fs_dir_entry *de = NULL;
166 	bool room = false;
167 	int max_slots;
168 
169 	f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH);
170 
171 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
172 	nblock = bucket_blocks(level);
173 
174 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
175 					le32_to_cpu(namehash) % nbucket);
176 	end_block = bidx + nblock;
177 
178 	for (; bidx < end_block; bidx++) {
179 		/* no need to allocate new dentry pages to all the indices */
180 		dentry_page = find_data_page(dir, bidx, true);
181 		if (IS_ERR(dentry_page)) {
182 			room = true;
183 			continue;
184 		}
185 
186 		de = find_in_block(dentry_page, name, &max_slots, res_page);
187 		if (de)
188 			break;
189 
190 		if (max_slots >= s)
191 			room = true;
192 		f2fs_put_page(dentry_page, 0);
193 	}
194 
195 	if (!de && room && F2FS_I(dir)->chash != namehash) {
196 		F2FS_I(dir)->chash = namehash;
197 		F2FS_I(dir)->clevel = level;
198 	}
199 
200 	return de;
201 }
202 
203 /*
204  * Find an entry in the specified directory with the wanted name.
205  * It returns the page where the entry was found (as a parameter - res_page),
206  * and the entry itself. Page is returned mapped and unlocked.
207  * Entry is guaranteed to be valid.
208  */
209 struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
210 			struct qstr *child, struct page **res_page)
211 {
212 	unsigned long npages = dir_blocks(dir);
213 	struct f2fs_dir_entry *de = NULL;
214 	f2fs_hash_t name_hash;
215 	unsigned int max_depth;
216 	unsigned int level;
217 
218 	*res_page = NULL;
219 
220 	if (f2fs_has_inline_dentry(dir))
221 		return find_in_inline_dir(dir, child, res_page);
222 
223 	if (npages == 0)
224 		return NULL;
225 
226 	name_hash = f2fs_dentry_hash(child);
227 	max_depth = F2FS_I(dir)->i_current_depth;
228 
229 	for (level = 0; level < max_depth; level++) {
230 		de = find_in_level(dir, level, child, name_hash, res_page);
231 		if (de)
232 			break;
233 	}
234 	if (!de && F2FS_I(dir)->chash != name_hash) {
235 		F2FS_I(dir)->chash = name_hash;
236 		F2FS_I(dir)->clevel = level - 1;
237 	}
238 	return de;
239 }
240 
241 struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
242 {
243 	struct page *page;
244 	struct f2fs_dir_entry *de;
245 	struct f2fs_dentry_block *dentry_blk;
246 
247 	if (f2fs_has_inline_dentry(dir))
248 		return f2fs_parent_inline_dir(dir, p);
249 
250 	page = get_lock_data_page(dir, 0);
251 	if (IS_ERR(page))
252 		return NULL;
253 
254 	dentry_blk = kmap(page);
255 	de = &dentry_blk->dentry[1];
256 	*p = page;
257 	unlock_page(page);
258 	return de;
259 }
260 
261 ino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
262 {
263 	ino_t res = 0;
264 	struct f2fs_dir_entry *de;
265 	struct page *page;
266 
267 	de = f2fs_find_entry(dir, qstr, &page);
268 	if (de) {
269 		res = le32_to_cpu(de->ino);
270 		f2fs_dentry_kunmap(dir, page);
271 		f2fs_put_page(page, 0);
272 	}
273 
274 	return res;
275 }
276 
277 void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
278 		struct page *page, struct inode *inode)
279 {
280 	enum page_type type = f2fs_has_inline_dentry(dir) ? NODE : DATA;
281 	lock_page(page);
282 	f2fs_wait_on_page_writeback(page, type);
283 	de->ino = cpu_to_le32(inode->i_ino);
284 	set_de_type(de, inode->i_mode);
285 	f2fs_dentry_kunmap(dir, page);
286 	set_page_dirty(page);
287 	dir->i_mtime = dir->i_ctime = CURRENT_TIME;
288 	mark_inode_dirty(dir);
289 
290 	f2fs_put_page(page, 1);
291 }
292 
293 static void init_dent_inode(const struct qstr *name, struct page *ipage)
294 {
295 	struct f2fs_inode *ri;
296 
297 	f2fs_wait_on_page_writeback(ipage, NODE);
298 
299 	/* copy name info. to this inode page */
300 	ri = F2FS_INODE(ipage);
301 	ri->i_namelen = cpu_to_le32(name->len);
302 	memcpy(ri->i_name, name->name, name->len);
303 	set_page_dirty(ipage);
304 }
305 
306 int update_dent_inode(struct inode *inode, const struct qstr *name)
307 {
308 	struct page *page;
309 
310 	page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
311 	if (IS_ERR(page))
312 		return PTR_ERR(page);
313 
314 	init_dent_inode(name, page);
315 	f2fs_put_page(page, 1);
316 
317 	return 0;
318 }
319 
320 void do_make_empty_dir(struct inode *inode, struct inode *parent,
321 					struct f2fs_dentry_ptr *d)
322 {
323 	struct f2fs_dir_entry *de;
324 
325 	de = &d->dentry[0];
326 	de->name_len = cpu_to_le16(1);
327 	de->hash_code = 0;
328 	de->ino = cpu_to_le32(inode->i_ino);
329 	memcpy(d->filename[0], ".", 1);
330 	set_de_type(de, inode->i_mode);
331 
332 	de = &d->dentry[1];
333 	de->hash_code = 0;
334 	de->name_len = cpu_to_le16(2);
335 	de->ino = cpu_to_le32(parent->i_ino);
336 	memcpy(d->filename[1], "..", 2);
337 	set_de_type(de, parent->i_mode);
338 
339 	test_and_set_bit_le(0, (void *)d->bitmap);
340 	test_and_set_bit_le(1, (void *)d->bitmap);
341 }
342 
343 static int make_empty_dir(struct inode *inode,
344 		struct inode *parent, struct page *page)
345 {
346 	struct page *dentry_page;
347 	struct f2fs_dentry_block *dentry_blk;
348 	struct f2fs_dentry_ptr d;
349 
350 	if (f2fs_has_inline_dentry(inode))
351 		return make_empty_inline_dir(inode, parent, page);
352 
353 	dentry_page = get_new_data_page(inode, page, 0, true);
354 	if (IS_ERR(dentry_page))
355 		return PTR_ERR(dentry_page);
356 
357 	dentry_blk = kmap_atomic(dentry_page);
358 
359 	make_dentry_ptr(&d, (void *)dentry_blk, 1);
360 	do_make_empty_dir(inode, parent, &d);
361 
362 	kunmap_atomic(dentry_blk);
363 
364 	set_page_dirty(dentry_page);
365 	f2fs_put_page(dentry_page, 1);
366 	return 0;
367 }
368 
369 struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
370 			const struct qstr *name, struct page *dpage)
371 {
372 	struct page *page;
373 	int err;
374 
375 	if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
376 		page = new_inode_page(inode);
377 		if (IS_ERR(page))
378 			return page;
379 
380 		if (S_ISDIR(inode->i_mode)) {
381 			err = make_empty_dir(inode, dir, page);
382 			if (err)
383 				goto error;
384 		}
385 
386 		err = f2fs_init_acl(inode, dir, page, dpage);
387 		if (err)
388 			goto put_error;
389 
390 		err = f2fs_init_security(inode, dir, name, page);
391 		if (err)
392 			goto put_error;
393 	} else {
394 		page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
395 		if (IS_ERR(page))
396 			return page;
397 
398 		set_cold_node(inode, page);
399 	}
400 
401 	if (name)
402 		init_dent_inode(name, page);
403 
404 	/*
405 	 * This file should be checkpointed during fsync.
406 	 * We lost i_pino from now on.
407 	 */
408 	if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
409 		file_lost_pino(inode);
410 		/*
411 		 * If link the tmpfile to alias through linkat path,
412 		 * we should remove this inode from orphan list.
413 		 */
414 		if (inode->i_nlink == 0)
415 			remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
416 		inc_nlink(inode);
417 	}
418 	return page;
419 
420 put_error:
421 	f2fs_put_page(page, 1);
422 error:
423 	/* once the failed inode becomes a bad inode, i_mode is S_IFREG */
424 	truncate_inode_pages(&inode->i_data, 0);
425 	truncate_blocks(inode, 0, false);
426 	remove_dirty_dir_inode(inode);
427 	remove_inode_page(inode);
428 	return ERR_PTR(err);
429 }
430 
431 void update_parent_metadata(struct inode *dir, struct inode *inode,
432 						unsigned int current_depth)
433 {
434 	if (inode && is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
435 		if (S_ISDIR(inode->i_mode)) {
436 			inc_nlink(dir);
437 			set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
438 		}
439 		clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
440 	}
441 	dir->i_mtime = dir->i_ctime = CURRENT_TIME;
442 	mark_inode_dirty(dir);
443 
444 	if (F2FS_I(dir)->i_current_depth != current_depth) {
445 		F2FS_I(dir)->i_current_depth = current_depth;
446 		set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
447 	}
448 
449 	if (inode && is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
450 		clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
451 }
452 
453 int room_for_filename(const void *bitmap, int slots, int max_slots)
454 {
455 	int bit_start = 0;
456 	int zero_start, zero_end;
457 next:
458 	zero_start = find_next_zero_bit_le(bitmap, max_slots, bit_start);
459 	if (zero_start >= max_slots)
460 		return max_slots;
461 
462 	zero_end = find_next_bit_le(bitmap, max_slots, zero_start);
463 	if (zero_end - zero_start >= slots)
464 		return zero_start;
465 
466 	bit_start = zero_end + 1;
467 
468 	if (zero_end + 1 >= max_slots)
469 		return max_slots;
470 	goto next;
471 }
472 
473 void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
474 				const struct qstr *name, f2fs_hash_t name_hash,
475 				unsigned int bit_pos)
476 {
477 	struct f2fs_dir_entry *de;
478 	int slots = GET_DENTRY_SLOTS(name->len);
479 	int i;
480 
481 	de = &d->dentry[bit_pos];
482 	de->hash_code = name_hash;
483 	de->name_len = cpu_to_le16(name->len);
484 	memcpy(d->filename[bit_pos], name->name, name->len);
485 	de->ino = cpu_to_le32(ino);
486 	set_de_type(de, mode);
487 	for (i = 0; i < slots; i++)
488 		test_and_set_bit_le(bit_pos + i, (void *)d->bitmap);
489 }
490 
491 /*
492  * Caller should grab and release a rwsem by calling f2fs_lock_op() and
493  * f2fs_unlock_op().
494  */
495 int __f2fs_add_link(struct inode *dir, const struct qstr *name,
496 				struct inode *inode, nid_t ino, umode_t mode)
497 {
498 	unsigned int bit_pos;
499 	unsigned int level;
500 	unsigned int current_depth;
501 	unsigned long bidx, block;
502 	f2fs_hash_t dentry_hash;
503 	unsigned int nbucket, nblock;
504 	size_t namelen = name->len;
505 	struct page *dentry_page = NULL;
506 	struct f2fs_dentry_block *dentry_blk = NULL;
507 	struct f2fs_dentry_ptr d;
508 	int slots = GET_DENTRY_SLOTS(namelen);
509 	struct page *page = NULL;
510 	int err = 0;
511 
512 	if (f2fs_has_inline_dentry(dir)) {
513 		err = f2fs_add_inline_entry(dir, name, inode, ino, mode);
514 		if (!err || err != -EAGAIN)
515 			return err;
516 		else
517 			err = 0;
518 	}
519 
520 	dentry_hash = f2fs_dentry_hash(name);
521 	level = 0;
522 	current_depth = F2FS_I(dir)->i_current_depth;
523 	if (F2FS_I(dir)->chash == dentry_hash) {
524 		level = F2FS_I(dir)->clevel;
525 		F2FS_I(dir)->chash = 0;
526 	}
527 
528 start:
529 	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
530 		return -ENOSPC;
531 
532 	/* Increase the depth, if required */
533 	if (level == current_depth)
534 		++current_depth;
535 
536 	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
537 	nblock = bucket_blocks(level);
538 
539 	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
540 				(le32_to_cpu(dentry_hash) % nbucket));
541 
542 	for (block = bidx; block <= (bidx + nblock - 1); block++) {
543 		dentry_page = get_new_data_page(dir, NULL, block, true);
544 		if (IS_ERR(dentry_page))
545 			return PTR_ERR(dentry_page);
546 
547 		dentry_blk = kmap(dentry_page);
548 		bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
549 						slots, NR_DENTRY_IN_BLOCK);
550 		if (bit_pos < NR_DENTRY_IN_BLOCK)
551 			goto add_dentry;
552 
553 		kunmap(dentry_page);
554 		f2fs_put_page(dentry_page, 1);
555 	}
556 
557 	/* Move to next level to find the empty slot for new dentry */
558 	++level;
559 	goto start;
560 add_dentry:
561 	f2fs_wait_on_page_writeback(dentry_page, DATA);
562 
563 	if (inode) {
564 		down_write(&F2FS_I(inode)->i_sem);
565 		page = init_inode_metadata(inode, dir, name, NULL);
566 		if (IS_ERR(page)) {
567 			err = PTR_ERR(page);
568 			goto fail;
569 		}
570 	}
571 
572 	make_dentry_ptr(&d, (void *)dentry_blk, 1);
573 	f2fs_update_dentry(ino, mode, &d, name, dentry_hash, bit_pos);
574 
575 	set_page_dirty(dentry_page);
576 
577 	if (inode) {
578 		/* we don't need to mark_inode_dirty now */
579 		F2FS_I(inode)->i_pino = dir->i_ino;
580 		update_inode(inode, page);
581 		f2fs_put_page(page, 1);
582 	}
583 
584 	update_parent_metadata(dir, inode, current_depth);
585 fail:
586 	if (inode)
587 		up_write(&F2FS_I(inode)->i_sem);
588 
589 	if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
590 		update_inode_page(dir);
591 		clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
592 	}
593 	kunmap(dentry_page);
594 	f2fs_put_page(dentry_page, 1);
595 	return err;
596 }
597 
598 int f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
599 {
600 	struct page *page;
601 	int err = 0;
602 
603 	down_write(&F2FS_I(inode)->i_sem);
604 	page = init_inode_metadata(inode, dir, NULL, NULL);
605 	if (IS_ERR(page)) {
606 		err = PTR_ERR(page);
607 		goto fail;
608 	}
609 	/* we don't need to mark_inode_dirty now */
610 	update_inode(inode, page);
611 	f2fs_put_page(page, 1);
612 
613 	clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
614 fail:
615 	up_write(&F2FS_I(inode)->i_sem);
616 	return err;
617 }
618 
619 void f2fs_drop_nlink(struct inode *dir, struct inode *inode, struct page *page)
620 {
621 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
622 
623 	down_write(&F2FS_I(inode)->i_sem);
624 
625 	if (S_ISDIR(inode->i_mode)) {
626 		drop_nlink(dir);
627 		if (page)
628 			update_inode(dir, page);
629 		else
630 			update_inode_page(dir);
631 	}
632 	inode->i_ctime = CURRENT_TIME;
633 
634 	drop_nlink(inode);
635 	if (S_ISDIR(inode->i_mode)) {
636 		drop_nlink(inode);
637 		i_size_write(inode, 0);
638 	}
639 	up_write(&F2FS_I(inode)->i_sem);
640 	update_inode_page(inode);
641 
642 	if (inode->i_nlink == 0)
643 		add_orphan_inode(sbi, inode->i_ino);
644 	else
645 		release_orphan_inode(sbi);
646 }
647 
648 /*
649  * It only removes the dentry from the dentry page, corresponding name
650  * entry in name page does not need to be touched during deletion.
651  */
652 void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
653 					struct inode *dir, struct inode *inode)
654 {
655 	struct	f2fs_dentry_block *dentry_blk;
656 	unsigned int bit_pos;
657 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
658 	int i;
659 
660 	if (f2fs_has_inline_dentry(dir))
661 		return f2fs_delete_inline_entry(dentry, page, dir, inode);
662 
663 	lock_page(page);
664 	f2fs_wait_on_page_writeback(page, DATA);
665 
666 	dentry_blk = page_address(page);
667 	bit_pos = dentry - dentry_blk->dentry;
668 	for (i = 0; i < slots; i++)
669 		clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
670 
671 	/* Let's check and deallocate this dentry page */
672 	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
673 			NR_DENTRY_IN_BLOCK,
674 			0);
675 	kunmap(page); /* kunmap - pair of f2fs_find_entry */
676 	set_page_dirty(page);
677 
678 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
679 
680 	if (inode)
681 		f2fs_drop_nlink(dir, inode, NULL);
682 
683 	if (bit_pos == NR_DENTRY_IN_BLOCK) {
684 		truncate_hole(dir, page->index, page->index + 1);
685 		clear_page_dirty_for_io(page);
686 		ClearPagePrivate(page);
687 		ClearPageUptodate(page);
688 		inode_dec_dirty_pages(dir);
689 	}
690 	f2fs_put_page(page, 1);
691 }
692 
693 bool f2fs_empty_dir(struct inode *dir)
694 {
695 	unsigned long bidx;
696 	struct page *dentry_page;
697 	unsigned int bit_pos;
698 	struct f2fs_dentry_block *dentry_blk;
699 	unsigned long nblock = dir_blocks(dir);
700 
701 	if (f2fs_has_inline_dentry(dir))
702 		return f2fs_empty_inline_dir(dir);
703 
704 	for (bidx = 0; bidx < nblock; bidx++) {
705 		dentry_page = get_lock_data_page(dir, bidx);
706 		if (IS_ERR(dentry_page)) {
707 			if (PTR_ERR(dentry_page) == -ENOENT)
708 				continue;
709 			else
710 				return false;
711 		}
712 
713 		dentry_blk = kmap_atomic(dentry_page);
714 		if (bidx == 0)
715 			bit_pos = 2;
716 		else
717 			bit_pos = 0;
718 		bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
719 						NR_DENTRY_IN_BLOCK,
720 						bit_pos);
721 		kunmap_atomic(dentry_blk);
722 
723 		f2fs_put_page(dentry_page, 1);
724 
725 		if (bit_pos < NR_DENTRY_IN_BLOCK)
726 			return false;
727 	}
728 	return true;
729 }
730 
731 bool f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
732 						unsigned int start_pos)
733 {
734 	unsigned char d_type = DT_UNKNOWN;
735 	unsigned int bit_pos;
736 	struct f2fs_dir_entry *de = NULL;
737 
738 	bit_pos = ((unsigned long)ctx->pos % d->max);
739 
740 	while (bit_pos < d->max) {
741 		bit_pos = find_next_bit_le(d->bitmap, d->max, bit_pos);
742 		if (bit_pos >= d->max)
743 			break;
744 
745 		de = &d->dentry[bit_pos];
746 		if (de->file_type < F2FS_FT_MAX)
747 			d_type = f2fs_filetype_table[de->file_type];
748 		else
749 			d_type = DT_UNKNOWN;
750 		if (!dir_emit(ctx, d->filename[bit_pos],
751 					le16_to_cpu(de->name_len),
752 					le32_to_cpu(de->ino), d_type))
753 			return true;
754 
755 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
756 		ctx->pos = start_pos + bit_pos;
757 	}
758 	return false;
759 }
760 
761 static int f2fs_readdir(struct file *file, struct dir_context *ctx)
762 {
763 	struct inode *inode = file_inode(file);
764 	unsigned long npages = dir_blocks(inode);
765 	struct f2fs_dentry_block *dentry_blk = NULL;
766 	struct page *dentry_page = NULL;
767 	struct file_ra_state *ra = &file->f_ra;
768 	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
769 	struct f2fs_dentry_ptr d;
770 
771 	if (f2fs_has_inline_dentry(inode))
772 		return f2fs_read_inline_dir(file, ctx);
773 
774 	/* readahead for multi pages of dir */
775 	if (npages - n > 1 && !ra_has_index(ra, n))
776 		page_cache_sync_readahead(inode->i_mapping, ra, file, n,
777 				min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
778 
779 	for (; n < npages; n++) {
780 		dentry_page = get_lock_data_page(inode, n);
781 		if (IS_ERR(dentry_page))
782 			continue;
783 
784 		dentry_blk = kmap(dentry_page);
785 
786 		make_dentry_ptr(&d, (void *)dentry_blk, 1);
787 
788 		if (f2fs_fill_dentries(ctx, &d, n * NR_DENTRY_IN_BLOCK))
789 			goto stop;
790 
791 		ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
792 		kunmap(dentry_page);
793 		f2fs_put_page(dentry_page, 1);
794 		dentry_page = NULL;
795 	}
796 stop:
797 	if (dentry_page && !IS_ERR(dentry_page)) {
798 		kunmap(dentry_page);
799 		f2fs_put_page(dentry_page, 1);
800 	}
801 
802 	return 0;
803 }
804 
805 const struct file_operations f2fs_dir_operations = {
806 	.llseek		= generic_file_llseek,
807 	.read		= generic_read_dir,
808 	.iterate	= f2fs_readdir,
809 	.fsync		= f2fs_sync_file,
810 	.unlocked_ioctl	= f2fs_ioctl,
811 };
812