blob: 964652435fd1a5f40e061cfe1c3a7dd1d1198248 [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 Masond3977122009-01-05 21:25:51 -050027#define MAX_CSUM_ITEMS(r, size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
Josef Bacik607d4322008-12-02 07:17:45 -050028 sizeof(struct btrfs_item) * 2) / \
29 size) - 1))
Yan Zheng07d400a2009-01-06 11:42:00 -050030
31#define MAX_ORDERED_SUM_BYTES(r) ((PAGE_SIZE - \
32 sizeof(struct btrfs_ordered_sum)) / \
33 sizeof(struct btrfs_sector_sum) * \
34 (r)->sectorsize - (r)->sectorsize)
35
Chris Masonb18c6682007-04-17 13:26:50 -040036int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Sage Weilf2eb0a22008-05-02 14:43:14 -040037 struct btrfs_root *root,
38 u64 objectid, u64 pos,
39 u64 disk_offset, u64 disk_num_bytes,
Chris Masonc8b97812008-10-29 14:49:59 -040040 u64 num_bytes, u64 offset, u64 ram_bytes,
41 u8 compression, u8 encryption, u16 other_encoding)
Chris Mason9f5fae22007-03-20 14:38:32 -040042{
Chris Masondee26a92007-03-26 16:00:06 -040043 int ret = 0;
44 struct btrfs_file_extent_item *item;
45 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040046 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040047 struct extent_buffer *leaf;
Chris Masondee26a92007-03-26 16:00:06 -040048
Chris Mason5caf2a02007-04-02 11:20:42 -040049 path = btrfs_alloc_path();
50 BUG_ON(!path);
Chris Masondee26a92007-03-26 16:00:06 -040051 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040052 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040053 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
54
Chris Mason5caf2a02007-04-02 11:20:42 -040055 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040056 sizeof(*item));
Chris Mason54aa1f42007-06-22 14:16:25 -040057 if (ret < 0)
58 goto out;
Chris Mason9773a782007-03-27 11:26:26 -040059 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -040060 leaf = path->nodes[0];
61 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040062 struct btrfs_file_extent_item);
Sage Weilf2eb0a22008-05-02 14:43:14 -040063 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
Chris Masondb945352007-10-15 16:15:53 -040064 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
Sage Weilf2eb0a22008-05-02 14:43:14 -040065 btrfs_set_file_extent_offset(leaf, item, offset);
Chris Masondb945352007-10-15 16:15:53 -040066 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -040067 btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -040068 btrfs_set_file_extent_generation(leaf, item, trans->transid);
69 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
Chris Masonc8b97812008-10-29 14:49:59 -040070 btrfs_set_file_extent_compression(leaf, item, compression);
71 btrfs_set_file_extent_encryption(leaf, item, encryption);
72 btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
73
Chris Mason5f39d392007-10-15 16:14:19 -040074 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -040075out:
Chris Mason5caf2a02007-04-02 11:20:42 -040076 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -040077 return ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040078}
Chris Masondee26a92007-03-26 16:00:06 -040079
Chris Masonb18c6682007-04-17 13:26:50 -040080struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
81 struct btrfs_root *root,
82 struct btrfs_path *path,
Chris Masond20f7042008-12-08 16:58:54 -050083 u64 bytenr, int cow)
Chris Mason6567e832007-04-16 09:22:45 -040084{
85 int ret;
86 struct btrfs_key file_key;
87 struct btrfs_key found_key;
88 struct btrfs_csum_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040089 struct extent_buffer *leaf;
Chris Mason6567e832007-04-16 09:22:45 -040090 u64 csum_offset = 0;
Josef Bacik607d4322008-12-02 07:17:45 -050091 u16 csum_size =
92 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Masona429e512007-04-18 16:15:28 -040093 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040094
Chris Masond20f7042008-12-08 16:58:54 -050095 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
96 file_key.offset = bytenr;
97 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040098 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040099 if (ret < 0)
100 goto fail;
Chris Mason5f39d392007-10-15 16:14:19 -0400101 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -0400102 if (ret > 0) {
103 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -0400104 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -0400105 goto fail;
106 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masond20f7042008-12-08 16:58:54 -0500108 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
Chris Mason6567e832007-04-16 09:22:45 -0400109 goto fail;
Chris Masond20f7042008-12-08 16:58:54 -0500110
111 csum_offset = (bytenr - found_key.offset) >>
Chris Mason6567e832007-04-16 09:22:45 -0400112 root->fs_info->sb->s_blocksize_bits;
Chris Mason5f39d392007-10-15 16:14:19 -0400113 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500114 csums_in_item /= csum_size;
Chris Masona429e512007-04-18 16:15:28 -0400115
116 if (csum_offset >= csums_in_item) {
117 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -0400118 goto fail;
119 }
120 }
121 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -0400122 item = (struct btrfs_csum_item *)((unsigned char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500123 csum_offset * csum_size);
Chris Mason6567e832007-04-16 09:22:45 -0400124 return item;
125fail:
126 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -0400127 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -0400128 return ERR_PTR(ret);
129}
130
131
Chris Masondee26a92007-03-26 16:00:06 -0400132int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
133 struct btrfs_root *root,
134 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400135 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400136{
137 int ret;
138 struct btrfs_key file_key;
139 int ins_len = mod < 0 ? -1 : 0;
140 int cow = mod != 0;
141
142 file_key.objectid = objectid;
Chris Mason70b2bef2007-04-17 15:39:32 -0400143 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400144 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
145 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
146 return ret;
147}
Chris Masonf254e522007-03-29 15:15:27 -0400148
Yan Zheng17d217f2008-12-12 10:03:38 -0500149
Chris Mason61b49442008-07-31 15:42:53 -0400150int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
Chris Masond20f7042008-12-08 16:58:54 -0500151 struct bio *bio, u32 *dst)
Chris Mason61b49442008-07-31 15:42:53 -0400152{
153 u32 sum;
154 struct bio_vec *bvec = bio->bi_io_vec;
155 int bio_index = 0;
156 u64 offset;
157 u64 item_start_offset = 0;
158 u64 item_last_offset = 0;
Chris Masond20f7042008-12-08 16:58:54 -0500159 u64 disk_bytenr;
Chris Mason61b49442008-07-31 15:42:53 -0400160 u32 diff;
Josef Bacik607d4322008-12-02 07:17:45 -0500161 u16 csum_size =
162 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Mason61b49442008-07-31 15:42:53 -0400163 int ret;
164 struct btrfs_path *path;
165 struct btrfs_csum_item *item = NULL;
166 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
167
168 path = btrfs_alloc_path();
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400169 if (bio->bi_size > PAGE_CACHE_SIZE * 8)
170 path->reada = 2;
Chris Mason61b49442008-07-31 15:42:53 -0400171
172 WARN_ON(bio->bi_vcnt <= 0);
173
Chris Masond20f7042008-12-08 16:58:54 -0500174 disk_bytenr = (u64)bio->bi_sector << 9;
Chris Masond3977122009-01-05 21:25:51 -0500175 while (bio_index < bio->bi_vcnt) {
Chris Mason61b49442008-07-31 15:42:53 -0400176 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
Chris Masond20f7042008-12-08 16:58:54 -0500177 ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
Chris Mason61b49442008-07-31 15:42:53 -0400178 if (ret == 0)
179 goto found;
180
Chris Masond20f7042008-12-08 16:58:54 -0500181 if (!item || disk_bytenr < item_start_offset ||
182 disk_bytenr >= item_last_offset) {
Chris Mason61b49442008-07-31 15:42:53 -0400183 struct btrfs_key found_key;
184 u32 item_size;
185
186 if (item)
187 btrfs_release_path(root, path);
Chris Masond20f7042008-12-08 16:58:54 -0500188 item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
189 path, disk_bytenr, 0);
Chris Mason61b49442008-07-31 15:42:53 -0400190 if (IS_ERR(item)) {
191 ret = PTR_ERR(item);
192 if (ret == -ENOENT || ret == -EFBIG)
193 ret = 0;
194 sum = 0;
Yan Zheng17d217f2008-12-12 10:03:38 -0500195 if (BTRFS_I(inode)->root->root_key.objectid ==
196 BTRFS_DATA_RELOC_TREE_OBJECTID) {
197 set_extent_bits(io_tree, offset,
198 offset + bvec->bv_len - 1,
199 EXTENT_NODATASUM, GFP_NOFS);
200 } else {
Chris Masond3977122009-01-05 21:25:51 -0500201 printk(KERN_INFO "btrfs no csum found "
202 "for inode %lu start %llu\n",
203 inode->i_ino,
Yan Zheng17d217f2008-12-12 10:03:38 -0500204 (unsigned long long)offset);
205 }
Chris Mason6dab8152008-08-04 08:35:53 -0400206 item = NULL;
Chris Mason39be25c2008-11-10 11:50:50 -0500207 btrfs_release_path(root, path);
Chris Mason61b49442008-07-31 15:42:53 -0400208 goto found;
209 }
210 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
211 path->slots[0]);
212
213 item_start_offset = found_key.offset;
214 item_size = btrfs_item_size_nr(path->nodes[0],
215 path->slots[0]);
216 item_last_offset = item_start_offset +
Josef Bacik607d4322008-12-02 07:17:45 -0500217 (item_size / csum_size) *
Chris Mason61b49442008-07-31 15:42:53 -0400218 root->sectorsize;
219 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
220 struct btrfs_csum_item);
221 }
222 /*
223 * this byte range must be able to fit inside
224 * a single leaf so it will also fit inside a u32
225 */
Chris Masond20f7042008-12-08 16:58:54 -0500226 diff = disk_bytenr - item_start_offset;
Chris Mason61b49442008-07-31 15:42:53 -0400227 diff = diff / root->sectorsize;
Josef Bacik607d4322008-12-02 07:17:45 -0500228 diff = diff * csum_size;
Chris Mason61b49442008-07-31 15:42:53 -0400229
230 read_extent_buffer(path->nodes[0], &sum,
Chris Mason3de9d6b2008-08-04 23:17:27 -0400231 ((unsigned long)item) + diff,
Josef Bacik607d4322008-12-02 07:17:45 -0500232 csum_size);
Chris Mason61b49442008-07-31 15:42:53 -0400233found:
Chris Masond20f7042008-12-08 16:58:54 -0500234 if (dst)
235 *dst++ = sum;
236 else
237 set_state_private(io_tree, offset, sum);
238 disk_bytenr += bvec->bv_len;
Chris Mason61b49442008-07-31 15:42:53 -0400239 bio_index++;
240 bvec++;
241 }
242 btrfs_free_path(path);
243 return 0;
244}
245
Yan Zheng17d217f2008-12-12 10:03:38 -0500246int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
247 struct list_head *list)
248{
249 struct btrfs_key key;
250 struct btrfs_path *path;
251 struct extent_buffer *leaf;
252 struct btrfs_ordered_sum *sums;
253 struct btrfs_sector_sum *sector_sum;
254 struct btrfs_csum_item *item;
255 unsigned long offset;
256 int ret;
257 size_t size;
258 u64 csum_end;
259 u16 csum_size = btrfs_super_csum_size(&root->fs_info->super_copy);
260
261 path = btrfs_alloc_path();
262 BUG_ON(!path);
263
264 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
265 key.offset = start;
266 key.type = BTRFS_EXTENT_CSUM_KEY;
267
Yan Zheng07d400a2009-01-06 11:42:00 -0500268 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -0500269 if (ret < 0)
270 goto fail;
271 if (ret > 0 && path->slots[0] > 0) {
272 leaf = path->nodes[0];
273 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
274 if (key.objectid == BTRFS_EXTENT_CSUM_OBJECTID &&
275 key.type == BTRFS_EXTENT_CSUM_KEY) {
276 offset = (start - key.offset) >>
277 root->fs_info->sb->s_blocksize_bits;
278 if (offset * csum_size <
279 btrfs_item_size_nr(leaf, path->slots[0] - 1))
280 path->slots[0]--;
281 }
282 }
283
284 while (start <= end) {
285 leaf = path->nodes[0];
286 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
Yan Zheng07d400a2009-01-06 11:42:00 -0500287 ret = btrfs_next_leaf(root, path);
Yan Zheng17d217f2008-12-12 10:03:38 -0500288 if (ret < 0)
289 goto fail;
290 if (ret > 0)
291 break;
292 leaf = path->nodes[0];
293 }
294
295 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
296 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
297 key.type != BTRFS_EXTENT_CSUM_KEY)
298 break;
299
300 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
301 if (key.offset > end)
302 break;
303
304 if (key.offset > start)
305 start = key.offset;
306
307 size = btrfs_item_size_nr(leaf, path->slots[0]);
308 csum_end = key.offset + (size / csum_size) * root->sectorsize;
Yan Zheng87b29b22008-12-17 10:21:48 -0500309 if (csum_end <= start) {
310 path->slots[0]++;
311 continue;
312 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500313
Yan Zheng07d400a2009-01-06 11:42:00 -0500314 csum_end = min(csum_end, end + 1);
Yan Zheng17d217f2008-12-12 10:03:38 -0500315 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
316 struct btrfs_csum_item);
Yan Zheng07d400a2009-01-06 11:42:00 -0500317 while (start < csum_end) {
318 size = min_t(size_t, csum_end - start,
319 MAX_ORDERED_SUM_BYTES(root));
320 sums = kzalloc(btrfs_ordered_sum_size(root, size),
321 GFP_NOFS);
322 BUG_ON(!sums);
Yan Zheng17d217f2008-12-12 10:03:38 -0500323
Yan Zheng07d400a2009-01-06 11:42:00 -0500324 sector_sum = sums->sums;
325 sums->bytenr = start;
326 sums->len = size;
327
328 offset = (start - key.offset) >>
329 root->fs_info->sb->s_blocksize_bits;
330 offset *= csum_size;
331
332 while (size > 0) {
333 read_extent_buffer(path->nodes[0],
334 &sector_sum->sum,
335 ((unsigned long)item) +
336 offset, csum_size);
337 sector_sum->bytenr = start;
338
339 size -= root->sectorsize;
340 start += root->sectorsize;
341 offset += csum_size;
342 sector_sum++;
343 }
344 list_add_tail(&sums->list, list);
Yan Zheng17d217f2008-12-12 10:03:38 -0500345 }
Yan Zheng17d217f2008-12-12 10:03:38 -0500346 path->slots[0]++;
347 }
348 ret = 0;
349fail:
350 btrfs_free_path(path);
351 return ret;
352}
353
Chris Mason3edf7d32008-07-18 06:17:13 -0400354int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
Chris Masond20f7042008-12-08 16:58:54 -0500355 struct bio *bio, u64 file_start, int contig)
Chris Masone0156402008-04-16 11:15:20 -0400356{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400357 struct btrfs_ordered_sum *sums;
358 struct btrfs_sector_sum *sector_sum;
Chris Mason3edf7d32008-07-18 06:17:13 -0400359 struct btrfs_ordered_extent *ordered;
Chris Masone0156402008-04-16 11:15:20 -0400360 char *data;
361 struct bio_vec *bvec = bio->bi_io_vec;
362 int bio_index = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400363 unsigned long total_bytes = 0;
364 unsigned long this_sum_bytes = 0;
365 u64 offset;
Chris Masond20f7042008-12-08 16:58:54 -0500366 u64 disk_bytenr;
Chris Masone0156402008-04-16 11:15:20 -0400367
Chris Masone6dcd2d2008-07-17 12:53:50 -0400368 WARN_ON(bio->bi_vcnt <= 0);
369 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
Chris Masone0156402008-04-16 11:15:20 -0400370 if (!sums)
371 return -ENOMEM;
Chris Mason3edf7d32008-07-18 06:17:13 -0400372
Chris Masoned98b562008-07-22 23:06:42 -0400373 sector_sum = sums->sums;
Chris Masond20f7042008-12-08 16:58:54 -0500374 disk_bytenr = (u64)bio->bi_sector << 9;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400375 sums->len = bio->bi_size;
376 INIT_LIST_HEAD(&sums->list);
Chris Masond20f7042008-12-08 16:58:54 -0500377
378 if (contig)
379 offset = file_start;
380 else
381 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
382
383 ordered = btrfs_lookup_ordered_extent(inode, offset);
Chris Mason3edf7d32008-07-18 06:17:13 -0400384 BUG_ON(!ordered);
Chris Masond20f7042008-12-08 16:58:54 -0500385 sums->bytenr = ordered->start;
Chris Masone0156402008-04-16 11:15:20 -0400386
Chris Masond3977122009-01-05 21:25:51 -0500387 while (bio_index < bio->bi_vcnt) {
Chris Masond20f7042008-12-08 16:58:54 -0500388 if (!contig)
389 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
390
391 if (!contig && (offset >= ordered->file_offset + ordered->len ||
392 offset < ordered->file_offset)) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400393 unsigned long bytes_left;
394 sums->len = this_sum_bytes;
395 this_sum_bytes = 0;
396 btrfs_add_ordered_sum(inode, ordered, sums);
397 btrfs_put_ordered_extent(ordered);
398
399 bytes_left = bio->bi_size - total_bytes;
400
401 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
402 GFP_NOFS);
403 BUG_ON(!sums);
Chris Masoned98b562008-07-22 23:06:42 -0400404 sector_sum = sums->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400405 sums->len = bytes_left;
Chris Masond20f7042008-12-08 16:58:54 -0500406 ordered = btrfs_lookup_ordered_extent(inode, offset);
Chris Mason3edf7d32008-07-18 06:17:13 -0400407 BUG_ON(!ordered);
Chris Masond20f7042008-12-08 16:58:54 -0500408 sums->bytenr = ordered->start;
Chris Mason3edf7d32008-07-18 06:17:13 -0400409 }
410
Chris Masone0156402008-04-16 11:15:20 -0400411 data = kmap_atomic(bvec->bv_page, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400412 sector_sum->sum = ~(u32)0;
413 sector_sum->sum = btrfs_csum_data(root,
414 data + bvec->bv_offset,
415 sector_sum->sum,
416 bvec->bv_len);
Chris Masone0156402008-04-16 11:15:20 -0400417 kunmap_atomic(data, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400418 btrfs_csum_final(sector_sum->sum,
419 (char *)&sector_sum->sum);
Chris Masond20f7042008-12-08 16:58:54 -0500420 sector_sum->bytenr = disk_bytenr;
Chris Masoned98b562008-07-22 23:06:42 -0400421
Chris Masone6dcd2d2008-07-17 12:53:50 -0400422 sector_sum++;
Chris Masone0156402008-04-16 11:15:20 -0400423 bio_index++;
Chris Mason3edf7d32008-07-18 06:17:13 -0400424 total_bytes += bvec->bv_len;
425 this_sum_bytes += bvec->bv_len;
Chris Masond20f7042008-12-08 16:58:54 -0500426 disk_bytenr += bvec->bv_len;
427 offset += bvec->bv_len;
Chris Masone0156402008-04-16 11:15:20 -0400428 bvec++;
429 }
Chris Masoned98b562008-07-22 23:06:42 -0400430 this_sum_bytes = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400431 btrfs_add_ordered_sum(inode, ordered, sums);
432 btrfs_put_ordered_extent(ordered);
Chris Masone0156402008-04-16 11:15:20 -0400433 return 0;
434}
435
Chris Mason459931e2008-12-10 09:10:46 -0500436/*
437 * helper function for csum removal, this expects the
438 * key to describe the csum pointed to by the path, and it expects
439 * the csum to overlap the range [bytenr, len]
440 *
441 * The csum should not be entirely contained in the range and the
442 * range should not be entirely contained in the csum.
443 *
444 * This calls btrfs_truncate_item with the correct args based on the
445 * overlap, and fixes up the key as required.
446 */
447static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
448 struct btrfs_root *root,
449 struct btrfs_path *path,
450 struct btrfs_key *key,
451 u64 bytenr, u64 len)
452{
453 struct extent_buffer *leaf;
454 u16 csum_size =
455 btrfs_super_csum_size(&root->fs_info->super_copy);
456 u64 csum_end;
457 u64 end_byte = bytenr + len;
458 u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
459 int ret;
460
461 leaf = path->nodes[0];
462 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
463 csum_end <<= root->fs_info->sb->s_blocksize_bits;
464 csum_end += key->offset;
465
466 if (key->offset < bytenr && csum_end <= end_byte) {
467 /*
468 * [ bytenr - len ]
469 * [ ]
470 * [csum ]
471 * A simple truncate off the end of the item
472 */
473 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
474 new_size *= csum_size;
475 ret = btrfs_truncate_item(trans, root, path, new_size, 1);
476 BUG_ON(ret);
477 } else if (key->offset >= bytenr && csum_end > end_byte &&
478 end_byte > key->offset) {
479 /*
480 * [ bytenr - len ]
481 * [ ]
482 * [csum ]
483 * we need to truncate from the beginning of the csum
484 */
485 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
486 new_size *= csum_size;
487
488 ret = btrfs_truncate_item(trans, root, path, new_size, 0);
489 BUG_ON(ret);
490
491 key->offset = end_byte;
492 ret = btrfs_set_item_key_safe(trans, root, path, key);
493 BUG_ON(ret);
494 } else {
495 BUG();
496 }
497 return 0;
498}
499
500/*
501 * deletes the csum items from the csum tree for a given
502 * range of bytes.
503 */
504int btrfs_del_csums(struct btrfs_trans_handle *trans,
505 struct btrfs_root *root, u64 bytenr, u64 len)
506{
507 struct btrfs_path *path;
508 struct btrfs_key key;
509 u64 end_byte = bytenr + len;
510 u64 csum_end;
511 struct extent_buffer *leaf;
512 int ret;
513 u16 csum_size =
514 btrfs_super_csum_size(&root->fs_info->super_copy);
515 int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
516
517 root = root->fs_info->csum_root;
518
519 path = btrfs_alloc_path();
520
Chris Masond3977122009-01-05 21:25:51 -0500521 while (1) {
Chris Mason459931e2008-12-10 09:10:46 -0500522 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
523 key.offset = end_byte - 1;
524 key.type = BTRFS_EXTENT_CSUM_KEY;
525
526 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
527 if (ret > 0) {
528 if (path->slots[0] == 0)
529 goto out;
530 path->slots[0]--;
531 }
532 leaf = path->nodes[0];
533 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
534
535 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
536 key.type != BTRFS_EXTENT_CSUM_KEY) {
537 break;
538 }
539
540 if (key.offset >= end_byte)
541 break;
542
543 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
544 csum_end <<= blocksize_bits;
545 csum_end += key.offset;
546
547 /* this csum ends before we start, we're done */
548 if (csum_end <= bytenr)
549 break;
550
551 /* delete the entire item, it is inside our range */
552 if (key.offset >= bytenr && csum_end <= end_byte) {
553 ret = btrfs_del_item(trans, root, path);
554 BUG_ON(ret);
Chris Masondcbdd4d2008-12-16 13:51:01 -0500555 if (key.offset == bytenr)
556 break;
Chris Mason459931e2008-12-10 09:10:46 -0500557 } else if (key.offset < bytenr && csum_end > end_byte) {
558 unsigned long offset;
559 unsigned long shift_len;
560 unsigned long item_offset;
561 /*
562 * [ bytenr - len ]
563 * [csum ]
564 *
565 * Our bytes are in the middle of the csum,
566 * we need to split this item and insert a new one.
567 *
568 * But we can't drop the path because the
569 * csum could change, get removed, extended etc.
570 *
571 * The trick here is the max size of a csum item leaves
572 * enough room in the tree block for a single
573 * item header. So, we split the item in place,
574 * adding a new header pointing to the existing
575 * bytes. Then we loop around again and we have
576 * a nicely formed csum item that we can neatly
577 * truncate.
578 */
579 offset = (bytenr - key.offset) >> blocksize_bits;
580 offset *= csum_size;
581
582 shift_len = (len >> blocksize_bits) * csum_size;
583
584 item_offset = btrfs_item_ptr_offset(leaf,
585 path->slots[0]);
586
587 memset_extent_buffer(leaf, 0, item_offset + offset,
588 shift_len);
589 key.offset = bytenr;
590
591 /*
592 * btrfs_split_item returns -EAGAIN when the
593 * item changed size or key
594 */
595 ret = btrfs_split_item(trans, root, path, &key, offset);
596 BUG_ON(ret && ret != -EAGAIN);
597
598 key.offset = end_byte - 1;
599 } else {
600 ret = truncate_one_csum(trans, root, path,
601 &key, bytenr, len);
602 BUG_ON(ret);
Chris Masondcbdd4d2008-12-16 13:51:01 -0500603 if (key.offset < bytenr)
604 break;
Chris Mason459931e2008-12-10 09:10:46 -0500605 }
606 btrfs_release_path(root, path);
607 }
608out:
609 btrfs_free_path(path);
610 return 0;
611}
612
Chris Mason065631f2008-02-20 12:07:25 -0500613int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
Chris Masond20f7042008-12-08 16:58:54 -0500614 struct btrfs_root *root,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400615 struct btrfs_ordered_sum *sums)
Chris Masonf254e522007-03-29 15:15:27 -0400616{
Chris Masond20f7042008-12-08 16:58:54 -0500617 u64 bytenr;
Chris Masonf254e522007-03-29 15:15:27 -0400618 int ret;
619 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400620 struct btrfs_key found_key;
Chris Mason065631f2008-02-20 12:07:25 -0500621 u64 next_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400622 u64 total_bytes = 0;
Chris Mason065631f2008-02-20 12:07:25 -0500623 int found_next;
Chris Mason5caf2a02007-04-02 11:20:42 -0400624 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400625 struct btrfs_csum_item *item;
Chris Mason065631f2008-02-20 12:07:25 -0500626 struct btrfs_csum_item *item_end;
Chris Masonff79f812007-10-15 16:22:25 -0400627 struct extent_buffer *leaf = NULL;
Chris Mason6567e832007-04-16 09:22:45 -0400628 u64 csum_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400629 struct btrfs_sector_sum *sector_sum;
Chris Masonf578d4b2007-10-25 15:42:56 -0400630 u32 nritems;
631 u32 ins_size;
Chris Mason6e92f5e2008-02-20 12:07:25 -0500632 char *eb_map;
633 char *eb_token;
634 unsigned long map_len;
635 unsigned long map_start;
Josef Bacik607d4322008-12-02 07:17:45 -0500636 u16 csum_size =
637 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500638
Chris Mason5caf2a02007-04-02 11:20:42 -0400639 path = btrfs_alloc_path();
640 BUG_ON(!path);
Chris Masoned98b562008-07-22 23:06:42 -0400641 sector_sum = sums->sums;
Chris Mason065631f2008-02-20 12:07:25 -0500642again:
643 next_offset = (u64)-1;
644 found_next = 0;
Chris Masond20f7042008-12-08 16:58:54 -0500645 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
646 file_key.offset = sector_sum->bytenr;
647 bytenr = sector_sum->bytenr;
648 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400649
Chris Masond20f7042008-12-08 16:58:54 -0500650 item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
Chris Masonff79f812007-10-15 16:22:25 -0400651 if (!IS_ERR(item)) {
652 leaf = path->nodes[0];
Chris Mason639cb582008-08-28 06:15:25 -0400653 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -0400654 goto found;
Chris Masonff79f812007-10-15 16:22:25 -0400655 }
Chris Masona429e512007-04-18 16:15:28 -0400656 ret = PTR_ERR(item);
657 if (ret == -EFBIG) {
658 u32 item_size;
659 /* we found one, but it isn't big enough yet */
Chris Mason5f39d392007-10-15 16:14:19 -0400660 leaf = path->nodes[0];
661 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500662 if ((item_size / csum_size) >=
663 MAX_CSUM_ITEMS(root, csum_size)) {
Chris Masona429e512007-04-18 16:15:28 -0400664 /* already at max size, make a new one */
665 goto insert;
666 }
667 } else {
Chris Masonf578d4b2007-10-25 15:42:56 -0400668 int slot = path->slots[0] + 1;
Chris Masona429e512007-04-18 16:15:28 -0400669 /* we didn't find a csum item, insert one */
Chris Masonf578d4b2007-10-25 15:42:56 -0400670 nritems = btrfs_header_nritems(path->nodes[0]);
671 if (path->slots[0] >= nritems - 1) {
672 ret = btrfs_next_leaf(root, path);
Yanb56baf52007-10-29 12:01:05 -0400673 if (ret == 1)
Chris Masonf578d4b2007-10-25 15:42:56 -0400674 found_next = 1;
Yanb56baf52007-10-29 12:01:05 -0400675 if (ret != 0)
Chris Masonf578d4b2007-10-25 15:42:56 -0400676 goto insert;
Yanb56baf52007-10-29 12:01:05 -0400677 slot = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400678 }
679 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
Chris Masond20f7042008-12-08 16:58:54 -0500680 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
681 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
Chris Masonf578d4b2007-10-25 15:42:56 -0400682 found_next = 1;
683 goto insert;
684 }
685 next_offset = found_key.offset;
686 found_next = 1;
Chris Masona429e512007-04-18 16:15:28 -0400687 goto insert;
688 }
689
690 /*
691 * at this point, we know the tree has an item, but it isn't big
692 * enough yet to put our csum in. Grow it
693 */
694 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400695 ret = btrfs_search_slot(trans, root, &file_key, path,
Josef Bacik607d4322008-12-02 07:17:45 -0500696 csum_size, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400697 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400698 goto fail_unlock;
Chris Mason459931e2008-12-10 09:10:46 -0500699
700 if (ret > 0) {
701 if (path->slots[0] == 0)
702 goto insert;
703 path->slots[0]--;
Chris Mason6567e832007-04-16 09:22:45 -0400704 }
Chris Mason459931e2008-12-10 09:10:46 -0500705
Chris Mason5f39d392007-10-15 16:14:19 -0400706 leaf = path->nodes[0];
707 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masond20f7042008-12-08 16:58:54 -0500708 csum_offset = (bytenr - found_key.offset) >>
Chris Mason6567e832007-04-16 09:22:45 -0400709 root->fs_info->sb->s_blocksize_bits;
Chris Mason459931e2008-12-10 09:10:46 -0500710
Chris Masond20f7042008-12-08 16:58:54 -0500711 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
712 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
Josef Bacik607d4322008-12-02 07:17:45 -0500713 csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
Chris Mason6567e832007-04-16 09:22:45 -0400714 goto insert;
715 }
Chris Mason459931e2008-12-10 09:10:46 -0500716
Chris Mason5f39d392007-10-15 16:14:19 -0400717 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
Josef Bacik607d4322008-12-02 07:17:45 -0500718 csum_size) {
719 u32 diff = (csum_offset + 1) * csum_size;
Chris Mason459931e2008-12-10 09:10:46 -0500720
721 /*
722 * is the item big enough already? we dropped our lock
723 * before and need to recheck
724 */
725 if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
726 goto csum;
727
Chris Mason5f39d392007-10-15 16:14:19 -0400728 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
Chris Masond3977122009-01-05 21:25:51 -0500729 if (diff != csum_size)
Chris Mason3a686372007-05-24 13:35:57 -0400730 goto insert;
Chris Mason459931e2008-12-10 09:10:46 -0500731
Chris Masona429e512007-04-18 16:15:28 -0400732 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400733 BUG_ON(ret);
734 goto csum;
735 }
736
737insert:
Chris Masona429e512007-04-18 16:15:28 -0400738 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400739 csum_offset = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400740 if (found_next) {
Chris Masond20f7042008-12-08 16:58:54 -0500741 u64 tmp = total_bytes + root->sectorsize;
742 u64 next_sector = sector_sum->bytenr;
743 struct btrfs_sector_sum *next = sector_sum + 1;
744
Chris Masond3977122009-01-05 21:25:51 -0500745 while (tmp < sums->len) {
Chris Masond20f7042008-12-08 16:58:54 -0500746 if (next_sector + root->sectorsize != next->bytenr)
747 break;
748 tmp += root->sectorsize;
749 next_sector = next->bytenr;
750 next++;
751 }
752 tmp = min(tmp, next_offset - file_key.offset);
Chris Masonf578d4b2007-10-25 15:42:56 -0400753 tmp >>= root->fs_info->sb->s_blocksize_bits;
754 tmp = max((u64)1, tmp);
Josef Bacik607d4322008-12-02 07:17:45 -0500755 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
756 ins_size = csum_size * tmp;
Chris Masonf578d4b2007-10-25 15:42:56 -0400757 } else {
Josef Bacik607d4322008-12-02 07:17:45 -0500758 ins_size = csum_size;
Chris Masonf578d4b2007-10-25 15:42:56 -0400759 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400760 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masonf578d4b2007-10-25 15:42:56 -0400761 ins_size);
Chris Mason54aa1f42007-06-22 14:16:25 -0400762 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400763 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400764 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400765 WARN_ON(1);
Chris Mason53863232008-08-15 15:34:18 -0400766 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400767 }
Chris Mason6567e832007-04-16 09:22:45 -0400768csum:
Chris Mason5f39d392007-10-15 16:14:19 -0400769 leaf = path->nodes[0];
770 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Masonf254e522007-03-29 15:15:27 -0400771 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400772 item = (struct btrfs_csum_item *)((unsigned char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500773 csum_offset * csum_size);
Chris Masonb18c6682007-04-17 13:26:50 -0400774found:
Chris Mason065631f2008-02-20 12:07:25 -0500775 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
776 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
777 btrfs_item_size_nr(leaf, path->slots[0]));
Chris Mason6e92f5e2008-02-20 12:07:25 -0500778 eb_token = NULL;
Chris Mason53863232008-08-15 15:34:18 -0400779 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400780next_sector:
Chris Masonaadfeb62008-01-29 09:10:27 -0500781
Chris Mason6e92f5e2008-02-20 12:07:25 -0500782 if (!eb_token ||
Josef Bacik607d4322008-12-02 07:17:45 -0500783 (unsigned long)item + csum_size >= map_start + map_len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500784 int err;
785
786 if (eb_token)
Chris Masoneb209782008-02-21 09:30:08 -0500787 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500788 eb_token = NULL;
789 err = map_private_extent_buffer(leaf, (unsigned long)item,
Josef Bacik607d4322008-12-02 07:17:45 -0500790 csum_size,
Chris Mason6e92f5e2008-02-20 12:07:25 -0500791 &eb_token, &eb_map,
Chris Masoneb209782008-02-21 09:30:08 -0500792 &map_start, &map_len, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500793 if (err)
794 eb_token = NULL;
795 }
796 if (eb_token) {
797 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
Josef Bacik607d4322008-12-02 07:17:45 -0500798 &sector_sum->sum, csum_size);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500799 } else {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400800 write_extent_buffer(leaf, &sector_sum->sum,
Josef Bacik607d4322008-12-02 07:17:45 -0500801 (unsigned long)item, csum_size);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500802 }
Chris Mason7f3c74f2008-07-18 12:01:11 -0400803
Chris Masone6dcd2d2008-07-17 12:53:50 -0400804 total_bytes += root->sectorsize;
805 sector_sum++;
806 if (total_bytes < sums->len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500807 item = (struct btrfs_csum_item *)((char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500808 csum_size);
Chris Masond20f7042008-12-08 16:58:54 -0500809 if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
810 sector_sum->bytenr) {
811 bytenr = sector_sum->bytenr;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400812 goto next_sector;
Chris Mason2e1a9922008-02-20 15:44:32 -0500813 }
Chris Mason065631f2008-02-20 12:07:25 -0500814 }
Chris Mason6e92f5e2008-02-20 12:07:25 -0500815 if (eb_token) {
Chris Masoneb209782008-02-21 09:30:08 -0500816 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500817 eb_token = NULL;
818 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400819 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason53863232008-08-15 15:34:18 -0400820 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400821 if (total_bytes < sums->len) {
Chris Mason065631f2008-02-20 12:07:25 -0500822 btrfs_release_path(root, path);
823 goto again;
824 }
Chris Mason53863232008-08-15 15:34:18 -0400825out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400826 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400827 return ret;
Chris Mason53863232008-08-15 15:34:18 -0400828
829fail_unlock:
Chris Mason53863232008-08-15 15:34:18 -0400830 goto out;
Chris Masonf254e522007-03-29 15:15:27 -0400831}