summaryrefslogtreecommitdiff
path: root/src/shared/btrfs-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2014-12-25 03:19:19 +0100
committerLennart Poettering <lennart@poettering.net>2014-12-25 03:19:19 +0100
commit10f9c75519671e7c7ab8993b54fe22da7c2d0c38 (patch)
tree78777a123c0261f892af164581884f8a07756203 /src/shared/btrfs-util.c
parent5fa89b2cb366d533e56a9b7a9ce548480776f973 (diff)
machined: beef up machined image listing with creation/modification times of subvolumes
We make use of the btrfs subvol crtime for this, and for gpt images of a manually managed xattr, if we can.
Diffstat (limited to 'src/shared/btrfs-util.c')
-rw-r--r--src/shared/btrfs-util.c82
1 files changed, 79 insertions, 3 deletions
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;
+}