]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/btrfs/file-item.c
Btrfs: Fix streaming read performance with checksumming on
[linux-2.6-omap-h63xx.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 disk_offset, u64 disk_num_bytes,
34                              u64 num_bytes, u64 offset)
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, disk_offset);
57         btrfs_set_file_extent_disk_num_bytes(leaf, item, disk_num_bytes);
58         btrfs_set_file_extent_offset(leaf, item, offset);
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_lookup_bio_sums(struct btrfs_root *root, struct inode *inode,
138                           struct bio *bio)
139 {
140         u32 sum;
141         struct bio_vec *bvec = bio->bi_io_vec;
142         int bio_index = 0;
143         u64 offset;
144         u64 item_start_offset = 0;
145         u64 item_last_offset = 0;
146         u32 diff;
147         int ret;
148         struct btrfs_path *path;
149         struct btrfs_csum_item *item = NULL;
150         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
151
152         path = btrfs_alloc_path();
153         path->reada = 2;
154
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);
180                                 goto found;
181                         }
182                         btrfs_item_key_to_cpu(path->nodes[0], &found_key,
183                                               path->slots[0]);
184
185                         item_start_offset = found_key.offset;
186                         item_size = btrfs_item_size_nr(path->nodes[0],
187                                                        path->slots[0]);
188                         item_last_offset = item_start_offset +
189                                 (item_size / BTRFS_CRC32_SIZE) *
190                                 root->sectorsize;
191                         item = btrfs_item_ptr(path->nodes[0], path->slots[0],
192                                               struct btrfs_csum_item);
193                 }
194                 /*
195                  * this byte range must be able to fit inside
196                  * a single leaf so it will also fit inside a u32
197                  */
198                 diff = offset - item_start_offset;
199                 diff = diff / root->sectorsize;
200                 diff = diff * BTRFS_CRC32_SIZE;
201
202                 read_extent_buffer(path->nodes[0], &sum,
203                                    (unsigned long)item + diff,
204                                    BTRFS_CRC32_SIZE);
205 found:
206                 set_state_private(io_tree, offset, sum);
207                 bio_index++;
208                 bvec++;
209         }
210         btrfs_free_path(path);
211         return 0;
212 }
213
214 int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
215                        struct bio *bio)
216 {
217         struct btrfs_ordered_sum *sums;
218         struct btrfs_sector_sum *sector_sum;
219         struct btrfs_ordered_extent *ordered;
220         char *data;
221         struct bio_vec *bvec = bio->bi_io_vec;
222         int bio_index = 0;
223         unsigned long total_bytes = 0;
224         unsigned long this_sum_bytes = 0;
225         u64 offset;
226
227         WARN_ON(bio->bi_vcnt <= 0);
228         sums = kzalloc(btrfs_ordered_sum_size(root, bio->bi_size), GFP_NOFS);
229         if (!sums)
230                 return -ENOMEM;
231
232         sector_sum = sums->sums;
233         sums->file_offset = page_offset(bvec->bv_page) + bvec->bv_offset;
234         sums->len = bio->bi_size;
235         INIT_LIST_HEAD(&sums->list);
236         ordered = btrfs_lookup_ordered_extent(inode, sums->file_offset);
237         BUG_ON(!ordered);
238
239         while(bio_index < bio->bi_vcnt) {
240                 offset = page_offset(bvec->bv_page) + bvec->bv_offset;
241                 if (offset >= ordered->file_offset + ordered->len ||
242                     offset < ordered->file_offset) {
243                         unsigned long bytes_left;
244                         sums->len = this_sum_bytes;
245                         this_sum_bytes = 0;
246                         btrfs_add_ordered_sum(inode, ordered, sums);
247                         btrfs_put_ordered_extent(ordered);
248
249                         bytes_left = bio->bi_size - total_bytes;
250
251                         sums = kzalloc(btrfs_ordered_sum_size(root, bytes_left),
252                                        GFP_NOFS);
253                         BUG_ON(!sums);
254                         sector_sum = sums->sums;
255                         sums->len = bytes_left;
256                         sums->file_offset = offset;
257                         ordered = btrfs_lookup_ordered_extent(inode,
258                                                       sums->file_offset);
259                         BUG_ON(!ordered);
260                 }
261
262                 data = kmap_atomic(bvec->bv_page, KM_USER0);
263                 sector_sum->sum = ~(u32)0;
264                 sector_sum->sum = btrfs_csum_data(root,
265                                                   data + bvec->bv_offset,
266                                                   sector_sum->sum,
267                                                   bvec->bv_len);
268                 kunmap_atomic(data, KM_USER0);
269                 btrfs_csum_final(sector_sum->sum,
270                                  (char *)&sector_sum->sum);
271                 sector_sum->offset = page_offset(bvec->bv_page) +
272                         bvec->bv_offset;
273
274                 sector_sum++;
275                 bio_index++;
276                 total_bytes += bvec->bv_len;
277                 this_sum_bytes += bvec->bv_len;
278                 bvec++;
279         }
280         this_sum_bytes = 0;
281         btrfs_add_ordered_sum(inode, ordered, sums);
282         btrfs_put_ordered_extent(ordered);
283         return 0;
284 }
285
286 int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
287                            struct btrfs_root *root, struct inode *inode,
288                            struct btrfs_ordered_sum *sums)
289 {
290         u64 objectid = inode->i_ino;
291         u64 offset;
292         int ret;
293         struct btrfs_key file_key;
294         struct btrfs_key found_key;
295         u64 next_offset;
296         u64 total_bytes = 0;
297         int found_next;
298         struct btrfs_path *path;
299         struct btrfs_csum_item *item;
300         struct btrfs_csum_item *item_end;
301         struct extent_buffer *leaf = NULL;
302         u64 csum_offset;
303         struct btrfs_sector_sum *sector_sum;
304         u32 nritems;
305         u32 ins_size;
306         char *eb_map;
307         char *eb_token;
308         unsigned long map_len;
309         unsigned long map_start;
310
311         path = btrfs_alloc_path();
312         BUG_ON(!path);
313         sector_sum = sums->sums;
314 again:
315         next_offset = (u64)-1;
316         found_next = 0;
317         offset = sector_sum->offset;
318         file_key.objectid = objectid;
319         file_key.offset = offset;
320         btrfs_set_key_type(&file_key, BTRFS_CSUM_ITEM_KEY);
321
322         item = btrfs_lookup_csum(trans, root, path, objectid, offset, 1);
323         if (!IS_ERR(item)) {
324                 leaf = path->nodes[0];
325                 goto found;
326         }
327         ret = PTR_ERR(item);
328         if (ret == -EFBIG) {
329                 u32 item_size;
330                 /* we found one, but it isn't big enough yet */
331                 leaf = path->nodes[0];
332                 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
333                 if ((item_size / BTRFS_CRC32_SIZE) >= MAX_CSUM_ITEMS(root)) {
334                         /* already at max size, make a new one */
335                         goto insert;
336                 }
337         } else {
338                 int slot = path->slots[0] + 1;
339                 /* we didn't find a csum item, insert one */
340                 nritems = btrfs_header_nritems(path->nodes[0]);
341                 if (path->slots[0] >= nritems - 1) {
342                         ret = btrfs_next_leaf(root, path);
343                         if (ret == 1)
344                                 found_next = 1;
345                         if (ret != 0)
346                                 goto insert;
347                         slot = 0;
348                 }
349                 btrfs_item_key_to_cpu(path->nodes[0], &found_key, slot);
350                 if (found_key.objectid != objectid ||
351                     found_key.type != BTRFS_CSUM_ITEM_KEY) {
352                         found_next = 1;
353                         goto insert;
354                 }
355                 next_offset = found_key.offset;
356                 found_next = 1;
357                 goto insert;
358         }
359
360         /*
361          * at this point, we know the tree has an item, but it isn't big
362          * enough yet to put our csum in.  Grow it
363          */
364         btrfs_release_path(root, path);
365         ret = btrfs_search_slot(trans, root, &file_key, path,
366                                 BTRFS_CRC32_SIZE, 1);
367         if (ret < 0)
368                 goto fail;
369         if (ret == 0) {
370                 BUG();
371         }
372         if (path->slots[0] == 0) {
373                 goto insert;
374         }
375         path->slots[0]--;
376         leaf = path->nodes[0];
377         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
378         csum_offset = (offset - found_key.offset) >>
379                         root->fs_info->sb->s_blocksize_bits;
380         if (btrfs_key_type(&found_key) != BTRFS_CSUM_ITEM_KEY ||
381             found_key.objectid != objectid ||
382             csum_offset >= MAX_CSUM_ITEMS(root)) {
383                 goto insert;
384         }
385         if (csum_offset >= btrfs_item_size_nr(leaf, path->slots[0]) /
386             BTRFS_CRC32_SIZE) {
387                 u32 diff = (csum_offset + 1) * BTRFS_CRC32_SIZE;
388                 diff = diff - btrfs_item_size_nr(leaf, path->slots[0]);
389                 if (diff != BTRFS_CRC32_SIZE)
390                         goto insert;
391                 ret = btrfs_extend_item(trans, root, path, diff);
392                 BUG_ON(ret);
393                 goto csum;
394         }
395
396 insert:
397         btrfs_release_path(root, path);
398         csum_offset = 0;
399         if (found_next) {
400                 u64 tmp = min((u64)i_size_read(inode), next_offset);
401                 tmp -= offset & ~((u64)root->sectorsize -1);
402                 tmp >>= root->fs_info->sb->s_blocksize_bits;
403                 tmp = max((u64)1, tmp);
404                 tmp = min(tmp, (u64)MAX_CSUM_ITEMS(root));
405                 ins_size = BTRFS_CRC32_SIZE * tmp;
406         } else {
407                 ins_size = BTRFS_CRC32_SIZE;
408         }
409         ret = btrfs_insert_empty_item(trans, root, path, &file_key,
410                                       ins_size);
411         if (ret < 0)
412                 goto fail;
413         if (ret != 0) {
414                 WARN_ON(1);
415                 goto fail;
416         }
417 csum:
418         leaf = path->nodes[0];
419         item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
420         ret = 0;
421         item = (struct btrfs_csum_item *)((unsigned char *)item +
422                                           csum_offset * BTRFS_CRC32_SIZE);
423 found:
424         item_end = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_csum_item);
425         item_end = (struct btrfs_csum_item *)((unsigned char *)item_end +
426                                       btrfs_item_size_nr(leaf, path->slots[0]));
427         eb_token = NULL;
428 next_sector:
429
430         if (!eb_token ||
431            (unsigned long)item  + BTRFS_CRC32_SIZE >= map_start + map_len) {
432                 int err;
433
434                 if (eb_token)
435                         unmap_extent_buffer(leaf, eb_token, KM_USER1);
436                 eb_token = NULL;
437                 err = map_private_extent_buffer(leaf, (unsigned long)item,
438                                                 BTRFS_CRC32_SIZE,
439                                                 &eb_token, &eb_map,
440                                                 &map_start, &map_len, KM_USER1);
441                 if (err)
442                         eb_token = NULL;
443         }
444         if (eb_token) {
445                 memcpy(eb_token + ((unsigned long)item & (PAGE_CACHE_SIZE - 1)),
446                        &sector_sum->sum, BTRFS_CRC32_SIZE);
447         } else {
448                 write_extent_buffer(leaf, &sector_sum->sum,
449                                     (unsigned long)item, BTRFS_CRC32_SIZE);
450         }
451
452         total_bytes += root->sectorsize;
453         sector_sum++;
454         if (total_bytes < sums->len) {
455                 item = (struct btrfs_csum_item *)((char *)item +
456                                                   BTRFS_CRC32_SIZE);
457                 if (item < item_end && offset + PAGE_CACHE_SIZE ==
458                     sector_sum->offset) {
459                             offset = sector_sum->offset;
460                         goto next_sector;
461                 }
462         }
463         if (eb_token) {
464                 unmap_extent_buffer(leaf, eb_token, KM_USER1);
465                 eb_token = NULL;
466         }
467         btrfs_mark_buffer_dirty(path->nodes[0]);
468         if (total_bytes < sums->len) {
469                 btrfs_release_path(root, path);
470                 goto again;
471         }
472 fail:
473         btrfs_free_path(path);
474         return ret;
475 }
476
477 int btrfs_csum_truncate(struct btrfs_trans_handle *trans,
478                         struct btrfs_root *root, struct btrfs_path *path,
479                         u64 isize)
480 {
481         struct btrfs_key key;
482         struct extent_buffer *leaf = path->nodes[0];
483         int slot = path->slots[0];
484         int ret;
485         u32 new_item_size;
486         u64 new_item_span;
487         u64 blocks;
488
489         btrfs_item_key_to_cpu(leaf, &key, slot);
490         if (isize <= key.offset)
491                 return 0;
492         new_item_span = isize - key.offset;
493         blocks = (new_item_span + root->sectorsize - 1) >>
494                 root->fs_info->sb->s_blocksize_bits;
495         new_item_size = blocks * BTRFS_CRC32_SIZE;
496         if (new_item_size >= btrfs_item_size_nr(leaf, slot))
497                 return 0;
498         ret = btrfs_truncate_item(trans, root, path, new_item_size, 1);
499         BUG_ON(ret);
500         return ret;
501 }