f2fs: retain inconsistency information to initiate fsck.f2fs
[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{
e18c65b2
HL
18 block_t nr_blocks;
19 loff_t i_size;
20
4081363f 21 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
e18c65b2
HL
22 return false;
23
24 nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2;
25 if (inode->i_blocks > nr_blocks)
26 return false;
27
28 i_size = i_size_read(inode);
29 if (i_size > MAX_INLINE_DATA)
30 return false;
31
32 return true;
33}
34
35int f2fs_read_inline_data(struct inode *inode, struct page *page)
36{
e18c65b2
HL
37 struct page *ipage;
38 void *src_addr, *dst_addr;
39
04a17fb1
CY
40 if (page->index) {
41 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
42 goto out;
43 }
44
4081363f 45 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
d54c795b
CY
46 if (IS_ERR(ipage)) {
47 unlock_page(page);
e18c65b2 48 return PTR_ERR(ipage);
d54c795b 49 }
e18c65b2 50
18309aaa 51 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
e18c65b2
HL
52
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);
57 kunmap(page);
58 f2fs_put_page(ipage, 1);
59
04a17fb1 60out:
e18c65b2
HL
61 SetPageUptodate(page);
62 unlock_page(page);
63
64 return 0;
65}
66
67static int __f2fs_convert_inline_data(struct inode *inode, struct page *page)
68{
ec4e7af4 69 int err = 0;
e18c65b2
HL
70 struct page *ipage;
71 struct dnode_of_data dn;
72 void *src_addr, *dst_addr;
73 block_t new_blk_addr;
4081363f 74 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
e18c65b2
HL
75 struct f2fs_io_info fio = {
76 .type = DATA,
77 .rw = WRITE_SYNC | REQ_PRIO,
78 };
79
80 f2fs_lock_op(sbi);
81 ipage = get_node_page(sbi, inode->i_ino);
15c6e3aa
JK
82 if (IS_ERR(ipage)) {
83 err = PTR_ERR(ipage);
84 goto out;
85 }
e18c65b2 86
ec4e7af4
JK
87 /* someone else converted inline_data already */
88 if (!f2fs_has_inline_data(inode))
89 goto out;
90
e18c65b2
HL
91 /*
92 * i_addr[0] is not used for inline data,
93 * so reserving new block will not destroy inline data
94 */
a8865372 95 set_new_dnode(&dn, inode, ipage, NULL, 0);
e18c65b2 96 err = f2fs_reserve_block(&dn, 0);
15c6e3aa
JK
97 if (err)
98 goto out;
e18c65b2 99
9ac1349a 100 f2fs_wait_on_page_writeback(page, DATA);
18309aaa 101 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
e18c65b2
HL
102
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);
107 kunmap(page);
9e09fc85 108 SetPageUptodate(page);
e18c65b2
HL
109
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);
5514f0aa 114 f2fs_wait_on_page_writeback(page, DATA);
e18c65b2
HL
115
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);
0dbdc2ae 120 stat_dec_inline_inode(inode);
e18c65b2
HL
121
122 sync_inode_page(&dn);
a8865372 123 f2fs_put_dnode(&dn);
15c6e3aa 124out:
e18c65b2 125 f2fs_unlock_op(sbi);
e18c65b2
HL
126 return err;
127}
128
b067ba1f
JK
129int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size,
130 struct page *page)
e18c65b2 131{
b067ba1f 132 struct page *new_page = page;
9e09fc85 133 int err;
e18c65b2 134
9e09fc85
JK
135 if (!f2fs_has_inline_data(inode))
136 return 0;
137 else if (to_size <= MAX_INLINE_DATA)
138 return 0;
e18c65b2 139
b067ba1f
JK
140 if (!page || page->index != 0) {
141 new_page = grab_cache_page(inode->i_mapping, 0);
142 if (!new_page)
143 return -ENOMEM;
144 }
e18c65b2 145
b067ba1f
JK
146 err = __f2fs_convert_inline_data(inode, new_page);
147 if (!page || page->index != 0)
148 f2fs_put_page(new_page, 1);
e18c65b2
HL
149 return err;
150}
151
152int f2fs_write_inline_data(struct inode *inode,
b067ba1f 153 struct page *page, unsigned size)
e18c65b2
HL
154{
155 void *src_addr, *dst_addr;
156 struct page *ipage;
157 struct dnode_of_data dn;
158 int err;
159
160 set_new_dnode(&dn, inode, NULL, NULL, 0);
161 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
162 if (err)
163 return err;
164 ipage = dn.inode_page;
165
54b591df 166 f2fs_wait_on_page_writeback(ipage, NODE);
e18c65b2
HL
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);
172 kunmap(page);
173
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);
0dbdc2ae 178 stat_inc_inline_inode(inode);
e18c65b2
HL
179 }
180
fff04f90 181 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
e18c65b2
HL
182 sync_inode_page(&dn);
183 f2fs_put_dnode(&dn);
184
185 return 0;
186}
1e1bb4ba 187
8aa6f1c5
CY
188void truncate_inline_data(struct inode *inode, u64 from)
189{
8aa6f1c5
CY
190 struct page *ipage;
191
192 if (from >= MAX_INLINE_DATA)
193 return;
194
4081363f 195 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
8aa6f1c5
CY
196 if (IS_ERR(ipage))
197 return;
198
54b591df
JK
199 f2fs_wait_on_page_writeback(ipage, NODE);
200
8aa6f1c5
CY
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);
205}
206
0342fd30 207bool recover_inline_data(struct inode *inode, struct page *npage)
1e1bb4ba 208{
4081363f 209 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
1e1bb4ba
JK
210 struct f2fs_inode *ri = NULL;
211 void *src_addr, *dst_addr;
212 struct page *ipage;
213
214 /*
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
221 */
222 if (IS_INODE(npage))
223 ri = F2FS_INODE(npage);
224
225 if (f2fs_has_inline_data(inode) &&
0342fd30 226 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
1e1bb4ba
JK
227process_inline:
228 ipage = get_node_page(sbi, inode->i_ino);
229 f2fs_bug_on(IS_ERR(ipage));
230
54b591df
JK
231 f2fs_wait_on_page_writeback(ipage, NODE);
232
1e1bb4ba
JK
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);
0342fd30 238 return true;
1e1bb4ba
JK
239 }
240
241 if (f2fs_has_inline_data(inode)) {
242 ipage = get_node_page(sbi, inode->i_ino);
243 f2fs_bug_on(IS_ERR(ipage));
54b591df 244 f2fs_wait_on_page_writeback(ipage, NODE);
1e1bb4ba
JK
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);
0342fd30 250 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
764aa3e9 251 truncate_blocks(inode, 0, false);
1e1bb4ba
JK
252 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
253 goto process_inline;
254 }
0342fd30 255 return false;
1e1bb4ba 256}
This page took 0.063208 seconds and 5 git commands to generate.