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