| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/* |
| 3 |
* This file is part of libnvme. |
| 4 |
* Copyright (c) 2020 Western Digital Corporation or its affiliates. |
| 5 |
* |
| 6 |
* Authors: Keith Busch <[email protected]> |
| 7 |
* Chaitanya Kulkarni <[email protected]> |
| 8 |
*/ |
| 9 |
#include <stdio.h> |
| 10 |
#include <string.h> |
| 11 |
#include <dirent.h> |
| 12 |
|
| 13 |
#include "filters.h" |
| 14 |
#include "private.h" |
| 15 |
|
| 16 |
int nvme_namespace_filter(const struct dirent *d) |
| 17 |
{ |
| 18 |
int i, n; |
| 19 |
|
| 20 |
if (d->d_name[0] == '.') |
| 21 |
return 0; |
| 22 |
|
| 23 |
if (strstr(d->d_name, "nvme")) |
| 24 |
if (sscanf(d->d_name, "nvme%dn%d", &i, &n) == 2) |
| 25 |
return 1; |
| 26 |
|
| 27 |
return 0; |
| 28 |
} |
| 29 |
|
| 30 |
int nvme_paths_filter(const struct dirent *d) |
| 31 |
{ |
| 32 |
int i, c, n; |
| 33 |
|
| 34 |
if (d->d_name[0] == '.') |
| 35 |
return 0; |
| 36 |
|
| 37 |
if (strstr(d->d_name, "nvme")) |
| 38 |
if (sscanf(d->d_name, "nvme%dc%dn%d", &i, &c, &n) == 3) |
| 39 |
return 1; |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int nvme_ctrls_filter(const struct dirent *d) |
| 45 |
{ |
| 46 |
int i, c, n; |
| 47 |
|
| 48 |
if (d->d_name[0] == '.') |
| 49 |
return 0; |
| 50 |
|
| 51 |
if (strstr(d->d_name, "nvme")) { |
| 52 |
if (sscanf(d->d_name, "nvme%dc%dn%d", &i, &c, &n) == 3) |
| 53 |
return 0; |
| 54 |
if (sscanf(d->d_name, "nvme%dn%d", &i, &n) == 2) |
| 55 |
return 0; |
| 56 |
if (sscanf(d->d_name, "nvme%d", &i) == 1) |
| 57 |
return 1; |
| 58 |
} |
| 59 |
|
| 60 |
return 0; |
| 61 |
} |
| 62 |
|
| 63 |
int nvme_subsys_filter(const struct dirent *d) |
| 64 |
{ |
| 65 |
int i; |
| 66 |
|
| 67 |
if (d->d_name[0] == '.') |
| 68 |
return 0; |
| 69 |
|
| 70 |
if (strstr(d->d_name, "nvme-subsys")) |
| 71 |
if (sscanf(d->d_name, "nvme-subsys%d", &i) == 1) |
| 72 |
return 1; |
| 73 |
|
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
int nvme_scan_subsystems(struct dirent ***subsys) |
| 78 |
{ |
| 79 |
const char *dir = nvme_subsys_sysfs_dir(); |
| 80 |
|
| 81 |
return scandir(dir, subsys, nvme_subsys_filter, alphasort); |
| 82 |
} |
| 83 |
|
| 84 |
int nvme_scan_subsystem_namespaces(nvme_subsystem_t s, struct dirent ***ns) |
| 85 |
{ |
| 86 |
return scandir(nvme_subsystem_get_sysfs_dir(s), ns, |
| 87 |
nvme_namespace_filter, alphasort); |
| 88 |
} |
| 89 |
|
| 90 |
int nvme_scan_ctrls(struct dirent ***ctrls) |
| 91 |
{ |
| 92 |
const char *dir = nvme_ctrl_sysfs_dir(); |
| 93 |
|
| 94 |
return scandir(dir, ctrls, nvme_ctrls_filter, alphasort); |
| 95 |
} |
| 96 |
|
| 97 |
int nvme_scan_ctrl_namespace_paths(nvme_ctrl_t c, struct dirent ***paths) |
| 98 |
{ |
| 99 |
return scandir(nvme_ctrl_get_sysfs_dir(c), paths, |
| 100 |
nvme_paths_filter, alphasort); |
| 101 |
} |
| 102 |
|
| 103 |
int nvme_scan_ctrl_namespaces(nvme_ctrl_t c, struct dirent ***ns) |
| 104 |
{ |
| 105 |
return scandir(nvme_ctrl_get_sysfs_dir(c), ns, |
| 106 |
nvme_namespace_filter, alphasort); |
| 107 |
} |
| 108 |
|
| 109 |
int nvme_scan_ns_head_paths(nvme_ns_head_t head, struct dirent ***paths) |
| 110 |
{ |
| 111 |
return scandir(nvme_ns_head_get_sysfs_dir(head), paths, |
| 112 |
nvme_paths_filter, alphasort); |
| 113 |
} |