~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to fs/btrfs/relocation.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
#include "btrfs_inode.h"
31
31
#include "async-thread.h"
32
32
#include "free-space-cache.h"
 
33
#include "inode-map.h"
33
34
 
34
35
/*
35
36
 * backref_node, mapping_node and tree_block start with this
507
508
        return 1;
508
509
}
509
510
 
 
511
 
510
512
static int should_ignore_root(struct btrfs_root *root)
511
513
{
512
514
        struct btrfs_root *reloc_root;
529
531
         */
530
532
        return 1;
531
533
}
532
 
 
533
534
/*
534
535
 * find reloc tree by address of tree root
535
536
 */
676
677
                err = -ENOMEM;
677
678
                goto out;
678
679
        }
 
680
        path1->reada = 1;
 
681
        path2->reada = 2;
679
682
 
680
683
        node = alloc_backref_node(cache);
681
684
        if (!node) {
709
712
        WARN_ON(cur->checked);
710
713
        if (!list_empty(&cur->upper)) {
711
714
                /*
712
 
                 * the backref was added previously when processsing
 
715
                 * the backref was added previously when processing
713
716
                 * backref of type BTRFS_TREE_BLOCK_REF_KEY
714
717
                 */
715
718
                BUG_ON(!list_is_singular(&cur->upper));
961
964
                        lower = upper;
962
965
                        upper = NULL;
963
966
                }
964
 
                btrfs_release_path(root, path2);
 
967
                btrfs_release_path(path2);
965
968
next:
966
969
                if (ptr < end) {
967
970
                        ptr += btrfs_extent_inline_ref_size(key.type);
974
977
                if (ptr >= end)
975
978
                        path1->slots[0]++;
976
979
        }
977
 
        btrfs_release_path(rc->extent_root, path1);
 
980
        btrfs_release_path(path1);
978
981
 
979
982
        cur->checked = 1;
980
983
        WARN_ON(exist);
1365
1368
        int ret;
1366
1369
 
1367
1370
        if (!root->reloc_root)
1368
 
                return 0;
 
1371
                goto out;
1369
1372
 
1370
1373
        reloc_root = root->reloc_root;
1371
1374
        root_item = &reloc_root->root_item;
1387
1390
        ret = btrfs_update_root(trans, root->fs_info->tree_root,
1388
1391
                                &reloc_root->root_key, root_item);
1389
1392
        BUG_ON(ret);
 
1393
 
 
1394
out:
1390
1395
        return 0;
1391
1396
}
1392
1397
 
1409
1414
                prev = node;
1410
1415
                entry = rb_entry(node, struct btrfs_inode, rb_node);
1411
1416
 
1412
 
                if (objectid < entry->vfs_inode.i_ino)
 
1417
                if (objectid < btrfs_ino(&entry->vfs_inode))
1413
1418
                        node = node->rb_left;
1414
 
                else if (objectid > entry->vfs_inode.i_ino)
 
1419
                else if (objectid > btrfs_ino(&entry->vfs_inode))
1415
1420
                        node = node->rb_right;
1416
1421
                else
1417
1422
                        break;
1419
1424
        if (!node) {
1420
1425
                while (prev) {
1421
1426
                        entry = rb_entry(prev, struct btrfs_inode, rb_node);
1422
 
                        if (objectid <= entry->vfs_inode.i_ino) {
 
1427
                        if (objectid <= btrfs_ino(&entry->vfs_inode)) {
1423
1428
                                node = prev;
1424
1429
                                break;
1425
1430
                        }
1434
1439
                        return inode;
1435
1440
                }
1436
1441
 
1437
 
                objectid = entry->vfs_inode.i_ino + 1;
 
1442
                objectid = btrfs_ino(&entry->vfs_inode) + 1;
1438
1443
                if (cond_resched_lock(&root->inode_lock))
1439
1444
                        goto again;
1440
1445
 
1470
1475
                return -ENOMEM;
1471
1476
 
1472
1477
        bytenr -= BTRFS_I(reloc_inode)->index_cnt;
1473
 
        ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
 
1478
        ret = btrfs_lookup_file_extent(NULL, root, path, btrfs_ino(reloc_inode),
1474
1479
                                       bytenr, 0);
1475
1480
        if (ret < 0)
1476
1481
                goto out;
1558
1563
                        if (first) {
1559
1564
                                inode = find_next_inode(root, key.objectid);
1560
1565
                                first = 0;
1561
 
                        } else if (inode && inode->i_ino < key.objectid) {
 
1566
                        } else if (inode && btrfs_ino(inode) < key.objectid) {
1562
1567
                                btrfs_add_delayed_iput(inode);
1563
1568
                                inode = find_next_inode(root, key.objectid);
1564
1569
                        }
1565
 
                        if (inode && inode->i_ino == key.objectid) {
 
1570
                        if (inode && btrfs_ino(inode) == key.objectid) {
1566
1571
                                end = key.offset +
1567
1572
                                      btrfs_file_extent_num_bytes(leaf, fi);
1568
1573
                                WARN_ON(!IS_ALIGNED(key.offset,
1724
1729
 
1725
1730
                        eb = read_tree_block(dest, old_bytenr, blocksize,
1726
1731
                                             old_ptr_gen);
 
1732
                        BUG_ON(!eb);
1727
1733
                        btrfs_tree_lock(eb);
1728
1734
                        if (cow) {
1729
1735
                                ret = btrfs_cow_block(trans, dest, eb, parent,
1748
1754
 
1749
1755
                btrfs_node_key_to_cpu(path->nodes[level], &key,
1750
1756
                                      path->slots[level]);
1751
 
                btrfs_release_path(src, path);
 
1757
                btrfs_release_path(path);
1752
1758
 
1753
1759
                path->lowest_level = level;
1754
1760
                ret = btrfs_search_slot(trans, src, &key, path, 0, 1);
1892
1898
        struct inode *inode = NULL;
1893
1899
        u64 objectid;
1894
1900
        u64 start, end;
 
1901
        u64 ino;
1895
1902
 
1896
1903
        objectid = min_key->objectid;
1897
1904
        while (1) {
1904
1911
                inode = find_next_inode(root, objectid);
1905
1912
                if (!inode)
1906
1913
                        break;
 
1914
                ino = btrfs_ino(inode);
1907
1915
 
1908
 
                if (inode->i_ino > max_key->objectid) {
 
1916
                if (ino > max_key->objectid) {
1909
1917
                        iput(inode);
1910
1918
                        break;
1911
1919
                }
1912
1920
 
1913
 
                objectid = inode->i_ino + 1;
 
1921
                objectid = ino + 1;
1914
1922
                if (!S_ISREG(inode->i_mode))
1915
1923
                        continue;
1916
1924
 
1917
 
                if (unlikely(min_key->objectid == inode->i_ino)) {
 
1925
                if (unlikely(min_key->objectid == ino)) {
1918
1926
                        if (min_key->type > BTRFS_EXTENT_DATA_KEY)
1919
1927
                                continue;
1920
1928
                        if (min_key->type < BTRFS_EXTENT_DATA_KEY)
1927
1935
                        start = 0;
1928
1936
                }
1929
1937
 
1930
 
                if (unlikely(max_key->objectid == inode->i_ino)) {
 
1938
                if (unlikely(max_key->objectid == ino)) {
1931
1939
                        if (max_key->type < BTRFS_EXTENT_DATA_KEY)
1932
1940
                                continue;
1933
1941
                        if (max_key->type > BTRFS_EXTENT_DATA_KEY) {
1995
2003
        path = btrfs_alloc_path();
1996
2004
        if (!path)
1997
2005
                return -ENOMEM;
 
2006
        path->reada = 1;
1998
2007
 
1999
2008
        reloc_root = root->reloc_root;
2000
2009
        root_item = &reloc_root->root_item;
2135
2144
        u64 num_bytes = 0;
2136
2145
        int ret;
2137
2146
 
2138
 
        mutex_lock(&root->fs_info->trans_mutex);
 
2147
        mutex_lock(&root->fs_info->reloc_mutex);
2139
2148
        rc->merging_rsv_size += root->nodesize * (BTRFS_MAX_LEVEL - 1) * 2;
2140
2149
        rc->merging_rsv_size += rc->nodes_relocated * 2;
2141
 
        mutex_unlock(&root->fs_info->trans_mutex);
 
2150
        mutex_unlock(&root->fs_info->reloc_mutex);
 
2151
 
2142
2152
again:
2143
2153
        if (!err) {
2144
2154
                num_bytes = rc->merging_rsv_size;
2148
2158
                        err = ret;
2149
2159
        }
2150
2160
 
2151
 
        trans = btrfs_join_transaction(rc->extent_root, 1);
 
2161
        trans = btrfs_join_transaction(rc->extent_root);
2152
2162
        if (IS_ERR(trans)) {
2153
2163
                if (!err)
2154
2164
                        btrfs_block_rsv_release(rc->extent_root,
2207
2217
        int ret;
2208
2218
again:
2209
2219
        root = rc->extent_root;
2210
 
        mutex_lock(&root->fs_info->trans_mutex);
 
2220
 
 
2221
        /*
 
2222
         * this serializes us with btrfs_record_root_in_transaction,
 
2223
         * we have to make sure nobody is in the middle of
 
2224
         * adding their roots to the list while we are
 
2225
         * doing this splice
 
2226
         */
 
2227
        mutex_lock(&root->fs_info->reloc_mutex);
2211
2228
        list_splice_init(&rc->reloc_roots, &reloc_roots);
2212
 
        mutex_unlock(&root->fs_info->trans_mutex);
 
2229
        mutex_unlock(&root->fs_info->reloc_mutex);
2213
2230
 
2214
2231
        while (!list_empty(&reloc_roots)) {
2215
2232
                found = 1;
2345
2362
                root = next->root;
2346
2363
                BUG_ON(!root);
2347
2364
 
2348
 
                /* no other choice for non-refernce counted tree */
 
2365
                /* no other choice for non-references counted tree */
2349
2366
                if (!root->ref_cows)
2350
2367
                        return root;
2351
2368
 
2495
2512
                        path->locks[upper->level] = 0;
2496
2513
 
2497
2514
                        slot = path->slots[upper->level];
2498
 
                        btrfs_release_path(NULL, path);
 
2515
                        btrfs_release_path(path);
2499
2516
                } else {
2500
2517
                        ret = btrfs_bin_search(upper->eb, key, upper->level,
2501
2518
                                               &slot);
2513
2530
                blocksize = btrfs_level_size(root, node->level);
2514
2531
                generation = btrfs_node_ptr_generation(upper->eb, slot);
2515
2532
                eb = read_tree_block(root, bytenr, blocksize, generation);
 
2533
                if (!eb) {
 
2534
                        err = -EIO;
 
2535
                        goto next;
 
2536
                }
2516
2537
                btrfs_tree_lock(eb);
2517
2538
                btrfs_set_lock_blocking(eb);
2518
2539
 
2670
2691
        BUG_ON(block->key_ready);
2671
2692
        eb = read_tree_block(rc->extent_root, block->bytenr,
2672
2693
                             block->key.objectid, block->key.offset);
 
2694
        BUG_ON(!eb);
2673
2695
        WARN_ON(btrfs_header_level(eb) != block->level);
2674
2696
        if (block->level == 0)
2675
2697
                btrfs_item_key_to_cpu(eb, &block->key, 0);
2731
2753
                } else {
2732
2754
                        path->lowest_level = node->level;
2733
2755
                        ret = btrfs_search_slot(trans, root, key, path, 0, 1);
2734
 
                        btrfs_release_path(root, path);
 
2756
                        btrfs_release_path(path);
2735
2757
                        if (ret > 0)
2736
2758
                                ret = 0;
2737
2759
                }
2864
2886
        struct extent_map *em;
2865
2887
        int ret = 0;
2866
2888
 
2867
 
        em = alloc_extent_map(GFP_NOFS);
 
2889
        em = alloc_extent_map();
2868
2890
        if (!em)
2869
2891
                return -ENOMEM;
2870
2892
 
3113
3135
#endif
3114
3136
        }
3115
3137
 
3116
 
        btrfs_release_path(rc->extent_root, path);
 
3138
        btrfs_release_path(path);
3117
3139
 
3118
3140
        BUG_ON(level == -1);
3119
3141
 
3214
3236
        key.offset = 0;
3215
3237
 
3216
3238
        inode = btrfs_iget(fs_info->sb, &key, root, NULL);
3217
 
        if (!inode || IS_ERR(inode) || is_bad_inode(inode)) {
 
3239
        if (IS_ERR_OR_NULL(inode) || is_bad_inode(inode)) {
3218
3240
                if (inode && !IS_ERR(inode))
3219
3241
                        iput(inode);
3220
3242
                return -ENOENT;
3227
3249
                goto out;
3228
3250
        }
3229
3251
 
3230
 
        trans = btrfs_join_transaction(root, 0);
 
3252
        trans = btrfs_join_transaction(root);
3231
3253
        if (IS_ERR(trans)) {
3232
3254
                btrfs_free_path(path);
3233
3255
                ret = PTR_ERR(trans);
3291
3313
        path = btrfs_alloc_path();
3292
3314
        if (!path)
3293
3315
                return -ENOMEM;
 
3316
        path->reada = 1;
3294
3317
 
3295
3318
        root = read_fs_root(rc->extent_root->fs_info, ref_root);
3296
3319
        if (IS_ERR(root)) {
3499
3522
                }
3500
3523
                path->slots[0]++;
3501
3524
        }
3502
 
        btrfs_release_path(rc->extent_root, path);
 
3525
        btrfs_release_path(path);
3503
3526
        if (err)
3504
3527
                free_block_list(blocks);
3505
3528
        return err;
3562
3585
                                            EXTENT_DIRTY);
3563
3586
 
3564
3587
                if (ret == 0 && start <= key.objectid) {
3565
 
                        btrfs_release_path(rc->extent_root, path);
 
3588
                        btrfs_release_path(path);
3566
3589
                        rc->search_start = end + 1;
3567
3590
                } else {
3568
3591
                        rc->search_start = key.objectid + key.offset;
3570
3593
                        return 0;
3571
3594
                }
3572
3595
        }
3573
 
        btrfs_release_path(rc->extent_root, path);
 
3596
        btrfs_release_path(path);
3574
3597
        return ret;
3575
3598
}
3576
3599
 
3577
3600
static void set_reloc_control(struct reloc_control *rc)
3578
3601
{
3579
3602
        struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3580
 
        mutex_lock(&fs_info->trans_mutex);
 
3603
 
 
3604
        mutex_lock(&fs_info->reloc_mutex);
3581
3605
        fs_info->reloc_ctl = rc;
3582
 
        mutex_unlock(&fs_info->trans_mutex);
 
3606
        mutex_unlock(&fs_info->reloc_mutex);
3583
3607
}
3584
3608
 
3585
3609
static void unset_reloc_control(struct reloc_control *rc)
3586
3610
{
3587
3611
        struct btrfs_fs_info *fs_info = rc->extent_root->fs_info;
3588
 
        mutex_lock(&fs_info->trans_mutex);
 
3612
 
 
3613
        mutex_lock(&fs_info->reloc_mutex);
3589
3614
        fs_info->reloc_ctl = NULL;
3590
 
        mutex_unlock(&fs_info->trans_mutex);
 
3615
        mutex_unlock(&fs_info->reloc_mutex);
3591
3616
}
3592
3617
 
3593
3618
static int check_extent_flags(u64 flags)
3636
3661
        rc->create_reloc_tree = 1;
3637
3662
        set_reloc_control(rc);
3638
3663
 
3639
 
        trans = btrfs_join_transaction(rc->extent_root, 1);
 
3664
        trans = btrfs_join_transaction(rc->extent_root);
3640
3665
        BUG_ON(IS_ERR(trans));
3641
3666
        btrfs_commit_transaction(trans, rc->extent_root);
3642
3667
        return 0;
3659
3684
        path = btrfs_alloc_path();
3660
3685
        if (!path)
3661
3686
                return -ENOMEM;
 
3687
        path->reada = 1;
3662
3688
 
3663
3689
        ret = prepare_to_relocate(rc);
3664
3690
        if (ret) {
3707
3733
                                flags = BTRFS_EXTENT_FLAG_DATA;
3708
3734
 
3709
3735
                        if (path_change) {
3710
 
                                btrfs_release_path(rc->extent_root, path);
 
3736
                                btrfs_release_path(path);
3711
3737
 
3712
3738
                                path->search_commit_root = 1;
3713
3739
                                path->skip_locking = 1;
3730
3756
                           (flags & BTRFS_EXTENT_FLAG_DATA)) {
3731
3757
                        ret = add_data_references(rc, &key, path, &blocks);
3732
3758
                } else {
3733
 
                        btrfs_release_path(rc->extent_root, path);
 
3759
                        btrfs_release_path(path);
3734
3760
                        ret = 0;
3735
3761
                }
3736
3762
                if (ret < 0) {
3793
3819
                }
3794
3820
        }
3795
3821
 
3796
 
        btrfs_release_path(rc->extent_root, path);
 
3822
        btrfs_release_path(path);
3797
3823
        clear_extent_bits(&rc->processed_blocks, 0, (u64)-1, EXTENT_DIRTY,
3798
3824
                          GFP_NOFS);
3799
3825
 
3825
3851
        btrfs_block_rsv_release(rc->extent_root, rc->block_rsv, (u64)-1);
3826
3852
 
3827
3853
        /* get rid of pinned extents */
3828
 
        trans = btrfs_join_transaction(rc->extent_root, 1);
 
3854
        trans = btrfs_join_transaction(rc->extent_root);
3829
3855
        if (IS_ERR(trans))
3830
3856
                err = PTR_ERR(trans);
3831
3857
        else
3861
3887
        btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS |
3862
3888
                                          BTRFS_INODE_PREALLOC);
3863
3889
        btrfs_mark_buffer_dirty(leaf);
3864
 
        btrfs_release_path(root, path);
 
3890
        btrfs_release_path(path);
3865
3891
out:
3866
3892
        btrfs_free_path(path);
3867
3893
        return ret;
3891
3917
        if (IS_ERR(trans))
3892
3918
                return ERR_CAST(trans);
3893
3919
 
3894
 
        err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
 
3920
        err = btrfs_find_free_objectid(root, &objectid);
3895
3921
        if (err)
3896
3922
                goto out;
3897
3923
 
3929
3955
        INIT_LIST_HEAD(&rc->reloc_roots);
3930
3956
        backref_cache_init(&rc->backref_cache);
3931
3957
        mapping_tree_init(&rc->reloc_root_tree);
3932
 
        extent_io_tree_init(&rc->processed_blocks, NULL, GFP_NOFS);
 
3958
        extent_io_tree_init(&rc->processed_blocks, NULL);
3933
3959
        return rc;
3934
3960
}
3935
3961
 
4084
4110
        path = btrfs_alloc_path();
4085
4111
        if (!path)
4086
4112
                return -ENOMEM;
 
4113
        path->reada = -1;
4087
4114
 
4088
4115
        key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4089
4116
        key.type = BTRFS_ROOT_ITEM_KEY;
4103
4130
                }
4104
4131
                leaf = path->nodes[0];
4105
4132
                btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4106
 
                btrfs_release_path(root->fs_info->tree_root, path);
 
4133
                btrfs_release_path(path);
4107
4134
 
4108
4135
                if (key.objectid != BTRFS_TREE_RELOC_OBJECTID ||
4109
4136
                    key.type != BTRFS_ROOT_ITEM_KEY)
4135
4162
 
4136
4163
                key.offset--;
4137
4164
        }
4138
 
        btrfs_release_path(root->fs_info->tree_root, path);
 
4165
        btrfs_release_path(path);
4139
4166
 
4140
4167
        if (list_empty(&reloc_roots))
4141
4168
                goto out;
4150
4177
 
4151
4178
        set_reloc_control(rc);
4152
4179
 
4153
 
        trans = btrfs_join_transaction(rc->extent_root, 1);
 
4180
        trans = btrfs_join_transaction(rc->extent_root);
4154
4181
        if (IS_ERR(trans)) {
4155
4182
                unset_reloc_control(rc);
4156
4183
                err = PTR_ERR(trans);
4184
4211
 
4185
4212
        unset_reloc_control(rc);
4186
4213
 
4187
 
        trans = btrfs_join_transaction(rc->extent_root, 1);
 
4214
        trans = btrfs_join_transaction(rc->extent_root);
4188
4215
        if (IS_ERR(trans))
4189
4216
                err = PTR_ERR(trans);
4190
4217
        else
4209
4236
                if (IS_ERR(fs_root))
4210
4237
                        err = PTR_ERR(fs_root);
4211
4238
                else
4212
 
                        btrfs_orphan_cleanup(fs_root);
 
4239
                        err = btrfs_orphan_cleanup(fs_root);
4213
4240
        }
4214
4241
        return err;
4215
4242
}
4236
4263
 
4237
4264
        disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
4238
4265
        ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
4239
 
                                       disk_bytenr + len - 1, &list);
 
4266
                                       disk_bytenr + len - 1, &list, 0);
4240
4267
 
4241
4268
        while (!list_empty(&list)) {
4242
4269
                sums = list_entry(list.next, struct btrfs_ordered_sum, list);