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