blob: 1bba5228c197443eb3a8ec893ab84d09462525e4 [file] [log] [blame]
Huajun Lie18c65b2013-11-10 23:13:19 +08001/*
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
Huajun Lie18c65b2013-11-10 23:13:19 +080016bool 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
Chao Yu04a17fb2013-12-30 18:36:23 +080042 if (page->index) {
43 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
44 goto out;
45 }
46
Huajun Lie18c65b2013-11-10 23:13:19 +080047 ipage = get_node_page(sbi, inode->i_ino);
Chao Yud54c7952014-03-29 15:30:40 +080048 if (IS_ERR(ipage)) {
49 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080050 return PTR_ERR(ipage);
Chao Yud54c7952014-03-29 15:30:40 +080051 }
Huajun Lie18c65b2013-11-10 23:13:19 +080052
Chao Yu18309aa2013-12-30 09:29:06 +080053 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080054
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
Chao Yu04a17fb2013-12-30 18:36:23 +080062out:
Huajun Lie18c65b2013-11-10 23:13:19 +080063 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{
71 int err;
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);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +090084 if (IS_ERR(ipage)) {
85 err = PTR_ERR(ipage);
86 goto out;
87 }
Huajun Lie18c65b2013-11-10 23:13:19 +080088
89 /*
90 * i_addr[0] is not used for inline data,
91 * so reserving new block will not destroy inline data
92 */
Jaegeuk Kima8865372013-12-27 17:04:17 +090093 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lie18c65b2013-11-10 23:13:19 +080094 err = f2fs_reserve_block(&dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +090095 if (err)
96 goto out;
Huajun Lie18c65b2013-11-10 23:13:19 +080097
Jaegeuk Kim9ac13492014-04-29 17:35:10 +090098 f2fs_wait_on_page_writeback(page, DATA);
Chao Yu18309aa2013-12-30 09:29:06 +080099 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800100
101 /* Copy the whole inline data block */
102 src_addr = inline_data_addr(ipage);
103 dst_addr = kmap(page);
104 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
105 kunmap(page);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900106 SetPageUptodate(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800107
108 /* write data page to try to make data consistent */
109 set_page_writeback(page);
110 write_data_page(page, &dn, &new_blk_addr, &fio);
111 update_extent_cache(new_blk_addr, &dn);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000112 f2fs_wait_on_page_writeback(page, DATA);
Huajun Lie18c65b2013-11-10 23:13:19 +0800113
114 /* clear inline data and flag after data writeback */
115 zero_user_segment(ipage, INLINE_DATA_OFFSET,
116 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
117 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +0900118 stat_dec_inline_inode(inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800119
120 sync_inode_page(&dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900121 f2fs_put_dnode(&dn);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900122out:
Huajun Lie18c65b2013-11-10 23:13:19 +0800123 f2fs_unlock_op(sbi);
Huajun Lie18c65b2013-11-10 23:13:19 +0800124 return err;
125}
126
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900127int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size)
Huajun Lie18c65b2013-11-10 23:13:19 +0800128{
Huajun Lie18c65b2013-11-10 23:13:19 +0800129 struct page *page;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900130 int err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800131
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900132 if (!f2fs_has_inline_data(inode))
133 return 0;
134 else if (to_size <= MAX_INLINE_DATA)
135 return 0;
136
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900137 page = grab_cache_page(inode->i_mapping, 0);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900138 if (!page)
139 return -ENOMEM;
Huajun Lie18c65b2013-11-10 23:13:19 +0800140
141 err = __f2fs_convert_inline_data(inode, page);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900142 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800143 return err;
144}
145
146int f2fs_write_inline_data(struct inode *inode,
147 struct page *page, unsigned size)
148{
149 void *src_addr, *dst_addr;
150 struct page *ipage;
151 struct dnode_of_data dn;
152 int err;
153
154 set_new_dnode(&dn, inode, NULL, NULL, 0);
155 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
156 if (err)
157 return err;
158 ipage = dn.inode_page;
159
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900160 f2fs_wait_on_page_writeback(ipage, NODE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800161 zero_user_segment(ipage, INLINE_DATA_OFFSET,
162 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
163 src_addr = kmap(page);
164 dst_addr = inline_data_addr(ipage);
165 memcpy(dst_addr, src_addr, size);
166 kunmap(page);
167
168 /* Release the first data block if it is allocated */
169 if (!f2fs_has_inline_data(inode)) {
170 truncate_data_blocks_range(&dn, 1);
171 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +0900172 stat_inc_inline_inode(inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800173 }
174
175 sync_inode_page(&dn);
176 f2fs_put_dnode(&dn);
177
178 return 0;
179}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900180
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800181void truncate_inline_data(struct inode *inode, u64 from)
182{
183 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
184 struct page *ipage;
185
186 if (from >= MAX_INLINE_DATA)
187 return;
188
189 ipage = get_node_page(sbi, inode->i_ino);
190 if (IS_ERR(ipage))
191 return;
192
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900193 f2fs_wait_on_page_writeback(ipage, NODE);
194
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800195 zero_user_segment(ipage, INLINE_DATA_OFFSET + from,
196 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
197 set_page_dirty(ipage);
198 f2fs_put_page(ipage, 1);
199}
200
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900201int recover_inline_data(struct inode *inode, struct page *npage)
202{
203 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
204 struct f2fs_inode *ri = NULL;
205 void *src_addr, *dst_addr;
206 struct page *ipage;
207
208 /*
209 * The inline_data recovery policy is as follows.
210 * [prev.] [next] of inline_data flag
211 * o o -> recover inline_data
212 * o x -> remove inline_data, and then recover data blocks
213 * x o -> remove inline_data, and then recover inline_data
214 * x x -> recover data blocks
215 */
216 if (IS_INODE(npage))
217 ri = F2FS_INODE(npage);
218
219 if (f2fs_has_inline_data(inode) &&
220 ri && ri->i_inline & F2FS_INLINE_DATA) {
221process_inline:
222 ipage = get_node_page(sbi, inode->i_ino);
223 f2fs_bug_on(IS_ERR(ipage));
224
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900225 f2fs_wait_on_page_writeback(ipage, NODE);
226
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900227 src_addr = inline_data_addr(npage);
228 dst_addr = inline_data_addr(ipage);
229 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
230 update_inode(inode, ipage);
231 f2fs_put_page(ipage, 1);
232 return -1;
233 }
234
235 if (f2fs_has_inline_data(inode)) {
236 ipage = get_node_page(sbi, inode->i_ino);
237 f2fs_bug_on(IS_ERR(ipage));
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900238 f2fs_wait_on_page_writeback(ipage, NODE);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900239 zero_user_segment(ipage, INLINE_DATA_OFFSET,
240 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
241 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
242 update_inode(inode, ipage);
243 f2fs_put_page(ipage, 1);
244 } else if (ri && ri->i_inline & F2FS_INLINE_DATA) {
245 truncate_blocks(inode, 0);
246 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
247 goto process_inline;
248 }
249 return 0;
250}