xref: /spdk/module/bdev/ocf/utils.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 #include "spdk/stdinc.h"
35 
36 #include "utils.h"
37 #include "vbdev_ocf.h"
38 
39 static char *cache_modes[ocf_cache_mode_max] = {
40 	[ocf_cache_mode_wt] = "wt",
41 	[ocf_cache_mode_wb] = "wb",
42 	[ocf_cache_mode_wa] = "wa",
43 	[ocf_cache_mode_pt] = "pt",
44 	[ocf_cache_mode_wi] = "wi",
45 	[ocf_cache_mode_wo] = "wo",
46 };
47 
48 ocf_cache_mode_t
49 ocf_get_cache_mode(const char *cache_mode)
50 {
51 	int i;
52 
53 	for (i = 0; i < ocf_cache_mode_max; i++) {
54 		if (strcmp(cache_mode, cache_modes[i]) == 0) {
55 			return i;
56 		}
57 	}
58 
59 	return ocf_cache_mode_none;
60 }
61 
62 const char *
63 ocf_get_cache_modename(ocf_cache_mode_t mode)
64 {
65 	if (mode > ocf_cache_mode_none && mode < ocf_cache_mode_max) {
66 		return cache_modes[mode];
67 	} else {
68 		return NULL;
69 	}
70 }
71 
72 int
73 vbdev_ocf_mngt_start(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_fn *path,
74 		     vbdev_ocf_mngt_callback cb, void *cb_arg)
75 {
76 	if (vbdev->mngt_ctx.current_step) {
77 		return -EBUSY;
78 	}
79 
80 	memset(&vbdev->mngt_ctx, 0, sizeof(vbdev->mngt_ctx));
81 
82 	vbdev->mngt_ctx.current_step = path;
83 	vbdev->mngt_ctx.cb = cb;
84 	vbdev->mngt_ctx.cb_arg = cb_arg;
85 
86 	(*vbdev->mngt_ctx.current_step)(vbdev);
87 
88 	return 0;
89 }
90 
91 void
92 vbdev_ocf_mngt_stop(struct vbdev_ocf *vbdev, vbdev_ocf_mngt_fn *rollback_path, int status)
93 {
94 	if (status) {
95 		vbdev->mngt_ctx.status = status;
96 	}
97 
98 	if (vbdev->mngt_ctx.status && rollback_path) {
99 		vbdev->mngt_ctx.poller_fn = NULL;
100 		vbdev->mngt_ctx.current_step = rollback_path;
101 		(*vbdev->mngt_ctx.current_step)(vbdev);
102 		return;
103 	}
104 
105 	if (vbdev->mngt_ctx.cb) {
106 		vbdev->mngt_ctx.cb(vbdev->mngt_ctx.status, vbdev, vbdev->mngt_ctx.cb_arg);
107 	}
108 
109 	memset(&vbdev->mngt_ctx, 0, sizeof(vbdev->mngt_ctx));
110 }
111 
112 void
113 vbdev_ocf_mngt_continue(struct vbdev_ocf *vbdev, int status)
114 {
115 	if (vbdev->mngt_ctx.current_step == NULL) {
116 		return;
117 	}
118 
119 	assert((*vbdev->mngt_ctx.current_step) != NULL);
120 
121 	vbdev->mngt_ctx.status = status;
122 
123 	vbdev->mngt_ctx.current_step++;
124 	if (*vbdev->mngt_ctx.current_step) {
125 		(*vbdev->mngt_ctx.current_step)(vbdev);
126 		return;
127 	}
128 
129 	vbdev_ocf_mngt_stop(vbdev, NULL, 0);
130 }
131 
132 int
133 vbdev_ocf_mngt_get_status(struct vbdev_ocf *vbdev)
134 {
135 	return vbdev->mngt_ctx.status;
136 }
137