xref: /spdk/module/bdev/ocf/vbdev_ocf.c (revision e316ec90b21eb3fea2dfa930261266dceb1f05aa)
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 #include <ocf/ocf.h>
35 #include <ocf/ocf_types.h>
36 #include <ocf/ocf_mngt.h>
37 
38 #include "ctx.h"
39 #include "data.h"
40 #include "volume.h"
41 #include "utils.h"
42 #include "vbdev_ocf.h"
43 
44 #include "spdk/bdev_module.h"
45 #include "spdk/conf.h"
46 #include "spdk/thread.h"
47 #include "spdk/string.h"
48 #include "spdk_internal/log.h"
49 #include "spdk/cpuset.h"
50 
51 static struct spdk_bdev_module ocf_if;
52 
53 static TAILQ_HEAD(, vbdev_ocf) g_ocf_vbdev_head
54 	= TAILQ_HEAD_INITIALIZER(g_ocf_vbdev_head);
55 
56 static TAILQ_HEAD(, examining_bdev) g_ocf_examining_bdevs_head
57 	= TAILQ_HEAD_INITIALIZER(g_ocf_examining_bdevs_head);
58 
59 bool g_fini_started = false;
60 
61 /* Structure for keeping list of bdevs that are claimed but not used yet */
62 struct examining_bdev {
63 	struct spdk_bdev           *bdev;
64 	TAILQ_ENTRY(examining_bdev) tailq;
65 };
66 
67 /* Add bdev to list of claimed */
68 static void
69 examine_start(struct spdk_bdev *bdev)
70 {
71 	struct examining_bdev *entry = malloc(sizeof(*entry));
72 
73 	assert(entry);
74 	entry->bdev = bdev;
75 	TAILQ_INSERT_TAIL(&g_ocf_examining_bdevs_head, entry, tailq);
76 }
77 
78 /* Find bdev on list of claimed bdevs, then remove it,
79  * if it was the last one on list then report examine done */
80 static void
81 examine_done(int status, struct vbdev_ocf *vbdev, void *cb_arg)
82 {
83 	struct spdk_bdev *bdev = cb_arg;
84 	struct examining_bdev *entry, *safe, *found = NULL;
85 
86 	TAILQ_FOREACH_SAFE(entry, &g_ocf_examining_bdevs_head, tailq, safe) {
87 		if (entry->bdev == bdev) {
88 			if (found) {
89 				goto remove;
90 			} else {
91 				found = entry;
92 			}
93 		}
94 	}
95 
96 	assert(found);
97 	spdk_bdev_module_examine_done(&ocf_if);
98 
99 remove:
100 	TAILQ_REMOVE(&g_ocf_examining_bdevs_head, found, tailq);
101 	free(found);
102 }
103 
104 /* Free allocated strings and structure itself
105  * Used at shutdown only */
106 static void
107 free_vbdev(struct vbdev_ocf *vbdev)
108 {
109 	if (!vbdev) {
110 		return;
111 	}
112 
113 	free(vbdev->name);
114 	free(vbdev->cache.name);
115 	free(vbdev->core.name);
116 	free(vbdev);
117 }
118 
119 /* Get existing cache base
120  * that is attached to other vbdev */
121 static struct vbdev_ocf_base *
122 get_other_cache_base(struct vbdev_ocf_base *base)
123 {
124 	struct vbdev_ocf *vbdev;
125 
126 	TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) {
127 		if (&vbdev->cache == base || !vbdev->cache.attached) {
128 			continue;
129 		}
130 		if (!strcmp(vbdev->cache.name, base->name)) {
131 			return &vbdev->cache;
132 		}
133 	}
134 
135 	return NULL;
136 }
137 
138 /* Get existing OCF cache instance
139  * that is started by other vbdev */
140 static ocf_cache_t
141 get_other_cache_instance(struct vbdev_ocf *vbdev)
142 {
143 	struct vbdev_ocf *cmp;
144 
145 	TAILQ_FOREACH(cmp, &g_ocf_vbdev_head, tailq) {
146 		if (cmp->state.doing_finish || cmp == vbdev) {
147 			continue;
148 		}
149 		if (strcmp(cmp->cache.name, vbdev->cache.name)) {
150 			continue;
151 		}
152 		if (cmp->ocf_cache) {
153 			return cmp->ocf_cache;
154 		}
155 	}
156 
157 	return NULL;
158 }
159 
160 static void
161 _remove_base_bdev(void *ctx)
162 {
163 	struct spdk_bdev_desc *desc = ctx;
164 
165 	spdk_bdev_close(desc);
166 }
167 
168 /* Close and unclaim base bdev */
169 static void
170 remove_base_bdev(struct vbdev_ocf_base *base)
171 {
172 	if (base->attached) {
173 		if (base->management_channel) {
174 			spdk_put_io_channel(base->management_channel);
175 		}
176 
177 		spdk_bdev_module_release_bdev(base->bdev);
178 		/* Close the underlying bdev on its same opened thread. */
179 		if (base->thread && base->thread != spdk_get_thread()) {
180 			spdk_thread_send_msg(base->thread, _remove_base_bdev, base->desc);
181 		} else {
182 			spdk_bdev_close(base->desc);
183 		}
184 		base->attached = false;
185 	}
186 }
187 
188 /* Finish unregister operation */
189 static void
190 unregister_finish(struct vbdev_ocf *vbdev)
191 {
192 	spdk_bdev_destruct_done(&vbdev->exp_bdev, vbdev->state.stop_status);
193 	vbdev_ocf_cache_ctx_put(vbdev->cache_ctx);
194 	vbdev_ocf_mngt_continue(vbdev, 0);
195 }
196 
197 static void
198 close_core_bdev(struct vbdev_ocf *vbdev)
199 {
200 	remove_base_bdev(&vbdev->core);
201 	vbdev_ocf_mngt_continue(vbdev, 0);
202 }
203 
204 static void
205 remove_core_cmpl(void *priv, int error)
206 {
207 	struct vbdev_ocf *vbdev = priv;
208 
209 	ocf_mngt_cache_unlock(vbdev->ocf_cache);
210 	vbdev_ocf_mngt_continue(vbdev, error);
211 }
212 
213 /* Try to lock cache, then remove core */
214 static void
215 remove_core_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error)
216 {
217 	struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv;
218 
219 	if (error) {
220 		SPDK_ERRLOG("Error %d, can not lock cache instance %s\n",
221 			    error, vbdev->name);
222 		vbdev_ocf_mngt_continue(vbdev, error);
223 		return;
224 	}
225 
226 	ocf_mngt_cache_remove_core(vbdev->ocf_core, remove_core_cmpl, vbdev);
227 }
228 
229 /* Detach core base */
230 static void
231 detach_core(struct vbdev_ocf *vbdev)
232 {
233 	if (vbdev->ocf_cache && ocf_cache_is_running(vbdev->ocf_cache)) {
234 		ocf_mngt_cache_lock(vbdev->ocf_cache, remove_core_cache_lock_cmpl, vbdev);
235 	} else {
236 		vbdev_ocf_mngt_continue(vbdev, 0);
237 	}
238 }
239 
240 static void
241 close_cache_bdev(struct vbdev_ocf *vbdev)
242 {
243 	remove_base_bdev(&vbdev->cache);
244 	vbdev_ocf_mngt_continue(vbdev, 0);
245 }
246 
247 /* Detach cache base */
248 static void
249 detach_cache(struct vbdev_ocf *vbdev)
250 {
251 	vbdev->state.stop_status = vbdev->mngt_ctx.status;
252 
253 	/* If some other vbdev references this cache bdev,
254 	 * we detach this only by changing the flag, without actual close */
255 	if (get_other_cache_base(&vbdev->cache)) {
256 		vbdev->cache.attached = false;
257 	}
258 
259 	vbdev_ocf_mngt_continue(vbdev, 0);
260 }
261 
262 static void
263 stop_vbdev_cmpl(ocf_cache_t cache, void *priv, int error)
264 {
265 	struct vbdev_ocf *vbdev = priv;
266 
267 	vbdev_ocf_queue_put(vbdev->cache_ctx->mngt_queue);
268 	ocf_mngt_cache_unlock(cache);
269 
270 	vbdev_ocf_mngt_continue(vbdev, error);
271 }
272 
273 /* Try to lock cache, then stop it */
274 static void
275 stop_vbdev_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error)
276 {
277 	struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv;
278 
279 	if (error) {
280 		SPDK_ERRLOG("Error %d, can not lock cache instance %s\n",
281 			    error, vbdev->name);
282 		vbdev_ocf_mngt_continue(vbdev, error);
283 		return;
284 	}
285 
286 	ocf_mngt_cache_stop(vbdev->ocf_cache, stop_vbdev_cmpl, vbdev);
287 }
288 
289 /* Stop OCF cache object
290  * vbdev_ocf is not operational after this */
291 static void
292 stop_vbdev(struct vbdev_ocf *vbdev)
293 {
294 	if (!ocf_cache_is_running(vbdev->ocf_cache)) {
295 		vbdev_ocf_mngt_continue(vbdev, 0);
296 		return;
297 	}
298 
299 	if (!g_fini_started && get_other_cache_instance(vbdev)) {
300 		SPDK_NOTICELOG("Not stopping cache instance '%s'"
301 			       " because it is referenced by other OCF bdev\n",
302 			       vbdev->cache.name);
303 		vbdev_ocf_mngt_continue(vbdev, 0);
304 		return;
305 	}
306 
307 	ocf_mngt_cache_lock(vbdev->ocf_cache, stop_vbdev_cache_lock_cmpl, vbdev);
308 }
309 
310 static void
311 flush_vbdev_cmpl(ocf_cache_t cache, void *priv, int error)
312 {
313 	struct vbdev_ocf *vbdev = priv;
314 
315 	ocf_mngt_cache_unlock(cache);
316 	vbdev_ocf_mngt_continue(vbdev, error);
317 }
318 
319 static void
320 flush_vbdev_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error)
321 {
322 	struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv;
323 
324 	if (error) {
325 		SPDK_ERRLOG("Error %d, can not lock cache instance %s\n",
326 			    error, vbdev->name);
327 		vbdev_ocf_mngt_continue(vbdev, error);
328 		return;
329 	}
330 
331 	ocf_mngt_cache_flush(vbdev->ocf_cache, flush_vbdev_cmpl, vbdev);
332 }
333 
334 static void
335 flush_vbdev(struct vbdev_ocf *vbdev)
336 {
337 	if (!ocf_cache_is_running(vbdev->ocf_cache)) {
338 		vbdev_ocf_mngt_continue(vbdev, -EINVAL);
339 		return;
340 	}
341 
342 	ocf_mngt_cache_lock(vbdev->ocf_cache, flush_vbdev_cache_lock_cmpl, vbdev);
343 }
344 
345 /* Procedures called during dirty unregister */
346 vbdev_ocf_mngt_fn unregister_path_dirty[] = {
347 	flush_vbdev,
348 	stop_vbdev,
349 	detach_cache,
350 	close_cache_bdev,
351 	detach_core,
352 	close_core_bdev,
353 	unregister_finish,
354 	NULL
355 };
356 
357 /* Procedures called during clean unregister */
358 vbdev_ocf_mngt_fn unregister_path_clean[] = {
359 	flush_vbdev,
360 	detach_core,
361 	close_core_bdev,
362 	stop_vbdev,
363 	detach_cache,
364 	close_cache_bdev,
365 	unregister_finish,
366 	NULL
367 };
368 
369 /* Start asynchronous management operation using unregister_path */
370 static void
371 unregister_cb(void *opaque)
372 {
373 	struct vbdev_ocf *vbdev = opaque;
374 	vbdev_ocf_mngt_fn *unregister_path;
375 	int rc;
376 
377 	unregister_path = vbdev->state.doing_clean_delete ?
378 			  unregister_path_clean : unregister_path_dirty;
379 
380 	rc = vbdev_ocf_mngt_start(vbdev, unregister_path, NULL, NULL);
381 	if (rc) {
382 		SPDK_ERRLOG("Unable to unregister OCF bdev: %d\n", rc);
383 		spdk_bdev_destruct_done(&vbdev->exp_bdev, rc);
384 	}
385 }
386 
387 /* Clean remove case - remove core and then cache, this order
388  * will remove instance permanently */
389 static void
390 _vbdev_ocf_destruct_clean(struct vbdev_ocf *vbdev)
391 {
392 	if (vbdev->core.attached) {
393 		detach_core(vbdev);
394 		close_core_bdev(vbdev);
395 	}
396 
397 	if (vbdev->cache.attached) {
398 		detach_cache(vbdev);
399 		close_cache_bdev(vbdev);
400 	}
401 }
402 
403 /* Dirty shutdown/hot remove case - remove cache and then core, this order
404  * will allow us to recover this instance in the future */
405 static void
406 _vbdev_ocf_destruct_dirty(struct vbdev_ocf *vbdev)
407 {
408 	if (vbdev->cache.attached) {
409 		detach_cache(vbdev);
410 		close_cache_bdev(vbdev);
411 	}
412 
413 	if (vbdev->core.attached) {
414 		detach_core(vbdev);
415 		close_core_bdev(vbdev);
416 	}
417 }
418 
419 /* Unregister io device with callback to unregister_cb
420  * This function is called during spdk_bdev_unregister */
421 static int
422 vbdev_ocf_destruct(void *opaque)
423 {
424 	struct vbdev_ocf *vbdev = opaque;
425 
426 	if (vbdev->state.doing_finish) {
427 		return -EALREADY;
428 	}
429 
430 	if (vbdev->state.starting && !vbdev->state.started) {
431 		/* Prevent before detach cache/core during register path of
432 		  this bdev */
433 		return -EBUSY;
434 	}
435 
436 	vbdev->state.doing_finish = true;
437 
438 	if (vbdev->state.started) {
439 		spdk_io_device_unregister(vbdev, unregister_cb);
440 		/* Return 1 because unregister is delayed */
441 		return 1;
442 	}
443 
444 	if (vbdev->state.doing_clean_delete) {
445 		_vbdev_ocf_destruct_clean(vbdev);
446 	} else {
447 		_vbdev_ocf_destruct_dirty(vbdev);
448 	}
449 
450 	return 0;
451 }
452 
453 /* Stop OCF cache and unregister SPDK bdev */
454 int
455 vbdev_ocf_delete(struct vbdev_ocf *vbdev, void (*cb)(void *, int), void *cb_arg)
456 {
457 	int rc = 0;
458 
459 	if (vbdev->state.started) {
460 		spdk_bdev_unregister(&vbdev->exp_bdev, cb, cb_arg);
461 	} else {
462 		rc = vbdev_ocf_destruct(vbdev);
463 		if (rc == 0 && cb) {
464 			cb(cb_arg, 0);
465 		}
466 	}
467 
468 	return rc;
469 }
470 
471 /* Remove cores permanently and then stop OCF cache and unregister SPDK bdev */
472 int
473 vbdev_ocf_delete_clean(struct vbdev_ocf *vbdev, void (*cb)(void *, int),
474 		       void *cb_arg)
475 {
476 	vbdev->state.doing_clean_delete = true;
477 
478 	return vbdev_ocf_delete(vbdev, cb, cb_arg);
479 }
480 
481 
482 /* If vbdev is online, return its object */
483 struct vbdev_ocf *
484 vbdev_ocf_get_by_name(const char *name)
485 {
486 	struct vbdev_ocf *vbdev;
487 
488 	if (name == NULL) {
489 		assert(false);
490 		return NULL;
491 	}
492 
493 	TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) {
494 		if (vbdev->name == NULL || vbdev->state.doing_finish) {
495 			continue;
496 		}
497 		if (strcmp(vbdev->name, name) == 0) {
498 			return vbdev;
499 		}
500 	}
501 	return NULL;
502 }
503 
504 /* Return matching base if parent vbdev is online */
505 struct vbdev_ocf_base *
506 vbdev_ocf_get_base_by_name(const char *name)
507 {
508 	struct vbdev_ocf *vbdev;
509 
510 	if (name == NULL) {
511 		assert(false);
512 		return NULL;
513 	}
514 
515 	TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) {
516 		if (vbdev->state.doing_finish) {
517 			continue;
518 		}
519 
520 		if (vbdev->cache.name && strcmp(vbdev->cache.name, name) == 0) {
521 			return &vbdev->cache;
522 		}
523 		if (vbdev->core.name && strcmp(vbdev->core.name, name) == 0) {
524 			return &vbdev->core;
525 		}
526 	}
527 	return NULL;
528 }
529 
530 /* Execute fn for each OCF device that is online or waits for base devices */
531 void
532 vbdev_ocf_foreach(vbdev_ocf_foreach_fn fn, void *ctx)
533 {
534 	struct vbdev_ocf *vbdev;
535 
536 	assert(fn != NULL);
537 
538 	TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) {
539 		if (!vbdev->state.doing_finish) {
540 			fn(vbdev, ctx);
541 		}
542 	}
543 }
544 
545 /* Called from OCF when SPDK_IO is completed */
546 static void
547 vbdev_ocf_io_submit_cb(struct ocf_io *io, int error)
548 {
549 	struct spdk_bdev_io *bdev_io = io->priv1;
550 
551 	if (error == 0) {
552 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_SUCCESS);
553 	} else if (error == -ENOMEM) {
554 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
555 	} else {
556 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
557 	}
558 
559 	ocf_io_put(io);
560 }
561 
562 /* Configure io parameters and send it to OCF */
563 static int
564 io_submit_to_ocf(struct spdk_bdev_io *bdev_io, struct ocf_io *io)
565 {
566 	int dir;
567 	uint64_t len = bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen;
568 	uint64_t offset = bdev_io->u.bdev.offset_blocks * bdev_io->bdev->blocklen;
569 
570 	switch (bdev_io->type) {
571 	case SPDK_BDEV_IO_TYPE_WRITE:
572 	case SPDK_BDEV_IO_TYPE_READ:
573 		dir = OCF_READ;
574 		if (bdev_io->type == SPDK_BDEV_IO_TYPE_WRITE) {
575 			dir = OCF_WRITE;
576 		}
577 		ocf_io_configure(io, offset, len, dir, 0, 0);
578 		ocf_core_submit_io(io);
579 		return 0;
580 	case SPDK_BDEV_IO_TYPE_FLUSH:
581 		ocf_io_configure(io, offset, len, OCF_WRITE, 0, OCF_WRITE_FLUSH);
582 		ocf_core_submit_flush(io);
583 		return 0;
584 	case SPDK_BDEV_IO_TYPE_UNMAP:
585 		ocf_io_configure(io, offset, len, 0, 0, 0);
586 		ocf_core_submit_discard(io);
587 		return 0;
588 	case SPDK_BDEV_IO_TYPE_RESET:
589 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
590 	default:
591 		SPDK_ERRLOG("Unsupported IO type: %d\n", bdev_io->type);
592 		return -EINVAL;
593 	}
594 }
595 
596 /* Submit SPDK-IO to OCF */
597 static void
598 io_handle(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
599 {
600 	struct vbdev_ocf *vbdev = bdev_io->bdev->ctxt;
601 	struct ocf_io *io = NULL;
602 	struct bdev_ocf_data *data = NULL;
603 	struct vbdev_ocf_qcxt *qctx = spdk_io_channel_get_ctx(ch);
604 	int err;
605 
606 	io = ocf_core_new_io(vbdev->ocf_core);
607 	if (!io) {
608 		err = -ENOMEM;
609 		goto fail;
610 	}
611 
612 	ocf_io_set_queue(io, qctx->queue);
613 
614 	data = vbdev_ocf_data_from_spdk_io(bdev_io);
615 	if (!data) {
616 		err = -ENOMEM;
617 		goto fail;
618 	}
619 
620 	err = ocf_io_set_data(io, data, 0);
621 	if (err) {
622 		goto fail;
623 	}
624 
625 	ocf_io_set_cmpl(io, bdev_io, NULL, vbdev_ocf_io_submit_cb);
626 
627 	err = io_submit_to_ocf(bdev_io, io);
628 	if (err) {
629 		goto fail;
630 	}
631 
632 	return;
633 
634 fail:
635 	if (io) {
636 		ocf_io_put(io);
637 	}
638 
639 	if (err == -ENOMEM) {
640 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_NOMEM);
641 	} else {
642 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
643 	}
644 }
645 
646 static void
647 vbdev_ocf_get_buf_cb(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io,
648 		     bool success)
649 {
650 	if (!success) {
651 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
652 		return;
653 	}
654 
655 	io_handle(ch, bdev_io);
656 }
657 
658 /* Called from bdev layer when an io to Cache vbdev is submitted */
659 static void
660 vbdev_ocf_submit_request(struct spdk_io_channel *ch, struct spdk_bdev_io *bdev_io)
661 {
662 	switch (bdev_io->type) {
663 	case SPDK_BDEV_IO_TYPE_READ:
664 		/* User does not have to allocate io vectors for the request,
665 		 * so in case they are not allocated, we allocate them here */
666 		spdk_bdev_io_get_buf(bdev_io, vbdev_ocf_get_buf_cb,
667 				     bdev_io->u.bdev.num_blocks * bdev_io->bdev->blocklen);
668 		break;
669 	case SPDK_BDEV_IO_TYPE_WRITE:
670 	case SPDK_BDEV_IO_TYPE_FLUSH:
671 	case SPDK_BDEV_IO_TYPE_UNMAP:
672 		io_handle(ch, bdev_io);
673 		break;
674 	case SPDK_BDEV_IO_TYPE_RESET:
675 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
676 	default:
677 		SPDK_ERRLOG("Unknown I/O type %d\n", bdev_io->type);
678 		spdk_bdev_io_complete(bdev_io, SPDK_BDEV_IO_STATUS_FAILED);
679 		break;
680 	}
681 }
682 
683 /* Called from bdev layer */
684 static bool
685 vbdev_ocf_io_type_supported(void *opaque, enum spdk_bdev_io_type io_type)
686 {
687 	struct vbdev_ocf *vbdev = opaque;
688 
689 	switch (io_type) {
690 	case SPDK_BDEV_IO_TYPE_READ:
691 	case SPDK_BDEV_IO_TYPE_WRITE:
692 	case SPDK_BDEV_IO_TYPE_FLUSH:
693 	case SPDK_BDEV_IO_TYPE_UNMAP:
694 		return spdk_bdev_io_type_supported(vbdev->core.bdev, io_type);
695 	case SPDK_BDEV_IO_TYPE_RESET:
696 	case SPDK_BDEV_IO_TYPE_WRITE_ZEROES:
697 	default:
698 		return false;
699 	}
700 }
701 
702 /* Called from bdev layer */
703 static struct spdk_io_channel *
704 vbdev_ocf_get_io_channel(void *opaque)
705 {
706 	struct vbdev_ocf *bdev = opaque;
707 
708 	return spdk_get_io_channel(bdev);
709 }
710 
711 static int
712 vbdev_ocf_dump_info_json(void *opaque, struct spdk_json_write_ctx *w)
713 {
714 	struct vbdev_ocf *vbdev = opaque;
715 
716 	spdk_json_write_named_string(w, "cache_device", vbdev->cache.name);
717 	spdk_json_write_named_string(w, "core_device", vbdev->core.name);
718 
719 	spdk_json_write_named_string(w, "mode",
720 				     ocf_get_cache_modename(ocf_cache_get_mode(vbdev->ocf_cache)));
721 	spdk_json_write_named_uint32(w, "cache_line_size",
722 				     ocf_cache_get_line_size(vbdev->ocf_cache));
723 	spdk_json_write_named_bool(w, "metadata_volatile",
724 				   vbdev->cfg.cache.metadata_volatile);
725 
726 	return 0;
727 }
728 
729 static void
730 vbdev_ocf_write_json_config(struct spdk_bdev *bdev, struct spdk_json_write_ctx *w)
731 {
732 	struct vbdev_ocf *vbdev = bdev->ctxt;
733 
734 	spdk_json_write_object_begin(w);
735 
736 	spdk_json_write_named_string(w, "method", "bdev_ocf_create");
737 
738 	spdk_json_write_named_object_begin(w, "params");
739 	spdk_json_write_named_string(w, "name", vbdev->name);
740 	spdk_json_write_named_string(w, "mode",
741 				     ocf_get_cache_modename(ocf_cache_get_mode(vbdev->ocf_cache)));
742 	spdk_json_write_named_string(w, "cache_bdev_name", vbdev->cache.name);
743 	spdk_json_write_named_string(w, "core_bdev_name", vbdev->core.name);
744 	spdk_json_write_object_end(w);
745 
746 	spdk_json_write_object_end(w);
747 }
748 
749 /* Cache vbdev function table
750  * Used by bdev layer */
751 static struct spdk_bdev_fn_table cache_dev_fn_table = {
752 	.destruct = vbdev_ocf_destruct,
753 	.io_type_supported = vbdev_ocf_io_type_supported,
754 	.submit_request	= vbdev_ocf_submit_request,
755 	.get_io_channel	= vbdev_ocf_get_io_channel,
756 	.write_config_json = vbdev_ocf_write_json_config,
757 	.dump_info_json = vbdev_ocf_dump_info_json,
758 };
759 
760 /* Poller function for the OCF queue
761  * We execute OCF requests here synchronously */
762 static int
763 queue_poll(void *opaque)
764 {
765 	struct vbdev_ocf_qcxt *qctx = opaque;
766 	uint32_t iono = ocf_queue_pending_io(qctx->queue);
767 	int i, max = spdk_min(32, iono);
768 
769 	for (i = 0; i < max; i++) {
770 		ocf_queue_run_single(qctx->queue);
771 	}
772 
773 	if (iono > 0) {
774 		return 1;
775 	} else {
776 		return 0;
777 	}
778 }
779 
780 /* Called during ocf_submit_io, ocf_purge*
781  * and any other requests that need to submit io */
782 static void
783 vbdev_ocf_ctx_queue_kick(ocf_queue_t q)
784 {
785 }
786 
787 /* OCF queue deinitialization
788  * Called at ocf_cache_stop */
789 static void
790 vbdev_ocf_ctx_queue_stop(ocf_queue_t q)
791 {
792 	struct vbdev_ocf_qcxt *qctx = ocf_queue_get_priv(q);
793 
794 	if (qctx) {
795 		spdk_put_io_channel(qctx->cache_ch);
796 		spdk_put_io_channel(qctx->core_ch);
797 		spdk_poller_unregister(&qctx->poller);
798 		if (qctx->allocated) {
799 			free(qctx);
800 		}
801 	}
802 }
803 
804 /* Queue ops is an interface for running queue thread
805  * stop() operation in called just before queue gets destroyed */
806 const struct ocf_queue_ops queue_ops = {
807 	.kick_sync = vbdev_ocf_ctx_queue_kick,
808 	.kick = vbdev_ocf_ctx_queue_kick,
809 	.stop = vbdev_ocf_ctx_queue_stop,
810 };
811 
812 /* Called on cache vbdev creation at every thread
813  * We allocate OCF queues here and SPDK poller for it */
814 static int
815 io_device_create_cb(void *io_device, void *ctx_buf)
816 {
817 	struct vbdev_ocf *vbdev = io_device;
818 	struct vbdev_ocf_qcxt *qctx = ctx_buf;
819 	int rc;
820 
821 	rc = vbdev_ocf_queue_create(vbdev->ocf_cache, &qctx->queue, &queue_ops);
822 	if (rc) {
823 		return rc;
824 	}
825 
826 	ocf_queue_set_priv(qctx->queue, qctx);
827 
828 	qctx->vbdev      = vbdev;
829 	qctx->cache_ch   = spdk_bdev_get_io_channel(vbdev->cache.desc);
830 	qctx->core_ch    = spdk_bdev_get_io_channel(vbdev->core.desc);
831 	qctx->poller     = SPDK_POLLER_REGISTER(queue_poll, qctx, 0);
832 
833 	return rc;
834 }
835 
836 /* Called per thread
837  * Put OCF queue and relaunch poller with new context to finish pending requests */
838 static void
839 io_device_destroy_cb(void *io_device, void *ctx_buf)
840 {
841 	/* Making a copy of context to use it after io channel will be destroyed */
842 	struct vbdev_ocf_qcxt *copy = malloc(sizeof(*copy));
843 	struct vbdev_ocf_qcxt *qctx = ctx_buf;
844 
845 	if (copy) {
846 		ocf_queue_set_priv(qctx->queue, copy);
847 		memcpy(copy, qctx, sizeof(*copy));
848 		spdk_poller_unregister(&qctx->poller);
849 		copy->poller = SPDK_POLLER_REGISTER(queue_poll, copy, 0);
850 		copy->allocated = true;
851 	} else {
852 		SPDK_ERRLOG("Unable to stop OCF queue properly: %s\n",
853 			    spdk_strerror(ENOMEM));
854 	}
855 
856 	vbdev_ocf_queue_put(qctx->queue);
857 }
858 
859 /* OCF management queue deinitialization */
860 static void
861 vbdev_ocf_ctx_mngt_queue_stop(ocf_queue_t q)
862 {
863 	struct spdk_poller *poller = ocf_queue_get_priv(q);
864 
865 	if (poller) {
866 		spdk_poller_unregister(&poller);
867 	}
868 }
869 
870 static int
871 mngt_queue_poll(void *opaque)
872 {
873 	ocf_queue_t q = opaque;
874 	uint32_t iono = ocf_queue_pending_io(q);
875 	int i, max = spdk_min(32, iono);
876 
877 	for (i = 0; i < max; i++) {
878 		ocf_queue_run_single(q);
879 	}
880 
881 	if (iono > 0) {
882 		return 1;
883 	} else {
884 		return 0;
885 	}
886 }
887 
888 static void
889 vbdev_ocf_ctx_mngt_queue_kick(ocf_queue_t q)
890 {
891 }
892 
893 /* Queue ops is an interface for running queue thread
894  * stop() operation in called just before queue gets destroyed */
895 const struct ocf_queue_ops mngt_queue_ops = {
896 	.kick_sync = NULL,
897 	.kick = vbdev_ocf_ctx_mngt_queue_kick,
898 	.stop = vbdev_ocf_ctx_mngt_queue_stop,
899 };
900 
901 static void
902 clear_starting_indicator_vbdev(struct vbdev_ocf *vbdev)
903 {
904 	vbdev->state.starting = false;
905 }
906 
907 /* Create exported spdk object */
908 static void
909 finish_register(struct vbdev_ocf *vbdev)
910 {
911 	int result;
912 
913 	/* Copy properties of the base bdev */
914 	vbdev->exp_bdev.blocklen = vbdev->core.bdev->blocklen;
915 	vbdev->exp_bdev.write_cache = vbdev->core.bdev->write_cache;
916 	vbdev->exp_bdev.required_alignment = vbdev->core.bdev->required_alignment;
917 
918 	vbdev->exp_bdev.name = vbdev->name;
919 	vbdev->exp_bdev.product_name = "SPDK OCF";
920 
921 	vbdev->exp_bdev.blockcnt = vbdev->core.bdev->blockcnt;
922 	vbdev->exp_bdev.ctxt = vbdev;
923 	vbdev->exp_bdev.fn_table = &cache_dev_fn_table;
924 	vbdev->exp_bdev.module = &ocf_if;
925 
926 	/* Finally register vbdev in SPDK */
927 	spdk_io_device_register(vbdev, io_device_create_cb, io_device_destroy_cb,
928 				sizeof(struct vbdev_ocf_qcxt), vbdev->name);
929 	result = spdk_bdev_register(&vbdev->exp_bdev);
930 	if (result) {
931 		SPDK_ERRLOG("Could not register exposed bdev %s\n",
932 			    vbdev->name);
933 		clear_starting_indicator_vbdev(vbdev);
934 		vbdev_ocf_mngt_stop(vbdev, unregister_path_dirty, result);
935 		return;
936 	} else {
937 		vbdev->state.started = true;
938 	}
939 
940 	vbdev_ocf_mngt_continue(vbdev, result);
941 }
942 
943 static void
944 add_core_cmpl(ocf_cache_t cache, ocf_core_t core, void *priv, int error)
945 {
946 	struct vbdev_ocf *vbdev = priv;
947 
948 	ocf_mngt_cache_unlock(cache);
949 
950 	if (error) {
951 		SPDK_ERRLOG("Error %d, failed to add core device to cache instance %s,"
952 			    "starting rollback\n", error, vbdev->name);
953 		clear_starting_indicator_vbdev(vbdev);
954 		vbdev_ocf_mngt_stop(vbdev, unregister_path_dirty, error);
955 		return;
956 	} else {
957 		vbdev->ocf_core = core;
958 		vbdev->core.id  = ocf_core_get_id(core);
959 	}
960 
961 	vbdev_ocf_mngt_continue(vbdev, error);
962 }
963 
964 /* Try to lock cache, then add core */
965 static void
966 add_core_cache_lock_cmpl(ocf_cache_t cache, void *priv, int error)
967 {
968 	struct vbdev_ocf *vbdev = (struct vbdev_ocf *)priv;
969 
970 	if (error) {
971 		SPDK_ERRLOG("Error %d, can not lock cache instance %s,"
972 			    "starting rollback\n", error, vbdev->name);
973 		clear_starting_indicator_vbdev(vbdev);
974 		vbdev_ocf_mngt_stop(vbdev, unregister_path_dirty, error);
975 	}
976 	ocf_mngt_cache_add_core(vbdev->ocf_cache, &vbdev->cfg.core, add_core_cmpl, vbdev);
977 }
978 
979 /* Add core for existing OCF cache instance */
980 static void
981 add_core(struct vbdev_ocf *vbdev)
982 {
983 	ocf_mngt_cache_lock(vbdev->ocf_cache, add_core_cache_lock_cmpl, vbdev);
984 }
985 
986 static void
987 start_cache_cmpl(ocf_cache_t cache, void *priv, int error)
988 {
989 	struct vbdev_ocf *vbdev = priv;
990 
991 	ocf_mngt_cache_unlock(cache);
992 
993 	if (error) {
994 		SPDK_ERRLOG("Error %d during start cache %s, starting rollback\n",
995 			    error, vbdev->name);
996 		clear_starting_indicator_vbdev(vbdev);
997 		vbdev_ocf_mngt_stop(vbdev, unregister_path_dirty, error);
998 		return;
999 	}
1000 
1001 	vbdev_ocf_mngt_continue(vbdev, error);
1002 }
1003 
1004 static int
1005 create_management_queue(struct vbdev_ocf *vbdev)
1006 {
1007 	struct spdk_poller *mngt_poller;
1008 	int rc;
1009 
1010 	rc = vbdev_ocf_queue_create(vbdev->ocf_cache, &vbdev->cache_ctx->mngt_queue, &mngt_queue_ops);
1011 	if (rc) {
1012 		SPDK_ERRLOG("Unable to create mngt_queue: %d\n", rc);
1013 		return rc;
1014 	}
1015 
1016 	mngt_poller = SPDK_POLLER_REGISTER(mngt_queue_poll, vbdev->cache_ctx->mngt_queue, 100);
1017 	if (mngt_poller == NULL) {
1018 		SPDK_ERRLOG("Unable to initiate mngt request: %s", spdk_strerror(ENOMEM));
1019 		return -ENOMEM;
1020 	}
1021 
1022 	ocf_queue_set_priv(vbdev->cache_ctx->mngt_queue, mngt_poller);
1023 	ocf_mngt_cache_set_mngt_queue(vbdev->ocf_cache, vbdev->cache_ctx->mngt_queue);
1024 
1025 	return 0;
1026 }
1027 
1028 /* Start OCF cache, attach caching device */
1029 static void
1030 start_cache(struct vbdev_ocf *vbdev)
1031 {
1032 	ocf_cache_t existing;
1033 	int rc;
1034 
1035 	if (vbdev->ocf_cache) {
1036 		vbdev_ocf_mngt_stop(vbdev, NULL, -EALREADY);
1037 		return;
1038 	}
1039 
1040 	existing = get_other_cache_instance(vbdev);
1041 	if (existing) {
1042 		SPDK_NOTICELOG("OCF bdev %s connects to existing cache device %s\n",
1043 			       vbdev->name, vbdev->cache.name);
1044 		vbdev->ocf_cache = existing;
1045 		vbdev->cache.id = ocf_cache_get_id(existing);
1046 		vbdev->cache_ctx = ocf_cache_get_priv(existing);
1047 		vbdev_ocf_cache_ctx_get(vbdev->cache_ctx);
1048 		vbdev_ocf_mngt_continue(vbdev, 0);
1049 		return;
1050 	}
1051 
1052 	vbdev->cache_ctx = calloc(1, sizeof(struct vbdev_ocf_cache_ctx));
1053 	if (vbdev->cache_ctx == NULL) {
1054 		clear_starting_indicator_vbdev(vbdev);
1055 		vbdev_ocf_mngt_stop(vbdev, unregister_path_dirty, -ENOMEM);
1056 		return;
1057 	}
1058 
1059 	vbdev_ocf_cache_ctx_get(vbdev->cache_ctx);
1060 	pthread_mutex_init(&vbdev->cache_ctx->lock, NULL);
1061 
1062 	rc = ocf_mngt_cache_start(vbdev_ocf_ctx, &vbdev->ocf_cache, &vbdev->cfg.cache);
1063 	if (rc) {
1064 		clear_starting_indicator_vbdev(vbdev);
1065 		vbdev_ocf_mngt_stop(vbdev, unregister_path_dirty, rc);
1066 		return;
1067 	}
1068 
1069 	vbdev->cache.id = ocf_cache_get_id(vbdev->ocf_cache);
1070 	ocf_cache_set_priv(vbdev->ocf_cache, vbdev->cache_ctx);
1071 
1072 	rc = create_management_queue(vbdev);
1073 	if (rc) {
1074 		SPDK_ERRLOG("Unable to create mngt_queue: %d\n", rc);
1075 		clear_starting_indicator_vbdev(vbdev);
1076 		vbdev_ocf_mngt_stop(vbdev, unregister_path_dirty, rc);
1077 		return;
1078 	}
1079 
1080 	if (vbdev->cfg.loadq) {
1081 		ocf_mngt_cache_load(vbdev->ocf_cache, &vbdev->cfg.device, start_cache_cmpl, vbdev);
1082 	} else {
1083 		ocf_mngt_cache_attach(vbdev->ocf_cache, &vbdev->cfg.device, start_cache_cmpl, vbdev);
1084 	}
1085 }
1086 
1087 /* Procedures called during register operation */
1088 vbdev_ocf_mngt_fn register_path[] = {
1089 	start_cache,
1090 	add_core,
1091 	finish_register,
1092 	NULL
1093 };
1094 
1095 /* Start cache instance and register OCF bdev */
1096 static void
1097 register_vbdev(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_callback cb, void *cb_arg)
1098 {
1099 	int rc;
1100 
1101 	if (!(vbdev->core.attached && vbdev->cache.attached) || vbdev->state.started) {
1102 		cb(-EPERM, vbdev, cb_arg);
1103 		return;
1104 	}
1105 
1106 	vbdev->state.starting = true;
1107 	rc = vbdev_ocf_mngt_start(vbdev, register_path, cb, cb_arg);
1108 	if (rc) {
1109 		cb(rc, vbdev, cb_arg);
1110 	}
1111 }
1112 
1113 /* Init OCF configuration options
1114  * for core and cache devices */
1115 static void
1116 init_vbdev_config(struct vbdev_ocf *vbdev)
1117 {
1118 	struct vbdev_ocf_config *cfg = &vbdev->cfg;
1119 
1120 	/* Id 0 means OCF decides the id */
1121 	cfg->cache.id = 0;
1122 	cfg->cache.name = vbdev->name;
1123 
1124 	/* TODO [metadata]: make configurable with persistent
1125 	 * metadata support */
1126 	cfg->cache.metadata_volatile = false;
1127 
1128 	/* TODO [cache line size]: make cache line size configurable
1129 	 * Using standard 4KiB for now */
1130 	cfg->cache.cache_line_size = ocf_cache_line_size_4;
1131 
1132 	/* This are suggested values that
1133 	 * should be sufficient for most use cases */
1134 	cfg->cache.backfill.max_queue_size = 65536;
1135 	cfg->cache.backfill.queue_unblock_size = 60000;
1136 
1137 	/* TODO [cache line size] */
1138 	cfg->device.cache_line_size = ocf_cache_line_size_4;
1139 	cfg->device.force = true;
1140 	cfg->device.perform_test = false;
1141 	cfg->device.discard_on_start = false;
1142 
1143 	vbdev->cfg.cache.locked = true;
1144 
1145 	cfg->core.volume_type = SPDK_OBJECT;
1146 	cfg->device.volume_type = SPDK_OBJECT;
1147 	cfg->core.core_id = OCF_CORE_MAX;
1148 
1149 	if (vbdev->cfg.loadq) {
1150 		/* When doing cache_load(), we need to set try_add to true,
1151 		 * otherwise OCF will interpret this core as new
1152 		 * instead of the inactive one */
1153 		vbdev->cfg.core.try_add = true;
1154 	}
1155 
1156 	/* Serialize bdev names in OCF UUID to interpret on future loads
1157 	 * Core UUID is a triple of (core name, vbdev name, cache name)
1158 	 * Cache UUID is cache bdev name */
1159 	cfg->device.uuid.size = strlen(vbdev->cache.name) + 1;
1160 	cfg->device.uuid.data = vbdev->cache.name;
1161 
1162 	snprintf(vbdev->uuid, VBDEV_OCF_MD_MAX_LEN, "%s %s %s",
1163 		 vbdev->core.name, vbdev->name, vbdev->cache.name);
1164 	cfg->core.uuid.size = strlen(vbdev->uuid) + 1;
1165 	cfg->core.uuid.data = vbdev->uuid;
1166 	vbdev->uuid[strlen(vbdev->core.name)] = 0;
1167 	vbdev->uuid[strlen(vbdev->core.name) + 1 + strlen(vbdev->name)] = 0;
1168 }
1169 
1170 /* Allocate vbdev structure object and add it to the global list */
1171 static int
1172 init_vbdev(const char *vbdev_name,
1173 	   const char *cache_mode_name,
1174 	   const char *cache_name,
1175 	   const char *core_name,
1176 	   bool loadq)
1177 {
1178 	struct vbdev_ocf *vbdev;
1179 	int rc = 0;
1180 
1181 	if (spdk_bdev_get_by_name(vbdev_name) || vbdev_ocf_get_by_name(vbdev_name)) {
1182 		SPDK_ERRLOG("Device with name '%s' already exists\n", vbdev_name);
1183 		return -EPERM;
1184 	}
1185 
1186 	vbdev = calloc(1, sizeof(*vbdev));
1187 	if (!vbdev) {
1188 		goto error_mem;
1189 	}
1190 
1191 	vbdev->cache.parent = vbdev;
1192 	vbdev->core.parent = vbdev;
1193 	vbdev->cache.is_cache = true;
1194 	vbdev->core.is_cache = false;
1195 
1196 	if (cache_mode_name) {
1197 		vbdev->cfg.cache.cache_mode
1198 			= ocf_get_cache_mode(cache_mode_name);
1199 	} else if (!loadq) { /* In load path it is OK to pass NULL as cache mode */
1200 		SPDK_ERRLOG("No cache mode specified\n");
1201 		rc = -EINVAL;
1202 		goto error_free;
1203 	}
1204 	if (vbdev->cfg.cache.cache_mode < 0) {
1205 		SPDK_ERRLOG("Incorrect cache mode '%s'\n", cache_mode_name);
1206 		rc = -EINVAL;
1207 		goto error_free;
1208 	}
1209 
1210 	vbdev->name = strdup(vbdev_name);
1211 	if (!vbdev->name) {
1212 		goto error_mem;
1213 	}
1214 
1215 	vbdev->cache.name = strdup(cache_name);
1216 	if (!vbdev->cache.name) {
1217 		goto error_mem;
1218 	}
1219 
1220 	vbdev->core.name = strdup(core_name);
1221 	if (!vbdev->core.name) {
1222 		goto error_mem;
1223 	}
1224 
1225 	vbdev->cfg.loadq = loadq;
1226 	init_vbdev_config(vbdev);
1227 	TAILQ_INSERT_TAIL(&g_ocf_vbdev_head, vbdev, tailq);
1228 	return rc;
1229 
1230 error_mem:
1231 	rc = -ENOMEM;
1232 error_free:
1233 	free_vbdev(vbdev);
1234 	return rc;
1235 }
1236 
1237 /* Read configuration file at the start of SPDK application
1238  * This adds vbdevs to global list if some mentioned in config */
1239 static int
1240 vbdev_ocf_init(void)
1241 {
1242 	const char *vbdev_name, *modename, *cache_name, *core_name;
1243 	struct spdk_conf_section *sp;
1244 	int status;
1245 
1246 	status = vbdev_ocf_ctx_init();
1247 	if (status) {
1248 		SPDK_ERRLOG("OCF ctx initialization failed with=%d\n", status);
1249 		return status;
1250 	}
1251 
1252 	status = vbdev_ocf_volume_init();
1253 	if (status) {
1254 		vbdev_ocf_ctx_cleanup();
1255 		SPDK_ERRLOG("OCF volume initialization failed with=%d\n", status);
1256 		return status;
1257 	}
1258 
1259 	sp = spdk_conf_find_section(NULL, "OCF");
1260 	if (sp == NULL) {
1261 		return 0;
1262 	}
1263 
1264 	for (int i = 0; ; i++) {
1265 		if (!spdk_conf_section_get_nval(sp, "OCF", i)) {
1266 			break;
1267 		}
1268 
1269 		vbdev_name = spdk_conf_section_get_nmval(sp, "OCF", i, 0);
1270 		if (!vbdev_name) {
1271 			SPDK_ERRLOG("No vbdev name specified\n");
1272 			continue;
1273 		}
1274 
1275 		modename = spdk_conf_section_get_nmval(sp, "OCF", i, 1);
1276 		if (!modename) {
1277 			SPDK_ERRLOG("No modename specified for OCF vbdev '%s'\n", vbdev_name);
1278 			continue;
1279 		}
1280 
1281 		cache_name = spdk_conf_section_get_nmval(sp, "OCF", i, 2);
1282 		if (!cache_name) {
1283 			SPDK_ERRLOG("No cache device specified for OCF vbdev '%s'\n", vbdev_name);
1284 			continue;
1285 		}
1286 
1287 		core_name = spdk_conf_section_get_nmval(sp, "OCF", i, 3);
1288 		if (!core_name) {
1289 			SPDK_ERRLOG("No core devices specified for OCF vbdev '%s'\n", vbdev_name);
1290 			continue;
1291 		}
1292 
1293 		status = init_vbdev(vbdev_name, modename, cache_name, core_name, false);
1294 		if (status) {
1295 			SPDK_ERRLOG("Config initialization failed with code: %d\n", status);
1296 		}
1297 	}
1298 
1299 	return status;
1300 }
1301 
1302 /* Called after application shutdown started
1303  * Release memory of allocated structures here */
1304 static void
1305 vbdev_ocf_module_fini(void)
1306 {
1307 	struct vbdev_ocf *vbdev;
1308 
1309 	while ((vbdev = TAILQ_FIRST(&g_ocf_vbdev_head))) {
1310 		TAILQ_REMOVE(&g_ocf_vbdev_head, vbdev, tailq);
1311 		free_vbdev(vbdev);
1312 	}
1313 
1314 	vbdev_ocf_volume_cleanup();
1315 	vbdev_ocf_ctx_cleanup();
1316 }
1317 
1318 /* When base device gets unpluged this is called
1319  * We will unregister cache vbdev here
1320  * When cache device is removed, we delete every OCF bdev that used it */
1321 static void
1322 hotremove_cb(void *ctx)
1323 {
1324 	struct vbdev_ocf_base *base = ctx;
1325 	struct vbdev_ocf *vbdev;
1326 
1327 	if (!base->is_cache) {
1328 		if (base->parent->state.doing_finish) {
1329 			return;
1330 		}
1331 
1332 		SPDK_NOTICELOG("Deinitializing '%s' because its core device '%s' was removed\n",
1333 			       base->parent->name, base->name);
1334 		vbdev_ocf_delete(base->parent, NULL, NULL);
1335 		return;
1336 	}
1337 
1338 	TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) {
1339 		if (vbdev->state.doing_finish) {
1340 			continue;
1341 		}
1342 		if (strcmp(base->name, vbdev->cache.name) == 0) {
1343 			SPDK_NOTICELOG("Deinitializing '%s' because"
1344 				       " its cache device '%s' was removed\n",
1345 				       vbdev->name, base->name);
1346 			vbdev_ocf_delete(vbdev, NULL, NULL);
1347 		}
1348 	}
1349 }
1350 
1351 /* Open base SPDK bdev and claim it */
1352 static int
1353 attach_base(struct vbdev_ocf_base *base)
1354 {
1355 	int status;
1356 
1357 	if (base->attached) {
1358 		return -EALREADY;
1359 	}
1360 
1361 	/* If base cache bdev was already opened by other vbdev,
1362 	 * we just copy its descriptor here */
1363 	if (base->is_cache) {
1364 		struct vbdev_ocf_base *existing = get_other_cache_base(base);
1365 		if (existing) {
1366 			base->desc = existing->desc;
1367 			base->management_channel = existing->management_channel;
1368 			base->attached = true;
1369 			return 0;
1370 		}
1371 	}
1372 
1373 	status = spdk_bdev_open(base->bdev, true, hotremove_cb, base, &base->desc);
1374 	if (status) {
1375 		SPDK_ERRLOG("Unable to open device '%s' for writing\n", base->name);
1376 		return status;
1377 	}
1378 
1379 	status = spdk_bdev_module_claim_bdev(base->bdev, base->desc,
1380 					     &ocf_if);
1381 	if (status) {
1382 		SPDK_ERRLOG("Unable to claim device '%s'\n", base->name);
1383 		spdk_bdev_close(base->desc);
1384 		return status;
1385 	}
1386 
1387 	base->management_channel = spdk_bdev_get_io_channel(base->desc);
1388 	if (!base->management_channel) {
1389 		SPDK_ERRLOG("Unable to get io channel '%s'\n", base->name);
1390 		spdk_bdev_module_release_bdev(base->bdev);
1391 		spdk_bdev_close(base->desc);
1392 		return -ENOMEM;
1393 	}
1394 
1395 	/* Save the thread where the base device is opened */
1396 	base->thread = spdk_get_thread();
1397 
1398 	base->attached = true;
1399 	return status;
1400 }
1401 
1402 /* Attach base bdevs */
1403 static int
1404 attach_base_bdevs(struct vbdev_ocf *vbdev,
1405 		  struct spdk_bdev *cache_bdev,
1406 		  struct spdk_bdev *core_bdev)
1407 {
1408 	int rc = 0;
1409 
1410 	if (cache_bdev) {
1411 		vbdev->cache.bdev = cache_bdev;
1412 		rc |= attach_base(&vbdev->cache);
1413 	}
1414 
1415 	if (core_bdev) {
1416 		vbdev->core.bdev = core_bdev;
1417 		rc |= attach_base(&vbdev->core);
1418 	}
1419 
1420 	return rc;
1421 }
1422 
1423 /* Init and then start vbdev if all base devices are present */
1424 void
1425 vbdev_ocf_construct(const char *vbdev_name,
1426 		    const char *cache_mode_name,
1427 		    const char *cache_name,
1428 		    const char *core_name,
1429 		    bool loadq,
1430 		    void (*cb)(int, struct vbdev_ocf *, void *),
1431 		    void *cb_arg)
1432 {
1433 	int rc;
1434 	struct spdk_bdev *cache_bdev = spdk_bdev_get_by_name(cache_name);
1435 	struct spdk_bdev *core_bdev = spdk_bdev_get_by_name(core_name);
1436 	struct vbdev_ocf *vbdev;
1437 
1438 	rc = init_vbdev(vbdev_name, cache_mode_name, cache_name, core_name, loadq);
1439 	if (rc) {
1440 		cb(rc, NULL, cb_arg);
1441 		return;
1442 	}
1443 
1444 	vbdev = vbdev_ocf_get_by_name(vbdev_name);
1445 	if (vbdev == NULL) {
1446 		cb(-ENODEV, NULL, cb_arg);
1447 		return;
1448 	}
1449 
1450 	if (cache_bdev == NULL) {
1451 		SPDK_NOTICELOG("OCF bdev '%s' is waiting for cache device '%s' to connect\n",
1452 			       vbdev->name, cache_name);
1453 	}
1454 	if (core_bdev == NULL) {
1455 		SPDK_NOTICELOG("OCF bdev '%s' is waiting for core device '%s' to connect\n",
1456 			       vbdev->name, core_name);
1457 	}
1458 
1459 	rc = attach_base_bdevs(vbdev, cache_bdev, core_bdev);
1460 	if (rc) {
1461 		cb(rc, vbdev, cb_arg);
1462 		return;
1463 	}
1464 
1465 	if (core_bdev && cache_bdev) {
1466 		register_vbdev(vbdev, cb, cb_arg);
1467 	} else {
1468 		cb(0, vbdev, cb_arg);
1469 	}
1470 }
1471 
1472 /* This called if new device is created in SPDK application
1473  * If that device named as one of base bdevs of OCF vbdev,
1474  * claim and open them */
1475 static void
1476 vbdev_ocf_examine(struct spdk_bdev *bdev)
1477 {
1478 	const char *bdev_name = spdk_bdev_get_name(bdev);
1479 	struct vbdev_ocf *vbdev;
1480 
1481 	TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) {
1482 		if (vbdev->state.doing_finish) {
1483 			continue;
1484 		}
1485 
1486 		if (!strcmp(bdev_name, vbdev->cache.name)) {
1487 			attach_base_bdevs(vbdev, bdev, NULL);
1488 			continue;
1489 		}
1490 		if (!strcmp(bdev_name, vbdev->core.name)) {
1491 			attach_base_bdevs(vbdev, NULL, bdev);
1492 			break;
1493 		}
1494 	}
1495 	spdk_bdev_module_examine_done(&ocf_if);
1496 }
1497 
1498 struct metadata_probe_ctx {
1499 	struct vbdev_ocf_base base;
1500 	ocf_volume_t volume;
1501 
1502 	struct ocf_volume_uuid *core_uuids;
1503 	unsigned int uuid_count;
1504 
1505 	int result;
1506 	int refcnt;
1507 };
1508 
1509 static void
1510 _examine_ctx_put(void *ctx)
1511 {
1512 	struct spdk_bdev_desc *desc = ctx;
1513 
1514 	spdk_bdev_close(desc);
1515 }
1516 
1517 static void
1518 examine_ctx_put(struct metadata_probe_ctx *ctx)
1519 {
1520 	unsigned int i;
1521 
1522 	ctx->refcnt--;
1523 	if (ctx->refcnt > 0) {
1524 		return;
1525 	}
1526 
1527 	if (ctx->result) {
1528 		SPDK_ERRLOG("OCF metadata probe for bdev '%s' failed with %d\n",
1529 			    spdk_bdev_get_name(ctx->base.bdev), ctx->result);
1530 	}
1531 
1532 	if (ctx->base.desc) {
1533 		/* Close the underlying bdev on its same opened thread. */
1534 		if (ctx->base.thread && ctx->base.thread != spdk_get_thread()) {
1535 			spdk_thread_send_msg(ctx->base.thread, _examine_ctx_put, ctx->base.desc);
1536 		} else {
1537 			spdk_bdev_close(ctx->base.desc);
1538 		}
1539 	}
1540 
1541 	if (ctx->volume) {
1542 		ocf_volume_destroy(ctx->volume);
1543 	}
1544 
1545 	if (ctx->core_uuids) {
1546 		for (i = 0; i < ctx->uuid_count; i++) {
1547 			free(ctx->core_uuids[i].data);
1548 		}
1549 	}
1550 	free(ctx->core_uuids);
1551 
1552 	examine_done(ctx->result, NULL, ctx->base.bdev);
1553 	free(ctx);
1554 }
1555 
1556 static void
1557 metadata_probe_construct_cb(int rc, struct vbdev_ocf *vbdev, void *vctx)
1558 {
1559 	struct metadata_probe_ctx *ctx = vctx;
1560 
1561 	examine_ctx_put(ctx);
1562 }
1563 
1564 /* This is second callback for ocf_metadata_probe_cores()
1565  * Here we create vbdev configurations based on UUIDs */
1566 static void
1567 metadata_probe_cores_construct(void *priv, int error, unsigned int num_cores)
1568 {
1569 	struct metadata_probe_ctx *ctx = priv;
1570 	const char *vbdev_name;
1571 	const char *core_name;
1572 	const char *cache_name;
1573 	unsigned int i;
1574 
1575 	if (error) {
1576 		ctx->result = error;
1577 		examine_ctx_put(ctx);
1578 		return;
1579 	}
1580 
1581 	for (i = 0; i < num_cores; i++) {
1582 		core_name = ocf_uuid_to_str(&ctx->core_uuids[i]);
1583 		vbdev_name = core_name + strlen(core_name) + 1;
1584 		cache_name = vbdev_name + strlen(vbdev_name) + 1;
1585 
1586 		if (strcmp(ctx->base.bdev->name, cache_name)) {
1587 			SPDK_NOTICELOG("OCF metadata found on %s belongs to bdev named '%s'\n",
1588 				       ctx->base.bdev->name, cache_name);
1589 		}
1590 
1591 		ctx->refcnt++;
1592 		vbdev_ocf_construct(vbdev_name, NULL, cache_name, core_name, true,
1593 				    metadata_probe_construct_cb, ctx);
1594 	}
1595 
1596 	examine_ctx_put(ctx);
1597 }
1598 
1599 /* This callback is called after OCF reads cores UUIDs from cache metadata
1600  * Here we allocate memory for those UUIDs and call ocf_metadata_probe_cores() again */
1601 static void
1602 metadata_probe_cores_get_num(void *priv, int error, unsigned int num_cores)
1603 {
1604 	struct metadata_probe_ctx *ctx = priv;
1605 	unsigned int i;
1606 
1607 	if (error) {
1608 		ctx->result = error;
1609 		examine_ctx_put(ctx);
1610 		return;
1611 	}
1612 
1613 	ctx->uuid_count = num_cores;
1614 	ctx->core_uuids = calloc(num_cores, sizeof(struct ocf_volume_uuid));
1615 	if (!ctx->core_uuids) {
1616 		ctx->result = -ENOMEM;
1617 		examine_ctx_put(ctx);
1618 		return;
1619 	}
1620 
1621 	for (i = 0; i < ctx->uuid_count; i++) {
1622 		ctx->core_uuids[i].size = OCF_VOLUME_UUID_MAX_SIZE;
1623 		ctx->core_uuids[i].data = malloc(OCF_VOLUME_UUID_MAX_SIZE);
1624 		if (!ctx->core_uuids[i].data) {
1625 			ctx->result = -ENOMEM;
1626 			examine_ctx_put(ctx);
1627 			return;
1628 		}
1629 	}
1630 
1631 	ocf_metadata_probe_cores(vbdev_ocf_ctx, ctx->volume, ctx->core_uuids, ctx->uuid_count,
1632 				 metadata_probe_cores_construct, ctx);
1633 }
1634 
1635 static void
1636 metadata_probe_cb(void *priv, int rc,
1637 		  struct ocf_metadata_probe_status *status)
1638 {
1639 	struct metadata_probe_ctx *ctx = priv;
1640 
1641 	if (rc) {
1642 		/* -ENODATA means device does not have cache metadata on it */
1643 		if (rc != -OCF_ERR_NO_METADATA) {
1644 			ctx->result = rc;
1645 		}
1646 		examine_ctx_put(ctx);
1647 		return;
1648 	}
1649 
1650 	ocf_metadata_probe_cores(vbdev_ocf_ctx, ctx->volume, NULL, 0,
1651 				 metadata_probe_cores_get_num, ctx);
1652 }
1653 
1654 /* This is called after vbdev_ocf_examine
1655  * It allows to delay application initialization
1656  * until all OCF bdevs get registered
1657  * If vbdev has all of its base devices it starts asynchronously here
1658  * We first check if bdev appears in configuration,
1659  * if not we do metadata_probe() to create its configuration from bdev metadata */
1660 static void
1661 vbdev_ocf_examine_disk(struct spdk_bdev *bdev)
1662 {
1663 	const char *bdev_name = spdk_bdev_get_name(bdev);
1664 	struct vbdev_ocf *vbdev;
1665 	struct metadata_probe_ctx *ctx;
1666 	bool created_from_config = false;
1667 	int rc;
1668 
1669 	examine_start(bdev);
1670 
1671 	TAILQ_FOREACH(vbdev, &g_ocf_vbdev_head, tailq) {
1672 		if (vbdev->state.doing_finish || vbdev->state.started) {
1673 			continue;
1674 		}
1675 
1676 		if (!strcmp(bdev_name, vbdev->cache.name)) {
1677 			examine_start(bdev);
1678 			register_vbdev(vbdev, examine_done, bdev);
1679 			created_from_config = true;
1680 			continue;
1681 		}
1682 		if (!strcmp(bdev_name, vbdev->core.name)) {
1683 			examine_start(bdev);
1684 			register_vbdev(vbdev, examine_done, bdev);
1685 			examine_done(0, NULL, bdev);
1686 			return;
1687 		}
1688 	}
1689 
1690 	/* If devices is discovered during config we do not check for metadata */
1691 	if (created_from_config) {
1692 		examine_done(0, NULL, bdev);
1693 		return;
1694 	}
1695 
1696 	/* Metadata probe path
1697 	 * We create temporary OCF volume and a temporary base structure
1698 	 * to use them for ocf_metadata_probe() and for bottom adapter IOs
1699 	 * Then we get UUIDs of core devices an create configurations based on them */
1700 	ctx = calloc(1, sizeof(*ctx));
1701 	if (!ctx) {
1702 		examine_done(-ENOMEM, NULL, bdev);
1703 		return;
1704 	}
1705 
1706 	ctx->base.bdev = bdev;
1707 	ctx->refcnt = 1;
1708 
1709 	rc = spdk_bdev_open(ctx->base.bdev, true, NULL, NULL, &ctx->base.desc);
1710 	if (rc) {
1711 		ctx->result = rc;
1712 		examine_ctx_put(ctx);
1713 		return;
1714 	}
1715 
1716 	rc = ocf_ctx_volume_create(vbdev_ocf_ctx, &ctx->volume, NULL, SPDK_OBJECT);
1717 	if (rc) {
1718 		ctx->result = rc;
1719 		examine_ctx_put(ctx);
1720 		return;
1721 	}
1722 
1723 	rc = ocf_volume_open(ctx->volume, &ctx->base);
1724 	if (rc) {
1725 		ctx->result = rc;
1726 		examine_ctx_put(ctx);
1727 		return;
1728 	}
1729 
1730 	/* Save the thread where the base device is opened */
1731 	ctx->base.thread = spdk_get_thread();
1732 
1733 	ocf_metadata_probe(vbdev_ocf_ctx, ctx->volume, metadata_probe_cb, ctx);
1734 }
1735 
1736 static int
1737 vbdev_ocf_get_ctx_size(void)
1738 {
1739 	return sizeof(struct bdev_ocf_data);
1740 }
1741 
1742 static void
1743 fini_start(void)
1744 {
1745 	g_fini_started = true;
1746 }
1747 
1748 /* Module-global function table
1749  * Does not relate to vbdev instances */
1750 static struct spdk_bdev_module ocf_if = {
1751 	.name = "ocf",
1752 	.module_init = vbdev_ocf_init,
1753 	.fini_start = fini_start,
1754 	.module_fini = vbdev_ocf_module_fini,
1755 	.config_text = NULL,
1756 	.get_ctx_size = vbdev_ocf_get_ctx_size,
1757 	.examine_config = vbdev_ocf_examine,
1758 	.examine_disk   = vbdev_ocf_examine_disk,
1759 };
1760 SPDK_BDEV_MODULE_REGISTER(ocf, &ocf_if);
1761 
1762 SPDK_LOG_REGISTER_COMPONENT("vbdev_ocf", SPDK_TRACE_VBDEV_OCF)
1763