~ubuntu-branches/debian/jessie/btrfs-tools/jessie

« back to all changes in this revision

Viewing changes to free-space-cache.c

  • Committer: Package Import Robot
  • Author(s): Dimitri John Ledkov
  • Date: 2014-10-23 22:04:07 UTC
  • mfrom: (1.2.15) (6.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20141023220407-skt9hy0ft4oim95o
Tags: 3.17-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
#include "crc32c.h"
26
26
#include "bitops.h"
27
27
 
28
 
#define CACHE_SECTORSIZE        4096
29
 
#define BITS_PER_BITMAP         (CACHE_SECTORSIZE * 8)
 
28
/*
 
29
 * Kernel always uses PAGE_CACHE_SIZE for sectorsize, but we don't have
 
30
 * anything like that in userspace and have to get the value from the
 
31
 * filesystem
 
32
 */
 
33
#define BITS_PER_BITMAP(sectorsize)             ((sectorsize) * 8)
30
34
#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
31
35
 
32
36
static int link_free_space(struct btrfs_free_space_ctl *ctl,
48
52
                       struct btrfs_root *root)
49
53
{
50
54
        memset(io_ctl, 0, sizeof(struct io_ctl));
51
 
        io_ctl->num_pages = (size + CACHE_SECTORSIZE - 1) / CACHE_SECTORSIZE;
 
55
        io_ctl->num_pages = (size + root->sectorsize - 1) / root->sectorsize;
52
56
        io_ctl->buffer = kzalloc(size, GFP_NOFS);
53
57
        if (!io_ctl->buffer)
54
58
                return -ENOMEM;
75
79
static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
76
80
{
77
81
        BUG_ON(io_ctl->index >= io_ctl->num_pages);
78
 
        io_ctl->cur = io_ctl->buffer + (io_ctl->index++ * CACHE_SECTORSIZE);
 
82
        io_ctl->cur = io_ctl->buffer + (io_ctl->index++ * io_ctl->root->sectorsize);
79
83
        io_ctl->orig = io_ctl->cur;
80
 
        io_ctl->size = CACHE_SECTORSIZE;
 
84
        io_ctl->size = io_ctl->root->sectorsize;
81
85
        if (clear)
82
 
                memset(io_ctl->cur, 0, CACHE_SECTORSIZE);
 
86
                memset(io_ctl->cur, 0, io_ctl->root->sectorsize);
83
87
}
84
88
 
85
89
static void io_ctl_drop_pages(struct io_ctl *io_ctl)
203
207
        val = *tmp;
204
208
 
205
209
        io_ctl_map_page(io_ctl, 0);
206
 
        crc = crc32c(crc, io_ctl->orig + offset, CACHE_SECTORSIZE - offset);
 
210
        crc = crc32c(crc, io_ctl->orig + offset, io_ctl->root->sectorsize - offset);
207
211
        btrfs_csum_final(crc, (char *)&crc);
208
212
        if (val != crc) {
209
213
                printk("btrfs: csum mismatch on free space cache\n");
250
254
        if (ret)
251
255
                return ret;
252
256
 
253
 
        memcpy(entry->bitmap, io_ctl->cur, CACHE_SECTORSIZE);
 
257
        memcpy(entry->bitmap, io_ctl->cur, io_ctl->root->sectorsize);
254
258
        io_ctl_unmap_page(io_ctl);
255
259
 
256
260
        return 0;
375
379
                } else {
376
380
                        BUG_ON(!num_bitmaps);
377
381
                        num_bitmaps--;
378
 
                        e->bitmap = kzalloc(CACHE_SECTORSIZE, GFP_NOFS);
 
382
                        e->bitmap = kzalloc(ctl->sectorsize, GFP_NOFS);
379
383
                        if (!e->bitmap) {
380
384
                                free(e);
381
385
                                goto free_cache;
462
466
{
463
467
        u64 bitmap_start;
464
468
        u64 bytes_per_bitmap;
 
469
        u32 sectorsize = ctl->sectorsize;
465
470
 
466
 
        bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
 
471
        bytes_per_bitmap = BITS_PER_BITMAP(sectorsize) * ctl->unit;
467
472
        bitmap_start = offset - ctl->start;
468
473
        bitmap_start = bitmap_start / bytes_per_bitmap;
469
474
        bitmap_start *= bytes_per_bitmap;
532
537
{
533
538
        struct rb_node *n = ctl->free_space_offset.rb_node;
534
539
        struct btrfs_free_space *entry, *prev = NULL;
 
540
        u32 sectorsize = ctl->sectorsize;
535
541
 
536
542
        /* find entry that is closest to the 'offset' */
537
543
        while (1) {
616
622
                            prev->offset + prev->bytes > offset)
617
623
                                return prev;
618
624
                }
619
 
                if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
 
625
                if (entry->offset + BITS_PER_BITMAP(sectorsize) * ctl->unit > offset)
620
626
                        return entry;
621
627
        } else if (entry->offset + entry->bytes > offset)
622
628
                return entry;
626
632
 
627
633
        while (1) {
628
634
                if (entry->bitmap) {
629
 
                        if (entry->offset + BITS_PER_BITMAP *
 
635
                        if (entry->offset + BITS_PER_BITMAP(sectorsize) *
630
636
                            ctl->unit > offset)
631
637
                                break;
632
638
                } else {
673
679
        unsigned long found_bits = 0;
674
680
        unsigned long bits, i;
675
681
        unsigned long next_zero;
 
682
        u32 sectorsize = ctl->sectorsize;
676
683
 
677
684
        i = offset_to_bit(bitmap_info->offset, ctl->unit,
678
685
                          max_t(u64, *offset, bitmap_info->offset));
679
686
        bits = bytes_to_bits(*bytes, ctl->unit);
680
687
 
681
 
        for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP) {
 
688
        for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP(sectorsize)) {
682
689
                next_zero = find_next_zero_bit(bitmap_info->bitmap,
683
 
                                               BITS_PER_BITMAP, i);
 
690
                                               BITS_PER_BITMAP(sectorsize), i);
684
691
                if ((next_zero - i) >= bits) {
685
692
                        found_bits = next_zero - i;
686
693
                        break;
767
774
        if (!ctl)
768
775
                return -ENOMEM;
769
776
 
 
777
        ctl->sectorsize = sectorsize;
770
778
        ctl->unit = sectorsize;
771
779
        ctl->start = block_group->key.objectid;
772
780
        ctl->private = block_group;
827
835
        struct btrfs_free_space *e, *prev = NULL;
828
836
        struct rb_node *n;
829
837
        int ret;
 
838
        u32 sectorsize = ctl->sectorsize;
830
839
 
831
840
again:
832
841
        prev = NULL;
836
845
                        u64 offset = e->offset, bytes = ctl->unit;
837
846
                        u64 end;
838
847
 
839
 
                        end = e->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
 
848
                        end = e->offset + (u64)(BITS_PER_BITMAP(sectorsize) * ctl->unit);
840
849
 
841
850
                        unlink_free_space(ctl, e);
842
851
                        while (!(search_bitmap(ctl, e, &offset, &bytes))) {