-static int is_subvolume_cleaned(int fd, u64 subvolid)
-{
- int ret;
- struct btrfs_ioctl_search_args args;
- struct btrfs_ioctl_search_key *sk = &args.key;
-
- sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
- sk->min_objectid = subvolid;
- sk->max_objectid = subvolid;
- sk->min_type = BTRFS_ROOT_ITEM_KEY;
- sk->max_type = BTRFS_ROOT_ITEM_KEY;
- sk->min_offset = 0;
- sk->max_offset = (u64)-1;
- sk->min_transid = 0;
- sk->max_transid = (u64)-1;
- sk->nr_items = 1;
-
- ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
- if (ret < 0)
- return -errno;
-
- if (sk->nr_items == 0)
- return 1;
-
- return 0;
-}
-
-/*
- * If we're looking for any dead subvolume, take a shortcut and look
- * for any ORPHAN_ITEMs in the tree root
- */
-static int fs_has_dead_subvolumes(int fd)
-{
- int ret;
- struct btrfs_ioctl_search_args args;
- struct btrfs_ioctl_search_key *sk = &args.key;
- struct btrfs_ioctl_search_header sh;
- u64 min_subvolid = 0;
-
-again:
- sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
- sk->min_objectid = BTRFS_ORPHAN_OBJECTID;
- sk->max_objectid = BTRFS_ORPHAN_OBJECTID;
- sk->min_type = BTRFS_ORPHAN_ITEM_KEY;
- sk->max_type = BTRFS_ORPHAN_ITEM_KEY;
- sk->min_offset = min_subvolid;
- sk->max_offset = (u64)-1;
- sk->min_transid = 0;
- sk->max_transid = (u64)-1;
- sk->nr_items = 1;
-
- ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
- if (ret < 0)
- return -errno;
-
- if (!sk->nr_items)
- return 0;
-
- memcpy(&sh, args.buf, sizeof(sh));
- min_subvolid = sh.offset;
-
- /*
- * Verify that the root item is really there and we haven't hit
- * a stale orphan
- */
- sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
- sk->min_objectid = min_subvolid;
- sk->max_objectid = min_subvolid;
- sk->min_type = BTRFS_ROOT_ITEM_KEY;
- sk->max_type = BTRFS_ROOT_ITEM_KEY;
- sk->min_offset = 0;
- sk->max_offset = (u64)-1;
- sk->min_transid = 0;
- sk->max_transid = (u64)-1;
- sk->nr_items = 1;
-
- ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
- if (ret < 0)
- return -errno;
-
- /*
- * Stale orphan, try the next one
- */
- if (!sk->nr_items) {
- min_subvolid++;
- goto again;
- }
-
- return 1;
-}
-