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 | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 27 | #define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \ |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 28 | sizeof(struct btrfs_item) * 2) / \ |
| 29 | size) - 1)) |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 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 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 36 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 37 | struct btrfs_root *root, |
| 38 | u64 objectid, u64 pos, |
| 39 | u64 disk_offset, u64 disk_num_bytes, |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 40 | u64 num_bytes, u64 offset, u64 ram_bytes, |
| 41 | u8 compression, u8 encryption, u16 other_encoding) |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 42 | { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 43 | int ret = 0; |
| 44 | struct btrfs_file_extent_item *item; |
| 45 | struct btrfs_key file_key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 46 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 47 | struct extent_buffer *leaf; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 48 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 49 | path = btrfs_alloc_path(); |
| 50 | BUG_ON(!path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 51 | file_key.objectid = objectid; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 52 | file_key.offset = pos; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 53 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 54 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 55 | path->leave_spinning = 1; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 56 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 57 | sizeof(*item)); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 58 | if (ret < 0) |
| 59 | goto out; |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 60 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 61 | leaf = path->nodes[0]; |
| 62 | item = btrfs_item_ptr(leaf, path->slots[0], |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 63 | struct btrfs_file_extent_item); |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 64 | btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 65 | btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); |
Sage Weil | f2eb0a2 | 2008-05-02 14:43:14 -0400 | [diff] [blame] | 66 | btrfs_set_file_extent_offset(leaf, item, offset); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 67 | btrfs_set_file_extent_num_bytes(leaf, item, num_bytes); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 68 | btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 69 | btrfs_set_file_extent_generation(leaf, item, trans->transid); |
| 70 | btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); |
Chris Mason | c8b9781 | 2008-10-29 14:49:59 -0400 | [diff] [blame] | 71 | btrfs_set_file_extent_compression(leaf, item, compression); |
| 72 | btrfs_set_file_extent_encryption(leaf, item, encryption); |
| 73 | btrfs_set_file_extent_other_encoding(leaf, item, other_encoding); |
| 74 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 75 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 76 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 77 | btrfs_free_path(path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 78 | return ret; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 79 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 80 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 81 | struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, |
| 82 | struct btrfs_root *root, |
| 83 | struct btrfs_path *path, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 84 | u64 bytenr, int cow) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 85 | { |
| 86 | int ret; |
| 87 | struct btrfs_key file_key; |
| 88 | struct btrfs_key found_key; |
| 89 | struct btrfs_csum_item *item; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 90 | struct extent_buffer *leaf; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 91 | u64 csum_offset = 0; |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 92 | u16 csum_size = |
| 93 | btrfs_super_csum_size(&root->fs_info->super_copy); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 94 | int csums_in_item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 95 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 96 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 97 | file_key.offset = bytenr; |
| 98 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 99 | ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 100 | if (ret < 0) |
| 101 | goto fail; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 102 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 103 | if (ret > 0) { |
| 104 | ret = 1; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 105 | if (path->slots[0] == 0) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 106 | goto fail; |
| 107 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 108 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 109 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 110 | goto fail; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 111 | |
| 112 | csum_offset = (bytenr - found_key.offset) >> |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 113 | root->fs_info->sb->s_blocksize_bits; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 114 | csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]); |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 115 | csums_in_item /= csum_size; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 116 | |
| 117 | if (csum_offset >= csums_in_item) { |
| 118 | ret = -EFBIG; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 119 | goto fail; |
| 120 | } |
| 121 | } |
| 122 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 123 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 124 | csum_offset * csum_size); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 125 | return item; |
| 126 | fail: |
| 127 | if (ret > 0) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 128 | ret = -ENOENT; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 129 | return ERR_PTR(ret); |
| 130 | } |
| 131 | |
| 132 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 133 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, |
| 134 | struct btrfs_root *root, |
| 135 | struct btrfs_path *path, u64 objectid, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 136 | u64 offset, int mod) |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 137 | { |
| 138 | int ret; |
| 139 | struct btrfs_key file_key; |
| 140 | int ins_len = mod < 0 ? -1 : 0; |
| 141 | int cow = mod != 0; |
| 142 | |
| 143 | file_key.objectid = objectid; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 144 | file_key.offset = offset; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 145 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 146 | ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); |
| 147 | return ret; |
| 148 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 149 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 150 | |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 151 | int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 152 | struct bio *bio, u32 *dst) |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 153 | { |
| 154 | u32 sum; |
| 155 | struct bio_vec *bvec = bio->bi_io_vec; |
| 156 | int bio_index = 0; |
| 157 | u64 offset; |
| 158 | u64 item_start_offset = 0; |
| 159 | u64 item_last_offset = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 160 | u64 disk_bytenr; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 161 | u32 diff; |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 162 | u16 csum_size = |
| 163 | btrfs_super_csum_size(&root->fs_info->super_copy); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 164 | int ret; |
| 165 | struct btrfs_path *path; |
| 166 | struct btrfs_csum_item *item = NULL; |
| 167 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
| 168 | |
| 169 | path = btrfs_alloc_path(); |
Chris Mason | 4d1b5fb | 2008-08-20 09:44:52 -0400 | [diff] [blame] | 170 | if (bio->bi_size > PAGE_CACHE_SIZE * 8) |
| 171 | path->reada = 2; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 172 | |
| 173 | WARN_ON(bio->bi_vcnt <= 0); |
| 174 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 175 | disk_bytenr = (u64)bio->bi_sector << 9; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 176 | while (bio_index < bio->bi_vcnt) { |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 177 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 178 | ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 179 | if (ret == 0) |
| 180 | goto found; |
| 181 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 182 | if (!item || disk_bytenr < item_start_offset || |
| 183 | disk_bytenr >= item_last_offset) { |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 184 | struct btrfs_key found_key; |
| 185 | u32 item_size; |
| 186 | |
| 187 | if (item) |
| 188 | btrfs_release_path(root, path); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 189 | item = btrfs_lookup_csum(NULL, root->fs_info->csum_root, |
| 190 | path, disk_bytenr, 0); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 191 | if (IS_ERR(item)) { |
| 192 | ret = PTR_ERR(item); |
| 193 | if (ret == -ENOENT || ret == -EFBIG) |
| 194 | ret = 0; |
| 195 | sum = 0; |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 196 | if (BTRFS_I(inode)->root->root_key.objectid == |
| 197 | BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 198 | set_extent_bits(io_tree, offset, |
| 199 | offset + bvec->bv_len - 1, |
| 200 | EXTENT_NODATASUM, GFP_NOFS); |
| 201 | } else { |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 202 | printk(KERN_INFO "btrfs no csum found " |
| 203 | "for inode %lu start %llu\n", |
| 204 | inode->i_ino, |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 205 | (unsigned long long)offset); |
| 206 | } |
Chris Mason | 6dab815 | 2008-08-04 08:35:53 -0400 | [diff] [blame] | 207 | item = NULL; |
Chris Mason | 39be25c | 2008-11-10 11:50:50 -0500 | [diff] [blame] | 208 | btrfs_release_path(root, path); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 209 | goto found; |
| 210 | } |
| 211 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 212 | path->slots[0]); |
| 213 | |
| 214 | item_start_offset = found_key.offset; |
| 215 | item_size = btrfs_item_size_nr(path->nodes[0], |
| 216 | path->slots[0]); |
| 217 | item_last_offset = item_start_offset + |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 218 | (item_size / csum_size) * |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 219 | root->sectorsize; |
| 220 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 221 | struct btrfs_csum_item); |
| 222 | } |
| 223 | /* |
| 224 | * this byte range must be able to fit inside |
| 225 | * a single leaf so it will also fit inside a u32 |
| 226 | */ |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 227 | diff = disk_bytenr - item_start_offset; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 228 | diff = diff / root->sectorsize; |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 229 | diff = diff * csum_size; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 230 | |
| 231 | read_extent_buffer(path->nodes[0], &sum, |
Chris Mason | 3de9d6b | 2008-08-04 23:17:27 -0400 | [diff] [blame] | 232 | ((unsigned long)item) + diff, |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 233 | csum_size); |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 234 | found: |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 235 | if (dst) |
| 236 | *dst++ = sum; |
| 237 | else |
| 238 | set_state_private(io_tree, offset, sum); |
| 239 | disk_bytenr += bvec->bv_len; |
Chris Mason | 61b4944 | 2008-07-31 15:42:53 -0400 | [diff] [blame] | 240 | bio_index++; |
| 241 | bvec++; |
| 242 | } |
| 243 | btrfs_free_path(path); |
| 244 | return 0; |
| 245 | } |
| 246 | |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 247 | int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, |
| 248 | struct list_head *list) |
| 249 | { |
| 250 | struct btrfs_key key; |
| 251 | struct btrfs_path *path; |
| 252 | struct extent_buffer *leaf; |
| 253 | struct btrfs_ordered_sum *sums; |
| 254 | struct btrfs_sector_sum *sector_sum; |
| 255 | struct btrfs_csum_item *item; |
| 256 | unsigned long offset; |
| 257 | int ret; |
| 258 | size_t size; |
| 259 | u64 csum_end; |
| 260 | u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy); |
| 261 | |
| 262 | path = btrfs_alloc_path(); |
| 263 | BUG_ON(!path); |
| 264 | |
| 265 | key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 266 | key.offset = start; |
| 267 | key.type = BTRFS_EXTENT_CSUM_KEY; |
| 268 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 269 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 270 | if (ret < 0) |
| 271 | goto fail; |
| 272 | if (ret > 0 && path->slots[0] > 0) { |
| 273 | leaf = path->nodes[0]; |
| 274 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); |
| 275 | if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID && |
| 276 | key.type == BTRFS_EXTENT_CSUM_KEY) { |
| 277 | offset = (start - key.offset) >> |
| 278 | root->fs_info->sb->s_blocksize_bits; |
| 279 | if (offset * csum_size < |
| 280 | btrfs_item_size_nr(leaf, path->slots[0] - 1)) |
| 281 | path->slots[0]--; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | while (start <= end) { |
| 286 | leaf = path->nodes[0]; |
| 287 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 288 | ret = btrfs_next_leaf(root, path); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 289 | if (ret < 0) |
| 290 | goto fail; |
| 291 | if (ret > 0) |
| 292 | break; |
| 293 | leaf = path->nodes[0]; |
| 294 | } |
| 295 | |
| 296 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 297 | if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
| 298 | key.type != BTRFS_EXTENT_CSUM_KEY) |
| 299 | break; |
| 300 | |
| 301 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 302 | if (key.offset > end) |
| 303 | break; |
| 304 | |
| 305 | if (key.offset > start) |
| 306 | start = key.offset; |
| 307 | |
| 308 | size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 309 | csum_end = key.offset + (size / csum_size) * root->sectorsize; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 310 | if (csum_end <= start) { |
| 311 | path->slots[0]++; |
| 312 | continue; |
| 313 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 314 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 315 | csum_end = min(csum_end, end + 1); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 316 | item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 317 | struct btrfs_csum_item); |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 318 | while (start < csum_end) { |
| 319 | size = min_t(size_t, csum_end - start, |
| 320 | MAX_ORDERED_SUM_BYTES(root)); |
| 321 | sums = kzalloc(btrfs_ordered_sum_size(root, size), |
| 322 | GFP_NOFS); |
| 323 | BUG_ON(!sums); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 324 | |
Yan Zheng | 07d400a | 2009-01-06 11:42:00 -0500 | [diff] [blame] | 325 | sector_sum = sums->sums; |
| 326 | sums->bytenr = start; |
| 327 | sums->len = size; |
| 328 | |
| 329 | offset = (start - key.offset) >> |
| 330 | root->fs_info->sb->s_blocksize_bits; |
| 331 | offset *= csum_size; |
| 332 | |
| 333 | while (size > 0) { |
| 334 | read_extent_buffer(path->nodes[0], |
| 335 | §or_sum->sum, |
| 336 | ((unsigned long)item) + |
| 337 | offset, csum_size); |
| 338 | sector_sum->bytenr = start; |
| 339 | |
| 340 | size -= root->sectorsize; |
| 341 | start += root->sectorsize; |
| 342 | offset += csum_size; |
| 343 | sector_sum++; |
| 344 | } |
| 345 | list_add_tail(&sums->list, list); |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 346 | } |
Yan Zheng | 17d217f | 2008-12-12 10:03:38 -0500 | [diff] [blame] | 347 | path->slots[0]++; |
| 348 | } |
| 349 | ret = 0; |
| 350 | fail: |
| 351 | btrfs_free_path(path); |
| 352 | return ret; |
| 353 | } |
| 354 | |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 355 | int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 356 | struct bio *bio, u64 file_start, int contig) |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 357 | { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 358 | struct btrfs_ordered_sum *sums; |
| 359 | struct btrfs_sector_sum *sector_sum; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 360 | struct btrfs_ordered_extent *ordered; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 361 | char *data; |
| 362 | struct bio_vec *bvec = bio->bi_io_vec; |
| 363 | int bio_index = 0; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 364 | unsigned long total_bytes = 0; |
| 365 | unsigned long this_sum_bytes = 0; |
| 366 | u64 offset; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 367 | u64 disk_bytenr; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 368 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 369 | WARN_ON(bio->bi_vcnt <= 0); |
| 370 | sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 371 | if (!sums) |
| 372 | return -ENOMEM; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 373 | |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 374 | sector_sum = sums->sums; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 375 | disk_bytenr = (u64)bio->bi_sector << 9; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 376 | sums->len = bio->bi_size; |
| 377 | INIT_LIST_HEAD(&sums->list); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 378 | |
| 379 | if (contig) |
| 380 | offset = file_start; |
| 381 | else |
| 382 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
| 383 | |
| 384 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 385 | BUG_ON(!ordered); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 386 | sums->bytenr = ordered->start; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 387 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 388 | while (bio_index < bio->bi_vcnt) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 389 | if (!contig) |
| 390 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
| 391 | |
| 392 | if (!contig && (offset >= ordered->file_offset + ordered->len || |
| 393 | offset < ordered->file_offset)) { |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 394 | unsigned long bytes_left; |
| 395 | sums->len = this_sum_bytes; |
| 396 | this_sum_bytes = 0; |
| 397 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 398 | btrfs_put_ordered_extent(ordered); |
| 399 | |
| 400 | bytes_left = bio->bi_size - total_bytes; |
| 401 | |
| 402 | sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left), |
| 403 | GFP_NOFS); |
| 404 | BUG_ON(!sums); |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 405 | sector_sum = sums->sums; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 406 | sums->len = bytes_left; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 407 | ordered = btrfs_lookup_ordered_extent(inode, offset); |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 408 | BUG_ON(!ordered); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 409 | sums->bytenr = ordered->start; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 410 | } |
| 411 | |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 412 | data = kmap_atomic(bvec->bv_page, KM_USER0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 413 | sector_sum->sum = ~(u32)0; |
| 414 | sector_sum->sum = btrfs_csum_data(root, |
| 415 | data + bvec->bv_offset, |
| 416 | sector_sum->sum, |
| 417 | bvec->bv_len); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 418 | kunmap_atomic(data, KM_USER0); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 419 | btrfs_csum_final(sector_sum->sum, |
| 420 | (char *)§or_sum->sum); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 421 | sector_sum->bytenr = disk_bytenr; |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 422 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 423 | sector_sum++; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 424 | bio_index++; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 425 | total_bytes += bvec->bv_len; |
| 426 | this_sum_bytes += bvec->bv_len; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 427 | disk_bytenr += bvec->bv_len; |
| 428 | offset += bvec->bv_len; |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 429 | bvec++; |
| 430 | } |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 431 | this_sum_bytes = 0; |
Chris Mason | 3edf7d3 | 2008-07-18 06:17:13 -0400 | [diff] [blame] | 432 | btrfs_add_ordered_sum(inode, ordered, sums); |
| 433 | btrfs_put_ordered_extent(ordered); |
Chris Mason | e015640 | 2008-04-16 11:15:20 -0400 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 437 | /* |
| 438 | * helper function for csum removal, this expects the |
| 439 | * key to describe the csum pointed to by the path, and it expects |
| 440 | * the csum to overlap the range [bytenr, len] |
| 441 | * |
| 442 | * The csum should not be entirely contained in the range and the |
| 443 | * range should not be entirely contained in the csum. |
| 444 | * |
| 445 | * This calls btrfs_truncate_item with the correct args based on the |
| 446 | * overlap, and fixes up the key as required. |
| 447 | */ |
| 448 | static noinline int truncate_one_csum(struct btrfs_trans_handle *trans, |
| 449 | struct btrfs_root *root, |
| 450 | struct btrfs_path *path, |
| 451 | struct btrfs_key *key, |
| 452 | u64 bytenr, u64 len) |
| 453 | { |
| 454 | struct extent_buffer *leaf; |
| 455 | u16 csum_size = |
| 456 | btrfs_super_csum_size(&root->fs_info->super_copy); |
| 457 | u64 csum_end; |
| 458 | u64 end_byte = bytenr + len; |
| 459 | u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits; |
| 460 | int ret; |
| 461 | |
| 462 | leaf = path->nodes[0]; |
| 463 | csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; |
| 464 | csum_end <<= root->fs_info->sb->s_blocksize_bits; |
| 465 | csum_end += key->offset; |
| 466 | |
| 467 | if (key->offset < bytenr && csum_end <= end_byte) { |
| 468 | /* |
| 469 | * [ bytenr - len ] |
| 470 | * [ ] |
| 471 | * [csum ] |
| 472 | * A simple truncate off the end of the item |
| 473 | */ |
| 474 | u32 new_size = (bytenr - key->offset) >> blocksize_bits; |
| 475 | new_size *= csum_size; |
| 476 | ret = btrfs_truncate_item(trans, root, path, new_size, 1); |
| 477 | BUG_ON(ret); |
| 478 | } else if (key->offset >= bytenr && csum_end > end_byte && |
| 479 | end_byte > key->offset) { |
| 480 | /* |
| 481 | * [ bytenr - len ] |
| 482 | * [ ] |
| 483 | * [csum ] |
| 484 | * we need to truncate from the beginning of the csum |
| 485 | */ |
| 486 | u32 new_size = (csum_end - end_byte) >> blocksize_bits; |
| 487 | new_size *= csum_size; |
| 488 | |
| 489 | ret = btrfs_truncate_item(trans, root, path, new_size, 0); |
| 490 | BUG_ON(ret); |
| 491 | |
| 492 | key->offset = end_byte; |
| 493 | ret = btrfs_set_item_key_safe(trans, root, path, key); |
| 494 | BUG_ON(ret); |
| 495 | } else { |
| 496 | BUG(); |
| 497 | } |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * deletes the csum items from the csum tree for a given |
| 503 | * range of bytes. |
| 504 | */ |
| 505 | int btrfs_del_csums(struct btrfs_trans_handle *trans, |
| 506 | struct btrfs_root *root, u64 bytenr, u64 len) |
| 507 | { |
| 508 | struct btrfs_path *path; |
| 509 | struct btrfs_key key; |
| 510 | u64 end_byte = bytenr + len; |
| 511 | u64 csum_end; |
| 512 | struct extent_buffer *leaf; |
| 513 | int ret; |
| 514 | u16 csum_size = |
| 515 | btrfs_super_csum_size(&root->fs_info->super_copy); |
| 516 | int blocksize_bits = root->fs_info->sb->s_blocksize_bits; |
| 517 | |
| 518 | root = root->fs_info->csum_root; |
| 519 | |
| 520 | path = btrfs_alloc_path(); |
| 521 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 522 | while (1) { |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 523 | key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 524 | key.offset = end_byte - 1; |
| 525 | key.type = BTRFS_EXTENT_CSUM_KEY; |
| 526 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 527 | path->leave_spinning = 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 528 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 529 | if (ret > 0) { |
| 530 | if (path->slots[0] == 0) |
| 531 | goto out; |
| 532 | path->slots[0]--; |
| 533 | } |
| 534 | leaf = path->nodes[0]; |
| 535 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 536 | |
| 537 | if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
| 538 | key.type != BTRFS_EXTENT_CSUM_KEY) { |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | if (key.offset >= end_byte) |
| 543 | break; |
| 544 | |
| 545 | csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size; |
| 546 | csum_end <<= blocksize_bits; |
| 547 | csum_end += key.offset; |
| 548 | |
| 549 | /* this csum ends before we start, we're done */ |
| 550 | if (csum_end <= bytenr) |
| 551 | break; |
| 552 | |
| 553 | /* delete the entire item, it is inside our range */ |
| 554 | if (key.offset >= bytenr && csum_end <= end_byte) { |
| 555 | ret = btrfs_del_item(trans, root, path); |
| 556 | BUG_ON(ret); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 557 | if (key.offset == bytenr) |
| 558 | break; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 559 | } else if (key.offset < bytenr && csum_end > end_byte) { |
| 560 | unsigned long offset; |
| 561 | unsigned long shift_len; |
| 562 | unsigned long item_offset; |
| 563 | /* |
| 564 | * [ bytenr - len ] |
| 565 | * [csum ] |
| 566 | * |
| 567 | * Our bytes are in the middle of the csum, |
| 568 | * we need to split this item and insert a new one. |
| 569 | * |
| 570 | * But we can't drop the path because the |
| 571 | * csum could change, get removed, extended etc. |
| 572 | * |
| 573 | * The trick here is the max size of a csum item leaves |
| 574 | * enough room in the tree block for a single |
| 575 | * item header. So, we split the item in place, |
| 576 | * adding a new header pointing to the existing |
| 577 | * bytes. Then we loop around again and we have |
| 578 | * a nicely formed csum item that we can neatly |
| 579 | * truncate. |
| 580 | */ |
| 581 | offset = (bytenr - key.offset) >> blocksize_bits; |
| 582 | offset *= csum_size; |
| 583 | |
| 584 | shift_len = (len >> blocksize_bits) * csum_size; |
| 585 | |
| 586 | item_offset = btrfs_item_ptr_offset(leaf, |
| 587 | path->slots[0]); |
| 588 | |
| 589 | memset_extent_buffer(leaf, 0, item_offset + offset, |
| 590 | shift_len); |
| 591 | key.offset = bytenr; |
| 592 | |
| 593 | /* |
| 594 | * btrfs_split_item returns -EAGAIN when the |
| 595 | * item changed size or key |
| 596 | */ |
| 597 | ret = btrfs_split_item(trans, root, path, &key, offset); |
| 598 | BUG_ON(ret && ret != -EAGAIN); |
| 599 | |
| 600 | key.offset = end_byte - 1; |
| 601 | } else { |
| 602 | ret = truncate_one_csum(trans, root, path, |
| 603 | &key, bytenr, len); |
| 604 | BUG_ON(ret); |
Chris Mason | dcbdd4d | 2008-12-16 13:51:01 -0500 | [diff] [blame] | 605 | if (key.offset < bytenr) |
| 606 | break; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 607 | } |
| 608 | btrfs_release_path(root, path); |
| 609 | } |
| 610 | out: |
| 611 | btrfs_free_path(path); |
| 612 | return 0; |
| 613 | } |
| 614 | |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 615 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 616 | struct btrfs_root *root, |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 617 | struct btrfs_ordered_sum *sums) |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 618 | { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 619 | u64 bytenr; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 620 | int ret; |
| 621 | struct btrfs_key file_key; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 622 | struct btrfs_key found_key; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 623 | u64 next_offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 624 | u64 total_bytes = 0; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 625 | int found_next; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 626 | struct btrfs_path *path; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 627 | struct btrfs_csum_item *item; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 628 | struct btrfs_csum_item *item_end; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 629 | struct extent_buffer *leaf = NULL; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 630 | u64 csum_offset; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 631 | struct btrfs_sector_sum *sector_sum; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 632 | u32 nritems; |
| 633 | u32 ins_size; |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 634 | char *eb_map; |
| 635 | char *eb_token; |
| 636 | unsigned long map_len; |
| 637 | unsigned long map_start; |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 638 | u16 csum_size = |
| 639 | btrfs_super_csum_size(&root->fs_info->super_copy); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 640 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 641 | path = btrfs_alloc_path(); |
| 642 | BUG_ON(!path); |
Chris Mason | ed98b56 | 2008-07-22 23:06:42 -0400 | [diff] [blame] | 643 | sector_sum = sums->sums; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 644 | again: |
| 645 | next_offset = (u64)-1; |
| 646 | found_next = 0; |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 647 | file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; |
| 648 | file_key.offset = sector_sum->bytenr; |
| 649 | bytenr = sector_sum->bytenr; |
| 650 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 651 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 652 | item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 653 | if (!IS_ERR(item)) { |
| 654 | leaf = path->nodes[0]; |
Chris Mason | 639cb58 | 2008-08-28 06:15:25 -0400 | [diff] [blame] | 655 | ret = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 656 | goto found; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 657 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 658 | ret = PTR_ERR(item); |
| 659 | if (ret == -EFBIG) { |
| 660 | u32 item_size; |
| 661 | /* we found one, but it isn't big enough yet */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 662 | leaf = path->nodes[0]; |
| 663 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 664 | if ((item_size / csum_size) >= |
| 665 | MAX_CSUM_ITEMS(root, csum_size)) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 666 | /* already at max size, make a new one */ |
| 667 | goto insert; |
| 668 | } |
| 669 | } else { |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 670 | int slot = path->slots[0] + 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 671 | /* we didn't find a csum item, insert one */ |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 672 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 673 | if (path->slots[0] >= nritems - 1) { |
| 674 | ret = btrfs_next_leaf(root, path); |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 675 | if (ret == 1) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 676 | found_next = 1; |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 677 | if (ret != 0) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 678 | goto insert; |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 679 | slot = 0; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 680 | } |
| 681 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 682 | if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
| 683 | found_key.type != BTRFS_EXTENT_CSUM_KEY) { |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 684 | found_next = 1; |
| 685 | goto insert; |
| 686 | } |
| 687 | next_offset = found_key.offset; |
| 688 | found_next = 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 689 | goto insert; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * at this point, we know the tree has an item, but it isn't big |
| 694 | * enough yet to put our csum in. Grow it |
| 695 | */ |
| 696 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 697 | ret = btrfs_search_slot(trans, root, &file_key, path, |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 698 | csum_size, 1); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 699 | if (ret < 0) |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 700 | goto fail_unlock; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 701 | |
| 702 | if (ret > 0) { |
| 703 | if (path->slots[0] == 0) |
| 704 | goto insert; |
| 705 | path->slots[0]--; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 706 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 707 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 708 | leaf = path->nodes[0]; |
| 709 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 710 | csum_offset = (bytenr - found_key.offset) >> |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 711 | root->fs_info->sb->s_blocksize_bits; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 712 | |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 713 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY || |
| 714 | found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID || |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 715 | csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 716 | goto insert; |
| 717 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 718 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 719 | if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 720 | csum_size) { |
| 721 | u32 diff = (csum_offset + 1) * csum_size; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 722 | |
| 723 | /* |
| 724 | * is the item big enough already? we dropped our lock |
| 725 | * before and need to recheck |
| 726 | */ |
| 727 | if (diff < btrfs_item_size_nr(leaf, path->slots[0])) |
| 728 | goto csum; |
| 729 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 730 | diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 731 | if (diff != csum_size) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 732 | goto insert; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 733 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 734 | ret = btrfs_extend_item(trans, root, path, diff); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 735 | BUG_ON(ret); |
| 736 | goto csum; |
| 737 | } |
| 738 | |
| 739 | insert: |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 740 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 741 | csum_offset = 0; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 742 | if (found_next) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 743 | u64 tmp = total_bytes + root->sectorsize; |
| 744 | u64 next_sector = sector_sum->bytenr; |
| 745 | struct btrfs_sector_sum *next = sector_sum + 1; |
| 746 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 747 | while (tmp < sums->len) { |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 748 | if (next_sector + root->sectorsize != next->bytenr) |
| 749 | break; |
| 750 | tmp += root->sectorsize; |
| 751 | next_sector = next->bytenr; |
| 752 | next++; |
| 753 | } |
| 754 | tmp = min(tmp, next_offset - file_key.offset); |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 755 | tmp >>= root->fs_info->sb->s_blocksize_bits; |
| 756 | tmp = max((u64)1, tmp); |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 757 | tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size)); |
| 758 | ins_size = csum_size * tmp; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 759 | } else { |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 760 | ins_size = csum_size; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 761 | } |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 762 | path->leave_spinning = 1; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 763 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 764 | ins_size); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 765 | path->leave_spinning = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 766 | if (ret < 0) |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 767 | goto fail_unlock; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 768 | if (ret != 0) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 769 | WARN_ON(1); |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 770 | goto fail_unlock; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 771 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 772 | csum: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 773 | leaf = path->nodes[0]; |
| 774 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 775 | ret = 0; |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 776 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 777 | csum_offset * csum_size); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 778 | found: |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 779 | item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
| 780 | item_end = (struct btrfs_csum_item *)((unsigned char *)item_end + |
| 781 | btrfs_item_size_nr(leaf, path->slots[0])); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 782 | eb_token = NULL; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 783 | next_sector: |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 784 | |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 785 | if (!eb_token || |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 786 | (unsigned long)item + csum_size >= map_start + map_len) { |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 787 | int err; |
| 788 | |
| 789 | if (eb_token) |
Chris Mason | eb20978 | 2008-02-21 09:30:08 -0500 | [diff] [blame] | 790 | unmap_extent_buffer(leaf, eb_token, KM_USER1); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 791 | eb_token = NULL; |
| 792 | err = map_private_extent_buffer(leaf, (unsigned long)item, |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 793 | csum_size, |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 794 | &eb_token, &eb_map, |
Chris Mason | eb20978 | 2008-02-21 09:30:08 -0500 | [diff] [blame] | 795 | &map_start, &map_len, KM_USER1); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 796 | if (err) |
| 797 | eb_token = NULL; |
| 798 | } |
| 799 | if (eb_token) { |
| 800 | memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)), |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 801 | §or_sum->sum, csum_size); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 802 | } else { |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 803 | write_extent_buffer(leaf, §or_sum->sum, |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 804 | (unsigned long)item, csum_size); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 805 | } |
Chris Mason | 7f3c74f | 2008-07-18 12:01:11 -0400 | [diff] [blame] | 806 | |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 807 | total_bytes += root->sectorsize; |
| 808 | sector_sum++; |
| 809 | if (total_bytes < sums->len) { |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 810 | item = (struct btrfs_csum_item *)((char *)item + |
Josef Bacik | 607d432 | 2008-12-02 07:17:45 -0500 | [diff] [blame] | 811 | csum_size); |
Chris Mason | d20f704 | 2008-12-08 16:58:54 -0500 | [diff] [blame] | 812 | if (item < item_end && bytenr + PAGE_CACHE_SIZE == |
| 813 | sector_sum->bytenr) { |
| 814 | bytenr = sector_sum->bytenr; |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 815 | goto next_sector; |
Chris Mason | 2e1a992 | 2008-02-20 15:44:32 -0500 | [diff] [blame] | 816 | } |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 817 | } |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 818 | if (eb_token) { |
Chris Mason | eb20978 | 2008-02-21 09:30:08 -0500 | [diff] [blame] | 819 | unmap_extent_buffer(leaf, eb_token, KM_USER1); |
Chris Mason | 6e92f5e | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 820 | eb_token = NULL; |
| 821 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 822 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | e6dcd2d | 2008-07-17 12:53:50 -0400 | [diff] [blame] | 823 | if (total_bytes < sums->len) { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 824 | btrfs_release_path(root, path); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 825 | cond_resched(); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame] | 826 | goto again; |
| 827 | } |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 828 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 829 | btrfs_free_path(path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 830 | return ret; |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 831 | |
| 832 | fail_unlock: |
Chris Mason | 5386323 | 2008-08-15 15:34:18 -0400 | [diff] [blame] | 833 | goto out; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 834 | } |