xref: /spdk/module/bdev/ocf/vbdev_ocf.h (revision 407e88fd2ab020d753e33014cf759353a9901b51)
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_qcxt {
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 	/* Link to per-bdev list of queue contexts */
59 	TAILQ_ENTRY(vbdev_ocf_qcxt)  tailq;
60 };
61 
62 /* Important states */
63 struct vbdev_ocf_state {
64 	/* From the moment when finish started */
65 	bool                         doing_finish;
66 	/* From the moment when reset IO recieved, until it is completed */
67 	bool                         doing_reset;
68 	/* From the moment when exp_bdev is registered */
69 	bool                         started;
70 	/* Status of last attempt for stopping this device */
71 	int                          stop_status;
72 };
73 
74 /*
75  * OCF cache configuration options
76  */
77 struct vbdev_ocf_config {
78 	/* Initial cache configuration  */
79 	struct ocf_mngt_cache_config        cache;
80 
81 	/* Cache device config */
82 	struct ocf_mngt_cache_device_config device;
83 
84 	/* Core initial config */
85 	struct ocf_mngt_core_config         core;
86 
87 	/* Load flag, if set to true, then we will try load cache instance from disk,
88 	 * otherwise we will create new cache on that disk */
89 	bool                                loadq;
90 };
91 
92 /* Types for management operations */
93 typedef void (*vbdev_ocf_mngt_fn)(struct vbdev_ocf *);
94 typedef void (*vbdev_ocf_mngt_callback)(int, struct vbdev_ocf *, void *);
95 
96 /* Context for asynchronous management operations
97  * Single management operation usually contains a list of sub procedures,
98  * this structure handles sharing between those sub procedures */
99 struct vbdev_ocf_mngt_ctx {
100 	/* Pointer to function that is currently being executed
101 	 * It gets incremented on each step until it dereferences to NULL */
102 	vbdev_ocf_mngt_fn                  *current_step;
103 
104 	/* Poller, registered once per whole management operation */
105 	struct spdk_poller                 *poller;
106 	/* Function that gets invoked by poller on each iteration */
107 	vbdev_ocf_mngt_fn                   poller_fn;
108 	/* Poller timeout time stamp - when the poller should stop with error */
109 	uint64_t                            timeout_ts;
110 
111 	/* Status of management operation */
112 	int                                 status;
113 
114 	/* External callback and its argument */
115 	vbdev_ocf_mngt_callback             cb;
116 	void                               *cb_arg;
117 };
118 
119 /* Base device info */
120 struct vbdev_ocf_base {
121 	/* OCF unique internal id */
122 	int                          id;
123 
124 	/* OCF internal name */
125 	char                        *name;
126 
127 	/* True if this is a caching device */
128 	bool                         is_cache;
129 
130 	/* Connected SPDK block device */
131 	struct spdk_bdev            *bdev;
132 
133 	/* SPDK device io handle */
134 	struct spdk_bdev_desc       *desc;
135 
136 	/* True if SPDK bdev has been claimed and opened for writing */
137 	bool                         attached;
138 
139 	/* Channel for cleaner operations */
140 	struct spdk_io_channel      *management_channel;
141 
142 	/* Reference to main vbdev */
143 	struct vbdev_ocf            *parent;
144 };
145 
146 /*
147  * The main information provider
148  * It's also registered as io_device
149  */
150 struct vbdev_ocf {
151 	/* Exposed unique name */
152 	char                        *name;
153 
154 	/* Base bdevs */
155 	struct vbdev_ocf_base        cache;
156 	struct vbdev_ocf_base        core;
157 
158 	/* Base bdevs OCF objects */
159 	ocf_cache_t                  ocf_cache;
160 	ocf_core_t                   ocf_core;
161 
162 	/* Parameters */
163 	struct vbdev_ocf_config      cfg;
164 	struct vbdev_ocf_state       state;
165 
166 	/* Management context */
167 	struct vbdev_ocf_mngt_ctx    mngt_ctx;
168 	/* Cache conext */
169 	struct vbdev_ocf_cache_ctx  *cache_ctx;
170 
171 	/* Exposed SPDK bdev. Registered in bdev layer */
172 	struct spdk_bdev             exp_bdev;
173 
174 	/* OCF uuid for core device of this vbdev */
175 	char uuid[VBDEV_OCF_MD_MAX_LEN];
176 
177 	/* Link to global list of this type structures */
178 	TAILQ_ENTRY(vbdev_ocf)       tailq;
179 };
180 
181 void vbdev_ocf_construct(
182 	const char *vbdev_name,
183 	const char *cache_mode_name,
184 	const char *cache_name,
185 	const char *core_name,
186 	bool loadq,
187 	void (*cb)(int, struct vbdev_ocf *, void *),
188 	void *cb_arg);
189 
190 /* If vbdev is online, return its object */
191 struct vbdev_ocf *vbdev_ocf_get_by_name(const char *name);
192 
193 /* Return matching base if parent vbdev is online */
194 struct vbdev_ocf_base *vbdev_ocf_get_base_by_name(const char *name);
195 
196 /* Stop OCF cache and unregister SPDK bdev */
197 int vbdev_ocf_delete(struct vbdev_ocf *vbdev, void (*cb)(void *, int), void *cb_arg);
198 
199 typedef void (*vbdev_ocf_foreach_fn)(struct vbdev_ocf *, void *);
200 
201 /* Execute fn for each OCF device that is online or waits for base devices */
202 void vbdev_ocf_foreach(vbdev_ocf_foreach_fn fn, void *ctx);
203 
204 #endif
205