diff options
Diffstat (limited to 'fs/btrfs/file-item.c')
-rw-r--r-- | fs/btrfs/file-item.c | 831 |
1 files changed, 831 insertions, 0 deletions
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c new file mode 100644 index 000000000000..964652435fd1 --- /dev/null +++ b/fs/btrfs/file-item.c | |||
@@ -0,0 +1,831 @@ | |||
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 | |||
19 | #include <linux/bio.h> | ||
20 | #include <linux/pagemap.h> | ||
21 | #include <linux/highmem.h> | ||
22 | #include "ctree.h" | ||
23 | #include "disk-io.h" | ||
24 | #include "transaction.h" | ||
25 | #include "print-tree.h" | ||
26 | |||
27 | #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \ | ||
28 | sizeof(struct btrfs_item) * 2) / \ | ||
29 | size) - 1)) | ||
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 | |||
36 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, | ||
37 | struct btrfs_root *root, | ||
38 | u64 objectid, u64 pos, | ||
39 | u64 disk_offset, u64 disk_num_bytes, | ||
40 | u64 num_bytes, u64 offset, u64 ram_bytes, | ||
41 | u8 compression, u8 encryption, u16 other_encoding) | ||
42 | { | ||
43 | int ret = 0; | ||
44 | struct btrfs_file_extent_item *item; | ||
45 | struct btrfs_key file_key; | ||
46 | struct btrfs_path *path; | ||
47 | struct extent_buffer *leaf; | ||
48 | |||
49 | path = btrfs_alloc_path(); | ||
50 | BUG_ON(!path); | ||
51 | file_key.objectid = objectid; | ||
52 | file_key.offset = pos; | ||
53 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); | ||
54 | |||
55 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, | ||
56 | sizeof(*item)); | ||
57 | if (ret < 0) | ||
58 | goto out; | ||
59 | BUG_ON(ret); | ||
60 | leaf = path->nodes[0]; | ||
61 | item = btrfs_item_ptr(leaf, path->slots[0], | ||
62 | struct btrfs_file_extent_item); | ||
63 | btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); | ||
64 | btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); | ||
65 | btrfs_set_file_extent_offset(leaf, item, offset); | ||
66 | btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); | ||
67 | btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); | ||
68 | btrfs_set_file_extent_generation(leaf, item, trans->transid); | ||
69 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); | ||
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 | |||
74 | btrfs_mark_buffer_dirty(leaf); | ||
75 | out: | ||
76 | btrfs_free_path(path); | ||
77 | return ret; | ||
78 | } | ||
79 | |||
80 | struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, | ||
81 | struct btrfs_root *root, | ||
82 | struct btrfs_path *path, | ||
83 | u64 bytenr, int cow) | ||
84 | { | ||
85 | int ret; | ||
86 | struct btrfs_key file_key; | ||
87 | struct btrfs_key found_key; | ||
88 | struct btrfs_csum_item *item; | ||
89 | struct extent_buffer *leaf; | ||
90 | u64 csum_offset = 0; | ||
91 | u16 csum_size = | ||
92 | btrfs_super_csum_size(&root->fs_info->super_copy); | ||
93 | int csums_in_item; | ||
94 | |||
95 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; | ||
96 | file_key.offset = bytenr; | ||
97 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); | ||
98 | ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); | ||
99 | if (ret < 0) | ||
100 | goto fail; | ||
101 | leaf = path->nodes[0]; | ||
102 | if (ret > 0) { | ||
103 | ret = 1; | ||
104 | if (path->slots[0] == 0) | ||
105 | goto fail; | ||
106 | path->slots[0]--; | ||
107 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | ||
108 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY) | ||
109 | goto fail; | ||
110 | |||
111 | csum_offset = (bytenr - found_key.offset) >> | ||
112 | root->fs_info->sb->s_blocksize_bits; | ||
113 | csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); | ||
114 | csums_in_item /= csum_size; | ||
115 | |||
116 | if (csum_offset >= csums_in_item) { | ||
117 | ret = -EFBIG; | ||
118 | goto fail; | ||
119 | } | ||
120 | } | ||
121 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); | ||
122 | item = (struct btrfs_csum_item *)((unsigned char *)item + | ||
123 | csum_offset * csum_size); | ||
124 | return item; | ||
125 | fail: | ||
126 | if (ret > 0) | ||
127 | ret = -ENOENT; | ||
128 | return ERR_PTR(ret); | ||
129 | } | ||
130 | |||
131 | |||
132 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, | ||
133 | struct btrfs_root *root, | ||
134 | struct btrfs_path *path, u64 objectid, | ||
135 | u64 offset, int mod) | ||
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; | ||
143 | file_key.offset = offset; | ||
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 | } | ||
148 | |||
149 | |||
150 | int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, | ||
151 | struct bio *bio, u32 *dst) | ||
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; | ||
159 | u64 disk_bytenr; | ||
160 | u32 diff; | ||
161 | u16 csum_size = | ||
162 | btrfs_super_csum_size(&root->fs_info->super_copy); | ||
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(); | ||
169 | if (bio->bi_size > PAGE_CACHE_SIZE * 8) | ||
170 | path->reada = 2; | ||
171 | |||
172 | WARN_ON(bio->bi_vcnt <= 0); | ||
173 | |||
174 | disk_bytenr = (u64)bio->bi_sector << 9; | ||
175 | while (bio_index < bio->bi_vcnt) { | ||
176 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; | ||
177 | ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum); | ||
178 | if (ret == 0) | ||
179 | goto found; | ||
180 | |||
181 | if (!item || disk_bytenr < item_start_offset || | ||
182 | disk_bytenr >= item_last_offset) { | ||
183 | struct btrfs_key found_key; | ||
184 | u32 item_size; | ||
185 | |||
186 | if (item) | ||
187 | btrfs_release_path(root, path); | ||
188 | item = btrfs_lookup_csum(NULL, root->fs_info->csum_root, | ||
189 | path, disk_bytenr, 0); | ||
190 | if (IS_ERR(item)) { | ||
191 | ret = PTR_ERR(item); | ||
192 | if (ret == -ENOENT || ret == -EFBIG) | ||
193 | ret = 0; | ||
194 | sum = 0; | ||
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 { | ||
201 | printk(KERN_INFO "btrfs no csum found " | ||
202 | "for inode %lu start %llu\n", | ||
203 | inode->i_ino, | ||
204 | (unsigned long long)offset); | ||
205 | } | ||
206 | item = NULL; | ||
207 | btrfs_release_path(root, path); | ||
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 + | ||
217 | (item_size / csum_size) * | ||
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 | */ | ||
226 | diff = disk_bytenr - item_start_offset; | ||
227 | diff = diff / root->sectorsize; | ||
228 | diff = diff * csum_size; | ||
229 | |||
230 | read_extent_buffer(path->nodes[0], &sum, | ||
231 | ((unsigned long)item) + diff, | ||
232 | csum_size); | ||
233 | found: | ||
234 | if (dst) | ||
235 | *dst++ = sum; | ||
236 | else | ||
237 | set_state_private(io_tree, offset, sum); | ||
238 | disk_bytenr += bvec->bv_len; | ||
239 | bio_index++; | ||
240 | bvec++; | ||
241 | } | ||
242 | btrfs_free_path(path); | ||
243 | return 0; | ||
244 | } | ||
245 | |||
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 | |||
268 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | ||
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)) { | ||
287 | ret = btrfs_next_leaf(root, path); | ||
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; | ||
309 | if (csum_end <= start) { | ||
310 | path->slots[0]++; | ||
311 | continue; | ||
312 | } | ||
313 | |||
314 | csum_end = min(csum_end, end + 1); | ||
315 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], | ||
316 | struct btrfs_csum_item); | ||
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); | ||
323 | |||
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 | } | ||
346 | path->slots[0]++; | ||
347 | } | ||
348 | ret = 0; | ||
349 | fail: | ||
350 | btrfs_free_path(path); | ||
351 | return ret; | ||
352 | } | ||
353 | |||
354 | int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, | ||
355 | struct bio *bio, u64 file_start, int contig) | ||
356 | { | ||
357 | struct btrfs_ordered_sum *sums; | ||
358 | struct btrfs_sector_sum *sector_sum; | ||
359 | struct btrfs_ordered_extent *ordered; | ||
360 | char *data; | ||
361 | struct bio_vec *bvec = bio->bi_io_vec; | ||
362 | int bio_index = 0; | ||
363 | unsigned long total_bytes = 0; | ||
364 | unsigned long this_sum_bytes = 0; | ||
365 | u64 offset; | ||
366 | u64 disk_bytenr; | ||
367 | |||
368 | WARN_ON(bio->bi_vcnt <= 0); | ||
369 | sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); | ||
370 | if (!sums) | ||
371 | return -ENOMEM; | ||
372 | |||
373 | sector_sum = sums->sums; | ||
374 | disk_bytenr = (u64)bio->bi_sector << 9; | ||
375 | sums->len = bio->bi_size; | ||
376 | INIT_LIST_HEAD(&sums->list); | ||
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); | ||
384 | BUG_ON(!ordered); | ||
385 | sums->bytenr = ordered->start; | ||
386 | |||
387 | while (bio_index < bio->bi_vcnt) { | ||
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)) { | ||
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); | ||
404 | sector_sum = sums->sums; | ||
405 | sums->len = bytes_left; | ||
406 | ordered = btrfs_lookup_ordered_extent(inode, offset); | ||
407 | BUG_ON(!ordered); | ||
408 | sums->bytenr = ordered->start; | ||
409 | } | ||
410 | |||
411 | data = kmap_atomic(bvec->bv_page, KM_USER0); | ||
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); | ||
417 | kunmap_atomic(data, KM_USER0); | ||
418 | btrfs_csum_final(sector_sum->sum, | ||
419 | (char *)§or_sum->sum); | ||
420 | sector_sum->bytenr = disk_bytenr; | ||
421 | |||
422 | sector_sum++; | ||
423 | bio_index++; | ||
424 | total_bytes += bvec->bv_len; | ||
425 | this_sum_bytes += bvec->bv_len; | ||
426 | disk_bytenr += bvec->bv_len; | ||
427 | offset += bvec->bv_len; | ||
428 | bvec++; | ||
429 | } | ||
430 | this_sum_bytes = 0; | ||
431 | btrfs_add_ordered_sum(inode, ordered, sums); | ||
432 | btrfs_put_ordered_extent(ordered); | ||
433 | return 0; | ||
434 | } | ||
435 | |||
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 | |||
521 | while (1) { | ||
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); | ||
555 | if (key.offset == bytenr) | ||
556 | break; | ||
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); | ||
603 | if (key.offset < bytenr) | ||
604 | break; | ||
605 | } | ||
606 | btrfs_release_path(root, path); | ||
607 | } | ||
608 | out: | ||
609 | btrfs_free_path(path); | ||
610 | return 0; | ||
611 | } | ||
612 | |||
613 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, | ||
614 | struct btrfs_root *root, | ||
615 | struct btrfs_ordered_sum *sums) | ||
616 | { | ||
617 | u64 bytenr; | ||
618 | int ret; | ||
619 | struct btrfs_key file_key; | ||
620 | struct btrfs_key found_key; | ||
621 | u64 next_offset; | ||
622 | u64 total_bytes = 0; | ||
623 | int found_next; | ||
624 | struct btrfs_path *path; | ||
625 | struct btrfs_csum_item *item; | ||
626 | struct btrfs_csum_item *item_end; | ||
627 | struct extent_buffer *leaf = NULL; | ||
628 | u64 csum_offset; | ||
629 | struct btrfs_sector_sum *sector_sum; | ||
630 | u32 nritems; | ||
631 | u32 ins_size; | ||
632 | char *eb_map; | ||
633 | char *eb_token; | ||
634 | unsigned long map_len; | ||
635 | unsigned long map_start; | ||
636 | u16 csum_size = | ||
637 | btrfs_super_csum_size(&root->fs_info->super_copy); | ||
638 | |||
639 | path = btrfs_alloc_path(); | ||
640 | BUG_ON(!path); | ||
641 | sector_sum = sums->sums; | ||
642 | again: | ||
643 | next_offset = (u64)-1; | ||
644 | found_next = 0; | ||
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); | ||
649 | |||
650 | item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1); | ||
651 | if (!IS_ERR(item)) { | ||
652 | leaf = path->nodes[0]; | ||
653 | ret = 0; | ||
654 | goto found; | ||
655 | } | ||
656 | ret = PTR_ERR(item); | ||
657 | if (ret == -EFBIG) { | ||
658 | u32 item_size; | ||
659 | /* we found one, but it isn't big enough yet */ | ||
660 | leaf = path->nodes[0]; | ||
661 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | ||
662 | if ((item_size / csum_size) >= | ||
663 | MAX_CSUM_ITEMS(root, csum_size)) { | ||
664 | /* already at max size, make a new one */ | ||
665 | goto insert; | ||
666 | } | ||
667 | } else { | ||
668 | int slot = path->slots[0] + 1; | ||
669 | /* we didn't find a csum item, insert one */ | ||
670 | nritems = btrfs_header_nritems(path->nodes[0]); | ||
671 | if (path->slots[0] >= nritems - 1) { | ||
672 | ret = btrfs_next_leaf(root, path); | ||
673 | if (ret == 1) | ||
674 | found_next = 1; | ||
675 | if (ret != 0) | ||
676 | goto insert; | ||
677 | slot = 0; | ||
678 | } | ||
679 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); | ||
680 | if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || | ||
681 | found_key.type != BTRFS_EXTENT_CSUM_KEY) { | ||
682 | found_next = 1; | ||
683 | goto insert; | ||
684 | } | ||
685 | next_offset = found_key.offset; | ||
686 | found_next = 1; | ||
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); | ||
695 | ret = btrfs_search_slot(trans, root, &file_key, path, | ||
696 | csum_size, 1); | ||
697 | if (ret < 0) | ||
698 | goto fail_unlock; | ||
699 | |||
700 | if (ret > 0) { | ||
701 | if (path->slots[0] == 0) | ||
702 | goto insert; | ||
703 | path->slots[0]--; | ||
704 | } | ||
705 | |||
706 | leaf = path->nodes[0]; | ||
707 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | ||
708 | csum_offset = (bytenr - found_key.offset) >> | ||
709 | root->fs_info->sb->s_blocksize_bits; | ||
710 | |||
711 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY || | ||
712 | found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || | ||
713 | csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) { | ||
714 | goto insert; | ||
715 | } | ||
716 | |||
717 | if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / | ||
718 | csum_size) { | ||
719 | u32 diff = (csum_offset + 1) * csum_size; | ||
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 | |||
728 | diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); | ||
729 | if (diff != csum_size) | ||
730 | goto insert; | ||
731 | |||
732 | ret = btrfs_extend_item(trans, root, path, diff); | ||
733 | BUG_ON(ret); | ||
734 | goto csum; | ||
735 | } | ||
736 | |||
737 | insert: | ||
738 | btrfs_release_path(root, path); | ||
739 | csum_offset = 0; | ||
740 | if (found_next) { | ||
741 | u64 tmp = total_bytes + root->sectorsize; | ||
742 | u64 next_sector = sector_sum->bytenr; | ||
743 | struct btrfs_sector_sum *next = sector_sum + 1; | ||
744 | |||
745 | while (tmp < sums->len) { | ||
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); | ||
753 | tmp >>= root->fs_info->sb->s_blocksize_bits; | ||
754 | tmp = max((u64)1, tmp); | ||
755 | tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size)); | ||
756 | ins_size = csum_size * tmp; | ||
757 | } else { | ||
758 | ins_size = csum_size; | ||
759 | } | ||
760 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, | ||
761 | ins_size); | ||
762 | if (ret < 0) | ||
763 | goto fail_unlock; | ||
764 | if (ret != 0) { | ||
765 | WARN_ON(1); | ||
766 | goto fail_unlock; | ||
767 | } | ||
768 | csum: | ||
769 | leaf = path->nodes[0]; | ||
770 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); | ||
771 | ret = 0; | ||
772 | item = (struct btrfs_csum_item *)((unsigned char *)item + | ||
773 | csum_offset * csum_size); | ||
774 | found: | ||
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])); | ||
778 | eb_token = NULL; | ||
779 | cond_resched(); | ||
780 | next_sector: | ||
781 | |||
782 | if (!eb_token || | ||
783 | (unsigned long)item + csum_size >= map_start + map_len) { | ||
784 | int err; | ||
785 | |||
786 | if (eb_token) | ||
787 | unmap_extent_buffer(leaf, eb_token, KM_USER1); | ||
788 | eb_token = NULL; | ||
789 | err = map_private_extent_buffer(leaf, (unsigned long)item, | ||
790 | csum_size, | ||
791 | &eb_token, &eb_map, | ||
792 | &map_start, &map_len, KM_USER1); | ||
793 | if (err) | ||
794 | eb_token = NULL; | ||
795 | } | ||
796 | if (eb_token) { | ||
797 | memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)), | ||
798 | §or_sum->sum, csum_size); | ||
799 | } else { | ||
800 | write_extent_buffer(leaf, §or_sum->sum, | ||
801 | (unsigned long)item, csum_size); | ||
802 | } | ||
803 | |||
804 | total_bytes += root->sectorsize; | ||
805 | sector_sum++; | ||
806 | if (total_bytes < sums->len) { | ||
807 | item = (struct btrfs_csum_item *)((char *)item + | ||
808 | csum_size); | ||
809 | if (item < item_end && bytenr + PAGE_CACHE_SIZE == | ||
810 | sector_sum->bytenr) { | ||
811 | bytenr = sector_sum->bytenr; | ||
812 | goto next_sector; | ||
813 | } | ||
814 | } | ||
815 | if (eb_token) { | ||
816 | unmap_extent_buffer(leaf, eb_token, KM_USER1); | ||
817 | eb_token = NULL; | ||
818 | } | ||
819 | btrfs_mark_buffer_dirty(path->nodes[0]); | ||
820 | cond_resched(); | ||
821 | if (total_bytes < sums->len) { | ||
822 | btrfs_release_path(root, path); | ||
823 | goto again; | ||
824 | } | ||
825 | out: | ||
826 | btrfs_free_path(path); | ||
827 | return ret; | ||
828 | |||
829 | fail_unlock: | ||
830 | goto out; | ||
831 | } | ||