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.
19 #include <linux/bio.h>
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
24 #include "transaction.h"
25 #include "print-tree.h"
27 #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
28 sizeof(struct btrfs_item) * 2) / \
29 BTRFS_CRC32_SIZE) - 1))
30 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
31 struct btrfs_root *root,
32 u64 objectid, u64 pos,
33 u64 disk_offset, u64 disk_num_bytes,
34 u64 num_bytes, u64 offset)
37 struct btrfs_file_extent_item *item;
38 struct btrfs_key file_key;
39 struct btrfs_path *path;
40 struct extent_buffer *leaf;
42 path = btrfs_alloc_path();
44 file_key.objectid = objectid;
45 file_key.offset = pos;
46 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
48 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
53 leaf = path->nodes[0];
54 item = btrfs_item_ptr(leaf, path->slots[0],
55 struct btrfs_file_extent_item);
56 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
57 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
58 btrfs_set_file_extent_offset(leaf, item, offset);
59 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
60 btrfs_set_file_extent_generation(leaf, item, trans->transid);
61 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
62 btrfs_mark_buffer_dirty(leaf);
64 btrfs_free_path(path);
68 struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
69 struct btrfs_root *root,
70 struct btrfs_path *path,
71 u64 objectid, u64 offset,
75 struct btrfs_key file_key;
76 struct btrfs_key found_key;
77 struct btrfs_csum_item *item;
78 struct extent_buffer *leaf;
82 file_key.objectid = objectid;
83 file_key.offset = offset;
84 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
85 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
88 leaf = path->nodes[0];
91 if (path->slots[0] == 0)
94 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
95 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
96 found_key.objectid != objectid) {
99 csum_offset = (offset - found_key.offset) >>
100 root->fs_info->sb->s_blocksize_bits;
101 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
102 csums_in_item /= BTRFS_CRC32_SIZE;
104 if (csum_offset >= csums_in_item) {
109 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
110 item = (struct btrfs_csum_item *)((unsigned char *)item +
111 csum_offset * BTRFS_CRC32_SIZE);
120 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
121 struct btrfs_root *root,
122 struct btrfs_path *path, u64 objectid,
126 struct btrfs_key file_key;
127 int ins_len = mod < 0 ? -1 : 0;
130 file_key.objectid = objectid;
131 file_key.offset = offset;
132 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
133 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
137 int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
140 struct btrfs_ordered_sum *sums;
141 struct btrfs_sector_sum *sector_sum;
142 struct btrfs_ordered_extent *ordered;
144 struct bio_vec *bvec = bio->bi_io_vec;
146 unsigned long total_bytes = 0;
147 unsigned long this_sum_bytes = 0;
150 WARN_ON(bio->bi_vcnt <= 0);
151 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
155 sector_sum = &sums->sums;
156 sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset;
157 sums->len = bio->bi_size;
158 INIT_LIST_HEAD(&sums->list);
159 ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset);
162 while(bio_index < bio->bi_vcnt) {
163 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
164 if (offset >= ordered->file_offset + ordered->len) {
165 unsigned long bytes_left;
166 sums->len = this_sum_bytes;
168 btrfs_add_ordered_sum(inode, ordered, sums);
169 btrfs_put_ordered_extent(ordered);
171 bytes_left = bio->bi_size - total_bytes;
173 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
176 sector_sum = &sums->sums;
177 sums->len = bytes_left;
178 sums->file_offset = offset;
179 ordered = btrfs_lookup_ordered_extent(inode,
184 data = kmap_atomic(bvec->bv_page, KM_USER0);
185 sector_sum->sum = ~(u32)0;
186 sector_sum->sum = btrfs_csum_data(root,
187 data + bvec->bv_offset,
190 kunmap_atomic(data, KM_USER0);
191 btrfs_csum_final(sector_sum->sum,
192 (char *)§or_sum->sum);
193 sector_sum->offset = page_offset(bvec->bv_page) +
197 total_bytes += bvec->bv_len;
198 this_sum_bytes += bvec->bv_len;
201 btrfs_add_ordered_sum(inode, ordered, sums);
202 btrfs_put_ordered_extent(ordered);
206 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
207 struct btrfs_root *root, struct inode *inode,
208 struct btrfs_ordered_sum *sums)
210 u64 objectid = inode->i_ino;
213 struct btrfs_key file_key;
214 struct btrfs_key found_key;
218 struct btrfs_path *path;
219 struct btrfs_csum_item *item;
220 struct btrfs_csum_item *item_end;
221 struct extent_buffer *leaf = NULL;
223 struct btrfs_sector_sum *sector_sum;
228 unsigned long map_len;
229 unsigned long map_start;
231 path = btrfs_alloc_path();
233 sector_sum = &sums->sums;
235 next_offset = (u64)-1;
237 offset = sector_sum->offset;
238 file_key.objectid = objectid;
239 file_key.offset = offset;
240 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
242 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
244 leaf = path->nodes[0];
250 /* we found one, but it isn't big enough yet */
251 leaf = path->nodes[0];
252 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
253 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
254 /* already at max size, make a new one */
258 int slot = path->slots[0] + 1;
259 /* we didn't find a csum item, insert one */
260 nritems = btrfs_header_nritems(path->nodes[0]);
261 if (path->slots[0] >= nritems - 1) {
262 ret = btrfs_next_leaf(root, path);
269 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
270 if (found_key.objectid != objectid ||
271 found_key.type != BTRFS_CSUM_ITEM_KEY) {
275 next_offset = found_key.offset;
281 * at this point, we know the tree has an item, but it isn't big
282 * enough yet to put our csum in. Grow it
284 btrfs_release_path(root, path);
285 ret = btrfs_search_slot(trans, root, &file_key, path,
286 BTRFS_CRC32_SIZE, 1);
292 if (path->slots[0] == 0) {
296 leaf = path->nodes[0];
297 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
298 csum_offset = (offset - found_key.offset) >>
299 root->fs_info->sb->s_blocksize_bits;
300 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
301 found_key.objectid != objectid ||
302 csum_offset >= MAX_CSUM_ITEMS(root)) {
305 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
307 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
308 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
309 if (diff != BTRFS_CRC32_SIZE)
311 ret = btrfs_extend_item(trans, root, path, diff);
317 btrfs_release_path(root, path);
320 u64 tmp = min((u64)i_size_read(inode), next_offset);
321 tmp -= offset & ~((u64)root->sectorsize -1);
322 tmp >>= root->fs_info->sb->s_blocksize_bits;
323 tmp = max((u64)1, tmp);
324 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
325 ins_size = BTRFS_CRC32_SIZE * tmp;
327 ins_size = BTRFS_CRC32_SIZE;
329 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
338 leaf = path->nodes[0];
339 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
341 item = (struct btrfs_csum_item *)((unsigned char *)item +
342 csum_offset * BTRFS_CRC32_SIZE);
344 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
345 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
346 btrfs_item_size_nr(leaf, path->slots[0]));
351 (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
355 unmap_extent_buffer(leaf, eb_token, KM_USER1);
357 err = map_private_extent_buffer(leaf, (unsigned long)item,
360 &map_start, &map_len, KM_USER1);
365 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
366 §or_sum->sum, BTRFS_CRC32_SIZE);
368 write_extent_buffer(leaf, §or_sum->sum,
369 (unsigned long)item, BTRFS_CRC32_SIZE);
372 total_bytes += root->sectorsize;
374 if (total_bytes < sums->len) {
375 item = (struct btrfs_csum_item *)((char *)item +
377 if (item < item_end && offset + PAGE_CACHE_SIZE ==
378 sector_sum->offset) {
379 offset = sector_sum->offset;
384 unmap_extent_buffer(leaf, eb_token, KM_USER1);
387 btrfs_mark_buffer_dirty(path->nodes[0]);
388 if (total_bytes < sums->len) {
389 btrfs_release_path(root, path);
393 btrfs_free_path(path);
397 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
398 struct btrfs_root *root, struct btrfs_path *path,
401 struct btrfs_key key;
402 struct extent_buffer *leaf = path->nodes[0];
403 int slot = path->slots[0];
409 btrfs_item_key_to_cpu(leaf, &key, slot);
410 if (isize <= key.offset)
412 new_item_span = isize - key.offset;
413 blocks = (new_item_span + root->sectorsize - 1) >>
414 root->fs_info->sb->s_blocksize_bits;
415 new_item_size = blocks * BTRFS_CRC32_SIZE;
416 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
418 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);