3 * Copyright (c) 2013, Intel Corporation
4 * Authors: Huajun Li <huajun.li@intel.com>
5 * Haicheng Li <haicheng.li@intel.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
16 bool f2fs_may_inline(struct inode *inode)
21 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
24 nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2;
25 if (inode->i_blocks > nr_blocks)
28 i_size = i_size_read(inode);
29 if (i_size > MAX_INLINE_DATA)
35 int f2fs_read_inline_data(struct inode *inode, struct page *page)
38 void *src_addr, *dst_addr;
41 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
45 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
48 return PTR_ERR(ipage);
51 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
53 /* Copy the whole inline data block */
54 src_addr = inline_data_addr(ipage);
55 dst_addr = kmap(page);
56 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
58 f2fs_put_page(ipage, 1);
61 SetPageUptodate(page);
67 static int __f2fs_convert_inline_data(struct inode *inode, struct page *page)
71 struct dnode_of_data dn;
72 void *src_addr, *dst_addr;
74 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
75 struct f2fs_io_info fio = {
77 .rw = WRITE_SYNC | REQ_PRIO,
81 ipage = get_node_page(sbi, inode->i_ino);
87 /* someone else converted inline_data already */
88 if (!f2fs_has_inline_data(inode))
92 * i_addr[0] is not used for inline data,
93 * so reserving new block will not destroy inline data
95 set_new_dnode(&dn, inode, ipage, NULL, 0);
96 err = f2fs_reserve_block(&dn, 0);
100 f2fs_wait_on_page_writeback(page, DATA);
101 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
103 /* Copy the whole inline data block */
104 src_addr = inline_data_addr(ipage);
105 dst_addr = kmap(page);
106 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
108 SetPageUptodate(page);
110 /* write data page to try to make data consistent */
111 set_page_writeback(page);
112 write_data_page(page, &dn, &new_blk_addr, &fio);
113 update_extent_cache(new_blk_addr, &dn);
114 f2fs_wait_on_page_writeback(page, DATA);
116 /* clear inline data and flag after data writeback */
117 zero_user_segment(ipage, INLINE_DATA_OFFSET,
118 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
119 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
120 stat_dec_inline_inode(inode);
122 sync_inode_page(&dn);
129 int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size,
132 struct page *new_page = page;
135 if (!f2fs_has_inline_data(inode))
137 else if (to_size <= MAX_INLINE_DATA)
140 if (!page || page->index != 0) {
141 new_page = grab_cache_page(inode->i_mapping, 0);
146 err = __f2fs_convert_inline_data(inode, new_page);
147 if (!page || page->index != 0)
148 f2fs_put_page(new_page, 1);
152 int f2fs_write_inline_data(struct inode *inode,
153 struct page *page, unsigned size)
155 void *src_addr, *dst_addr;
157 struct dnode_of_data dn;
160 set_new_dnode(&dn, inode, NULL, NULL, 0);
161 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
164 ipage = dn.inode_page;
166 f2fs_wait_on_page_writeback(ipage, NODE);
167 zero_user_segment(ipage, INLINE_DATA_OFFSET,
168 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
169 src_addr = kmap(page);
170 dst_addr = inline_data_addr(ipage);
171 memcpy(dst_addr, src_addr, size);
174 /* Release the first data block if it is allocated */
175 if (!f2fs_has_inline_data(inode)) {
176 truncate_data_blocks_range(&dn, 1);
177 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
178 stat_inc_inline_inode(inode);
181 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
182 sync_inode_page(&dn);
188 void truncate_inline_data(struct inode *inode, u64 from)
192 if (from >= MAX_INLINE_DATA)
195 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
199 f2fs_wait_on_page_writeback(ipage, NODE);
201 zero_user_segment(ipage, INLINE_DATA_OFFSET + from,
202 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
203 set_page_dirty(ipage);
204 f2fs_put_page(ipage, 1);
207 bool recover_inline_data(struct inode *inode, struct page *npage)
209 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
210 struct f2fs_inode *ri = NULL;
211 void *src_addr, *dst_addr;
215 * The inline_data recovery policy is as follows.
216 * [prev.] [next] of inline_data flag
217 * o o -> recover inline_data
218 * o x -> remove inline_data, and then recover data blocks
219 * x o -> remove inline_data, and then recover inline_data
220 * x x -> recover data blocks
223 ri = F2FS_INODE(npage);
225 if (f2fs_has_inline_data(inode) &&
226 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
228 ipage = get_node_page(sbi, inode->i_ino);
229 f2fs_bug_on(IS_ERR(ipage));
231 f2fs_wait_on_page_writeback(ipage, NODE);
233 src_addr = inline_data_addr(npage);
234 dst_addr = inline_data_addr(ipage);
235 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
236 update_inode(inode, ipage);
237 f2fs_put_page(ipage, 1);
241 if (f2fs_has_inline_data(inode)) {
242 ipage = get_node_page(sbi, inode->i_ino);
243 f2fs_bug_on(IS_ERR(ipage));
244 f2fs_wait_on_page_writeback(ipage, NODE);
245 zero_user_segment(ipage, INLINE_DATA_OFFSET,
246 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
247 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
248 update_inode(inode, ipage);
249 f2fs_put_page(ipage, 1);
250 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
251 truncate_blocks(inode, 0, false);
252 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);