summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/btrfs-ctree.h68
-rw-r--r--src/shared/btrfs-util.c82
-rw-r--r--src/shared/btrfs-util.h19
-rw-r--r--src/shared/missing.h8
-rw-r--r--src/shared/util.c55
-rw-r--r--src/shared/util.h3
6 files changed, 218 insertions, 17 deletions
diff --git a/src/shared/btrfs-ctree.h b/src/shared/btrfs-ctree.h
new file mode 100644
index 0000000000..45b94cdbf6
--- /dev/null
+++ b/src/shared/btrfs-ctree.h
@@ -0,0 +1,68 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+#pragma once
+
+#include "sparse-endian.h"
+
+/* Stolen from btrfs' ctree.h */
+
+struct btrfs_timespec {
+ le64_t sec;
+ le32_t nsec;
+} _packed_;
+
+struct btrfs_disk_key {
+ le64_t objectid;
+ uint8_t type;
+ le64_t offset;
+} _packed_;
+
+struct btrfs_inode_item {
+ le64_t generation;
+ le64_t transid;
+ le64_t size;
+ le64_t nbytes;
+ le64_t block_group;
+ le32_t nlink;
+ le32_t uid;
+ le32_t gid;
+ le32_t mode;
+ le64_t rdev;
+ le64_t flags;
+ le64_t sequence;
+ le64_t reserved[4];
+ struct btrfs_timespec atime;
+ struct btrfs_timespec ctime;
+ struct btrfs_timespec mtime;
+ struct btrfs_timespec otime;
+} _packed_;
+
+struct btrfs_root_item {
+ struct btrfs_inode_item inode;
+ le64_t generation;
+ le64_t root_dirid;
+ le64_t bytenr;
+ le64_t byte_limit;
+ le64_t bytes_used;
+ le64_t last_snapshot;
+ le64_t flags;
+ le32_t refs;
+ struct btrfs_disk_key drop_progress;
+ uint8_t drop_level;
+ uint8_t level;
+ le64_t generation_v2;
+ uint8_t uuid[BTRFS_UUID_SIZE];
+ uint8_t parent_uuid[BTRFS_UUID_SIZE];
+ uint8_t received_uuid[BTRFS_UUID_SIZE];
+ le64_t ctransid;
+ le64_t otransid;
+ le64_t stransid;
+ le64_t rtransid;
+ struct btrfs_timespec ctime;
+ struct btrfs_timespec otime;
+ struct btrfs_timespec stime;
+ struct btrfs_timespec rtime;
+ le64_t reserved[8];
+} _packed_;
+
+#define BTRFS_ROOT_SUBVOL_RDONLY (1ULL << 0)
diff --git a/src/shared/btrfs-util.c b/src/shared/btrfs-util.c
index d685b3ecbd..84c81106fa 100644
--- a/src/shared/btrfs-util.c
+++ b/src/shared/btrfs-util.c
@@ -33,6 +33,7 @@
#include "macro.h"
#include "strv.h"
#include "copy.h"
+#include "btrfs-ctree.h"
#include "btrfs-util.h"
static int validate_subvolume_name(const char *name) {
@@ -129,7 +130,7 @@ int btrfs_subvol_snapshot(const char *old_path, const char *new_path, bool read_
}
if (read_only) {
- r = btrfs_subvol_read_only(new_path, true);
+ r = btrfs_subvol_set_read_only(new_path, true);
if (r < 0) {
btrfs_subvol_remove(new_path);
return r;
@@ -207,7 +208,7 @@ int btrfs_subvol_remove(const char *path) {
return 0;
}
-int btrfs_subvol_read_only(const char *path, bool b) {
+int btrfs_subvol_set_read_only(const char *path, bool b) {
_cleanup_close_ int fd = -1;
uint64_t flags, nflags;
@@ -232,7 +233,7 @@ int btrfs_subvol_read_only(const char *path, bool b) {
return 0;
}
-int btrfs_subvol_is_read_only_fd(int fd) {
+int btrfs_subvol_get_read_only_fd(int fd) {
uint64_t flags;
if (ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags) < 0)
@@ -301,3 +302,78 @@ int btrfs_get_block_device(const char *path, dev_t *dev) {
return -ENODEV;
}
+
+int btrfs_subvol_get_id_fd(int fd, uint64_t *ret) {
+ struct btrfs_ioctl_ino_lookup_args args = {
+ .objectid = BTRFS_FIRST_FREE_OBJECTID
+ };
+
+ assert(fd >= 0);
+ assert(ret);
+
+ if (ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args) < 0)
+ return -errno;
+
+ *ret = args.treeid;
+ return 0;
+}
+
+int btrfs_subvol_get_info_fd(int fd, BtrfsSubvolInfo *ret) {
+ struct btrfs_ioctl_search_args args = {
+ /* Tree of tree roots */
+ .key.tree_id = 1,
+
+ /* Look precisely for the subvolume items */
+ .key.min_type = BTRFS_ROOT_ITEM_KEY,
+ .key.max_type = BTRFS_ROOT_ITEM_KEY,
+
+ /* No restrictions on the other components */
+ .key.min_offset = 0,
+ .key.max_offset = (uint64_t) -1,
+ .key.min_transid = 0,
+ .key.max_transid = (uint64_t) -1,
+
+ /* Some large value */
+ .key.nr_items = 2,
+ };
+
+ struct btrfs_ioctl_search_header *sh;
+ struct btrfs_root_item *ri;
+ uint64_t subvol_id;
+ int r;
+
+ assert(fd >= 0);
+ assert(ret);
+
+ r = btrfs_subvol_get_id_fd(fd, &subvol_id);
+ if (r < 0)
+ return r;
+
+ args.key.min_objectid = args.key.max_objectid = subvol_id;
+ if (ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args) < 0)
+ return -errno;
+
+ if (args.key.nr_items != 1)
+ return -EIO;
+
+ sh = (struct btrfs_ioctl_search_header*) args.buf;
+ assert(sh->type == BTRFS_ROOT_ITEM_KEY);
+ assert(sh->objectid == subvol_id);
+
+ if (sh->len < offsetof(struct btrfs_root_item, otime) + sizeof(struct btrfs_timespec))
+ return -ENOTSUP;
+
+ ri = (struct btrfs_root_item *)(args.buf + sizeof(struct btrfs_ioctl_search_header));
+
+ ret->otime = (usec_t) le64toh(ri->otime.sec) * USEC_PER_SEC +
+ (usec_t) le32toh(ri->otime.nsec) / NSEC_PER_USEC;
+
+ ret->subvol_id = subvol_id;
+ ret->read_only = !!(le64toh(ri->flags) & BTRFS_ROOT_SUBVOL_RDONLY);
+
+ assert_cc(sizeof(ri->uuid) == sizeof(ret->uuid));
+ memcpy(&ret->uuid, ri->uuid, sizeof(ret->uuid));
+ memcpy(&ret->parent_uuid, ri->parent_uuid, sizeof(ret->parent_uuid));
+
+ return 0;
+}
diff --git a/src/shared/btrfs-util.h b/src/shared/btrfs-util.h
index c8f798b6a6..f51f37a659 100644
--- a/src/shared/btrfs-util.h
+++ b/src/shared/btrfs-util.h
@@ -22,13 +22,28 @@
#include <stdbool.h>
#include <sys/types.h>
+#include "time-util.h"
+
+typedef struct BtrfsSubvolInfo {
+ uint64_t subvol_id;
+ usec_t otime;
+
+ sd_id128_t uuid;
+ sd_id128_t parent_uuid;
+
+ bool read_only;
+} BtrfsSubvolInfo;
+
int btrfs_is_snapshot(int fd);
int btrfs_subvol_make(const char *path);
int btrfs_subvol_remove(const char *path);
int btrfs_subvol_snapshot(const char *old_path, const char *new_path, bool read_only, bool fallback_copy);
-int btrfs_subvol_read_only(const char *path, bool b);
-int btrfs_subvol_is_read_only_fd(int fd);
+
+int btrfs_subvol_set_read_only(const char *path, bool b);
+int btrfs_subvol_get_read_only_fd(int fd);
+int btrfs_subvol_get_id_fd(int fd, uint64_t *ret);
+int btrfs_subvol_get_info_fd(int fd, BtrfsSubvolInfo *info);
int btrfs_reflink(int infd, int outfd);
diff --git a/src/shared/missing.h b/src/shared/missing.h
index 91a6215226..dd7bef4d9d 100644
--- a/src/shared/missing.h
+++ b/src/shared/missing.h
@@ -250,6 +250,14 @@ struct btrfs_ioctl_fs_info_args {
struct btrfs_ioctl_vol_args)
#endif
+#ifndef BTRFS_FIRST_FREE_OBJECTID
+#define BTRFS_FIRST_FREE_OBJECTID 256
+#endif
+
+#ifndef BTRFS_ROOT_ITEM_KEY
+#define BTRFS_ROOT_ITEM_KEY 132
+#endif
+
#ifndef BTRFS_SUPER_MAGIC
#define BTRFS_SUPER_MAGIC 0x9123683E
#endif
diff --git a/src/shared/util.c b/src/shared/util.c
index 98b3465d4a..52e5df44a9 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -7561,8 +7561,37 @@ int openpt_in_namespace(pid_t pid, int flags) {
return -EIO;
}
-int fd_getcrtime(int fd, usec_t *usec) {
+ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags) {
+ _cleanup_close_ int fd = -1;
+ ssize_t l;
+
+ /* The kernel doesn't have a fgetxattrat() command, hence let's emulate one */
+
+ fd = openat(dirfd, filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOATIME|(flags & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW : 0));
+ if (fd < 0)
+ return -errno;
+
+ l = fgetxattr(fd, attribute, value, size);
+ if (l < 0)
+ return -errno;
+
+ return l;
+}
+
+static int parse_crtime(le64_t le, usec_t *usec) {
uint64_t u;
+
+ assert(usec);
+
+ u = le64toh(le);
+ if (u == 0 || u == (uint64_t) -1)
+ return -EIO;
+
+ *usec = (usec_t) u;
+ return 0;
+}
+
+int fd_getcrtime(int fd, usec_t *usec) {
le64_t le;
ssize_t n;
@@ -7578,16 +7607,23 @@ int fd_getcrtime(int fd, usec_t *usec) {
if (n != sizeof(le))
return -EIO;
- u = le64toh(le);
- if (u == 0 || u == (uint64_t) -1)
+ return parse_crtime(le, usec);
+}
+
+int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags) {
+ le64_t le;
+ ssize_t n;
+
+ n = fgetxattrat_fake(dirfd, name, "user.crtime_usec", &le, sizeof(le), flags);
+ if (n < 0)
+ return -errno;
+ if (n != sizeof(le))
return -EIO;
- *usec = (usec_t) u;
- return 0;
+ return parse_crtime(le, usec);
}
int path_getcrtime(const char *p, usec_t *usec) {
- uint64_t u;
le64_t le;
ssize_t n;
@@ -7600,12 +7636,7 @@ int path_getcrtime(const char *p, usec_t *usec) {
if (n != sizeof(le))
return -EIO;
- u = le64toh(le);
- if (u == 0 || u == (uint64_t) -1)
- return -EIO;
-
- *usec = (usec_t) u;
- return 0;
+ return parse_crtime(le, usec);
}
int fd_setcrtime(int fd, usec_t usec) {
diff --git a/src/shared/util.h b/src/shared/util.h
index 4d5b9824d9..a131a3c0f1 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -1063,6 +1063,9 @@ int ptsname_malloc(int fd, char **ret);
int openpt_in_namespace(pid_t pid, int flags);
+ssize_t fgetxattrat_fake(int dirfd, const char *filename, const char *attribute, void *value, size_t size, int flags);
+
int fd_setcrtime(int fd, usec_t usec);
int fd_getcrtime(int fd, usec_t *usec);
int path_getcrtime(const char *p, usec_t *usec);
+int fd_getcrtime_at(int dirfd, const char *name, usec_t *usec, int flags);