ctl_backend_block.c revision 268678
1/*- 2 * Copyright (c) 2003 Silicon Graphics International Corp. 3 * Copyright (c) 2009-2011 Spectra Logic Corporation 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 36 */ 37/* 38 * CAM Target Layer driver backend for block devices. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_backend_block.c 268678 2014-07-15 16:58:38Z mav $"); 44 45#include <opt_kdtrace.h> 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/types.h> 51#include <sys/kthread.h> 52#include <sys/bio.h> 53#include <sys/fcntl.h> 54#include <sys/limits.h> 55#include <sys/lock.h> 56#include <sys/mutex.h> 57#include <sys/condvar.h> 58#include <sys/malloc.h> 59#include <sys/conf.h> 60#include <sys/ioccom.h> 61#include <sys/queue.h> 62#include <sys/sbuf.h> 63#include <sys/endian.h> 64#include <sys/uio.h> 65#include <sys/buf.h> 66#include <sys/taskqueue.h> 67#include <sys/vnode.h> 68#include <sys/namei.h> 69#include <sys/mount.h> 70#include <sys/disk.h> 71#include <sys/fcntl.h> 72#include <sys/filedesc.h> 73#include <sys/proc.h> 74#include <sys/pcpu.h> 75#include <sys/module.h> 76#include <sys/sdt.h> 77#include <sys/devicestat.h> 78#include <sys/sysctl.h> 79 80#include <geom/geom.h> 81 82#include <cam/cam.h> 83#include <cam/scsi/scsi_all.h> 84#include <cam/scsi/scsi_da.h> 85#include <cam/ctl/ctl_io.h> 86#include <cam/ctl/ctl.h> 87#include <cam/ctl/ctl_backend.h> 88#include <cam/ctl/ctl_frontend_internal.h> 89#include <cam/ctl/ctl_ioctl.h> 90#include <cam/ctl/ctl_scsi_all.h> 91#include <cam/ctl/ctl_error.h> 92 93/* 94 * The idea here is that we'll allocate enough S/G space to hold a 1MB 95 * I/O. If we get an I/O larger than that, we'll split it. 96 */ 97#define CTLBLK_HALF_IO_SIZE (512 * 1024) 98#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 99#define CTLBLK_MAX_SEG MAXPHYS 100#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 101#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 102 103#ifdef CTLBLK_DEBUG 104#define DPRINTF(fmt, args...) \ 105 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 106#else 107#define DPRINTF(fmt, args...) do {} while(0) 108#endif 109 110#define PRIV(io) \ 111 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 112#define ARGS(io) \ 113 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 114 115SDT_PROVIDER_DEFINE(cbb); 116 117typedef enum { 118 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 119 CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 120 CTL_BE_BLOCK_LUN_WAITING = 0x04, 121 CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 122} ctl_be_block_lun_flags; 123 124typedef enum { 125 CTL_BE_BLOCK_NONE, 126 CTL_BE_BLOCK_DEV, 127 CTL_BE_BLOCK_FILE 128} ctl_be_block_type; 129 130struct ctl_be_block_devdata { 131 struct cdev *cdev; 132 struct cdevsw *csw; 133 int dev_ref; 134}; 135 136struct ctl_be_block_filedata { 137 struct ucred *cred; 138}; 139 140union ctl_be_block_bedata { 141 struct ctl_be_block_devdata dev; 142 struct ctl_be_block_filedata file; 143}; 144 145struct ctl_be_block_io; 146struct ctl_be_block_lun; 147 148typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 149 struct ctl_be_block_io *beio); 150 151/* 152 * Backend LUN structure. There is a 1:1 mapping between a block device 153 * and a backend block LUN, and between a backend block LUN and a CTL LUN. 154 */ 155struct ctl_be_block_lun { 156 struct ctl_block_disk *disk; 157 char lunname[32]; 158 char *dev_path; 159 ctl_be_block_type dev_type; 160 struct vnode *vn; 161 union ctl_be_block_bedata backend; 162 cbb_dispatch_t dispatch; 163 cbb_dispatch_t lun_flush; 164 cbb_dispatch_t unmap; 165 uma_zone_t lun_zone; 166 uint64_t size_blocks; 167 uint64_t size_bytes; 168 uint32_t blocksize; 169 int blocksize_shift; 170 uint16_t pblockexp; 171 uint16_t pblockoff; 172 struct ctl_be_block_softc *softc; 173 struct devstat *disk_stats; 174 ctl_be_block_lun_flags flags; 175 STAILQ_ENTRY(ctl_be_block_lun) links; 176 struct ctl_be_lun ctl_be_lun; 177 struct taskqueue *io_taskqueue; 178 struct task io_task; 179 int num_threads; 180 STAILQ_HEAD(, ctl_io_hdr) input_queue; 181 STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 182 STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 183 struct mtx_padalign io_lock; 184 struct mtx_padalign queue_lock; 185}; 186 187/* 188 * Overall softc structure for the block backend module. 189 */ 190struct ctl_be_block_softc { 191 struct mtx lock; 192 int num_disks; 193 STAILQ_HEAD(, ctl_block_disk) disk_list; 194 int num_luns; 195 STAILQ_HEAD(, ctl_be_block_lun) lun_list; 196}; 197 198static struct ctl_be_block_softc backend_block_softc; 199 200/* 201 * Per-I/O information. 202 */ 203struct ctl_be_block_io { 204 union ctl_io *io; 205 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 206 struct iovec xiovecs[CTLBLK_MAX_SEGS]; 207 int bio_cmd; 208 int bio_flags; 209 int num_segs; 210 int num_bios_sent; 211 int num_bios_done; 212 int send_complete; 213 int num_errors; 214 struct bintime ds_t0; 215 devstat_tag_type ds_tag_type; 216 devstat_trans_flags ds_trans_type; 217 uint64_t io_len; 218 uint64_t io_offset; 219 struct ctl_be_block_softc *softc; 220 struct ctl_be_block_lun *lun; 221 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 222}; 223 224static int cbb_num_threads = 14; 225TUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads); 226SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 227 "CAM Target Layer Block Backend"); 228SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW, 229 &cbb_num_threads, 0, "Number of threads per backing file"); 230 231static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 232static void ctl_free_beio(struct ctl_be_block_io *beio); 233static void ctl_complete_beio(struct ctl_be_block_io *beio); 234static int ctl_be_block_move_done(union ctl_io *io); 235static void ctl_be_block_biodone(struct bio *bio); 236static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 237 struct ctl_be_block_io *beio); 238static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 239 struct ctl_be_block_io *beio); 240static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 241 struct ctl_be_block_io *beio); 242static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 243 struct ctl_be_block_io *beio); 244static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 245 struct ctl_be_block_io *beio); 246static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 247 union ctl_io *io); 248static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 249 union ctl_io *io); 250static void ctl_be_block_worker(void *context, int pending); 251static int ctl_be_block_submit(union ctl_io *io); 252static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 253 int flag, struct thread *td); 254static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 255 struct ctl_lun_req *req); 256static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 257 struct ctl_lun_req *req); 258static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 259static int ctl_be_block_open(struct ctl_be_block_softc *softc, 260 struct ctl_be_block_lun *be_lun, 261 struct ctl_lun_req *req); 262static int ctl_be_block_create(struct ctl_be_block_softc *softc, 263 struct ctl_lun_req *req); 264static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 265 struct ctl_lun_req *req); 266static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 267 struct ctl_lun_req *req); 268static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 269 struct ctl_lun_req *req); 270static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 271 struct ctl_lun_req *req); 272static void ctl_be_block_lun_shutdown(void *be_lun); 273static void ctl_be_block_lun_config_status(void *be_lun, 274 ctl_lun_config_status status); 275static int ctl_be_block_config_write(union ctl_io *io); 276static int ctl_be_block_config_read(union ctl_io *io); 277static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 278int ctl_be_block_init(void); 279 280static struct ctl_backend_driver ctl_be_block_driver = 281{ 282 .name = "block", 283 .flags = CTL_BE_FLAG_HAS_CONFIG, 284 .init = ctl_be_block_init, 285 .data_submit = ctl_be_block_submit, 286 .data_move_done = ctl_be_block_move_done, 287 .config_read = ctl_be_block_config_read, 288 .config_write = ctl_be_block_config_write, 289 .ioctl = ctl_be_block_ioctl, 290 .lun_info = ctl_be_block_lun_info 291}; 292 293MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 294CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 295 296static uma_zone_t beio_zone; 297 298static struct ctl_be_block_io * 299ctl_alloc_beio(struct ctl_be_block_softc *softc) 300{ 301 struct ctl_be_block_io *beio; 302 303 beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 304 beio->softc = softc; 305 return (beio); 306} 307 308static void 309ctl_free_beio(struct ctl_be_block_io *beio) 310{ 311 int duplicate_free; 312 int i; 313 314 duplicate_free = 0; 315 316 for (i = 0; i < beio->num_segs; i++) { 317 if (beio->sg_segs[i].addr == NULL) 318 duplicate_free++; 319 320 uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 321 beio->sg_segs[i].addr = NULL; 322 323 /* For compare we had two equal S/G lists. */ 324 if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 325 uma_zfree(beio->lun->lun_zone, 326 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 327 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 328 } 329 } 330 331 if (duplicate_free > 0) { 332 printf("%s: %d duplicate frees out of %d segments\n", __func__, 333 duplicate_free, beio->num_segs); 334 } 335 336 uma_zfree(beio_zone, beio); 337} 338 339static void 340ctl_complete_beio(struct ctl_be_block_io *beio) 341{ 342 union ctl_io *io = beio->io; 343 344 if (beio->beio_cont != NULL) { 345 beio->beio_cont(beio); 346 } else { 347 ctl_free_beio(beio); 348 ctl_data_submit_done(io); 349 } 350} 351 352static int 353ctl_be_block_move_done(union ctl_io *io) 354{ 355 struct ctl_be_block_io *beio; 356 struct ctl_be_block_lun *be_lun; 357 struct ctl_lba_len_flags *lbalen; 358#ifdef CTL_TIME_IO 359 struct bintime cur_bt; 360#endif 361 int i; 362 363 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 364 be_lun = beio->lun; 365 366 DPRINTF("entered\n"); 367 368#ifdef CTL_TIME_IO 369 getbintime(&cur_bt); 370 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 371 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 372 io->io_hdr.num_dmas++; 373#endif 374 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 375 376 /* 377 * We set status at this point for read commands, and write 378 * commands with errors. 379 */ 380 if ((io->io_hdr.port_status == 0) && 381 ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) && 382 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 383 lbalen = ARGS(beio->io); 384 if (lbalen->flags & CTL_LLF_READ) { 385 ctl_set_success(&io->scsiio); 386 } else if (lbalen->flags & CTL_LLF_COMPARE) { 387 /* We have two data blocks ready for comparison. */ 388 for (i = 0; i < beio->num_segs; i++) { 389 if (memcmp(beio->sg_segs[i].addr, 390 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 391 beio->sg_segs[i].len) != 0) 392 break; 393 } 394 if (i < beio->num_segs) 395 ctl_set_sense(&io->scsiio, 396 /*current_error*/ 1, 397 /*sense_key*/ SSD_KEY_MISCOMPARE, 398 /*asc*/ 0x1D, 399 /*ascq*/ 0x00, 400 SSD_ELEM_NONE); 401 else 402 ctl_set_success(&io->scsiio); 403 } 404 } 405 else if ((io->io_hdr.port_status != 0) 406 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 407 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 408 /* 409 * For hardware error sense keys, the sense key 410 * specific value is defined to be a retry count, 411 * but we use it to pass back an internal FETD 412 * error code. XXX KDM Hopefully the FETD is only 413 * using 16 bits for an error code, since that's 414 * all the space we have in the sks field. 415 */ 416 ctl_set_internal_failure(&io->scsiio, 417 /*sks_valid*/ 1, 418 /*retry_count*/ 419 io->io_hdr.port_status); 420 } 421 422 /* 423 * If this is a read, or a write with errors, it is done. 424 */ 425 if ((beio->bio_cmd == BIO_READ) 426 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 427 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 428 ctl_complete_beio(beio); 429 return (0); 430 } 431 432 /* 433 * At this point, we have a write and the DMA completed 434 * successfully. We now have to queue it to the task queue to 435 * execute the backend I/O. That is because we do blocking 436 * memory allocations, and in the file backing case, blocking I/O. 437 * This move done routine is generally called in the SIM's 438 * interrupt context, and therefore we cannot block. 439 */ 440 mtx_lock(&be_lun->queue_lock); 441 /* 442 * XXX KDM make sure that links is okay to use at this point. 443 * Otherwise, we either need to add another field to ctl_io_hdr, 444 * or deal with resource allocation here. 445 */ 446 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 447 mtx_unlock(&be_lun->queue_lock); 448 449 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 450 451 return (0); 452} 453 454static void 455ctl_be_block_biodone(struct bio *bio) 456{ 457 struct ctl_be_block_io *beio; 458 struct ctl_be_block_lun *be_lun; 459 union ctl_io *io; 460 int error; 461 462 beio = bio->bio_caller1; 463 be_lun = beio->lun; 464 io = beio->io; 465 466 DPRINTF("entered\n"); 467 468 error = bio->bio_error; 469 mtx_lock(&be_lun->io_lock); 470 if (error != 0) 471 beio->num_errors++; 472 473 beio->num_bios_done++; 474 475 /* 476 * XXX KDM will this cause WITNESS to complain? Holding a lock 477 * during the free might cause it to complain. 478 */ 479 g_destroy_bio(bio); 480 481 /* 482 * If the send complete bit isn't set, or we aren't the last I/O to 483 * complete, then we're done. 484 */ 485 if ((beio->send_complete == 0) 486 || (beio->num_bios_done < beio->num_bios_sent)) { 487 mtx_unlock(&be_lun->io_lock); 488 return; 489 } 490 491 /* 492 * At this point, we've verified that we are the last I/O to 493 * complete, so it's safe to drop the lock. 494 */ 495 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 496 beio->ds_tag_type, beio->ds_trans_type, 497 /*now*/ NULL, /*then*/&beio->ds_t0); 498 mtx_unlock(&be_lun->io_lock); 499 500 /* 501 * If there are any errors from the backing device, we fail the 502 * entire I/O with a medium error. 503 */ 504 if (beio->num_errors > 0) { 505 if (error == EOPNOTSUPP) { 506 ctl_set_invalid_opcode(&io->scsiio); 507 } else if (beio->bio_cmd == BIO_FLUSH) { 508 /* XXX KDM is there is a better error here? */ 509 ctl_set_internal_failure(&io->scsiio, 510 /*sks_valid*/ 1, 511 /*retry_count*/ 0xbad2); 512 } else 513 ctl_set_medium_error(&io->scsiio); 514 ctl_complete_beio(beio); 515 return; 516 } 517 518 /* 519 * If this is a write, a flush, a delete or verify, we're all done. 520 * If this is a read, we can now send the data to the user. 521 */ 522 if ((beio->bio_cmd == BIO_WRITE) 523 || (beio->bio_cmd == BIO_FLUSH) 524 || (beio->bio_cmd == BIO_DELETE) 525 || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 526 ctl_set_success(&io->scsiio); 527 ctl_complete_beio(beio); 528 } else { 529#ifdef CTL_TIME_IO 530 getbintime(&io->io_hdr.dma_start_bt); 531#endif 532 ctl_datamove(io); 533 } 534} 535 536static void 537ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 538 struct ctl_be_block_io *beio) 539{ 540 union ctl_io *io = beio->io; 541 struct mount *mountpoint; 542 int error, lock_flags; 543 544 DPRINTF("entered\n"); 545 546 binuptime(&beio->ds_t0); 547 mtx_lock(&be_lun->io_lock); 548 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 549 mtx_unlock(&be_lun->io_lock); 550 551 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 552 553 if (MNT_SHARED_WRITES(mountpoint) 554 || ((mountpoint == NULL) 555 && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 556 lock_flags = LK_SHARED; 557 else 558 lock_flags = LK_EXCLUSIVE; 559 560 vn_lock(be_lun->vn, lock_flags | LK_RETRY); 561 562 error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 563 VOP_UNLOCK(be_lun->vn, 0); 564 565 vn_finished_write(mountpoint); 566 567 mtx_lock(&be_lun->io_lock); 568 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 569 beio->ds_tag_type, beio->ds_trans_type, 570 /*now*/ NULL, /*then*/&beio->ds_t0); 571 mtx_unlock(&be_lun->io_lock); 572 573 if (error == 0) 574 ctl_set_success(&io->scsiio); 575 else { 576 /* XXX KDM is there is a better error here? */ 577 ctl_set_internal_failure(&io->scsiio, 578 /*sks_valid*/ 1, 579 /*retry_count*/ 0xbad1); 580 } 581 582 ctl_complete_beio(beio); 583} 584 585SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 586SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 587SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 588SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 589 590static void 591ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 592 struct ctl_be_block_io *beio) 593{ 594 struct ctl_be_block_filedata *file_data; 595 union ctl_io *io; 596 struct uio xuio; 597 struct iovec *xiovec; 598 int flags; 599 int error, i; 600 601 DPRINTF("entered\n"); 602 603 file_data = &be_lun->backend.file; 604 io = beio->io; 605 flags = beio->bio_flags; 606 607 bzero(&xuio, sizeof(xuio)); 608 if (beio->bio_cmd == BIO_READ) { 609 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 610 xuio.uio_rw = UIO_READ; 611 } else { 612 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 613 xuio.uio_rw = UIO_WRITE; 614 } 615 xuio.uio_offset = beio->io_offset; 616 xuio.uio_resid = beio->io_len; 617 xuio.uio_segflg = UIO_SYSSPACE; 618 xuio.uio_iov = beio->xiovecs; 619 xuio.uio_iovcnt = beio->num_segs; 620 xuio.uio_td = curthread; 621 622 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 623 xiovec->iov_base = beio->sg_segs[i].addr; 624 xiovec->iov_len = beio->sg_segs[i].len; 625 } 626 627 binuptime(&beio->ds_t0); 628 mtx_lock(&be_lun->io_lock); 629 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 630 mtx_unlock(&be_lun->io_lock); 631 632 if (beio->bio_cmd == BIO_READ) { 633 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 634 635 /* 636 * UFS pays attention to IO_DIRECT for reads. If the 637 * DIRECTIO option is configured into the kernel, it calls 638 * ffs_rawread(). But that only works for single-segment 639 * uios with user space addresses. In our case, with a 640 * kernel uio, it still reads into the buffer cache, but it 641 * will just try to release the buffer from the cache later 642 * on in ffs_read(). 643 * 644 * ZFS does not pay attention to IO_DIRECT for reads. 645 * 646 * UFS does not pay attention to IO_SYNC for reads. 647 * 648 * ZFS pays attention to IO_SYNC (which translates into the 649 * Solaris define FRSYNC for zfs_read()) for reads. It 650 * attempts to sync the file before reading. 651 * 652 * So, to attempt to provide some barrier semantics in the 653 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 654 */ 655 error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 656 (IO_DIRECT|IO_SYNC) : 0, file_data->cred); 657 658 VOP_UNLOCK(be_lun->vn, 0); 659 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 660 } else { 661 struct mount *mountpoint; 662 int lock_flags; 663 664 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 665 666 if (MNT_SHARED_WRITES(mountpoint) 667 || ((mountpoint == NULL) 668 && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 669 lock_flags = LK_SHARED; 670 else 671 lock_flags = LK_EXCLUSIVE; 672 673 vn_lock(be_lun->vn, lock_flags | LK_RETRY); 674 675 /* 676 * UFS pays attention to IO_DIRECT for writes. The write 677 * is done asynchronously. (Normally the write would just 678 * get put into cache. 679 * 680 * UFS pays attention to IO_SYNC for writes. It will 681 * attempt to write the buffer out synchronously if that 682 * flag is set. 683 * 684 * ZFS does not pay attention to IO_DIRECT for writes. 685 * 686 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 687 * for writes. It will flush the transaction from the 688 * cache before returning. 689 * 690 * So if we've got the BIO_ORDERED flag set, we want 691 * IO_SYNC in either the UFS or ZFS case. 692 */ 693 error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 694 IO_SYNC : 0, file_data->cred); 695 VOP_UNLOCK(be_lun->vn, 0); 696 697 vn_finished_write(mountpoint); 698 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 699 } 700 701 mtx_lock(&be_lun->io_lock); 702 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 703 beio->ds_tag_type, beio->ds_trans_type, 704 /*now*/ NULL, /*then*/&beio->ds_t0); 705 mtx_unlock(&be_lun->io_lock); 706 707 /* 708 * If we got an error, set the sense data to "MEDIUM ERROR" and 709 * return the I/O to the user. 710 */ 711 if (error != 0) { 712 char path_str[32]; 713 714 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 715 /* 716 * XXX KDM ZFS returns ENOSPC when the underlying 717 * filesystem fills up. What kind of SCSI error should we 718 * return for that? 719 */ 720 printf("%s%s command returned errno %d\n", path_str, 721 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 722 ctl_set_medium_error(&io->scsiio); 723 ctl_complete_beio(beio); 724 return; 725 } 726 727 /* 728 * If this is a write, we're all done. 729 * If this is a read, we can now send the data to the user. 730 */ 731 if (ARGS(io)->flags & (CTL_LLF_WRITE | CTL_LLF_VERIFY)) { 732 ctl_set_success(&io->scsiio); 733 ctl_complete_beio(beio); 734 } else { 735#ifdef CTL_TIME_IO 736 getbintime(&io->io_hdr.dma_start_bt); 737#endif 738 ctl_datamove(io); 739 } 740} 741 742static void 743ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 744 struct ctl_be_block_io *beio) 745{ 746 struct bio *bio; 747 union ctl_io *io; 748 struct ctl_be_block_devdata *dev_data; 749 750 dev_data = &be_lun->backend.dev; 751 io = beio->io; 752 753 DPRINTF("entered\n"); 754 755 /* This can't fail, it's a blocking allocation. */ 756 bio = g_alloc_bio(); 757 758 bio->bio_cmd = BIO_FLUSH; 759 bio->bio_flags |= BIO_ORDERED; 760 bio->bio_dev = dev_data->cdev; 761 bio->bio_offset = 0; 762 bio->bio_data = 0; 763 bio->bio_done = ctl_be_block_biodone; 764 bio->bio_caller1 = beio; 765 bio->bio_pblkno = 0; 766 767 /* 768 * We don't need to acquire the LUN lock here, because we are only 769 * sending one bio, and so there is no other context to synchronize 770 * with. 771 */ 772 beio->num_bios_sent = 1; 773 beio->send_complete = 1; 774 775 binuptime(&beio->ds_t0); 776 mtx_lock(&be_lun->io_lock); 777 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 778 mtx_unlock(&be_lun->io_lock); 779 780 (*dev_data->csw->d_strategy)(bio); 781} 782 783static void 784ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 785 struct ctl_be_block_io *beio, 786 uint64_t off, uint64_t len, int last) 787{ 788 struct bio *bio; 789 struct ctl_be_block_devdata *dev_data; 790 uint64_t maxlen; 791 792 dev_data = &be_lun->backend.dev; 793 maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 794 while (len > 0) { 795 bio = g_alloc_bio(); 796 bio->bio_cmd = BIO_DELETE; 797 bio->bio_flags |= beio->bio_flags; 798 bio->bio_dev = dev_data->cdev; 799 bio->bio_offset = off; 800 bio->bio_length = MIN(len, maxlen); 801 bio->bio_data = 0; 802 bio->bio_done = ctl_be_block_biodone; 803 bio->bio_caller1 = beio; 804 bio->bio_pblkno = off / be_lun->blocksize; 805 806 off += bio->bio_length; 807 len -= bio->bio_length; 808 809 mtx_lock(&be_lun->io_lock); 810 beio->num_bios_sent++; 811 if (last && len == 0) 812 beio->send_complete = 1; 813 mtx_unlock(&be_lun->io_lock); 814 815 (*dev_data->csw->d_strategy)(bio); 816 } 817} 818 819static void 820ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 821 struct ctl_be_block_io *beio) 822{ 823 union ctl_io *io; 824 struct ctl_be_block_devdata *dev_data; 825 struct ctl_ptr_len_flags *ptrlen; 826 struct scsi_unmap_desc *buf, *end; 827 uint64_t len; 828 829 dev_data = &be_lun->backend.dev; 830 io = beio->io; 831 832 DPRINTF("entered\n"); 833 834 binuptime(&beio->ds_t0); 835 mtx_lock(&be_lun->io_lock); 836 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 837 mtx_unlock(&be_lun->io_lock); 838 839 if (beio->io_offset == -1) { 840 beio->io_len = 0; 841 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 842 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 843 end = buf + ptrlen->len / sizeof(*buf); 844 for (; buf < end; buf++) { 845 len = (uint64_t)scsi_4btoul(buf->length) * 846 be_lun->blocksize; 847 beio->io_len += len; 848 ctl_be_block_unmap_dev_range(be_lun, beio, 849 scsi_8btou64(buf->lba) * be_lun->blocksize, len, 850 (end - buf < 2) ? TRUE : FALSE); 851 } 852 } else 853 ctl_be_block_unmap_dev_range(be_lun, beio, 854 beio->io_offset, beio->io_len, TRUE); 855} 856 857static void 858ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 859 struct ctl_be_block_io *beio) 860{ 861 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 862 int i; 863 struct bio *bio; 864 struct ctl_be_block_devdata *dev_data; 865 off_t cur_offset; 866 int max_iosize; 867 868 DPRINTF("entered\n"); 869 870 dev_data = &be_lun->backend.dev; 871 872 /* 873 * We have to limit our I/O size to the maximum supported by the 874 * backend device. Hopefully it is MAXPHYS. If the driver doesn't 875 * set it properly, use DFLTPHYS. 876 */ 877 max_iosize = dev_data->cdev->si_iosize_max; 878 if (max_iosize < PAGE_SIZE) 879 max_iosize = DFLTPHYS; 880 881 cur_offset = beio->io_offset; 882 for (i = 0; i < beio->num_segs; i++) { 883 size_t cur_size; 884 uint8_t *cur_ptr; 885 886 cur_size = beio->sg_segs[i].len; 887 cur_ptr = beio->sg_segs[i].addr; 888 889 while (cur_size > 0) { 890 /* This can't fail, it's a blocking allocation. */ 891 bio = g_alloc_bio(); 892 893 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 894 895 bio->bio_cmd = beio->bio_cmd; 896 bio->bio_flags |= beio->bio_flags; 897 bio->bio_dev = dev_data->cdev; 898 bio->bio_caller1 = beio; 899 bio->bio_length = min(cur_size, max_iosize); 900 bio->bio_offset = cur_offset; 901 bio->bio_data = cur_ptr; 902 bio->bio_done = ctl_be_block_biodone; 903 bio->bio_pblkno = cur_offset / be_lun->blocksize; 904 905 cur_offset += bio->bio_length; 906 cur_ptr += bio->bio_length; 907 cur_size -= bio->bio_length; 908 909 TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 910 beio->num_bios_sent++; 911 } 912 } 913 binuptime(&beio->ds_t0); 914 mtx_lock(&be_lun->io_lock); 915 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 916 beio->send_complete = 1; 917 mtx_unlock(&be_lun->io_lock); 918 919 /* 920 * Fire off all allocated requests! 921 */ 922 while ((bio = TAILQ_FIRST(&queue)) != NULL) { 923 TAILQ_REMOVE(&queue, bio, bio_queue); 924 (*dev_data->csw->d_strategy)(bio); 925 } 926} 927 928static void 929ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 930{ 931 union ctl_io *io; 932 933 io = beio->io; 934 ctl_free_beio(beio); 935 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 936 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 937 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 938 ctl_config_write_done(io); 939 return; 940 } 941 942 ctl_be_block_config_write(io); 943} 944 945static void 946ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 947 union ctl_io *io) 948{ 949 struct ctl_be_block_io *beio; 950 struct ctl_be_block_softc *softc; 951 struct ctl_lba_len_flags *lbalen; 952 uint64_t len_left, lba; 953 int i, seglen; 954 uint8_t *buf, *end; 955 956 DPRINTF("entered\n"); 957 958 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 959 softc = be_lun->softc; 960 lbalen = ARGS(beio->io); 961 962 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP) || 963 (lbalen->flags & SWS_UNMAP && be_lun->unmap == NULL)) { 964 ctl_free_beio(beio); 965 ctl_set_invalid_field(&io->scsiio, 966 /*sks_valid*/ 1, 967 /*command*/ 1, 968 /*field*/ 1, 969 /*bit_valid*/ 0, 970 /*bit*/ 0); 971 ctl_config_write_done(io); 972 return; 973 } 974 975 /* 976 * If the I/O came down with an ordered or head of queue tag, set 977 * the BIO_ORDERED attribute. For head of queue tags, that's 978 * pretty much the best we can do. 979 */ 980 if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 981 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 982 beio->bio_flags = BIO_ORDERED; 983 984 switch (io->scsiio.tag_type) { 985 case CTL_TAG_ORDERED: 986 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 987 break; 988 case CTL_TAG_HEAD_OF_QUEUE: 989 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 990 break; 991 case CTL_TAG_UNTAGGED: 992 case CTL_TAG_SIMPLE: 993 case CTL_TAG_ACA: 994 default: 995 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 996 break; 997 } 998 999 if (lbalen->flags & SWS_UNMAP) { 1000 beio->io_offset = lbalen->lba * be_lun->blocksize; 1001 beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1002 beio->bio_cmd = BIO_DELETE; 1003 beio->ds_trans_type = DEVSTAT_FREE; 1004 1005 be_lun->unmap(be_lun, beio); 1006 return; 1007 } 1008 1009 beio->bio_cmd = BIO_WRITE; 1010 beio->ds_trans_type = DEVSTAT_WRITE; 1011 1012 DPRINTF("WRITE SAME at LBA %jx len %u\n", 1013 (uintmax_t)lbalen->lba, lbalen->len); 1014 1015 len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1016 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1017 1018 /* 1019 * Setup the S/G entry for this chunk. 1020 */ 1021 seglen = MIN(CTLBLK_MAX_SEG, len_left); 1022 seglen -= seglen % be_lun->blocksize; 1023 beio->sg_segs[i].len = seglen; 1024 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1025 1026 DPRINTF("segment %d addr %p len %zd\n", i, 1027 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1028 1029 beio->num_segs++; 1030 len_left -= seglen; 1031 1032 buf = beio->sg_segs[i].addr; 1033 end = buf + seglen; 1034 for (; buf < end; buf += be_lun->blocksize) { 1035 memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1036 if (lbalen->flags & SWS_LBDATA) 1037 scsi_ulto4b(lbalen->lba + lba, buf); 1038 lba++; 1039 } 1040 } 1041 1042 beio->io_offset = lbalen->lba * be_lun->blocksize; 1043 beio->io_len = lba * be_lun->blocksize; 1044 1045 /* We can not do all in one run. Correct and schedule rerun. */ 1046 if (len_left > 0) { 1047 lbalen->lba += lba; 1048 lbalen->len -= lba; 1049 beio->beio_cont = ctl_be_block_cw_done_ws; 1050 } 1051 1052 be_lun->dispatch(be_lun, beio); 1053} 1054 1055static void 1056ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1057 union ctl_io *io) 1058{ 1059 struct ctl_be_block_io *beio; 1060 struct ctl_be_block_softc *softc; 1061 struct ctl_ptr_len_flags *ptrlen; 1062 1063 DPRINTF("entered\n"); 1064 1065 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1066 softc = be_lun->softc; 1067 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1068 1069 if (ptrlen->flags != 0 || be_lun->unmap == NULL) { 1070 ctl_free_beio(beio); 1071 ctl_set_invalid_field(&io->scsiio, 1072 /*sks_valid*/ 0, 1073 /*command*/ 1, 1074 /*field*/ 0, 1075 /*bit_valid*/ 0, 1076 /*bit*/ 0); 1077 ctl_config_write_done(io); 1078 return; 1079 } 1080 1081 /* 1082 * If the I/O came down with an ordered or head of queue tag, set 1083 * the BIO_ORDERED attribute. For head of queue tags, that's 1084 * pretty much the best we can do. 1085 */ 1086 if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1087 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1088 beio->bio_flags = BIO_ORDERED; 1089 1090 switch (io->scsiio.tag_type) { 1091 case CTL_TAG_ORDERED: 1092 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1093 break; 1094 case CTL_TAG_HEAD_OF_QUEUE: 1095 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1096 break; 1097 case CTL_TAG_UNTAGGED: 1098 case CTL_TAG_SIMPLE: 1099 case CTL_TAG_ACA: 1100 default: 1101 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1102 break; 1103 } 1104 1105 beio->io_len = 0; 1106 beio->io_offset = -1; 1107 1108 beio->bio_cmd = BIO_DELETE; 1109 beio->ds_trans_type = DEVSTAT_FREE; 1110 1111 DPRINTF("UNMAP\n"); 1112 1113 be_lun->unmap(be_lun, beio); 1114} 1115 1116static void 1117ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1118{ 1119 union ctl_io *io; 1120 1121 io = beio->io; 1122 ctl_free_beio(beio); 1123 ctl_config_write_done(io); 1124} 1125 1126static void 1127ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1128 union ctl_io *io) 1129{ 1130 struct ctl_be_block_io *beio; 1131 struct ctl_be_block_softc *softc; 1132 1133 DPRINTF("entered\n"); 1134 1135 softc = be_lun->softc; 1136 beio = ctl_alloc_beio(softc); 1137 beio->io = io; 1138 beio->lun = be_lun; 1139 beio->beio_cont = ctl_be_block_cw_done; 1140 PRIV(io)->ptr = (void *)beio; 1141 1142 switch (io->scsiio.cdb[0]) { 1143 case SYNCHRONIZE_CACHE: 1144 case SYNCHRONIZE_CACHE_16: 1145 beio->bio_cmd = BIO_FLUSH; 1146 beio->ds_trans_type = DEVSTAT_NO_DATA; 1147 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1148 beio->io_len = 0; 1149 be_lun->lun_flush(be_lun, beio); 1150 break; 1151 case WRITE_SAME_10: 1152 case WRITE_SAME_16: 1153 ctl_be_block_cw_dispatch_ws(be_lun, io); 1154 break; 1155 case UNMAP: 1156 ctl_be_block_cw_dispatch_unmap(be_lun, io); 1157 break; 1158 default: 1159 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1160 break; 1161 } 1162} 1163 1164SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1165SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1166SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1167SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1168 1169static void 1170ctl_be_block_next(struct ctl_be_block_io *beio) 1171{ 1172 struct ctl_be_block_lun *be_lun; 1173 union ctl_io *io; 1174 1175 io = beio->io; 1176 be_lun = beio->lun; 1177 ctl_free_beio(beio); 1178 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1179 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1180 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1181 ctl_data_submit_done(io); 1182 return; 1183 } 1184 1185 io->io_hdr.status &= ~CTL_STATUS_MASK; 1186 io->io_hdr.status |= CTL_STATUS_NONE; 1187 1188 mtx_lock(&be_lun->queue_lock); 1189 /* 1190 * XXX KDM make sure that links is okay to use at this point. 1191 * Otherwise, we either need to add another field to ctl_io_hdr, 1192 * or deal with resource allocation here. 1193 */ 1194 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1195 mtx_unlock(&be_lun->queue_lock); 1196 1197 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1198} 1199 1200static void 1201ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1202 union ctl_io *io) 1203{ 1204 struct ctl_be_block_io *beio; 1205 struct ctl_be_block_softc *softc; 1206 struct ctl_lba_len_flags *lbalen; 1207 struct ctl_ptr_len_flags *bptrlen; 1208 uint64_t len_left, lbas; 1209 int i; 1210 1211 softc = be_lun->softc; 1212 1213 DPRINTF("entered\n"); 1214 1215 lbalen = ARGS(io); 1216 if (lbalen->flags & CTL_LLF_WRITE) { 1217 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1218 } else { 1219 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1220 } 1221 1222 beio = ctl_alloc_beio(softc); 1223 beio->io = io; 1224 beio->lun = be_lun; 1225 bptrlen = PRIV(io); 1226 bptrlen->ptr = (void *)beio; 1227 1228 /* 1229 * If the I/O came down with an ordered or head of queue tag, set 1230 * the BIO_ORDERED attribute. For head of queue tags, that's 1231 * pretty much the best we can do. 1232 * 1233 * XXX KDM we don't have a great way to easily know about the FUA 1234 * bit right now (it is decoded in ctl_read_write(), but we don't 1235 * pass that knowledge to the backend), and in any case we would 1236 * need to determine how to handle it. 1237 */ 1238 if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1239 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1240 beio->bio_flags = BIO_ORDERED; 1241 1242 switch (io->scsiio.tag_type) { 1243 case CTL_TAG_ORDERED: 1244 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1245 break; 1246 case CTL_TAG_HEAD_OF_QUEUE: 1247 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1248 break; 1249 case CTL_TAG_UNTAGGED: 1250 case CTL_TAG_SIMPLE: 1251 case CTL_TAG_ACA: 1252 default: 1253 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1254 break; 1255 } 1256 1257 if (lbalen->flags & CTL_LLF_WRITE) { 1258 beio->bio_cmd = BIO_WRITE; 1259 beio->ds_trans_type = DEVSTAT_WRITE; 1260 } else { 1261 beio->bio_cmd = BIO_READ; 1262 beio->ds_trans_type = DEVSTAT_READ; 1263 } 1264 1265 DPRINTF("%s at LBA %jx len %u @%ju\n", 1266 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1267 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1268 if (lbalen->flags & CTL_LLF_COMPARE) 1269 lbas = CTLBLK_HALF_IO_SIZE; 1270 else 1271 lbas = CTLBLK_MAX_IO_SIZE; 1272 lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1273 beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1274 beio->io_len = lbas * be_lun->blocksize; 1275 bptrlen->len += lbas; 1276 1277 for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1278 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1279 i, CTLBLK_MAX_SEGS)); 1280 1281 /* 1282 * Setup the S/G entry for this chunk. 1283 */ 1284 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1285 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1286 1287 DPRINTF("segment %d addr %p len %zd\n", i, 1288 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1289 1290 /* Set up second segment for compare operation. */ 1291 if (lbalen->flags & CTL_LLF_COMPARE) { 1292 beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1293 beio->sg_segs[i].len; 1294 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1295 uma_zalloc(be_lun->lun_zone, M_WAITOK); 1296 } 1297 1298 beio->num_segs++; 1299 len_left -= beio->sg_segs[i].len; 1300 } 1301 if (bptrlen->len < lbalen->len) 1302 beio->beio_cont = ctl_be_block_next; 1303 io->scsiio.be_move_done = ctl_be_block_move_done; 1304 /* For compare we have separate S/G lists for read and datamove. */ 1305 if (lbalen->flags & CTL_LLF_COMPARE) 1306 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1307 else 1308 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1309 io->scsiio.kern_data_len = beio->io_len; 1310 io->scsiio.kern_data_resid = 0; 1311 io->scsiio.kern_sg_entries = beio->num_segs; 1312 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1313 1314 /* 1315 * For the read case, we need to read the data into our buffers and 1316 * then we can send it back to the user. For the write case, we 1317 * need to get the data from the user first. 1318 */ 1319 if (beio->bio_cmd == BIO_READ) { 1320 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1321 be_lun->dispatch(be_lun, beio); 1322 } else { 1323 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1324#ifdef CTL_TIME_IO 1325 getbintime(&io->io_hdr.dma_start_bt); 1326#endif 1327 ctl_datamove(io); 1328 } 1329} 1330 1331static void 1332ctl_be_block_worker(void *context, int pending) 1333{ 1334 struct ctl_be_block_lun *be_lun; 1335 struct ctl_be_block_softc *softc; 1336 union ctl_io *io; 1337 1338 be_lun = (struct ctl_be_block_lun *)context; 1339 softc = be_lun->softc; 1340 1341 DPRINTF("entered\n"); 1342 1343 mtx_lock(&be_lun->queue_lock); 1344 for (;;) { 1345 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1346 if (io != NULL) { 1347 struct ctl_be_block_io *beio; 1348 1349 DPRINTF("datamove queue\n"); 1350 1351 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1352 ctl_io_hdr, links); 1353 1354 mtx_unlock(&be_lun->queue_lock); 1355 1356 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1357 1358 be_lun->dispatch(be_lun, beio); 1359 1360 mtx_lock(&be_lun->queue_lock); 1361 continue; 1362 } 1363 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1364 if (io != NULL) { 1365 1366 DPRINTF("config write queue\n"); 1367 1368 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1369 ctl_io_hdr, links); 1370 1371 mtx_unlock(&be_lun->queue_lock); 1372 1373 ctl_be_block_cw_dispatch(be_lun, io); 1374 1375 mtx_lock(&be_lun->queue_lock); 1376 continue; 1377 } 1378 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1379 if (io != NULL) { 1380 DPRINTF("input queue\n"); 1381 1382 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1383 ctl_io_hdr, links); 1384 mtx_unlock(&be_lun->queue_lock); 1385 1386 /* 1387 * We must drop the lock, since this routine and 1388 * its children may sleep. 1389 */ 1390 ctl_be_block_dispatch(be_lun, io); 1391 1392 mtx_lock(&be_lun->queue_lock); 1393 continue; 1394 } 1395 1396 /* 1397 * If we get here, there is no work left in the queues, so 1398 * just break out and let the task queue go to sleep. 1399 */ 1400 break; 1401 } 1402 mtx_unlock(&be_lun->queue_lock); 1403} 1404 1405/* 1406 * Entry point from CTL to the backend for I/O. We queue everything to a 1407 * work thread, so this just puts the I/O on a queue and wakes up the 1408 * thread. 1409 */ 1410static int 1411ctl_be_block_submit(union ctl_io *io) 1412{ 1413 struct ctl_be_block_lun *be_lun; 1414 struct ctl_be_lun *ctl_be_lun; 1415 1416 DPRINTF("entered\n"); 1417 1418 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1419 CTL_PRIV_BACKEND_LUN].ptr; 1420 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1421 1422 /* 1423 * Make sure we only get SCSI I/O. 1424 */ 1425 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1426 "%#x) encountered", io->io_hdr.io_type)); 1427 1428 PRIV(io)->len = 0; 1429 1430 mtx_lock(&be_lun->queue_lock); 1431 /* 1432 * XXX KDM make sure that links is okay to use at this point. 1433 * Otherwise, we either need to add another field to ctl_io_hdr, 1434 * or deal with resource allocation here. 1435 */ 1436 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1437 mtx_unlock(&be_lun->queue_lock); 1438 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1439 1440 return (CTL_RETVAL_COMPLETE); 1441} 1442 1443static int 1444ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1445 int flag, struct thread *td) 1446{ 1447 struct ctl_be_block_softc *softc; 1448 int error; 1449 1450 softc = &backend_block_softc; 1451 1452 error = 0; 1453 1454 switch (cmd) { 1455 case CTL_LUN_REQ: { 1456 struct ctl_lun_req *lun_req; 1457 1458 lun_req = (struct ctl_lun_req *)addr; 1459 1460 switch (lun_req->reqtype) { 1461 case CTL_LUNREQ_CREATE: 1462 error = ctl_be_block_create(softc, lun_req); 1463 break; 1464 case CTL_LUNREQ_RM: 1465 error = ctl_be_block_rm(softc, lun_req); 1466 break; 1467 case CTL_LUNREQ_MODIFY: 1468 error = ctl_be_block_modify(softc, lun_req); 1469 break; 1470 default: 1471 lun_req->status = CTL_LUN_ERROR; 1472 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1473 "%s: invalid LUN request type %d", __func__, 1474 lun_req->reqtype); 1475 break; 1476 } 1477 break; 1478 } 1479 default: 1480 error = ENOTTY; 1481 break; 1482 } 1483 1484 return (error); 1485} 1486 1487static int 1488ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1489{ 1490 struct ctl_be_block_filedata *file_data; 1491 struct ctl_lun_create_params *params; 1492 struct vattr vattr; 1493 int error; 1494 1495 error = 0; 1496 file_data = &be_lun->backend.file; 1497 params = &req->reqdata.create; 1498 1499 be_lun->dev_type = CTL_BE_BLOCK_FILE; 1500 be_lun->dispatch = ctl_be_block_dispatch_file; 1501 be_lun->lun_flush = ctl_be_block_flush_file; 1502 1503 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1504 if (error != 0) { 1505 snprintf(req->error_str, sizeof(req->error_str), 1506 "error calling VOP_GETATTR() for file %s", 1507 be_lun->dev_path); 1508 return (error); 1509 } 1510 1511 /* 1512 * Verify that we have the ability to upgrade to exclusive 1513 * access on this file so we can trap errors at open instead 1514 * of reporting them during first access. 1515 */ 1516 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1517 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1518 if (be_lun->vn->v_iflag & VI_DOOMED) { 1519 error = EBADF; 1520 snprintf(req->error_str, sizeof(req->error_str), 1521 "error locking file %s", be_lun->dev_path); 1522 return (error); 1523 } 1524 } 1525 1526 1527 file_data->cred = crhold(curthread->td_ucred); 1528 if (params->lun_size_bytes != 0) 1529 be_lun->size_bytes = params->lun_size_bytes; 1530 else 1531 be_lun->size_bytes = vattr.va_size; 1532 /* 1533 * We set the multi thread flag for file operations because all 1534 * filesystems (in theory) are capable of allowing multiple readers 1535 * of a file at once. So we want to get the maximum possible 1536 * concurrency. 1537 */ 1538 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1539 1540 /* 1541 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here. 1542 * With ZFS, it is 131072 bytes. Block sizes that large don't work 1543 * with disklabel and UFS on FreeBSD at least. Large block sizes 1544 * may not work with other OSes as well. So just export a sector 1545 * size of 512 bytes, which should work with any OS or 1546 * application. Since our backing is a file, any block size will 1547 * work fine for the backing store. 1548 */ 1549#if 0 1550 be_lun->blocksize= vattr.va_blocksize; 1551#endif 1552 if (params->blocksize_bytes != 0) 1553 be_lun->blocksize = params->blocksize_bytes; 1554 else 1555 be_lun->blocksize = 512; 1556 1557 /* 1558 * Sanity check. The media size has to be at least one 1559 * sector long. 1560 */ 1561 if (be_lun->size_bytes < be_lun->blocksize) { 1562 error = EINVAL; 1563 snprintf(req->error_str, sizeof(req->error_str), 1564 "file %s size %ju < block size %u", be_lun->dev_path, 1565 (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1566 } 1567 return (error); 1568} 1569 1570static int 1571ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1572{ 1573 struct ctl_lun_create_params *params; 1574 struct vattr vattr; 1575 struct cdev *dev; 1576 struct cdevsw *devsw; 1577 int error; 1578 off_t ps, pss, po, pos; 1579 1580 params = &req->reqdata.create; 1581 1582 be_lun->dev_type = CTL_BE_BLOCK_DEV; 1583 be_lun->dispatch = ctl_be_block_dispatch_dev; 1584 be_lun->lun_flush = ctl_be_block_flush_dev; 1585 be_lun->unmap = ctl_be_block_unmap_dev; 1586 be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1587 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1588 &be_lun->backend.dev.dev_ref); 1589 if (be_lun->backend.dev.csw == NULL) 1590 panic("Unable to retrieve device switch"); 1591 1592 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1593 if (error) { 1594 snprintf(req->error_str, sizeof(req->error_str), 1595 "%s: error getting vnode attributes for device %s", 1596 __func__, be_lun->dev_path); 1597 return (error); 1598 } 1599 1600 dev = be_lun->vn->v_rdev; 1601 devsw = dev->si_devsw; 1602 if (!devsw->d_ioctl) { 1603 snprintf(req->error_str, sizeof(req->error_str), 1604 "%s: no d_ioctl for device %s!", __func__, 1605 be_lun->dev_path); 1606 return (ENODEV); 1607 } 1608 1609 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1610 (caddr_t)&be_lun->blocksize, FREAD, 1611 curthread); 1612 if (error) { 1613 snprintf(req->error_str, sizeof(req->error_str), 1614 "%s: error %d returned for DIOCGSECTORSIZE ioctl " 1615 "on %s!", __func__, error, be_lun->dev_path); 1616 return (error); 1617 } 1618 1619 /* 1620 * If the user has asked for a blocksize that is greater than the 1621 * backing device's blocksize, we can do it only if the blocksize 1622 * the user is asking for is an even multiple of the underlying 1623 * device's blocksize. 1624 */ 1625 if ((params->blocksize_bytes != 0) 1626 && (params->blocksize_bytes > be_lun->blocksize)) { 1627 uint32_t bs_multiple, tmp_blocksize; 1628 1629 bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1630 1631 tmp_blocksize = bs_multiple * be_lun->blocksize; 1632 1633 if (tmp_blocksize == params->blocksize_bytes) { 1634 be_lun->blocksize = params->blocksize_bytes; 1635 } else { 1636 snprintf(req->error_str, sizeof(req->error_str), 1637 "%s: requested blocksize %u is not an even " 1638 "multiple of backing device blocksize %u", 1639 __func__, params->blocksize_bytes, 1640 be_lun->blocksize); 1641 return (EINVAL); 1642 1643 } 1644 } else if ((params->blocksize_bytes != 0) 1645 && (params->blocksize_bytes != be_lun->blocksize)) { 1646 snprintf(req->error_str, sizeof(req->error_str), 1647 "%s: requested blocksize %u < backing device " 1648 "blocksize %u", __func__, params->blocksize_bytes, 1649 be_lun->blocksize); 1650 return (EINVAL); 1651 } 1652 1653 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1654 (caddr_t)&be_lun->size_bytes, FREAD, 1655 curthread); 1656 if (error) { 1657 snprintf(req->error_str, sizeof(req->error_str), 1658 "%s: error %d returned for DIOCGMEDIASIZE " 1659 " ioctl on %s!", __func__, error, 1660 be_lun->dev_path); 1661 return (error); 1662 } 1663 1664 if (params->lun_size_bytes != 0) { 1665 if (params->lun_size_bytes > be_lun->size_bytes) { 1666 snprintf(req->error_str, sizeof(req->error_str), 1667 "%s: requested LUN size %ju > backing device " 1668 "size %ju", __func__, 1669 (uintmax_t)params->lun_size_bytes, 1670 (uintmax_t)be_lun->size_bytes); 1671 return (EINVAL); 1672 } 1673 1674 be_lun->size_bytes = params->lun_size_bytes; 1675 } 1676 1677 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1678 (caddr_t)&ps, FREAD, curthread); 1679 if (error) 1680 ps = po = 0; 1681 else { 1682 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1683 (caddr_t)&po, FREAD, curthread); 1684 if (error) 1685 po = 0; 1686 } 1687 pss = ps / be_lun->blocksize; 1688 pos = po / be_lun->blocksize; 1689 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1690 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1691 be_lun->pblockexp = fls(pss) - 1; 1692 be_lun->pblockoff = (pss - pos) % pss; 1693 } 1694 1695 return (0); 1696} 1697 1698static int 1699ctl_be_block_close(struct ctl_be_block_lun *be_lun) 1700{ 1701 DROP_GIANT(); 1702 if (be_lun->vn) { 1703 int flags = FREAD | FWRITE; 1704 1705 switch (be_lun->dev_type) { 1706 case CTL_BE_BLOCK_DEV: 1707 if (be_lun->backend.dev.csw) { 1708 dev_relthread(be_lun->backend.dev.cdev, 1709 be_lun->backend.dev.dev_ref); 1710 be_lun->backend.dev.csw = NULL; 1711 be_lun->backend.dev.cdev = NULL; 1712 } 1713 break; 1714 case CTL_BE_BLOCK_FILE: 1715 break; 1716 case CTL_BE_BLOCK_NONE: 1717 break; 1718 default: 1719 panic("Unexpected backend type."); 1720 break; 1721 } 1722 1723 (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1724 be_lun->vn = NULL; 1725 1726 switch (be_lun->dev_type) { 1727 case CTL_BE_BLOCK_DEV: 1728 break; 1729 case CTL_BE_BLOCK_FILE: 1730 if (be_lun->backend.file.cred != NULL) { 1731 crfree(be_lun->backend.file.cred); 1732 be_lun->backend.file.cred = NULL; 1733 } 1734 break; 1735 case CTL_BE_BLOCK_NONE: 1736 break; 1737 default: 1738 panic("Unexpected backend type."); 1739 break; 1740 } 1741 } 1742 PICKUP_GIANT(); 1743 1744 return (0); 1745} 1746 1747static int 1748ctl_be_block_open(struct ctl_be_block_softc *softc, 1749 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1750{ 1751 struct nameidata nd; 1752 int flags; 1753 int error; 1754 1755 /* 1756 * XXX KDM allow a read-only option? 1757 */ 1758 flags = FREAD | FWRITE; 1759 error = 0; 1760 1761 if (rootvnode == NULL) { 1762 snprintf(req->error_str, sizeof(req->error_str), 1763 "%s: Root filesystem is not mounted", __func__); 1764 return (1); 1765 } 1766 1767 if (!curthread->td_proc->p_fd->fd_cdir) { 1768 curthread->td_proc->p_fd->fd_cdir = rootvnode; 1769 VREF(rootvnode); 1770 } 1771 if (!curthread->td_proc->p_fd->fd_rdir) { 1772 curthread->td_proc->p_fd->fd_rdir = rootvnode; 1773 VREF(rootvnode); 1774 } 1775 if (!curthread->td_proc->p_fd->fd_jdir) { 1776 curthread->td_proc->p_fd->fd_jdir = rootvnode; 1777 VREF(rootvnode); 1778 } 1779 1780 again: 1781 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1782 error = vn_open(&nd, &flags, 0, NULL); 1783 if (error) { 1784 /* 1785 * This is the only reasonable guess we can make as far as 1786 * path if the user doesn't give us a fully qualified path. 1787 * If they want to specify a file, they need to specify the 1788 * full path. 1789 */ 1790 if (be_lun->dev_path[0] != '/') { 1791 char *dev_path = "/dev/"; 1792 char *dev_name; 1793 1794 /* Try adding device path at beginning of name */ 1795 dev_name = malloc(strlen(be_lun->dev_path) 1796 + strlen(dev_path) + 1, 1797 M_CTLBLK, M_WAITOK); 1798 if (dev_name) { 1799 sprintf(dev_name, "%s%s", dev_path, 1800 be_lun->dev_path); 1801 free(be_lun->dev_path, M_CTLBLK); 1802 be_lun->dev_path = dev_name; 1803 goto again; 1804 } 1805 } 1806 snprintf(req->error_str, sizeof(req->error_str), 1807 "%s: error opening %s", __func__, be_lun->dev_path); 1808 return (error); 1809 } 1810 1811 NDFREE(&nd, NDF_ONLY_PNBUF); 1812 1813 be_lun->vn = nd.ni_vp; 1814 1815 /* We only support disks and files. */ 1816 if (vn_isdisk(be_lun->vn, &error)) { 1817 error = ctl_be_block_open_dev(be_lun, req); 1818 } else if (be_lun->vn->v_type == VREG) { 1819 error = ctl_be_block_open_file(be_lun, req); 1820 } else { 1821 error = EINVAL; 1822 snprintf(req->error_str, sizeof(req->error_str), 1823 "%s is not a disk or plain file", be_lun->dev_path); 1824 } 1825 VOP_UNLOCK(be_lun->vn, 0); 1826 1827 if (error != 0) { 1828 ctl_be_block_close(be_lun); 1829 return (error); 1830 } 1831 1832 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1833 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1834 1835 return (0); 1836} 1837 1838static int 1839ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1840{ 1841 struct ctl_be_block_lun *be_lun; 1842 struct ctl_lun_create_params *params; 1843 char num_thread_str[16]; 1844 char tmpstr[32]; 1845 char *value; 1846 int retval, num_threads, unmap; 1847 int tmp_num_threads; 1848 1849 params = &req->reqdata.create; 1850 retval = 0; 1851 1852 num_threads = cbb_num_threads; 1853 1854 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1855 1856 be_lun->softc = softc; 1857 STAILQ_INIT(&be_lun->input_queue); 1858 STAILQ_INIT(&be_lun->config_write_queue); 1859 STAILQ_INIT(&be_lun->datamove_queue); 1860 sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1861 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 1862 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 1863 ctl_init_opts(&be_lun->ctl_be_lun.options, 1864 req->num_be_args, req->kern_be_args); 1865 1866 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1867 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1868 1869 if (be_lun->lun_zone == NULL) { 1870 snprintf(req->error_str, sizeof(req->error_str), 1871 "%s: error allocating UMA zone", __func__); 1872 goto bailout_error; 1873 } 1874 1875 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1876 be_lun->ctl_be_lun.lun_type = params->device_type; 1877 else 1878 be_lun->ctl_be_lun.lun_type = T_DIRECT; 1879 1880 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1881 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 1882 if (value == NULL) { 1883 snprintf(req->error_str, sizeof(req->error_str), 1884 "%s: no file argument specified", __func__); 1885 goto bailout_error; 1886 } 1887 be_lun->dev_path = strdup(value, M_CTLBLK); 1888 1889 retval = ctl_be_block_open(softc, be_lun, req); 1890 if (retval != 0) { 1891 retval = 0; 1892 goto bailout_error; 1893 } 1894 1895 /* 1896 * Tell the user the size of the file/device. 1897 */ 1898 params->lun_size_bytes = be_lun->size_bytes; 1899 1900 /* 1901 * The maximum LBA is the size - 1. 1902 */ 1903 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 1904 } else { 1905 /* 1906 * For processor devices, we don't have any size. 1907 */ 1908 be_lun->blocksize = 0; 1909 be_lun->pblockexp = 0; 1910 be_lun->pblockoff = 0; 1911 be_lun->size_blocks = 0; 1912 be_lun->size_bytes = 0; 1913 be_lun->ctl_be_lun.maxlba = 0; 1914 params->lun_size_bytes = 0; 1915 1916 /* 1917 * Default to just 1 thread for processor devices. 1918 */ 1919 num_threads = 1; 1920 } 1921 1922 /* 1923 * XXX This searching loop might be refactored to be combined with 1924 * the loop above, 1925 */ 1926 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 1927 if (value != NULL) { 1928 tmp_num_threads = strtol(value, NULL, 0); 1929 1930 /* 1931 * We don't let the user specify less than one 1932 * thread, but hope he's clueful enough not to 1933 * specify 1000 threads. 1934 */ 1935 if (tmp_num_threads < 1) { 1936 snprintf(req->error_str, sizeof(req->error_str), 1937 "%s: invalid number of threads %s", 1938 __func__, num_thread_str); 1939 goto bailout_error; 1940 } 1941 num_threads = tmp_num_threads; 1942 } 1943 unmap = 0; 1944 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 1945 if (value != NULL && strcmp(value, "on") == 0) 1946 unmap = 1; 1947 1948 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 1949 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 1950 if (unmap) 1951 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 1952 be_lun->ctl_be_lun.be_lun = be_lun; 1953 be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 1954 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 1955 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 1956 /* Tell the user the blocksize we ended up using */ 1957 params->blocksize_bytes = be_lun->blocksize; 1958 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 1959 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 1960 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 1961 } else 1962 be_lun->ctl_be_lun.req_lun_id = 0; 1963 1964 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 1965 be_lun->ctl_be_lun.lun_config_status = 1966 ctl_be_block_lun_config_status; 1967 be_lun->ctl_be_lun.be = &ctl_be_block_driver; 1968 1969 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 1970 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 1971 softc->num_luns); 1972 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 1973 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1974 sizeof(tmpstr))); 1975 1976 /* Tell the user what we used for a serial number */ 1977 strncpy((char *)params->serial_num, tmpstr, 1978 ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 1979 } else { 1980 strncpy((char *)be_lun->ctl_be_lun.serial_num, 1981 params->serial_num, 1982 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1983 sizeof(params->serial_num))); 1984 } 1985 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 1986 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 1987 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 1988 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1989 sizeof(tmpstr))); 1990 1991 /* Tell the user what we used for a device ID */ 1992 strncpy((char *)params->device_id, tmpstr, 1993 ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 1994 } else { 1995 strncpy((char *)be_lun->ctl_be_lun.device_id, 1996 params->device_id, 1997 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1998 sizeof(params->device_id))); 1999 } 2000 2001 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2002 2003 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2004 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2005 2006 if (be_lun->io_taskqueue == NULL) { 2007 snprintf(req->error_str, sizeof(req->error_str), 2008 "%s: Unable to create taskqueue", __func__); 2009 goto bailout_error; 2010 } 2011 2012 /* 2013 * Note that we start the same number of threads by default for 2014 * both the file case and the block device case. For the file 2015 * case, we need multiple threads to allow concurrency, because the 2016 * vnode interface is designed to be a blocking interface. For the 2017 * block device case, ZFS zvols at least will block the caller's 2018 * context in many instances, and so we need multiple threads to 2019 * overcome that problem. Other block devices don't need as many 2020 * threads, but they shouldn't cause too many problems. 2021 * 2022 * If the user wants to just have a single thread for a block 2023 * device, he can specify that when the LUN is created, or change 2024 * the tunable/sysctl to alter the default number of threads. 2025 */ 2026 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2027 /*num threads*/num_threads, 2028 /*priority*/PWAIT, 2029 /*thread name*/ 2030 "%s taskq", be_lun->lunname); 2031 2032 if (retval != 0) 2033 goto bailout_error; 2034 2035 be_lun->num_threads = num_threads; 2036 2037 mtx_lock(&softc->lock); 2038 softc->num_luns++; 2039 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2040 2041 mtx_unlock(&softc->lock); 2042 2043 retval = ctl_add_lun(&be_lun->ctl_be_lun); 2044 if (retval != 0) { 2045 mtx_lock(&softc->lock); 2046 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2047 links); 2048 softc->num_luns--; 2049 mtx_unlock(&softc->lock); 2050 snprintf(req->error_str, sizeof(req->error_str), 2051 "%s: ctl_add_lun() returned error %d, see dmesg for " 2052 "details", __func__, retval); 2053 retval = 0; 2054 goto bailout_error; 2055 } 2056 2057 mtx_lock(&softc->lock); 2058 2059 /* 2060 * Tell the config_status routine that we're waiting so it won't 2061 * clean up the LUN in the event of an error. 2062 */ 2063 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2064 2065 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2066 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2067 if (retval == EINTR) 2068 break; 2069 } 2070 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2071 2072 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2073 snprintf(req->error_str, sizeof(req->error_str), 2074 "%s: LUN configuration error, see dmesg for details", 2075 __func__); 2076 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2077 links); 2078 softc->num_luns--; 2079 mtx_unlock(&softc->lock); 2080 goto bailout_error; 2081 } else { 2082 params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2083 } 2084 2085 mtx_unlock(&softc->lock); 2086 2087 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2088 be_lun->blocksize, 2089 DEVSTAT_ALL_SUPPORTED, 2090 be_lun->ctl_be_lun.lun_type 2091 | DEVSTAT_TYPE_IF_OTHER, 2092 DEVSTAT_PRIORITY_OTHER); 2093 2094 2095 req->status = CTL_LUN_OK; 2096 2097 return (retval); 2098 2099bailout_error: 2100 req->status = CTL_LUN_ERROR; 2101 2102 if (be_lun->io_taskqueue != NULL) 2103 taskqueue_free(be_lun->io_taskqueue); 2104 ctl_be_block_close(be_lun); 2105 if (be_lun->dev_path != NULL) 2106 free(be_lun->dev_path, M_CTLBLK); 2107 if (be_lun->lun_zone != NULL) 2108 uma_zdestroy(be_lun->lun_zone); 2109 ctl_free_opts(&be_lun->ctl_be_lun.options); 2110 mtx_destroy(&be_lun->queue_lock); 2111 mtx_destroy(&be_lun->io_lock); 2112 free(be_lun, M_CTLBLK); 2113 2114 return (retval); 2115} 2116 2117static int 2118ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2119{ 2120 struct ctl_lun_rm_params *params; 2121 struct ctl_be_block_lun *be_lun; 2122 int retval; 2123 2124 params = &req->reqdata.rm; 2125 2126 mtx_lock(&softc->lock); 2127 2128 be_lun = NULL; 2129 2130 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2131 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2132 break; 2133 } 2134 mtx_unlock(&softc->lock); 2135 2136 if (be_lun == NULL) { 2137 snprintf(req->error_str, sizeof(req->error_str), 2138 "%s: LUN %u is not managed by the block backend", 2139 __func__, params->lun_id); 2140 goto bailout_error; 2141 } 2142 2143 retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2144 2145 if (retval != 0) { 2146 snprintf(req->error_str, sizeof(req->error_str), 2147 "%s: error %d returned from ctl_disable_lun() for " 2148 "LUN %d", __func__, retval, params->lun_id); 2149 goto bailout_error; 2150 2151 } 2152 2153 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2154 if (retval != 0) { 2155 snprintf(req->error_str, sizeof(req->error_str), 2156 "%s: error %d returned from ctl_invalidate_lun() for " 2157 "LUN %d", __func__, retval, params->lun_id); 2158 goto bailout_error; 2159 } 2160 2161 mtx_lock(&softc->lock); 2162 2163 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2164 2165 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2166 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2167 if (retval == EINTR) 2168 break; 2169 } 2170 2171 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2172 2173 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2174 snprintf(req->error_str, sizeof(req->error_str), 2175 "%s: interrupted waiting for LUN to be freed", 2176 __func__); 2177 mtx_unlock(&softc->lock); 2178 goto bailout_error; 2179 } 2180 2181 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2182 2183 softc->num_luns--; 2184 mtx_unlock(&softc->lock); 2185 2186 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2187 2188 taskqueue_free(be_lun->io_taskqueue); 2189 2190 ctl_be_block_close(be_lun); 2191 2192 if (be_lun->disk_stats != NULL) 2193 devstat_remove_entry(be_lun->disk_stats); 2194 2195 uma_zdestroy(be_lun->lun_zone); 2196 2197 ctl_free_opts(&be_lun->ctl_be_lun.options); 2198 free(be_lun->dev_path, M_CTLBLK); 2199 mtx_destroy(&be_lun->queue_lock); 2200 mtx_destroy(&be_lun->io_lock); 2201 free(be_lun, M_CTLBLK); 2202 2203 req->status = CTL_LUN_OK; 2204 2205 return (0); 2206 2207bailout_error: 2208 2209 req->status = CTL_LUN_ERROR; 2210 2211 return (0); 2212} 2213 2214static int 2215ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2216 struct ctl_lun_req *req) 2217{ 2218 struct vattr vattr; 2219 int error; 2220 struct ctl_lun_modify_params *params; 2221 2222 params = &req->reqdata.modify; 2223 2224 if (params->lun_size_bytes != 0) { 2225 be_lun->size_bytes = params->lun_size_bytes; 2226 } else { 2227 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2228 if (error != 0) { 2229 snprintf(req->error_str, sizeof(req->error_str), 2230 "error calling VOP_GETATTR() for file %s", 2231 be_lun->dev_path); 2232 return (error); 2233 } 2234 2235 be_lun->size_bytes = vattr.va_size; 2236 } 2237 2238 return (0); 2239} 2240 2241static int 2242ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2243 struct ctl_lun_req *req) 2244{ 2245 struct cdev *dev; 2246 struct cdevsw *devsw; 2247 int error; 2248 struct ctl_lun_modify_params *params; 2249 uint64_t size_bytes; 2250 2251 params = &req->reqdata.modify; 2252 2253 dev = be_lun->vn->v_rdev; 2254 devsw = dev->si_devsw; 2255 if (!devsw->d_ioctl) { 2256 snprintf(req->error_str, sizeof(req->error_str), 2257 "%s: no d_ioctl for device %s!", __func__, 2258 be_lun->dev_path); 2259 return (ENODEV); 2260 } 2261 2262 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 2263 (caddr_t)&size_bytes, FREAD, 2264 curthread); 2265 if (error) { 2266 snprintf(req->error_str, sizeof(req->error_str), 2267 "%s: error %d returned for DIOCGMEDIASIZE ioctl " 2268 "on %s!", __func__, error, be_lun->dev_path); 2269 return (error); 2270 } 2271 2272 if (params->lun_size_bytes != 0) { 2273 if (params->lun_size_bytes > size_bytes) { 2274 snprintf(req->error_str, sizeof(req->error_str), 2275 "%s: requested LUN size %ju > backing device " 2276 "size %ju", __func__, 2277 (uintmax_t)params->lun_size_bytes, 2278 (uintmax_t)size_bytes); 2279 return (EINVAL); 2280 } 2281 2282 be_lun->size_bytes = params->lun_size_bytes; 2283 } else { 2284 be_lun->size_bytes = size_bytes; 2285 } 2286 2287 return (0); 2288} 2289 2290static int 2291ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2292{ 2293 struct ctl_lun_modify_params *params; 2294 struct ctl_be_block_lun *be_lun; 2295 int error; 2296 2297 params = &req->reqdata.modify; 2298 2299 mtx_lock(&softc->lock); 2300 2301 be_lun = NULL; 2302 2303 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2304 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2305 break; 2306 } 2307 mtx_unlock(&softc->lock); 2308 2309 if (be_lun == NULL) { 2310 snprintf(req->error_str, sizeof(req->error_str), 2311 "%s: LUN %u is not managed by the block backend", 2312 __func__, params->lun_id); 2313 goto bailout_error; 2314 } 2315 2316 if (params->lun_size_bytes != 0) { 2317 if (params->lun_size_bytes < be_lun->blocksize) { 2318 snprintf(req->error_str, sizeof(req->error_str), 2319 "%s: LUN size %ju < blocksize %u", __func__, 2320 params->lun_size_bytes, be_lun->blocksize); 2321 goto bailout_error; 2322 } 2323 } 2324 2325 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2326 2327 if (be_lun->vn->v_type == VREG) 2328 error = ctl_be_block_modify_file(be_lun, req); 2329 else 2330 error = ctl_be_block_modify_dev(be_lun, req); 2331 2332 VOP_UNLOCK(be_lun->vn, 0); 2333 2334 if (error != 0) 2335 goto bailout_error; 2336 2337 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2338 2339 /* 2340 * The maximum LBA is the size - 1. 2341 * 2342 * XXX: Note that this field is being updated without locking, 2343 * which might cause problems on 32-bit architectures. 2344 */ 2345 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 2346 ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2347 2348 /* Tell the user the exact size we ended up using */ 2349 params->lun_size_bytes = be_lun->size_bytes; 2350 2351 req->status = CTL_LUN_OK; 2352 2353 return (0); 2354 2355bailout_error: 2356 req->status = CTL_LUN_ERROR; 2357 2358 return (0); 2359} 2360 2361static void 2362ctl_be_block_lun_shutdown(void *be_lun) 2363{ 2364 struct ctl_be_block_lun *lun; 2365 struct ctl_be_block_softc *softc; 2366 2367 lun = (struct ctl_be_block_lun *)be_lun; 2368 2369 softc = lun->softc; 2370 2371 mtx_lock(&softc->lock); 2372 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2373 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2374 wakeup(lun); 2375 mtx_unlock(&softc->lock); 2376 2377} 2378 2379static void 2380ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2381{ 2382 struct ctl_be_block_lun *lun; 2383 struct ctl_be_block_softc *softc; 2384 2385 lun = (struct ctl_be_block_lun *)be_lun; 2386 softc = lun->softc; 2387 2388 if (status == CTL_LUN_CONFIG_OK) { 2389 mtx_lock(&softc->lock); 2390 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2391 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2392 wakeup(lun); 2393 mtx_unlock(&softc->lock); 2394 2395 /* 2396 * We successfully added the LUN, attempt to enable it. 2397 */ 2398 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2399 printf("%s: ctl_enable_lun() failed!\n", __func__); 2400 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2401 printf("%s: ctl_invalidate_lun() failed!\n", 2402 __func__); 2403 } 2404 } 2405 2406 return; 2407 } 2408 2409 2410 mtx_lock(&softc->lock); 2411 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2412 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2413 wakeup(lun); 2414 mtx_unlock(&softc->lock); 2415} 2416 2417 2418static int 2419ctl_be_block_config_write(union ctl_io *io) 2420{ 2421 struct ctl_be_block_lun *be_lun; 2422 struct ctl_be_lun *ctl_be_lun; 2423 int retval; 2424 2425 retval = 0; 2426 2427 DPRINTF("entered\n"); 2428 2429 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2430 CTL_PRIV_BACKEND_LUN].ptr; 2431 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2432 2433 switch (io->scsiio.cdb[0]) { 2434 case SYNCHRONIZE_CACHE: 2435 case SYNCHRONIZE_CACHE_16: 2436 case WRITE_SAME_10: 2437 case WRITE_SAME_16: 2438 case UNMAP: 2439 /* 2440 * The upper level CTL code will filter out any CDBs with 2441 * the immediate bit set and return the proper error. 2442 * 2443 * We don't really need to worry about what LBA range the 2444 * user asked to be synced out. When they issue a sync 2445 * cache command, we'll sync out the whole thing. 2446 */ 2447 mtx_lock(&be_lun->queue_lock); 2448 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2449 links); 2450 mtx_unlock(&be_lun->queue_lock); 2451 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2452 break; 2453 case START_STOP_UNIT: { 2454 struct scsi_start_stop_unit *cdb; 2455 2456 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2457 2458 if (cdb->how & SSS_START) 2459 retval = ctl_start_lun(ctl_be_lun); 2460 else { 2461 retval = ctl_stop_lun(ctl_be_lun); 2462 /* 2463 * XXX KDM Copan-specific offline behavior. 2464 * Figure out a reasonable way to port this? 2465 */ 2466#ifdef NEEDTOPORT 2467 if ((retval == 0) 2468 && (cdb->byte2 & SSS_ONOFFLINE)) 2469 retval = ctl_lun_offline(ctl_be_lun); 2470#endif 2471 } 2472 2473 /* 2474 * In general, the above routines should not fail. They 2475 * just set state for the LUN. So we've got something 2476 * pretty wrong here if we can't start or stop the LUN. 2477 */ 2478 if (retval != 0) { 2479 ctl_set_internal_failure(&io->scsiio, 2480 /*sks_valid*/ 1, 2481 /*retry_count*/ 0xf051); 2482 retval = CTL_RETVAL_COMPLETE; 2483 } else { 2484 ctl_set_success(&io->scsiio); 2485 } 2486 ctl_config_write_done(io); 2487 break; 2488 } 2489 default: 2490 ctl_set_invalid_opcode(&io->scsiio); 2491 ctl_config_write_done(io); 2492 retval = CTL_RETVAL_COMPLETE; 2493 break; 2494 } 2495 2496 return (retval); 2497 2498} 2499 2500static int 2501ctl_be_block_config_read(union ctl_io *io) 2502{ 2503 return (0); 2504} 2505 2506static int 2507ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2508{ 2509 struct ctl_be_block_lun *lun; 2510 int retval; 2511 2512 lun = (struct ctl_be_block_lun *)be_lun; 2513 retval = 0; 2514 2515 retval = sbuf_printf(sb, "\t<num_threads>"); 2516 2517 if (retval != 0) 2518 goto bailout; 2519 2520 retval = sbuf_printf(sb, "%d", lun->num_threads); 2521 2522 if (retval != 0) 2523 goto bailout; 2524 2525 retval = sbuf_printf(sb, "</num_threads>\n"); 2526 2527bailout: 2528 2529 return (retval); 2530} 2531 2532int 2533ctl_be_block_init(void) 2534{ 2535 struct ctl_be_block_softc *softc; 2536 int retval; 2537 2538 softc = &backend_block_softc; 2539 retval = 0; 2540 2541 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2542 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2543 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2544 STAILQ_INIT(&softc->disk_list); 2545 STAILQ_INIT(&softc->lun_list); 2546 2547 return (retval); 2548} 2549