Merge tag 'for-f2fs-3.17-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/jaegeu...
[deliverable/linux.git] / fs / f2fs / inline.c
CommitLineData
e18c65b2
HL
1/*
2 * fs/f2fs/inline.c
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.
9 */
10
11#include <linux/fs.h>
12#include <linux/f2fs_fs.h>
13
14#include "f2fs.h"
15
e18c65b2
HL
16bool f2fs_may_inline(struct inode *inode)
17{
18 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
19 block_t nr_blocks;
20 loff_t i_size;
21
22 if (!test_opt(sbi, INLINE_DATA))
23 return false;
24
25 nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2;
26 if (inode->i_blocks > nr_blocks)
27 return false;
28
29 i_size = i_size_read(inode);
30 if (i_size > MAX_INLINE_DATA)
31 return false;
32
33 return true;
34}
35
36int f2fs_read_inline_data(struct inode *inode, struct page *page)
37{
38 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
39 struct page *ipage;
40 void *src_addr, *dst_addr;
41
04a17fb1
CY
42 if (page->index) {
43 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
44 goto out;
45 }
46
e18c65b2 47 ipage = get_node_page(sbi, inode->i_ino);
d54c795b
CY
48 if (IS_ERR(ipage)) {
49 unlock_page(page);
e18c65b2 50 return PTR_ERR(ipage);
d54c795b 51 }
e18c65b2 52
18309aaa 53 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
e18c65b2
HL
54
55 /* Copy the whole inline data block */
56 src_addr = inline_data_addr(ipage);
57 dst_addr = kmap(page);
58 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
59 kunmap(page);
60 f2fs_put_page(ipage, 1);
61
04a17fb1 62out:
e18c65b2
HL
63 SetPageUptodate(page);
64 unlock_page(page);
65
66 return 0;
67}
68
69static int __f2fs_convert_inline_data(struct inode *inode, struct page *page)
70{
ec4e7af4 71 int err = 0;
e18c65b2
HL
72 struct page *ipage;
73 struct dnode_of_data dn;
74 void *src_addr, *dst_addr;
75 block_t new_blk_addr;
76 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
77 struct f2fs_io_info fio = {
78 .type = DATA,
79 .rw = WRITE_SYNC | REQ_PRIO,
80 };
81
82 f2fs_lock_op(sbi);
83 ipage = get_node_page(sbi, inode->i_ino);
15c6e3aa
JK
84 if (IS_ERR(ipage)) {
85 err = PTR_ERR(ipage);
86 goto out;
87 }
e18c65b2 88
ec4e7af4
JK
89 /* someone else converted inline_data already */
90 if (!f2fs_has_inline_data(inode))
91 goto out;
92
e18c65b2
HL
93 /*
94 * i_addr[0] is not used for inline data,
95 * so reserving new block will not destroy inline data
96 */
a8865372 97 set_new_dnode(&dn, inode, ipage, NULL, 0);
e18c65b2 98 err = f2fs_reserve_block(&dn, 0);
15c6e3aa
JK
99 if (err)
100 goto out;
e18c65b2 101
9ac1349a 102 f2fs_wait_on_page_writeback(page, DATA);
18309aaa 103 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
e18c65b2
HL
104
105 /* Copy the whole inline data block */
106 src_addr = inline_data_addr(ipage);
107 dst_addr = kmap(page);
108 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
109 kunmap(page);
9e09fc85 110 SetPageUptodate(page);
e18c65b2
HL
111
112 /* write data page to try to make data consistent */
113 set_page_writeback(page);
114 write_data_page(page, &dn, &new_blk_addr, &fio);
115 update_extent_cache(new_blk_addr, &dn);
5514f0aa 116 f2fs_wait_on_page_writeback(page, DATA);
e18c65b2
HL
117
118 /* clear inline data and flag after data writeback */
119 zero_user_segment(ipage, INLINE_DATA_OFFSET,
120 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
121 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
0dbdc2ae 122 stat_dec_inline_inode(inode);
e18c65b2
HL
123
124 sync_inode_page(&dn);
a8865372 125 f2fs_put_dnode(&dn);
15c6e3aa 126out:
e18c65b2 127 f2fs_unlock_op(sbi);
e18c65b2
HL
128 return err;
129}
130
b067ba1f
JK
131int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size,
132 struct page *page)
e18c65b2 133{
b067ba1f 134 struct page *new_page = page;
9e09fc85 135 int err;
e18c65b2 136
9e09fc85
JK
137 if (!f2fs_has_inline_data(inode))
138 return 0;
139 else if (to_size <= MAX_INLINE_DATA)
140 return 0;
e18c65b2 141
b067ba1f
JK
142 if (!page || page->index != 0) {
143 new_page = grab_cache_page(inode->i_mapping, 0);
144 if (!new_page)
145 return -ENOMEM;
146 }
e18c65b2 147
b067ba1f
JK
148 err = __f2fs_convert_inline_data(inode, new_page);
149 if (!page || page->index != 0)
150 f2fs_put_page(new_page, 1);
e18c65b2
HL
151 return err;
152}
153
154int f2fs_write_inline_data(struct inode *inode,
b067ba1f 155 struct page *page, unsigned size)
e18c65b2
HL
156{
157 void *src_addr, *dst_addr;
158 struct page *ipage;
159 struct dnode_of_data dn;
160 int err;
161
162 set_new_dnode(&dn, inode, NULL, NULL, 0);
163 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
164 if (err)
165 return err;
166 ipage = dn.inode_page;
167
54b591df 168 f2fs_wait_on_page_writeback(ipage, NODE);
e18c65b2
HL
169 zero_user_segment(ipage, INLINE_DATA_OFFSET,
170 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
171 src_addr = kmap(page);
172 dst_addr = inline_data_addr(ipage);
173 memcpy(dst_addr, src_addr, size);
174 kunmap(page);
175
176 /* Release the first data block if it is allocated */
177 if (!f2fs_has_inline_data(inode)) {
178 truncate_data_blocks_range(&dn, 1);
179 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
0dbdc2ae 180 stat_inc_inline_inode(inode);
e18c65b2
HL
181 }
182
fff04f90 183 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
e18c65b2
HL
184 sync_inode_page(&dn);
185 f2fs_put_dnode(&dn);
186
187 return 0;
188}
1e1bb4ba 189
8aa6f1c5
CY
190void truncate_inline_data(struct inode *inode, u64 from)
191{
192 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
193 struct page *ipage;
194
195 if (from >= MAX_INLINE_DATA)
196 return;
197
198 ipage = get_node_page(sbi, inode->i_ino);
199 if (IS_ERR(ipage))
200 return;
201
54b591df
JK
202 f2fs_wait_on_page_writeback(ipage, NODE);
203
8aa6f1c5
CY
204 zero_user_segment(ipage, INLINE_DATA_OFFSET + from,
205 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
206 set_page_dirty(ipage);
207 f2fs_put_page(ipage, 1);
208}
209
0342fd30 210bool recover_inline_data(struct inode *inode, struct page *npage)
1e1bb4ba
JK
211{
212 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
213 struct f2fs_inode *ri = NULL;
214 void *src_addr, *dst_addr;
215 struct page *ipage;
216
217 /*
218 * The inline_data recovery policy is as follows.
219 * [prev.] [next] of inline_data flag
220 * o o -> recover inline_data
221 * o x -> remove inline_data, and then recover data blocks
222 * x o -> remove inline_data, and then recover inline_data
223 * x x -> recover data blocks
224 */
225 if (IS_INODE(npage))
226 ri = F2FS_INODE(npage);
227
228 if (f2fs_has_inline_data(inode) &&
0342fd30 229 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
1e1bb4ba
JK
230process_inline:
231 ipage = get_node_page(sbi, inode->i_ino);
232 f2fs_bug_on(IS_ERR(ipage));
233
54b591df
JK
234 f2fs_wait_on_page_writeback(ipage, NODE);
235
1e1bb4ba
JK
236 src_addr = inline_data_addr(npage);
237 dst_addr = inline_data_addr(ipage);
238 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
239 update_inode(inode, ipage);
240 f2fs_put_page(ipage, 1);
0342fd30 241 return true;
1e1bb4ba
JK
242 }
243
244 if (f2fs_has_inline_data(inode)) {
245 ipage = get_node_page(sbi, inode->i_ino);
246 f2fs_bug_on(IS_ERR(ipage));
54b591df 247 f2fs_wait_on_page_writeback(ipage, NODE);
1e1bb4ba
JK
248 zero_user_segment(ipage, INLINE_DATA_OFFSET,
249 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
250 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
251 update_inode(inode, ipage);
252 f2fs_put_page(ipage, 1);
0342fd30 253 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
764aa3e9 254 truncate_blocks(inode, 0, false);
1e1bb4ba
JK
255 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
256 goto process_inline;
257 }
0342fd30 258 return false;
1e1bb4ba 259}
This page took 0.062962 seconds and 5 git commands to generate.