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