Skip to content

Commit 6f5e357

Browse files
minwooimigaw
authored andcommitted
tree: fix generic device open failure
If scan_namespace is called with a generic device (e.g., /dev/ng0n1) given, it fails to scan a namespace based on the generic device because there's no entry point in /sys/block/ for the generic device. This patch provides two helpers to change the given generic device name to a block device name based on the instances: ng0n1 -> nvme0n1 This patch fixes command failure: root@vm:~/work/nvme-cli.git# nvme show-regs /dev/ng0n1 Unable to find ng0n1 get-property: Invalid argument Signed-off-by: Minwoo Im <[email protected]> [dwagner: also catch blkdev allocation failures] Signed-off-by: Daniel Wagner <[email protected]>
1 parent 342b186 commit 6f5e357

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

src/nvme/tree.c

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,27 +1864,62 @@ static nvme_ns_t nvme_ns_open(const char *name)
18641864
return NULL;
18651865
}
18661866

1867+
static inline bool nvme_ns_is_generic(const char *name)
1868+
{
1869+
int instance, head_instance;
1870+
1871+
if (sscanf(name, "ng%dn%d", &instance, &head_instance) != 2)
1872+
return false;
1873+
return true;
1874+
}
1875+
1876+
static char *nvme_ns_generic_to_blkdev(const char *generic)
1877+
{
1878+
1879+
int instance, head_instance;
1880+
char blkdev[PATH_MAX];
1881+
1882+
if (!nvme_ns_is_generic(generic))
1883+
return strdup(generic);
1884+
1885+
sscanf(generic, "ng%dn%d", &instance, &head_instance);
1886+
sprintf(blkdev, "nvme%dn%d", instance, head_instance);
1887+
1888+
return strdup(blkdev);
1889+
}
1890+
18671891
static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char *name)
18681892
{
18691893
struct nvme_ns *n;
18701894
char *path;
18711895
int ret;
1896+
char *blkdev;
18721897

1873-
ret = asprintf(&path, "%s/%s", sysfs_dir, name);
1874-
if (ret < 0) {
1898+
blkdev = nvme_ns_generic_to_blkdev(name);
1899+
if (!blkdev) {
18751900
errno = ENOMEM;
18761901
return NULL;
18771902
}
18781903

1879-
n = nvme_ns_open(name);
1904+
ret = asprintf(&path, "%s/%s", sysfs_dir, blkdev);
1905+
if (ret < 0) {
1906+
errno = ENOMEM;
1907+
goto free_blkdev;
1908+
}
1909+
1910+
n = nvme_ns_open(blkdev);
18801911
if (!n)
18811912
goto free_path;
18821913

18831914
n->sysfs_dir = path;
1915+
1916+
free(blkdev);
18841917
return n;
18851918

18861919
free_path:
18871920
free(path);
1921+
free_blkdev:
1922+
free(blkdev);
18881923
return NULL;
18891924
}
18901925

0 commit comments

Comments
 (0)