+#endif
+
+int btrfs_subvolid_resolve(int fd, char *path, size_t path_len, u64 subvol_id)
+{
+ if (path_len < 1)
+ return -EOVERFLOW;
+ path[0] = '\0';
+ path_len--;
+ path[path_len] = '\0';
+ return btrfs_subvolid_resolve_sub(fd, path, &path_len, subvol_id);
+}
+
+static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len,
+ u64 subvol_id)
+{
+ int ret;
+ struct btrfs_ioctl_search_args search_arg;
+ struct btrfs_ioctl_ino_lookup_args ino_lookup_arg;
+ struct btrfs_ioctl_search_header *search_header;
+ struct btrfs_root_ref *backref_item;
+
+ if (subvol_id == BTRFS_FS_TREE_OBJECTID) {
+ if (*path_len < 1)
+ return -EOVERFLOW;
+ *path = '\0';
+ (*path_len)--;
+ return 0;
+ }
+
+ memset(&search_arg, 0, sizeof(search_arg));
+ search_arg.key.tree_id = BTRFS_ROOT_TREE_OBJECTID;
+ search_arg.key.min_objectid = subvol_id;
+ search_arg.key.max_objectid = subvol_id;
+ search_arg.key.min_type = BTRFS_ROOT_BACKREF_KEY;
+ search_arg.key.max_type = BTRFS_ROOT_BACKREF_KEY;
+ search_arg.key.max_offset = (u64)-1;
+ search_arg.key.max_transid = (u64)-1;
+ search_arg.key.nr_items = 1;
+ ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_arg);
+ if (ret < 0) {
+ fprintf(stderr,
+ "ioctl(BTRFS_IOC_TREE_SEARCH, subvol_id %llu) ret=%d, error: %m\n",
+ (unsigned long long)subvol_id, ret);
+ return ret;
+ }
+
+ if (search_arg.key.nr_items < 1) {
+ fprintf(stderr,
+ "failed to lookup subvol_id %llu!\n",
+ (unsigned long long)subvol_id);
+ return -ENOENT;
+ }
+ search_header = (struct btrfs_ioctl_search_header *)search_arg.buf;
+ backref_item = (struct btrfs_root_ref *)(search_header + 1);
+ if (btrfs_search_header_offset(search_header)
+ != BTRFS_FS_TREE_OBJECTID) {
+ int sub_ret;
+
+ sub_ret = btrfs_subvolid_resolve_sub(fd, path, path_len,
+ btrfs_search_header_offset(search_header));
+ if (sub_ret)
+ return sub_ret;
+ if (*path_len < 1)
+ return -EOVERFLOW;
+ strcat(path, "/");
+ (*path_len)--;
+ }
+
+ if (btrfs_stack_root_ref_dirid(backref_item) !=
+ BTRFS_FIRST_FREE_OBJECTID) {
+ int len;
+
+ memset(&ino_lookup_arg, 0, sizeof(ino_lookup_arg));
+ ino_lookup_arg.treeid =
+ btrfs_search_header_offset(search_header);
+ ino_lookup_arg.objectid =
+ btrfs_stack_root_ref_dirid(backref_item);
+ ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_lookup_arg);
+ if (ret < 0) {
+ fprintf(stderr,
+ "ioctl(BTRFS_IOC_INO_LOOKUP) ret=%d, error: %m\n",
+ ret);
+ return ret;
+ }
+
+ len = strlen(ino_lookup_arg.name);
+ if (*path_len < len)
+ return -EOVERFLOW;
+ strcat(path, ino_lookup_arg.name);
+ (*path_len) -= len;
+ }
+
+ if (*path_len < btrfs_stack_root_ref_name_len(backref_item))
+ return -EOVERFLOW;
+ strncat(path, (char *)(backref_item + 1),
+ btrfs_stack_root_ref_name_len(backref_item));
+ (*path_len) -= btrfs_stack_root_ref_name_len(backref_item);
+ return 0;
+}
+
+#ifdef BTRFS_COMPAT_SEND_NO_UUID_TREE
+static int count_bytes(void *buf, int len, char b)
+{
+ int cnt = 0;
+ int i;
+
+ for (i = 0; i < len; i++) {
+ if (((char *)buf)[i] == b)
+ cnt++;
+ }
+ return cnt;
+}
+
+void subvol_uuid_search_add(struct subvol_uuid_search *s,
+ struct subvol_info *si)
+{
+ int cnt;
+
+ tree_insert(&s->root_id_subvols, si, subvol_search_by_root_id);
+ tree_insert(&s->path_subvols, si, subvol_search_by_path);
+
+ cnt = count_bytes(si->uuid, BTRFS_UUID_SIZE, 0);
+ if (cnt != BTRFS_UUID_SIZE)
+ tree_insert(&s->local_subvols, si, subvol_search_by_uuid);
+ cnt = count_bytes(si->received_uuid, BTRFS_UUID_SIZE, 0);
+ if (cnt != BTRFS_UUID_SIZE)
+ tree_insert(&s->received_subvols, si,
+ subvol_search_by_received_uuid);
+}