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