blob: 289f99e9a339623921f1458298062b2350616b9d [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{
Huajun Lie18c65b2013-11-10 23:13:19 +080018 block_t nr_blocks;
19 loff_t i_size;
20
Jaegeuk Kim40813632014-09-02 15:31:18 -070021 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
Huajun Lie18c65b2013-11-10 23:13:19 +080022 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{
Huajun Lie18c65b2013-11-10 23:13:19 +080037 struct page *ipage;
38 void *src_addr, *dst_addr;
39
Chao Yu04a17fb2013-12-30 18:36:23 +080040 if (page->index) {
41 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
42 goto out;
43 }
44
Jaegeuk Kim40813632014-09-02 15:31:18 -070045 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yud54c7952014-03-29 15:30:40 +080046 if (IS_ERR(ipage)) {
47 unlock_page(page);
Huajun Lie18c65b2013-11-10 23:13:19 +080048 return PTR_ERR(ipage);
Chao Yud54c7952014-03-29 15:30:40 +080049 }
Huajun Lie18c65b2013-11-10 23:13:19 +080050
Chao Yu18309aa2013-12-30 09:29:06 +080051 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +080052
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
Chao Yu04a17fb2013-12-30 18:36:23 +080060out:
Huajun Lie18c65b2013-11-10 23:13:19 +080061 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{
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -070069 int err = 0;
Huajun Lie18c65b2013-11-10 23:13:19 +080070 struct page *ipage;
71 struct dnode_of_data dn;
72 void *src_addr, *dst_addr;
73 block_t new_blk_addr;
Jaegeuk Kim40813632014-09-02 15:31:18 -070074 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Huajun Lie18c65b2013-11-10 23:13:19 +080075 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);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +090082 if (IS_ERR(ipage)) {
83 err = PTR_ERR(ipage);
84 goto out;
85 }
Huajun Lie18c65b2013-11-10 23:13:19 +080086
Jaegeuk Kimec4e7af2014-08-18 14:41:11 -070087 /* someone else converted inline_data already */
88 if (!f2fs_has_inline_data(inode))
89 goto out;
90
Huajun Lie18c65b2013-11-10 23:13:19 +080091 /*
92 * i_addr[0] is not used for inline data,
93 * so reserving new block will not destroy inline data
94 */
Jaegeuk Kima8865372013-12-27 17:04:17 +090095 set_new_dnode(&dn, inode, ipage, NULL, 0);
Huajun Lie18c65b2013-11-10 23:13:19 +080096 err = f2fs_reserve_block(&dn, 0);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +090097 if (err)
98 goto out;
Huajun Lie18c65b2013-11-10 23:13:19 +080099
Jaegeuk Kim9ac13492014-04-29 17:35:10 +0900100 f2fs_wait_on_page_writeback(page, DATA);
Chao Yu18309aa2013-12-30 09:29:06 +0800101 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800102
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);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900108 SetPageUptodate(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800109
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);
Yuan Zhong5514f0a2014-01-10 07:26:14 +0000114 f2fs_wait_on_page_writeback(page, DATA);
Huajun Lie18c65b2013-11-10 23:13:19 +0800115
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);
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +0900120 stat_dec_inline_inode(inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800121
122 sync_inode_page(&dn);
Jaegeuk Kima8865372013-12-27 17:04:17 +0900123 f2fs_put_dnode(&dn);
Jaegeuk Kim15c6e3a2014-04-16 14:22:50 +0900124out:
Huajun Lie18c65b2013-11-10 23:13:19 +0800125 f2fs_unlock_op(sbi);
Huajun Lie18c65b2013-11-10 23:13:19 +0800126 return err;
127}
128
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700129int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size,
130 struct page *page)
Huajun Lie18c65b2013-11-10 23:13:19 +0800131{
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700132 struct page *new_page = page;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900133 int err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800134
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900135 if (!f2fs_has_inline_data(inode))
136 return 0;
137 else if (to_size <= MAX_INLINE_DATA)
138 return 0;
139
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700140 if (!page || page->index != 0) {
141 new_page = grab_cache_page(inode->i_mapping, 0);
142 if (!new_page)
143 return -ENOMEM;
144 }
Huajun Lie18c65b2013-11-10 23:13:19 +0800145
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700146 err = __f2fs_convert_inline_data(inode, new_page);
147 if (!page || page->index != 0)
148 f2fs_put_page(new_page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800149 return err;
150}
151
152int f2fs_write_inline_data(struct inode *inode,
Jaegeuk Kimb067ba1f2014-08-07 16:32:25 -0700153 struct page *page, unsigned size)
Huajun Lie18c65b2013-11-10 23:13:19 +0800154{
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
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900166 f2fs_wait_on_page_writeback(ipage, NODE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800167 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);
Jaegeuk Kim0dbdc2a2013-11-26 11:08:57 +0900178 stat_inc_inline_inode(inode);
Huajun Lie18c65b2013-11-10 23:13:19 +0800179 }
180
Jaegeuk Kimfff04f92014-07-25 07:40:59 -0700181 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
Huajun Lie18c65b2013-11-10 23:13:19 +0800182 sync_inode_page(&dn);
183 f2fs_put_dnode(&dn);
184
185 return 0;
186}
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900187
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800188void truncate_inline_data(struct inode *inode, u64 from)
189{
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800190 struct page *ipage;
191
192 if (from >= MAX_INLINE_DATA)
193 return;
194
Jaegeuk Kim40813632014-09-02 15:31:18 -0700195 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800196 if (IS_ERR(ipage))
197 return;
198
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900199 f2fs_wait_on_page_writeback(ipage, NODE);
200
Chao Yu8aa6f1c2014-04-29 09:03:03 +0800201 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
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700207bool recover_inline_data(struct inode *inode, struct page *npage)
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900208{
Jaegeuk Kim40813632014-09-02 15:31:18 -0700209 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900210 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) &&
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700226 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900227process_inline:
228 ipage = get_node_page(sbi, inode->i_ino);
229 f2fs_bug_on(IS_ERR(ipage));
230
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900231 f2fs_wait_on_page_writeback(ipage, NODE);
232
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900233 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);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700238 return true;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900239 }
240
241 if (f2fs_has_inline_data(inode)) {
242 ipage = get_node_page(sbi, inode->i_ino);
243 f2fs_bug_on(IS_ERR(ipage));
Jaegeuk Kim54b591d2014-04-29 17:28:32 +0900244 f2fs_wait_on_page_writeback(ipage, NODE);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900245 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);
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700250 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
Jaegeuk Kim764aa3e2014-08-14 16:32:54 -0700251 truncate_blocks(inode, 0, false);
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900252 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
253 goto process_inline;
254 }
Jaegeuk Kim0342fd32014-08-07 16:57:17 -0700255 return false;
Jaegeuk Kim1e1bb4b2013-12-26 12:49:48 +0900256}