blob: 3ebef871ee6cf11e9be16d02504ae6ebd4c519da [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
Josef Bacik607d4322008-12-02 07:17:45 -050027#define MAX_CSUM_ITEMS(r,size) ((((BTRFS_LEAF_DATA_SIZE(r) - \
28 sizeof(struct btrfs_item) * 2) / \
29 size) - 1))
Chris Masonb18c6682007-04-17 13:26:50 -040030int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
Sage Weilf2eb0a22008-05-02 14:43:14 -040031 struct btrfs_root *root,
32 u64 objectid, u64 pos,
33 u64 disk_offset, u64 disk_num_bytes,
Chris Masonc8b97812008-10-29 14:49:59 -040034 u64 num_bytes, u64 offset, u64 ram_bytes,
35 u8 compression, u8 encryption, u16 other_encoding)
Chris Mason9f5fae22007-03-20 14:38:32 -040036{
Chris Masondee26a92007-03-26 16:00:06 -040037 int ret = 0;
38 struct btrfs_file_extent_item *item;
39 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040040 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040041 struct extent_buffer *leaf;
Chris Masondee26a92007-03-26 16:00:06 -040042
Chris Mason5caf2a02007-04-02 11:20:42 -040043 path = btrfs_alloc_path();
44 BUG_ON(!path);
Chris Masondee26a92007-03-26 16:00:06 -040045 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040046 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040047 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
48
Chris Mason5caf2a02007-04-02 11:20:42 -040049 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040050 sizeof(*item));
Chris Mason54aa1f42007-06-22 14:16:25 -040051 if (ret < 0)
52 goto out;
Chris Mason9773a782007-03-27 11:26:26 -040053 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -040054 leaf = path->nodes[0];
55 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040056 struct btrfs_file_extent_item);
Sage Weilf2eb0a22008-05-02 14:43:14 -040057 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
Chris Masondb945352007-10-15 16:15:53 -040058 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
Sage Weilf2eb0a22008-05-02 14:43:14 -040059 btrfs_set_file_extent_offset(leaf, item, offset);
Chris Masondb945352007-10-15 16:15:53 -040060 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -040061 btrfs_set_file_extent_ram_bytes(leaf, item, ram_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -040062 btrfs_set_file_extent_generation(leaf, item, trans->transid);
63 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
Chris Masonc8b97812008-10-29 14:49:59 -040064 btrfs_set_file_extent_compression(leaf, item, compression);
65 btrfs_set_file_extent_encryption(leaf, item, encryption);
66 btrfs_set_file_extent_other_encoding(leaf, item, other_encoding);
67
Chris Mason5f39d392007-10-15 16:14:19 -040068 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -040069out:
Chris Mason5caf2a02007-04-02 11:20:42 -040070 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -040071 return ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040072}
Chris Masondee26a92007-03-26 16:00:06 -040073
Chris Masonb18c6682007-04-17 13:26:50 -040074struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
75 struct btrfs_root *root,
76 struct btrfs_path *path,
Chris Masond20f7042008-12-08 16:58:54 -050077 u64 bytenr, int cow)
Chris Mason6567e832007-04-16 09:22:45 -040078{
79 int ret;
80 struct btrfs_key file_key;
81 struct btrfs_key found_key;
82 struct btrfs_csum_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040083 struct extent_buffer *leaf;
Chris Mason6567e832007-04-16 09:22:45 -040084 u64 csum_offset = 0;
Josef Bacik607d4322008-12-02 07:17:45 -050085 u16 csum_size =
86 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Masona429e512007-04-18 16:15:28 -040087 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040088
Chris Masond20f7042008-12-08 16:58:54 -050089 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
90 file_key.offset = bytenr;
91 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040092 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040093 if (ret < 0)
94 goto fail;
Chris Mason5f39d392007-10-15 16:14:19 -040095 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -040096 if (ret > 0) {
97 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040098 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040099 goto fail;
100 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400101 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masond20f7042008-12-08 16:58:54 -0500102 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY)
Chris Mason6567e832007-04-16 09:22:45 -0400103 goto fail;
Chris Masond20f7042008-12-08 16:58:54 -0500104
105 csum_offset = (bytenr - found_key.offset) >>
Chris Mason6567e832007-04-16 09:22:45 -0400106 root->fs_info->sb->s_blocksize_bits;
Chris Mason5f39d392007-10-15 16:14:19 -0400107 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500108 csums_in_item /= csum_size;
Chris Masona429e512007-04-18 16:15:28 -0400109
110 if (csum_offset >= csums_in_item) {
111 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -0400112 goto fail;
113 }
114 }
115 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -0400116 item = (struct btrfs_csum_item *)((unsigned char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500117 csum_offset * csum_size);
Chris Mason6567e832007-04-16 09:22:45 -0400118 return item;
119fail:
120 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -0400121 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -0400122 return ERR_PTR(ret);
123}
124
125
Chris Masondee26a92007-03-26 16:00:06 -0400126int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
127 struct btrfs_root *root,
128 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400129 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400130{
131 int ret;
132 struct btrfs_key file_key;
133 int ins_len = mod < 0 ? -1 : 0;
134 int cow = mod != 0;
135
136 file_key.objectid = objectid;
Chris Mason70b2bef2007-04-17 15:39:32 -0400137 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400138 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
139 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
140 return ret;
141}
Chris Masonf254e522007-03-29 15:15:27 -0400142
Chris Mason61b49442008-07-31 15:42:53 -0400143int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
Chris Masond20f7042008-12-08 16:58:54 -0500144 struct bio *bio, u32 *dst)
Chris Mason61b49442008-07-31 15:42:53 -0400145{
146 u32 sum;
147 struct bio_vec *bvec = bio->bi_io_vec;
148 int bio_index = 0;
149 u64 offset;
150 u64 item_start_offset = 0;
151 u64 item_last_offset = 0;
Chris Masond20f7042008-12-08 16:58:54 -0500152 u64 disk_bytenr;
Chris Mason61b49442008-07-31 15:42:53 -0400153 u32 diff;
Josef Bacik607d4322008-12-02 07:17:45 -0500154 u16 csum_size =
155 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Mason61b49442008-07-31 15:42:53 -0400156 int ret;
157 struct btrfs_path *path;
158 struct btrfs_csum_item *item = NULL;
159 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
160
161 path = btrfs_alloc_path();
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400162 if (bio->bi_size > PAGE_CACHE_SIZE * 8)
163 path->reada = 2;
Chris Mason61b49442008-07-31 15:42:53 -0400164
165 WARN_ON(bio->bi_vcnt <= 0);
166
Chris Masond20f7042008-12-08 16:58:54 -0500167 disk_bytenr = (u64)bio->bi_sector << 9;
Chris Mason61b49442008-07-31 15:42:53 -0400168 while(bio_index < bio->bi_vcnt) {
169 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
Chris Masond20f7042008-12-08 16:58:54 -0500170 ret = btrfs_find_ordered_sum(inode, offset, disk_bytenr, &sum);
Chris Mason61b49442008-07-31 15:42:53 -0400171 if (ret == 0)
172 goto found;
173
Chris Masond20f7042008-12-08 16:58:54 -0500174 if (!item || disk_bytenr < item_start_offset ||
175 disk_bytenr >= item_last_offset) {
Chris Mason61b49442008-07-31 15:42:53 -0400176 struct btrfs_key found_key;
177 u32 item_size;
178
179 if (item)
180 btrfs_release_path(root, path);
Chris Masond20f7042008-12-08 16:58:54 -0500181 item = btrfs_lookup_csum(NULL, root->fs_info->csum_root,
182 path, disk_bytenr, 0);
Chris Mason61b49442008-07-31 15:42:53 -0400183 if (IS_ERR(item)) {
184 ret = PTR_ERR(item);
185 if (ret == -ENOENT || ret == -EFBIG)
186 ret = 0;
187 sum = 0;
188 printk("no csum found for inode %lu start "
189 "%llu\n", inode->i_ino,
190 (unsigned long long)offset);
Chris Mason6dab8152008-08-04 08:35:53 -0400191 item = NULL;
Chris Mason39be25c2008-11-10 11:50:50 -0500192 btrfs_release_path(root, path);
Chris Mason61b49442008-07-31 15:42:53 -0400193 goto found;
194 }
195 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
196 path->slots[0]);
197
198 item_start_offset = found_key.offset;
199 item_size = btrfs_item_size_nr(path->nodes[0],
200 path->slots[0]);
201 item_last_offset = item_start_offset +
Josef Bacik607d4322008-12-02 07:17:45 -0500202 (item_size / csum_size) *
Chris Mason61b49442008-07-31 15:42:53 -0400203 root->sectorsize;
204 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
205 struct btrfs_csum_item);
206 }
207 /*
208 * this byte range must be able to fit inside
209 * a single leaf so it will also fit inside a u32
210 */
Chris Masond20f7042008-12-08 16:58:54 -0500211 diff = disk_bytenr - item_start_offset;
Chris Mason61b49442008-07-31 15:42:53 -0400212 diff = diff / root->sectorsize;
Josef Bacik607d4322008-12-02 07:17:45 -0500213 diff = diff * csum_size;
Chris Mason61b49442008-07-31 15:42:53 -0400214
215 read_extent_buffer(path->nodes[0], &sum,
Chris Mason3de9d6b2008-08-04 23:17:27 -0400216 ((unsigned long)item) + diff,
Josef Bacik607d4322008-12-02 07:17:45 -0500217 csum_size);
Chris Mason61b49442008-07-31 15:42:53 -0400218found:
Chris Masond20f7042008-12-08 16:58:54 -0500219 if (dst)
220 *dst++ = sum;
221 else
222 set_state_private(io_tree, offset, sum);
223 disk_bytenr += bvec->bv_len;
Chris Mason61b49442008-07-31 15:42:53 -0400224 bio_index++;
225 bvec++;
226 }
227 btrfs_free_path(path);
228 return 0;
229}
230
Chris Mason3edf7d32008-07-18 06:17:13 -0400231int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
Chris Masond20f7042008-12-08 16:58:54 -0500232 struct bio *bio, u64 file_start, int contig)
Chris Masone0156402008-04-16 11:15:20 -0400233{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400234 struct btrfs_ordered_sum *sums;
235 struct btrfs_sector_sum *sector_sum;
Chris Mason3edf7d32008-07-18 06:17:13 -0400236 struct btrfs_ordered_extent *ordered;
Chris Masone0156402008-04-16 11:15:20 -0400237 char *data;
238 struct bio_vec *bvec = bio->bi_io_vec;
239 int bio_index = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400240 unsigned long total_bytes = 0;
241 unsigned long this_sum_bytes = 0;
242 u64 offset;
Chris Masond20f7042008-12-08 16:58:54 -0500243 u64 disk_bytenr;
Chris Masone0156402008-04-16 11:15:20 -0400244
Chris Masone6dcd2d2008-07-17 12:53:50 -0400245 WARN_ON(bio->bi_vcnt <= 0);
246 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
Chris Masone0156402008-04-16 11:15:20 -0400247 if (!sums)
248 return -ENOMEM;
Chris Mason3edf7d32008-07-18 06:17:13 -0400249
Chris Masoned98b562008-07-22 23:06:42 -0400250 sector_sum = sums->sums;
Chris Masond20f7042008-12-08 16:58:54 -0500251 disk_bytenr = (u64)bio->bi_sector << 9;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400252 sums->len = bio->bi_size;
253 INIT_LIST_HEAD(&sums->list);
Chris Masond20f7042008-12-08 16:58:54 -0500254
255 if (contig)
256 offset = file_start;
257 else
258 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
259
260 ordered = btrfs_lookup_ordered_extent(inode, offset);
Chris Mason3edf7d32008-07-18 06:17:13 -0400261 BUG_ON(!ordered);
Chris Masond20f7042008-12-08 16:58:54 -0500262 sums->bytenr = ordered->start;
Chris Masone0156402008-04-16 11:15:20 -0400263
264 while(bio_index < bio->bi_vcnt) {
Chris Masond20f7042008-12-08 16:58:54 -0500265 if (!contig)
266 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
267
268 if (!contig && (offset >= ordered->file_offset + ordered->len ||
269 offset < ordered->file_offset)) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400270 unsigned long bytes_left;
271 sums->len = this_sum_bytes;
272 this_sum_bytes = 0;
273 btrfs_add_ordered_sum(inode, ordered, sums);
274 btrfs_put_ordered_extent(ordered);
275
276 bytes_left = bio->bi_size - total_bytes;
277
278 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
279 GFP_NOFS);
280 BUG_ON(!sums);
Chris Masoned98b562008-07-22 23:06:42 -0400281 sector_sum = sums->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400282 sums->len = bytes_left;
Chris Masond20f7042008-12-08 16:58:54 -0500283 ordered = btrfs_lookup_ordered_extent(inode, offset);
Chris Mason3edf7d32008-07-18 06:17:13 -0400284 BUG_ON(!ordered);
Chris Masond20f7042008-12-08 16:58:54 -0500285 sums->bytenr = ordered->start;
Chris Mason3edf7d32008-07-18 06:17:13 -0400286 }
287
Chris Masone0156402008-04-16 11:15:20 -0400288 data = kmap_atomic(bvec->bv_page, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400289 sector_sum->sum = ~(u32)0;
290 sector_sum->sum = btrfs_csum_data(root,
291 data + bvec->bv_offset,
292 sector_sum->sum,
293 bvec->bv_len);
Chris Masone0156402008-04-16 11:15:20 -0400294 kunmap_atomic(data, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400295 btrfs_csum_final(sector_sum->sum,
296 (char *)&sector_sum->sum);
Chris Masond20f7042008-12-08 16:58:54 -0500297 sector_sum->bytenr = disk_bytenr;
Chris Masoned98b562008-07-22 23:06:42 -0400298
Chris Masone6dcd2d2008-07-17 12:53:50 -0400299 sector_sum++;
Chris Masone0156402008-04-16 11:15:20 -0400300 bio_index++;
Chris Mason3edf7d32008-07-18 06:17:13 -0400301 total_bytes += bvec->bv_len;
302 this_sum_bytes += bvec->bv_len;
Chris Masond20f7042008-12-08 16:58:54 -0500303 disk_bytenr += bvec->bv_len;
304 offset += bvec->bv_len;
Chris Masone0156402008-04-16 11:15:20 -0400305 bvec++;
306 }
Chris Masoned98b562008-07-22 23:06:42 -0400307 this_sum_bytes = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400308 btrfs_add_ordered_sum(inode, ordered, sums);
309 btrfs_put_ordered_extent(ordered);
Chris Masone0156402008-04-16 11:15:20 -0400310 return 0;
311}
312
Chris Mason459931e2008-12-10 09:10:46 -0500313/*
314 * helper function for csum removal, this expects the
315 * key to describe the csum pointed to by the path, and it expects
316 * the csum to overlap the range [bytenr, len]
317 *
318 * The csum should not be entirely contained in the range and the
319 * range should not be entirely contained in the csum.
320 *
321 * This calls btrfs_truncate_item with the correct args based on the
322 * overlap, and fixes up the key as required.
323 */
324static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
325 struct btrfs_root *root,
326 struct btrfs_path *path,
327 struct btrfs_key *key,
328 u64 bytenr, u64 len)
329{
330 struct extent_buffer *leaf;
331 u16 csum_size =
332 btrfs_super_csum_size(&root->fs_info->super_copy);
333 u64 csum_end;
334 u64 end_byte = bytenr + len;
335 u32 blocksize_bits = root->fs_info->sb->s_blocksize_bits;
336 int ret;
337
338 leaf = path->nodes[0];
339 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
340 csum_end <<= root->fs_info->sb->s_blocksize_bits;
341 csum_end += key->offset;
342
343 if (key->offset < bytenr && csum_end <= end_byte) {
344 /*
345 * [ bytenr - len ]
346 * [ ]
347 * [csum ]
348 * A simple truncate off the end of the item
349 */
350 u32 new_size = (bytenr - key->offset) >> blocksize_bits;
351 new_size *= csum_size;
352 ret = btrfs_truncate_item(trans, root, path, new_size, 1);
353 BUG_ON(ret);
354 } else if (key->offset >= bytenr && csum_end > end_byte &&
355 end_byte > key->offset) {
356 /*
357 * [ bytenr - len ]
358 * [ ]
359 * [csum ]
360 * we need to truncate from the beginning of the csum
361 */
362 u32 new_size = (csum_end - end_byte) >> blocksize_bits;
363 new_size *= csum_size;
364
365 ret = btrfs_truncate_item(trans, root, path, new_size, 0);
366 BUG_ON(ret);
367
368 key->offset = end_byte;
369 ret = btrfs_set_item_key_safe(trans, root, path, key);
370 BUG_ON(ret);
371 } else {
372 BUG();
373 }
374 return 0;
375}
376
377/*
378 * deletes the csum items from the csum tree for a given
379 * range of bytes.
380 */
381int btrfs_del_csums(struct btrfs_trans_handle *trans,
382 struct btrfs_root *root, u64 bytenr, u64 len)
383{
384 struct btrfs_path *path;
385 struct btrfs_key key;
386 u64 end_byte = bytenr + len;
387 u64 csum_end;
388 struct extent_buffer *leaf;
389 int ret;
390 u16 csum_size =
391 btrfs_super_csum_size(&root->fs_info->super_copy);
392 int blocksize_bits = root->fs_info->sb->s_blocksize_bits;
393
394 root = root->fs_info->csum_root;
395
396 path = btrfs_alloc_path();
397
398 while(1) {
399 key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
400 key.offset = end_byte - 1;
401 key.type = BTRFS_EXTENT_CSUM_KEY;
402
403 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
404 if (ret > 0) {
405 if (path->slots[0] == 0)
406 goto out;
407 path->slots[0]--;
408 }
409 leaf = path->nodes[0];
410 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
411
412 if (key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
413 key.type != BTRFS_EXTENT_CSUM_KEY) {
414 break;
415 }
416
417 if (key.offset >= end_byte)
418 break;
419
420 csum_end = btrfs_item_size_nr(leaf, path->slots[0]) / csum_size;
421 csum_end <<= blocksize_bits;
422 csum_end += key.offset;
423
424 /* this csum ends before we start, we're done */
425 if (csum_end <= bytenr)
426 break;
427
428 /* delete the entire item, it is inside our range */
429 if (key.offset >= bytenr && csum_end <= end_byte) {
430 ret = btrfs_del_item(trans, root, path);
431 BUG_ON(ret);
432 } else if (key.offset < bytenr && csum_end > end_byte) {
433 unsigned long offset;
434 unsigned long shift_len;
435 unsigned long item_offset;
436 /*
437 * [ bytenr - len ]
438 * [csum ]
439 *
440 * Our bytes are in the middle of the csum,
441 * we need to split this item and insert a new one.
442 *
443 * But we can't drop the path because the
444 * csum could change, get removed, extended etc.
445 *
446 * The trick here is the max size of a csum item leaves
447 * enough room in the tree block for a single
448 * item header. So, we split the item in place,
449 * adding a new header pointing to the existing
450 * bytes. Then we loop around again and we have
451 * a nicely formed csum item that we can neatly
452 * truncate.
453 */
454 offset = (bytenr - key.offset) >> blocksize_bits;
455 offset *= csum_size;
456
457 shift_len = (len >> blocksize_bits) * csum_size;
458
459 item_offset = btrfs_item_ptr_offset(leaf,
460 path->slots[0]);
461
462 memset_extent_buffer(leaf, 0, item_offset + offset,
463 shift_len);
464 key.offset = bytenr;
465
466 /*
467 * btrfs_split_item returns -EAGAIN when the
468 * item changed size or key
469 */
470 ret = btrfs_split_item(trans, root, path, &key, offset);
471 BUG_ON(ret && ret != -EAGAIN);
472
473 key.offset = end_byte - 1;
474 } else {
475 ret = truncate_one_csum(trans, root, path,
476 &key, bytenr, len);
477 BUG_ON(ret);
478 }
479 btrfs_release_path(root, path);
480 }
481out:
482 btrfs_free_path(path);
483 return 0;
484}
485
Chris Mason065631f2008-02-20 12:07:25 -0500486int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
Chris Masond20f7042008-12-08 16:58:54 -0500487 struct btrfs_root *root,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400488 struct btrfs_ordered_sum *sums)
Chris Masonf254e522007-03-29 15:15:27 -0400489{
Chris Masond20f7042008-12-08 16:58:54 -0500490 u64 bytenr;
Chris Masonf254e522007-03-29 15:15:27 -0400491 int ret;
492 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400493 struct btrfs_key found_key;
Chris Mason065631f2008-02-20 12:07:25 -0500494 u64 next_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400495 u64 total_bytes = 0;
Chris Mason065631f2008-02-20 12:07:25 -0500496 int found_next;
Chris Mason5caf2a02007-04-02 11:20:42 -0400497 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400498 struct btrfs_csum_item *item;
Chris Mason065631f2008-02-20 12:07:25 -0500499 struct btrfs_csum_item *item_end;
Chris Masonff79f812007-10-15 16:22:25 -0400500 struct extent_buffer *leaf = NULL;
Chris Mason6567e832007-04-16 09:22:45 -0400501 u64 csum_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400502 struct btrfs_sector_sum *sector_sum;
Chris Masonf578d4b2007-10-25 15:42:56 -0400503 u32 nritems;
504 u32 ins_size;
Chris Mason6e92f5e2008-02-20 12:07:25 -0500505 char *eb_map;
506 char *eb_token;
507 unsigned long map_len;
508 unsigned long map_start;
Josef Bacik607d4322008-12-02 07:17:45 -0500509 u16 csum_size =
510 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500511
Chris Mason5caf2a02007-04-02 11:20:42 -0400512 path = btrfs_alloc_path();
513 BUG_ON(!path);
Chris Masoned98b562008-07-22 23:06:42 -0400514 sector_sum = sums->sums;
Chris Mason065631f2008-02-20 12:07:25 -0500515again:
516 next_offset = (u64)-1;
517 found_next = 0;
Chris Masond20f7042008-12-08 16:58:54 -0500518 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
519 file_key.offset = sector_sum->bytenr;
520 bytenr = sector_sum->bytenr;
521 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400522
Chris Masond20f7042008-12-08 16:58:54 -0500523 item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
Chris Masonff79f812007-10-15 16:22:25 -0400524 if (!IS_ERR(item)) {
525 leaf = path->nodes[0];
Chris Mason639cb582008-08-28 06:15:25 -0400526 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -0400527 goto found;
Chris Masonff79f812007-10-15 16:22:25 -0400528 }
Chris Masona429e512007-04-18 16:15:28 -0400529 ret = PTR_ERR(item);
530 if (ret == -EFBIG) {
531 u32 item_size;
532 /* we found one, but it isn't big enough yet */
Chris Mason5f39d392007-10-15 16:14:19 -0400533 leaf = path->nodes[0];
534 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500535 if ((item_size / csum_size) >=
536 MAX_CSUM_ITEMS(root, csum_size)) {
Chris Masona429e512007-04-18 16:15:28 -0400537 /* already at max size, make a new one */
538 goto insert;
539 }
540 } else {
Chris Masonf578d4b2007-10-25 15:42:56 -0400541 int slot = path->slots[0] + 1;
Chris Masona429e512007-04-18 16:15:28 -0400542 /* we didn't find a csum item, insert one */
Chris Masonf578d4b2007-10-25 15:42:56 -0400543 nritems = btrfs_header_nritems(path->nodes[0]);
544 if (path->slots[0] >= nritems - 1) {
545 ret = btrfs_next_leaf(root, path);
Yanb56baf52007-10-29 12:01:05 -0400546 if (ret == 1)
Chris Masonf578d4b2007-10-25 15:42:56 -0400547 found_next = 1;
Yanb56baf52007-10-29 12:01:05 -0400548 if (ret != 0)
Chris Masonf578d4b2007-10-25 15:42:56 -0400549 goto insert;
Yanb56baf52007-10-29 12:01:05 -0400550 slot = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400551 }
552 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
Chris Masond20f7042008-12-08 16:58:54 -0500553 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
554 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
Chris Masonf578d4b2007-10-25 15:42:56 -0400555 found_next = 1;
556 goto insert;
557 }
558 next_offset = found_key.offset;
559 found_next = 1;
Chris Masona429e512007-04-18 16:15:28 -0400560 goto insert;
561 }
562
563 /*
564 * at this point, we know the tree has an item, but it isn't big
565 * enough yet to put our csum in. Grow it
566 */
567 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400568 ret = btrfs_search_slot(trans, root, &file_key, path,
Josef Bacik607d4322008-12-02 07:17:45 -0500569 csum_size, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400570 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400571 goto fail_unlock;
Chris Mason459931e2008-12-10 09:10:46 -0500572
573 if (ret > 0) {
574 if (path->slots[0] == 0)
575 goto insert;
576 path->slots[0]--;
Chris Mason6567e832007-04-16 09:22:45 -0400577 }
Chris Mason459931e2008-12-10 09:10:46 -0500578
Chris Mason5f39d392007-10-15 16:14:19 -0400579 leaf = path->nodes[0];
580 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masond20f7042008-12-08 16:58:54 -0500581 csum_offset = (bytenr - found_key.offset) >>
Chris Mason6567e832007-04-16 09:22:45 -0400582 root->fs_info->sb->s_blocksize_bits;
Chris Mason459931e2008-12-10 09:10:46 -0500583
Chris Masond20f7042008-12-08 16:58:54 -0500584 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
585 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
Josef Bacik607d4322008-12-02 07:17:45 -0500586 csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
Chris Mason6567e832007-04-16 09:22:45 -0400587 goto insert;
588 }
Chris Mason459931e2008-12-10 09:10:46 -0500589
Chris Mason5f39d392007-10-15 16:14:19 -0400590 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
Josef Bacik607d4322008-12-02 07:17:45 -0500591 csum_size) {
592 u32 diff = (csum_offset + 1) * csum_size;
Chris Mason459931e2008-12-10 09:10:46 -0500593
594 /*
595 * is the item big enough already? we dropped our lock
596 * before and need to recheck
597 */
598 if (diff < btrfs_item_size_nr(leaf, path->slots[0]))
599 goto csum;
600
Chris Mason5f39d392007-10-15 16:14:19 -0400601 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -0500602 if (diff != csum_size) {
Chris Mason3a686372007-05-24 13:35:57 -0400603 goto insert;
Chris Mason459931e2008-12-10 09:10:46 -0500604 }
605
Chris Masona429e512007-04-18 16:15:28 -0400606 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400607 BUG_ON(ret);
608 goto csum;
609 }
610
611insert:
Chris Masona429e512007-04-18 16:15:28 -0400612 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400613 csum_offset = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400614 if (found_next) {
Chris Masond20f7042008-12-08 16:58:54 -0500615 u64 tmp = total_bytes + root->sectorsize;
616 u64 next_sector = sector_sum->bytenr;
617 struct btrfs_sector_sum *next = sector_sum + 1;
618
619 while(tmp < sums->len) {
620 if (next_sector + root->sectorsize != next->bytenr)
621 break;
622 tmp += root->sectorsize;
623 next_sector = next->bytenr;
624 next++;
625 }
626 tmp = min(tmp, next_offset - file_key.offset);
Chris Masonf578d4b2007-10-25 15:42:56 -0400627 tmp >>= root->fs_info->sb->s_blocksize_bits;
628 tmp = max((u64)1, tmp);
Josef Bacik607d4322008-12-02 07:17:45 -0500629 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
630 ins_size = csum_size * tmp;
Chris Masonf578d4b2007-10-25 15:42:56 -0400631 } else {
Josef Bacik607d4322008-12-02 07:17:45 -0500632 ins_size = csum_size;
Chris Masonf578d4b2007-10-25 15:42:56 -0400633 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400634 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masonf578d4b2007-10-25 15:42:56 -0400635 ins_size);
Chris Mason54aa1f42007-06-22 14:16:25 -0400636 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400637 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400638 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400639 WARN_ON(1);
Chris Mason53863232008-08-15 15:34:18 -0400640 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400641 }
Chris Mason6567e832007-04-16 09:22:45 -0400642csum:
Chris Mason5f39d392007-10-15 16:14:19 -0400643 leaf = path->nodes[0];
644 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Masonf254e522007-03-29 15:15:27 -0400645 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400646 item = (struct btrfs_csum_item *)((unsigned char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500647 csum_offset * csum_size);
Chris Masonb18c6682007-04-17 13:26:50 -0400648found:
Chris Mason065631f2008-02-20 12:07:25 -0500649 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
650 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
651 btrfs_item_size_nr(leaf, path->slots[0]));
Chris Mason6e92f5e2008-02-20 12:07:25 -0500652 eb_token = NULL;
Chris Mason53863232008-08-15 15:34:18 -0400653 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400654next_sector:
Chris Masonaadfeb62008-01-29 09:10:27 -0500655
Chris Mason6e92f5e2008-02-20 12:07:25 -0500656 if (!eb_token ||
Josef Bacik607d4322008-12-02 07:17:45 -0500657 (unsigned long)item + csum_size >= map_start + map_len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500658 int err;
659
660 if (eb_token)
Chris Masoneb209782008-02-21 09:30:08 -0500661 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500662 eb_token = NULL;
663 err = map_private_extent_buffer(leaf, (unsigned long)item,
Josef Bacik607d4322008-12-02 07:17:45 -0500664 csum_size,
Chris Mason6e92f5e2008-02-20 12:07:25 -0500665 &eb_token, &eb_map,
Chris Masoneb209782008-02-21 09:30:08 -0500666 &map_start, &map_len, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500667 if (err)
668 eb_token = NULL;
669 }
670 if (eb_token) {
671 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
Josef Bacik607d4322008-12-02 07:17:45 -0500672 &sector_sum->sum, csum_size);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500673 } else {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400674 write_extent_buffer(leaf, &sector_sum->sum,
Josef Bacik607d4322008-12-02 07:17:45 -0500675 (unsigned long)item, csum_size);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500676 }
Chris Mason7f3c74f2008-07-18 12:01:11 -0400677
Chris Masone6dcd2d2008-07-17 12:53:50 -0400678 total_bytes += root->sectorsize;
679 sector_sum++;
680 if (total_bytes < sums->len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500681 item = (struct btrfs_csum_item *)((char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500682 csum_size);
Chris Masond20f7042008-12-08 16:58:54 -0500683 if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
684 sector_sum->bytenr) {
685 bytenr = sector_sum->bytenr;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400686 goto next_sector;
Chris Mason2e1a9922008-02-20 15:44:32 -0500687 }
Chris Mason065631f2008-02-20 12:07:25 -0500688 }
Chris Mason6e92f5e2008-02-20 12:07:25 -0500689 if (eb_token) {
Chris Masoneb209782008-02-21 09:30:08 -0500690 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500691 eb_token = NULL;
692 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400693 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason53863232008-08-15 15:34:18 -0400694 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400695 if (total_bytes < sums->len) {
Chris Mason065631f2008-02-20 12:07:25 -0500696 btrfs_release_path(root, path);
697 goto again;
698 }
Chris Mason53863232008-08-15 15:34:18 -0400699out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400700 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400701 return ret;
Chris Mason53863232008-08-15 15:34:18 -0400702
703fail_unlock:
Chris Mason53863232008-08-15 15:34:18 -0400704 goto out;
Chris Masonf254e522007-03-29 15:15:27 -0400705}