xref: /openbmc/linux/fs/f2fs/inline.c (revision ec4e7af4ca04ff81f786554d670876f1037a9ded)
1e18c65b2SHuajun Li /*
2e18c65b2SHuajun Li  * fs/f2fs/inline.c
3e18c65b2SHuajun Li  * Copyright (c) 2013, Intel Corporation
4e18c65b2SHuajun Li  * Authors: Huajun Li <huajun.li@intel.com>
5e18c65b2SHuajun Li  *          Haicheng Li <haicheng.li@intel.com>
6e18c65b2SHuajun Li  * This program is free software; you can redistribute it and/or modify
7e18c65b2SHuajun Li  * it under the terms of the GNU General Public License version 2 as
8e18c65b2SHuajun Li  * published by the Free Software Foundation.
9e18c65b2SHuajun Li  */
10e18c65b2SHuajun Li 
11e18c65b2SHuajun Li #include <linux/fs.h>
12e18c65b2SHuajun Li #include <linux/f2fs_fs.h>
13e18c65b2SHuajun Li 
14e18c65b2SHuajun Li #include "f2fs.h"
15e18c65b2SHuajun Li 
16e18c65b2SHuajun Li bool f2fs_may_inline(struct inode *inode)
17e18c65b2SHuajun Li {
18e18c65b2SHuajun Li 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
19e18c65b2SHuajun Li 	block_t nr_blocks;
20e18c65b2SHuajun Li 	loff_t i_size;
21e18c65b2SHuajun Li 
22e18c65b2SHuajun Li 	if (!test_opt(sbi, INLINE_DATA))
23e18c65b2SHuajun Li 		return false;
24e18c65b2SHuajun Li 
25e18c65b2SHuajun Li 	nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2;
26e18c65b2SHuajun Li 	if (inode->i_blocks > nr_blocks)
27e18c65b2SHuajun Li 		return false;
28e18c65b2SHuajun Li 
29e18c65b2SHuajun Li 	i_size = i_size_read(inode);
30e18c65b2SHuajun Li 	if (i_size > MAX_INLINE_DATA)
31e18c65b2SHuajun Li 		return false;
32e18c65b2SHuajun Li 
33e18c65b2SHuajun Li 	return true;
34e18c65b2SHuajun Li }
35e18c65b2SHuajun Li 
36e18c65b2SHuajun Li int f2fs_read_inline_data(struct inode *inode, struct page *page)
37e18c65b2SHuajun Li {
38e18c65b2SHuajun Li 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
39e18c65b2SHuajun Li 	struct page *ipage;
40e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
41e18c65b2SHuajun Li 
4204a17fb1SChao Yu 	if (page->index) {
4304a17fb1SChao Yu 		zero_user_segment(page, 0, PAGE_CACHE_SIZE);
4404a17fb1SChao Yu 		goto out;
4504a17fb1SChao Yu 	}
4604a17fb1SChao Yu 
47e18c65b2SHuajun Li 	ipage = get_node_page(sbi, inode->i_ino);
48d54c795bSChao Yu 	if (IS_ERR(ipage)) {
49d54c795bSChao Yu 		unlock_page(page);
50e18c65b2SHuajun Li 		return PTR_ERR(ipage);
51d54c795bSChao Yu 	}
52e18c65b2SHuajun Li 
5318309aaaSChao Yu 	zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
54e18c65b2SHuajun Li 
55e18c65b2SHuajun Li 	/* Copy the whole inline data block */
56e18c65b2SHuajun Li 	src_addr = inline_data_addr(ipage);
57e18c65b2SHuajun Li 	dst_addr = kmap(page);
58e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
59e18c65b2SHuajun Li 	kunmap(page);
60e18c65b2SHuajun Li 	f2fs_put_page(ipage, 1);
61e18c65b2SHuajun Li 
6204a17fb1SChao Yu out:
63e18c65b2SHuajun Li 	SetPageUptodate(page);
64e18c65b2SHuajun Li 	unlock_page(page);
65e18c65b2SHuajun Li 
66e18c65b2SHuajun Li 	return 0;
67e18c65b2SHuajun Li }
68e18c65b2SHuajun Li 
69e18c65b2SHuajun Li static int __f2fs_convert_inline_data(struct inode *inode, struct page *page)
70e18c65b2SHuajun Li {
71*ec4e7af4SJaegeuk Kim 	int err = 0;
72e18c65b2SHuajun Li 	struct page *ipage;
73e18c65b2SHuajun Li 	struct dnode_of_data dn;
74e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
75e18c65b2SHuajun Li 	block_t new_blk_addr;
76e18c65b2SHuajun Li 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
77e18c65b2SHuajun Li 	struct f2fs_io_info fio = {
78e18c65b2SHuajun Li 		.type = DATA,
79e18c65b2SHuajun Li 		.rw = WRITE_SYNC | REQ_PRIO,
80e18c65b2SHuajun Li 	};
81e18c65b2SHuajun Li 
82e18c65b2SHuajun Li 	f2fs_lock_op(sbi);
83e18c65b2SHuajun Li 	ipage = get_node_page(sbi, inode->i_ino);
8415c6e3aaSJaegeuk Kim 	if (IS_ERR(ipage)) {
8515c6e3aaSJaegeuk Kim 		err = PTR_ERR(ipage);
8615c6e3aaSJaegeuk Kim 		goto out;
8715c6e3aaSJaegeuk Kim 	}
88e18c65b2SHuajun Li 
89*ec4e7af4SJaegeuk Kim 	/* someone else converted inline_data already */
90*ec4e7af4SJaegeuk Kim 	if (!f2fs_has_inline_data(inode))
91*ec4e7af4SJaegeuk Kim 		goto out;
92*ec4e7af4SJaegeuk Kim 
93e18c65b2SHuajun Li 	/*
94e18c65b2SHuajun Li 	 * i_addr[0] is not used for inline data,
95e18c65b2SHuajun Li 	 * so reserving new block will not destroy inline data
96e18c65b2SHuajun Li 	 */
97a8865372SJaegeuk Kim 	set_new_dnode(&dn, inode, ipage, NULL, 0);
98e18c65b2SHuajun Li 	err = f2fs_reserve_block(&dn, 0);
9915c6e3aaSJaegeuk Kim 	if (err)
10015c6e3aaSJaegeuk Kim 		goto out;
101e18c65b2SHuajun Li 
1029ac1349aSJaegeuk Kim 	f2fs_wait_on_page_writeback(page, DATA);
10318309aaaSChao Yu 	zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
104e18c65b2SHuajun Li 
105e18c65b2SHuajun Li 	/* Copy the whole inline data block */
106e18c65b2SHuajun Li 	src_addr = inline_data_addr(ipage);
107e18c65b2SHuajun Li 	dst_addr = kmap(page);
108e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
109e18c65b2SHuajun Li 	kunmap(page);
1109e09fc85SJaegeuk Kim 	SetPageUptodate(page);
111e18c65b2SHuajun Li 
112e18c65b2SHuajun Li 	/* write data page to try to make data consistent */
113e18c65b2SHuajun Li 	set_page_writeback(page);
114e18c65b2SHuajun Li 	write_data_page(page, &dn, &new_blk_addr, &fio);
115e18c65b2SHuajun Li 	update_extent_cache(new_blk_addr, &dn);
1165514f0aaSYuan Zhong 	f2fs_wait_on_page_writeback(page, DATA);
117e18c65b2SHuajun Li 
118e18c65b2SHuajun Li 	/* clear inline data and flag after data writeback */
119e18c65b2SHuajun Li 	zero_user_segment(ipage, INLINE_DATA_OFFSET,
120e18c65b2SHuajun Li 				 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
121e18c65b2SHuajun Li 	clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
1220dbdc2aeSJaegeuk Kim 	stat_dec_inline_inode(inode);
123e18c65b2SHuajun Li 
124e18c65b2SHuajun Li 	sync_inode_page(&dn);
125a8865372SJaegeuk Kim 	f2fs_put_dnode(&dn);
12615c6e3aaSJaegeuk Kim out:
127e18c65b2SHuajun Li 	f2fs_unlock_op(sbi);
128e18c65b2SHuajun Li 	return err;
129e18c65b2SHuajun Li }
130e18c65b2SHuajun Li 
131b067ba1fSJaegeuk Kim int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size,
132b067ba1fSJaegeuk Kim 						struct page *page)
133e18c65b2SHuajun Li {
134b067ba1fSJaegeuk Kim 	struct page *new_page = page;
1359e09fc85SJaegeuk Kim 	int err;
136e18c65b2SHuajun Li 
1379e09fc85SJaegeuk Kim 	if (!f2fs_has_inline_data(inode))
1389e09fc85SJaegeuk Kim 		return 0;
1399e09fc85SJaegeuk Kim 	else if (to_size <= MAX_INLINE_DATA)
1409e09fc85SJaegeuk Kim 		return 0;
1419e09fc85SJaegeuk Kim 
142b067ba1fSJaegeuk Kim 	if (!page || page->index != 0) {
143b067ba1fSJaegeuk Kim 		new_page = grab_cache_page(inode->i_mapping, 0);
144b067ba1fSJaegeuk Kim 		if (!new_page)
1459e09fc85SJaegeuk Kim 			return -ENOMEM;
146b067ba1fSJaegeuk Kim 	}
147e18c65b2SHuajun Li 
148b067ba1fSJaegeuk Kim 	err = __f2fs_convert_inline_data(inode, new_page);
149b067ba1fSJaegeuk Kim 	if (!page || page->index != 0)
150b067ba1fSJaegeuk Kim 		f2fs_put_page(new_page, 1);
151e18c65b2SHuajun Li 	return err;
152e18c65b2SHuajun Li }
153e18c65b2SHuajun Li 
154e18c65b2SHuajun Li int f2fs_write_inline_data(struct inode *inode,
155e18c65b2SHuajun Li 				struct page *page, unsigned size)
156e18c65b2SHuajun Li {
157e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
158e18c65b2SHuajun Li 	struct page *ipage;
159e18c65b2SHuajun Li 	struct dnode_of_data dn;
160e18c65b2SHuajun Li 	int err;
161e18c65b2SHuajun Li 
162e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, NULL, NULL, 0);
163e18c65b2SHuajun Li 	err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
164e18c65b2SHuajun Li 	if (err)
165e18c65b2SHuajun Li 		return err;
166e18c65b2SHuajun Li 	ipage = dn.inode_page;
167e18c65b2SHuajun Li 
16854b591dfSJaegeuk Kim 	f2fs_wait_on_page_writeback(ipage, NODE);
169e18c65b2SHuajun Li 	zero_user_segment(ipage, INLINE_DATA_OFFSET,
170e18c65b2SHuajun Li 				 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
171e18c65b2SHuajun Li 	src_addr = kmap(page);
172e18c65b2SHuajun Li 	dst_addr = inline_data_addr(ipage);
173e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, size);
174e18c65b2SHuajun Li 	kunmap(page);
175e18c65b2SHuajun Li 
176e18c65b2SHuajun Li 	/* Release the first data block if it is allocated */
177e18c65b2SHuajun Li 	if (!f2fs_has_inline_data(inode)) {
178e18c65b2SHuajun Li 		truncate_data_blocks_range(&dn, 1);
179e18c65b2SHuajun Li 		set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
1800dbdc2aeSJaegeuk Kim 		stat_inc_inline_inode(inode);
181e18c65b2SHuajun Li 	}
182e18c65b2SHuajun Li 
183fff04f90SJaegeuk Kim 	set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
184e18c65b2SHuajun Li 	sync_inode_page(&dn);
185e18c65b2SHuajun Li 	f2fs_put_dnode(&dn);
186e18c65b2SHuajun Li 
187e18c65b2SHuajun Li 	return 0;
188e18c65b2SHuajun Li }
1891e1bb4baSJaegeuk Kim 
1908aa6f1c5SChao Yu void truncate_inline_data(struct inode *inode, u64 from)
1918aa6f1c5SChao Yu {
1928aa6f1c5SChao Yu 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
1938aa6f1c5SChao Yu 	struct page *ipage;
1948aa6f1c5SChao Yu 
1958aa6f1c5SChao Yu 	if (from >= MAX_INLINE_DATA)
1968aa6f1c5SChao Yu 		return;
1978aa6f1c5SChao Yu 
1988aa6f1c5SChao Yu 	ipage = get_node_page(sbi, inode->i_ino);
1998aa6f1c5SChao Yu 	if (IS_ERR(ipage))
2008aa6f1c5SChao Yu 		return;
2018aa6f1c5SChao Yu 
20254b591dfSJaegeuk Kim 	f2fs_wait_on_page_writeback(ipage, NODE);
20354b591dfSJaegeuk Kim 
2048aa6f1c5SChao Yu 	zero_user_segment(ipage, INLINE_DATA_OFFSET + from,
2058aa6f1c5SChao Yu 				INLINE_DATA_OFFSET + MAX_INLINE_DATA);
2068aa6f1c5SChao Yu 	set_page_dirty(ipage);
2078aa6f1c5SChao Yu 	f2fs_put_page(ipage, 1);
2088aa6f1c5SChao Yu }
2098aa6f1c5SChao Yu 
2100342fd30SJaegeuk Kim bool recover_inline_data(struct inode *inode, struct page *npage)
2111e1bb4baSJaegeuk Kim {
2121e1bb4baSJaegeuk Kim 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
2131e1bb4baSJaegeuk Kim 	struct f2fs_inode *ri = NULL;
2141e1bb4baSJaegeuk Kim 	void *src_addr, *dst_addr;
2151e1bb4baSJaegeuk Kim 	struct page *ipage;
2161e1bb4baSJaegeuk Kim 
2171e1bb4baSJaegeuk Kim 	/*
2181e1bb4baSJaegeuk Kim 	 * The inline_data recovery policy is as follows.
2191e1bb4baSJaegeuk Kim 	 * [prev.] [next] of inline_data flag
2201e1bb4baSJaegeuk Kim 	 *    o       o  -> recover inline_data
2211e1bb4baSJaegeuk Kim 	 *    o       x  -> remove inline_data, and then recover data blocks
2221e1bb4baSJaegeuk Kim 	 *    x       o  -> remove inline_data, and then recover inline_data
2231e1bb4baSJaegeuk Kim 	 *    x       x  -> recover data blocks
2241e1bb4baSJaegeuk Kim 	 */
2251e1bb4baSJaegeuk Kim 	if (IS_INODE(npage))
2261e1bb4baSJaegeuk Kim 		ri = F2FS_INODE(npage);
2271e1bb4baSJaegeuk Kim 
2281e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode) &&
2290342fd30SJaegeuk Kim 			ri && (ri->i_inline & F2FS_INLINE_DATA)) {
2301e1bb4baSJaegeuk Kim process_inline:
2311e1bb4baSJaegeuk Kim 		ipage = get_node_page(sbi, inode->i_ino);
2321e1bb4baSJaegeuk Kim 		f2fs_bug_on(IS_ERR(ipage));
2331e1bb4baSJaegeuk Kim 
23454b591dfSJaegeuk Kim 		f2fs_wait_on_page_writeback(ipage, NODE);
23554b591dfSJaegeuk Kim 
2361e1bb4baSJaegeuk Kim 		src_addr = inline_data_addr(npage);
2371e1bb4baSJaegeuk Kim 		dst_addr = inline_data_addr(ipage);
2381e1bb4baSJaegeuk Kim 		memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
2391e1bb4baSJaegeuk Kim 		update_inode(inode, ipage);
2401e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
2410342fd30SJaegeuk Kim 		return true;
2421e1bb4baSJaegeuk Kim 	}
2431e1bb4baSJaegeuk Kim 
2441e1bb4baSJaegeuk Kim 	if (f2fs_has_inline_data(inode)) {
2451e1bb4baSJaegeuk Kim 		ipage = get_node_page(sbi, inode->i_ino);
2461e1bb4baSJaegeuk Kim 		f2fs_bug_on(IS_ERR(ipage));
24754b591dfSJaegeuk Kim 		f2fs_wait_on_page_writeback(ipage, NODE);
2481e1bb4baSJaegeuk Kim 		zero_user_segment(ipage, INLINE_DATA_OFFSET,
2491e1bb4baSJaegeuk Kim 				 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
2501e1bb4baSJaegeuk Kim 		clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
2511e1bb4baSJaegeuk Kim 		update_inode(inode, ipage);
2521e1bb4baSJaegeuk Kim 		f2fs_put_page(ipage, 1);
2530342fd30SJaegeuk Kim 	} else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
254764aa3e9SJaegeuk Kim 		truncate_blocks(inode, 0, false);
2551e1bb4baSJaegeuk Kim 		set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
2561e1bb4baSJaegeuk Kim 		goto process_inline;
2571e1bb4baSJaegeuk Kim 	}
2580342fd30SJaegeuk Kim 	return false;
2591e1bb4baSJaegeuk Kim }
260