4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef SPDK_VBDEV_OCF_H
35 #define SPDK_VBDEV_OCF_H
39 #include "spdk/bdev.h"
40 #include "spdk/bdev_module.h"
42 #define VBDEV_OCF_MD_MAX_LEN 4096
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 */
60 /* Link to per-bdev list of queue contexts */
61 TAILQ_ENTRY(vbdev_ocf_qctx
) tailq
;
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 */
70 /* From the moment when reset IO recieved, until it is completed */
72 /* From the moment when exp_bdev is registered */
74 /* From the moment when register path started */
76 /* Status of last attempt for stopping this device */
81 * OCF cache configuration options
83 struct vbdev_ocf_config
{
84 /* Initial cache configuration */
85 struct ocf_mngt_cache_config cache
;
87 /* Cache device config */
88 struct ocf_mngt_cache_device_config device
;
90 /* Core initial config */
91 struct ocf_mngt_core_config core
;
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 */
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 *);
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
;
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 */
115 /* Status of management operation */
118 /* External callback and its argument */
119 vbdev_ocf_mngt_callback cb
;
123 /* Base device info */
124 struct vbdev_ocf_base
{
125 /* OCF internal name */
128 /* True if this is a caching device */
131 /* Connected SPDK block device */
132 struct spdk_bdev
*bdev
;
134 /* SPDK device io handle */
135 struct spdk_bdev_desc
*desc
;
137 /* True if SPDK bdev has been claimed and opened for writing */
140 /* Channel for cleaner operations */
141 struct spdk_io_channel
*management_channel
;
143 /* Reference to main vbdev */
144 struct vbdev_ocf
*parent
;
146 /* thread where base device is opened */
147 struct spdk_thread
*thread
;
151 * The main information provider
152 * It's also registered as io_device
155 /* Exposed unique name */
159 struct vbdev_ocf_base cache
;
160 struct vbdev_ocf_base core
;
162 /* Base bdevs OCF objects */
163 ocf_cache_t ocf_cache
;
167 struct vbdev_ocf_config cfg
;
168 struct vbdev_ocf_state state
;
170 /* Management context */
171 struct vbdev_ocf_mngt_ctx mngt_ctx
;
173 struct vbdev_ocf_cache_ctx
*cache_ctx
;
175 /* Exposed SPDK bdev. Registered in bdev layer */
176 struct spdk_bdev exp_bdev
;
178 /* OCF uuid for core device of this vbdev */
179 char uuid
[VBDEV_OCF_MD_MAX_LEN
];
181 /* Link to global list of this type structures */
182 TAILQ_ENTRY(vbdev_ocf
) tailq
;
185 void vbdev_ocf_construct(
186 const char *vbdev_name
,
187 const char *cache_mode_name
,
188 const char *cache_name
,
189 const char *core_name
,
191 void (*cb
)(int, struct vbdev_ocf
*, void *),
194 /* If vbdev is online, return its object */
195 struct vbdev_ocf
*vbdev_ocf_get_by_name(const char *name
);
197 /* Return matching base if parent vbdev is online */
198 struct vbdev_ocf_base
*vbdev_ocf_get_base_by_name(const char *name
);
200 /* Stop OCF cache and unregister SPDK bdev */
201 int vbdev_ocf_delete(struct vbdev_ocf
*vbdev
, void (*cb
)(void *, int), void *cb_arg
);
203 int vbdev_ocf_delete_clean(struct vbdev_ocf
*vbdev
, void (*cb
)(void *, int), void *cb_arg
);
205 typedef void (*vbdev_ocf_foreach_fn
)(struct vbdev_ocf
*, void *);
207 /* Execute fn for each OCF device that is online or waits for base devices */
208 void vbdev_ocf_foreach(vbdev_ocf_foreach_fn fn
, void *ctx
);