libbtrfsutils: add python-devel detection
[platform/upstream/btrfs-progs.git] / libbtrfsutil / subvolume.c
index 4f71cb0..d972828 100644 (file)
@@ -838,6 +838,275 @@ out_iter:
        return err;
 }
 
+static enum btrfs_util_error snapshot_subvolume_children(int fd, int parent_fd,
+                                                        const char *name,
+                                                        uint64_t *async_transid)
+{
+       struct btrfs_util_subvolume_iterator *iter;
+       enum btrfs_util_error err;
+       int dstfd;
+
+       dstfd = openat(parent_fd, name, O_RDONLY);
+       if (dstfd == -1)
+               return BTRFS_UTIL_ERROR_OPEN_FAILED;
+
+       err = btrfs_util_create_subvolume_iterator_fd(fd, 0, 0, &iter);
+       if (err)
+               goto out;
+
+       for (;;) {
+               char child_name[BTRFS_SUBVOL_NAME_MAX + 1];
+               char *child_path;
+               int child_fd, new_parent_fd;
+               uint64_t tmp_transid;
+
+               err = btrfs_util_subvolume_iterator_next(iter, &child_path,
+                                                        NULL);
+               if (err) {
+                       if (err == BTRFS_UTIL_ERROR_STOP_ITERATION)
+                               err = BTRFS_UTIL_OK;
+                       break;
+               }
+
+               /* Remove the placeholder directory. */
+               if (unlinkat(dstfd, child_path, AT_REMOVEDIR) == -1) {
+                       free(child_path);
+                       err = BTRFS_UTIL_ERROR_RMDIR_FAILED;
+                       break;
+               }
+
+               child_fd = openat(fd, child_path, O_RDONLY);
+               if (child_fd == -1) {
+                       free(child_path);
+                       err = BTRFS_UTIL_ERROR_OPEN_FAILED;
+                       break;
+               }
+
+               err = openat_parent_and_name(dstfd, child_path, child_name,
+                                            sizeof(child_name),
+                                            &new_parent_fd);
+               free(child_path);
+               if (err) {
+                       SAVE_ERRNO_AND_CLOSE(child_fd);
+                       break;
+               }
+
+               err = btrfs_util_create_snapshot_fd2(child_fd, new_parent_fd,
+                                                    child_name, 0,
+                                                    async_transid ? &tmp_transid : NULL,
+                                                    NULL);
+               SAVE_ERRNO_AND_CLOSE(child_fd);
+               SAVE_ERRNO_AND_CLOSE(new_parent_fd);
+               if (err)
+                       break;
+               if (async_transid && tmp_transid > *async_transid)
+                       *async_transid = tmp_transid;
+       }
+
+       btrfs_util_destroy_subvolume_iterator(iter);
+out:
+       SAVE_ERRNO_AND_CLOSE(dstfd);
+       return err;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_create_snapshot(const char *source,
+                                                       const char *path,
+                                                       int flags,
+                                                       uint64_t *async_transid,
+                                                       struct btrfs_util_qgroup_inherit *qgroup_inherit)
+{
+       enum btrfs_util_error err;
+       int fd;
+
+       fd = open(source, O_RDONLY);
+       if (fd == -1)
+               return BTRFS_UTIL_ERROR_OPEN_FAILED;
+
+       err = btrfs_util_create_snapshot_fd(fd, path, flags, async_transid,
+                                           qgroup_inherit);
+       SAVE_ERRNO_AND_CLOSE(fd);
+       return err;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_create_snapshot_fd(int fd,
+                                                          const char *path,
+                                                          int flags,
+                                                          uint64_t *async_transid,
+                                                          struct btrfs_util_qgroup_inherit *qgroup_inherit)
+{
+       char name[BTRFS_SUBVOL_NAME_MAX + 1];
+       enum btrfs_util_error err;
+       int parent_fd;
+
+       err = openat_parent_and_name(AT_FDCWD, path, name, sizeof(name),
+                                    &parent_fd);
+       if (err)
+               return err;
+
+       err = btrfs_util_create_snapshot_fd2(fd, parent_fd, name, flags,
+                                            async_transid, qgroup_inherit);
+       SAVE_ERRNO_AND_CLOSE(parent_fd);
+       return err;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_create_snapshot_fd2(int fd,
+                                                           int parent_fd,
+                                                           const char *name,
+                                                           int flags,
+                                                           uint64_t *async_transid,
+                                                           struct btrfs_util_qgroup_inherit *qgroup_inherit)
+{
+       struct btrfs_ioctl_vol_args_v2 args = {.fd = fd};
+       enum btrfs_util_error err;
+       size_t len;
+       int ret;
+
+       if ((flags & ~BTRFS_UTIL_CREATE_SNAPSHOT_MASK) ||
+           ((flags & BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY) &&
+            (flags & BTRFS_UTIL_CREATE_SNAPSHOT_RECURSIVE))) {
+               errno = EINVAL;
+               return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
+       }
+
+       if (flags & BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY)
+               args.flags |= BTRFS_SUBVOL_RDONLY;
+       if (async_transid)
+               args.flags |= BTRFS_SUBVOL_CREATE_ASYNC;
+       if (qgroup_inherit) {
+               args.flags |= BTRFS_SUBVOL_QGROUP_INHERIT;
+               args.qgroup_inherit = (struct btrfs_qgroup_inherit *)qgroup_inherit;
+               args.size = (sizeof(*args.qgroup_inherit) +
+                            args.qgroup_inherit->num_qgroups *
+                            sizeof(args.qgroup_inherit->qgroups[0]));
+       }
+
+       len = strlen(name);
+       if (len >= sizeof(args.name)) {
+               errno = ENAMETOOLONG;
+               return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
+       }
+       memcpy(args.name, name, len);
+       args.name[len] = '\0';
+
+       ret = ioctl(parent_fd, BTRFS_IOC_SNAP_CREATE_V2, &args);
+       if (ret == -1)
+               return BTRFS_UTIL_ERROR_SUBVOL_CREATE_FAILED;
+
+       if (async_transid)
+               *async_transid = args.transid;
+
+       if (flags & BTRFS_UTIL_CREATE_SNAPSHOT_RECURSIVE) {
+               err = snapshot_subvolume_children(fd, parent_fd, name,
+                                                 async_transid);
+               if (err)
+                       return err;
+       }
+
+       return BTRFS_UTIL_OK;
+}
+
+static enum btrfs_util_error delete_subvolume_children(int parent_fd,
+                                                      const char *name)
+{
+       struct btrfs_util_subvolume_iterator *iter;
+       enum btrfs_util_error err;
+       int fd;
+
+       fd = openat(parent_fd, name, O_RDONLY);
+       if (fd == -1)
+               return BTRFS_UTIL_ERROR_OPEN_FAILED;
+
+       err = btrfs_util_create_subvolume_iterator_fd(fd, 0,
+                                                     BTRFS_UTIL_SUBVOLUME_ITERATOR_POST_ORDER,
+                                                     &iter);
+       if (err)
+               goto out;
+
+       for (;;) {
+               char child_name[BTRFS_PATH_NAME_MAX + 1];
+               char *child_path;
+               int child_parent_fd;
+
+               err = btrfs_util_subvolume_iterator_next(iter, &child_path,
+                                                        NULL);
+               if (err) {
+                       if (err == BTRFS_UTIL_ERROR_STOP_ITERATION)
+                               err = BTRFS_UTIL_OK;
+                       break;
+               }
+
+               err = openat_parent_and_name(fd, child_path, child_name,
+                                            sizeof(child_name),
+                                            &child_parent_fd);
+               free(child_path);
+               if (err)
+                       break;
+
+               err = btrfs_util_delete_subvolume_fd(child_parent_fd,
+                                                    child_name, 0);
+               SAVE_ERRNO_AND_CLOSE(child_parent_fd);
+               if (err)
+                       break;
+       }
+
+       btrfs_util_destroy_subvolume_iterator(iter);
+out:
+       SAVE_ERRNO_AND_CLOSE(fd);
+       return err;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_delete_subvolume(const char *path,
+                                                        int flags)
+{
+       char name[BTRFS_PATH_NAME_MAX + 1];
+       enum btrfs_util_error err;
+       int parent_fd;
+
+       err = openat_parent_and_name(AT_FDCWD, path, name, sizeof(name),
+                                    &parent_fd);
+       if (err)
+               return err;
+
+       err = btrfs_util_delete_subvolume_fd(parent_fd, name, flags);
+       SAVE_ERRNO_AND_CLOSE(parent_fd);
+       return err;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_delete_subvolume_fd(int parent_fd,
+                                                           const char *name,
+                                                           int flags)
+{
+       struct btrfs_ioctl_vol_args args = {};
+       enum btrfs_util_error err;
+       size_t len;
+       int ret;
+
+       if (flags & ~BTRFS_UTIL_DELETE_SUBVOLUME_MASK) {
+               errno = EINVAL;
+               return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
+       }
+
+       if (flags & BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE) {
+               err = delete_subvolume_children(parent_fd, name);
+               if (err)
+                       return err;
+       }
+
+       len = strlen(name);
+       if (len >= sizeof(args.name)) {
+               errno = ENAMETOOLONG;
+               return BTRFS_UTIL_ERROR_INVALID_ARGUMENT;
+       }
+       memcpy(args.name, name, len);
+       args.name[len] = '\0';
+
+       ret = ioctl(parent_fd, BTRFS_IOC_SNAP_DESTROY, &args);
+       if (ret == -1)
+               return BTRFS_UTIL_ERROR_SNAP_DESTROY_FAILED;
+
+       return BTRFS_UTIL_OK;
+}
+
 PUBLIC void btrfs_util_destroy_subvolume_iterator(struct btrfs_util_subvolume_iterator *iter)
 {
        if (iter) {
@@ -1010,3 +1279,92 @@ PUBLIC enum btrfs_util_error btrfs_util_subvolume_iterator_next_info(struct btrf
 
        return btrfs_util_subvolume_info_fd(iter->fd, id, subvol);
 }
+
+PUBLIC enum btrfs_util_error btrfs_util_deleted_subvolumes(const char *path,
+                                                          uint64_t **ids,
+                                                          size_t *n)
+{
+       enum btrfs_util_error err;
+       int fd;
+
+       fd = open(path, O_RDONLY);
+       if (fd == -1)
+               return BTRFS_UTIL_ERROR_OPEN_FAILED;
+
+       err = btrfs_util_deleted_subvolumes_fd(fd, ids, n);
+       SAVE_ERRNO_AND_CLOSE(fd);
+       return err;
+}
+
+PUBLIC enum btrfs_util_error btrfs_util_deleted_subvolumes_fd(int fd,
+                                                             uint64_t **ids,
+                                                             size_t *n)
+{
+       size_t capacity = 0;
+       struct btrfs_ioctl_search_args search = {
+               .key = {
+                       .tree_id = BTRFS_ROOT_TREE_OBJECTID,
+                       .min_objectid = BTRFS_ORPHAN_OBJECTID,
+                       .max_objectid = BTRFS_ORPHAN_OBJECTID,
+                       .min_type = BTRFS_ORPHAN_ITEM_KEY,
+                       .max_type = BTRFS_ORPHAN_ITEM_KEY,
+                       .min_offset = 0,
+                       .max_offset = UINT64_MAX,
+                       .min_transid = 0,
+                       .max_transid = UINT64_MAX,
+                       .nr_items = 0,
+               },
+       };
+       enum btrfs_util_error err;
+       size_t items_pos = 0, buf_off = 0;
+       int ret;
+
+       *ids = NULL;
+       *n = 0;
+       for (;;) {
+               const struct btrfs_ioctl_search_header *header;
+
+               if (items_pos >= search.key.nr_items) {
+                       search.key.nr_items = 4096;
+                       ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search);
+                       if (ret == -1) {
+                               err = BTRFS_UTIL_ERROR_SEARCH_FAILED;
+                               goto out;
+                       }
+                       items_pos = 0;
+                       buf_off = 0;
+
+                       if (search.key.nr_items == 0)
+                               break;
+               }
+
+               header = (struct btrfs_ioctl_search_header *)(search.buf + buf_off);
+               if (*n >= capacity) {
+                       size_t new_capacity = capacity ? capacity * 2 : 1;
+                       uint64_t *new_ids;
+
+                       new_ids = reallocarray(*ids, new_capacity,
+                                              sizeof(**ids));
+                       if (!new_ids)
+                               return BTRFS_UTIL_ERROR_NO_MEMORY;
+
+                       *ids = new_ids;
+                       capacity = new_capacity;
+               }
+
+               (*ids)[(*n)++] = header->offset;
+
+               items_pos++;
+               buf_off += sizeof(*header) + header->len;
+               search.key.min_offset = header->offset + 1;
+       }
+
+       err = BTRFS_UTIL_OK;
+out:
+       if (err) {
+               free(*ids);
+               *ids = NULL;
+               *n = 0;
+       }
+       return err;
+}