btrfs-util.c revision 21222ea5cdec65fa30a75bd5a78475459075b946
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdlib.h>
#ifdef HAVE_LINUX_BTRFS_H
#endif
#include "missing.h"
#include "util.h"
#include "path-util.h"
#include "macro.h"
#include "copy.h"
#include "selinux-util.h"
#include "smack-util.h"
#include "fileio.h"
#include "btrfs-ctree.h"
#include "btrfs-util.h"
static int validate_subvolume_name(const char *name) {
if (!filename_is_valid(name))
return -EINVAL;
return -E2BIG;
return 0;
}
int r, fd;
if (r < 0)
return r;
if (fd < 0)
return -errno;
return fd;
}
const char *fn;
int r;
r = validate_subvolume_name(fn);
if (r < 0)
return r;
return 0;
}
int btrfs_is_filesystem(int fd) {
return -errno;
}
int btrfs_is_subvol(int fd) {
/* On btrfs subvolumes always have the inode 256 */
return -errno;
return 0;
return btrfs_is_filesystem(fd);
}
int btrfs_subvol_make(const char *path) {
struct btrfs_ioctl_vol_args args = {};
const char *subvolume;
int r;
if (r < 0)
return r;
if (fd < 0)
return fd;
return -errno;
return 0;
}
int btrfs_subvol_make_label(const char *path) {
int r;
if (r < 0)
return r;
r = btrfs_subvol_make(path);
if (r < 0)
return r;
return mac_smack_fix(path, false, false);
}
int btrfs_subvol_set_read_only_fd(int fd, bool b) {
return -errno;
return -EINVAL;
return -errno;
if (b)
else
return 0;
return -errno;
return 0;
}
int btrfs_subvol_set_read_only(const char *path, bool b) {
if (fd < 0)
return -errno;
return btrfs_subvol_set_read_only_fd(fd, b);
}
int btrfs_subvol_get_read_only_fd(int fd) {
return -errno;
return !!(flags & BTRFS_SUBVOL_RDONLY);
}
int r;
if (r < 0)
return -errno;
return 0;
}
struct btrfs_ioctl_clone_range_args args = {
.src_offset = in_offset,
.src_length = sz,
};
int r;
if (r < 0)
return -errno;
return 0;
}
struct btrfs_ioctl_fs_info_args fsi = {};
return -errno;
/* We won't do this for btrfs RAID */
return 0;
struct btrfs_ioctl_dev_info_args di = {
};
continue;
return -errno;
}
return -errno;
return -ENODEV;
return -ENODEV;
return 1;
}
return -ENODEV;
}
if (fd < 0)
return -errno;
}
struct btrfs_ioctl_ino_lookup_args args = {
};
return -errno;
return 0;
}
/* the objectid, type, offset together make up the btrfs key,
* which is considered a single 136byte integer when
* comparing. This call increases the counter by one, dealing
* with the overflow between the overflows */
return true;
}
return true;
}
return true;
}
return 0;
}
static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args *args, const struct btrfs_ioctl_search_header *h) {
assert(h);
}
/* Compare min and max */
return -1;
return 1;
return -1;
return 1;
return -1;
return 1;
return 0;
}
for ((i) = 0, \
(i)++, \
(sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
#define BTRFS_IOCTL_SEARCH_HEADER_BODY(sh) \
struct btrfs_ioctl_search_args args = {
/* Tree of tree roots */
/* Look precisely for the subvolume items */
.key.min_offset = 0,
/* No restrictions on the other components */
.key.min_transid = 0,
};
bool found = false;
int r;
if (r < 0)
return r;
while (btrfs_ioctl_search_args_compare(&args) <= 0) {
const struct btrfs_ioctl_search_header *sh;
unsigned i;
return -errno;
break;
const struct btrfs_root_item *ri;
/* Make sure we start the next search at least from this entry */
continue;
continue;
/* Older versions of the struct lacked the otime setting */
continue;
found = true;
goto finish;
}
/* Increase search key by one, to read the next item, if we can. */
if (!btrfs_ioctl_search_args_inc(&args))
break;
}
if (!found)
return -ENODATA;
return 0;
}
struct btrfs_ioctl_search_args args = {
/* Tree of quota items */
/* The object ID is always 0 */
.key.min_objectid = 0,
.key.max_objectid = 0,
/* Look precisely for the quota items */
/* No restrictions on the other components */
.key.min_transid = 0,
};
bool found_info = false, found_limit = false;
int r;
if (r < 0)
return r;
while (btrfs_ioctl_search_args_compare(&args) <= 0) {
const struct btrfs_ioctl_search_header *sh;
unsigned i;
return -errno;
break;
/* Make sure we start the next search at least from this entry */
continue;
continue;
found_info = true;
if (ret->referenced_max == 0)
if (ret->exclusive_max == 0)
found_limit = true;
}
if (found_info && found_limit)
goto finish;
}
/* Increase search key by one, to read the next item, if we can. */
if (!btrfs_ioctl_search_args_inc(&args))
break;
}
if (!found_limit && !found_info)
return -ENODATA;
if (!found_info) {
}
if (!found_limit) {
}
return 0;
}
int btrfs_defrag_fd(int fd) {
return -errno;
return 0;
}
int btrfs_defrag(const char *p) {
if (fd < 0)
return -errno;
return btrfs_defrag_fd(fd);
}
int btrfs_quota_enable_fd(int fd, bool b) {
struct btrfs_ioctl_quota_ctl_args args = {
};
return -errno;
return 0;
}
int btrfs_quota_enable(const char *path, bool b) {
if (fd < 0)
return -errno;
return btrfs_quota_enable_fd(fd, b);
}
struct btrfs_ioctl_qgroup_limit_args args = {
};
return -errno;
return 0;
}
if (fd < 0)
return -errno;
}
struct btrfs_ioctl_vol_args args = {};
int r;
/* btrfs cannot handle file systems < 16M, hence use this as minimum */
if (r < 0)
return r;
if (r == 0)
return -ENODEV;
return -ENOMEM;
r = read_one_line_file(p, &backing);
if (r == -ENOENT)
return -ENODEV;
if (r < 0)
return r;
return -ENODEV;
if (backing_fd < 0)
return -errno;
return -errno;
return -ENODEV;
return 0;
return -EINVAL;
return -ENOMEM;
if (loop_fd < 0)
return -errno;
return -EINVAL;
/* Decrease size: first decrease btrfs size, then shorten loopback */
return -errno;
}
return -errno;
return -errno;
/* Increase size: first enlarge loopback, then increase btrfs size */
return -errno;
}
/* Make sure the free disk space is correctly updated for both file systems */
(void) fsync(backing_fd);
return 1;
}
if (fd < 0)
return -errno;
}
static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol_id, bool recursive) {
struct btrfs_ioctl_search_args args = {
.key.min_transid = 0,
};
struct btrfs_ioctl_vol_args vol_args = {};
int r;
/* First, try to remove the subvolume. If it happens to be
* already empty, this will just work. */
return 0;
return -errno;
/* OK, the subvolume is not empty, let's look for child
* subvolumes, and remove them, first */
if (subvol_fd < 0)
return -errno;
if (subvol_id == 0) {
if (r < 0)
return r;
}
while (btrfs_ioctl_search_args_compare(&args) <= 0) {
const struct btrfs_ioctl_search_header *sh;
unsigned i;
return -errno;
break;
_cleanup_free_ char *p = NULL;
const struct btrfs_root_ref *ref;
struct btrfs_ioctl_ino_lookup_args ino_args;
continue;
continue;
if (!p)
return -ENOMEM;
return -errno;
/* Subvolume is in the top-level
* directory of the subvolume. */
else {
/* Subvolume is somewhere further down,
* hence we need to open the
* containing directory first */
if (child_fd < 0)
return -errno;
}
if (r < 0)
return r;
}
/* Increase search key by one, to read the next item, if we can. */
if (!btrfs_ioctl_search_args_inc(&args))
break;
}
/* OK, the child subvolumes should all be gone now, let's try
* again to remove the subvolume */
return -errno;
return 0;
}
const char *subvolume;
int r;
if (r < 0)
return r;
if (fd < 0)
return fd;
}
}
static int subvol_snapshot_children(int old_fd, int new_fd, const char *subvolume, uint64_t subvol_id, BtrfsSnapshotFlags flags) {
struct btrfs_ioctl_search_args args = {
.key.min_transid = 0,
};
struct btrfs_ioctl_vol_args_v2 vol_args = {
};
int r;
return -errno;
if (!(flags & BTRFS_SNAPSHOT_RECURSIVE))
return 0;
if (subvol_id == 0) {
if (r < 0)
return r;
}
while (btrfs_ioctl_search_args_compare(&args) <= 0) {
const struct btrfs_ioctl_search_header *sh;
unsigned i;
return -errno;
break;
struct btrfs_ioctl_ino_lookup_args ino_args;
const struct btrfs_root_ref *ref;
continue;
continue;
if (!p)
return -ENOMEM;
return -errno;
/* The kernel returns an empty name if the
* subvolume is in the top-level directory,
* and otherwise appends a slash, so that we
* can just concatenate easily here, without
* adding a slash. */
if (!c)
return -ENOMEM;
if (old_child_fd < 0)
return -errno;
if (!np)
return -ENOMEM;
if (new_child_fd < 0)
return -errno;
/* When btrfs clones the subvolumes, child
* subvolumes appear as directories. Remove
* them, so that we can create a new snapshot
* in their place */
return -errno;
r = subvol_snapshot_children(old_child_fd, new_child_fd, p, sh->objectid, flags & ~BTRFS_SNAPSHOT_FALLBACK_COPY);
if (r < 0)
return r;
}
/* Increase search key by one, to read the next item, if we can. */
if (!btrfs_ioctl_search_args_inc(&args))
break;
}
return 0;
}
const char *subvolume;
int r;
r = btrfs_is_subvol(old_fd);
if (r < 0)
return r;
if (r == 0) {
if (!(flags & BTRFS_SNAPSHOT_FALLBACK_COPY))
return -EISDIR;
r = btrfs_subvol_make(new_path);
if (r < 0)
return r;
if (r < 0) {
btrfs_subvol_remove(new_path, false);
return r;
}
if (flags & BTRFS_SNAPSHOT_READ_ONLY) {
r = btrfs_subvol_set_read_only(new_path, true);
if (r < 0) {
btrfs_subvol_remove(new_path, false);
return r;
}
}
return 0;
}
if (r < 0)
return r;
if (new_fd < 0)
return new_fd;
}
if (old_fd < 0)
return -errno;
}