blob: f4d3fa71bc419c3c5ef569eda6ded776ba3e605e [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,
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,
77 u64 objectid, u64 offset,
78 int cow)
Chris Mason6567e832007-04-16 09:22:45 -040079{
80 int ret;
81 struct btrfs_key file_key;
82 struct btrfs_key found_key;
83 struct btrfs_csum_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -040084 struct extent_buffer *leaf;
Chris Mason6567e832007-04-16 09:22:45 -040085 u64 csum_offset = 0;
Chris Masona429e512007-04-18 16:15:28 -040086 int csums_in_item;
Chris Mason6567e832007-04-16 09:22:45 -040087
88 file_key.objectid = objectid;
89 file_key.offset = offset;
Chris Mason6567e832007-04-16 09:22:45 -040090 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masonb18c6682007-04-17 13:26:50 -040091 ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
Chris Mason6567e832007-04-16 09:22:45 -040092 if (ret < 0)
93 goto fail;
Chris Mason5f39d392007-10-15 16:14:19 -040094 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -040095 if (ret > 0) {
96 ret = 1;
Chris Mason70b2bef2007-04-17 15:39:32 -040097 if (path->slots[0] == 0)
Chris Mason6567e832007-04-16 09:22:45 -040098 goto fail;
99 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400100 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason6567e832007-04-16 09:22:45 -0400101 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
102 found_key.objectid != objectid) {
103 goto fail;
104 }
105 csum_offset = (offset - found_key.offset) >>
106 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]);
Chris Mason509659c2007-05-10 12:36:17 -0400108 csums_in_item /= BTRFS_CRC32_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 +
117 csum_offset * BTRFS_CRC32_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,
144 struct bio *bio)
145{
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;
152 u32 diff;
153 int ret;
154 struct btrfs_path *path;
155 struct btrfs_csum_item *item = NULL;
156 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
157
158 path = btrfs_alloc_path();
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400159 if (bio->bi_size > PAGE_CACHE_SIZE * 8)
160 path->reada = 2;
Chris Mason61b49442008-07-31 15:42:53 -0400161
162 WARN_ON(bio->bi_vcnt <= 0);
163
164 while(bio_index < bio->bi_vcnt) {
165 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
166 ret = btrfs_find_ordered_sum(inode, offset, &sum);
167 if (ret == 0)
168 goto found;
169
170 if (!item || offset < item_start_offset ||
171 offset >= item_last_offset) {
172 struct btrfs_key found_key;
173 u32 item_size;
174
175 if (item)
176 btrfs_release_path(root, path);
177 item = btrfs_lookup_csum(NULL, root, path,
178 inode->i_ino, offset, 0);
179 if (IS_ERR(item)) {
180 ret = PTR_ERR(item);
181 if (ret == -ENOENT || ret == -EFBIG)
182 ret = 0;
183 sum = 0;
184 printk("no csum found for inode %lu start "
185 "%llu\n", inode->i_ino,
186 (unsigned long long)offset);
Chris Mason6dab8152008-08-04 08:35:53 -0400187 item = NULL;
Chris Mason61b49442008-07-31 15:42:53 -0400188 goto found;
189 }
190 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
191 path->slots[0]);
192
193 item_start_offset = found_key.offset;
194 item_size = btrfs_item_size_nr(path->nodes[0],
195 path->slots[0]);
196 item_last_offset = item_start_offset +
197 (item_size / BTRFS_CRC32_SIZE) *
198 root->sectorsize;
199 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
200 struct btrfs_csum_item);
201 }
202 /*
203 * this byte range must be able to fit inside
204 * a single leaf so it will also fit inside a u32
205 */
206 diff = offset - item_start_offset;
207 diff = diff / root->sectorsize;
208 diff = diff * BTRFS_CRC32_SIZE;
209
210 read_extent_buffer(path->nodes[0], &sum,
Chris Mason3de9d6b2008-08-04 23:17:27 -0400211 ((unsigned long)item) + diff,
Chris Mason61b49442008-07-31 15:42:53 -0400212 BTRFS_CRC32_SIZE);
213found:
214 set_state_private(io_tree, offset, sum);
215 bio_index++;
216 bvec++;
217 }
218 btrfs_free_path(path);
219 return 0;
220}
221
Chris Masonc8b97812008-10-29 14:49:59 -0400222int btrfs_csum_file_bytes(struct btrfs_root *root, struct inode *inode,
223 u64 start, unsigned long len)
224{
225 struct btrfs_ordered_sum *sums;
226 struct btrfs_sector_sum *sector_sum;
227 struct btrfs_ordered_extent *ordered;
228 char *data;
229 struct page *page;
230 unsigned long total_bytes = 0;
231 unsigned long this_sum_bytes = 0;
232
233 sums = kzalloc(btrfs_ordered_sum_size(root, len), GFP_NOFS);
234 if (!sums)
235 return -ENOMEM;
236
237 sector_sum = sums->sums;
238 sums->file_offset = start;
239 sums->len = len;
240 INIT_LIST_HEAD(&sums->list);
241 ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset);
242 BUG_ON(!ordered);
243
244 while(len > 0) {
245 if (start >= ordered->file_offset + ordered->len ||
246 start < ordered->file_offset) {
247 sums->len = this_sum_bytes;
248 this_sum_bytes = 0;
249 btrfs_add_ordered_sum(inode, ordered, sums);
250 btrfs_put_ordered_extent(ordered);
251
252 sums = kzalloc(btrfs_ordered_sum_size(root, len),
253 GFP_NOFS);
254 BUG_ON(!sums);
255 sector_sum = sums->sums;
256 sums->len = len;
257 sums->file_offset = start;
258 ordered = btrfs_lookup_ordered_extent(inode,
259 sums->file_offset);
260 BUG_ON(!ordered);
261 }
262
263 page = find_get_page(inode->i_mapping,
264 start >> PAGE_CACHE_SHIFT);
265
266 data = kmap_atomic(page, KM_USER0);
267 sector_sum->sum = ~(u32)0;
268 sector_sum->sum = btrfs_csum_data(root, data, sector_sum->sum,
269 PAGE_CACHE_SIZE);
270 kunmap_atomic(data, KM_USER0);
271 btrfs_csum_final(sector_sum->sum,
272 (char *)&sector_sum->sum);
273 sector_sum->offset = page_offset(page);
274 page_cache_release(page);
275
276 sector_sum++;
277 total_bytes += PAGE_CACHE_SIZE;
278 this_sum_bytes += PAGE_CACHE_SIZE;
279 start += PAGE_CACHE_SIZE;
280
281 WARN_ON(len < PAGE_CACHE_SIZE);
282 len -= PAGE_CACHE_SIZE;
283 }
284 btrfs_add_ordered_sum(inode, ordered, sums);
285 btrfs_put_ordered_extent(ordered);
286 return 0;
287}
288
Chris Mason3edf7d32008-07-18 06:17:13 -0400289int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
290 struct bio *bio)
Chris Masone0156402008-04-16 11:15:20 -0400291{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400292 struct btrfs_ordered_sum *sums;
293 struct btrfs_sector_sum *sector_sum;
Chris Mason3edf7d32008-07-18 06:17:13 -0400294 struct btrfs_ordered_extent *ordered;
Chris Masone0156402008-04-16 11:15:20 -0400295 char *data;
296 struct bio_vec *bvec = bio->bi_io_vec;
297 int bio_index = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400298 unsigned long total_bytes = 0;
299 unsigned long this_sum_bytes = 0;
300 u64 offset;
Chris Masone0156402008-04-16 11:15:20 -0400301
Chris Masone6dcd2d2008-07-17 12:53:50 -0400302 WARN_ON(bio->bi_vcnt <= 0);
303 sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
Chris Masone0156402008-04-16 11:15:20 -0400304 if (!sums)
305 return -ENOMEM;
Chris Mason3edf7d32008-07-18 06:17:13 -0400306
Chris Masoned98b562008-07-22 23:06:42 -0400307 sector_sum = sums->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400308 sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400309 sums->len = bio->bi_size;
310 INIT_LIST_HEAD(&sums->list);
Chris Mason3edf7d32008-07-18 06:17:13 -0400311 ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset);
312 BUG_ON(!ordered);
Chris Masone0156402008-04-16 11:15:20 -0400313
314 while(bio_index < bio->bi_vcnt) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400315 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
Chris Masone5a22172008-07-18 20:42:20 -0400316 if (offset >= ordered->file_offset + ordered->len ||
317 offset < ordered->file_offset) {
Chris Mason3edf7d32008-07-18 06:17:13 -0400318 unsigned long bytes_left;
319 sums->len = this_sum_bytes;
320 this_sum_bytes = 0;
321 btrfs_add_ordered_sum(inode, ordered, sums);
322 btrfs_put_ordered_extent(ordered);
323
324 bytes_left = bio->bi_size - total_bytes;
325
326 sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
327 GFP_NOFS);
328 BUG_ON(!sums);
Chris Masoned98b562008-07-22 23:06:42 -0400329 sector_sum = sums->sums;
Chris Mason3edf7d32008-07-18 06:17:13 -0400330 sums->len = bytes_left;
331 sums->file_offset = offset;
332 ordered = btrfs_lookup_ordered_extent(inode,
333 sums->file_offset);
334 BUG_ON(!ordered);
335 }
336
Chris Masone0156402008-04-16 11:15:20 -0400337 data = kmap_atomic(bvec->bv_page, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400338 sector_sum->sum = ~(u32)0;
339 sector_sum->sum = btrfs_csum_data(root,
340 data + bvec->bv_offset,
341 sector_sum->sum,
342 bvec->bv_len);
Chris Masone0156402008-04-16 11:15:20 -0400343 kunmap_atomic(data, KM_USER0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400344 btrfs_csum_final(sector_sum->sum,
345 (char *)&sector_sum->sum);
346 sector_sum->offset = page_offset(bvec->bv_page) +
347 bvec->bv_offset;
Chris Masoned98b562008-07-22 23:06:42 -0400348
Chris Masone6dcd2d2008-07-17 12:53:50 -0400349 sector_sum++;
Chris Masone0156402008-04-16 11:15:20 -0400350 bio_index++;
Chris Mason3edf7d32008-07-18 06:17:13 -0400351 total_bytes += bvec->bv_len;
352 this_sum_bytes += bvec->bv_len;
Chris Masone0156402008-04-16 11:15:20 -0400353 bvec++;
354 }
Chris Masoned98b562008-07-22 23:06:42 -0400355 this_sum_bytes = 0;
Chris Mason3edf7d32008-07-18 06:17:13 -0400356 btrfs_add_ordered_sum(inode, ordered, sums);
357 btrfs_put_ordered_extent(ordered);
Chris Masone0156402008-04-16 11:15:20 -0400358 return 0;
359}
360
Chris Mason065631f2008-02-20 12:07:25 -0500361int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
362 struct btrfs_root *root, struct inode *inode,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400363 struct btrfs_ordered_sum *sums)
Chris Masonf254e522007-03-29 15:15:27 -0400364{
Chris Mason065631f2008-02-20 12:07:25 -0500365 u64 objectid = inode->i_ino;
366 u64 offset;
Chris Masonf254e522007-03-29 15:15:27 -0400367 int ret;
368 struct btrfs_key file_key;
Chris Mason6567e832007-04-16 09:22:45 -0400369 struct btrfs_key found_key;
Chris Mason065631f2008-02-20 12:07:25 -0500370 u64 next_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400371 u64 total_bytes = 0;
Chris Mason065631f2008-02-20 12:07:25 -0500372 int found_next;
Chris Mason5caf2a02007-04-02 11:20:42 -0400373 struct btrfs_path *path;
Chris Masonf254e522007-03-29 15:15:27 -0400374 struct btrfs_csum_item *item;
Chris Mason065631f2008-02-20 12:07:25 -0500375 struct btrfs_csum_item *item_end;
Chris Masonff79f812007-10-15 16:22:25 -0400376 struct extent_buffer *leaf = NULL;
Chris Mason6567e832007-04-16 09:22:45 -0400377 u64 csum_offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400378 struct btrfs_sector_sum *sector_sum;
Chris Masonf578d4b2007-10-25 15:42:56 -0400379 u32 nritems;
380 u32 ins_size;
Chris Mason6e92f5e2008-02-20 12:07:25 -0500381 char *eb_map;
382 char *eb_token;
383 unsigned long map_len;
384 unsigned long map_start;
385
Chris Mason5caf2a02007-04-02 11:20:42 -0400386 path = btrfs_alloc_path();
387 BUG_ON(!path);
Chris Masoned98b562008-07-22 23:06:42 -0400388 sector_sum = sums->sums;
Chris Mason065631f2008-02-20 12:07:25 -0500389again:
390 next_offset = (u64)-1;
391 found_next = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400392 offset = sector_sum->offset;
Chris Masonf254e522007-03-29 15:15:27 -0400393 file_key.objectid = objectid;
394 file_key.offset = offset;
Chris Masonf254e522007-03-29 15:15:27 -0400395 btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
Chris Masona429e512007-04-18 16:15:28 -0400396
Chris Mason53863232008-08-15 15:34:18 -0400397 mutex_lock(&BTRFS_I(inode)->csum_mutex);
Chris Masona429e512007-04-18 16:15:28 -0400398 item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
Chris Masonff79f812007-10-15 16:22:25 -0400399 if (!IS_ERR(item)) {
400 leaf = path->nodes[0];
Chris Mason639cb582008-08-28 06:15:25 -0400401 ret = 0;
Chris Masona429e512007-04-18 16:15:28 -0400402 goto found;
Chris Masonff79f812007-10-15 16:22:25 -0400403 }
Chris Masona429e512007-04-18 16:15:28 -0400404 ret = PTR_ERR(item);
405 if (ret == -EFBIG) {
406 u32 item_size;
407 /* we found one, but it isn't big enough yet */
Chris Mason5f39d392007-10-15 16:14:19 -0400408 leaf = path->nodes[0];
409 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason509659c2007-05-10 12:36:17 -0400410 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
Chris Masona429e512007-04-18 16:15:28 -0400411 /* already at max size, make a new one */
412 goto insert;
413 }
414 } else {
Chris Masonf578d4b2007-10-25 15:42:56 -0400415 int slot = path->slots[0] + 1;
Chris Masona429e512007-04-18 16:15:28 -0400416 /* we didn't find a csum item, insert one */
Chris Masonf578d4b2007-10-25 15:42:56 -0400417 nritems = btrfs_header_nritems(path->nodes[0]);
418 if (path->slots[0] >= nritems - 1) {
419 ret = btrfs_next_leaf(root, path);
Yanb56baf52007-10-29 12:01:05 -0400420 if (ret == 1)
Chris Masonf578d4b2007-10-25 15:42:56 -0400421 found_next = 1;
Yanb56baf52007-10-29 12:01:05 -0400422 if (ret != 0)
Chris Masonf578d4b2007-10-25 15:42:56 -0400423 goto insert;
Yanb56baf52007-10-29 12:01:05 -0400424 slot = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400425 }
426 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
427 if (found_key.objectid != objectid ||
428 found_key.type != BTRFS_CSUM_ITEM_KEY) {
429 found_next = 1;
430 goto insert;
431 }
432 next_offset = found_key.offset;
433 found_next = 1;
Chris Masona429e512007-04-18 16:15:28 -0400434 goto insert;
435 }
436
437 /*
438 * at this point, we know the tree has an item, but it isn't big
439 * enough yet to put our csum in. Grow it
440 */
441 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400442 ret = btrfs_search_slot(trans, root, &file_key, path,
Chris Mason509659c2007-05-10 12:36:17 -0400443 BTRFS_CRC32_SIZE, 1);
Chris Mason6567e832007-04-16 09:22:45 -0400444 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400445 goto fail_unlock;
Chris Mason6567e832007-04-16 09:22:45 -0400446 if (ret == 0) {
Chris Masonb18c6682007-04-17 13:26:50 -0400447 BUG();
Chris Mason6567e832007-04-16 09:22:45 -0400448 }
449 if (path->slots[0] == 0) {
Chris Mason6567e832007-04-16 09:22:45 -0400450 goto insert;
451 }
452 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400453 leaf = path->nodes[0];
454 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason6567e832007-04-16 09:22:45 -0400455 csum_offset = (offset - found_key.offset) >>
456 root->fs_info->sb->s_blocksize_bits;
457 if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
458 found_key.objectid != objectid ||
459 csum_offset >= MAX_CSUM_ITEMS(root)) {
Chris Mason6567e832007-04-16 09:22:45 -0400460 goto insert;
461 }
Chris Mason5f39d392007-10-15 16:14:19 -0400462 if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
Chris Mason509659c2007-05-10 12:36:17 -0400463 BTRFS_CRC32_SIZE) {
464 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
Chris Mason5f39d392007-10-15 16:14:19 -0400465 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
Chris Mason3a686372007-05-24 13:35:57 -0400466 if (diff != BTRFS_CRC32_SIZE)
467 goto insert;
Chris Masona429e512007-04-18 16:15:28 -0400468 ret = btrfs_extend_item(trans, root, path, diff);
Chris Mason6567e832007-04-16 09:22:45 -0400469 BUG_ON(ret);
470 goto csum;
471 }
472
473insert:
Chris Masona429e512007-04-18 16:15:28 -0400474 btrfs_release_path(root, path);
Chris Mason6567e832007-04-16 09:22:45 -0400475 csum_offset = 0;
Chris Masonf578d4b2007-10-25 15:42:56 -0400476 if (found_next) {
477 u64 tmp = min((u64)i_size_read(inode), next_offset);
Yanb56baf52007-10-29 12:01:05 -0400478 tmp -= offset & ~((u64)root->sectorsize -1);
Chris Masonf578d4b2007-10-25 15:42:56 -0400479 tmp >>= root->fs_info->sb->s_blocksize_bits;
480 tmp = max((u64)1, tmp);
481 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
482 ins_size = BTRFS_CRC32_SIZE * tmp;
483 } else {
484 ins_size = BTRFS_CRC32_SIZE;
485 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400486 ret = btrfs_insert_empty_item(trans, root, path, &file_key,
Chris Masonf578d4b2007-10-25 15:42:56 -0400487 ins_size);
Chris Mason54aa1f42007-06-22 14:16:25 -0400488 if (ret < 0)
Chris Mason53863232008-08-15 15:34:18 -0400489 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400490 if (ret != 0) {
Chris Masona429e512007-04-18 16:15:28 -0400491 WARN_ON(1);
Chris Mason53863232008-08-15 15:34:18 -0400492 goto fail_unlock;
Chris Masona429e512007-04-18 16:15:28 -0400493 }
Chris Mason6567e832007-04-16 09:22:45 -0400494csum:
Chris Mason5f39d392007-10-15 16:14:19 -0400495 leaf = path->nodes[0];
496 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
Chris Masonf254e522007-03-29 15:15:27 -0400497 ret = 0;
Chris Mason509659c2007-05-10 12:36:17 -0400498 item = (struct btrfs_csum_item *)((unsigned char *)item +
499 csum_offset * BTRFS_CRC32_SIZE);
Chris Masonb18c6682007-04-17 13:26:50 -0400500found:
Chris Mason065631f2008-02-20 12:07:25 -0500501 item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
502 item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
503 btrfs_item_size_nr(leaf, path->slots[0]));
Chris Mason6e92f5e2008-02-20 12:07:25 -0500504 eb_token = NULL;
Chris Mason53863232008-08-15 15:34:18 -0400505 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
506 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400507next_sector:
Chris Masonaadfeb62008-01-29 09:10:27 -0500508
Chris Mason6e92f5e2008-02-20 12:07:25 -0500509 if (!eb_token ||
510 (unsigned long)item + BTRFS_CRC32_SIZE >= map_start + map_len) {
511 int err;
512
513 if (eb_token)
Chris Masoneb209782008-02-21 09:30:08 -0500514 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500515 eb_token = NULL;
516 err = map_private_extent_buffer(leaf, (unsigned long)item,
517 BTRFS_CRC32_SIZE,
518 &eb_token, &eb_map,
Chris Masoneb209782008-02-21 09:30:08 -0500519 &map_start, &map_len, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500520 if (err)
521 eb_token = NULL;
522 }
523 if (eb_token) {
524 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
Chris Masone6dcd2d2008-07-17 12:53:50 -0400525 &sector_sum->sum, BTRFS_CRC32_SIZE);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500526 } else {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400527 write_extent_buffer(leaf, &sector_sum->sum,
528 (unsigned long)item, BTRFS_CRC32_SIZE);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500529 }
Chris Mason7f3c74f2008-07-18 12:01:11 -0400530
Chris Masone6dcd2d2008-07-17 12:53:50 -0400531 total_bytes += root->sectorsize;
532 sector_sum++;
533 if (total_bytes < sums->len) {
Chris Mason6e92f5e2008-02-20 12:07:25 -0500534 item = (struct btrfs_csum_item *)((char *)item +
535 BTRFS_CRC32_SIZE);
Chris Mason2e1a9922008-02-20 15:44:32 -0500536 if (item < item_end && offset + PAGE_CACHE_SIZE ==
Chris Masone6dcd2d2008-07-17 12:53:50 -0400537 sector_sum->offset) {
538 offset = sector_sum->offset;
539 goto next_sector;
Chris Mason2e1a9922008-02-20 15:44:32 -0500540 }
Chris Mason065631f2008-02-20 12:07:25 -0500541 }
Chris Mason6e92f5e2008-02-20 12:07:25 -0500542 if (eb_token) {
Chris Masoneb209782008-02-21 09:30:08 -0500543 unmap_extent_buffer(leaf, eb_token, KM_USER1);
Chris Mason6e92f5e2008-02-20 12:07:25 -0500544 eb_token = NULL;
545 }
Chris Mason5caf2a02007-04-02 11:20:42 -0400546 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason53863232008-08-15 15:34:18 -0400547 cond_resched();
Chris Masone6dcd2d2008-07-17 12:53:50 -0400548 if (total_bytes < sums->len) {
Chris Mason065631f2008-02-20 12:07:25 -0500549 btrfs_release_path(root, path);
550 goto again;
551 }
Chris Mason53863232008-08-15 15:34:18 -0400552out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400553 btrfs_free_path(path);
Chris Masonf254e522007-03-29 15:15:27 -0400554 return ret;
Chris Mason53863232008-08-15 15:34:18 -0400555
556fail_unlock:
557 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
558 goto out;
Chris Masonf254e522007-03-29 15:15:27 -0400559}
560
Chris Mason1de037a2007-05-29 15:17:08 -0400561int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
562 struct btrfs_root *root, struct btrfs_path *path,
563 u64 isize)
564{
565 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400566 struct extent_buffer *leaf = path->nodes[0];
Chris Mason1de037a2007-05-29 15:17:08 -0400567 int slot = path->slots[0];
568 int ret;
569 u32 new_item_size;
570 u64 new_item_span;
571 u64 blocks;
572
Chris Mason5f39d392007-10-15 16:14:19 -0400573 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason1de037a2007-05-29 15:17:08 -0400574 if (isize <= key.offset)
575 return 0;
576 new_item_span = isize - key.offset;
Chris Mason5f39d392007-10-15 16:14:19 -0400577 blocks = (new_item_span + root->sectorsize - 1) >>
Chris Mason84f54cf2007-06-12 07:43:08 -0400578 root->fs_info->sb->s_blocksize_bits;
Chris Mason1de037a2007-05-29 15:17:08 -0400579 new_item_size = blocks * BTRFS_CRC32_SIZE;
Chris Mason5f39d392007-10-15 16:14:19 -0400580 if (new_item_size >= btrfs_item_size_nr(leaf, slot))
Chris Mason1de037a2007-05-29 15:17:08 -0400581 return 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400582 ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
Chris Mason1de037a2007-05-29 15:17:08 -0400583 BUG_ON(ret);
584 return ret;
585}