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