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_lun_create_params params; 155 struct ctl_block_disk *disk; 156 char lunname[32]; 157 char *dev_path; 158 ctl_be_block_type dev_type; 159 struct vnode *vn; 160 union ctl_be_block_bedata backend; 161 cbb_dispatch_t dispatch; 162 cbb_dispatch_t lun_flush; 163 cbb_dispatch_t unmap; 164 uma_zone_t lun_zone; 165 uint64_t size_blocks; 166 uint64_t size_bytes; 167 uint32_t blocksize; 168 int blocksize_shift; 169 uint16_t pblockexp; 170 uint16_t pblockoff; 171 struct ctl_be_block_softc *softc; 172 struct devstat *disk_stats; 173 ctl_be_block_lun_flags flags; 174 STAILQ_ENTRY(ctl_be_block_lun) links; 175 struct ctl_be_lun ctl_be_lun; 176 struct taskqueue *io_taskqueue; 177 struct task io_task; 178 int num_threads; 179 STAILQ_HEAD(, ctl_io_hdr) input_queue; 180 STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 181 STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 182 struct mtx_padalign io_lock; 183 struct mtx_padalign queue_lock; 184 }; 185 186 /* 187 * Overall softc structure for the block backend module. 188 */ 189 struct ctl_be_block_softc { 190 struct mtx lock; 191 int num_disks; 192 STAILQ_HEAD(, ctl_block_disk) disk_list; 193 int num_luns; 194 STAILQ_HEAD(, ctl_be_block_lun) lun_list; 195 }; 196 197 static struct ctl_be_block_softc backend_block_softc; 198 199 /* 200 * Per-I/O information. 201 */ 202 struct ctl_be_block_io { 203 union ctl_io *io; 204 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 205 struct iovec xiovecs[CTLBLK_MAX_SEGS]; 206 int bio_cmd; 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 = 0; 603 if (ARGS(io)->flags & CTL_LLF_DPO) 604 flags |= IO_DIRECT; 605 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 606 flags |= IO_SYNC; 607 608 bzero(&xuio, sizeof(xuio)); 609 if (beio->bio_cmd == BIO_READ) { 610 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 611 xuio.uio_rw = UIO_READ; 612 } else { 613 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 614 xuio.uio_rw = UIO_WRITE; 615 } 616 xuio.uio_offset = beio->io_offset; 617 xuio.uio_resid = beio->io_len; 618 xuio.uio_segflg = UIO_SYSSPACE; 619 xuio.uio_iov = beio->xiovecs; 620 xuio.uio_iovcnt = beio->num_segs; 621 xuio.uio_td = curthread; 622 623 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 624 xiovec->iov_base = beio->sg_segs[i].addr; 625 xiovec->iov_len = beio->sg_segs[i].len; 626 } 627 628 binuptime(&beio->ds_t0); 629 mtx_lock(&be_lun->io_lock); 630 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 631 mtx_unlock(&be_lun->io_lock); 632 633 if (beio->bio_cmd == BIO_READ) { 634 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 635 636 /* 637 * UFS pays attention to IO_DIRECT for reads. If the 638 * DIRECTIO option is configured into the kernel, it calls 639 * ffs_rawread(). But that only works for single-segment 640 * uios with user space addresses. In our case, with a 641 * kernel uio, it still reads into the buffer cache, but it 642 * will just try to release the buffer from the cache later 643 * on in ffs_read(). 644 * 645 * ZFS does not pay attention to IO_DIRECT for reads. 646 * 647 * UFS does not pay attention to IO_SYNC for reads. 648 * 649 * ZFS pays attention to IO_SYNC (which translates into the 650 * Solaris define FRSYNC for zfs_read()) for reads. It 651 * attempts to sync the file before reading. 652 * 653 * So, to attempt to provide some barrier semantics in the 654 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 655 */ 656 error = VOP_READ(be_lun->vn, &xuio, flags, 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, file_data->cred); 694 VOP_UNLOCK(be_lun->vn, 0); 695 696 vn_finished_write(mountpoint); 697 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 698 } 699 700 mtx_lock(&be_lun->io_lock); 701 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 702 beio->ds_tag_type, beio->ds_trans_type, 703 /*now*/ NULL, /*then*/&beio->ds_t0); 704 mtx_unlock(&be_lun->io_lock); 705 706 /* 707 * If we got an error, set the sense data to "MEDIUM ERROR" and 708 * return the I/O to the user. 709 */ 710 if (error != 0) { 711 char path_str[32]; 712 713 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 714 /* 715 * XXX KDM ZFS returns ENOSPC when the underlying 716 * filesystem fills up. What kind of SCSI error should we 717 * return for that? 718 */ 719 printf("%s%s command returned errno %d\n", path_str, 720 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 721 ctl_set_medium_error(&io->scsiio); 722 ctl_complete_beio(beio); 723 return; 724 } 725 726 /* 727 * If this is a write or a verify, we're all done. 728 * If this is a read, we can now send the data to the user. 729 */ 730 if ((beio->bio_cmd == BIO_WRITE) || 731 (ARGS(io)->flags & 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 742 static void 743 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 744 struct ctl_be_block_io *beio) 745 { 746 struct ctl_be_block_devdata *dev_data; 747 union ctl_io *io; 748 struct uio xuio; 749 struct iovec *xiovec; 750 int flags; 751 int error, i; 752 753 DPRINTF("entered\n"); 754 755 dev_data = &be_lun->backend.dev; 756 io = beio->io; 757 flags = 0; 758 if (ARGS(io)->flags & CTL_LLF_DPO) 759 flags |= IO_DIRECT; 760 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 761 flags |= IO_SYNC; 762 763 bzero(&xuio, sizeof(xuio)); 764 if (beio->bio_cmd == BIO_READ) { 765 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 766 xuio.uio_rw = UIO_READ; 767 } else { 768 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 769 xuio.uio_rw = UIO_WRITE; 770 } 771 xuio.uio_offset = beio->io_offset; 772 xuio.uio_resid = beio->io_len; 773 xuio.uio_segflg = UIO_SYSSPACE; 774 xuio.uio_iov = beio->xiovecs; 775 xuio.uio_iovcnt = beio->num_segs; 776 xuio.uio_td = curthread; 777 778 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 779 xiovec->iov_base = beio->sg_segs[i].addr; 780 xiovec->iov_len = beio->sg_segs[i].len; 781 } 782 783 binuptime(&beio->ds_t0); 784 mtx_lock(&be_lun->io_lock); 785 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 786 mtx_unlock(&be_lun->io_lock); 787 788 if (beio->bio_cmd == BIO_READ) { 789 error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 790 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 791 } else { 792 error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 793 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 794 } 795 796 mtx_lock(&be_lun->io_lock); 797 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 798 beio->ds_tag_type, beio->ds_trans_type, 799 /*now*/ NULL, /*then*/&beio->ds_t0); 800 mtx_unlock(&be_lun->io_lock); 801 802 /* 803 * If we got an error, set the sense data to "MEDIUM ERROR" and 804 * return the I/O to the user. 805 */ 806 if (error != 0) { 807 ctl_set_medium_error(&io->scsiio); 808 ctl_complete_beio(beio); 809 return; 810 } 811 812 /* 813 * If this is a write or a verify, we're all done. 814 * If this is a read, we can now send the data to the user. 815 */ 816 if ((beio->bio_cmd == BIO_WRITE) || 817 (ARGS(io)->flags & CTL_LLF_VERIFY)) { 818 ctl_set_success(&io->scsiio); 819 ctl_complete_beio(beio); 820 } else { 821 #ifdef CTL_TIME_IO 822 getbintime(&io->io_hdr.dma_start_bt); 823 #endif 824 ctl_datamove(io); 825 } 826 } 827 828 static void 829 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 830 struct ctl_be_block_io *beio) 831 { 832 struct bio *bio; 833 union ctl_io *io; 834 struct ctl_be_block_devdata *dev_data; 835 836 dev_data = &be_lun->backend.dev; 837 io = beio->io; 838 839 DPRINTF("entered\n"); 840 841 /* This can't fail, it's a blocking allocation. */ 842 bio = g_alloc_bio(); 843 844 bio->bio_cmd = BIO_FLUSH; 845 bio->bio_flags |= BIO_ORDERED; 846 bio->bio_dev = dev_data->cdev; 847 bio->bio_offset = 0; 848 bio->bio_data = 0; 849 bio->bio_done = ctl_be_block_biodone; 850 bio->bio_caller1 = beio; 851 bio->bio_pblkno = 0; 852 853 /* 854 * We don't need to acquire the LUN lock here, because we are only 855 * sending one bio, and so there is no other context to synchronize 856 * with. 857 */ 858 beio->num_bios_sent = 1; 859 beio->send_complete = 1; 860 861 binuptime(&beio->ds_t0); 862 mtx_lock(&be_lun->io_lock); 863 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 864 mtx_unlock(&be_lun->io_lock); 865 866 (*dev_data->csw->d_strategy)(bio); 867 } 868 869 static void 870 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 871 struct ctl_be_block_io *beio, 872 uint64_t off, uint64_t len, int last) 873 { 874 struct bio *bio; 875 struct ctl_be_block_devdata *dev_data; 876 uint64_t maxlen; 877 878 dev_data = &be_lun->backend.dev; 879 maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 880 while (len > 0) { 881 bio = g_alloc_bio(); 882 bio->bio_cmd = BIO_DELETE; 883 bio->bio_dev = dev_data->cdev; 884 bio->bio_offset = off; 885 bio->bio_length = MIN(len, maxlen); 886 bio->bio_data = 0; 887 bio->bio_done = ctl_be_block_biodone; 888 bio->bio_caller1 = beio; 889 bio->bio_pblkno = off / be_lun->blocksize; 890 891 off += bio->bio_length; 892 len -= bio->bio_length; 893 894 mtx_lock(&be_lun->io_lock); 895 beio->num_bios_sent++; 896 if (last && len == 0) 897 beio->send_complete = 1; 898 mtx_unlock(&be_lun->io_lock); 899 900 (*dev_data->csw->d_strategy)(bio); 901 } 902 } 903 904 static void 905 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 906 struct ctl_be_block_io *beio) 907 { 908 union ctl_io *io; 909 struct ctl_be_block_devdata *dev_data; 910 struct ctl_ptr_len_flags *ptrlen; 911 struct scsi_unmap_desc *buf, *end; 912 uint64_t len; 913 914 dev_data = &be_lun->backend.dev; 915 io = beio->io; 916 917 DPRINTF("entered\n"); 918 919 binuptime(&beio->ds_t0); 920 mtx_lock(&be_lun->io_lock); 921 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 922 mtx_unlock(&be_lun->io_lock); 923 924 if (beio->io_offset == -1) { 925 beio->io_len = 0; 926 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 927 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 928 end = buf + ptrlen->len / sizeof(*buf); 929 for (; buf < end; buf++) { 930 len = (uint64_t)scsi_4btoul(buf->length) * 931 be_lun->blocksize; 932 beio->io_len += len; 933 ctl_be_block_unmap_dev_range(be_lun, beio, 934 scsi_8btou64(buf->lba) * be_lun->blocksize, len, 935 (end - buf < 2) ? TRUE : FALSE); 936 } 937 } else 938 ctl_be_block_unmap_dev_range(be_lun, beio, 939 beio->io_offset, beio->io_len, TRUE); 940 } 941 942 static void 943 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 944 struct ctl_be_block_io *beio) 945 { 946 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 947 int i; 948 struct bio *bio; 949 struct ctl_be_block_devdata *dev_data; 950 off_t cur_offset; 951 int max_iosize; 952 953 DPRINTF("entered\n"); 954 955 dev_data = &be_lun->backend.dev; 956 957 /* 958 * We have to limit our I/O size to the maximum supported by the 959 * backend device. Hopefully it is MAXPHYS. If the driver doesn't 960 * set it properly, use DFLTPHYS. 961 */ 962 max_iosize = dev_data->cdev->si_iosize_max; 963 if (max_iosize < PAGE_SIZE) 964 max_iosize = DFLTPHYS; 965 966 cur_offset = beio->io_offset; 967 for (i = 0; i < beio->num_segs; i++) { 968 size_t cur_size; 969 uint8_t *cur_ptr; 970 971 cur_size = beio->sg_segs[i].len; 972 cur_ptr = beio->sg_segs[i].addr; 973 974 while (cur_size > 0) { 975 /* This can't fail, it's a blocking allocation. */ 976 bio = g_alloc_bio(); 977 978 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 979 980 bio->bio_cmd = beio->bio_cmd; 981 bio->bio_dev = dev_data->cdev; 982 bio->bio_caller1 = beio; 983 bio->bio_length = min(cur_size, max_iosize); 984 bio->bio_offset = cur_offset; 985 bio->bio_data = cur_ptr; 986 bio->bio_done = ctl_be_block_biodone; 987 bio->bio_pblkno = cur_offset / be_lun->blocksize; 988 989 cur_offset += bio->bio_length; 990 cur_ptr += bio->bio_length; 991 cur_size -= bio->bio_length; 992 993 TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 994 beio->num_bios_sent++; 995 } 996 } 997 binuptime(&beio->ds_t0); 998 mtx_lock(&be_lun->io_lock); 999 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1000 beio->send_complete = 1; 1001 mtx_unlock(&be_lun->io_lock); 1002 1003 /* 1004 * Fire off all allocated requests! 1005 */ 1006 while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1007 TAILQ_REMOVE(&queue, bio, bio_queue); 1008 (*dev_data->csw->d_strategy)(bio); 1009 } 1010 } 1011 1012 static void 1013 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1014 { 1015 union ctl_io *io; 1016 1017 io = beio->io; 1018 ctl_free_beio(beio); 1019 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1020 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1021 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1022 ctl_config_write_done(io); 1023 return; 1024 } 1025 1026 ctl_be_block_config_write(io); 1027 } 1028 1029 static void 1030 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1031 union ctl_io *io) 1032 { 1033 struct ctl_be_block_io *beio; 1034 struct ctl_be_block_softc *softc; 1035 struct ctl_lba_len_flags *lbalen; 1036 uint64_t len_left, lba; 1037 int i, seglen; 1038 uint8_t *buf, *end; 1039 1040 DPRINTF("entered\n"); 1041 1042 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1043 softc = be_lun->softc; 1044 lbalen = ARGS(beio->io); 1045 1046 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1047 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1048 ctl_free_beio(beio); 1049 ctl_set_invalid_field(&io->scsiio, 1050 /*sks_valid*/ 1, 1051 /*command*/ 1, 1052 /*field*/ 1, 1053 /*bit_valid*/ 0, 1054 /*bit*/ 0); 1055 ctl_config_write_done(io); 1056 return; 1057 } 1058 1059 switch (io->scsiio.tag_type) { 1060 case CTL_TAG_ORDERED: 1061 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1062 break; 1063 case CTL_TAG_HEAD_OF_QUEUE: 1064 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1065 break; 1066 case CTL_TAG_UNTAGGED: 1067 case CTL_TAG_SIMPLE: 1068 case CTL_TAG_ACA: 1069 default: 1070 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1071 break; 1072 } 1073 1074 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1075 beio->io_offset = lbalen->lba * be_lun->blocksize; 1076 beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1077 beio->bio_cmd = BIO_DELETE; 1078 beio->ds_trans_type = DEVSTAT_FREE; 1079 1080 be_lun->unmap(be_lun, beio); 1081 return; 1082 } 1083 1084 beio->bio_cmd = BIO_WRITE; 1085 beio->ds_trans_type = DEVSTAT_WRITE; 1086 1087 DPRINTF("WRITE SAME at LBA %jx len %u\n", 1088 (uintmax_t)lbalen->lba, lbalen->len); 1089 1090 len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1091 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1092 1093 /* 1094 * Setup the S/G entry for this chunk. 1095 */ 1096 seglen = MIN(CTLBLK_MAX_SEG, len_left); 1097 seglen -= seglen % be_lun->blocksize; 1098 beio->sg_segs[i].len = seglen; 1099 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1100 1101 DPRINTF("segment %d addr %p len %zd\n", i, 1102 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1103 1104 beio->num_segs++; 1105 len_left -= seglen; 1106 1107 buf = beio->sg_segs[i].addr; 1108 end = buf + seglen; 1109 for (; buf < end; buf += be_lun->blocksize) { 1110 memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1111 if (lbalen->flags & SWS_LBDATA) 1112 scsi_ulto4b(lbalen->lba + lba, buf); 1113 lba++; 1114 } 1115 } 1116 1117 beio->io_offset = lbalen->lba * be_lun->blocksize; 1118 beio->io_len = lba * be_lun->blocksize; 1119 1120 /* We can not do all in one run. Correct and schedule rerun. */ 1121 if (len_left > 0) { 1122 lbalen->lba += lba; 1123 lbalen->len -= lba; 1124 beio->beio_cont = ctl_be_block_cw_done_ws; 1125 } 1126 1127 be_lun->dispatch(be_lun, beio); 1128 } 1129 1130 static void 1131 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1132 union ctl_io *io) 1133 { 1134 struct ctl_be_block_io *beio; 1135 struct ctl_be_block_softc *softc; 1136 struct ctl_ptr_len_flags *ptrlen; 1137 1138 DPRINTF("entered\n"); 1139 1140 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1141 softc = be_lun->softc; 1142 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1143 1144 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1145 ctl_free_beio(beio); 1146 ctl_set_invalid_field(&io->scsiio, 1147 /*sks_valid*/ 0, 1148 /*command*/ 1, 1149 /*field*/ 0, 1150 /*bit_valid*/ 0, 1151 /*bit*/ 0); 1152 ctl_config_write_done(io); 1153 return; 1154 } 1155 1156 switch (io->scsiio.tag_type) { 1157 case CTL_TAG_ORDERED: 1158 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1159 break; 1160 case CTL_TAG_HEAD_OF_QUEUE: 1161 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1162 break; 1163 case CTL_TAG_UNTAGGED: 1164 case CTL_TAG_SIMPLE: 1165 case CTL_TAG_ACA: 1166 default: 1167 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1168 break; 1169 } 1170 1171 beio->io_len = 0; 1172 beio->io_offset = -1; 1173 1174 beio->bio_cmd = BIO_DELETE; 1175 beio->ds_trans_type = DEVSTAT_FREE; 1176 1177 DPRINTF("UNMAP\n"); 1178 1179 be_lun->unmap(be_lun, beio); 1180 } 1181 1182 static void 1183 ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1184 { 1185 union ctl_io *io; 1186 1187 io = beio->io; 1188 ctl_free_beio(beio); 1189 ctl_config_write_done(io); 1190 } 1191 1192 static void 1193 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1194 union ctl_io *io) 1195 { 1196 struct ctl_be_block_io *beio; 1197 struct ctl_be_block_softc *softc; 1198 1199 DPRINTF("entered\n"); 1200 1201 softc = be_lun->softc; 1202 beio = ctl_alloc_beio(softc); 1203 beio->io = io; 1204 beio->lun = be_lun; 1205 beio->beio_cont = ctl_be_block_cw_done; 1206 PRIV(io)->ptr = (void *)beio; 1207 1208 switch (io->scsiio.cdb[0]) { 1209 case SYNCHRONIZE_CACHE: 1210 case SYNCHRONIZE_CACHE_16: 1211 beio->bio_cmd = BIO_FLUSH; 1212 beio->ds_trans_type = DEVSTAT_NO_DATA; 1213 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1214 beio->io_len = 0; 1215 be_lun->lun_flush(be_lun, beio); 1216 break; 1217 case WRITE_SAME_10: 1218 case WRITE_SAME_16: 1219 ctl_be_block_cw_dispatch_ws(be_lun, io); 1220 break; 1221 case UNMAP: 1222 ctl_be_block_cw_dispatch_unmap(be_lun, io); 1223 break; 1224 default: 1225 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1226 break; 1227 } 1228 } 1229 1230 SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1231 SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1232 SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1233 SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1234 1235 static void 1236 ctl_be_block_next(struct ctl_be_block_io *beio) 1237 { 1238 struct ctl_be_block_lun *be_lun; 1239 union ctl_io *io; 1240 1241 io = beio->io; 1242 be_lun = beio->lun; 1243 ctl_free_beio(beio); 1244 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1245 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1246 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1247 ctl_data_submit_done(io); 1248 return; 1249 } 1250 1251 io->io_hdr.status &= ~CTL_STATUS_MASK; 1252 io->io_hdr.status |= CTL_STATUS_NONE; 1253 1254 mtx_lock(&be_lun->queue_lock); 1255 /* 1256 * XXX KDM make sure that links is okay to use at this point. 1257 * Otherwise, we either need to add another field to ctl_io_hdr, 1258 * or deal with resource allocation here. 1259 */ 1260 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1261 mtx_unlock(&be_lun->queue_lock); 1262 1263 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1264 } 1265 1266 static void 1267 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1268 union ctl_io *io) 1269 { 1270 struct ctl_be_block_io *beio; 1271 struct ctl_be_block_softc *softc; 1272 struct ctl_lba_len_flags *lbalen; 1273 struct ctl_ptr_len_flags *bptrlen; 1274 uint64_t len_left, lbas; 1275 int i; 1276 1277 softc = be_lun->softc; 1278 1279 DPRINTF("entered\n"); 1280 1281 lbalen = ARGS(io); 1282 if (lbalen->flags & CTL_LLF_WRITE) { 1283 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1284 } else { 1285 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1286 } 1287 1288 beio = ctl_alloc_beio(softc); 1289 beio->io = io; 1290 beio->lun = be_lun; 1291 bptrlen = PRIV(io); 1292 bptrlen->ptr = (void *)beio; 1293 1294 switch (io->scsiio.tag_type) { 1295 case CTL_TAG_ORDERED: 1296 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1297 break; 1298 case CTL_TAG_HEAD_OF_QUEUE: 1299 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1300 break; 1301 case CTL_TAG_UNTAGGED: 1302 case CTL_TAG_SIMPLE: 1303 case CTL_TAG_ACA: 1304 default: 1305 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1306 break; 1307 } 1308 1309 if (lbalen->flags & CTL_LLF_WRITE) { 1310 beio->bio_cmd = BIO_WRITE; 1311 beio->ds_trans_type = DEVSTAT_WRITE; 1312 } else { 1313 beio->bio_cmd = BIO_READ; 1314 beio->ds_trans_type = DEVSTAT_READ; 1315 } 1316 1317 DPRINTF("%s at LBA %jx len %u @%ju\n", 1318 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1319 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1320 if (lbalen->flags & CTL_LLF_COMPARE) 1321 lbas = CTLBLK_HALF_IO_SIZE; 1322 else 1323 lbas = CTLBLK_MAX_IO_SIZE; 1324 lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1325 beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1326 beio->io_len = lbas * be_lun->blocksize; 1327 bptrlen->len += lbas; 1328 1329 for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1330 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1331 i, CTLBLK_MAX_SEGS)); 1332 1333 /* 1334 * Setup the S/G entry for this chunk. 1335 */ 1336 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1337 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1338 1339 DPRINTF("segment %d addr %p len %zd\n", i, 1340 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1341 1342 /* Set up second segment for compare operation. */ 1343 if (lbalen->flags & CTL_LLF_COMPARE) { 1344 beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1345 beio->sg_segs[i].len; 1346 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1347 uma_zalloc(be_lun->lun_zone, M_WAITOK); 1348 } 1349 1350 beio->num_segs++; 1351 len_left -= beio->sg_segs[i].len; 1352 } 1353 if (bptrlen->len < lbalen->len) 1354 beio->beio_cont = ctl_be_block_next; 1355 io->scsiio.be_move_done = ctl_be_block_move_done; 1356 /* For compare we have separate S/G lists for read and datamove. */ 1357 if (lbalen->flags & CTL_LLF_COMPARE) 1358 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1359 else 1360 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1361 io->scsiio.kern_data_len = beio->io_len; 1362 io->scsiio.kern_data_resid = 0; 1363 io->scsiio.kern_sg_entries = beio->num_segs; 1364 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1365 1366 /* 1367 * For the read case, we need to read the data into our buffers and 1368 * then we can send it back to the user. For the write case, we 1369 * need to get the data from the user first. 1370 */ 1371 if (beio->bio_cmd == BIO_READ) { 1372 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1373 be_lun->dispatch(be_lun, beio); 1374 } else { 1375 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1376 #ifdef CTL_TIME_IO 1377 getbintime(&io->io_hdr.dma_start_bt); 1378 #endif 1379 ctl_datamove(io); 1380 } 1381 } 1382 1383 static void 1384 ctl_be_block_worker(void *context, int pending) 1385 { 1386 struct ctl_be_block_lun *be_lun; 1387 struct ctl_be_block_softc *softc; 1388 union ctl_io *io; 1389 1390 be_lun = (struct ctl_be_block_lun *)context; 1391 softc = be_lun->softc; 1392 1393 DPRINTF("entered\n"); 1394 1395 mtx_lock(&be_lun->queue_lock); 1396 for (;;) { 1397 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1398 if (io != NULL) { 1399 struct ctl_be_block_io *beio; 1400 1401 DPRINTF("datamove queue\n"); 1402 1403 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1404 ctl_io_hdr, links); 1405 1406 mtx_unlock(&be_lun->queue_lock); 1407 1408 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1409 1410 be_lun->dispatch(be_lun, beio); 1411 1412 mtx_lock(&be_lun->queue_lock); 1413 continue; 1414 } 1415 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1416 if (io != NULL) { 1417 1418 DPRINTF("config write queue\n"); 1419 1420 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1421 ctl_io_hdr, links); 1422 1423 mtx_unlock(&be_lun->queue_lock); 1424 1425 ctl_be_block_cw_dispatch(be_lun, io); 1426 1427 mtx_lock(&be_lun->queue_lock); 1428 continue; 1429 } 1430 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1431 if (io != NULL) { 1432 DPRINTF("input queue\n"); 1433 1434 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1435 ctl_io_hdr, links); 1436 mtx_unlock(&be_lun->queue_lock); 1437 1438 /* 1439 * We must drop the lock, since this routine and 1440 * its children may sleep. 1441 */ 1442 ctl_be_block_dispatch(be_lun, io); 1443 1444 mtx_lock(&be_lun->queue_lock); 1445 continue; 1446 } 1447 1448 /* 1449 * If we get here, there is no work left in the queues, so 1450 * just break out and let the task queue go to sleep. 1451 */ 1452 break; 1453 } 1454 mtx_unlock(&be_lun->queue_lock); 1455 } 1456 1457 /* 1458 * Entry point from CTL to the backend for I/O. We queue everything to a 1459 * work thread, so this just puts the I/O on a queue and wakes up the 1460 * thread. 1461 */ 1462 static int 1463 ctl_be_block_submit(union ctl_io *io) 1464 { 1465 struct ctl_be_block_lun *be_lun; 1466 struct ctl_be_lun *ctl_be_lun; 1467 1468 DPRINTF("entered\n"); 1469 1470 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1471 CTL_PRIV_BACKEND_LUN].ptr; 1472 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1473 1474 /* 1475 * Make sure we only get SCSI I/O. 1476 */ 1477 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1478 "%#x) encountered", io->io_hdr.io_type)); 1479 1480 PRIV(io)->len = 0; 1481 1482 mtx_lock(&be_lun->queue_lock); 1483 /* 1484 * XXX KDM make sure that links is okay to use at this point. 1485 * Otherwise, we either need to add another field to ctl_io_hdr, 1486 * or deal with resource allocation here. 1487 */ 1488 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1489 mtx_unlock(&be_lun->queue_lock); 1490 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1491 1492 return (CTL_RETVAL_COMPLETE); 1493 } 1494 1495 static int 1496 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1497 int flag, struct thread *td) 1498 { 1499 struct ctl_be_block_softc *softc; 1500 int error; 1501 1502 softc = &backend_block_softc; 1503 1504 error = 0; 1505 1506 switch (cmd) { 1507 case CTL_LUN_REQ: { 1508 struct ctl_lun_req *lun_req; 1509 1510 lun_req = (struct ctl_lun_req *)addr; 1511 1512 switch (lun_req->reqtype) { 1513 case CTL_LUNREQ_CREATE: 1514 error = ctl_be_block_create(softc, lun_req); 1515 break; 1516 case CTL_LUNREQ_RM: 1517 error = ctl_be_block_rm(softc, lun_req); 1518 break; 1519 case CTL_LUNREQ_MODIFY: 1520 error = ctl_be_block_modify(softc, lun_req); 1521 break; 1522 default: 1523 lun_req->status = CTL_LUN_ERROR; 1524 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1525 "invalid LUN request type %d", 1526 lun_req->reqtype); 1527 break; 1528 } 1529 break; 1530 } 1531 default: 1532 error = ENOTTY; 1533 break; 1534 } 1535 1536 return (error); 1537 } 1538 1539 static int 1540 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1541 { 1542 struct ctl_be_block_filedata *file_data; 1543 struct ctl_lun_create_params *params; 1544 struct vattr vattr; 1545 int error; 1546 1547 error = 0; 1548 file_data = &be_lun->backend.file; 1549 params = &be_lun->params; 1550 1551 be_lun->dev_type = CTL_BE_BLOCK_FILE; 1552 be_lun->dispatch = ctl_be_block_dispatch_file; 1553 be_lun->lun_flush = ctl_be_block_flush_file; 1554 1555 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1556 if (error != 0) { 1557 snprintf(req->error_str, sizeof(req->error_str), 1558 "error calling VOP_GETATTR() for file %s", 1559 be_lun->dev_path); 1560 return (error); 1561 } 1562 1563 /* 1564 * Verify that we have the ability to upgrade to exclusive 1565 * access on this file so we can trap errors at open instead 1566 * of reporting them during first access. 1567 */ 1568 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1569 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1570 if (be_lun->vn->v_iflag & VI_DOOMED) { 1571 error = EBADF; 1572 snprintf(req->error_str, sizeof(req->error_str), 1573 "error locking file %s", be_lun->dev_path); 1574 return (error); 1575 } 1576 } 1577 1578 1579 file_data->cred = crhold(curthread->td_ucred); 1580 if (params->lun_size_bytes != 0) 1581 be_lun->size_bytes = params->lun_size_bytes; 1582 else 1583 be_lun->size_bytes = vattr.va_size; 1584 /* 1585 * We set the multi thread flag for file operations because all 1586 * filesystems (in theory) are capable of allowing multiple readers 1587 * of a file at once. So we want to get the maximum possible 1588 * concurrency. 1589 */ 1590 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1591 1592 /* 1593 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here. 1594 * With ZFS, it is 131072 bytes. Block sizes that large don't work 1595 * with disklabel and UFS on FreeBSD at least. Large block sizes 1596 * may not work with other OSes as well. So just export a sector 1597 * size of 512 bytes, which should work with any OS or 1598 * application. Since our backing is a file, any block size will 1599 * work fine for the backing store. 1600 */ 1601 #if 0 1602 be_lun->blocksize= vattr.va_blocksize; 1603 #endif 1604 if (params->blocksize_bytes != 0) 1605 be_lun->blocksize = params->blocksize_bytes; 1606 else 1607 be_lun->blocksize = 512; 1608 1609 /* 1610 * Sanity check. The media size has to be at least one 1611 * sector long. 1612 */ 1613 if (be_lun->size_bytes < be_lun->blocksize) { 1614 error = EINVAL; 1615 snprintf(req->error_str, sizeof(req->error_str), 1616 "file %s size %ju < block size %u", be_lun->dev_path, 1617 (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1618 } 1619 return (error); 1620 } 1621 1622 static int 1623 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1624 { 1625 struct ctl_lun_create_params *params; 1626 struct vattr vattr; 1627 struct cdev *dev; 1628 struct cdevsw *devsw; 1629 int error; 1630 off_t ps, pss, po, pos; 1631 1632 params = &be_lun->params; 1633 1634 be_lun->dev_type = CTL_BE_BLOCK_DEV; 1635 be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1636 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1637 &be_lun->backend.dev.dev_ref); 1638 if (be_lun->backend.dev.csw == NULL) 1639 panic("Unable to retrieve device switch"); 1640 if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) 1641 be_lun->dispatch = ctl_be_block_dispatch_zvol; 1642 else 1643 be_lun->dispatch = ctl_be_block_dispatch_dev; 1644 be_lun->lun_flush = ctl_be_block_flush_dev; 1645 be_lun->unmap = ctl_be_block_unmap_dev; 1646 1647 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1648 if (error) { 1649 snprintf(req->error_str, sizeof(req->error_str), 1650 "error getting vnode attributes for device %s", 1651 be_lun->dev_path); 1652 return (error); 1653 } 1654 1655 dev = be_lun->vn->v_rdev; 1656 devsw = dev->si_devsw; 1657 if (!devsw->d_ioctl) { 1658 snprintf(req->error_str, sizeof(req->error_str), 1659 "no d_ioctl for device %s!", 1660 be_lun->dev_path); 1661 return (ENODEV); 1662 } 1663 1664 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1665 (caddr_t)&be_lun->blocksize, FREAD, 1666 curthread); 1667 if (error) { 1668 snprintf(req->error_str, sizeof(req->error_str), 1669 "error %d returned for DIOCGSECTORSIZE ioctl " 1670 "on %s!", error, be_lun->dev_path); 1671 return (error); 1672 } 1673 1674 /* 1675 * If the user has asked for a blocksize that is greater than the 1676 * backing device's blocksize, we can do it only if the blocksize 1677 * the user is asking for is an even multiple of the underlying 1678 * device's blocksize. 1679 */ 1680 if ((params->blocksize_bytes != 0) 1681 && (params->blocksize_bytes > be_lun->blocksize)) { 1682 uint32_t bs_multiple, tmp_blocksize; 1683 1684 bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1685 1686 tmp_blocksize = bs_multiple * be_lun->blocksize; 1687 1688 if (tmp_blocksize == params->blocksize_bytes) { 1689 be_lun->blocksize = params->blocksize_bytes; 1690 } else { 1691 snprintf(req->error_str, sizeof(req->error_str), 1692 "requested blocksize %u is not an even " 1693 "multiple of backing device blocksize %u", 1694 params->blocksize_bytes, 1695 be_lun->blocksize); 1696 return (EINVAL); 1697 1698 } 1699 } else if ((params->blocksize_bytes != 0) 1700 && (params->blocksize_bytes != be_lun->blocksize)) { 1701 snprintf(req->error_str, sizeof(req->error_str), 1702 "requested blocksize %u < backing device " 1703 "blocksize %u", params->blocksize_bytes, 1704 be_lun->blocksize); 1705 return (EINVAL); 1706 } 1707 1708 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1709 (caddr_t)&be_lun->size_bytes, FREAD, 1710 curthread); 1711 if (error) { 1712 snprintf(req->error_str, sizeof(req->error_str), 1713 "error %d returned for DIOCGMEDIASIZE " 1714 " ioctl on %s!", error, 1715 be_lun->dev_path); 1716 return (error); 1717 } 1718 1719 if (params->lun_size_bytes != 0) { 1720 if (params->lun_size_bytes > be_lun->size_bytes) { 1721 snprintf(req->error_str, sizeof(req->error_str), 1722 "requested LUN size %ju > backing device " 1723 "size %ju", 1724 (uintmax_t)params->lun_size_bytes, 1725 (uintmax_t)be_lun->size_bytes); 1726 return (EINVAL); 1727 } 1728 1729 be_lun->size_bytes = params->lun_size_bytes; 1730 } 1731 1732 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1733 (caddr_t)&ps, FREAD, curthread); 1734 if (error) 1735 ps = po = 0; 1736 else { 1737 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1738 (caddr_t)&po, FREAD, curthread); 1739 if (error) 1740 po = 0; 1741 } 1742 pss = ps / be_lun->blocksize; 1743 pos = po / be_lun->blocksize; 1744 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1745 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1746 be_lun->pblockexp = fls(pss) - 1; 1747 be_lun->pblockoff = (pss - pos) % pss; 1748 } 1749 1750 return (0); 1751 } 1752 1753 static int 1754 ctl_be_block_close(struct ctl_be_block_lun *be_lun) 1755 { 1756 DROP_GIANT(); 1757 if (be_lun->vn) { 1758 int flags = FREAD | FWRITE; 1759 1760 switch (be_lun->dev_type) { 1761 case CTL_BE_BLOCK_DEV: 1762 if (be_lun->backend.dev.csw) { 1763 dev_relthread(be_lun->backend.dev.cdev, 1764 be_lun->backend.dev.dev_ref); 1765 be_lun->backend.dev.csw = NULL; 1766 be_lun->backend.dev.cdev = NULL; 1767 } 1768 break; 1769 case CTL_BE_BLOCK_FILE: 1770 break; 1771 case CTL_BE_BLOCK_NONE: 1772 break; 1773 default: 1774 panic("Unexpected backend type."); 1775 break; 1776 } 1777 1778 (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1779 be_lun->vn = NULL; 1780 1781 switch (be_lun->dev_type) { 1782 case CTL_BE_BLOCK_DEV: 1783 break; 1784 case CTL_BE_BLOCK_FILE: 1785 if (be_lun->backend.file.cred != NULL) { 1786 crfree(be_lun->backend.file.cred); 1787 be_lun->backend.file.cred = NULL; 1788 } 1789 break; 1790 case CTL_BE_BLOCK_NONE: 1791 break; 1792 default: 1793 panic("Unexpected backend type."); 1794 break; 1795 } 1796 be_lun->dev_type = CTL_BE_BLOCK_NONE; 1797 } 1798 PICKUP_GIANT(); 1799 1800 return (0); 1801 } 1802 1803 static int 1804 ctl_be_block_open(struct ctl_be_block_softc *softc, 1805 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1806 { 1807 struct nameidata nd; 1808 int flags; 1809 int error; 1810 1811 /* 1812 * XXX KDM allow a read-only option? 1813 */ 1814 flags = FREAD | FWRITE; 1815 error = 0; 1816 1817 if (rootvnode == NULL) { 1818 snprintf(req->error_str, sizeof(req->error_str), 1819 "Root filesystem is not mounted"); 1820 return (1); 1821 } 1822 1823 if (!curthread->td_proc->p_fd->fd_cdir) { 1824 curthread->td_proc->p_fd->fd_cdir = rootvnode; 1825 VREF(rootvnode); 1826 } 1827 if (!curthread->td_proc->p_fd->fd_rdir) { 1828 curthread->td_proc->p_fd->fd_rdir = rootvnode; 1829 VREF(rootvnode); 1830 } 1831 if (!curthread->td_proc->p_fd->fd_jdir) { 1832 curthread->td_proc->p_fd->fd_jdir = rootvnode; 1833 VREF(rootvnode); 1834 } 1835 1836 again: 1837 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1838 error = vn_open(&nd, &flags, 0, NULL); 1839 if (error) { 1840 /* 1841 * This is the only reasonable guess we can make as far as 1842 * path if the user doesn't give us a fully qualified path. 1843 * If they want to specify a file, they need to specify the 1844 * full path. 1845 */ 1846 if (be_lun->dev_path[0] != '/') { 1847 char *dev_path = "/dev/"; 1848 char *dev_name; 1849 1850 /* Try adding device path at beginning of name */ 1851 dev_name = malloc(strlen(be_lun->dev_path) 1852 + strlen(dev_path) + 1, 1853 M_CTLBLK, M_WAITOK); 1854 if (dev_name) { 1855 sprintf(dev_name, "%s%s", dev_path, 1856 be_lun->dev_path); 1857 free(be_lun->dev_path, M_CTLBLK); 1858 be_lun->dev_path = dev_name; 1859 goto again; 1860 } 1861 } 1862 snprintf(req->error_str, sizeof(req->error_str), 1863 "error opening %s: %d", be_lun->dev_path, error); 1864 return (error); 1865 } 1866 1867 NDFREE(&nd, NDF_ONLY_PNBUF); 1868 1869 be_lun->vn = nd.ni_vp; 1870 1871 /* We only support disks and files. */ 1872 if (vn_isdisk(be_lun->vn, &error)) { 1873 error = ctl_be_block_open_dev(be_lun, req); 1874 } else if (be_lun->vn->v_type == VREG) { 1875 error = ctl_be_block_open_file(be_lun, req); 1876 } else { 1877 error = EINVAL; 1878 snprintf(req->error_str, sizeof(req->error_str), 1879 "%s is not a disk or plain file", be_lun->dev_path); 1880 } 1881 VOP_UNLOCK(be_lun->vn, 0); 1882 1883 if (error != 0) { 1884 ctl_be_block_close(be_lun); 1885 return (error); 1886 } 1887 1888 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1889 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1890 1891 return (0); 1892 } 1893 1894 static int 1895 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1896 { 1897 struct ctl_be_block_lun *be_lun; 1898 struct ctl_lun_create_params *params; 1899 char num_thread_str[16]; 1900 char tmpstr[32]; 1901 char *value; 1902 int retval, num_threads, unmap; 1903 int tmp_num_threads; 1904 1905 params = &req->reqdata.create; 1906 retval = 0; 1907 req->status = CTL_LUN_OK; 1908 1909 num_threads = cbb_num_threads; 1910 1911 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1912 1913 be_lun->params = req->reqdata.create; 1914 be_lun->softc = softc; 1915 STAILQ_INIT(&be_lun->input_queue); 1916 STAILQ_INIT(&be_lun->config_write_queue); 1917 STAILQ_INIT(&be_lun->datamove_queue); 1918 sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1919 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 1920 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 1921 ctl_init_opts(&be_lun->ctl_be_lun.options, 1922 req->num_be_args, req->kern_be_args); 1923 1924 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1925 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1926 1927 if (be_lun->lun_zone == NULL) { 1928 snprintf(req->error_str, sizeof(req->error_str), 1929 "error allocating UMA zone"); 1930 goto bailout_error; 1931 } 1932 1933 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1934 be_lun->ctl_be_lun.lun_type = params->device_type; 1935 else 1936 be_lun->ctl_be_lun.lun_type = T_DIRECT; 1937 1938 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1939 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 1940 if (value == NULL) { 1941 snprintf(req->error_str, sizeof(req->error_str), 1942 "no file argument specified"); 1943 goto bailout_error; 1944 } 1945 be_lun->dev_path = strdup(value, M_CTLBLK); 1946 be_lun->blocksize = 512; 1947 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1948 1949 retval = ctl_be_block_open(softc, be_lun, req); 1950 if (retval != 0) { 1951 retval = 0; 1952 req->status = CTL_LUN_WARNING; 1953 } 1954 } else { 1955 /* 1956 * For processor devices, we don't have any size. 1957 */ 1958 be_lun->blocksize = 0; 1959 be_lun->pblockexp = 0; 1960 be_lun->pblockoff = 0; 1961 be_lun->size_blocks = 0; 1962 be_lun->size_bytes = 0; 1963 be_lun->ctl_be_lun.maxlba = 0; 1964 1965 /* 1966 * Default to just 1 thread for processor devices. 1967 */ 1968 num_threads = 1; 1969 } 1970 1971 /* 1972 * XXX This searching loop might be refactored to be combined with 1973 * the loop above, 1974 */ 1975 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 1976 if (value != NULL) { 1977 tmp_num_threads = strtol(value, NULL, 0); 1978 1979 /* 1980 * We don't let the user specify less than one 1981 * thread, but hope he's clueful enough not to 1982 * specify 1000 threads. 1983 */ 1984 if (tmp_num_threads < 1) { 1985 snprintf(req->error_str, sizeof(req->error_str), 1986 "invalid number of threads %s", 1987 num_thread_str); 1988 goto bailout_error; 1989 } 1990 num_threads = tmp_num_threads; 1991 } 1992 unmap = 0; 1993 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 1994 if (value != NULL && strcmp(value, "on") == 0) 1995 unmap = 1; 1996 1997 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 1998 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 1999 if (be_lun->vn == NULL) 2000 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2001 if (unmap) 2002 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2003 be_lun->ctl_be_lun.be_lun = be_lun; 2004 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2005 0 : (be_lun->size_blocks - 1); 2006 be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2007 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2008 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2009 if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2010 be_lun->blocksize != 0) 2011 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2012 be_lun->blocksize; 2013 /* Tell the user the blocksize we ended up using */ 2014 params->lun_size_bytes = be_lun->size_bytes; 2015 params->blocksize_bytes = be_lun->blocksize; 2016 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2017 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2018 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2019 } else 2020 be_lun->ctl_be_lun.req_lun_id = 0; 2021 2022 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2023 be_lun->ctl_be_lun.lun_config_status = 2024 ctl_be_block_lun_config_status; 2025 be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2026 2027 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2028 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2029 softc->num_luns); 2030 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2031 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2032 sizeof(tmpstr))); 2033 2034 /* Tell the user what we used for a serial number */ 2035 strncpy((char *)params->serial_num, tmpstr, 2036 ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 2037 } else { 2038 strncpy((char *)be_lun->ctl_be_lun.serial_num, 2039 params->serial_num, 2040 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2041 sizeof(params->serial_num))); 2042 } 2043 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2044 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2045 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2046 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2047 sizeof(tmpstr))); 2048 2049 /* Tell the user what we used for a device ID */ 2050 strncpy((char *)params->device_id, tmpstr, 2051 ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2052 } else { 2053 strncpy((char *)be_lun->ctl_be_lun.device_id, 2054 params->device_id, 2055 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2056 sizeof(params->device_id))); 2057 } 2058 2059 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2060 2061 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2062 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2063 2064 if (be_lun->io_taskqueue == NULL) { 2065 snprintf(req->error_str, sizeof(req->error_str), 2066 "unable to create taskqueue"); 2067 goto bailout_error; 2068 } 2069 2070 /* 2071 * Note that we start the same number of threads by default for 2072 * both the file case and the block device case. For the file 2073 * case, we need multiple threads to allow concurrency, because the 2074 * vnode interface is designed to be a blocking interface. For the 2075 * block device case, ZFS zvols at least will block the caller's 2076 * context in many instances, and so we need multiple threads to 2077 * overcome that problem. Other block devices don't need as many 2078 * threads, but they shouldn't cause too many problems. 2079 * 2080 * If the user wants to just have a single thread for a block 2081 * device, he can specify that when the LUN is created, or change 2082 * the tunable/sysctl to alter the default number of threads. 2083 */ 2084 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2085 /*num threads*/num_threads, 2086 /*priority*/PWAIT, 2087 /*thread name*/ 2088 "%s taskq", be_lun->lunname); 2089 2090 if (retval != 0) 2091 goto bailout_error; 2092 2093 be_lun->num_threads = num_threads; 2094 2095 mtx_lock(&softc->lock); 2096 softc->num_luns++; 2097 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2098 2099 mtx_unlock(&softc->lock); 2100 2101 retval = ctl_add_lun(&be_lun->ctl_be_lun); 2102 if (retval != 0) { 2103 mtx_lock(&softc->lock); 2104 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2105 links); 2106 softc->num_luns--; 2107 mtx_unlock(&softc->lock); 2108 snprintf(req->error_str, sizeof(req->error_str), 2109 "ctl_add_lun() returned error %d, see dmesg for " 2110 "details", retval); 2111 retval = 0; 2112 goto bailout_error; 2113 } 2114 2115 mtx_lock(&softc->lock); 2116 2117 /* 2118 * Tell the config_status routine that we're waiting so it won't 2119 * clean up the LUN in the event of an error. 2120 */ 2121 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2122 2123 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2124 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2125 if (retval == EINTR) 2126 break; 2127 } 2128 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2129 2130 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2131 snprintf(req->error_str, sizeof(req->error_str), 2132 "LUN configuration error, see dmesg for details"); 2133 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2134 links); 2135 softc->num_luns--; 2136 mtx_unlock(&softc->lock); 2137 goto bailout_error; 2138 } else { 2139 params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2140 } 2141 2142 mtx_unlock(&softc->lock); 2143 2144 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2145 be_lun->blocksize, 2146 DEVSTAT_ALL_SUPPORTED, 2147 be_lun->ctl_be_lun.lun_type 2148 | DEVSTAT_TYPE_IF_OTHER, 2149 DEVSTAT_PRIORITY_OTHER); 2150 2151 return (retval); 2152 2153 bailout_error: 2154 req->status = CTL_LUN_ERROR; 2155 2156 if (be_lun->io_taskqueue != NULL) 2157 taskqueue_free(be_lun->io_taskqueue); 2158 ctl_be_block_close(be_lun); 2159 if (be_lun->dev_path != NULL) 2160 free(be_lun->dev_path, M_CTLBLK); 2161 if (be_lun->lun_zone != NULL) 2162 uma_zdestroy(be_lun->lun_zone); 2163 ctl_free_opts(&be_lun->ctl_be_lun.options); 2164 mtx_destroy(&be_lun->queue_lock); 2165 mtx_destroy(&be_lun->io_lock); 2166 free(be_lun, M_CTLBLK); 2167 2168 return (retval); 2169 } 2170 2171 static int 2172 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2173 { 2174 struct ctl_lun_rm_params *params; 2175 struct ctl_be_block_lun *be_lun; 2176 int retval; 2177 2178 params = &req->reqdata.rm; 2179 2180 mtx_lock(&softc->lock); 2181 2182 be_lun = NULL; 2183 2184 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2185 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2186 break; 2187 } 2188 mtx_unlock(&softc->lock); 2189 2190 if (be_lun == NULL) { 2191 snprintf(req->error_str, sizeof(req->error_str), 2192 "LUN %u is not managed by the block backend", 2193 params->lun_id); 2194 goto bailout_error; 2195 } 2196 2197 retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2198 2199 if (retval != 0) { 2200 snprintf(req->error_str, sizeof(req->error_str), 2201 "error %d returned from ctl_disable_lun() for " 2202 "LUN %d", retval, params->lun_id); 2203 goto bailout_error; 2204 2205 } 2206 2207 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2208 if (retval != 0) { 2209 snprintf(req->error_str, sizeof(req->error_str), 2210 "error %d returned from ctl_invalidate_lun() for " 2211 "LUN %d", retval, params->lun_id); 2212 goto bailout_error; 2213 } 2214 2215 mtx_lock(&softc->lock); 2216 2217 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2218 2219 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2220 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2221 if (retval == EINTR) 2222 break; 2223 } 2224 2225 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2226 2227 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2228 snprintf(req->error_str, sizeof(req->error_str), 2229 "interrupted waiting for LUN to be freed"); 2230 mtx_unlock(&softc->lock); 2231 goto bailout_error; 2232 } 2233 2234 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2235 2236 softc->num_luns--; 2237 mtx_unlock(&softc->lock); 2238 2239 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2240 2241 taskqueue_free(be_lun->io_taskqueue); 2242 2243 ctl_be_block_close(be_lun); 2244 2245 if (be_lun->disk_stats != NULL) 2246 devstat_remove_entry(be_lun->disk_stats); 2247 2248 uma_zdestroy(be_lun->lun_zone); 2249 2250 ctl_free_opts(&be_lun->ctl_be_lun.options); 2251 free(be_lun->dev_path, M_CTLBLK); 2252 mtx_destroy(&be_lun->queue_lock); 2253 mtx_destroy(&be_lun->io_lock); 2254 free(be_lun, M_CTLBLK); 2255 2256 req->status = CTL_LUN_OK; 2257 2258 return (0); 2259 2260 bailout_error: 2261 2262 req->status = CTL_LUN_ERROR; 2263 2264 return (0); 2265 } 2266 2267 static int 2268 ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2269 struct ctl_lun_req *req) 2270 { 2271 struct vattr vattr; 2272 int error; 2273 struct ctl_lun_create_params *params = &be_lun->params; 2274 2275 if (params->lun_size_bytes != 0) { 2276 be_lun->size_bytes = params->lun_size_bytes; 2277 } else { 2278 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2279 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2280 VOP_UNLOCK(be_lun->vn, 0); 2281 if (error != 0) { 2282 snprintf(req->error_str, sizeof(req->error_str), 2283 "error calling VOP_GETATTR() for file %s", 2284 be_lun->dev_path); 2285 return (error); 2286 } 2287 2288 be_lun->size_bytes = vattr.va_size; 2289 } 2290 2291 return (0); 2292 } 2293 2294 static int 2295 ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2296 struct ctl_lun_req *req) 2297 { 2298 struct ctl_be_block_devdata *dev_data; 2299 int error; 2300 struct ctl_lun_create_params *params = &be_lun->params; 2301 uint64_t size_bytes; 2302 2303 dev_data = &be_lun->backend.dev; 2304 if (!dev_data->csw->d_ioctl) { 2305 snprintf(req->error_str, sizeof(req->error_str), 2306 "no d_ioctl for device %s!", be_lun->dev_path); 2307 return (ENODEV); 2308 } 2309 2310 error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2311 (caddr_t)&size_bytes, FREAD, 2312 curthread); 2313 if (error) { 2314 snprintf(req->error_str, sizeof(req->error_str), 2315 "error %d returned for DIOCGMEDIASIZE ioctl " 2316 "on %s!", error, be_lun->dev_path); 2317 return (error); 2318 } 2319 2320 if (params->lun_size_bytes != 0) { 2321 if (params->lun_size_bytes > size_bytes) { 2322 snprintf(req->error_str, sizeof(req->error_str), 2323 "requested LUN size %ju > backing device " 2324 "size %ju", 2325 (uintmax_t)params->lun_size_bytes, 2326 (uintmax_t)size_bytes); 2327 return (EINVAL); 2328 } 2329 2330 be_lun->size_bytes = params->lun_size_bytes; 2331 } else { 2332 be_lun->size_bytes = size_bytes; 2333 } 2334 2335 return (0); 2336 } 2337 2338 static int 2339 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2340 { 2341 struct ctl_lun_modify_params *params; 2342 struct ctl_be_block_lun *be_lun; 2343 uint64_t oldsize; 2344 int error; 2345 2346 params = &req->reqdata.modify; 2347 2348 mtx_lock(&softc->lock); 2349 be_lun = NULL; 2350 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2351 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2352 break; 2353 } 2354 mtx_unlock(&softc->lock); 2355 2356 if (be_lun == NULL) { 2357 snprintf(req->error_str, sizeof(req->error_str), 2358 "LUN %u is not managed by the block backend", 2359 params->lun_id); 2360 goto bailout_error; 2361 } 2362 2363 be_lun->params.lun_size_bytes = params->lun_size_bytes; 2364 2365 oldsize = be_lun->size_blocks; 2366 if (be_lun->vn == NULL) 2367 error = ctl_be_block_open(softc, be_lun, req); 2368 else if (be_lun->vn->v_type == VREG) 2369 error = ctl_be_block_modify_file(be_lun, req); 2370 else 2371 error = ctl_be_block_modify_dev(be_lun, req); 2372 2373 if (error == 0 && be_lun->size_blocks != oldsize) { 2374 be_lun->size_blocks = be_lun->size_bytes >> 2375 be_lun->blocksize_shift; 2376 2377 /* 2378 * The maximum LBA is the size - 1. 2379 * 2380 * XXX: Note that this field is being updated without locking, 2381 * which might cause problems on 32-bit architectures. 2382 */ 2383 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2384 0 : (be_lun->size_blocks - 1); 2385 be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2386 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2387 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2388 if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2389 be_lun->blocksize != 0) 2390 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2391 be_lun->blocksize; 2392 ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2393 if (oldsize == 0 && be_lun->size_blocks != 0) 2394 ctl_lun_online(&be_lun->ctl_be_lun); 2395 } 2396 2397 /* Tell the user the exact size we ended up using */ 2398 params->lun_size_bytes = be_lun->size_bytes; 2399 2400 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2401 2402 return (0); 2403 2404 bailout_error: 2405 req->status = CTL_LUN_ERROR; 2406 2407 return (0); 2408 } 2409 2410 static void 2411 ctl_be_block_lun_shutdown(void *be_lun) 2412 { 2413 struct ctl_be_block_lun *lun; 2414 struct ctl_be_block_softc *softc; 2415 2416 lun = (struct ctl_be_block_lun *)be_lun; 2417 2418 softc = lun->softc; 2419 2420 mtx_lock(&softc->lock); 2421 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2422 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2423 wakeup(lun); 2424 mtx_unlock(&softc->lock); 2425 2426 } 2427 2428 static void 2429 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2430 { 2431 struct ctl_be_block_lun *lun; 2432 struct ctl_be_block_softc *softc; 2433 2434 lun = (struct ctl_be_block_lun *)be_lun; 2435 softc = lun->softc; 2436 2437 if (status == CTL_LUN_CONFIG_OK) { 2438 mtx_lock(&softc->lock); 2439 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2440 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2441 wakeup(lun); 2442 mtx_unlock(&softc->lock); 2443 2444 /* 2445 * We successfully added the LUN, attempt to enable it. 2446 */ 2447 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2448 printf("%s: ctl_enable_lun() failed!\n", __func__); 2449 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2450 printf("%s: ctl_invalidate_lun() failed!\n", 2451 __func__); 2452 } 2453 } 2454 2455 return; 2456 } 2457 2458 2459 mtx_lock(&softc->lock); 2460 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2461 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2462 wakeup(lun); 2463 mtx_unlock(&softc->lock); 2464 } 2465 2466 2467 static int 2468 ctl_be_block_config_write(union ctl_io *io) 2469 { 2470 struct ctl_be_block_lun *be_lun; 2471 struct ctl_be_lun *ctl_be_lun; 2472 int retval; 2473 2474 retval = 0; 2475 2476 DPRINTF("entered\n"); 2477 2478 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2479 CTL_PRIV_BACKEND_LUN].ptr; 2480 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2481 2482 switch (io->scsiio.cdb[0]) { 2483 case SYNCHRONIZE_CACHE: 2484 case SYNCHRONIZE_CACHE_16: 2485 case WRITE_SAME_10: 2486 case WRITE_SAME_16: 2487 case UNMAP: 2488 /* 2489 * The upper level CTL code will filter out any CDBs with 2490 * the immediate bit set and return the proper error. 2491 * 2492 * We don't really need to worry about what LBA range the 2493 * user asked to be synced out. When they issue a sync 2494 * cache command, we'll sync out the whole thing. 2495 */ 2496 mtx_lock(&be_lun->queue_lock); 2497 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2498 links); 2499 mtx_unlock(&be_lun->queue_lock); 2500 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2501 break; 2502 case START_STOP_UNIT: { 2503 struct scsi_start_stop_unit *cdb; 2504 2505 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2506 2507 if (cdb->how & SSS_START) 2508 retval = ctl_start_lun(ctl_be_lun); 2509 else { 2510 retval = ctl_stop_lun(ctl_be_lun); 2511 /* 2512 * XXX KDM Copan-specific offline behavior. 2513 * Figure out a reasonable way to port this? 2514 */ 2515 #ifdef NEEDTOPORT 2516 if ((retval == 0) 2517 && (cdb->byte2 & SSS_ONOFFLINE)) 2518 retval = ctl_lun_offline(ctl_be_lun); 2519 #endif 2520 } 2521 2522 /* 2523 * In general, the above routines should not fail. They 2524 * just set state for the LUN. So we've got something 2525 * pretty wrong here if we can't start or stop the LUN. 2526 */ 2527 if (retval != 0) { 2528 ctl_set_internal_failure(&io->scsiio, 2529 /*sks_valid*/ 1, 2530 /*retry_count*/ 0xf051); 2531 retval = CTL_RETVAL_COMPLETE; 2532 } else { 2533 ctl_set_success(&io->scsiio); 2534 } 2535 ctl_config_write_done(io); 2536 break; 2537 } 2538 default: 2539 ctl_set_invalid_opcode(&io->scsiio); 2540 ctl_config_write_done(io); 2541 retval = CTL_RETVAL_COMPLETE; 2542 break; 2543 } 2544 2545 return (retval); 2546 2547 } 2548 2549 static int 2550 ctl_be_block_config_read(union ctl_io *io) 2551 { 2552 return (0); 2553 } 2554 2555 static int 2556 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2557 { 2558 struct ctl_be_block_lun *lun; 2559 int retval; 2560 2561 lun = (struct ctl_be_block_lun *)be_lun; 2562 retval = 0; 2563 2564 retval = sbuf_printf(sb, "\t<num_threads>"); 2565 2566 if (retval != 0) 2567 goto bailout; 2568 2569 retval = sbuf_printf(sb, "%d", lun->num_threads); 2570 2571 if (retval != 0) 2572 goto bailout; 2573 2574 retval = sbuf_printf(sb, "</num_threads>\n"); 2575 2576 bailout: 2577 2578 return (retval); 2579 } 2580 2581 int 2582 ctl_be_block_init(void) 2583 { 2584 struct ctl_be_block_softc *softc; 2585 int retval; 2586 2587 softc = &backend_block_softc; 2588 retval = 0; 2589 2590 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2591 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2592 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2593 STAILQ_INIT(&softc->disk_list); 2594 STAILQ_INIT(&softc->lun_list); 2595 2596 return (retval); 2597 } 2598