xref: /openbmc/linux/fs/f2fs/inline.c (revision d8c6822a0556a88b13c8483599589752cf3e39f7)
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 
1601b960e9SJaegeuk Kim bool f2fs_may_inline_data(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 
24368a0e40SWanpeng Li 	if (!S_ISREG(inode->i_mode) && !S_ISLNK(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 
30fcc85a4dSJaegeuk Kim 	if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
31fcc85a4dSJaegeuk Kim 		return false;
32fcc85a4dSJaegeuk Kim 
33e18c65b2SHuajun Li 	return true;
34e18c65b2SHuajun Li }
35e18c65b2SHuajun Li 
3601b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *inode)
3701b960e9SJaegeuk Kim {
3801b960e9SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
3901b960e9SJaegeuk Kim 		return false;
4001b960e9SJaegeuk Kim 
4101b960e9SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
4201b960e9SJaegeuk Kim 		return false;
4301b960e9SJaegeuk Kim 
4401b960e9SJaegeuk Kim 	return true;
4501b960e9SJaegeuk Kim }
4601b960e9SJaegeuk Kim 
47b3d208f9SJaegeuk Kim void read_inline_data(struct page *page, struct page *ipage)
48e18c65b2SHuajun Li {
49e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
50e18c65b2SHuajun Li 
51b3d208f9SJaegeuk Kim 	if (PageUptodate(page))
52b3d208f9SJaegeuk Kim 		return;
5304a17fb1SChao Yu 
54b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_P_SB(page), page->index);
55e18c65b2SHuajun Li 
5618309aaaSChao Yu 	zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
57e18c65b2SHuajun Li 
58e18c65b2SHuajun Li 	/* Copy the whole inline data block */
59e18c65b2SHuajun Li 	src_addr = inline_data_addr(ipage);
60f1e33a04SJaegeuk Kim 	dst_addr = kmap_atomic(page);
61e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
62427a45c8SJaegeuk Kim 	flush_dcache_page(page);
63f1e33a04SJaegeuk Kim 	kunmap_atomic(dst_addr);
64e18c65b2SHuajun Li 	SetPageUptodate(page);
65b3d208f9SJaegeuk Kim }
66e18c65b2SHuajun Li 
670bfcfccaSChao Yu bool truncate_inline_inode(struct page *ipage, u64 from)
68feeb0debSChao Yu {
690bfcfccaSChao Yu 	void *addr;
700bfcfccaSChao Yu 
710bfcfccaSChao Yu 	if (from >= MAX_INLINE_DATA)
720bfcfccaSChao Yu 		return false;
730bfcfccaSChao Yu 
740bfcfccaSChao Yu 	addr = inline_data_addr(ipage);
750bfcfccaSChao Yu 
76feeb0debSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE);
770bfcfccaSChao Yu 	memset(addr + from, 0, MAX_INLINE_DATA - from);
780bfcfccaSChao Yu 
790bfcfccaSChao Yu 	return true;
80feeb0debSChao Yu }
81feeb0debSChao Yu 
82b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page)
83b3d208f9SJaegeuk Kim {
84b3d208f9SJaegeuk Kim 	struct page *ipage;
85b3d208f9SJaegeuk Kim 
86b3d208f9SJaegeuk Kim 	ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
87b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
88b3d208f9SJaegeuk Kim 		unlock_page(page);
89b3d208f9SJaegeuk Kim 		return PTR_ERR(ipage);
90b3d208f9SJaegeuk Kim 	}
91b3d208f9SJaegeuk Kim 
92b3d208f9SJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
93b3d208f9SJaegeuk Kim 		f2fs_put_page(ipage, 1);
94b3d208f9SJaegeuk Kim 		return -EAGAIN;
95b3d208f9SJaegeuk Kim 	}
96b3d208f9SJaegeuk Kim 
97b3d208f9SJaegeuk Kim 	if (page->index)
98b3d208f9SJaegeuk Kim 		zero_user_segment(page, 0, PAGE_CACHE_SIZE);
99b3d208f9SJaegeuk Kim 	else
100b3d208f9SJaegeuk Kim 		read_inline_data(page, ipage);
101b3d208f9SJaegeuk Kim 
102b3d208f9SJaegeuk Kim 	SetPageUptodate(page);
103b3d208f9SJaegeuk Kim 	f2fs_put_page(ipage, 1);
104b3d208f9SJaegeuk Kim 	unlock_page(page);
105e18c65b2SHuajun Li 	return 0;
106e18c65b2SHuajun Li }
107e18c65b2SHuajun Li 
108b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
109e18c65b2SHuajun Li {
110e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
111e18c65b2SHuajun Li 	struct f2fs_io_info fio = {
11205ca3632SJaegeuk Kim 		.sbi = F2FS_I_SB(dn->inode),
113e18c65b2SHuajun Li 		.type = DATA,
114e18c65b2SHuajun Li 		.rw = WRITE_SYNC | REQ_PRIO,
11505ca3632SJaegeuk Kim 		.page = page,
1164375a336SJaegeuk Kim 		.encrypted_page = NULL,
117e18c65b2SHuajun Li 	};
118158c194cSJaegeuk Kim 	int dirty, err;
119e18c65b2SHuajun Li 
120b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
121e18c65b2SHuajun Li 
122b3d208f9SJaegeuk Kim 	if (!f2fs_exist_data(dn->inode))
123b3d208f9SJaegeuk Kim 		goto clear_out;
124ec4e7af4SJaegeuk Kim 
125b3d208f9SJaegeuk Kim 	err = f2fs_reserve_block(dn, 0);
12615c6e3aaSJaegeuk Kim 	if (err)
127b3d208f9SJaegeuk Kim 		return err;
128e18c65b2SHuajun Li 
1299ac1349aSJaegeuk Kim 	f2fs_wait_on_page_writeback(page, DATA);
130b3d208f9SJaegeuk Kim 
131b3d208f9SJaegeuk Kim 	if (PageUptodate(page))
132b3d208f9SJaegeuk Kim 		goto no_update;
133b3d208f9SJaegeuk Kim 
13418309aaaSChao Yu 	zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
135e18c65b2SHuajun Li 
136e18c65b2SHuajun Li 	/* Copy the whole inline data block */
137b3d208f9SJaegeuk Kim 	src_addr = inline_data_addr(dn->inode_page);
138f1e33a04SJaegeuk Kim 	dst_addr = kmap_atomic(page);
139e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
14009b8b3c8SJaegeuk Kim 	flush_dcache_page(page);
141f1e33a04SJaegeuk Kim 	kunmap_atomic(dst_addr);
1429e09fc85SJaegeuk Kim 	SetPageUptodate(page);
143b3d208f9SJaegeuk Kim no_update:
144158c194cSJaegeuk Kim 	/* clear dirty state */
145158c194cSJaegeuk Kim 	dirty = clear_page_dirty_for_io(page);
146158c194cSJaegeuk Kim 
147e18c65b2SHuajun Li 	/* write data page to try to make data consistent */
148e18c65b2SHuajun Li 	set_page_writeback(page);
149cf04e8ebSJaegeuk Kim 	fio.blk_addr = dn->data_blkaddr;
15005ca3632SJaegeuk Kim 	write_data_page(dn, &fio);
151216a620aSChao Yu 	set_data_blkaddr(dn);
1527e4dde79SChao Yu 	f2fs_update_extent_cache(dn);
1535514f0aaSYuan Zhong 	f2fs_wait_on_page_writeback(page, DATA);
154158c194cSJaegeuk Kim 	if (dirty)
155158c194cSJaegeuk Kim 		inode_dec_dirty_pages(dn->inode);
156e18c65b2SHuajun Li 
15795f5b0fcSJaegeuk Kim 	/* this converted inline_data should be recovered. */
15895f5b0fcSJaegeuk Kim 	set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
15995f5b0fcSJaegeuk Kim 
160e18c65b2SHuajun Li 	/* clear inline data and flag after data writeback */
1610bfcfccaSChao Yu 	truncate_inline_inode(dn->inode_page, 0);
162b3d208f9SJaegeuk Kim clear_out:
163b3d208f9SJaegeuk Kim 	stat_dec_inline_inode(dn->inode);
16457e2a2c0SJaegeuk Kim 	f2fs_clear_inline_inode(dn->inode);
165b3d208f9SJaegeuk Kim 	sync_inode_page(dn);
166b3d208f9SJaegeuk Kim 	f2fs_put_dnode(dn);
167b3d208f9SJaegeuk Kim 	return 0;
168e18c65b2SHuajun Li }
169e18c65b2SHuajun Li 
170b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode)
171e18c65b2SHuajun Li {
172b3d208f9SJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
173b3d208f9SJaegeuk Kim 	struct dnode_of_data dn;
174b3d208f9SJaegeuk Kim 	struct page *ipage, *page;
175b3d208f9SJaegeuk Kim 	int err = 0;
176e18c65b2SHuajun Li 
177b3d208f9SJaegeuk Kim 	page = grab_cache_page(inode->i_mapping, 0);
178b3d208f9SJaegeuk Kim 	if (!page)
1799e09fc85SJaegeuk Kim 		return -ENOMEM;
180b3d208f9SJaegeuk Kim 
181b3d208f9SJaegeuk Kim 	f2fs_lock_op(sbi);
182b3d208f9SJaegeuk Kim 
183b3d208f9SJaegeuk Kim 	ipage = get_node_page(sbi, inode->i_ino);
184b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
1856d20aff8SJaegeuk Kim 		err = PTR_ERR(ipage);
1866d20aff8SJaegeuk Kim 		goto out;
187b067ba1fSJaegeuk Kim 	}
188e18c65b2SHuajun Li 
189b3d208f9SJaegeuk Kim 	set_new_dnode(&dn, inode, ipage, ipage, 0);
190b3d208f9SJaegeuk Kim 
191b3d208f9SJaegeuk Kim 	if (f2fs_has_inline_data(inode))
192b3d208f9SJaegeuk Kim 		err = f2fs_convert_inline_page(&dn, page);
193b3d208f9SJaegeuk Kim 
194b3d208f9SJaegeuk Kim 	f2fs_put_dnode(&dn);
1956d20aff8SJaegeuk Kim out:
196b3d208f9SJaegeuk Kim 	f2fs_unlock_op(sbi);
197b3d208f9SJaegeuk Kim 
198b3d208f9SJaegeuk Kim 	f2fs_put_page(page, 1);
199e18c65b2SHuajun Li 	return err;
200e18c65b2SHuajun Li }
201e18c65b2SHuajun Li 
202b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page)
203e18c65b2SHuajun Li {
204e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
205e18c65b2SHuajun Li 	struct dnode_of_data dn;
206e18c65b2SHuajun Li 	int err;
207e18c65b2SHuajun Li 
208e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, NULL, NULL, 0);
209e18c65b2SHuajun Li 	err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
210e18c65b2SHuajun Li 	if (err)
211e18c65b2SHuajun Li 		return err;
212e18c65b2SHuajun Li 
213c08a690bSJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
214b3d208f9SJaegeuk Kim 		f2fs_put_dnode(&dn);
215b3d208f9SJaegeuk Kim 		return -EAGAIN;
216c08a690bSJaegeuk Kim 	}
217c08a690bSJaegeuk Kim 
218b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_I_SB(inode), page->index);
219b3d208f9SJaegeuk Kim 
220b3d208f9SJaegeuk Kim 	f2fs_wait_on_page_writeback(dn.inode_page, NODE);
221f1e33a04SJaegeuk Kim 	src_addr = kmap_atomic(page);
222b3d208f9SJaegeuk Kim 	dst_addr = inline_data_addr(dn.inode_page);
223b3d208f9SJaegeuk Kim 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
224f1e33a04SJaegeuk Kim 	kunmap_atomic(src_addr);
225e18c65b2SHuajun Li 
226fff04f90SJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
227b3d208f9SJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
228b3d208f9SJaegeuk Kim 
229e18c65b2SHuajun Li 	sync_inode_page(&dn);
230e18c65b2SHuajun Li 	f2fs_put_dnode(&dn);
231e18c65b2SHuajun Li 	return 0;
232e18c65b2SHuajun Li }
2331e1bb4baSJaegeuk Kim 
2340342fd30SJaegeuk Kim bool recover_inline_data(struct inode *inode, struct page *npage)
2351e1bb4baSJaegeuk Kim {
2364081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2371e1bb4baSJaegeuk Kim 	struct f2fs_inode *ri = NULL;
2381e1bb4baSJaegeuk Kim 	void *src_addr, *dst_addr;
2391e1bb4baSJaegeuk Kim 	struct page *ipage;
2401e1bb4baSJaegeuk Kim 
2411e1bb4baSJaegeuk Kim 	/*
2421e1bb4baSJaegeuk Kim 	 * The inline_data recovery policy is as follows.
2431e1bb4baSJaegeuk Kim 	 * [prev.] [next] of inline_data flag
2441e1bb4baSJaegeuk Kim 	 *    o       o  -> recover inline_data
2451e1bb4baSJaegeuk Kim 	 *    o       x  -> remove inline_data, and then recover data blocks
2461e1bb4baSJaegeuk Kim 	 *    x       o  -> remove inline_data, and then recover inline_data
2471e1bb4baSJaegeuk Kim 	 *    x       x  -> recover data blocks
2481e1bb4baSJaegeuk Kim 	 */
2491e1bb4baSJaegeuk Kim 	if (IS_INODE(npage))
2501e1bb4baSJaegeuk Kim 		ri = F2FS_INODE(npage);
2511e1bb4baSJaegeuk Kim 
2521e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode) &&
2530342fd30SJaegeuk Kim 			ri && (ri->i_inline & F2FS_INLINE_DATA)) {
2541e1bb4baSJaegeuk Kim process_inline:
2551e1bb4baSJaegeuk Kim 		ipage = get_node_page(sbi, inode->i_ino);
2569850cf4aSJaegeuk Kim 		f2fs_bug_on(sbi, IS_ERR(ipage));
2571e1bb4baSJaegeuk Kim 
25854b591dfSJaegeuk Kim 		f2fs_wait_on_page_writeback(ipage, NODE);
25954b591dfSJaegeuk Kim 
2601e1bb4baSJaegeuk Kim 		src_addr = inline_data_addr(npage);
2611e1bb4baSJaegeuk Kim 		dst_addr = inline_data_addr(ipage);
2621e1bb4baSJaegeuk Kim 		memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
263b3d208f9SJaegeuk Kim 
264b3d208f9SJaegeuk Kim 		set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
265b3d208f9SJaegeuk Kim 		set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
266b3d208f9SJaegeuk Kim 
2671e1bb4baSJaegeuk Kim 		update_inode(inode, ipage);
2681e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
2690342fd30SJaegeuk Kim 		return true;
2701e1bb4baSJaegeuk Kim 	}
2711e1bb4baSJaegeuk Kim 
2721e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode)) {
2731e1bb4baSJaegeuk Kim 		ipage = get_node_page(sbi, inode->i_ino);
2749850cf4aSJaegeuk Kim 		f2fs_bug_on(sbi, IS_ERR(ipage));
2750bfcfccaSChao Yu 		truncate_inline_inode(ipage, 0);
276b3d208f9SJaegeuk Kim 		f2fs_clear_inline_inode(inode);
2771e1bb4baSJaegeuk Kim 		update_inode(inode, ipage);
2781e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
2790342fd30SJaegeuk Kim 	} else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
280764aa3e9SJaegeuk Kim 		truncate_blocks(inode, 0, false);
2811e1bb4baSJaegeuk Kim 		goto process_inline;
2821e1bb4baSJaegeuk Kim 	}
2830342fd30SJaegeuk Kim 	return false;
2841e1bb4baSJaegeuk Kim }
285201a05beSChao Yu 
286201a05beSChao Yu struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
287201a05beSChao Yu 				struct qstr *name, struct page **res_page)
288201a05beSChao Yu {
289201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
2904e6ebf6dSJaegeuk Kim 	struct f2fs_inline_dentry *inline_dentry;
291201a05beSChao Yu 	struct f2fs_dir_entry *de;
2927b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
2934e6ebf6dSJaegeuk Kim 	struct page *ipage;
294201a05beSChao Yu 
295201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
296201a05beSChao Yu 	if (IS_ERR(ipage))
297201a05beSChao Yu 		return NULL;
298201a05beSChao Yu 
2994e6ebf6dSJaegeuk Kim 	inline_dentry = inline_data_addr(ipage);
300201a05beSChao Yu 
301*d8c6822aSJaegeuk Kim 	make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
3027b3cd7d6SJaegeuk Kim 	de = find_target_dentry(name, NULL, &d);
3037b3cd7d6SJaegeuk Kim 
3044e6ebf6dSJaegeuk Kim 	unlock_page(ipage);
3054e6ebf6dSJaegeuk Kim 	if (de)
306201a05beSChao Yu 		*res_page = ipage;
3074e6ebf6dSJaegeuk Kim 	else
3084e6ebf6dSJaegeuk Kim 		f2fs_put_page(ipage, 0);
309201a05beSChao Yu 
310201a05beSChao Yu 	/*
311201a05beSChao Yu 	 * For the most part, it should be a bug when name_len is zero.
3124e6ebf6dSJaegeuk Kim 	 * We stop here for figuring out where the bugs has occurred.
313201a05beSChao Yu 	 */
3147b3cd7d6SJaegeuk Kim 	f2fs_bug_on(sbi, d.max < 0);
315201a05beSChao Yu 	return de;
316201a05beSChao Yu }
317201a05beSChao Yu 
318201a05beSChao Yu struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
319201a05beSChao Yu 							struct page **p)
320201a05beSChao Yu {
321201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
322201a05beSChao Yu 	struct page *ipage;
323201a05beSChao Yu 	struct f2fs_dir_entry *de;
324201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk;
325201a05beSChao Yu 
326201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
327201a05beSChao Yu 	if (IS_ERR(ipage))
328201a05beSChao Yu 		return NULL;
329201a05beSChao Yu 
330201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
331201a05beSChao Yu 	de = &dentry_blk->dentry[1];
332201a05beSChao Yu 	*p = ipage;
333201a05beSChao Yu 	unlock_page(ipage);
334201a05beSChao Yu 	return de;
335201a05beSChao Yu }
336201a05beSChao Yu 
337201a05beSChao Yu int make_empty_inline_dir(struct inode *inode, struct inode *parent,
338201a05beSChao Yu 							struct page *ipage)
339201a05beSChao Yu {
340201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk;
341062a3e7bSJaegeuk Kim 	struct f2fs_dentry_ptr d;
342201a05beSChao Yu 
343201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
344201a05beSChao Yu 
345*d8c6822aSJaegeuk Kim 	make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
346062a3e7bSJaegeuk Kim 	do_make_empty_dir(inode, parent, &d);
347201a05beSChao Yu 
348201a05beSChao Yu 	set_page_dirty(ipage);
349201a05beSChao Yu 
350201a05beSChao Yu 	/* update i_size to MAX_INLINE_DATA */
351201a05beSChao Yu 	if (i_size_read(inode) < MAX_INLINE_DATA) {
352201a05beSChao Yu 		i_size_write(inode, MAX_INLINE_DATA);
353201a05beSChao Yu 		set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
354201a05beSChao Yu 	}
355201a05beSChao Yu 	return 0;
356201a05beSChao Yu }
357201a05beSChao Yu 
358d64948a4SJaegeuk Kim static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
359201a05beSChao Yu 				struct f2fs_inline_dentry *inline_dentry)
360201a05beSChao Yu {
361201a05beSChao Yu 	struct page *page;
362201a05beSChao Yu 	struct dnode_of_data dn;
363201a05beSChao Yu 	struct f2fs_dentry_block *dentry_blk;
364201a05beSChao Yu 	int err;
365201a05beSChao Yu 
366201a05beSChao Yu 	page = grab_cache_page(dir->i_mapping, 0);
367201a05beSChao Yu 	if (!page)
368201a05beSChao Yu 		return -ENOMEM;
369201a05beSChao Yu 
370201a05beSChao Yu 	set_new_dnode(&dn, dir, ipage, NULL, 0);
371201a05beSChao Yu 	err = f2fs_reserve_block(&dn, 0);
372201a05beSChao Yu 	if (err)
373201a05beSChao Yu 		goto out;
374201a05beSChao Yu 
375201a05beSChao Yu 	f2fs_wait_on_page_writeback(page, DATA);
376201a05beSChao Yu 	zero_user_segment(page, 0, PAGE_CACHE_SIZE);
377201a05beSChao Yu 
378f1e33a04SJaegeuk Kim 	dentry_blk = kmap_atomic(page);
379201a05beSChao Yu 
380201a05beSChao Yu 	/* copy data from inline dentry block to new dentry block */
381201a05beSChao Yu 	memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
382201a05beSChao Yu 					INLINE_DENTRY_BITMAP_SIZE);
383201a05beSChao Yu 	memcpy(dentry_blk->dentry, inline_dentry->dentry,
384201a05beSChao Yu 			sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
385201a05beSChao Yu 	memcpy(dentry_blk->filename, inline_dentry->filename,
386201a05beSChao Yu 					NR_INLINE_DENTRY * F2FS_SLOT_LEN);
387201a05beSChao Yu 
388f1e33a04SJaegeuk Kim 	kunmap_atomic(dentry_blk);
389201a05beSChao Yu 	SetPageUptodate(page);
390201a05beSChao Yu 	set_page_dirty(page);
391201a05beSChao Yu 
392201a05beSChao Yu 	/* clear inline dir and flag after data writeback */
3930bfcfccaSChao Yu 	truncate_inline_inode(ipage, 0);
394b3d208f9SJaegeuk Kim 
3953289c061SJaegeuk Kim 	stat_dec_inline_dir(dir);
396622f28aeSChao Yu 	clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
397201a05beSChao Yu 
398201a05beSChao Yu 	if (i_size_read(dir) < PAGE_CACHE_SIZE) {
399201a05beSChao Yu 		i_size_write(dir, PAGE_CACHE_SIZE);
400201a05beSChao Yu 		set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
401201a05beSChao Yu 	}
402201a05beSChao Yu 
403201a05beSChao Yu 	sync_inode_page(&dn);
404201a05beSChao Yu out:
405201a05beSChao Yu 	f2fs_put_page(page, 1);
406201a05beSChao Yu 	return err;
407201a05beSChao Yu }
408201a05beSChao Yu 
409201a05beSChao Yu int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
410510022a8SJaegeuk Kim 			struct inode *inode, nid_t ino, umode_t mode)
411201a05beSChao Yu {
412201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
413201a05beSChao Yu 	struct page *ipage;
414201a05beSChao Yu 	unsigned int bit_pos;
415201a05beSChao Yu 	f2fs_hash_t name_hash;
416201a05beSChao Yu 	size_t namelen = name->len;
417201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk = NULL;
4183b4d732aSChao Yu 	struct f2fs_dentry_ptr d;
419201a05beSChao Yu 	int slots = GET_DENTRY_SLOTS(namelen);
420510022a8SJaegeuk Kim 	struct page *page = NULL;
421201a05beSChao Yu 	int err = 0;
422201a05beSChao Yu 
423201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
424201a05beSChao Yu 	if (IS_ERR(ipage))
425201a05beSChao Yu 		return PTR_ERR(ipage);
426201a05beSChao Yu 
427201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
428a82afa20SJaegeuk Kim 	bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
429a82afa20SJaegeuk Kim 						slots, NR_INLINE_DENTRY);
430201a05beSChao Yu 	if (bit_pos >= NR_INLINE_DENTRY) {
431201a05beSChao Yu 		err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
432201a05beSChao Yu 		if (!err)
433201a05beSChao Yu 			err = -EAGAIN;
434201a05beSChao Yu 		goto out;
435201a05beSChao Yu 	}
436201a05beSChao Yu 
437510022a8SJaegeuk Kim 	if (inode) {
438201a05beSChao Yu 		down_write(&F2FS_I(inode)->i_sem);
439bce8d112SJaegeuk Kim 		page = init_inode_metadata(inode, dir, name, ipage);
440201a05beSChao Yu 		if (IS_ERR(page)) {
441201a05beSChao Yu 			err = PTR_ERR(page);
442201a05beSChao Yu 			goto fail;
443201a05beSChao Yu 		}
444510022a8SJaegeuk Kim 	}
445bce8d112SJaegeuk Kim 
446bce8d112SJaegeuk Kim 	f2fs_wait_on_page_writeback(ipage, NODE);
4473b4d732aSChao Yu 
4483b4d732aSChao Yu 	name_hash = f2fs_dentry_hash(name);
449*d8c6822aSJaegeuk Kim 	make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
450510022a8SJaegeuk Kim 	f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
4513b4d732aSChao Yu 
452201a05beSChao Yu 	set_page_dirty(ipage);
453201a05beSChao Yu 
454201a05beSChao Yu 	/* we don't need to mark_inode_dirty now */
455510022a8SJaegeuk Kim 	if (inode) {
456201a05beSChao Yu 		F2FS_I(inode)->i_pino = dir->i_ino;
457201a05beSChao Yu 		update_inode(inode, page);
458201a05beSChao Yu 		f2fs_put_page(page, 1);
459510022a8SJaegeuk Kim 	}
460201a05beSChao Yu 
461201a05beSChao Yu 	update_parent_metadata(dir, inode, 0);
462201a05beSChao Yu fail:
463510022a8SJaegeuk Kim 	if (inode)
464201a05beSChao Yu 		up_write(&F2FS_I(inode)->i_sem);
465201a05beSChao Yu 
466201a05beSChao Yu 	if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
467201a05beSChao Yu 		update_inode(dir, ipage);
468201a05beSChao Yu 		clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
469201a05beSChao Yu 	}
470201a05beSChao Yu out:
471201a05beSChao Yu 	f2fs_put_page(ipage, 1);
472201a05beSChao Yu 	return err;
473201a05beSChao Yu }
474201a05beSChao Yu 
475201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
476201a05beSChao Yu 					struct inode *dir, struct inode *inode)
477201a05beSChao Yu {
478201a05beSChao Yu 	struct f2fs_inline_dentry *inline_dentry;
479201a05beSChao Yu 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
480201a05beSChao Yu 	unsigned int bit_pos;
481201a05beSChao Yu 	int i;
482201a05beSChao Yu 
483201a05beSChao Yu 	lock_page(page);
48459a06155SJaegeuk Kim 	f2fs_wait_on_page_writeback(page, NODE);
485201a05beSChao Yu 
486201a05beSChao Yu 	inline_dentry = inline_data_addr(page);
487201a05beSChao Yu 	bit_pos = dentry - inline_dentry->dentry;
488201a05beSChao Yu 	for (i = 0; i < slots; i++)
489201a05beSChao Yu 		test_and_clear_bit_le(bit_pos + i,
490201a05beSChao Yu 				&inline_dentry->dentry_bitmap);
491201a05beSChao Yu 
492201a05beSChao Yu 	set_page_dirty(page);
493201a05beSChao Yu 
494201a05beSChao Yu 	dir->i_ctime = dir->i_mtime = CURRENT_TIME;
495201a05beSChao Yu 
496201a05beSChao Yu 	if (inode)
497201a05beSChao Yu 		f2fs_drop_nlink(dir, inode, page);
498201a05beSChao Yu 
499201a05beSChao Yu 	f2fs_put_page(page, 1);
500201a05beSChao Yu }
501201a05beSChao Yu 
502201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir)
503201a05beSChao Yu {
504201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
505201a05beSChao Yu 	struct page *ipage;
506201a05beSChao Yu 	unsigned int bit_pos = 2;
507201a05beSChao Yu 	struct f2fs_inline_dentry *dentry_blk;
508201a05beSChao Yu 
509201a05beSChao Yu 	ipage = get_node_page(sbi, dir->i_ino);
510201a05beSChao Yu 	if (IS_ERR(ipage))
511201a05beSChao Yu 		return false;
512201a05beSChao Yu 
513201a05beSChao Yu 	dentry_blk = inline_data_addr(ipage);
514201a05beSChao Yu 	bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
515201a05beSChao Yu 					NR_INLINE_DENTRY,
516201a05beSChao Yu 					bit_pos);
517201a05beSChao Yu 
518201a05beSChao Yu 	f2fs_put_page(ipage, 1);
519201a05beSChao Yu 
520201a05beSChao Yu 	if (bit_pos < NR_INLINE_DENTRY)
521201a05beSChao Yu 		return false;
522201a05beSChao Yu 
523201a05beSChao Yu 	return true;
524201a05beSChao Yu }
525201a05beSChao Yu 
526*d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
527*d8c6822aSJaegeuk Kim 				struct f2fs_str *fstr)
528201a05beSChao Yu {
529201a05beSChao Yu 	struct inode *inode = file_inode(file);
530201a05beSChao Yu 	struct f2fs_inline_dentry *inline_dentry = NULL;
531201a05beSChao Yu 	struct page *ipage = NULL;
5327b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
533201a05beSChao Yu 
534201a05beSChao Yu 	if (ctx->pos == NR_INLINE_DENTRY)
535201a05beSChao Yu 		return 0;
536201a05beSChao Yu 
53738594de7SJaegeuk Kim 	ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
538201a05beSChao Yu 	if (IS_ERR(ipage))
539201a05beSChao Yu 		return PTR_ERR(ipage);
540201a05beSChao Yu 
541201a05beSChao Yu 	inline_dentry = inline_data_addr(ipage);
542201a05beSChao Yu 
543*d8c6822aSJaegeuk Kim 	make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
5447b3cd7d6SJaegeuk Kim 
545*d8c6822aSJaegeuk Kim 	if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
546201a05beSChao Yu 		ctx->pos = NR_INLINE_DENTRY;
547201a05beSChao Yu 
54838594de7SJaegeuk Kim 	f2fs_put_page(ipage, 1);
549201a05beSChao Yu 	return 0;
550201a05beSChao Yu }
551