+ /* 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 has_seed_devices(struct btrfs_fs_devices *fs_devices)
+{
+ struct btrfs_device *device;
+ int dev_cnt_total, dev_cnt = 0;
+
+ device = list_first_entry(&fs_devices->devices, struct btrfs_device,
+ dev_list);