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 off_t pss; 1546 int error; 1547 1548 error = 0; 1549 file_data = &be_lun->backend.file; 1550 params = &be_lun->params; 1551 1552 be_lun->dev_type = CTL_BE_BLOCK_FILE; 1553 be_lun->dispatch = ctl_be_block_dispatch_file; 1554 be_lun->lun_flush = ctl_be_block_flush_file; 1555 1556 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1557 if (error != 0) { 1558 snprintf(req->error_str, sizeof(req->error_str), 1559 "error calling VOP_GETATTR() for file %s", 1560 be_lun->dev_path); 1561 return (error); 1562 } 1563 1564 /* 1565 * Verify that we have the ability to upgrade to exclusive 1566 * access on this file so we can trap errors at open instead 1567 * of reporting them during first access. 1568 */ 1569 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1570 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1571 if (be_lun->vn->v_iflag & VI_DOOMED) { 1572 error = EBADF; 1573 snprintf(req->error_str, sizeof(req->error_str), 1574 "error locking file %s", be_lun->dev_path); 1575 return (error); 1576 } 1577 } 1578 1579 1580 file_data->cred = crhold(curthread->td_ucred); 1581 if (params->lun_size_bytes != 0) 1582 be_lun->size_bytes = params->lun_size_bytes; 1583 else 1584 be_lun->size_bytes = vattr.va_size; 1585 /* 1586 * We set the multi thread flag for file operations because all 1587 * filesystems (in theory) are capable of allowing multiple readers 1588 * of a file at once. So we want to get the maximum possible 1589 * concurrency. 1590 */ 1591 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1592 1593 /* 1594 * For files we can use any logical block size. Prefer 512 bytes 1595 * for compatibility reasons. If file's vattr.va_blocksize 1596 * (preferred I/O block size) is bigger and multiple to chosen 1597 * logical block size -- report it as physical block size. 1598 */ 1599 if (params->blocksize_bytes != 0) 1600 be_lun->blocksize = params->blocksize_bytes; 1601 else 1602 be_lun->blocksize = 512; 1603 pss = vattr.va_blocksize / be_lun->blocksize; 1604 if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) && 1605 ((pss & (pss - 1)) == 0)) { 1606 be_lun->pblockexp = fls(pss) - 1; 1607 be_lun->pblockoff = 0; 1608 } 1609 1610 /* 1611 * Sanity check. The media size has to be at least one 1612 * sector long. 1613 */ 1614 if (be_lun->size_bytes < be_lun->blocksize) { 1615 error = EINVAL; 1616 snprintf(req->error_str, sizeof(req->error_str), 1617 "file %s size %ju < block size %u", be_lun->dev_path, 1618 (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1619 } 1620 return (error); 1621 } 1622 1623 static int 1624 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1625 { 1626 struct ctl_lun_create_params *params; 1627 struct vattr vattr; 1628 struct cdev *dev; 1629 struct cdevsw *devsw; 1630 int error; 1631 off_t ps, pss, po, pos; 1632 1633 params = &be_lun->params; 1634 1635 be_lun->dev_type = CTL_BE_BLOCK_DEV; 1636 be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1637 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1638 &be_lun->backend.dev.dev_ref); 1639 if (be_lun->backend.dev.csw == NULL) 1640 panic("Unable to retrieve device switch"); 1641 if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) 1642 be_lun->dispatch = ctl_be_block_dispatch_zvol; 1643 else 1644 be_lun->dispatch = ctl_be_block_dispatch_dev; 1645 be_lun->lun_flush = ctl_be_block_flush_dev; 1646 be_lun->unmap = ctl_be_block_unmap_dev; 1647 1648 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1649 if (error) { 1650 snprintf(req->error_str, sizeof(req->error_str), 1651 "error getting vnode attributes for device %s", 1652 be_lun->dev_path); 1653 return (error); 1654 } 1655 1656 dev = be_lun->vn->v_rdev; 1657 devsw = dev->si_devsw; 1658 if (!devsw->d_ioctl) { 1659 snprintf(req->error_str, sizeof(req->error_str), 1660 "no d_ioctl for device %s!", 1661 be_lun->dev_path); 1662 return (ENODEV); 1663 } 1664 1665 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1666 (caddr_t)&be_lun->blocksize, FREAD, 1667 curthread); 1668 if (error) { 1669 snprintf(req->error_str, sizeof(req->error_str), 1670 "error %d returned for DIOCGSECTORSIZE ioctl " 1671 "on %s!", error, be_lun->dev_path); 1672 return (error); 1673 } 1674 1675 /* 1676 * If the user has asked for a blocksize that is greater than the 1677 * backing device's blocksize, we can do it only if the blocksize 1678 * the user is asking for is an even multiple of the underlying 1679 * device's blocksize. 1680 */ 1681 if ((params->blocksize_bytes != 0) 1682 && (params->blocksize_bytes > be_lun->blocksize)) { 1683 uint32_t bs_multiple, tmp_blocksize; 1684 1685 bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1686 1687 tmp_blocksize = bs_multiple * be_lun->blocksize; 1688 1689 if (tmp_blocksize == params->blocksize_bytes) { 1690 be_lun->blocksize = params->blocksize_bytes; 1691 } else { 1692 snprintf(req->error_str, sizeof(req->error_str), 1693 "requested blocksize %u is not an even " 1694 "multiple of backing device blocksize %u", 1695 params->blocksize_bytes, 1696 be_lun->blocksize); 1697 return (EINVAL); 1698 1699 } 1700 } else if ((params->blocksize_bytes != 0) 1701 && (params->blocksize_bytes != be_lun->blocksize)) { 1702 snprintf(req->error_str, sizeof(req->error_str), 1703 "requested blocksize %u < backing device " 1704 "blocksize %u", params->blocksize_bytes, 1705 be_lun->blocksize); 1706 return (EINVAL); 1707 } 1708 1709 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1710 (caddr_t)&be_lun->size_bytes, FREAD, 1711 curthread); 1712 if (error) { 1713 snprintf(req->error_str, sizeof(req->error_str), 1714 "error %d returned for DIOCGMEDIASIZE " 1715 " ioctl on %s!", error, 1716 be_lun->dev_path); 1717 return (error); 1718 } 1719 1720 if (params->lun_size_bytes != 0) { 1721 if (params->lun_size_bytes > be_lun->size_bytes) { 1722 snprintf(req->error_str, sizeof(req->error_str), 1723 "requested LUN size %ju > backing device " 1724 "size %ju", 1725 (uintmax_t)params->lun_size_bytes, 1726 (uintmax_t)be_lun->size_bytes); 1727 return (EINVAL); 1728 } 1729 1730 be_lun->size_bytes = params->lun_size_bytes; 1731 } 1732 1733 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1734 (caddr_t)&ps, FREAD, curthread); 1735 if (error) 1736 ps = po = 0; 1737 else { 1738 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1739 (caddr_t)&po, FREAD, curthread); 1740 if (error) 1741 po = 0; 1742 } 1743 pss = ps / be_lun->blocksize; 1744 pos = po / be_lun->blocksize; 1745 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1746 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1747 be_lun->pblockexp = fls(pss) - 1; 1748 be_lun->pblockoff = (pss - pos) % pss; 1749 } 1750 1751 return (0); 1752 } 1753 1754 static int 1755 ctl_be_block_close(struct ctl_be_block_lun *be_lun) 1756 { 1757 DROP_GIANT(); 1758 if (be_lun->vn) { 1759 int flags = FREAD | FWRITE; 1760 1761 switch (be_lun->dev_type) { 1762 case CTL_BE_BLOCK_DEV: 1763 if (be_lun->backend.dev.csw) { 1764 dev_relthread(be_lun->backend.dev.cdev, 1765 be_lun->backend.dev.dev_ref); 1766 be_lun->backend.dev.csw = NULL; 1767 be_lun->backend.dev.cdev = NULL; 1768 } 1769 break; 1770 case CTL_BE_BLOCK_FILE: 1771 break; 1772 case CTL_BE_BLOCK_NONE: 1773 break; 1774 default: 1775 panic("Unexpected backend type."); 1776 break; 1777 } 1778 1779 (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1780 be_lun->vn = NULL; 1781 1782 switch (be_lun->dev_type) { 1783 case CTL_BE_BLOCK_DEV: 1784 break; 1785 case CTL_BE_BLOCK_FILE: 1786 if (be_lun->backend.file.cred != NULL) { 1787 crfree(be_lun->backend.file.cred); 1788 be_lun->backend.file.cred = NULL; 1789 } 1790 break; 1791 case CTL_BE_BLOCK_NONE: 1792 break; 1793 default: 1794 panic("Unexpected backend type."); 1795 break; 1796 } 1797 be_lun->dev_type = CTL_BE_BLOCK_NONE; 1798 } 1799 PICKUP_GIANT(); 1800 1801 return (0); 1802 } 1803 1804 static int 1805 ctl_be_block_open(struct ctl_be_block_softc *softc, 1806 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1807 { 1808 struct nameidata nd; 1809 int flags; 1810 int error; 1811 1812 /* 1813 * XXX KDM allow a read-only option? 1814 */ 1815 flags = FREAD | FWRITE; 1816 error = 0; 1817 1818 if (rootvnode == NULL) { 1819 snprintf(req->error_str, sizeof(req->error_str), 1820 "Root filesystem is not mounted"); 1821 return (1); 1822 } 1823 1824 if (!curthread->td_proc->p_fd->fd_cdir) { 1825 curthread->td_proc->p_fd->fd_cdir = rootvnode; 1826 VREF(rootvnode); 1827 } 1828 if (!curthread->td_proc->p_fd->fd_rdir) { 1829 curthread->td_proc->p_fd->fd_rdir = rootvnode; 1830 VREF(rootvnode); 1831 } 1832 if (!curthread->td_proc->p_fd->fd_jdir) { 1833 curthread->td_proc->p_fd->fd_jdir = rootvnode; 1834 VREF(rootvnode); 1835 } 1836 1837 again: 1838 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1839 error = vn_open(&nd, &flags, 0, NULL); 1840 if (error) { 1841 /* 1842 * This is the only reasonable guess we can make as far as 1843 * path if the user doesn't give us a fully qualified path. 1844 * If they want to specify a file, they need to specify the 1845 * full path. 1846 */ 1847 if (be_lun->dev_path[0] != '/') { 1848 char *dev_path = "/dev/"; 1849 char *dev_name; 1850 1851 /* Try adding device path at beginning of name */ 1852 dev_name = malloc(strlen(be_lun->dev_path) 1853 + strlen(dev_path) + 1, 1854 M_CTLBLK, M_WAITOK); 1855 if (dev_name) { 1856 sprintf(dev_name, "%s%s", dev_path, 1857 be_lun->dev_path); 1858 free(be_lun->dev_path, M_CTLBLK); 1859 be_lun->dev_path = dev_name; 1860 goto again; 1861 } 1862 } 1863 snprintf(req->error_str, sizeof(req->error_str), 1864 "error opening %s: %d", be_lun->dev_path, error); 1865 return (error); 1866 } 1867 1868 NDFREE(&nd, NDF_ONLY_PNBUF); 1869 1870 be_lun->vn = nd.ni_vp; 1871 1872 /* We only support disks and files. */ 1873 if (vn_isdisk(be_lun->vn, &error)) { 1874 error = ctl_be_block_open_dev(be_lun, req); 1875 } else if (be_lun->vn->v_type == VREG) { 1876 error = ctl_be_block_open_file(be_lun, req); 1877 } else { 1878 error = EINVAL; 1879 snprintf(req->error_str, sizeof(req->error_str), 1880 "%s is not a disk or plain file", be_lun->dev_path); 1881 } 1882 VOP_UNLOCK(be_lun->vn, 0); 1883 1884 if (error != 0) { 1885 ctl_be_block_close(be_lun); 1886 return (error); 1887 } 1888 1889 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1890 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1891 1892 return (0); 1893 } 1894 1895 static int 1896 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1897 { 1898 struct ctl_be_block_lun *be_lun; 1899 struct ctl_lun_create_params *params; 1900 char num_thread_str[16]; 1901 char tmpstr[32]; 1902 char *value; 1903 int retval, num_threads, unmap; 1904 int tmp_num_threads; 1905 1906 params = &req->reqdata.create; 1907 retval = 0; 1908 req->status = CTL_LUN_OK; 1909 1910 num_threads = cbb_num_threads; 1911 1912 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1913 1914 be_lun->params = req->reqdata.create; 1915 be_lun->softc = softc; 1916 STAILQ_INIT(&be_lun->input_queue); 1917 STAILQ_INIT(&be_lun->config_write_queue); 1918 STAILQ_INIT(&be_lun->datamove_queue); 1919 sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1920 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 1921 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 1922 ctl_init_opts(&be_lun->ctl_be_lun.options, 1923 req->num_be_args, req->kern_be_args); 1924 1925 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1926 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1927 1928 if (be_lun->lun_zone == NULL) { 1929 snprintf(req->error_str, sizeof(req->error_str), 1930 "error allocating UMA zone"); 1931 goto bailout_error; 1932 } 1933 1934 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1935 be_lun->ctl_be_lun.lun_type = params->device_type; 1936 else 1937 be_lun->ctl_be_lun.lun_type = T_DIRECT; 1938 1939 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1940 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 1941 if (value == NULL) { 1942 snprintf(req->error_str, sizeof(req->error_str), 1943 "no file argument specified"); 1944 goto bailout_error; 1945 } 1946 be_lun->dev_path = strdup(value, M_CTLBLK); 1947 be_lun->blocksize = 512; 1948 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1949 1950 retval = ctl_be_block_open(softc, be_lun, req); 1951 if (retval != 0) { 1952 retval = 0; 1953 req->status = CTL_LUN_WARNING; 1954 } 1955 } else { 1956 /* 1957 * For processor devices, we don't have any size. 1958 */ 1959 be_lun->blocksize = 0; 1960 be_lun->pblockexp = 0; 1961 be_lun->pblockoff = 0; 1962 be_lun->size_blocks = 0; 1963 be_lun->size_bytes = 0; 1964 be_lun->ctl_be_lun.maxlba = 0; 1965 1966 /* 1967 * Default to just 1 thread for processor devices. 1968 */ 1969 num_threads = 1; 1970 } 1971 1972 /* 1973 * XXX This searching loop might be refactored to be combined with 1974 * the loop above, 1975 */ 1976 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 1977 if (value != NULL) { 1978 tmp_num_threads = strtol(value, NULL, 0); 1979 1980 /* 1981 * We don't let the user specify less than one 1982 * thread, but hope he's clueful enough not to 1983 * specify 1000 threads. 1984 */ 1985 if (tmp_num_threads < 1) { 1986 snprintf(req->error_str, sizeof(req->error_str), 1987 "invalid number of threads %s", 1988 num_thread_str); 1989 goto bailout_error; 1990 } 1991 num_threads = tmp_num_threads; 1992 } 1993 unmap = 0; 1994 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 1995 if (value != NULL && strcmp(value, "on") == 0) 1996 unmap = 1; 1997 1998 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 1999 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 2000 if (be_lun->vn == NULL) 2001 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2002 if (unmap) 2003 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2004 be_lun->ctl_be_lun.be_lun = be_lun; 2005 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2006 0 : (be_lun->size_blocks - 1); 2007 be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2008 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2009 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2010 if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2011 be_lun->blocksize != 0) 2012 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2013 be_lun->blocksize; 2014 /* Tell the user the blocksize we ended up using */ 2015 params->lun_size_bytes = be_lun->size_bytes; 2016 params->blocksize_bytes = be_lun->blocksize; 2017 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2018 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2019 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2020 } else 2021 be_lun->ctl_be_lun.req_lun_id = 0; 2022 2023 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2024 be_lun->ctl_be_lun.lun_config_status = 2025 ctl_be_block_lun_config_status; 2026 be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2027 2028 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2029 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2030 softc->num_luns); 2031 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2032 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2033 sizeof(tmpstr))); 2034 2035 /* Tell the user what we used for a serial number */ 2036 strncpy((char *)params->serial_num, tmpstr, 2037 ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 2038 } else { 2039 strncpy((char *)be_lun->ctl_be_lun.serial_num, 2040 params->serial_num, 2041 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2042 sizeof(params->serial_num))); 2043 } 2044 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2045 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2046 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2047 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2048 sizeof(tmpstr))); 2049 2050 /* Tell the user what we used for a device ID */ 2051 strncpy((char *)params->device_id, tmpstr, 2052 ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2053 } else { 2054 strncpy((char *)be_lun->ctl_be_lun.device_id, 2055 params->device_id, 2056 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2057 sizeof(params->device_id))); 2058 } 2059 2060 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2061 2062 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2063 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2064 2065 if (be_lun->io_taskqueue == NULL) { 2066 snprintf(req->error_str, sizeof(req->error_str), 2067 "unable to create taskqueue"); 2068 goto bailout_error; 2069 } 2070 2071 /* 2072 * Note that we start the same number of threads by default for 2073 * both the file case and the block device case. For the file 2074 * case, we need multiple threads to allow concurrency, because the 2075 * vnode interface is designed to be a blocking interface. For the 2076 * block device case, ZFS zvols at least will block the caller's 2077 * context in many instances, and so we need multiple threads to 2078 * overcome that problem. Other block devices don't need as many 2079 * threads, but they shouldn't cause too many problems. 2080 * 2081 * If the user wants to just have a single thread for a block 2082 * device, he can specify that when the LUN is created, or change 2083 * the tunable/sysctl to alter the default number of threads. 2084 */ 2085 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2086 /*num threads*/num_threads, 2087 /*priority*/PWAIT, 2088 /*thread name*/ 2089 "%s taskq", be_lun->lunname); 2090 2091 if (retval != 0) 2092 goto bailout_error; 2093 2094 be_lun->num_threads = num_threads; 2095 2096 mtx_lock(&softc->lock); 2097 softc->num_luns++; 2098 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2099 2100 mtx_unlock(&softc->lock); 2101 2102 retval = ctl_add_lun(&be_lun->ctl_be_lun); 2103 if (retval != 0) { 2104 mtx_lock(&softc->lock); 2105 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2106 links); 2107 softc->num_luns--; 2108 mtx_unlock(&softc->lock); 2109 snprintf(req->error_str, sizeof(req->error_str), 2110 "ctl_add_lun() returned error %d, see dmesg for " 2111 "details", retval); 2112 retval = 0; 2113 goto bailout_error; 2114 } 2115 2116 mtx_lock(&softc->lock); 2117 2118 /* 2119 * Tell the config_status routine that we're waiting so it won't 2120 * clean up the LUN in the event of an error. 2121 */ 2122 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2123 2124 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2125 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2126 if (retval == EINTR) 2127 break; 2128 } 2129 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2130 2131 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2132 snprintf(req->error_str, sizeof(req->error_str), 2133 "LUN configuration error, see dmesg for details"); 2134 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2135 links); 2136 softc->num_luns--; 2137 mtx_unlock(&softc->lock); 2138 goto bailout_error; 2139 } else { 2140 params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2141 } 2142 2143 mtx_unlock(&softc->lock); 2144 2145 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2146 be_lun->blocksize, 2147 DEVSTAT_ALL_SUPPORTED, 2148 be_lun->ctl_be_lun.lun_type 2149 | DEVSTAT_TYPE_IF_OTHER, 2150 DEVSTAT_PRIORITY_OTHER); 2151 2152 return (retval); 2153 2154 bailout_error: 2155 req->status = CTL_LUN_ERROR; 2156 2157 if (be_lun->io_taskqueue != NULL) 2158 taskqueue_free(be_lun->io_taskqueue); 2159 ctl_be_block_close(be_lun); 2160 if (be_lun->dev_path != NULL) 2161 free(be_lun->dev_path, M_CTLBLK); 2162 if (be_lun->lun_zone != NULL) 2163 uma_zdestroy(be_lun->lun_zone); 2164 ctl_free_opts(&be_lun->ctl_be_lun.options); 2165 mtx_destroy(&be_lun->queue_lock); 2166 mtx_destroy(&be_lun->io_lock); 2167 free(be_lun, M_CTLBLK); 2168 2169 return (retval); 2170 } 2171 2172 static int 2173 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2174 { 2175 struct ctl_lun_rm_params *params; 2176 struct ctl_be_block_lun *be_lun; 2177 int retval; 2178 2179 params = &req->reqdata.rm; 2180 2181 mtx_lock(&softc->lock); 2182 2183 be_lun = NULL; 2184 2185 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2186 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2187 break; 2188 } 2189 mtx_unlock(&softc->lock); 2190 2191 if (be_lun == NULL) { 2192 snprintf(req->error_str, sizeof(req->error_str), 2193 "LUN %u is not managed by the block backend", 2194 params->lun_id); 2195 goto bailout_error; 2196 } 2197 2198 retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2199 2200 if (retval != 0) { 2201 snprintf(req->error_str, sizeof(req->error_str), 2202 "error %d returned from ctl_disable_lun() for " 2203 "LUN %d", retval, params->lun_id); 2204 goto bailout_error; 2205 2206 } 2207 2208 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2209 if (retval != 0) { 2210 snprintf(req->error_str, sizeof(req->error_str), 2211 "error %d returned from ctl_invalidate_lun() for " 2212 "LUN %d", retval, params->lun_id); 2213 goto bailout_error; 2214 } 2215 2216 mtx_lock(&softc->lock); 2217 2218 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2219 2220 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2221 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2222 if (retval == EINTR) 2223 break; 2224 } 2225 2226 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2227 2228 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2229 snprintf(req->error_str, sizeof(req->error_str), 2230 "interrupted waiting for LUN to be freed"); 2231 mtx_unlock(&softc->lock); 2232 goto bailout_error; 2233 } 2234 2235 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2236 2237 softc->num_luns--; 2238 mtx_unlock(&softc->lock); 2239 2240 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2241 2242 taskqueue_free(be_lun->io_taskqueue); 2243 2244 ctl_be_block_close(be_lun); 2245 2246 if (be_lun->disk_stats != NULL) 2247 devstat_remove_entry(be_lun->disk_stats); 2248 2249 uma_zdestroy(be_lun->lun_zone); 2250 2251 ctl_free_opts(&be_lun->ctl_be_lun.options); 2252 free(be_lun->dev_path, M_CTLBLK); 2253 mtx_destroy(&be_lun->queue_lock); 2254 mtx_destroy(&be_lun->io_lock); 2255 free(be_lun, M_CTLBLK); 2256 2257 req->status = CTL_LUN_OK; 2258 2259 return (0); 2260 2261 bailout_error: 2262 2263 req->status = CTL_LUN_ERROR; 2264 2265 return (0); 2266 } 2267 2268 static int 2269 ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2270 struct ctl_lun_req *req) 2271 { 2272 struct vattr vattr; 2273 int error; 2274 struct ctl_lun_create_params *params = &be_lun->params; 2275 2276 if (params->lun_size_bytes != 0) { 2277 be_lun->size_bytes = params->lun_size_bytes; 2278 } else { 2279 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2280 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2281 VOP_UNLOCK(be_lun->vn, 0); 2282 if (error != 0) { 2283 snprintf(req->error_str, sizeof(req->error_str), 2284 "error calling VOP_GETATTR() for file %s", 2285 be_lun->dev_path); 2286 return (error); 2287 } 2288 2289 be_lun->size_bytes = vattr.va_size; 2290 } 2291 2292 return (0); 2293 } 2294 2295 static int 2296 ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2297 struct ctl_lun_req *req) 2298 { 2299 struct ctl_be_block_devdata *dev_data; 2300 int error; 2301 struct ctl_lun_create_params *params = &be_lun->params; 2302 uint64_t size_bytes; 2303 2304 dev_data = &be_lun->backend.dev; 2305 if (!dev_data->csw->d_ioctl) { 2306 snprintf(req->error_str, sizeof(req->error_str), 2307 "no d_ioctl for device %s!", be_lun->dev_path); 2308 return (ENODEV); 2309 } 2310 2311 error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2312 (caddr_t)&size_bytes, FREAD, 2313 curthread); 2314 if (error) { 2315 snprintf(req->error_str, sizeof(req->error_str), 2316 "error %d returned for DIOCGMEDIASIZE ioctl " 2317 "on %s!", error, be_lun->dev_path); 2318 return (error); 2319 } 2320 2321 if (params->lun_size_bytes != 0) { 2322 if (params->lun_size_bytes > size_bytes) { 2323 snprintf(req->error_str, sizeof(req->error_str), 2324 "requested LUN size %ju > backing device " 2325 "size %ju", 2326 (uintmax_t)params->lun_size_bytes, 2327 (uintmax_t)size_bytes); 2328 return (EINVAL); 2329 } 2330 2331 be_lun->size_bytes = params->lun_size_bytes; 2332 } else { 2333 be_lun->size_bytes = size_bytes; 2334 } 2335 2336 return (0); 2337 } 2338 2339 static int 2340 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2341 { 2342 struct ctl_lun_modify_params *params; 2343 struct ctl_be_block_lun *be_lun; 2344 uint64_t oldsize; 2345 int error; 2346 2347 params = &req->reqdata.modify; 2348 2349 mtx_lock(&softc->lock); 2350 be_lun = NULL; 2351 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2352 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2353 break; 2354 } 2355 mtx_unlock(&softc->lock); 2356 2357 if (be_lun == NULL) { 2358 snprintf(req->error_str, sizeof(req->error_str), 2359 "LUN %u is not managed by the block backend", 2360 params->lun_id); 2361 goto bailout_error; 2362 } 2363 2364 be_lun->params.lun_size_bytes = params->lun_size_bytes; 2365 2366 oldsize = be_lun->size_blocks; 2367 if (be_lun->vn == NULL) 2368 error = ctl_be_block_open(softc, be_lun, req); 2369 else if (be_lun->vn->v_type == VREG) 2370 error = ctl_be_block_modify_file(be_lun, req); 2371 else 2372 error = ctl_be_block_modify_dev(be_lun, req); 2373 2374 if (error == 0 && be_lun->size_blocks != oldsize) { 2375 be_lun->size_blocks = be_lun->size_bytes >> 2376 be_lun->blocksize_shift; 2377 2378 /* 2379 * The maximum LBA is the size - 1. 2380 * 2381 * XXX: Note that this field is being updated without locking, 2382 * which might cause problems on 32-bit architectures. 2383 */ 2384 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2385 0 : (be_lun->size_blocks - 1); 2386 be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2387 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2388 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2389 if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2390 be_lun->blocksize != 0) 2391 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2392 be_lun->blocksize; 2393 ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2394 if (oldsize == 0 && be_lun->size_blocks != 0) 2395 ctl_lun_online(&be_lun->ctl_be_lun); 2396 } 2397 2398 /* Tell the user the exact size we ended up using */ 2399 params->lun_size_bytes = be_lun->size_bytes; 2400 2401 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2402 2403 return (0); 2404 2405 bailout_error: 2406 req->status = CTL_LUN_ERROR; 2407 2408 return (0); 2409 } 2410 2411 static void 2412 ctl_be_block_lun_shutdown(void *be_lun) 2413 { 2414 struct ctl_be_block_lun *lun; 2415 struct ctl_be_block_softc *softc; 2416 2417 lun = (struct ctl_be_block_lun *)be_lun; 2418 2419 softc = lun->softc; 2420 2421 mtx_lock(&softc->lock); 2422 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2423 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2424 wakeup(lun); 2425 mtx_unlock(&softc->lock); 2426 2427 } 2428 2429 static void 2430 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2431 { 2432 struct ctl_be_block_lun *lun; 2433 struct ctl_be_block_softc *softc; 2434 2435 lun = (struct ctl_be_block_lun *)be_lun; 2436 softc = lun->softc; 2437 2438 if (status == CTL_LUN_CONFIG_OK) { 2439 mtx_lock(&softc->lock); 2440 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2441 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2442 wakeup(lun); 2443 mtx_unlock(&softc->lock); 2444 2445 /* 2446 * We successfully added the LUN, attempt to enable it. 2447 */ 2448 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2449 printf("%s: ctl_enable_lun() failed!\n", __func__); 2450 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2451 printf("%s: ctl_invalidate_lun() failed!\n", 2452 __func__); 2453 } 2454 } 2455 2456 return; 2457 } 2458 2459 2460 mtx_lock(&softc->lock); 2461 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2462 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2463 wakeup(lun); 2464 mtx_unlock(&softc->lock); 2465 } 2466 2467 2468 static int 2469 ctl_be_block_config_write(union ctl_io *io) 2470 { 2471 struct ctl_be_block_lun *be_lun; 2472 struct ctl_be_lun *ctl_be_lun; 2473 int retval; 2474 2475 retval = 0; 2476 2477 DPRINTF("entered\n"); 2478 2479 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2480 CTL_PRIV_BACKEND_LUN].ptr; 2481 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2482 2483 switch (io->scsiio.cdb[0]) { 2484 case SYNCHRONIZE_CACHE: 2485 case SYNCHRONIZE_CACHE_16: 2486 case WRITE_SAME_10: 2487 case WRITE_SAME_16: 2488 case UNMAP: 2489 /* 2490 * The upper level CTL code will filter out any CDBs with 2491 * the immediate bit set and return the proper error. 2492 * 2493 * We don't really need to worry about what LBA range the 2494 * user asked to be synced out. When they issue a sync 2495 * cache command, we'll sync out the whole thing. 2496 */ 2497 mtx_lock(&be_lun->queue_lock); 2498 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2499 links); 2500 mtx_unlock(&be_lun->queue_lock); 2501 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2502 break; 2503 case START_STOP_UNIT: { 2504 struct scsi_start_stop_unit *cdb; 2505 2506 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2507 2508 if (cdb->how & SSS_START) 2509 retval = ctl_start_lun(ctl_be_lun); 2510 else { 2511 retval = ctl_stop_lun(ctl_be_lun); 2512 /* 2513 * XXX KDM Copan-specific offline behavior. 2514 * Figure out a reasonable way to port this? 2515 */ 2516 #ifdef NEEDTOPORT 2517 if ((retval == 0) 2518 && (cdb->byte2 & SSS_ONOFFLINE)) 2519 retval = ctl_lun_offline(ctl_be_lun); 2520 #endif 2521 } 2522 2523 /* 2524 * In general, the above routines should not fail. They 2525 * just set state for the LUN. So we've got something 2526 * pretty wrong here if we can't start or stop the LUN. 2527 */ 2528 if (retval != 0) { 2529 ctl_set_internal_failure(&io->scsiio, 2530 /*sks_valid*/ 1, 2531 /*retry_count*/ 0xf051); 2532 retval = CTL_RETVAL_COMPLETE; 2533 } else { 2534 ctl_set_success(&io->scsiio); 2535 } 2536 ctl_config_write_done(io); 2537 break; 2538 } 2539 default: 2540 ctl_set_invalid_opcode(&io->scsiio); 2541 ctl_config_write_done(io); 2542 retval = CTL_RETVAL_COMPLETE; 2543 break; 2544 } 2545 2546 return (retval); 2547 2548 } 2549 2550 static int 2551 ctl_be_block_config_read(union ctl_io *io) 2552 { 2553 return (0); 2554 } 2555 2556 static int 2557 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2558 { 2559 struct ctl_be_block_lun *lun; 2560 int retval; 2561 2562 lun = (struct ctl_be_block_lun *)be_lun; 2563 retval = 0; 2564 2565 retval = sbuf_printf(sb, "\t<num_threads>"); 2566 2567 if (retval != 0) 2568 goto bailout; 2569 2570 retval = sbuf_printf(sb, "%d", lun->num_threads); 2571 2572 if (retval != 0) 2573 goto bailout; 2574 2575 retval = sbuf_printf(sb, "</num_threads>\n"); 2576 2577 bailout: 2578 2579 return (retval); 2580 } 2581 2582 int 2583 ctl_be_block_init(void) 2584 { 2585 struct ctl_be_block_softc *softc; 2586 int retval; 2587 2588 softc = &backend_block_softc; 2589 retval = 0; 2590 2591 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2592 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2593 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2594 STAILQ_INIT(&softc->disk_list); 2595 STAILQ_INIT(&softc->lun_list); 2596 2597 return (retval); 2598 } 2599