|
| 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 | + |
| 10 | +#include <libnvme.h> |
| 11 | + |
| 12 | +#include "cleanup.h" |
| 13 | +#include "private.h" |
| 14 | + |
| 15 | +#include <fcntl.h> |
| 16 | +#include <sys/ioctl.h> |
| 17 | + |
| 18 | +struct nvme_global_ctx *nvme_create_global_ctx(FILE *fp, int log_level) |
| 19 | +{ |
| 20 | + struct nvme_global_ctx *ctx; |
| 21 | + int fd; |
| 22 | + |
| 23 | + ctx = calloc(1, sizeof(*ctx)); |
| 24 | + if (!ctx) |
| 25 | + return NULL; |
| 26 | + |
| 27 | + if (fp) { |
| 28 | + fd = fileno(fp); |
| 29 | + if (fd < 0) { |
| 30 | + free(ctx); |
| 31 | + return NULL; |
| 32 | + } |
| 33 | + } else |
| 34 | + fd = STDERR_FILENO; |
| 35 | + |
| 36 | + ctx->log.fd = fd; |
| 37 | + ctx->log.level = log_level; |
| 38 | + |
| 39 | + list_head_init(&ctx->hosts); |
| 40 | + list_head_init(&ctx->endpoints); |
| 41 | + |
| 42 | + ctx->ioctl_probing = true; |
| 43 | + |
| 44 | + return ctx; |
| 45 | +} |
| 46 | + |
| 47 | +void nvme_free_global_ctx(struct nvme_global_ctx *ctx) |
| 48 | +{ |
| 49 | + struct nvme_host *h, *_h; |
| 50 | + |
| 51 | + if (!ctx) |
| 52 | + return; |
| 53 | + |
| 54 | + freeifaddrs(ctx->ifaddrs_cache); /* NULL-safe */ |
| 55 | + ctx->ifaddrs_cache = NULL; |
| 56 | + |
| 57 | + free(ctx->options); |
| 58 | + nvme_for_each_host_safe(ctx, h, _h) |
| 59 | + __nvme_free_host(h); |
| 60 | + free(ctx->config_file); |
| 61 | + free(ctx->application); |
| 62 | + free(ctx); |
| 63 | +} |
| 64 | + |
| 65 | +void nvme_set_dry_run(struct nvme_global_ctx *ctx, bool enable) |
| 66 | +{ |
| 67 | + ctx->dry_run = enable; |
| 68 | +} |
| 69 | + |
| 70 | +void nvme_set_ioctl_probing(struct nvme_global_ctx *ctx, bool enable) |
| 71 | +{ |
| 72 | + ctx->ioctl_probing = enable; |
| 73 | +} |
| 74 | + |
| 75 | +void nvme_transport_handle_set_submit_entry(struct nvme_transport_handle *hdl, |
| 76 | + void *(*submit_entry)(struct nvme_transport_handle *hdl, |
| 77 | + struct nvme_passthru_cmd *cmd)) |
| 78 | +{ |
| 79 | + hdl->submit_entry = submit_entry; |
| 80 | + if (!hdl->submit_exit) |
| 81 | + hdl->submit_exit = __nvme_submit_exit; |
| 82 | +} |
| 83 | + |
| 84 | +void nvme_transport_handle_set_submit_exit(struct nvme_transport_handle *hdl, |
| 85 | + void (*submit_exit)(struct nvme_transport_handle *hdl, |
| 86 | + struct nvme_passthru_cmd *cmd, |
| 87 | + int err, void *user_data)) |
| 88 | +{ |
| 89 | + hdl->submit_exit = submit_exit; |
| 90 | + if (!hdl->submit_exit) |
| 91 | + hdl->submit_exit = __nvme_submit_exit; |
| 92 | +} |
| 93 | + |
| 94 | +void nvme_transport_handle_set_decide_retry(struct nvme_transport_handle *hdl, |
| 95 | + bool (*decide_retry)(struct nvme_transport_handle *hdl, |
| 96 | + struct nvme_passthru_cmd *cmd, int err)) |
| 97 | +{ |
| 98 | + hdl->decide_retry = decide_retry; |
| 99 | + if (!hdl->decide_retry) |
| 100 | + hdl->decide_retry = __nvme_decide_retry; |
| 101 | +} |
| 102 | + |
| 103 | +static int __nvme_transport_handle_open_direct(struct nvme_transport_handle *hdl, const char *devname) |
| 104 | +{ |
| 105 | + struct nvme_passthru_cmd dummy = { 0 }; |
| 106 | + _cleanup_free_ char *path = NULL; |
| 107 | + char *name = basename(devname); |
| 108 | + int ret, id, ns; |
| 109 | + bool c = true; |
| 110 | + |
| 111 | + hdl->type = NVME_TRANSPORT_HANDLE_TYPE_DIRECT; |
| 112 | + |
| 113 | + ret = sscanf(name, "nvme%dn%d", &id, &ns); |
| 114 | + if (ret == 2) |
| 115 | + c = false; |
| 116 | + else if (ret != 1 && sscanf(name, "ng%dn%d", &id, &ns) != 2) |
| 117 | + return -EINVAL; |
| 118 | + |
| 119 | + ret = asprintf(&path, "%s/%s", "/dev", name); |
| 120 | + if (ret < 0) |
| 121 | + return -ENOMEM; |
| 122 | + |
| 123 | + hdl->fd = open(path, O_RDONLY); |
| 124 | + if (hdl->fd < 0) |
| 125 | + return -errno; |
| 126 | + |
| 127 | + ret = fstat(hdl->fd, &hdl->stat); |
| 128 | + if (ret < 0) |
| 129 | + return -errno; |
| 130 | + |
| 131 | + if (c) { |
| 132 | + if (!S_ISCHR(hdl->stat.st_mode)) |
| 133 | + return -EINVAL; |
| 134 | + } else if (!S_ISBLK(hdl->stat.st_mode)) { |
| 135 | + return -EINVAL; |
| 136 | + } |
| 137 | + |
| 138 | + if (hdl->ctx->ioctl_probing) { |
| 139 | + ret = ioctl(hdl->fd, NVME_IOCTL_ADMIN64_CMD, &dummy); |
| 140 | + if (ret > 0) |
| 141 | + hdl->ioctl64 = true; |
| 142 | + } |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +void __nvme_transport_handle_close_direct(struct nvme_transport_handle *hdl) |
| 148 | +{ |
| 149 | + close(hdl->fd); |
| 150 | + free(hdl); |
| 151 | +} |
| 152 | + |
| 153 | +struct nvme_transport_handle *__nvme_create_transport_handle(struct nvme_global_ctx *ctx) |
| 154 | +{ |
| 155 | + struct nvme_transport_handle *hdl; |
| 156 | + |
| 157 | + hdl = calloc(1, sizeof(*hdl)); |
| 158 | + if (!hdl) |
| 159 | + return NULL; |
| 160 | + |
| 161 | + hdl->ctx = ctx; |
| 162 | + hdl->submit_entry = __nvme_submit_entry; |
| 163 | + hdl->submit_exit = __nvme_submit_exit; |
| 164 | + hdl->decide_retry = __nvme_decide_retry; |
| 165 | + |
| 166 | + return hdl; |
| 167 | +} |
| 168 | + |
| 169 | +int nvme_open(struct nvme_global_ctx *ctx, const char *name, |
| 170 | + struct nvme_transport_handle **hdlp) |
| 171 | +{ |
| 172 | + struct nvme_transport_handle *hdl; |
| 173 | + int ret; |
| 174 | + |
| 175 | + hdl = __nvme_create_transport_handle(ctx); |
| 176 | + if (!hdl) |
| 177 | + return -ENOMEM; |
| 178 | + |
| 179 | + hdl->name = strdup(name); |
| 180 | + if (!hdl->name) { |
| 181 | + free(hdl); |
| 182 | + return -ENOMEM; |
| 183 | + } |
| 184 | + |
| 185 | + if (!strncmp(name, "NVME_TEST_FD", 12)) { |
| 186 | + hdl->type = NVME_TRANSPORT_HANDLE_TYPE_DIRECT; |
| 187 | + hdl->fd = 0xFD; |
| 188 | + |
| 189 | + if (!strcmp(name, "NVME_TEST_FD64")) |
| 190 | + hdl->ioctl64 = true; |
| 191 | + |
| 192 | + *hdlp = hdl; |
| 193 | + return 0; |
| 194 | + } |
| 195 | + |
| 196 | + if (!strncmp(name, "mctp:", strlen("mctp:"))) |
| 197 | + ret = __nvme_transport_handle_open_mi(hdl, name); |
| 198 | + else |
| 199 | + ret = __nvme_transport_handle_open_direct(hdl, name); |
| 200 | + |
| 201 | + if (ret) { |
| 202 | + nvme_close(hdl); |
| 203 | + return ret; |
| 204 | + } |
| 205 | + |
| 206 | + *hdlp = hdl; |
| 207 | + |
| 208 | + return 0; |
| 209 | +} |
| 210 | + |
| 211 | +void nvme_close(struct nvme_transport_handle *hdl) |
| 212 | +{ |
| 213 | + if (!hdl) |
| 214 | + return; |
| 215 | + |
| 216 | + free(hdl->name); |
| 217 | + |
| 218 | + switch (hdl->type) { |
| 219 | + case NVME_TRANSPORT_HANDLE_TYPE_DIRECT: |
| 220 | + __nvme_transport_handle_close_direct(hdl); |
| 221 | + break; |
| 222 | + case NVME_TRANSPORT_HANDLE_TYPE_MI: |
| 223 | + __nvme_transport_handle_close_mi(hdl); |
| 224 | + break; |
| 225 | + case NVME_TRANSPORT_HANDLE_TYPE_UNKNOWN: |
| 226 | + free(hdl); |
| 227 | + break; |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +int nvme_transport_handle_get_fd(struct nvme_transport_handle *hdl) |
| 232 | +{ |
| 233 | + return hdl->fd; |
| 234 | +} |
| 235 | + |
| 236 | +const char *nvme_transport_handle_get_name(struct nvme_transport_handle *hdl) |
| 237 | +{ |
| 238 | + return basename(hdl->name); |
| 239 | +} |
| 240 | + |
| 241 | +bool nvme_transport_handle_is_blkdev(struct nvme_transport_handle *hdl) |
| 242 | +{ |
| 243 | + return S_ISBLK(hdl->stat.st_mode); |
| 244 | +} |
| 245 | + |
| 246 | +bool nvme_transport_handle_is_chardev(struct nvme_transport_handle *hdl) |
| 247 | +{ |
| 248 | + return S_ISCHR(hdl->stat.st_mode); |
| 249 | +} |
| 250 | + |
| 251 | +bool nvme_transport_handle_is_direct(struct nvme_transport_handle *hdl) |
| 252 | +{ |
| 253 | + return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_DIRECT; |
| 254 | +} |
| 255 | + |
| 256 | +bool nvme_transport_handle_is_mi(struct nvme_transport_handle *hdl) |
| 257 | +{ |
| 258 | + return hdl->type == NVME_TRANSPORT_HANDLE_TYPE_MI; |
| 259 | +} |
| 260 | + |
0 commit comments