xref: /openbmc/linux/fs/f2fs/inline.c (revision ac8f933664c3a0e2d42f6ee9a2a6d25f87cb23f6)
17c1a000dSChao Yu // SPDX-License-Identifier: GPL-2.0
2e18c65b2SHuajun Li /*
3e18c65b2SHuajun Li  * fs/f2fs/inline.c
4e18c65b2SHuajun Li  * Copyright (c) 2013, Intel Corporation
5e18c65b2SHuajun Li  * Authors: Huajun Li <huajun.li@intel.com>
6e18c65b2SHuajun Li  *          Haicheng Li <haicheng.li@intel.com>
7e18c65b2SHuajun Li  */
8e18c65b2SHuajun Li 
9e18c65b2SHuajun Li #include <linux/fs.h>
10e18c65b2SHuajun Li #include <linux/f2fs_fs.h>
1110c5db28SChristoph Hellwig #include <linux/fiemap.h>
12e18c65b2SHuajun Li 
13e18c65b2SHuajun Li #include "f2fs.h"
1467f8cf3cSJaegeuk Kim #include "node.h"
15dd5a09bdSChao Yu #include <trace/events/f2fs.h>
16e18c65b2SHuajun Li 
support_inline_data(struct inode * inode)17677a82b4SChao Yu static bool support_inline_data(struct inode *inode)
18e18c65b2SHuajun Li {
19b82d4300SSunmin Jeong 	if (f2fs_used_in_atomic_write(inode))
2088b88a66SJaegeuk Kim 		return false;
21368a0e40SWanpeng Li 	if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
22e18c65b2SHuajun Li 		return false;
23f2470371SChao Yu 	if (i_size_read(inode) > MAX_INLINE_DATA(inode))
2492dffd01SJaegeuk Kim 		return false;
25677a82b4SChao Yu 	return true;
26677a82b4SChao Yu }
2792dffd01SJaegeuk Kim 
f2fs_may_inline_data(struct inode * inode)28677a82b4SChao Yu bool f2fs_may_inline_data(struct inode *inode)
29677a82b4SChao Yu {
30677a82b4SChao Yu 	if (!support_inline_data(inode))
31fcc85a4dSJaegeuk Kim 		return false;
32fcc85a4dSJaegeuk Kim 
33677a82b4SChao Yu 	return !f2fs_post_read_required(inode);
34677a82b4SChao Yu }
35677a82b4SChao Yu 
inode_has_blocks(struct inode * inode,struct page * ipage)36*6290d3f5SChao Yu static bool inode_has_blocks(struct inode *inode, struct page *ipage)
37*6290d3f5SChao Yu {
38*6290d3f5SChao Yu 	struct f2fs_inode *ri = F2FS_INODE(ipage);
39*6290d3f5SChao Yu 	int i;
40*6290d3f5SChao Yu 
41*6290d3f5SChao Yu 	if (F2FS_HAS_BLOCKS(inode))
42*6290d3f5SChao Yu 		return true;
43*6290d3f5SChao Yu 
44*6290d3f5SChao Yu 	for (i = 0; i < DEF_NIDS_PER_INODE; i++) {
45*6290d3f5SChao Yu 		if (ri->i_nid[i])
46*6290d3f5SChao Yu 			return true;
47*6290d3f5SChao Yu 	}
48*6290d3f5SChao Yu 	return false;
49*6290d3f5SChao Yu }
50*6290d3f5SChao Yu 
f2fs_sanity_check_inline_data(struct inode * inode,struct page * ipage)51*6290d3f5SChao Yu bool f2fs_sanity_check_inline_data(struct inode *inode, struct page *ipage)
52677a82b4SChao Yu {
53677a82b4SChao Yu 	if (!f2fs_has_inline_data(inode))
54677a82b4SChao Yu 		return false;
55677a82b4SChao Yu 
56*6290d3f5SChao Yu 	if (inode_has_blocks(inode, ipage))
57*6290d3f5SChao Yu 		return false;
58*6290d3f5SChao Yu 
59677a82b4SChao Yu 	if (!support_inline_data(inode))
60e18c65b2SHuajun Li 		return true;
61677a82b4SChao Yu 
62677a82b4SChao Yu 	/*
63677a82b4SChao Yu 	 * used by sanity_check_inode(), when disk layout fields has not
64677a82b4SChao Yu 	 * been synchronized to inmem fields.
65677a82b4SChao Yu 	 */
66677a82b4SChao Yu 	return (S_ISREG(inode->i_mode) &&
67677a82b4SChao Yu 		(file_is_encrypt(inode) || file_is_verity(inode) ||
68677a82b4SChao Yu 		(F2FS_I(inode)->i_flags & F2FS_COMPR_FL)));
69e18c65b2SHuajun Li }
70e18c65b2SHuajun Li 
f2fs_may_inline_dentry(struct inode * inode)7101b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *inode)
7201b960e9SJaegeuk Kim {
7301b960e9SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
7401b960e9SJaegeuk Kim 		return false;
7501b960e9SJaegeuk Kim 
7601b960e9SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
7701b960e9SJaegeuk Kim 		return false;
7801b960e9SJaegeuk Kim 
7901b960e9SJaegeuk Kim 	return true;
8001b960e9SJaegeuk Kim }
8101b960e9SJaegeuk Kim 
f2fs_do_read_inline_data(struct page * page,struct page * ipage)824d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
83e18c65b2SHuajun Li {
84f2470371SChao Yu 	struct inode *inode = page->mapping->host;
85e18c65b2SHuajun Li 
86b3d208f9SJaegeuk Kim 	if (PageUptodate(page))
87b3d208f9SJaegeuk Kim 		return;
8804a17fb1SChao Yu 
89b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_P_SB(page), page->index);
90e18c65b2SHuajun Li 
91f2470371SChao Yu 	zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
92e18c65b2SHuajun Li 
93e18c65b2SHuajun Li 	/* Copy the whole inline data block */
94b87846bdSEric Biggers 	memcpy_to_page(page, 0, inline_data_addr(inode, ipage),
95b87846bdSEric Biggers 		       MAX_INLINE_DATA(inode));
96237c0790SJaegeuk Kim 	if (!PageUptodate(page))
97e18c65b2SHuajun Li 		SetPageUptodate(page);
98b3d208f9SJaegeuk Kim }
99e18c65b2SHuajun Li 
f2fs_truncate_inline_inode(struct inode * inode,struct page * ipage,u64 from)1004d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
1014d57b86dSChao Yu 					struct page *ipage, u64 from)
102feeb0debSChao Yu {
1030bfcfccaSChao Yu 	void *addr;
1040bfcfccaSChao Yu 
105f2470371SChao Yu 	if (from >= MAX_INLINE_DATA(inode))
106bd4667cbSKinglong Mee 		return;
1070bfcfccaSChao Yu 
108f2470371SChao Yu 	addr = inline_data_addr(inode, ipage);
1090bfcfccaSChao Yu 
110bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
111f2470371SChao Yu 	memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
112ee6d182fSJaegeuk Kim 	set_page_dirty(ipage);
113bd4667cbSKinglong Mee 
114bd4667cbSKinglong Mee 	if (from == 0)
115bd4667cbSKinglong Mee 		clear_inode_flag(inode, FI_DATA_EXIST);
116feeb0debSChao Yu }
117feeb0debSChao Yu 
f2fs_read_inline_data(struct inode * inode,struct page * page)118b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page)
119b3d208f9SJaegeuk Kim {
120b3d208f9SJaegeuk Kim 	struct page *ipage;
121b3d208f9SJaegeuk Kim 
1224d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
123b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
124b3d208f9SJaegeuk Kim 		unlock_page(page);
125b3d208f9SJaegeuk Kim 		return PTR_ERR(ipage);
126b3d208f9SJaegeuk Kim 	}
127b3d208f9SJaegeuk Kim 
128b3d208f9SJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
129b3d208f9SJaegeuk Kim 		f2fs_put_page(ipage, 1);
130b3d208f9SJaegeuk Kim 		return -EAGAIN;
131b3d208f9SJaegeuk Kim 	}
132b3d208f9SJaegeuk Kim 
133b3d208f9SJaegeuk Kim 	if (page->index)
13409cbfeafSKirill A. Shutemov 		zero_user_segment(page, 0, PAGE_SIZE);
135b3d208f9SJaegeuk Kim 	else
1364d57b86dSChao Yu 		f2fs_do_read_inline_data(page, ipage);
137b3d208f9SJaegeuk Kim 
138237c0790SJaegeuk Kim 	if (!PageUptodate(page))
139b3d208f9SJaegeuk Kim 		SetPageUptodate(page);
140b3d208f9SJaegeuk Kim 	f2fs_put_page(ipage, 1);
141b3d208f9SJaegeuk Kim 	unlock_page(page);
142e18c65b2SHuajun Li 	return 0;
143e18c65b2SHuajun Li }
144e18c65b2SHuajun Li 
f2fs_convert_inline_page(struct dnode_of_data * dn,struct page * page)145b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
146e18c65b2SHuajun Li {
147e18c65b2SHuajun Li 	struct f2fs_io_info fio = {
14805ca3632SJaegeuk Kim 		.sbi = F2FS_I_SB(dn->inode),
14939d787beSChao Yu 		.ino = dn->inode->i_ino,
150e18c65b2SHuajun Li 		.type = DATA,
15104d328deSMike Christie 		.op = REQ_OP_WRITE,
15270fd7614SChristoph Hellwig 		.op_flags = REQ_SYNC | REQ_PRIO,
15305ca3632SJaegeuk Kim 		.page = page,
1544375a336SJaegeuk Kim 		.encrypted_page = NULL,
155b0af6d49SChao Yu 		.io_type = FS_DATA_IO,
156e18c65b2SHuajun Li 	};
1577735730dSChao Yu 	struct node_info ni;
158158c194cSJaegeuk Kim 	int dirty, err;
159e18c65b2SHuajun Li 
160b3d208f9SJaegeuk Kim 	if (!f2fs_exist_data(dn->inode))
161b3d208f9SJaegeuk Kim 		goto clear_out;
162ec4e7af4SJaegeuk Kim 
163b3d208f9SJaegeuk Kim 	err = f2fs_reserve_block(dn, 0);
16415c6e3aaSJaegeuk Kim 	if (err)
165b3d208f9SJaegeuk Kim 		return err;
166e18c65b2SHuajun Li 
167a9419b63SJaegeuk Kim 	err = f2fs_get_node_info(fio.sbi, dn->nid, &ni, false);
1687735730dSChao Yu 	if (err) {
169e8c82c11SChao Yu 		f2fs_truncate_data_blocks_range(dn, 1);
1707735730dSChao Yu 		f2fs_put_dnode(dn);
1717735730dSChao Yu 		return err;
1727735730dSChao Yu 	}
1737735730dSChao Yu 
1747735730dSChao Yu 	fio.version = ni.version;
1757735730dSChao Yu 
1764dbe38dcSChao Yu 	if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
1774dbe38dcSChao Yu 		f2fs_put_dnode(dn);
1784dbe38dcSChao Yu 		set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
179dcbb4c10SJoe Perches 		f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
1804dbe38dcSChao Yu 			  __func__, dn->inode->i_ino, dn->data_blkaddr);
18195fa90c9SChao Yu 		f2fs_handle_error(fio.sbi, ERROR_INVALID_BLKADDR);
18210f966bbSChao Yu 		return -EFSCORRUPTED;
1834dbe38dcSChao Yu 	}
1844dbe38dcSChao Yu 
18585ead818SYunlei He 	f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
186b3d208f9SJaegeuk Kim 
1874d57b86dSChao Yu 	f2fs_do_read_inline_data(page, dn->inode_page);
1886282adbfSJaegeuk Kim 	set_page_dirty(page);
1896282adbfSJaegeuk Kim 
190158c194cSJaegeuk Kim 	/* clear dirty state */
191158c194cSJaegeuk Kim 	dirty = clear_page_dirty_for_io(page);
192158c194cSJaegeuk Kim 
193e18c65b2SHuajun Li 	/* write data page to try to make data consistent */
194e18c65b2SHuajun Li 	set_page_writeback(page);
1957a9d7548SChao Yu 	fio.old_blkaddr = dn->data_blkaddr;
196ef095d19SJaegeuk Kim 	set_inode_flag(dn->inode, FI_HOT_DATA);
1974d57b86dSChao Yu 	f2fs_outplace_write_data(dn, &fio);
198bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, DATA, true, true);
199933439c8SChao Yu 	if (dirty) {
200158c194cSJaegeuk Kim 		inode_dec_dirty_pages(dn->inode);
2014d57b86dSChao Yu 		f2fs_remove_dirty_inode(dn->inode);
202933439c8SChao Yu 	}
203e18c65b2SHuajun Li 
20495f5b0fcSJaegeuk Kim 	/* this converted inline_data should be recovered. */
20591942321SJaegeuk Kim 	set_inode_flag(dn->inode, FI_APPEND_WRITE);
20695f5b0fcSJaegeuk Kim 
207e18c65b2SHuajun Li 	/* clear inline data and flag after data writeback */
2084d57b86dSChao Yu 	f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
209b763f3beSChao Yu 	clear_page_private_inline(dn->inode_page);
210b3d208f9SJaegeuk Kim clear_out:
211b3d208f9SJaegeuk Kim 	stat_dec_inline_inode(dn->inode);
212bd4667cbSKinglong Mee 	clear_inode_flag(dn->inode, FI_INLINE_DATA);
213b3d208f9SJaegeuk Kim 	f2fs_put_dnode(dn);
214b3d208f9SJaegeuk Kim 	return 0;
215e18c65b2SHuajun Li }
216e18c65b2SHuajun Li 
f2fs_convert_inline_inode(struct inode * inode)217b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode)
218e18c65b2SHuajun Li {
219b3d208f9SJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
220b3d208f9SJaegeuk Kim 	struct dnode_of_data dn;
221b3d208f9SJaegeuk Kim 	struct page *ipage, *page;
222b3d208f9SJaegeuk Kim 	int err = 0;
223e18c65b2SHuajun Li 
224077f0e24SChao Yu 	if (f2fs_hw_is_readonly(sbi) || f2fs_readonly(sbi->sb))
225077f0e24SChao Yu 		return -EROFS;
226077f0e24SChao Yu 
227077f0e24SChao Yu 	if (!f2fs_has_inline_data(inode))
228b9d777b8SJaegeuk Kim 		return 0;
229b9d777b8SJaegeuk Kim 
23010a26878SChao Yu 	err = f2fs_dquot_initialize(inode);
23125fb04dbSYi Chen 	if (err)
23225fb04dbSYi Chen 		return err;
23325fb04dbSYi Chen 
234300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
235b3d208f9SJaegeuk Kim 	if (!page)
2369e09fc85SJaegeuk Kim 		return -ENOMEM;
237b3d208f9SJaegeuk Kim 
238b3d208f9SJaegeuk Kim 	f2fs_lock_op(sbi);
239b3d208f9SJaegeuk Kim 
2404d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
241b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
2426d20aff8SJaegeuk Kim 		err = PTR_ERR(ipage);
2436d20aff8SJaegeuk Kim 		goto out;
244b067ba1fSJaegeuk Kim 	}
245e18c65b2SHuajun Li 
246b3d208f9SJaegeuk Kim 	set_new_dnode(&dn, inode, ipage, ipage, 0);
247b3d208f9SJaegeuk Kim 
248b3d208f9SJaegeuk Kim 	if (f2fs_has_inline_data(inode))
249b3d208f9SJaegeuk Kim 		err = f2fs_convert_inline_page(&dn, page);
250b3d208f9SJaegeuk Kim 
251b3d208f9SJaegeuk Kim 	f2fs_put_dnode(&dn);
2526d20aff8SJaegeuk Kim out:
253b3d208f9SJaegeuk Kim 	f2fs_unlock_op(sbi);
254b3d208f9SJaegeuk Kim 
255b3d208f9SJaegeuk Kim 	f2fs_put_page(page, 1);
2562a340760SJaegeuk Kim 
25728e18ee6SColin Ian King 	if (!err)
2582c4db1a6SJaegeuk Kim 		f2fs_balance_fs(sbi, dn.node_changed);
2592a340760SJaegeuk Kim 
260e18c65b2SHuajun Li 	return err;
261e18c65b2SHuajun Li }
262e18c65b2SHuajun Li 
f2fs_write_inline_data(struct inode * inode,struct page * page)263b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page)
264e18c65b2SHuajun Li {
265e18c65b2SHuajun Li 	struct dnode_of_data dn;
266e18c65b2SHuajun Li 	int err;
267e18c65b2SHuajun Li 
268e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, NULL, NULL, 0);
2694d57b86dSChao Yu 	err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
270e18c65b2SHuajun Li 	if (err)
271e18c65b2SHuajun Li 		return err;
272e18c65b2SHuajun Li 
273c08a690bSJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
274b3d208f9SJaegeuk Kim 		f2fs_put_dnode(&dn);
275b3d208f9SJaegeuk Kim 		return -EAGAIN;
276c08a690bSJaegeuk Kim 	}
277c08a690bSJaegeuk Kim 
278b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_I_SB(inode), page->index);
279b3d208f9SJaegeuk Kim 
280bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
281b87846bdSEric Biggers 	memcpy_from_page(inline_data_addr(inode, dn.inode_page),
282b87846bdSEric Biggers 			 page, 0, MAX_INLINE_DATA(inode));
283ee6d182fSJaegeuk Kim 	set_page_dirty(dn.inode_page);
284e18c65b2SHuajun Li 
2855ec2d99dSMatthew Wilcox 	f2fs_clear_page_cache_dirty_tag(page);
2860abd8e70SDaeho Jeong 
28791942321SJaegeuk Kim 	set_inode_flag(inode, FI_APPEND_WRITE);
28891942321SJaegeuk Kim 	set_inode_flag(inode, FI_DATA_EXIST);
289b3d208f9SJaegeuk Kim 
290b763f3beSChao Yu 	clear_page_private_inline(dn.inode_page);
291e18c65b2SHuajun Li 	f2fs_put_dnode(&dn);
292e18c65b2SHuajun Li 	return 0;
293e18c65b2SHuajun Li }
2941e1bb4baSJaegeuk Kim 
f2fs_recover_inline_data(struct inode * inode,struct page * npage)2959627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage)
2961e1bb4baSJaegeuk Kim {
2974081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2981e1bb4baSJaegeuk Kim 	struct f2fs_inode *ri = NULL;
2991e1bb4baSJaegeuk Kim 	void *src_addr, *dst_addr;
3001e1bb4baSJaegeuk Kim 	struct page *ipage;
3011e1bb4baSJaegeuk Kim 
3021e1bb4baSJaegeuk Kim 	/*
3031e1bb4baSJaegeuk Kim 	 * The inline_data recovery policy is as follows.
3041e1bb4baSJaegeuk Kim 	 * [prev.] [next] of inline_data flag
3051e1bb4baSJaegeuk Kim 	 *    o       o  -> recover inline_data
3061e1bb4baSJaegeuk Kim 	 *    o       x  -> remove inline_data, and then recover data blocks
3076e5ca4fcSJack Qiu 	 *    x       o  -> remove data blocks, and then recover inline_data
3081e1bb4baSJaegeuk Kim 	 *    x       x  -> recover data blocks
3091e1bb4baSJaegeuk Kim 	 */
3101e1bb4baSJaegeuk Kim 	if (IS_INODE(npage))
3111e1bb4baSJaegeuk Kim 		ri = F2FS_INODE(npage);
3121e1bb4baSJaegeuk Kim 
3131e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode) &&
3140342fd30SJaegeuk Kim 			ri && (ri->i_inline & F2FS_INLINE_DATA)) {
3151e1bb4baSJaegeuk Kim process_inline:
3164d57b86dSChao Yu 		ipage = f2fs_get_node_page(sbi, inode->i_ino);
3179627a7b3SChao Yu 		if (IS_ERR(ipage))
3189627a7b3SChao Yu 			return PTR_ERR(ipage);
3191e1bb4baSJaegeuk Kim 
320bae0ee7aSChao Yu 		f2fs_wait_on_page_writeback(ipage, NODE, true, true);
32154b591dfSJaegeuk Kim 
322f2470371SChao Yu 		src_addr = inline_data_addr(inode, npage);
323f2470371SChao Yu 		dst_addr = inline_data_addr(inode, ipage);
324f2470371SChao Yu 		memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
325b3d208f9SJaegeuk Kim 
32691942321SJaegeuk Kim 		set_inode_flag(inode, FI_INLINE_DATA);
32791942321SJaegeuk Kim 		set_inode_flag(inode, FI_DATA_EXIST);
328b3d208f9SJaegeuk Kim 
329ee6d182fSJaegeuk Kim 		set_page_dirty(ipage);
3301e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
3319627a7b3SChao Yu 		return 1;
3321e1bb4baSJaegeuk Kim 	}
3331e1bb4baSJaegeuk Kim 
3341e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode)) {
3354d57b86dSChao Yu 		ipage = f2fs_get_node_page(sbi, inode->i_ino);
3369627a7b3SChao Yu 		if (IS_ERR(ipage))
3379627a7b3SChao Yu 			return PTR_ERR(ipage);
3384d57b86dSChao Yu 		f2fs_truncate_inline_inode(inode, ipage, 0);
33984921561SJack Qiu 		stat_dec_inline_inode(inode);
340bd4667cbSKinglong Mee 		clear_inode_flag(inode, FI_INLINE_DATA);
3411e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
3420342fd30SJaegeuk Kim 	} else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
3439627a7b3SChao Yu 		int ret;
3449627a7b3SChao Yu 
3459627a7b3SChao Yu 		ret = f2fs_truncate_blocks(inode, 0, false);
3469627a7b3SChao Yu 		if (ret)
3479627a7b3SChao Yu 			return ret;
34884921561SJack Qiu 		stat_inc_inline_inode(inode);
3491e1bb4baSJaegeuk Kim 		goto process_inline;
3501e1bb4baSJaegeuk Kim 	}
3519627a7b3SChao Yu 	return 0;
3521e1bb4baSJaegeuk Kim }
353201a05beSChao Yu 
f2fs_find_in_inline_dir(struct inode * dir,const struct f2fs_filename * fname,struct page ** res_page)3544d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
35543c780baSEric Biggers 					const struct f2fs_filename *fname,
35643c780baSEric Biggers 					struct page **res_page)
357201a05beSChao Yu {
358201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
359201a05beSChao Yu 	struct f2fs_dir_entry *de;
3607b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
3614e6ebf6dSJaegeuk Kim 	struct page *ipage;
362f2470371SChao Yu 	void *inline_dentry;
363201a05beSChao Yu 
3644d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
36542d96401SJaegeuk Kim 	if (IS_ERR(ipage)) {
36642d96401SJaegeuk Kim 		*res_page = ipage;
367201a05beSChao Yu 		return NULL;
36842d96401SJaegeuk Kim 	}
369201a05beSChao Yu 
370f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
371201a05beSChao Yu 
372f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
37343c780baSEric Biggers 	de = f2fs_find_target_dentry(&d, fname, NULL);
3744e6ebf6dSJaegeuk Kim 	unlock_page(ipage);
3757ad08a58SDaniel Rosenberg 	if (IS_ERR(de)) {
3767ad08a58SDaniel Rosenberg 		*res_page = ERR_CAST(de);
3777ad08a58SDaniel Rosenberg 		de = NULL;
3787ad08a58SDaniel Rosenberg 	}
3794e6ebf6dSJaegeuk Kim 	if (de)
380201a05beSChao Yu 		*res_page = ipage;
3814e6ebf6dSJaegeuk Kim 	else
3824e6ebf6dSJaegeuk Kim 		f2fs_put_page(ipage, 0);
383201a05beSChao Yu 
384201a05beSChao Yu 	return de;
385201a05beSChao Yu }
386201a05beSChao Yu 
f2fs_make_empty_inline_dir(struct inode * inode,struct inode * parent,struct page * ipage)3874d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
388201a05beSChao Yu 							struct page *ipage)
389201a05beSChao Yu {
390062a3e7bSJaegeuk Kim 	struct f2fs_dentry_ptr d;
391f2470371SChao Yu 	void *inline_dentry;
392201a05beSChao Yu 
393f2470371SChao Yu 	inline_dentry = inline_data_addr(inode, ipage);
394201a05beSChao Yu 
395f2470371SChao Yu 	make_dentry_ptr_inline(inode, &d, inline_dentry);
3964d57b86dSChao Yu 	f2fs_do_make_empty_dir(inode, parent, &d);
397201a05beSChao Yu 
398201a05beSChao Yu 	set_page_dirty(ipage);
399201a05beSChao Yu 
400201a05beSChao Yu 	/* update i_size to MAX_INLINE_DATA */
401f2470371SChao Yu 	if (i_size_read(inode) < MAX_INLINE_DATA(inode))
402f2470371SChao Yu 		f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
403201a05beSChao Yu 	return 0;
404201a05beSChao Yu }
405201a05beSChao Yu 
406470f00e9SChao Yu /*
407470f00e9SChao Yu  * NOTE: ipage is grabbed by caller, but if any error occurs, we should
408470f00e9SChao Yu  * release ipage in this function.
409470f00e9SChao Yu  */
f2fs_move_inline_dirents(struct inode * dir,struct page * ipage,void * inline_dentry)410675f10bdSChao Yu static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
411f2470371SChao Yu 							void *inline_dentry)
412201a05beSChao Yu {
413201a05beSChao Yu 	struct page *page;
414201a05beSChao Yu 	struct dnode_of_data dn;
415201a05beSChao Yu 	struct f2fs_dentry_block *dentry_blk;
41676a9dd85SChao Yu 	struct f2fs_dentry_ptr src, dst;
417201a05beSChao Yu 	int err;
418201a05beSChao Yu 
419dd973007SJaegeuk Kim 	page = f2fs_grab_cache_page(dir->i_mapping, 0, true);
420470f00e9SChao Yu 	if (!page) {
421470f00e9SChao Yu 		f2fs_put_page(ipage, 1);
422201a05beSChao Yu 		return -ENOMEM;
423470f00e9SChao Yu 	}
424201a05beSChao Yu 
425201a05beSChao Yu 	set_new_dnode(&dn, dir, ipage, NULL, 0);
426201a05beSChao Yu 	err = f2fs_reserve_block(&dn, 0);
427201a05beSChao Yu 	if (err)
428201a05beSChao Yu 		goto out;
429201a05beSChao Yu 
4304dbe38dcSChao Yu 	if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
4314dbe38dcSChao Yu 		f2fs_put_dnode(&dn);
4324dbe38dcSChao Yu 		set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
433dcbb4c10SJoe Perches 		f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
4344dbe38dcSChao Yu 			  __func__, dir->i_ino, dn.data_blkaddr);
43595fa90c9SChao Yu 		f2fs_handle_error(F2FS_P_SB(page), ERROR_INVALID_BLKADDR);
43610f966bbSChao Yu 		err = -EFSCORRUPTED;
4374dbe38dcSChao Yu 		goto out;
4384dbe38dcSChao Yu 	}
4394dbe38dcSChao Yu 
440bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, DATA, true, true);
441201a05beSChao Yu 
442bdbc90faSYunlong Song 	dentry_blk = page_address(page);
443201a05beSChao Yu 
4449a5571cfSEric Biggers 	/*
4459a5571cfSEric Biggers 	 * Start by zeroing the full block, to ensure that all unused space is
4469a5571cfSEric Biggers 	 * zeroed and no uninitialized memory is leaked to disk.
4479a5571cfSEric Biggers 	 */
4489a5571cfSEric Biggers 	memset(dentry_blk, 0, F2FS_BLKSIZE);
4499a5571cfSEric Biggers 
450f2470371SChao Yu 	make_dentry_ptr_inline(dir, &src, inline_dentry);
451f2470371SChao Yu 	make_dentry_ptr_block(dir, &dst, dentry_blk);
45276a9dd85SChao Yu 
453201a05beSChao Yu 	/* copy data from inline dentry block to new dentry block */
45476a9dd85SChao Yu 	memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
45576a9dd85SChao Yu 	memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
45676a9dd85SChao Yu 	memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
457201a05beSChao Yu 
458237c0790SJaegeuk Kim 	if (!PageUptodate(page))
459201a05beSChao Yu 		SetPageUptodate(page);
460201a05beSChao Yu 	set_page_dirty(page);
461201a05beSChao Yu 
462201a05beSChao Yu 	/* clear inline dir and flag after data writeback */
4634d57b86dSChao Yu 	f2fs_truncate_inline_inode(dir, ipage, 0);
464b3d208f9SJaegeuk Kim 
4653289c061SJaegeuk Kim 	stat_dec_inline_dir(dir);
46691942321SJaegeuk Kim 	clear_inode_flag(dir, FI_INLINE_DENTRY);
467201a05beSChao Yu 
46845a74688SChao Yu 	/*
46945a74688SChao Yu 	 * should retrieve reserved space which was used to keep
47045a74688SChao Yu 	 * inline_dentry's structure for backward compatibility.
47145a74688SChao Yu 	 */
47245a74688SChao Yu 	if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
47345a74688SChao Yu 			!f2fs_has_inline_xattr(dir))
47445a74688SChao Yu 		F2FS_I(dir)->i_inline_xattr_size = 0;
47545a74688SChao Yu 
476205b9822SJaegeuk Kim 	f2fs_i_depth_write(dir, 1);
477ee6d182fSJaegeuk Kim 	if (i_size_read(dir) < PAGE_SIZE)
478fc9581c8SJaegeuk Kim 		f2fs_i_size_write(dir, PAGE_SIZE);
479201a05beSChao Yu out:
480201a05beSChao Yu 	f2fs_put_page(page, 1);
481201a05beSChao Yu 	return err;
482201a05beSChao Yu }
483201a05beSChao Yu 
f2fs_add_inline_entries(struct inode * dir,void * inline_dentry)484f2470371SChao Yu static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
485675f10bdSChao Yu {
486675f10bdSChao Yu 	struct f2fs_dentry_ptr d;
487675f10bdSChao Yu 	unsigned long bit_pos = 0;
488675f10bdSChao Yu 	int err = 0;
489675f10bdSChao Yu 
490f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
491675f10bdSChao Yu 
492675f10bdSChao Yu 	while (bit_pos < d.max) {
493675f10bdSChao Yu 		struct f2fs_dir_entry *de;
49443c780baSEric Biggers 		struct f2fs_filename fname;
495675f10bdSChao Yu 		nid_t ino;
496675f10bdSChao Yu 		umode_t fake_mode;
497675f10bdSChao Yu 
498675f10bdSChao Yu 		if (!test_bit_le(bit_pos, d.bitmap)) {
499675f10bdSChao Yu 			bit_pos++;
500675f10bdSChao Yu 			continue;
501675f10bdSChao Yu 		}
502675f10bdSChao Yu 
503675f10bdSChao Yu 		de = &d.dentry[bit_pos];
504a4a13f58SChao Yu 
505a4a13f58SChao Yu 		if (unlikely(!de->name_len)) {
506a4a13f58SChao Yu 			bit_pos++;
507a4a13f58SChao Yu 			continue;
508a4a13f58SChao Yu 		}
509a4a13f58SChao Yu 
51043c780baSEric Biggers 		/*
51143c780baSEric Biggers 		 * We only need the disk_name and hash to move the dentry.
51243c780baSEric Biggers 		 * We don't need the original or casefolded filenames.
51343c780baSEric Biggers 		 */
51443c780baSEric Biggers 		memset(&fname, 0, sizeof(fname));
51543c780baSEric Biggers 		fname.disk_name.name = d.filename[bit_pos];
51643c780baSEric Biggers 		fname.disk_name.len = le16_to_cpu(de->name_len);
51743c780baSEric Biggers 		fname.hash = de->hash_code;
518675f10bdSChao Yu 
519675f10bdSChao Yu 		ino = le32_to_cpu(de->ino);
5200c9f4521SWeizhao Ouyang 		fake_mode = fs_ftype_to_dtype(de->file_type) << S_DT_SHIFT;
521675f10bdSChao Yu 
52243c780baSEric Biggers 		err = f2fs_add_regular_entry(dir, &fname, NULL, ino, fake_mode);
523675f10bdSChao Yu 		if (err)
524675f10bdSChao Yu 			goto punch_dentry_pages;
525675f10bdSChao Yu 
526675f10bdSChao Yu 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
527675f10bdSChao Yu 	}
528675f10bdSChao Yu 	return 0;
529675f10bdSChao Yu punch_dentry_pages:
530675f10bdSChao Yu 	truncate_inode_pages(&dir->i_data, 0);
531c42d28ceSChao Yu 	f2fs_truncate_blocks(dir, 0, false);
5324d57b86dSChao Yu 	f2fs_remove_dirty_inode(dir);
533675f10bdSChao Yu 	return err;
534675f10bdSChao Yu }
535675f10bdSChao Yu 
f2fs_move_rehashed_dirents(struct inode * dir,struct page * ipage,void * inline_dentry)536675f10bdSChao Yu static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
537f2470371SChao Yu 							void *inline_dentry)
538675f10bdSChao Yu {
539f2470371SChao Yu 	void *backup_dentry;
540675f10bdSChao Yu 	int err;
541675f10bdSChao Yu 
5421ecc0c5cSChao Yu 	backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
543f2470371SChao Yu 				MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
5448975bdf4SChao Yu 	if (!backup_dentry) {
5458975bdf4SChao Yu 		f2fs_put_page(ipage, 1);
546675f10bdSChao Yu 		return -ENOMEM;
5478975bdf4SChao Yu 	}
548675f10bdSChao Yu 
549f2470371SChao Yu 	memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
5504d57b86dSChao Yu 	f2fs_truncate_inline_inode(dir, ipage, 0);
551675f10bdSChao Yu 
552675f10bdSChao Yu 	unlock_page(ipage);
553675f10bdSChao Yu 
554675f10bdSChao Yu 	err = f2fs_add_inline_entries(dir, backup_dentry);
555675f10bdSChao Yu 	if (err)
556675f10bdSChao Yu 		goto recover;
557675f10bdSChao Yu 
558675f10bdSChao Yu 	lock_page(ipage);
559675f10bdSChao Yu 
560675f10bdSChao Yu 	stat_dec_inline_dir(dir);
56191942321SJaegeuk Kim 	clear_inode_flag(dir, FI_INLINE_DENTRY);
56245a74688SChao Yu 
56345a74688SChao Yu 	/*
56445a74688SChao Yu 	 * should retrieve reserved space which was used to keep
56545a74688SChao Yu 	 * inline_dentry's structure for backward compatibility.
56645a74688SChao Yu 	 */
56745a74688SChao Yu 	if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
56845a74688SChao Yu 			!f2fs_has_inline_xattr(dir))
56945a74688SChao Yu 		F2FS_I(dir)->i_inline_xattr_size = 0;
57045a74688SChao Yu 
571c8eb7024SChao Yu 	kfree(backup_dentry);
572675f10bdSChao Yu 	return 0;
573675f10bdSChao Yu recover:
574675f10bdSChao Yu 	lock_page(ipage);
575bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
576f2470371SChao Yu 	memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
577205b9822SJaegeuk Kim 	f2fs_i_depth_write(dir, 0);
578f2470371SChao Yu 	f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
579ee6d182fSJaegeuk Kim 	set_page_dirty(ipage);
580675f10bdSChao Yu 	f2fs_put_page(ipage, 1);
581675f10bdSChao Yu 
582c8eb7024SChao Yu 	kfree(backup_dentry);
583675f10bdSChao Yu 	return err;
584675f10bdSChao Yu }
585675f10bdSChao Yu 
do_convert_inline_dir(struct inode * dir,struct page * ipage,void * inline_dentry)586b06af2afSJaegeuk Kim static int do_convert_inline_dir(struct inode *dir, struct page *ipage,
587f2470371SChao Yu 							void *inline_dentry)
588675f10bdSChao Yu {
589675f10bdSChao Yu 	if (!F2FS_I(dir)->i_dir_level)
590675f10bdSChao Yu 		return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
591675f10bdSChao Yu 	else
592675f10bdSChao Yu 		return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
593675f10bdSChao Yu }
594675f10bdSChao Yu 
f2fs_try_convert_inline_dir(struct inode * dir,struct dentry * dentry)595b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry)
596b06af2afSJaegeuk Kim {
597b06af2afSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
598b06af2afSJaegeuk Kim 	struct page *ipage;
59943c780baSEric Biggers 	struct f2fs_filename fname;
600b06af2afSJaegeuk Kim 	void *inline_dentry = NULL;
601b06af2afSJaegeuk Kim 	int err = 0;
602b06af2afSJaegeuk Kim 
603b06af2afSJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
604b06af2afSJaegeuk Kim 		return 0;
605b06af2afSJaegeuk Kim 
606b06af2afSJaegeuk Kim 	f2fs_lock_op(sbi);
607b06af2afSJaegeuk Kim 
60843c780baSEric Biggers 	err = f2fs_setup_filename(dir, &dentry->d_name, 0, &fname);
609b06af2afSJaegeuk Kim 	if (err)
610b06af2afSJaegeuk Kim 		goto out;
611b06af2afSJaegeuk Kim 
612b06af2afSJaegeuk Kim 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
613b06af2afSJaegeuk Kim 	if (IS_ERR(ipage)) {
614b06af2afSJaegeuk Kim 		err = PTR_ERR(ipage);
615ff5f85c8SEric Biggers 		goto out_fname;
616b06af2afSJaegeuk Kim 	}
617b06af2afSJaegeuk Kim 
618b06af2afSJaegeuk Kim 	if (f2fs_has_enough_room(dir, ipage, &fname)) {
619b06af2afSJaegeuk Kim 		f2fs_put_page(ipage, 1);
620ff5f85c8SEric Biggers 		goto out_fname;
621b06af2afSJaegeuk Kim 	}
622b06af2afSJaegeuk Kim 
623b06af2afSJaegeuk Kim 	inline_dentry = inline_data_addr(dir, ipage);
624b06af2afSJaegeuk Kim 
625b06af2afSJaegeuk Kim 	err = do_convert_inline_dir(dir, ipage, inline_dentry);
626b06af2afSJaegeuk Kim 	if (!err)
627b06af2afSJaegeuk Kim 		f2fs_put_page(ipage, 1);
628ff5f85c8SEric Biggers out_fname:
62943c780baSEric Biggers 	f2fs_free_filename(&fname);
630b06af2afSJaegeuk Kim out:
631b06af2afSJaegeuk Kim 	f2fs_unlock_op(sbi);
632b06af2afSJaegeuk Kim 	return err;
633b06af2afSJaegeuk Kim }
634b06af2afSJaegeuk Kim 
f2fs_add_inline_entry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)63543c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
636510022a8SJaegeuk Kim 			  struct inode *inode, nid_t ino, umode_t mode)
637201a05beSChao Yu {
638201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
639201a05beSChao Yu 	struct page *ipage;
640201a05beSChao Yu 	unsigned int bit_pos;
641f2470371SChao Yu 	void *inline_dentry = NULL;
6423b4d732aSChao Yu 	struct f2fs_dentry_ptr d;
64343c780baSEric Biggers 	int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
644510022a8SJaegeuk Kim 	struct page *page = NULL;
645201a05beSChao Yu 	int err = 0;
646201a05beSChao Yu 
6474d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
648201a05beSChao Yu 	if (IS_ERR(ipage))
649201a05beSChao Yu 		return PTR_ERR(ipage);
650201a05beSChao Yu 
651f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
652f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
65376a9dd85SChao Yu 
6544d57b86dSChao Yu 	bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
65576a9dd85SChao Yu 	if (bit_pos >= d.max) {
656b06af2afSJaegeuk Kim 		err = do_convert_inline_dir(dir, ipage, inline_dentry);
657470f00e9SChao Yu 		if (err)
658470f00e9SChao Yu 			return err;
659201a05beSChao Yu 		err = -EAGAIN;
660201a05beSChao Yu 		goto out;
661201a05beSChao Yu 	}
662201a05beSChao Yu 
663510022a8SJaegeuk Kim 	if (inode) {
6645c13e238SJaegeuk Kim 		f2fs_down_write_nested(&F2FS_I(inode)->i_sem,
6655c13e238SJaegeuk Kim 						SINGLE_DEPTH_NESTING);
66643c780baSEric Biggers 		page = f2fs_init_inode_metadata(inode, dir, fname, ipage);
667201a05beSChao Yu 		if (IS_ERR(page)) {
668201a05beSChao Yu 			err = PTR_ERR(page);
669201a05beSChao Yu 			goto fail;
670201a05beSChao Yu 		}
671510022a8SJaegeuk Kim 	}
672bce8d112SJaegeuk Kim 
673bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
6743b4d732aSChao Yu 
67543c780baSEric Biggers 	f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
67643c780baSEric Biggers 			   bit_pos);
6773b4d732aSChao Yu 
678201a05beSChao Yu 	set_page_dirty(ipage);
679201a05beSChao Yu 
680201a05beSChao Yu 	/* we don't need to mark_inode_dirty now */
681510022a8SJaegeuk Kim 	if (inode) {
682205b9822SJaegeuk Kim 		f2fs_i_pino_write(inode, dir->i_ino);
68398194030SChao Yu 
68498194030SChao Yu 		/* synchronize inode page's data from inode cache */
68598194030SChao Yu 		if (is_inode_flag_set(inode, FI_NEW_INODE))
68698194030SChao Yu 			f2fs_update_inode(inode, page);
68798194030SChao Yu 
688201a05beSChao Yu 		f2fs_put_page(page, 1);
689510022a8SJaegeuk Kim 	}
690201a05beSChao Yu 
6914d57b86dSChao Yu 	f2fs_update_parent_metadata(dir, inode, 0);
692201a05beSChao Yu fail:
693510022a8SJaegeuk Kim 	if (inode)
694e4544b63STim Murray 		f2fs_up_write(&F2FS_I(inode)->i_sem);
695201a05beSChao Yu out:
696201a05beSChao Yu 	f2fs_put_page(ipage, 1);
697201a05beSChao Yu 	return err;
698201a05beSChao Yu }
699201a05beSChao Yu 
f2fs_delete_inline_entry(struct f2fs_dir_entry * dentry,struct page * page,struct inode * dir,struct inode * inode)700201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
701201a05beSChao Yu 					struct inode *dir, struct inode *inode)
702201a05beSChao Yu {
70376a9dd85SChao Yu 	struct f2fs_dentry_ptr d;
704f2470371SChao Yu 	void *inline_dentry;
705201a05beSChao Yu 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
706201a05beSChao Yu 	unsigned int bit_pos;
707201a05beSChao Yu 	int i;
708201a05beSChao Yu 
709201a05beSChao Yu 	lock_page(page);
710bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, NODE, true, true);
711201a05beSChao Yu 
712f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, page);
713f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
71476a9dd85SChao Yu 
71576a9dd85SChao Yu 	bit_pos = dentry - d.dentry;
716201a05beSChao Yu 	for (i = 0; i < slots; i++)
71776a9dd85SChao Yu 		__clear_bit_le(bit_pos + i, d.bitmap);
718201a05beSChao Yu 
719201a05beSChao Yu 	set_page_dirty(page);
7209f7c45ccSJaegeuk Kim 	f2fs_put_page(page, 1);
721201a05beSChao Yu 
722c62ebd35SJeff Layton 	dir->i_mtime = inode_set_ctime_current(dir);
7237c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(dir, false);
724201a05beSChao Yu 
725201a05beSChao Yu 	if (inode)
7269f7c45ccSJaegeuk Kim 		f2fs_drop_nlink(dir, inode);
727201a05beSChao Yu }
728201a05beSChao Yu 
f2fs_empty_inline_dir(struct inode * dir)729201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir)
730201a05beSChao Yu {
731201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
732201a05beSChao Yu 	struct page *ipage;
733201a05beSChao Yu 	unsigned int bit_pos = 2;
734f2470371SChao Yu 	void *inline_dentry;
73576a9dd85SChao Yu 	struct f2fs_dentry_ptr d;
736201a05beSChao Yu 
7374d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
738201a05beSChao Yu 	if (IS_ERR(ipage))
739201a05beSChao Yu 		return false;
740201a05beSChao Yu 
741f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
742f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
74376a9dd85SChao Yu 
74476a9dd85SChao Yu 	bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
745201a05beSChao Yu 
746201a05beSChao Yu 	f2fs_put_page(ipage, 1);
747201a05beSChao Yu 
74876a9dd85SChao Yu 	if (bit_pos < d.max)
749201a05beSChao Yu 		return false;
750201a05beSChao Yu 
751201a05beSChao Yu 	return true;
752201a05beSChao Yu }
753201a05beSChao Yu 
f2fs_read_inline_dir(struct file * file,struct dir_context * ctx,struct fscrypt_str * fstr)754d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
7550b81d077SJaegeuk Kim 				struct fscrypt_str *fstr)
756201a05beSChao Yu {
757201a05beSChao Yu 	struct inode *inode = file_inode(file);
758201a05beSChao Yu 	struct page *ipage = NULL;
7597b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
760f2470371SChao Yu 	void *inline_dentry = NULL;
761ed6bd4b1SChao Yu 	int err;
762201a05beSChao Yu 
76376a9dd85SChao Yu 	make_dentry_ptr_inline(inode, &d, inline_dentry);
76476a9dd85SChao Yu 
76576a9dd85SChao Yu 	if (ctx->pos == d.max)
766201a05beSChao Yu 		return 0;
767201a05beSChao Yu 
7684d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
769201a05beSChao Yu 	if (IS_ERR(ipage))
770201a05beSChao Yu 		return PTR_ERR(ipage);
771201a05beSChao Yu 
772aadcef64SChao Yu 	/*
773aadcef64SChao Yu 	 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
774aadcef64SChao Yu 	 * ipage without page's lock held.
775aadcef64SChao Yu 	 */
776aadcef64SChao Yu 	unlock_page(ipage);
777aadcef64SChao Yu 
778f2470371SChao Yu 	inline_dentry = inline_data_addr(inode, ipage);
779201a05beSChao Yu 
78064c24ecbSTomohiro Kusumi 	make_dentry_ptr_inline(inode, &d, inline_dentry);
7817b3cd7d6SJaegeuk Kim 
782ed6bd4b1SChao Yu 	err = f2fs_fill_dentries(ctx, &d, 0, fstr);
783ed6bd4b1SChao Yu 	if (!err)
78476a9dd85SChao Yu 		ctx->pos = d.max;
785201a05beSChao Yu 
786aadcef64SChao Yu 	f2fs_put_page(ipage, 0);
787ed6bd4b1SChao Yu 	return err < 0 ? err : 0;
788201a05beSChao Yu }
78967f8cf3cSJaegeuk Kim 
f2fs_inline_data_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,__u64 start,__u64 len)79067f8cf3cSJaegeuk Kim int f2fs_inline_data_fiemap(struct inode *inode,
79167f8cf3cSJaegeuk Kim 		struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
79267f8cf3cSJaegeuk Kim {
79367f8cf3cSJaegeuk Kim 	__u64 byteaddr, ilen;
79467f8cf3cSJaegeuk Kim 	__u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
79567f8cf3cSJaegeuk Kim 		FIEMAP_EXTENT_LAST;
79667f8cf3cSJaegeuk Kim 	struct node_info ni;
79767f8cf3cSJaegeuk Kim 	struct page *ipage;
79867f8cf3cSJaegeuk Kim 	int err = 0;
79967f8cf3cSJaegeuk Kim 
8004d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
80167f8cf3cSJaegeuk Kim 	if (IS_ERR(ipage))
80267f8cf3cSJaegeuk Kim 		return PTR_ERR(ipage);
80367f8cf3cSJaegeuk Kim 
8047975f349SChao Yu 	if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
8057975f349SChao Yu 				!f2fs_has_inline_data(inode)) {
8067975f349SChao Yu 		err = -EAGAIN;
8077975f349SChao Yu 		goto out;
8087975f349SChao Yu 	}
8097975f349SChao Yu 
8107975f349SChao Yu 	if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
81167f8cf3cSJaegeuk Kim 		err = -EAGAIN;
81267f8cf3cSJaegeuk Kim 		goto out;
81367f8cf3cSJaegeuk Kim 	}
81467f8cf3cSJaegeuk Kim 
815f2470371SChao Yu 	ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
81667f8cf3cSJaegeuk Kim 	if (start >= ilen)
81767f8cf3cSJaegeuk Kim 		goto out;
81867f8cf3cSJaegeuk Kim 	if (start + len < ilen)
81967f8cf3cSJaegeuk Kim 		ilen = start + len;
82067f8cf3cSJaegeuk Kim 	ilen -= start;
82167f8cf3cSJaegeuk Kim 
822a9419b63SJaegeuk Kim 	err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni, false);
8237735730dSChao Yu 	if (err)
8247735730dSChao Yu 		goto out;
8257735730dSChao Yu 
82667f8cf3cSJaegeuk Kim 	byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
827f2470371SChao Yu 	byteaddr += (char *)inline_data_addr(inode, ipage) -
828f2470371SChao Yu 					(char *)F2FS_INODE(ipage);
82967f8cf3cSJaegeuk Kim 	err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
830dd5a09bdSChao Yu 	trace_f2fs_fiemap(inode, start, byteaddr, ilen, flags, err);
83167f8cf3cSJaegeuk Kim out:
83267f8cf3cSJaegeuk Kim 	f2fs_put_page(ipage, 1);
83367f8cf3cSJaegeuk Kim 	return err;
83467f8cf3cSJaegeuk Kim }
835