xref: /openbmc/linux/fs/f2fs/inline.c (revision 84921561532f771bd338e7791bf275b2b605d642)
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 
1701b960e9SJaegeuk Kim bool f2fs_may_inline_data(struct inode *inode)
18e18c65b2SHuajun Li {
1988b88a66SJaegeuk Kim 	if (f2fs_is_atomic_file(inode))
2088b88a66SJaegeuk Kim 		return false;
2188b88a66SJaegeuk Kim 
22368a0e40SWanpeng Li 	if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
23e18c65b2SHuajun Li 		return false;
24e18c65b2SHuajun Li 
25f2470371SChao Yu 	if (i_size_read(inode) > MAX_INLINE_DATA(inode))
2692dffd01SJaegeuk Kim 		return false;
2792dffd01SJaegeuk Kim 
286dbb1796SEric Biggers 	if (f2fs_post_read_required(inode))
29fcc85a4dSJaegeuk Kim 		return false;
30fcc85a4dSJaegeuk Kim 
31e18c65b2SHuajun Li 	return true;
32e18c65b2SHuajun Li }
33e18c65b2SHuajun Li 
3401b960e9SJaegeuk Kim bool f2fs_may_inline_dentry(struct inode *inode)
3501b960e9SJaegeuk Kim {
3601b960e9SJaegeuk Kim 	if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
3701b960e9SJaegeuk Kim 		return false;
3801b960e9SJaegeuk Kim 
3901b960e9SJaegeuk Kim 	if (!S_ISDIR(inode->i_mode))
4001b960e9SJaegeuk Kim 		return false;
4101b960e9SJaegeuk Kim 
4201b960e9SJaegeuk Kim 	return true;
4301b960e9SJaegeuk Kim }
4401b960e9SJaegeuk Kim 
454d57b86dSChao Yu void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
46e18c65b2SHuajun Li {
47f2470371SChao Yu 	struct inode *inode = page->mapping->host;
48e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
49e18c65b2SHuajun Li 
50b3d208f9SJaegeuk Kim 	if (PageUptodate(page))
51b3d208f9SJaegeuk Kim 		return;
5204a17fb1SChao Yu 
53b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_P_SB(page), page->index);
54e18c65b2SHuajun Li 
55f2470371SChao Yu 	zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
56e18c65b2SHuajun Li 
57e18c65b2SHuajun Li 	/* Copy the whole inline data block */
58f2470371SChao Yu 	src_addr = inline_data_addr(inode, ipage);
59f1e33a04SJaegeuk Kim 	dst_addr = kmap_atomic(page);
60f2470371SChao Yu 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
61427a45c8SJaegeuk Kim 	flush_dcache_page(page);
62f1e33a04SJaegeuk Kim 	kunmap_atomic(dst_addr);
63237c0790SJaegeuk Kim 	if (!PageUptodate(page))
64e18c65b2SHuajun Li 		SetPageUptodate(page);
65b3d208f9SJaegeuk Kim }
66e18c65b2SHuajun Li 
674d57b86dSChao Yu void f2fs_truncate_inline_inode(struct inode *inode,
684d57b86dSChao Yu 					struct page *ipage, u64 from)
69feeb0debSChao Yu {
700bfcfccaSChao Yu 	void *addr;
710bfcfccaSChao Yu 
72f2470371SChao Yu 	if (from >= MAX_INLINE_DATA(inode))
73bd4667cbSKinglong Mee 		return;
740bfcfccaSChao Yu 
75f2470371SChao Yu 	addr = inline_data_addr(inode, ipage);
760bfcfccaSChao Yu 
77bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
78f2470371SChao Yu 	memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
79ee6d182fSJaegeuk Kim 	set_page_dirty(ipage);
80bd4667cbSKinglong Mee 
81bd4667cbSKinglong Mee 	if (from == 0)
82bd4667cbSKinglong Mee 		clear_inode_flag(inode, FI_DATA_EXIST);
83feeb0debSChao Yu }
84feeb0debSChao Yu 
85b3d208f9SJaegeuk Kim int f2fs_read_inline_data(struct inode *inode, struct page *page)
86b3d208f9SJaegeuk Kim {
87b3d208f9SJaegeuk Kim 	struct page *ipage;
88b3d208f9SJaegeuk Kim 
894d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
90b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
91b3d208f9SJaegeuk Kim 		unlock_page(page);
92b3d208f9SJaegeuk Kim 		return PTR_ERR(ipage);
93b3d208f9SJaegeuk Kim 	}
94b3d208f9SJaegeuk Kim 
95b3d208f9SJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
96b3d208f9SJaegeuk Kim 		f2fs_put_page(ipage, 1);
97b3d208f9SJaegeuk Kim 		return -EAGAIN;
98b3d208f9SJaegeuk Kim 	}
99b3d208f9SJaegeuk Kim 
100b3d208f9SJaegeuk Kim 	if (page->index)
10109cbfeafSKirill A. Shutemov 		zero_user_segment(page, 0, PAGE_SIZE);
102b3d208f9SJaegeuk Kim 	else
1034d57b86dSChao Yu 		f2fs_do_read_inline_data(page, ipage);
104b3d208f9SJaegeuk Kim 
105237c0790SJaegeuk Kim 	if (!PageUptodate(page))
106b3d208f9SJaegeuk Kim 		SetPageUptodate(page);
107b3d208f9SJaegeuk Kim 	f2fs_put_page(ipage, 1);
108b3d208f9SJaegeuk Kim 	unlock_page(page);
109e18c65b2SHuajun Li 	return 0;
110e18c65b2SHuajun Li }
111e18c65b2SHuajun Li 
112b3d208f9SJaegeuk Kim int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
113e18c65b2SHuajun Li {
114e18c65b2SHuajun Li 	struct f2fs_io_info fio = {
11505ca3632SJaegeuk Kim 		.sbi = F2FS_I_SB(dn->inode),
11639d787beSChao Yu 		.ino = dn->inode->i_ino,
117e18c65b2SHuajun Li 		.type = DATA,
11804d328deSMike Christie 		.op = REQ_OP_WRITE,
11970fd7614SChristoph Hellwig 		.op_flags = REQ_SYNC | REQ_PRIO,
12005ca3632SJaegeuk Kim 		.page = page,
1214375a336SJaegeuk Kim 		.encrypted_page = NULL,
122b0af6d49SChao Yu 		.io_type = FS_DATA_IO,
123e18c65b2SHuajun Li 	};
1247735730dSChao Yu 	struct node_info ni;
125158c194cSJaegeuk Kim 	int dirty, err;
126e18c65b2SHuajun Li 
127b3d208f9SJaegeuk Kim 	if (!f2fs_exist_data(dn->inode))
128b3d208f9SJaegeuk Kim 		goto clear_out;
129ec4e7af4SJaegeuk Kim 
130b3d208f9SJaegeuk Kim 	err = f2fs_reserve_block(dn, 0);
13115c6e3aaSJaegeuk Kim 	if (err)
132b3d208f9SJaegeuk Kim 		return err;
133e18c65b2SHuajun Li 
1347735730dSChao Yu 	err = f2fs_get_node_info(fio.sbi, dn->nid, &ni);
1357735730dSChao Yu 	if (err) {
136e8c82c11SChao Yu 		f2fs_truncate_data_blocks_range(dn, 1);
1377735730dSChao Yu 		f2fs_put_dnode(dn);
1387735730dSChao Yu 		return err;
1397735730dSChao Yu 	}
1407735730dSChao Yu 
1417735730dSChao Yu 	fio.version = ni.version;
1427735730dSChao Yu 
1434dbe38dcSChao Yu 	if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
1444dbe38dcSChao Yu 		f2fs_put_dnode(dn);
1454dbe38dcSChao Yu 		set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
146dcbb4c10SJoe Perches 		f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
1474dbe38dcSChao Yu 			  __func__, dn->inode->i_ino, dn->data_blkaddr);
14810f966bbSChao Yu 		return -EFSCORRUPTED;
1494dbe38dcSChao Yu 	}
1504dbe38dcSChao Yu 
15185ead818SYunlei He 	f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
152b3d208f9SJaegeuk Kim 
1534d57b86dSChao Yu 	f2fs_do_read_inline_data(page, dn->inode_page);
1546282adbfSJaegeuk Kim 	set_page_dirty(page);
1556282adbfSJaegeuk Kim 
156158c194cSJaegeuk Kim 	/* clear dirty state */
157158c194cSJaegeuk Kim 	dirty = clear_page_dirty_for_io(page);
158158c194cSJaegeuk Kim 
159e18c65b2SHuajun Li 	/* write data page to try to make data consistent */
160e18c65b2SHuajun Li 	set_page_writeback(page);
16117c50035SJaegeuk Kim 	ClearPageError(page);
1627a9d7548SChao Yu 	fio.old_blkaddr = dn->data_blkaddr;
163ef095d19SJaegeuk Kim 	set_inode_flag(dn->inode, FI_HOT_DATA);
1644d57b86dSChao Yu 	f2fs_outplace_write_data(dn, &fio);
165bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, DATA, true, true);
166933439c8SChao Yu 	if (dirty) {
167158c194cSJaegeuk Kim 		inode_dec_dirty_pages(dn->inode);
1684d57b86dSChao Yu 		f2fs_remove_dirty_inode(dn->inode);
169933439c8SChao Yu 	}
170e18c65b2SHuajun Li 
17195f5b0fcSJaegeuk Kim 	/* this converted inline_data should be recovered. */
17291942321SJaegeuk Kim 	set_inode_flag(dn->inode, FI_APPEND_WRITE);
17395f5b0fcSJaegeuk Kim 
174e18c65b2SHuajun Li 	/* clear inline data and flag after data writeback */
1754d57b86dSChao Yu 	f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
1762049d4fcSJaegeuk Kim 	clear_inline_node(dn->inode_page);
177b3d208f9SJaegeuk Kim clear_out:
178b3d208f9SJaegeuk Kim 	stat_dec_inline_inode(dn->inode);
179bd4667cbSKinglong Mee 	clear_inode_flag(dn->inode, FI_INLINE_DATA);
180b3d208f9SJaegeuk Kim 	f2fs_put_dnode(dn);
181b3d208f9SJaegeuk Kim 	return 0;
182e18c65b2SHuajun Li }
183e18c65b2SHuajun Li 
184b3d208f9SJaegeuk Kim int f2fs_convert_inline_inode(struct inode *inode)
185e18c65b2SHuajun Li {
186b3d208f9SJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
187b3d208f9SJaegeuk Kim 	struct dnode_of_data dn;
188b3d208f9SJaegeuk Kim 	struct page *ipage, *page;
189b3d208f9SJaegeuk Kim 	int err = 0;
190e18c65b2SHuajun Li 
191ec2ddf49SJaegeuk Kim 	if (!f2fs_has_inline_data(inode) ||
192ec2ddf49SJaegeuk Kim 			f2fs_hw_is_readonly(sbi) || f2fs_readonly(sbi->sb))
193b9d777b8SJaegeuk Kim 		return 0;
194b9d777b8SJaegeuk Kim 
195300e129cSJaegeuk Kim 	page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
196b3d208f9SJaegeuk Kim 	if (!page)
1979e09fc85SJaegeuk Kim 		return -ENOMEM;
198b3d208f9SJaegeuk Kim 
199b3d208f9SJaegeuk Kim 	f2fs_lock_op(sbi);
200b3d208f9SJaegeuk Kim 
2014d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, inode->i_ino);
202b3d208f9SJaegeuk Kim 	if (IS_ERR(ipage)) {
2036d20aff8SJaegeuk Kim 		err = PTR_ERR(ipage);
2046d20aff8SJaegeuk Kim 		goto out;
205b067ba1fSJaegeuk Kim 	}
206e18c65b2SHuajun Li 
207b3d208f9SJaegeuk Kim 	set_new_dnode(&dn, inode, ipage, ipage, 0);
208b3d208f9SJaegeuk Kim 
209b3d208f9SJaegeuk Kim 	if (f2fs_has_inline_data(inode))
210b3d208f9SJaegeuk Kim 		err = f2fs_convert_inline_page(&dn, page);
211b3d208f9SJaegeuk Kim 
212b3d208f9SJaegeuk Kim 	f2fs_put_dnode(&dn);
2136d20aff8SJaegeuk Kim out:
214b3d208f9SJaegeuk Kim 	f2fs_unlock_op(sbi);
215b3d208f9SJaegeuk Kim 
216b3d208f9SJaegeuk Kim 	f2fs_put_page(page, 1);
2172a340760SJaegeuk Kim 
2182c4db1a6SJaegeuk Kim 	f2fs_balance_fs(sbi, dn.node_changed);
2192a340760SJaegeuk Kim 
220e18c65b2SHuajun Li 	return err;
221e18c65b2SHuajun Li }
222e18c65b2SHuajun Li 
223b3d208f9SJaegeuk Kim int f2fs_write_inline_data(struct inode *inode, struct page *page)
224e18c65b2SHuajun Li {
225e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
226e18c65b2SHuajun Li 	struct dnode_of_data dn;
227e18c65b2SHuajun Li 	int err;
228e18c65b2SHuajun Li 
229e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, NULL, NULL, 0);
2304d57b86dSChao Yu 	err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
231e18c65b2SHuajun Li 	if (err)
232e18c65b2SHuajun Li 		return err;
233e18c65b2SHuajun Li 
234c08a690bSJaegeuk Kim 	if (!f2fs_has_inline_data(inode)) {
235b3d208f9SJaegeuk Kim 		f2fs_put_dnode(&dn);
236b3d208f9SJaegeuk Kim 		return -EAGAIN;
237c08a690bSJaegeuk Kim 	}
238c08a690bSJaegeuk Kim 
239b3d208f9SJaegeuk Kim 	f2fs_bug_on(F2FS_I_SB(inode), page->index);
240b3d208f9SJaegeuk Kim 
241bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
242f1e33a04SJaegeuk Kim 	src_addr = kmap_atomic(page);
243f2470371SChao Yu 	dst_addr = inline_data_addr(inode, dn.inode_page);
244f2470371SChao Yu 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
245f1e33a04SJaegeuk Kim 	kunmap_atomic(src_addr);
246ee6d182fSJaegeuk Kim 	set_page_dirty(dn.inode_page);
247e18c65b2SHuajun Li 
2485ec2d99dSMatthew Wilcox 	f2fs_clear_page_cache_dirty_tag(page);
2490abd8e70SDaeho Jeong 
25091942321SJaegeuk Kim 	set_inode_flag(inode, FI_APPEND_WRITE);
25191942321SJaegeuk Kim 	set_inode_flag(inode, FI_DATA_EXIST);
252b3d208f9SJaegeuk Kim 
2532049d4fcSJaegeuk Kim 	clear_inline_node(dn.inode_page);
254e18c65b2SHuajun Li 	f2fs_put_dnode(&dn);
255e18c65b2SHuajun Li 	return 0;
256e18c65b2SHuajun Li }
2571e1bb4baSJaegeuk Kim 
2589627a7b3SChao Yu int f2fs_recover_inline_data(struct inode *inode, struct page *npage)
2591e1bb4baSJaegeuk Kim {
2604081363fSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
2611e1bb4baSJaegeuk Kim 	struct f2fs_inode *ri = NULL;
2621e1bb4baSJaegeuk Kim 	void *src_addr, *dst_addr;
2631e1bb4baSJaegeuk Kim 	struct page *ipage;
2641e1bb4baSJaegeuk Kim 
2651e1bb4baSJaegeuk Kim 	/*
2661e1bb4baSJaegeuk Kim 	 * The inline_data recovery policy is as follows.
2671e1bb4baSJaegeuk Kim 	 * [prev.] [next] of inline_data flag
2681e1bb4baSJaegeuk Kim 	 *    o       o  -> recover inline_data
2691e1bb4baSJaegeuk Kim 	 *    o       x  -> remove inline_data, and then recover data blocks
2706e5ca4fcSJack Qiu 	 *    x       o  -> remove data blocks, and then recover inline_data
2711e1bb4baSJaegeuk Kim 	 *    x       x  -> recover data blocks
2721e1bb4baSJaegeuk Kim 	 */
2731e1bb4baSJaegeuk Kim 	if (IS_INODE(npage))
2741e1bb4baSJaegeuk Kim 		ri = F2FS_INODE(npage);
2751e1bb4baSJaegeuk Kim 
2761e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode) &&
2770342fd30SJaegeuk Kim 			ri && (ri->i_inline & F2FS_INLINE_DATA)) {
2781e1bb4baSJaegeuk Kim process_inline:
2794d57b86dSChao Yu 		ipage = f2fs_get_node_page(sbi, inode->i_ino);
2809627a7b3SChao Yu 		if (IS_ERR(ipage))
2819627a7b3SChao Yu 			return PTR_ERR(ipage);
2821e1bb4baSJaegeuk Kim 
283bae0ee7aSChao Yu 		f2fs_wait_on_page_writeback(ipage, NODE, true, true);
28454b591dfSJaegeuk Kim 
285f2470371SChao Yu 		src_addr = inline_data_addr(inode, npage);
286f2470371SChao Yu 		dst_addr = inline_data_addr(inode, ipage);
287f2470371SChao Yu 		memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
288b3d208f9SJaegeuk Kim 
28991942321SJaegeuk Kim 		set_inode_flag(inode, FI_INLINE_DATA);
29091942321SJaegeuk Kim 		set_inode_flag(inode, FI_DATA_EXIST);
291b3d208f9SJaegeuk Kim 
292ee6d182fSJaegeuk Kim 		set_page_dirty(ipage);
2931e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
2949627a7b3SChao Yu 		return 1;
2951e1bb4baSJaegeuk Kim 	}
2961e1bb4baSJaegeuk Kim 
2971e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode)) {
2984d57b86dSChao Yu 		ipage = f2fs_get_node_page(sbi, inode->i_ino);
2999627a7b3SChao Yu 		if (IS_ERR(ipage))
3009627a7b3SChao Yu 			return PTR_ERR(ipage);
3014d57b86dSChao Yu 		f2fs_truncate_inline_inode(inode, ipage, 0);
302*84921561SJack Qiu 		stat_dec_inline_inode(inode);
303bd4667cbSKinglong Mee 		clear_inode_flag(inode, FI_INLINE_DATA);
3041e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
3050342fd30SJaegeuk Kim 	} else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
3069627a7b3SChao Yu 		int ret;
3079627a7b3SChao Yu 
3089627a7b3SChao Yu 		ret = f2fs_truncate_blocks(inode, 0, false);
3099627a7b3SChao Yu 		if (ret)
3109627a7b3SChao Yu 			return ret;
311*84921561SJack Qiu 		stat_inc_inline_inode(inode);
3121e1bb4baSJaegeuk Kim 		goto process_inline;
3131e1bb4baSJaegeuk Kim 	}
3149627a7b3SChao Yu 	return 0;
3151e1bb4baSJaegeuk Kim }
316201a05beSChao Yu 
3174d57b86dSChao Yu struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
31843c780baSEric Biggers 					const struct f2fs_filename *fname,
31943c780baSEric Biggers 					struct page **res_page)
320201a05beSChao Yu {
321201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
322201a05beSChao Yu 	struct f2fs_dir_entry *de;
3237b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
3244e6ebf6dSJaegeuk Kim 	struct page *ipage;
325f2470371SChao Yu 	void *inline_dentry;
326201a05beSChao Yu 
3274d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
32842d96401SJaegeuk Kim 	if (IS_ERR(ipage)) {
32942d96401SJaegeuk Kim 		*res_page = ipage;
330201a05beSChao Yu 		return NULL;
33142d96401SJaegeuk Kim 	}
332201a05beSChao Yu 
333f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
334201a05beSChao Yu 
335f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
33643c780baSEric Biggers 	de = f2fs_find_target_dentry(&d, fname, NULL);
3374e6ebf6dSJaegeuk Kim 	unlock_page(ipage);
3387ad08a58SDaniel Rosenberg 	if (IS_ERR(de)) {
3397ad08a58SDaniel Rosenberg 		*res_page = ERR_CAST(de);
3407ad08a58SDaniel Rosenberg 		de = NULL;
3417ad08a58SDaniel Rosenberg 	}
3424e6ebf6dSJaegeuk Kim 	if (de)
343201a05beSChao Yu 		*res_page = ipage;
3444e6ebf6dSJaegeuk Kim 	else
3454e6ebf6dSJaegeuk Kim 		f2fs_put_page(ipage, 0);
346201a05beSChao Yu 
347201a05beSChao Yu 	return de;
348201a05beSChao Yu }
349201a05beSChao Yu 
3504d57b86dSChao Yu int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
351201a05beSChao Yu 							struct page *ipage)
352201a05beSChao Yu {
353062a3e7bSJaegeuk Kim 	struct f2fs_dentry_ptr d;
354f2470371SChao Yu 	void *inline_dentry;
355201a05beSChao Yu 
356f2470371SChao Yu 	inline_dentry = inline_data_addr(inode, ipage);
357201a05beSChao Yu 
358f2470371SChao Yu 	make_dentry_ptr_inline(inode, &d, inline_dentry);
3594d57b86dSChao Yu 	f2fs_do_make_empty_dir(inode, parent, &d);
360201a05beSChao Yu 
361201a05beSChao Yu 	set_page_dirty(ipage);
362201a05beSChao Yu 
363201a05beSChao Yu 	/* update i_size to MAX_INLINE_DATA */
364f2470371SChao Yu 	if (i_size_read(inode) < MAX_INLINE_DATA(inode))
365f2470371SChao Yu 		f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
366201a05beSChao Yu 	return 0;
367201a05beSChao Yu }
368201a05beSChao Yu 
369470f00e9SChao Yu /*
370470f00e9SChao Yu  * NOTE: ipage is grabbed by caller, but if any error occurs, we should
371470f00e9SChao Yu  * release ipage in this function.
372470f00e9SChao Yu  */
373675f10bdSChao Yu static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
374f2470371SChao Yu 							void *inline_dentry)
375201a05beSChao Yu {
376201a05beSChao Yu 	struct page *page;
377201a05beSChao Yu 	struct dnode_of_data dn;
378201a05beSChao Yu 	struct f2fs_dentry_block *dentry_blk;
37976a9dd85SChao Yu 	struct f2fs_dentry_ptr src, dst;
380201a05beSChao Yu 	int err;
381201a05beSChao Yu 
382dd973007SJaegeuk Kim 	page = f2fs_grab_cache_page(dir->i_mapping, 0, true);
383470f00e9SChao Yu 	if (!page) {
384470f00e9SChao Yu 		f2fs_put_page(ipage, 1);
385201a05beSChao Yu 		return -ENOMEM;
386470f00e9SChao Yu 	}
387201a05beSChao Yu 
388201a05beSChao Yu 	set_new_dnode(&dn, dir, ipage, NULL, 0);
389201a05beSChao Yu 	err = f2fs_reserve_block(&dn, 0);
390201a05beSChao Yu 	if (err)
391201a05beSChao Yu 		goto out;
392201a05beSChao Yu 
3934dbe38dcSChao Yu 	if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
3944dbe38dcSChao Yu 		f2fs_put_dnode(&dn);
3954dbe38dcSChao Yu 		set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
396dcbb4c10SJoe Perches 		f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
3974dbe38dcSChao Yu 			  __func__, dir->i_ino, dn.data_blkaddr);
39810f966bbSChao Yu 		err = -EFSCORRUPTED;
3994dbe38dcSChao Yu 		goto out;
4004dbe38dcSChao Yu 	}
4014dbe38dcSChao Yu 
402bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, DATA, true, true);
403201a05beSChao Yu 
404bdbc90faSYunlong Song 	dentry_blk = page_address(page);
405201a05beSChao Yu 
406f2470371SChao Yu 	make_dentry_ptr_inline(dir, &src, inline_dentry);
407f2470371SChao Yu 	make_dentry_ptr_block(dir, &dst, dentry_blk);
40876a9dd85SChao Yu 
409201a05beSChao Yu 	/* copy data from inline dentry block to new dentry block */
41076a9dd85SChao Yu 	memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
41176a9dd85SChao Yu 	memset(dst.bitmap + src.nr_bitmap, 0, dst.nr_bitmap - src.nr_bitmap);
4124ec17d68SChao Yu 	/*
4134ec17d68SChao Yu 	 * we do not need to zero out remainder part of dentry and filename
4144ec17d68SChao Yu 	 * field, since we have used bitmap for marking the usage status of
4154ec17d68SChao Yu 	 * them, besides, we can also ignore copying/zeroing reserved space
4164ec17d68SChao Yu 	 * of dentry block, because them haven't been used so far.
4174ec17d68SChao Yu 	 */
41876a9dd85SChao Yu 	memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
41976a9dd85SChao Yu 	memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
420201a05beSChao Yu 
421237c0790SJaegeuk Kim 	if (!PageUptodate(page))
422201a05beSChao Yu 		SetPageUptodate(page);
423201a05beSChao Yu 	set_page_dirty(page);
424201a05beSChao Yu 
425201a05beSChao Yu 	/* clear inline dir and flag after data writeback */
4264d57b86dSChao Yu 	f2fs_truncate_inline_inode(dir, ipage, 0);
427b3d208f9SJaegeuk Kim 
4283289c061SJaegeuk Kim 	stat_dec_inline_dir(dir);
42991942321SJaegeuk Kim 	clear_inode_flag(dir, FI_INLINE_DENTRY);
430201a05beSChao Yu 
43145a74688SChao Yu 	/*
43245a74688SChao Yu 	 * should retrieve reserved space which was used to keep
43345a74688SChao Yu 	 * inline_dentry's structure for backward compatibility.
43445a74688SChao Yu 	 */
43545a74688SChao Yu 	if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
43645a74688SChao Yu 			!f2fs_has_inline_xattr(dir))
43745a74688SChao Yu 		F2FS_I(dir)->i_inline_xattr_size = 0;
43845a74688SChao Yu 
439205b9822SJaegeuk Kim 	f2fs_i_depth_write(dir, 1);
440ee6d182fSJaegeuk Kim 	if (i_size_read(dir) < PAGE_SIZE)
441fc9581c8SJaegeuk Kim 		f2fs_i_size_write(dir, PAGE_SIZE);
442201a05beSChao Yu out:
443201a05beSChao Yu 	f2fs_put_page(page, 1);
444201a05beSChao Yu 	return err;
445201a05beSChao Yu }
446201a05beSChao Yu 
447f2470371SChao Yu static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
448675f10bdSChao Yu {
449675f10bdSChao Yu 	struct f2fs_dentry_ptr d;
450675f10bdSChao Yu 	unsigned long bit_pos = 0;
451675f10bdSChao Yu 	int err = 0;
452675f10bdSChao Yu 
453f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
454675f10bdSChao Yu 
455675f10bdSChao Yu 	while (bit_pos < d.max) {
456675f10bdSChao Yu 		struct f2fs_dir_entry *de;
45743c780baSEric Biggers 		struct f2fs_filename fname;
458675f10bdSChao Yu 		nid_t ino;
459675f10bdSChao Yu 		umode_t fake_mode;
460675f10bdSChao Yu 
461675f10bdSChao Yu 		if (!test_bit_le(bit_pos, d.bitmap)) {
462675f10bdSChao Yu 			bit_pos++;
463675f10bdSChao Yu 			continue;
464675f10bdSChao Yu 		}
465675f10bdSChao Yu 
466675f10bdSChao Yu 		de = &d.dentry[bit_pos];
467a4a13f58SChao Yu 
468a4a13f58SChao Yu 		if (unlikely(!de->name_len)) {
469a4a13f58SChao Yu 			bit_pos++;
470a4a13f58SChao Yu 			continue;
471a4a13f58SChao Yu 		}
472a4a13f58SChao Yu 
47343c780baSEric Biggers 		/*
47443c780baSEric Biggers 		 * We only need the disk_name and hash to move the dentry.
47543c780baSEric Biggers 		 * We don't need the original or casefolded filenames.
47643c780baSEric Biggers 		 */
47743c780baSEric Biggers 		memset(&fname, 0, sizeof(fname));
47843c780baSEric Biggers 		fname.disk_name.name = d.filename[bit_pos];
47943c780baSEric Biggers 		fname.disk_name.len = le16_to_cpu(de->name_len);
48043c780baSEric Biggers 		fname.hash = de->hash_code;
481675f10bdSChao Yu 
482675f10bdSChao Yu 		ino = le32_to_cpu(de->ino);
4834d57b86dSChao Yu 		fake_mode = f2fs_get_de_type(de) << S_SHIFT;
484675f10bdSChao Yu 
48543c780baSEric Biggers 		err = f2fs_add_regular_entry(dir, &fname, NULL, ino, fake_mode);
486675f10bdSChao Yu 		if (err)
487675f10bdSChao Yu 			goto punch_dentry_pages;
488675f10bdSChao Yu 
489675f10bdSChao Yu 		bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
490675f10bdSChao Yu 	}
491675f10bdSChao Yu 	return 0;
492675f10bdSChao Yu punch_dentry_pages:
493675f10bdSChao Yu 	truncate_inode_pages(&dir->i_data, 0);
494c42d28ceSChao Yu 	f2fs_truncate_blocks(dir, 0, false);
4954d57b86dSChao Yu 	f2fs_remove_dirty_inode(dir);
496675f10bdSChao Yu 	return err;
497675f10bdSChao Yu }
498675f10bdSChao Yu 
499675f10bdSChao Yu static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
500f2470371SChao Yu 							void *inline_dentry)
501675f10bdSChao Yu {
502f2470371SChao Yu 	void *backup_dentry;
503675f10bdSChao Yu 	int err;
504675f10bdSChao Yu 
5051ecc0c5cSChao Yu 	backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
506f2470371SChao Yu 				MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
5078975bdf4SChao Yu 	if (!backup_dentry) {
5088975bdf4SChao Yu 		f2fs_put_page(ipage, 1);
509675f10bdSChao Yu 		return -ENOMEM;
5108975bdf4SChao Yu 	}
511675f10bdSChao Yu 
512f2470371SChao Yu 	memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
5134d57b86dSChao Yu 	f2fs_truncate_inline_inode(dir, ipage, 0);
514675f10bdSChao Yu 
515675f10bdSChao Yu 	unlock_page(ipage);
516675f10bdSChao Yu 
517675f10bdSChao Yu 	err = f2fs_add_inline_entries(dir, backup_dentry);
518675f10bdSChao Yu 	if (err)
519675f10bdSChao Yu 		goto recover;
520675f10bdSChao Yu 
521675f10bdSChao Yu 	lock_page(ipage);
522675f10bdSChao Yu 
523675f10bdSChao Yu 	stat_dec_inline_dir(dir);
52491942321SJaegeuk Kim 	clear_inode_flag(dir, FI_INLINE_DENTRY);
52545a74688SChao Yu 
52645a74688SChao Yu 	/*
52745a74688SChao Yu 	 * should retrieve reserved space which was used to keep
52845a74688SChao Yu 	 * inline_dentry's structure for backward compatibility.
52945a74688SChao Yu 	 */
53045a74688SChao Yu 	if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
53145a74688SChao Yu 			!f2fs_has_inline_xattr(dir))
53245a74688SChao Yu 		F2FS_I(dir)->i_inline_xattr_size = 0;
53345a74688SChao Yu 
534c8eb7024SChao Yu 	kfree(backup_dentry);
535675f10bdSChao Yu 	return 0;
536675f10bdSChao Yu recover:
537675f10bdSChao Yu 	lock_page(ipage);
538bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
539f2470371SChao Yu 	memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
540205b9822SJaegeuk Kim 	f2fs_i_depth_write(dir, 0);
541f2470371SChao Yu 	f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
542ee6d182fSJaegeuk Kim 	set_page_dirty(ipage);
543675f10bdSChao Yu 	f2fs_put_page(ipage, 1);
544675f10bdSChao Yu 
545c8eb7024SChao Yu 	kfree(backup_dentry);
546675f10bdSChao Yu 	return err;
547675f10bdSChao Yu }
548675f10bdSChao Yu 
549b06af2afSJaegeuk Kim static int do_convert_inline_dir(struct inode *dir, struct page *ipage,
550f2470371SChao Yu 							void *inline_dentry)
551675f10bdSChao Yu {
552675f10bdSChao Yu 	if (!F2FS_I(dir)->i_dir_level)
553675f10bdSChao Yu 		return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
554675f10bdSChao Yu 	else
555675f10bdSChao Yu 		return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
556675f10bdSChao Yu }
557675f10bdSChao Yu 
558b06af2afSJaegeuk Kim int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry)
559b06af2afSJaegeuk Kim {
560b06af2afSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
561b06af2afSJaegeuk Kim 	struct page *ipage;
56243c780baSEric Biggers 	struct f2fs_filename fname;
563b06af2afSJaegeuk Kim 	void *inline_dentry = NULL;
564b06af2afSJaegeuk Kim 	int err = 0;
565b06af2afSJaegeuk Kim 
566b06af2afSJaegeuk Kim 	if (!f2fs_has_inline_dentry(dir))
567b06af2afSJaegeuk Kim 		return 0;
568b06af2afSJaegeuk Kim 
569b06af2afSJaegeuk Kim 	f2fs_lock_op(sbi);
570b06af2afSJaegeuk Kim 
57143c780baSEric Biggers 	err = f2fs_setup_filename(dir, &dentry->d_name, 0, &fname);
572b06af2afSJaegeuk Kim 	if (err)
573b06af2afSJaegeuk Kim 		goto out;
574b06af2afSJaegeuk Kim 
575b06af2afSJaegeuk Kim 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
576b06af2afSJaegeuk Kim 	if (IS_ERR(ipage)) {
577b06af2afSJaegeuk Kim 		err = PTR_ERR(ipage);
578ff5f85c8SEric Biggers 		goto out_fname;
579b06af2afSJaegeuk Kim 	}
580b06af2afSJaegeuk Kim 
581b06af2afSJaegeuk Kim 	if (f2fs_has_enough_room(dir, ipage, &fname)) {
582b06af2afSJaegeuk Kim 		f2fs_put_page(ipage, 1);
583ff5f85c8SEric Biggers 		goto out_fname;
584b06af2afSJaegeuk Kim 	}
585b06af2afSJaegeuk Kim 
586b06af2afSJaegeuk Kim 	inline_dentry = inline_data_addr(dir, ipage);
587b06af2afSJaegeuk Kim 
588b06af2afSJaegeuk Kim 	err = do_convert_inline_dir(dir, ipage, inline_dentry);
589b06af2afSJaegeuk Kim 	if (!err)
590b06af2afSJaegeuk Kim 		f2fs_put_page(ipage, 1);
591ff5f85c8SEric Biggers out_fname:
59243c780baSEric Biggers 	f2fs_free_filename(&fname);
593b06af2afSJaegeuk Kim out:
594b06af2afSJaegeuk Kim 	f2fs_unlock_op(sbi);
595b06af2afSJaegeuk Kim 	return err;
596b06af2afSJaegeuk Kim }
597b06af2afSJaegeuk Kim 
59843c780baSEric Biggers int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
599510022a8SJaegeuk Kim 			  struct inode *inode, nid_t ino, umode_t mode)
600201a05beSChao Yu {
601201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
602201a05beSChao Yu 	struct page *ipage;
603201a05beSChao Yu 	unsigned int bit_pos;
604f2470371SChao Yu 	void *inline_dentry = NULL;
6053b4d732aSChao Yu 	struct f2fs_dentry_ptr d;
60643c780baSEric Biggers 	int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
607510022a8SJaegeuk Kim 	struct page *page = NULL;
608201a05beSChao Yu 	int err = 0;
609201a05beSChao Yu 
6104d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
611201a05beSChao Yu 	if (IS_ERR(ipage))
612201a05beSChao Yu 		return PTR_ERR(ipage);
613201a05beSChao Yu 
614f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
615f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
61676a9dd85SChao Yu 
6174d57b86dSChao Yu 	bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
61876a9dd85SChao Yu 	if (bit_pos >= d.max) {
619b06af2afSJaegeuk Kim 		err = do_convert_inline_dir(dir, ipage, inline_dentry);
620470f00e9SChao Yu 		if (err)
621470f00e9SChao Yu 			return err;
622201a05beSChao Yu 		err = -EAGAIN;
623201a05beSChao Yu 		goto out;
624201a05beSChao Yu 	}
625201a05beSChao Yu 
626510022a8SJaegeuk Kim 	if (inode) {
627201a05beSChao Yu 		down_write(&F2FS_I(inode)->i_sem);
62843c780baSEric Biggers 		page = f2fs_init_inode_metadata(inode, dir, fname, ipage);
629201a05beSChao Yu 		if (IS_ERR(page)) {
630201a05beSChao Yu 			err = PTR_ERR(page);
631201a05beSChao Yu 			goto fail;
632201a05beSChao Yu 		}
633510022a8SJaegeuk Kim 	}
634bce8d112SJaegeuk Kim 
635bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(ipage, NODE, true, true);
6363b4d732aSChao Yu 
63743c780baSEric Biggers 	f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
63843c780baSEric Biggers 			   bit_pos);
6393b4d732aSChao Yu 
640201a05beSChao Yu 	set_page_dirty(ipage);
641201a05beSChao Yu 
642201a05beSChao Yu 	/* we don't need to mark_inode_dirty now */
643510022a8SJaegeuk Kim 	if (inode) {
644205b9822SJaegeuk Kim 		f2fs_i_pino_write(inode, dir->i_ino);
64598194030SChao Yu 
64698194030SChao Yu 		/* synchronize inode page's data from inode cache */
64798194030SChao Yu 		if (is_inode_flag_set(inode, FI_NEW_INODE))
64898194030SChao Yu 			f2fs_update_inode(inode, page);
64998194030SChao Yu 
650201a05beSChao Yu 		f2fs_put_page(page, 1);
651510022a8SJaegeuk Kim 	}
652201a05beSChao Yu 
6534d57b86dSChao Yu 	f2fs_update_parent_metadata(dir, inode, 0);
654201a05beSChao Yu fail:
655510022a8SJaegeuk Kim 	if (inode)
656201a05beSChao Yu 		up_write(&F2FS_I(inode)->i_sem);
657201a05beSChao Yu out:
658201a05beSChao Yu 	f2fs_put_page(ipage, 1);
659201a05beSChao Yu 	return err;
660201a05beSChao Yu }
661201a05beSChao Yu 
662201a05beSChao Yu void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
663201a05beSChao Yu 					struct inode *dir, struct inode *inode)
664201a05beSChao Yu {
66576a9dd85SChao Yu 	struct f2fs_dentry_ptr d;
666f2470371SChao Yu 	void *inline_dentry;
667201a05beSChao Yu 	int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
668201a05beSChao Yu 	unsigned int bit_pos;
669201a05beSChao Yu 	int i;
670201a05beSChao Yu 
671201a05beSChao Yu 	lock_page(page);
672bae0ee7aSChao Yu 	f2fs_wait_on_page_writeback(page, NODE, true, true);
673201a05beSChao Yu 
674f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, page);
675f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
67676a9dd85SChao Yu 
67776a9dd85SChao Yu 	bit_pos = dentry - d.dentry;
678201a05beSChao Yu 	for (i = 0; i < slots; i++)
67976a9dd85SChao Yu 		__clear_bit_le(bit_pos + i, d.bitmap);
680201a05beSChao Yu 
681201a05beSChao Yu 	set_page_dirty(page);
6829f7c45ccSJaegeuk Kim 	f2fs_put_page(page, 1);
683201a05beSChao Yu 
684078cd827SDeepa Dinamani 	dir->i_ctime = dir->i_mtime = current_time(dir);
6857c45729aSJaegeuk Kim 	f2fs_mark_inode_dirty_sync(dir, false);
686201a05beSChao Yu 
687201a05beSChao Yu 	if (inode)
6889f7c45ccSJaegeuk Kim 		f2fs_drop_nlink(dir, inode);
689201a05beSChao Yu }
690201a05beSChao Yu 
691201a05beSChao Yu bool f2fs_empty_inline_dir(struct inode *dir)
692201a05beSChao Yu {
693201a05beSChao Yu 	struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
694201a05beSChao Yu 	struct page *ipage;
695201a05beSChao Yu 	unsigned int bit_pos = 2;
696f2470371SChao Yu 	void *inline_dentry;
69776a9dd85SChao Yu 	struct f2fs_dentry_ptr d;
698201a05beSChao Yu 
6994d57b86dSChao Yu 	ipage = f2fs_get_node_page(sbi, dir->i_ino);
700201a05beSChao Yu 	if (IS_ERR(ipage))
701201a05beSChao Yu 		return false;
702201a05beSChao Yu 
703f2470371SChao Yu 	inline_dentry = inline_data_addr(dir, ipage);
704f2470371SChao Yu 	make_dentry_ptr_inline(dir, &d, inline_dentry);
70576a9dd85SChao Yu 
70676a9dd85SChao Yu 	bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
707201a05beSChao Yu 
708201a05beSChao Yu 	f2fs_put_page(ipage, 1);
709201a05beSChao Yu 
71076a9dd85SChao Yu 	if (bit_pos < d.max)
711201a05beSChao Yu 		return false;
712201a05beSChao Yu 
713201a05beSChao Yu 	return true;
714201a05beSChao Yu }
715201a05beSChao Yu 
716d8c6822aSJaegeuk Kim int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
7170b81d077SJaegeuk Kim 				struct fscrypt_str *fstr)
718201a05beSChao Yu {
719201a05beSChao Yu 	struct inode *inode = file_inode(file);
720201a05beSChao Yu 	struct page *ipage = NULL;
7217b3cd7d6SJaegeuk Kim 	struct f2fs_dentry_ptr d;
722f2470371SChao Yu 	void *inline_dentry = NULL;
723ed6bd4b1SChao Yu 	int err;
724201a05beSChao Yu 
72576a9dd85SChao Yu 	make_dentry_ptr_inline(inode, &d, inline_dentry);
72676a9dd85SChao Yu 
72776a9dd85SChao Yu 	if (ctx->pos == d.max)
728201a05beSChao Yu 		return 0;
729201a05beSChao Yu 
7304d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
731201a05beSChao Yu 	if (IS_ERR(ipage))
732201a05beSChao Yu 		return PTR_ERR(ipage);
733201a05beSChao Yu 
734aadcef64SChao Yu 	/*
735aadcef64SChao Yu 	 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
736aadcef64SChao Yu 	 * ipage without page's lock held.
737aadcef64SChao Yu 	 */
738aadcef64SChao Yu 	unlock_page(ipage);
739aadcef64SChao Yu 
740f2470371SChao Yu 	inline_dentry = inline_data_addr(inode, ipage);
741201a05beSChao Yu 
74264c24ecbSTomohiro Kusumi 	make_dentry_ptr_inline(inode, &d, inline_dentry);
7437b3cd7d6SJaegeuk Kim 
744ed6bd4b1SChao Yu 	err = f2fs_fill_dentries(ctx, &d, 0, fstr);
745ed6bd4b1SChao Yu 	if (!err)
74676a9dd85SChao Yu 		ctx->pos = d.max;
747201a05beSChao Yu 
748aadcef64SChao Yu 	f2fs_put_page(ipage, 0);
749ed6bd4b1SChao Yu 	return err < 0 ? err : 0;
750201a05beSChao Yu }
75167f8cf3cSJaegeuk Kim 
75267f8cf3cSJaegeuk Kim int f2fs_inline_data_fiemap(struct inode *inode,
75367f8cf3cSJaegeuk Kim 		struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
75467f8cf3cSJaegeuk Kim {
75567f8cf3cSJaegeuk Kim 	__u64 byteaddr, ilen;
75667f8cf3cSJaegeuk Kim 	__u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
75767f8cf3cSJaegeuk Kim 		FIEMAP_EXTENT_LAST;
75867f8cf3cSJaegeuk Kim 	struct node_info ni;
75967f8cf3cSJaegeuk Kim 	struct page *ipage;
76067f8cf3cSJaegeuk Kim 	int err = 0;
76167f8cf3cSJaegeuk Kim 
7624d57b86dSChao Yu 	ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
76367f8cf3cSJaegeuk Kim 	if (IS_ERR(ipage))
76467f8cf3cSJaegeuk Kim 		return PTR_ERR(ipage);
76567f8cf3cSJaegeuk Kim 
7667975f349SChao Yu 	if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
7677975f349SChao Yu 				!f2fs_has_inline_data(inode)) {
7687975f349SChao Yu 		err = -EAGAIN;
7697975f349SChao Yu 		goto out;
7707975f349SChao Yu 	}
7717975f349SChao Yu 
7727975f349SChao Yu 	if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
77367f8cf3cSJaegeuk Kim 		err = -EAGAIN;
77467f8cf3cSJaegeuk Kim 		goto out;
77567f8cf3cSJaegeuk Kim 	}
77667f8cf3cSJaegeuk Kim 
777f2470371SChao Yu 	ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
77867f8cf3cSJaegeuk Kim 	if (start >= ilen)
77967f8cf3cSJaegeuk Kim 		goto out;
78067f8cf3cSJaegeuk Kim 	if (start + len < ilen)
78167f8cf3cSJaegeuk Kim 		ilen = start + len;
78267f8cf3cSJaegeuk Kim 	ilen -= start;
78367f8cf3cSJaegeuk Kim 
7847735730dSChao Yu 	err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
7857735730dSChao Yu 	if (err)
7867735730dSChao Yu 		goto out;
7877735730dSChao Yu 
78867f8cf3cSJaegeuk Kim 	byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
789f2470371SChao Yu 	byteaddr += (char *)inline_data_addr(inode, ipage) -
790f2470371SChao Yu 					(char *)F2FS_INODE(ipage);
79167f8cf3cSJaegeuk Kim 	err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
792dd5a09bdSChao Yu 	trace_f2fs_fiemap(inode, start, byteaddr, ilen, flags, err);
79367f8cf3cSJaegeuk Kim out:
79467f8cf3cSJaegeuk Kim 	f2fs_put_page(ipage, 1);
79567f8cf3cSJaegeuk Kim 	return err;
79667f8cf3cSJaegeuk Kim }
797