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