]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/btrfs/file-item.c
Fix inline extent handling in btrfs_get_extent
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / file-item.c
CommitLineData
6cbd5570
CM
1/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
1e1d2701 19#include "ctree.h"
dee26a9f 20#include "disk-io.h"
9f5fae2f 21#include "transaction.h"
1de037a4 22#include "print-tree.h"
1e1d2701 23
6567e837 24#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
509659cd
CM
25 sizeof(struct btrfs_item) * 2) / \
26 BTRFS_CRC32_SIZE) - 1))
b18c6685 27int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
dee26a9f 28 struct btrfs_root *root,
b18c6685 29 u64 objectid, u64 pos,
db94535d
CM
30 u64 offset, u64 disk_num_bytes,
31 u64 num_bytes)
9f5fae2f 32{
dee26a9f
CM
33 int ret = 0;
34 struct btrfs_file_extent_item *item;
35 struct btrfs_key file_key;
5caf2a00 36 struct btrfs_path *path;
5f39d397 37 struct extent_buffer *leaf;
dee26a9f 38
5caf2a00
CM
39 path = btrfs_alloc_path();
40 BUG_ON(!path);
dee26a9f 41 file_key.objectid = objectid;
b18c6685 42 file_key.offset = pos;
dee26a9f
CM
43 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
44
5caf2a00 45 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
dee26a9f 46 sizeof(*item));
54aa1f4d
CM
47 if (ret < 0)
48 goto out;
9773a788 49 BUG_ON(ret);
5f39d397
CM
50 leaf = path->nodes[0];
51 item = btrfs_item_ptr(leaf, path->slots[0],
dee26a9f 52 struct btrfs_file_extent_item);
db94535d
CM
53 btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
54 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
5f39d397 55 btrfs_set_file_extent_offset(leaf, item, 0);
db94535d 56 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
5f39d397
CM
57 btrfs_set_file_extent_generation(leaf, item, trans->transid);
58 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
59 btrfs_mark_buffer_dirty(leaf);
54aa1f4d 60out:
5caf2a00 61 btrfs_free_path(path);
54aa1f4d 62 return ret;
9f5fae2f 63}
dee26a9f 64
b18c6685
CM
65struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
66 struct btrfs_root *root,
67 struct btrfs_path *path,
68 u64 objectid, u64 offset,
69 int cow)
6567e837
CM
70{
71 int ret;
72 struct btrfs_key file_key;
73 struct btrfs_key found_key;
74 struct btrfs_csum_item *item;
5f39d397 75 struct extent_buffer *leaf;
6567e837 76 u64 csum_offset = 0;
a429e513 77 int csums_in_item;
6567e837
CM
78
79 file_key.objectid = objectid;
80 file_key.offset = offset;
6567e837 81 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
b18c6685 82 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
6567e837
CM
83 if (ret < 0)
84 goto fail;
5f39d397 85 leaf = path->nodes[0];
6567e837
CM
86 if (ret > 0) {
87 ret = 1;
70b2befd 88 if (path->slots[0] == 0)
6567e837
CM
89 goto fail;
90 path->slots[0]--;
5f39d397 91 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6567e837
CM
92 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
93 found_key.objectid != objectid) {
94 goto fail;
95 }
96 csum_offset = (offset - found_key.offset) >>
97 root->fs_info->sb->s_blocksize_bits;
5f39d397 98 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
509659cd 99 csums_in_item /= BTRFS_CRC32_SIZE;
a429e513
CM
100
101 if (csum_offset >= csums_in_item) {
102 ret = -EFBIG;
6567e837
CM
103 goto fail;
104 }
105 }
106 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
509659cd
CM
107 item = (struct btrfs_csum_item *)((unsigned char *)item +
108 csum_offset * BTRFS_CRC32_SIZE);
6567e837
CM
109 return item;
110fail:
111 if (ret > 0)
b18c6685 112 ret = -ENOENT;
6567e837
CM
113 return ERR_PTR(ret);
114}
115
116
dee26a9f
CM
117int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
118 struct btrfs_root *root,
119 struct btrfs_path *path, u64 objectid,
9773a788 120 u64 offset, int mod)
dee26a9f
CM
121{
122 int ret;
123 struct btrfs_key file_key;
124 int ins_len = mod < 0 ? -1 : 0;
125 int cow = mod != 0;
126
127 file_key.objectid = objectid;
70b2befd 128 file_key.offset = offset;
dee26a9f
CM
129 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
130 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
131 return ret;
132}
f254e52c
CM
133
134int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
135 struct btrfs_root *root,
f578d4bd 136 struct inode *inode,
f254e52c
CM
137 u64 objectid, u64 offset,
138 char *data, size_t len)
139{
140 int ret;
141 struct btrfs_key file_key;
6567e837 142 struct btrfs_key found_key;
f578d4bd
CM
143 u64 next_offset = (u64)-1;
144 int found_next = 0;
5caf2a00 145 struct btrfs_path *path;
f254e52c 146 struct btrfs_csum_item *item;
ff79f819 147 struct extent_buffer *leaf = NULL;
6567e837 148 u64 csum_offset;
ff79f819 149 u32 csum_result = ~(u32)0;
f578d4bd
CM
150 u32 nritems;
151 u32 ins_size;
f254e52c 152
5caf2a00
CM
153 path = btrfs_alloc_path();
154 BUG_ON(!path);
b18c6685 155
f254e52c
CM
156 file_key.objectid = objectid;
157 file_key.offset = offset;
f254e52c 158 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
a429e513
CM
159
160 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
ff79f819
CM
161 if (!IS_ERR(item)) {
162 leaf = path->nodes[0];
a429e513 163 goto found;
ff79f819 164 }
a429e513
CM
165 ret = PTR_ERR(item);
166 if (ret == -EFBIG) {
167 u32 item_size;
168 /* we found one, but it isn't big enough yet */
5f39d397
CM
169 leaf = path->nodes[0];
170 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
509659cd 171 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
a429e513
CM
172 /* already at max size, make a new one */
173 goto insert;
174 }
175 } else {
f578d4bd 176 int slot = path->slots[0] + 1;
a429e513 177 /* we didn't find a csum item, insert one */
f578d4bd
CM
178 nritems = btrfs_header_nritems(path->nodes[0]);
179 if (path->slots[0] >= nritems - 1) {
180 ret = btrfs_next_leaf(root, path);
181 if (ret == 1) {
182 found_next = 1;
183 } else if (ret == 0) {
184 slot = 0;
185 } else {
186 goto insert;
187 }
188 }
189 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
190 if (found_key.objectid != objectid ||
191 found_key.type != BTRFS_CSUM_ITEM_KEY) {
192 found_next = 1;
193 goto insert;
194 }
195 next_offset = found_key.offset;
196 found_next = 1;
a429e513
CM
197 goto insert;
198 }
199
200 /*
201 * at this point, we know the tree has an item, but it isn't big
202 * enough yet to put our csum in. Grow it
203 */
204 btrfs_release_path(root, path);
6567e837 205 ret = btrfs_search_slot(trans, root, &file_key, path,
509659cd 206 BTRFS_CRC32_SIZE, 1);
6567e837
CM
207 if (ret < 0)
208 goto fail;
209 if (ret == 0) {
b18c6685 210 BUG();
6567e837
CM
211 }
212 if (path->slots[0] == 0) {
6567e837
CM
213 goto insert;
214 }
215 path->slots[0]--;
5f39d397
CM
216 leaf = path->nodes[0];
217 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6567e837
CM
218 csum_offset = (offset - found_key.offset) >>
219 root->fs_info->sb->s_blocksize_bits;
220 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
221 found_key.objectid != objectid ||
222 csum_offset >= MAX_CSUM_ITEMS(root)) {
6567e837
CM
223 goto insert;
224 }
5f39d397 225 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
509659cd
CM
226 BTRFS_CRC32_SIZE) {
227 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
5f39d397 228 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
3a686375
CM
229 if (diff != BTRFS_CRC32_SIZE)
230 goto insert;
a429e513 231 ret = btrfs_extend_item(trans, root, path, diff);
6567e837
CM
232 BUG_ON(ret);
233 goto csum;
234 }
235
236insert:
a429e513 237 btrfs_release_path(root, path);
6567e837 238 csum_offset = 0;
f578d4bd
CM
239 if (found_next) {
240 u64 tmp = min((u64)i_size_read(inode), next_offset);
241 tmp -= offset + root->sectorsize - 1;
242 tmp >>= root->fs_info->sb->s_blocksize_bits;
243 tmp = max((u64)1, tmp);
244 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
245 ins_size = BTRFS_CRC32_SIZE * tmp;
246 } else {
247 ins_size = BTRFS_CRC32_SIZE;
248 }
5caf2a00 249 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
f578d4bd 250 ins_size);
54aa1f4d
CM
251 if (ret < 0)
252 goto fail;
a429e513 253 if (ret != 0) {
a429e513 254 WARN_ON(1);
f254e52c 255 goto fail;
a429e513 256 }
6567e837 257csum:
5f39d397
CM
258 leaf = path->nodes[0];
259 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
f254e52c 260 ret = 0;
509659cd
CM
261 item = (struct btrfs_csum_item *)((unsigned char *)item +
262 csum_offset * BTRFS_CRC32_SIZE);
b18c6685 263found:
ff79f819
CM
264 csum_result = btrfs_csum_data(root, data, csum_result, len);
265 btrfs_csum_final(csum_result, (char *)&csum_result);
266 write_extent_buffer(leaf, &csum_result, (unsigned long)item,
267 BTRFS_CRC32_SIZE);
5caf2a00 268 btrfs_mark_buffer_dirty(path->nodes[0]);
f254e52c 269fail:
5caf2a00
CM
270 btrfs_release_path(root, path);
271 btrfs_free_path(path);
f254e52c
CM
272 return ret;
273}
274
1de037a4
CM
275int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root, struct btrfs_path *path,
277 u64 isize)
278{
279 struct btrfs_key key;
5f39d397 280 struct extent_buffer *leaf = path->nodes[0];
1de037a4
CM
281 int slot = path->slots[0];
282 int ret;
283 u32 new_item_size;
284 u64 new_item_span;
285 u64 blocks;
286
5f39d397 287 btrfs_item_key_to_cpu(leaf, &key, slot);
1de037a4
CM
288 if (isize <= key.offset)
289 return 0;
290 new_item_span = isize - key.offset;
5f39d397 291 blocks = (new_item_span + root->sectorsize - 1) >>
84f54cfa 292 root->fs_info->sb->s_blocksize_bits;
1de037a4 293 new_item_size = blocks * BTRFS_CRC32_SIZE;
5f39d397 294 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
1de037a4
CM
295 return 0;
296 ret = btrfs_truncate_item(trans, root, path, new_item_size);
297 BUG_ON(ret);
298 return ret;
299}
300