Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 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 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 19 | #include <linux/bio.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/highmem.h> |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 22 | #include "ctree.h" |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 23 | #include "disk-io.h" |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 24 | #include "transaction.h" |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 25 | #include "print-tree.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 26 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 27 | #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 28 | sizeof(struct btrfs_item) * 2) / \ |
| 29 | BTRFS_CRC32_SIZE) - 1)) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 30 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 31 | struct btrfs_root *root, |
| 32 | u64 objectid, u64 pos, |
| 33 | u64 disk_offset, u64 disk_num_bytes, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 34 | u64 num_bytes, u64 offset, u64 ram_bytes, |
| 35 | u8 compression, u8 encryption, u16 other_encoding) |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 36 | { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 37 | int ret = 0; |
| 38 | struct btrfs_file_extent_item *item; |
| 39 | struct btrfs_key file_key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 40 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 41 | struct extent_buffer *leaf; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 42 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 43 | path = btrfs_alloc_path(); |
| 44 | BUG_ON(!path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 45 | file_key.objectid = objectid; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 46 | file_key.offset = pos; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 47 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 48 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 49 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 50 | sizeof(*item)); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 51 | if (ret < 0) |
| 52 | goto out; |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 53 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 54 | leaf = path->nodes[0]; |
| 55 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 56 | struct btrfs_file_extent_item); |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 57 | btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 58 | btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 59 | btrfs_set_file_extent_offset(leaf, item, offset); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 60 | btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 61 | btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 62 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 63 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 64 | btrfs_set_file_extent_compression(leaf, item, compression); |
| 65 | btrfs_set_file_extent_encryption(leaf, item, encryption); |
| 66 | btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); |
| 67 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 68 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 69 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 70 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 71 | return ret; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 72 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 73 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 74 | struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, |
| 75 | struct btrfs_root *root, |
| 76 | struct btrfs_path *path, |
| 77 | u64 objectid, u64 offset, |
| 78 | int cow) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 79 | { |
| 80 | int ret; |
| 81 | struct btrfs_key file_key; |
| 82 | struct btrfs_key found_key; |
| 83 | struct btrfs_csum_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 84 | struct extent_buffer *leaf; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 85 | u64 csum_offset = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 86 | int csums_in_item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 87 | |
| 88 | file_key.objectid = objectid; |
| 89 | file_key.offset = offset; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 90 | btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 91 | ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 92 | if (ret < 0) |
| 93 | goto fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 94 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 95 | if (ret > 0) { |
| 96 | ret = 1; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 97 | if (path->slots[0] == 0) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 98 | goto fail; |
| 99 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 100 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 101 | if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || |
| 102 | found_key.objectid != objectid) { |
| 103 | goto fail; |
| 104 | } |
| 105 | csum_offset = (offset - found_key.offset) >> |
| 106 | root->fs_info->sb->s_blocksize_bits; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 107 | csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 108 | csums_in_item /= BTRFS_CRC32_SIZE; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 109 | |
| 110 | if (csum_offset >= csums_in_item) { |
| 111 | ret = -EFBIG; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 112 | goto fail; |
| 113 | } |
| 114 | } |
| 115 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 116 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
| 117 | csum_offset * BTRFS_CRC32_SIZE); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 118 | return item; |
| 119 | fail: |
| 120 | if (ret > 0) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 121 | ret = -ENOENT; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 122 | return ERR_PTR(ret); |
| 123 | } |
| 124 | |
| 125 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 126 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, |
| 127 | struct btrfs_root *root, |
| 128 | struct btrfs_path *path, u64 objectid, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 129 | u64 offset, int mod) |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 130 | { |
| 131 | int ret; |
| 132 | struct btrfs_key file_key; |
| 133 | int ins_len = mod < 0 ? -1 : 0; |
| 134 | int cow = mod != 0; |
| 135 | |
| 136 | file_key.objectid = objectid; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 137 | file_key.offset = offset; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 138 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 139 | ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); |
| 140 | return ret; |
| 141 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 142 | |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 143 | int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, |
| 144 | struct bio *bio) |
| 145 | { |
| 146 | u32 sum; |
| 147 | struct bio_vec *bvec = bio->bi_io_vec; |
| 148 | int bio_index = 0; |
| 149 | u64 offset; |
| 150 | u64 item_start_offset = 0; |
| 151 | u64 item_last_offset = 0; |
| 152 | u32 diff; |
| 153 | int ret; |
| 154 | struct btrfs_path *path; |
| 155 | struct btrfs_csum_item *item = NULL; |
| 156 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 157 | |
| 158 | path = btrfs_alloc_path(); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 159 | if (bio->bi_size > PAGE_CACHE_SIZE * 8) |
| 160 | path->reada = 2; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 161 | |
| 162 | WARN_ON(bio->bi_vcnt <= 0); |
| 163 | |
| 164 | while(bio_index < bio->bi_vcnt) { |
| 165 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
| 166 | ret = btrfs_find_ordered_sum(inode, offset, &sum); |
| 167 | if (ret == 0) |
| 168 | goto found; |
| 169 | |
| 170 | if (!item || offset < item_start_offset || |
| 171 | offset >= item_last_offset) { |
| 172 | struct btrfs_key found_key; |
| 173 | u32 item_size; |
| 174 | |
| 175 | if (item) |
| 176 | btrfs_release_path(root, path); |
| 177 | item = btrfs_lookup_csum(NULL, root, path, |
| 178 | inode->i_ino, offset, 0); |
| 179 | if (IS_ERR(item)) { |
| 180 | ret = PTR_ERR(item); |
| 181 | if (ret == -ENOENT || ret == -EFBIG) |
| 182 | ret = 0; |
| 183 | sum = 0; |
| 184 | printk("no csum found for inode %lu start " |
| 185 | "%llu\n", inode->i_ino, |
| 186 | (unsigned long long)offset); |
Chris Mason | 6dab815 | 2008-08-04 08:35:53 -0400 | [diff] [blame] | 187 | item = NULL; |
Chris Mason | 39be25c | 2008-11-10 11:50:50 -0500 | [diff] [blame^] | 188 | btrfs_release_path(root, path); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 189 | goto found; |
| 190 | } |
| 191 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 192 | path->slots[0]); |
| 193 | |
| 194 | item_start_offset = found_key.offset; |
| 195 | item_size = btrfs_item_size_nr(path->nodes[0], |
| 196 | path->slots[0]); |
| 197 | item_last_offset = item_start_offset + |
| 198 | (item_size / BTRFS_CRC32_SIZE) * |
| 199 | root->sectorsize; |
| 200 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 201 | struct btrfs_csum_item); |
| 202 | } |
| 203 | /* |
| 204 | * this byte range must be able to fit inside |
| 205 | * a single leaf so it will also fit inside a u32 |
| 206 | */ |
| 207 | diff = offset - item_start_offset; |
| 208 | diff = diff / root->sectorsize; |
| 209 | diff = diff * BTRFS_CRC32_SIZE; |
| 210 | |
| 211 | read_extent_buffer(path->nodes[0], &sum, |
Chris Mason | 3de9d6b | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 212 | ((unsigned long)item) + diff, |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 213 | BTRFS_CRC32_SIZE); |
| 214 | found: |
| 215 | set_state_private(io_tree, offset, sum); |
| 216 | bio_index++; |
| 217 | bvec++; |
| 218 | } |
| 219 | btrfs_free_path(path); |
| 220 | return 0; |
| 221 | } |
| 222 | |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 223 | int btrfs_csum_file_bytes(struct btrfs_root *root, struct inode *inode, |
| 224 | u64 start, unsigned long len) |
| 225 | { |
| 226 | struct btrfs_ordered_sum *sums; |
| 227 | struct btrfs_sector_sum *sector_sum; |
| 228 | struct btrfs_ordered_extent *ordered; |
| 229 | char *data; |
| 230 | struct page *page; |
| 231 | unsigned long total_bytes = 0; |
| 232 | unsigned long this_sum_bytes = 0; |
| 233 | |
| 234 | sums = kzalloc(btrfs_ordered_sum_size(root, len), GFP_NOFS); |
| 235 | if (!sums) |
| 236 | return -ENOMEM; |
| 237 | |
| 238 | sector_sum = sums->sums; |
| 239 | sums->file_offset = start; |
| 240 | sums->len = len; |
| 241 | INIT_LIST_HEAD(&sums->list); |
| 242 | ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset); |
| 243 | BUG_ON(!ordered); |
| 244 | |
| 245 | while(len > 0) { |
| 246 | if (start >= ordered->file_offset + ordered->len || |
| 247 | start < ordered->file_offset) { |
| 248 | sums->len = this_sum_bytes; |
| 249 | this_sum_bytes = 0; |
| 250 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 251 | btrfs_put_ordered_extent(ordered); |
| 252 | |
| 253 | sums = kzalloc(btrfs_ordered_sum_size(root, len), |
| 254 | GFP_NOFS); |
| 255 | BUG_ON(!sums); |
| 256 | sector_sum = sums->sums; |
| 257 | sums->len = len; |
| 258 | sums->file_offset = start; |
| 259 | ordered = btrfs_lookup_ordered_extent(inode, |
| 260 | sums->file_offset); |
| 261 | BUG_ON(!ordered); |
| 262 | } |
| 263 | |
| 264 | page = find_get_page(inode->i_mapping, |
| 265 | start >> PAGE_CACHE_SHIFT); |
| 266 | |
| 267 | data = kmap_atomic(page, KM_USER0); |
| 268 | sector_sum->sum = ~(u32)0; |
| 269 | sector_sum->sum = btrfs_csum_data(root, data, sector_sum->sum, |
| 270 | PAGE_CACHE_SIZE); |
| 271 | kunmap_atomic(data, KM_USER0); |
| 272 | btrfs_csum_final(sector_sum->sum, |
| 273 | (char *)§or_sum->sum); |
| 274 | sector_sum->offset = page_offset(page); |
| 275 | page_cache_release(page); |
| 276 | |
| 277 | sector_sum++; |
| 278 | total_bytes += PAGE_CACHE_SIZE; |
| 279 | this_sum_bytes += PAGE_CACHE_SIZE; |
| 280 | start += PAGE_CACHE_SIZE; |
| 281 | |
| 282 | WARN_ON(len < PAGE_CACHE_SIZE); |
| 283 | len -= PAGE_CACHE_SIZE; |
| 284 | } |
| 285 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 286 | btrfs_put_ordered_extent(ordered); |
| 287 | return 0; |
| 288 | } |
| 289 | |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 290 | int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, |
| 291 | struct bio *bio) |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 292 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 293 | struct btrfs_ordered_sum *sums; |
| 294 | struct btrfs_sector_sum *sector_sum; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 295 | struct btrfs_ordered_extent *ordered; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 296 | char *data; |
| 297 | struct bio_vec *bvec = bio->bi_io_vec; |
| 298 | int bio_index = 0; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 299 | unsigned long total_bytes = 0; |
| 300 | unsigned long this_sum_bytes = 0; |
| 301 | u64 offset; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 302 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 303 | WARN_ON(bio->bi_vcnt <= 0); |
| 304 | sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 305 | if (!sums) |
| 306 | return -ENOMEM; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 307 | |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 308 | sector_sum = sums->sums; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 309 | sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 310 | sums->len = bio->bi_size; |
| 311 | INIT_LIST_HEAD(&sums->list); |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 312 | ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset); |
| 313 | BUG_ON(!ordered); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 314 | |
| 315 | while(bio_index < bio->bi_vcnt) { |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 316 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
Chris Mason | e5a2217 | 2008-07-18 20:42:20 -0400 | [diff] [blame] | 317 | if (offset >= ordered->file_offset + ordered->len || |
| 318 | offset < ordered->file_offset) { |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 319 | unsigned long bytes_left; |
| 320 | sums->len = this_sum_bytes; |
| 321 | this_sum_bytes = 0; |
| 322 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 323 | btrfs_put_ordered_extent(ordered); |
| 324 | |
| 325 | bytes_left = bio->bi_size - total_bytes; |
| 326 | |
| 327 | sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), |
| 328 | GFP_NOFS); |
| 329 | BUG_ON(!sums); |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 330 | sector_sum = sums->sums; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 331 | sums->len = bytes_left; |
| 332 | sums->file_offset = offset; |
| 333 | ordered = btrfs_lookup_ordered_extent(inode, |
| 334 | sums->file_offset); |
| 335 | BUG_ON(!ordered); |
| 336 | } |
| 337 | |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 338 | data = kmap_atomic(bvec->bv_page, KM_USER0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 339 | sector_sum->sum = ~(u32)0; |
| 340 | sector_sum->sum = btrfs_csum_data(root, |
| 341 | data + bvec->bv_offset, |
| 342 | sector_sum->sum, |
| 343 | bvec->bv_len); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 344 | kunmap_atomic(data, KM_USER0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 345 | btrfs_csum_final(sector_sum->sum, |
| 346 | (char *)§or_sum->sum); |
| 347 | sector_sum->offset = page_offset(bvec->bv_page) + |
| 348 | bvec->bv_offset; |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 349 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 350 | sector_sum++; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 351 | bio_index++; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 352 | total_bytes += bvec->bv_len; |
| 353 | this_sum_bytes += bvec->bv_len; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 354 | bvec++; |
| 355 | } |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 356 | this_sum_bytes = 0; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 357 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 358 | btrfs_put_ordered_extent(ordered); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 362 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, |
| 363 | struct btrfs_root *root, struct inode *inode, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 364 | struct btrfs_ordered_sum *sums) |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 365 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 366 | u64 objectid = inode->i_ino; |
| 367 | u64 offset; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 368 | int ret; |
| 369 | struct btrfs_key file_key; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 370 | struct btrfs_key found_key; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 371 | u64 next_offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 372 | u64 total_bytes = 0; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 373 | int found_next; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 374 | struct btrfs_path *path; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 375 | struct btrfs_csum_item *item; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 376 | struct btrfs_csum_item *item_end; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 377 | struct extent_buffer *leaf = NULL; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 378 | u64 csum_offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 379 | struct btrfs_sector_sum *sector_sum; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 380 | u32 nritems; |
| 381 | u32 ins_size; |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 382 | char *eb_map; |
| 383 | char *eb_token; |
| 384 | unsigned long map_len; |
| 385 | unsigned long map_start; |
| 386 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 387 | path = btrfs_alloc_path(); |
| 388 | BUG_ON(!path); |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 389 | sector_sum = sums->sums; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 390 | again: |
| 391 | next_offset = (u64)-1; |
| 392 | found_next = 0; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 393 | offset = sector_sum->offset; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 394 | file_key.objectid = objectid; |
| 395 | file_key.offset = offset; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 396 | btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 397 | |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 398 | mutex_lock(&BTRFS_I(inode)->csum_mutex); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 399 | item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 400 | if (!IS_ERR(item)) { |
| 401 | leaf = path->nodes[0]; |
Chris Mason | 639cb58 | 2008-08-28 06:15:25 -0400 | [diff] [blame] | 402 | ret = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 403 | goto found; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 404 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 405 | ret = PTR_ERR(item); |
| 406 | if (ret == -EFBIG) { |
| 407 | u32 item_size; |
| 408 | /* we found one, but it isn't big enough yet */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 409 | leaf = path->nodes[0]; |
| 410 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 411 | if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 412 | /* already at max size, make a new one */ |
| 413 | goto insert; |
| 414 | } |
| 415 | } else { |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 416 | int slot = path->slots[0] + 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 417 | /* we didn't find a csum item, insert one */ |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 418 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 419 | if (path->slots[0] >= nritems - 1) { |
| 420 | ret = btrfs_next_leaf(root, path); |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 421 | if (ret == 1) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 422 | found_next = 1; |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 423 | if (ret != 0) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 424 | goto insert; |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 425 | slot = 0; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 426 | } |
| 427 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); |
| 428 | if (found_key.objectid != objectid || |
| 429 | found_key.type != BTRFS_CSUM_ITEM_KEY) { |
| 430 | found_next = 1; |
| 431 | goto insert; |
| 432 | } |
| 433 | next_offset = found_key.offset; |
| 434 | found_next = 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 435 | goto insert; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * at this point, we know the tree has an item, but it isn't big |
| 440 | * enough yet to put our csum in. Grow it |
| 441 | */ |
| 442 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 443 | ret = btrfs_search_slot(trans, root, &file_key, path, |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 444 | BTRFS_CRC32_SIZE, 1); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 445 | if (ret < 0) |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 446 | goto fail_unlock; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 447 | if (ret == 0) { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 448 | BUG(); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 449 | } |
| 450 | if (path->slots[0] == 0) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 451 | goto insert; |
| 452 | } |
| 453 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 454 | leaf = path->nodes[0]; |
| 455 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 456 | csum_offset = (offset - found_key.offset) >> |
| 457 | root->fs_info->sb->s_blocksize_bits; |
| 458 | if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || |
| 459 | found_key.objectid != objectid || |
| 460 | csum_offset >= MAX_CSUM_ITEMS(root)) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 461 | goto insert; |
| 462 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 463 | if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 464 | BTRFS_CRC32_SIZE) { |
| 465 | u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 466 | diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 467 | if (diff != BTRFS_CRC32_SIZE) |
| 468 | goto insert; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 469 | ret = btrfs_extend_item(trans, root, path, diff); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 470 | BUG_ON(ret); |
| 471 | goto csum; |
| 472 | } |
| 473 | |
| 474 | insert: |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 475 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 476 | csum_offset = 0; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 477 | if (found_next) { |
| 478 | u64 tmp = min((u64)i_size_read(inode), next_offset); |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 479 | tmp -= offset & ~((u64)root->sectorsize -1); |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 480 | tmp >>= root->fs_info->sb->s_blocksize_bits; |
| 481 | tmp = max((u64)1, tmp); |
| 482 | tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root)); |
| 483 | ins_size = BTRFS_CRC32_SIZE * tmp; |
| 484 | } else { |
| 485 | ins_size = BTRFS_CRC32_SIZE; |
| 486 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 487 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 488 | ins_size); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 489 | if (ret < 0) |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 490 | goto fail_unlock; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 491 | if (ret != 0) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 492 | WARN_ON(1); |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 493 | goto fail_unlock; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 494 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 495 | csum: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 496 | leaf = path->nodes[0]; |
| 497 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 498 | ret = 0; |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 499 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
| 500 | csum_offset * BTRFS_CRC32_SIZE); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 501 | found: |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 502 | item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
| 503 | item_end = (struct btrfs_csum_item *)((unsigned char *)item_end + |
| 504 | btrfs_item_size_nr(leaf, path->slots[0])); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 505 | eb_token = NULL; |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 506 | mutex_unlock(&BTRFS_I(inode)->csum_mutex); |
| 507 | cond_resched(); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 508 | next_sector: |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 509 | |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 510 | if (!eb_token || |
| 511 | (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) { |
| 512 | int err; |
| 513 | |
| 514 | if (eb_token) |
Chris Mason | eb20978 | 2008-02-21 09:30:08 -0500 | [diff] [blame] | 515 | unmap_extent_buffer(leaf, eb_token, KM_USER1); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 516 | eb_token = NULL; |
| 517 | err = map_private_extent_buffer(leaf, (unsigned long)item, |
| 518 | BTRFS_CRC32_SIZE, |
| 519 | &eb_token, &eb_map, |
Chris Mason | eb20978 | 2008-02-21 09:30:08 -0500 | [diff] [blame] | 520 | &map_start, &map_len, KM_USER1); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 521 | if (err) |
| 522 | eb_token = NULL; |
| 523 | } |
| 524 | if (eb_token) { |
| 525 | memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)), |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 526 | §or_sum->sum, BTRFS_CRC32_SIZE); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 527 | } else { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 528 | write_extent_buffer(leaf, §or_sum->sum, |
| 529 | (unsigned long)item, BTRFS_CRC32_SIZE); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 530 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 531 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 532 | total_bytes += root->sectorsize; |
| 533 | sector_sum++; |
| 534 | if (total_bytes < sums->len) { |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 535 | item = (struct btrfs_csum_item *)((char *)item + |
| 536 | BTRFS_CRC32_SIZE); |
Chris Mason | 2e1a992 | 2008-02-20 15:44:32 -0500 | [diff] [blame] | 537 | if (item < item_end && offset + PAGE_CACHE_SIZE == |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 538 | sector_sum->offset) { |
| 539 | offset = sector_sum->offset; |
| 540 | goto next_sector; |
Chris Mason | 2e1a992 | 2008-02-20 15:44:32 -0500 | [diff] [blame] | 541 | } |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 542 | } |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 543 | if (eb_token) { |
Chris Mason | eb20978 | 2008-02-21 09:30:08 -0500 | [diff] [blame] | 544 | unmap_extent_buffer(leaf, eb_token, KM_USER1); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 545 | eb_token = NULL; |
| 546 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 547 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 548 | cond_resched(); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 549 | if (total_bytes < sums->len) { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 550 | btrfs_release_path(root, path); |
| 551 | goto again; |
| 552 | } |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 553 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 554 | btrfs_free_path(path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 555 | return ret; |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 556 | |
| 557 | fail_unlock: |
| 558 | mutex_unlock(&BTRFS_I(inode)->csum_mutex); |
| 559 | goto out; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 560 | } |
| 561 | |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 562 | int btrfs_csum_truncate(struct btrfs_trans_handle *trans, |
| 563 | struct btrfs_root *root, struct btrfs_path *path, |
| 564 | u64 isize) |
| 565 | { |
| 566 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 567 | struct extent_buffer *leaf = path->nodes[0]; |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 568 | int slot = path->slots[0]; |
| 569 | int ret; |
| 570 | u32 new_item_size; |
| 571 | u64 new_item_span; |
| 572 | u64 blocks; |
| 573 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 574 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 575 | if (isize <= key.offset) |
| 576 | return 0; |
| 577 | new_item_span = isize - key.offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 578 | blocks = (new_item_span + root->sectorsize - 1) >> |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 579 | root->fs_info->sb->s_blocksize_bits; |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 580 | new_item_size = blocks * BTRFS_CRC32_SIZE; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 581 | if (new_item_size >= btrfs_item_size_nr(leaf, slot)) |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 582 | return 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 583 | ret = btrfs_truncate_item(trans, root, path, new_item_size, 1); |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 584 | BUG_ON(ret); |
| 585 | return ret; |
| 586 | } |