2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "transaction.h"
26 #include "print-tree.h"
29 #define MAX_CSUM_ITEMS(r,size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
30 sizeof(struct btrfs_item) * 2) / \
32 int btrfs_create_file(struct btrfs_trans_handle *trans,
33 struct btrfs_root *root, u64 dirid, u64 *objectid)
38 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
39 struct btrfs_root *root,
40 u64 objectid, u64 pos, u64 offset,
41 u64 disk_num_bytes, u64 num_bytes)
44 struct btrfs_file_extent_item *item;
45 struct btrfs_key file_key;
46 struct btrfs_path *path;
47 struct extent_buffer *leaf;
49 path = btrfs_alloc_path();
51 file_key.objectid = objectid;
52 file_key.offset = pos;
53 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
55 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
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, offset);
64 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
65 btrfs_set_file_extent_offset(leaf, item, 0);
66 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
67 btrfs_set_file_extent_ram_bytes(leaf, item, num_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, 0);
71 btrfs_set_file_extent_encryption(leaf, item, 0);
72 btrfs_set_file_extent_other_encoding(leaf, item, 0);
73 btrfs_mark_buffer_dirty(leaf);
75 btrfs_free_path(path);
79 int btrfs_insert_inline_extent(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root, u64 objectid,
81 u64 offset, char *buffer, size_t size)
84 struct btrfs_path *path;
85 struct extent_buffer *leaf;
87 struct btrfs_file_extent_item *ei;
92 path = btrfs_alloc_path();
96 key.objectid = objectid;
98 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
100 datasize = btrfs_file_extent_calc_inline_size(size);
101 ret = btrfs_insert_empty_item(trans, root, path, &key, datasize);
107 leaf = path->nodes[0];
108 ei = btrfs_item_ptr(leaf, path->slots[0],
109 struct btrfs_file_extent_item);
110 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
111 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
112 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
113 btrfs_set_file_extent_compression(leaf, ei, 0);
114 btrfs_set_file_extent_encryption(leaf, ei, 0);
115 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
117 ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
118 write_extent_buffer(leaf, buffer, ptr, size);
119 btrfs_mark_buffer_dirty(leaf);
121 btrfs_free_path(path);
125 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
126 struct btrfs_root *root,
127 struct btrfs_path *path,
131 struct btrfs_key file_key;
132 struct btrfs_key found_key;
133 struct btrfs_csum_item *item;
134 struct extent_buffer *leaf;
137 btrfs_super_csum_size(root->fs_info->super_copy);
140 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
141 file_key.offset = bytenr;
142 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
143 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
146 leaf = path->nodes[0];
149 if (path->slots[0] == 0)
152 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
153 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
156 csum_offset = (bytenr - found_key.offset) / root->sectorsize;
157 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
158 csums_in_item /= csum_size;
160 if (csum_offset >= csums_in_item) {
165 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
166 item = (struct btrfs_csum_item *)((unsigned char *)item +
167 csum_offset * csum_size);
175 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
176 struct btrfs_root *root,
177 struct btrfs_path *path, u64 objectid,
181 struct btrfs_key file_key;
182 int ins_len = mod < 0 ? -1 : 0;
185 file_key.objectid = objectid;
186 file_key.offset = offset;
187 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
188 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
192 int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
193 struct btrfs_root *root, u64 alloc_end,
194 u64 bytenr, char *data, size_t len)
197 struct btrfs_key file_key;
198 struct btrfs_key found_key;
199 u64 next_offset = (u64)-1;
201 struct btrfs_path *path;
202 struct btrfs_csum_item *item;
203 struct extent_buffer *leaf = NULL;
205 u32 csum_result = ~(u32)0;
209 btrfs_super_csum_size(root->fs_info->super_copy);
211 path = btrfs_alloc_path();
214 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
215 file_key.offset = bytenr;
216 file_key.type = BTRFS_EXTENT_CSUM_KEY;
218 item = btrfs_lookup_csum(trans, root, path, bytenr, 1);
220 leaf = path->nodes[0];
227 /* we found one, but it isn't big enough yet */
228 leaf = path->nodes[0];
229 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
230 if ((item_size / csum_size) >= MAX_CSUM_ITEMS(root, csum_size)) {
231 /* already at max size, make a new one */
235 int slot = path->slots[0] + 1;
236 /* we didn't find a csum item, insert one */
237 nritems = btrfs_header_nritems(path->nodes[0]);
238 if (path->slots[0] >= nritems - 1) {
239 ret = btrfs_next_leaf(root, path);
246 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
247 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
248 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
252 next_offset = found_key.offset;
258 * at this point, we know the tree has an item, but it isn't big
259 * enough yet to put our csum in. Grow it
261 btrfs_release_path(root, path);
262 ret = btrfs_search_slot(trans, root, &file_key, path,
269 if (path->slots[0] == 0) {
273 leaf = path->nodes[0];
274 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
275 csum_offset = (file_key.offset - found_key.offset) / root->sectorsize;
276 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
277 found_key.type != BTRFS_EXTENT_CSUM_KEY ||
278 csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
281 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
283 u32 diff = (csum_offset + 1) * csum_size;
284 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
285 if (diff != csum_size)
287 ret = btrfs_extend_item(trans, root, path, diff);
293 btrfs_release_path(root, path);
296 u64 tmp = min(alloc_end, next_offset);
297 tmp -= file_key.offset;
298 tmp /= root->sectorsize;
299 tmp = max((u64)1, tmp);
300 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
301 ins_size = csum_size * tmp;
303 ins_size = csum_size;
305 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
314 leaf = path->nodes[0];
315 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
317 item = (struct btrfs_csum_item *)((unsigned char *)item +
318 csum_offset * csum_size);
320 csum_result = btrfs_csum_data(root, data, csum_result, len);
321 btrfs_csum_final(csum_result, (char *)&csum_result);
322 if (csum_result == 0) {
323 printk("csum result is 0 for block %llu\n",
324 (unsigned long long)bytenr);
327 write_extent_buffer(leaf, &csum_result, (unsigned long)item,
329 btrfs_mark_buffer_dirty(path->nodes[0]);
331 btrfs_release_path(root, path);
332 btrfs_free_path(path);
337 * helper function for csum removal, this expects the
338 * key to describe the csum pointed to by the path, and it expects
339 * the csum to overlap the range [bytenr, len]
341 * The csum should not be entirely contained in the range and the
342 * range should not be entirely contained in the csum.
344 * This calls btrfs_truncate_item with the correct args based on the
345 * overlap, and fixes up the key as required.
347 static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
348 struct btrfs_root *root,
349 struct btrfs_path *path,
350 struct btrfs_key *key,
353 struct extent_buffer *leaf;
355 btrfs_super_csum_size(root->fs_info->super_copy);
357 u64 end_byte = bytenr + len;
358 u32 blocksize = root->sectorsize;
361 leaf = path->nodes[0];
362 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
363 csum_end *= root->sectorsize;
364 csum_end += key->offset;
366 if (key->offset < bytenr && csum_end <= end_byte) {
371 * A simple truncate off the end of the item
373 u32 new_size = (bytenr - key->offset) / blocksize;
374 new_size *= csum_size;
375 ret = btrfs_truncate_item(trans, root, path, new_size, 1);
377 } else if (key->offset >= bytenr && csum_end > end_byte &&
378 end_byte > key->offset) {
383 * we need to truncate from the beginning of the csum
385 u32 new_size = (csum_end - end_byte) / blocksize;
386 new_size *= csum_size;
388 ret = btrfs_truncate_item(trans, root, path, new_size, 0);
391 key->offset = end_byte;
392 ret = btrfs_set_item_key_safe(trans, root, path, key);
401 * deletes the csum items from the csum tree for a given
404 int btrfs_del_csums(struct btrfs_trans_handle *trans,
405 struct btrfs_root *root, u64 bytenr, u64 len)
407 struct btrfs_path *path;
408 struct btrfs_key key;
409 u64 end_byte = bytenr + len;
411 struct extent_buffer *leaf;
414 btrfs_super_csum_size(root->fs_info->super_copy);
415 int blocksize = root->sectorsize;
417 root = root->fs_info->csum_root;
419 path = btrfs_alloc_path();
422 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
423 key.offset = end_byte - 1;
424 key.type = BTRFS_EXTENT_CSUM_KEY;
426 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
428 if (path->slots[0] == 0)
432 leaf = path->nodes[0];
433 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
435 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
436 key.type != BTRFS_EXTENT_CSUM_KEY) {
440 if (key.offset >= end_byte)
443 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
444 csum_end *= blocksize;
445 csum_end += key.offset;
447 /* this csum ends before we start, we're done */
448 if (csum_end <= bytenr)
451 /* delete the entire item, it is inside our range */
452 if (key.offset >= bytenr && csum_end <= end_byte) {
453 ret = btrfs_del_item(trans, root, path);
455 } else if (key.offset < bytenr && csum_end > end_byte) {
456 unsigned long offset;
457 unsigned long shift_len;
458 unsigned long item_offset;
463 * Our bytes are in the middle of the csum,
464 * we need to split this item and insert a new one.
466 * But we can't drop the path because the
467 * csum could change, get removed, extended etc.
469 * The trick here is the max size of a csum item leaves
470 * enough room in the tree block for a single
471 * item header. So, we split the item in place,
472 * adding a new header pointing to the existing
473 * bytes. Then we loop around again and we have
474 * a nicely formed csum item that we can neatly
477 offset = (bytenr - key.offset) / blocksize;
480 shift_len = (len / blocksize) * csum_size;
482 item_offset = btrfs_item_ptr_offset(leaf,
485 memset_extent_buffer(leaf, 0, item_offset + offset,
490 * btrfs_split_item returns -EAGAIN when the
491 * item changed size or key
493 ret = btrfs_split_item(trans, root, path, &key, offset);
494 BUG_ON(ret && ret != -EAGAIN);
496 key.offset = end_byte - 1;
498 ret = truncate_one_csum(trans, root, path,
502 btrfs_release_path(root, path);
505 btrfs_free_path(path);