blob: 68859934ae25ca370106e4cafa911a0d04ea3776 [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 Mason2e635a22007-03-21 11:12:56 -040019#include <linux/module.h>
Chris Mason1e1d2702007-03-15 19:03:33 -040020#include "ctree.h"
Chris Masondee26a92007-03-26 16:00:06 -040021#include "disk-io.h"
Chris Mason9f5fae22007-03-20 14:38:32 -040022#include "transaction.h"
Chris Mason1de037a2007-05-29 15:17:08 -040023#include "print-tree.h"
Chris Mason1e1d2702007-03-15 19:03:33 -040024
Chris Mason6567e832007-04-16 09:22:45 -040025#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
Chris Mason509659c2007-05-10 12:36:17 -040026 sizeof(struct btrfs_item) * 2) / \
27 BTRFS_CRC32_SIZE) - 1))
Chris Masonb18c6682007-04-17 13:26:50 -040028int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Chris Masondee26a92007-03-26 16:00:06 -040029 struct btrfs_root *root,
Chris Masonb18c6682007-04-17 13:26:50 -040030 u64 objectid, u64 pos,
Chris Mason3a686372007-05-24 13:35:57 -040031 u64 offset, u64 disk_num_blocks,
32 u64 num_blocks)
Chris Mason9f5fae22007-03-20 14:38:32 -040033{
Chris Masondee26a92007-03-26 16:00:06 -040034 int ret = 0;
35 struct btrfs_file_extent_item *item;
36 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040037 struct btrfs_path *path;
Chris Masondee26a92007-03-26 16:00:06 -040038
Chris Mason5caf2a02007-04-02 11:20:42 -040039 path = btrfs_alloc_path();
40 BUG_ON(!path);
Chris Masondee26a92007-03-26 16:00:06 -040041 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040042 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040043 file_key.flags = 0;
44 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
45
Chris Mason5caf2a02007-04-02 11:20:42 -040046 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040047 sizeof(*item));
Chris Mason54aa1f42007-06-22 14:16:25 -040048 if (ret < 0)
49 goto out;
Chris Mason9773a782007-03-27 11:26:26 -040050 BUG_ON(ret);
Chris Mason5caf2a02007-04-02 11:20:42 -040051 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040052 struct btrfs_file_extent_item);
Chris Masonb18c6682007-04-17 13:26:50 -040053 btrfs_set_file_extent_disk_blocknr(item, offset);
Chris Mason3a686372007-05-24 13:35:57 -040054 btrfs_set_file_extent_disk_num_blocks(item, disk_num_blocks);
Chris Masondee26a92007-03-26 16:00:06 -040055 btrfs_set_file_extent_offset(item, 0);
Chris Masonb18c6682007-04-17 13:26:50 -040056 btrfs_set_file_extent_num_blocks(item, num_blocks);
Chris Mason71951f32007-03-27 09:16:29 -040057 btrfs_set_file_extent_generation(item, trans->transid);
Chris Mason236454d2007-04-19 13:37:44 -040058 btrfs_set_file_extent_type(item, BTRFS_FILE_EXTENT_REG);
Chris Mason5caf2a02007-04-02 11:20:42 -040059 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason54aa1f42007-06-22 14:16:25 -040060out:
Chris Mason5caf2a02007-04-02 11:20:42 -040061 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -040062 return ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040063}
Chris Masondee26a92007-03-26 16:00:06 -040064
Chris Masonb18c6682007-04-17 13:26:50 -040065struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
66 struct btrfs_root *root,
67 struct btrfs_path *path,
68 u64 objectid, u64 offset,
69 int cow)
Chris Mason6567e832007-04-16 09:22:45 -040070{
71 int ret;
72 struct btrfs_key file_key;
73 struct btrfs_key found_key;
74 struct btrfs_csum_item *item;
75 struct btrfs_leaf *leaf;
76 u64 csum_offset = 0;
Chris Masona429e512007-04-18 16:15:28 -040077 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040078
79 file_key.objectid = objectid;
80 file_key.offset = offset;
81 file_key.flags = 0;
82 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040083 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040084 if (ret < 0)
85 goto fail;
86 leaf = btrfs_buffer_leaf(path->nodes[0]);
87 if (ret > 0) {
88 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040089 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040090 goto fail;
91 path->slots[0]--;
92 btrfs_disk_key_to_cpu(&found_key,
93 &leaf->items[path->slots[0]].key);
94 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
95 found_key.objectid != objectid) {
96 goto fail;
97 }
98 csum_offset = (offset - found_key.offset) >>
99 root->fs_info->sb->s_blocksize_bits;
Chris Masona429e512007-04-18 16:15:28 -0400100 csums_in_item = btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400101 csums_in_item /= BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -0400102
103 if (csum_offset >= csums_in_item) {
104 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -0400105 goto fail;
106 }
107 }
108 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -0400109 item = (struct btrfs_csum_item *)((unsigned char *)item +
110 csum_offset * BTRFS_CRC32_SIZE);
Chris Mason6567e832007-04-16 09:22:45 -0400111 return item;
112fail:
113 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -0400114 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -0400115 return ERR_PTR(ret);
116}
117
118
Chris Masondee26a92007-03-26 16:00:06 -0400119int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
120 struct btrfs_root *root,
121 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400122 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400123{
124 int ret;
125 struct btrfs_key file_key;
126 int ins_len = mod < 0 ? -1 : 0;
127 int cow = mod != 0;
128
129 file_key.objectid = objectid;
Chris Mason70b2bef2007-04-17 15:39:32 -0400130 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400131 file_key.flags = 0;
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 Masonf254e522007-03-29 15:15:27 -0400136
137int btrfs_csum_file_block(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root,
139 u64 objectid, u64 offset,
140 char *data, size_t len)
141{
142 int ret;
143 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400144 struct btrfs_key found_key;
Chris Mason5caf2a02007-04-02 11:20:42 -0400145 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400146 struct btrfs_csum_item *item;
Chris Mason6567e832007-04-16 09:22:45 -0400147 struct btrfs_leaf *leaf;
148 u64 csum_offset;
Chris Masonf254e522007-03-29 15:15:27 -0400149
Chris Mason5caf2a02007-04-02 11:20:42 -0400150 path = btrfs_alloc_path();
151 BUG_ON(!path);
Chris Masonb18c6682007-04-17 13:26:50 -0400152
Chris Masonf254e522007-03-29 15:15:27 -0400153 file_key.objectid = objectid;
154 file_key.offset = offset;
155 file_key.flags = 0;
156 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400157
158 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
159 if (!IS_ERR(item))
160 goto found;
161 ret = PTR_ERR(item);
162 if (ret == -EFBIG) {
163 u32 item_size;
164 /* we found one, but it isn't big enough yet */
165 leaf = btrfs_buffer_leaf(path->nodes[0]);
166 item_size = btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400167 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
Chris Masona429e512007-04-18 16:15:28 -0400168 /* already at max size, make a new one */
169 goto insert;
170 }
171 } else {
172 /* we didn't find a csum item, insert one */
173 goto insert;
174 }
175
176 /*
177 * at this point, we know the tree has an item, but it isn't big
178 * enough yet to put our csum in. Grow it
179 */
180 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400181 ret = btrfs_search_slot(trans, root, &file_key, path,
Chris Mason509659c2007-05-10 12:36:17 -0400182 BTRFS_CRC32_SIZE, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400183 if (ret < 0)
184 goto fail;
185 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400186 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400187 }
188 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400189 goto insert;
190 }
191 path->slots[0]--;
192 leaf = btrfs_buffer_leaf(path->nodes[0]);
193 btrfs_disk_key_to_cpu(&found_key, &leaf->items[path->slots[0]].key);
194 csum_offset = (offset - found_key.offset) >>
195 root->fs_info->sb->s_blocksize_bits;
196 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
197 found_key.objectid != objectid ||
198 csum_offset >= MAX_CSUM_ITEMS(root)) {
Chris Mason6567e832007-04-16 09:22:45 -0400199 goto insert;
200 }
201 if (csum_offset >= btrfs_item_size(leaf->items + path->slots[0]) /
Chris Mason509659c2007-05-10 12:36:17 -0400202 BTRFS_CRC32_SIZE) {
203 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -0400204 diff = diff - btrfs_item_size(leaf->items + path->slots[0]);
Chris Mason3a686372007-05-24 13:35:57 -0400205 if (diff != BTRFS_CRC32_SIZE)
206 goto insert;
Chris Masona429e512007-04-18 16:15:28 -0400207 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400208 BUG_ON(ret);
209 goto csum;
210 }
211
212insert:
Chris Masona429e512007-04-18 16:15:28 -0400213 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400214 csum_offset = 0;
Chris Mason5caf2a02007-04-02 11:20:42 -0400215 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Mason509659c2007-05-10 12:36:17 -0400216 BTRFS_CRC32_SIZE);
Chris Mason54aa1f42007-06-22 14:16:25 -0400217 if (ret < 0)
218 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400219 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400220 WARN_ON(1);
Chris Masonf254e522007-03-29 15:15:27 -0400221 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400222 }
Chris Mason6567e832007-04-16 09:22:45 -0400223csum:
Chris Mason5caf2a02007-04-02 11:20:42 -0400224 item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
Chris Masonf254e522007-03-29 15:15:27 -0400225 struct btrfs_csum_item);
226 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400227 item = (struct btrfs_csum_item *)((unsigned char *)item +
228 csum_offset * BTRFS_CRC32_SIZE);
Chris Masonb18c6682007-04-17 13:26:50 -0400229found:
Chris Mason509659c2007-05-10 12:36:17 -0400230 btrfs_check_bounds(&item->csum, BTRFS_CRC32_SIZE,
231 path->nodes[0]->b_data,
232 root->fs_info->sb->s_blocksize);
233 ret = btrfs_csum_data(root, data, len, &item->csum);
Chris Mason5caf2a02007-04-02 11:20:42 -0400234 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masonf254e522007-03-29 15:15:27 -0400235fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400236 btrfs_release_path(root, path);
237 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400238 return ret;
239}
240
Chris Mason1de037a2007-05-29 15:17:08 -0400241int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
242 struct btrfs_root *root, struct btrfs_path *path,
243 u64 isize)
244{
245 struct btrfs_key key;
246 struct btrfs_leaf *leaf = btrfs_buffer_leaf(path->nodes[0]);
247 int slot = path->slots[0];
248 int ret;
249 u32 new_item_size;
250 u64 new_item_span;
251 u64 blocks;
252
253 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
254 if (isize <= key.offset)
255 return 0;
256 new_item_span = isize - key.offset;
Chris Mason84f54cf2007-06-12 07:43:08 -0400257 blocks = (new_item_span + root->blocksize - 1) >>
258 root->fs_info->sb->s_blocksize_bits;
Chris Mason1de037a2007-05-29 15:17:08 -0400259 new_item_size = blocks * BTRFS_CRC32_SIZE;
260 if (new_item_size >= btrfs_item_size(leaf->items + slot))
261 return 0;
262 ret = btrfs_truncate_item(trans, root, path, new_item_size);
263 BUG_ON(ret);
264 return ret;
265}
266