xref: /openbmc/linux/fs/f2fs/inline.c (revision 9e09fc855dd6f6ed510b3db7f3c3c1dd73631ac7)
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 inline int f2fs_has_inline_data(struct inode *inode)
17e18c65b2SHuajun Li {
18e18c65b2SHuajun Li 	return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
19e18c65b2SHuajun Li }
20e18c65b2SHuajun Li 
21e18c65b2SHuajun Li bool f2fs_may_inline(struct inode *inode)
22e18c65b2SHuajun Li {
23e18c65b2SHuajun Li 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
24e18c65b2SHuajun Li 	block_t nr_blocks;
25e18c65b2SHuajun Li 	loff_t i_size;
26e18c65b2SHuajun Li 
27e18c65b2SHuajun Li 	if (!test_opt(sbi, INLINE_DATA))
28e18c65b2SHuajun Li 		return false;
29e18c65b2SHuajun Li 
30e18c65b2SHuajun Li 	nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2;
31e18c65b2SHuajun Li 	if (inode->i_blocks > nr_blocks)
32e18c65b2SHuajun Li 		return false;
33e18c65b2SHuajun Li 
34e18c65b2SHuajun Li 	i_size = i_size_read(inode);
35e18c65b2SHuajun Li 	if (i_size > MAX_INLINE_DATA)
36e18c65b2SHuajun Li 		return false;
37e18c65b2SHuajun Li 
38e18c65b2SHuajun Li 	return true;
39e18c65b2SHuajun Li }
40e18c65b2SHuajun Li 
41e18c65b2SHuajun Li int f2fs_read_inline_data(struct inode *inode, struct page *page)
42e18c65b2SHuajun Li {
43e18c65b2SHuajun Li 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
44e18c65b2SHuajun Li 	struct page *ipage;
45e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
46e18c65b2SHuajun Li 
47e18c65b2SHuajun Li 	ipage = get_node_page(sbi, inode->i_ino);
48e18c65b2SHuajun Li 	if (IS_ERR(ipage))
49e18c65b2SHuajun Li 		return PTR_ERR(ipage);
50e18c65b2SHuajun Li 
51e18c65b2SHuajun Li 	zero_user_segment(page, INLINE_DATA_OFFSET,
52e18c65b2SHuajun Li 				INLINE_DATA_OFFSET + MAX_INLINE_DATA);
53e18c65b2SHuajun Li 
54e18c65b2SHuajun Li 	/* Copy the whole inline data block */
55e18c65b2SHuajun Li 	src_addr = inline_data_addr(ipage);
56e18c65b2SHuajun Li 	dst_addr = kmap(page);
57e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
58e18c65b2SHuajun Li 	kunmap(page);
59e18c65b2SHuajun Li 	f2fs_put_page(ipage, 1);
60e18c65b2SHuajun Li 
61e18c65b2SHuajun Li 	SetPageUptodate(page);
62e18c65b2SHuajun Li 	unlock_page(page);
63e18c65b2SHuajun Li 
64e18c65b2SHuajun Li 	return 0;
65e18c65b2SHuajun Li }
66e18c65b2SHuajun Li 
67e18c65b2SHuajun Li static int __f2fs_convert_inline_data(struct inode *inode, struct page *page)
68e18c65b2SHuajun Li {
69e18c65b2SHuajun Li 	int err;
70e18c65b2SHuajun Li 	struct page *ipage;
71e18c65b2SHuajun Li 	struct dnode_of_data dn;
72e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
73e18c65b2SHuajun Li 	block_t new_blk_addr;
74e18c65b2SHuajun Li 	struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
75e18c65b2SHuajun Li 	struct f2fs_io_info fio = {
76e18c65b2SHuajun Li 		.type = DATA,
77e18c65b2SHuajun Li 		.rw = WRITE_SYNC | REQ_PRIO,
78e18c65b2SHuajun Li 	};
79e18c65b2SHuajun Li 
80e18c65b2SHuajun Li 	f2fs_lock_op(sbi);
81e18c65b2SHuajun Li 	ipage = get_node_page(sbi, inode->i_ino);
82e18c65b2SHuajun Li 	if (IS_ERR(ipage))
83e18c65b2SHuajun Li 		return PTR_ERR(ipage);
84e18c65b2SHuajun Li 
85e18c65b2SHuajun Li 	/*
86e18c65b2SHuajun Li 	 * i_addr[0] is not used for inline data,
87e18c65b2SHuajun Li 	 * so reserving new block will not destroy inline data
88e18c65b2SHuajun Li 	 */
89e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, ipage, ipage, 0);
90e18c65b2SHuajun Li 	err = f2fs_reserve_block(&dn, 0);
91e18c65b2SHuajun Li 	if (err) {
92e18c65b2SHuajun Li 		f2fs_put_page(ipage, 1);
93e18c65b2SHuajun Li 		f2fs_unlock_op(sbi);
94e18c65b2SHuajun Li 		return err;
95e18c65b2SHuajun Li 	}
96e18c65b2SHuajun Li 
97e18c65b2SHuajun Li 	zero_user_segment(page, 0, PAGE_CACHE_SIZE);
98e18c65b2SHuajun Li 
99e18c65b2SHuajun Li 	/* Copy the whole inline data block */
100e18c65b2SHuajun Li 	src_addr = inline_data_addr(ipage);
101e18c65b2SHuajun Li 	dst_addr = kmap(page);
102e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
103e18c65b2SHuajun Li 	kunmap(page);
104*9e09fc85SJaegeuk Kim 	SetPageUptodate(page);
105e18c65b2SHuajun Li 
106e18c65b2SHuajun Li 	/* write data page to try to make data consistent */
107e18c65b2SHuajun Li 	set_page_writeback(page);
108e18c65b2SHuajun Li 	write_data_page(page, &dn, &new_blk_addr, &fio);
109e18c65b2SHuajun Li 	update_extent_cache(new_blk_addr, &dn);
110e18c65b2SHuajun Li 	f2fs_wait_on_page_writeback(page, DATA, true);
111e18c65b2SHuajun Li 
112e18c65b2SHuajun Li 	/* clear inline data and flag after data writeback */
113e18c65b2SHuajun Li 	zero_user_segment(ipage, INLINE_DATA_OFFSET,
114e18c65b2SHuajun Li 				 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
115e18c65b2SHuajun Li 	clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
116e18c65b2SHuajun Li 
117e18c65b2SHuajun Li 	sync_inode_page(&dn);
118e18c65b2SHuajun Li 	f2fs_put_page(ipage, 1);
119e18c65b2SHuajun Li 	f2fs_unlock_op(sbi);
120e18c65b2SHuajun Li 
121e18c65b2SHuajun Li 	return err;
122e18c65b2SHuajun Li }
123e18c65b2SHuajun Li 
124*9e09fc85SJaegeuk Kim int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size)
125e18c65b2SHuajun Li {
126e18c65b2SHuajun Li 	struct page *page;
127*9e09fc85SJaegeuk Kim 	int err;
128e18c65b2SHuajun Li 
129*9e09fc85SJaegeuk Kim 	if (!f2fs_has_inline_data(inode))
130*9e09fc85SJaegeuk Kim 		return 0;
131*9e09fc85SJaegeuk Kim 	else if (to_size <= MAX_INLINE_DATA)
132*9e09fc85SJaegeuk Kim 		return 0;
133*9e09fc85SJaegeuk Kim 
134*9e09fc85SJaegeuk Kim 	page = grab_cache_page_write_begin(inode->i_mapping, 0, AOP_FLAG_NOFS);
135*9e09fc85SJaegeuk Kim 	if (!page)
136*9e09fc85SJaegeuk Kim 		return -ENOMEM;
137e18c65b2SHuajun Li 
138e18c65b2SHuajun Li 	err = __f2fs_convert_inline_data(inode, page);
139e18c65b2SHuajun Li 	f2fs_put_page(page, 1);
140e18c65b2SHuajun Li 	return err;
141e18c65b2SHuajun Li }
142e18c65b2SHuajun Li 
143e18c65b2SHuajun Li int f2fs_write_inline_data(struct inode *inode,
144e18c65b2SHuajun Li 			   struct page *page, unsigned size)
145e18c65b2SHuajun Li {
146e18c65b2SHuajun Li 	void *src_addr, *dst_addr;
147e18c65b2SHuajun Li 	struct page *ipage;
148e18c65b2SHuajun Li 	struct dnode_of_data dn;
149e18c65b2SHuajun Li 	int err;
150e18c65b2SHuajun Li 
151e18c65b2SHuajun Li 	set_new_dnode(&dn, inode, NULL, NULL, 0);
152e18c65b2SHuajun Li 	err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
153e18c65b2SHuajun Li 	if (err)
154e18c65b2SHuajun Li 		return err;
155e18c65b2SHuajun Li 	ipage = dn.inode_page;
156e18c65b2SHuajun Li 
157e18c65b2SHuajun Li 	zero_user_segment(ipage, INLINE_DATA_OFFSET,
158e18c65b2SHuajun Li 				 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
159e18c65b2SHuajun Li 	src_addr = kmap(page);
160e18c65b2SHuajun Li 	dst_addr = inline_data_addr(ipage);
161e18c65b2SHuajun Li 	memcpy(dst_addr, src_addr, size);
162e18c65b2SHuajun Li 	kunmap(page);
163e18c65b2SHuajun Li 
164e18c65b2SHuajun Li 	/* Release the first data block if it is allocated */
165e18c65b2SHuajun Li 	if (!f2fs_has_inline_data(inode)) {
166e18c65b2SHuajun Li 		truncate_data_blocks_range(&dn, 1);
167e18c65b2SHuajun Li 		set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
168e18c65b2SHuajun Li 	}
169e18c65b2SHuajun Li 
170e18c65b2SHuajun Li 	sync_inode_page(&dn);
171e18c65b2SHuajun Li 	f2fs_put_dnode(&dn);
172e18c65b2SHuajun Li 
173e18c65b2SHuajun Li 	return 0;
174e18c65b2SHuajun Li }
175