Skip to content

Instantly share code, notes, and snippets.

@naota
Created June 4, 2013 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naota/5703328 to your computer and use it in GitHub Desktop.
Save naota/5703328 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <uuid/uuid.h>
#include "kerncompat.h"
#include "ctree.h"
#include "disk-io.h"
#include "volumes.h"
#include "print-tree.h"
int main()
{
struct btrfs_root *root;
struct btrfs_fs_info *info;
struct extent_buffer *leaf;
struct btrfs_item *item;
struct btrfs_multi_bio *multi = NULL;
struct btrfs_device *device;
int mirror_num, num_copies;
u64 length;
/* problem block number */
u64 block = 387949473792ULL;
/* open btrfs */
info = open_ctree_fs_info("/dev/sda2", 0, 0, 1, 1);
root = info->fs_root;
/* rewrite the FS :-) */
/* write extent buffers */
leaf = btrfs_find_create_tree_block(root, block, root->leafsize);
mirror_num = 0;
while(1) {
/* read the leaf */
btrfs_map_block(&root->fs_info->mapping_tree, READ,
leaf->start, &length, &multi,
mirror_num, NULL);
device = multi->stripes[0].dev;
leaf->fd = device->fd;
device->total_ios++;
leaf->dev_bytenr = multi->stripes[0].physical;
kfree(multi);
read_extent_from_disk(leaf, 0, leaf->len);
/* rewrite item 6 offset */
item = btrfs_item_nr(leaf, 6);
btrfs_set_item_offset(leaf, item, 3703); // was 69239
/* rewrite checksum */
csum_tree_block(root, leaf, 0);
// btrfs_print_leaf(root, leaf);
/* write to the disk */
write_extent_to_disk(leaf);
fsync(leaf->fd);
/* get number of mirrors of the leaf */
num_copies = btrfs_num_copies(&root->fs_info->mapping_tree,
leaf->start, leaf->len);
if (num_copies == 1) break;
mirror_num++;
if (mirror_num > num_copies) break;
}
free_extent_buffer(leaf);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment