xref: /openbmc/linux/fs/f2fs/inline.c (revision 677a82b44ebf263d4f9a0cfbd576a6ade797a07b)
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 
17*677a82b4SChao Yu static bool support_inline_data(struct inode *inode)
18e18c65b2SHuajun Li {
1988b88a66SJaegeuk Kim 	if (f2fs_is_atomic_file(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;
25*677a82b4SChao Yu 	return true;
26*677a82b4SChao Yu }
2792dffd01SJaegeuk Kim 
28*677a82b4SChao Yu bool f2fs_may_inline_data(struct inode *inode)
29*677a82b4SChao Yu {
30*677a82b4SChao Yu 	if (!support_inline_data(inode))
31fcc85a4dSJaegeuk Kim 		return false;
32fcc85a4dSJaegeuk Kim 
33*677a82b4SChao Yu 	return !f2fs_post_read_required(inode);
34*677a82b4SChao Yu }
35*677a82b4SChao Yu 
36*677a82b4SChao Yu bool f2fs_sanity_check_inline_data(struct inode *inode)
37*677a82b4SChao Yu {
38*677a82b4SChao Yu 	if (!f2fs_has_inline_data(inode))
39*677a82b4SChao Yu 		return false;
40*677a82b4SChao Yu 
41*677a82b4SChao Yu 	if (!support_inline_data(inode))
42e18c65b2SHuajun Li 		return true;
43*677a82b4SChao Yu 
44*677a82b4SChao Yu 	/*
45*677a82b4SChao Yu 	 * used by sanity_check_inode(), when disk layout fields has not
46*677a82b4SChao Yu 	 * been synchronized to inmem fields.
47*677a82b4SChao Yu 	 */
48*677a82b4SChao Yu 	return (S_ISREG(inode->i_mode) &&
49*677a82b4SChao Yu 		(file_is_encrypt(inode) || file_is_verity(inode) ||
50*677a82b4SChao Yu 		(F2FS_I(inode)->i_flags & F2FS_COMPR_FL)));
51e18c65b2SHuajun Li }
52e18c65b2SHuajun Li 
5301b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *inode)
5401b960e9SJaegeuk Kim {
5501b960e9SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
5601b960e9SJaegeuk Kim 		return false;
5701b960e9SJaegeuk Kim 
5801b960e9SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
5901b960e9SJaegeuk Kim 		return false;
6001b960e9SJaegeuk Kim 
6101b960e9SJaegeuk Kim 	return true;
6201b960e9SJaegeuk Kim }
6301b960e9SJaegeuk Kim 
644d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
65e18c65b2SHuajun Li {
66f2470371SChao Yu 	struct inode *inode = page->mapping->host;
67e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
68e18c65b2SHuajun Li 
69b3d208f9SJaegeuk Kim 	if (PageUptodate(page))
70b3d208f9SJaegeuk Kim 		return;
7104a17fb1SChao Yu 
72b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_P_SB(page), page->index);
73e18c65b2SHuajun Li 
74f2470371SChao Yu 	zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
75e18c65b2SHuajun Li 
76e18c65b2SHuajun Li 	/* Copy the whole inline data block */
77f2470371SChao Yu 	src_addr = inline_data_addr(inode, ipage);
78f1e33a04SJaegeuk Kim 	dst_addr = kmap_atomic(page);
79f2470371SChao Yu 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
80427a45c8SJaegeuk Kim 	flush_dcache_page(page);
81f1e33a04SJaegeuk Kim 	kunmap_atomic(dst_addr);
82237c0790SJaegeuk Kim 	if (!PageUptodate(page))
83e18c65b2SHuajun Li 		SetPageUptodate(page);
84b3d208f9SJaegeuk Kim }
85e18c65b2SHuajun Li 
864d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
874d57b86dSChao Yu 					struct page *ipage, u64 from)
88feeb0debSChao Yu {
890bfcfccaSChao Yu 	void *addr;
900bfcfccaSChao Yu 
91f2470371SChao Yu 	if (from >= MAX_INLINE_DATA(inode))
92bd4667cbSKinglong Mee 		return;
930bfcfccaSChao Yu 
94f2470371SChao Yu 	addr = inline_data_addr(inode, ipage);
950bfcfccaSChao Yu 
96bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
97f2470371SChao Yu 	memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
98ee6d182fSJaegeuk Kim 	set_page_dirty(ipage);
99bd4667cbSKinglong Mee 
100bd4667cbSKinglong Mee 	if (from == 0)
101bd4667cbSKinglong Mee 		clear_inode_flag(inode, FI_DATA_EXIST);
102feeb0debSChao Yu }
103feeb0debSChao Yu 
104b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page)
105b3d208f9SJaegeuk Kim {
106b3d208f9SJaegeuk Kim 	struct page *ipage;
107b3d208f9SJaegeuk Kim 
1084d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
109b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
110b3d208f9SJaegeuk Kim 		unlock_page(page);
111b3d208f9SJaegeuk Kim 		return PTR_ERR(ipage);
112b3d208f9SJaegeuk Kim 	}
113b3d208f9SJaegeuk Kim 
114b3d208f9SJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
115b3d208f9SJaegeuk Kim 		f2fs_put_page(ipage, 1);
116b3d208f9SJaegeuk Kim 		return -EAGAIN;
117b3d208f9SJaegeuk Kim 	}
118b3d208f9SJaegeuk Kim 
119b3d208f9SJaegeuk Kim 	if (page->index)
12009cbfeafSKirill A. Shutemov 		zero_user_segment(page, 0, PAGE_SIZE);
121b3d208f9SJaegeuk Kim 	else
1224d57b86dSChao Yu 		f2fs_do_read_inline_data(page, ipage);
123b3d208f9SJaegeuk Kim 
124237c0790SJaegeuk Kim 	if (!PageUptodate(page))
125b3d208f9SJaegeuk Kim 		SetPageUptodate(page);
126b3d208f9SJaegeuk Kim 	f2fs_put_page(ipage, 1);
127b3d208f9SJaegeuk Kim 	unlock_page(page);
128e18c65b2SHuajun Li 	return 0;
129e18c65b2SHuajun Li }
130e18c65b2SHuajun Li 
131b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
132e18c65b2SHuajun Li {
133e18c65b2SHuajun Li 	struct f2fs_io_info fio = {
13405ca3632SJaegeuk Kim 		.sbi = F2FS_I_SB(dn->inode),
13539d787beSChao Yu 		.ino = dn->inode->i_ino,
136e18c65b2SHuajun Li 		.type = DATA,
13704d328deSMike Christie 		.op = REQ_OP_WRITE,
13870fd7614SChristoph Hellwig 		.op_flags = REQ_SYNC | REQ_PRIO,
13905ca3632SJaegeuk Kim 		.page = page,
1404375a336SJaegeuk Kim 		.encrypted_page = NULL,
141b0af6d49SChao Yu 		.io_type = FS_DATA_IO,
142e18c65b2SHuajun Li 	};
1437735730dSChao Yu 	struct node_info ni;
144158c194cSJaegeuk Kim 	int dirty, err;
145e18c65b2SHuajun Li 
146b3d208f9SJaegeuk Kim 	if (!f2fs_exist_data(dn->inode))
147b3d208f9SJaegeuk Kim 		goto clear_out;
148ec4e7af4SJaegeuk Kim 
149b3d208f9SJaegeuk Kim 	err = f2fs_reserve_block(dn, 0);
15015c6e3aaSJaegeuk Kim 	if (err)
151b3d208f9SJaegeuk Kim 		return err;
152e18c65b2SHuajun Li 
153a9419b63SJaegeuk Kim 	err = f2fs_get_node_info(fio.sbi, dn->nid, &ni, false);
1547735730dSChao Yu 	if (err) {
155e8c82c11SChao Yu 		f2fs_truncate_data_blocks_range(dn, 1);
1567735730dSChao Yu 		f2fs_put_dnode(dn);
1577735730dSChao Yu 		return err;
1587735730dSChao Yu 	}
1597735730dSChao Yu 
1607735730dSChao Yu 	fio.version = ni.version;
1617735730dSChao Yu 
1624dbe38dcSChao Yu 	if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
1634dbe38dcSChao Yu 		f2fs_put_dnode(dn);
1644dbe38dcSChao Yu 		set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
165dcbb4c10SJoe Perches 		f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
1664dbe38dcSChao Yu 			  __func__, dn->inode->i_ino, dn->data_blkaddr);
16710f966bbSChao Yu 		return -EFSCORRUPTED;
1684dbe38dcSChao Yu 	}
1694dbe38dcSChao Yu 
17085ead818SYunlei He 	f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
171b3d208f9SJaegeuk Kim 
1724d57b86dSChao Yu 	f2fs_do_read_inline_data(page, dn->inode_page);
1736282adbfSJaegeuk Kim 	set_page_dirty(page);
1746282adbfSJaegeuk Kim 
175158c194cSJaegeuk Kim 	/* clear dirty state */
176158c194cSJaegeuk Kim 	dirty = clear_page_dirty_for_io(page);
177158c194cSJaegeuk Kim 
178e18c65b2SHuajun Li 	/* write data page to try to make data consistent */
179e18c65b2SHuajun Li 	set_page_writeback(page);
18017c50035SJaegeuk Kim 	ClearPageError(page);
1817a9d7548SChao Yu 	fio.old_blkaddr = dn->data_blkaddr;
182ef095d19SJaegeuk Kim 	set_inode_flag(dn->inode, FI_HOT_DATA);
1834d57b86dSChao Yu 	f2fs_outplace_write_data(dn, &fio);
184bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, DATA, true, true);
185933439c8SChao Yu 	if (dirty) {
186158c194cSJaegeuk Kim 		inode_dec_dirty_pages(dn->inode);
1874d57b86dSChao Yu 		f2fs_remove_dirty_inode(dn->inode);
188933439c8SChao Yu 	}
189e18c65b2SHuajun Li 
19095f5b0fcSJaegeuk Kim 	/* this converted inline_data should be recovered. */
19191942321SJaegeuk Kim 	set_inode_flag(dn->inode, FI_APPEND_WRITE);
19295f5b0fcSJaegeuk Kim 
193e18c65b2SHuajun Li 	/* clear inline data and flag after data writeback */
1944d57b86dSChao Yu 	f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
195b763f3beSChao Yu 	clear_page_private_inline(dn->inode_page);
196b3d208f9SJaegeuk Kim clear_out:
197b3d208f9SJaegeuk Kim 	stat_dec_inline_inode(dn->inode);
198bd4667cbSKinglong Mee 	clear_inode_flag(dn->inode, FI_INLINE_DATA);
199b3d208f9SJaegeuk Kim 	f2fs_put_dnode(dn);
200b3d208f9SJaegeuk Kim 	return 0;
201e18c65b2SHuajun Li }
202e18c65b2SHuajun Li 
203b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode)
204e18c65b2SHuajun Li {
205b3d208f9SJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
206b3d208f9SJaegeuk Kim 	struct dnode_of_data dn;
207b3d208f9SJaegeuk Kim 	struct page *ipage, *page;
208b3d208f9SJaegeuk Kim 	int err = 0;
209e18c65b2SHuajun Li 
210ec2ddf49SJaegeuk Kim 	if (!f2fs_has_inline_data(inode) ||
211ec2ddf49SJaegeuk Kim 			f2fs_hw_is_readonly(sbi) || f2fs_readonly(sbi->sb))
212b9d777b8SJaegeuk Kim 		return 0;
213b9d777b8SJaegeuk Kim 
21410a26878SChao Yu 	err = f2fs_dquot_initialize(inode);
21525fb04dbSYi Chen 	if (err)
21625fb04dbSYi Chen 		return err;
21725fb04dbSYi Chen 
218300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
219b3d208f9SJaegeuk Kim 	if (!page)
2209e09fc85SJaegeuk Kim 		return -ENOMEM;
221b3d208f9SJaegeuk Kim 
222b3d208f9SJaegeuk Kim 	f2fs_lock_op(sbi);
223b3d208f9SJaegeuk Kim 
2244d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
225b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
2266d20aff8SJaegeuk Kim 		err = PTR_ERR(ipage);
2276d20aff8SJaegeuk Kim 		goto out;
228b067ba1fSJaegeuk Kim 	}
229e18c65b2SHuajun Li 
230b3d208f9SJaegeuk Kim 	set_new_dnode(&dn, inode, ipage, ipage, 0);
231b3d208f9SJaegeuk Kim 
232b3d208f9SJaegeuk Kim 	if (f2fs_has_inline_data(inode))
233b3d208f9SJaegeuk Kim 		err = f2fs_convert_inline_page(&dn, page);
234b3d208f9SJaegeuk Kim 
235b3d208f9SJaegeuk Kim 	f2fs_put_dnode(&dn);
2366d20aff8SJaegeuk Kim out:
237b3d208f9SJaegeuk Kim 	f2fs_unlock_op(sbi);
238b3d208f9SJaegeuk Kim 
239b3d208f9SJaegeuk Kim 	f2fs_put_page(page, 1);
2402a340760SJaegeuk Kim 
24128e18ee6SColin Ian King 	if (!err)
2422c4db1a6SJaegeuk Kim 		f2fs_balance_fs(sbi, dn.node_changed);
2432a340760SJaegeuk Kim 
244e18c65b2SHuajun Li 	return err;
245e18c65b2SHuajun Li }
246e18c65b2SHuajun Li 
247b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page)
248e18c65b2SHuajun Li {
249e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
250e18c65b2SHuajun Li 	struct dnode_of_data dn;
251e18c65b2SHuajun Li 	int err;
252e18c65b2SHuajun Li 
253e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, NULL, NULL, 0);
2544d57b86dSChao Yu 	err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
255e18c65b2SHuajun Li 	if (err)
256e18c65b2SHuajun Li 		return err;
257e18c65b2SHuajun Li 
258c08a690bSJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
259b3d208f9SJaegeuk Kim 		f2fs_put_dnode(&dn);
260b3d208f9SJaegeuk Kim 		return -EAGAIN;
261c08a690bSJaegeuk Kim 	}
262c08a690bSJaegeuk Kim 
263b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_I_SB(inode), page->index);
264b3d208f9SJaegeuk Kim 
265bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
266f1e33a04SJaegeuk Kim 	src_addr = kmap_atomic(page);
267f2470371SChao Yu 	dst_addr = inline_data_addr(inode, dn.inode_page);
268f2470371SChao Yu 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
269f1e33a04SJaegeuk Kim 	kunmap_atomic(src_addr);
270ee6d182fSJaegeuk Kim 	set_page_dirty(dn.inode_page);
271e18c65b2SHuajun Li 
2725ec2d99dSMatthew Wilcox 	f2fs_clear_page_cache_dirty_tag(page);
2730abd8e70SDaeho Jeong 
27491942321SJaegeuk Kim 	set_inode_flag(inode, FI_APPEND_WRITE);
27591942321SJaegeuk Kim 	set_inode_flag(inode, FI_DATA_EXIST);
276b3d208f9SJaegeuk Kim 
277b763f3beSChao Yu 	clear_page_private_inline(dn.inode_page);
278e18c65b2SHuajun Li 	f2fs_put_dnode(&dn);
279e18c65b2SHuajun Li 	return 0;
280e18c65b2SHuajun Li }
2811e1bb4baSJaegeuk Kim 
2829627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage)
2831e1bb4baSJaegeuk Kim {
2844081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2851e1bb4baSJaegeuk Kim 	struct f2fs_inode *ri = NULL;
2861e1bb4baSJaegeuk Kim 	void *src_addr, *dst_addr;
2871e1bb4baSJaegeuk Kim 	struct page *ipage;
2881e1bb4baSJaegeuk Kim 
2891e1bb4baSJaegeuk Kim 	/*
2901e1bb4baSJaegeuk Kim 	 * The inline_data recovery policy is as follows.
2911e1bb4baSJaegeuk Kim 	 * [prev.] [next] of inline_data flag
2921e1bb4baSJaegeuk Kim 	 *    o       o  -> recover inline_data
2931e1bb4baSJaegeuk Kim 	 *    o       x  -> remove inline_data, and then recover data blocks
2946e5ca4fcSJack Qiu 	 *    x       o  -> remove data blocks, and then recover inline_data
2951e1bb4baSJaegeuk Kim 	 *    x       x  -> recover data blocks
2961e1bb4baSJaegeuk Kim 	 */
2971e1bb4baSJaegeuk Kim 	if (IS_INODE(npage))
2981e1bb4baSJaegeuk Kim 		ri = F2FS_INODE(npage);
2991e1bb4baSJaegeuk Kim 
3001e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode) &&
3010342fd30SJaegeuk Kim 			ri && (ri->i_inline & F2FS_INLINE_DATA)) {
3021e1bb4baSJaegeuk Kim process_inline:
3034d57b86dSChao Yu 		ipage = f2fs_get_node_page(sbi, inode->i_ino);
3049627a7b3SChao Yu 		if (IS_ERR(ipage))
3059627a7b3SChao Yu 			return PTR_ERR(ipage);
3061e1bb4baSJaegeuk Kim 
307bae0ee7aSChao Yu 		f2fs_wait_on_page_writeback(ipage, NODE, true, true);
30854b591dfSJaegeuk Kim 
309f2470371SChao Yu 		src_addr = inline_data_addr(inode, npage);
310f2470371SChao Yu 		dst_addr = inline_data_addr(inode, ipage);
311f2470371SChao Yu 		memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
312b3d208f9SJaegeuk Kim 
31391942321SJaegeuk Kim 		set_inode_flag(inode, FI_INLINE_DATA);
31491942321SJaegeuk Kim 		set_inode_flag(inode, FI_DATA_EXIST);
315b3d208f9SJaegeuk Kim 
316ee6d182fSJaegeuk Kim 		set_page_dirty(ipage);
3171e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
3189627a7b3SChao Yu 		return 1;
3191e1bb4baSJaegeuk Kim 	}
3201e1bb4baSJaegeuk Kim 
3211e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode)) {
3224d57b86dSChao Yu 		ipage = f2fs_get_node_page(sbi, inode->i_ino);
3239627a7b3SChao Yu 		if (IS_ERR(ipage))
3249627a7b3SChao Yu 			return PTR_ERR(ipage);
3254d57b86dSChao Yu 		f2fs_truncate_inline_inode(inode, ipage, 0);
32684921561SJack Qiu 		stat_dec_inline_inode(inode);
327bd4667cbSKinglong Mee 		clear_inode_flag(inode, FI_INLINE_DATA);
3281e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
3290342fd30SJaegeuk Kim 	} else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
3309627a7b3SChao Yu 		int ret;
3319627a7b3SChao Yu 
3329627a7b3SChao Yu 		ret = f2fs_truncate_blocks(inode, 0, false);
3339627a7b3SChao Yu 		if (ret)
3349627a7b3SChao Yu 			return ret;
33584921561SJack Qiu 		stat_inc_inline_inode(inode);
3361e1bb4baSJaegeuk Kim 		goto process_inline;
3371e1bb4baSJaegeuk Kim 	}
3389627a7b3SChao Yu 	return 0;
3391e1bb4baSJaegeuk Kim }
340201a05beSChao Yu 
3414d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
34243c780baSEric Biggers 					const struct f2fs_filename *fname,
34343c780baSEric Biggers 					struct page **res_page)
344201a05beSChao Yu {
345201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
346201a05beSChao Yu 	struct f2fs_dir_entry *de;
3477b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
3484e6ebf6dSJaegeuk Kim 	struct page *ipage;
349f2470371SChao Yu 	void *inline_dentry;
350201a05beSChao Yu 
3514d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
35242d96401SJaegeuk Kim 	if (IS_ERR(ipage)) {
35342d96401SJaegeuk Kim 		*res_page = ipage;
354201a05beSChao Yu 		return NULL;
35542d96401SJaegeuk Kim 	}
356201a05beSChao Yu 
357f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
358201a05beSChao Yu 
359f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
36043c780baSEric Biggers 	de = f2fs_find_target_dentry(&d, fname, NULL);
3614e6ebf6dSJaegeuk Kim 	unlock_page(ipage);
3627ad08a58SDaniel Rosenberg 	if (IS_ERR(de)) {
3637ad08a58SDaniel Rosenberg 		*res_page = ERR_CAST(de);
3647ad08a58SDaniel Rosenberg 		de = NULL;
3657ad08a58SDaniel Rosenberg 	}
3664e6ebf6dSJaegeuk Kim 	if (de)
367201a05beSChao Yu 		*res_page = ipage;
3684e6ebf6dSJaegeuk Kim 	else
3694e6ebf6dSJaegeuk Kim 		f2fs_put_page(ipage, 0);
370201a05beSChao Yu 
371201a05beSChao Yu 	return de;
372201a05beSChao Yu }
373201a05beSChao Yu 
3744d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
375201a05beSChao Yu 							struct page *ipage)
376201a05beSChao Yu {
377062a3e7bSJaegeuk Kim 	struct f2fs_dentry_ptr d;
378f2470371SChao Yu 	void *inline_dentry;
379201a05beSChao Yu 
380f2470371SChao Yu 	inline_dentry = inline_data_addr(inode, ipage);
381201a05beSChao Yu 
382f2470371SChao Yu 	make_dentry_ptr_inline(inode, &d, inline_dentry);
3834d57b86dSChao Yu 	f2fs_do_make_empty_dir(inode, parent, &d);
384201a05beSChao Yu 
385201a05beSChao Yu 	set_page_dirty(ipage);
386201a05beSChao Yu 
387201a05beSChao Yu 	/* update i_size to MAX_INLINE_DATA */
388f2470371SChao Yu 	if (i_size_read(inode) < MAX_INLINE_DATA(inode))
389f2470371SChao Yu 		f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
390201a05beSChao Yu 	return 0;
391201a05beSChao Yu }
392201a05beSChao Yu 
393470f00e9SChao Yu /*
394470f00e9SChao Yu  * NOTE: ipage is grabbed by caller, but if any error occurs, we should
395470f00e9SChao Yu  * release ipage in this function.
396470f00e9SChao Yu  */
397675f10bdSChao Yu static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
398f2470371SChao Yu 							void *inline_dentry)
399201a05beSChao Yu {
400201a05beSChao Yu 	struct page *page;
401201a05beSChao Yu 	struct dnode_of_data dn;
402201a05beSChao Yu 	struct f2fs_dentry_block *dentry_blk;
40376a9dd85SChao Yu 	struct f2fs_dentry_ptr src, dst;
404201a05beSChao Yu 	int err;
405201a05beSChao Yu 
406dd973007SJaegeuk Kim 	page = f2fs_grab_cache_page(dir->i_mapping, 0, true);
407470f00e9SChao Yu 	if (!page) {
408470f00e9SChao Yu 		f2fs_put_page(ipage, 1);
409201a05beSChao Yu 		return -ENOMEM;
410470f00e9SChao Yu 	}
411201a05beSChao Yu 
412201a05beSChao Yu 	set_new_dnode(&dn, dir, ipage, NULL, 0);
413201a05beSChao Yu 	err = f2fs_reserve_block(&dn, 0);
414201a05beSChao Yu 	if (err)
415201a05beSChao Yu 		goto out;
416201a05beSChao Yu 
4174dbe38dcSChao Yu 	if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
4184dbe38dcSChao Yu 		f2fs_put_dnode(&dn);
4194dbe38dcSChao Yu 		set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
420dcbb4c10SJoe Perches 		f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
4214dbe38dcSChao Yu 			  __func__, dir->i_ino, dn.data_blkaddr);
42210f966bbSChao Yu 		err = -EFSCORRUPTED;
4234dbe38dcSChao Yu 		goto out;
4244dbe38dcSChao Yu 	}
4254dbe38dcSChao Yu 
426bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, DATA, true, true);
427201a05beSChao Yu 
428bdbc90faSYunlong Song 	dentry_blk = page_address(page);
429201a05beSChao Yu 
430f2470371SChao Yu 	make_dentry_ptr_inline(dir, &src, inline_dentry);
431f2470371SChao Yu 	make_dentry_ptr_block(dir, &dst, dentry_blk);
43276a9dd85SChao Yu 
433201a05beSChao Yu 	/* copy data from inline dentry block to new dentry block */
43476a9dd85SChao Yu 	memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
43576a9dd85SChao Yu 	memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
4364ec17d68SChao Yu 	/*
4374ec17d68SChao Yu 	 * we do not need to zero out remainder part of dentry and filename
4384ec17d68SChao Yu 	 * field, since we have used bitmap for marking the usage status of
4394ec17d68SChao Yu 	 * them, besides, we can also ignore copying/zeroing reserved space
4404ec17d68SChao Yu 	 * of dentry block, because them haven't been used so far.
4414ec17d68SChao Yu 	 */
44276a9dd85SChao Yu 	memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
44376a9dd85SChao Yu 	memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
444201a05beSChao Yu 
445237c0790SJaegeuk Kim 	if (!PageUptodate(page))
446201a05beSChao Yu 		SetPageUptodate(page);
447201a05beSChao Yu 	set_page_dirty(page);
448201a05beSChao Yu 
449201a05beSChao Yu 	/* clear inline dir and flag after data writeback */
4504d57b86dSChao Yu 	f2fs_truncate_inline_inode(dir, ipage, 0);
451b3d208f9SJaegeuk Kim 
4523289c061SJaegeuk Kim 	stat_dec_inline_dir(dir);
45391942321SJaegeuk Kim 	clear_inode_flag(dir, FI_INLINE_DENTRY);
454201a05beSChao Yu 
45545a74688SChao Yu 	/*
45645a74688SChao Yu 	 * should retrieve reserved space which was used to keep
45745a74688SChao Yu 	 * inline_dentry's structure for backward compatibility.
45845a74688SChao Yu 	 */
45945a74688SChao Yu 	if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
46045a74688SChao Yu 			!f2fs_has_inline_xattr(dir))
46145a74688SChao Yu 		F2FS_I(dir)->i_inline_xattr_size = 0;
46245a74688SChao Yu 
463205b9822SJaegeuk Kim 	f2fs_i_depth_write(dir, 1);
464ee6d182fSJaegeuk Kim 	if (i_size_read(dir) < PAGE_SIZE)
465fc9581c8SJaegeuk Kim 		f2fs_i_size_write(dir, PAGE_SIZE);
466201a05beSChao Yu out:
467201a05beSChao Yu 	f2fs_put_page(page, 1);
468201a05beSChao Yu 	return err;
469201a05beSChao Yu }
470201a05beSChao Yu 
471f2470371SChao Yu static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
472675f10bdSChao Yu {
473675f10bdSChao Yu 	struct f2fs_dentry_ptr d;
474675f10bdSChao Yu 	unsigned long bit_pos = 0;
475675f10bdSChao Yu 	int err = 0;
476675f10bdSChao Yu 
477f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
478675f10bdSChao Yu 
479675f10bdSChao Yu 	while (bit_pos < d.max) {
480675f10bdSChao Yu 		struct f2fs_dir_entry *de;
48143c780baSEric Biggers 		struct f2fs_filename fname;
482675f10bdSChao Yu 		nid_t ino;
483675f10bdSChao Yu 		umode_t fake_mode;
484675f10bdSChao Yu 
485675f10bdSChao Yu 		if (!test_bit_le(bit_pos, d.bitmap)) {
486675f10bdSChao Yu 			bit_pos++;
487675f10bdSChao Yu 			continue;
488675f10bdSChao Yu 		}
489675f10bdSChao Yu 
490675f10bdSChao Yu 		de = &d.dentry[bit_pos];
491a4a13f58SChao Yu 
492a4a13f58SChao Yu 		if (unlikely(!de->name_len)) {
493a4a13f58SChao Yu 			bit_pos++;
494a4a13f58SChao Yu 			continue;
495a4a13f58SChao Yu 		}
496a4a13f58SChao Yu 
49743c780baSEric Biggers 		/*
49843c780baSEric Biggers 		 * We only need the disk_name and hash to move the dentry.
49943c780baSEric Biggers 		 * We don't need the original or casefolded filenames.
50043c780baSEric Biggers 		 */
50143c780baSEric Biggers 		memset(&fname, 0, sizeof(fname));
50243c780baSEric Biggers 		fname.disk_name.name = d.filename[bit_pos];
50343c780baSEric Biggers 		fname.disk_name.len = le16_to_cpu(de->name_len);
50443c780baSEric Biggers 		fname.hash = de->hash_code;
505675f10bdSChao Yu 
506675f10bdSChao Yu 		ino = le32_to_cpu(de->ino);
5074d57b86dSChao Yu 		fake_mode = f2fs_get_de_type(de) << S_SHIFT;
508675f10bdSChao Yu 
50943c780baSEric Biggers 		err = f2fs_add_regular_entry(dir, &fname, NULL, ino, fake_mode);
510675f10bdSChao Yu 		if (err)
511675f10bdSChao Yu 			goto punch_dentry_pages;
512675f10bdSChao Yu 
513675f10bdSChao Yu 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
514675f10bdSChao Yu 	}
515675f10bdSChao Yu 	return 0;
516675f10bdSChao Yu punch_dentry_pages:
517675f10bdSChao Yu 	truncate_inode_pages(&dir->i_data, 0);
518c42d28ceSChao Yu 	f2fs_truncate_blocks(dir, 0, false);
5194d57b86dSChao Yu 	f2fs_remove_dirty_inode(dir);
520675f10bdSChao Yu 	return err;
521675f10bdSChao Yu }
522675f10bdSChao Yu 
523675f10bdSChao Yu static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
524f2470371SChao Yu 							void *inline_dentry)
525675f10bdSChao Yu {
526f2470371SChao Yu 	void *backup_dentry;
527675f10bdSChao Yu 	int err;
528675f10bdSChao Yu 
5291ecc0c5cSChao Yu 	backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
530f2470371SChao Yu 				MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
5318975bdf4SChao Yu 	if (!backup_dentry) {
5328975bdf4SChao Yu 		f2fs_put_page(ipage, 1);
533675f10bdSChao Yu 		return -ENOMEM;
5348975bdf4SChao Yu 	}
535675f10bdSChao Yu 
536f2470371SChao Yu 	memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
5374d57b86dSChao Yu 	f2fs_truncate_inline_inode(dir, ipage, 0);
538675f10bdSChao Yu 
539675f10bdSChao Yu 	unlock_page(ipage);
540675f10bdSChao Yu 
541675f10bdSChao Yu 	err = f2fs_add_inline_entries(dir, backup_dentry);
542675f10bdSChao Yu 	if (err)
543675f10bdSChao Yu 		goto recover;
544675f10bdSChao Yu 
545675f10bdSChao Yu 	lock_page(ipage);
546675f10bdSChao Yu 
547675f10bdSChao Yu 	stat_dec_inline_dir(dir);
54891942321SJaegeuk Kim 	clear_inode_flag(dir, FI_INLINE_DENTRY);
54945a74688SChao Yu 
55045a74688SChao Yu 	/*
55145a74688SChao Yu 	 * should retrieve reserved space which was used to keep
55245a74688SChao Yu 	 * inline_dentry's structure for backward compatibility.
55345a74688SChao Yu 	 */
55445a74688SChao Yu 	if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
55545a74688SChao Yu 			!f2fs_has_inline_xattr(dir))
55645a74688SChao Yu 		F2FS_I(dir)->i_inline_xattr_size = 0;
55745a74688SChao Yu 
558c8eb7024SChao Yu 	kfree(backup_dentry);
559675f10bdSChao Yu 	return 0;
560675f10bdSChao Yu recover:
561675f10bdSChao Yu 	lock_page(ipage);
562bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
563f2470371SChao Yu 	memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
564205b9822SJaegeuk Kim 	f2fs_i_depth_write(dir, 0);
565f2470371SChao Yu 	f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
566ee6d182fSJaegeuk Kim 	set_page_dirty(ipage);
567675f10bdSChao Yu 	f2fs_put_page(ipage, 1);
568675f10bdSChao Yu 
569c8eb7024SChao Yu 	kfree(backup_dentry);
570675f10bdSChao Yu 	return err;
571675f10bdSChao Yu }
572675f10bdSChao Yu 
573b06af2afSJaegeuk Kim static int do_convert_inline_dir(struct inode *dir, struct page *ipage,
574f2470371SChao Yu 							void *inline_dentry)
575675f10bdSChao Yu {
576675f10bdSChao Yu 	if (!F2FS_I(dir)->i_dir_level)
577675f10bdSChao Yu 		return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
578675f10bdSChao Yu 	else
579675f10bdSChao Yu 		return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
580675f10bdSChao Yu }
581675f10bdSChao Yu 
582b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry)
583b06af2afSJaegeuk Kim {
584b06af2afSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
585b06af2afSJaegeuk Kim 	struct page *ipage;
58643c780baSEric Biggers 	struct f2fs_filename fname;
587b06af2afSJaegeuk Kim 	void *inline_dentry = NULL;
588b06af2afSJaegeuk Kim 	int err = 0;
589b06af2afSJaegeuk Kim 
590b06af2afSJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
591b06af2afSJaegeuk Kim 		return 0;
592b06af2afSJaegeuk Kim 
593b06af2afSJaegeuk Kim 	f2fs_lock_op(sbi);
594b06af2afSJaegeuk Kim 
59543c780baSEric Biggers 	err = f2fs_setup_filename(dir, &dentry->d_name, 0, &fname);
596b06af2afSJaegeuk Kim 	if (err)
597b06af2afSJaegeuk Kim 		goto out;
598b06af2afSJaegeuk Kim 
599b06af2afSJaegeuk Kim 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
600b06af2afSJaegeuk Kim 	if (IS_ERR(ipage)) {
601b06af2afSJaegeuk Kim 		err = PTR_ERR(ipage);
602ff5f85c8SEric Biggers 		goto out_fname;
603b06af2afSJaegeuk Kim 	}
604b06af2afSJaegeuk Kim 
605b06af2afSJaegeuk Kim 	if (f2fs_has_enough_room(dir, ipage, &fname)) {
606b06af2afSJaegeuk Kim 		f2fs_put_page(ipage, 1);
607ff5f85c8SEric Biggers 		goto out_fname;
608b06af2afSJaegeuk Kim 	}
609b06af2afSJaegeuk Kim 
610b06af2afSJaegeuk Kim 	inline_dentry = inline_data_addr(dir, ipage);
611b06af2afSJaegeuk Kim 
612b06af2afSJaegeuk Kim 	err = do_convert_inline_dir(dir, ipage, inline_dentry);
613b06af2afSJaegeuk Kim 	if (!err)
614b06af2afSJaegeuk Kim 		f2fs_put_page(ipage, 1);
615ff5f85c8SEric Biggers out_fname:
61643c780baSEric Biggers 	f2fs_free_filename(&fname);
617b06af2afSJaegeuk Kim out:
618b06af2afSJaegeuk Kim 	f2fs_unlock_op(sbi);
619b06af2afSJaegeuk Kim 	return err;
620b06af2afSJaegeuk Kim }
621b06af2afSJaegeuk Kim 
62243c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
623510022a8SJaegeuk Kim 			  struct inode *inode, nid_t ino, umode_t mode)
624201a05beSChao Yu {
625201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
626201a05beSChao Yu 	struct page *ipage;
627201a05beSChao Yu 	unsigned int bit_pos;
628f2470371SChao Yu 	void *inline_dentry = NULL;
6293b4d732aSChao Yu 	struct f2fs_dentry_ptr d;
63043c780baSEric Biggers 	int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
631510022a8SJaegeuk Kim 	struct page *page = NULL;
632201a05beSChao Yu 	int err = 0;
633201a05beSChao Yu 
6344d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
635201a05beSChao Yu 	if (IS_ERR(ipage))
636201a05beSChao Yu 		return PTR_ERR(ipage);
637201a05beSChao Yu 
638f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
639f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
64076a9dd85SChao Yu 
6414d57b86dSChao Yu 	bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
64276a9dd85SChao Yu 	if (bit_pos >= d.max) {
643b06af2afSJaegeuk Kim 		err = do_convert_inline_dir(dir, ipage, inline_dentry);
644470f00e9SChao Yu 		if (err)
645470f00e9SChao Yu 			return err;
646201a05beSChao Yu 		err = -EAGAIN;
647201a05beSChao Yu 		goto out;
648201a05beSChao Yu 	}
649201a05beSChao Yu 
650510022a8SJaegeuk Kim 	if (inode) {
651e4544b63STim Murray 		f2fs_down_write(&F2FS_I(inode)->i_sem);
65243c780baSEric Biggers 		page = f2fs_init_inode_metadata(inode, dir, fname, ipage);
653201a05beSChao Yu 		if (IS_ERR(page)) {
654201a05beSChao Yu 			err = PTR_ERR(page);
655201a05beSChao Yu 			goto fail;
656201a05beSChao Yu 		}
657510022a8SJaegeuk Kim 	}
658bce8d112SJaegeuk Kim 
659bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
6603b4d732aSChao Yu 
66143c780baSEric Biggers 	f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
66243c780baSEric Biggers 			   bit_pos);
6633b4d732aSChao Yu 
664201a05beSChao Yu 	set_page_dirty(ipage);
665201a05beSChao Yu 
666201a05beSChao Yu 	/* we don't need to mark_inode_dirty now */
667510022a8SJaegeuk Kim 	if (inode) {
668205b9822SJaegeuk Kim 		f2fs_i_pino_write(inode, dir->i_ino);
66998194030SChao Yu 
67098194030SChao Yu 		/* synchronize inode page's data from inode cache */
67198194030SChao Yu 		if (is_inode_flag_set(inode, FI_NEW_INODE))
67298194030SChao Yu 			f2fs_update_inode(inode, page);
67398194030SChao Yu 
674201a05beSChao Yu 		f2fs_put_page(page, 1);
675510022a8SJaegeuk Kim 	}
676201a05beSChao Yu 
6774d57b86dSChao Yu 	f2fs_update_parent_metadata(dir, inode, 0);
678201a05beSChao Yu fail:
679510022a8SJaegeuk Kim 	if (inode)
680e4544b63STim Murray 		f2fs_up_write(&F2FS_I(inode)->i_sem);
681201a05beSChao Yu out:
682201a05beSChao Yu 	f2fs_put_page(ipage, 1);
683201a05beSChao Yu 	return err;
684201a05beSChao Yu }
685201a05beSChao Yu 
686201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
687201a05beSChao Yu 					struct inode *dir, struct inode *inode)
688201a05beSChao Yu {
68976a9dd85SChao Yu 	struct f2fs_dentry_ptr d;
690f2470371SChao Yu 	void *inline_dentry;
691201a05beSChao Yu 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
692201a05beSChao Yu 	unsigned int bit_pos;
693201a05beSChao Yu 	int i;
694201a05beSChao Yu 
695201a05beSChao Yu 	lock_page(page);
696bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, NODE, true, true);
697201a05beSChao Yu 
698f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, page);
699f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
70076a9dd85SChao Yu 
70176a9dd85SChao Yu 	bit_pos = dentry - d.dentry;
702201a05beSChao Yu 	for (i = 0; i < slots; i++)
70376a9dd85SChao Yu 		__clear_bit_le(bit_pos + i, d.bitmap);
704201a05beSChao Yu 
705201a05beSChao Yu 	set_page_dirty(page);
7069f7c45ccSJaegeuk Kim 	f2fs_put_page(page, 1);
707201a05beSChao Yu 
708078cd827SDeepa Dinamani 	dir->i_ctime = dir->i_mtime = current_time(dir);
7097c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(dir, false);
710201a05beSChao Yu 
711201a05beSChao Yu 	if (inode)
7129f7c45ccSJaegeuk Kim 		f2fs_drop_nlink(dir, inode);
713201a05beSChao Yu }
714201a05beSChao Yu 
715201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir)
716201a05beSChao Yu {
717201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
718201a05beSChao Yu 	struct page *ipage;
719201a05beSChao Yu 	unsigned int bit_pos = 2;
720f2470371SChao Yu 	void *inline_dentry;
72176a9dd85SChao Yu 	struct f2fs_dentry_ptr d;
722201a05beSChao Yu 
7234d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
724201a05beSChao Yu 	if (IS_ERR(ipage))
725201a05beSChao Yu 		return false;
726201a05beSChao Yu 
727f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
728f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
72976a9dd85SChao Yu 
73076a9dd85SChao Yu 	bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
731201a05beSChao Yu 
732201a05beSChao Yu 	f2fs_put_page(ipage, 1);
733201a05beSChao Yu 
73476a9dd85SChao Yu 	if (bit_pos < d.max)
735201a05beSChao Yu 		return false;
736201a05beSChao Yu 
737201a05beSChao Yu 	return true;
738201a05beSChao Yu }
739201a05beSChao Yu 
740d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
7410b81d077SJaegeuk Kim 				struct fscrypt_str *fstr)
742201a05beSChao Yu {
743201a05beSChao Yu 	struct inode *inode = file_inode(file);
744201a05beSChao Yu 	struct page *ipage = NULL;
7457b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
746f2470371SChao Yu 	void *inline_dentry = NULL;
747ed6bd4b1SChao Yu 	int err;
748201a05beSChao Yu 
74976a9dd85SChao Yu 	make_dentry_ptr_inline(inode, &d, inline_dentry);
75076a9dd85SChao Yu 
75176a9dd85SChao Yu 	if (ctx->pos == d.max)
752201a05beSChao Yu 		return 0;
753201a05beSChao Yu 
7544d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
755201a05beSChao Yu 	if (IS_ERR(ipage))
756201a05beSChao Yu 		return PTR_ERR(ipage);
757201a05beSChao Yu 
758aadcef64SChao Yu 	/*
759aadcef64SChao Yu 	 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
760aadcef64SChao Yu 	 * ipage without page's lock held.
761aadcef64SChao Yu 	 */
762aadcef64SChao Yu 	unlock_page(ipage);
763aadcef64SChao Yu 
764f2470371SChao Yu 	inline_dentry = inline_data_addr(inode, ipage);
765201a05beSChao Yu 
76664c24ecbSTomohiro Kusumi 	make_dentry_ptr_inline(inode, &d, inline_dentry);
7677b3cd7d6SJaegeuk Kim 
768ed6bd4b1SChao Yu 	err = f2fs_fill_dentries(ctx, &d, 0, fstr);
769ed6bd4b1SChao Yu 	if (!err)
77076a9dd85SChao Yu 		ctx->pos = d.max;
771201a05beSChao Yu 
772aadcef64SChao Yu 	f2fs_put_page(ipage, 0);
773ed6bd4b1SChao Yu 	return err < 0 ? err : 0;
774201a05beSChao Yu }
77567f8cf3cSJaegeuk Kim 
77667f8cf3cSJaegeuk Kim int f2fs_inline_data_fiemap(struct inode *inode,
77767f8cf3cSJaegeuk Kim 		struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
77867f8cf3cSJaegeuk Kim {
77967f8cf3cSJaegeuk Kim 	__u64 byteaddr, ilen;
78067f8cf3cSJaegeuk Kim 	__u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
78167f8cf3cSJaegeuk Kim 		FIEMAP_EXTENT_LAST;
78267f8cf3cSJaegeuk Kim 	struct node_info ni;
78367f8cf3cSJaegeuk Kim 	struct page *ipage;
78467f8cf3cSJaegeuk Kim 	int err = 0;
78567f8cf3cSJaegeuk Kim 
7864d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
78767f8cf3cSJaegeuk Kim 	if (IS_ERR(ipage))
78867f8cf3cSJaegeuk Kim 		return PTR_ERR(ipage);
78967f8cf3cSJaegeuk Kim 
7907975f349SChao Yu 	if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
7917975f349SChao Yu 				!f2fs_has_inline_data(inode)) {
7927975f349SChao Yu 		err = -EAGAIN;
7937975f349SChao Yu 		goto out;
7947975f349SChao Yu 	}
7957975f349SChao Yu 
7967975f349SChao Yu 	if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
79767f8cf3cSJaegeuk Kim 		err = -EAGAIN;
79867f8cf3cSJaegeuk Kim 		goto out;
79967f8cf3cSJaegeuk Kim 	}
80067f8cf3cSJaegeuk Kim 
801f2470371SChao Yu 	ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
80267f8cf3cSJaegeuk Kim 	if (start >= ilen)
80367f8cf3cSJaegeuk Kim 		goto out;
80467f8cf3cSJaegeuk Kim 	if (start + len < ilen)
80567f8cf3cSJaegeuk Kim 		ilen = start + len;
80667f8cf3cSJaegeuk Kim 	ilen -= start;
80767f8cf3cSJaegeuk Kim 
808a9419b63SJaegeuk Kim 	err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni, false);
8097735730dSChao Yu 	if (err)
8107735730dSChao Yu 		goto out;
8117735730dSChao Yu 
81267f8cf3cSJaegeuk Kim 	byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
813f2470371SChao Yu 	byteaddr += (char *)inline_data_addr(inode, ipage) -
814f2470371SChao Yu 					(char *)F2FS_INODE(ipage);
81567f8cf3cSJaegeuk Kim 	err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
816dd5a09bdSChao Yu 	trace_f2fs_fiemap(inode, start, byteaddr, ilen, flags, err);
81767f8cf3cSJaegeuk Kim out:
81867f8cf3cSJaegeuk Kim 	f2fs_put_page(ipage, 1);
81967f8cf3cSJaegeuk Kim 	return err;
82067f8cf3cSJaegeuk Kim }
821