chiark / gitweb /
machined: beef up machined image listing with creation/modification times of subvolumes
authorLennart Poettering <lennart@poettering.net>
Thu, 25 Dec 2014 02:19:19 +0000 (03:19 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 25 Dec 2014 02:19:19 +0000 (03:19 +0100)
We make use of the btrfs subvol crtime for this, and for gpt images of a
manually managed xattr, if we can.

15 files changed:
Makefile.am
src/import/import-dkr.c
src/import/import-gpt.c
src/machine/image-dbus.c
src/machine/image.c
src/machine/image.h
src/machine/machinectl.c
src/machine/machined-dbus.c
src/shared/btrfs-ctree.h [new file with mode: 0644]
src/shared/btrfs-util.c
src/shared/btrfs-util.h
src/shared/missing.h
src/shared/util.c
src/shared/util.h
src/test/test-btrfs.c

index 89f3af89f5dc199ccda040e55bbeaceb5f948cd0..fd1a8a5ae9ef1b70dcf422f56ae1308c2d172141 100644 (file)
@@ -896,6 +896,7 @@ libsystemd_shared_la_SOURCES = \
        src/shared/nss-util.h \
        src/shared/btrfs-util.c \
        src/shared/btrfs-util.h \
+       src/shared/btrfs-ctree.h \
        src/shared/verbs.c \
        src/shared/verbs.h \
        src/shared/build.h
index 8f26191c40fd90a9d7e95dd3b9f50a3e9180f275..b290619305552e7eb460486476b996a799d27a4a 100644 (file)
@@ -780,7 +780,7 @@ static void dkr_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result
                         goto fail;
                 }
 
-                r = btrfs_subvol_read_only(job->temp_path, true);
+                r = btrfs_subvol_set_read_only(job->temp_path, true);
                 if (r < 0) {
                         log_error_errno(r, "Failed to mark snapshot read-only: %m");
                         goto fail;
index 503f1e64cf6121e49c9504474e5048a176eb3d5f..a81efa2bba46489abe165abfdfc822274342fd33 100644 (file)
@@ -171,6 +171,8 @@ static void gpt_import_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result
                 ut[1] = ut[0];
 
                 (void) futimens(f->disk_fd, ut);
+
+                fd_setcrtime(f->disk_fd, f->mtime);
         }
 
         if (fstat(f->disk_fd, &st) < 0) {
index afb849b41afb5d263bb3ca77a8b91f225c7e0e1e..8d9ad5589ed828d747f041cb85ceeca51d30f455 100644 (file)
@@ -159,12 +159,68 @@ static int property_get_read_only(
         return 1;
 }
 
+static int property_get_crtime(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+
+        _cleanup_(image_unrefp) Image *image = NULL;
+        int r;
+
+        assert(bus);
+        assert(reply);
+
+        r = image_find_by_bus_path_with_error(path, &image, error);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append(reply, "t", image->crtime);
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
+static int property_get_mtime(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+
+        _cleanup_(image_unrefp) Image *image = NULL;
+        int r;
+
+        assert(bus);
+        assert(reply);
+
+        r = image_find_by_bus_path_with_error(path, &image, error);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_message_append(reply, "t", image->mtime);
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
 const sd_bus_vtable image_vtable[] = {
         SD_BUS_VTABLE_START(0),
-        SD_BUS_PROPERTY("Name",     "s", property_get_name,      0, 0),
-        SD_BUS_PROPERTY("Path",     "s", property_get_path,      0, 0),
-        SD_BUS_PROPERTY("Type",     "s", property_get_type,      0, 0),
-        SD_BUS_PROPERTY("ReadOnly", "b", property_get_read_only, 0, 0),
+        SD_BUS_PROPERTY("Name",                  "s", property_get_name,      0, 0),
+        SD_BUS_PROPERTY("Path",                  "s", property_get_path,      0, 0),
+        SD_BUS_PROPERTY("Type",                  "s", property_get_type,      0, 0),
+        SD_BUS_PROPERTY("ReadOnly",              "b", property_get_read_only, 0, 0),
+        SD_BUS_PROPERTY("CreationTimestamp",     "t", property_get_crtime,    0, 0),
+        SD_BUS_PROPERTY("ModificationTimestamp", "t", property_get_mtime,     0, 0),
         SD_BUS_VTABLE_END
 };
 
index 2ffe9444e3e511a4a20a65d15acf1d2681d2ecdf..9f88b0551f51c1e4328b59e9d2272591c2dbead2 100644 (file)
@@ -46,8 +46,8 @@ static int image_new(
                 const char *name,
                 const char *path,
                 bool read_only,
+                usec_t crtime,
                 usec_t mtime,
-                usec_t btime,
                 Image **ret) {
 
         _cleanup_(image_unrefp) Image *i = NULL;
@@ -63,8 +63,8 @@ static int image_new(
 
         i->type = t;
         i->read_only = read_only;
+        i->crtime = crtime;
         i->mtime = mtime;
-        i->btime = btime;
 
         i->name = strdup(name);
         if (!i->name)
@@ -116,25 +116,20 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret)
                                 return -errno;
 
                         if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
-                                usec_t btime = 0;
-                                int ro;
+                                BtrfsSubvolInfo info;
 
                                 /* It's a btrfs subvolume */
 
-                                ro = btrfs_subvol_is_read_only_fd(fd);
-                                if (ro < 0)
-                                        return ro;
-
-                                /* r = btrfs_subvol_get_btime(fd, &btime); */
-                                /* if (r < 0) */
-                                /*         return r; */
+                                r = btrfs_subvol_get_info_fd(fd, &info);
+                                if (r < 0)
+                                        return r;
 
                                 r = image_new(IMAGE_SUBVOLUME,
                                               name,
                                               path,
-                                              ro,
+                                              info.read_only,
+                                              info.otime,
                                               0,
-                                              btime,
                                               ret);
                                 if (r < 0)
                                         return r;
@@ -158,18 +153,24 @@ static int image_make(int dfd, const char *name, const char *path, Image **ret)
                 return 1;
 
         } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) {
+                const char *truncated;
+                usec_t crtime = 0;
 
                 /* It's a GPT block device */
 
                 if (!ret)
                         return 1;
 
+                fd_getcrtime_at(dfd, name, &crtime, 0);
+
+                truncated = strndupa(name, strlen(name) - 4);
+
                 r = image_new(IMAGE_GPT,
-                              name,
+                              truncated,
                               path,
-                              !!(st.st_mode & 0222),
+                              !(st.st_mode & 0222),
+                              crtime,
                               timespec_load(&st.st_mtim),
-                              0,
                               ret);
                 if (r < 0)
                         return r;
index f04be239d3797f6ae33626061af04bd1458f5b4f..2e8f78147cf73a973dfed0cc470e8194bcbeb2b1 100644 (file)
@@ -39,8 +39,8 @@ typedef struct Image {
         char *path;
         bool read_only;
 
+        usec_t crtime;
         usec_t mtime;
-        usec_t btime;
 } Image;
 
 Image *image_unref(Image *i);
index d223e7895a8fba2300b497ebcbbda067c2359cc8..1cc5dfc68edbc514dda7bbac39a290e59e7aa5a3 100644 (file)
@@ -129,6 +129,8 @@ typedef struct ImageInfo {
         const char *name;
         const char *type;
         bool read_only;
+        usec_t crtime;
+        usec_t mtime;
 } ImageInfo;
 
 static int compare_image_info(const void *a, const void *b) {
@@ -140,14 +142,14 @@ static int compare_image_info(const void *a, const void *b) {
 static int list_images(int argc, char *argv[], void *userdata) {
 
         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
-        size_t max_name = strlen("NAME"), max_type = strlen("TYPE");
+        size_t max_name = strlen("NAME"), max_type = strlen("TYPE"), max_crtime = strlen("CREATED"), max_mtime = strlen("MODIFIED");
         _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
         _cleanup_free_ ImageInfo *images = NULL;
         size_t n_images = 0, n_allocated = 0, j;
         const char *name, *type, *object;
         sd_bus *bus = userdata;
-        int read_only;
-        int r;
+        uint64_t crtime, mtime;
+        int read_only, r;
 
         assert(bus);
 
@@ -167,11 +169,13 @@ static int list_images(int argc, char *argv[], void *userdata) {
                 return r;
         }
 
-        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbo)");
+        r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssbtto)");
         if (r < 0)
                 return bus_log_parse_error(r);
 
-        while ((r = sd_bus_message_read(reply, "(ssbo)", &name, &type, &read_only, &object)) > 0) {
+        while ((r = sd_bus_message_read(reply, "(ssbtto)", &name, &type, &read_only, &crtime, &mtime, &object)) > 0) {
+                char buf[FORMAT_TIMESTAMP_MAX];
+                size_t l;
 
                 if (name[0] == '.' && !arg_all)
                         continue;
@@ -182,12 +186,28 @@ static int list_images(int argc, char *argv[], void *userdata) {
                 images[n_images].name = name;
                 images[n_images].type = type;
                 images[n_images].read_only = read_only;
+                images[n_images].crtime = crtime;
+                images[n_images].mtime = mtime;
+
+                l = strlen(name);
+                if (l > max_name)
+                        max_name = l;
+
+                l = strlen(type);
+                if (l > max_type)
+                        max_type = l;
 
-                if (strlen(name) > max_name)
-                        max_name = strlen(name);
+                if (crtime != 0) {
+                        l = strlen(format_timestamp(buf, sizeof(buf), crtime));
+                        if (l > max_crtime)
+                                max_crtime = l;
+                }
 
-                if (strlen(type) > max_type)
-                        max_type = strlen(type);
+                if (mtime != 0) {
+                        l = strlen(format_timestamp(buf, sizeof(buf), mtime));
+                        if (l > max_mtime)
+                                max_mtime = l;
+                }
 
                 n_images++;
         }
@@ -201,13 +221,22 @@ static int list_images(int argc, char *argv[], void *userdata) {
         qsort_safe(images, n_images, sizeof(ImageInfo), compare_image_info);
 
         if (arg_legend)
-                printf("%-*s %-*s %-3s\n", (int) max_name, "NAME", (int) max_type, "TYPE", "RO");
+                printf("%-*s %-*s %-3s %*s %*s\n",
+                       (int) max_name, "NAME",
+                       (int) max_type, "TYPE",
+                       "RO",
+                       (int) max_crtime, "CREATED",
+                       (int) max_mtime, "MODIFIED");
 
         for (j = 0; j < n_images; j++) {
-                printf("%-*s %-*s %-3s\n",
+                char crtime_buf[FORMAT_TIMESTAMP_MAX], mtime_buf[FORMAT_TIMESTAMP_MAX];
+
+                printf("%-*s %-*s %-3s %*s %*s\n",
                        (int) max_name, images[j].name,
                        (int) max_type, images[j].type,
-                       yes_no(images[j].read_only));
+                       yes_no(images[j].read_only),
+                       (int) max_crtime, images[j].crtime != 0 ? format_timestamp(crtime_buf, sizeof(crtime_buf), images[j].crtime) : "-",
+                       (int) max_mtime, images[j].mtime != 0 ? format_timestamp(mtime_buf, sizeof(mtime_buf), images[j].mtime) : "-");
         }
 
         if (r < 0)
index 0bf97e1ebb640acb34fa0c82e029254a52c5b492..e264dacbe6ed14999f188c6d70c121111a667e80 100644 (file)
@@ -488,7 +488,7 @@ static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userda
         if (r < 0)
                 return r;
 
-        r = sd_bus_message_open_container(reply, 'a', "(ssbo)");
+        r = sd_bus_message_open_container(reply, 'a', "(ssbtto)");
         if (r < 0)
                 return r;
 
@@ -499,10 +499,12 @@ static int method_list_images(sd_bus *bus, sd_bus_message *message, void *userda
                 if (!p)
                         return -ENOMEM;
 
-                r = sd_bus_message_append(reply, "(ssbo)",
+                r = sd_bus_message_append(reply, "(ssbtto)",
                                           image->name,
                                           image_type_to_string(image->type),
                                           image->read_only,
+                                          image->crtime,
+                                          image->mtime,
                                           p);
                 if (r < 0)
                         return r;
@@ -563,7 +565,7 @@ const sd_bus_vtable manager_vtable[] = {
         SD_BUS_METHOD("GetImage", "s", "o", method_get_image, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("ListMachines", NULL, "a(ssso)", method_list_machines, SD_BUS_VTABLE_UNPRIVILEGED),
-        SD_BUS_METHOD("ListImages", NULL, "a(ssbo)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED),
+        SD_BUS_METHOD("ListImages", NULL, "a(ssbtto)", method_list_images, SD_BUS_VTABLE_UNPRIVILEGED),
         SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine, 0),
         SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network, 0),
         SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine, 0),
diff --git a/src/shared/btrfs-ctree.h b/src/shared/btrfs-ctree.h
new file mode 100644 (file)
index 0000000..45b94cd
--- /dev/null
@@ -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)
index d685b3ecbd23b12f9341974d0a8c6348bdfd7036..84c81106fa2a1fdd0cc9438408ac1a637e1ddb2e 100644 (file)
@@ -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;
+}
index c8f798b6a66eb132e70b45c8ff86396ec29fb5f0..f51f37a6590508ef5bc4167444fe5ee515d09043 100644 (file)
 #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);
 
index 91a6215226f840edb57c437f1b3d28c0c430b603..dd7bef4d9d70c1fe83d134ccbb2425849b555b00 100644 (file)
@@ -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
index 98b3465d4a58002fa00e767de90c83bf68429664..52e5df44a9994e759f1061a1eb715543e85d8350 100644 (file)
@@ -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) {
index 4d5b9824d976a3330bb01946124624f00b063af2..a131a3c0f127190489a306951fff084c1cb6a003 100644 (file)
@@ -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);
index 7c4cc554424910911311c9b02949de4a212e708f..4a08c72fbb5c9ed5b69459f7f1b7d62d77834ff9 100644 (file)
 ***/
 
 #include <stdlib.h>
+#include <fcntl.h>
 
 #include "log.h"
-#include "btrfs-util.h"
 #include "fileio.h"
+#include "util.h"
+#include "btrfs-util.h"
 
 int main(int argc, char *argv[]) {
         int r;
+        BtrfsSubvolInfo info;
+        char ts[FORMAT_TIMESTAMP_MAX];
+        int fd;
+
+        fd = open("/", O_RDONLY|O_CLOEXEC|O_DIRECTORY);
+        if (fd < 0)
+                log_error_errno(errno, "Failed to open root directory: %m");
+        else {
+                r = btrfs_subvol_get_info_fd(fd, &info);
+                if (r < 0)
+                        log_error_errno(r, "Failed to get subvolume info: %m");
+                else {
+                        log_info("otime: %s", format_timestamp(ts, sizeof(ts), info.otime));
+                        log_info("read-only: %s", yes_no(info.read_only));
+                }
+
+                r = btrfs_subvol_get_read_only_fd(fd);
+                assert_se(r >= 0);
+
+                log_info("read-only: %s", yes_no(r));
+        }
 
         r = btrfs_subvol_make("/xxxtest");
         if (r < 0)