blob: e17cce33fe95873f01157943b5a416325dd64b94 [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 Mason065631f2008-02-20 12:07:25 -050019#include <linux/bio.h>
20#include <linux/pagemap.h>
21#include <linux/highmem.h>
Chris Mason1e1d2702007-03-15 19:03:33 -040022#include "ctree.h"
Chris Masondee26a92007-03-26 16:00:06 -040023#include "disk-io.h"
Chris Mason9f5fae22007-03-20 14:38:32 -040024#include "transaction.h"
Chris Mason1de037a2007-05-29 15:17:08 -040025#include "print-tree.h"
Chris Mason1e1d2702007-03-15 19:03:33 -040026
Chris Mason6567e832007-04-16 09:22:45 -040027#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
Chris Mason509659c2007-05-10 12:36:17 -040028 sizeof(struct btrfs_item) * 2) / \
29 BTRFS_CRC32_SIZE) - 1))
Chris Masonb18c6682007-04-17 13:26:50 -040030int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Chris Masondee26a92007-03-26 16:00:06 -040031 struct btrfs_root *root,
Chris Masonb18c6682007-04-17 13:26:50 -040032 u64 objectid, u64 pos,
Chris Masondb945352007-10-15 16:15:53 -040033 u64 offset, u64 disk_num_bytes,
34 u64 num_bytes)
Chris Mason9f5fae22007-03-20 14:38:32 -040035{
Chris Masondee26a92007-03-26 16:00:06 -040036 int ret = 0;
37 struct btrfs_file_extent_item *item;
38 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040039 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040040 struct extent_buffer *leaf;
Chris Masondee26a92007-03-26 16:00:06 -040041
Chris Mason5caf2a02007-04-02 11:20:42 -040042 path = btrfs_alloc_path();
43 BUG_ON(!path);
Chris Masondee26a92007-03-26 16:00:06 -040044 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040045 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040046 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
47
Chris Mason5caf2a02007-04-02 11:20:42 -040048 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040049 sizeof(*item));
Chris Mason54aa1f42007-06-22 14:16:25 -040050 if (ret < 0)
51 goto out;
Chris Mason9773a782007-03-27 11:26:26 -040052 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -040053 leaf = path->nodes[0];
54 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040055 struct btrfs_file_extent_item);
Chris Masondb945352007-10-15 16:15:53 -040056 btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
57 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -040058 btrfs_set_file_extent_offset(leaf, item, 0);
Chris Masondb945352007-10-15 16:15:53 -040059 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -040060 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 Mason54aa1f42007-06-22 14:16:25 -040063out:
Chris Mason5caf2a02007-04-02 11:20:42 -040064 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -040065 return ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040066}
Chris Masondee26a92007-03-26 16:00:06 -040067
Chris Masonb18c6682007-04-17 13:26:50 -040068struct 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 Mason6567e832007-04-16 09:22:45 -040073{
74 int ret;
75 struct btrfs_key file_key;
76 struct btrfs_key found_key;
77 struct btrfs_csum_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040078 struct extent_buffer *leaf;
Chris Mason6567e832007-04-16 09:22:45 -040079 u64 csum_offset = 0;
Chris Masona429e512007-04-18 16:15:28 -040080 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040081
82 file_key.objectid = objectid;
83 file_key.offset = offset;
Chris Mason6567e832007-04-16 09:22:45 -040084 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040085 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040086 if (ret < 0)
87 goto fail;
Chris Mason5f39d392007-10-15 16:14:19 -040088 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -040089 if (ret > 0) {
90 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040091 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040092 goto fail;
93 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -040094 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason6567e832007-04-16 09:22:45 -040095 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 Mason5f39d392007-10-15 16:14:19 -0400101 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400102 csums_in_item /= BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -0400103
104 if (csum_offset >= csums_in_item) {
105 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -0400106 goto fail;
107 }
108 }
109 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -0400110 item = (struct btrfs_csum_item *)((unsigned char *)item +
111 csum_offset * BTRFS_CRC32_SIZE);
Chris Mason6567e832007-04-16 09:22:45 -0400112 return item;
113fail:
114 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -0400115 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -0400116 return ERR_PTR(ret);
117}
118
119
Chris Masondee26a92007-03-26 16:00:06 -0400120int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
121 struct btrfs_root *root,
122 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400123 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400124{
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 Mason70b2bef2007-04-17 15:39:32 -0400131 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400132 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
Chris Mason065631f2008-02-20 12:07:25 -0500137int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
138 struct btrfs_root *root, struct inode *inode,
139 struct bio *bio)
Chris Masonf254e522007-03-29 15:15:27 -0400140{
Chris Mason065631f2008-02-20 12:07:25 -0500141 u64 objectid = inode->i_ino;
142 u64 offset;
Chris Masonf254e522007-03-29 15:15:27 -0400143 int ret;
144 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400145 struct btrfs_key found_key;
Chris Mason065631f2008-02-20 12:07:25 -0500146 u64 next_offset;
147 int found_next;
Chris Mason5caf2a02007-04-02 11:20:42 -0400148 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400149 struct btrfs_csum_item *item;
Chris Mason065631f2008-02-20 12:07:25 -0500150 struct btrfs_csum_item *item_end;
Chris Masonff79f812007-10-15 16:22:25 -0400151 struct extent_buffer *leaf = NULL;
Chris Mason6567e832007-04-16 09:22:45 -0400152 u64 csum_offset;
Chris Mason065631f2008-02-20 12:07:25 -0500153 u32 csum_result;
Chris Masonf578d4b2007-10-25 15:42:56 -0400154 u32 nritems;
155 u32 ins_size;
Chris Mason065631f2008-02-20 12:07:25 -0500156 int bio_index = 0;
157 struct bio_vec *bvec = bio->bi_io_vec;
158 char *data;
Chris Mason6e92f5e2008-02-20 12:07:25 -0500159 char *eb_map;
160 char *eb_token;
161 unsigned long map_len;
162 unsigned long map_start;
163
Chris Mason5caf2a02007-04-02 11:20:42 -0400164 path = btrfs_alloc_path();
165 BUG_ON(!path);
Chris Mason065631f2008-02-20 12:07:25 -0500166again:
167 next_offset = (u64)-1;
168 found_next = 0;
169 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
Chris Masonf254e522007-03-29 15:15:27 -0400170 file_key.objectid = objectid;
171 file_key.offset = offset;
Chris Masonf254e522007-03-29 15:15:27 -0400172 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400173
174 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
Chris Masonff79f812007-10-15 16:22:25 -0400175 if (!IS_ERR(item)) {
176 leaf = path->nodes[0];
Chris Masona429e512007-04-18 16:15:28 -0400177 goto found;
Chris Masonff79f812007-10-15 16:22:25 -0400178 }
Chris Masona429e512007-04-18 16:15:28 -0400179 ret = PTR_ERR(item);
180 if (ret == -EFBIG) {
181 u32 item_size;
182 /* we found one, but it isn't big enough yet */
Chris Mason5f39d392007-10-15 16:14:19 -0400183 leaf = path->nodes[0];
184 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400185 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
Chris Masona429e512007-04-18 16:15:28 -0400186 /* already at max size, make a new one */
187 goto insert;
188 }
189 } else {
Chris Masonf578d4b2007-10-25 15:42:56 -0400190 int slot = path->slots[0] + 1;
Chris Masona429e512007-04-18 16:15:28 -0400191 /* we didn't find a csum item, insert one */
Chris Masonf578d4b2007-10-25 15:42:56 -0400192 nritems = btrfs_header_nritems(path->nodes[0]);
193 if (path->slots[0] >= nritems - 1) {
194 ret = btrfs_next_leaf(root, path);
Yanb56baf52007-10-29 12:01:05 -0400195 if (ret == 1)
Chris Masonf578d4b2007-10-25 15:42:56 -0400196 found_next = 1;
Yanb56baf52007-10-29 12:01:05 -0400197 if (ret != 0)
Chris Masonf578d4b2007-10-25 15:42:56 -0400198 goto insert;
Yanb56baf52007-10-29 12:01:05 -0400199 slot = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400200 }
201 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
202 if (found_key.objectid != objectid ||
203 found_key.type != BTRFS_CSUM_ITEM_KEY) {
204 found_next = 1;
205 goto insert;
206 }
207 next_offset = found_key.offset;
208 found_next = 1;
Chris Masona429e512007-04-18 16:15:28 -0400209 goto insert;
210 }
211
212 /*
213 * at this point, we know the tree has an item, but it isn't big
214 * enough yet to put our csum in. Grow it
215 */
216 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400217 ret = btrfs_search_slot(trans, root, &file_key, path,
Chris Mason509659c2007-05-10 12:36:17 -0400218 BTRFS_CRC32_SIZE, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400219 if (ret < 0)
220 goto fail;
221 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400222 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400223 }
224 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400225 goto insert;
226 }
227 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400228 leaf = path->nodes[0];
229 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason6567e832007-04-16 09:22:45 -0400230 csum_offset = (offset - found_key.offset) >>
231 root->fs_info->sb->s_blocksize_bits;
232 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
233 found_key.objectid != objectid ||
234 csum_offset >= MAX_CSUM_ITEMS(root)) {
Chris Mason6567e832007-04-16 09:22:45 -0400235 goto insert;
236 }
Chris Mason5f39d392007-10-15 16:14:19 -0400237 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
Chris Mason509659c2007-05-10 12:36:17 -0400238 BTRFS_CRC32_SIZE) {
239 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
Chris Mason5f39d392007-10-15 16:14:19 -0400240 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason3a686372007-05-24 13:35:57 -0400241 if (diff != BTRFS_CRC32_SIZE)
242 goto insert;
Chris Masona429e512007-04-18 16:15:28 -0400243 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400244 BUG_ON(ret);
245 goto csum;
246 }
247
248insert:
Chris Masona429e512007-04-18 16:15:28 -0400249 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400250 csum_offset = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400251 if (found_next) {
252 u64 tmp = min((u64)i_size_read(inode), next_offset);
Yanb56baf52007-10-29 12:01:05 -0400253 tmp -= offset & ~((u64)root->sectorsize -1);
Chris Masonf578d4b2007-10-25 15:42:56 -0400254 tmp >>= root->fs_info->sb->s_blocksize_bits;
255 tmp = max((u64)1, tmp);
256 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
257 ins_size = BTRFS_CRC32_SIZE * tmp;
258 } else {
259 ins_size = BTRFS_CRC32_SIZE;
260 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400261 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masonf578d4b2007-10-25 15:42:56 -0400262 ins_size);
Chris Mason54aa1f42007-06-22 14:16:25 -0400263 if (ret < 0)
264 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400265 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400266 WARN_ON(1);
Chris Masonf254e522007-03-29 15:15:27 -0400267 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400268 }
Chris Mason6567e832007-04-16 09:22:45 -0400269csum:
Chris Mason5f39d392007-10-15 16:14:19 -0400270 leaf = path->nodes[0];
271 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Masonf254e522007-03-29 15:15:27 -0400272 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400273 item = (struct btrfs_csum_item *)((unsigned char *)item +
274 csum_offset * BTRFS_CRC32_SIZE);
Chris Masonb18c6682007-04-17 13:26:50 -0400275found:
Chris Mason065631f2008-02-20 12:07:25 -0500276 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
277 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
278 btrfs_item_size_nr(leaf, path->slots[0]));
Chris Mason6e92f5e2008-02-20 12:07:25 -0500279 eb_token = NULL;
Chris Mason065631f2008-02-20 12:07:25 -0500280next_bvec:
281 data = kmap_atomic(bvec->bv_page, KM_IRQ0);
282 csum_result = ~(u32)0;
283 csum_result = btrfs_csum_data(root, data + bvec->bv_offset,
284 csum_result, bvec->bv_len);
285 kunmap_atomic(data, KM_IRQ0);
Chris Masonff79f812007-10-15 16:22:25 -0400286 btrfs_csum_final(csum_result, (char *)&csum_result);
Chris Masonaadfeb62008-01-29 09:10:27 -0500287 if (csum_result == 0) {
288 printk("csum result is 0 for inode %lu offset %Lu\n", inode->i_ino, offset);
289 }
290
Chris Mason6e92f5e2008-02-20 12:07:25 -0500291 if (!eb_token ||
292 (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
293 int err;
294
295 if (eb_token)
296 unmap_extent_buffer(leaf, eb_token, KM_IRQ1);
297 eb_token = NULL;
298 err = map_private_extent_buffer(leaf, (unsigned long)item,
299 BTRFS_CRC32_SIZE,
300 &eb_token, &eb_map,
301 &map_start, &map_len, KM_IRQ1);
302 if (err)
303 eb_token = NULL;
304 }
305 if (eb_token) {
306 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
307 &csum_result, BTRFS_CRC32_SIZE);
308 } else {
309 write_extent_buffer(leaf, &csum_result, (unsigned long)item,
310 BTRFS_CRC32_SIZE);
311 }
Chris Mason065631f2008-02-20 12:07:25 -0500312 bio_index++;
313 bvec++;
314 if (bio_index < bio->bi_vcnt) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500315 item = (struct btrfs_csum_item *)((char *)item +
316 BTRFS_CRC32_SIZE);
Chris Mason2e1a9922008-02-20 15:44:32 -0500317 if (item < item_end && offset + PAGE_CACHE_SIZE ==
318 page_offset(bvec->bv_page)) {
319 offset = page_offset(bvec->bv_page);
Chris Mason065631f2008-02-20 12:07:25 -0500320 goto next_bvec;
Chris Mason2e1a9922008-02-20 15:44:32 -0500321 }
Chris Mason065631f2008-02-20 12:07:25 -0500322 }
Chris Mason6e92f5e2008-02-20 12:07:25 -0500323 if (eb_token) {
324 unmap_extent_buffer(leaf, eb_token, KM_IRQ1);
325 eb_token = NULL;
326 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400327 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason065631f2008-02-20 12:07:25 -0500328 if (bio_index < bio->bi_vcnt) {
329 btrfs_release_path(root, path);
330 goto again;
331 }
Chris Masonf254e522007-03-29 15:15:27 -0400332fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400333 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400334 return ret;
335}
336
Chris Mason1de037a2007-05-29 15:17:08 -0400337int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
338 struct btrfs_root *root, struct btrfs_path *path,
339 u64 isize)
340{
341 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400342 struct extent_buffer *leaf = path->nodes[0];
Chris Mason1de037a2007-05-29 15:17:08 -0400343 int slot = path->slots[0];
344 int ret;
345 u32 new_item_size;
346 u64 new_item_span;
347 u64 blocks;
348
Chris Mason5f39d392007-10-15 16:14:19 -0400349 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason1de037a2007-05-29 15:17:08 -0400350 if (isize <= key.offset)
351 return 0;
352 new_item_span = isize - key.offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400353 blocks = (new_item_span + root->sectorsize - 1) >>
Chris Mason84f54cf2007-06-12 07:43:08 -0400354 root->fs_info->sb->s_blocksize_bits;
Chris Mason1de037a2007-05-29 15:17:08 -0400355 new_item_size = blocks * BTRFS_CRC32_SIZE;
Chris Mason5f39d392007-10-15 16:14:19 -0400356 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
Chris Mason1de037a2007-05-29 15:17:08 -0400357 return 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400358 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
Chris Mason1de037a2007-05-29 15:17:08 -0400359 BUG_ON(ret);
360 return ret;
361}
362