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