1463
1471
printf("reset isize for dir %Lu root %Lu\n", rec->ino,
1464
1472
root->root_key.objectid);
1474
btrfs_release_path(path);
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)
1483
struct btrfs_key key;
1486
key.objectid = BTRFS_ORPHAN_OBJECTID;
1487
key.type = BTRFS_ORPHAN_ITEM_KEY;
1488
key.offset = rec->ino;
1490
ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1491
btrfs_release_path(path);
1493
rec->errors &= ~I_ERR_NO_ORPHAN_ITEM;
1497
static int try_repair_inode(struct btrfs_root *root, struct inode_record *rec)
1499
struct btrfs_trans_handle *trans;
1500
struct btrfs_path *path;
1503
/* So far we just fix dir isize wrong */
1504
if (!(rec->errors & (I_ERR_DIR_ISIZE_WRONG | I_ERR_NO_ORPHAN_ITEM)))
1507
path = btrfs_alloc_path();
1511
trans = btrfs_start_transaction(root, 1);
1512
if (IS_ERR(trans)) {
1513
btrfs_free_path(path);
1514
return PTR_ERR(trans);
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);
2283
2337
return btrfs_add_corrupt_extent_record(info, &key, start, len, 0);
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)
2343
if (btrfs_header_level(buf)) {
2344
struct btrfs_key_ptr ptr1, ptr2;
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));
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);
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;
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);
2378
item1_data = malloc(item1_size);
2381
item2_data = malloc(item2_size);
2387
read_extent_buffer(buf, item1_data, item1_offset, item1_size);
2388
read_extent_buffer(buf, item2_data, item2_offset, item2_size);
2390
write_extent_buffer(buf, item1_data, item2_offset, item2_size);
2391
write_extent_buffer(buf, item2_data, item1_offset, item1_size);
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);
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);
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.
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)
2418
struct btrfs_path *path;
2419
struct btrfs_key k1, k2;
2423
if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
2426
k1.objectid = btrfs_header_owner(buf);
2427
k1.type = BTRFS_ROOT_ITEM_KEY;
2428
k1.offset = (u64)-1;
2430
root = btrfs_read_fs_root(root->fs_info, &k1);
2434
path = btrfs_alloc_path();
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);
2443
btrfs_item_key_to_cpu(buf, &k1, 0);
2445
ret = btrfs_search_slot(trans, root, &k1, path, 0, 1);
2447
btrfs_free_path(path);
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);
2457
btrfs_item_key_to_cpu(buf, &k1, i);
2458
btrfs_item_key_to_cpu(buf, &k2, i + 1);
2460
if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
2462
ret = swap_values(root, path, buf, i);
2465
btrfs_mark_buffer_dirty(buf);
2469
btrfs_free_path(path);
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)
2290
2478
struct extent_record *rec;
2291
2479
struct cache_extent *cache;
2292
2480
struct btrfs_key key;
2481
enum btrfs_tree_block_status status;
2296
2485
cache = lookup_cache_extent(extent_cache, buf->start, buf->len);
2312
2501
rec->info_level = level;
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);
2317
ret = btrfs_check_node(root, &rec->parent_key, buf);
2506
status = btrfs_check_node(root, &rec->parent_key, buf);
2320
fprintf(stderr, "bad block %llu\n",
2321
(unsigned long long)buf->start);
2508
if (status != BTRFS_TREE_BLOCK_CLEAN) {
2510
status = try_to_fix_bad_block(trans, root, buf,
2513
if (status != BTRFS_TREE_BLOCK_CLEAN) {
2515
fprintf(stderr, "bad block %llu\n",
2516
(unsigned long long)buf->start);
2519
* Signal to callers we need to start the scan over
2520
* again since we'll have cow'ed blocks.
2323
2525
rec->content_checked = 1;
2324
2526
if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)
3664
3892
remove_cache_extent(nodes, cache);
3667
cache = lookup_cache_extent(seen, bytenr, size);
3895
cache = lookup_cache_extent(extent_cache, bytenr, size);
3669
remove_cache_extent(seen, cache);
3897
struct extent_record *rec;
3899
rec = container_of(cache, struct extent_record, cache);
3900
gen = rec->parent_generation;
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);
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)),
5797
add_root_to_pending(buf, &extent_cache,
5798
&pending, &seen, &nodes,
5800
free_extent_buffer(buf);
5802
struct dropping_root_item_record *dri_rec;
5803
dri_rec = malloc(sizeof(*dri_rec));
5808
memcpy(&dri_rec->ri, &ri, sizeof(ri));
5809
memcpy(&dri_rec->found_key, &found_key,
5811
list_add_tail(&dri_rec->list, &dropping_trees);
5521
5814
path.slots[0]++;
5523
5816
btrfs_release_path(&path);
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);
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,
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);
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);
5845
add_root_to_pending(buf, &extent_cache, &pending,
5846
&seen, &nodes, &rec->found_key);
5848
ret = run_next_block(trans, root, bits, bits_nr, &last,
5849
&pending, &seen, &reada,
5850
&nodes, &extent_cache,
5851
&chunk_cache, &dev_cache,
5858
free_extent_buffer(buf);
5859
list_del(&rec->list);
5864
ret = check_extent_refs(trans, root, &extent_cache);
5534
5865
if (ret == -EAGAIN) {
5535
5866
ret = btrfs_commit_transaction(trans, root);
5615
5952
btrfs_header_fsid(), BTRFS_FSID_SIZE);
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);
5621
5958
btrfs_mark_buffer_dirty(c);
5960
* this case can happen in the following case:
5962
* 1.overwrite previous root.
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.
5968
if (old->start == c->start) {
5969
btrfs_set_root_generation(&root->root_item,
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);
5975
free_extent_buffer(c);
5623
5979
free_extent_buffer(old);
5624
5980
root->node = c;
5625
5981
add_root_to_dirty_list(root);
6381
static int delete_bad_item(struct btrfs_root *root, struct bad_item *bad)
6383
struct btrfs_path *path;
6384
struct btrfs_trans_handle *trans;
6385
struct btrfs_key key;
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;
6394
root = btrfs_read_fs_root(root->fs_info, &key);
6396
fprintf(stderr, "Couldn't find owner root %llu\n",
6398
return PTR_ERR(root);
6401
path = btrfs_alloc_path();
6405
trans = btrfs_start_transaction(root, 1);
6406
if (IS_ERR(trans)) {
6407
btrfs_free_path(path);
6408
return PTR_ERR(trans);
6411
ret = btrfs_search_slot(trans, root, &bad->key, path, -1, 1);
6417
ret = btrfs_del_item(trans, root, path);
6419
btrfs_commit_transaction(trans, root);
6420
btrfs_free_path(path);
6017
6424
static struct option long_options[] = {
6018
6425
{ "super", 1, NULL, 's' },
6019
6426
{ "repair", 0, NULL, 0 },
6097
6511
if((ret = check_mounted(argv[optind])) < 0) {
6098
6512
fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
6100
6514
} else if(ret) {
6101
6515
fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]);
6105
6520
info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
6107
6522
fprintf(stderr, "Couldn't open file system\n");
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);
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");
6122
root = info->fs_root;
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;
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);
6549
if (init_extent_tree) {
6550
printf("Creating a new extent tree\n");
6551
ret = reinit_extent_tree(trans, info);
6556
if (init_csum_tree) {
6557
fprintf(stderr, "Reinit crc root\n");
6558
ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6560
fprintf(stderr, "crc root initialization failed\n");
6566
* Ok now we commit and run the normal fsck, which will add
6567
* extent entries for all of the items it finds.
6569
ret = btrfs_commit_transaction(trans, info->extent_root);
6573
if (!extent_buffer_uptodate(info->extent_root->node)) {
6574
fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n");
6130
6579
fprintf(stderr, "checking extents\n");
6131
if (init_csum_tree) {
6132
struct btrfs_trans_handle *trans;
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);
6141
ret = btrfs_fsck_reinit_root(trans, info->csum_root, 0);
6143
fprintf(stderr, "crc root initialization failed\n");
6147
ret = btrfs_commit_transaction(trans, info->csum_root);
6152
6580
ret = check_chunks_and_extents(root);
6154
6582
fprintf(stderr, "Errors found in extent allocation tree or chunk allocation\n");