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