diff --git a/.gitignore b/.gitignore index b606ca1..902d3b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,16 @@ -btrfs-progs-0.19.tar.bz2 +*.o +.*.o.d +version.h +man/*.gz +btrfs +btrfs-debug-tree +btrfs-map-logical +btrfs-show +btrfs-vol +btrfsck +btrfsctl +find-root +mkfs.btrfs +repair +restore +/btrfs-progs-0.19.20120817git043a639.tar.bz2 diff --git a/Btrfs-progs-add-btrfs-device-ready-command.patch b/Btrfs-progs-add-btrfs-device-ready-command.patch new file mode 100644 index 0000000..b59b9d1 --- /dev/null +++ b/Btrfs-progs-add-btrfs-device-ready-command.patch @@ -0,0 +1,84 @@ +From 01077c81987011d8a50999824f96032c10b2733b Mon Sep 17 00:00:00 2001 +From: Josef Bacik +Date: Thu, 21 Jun 2012 16:02:10 -0400 +Subject: [PATCH 1/3] Btrfs-progs: add btrfs device ready command + +This command will be used by things like dracut that wish to know very +simply if all of the devices have been added to the kernel cache yet for the +device to be fully mounted. This keeps initrd's from constantly having to +try to mount the file system until it succeeds every time a device is added +to the system. Thanks, + +Signed-off-by: Josef Bacik +--- + cmds-device.c | 35 +++++++++++++++++++++++++++++++++++ + ioctl.h | 2 ++ + 2 files changed, 37 insertions(+), 0 deletions(-) + +diff --git a/cmds-device.c b/cmds-device.c +index 771856b..b24e2a3 100644 +--- a/cmds-device.c ++++ b/cmds-device.c +@@ -249,11 +249,46 @@ static int cmd_scan_dev(int argc, char **argv) + return 0; + } + ++static const char * const cmd_ready_dev_usage[] = { ++ "btrfs device ready ", ++ "Check device to see if it has all of it's devices in cache for mounting", ++ NULL ++}; ++ ++static int cmd_ready_dev(int argc, char **argv) ++{ ++ struct btrfs_ioctl_vol_args args; ++ int fd; ++ int ret; ++ ++ if (check_argc_min(argc, 2)) ++ usage(cmd_ready_dev_usage); ++ ++ fd = open("/dev/btrfs-control", O_RDWR); ++ if (fd < 0) { ++ perror("failed to open /dev/btrfs-control"); ++ return 10; ++ } ++ ++ strncpy(args.name, argv[argc - 1], BTRFS_PATH_NAME_MAX); ++ ret = ioctl(fd, BTRFS_IOC_DEVICES_READY, &args); ++ if (ret < 0) { ++ fprintf(stderr, "ERROR: unable to determine if the device '%s'" ++ " is ready for mounting - %s\n", argv[argc - 1], ++ strerror(errno)); ++ ret = 1; ++ } ++ ++ close(fd); ++ return ret; ++} ++ + const struct cmd_group device_cmd_group = { + device_cmd_group_usage, NULL, { + { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 }, + { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 }, + { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 }, ++ { "ready", cmd_ready_dev, cmd_ready_dev_usage, NULL, 0 }, + { 0, 0, 0, 0, 0 } + } + }; +diff --git a/ioctl.h b/ioctl.h +index d6f3d07..30220ad 100644 +--- a/ioctl.h ++++ b/ioctl.h +@@ -365,6 +365,8 @@ struct btrfs_ioctl_clone_range_args { + struct btrfs_ioctl_ino_path_args) + #define BTRFS_IOC_LOGICAL_INO _IOWR(BTRFS_IOCTL_MAGIC, 36, \ + struct btrfs_ioctl_ino_path_args) ++#define BTRFS_IOC_DEVICES_READY _IOR(BTRFS_IOCTL_MAGIC, 39, \ ++ struct btrfs_ioctl_vol_args) + + #define BTRFS_IOC_SET_RECEIVED_SUBVOL _IOWR(BTRFS_IOCTL_MAGIC, 37, \ + struct btrfs_ioctl_received_subvol_args) +-- +1.7.7.6 + diff --git a/Btrfs-progs-detect-if-the-disk-we-are-formatting-is-.patch b/Btrfs-progs-detect-if-the-disk-we-are-formatting-is-.patch new file mode 100644 index 0000000..ef8528d --- /dev/null +++ b/Btrfs-progs-detect-if-the-disk-we-are-formatting-is-.patch @@ -0,0 +1,163 @@ +From f507119002c6bb972df09a47edfa8998c0fc8172 Mon Sep 17 00:00:00 2001 +From: Josef Bacik +Date: Fri, 20 Jul 2012 11:09:43 -0400 +Subject: [PATCH 2/3] Btrfs-progs: detect if the disk we are formatting is a + ssd + +SSD's do not gain anything by having metadata DUP turned on. The underlying +file system that is a part of all SSD's could easily map duplicate metadat +blocks into the same erase block which effectively eliminates the benefit of +duplicating the metadata on disk. So detect if we are formatting a single +SSD drive and if we are do not use DUP. Thanks, + +Signed-off-by: Josef Bacik +--- + Makefile | 2 +- + man/mkfs.btrfs.8.in | 5 +++- + mkfs.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-- + 3 files changed, 60 insertions(+), 5 deletions(-) + +diff --git a/Makefile b/Makefile +index c0aaa3d..1afef49 100644 +--- a/Makefile ++++ b/Makefile +@@ -66,7 +66,7 @@ btrfsck: $(objects) btrfsck.o + $(CC) $(CFLAGS) -o btrfsck btrfsck.o $(objects) $(LDFLAGS) $(LIBS) + + mkfs.btrfs: $(objects) mkfs.o +- $(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS) ++ $(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS) -lblkid + + btrfs-debug-tree: $(objects) debug-tree.o + $(CC) $(CFLAGS) -o btrfs-debug-tree $(objects) debug-tree.o $(LDFLAGS) $(LIBS) +diff --git a/man/mkfs.btrfs.8.in b/man/mkfs.btrfs.8.in +index fc2e1d2..dfa7996 100644 +--- a/man/mkfs.btrfs.8.in ++++ b/man/mkfs.btrfs.8.in +@@ -46,7 +46,10 @@ Specify a label for the filesystem. + .TP + \fB\-m\fR, \fB\-\-metadata \fIprofile\fR + Specify how metadata must be spanned across the devices specified. Valid +-values are raid0, raid1, raid10 or single. ++values are raid0, raid1, raid10, single or dup. Single device will have dup ++set by default except in the case of SSDs which will default to single. This is ++because SSDs can remap blocks internally so duplicate blocks could end up in the ++same erase block which negates the benefits of doing metadata duplication. + .TP + \fB\-M\fR, \fB\-\-mixed\fR + Mix data and metadata chunks together for more efficient space +diff --git a/mkfs.c b/mkfs.c +index dff5eb8..8816db8 100644 +--- a/mkfs.c ++++ b/mkfs.c +@@ -37,6 +37,7 @@ + #include + #include + #include ++#include + #include "kerncompat.h" + #include "ctree.h" + #include "disk-io.h" +@@ -234,7 +235,7 @@ static int create_one_raid_group(struct btrfs_trans_handle *trans, + static int create_raid_groups(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 data_profile, + int data_profile_opt, u64 metadata_profile, +- int metadata_profile_opt, int mixed) ++ int metadata_profile_opt, int mixed, int ssd) + { + u64 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy); + u64 allowed; +@@ -245,8 +246,12 @@ static int create_raid_groups(struct btrfs_trans_handle *trans, + * For mixed groups defaults are single/single. + */ + if (!metadata_profile_opt && !mixed) { ++ if (num_devices == 1 && ssd) ++ printf("Detected a SSD, turning off metadata " ++ "duplication. Mkfs with -m dup if you want to " ++ "force metadata duplication.\n"); + metadata_profile = (num_devices > 1) ? +- BTRFS_BLOCK_GROUP_RAID1 : BTRFS_BLOCK_GROUP_DUP; ++ BTRFS_BLOCK_GROUP_RAID1 : (ssd) ? 0: BTRFS_BLOCK_GROUP_DUP; + } + if (!data_profile_opt && !mixed) { + data_profile = (num_devices > 1) ? +@@ -1201,6 +1206,49 @@ static int zero_output_file(int out_fd, u64 size, u32 sectorsize) + return ret; + } + ++static int is_ssd(const char *file) ++{ ++ char *devname; ++ blkid_probe probe; ++ char *dev; ++ char path[PATH_MAX]; ++ dev_t disk; ++ int fd; ++ char rotational; ++ ++ probe = blkid_new_probe_from_filename(file); ++ if (!probe) ++ return 0; ++ ++ /* ++ * We want to use blkid_devno_to_wholedisk() but it's broken for some ++ * reason on F17 at least so we'll do this trickery ++ */ ++ disk = blkid_probe_get_wholedisk_devno(probe); ++ devname = blkid_devno_to_devname(disk); ++ ++ dev = strrchr(devname, '/'); ++ dev++; ++ ++ snprintf(path, PATH_MAX, "/sys/block/%s/queue/rotational", dev); ++ ++ free(devname); ++ blkid_free_probe(probe); ++ ++ fd = open(path, O_RDONLY); ++ if (fd < 0) { ++ return 0; ++ } ++ ++ if (read(fd, &rotational, sizeof(char)) < sizeof(char)) { ++ close(fd); ++ return 0; ++ } ++ close(fd); ++ ++ return !atoi((const char *)&rotational); ++} ++ + int main(int ac, char **av) + { + char *file; +@@ -1227,6 +1275,7 @@ int main(int ac, char **av) + int data_profile_opt = 0; + int metadata_profile_opt = 0; + int nodiscard = 0; ++ int ssd = 0; + + char *source_dir = NULL; + int source_dir_set = 0; +@@ -1352,6 +1401,9 @@ int main(int ac, char **av) + exit(1); + } + } ++ ++ ssd = is_ssd(file); ++ + if (mixed) { + if (metadata_profile != data_profile) { + fprintf(stderr, "With mixed block groups data and metadata " +@@ -1438,7 +1490,7 @@ raid_groups: + if (!source_dir_set) { + ret = create_raid_groups(trans, root, data_profile, + data_profile_opt, metadata_profile, +- metadata_profile_opt, mixed); ++ metadata_profile_opt, mixed, ssd); + BUG_ON(ret); + } + +-- +1.7.7.6 + diff --git a/Btrfs-progs-only-enforce-a-maximum-size-if-we-specif.patch b/Btrfs-progs-only-enforce-a-maximum-size-if-we-specif.patch new file mode 100644 index 0000000..93aa5e0 --- /dev/null +++ b/Btrfs-progs-only-enforce-a-maximum-size-if-we-specif.patch @@ -0,0 +1,143 @@ +From cbe7ca77431c40bab80135c7b8ee6a5dece56e03 Mon Sep 17 00:00:00 2001 +From: Josef Bacik +Date: Fri, 27 Jul 2012 08:24:37 -0400 +Subject: [PATCH 3/3] Btrfs-progs: only enforce a maximum size if we specify + one + +My patch + +04609add88ef8428d725de6ef60f46a3ff0dbc8e + +introduced a regression where if you mkfs'ed a group of disks with different +sizes it limited the disks to the size of the first one that is specified. +This was not the intent of my patch, I only want it to limit the size based +on the -b option, so I've reworked the code to pass in a max block count and +that fixes the issue. Thanks, + +Signed-off-by: Josef Bacik +--- + btrfs-vol.c | 3 ++- + cmds-device.c | 3 ++- + mkfs.c | 15 ++++++--------- + utils.c | 12 +++--------- + utils.h | 6 ++---- + 5 files changed, 15 insertions(+), 24 deletions(-) + +diff --git a/btrfs-vol.c b/btrfs-vol.c +index 0efdbc1..ad824bd 100644 +--- a/btrfs-vol.c ++++ b/btrfs-vol.c +@@ -150,7 +150,8 @@ int main(int ac, char **av) + if (cmd == BTRFS_IOC_ADD_DEV) { + int mixed = 0; + +- ret = btrfs_prepare_device(devfd, device, 1, &dev_block_count, &mixed); ++ ret = btrfs_prepare_device(devfd, device, 1, &dev_block_count, ++ 0, &mixed, 0); + if (ret) { + fprintf(stderr, "Unable to init %s\n", device); + exit(1); +diff --git a/cmds-device.c b/cmds-device.c +index b24e2a3..75ee293 100644 +--- a/cmds-device.c ++++ b/cmds-device.c +@@ -107,7 +107,8 @@ static int cmd_add_dev(int argc, char **argv) + continue; + } + +- res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count, &mixed); ++ res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count, ++ 0, &mixed, 0); + if (res) { + fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]); + close(devfd); +diff --git a/mkfs.c b/mkfs.c +index 8816db8..93cd83c 100644 +--- a/mkfs.c ++++ b/mkfs.c +@@ -1373,11 +1373,9 @@ int main(int ac, char **av) + exit(1); + } + first_file = file; +- ret = __btrfs_prepare_device(fd, file, zero_end, +- &dev_block_count, &mixed, nodiscard); +- if (block_count == 0) +- block_count = dev_block_count; +- else if (block_count > dev_block_count) { ++ ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, ++ block_count, &mixed, nodiscard); ++ if (block_count && block_count > dev_block_count) { + fprintf(stderr, "%s is smaller than requested size\n", file); + exit(1); + } +@@ -1418,7 +1416,7 @@ int main(int ac, char **av) + leafsize * i; + } + +- ret = make_btrfs(fd, file, label, blocks, block_count, ++ ret = make_btrfs(fd, file, label, blocks, dev_block_count, + nodesize, leafsize, + sectorsize, stripesize); + if (ret) { +@@ -1474,9 +1472,8 @@ int main(int ac, char **av) + close(fd); + continue; + } +- dev_block_count = block_count; +- ret = __btrfs_prepare_device(fd, file, zero_end, +- &dev_block_count, &mixed, nodiscard); ++ ret = btrfs_prepare_device(fd, file, zero_end, &dev_block_count, ++ block_count, &mixed, nodiscard); + mixed = old_mixed; + BUG_ON(ret); + +diff --git a/utils.c b/utils.c +index aade9e2..a5ffb62 100644 +--- a/utils.c ++++ b/utils.c +@@ -537,13 +537,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, + } + + int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, +- int *mixed) +-{ +- /* discard by default when called from 'device add' */ +- return __btrfs_prepare_device(fd, file, zero_end, block_count_ret, mixed, 0); +-} +-int __btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, +- int *mixed, int nodiscard) ++ u64 max_block_count, int *mixed, int nodiscard) + { + u64 block_count; + u64 bytenr; +@@ -561,8 +555,8 @@ int __btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_re + fprintf(stderr, "unable to find %s size\n", file); + exit(1); + } +- if (*block_count_ret) +- block_count = min(block_count, *block_count_ret); ++ if (max_block_count) ++ block_count = min(block_count, max_block_count); + zero_end = 1; + + if (block_count < 1024 * 1024 * 1024 && !(*mixed)) { +diff --git a/utils.h b/utils.h +index c147c12..3a0368b 100644 +--- a/utils.h ++++ b/utils.h +@@ -26,10 +26,8 @@ int make_btrfs(int fd, const char *device, const char *label, + u32 leafsize, u32 sectorsize, u32 stripesize); + int btrfs_make_root_dir(struct btrfs_trans_handle *trans, + struct btrfs_root *root, u64 objectid); +-int btrfs_prepare_device(int fd, char *file, int zero_end, +- u64 *block_count_ret, int *mixed); +-int __btrfs_prepare_device(int fd, char *file, int zero_end, +- u64 *block_count_ret, int *mixed, int nodiscard); ++int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret, ++ u64 max_block_count, int *mixed, int nodiscard); + int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, + struct btrfs_root *root, int fd, char *path, + u64 block_count, u32 io_width, u32 io_align, +-- +1.7.7.6 + diff --git a/btrfs-init-dev-list.patch b/btrfs-init-dev-list.patch new file mode 100644 index 0000000..80c0959 --- /dev/null +++ b/btrfs-init-dev-list.patch @@ -0,0 +1,21 @@ +diff --git a/utils.c b/utils.c +index a5ffb62..f6686c6 100644 +--- a/utils.c ++++ b/utils.c +@@ -476,7 +476,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, + u64 num_devs; + int ret; + +- device = kmalloc(sizeof(*device), GFP_NOFS); ++ device = kzalloc(sizeof(*device), GFP_NOFS); + if (!device) + return -ENOMEM; + buf = kmalloc(sectorsize, GFP_NOFS); +@@ -502,6 +502,7 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans, + device->bytes_used = 0; + device->total_ios = 0; + device->dev_root = root->fs_info->dev_root; ++ INIT_LIST_HEAD(&device->dev_list); + + ret = btrfs_add_device(trans, root, device); + BUG_ON(ret); diff --git a/btrfs-progs-upstream.patch b/btrfs-progs-upstream.patch deleted file mode 100644 index 83e06a0..0000000 --- a/btrfs-progs-upstream.patch +++ /dev/null @@ -1,16713 +0,0 @@ -diff --git a/.gitignore b/.gitignore -new file mode 100644 -index 0000000..0e560d5 ---- /dev/null -+++ b/.gitignore -@@ -0,0 +1,15 @@ -+*.o -+.*.o.d -+version.h -+man/*.gz -+btrfs -+btrfs-debug-tree -+btrfs-map-logical -+btrfs-show -+btrfs-vol -+btrfsck -+btrfsctl -+find-root -+mkfs.btrfs -+repair -+restore -diff --git a/INSTALL b/INSTALL -index 16b45a5..6afbd90 100644 ---- a/INSTALL -+++ b/INSTALL -@@ -22,27 +22,38 @@ in the e2fsprogs sources, and is usually available as libuuid or - e2fsprogs-devel from various distros. - - Building the utilities is just make ; make install. The programs go --into /usr/local/bin. The commands available are: -+into /usr/local/bin. The mains commands available are: - - mkfs.btrfs: create a filesystem - --btrfsctl: control program to create snapshots and subvolumes: -- -+btrfs: control program to create snapshots and subvolumes: -+ # mount a btrfs filesystem - mount /dev/sda2 /mnt -- btrfsctl -s new_subvol_name /mnt -- btrfsctl -s snapshot_of_default /mnt/default -- btrfsctl -s snapshot_of_new_subvol /mnt/new_subvol_name -- btrfsctl -s snapshot_of_a_snapshot /mnt/snapshot_of_new_subvol -+ -+ # create a subvolume -+ btrfs subvolume create /mnt/new_subvol_name -+ -+ # snapshot of a subvolume -+ btrfs subvolume snapshot /mnt/default /mnt/snapshot_of_default -+ btrfs subvolume snapshot /mnt/snapshot_of_default \ -+ /mnt/snapshot_of_a_snapshot -+ -+ # list of the subvolumes - ls /mnt - default snapshot_of_a_snapshot snapshot_of_new_subvol - new_subvol_name snapshot_of_default - -- Snapshots and subvolumes cannot be deleted right now, but you can -- rm -rf all the files and directories inside them. -+ # removal of a subvolume or a snapshot -+ btrfs subvolume delete /mn/snapshot_of_a_snapshot -+ -+ # look a the btrfs man page for further information -+ man btrfs - - btrfsck: do a limited check of the FS extent trees. - --debug-tree: print all of the FS metadata in text form. Example: -+btrfs-debug-tree: print all of the FS metadata in text form. Example: -+ -+ btrfs-debug-tree /dev/sda2 >& big_output_file -+ - -- debug-tree /dev/sda2 >& big_output_file - -diff --git a/Makefile b/Makefile -index 8097b5a..79818e6 100644 ---- a/Makefile -+++ b/Makefile -@@ -1,28 +1,32 @@ --CC=gcc -+CC = gcc - AM_CFLAGS = -Wall -D_FILE_OFFSET_BITS=64 -D_FORTIFY_SOURCE=2 --CFLAGS = -g -Werror -Os -+CFLAGS = -g -O0 - objects = ctree.o disk-io.o radix-tree.o extent-tree.o print-tree.o \ - root-tree.o dir-item.o file-item.o inode-item.o \ - inode-map.o crc32c.o rbtree.o extent-cache.o extent_io.o \ -- volumes.o utils.o -+ volumes.o utils.o btrfs-list.o btrfslabel.o repair.o -+cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \ -+ cmds-inspect.o cmds-balance.o - --# --CHECKFLAGS=-D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \ -- -Wuninitialized -Wshadow -Wundef -+CHECKFLAGS= -D__linux__ -Dlinux -D__STDC__ -Dunix -D__unix__ -Wbitwise \ -+ -Wuninitialized -Wshadow -Wundef - DEPFLAGS = -Wp,-MMD,$(@D)/.$(@F).d,-MT,$@ - --INSTALL= install -+INSTALL = install - prefix ?= /usr/local - bindir = $(prefix)/bin - LIBS=-luuid -+RESTORE_LIBS=-lz - --progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck -+progs = btrfsctl mkfs.btrfs btrfs-debug-tree btrfs-show btrfs-vol btrfsck \ -+ btrfs btrfs-map-logical btrfs-image btrfs-zero-log btrfs-convert \ -+ btrfs-find-root btrfs-restore btrfstune - - # make C=1 to enable sparse - ifdef C -- check=sparse $(CHECKFLAGS) -+ check = sparse $(CHECKFLAGS) - else -- check=ls -+ check = ls - endif - - .c.o: -@@ -35,38 +39,66 @@ all: version $(progs) manpages - version: - bash version.sh - -+btrfs: $(objects) btrfs.o help.o common.o $(cmds_objects) -+ $(CC) $(CFLAGS) -o btrfs btrfs.o help.o common.o $(cmds_objects) \ -+ $(objects) $(LDFLAGS) $(LIBS) -lpthread -+ -+calc-size: $(objects) calc-size.o -+ gcc $(CFLAGS) -o calc-size calc-size.o $(objects) $(LDFLAGS) $(LIBS) -+ -+btrfs-find-root: $(objects) find-root.o -+ gcc $(CFLAGS) -o btrfs-find-root find-root.o $(objects) $(LDFLAGS) $(LIBS) -+ -+btrfs-restore: $(objects) restore.o -+ gcc $(CFLAGS) -o btrfs-restore restore.o $(objects) $(LDFLAGS) $(LIBS) $(RESTORE_LIBS) -+ - btrfsctl: $(objects) btrfsctl.o -- gcc $(CFLAGS) -o btrfsctl btrfsctl.o $(objects) $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o btrfsctl btrfsctl.o $(objects) $(LDFLAGS) $(LIBS) - - btrfs-vol: $(objects) btrfs-vol.o -- gcc $(CFLAGS) -o btrfs-vol btrfs-vol.o $(objects) $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o btrfs-vol btrfs-vol.o $(objects) $(LDFLAGS) $(LIBS) - - btrfs-show: $(objects) btrfs-show.o -- gcc $(CFLAGS) -o btrfs-show btrfs-show.o $(objects) $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o btrfs-show btrfs-show.o $(objects) $(LDFLAGS) $(LIBS) - - btrfsck: $(objects) btrfsck.o -- gcc $(CFLAGS) -o btrfsck btrfsck.o $(objects) $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o btrfsck btrfsck.o $(objects) $(LDFLAGS) $(LIBS) - - mkfs.btrfs: $(objects) mkfs.o -- gcc $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o mkfs.btrfs $(objects) mkfs.o $(LDFLAGS) $(LIBS) - - btrfs-debug-tree: $(objects) debug-tree.o -- gcc $(CFLAGS) -o btrfs-debug-tree $(objects) debug-tree.o $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o btrfs-debug-tree $(objects) debug-tree.o $(LDFLAGS) $(LIBS) -+ -+btrfs-zero-log: $(objects) btrfs-zero-log.o -+ $(CC) $(CFLAGS) -o btrfs-zero-log $(objects) btrfs-zero-log.o $(LDFLAGS) $(LIBS) -+ -+btrfs-select-super: $(objects) btrfs-select-super.o -+ $(CC) $(CFLAGS) -o btrfs-select-super $(objects) btrfs-select-super.o $(LDFLAGS) $(LIBS) - - btrfstune: $(objects) btrfstune.o -- gcc $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o btrfstune $(objects) btrfstune.o $(LDFLAGS) $(LIBS) -+ -+btrfs-map-logical: $(objects) btrfs-map-logical.o -+ $(CC) $(CFLAGS) -o btrfs-map-logical $(objects) btrfs-map-logical.o $(LDFLAGS) $(LIBS) -+ -+btrfs-corrupt-block: $(objects) btrfs-corrupt-block.o -+ $(CC) $(CFLAGS) -o btrfs-corrupt-block $(objects) btrfs-corrupt-block.o $(LDFLAGS) $(LIBS) - - btrfs-image: $(objects) btrfs-image.o -- gcc $(CFLAGS) -o btrfs-image $(objects) btrfs-image.o -lpthread -lz $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o btrfs-image $(objects) btrfs-image.o -lpthread -lz $(LDFLAGS) $(LIBS) - - dir-test: $(objects) dir-test.o -- gcc $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o dir-test $(objects) dir-test.o $(LDFLAGS) $(LIBS) - - quick-test: $(objects) quick-test.o -- gcc $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS) -+ $(CC) $(CFLAGS) -o quick-test $(objects) quick-test.o $(LDFLAGS) $(LIBS) -+ -+btrfs-convert: $(objects) convert.o -+ $(CC) $(CFLAGS) -o btrfs-convert $(objects) convert.o -lext2fs -lcom_err $(LDFLAGS) $(LIBS) - --convert: $(objects) convert.o -- gcc $(CFLAGS) -o btrfs-convert $(objects) convert.o -lext2fs $(LDFLAGS) $(LIBS) -+ioctl-test: $(objects) ioctl-test.o -+ $(CC) $(CFLAGS) -o ioctl-test $(objects) ioctl-test.o $(LDFLAGS) $(LIBS) - - manpages: - cd man; make -@@ -75,12 +107,12 @@ install-man: - cd man; make install - - clean : -- rm -f $(progs) cscope.out *.o .*.d btrfs-convert -+ rm -f $(progs) cscope.out *.o .*.d btrfs-convert btrfs-image btrfs-select-super \ -+ btrfs-zero-log btrfstune dir-test ioctl-test quick-test version.h - cd man; make clean - - install: $(progs) install-man - $(INSTALL) -m755 -d $(DESTDIR)$(bindir) - $(INSTALL) $(progs) $(DESTDIR)$(bindir) -- if [ -e btrfs-convert ]; then $(INSTALL) btrfs-convert $(DESTDIR)$(bindir); fi - - -include .*.d -diff --git a/bcp b/bcp -index 5729e91..e7ca641 100755 ---- a/bcp -+++ b/bcp -@@ -136,8 +136,7 @@ for srci in xrange(0, src_args): - srcname = os.path.join(dirpath, x) - statinfo = os.lstat(srcname) - -- if srcname.startswith(src): -- part = srcname[len(src) + 1:] -+ part = os.path.relpath(srcname, src) - - if stat.S_ISLNK(statinfo.st_mode): - copylink(srcname, dst, part, statinfo, None) -@@ -152,8 +151,7 @@ for srci in xrange(0, src_args): - - for f in filenames: - srcname = os.path.join(dirpath, f) -- if srcname.startswith(src): -- part = srcname[len(src) + 1:] -+ part = os.path.relpath(srcname, src) - - statinfo = os.lstat(srcname) - copyfile(srcname, dst, part, statinfo, None) -diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c -new file mode 100644 -index 0000000..7051e99 ---- /dev/null -+++ b/btrfs-corrupt-block.c -@@ -0,0 +1,396 @@ -+/* -+ * Copyright (C) 2009 Oracle. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _XOPEN_SOURCE 500 -+#define _GNU_SOURCE 1 -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "volumes.h" -+#include "disk-io.h" -+#include "print-tree.h" -+#include "transaction.h" -+#include "list.h" -+#include "version.h" -+ -+struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr, -+ u32 blocksize, int copy) -+{ -+ int ret; -+ struct extent_buffer *eb; -+ u64 length; -+ struct btrfs_multi_bio *multi = NULL; -+ struct btrfs_device *device; -+ int num_copies; -+ int mirror_num = 1; -+ -+ eb = btrfs_find_create_tree_block(root, bytenr, blocksize); -+ if (!eb) -+ return NULL; -+ -+ length = blocksize; -+ while (1) { -+ ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, -+ eb->start, &length, &multi, mirror_num); -+ BUG_ON(ret); -+ device = multi->stripes[0].dev; -+ eb->fd = device->fd; -+ device->total_ios++; -+ eb->dev_bytenr = multi->stripes[0].physical; -+ -+ fprintf(stdout, "mirror %d logical %Lu physical %Lu " -+ "device %s\n", mirror_num, (unsigned long long)bytenr, -+ (unsigned long long)eb->dev_bytenr, device->name); -+ kfree(multi); -+ -+ if (!copy || mirror_num == copy) { -+ ret = read_extent_from_disk(eb); -+ printf("corrupting %llu copy %d\n", eb->start, -+ mirror_num); -+ memset(eb->data, 0, eb->len); -+ write_extent_to_disk(eb); -+ fsync(eb->fd); -+ } -+ -+ num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, -+ eb->start, eb->len); -+ if (num_copies == 1) -+ break; -+ -+ mirror_num++; -+ if (mirror_num > num_copies) -+ break; -+ } -+ return eb; -+} -+ -+static void print_usage(void) -+{ -+ fprintf(stderr, "usage: btrfs-map-logical [options] mount_point\n"); -+ fprintf(stderr, "\t-l Logical extent to map\n"); -+ fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n"); -+ fprintf(stderr, "\t-o Output file to hold the extent\n"); -+ fprintf(stderr, "\t-b Number of bytes to read\n"); -+ exit(1); -+} -+ -+static void corrupt_keys(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, -+ struct extent_buffer *eb) -+{ -+ int slot; -+ int bad_slot; -+ int nr; -+ struct btrfs_disk_key bad_key;; -+ -+ nr = btrfs_header_nritems(eb); -+ if (nr == 0) -+ return; -+ -+ slot = rand() % nr; -+ bad_slot = rand() % nr; -+ -+ if (bad_slot == slot) -+ return; -+ -+ fprintf(stderr, "corrupting keys in block %llu slot %d swapping with %d\n", -+ (unsigned long long)eb->start, slot, bad_slot); -+ -+ if (btrfs_header_level(eb) == 0) { -+ btrfs_item_key(eb, &bad_key, bad_slot); -+ btrfs_set_item_key(eb, &bad_key, slot); -+ } else { -+ btrfs_node_key(eb, &bad_key, bad_slot); -+ btrfs_set_node_key(eb, &bad_key, slot); -+ } -+ btrfs_mark_buffer_dirty(eb); -+ if (!trans) { -+ csum_tree_block(root, eb, 0); -+ write_extent_to_disk(eb); -+ } -+} -+ -+ -+static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr) -+{ -+ struct extent_buffer *eb; -+ -+ eb = read_tree_block(root, bytenr, root->leafsize, 0); -+ if (!eb) -+ return -EIO;; -+ -+ corrupt_keys(NULL, root, eb); -+ free_extent_buffer(eb); -+ return 0; -+} -+ -+static int corrupt_extent(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, u64 bytenr, int copy) -+{ -+ struct btrfs_key key; -+ struct extent_buffer *leaf; -+ u32 item_size; -+ unsigned long ptr; -+ struct btrfs_path *path; -+ int ret; -+ int slot; -+ int should_del = rand() % 3; -+ -+ path = btrfs_alloc_path(); -+ -+ key.objectid = bytenr; -+ key.type = (u8)-1; -+ key.offset = (u64)-1; -+ -+ while(1) { -+ ret = btrfs_search_slot(trans, root->fs_info->extent_root, -+ &key, path, -1, 1); -+ if (ret < 0) -+ break; -+ -+ if (ret > 0) { -+ if (path->slots[0] == 0) -+ break; -+ path->slots[0]--; -+ ret = 0; -+ } -+ leaf = path->nodes[0]; -+ slot = path->slots[0]; -+ btrfs_item_key_to_cpu(leaf, &key, slot); -+ if (key.objectid != bytenr) -+ break; -+ -+ if (key.type != BTRFS_EXTENT_ITEM_KEY && -+ key.type != BTRFS_TREE_BLOCK_REF_KEY && -+ key.type != BTRFS_EXTENT_DATA_REF_KEY && -+ key.type != BTRFS_EXTENT_REF_V0_KEY && -+ key.type != BTRFS_SHARED_BLOCK_REF_KEY && -+ key.type != BTRFS_SHARED_DATA_REF_KEY) -+ goto next; -+ -+ if (should_del) { -+ fprintf(stderr, "deleting extent record: key %Lu %u %Lu\n", -+ key.objectid, key.type, key.offset); -+ -+ if (key.type == BTRFS_EXTENT_ITEM_KEY) { -+ /* make sure this extent doesn't get -+ * reused for other purposes */ -+ btrfs_pin_extent(root->fs_info, -+ key.objectid, key.offset); -+ } -+ -+ btrfs_del_item(trans, root, path); -+ } else { -+ fprintf(stderr, "corrupting extent record: key %Lu %u %Lu\n", -+ key.objectid, key.type, key.offset); -+ ptr = btrfs_item_ptr_offset(leaf, slot); -+ item_size = btrfs_item_size_nr(leaf, slot); -+ memset_extent_buffer(leaf, 0, ptr, item_size); -+ btrfs_mark_buffer_dirty(leaf); -+ } -+next: -+ btrfs_release_path(NULL, path); -+ -+ if (key.offset > 0) -+ key.offset--; -+ if (key.offset == 0) -+ break; -+ } -+ -+ btrfs_free_path(path); -+ return 0; -+} -+ -+static void btrfs_corrupt_extent_leaf(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, struct extent_buffer *eb) -+{ -+ u32 nr = btrfs_header_nritems(eb); -+ u32 victim = rand() % nr; -+ u64 objectid; -+ struct btrfs_key key; -+ -+ btrfs_item_key_to_cpu(eb, &key, victim); -+ objectid = key.objectid; -+ corrupt_extent(trans, root, objectid, 1); -+} -+ -+static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, struct extent_buffer *eb) -+{ -+ int i; -+ u32 nr; -+ -+ if (!eb) -+ return; -+ -+ nr = btrfs_header_nritems(eb); -+ if (btrfs_is_leaf(eb)) { -+ btrfs_corrupt_extent_leaf(trans, root, eb); -+ return; -+ } -+ -+ if (btrfs_header_level(eb) == 1 && eb != root->node) { -+ if (rand() % 5) -+ return; -+ } -+ -+ for (i = 0; i < nr; i++) { -+ struct extent_buffer *next; -+ -+ next = read_tree_block(root, btrfs_node_blockptr(eb, i), -+ root->leafsize, btrfs_node_ptr_generation(eb, i)); -+ if (!next) -+ continue; -+ btrfs_corrupt_extent_tree(trans, root, next); -+ free_extent_buffer(next); -+ } -+} -+ -+static struct option long_options[] = { -+ /* { "byte-count", 1, NULL, 'b' }, */ -+ { "logical", 1, NULL, 'l' }, -+ { "copy", 1, NULL, 'c' }, -+ { "bytes", 1, NULL, 'b' }, -+ { "extent-record", 0, NULL, 'e' }, -+ { "extent-tree", 0, NULL, 'E' }, -+ { "keys", 0, NULL, 'k' }, -+ { 0, 0, 0, 0} -+}; -+ -+ -+int main(int ac, char **av) -+{ -+ struct cache_tree root_cache; -+ struct btrfs_root *root; -+ struct extent_buffer *eb; -+ char *dev; -+ u64 logical = 0; -+ int ret = 0; -+ int option_index = 0; -+ int copy = 0; -+ u64 bytes = 4096; -+ int extent_rec = 0; -+ int extent_tree = 0; -+ int corrupt_block_keys = 0; -+ -+ srand(128); -+ -+ while(1) { -+ int c; -+ c = getopt_long(ac, av, "l:c:eEk", long_options, -+ &option_index); -+ if (c < 0) -+ break; -+ switch(c) { -+ case 'l': -+ logical = atoll(optarg); -+ if (logical == 0) { -+ fprintf(stderr, -+ "invalid extent number\n"); -+ print_usage(); -+ } -+ break; -+ case 'c': -+ copy = atoi(optarg); -+ if (copy == 0) { -+ fprintf(stderr, -+ "invalid copy number\n"); -+ print_usage(); -+ } -+ break; -+ case 'b': -+ bytes = atoll(optarg); -+ if (bytes == 0) { -+ fprintf(stderr, -+ "invalid byte count\n"); -+ print_usage(); -+ } -+ break; -+ case 'e': -+ extent_rec = 1; -+ break; -+ case 'E': -+ extent_tree = 1; -+ break; -+ case 'k': -+ corrupt_block_keys = 1; -+ break; -+ default: -+ print_usage(); -+ } -+ } -+ ac = ac - optind; -+ if (ac == 0) -+ print_usage(); -+ if (logical == 0 && !extent_tree) -+ print_usage(); -+ if (copy < 0) -+ print_usage(); -+ -+ dev = av[optind]; -+ -+ radix_tree_init(); -+ cache_tree_init(&root_cache); -+ -+ root = open_ctree(dev, 0, 1); -+ if (!root) { -+ fprintf(stderr, "Open ctree failed\n"); -+ exit(1); -+ } -+ if (extent_rec) { -+ struct btrfs_trans_handle *trans; -+ trans = btrfs_start_transaction(root, 1); -+ ret = corrupt_extent (trans, root, logical, 0); -+ btrfs_commit_transaction(trans, root); -+ goto out_close; -+ } -+ if (extent_tree) { -+ struct btrfs_trans_handle *trans; -+ trans = btrfs_start_transaction(root, 1); -+ btrfs_corrupt_extent_tree(trans, root->fs_info->extent_root, -+ root->fs_info->extent_root->node); -+ btrfs_commit_transaction(trans, root); -+ goto out_close; -+ } -+ -+ if (bytes == 0) -+ bytes = root->sectorsize; -+ -+ bytes = (bytes + root->sectorsize - 1) / root->sectorsize; -+ bytes *= root->sectorsize; -+ -+ while (bytes > 0) { -+ if (corrupt_block_keys) { -+ corrupt_keys_in_block(root, logical); -+ } else { -+ eb = debug_corrupt_block(root, logical, -+ root->sectorsize, copy); -+ free_extent_buffer(eb); -+ } -+ logical += root->sectorsize; -+ bytes -= root->sectorsize; -+ } -+ return ret; -+out_close: -+ close_ctree(root); -+ return ret; -+} -diff --git a/btrfs-defrag.c b/btrfs-defrag.c -new file mode 100644 -index 0000000..8f1525a ---- /dev/null -+++ b/btrfs-defrag.c -@@ -0,0 +1,39 @@ -+/* -+ * Copyright (C) 2010 Oracle. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#ifndef __CHECKER__ -+#include -+#include -+#include "ioctl.h" -+#endif -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "transaction.h" -+#include "utils.h" -+#include "version.h" -+ -diff --git a/btrfs-list.c b/btrfs-list.c -new file mode 100644 -index 0000000..5f4a9be ---- /dev/null -+++ b/btrfs-list.c -@@ -0,0 +1,936 @@ -+/* -+ * Copyright (C) 2010 Oracle. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _GNU_SOURCE -+#ifndef __CHECKER__ -+#include -+#include -+#include "ioctl.h" -+#endif -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "transaction.h" -+#include "utils.h" -+ -+/* we store all the roots we find in an rbtree so that we can -+ * search for them later. -+ */ -+struct root_lookup { -+ struct rb_root root; -+}; -+ -+/* -+ * one of these for each root we find. -+ */ -+struct root_info { -+ struct rb_node rb_node; -+ -+ /* this root's id */ -+ u64 root_id; -+ -+ /* the id of the root that references this one */ -+ u64 ref_tree; -+ -+ /* the dir id we're in from ref_tree */ -+ u64 dir_id; -+ -+ /* path from the subvol we live in to this root, including the -+ * root's name. This is null until we do the extra lookup ioctl. -+ */ -+ char *path; -+ -+ /* the name of this root in the directory it lives in */ -+ char name[]; -+}; -+ -+static void root_lookup_init(struct root_lookup *tree) -+{ -+ tree->root.rb_node = NULL; -+} -+ -+static int comp_entry(struct root_info *entry, u64 root_id, u64 ref_tree) -+{ -+ if (entry->root_id > root_id) -+ return 1; -+ if (entry->root_id < root_id) -+ return -1; -+ if (entry->ref_tree > ref_tree) -+ return 1; -+ if (entry->ref_tree < ref_tree) -+ return -1; -+ return 0; -+} -+ -+/* -+ * insert a new root into the tree. returns the existing root entry -+ * if one is already there. Both root_id and ref_tree are used -+ * as the key -+ */ -+static struct rb_node *tree_insert(struct rb_root *root, u64 root_id, -+ u64 ref_tree, struct rb_node *node) -+{ -+ struct rb_node ** p = &root->rb_node; -+ struct rb_node * parent = NULL; -+ struct root_info *entry; -+ int comp; -+ -+ while(*p) { -+ parent = *p; -+ entry = rb_entry(parent, struct root_info, rb_node); -+ -+ comp = comp_entry(entry, root_id, ref_tree); -+ -+ if (comp < 0) -+ p = &(*p)->rb_left; -+ else if (comp > 0) -+ p = &(*p)->rb_right; -+ else -+ return parent; -+ } -+ -+ entry = rb_entry(parent, struct root_info, rb_node); -+ rb_link_node(node, parent, p); -+ rb_insert_color(node, root); -+ return NULL; -+} -+ -+/* -+ * find a given root id in the tree. We return the smallest one, -+ * rb_next can be used to move forward looking for more if required -+ */ -+static struct root_info *tree_search(struct rb_root *root, u64 root_id) -+{ -+ struct rb_node * n = root->rb_node; -+ struct root_info *entry; -+ -+ while(n) { -+ entry = rb_entry(n, struct root_info, rb_node); -+ -+ if (entry->root_id < root_id) -+ n = n->rb_left; -+ else if (entry->root_id > root_id) -+ n = n->rb_right; -+ else { -+ struct root_info *prev; -+ struct rb_node *prev_n; -+ while (1) { -+ prev_n = rb_prev(n); -+ if (!prev_n) -+ break; -+ prev = rb_entry(prev_n, struct root_info, -+ rb_node); -+ if (prev->root_id != root_id) -+ break; -+ entry = prev; -+ n = prev_n; -+ } -+ return entry; -+ } -+ } -+ return NULL; -+} -+ -+/* -+ * this allocates a new root in the lookup tree. -+ * -+ * root_id should be the object id of the root -+ * -+ * ref_tree is the objectid of the referring root. -+ * -+ * dir_id is the directory in ref_tree where this root_id can be found. -+ * -+ * name is the name of root_id in that directory -+ * -+ * name_len is the length of name -+ */ -+static int add_root(struct root_lookup *root_lookup, -+ u64 root_id, u64 ref_tree, u64 dir_id, char *name, -+ int name_len) -+{ -+ struct root_info *ri; -+ struct rb_node *ret; -+ ri = malloc(sizeof(*ri) + name_len + 1); -+ if (!ri) { -+ printf("memory allocation failed\n"); -+ exit(1); -+ } -+ memset(ri, 0, sizeof(*ri) + name_len + 1); -+ ri->path = NULL; -+ ri->dir_id = dir_id; -+ ri->root_id = root_id; -+ ri->ref_tree = ref_tree; -+ strncpy(ri->name, name, name_len); -+ -+ ret = tree_insert(&root_lookup->root, root_id, ref_tree, &ri->rb_node); -+ if (ret) { -+ printf("failed to insert tree %llu\n", (unsigned long long)root_id); -+ exit(1); -+ } -+ return 0; -+} -+ -+/* -+ * for a given root_info, search through the root_lookup tree to construct -+ * the full path name to it. -+ * -+ * This can't be called until all the root_info->path fields are filled -+ * in by lookup_ino_path -+ */ -+static int resolve_root(struct root_lookup *rl, struct root_info *ri, -+ u64 *root_id, u64 *parent_id, u64 *top_id, char **path) -+{ -+ char *full_path = NULL; -+ int len = 0; -+ struct root_info *found; -+ -+ /* -+ * we go backwards from the root_info object and add pathnames -+ * from parent directories as we go. -+ */ -+ *parent_id = 0; -+ found = ri; -+ while (1) { -+ char *tmp; -+ u64 next; -+ int add_len = strlen(found->path); -+ -+ /* room for / and for null */ -+ tmp = malloc(add_len + 2 + len); -+ if (full_path) { -+ memcpy(tmp + add_len + 1, full_path, len); -+ tmp[add_len] = '/'; -+ memcpy(tmp, found->path, add_len); -+ tmp [add_len + len + 1] = '\0'; -+ free(full_path); -+ full_path = tmp; -+ len += add_len + 1; -+ } else { -+ full_path = strdup(found->path); -+ len = add_len; -+ } -+ -+ next = found->ref_tree; -+ /* record the first parent */ -+ if (*parent_id == 0) -+ *parent_id = next; -+ -+ /* if the ref_tree refers to ourselves, we're at the top */ -+ if (next == found->root_id) { -+ *top_id = next; -+ break; -+ } -+ -+ /* -+ * if the ref_tree wasn't in our tree of roots, we're -+ * at the top -+ */ -+ found = tree_search(&rl->root, next); -+ if (!found) { -+ *top_id = next; -+ break; -+ } -+ } -+ -+ *root_id = ri->root_id; -+ *path = full_path; -+ -+ return 0; -+} -+ -+/* -+ * for a single root_info, ask the kernel to give us a path name -+ * inside it's ref_root for the dir_id where it lives. -+ * -+ * This fills in root_info->path with the path to the directory and and -+ * appends this root's name. -+ */ -+static int lookup_ino_path(int fd, struct root_info *ri) -+{ -+ struct btrfs_ioctl_ino_lookup_args args; -+ int ret, e; -+ -+ if (ri->path) -+ return 0; -+ -+ memset(&args, 0, sizeof(args)); -+ args.treeid = ri->ref_tree; -+ args.objectid = ri->dir_id; -+ -+ ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); -+ e = errno; -+ if (ret) { -+ fprintf(stderr, "ERROR: Failed to lookup path for root %llu - %s\n", -+ (unsigned long long)ri->ref_tree, -+ strerror(e)); -+ return ret; -+ } -+ -+ if (args.name[0]) { -+ /* -+ * we're in a subdirectory of ref_tree, the kernel ioctl -+ * puts a / in there for us -+ */ -+ ri->path = malloc(strlen(ri->name) + strlen(args.name) + 1); -+ if (!ri->path) { -+ perror("malloc failed"); -+ exit(1); -+ } -+ strcpy(ri->path, args.name); -+ strcat(ri->path, ri->name); -+ } else { -+ /* we're at the root of ref_tree */ -+ ri->path = strdup(ri->name); -+ if (!ri->path) { -+ perror("strdup failed"); -+ exit(1); -+ } -+ } -+ return 0; -+} -+ -+/* finding the generation for a given path is a two step process. -+ * First we use the inode loookup routine to find out the root id -+ * -+ * Then we use the tree search ioctl to scan all the root items for a -+ * given root id and spit out the latest generation we can find -+ */ -+static u64 find_root_gen(int fd) -+{ -+ struct btrfs_ioctl_ino_lookup_args ino_args; -+ int ret; -+ struct btrfs_ioctl_search_args args; -+ struct btrfs_ioctl_search_key *sk = &args.key; -+ struct btrfs_ioctl_search_header *sh; -+ unsigned long off = 0; -+ u64 max_found = 0; -+ int i; -+ int e; -+ -+ memset(&ino_args, 0, sizeof(ino_args)); -+ ino_args.objectid = BTRFS_FIRST_FREE_OBJECTID; -+ -+ /* this ioctl fills in ino_args->treeid */ -+ ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args); -+ e = errno; -+ if (ret) { -+ fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n", -+ (unsigned long long)BTRFS_FIRST_FREE_OBJECTID, -+ strerror(e)); -+ return 0; -+ } -+ -+ memset(&args, 0, sizeof(args)); -+ -+ sk->tree_id = 1; -+ -+ /* -+ * there may be more than one ROOT_ITEM key if there are -+ * snapshots pending deletion, we have to loop through -+ * them. -+ */ -+ sk->min_objectid = ino_args.treeid; -+ sk->max_objectid = ino_args.treeid; -+ sk->max_type = BTRFS_ROOT_ITEM_KEY; -+ sk->min_type = BTRFS_ROOT_ITEM_KEY; -+ sk->max_offset = (u64)-1; -+ sk->max_transid = (u64)-1; -+ sk->nr_items = 4096; -+ -+ while (1) { -+ ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); -+ e = errno; -+ if (ret < 0) { -+ fprintf(stderr, "ERROR: can't perform the search - %s\n", -+ strerror(e)); -+ return 0; -+ } -+ /* the ioctl returns the number of item it found in nr_items */ -+ if (sk->nr_items == 0) -+ break; -+ -+ off = 0; -+ for (i = 0; i < sk->nr_items; i++) { -+ struct btrfs_root_item *item; -+ sh = (struct btrfs_ioctl_search_header *)(args.buf + -+ off); -+ -+ off += sizeof(*sh); -+ item = (struct btrfs_root_item *)(args.buf + off); -+ off += sh->len; -+ -+ sk->min_objectid = sh->objectid; -+ sk->min_type = sh->type; -+ sk->min_offset = sh->offset; -+ -+ if (sh->objectid > ino_args.treeid) -+ break; -+ -+ if (sh->objectid == ino_args.treeid && -+ sh->type == BTRFS_ROOT_ITEM_KEY) { -+ max_found = max(max_found, -+ btrfs_root_generation(item)); -+ } -+ } -+ if (sk->min_offset < (u64)-1) -+ sk->min_offset++; -+ else -+ break; -+ -+ if (sk->min_type != BTRFS_ROOT_ITEM_KEY) -+ break; -+ if (sk->min_objectid != BTRFS_ROOT_ITEM_KEY) -+ break; -+ } -+ return max_found; -+} -+ -+/* pass in a directory id and this will return -+ * the full path of the parent directory inside its -+ * subvolume root. -+ * -+ * It may return NULL if it is in the root, or an ERR_PTR if things -+ * go badly. -+ */ -+static char *__ino_resolve(int fd, u64 dirid) -+{ -+ struct btrfs_ioctl_ino_lookup_args args; -+ int ret; -+ char *full; -+ int e; -+ -+ memset(&args, 0, sizeof(args)); -+ args.objectid = dirid; -+ -+ ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); -+ e = errno; -+ if (ret) { -+ fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n", -+ (unsigned long long)dirid, strerror(e) ); -+ return ERR_PTR(ret); -+ } -+ -+ if (args.name[0]) { -+ /* -+ * we're in a subdirectory of ref_tree, the kernel ioctl -+ * puts a / in there for us -+ */ -+ full = strdup(args.name); -+ if (!full) { -+ perror("malloc failed"); -+ return ERR_PTR(-ENOMEM); -+ } -+ } else { -+ /* we're at the root of ref_tree */ -+ full = NULL; -+ } -+ return full; -+} -+ -+/* -+ * simple string builder, returning a new string with both -+ * dirid and name -+ */ -+char *build_name(char *dirid, char *name) -+{ -+ char *full; -+ if (!dirid) -+ return strdup(name); -+ -+ full = malloc(strlen(dirid) + strlen(name) + 1); -+ if (!full) -+ return NULL; -+ strcpy(full, dirid); -+ strcat(full, name); -+ return full; -+} -+ -+/* -+ * given an inode number, this returns the full path name inside the subvolume -+ * to that file/directory. cache_dirid and cache_name are used to -+ * cache the results so we can avoid tree searches if a later call goes -+ * to the same directory or file name -+ */ -+static char *ino_resolve(int fd, u64 ino, u64 *cache_dirid, char **cache_name) -+ -+{ -+ u64 dirid; -+ char *dirname; -+ char *name; -+ char *full; -+ int ret; -+ struct btrfs_ioctl_search_args args; -+ struct btrfs_ioctl_search_key *sk = &args.key; -+ struct btrfs_ioctl_search_header *sh; -+ unsigned long off = 0; -+ int namelen; -+ int e; -+ -+ memset(&args, 0, sizeof(args)); -+ -+ sk->tree_id = 0; -+ -+ /* -+ * step one, we search for the inode back ref. We just use the first -+ * one -+ */ -+ sk->min_objectid = ino; -+ sk->max_objectid = ino; -+ sk->max_type = BTRFS_INODE_REF_KEY; -+ sk->max_offset = (u64)-1; -+ sk->min_type = BTRFS_INODE_REF_KEY; -+ sk->max_transid = (u64)-1; -+ sk->nr_items = 1; -+ -+ ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); -+ e = errno; -+ if (ret < 0) { -+ fprintf(stderr, "ERROR: can't perform the search - %s\n", -+ strerror(e)); -+ return NULL; -+ } -+ /* the ioctl returns the number of item it found in nr_items */ -+ if (sk->nr_items == 0) -+ return NULL; -+ -+ off = 0; -+ sh = (struct btrfs_ioctl_search_header *)(args.buf + off); -+ -+ if (sh->type == BTRFS_INODE_REF_KEY) { -+ struct btrfs_inode_ref *ref; -+ dirid = sh->offset; -+ -+ ref = (struct btrfs_inode_ref *)(sh + 1); -+ namelen = btrfs_stack_inode_ref_name_len(ref); -+ -+ name = (char *)(ref + 1); -+ name = strndup(name, namelen); -+ -+ /* use our cached value */ -+ if (dirid == *cache_dirid && *cache_name) { -+ dirname = *cache_name; -+ goto build; -+ } -+ } else { -+ return NULL; -+ } -+ /* -+ * the inode backref gives us the file name and the parent directory id. -+ * From here we use __ino_resolve to get the path to the parent -+ */ -+ dirname = __ino_resolve(fd, dirid); -+build: -+ full = build_name(dirname, name); -+ if (*cache_name && dirname != *cache_name) -+ free(*cache_name); -+ -+ *cache_name = dirname; -+ *cache_dirid = dirid; -+ free(name); -+ -+ return full; -+} -+ -+static int __list_subvol_search(int fd, struct root_lookup *root_lookup) -+{ -+ int ret; -+ struct btrfs_ioctl_search_args args; -+ struct btrfs_ioctl_search_key *sk = &args.key; -+ struct btrfs_ioctl_search_header *sh; -+ struct btrfs_root_ref *ref; -+ unsigned long off = 0; -+ int name_len; -+ char *name; -+ u64 dir_id; -+ int i; -+ -+ root_lookup_init(root_lookup); -+ memset(&args, 0, sizeof(args)); -+ -+ root_lookup_init(root_lookup); -+ -+ memset(&args, 0, sizeof(args)); -+ -+ /* search in the tree of tree roots */ -+ sk->tree_id = 1; -+ -+ /* -+ * set the min and max to backref keys. The search will -+ * only send back this type of key now. -+ */ -+ sk->max_type = BTRFS_ROOT_BACKREF_KEY; -+ sk->min_type = BTRFS_ROOT_BACKREF_KEY; -+ -+ /* -+ * set all the other params to the max, we'll take any objectid -+ * and any trans -+ */ -+ sk->max_objectid = (u64)-1; -+ sk->max_offset = (u64)-1; -+ sk->max_transid = (u64)-1; -+ -+ /* just a big number, doesn't matter much */ -+ sk->nr_items = 4096; -+ -+ while(1) { -+ ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); -+ if (ret < 0) -+ return ret; -+ /* the ioctl returns the number of item it found in nr_items */ -+ if (sk->nr_items == 0) -+ break; -+ -+ off = 0; -+ -+ /* -+ * for each item, pull the key out of the header and then -+ * read the root_ref item it contains -+ */ -+ for (i = 0; i < sk->nr_items; i++) { -+ sh = (struct btrfs_ioctl_search_header *)(args.buf + -+ off); -+ off += sizeof(*sh); -+ if (sh->type == BTRFS_ROOT_BACKREF_KEY) { -+ ref = (struct btrfs_root_ref *)(args.buf + off); -+ name_len = btrfs_stack_root_ref_name_len(ref); -+ name = (char *)(ref + 1); -+ dir_id = btrfs_stack_root_ref_dirid(ref); -+ -+ add_root(root_lookup, sh->objectid, sh->offset, -+ dir_id, name, name_len); -+ } -+ -+ off += sh->len; -+ -+ /* -+ * record the mins in sk so we can make sure the -+ * next search doesn't repeat this root -+ */ -+ sk->min_objectid = sh->objectid; -+ sk->min_type = sh->type; -+ sk->min_offset = sh->offset; -+ } -+ sk->nr_items = 4096; -+ /* this iteration is done, step forward one root for the next -+ * ioctl -+ */ -+ if (sk->min_type < BTRFS_ROOT_BACKREF_KEY) { -+ sk->min_type = BTRFS_ROOT_BACKREF_KEY; -+ sk->min_offset = 0; -+ } else if (sk->min_objectid < (u64)-1) { -+ sk->min_objectid++; -+ sk->min_type = BTRFS_ROOT_BACKREF_KEY; -+ sk->min_offset = 0; -+ } else -+ break; -+ } -+ -+ return 0; -+} -+ -+static int __list_subvol_fill_paths(int fd, struct root_lookup *root_lookup) -+{ -+ struct rb_node *n; -+ -+ n = rb_first(&root_lookup->root); -+ while (n) { -+ struct root_info *entry; -+ int ret; -+ entry = rb_entry(n, struct root_info, rb_node); -+ ret = lookup_ino_path(fd, entry); -+ if(ret < 0) -+ return ret; -+ n = rb_next(n); -+ } -+ -+ return 0; -+} -+ -+int list_subvols(int fd, int print_parent) -+{ -+ struct root_lookup root_lookup; -+ struct rb_node *n; -+ int ret; -+ -+ ret = __list_subvol_search(fd, &root_lookup); -+ if (ret) { -+ fprintf(stderr, "ERROR: can't perform the search - %s\n", -+ strerror(errno)); -+ return ret; -+ } -+ -+ /* -+ * now we have an rbtree full of root_info objects, but we need to fill -+ * in their path names within the subvol that is referencing each one. -+ */ -+ ret = __list_subvol_fill_paths(fd, &root_lookup); -+ if (ret < 0) -+ return ret; -+ -+ /* now that we have all the subvol-relative paths filled in, -+ * we have to string the subvols together so that we can get -+ * a path all the way back to the FS root -+ */ -+ n = rb_last(&root_lookup.root); -+ while (n) { -+ struct root_info *entry; -+ u64 root_id; -+ u64 level; -+ u64 parent_id; -+ char *path; -+ entry = rb_entry(n, struct root_info, rb_node); -+ resolve_root(&root_lookup, entry, &root_id, &parent_id, -+ &level, &path); -+ if (print_parent) { -+ printf("ID %llu parent %llu top level %llu path %s\n", -+ (unsigned long long)root_id, -+ (unsigned long long)parent_id, -+ (unsigned long long)level, path); -+ } else { -+ printf("ID %llu top level %llu path %s\n", -+ (unsigned long long)root_id, -+ (unsigned long long)level, path); -+ } -+ free(path); -+ n = rb_prev(n); -+ } -+ -+ return ret; -+} -+ -+static int print_one_extent(int fd, struct btrfs_ioctl_search_header *sh, -+ struct btrfs_file_extent_item *item, -+ u64 found_gen, u64 *cache_dirid, -+ char **cache_dir_name, u64 *cache_ino, -+ char **cache_full_name) -+{ -+ u64 len = 0; -+ u64 disk_start = 0; -+ u64 disk_offset = 0; -+ u8 type; -+ int compressed = 0; -+ int flags = 0; -+ char *name = NULL; -+ -+ if (sh->objectid == *cache_ino) { -+ name = *cache_full_name; -+ } else if (*cache_full_name) { -+ free(*cache_full_name); -+ *cache_full_name = NULL; -+ } -+ if (!name) { -+ name = ino_resolve(fd, sh->objectid, cache_dirid, -+ cache_dir_name); -+ *cache_full_name = name; -+ *cache_ino = sh->objectid; -+ } -+ if (!name) -+ return -EIO; -+ -+ type = btrfs_stack_file_extent_type(item); -+ compressed = btrfs_stack_file_extent_compression(item); -+ -+ if (type == BTRFS_FILE_EXTENT_REG || -+ type == BTRFS_FILE_EXTENT_PREALLOC) { -+ disk_start = btrfs_stack_file_extent_disk_bytenr(item); -+ disk_offset = btrfs_stack_file_extent_offset(item); -+ len = btrfs_stack_file_extent_num_bytes(item); -+ } else if (type == BTRFS_FILE_EXTENT_INLINE) { -+ disk_start = 0; -+ disk_offset = 0; -+ len = btrfs_stack_file_extent_ram_bytes(item); -+ } else { -+ printf("unhandled extent type %d for inode %llu " -+ "file offset %llu gen %llu\n", -+ type, -+ (unsigned long long)sh->objectid, -+ (unsigned long long)sh->offset, -+ (unsigned long long)found_gen); -+ -+ return -EIO; -+ } -+ printf("inode %llu file offset %llu len %llu disk start %llu " -+ "offset %llu gen %llu flags ", -+ (unsigned long long)sh->objectid, -+ (unsigned long long)sh->offset, -+ (unsigned long long)len, -+ (unsigned long long)disk_start, -+ (unsigned long long)disk_offset, -+ (unsigned long long)found_gen); -+ -+ if (compressed) { -+ printf("COMPRESS"); -+ flags++; -+ } -+ if (type == BTRFS_FILE_EXTENT_PREALLOC) { -+ printf("%sPREALLOC", flags ? "|" : ""); -+ flags++; -+ } -+ if (type == BTRFS_FILE_EXTENT_INLINE) { -+ printf("%sINLINE", flags ? "|" : ""); -+ flags++; -+ } -+ if (!flags) -+ printf("NONE"); -+ -+ printf(" %s\n", name); -+ return 0; -+} -+ -+int find_updated_files(int fd, u64 root_id, u64 oldest_gen) -+{ -+ int ret; -+ struct btrfs_ioctl_search_args args; -+ struct btrfs_ioctl_search_key *sk = &args.key; -+ struct btrfs_ioctl_search_header *sh; -+ struct btrfs_file_extent_item *item; -+ unsigned long off = 0; -+ u64 found_gen; -+ u64 max_found = 0; -+ int i; -+ int e; -+ u64 cache_dirid = 0; -+ u64 cache_ino = 0; -+ char *cache_dir_name = NULL; -+ char *cache_full_name = NULL; -+ struct btrfs_file_extent_item backup; -+ -+ memset(&backup, 0, sizeof(backup)); -+ memset(&args, 0, sizeof(args)); -+ -+ sk->tree_id = root_id; -+ -+ /* -+ * set all the other params to the max, we'll take any objectid -+ * and any trans -+ */ -+ sk->max_objectid = (u64)-1; -+ sk->max_offset = (u64)-1; -+ sk->max_transid = (u64)-1; -+ sk->max_type = BTRFS_EXTENT_DATA_KEY; -+ sk->min_transid = oldest_gen; -+ /* just a big number, doesn't matter much */ -+ sk->nr_items = 4096; -+ -+ max_found = find_root_gen(fd); -+ while(1) { -+ ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args); -+ e = errno; -+ if (ret < 0) { -+ fprintf(stderr, "ERROR: can't perform the search- %s\n", -+ strerror(e)); -+ return ret; -+ } -+ /* the ioctl returns the number of item it found in nr_items */ -+ if (sk->nr_items == 0) -+ break; -+ -+ off = 0; -+ -+ /* -+ * for each item, pull the key out of the header and then -+ * read the root_ref item it contains -+ */ -+ for (i = 0; i < sk->nr_items; i++) { -+ sh = (struct btrfs_ioctl_search_header *)(args.buf + -+ off); -+ off += sizeof(*sh); -+ -+ /* -+ * just in case the item was too big, pass something other -+ * than garbage -+ */ -+ if (sh->len == 0) -+ item = &backup; -+ else -+ item = (struct btrfs_file_extent_item *)(args.buf + -+ off); -+ found_gen = btrfs_stack_file_extent_generation(item); -+ if (sh->type == BTRFS_EXTENT_DATA_KEY && -+ found_gen >= oldest_gen) { -+ print_one_extent(fd, sh, item, found_gen, -+ &cache_dirid, &cache_dir_name, -+ &cache_ino, &cache_full_name); -+ } -+ off += sh->len; -+ -+ /* -+ * record the mins in sk so we can make sure the -+ * next search doesn't repeat this root -+ */ -+ sk->min_objectid = sh->objectid; -+ sk->min_offset = sh->offset; -+ sk->min_type = sh->type; -+ } -+ sk->nr_items = 4096; -+ if (sk->min_offset < (u64)-1) -+ sk->min_offset++; -+ else if (sk->min_objectid < (u64)-1) { -+ sk->min_objectid++; -+ sk->min_offset = 0; -+ sk->min_type = 0; -+ } else -+ break; -+ } -+ free(cache_dir_name); -+ free(cache_full_name); -+ printf("transid marker was %llu\n", (unsigned long long)max_found); -+ return ret; -+} -+ -+char *path_for_root(int fd, u64 root) -+{ -+ struct root_lookup root_lookup; -+ struct rb_node *n; -+ char *ret_path = NULL; -+ int ret; -+ -+ ret = __list_subvol_search(fd, &root_lookup); -+ if (ret < 0) -+ return ERR_PTR(ret); -+ -+ ret = __list_subvol_fill_paths(fd, &root_lookup); -+ if (ret < 0) -+ return ERR_PTR(ret); -+ -+ n = rb_last(&root_lookup.root); -+ while (n) { -+ struct root_info *entry; -+ u64 root_id; -+ u64 parent_id; -+ u64 level; -+ char *path; -+ entry = rb_entry(n, struct root_info, rb_node); -+ resolve_root(&root_lookup, entry, &root_id, &parent_id, &level, -+ &path); -+ if (root_id == root) -+ ret_path = path; -+ else -+ free(path); -+ n = rb_prev(n); -+ } -+ -+ return ret_path; -+} -diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c -new file mode 100644 -index 0000000..d79a73a ---- /dev/null -+++ b/btrfs-map-logical.c -@@ -0,0 +1,220 @@ -+/* -+ * Copyright (C) 2009 Oracle. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _XOPEN_SOURCE 500 -+#define _GNU_SOURCE 1 -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "volumes.h" -+#include "disk-io.h" -+#include "print-tree.h" -+#include "transaction.h" -+#include "list.h" -+#include "version.h" -+ -+/* we write the mirror info to stdout unless they are dumping the data -+ * to stdout -+ * */ -+static FILE *info_file; -+ -+struct extent_buffer *debug_read_block(struct btrfs_root *root, u64 bytenr, -+ u32 blocksize, int copy) -+{ -+ int ret; -+ struct extent_buffer *eb; -+ u64 length; -+ struct btrfs_multi_bio *multi = NULL; -+ struct btrfs_device *device; -+ int num_copies; -+ int mirror_num = 1; -+ -+ eb = btrfs_find_create_tree_block(root, bytenr, blocksize); -+ if (!eb) -+ return NULL; -+ -+ length = blocksize; -+ while (1) { -+ ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, -+ eb->start, &length, &multi, mirror_num); -+ BUG_ON(ret); -+ device = multi->stripes[0].dev; -+ eb->fd = device->fd; -+ device->total_ios++; -+ eb->dev_bytenr = multi->stripes[0].physical; -+ -+ fprintf(info_file, "mirror %d logical %Lu physical %Lu " -+ "device %s\n", mirror_num, (unsigned long long)bytenr, -+ (unsigned long long)eb->dev_bytenr, device->name); -+ kfree(multi); -+ -+ if (!copy || mirror_num == copy) -+ ret = read_extent_from_disk(eb); -+ -+ num_copies = btrfs_num_copies(&root->fs_info->mapping_tree, -+ eb->start, eb->len); -+ if (num_copies == 1) -+ break; -+ -+ mirror_num++; -+ if (mirror_num > num_copies) -+ break; -+ } -+ return eb; -+} -+ -+static void print_usage(void) -+{ -+ fprintf(stderr, "usage: btrfs-map-logical [options] mount_point\n"); -+ fprintf(stderr, "\t-l Logical extent to map\n"); -+ fprintf(stderr, "\t-c Copy of the extent to read (usually 1 or 2)\n"); -+ fprintf(stderr, "\t-o Output file to hold the extent\n"); -+ fprintf(stderr, "\t-b Number of bytes to read\n"); -+ exit(1); -+} -+ -+static struct option long_options[] = { -+ /* { "byte-count", 1, NULL, 'b' }, */ -+ { "logical", 1, NULL, 'l' }, -+ { "copy", 1, NULL, 'c' }, -+ { "output", 1, NULL, 'c' }, -+ { "bytes", 1, NULL, 'b' }, -+ { 0, 0, 0, 0} -+}; -+ -+int main(int ac, char **av) -+{ -+ struct cache_tree root_cache; -+ struct btrfs_root *root; -+ struct extent_buffer *eb; -+ char *dev; -+ char *output_file = NULL; -+ u64 logical = 0; -+ int ret = 0; -+ int option_index = 0; -+ int copy = 0; -+ u64 bytes = 0; -+ int out_fd = 0; -+ int err; -+ -+ while(1) { -+ int c; -+ c = getopt_long(ac, av, "l:c:o:b:", long_options, -+ &option_index); -+ if (c < 0) -+ break; -+ switch(c) { -+ case 'l': -+ logical = atoll(optarg); -+ if (logical == 0) { -+ fprintf(stderr, -+ "invalid extent number\n"); -+ print_usage(); -+ } -+ break; -+ case 'c': -+ copy = atoi(optarg); -+ if (copy == 0) { -+ fprintf(stderr, -+ "invalid copy number\n"); -+ print_usage(); -+ } -+ break; -+ case 'b': -+ bytes = atoll(optarg); -+ if (bytes == 0) { -+ fprintf(stderr, -+ "invalid byte count\n"); -+ print_usage(); -+ } -+ break; -+ case 'o': -+ output_file = strdup(optarg); -+ break; -+ default: -+ print_usage(); -+ } -+ } -+ ac = ac - optind; -+ if (ac == 0) -+ print_usage(); -+ if (logical == 0) -+ print_usage(); -+ if (copy < 0) -+ print_usage(); -+ -+ dev = av[optind]; -+ -+ radix_tree_init(); -+ cache_tree_init(&root_cache); -+ -+ root = open_ctree(dev, 0, 0); -+ if (!root) { -+ fprintf(stderr, "Open ctree failed\n"); -+ exit(1); -+ } -+ -+ info_file = stdout; -+ if (output_file) { -+ if (strcmp(output_file, "-") == 0) { -+ out_fd = 1; -+ info_file = stderr; -+ } else { -+ out_fd = open(output_file, O_RDWR | O_CREAT, 0600); -+ if (out_fd < 0) -+ goto close; -+ err = ftruncate(out_fd, 0); -+ if (err) { -+ close(out_fd); -+ goto close; -+ } -+ info_file = stdout; -+ } -+ } -+ -+ if (bytes == 0) -+ bytes = root->sectorsize; -+ -+ bytes = (bytes + root->sectorsize - 1) / root->sectorsize; -+ bytes *= root->sectorsize; -+ -+ while (bytes > 0) { -+ eb = debug_read_block(root, logical, root->sectorsize, copy); -+ if (eb && output_file) { -+ err = write(out_fd, eb->data, eb->len); -+ if (err < 0 || err != eb->len) { -+ fprintf(stderr, "output file write failed\n"); -+ goto out_close_fd; -+ } -+ } -+ free_extent_buffer(eb); -+ logical += root->sectorsize; -+ bytes -= root->sectorsize; -+ } -+ -+out_close_fd: -+ if (output_file && out_fd != 1) -+ close(out_fd); -+close: -+ close_ctree(root); -+ return ret; -+} -diff --git a/btrfs-select-super.c b/btrfs-select-super.c -new file mode 100644 -index 0000000..51eb9c9 ---- /dev/null -+++ b/btrfs-select-super.c -@@ -0,0 +1,99 @@ -+/* -+ * Copyright (C) 2007 Oracle. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _XOPEN_SOURCE 500 -+#define _GNU_SOURCE 1 -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "disk-io.h" -+#include "print-tree.h" -+#include "transaction.h" -+#include "list.h" -+#include "version.h" -+#include "utils.h" -+ -+static void print_usage(void) -+{ -+ fprintf(stderr, "usage: btrfs-select-super -s number dev\n"); -+ fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION); -+ exit(1); -+} -+ -+int main(int ac, char **av) -+{ -+ struct btrfs_root *root; -+ int ret; -+ int num; -+ u64 bytenr = 0; -+ -+ while(1) { -+ int c; -+ c = getopt(ac, av, "s:"); -+ if (c < 0) -+ break; -+ switch(c) { -+ case 's': -+ num = atol(optarg); -+ bytenr = btrfs_sb_offset(num); -+ printf("using SB copy %d, bytenr %llu\n", num, -+ (unsigned long long)bytenr); -+ break; -+ default: -+ print_usage(); -+ } -+ } -+ ac = ac - optind; -+ -+ if (ac != 1) -+ print_usage(); -+ -+ if (bytenr == 0) { -+ fprintf(stderr, "Please select the super copy with -s\n"); -+ print_usage(); -+ } -+ -+ radix_tree_init(); -+ -+ if((ret = check_mounted(av[optind])) < 0) { -+ fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret)); -+ return ret; -+ } else if(ret) { -+ fprintf(stderr, "%s is currently mounted. Aborting.\n", av[optind]); -+ return -EBUSY; -+ } -+ -+ root = open_ctree(av[optind], bytenr, 1); -+ -+ if (root == NULL) -+ return 1; -+ -+ /* make the super writing code think we've read the first super */ -+ root->fs_info->super_bytenr = BTRFS_SUPER_INFO_OFFSET; -+ ret = write_all_supers(root); -+ -+ /* we don't close the ctree or anything, because we don't want a real -+ * transaction commit. We just want the super copy we pulled off the -+ * disk to overwrite all the other copies -+ */ -+ return ret; -+} -diff --git a/btrfs-show.c b/btrfs-show.c -index c49626c..8210fd2 100644 ---- a/btrfs-show.c -+++ b/btrfs-show.c -@@ -117,6 +117,11 @@ int main(int ac, char **av) - int ret; - int option_index = 0; - -+ printf( "**\n" -+ "** WARNING: this program is considered deprecated\n" -+ "** Please consider to switch to the btrfs utility\n" -+ "**\n"); -+ - while(1) { - int c; - c = getopt_long(ac, av, "", long_options, -diff --git a/btrfs-vol.c b/btrfs-vol.c -index 8069778..0efdbc1 100644 ---- a/btrfs-vol.c -+++ b/btrfs-vol.c -@@ -78,6 +78,11 @@ int main(int ac, char **av) - struct btrfs_ioctl_vol_args args; - u64 dev_block_count = 0; - -+ printf( "**\n" -+ "** WARNING: this program is considered deprecated\n" -+ "** Please consider to switch to the btrfs utility\n" -+ "**\n"); -+ - while(1) { - int c; - c = getopt_long(ac, av, "a:br:", long_options, -@@ -108,10 +113,24 @@ int main(int ac, char **av) - if (device && strcmp(device, "missing") == 0 && - cmd == BTRFS_IOC_RM_DEV) { - fprintf(stderr, "removing missing devices from %s\n", mnt); -- } else if (device) { -+ } else if (cmd != BTRFS_IOC_BALANCE) { -+ if (cmd == BTRFS_IOC_ADD_DEV) { -+ ret = check_mounted(device); -+ if (ret < 0) { -+ fprintf(stderr, -+ "error checking %s mount status\n", -+ device); -+ exit(1); -+ } -+ if (ret == 1) { -+ fprintf(stderr, "%s is mounted\n", device); -+ exit(1); -+ } -+ } - devfd = open(device, O_RDWR); -- if (!devfd) { -+ if (devfd < 0) { - fprintf(stderr, "Unable to open device %s\n", device); -+ exit(1); - } - ret = fstat(devfd, &st); - if (ret) { -@@ -129,7 +148,9 @@ int main(int ac, char **av) - exit(1); - } - if (cmd == BTRFS_IOC_ADD_DEV) { -- ret = btrfs_prepare_device(devfd, device, 1, &dev_block_count); -+ int mixed = 0; -+ -+ ret = btrfs_prepare_device(devfd, device, 1, &dev_block_count, &mixed); - if (ret) { - fprintf(stderr, "Unable to init %s\n", device); - exit(1); -diff --git a/btrfs-zero-log.c b/btrfs-zero-log.c -new file mode 100644 -index 0000000..1ea867b ---- /dev/null -+++ b/btrfs-zero-log.c -@@ -0,0 +1,72 @@ -+/* -+ * Copyright (C) 2007 Oracle. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _XOPEN_SOURCE 500 -+#define _GNU_SOURCE 1 -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "disk-io.h" -+#include "print-tree.h" -+#include "transaction.h" -+#include "list.h" -+#include "version.h" -+#include "utils.h" -+ -+static void print_usage(void) -+{ -+ fprintf(stderr, "usage: btrfs-zero-log dev\n"); -+ fprintf(stderr, "%s\n", BTRFS_BUILD_VERSION); -+ exit(1); -+} -+ -+int main(int ac, char **av) -+{ -+ struct btrfs_root *root; -+ struct btrfs_trans_handle *trans; -+ int ret; -+ -+ if (ac != 2) -+ print_usage(); -+ -+ radix_tree_init(); -+ -+ if((ret = check_mounted(av[1])) < 0) { -+ fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret)); -+ return ret; -+ } else if(ret) { -+ fprintf(stderr, "%s is currently mounted. Aborting.\n", av[1]); -+ return -EBUSY; -+ } -+ -+ root = open_ctree(av[1], 0, 1); -+ -+ if (root == NULL) -+ return 1; -+ -+ trans = btrfs_start_transaction(root, 1); -+ btrfs_set_super_log_root(&root->fs_info->super_copy, 0); -+ btrfs_set_super_log_root_level(&root->fs_info->super_copy, 0); -+ btrfs_commit_transaction(trans, root); -+ close_ctree(root); -+ return ret; -+} -diff --git a/btrfs.c b/btrfs.c -new file mode 100644 -index 0000000..88238d6 ---- /dev/null -+++ b/btrfs.c -@@ -0,0 +1,276 @@ -+/* -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _GNU_SOURCE -+#include -+#include -+#include -+ -+#include "commands.h" -+#include "version.h" -+ -+static const char * const btrfs_cmd_group_usage[] = { -+ "btrfs [--help] [--version] [...] []", -+ NULL -+}; -+ -+static const char btrfs_cmd_group_info[] = -+ "Use --help as an argument for information on a specific group or command."; -+ -+char argv0_buf[ARGV0_BUF_SIZE] = "btrfs"; -+ -+static inline const char *skip_prefix(const char *str, const char *prefix) -+{ -+ size_t len = strlen(prefix); -+ return strncmp(str, prefix, len) ? NULL : str + len; -+} -+ -+int prefixcmp(const char *str, const char *prefix) -+{ -+ for (; ; str++, prefix++) -+ if (!*prefix) -+ return 0; -+ else if (*str != *prefix) -+ return (unsigned char)*prefix - (unsigned char)*str; -+} -+ -+static int parse_one_token(const char *arg, const struct cmd_group *grp, -+ const struct cmd_struct **cmd_ret) -+{ -+ const struct cmd_struct *cmd = grp->commands; -+ const struct cmd_struct *abbrev_cmd = NULL, *ambiguous_cmd = NULL; -+ -+ for (; cmd->token; cmd++) { -+ const char *rest; -+ -+ rest = skip_prefix(arg, cmd->token); -+ if (!rest) { -+ if (!prefixcmp(cmd->token, arg)) { -+ if (abbrev_cmd) { -+ /* -+ * If this is abbreviated, it is -+ * ambiguous. So when there is no -+ * exact match later, we need to -+ * error out. -+ */ -+ ambiguous_cmd = abbrev_cmd; -+ } -+ abbrev_cmd = cmd; -+ } -+ continue; -+ } -+ if (*rest) -+ continue; -+ -+ *cmd_ret = cmd; -+ return 0; -+ } -+ -+ if (ambiguous_cmd) -+ return -2; -+ -+ if (abbrev_cmd) { -+ *cmd_ret = abbrev_cmd; -+ return 0; -+ } -+ -+ return -1; -+} -+ -+static const struct cmd_struct * -+parse_command_token(const char *arg, const struct cmd_group *grp) -+{ -+ const struct cmd_struct *cmd; -+ -+ switch(parse_one_token(arg, grp, &cmd)) { -+ case -1: -+ help_unknown_token(arg, grp); -+ case -2: -+ help_ambiguous_token(arg, grp); -+ } -+ -+ return cmd; -+} -+ -+void handle_help_options_next_level(const struct cmd_struct *cmd, -+ int argc, char **argv) -+{ -+ if (argc < 2) -+ return; -+ -+ if (!strcmp(argv[1], "--help")) { -+ if (cmd->next) { -+ argc--; -+ argv++; -+ help_command_group(cmd->next, argc, argv); -+ } else { -+ usage_command(cmd, 1, 0); -+ } -+ -+ exit(0); -+ } -+} -+ -+static void fixup_argv0(char **argv, const char *token) -+{ -+ int len = strlen(argv0_buf); -+ -+ snprintf(argv0_buf + len, sizeof(argv0_buf) - len, " %s", token); -+ argv[0] = argv0_buf; -+} -+ -+int handle_command_group(const struct cmd_group *grp, int argc, -+ char **argv) -+ -+{ -+ const struct cmd_struct *cmd; -+ -+ argc--; -+ argv++; -+ if (argc < 1) { -+ usage_command_group(grp, 0, 0); -+ exit(1); -+ } -+ -+ cmd = parse_command_token(argv[0], grp); -+ -+ handle_help_options_next_level(cmd, argc, argv); -+ -+ fixup_argv0(argv, cmd->token); -+ return cmd->fn(argc, argv); -+} -+ -+int check_argc_exact(int nargs, int expected) -+{ -+ if (nargs < expected) -+ fprintf(stderr, "%s: too few arguments\n", argv0_buf); -+ if (nargs > expected) -+ fprintf(stderr, "%s: too many arguments\n", argv0_buf); -+ -+ return nargs != expected; -+} -+ -+int check_argc_min(int nargs, int expected) -+{ -+ if (nargs < expected) { -+ fprintf(stderr, "%s: too few arguments\n", argv0_buf); -+ return 1; -+ } -+ -+ return 0; -+} -+ -+int check_argc_max(int nargs, int expected) -+{ -+ if (nargs > expected) { -+ fprintf(stderr, "%s: too many arguments\n", argv0_buf); -+ return 1; -+ } -+ -+ return 0; -+} -+ -+const struct cmd_group btrfs_cmd_group; -+ -+static const char * const cmd_help_usage[] = { -+ "btrfs help [--full]", -+ "Dislay help information", -+ "", -+ "--full display detailed help on every command", -+ NULL -+}; -+ -+static int cmd_help(int argc, char **argv) -+{ -+ help_command_group(&btrfs_cmd_group, argc, argv); -+ return 0; -+} -+ -+static const char * const cmd_version_usage[] = { -+ "btrfs version", -+ "Display btrfs-progs version", -+ NULL -+}; -+ -+static int cmd_version(int argc, char **argv) -+{ -+ printf("%s\n", BTRFS_BUILD_VERSION); -+ return 0; -+} -+ -+static int handle_options(int *argc, char ***argv) -+{ -+ char **orig_argv = *argv; -+ -+ while (*argc > 0) { -+ const char *arg = (*argv)[0]; -+ if (arg[0] != '-') -+ break; -+ -+ if (!strcmp(arg, "--help")) { -+ break; -+ } else if (!strcmp(arg, "--version")) { -+ break; -+ } else { -+ fprintf(stderr, "Unknown option: %s\n", arg); -+ fprintf(stderr, "usage: %s\n", -+ btrfs_cmd_group.usagestr[0]); -+ exit(129); -+ } -+ -+ (*argv)++; -+ (*argc)--; -+ } -+ -+ return (*argv) - orig_argv; -+} -+ -+const struct cmd_group btrfs_cmd_group = { -+ btrfs_cmd_group_usage, btrfs_cmd_group_info, { -+ { "subvolume", cmd_subvolume, NULL, &subvolume_cmd_group, 0 }, -+ { "filesystem", cmd_filesystem, NULL, &filesystem_cmd_group, 0 }, -+ { "balance", cmd_balance, NULL, &balance_cmd_group, 0 }, -+ { "device", cmd_device, NULL, &device_cmd_group, 0 }, -+ { "scrub", cmd_scrub, NULL, &scrub_cmd_group, 0 }, -+ { "inspect-internal", cmd_inspect, NULL, &inspect_cmd_group, 0 }, -+ { "help", cmd_help, cmd_help_usage, NULL, 0 }, -+ { "version", cmd_version, cmd_version_usage, NULL, 0 }, -+ { 0, 0, 0, 0, 0 } -+ }, -+}; -+ -+int main(int argc, char **argv) -+{ -+ const struct cmd_struct *cmd; -+ -+ argc--; -+ argv++; -+ handle_options(&argc, &argv); -+ if (argc > 0) { -+ if (!prefixcmp(argv[0], "--")) -+ argv[0] += 2; -+ } else { -+ usage_command_group(&btrfs_cmd_group, 0, 0); -+ exit(1); -+ } -+ -+ cmd = parse_command_token(argv[0], &btrfs_cmd_group); -+ -+ handle_help_options_next_level(cmd, argc, argv); -+ -+ fixup_argv0(argv, cmd->token); -+ exit(cmd->fn(argc, argv)); -+} -diff --git a/btrfsck.c b/btrfsck.c -index 40c90f8..7aac736 100644 ---- a/btrfsck.c -+++ b/btrfsck.c -@@ -20,14 +20,20 @@ - #define _GNU_SOURCE 1 - #include - #include -+#include - #include -+#include -+#include - #include "kerncompat.h" - #include "ctree.h" -+#include "volumes.h" -+#include "repair.h" - #include "disk-io.h" - #include "print-tree.h" - #include "transaction.h" - #include "list.h" - #include "version.h" -+#include "utils.h" - - static u64 bytes_used = 0; - static u64 total_csum_bytes = 0; -@@ -36,7 +42,7 @@ static u64 total_fs_tree_bytes = 0; - static u64 btree_space_waste = 0; - static u64 data_bytes_allocated = 0; - static u64 data_bytes_referenced = 0; --int found_old_backref = 0; -+static int found_old_backref = 0; - - struct extent_backref { - struct list_head list; -@@ -71,9 +77,13 @@ struct extent_record { - struct cache_extent cache; - struct btrfs_disk_key parent_key; - u64 start; -+ u64 max_size; - u64 nr; - u64 refs; - u64 extent_item_refs; -+ u64 generation; -+ u64 info_objectid; -+ u8 info_level; - unsigned int content_checked:1; - unsigned int owner_ref_checked:1; - unsigned int is_root:1; -@@ -100,7 +110,11 @@ struct inode_backref { - #define REF_ERR_DUP_INODE_REF (1 << 5) - #define REF_ERR_INDEX_UNMATCH (1 << 6) - #define REF_ERR_FILETYPE_UNMATCH (1 << 7) --#define REF_ERR_NAME_TOO_LONG (1 << 8) -+#define REF_ERR_NAME_TOO_LONG (1 << 8) // 100 -+#define REF_ERR_NO_ROOT_REF (1 << 9) -+#define REF_ERR_NO_ROOT_BACKREF (1 << 10) -+#define REF_ERR_DUP_ROOT_REF (1 << 11) -+#define REF_ERR_DUP_ROOT_BACKREF (1 << 12) - - struct inode_record { - struct list_head backrefs; -@@ -144,6 +158,29 @@ struct inode_record { - #define I_ERR_SOME_CSUM_MISSING (1 << 12) - #define I_ERR_LINK_COUNT_WRONG (1 << 13) - -+struct root_backref { -+ struct list_head list; -+ unsigned int found_dir_item:1; -+ unsigned int found_dir_index:1; -+ unsigned int found_back_ref:1; -+ unsigned int found_forward_ref:1; -+ unsigned int reachable:1; -+ int errors; -+ u64 ref_root; -+ u64 dir; -+ u64 index; -+ u16 namelen; -+ char name[0]; -+}; -+ -+struct root_record { -+ struct list_head backrefs; -+ struct cache_extent cache; -+ unsigned int found_root_item:1; -+ u64 objectid; -+ u32 found_ref; -+}; -+ - struct ptr_node { - struct cache_extent cache; - void *data; -@@ -151,6 +188,7 @@ struct ptr_node { - - struct shared_node { - struct cache_extent cache; -+ struct cache_tree root_cache; - struct cache_tree inode_cache; - struct inode_record *current; - u32 refs; -@@ -258,6 +296,14 @@ static void free_inode_rec(struct inode_record *rec) - free(rec); - } - -+static int can_free_inode_rec(struct inode_record *rec) -+{ -+ if (!rec->errors && rec->checked && rec->found_inode_item && -+ rec->nlink == rec->found_link && list_empty(&rec->backrefs)) -+ return 1; -+ return 0; -+} -+ - static void maybe_free_inode_rec(struct cache_tree *inode_cache, - struct inode_record *rec) - { -@@ -309,8 +355,7 @@ static void maybe_free_inode_rec(struct cache_tree *inode_cache, - } - - BUG_ON(rec->refs != 1); -- if (!rec->errors && rec->nlink == rec->found_link && -- list_empty(&rec->backrefs)) { -+ if (can_free_inode_rec(rec)) { - cache = find_cache_extent(inode_cache, rec->ino, 1); - node = container_of(cache, struct ptr_node, cache); - BUG_ON(node->data != rec); -@@ -338,14 +383,12 @@ static int check_orphan_item(struct btrfs_root *root, u64 ino) - return ret; - } - --static int process_inode_item(struct btrfs_root *root, -- struct extent_buffer *eb, -+static int process_inode_item(struct extent_buffer *eb, - int slot, struct btrfs_key *key, - struct shared_node *active_node) - { - struct inode_record *rec; - struct btrfs_inode_item *item; -- int ret; - - rec = active_node->current; - BUG_ON(rec->ino != key->objectid || rec->refs > 1); -@@ -361,11 +404,8 @@ static int process_inode_item(struct btrfs_root *root, - if (btrfs_inode_flags(eb, item) & BTRFS_INODE_NODATASUM) - rec->nodatasum = 1; - rec->found_inode_item = 1; -- if (rec->nlink == 0) { -- ret = check_orphan_item(root, rec->ino); -- if (ret == -ENOENT) -- rec->errors |= I_ERR_NO_ORPHAN_ITEM; -- } -+ if (rec->nlink == 0) -+ rec->errors |= I_ERR_NO_ORPHAN_ITEM; - maybe_free_inode_rec(&active_node->inode_cache, rec); - return 0; - } -@@ -391,7 +431,6 @@ static struct inode_backref *get_inode_backref(struct inode_record *rec, - memcpy(backref->name, name, namelen); - backref->name[namelen] = '\0'; - list_add_tail(&backref->list, &rec->backrefs); -- rec->found_link++; - return backref; - } - -@@ -419,6 +458,7 @@ static int add_inode_backref(struct cache_tree *inode_cache, - backref->filetype = filetype; - backref->found_dir_index = 1; - } else if (itemtype == BTRFS_DIR_ITEM_KEY) { -+ rec->found_link++; - if (backref->found_dir_item) - backref->errors |= REF_ERR_DUP_DIR_ITEM; - if (backref->found_dir_index && backref->filetype != filetype) -@@ -443,10 +483,10 @@ static int add_inode_backref(struct cache_tree *inode_cache, - } - - static int merge_inode_recs(struct inode_record *src, struct inode_record *dst, -- struct shared_node *dst_node) -+ struct cache_tree *dst_cache) - { - struct inode_backref *backref; -- struct cache_tree *dst_cache = &dst_node->inode_cache; -+ u32 dir_count = 0; - - dst->merging = 1; - list_for_each_entry(backref, &src->backrefs, list) { -@@ -457,6 +497,7 @@ static int merge_inode_recs(struct inode_record *src, struct inode_record *dst, - BTRFS_DIR_INDEX_KEY, backref->errors); - } - if (backref->found_dir_item) { -+ dir_count++; - add_inode_backref(dst_cache, dst->ino, - backref->dir, 0, backref->name, - backref->namelen, backref->filetype, -@@ -481,6 +522,8 @@ static int merge_inode_recs(struct inode_record *src, struct inode_record *dst, - if (dst->first_extent_gap > src->first_extent_gap) - dst->first_extent_gap = src->first_extent_gap; - -+ BUG_ON(src->found_link < dir_count); -+ dst->found_link += src->found_link - dir_count; - dst->found_size += src->found_size; - if (src->extent_start != (u64)-1) { - if (dst->extent_start == (u64)-1) { -@@ -510,14 +553,8 @@ static int merge_inode_recs(struct inode_record *src, struct inode_record *dst, - dst->errors |= I_ERR_DUP_INODE_ITEM; - } - } -- -- if (src->checked) { -- dst->checked = 1; -- if (dst_node->current == dst) -- dst_node->current = NULL; -- } - dst->merging = 0; -- maybe_free_inode_rec(dst_cache, dst); -+ - return 0; - } - -@@ -537,8 +574,9 @@ static int splice_shared_node(struct shared_node *src_node, - if (src_node->current) - current_ino = src_node->current->ino; - -- src = &src_node->inode_cache; -- dst = &dst_node->inode_cache; -+ src = &src_node->root_cache; -+ dst = &dst_node->root_cache; -+again: - cache = find_first_cache_extent(src, 0); - while (cache) { - node = container_of(cache, struct ptr_node, cache); -@@ -558,13 +596,26 @@ static int splice_shared_node(struct shared_node *src_node, - ret = insert_existing_cache_extent(dst, &ins->cache); - if (ret == -EEXIST) { - conflict = get_inode_rec(dst, rec->ino, 1); -- merge_inode_recs(rec, conflict, dst_node); -+ merge_inode_recs(rec, conflict, dst); -+ if (rec->checked) { -+ conflict->checked = 1; -+ if (dst_node->current == conflict) -+ dst_node->current = NULL; -+ } -+ maybe_free_inode_rec(dst, conflict); - free_inode_rec(rec); - free(ins); - } else { - BUG_ON(ret); - } - } -+ -+ if (src == &src_node->root_cache) { -+ src = &src_node->inode_cache; -+ dst = &dst_node->inode_cache; -+ goto again; -+ } -+ - if (current_ino > 0 && (!dst_node->current || - current_ino > dst_node->current->ino)) { - if (dst_node->current) { -@@ -616,6 +667,7 @@ static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs) - node = calloc(1, sizeof(*node)); - node->cache.start = bytenr; - node->cache.size = 1; -+ cache_tree_init(&node->root_cache); - cache_tree_init(&node->inode_cache); - node->refs = refs; - -@@ -646,6 +698,7 @@ static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs, - if (wc->root_level == wc->active_node && - btrfs_root_refs(&root->root_item) == 0) { - if (--node->refs == 0) { -+ free_inode_recs(&node->root_cache); - free_inode_recs(&node->inode_cache); - remove_cache_extent(&wc->shared, &node->cache); - free(node); -@@ -708,10 +761,12 @@ static int process_dir_item(struct extent_buffer *eb, - int filetype; - struct btrfs_dir_item *di; - struct inode_record *rec; -+ struct cache_tree *root_cache; - struct cache_tree *inode_cache; - struct btrfs_key location; - char namebuf[BTRFS_NAME_LEN]; - -+ root_cache = &active_node->root_cache; - inode_cache = &active_node->inode_cache; - rec = active_node->current; - rec->found_dir_item = 1; -@@ -740,7 +795,9 @@ static int process_dir_item(struct extent_buffer *eb, - key->objectid, key->offset, namebuf, - len, filetype, key->type, error); - } else if (location.type == BTRFS_ROOT_ITEM_KEY) { -- /* fixme: check root back & forward references */ -+ add_inode_backref(root_cache, location.objectid, -+ key->objectid, key->offset, namebuf, -+ len, filetype, key->type, error); - } else { - fprintf(stderr, "warning line %d\n", __LINE__); - } -@@ -945,7 +1002,7 @@ static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb, - struct btrfs_key key; - u32 nritems; - int i; -- int ret; -+ int ret = 0; - struct cache_tree *inode_cache; - struct shared_node *active_node; - -@@ -977,8 +1034,7 @@ static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb, - ret = process_inode_ref(eb, i, &key, active_node); - break; - case BTRFS_INODE_ITEM_KEY: -- ret = process_inode_item(root, eb, i, &key, -- active_node); -+ ret = process_inode_item(eb, i, &key, active_node); - break; - case BTRFS_EXTENT_DATA_KEY: - ret = process_file_extent(root, eb, i, &key, -@@ -988,7 +1044,7 @@ static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb, - break; - }; - } -- return 0; -+ return ret; - } - - static void reada_walk_down(struct btrfs_root *root, -@@ -1033,7 +1089,9 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path, - ret = btrfs_lookup_extent_info(NULL, root, - path->nodes[*level]->start, - path->nodes[*level]->len, &refs, NULL); -- BUG_ON(ret); -+ if (ret < 0) -+ goto out; -+ - if (refs > 1) { - ret = enter_shared_node(root, path->nodes[*level]->start, - refs, wc, *level); -@@ -1060,7 +1118,8 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path, - blocksize = btrfs_level_size(root, *level - 1); - ret = btrfs_lookup_extent_info(NULL, root, bytenr, blocksize, - &refs, NULL); -- BUG_ON(ret); -+ if (ret < 0) -+ refs = 0; - - if (refs > 1) { - ret = enter_shared_node(root, bytenr, refs, -@@ -1120,7 +1179,7 @@ static int check_root_dir(struct inode_record *rec) - - if (!rec->found_inode_item || rec->errors) - goto out; -- if (rec->nlink != 1 || rec->found_link != 1) -+ if (rec->nlink != 1 || rec->found_link != 0) - goto out; - if (list_empty(&rec->backrefs)) - goto out; -@@ -1176,13 +1235,23 @@ static int check_inode_recs(struct btrfs_root *root, - node = container_of(cache, struct ptr_node, cache); - rec = node->data; - remove_cache_extent(inode_cache, &node->cache); -+ free(node); - if (rec->ino == root_dirid || - rec->ino == BTRFS_ORPHAN_OBJECTID) { -- free(node); - free_inode_rec(rec); - continue; - } - -+ if (rec->errors & I_ERR_NO_ORPHAN_ITEM) { -+ ret = check_orphan_item(root, rec->ino); -+ if (ret == 0) -+ rec->errors &= ~I_ERR_NO_ORPHAN_ITEM; -+ if (can_free_inode_rec(rec)) { -+ free_inode_rec(rec); -+ continue; -+ } -+ } -+ - error++; - if (!rec->found_inode_item) - rec->errors |= I_ERR_NO_INODE_ITEM; -@@ -1205,13 +1274,314 @@ static int check_inode_recs(struct btrfs_root *root, - backref->namelen, backref->name, - backref->filetype, backref->errors); - } -- free(node); - free_inode_rec(rec); - } - return (error > 0) ? -1 : 0; - } - -+static struct root_record *get_root_rec(struct cache_tree *root_cache, -+ u64 objectid) -+{ -+ struct cache_extent *cache; -+ struct root_record *rec = NULL; -+ int ret; -+ -+ cache = find_cache_extent(root_cache, objectid, 1); -+ if (cache) { -+ rec = container_of(cache, struct root_record, cache); -+ } else { -+ rec = calloc(1, sizeof(*rec)); -+ rec->objectid = objectid; -+ INIT_LIST_HEAD(&rec->backrefs); -+ rec->cache.start = objectid; -+ rec->cache.size = 1; -+ -+ ret = insert_existing_cache_extent(root_cache, &rec->cache); -+ BUG_ON(ret); -+ } -+ return rec; -+} -+ -+static struct root_backref *get_root_backref(struct root_record *rec, -+ u64 ref_root, u64 dir, u64 index, -+ const char *name, int namelen) -+{ -+ struct root_backref *backref; -+ -+ list_for_each_entry(backref, &rec->backrefs, list) { -+ if (backref->ref_root != ref_root || backref->dir != dir || -+ backref->namelen != namelen) -+ continue; -+ if (memcmp(name, backref->name, namelen)) -+ continue; -+ return backref; -+ } -+ -+ backref = malloc(sizeof(*backref) + namelen + 1); -+ memset(backref, 0, sizeof(*backref)); -+ backref->ref_root = ref_root; -+ backref->dir = dir; -+ backref->index = index; -+ backref->namelen = namelen; -+ memcpy(backref->name, name, namelen); -+ backref->name[namelen] = '\0'; -+ list_add_tail(&backref->list, &rec->backrefs); -+ return backref; -+} -+ -+static void free_root_recs(struct cache_tree *root_cache) -+{ -+ struct cache_extent *cache; -+ struct root_record *rec; -+ struct root_backref *backref; -+ -+ while (1) { -+ cache = find_first_cache_extent(root_cache, 0); -+ if (!cache) -+ break; -+ rec = container_of(cache, struct root_record, cache); -+ remove_cache_extent(root_cache, &rec->cache); -+ -+ while (!list_empty(&rec->backrefs)) { -+ backref = list_entry(rec->backrefs.next, -+ struct root_backref, list); -+ list_del(&backref->list); -+ free(backref); -+ } -+ kfree(rec); -+ } -+} -+ -+static int add_root_backref(struct cache_tree *root_cache, -+ u64 root_id, u64 ref_root, u64 dir, u64 index, -+ const char *name, int namelen, -+ int item_type, int errors) -+{ -+ struct root_record *rec; -+ struct root_backref *backref; -+ -+ rec = get_root_rec(root_cache, root_id); -+ backref = get_root_backref(rec, ref_root, dir, index, name, namelen); -+ -+ backref->errors |= errors; -+ -+ if (item_type != BTRFS_DIR_ITEM_KEY) { -+ if (backref->found_dir_index || backref->found_back_ref || -+ backref->found_forward_ref) { -+ if (backref->index != index) -+ backref->errors |= REF_ERR_INDEX_UNMATCH; -+ } else { -+ backref->index = index; -+ } -+ } -+ -+ if (item_type == BTRFS_DIR_ITEM_KEY) { -+ backref->found_dir_item = 1; -+ backref->reachable = 1; -+ rec->found_ref++; -+ } else if (item_type == BTRFS_DIR_INDEX_KEY) { -+ backref->found_dir_index = 1; -+ } else if (item_type == BTRFS_ROOT_REF_KEY) { -+ if (backref->found_forward_ref) -+ backref->errors |= REF_ERR_DUP_ROOT_REF; -+ backref->found_forward_ref = 1; -+ } else if (item_type == BTRFS_ROOT_BACKREF_KEY) { -+ if (backref->found_back_ref) -+ backref->errors |= REF_ERR_DUP_ROOT_BACKREF; -+ backref->found_back_ref = 1; -+ } else { -+ BUG_ON(1); -+ } -+ -+ return 0; -+} -+ -+static int merge_root_recs(struct btrfs_root *root, -+ struct cache_tree *src_cache, -+ struct cache_tree *dst_cache) -+{ -+ struct cache_extent *cache; -+ struct ptr_node *node; -+ struct inode_record *rec; -+ struct inode_backref *backref; -+ -+ if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) { -+ free_inode_recs(src_cache); -+ return 0; -+ } -+ -+ while (1) { -+ cache = find_first_cache_extent(src_cache, 0); -+ if (!cache) -+ break; -+ node = container_of(cache, struct ptr_node, cache); -+ rec = node->data; -+ remove_cache_extent(src_cache, &node->cache); -+ free(node); -+ -+ list_for_each_entry(backref, &rec->backrefs, list) { -+ BUG_ON(backref->found_inode_ref); -+ if (backref->found_dir_item) -+ add_root_backref(dst_cache, rec->ino, -+ root->root_key.objectid, backref->dir, -+ backref->index, backref->name, -+ backref->namelen, BTRFS_DIR_ITEM_KEY, -+ backref->errors); -+ if (backref->found_dir_index) -+ add_root_backref(dst_cache, rec->ino, -+ root->root_key.objectid, backref->dir, -+ backref->index, backref->name, -+ backref->namelen, BTRFS_DIR_INDEX_KEY, -+ backref->errors); -+ } -+ free_inode_rec(rec); -+ } -+ return 0; -+} -+ -+static int check_root_refs(struct btrfs_root *root, -+ struct cache_tree *root_cache) -+{ -+ struct root_record *rec; -+ struct root_record *ref_root; -+ struct root_backref *backref; -+ struct cache_extent *cache; -+ int loop = 1; -+ int ret; -+ int error; -+ int errors = 0; -+ -+ rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID); -+ rec->found_ref = 1; -+ -+ /* fixme: this can not detect circular references */ -+ while (loop) { -+ loop = 0; -+ cache = find_first_cache_extent(root_cache, 0); -+ while (1) { -+ if (!cache) -+ break; -+ rec = container_of(cache, struct root_record, cache); -+ cache = next_cache_extent(cache); -+ -+ if (rec->found_ref == 0) -+ continue; -+ -+ list_for_each_entry(backref, &rec->backrefs, list) { -+ if (!backref->reachable) -+ continue; -+ -+ ref_root = get_root_rec(root_cache, -+ backref->ref_root); -+ if (ref_root->found_ref > 0) -+ continue; -+ -+ backref->reachable = 0; -+ rec->found_ref--; -+ if (rec->found_ref == 0) -+ loop = 1; -+ } -+ } -+ } -+ -+ cache = find_first_cache_extent(root_cache, 0); -+ while (1) { -+ if (!cache) -+ break; -+ rec = container_of(cache, struct root_record, cache); -+ cache = next_cache_extent(cache); -+ -+ if (rec->found_ref == 0 && -+ rec->objectid >= BTRFS_FIRST_FREE_OBJECTID && -+ rec->objectid <= BTRFS_LAST_FREE_OBJECTID) { -+ ret = check_orphan_item(root->fs_info->tree_root, -+ rec->objectid); -+ if (ret == 0) -+ continue; -+ errors++; -+ fprintf(stderr, "fs tree %llu not referenced\n", -+ (unsigned long long)rec->objectid); -+ } -+ -+ error = 0; -+ if (rec->found_ref > 0 && !rec->found_root_item) -+ error = 1; -+ list_for_each_entry(backref, &rec->backrefs, list) { -+ if (!backref->found_dir_item) -+ backref->errors |= REF_ERR_NO_DIR_ITEM; -+ if (!backref->found_dir_index) -+ backref->errors |= REF_ERR_NO_DIR_INDEX; -+ if (!backref->found_back_ref) -+ backref->errors |= REF_ERR_NO_ROOT_BACKREF; -+ if (!backref->found_forward_ref) -+ backref->errors |= REF_ERR_NO_ROOT_REF; -+ if (backref->reachable && backref->errors) -+ error = 1; -+ } -+ if (!error) -+ continue; -+ -+ errors++; -+ fprintf(stderr, "fs tree %llu refs %u %s\n", -+ (unsigned long long)rec->objectid, rec->found_ref, -+ rec->found_root_item ? "" : "not found"); -+ -+ list_for_each_entry(backref, &rec->backrefs, list) { -+ if (!backref->reachable) -+ continue; -+ if (!backref->errors && rec->found_root_item) -+ continue; -+ fprintf(stderr, "\tunresolved ref root %llu dir %llu" -+ " index %llu namelen %u name %s error %x\n", -+ (unsigned long long)backref->ref_root, -+ (unsigned long long)backref->dir, -+ (unsigned long long)backref->index, -+ backref->namelen, backref->name, -+ backref->errors); -+ } -+ } -+ return errors > 0 ? 1 : 0; -+} -+ -+static int process_root_ref(struct extent_buffer *eb, int slot, -+ struct btrfs_key *key, -+ struct cache_tree *root_cache) -+{ -+ u64 dirid; -+ u64 index; -+ u32 len; -+ u32 name_len; -+ struct btrfs_root_ref *ref; -+ char namebuf[BTRFS_NAME_LEN]; -+ int error; -+ -+ ref = btrfs_item_ptr(eb, slot, struct btrfs_root_ref); -+ -+ dirid = btrfs_root_ref_dirid(eb, ref); -+ index = btrfs_root_ref_sequence(eb, ref); -+ name_len = btrfs_root_ref_name_len(eb, ref); -+ -+ if (name_len <= BTRFS_NAME_LEN) { -+ len = name_len; -+ error = 0; -+ } else { -+ len = BTRFS_NAME_LEN; -+ error = REF_ERR_NAME_TOO_LONG; -+ } -+ read_extent_buffer(eb, namebuf, (unsigned long)(ref + 1), len); -+ -+ if (key->type == BTRFS_ROOT_REF_KEY) { -+ add_root_backref(root_cache, key->offset, key->objectid, dirid, -+ index, namebuf, len, key->type, error); -+ } else { -+ add_root_backref(root_cache, key->objectid, key->offset, dirid, -+ index, namebuf, len, key->type, error); -+ } -+ return 0; -+} -+ - static int check_fs_root(struct btrfs_root *root, -+ struct cache_tree *root_cache, - struct walk_control *wc) - { - int ret = 0; -@@ -1219,10 +1589,18 @@ static int check_fs_root(struct btrfs_root *root, - int level; - struct btrfs_path path; - struct shared_node root_node; -+ struct root_record *rec; - struct btrfs_root_item *root_item = &root->root_item; - -+ if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { -+ rec = get_root_rec(root_cache, root->root_key.objectid); -+ if (btrfs_root_refs(root_item) > 0) -+ rec->found_root_item = 1; -+ } -+ - btrfs_init_path(&path); - memset(&root_node, 0, sizeof(root_node)); -+ cache_tree_init(&root_node.root_cache); - cache_tree_init(&root_node.inode_cache); - - level = btrfs_header_level(root->node); -@@ -1266,6 +1644,8 @@ static int check_fs_root(struct btrfs_root *root, - } - btrfs_release_path(root, &path); - -+ merge_root_recs(root, &root_node.root_cache, root_cache); -+ - if (root_node.current) { - root_node.current->checked = 1; - maybe_free_inode_rec(&root_node.inode_cache, -@@ -1280,13 +1660,15 @@ static int fs_root_objectid(u64 objectid) - { - if (objectid == BTRFS_FS_TREE_OBJECTID || - objectid == BTRFS_TREE_RELOC_OBJECTID || -+ objectid == BTRFS_DATA_RELOC_TREE_OBJECTID || - (objectid >= BTRFS_FIRST_FREE_OBJECTID && -- objectid < BTRFS_LAST_FREE_OBJECTID)) -+ objectid <= BTRFS_LAST_FREE_OBJECTID)) - return 1; - return 0; - } - --static int check_fs_roots(struct btrfs_root *root) -+static int check_fs_roots(struct btrfs_root *root, -+ struct cache_tree *root_cache) - { - struct btrfs_path path; - struct btrfs_key key; -@@ -1319,10 +1701,14 @@ static int check_fs_roots(struct btrfs_root *root) - fs_root_objectid(key.objectid)) { - tmp_root = btrfs_read_fs_root_no_cache(root->fs_info, - &key); -- ret = check_fs_root(tmp_root, &wc); -+ ret = check_fs_root(tmp_root, root_cache, &wc); - if (ret) - err = 1; - btrfs_free_fs_root(root->fs_info, tmp_root); -+ } else if (key.type == BTRFS_ROOT_REF_KEY || -+ key.type == BTRFS_ROOT_BACKREF_KEY) { -+ process_root_ref(leaf, path.slots[0], &key, -+ root_cache); - } - path.slots[0]++; - } -@@ -1334,86 +1720,6 @@ static int check_fs_roots(struct btrfs_root *root) - return err; - } - --static int check_node(struct btrfs_root *root, -- struct btrfs_disk_key *parent_key, -- struct extent_buffer *buf) --{ -- int i; -- struct btrfs_key cpukey; -- struct btrfs_disk_key key; -- u32 nritems = btrfs_header_nritems(buf); -- -- if (nritems == 0 || nritems > BTRFS_NODEPTRS_PER_BLOCK(root)) -- return 1; -- if (parent_key->type) { -- btrfs_node_key(buf, &key, 0); -- if (memcmp(parent_key, &key, sizeof(key))) -- return 1; -- } -- for (i = 0; nritems > 1 && i < nritems - 2; i++) { -- btrfs_node_key(buf, &key, i); -- btrfs_node_key_to_cpu(buf, &cpukey, i + 1); -- if (btrfs_comp_keys(&key, &cpukey) >= 0) -- return 1; -- } -- return 0; --} -- --static int check_leaf(struct btrfs_root *root, -- struct btrfs_disk_key *parent_key, -- struct extent_buffer *buf) --{ -- int i; -- struct btrfs_key cpukey; -- struct btrfs_disk_key key; -- u32 nritems = btrfs_header_nritems(buf); -- -- if (btrfs_header_level(buf) != 0) { -- fprintf(stderr, "leaf is not a leaf %llu\n", -- (unsigned long long)btrfs_header_bytenr(buf)); -- return 1; -- } -- if (btrfs_leaf_free_space(root, buf) < 0) { -- fprintf(stderr, "leaf free space incorrect %llu %d\n", -- (unsigned long long)btrfs_header_bytenr(buf), -- btrfs_leaf_free_space(root, buf)); -- return 1; -- } -- -- if (nritems == 0) -- return 0; -- -- btrfs_item_key(buf, &key, 0); -- if (parent_key->type && memcmp(parent_key, &key, sizeof(key))) { -- fprintf(stderr, "leaf parent key incorrect %llu\n", -- (unsigned long long)btrfs_header_bytenr(buf)); -- return 1; -- } -- for (i = 0; nritems > 1 && i < nritems - 2; i++) { -- btrfs_item_key(buf, &key, i); -- btrfs_item_key_to_cpu(buf, &cpukey, i + 1); -- if (btrfs_comp_keys(&key, &cpukey) >= 0) { -- fprintf(stderr, "bad key ordering %d %d\n", i, i+1); -- return 1; -- } -- if (btrfs_item_offset_nr(buf, i) != -- btrfs_item_end_nr(buf, i + 1)) { -- fprintf(stderr, "incorrect offsets %u %u\n", -- btrfs_item_offset_nr(buf, i), -- btrfs_item_end_nr(buf, i + 1)); -- return 1; -- } -- if (i == 0 && btrfs_item_end_nr(buf, i) != -- BTRFS_LEAF_DATA_SIZE(root)) { -- fprintf(stderr, "bad item end %u wanted %u\n", -- btrfs_item_end_nr(buf, i), -- (unsigned)BTRFS_LEAF_DATA_SIZE(root)); -- return 1; -- } -- } -- return 0; --} -- - static int all_backpointers_checked(struct extent_record *rec, int print_errs) - { - struct list_head *cur = rec->backrefs.next; -@@ -1458,12 +1764,12 @@ static int all_backpointers_checked(struct extent_record *rec, int print_errs) - if (!print_errs) - goto out; - tback = (struct tree_backref *)back; -- fprintf(stderr, "Backref %llu %s %llu not referenced\n", -+ fprintf(stderr, "Backref %llu %s %llu not referenced back %p\n", - (unsigned long long)rec->start, - back->full_backref ? "parent" : "root", - back->full_backref ? - (unsigned long long)tback->parent : -- (unsigned long long)tback->root); -+ (unsigned long long)tback->root, back); - } - if (back->is_data) { - dback = (struct data_backref *)back; -@@ -1473,7 +1779,7 @@ static int all_backpointers_checked(struct extent_record *rec, int print_errs) - goto out; - fprintf(stderr, "Incorrect local backref count" - " on %llu %s %llu owner %llu" -- " offset %llu found %u wanted %u\n", -+ " offset %llu found %u wanted %u back %p\n", - (unsigned long long)rec->start, - back->full_backref ? - "parent" : "root", -@@ -1482,7 +1788,7 @@ static int all_backpointers_checked(struct extent_record *rec, int print_errs) - (unsigned long long)dback->root, - (unsigned long long)dback->owner, - (unsigned long long)dback->offset, -- dback->found_ref, dback->num_refs); -+ dback->found_ref, dback->num_refs, back); - } - } - if (!back->is_data) { -@@ -1541,7 +1847,6 @@ static int check_owner_ref(struct btrfs_root *root, - struct btrfs_root *ref_root; - struct btrfs_key key; - struct btrfs_path path; -- int ret; - int level; - int found = 0; - -@@ -1571,10 +1876,10 @@ static int check_owner_ref(struct btrfs_root *root, - btrfs_item_key_to_cpu(buf, &key, 0); - else - btrfs_node_key_to_cpu(buf, &key, 0); -- -+ - btrfs_init_path(&path); - path.lowest_level = level + 1; -- ret = btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0); -+ btrfs_search_slot(NULL, ref_root, &key, &path, 0, 0); - - if (buf->start == btrfs_node_blockptr(path.nodes[level + 1], - path.slots[level + 1])) -@@ -1584,23 +1889,81 @@ static int check_owner_ref(struct btrfs_root *root, - return found ? 0 : 1; - } - -+static int is_extent_tree_record(struct extent_record *rec) -+{ -+ struct list_head *cur = rec->backrefs.next; -+ struct extent_backref *node; -+ struct tree_backref *back; -+ int is_extent = 0; -+ -+ while(cur != &rec->backrefs) { -+ node = list_entry(cur, struct extent_backref, list); -+ cur = cur->next; -+ if (node->is_data) -+ return 0; -+ back = (struct tree_backref *)node; -+ if (node->full_backref) -+ return 0; -+ if (back->root == BTRFS_EXTENT_TREE_OBJECTID) -+ is_extent = 1; -+ } -+ return is_extent; -+} -+ -+ -+static int record_bad_block_io(struct btrfs_fs_info *info, -+ struct cache_tree *extent_cache, -+ u64 start, u64 len) -+{ -+ struct extent_record *rec; -+ struct cache_extent *cache; -+ struct btrfs_key key; -+ -+ cache = find_cache_extent(extent_cache, start, len); -+ if (!cache) -+ return 0; -+ -+ rec = container_of(cache, struct extent_record, cache); -+ if (!is_extent_tree_record(rec)) -+ return 0; -+ -+ btrfs_disk_key_to_cpu(&key, &rec->parent_key); -+ return btrfs_add_corrupt_extent_record(info, &key, start, len, 0); -+} -+ - static int check_block(struct btrfs_root *root, - struct cache_tree *extent_cache, - struct extent_buffer *buf, u64 flags) - { - struct extent_record *rec; - struct cache_extent *cache; -+ struct btrfs_key key; - int ret = 1; -+ int level; - - cache = find_cache_extent(extent_cache, buf->start, buf->len); - if (!cache) - return 1; - rec = container_of(cache, struct extent_record, cache); -- if (btrfs_is_leaf(buf)) { -- ret = check_leaf(root, &rec->parent_key, buf); -- } else { -- ret = check_node(root, &rec->parent_key, buf); -+ rec->generation = btrfs_header_generation(buf); -+ -+ level = btrfs_header_level(buf); -+ if (btrfs_header_nritems(buf) > 0) { -+ -+ if (level == 0) -+ btrfs_item_key_to_cpu(buf, &key, 0); -+ else -+ btrfs_node_key_to_cpu(buf, &key, 0); -+ -+ rec->info_objectid = key.objectid; - } -+ rec->info_level = level; -+ -+ if (btrfs_is_leaf(buf)) -+ ret = btrfs_check_leaf(root, &rec->parent_key, buf); -+ else -+ ret = btrfs_check_node(root, &rec->parent_key, buf); -+ - if (ret) { - fprintf(stderr, "bad block %llu\n", - (unsigned long long)buf->start); -@@ -1660,6 +2023,7 @@ static struct tree_backref *alloc_tree_backref(struct extent_record *rec, - ref->node.full_backref = 0; - } - list_add_tail(&ref->node.list, &rec->backrefs); -+ - return ref; - } - -@@ -1677,7 +2041,7 @@ static struct data_backref *find_data_backref(struct extent_record *rec, - if (!node->is_data) - continue; - back = (struct data_backref *)node; -- if (parent > 0) { -+ if (parent > 0) { - if (!node->full_backref) - continue; - if (parent == back->parent) -@@ -1695,11 +2059,13 @@ static struct data_backref *find_data_backref(struct extent_record *rec, - - static struct data_backref *alloc_data_backref(struct extent_record *rec, - u64 parent, u64 root, -- u64 owner, u64 offset) -+ u64 owner, u64 offset, -+ u64 max_size) - { - struct data_backref *ref = malloc(sizeof(*ref)); - memset(&ref->node, 0, sizeof(ref->node)); - ref->node.is_data = 1; -+ - if (parent > 0) { - ref->parent = parent; - ref->owner = 0; -@@ -1714,13 +2080,16 @@ static struct data_backref *alloc_data_backref(struct extent_record *rec, - ref->found_ref = 0; - ref->num_refs = 0; - list_add_tail(&ref->node.list, &rec->backrefs); -+ if (max_size > rec->max_size) -+ rec->max_size = max_size; - return ref; - } - - static int add_extent_rec(struct cache_tree *extent_cache, - struct btrfs_key *parent_key, - u64 start, u64 nr, u64 extent_item_refs, -- int is_root, int inc_ref, int set_checked) -+ int is_root, int inc_ref, int set_checked, -+ u64 max_size) - { - struct extent_record *rec; - struct cache_extent *cache; -@@ -1732,7 +2101,7 @@ static int add_extent_rec(struct cache_tree *extent_cache, - if (inc_ref) - rec->refs++; - if (rec->nr == 1) -- rec->nr = nr; -+ rec->nr = max(nr, max_size); - - if (start != rec->start) { - fprintf(stderr, "warning, start mismatch %llu %llu\n", -@@ -1761,12 +2130,16 @@ static int add_extent_rec(struct cache_tree *extent_cache, - if (parent_key) - btrfs_cpu_key_to_disk(&rec->parent_key, parent_key); - -+ if (rec->max_size < max_size) -+ rec->max_size = max_size; -+ - maybe_free_extent_rec(extent_cache, rec); - return ret; - } - rec = malloc(sizeof(*rec)); - rec->start = start; -- rec->nr = nr; -+ rec->max_size = max_size; -+ rec->nr = max(nr, max_size); - rec->content_checked = 0; - rec->owner_ref_checked = 0; - INIT_LIST_HEAD(&rec->backrefs); -@@ -1812,7 +2185,7 @@ static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr, - - cache = find_cache_extent(extent_cache, bytenr, 1); - if (!cache) { -- add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0); -+ add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0, 0); - cache = find_cache_extent(extent_cache, bytenr, 1); - if (!cache) - abort(); -@@ -1851,7 +2224,7 @@ static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr, - - static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr, - u64 parent, u64 root, u64 owner, u64 offset, -- u32 num_refs, int found_ref) -+ u32 num_refs, int found_ref, u64 max_size) - { - struct extent_record *rec; - struct data_backref *back; -@@ -1859,7 +2232,8 @@ static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr, - - cache = find_cache_extent(extent_cache, bytenr, 1); - if (!cache) { -- add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0); -+ add_extent_rec(extent_cache, NULL, bytenr, 1, 0, 0, 0, 0, -+ max_size); - cache = find_cache_extent(extent_cache, bytenr, 1); - if (!cache) - abort(); -@@ -1869,9 +2243,13 @@ static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr, - if (rec->start != bytenr) { - abort(); - } -+ if (rec->max_size < max_size) -+ rec->max_size = max_size; -+ - back = find_data_backref(rec, parent, root, owner, offset); - if (!back) -- back = alloc_data_backref(rec, parent, root, owner, offset); -+ back = alloc_data_backref(rec, parent, root, owner, offset, -+ max_size); - - if (found_ref) { - BUG_ON(num_refs != 1); -@@ -1895,7 +2273,6 @@ static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr, - return 0; - } - -- - static int add_pending(struct cache_tree *pending, - struct cache_tree *seen, u64 bytenr, u32 size) - { -@@ -1985,11 +2362,10 @@ static int process_extent_ref_v0(struct cache_tree *extent_cache, - btrfs_item_key_to_cpu(leaf, &key, slot); - ref0 = btrfs_item_ptr(leaf, slot, struct btrfs_extent_ref_v0); - if (btrfs_ref_objectid_v0(leaf, ref0) < BTRFS_FIRST_FREE_OBJECTID) { -- add_tree_backref(extent_cache, key.objectid, key.offset, -- 0, 0); -+ add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0); - } else { - add_data_backref(extent_cache, key.objectid, key.offset, 0, -- 0, 0, btrfs_ref_count_v0(leaf, ref0), 0); -+ 0, 0, btrfs_ref_count_v0(leaf, ref0), 0, 0); - } - return 0; - } -@@ -2022,14 +2398,14 @@ static int process_extent_item(struct cache_tree *extent_cache, - BUG(); - #endif - return add_extent_rec(extent_cache, NULL, key.objectid, -- key.offset, refs, 0, 0, 0); -+ key.offset, refs, 0, 0, 0, key.offset); - } - - ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item); - refs = btrfs_extent_refs(eb, ei); - - add_extent_rec(extent_cache, NULL, key.objectid, key.offset, -- refs, 0, 0, 0); -+ refs, 0, 0, 0, key.offset); - - ptr = (unsigned long)(ei + 1); - if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK) -@@ -2057,21 +2433,24 @@ static int process_extent_item(struct cache_tree *extent_cache, - dref), - btrfs_extent_data_ref_offset(eb, dref), - btrfs_extent_data_ref_count(eb, dref), -- 0); -+ 0, key.offset); - break; - case BTRFS_SHARED_DATA_REF_KEY: - sref = (struct btrfs_shared_data_ref *)(iref + 1); - add_data_backref(extent_cache, key.objectid, offset, - 0, 0, 0, - btrfs_shared_data_ref_count(eb, sref), -- 0); -+ 0, key.offset); - break; - default: -- BUG(); -+ fprintf(stderr, "corrupt extent record: key %Lu %u %Lu\n", -+ key.objectid, key.type, key.offset); -+ goto out; - } - ptr += btrfs_extent_inline_ref_size(type); - } - WARN_ON(ptr > end); -+out: - return 0; - } - -@@ -2135,9 +2514,18 @@ static int run_next_block(struct btrfs_root *root, - - /* fixme, get the real parent transid */ - buf = read_tree_block(root, bytenr, size, 0); -+ if (!extent_buffer_uptodate(buf)) { -+ record_bad_block_io(root->fs_info, -+ extent_cache, bytenr, size); -+ free_extent_buffer(buf); -+ goto out; -+ } -+ - nritems = btrfs_header_nritems(buf); - - ret = btrfs_lookup_extent_info(NULL, root, bytenr, size, NULL, &flags); -+ if (ret < 0) -+ flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; - - if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { - parent = bytenr; -@@ -2148,6 +2536,8 @@ static int run_next_block(struct btrfs_root *root, - } - - ret = check_block(root, extent_cache, buf, flags); -+ if (ret) -+ goto out; - - if (btrfs_is_leaf(buf)) { - btree_space_waste += btrfs_leaf_free_space(root, buf); -@@ -2164,16 +2554,6 @@ static int run_next_block(struct btrfs_root *root, - continue; - } - if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { -- struct btrfs_block_group_item *bi; -- bi = btrfs_item_ptr(buf, i, -- struct btrfs_block_group_item); --#if 0 -- fprintf(stderr,"block group %Lu %Lu used %Lu ", -- btrfs_disk_key_objectid(disk_key), -- btrfs_disk_key_offset(disk_key), -- btrfs_block_group_used(bi)); -- fprintf(stderr, "flags %x\n", bi->flags); --#endif - continue; - } - if (key.type == BTRFS_EXTENT_REF_V0_KEY) { -@@ -2206,7 +2586,7 @@ static int run_next_block(struct btrfs_root *root, - ref), - btrfs_extent_data_ref_offset(buf, ref), - btrfs_extent_data_ref_count(buf, ref), -- 0); -+ 0, root->sectorsize); - continue; - } - if (key.type == BTRFS_SHARED_DATA_REF_KEY) { -@@ -2216,7 +2596,7 @@ static int run_next_block(struct btrfs_root *root, - add_data_backref(extent_cache, - key.objectid, key.offset, 0, 0, 0, - btrfs_shared_data_ref_count(buf, ref), -- 0); -+ 0, root->sectorsize); - continue; - } - if (key.type != BTRFS_EXTENT_DATA_KEY) -@@ -2239,26 +2619,33 @@ static int run_next_block(struct btrfs_root *root, - ret = add_extent_rec(extent_cache, NULL, - btrfs_file_extent_disk_bytenr(buf, fi), - btrfs_file_extent_disk_num_bytes(buf, fi), -- 0, 0, 1, 1); -+ 0, 0, 1, 1, -+ btrfs_file_extent_disk_num_bytes(buf, fi)); - add_data_backref(extent_cache, - btrfs_file_extent_disk_bytenr(buf, fi), - parent, owner, key.objectid, key.offset - -- btrfs_file_extent_offset(buf, fi), 1, 1); -+ btrfs_file_extent_offset(buf, fi), 1, 1, -+ btrfs_file_extent_disk_num_bytes(buf, fi)); - BUG_ON(ret); - } - } else { - int level; -+ struct btrfs_key first_key; -+ -+ first_key.objectid = 0; -+ -+ if (nritems > 0) -+ btrfs_item_key_to_cpu(buf, &first_key, 0); - level = btrfs_header_level(buf); - for (i = 0; i < nritems; i++) { - u64 ptr = btrfs_node_blockptr(buf, i); - u32 size = btrfs_level_size(root, level - 1); - btrfs_node_key_to_cpu(buf, &key, i); - ret = add_extent_rec(extent_cache, &key, -- ptr, size, 0, 0, 1, 0); -+ ptr, size, 0, 0, 1, 0, size); - BUG_ON(ret); - -- add_tree_backref(extent_cache, ptr, parent, -- owner, 1); -+ add_tree_backref(extent_cache, ptr, parent, owner, 1); - - if (level > 1) { - add_pending(nodes, seen, ptr, size); -@@ -2277,6 +2664,7 @@ static int run_next_block(struct btrfs_root *root, - btrfs_header_backref_rev(buf) == BTRFS_MIXED_BACKREF_REV && - !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) - found_old_backref = 1; -+out: - free_extent_buffer(buf); - return 0; - } -@@ -2296,25 +2684,553 @@ static int add_root_to_pending(struct extent_buffer *buf, - else - add_pending(pending, seen, buf->start, buf->len); - add_extent_rec(extent_cache, NULL, buf->start, buf->len, -- 0, 1, 1, 0); -+ 0, 1, 1, 0, buf->len); - - if (root_key->objectid == BTRFS_TREE_RELOC_OBJECTID || - btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) -- add_tree_backref(extent_cache, buf->start, buf->start, 0, 1); -+ add_tree_backref(extent_cache, buf->start, buf->start, -+ 0, 1); - else - add_tree_backref(extent_cache, buf->start, 0, - root_key->objectid, 1); - return 0; - } - --static int check_extent_refs(struct btrfs_root *root, -- struct cache_tree *extent_cache) -+/* as we fix the tree, we might be deleting blocks that -+ * we're tracking for repair. This hook makes sure we -+ * remove any backrefs for blocks as we are fixing them. -+ */ -+static int free_extent_hook(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, -+ u64 bytenr, u64 num_bytes, u64 parent, -+ u64 root_objectid, u64 owner, u64 offset, -+ int refs_to_drop) -+{ -+ struct extent_record *rec; -+ struct cache_extent *cache; -+ int is_data; -+ struct cache_tree *extent_cache = root->fs_info->fsck_extent_cache; -+ -+ is_data = owner >= BTRFS_FIRST_FREE_OBJECTID; -+ cache = find_cache_extent(extent_cache, bytenr, num_bytes); -+ if (!cache) -+ return 0; -+ -+ rec = container_of(cache, struct extent_record, cache); -+ if (is_data) { -+ struct data_backref *back; -+ back = find_data_backref(rec, parent, root_objectid, owner, -+ offset); -+ if (!back) -+ goto out; -+ if (back->node.found_ref) { -+ back->found_ref -= refs_to_drop; -+ if (rec->refs) -+ rec->refs -= refs_to_drop; -+ } -+ if (back->node.found_extent_tree) { -+ back->num_refs -= refs_to_drop; -+ if (rec->extent_item_refs) -+ rec->extent_item_refs -= refs_to_drop; -+ } -+ if (back->found_ref == 0) -+ back->node.found_ref = 0; -+ if (back->num_refs == 0) -+ back->node.found_extent_tree = 0; -+ -+ if (!back->node.found_extent_tree && back->node.found_ref) { -+ list_del(&back->node.list); -+ free(back); -+ } -+ } else { -+ struct tree_backref *back; -+ back = find_tree_backref(rec, parent, root_objectid); -+ if (!back) -+ goto out; -+ if (back->node.found_ref) { -+ if (rec->refs) -+ rec->refs--; -+ back->node.found_ref = 0; -+ } -+ if (back->node.found_extent_tree) { -+ if (rec->extent_item_refs) -+ rec->extent_item_refs--; -+ back->node.found_extent_tree = 0; -+ } -+ if (!back->node.found_extent_tree && back->node.found_ref) { -+ list_del(&back->node.list); -+ free(back); -+ } -+ } -+ maybe_free_extent_rec(extent_cache, rec); -+out: -+ return 0; -+} -+ -+static int delete_extent_records(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, -+ struct btrfs_path *path, -+ u64 bytenr, u64 new_len) -+{ -+ struct btrfs_key key; -+ struct btrfs_key found_key; -+ struct extent_buffer *leaf; -+ int ret; -+ int slot; -+ -+ -+ key.objectid = bytenr; -+ key.type = (u8)-1; -+ key.offset = (u64)-1; -+ -+ while(1) { -+ ret = btrfs_search_slot(trans, root->fs_info->extent_root, -+ &key, path, 0, 1); -+ if (ret < 0) -+ break; -+ -+ if (ret > 0) { -+ ret = 0; -+ if (path->slots[0] == 0) -+ break; -+ path->slots[0]--; -+ } -+ ret = 0; -+ -+ leaf = path->nodes[0]; -+ slot = path->slots[0]; -+ -+ btrfs_item_key_to_cpu(leaf, &found_key, slot); -+ if (found_key.objectid != bytenr) -+ break; -+ -+ if (found_key.type != BTRFS_EXTENT_ITEM_KEY && -+ found_key.type != BTRFS_TREE_BLOCK_REF_KEY && -+ found_key.type != BTRFS_EXTENT_DATA_REF_KEY && -+ found_key.type != BTRFS_EXTENT_REF_V0_KEY && -+ found_key.type != BTRFS_SHARED_BLOCK_REF_KEY && -+ found_key.type != BTRFS_SHARED_DATA_REF_KEY) { -+ btrfs_release_path(NULL, path); -+ if (found_key.type == 0) { -+ if (found_key.offset == 0) -+ break; -+ key.offset = found_key.offset - 1; -+ key.type = found_key.type; -+ } -+ key.type = found_key.type - 1; -+ key.offset = (u64)-1; -+ continue; -+ } -+ -+ fprintf(stderr, "repair deleting extent record: key %Lu %u %Lu\n", -+ found_key.objectid, found_key.type, found_key.offset); -+ -+ ret = btrfs_del_item(trans, root->fs_info->extent_root, path); -+ if (ret) -+ break; -+ btrfs_release_path(NULL, path); -+ -+ if (found_key.type == BTRFS_EXTENT_ITEM_KEY) { -+ ret = btrfs_update_block_group(trans, root, bytenr, -+ found_key.offset, 0, 0); -+ if (ret) -+ break; -+ } -+ } -+ -+ btrfs_release_path(NULL, path); -+ return ret; -+} -+ -+/* -+ * for a single backref, this will allocate a new extent -+ * and add the backref to it. -+ */ -+static int record_extent(struct btrfs_trans_handle *trans, -+ struct btrfs_fs_info *info, -+ struct btrfs_path *path, -+ struct extent_record *rec, -+ struct extent_backref *back, -+ int allocated, u64 flags) -+{ -+ int ret; -+ struct btrfs_root *extent_root = info->extent_root; -+ struct extent_buffer *leaf; -+ struct btrfs_key ins_key; -+ struct btrfs_extent_item *ei; -+ struct tree_backref *tback; -+ struct data_backref *dback; -+ struct btrfs_tree_block_info *bi; -+ -+ if (!back->is_data) -+ rec->max_size = max_t(u64, rec->max_size, -+ info->extent_root->leafsize); -+ -+ if (!allocated) { -+ u32 item_size = sizeof(*ei); -+ -+ if (!back->is_data) -+ item_size += sizeof(*bi); -+ -+ ins_key.objectid = rec->start; -+ ins_key.offset = rec->max_size; -+ ins_key.type = BTRFS_EXTENT_ITEM_KEY; -+ -+ ret = btrfs_insert_empty_item(trans, extent_root, path, -+ &ins_key, item_size); -+ if (ret) -+ goto fail; -+ -+ leaf = path->nodes[0]; -+ ei = btrfs_item_ptr(leaf, path->slots[0], -+ struct btrfs_extent_item); -+ -+ btrfs_set_extent_refs(leaf, ei, 0); -+ btrfs_set_extent_generation(leaf, ei, rec->generation); -+ -+ if (back->is_data) { -+ btrfs_set_extent_flags(leaf, ei, -+ BTRFS_EXTENT_FLAG_DATA); -+ } else { -+ struct btrfs_disk_key copy_key;; -+ -+ tback = (struct tree_backref *)back; -+ bi = (struct btrfs_tree_block_info *)(ei + 1); -+ memset_extent_buffer(leaf, 0, (unsigned long)bi, -+ sizeof(*bi)); -+ memset(©_key, 0, sizeof(copy_key)); -+ -+ copy_key.objectid = le64_to_cpu(rec->info_objectid); -+ btrfs_set_tree_block_level(leaf, bi, rec->info_level); -+ btrfs_set_tree_block_key(leaf, bi, ©_key); -+ -+ btrfs_set_extent_flags(leaf, ei, -+ BTRFS_EXTENT_FLAG_TREE_BLOCK | flags); -+ } -+ -+ btrfs_mark_buffer_dirty(leaf); -+ ret = btrfs_update_block_group(trans, extent_root, rec->start, -+ rec->max_size, 1, 0); -+ if (ret) -+ goto fail; -+ btrfs_release_path(NULL, path); -+ } -+ -+ if (back->is_data) { -+ u64 parent; -+ int i; -+ -+ dback = (struct data_backref *)back; -+ if (back->full_backref) -+ parent = dback->parent; -+ else -+ parent = 0; -+ -+ for (i = 0; i < dback->found_ref; i++) { -+ /* if parent != 0, we're doing a full backref -+ * passing BTRFS_FIRST_FREE_OBJECTID as the owner -+ * just makes the backref allocator create a data -+ * backref -+ */ -+ ret = btrfs_inc_extent_ref(trans, info->extent_root, -+ rec->start, rec->max_size, -+ parent, -+ dback->root, -+ parent ? -+ BTRFS_FIRST_FREE_OBJECTID : -+ dback->owner, -+ dback->offset); -+ if (ret) -+ break; -+ } -+ fprintf(stderr, "adding new data backref" -+ " on %llu %s %llu owner %llu" -+ " offset %llu found %d\n", -+ (unsigned long long)rec->start, -+ back->full_backref ? -+ "parent" : "root", -+ back->full_backref ? -+ (unsigned long long)parent : -+ (unsigned long long)dback->root, -+ (unsigned long long)dback->owner, -+ (unsigned long long)dback->offset, -+ dback->found_ref); -+ } else { -+ u64 parent; -+ -+ tback = (struct tree_backref *)back; -+ if (back->full_backref) -+ parent = tback->parent; -+ else -+ parent = 0; -+ -+ ret = btrfs_inc_extent_ref(trans, info->extent_root, -+ rec->start, rec->max_size, -+ parent, tback->root, 0, 0); -+ fprintf(stderr, "adding new tree backref on " -+ "start %llu len %llu parent %llu root %llu\n", -+ rec->start, rec->max_size, tback->parent, tback->root); -+ } -+ if (ret) -+ goto fail; -+fail: -+ btrfs_release_path(NULL, path); -+ return ret; -+} -+ -+/* -+ * when an incorrect extent item is found, this will delete -+ * all of the existing entries for it and recreate them -+ * based on what the tree scan found. -+ */ -+static int fixup_extent_refs(struct btrfs_trans_handle *trans, -+ struct btrfs_fs_info *info, -+ struct extent_record *rec) -+{ -+ int ret; -+ struct btrfs_path *path; -+ struct list_head *cur = rec->backrefs.next; -+ struct cache_extent *cache; -+ struct extent_backref *back; -+ int allocated = 0; -+ u64 flags = 0; -+ -+ /* remember our flags for recreating the extent */ -+ ret = btrfs_lookup_extent_info(NULL, info->extent_root, rec->start, -+ rec->max_size, NULL, &flags); -+ if (ret < 0) -+ flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; -+ -+ path = btrfs_alloc_path(); -+ -+ /* step one, delete all the existing records */ -+ ret = delete_extent_records(trans, info->extent_root, path, -+ rec->start, rec->max_size); -+ -+ if (ret < 0) -+ goto out; -+ -+ /* was this block corrupt? If so, don't add references to it */ -+ cache = find_cache_extent(info->corrupt_blocks, rec->start, rec->max_size); -+ if (cache) { -+ ret = 0; -+ goto out; -+ } -+ -+ /* step two, recreate all the refs we did find */ -+ while(cur != &rec->backrefs) { -+ back = list_entry(cur, struct extent_backref, list); -+ cur = cur->next; -+ -+ /* -+ * if we didn't find any references, don't create a -+ * new extent record -+ */ -+ if (!back->found_ref) -+ continue; -+ -+ ret = record_extent(trans, info, path, rec, back, allocated, flags); -+ allocated = 1; -+ -+ if (ret) -+ goto out; -+ } -+out: -+ btrfs_free_path(path); -+ return ret; -+} -+ -+/* right now we only prune from the extent allocation tree */ -+static int prune_one_block(struct btrfs_trans_handle *trans, -+ struct btrfs_fs_info *info, -+ struct btrfs_corrupt_block *corrupt) -+{ -+ int ret; -+ struct btrfs_path path; -+ struct extent_buffer *eb; -+ u64 found; -+ int slot; -+ int nritems; -+ int level = corrupt->level + 1; -+ -+ btrfs_init_path(&path); -+again: -+ /* we want to stop at the parent to our busted block */ -+ path.lowest_level = level; -+ -+ ret = btrfs_search_slot(trans, info->extent_root, -+ &corrupt->key, &path, -1, 1); -+ -+ if (ret < 0) -+ goto out; -+ -+ eb = path.nodes[level]; -+ if (!eb) { -+ ret = -ENOENT; -+ goto out; -+ } -+ -+ /* -+ * hopefully the search gave us the block we want to prune, -+ * lets try that first -+ */ -+ slot = path.slots[level]; -+ found = btrfs_node_blockptr(eb, slot); -+ if (found == corrupt->cache.start) -+ goto del_ptr; -+ -+ nritems = btrfs_header_nritems(eb); -+ -+ /* the search failed, lets scan this node and hope we find it */ -+ for (slot = 0; slot < nritems; slot++) { -+ found = btrfs_node_blockptr(eb, slot); -+ if (found == corrupt->cache.start) -+ goto del_ptr; -+ } -+ /* -+ * we couldn't find the bad block. TODO, search all the nodes for pointers -+ * to this block -+ */ -+ if (eb == info->extent_root->node) { -+ ret = -ENOENT; -+ goto out; -+ } else { -+ level++; -+ btrfs_release_path(NULL, &path); -+ goto again; -+ } -+ -+del_ptr: -+ printk("deleting pointer to block %Lu\n", corrupt->cache.start); -+ ret = btrfs_del_ptr(trans, info->extent_root, &path, level, slot); -+ -+out: -+ btrfs_release_path(NULL, &path); -+ return ret; -+} -+ -+static int prune_corrupt_blocks(struct btrfs_trans_handle *trans, -+ struct btrfs_fs_info *info) -+{ -+ struct cache_extent *cache; -+ struct btrfs_corrupt_block *corrupt; -+ -+ cache = find_first_cache_extent(info->corrupt_blocks, 0); -+ while (1) { -+ if (!cache) -+ break; -+ corrupt = container_of(cache, struct btrfs_corrupt_block, cache); -+ prune_one_block(trans, info, corrupt); -+ cache = next_cache_extent(cache); -+ } -+ return 0; -+} -+ -+static void free_corrupt_blocks(struct btrfs_fs_info *info) -+{ -+ struct cache_extent *cache; -+ struct btrfs_corrupt_block *corrupt; -+ -+ while (1) { -+ cache = find_first_cache_extent(info->corrupt_blocks, 0); -+ if (!cache) -+ break; -+ corrupt = container_of(cache, struct btrfs_corrupt_block, cache); -+ remove_cache_extent(info->corrupt_blocks, cache); -+ free(corrupt); -+ } -+} -+ -+static int check_block_group(struct btrfs_trans_handle *trans, -+ struct btrfs_fs_info *info, -+ struct map_lookup *map, -+ int *reinit) -+{ -+ struct btrfs_key key; -+ struct btrfs_path path; -+ int ret; -+ -+ key.objectid = map->ce.start; -+ key.offset = map->ce.size; -+ key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; -+ -+ btrfs_init_path(&path); -+ ret = btrfs_search_slot(NULL, info->extent_root, -+ &key, &path, 0, 0); -+ btrfs_release_path(NULL, &path); -+ if (ret <= 0) -+ goto out; -+ -+ ret = btrfs_make_block_group(trans, info->extent_root, 0, map->type, -+ BTRFS_FIRST_CHUNK_TREE_OBJECTID, -+ key.objectid, key.offset); -+ *reinit = 1; -+out: -+ return ret; -+} -+ -+static int check_block_groups(struct btrfs_trans_handle *trans, -+ struct btrfs_fs_info *info, int *reinit) -+{ -+ struct cache_extent *ce; -+ struct map_lookup *map; -+ struct btrfs_mapping_tree *map_tree = &info->mapping_tree; -+ -+ /* this isn't quite working */ -+ return 0; -+ -+ ce = find_first_cache_extent(&map_tree->cache_tree, 0); -+ while (1) { -+ if (!ce) -+ break; -+ map = container_of(ce, struct map_lookup, ce); -+ check_block_group(trans, info, map, reinit); -+ ce = next_cache_extent(ce); -+ } -+ return 0; -+} -+ -+static int check_extent_refs(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, -+ struct cache_tree *extent_cache, int repair) - { - struct extent_record *rec; - struct cache_extent *cache; - int err = 0; -+ int ret = 0; -+ int fixed = 0; -+ int reinit = 0; -+ -+ if (repair) { -+ /* -+ * if we're doing a repair, we have to make sure -+ * we don't allocate from the problem extents. -+ * In the worst case, this will be all the -+ * extents in the FS -+ */ -+ cache = find_first_cache_extent(extent_cache, 0); -+ while(cache) { -+ rec = container_of(cache, struct extent_record, cache); -+ btrfs_pin_extent(root->fs_info, -+ rec->start, rec->max_size); -+ cache = next_cache_extent(cache); -+ } - -+ /* pin down all the corrupted blocks too */ -+ cache = find_first_cache_extent(root->fs_info->corrupt_blocks, 0); -+ while(cache) { -+ rec = container_of(cache, struct extent_record, cache); -+ btrfs_pin_extent(root->fs_info, -+ rec->start, rec->max_size); -+ cache = next_cache_extent(cache); -+ } -+ prune_corrupt_blocks(trans, root->fs_info); -+ check_block_groups(trans, root->fs_info, &reinit); -+ if (reinit) -+ btrfs_read_block_groups(root->fs_info->extent_root); -+ } - while(1) { -+ fixed = 0; - cache = find_first_cache_extent(extent_cache, 0); - if (!cache) - break; -@@ -2326,19 +3242,39 @@ static int check_extent_refs(struct btrfs_root *root, - fprintf(stderr, "extent item %llu, found %llu\n", - (unsigned long long)rec->extent_item_refs, - (unsigned long long)rec->refs); -+ if (!fixed && repair) { -+ ret = fixup_extent_refs(trans, root->fs_info, rec); -+ if (ret) -+ goto repair_abort; -+ fixed = 1; -+ } - err = 1; -+ - } - if (all_backpointers_checked(rec, 1)) { - fprintf(stderr, "backpointer mismatch on [%llu %llu]\n", - (unsigned long long)rec->start, - (unsigned long long)rec->nr); - -+ if (!fixed && repair) { -+ ret = fixup_extent_refs(trans, root->fs_info, rec); -+ if (ret) -+ goto repair_abort; -+ fixed = 1; -+ } -+ - err = 1; - } - if (!rec->owner_ref_checked) { - fprintf(stderr, "owner ref check failed [%llu %llu]\n", - (unsigned long long)rec->start, - (unsigned long long)rec->nr); -+ if (!fixed && repair) { -+ ret = fixup_extent_refs(trans, root->fs_info, rec); -+ if (ret) -+ goto repair_abort; -+ fixed = 1; -+ } - err = 1; - } - -@@ -2346,16 +3282,30 @@ static int check_extent_refs(struct btrfs_root *root, - free_all_extent_backrefs(rec); - free(rec); - } -+repair_abort: -+ if (repair) { -+ if (ret) { -+ fprintf(stderr, "failed to repair damaged filesystem, aborting\n"); -+ exit(1); -+ } else { -+ btrfs_fix_block_accounting(trans, root); -+ } -+ if (err) -+ fprintf(stderr, "repaired damaged extent references\n"); -+ return ret; -+ } - return err; - } - --static int check_extents(struct btrfs_root *root) -+static int check_extents(struct btrfs_trans_handle *trans, -+ struct btrfs_root *root, int repair) - { - struct cache_tree extent_cache; - struct cache_tree seen; - struct cache_tree pending; - struct cache_tree reada; - struct cache_tree nodes; -+ struct cache_tree corrupt_blocks; - struct btrfs_path path; - struct btrfs_key key; - struct btrfs_key found_key; -@@ -2372,6 +3322,13 @@ static int check_extents(struct btrfs_root *root) - cache_tree_init(&pending); - cache_tree_init(&nodes); - cache_tree_init(&reada); -+ cache_tree_init(&corrupt_blocks); -+ -+ if (repair) { -+ root->fs_info->fsck_extent_cache = &extent_cache; -+ root->fs_info->free_extent_hook = free_extent_hook; -+ root->fs_info->corrupt_blocks = &corrupt_blocks; -+ } - - bits_nr = 1024; - bits = malloc(bits_nr * sizeof(struct block_info)); -@@ -2430,7 +3387,15 @@ static int check_extents(struct btrfs_root *root) - if (ret != 0) - break; - } -- ret = check_extent_refs(root, &extent_cache); -+ ret = check_extent_refs(trans, root, &extent_cache, repair); -+ -+ if (repair) { -+ free_corrupt_blocks(root->fs_info); -+ root->fs_info->fsck_extent_cache = NULL; -+ root->fs_info->free_extent_hook = NULL; -+ root->fs_info->corrupt_blocks = NULL; -+ } -+ - return ret; - } - -@@ -2441,29 +3406,120 @@ static void print_usage(void) - exit(1); - } - -+static struct option long_options[] = { -+ { "super", 1, NULL, 's' }, -+ { "repair", 0, NULL, 0 }, -+ { "init-csum-tree", 0, NULL, 0 }, -+ { "init-extent-tree", 0, NULL, 0 }, -+ { 0, 0, 0, 0} -+}; -+ - int main(int ac, char **av) - { -+ struct cache_tree root_cache; - struct btrfs_root *root; -+ struct btrfs_fs_info *info; -+ struct btrfs_trans_handle *trans = NULL; -+ u64 bytenr = 0; - int ret; -+ int num; -+ int repair = 0; -+ int option_index = 0; -+ int init_csum_tree = 0; -+ int rw = 0; -+ -+ while(1) { -+ int c; -+ c = getopt_long(ac, av, "", long_options, -+ &option_index); -+ if (c < 0) -+ break; -+ switch(c) { -+ case 's': -+ num = atol(optarg); -+ bytenr = btrfs_sb_offset(num); -+ printf("using SB copy %d, bytenr %llu\n", num, -+ (unsigned long long)bytenr); -+ break; -+ case '?': -+ print_usage(); -+ } -+ if (option_index == 1) { -+ printf("enabling repair mode\n"); -+ repair = 1; -+ rw = 1; -+ } else if (option_index == 2) { -+ printf("Creating a new CRC tree\n"); -+ init_csum_tree = 1; -+ rw = 1; -+ } -+ -+ } -+ ac = ac - optind; - -- if (ac < 2) -+ if (ac != 1) - print_usage(); - - radix_tree_init(); -- root = open_ctree(av[1], 0, 0); -+ cache_tree_init(&root_cache); - -- if (root == NULL) -+ if((ret = check_mounted(av[optind])) < 0) { -+ fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret)); -+ return ret; -+ } else if(ret) { -+ fprintf(stderr, "%s is currently mounted. Aborting.\n", av[optind]); -+ return -EBUSY; -+ } -+ -+ info = open_ctree_fs_info(av[optind], bytenr, rw, 1); -+ -+ if (info == NULL) - return 1; - -- ret = check_extents(root); -+ if (!extent_buffer_uptodate(info->tree_root->node) || -+ !extent_buffer_uptodate(info->dev_root->node) || -+ !extent_buffer_uptodate(info->extent_root->node) || -+ !extent_buffer_uptodate(info->chunk_root->node)) { -+ fprintf(stderr, "Critical roots corrupted, unable to fsck the FS\n"); -+ return -EIO; -+ } -+ -+ root = info->fs_root; -+ -+ fprintf(stderr, "checking extents\n"); -+ if (rw) -+ trans = btrfs_start_transaction(root, 1); -+ -+ if (init_csum_tree) { -+ fprintf(stderr, "Reinit crc root\n"); -+ ret = btrfs_fsck_reinit_root(trans, info->csum_root); -+ if (ret) { -+ fprintf(stderr, "crc root initialization failed\n"); -+ return -EIO; -+ } -+ goto out; -+ } -+ ret = check_extents(trans, root, repair); -+ if (ret) -+ fprintf(stderr, "Errors found in extent allocation tree\n"); -+ -+ fprintf(stderr, "checking fs roots\n"); -+ ret = check_fs_roots(root, &root_cache); - if (ret) - goto out; -- ret = check_fs_roots(root); - -+ fprintf(stderr, "checking root refs\n"); -+ ret = check_root_refs(root, &root_cache); - out: -+ free_root_recs(&root_cache); -+ if (rw) { -+ ret = btrfs_commit_transaction(trans, root); -+ if (ret) -+ exit(1); -+ } - close_ctree(root); -- if (found_old_backref) { -- /* -+ -+ if (found_old_backref) { /* - * there was a disk format change when mixed - * backref was in testing tree. The old format - * existed about one week. -diff --git a/btrfsctl.c b/btrfsctl.c -index b323818..d45e2a7 100644 ---- a/btrfsctl.c -+++ b/btrfsctl.c -@@ -29,6 +29,7 @@ - #include - #include - #include -+#include - #include "kerncompat.h" - #include "ctree.h" - #include "transaction.h" -@@ -46,7 +47,7 @@ static inline int ioctl(int fd, int define, void *arg) { return 0; } - static void print_usage(void) - { - printf("usage: btrfsctl [ -d file|dir] [ -s snap_name subvol|tree ]\n"); -- printf(" [-r size] [-A device] [-a] [-c]\n"); -+ printf(" [-r size] [-A device] [-a] [-c] [-D dir .]\n"); - printf("\t-d filename: defragments one file\n"); - printf("\t-d directory: defragments the entire Btree\n"); - printf("\t-s snap_name dir: creates a new snapshot of dir\n"); -@@ -55,6 +56,9 @@ static void print_usage(void) - printf("\t-A device: scans the device file for a Btrfs filesystem\n"); - printf("\t-a: scans all devices for Btrfs filesystems\n"); - printf("\t-c: forces a single FS sync\n"); -+ printf("\t-D: delete snapshot\n"); -+ printf("\t-m [tree id] directory: set the default mounted subvolume" -+ " to the [tree id] or the directory\n"); - printf("%s\n", BTRFS_BUILD_VERSION); - exit(1); - } -@@ -99,8 +103,15 @@ int main(int ac, char **av) - int i; - unsigned long command = 0; - int len; -+ char *pos; - char *fullpath; -+ u64 objectid = 0; - -+ printf( "**\n" -+ "** WARNING: this program is considered deprecated\n" -+ "** Please consider to switch to the btrfs utility\n" -+ "**\n"); -+ - if (ac == 2 && strcmp(av[1], "-a") == 0) { - fprintf(stderr, "Scanning for Btrfs filesystems\n"); - btrfs_scan_one_dir("/dev", 1); -@@ -158,6 +169,28 @@ int main(int ac, char **av) - print_usage(); - } - command = BTRFS_IOC_DEFRAG; -+ } else if (strcmp(av[i], "-D") == 0) { -+ if (i >= ac - 1) { -+ fprintf(stderr, "-D requires an arg\n"); -+ print_usage(); -+ } -+ command = BTRFS_IOC_SNAP_DESTROY; -+ name = av[i + 1]; -+ len = strlen(name); -+ pos = strchr(name, '/'); -+ if (pos) { -+ if (*(pos + 1) == '\0') -+ *(pos) = '\0'; -+ else { -+ fprintf(stderr, -+ "error: / not allowed in names\n"); -+ exit(1); -+ } -+ } -+ if (len == 0 || len >= BTRFS_VOL_NAME_MAX) { -+ fprintf(stderr, "-D size too long\n"); -+ exit(1); -+ } - } else if (strcmp(av[i], "-A") == 0) { - if (i >= ac - 1) { - fprintf(stderr, "-A requires an arg\n"); -@@ -178,6 +211,16 @@ int main(int ac, char **av) - command = BTRFS_IOC_RESIZE; - } else if (strcmp(av[i], "-c") == 0) { - command = BTRFS_IOC_SYNC; -+ } else if (strcmp(av[i], "-m") == 0) { -+ command = BTRFS_IOC_DEFAULT_SUBVOL; -+ if (i == ac - 3) { -+ objectid = (unsigned long long) -+ strtoll(av[i + 1], NULL, 0); -+ if (errno == ERANGE) { -+ fprintf(stderr, "invalid tree id\n"); -+ exit(1); -+ } -+ } - } - } - if (command == 0) { -@@ -199,13 +242,16 @@ int main(int ac, char **av) - } - - if (name) -- strcpy(args.name, name); -+ strncpy(args.name, name, BTRFS_PATH_NAME_MAX + 1); - else - args.name[0] = '\0'; - - if (command == BTRFS_IOC_SNAP_CREATE) { - args.fd = fd; - ret = ioctl(snap_fd, command, &args); -+ } else if (command == BTRFS_IOC_DEFAULT_SUBVOL) { -+ printf("objectid is %llu\n", (unsigned long long)objectid); -+ ret = ioctl(fd, command, &objectid); - } else - ret = ioctl(fd, command, &args); - if (ret < 0) { -@@ -219,8 +265,8 @@ int main(int ac, char **av) - } - printf("%s\n", BTRFS_BUILD_VERSION); - if (ret) -- exit(0); -- else - exit(1); -+ -+ return 0; - } - -diff --git a/btrfslabel.c b/btrfslabel.c -new file mode 100644 -index 0000000..c9f4684 ---- /dev/null -+++ b/btrfslabel.c -@@ -0,0 +1,121 @@ -+/* -+ * Copyright (C) 2008 Morey Roof. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _GNU_SOURCE -+ -+#ifndef __CHECKER__ -+#include -+#include -+#include "ioctl.h" -+#endif /* __CHECKER__ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "utils.h" -+#include "version.h" -+#include "disk-io.h" -+#include "transaction.h" -+ -+#define MOUNTED 1 -+#define UNMOUNTED 2 -+#define GET_LABEL 3 -+#define SET_LABEL 4 -+ -+static void change_label_unmounted(char *dev, char *nLabel) -+{ -+ struct btrfs_root *root; -+ struct btrfs_trans_handle *trans; -+ -+ /* Open the super_block at the default location -+ * and as read-write. -+ */ -+ root = open_ctree(dev, 0, 1); -+ -+ trans = btrfs_start_transaction(root, 1); -+ strncpy(root->fs_info->super_copy.label, nLabel, BTRFS_LABEL_SIZE); -+ btrfs_commit_transaction(trans, root); -+ -+ /* Now we close it since we are done. */ -+ close_ctree(root); -+} -+ -+static void get_label_unmounted(char *dev) -+{ -+ struct btrfs_root *root; -+ -+ /* Open the super_block at the default location -+ * and as read-only. -+ */ -+ root = open_ctree(dev, 0, 0); -+ -+ fprintf(stdout, "%s\n", root->fs_info->super_copy.label); -+ -+ /* Now we close it since we are done. */ -+ close_ctree(root); -+} -+ -+int get_label(char *btrfs_dev) -+{ -+ -+ int ret; -+ ret = check_mounted(btrfs_dev); -+ if (ret < 0) -+ { -+ fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev); -+ return -1; -+ } -+ -+ if(ret != 0) -+ { -+ fprintf(stderr, "FATAL: the filesystem has to be unmounted\n"); -+ return -2; -+ } -+ get_label_unmounted(btrfs_dev); -+ return 0; -+} -+ -+ -+int set_label(char *btrfs_dev, char *nLabel) -+{ -+ -+ int ret; -+ ret = check_mounted(btrfs_dev); -+ if (ret < 0) -+ { -+ fprintf(stderr, "FATAL: error checking %s mount status\n", btrfs_dev); -+ return -1; -+ } -+ -+ if(ret != 0) -+ { -+ fprintf(stderr, "FATAL: the filesystem has to be unmounted\n"); -+ return -2; -+ } -+ change_label_unmounted(btrfs_dev, nLabel); -+ return 0; -+} -diff --git a/btrfslabel.h b/btrfslabel.h -new file mode 100644 -index 0000000..abf43ad ---- /dev/null -+++ b/btrfslabel.h -@@ -0,0 +1,5 @@ -+/* btrflabel.h */ -+ -+ -+int get_label(char *btrfs_dev); -+int set_label(char *btrfs_dev, char *nLabel); -\ No newline at end of file -diff --git a/calc-size.c b/calc-size.c -new file mode 100644 -index 0000000..c4adfb0 ---- /dev/null -+++ b/calc-size.c -@@ -0,0 +1,268 @@ -+/* -+ * Copyright (C) 2011 Red Hat. All rights reserved. -+ * -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#define _XOPEN_SOURCE 500 -+#define _GNU_SOURCE 1 -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include "kerncompat.h" -+#include "ctree.h" -+#include "disk-io.h" -+#include "print-tree.h" -+#include "transaction.h" -+#include "list.h" -+#include "version.h" -+#include "volumes.h" -+#include "utils.h" -+ -+static int verbose = 0; -+static int no_pretty = 0; -+ -+struct root_stats { -+ u64 total_nodes; -+ u64 total_leaves; -+ u64 total_bytes; -+ u64 total_inline; -+ int total_levels; -+}; -+ -+struct fs_root { -+ struct btrfs_key key; -+ struct btrfs_key *snaps; -+}; -+ -+static int walk_leaf(struct btrfs_root *root, struct btrfs_path *path, -+ struct root_stats *stat, int find_inline) -+{ -+ struct extent_buffer *b = path->nodes[0]; -+ struct btrfs_file_extent_item *fi; -+ struct btrfs_key found_key; -+ int i; -+ -+ stat->total_bytes += root->leafsize; -+ stat->total_leaves++; -+ -+ if (!find_inline) -+ return 0; -+ -+ for (i = 0; i < btrfs_header_nritems(b); i++) { -+ btrfs_item_key_to_cpu(b, &found_key, i); -+ if (found_key.type != BTRFS_EXTENT_DATA_KEY) -+ continue; -+ -+ fi = btrfs_item_ptr(b, i, struct btrfs_file_extent_item); -+ if (btrfs_file_extent_type(b, fi) == BTRFS_FILE_EXTENT_INLINE) -+ stat->total_inline += -+ btrfs_file_extent_inline_item_len(b, -+ btrfs_item_nr(b, i)); -+ } -+ -+ return 0; -+} -+ -+static int walk_nodes(struct btrfs_root *root, struct btrfs_path *path, -+ struct root_stats *stat, int level, int find_inline) -+{ -+ struct extent_buffer *b = path->nodes[level]; -+ int i; -+ int ret = 0; -+ -+ stat->total_bytes += root->nodesize; -+ stat->total_nodes++; -+ -+ for (i = 0; i < btrfs_header_nritems(b); i++) { -+ struct extent_buffer *tmp = NULL; -+ -+ path->slots[level] = i; -+ if ((level - 1) > 0 || find_inline) { -+ tmp = read_tree_block(root, btrfs_node_blockptr(b, i), -+ btrfs_level_size(root, level - 1), -+ btrfs_node_ptr_generation(b, i)); -+ if (!tmp) { -+ fprintf(stderr, "Failed to read blocknr %Lu\n", -+ btrfs_node_blockptr(b, i)); -+ continue; -+ } -+ path->nodes[level - 1] = tmp; -+ } -+ if (level - 1) -+ ret = walk_nodes(root, path, stat, level - 1, -+ find_inline); -+ else -+ ret = walk_leaf(root, path, stat, find_inline); -+ free_extent_buffer(tmp); -+ if (ret) { -+ fprintf(stderr, "Error walking down path\n"); -+ break; -+ } -+ } -+ -+ return ret; -+} -+ -+static int calc_root_size(struct btrfs_root *tree_root, struct btrfs_key *key, -+ int find_inline) -+{ -+ struct btrfs_root *root; -+ struct btrfs_path *path; -+ struct root_stats stat; -+ int level; -+ int ret = 0; -+ int size_fail = 0; -+ -+ root = btrfs_read_fs_root(tree_root->fs_info, key); -+ if (!root) { -+ fprintf(stderr, "Failed to read root %Lu\n", key->objectid); -+ return 1; -+ } -+ -+ path = btrfs_alloc_path(); -+ if (!path) { -+ fprintf(stderr, "Could not allocate path\n"); -+ return 1; -+ } -+ -+ memset(&stat, 0, sizeof(stat)); -+ level = btrfs_header_level(root->node); -+ path->nodes[level] = root->node; -+ if (!level) { -+ ret = walk_leaf(root, path, &stat, find_inline); -+ if (ret) -+ goto out; -+ goto out_print; -+ } -+ -+ ret = walk_nodes(root, path, &stat, level, find_inline); -+ if (ret) -+ goto out; -+out_print: -+ if (no_pretty || size_fail) { -+ printf("\t%Lu total bytes, %Lu inline data bytes, %Lu nodes, " -+ "%Lu leaves, %d levels\n", stat.total_bytes, -+ stat.total_inline, stat.total_nodes, stat.total_leaves, -+ level + 1); -+ } else { -+ char *total_size; -+ char *inline_size; -+ -+ total_size = pretty_sizes(stat.total_bytes); -+ inline_size = pretty_sizes(stat.total_inline); -+ -+ printf("\t%s total size, %s inline data, %Lu nodes, " -+ "%Lu leaves, %d levels\n", -+ total_size, inline_size, stat.total_nodes, -+ stat.total_leaves, level + 1); -+ free(total_size); -+ free(inline_size); -+ } -+out: -+ btrfs_free_path(path); -+ return ret; -+} -+ -+static void usage() -+{ -+ fprintf(stderr, "Usage: calc-size [-v] [-b] \n"); -+} -+ -+int main(int argc, char **argv) -+{ -+ struct btrfs_key key; -+ struct fs_root *roots; -+ struct btrfs_root *root; -+ size_t fs_roots_size = sizeof(struct fs_root); -+ int opt; -+ int ret = 0; -+ -+ while ((opt = getopt(argc, argv, "vb")) != -1) { -+ switch (opt) { -+ case 'v': -+ verbose++; -+ break; -+ case 'b': -+ no_pretty = 1; -+ break; -+ default: -+ usage(); -+ exit(1); -+ } -+ } -+ -+ if (optind >= argc) { -+ usage(); -+ exit(1); -+ } -+ -+ /* -+ if ((ret = check_mounted(argv[optind])) < 0) { -+ fprintf(stderr, "Could not check mount status: %d\n", ret); -+ if (ret == -EACCES) -+ fprintf(stderr, "Maybe you need to run as root?\n"); -+ return ret; -+ } else if (ret) { -+ fprintf(stderr, "%s is currently mounted. Aborting.\n", -+ argv[optind]); -+ return -EBUSY; -+ } -+ */ -+ -+ root = open_ctree(argv[optind], 0, 0); -+ if (!root) { -+ fprintf(stderr, "Couldn't open ctree\n"); -+ exit(1); -+ } -+ -+ roots = malloc(fs_roots_size); -+ if (!roots) { -+ fprintf(stderr, "No memory\n"); -+ goto out; -+ } -+ -+ printf("Calculating size of root tree\n"); -+ key.objectid = BTRFS_ROOT_TREE_OBJECTID; -+ ret = calc_root_size(root, &key, 0); -+ if (ret) -+ goto out; -+ -+ printf("Calculating size of extent tree\n"); -+ key.objectid = BTRFS_EXTENT_TREE_OBJECTID; -+ ret = calc_root_size(root, &key, 0); -+ if (ret) -+ goto out; -+ -+ printf("Calculating size of csum tree\n"); -+ key.objectid = BTRFS_CSUM_TREE_OBJECTID; -+ ret = calc_root_size(root, &key, 0); -+ if (ret) -+ goto out; -+ -+ roots[0].key.objectid = BTRFS_FS_TREE_OBJECTID; -+ roots[0].key.offset = (u64)-1; -+ printf("Calculatin' size of fs tree\n"); -+ ret = calc_root_size(root, &roots[0].key, 1); -+ if (ret) -+ goto out; -+out: -+ close_ctree(root); -+ return ret; -+} -diff --git a/cmds-balance.c b/cmds-balance.c -new file mode 100644 -index 0000000..38a7426 ---- /dev/null -+++ b/cmds-balance.c -@@ -0,0 +1,713 @@ -+/* -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "kerncompat.h" -+#include "ctree.h" -+#include "ioctl.h" -+#include "volumes.h" -+ -+#include "commands.h" -+ -+static const char * const balance_cmd_group_usage[] = { -+ "btrfs [filesystem] balance [options] ", -+ "btrfs [filesystem] balance ", -+ NULL -+}; -+ -+static const char balance_cmd_group_info[] = -+ "'btrfs filesystem balance' command is deprecated, please use\n" -+ "'btrfs balance start' command instead."; -+ -+static int parse_one_profile(const char *profile, u64 *flags) -+{ -+ if (!strcmp(profile, "raid0")) { -+ *flags |= BTRFS_BLOCK_GROUP_RAID0; -+ } else if (!strcmp(profile, "raid1")) { -+ *flags |= BTRFS_BLOCK_GROUP_RAID1; -+ } else if (!strcmp(profile, "raid10")) { -+ *flags |= BTRFS_BLOCK_GROUP_RAID10; -+ } else if (!strcmp(profile, "dup")) { -+ *flags |= BTRFS_BLOCK_GROUP_DUP; -+ } else if (!strcmp(profile, "single")) { -+ *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE; -+ } else { -+ fprintf(stderr, "Unknown profile '%s'\n", profile); -+ return 1; -+ } -+ -+ return 0; -+} -+ -+static int parse_profiles(char *profiles, u64 *flags) -+{ -+ char *this_char; -+ char *save_ptr; -+ -+ for (this_char = strtok_r(profiles, "|", &save_ptr); -+ this_char != NULL; -+ this_char = strtok_r(NULL, "|", &save_ptr)) { -+ if (parse_one_profile(this_char, flags)) -+ return 1; -+ } -+ -+ return 0; -+} -+ -+static int parse_u64(const char *str, u64 *result) -+{ -+ char *endptr; -+ u64 val; -+ -+ val = strtoull(str, &endptr, 10); -+ if (*endptr) -+ return 1; -+ -+ *result = val; -+ return 0; -+} -+ -+static int parse_range(const char *range, u64 *start, u64 *end) -+{ -+ char *dots; -+ -+ dots = strstr(range, ".."); -+ if (dots) { -+ const char *rest = dots + 2; -+ int skipped = 0; -+ -+ *dots = 0; -+ -+ if (!*rest) { -+ *end = (u64)-1; -+ skipped++; -+ } else { -+ if (parse_u64(rest, end)) -+ return 1; -+ } -+ if (dots == range) { -+ *start = 0; -+ skipped++; -+ } else { -+ if (parse_u64(range, start)) -+ return 1; -+ } -+ -+ if (*start >= *end) { -+ fprintf(stderr, "Range %llu..%llu doesn't make " -+ "sense\n", (unsigned long long)*start, -+ (unsigned long long)*end); -+ return 1; -+ } -+ -+ if (skipped <= 1) -+ return 0; -+ } -+ -+ return 1; -+} -+ -+static int parse_filters(char *filters, struct btrfs_balance_args *args) -+{ -+ char *this_char; -+ char *value; -+ char *save_ptr; -+ -+ if (!filters) -+ return 0; -+ -+ for (this_char = strtok_r(filters, ",", &save_ptr); -+ this_char != NULL; -+ this_char = strtok_r(NULL, ",", &save_ptr)) { -+ if ((value = strchr(this_char, '=')) != NULL) -+ *value++ = 0; -+ if (!strcmp(this_char, "profiles")) { -+ if (!value || !*value) { -+ fprintf(stderr, "the profiles filter requires " -+ "an argument\n"); -+ return 1; -+ } -+ if (parse_profiles(value, &args->profiles)) { -+ fprintf(stderr, "Invalid profiles argument\n"); -+ return 1; -+ } -+ args->flags |= BTRFS_BALANCE_ARGS_PROFILES; -+ } else if (!strcmp(this_char, "usage")) { -+ if (!value || !*value) { -+ fprintf(stderr, "the usage filter requires " -+ "an argument\n"); -+ return 1; -+ } -+ if (parse_u64(value, &args->usage) || -+ args->usage < 1 || args->usage > 100) { -+ fprintf(stderr, "Invalid usage argument: %s\n", -+ value); -+ return 1; -+ } -+ args->flags |= BTRFS_BALANCE_ARGS_USAGE; -+ } else if (!strcmp(this_char, "devid")) { -+ if (!value || !*value) { -+ fprintf(stderr, "the devid filter requires " -+ "an argument\n"); -+ return 1; -+ } -+ if (parse_u64(value, &args->devid) || -+ args->devid == 0) { -+ fprintf(stderr, "Invalid devid argument: %s\n", -+ value); -+ return 1; -+ } -+ args->flags |= BTRFS_BALANCE_ARGS_DEVID; -+ } else if (!strcmp(this_char, "drange")) { -+ if (!value || !*value) { -+ fprintf(stderr, "the drange filter requires " -+ "an argument\n"); -+ return 1; -+ } -+ if (parse_range(value, &args->pstart, &args->pend)) { -+ fprintf(stderr, "Invalid drange argument\n"); -+ return 1; -+ } -+ args->flags |= BTRFS_BALANCE_ARGS_DRANGE; -+ } else if (!strcmp(this_char, "vrange")) { -+ if (!value || !*value) { -+ fprintf(stderr, "the vrange filter requires " -+ "an argument\n"); -+ return 1; -+ } -+ if (parse_range(value, &args->vstart, &args->vend)) { -+ fprintf(stderr, "Invalid vrange argument\n"); -+ return 1; -+ } -+ args->flags |= BTRFS_BALANCE_ARGS_VRANGE; -+ } else if (!strcmp(this_char, "convert")) { -+ if (!value || !*value) { -+ fprintf(stderr, "the convert option requires " -+ "an argument\n"); -+ return 1; -+ } -+ if (parse_one_profile(value, &args->target)) { -+ fprintf(stderr, "Invalid convert argument\n"); -+ return 1; -+ } -+ args->flags |= BTRFS_BALANCE_ARGS_CONVERT; -+ } else if (!strcmp(this_char, "soft")) { -+ args->flags |= BTRFS_BALANCE_ARGS_SOFT; -+ } else { -+ fprintf(stderr, "Unrecognized balance option '%s'\n", -+ this_char); -+ return 1; -+ } -+ } -+ -+ return 0; -+} -+ -+static void dump_balance_args(struct btrfs_balance_args *args) -+{ -+ if (args->flags & BTRFS_BALANCE_ARGS_CONVERT) { -+ printf("converting, target=%llu, soft is %s", -+ (unsigned long long)args->target, -+ (args->flags & BTRFS_BALANCE_ARGS_SOFT) ? "on" : "off"); -+ } else { -+ printf("balancing"); -+ } -+ -+ if (args->flags & BTRFS_BALANCE_ARGS_PROFILES) -+ printf(", profiles=%llu", (unsigned long long)args->profiles); -+ if (args->flags & BTRFS_BALANCE_ARGS_USAGE) -+ printf(", usage=%llu", (unsigned long long)args->usage); -+ if (args->flags & BTRFS_BALANCE_ARGS_DEVID) -+ printf(", devid=%llu", (unsigned long long)args->devid); -+ if (args->flags & BTRFS_BALANCE_ARGS_DRANGE) -+ printf(", drange=%llu..%llu", -+ (unsigned long long)args->pstart, -+ (unsigned long long)args->pend); -+ if (args->flags & BTRFS_BALANCE_ARGS_VRANGE) -+ printf(", vrange=%llu..%llu", -+ (unsigned long long)args->vstart, -+ (unsigned long long)args->vend); -+ -+ printf("\n"); -+} -+ -+static void dump_ioctl_balance_args(struct btrfs_ioctl_balance_args *args) -+{ -+ printf("Dumping filters: flags 0x%llx, state 0x%llx, force is %s\n", -+ (unsigned long long)args->flags, (unsigned long long)args->state, -+ (args->flags & BTRFS_BALANCE_FORCE) ? "on" : "off"); -+ if (args->flags & BTRFS_BALANCE_DATA) { -+ printf(" DATA (flags 0x%llx): ", -+ (unsigned long long)args->data.flags); -+ dump_balance_args(&args->data); -+ } -+ if (args->flags & BTRFS_BALANCE_METADATA) { -+ printf(" METADATA (flags 0x%llx): ", -+ (unsigned long long)args->meta.flags); -+ dump_balance_args(&args->meta); -+ } -+ if (args->flags & BTRFS_BALANCE_SYSTEM) { -+ printf(" SYSTEM (flags 0x%llx): ", -+ (unsigned long long)args->sys.flags); -+ dump_balance_args(&args->sys); -+ } -+} -+ -+static int do_balance_v1(int fd) -+{ -+ struct btrfs_ioctl_vol_args args; -+ int ret; -+ -+ memset(&args, 0, sizeof(args)); -+ ret = ioctl(fd, BTRFS_IOC_BALANCE, &args); -+ return ret; -+} -+ -+static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, -+ int nofilters) -+{ -+ int fd; -+ int ret; -+ int e; -+ -+ fd = open_file_or_dir(path); -+ if (fd < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", path); -+ return 12; -+ } -+ -+ ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args); -+ e = errno; -+ -+ if (ret < 0) { -+ /* -+ * older kernels don't have the new balance ioctl, try the -+ * old one. But, the old one doesn't know any filters, so -+ * don't fall back if they tried to use the fancy new things -+ */ -+ if (e == ENOTTY && nofilters) { -+ ret = do_balance_v1(fd); -+ if (ret == 0) -+ goto out; -+ e = errno; -+ } -+ -+ if (e == ECANCELED) { -+ if (args->state & BTRFS_BALANCE_STATE_PAUSE_REQ) -+ fprintf(stderr, "balance paused by user\n"); -+ if (args->state & BTRFS_BALANCE_STATE_CANCEL_REQ) -+ fprintf(stderr, "balance canceled by user\n"); -+ ret = 0; -+ } else { -+ fprintf(stderr, "ERROR: error during balancing '%s' " -+ "- %s\n", path, strerror(e)); -+ if (e != EINPROGRESS) -+ fprintf(stderr, "There may be more info in " -+ "syslog - try dmesg | tail\n"); -+ ret = 19; -+ } -+ } else { -+ printf("Done, had to relocate %llu out of %llu chunks\n", -+ (unsigned long long)args->stat.completed, -+ (unsigned long long)args->stat.considered); -+ ret = 0; -+ } -+ -+out: -+ close(fd); -+ return ret; -+} -+ -+static const char * const cmd_balance_start_usage[] = { -+ "btrfs [filesystem] balance start [options] ", -+ "Balance chunks across the devices", -+ "Balance and/or convert (change allocation profile of) chunks that", -+ "passed all filters in a comma-separated list of filters for a", -+ "particular chunk type. If filter list is not given balance all", -+ "chunks of that type. In case none of the -d, -m or -s options is", -+ "given balance all chunks in a filesystem.", -+ "", -+ "-d[filters] act on data chunks", -+ "-m[filters] act on metadata chunks", -+ "-s[filetrs] act on system chunks (only under -f)", -+ "-v be verbose", -+ "-f force reducing of metadata integrity", -+ NULL -+}; -+ -+static int cmd_balance_start(int argc, char **argv) -+{ -+ struct btrfs_ioctl_balance_args args; -+ struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys, -+ &args.meta, NULL }; -+ int force = 0; -+ int verbose = 0; -+ int nofilters = 1; -+ int i; -+ -+ memset(&args, 0, sizeof(args)); -+ -+ optind = 1; -+ while (1) { -+ int longindex; -+ static struct option longopts[] = { -+ { "data", optional_argument, NULL, 'd'}, -+ { "metadata", optional_argument, NULL, 'm' }, -+ { "system", optional_argument, NULL, 's' }, -+ { "force", no_argument, NULL, 'f' }, -+ { "verbose", no_argument, NULL, 'v' }, -+ { 0, 0, 0, 0 } -+ }; -+ -+ int opt = getopt_long(argc, argv, "d::s::m::fv", longopts, -+ &longindex); -+ if (opt < 0) -+ break; -+ -+ switch (opt) { -+ case 'd': -+ nofilters = 0; -+ args.flags |= BTRFS_BALANCE_DATA; -+ -+ if (parse_filters(optarg, &args.data)) -+ return 1; -+ break; -+ case 's': -+ nofilters = 0; -+ args.flags |= BTRFS_BALANCE_SYSTEM; -+ -+ if (parse_filters(optarg, &args.sys)) -+ return 1; -+ break; -+ case 'm': -+ nofilters = 0; -+ args.flags |= BTRFS_BALANCE_METADATA; -+ -+ if (parse_filters(optarg, &args.meta)) -+ return 1; -+ break; -+ case 'f': -+ force = 1; -+ break; -+ case 'v': -+ verbose = 1; -+ break; -+ default: -+ usage(cmd_balance_start_usage); -+ } -+ } -+ -+ if (check_argc_exact(argc - optind, 1)) -+ usage(cmd_balance_start_usage); -+ -+ /* -+ * allow -s only under --force, otherwise do with system chunks -+ * the same thing we were ordered to do with meta chunks -+ */ -+ if (args.flags & BTRFS_BALANCE_SYSTEM) { -+ if (!force) { -+ fprintf(stderr, -+"Refusing to explicitly operate on system chunks.\n" -+"Pass --force if you really want to do that.\n"); -+ return 1; -+ } -+ } else if (args.flags & BTRFS_BALANCE_METADATA) { -+ args.flags |= BTRFS_BALANCE_SYSTEM; -+ memcpy(&args.sys, &args.meta, -+ sizeof(struct btrfs_balance_args)); -+ } -+ -+ if (nofilters) { -+ /* relocate everything - no filters */ -+ args.flags |= BTRFS_BALANCE_TYPE_MASK; -+ } -+ -+ /* drange makes sense only when devid is set */ -+ for (i = 0; ptrs[i]; i++) { -+ if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_DRANGE) && -+ !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_DEVID)) { -+ fprintf(stderr, "drange filter can be used only if " -+ "devid filter is used\n"); -+ return 1; -+ } -+ } -+ -+ /* soft makes sense only when convert for corresponding type is set */ -+ for (i = 0; ptrs[i]; i++) { -+ if ((ptrs[i]->flags & BTRFS_BALANCE_ARGS_SOFT) && -+ !(ptrs[i]->flags & BTRFS_BALANCE_ARGS_CONVERT)) { -+ fprintf(stderr, "'soft' option can be used only if " -+ "changing profiles\n"); -+ return 1; -+ } -+ } -+ -+ if (force) -+ args.flags |= BTRFS_BALANCE_FORCE; -+ if (verbose) -+ dump_ioctl_balance_args(&args); -+ -+ return do_balance(argv[optind], &args, nofilters); -+} -+ -+static const char * const cmd_balance_pause_usage[] = { -+ "btrfs [filesystem] balance pause ", -+ "Pause running balance", -+ NULL -+}; -+ -+static int cmd_balance_pause(int argc, char **argv) -+{ -+ const char *path; -+ int fd; -+ int ret; -+ int e; -+ -+ if (check_argc_exact(argc, 2)) -+ usage(cmd_balance_pause_usage); -+ -+ path = argv[1]; -+ -+ fd = open_file_or_dir(path); -+ if (fd < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", path); -+ return 12; -+ } -+ -+ ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE); -+ e = errno; -+ close(fd); -+ -+ if (ret < 0) { -+ fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n", -+ path, (e == ENOTCONN) ? "Not running" : strerror(e)); -+ return 19; -+ } -+ -+ return 0; -+} -+ -+static const char * const cmd_balance_cancel_usage[] = { -+ "btrfs [filesystem] balance cancel ", -+ "Cancel running or paused balance", -+ NULL -+}; -+ -+static int cmd_balance_cancel(int argc, char **argv) -+{ -+ const char *path; -+ int fd; -+ int ret; -+ int e; -+ -+ if (check_argc_exact(argc, 2)) -+ usage(cmd_balance_cancel_usage); -+ -+ path = argv[1]; -+ -+ fd = open_file_or_dir(path); -+ if (fd < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", path); -+ return 12; -+ } -+ -+ ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL); -+ e = errno; -+ close(fd); -+ -+ if (ret < 0) { -+ fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n", -+ path, (e == ENOTCONN) ? "Not in progress" : strerror(e)); -+ return 19; -+ } -+ -+ return 0; -+} -+ -+static const char * const cmd_balance_resume_usage[] = { -+ "btrfs [filesystem] balance resume ", -+ "Resume interrupted balance", -+ NULL -+}; -+ -+static int cmd_balance_resume(int argc, char **argv) -+{ -+ struct btrfs_ioctl_balance_args args; -+ const char *path; -+ int fd; -+ int ret; -+ int e; -+ -+ if (check_argc_exact(argc, 2)) -+ usage(cmd_balance_resume_usage); -+ -+ path = argv[1]; -+ -+ fd = open_file_or_dir(path); -+ if (fd < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", path); -+ return 12; -+ } -+ -+ memset(&args, 0, sizeof(args)); -+ args.flags |= BTRFS_BALANCE_RESUME; -+ -+ ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, &args); -+ e = errno; -+ close(fd); -+ -+ if (ret < 0) { -+ if (e == ECANCELED) { -+ if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ) -+ fprintf(stderr, "balance paused by user\n"); -+ if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ) -+ fprintf(stderr, "balance canceled by user\n"); -+ } else if (e == ENOTCONN || e == EINPROGRESS) { -+ fprintf(stderr, "ERROR: balance resume on '%s' " -+ "failed - %s\n", path, -+ (e == ENOTCONN) ? "Not in progress" : -+ "Already running"); -+ return 19; -+ } else { -+ fprintf(stderr, -+"ERROR: error during balancing '%s' - %s\n" -+"There may be more info in syslog - try dmesg | tail\n", path, strerror(e)); -+ return 19; -+ } -+ } else { -+ printf("Done, had to relocate %llu out of %llu chunks\n", -+ (unsigned long long)args.stat.completed, -+ (unsigned long long)args.stat.considered); -+ } -+ -+ return 0; -+} -+ -+static const char * const cmd_balance_status_usage[] = { -+ "btrfs [filesystem] balance status [-v] ", -+ "Show status of running or paused balance", -+ "", -+ "-v be verbose", -+ NULL -+}; -+ -+static int cmd_balance_status(int argc, char **argv) -+{ -+ struct btrfs_ioctl_balance_args args; -+ const char *path; -+ int fd; -+ int verbose = 0; -+ int ret; -+ int e; -+ -+ optind = 1; -+ while (1) { -+ int longindex; -+ static struct option longopts[] = { -+ { "verbose", no_argument, NULL, 'v' }, -+ { 0, 0, 0, 0} -+ }; -+ -+ int opt = getopt_long(argc, argv, "v", longopts, &longindex); -+ if (opt < 0) -+ break; -+ -+ switch (opt) { -+ case 'v': -+ verbose = 1; -+ break; -+ default: -+ usage(cmd_balance_status_usage); -+ } -+ } -+ -+ if (check_argc_exact(argc - optind, 1)) -+ usage(cmd_balance_status_usage); -+ -+ path = argv[optind]; -+ -+ fd = open_file_or_dir(path); -+ if (fd < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", path); -+ return 12; -+ } -+ -+ ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &args); -+ e = errno; -+ close(fd); -+ -+ if (ret < 0) { -+ fprintf(stderr, "ERROR: balance status on '%s' failed - %s\n", -+ path, (e == ENOTCONN) ? "Not in progress" : strerror(e)); -+ return 19; -+ } -+ -+ if (args.state & BTRFS_BALANCE_STATE_RUNNING) { -+ printf("Balance on '%s' is running", path); -+ if (args.state & BTRFS_BALANCE_STATE_CANCEL_REQ) -+ printf(", cancel requested\n"); -+ else if (args.state & BTRFS_BALANCE_STATE_PAUSE_REQ) -+ printf(", pause requested\n"); -+ else -+ printf("\n"); -+ } else { -+ printf("Balance on '%s' is paused\n", path); -+ } -+ -+ printf("%llu out of about %llu chunks balanced (%llu considered), " -+ "%3.f%% left\n", (unsigned long long)args.stat.completed, -+ (unsigned long long)args.stat.expected, -+ (unsigned long long)args.stat.considered, -+ 100 * (1 - (float)args.stat.completed/args.stat.expected)); -+ -+ if (verbose) -+ dump_ioctl_balance_args(&args); -+ -+ return 0; -+} -+ -+const struct cmd_group balance_cmd_group = { -+ balance_cmd_group_usage, balance_cmd_group_info, { -+ { "start", cmd_balance_start, cmd_balance_start_usage, NULL, 0 }, -+ { "pause", cmd_balance_pause, cmd_balance_pause_usage, NULL, 0 }, -+ { "cancel", cmd_balance_cancel, cmd_balance_cancel_usage, NULL, 0 }, -+ { "resume", cmd_balance_resume, cmd_balance_resume_usage, NULL, 0 }, -+ { "status", cmd_balance_status, cmd_balance_status_usage, NULL, 0 }, -+ { 0, 0, 0, 0, 0 } -+ } -+}; -+ -+int cmd_balance(int argc, char **argv) -+{ -+ if (argc == 2) { -+ /* old 'btrfs filesystem balance ' syntax */ -+ struct btrfs_ioctl_balance_args args; -+ -+ memset(&args, 0, sizeof(args)); -+ args.flags |= BTRFS_BALANCE_TYPE_MASK; -+ -+ return do_balance(argv[1], &args, 1); -+ } -+ -+ return handle_command_group(&balance_cmd_group, argc, argv); -+} -diff --git a/cmds-device.c b/cmds-device.c -new file mode 100644 -index 0000000..db625a6 ---- /dev/null -+++ b/cmds-device.c -@@ -0,0 +1,261 @@ -+/* -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "kerncompat.h" -+#include "ctree.h" -+#include "ioctl.h" -+#include "utils.h" -+ -+#include "commands.h" -+ -+/* FIXME - imported cruft, fix sparse errors and warnings */ -+#ifdef __CHECKER__ -+#define BLKGETSIZE64 0 -+#define BTRFS_IOC_SNAP_CREATE_V2 0 -+#define BTRFS_VOL_NAME_MAX 255 -+struct btrfs_ioctl_vol_args { char name[BTRFS_VOL_NAME_MAX]; }; -+static inline int ioctl(int fd, int define, void *arg) { return 0; } -+#endif -+ -+static const char * const device_cmd_group_usage[] = { -+ "btrfs device []", -+ NULL -+}; -+ -+static const char * const cmd_add_dev_usage[] = { -+ "btrfs device add [...] ", -+ "Add a device to a filesystem", -+ NULL -+}; -+ -+static int cmd_add_dev(int argc, char **argv) -+{ -+ char *mntpnt; -+ int i, fdmnt, ret=0, e; -+ -+ if (check_argc_min(argc, 3)) -+ usage(cmd_add_dev_usage); -+ -+ mntpnt = argv[argc - 1]; -+ -+ fdmnt = open_file_or_dir(mntpnt); -+ if (fdmnt < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt); -+ return 12; -+ } -+ -+ for (i = 1; i < argc - 1; i++ ){ -+ struct btrfs_ioctl_vol_args ioctl_args; -+ int devfd, res; -+ u64 dev_block_count = 0; -+ struct stat st; -+ int mixed = 0; -+ -+ res = check_mounted(argv[i]); -+ if (res < 0) { -+ fprintf(stderr, "error checking %s mount status\n", -+ argv[i]); -+ ret++; -+ continue; -+ } -+ if (res == 1) { -+ fprintf(stderr, "%s is mounted\n", argv[i]); -+ ret++; -+ continue; -+ } -+ -+ devfd = open(argv[i], O_RDWR); -+ if (!devfd) { -+ fprintf(stderr, "ERROR: Unable to open device '%s'\n", argv[i]); -+ close(devfd); -+ ret++; -+ continue; -+ } -+ res = fstat(devfd, &st); -+ if (res) { -+ fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]); -+ close(devfd); -+ ret++; -+ continue; -+ } -+ if (!S_ISBLK(st.st_mode)) { -+ fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]); -+ close(devfd); -+ ret++; -+ continue; -+ } -+ -+ res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count, &mixed); -+ if (res) { -+ fprintf(stderr, "ERROR: Unable to init '%s'\n", argv[i]); -+ close(devfd); -+ ret++; -+ continue; -+ } -+ close(devfd); -+ -+ strncpy(ioctl_args.name, argv[i], BTRFS_PATH_NAME_MAX); -+ res = ioctl(fdmnt, BTRFS_IOC_ADD_DEV, &ioctl_args); -+ e = errno; -+ if(res<0){ -+ fprintf(stderr, "ERROR: error adding the device '%s' - %s\n", -+ argv[i], strerror(e)); -+ ret++; -+ } -+ -+ } -+ -+ close(fdmnt); -+ if (ret) -+ return ret+20; -+ else -+ return 0; -+} -+ -+static const char * const cmd_rm_dev_usage[] = { -+ "btrfs device delete [...] ", -+ "Remove a device from a filesystem", -+ NULL -+}; -+ -+static int cmd_rm_dev(int argc, char **argv) -+{ -+ char *mntpnt; -+ int i, fdmnt, ret=0, e; -+ -+ if (check_argc_min(argc, 3)) -+ usage(cmd_rm_dev_usage); -+ -+ mntpnt = argv[argc - 1]; -+ -+ fdmnt = open_file_or_dir(mntpnt); -+ if (fdmnt < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", mntpnt); -+ return 12; -+ } -+ -+ for(i=1 ; i < argc - 1; i++ ){ -+ struct btrfs_ioctl_vol_args arg; -+ int res; -+ -+ strncpy(arg.name, argv[i], BTRFS_PATH_NAME_MAX); -+ res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg); -+ e = errno; -+ if(res<0){ -+ fprintf(stderr, "ERROR: error removing the device '%s' - %s\n", -+ argv[i], strerror(e)); -+ ret++; -+ } -+ } -+ -+ close(fdmnt); -+ if( ret) -+ return ret+20; -+ else -+ return 0; -+} -+ -+static const char * const cmd_scan_dev_usage[] = { -+ "btrfs device scan [...]", -+ "Scan devices for a btrfs filesystem", -+ NULL -+}; -+ -+static int cmd_scan_dev(int argc, char **argv) -+{ -+ int i, fd, e; -+ int checklist = 1; -+ int devstart = 1; -+ -+ if( argc > 1 && !strcmp(argv[1],"--all-devices")){ -+ if (check_argc_max(argc, 2)) -+ usage(cmd_scan_dev_usage); -+ -+ checklist = 0; -+ devstart += 1; -+ } -+ -+ if(argc<=devstart){ -+ -+ int ret; -+ -+ printf("Scanning for Btrfs filesystems\n"); -+ if(checklist) -+ ret = btrfs_scan_block_devices(1); -+ else -+ ret = btrfs_scan_one_dir("/dev", 1); -+ if (ret){ -+ fprintf(stderr, "ERROR: error %d while scanning\n", ret); -+ return 18; -+ } -+ return 0; -+ } -+ -+ fd = open("/dev/btrfs-control", O_RDWR); -+ if (fd < 0) { -+ perror("failed to open /dev/btrfs-control"); -+ return 10; -+ } -+ -+ for( i = devstart ; i < argc ; i++ ){ -+ struct btrfs_ioctl_vol_args args; -+ int ret; -+ -+ printf("Scanning for Btrfs filesystems in '%s'\n", argv[i]); -+ -+ strncpy(args.name, argv[i], BTRFS_PATH_NAME_MAX); -+ /* -+ * FIXME: which are the error code returned by this ioctl ? -+ * it seems that is impossible to understand if there no is -+ * a btrfs filesystem from an I/O error !!! -+ */ -+ ret = ioctl(fd, BTRFS_IOC_SCAN_DEV, &args); -+ e = errno; -+ -+ if( ret < 0 ){ -+ close(fd); -+ fprintf(stderr, "ERROR: unable to scan the device '%s' - %s\n", -+ argv[i], strerror(e)); -+ return 11; -+ } -+ } -+ -+ close(fd); -+ return 0; -+} -+ -+const struct cmd_group device_cmd_group = { -+ device_cmd_group_usage, NULL, { -+ { "add", cmd_add_dev, cmd_add_dev_usage, NULL, 0 }, -+ { "delete", cmd_rm_dev, cmd_rm_dev_usage, NULL, 0 }, -+ { "scan", cmd_scan_dev, cmd_scan_dev_usage, NULL, 0 }, -+ { 0, 0, 0, 0, 0 } -+ } -+}; -+ -+int cmd_device(int argc, char **argv) -+{ -+ return handle_command_group(&device_cmd_group, argc, argv); -+} -diff --git a/cmds-filesystem.c b/cmds-filesystem.c -new file mode 100644 -index 0000000..1f53d1c ---- /dev/null -+++ b/cmds-filesystem.c -@@ -0,0 +1,538 @@ -+/* -+ * This program is free software; you can redistribute it and/or -+ * modify it under the terms of the GNU General Public -+ * License v2 as published by the Free Software Foundation. -+ * -+ * This program is distributed in the hope that it will be useful, -+ * but WITHOUT ANY WARRANTY; without even the implied warranty of -+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -+ * General Public License for more details. -+ * -+ * You should have received a copy of the GNU General Public -+ * License along with this program; if not, write to the -+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330, -+ * Boston, MA 021110-1307, USA. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "kerncompat.h" -+#include "ctree.h" -+#include "ioctl.h" -+#include "utils.h" -+#include "volumes.h" -+ -+#include "version.h" -+ -+#include "commands.h" -+#include "btrfslabel.h" -+ -+static const char * const filesystem_cmd_group_usage[] = { -+ "btrfs filesystem [] []", -+ NULL -+}; -+ -+static const char * const cmd_df_usage[] = { -+ "btrfs filesystem df ", -+ "Show space usage information for a mount point", -+ NULL -+}; -+ -+static int cmd_df(int argc, char **argv) -+{ -+ struct btrfs_ioctl_space_args *sargs; -+ u64 count = 0, i; -+ int ret; -+ int fd; -+ int e; -+ char *path; -+ -+ if (check_argc_exact(argc, 2)) -+ usage(cmd_df_usage); -+ -+ path = argv[1]; -+ -+ fd = open_file_or_dir(path); -+ if (fd < 0) { -+ fprintf(stderr, "ERROR: can't access to '%s'\n", path); -+ return 12; -+ } -+ -+ sargs = malloc(sizeof(struct btrfs_ioctl_space_args)); -+ if (!sargs) -+ return -ENOMEM; -+ -+ sargs->space_slots = 0; -+ sargs->total_spaces = 0; -+ -+ ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); -+ e = errno; -+ if (ret) { -+ fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n", -+ path, strerror(e)); -+ free(sargs); -+ return ret; -+ } -+ if (!sargs->total_spaces) -+ return 0; -+ -+ count = sargs->total_spaces; -+ -+ sargs = realloc(sargs, sizeof(struct btrfs_ioctl_space_args) + -+ (count * sizeof(struct btrfs_ioctl_space_info))); -+ if (!sargs) -+ return -ENOMEM; -+ -+ sargs->space_slots = count; -+ sargs->total_spaces = 0; -+ -+ ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); -+ e = errno; -+ if (ret) { -+ fprintf(stderr, "ERROR: couldn't get space info on '%s' - %s\n", -+ path, strerror(e)); -+ close(fd); -+ free(sargs); -+ return ret; -+ } -+ -+ for (i = 0; i < sargs->total_spaces; i++) { -+ char description[80]; -+ char *total_bytes; -+ char *used_bytes; -+ int written = 0; -+ u64 flags = sargs->spaces[i].flags; -+ -+ memset(description, 0, 80); -+ -+ if (flags & BTRFS_BLOCK_GROUP_DATA) { -+ if (flags & BTRFS_BLOCK_GROUP_METADATA) { -+ snprintf(description, 14, "%s", -+ "Data+Metadata"); -+ written += 13; -+ } else { -+ snprintf(description, 5, "%s", "Data"); -+ written += 4; -+ } -+ } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) { -+ snprintf(description, 7, "%s", "System"); -+ written += 6; -+ } else if (flags & BTRFS_BLOCK_GROUP_METADATA) { -+ snprintf(description, 9, "%s", "Metadata"); -+ written += 8; -+ } -+ -+ if (flags & BTRFS_BLOCK_GROUP_RAID0) { -+ snprintf(description+written, 8, "%s", ", RAID0"); -+ written += 7; -+ } else if (flags & BTRFS_BLOCK_GROUP_RAID1) { -+ snprintf(description+written, 8, "%s", ", RAID1"); -+ written += 7; -+ } else if (flags & BTRFS_BLOCK_GROUP_DUP) { -+ snprintf(description+written, 6, "%s", ", DUP"); -+ written += 5; -+ } else if (flags & BTRFS_BLOCK_GROUP_RAID10) { -+ snprintf(description+written, 9, "%s", ", RAID10"); -+ written += 8; -+ } -+ -+ total_bytes = pretty_sizes(sargs->spaces[i].total_bytes); -+ used_bytes = pretty_sizes(sargs->spaces[i].used_bytes); -+ printf("%s: total=%s, used=%s\n", description, total_bytes, -+ used_bytes); -+ } -+ free(sargs); -+ -+ return 0; -+} -+ -+static int uuid_search(struct btrfs_fs_devices *fs_devices, char *search) -+{ -+ struct list_head *cur; -+ struct btrfs_device *device; -+ -+ list_for_each(cur, &fs_devices->devices) { -+ device = list_entry(cur, struct btrfs_device, dev_list); -+ if ((device->label && strcmp(device->label, search) == 0) || -+ strcmp(device->name, search) == 0) -+ return 1; -+ } -+ return 0; -+} -+ -+static void print_one_uuid(struct btrfs_fs_devices *fs_devices) -+{ -+ char uuidbuf[37]; -+ struct list_head *cur; -+ struct btrfs_device *device; -+ char *super_bytes_used; -+ u64 devs_found = 0; -+ u64 total; -+ -+ uuid_unparse(fs_devices->fsid, uuidbuf); -+ device = list_entry(fs_devices->devices.next, struct btrfs_device, -+ dev_list); -+ if (device->label && device->label[0]) -+ printf("Label: '%s' ", device->label); -+ else -+ printf("Label: none "); -+ -+ super_bytes_used = pretty_sizes(device->super_bytes_used); -+ -+ total = device->total_devs; -+ printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf, -+ (unsigned long long)total, super_bytes_used); -+ -+ free(super_bytes_used); -+ -+ list_for_each(cur, &fs_devices->devices) { -+ char *total_bytes; -+ char *bytes_used; -+ device = list_entry(cur, struct btrfs_device, dev_list); -+ total_bytes = pretty_sizes(device->total_bytes); -+ bytes_used = pretty_sizes(device->bytes_used); -+ printf("\tdevid %4llu size %s used %s path %s\n", -+ (unsigned long long)device->devid, -+ total_bytes, bytes_used, device->name); -+ free(total_bytes); -+ free(bytes_used); -+ devs_found++; -+ } -+ if (devs_found < total) { -+ printf("\t*** Some devices missing\n"); -+ } -+ printf("\n"); -+} -+ -+static const char * const cmd_show_usage[] = { -+ "btrfs filesystem show [--all-devices] [|