xref: /spdk/module/bdev/ocf/vbdev_ocf.h (revision 927f1fd57bd004df581518466ec4c1b8083e5d23)
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 #ifndef SPDK_VBDEV_OCF_H
35 #define SPDK_VBDEV_OCF_H
36 
37 #include <ocf/ocf.h>
38 
39 #include "spdk/bdev.h"
40 #include "spdk/bdev_module.h"
41 
42 #define VBDEV_OCF_MD_MAX_LEN 4096
43 
44 struct vbdev_ocf;
45 
46 /* Context for OCF queue poller
47  * Used for mapping SPDK threads to OCF queues */
48 struct vbdev_ocf_qctx {
49 	/* OCF queue. Contains OCF requests */
50 	struct ocf_queue            *queue;
51 	/* Poller for OCF queue. Runs OCF requests */
52 	struct spdk_poller          *poller;
53 	/* Reference to parent vbdev */
54 	struct vbdev_ocf            *vbdev;
55 	/* Base devices channels */
56 	struct spdk_io_channel      *cache_ch;
57 	struct spdk_io_channel      *core_ch;
58 	/* If true, we have to free this context on queue stop */
59 	bool allocated;
60 	/* Link to per-bdev list of queue contexts */
61 	TAILQ_ENTRY(vbdev_ocf_qctx)  tailq;
62 };
63 
64 /* Important states */
65 struct vbdev_ocf_state {
66 	/* From the moment when clean delete started */
67 	bool                         doing_clean_delete;
68 	/* From the moment when finish started */
69 	bool                         doing_finish;
70 	/* From the moment when reset IO received, until it is completed */
71 	bool                         doing_reset;
72 	/* From the moment when exp_bdev is registered */
73 	bool                         started;
74 	/* From the moment when register path started */
75 	bool                         starting;
76 	/* Status of last attempt for stopping this device */
77 	int                          stop_status;
78 };
79 
80 /*
81  * OCF cache configuration options
82  */
83 struct vbdev_ocf_config {
84 	/* Initial cache configuration  */
85 	struct ocf_mngt_cache_config        cache;
86 
87 	/* Cache device config */
88 	struct ocf_mngt_cache_device_config device;
89 
90 	/* Core initial config */
91 	struct ocf_mngt_core_config         core;
92 
93 	/* Load flag, if set to true, then we will try load cache instance from disk,
94 	 * otherwise we will create new cache on that disk */
95 	bool                                loadq;
96 };
97 
98 /* Types for management operations */
99 typedef void (*vbdev_ocf_mngt_fn)(struct vbdev_ocf *);
100 typedef void (*vbdev_ocf_mngt_callback)(int, struct vbdev_ocf *, void *);
101 
102 /* Context for asynchronous management operations
103  * Single management operation usually contains a list of sub procedures,
104  * this structure handles sharing between those sub procedures */
105 struct vbdev_ocf_mngt_ctx {
106 	/* Pointer to function that is currently being executed
107 	 * It gets incremented on each step until it dereferences to NULL */
108 	vbdev_ocf_mngt_fn                  *current_step;
109 
110 	/* Function that gets invoked by poller on each iteration */
111 	vbdev_ocf_mngt_fn                   poller_fn;
112 	/* Poller timeout time stamp - when the poller should stop with error */
113 	uint64_t                            timeout_ts;
114 
115 	/* Status of management operation */
116 	int                                 status;
117 
118 	/* External callback and its argument */
119 	vbdev_ocf_mngt_callback             cb;
120 	void                               *cb_arg;
121 };
122 
123 /* Base device info */
124 struct vbdev_ocf_base {
125 	/* OCF internal name */
126 	char                        *name;
127 
128 	/* True if this is a caching device */
129 	bool                         is_cache;
130 
131 	/* Connected SPDK block device */
132 	struct spdk_bdev            *bdev;
133 
134 	/* SPDK device io handle */
135 	struct spdk_bdev_desc       *desc;
136 
137 	/* True if SPDK bdev has been claimed and opened for writing */
138 	bool                         attached;
139 
140 	/* Channel for cleaner operations */
141 	struct spdk_io_channel      *management_channel;
142 
143 	/* Reference to main vbdev */
144 	struct vbdev_ocf            *parent;
145 
146 	/* thread where base device is opened */
147 	struct spdk_thread	    *thread;
148 };
149 
150 /*
151  * The main information provider
152  * It's also registered as io_device
153  */
154 struct vbdev_ocf {
155 	/* Exposed unique name */
156 	char                        *name;
157 
158 	/* Base bdevs */
159 	struct vbdev_ocf_base        cache;
160 	struct vbdev_ocf_base        core;
161 
162 	/* Base bdevs OCF objects */
163 	ocf_cache_t                  ocf_cache;
164 	ocf_core_t                   ocf_core;
165 
166 	/* Parameters */
167 	struct vbdev_ocf_config      cfg;
168 	struct vbdev_ocf_state       state;
169 
170 	/* Management context */
171 	struct vbdev_ocf_mngt_ctx    mngt_ctx;
172 	/* Cache conext */
173 	struct vbdev_ocf_cache_ctx  *cache_ctx;
174 
175 	/* Exposed SPDK bdev. Registered in bdev layer */
176 	struct spdk_bdev             exp_bdev;
177 
178 	/* OCF uuid for core device of this vbdev */
179 	char uuid[VBDEV_OCF_MD_MAX_LEN];
180 
181 	/* Link to global list of this type structures */
182 	TAILQ_ENTRY(vbdev_ocf)       tailq;
183 };
184 
185 void vbdev_ocf_construct(
186 	const char *vbdev_name,
187 	const char *cache_mode_name,
188 	const uint64_t cache_line_size,
189 	const char *cache_name,
190 	const char *core_name,
191 	bool loadq,
192 	void (*cb)(int, struct vbdev_ocf *, void *),
193 	void *cb_arg);
194 
195 /* If vbdev is online, return its object */
196 struct vbdev_ocf *vbdev_ocf_get_by_name(const char *name);
197 
198 /* Return matching base if parent vbdev is online */
199 struct vbdev_ocf_base *vbdev_ocf_get_base_by_name(const char *name);
200 
201 /* Stop OCF cache and unregister SPDK bdev */
202 int vbdev_ocf_delete(struct vbdev_ocf *vbdev, void (*cb)(void *, int), void *cb_arg);
203 
204 int vbdev_ocf_delete_clean(struct vbdev_ocf *vbdev, void (*cb)(void *, int), void *cb_arg);
205 
206 /* Set new cache mode on OCF cache */
207 void vbdev_ocf_set_cache_mode(
208 	struct vbdev_ocf *vbdev,
209 	const char *cache_mode_name,
210 	void (*cb)(int, struct vbdev_ocf *, void *),
211 	void *cb_arg);
212 
213 /* Set sequential cutoff parameters on OCF cache */
214 void vbdev_ocf_set_seqcutoff(
215 	struct vbdev_ocf *vbdev,
216 	const char *policy_name,
217 	uint32_t threshold,
218 	uint32_t promotion_count,
219 	void (*cb)(int, void *),
220 	void *cb_arg);
221 
222 typedef void (*vbdev_ocf_foreach_fn)(struct vbdev_ocf *, void *);
223 
224 /* Execute fn for each OCF device that is online or waits for base devices */
225 void vbdev_ocf_foreach(vbdev_ocf_foreach_fn fn, void *ctx);
226 
227 #endif
228