]>
Commit | Line | Data |
---|---|---|
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 | ||
065631f6 CM |
19 | #include <linux/bio.h> |
20 | #include <linux/pagemap.h> | |
21 | #include <linux/highmem.h> | |
1e1d2701 | 22 | #include "ctree.h" |
dee26a9f | 23 | #include "disk-io.h" |
9f5fae2f | 24 | #include "transaction.h" |
1de037a4 | 25 | #include "print-tree.h" |
1e1d2701 | 26 | |
d397712b | 27 | #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \ |
607d432d JB |
28 | sizeof(struct btrfs_item) * 2) / \ |
29 | size) - 1)) | |
07d400a6 YZ |
30 | |
31 | #define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \ | |
32 | sizeof(struct btrfs_ordered_sum)) / \ | |
33 | sizeof(struct btrfs_sector_sum) * \ | |
34 | (r)->sectorsize - (r)->sectorsize) | |
35 | ||
b18c6685 | 36 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, |
f2eb0a24 SW |
37 | struct btrfs_root *root, |
38 | u64 objectid, u64 pos, | |
39 | u64 disk_offset, u64 disk_num_bytes, | |
c8b97818 CM |
40 | u64 num_bytes, u64 offset, u64 ram_bytes, |
41 | u8 compression, u8 encryption, u16 other_encoding) | |
9f5fae2f | 42 | { |
dee26a9f CM |
43 | int ret = 0; |
44 | struct btrfs_file_extent_item *item; | |
45 | struct btrfs_key file_key; | |
5caf2a00 | 46 | struct btrfs_path *path; |
5f39d397 | 47 | struct extent_buffer *leaf; |
dee26a9f | 48 | |
5caf2a00 CM |
49 | path = btrfs_alloc_path(); |
50 | BUG_ON(!path); | |
dee26a9f | 51 | file_key.objectid = objectid; |
b18c6685 | 52 | file_key.offset = pos; |
dee26a9f CM |
53 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
54 | ||
5caf2a00 | 55 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
dee26a9f | 56 | sizeof(*item)); |
54aa1f4d CM |
57 | if (ret < 0) |
58 | goto out; | |
9773a788 | 59 | BUG_ON(ret); |
5f39d397 CM |
60 | leaf = path->nodes[0]; |
61 | item = btrfs_item_ptr(leaf, path->slots[0], | |
dee26a9f | 62 | struct btrfs_file_extent_item); |
f2eb0a24 | 63 | btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); |
db94535d | 64 | btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); |
f2eb0a24 | 65 | btrfs_set_file_extent_offset(leaf, item, offset); |
db94535d | 66 | btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); |
c8b97818 | 67 | btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); |
5f39d397 CM |
68 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
69 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | |
c8b97818 CM |
70 | btrfs_set_file_extent_compression(leaf, item, compression); |
71 | btrfs_set_file_extent_encryption(leaf, item, encryption); | |
72 | btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); | |
73 | ||
5f39d397 | 74 | btrfs_mark_buffer_dirty(leaf); |
54aa1f4d | 75 | out: |
5caf2a00 | 76 | btrfs_free_path(path); |
54aa1f4d | 77 | return ret; |
9f5fae2f | 78 | } |
dee26a9f | 79 | |
b18c6685 CM |
80 | struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, |
81 | struct btrfs_root *root, | |
82 | struct btrfs_path *path, | |
d20f7043 | 83 | u64 bytenr, int cow) |
6567e837 CM |
84 | { |
85 | int ret; | |
86 | struct btrfs_key file_key; | |
87 | struct btrfs_key found_key; | |
88 | struct btrfs_csum_item *item; | |
5f39d397 | 89 | struct extent_buffer *leaf; |
6567e837 | 90 | u64 csum_offset = 0; |
607d432d JB |
91 | u16 csum_size = |
92 | btrfs_super_csum_size(&root->fs_info->super_copy); | |
a429e513 | 93 | int csums_in_item; |
6567e837 | 94 | |
d20f7043 CM |
95 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
96 | file_key.offset = bytenr; | |
97 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); | |
b18c6685 | 98 | ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); |
6567e837 CM |
99 | if (ret < 0) |
100 | goto fail; | |
5f39d397 | 101 | leaf = path->nodes[0]; |
6567e837 CM |
102 | if (ret > 0) { |
103 | ret = 1; | |
70b2befd | 104 | if (path->slots[0] == 0) |
6567e837 CM |
105 | goto fail; |
106 | path->slots[0]--; | |
5f39d397 | 107 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
d20f7043 | 108 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY) |
6567e837 | 109 | goto fail; |
d20f7043 CM |
110 | |
111 | csum_offset = (bytenr - found_key.offset) >> | |
6567e837 | 112 | root->fs_info->sb->s_blocksize_bits; |
5f39d397 | 113 | csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); |
607d432d | 114 | csums_in_item /= csum_size; |
a429e513 CM |
115 | |
116 | if (csum_offset >= csums_in_item) { | |
117 | ret = -EFBIG; | |
6567e837 CM |
118 | goto fail; |
119 | } | |
120 | } | |
121 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); | |
509659cd | 122 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
607d432d | 123 | csum_offset * csum_size); |
6567e837 CM |
124 | return item; |
125 | fail: | |
126 | if (ret > 0) | |
b18c6685 | 127 | ret = -ENOENT; |
6567e837 CM |
128 | return ERR_PTR(ret); |
129 | } | |
130 | ||
131 | ||
dee26a9f CM |
132 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, |
133 | struct btrfs_root *root, | |
134 | struct btrfs_path *path, u64 objectid, | |
9773a788 | 135 | u64 offset, int mod) |
dee26a9f CM |
136 | { |
137 | int ret; | |
138 | struct btrfs_key file_key; | |
139 | int ins_len = mod < 0 ? -1 : 0; | |
140 | int cow = mod != 0; | |
141 | ||
142 | file_key.objectid = objectid; | |
70b2befd | 143 | file_key.offset = offset; |
dee26a9f CM |
144 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
145 | ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); | |
146 | return ret; | |
147 | } | |
f254e52c | 148 | |
17d217fe | 149 | |
61b49440 | 150 | int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, |
d20f7043 | 151 | struct bio *bio, u32 *dst) |
61b49440 CM |
152 | { |
153 | u32 sum; | |
154 | struct bio_vec *bvec = bio->bi_io_vec; | |
155 | int bio_index = 0; | |
156 | u64 offset; | |
157 | u64 item_start_offset = 0; | |
158 | u64 item_last_offset = 0; | |
d20f7043 | 159 | u64 disk_bytenr; |
61b49440 | 160 | u32 diff; |
607d432d JB |
161 | u16 csum_size = |
162 | btrfs_super_csum_size(&root->fs_info->super_copy); | |
61b49440 CM |
163 | int ret; |
164 | struct btrfs_path *path; | |
165 | struct btrfs_csum_item *item = NULL; | |
166 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
167 | ||
168 | path = btrfs_alloc_path(); | |
4d1b5fb4 CM |
169 | if (bio->bi_size > PAGE_CACHE_SIZE * 8) |
170 | path->reada = 2; | |
61b49440 CM |
171 | |
172 | WARN_ON(bio->bi_vcnt <= 0); | |
173 | ||
d20f7043 | 174 | disk_bytenr = (u64)bio->bi_sector << 9; |
d397712b | 175 | while (bio_index < bio->bi_vcnt) { |
61b49440 | 176 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
d20f7043 | 177 | ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum); |
61b49440 CM |
178 | if (ret == 0) |
179 | goto found; | |
180 | ||
d20f7043 CM |
181 | if (!item || disk_bytenr < item_start_offset || |
182 | disk_bytenr >= item_last_offset) { | |
61b49440 CM |
183 | struct btrfs_key found_key; |
184 | u32 item_size; | |
185 | ||
186 | if (item) | |
187 | btrfs_release_path(root, path); | |
d20f7043 CM |
188 | item = btrfs_lookup_csum(NULL, root->fs_info->csum_root, |
189 | path, disk_bytenr, 0); | |
61b49440 CM |
190 | if (IS_ERR(item)) { |
191 | ret = PTR_ERR(item); | |
192 | if (ret == -ENOENT || ret == -EFBIG) | |
193 | ret = 0; | |
194 | sum = 0; | |
17d217fe YZ |
195 | if (BTRFS_I(inode)->root->root_key.objectid == |
196 | BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
197 | set_extent_bits(io_tree, offset, | |
198 | offset + bvec->bv_len - 1, | |
199 | EXTENT_NODATASUM, GFP_NOFS); | |
200 | } else { | |
d397712b CM |
201 | printk(KERN_INFO "btrfs no csum found " |
202 | "for inode %lu start %llu\n", | |
203 | inode->i_ino, | |
17d217fe YZ |
204 | (unsigned long long)offset); |
205 | } | |
6dab8157 | 206 | item = NULL; |
39be25cd | 207 | btrfs_release_path(root, path); |
61b49440 CM |
208 | goto found; |
209 | } | |
210 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
211 | path->slots[0]); | |
212 | ||
213 | item_start_offset = found_key.offset; | |
214 | item_size = btrfs_item_size_nr(path->nodes[0], | |
215 | path->slots[0]); | |
216 | item_last_offset = item_start_offset + | |
607d432d | 217 | (item_size / csum_size) * |
61b49440 CM |
218 | root->sectorsize; |
219 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
220 | struct btrfs_csum_item); | |
221 | } | |
222 | /* | |
223 | * this byte range must be able to fit inside | |
224 | * a single leaf so it will also fit inside a u32 | |
225 | */ | |
d20f7043 | 226 | diff = disk_bytenr - item_start_offset; |
61b49440 | 227 | diff = diff / root->sectorsize; |
607d432d | 228 | diff = diff * csum_size; |
61b49440 CM |
229 | |
230 | read_extent_buffer(path->nodes[0], &sum, | |
3de9d6b6 | 231 | ((unsigned long)item) + diff, |
607d432d | 232 | csum_size); |
61b49440 | 233 | found: |
d20f7043 CM |
234 | if (dst) |
235 | *dst++ = sum; | |
236 | else | |
237 | set_state_private(io_tree, offset, sum); | |
238 | disk_bytenr += bvec->bv_len; | |
61b49440 CM |
239 | bio_index++; |
240 | bvec++; | |
241 | } | |
242 | btrfs_free_path(path); | |
243 | return 0; | |
244 | } | |
245 | ||
17d217fe YZ |
246 | int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, |
247 | struct list_head *list) | |
248 | { | |
249 | struct btrfs_key key; | |
250 | struct btrfs_path *path; | |
251 | struct extent_buffer *leaf; | |
252 | struct btrfs_ordered_sum *sums; | |
253 | struct btrfs_sector_sum *sector_sum; | |
254 | struct btrfs_csum_item *item; | |
255 | unsigned long offset; | |
256 | int ret; | |
257 | size_t size; | |
258 | u64 csum_end; | |
259 | u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy); | |
260 | ||
261 | path = btrfs_alloc_path(); | |
262 | BUG_ON(!path); | |
263 | ||
264 | key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; | |
265 | key.offset = start; | |
266 | key.type = BTRFS_EXTENT_CSUM_KEY; | |
267 | ||
07d400a6 | 268 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
17d217fe YZ |
269 | if (ret < 0) |
270 | goto fail; | |
271 | if (ret > 0 && path->slots[0] > 0) { | |
272 | leaf = path->nodes[0]; | |
273 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); | |
274 | if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && | |
275 | key.type == BTRFS_EXTENT_CSUM_KEY) { | |
276 | offset = (start - key.offset) >> | |
277 | root->fs_info->sb->s_blocksize_bits; | |
278 | if (offset * csum_size < | |
279 | btrfs_item_size_nr(leaf, path->slots[0] - 1)) | |
280 | path->slots[0]--; | |
281 | } | |
282 | } | |
283 | ||
284 | while (start <= end) { | |
285 | leaf = path->nodes[0]; | |
286 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
07d400a6 | 287 | ret = btrfs_next_leaf(root, path); |
17d217fe YZ |
288 | if (ret < 0) |
289 | goto fail; | |
290 | if (ret > 0) | |
291 | break; | |
292 | leaf = path->nodes[0]; | |
293 | } | |
294 | ||
295 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
296 | if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || | |
297 | key.type != BTRFS_EXTENT_CSUM_KEY) | |
298 | break; | |
299 | ||
300 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
301 | if (key.offset > end) | |
302 | break; | |
303 | ||
304 | if (key.offset > start) | |
305 | start = key.offset; | |
306 | ||
307 | size = btrfs_item_size_nr(leaf, path->slots[0]); | |
308 | csum_end = key.offset + (size / csum_size) * root->sectorsize; | |
87b29b20 YZ |
309 | if (csum_end <= start) { |
310 | path->slots[0]++; | |
311 | continue; | |
312 | } | |
17d217fe | 313 | |
07d400a6 | 314 | csum_end = min(csum_end, end + 1); |
17d217fe YZ |
315 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
316 | struct btrfs_csum_item); | |
07d400a6 YZ |
317 | while (start < csum_end) { |
318 | size = min_t(size_t, csum_end - start, | |
319 | MAX_ORDERED_SUM_BYTES(root)); | |
320 | sums = kzalloc(btrfs_ordered_sum_size(root, size), | |
321 | GFP_NOFS); | |
322 | BUG_ON(!sums); | |
17d217fe | 323 | |
07d400a6 YZ |
324 | sector_sum = sums->sums; |
325 | sums->bytenr = start; | |
326 | sums->len = size; | |
327 | ||
328 | offset = (start - key.offset) >> | |
329 | root->fs_info->sb->s_blocksize_bits; | |
330 | offset *= csum_size; | |
331 | ||
332 | while (size > 0) { | |
333 | read_extent_buffer(path->nodes[0], | |
334 | §or_sum->sum, | |
335 | ((unsigned long)item) + | |
336 | offset, csum_size); | |
337 | sector_sum->bytenr = start; | |
338 | ||
339 | size -= root->sectorsize; | |
340 | start += root->sectorsize; | |
341 | offset += csum_size; | |
342 | sector_sum++; | |
343 | } | |
344 | list_add_tail(&sums->list, list); | |
345 | } | |
17d217fe YZ |
346 | path->slots[0]++; |
347 | } | |
348 | ret = 0; | |
349 | fail: | |
350 | btrfs_free_path(path); | |
351 | return ret; | |
352 | } | |
353 | ||
3edf7d33 | 354 | int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, |
d20f7043 | 355 | struct bio *bio, u64 file_start, int contig) |
e015640f | 356 | { |
e6dcd2dc CM |
357 | struct btrfs_ordered_sum *sums; |
358 | struct btrfs_sector_sum *sector_sum; | |
3edf7d33 | 359 | struct btrfs_ordered_extent *ordered; |
e015640f CM |
360 | char *data; |
361 | struct bio_vec *bvec = bio->bi_io_vec; | |
362 | int bio_index = 0; | |
3edf7d33 CM |
363 | unsigned long total_bytes = 0; |
364 | unsigned long this_sum_bytes = 0; | |
365 | u64 offset; | |
d20f7043 | 366 | u64 disk_bytenr; |
e015640f | 367 | |
e6dcd2dc CM |
368 | WARN_ON(bio->bi_vcnt <= 0); |
369 | sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); | |
e015640f CM |
370 | if (!sums) |
371 | return -ENOMEM; | |
3edf7d33 | 372 | |
ed98b56a | 373 | sector_sum = sums->sums; |
d20f7043 | 374 | disk_bytenr = (u64)bio->bi_sector << 9; |
e6dcd2dc CM |
375 | sums->len = bio->bi_size; |
376 | INIT_LIST_HEAD(&sums->list); | |
d20f7043 CM |
377 | |
378 | if (contig) | |
379 | offset = file_start; | |
380 | else | |
381 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; | |
382 | ||
383 | ordered = btrfs_lookup_ordered_extent(inode, offset); | |
3edf7d33 | 384 | BUG_ON(!ordered); |
d20f7043 | 385 | sums->bytenr = ordered->start; |
e015640f | 386 | |
d397712b | 387 | while (bio_index < bio->bi_vcnt) { |
d20f7043 CM |
388 | if (!contig) |
389 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; | |
390 | ||
391 | if (!contig && (offset >= ordered->file_offset + ordered->len || | |
392 | offset < ordered->file_offset)) { | |
3edf7d33 CM |
393 | unsigned long bytes_left; |
394 | sums->len = this_sum_bytes; | |
395 | this_sum_bytes = 0; | |
396 | btrfs_add_ordered_sum(inode, ordered, sums); | |
397 | btrfs_put_ordered_extent(ordered); | |
398 | ||
399 | bytes_left = bio->bi_size - total_bytes; | |
400 | ||
401 | sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), | |
402 | GFP_NOFS); | |
403 | BUG_ON(!sums); | |
ed98b56a | 404 | sector_sum = sums->sums; |
3edf7d33 | 405 | sums->len = bytes_left; |
d20f7043 | 406 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
3edf7d33 | 407 | BUG_ON(!ordered); |
d20f7043 | 408 | sums->bytenr = ordered->start; |
3edf7d33 CM |
409 | } |
410 | ||
e015640f | 411 | data = kmap_atomic(bvec->bv_page, KM_USER0); |
e6dcd2dc CM |
412 | sector_sum->sum = ~(u32)0; |
413 | sector_sum->sum = btrfs_csum_data(root, | |
414 | data + bvec->bv_offset, | |
415 | sector_sum->sum, | |
416 | bvec->bv_len); | |
e015640f | 417 | kunmap_atomic(data, KM_USER0); |
e6dcd2dc CM |
418 | btrfs_csum_final(sector_sum->sum, |
419 | (char *)§or_sum->sum); | |
d20f7043 | 420 | sector_sum->bytenr = disk_bytenr; |
ed98b56a | 421 | |
e6dcd2dc | 422 | sector_sum++; |
e015640f | 423 | bio_index++; |
3edf7d33 CM |
424 | total_bytes += bvec->bv_len; |
425 | this_sum_bytes += bvec->bv_len; | |
d20f7043 CM |
426 | disk_bytenr += bvec->bv_len; |
427 | offset += bvec->bv_len; | |
e015640f CM |
428 | bvec++; |
429 | } | |
ed98b56a | 430 | this_sum_bytes = 0; |
3edf7d33 CM |
431 | btrfs_add_ordered_sum(inode, ordered, sums); |
432 | btrfs_put_ordered_extent(ordered); | |
e015640f CM |
433 | return 0; |
434 | } | |
435 | ||
459931ec CM |
436 | /* |
437 | * helper function for csum removal, this expects the | |
438 | * key to describe the csum pointed to by the path, and it expects | |
439 | * the csum to overlap the range [bytenr, len] | |
440 | * | |
441 | * The csum should not be entirely contained in the range and the | |
442 | * range should not be entirely contained in the csum. | |
443 | * | |
444 | * This calls btrfs_truncate_item with the correct args based on the | |
445 | * overlap, and fixes up the key as required. | |
446 | */ | |
447 | static noinline int truncate_one_csum(struct btrfs_trans_handle *trans, | |
448 | struct btrfs_root *root, | |
449 | struct btrfs_path *path, | |
450 | struct btrfs_key *key, | |
451 | u64 bytenr, u64 len) | |
452 | { | |
453 | struct extent_buffer *leaf; | |
454 | u16 csum_size = | |
455 | btrfs_super_csum_size(&root->fs_info->super_copy); | |
456 | u64 csum_end; | |
457 | u64 end_byte = bytenr + len; | |
458 | u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits; | |
459 | int ret; | |
460 | ||
461 | leaf = path->nodes[0]; | |
462 | csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; | |
463 | csum_end <<= root->fs_info->sb->s_blocksize_bits; | |
464 | csum_end += key->offset; | |
465 | ||
466 | if (key->offset < bytenr && csum_end <= end_byte) { | |
467 | /* | |
468 | * [ bytenr - len ] | |
469 | * [ ] | |
470 | * [csum ] | |
471 | * A simple truncate off the end of the item | |
472 | */ | |
473 | u32 new_size = (bytenr - key->offset) >> blocksize_bits; | |
474 | new_size *= csum_size; | |
475 | ret = btrfs_truncate_item(trans, root, path, new_size, 1); | |
476 | BUG_ON(ret); | |
477 | } else if (key->offset >= bytenr && csum_end > end_byte && | |
478 | end_byte > key->offset) { | |
479 | /* | |
480 | * [ bytenr - len ] | |
481 | * [ ] | |
482 | * [csum ] | |
483 | * we need to truncate from the beginning of the csum | |
484 | */ | |
485 | u32 new_size = (csum_end - end_byte) >> blocksize_bits; | |
486 | new_size *= csum_size; | |
487 | ||
488 | ret = btrfs_truncate_item(trans, root, path, new_size, 0); | |
489 | BUG_ON(ret); | |
490 | ||
491 | key->offset = end_byte; | |
492 | ret = btrfs_set_item_key_safe(trans, root, path, key); | |
493 | BUG_ON(ret); | |
494 | } else { | |
495 | BUG(); | |
496 | } | |
497 | return 0; | |
498 | } | |
499 | ||
500 | /* | |
501 | * deletes the csum items from the csum tree for a given | |
502 | * range of bytes. | |
503 | */ | |
504 | int btrfs_del_csums(struct btrfs_trans_handle *trans, | |
505 | struct btrfs_root *root, u64 bytenr, u64 len) | |
506 | { | |
507 | struct btrfs_path *path; | |
508 | struct btrfs_key key; | |
509 | u64 end_byte = bytenr + len; | |
510 | u64 csum_end; | |
511 | struct extent_buffer *leaf; | |
512 | int ret; | |
513 | u16 csum_size = | |
514 | btrfs_super_csum_size(&root->fs_info->super_copy); | |
515 | int blocksize_bits = root->fs_info->sb->s_blocksize_bits; | |
516 | ||
517 | root = root->fs_info->csum_root; | |
518 | ||
519 | path = btrfs_alloc_path(); | |
520 | ||
d397712b | 521 | while (1) { |
459931ec CM |
522 | key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
523 | key.offset = end_byte - 1; | |
524 | key.type = BTRFS_EXTENT_CSUM_KEY; | |
525 | ||
526 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
527 | if (ret > 0) { | |
528 | if (path->slots[0] == 0) | |
529 | goto out; | |
530 | path->slots[0]--; | |
531 | } | |
532 | leaf = path->nodes[0]; | |
533 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
534 | ||
535 | if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || | |
536 | key.type != BTRFS_EXTENT_CSUM_KEY) { | |
537 | break; | |
538 | } | |
539 | ||
540 | if (key.offset >= end_byte) | |
541 | break; | |
542 | ||
543 | csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; | |
544 | csum_end <<= blocksize_bits; | |
545 | csum_end += key.offset; | |
546 | ||
547 | /* this csum ends before we start, we're done */ | |
548 | if (csum_end <= bytenr) | |
549 | break; | |
550 | ||
551 | /* delete the entire item, it is inside our range */ | |
552 | if (key.offset >= bytenr && csum_end <= end_byte) { | |
553 | ret = btrfs_del_item(trans, root, path); | |
554 | BUG_ON(ret); | |
dcbdd4dc CM |
555 | if (key.offset == bytenr) |
556 | break; | |
459931ec CM |
557 | } else if (key.offset < bytenr && csum_end > end_byte) { |
558 | unsigned long offset; | |
559 | unsigned long shift_len; | |
560 | unsigned long item_offset; | |
561 | /* | |
562 | * [ bytenr - len ] | |
563 | * [csum ] | |
564 | * | |
565 | * Our bytes are in the middle of the csum, | |
566 | * we need to split this item and insert a new one. | |
567 | * | |
568 | * But we can't drop the path because the | |
569 | * csum could change, get removed, extended etc. | |
570 | * | |
571 | * The trick here is the max size of a csum item leaves | |
572 | * enough room in the tree block for a single | |
573 | * item header. So, we split the item in place, | |
574 | * adding a new header pointing to the existing | |
575 | * bytes. Then we loop around again and we have | |
576 | * a nicely formed csum item that we can neatly | |
577 | * truncate. | |
578 | */ | |
579 | offset = (bytenr - key.offset) >> blocksize_bits; | |
580 | offset *= csum_size; | |
581 | ||
582 | shift_len = (len >> blocksize_bits) * csum_size; | |
583 | ||
584 | item_offset = btrfs_item_ptr_offset(leaf, | |
585 | path->slots[0]); | |
586 | ||
587 | memset_extent_buffer(leaf, 0, item_offset + offset, | |
588 | shift_len); | |
589 | key.offset = bytenr; | |
590 | ||
591 | /* | |
592 | * btrfs_split_item returns -EAGAIN when the | |
593 | * item changed size or key | |
594 | */ | |
595 | ret = btrfs_split_item(trans, root, path, &key, offset); | |
596 | BUG_ON(ret && ret != -EAGAIN); | |
597 | ||
598 | key.offset = end_byte - 1; | |
599 | } else { | |
600 | ret = truncate_one_csum(trans, root, path, | |
601 | &key, bytenr, len); | |
602 | BUG_ON(ret); | |
dcbdd4dc CM |
603 | if (key.offset < bytenr) |
604 | break; | |
459931ec CM |
605 | } |
606 | btrfs_release_path(root, path); | |
607 | } | |
608 | out: | |
609 | btrfs_free_path(path); | |
610 | return 0; | |
611 | } | |
612 | ||
065631f6 | 613 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, |
d20f7043 | 614 | struct btrfs_root *root, |
e6dcd2dc | 615 | struct btrfs_ordered_sum *sums) |
f254e52c | 616 | { |
d20f7043 | 617 | u64 bytenr; |
f254e52c CM |
618 | int ret; |
619 | struct btrfs_key file_key; | |
6567e837 | 620 | struct btrfs_key found_key; |
065631f6 | 621 | u64 next_offset; |
e6dcd2dc | 622 | u64 total_bytes = 0; |
065631f6 | 623 | int found_next; |
5caf2a00 | 624 | struct btrfs_path *path; |
f254e52c | 625 | struct btrfs_csum_item *item; |
065631f6 | 626 | struct btrfs_csum_item *item_end; |
ff79f819 | 627 | struct extent_buffer *leaf = NULL; |
6567e837 | 628 | u64 csum_offset; |
e6dcd2dc | 629 | struct btrfs_sector_sum *sector_sum; |
f578d4bd CM |
630 | u32 nritems; |
631 | u32 ins_size; | |
6e92f5e6 CM |
632 | char *eb_map; |
633 | char *eb_token; | |
634 | unsigned long map_len; | |
635 | unsigned long map_start; | |
607d432d JB |
636 | u16 csum_size = |
637 | btrfs_super_csum_size(&root->fs_info->super_copy); | |
6e92f5e6 | 638 | |
5caf2a00 CM |
639 | path = btrfs_alloc_path(); |
640 | BUG_ON(!path); | |
ed98b56a | 641 | sector_sum = sums->sums; |
065631f6 CM |
642 | again: |
643 | next_offset = (u64)-1; | |
644 | found_next = 0; | |
d20f7043 CM |
645 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
646 | file_key.offset = sector_sum->bytenr; | |
647 | bytenr = sector_sum->bytenr; | |
648 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); | |
a429e513 | 649 | |
d20f7043 | 650 | item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1); |
ff79f819 CM |
651 | if (!IS_ERR(item)) { |
652 | leaf = path->nodes[0]; | |
639cb586 | 653 | ret = 0; |
a429e513 | 654 | goto found; |
ff79f819 | 655 | } |
a429e513 CM |
656 | ret = PTR_ERR(item); |
657 | if (ret == -EFBIG) { | |
658 | u32 item_size; | |
659 | /* we found one, but it isn't big enough yet */ | |
5f39d397 CM |
660 | leaf = path->nodes[0]; |
661 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
607d432d JB |
662 | if ((item_size / csum_size) >= |
663 | MAX_CSUM_ITEMS(root, csum_size)) { | |
a429e513 CM |
664 | /* already at max size, make a new one */ |
665 | goto insert; | |
666 | } | |
667 | } else { | |
f578d4bd | 668 | int slot = path->slots[0] + 1; |
a429e513 | 669 | /* we didn't find a csum item, insert one */ |
f578d4bd CM |
670 | nritems = btrfs_header_nritems(path->nodes[0]); |
671 | if (path->slots[0] >= nritems - 1) { | |
672 | ret = btrfs_next_leaf(root, path); | |
b56baf5b | 673 | if (ret == 1) |
f578d4bd | 674 | found_next = 1; |
b56baf5b | 675 | if (ret != 0) |
f578d4bd | 676 | goto insert; |
b56baf5b | 677 | slot = 0; |
f578d4bd CM |
678 | } |
679 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); | |
d20f7043 CM |
680 | if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
681 | found_key.type != BTRFS_EXTENT_CSUM_KEY) { | |
f578d4bd CM |
682 | found_next = 1; |
683 | goto insert; | |
684 | } | |
685 | next_offset = found_key.offset; | |
686 | found_next = 1; | |
a429e513 CM |
687 | goto insert; |
688 | } | |
689 | ||
690 | /* | |
691 | * at this point, we know the tree has an item, but it isn't big | |
692 | * enough yet to put our csum in. Grow it | |
693 | */ | |
694 | btrfs_release_path(root, path); | |
6567e837 | 695 | ret = btrfs_search_slot(trans, root, &file_key, path, |
607d432d | 696 | csum_size, 1); |
6567e837 | 697 | if (ret < 0) |
53863232 | 698 | goto fail_unlock; |
459931ec CM |
699 | |
700 | if (ret > 0) { | |
701 | if (path->slots[0] == 0) | |
702 | goto insert; | |
703 | path->slots[0]--; | |
6567e837 | 704 | } |
459931ec | 705 | |
5f39d397 CM |
706 | leaf = path->nodes[0]; |
707 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
d20f7043 | 708 | csum_offset = (bytenr - found_key.offset) >> |
6567e837 | 709 | root->fs_info->sb->s_blocksize_bits; |
459931ec | 710 | |
d20f7043 CM |
711 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY || |
712 | found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || | |
607d432d | 713 | csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) { |
6567e837 CM |
714 | goto insert; |
715 | } | |
459931ec | 716 | |
5f39d397 | 717 | if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / |
607d432d JB |
718 | csum_size) { |
719 | u32 diff = (csum_offset + 1) * csum_size; | |
459931ec CM |
720 | |
721 | /* | |
722 | * is the item big enough already? we dropped our lock | |
723 | * before and need to recheck | |
724 | */ | |
725 | if (diff < btrfs_item_size_nr(leaf, path->slots[0])) | |
726 | goto csum; | |
727 | ||
5f39d397 | 728 | diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); |
d397712b | 729 | if (diff != csum_size) |
3a686375 | 730 | goto insert; |
459931ec | 731 | |
a429e513 | 732 | ret = btrfs_extend_item(trans, root, path, diff); |
6567e837 CM |
733 | BUG_ON(ret); |
734 | goto csum; | |
735 | } | |
736 | ||
737 | insert: | |
a429e513 | 738 | btrfs_release_path(root, path); |
6567e837 | 739 | csum_offset = 0; |
f578d4bd | 740 | if (found_next) { |
d20f7043 CM |
741 | u64 tmp = total_bytes + root->sectorsize; |
742 | u64 next_sector = sector_sum->bytenr; | |
743 | struct btrfs_sector_sum *next = sector_sum + 1; | |
744 | ||
d397712b | 745 | while (tmp < sums->len) { |
d20f7043 CM |
746 | if (next_sector + root->sectorsize != next->bytenr) |
747 | break; | |
748 | tmp += root->sectorsize; | |
749 | next_sector = next->bytenr; | |
750 | next++; | |
751 | } | |
752 | tmp = min(tmp, next_offset - file_key.offset); | |
f578d4bd CM |
753 | tmp >>= root->fs_info->sb->s_blocksize_bits; |
754 | tmp = max((u64)1, tmp); | |
607d432d JB |
755 | tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size)); |
756 | ins_size = csum_size * tmp; | |
f578d4bd | 757 | } else { |
607d432d | 758 | ins_size = csum_size; |
f578d4bd | 759 | } |
5caf2a00 | 760 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
f578d4bd | 761 | ins_size); |
54aa1f4d | 762 | if (ret < 0) |
53863232 | 763 | goto fail_unlock; |
a429e513 | 764 | if (ret != 0) { |
a429e513 | 765 | WARN_ON(1); |
53863232 | 766 | goto fail_unlock; |
a429e513 | 767 | } |
6567e837 | 768 | csum: |
5f39d397 CM |
769 | leaf = path->nodes[0]; |
770 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); | |
f254e52c | 771 | ret = 0; |
509659cd | 772 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
607d432d | 773 | csum_offset * csum_size); |
b18c6685 | 774 | found: |
065631f6 CM |
775 | item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
776 | item_end = (struct btrfs_csum_item *)((unsigned char *)item_end + | |
777 | btrfs_item_size_nr(leaf, path->slots[0])); | |
6e92f5e6 | 778 | eb_token = NULL; |
53863232 | 779 | cond_resched(); |
e6dcd2dc | 780 | next_sector: |
aadfeb6e | 781 | |
6e92f5e6 | 782 | if (!eb_token || |
607d432d | 783 | (unsigned long)item + csum_size >= map_start + map_len) { |
6e92f5e6 CM |
784 | int err; |
785 | ||
786 | if (eb_token) | |
eb20978f | 787 | unmap_extent_buffer(leaf, eb_token, KM_USER1); |
6e92f5e6 CM |
788 | eb_token = NULL; |
789 | err = map_private_extent_buffer(leaf, (unsigned long)item, | |
607d432d | 790 | csum_size, |
6e92f5e6 | 791 | &eb_token, &eb_map, |
eb20978f | 792 | &map_start, &map_len, KM_USER1); |
6e92f5e6 CM |
793 | if (err) |
794 | eb_token = NULL; | |
795 | } | |
796 | if (eb_token) { | |
797 | memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)), | |
607d432d | 798 | §or_sum->sum, csum_size); |
6e92f5e6 | 799 | } else { |
e6dcd2dc | 800 | write_extent_buffer(leaf, §or_sum->sum, |
607d432d | 801 | (unsigned long)item, csum_size); |
6e92f5e6 | 802 | } |
7f3c74fb | 803 | |
e6dcd2dc CM |
804 | total_bytes += root->sectorsize; |
805 | sector_sum++; | |
806 | if (total_bytes < sums->len) { | |
6e92f5e6 | 807 | item = (struct btrfs_csum_item *)((char *)item + |
607d432d | 808 | csum_size); |
d20f7043 CM |
809 | if (item < item_end && bytenr + PAGE_CACHE_SIZE == |
810 | sector_sum->bytenr) { | |
811 | bytenr = sector_sum->bytenr; | |
e6dcd2dc | 812 | goto next_sector; |
2e1a992e | 813 | } |
065631f6 | 814 | } |
6e92f5e6 | 815 | if (eb_token) { |
eb20978f | 816 | unmap_extent_buffer(leaf, eb_token, KM_USER1); |
6e92f5e6 CM |
817 | eb_token = NULL; |
818 | } | |
5caf2a00 | 819 | btrfs_mark_buffer_dirty(path->nodes[0]); |
53863232 | 820 | cond_resched(); |
e6dcd2dc | 821 | if (total_bytes < sums->len) { |
065631f6 CM |
822 | btrfs_release_path(root, path); |
823 | goto again; | |
824 | } | |
53863232 | 825 | out: |
5caf2a00 | 826 | btrfs_free_path(path); |
f254e52c | 827 | return ret; |
53863232 CM |
828 | |
829 | fail_unlock: | |
53863232 | 830 | goto out; |
f254e52c | 831 | } |