10a8165d7c2cf1395059db20ab07665baf3758fcdJaegeuk Kim/*
26b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * fs/f2fs/dir.c
36b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim *
46b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * Copyright (c) 2012 Samsung Electronics Co., Ltd.
56b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim *             http://www.samsung.com/
66b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim *
76b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * This program is free software; you can redistribute it and/or modify
86b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * it under the terms of the GNU General Public License version 2 as
96b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * published by the Free Software Foundation.
106b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim */
116b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim#include <linux/fs.h>
126b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim#include <linux/f2fs_fs.h>
136b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim#include "f2fs.h"
14398b1ac5a57219823f942a8d3665b27ab99354deJaegeuk Kim#include "node.h"
156b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim#include "acl.h"
168ae8f1627f39bae505b90cade50cd8a911b8bda6Jaegeuk Kim#include "xattr.h"
176b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
186b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic unsigned long dir_blocks(struct inode *inode)
196b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
206b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return ((unsigned long long) (i_size_read(inode) + PAGE_CACHE_SIZE - 1))
216b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim							>> PAGE_CACHE_SHIFT;
226b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
236b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
243843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kimstatic unsigned int dir_buckets(unsigned int level, int dir_level)
256b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
26bfec07d0f8ed78b10df3ca3bc23e27de1166ea45Chao Yu	if (level + dir_level < MAX_DIR_HASH_DEPTH / 2)
273843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim		return 1 << (level + dir_level);
286b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	else
29bfec07d0f8ed78b10df3ca3bc23e27de1166ea45Chao Yu		return MAX_DIR_BUCKETS;
306b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
316b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
326b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic unsigned int bucket_blocks(unsigned int level)
336b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
346b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (level < MAX_DIR_HASH_DEPTH / 2)
356b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return 2;
366b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	else
376b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return 4;
386b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
396b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
406b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic unsigned char f2fs_filetype_table[F2FS_FT_MAX] = {
416b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_UNKNOWN]	= DT_UNKNOWN,
426b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_REG_FILE]	= DT_REG,
436b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_DIR]		= DT_DIR,
446b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_CHRDEV]	= DT_CHR,
456b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_BLKDEV]	= DT_BLK,
466b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_FIFO]		= DT_FIFO,
476b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_SOCK]		= DT_SOCK,
486b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[F2FS_FT_SYMLINK]	= DT_LNK,
496b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim};
506b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
516b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim#define S_SHIFT 12
526b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic unsigned char f2fs_type_by_mode[S_IFMT >> S_SHIFT] = {
536b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[S_IFREG >> S_SHIFT]	= F2FS_FT_REG_FILE,
546b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[S_IFDIR >> S_SHIFT]	= F2FS_FT_DIR,
556b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[S_IFCHR >> S_SHIFT]	= F2FS_FT_CHRDEV,
566b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[S_IFBLK >> S_SHIFT]	= F2FS_FT_BLKDEV,
576b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[S_IFIFO >> S_SHIFT]	= F2FS_FT_FIFO,
586b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[S_IFSOCK >> S_SHIFT]	= F2FS_FT_SOCK,
596b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	[S_IFLNK >> S_SHIFT]	= F2FS_FT_SYMLINK,
606b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim};
616b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
626b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic void set_de_type(struct f2fs_dir_entry *de, struct inode *inode)
636b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
640ecc833bac594099505a090cbca6ccd5b83d5975Al Viro	umode_t mode = inode->i_mode;
656b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de->file_type = f2fs_type_by_mode[(mode & S_IFMT) >> S_SHIFT];
666b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
676b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
683843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kimstatic unsigned long dir_block_index(unsigned int level,
693843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim				int dir_level, unsigned int idx)
706b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
716b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned long i;
726b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned long bidx = 0;
736b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
746b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	for (i = 0; i < level; i++)
753843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim		bidx += dir_buckets(i, dir_level) * bucket_blocks(i);
766b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	bidx += idx * bucket_blocks(level);
776b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return bidx;
786b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
796b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
80eee6160f2e324f0f6a626159fa1120caad31a6beGu Zhengstatic bool early_match_name(size_t namelen, f2fs_hash_t namehash,
81eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng				struct f2fs_dir_entry *de)
826b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
836b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (le16_to_cpu(de->name_len) != namelen)
846b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return false;
856b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
8625ca923b2a766b9c93b63777ead351137533a623Jaegeuk Kim	if (de->hash_code != namehash)
876b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return false;
886b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
896b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return true;
906b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
916b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
926b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic struct f2fs_dir_entry *find_in_block(struct page *dentry_page,
93eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng			struct qstr *name, int *max_slots,
946b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			f2fs_hash_t namehash, struct page **res_page)
956b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
966b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dir_entry *de;
975d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim	unsigned long bit_pos = 0;
986b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dentry_block *dentry_blk = kmap(dentry_page);
9920f70751c6b4ac5055be9a0d8a3d3189a81afc5aJaegeuk Kim	const void *dentry_bits = &dentry_blk->dentry_bitmap;
1005d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim	int max_len = 0;
1016b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1026b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	while (bit_pos < NR_DENTRY_IN_BLOCK) {
10320f70751c6b4ac5055be9a0d8a3d3189a81afc5aJaegeuk Kim		if (!test_bit_le(bit_pos, dentry_bits)) {
1045d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim			if (bit_pos == 0)
1055d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim				max_len = 1;
10620f70751c6b4ac5055be9a0d8a3d3189a81afc5aJaegeuk Kim			else if (!test_bit_le(bit_pos - 1, dentry_bits))
1075d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim				max_len++;
1085d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim			bit_pos++;
1095d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim			continue;
1105d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim		}
11120f70751c6b4ac5055be9a0d8a3d3189a81afc5aJaegeuk Kim		de = &dentry_blk->dentry[bit_pos];
112eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng		if (early_match_name(name->len, namehash, de)) {
1136b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			if (!memcmp(dentry_blk->filename[bit_pos],
114eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng							name->name,
115eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng							name->len)) {
1166b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim				*res_page = dentry_page;
1176b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim				goto found;
1186b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			}
1196b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		}
1205d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim		if (max_len > *max_slots) {
1215d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim			*max_slots = max_len;
1225d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim			max_len = 0;
1235d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim		}
12481e366f87f52c62671fb1d8050572e68dbcf1d22Chao Yu
12581e366f87f52c62671fb1d8050572e68dbcf1d22Chao Yu		/*
12681e366f87f52c62671fb1d8050572e68dbcf1d22Chao Yu		 * For the most part, it should be a bug when name_len is zero.
127e1c42045203071c4634b89e696037357810d3083arter		 * We stop here for figuring out where the bugs has occurred.
12881e366f87f52c62671fb1d8050572e68dbcf1d22Chao Yu		 */
1299850cf4a8908886370b1f15aacf83d291f098c72Jaegeuk Kim		f2fs_bug_on(F2FS_P_SB(dentry_page), !de->name_len);
13081e366f87f52c62671fb1d8050572e68dbcf1d22Chao Yu
1315d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
1326b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
1336b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1346b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de = NULL;
1356b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	kunmap(dentry_page);
1366b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimfound:
1375d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim	if (max_len > *max_slots)
1385d0c667121bfc8be76d1580f485bddbe73465d1aJaegeuk Kim		*max_slots = max_len;
1396b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return de;
1406b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
1416b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1426b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic struct f2fs_dir_entry *find_in_level(struct inode *dir,
143eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng			unsigned int level, struct qstr *name,
1446b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			f2fs_hash_t namehash, struct page **res_page)
1456b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
146eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng	int s = GET_DENTRY_SLOTS(name->len);
1476b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int nbucket, nblock;
1486b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int bidx, end_block;
1496b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct page *dentry_page;
1506b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dir_entry *de = NULL;
1516b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	bool room = false;
1526b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	int max_slots = 0;
1536b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1549850cf4a8908886370b1f15aacf83d291f098c72Jaegeuk Kim	f2fs_bug_on(F2FS_I_SB(dir), level > MAX_DIR_HASH_DEPTH);
1556b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1563843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
1576b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	nblock = bucket_blocks(level);
1586b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1593843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
1603843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim					le32_to_cpu(namehash) % nbucket);
1616b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	end_block = bidx + nblock;
1626b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1636b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	for (; bidx < end_block; bidx++) {
1646b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		/* no need to allocate new dentry pages to all the indices */
165c718379b6b0954a04a153d7e5dc8b3136a301ee6Jaegeuk Kim		dentry_page = find_data_page(dir, bidx, true);
1666b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (IS_ERR(dentry_page)) {
1676b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			room = true;
1686b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			continue;
1696b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		}
1706b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
171eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng		de = find_in_block(dentry_page, name, &max_slots,
172eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng					namehash, res_page);
1736b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (de)
1746b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			break;
1756b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1766b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (max_slots >= s)
1776b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			room = true;
1786b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		f2fs_put_page(dentry_page, 0);
1796b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
1806b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1816b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (!de && room && F2FS_I(dir)->chash != namehash) {
1826b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		F2FS_I(dir)->chash = namehash;
1836b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		F2FS_I(dir)->clevel = level;
1846b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
1856b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1866b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return de;
1876b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
1886b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
1896b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim/*
1906b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * Find an entry in the specified directory with the wanted name.
1916b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * It returns the page where the entry was found (as a parameter - res_page),
1926b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * and the entry itself. Page is returned mapped and unlocked.
1936b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * Entry is guaranteed to be valid.
1946b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim */
1956b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstruct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
1966b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			struct qstr *child, struct page **res_page)
1976b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
1986b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned long npages = dir_blocks(dir);
1996b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dir_entry *de = NULL;
2006b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	f2fs_hash_t name_hash;
2016b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int max_depth;
2026b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int level;
2036b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2046b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (npages == 0)
2056b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return NULL;
2066b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2076b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	*res_page = NULL;
2086b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
209eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng	name_hash = f2fs_dentry_hash(child);
2106b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	max_depth = F2FS_I(dir)->i_current_depth;
2116b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2126b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	for (level = 0; level < max_depth; level++) {
213eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng		de = find_in_level(dir, level, child, name_hash, res_page);
2146b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (de)
2156b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			break;
2166b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
2176b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (!de && F2FS_I(dir)->chash != name_hash) {
2186b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		F2FS_I(dir)->chash = name_hash;
2196b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		F2FS_I(dir)->clevel = level - 1;
2206b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
2216b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return de;
2226b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
2236b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2246b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstruct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p)
2256b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
2264777f86b7c0a587dde275a5c1ff3022b2e601313Namjae Jeon	struct page *page;
2274777f86b7c0a587dde275a5c1ff3022b2e601313Namjae Jeon	struct f2fs_dir_entry *de;
2284777f86b7c0a587dde275a5c1ff3022b2e601313Namjae Jeon	struct f2fs_dentry_block *dentry_blk;
2296b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2306b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	page = get_lock_data_page(dir, 0);
2316b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (IS_ERR(page))
2326b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return NULL;
2336b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2346b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	dentry_blk = kmap(page);
2356b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de = &dentry_blk->dentry[1];
2366b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	*p = page;
2376b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unlock_page(page);
2386b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return de;
2396b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
2406b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2416b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimino_t f2fs_inode_by_name(struct inode *dir, struct qstr *qstr)
2426b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
2436b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	ino_t res = 0;
2446b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dir_entry *de;
2456b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct page *page;
2466b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2476b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de = f2fs_find_entry(dir, qstr, &page);
2486b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (de) {
2496b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		res = le32_to_cpu(de->ino);
2506b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		kunmap(page);
2516b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		f2fs_put_page(page, 0);
2526b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
2536b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2546b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return res;
2556b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
2566b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2576b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimvoid f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
2586b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		struct page *page, struct inode *inode)
2596b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
2606b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	lock_page(page);
2613cb5ad152b54430f3e5f338c15f8cd434e7160c8Jaegeuk Kim	f2fs_wait_on_page_writeback(page, DATA);
2626b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de->ino = cpu_to_le32(inode->i_ino);
2636b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	set_de_type(de, inode);
2646b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	kunmap(page);
2656b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	set_page_dirty(page);
2666b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2676b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	mark_inode_dirty(dir);
2686666e6aa9f36b2bfd6b30072c07b34f2a24becf1Jaegeuk Kim
2696b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	f2fs_put_page(page, 1);
2706b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
2716b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
27244a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kimstatic void init_dent_inode(const struct qstr *name, struct page *ipage)
2736b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
27458bfaf44df58082c72882b235cae611c975537d4Jaegeuk Kim	struct f2fs_inode *ri;
2756b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
27654b591dfda1f5ab0bc2a9ce1bee5364110168777Jaegeuk Kim	f2fs_wait_on_page_writeback(ipage, NODE);
27754b591dfda1f5ab0bc2a9ce1bee5364110168777Jaegeuk Kim
27853dc9a67769d0a9733adb5156adfc07edcbc1ea3Al Viro	/* copy name info. to this inode page */
27958bfaf44df58082c72882b235cae611c975537d4Jaegeuk Kim	ri = F2FS_INODE(ipage);
28058bfaf44df58082c72882b235cae611c975537d4Jaegeuk Kim	ri->i_namelen = cpu_to_le32(name->len);
28158bfaf44df58082c72882b235cae611c975537d4Jaegeuk Kim	memcpy(ri->i_name, name->name, name->len);
2826b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	set_page_dirty(ipage);
2836b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
2846b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
2851cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kimint update_dent_inode(struct inode *inode, const struct qstr *name)
2861cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim{
2871cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim	struct page *page;
2881cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim
2894081363fbe84a7ebac6d3339dd2775df45d856d0Jaegeuk Kim	page = get_node_page(F2FS_I_SB(inode), inode->i_ino);
2901cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim	if (IS_ERR(page))
2911cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim		return PTR_ERR(page);
2921cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim
2931cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim	init_dent_inode(name, page);
2941cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim	f2fs_put_page(page, 1);
2951cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim
2961cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim	return 0;
2971cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim}
2981cd14cafc694bcedc5017a4f0dcb3c3faddec622Jaegeuk Kim
29944a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kimstatic int make_empty_dir(struct inode *inode,
30044a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim		struct inode *parent, struct page *page)
301399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim{
302399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	struct page *dentry_page;
303399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	struct f2fs_dentry_block *dentry_blk;
304399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	struct f2fs_dir_entry *de;
305399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
30644a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	dentry_page = get_new_data_page(inode, page, 0, true);
307399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	if (IS_ERR(dentry_page))
308399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim		return PTR_ERR(dentry_page);
309399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
3101c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng
3111c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng	dentry_blk = kmap_atomic(dentry_page);
312399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
313399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de = &dentry_blk->dentry[0];
314399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de->name_len = cpu_to_le16(1);
315399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de->hash_code = 0;
316399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de->ino = cpu_to_le32(inode->i_ino);
317399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	memcpy(dentry_blk->filename[0], ".", 1);
318399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	set_de_type(de, inode);
319399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
320399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de = &dentry_blk->dentry[1];
321399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de->hash_code = 0;
322399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de->name_len = cpu_to_le16(2);
323399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	de->ino = cpu_to_le32(parent->i_ino);
324399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	memcpy(dentry_blk->filename[1], "..", 2);
325399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	set_de_type(de, inode);
326399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
327399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	test_and_set_bit_le(0, &dentry_blk->dentry_bitmap);
328399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	test_and_set_bit_le(1, &dentry_blk->dentry_bitmap);
3291c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng	kunmap_atomic(dentry_blk);
330399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
331399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	set_page_dirty(dentry_page);
332399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	f2fs_put_page(dentry_page, 1);
333399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim	return 0;
334399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim}
335399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
33644a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kimstatic struct page *init_inode_metadata(struct inode *inode,
33769f24eac55725859a89c440ee2d19f36fa09e8fcAl Viro		struct inode *dir, const struct qstr *name)
3386b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
33944a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	struct page *page;
34044a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	int err;
34144a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim
342b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
343a014e037be26b5c9ee6fb4e49e7804141cf3bb89Jaegeuk Kim		page = new_inode_page(inode);
34444a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim		if (IS_ERR(page))
34544a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim			return page;
3466b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
3476b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (S_ISDIR(inode->i_mode)) {
34844a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim			err = make_empty_dir(inode, dir, page);
34944a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim			if (err)
35044a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim				goto error;
3516b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		}
3526b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
3532ed2d5b33cb564025b1eb90650d70a0a3592c0e3Jaegeuk Kim		err = f2fs_init_acl(inode, dir, page);
35444a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim		if (err)
355a8865372a8414298982e07f4ac8d6dc0ab1e0a3dJaegeuk Kim			goto put_error;
35644a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim
3578ae8f1627f39bae505b90cade50cd8a911b8bda6Jaegeuk Kim		err = f2fs_init_security(inode, dir, name, page);
3588ae8f1627f39bae505b90cade50cd8a911b8bda6Jaegeuk Kim		if (err)
359a8865372a8414298982e07f4ac8d6dc0ab1e0a3dJaegeuk Kim			goto put_error;
3606b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	} else {
3614081363fbe84a7ebac6d3339dd2775df45d856d0Jaegeuk Kim		page = get_node_page(F2FS_I_SB(dir), inode->i_ino);
36244a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim		if (IS_ERR(page))
36344a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim			return page;
36444a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim
36544a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim		set_cold_node(inode, page);
3666b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
36744a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim
368b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	if (name)
369b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim		init_dent_inode(name, page);
37044a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim
37183d5d6f66b375f21bee4c2e17178f7c073a66301Jaegeuk Kim	/*
37283d5d6f66b375f21bee4c2e17178f7c073a66301Jaegeuk Kim	 * This file should be checkpointed during fsync.
37383d5d6f66b375f21bee4c2e17178f7c073a66301Jaegeuk Kim	 * We lost i_pino from now on.
37483d5d6f66b375f21bee4c2e17178f7c073a66301Jaegeuk Kim	 */
3756b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK)) {
376354a3399dc6f7e556d04e1c731cd50e08eeb44bdJaegeuk Kim		file_lost_pino(inode);
37750732df02eefb39ab414ef655979c2c9b64ad21cChao Yu		/*
37850732df02eefb39ab414ef655979c2c9b64ad21cChao Yu		 * If link the tmpfile to alias through linkat path,
37950732df02eefb39ab414ef655979c2c9b64ad21cChao Yu		 * we should remove this inode from orphan list.
38050732df02eefb39ab414ef655979c2c9b64ad21cChao Yu		 */
38150732df02eefb39ab414ef655979c2c9b64ad21cChao Yu		if (inode->i_nlink == 0)
3824081363fbe84a7ebac6d3339dd2775df45d856d0Jaegeuk Kim			remove_orphan_inode(F2FS_I_SB(dir), inode->i_ino);
3836b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		inc_nlink(inode);
3846b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
38544a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	return page;
38644a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim
387a8865372a8414298982e07f4ac8d6dc0ab1e0a3dJaegeuk Kimput_error:
38844a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	f2fs_put_page(page, 1);
38990d72459ccb47335a4348947506fd091e63f7cf8Chao Yuerror:
390bd859c6598dd2b73c517b3a36ecc5dd387eb1eb2Jaegeuk Kim	/* once the failed inode becomes a bad inode, i_mode is S_IFREG */
391bd859c6598dd2b73c517b3a36ecc5dd387eb1eb2Jaegeuk Kim	truncate_inode_pages(&inode->i_data, 0);
392764aa3e978020121cbb86111b5d8f42830015a06Jaegeuk Kim	truncate_blocks(inode, 0, false);
393bd859c6598dd2b73c517b3a36ecc5dd387eb1eb2Jaegeuk Kim	remove_dirty_dir_inode(inode);
39444a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	remove_inode_page(inode);
39544a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	return ERR_PTR(err);
3966b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
3976b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
3986b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic void update_parent_metadata(struct inode *dir, struct inode *inode,
3996b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						unsigned int current_depth)
4006b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
4016b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (is_inode_flag_set(F2FS_I(inode), FI_NEW_INODE)) {
4026b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (S_ISDIR(inode->i_mode)) {
4036b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			inc_nlink(dir);
404699489bbbea4fc3b9b735d69941cf4fca91ce1d5Jaegeuk Kim			set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
4056b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		}
4066b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
4076b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
4086b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	dir->i_mtime = dir->i_ctime = CURRENT_TIME;
409a18ff063406dd6aec41fda598eabe2691007a30dJaegeuk Kim	mark_inode_dirty(dir);
410a18ff063406dd6aec41fda598eabe2691007a30dJaegeuk Kim
4116b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (F2FS_I(dir)->i_current_depth != current_depth) {
4126b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		F2FS_I(dir)->i_current_depth = current_depth;
413699489bbbea4fc3b9b735d69941cf4fca91ce1d5Jaegeuk Kim		set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
4146b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
4156b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4166b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (is_inode_flag_set(F2FS_I(inode), FI_INC_LINK))
4176b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		clear_inode_flag(F2FS_I(inode), FI_INC_LINK);
4186b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
4196b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4206b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstatic int room_for_filename(struct f2fs_dentry_block *dentry_blk, int slots)
4216b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
4226b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	int bit_start = 0;
4236b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	int zero_start, zero_end;
4246b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimnext:
4256b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	zero_start = find_next_zero_bit_le(&dentry_blk->dentry_bitmap,
4266b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						NR_DENTRY_IN_BLOCK,
4276b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						bit_start);
4286b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (zero_start >= NR_DENTRY_IN_BLOCK)
4296b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return NR_DENTRY_IN_BLOCK;
4306b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4316b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	zero_end = find_next_bit_le(&dentry_blk->dentry_bitmap,
4326b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						NR_DENTRY_IN_BLOCK,
4336b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						zero_start);
4346b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (zero_end - zero_start >= slots)
4356b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return zero_start;
4366b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4376b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	bit_start = zero_end + 1;
4386b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4396b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (zero_end + 1 >= NR_DENTRY_IN_BLOCK)
4406b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return NR_DENTRY_IN_BLOCK;
4416b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	goto next;
4426b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
4436b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
444399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim/*
4454f4124d0b99682efa7307191a28ec050872d2079Chao Yu * Caller should grab and release a rwsem by calling f2fs_lock_op() and
4464f4124d0b99682efa7307191a28ec050872d2079Chao Yu * f2fs_unlock_op().
447399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim */
4486c311ec6c2d9e015d454b4e3fda8008b5bebf316Chris Friesint __f2fs_add_link(struct inode *dir, const struct qstr *name,
4496c311ec6c2d9e015d454b4e3fda8008b5bebf316Chris Fries						struct inode *inode)
4506b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
4516b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int bit_pos;
4526b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int level;
4536b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int current_depth;
4546b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned long bidx, block;
4556b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	f2fs_hash_t dentry_hash;
4566b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dir_entry *de;
4576b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int nbucket, nblock;
458b7f7a5e0be94d13875a1c6c9aa65eeb11a46fc1bAl Viro	size_t namelen = name->len;
4596b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct page *dentry_page = NULL;
4606b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dentry_block *dentry_blk = NULL;
461457d08ee4fd91c8df17917ff2d32565e6adacbfcNamjae Jeon	int slots = GET_DENTRY_SLOTS(namelen);
46244a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	struct page *page;
4636b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	int err = 0;
4646b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	int i;
4656b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
466eee6160f2e324f0f6a626159fa1120caad31a6beGu Zheng	dentry_hash = f2fs_dentry_hash(name);
4676b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	level = 0;
4686b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	current_depth = F2FS_I(dir)->i_current_depth;
4696b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (F2FS_I(dir)->chash == dentry_hash) {
4706b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		level = F2FS_I(dir)->clevel;
4716b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		F2FS_I(dir)->chash = 0;
4726b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
4736b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4746b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimstart:
475cfb271d485d0ec31eb92b51f4fbe54bf6542e8e6Chao Yu	if (unlikely(current_depth == MAX_DIR_HASH_DEPTH))
4766b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		return -ENOSPC;
4776b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4786b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	/* Increase the depth, if required */
4796b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (level == current_depth)
4806b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		++current_depth;
4816b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4823843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim	nbucket = dir_buckets(level, F2FS_I(dir)->i_dir_level);
4836b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	nblock = bucket_blocks(level);
4846b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4853843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim	bidx = dir_block_index(level, F2FS_I(dir)->i_dir_level,
4863843154598a00408f4214a68bd536fdf27b1df10Jaegeuk Kim				(le32_to_cpu(dentry_hash) % nbucket));
4876b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4886b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	for (block = bidx; block <= (bidx + nblock - 1); block++) {
48964aa7ed98db489d1c41ef140876ada38498678abJaegeuk Kim		dentry_page = get_new_data_page(dir, NULL, block, true);
490399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim		if (IS_ERR(dentry_page))
4916b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			return PTR_ERR(dentry_page);
4926b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4936b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		dentry_blk = kmap(dentry_page);
4946b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		bit_pos = room_for_filename(dentry_blk, slots);
4956b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (bit_pos < NR_DENTRY_IN_BLOCK)
4966b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			goto add_dentry;
4976b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
4986b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		kunmap(dentry_page);
4996b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		f2fs_put_page(dentry_page, 1);
5006b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
5016b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
5026b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	/* Move to next level to find the empty slot for new dentry */
5036b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	++level;
5046b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	goto start;
5056b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimadd_dentry:
5063cb5ad152b54430f3e5f338c15f8cd434e7160c8Jaegeuk Kim	f2fs_wait_on_page_writeback(dentry_page, DATA);
5076b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
508d928bfbfe77aa457b765c19e9db8cd4cc72b3c89Jaegeuk Kim	down_write(&F2FS_I(inode)->i_sem);
50944a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	page = init_inode_metadata(inode, dir, name);
51044a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	if (IS_ERR(page)) {
51144a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim		err = PTR_ERR(page);
51244a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim		goto fail;
51344a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	}
5146b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de = &dentry_blk->dentry[bit_pos];
51525ca923b2a766b9c93b63777ead351137533a623Jaegeuk Kim	de->hash_code = dentry_hash;
5166b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de->name_len = cpu_to_le16(namelen);
517b7f7a5e0be94d13875a1c6c9aa65eeb11a46fc1bAl Viro	memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
5186b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	de->ino = cpu_to_le32(inode->i_ino);
5196b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	set_de_type(de, inode);
5206b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	for (i = 0; i < slots; i++)
5216b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
5226b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	set_page_dirty(dentry_page);
5236666e6aa9f36b2bfd6b30072c07b34f2a24becf1Jaegeuk Kim
52444a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	/* we don't need to mark_inode_dirty now */
5256666e6aa9f36b2bfd6b30072c07b34f2a24becf1Jaegeuk Kim	F2FS_I(inode)->i_pino = dir->i_ino;
52644a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	update_inode(inode, page);
52744a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	f2fs_put_page(page, 1);
52844a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim
52944a83ff6a81d84ab83bcb43a49ff1ba6c7e17cd1Jaegeuk Kim	update_parent_metadata(dir, inode, current_depth);
5306b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimfail:
531d928bfbfe77aa457b765c19e9db8cd4cc72b3c89Jaegeuk Kim	up_write(&F2FS_I(inode)->i_sem);
532d928bfbfe77aa457b765c19e9db8cd4cc72b3c89Jaegeuk Kim
533924a2ddbd0c2829ebca9ac899522cbb16a9b6d8cJaegeuk Kim	if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
534924a2ddbd0c2829ebca9ac899522cbb16a9b6d8cJaegeuk Kim		update_inode_page(dir);
535924a2ddbd0c2829ebca9ac899522cbb16a9b6d8cJaegeuk Kim		clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
536924a2ddbd0c2829ebca9ac899522cbb16a9b6d8cJaegeuk Kim	}
5376b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	kunmap(dentry_page);
5386b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	f2fs_put_page(dentry_page, 1);
5396b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return err;
5406b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
5416b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
542b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kimint f2fs_do_tmpfile(struct inode *inode, struct inode *dir)
543b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim{
544b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	struct page *page;
545b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	int err = 0;
546b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim
547b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	down_write(&F2FS_I(inode)->i_sem);
548b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	page = init_inode_metadata(inode, dir, NULL);
549b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	if (IS_ERR(page)) {
550b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim		err = PTR_ERR(page);
551b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim		goto fail;
552b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	}
553b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	/* we don't need to mark_inode_dirty now */
554b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	update_inode(inode, page);
555b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	f2fs_put_page(page, 1);
556b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim
557b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	clear_inode_flag(F2FS_I(inode), FI_NEW_INODE);
558b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kimfail:
559b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	up_write(&F2FS_I(inode)->i_sem);
560b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim	return err;
561b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim}
562b97a9b5da891ab6aff5a6a19c569c9c4c5563d48Jaegeuk Kim
5630a8165d7c2cf1395059db20ab07665baf3758fcdJaegeuk Kim/*
564e1c42045203071c4634b89e696037357810d3083arter * It only removes the dentry from the dentry page, corresponding name
5656b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim * entry in name page does not need to be touched during deletion.
5666b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim */
5676b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimvoid f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
5686b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						struct inode *inode)
5696b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
5706b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct	f2fs_dentry_block *dentry_blk;
5716b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int bit_pos;
5724081363fbe84a7ebac6d3339dd2775df45d856d0Jaegeuk Kim	struct inode *dir = page->mapping->host;
573457d08ee4fd91c8df17917ff2d32565e6adacbfcNamjae Jeon	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
5746b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	int i;
5756b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
5766b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	lock_page(page);
5773cb5ad152b54430f3e5f338c15f8cd434e7160c8Jaegeuk Kim	f2fs_wait_on_page_writeback(page, DATA);
5786b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
5791c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng	dentry_blk = page_address(page);
5801c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng	bit_pos = dentry - dentry_blk->dentry;
5816b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	for (i = 0; i < slots; i++)
5826b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		test_and_clear_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
5836b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
5846b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	/* Let's check and deallocate this dentry page */
5856b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
5866b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			NR_DENTRY_IN_BLOCK,
5876b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			0);
5886b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	kunmap(page); /* kunmap - pair of f2fs_find_entry */
5896b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	set_page_dirty(page);
5906b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
5916b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
5926b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
5936b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (inode) {
5944081363fbe84a7ebac6d3339dd2775df45d856d0Jaegeuk Kim		struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
5951fe54f9dd3acfaa3ed4e1d1e3278fd0f1d1e98cdJaegeuk Kim
596d928bfbfe77aa457b765c19e9db8cd4cc72b3c89Jaegeuk Kim		down_write(&F2FS_I(inode)->i_sem);
597d928bfbfe77aa457b765c19e9db8cd4cc72b3c89Jaegeuk Kim
598e8dae6045882e32a067326f47b7ccd3aaf8814e2Jaegeuk Kim		if (S_ISDIR(inode->i_mode)) {
599e8dae6045882e32a067326f47b7ccd3aaf8814e2Jaegeuk Kim			drop_nlink(dir);
600e8dae6045882e32a067326f47b7ccd3aaf8814e2Jaegeuk Kim			update_inode_page(dir);
601e8dae6045882e32a067326f47b7ccd3aaf8814e2Jaegeuk Kim		}
602163799872b65b0cbf0091d82971233cc3d2425d3Namjae Jeon		inode->i_ctime = CURRENT_TIME;
6036b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		drop_nlink(inode);
6046b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (S_ISDIR(inode->i_mode)) {
6056b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			drop_nlink(inode);
6066b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			i_size_write(inode, 0);
6076b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		}
608d928bfbfe77aa457b765c19e9db8cd4cc72b3c89Jaegeuk Kim		up_write(&F2FS_I(inode)->i_sem);
609399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim		update_inode_page(inode);
610399368372ed9f3c396eadb5c2bbc98be8c774a39Jaegeuk Kim
6116b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (inode->i_nlink == 0)
6126b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			add_orphan_inode(sbi, inode->i_ino);
613cbd56e7d20d7188d62a85aa6986a7b2c8e755ab5Jaegeuk Kim		else
614cbd56e7d20d7188d62a85aa6986a7b2c8e755ab5Jaegeuk Kim			release_orphan_inode(sbi);
6156b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
6166b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6176b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (bit_pos == NR_DENTRY_IN_BLOCK) {
6186b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		truncate_hole(dir, page->index, page->index + 1);
6196b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		clear_page_dirty_for_io(page);
6206b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		ClearPageUptodate(page);
621a7ffdbe22cecaed59b5d76a5f003d68907d64240Jaegeuk Kim		inode_dec_dirty_pages(dir);
6226b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
623508198be3c2f7f8929101bb0daeb8f0039c1dc7fNamjae Jeon	f2fs_put_page(page, 1);
6246b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
6256b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6266b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimbool f2fs_empty_dir(struct inode *dir)
6276b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
6286b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned long bidx;
6296b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct page *dentry_page;
6306b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned int bit_pos;
6316b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct	f2fs_dentry_block *dentry_blk;
6326b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned long nblock = dir_blocks(dir);
6336b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6346b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	for (bidx = 0; bidx < nblock; bidx++) {
6356b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		dentry_page = get_lock_data_page(dir, bidx);
6366b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (IS_ERR(dentry_page)) {
6376b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			if (PTR_ERR(dentry_page) == -ENOENT)
6386b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim				continue;
6396b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			else
6406b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim				return false;
6416b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		}
6426b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6431c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng
6441c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng		dentry_blk = kmap_atomic(dentry_page);
6456b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (bidx == 0)
6466b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			bit_pos = 2;
6476b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		else
6486b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			bit_pos = 0;
6496b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
6506b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						NR_DENTRY_IN_BLOCK,
6516b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim						bit_pos);
6521c3bb97899c91d420562c51882ee81a9d7c35306Gu Zheng		kunmap_atomic(dentry_blk);
6536b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6546b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		f2fs_put_page(dentry_page, 1);
6556b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6566b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (bit_pos < NR_DENTRY_IN_BLOCK)
6576b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			return false;
6586b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
6596b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return true;
6606b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
6616b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6626f7f231e7b4f819b912f848a33d946b54261483dAl Virostatic int f2fs_readdir(struct file *file, struct dir_context *ctx)
6636b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim{
664496ad9aa8ef448058e36ca7a787c61f2e63f0f54Al Viro	struct inode *inode = file_inode(file);
6656b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned long npages = dir_blocks(inode);
66699b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim	unsigned int bit_pos = 0;
6676b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dentry_block *dentry_blk = NULL;
6686b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct f2fs_dir_entry *de = NULL;
6696b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	struct page *dentry_page = NULL;
670817202d937e6cca7e60f42e6495aaa51d70d9d7eChao Yu	struct file_ra_state *ra = &file->f_ra;
6716f7f231e7b4f819b912f848a33d946b54261483dAl Viro	unsigned int n = ((unsigned long)ctx->pos / NR_DENTRY_IN_BLOCK);
6726b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	unsigned char d_type = DT_UNKNOWN;
6736b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6746f7f231e7b4f819b912f848a33d946b54261483dAl Viro	bit_pos = ((unsigned long)ctx->pos % NR_DENTRY_IN_BLOCK);
6756b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
676817202d937e6cca7e60f42e6495aaa51d70d9d7eChao Yu	/* readahead for multi pages of dir */
677817202d937e6cca7e60f42e6495aaa51d70d9d7eChao Yu	if (npages - n > 1 && !ra_has_index(ra, n))
678817202d937e6cca7e60f42e6495aaa51d70d9d7eChao Yu		page_cache_sync_readahead(inode->i_mapping, ra, file, n,
679817202d937e6cca7e60f42e6495aaa51d70d9d7eChao Yu				min(npages - n, (pgoff_t)MAX_DIR_RA_PAGES));
680817202d937e6cca7e60f42e6495aaa51d70d9d7eChao Yu
6816c311ec6c2d9e015d454b4e3fda8008b5bebf316Chris Fries	for (; n < npages; n++) {
6826b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		dentry_page = get_lock_data_page(inode, n);
6836b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		if (IS_ERR(dentry_page))
6846b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			continue;
6856b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6866b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		dentry_blk = kmap(dentry_page);
6876b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		while (bit_pos < NR_DENTRY_IN_BLOCK) {
6886b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
6896b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim							NR_DENTRY_IN_BLOCK,
6906b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim							bit_pos);
6916b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			if (bit_pos >= NR_DENTRY_IN_BLOCK)
6926b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim				break;
6936b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
6946b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim			de = &dentry_blk->dentry[bit_pos];
6956f7f231e7b4f819b912f848a33d946b54261483dAl Viro			if (de->file_type < F2FS_FT_MAX)
6966f7f231e7b4f819b912f848a33d946b54261483dAl Viro				d_type = f2fs_filetype_table[de->file_type];
6976f7f231e7b4f819b912f848a33d946b54261483dAl Viro			else
6986f7f231e7b4f819b912f848a33d946b54261483dAl Viro				d_type = DT_UNKNOWN;
6996f7f231e7b4f819b912f848a33d946b54261483dAl Viro			if (!dir_emit(ctx,
70099b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim					dentry_blk->filename[bit_pos],
70199b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim					le16_to_cpu(de->name_len),
70299b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim					le32_to_cpu(de->ino), d_type))
70399b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim				goto stop;
70499b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim
70599b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim			bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
70699b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kim			ctx->pos = n * NR_DENTRY_IN_BLOCK + bit_pos;
7076b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		}
7086b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		bit_pos = 0;
7096f7f231e7b4f819b912f848a33d946b54261483dAl Viro		ctx->pos = (n + 1) * NR_DENTRY_IN_BLOCK;
7106b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		kunmap(dentry_page);
7116b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		f2fs_put_page(dentry_page, 1);
7126b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		dentry_page = NULL;
7136b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
71499b072bb38c9b398bc7c3fc8a0f30d0801f78750Jaegeuk Kimstop:
7156b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	if (dentry_page && !IS_ERR(dentry_page)) {
7166b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		kunmap(dentry_page);
7176b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim		f2fs_put_page(dentry_page, 1);
7186b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	}
7196b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
7206b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	return 0;
7216b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim}
7226b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim
7236b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kimconst struct file_operations f2fs_dir_operations = {
7246b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	.llseek		= generic_file_llseek,
7256b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	.read		= generic_read_dir,
7266f7f231e7b4f819b912f848a33d946b54261483dAl Viro	.iterate	= f2fs_readdir,
7276b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	.fsync		= f2fs_sync_file,
7286b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim	.unlocked_ioctl	= f2fs_ioctl,
7296b4ea0160ae236a6561defa28e19f973aedda9ffJaegeuk Kim};
730