+/* adds up all the used spaces as reported by the space info ioctl
+ */
+static u64 calc_used_bytes(struct btrfs_ioctl_space_args *si)
+{
+ u64 ret = 0;
+ int i;
+ for (i = 0; i < si->total_spaces; i++)
+ ret += si->spaces[i].used_bytes;
+ return ret;
+}
+
+static int print_one_fs(struct btrfs_ioctl_fs_info_args *fs_info,
+ struct btrfs_ioctl_dev_info_args *dev_info,
+ struct btrfs_ioctl_space_args *space_info,
+ char *label, char *path)
+{
+ int i;
+ int fd;
+ int missing = 0;
+ char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
+ struct btrfs_ioctl_dev_info_args *tmp_dev_info;
+ int ret;
+
+ ret = add_seen_fsid(fs_info->fsid);
+ if (ret == -EEXIST)
+ return 0;
+ else if (ret)
+ return ret;
+
+ uuid_unparse(fs_info->fsid, uuidbuf);
+ if (label && strlen(label))
+ printf("Label: '%s' ", label);
+ else
+ printf("Label: none ");
+
+ printf(" uuid: %s\n\tTotal devices %llu FS bytes used %s\n", uuidbuf,
+ fs_info->num_devices,
+ pretty_size(calc_used_bytes(space_info)));
+
+ for (i = 0; i < fs_info->num_devices; i++) {
+ tmp_dev_info = (struct btrfs_ioctl_dev_info_args *)&dev_info[i];
+
+ /* Add check for missing devices even mounted */
+ fd = open((char *)tmp_dev_info->path, O_RDONLY);
+ if (fd < 0) {
+ missing = 1;
+ continue;
+ }
+ close(fd);
+ printf("\tdevid %4llu size %s used %s path %s\n",
+ tmp_dev_info->devid,
+ pretty_size(tmp_dev_info->total_bytes),
+ pretty_size(tmp_dev_info->bytes_used),
+ tmp_dev_info->path);
+ }
+
+ if (missing)
+ printf("\t*** Some devices missing\n");
+ printf("\n");
+ return 0;
+}
+
+/* This function checks if the given input parameter is
+ * an uuid or a path
+ * return -1: some error in the given input
+ * return 0: unknow input
+ * return 1: given input is uuid
+ * return 2: given input is path
+ */
+static int check_arg_type(char *input)
+{
+ uuid_t out;
+ char path[PATH_MAX];
+
+ if (!input)
+ return -EINVAL;
+
+ if (realpath(input, path)) {
+ if (is_block_device(path) == 1)
+ return BTRFS_ARG_BLKDEV;
+
+ if (is_mount_point(path) == 1)
+ return BTRFS_ARG_MNTPOINT;
+
+ return BTRFS_ARG_UNKNOWN;
+ }
+
+ if (strlen(input) == (BTRFS_UUID_UNPARSED_SIZE - 1) &&
+ !uuid_parse(input, out))
+ return BTRFS_ARG_UUID;
+
+ return BTRFS_ARG_UNKNOWN;
+}
+
+static int btrfs_scan_kernel(void *search)
+{
+ int ret = 0, fd;
+ int found = 0;
+ FILE *f;
+ struct mntent *mnt;
+ struct btrfs_ioctl_fs_info_args fs_info_arg;
+ struct btrfs_ioctl_dev_info_args *dev_info_arg = NULL;
+ struct btrfs_ioctl_space_args *space_info_arg = NULL;
+ char label[BTRFS_LABEL_SIZE];
+
+ f = setmntent("/proc/self/mounts", "r");
+ if (f == NULL)
+ return 1;
+
+ memset(label, 0, sizeof(label));
+ while ((mnt = getmntent(f)) != NULL) {
+ if (strcmp(mnt->mnt_type, "btrfs"))
+ continue;
+ ret = get_fs_info(mnt->mnt_dir, &fs_info_arg,
+ &dev_info_arg);
+ if (ret)
+ goto out;
+
+ if (get_label_mounted(mnt->mnt_dir, label)) {
+ kfree(dev_info_arg);
+ goto out;
+ }
+ if (search && !match_search_item_kernel(fs_info_arg.fsid,
+ mnt->mnt_dir, label, search)) {
+ kfree(dev_info_arg);
+ continue;
+ }
+
+ fd = open(mnt->mnt_dir, O_RDONLY);
+ if ((fd != -1) && !get_df(fd, &space_info_arg)) {
+ print_one_fs(&fs_info_arg, dev_info_arg,
+ space_info_arg, label, mnt->mnt_dir);
+ kfree(space_info_arg);
+ memset(label, 0, sizeof(label));
+ found = 1;
+ }
+ if (fd != -1)
+ close(fd);
+ kfree(dev_info_arg);
+ }
+
+out:
+ endmntent(f);
+ return !found;
+}
+
+static int dev_to_fsid(char *dev, __u8 *fsid)
+{
+ struct btrfs_super_block *disk_super;
+ char *buf;
+ int ret;
+ int fd;
+
+ buf = malloc(4096);
+ if (!buf)
+ return -ENOMEM;
+
+ fd = open(dev, O_RDONLY);
+ if (fd < 0) {
+ ret = -errno;
+ free(buf);
+ return ret;
+ }
+
+ disk_super = (struct btrfs_super_block *)buf;
+ ret = btrfs_read_dev_super(fd, disk_super,
+ BTRFS_SUPER_INFO_OFFSET, 0);
+ if (ret)
+ goto out;
+
+ memcpy(fsid, disk_super->fsid, BTRFS_FSID_SIZE);
+ ret = 0;
+
+out:
+ close(fd);
+ free(buf);
+ return ret;
+}
+
+static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
+{
+ struct btrfs_fs_devices *cur_seed, *next_seed;
+ struct btrfs_device *device;
+
+ while (!list_empty(&fs_devices->devices)) {
+ device = list_entry(fs_devices->devices.next,
+ struct btrfs_device, dev_list);
+ list_del(&device->dev_list);
+
+ free(device->name);
+ free(device->label);
+ free(device);
+ }
+
+ /* free seed fs chain */
+ cur_seed = fs_devices->seed;
+ fs_devices->seed = NULL;
+ while (cur_seed) {
+ next_seed = cur_seed->seed;
+ free(cur_seed);
+
+ cur_seed = next_seed;
+ }
+
+ list_del(&fs_devices->list);
+ free(fs_devices);
+}
+
+static int copy_device(struct btrfs_device *dst,
+ struct btrfs_device *src)
+{
+ dst->devid = src->devid;
+ memcpy(dst->uuid, src->uuid, BTRFS_UUID_SIZE);
+ if (src->name == NULL)
+ dst->name = NULL;
+ else {
+ dst->name = strdup(src->name);
+ if (!dst->name)
+ return -ENOMEM;
+ }
+ if (src->label == NULL)
+ dst->label = NULL;
+ else {
+ dst->label = strdup(src->label);
+ if (!dst->label) {
+ free(dst->name);
+ return -ENOMEM;
+ }
+ }
+ dst->total_devs = src->total_devs;
+ dst->super_bytes_used = src->super_bytes_used;
+ dst->total_bytes = src->total_bytes;
+ dst->bytes_used = src->bytes_used;
+ dst->generation = src->generation;
+
+ return 0;
+}
+
+static int copy_fs_devices(struct btrfs_fs_devices *dst,
+ struct btrfs_fs_devices *src)
+{
+ struct btrfs_device *cur_dev, *dev_copy;
+ int ret = 0;
+
+ memcpy(dst->fsid, src->fsid, BTRFS_FSID_SIZE);
+ INIT_LIST_HEAD(&dst->devices);
+ dst->seed = NULL;
+
+ list_for_each_entry(cur_dev, &src->devices, dev_list) {
+ dev_copy = malloc(sizeof(*dev_copy));
+ if (!dev_copy) {
+ ret = -ENOMEM;
+ break;
+ }
+
+ ret = copy_device(dev_copy, cur_dev);
+ if (ret) {
+ free(dev_copy);
+ break;
+ }
+
+ list_add(&dev_copy->dev_list, &dst->devices);
+ dev_copy->fs_devices = dst;
+ }
+
+ return ret;
+}
+
+static int find_and_copy_seed(struct btrfs_fs_devices *seed,
+ struct btrfs_fs_devices *copy,
+ struct list_head *fs_uuids) {
+ struct btrfs_fs_devices *cur_fs;
+
+ list_for_each_entry(cur_fs, fs_uuids, list)
+ if (!memcmp(seed->fsid, cur_fs->fsid, BTRFS_FSID_SIZE))
+ return copy_fs_devices(copy, cur_fs);
+
+ return 1;
+}
+
+static int map_seed_devices(struct list_head *all_uuids,
+ char *search, int *found)
+{
+ struct btrfs_fs_devices *cur_fs, *cur_seed;
+ struct btrfs_fs_devices *fs_copy, *seed_copy;
+ struct btrfs_fs_devices *opened_fs;
+ struct btrfs_device *device;
+ struct btrfs_fs_info *fs_info;
+ struct list_head *fs_uuids;
+ int ret = 0;
+
+ fs_uuids = btrfs_scanned_uuids();
+
+ /*
+ * The fs_uuids list is global, and open_ctree_* will
+ * modify it, make a private copy here
+ */
+ list_for_each_entry(cur_fs, fs_uuids, list) {
+ /* don't bother handle all fs, if search target specified */
+ if (search) {
+ if (uuid_search(cur_fs, search) == 0)
+ continue;
+ *found = 1;
+ }
+
+ /* skip all fs already shown as mounted fs */
+ if (is_seen_fsid(cur_fs->fsid))
+ continue;
+
+ fs_copy = malloc(sizeof(*fs_copy));
+ if (!fs_copy) {
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ ret = copy_fs_devices(fs_copy, cur_fs);
+ if (ret) {
+ free(fs_copy);
+ goto out;
+ }
+
+ list_add(&fs_copy->list, all_uuids);
+ }
+
+ list_for_each_entry(cur_fs, all_uuids, list) {
+ device = list_first_entry(&cur_fs->devices,
+ struct btrfs_device, dev_list);
+ if (!device)
+ continue;
+ /*
+ * open_ctree_* detects seed/sprout mapping
+ */
+ fs_info = open_ctree_fs_info(device->name, 0, 0,
+ OPEN_CTREE_PARTIAL);
+ if (!fs_info)
+ continue;
+
+ /*
+ * copy the seed chain under the opened fs
+ */
+ opened_fs = fs_info->fs_devices;
+ cur_seed = cur_fs;
+ while (opened_fs->seed) {
+ seed_copy = malloc(sizeof(*seed_copy));
+ if (!seed_copy) {
+ ret = -ENOMEM;
+ goto fail_out;
+ }
+ ret = find_and_copy_seed(opened_fs->seed, seed_copy,
+ fs_uuids);
+ if (ret) {
+ free(seed_copy);
+ goto fail_out;
+ }
+
+ cur_seed->seed = seed_copy;
+
+ opened_fs = opened_fs->seed;
+ cur_seed = cur_seed->seed;
+ }
+
+ close_ctree(fs_info->chunk_root);
+ }
+
+out:
+ return ret;
+fail_out:
+ close_ctree(fs_info->chunk_root);
+ goto out;
+}
+