Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 2 | #include "ctree.h" |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 3 | #include "disk-io.h" |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 4 | #include "transaction.h" |
Chris Mason | 1e1d270 | 2007-03-15 19:03:33 -0400 | [diff] [blame] | 5 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 6 | #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \ |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 7 | sizeof(struct btrfs_item) * 2) / \ |
| 8 | BTRFS_CRC32_SIZE) - 1)) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 9 | int btrfs_insert_file_extent(struct btrfs_trans_handle *trans, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 10 | struct btrfs_root *root, |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 11 | u64 objectid, u64 pos, |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame^] | 12 | u64 offset, u64 disk_num_blocks, |
| 13 | u64 num_blocks) |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 14 | { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 15 | int ret = 0; |
| 16 | struct btrfs_file_extent_item *item; |
| 17 | struct btrfs_key file_key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 18 | struct btrfs_path *path; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 19 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 20 | path = btrfs_alloc_path(); |
| 21 | BUG_ON(!path); |
| 22 | btrfs_init_path(path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 23 | file_key.objectid = objectid; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 24 | file_key.offset = pos; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 25 | file_key.flags = 0; |
| 26 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 27 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 28 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 29 | sizeof(*item)); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 30 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 31 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 32 | struct btrfs_file_extent_item); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 33 | btrfs_set_file_extent_disk_blocknr(item, offset); |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame^] | 34 | btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 35 | btrfs_set_file_extent_offset(item, 0); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 36 | btrfs_set_file_extent_num_blocks(item, num_blocks); |
Chris Mason | 71951f3 | 2007-03-27 09:16:29 -0400 | [diff] [blame] | 37 | btrfs_set_file_extent_generation(item, trans->transid); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 38 | btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 39 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 40 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 41 | btrfs_release_path(root, path); |
| 42 | btrfs_free_path(path); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 43 | return 0; |
| 44 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 45 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 46 | struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans, |
| 47 | struct btrfs_root *root, |
| 48 | struct btrfs_path *path, |
| 49 | u64 objectid, u64 offset, |
| 50 | int cow) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 51 | { |
| 52 | int ret; |
| 53 | struct btrfs_key file_key; |
| 54 | struct btrfs_key found_key; |
| 55 | struct btrfs_csum_item *item; |
| 56 | struct btrfs_leaf *leaf; |
| 57 | u64 csum_offset = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 58 | int csums_in_item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 59 | |
| 60 | file_key.objectid = objectid; |
| 61 | file_key.offset = offset; |
| 62 | file_key.flags = 0; |
| 63 | btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 64 | ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 65 | if (ret < 0) |
| 66 | goto fail; |
| 67 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 68 | if (ret > 0) { |
| 69 | ret = 1; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 70 | if (path->slots[0] == 0) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 71 | goto fail; |
| 72 | path->slots[0]--; |
| 73 | btrfs_disk_key_to_cpu(&found_key, |
| 74 | &leaf->items[path->slots[0]].key); |
| 75 | if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || |
| 76 | found_key.objectid != objectid) { |
| 77 | goto fail; |
| 78 | } |
| 79 | csum_offset = (offset - found_key.offset) >> |
| 80 | root->fs_info->sb->s_blocksize_bits; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 81 | csums_in_item = btrfs_item_size(leaf->items + path->slots[0]); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 82 | csums_in_item /= BTRFS_CRC32_SIZE; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 83 | |
| 84 | if (csum_offset >= csums_in_item) { |
| 85 | ret = -EFBIG; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 86 | goto fail; |
| 87 | } |
| 88 | } |
| 89 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 90 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
| 91 | csum_offset * BTRFS_CRC32_SIZE); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 92 | return item; |
| 93 | fail: |
| 94 | if (ret > 0) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 95 | ret = -ENOENT; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 96 | return ERR_PTR(ret); |
| 97 | } |
| 98 | |
| 99 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 100 | int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans, |
| 101 | struct btrfs_root *root, |
| 102 | struct btrfs_path *path, u64 objectid, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 103 | u64 offset, int mod) |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 104 | { |
| 105 | int ret; |
| 106 | struct btrfs_key file_key; |
| 107 | int ins_len = mod < 0 ? -1 : 0; |
| 108 | int cow = mod != 0; |
| 109 | |
| 110 | file_key.objectid = objectid; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 111 | file_key.offset = offset; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 112 | file_key.flags = 0; |
| 113 | btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY); |
| 114 | ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow); |
| 115 | return ret; |
| 116 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 117 | |
| 118 | int btrfs_csum_file_block(struct btrfs_trans_handle *trans, |
| 119 | struct btrfs_root *root, |
| 120 | u64 objectid, u64 offset, |
| 121 | char *data, size_t len) |
| 122 | { |
| 123 | int ret; |
| 124 | struct btrfs_key file_key; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 125 | struct btrfs_key found_key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 126 | struct btrfs_path *path; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 127 | struct btrfs_csum_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 128 | struct btrfs_leaf *leaf; |
| 129 | u64 csum_offset; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 130 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 131 | path = btrfs_alloc_path(); |
| 132 | BUG_ON(!path); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 133 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 134 | file_key.objectid = objectid; |
| 135 | file_key.offset = offset; |
| 136 | file_key.flags = 0; |
| 137 | btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 138 | |
| 139 | item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1); |
| 140 | if (!IS_ERR(item)) |
| 141 | goto found; |
| 142 | ret = PTR_ERR(item); |
| 143 | if (ret == -EFBIG) { |
| 144 | u32 item_size; |
| 145 | /* we found one, but it isn't big enough yet */ |
| 146 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 147 | item_size = btrfs_item_size(leaf->items + path->slots[0]); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 148 | if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 149 | /* already at max size, make a new one */ |
| 150 | goto insert; |
| 151 | } |
| 152 | } else { |
| 153 | /* we didn't find a csum item, insert one */ |
| 154 | goto insert; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * at this point, we know the tree has an item, but it isn't big |
| 159 | * enough yet to put our csum in. Grow it |
| 160 | */ |
| 161 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 162 | ret = btrfs_search_slot(trans, root, &file_key, path, |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 163 | BTRFS_CRC32_SIZE, 1); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 164 | if (ret < 0) |
| 165 | goto fail; |
| 166 | if (ret == 0) { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 167 | BUG(); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 168 | } |
| 169 | if (path->slots[0] == 0) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 170 | goto insert; |
| 171 | } |
| 172 | path->slots[0]--; |
| 173 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 174 | btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key); |
| 175 | csum_offset = (offset - found_key.offset) >> |
| 176 | root->fs_info->sb->s_blocksize_bits; |
| 177 | if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || |
| 178 | found_key.objectid != objectid || |
| 179 | csum_offset >= MAX_CSUM_ITEMS(root)) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 180 | goto insert; |
| 181 | } |
| 182 | if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) / |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 183 | BTRFS_CRC32_SIZE) { |
| 184 | u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 185 | diff = diff - btrfs_item_size(leaf->items + path->slots[0]); |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame^] | 186 | if (diff != BTRFS_CRC32_SIZE) |
| 187 | goto insert; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 188 | ret = btrfs_extend_item(trans, root, path, diff); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 189 | BUG_ON(ret); |
| 190 | goto csum; |
| 191 | } |
| 192 | |
| 193 | insert: |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 194 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 195 | csum_offset = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 196 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 197 | BTRFS_CRC32_SIZE); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 198 | if (ret != 0) { |
| 199 | printk("at insert for %Lu %u %Lu ret is %d\n", file_key.objectid, file_key.flags, file_key.offset, ret); |
| 200 | WARN_ON(1); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 201 | goto fail; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 202 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 203 | csum: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 204 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 205 | struct btrfs_csum_item); |
| 206 | ret = 0; |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 207 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
| 208 | csum_offset * BTRFS_CRC32_SIZE); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 209 | found: |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 210 | btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE, |
| 211 | path->nodes[0]->b_data, |
| 212 | root->fs_info->sb->s_blocksize); |
| 213 | ret = btrfs_csum_data(root, data, len, &item->csum); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 214 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 215 | fail: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 216 | btrfs_release_path(root, path); |
| 217 | btrfs_free_path(path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | int btrfs_csum_verify_file_block(struct btrfs_root *root, |
| 222 | u64 objectid, u64 offset, |
| 223 | char *data, size_t len) |
| 224 | { |
| 225 | int ret; |
| 226 | struct btrfs_key file_key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 227 | struct btrfs_path *path; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 228 | struct btrfs_csum_item *item; |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 229 | char result[BTRFS_CRC32_SIZE]; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 230 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 231 | path = btrfs_alloc_path(); |
| 232 | BUG_ON(!path); |
| 233 | btrfs_init_path(path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 234 | file_key.objectid = objectid; |
| 235 | file_key.offset = offset; |
| 236 | file_key.flags = 0; |
| 237 | btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | 2da566e | 2007-04-02 15:43:21 -0400 | [diff] [blame] | 238 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 239 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 240 | item = btrfs_lookup_csum(NULL, root, path, objectid, offset, 0); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 241 | if (IS_ERR(item)) { |
| 242 | ret = PTR_ERR(item); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 243 | /* a csum that isn't present is a preallocated region. */ |
| 244 | if (ret == -ENOENT || ret == -EFBIG) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame^] | 245 | ret = -ENOENT; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 246 | goto fail; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 247 | } |
| 248 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 249 | ret = btrfs_csum_data(root, data, len, result); |
| 250 | WARN_ON(ret); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 251 | if (memcmp(result, &item->csum, BTRFS_CRC32_SIZE)) |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 252 | ret = 1; |
| 253 | fail: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 254 | btrfs_release_path(root, path); |
| 255 | btrfs_free_path(path); |
Chris Mason | 2da566e | 2007-04-02 15:43:21 -0400 | [diff] [blame] | 256 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 257 | return ret; |
| 258 | } |
| 259 | |