btrfs-util.c revision 90578cbd71812c0f1b6d151c708cb00bbb359053
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering This file is part of systemd.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Copyright 2014 Lennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering systemd is free software; you can redistribute it and/or modify it
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering under the terms of the GNU Lesser General Public License as published by
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering (at your option) any later version.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering systemd is distributed in the hope that it will be useful, but
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Lesser General Public License for more details.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering You should have received a copy of the GNU Lesser General Public License
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/* WARNING: Be careful with file system ioctls! When we get an fd, we
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * need to make sure it either refers to only a regular file or
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * directory, or that it is located on btrfs, before invoking any
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * btrfs ioctls. The ioctl numbers are reused by some device drivers
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering * (such as DRM), and hence might have bad effects when invoked on
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering * device nodes (that reference drivers) rather than fds to normal
return -EINVAL;
return -E2BIG;
int r, fd;
if (fd < 0)
return -errno;
return fd;
const char *fn;
return -errno;
return -errno;
const char *subvolume;
if (fd < 0)
return fd;
return -errno;
return -errno;
return -EINVAL;
return -errno;
return -errno;
if (fd < 0)
return -errno;
return -errno;
return -EINVAL;
return -errno;
return -errno;
return -EINVAL;
return -errno;
return -errno;
return -EINVAL;
return -errno;
return -ENOTTY;
return -errno;
return -errno;
return -errno;
return -ENODEV;
return -ENODEV;
return -ENODEV;
if (fd < 0)
return -errno;
return -ENOTTY;
return -errno;
if (subvol_fd < 0)
return -errno;
static void btrfs_ioctl_search_args_set(struct btrfs_ioctl_search_args *args, const struct btrfs_ioctl_search_header *h) {
assert(h);
(sh) = (const struct btrfs_ioctl_search_header*) ((uint8_t*) (sh) + sizeof(struct btrfs_ioctl_search_header) + (sh)->len))
bool found = false;
return -errno;
found = true;
goto finish;
if (!found)
return -ENODATA;
return -errno;
found_info = true;
found_limit = true;
goto finish;
return -ENODATA;
if (!found_info) {
if (!found_limit) {
return -errno;
return -EINVAL;
return -errno;
int btrfs_defrag(const char *p) {
if (fd < 0)
return -errno;
return -ENOTTY;
return -errno;
if (fd < 0)
return -errno;
return -ENOTTY;
return -errno;
if (fd < 0)
return -errno;
return -ENODEV;
return -ENOMEM;
if (r == -ENOENT)
return -ENODEV;
return -ENODEV;
if (backing_fd < 0)
return -errno;
return -errno;
return -ENODEV;
return -EINVAL;
return -ENOMEM;
if (loop_fd < 0)
return -errno;
return -EINVAL;
return -errno;
return -errno;
return -errno;
return -errno;
if (fd < 0)
return -errno;
static int subvol_remove_children(int fd, const char *subvolume, uint64_t subvol_id, bool recursive) {
bool made_writable = false;
return -errno;
return -EINVAL;
return -errno;
if (subvol_fd < 0)
return -errno;
if (subvol_id == 0) {
return -errno;
return -ENOMEM;
return -errno;
if (!made_writable) {
made_writable = true;
if (child_fd < 0)
return -errno;
return -errno;
const char *subvolume;
if (fd < 0)
return fd;
static int subvol_snapshot_children(int old_fd, int new_fd, const char *subvolume, uint64_t old_subvol_id, BtrfsSnapshotFlags flags) {
return -errno;
if (old_subvol_id == 0) {
return -errno;
return -ENOMEM;
return -errno;
return -ENOMEM;
if (old_child_fd < 0)
return -errno;
if (!np)
return -ENOMEM;
if (new_child_fd < 0)
return -errno;
if (subvolume_fd < 0) {
if (subvolume_fd < 0)
return -errno;
int k = -errno;
r = subvol_snapshot_children(old_child_fd, new_child_fd, p, sh->objectid, flags & ~BTRFS_SNAPSHOT_FALLBACK_COPY);
const char *subvolume;
return -EISDIR;
if (new_fd < 0)
return new_fd;
if (old_fd < 0)
return -errno;