blob: ea4dd4c5fce3ef211471e127b7e00892fe5b0f54 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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 Mason1e1d2702007-03-15 19:03:33 -040019#include "ctree.h"
Chris Masondee26a92007-03-26 16:00:06 -040020#include "disk-io.h"
Chris Mason9f5fae22007-03-20 14:38:32 -040021#include "transaction.h"
Chris Mason1de037a2007-05-29 15:17:08 -040022#include "print-tree.h"
Chris Mason1e1d2702007-03-15 19:03:33 -040023
Chris Mason6567e832007-04-16 09:22:45 -040024#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
Chris Mason509659c2007-05-10 12:36:17 -040025 sizeof(struct btrfs_item) * 2) / \
26 BTRFS_CRC32_SIZE) - 1))
Chris Masonb18c6682007-04-17 13:26:50 -040027int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Chris Masondee26a92007-03-26 16:00:06 -040028 struct btrfs_root *root,
Chris Masonb18c6682007-04-17 13:26:50 -040029 u64 objectid, u64 pos,
Chris Mason3a686372007-05-24 13:35:57 -040030 u64 offset, u64 disk_num_blocks,
31 u64 num_blocks)
Chris Mason9f5fae22007-03-20 14:38:32 -040032{
Chris Masondee26a92007-03-26 16:00:06 -040033 int ret = 0;
34 struct btrfs_file_extent_item *item;
35 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040036 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -040037
Chris Mason5caf2a02007-04-02 11:20:42 -040038 path = btrfs_alloc_path();
39 BUG_ON(!path);
Chris Masondee26a92007-03-26 16:00:06 -040040 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040041 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040042 file_key.flags = 0;
43 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
44
Chris Mason5caf2a02007-04-02 11:20:42 -040045 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040046 sizeof(*item));
Chris Mason54aa1f42007-06-22 14:16:25 -040047 if (ret < 0)
48 goto out;
Chris Mason9773a782007-03-27 11:26:26 -040049 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040050 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040051 struct btrfs_file_extent_item);
Chris Masonb18c6682007-04-17 13:26:50 -040052 btrfs_set_file_extent_disk_blocknr(item, offset);
Chris Mason3a686372007-05-24 13:35:57 -040053 btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks);
Chris Masondee26a92007-03-26 16:00:06 -040054 btrfs_set_file_extent_offset(item, 0);
Chris Masonb18c6682007-04-17 13:26:50 -040055 btrfs_set_file_extent_num_blocks(item, num_blocks);
Chris Mason71951f32007-03-27 09:16:29 -040056 btrfs_set_file_extent_generation(item, trans->transid);
Chris Mason236454d2007-04-19 13:37:44 -040057 btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG);
Chris Mason5caf2a02007-04-02 11:20:42 -040058 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason54aa1f42007-06-22 14:16:25 -040059out:
Chris Mason5caf2a02007-04-02 11:20:42 -040060 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -040061 return ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040062}
Chris Masondee26a92007-03-26 16:00:06 -040063
Chris Masonb18c6682007-04-17 13:26:50 -040064struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
65 struct btrfs_root *root,
66 struct btrfs_path *path,
67 u64 objectid, u64 offset,
68 int cow)
Chris Mason6567e832007-04-16 09:22:45 -040069{
70 int ret;
71 struct btrfs_key file_key;
72 struct btrfs_key found_key;
73 struct btrfs_csum_item *item;
74 struct btrfs_leaf *leaf;
75 u64 csum_offset = 0;
Chris Masona429e512007-04-18 16:15:28 -040076 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040077
78 file_key.objectid = objectid;
79 file_key.offset = offset;
80 file_key.flags = 0;
81 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040082 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040083 if (ret < 0)
84 goto fail;
85 leaf = btrfs_buffer_leaf(path->nodes[0]);
86 if (ret > 0) {
87 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040088 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040089 goto fail;
90 path->slots[0]--;
91 btrfs_disk_key_to_cpu(&found_key,
92 &leaf->items[path->slots[0]].key);
93 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
94 found_key.objectid != objectid) {
95 goto fail;
96 }
97 csum_offset = (offset - found_key.offset) >>
98 root->fs_info->sb->s_blocksize_bits;
Chris Masona429e512007-04-18 16:15:28 -040099 csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400100 csums_in_item /= BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -0400101
102 if (csum_offset >= csums_in_item) {
103 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -0400104 goto fail;
105 }
106 }
107 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -0400108 item = (struct btrfs_csum_item *)((unsigned char *)item +
109 csum_offset * BTRFS_CRC32_SIZE);
Chris Mason6567e832007-04-16 09:22:45 -0400110 return item;
111fail:
112 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -0400113 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -0400114 return ERR_PTR(ret);
115}
116
117
Chris Masondee26a92007-03-26 16:00:06 -0400118int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
119 struct btrfs_root *root,
120 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400121 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400122{
123 int ret;
124 struct btrfs_key file_key;
125 int ins_len = mod < 0 ? -1 : 0;
126 int cow = mod != 0;
127
128 file_key.objectid = objectid;
Chris Mason70b2bef2007-04-17 15:39:32 -0400129 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400130 file_key.flags = 0;
131 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
132 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
133 return ret;
134}
Chris Masonf254e522007-03-29 15:15:27 -0400135
136int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
137 struct btrfs_root *root,
138 u64 objectid, u64 offset,
139 char *data, size_t len)
140{
141 int ret;
142 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400143 struct btrfs_key found_key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400144 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400145 struct btrfs_csum_item *item;
Chris Mason6567e832007-04-16 09:22:45 -0400146 struct btrfs_leaf *leaf;
147 u64 csum_offset;
Chris Masonf254e522007-03-29 15:15:27 -0400148
Chris Mason5caf2a02007-04-02 11:20:42 -0400149 path = btrfs_alloc_path();
150 BUG_ON(!path);
Chris Masonb18c6682007-04-17 13:26:50 -0400151
Chris Masonf254e522007-03-29 15:15:27 -0400152 file_key.objectid = objectid;
153 file_key.offset = offset;
154 file_key.flags = 0;
155 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400156
157 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
158 if (!IS_ERR(item))
159 goto found;
160 ret = PTR_ERR(item);
161 if (ret == -EFBIG) {
162 u32 item_size;
163 /* we found one, but it isn't big enough yet */
164 leaf = btrfs_buffer_leaf(path->nodes[0]);
165 item_size = btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400166 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
Chris Masona429e512007-04-18 16:15:28 -0400167 /* already at max size, make a new one */
168 goto insert;
169 }
170 } else {
171 /* we didn't find a csum item, insert one */
172 goto insert;
173 }
174
175 /*
176 * at this point, we know the tree has an item, but it isn't big
177 * enough yet to put our csum in. Grow it
178 */
179 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400180 ret = btrfs_search_slot(trans, root, &file_key, path,
Chris Mason509659c2007-05-10 12:36:17 -0400181 BTRFS_CRC32_SIZE, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400182 if (ret < 0)
183 goto fail;
184 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400185 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400186 }
187 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400188 goto insert;
189 }
190 path->slots[0]--;
191 leaf = btrfs_buffer_leaf(path->nodes[0]);
192 btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key);
193 csum_offset = (offset - found_key.offset) >>
194 root->fs_info->sb->s_blocksize_bits;
195 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
196 found_key.objectid != objectid ||
197 csum_offset >= MAX_CSUM_ITEMS(root)) {
Chris Mason6567e832007-04-16 09:22:45 -0400198 goto insert;
199 }
200 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
Chris Mason509659c2007-05-10 12:36:17 -0400201 BTRFS_CRC32_SIZE) {
202 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -0400203 diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason3a686372007-05-24 13:35:57 -0400204 if (diff != BTRFS_CRC32_SIZE)
205 goto insert;
Chris Masona429e512007-04-18 16:15:28 -0400206 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400207 BUG_ON(ret);
208 goto csum;
209 }
210
211insert:
Chris Masona429e512007-04-18 16:15:28 -0400212 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400213 csum_offset = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400214 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Mason509659c2007-05-10 12:36:17 -0400215 BTRFS_CRC32_SIZE);
Chris Mason54aa1f42007-06-22 14:16:25 -0400216 if (ret < 0)
217 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400218 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400219 WARN_ON(1);
Chris Masonf254e522007-03-29 15:15:27 -0400220 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400221 }
Chris Mason6567e832007-04-16 09:22:45 -0400222csum:
Chris Mason5caf2a02007-04-02 11:20:42 -0400223 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400224 struct btrfs_csum_item);
225 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400226 item = (struct btrfs_csum_item *)((unsigned char *)item +
227 csum_offset * BTRFS_CRC32_SIZE);
Chris Masonb18c6682007-04-17 13:26:50 -0400228found:
Chris Mason509659c2007-05-10 12:36:17 -0400229 btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE,
230 path->nodes[0]->b_data,
231 root->fs_info->sb->s_blocksize);
232 ret = btrfs_csum_data(root, data, len, &item->csum);
Chris Mason5caf2a02007-04-02 11:20:42 -0400233 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masonf254e522007-03-29 15:15:27 -0400234fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400235 btrfs_release_path(root, path);
236 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400237 return ret;
238}
239
Chris Mason1de037a2007-05-29 15:17:08 -0400240int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root, struct btrfs_path *path,
242 u64 isize)
243{
244 struct btrfs_key key;
245 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]);
246 int slot = path->slots[0];
247 int ret;
248 u32 new_item_size;
249 u64 new_item_span;
250 u64 blocks;
251
252 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
253 if (isize <= key.offset)
254 return 0;
255 new_item_span = isize - key.offset;
Chris Mason84f54cf2007-06-12 07:43:08 -0400256 blocks = (new_item_span + root->blocksize - 1) >>
257 root->fs_info->sb->s_blocksize_bits;
Chris Mason1de037a2007-05-29 15:17:08 -0400258 new_item_size = blocks * BTRFS_CRC32_SIZE;
259 if (new_item_size >= btrfs_item_size(leaf->items + slot))
260 return 0;
261 ret = btrfs_truncate_item(trans, root, path, new_item_size);
262 BUG_ON(ret);
263 return ret;
264}
265