btrfs-util.c revision 26166c88e0b47b83972f32b5057ecbffe06bf904
/*-*- 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_snapshot(int fd) {
/* On btrfs subvolumes always have the inode 256 */
return -errno;
return 0;
return -errno;
}
int btrfs_subvol_snapshot(const char *old_path, const char *new_path, bool read_only, bool fallback_copy) {
struct btrfs_ioctl_vol_args_v2 args = {
};
const char *subvolume;
int r;
if (old_fd < 0)
return -errno;
r = btrfs_is_snapshot(old_fd);
if (r < 0)
return r;
if (r == 0) {
if (fallback_copy) {
r = btrfs_subvol_make(new_path);
if (r < 0)
return r;
if (r < 0) {
return r;
}
if (read_only) {
r = btrfs_subvol_set_read_only(new_path, true);
if (r < 0) {
return r;
}
}
return 0;
}
return -EISDIR;
}
if (r < 0)
return r;
if (new_fd < 0)
return new_fd;
return -errno;
return 0;
}
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_remove(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_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->referred_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;
}