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

« back to all changes in this revision

Viewing changes to cmds-check.c

  • Committer: Package Import Robot
  • Author(s): Dimitri John Ledkov
  • Date: 2014-04-19 12:12:11 UTC
  • mfrom: (1.2.12) (6.1.37 sid)
  • Revision ID: package-import@ubuntu.com-20140419121211-mski0g757tsdv4x1
Tags: 3.14.1-1
* New upstream release.
* Switch to git-dpm.
* Rebase and cleanup patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
static u64 data_bytes_referenced = 0;
50
50
static int found_old_backref = 0;
51
51
static LIST_HEAD(duplicate_extents);
 
52
static LIST_HEAD(delete_items);
52
53
static int repair = 0;
 
54
static int no_holes = 0;
 
55
static int init_extent_tree = 0;
53
56
 
54
57
struct extent_backref {
55
58
        struct list_head list;
89
92
        struct list_head list;
90
93
        struct cache_extent cache;
91
94
        struct btrfs_disk_key parent_key;
92
 
        unsigned int found_rec;
93
95
        u64 start;
94
96
        u64 max_size;
95
97
        u64 nr;
96
98
        u64 refs;
97
99
        u64 extent_item_refs;
98
100
        u64 generation;
 
101
        u64 parent_generation;
99
102
        u64 info_objectid;
100
 
        u64 num_duplicates;
 
103
        u32 num_duplicates;
101
104
        u8 info_level;
 
105
        unsigned int found_rec:1;
102
106
        unsigned int content_checked:1;
103
107
        unsigned int owner_ref_checked:1;
104
108
        unsigned int is_root:1;
119
123
        char name[0];
120
124
};
121
125
 
 
126
struct dropping_root_item_record {
 
127
        struct list_head list;
 
128
        struct btrfs_root_item ri;
 
129
        struct btrfs_key found_key;
 
130
};
 
131
 
122
132
#define REF_ERR_NO_DIR_ITEM             (1 << 0)
123
133
#define REF_ERR_NO_DIR_INDEX            (1 << 1)
124
134
#define REF_ERR_NO_INODE_REF            (1 << 2)
223
233
        int root_level;
224
234
};
225
235
 
 
236
struct bad_item {
 
237
        struct btrfs_key key;
 
238
        u64 root_id;
 
239
        struct list_head list;
 
240
};
 
241
 
226
242
static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
227
243
 
228
244
static u8 imode_to_type(u32 imode)
443
459
                        rec->errors |= I_ERR_FILE_NBYTES_WRONG;
444
460
                if (rec->extent_start == (u64)-1 || rec->extent_start > 0)
445
461
                        rec->first_extent_gap = 0;
446
 
                if (rec->nlink > 0 && (rec->extent_end < rec->isize ||
447
 
                    rec->first_extent_gap < rec->isize))
 
462
                if (rec->nlink > 0 && !no_holes &&
 
463
                    (rec->extent_end < rec->isize ||
 
464
                     rec->first_extent_gap < rec->isize))
448
465
                        rec->errors |= I_ERR_FILE_EXTENT_DISCOUNT;
449
466
        }
450
467
 
1149
1166
        extent_type = btrfs_file_extent_type(eb, fi);
1150
1167
 
1151
1168
        if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1152
 
                num_bytes = btrfs_file_extent_inline_len(eb, fi);
 
1169
                num_bytes = btrfs_file_extent_inline_len(eb, slot, fi);
1153
1170
                if (num_bytes == 0)
1154
1171
                        rec->errors |= I_ERR_BAD_FILE_EXTENT;
1155
1172
                rec->found_size += num_bytes;
1204
1221
        u32 nritems;
1205
1222
        int i;
1206
1223
        int ret = 0;
 
1224
        int error = 0;
1207
1225
        struct cache_tree *inode_cache;
1208
1226
        struct shared_node *active_node;
1209
1227
 
1219
1237
 
1220
1238
                if (key.objectid == BTRFS_FREE_SPACE_OBJECTID)
1221
1239
                        continue;
 
1240
                if (key.type == BTRFS_ORPHAN_ITEM_KEY)
 
1241
                        continue;
1222
1242
 
1223
1243
                if (active_node->current == NULL ||
1224
1244
                    active_node->current->ino < key.objectid) {
1251
1271
                default:
1252
1272
                        break;
1253
1273
                };
 
1274
                if (ret != 0)
 
1275
                        error = 1;
1254
1276
        }
1255
 
        return ret;
 
1277
        return error;
1256
1278
}
1257
1279
 
1258
1280
static void reada_walk_down(struct btrfs_root *root,
1412
1434
        return ret;
1413
1435
}
1414
1436
 
1415
 
static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
 
1437
static int repair_inode_isize(struct btrfs_trans_handle *trans,
 
1438
                              struct btrfs_root *root, struct btrfs_path *path,
 
1439
                              struct inode_record *rec)
1416
1440
{
1417
 
        struct btrfs_trans_handle *trans;
1418
 
        struct btrfs_path *path;
1419
1441
        struct btrfs_inode_item *ei;
1420
1442
        struct btrfs_key key;
1421
1443
        int ret;
1422
1444
 
1423
 
        /* So far we just fix dir isize wrong */
1424
 
        if (!(rec->errors & I_ERR_DIR_ISIZE_WRONG))
1425
 
                return 1;
1426
 
 
1427
 
        path = btrfs_alloc_path();
1428
 
        if (!path)
1429
 
                return -ENOMEM;
1430
 
 
1431
 
        trans = btrfs_start_transaction(root, 1);
1432
 
        if (IS_ERR(trans)) {
1433
 
                btrfs_free_path(path);
1434
 
                return PTR_ERR(trans);
1435
 
        }
1436
 
 
1437
1445
        key.objectid = rec->ino;
1438
1446
        key.type = BTRFS_INODE_ITEM_KEY;
1439
1447
        key.offset = (u64)-1;
1463
1471
        printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1464
1472
               root->root_key.objectid);
1465
1473
out:
 
1474
        btrfs_release_path(path);
 
1475
        return ret;
 
1476
}
 
1477
 
 
1478
static int repair_inode_orphan_item(struct btrfs_trans_handle *trans,
 
1479
                                    struct btrfs_root *root,
 
1480
                                    struct btrfs_path *path,
 
1481
                                    struct inode_record *rec)
 
1482
{
 
1483
        struct btrfs_key key;
 
1484
        int ret;
 
1485
 
 
1486
        key.objectid = BTRFS_ORPHAN_OBJECTID;
 
1487
        key.type = BTRFS_ORPHAN_ITEM_KEY;
 
1488
        key.offset = rec->ino;
 
1489
 
 
1490
        ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
 
1491
        btrfs_release_path(path);
 
1492
        if (!ret)
 
1493
                rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
 
1494
        return ret;
 
1495
}
 
1496
 
 
1497
static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
 
1498
{
 
1499
        struct btrfs_trans_handle *trans;
 
1500
        struct btrfs_path *path;
 
1501
        int ret = 0;
 
1502
 
 
1503
        /* So far we just fix dir isize wrong */
 
1504
        if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
 
1505
                return 1;
 
1506
 
 
1507
        path = btrfs_alloc_path();
 
1508
        if (!path)
 
1509
                return -ENOMEM;
 
1510
 
 
1511
        trans = btrfs_start_transaction(root, 1);
 
1512
        if (IS_ERR(trans)) {
 
1513
                btrfs_free_path(path);
 
1514
                return PTR_ERR(trans);
 
1515
        }
 
1516
 
 
1517
        if (rec->errors & I_ERR_DIR_ISIZE_WRONG)
 
1518
                ret = repair_inode_isize(trans, root, path, rec);
 
1519
        if (!ret && rec->errors & I_ERR_NO_ORPHAN_ITEM)
 
1520
                ret = repair_inode_orphan_item(trans, root, path, rec);
1466
1521
        btrfs_commit_transaction(trans, root);
1467
1522
        btrfs_free_path(path);
1468
1523
        return ret;
1999
2054
                btrfs_item_key_to_cpu(leaf, &key, path.slots[0]);
2000
2055
                if (key.type == BTRFS_ROOT_ITEM_KEY &&
2001
2056
                    fs_root_objectid(key.objectid)) {
2002
 
                        tmp_root = btrfs_read_fs_root_no_cache(root->fs_info,
2003
 
                                                               &key);
 
2057
                        key.offset = (u64)-1;
 
2058
                        tmp_root = btrfs_read_fs_root(root->fs_info, &key);
2004
2059
                        if (IS_ERR(tmp_root)) {
2005
2060
                                err = 1;
2006
2061
                                goto next;
2008
2063
                        ret = check_fs_root(tmp_root, root_cache, &wc);
2009
2064
                        if (ret)
2010
2065
                                err = 1;
2011
 
                        btrfs_free_fs_root(tmp_root);
2012
2066
                } else if (key.type == BTRFS_ROOT_REF_KEY ||
2013
2067
                           key.type == BTRFS_ROOT_BACKREF_KEY) {
2014
2068
                        process_root_ref(leaf, path.slots[0], &key,
2283
2337
        return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
2284
2338
}
2285
2339
 
2286
 
static int check_block(struct btrfs_root *root,
 
2340
static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
 
2341
                       struct extent_buffer *buf, int slot)
 
2342
{
 
2343
        if (btrfs_header_level(buf)) {
 
2344
                struct btrfs_key_ptr ptr1, ptr2;
 
2345
 
 
2346
                read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
 
2347
                                   sizeof(struct btrfs_key_ptr));
 
2348
                read_extent_buffer(buf, &ptr2,
 
2349
                                   btrfs_node_key_ptr_offset(slot + 1),
 
2350
                                   sizeof(struct btrfs_key_ptr));
 
2351
                write_extent_buffer(buf, &ptr1,
 
2352
                                    btrfs_node_key_ptr_offset(slot + 1),
 
2353
                                    sizeof(struct btrfs_key_ptr));
 
2354
                write_extent_buffer(buf, &ptr2,
 
2355
                                    btrfs_node_key_ptr_offset(slot),
 
2356
                                    sizeof(struct btrfs_key_ptr));
 
2357
                if (slot == 0) {
 
2358
                        struct btrfs_disk_key key;
 
2359
                        btrfs_node_key(buf, &key, 0);
 
2360
                        btrfs_fixup_low_keys(root, path, &key,
 
2361
                                             btrfs_header_level(buf) + 1);
 
2362
                }
 
2363
        } else {
 
2364
                struct btrfs_item *item1, *item2;
 
2365
                struct btrfs_key k1, k2;
 
2366
                char *item1_data, *item2_data;
 
2367
                u32 item1_offset, item2_offset, item1_size, item2_size;
 
2368
 
 
2369
                item1 = btrfs_item_nr(slot);
 
2370
                item2 = btrfs_item_nr(slot + 1);
 
2371
                btrfs_item_key_to_cpu(buf, &k1, slot);
 
2372
                btrfs_item_key_to_cpu(buf, &k2, slot + 1);
 
2373
                item1_offset = btrfs_item_offset(buf, item1);
 
2374
                item2_offset = btrfs_item_offset(buf, item2);
 
2375
                item1_size = btrfs_item_size(buf, item1);
 
2376
                item2_size = btrfs_item_size(buf, item2);
 
2377
 
 
2378
                item1_data = malloc(item1_size);
 
2379
                if (!item1_data)
 
2380
                        return -ENOMEM;
 
2381
                item2_data = malloc(item2_size);
 
2382
                if (!item2_data) {
 
2383
                        free(item1_data);
 
2384
                        return -ENOMEM;
 
2385
                }
 
2386
 
 
2387
                read_extent_buffer(buf, item1_data, item1_offset, item1_size);
 
2388
                read_extent_buffer(buf, item2_data, item2_offset, item2_size);
 
2389
 
 
2390
                write_extent_buffer(buf, item1_data, item2_offset, item2_size);
 
2391
                write_extent_buffer(buf, item2_data, item1_offset, item1_size);
 
2392
                free(item1_data);
 
2393
                free(item2_data);
 
2394
 
 
2395
                btrfs_set_item_offset(buf, item1, item2_offset);
 
2396
                btrfs_set_item_offset(buf, item2, item1_offset);
 
2397
                btrfs_set_item_size(buf, item1, item2_size);
 
2398
                btrfs_set_item_size(buf, item2, item1_size);
 
2399
 
 
2400
                path->slots[0] = slot;
 
2401
                btrfs_set_item_key_unsafe(root, path, &k2);
 
2402
                path->slots[0] = slot + 1;
 
2403
                btrfs_set_item_key_unsafe(root, path, &k1);
 
2404
        }
 
2405
        return 0;
 
2406
}
 
2407
 
 
2408
/*
 
2409
 * Attempt to fix basic block failures.  Currently we only handle bad key
 
2410
 * orders, we will cycle through the keys and swap them if necessary.
 
2411
 */
 
2412
static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
 
2413
                                struct btrfs_root *root,
 
2414
                                struct extent_buffer *buf,
 
2415
                                struct btrfs_disk_key *parent_key,
 
2416
                                enum btrfs_tree_block_status status)
 
2417
{
 
2418
        struct btrfs_path *path;
 
2419
        struct btrfs_key k1, k2;
 
2420
        int i;
 
2421
        int ret;
 
2422
 
 
2423
        if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
 
2424
                return -EIO;
 
2425
 
 
2426
        k1.objectid = btrfs_header_owner(buf);
 
2427
        k1.type = BTRFS_ROOT_ITEM_KEY;
 
2428
        k1.offset = (u64)-1;
 
2429
 
 
2430
        root = btrfs_read_fs_root(root->fs_info, &k1);
 
2431
        if (IS_ERR(root))
 
2432
                return -EIO;
 
2433
 
 
2434
        path = btrfs_alloc_path();
 
2435
        if (!path)
 
2436
                return -EIO;
 
2437
 
 
2438
        path->lowest_level = btrfs_header_level(buf);
 
2439
        path->skip_check_block = 1;
 
2440
        if (btrfs_header_level(buf))
 
2441
                btrfs_node_key_to_cpu(buf, &k1, 0);
 
2442
        else
 
2443
                btrfs_item_key_to_cpu(buf, &k1, 0);
 
2444
 
 
2445
        ret = btrfs_search_slot(trans, root, &k1, path, 0, 1);
 
2446
        if (ret) {
 
2447
                btrfs_free_path(path);
 
2448
                return -EIO;
 
2449
        }
 
2450
 
 
2451
        buf = path->nodes[0];
 
2452
        for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
 
2453
                if (btrfs_header_level(buf)) {
 
2454
                        btrfs_node_key_to_cpu(buf, &k1, i);
 
2455
                        btrfs_node_key_to_cpu(buf, &k2, i + 1);
 
2456
                } else {
 
2457
                        btrfs_item_key_to_cpu(buf, &k1, i);
 
2458
                        btrfs_item_key_to_cpu(buf, &k2, i + 1);
 
2459
                }
 
2460
                if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
 
2461
                        continue;
 
2462
                ret = swap_values(root, path, buf, i);
 
2463
                if (ret)
 
2464
                        break;
 
2465
                btrfs_mark_buffer_dirty(buf);
 
2466
                i = 0;
 
2467
        }
 
2468
 
 
2469
        btrfs_free_path(path);
 
2470
        return ret;
 
2471
}
 
2472
 
 
2473
static int check_block(struct btrfs_trans_handle *trans,
 
2474
                       struct btrfs_root *root,
2287
2475
                       struct cache_tree *extent_cache,
2288
2476
                       struct extent_buffer *buf, u64 flags)
2289
2477
{
2290
2478
        struct extent_record *rec;
2291
2479
        struct cache_extent *cache;
2292
2480
        struct btrfs_key key;
2293
 
        int ret = 1;
 
2481
        enum btrfs_tree_block_status status;
 
2482
        int ret = 0;
2294
2483
        int level;
2295
2484
 
2296
2485
        cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2312
2501
        rec->info_level = level;
2313
2502
 
2314
2503
        if (btrfs_is_leaf(buf))
2315
 
                ret = btrfs_check_leaf(root, &rec->parent_key, buf);
 
2504
                status = btrfs_check_leaf(root, &rec->parent_key, buf);
2316
2505
        else
2317
 
                ret = btrfs_check_node(root, &rec->parent_key, buf);
 
2506
                status = btrfs_check_node(root, &rec->parent_key, buf);
2318
2507
 
2319
 
        if (ret) {
2320
 
                fprintf(stderr, "bad block %llu\n",
2321
 
                        (unsigned long long)buf->start);
 
2508
        if (status != BTRFS_TREE_BLOCK_CLEAN) {
 
2509
                if (repair)
 
2510
                        status = try_to_fix_bad_block(trans, root, buf,
 
2511
                                                      &rec->parent_key,
 
2512
                                                      status);
 
2513
                if (status != BTRFS_TREE_BLOCK_CLEAN) {
 
2514
                        ret = -EIO;
 
2515
                        fprintf(stderr, "bad block %llu\n",
 
2516
                                (unsigned long long)buf->start);
 
2517
                } else {
 
2518
                        /*
 
2519
                         * Signal to callers we need to start the scan over
 
2520
                         * again since we'll have cow'ed blocks.
 
2521
                         */
 
2522
                        ret = -EAGAIN;
 
2523
                }
2322
2524
        } else {
2323
2525
                rec->content_checked = 1;
2324
2526
                if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
2446
2648
}
2447
2649
 
2448
2650
static int add_extent_rec(struct cache_tree *extent_cache,
2449
 
                          struct btrfs_key *parent_key,
 
2651
                          struct btrfs_key *parent_key, u64 parent_gen,
2450
2652
                          u64 start, u64 nr, u64 extent_item_refs,
2451
2653
                          int is_root, int inc_ref, int set_checked,
2452
2654
                          int metadata, int extent_rec, u64 max_size)
2522
2724
 
2523
2725
                if (parent_key)
2524
2726
                        btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
 
2727
                if (parent_gen)
 
2728
                        rec->parent_generation = parent_gen;
2525
2729
 
2526
2730
                if (rec->max_size < max_size)
2527
2731
                        rec->max_size = max_size;
2533
2737
        rec->start = start;
2534
2738
        rec->max_size = max_size;
2535
2739
        rec->nr = max(nr, max_size);
2536
 
        rec->found_rec = extent_rec;
 
2740
        rec->found_rec = !!extent_rec;
2537
2741
        rec->content_checked = 0;
2538
2742
        rec->owner_ref_checked = 0;
2539
2743
        rec->num_duplicates = 0;
2562
2766
        else
2563
2767
                memset(&rec->parent_key, 0, sizeof(*parent_key));
2564
2768
 
 
2769
        if (parent_gen)
 
2770
                rec->parent_generation = parent_gen;
 
2771
        else
 
2772
                rec->parent_generation = 0;
 
2773
 
2565
2774
        rec->cache.start = start;
2566
2775
        rec->cache.size = nr;
2567
2776
        ret = insert_cache_extent(extent_cache, &rec->cache);
2583
2792
 
2584
2793
        cache = lookup_cache_extent(extent_cache, bytenr, 1);
2585
2794
        if (!cache) {
2586
 
                add_extent_rec(extent_cache, NULL, bytenr,
 
2795
                add_extent_rec(extent_cache, NULL, 0, bytenr,
2587
2796
                               1, 0, 0, 0, 0, 1, 0, 0);
2588
2797
                cache = lookup_cache_extent(extent_cache, bytenr, 1);
2589
2798
                if (!cache)
2631
2840
 
2632
2841
        cache = lookup_cache_extent(extent_cache, bytenr, 1);
2633
2842
        if (!cache) {
2634
 
                add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0,
 
2843
                add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
2635
2844
                               0, 0, max_size);
2636
2845
                cache = lookup_cache_extent(extent_cache, bytenr, 1);
2637
2846
                if (!cache)
2711
2920
        cache = search_cache_extent(reada, 0);
2712
2921
        if (cache) {
2713
2922
                bits[0].start = cache->start;
2714
 
                bits[1].size = cache->size;
 
2923
                bits[0].size = cache->size;
2715
2924
                *reada_bits = 1;
2716
2925
                return 1;
2717
2926
        }
3118
3327
#else
3119
3328
                BUG();
3120
3329
#endif
3121
 
                return add_extent_rec(extent_cache, NULL, key.objectid,
 
3330
                return add_extent_rec(extent_cache, NULL, 0, key.objectid,
3122
3331
                                      num_bytes, refs, 0, 0, 0, metadata, 1,
3123
3332
                                      num_bytes);
3124
3333
        }
3126
3335
        ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
3127
3336
        refs = btrfs_extent_refs(eb, ei);
3128
3337
 
3129
 
        add_extent_rec(extent_cache, NULL, key.objectid, num_bytes,
 
3338
        add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
3130
3339
                       refs, 0, 0, 0, metadata, 1, num_bytes);
3131
3340
 
3132
3341
        ptr = (unsigned long)(ei + 1);
3361
3570
        int ret;
3362
3571
        int error = 0;
3363
3572
 
3364
 
        if (btrfs_super_generation(root->fs_info->super_copy) !=
 
3573
        if (btrfs_super_cache_generation(root->fs_info->super_copy) != -1ULL &&
 
3574
            btrfs_super_generation(root->fs_info->super_copy) !=
3365
3575
            btrfs_super_cache_generation(root->fs_info->super_copy)) {
3366
3576
                printf("cache and super generation don't match, space cache "
3367
3577
                       "will be invalidated\n");
3600
3810
        return errors;
3601
3811
}
3602
3812
 
3603
 
static int run_next_block(struct btrfs_root *root,
 
3813
static int is_dropped_key(struct btrfs_key *key,
 
3814
                          struct btrfs_key *drop_key) {
 
3815
        if (key->objectid < drop_key->objectid)
 
3816
                return 1;
 
3817
        else if (key->objectid == drop_key->objectid) {
 
3818
                if (key->type < drop_key->type)
 
3819
                        return 1;
 
3820
                else if (key->type == drop_key->type) {
 
3821
                        if (key->offset < drop_key->offset)
 
3822
                                return 1;
 
3823
                }
 
3824
        }
 
3825
        return 0;
 
3826
}
 
3827
 
 
3828
static int run_next_block(struct btrfs_trans_handle *trans,
 
3829
                          struct btrfs_root *root,
3604
3830
                          struct block_info *bits,
3605
3831
                          int bits_nr,
3606
3832
                          u64 *last,
3612
3838
                          struct cache_tree *chunk_cache,
3613
3839
                          struct rb_root *dev_cache,
3614
3840
                          struct block_group_tree *block_group_cache,
3615
 
                          struct device_extent_tree *dev_extent_cache)
 
3841
                          struct device_extent_tree *dev_extent_cache,
 
3842
                          struct btrfs_root_item *ri)
3616
3843
{
3617
3844
        struct extent_buffer *buf;
3618
3845
        u64 bytenr;
3621
3848
        u64 owner;
3622
3849
        u64 flags;
3623
3850
        u64 ptr;
3624
 
        int ret;
 
3851
        u64 gen = 0;
 
3852
        int ret = 0;
3625
3853
        int i;
3626
3854
        int nritems;
3627
3855
        struct btrfs_key key;
3664
3892
                remove_cache_extent(nodes, cache);
3665
3893
                free(cache);
3666
3894
        }
3667
 
        cache = lookup_cache_extent(seen, bytenr, size);
 
3895
        cache = lookup_cache_extent(extent_cache, bytenr, size);
3668
3896
        if (cache) {
3669
 
                remove_cache_extent(seen, cache);
3670
 
                free(cache);
 
3897
                struct extent_record *rec;
 
3898
 
 
3899
                rec = container_of(cache, struct extent_record, cache);
 
3900
                gen = rec->parent_generation;
3671
3901
        }
3672
3902
 
3673
3903
        /* fixme, get the real parent transid */
3674
 
        buf = read_tree_block(root, bytenr, size, 0);
 
3904
        buf = read_tree_block(root, bytenr, size, gen);
3675
3905
        if (!extent_buffer_uptodate(buf)) {
3676
3906
                record_bad_block_io(root->fs_info,
3677
3907
                                    extent_cache, bytenr, size);
3680
3910
 
3681
3911
        nritems = btrfs_header_nritems(buf);
3682
3912
 
3683
 
        ret = btrfs_lookup_extent_info(NULL, root, bytenr,
 
3913
        /*
 
3914
         * FIXME, this only works only if we don't have any full
 
3915
         * backref mode.
 
3916
         */
 
3917
        if (!init_extent_tree) {
 
3918
                ret = btrfs_lookup_extent_info(NULL, root, bytenr,
3684
3919
                                       btrfs_header_level(buf), 1, NULL,
3685
3920
                                       &flags);
3686
 
        if (ret < 0)
3687
 
                flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
 
3921
                if (ret < 0)
 
3922
                        flags = 0;
 
3923
        } else {
 
3924
                flags = 0;
 
3925
        }
3688
3926
 
3689
3927
        if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
3690
3928
                parent = bytenr;
3694
3932
                owner = btrfs_header_owner(buf);
3695
3933
        }
3696
3934
 
3697
 
        ret = check_block(root, extent_cache, buf, flags);
 
3935
        ret = check_block(trans, root, extent_cache, buf, flags);
3698
3936
        if (ret)
3699
3937
                goto out;
3700
3938
 
3780
4018
                                        0, root->sectorsize);
3781
4019
                                continue;
3782
4020
                        }
 
4021
                        if (key.type == BTRFS_ORPHAN_ITEM_KEY) {
 
4022
                                struct bad_item *bad;
 
4023
 
 
4024
                                if (key.objectid == BTRFS_ORPHAN_OBJECTID)
 
4025
                                        continue;
 
4026
                                if (!owner)
 
4027
                                        continue;
 
4028
                                bad = malloc(sizeof(struct bad_item));
 
4029
                                if (!bad)
 
4030
                                        continue;
 
4031
                                INIT_LIST_HEAD(&bad->list);
 
4032
                                memcpy(&bad->key, &key,
 
4033
                                       sizeof(struct btrfs_key));
 
4034
                                bad->root_id = owner;
 
4035
                                list_add_tail(&bad->list, &delete_items);
 
4036
                                continue;
 
4037
                        }
3783
4038
                        if (key.type != BTRFS_EXTENT_DATA_KEY)
3784
4039
                                continue;
3785
4040
                        fi = btrfs_item_ptr(buf, i,
3802
4057
                                parent, owner, key.objectid, key.offset -
3803
4058
                                btrfs_file_extent_offset(buf, fi), 1, 1,
3804
4059
                                btrfs_file_extent_disk_num_bytes(buf, fi));
3805
 
                        BUG_ON(ret);
3806
4060
                }
3807
4061
        } else {
3808
4062
                int level;
3817
4071
                        ptr = btrfs_node_blockptr(buf, i);
3818
4072
                        size = btrfs_level_size(root, level - 1);
3819
4073
                        btrfs_node_key_to_cpu(buf, &key, i);
 
4074
                        if (ri != NULL) {
 
4075
                                struct btrfs_key drop_key;
 
4076
                                btrfs_disk_key_to_cpu(&drop_key,
 
4077
                                                      &ri->drop_progress);
 
4078
                                if ((level == ri->drop_level)
 
4079
                                    && is_dropped_key(&key, &drop_key)) {
 
4080
                                        continue;
 
4081
                                }
 
4082
                        }
3820
4083
                        ret = add_extent_rec(extent_cache, &key,
 
4084
                                             btrfs_node_ptr_generation(buf, i),
3821
4085
                                             ptr, size, 0, 0, 1, 0, 1, 0,
3822
4086
                                             size);
3823
4087
                        BUG_ON(ret);
3845
4109
                found_old_backref = 1;
3846
4110
out:
3847
4111
        free_extent_buffer(buf);
3848
 
        return 0;
 
4112
        return ret;
3849
4113
}
3850
4114
 
3851
4115
static int add_root_to_pending(struct extent_buffer *buf,
3859
4123
                add_pending(nodes, seen, buf->start, buf->len);
3860
4124
        else
3861
4125
                add_pending(pending, seen, buf->start, buf->len);
3862
 
        add_extent_rec(extent_cache, NULL, buf->start, buf->len,
 
4126
        add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
3863
4127
                       0, 1, 1, 0, 1, 0, buf->len);
3864
4128
 
3865
4129
        if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID ||
4841
5105
        int allocated = 0;
4842
5106
        u64 flags = 0;
4843
5107
 
4844
 
        /* remember our flags for recreating the extent */
4845
 
        ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start,
4846
 
                                       rec->max_size, rec->metadata, NULL,
4847
 
                                       &flags);
4848
 
        if (ret < 0)
4849
 
                flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
 
5108
        /*
 
5109
         * remember our flags for recreating the extent.
 
5110
         * FIXME, if we have cleared extent tree, we can not
 
5111
         * lookup extent info in extent tree.
 
5112
         */
 
5113
        if (!init_extent_tree) {
 
5114
                ret = btrfs_lookup_extent_info(NULL, info->extent_root,
 
5115
                                        rec->start, rec->max_size,
 
5116
                                        rec->metadata, NULL, &flags);
 
5117
                if (ret < 0)
 
5118
                        flags = 0;
 
5119
        } else {
 
5120
                flags = 0;
 
5121
        }
4850
5122
 
4851
5123
        path = btrfs_alloc_path();
4852
5124
        if (!path)
5446
5718
        struct btrfs_trans_handle *trans = NULL;
5447
5719
        int slot;
5448
5720
        struct btrfs_root_item ri;
 
5721
        struct list_head dropping_trees;
5449
5722
 
5450
5723
        dev_cache = RB_ROOT;
5451
5724
        cache_tree_init(&chunk_cache);
5458
5731
        cache_tree_init(&nodes);
5459
5732
        cache_tree_init(&reada);
5460
5733
        cache_tree_init(&corrupt_blocks);
 
5734
        INIT_LIST_HEAD(&dropping_trees);
5461
5735
 
5462
5736
        if (repair) {
5463
5737
                trans = btrfs_start_transaction(root, 1);
5510
5784
 
5511
5785
                        offset = btrfs_item_ptr_offset(leaf, path.slots[0]);
5512
5786
                        read_extent_buffer(leaf, &ri, offset, sizeof(ri));
5513
 
                        buf = read_tree_block(root->fs_info->tree_root,
5514
 
                                              btrfs_root_bytenr(&ri),
5515
 
                                              btrfs_level_size(root,
5516
 
                                               btrfs_root_level(&ri)), 0);
5517
 
                        add_root_to_pending(buf, &extent_cache, &pending,
5518
 
                                            &seen, &nodes, &found_key);
5519
 
                        free_extent_buffer(buf);
 
5787
                        if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
 
5788
                                buf = read_tree_block(root->fs_info->tree_root,
 
5789
                                                      btrfs_root_bytenr(&ri),
 
5790
                                                      btrfs_level_size(root,
 
5791
                                                      btrfs_root_level(&ri)),
 
5792
                                                      0);
 
5793
                                if (!buf) {
 
5794
                                        ret = -EIO;
 
5795
                                        goto out;
 
5796
                                }
 
5797
                                add_root_to_pending(buf, &extent_cache,
 
5798
                                                    &pending, &seen, &nodes,
 
5799
                                                    &found_key);
 
5800
                                free_extent_buffer(buf);
 
5801
                        } else {
 
5802
                                struct dropping_root_item_record *dri_rec;
 
5803
                                dri_rec = malloc(sizeof(*dri_rec));
 
5804
                                if (!dri_rec) {
 
5805
                                        perror("malloc");
 
5806
                                        exit(1);
 
5807
                                }
 
5808
                                memcpy(&dri_rec->ri, &ri, sizeof(ri));
 
5809
                                memcpy(&dri_rec->found_key, &found_key,
 
5810
                                       sizeof(found_key));
 
5811
                                list_add_tail(&dri_rec->list, &dropping_trees);
 
5812
                        }
5520
5813
                }
5521
5814
                path.slots[0]++;
5522
5815
        }
5523
5816
        btrfs_release_path(&path);
5524
 
        while(1) {
5525
 
                ret = run_next_block(root, bits, bits_nr, &last, &pending,
5526
 
                                     &seen, &reada, &nodes, &extent_cache,
5527
 
                                     &chunk_cache, &dev_cache,
5528
 
                                     &block_group_cache, &dev_extent_cache);
 
5817
        while (1) {
 
5818
                ret = run_next_block(trans, root, bits, bits_nr, &last,
 
5819
                                     &pending, &seen, &reada, &nodes,
 
5820
                                     &extent_cache, &chunk_cache, &dev_cache,
 
5821
                                     &block_group_cache, &dev_extent_cache,
 
5822
                                     NULL);
5529
5823
                if (ret != 0)
5530
5824
                        break;
5531
5825
        }
5532
5826
 
5533
 
        ret = check_extent_refs(trans, root, &extent_cache);
 
5827
        while (!list_empty(&dropping_trees)) {
 
5828
                struct dropping_root_item_record *rec;
 
5829
                struct extent_buffer *buf;
 
5830
                rec = list_entry(dropping_trees.next,
 
5831
                                 struct dropping_root_item_record, list);
 
5832
                last = 0;
 
5833
                if (!bits) {
 
5834
                        perror("realloc");
 
5835
                        exit(1);
 
5836
                }
 
5837
                buf = read_tree_block(root->fs_info->tree_root,
 
5838
                                      btrfs_root_bytenr(&rec->ri),
 
5839
                                      btrfs_level_size(root,
 
5840
                                      btrfs_root_level(&rec->ri)), 0);
 
5841
                if (!buf) {
 
5842
                        ret = -EIO;
 
5843
                        goto out;
 
5844
                }
 
5845
                add_root_to_pending(buf, &extent_cache, &pending,
 
5846
                                    &seen, &nodes, &rec->found_key);
 
5847
                while (1) {
 
5848
                        ret = run_next_block(trans, root, bits, bits_nr, &last,
 
5849
                                             &pending, &seen, &reada,
 
5850
                                             &nodes, &extent_cache,
 
5851
                                             &chunk_cache, &dev_cache,
 
5852
                                             &block_group_cache,
 
5853
                                             &dev_extent_cache,
 
5854
                                             &rec->ri);
 
5855
                        if (ret != 0)
 
5856
                                break;
 
5857
                }
 
5858
                free_extent_buffer(buf);
 
5859
                list_del(&rec->list);
 
5860
                free(rec);
 
5861
        }
 
5862
 
 
5863
        if (ret >= 0)
 
5864
                ret = check_extent_refs(trans, root, &extent_cache);
5534
5865
        if (ret == -EAGAIN) {
5535
5866
                ret = btrfs_commit_transaction(trans, root);
5536
5867
                if (ret)
5577
5908
        free_device_cache_tree(&dev_cache);
5578
5909
        free_block_group_tree(&block_group_cache);
5579
5910
        free_device_extent_tree(&dev_extent_cache);
 
5911
        free_extent_cache_tree(&seen);
 
5912
        free_extent_cache_tree(&pending);
 
5913
        free_extent_cache_tree(&reada);
 
5914
        free_extent_cache_tree(&nodes);
5580
5915
        return ret;
5581
5916
}
5582
5917
 
5586
5921
        struct extent_buffer *c;
5587
5922
        struct extent_buffer *old = root->node;
5588
5923
        int level;
 
5924
        int ret;
5589
5925
        struct btrfs_disk_key disk_key = {0,0,0};
5590
5926
 
5591
5927
        level = 0;
5602
5938
        if (IS_ERR(c)) {
5603
5939
                c = old;
5604
5940
                extent_buffer_get(c);
 
5941
                overwrite = 1;
5605
5942
        }
5606
5943
init:
5607
5944
        memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
5615
5952
                            btrfs_header_fsid(), BTRFS_FSID_SIZE);
5616
5953
 
5617
5954
        write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
5618
 
                            (unsigned long)btrfs_header_chunk_tree_uuid(c),
 
5955
                            btrfs_header_chunk_tree_uuid(c),
5619
5956
                            BTRFS_UUID_SIZE);
5620
5957
 
5621
5958
        btrfs_mark_buffer_dirty(c);
5622
 
 
 
5959
        /*
 
5960
         * this case can happen in the following case:
 
5961
         *
 
5962
         * 1.overwrite previous root.
 
5963
         *
 
5964
         * 2.reinit reloc data root, this is because we skip pin
 
5965
         * down reloc data tree before which means we can allocate
 
5966
         * same block bytenr here.
 
5967
         */
 
5968
        if (old->start == c->start) {
 
5969
                btrfs_set_root_generation(&root->root_item,
 
5970
                                          trans->transid);
 
5971
                root->root_item.level = btrfs_header_level(root->node);
 
5972
                ret = btrfs_update_root(trans, root->fs_info->tree_root,
 
5973
                                        &root->root_key, &root->root_item);
 
5974
                if (ret) {
 
5975
                        free_extent_buffer(c);
 
5976
                        return ret;
 
5977
                }
 
5978
        }
5623
5979
        free_extent_buffer(old);
5624
5980
        root->node = c;
5625
5981
        add_root_to_dirty_list(root);
5710
6066
 
5711
6067
static int reset_block_groups(struct btrfs_fs_info *fs_info)
5712
6068
{
 
6069
        struct btrfs_block_group_cache *cache;
5713
6070
        struct btrfs_path *path;
5714
6071
        struct extent_buffer *leaf;
5715
6072
        struct btrfs_chunk *chunk;
5716
6073
        struct btrfs_key key;
5717
6074
        int ret;
 
6075
        u64 start;
5718
6076
 
5719
6077
        path = btrfs_alloc_path();
5720
6078
        if (!path)
5765
6123
                                      btrfs_chunk_type(leaf, chunk),
5766
6124
                                      key.objectid, key.offset,
5767
6125
                                      btrfs_chunk_length(leaf, chunk));
 
6126
                set_extent_dirty(&fs_info->free_space_cache, key.offset,
 
6127
                                 key.offset + btrfs_chunk_length(leaf, chunk),
 
6128
                                 GFP_NOFS);
5768
6129
                path->slots[0]++;
5769
6130
        }
 
6131
        start = 0;
 
6132
        while (1) {
 
6133
                cache = btrfs_lookup_first_block_group(fs_info, start);
 
6134
                if (!cache)
 
6135
                        break;
 
6136
                cache->cached = 1;
 
6137
                start = cache->key.objectid + cache->key.offset;
 
6138
        }
5770
6139
 
5771
6140
        btrfs_free_path(path);
5772
6141
        return 0;
5795
6164
        if (ret) {
5796
6165
                if (ret > 0)
5797
6166
                        ret = 0;
5798
 
                goto out;
 
6167
                if (!ret)
 
6168
                        goto reinit_data_reloc;
 
6169
                else
 
6170
                        goto out;
5799
6171
        }
5800
6172
 
5801
6173
        ret = btrfs_del_item(trans, root, path);
5857
6229
        }
5858
6230
        btrfs_release_path(path);
5859
6231
 
 
6232
reinit_data_reloc:
5860
6233
        key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5861
6234
        key.type = BTRFS_ROOT_ITEM_KEY;
5862
6235
        key.offset = (u64)-1;
5872
6245
                extent_buffer_get(root->node);
5873
6246
        }
5874
6247
        ret = btrfs_fsck_reinit_root(trans, root, 0);
 
6248
        if (ret)
 
6249
                goto out;
 
6250
        ret = btrfs_make_root_dir(trans, root, BTRFS_FIRST_FREE_OBJECTID);
5875
6251
out:
5876
6252
        btrfs_free_path(path);
5877
6253
        return ret;
5878
6254
}
5879
6255
 
5880
 
static int reinit_extent_tree(struct btrfs_fs_info *fs_info)
 
6256
static int reinit_extent_tree(struct btrfs_trans_handle *trans,
 
6257
                              struct btrfs_fs_info *fs_info)
5881
6258
{
5882
 
        struct btrfs_trans_handle *trans;
5883
6259
        u64 start = 0;
5884
6260
        int ret;
5885
6261
 
5898
6274
                return -EINVAL;
5899
6275
        }
5900
6276
 
5901
 
        trans = btrfs_start_transaction(fs_info->extent_root, 1);
5902
 
        if (IS_ERR(trans)) {
5903
 
                fprintf(stderr, "Error starting transaction\n");
5904
 
                return PTR_ERR(trans);
5905
 
        }
5906
 
 
5907
6277
        /*
5908
6278
         * first we need to walk all of the trees except the extent tree and pin
5909
6279
         * down the bytes that are in use so we don't overwrite any existing
5927
6297
        }
5928
6298
 
5929
6299
        /* Ok we can allocate now, reinit the extent root */
5930
 
        ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 1);
 
6300
        ret = btrfs_fsck_reinit_root(trans, fs_info->extent_root, 0);
5931
6301
        if (ret) {
5932
6302
                fprintf(stderr, "extent root initialization failed\n");
5933
6303
                /*
5938
6308
                return ret;
5939
6309
        }
5940
6310
 
5941
 
        ret = reset_balance(trans, fs_info);
5942
 
        if (ret) {
5943
 
                fprintf(stderr, "error reseting the pending balance\n");
5944
 
                return ret;
5945
 
        }
5946
 
 
5947
6311
        /*
5948
6312
         * Now we have all the in-memory block groups setup so we can make
5949
6313
         * allocations properly, and the metadata we care about is safe since we
5966
6330
                btrfs_extent_post_op(trans, fs_info->extent_root);
5967
6331
        }
5968
6332
 
5969
 
        /*
5970
 
         * Ok now we commit and run the normal fsck, which will add extent
5971
 
         * entries for all of the items it finds.
5972
 
         */
5973
 
        return btrfs_commit_transaction(trans, fs_info->extent_root);
 
6333
        ret = reset_balance(trans, fs_info);
 
6334
        if (ret)
 
6335
                fprintf(stderr, "error reseting the pending balance\n");
 
6336
 
 
6337
        return ret;
5974
6338
}
5975
6339
 
5976
6340
static int recow_extent_buffer(struct btrfs_root *root, struct extent_buffer *eb)
6014
6378
        return ret;
6015
6379
}
6016
6380
 
 
6381
static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
 
6382
{
 
6383
        struct btrfs_path *path;
 
6384
        struct btrfs_trans_handle *trans;
 
6385
        struct btrfs_key key;
 
6386
        int ret;
 
6387
 
 
6388
        printf("Deleting bad item [%llu,%u,%llu]\n", bad->key.objectid,
 
6389
               bad->key.type, bad->key.offset);
 
6390
        key.objectid = bad->root_id;
 
6391
        key.type = BTRFS_ROOT_ITEM_KEY;
 
6392
        key.offset = (u64)-1;
 
6393
 
 
6394
        root = btrfs_read_fs_root(root->fs_info, &key);
 
6395
        if (IS_ERR(root)) {
 
6396
                fprintf(stderr, "Couldn't find owner root %llu\n",
 
6397
                        key.objectid);
 
6398
                return PTR_ERR(root);
 
6399
        }
 
6400
 
 
6401
        path = btrfs_alloc_path();
 
6402
        if (!path)
 
6403
                return -ENOMEM;
 
6404
 
 
6405
        trans = btrfs_start_transaction(root, 1);
 
6406
        if (IS_ERR(trans)) {
 
6407
                btrfs_free_path(path);
 
6408
                return PTR_ERR(trans);
 
6409
        }
 
6410
 
 
6411
        ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
 
6412
        if (ret) {
 
6413
                if (ret > 0)
 
6414
                        ret = 0;
 
6415
                goto out;
 
6416
        }
 
6417
        ret = btrfs_del_item(trans, root, path);
 
6418
out:
 
6419
        btrfs_commit_transaction(trans, root);
 
6420
        btrfs_free_path(path);
 
6421
        return ret;
 
6422
}
 
6423
 
6017
6424
static struct option long_options[] = {
6018
6425
        { "super", 1, NULL, 's' },
6019
6426
        { "repair", 0, NULL, 0 },
6041
6448
        struct btrfs_root *root;
6042
6449
        struct btrfs_fs_info *info;
6043
6450
        u64 bytenr = 0;
6044
 
        char uuidbuf[37];
 
6451
        char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
6045
6452
        int ret;
6046
 
        int num;
 
6453
        u64 num;
6047
6454
        int option_index = 0;
6048
6455
        int init_csum_tree = 0;
6049
 
        int init_extent_tree = 0;
6050
 
        enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_PARTIAL;
 
6456
        enum btrfs_open_ctree_flags ctree_flags =
 
6457
                OPEN_CTREE_PARTIAL | OPEN_CTREE_EXCLUSIVE;
6051
6458
 
6052
6459
        while(1) {
6053
6460
                int c;
6061
6468
                                ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
6062
6469
                                break;
6063
6470
                        case 's':
6064
 
                                num = atol(optarg);
6065
 
                                bytenr = btrfs_sb_offset(num);
6066
 
                                printf("using SB copy %d, bytenr %llu\n", num,
 
6471
                                num = arg_strtou64(optarg);
 
6472
                                if (num >= BTRFS_SUPER_MIRROR_MAX) {
 
6473
                                        fprintf(stderr,
 
6474
                                                "ERROR: super mirror should be less than: %d\n",
 
6475
                                                BTRFS_SUPER_MIRROR_MAX);
 
6476
                                        exit(1);
 
6477
                                }
 
6478
                                bytenr = btrfs_sb_offset(((int)num));
 
6479
                                printf("using SB copy %llu, bytenr %llu\n", num,
6067
6480
                                       (unsigned long long)bytenr);
6068
6481
                                break;
6069
6482
                        case '?':
6077
6490
                } else if (option_index == 2) {
6078
6491
                        printf("Creating a new CRC tree\n");
6079
6492
                        init_csum_tree = 1;
 
6493
                        repair = 1;
6080
6494
                        ctree_flags |= OPEN_CTREE_WRITES;
6081
6495
                } else if (option_index == 3) {
6082
6496
                        init_extent_tree = 1;
6096
6510
 
6097
6511
        if((ret = check_mounted(argv[optind])) < 0) {
6098
6512
                fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
6099
 
                return ret;
 
6513
                goto err_out;
6100
6514
        } else if(ret) {
6101
6515
                fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
6102
 
                return -EBUSY;
 
6516
                ret = -EBUSY;
 
6517
                goto err_out;
6103
6518
        }
6104
6519
 
6105
6520
        info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
6106
6521
        if (!info) {
6107
6522
                fprintf(stderr, "Couldn't open file system\n");
6108
 
                return -EIO;
 
6523
                ret = -EIO;
 
6524
                goto err_out;
6109
6525
        }
6110
6526
 
 
6527
        root = info->fs_root;
6111
6528
        uuid_unparse(info->super_copy->fsid, uuidbuf);
6112
6529
        printf("Checking filesystem on %s\nUUID: %s\n", argv[optind], uuidbuf);
6113
6530
 
6114
6531
        if (!extent_buffer_uptodate(info->tree_root->node) ||
6115
6532
            !extent_buffer_uptodate(info->dev_root->node) ||
6116
 
            !extent_buffer_uptodate(info->extent_root->node) ||
6117
6533
            !extent_buffer_uptodate(info->chunk_root->node)) {
6118
6534
                fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6119
 
                return -EIO;
 
6535
                ret = -EIO;
 
6536
                goto close_out;
6120
6537
        }
6121
6538
 
6122
 
        root = info->fs_root;
6123
 
 
6124
 
        if (init_extent_tree) {
6125
 
                printf("Creating a new extent tree\n");
6126
 
                ret = reinit_extent_tree(info);
 
6539
        if (init_extent_tree || init_csum_tree) {
 
6540
                struct btrfs_trans_handle *trans;
 
6541
 
 
6542
                trans = btrfs_start_transaction(info->extent_root, 0);
 
6543
                if (IS_ERR(trans)) {
 
6544
                        fprintf(stderr, "Error starting transaction\n");
 
6545
                        ret = PTR_ERR(trans);
 
6546
                        goto close_out;
 
6547
                }
 
6548
 
 
6549
                if (init_extent_tree) {
 
6550
                        printf("Creating a new extent tree\n");
 
6551
                        ret = reinit_extent_tree(trans, info);
 
6552
                        if (ret)
 
6553
                                goto close_out;
 
6554
                }
 
6555
 
 
6556
                if (init_csum_tree) {
 
6557
                        fprintf(stderr, "Reinit crc root\n");
 
6558
                        ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
 
6559
                        if (ret) {
 
6560
                                fprintf(stderr, "crc root initialization failed\n");
 
6561
                                ret = -EIO;
 
6562
                                goto close_out;
 
6563
                        }
 
6564
                }
 
6565
                /*
 
6566
                 * Ok now we commit and run the normal fsck, which will add
 
6567
                 * extent entries for all of the items it finds.
 
6568
                 */
 
6569
                ret = btrfs_commit_transaction(trans, info->extent_root);
6127
6570
                if (ret)
6128
 
                        return ret;
6129
 
        }
 
6571
                        goto close_out;
 
6572
        }
 
6573
        if (!extent_buffer_uptodate(info->extent_root->node)) {
 
6574
                fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
 
6575
                ret = -EIO;
 
6576
                goto close_out;
 
6577
        }
 
6578
 
6130
6579
        fprintf(stderr, "checking extents\n");
6131
 
        if (init_csum_tree) {
6132
 
                struct btrfs_trans_handle *trans;
6133
 
 
6134
 
                fprintf(stderr, "Reinit crc root\n");
6135
 
                trans = btrfs_start_transaction(info->csum_root, 1);
6136
 
                if (IS_ERR(trans)) {
6137
 
                        fprintf(stderr, "Error starting transaction\n");
6138
 
                        return PTR_ERR(trans);
6139
 
                }
6140
 
 
6141
 
                ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6142
 
                if (ret) {
6143
 
                        fprintf(stderr, "crc root initialization failed\n");
6144
 
                        return -EIO;
6145
 
                }
6146
 
 
6147
 
                ret = btrfs_commit_transaction(trans, info->csum_root);
6148
 
                if (ret)
6149
 
                        exit(1);
6150
 
                goto out;
6151
 
        }
6152
6580
        ret = check_chunks_and_extents(root);
6153
6581
        if (ret)
6154
6582
                fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");
6158
6586
        if (ret)
6159
6587
                goto out;
6160
6588
 
 
6589
        /*
 
6590
         * We used to have to have these hole extents in between our real
 
6591
         * extents so if we don't have this flag set we need to make sure there
 
6592
         * are no gaps in the file extents for inodes, otherwise we can just
 
6593
         * ignore it when this happens.
 
6594
         */
 
6595
        no_holes = btrfs_fs_incompat(root->fs_info,
 
6596
                                     BTRFS_FEATURE_INCOMPAT_NO_HOLES);
6161
6597
        fprintf(stderr, "checking fs roots\n");
6162
6598
        ret = check_fs_roots(root, &root_cache);
6163
6599
        if (ret)
6183
6619
                        break;
6184
6620
        }
6185
6621
 
 
6622
        while (!list_empty(&delete_items)) {
 
6623
                struct bad_item *bad;
 
6624
 
 
6625
                bad = list_first_entry(&delete_items, struct bad_item, list);
 
6626
                list_del_init(&bad->list);
 
6627
                if (repair)
 
6628
                        ret = delete_bad_item(root, bad);
 
6629
                free(bad);
 
6630
        }
 
6631
 
6186
6632
        if (!list_empty(&root->fs_info->recow_ebs)) {
6187
6633
                fprintf(stderr, "Transid errors in file system\n");
6188
6634
                ret = 1;
6189
6635
        }
6190
6636
out:
6191
 
        free_root_recs_tree(&root_cache);
6192
 
        close_ctree(root);
6193
 
 
6194
6637
        if (found_old_backref) { /*
6195
6638
                 * there was a disk format change when mixed
6196
6639
                 * backref was in testing tree. The old format
6217
6660
                (unsigned long long)data_bytes_allocated,
6218
6661
                (unsigned long long)data_bytes_referenced);
6219
6662
        printf("%s\n", BTRFS_BUILD_VERSION);
 
6663
 
 
6664
        free_root_recs_tree(&root_cache);
 
6665
close_out:
 
6666
        close_ctree(root);
 
6667
err_out:
6220
6668
        return ret;
6221
6669
}