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 "blobstore.h" 37 #include "request.h" 38 39 #include "spdk/io_channel.h" 40 #include "spdk/queue.h" 41 42 #include "spdk_internal/log.h" 43 44 void 45 spdk_bs_call_cpl(struct spdk_bs_cpl *cpl, int bserrno) 46 { 47 switch (cpl->type) { 48 case SPDK_BS_CPL_TYPE_BS_BASIC: 49 cpl->u.bs_basic.cb_fn(cpl->u.bs_basic.cb_arg, 50 bserrno); 51 break; 52 case SPDK_BS_CPL_TYPE_BS_HANDLE: 53 cpl->u.bs_handle.cb_fn(cpl->u.bs_handle.cb_arg, 54 cpl->u.bs_handle.bs, 55 bserrno); 56 break; 57 case SPDK_BS_CPL_TYPE_BLOB_BASIC: 58 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, 59 bserrno); 60 break; 61 case SPDK_BS_CPL_TYPE_BLOBID: 62 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, 63 cpl->u.blobid.blobid, 64 bserrno); 65 break; 66 case SPDK_BS_CPL_TYPE_BLOB_HANDLE: 67 cpl->u.blob_handle.cb_fn(cpl->u.blob_handle.cb_arg, 68 cpl->u.blob_handle.blob, 69 bserrno); 70 break; 71 case SPDK_BS_CPL_TYPE_NESTED_SEQUENCE: 72 cpl->u.nested_seq.cb_fn(cpl->u.nested_seq.cb_arg, 73 cpl->u.nested_seq.parent, 74 bserrno); 75 break; 76 case SPDK_BS_CPL_TYPE_NONE: 77 /* this completion's callback is handled elsewhere */ 78 break; 79 } 80 } 81 82 static void 83 spdk_bs_request_set_complete(struct spdk_bs_request_set *set) 84 { 85 struct spdk_bs_cpl cpl = set->cpl; 86 int bserrno = set->bserrno; 87 88 TAILQ_INSERT_TAIL(&set->channel->reqs, set, link); 89 90 spdk_bs_call_cpl(&cpl, bserrno); 91 } 92 93 static void 94 spdk_bs_sequence_completion(struct spdk_io_channel *channel, void *cb_arg, int bserrno) 95 { 96 struct spdk_bs_request_set *set = cb_arg; 97 98 set->bserrno = bserrno; 99 set->u.sequence.cb_fn((spdk_bs_sequence_t *)set, set->u.sequence.cb_arg, bserrno); 100 } 101 102 spdk_bs_sequence_t * 103 spdk_bs_sequence_start(struct spdk_io_channel *_channel, 104 struct spdk_bs_cpl *cpl) 105 { 106 struct spdk_bs_channel *channel; 107 struct spdk_bs_request_set *set; 108 109 channel = spdk_io_channel_get_ctx(_channel); 110 111 set = TAILQ_FIRST(&channel->reqs); 112 if (!set) { 113 return NULL; 114 } 115 TAILQ_REMOVE(&channel->reqs, set, link); 116 117 set->cpl = *cpl; 118 set->bserrno = 0; 119 set->channel = channel; 120 121 set->cb_args.cb_fn = spdk_bs_sequence_completion; 122 set->cb_args.cb_arg = set; 123 set->cb_args.channel = channel->dev_channel; 124 125 return (spdk_bs_sequence_t *)set; 126 } 127 128 void 129 spdk_bs_sequence_read_bs_dev(spdk_bs_sequence_t *seq, struct spdk_bs_dev *bs_dev, 130 void *payload, uint64_t lba, uint32_t lba_count, 131 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 132 { 133 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 134 struct spdk_bs_channel *channel = set->channel; 135 136 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba); 137 138 set->u.sequence.cb_fn = cb_fn; 139 set->u.sequence.cb_arg = cb_arg; 140 141 bs_dev->read(bs_dev, channel->dev_channel, payload, lba, lba_count, &set->cb_args); 142 } 143 144 void 145 spdk_bs_sequence_read(spdk_bs_sequence_t *seq, void *payload, 146 uint64_t lba, uint32_t lba_count, 147 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 148 { 149 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 150 struct spdk_bs_channel *channel = set->channel; 151 152 spdk_bs_sequence_read_bs_dev(seq, channel->dev, payload, lba, lba_count, cb_fn, cb_arg); 153 } 154 155 void 156 spdk_bs_sequence_write(spdk_bs_sequence_t *seq, void *payload, 157 uint64_t lba, uint32_t lba_count, 158 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 159 { 160 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 161 struct spdk_bs_channel *channel = set->channel; 162 163 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba); 164 165 set->u.sequence.cb_fn = cb_fn; 166 set->u.sequence.cb_arg = cb_arg; 167 168 channel->dev->write(channel->dev, channel->dev_channel, payload, lba, lba_count, 169 &set->cb_args); 170 } 171 172 void 173 spdk_bs_sequence_readv_bs_dev(spdk_bs_sequence_t *seq, struct spdk_bs_dev *bs_dev, 174 struct iovec *iov, int iovcnt, uint64_t lba, uint32_t lba_count, 175 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 176 { 177 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 178 struct spdk_bs_channel *channel = set->channel; 179 180 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba); 181 182 set->u.sequence.cb_fn = cb_fn; 183 set->u.sequence.cb_arg = cb_arg; 184 185 bs_dev->readv(bs_dev, channel->dev_channel, iov, iovcnt, lba, lba_count, 186 &set->cb_args); 187 } 188 189 void 190 spdk_bs_sequence_readv(spdk_bs_sequence_t *seq, struct iovec *iov, int iovcnt, 191 uint64_t lba, uint32_t lba_count, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 192 { 193 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 194 struct spdk_bs_channel *channel = set->channel; 195 196 spdk_bs_sequence_readv_bs_dev(seq, channel->dev, iov, iovcnt, lba, lba_count, cb_fn, cb_arg); 197 } 198 199 void 200 spdk_bs_sequence_writev(spdk_bs_sequence_t *seq, struct iovec *iov, int iovcnt, 201 uint64_t lba, uint32_t lba_count, 202 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 203 { 204 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 205 struct spdk_bs_channel *channel = set->channel; 206 207 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba); 208 209 set->u.sequence.cb_fn = cb_fn; 210 set->u.sequence.cb_arg = cb_arg; 211 212 channel->dev->writev(channel->dev, channel->dev_channel, iov, iovcnt, lba, lba_count, 213 &set->cb_args); 214 } 215 216 void 217 spdk_bs_sequence_flush(spdk_bs_sequence_t *seq, 218 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 219 { 220 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 221 struct spdk_bs_channel *channel = set->channel; 222 223 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Flushing\n"); 224 225 set->u.sequence.cb_fn = cb_fn; 226 set->u.sequence.cb_arg = cb_arg; 227 228 channel->dev->flush(channel->dev, channel->dev_channel, 229 &set->cb_args); 230 } 231 232 void 233 spdk_bs_sequence_unmap(spdk_bs_sequence_t *seq, 234 uint64_t lba, uint32_t lba_count, 235 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 236 { 237 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 238 struct spdk_bs_channel *channel = set->channel; 239 240 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Unmapping %u blocks at LBA %lu\n", lba_count, lba); 241 242 set->u.sequence.cb_fn = cb_fn; 243 set->u.sequence.cb_arg = cb_arg; 244 245 channel->dev->unmap(channel->dev, channel->dev_channel, lba, lba_count, 246 &set->cb_args); 247 } 248 249 void 250 spdk_bs_sequence_write_zeroes(spdk_bs_sequence_t *seq, 251 uint64_t lba, uint32_t lba_count, 252 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 253 { 254 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 255 struct spdk_bs_channel *channel = set->channel; 256 257 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "writing zeroes to %u blocks at LBA %lu\n", lba_count, lba); 258 259 set->u.sequence.cb_fn = cb_fn; 260 set->u.sequence.cb_arg = cb_arg; 261 262 channel->dev->write_zeroes(channel->dev, channel->dev_channel, lba, lba_count, 263 &set->cb_args); 264 } 265 266 void 267 spdk_bs_sequence_finish(spdk_bs_sequence_t *seq, int bserrno) 268 { 269 if (bserrno != 0) { 270 seq->bserrno = bserrno; 271 } 272 spdk_bs_request_set_complete((struct spdk_bs_request_set *)seq); 273 } 274 275 static void 276 spdk_bs_batch_completion(struct spdk_io_channel *_channel, 277 void *cb_arg, int bserrno) 278 { 279 struct spdk_bs_request_set *set = cb_arg; 280 281 set->u.batch.outstanding_ops--; 282 if (bserrno != 0) { 283 set->bserrno = bserrno; 284 } 285 286 if (set->u.batch.outstanding_ops == 0 && set->u.batch.batch_closed) { 287 if (set->u.batch.cb_fn) { 288 set->cb_args.cb_fn = spdk_bs_sequence_completion; 289 set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, bserrno); 290 } else { 291 spdk_bs_request_set_complete(set); 292 } 293 } 294 } 295 296 spdk_bs_batch_t * 297 spdk_bs_batch_open(struct spdk_io_channel *_channel, 298 struct spdk_bs_cpl *cpl) 299 { 300 struct spdk_bs_channel *channel; 301 struct spdk_bs_request_set *set; 302 303 channel = spdk_io_channel_get_ctx(_channel); 304 305 set = TAILQ_FIRST(&channel->reqs); 306 if (!set) { 307 return NULL; 308 } 309 TAILQ_REMOVE(&channel->reqs, set, link); 310 311 set->cpl = *cpl; 312 set->bserrno = 0; 313 set->channel = channel; 314 315 set->u.batch.cb_fn = NULL; 316 set->u.batch.cb_arg = NULL; 317 set->u.batch.outstanding_ops = 0; 318 set->u.batch.batch_closed = 0; 319 320 set->cb_args.cb_fn = spdk_bs_batch_completion; 321 set->cb_args.cb_arg = set; 322 set->cb_args.channel = channel->dev_channel; 323 324 return (spdk_bs_batch_t *)set; 325 } 326 327 void 328 spdk_bs_batch_read_bs_dev(spdk_bs_batch_t *batch, struct spdk_bs_dev *bs_dev, 329 void *payload, uint64_t lba, uint32_t lba_count) 330 { 331 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 332 struct spdk_bs_channel *channel = set->channel; 333 334 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba); 335 336 set->u.batch.outstanding_ops++; 337 bs_dev->read(bs_dev, channel->dev_channel, payload, lba, lba_count, &set->cb_args); 338 } 339 340 void 341 spdk_bs_batch_read(spdk_bs_batch_t *batch, void *payload, 342 uint64_t lba, uint32_t lba_count) 343 { 344 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 345 struct spdk_bs_channel *channel = set->channel; 346 347 spdk_bs_batch_read_bs_dev(batch, channel->dev, payload, lba, lba_count); 348 } 349 350 void 351 spdk_bs_batch_write(spdk_bs_batch_t *batch, void *payload, 352 uint64_t lba, uint32_t lba_count) 353 { 354 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 355 struct spdk_bs_channel *channel = set->channel; 356 357 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba); 358 359 set->u.batch.outstanding_ops++; 360 channel->dev->write(channel->dev, channel->dev_channel, payload, lba, lba_count, 361 &set->cb_args); 362 } 363 364 void 365 spdk_bs_batch_flush(spdk_bs_batch_t *batch) 366 { 367 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 368 struct spdk_bs_channel *channel = set->channel; 369 370 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Flushing\n"); 371 372 set->u.batch.outstanding_ops++; 373 channel->dev->flush(channel->dev, channel->dev_channel, 374 &set->cb_args); 375 } 376 377 void 378 spdk_bs_batch_unmap(spdk_bs_batch_t *batch, 379 uint64_t lba, uint32_t lba_count) 380 { 381 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 382 struct spdk_bs_channel *channel = set->channel; 383 384 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Unmapping %u blocks at LBA %lu\n", lba_count, lba); 385 386 set->u.batch.outstanding_ops++; 387 channel->dev->unmap(channel->dev, channel->dev_channel, lba, lba_count, 388 &set->cb_args); 389 } 390 391 void 392 spdk_bs_batch_write_zeroes(spdk_bs_batch_t *batch, 393 uint64_t lba, uint32_t lba_count) 394 { 395 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 396 struct spdk_bs_channel *channel = set->channel; 397 398 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Zeroing %u blocks at LBA %lu\n", lba_count, lba); 399 400 set->u.batch.outstanding_ops++; 401 channel->dev->write_zeroes(channel->dev, channel->dev_channel, lba, lba_count, 402 &set->cb_args); 403 } 404 405 void 406 spdk_bs_batch_close(spdk_bs_batch_t *batch) 407 { 408 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 409 410 set->u.batch.batch_closed = 1; 411 412 if (set->u.batch.outstanding_ops == 0) { 413 if (set->u.batch.cb_fn) { 414 set->cb_args.cb_fn = spdk_bs_sequence_completion; 415 set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, set->bserrno); 416 } else { 417 spdk_bs_request_set_complete(set); 418 } 419 } 420 } 421 422 spdk_bs_batch_t * 423 spdk_bs_sequence_to_batch(spdk_bs_sequence_t *seq, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 424 { 425 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 426 427 set->u.batch.cb_fn = cb_fn; 428 set->u.batch.cb_arg = cb_arg; 429 set->u.batch.outstanding_ops = 0; 430 set->u.batch.batch_closed = 0; 431 432 set->cb_args.cb_fn = spdk_bs_batch_completion; 433 434 return set; 435 } 436 437 SPDK_LOG_REGISTER_COMPONENT("blob_rw", SPDK_LOG_BLOB_RW) 438