xref: /openbmc/linux/fs/f2fs/inline.c (revision 158c194c375be1b82149f9de80fd90e522979dc5)
1e18c65b2SHuajun Li /*
2e18c65b2SHuajun Li  * fs/f2fs/inline.c
3e18c65b2SHuajun Li  * Copyright (c) 2013, Intel Corporation
4e18c65b2SHuajun Li  * Authors: Huajun Li <huajun.li@intel.com>
5e18c65b2SHuajun Li  *          Haicheng Li <haicheng.li@intel.com>
6e18c65b2SHuajun Li  * This program is free software; you can redistribute it and/or modify
7e18c65b2SHuajun Li  * it under the terms of the GNU General Public License version 2 as
8e18c65b2SHuajun Li  * published by the Free Software Foundation.
9e18c65b2SHuajun Li  */
10e18c65b2SHuajun Li 
11e18c65b2SHuajun Li #include <linux/fs.h>
12e18c65b2SHuajun Li #include <linux/f2fs_fs.h>
13e18c65b2SHuajun Li 
14e18c65b2SHuajun Li #include "f2fs.h"
15e18c65b2SHuajun Li 
16e18c65b2SHuajun Li bool f2fs_may_inline(struct inode *inode)
17e18c65b2SHuajun Li {
184081363fSJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
19e18c65b2SHuajun Li 		return false;
20e18c65b2SHuajun Li 
2188b88a66SJaegeuk Kim 	if (f2fs_is_atomic_file(inode))
2288b88a66SJaegeuk Kim 		return false;
2388b88a66SJaegeuk Kim 
24b3d208f9SJaegeuk Kim 	if (!S_ISREG(inode->i_mode))
25e18c65b2SHuajun Li 		return false;
26e18c65b2SHuajun Li 
2792dffd01SJaegeuk Kim 	if (i_size_read(inode) > MAX_INLINE_DATA)
2892dffd01SJaegeuk Kim 		return false;
2992dffd01SJaegeuk Kim 
30e18c65b2SHuajun Li 	return true;
31e18c65b2SHuajun Li }
32e18c65b2SHuajun Li 
33b3d208f9SJaegeuk Kim void read_inline_data(struct page *page, struct page *ipage)
34e18c65b2SHuajun Li {
35e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
36e18c65b2SHuajun Li 
37b3d208f9SJaegeuk Kim 	if (PageUptodate(page))
38b3d208f9SJaegeuk Kim 		return;
3904a17fb1SChao Yu 
40b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_P_SB(page), page->index);
41e18c65b2SHuajun Li 
4218309aaaSChao Yu 	zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
43e18c65b2SHuajun Li 
44e18c65b2SHuajun Li 	/* Copy the whole inline data block */
45e18c65b2SHuajun Li 	src_addr = inline_data_addr(ipage);
46f1e33a04SJaegeuk Kim 	dst_addr = kmap_atomic(page);
47e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
48427a45c8SJaegeuk Kim 	flush_dcache_page(page);
49f1e33a04SJaegeuk Kim 	kunmap_atomic(dst_addr);
50e18c65b2SHuajun Li 	SetPageUptodate(page);
51b3d208f9SJaegeuk Kim }
52e18c65b2SHuajun Li 
53b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page)
54b3d208f9SJaegeuk Kim {
55b3d208f9SJaegeuk Kim 	struct page *ipage;
56b3d208f9SJaegeuk Kim 
57b3d208f9SJaegeuk Kim 	ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
58b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
59b3d208f9SJaegeuk Kim 		unlock_page(page);
60b3d208f9SJaegeuk Kim 		return PTR_ERR(ipage);
61b3d208f9SJaegeuk Kim 	}
62b3d208f9SJaegeuk Kim 
63b3d208f9SJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
64b3d208f9SJaegeuk Kim 		f2fs_put_page(ipage, 1);
65b3d208f9SJaegeuk Kim 		return -EAGAIN;
66b3d208f9SJaegeuk Kim 	}
67b3d208f9SJaegeuk Kim 
68b3d208f9SJaegeuk Kim 	if (page->index)
69b3d208f9SJaegeuk Kim 		zero_user_segment(page, 0, PAGE_CACHE_SIZE);
70b3d208f9SJaegeuk Kim 	else
71b3d208f9SJaegeuk Kim 		read_inline_data(page, ipage);
72b3d208f9SJaegeuk Kim 
73b3d208f9SJaegeuk Kim 	SetPageUptodate(page);
74b3d208f9SJaegeuk Kim 	f2fs_put_page(ipage, 1);
75b3d208f9SJaegeuk Kim 	unlock_page(page);
76e18c65b2SHuajun Li 	return 0;
77e18c65b2SHuajun Li }
78e18c65b2SHuajun Li 
79b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
80e18c65b2SHuajun Li {
81e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
82e18c65b2SHuajun Li 	block_t new_blk_addr;
83e18c65b2SHuajun Li 	struct f2fs_io_info fio = {
84e18c65b2SHuajun Li 		.type = DATA,
85e18c65b2SHuajun Li 		.rw = WRITE_SYNC | REQ_PRIO,
86e18c65b2SHuajun Li 	};
87*158c194cSJaegeuk Kim 	int dirty, err;
88e18c65b2SHuajun Li 
89b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
90e18c65b2SHuajun Li 
91b3d208f9SJaegeuk Kim 	if (!f2fs_exist_data(dn->inode))
92b3d208f9SJaegeuk Kim 		goto clear_out;
93ec4e7af4SJaegeuk Kim 
94b3d208f9SJaegeuk Kim 	err = f2fs_reserve_block(dn, 0);
9515c6e3aaSJaegeuk Kim 	if (err)
96b3d208f9SJaegeuk Kim 		return err;
97e18c65b2SHuajun Li 
989ac1349aSJaegeuk Kim 	f2fs_wait_on_page_writeback(page, DATA);
99b3d208f9SJaegeuk Kim 
100b3d208f9SJaegeuk Kim 	if (PageUptodate(page))
101b3d208f9SJaegeuk Kim 		goto no_update;
102b3d208f9SJaegeuk Kim 
10318309aaaSChao Yu 	zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
104e18c65b2SHuajun Li 
105e18c65b2SHuajun Li 	/* Copy the whole inline data block */
106b3d208f9SJaegeuk Kim 	src_addr = inline_data_addr(dn->inode_page);
107f1e33a04SJaegeuk Kim 	dst_addr = kmap_atomic(page);
108e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
10909b8b3c8SJaegeuk Kim 	flush_dcache_page(page);
110f1e33a04SJaegeuk Kim 	kunmap_atomic(dst_addr);
1119e09fc85SJaegeuk Kim 	SetPageUptodate(page);
112b3d208f9SJaegeuk Kim no_update:
113*158c194cSJaegeuk Kim 	/* clear dirty state */
114*158c194cSJaegeuk Kim 	dirty = clear_page_dirty_for_io(page);
115*158c194cSJaegeuk Kim 
116e18c65b2SHuajun Li 	/* write data page to try to make data consistent */
117e18c65b2SHuajun Li 	set_page_writeback(page);
118b3d208f9SJaegeuk Kim 
119b3d208f9SJaegeuk Kim 	write_data_page(page, dn, &new_blk_addr, &fio);
120b3d208f9SJaegeuk Kim 	update_extent_cache(new_blk_addr, dn);
1215514f0aaSYuan Zhong 	f2fs_wait_on_page_writeback(page, DATA);
122*158c194cSJaegeuk Kim 	if (dirty)
123*158c194cSJaegeuk Kim 		inode_dec_dirty_pages(dn->inode);
124e18c65b2SHuajun Li 
125e18c65b2SHuajun Li 	/* clear inline data and flag after data writeback */
126b3d208f9SJaegeuk Kim 	truncate_inline_data(dn->inode_page, 0);
127b3d208f9SJaegeuk Kim clear_out:
128b3d208f9SJaegeuk Kim 	stat_dec_inline_inode(dn->inode);
12957e2a2c0SJaegeuk Kim 	f2fs_clear_inline_inode(dn->inode);
130b3d208f9SJaegeuk Kim 	sync_inode_page(dn);
131b3d208f9SJaegeuk Kim 	f2fs_put_dnode(dn);
132b3d208f9SJaegeuk Kim 	return 0;
133e18c65b2SHuajun Li }
134e18c65b2SHuajun Li 
135b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode)
136e18c65b2SHuajun Li {
137b3d208f9SJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
138b3d208f9SJaegeuk Kim 	struct dnode_of_data dn;
139b3d208f9SJaegeuk Kim 	struct page *ipage, *page;
140b3d208f9SJaegeuk Kim 	int err = 0;
141e18c65b2SHuajun Li 
142b3d208f9SJaegeuk Kim 	page = grab_cache_page(inode->i_mapping, 0);
143b3d208f9SJaegeuk Kim 	if (!page)
1449e09fc85SJaegeuk Kim 		return -ENOMEM;
145b3d208f9SJaegeuk Kim 
146b3d208f9SJaegeuk Kim 	f2fs_lock_op(sbi);
147b3d208f9SJaegeuk Kim 
148b3d208f9SJaegeuk Kim 	ipage = get_node_page(sbi, inode->i_ino);
149b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
1506d20aff8SJaegeuk Kim 		err = PTR_ERR(ipage);
1516d20aff8SJaegeuk Kim 		goto out;
152b067ba1fSJaegeuk Kim 	}
153e18c65b2SHuajun Li 
154b3d208f9SJaegeuk Kim 	set_new_dnode(&dn, inode, ipage, ipage, 0);
155b3d208f9SJaegeuk Kim 
156b3d208f9SJaegeuk Kim 	if (f2fs_has_inline_data(inode))
157b3d208f9SJaegeuk Kim 		err = f2fs_convert_inline_page(&dn, page);
158b3d208f9SJaegeuk Kim 
159b3d208f9SJaegeuk Kim 	f2fs_put_dnode(&dn);
1606d20aff8SJaegeuk Kim out:
161b3d208f9SJaegeuk Kim 	f2fs_unlock_op(sbi);
162b3d208f9SJaegeuk Kim 
163b3d208f9SJaegeuk Kim 	f2fs_put_page(page, 1);
164e18c65b2SHuajun Li 	return err;
165e18c65b2SHuajun Li }
166e18c65b2SHuajun Li 
167b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page)
168e18c65b2SHuajun Li {
169e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
170e18c65b2SHuajun Li 	struct dnode_of_data dn;
171e18c65b2SHuajun Li 	int err;
172e18c65b2SHuajun Li 
173e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, NULL, NULL, 0);
174e18c65b2SHuajun Li 	err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
175e18c65b2SHuajun Li 	if (err)
176e18c65b2SHuajun Li 		return err;
177e18c65b2SHuajun Li 
178c08a690bSJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
179b3d208f9SJaegeuk Kim 		f2fs_put_dnode(&dn);
180b3d208f9SJaegeuk Kim 		return -EAGAIN;
181c08a690bSJaegeuk Kim 	}
182c08a690bSJaegeuk Kim 
183b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_I_SB(inode), page->index);
184b3d208f9SJaegeuk Kim 
185b3d208f9SJaegeuk Kim 	f2fs_wait_on_page_writeback(dn.inode_page, NODE);
186f1e33a04SJaegeuk Kim 	src_addr = kmap_atomic(page);
187b3d208f9SJaegeuk Kim 	dst_addr = inline_data_addr(dn.inode_page);
188b3d208f9SJaegeuk Kim 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
189f1e33a04SJaegeuk Kim 	kunmap_atomic(src_addr);
190e18c65b2SHuajun Li 
191fff04f90SJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
192b3d208f9SJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
193b3d208f9SJaegeuk Kim 
194e18c65b2SHuajun Li 	sync_inode_page(&dn);
195e18c65b2SHuajun Li 	f2fs_put_dnode(&dn);
196e18c65b2SHuajun Li 	return 0;
197e18c65b2SHuajun Li }
1981e1bb4baSJaegeuk Kim 
199b3d208f9SJaegeuk Kim void truncate_inline_data(struct page *ipage, u64 from)
2008aa6f1c5SChao Yu {
201b3d208f9SJaegeuk Kim 	void *addr;
2028aa6f1c5SChao Yu 
2038aa6f1c5SChao Yu 	if (from >= MAX_INLINE_DATA)
2048aa6f1c5SChao Yu 		return;
2058aa6f1c5SChao Yu 
20654b591dfSJaegeuk Kim 	f2fs_wait_on_page_writeback(ipage, NODE);
20754b591dfSJaegeuk Kim 
208b3d208f9SJaegeuk Kim 	addr = inline_data_addr(ipage);
209b3d208f9SJaegeuk Kim 	memset(addr + from, 0, MAX_INLINE_DATA - from);
2108aa6f1c5SChao Yu }
2118aa6f1c5SChao Yu 
2120342fd30SJaegeuk Kim bool recover_inline_data(struct inode *inode, struct page *npage)
2131e1bb4baSJaegeuk Kim {
2144081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2151e1bb4baSJaegeuk Kim 	struct f2fs_inode *ri = NULL;
2161e1bb4baSJaegeuk Kim 	void *src_addr, *dst_addr;
2171e1bb4baSJaegeuk Kim 	struct page *ipage;
2181e1bb4baSJaegeuk Kim 
2191e1bb4baSJaegeuk Kim 	/*
2201e1bb4baSJaegeuk Kim 	 * The inline_data recovery policy is as follows.
2211e1bb4baSJaegeuk Kim 	 * [prev.] [next] of inline_data flag
2221e1bb4baSJaegeuk Kim 	 *    o       o  -> recover inline_data
2231e1bb4baSJaegeuk Kim 	 *    o       x  -> remove inline_data, and then recover data blocks
2241e1bb4baSJaegeuk Kim 	 *    x       o  -> remove inline_data, and then recover inline_data
2251e1bb4baSJaegeuk Kim 	 *    x       x  -> recover data blocks
2261e1bb4baSJaegeuk Kim 	 */
2271e1bb4baSJaegeuk Kim 	if (IS_INODE(npage))
2281e1bb4baSJaegeuk Kim 		ri = F2FS_INODE(npage);
2291e1bb4baSJaegeuk Kim 
2301e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode) &&
2310342fd30SJaegeuk Kim 			ri && (ri->i_inline & F2FS_INLINE_DATA)) {
2321e1bb4baSJaegeuk Kim process_inline:
2331e1bb4baSJaegeuk Kim 		ipage = get_node_page(sbi, inode->i_ino);
2349850cf4aSJaegeuk Kim 		f2fs_bug_on(sbi, IS_ERR(ipage));
2351e1bb4baSJaegeuk Kim 
23654b591dfSJaegeuk Kim 		f2fs_wait_on_page_writeback(ipage, NODE);
23754b591dfSJaegeuk Kim 
2381e1bb4baSJaegeuk Kim 		src_addr = inline_data_addr(npage);
2391e1bb4baSJaegeuk Kim 		dst_addr = inline_data_addr(ipage);
2401e1bb4baSJaegeuk Kim 		memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
241b3d208f9SJaegeuk Kim 
242b3d208f9SJaegeuk Kim 		set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
243b3d208f9SJaegeuk Kim 		set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
244b3d208f9SJaegeuk Kim 
2451e1bb4baSJaegeuk Kim 		update_inode(inode, ipage);
2461e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
2470342fd30SJaegeuk Kim 		return true;
2481e1bb4baSJaegeuk Kim 	}
2491e1bb4baSJaegeuk Kim 
2501e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode)) {
2511e1bb4baSJaegeuk Kim 		ipage = get_node_page(sbi, inode->i_ino);
2529850cf4aSJaegeuk Kim 		f2fs_bug_on(sbi, IS_ERR(ipage));
253b3d208f9SJaegeuk Kim 		truncate_inline_data(ipage, 0);
254b3d208f9SJaegeuk Kim 		f2fs_clear_inline_inode(inode);
2551e1bb4baSJaegeuk Kim 		update_inode(inode, ipage);
2561e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
2570342fd30SJaegeuk Kim 	} else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
258764aa3e9SJaegeuk Kim 		truncate_blocks(inode, 0, false);
2591e1bb4baSJaegeuk Kim 		goto process_inline;
2601e1bb4baSJaegeuk Kim 	}
2610342fd30SJaegeuk Kim 	return false;
2621e1bb4baSJaegeuk Kim }
263201a05beSChao Yu 
264201a05beSChao Yu struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
265201a05beSChao Yu 				struct qstr *name, struct page **res_page)
266201a05beSChao Yu {
267201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
2684e6ebf6dSJaegeuk Kim 	struct f2fs_inline_dentry *inline_dentry;
269201a05beSChao Yu 	struct f2fs_dir_entry *de;
2707b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
2714e6ebf6dSJaegeuk Kim 	struct page *ipage;
272201a05beSChao Yu 
273201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
274201a05beSChao Yu 	if (IS_ERR(ipage))
275201a05beSChao Yu 		return NULL;
276201a05beSChao Yu 
2774e6ebf6dSJaegeuk Kim 	inline_dentry = inline_data_addr(ipage);
278201a05beSChao Yu 
2797b3cd7d6SJaegeuk Kim 	make_dentry_ptr(&d, (void *)inline_dentry, 2);
2807b3cd7d6SJaegeuk Kim 	de = find_target_dentry(name, NULL, &d);
2817b3cd7d6SJaegeuk Kim 
2824e6ebf6dSJaegeuk Kim 	unlock_page(ipage);
2834e6ebf6dSJaegeuk Kim 	if (de)
284201a05beSChao Yu 		*res_page = ipage;
2854e6ebf6dSJaegeuk Kim 	else
2864e6ebf6dSJaegeuk Kim 		f2fs_put_page(ipage, 0);
287201a05beSChao Yu 
288201a05beSChao Yu 	/*
289201a05beSChao Yu 	 * For the most part, it should be a bug when name_len is zero.
2904e6ebf6dSJaegeuk Kim 	 * We stop here for figuring out where the bugs has occurred.
291201a05beSChao Yu 	 */
2927b3cd7d6SJaegeuk Kim 	f2fs_bug_on(sbi, d.max < 0);
293201a05beSChao Yu 	return de;
294201a05beSChao Yu }
295201a05beSChao Yu 
296201a05beSChao Yu struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
297201a05beSChao Yu 							struct page **p)
298201a05beSChao Yu {
299201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
300201a05beSChao Yu 	struct page *ipage;
301201a05beSChao Yu 	struct f2fs_dir_entry *de;
302201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk;
303201a05beSChao Yu 
304201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
305201a05beSChao Yu 	if (IS_ERR(ipage))
306201a05beSChao Yu 		return NULL;
307201a05beSChao Yu 
308201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
309201a05beSChao Yu 	de = &dentry_blk->dentry[1];
310201a05beSChao Yu 	*p = ipage;
311201a05beSChao Yu 	unlock_page(ipage);
312201a05beSChao Yu 	return de;
313201a05beSChao Yu }
314201a05beSChao Yu 
315201a05beSChao Yu int make_empty_inline_dir(struct inode *inode, struct inode *parent,
316201a05beSChao Yu 							struct page *ipage)
317201a05beSChao Yu {
318201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk;
319062a3e7bSJaegeuk Kim 	struct f2fs_dentry_ptr d;
320201a05beSChao Yu 
321201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
322201a05beSChao Yu 
323062a3e7bSJaegeuk Kim 	make_dentry_ptr(&d, (void *)dentry_blk, 2);
324062a3e7bSJaegeuk Kim 	do_make_empty_dir(inode, parent, &d);
325201a05beSChao Yu 
326201a05beSChao Yu 	set_page_dirty(ipage);
327201a05beSChao Yu 
328201a05beSChao Yu 	/* update i_size to MAX_INLINE_DATA */
329201a05beSChao Yu 	if (i_size_read(inode) < MAX_INLINE_DATA) {
330201a05beSChao Yu 		i_size_write(inode, MAX_INLINE_DATA);
331201a05beSChao Yu 		set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
332201a05beSChao Yu 	}
333201a05beSChao Yu 	return 0;
334201a05beSChao Yu }
335201a05beSChao Yu 
336d64948a4SJaegeuk Kim static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
337201a05beSChao Yu 				struct f2fs_inline_dentry *inline_dentry)
338201a05beSChao Yu {
339201a05beSChao Yu 	struct page *page;
340201a05beSChao Yu 	struct dnode_of_data dn;
341201a05beSChao Yu 	struct f2fs_dentry_block *dentry_blk;
342201a05beSChao Yu 	int err;
343201a05beSChao Yu 
344201a05beSChao Yu 	page = grab_cache_page(dir->i_mapping, 0);
345201a05beSChao Yu 	if (!page)
346201a05beSChao Yu 		return -ENOMEM;
347201a05beSChao Yu 
348201a05beSChao Yu 	set_new_dnode(&dn, dir, ipage, NULL, 0);
349201a05beSChao Yu 	err = f2fs_reserve_block(&dn, 0);
350201a05beSChao Yu 	if (err)
351201a05beSChao Yu 		goto out;
352201a05beSChao Yu 
353201a05beSChao Yu 	f2fs_wait_on_page_writeback(page, DATA);
354201a05beSChao Yu 	zero_user_segment(page, 0, PAGE_CACHE_SIZE);
355201a05beSChao Yu 
356f1e33a04SJaegeuk Kim 	dentry_blk = kmap_atomic(page);
357201a05beSChao Yu 
358201a05beSChao Yu 	/* copy data from inline dentry block to new dentry block */
359201a05beSChao Yu 	memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
360201a05beSChao Yu 					INLINE_DENTRY_BITMAP_SIZE);
361201a05beSChao Yu 	memcpy(dentry_blk->dentry, inline_dentry->dentry,
362201a05beSChao Yu 			sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
363201a05beSChao Yu 	memcpy(dentry_blk->filename, inline_dentry->filename,
364201a05beSChao Yu 					NR_INLINE_DENTRY * F2FS_SLOT_LEN);
365201a05beSChao Yu 
366f1e33a04SJaegeuk Kim 	kunmap_atomic(dentry_blk);
367201a05beSChao Yu 	SetPageUptodate(page);
368201a05beSChao Yu 	set_page_dirty(page);
369201a05beSChao Yu 
370201a05beSChao Yu 	/* clear inline dir and flag after data writeback */
371b3d208f9SJaegeuk Kim 	truncate_inline_data(ipage, 0);
372b3d208f9SJaegeuk Kim 
3733289c061SJaegeuk Kim 	stat_dec_inline_dir(dir);
374622f28aeSChao Yu 	clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
375201a05beSChao Yu 
376201a05beSChao Yu 	if (i_size_read(dir) < PAGE_CACHE_SIZE) {
377201a05beSChao Yu 		i_size_write(dir, PAGE_CACHE_SIZE);
378201a05beSChao Yu 		set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
379201a05beSChao Yu 	}
380201a05beSChao Yu 
381201a05beSChao Yu 	sync_inode_page(&dn);
382201a05beSChao Yu out:
383201a05beSChao Yu 	f2fs_put_page(page, 1);
384201a05beSChao Yu 	return err;
385201a05beSChao Yu }
386201a05beSChao Yu 
387201a05beSChao Yu int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
388201a05beSChao Yu 						struct inode *inode)
389201a05beSChao Yu {
390201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
391201a05beSChao Yu 	struct page *ipage;
392201a05beSChao Yu 	unsigned int bit_pos;
393201a05beSChao Yu 	f2fs_hash_t name_hash;
394201a05beSChao Yu 	struct f2fs_dir_entry *de;
395201a05beSChao Yu 	size_t namelen = name->len;
396201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk = NULL;
397201a05beSChao Yu 	int slots = GET_DENTRY_SLOTS(namelen);
398201a05beSChao Yu 	struct page *page;
399201a05beSChao Yu 	int err = 0;
400201a05beSChao Yu 	int i;
401201a05beSChao Yu 
402201a05beSChao Yu 	name_hash = f2fs_dentry_hash(name);
403201a05beSChao Yu 
404201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
405201a05beSChao Yu 	if (IS_ERR(ipage))
406201a05beSChao Yu 		return PTR_ERR(ipage);
407201a05beSChao Yu 
408201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
409a82afa20SJaegeuk Kim 	bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
410a82afa20SJaegeuk Kim 						slots, NR_INLINE_DENTRY);
411201a05beSChao Yu 	if (bit_pos >= NR_INLINE_DENTRY) {
412201a05beSChao Yu 		err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
413201a05beSChao Yu 		if (!err)
414201a05beSChao Yu 			err = -EAGAIN;
415201a05beSChao Yu 		goto out;
416201a05beSChao Yu 	}
417201a05beSChao Yu 
418201a05beSChao Yu 	down_write(&F2FS_I(inode)->i_sem);
419bce8d112SJaegeuk Kim 	page = init_inode_metadata(inode, dir, name, ipage);
420201a05beSChao Yu 	if (IS_ERR(page)) {
421201a05beSChao Yu 		err = PTR_ERR(page);
422201a05beSChao Yu 		goto fail;
423201a05beSChao Yu 	}
424bce8d112SJaegeuk Kim 
425bce8d112SJaegeuk Kim 	f2fs_wait_on_page_writeback(ipage, NODE);
426201a05beSChao Yu 	de = &dentry_blk->dentry[bit_pos];
427201a05beSChao Yu 	de->hash_code = name_hash;
428201a05beSChao Yu 	de->name_len = cpu_to_le16(namelen);
429201a05beSChao Yu 	memcpy(dentry_blk->filename[bit_pos], name->name, name->len);
430201a05beSChao Yu 	de->ino = cpu_to_le32(inode->i_ino);
431201a05beSChao Yu 	set_de_type(de, inode);
432201a05beSChao Yu 	for (i = 0; i < slots; i++)
433201a05beSChao Yu 		test_and_set_bit_le(bit_pos + i, &dentry_blk->dentry_bitmap);
434201a05beSChao Yu 	set_page_dirty(ipage);
435201a05beSChao Yu 
436201a05beSChao Yu 	/* we don't need to mark_inode_dirty now */
437201a05beSChao Yu 	F2FS_I(inode)->i_pino = dir->i_ino;
438201a05beSChao Yu 	update_inode(inode, page);
439201a05beSChao Yu 	f2fs_put_page(page, 1);
440201a05beSChao Yu 
441201a05beSChao Yu 	update_parent_metadata(dir, inode, 0);
442201a05beSChao Yu fail:
443201a05beSChao Yu 	up_write(&F2FS_I(inode)->i_sem);
444201a05beSChao Yu 
445201a05beSChao Yu 	if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
446201a05beSChao Yu 		update_inode(dir, ipage);
447201a05beSChao Yu 		clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
448201a05beSChao Yu 	}
449201a05beSChao Yu out:
450201a05beSChao Yu 	f2fs_put_page(ipage, 1);
451201a05beSChao Yu 	return err;
452201a05beSChao Yu }
453201a05beSChao Yu 
454201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
455201a05beSChao Yu 					struct inode *dir, struct inode *inode)
456201a05beSChao Yu {
457201a05beSChao Yu 	struct f2fs_inline_dentry *inline_dentry;
458201a05beSChao Yu 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
459201a05beSChao Yu 	unsigned int bit_pos;
460201a05beSChao Yu 	int i;
461201a05beSChao Yu 
462201a05beSChao Yu 	lock_page(page);
46359a06155SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, NODE);
464201a05beSChao Yu 
465201a05beSChao Yu 	inline_dentry = inline_data_addr(page);
466201a05beSChao Yu 	bit_pos = dentry - inline_dentry->dentry;
467201a05beSChao Yu 	for (i = 0; i < slots; i++)
468201a05beSChao Yu 		test_and_clear_bit_le(bit_pos + i,
469201a05beSChao Yu 				&inline_dentry->dentry_bitmap);
470201a05beSChao Yu 
471201a05beSChao Yu 	set_page_dirty(page);
472201a05beSChao Yu 
473201a05beSChao Yu 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
474201a05beSChao Yu 
475201a05beSChao Yu 	if (inode)
476201a05beSChao Yu 		f2fs_drop_nlink(dir, inode, page);
477201a05beSChao Yu 
478201a05beSChao Yu 	f2fs_put_page(page, 1);
479201a05beSChao Yu }
480201a05beSChao Yu 
481201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir)
482201a05beSChao Yu {
483201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
484201a05beSChao Yu 	struct page *ipage;
485201a05beSChao Yu 	unsigned int bit_pos = 2;
486201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk;
487201a05beSChao Yu 
488201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
489201a05beSChao Yu 	if (IS_ERR(ipage))
490201a05beSChao Yu 		return false;
491201a05beSChao Yu 
492201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
493201a05beSChao Yu 	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
494201a05beSChao Yu 					NR_INLINE_DENTRY,
495201a05beSChao Yu 					bit_pos);
496201a05beSChao Yu 
497201a05beSChao Yu 	f2fs_put_page(ipage, 1);
498201a05beSChao Yu 
499201a05beSChao Yu 	if (bit_pos < NR_INLINE_DENTRY)
500201a05beSChao Yu 		return false;
501201a05beSChao Yu 
502201a05beSChao Yu 	return true;
503201a05beSChao Yu }
504201a05beSChao Yu 
505201a05beSChao Yu int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx)
506201a05beSChao Yu {
507201a05beSChao Yu 	struct inode *inode = file_inode(file);
508201a05beSChao Yu 	struct f2fs_inline_dentry *inline_dentry = NULL;
509201a05beSChao Yu 	struct page *ipage = NULL;
5107b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
511201a05beSChao Yu 
512201a05beSChao Yu 	if (ctx->pos == NR_INLINE_DENTRY)
513201a05beSChao Yu 		return 0;
514201a05beSChao Yu 
51538594de7SJaegeuk Kim 	ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
516201a05beSChao Yu 	if (IS_ERR(ipage))
517201a05beSChao Yu 		return PTR_ERR(ipage);
518201a05beSChao Yu 
519201a05beSChao Yu 	inline_dentry = inline_data_addr(ipage);
520201a05beSChao Yu 
5217b3cd7d6SJaegeuk Kim 	make_dentry_ptr(&d, (void *)inline_dentry, 2);
5227b3cd7d6SJaegeuk Kim 
5237b3cd7d6SJaegeuk Kim 	if (!f2fs_fill_dentries(ctx, &d, 0))
524201a05beSChao Yu 		ctx->pos = NR_INLINE_DENTRY;
525201a05beSChao Yu 
52638594de7SJaegeuk Kim 	f2fs_put_page(ipage, 1);
527201a05beSChao Yu 	return 0;
528201a05beSChao Yu }
529