| 1 |
/* |
| 2 |
* Copyright (C) 2014-2021 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Tomas Bzatek <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <string.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <unistd.h> |
| 24 |
#include <sys/ioctl.h> |
| 25 |
#include <sys/stat.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <fcntl.h> |
| 28 |
#include <malloc.h> |
| 29 |
#include <linux/fs.h> |
| 30 |
#include <glib/gstdio.h> |
| 31 |
|
| 32 |
#include <libnvme.h> |
| 33 |
|
| 34 |
#include <blockdev/utils.h> |
| 35 |
#include <check_deps.h> |
| 36 |
#include "nvme.h" |
| 37 |
#include "nvme-private.h" |
| 38 |
|
| 39 |
|
| 40 |
/* nvme-cli defaults */ |
| 41 |
#define PATH_NVMF_CONFIG "/etc/nvme/config.json" |
| 42 |
#define MAX_DISC_RETRIES 10 |
| 43 |
|
| 44 |
|
| 45 |
static void parse_extra_args (const BDExtraArg **extra, struct nvme_fabrics_config *cfg, const gchar **config_file, const gchar **hostkey, const gchar **ctrlkey, const gchar **hostsymname) { |
| 46 |
const BDExtraArg **extra_i; |
| 47 |
|
| 48 |
if (!extra) |
| 49 |
return; |
| 50 |
|
| 51 |
#define SAFE_INT_CONV(target) { \ |
| 52 |
gint64 v; \ |
| 53 |
gchar *endptr = NULL; \ |
| 54 |
\ |
| 55 |
v = g_ascii_strtoll ((*extra_i)->val, &endptr, 0); \ |
| 56 |
if (endptr != (*extra_i)->val) \ |
| 57 |
target = v; \ |
| 58 |
} |
| 59 |
#define SAFE_BOOL_CONV(target) { \ |
| 60 |
if (g_ascii_strcasecmp ((*extra_i)->val, "on") == 0 || \ |
| 61 |
g_ascii_strcasecmp ((*extra_i)->val, "1") == 0 || \ |
| 62 |
g_ascii_strcasecmp ((*extra_i)->val, "true") == 0) \ |
| 63 |
target = TRUE; \ |
| 64 |
else \ |
| 65 |
if (g_ascii_strcasecmp ((*extra_i)->val, "off") == 0 || \ |
| 66 |
g_ascii_strcasecmp ((*extra_i)->val, "0") == 0 || \ |
| 67 |
g_ascii_strcasecmp ((*extra_i)->val, "false") == 0) \ |
| 68 |
target = FALSE; \ |
| 69 |
} |
| 70 |
|
| 71 |
for (extra_i = extra; *extra_i; extra_i++) { |
| 72 |
if (g_strcmp0 ((*extra_i)->opt, "config") == 0 && config_file) { |
| 73 |
if (g_ascii_strcasecmp ((*extra_i)->val, "none") == 0) |
| 74 |
*config_file = NULL; |
| 75 |
else |
| 76 |
*config_file = (*extra_i)->val; |
| 77 |
} else |
| 78 |
if (g_strcmp0 ((*extra_i)->opt, "dhchap_key") == 0 && hostkey) |
| 79 |
*hostkey = (*extra_i)->val; |
| 80 |
else |
| 81 |
if (g_strcmp0 ((*extra_i)->opt, "dhchap_ctrl_key") == 0 && ctrlkey) |
| 82 |
*ctrlkey = (*extra_i)->val; |
| 83 |
else |
| 84 |
if (g_strcmp0 ((*extra_i)->opt, "hostsymname") == 0 && hostsymname) |
| 85 |
*hostsymname = (*extra_i)->val; |
| 86 |
else |
| 87 |
if (g_strcmp0 ((*extra_i)->opt, "nr_io_queues") == 0) |
| 88 |
SAFE_INT_CONV (cfg->nr_io_queues) |
| 89 |
else |
| 90 |
if (g_strcmp0 ((*extra_i)->opt, "nr_write_queues") == 0) |
| 91 |
SAFE_INT_CONV (cfg->nr_write_queues) |
| 92 |
else |
| 93 |
if (g_strcmp0 ((*extra_i)->opt, "nr_poll_queues") == 0) |
| 94 |
SAFE_INT_CONV (cfg->nr_poll_queues) |
| 95 |
else |
| 96 |
if (g_strcmp0 ((*extra_i)->opt, "queue_size") == 0) |
| 97 |
SAFE_INT_CONV (cfg->queue_size) |
| 98 |
else |
| 99 |
if (g_strcmp0 ((*extra_i)->opt, "keep_alive_tmo") == 0) |
| 100 |
SAFE_INT_CONV (cfg->keep_alive_tmo) |
| 101 |
else |
| 102 |
if (g_strcmp0 ((*extra_i)->opt, "reconnect_delay") == 0) |
| 103 |
SAFE_INT_CONV (cfg->reconnect_delay) |
| 104 |
else |
| 105 |
if (g_strcmp0 ((*extra_i)->opt, "ctrl_loss_tmo") == 0) |
| 106 |
SAFE_INT_CONV (cfg->ctrl_loss_tmo) |
| 107 |
else |
| 108 |
if (g_strcmp0 ((*extra_i)->opt, "fast_io_fail_tmo") == 0) |
| 109 |
SAFE_INT_CONV (cfg->fast_io_fail_tmo) |
| 110 |
else |
| 111 |
if (g_strcmp0 ((*extra_i)->opt, "tos") == 0) |
| 112 |
SAFE_INT_CONV (cfg->tos) |
| 113 |
else |
| 114 |
if (g_strcmp0 ((*extra_i)->opt, "duplicate_connect") == 0) |
| 115 |
SAFE_BOOL_CONV (cfg->duplicate_connect) |
| 116 |
else |
| 117 |
if (g_strcmp0 ((*extra_i)->opt, "disable_sqflow") == 0) |
| 118 |
SAFE_BOOL_CONV (cfg->disable_sqflow) |
| 119 |
else |
| 120 |
if (g_strcmp0 ((*extra_i)->opt, "hdr_digest") == 0) |
| 121 |
SAFE_BOOL_CONV (cfg->hdr_digest) |
| 122 |
else |
| 123 |
if (g_strcmp0 ((*extra_i)->opt, "data_digest") == 0) |
| 124 |
SAFE_BOOL_CONV (cfg->data_digest) |
| 125 |
else |
| 126 |
if (g_strcmp0 ((*extra_i)->opt, "tls") == 0) |
| 127 |
SAFE_BOOL_CONV (cfg->tls) |
| 128 |
#ifdef HAVE_LIBNVME_1_4 |
| 129 |
else |
| 130 |
if (g_strcmp0 ((*extra_i)->opt, "keyring") == 0) { |
| 131 |
int keyring; |
| 132 |
|
| 133 |
keyring = nvme_lookup_keyring ((*extra_i)->val); |
| 134 |
if (keyring) { |
| 135 |
cfg->keyring = keyring; |
| 136 |
nvme_set_keyring (cfg->keyring); |
| 137 |
} |
| 138 |
} else |
| 139 |
if (g_strcmp0 ((*extra_i)->opt, "tls_key") == 0) { |
| 140 |
int key; |
| 141 |
|
| 142 |
key = nvme_lookup_key ("psk", (*extra_i)->val); |
| 143 |
if (key) |
| 144 |
cfg->tls_key = key; |
| 145 |
} |
| 146 |
#endif |
| 147 |
} |
| 148 |
|
| 149 |
#undef SAFE_INT_CONV |
| 150 |
#undef SAFE_BOOL_CONV |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
/** |
| 155 |
* bd_nvme_connect: |
| 156 |
* @subsysnqn: The name for the NVMe subsystem to connect to. |
| 157 |
* @transport: The network fabric used for a NVMe-over-Fabrics network. |
| 158 |
* @transport_addr: (nullable): The network address of the Controller. For transports using IP addressing (e.g. `rdma`) this should be an IP-based address. |
| 159 |
* @transport_svcid: (nullable): The transport service ID. For transports using IP addressing (e.g. `tcp`, `rdma`) this field is the port number. The default port number for the `tcp` and `rdma` transports is `4420` and `8009` respectively when the well-known Discovery NQN is specified. |
| 160 |
* @host_traddr: (nullable): The network address used on the host to connect to the Controller. For TCP, this sets the source address on the socket. |
| 161 |
* @host_iface: (nullable): The network interface used on the host to connect to the Controller (e.g. IP `eth1`, `enp2s0`). This forces the connection to be made on a specific interface instead of letting the system decide. |
| 162 |
* @host_nqn: (nullable): Overrides the default Host NQN that identifies the NVMe Host. If this option is %NULL, the default is read from `/etc/nvme/hostnqn` first. |
| 163 |
* If that does not exist, the autogenerated NQN value from the NVMe Host kernel module is used next. The Host NQN uniquely identifies the NVMe Host. |
| 164 |
* @host_id: (nullable): User-defined host UUID or %NULL to use default (as defined in `/etc/nvme/hostid`). |
| 165 |
* @extra: (nullable) (array zero-terminated=1): Additional arguments. |
| 166 |
* @error: (out) (nullable): Place to store error (if any). |
| 167 |
* |
| 168 |
* Creates a transport connection to a remote system (specified by @transport_addr and @transport_svcid) |
| 169 |
* and creates a NVMe over Fabrics controller for the NVMe subsystem specified by the @subsysnqn option. |
| 170 |
* |
| 171 |
* Valid values for @transport include: |
| 172 |
* - `"rdma"`: An rdma network (RoCE, iWARP, Infiniband, basic rdma, etc.) |
| 173 |
* - `"fc"`: A Fibre Channel network. |
| 174 |
* - `"tcp"`: A TCP/IP network. |
| 175 |
* - `"loop"`: A NVMe over Fabrics target on the local host. |
| 176 |
* |
| 177 |
* In addition to the primary options it's possible to supply @extra arguments: |
| 178 |
* - `"config"`: Use the specified JSON configuration file instead of the default file (see below) or |
| 179 |
* specify `"none"` to avoid reading any configuration file. |
| 180 |
* - `"dhchap_key"`: NVMe In-band authentication secret in ASCII format as described |
| 181 |
* in the NVMe 2.0 specification. When not specified, the secret is by default read |
| 182 |
* from `/etc/nvme/hostkey`. In case that file does not exist no in-band authentication |
| 183 |
* is attempted. |
| 184 |
* - `"dhchap_ctrl_key"`: NVMe In-band authentication controller secret for bi-directional authentication. |
| 185 |
* When not specified, no bi-directional authentication is attempted. |
| 186 |
* - `"nr_io_queues"`: The number of I/O queues. |
| 187 |
* - `"nr_write_queues"`: Number of additional queues that will be used for write I/O. |
| 188 |
* - `"nr_poll_queues"`: Number of additional queues that will be used for polling latency sensitive I/O. |
| 189 |
* - `"queue_size"`: Number of elements in the I/O queues. |
| 190 |
* - `"keep_alive_tmo"`: The keep alive timeout (in seconds). |
| 191 |
* - `"reconnect_delay"`: The delay (in seconds) before reconnect is attempted after a connect loss. |
| 192 |
* - `"ctrl_loss_tmo"`: The controller loss timeout period (in seconds). A special value of `-1` will cause reconnecting forever. |
| 193 |
* - `"fast_io_fail_tmo"`: Fast I/O Fail timeout (in seconds). |
| 194 |
* - `"tos"`: Type of service. |
| 195 |
* - `"duplicate_connect"`: Allow duplicated connections between same transport host and subsystem port. Boolean value. |
| 196 |
* - `"disable_sqflow"`: Disables SQ flow control to omit head doorbell update for submission queues when sending nvme completions. Boolean value. |
| 197 |
* - `"hdr_digest"`: Generates/verifies header digest (TCP). Boolean value. |
| 198 |
* - `"data_digest"`: Generates/verifies data digest (TCP). Boolean value. |
| 199 |
* - `"tls"`: Enable TLS encryption (TCP). Boolean value. |
| 200 |
* - `"hostsymname"`: TP8010: NVMe host symbolic name. |
| 201 |
* - `"keyring"`: Keyring to store and lookup keys. String value. |
| 202 |
* - `"tls_key"`: TLS PSK for the connection. String value. |
| 203 |
* |
| 204 |
* Boolean values can be expressed by "0"/"1", "on"/"off" or "True"/"False" case-insensitive |
| 205 |
* strings. Failed numerical or boolean string conversions will result in the option being ignored. |
| 206 |
* |
| 207 |
* By default additional options are read from the default configuration file `/etc/nvme/config.json`. |
| 208 |
* This follows the default behaviour of `nvme-cli`. Use the @extra `"config"` argument |
| 209 |
* to either specify a different config file or disable use of it. The JSON configuration |
| 210 |
* file format is documented in [https://raw.githubusercontent.com/linux-nvme/libnvme/master/doc/config-schema.json](https://raw.githubusercontent.com/linux-nvme/libnvme/master/doc/config-schema.json). |
| 211 |
* As a rule @extra key names are kept consistent with the JSON config file schema. |
| 212 |
* Any @extra option generally overrides particular option specified in a configuration file. |
| 213 |
* |
| 214 |
* Returns: %TRUE if the subsystem was connected successfully, %FALSE otherwise with @error set. |
| 215 |
* |
| 216 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 217 |
*/ |
| 218 |
gboolean bd_nvme_connect (const gchar *subsysnqn, const gchar *transport, const gchar *transport_addr, const gchar *transport_svcid, const gchar *host_traddr, const gchar *host_iface, const gchar *host_nqn, const gchar *host_id, const BDExtraArg **extra, GError **error) { |
| 219 |
int ret; |
| 220 |
const gchar *config_file = PATH_NVMF_CONFIG; |
| 221 |
gchar *host_nqn_val; |
| 222 |
gchar *host_id_val; |
| 223 |
const gchar *hostkey = NULL; |
| 224 |
const gchar *ctrlkey = NULL; |
| 225 |
const gchar *hostsymname = NULL; |
| 226 |
nvme_root_t root; |
| 227 |
nvme_host_t host; |
| 228 |
nvme_ctrl_t ctrl; |
| 229 |
struct nvme_fabrics_config cfg; |
| 230 |
|
| 231 |
if (subsysnqn == NULL) { |
| 232 |
g_set_error_literal (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 233 |
"Invalid value specified for the subsysnqn argument"); |
| 234 |
return FALSE; |
| 235 |
} |
| 236 |
if (transport == NULL) { |
| 237 |
g_set_error_literal (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 238 |
"Invalid value specified for the transport argument"); |
| 239 |
return FALSE; |
| 240 |
} |
| 241 |
if (transport_addr == NULL && !g_str_equal (transport, "loop") && !g_str_equal (transport, "pcie")) { |
| 242 |
g_set_error_literal (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 243 |
"Invalid value specified for the transport address argument"); |
| 244 |
return FALSE; |
| 245 |
} |
| 246 |
|
| 247 |
/* HostNQN checks */ |
| 248 |
host_nqn_val = g_strdup (host_nqn); |
| 249 |
host_id_val = g_strdup (host_id); |
| 250 |
if (host_nqn_val == NULL) |
| 251 |
host_nqn_val = nvmf_hostnqn_from_file (); |
| 252 |
if (host_id_val == NULL) |
| 253 |
host_id_val = nvmf_hostid_from_file (); |
| 254 |
if (host_nqn_val == NULL) |
| 255 |
host_nqn_val = nvmf_hostnqn_generate (); |
| 256 |
if (host_nqn_val == NULL) { |
| 257 |
g_set_error_literal (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 258 |
"Could not determine HostNQN"); |
| 259 |
g_free (host_nqn_val); |
| 260 |
g_free (host_id_val); |
| 261 |
return FALSE; |
| 262 |
} |
| 263 |
if (host_id_val == NULL) { |
| 264 |
/* derive hostid from hostnqn, newer kernels refuse empty hostid */ |
| 265 |
host_id_val = g_strrstr (host_nqn_val, "uuid:"); |
| 266 |
if (host_id_val) |
| 267 |
host_id_val = g_strdup (host_id_val + strlen ("uuid:")); |
| 268 |
/* TODO: in theory generating arbitrary uuid might work as a fallback */ |
| 269 |
} |
| 270 |
if (host_id_val == NULL) { |
| 271 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 272 |
"Could not determine HostID value from HostNQN '%s'", |
| 273 |
host_nqn_val); |
| 274 |
g_free (host_nqn_val); |
| 275 |
return FALSE; |
| 276 |
} |
| 277 |
|
| 278 |
/* parse extra arguments */ |
| 279 |
nvmf_default_config (&cfg); |
| 280 |
parse_extra_args (extra, &cfg, &config_file, &hostkey, &ctrlkey, &hostsymname); |
| 281 |
|
| 282 |
root = nvme_scan (config_file); |
| 283 |
g_assert (root != NULL); |
| 284 |
nvme_init_logging (root, -1, false, false); |
| 285 |
host = nvme_lookup_host (root, host_nqn_val, host_id_val); |
| 286 |
if (host == NULL) { |
| 287 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED, |
| 288 |
"Unable to lookup host for HostNQN '%s' and HostID '%s'", |
| 289 |
host_nqn_val, host_id_val); |
| 290 |
g_free (host_nqn_val); |
| 291 |
g_free (host_id_val); |
| 292 |
nvme_free_tree (root); |
| 293 |
return FALSE; |
| 294 |
} |
| 295 |
g_free (host_nqn_val); |
| 296 |
g_free (host_id_val); |
| 297 |
if (hostkey) |
| 298 |
nvme_host_set_dhchap_key (host, hostkey); |
| 299 |
if (hostsymname) |
| 300 |
nvme_host_set_hostsymname (host, hostsymname); |
| 301 |
|
| 302 |
/* tr_svcid defaults */ |
| 303 |
if (!transport_svcid) { |
| 304 |
if (g_strcmp0 (transport, "tcp") == 0) { |
| 305 |
if (g_strcmp0 (subsysnqn, NVME_DISC_SUBSYS_NAME) == 0) |
| 306 |
transport_svcid = G_STRINGIFY (NVME_DISC_IP_PORT); |
| 307 |
else |
| 308 |
transport_svcid = G_STRINGIFY (NVME_RDMA_IP_PORT); |
| 309 |
} else |
| 310 |
if (g_strcmp0(transport, "rdma") == 0) |
| 311 |
transport_svcid = G_STRINGIFY (NVME_RDMA_IP_PORT); |
| 312 |
} |
| 313 |
|
| 314 |
ctrl = nvme_create_ctrl (root, subsysnqn, transport, transport_addr, host_traddr, host_iface, transport_svcid); |
| 315 |
if (ctrl == NULL) { |
| 316 |
_nvme_fabrics_errno_to_gerror (-1, errno, error); |
| 317 |
g_prefix_error (error, "Error creating the controller: "); |
| 318 |
nvme_free_tree (root); |
| 319 |
return FALSE; |
| 320 |
} |
| 321 |
if (ctrlkey) |
| 322 |
nvme_ctrl_set_dhchap_key (ctrl, ctrlkey); |
| 323 |
|
| 324 |
ret = nvmf_add_ctrl (host, ctrl, &cfg); |
| 325 |
if (ret != 0) { |
| 326 |
_nvme_fabrics_errno_to_gerror (ret, errno, error); |
| 327 |
g_prefix_error (error, "Error connecting the controller: "); |
| 328 |
nvme_free_ctrl (ctrl); |
| 329 |
nvme_free_tree (root); |
| 330 |
return FALSE; |
| 331 |
} |
| 332 |
nvme_free_ctrl (ctrl); |
| 333 |
nvme_free_tree (root); |
| 334 |
|
| 335 |
return TRUE; |
| 336 |
} |
| 337 |
|
| 338 |
static gboolean _disconnect (const gchar *subsysnqn, const gchar *path, GError **error, gboolean *found) { |
| 339 |
nvme_root_t root; |
| 340 |
nvme_host_t host; |
| 341 |
nvme_subsystem_t subsys; |
| 342 |
nvme_ctrl_t ctrl; |
| 343 |
int ret; |
| 344 |
|
| 345 |
root = nvme_create_root (NULL, -1); |
| 346 |
if (root == NULL) { |
| 347 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED, |
| 348 |
"Failed to create topology root: %s", |
| 349 |
strerror_l (errno, _C_LOCALE)); |
| 350 |
return FALSE; |
| 351 |
} |
| 352 |
ret = nvme_scan_topology (root, NULL, NULL); |
| 353 |
if (ret < 0) { |
| 354 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED, |
| 355 |
"Failed to scan topology: %s", |
| 356 |
strerror_l (errno, _C_LOCALE)); |
| 357 |
nvme_free_tree (root); |
| 358 |
return FALSE; |
| 359 |
} |
| 360 |
nvme_for_each_host (root, host) |
| 361 |
nvme_for_each_subsystem (host, subsys) |
| 362 |
if (!subsysnqn || g_strcmp0 (nvme_subsystem_get_nqn (subsys), subsysnqn) == 0) |
| 363 |
nvme_subsystem_for_each_ctrl (subsys, ctrl) |
| 364 |
if (!path || g_strcmp0 (nvme_ctrl_get_name (ctrl), path) == 0) { |
| 365 |
ret = nvme_disconnect_ctrl (ctrl); |
| 366 |
if (ret != 0) { |
| 367 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_FAILED, |
| 368 |
"Error disconnecting the controller: %s", |
| 369 |
strerror_l (errno, _C_LOCALE)); |
| 370 |
nvme_free_tree (root); |
| 371 |
return FALSE; |
| 372 |
} |
| 373 |
*found = TRUE; |
| 374 |
} |
| 375 |
nvme_free_tree (root); |
| 376 |
return TRUE; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* bd_nvme_disconnect: |
| 381 |
* @subsysnqn: The name of the NVMe subsystem to disconnect. |
| 382 |
* @error: (out) (nullable): Place to store error (if any). |
| 383 |
* |
| 384 |
* Disconnects and removes one or more existing NVMe over Fabrics controllers. |
| 385 |
* This may disconnect multiple controllers with matching @subsysnqn and %TRUE |
| 386 |
* is only returned when all controllers were disconnected successfully. |
| 387 |
* |
| 388 |
* Returns: %TRUE if all matching controllers were disconnected successfully, %FALSE with @error |
| 389 |
* set in case of a disconnect error or when no matching controllers were found. |
| 390 |
* |
| 391 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 392 |
*/ |
| 393 |
gboolean bd_nvme_disconnect (const gchar *subsysnqn, GError **error) { |
| 394 |
gboolean found = FALSE; |
| 395 |
|
| 396 |
if (!_disconnect (subsysnqn, NULL, error, &found)) |
| 397 |
return FALSE; |
| 398 |
|
| 399 |
if (!found) { |
| 400 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_NO_MATCH, |
| 401 |
"No subsystems matching '%s' NQN found.", subsysnqn); |
| 402 |
return FALSE; |
| 403 |
} |
| 404 |
return TRUE; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* bd_nvme_disconnect_by_path: |
| 409 |
* @path: NVMe controller device to disconnect (e.g. `/dev/nvme0`). |
| 410 |
* @error: (out) (nullable): Place to store error (if any). |
| 411 |
* |
| 412 |
* Disconnects and removes a NVMe over Fabrics controller represented |
| 413 |
* by a block device path. |
| 414 |
* |
| 415 |
* Returns: %TRUE if the controller was disconnected successfully, |
| 416 |
* %FALSE otherwise with @error set. |
| 417 |
* |
| 418 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 419 |
*/ |
| 420 |
gboolean bd_nvme_disconnect_by_path (const gchar *path, GError **error) { |
| 421 |
const gchar *p; |
| 422 |
gboolean found = FALSE; |
| 423 |
|
| 424 |
p = path; |
| 425 |
if (g_str_has_prefix (p, "/dev/")) |
| 426 |
p += 5; |
| 427 |
|
| 428 |
if (!_disconnect (NULL, p, error, &found)) |
| 429 |
return FALSE; |
| 430 |
|
| 431 |
if (!found) { |
| 432 |
g_set_error (error, BD_NVME_ERROR, BD_NVME_ERROR_NO_MATCH, |
| 433 |
"No controllers matching the %s device name found.", path); |
| 434 |
return FALSE; |
| 435 |
} |
| 436 |
return TRUE; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
/** |
| 441 |
* bd_nvme_find_ctrls_for_ns: |
| 442 |
* @ns_sysfs_path: NVMe namespace sysfs path. |
| 443 |
* @subsysnqn: (nullable): Limit matching to the specified subsystem NQN. |
| 444 |
* @host_nqn: (nullable): Limit matching to the specified host NQN. |
| 445 |
* @host_id: (nullable): Limit matching to the specified host ID. |
| 446 |
* @error: (out) (nullable): Place to store error (if any). |
| 447 |
* |
| 448 |
* A convenient utility function to look up all controllers associated |
| 449 |
* with a NVMe subsystem the specified namespace is part of. |
| 450 |
* |
| 451 |
* Returns: (transfer full) (array zero-terminated=1): list of controller sysfs paths |
| 452 |
* or %NULL in case of an error (with @error set). |
| 453 |
* |
| 454 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 455 |
*/ |
| 456 |
gchar ** bd_nvme_find_ctrls_for_ns (const gchar *ns_sysfs_path, const gchar *subsysnqn, const gchar *host_nqn, const gchar *host_id, GError **error G_GNUC_UNUSED) { |
| 457 |
GPtrArray *ptr_array; |
| 458 |
nvme_root_t root; |
| 459 |
nvme_host_t h; |
| 460 |
nvme_subsystem_t s; |
| 461 |
nvme_ctrl_t c; |
| 462 |
nvme_ns_t n; |
| 463 |
char realp[PATH_MAX]; |
| 464 |
gchar *subsysnqn_p; |
| 465 |
|
| 466 |
/* libnvme strips trailing spaces and newlines when reading values from sysfs */ |
| 467 |
subsysnqn_p = g_strdup (subsysnqn); |
| 468 |
if (subsysnqn_p) |
| 469 |
g_strchomp (subsysnqn_p); |
| 470 |
|
| 471 |
ptr_array = g_ptr_array_new (); |
| 472 |
|
| 473 |
root = nvme_scan (NULL); |
| 474 |
g_warn_if_fail (root != NULL); |
| 475 |
|
| 476 |
nvme_for_each_host (root, h) { |
| 477 |
if (host_nqn && g_strcmp0 (nvme_host_get_hostnqn (h), host_nqn) != 0) |
| 478 |
continue; |
| 479 |
if (host_id && g_strcmp0 (nvme_host_get_hostid (h), host_id) != 0) |
| 480 |
continue; |
| 481 |
|
| 482 |
nvme_for_each_subsystem (h, s) { |
| 483 |
gboolean found = FALSE; |
| 484 |
|
| 485 |
if (subsysnqn && g_strcmp0 (nvme_subsystem_get_nqn (s), subsysnqn_p) != 0) |
| 486 |
continue; |
| 487 |
|
| 488 |
nvme_subsystem_for_each_ctrl (s, c) |
| 489 |
nvme_ctrl_for_each_ns (c, n) |
| 490 |
if (realpath (nvme_ns_get_sysfs_dir (n), realp) && |
| 491 |
g_strcmp0 (realp, ns_sysfs_path) == 0) { |
| 492 |
if (realpath (nvme_ctrl_get_sysfs_dir (c), realp)) { |
| 493 |
g_ptr_array_add (ptr_array, g_strdup (realp)); |
| 494 |
break; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
nvme_subsystem_for_each_ns (s, n) |
| 499 |
if (realpath (nvme_ns_get_sysfs_dir (n), realp) && |
| 500 |
g_strcmp0 (realp, ns_sysfs_path) == 0) { |
| 501 |
found = TRUE; |
| 502 |
/* at least one of the namespaces match, don't care about the rest */ |
| 503 |
break; |
| 504 |
} |
| 505 |
|
| 506 |
if (found) |
| 507 |
/* add all controllers in the subsystem */ |
| 508 |
nvme_subsystem_for_each_ctrl (s, c) { |
| 509 |
if (realpath (nvme_ctrl_get_sysfs_dir (c), realp)) { |
| 510 |
g_ptr_array_add (ptr_array, g_strdup (realp)); |
| 511 |
} |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
nvme_free_tree (root); |
| 516 |
g_free (subsysnqn_p); |
| 517 |
|
| 518 |
g_ptr_array_add (ptr_array, NULL); /* trailing NULL element */ |
| 519 |
return (gchar **) g_ptr_array_free (ptr_array, FALSE); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
/** |
| 524 |
* bd_nvme_find_namespaces_for_ctrl: |
| 525 |
* @ctrl_sysfs_path: NVMe controller sysfs path. |
| 526 |
* @subsysnqn: (nullable): Limit matching to the specified subsystem NQN. |
| 527 |
* @host_nqn: (nullable): Limit matching to the specified host NQN. |
| 528 |
* @host_id: (nullable): Limit matching to the specified host ID. |
| 529 |
* @error: (out) (nullable): Place to store error (if any). |
| 530 |
* |
| 531 |
* A convenient utility function to look up all namespaces associated |
| 532 |
* with the specified NVMe controller. |
| 533 |
* |
| 534 |
* Returns: (transfer full) (array zero-terminated=1): list of namespace sysfs paths |
| 535 |
* or %NULL in case of an error (with @error set). |
| 536 |
* |
| 537 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 538 |
*/ |
| 539 |
gchar ** bd_nvme_find_namespaces_for_ctrl (const gchar *ctrl_sysfs_path, const gchar *subsysnqn, const gchar *host_nqn, const gchar *host_id, GError **error G_GNUC_UNUSED) { |
| 540 |
GPtrArray *ptr_array; |
| 541 |
nvme_root_t root; |
| 542 |
nvme_host_t h; |
| 543 |
nvme_subsystem_t s; |
| 544 |
nvme_ctrl_t c; |
| 545 |
nvme_ns_t n; |
| 546 |
char realp[PATH_MAX]; |
| 547 |
gchar *subsysnqn_p; |
| 548 |
|
| 549 |
/* libnvme strips trailing spaces and newlines when reading values from sysfs */ |
| 550 |
subsysnqn_p = g_strdup (subsysnqn); |
| 551 |
if (subsysnqn_p) |
| 552 |
g_strchomp (subsysnqn_p); |
| 553 |
|
| 554 |
ptr_array = g_ptr_array_new (); |
| 555 |
|
| 556 |
root = nvme_scan (NULL); |
| 557 |
g_warn_if_fail (root != NULL); |
| 558 |
|
| 559 |
nvme_for_each_host (root, h) { |
| 560 |
if (host_nqn && g_strcmp0 (nvme_host_get_hostnqn (h), host_nqn) != 0) |
| 561 |
continue; |
| 562 |
if (host_id && g_strcmp0 (nvme_host_get_hostid (h), host_id) != 0) |
| 563 |
continue; |
| 564 |
|
| 565 |
nvme_for_each_subsystem (h, s) { |
| 566 |
if (subsysnqn && g_strcmp0 (nvme_subsystem_get_nqn (s), subsysnqn_p) != 0) |
| 567 |
continue; |
| 568 |
|
| 569 |
nvme_subsystem_for_each_ctrl (s, c) { |
| 570 |
if (realpath (nvme_ctrl_get_sysfs_dir (c), realp) && |
| 571 |
g_strcmp0 (realp, ctrl_sysfs_path) == 0) { |
| 572 |
/* Found matching controller, add all its namespaces */ |
| 573 |
nvme_ctrl_for_each_ns (c, n) { |
| 574 |
if (realpath (nvme_ns_get_sysfs_dir (n), realp)) { |
| 575 |
g_ptr_array_add (ptr_array, g_strdup (realp)); |
| 576 |
} |
| 577 |
} |
| 578 |
/* Also check subsystem-level namespaces */ |
| 579 |
nvme_subsystem_for_each_ns (s, n) { |
| 580 |
if (realpath (nvme_ns_get_sysfs_dir (n), realp)) { |
| 581 |
g_ptr_array_add (ptr_array, g_strdup (realp)); |
| 582 |
} |
| 583 |
} |
| 584 |
break; |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
nvme_free_tree (root); |
| 590 |
g_free (subsysnqn_p); |
| 591 |
|
| 592 |
g_ptr_array_add (ptr_array, NULL); /* trailing NULL element */ |
| 593 |
return (gchar **) g_ptr_array_free (ptr_array, FALSE); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
/** |
| 598 |
* bd_nvme_get_host_nqn: |
| 599 |
* @error: (out) (nullable): Place to store error (if any). |
| 600 |
* |
| 601 |
* Reads the Host NQN (NVM Qualified Name) value from the global `/etc/nvme/hostnqn` |
| 602 |
* file. An empty string is an indication that no Host NQN has been set. |
| 603 |
* |
| 604 |
* Returns: (transfer full): the Host NQN string or an empty string if none set. |
| 605 |
* |
| 606 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 607 |
*/ |
| 608 |
gchar * bd_nvme_get_host_nqn (G_GNUC_UNUSED GError **error) { |
| 609 |
char *hostnqn; |
| 610 |
|
| 611 |
/* FIXME: libnvme SYSCONFDIR might be different from PACKAGE_SYSCONF_DIR */ |
| 612 |
hostnqn = nvmf_hostnqn_from_file (); |
| 613 |
return hostnqn ? hostnqn : g_strdup (""); |
| 614 |
} |
| 615 |
|
| 616 |
/** |
| 617 |
* bd_nvme_generate_host_nqn: |
| 618 |
* @error: (out) (nullable): Place to store error (if any). |
| 619 |
* |
| 620 |
* Compute new Host NQN (NVM Qualified Name) value for the current system. This |
| 621 |
* takes in account various system identifiers (DMI, device tree) with the goal |
| 622 |
* of a stable unique identifier whenever feasible. |
| 623 |
* |
| 624 |
* Returns: (transfer full): the Host NQN string or %NULL with @error set. |
| 625 |
* |
| 626 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 627 |
*/ |
| 628 |
gchar * bd_nvme_generate_host_nqn (GError **error) { |
| 629 |
char *nqn; |
| 630 |
|
| 631 |
nqn = nvmf_hostnqn_generate (); |
| 632 |
if (!nqn) |
| 633 |
g_set_error_literal (error, BD_NVME_ERROR, BD_NVME_ERROR_INVALID_ARGUMENT, |
| 634 |
"Unable to generate Host NQN."); |
| 635 |
|
| 636 |
return nqn; |
| 637 |
} |
| 638 |
|
| 639 |
/** |
| 640 |
* bd_nvme_get_host_id: |
| 641 |
* @error: (out) (nullable): Place to store error (if any). |
| 642 |
* |
| 643 |
* Reads the Host ID value from the global `/etc/nvme/hostid` file. An empty |
| 644 |
* string is an indication that no Host ID has been set. |
| 645 |
* |
| 646 |
* Returns: (transfer full): the Host ID string or an empty string if none set. |
| 647 |
* |
| 648 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 649 |
*/ |
| 650 |
gchar * bd_nvme_get_host_id (G_GNUC_UNUSED GError **error) { |
| 651 |
char *hostid; |
| 652 |
|
| 653 |
hostid = nvmf_hostid_from_file (); |
| 654 |
return hostid ? hostid : g_strdup (""); |
| 655 |
} |
| 656 |
|
| 657 |
/** |
| 658 |
* bd_nvme_set_host_nqn: |
| 659 |
* @host_nqn: The Host NVM Qualified Name. |
| 660 |
* @error: (out) (nullable): Place to store error (if any). |
| 661 |
* |
| 662 |
* Writes the Host NQN (NVM Qualified Name) value to the system `/etc/nvme/hostnqn` file. |
| 663 |
* No validation of the string is performed. |
| 664 |
* |
| 665 |
* Returns: %TRUE if the value was set successfully or %FALSE otherwise with @error set. |
| 666 |
* |
| 667 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 668 |
*/ |
| 669 |
gboolean bd_nvme_set_host_nqn (const gchar *host_nqn, GError **error) { |
| 670 |
gchar *path; |
| 671 |
gchar *filename; |
| 672 |
gchar *s; |
| 673 |
gboolean ret; |
| 674 |
|
| 675 |
g_return_val_if_fail (host_nqn != NULL, FALSE); |
| 676 |
|
| 677 |
path = g_build_path (G_DIR_SEPARATOR_S, PACKAGE_SYSCONF_DIR, "nvme", NULL); |
| 678 |
if (g_mkdir_with_parents (path, 0755) != 0 && errno != EEXIST) { |
| 679 |
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), |
| 680 |
"Error creating %s: %s", |
| 681 |
path, strerror_l (errno, _C_LOCALE)); |
| 682 |
g_free (path); |
| 683 |
return FALSE; |
| 684 |
} |
| 685 |
filename = g_build_filename (path, "hostnqn", NULL); |
| 686 |
if (strlen (host_nqn) == 0 || host_nqn[strlen (host_nqn) - 1] != '\n') |
| 687 |
s = g_strdup_printf ("%s\n", host_nqn); |
| 688 |
else |
| 689 |
s = g_strdup (host_nqn); |
| 690 |
ret = g_file_set_contents (filename, s, -1, error); |
| 691 |
if (ret) |
| 692 |
g_chmod (filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 693 |
|
| 694 |
g_free (s); |
| 695 |
g_free (path); |
| 696 |
g_free (filename); |
| 697 |
|
| 698 |
return ret; |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* bd_nvme_set_host_id: |
| 703 |
* @host_id: The Host ID. |
| 704 |
* @error: (out) (nullable): Place to store error (if any). |
| 705 |
* |
| 706 |
* Writes the Host ID value to the system `/etc/nvme/hostid` file. |
| 707 |
* No validation of the string is performed. |
| 708 |
* |
| 709 |
* Returns: %TRUE if the value was set successfully or %FALSE otherwise with @error set. |
| 710 |
* |
| 711 |
* Tech category: %BD_NVME_TECH_FABRICS-%BD_NVME_TECH_MODE_INITIATOR |
| 712 |
*/ |
| 713 |
gboolean bd_nvme_set_host_id (const gchar *host_id, GError **error) { |
| 714 |
gchar *path; |
| 715 |
gchar *filename; |
| 716 |
gchar *s; |
| 717 |
gboolean ret; |
| 718 |
|
| 719 |
g_return_val_if_fail (host_id != NULL, FALSE); |
| 720 |
|
| 721 |
path = g_build_path (G_DIR_SEPARATOR_S, PACKAGE_SYSCONF_DIR, "nvme", NULL); |
| 722 |
if (g_mkdir_with_parents (path, 0755) != 0 && errno != EEXIST) { |
| 723 |
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), |
| 724 |
"Error creating %s: %s", |
| 725 |
path, strerror_l (errno, _C_LOCALE)); |
| 726 |
g_free (path); |
| 727 |
return FALSE; |
| 728 |
} |
| 729 |
filename = g_build_filename (path, "hostid", NULL); |
| 730 |
if (strlen (host_id) == 0 || host_id[strlen (host_id) - 1] != '\n') |
| 731 |
s = g_strdup_printf ("%s\n", host_id); |
| 732 |
else |
| 733 |
s = g_strdup (host_id); |
| 734 |
ret = g_file_set_contents (filename, s, -1, error); |
| 735 |
if (ret) |
| 736 |
g_chmod (filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 737 |
|
| 738 |
g_free (s); |
| 739 |
g_free (path); |
| 740 |
g_free (filename); |
| 741 |
|
| 742 |
return ret; |
| 743 |
} |