blob: 51aba8cee7ce3cc7add24c0bfe52e40cde09d12c [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason065631f2008-02-20 12:07:25 -050019#include <linux/bio.h>
20#include <linux/pagemap.h>
21#include <linux/highmem.h>
Chris Mason1e1d2702007-03-15 19:03:33 -040022#include "ctree.h"
Chris Masondee26a92007-03-26 16:00:06 -040023#include "disk-io.h"
Chris Mason9f5fae22007-03-20 14:38:32 -040024#include "transaction.h"
Chris Mason1de037a2007-05-29 15:17:08 -040025#include "print-tree.h"
Chris Mason1e1d2702007-03-15 19:03:33 -040026
Chris Mason6567e832007-04-16 09:22:45 -040027#define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
Chris Mason509659c2007-05-10 12:36:17 -040028 sizeof(struct btrfs_item) * 2) / \
29 BTRFS_CRC32_SIZE) - 1))
Chris Masonb18c6682007-04-17 13:26:50 -040030int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
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,
34 u64 num_bytes, u64 offset)
Chris Mason9f5fae22007-03-20 14:38:32 -040035{
Chris Masondee26a92007-03-26 16:00:06 -040036 int ret = 0;
37 struct btrfs_file_extent_item *item;
38 struct btrfs_key file_key;
Chris Mason5caf2a02007-04-02 11:20:42 -040039 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040040 struct extent_buffer *leaf;
Chris Masondee26a92007-03-26 16:00:06 -040041
Chris Mason5caf2a02007-04-02 11:20:42 -040042 path = btrfs_alloc_path();
43 BUG_ON(!path);
Chris Masondee26a92007-03-26 16:00:06 -040044 file_key.objectid = objectid;
Chris Masonb18c6682007-04-17 13:26:50 -040045 file_key.offset = pos;
Chris Masondee26a92007-03-26 16:00:06 -040046 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
47
Chris Mason5caf2a02007-04-02 11:20:42 -040048 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masondee26a92007-03-26 16:00:06 -040049 sizeof(*item));
Chris Mason54aa1f42007-06-22 14:16:25 -040050 if (ret < 0)
51 goto out;
Chris Mason9773a782007-03-27 11:26:26 -040052 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -040053 leaf = path->nodes[0];
54 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masondee26a92007-03-26 16:00:06 -040055 struct btrfs_file_extent_item);
Sage Weilf2eb0a22008-05-02 14:43:14 -040056 btrfs_set_file_extent_disk_bytenr(leaf, item, disk_offset);
Chris Masondb945352007-10-15 16:15:53 -040057 btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
Sage Weilf2eb0a22008-05-02 14:43:14 -040058 btrfs_set_file_extent_offset(leaf, item, offset);
Chris Masondb945352007-10-15 16:15:53 -040059 btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -040060 btrfs_set_file_extent_generation(leaf, item, trans->transid);
61 btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
62 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -040063out:
Chris Mason5caf2a02007-04-02 11:20:42 -040064 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -040065 return ret;
Chris Mason9f5fae22007-03-20 14:38:32 -040066}
Chris Masondee26a92007-03-26 16:00:06 -040067
Chris Masonb18c6682007-04-17 13:26:50 -040068struct btrfs_csum_item *btrfs_lookup_csum(struct btrfs_trans_handle *trans,
69 struct btrfs_root *root,
70 struct btrfs_path *path,
71 u64 objectid, u64 offset,
72 int cow)
Chris Mason6567e832007-04-16 09:22:45 -040073{
74 int ret;
75 struct btrfs_key file_key;
76 struct btrfs_key found_key;
77 struct btrfs_csum_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040078 struct extent_buffer *leaf;
Chris Mason6567e832007-04-16 09:22:45 -040079 u64 csum_offset = 0;
Chris Masona429e512007-04-18 16:15:28 -040080 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040081
82 file_key.objectid = objectid;
83 file_key.offset = offset;
Chris Mason6567e832007-04-16 09:22:45 -040084 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040085 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040086 if (ret < 0)
87 goto fail;
Chris Mason5f39d392007-10-15 16:14:19 -040088 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -040089 if (ret > 0) {
90 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040091 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040092 goto fail;
93 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -040094 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason6567e832007-04-16 09:22:45 -040095 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
96 found_key.objectid != objectid) {
97 goto fail;
98 }
99 csum_offset = (offset - found_key.offset) >>
100 root->fs_info->sb->s_blocksize_bits;
Chris Mason5f39d392007-10-15 16:14:19 -0400101 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400102 csums_in_item /= BTRFS_CRC32_SIZE;
Chris Masona429e512007-04-18 16:15:28 -0400103
104 if (csum_offset >= csums_in_item) {
105 ret = -EFBIG;
Chris Mason6567e832007-04-16 09:22:45 -0400106 goto fail;
107 }
108 }
109 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Mason509659c2007-05-10 12:36:17 -0400110 item = (struct btrfs_csum_item *)((unsigned char *)item +
111 csum_offset * BTRFS_CRC32_SIZE);
Chris Mason6567e832007-04-16 09:22:45 -0400112 return item;
113fail:
114 if (ret > 0)
Chris Masonb18c6682007-04-17 13:26:50 -0400115 ret = -ENOENT;
Chris Mason6567e832007-04-16 09:22:45 -0400116 return ERR_PTR(ret);
117}
118
119
Chris Masondee26a92007-03-26 16:00:06 -0400120int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
121 struct btrfs_root *root,
122 struct btrfs_path *path, u64 objectid,
Chris Mason9773a782007-03-27 11:26:26 -0400123 u64 offset, int mod)
Chris Masondee26a92007-03-26 16:00:06 -0400124{
125 int ret;
126 struct btrfs_key file_key;
127 int ins_len = mod < 0 ? -1 : 0;
128 int cow = mod != 0;
129
130 file_key.objectid = objectid;
Chris Mason70b2bef2007-04-17 15:39:32 -0400131 file_key.offset = offset;
Chris Masondee26a92007-03-26 16:00:06 -0400132 btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
133 ret = btrfs_search_slot(trans, root, &file_key, path, ins_len, cow);
134 return ret;
135}
Chris Masonf254e522007-03-29 15:15:27 -0400136
Chris Mason3de9d6b2008-08-04 23:17:27 -0400137#if 0 /* broken */
Chris Mason61b49442008-07-31 15:42:53 -0400138int btrfs_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
139 struct bio *bio)
140{
141 u32 sum;
142 struct bio_vec *bvec = bio->bi_io_vec;
143 int bio_index = 0;
144 u64 offset;
145 u64 item_start_offset = 0;
146 u64 item_last_offset = 0;
147 u32 diff;
148 int ret;
149 struct btrfs_path *path;
150 struct btrfs_csum_item *item = NULL;
151 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
152
153 path = btrfs_alloc_path();
Chris Mason61b49442008-07-31 15:42:53 -0400154
155 WARN_ON(bio->bi_vcnt <= 0);
156
157 while(bio_index < bio->bi_vcnt) {
158 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
159 ret = btrfs_find_ordered_sum(inode, offset, &sum);
160 if (ret == 0)
161 goto found;
162
163 if (!item || offset < item_start_offset ||
164 offset >= item_last_offset) {
165 struct btrfs_key found_key;
166 u32 item_size;
167
168 if (item)
169 btrfs_release_path(root, path);
170 item = btrfs_lookup_csum(NULL, root, path,
171 inode->i_ino, offset, 0);
172 if (IS_ERR(item)) {
173 ret = PTR_ERR(item);
174 if (ret == -ENOENT || ret == -EFBIG)
175 ret = 0;
176 sum = 0;
177 printk("no csum found for inode %lu start "
178 "%llu\n", inode->i_ino,
179 (unsigned long long)offset);
Chris Mason6dab8152008-08-04 08:35:53 -0400180 item = NULL;
Chris Mason61b49442008-07-31 15:42:53 -0400181 goto found;
182 }
183 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
184 path->slots[0]);
185
186 item_start_offset = found_key.offset;
187 item_size = btrfs_item_size_nr(path->nodes[0],
188 path->slots[0]);
189 item_last_offset = item_start_offset +
190 (item_size / BTRFS_CRC32_SIZE) *
191 root->sectorsize;
192 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
193 struct btrfs_csum_item);
194 }
195 /*
196 * this byte range must be able to fit inside
197 * a single leaf so it will also fit inside a u32
198 */
199 diff = offset - item_start_offset;
200 diff = diff / root->sectorsize;
201 diff = diff * BTRFS_CRC32_SIZE;
202
203 read_extent_buffer(path->nodes[0], &sum,
Chris Mason3de9d6b2008-08-04 23:17:27 -0400204 ((unsigned long)item) + diff,
Chris Mason61b49442008-07-31 15:42:53 -0400205 BTRFS_CRC32_SIZE);
206found:
207 set_state_private(io_tree, offset, sum);
208 bio_index++;
209 bvec++;
210 }
211 btrfs_free_path(path);
212 return 0;
213}
Chris Mason3de9d6b2008-08-04 23:17:27 -0400214#endif
Chris Mason61b49442008-07-31 15:42:53 -0400215
Chris Mason3edf7d32008-07-18 06:17:13 -0400216int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
217 struct bio *bio)
Chris Masone0156402008-04-16 11:15:20 -0400218{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400219 struct btrfs_ordered_sum *sums;
220 struct btrfs_sector_sum *sector_sum;
Chris Mason3edf7d32008-07-18 06:17:13 -0400221 struct btrfs_ordered_extent *ordered;
Chris Masone0156402008-04-16 11:15:20 -0400222 char *data;
223 struct bio_vec *bvec = bio->bi_io_vec;
224 int bio_index = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400225 unsigned long total_bytes = 0;
226 unsigned long this_sum_bytes = 0;
227 u64 offset;
Chris Masone0156402008-04-16 11:15:20 -0400228
Chris Masone6dcd2d2008-07-17 12:53:50 -0400229 WARN_ON(bio->bi_vcnt <= 0);
230 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
Chris Masone0156402008-04-16 11:15:20 -0400231 if (!sums)
232 return -ENOMEM;
Chris Mason3edf7d32008-07-18 06:17:13 -0400233
Chris Masoned98b562008-07-22 23:06:42 -0400234 sector_sum = sums->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400235 sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400236 sums->len = bio->bi_size;
237 INIT_LIST_HEAD(&sums->list);
Chris Mason3edf7d32008-07-18 06:17:13 -0400238 ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset);
239 BUG_ON(!ordered);
Chris Masone0156402008-04-16 11:15:20 -0400240
241 while(bio_index < bio->bi_vcnt) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400242 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
Chris Masone5a22172008-07-18 20:42:20 -0400243 if (offset >= ordered->file_offset + ordered->len ||
244 offset < ordered->file_offset) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400245 unsigned long bytes_left;
246 sums->len = this_sum_bytes;
247 this_sum_bytes = 0;
248 btrfs_add_ordered_sum(inode, ordered, sums);
249 btrfs_put_ordered_extent(ordered);
250
251 bytes_left = bio->bi_size - total_bytes;
252
253 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
254 GFP_NOFS);
255 BUG_ON(!sums);
Chris Masoned98b562008-07-22 23:06:42 -0400256 sector_sum = sums->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400257 sums->len = bytes_left;
258 sums->file_offset = offset;
259 ordered = btrfs_lookup_ordered_extent(inode,
260 sums->file_offset);
261 BUG_ON(!ordered);
262 }
263
Chris Masone0156402008-04-16 11:15:20 -0400264 data = kmap_atomic(bvec->bv_page, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400265 sector_sum->sum = ~(u32)0;
266 sector_sum->sum = btrfs_csum_data(root,
267 data + bvec->bv_offset,
268 sector_sum->sum,
269 bvec->bv_len);
Chris Masone0156402008-04-16 11:15:20 -0400270 kunmap_atomic(data, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400271 btrfs_csum_final(sector_sum->sum,
272 (char *)&sector_sum->sum);
273 sector_sum->offset = page_offset(bvec->bv_page) +
274 bvec->bv_offset;
Chris Masoned98b562008-07-22 23:06:42 -0400275
Chris Masone6dcd2d2008-07-17 12:53:50 -0400276 sector_sum++;
Chris Masone0156402008-04-16 11:15:20 -0400277 bio_index++;
Chris Mason3edf7d32008-07-18 06:17:13 -0400278 total_bytes += bvec->bv_len;
279 this_sum_bytes += bvec->bv_len;
Chris Masone0156402008-04-16 11:15:20 -0400280 bvec++;
281 }
Chris Masoned98b562008-07-22 23:06:42 -0400282 this_sum_bytes = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400283 btrfs_add_ordered_sum(inode, ordered, sums);
284 btrfs_put_ordered_extent(ordered);
Chris Masone0156402008-04-16 11:15:20 -0400285 return 0;
286}
287
Chris Mason065631f2008-02-20 12:07:25 -0500288int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
289 struct btrfs_root *root, struct inode *inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400290 struct btrfs_ordered_sum *sums)
Chris Masonf254e522007-03-29 15:15:27 -0400291{
Chris Mason065631f2008-02-20 12:07:25 -0500292 u64 objectid = inode->i_ino;
293 u64 offset;
Chris Masonf254e522007-03-29 15:15:27 -0400294 int ret;
295 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400296 struct btrfs_key found_key;
Chris Mason065631f2008-02-20 12:07:25 -0500297 u64 next_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400298 u64 total_bytes = 0;
Chris Mason065631f2008-02-20 12:07:25 -0500299 int found_next;
Chris Mason5caf2a02007-04-02 11:20:42 -0400300 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400301 struct btrfs_csum_item *item;
Chris Mason065631f2008-02-20 12:07:25 -0500302 struct btrfs_csum_item *item_end;
Chris Masonff79f812007-10-15 16:22:25 -0400303 struct extent_buffer *leaf = NULL;
Chris Mason6567e832007-04-16 09:22:45 -0400304 u64 csum_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400305 struct btrfs_sector_sum *sector_sum;
Chris Masonf578d4b2007-10-25 15:42:56 -0400306 u32 nritems;
307 u32 ins_size;
Chris Mason6e92f5e2008-02-20 12:07:25 -0500308 char *eb_map;
309 char *eb_token;
310 unsigned long map_len;
311 unsigned long map_start;
312
Chris Mason5caf2a02007-04-02 11:20:42 -0400313 path = btrfs_alloc_path();
314 BUG_ON(!path);
Chris Masoned98b562008-07-22 23:06:42 -0400315 sector_sum = sums->sums;
Chris Mason065631f2008-02-20 12:07:25 -0500316again:
317 next_offset = (u64)-1;
318 found_next = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400319 offset = sector_sum->offset;
Chris Masonf254e522007-03-29 15:15:27 -0400320 file_key.objectid = objectid;
321 file_key.offset = offset;
Chris Masonf254e522007-03-29 15:15:27 -0400322 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400323
324 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
Chris Masonff79f812007-10-15 16:22:25 -0400325 if (!IS_ERR(item)) {
326 leaf = path->nodes[0];
Chris Masona429e512007-04-18 16:15:28 -0400327 goto found;
Chris Masonff79f812007-10-15 16:22:25 -0400328 }
Chris Masona429e512007-04-18 16:15:28 -0400329 ret = PTR_ERR(item);
330 if (ret == -EFBIG) {
331 u32 item_size;
332 /* we found one, but it isn't big enough yet */
Chris Mason5f39d392007-10-15 16:14:19 -0400333 leaf = path->nodes[0];
334 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400335 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
Chris Masona429e512007-04-18 16:15:28 -0400336 /* already at max size, make a new one */
337 goto insert;
338 }
339 } else {
Chris Masonf578d4b2007-10-25 15:42:56 -0400340 int slot = path->slots[0] + 1;
Chris Masona429e512007-04-18 16:15:28 -0400341 /* we didn't find a csum item, insert one */
Chris Masonf578d4b2007-10-25 15:42:56 -0400342 nritems = btrfs_header_nritems(path->nodes[0]);
343 if (path->slots[0] >= nritems - 1) {
344 ret = btrfs_next_leaf(root, path);
Yanb56baf52007-10-29 12:01:05 -0400345 if (ret == 1)
Chris Masonf578d4b2007-10-25 15:42:56 -0400346 found_next = 1;
Yanb56baf52007-10-29 12:01:05 -0400347 if (ret != 0)
Chris Masonf578d4b2007-10-25 15:42:56 -0400348 goto insert;
Yanb56baf52007-10-29 12:01:05 -0400349 slot = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400350 }
351 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
352 if (found_key.objectid != objectid ||
353 found_key.type != BTRFS_CSUM_ITEM_KEY) {
354 found_next = 1;
355 goto insert;
356 }
357 next_offset = found_key.offset;
358 found_next = 1;
Chris Masona429e512007-04-18 16:15:28 -0400359 goto insert;
360 }
361
362 /*
363 * at this point, we know the tree has an item, but it isn't big
364 * enough yet to put our csum in. Grow it
365 */
366 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400367 ret = btrfs_search_slot(trans, root, &file_key, path,
Chris Mason509659c2007-05-10 12:36:17 -0400368 BTRFS_CRC32_SIZE, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400369 if (ret < 0)
370 goto fail;
371 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400372 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400373 }
374 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400375 goto insert;
376 }
377 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400378 leaf = path->nodes[0];
379 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason6567e832007-04-16 09:22:45 -0400380 csum_offset = (offset - found_key.offset) >>
381 root->fs_info->sb->s_blocksize_bits;
382 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
383 found_key.objectid != objectid ||
384 csum_offset >= MAX_CSUM_ITEMS(root)) {
Chris Mason6567e832007-04-16 09:22:45 -0400385 goto insert;
386 }
Chris Mason5f39d392007-10-15 16:14:19 -0400387 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
Chris Mason509659c2007-05-10 12:36:17 -0400388 BTRFS_CRC32_SIZE) {
389 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
Chris Mason5f39d392007-10-15 16:14:19 -0400390 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason3a686372007-05-24 13:35:57 -0400391 if (diff != BTRFS_CRC32_SIZE)
392 goto insert;
Chris Masona429e512007-04-18 16:15:28 -0400393 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400394 BUG_ON(ret);
395 goto csum;
396 }
397
398insert:
Chris Masona429e512007-04-18 16:15:28 -0400399 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400400 csum_offset = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400401 if (found_next) {
402 u64 tmp = min((u64)i_size_read(inode), next_offset);
Yanb56baf52007-10-29 12:01:05 -0400403 tmp -= offset & ~((u64)root->sectorsize -1);
Chris Masonf578d4b2007-10-25 15:42:56 -0400404 tmp >>= root->fs_info->sb->s_blocksize_bits;
405 tmp = max((u64)1, tmp);
406 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
407 ins_size = BTRFS_CRC32_SIZE * tmp;
408 } else {
409 ins_size = BTRFS_CRC32_SIZE;
410 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400411 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masonf578d4b2007-10-25 15:42:56 -0400412 ins_size);
Chris Mason54aa1f42007-06-22 14:16:25 -0400413 if (ret < 0)
414 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400415 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400416 WARN_ON(1);
Chris Masonf254e522007-03-29 15:15:27 -0400417 goto fail;
Chris Masona429e512007-04-18 16:15:28 -0400418 }
Chris Mason6567e832007-04-16 09:22:45 -0400419csum:
Chris Mason5f39d392007-10-15 16:14:19 -0400420 leaf = path->nodes[0];
421 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Masonf254e522007-03-29 15:15:27 -0400422 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400423 item = (struct btrfs_csum_item *)((unsigned char *)item +
424 csum_offset * BTRFS_CRC32_SIZE);
Chris Masonb18c6682007-04-17 13:26:50 -0400425found:
Chris Mason065631f2008-02-20 12:07:25 -0500426 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
427 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
428 btrfs_item_size_nr(leaf, path->slots[0]));
Chris Mason6e92f5e2008-02-20 12:07:25 -0500429 eb_token = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400430next_sector:
Chris Masonaadfeb62008-01-29 09:10:27 -0500431
Chris Mason6e92f5e2008-02-20 12:07:25 -0500432 if (!eb_token ||
433 (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
434 int err;
435
436 if (eb_token)
Chris Masoneb209782008-02-21 09:30:08 -0500437 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500438 eb_token = NULL;
439 err = map_private_extent_buffer(leaf, (unsigned long)item,
440 BTRFS_CRC32_SIZE,
441 &eb_token, &eb_map,
Chris Masoneb209782008-02-21 09:30:08 -0500442 &map_start, &map_len, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500443 if (err)
444 eb_token = NULL;
445 }
446 if (eb_token) {
447 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
Chris Masone6dcd2d2008-07-17 12:53:50 -0400448 &sector_sum->sum, BTRFS_CRC32_SIZE);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500449 } else {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400450 write_extent_buffer(leaf, &sector_sum->sum,
451 (unsigned long)item, BTRFS_CRC32_SIZE);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500452 }
Chris Mason7f3c74f2008-07-18 12:01:11 -0400453
Chris Masone6dcd2d2008-07-17 12:53:50 -0400454 total_bytes += root->sectorsize;
455 sector_sum++;
456 if (total_bytes < sums->len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500457 item = (struct btrfs_csum_item *)((char *)item +
458 BTRFS_CRC32_SIZE);
Chris Mason2e1a9922008-02-20 15:44:32 -0500459 if (item < item_end && offset + PAGE_CACHE_SIZE ==
Chris Masone6dcd2d2008-07-17 12:53:50 -0400460 sector_sum->offset) {
461 offset = sector_sum->offset;
462 goto next_sector;
Chris Mason2e1a9922008-02-20 15:44:32 -0500463 }
Chris Mason065631f2008-02-20 12:07:25 -0500464 }
Chris Mason6e92f5e2008-02-20 12:07:25 -0500465 if (eb_token) {
Chris Masoneb209782008-02-21 09:30:08 -0500466 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500467 eb_token = NULL;
468 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400469 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400470 if (total_bytes < sums->len) {
Chris Mason065631f2008-02-20 12:07:25 -0500471 btrfs_release_path(root, path);
472 goto again;
473 }
Chris Masonf254e522007-03-29 15:15:27 -0400474fail:
Chris Mason5caf2a02007-04-02 11:20:42 -0400475 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400476 return ret;
477}
478
Chris Mason1de037a2007-05-29 15:17:08 -0400479int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
480 struct btrfs_root *root, struct btrfs_path *path,
481 u64 isize)
482{
483 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400484 struct extent_buffer *leaf = path->nodes[0];
Chris Mason1de037a2007-05-29 15:17:08 -0400485 int slot = path->slots[0];
486 int ret;
487 u32 new_item_size;
488 u64 new_item_span;
489 u64 blocks;
490
Chris Mason5f39d392007-10-15 16:14:19 -0400491 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason1de037a2007-05-29 15:17:08 -0400492 if (isize <= key.offset)
493 return 0;
494 new_item_span = isize - key.offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400495 blocks = (new_item_span + root->sectorsize - 1) >>
Chris Mason84f54cf2007-06-12 07:43:08 -0400496 root->fs_info->sb->s_blocksize_bits;
Chris Mason1de037a2007-05-29 15:17:08 -0400497 new_item_size = blocks * BTRFS_CRC32_SIZE;
Chris Mason5f39d392007-10-15 16:14:19 -0400498 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
Chris Mason1de037a2007-05-29 15:17:08 -0400499 return 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400500 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
Chris Mason1de037a2007-05-29 15:17:08 -0400501 BUG_ON(ret);
502 return ret;
503}