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_dev(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_dev(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_dev(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_dev(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_unmap_dev(spdk_bs_sequence_t *seq, 218 uint64_t lba, uint32_t lba_count, 219 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 220 { 221 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 222 struct spdk_bs_channel *channel = set->channel; 223 224 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Unmapping %u blocks at LBA %lu\n", lba_count, lba); 225 226 set->u.sequence.cb_fn = cb_fn; 227 set->u.sequence.cb_arg = cb_arg; 228 229 channel->dev->unmap(channel->dev, channel->dev_channel, lba, lba_count, 230 &set->cb_args); 231 } 232 233 void 234 spdk_bs_sequence_write_zeroes_dev(spdk_bs_sequence_t *seq, 235 uint64_t lba, uint32_t lba_count, 236 spdk_bs_sequence_cpl cb_fn, void *cb_arg) 237 { 238 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 239 struct spdk_bs_channel *channel = set->channel; 240 241 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "writing zeroes to %u blocks at LBA %lu\n", lba_count, lba); 242 243 set->u.sequence.cb_fn = cb_fn; 244 set->u.sequence.cb_arg = cb_arg; 245 246 channel->dev->write_zeroes(channel->dev, channel->dev_channel, lba, lba_count, 247 &set->cb_args); 248 } 249 250 void 251 spdk_bs_sequence_finish(spdk_bs_sequence_t *seq, int bserrno) 252 { 253 if (bserrno != 0) { 254 seq->bserrno = bserrno; 255 } 256 spdk_bs_request_set_complete((struct spdk_bs_request_set *)seq); 257 } 258 259 static void 260 spdk_bs_batch_completion(struct spdk_io_channel *_channel, 261 void *cb_arg, int bserrno) 262 { 263 struct spdk_bs_request_set *set = cb_arg; 264 265 set->u.batch.outstanding_ops--; 266 if (bserrno != 0) { 267 set->bserrno = bserrno; 268 } 269 270 if (set->u.batch.outstanding_ops == 0 && set->u.batch.batch_closed) { 271 if (set->u.batch.cb_fn) { 272 set->cb_args.cb_fn = spdk_bs_sequence_completion; 273 set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, bserrno); 274 } else { 275 spdk_bs_request_set_complete(set); 276 } 277 } 278 } 279 280 spdk_bs_batch_t * 281 spdk_bs_batch_open(struct spdk_io_channel *_channel, 282 struct spdk_bs_cpl *cpl) 283 { 284 struct spdk_bs_channel *channel; 285 struct spdk_bs_request_set *set; 286 287 channel = spdk_io_channel_get_ctx(_channel); 288 289 set = TAILQ_FIRST(&channel->reqs); 290 if (!set) { 291 return NULL; 292 } 293 TAILQ_REMOVE(&channel->reqs, set, link); 294 295 set->cpl = *cpl; 296 set->bserrno = 0; 297 set->channel = channel; 298 299 set->u.batch.cb_fn = NULL; 300 set->u.batch.cb_arg = NULL; 301 set->u.batch.outstanding_ops = 0; 302 set->u.batch.batch_closed = 0; 303 304 set->cb_args.cb_fn = spdk_bs_batch_completion; 305 set->cb_args.cb_arg = set; 306 set->cb_args.channel = channel->dev_channel; 307 308 return (spdk_bs_batch_t *)set; 309 } 310 311 void 312 spdk_bs_batch_read_bs_dev(spdk_bs_batch_t *batch, struct spdk_bs_dev *bs_dev, 313 void *payload, uint64_t lba, uint32_t lba_count) 314 { 315 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 316 struct spdk_bs_channel *channel = set->channel; 317 318 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Reading %u blocks from LBA %lu\n", lba_count, lba); 319 320 set->u.batch.outstanding_ops++; 321 bs_dev->read(bs_dev, channel->dev_channel, payload, lba, lba_count, &set->cb_args); 322 } 323 324 void 325 spdk_bs_batch_read_dev(spdk_bs_batch_t *batch, void *payload, 326 uint64_t lba, uint32_t lba_count) 327 { 328 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 329 struct spdk_bs_channel *channel = set->channel; 330 331 spdk_bs_batch_read_bs_dev(batch, channel->dev, payload, lba, lba_count); 332 } 333 334 void 335 spdk_bs_batch_write_dev(spdk_bs_batch_t *batch, void *payload, 336 uint64_t lba, uint32_t lba_count) 337 { 338 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 339 struct spdk_bs_channel *channel = set->channel; 340 341 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Writing %u blocks to LBA %lu\n", lba_count, lba); 342 343 set->u.batch.outstanding_ops++; 344 channel->dev->write(channel->dev, channel->dev_channel, payload, lba, lba_count, 345 &set->cb_args); 346 } 347 348 void 349 spdk_bs_batch_unmap_dev(spdk_bs_batch_t *batch, 350 uint64_t lba, uint32_t lba_count) 351 { 352 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 353 struct spdk_bs_channel *channel = set->channel; 354 355 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Unmapping %u blocks at LBA %lu\n", lba_count, lba); 356 357 set->u.batch.outstanding_ops++; 358 channel->dev->unmap(channel->dev, channel->dev_channel, lba, lba_count, 359 &set->cb_args); 360 } 361 362 void 363 spdk_bs_batch_write_zeroes_dev(spdk_bs_batch_t *batch, 364 uint64_t lba, uint32_t lba_count) 365 { 366 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 367 struct spdk_bs_channel *channel = set->channel; 368 369 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Zeroing %u blocks at LBA %lu\n", lba_count, lba); 370 371 set->u.batch.outstanding_ops++; 372 channel->dev->write_zeroes(channel->dev, channel->dev_channel, lba, lba_count, 373 &set->cb_args); 374 } 375 376 static void 377 spdk_bs_batch_blob_op_complete(void *arg, int bserrno) 378 { 379 /* TODO: spdk_bs_batch_completion does not actually use the channel parameter - 380 * just pass NULL here instead of getting the channel from the set cb_arg. 381 */ 382 spdk_bs_batch_completion(NULL, arg, bserrno); 383 } 384 385 void 386 spdk_bs_batch_read_blob(spdk_bs_batch_t *batch, struct spdk_blob *blob, 387 void *payload, uint64_t offset, uint64_t length) 388 { 389 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 390 struct spdk_bs_channel *channel = set->channel; 391 392 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Reading %lu pages from offset %lu\n", length, offset); 393 394 set->u.batch.outstanding_ops++; 395 spdk_bs_io_read_blob(blob, spdk_io_channel_from_ctx(channel), payload, offset, 396 length, spdk_bs_batch_blob_op_complete, set); 397 } 398 399 void 400 spdk_bs_batch_write_blob(spdk_bs_batch_t *batch, struct spdk_blob *blob, 401 void *payload, uint64_t offset, uint64_t length) 402 { 403 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 404 struct spdk_bs_channel *channel = set->channel; 405 406 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Writing %lu pages from offset %lu\n", length, offset); 407 408 set->u.batch.outstanding_ops++; 409 spdk_bs_io_write_blob(blob, spdk_io_channel_from_ctx(channel), payload, offset, 410 length, spdk_bs_batch_blob_op_complete, set); 411 } 412 413 void 414 spdk_bs_batch_unmap_blob(spdk_bs_batch_t *batch, struct spdk_blob *blob, 415 uint64_t offset, uint64_t length) 416 { 417 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 418 struct spdk_bs_channel *channel = set->channel; 419 420 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Unmapping %lu pages from offset %lu\n", length, offset); 421 422 set->u.batch.outstanding_ops++; 423 spdk_bs_io_unmap_blob(blob, spdk_io_channel_from_ctx(channel), offset, length, 424 spdk_bs_batch_blob_op_complete, set); 425 } 426 427 void 428 spdk_bs_batch_write_zeroes_blob(spdk_bs_batch_t *batch, struct spdk_blob *blob, 429 uint64_t offset, uint64_t length) 430 { 431 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 432 struct spdk_bs_channel *channel = set->channel; 433 434 SPDK_DEBUGLOG(SPDK_LOG_BLOB_RW, "Zeroing %lu pages from offset %lu\n", length, offset); 435 436 set->u.batch.outstanding_ops++; 437 spdk_bs_io_write_zeroes_blob(blob, spdk_io_channel_from_ctx(channel), offset, length, 438 spdk_bs_batch_blob_op_complete, set); 439 } 440 441 void 442 spdk_bs_batch_close(spdk_bs_batch_t *batch) 443 { 444 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)batch; 445 446 set->u.batch.batch_closed = 1; 447 448 if (set->u.batch.outstanding_ops == 0) { 449 if (set->u.batch.cb_fn) { 450 set->cb_args.cb_fn = spdk_bs_sequence_completion; 451 set->u.batch.cb_fn((spdk_bs_sequence_t *)set, set->u.batch.cb_arg, set->bserrno); 452 } else { 453 spdk_bs_request_set_complete(set); 454 } 455 } 456 } 457 458 spdk_bs_batch_t * 459 spdk_bs_sequence_to_batch(spdk_bs_sequence_t *seq, spdk_bs_sequence_cpl cb_fn, void *cb_arg) 460 { 461 struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)seq; 462 463 set->u.batch.cb_fn = cb_fn; 464 set->u.batch.cb_arg = cb_arg; 465 set->u.batch.outstanding_ops = 0; 466 set->u.batch.batch_closed = 0; 467 468 set->cb_args.cb_fn = spdk_bs_batch_completion; 469 470 return set; 471 } 472 473 spdk_bs_user_op_t * 474 spdk_bs_user_op_alloc(struct spdk_io_channel *_channel, struct spdk_bs_cpl *cpl, 475 enum spdk_blob_op_type op_type, struct spdk_blob *blob, 476 void *payload, int iovcnt, uint64_t offset, uint64_t length) 477 { 478 struct spdk_bs_channel *channel; 479 struct spdk_bs_request_set *set; 480 struct spdk_bs_user_op_args *args; 481 482 channel = spdk_io_channel_get_ctx(_channel); 483 484 set = TAILQ_FIRST(&channel->reqs); 485 if (!set) { 486 return NULL; 487 } 488 TAILQ_REMOVE(&channel->reqs, set, link); 489 490 set->cpl = *cpl; 491 set->channel = channel; 492 493 args = &set->u.user_op; 494 495 args->type = op_type; 496 args->iovcnt = iovcnt; 497 args->blob = blob; 498 args->offset = offset; 499 args->length = length; 500 args->payload = payload; 501 502 return (spdk_bs_user_op_t *)set; 503 } 504 505 void 506 spdk_bs_user_op_execute(spdk_bs_user_op_t *op) 507 { 508 struct spdk_bs_request_set *set; 509 struct spdk_bs_user_op_args *args; 510 struct spdk_io_channel *ch; 511 512 set = (struct spdk_bs_request_set *)op; 513 args = &set->u.user_op; 514 ch = spdk_io_channel_from_ctx(set->channel); 515 516 switch (args->type) { 517 case SPDK_BLOB_READ: 518 spdk_bs_io_read_blob(args->blob, ch, args->payload, args->offset, args->length, 519 set->cpl.u.blob_basic.cb_fn, set->cpl.u.blob_basic.cb_arg); 520 break; 521 case SPDK_BLOB_WRITE: 522 spdk_bs_io_write_blob(args->blob, ch, args->payload, args->offset, args->length, 523 set->cpl.u.blob_basic.cb_fn, set->cpl.u.blob_basic.cb_arg); 524 break; 525 case SPDK_BLOB_UNMAP: 526 spdk_bs_io_unmap_blob(args->blob, ch, args->offset, args->length, 527 set->cpl.u.blob_basic.cb_fn, set->cpl.u.blob_basic.cb_arg); 528 break; 529 case SPDK_BLOB_WRITE_ZEROES: 530 spdk_bs_io_write_zeroes_blob(args->blob, ch, args->offset, args->length, 531 set->cpl.u.blob_basic.cb_fn, set->cpl.u.blob_basic.cb_arg); 532 break; 533 case SPDK_BLOB_READV: 534 spdk_bs_io_readv_blob(args->blob, ch, args->payload, args->iovcnt, 535 args->offset, args->length, 536 set->cpl.u.blob_basic.cb_fn, set->cpl.u.blob_basic.cb_arg); 537 break; 538 case SPDK_BLOB_WRITEV: 539 spdk_bs_io_writev_blob(args->blob, ch, args->payload, args->iovcnt, 540 args->offset, args->length, 541 set->cpl.u.blob_basic.cb_fn, set->cpl.u.blob_basic.cb_arg); 542 break; 543 } 544 TAILQ_INSERT_TAIL(&set->channel->reqs, set, link); 545 } 546 547 void 548 spdk_bs_user_op_abort(spdk_bs_user_op_t *op) 549 { 550 struct spdk_bs_request_set *set; 551 552 set = (struct spdk_bs_request_set *)op; 553 554 set->cpl.u.blob_basic.cb_fn(set->cpl.u.blob_basic.cb_arg, -EIO); 555 TAILQ_INSERT_TAIL(&set->channel->reqs, set, link); 556 } 557 558 SPDK_LOG_REGISTER_COMPONENT("blob_rw", SPDK_LOG_BLOB_RW) 559