1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2018 Intel Corporation. 3 * All rights reserved. 4 * Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 5 */ 6 7 /* 8 * This is a simple example of a virtual block device module that passes IO 9 * down to a bdev (or bdevs) that its configured to attach to. 10 */ 11 12 #include "spdk/stdinc.h" 13 14 #include "vbdev_passthru.h" 15 #include "spdk/rpc.h" 16 #include "spdk/env.h" 17 #include "spdk/endian.h" 18 #include "spdk/string.h" 19 #include "spdk/thread.h" 20 #include "spdk/util.h" 21 22 #include "spdk/bdev_module.h" 23 #include "spdk/log.h" 24 25 /* This namespace UUID was generated using uuid_generate() method. */ 26 #define BDEV_PASSTHRU_NAMESPACE_UUID "7e25812e-c8c0-4d3f-8599-16d790555b85" 27 28 static int vbdev_passthru_init(void); 29 static int vbdev_passthru_get_ctx_size(void); 30 static void vbdev_passthru_examine(struct spdk_bdev *bdev); 31 static void vbdev_passthru_finish(void); 32 static int vbdev_passthru_config_json(struct spdk_json_write_ctx *w); 33 34 static struct spdk_bdev_module passthru_if = { 35 .name = "passthru", 36 .module_init = vbdev_passthru_init, 37 .get_ctx_size = vbdev_passthru_get_ctx_size, 38 .examine_config = vbdev_passthru_examine, 39 .module_fini = vbdev_passthru_finish, 40 .config_json = vbdev_passthru_config_json 41 }; 42 43 SPDK_BDEV_MODULE_REGISTER(passthru, &passthru_if) 44 45 /* List of pt_bdev names and their base bdevs via configuration file. 46 * Used so we can parse the conf once at init and use this list in examine(). 47 */ 48 struct bdev_names { 49 char *vbdev_name; 50 char *bdev_name; 51 TAILQ_ENTRY(bdev_names) link; 52 }; 53 static TAILQ_HEAD(, bdev_names) g_bdev_names = TAILQ_HEAD_INITIALIZER(g_bdev_names); 54 55 /* List of virtual bdevs and associated info for each. */ 56 struct vbdev_passthru { 57 struct spdk_bdev *base_bdev; /* the thing we're attaching to */ 58 struct spdk_bdev_desc *base_desc; /* its descriptor we get from open */ 59 struct spdk_bdev pt_bdev; /* the PT virtual bdev */ 60 TAILQ_ENTRY(vbdev_passthru) link; 61 struct spdk_thread *thread; /* thread where base device is opened */ 62 }; 63 static TAILQ_HEAD(, vbdev_passthru) g_pt_nodes = TAILQ_HEAD_INITIALIZER(g_pt_nodes); 64 65 /* The pt vbdev channel struct. It is allocated and freed on my behalf by the io channel code. 66 * If this vbdev needed to implement a poller or a queue for IO, this is where those things 67 * would be defined. This passthru bdev doesn't actually need to allocate a channel, it could 68 * simply pass back the channel of the bdev underneath it but for example purposes we will 69 * present its own to the upper layers. 70 */ 71 struct pt_io_channel { 72 struct spdk_io_channel *base_ch; /* IO channel of base device */ 73 }; 74 75 /* Just for fun, this pt_bdev module doesn't need it but this is essentially a per IO 76 * context that we get handed by the bdev layer. 77 */ 78 struct passthru_bdev_io { 79 uint8_t test; 80 81 /* bdev related */ 82 struct spdk_io_channel *ch; 83 84 /* for bdev_io_wait */ 85 struct spdk_bdev_io_wait_entry bdev_io_wait; 86 }; 87 88 static void vbdev_passthru_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io); 89 90 91 /* Callback for unregistering the IO device. */ 92 static void 93 _device_unregister_cb(void *io_device) 94 { 95 struct vbdev_passthru *pt_node = io_device; 96 97 /* Done with this pt_node. */ 98 free(pt_node->pt_bdev.name); 99 free(pt_node); 100 } 101 102 /* Wrapper for the bdev close operation. */ 103 static void 104 _vbdev_passthru_destruct(void *ctx) 105 { 106 struct spdk_bdev_desc *desc = ctx; 107 108 spdk_bdev_close(desc); 109 } 110 111 /* Called after we've unregistered following a hot remove callback. 112 * Our finish entry point will be called next. 113 */ 114 static int 115 vbdev_passthru_destruct(void *ctx) 116 { 117 struct vbdev_passthru *pt_node = (struct vbdev_passthru *)ctx; 118 119 /* It is important to follow this exact sequence of steps for destroying 120 * a vbdev... 121 */ 122 123 TAILQ_REMOVE(&g_pt_nodes, pt_node, link); 124 125 /* Unclaim the underlying bdev. */ 126 spdk_bdev_module_release_bdev(pt_node->base_bdev); 127 128 /* Close the underlying bdev on its same opened thread. */ 129 if (pt_node->thread && pt_node->thread != spdk_get_thread()) { 130 spdk_thread_send_msg(pt_node->thread, _vbdev_passthru_destruct, pt_node->base_desc); 131 } else { 132 spdk_bdev_close(pt_node->base_desc); 133 } 134 135 /* Unregister the io_device. */ 136 spdk_io_device_unregister(pt_node, _device_unregister_cb); 137 138 return 0; 139 } 140 141 /* Completion callback for IO that were issued from this bdev. The original bdev_io 142 * is passed in as an arg so we'll complete that one with the appropriate status 143 * and then free the one that this module issued. 144 */ 145 static void 146 _pt_complete_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 147 { 148 struct spdk_bdev_io *orig_io = cb_arg; 149 int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 150 struct passthru_bdev_io *io_ctx = (struct passthru_bdev_io *)orig_io->driver_ctx; 151 152 /* We setup this value in the submission routine, just showing here that it is 153 * passed back to us. 154 */ 155 if (io_ctx->test != 0x5a) { 156 SPDK_ERRLOG("Error, original IO device_ctx is wrong! 0x%x\n", 157 io_ctx->test); 158 } 159 160 /* Complete the original IO and then free the one that we created here 161 * as a result of issuing an IO via submit_request. 162 */ 163 spdk_bdev_io_complete(orig_io, status); 164 spdk_bdev_free_io(bdev_io); 165 } 166 167 static void 168 _pt_complete_zcopy_io(struct spdk_bdev_io *bdev_io, bool success, void *cb_arg) 169 { 170 struct spdk_bdev_io *orig_io = cb_arg; 171 int status = success ? SPDK_BDEV_IO_STATUS_SUCCESS : SPDK_BDEV_IO_STATUS_FAILED; 172 struct passthru_bdev_io *io_ctx = (struct passthru_bdev_io *)orig_io->driver_ctx; 173 174 /* We setup this value in the submission routine, just showing here that it is 175 * passed back to us. 176 */ 177 if (io_ctx->test != 0x5a) { 178 SPDK_ERRLOG("Error, original IO device_ctx is wrong! 0x%x\n", 179 io_ctx->test); 180 } 181 182 /* Complete the original IO and then free the one that we created here 183 * as a result of issuing an IO via submit_request. 184 */ 185 spdk_bdev_io_set_buf(orig_io, bdev_io->u.bdev.iovs[0].iov_base, bdev_io->u.bdev.iovs[0].iov_len); 186 spdk_bdev_io_complete(orig_io, status); 187 spdk_bdev_free_io(bdev_io); 188 } 189 190 static void 191 vbdev_passthru_resubmit_io(void *arg) 192 { 193 struct spdk_bdev_io *bdev_io = (struct spdk_bdev_io *)arg; 194 struct passthru_bdev_io *io_ctx = (struct passthru_bdev_io *)bdev_io->driver_ctx; 195 196 vbdev_passthru_submit_request(io_ctx->ch, bdev_io); 197 } 198 199 static void 200 vbdev_passthru_queue_io(struct spdk_bdev_io *bdev_io) 201 { 202 struct passthru_bdev_io *io_ctx = (struct passthru_bdev_io *)bdev_io->driver_ctx; 203 struct pt_io_channel *pt_ch = spdk_io_channel_get_ctx(io_ctx->ch); 204 int rc; 205 206 io_ctx->bdev_io_wait.bdev = bdev_io->bdev; 207 io_ctx->bdev_io_wait.cb_fn = vbdev_passthru_resubmit_io; 208 io_ctx->bdev_io_wait.cb_arg = bdev_io; 209 210 /* Queue the IO using the channel of the base device. */ 211 rc = spdk_bdev_queue_io_wait(bdev_io->bdev, pt_ch->base_ch, &io_ctx->bdev_io_wait); 212 if (rc != 0) { 213 SPDK_ERRLOG("Queue io failed in vbdev_passthru_queue_io, rc=%d.\n", rc); 214 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 215 } 216 } 217 218 static void 219 pt_init_ext_io_opts(struct spdk_bdev_io *bdev_io, struct spdk_bdev_ext_io_opts *opts) 220 { 221 memset(opts, 0, sizeof(*opts)); 222 opts->size = sizeof(*opts); 223 opts->memory_domain = bdev_io->u.bdev.memory_domain; 224 opts->memory_domain_ctx = bdev_io->u.bdev.memory_domain_ctx; 225 opts->metadata = bdev_io->u.bdev.md_buf; 226 } 227 228 /* Callback for getting a buf from the bdev pool in the event that the caller passed 229 * in NULL, we need to own the buffer so it doesn't get freed by another vbdev module 230 * beneath us before we're done with it. That won't happen in this example but it could 231 * if this example were used as a template for something more complex. 232 */ 233 static void 234 pt_read_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io, bool success) 235 { 236 struct vbdev_passthru *pt_node = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_passthru, 237 pt_bdev); 238 struct pt_io_channel *pt_ch = spdk_io_channel_get_ctx(ch); 239 struct passthru_bdev_io *io_ctx = (struct passthru_bdev_io *)bdev_io->driver_ctx; 240 struct spdk_bdev_ext_io_opts io_opts; 241 int rc; 242 243 if (!success) { 244 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 245 return; 246 } 247 248 pt_init_ext_io_opts(bdev_io, &io_opts); 249 rc = spdk_bdev_readv_blocks_ext(pt_node->base_desc, pt_ch->base_ch, bdev_io->u.bdev.iovs, 250 bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.offset_blocks, 251 bdev_io->u.bdev.num_blocks, _pt_complete_io, 252 bdev_io, &io_opts); 253 if (rc != 0) { 254 if (rc == -ENOMEM) { 255 SPDK_ERRLOG("No memory, start to queue io for passthru.\n"); 256 io_ctx->ch = ch; 257 vbdev_passthru_queue_io(bdev_io); 258 } else { 259 SPDK_ERRLOG("ERROR on bdev_io submission!\n"); 260 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 261 } 262 } 263 } 264 265 /* Called when someone above submits IO to this pt vbdev. We're simply passing it on here 266 * via SPDK IO calls which in turn allocate another bdev IO and call our cpl callback provided 267 * below along with the original bdev_io so that we can complete it once this IO completes. 268 */ 269 static void 270 vbdev_passthru_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io) 271 { 272 struct vbdev_passthru *pt_node = SPDK_CONTAINEROF(bdev_io->bdev, struct vbdev_passthru, pt_bdev); 273 struct pt_io_channel *pt_ch = spdk_io_channel_get_ctx(ch); 274 struct passthru_bdev_io *io_ctx = (struct passthru_bdev_io *)bdev_io->driver_ctx; 275 struct spdk_bdev_ext_io_opts io_opts; 276 int rc = 0; 277 278 /* Setup a per IO context value; we don't do anything with it in the vbdev other 279 * than confirm we get the same thing back in the completion callback just to 280 * demonstrate. 281 */ 282 io_ctx->test = 0x5a; 283 284 switch (bdev_io->type) { 285 case SPDK_BDEV_IO_TYPE_READ: 286 spdk_bdev_io_get_buf(bdev_io, pt_read_get_buf_cb, 287 bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen); 288 break; 289 case SPDK_BDEV_IO_TYPE_WRITE: 290 pt_init_ext_io_opts(bdev_io, &io_opts); 291 rc = spdk_bdev_writev_blocks_ext(pt_node->base_desc, pt_ch->base_ch, bdev_io->u.bdev.iovs, 292 bdev_io->u.bdev.iovcnt, bdev_io->u.bdev.offset_blocks, 293 bdev_io->u.bdev.num_blocks, _pt_complete_io, 294 bdev_io, &io_opts); 295 break; 296 case SPDK_BDEV_IO_TYPE_WRITE_ZEROES: 297 rc = spdk_bdev_write_zeroes_blocks(pt_node->base_desc, pt_ch->base_ch, 298 bdev_io->u.bdev.offset_blocks, 299 bdev_io->u.bdev.num_blocks, 300 _pt_complete_io, bdev_io); 301 break; 302 case SPDK_BDEV_IO_TYPE_UNMAP: 303 rc = spdk_bdev_unmap_blocks(pt_node->base_desc, pt_ch->base_ch, 304 bdev_io->u.bdev.offset_blocks, 305 bdev_io->u.bdev.num_blocks, 306 _pt_complete_io, bdev_io); 307 break; 308 case SPDK_BDEV_IO_TYPE_FLUSH: 309 rc = spdk_bdev_flush_blocks(pt_node->base_desc, pt_ch->base_ch, 310 bdev_io->u.bdev.offset_blocks, 311 bdev_io->u.bdev.num_blocks, 312 _pt_complete_io, bdev_io); 313 break; 314 case SPDK_BDEV_IO_TYPE_RESET: 315 rc = spdk_bdev_reset(pt_node->base_desc, pt_ch->base_ch, 316 _pt_complete_io, bdev_io); 317 break; 318 case SPDK_BDEV_IO_TYPE_ZCOPY: 319 rc = spdk_bdev_zcopy_start(pt_node->base_desc, pt_ch->base_ch, NULL, 0, 320 bdev_io->u.bdev.offset_blocks, 321 bdev_io->u.bdev.num_blocks, bdev_io->u.bdev.zcopy.populate, 322 _pt_complete_zcopy_io, bdev_io); 323 break; 324 case SPDK_BDEV_IO_TYPE_ABORT: 325 rc = spdk_bdev_abort(pt_node->base_desc, pt_ch->base_ch, bdev_io->u.abort.bio_to_abort, 326 _pt_complete_io, bdev_io); 327 break; 328 case SPDK_BDEV_IO_TYPE_COPY: 329 rc = spdk_bdev_copy_blocks(pt_node->base_desc, pt_ch->base_ch, 330 bdev_io->u.bdev.offset_blocks, 331 bdev_io->u.bdev.copy.src_offset_blocks, 332 bdev_io->u.bdev.num_blocks, 333 _pt_complete_io, bdev_io); 334 break; 335 default: 336 SPDK_ERRLOG("passthru: unknown I/O type %d\n", bdev_io->type); 337 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 338 return; 339 } 340 if (rc != 0) { 341 if (rc == -ENOMEM) { 342 SPDK_ERRLOG("No memory, start to queue io for passthru.\n"); 343 io_ctx->ch = ch; 344 vbdev_passthru_queue_io(bdev_io); 345 } else { 346 SPDK_ERRLOG("ERROR on bdev_io submission!\n"); 347 spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED); 348 } 349 } 350 } 351 352 /* We'll just call the base bdev and let it answer however if we were more 353 * restrictive for some reason (or less) we could get the response back 354 * and modify according to our purposes. 355 */ 356 static bool 357 vbdev_passthru_io_type_supported(void *ctx, enum spdk_bdev_io_type io_type) 358 { 359 struct vbdev_passthru *pt_node = (struct vbdev_passthru *)ctx; 360 361 return spdk_bdev_io_type_supported(pt_node->base_bdev, io_type); 362 } 363 364 /* We supplied this as an entry point for upper layers who want to communicate to this 365 * bdev. This is how they get a channel. We are passed the same context we provided when 366 * we created our PT vbdev in examine() which, for this bdev, is the address of one of 367 * our context nodes. From here we'll ask the SPDK channel code to fill out our channel 368 * struct and we'll keep it in our PT node. 369 */ 370 static struct spdk_io_channel * 371 vbdev_passthru_get_io_channel(void *ctx) 372 { 373 struct vbdev_passthru *pt_node = (struct vbdev_passthru *)ctx; 374 struct spdk_io_channel *pt_ch = NULL; 375 376 /* The IO channel code will allocate a channel for us which consists of 377 * the SPDK channel structure plus the size of our pt_io_channel struct 378 * that we passed in when we registered our IO device. It will then call 379 * our channel create callback to populate any elements that we need to 380 * update. 381 */ 382 pt_ch = spdk_get_io_channel(pt_node); 383 384 return pt_ch; 385 } 386 387 /* This is the output for bdev_get_bdevs() for this vbdev */ 388 static int 389 vbdev_passthru_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) 390 { 391 struct vbdev_passthru *pt_node = (struct vbdev_passthru *)ctx; 392 393 spdk_json_write_name(w, "passthru"); 394 spdk_json_write_object_begin(w); 395 spdk_json_write_named_string(w, "name", spdk_bdev_get_name(&pt_node->pt_bdev)); 396 spdk_json_write_named_string(w, "base_bdev_name", spdk_bdev_get_name(pt_node->base_bdev)); 397 spdk_json_write_object_end(w); 398 399 return 0; 400 } 401 402 /* This is used to generate JSON that can configure this module to its current state. */ 403 static int 404 vbdev_passthru_config_json(struct spdk_json_write_ctx *w) 405 { 406 struct vbdev_passthru *pt_node; 407 408 TAILQ_FOREACH(pt_node, &g_pt_nodes, link) { 409 spdk_json_write_object_begin(w); 410 spdk_json_write_named_string(w, "method", "bdev_passthru_create"); 411 spdk_json_write_named_object_begin(w, "params"); 412 spdk_json_write_named_string(w, "base_bdev_name", spdk_bdev_get_name(pt_node->base_bdev)); 413 spdk_json_write_named_string(w, "name", spdk_bdev_get_name(&pt_node->pt_bdev)); 414 spdk_json_write_object_end(w); 415 spdk_json_write_object_end(w); 416 } 417 return 0; 418 } 419 420 /* We provide this callback for the SPDK channel code to create a channel using 421 * the channel struct we provided in our module get_io_channel() entry point. Here 422 * we get and save off an underlying base channel of the device below us so that 423 * we can communicate with the base bdev on a per channel basis. If we needed 424 * our own poller for this vbdev, we'd register it here. 425 */ 426 static int 427 pt_bdev_ch_create_cb(void *io_device, void *ctx_buf) 428 { 429 struct pt_io_channel *pt_ch = ctx_buf; 430 struct vbdev_passthru *pt_node = io_device; 431 432 pt_ch->base_ch = spdk_bdev_get_io_channel(pt_node->base_desc); 433 434 return 0; 435 } 436 437 /* We provide this callback for the SPDK channel code to destroy a channel 438 * created with our create callback. We just need to undo anything we did 439 * when we created. If this bdev used its own poller, we'd unregister it here. 440 */ 441 static void 442 pt_bdev_ch_destroy_cb(void *io_device, void *ctx_buf) 443 { 444 struct pt_io_channel *pt_ch = ctx_buf; 445 446 spdk_put_io_channel(pt_ch->base_ch); 447 } 448 449 /* Create the passthru association from the bdev and vbdev name and insert 450 * on the global list. */ 451 static int 452 vbdev_passthru_insert_name(const char *bdev_name, const char *vbdev_name) 453 { 454 struct bdev_names *name; 455 456 TAILQ_FOREACH(name, &g_bdev_names, link) { 457 if (strcmp(vbdev_name, name->vbdev_name) == 0) { 458 SPDK_ERRLOG("passthru bdev %s already exists\n", vbdev_name); 459 return -EEXIST; 460 } 461 } 462 463 name = calloc(1, sizeof(struct bdev_names)); 464 if (!name) { 465 SPDK_ERRLOG("could not allocate bdev_names\n"); 466 return -ENOMEM; 467 } 468 469 name->bdev_name = strdup(bdev_name); 470 if (!name->bdev_name) { 471 SPDK_ERRLOG("could not allocate name->bdev_name\n"); 472 free(name); 473 return -ENOMEM; 474 } 475 476 name->vbdev_name = strdup(vbdev_name); 477 if (!name->vbdev_name) { 478 SPDK_ERRLOG("could not allocate name->vbdev_name\n"); 479 free(name->bdev_name); 480 free(name); 481 return -ENOMEM; 482 } 483 484 TAILQ_INSERT_TAIL(&g_bdev_names, name, link); 485 486 return 0; 487 } 488 489 /* On init, just perform bdev module specific initialization. */ 490 static int 491 vbdev_passthru_init(void) 492 { 493 return 0; 494 } 495 496 /* Called when the entire module is being torn down. */ 497 static void 498 vbdev_passthru_finish(void) 499 { 500 struct bdev_names *name; 501 502 while ((name = TAILQ_FIRST(&g_bdev_names))) { 503 TAILQ_REMOVE(&g_bdev_names, name, link); 504 free(name->bdev_name); 505 free(name->vbdev_name); 506 free(name); 507 } 508 } 509 510 /* During init we'll be asked how much memory we'd like passed to us 511 * in bev_io structures as context. Here's where we specify how 512 * much context we want per IO. 513 */ 514 static int 515 vbdev_passthru_get_ctx_size(void) 516 { 517 return sizeof(struct passthru_bdev_io); 518 } 519 520 /* Where vbdev_passthru_config_json() is used to generate per module JSON config data, this 521 * function is called to output any per bdev specific methods. For the PT module, there are 522 * none. 523 */ 524 static void 525 vbdev_passthru_write_config_json(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w) 526 { 527 /* No config per bdev needed */ 528 } 529 530 static int 531 vbdev_passthru_get_memory_domains(void *ctx, struct spdk_memory_domain **domains, int array_size) 532 { 533 struct vbdev_passthru *pt_node = (struct vbdev_passthru *)ctx; 534 535 /* Passthru bdev doesn't work with data buffers, so it supports any memory domain used by base_bdev */ 536 return spdk_bdev_get_memory_domains(pt_node->base_bdev, domains, array_size); 537 } 538 539 /* When we register our bdev this is how we specify our entry points. */ 540 static const struct spdk_bdev_fn_table vbdev_passthru_fn_table = { 541 .destruct = vbdev_passthru_destruct, 542 .submit_request = vbdev_passthru_submit_request, 543 .io_type_supported = vbdev_passthru_io_type_supported, 544 .get_io_channel = vbdev_passthru_get_io_channel, 545 .dump_info_json = vbdev_passthru_dump_info_json, 546 .write_config_json = vbdev_passthru_write_config_json, 547 .get_memory_domains = vbdev_passthru_get_memory_domains, 548 }; 549 550 static void 551 vbdev_passthru_base_bdev_hotremove_cb(struct spdk_bdev *bdev_find) 552 { 553 struct vbdev_passthru *pt_node, *tmp; 554 555 TAILQ_FOREACH_SAFE(pt_node, &g_pt_nodes, link, tmp) { 556 if (bdev_find == pt_node->base_bdev) { 557 spdk_bdev_unregister(&pt_node->pt_bdev, NULL, NULL); 558 } 559 } 560 } 561 562 /* Called when the underlying base bdev triggers asynchronous event such as bdev removal. */ 563 static void 564 vbdev_passthru_base_bdev_event_cb(enum spdk_bdev_event_type type, struct spdk_bdev *bdev, 565 void *event_ctx) 566 { 567 switch (type) { 568 case SPDK_BDEV_EVENT_REMOVE: 569 vbdev_passthru_base_bdev_hotremove_cb(bdev); 570 break; 571 default: 572 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 573 break; 574 } 575 } 576 577 /* Create and register the passthru vbdev if we find it in our list of bdev names. 578 * This can be called either by the examine path or RPC method. 579 */ 580 static int 581 vbdev_passthru_register(const char *bdev_name) 582 { 583 struct bdev_names *name; 584 struct vbdev_passthru *pt_node; 585 struct spdk_bdev *bdev; 586 struct spdk_uuid ns_uuid; 587 int rc = 0; 588 589 spdk_uuid_parse(&ns_uuid, BDEV_PASSTHRU_NAMESPACE_UUID); 590 591 /* Check our list of names from config versus this bdev and if 592 * there's a match, create the pt_node & bdev accordingly. 593 */ 594 TAILQ_FOREACH(name, &g_bdev_names, link) { 595 if (strcmp(name->bdev_name, bdev_name) != 0) { 596 continue; 597 } 598 599 SPDK_NOTICELOG("Match on %s\n", bdev_name); 600 pt_node = calloc(1, sizeof(struct vbdev_passthru)); 601 if (!pt_node) { 602 rc = -ENOMEM; 603 SPDK_ERRLOG("could not allocate pt_node\n"); 604 break; 605 } 606 607 pt_node->pt_bdev.name = strdup(name->vbdev_name); 608 if (!pt_node->pt_bdev.name) { 609 rc = -ENOMEM; 610 SPDK_ERRLOG("could not allocate pt_bdev name\n"); 611 free(pt_node); 612 break; 613 } 614 pt_node->pt_bdev.product_name = "passthru"; 615 616 /* The base bdev that we're attaching to. */ 617 rc = spdk_bdev_open_ext(bdev_name, true, vbdev_passthru_base_bdev_event_cb, 618 NULL, &pt_node->base_desc); 619 if (rc) { 620 if (rc != -ENODEV) { 621 SPDK_ERRLOG("could not open bdev %s\n", bdev_name); 622 } 623 free(pt_node->pt_bdev.name); 624 free(pt_node); 625 break; 626 } 627 SPDK_NOTICELOG("base bdev opened\n"); 628 629 bdev = spdk_bdev_desc_get_bdev(pt_node->base_desc); 630 pt_node->base_bdev = bdev; 631 632 /* Generate UUID based on namespace UUID + base bdev UUID. */ 633 rc = spdk_uuid_generate_sha1(&pt_node->pt_bdev.uuid, &ns_uuid, 634 (const char *)&pt_node->base_bdev->uuid, sizeof(struct spdk_uuid)); 635 if (rc) { 636 SPDK_ERRLOG("Unable to generate new UUID for passthru bdev\n"); 637 spdk_bdev_close(pt_node->base_desc); 638 free(pt_node->pt_bdev.name); 639 free(pt_node); 640 break; 641 } 642 643 /* Copy some properties from the underlying base bdev. */ 644 pt_node->pt_bdev.write_cache = bdev->write_cache; 645 pt_node->pt_bdev.required_alignment = bdev->required_alignment; 646 pt_node->pt_bdev.optimal_io_boundary = bdev->optimal_io_boundary; 647 pt_node->pt_bdev.blocklen = bdev->blocklen; 648 pt_node->pt_bdev.blockcnt = bdev->blockcnt; 649 650 pt_node->pt_bdev.md_interleave = bdev->md_interleave; 651 pt_node->pt_bdev.md_len = bdev->md_len; 652 pt_node->pt_bdev.dif_type = bdev->dif_type; 653 pt_node->pt_bdev.dif_is_head_of_md = bdev->dif_is_head_of_md; 654 pt_node->pt_bdev.dif_check_flags = bdev->dif_check_flags; 655 656 /* This is the context that is passed to us when the bdev 657 * layer calls in so we'll save our pt_bdev node here. 658 */ 659 pt_node->pt_bdev.ctxt = pt_node; 660 pt_node->pt_bdev.fn_table = &vbdev_passthru_fn_table; 661 pt_node->pt_bdev.module = &passthru_if; 662 TAILQ_INSERT_TAIL(&g_pt_nodes, pt_node, link); 663 664 spdk_io_device_register(pt_node, pt_bdev_ch_create_cb, pt_bdev_ch_destroy_cb, 665 sizeof(struct pt_io_channel), 666 name->vbdev_name); 667 SPDK_NOTICELOG("io_device created at: 0x%p\n", pt_node); 668 669 /* Save the thread where the base device is opened */ 670 pt_node->thread = spdk_get_thread(); 671 672 rc = spdk_bdev_module_claim_bdev(bdev, pt_node->base_desc, pt_node->pt_bdev.module); 673 if (rc) { 674 SPDK_ERRLOG("could not claim bdev %s\n", bdev_name); 675 spdk_bdev_close(pt_node->base_desc); 676 TAILQ_REMOVE(&g_pt_nodes, pt_node, link); 677 spdk_io_device_unregister(pt_node, NULL); 678 free(pt_node->pt_bdev.name); 679 free(pt_node); 680 break; 681 } 682 SPDK_NOTICELOG("bdev claimed\n"); 683 684 rc = spdk_bdev_register(&pt_node->pt_bdev); 685 if (rc) { 686 SPDK_ERRLOG("could not register pt_bdev\n"); 687 spdk_bdev_module_release_bdev(&pt_node->pt_bdev); 688 spdk_bdev_close(pt_node->base_desc); 689 TAILQ_REMOVE(&g_pt_nodes, pt_node, link); 690 spdk_io_device_unregister(pt_node, NULL); 691 free(pt_node->pt_bdev.name); 692 free(pt_node); 693 break; 694 } 695 SPDK_NOTICELOG("pt_bdev registered\n"); 696 SPDK_NOTICELOG("created pt_bdev for: %s\n", name->vbdev_name); 697 } 698 699 return rc; 700 } 701 702 /* Create the passthru disk from the given bdev and vbdev name. */ 703 int 704 bdev_passthru_create_disk(const char *bdev_name, const char *vbdev_name) 705 { 706 int rc; 707 708 /* Insert the bdev name into our global name list even if it doesn't exist yet, 709 * it may show up soon... 710 */ 711 rc = vbdev_passthru_insert_name(bdev_name, vbdev_name); 712 if (rc) { 713 return rc; 714 } 715 716 rc = vbdev_passthru_register(bdev_name); 717 if (rc == -ENODEV) { 718 /* This is not an error, we tracked the name above and it still 719 * may show up later. 720 */ 721 SPDK_NOTICELOG("vbdev creation deferred pending base bdev arrival\n"); 722 rc = 0; 723 } 724 725 return rc; 726 } 727 728 void 729 bdev_passthru_delete_disk(const char *bdev_name, spdk_bdev_unregister_cb cb_fn, void *cb_arg) 730 { 731 struct bdev_names *name; 732 int rc; 733 734 /* Some cleanup happens in the destruct callback. */ 735 rc = spdk_bdev_unregister_by_name(bdev_name, &passthru_if, cb_fn, cb_arg); 736 if (rc == 0) { 737 /* Remove the association (vbdev, bdev) from g_bdev_names. This is required so that the 738 * vbdev does not get re-created if the same bdev is constructed at some other time, 739 * unless the underlying bdev was hot-removed. 740 */ 741 TAILQ_FOREACH(name, &g_bdev_names, link) { 742 if (strcmp(name->vbdev_name, bdev_name) == 0) { 743 TAILQ_REMOVE(&g_bdev_names, name, link); 744 free(name->bdev_name); 745 free(name->vbdev_name); 746 free(name); 747 break; 748 } 749 } 750 } else { 751 cb_fn(cb_arg, rc); 752 } 753 } 754 755 /* Because we specified this function in our pt bdev function table when we 756 * registered our pt bdev, we'll get this call anytime a new bdev shows up. 757 * Here we need to decide if we care about it and if so what to do. We 758 * parsed the config file at init so we check the new bdev against the list 759 * we built up at that time and if the user configured us to attach to this 760 * bdev, here's where we do it. 761 */ 762 static void 763 vbdev_passthru_examine(struct spdk_bdev *bdev) 764 { 765 vbdev_passthru_register(bdev->name); 766 767 spdk_bdev_module_examine_done(&passthru_if); 768 } 769 770 SPDK_LOG_REGISTER_COMPONENT(vbdev_passthru) 771