blob: e8891aa3ab8c095b42c718d1231484005a79145b [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
16inline int f2fs_has_inline_data(struct inode *inode)
17{
18 return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
19}
20
21bool f2fs_may_inline(struct inode *inode)
22{
23 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
24 block_t nr_blocks;
25 loff_t i_size;
26
27 if (!test_opt(sbi, INLINE_DATA))
28 return false;
29
30 nr_blocks = F2FS_I(inode)->i_xattr_nid ? 3 : 2;
31 if (inode->i_blocks > nr_blocks)
32 return false;
33
34 i_size = i_size_read(inode);
35 if (i_size > MAX_INLINE_DATA)
36 return false;
37
38 return true;
39}
40
41int f2fs_read_inline_data(struct inode *inode, struct page *page)
42{
43 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
44 struct page *ipage;
45 void *src_addr, *dst_addr;
46
47 ipage = get_node_page(sbi, inode->i_ino);
48 if (IS_ERR(ipage))
49 return PTR_ERR(ipage);
50
51 zero_user_segment(page, INLINE_DATA_OFFSET,
52 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
53
54 /* Copy the whole inline data block */
55 src_addr = inline_data_addr(ipage);
56 dst_addr = kmap(page);
57 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
58 kunmap(page);
59 f2fs_put_page(ipage, 1);
60
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{
69 int err;
70 struct page *ipage;
71 struct dnode_of_data dn;
72 void *src_addr, *dst_addr;
73 block_t new_blk_addr;
74 struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
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);
82 if (IS_ERR(ipage))
83 return PTR_ERR(ipage);
84
85 /*
86 * i_addr[0] is not used for inline data,
87 * so reserving new block will not destroy inline data
88 */
89 set_new_dnode(&dn, inode, ipage, ipage, 0);
90 err = f2fs_reserve_block(&dn, 0);
91 if (err) {
92 f2fs_put_page(ipage, 1);
93 f2fs_unlock_op(sbi);
94 return err;
95 }
96
97 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
98
99 /* Copy the whole inline data block */
100 src_addr = inline_data_addr(ipage);
101 dst_addr = kmap(page);
102 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
103 kunmap(page);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900104 SetPageUptodate(page);
Huajun Lie18c65b2013-11-10 23:13:19 +0800105
106 /* write data page to try to make data consistent */
107 set_page_writeback(page);
108 write_data_page(page, &dn, &new_blk_addr, &fio);
109 update_extent_cache(new_blk_addr, &dn);
110 f2fs_wait_on_page_writeback(page, DATA, true);
111
112 /* clear inline data and flag after data writeback */
113 zero_user_segment(ipage, INLINE_DATA_OFFSET,
114 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
115 clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
116
117 sync_inode_page(&dn);
118 f2fs_put_page(ipage, 1);
119 f2fs_unlock_op(sbi);
120
121 return err;
122}
123
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900124int f2fs_convert_inline_data(struct inode *inode, pgoff_t to_size)
Huajun Lie18c65b2013-11-10 23:13:19 +0800125{
Huajun Lie18c65b2013-11-10 23:13:19 +0800126 struct page *page;
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900127 int err;
Huajun Lie18c65b2013-11-10 23:13:19 +0800128
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900129 if (!f2fs_has_inline_data(inode))
130 return 0;
131 else if (to_size <= MAX_INLINE_DATA)
132 return 0;
133
134 page = grab_cache_page_write_begin(inode->i_mapping, 0, AOP_FLAG_NOFS);
135 if (!page)
136 return -ENOMEM;
Huajun Lie18c65b2013-11-10 23:13:19 +0800137
138 err = __f2fs_convert_inline_data(inode, page);
Jaegeuk Kim9e09fc82013-12-27 12:28:59 +0900139 f2fs_put_page(page, 1);
Huajun Lie18c65b2013-11-10 23:13:19 +0800140 return err;
141}
142
143int f2fs_write_inline_data(struct inode *inode,
144 struct page *page, unsigned size)
145{
146 void *src_addr, *dst_addr;
147 struct page *ipage;
148 struct dnode_of_data dn;
149 int err;
150
151 set_new_dnode(&dn, inode, NULL, NULL, 0);
152 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
153 if (err)
154 return err;
155 ipage = dn.inode_page;
156
157 zero_user_segment(ipage, INLINE_DATA_OFFSET,
158 INLINE_DATA_OFFSET + MAX_INLINE_DATA);
159 src_addr = kmap(page);
160 dst_addr = inline_data_addr(ipage);
161 memcpy(dst_addr, src_addr, size);
162 kunmap(page);
163
164 /* Release the first data block if it is allocated */
165 if (!f2fs_has_inline_data(inode)) {
166 truncate_data_blocks_range(&dn, 1);
167 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
168 }
169
170 sync_inode_page(&dn);
171 f2fs_put_dnode(&dn);
172
173 return 0;
174}