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, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 31 | struct btrfs_root *root, |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 32 | u64 objectid, u64 pos, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 33 | u64 offset, u64 disk_num_bytes, |
| 34 | u64 num_bytes) |
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); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 56 | btrfs_set_file_extent_disk_bytenr(leaf, item, offset); |
| 57 | btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 58 | btrfs_set_file_extent_offset(leaf, item, 0); |
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 | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 137 | int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans, |
| 138 | struct btrfs_root *root, struct inode *inode, |
| 139 | struct bio *bio) |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 140 | { |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 141 | u64 objectid = inode->i_ino; |
| 142 | u64 offset; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 143 | int ret; |
| 144 | struct btrfs_key file_key; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 145 | struct btrfs_key found_key; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 146 | u64 next_offset; |
| 147 | int found_next; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 148 | struct btrfs_path *path; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 149 | struct btrfs_csum_item *item; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 150 | struct btrfs_csum_item *item_end; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 151 | struct extent_buffer *leaf = NULL; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 152 | u64 csum_offset; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 153 | u32 csum_result; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 154 | u32 nritems; |
| 155 | u32 ins_size; |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 156 | int bio_index = 0; |
| 157 | struct bio_vec *bvec = bio->bi_io_vec; |
| 158 | char *data; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 159 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 160 | path = btrfs_alloc_path(); |
| 161 | BUG_ON(!path); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 162 | again: |
| 163 | next_offset = (u64)-1; |
| 164 | found_next = 0; |
| 165 | offset = page_offset(bvec->bv_page) + bvec->bv_offset; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 166 | file_key.objectid = objectid; |
| 167 | file_key.offset = offset; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 168 | btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 169 | |
| 170 | item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 171 | if (!IS_ERR(item)) { |
| 172 | leaf = path->nodes[0]; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 173 | goto found; |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 174 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 175 | ret = PTR_ERR(item); |
| 176 | if (ret == -EFBIG) { |
| 177 | u32 item_size; |
| 178 | /* we found one, but it isn't big enough yet */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 179 | leaf = path->nodes[0]; |
| 180 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 181 | if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 182 | /* already at max size, make a new one */ |
| 183 | goto insert; |
| 184 | } |
| 185 | } else { |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 186 | int slot = path->slots[0] + 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 187 | /* we didn't find a csum item, insert one */ |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 188 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 189 | if (path->slots[0] >= nritems - 1) { |
| 190 | ret = btrfs_next_leaf(root, path); |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 191 | if (ret == 1) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 192 | found_next = 1; |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 193 | if (ret != 0) |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 194 | goto insert; |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 195 | slot = 0; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 196 | } |
| 197 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot); |
| 198 | if (found_key.objectid != objectid || |
| 199 | found_key.type != BTRFS_CSUM_ITEM_KEY) { |
| 200 | found_next = 1; |
| 201 | goto insert; |
| 202 | } |
| 203 | next_offset = found_key.offset; |
| 204 | found_next = 1; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 205 | goto insert; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * at this point, we know the tree has an item, but it isn't big |
| 210 | * enough yet to put our csum in. Grow it |
| 211 | */ |
| 212 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 213 | ret = btrfs_search_slot(trans, root, &file_key, path, |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 214 | BTRFS_CRC32_SIZE, 1); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 215 | if (ret < 0) |
| 216 | goto fail; |
| 217 | if (ret == 0) { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 218 | BUG(); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 219 | } |
| 220 | if (path->slots[0] == 0) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 221 | goto insert; |
| 222 | } |
| 223 | path->slots[0]--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 224 | leaf = path->nodes[0]; |
| 225 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 226 | csum_offset = (offset - found_key.offset) >> |
| 227 | root->fs_info->sb->s_blocksize_bits; |
| 228 | if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY || |
| 229 | found_key.objectid != objectid || |
| 230 | csum_offset >= MAX_CSUM_ITEMS(root)) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 231 | goto insert; |
| 232 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 233 | if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) / |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 234 | BTRFS_CRC32_SIZE) { |
| 235 | u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 236 | diff = diff - btrfs_item_size_nr(leaf, path->slots[0]); |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 237 | if (diff != BTRFS_CRC32_SIZE) |
| 238 | goto insert; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 239 | ret = btrfs_extend_item(trans, root, path, diff); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 240 | BUG_ON(ret); |
| 241 | goto csum; |
| 242 | } |
| 243 | |
| 244 | insert: |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 245 | btrfs_release_path(root, path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 246 | csum_offset = 0; |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 247 | if (found_next) { |
| 248 | u64 tmp = min((u64)i_size_read(inode), next_offset); |
Yan | b56baf5 | 2007-10-29 12:01:05 -0400 | [diff] [blame] | 249 | tmp -= offset & ~((u64)root->sectorsize -1); |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 250 | tmp >>= root->fs_info->sb->s_blocksize_bits; |
| 251 | tmp = max((u64)1, tmp); |
| 252 | tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root)); |
| 253 | ins_size = BTRFS_CRC32_SIZE * tmp; |
| 254 | } else { |
| 255 | ins_size = BTRFS_CRC32_SIZE; |
| 256 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 257 | ret = btrfs_insert_empty_item(trans, root, path, &file_key, |
Chris Mason | f578d4b | 2007-10-25 15:42:56 -0400 | [diff] [blame] | 258 | ins_size); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 259 | if (ret < 0) |
| 260 | goto fail; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 261 | if (ret != 0) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 262 | WARN_ON(1); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 263 | goto fail; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 264 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 265 | csum: |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 266 | leaf = path->nodes[0]; |
| 267 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 268 | ret = 0; |
Chris Mason | 509659c | 2007-05-10 12:36:17 -0400 | [diff] [blame] | 269 | item = (struct btrfs_csum_item *)((unsigned char *)item + |
| 270 | csum_offset * BTRFS_CRC32_SIZE); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 271 | found: |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 272 | item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item); |
| 273 | item_end = (struct btrfs_csum_item *)((unsigned char *)item_end + |
| 274 | btrfs_item_size_nr(leaf, path->slots[0])); |
| 275 | next_bvec: |
| 276 | data = kmap_atomic(bvec->bv_page, KM_IRQ0); |
| 277 | csum_result = ~(u32)0; |
| 278 | csum_result = btrfs_csum_data(root, data + bvec->bv_offset, |
| 279 | csum_result, bvec->bv_len); |
| 280 | kunmap_atomic(data, KM_IRQ0); |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 281 | btrfs_csum_final(csum_result, (char *)&csum_result); |
Chris Mason | aadfeb6 | 2008-01-29 09:10:27 -0500 | [diff] [blame] | 282 | if (csum_result == 0) { |
| 283 | printk("csum result is 0 for inode %lu offset %Lu\n", inode->i_ino, offset); |
| 284 | } |
| 285 | |
Chris Mason | ff79f81 | 2007-10-15 16:22:25 -0400 | [diff] [blame] | 286 | write_extent_buffer(leaf, &csum_result, (unsigned long)item, |
| 287 | BTRFS_CRC32_SIZE); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 288 | bio_index++; |
| 289 | bvec++; |
| 290 | if (bio_index < bio->bi_vcnt) { |
| 291 | item = (struct btrfs_csum_item *)((char *)item + BTRFS_CRC32_SIZE); |
| 292 | if (item < item_end) |
| 293 | goto next_bvec; |
| 294 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 295 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 065631f | 2008-02-20 12:07:25 -0500 | [diff] [blame^] | 296 | if (bio_index < bio->bi_vcnt) { |
| 297 | btrfs_release_path(root, path); |
| 298 | goto again; |
| 299 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 300 | fail: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 301 | btrfs_free_path(path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 302 | return ret; |
| 303 | } |
| 304 | |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 305 | int btrfs_csum_truncate(struct btrfs_trans_handle *trans, |
| 306 | struct btrfs_root *root, struct btrfs_path *path, |
| 307 | u64 isize) |
| 308 | { |
| 309 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 310 | struct extent_buffer *leaf = path->nodes[0]; |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 311 | int slot = path->slots[0]; |
| 312 | int ret; |
| 313 | u32 new_item_size; |
| 314 | u64 new_item_span; |
| 315 | u64 blocks; |
| 316 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 317 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 318 | if (isize <= key.offset) |
| 319 | return 0; |
| 320 | new_item_span = isize - key.offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 321 | blocks = (new_item_span + root->sectorsize - 1) >> |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 322 | root->fs_info->sb->s_blocksize_bits; |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 323 | new_item_size = blocks * BTRFS_CRC32_SIZE; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 324 | if (new_item_size >= btrfs_item_size_nr(leaf, slot)) |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 325 | return 0; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 326 | ret = btrfs_truncate_item(trans, root, path, new_item_size, 1); |
Chris Mason | 1de037a | 2007-05-29 15:17:08 -0400 | [diff] [blame] | 327 | BUG_ON(ret); |
| 328 | return ret; |
| 329 | } |
| 330 | |