]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: Introduce a union in struct nvme_dev for different transport types
authorJeremy Kerr <jk@codeconstruct.com.au>
Wed, 13 Jul 2022 05:19:24 +0000 (13:19 +0800)
committerDaniel Wagner <dwagner@suse.de>
Fri, 12 Aug 2022 06:52:28 +0000 (08:52 +0200)
This change modifies struct nvme_dev to allow for future transport
types, by moving the transport-specific data into a union. We will add
new types in a future change.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
nvme.c
nvme.h

diff --git a/nvme.c b/nvme.c
index 50099a92ed3a7b08c4492d392fc1bbad8e484e9d..95c23ed4147387ae37be43a50d0c2bc07269f42c 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -207,12 +207,12 @@ static ssize_t getrandom_bytes(void *buf, size_t buflen)
 
 static bool is_chardev(struct nvme_dev *dev)
 {
-       return S_ISCHR(dev->stat.st_mode);
+       return S_ISCHR(dev->direct.stat.st_mode);
 }
 
 static bool is_blkdev(struct nvme_dev *dev)
 {
-       return S_ISBLK(dev->stat.st_mode);
+       return S_ISBLK(dev->direct.stat.st_mode);
 }
 
 static int open_dev(struct nvme_dev **devp, char *devstr, int flags)
@@ -223,15 +223,17 @@ static int open_dev(struct nvme_dev **devp, char *devstr, int flags)
        dev = calloc(1, sizeof(*dev));
        if (!dev)
                return -1;
+
+       dev->type = NVME_DEV_DIRECT;
        dev->name = basename(devstr);
        err = open(devstr, flags);
        if (err < 0) {
                perror(devstr);
                goto err_free;
        }
-       dev->fd = err;
+       dev->direct.fd = err;
 
-       err = fstat(dev_fd(dev), &dev->stat);
+       err = fstat(dev_fd(dev), &dev->direct.stat);
        if (err < 0) {
                perror(devstr);
                goto err_close;
diff --git a/nvme.h b/nvme.h
index 549cd9cd2472c447c94cb0bf9d3d8f50fc601dcb..0f0f1ead6205fa1a03db03e2eac7fb932b9cf4de 100644 (file)
--- a/nvme.h
+++ b/nvme.h
@@ -19,6 +19,7 @@
 #include <dirent.h>
 #include <stdbool.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <endian.h>
 #include <sys/time.h>
 #include <sys/stat.h>
@@ -37,15 +38,33 @@ enum nvme_print_flags {
 
 #define SYS_NVME "/sys/class/nvme"
 
+enum nvme_dev_type {
+       NVME_DEV_DIRECT,
+};
+
 struct nvme_dev {
-       int fd;
-       struct stat stat;
+       enum nvme_dev_type type;
+       union {
+               struct {
+                       int fd;
+                       struct stat stat;
+               } direct;
+       };
+
        const char *name;
 };
 
-static inline int dev_fd(struct nvme_dev *dev)
+#define dev_fd(d) __dev_fd(d, __func__, __LINE__)
+
+static inline int __dev_fd(struct nvme_dev *dev, const char *func, int line)
 {
-       return dev->fd;
+       if (dev->type != NVME_DEV_DIRECT) {
+               fprintf(stderr,
+                       "warning: %s:%d not a direct transport!\n",
+                       func, line);
+               return -1;
+       }
+       return dev->direct.fd;
 }
 
 void register_extension(struct plugin *plugin);