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