]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
tree: fix lba_count size calculation
authorDaniel Wagner <dwagner@suse.de>
Thu, 28 Mar 2024 16:20:13 +0000 (17:20 +0100)
committerDaniel Wagner <wagi@monom.org>
Wed, 3 Apr 2024 15:29:53 +0000 (17:29 +0200)
The kernel reports the size in 512 byte units, which is might not match
with lba_size.

Signed-off-by: Daniel Wagner <dwagner@suse.de>
src/nvme/private.h
src/nvme/tree.c

index 723740bed4ec43fdc8b8acc596d04dd8a6ccd01c..71d560650b4d50ec856988b052deb192af467371 100644 (file)
@@ -290,4 +290,7 @@ struct __mi_mctp_socket_ops {
 };
 void __nvme_mi_mctp_set_ops(const struct __mi_mctp_socket_ops *newops);
 
+#define SECTOR_SIZE    512
+#define SECTOR_SHIFT   9
+
 #endif /* _LIBNVME_PRIVATE_H */
index 6efdf99297e257da633ebf35d72beca6d3c8b6f3..fdba81a370b3721f78b00dcfd403b79ec9f5ca34 100644 (file)
@@ -2449,11 +2449,12 @@ static int nvme_ns_init(const char *path, struct nvme_ns *ns)
 {
        _cleanup_free_ char *attr = NULL;
        struct stat sb;
+       uint64_t size;
        int ret;
 
        struct sysfs_attr_table base[] = {
                { &ns->nsid,      nvme_strtou32,  true, "nsid" },
-               { &ns->lba_count, nvme_strtou64,  true, "size" },
+               { &size,          nvme_strtou64,  true, "size" },
                { &ns->lba_size,  nvme_strtou64,  true, "queue/logical_block_size" },
                { ns->eui64,      nvme_strtoeuid, false, "eui" },
                { ns->nguid,      nvme_strtouuid, false, "nguid" },
@@ -2465,6 +2466,11 @@ static int nvme_ns_init(const char *path, struct nvme_ns *ns)
                return ret;
 
        ns->lba_shift = GETSHIFT(ns->lba_size);
+       /*
+        * size is in 512 bytes units and lba_count is in lba_size which are not
+        * necessarily the same.
+        */
+       ns->lba_count = size >> (ns->lba_shift -  SECTOR_SHIFT);
 
        if (asprintf(&attr, "%s/csi", path) < 0)
                return -errno;