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

« back to all changes in this revision

Viewing changes to btrfs-corrupt-block.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:
108
108
        fprintf(stderr, "\t-K The key to corrupt in the format "
109
109
                "<num>,<num>,<num> (must also specify -f for the field)\n");
110
110
        fprintf(stderr, "\t-f The field in the item to corrupt\n");
 
111
        fprintf(stderr, "\t-I An item to corrupt (must also specify the field "
 
112
                "to corrupt and a root+key for the item)\n");
 
113
        fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
111
114
        exit(1);
112
115
}
113
116
 
301
304
        BTRFS_FILE_EXTENT_BAD,
302
305
};
303
306
 
 
307
enum btrfs_dir_item_field {
 
308
        BTRFS_DIR_ITEM_NAME,
 
309
        BTRFS_DIR_ITEM_LOCATION_OBJECTID,
 
310
        BTRFS_DIR_ITEM_BAD,
 
311
};
 
312
 
304
313
enum btrfs_metadata_block_field {
305
314
        BTRFS_METADATA_BLOCK_GENERATION,
 
315
        BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
306
316
        BTRFS_METADATA_BLOCK_BAD,
307
317
};
308
318
 
 
319
enum btrfs_item_field {
 
320
        BTRFS_ITEM_OFFSET,
 
321
        BTRFS_ITEM_BAD,
 
322
};
 
323
 
309
324
enum btrfs_key_field {
310
325
        BTRFS_KEY_OBJECTID,
311
326
        BTRFS_KEY_TYPE,
332
347
{
333
348
        if (!strncmp(field, "generation", FIELD_BUF_LEN))
334
349
                return BTRFS_METADATA_BLOCK_GENERATION;
 
350
        if (!strncmp(field, "shift_items", FIELD_BUF_LEN))
 
351
                return BTRFS_METADATA_BLOCK_SHIFT_ITEMS;
335
352
        return BTRFS_METADATA_BLOCK_BAD;
336
353
}
337
354
 
346
363
        return BTRFS_KEY_BAD;
347
364
}
348
365
 
 
366
static enum btrfs_item_field convert_item_field(char *field)
 
367
{
 
368
        if (!strncmp(field, "offset", FIELD_BUF_LEN))
 
369
                return BTRFS_ITEM_OFFSET;
 
370
        return BTRFS_ITEM_BAD;
 
371
}
 
372
 
 
373
static enum btrfs_dir_item_field convert_dir_item_field(char *field)
 
374
{
 
375
        if (!strncmp(field, "name", FIELD_BUF_LEN))
 
376
                return BTRFS_DIR_ITEM_NAME;
 
377
        if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
 
378
                return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
 
379
        return BTRFS_DIR_ITEM_BAD;
 
380
}
 
381
 
349
382
static u64 generate_u64(u64 orig)
350
383
{
351
384
        u64 ret;
355
388
        return ret;
356
389
}
357
390
 
 
391
static u32 generate_u32(u32 orig)
 
392
{
 
393
        u32 ret;
 
394
        do {
 
395
                ret = rand();
 
396
        } while (ret == orig);
 
397
        return ret;
 
398
}
 
399
 
358
400
static u8 generate_u8(u8 orig)
359
401
{
360
402
        u8 ret;
421
463
        return ret;
422
464
}
423
465
 
 
466
static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
 
467
                            char *field)
 
468
{
 
469
        struct btrfs_trans_handle *trans;
 
470
        struct btrfs_dir_item *di;
 
471
        struct btrfs_path *path;
 
472
        char *name;
 
473
        struct btrfs_key location;
 
474
        struct btrfs_disk_key disk_key;
 
475
        unsigned long name_ptr;
 
476
        enum btrfs_dir_item_field corrupt_field =
 
477
                convert_dir_item_field(field);
 
478
        u64 bogus;
 
479
        u16 name_len;
 
480
        int ret;
 
481
 
 
482
        if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
 
483
                fprintf(stderr, "Invalid field %s\n", field);
 
484
                return -EINVAL;
 
485
        }
 
486
 
 
487
        path = btrfs_alloc_path();
 
488
        if (!path)
 
489
                return -ENOMEM;
 
490
 
 
491
        trans = btrfs_start_transaction(root, 1);
 
492
        if (IS_ERR(trans)) {
 
493
                btrfs_free_path(path);
 
494
                return PTR_ERR(trans);
 
495
        }
 
496
 
 
497
        ret = btrfs_search_slot(trans, root, key, path, 0, 1);
 
498
        if (ret) {
 
499
                if (ret > 0)
 
500
                        ret = -ENOENT;
 
501
                fprintf(stderr, "Error searching for dir item %d\n", ret);
 
502
                goto out;
 
503
        }
 
504
 
 
505
        di = btrfs_item_ptr(path->nodes[0], path->slots[0],
 
506
                            struct btrfs_dir_item);
 
507
 
 
508
        switch (corrupt_field) {
 
509
        case BTRFS_DIR_ITEM_NAME:
 
510
                name_len = btrfs_dir_name_len(path->nodes[0], di);
 
511
                name = malloc(name_len);
 
512
                if (!name) {
 
513
                        ret = -ENOMEM;
 
514
                        goto out;
 
515
                }
 
516
                name_ptr = (unsigned long)(di + 1);
 
517
                read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
 
518
                name[0]++;
 
519
                write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
 
520
                btrfs_mark_buffer_dirty(path->nodes[0]);
 
521
                free(name);
 
522
                goto out;
 
523
        case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
 
524
                btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
 
525
                bogus = generate_u64(location.objectid);
 
526
                location.objectid = bogus;
 
527
                btrfs_cpu_key_to_disk(&disk_key, &location);
 
528
                btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
 
529
                btrfs_mark_buffer_dirty(path->nodes[0]);
 
530
                goto out;
 
531
        default:
 
532
                ret = -EINVAL;
 
533
                goto out;
 
534
        }
 
535
out:
 
536
        btrfs_commit_transaction(trans, root);
 
537
        btrfs_free_path(path);
 
538
        return ret;
 
539
}
424
540
 
425
541
static int corrupt_inode(struct btrfs_trans_handle *trans,
426
542
                         struct btrfs_root *root, u64 inode, char *field)
538
654
        return ret;
539
655
}
540
656
 
 
657
static void shift_items(struct btrfs_root *root, struct extent_buffer *eb)
 
658
{
 
659
        int nritems = btrfs_header_nritems(eb);
 
660
        int shift_space = btrfs_leaf_free_space(root, eb) / 2;
 
661
        int slot = nritems / 2;
 
662
        int i = 0;
 
663
        unsigned int data_end = btrfs_item_offset_nr(eb, nritems - 1);
 
664
 
 
665
        /* Shift the item data up to and including slot back by shift space */
 
666
        memmove_extent_buffer(eb, btrfs_leaf_data(eb) + data_end - shift_space,
 
667
                              btrfs_leaf_data(eb) + data_end,
 
668
                              btrfs_item_offset_nr(eb, slot - 1) - data_end);
 
669
 
 
670
        /* Now update the item pointers. */
 
671
        for (i = nritems - 1; i >= slot; i--) {
 
672
                u32 offset = btrfs_item_offset_nr(eb, i);
 
673
                offset -= shift_space;
 
674
                btrfs_set_item_offset(eb, btrfs_item_nr(i), offset);
 
675
        }
 
676
}
 
677
 
541
678
static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
542
679
                                  char *field)
543
680
{
608
745
                bogus = generate_u64(orig);
609
746
                btrfs_set_header_generation(eb, bogus);
610
747
                break;
 
748
        case BTRFS_METADATA_BLOCK_SHIFT_ITEMS:
 
749
                shift_items(root, path->nodes[level]);
 
750
                break;
611
751
        default:
612
752
                ret = -EINVAL;
613
753
                break;
619
759
        return ret;
620
760
}
621
761
 
 
762
static int corrupt_btrfs_item(struct btrfs_root *root, struct btrfs_key *key,
 
763
                              char *field)
 
764
{
 
765
        struct btrfs_trans_handle *trans;
 
766
        struct btrfs_path *path;
 
767
        enum btrfs_item_field corrupt_field;
 
768
        u32 orig, bogus;
 
769
        int ret;
 
770
 
 
771
        corrupt_field = convert_item_field(field);
 
772
        if (corrupt_field == BTRFS_ITEM_BAD) {
 
773
                fprintf(stderr, "Invalid field %s\n", field);
 
774
                return -EINVAL;
 
775
        }
 
776
 
 
777
        path = btrfs_alloc_path();
 
778
        if (!path)
 
779
                return -ENOMEM;
 
780
 
 
781
        trans = btrfs_start_transaction(root, 1);
 
782
        if (IS_ERR(trans)) {
 
783
                btrfs_free_path(path);
 
784
                fprintf(stderr, "Couldn't start transaction %ld\n",
 
785
                        PTR_ERR(trans));
 
786
                return PTR_ERR(trans);
 
787
        }
 
788
 
 
789
        ret = btrfs_search_slot(trans, root, key, path, 0, 1);
 
790
        if (ret != 0) {
 
791
                fprintf(stderr, "Error searching to node %d\n", ret);
 
792
                goto out;
 
793
        }
 
794
 
 
795
        ret = 0;
 
796
        switch (corrupt_field) {
 
797
        case BTRFS_ITEM_OFFSET:
 
798
                orig = btrfs_item_offset_nr(path->nodes[0], path->slots[0]);
 
799
                bogus = generate_u32(orig);
 
800
                btrfs_set_item_offset(path->nodes[0],
 
801
                                      btrfs_item_nr(path->slots[0]), bogus);
 
802
                break;
 
803
        default:
 
804
                ret = -EINVAL;
 
805
                break;
 
806
        }
 
807
        btrfs_mark_buffer_dirty(path->nodes[0]);
 
808
out:
 
809
        btrfs_commit_transaction(trans, root);
 
810
        btrfs_free_path(path);
 
811
        return ret;
 
812
}
 
813
 
622
814
static struct option long_options[] = {
623
815
        /* { "byte-count", 1, NULL, 'b' }, */
624
816
        { "logical", 1, NULL, 'l' },
634
826
        { "metadata-block", 1, NULL, 'm'},
635
827
        { "field", 1, NULL, 'f'},
636
828
        { "key", 1, NULL, 'K'},
 
829
        { "item", 0, NULL, 'I'},
 
830
        { "dir-item", 0, NULL, 'D'},
637
831
        { 0, 0, 0, 0}
638
832
};
639
833
 
797
991
        int corrupt_block_keys = 0;
798
992
        int chunk_rec = 0;
799
993
        int chunk_tree = 0;
 
994
        int corrupt_item = 0;
 
995
        int corrupt_di = 0;
800
996
        u64 metadata_block = 0;
801
997
        u64 inode = 0;
802
998
        u64 file_extent = (u64)-1;
808
1004
 
809
1005
        while(1) {
810
1006
                int c;
811
 
                c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:", long_options,
 
1007
                c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:ID", long_options,
812
1008
                                &option_index);
813
1009
                if (c < 0)
814
1010
                        break;
859
1055
                                        print_usage();
860
1056
                                }
861
1057
                                break;
 
1058
                        case 'D':
 
1059
                                corrupt_di = 1;
 
1060
                                break;
 
1061
                        case 'I':
 
1062
                                corrupt_item = 1;
 
1063
                                break;
862
1064
                        default:
863
1065
                                print_usage();
864
1066
                }
955
1157
                ret = corrupt_metadata_block(root, metadata_block, field);
956
1158
                goto out_close;
957
1159
        }
 
1160
        if (corrupt_di) {
 
1161
                if (!key.objectid || !strlen(field))
 
1162
                        print_usage();
 
1163
                ret = corrupt_dir_item(root, &key, field);
 
1164
                goto out_close;
 
1165
        }
 
1166
        if (corrupt_item) {
 
1167
                if (!key.objectid)
 
1168
                        print_usage();
 
1169
                ret = corrupt_btrfs_item(root, &key, field);
 
1170
                goto out_close;
 
1171
        }
958
1172
        if (key.objectid || key.offset || key.type) {
959
1173
                if (!strlen(field))
960
1174
                        print_usage();