+static const char * const cmd_subvol_sync_usage[] = {
+ "btrfs subvolume sync <path> [<subvol-id>...]",
+ "Wait until given subvolume(s) are completely removed from the filesystem.",
+ "Wait until given subvolume(s) are completely removed from the filesystem",
+ "after deletion.",
+ "If no subvolume id is given, wait until all current deletion requests",
+ "are completed, but do not wait for subvolumes deleted meanwhile.",
+ "The status of subvolume ids is checked periodically.",
+ "",
+ "-s <N> sleep N seconds between checks (default: 1)",
+ NULL
+};
+
+static int cmd_subvol_sync(int argc, char **argv)
+{
+ int fd = -1;
+ int ret = 1;
+ DIR *dirstream = NULL;
+ uint64_t *ids = NULL;
+ size_t id_count, i;
+ int sleep_interval = 1;
+ enum btrfs_util_error err;
+
+ while (1) {
+ int c = getopt(argc, argv, "s:");
+
+ if (c < 0)
+ break;
+
+ switch (c) {
+ case 's':
+ sleep_interval = atoi(optarg);
+ if (sleep_interval < 1) {
+ error("invalid sleep interval %s", optarg);
+ ret = 1;
+ goto out;
+ }
+ break;
+ default:
+ usage(cmd_subvol_sync_usage);
+ }
+ }
+
+ if (check_argc_min(argc - optind, 1))
+ usage(cmd_subvol_sync_usage);
+
+ fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+ if (fd < 0) {
+ ret = 1;
+ goto out;
+ }
+ optind++;
+
+ id_count = argc - optind;
+ if (!id_count) {
+ err = btrfs_util_deleted_subvolumes_fd(fd, &ids, &id_count);
+ if (err) {
+ error_btrfs_util(err);
+ ret = 1;
+ goto out;
+ }
+ if (id_count == 0) {
+ ret = 0;
+ goto out;
+ }
+ } else {
+ ids = malloc(id_count * sizeof(uint64_t));
+ if (!ids) {
+ error("not enough memory");
+ ret = 1;
+ goto out;
+ }
+
+ for (i = 0; i < id_count; i++) {
+ u64 id;
+ const char *arg;
+
+ arg = argv[optind + i];
+ errno = 0;
+ id = strtoull(arg, NULL, 10);
+ if (errno) {
+ error("unrecognized subvolume id %s", arg);
+ ret = 1;
+ goto out;
+ }
+ if (id < BTRFS_FIRST_FREE_OBJECTID ||
+ id > BTRFS_LAST_FREE_OBJECTID) {
+ error("subvolume id %s out of range", arg);
+ ret = 1;
+ goto out;
+ }
+ ids[i] = id;
+ }
+ }
+
+ ret = wait_for_subvolume_cleaning(fd, id_count, ids, sleep_interval);
+
+out:
+ free(ids);
+ close_file_or_dir(fd, dirstream);
+
+ return !!ret;