]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/btrfs/file-item.c
Btrfs: While doing checksums on bios, cache the extent_buffer mapping
[mv-sheeva.git] / fs / btrfs / file-item.c
1 /*
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
19 #include <linux/bio.h>
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include "ctree.h"
23 #include "disk-io.h"
24 #include "transaction.h"
25 #include "print-tree.h"
26
27 #define MAX_CSUM_ITEMS(r) ((((BTRFS_LEAF_DATA_SIZE(r) - \
28                                sizeof(struct btrfs_item) * 2) / \
29                                BTRFS_CRC32_SIZE) - 1))
30 int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
31                                struct btrfs_root *root,
32                                u64 objectid, u64 pos,
33                                u64 offset, u64 disk_num_bytes,
34                                u64 num_bytes)
35 {
36         int ret = 0;
37         struct btrfs_file_extent_item *item;
38         struct btrfs_key file_key;
39         struct btrfs_path *path;
40         struct extent_buffer *leaf;
41
42         path = btrfs_alloc_path();
43         BUG_ON(!path);
44         file_key.objectid = objectid;
45         file_key.offset = pos;
46         btrfs_set_key_type(&file_key, BTRFS_EXTENT_DATA_KEY);
47
48         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
49                                       sizeof(*item));
50         if (ret < 0)
51                 goto out;
52         BUG_ON(ret);
53         leaf = path->nodes[0];
54         item = btrfs_item_ptr(leaf, path->slots[0],
55                               struct btrfs_file_extent_item);
56         btrfs_set_file_extent_disk_bytenr(leaf, item, offset);
57         btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
58         btrfs_set_file_extent_offset(leaf, item, 0);
59         btrfs_set_file_extent_num_bytes(leaf, item, num_bytes);
60         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);
63 out:
64         btrfs_free_path(path);
65         return ret;
66 }
67
68 struct 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)
73 {
74         int ret;
75         struct btrfs_key file_key;
76         struct btrfs_key found_key;
77         struct btrfs_csum_item *item;
78         struct extent_buffer *leaf;
79         u64 csum_offset = 0;
80         int csums_in_item;
81
82         file_key.objectid = objectid;
83         file_key.offset = offset;
84         btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
85         ret = btrfs_search_slot(trans, root, &file_key, path, 0, cow);
86         if (ret < 0)
87                 goto fail;
88         leaf = path->nodes[0];
89         if (ret > 0) {
90                 ret = 1;
91                 if (path->slots[0] == 0)
92                         goto fail;
93                 path->slots[0]--;
94                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
95                 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;
101                 csums_in_item = btrfs_item_size_nr(leaf, path->slots[0]);
102                 csums_in_item /= BTRFS_CRC32_SIZE;
103
104                 if (csum_offset >= csums_in_item) {
105                         ret = -EFBIG;
106                         goto fail;
107                 }
108         }
109         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
110         item = (struct btrfs_csum_item *)((unsigned char *)item +
111                                           csum_offset * BTRFS_CRC32_SIZE);
112         return item;
113 fail:
114         if (ret > 0)
115                 ret = -ENOENT;
116         return ERR_PTR(ret);
117 }
118
119
120 int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
121                              struct btrfs_root *root,
122                              struct btrfs_path *path, u64 objectid,
123                              u64 offset, int mod)
124 {
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;
131         file_key.offset = offset;
132         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 }
136
137 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
138                            struct btrfs_root *root, struct inode *inode,
139                            struct bio *bio)
140 {
141         u64 objectid = inode->i_ino;
142         u64 offset;
143         int ret;
144         struct btrfs_key file_key;
145         struct btrfs_key found_key;
146         u64 next_offset;
147         int found_next;
148         struct btrfs_path *path;
149         struct btrfs_csum_item *item;
150         struct btrfs_csum_item *item_end;
151         struct extent_buffer *leaf = NULL;
152         u64 csum_offset;
153         u32 csum_result;
154         u32 nritems;
155         u32 ins_size;
156         int bio_index = 0;
157         struct bio_vec *bvec = bio->bi_io_vec;
158         char *data;
159         char *eb_map;
160         char *eb_token;
161         unsigned long map_len;
162         unsigned long map_start;
163
164
165         path = btrfs_alloc_path();
166         BUG_ON(!path);
167 again:
168         next_offset = (u64)-1;
169         found_next = 0;
170         offset = page_offset(bvec->bv_page) + bvec->bv_offset;
171         file_key.objectid = objectid;
172         file_key.offset = offset;
173         btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
174
175         item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
176         if (!IS_ERR(item)) {
177                 leaf = path->nodes[0];
178                 goto found;
179         }
180         ret = PTR_ERR(item);
181         if (ret == -EFBIG) {
182                 u32 item_size;
183                 /* we found one, but it isn't big enough yet */
184                 leaf = path->nodes[0];
185                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
186                 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
187                         /* already at max size, make a new one */
188                         goto insert;
189                 }
190         } else {
191                 int slot = path->slots[0] + 1;
192                 /* we didn't find a csum item, insert one */
193                 nritems = btrfs_header_nritems(path->nodes[0]);
194                 if (path->slots[0] >= nritems - 1) {
195                         ret = btrfs_next_leaf(root, path);
196                         if (ret == 1)
197                                 found_next = 1;
198                         if (ret != 0)
199                                 goto insert;
200                         slot = 0;
201                 }
202                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
203                 if (found_key.objectid != objectid ||
204                     found_key.type != BTRFS_CSUM_ITEM_KEY) {
205                         found_next = 1;
206                         goto insert;
207                 }
208                 next_offset = found_key.offset;
209                 found_next = 1;
210                 goto insert;
211         }
212
213         /*
214          * at this point, we know the tree has an item, but it isn't big
215          * enough yet to put our csum in.  Grow it
216          */
217         btrfs_release_path(root, path);
218         ret = btrfs_search_slot(trans, root, &file_key, path,
219                                 BTRFS_CRC32_SIZE, 1);
220         if (ret < 0)
221                 goto fail;
222         if (ret == 0) {
223                 BUG();
224         }
225         if (path->slots[0] == 0) {
226                 goto insert;
227         }
228         path->slots[0]--;
229         leaf = path->nodes[0];
230         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
231         csum_offset = (offset - found_key.offset) >>
232                         root->fs_info->sb->s_blocksize_bits;
233         if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
234             found_key.objectid != objectid ||
235             csum_offset >= MAX_CSUM_ITEMS(root)) {
236                 goto insert;
237         }
238         if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
239             BTRFS_CRC32_SIZE) {
240                 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
241                 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
242                 if (diff != BTRFS_CRC32_SIZE)
243                         goto insert;
244                 ret = btrfs_extend_item(trans, root, path, diff);
245                 BUG_ON(ret);
246                 goto csum;
247         }
248
249 insert:
250         btrfs_release_path(root, path);
251         csum_offset = 0;
252         if (found_next) {
253                 u64 tmp = min((u64)i_size_read(inode), next_offset);
254                 tmp -= offset & ~((u64)root->sectorsize -1);
255                 tmp >>= root->fs_info->sb->s_blocksize_bits;
256                 tmp = max((u64)1, tmp);
257                 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
258                 ins_size = BTRFS_CRC32_SIZE * tmp;
259         } else {
260                 ins_size = BTRFS_CRC32_SIZE;
261         }
262         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
263                                       ins_size);
264         if (ret < 0)
265                 goto fail;
266         if (ret != 0) {
267                 WARN_ON(1);
268                 goto fail;
269         }
270 csum:
271         leaf = path->nodes[0];
272         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
273         ret = 0;
274         item = (struct btrfs_csum_item *)((unsigned char *)item +
275                                           csum_offset * BTRFS_CRC32_SIZE);
276 found:
277         item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
278         item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
279                                       btrfs_item_size_nr(leaf, path->slots[0]));
280         eb_token = NULL;
281 next_bvec:
282         data = kmap_atomic(bvec->bv_page, KM_IRQ0);
283         csum_result = ~(u32)0;
284         csum_result = btrfs_csum_data(root, data + bvec->bv_offset,
285                                       csum_result, bvec->bv_len);
286         kunmap_atomic(data, KM_IRQ0);
287         btrfs_csum_final(csum_result, (char *)&csum_result);
288         if (csum_result == 0) {
289                 printk("csum result is 0 for inode %lu offset %Lu\n", inode->i_ino, offset);
290         }
291
292         if (!eb_token ||
293            (unsigned long)item  + BTRFS_CRC32_SIZE >= map_start + map_len) {
294                 int err;
295
296                 if (eb_token)
297                         unmap_extent_buffer(leaf, eb_token, KM_IRQ1);
298                 eb_token = NULL;
299                 err = map_private_extent_buffer(leaf, (unsigned long)item,
300                                                 BTRFS_CRC32_SIZE,
301                                                 &eb_token, &eb_map,
302                                                 &map_start, &map_len, KM_IRQ1);
303                 if (err)
304                         eb_token = NULL;
305         }
306         if (eb_token) {
307                 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
308                        &csum_result, BTRFS_CRC32_SIZE);
309         } else {
310                 write_extent_buffer(leaf, &csum_result, (unsigned long)item,
311                                     BTRFS_CRC32_SIZE);
312         }
313         bio_index++;
314         bvec++;
315         if (bio_index < bio->bi_vcnt) {
316                 item = (struct btrfs_csum_item *)((char *)item +
317                                                   BTRFS_CRC32_SIZE);
318                 if (item < item_end)
319                         goto next_bvec;
320         }
321         if (eb_token) {
322                 unmap_extent_buffer(leaf, eb_token, KM_IRQ1);
323                 eb_token = NULL;
324         }
325         btrfs_mark_buffer_dirty(path->nodes[0]);
326         if (bio_index < bio->bi_vcnt) {
327                 btrfs_release_path(root, path);
328                 goto again;
329         }
330 fail:
331         btrfs_free_path(path);
332         return ret;
333 }
334
335 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
336                         struct btrfs_root *root, struct btrfs_path *path,
337                         u64 isize)
338 {
339         struct btrfs_key key;
340         struct extent_buffer *leaf = path->nodes[0];
341         int slot = path->slots[0];
342         int ret;
343         u32 new_item_size;
344         u64 new_item_span;
345         u64 blocks;
346
347         btrfs_item_key_to_cpu(leaf, &key, slot);
348         if (isize <= key.offset)
349                 return 0;
350         new_item_span = isize - key.offset;
351         blocks = (new_item_span + root->sectorsize - 1) >>
352                 root->fs_info->sb->s_blocksize_bits;
353         new_item_size = blocks * BTRFS_CRC32_SIZE;
354         if (new_item_size >= btrfs_item_size_nr(leaf, slot))
355                 return 0;
356         ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
357         BUG_ON(ret);
358         return ret;
359 }
360