blob: a3ad2ce00116a6a8e7e8e27093f5f0a81bd7e6b9 [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 Mason065631f2008-02-20 12:07:25 -0500313int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
Chris Masond20f7042008-12-08 16:58:54 -0500314 struct btrfs_root *root,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400315 struct btrfs_ordered_sum *sums)
Chris Masonf254e522007-03-29 15:15:27 -0400316{
Chris Masond20f7042008-12-08 16:58:54 -0500317 u64 bytenr;
Chris Masonf254e522007-03-29 15:15:27 -0400318 int ret;
319 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400320 struct btrfs_key found_key;
Chris Mason065631f2008-02-20 12:07:25 -0500321 u64 next_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400322 u64 total_bytes = 0;
Chris Mason065631f2008-02-20 12:07:25 -0500323 int found_next;
Chris Mason5caf2a02007-04-02 11:20:42 -0400324 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400325 struct btrfs_csum_item *item;
Chris Mason065631f2008-02-20 12:07:25 -0500326 struct btrfs_csum_item *item_end;
Chris Masonff79f812007-10-15 16:22:25 -0400327 struct extent_buffer *leaf = NULL;
Chris Mason6567e832007-04-16 09:22:45 -0400328 u64 csum_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400329 struct btrfs_sector_sum *sector_sum;
Chris Masonf578d4b2007-10-25 15:42:56 -0400330 u32 nritems;
331 u32 ins_size;
Chris Mason6e92f5e2008-02-20 12:07:25 -0500332 char *eb_map;
333 char *eb_token;
334 unsigned long map_len;
335 unsigned long map_start;
Josef Bacik607d4322008-12-02 07:17:45 -0500336 u16 csum_size =
337 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500338
Chris Mason5caf2a02007-04-02 11:20:42 -0400339 path = btrfs_alloc_path();
340 BUG_ON(!path);
Chris Masoned98b562008-07-22 23:06:42 -0400341 sector_sum = sums->sums;
Chris Mason065631f2008-02-20 12:07:25 -0500342again:
343 next_offset = (u64)-1;
344 found_next = 0;
Chris Masond20f7042008-12-08 16:58:54 -0500345 file_key.objectid = BTRFS_EXTENT_CSUM_OBJECTID;
346 file_key.offset = sector_sum->bytenr;
347 bytenr = sector_sum->bytenr;
348 btrfs_set_key_type(&file_key, BTRFS_EXTENT_CSUM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400349
Chris Masond20f7042008-12-08 16:58:54 -0500350 item = btrfs_lookup_csum(trans, root, path, sector_sum->bytenr, 1);
Chris Masonff79f812007-10-15 16:22:25 -0400351 if (!IS_ERR(item)) {
352 leaf = path->nodes[0];
Chris Mason639cb582008-08-28 06:15:25 -0400353 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -0400354 goto found;
Chris Masonff79f812007-10-15 16:22:25 -0400355 }
Chris Masona429e512007-04-18 16:15:28 -0400356 ret = PTR_ERR(item);
357 if (ret == -EFBIG) {
358 u32 item_size;
359 /* we found one, but it isn't big enough yet */
Chris Mason5f39d392007-10-15 16:14:19 -0400360 leaf = path->nodes[0];
361 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500362 if ((item_size / csum_size) >=
363 MAX_CSUM_ITEMS(root, csum_size)) {
Chris Masona429e512007-04-18 16:15:28 -0400364 /* already at max size, make a new one */
365 goto insert;
366 }
367 } else {
Chris Masonf578d4b2007-10-25 15:42:56 -0400368 int slot = path->slots[0] + 1;
Chris Masona429e512007-04-18 16:15:28 -0400369 /* we didn't find a csum item, insert one */
Chris Masonf578d4b2007-10-25 15:42:56 -0400370 nritems = btrfs_header_nritems(path->nodes[0]);
371 if (path->slots[0] >= nritems - 1) {
372 ret = btrfs_next_leaf(root, path);
Yanb56baf52007-10-29 12:01:05 -0400373 if (ret == 1)
Chris Masonf578d4b2007-10-25 15:42:56 -0400374 found_next = 1;
Yanb56baf52007-10-29 12:01:05 -0400375 if (ret != 0)
Chris Masonf578d4b2007-10-25 15:42:56 -0400376 goto insert;
Yanb56baf52007-10-29 12:01:05 -0400377 slot = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400378 }
379 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
Chris Masond20f7042008-12-08 16:58:54 -0500380 if (found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
381 found_key.type != BTRFS_EXTENT_CSUM_KEY) {
Chris Masonf578d4b2007-10-25 15:42:56 -0400382 found_next = 1;
383 goto insert;
384 }
385 next_offset = found_key.offset;
386 found_next = 1;
Chris Masona429e512007-04-18 16:15:28 -0400387 goto insert;
388 }
389
390 /*
391 * at this point, we know the tree has an item, but it isn't big
392 * enough yet to put our csum in. Grow it
393 */
394 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400395 ret = btrfs_search_slot(trans, root, &file_key, path,
Josef Bacik607d4322008-12-02 07:17:45 -0500396 csum_size, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400397 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400398 goto fail_unlock;
Chris Mason6567e832007-04-16 09:22:45 -0400399 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400400 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400401 }
402 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400403 goto insert;
404 }
405 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400406 leaf = path->nodes[0];
407 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masond20f7042008-12-08 16:58:54 -0500408 csum_offset = (bytenr - found_key.offset) >>
Chris Mason6567e832007-04-16 09:22:45 -0400409 root->fs_info->sb->s_blocksize_bits;
Chris Masond20f7042008-12-08 16:58:54 -0500410 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_CSUM_KEY ||
411 found_key.objectid != BTRFS_EXTENT_CSUM_OBJECTID ||
Josef Bacik607d4322008-12-02 07:17:45 -0500412 csum_offset >= MAX_CSUM_ITEMS(root, csum_size)) {
Chris Mason6567e832007-04-16 09:22:45 -0400413 goto insert;
414 }
Chris Mason5f39d392007-10-15 16:14:19 -0400415 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
Josef Bacik607d4322008-12-02 07:17:45 -0500416 csum_size) {
417 u32 diff = (csum_offset + 1) * csum_size;
Chris Mason5f39d392007-10-15 16:14:19 -0400418 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
Josef Bacik607d4322008-12-02 07:17:45 -0500419 if (diff != csum_size)
Chris Mason3a686372007-05-24 13:35:57 -0400420 goto insert;
Chris Masona429e512007-04-18 16:15:28 -0400421 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400422 BUG_ON(ret);
423 goto csum;
424 }
425
426insert:
Chris Masona429e512007-04-18 16:15:28 -0400427 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400428 csum_offset = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400429 if (found_next) {
Chris Masond20f7042008-12-08 16:58:54 -0500430 u64 tmp = total_bytes + root->sectorsize;
431 u64 next_sector = sector_sum->bytenr;
432 struct btrfs_sector_sum *next = sector_sum + 1;
433
434 while(tmp < sums->len) {
435 if (next_sector + root->sectorsize != next->bytenr)
436 break;
437 tmp += root->sectorsize;
438 next_sector = next->bytenr;
439 next++;
440 }
441 tmp = min(tmp, next_offset - file_key.offset);
Chris Masonf578d4b2007-10-25 15:42:56 -0400442 tmp >>= root->fs_info->sb->s_blocksize_bits;
443 tmp = max((u64)1, tmp);
Josef Bacik607d4322008-12-02 07:17:45 -0500444 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root, csum_size));
445 ins_size = csum_size * tmp;
Chris Masonf578d4b2007-10-25 15:42:56 -0400446 } else {
Josef Bacik607d4322008-12-02 07:17:45 -0500447 ins_size = csum_size;
Chris Masonf578d4b2007-10-25 15:42:56 -0400448 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400449 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masonf578d4b2007-10-25 15:42:56 -0400450 ins_size);
Chris Mason54aa1f42007-06-22 14:16:25 -0400451 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400452 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400453 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400454 WARN_ON(1);
Chris Mason53863232008-08-15 15:34:18 -0400455 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400456 }
Chris Mason6567e832007-04-16 09:22:45 -0400457csum:
Chris Mason5f39d392007-10-15 16:14:19 -0400458 leaf = path->nodes[0];
459 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Masonf254e522007-03-29 15:15:27 -0400460 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400461 item = (struct btrfs_csum_item *)((unsigned char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500462 csum_offset * csum_size);
Chris Masonb18c6682007-04-17 13:26:50 -0400463found:
Chris Mason065631f2008-02-20 12:07:25 -0500464 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
465 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
466 btrfs_item_size_nr(leaf, path->slots[0]));
Chris Mason6e92f5e2008-02-20 12:07:25 -0500467 eb_token = NULL;
Chris Mason53863232008-08-15 15:34:18 -0400468 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400469next_sector:
Chris Masonaadfeb62008-01-29 09:10:27 -0500470
Chris Mason6e92f5e2008-02-20 12:07:25 -0500471 if (!eb_token ||
Josef Bacik607d4322008-12-02 07:17:45 -0500472 (unsigned long)item + csum_size >= map_start + map_len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500473 int err;
474
475 if (eb_token)
Chris Masoneb209782008-02-21 09:30:08 -0500476 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500477 eb_token = NULL;
478 err = map_private_extent_buffer(leaf, (unsigned long)item,
Josef Bacik607d4322008-12-02 07:17:45 -0500479 csum_size,
Chris Mason6e92f5e2008-02-20 12:07:25 -0500480 &eb_token, &eb_map,
Chris Masoneb209782008-02-21 09:30:08 -0500481 &map_start, &map_len, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500482 if (err)
483 eb_token = NULL;
484 }
485 if (eb_token) {
486 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
Josef Bacik607d4322008-12-02 07:17:45 -0500487 &sector_sum->sum, csum_size);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500488 } else {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400489 write_extent_buffer(leaf, &sector_sum->sum,
Josef Bacik607d4322008-12-02 07:17:45 -0500490 (unsigned long)item, csum_size);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500491 }
Chris Mason7f3c74f2008-07-18 12:01:11 -0400492
Chris Masone6dcd2d2008-07-17 12:53:50 -0400493 total_bytes += root->sectorsize;
494 sector_sum++;
495 if (total_bytes < sums->len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500496 item = (struct btrfs_csum_item *)((char *)item +
Josef Bacik607d4322008-12-02 07:17:45 -0500497 csum_size);
Chris Masond20f7042008-12-08 16:58:54 -0500498 if (item < item_end && bytenr + PAGE_CACHE_SIZE ==
499 sector_sum->bytenr) {
500 bytenr = sector_sum->bytenr;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400501 goto next_sector;
Chris Mason2e1a9922008-02-20 15:44:32 -0500502 }
Chris Mason065631f2008-02-20 12:07:25 -0500503 }
Chris Mason6e92f5e2008-02-20 12:07:25 -0500504 if (eb_token) {
Chris Masoneb209782008-02-21 09:30:08 -0500505 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500506 eb_token = NULL;
507 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400508 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason53863232008-08-15 15:34:18 -0400509 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400510 if (total_bytes < sums->len) {
Chris Mason065631f2008-02-20 12:07:25 -0500511 btrfs_release_path(root, path);
512 goto again;
513 }
Chris Mason53863232008-08-15 15:34:18 -0400514out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400515 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400516 return ret;
Chris Mason53863232008-08-15 15:34:18 -0400517
518fail_unlock:
Chris Mason53863232008-08-15 15:34:18 -0400519 goto out;
Chris Masonf254e522007-03-29 15:15:27 -0400520}
521
Chris Mason1de037a2007-05-29 15:17:08 -0400522int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
523 struct btrfs_root *root, struct btrfs_path *path,
524 u64 isize)
525{
526 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400527 struct extent_buffer *leaf = path->nodes[0];
Chris Mason1de037a2007-05-29 15:17:08 -0400528 int slot = path->slots[0];
529 int ret;
530 u32 new_item_size;
531 u64 new_item_span;
532 u64 blocks;
533
Chris Mason5f39d392007-10-15 16:14:19 -0400534 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason1de037a2007-05-29 15:17:08 -0400535 if (isize <= key.offset)
536 return 0;
537 new_item_span = isize - key.offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400538 blocks = (new_item_span + root->sectorsize - 1) >>
Chris Mason84f54cf2007-06-12 07:43:08 -0400539 root->fs_info->sb->s_blocksize_bits;
Josef Bacik607d4322008-12-02 07:17:45 -0500540 new_item_size = blocks *
541 btrfs_super_csum_size(&root->fs_info->super_copy);
Chris Mason5f39d392007-10-15 16:14:19 -0400542 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
Chris Mason1de037a2007-05-29 15:17:08 -0400543 return 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400544 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
Chris Mason1de037a2007-05-29 15:17:08 -0400545 BUG_ON(ret);
546 return ret;
547}