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