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 84 #include <geom/geom.h> 85 86 #include <cam/cam.h> 87 #include <cam/scsi/scsi_all.h> 88 #include <cam/scsi/scsi_da.h> 89 #include <cam/ctl/ctl_io.h> 90 #include <cam/ctl/ctl.h> 91 #include <cam/ctl/ctl_backend.h> 92 #include <cam/ctl/ctl_ioctl.h> 93 #include <cam/ctl/ctl_ha.h> 94 #include <cam/ctl/ctl_scsi_all.h> 95 #include <cam/ctl/ctl_private.h> 96 #include <cam/ctl/ctl_error.h> 97 98 /* 99 * The idea here is that we'll allocate enough S/G space to hold a 1MB 100 * I/O. If we get an I/O larger than that, we'll split it. 101 */ 102 #define CTLBLK_HALF_IO_SIZE (512 * 1024) 103 #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 104 #define CTLBLK_MAX_SEG MAXPHYS 105 #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 106 #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 107 108 #ifdef CTLBLK_DEBUG 109 #define DPRINTF(fmt, args...) \ 110 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 111 #else 112 #define DPRINTF(fmt, args...) do {} while(0) 113 #endif 114 115 #define PRIV(io) \ 116 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 117 #define ARGS(io) \ 118 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 119 120 SDT_PROVIDER_DEFINE(cbb); 121 122 typedef enum { 123 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 124 CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 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_lun_create_params params; 156 char lunname[32]; 157 char *dev_path; 158 ctl_be_block_type dev_type; 159 struct vnode *vn; 160 union ctl_be_block_bedata backend; 161 cbb_dispatch_t dispatch; 162 cbb_dispatch_t lun_flush; 163 cbb_dispatch_t unmap; 164 cbb_dispatch_t get_lba_status; 165 cbb_getattr_t getattr; 166 uma_zone_t lun_zone; 167 uint64_t size_blocks; 168 uint64_t size_bytes; 169 struct ctl_be_block_softc *softc; 170 struct devstat *disk_stats; 171 ctl_be_block_lun_flags flags; 172 STAILQ_ENTRY(ctl_be_block_lun) links; 173 struct ctl_be_lun cbe_lun; 174 struct taskqueue *io_taskqueue; 175 struct task io_task; 176 int num_threads; 177 STAILQ_HEAD(, ctl_io_hdr) input_queue; 178 STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 179 STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 180 STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 181 struct mtx_padalign io_lock; 182 struct mtx_padalign queue_lock; 183 }; 184 185 /* 186 * Overall softc structure for the block backend module. 187 */ 188 struct ctl_be_block_softc { 189 struct mtx lock; 190 uma_zone_t beio_zone; 191 int num_luns; 192 STAILQ_HEAD(, ctl_be_block_lun) lun_list; 193 }; 194 195 static struct ctl_be_block_softc backend_block_softc; 196 197 /* 198 * Per-I/O information. 199 */ 200 struct ctl_be_block_io { 201 union ctl_io *io; 202 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 203 struct iovec xiovecs[CTLBLK_MAX_SEGS]; 204 int bio_cmd; 205 int num_segs; 206 int num_bios_sent; 207 int num_bios_done; 208 int send_complete; 209 int first_error; 210 uint64_t first_error_offset; 211 struct bintime ds_t0; 212 devstat_tag_type ds_tag_type; 213 devstat_trans_flags ds_trans_type; 214 uint64_t io_len; 215 uint64_t io_offset; 216 int io_arg; 217 struct ctl_be_block_softc *softc; 218 struct ctl_be_block_lun *lun; 219 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 220 }; 221 222 extern struct ctl_softc *control_softc; 223 224 static int cbb_num_threads = 14; 225 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 226 "CAM Target Layer Block Backend"); 227 SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 228 &cbb_num_threads, 0, "Number of threads per backing file"); 229 230 static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 231 static void ctl_free_beio(struct ctl_be_block_io *beio); 232 static void ctl_complete_beio(struct ctl_be_block_io *beio); 233 static int ctl_be_block_move_done(union ctl_io *io); 234 static void ctl_be_block_biodone(struct bio *bio); 235 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 236 struct ctl_be_block_io *beio); 237 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 238 struct ctl_be_block_io *beio); 239 static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 240 struct ctl_be_block_io *beio); 241 static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 242 const char *attrname); 243 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 244 struct ctl_be_block_io *beio); 245 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 246 struct ctl_be_block_io *beio); 247 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 248 struct ctl_be_block_io *beio); 249 static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 250 const char *attrname); 251 static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 252 union ctl_io *io); 253 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 254 union ctl_io *io); 255 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 256 union ctl_io *io); 257 static void ctl_be_block_worker(void *context, int pending); 258 static int ctl_be_block_submit(union ctl_io *io); 259 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 260 int flag, struct thread *td); 261 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 262 struct ctl_lun_req *req); 263 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 264 struct ctl_lun_req *req); 265 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 266 static int ctl_be_block_open(struct ctl_be_block_lun *be_lun, 267 struct ctl_lun_req *req); 268 static int ctl_be_block_create(struct ctl_be_block_softc *softc, 269 struct ctl_lun_req *req); 270 static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 271 struct ctl_lun_req *req); 272 static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 273 struct ctl_lun_req *req); 274 static void ctl_be_block_lun_shutdown(void *be_lun); 275 static void ctl_be_block_lun_config_status(void *be_lun, 276 ctl_lun_config_status status); 277 static int ctl_be_block_config_write(union ctl_io *io); 278 static int ctl_be_block_config_read(union ctl_io *io); 279 static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 280 static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 281 static int ctl_be_block_init(void); 282 static int ctl_be_block_shutdown(void); 283 284 static struct ctl_backend_driver ctl_be_block_driver = 285 { 286 .name = "block", 287 .flags = CTL_BE_FLAG_HAS_CONFIG, 288 .init = ctl_be_block_init, 289 .shutdown = ctl_be_block_shutdown, 290 .data_submit = ctl_be_block_submit, 291 .data_move_done = ctl_be_block_move_done, 292 .config_read = ctl_be_block_config_read, 293 .config_write = ctl_be_block_config_write, 294 .ioctl = ctl_be_block_ioctl, 295 .lun_info = ctl_be_block_lun_info, 296 .lun_attr = ctl_be_block_lun_attr 297 }; 298 299 MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 300 CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 301 302 static struct ctl_be_block_io * 303 ctl_alloc_beio(struct ctl_be_block_softc *softc) 304 { 305 struct ctl_be_block_io *beio; 306 307 beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO); 308 beio->softc = softc; 309 return (beio); 310 } 311 312 static void 313 ctl_free_beio(struct ctl_be_block_io *beio) 314 { 315 int duplicate_free; 316 int i; 317 318 duplicate_free = 0; 319 320 for (i = 0; i < beio->num_segs; i++) { 321 if (beio->sg_segs[i].addr == NULL) 322 duplicate_free++; 323 324 uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 325 beio->sg_segs[i].addr = NULL; 326 327 /* For compare we had two equal S/G lists. */ 328 if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 329 uma_zfree(beio->lun->lun_zone, 330 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 331 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 332 } 333 } 334 335 if (duplicate_free > 0) { 336 printf("%s: %d duplicate frees out of %d segments\n", __func__, 337 duplicate_free, beio->num_segs); 338 } 339 340 uma_zfree(beio->softc->beio_zone, beio); 341 } 342 343 static void 344 ctl_complete_beio(struct ctl_be_block_io *beio) 345 { 346 union ctl_io *io = beio->io; 347 348 if (beio->beio_cont != NULL) { 349 beio->beio_cont(beio); 350 } else { 351 ctl_free_beio(beio); 352 ctl_data_submit_done(io); 353 } 354 } 355 356 static size_t 357 cmp(uint8_t *a, uint8_t *b, size_t size) 358 { 359 size_t i; 360 361 for (i = 0; i < size; i++) { 362 if (a[i] != b[i]) 363 break; 364 } 365 return (i); 366 } 367 368 static void 369 ctl_be_block_compare(union ctl_io *io) 370 { 371 struct ctl_be_block_io *beio; 372 uint64_t off, res; 373 int i; 374 uint8_t info[8]; 375 376 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 377 off = 0; 378 for (i = 0; i < beio->num_segs; i++) { 379 res = cmp(beio->sg_segs[i].addr, 380 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 381 beio->sg_segs[i].len); 382 off += res; 383 if (res < beio->sg_segs[i].len) 384 break; 385 } 386 if (i < beio->num_segs) { 387 scsi_u64to8b(off, info); 388 ctl_set_sense(&io->scsiio, /*current_error*/ 1, 389 /*sense_key*/ SSD_KEY_MISCOMPARE, 390 /*asc*/ 0x1D, /*ascq*/ 0x00, 391 /*type*/ SSD_ELEM_INFO, 392 /*size*/ sizeof(info), /*data*/ &info, 393 /*type*/ SSD_ELEM_NONE); 394 } else 395 ctl_set_success(&io->scsiio); 396 } 397 398 static int 399 ctl_be_block_move_done(union ctl_io *io) 400 { 401 struct ctl_be_block_io *beio; 402 struct ctl_be_block_lun *be_lun; 403 struct ctl_lba_len_flags *lbalen; 404 #ifdef CTL_TIME_IO 405 struct bintime cur_bt; 406 #endif 407 408 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 409 be_lun = beio->lun; 410 411 DPRINTF("entered\n"); 412 413 #ifdef CTL_TIME_IO 414 getbinuptime(&cur_bt); 415 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 416 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 417 #endif 418 io->io_hdr.num_dmas++; 419 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 420 421 /* 422 * We set status at this point for read commands, and write 423 * commands with errors. 424 */ 425 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 426 ; 427 } else if ((io->io_hdr.port_status != 0) && 428 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 429 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 430 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 431 /*retry_count*/ io->io_hdr.port_status); 432 } else if (io->scsiio.kern_data_resid != 0 && 433 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 434 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 435 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 436 ctl_set_invalid_field_ciu(&io->scsiio); 437 } else if ((io->io_hdr.port_status == 0) && 438 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 439 lbalen = ARGS(beio->io); 440 if (lbalen->flags & CTL_LLF_READ) { 441 ctl_set_success(&io->scsiio); 442 } else if (lbalen->flags & CTL_LLF_COMPARE) { 443 /* We have two data blocks ready for comparison. */ 444 ctl_be_block_compare(io); 445 } 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_sg_entries = beio->num_segs; 1638 io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 1639 1640 /* 1641 * For the read case, we need to read the data into our buffers and 1642 * then we can send it back to the user. For the write case, we 1643 * need to get the data from the user first. 1644 */ 1645 if (beio->bio_cmd == BIO_READ) { 1646 SDT_PROBE0(cbb, , read, alloc_done); 1647 be_lun->dispatch(be_lun, beio); 1648 } else { 1649 SDT_PROBE0(cbb, , write, alloc_done); 1650 #ifdef CTL_TIME_IO 1651 getbinuptime(&io->io_hdr.dma_start_bt); 1652 #endif 1653 ctl_datamove(io); 1654 } 1655 } 1656 1657 static void 1658 ctl_be_block_worker(void *context, int pending) 1659 { 1660 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1661 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1662 union ctl_io *io; 1663 struct ctl_be_block_io *beio; 1664 1665 DPRINTF("entered\n"); 1666 /* 1667 * Fetch and process I/Os from all queues. If we detect LUN 1668 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race, 1669 * so make response maximally opaque to not confuse initiator. 1670 */ 1671 for (;;) { 1672 mtx_lock(&be_lun->queue_lock); 1673 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1674 if (io != NULL) { 1675 DPRINTF("datamove queue\n"); 1676 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1677 ctl_io_hdr, links); 1678 mtx_unlock(&be_lun->queue_lock); 1679 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1680 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1681 ctl_set_busy(&io->scsiio); 1682 ctl_complete_beio(beio); 1683 return; 1684 } 1685 be_lun->dispatch(be_lun, beio); 1686 continue; 1687 } 1688 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1689 if (io != NULL) { 1690 DPRINTF("config write queue\n"); 1691 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1692 ctl_io_hdr, links); 1693 mtx_unlock(&be_lun->queue_lock); 1694 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1695 ctl_set_busy(&io->scsiio); 1696 ctl_config_write_done(io); 1697 return; 1698 } 1699 ctl_be_block_cw_dispatch(be_lun, io); 1700 continue; 1701 } 1702 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1703 if (io != NULL) { 1704 DPRINTF("config read queue\n"); 1705 STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1706 ctl_io_hdr, links); 1707 mtx_unlock(&be_lun->queue_lock); 1708 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1709 ctl_set_busy(&io->scsiio); 1710 ctl_config_read_done(io); 1711 return; 1712 } 1713 ctl_be_block_cr_dispatch(be_lun, io); 1714 continue; 1715 } 1716 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1717 if (io != NULL) { 1718 DPRINTF("input queue\n"); 1719 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1720 ctl_io_hdr, links); 1721 mtx_unlock(&be_lun->queue_lock); 1722 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1723 ctl_set_busy(&io->scsiio); 1724 ctl_data_submit_done(io); 1725 return; 1726 } 1727 ctl_be_block_dispatch(be_lun, io); 1728 continue; 1729 } 1730 1731 /* 1732 * If we get here, there is no work left in the queues, so 1733 * just break out and let the task queue go to sleep. 1734 */ 1735 mtx_unlock(&be_lun->queue_lock); 1736 break; 1737 } 1738 } 1739 1740 /* 1741 * Entry point from CTL to the backend for I/O. We queue everything to a 1742 * work thread, so this just puts the I/O on a queue and wakes up the 1743 * thread. 1744 */ 1745 static int 1746 ctl_be_block_submit(union ctl_io *io) 1747 { 1748 struct ctl_be_block_lun *be_lun; 1749 struct ctl_be_lun *cbe_lun; 1750 1751 DPRINTF("entered\n"); 1752 1753 cbe_lun = CTL_BACKEND_LUN(io); 1754 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1755 1756 /* 1757 * Make sure we only get SCSI I/O. 1758 */ 1759 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1760 "%#x) encountered", io->io_hdr.io_type)); 1761 1762 PRIV(io)->len = 0; 1763 1764 mtx_lock(&be_lun->queue_lock); 1765 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1766 mtx_unlock(&be_lun->queue_lock); 1767 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1768 1769 return (CTL_RETVAL_COMPLETE); 1770 } 1771 1772 static int 1773 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1774 int flag, struct thread *td) 1775 { 1776 struct ctl_be_block_softc *softc; 1777 int error; 1778 1779 softc = &backend_block_softc; 1780 1781 error = 0; 1782 1783 switch (cmd) { 1784 case CTL_LUN_REQ: { 1785 struct ctl_lun_req *lun_req; 1786 1787 lun_req = (struct ctl_lun_req *)addr; 1788 1789 switch (lun_req->reqtype) { 1790 case CTL_LUNREQ_CREATE: 1791 error = ctl_be_block_create(softc, lun_req); 1792 break; 1793 case CTL_LUNREQ_RM: 1794 error = ctl_be_block_rm(softc, lun_req); 1795 break; 1796 case CTL_LUNREQ_MODIFY: 1797 error = ctl_be_block_modify(softc, lun_req); 1798 break; 1799 default: 1800 lun_req->status = CTL_LUN_ERROR; 1801 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1802 "invalid LUN request type %d", 1803 lun_req->reqtype); 1804 break; 1805 } 1806 break; 1807 } 1808 default: 1809 error = ENOTTY; 1810 break; 1811 } 1812 1813 return (error); 1814 } 1815 1816 static int 1817 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1818 { 1819 struct ctl_be_lun *cbe_lun; 1820 struct ctl_be_block_filedata *file_data; 1821 struct ctl_lun_create_params *params; 1822 const char *value; 1823 struct vattr vattr; 1824 off_t ps, pss, po, pos, us, uss, uo, uos; 1825 int error; 1826 1827 cbe_lun = &be_lun->cbe_lun; 1828 file_data = &be_lun->backend.file; 1829 params = &be_lun->params; 1830 1831 be_lun->dev_type = CTL_BE_BLOCK_FILE; 1832 be_lun->dispatch = ctl_be_block_dispatch_file; 1833 be_lun->lun_flush = ctl_be_block_flush_file; 1834 be_lun->get_lba_status = ctl_be_block_gls_file; 1835 be_lun->getattr = ctl_be_block_getattr_file; 1836 be_lun->unmap = NULL; 1837 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1838 1839 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1840 if (error != 0) { 1841 snprintf(req->error_str, sizeof(req->error_str), 1842 "error calling VOP_GETATTR() for file %s", 1843 be_lun->dev_path); 1844 return (error); 1845 } 1846 1847 file_data->cred = crhold(curthread->td_ucred); 1848 if (params->lun_size_bytes != 0) 1849 be_lun->size_bytes = params->lun_size_bytes; 1850 else 1851 be_lun->size_bytes = vattr.va_size; 1852 1853 /* 1854 * For files we can use any logical block size. Prefer 512 bytes 1855 * for compatibility reasons. If file's vattr.va_blocksize 1856 * (preferred I/O block size) is bigger and multiple to chosen 1857 * logical block size -- report it as physical block size. 1858 */ 1859 if (params->blocksize_bytes != 0) 1860 cbe_lun->blocksize = params->blocksize_bytes; 1861 else if (cbe_lun->lun_type == T_CDROM) 1862 cbe_lun->blocksize = 2048; 1863 else 1864 cbe_lun->blocksize = 512; 1865 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1866 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1867 0 : (be_lun->size_blocks - 1); 1868 1869 us = ps = vattr.va_blocksize; 1870 uo = po = 0; 1871 1872 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL); 1873 if (value != NULL) 1874 ctl_expand_number(value, &ps); 1875 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL); 1876 if (value != NULL) 1877 ctl_expand_number(value, &po); 1878 pss = ps / cbe_lun->blocksize; 1879 pos = po / cbe_lun->blocksize; 1880 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1881 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1882 cbe_lun->pblockexp = fls(pss) - 1; 1883 cbe_lun->pblockoff = (pss - pos) % pss; 1884 } 1885 1886 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL); 1887 if (value != NULL) 1888 ctl_expand_number(value, &us); 1889 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL); 1890 if (value != NULL) 1891 ctl_expand_number(value, &uo); 1892 uss = us / cbe_lun->blocksize; 1893 uos = uo / cbe_lun->blocksize; 1894 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1895 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1896 cbe_lun->ublockexp = fls(uss) - 1; 1897 cbe_lun->ublockoff = (uss - uos) % uss; 1898 } 1899 1900 /* 1901 * Sanity check. The media size has to be at least one 1902 * sector long. 1903 */ 1904 if (be_lun->size_bytes < cbe_lun->blocksize) { 1905 error = EINVAL; 1906 snprintf(req->error_str, sizeof(req->error_str), 1907 "file %s size %ju < block size %u", be_lun->dev_path, 1908 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1909 } 1910 1911 cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1912 return (error); 1913 } 1914 1915 static int 1916 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1917 { 1918 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1919 struct ctl_lun_create_params *params; 1920 struct cdevsw *csw; 1921 struct cdev *dev; 1922 const char *value; 1923 int error, atomic, maxio, ref, unmap, tmp; 1924 off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1925 1926 params = &be_lun->params; 1927 1928 be_lun->dev_type = CTL_BE_BLOCK_DEV; 1929 csw = devvn_refthread(be_lun->vn, &dev, &ref); 1930 if (csw == NULL) 1931 return (ENXIO); 1932 if (strcmp(csw->d_name, "zvol") == 0) { 1933 be_lun->dispatch = ctl_be_block_dispatch_zvol; 1934 be_lun->get_lba_status = ctl_be_block_gls_zvol; 1935 atomic = maxio = CTLBLK_MAX_IO_SIZE; 1936 } else { 1937 be_lun->dispatch = ctl_be_block_dispatch_dev; 1938 be_lun->get_lba_status = NULL; 1939 atomic = 0; 1940 maxio = dev->si_iosize_max; 1941 if (maxio <= 0) 1942 maxio = DFLTPHYS; 1943 if (maxio > CTLBLK_MAX_IO_SIZE) 1944 maxio = CTLBLK_MAX_IO_SIZE; 1945 } 1946 be_lun->lun_flush = ctl_be_block_flush_dev; 1947 be_lun->getattr = ctl_be_block_getattr_dev; 1948 be_lun->unmap = ctl_be_block_unmap_dev; 1949 1950 if (!csw->d_ioctl) { 1951 dev_relthread(dev, ref); 1952 snprintf(req->error_str, sizeof(req->error_str), 1953 "no d_ioctl for device %s!", be_lun->dev_path); 1954 return (ENODEV); 1955 } 1956 1957 error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1958 curthread); 1959 if (error) { 1960 dev_relthread(dev, ref); 1961 snprintf(req->error_str, sizeof(req->error_str), 1962 "error %d returned for DIOCGSECTORSIZE ioctl " 1963 "on %s!", error, be_lun->dev_path); 1964 return (error); 1965 } 1966 1967 /* 1968 * If the user has asked for a blocksize that is greater than the 1969 * backing device's blocksize, we can do it only if the blocksize 1970 * the user is asking for is an even multiple of the underlying 1971 * device's blocksize. 1972 */ 1973 if ((params->blocksize_bytes != 0) && 1974 (params->blocksize_bytes >= tmp)) { 1975 if (params->blocksize_bytes % tmp == 0) { 1976 cbe_lun->blocksize = params->blocksize_bytes; 1977 } else { 1978 dev_relthread(dev, ref); 1979 snprintf(req->error_str, sizeof(req->error_str), 1980 "requested blocksize %u is not an even " 1981 "multiple of backing device blocksize %u", 1982 params->blocksize_bytes, tmp); 1983 return (EINVAL); 1984 } 1985 } else if (params->blocksize_bytes != 0) { 1986 dev_relthread(dev, ref); 1987 snprintf(req->error_str, sizeof(req->error_str), 1988 "requested blocksize %u < backing device " 1989 "blocksize %u", params->blocksize_bytes, tmp); 1990 return (EINVAL); 1991 } else if (cbe_lun->lun_type == T_CDROM) 1992 cbe_lun->blocksize = MAX(tmp, 2048); 1993 else 1994 cbe_lun->blocksize = tmp; 1995 1996 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1997 curthread); 1998 if (error) { 1999 dev_relthread(dev, ref); 2000 snprintf(req->error_str, sizeof(req->error_str), 2001 "error %d returned for DIOCGMEDIASIZE " 2002 " ioctl on %s!", error, 2003 be_lun->dev_path); 2004 return (error); 2005 } 2006 2007 if (params->lun_size_bytes != 0) { 2008 if (params->lun_size_bytes > otmp) { 2009 dev_relthread(dev, ref); 2010 snprintf(req->error_str, sizeof(req->error_str), 2011 "requested LUN size %ju > backing device " 2012 "size %ju", 2013 (uintmax_t)params->lun_size_bytes, 2014 (uintmax_t)otmp); 2015 return (EINVAL); 2016 } 2017 2018 be_lun->size_bytes = params->lun_size_bytes; 2019 } else 2020 be_lun->size_bytes = otmp; 2021 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2022 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2023 0 : (be_lun->size_blocks - 1); 2024 2025 error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 2026 curthread); 2027 if (error) 2028 ps = po = 0; 2029 else { 2030 error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2031 FREAD, curthread); 2032 if (error) 2033 po = 0; 2034 } 2035 us = ps; 2036 uo = po; 2037 2038 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL); 2039 if (value != NULL) 2040 ctl_expand_number(value, &ps); 2041 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL); 2042 if (value != NULL) 2043 ctl_expand_number(value, &po); 2044 pss = ps / cbe_lun->blocksize; 2045 pos = po / cbe_lun->blocksize; 2046 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2047 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2048 cbe_lun->pblockexp = fls(pss) - 1; 2049 cbe_lun->pblockoff = (pss - pos) % pss; 2050 } 2051 2052 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL); 2053 if (value != NULL) 2054 ctl_expand_number(value, &us); 2055 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL); 2056 if (value != NULL) 2057 ctl_expand_number(value, &uo); 2058 uss = us / cbe_lun->blocksize; 2059 uos = uo / cbe_lun->blocksize; 2060 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2061 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2062 cbe_lun->ublockexp = fls(uss) - 1; 2063 cbe_lun->ublockoff = (uss - uos) % uss; 2064 } 2065 2066 cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2067 cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2068 2069 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2070 unmap = 1; 2071 } else { 2072 struct diocgattr_arg arg; 2073 2074 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2075 arg.len = sizeof(arg.value.i); 2076 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2077 curthread); 2078 unmap = (error == 0) ? arg.value.i : 0; 2079 } 2080 value = dnvlist_get_string(cbe_lun->options, "unmap", NULL); 2081 if (value != NULL) 2082 unmap = (strcmp(value, "on") == 0); 2083 if (unmap) 2084 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2085 else 2086 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2087 2088 dev_relthread(dev, ref); 2089 return (0); 2090 } 2091 2092 static int 2093 ctl_be_block_close(struct ctl_be_block_lun *be_lun) 2094 { 2095 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2096 int flags; 2097 2098 if (be_lun->vn) { 2099 flags = FREAD; 2100 if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2101 flags |= FWRITE; 2102 (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2103 be_lun->vn = NULL; 2104 2105 switch (be_lun->dev_type) { 2106 case CTL_BE_BLOCK_DEV: 2107 break; 2108 case CTL_BE_BLOCK_FILE: 2109 if (be_lun->backend.file.cred != NULL) { 2110 crfree(be_lun->backend.file.cred); 2111 be_lun->backend.file.cred = NULL; 2112 } 2113 break; 2114 case CTL_BE_BLOCK_NONE: 2115 break; 2116 default: 2117 panic("Unexpected backend type %d", be_lun->dev_type); 2118 break; 2119 } 2120 be_lun->dev_type = CTL_BE_BLOCK_NONE; 2121 } 2122 return (0); 2123 } 2124 2125 static int 2126 ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2127 { 2128 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2129 struct nameidata nd; 2130 const char *value; 2131 int error, flags; 2132 2133 error = 0; 2134 if (rootvnode == NULL) { 2135 snprintf(req->error_str, sizeof(req->error_str), 2136 "Root filesystem is not mounted"); 2137 return (1); 2138 } 2139 pwd_ensure_dirs(); 2140 2141 value = dnvlist_get_string(cbe_lun->options, "file", NULL); 2142 if (value == NULL) { 2143 snprintf(req->error_str, sizeof(req->error_str), 2144 "no file argument specified"); 2145 return (1); 2146 } 2147 free(be_lun->dev_path, M_CTLBLK); 2148 be_lun->dev_path = strdup(value, M_CTLBLK); 2149 2150 flags = FREAD; 2151 value = dnvlist_get_string(cbe_lun->options, "readonly", NULL); 2152 if (value != NULL) { 2153 if (strcmp(value, "on") != 0) 2154 flags |= FWRITE; 2155 } else if (cbe_lun->lun_type == T_DIRECT) 2156 flags |= FWRITE; 2157 2158 again: 2159 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2160 error = vn_open(&nd, &flags, 0, NULL); 2161 if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2162 flags &= ~FWRITE; 2163 goto again; 2164 } 2165 if (error) { 2166 /* 2167 * This is the only reasonable guess we can make as far as 2168 * path if the user doesn't give us a fully qualified path. 2169 * If they want to specify a file, they need to specify the 2170 * full path. 2171 */ 2172 if (be_lun->dev_path[0] != '/') { 2173 char *dev_name; 2174 2175 asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2176 be_lun->dev_path); 2177 free(be_lun->dev_path, M_CTLBLK); 2178 be_lun->dev_path = dev_name; 2179 goto again; 2180 } 2181 snprintf(req->error_str, sizeof(req->error_str), 2182 "error opening %s: %d", be_lun->dev_path, error); 2183 return (error); 2184 } 2185 if (flags & FWRITE) 2186 cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2187 else 2188 cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2189 2190 NDFREE(&nd, NDF_ONLY_PNBUF); 2191 be_lun->vn = nd.ni_vp; 2192 2193 /* We only support disks and files. */ 2194 if (vn_isdisk(be_lun->vn, &error)) { 2195 error = ctl_be_block_open_dev(be_lun, req); 2196 } else if (be_lun->vn->v_type == VREG) { 2197 error = ctl_be_block_open_file(be_lun, req); 2198 } else { 2199 error = EINVAL; 2200 snprintf(req->error_str, sizeof(req->error_str), 2201 "%s is not a disk or plain file", be_lun->dev_path); 2202 } 2203 VOP_UNLOCK(be_lun->vn, 0); 2204 2205 if (error != 0) 2206 ctl_be_block_close(be_lun); 2207 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2208 if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2209 cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2210 value = dnvlist_get_string(cbe_lun->options, "serseq", NULL); 2211 if (value != NULL && strcmp(value, "on") == 0) 2212 cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2213 else if (value != NULL && strcmp(value, "read") == 0) 2214 cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2215 else if (value != NULL && strcmp(value, "off") == 0) 2216 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2217 return (0); 2218 } 2219 2220 static int 2221 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2222 { 2223 struct ctl_be_lun *cbe_lun; 2224 struct ctl_be_block_lun *be_lun; 2225 struct ctl_lun_create_params *params; 2226 char num_thread_str[16]; 2227 char tmpstr[32]; 2228 const char *value; 2229 int retval, num_threads; 2230 int tmp_num_threads; 2231 2232 params = &req->reqdata.create; 2233 retval = 0; 2234 req->status = CTL_LUN_OK; 2235 2236 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2237 cbe_lun = &be_lun->cbe_lun; 2238 cbe_lun->be_lun = be_lun; 2239 be_lun->params = req->reqdata.create; 2240 be_lun->softc = softc; 2241 STAILQ_INIT(&be_lun->input_queue); 2242 STAILQ_INIT(&be_lun->config_read_queue); 2243 STAILQ_INIT(&be_lun->config_write_queue); 2244 STAILQ_INIT(&be_lun->datamove_queue); 2245 sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2246 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2247 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2248 cbe_lun->options = nvlist_clone(req->args_nvl); 2249 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2250 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2251 if (be_lun->lun_zone == NULL) { 2252 snprintf(req->error_str, sizeof(req->error_str), 2253 "error allocating UMA zone"); 2254 goto bailout_error; 2255 } 2256 2257 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2258 cbe_lun->lun_type = params->device_type; 2259 else 2260 cbe_lun->lun_type = T_DIRECT; 2261 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2262 cbe_lun->flags = 0; 2263 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 2264 if (value != NULL) { 2265 if (strcmp(value, "primary") == 0) 2266 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2267 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2268 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2269 2270 if (cbe_lun->lun_type == T_DIRECT || 2271 cbe_lun->lun_type == T_CDROM) { 2272 be_lun->size_bytes = params->lun_size_bytes; 2273 if (params->blocksize_bytes != 0) 2274 cbe_lun->blocksize = params->blocksize_bytes; 2275 else if (cbe_lun->lun_type == T_CDROM) 2276 cbe_lun->blocksize = 2048; 2277 else 2278 cbe_lun->blocksize = 512; 2279 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2280 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2281 0 : (be_lun->size_blocks - 1); 2282 2283 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2284 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2285 retval = ctl_be_block_open(be_lun, req); 2286 if (retval != 0) { 2287 retval = 0; 2288 req->status = CTL_LUN_WARNING; 2289 } 2290 } 2291 num_threads = cbb_num_threads; 2292 } else { 2293 num_threads = 1; 2294 } 2295 2296 value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL); 2297 if (value != NULL) { 2298 tmp_num_threads = strtol(value, NULL, 0); 2299 2300 /* 2301 * We don't let the user specify less than one 2302 * thread, but hope he's clueful enough not to 2303 * specify 1000 threads. 2304 */ 2305 if (tmp_num_threads < 1) { 2306 snprintf(req->error_str, sizeof(req->error_str), 2307 "invalid number of threads %s", 2308 num_thread_str); 2309 goto bailout_error; 2310 } 2311 num_threads = tmp_num_threads; 2312 } 2313 2314 if (be_lun->vn == NULL) 2315 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2316 /* Tell the user the blocksize we ended up using */ 2317 params->lun_size_bytes = be_lun->size_bytes; 2318 params->blocksize_bytes = cbe_lun->blocksize; 2319 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2320 cbe_lun->req_lun_id = params->req_lun_id; 2321 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2322 } else 2323 cbe_lun->req_lun_id = 0; 2324 2325 cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2326 cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2327 cbe_lun->be = &ctl_be_block_driver; 2328 2329 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2330 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d", 2331 softc->num_luns); 2332 strncpy((char *)cbe_lun->serial_num, tmpstr, 2333 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2334 2335 /* Tell the user what we used for a serial number */ 2336 strncpy((char *)params->serial_num, tmpstr, 2337 MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2338 } else { 2339 strncpy((char *)cbe_lun->serial_num, params->serial_num, 2340 MIN(sizeof(cbe_lun->serial_num), 2341 sizeof(params->serial_num))); 2342 } 2343 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2344 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns); 2345 strncpy((char *)cbe_lun->device_id, tmpstr, 2346 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2347 2348 /* Tell the user what we used for a device ID */ 2349 strncpy((char *)params->device_id, tmpstr, 2350 MIN(sizeof(params->device_id), sizeof(tmpstr))); 2351 } else { 2352 strncpy((char *)cbe_lun->device_id, params->device_id, 2353 MIN(sizeof(cbe_lun->device_id), 2354 sizeof(params->device_id))); 2355 } 2356 2357 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2358 2359 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2360 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2361 2362 if (be_lun->io_taskqueue == NULL) { 2363 snprintf(req->error_str, sizeof(req->error_str), 2364 "unable to create taskqueue"); 2365 goto bailout_error; 2366 } 2367 2368 /* 2369 * Note that we start the same number of threads by default for 2370 * both the file case and the block device case. For the file 2371 * case, we need multiple threads to allow concurrency, because the 2372 * vnode interface is designed to be a blocking interface. For the 2373 * block device case, ZFS zvols at least will block the caller's 2374 * context in many instances, and so we need multiple threads to 2375 * overcome that problem. Other block devices don't need as many 2376 * threads, but they shouldn't cause too many problems. 2377 * 2378 * If the user wants to just have a single thread for a block 2379 * device, he can specify that when the LUN is created, or change 2380 * the tunable/sysctl to alter the default number of threads. 2381 */ 2382 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2383 /*num threads*/num_threads, 2384 /*priority*/PUSER, 2385 /*thread name*/ 2386 "%s taskq", be_lun->lunname); 2387 2388 if (retval != 0) 2389 goto bailout_error; 2390 2391 be_lun->num_threads = num_threads; 2392 2393 mtx_lock(&softc->lock); 2394 softc->num_luns++; 2395 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2396 2397 mtx_unlock(&softc->lock); 2398 2399 retval = ctl_add_lun(&be_lun->cbe_lun); 2400 if (retval != 0) { 2401 mtx_lock(&softc->lock); 2402 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2403 links); 2404 softc->num_luns--; 2405 mtx_unlock(&softc->lock); 2406 snprintf(req->error_str, sizeof(req->error_str), 2407 "ctl_add_lun() returned error %d, see dmesg for " 2408 "details", retval); 2409 retval = 0; 2410 goto bailout_error; 2411 } 2412 2413 mtx_lock(&softc->lock); 2414 2415 /* 2416 * Tell the config_status routine that we're waiting so it won't 2417 * clean up the LUN in the event of an error. 2418 */ 2419 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2420 2421 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2422 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2423 if (retval == EINTR) 2424 break; 2425 } 2426 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2427 2428 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2429 snprintf(req->error_str, sizeof(req->error_str), 2430 "LUN configuration error, see dmesg for details"); 2431 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2432 links); 2433 softc->num_luns--; 2434 mtx_unlock(&softc->lock); 2435 goto bailout_error; 2436 } else { 2437 params->req_lun_id = cbe_lun->lun_id; 2438 } 2439 2440 mtx_unlock(&softc->lock); 2441 2442 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2443 cbe_lun->blocksize, 2444 DEVSTAT_ALL_SUPPORTED, 2445 cbe_lun->lun_type 2446 | DEVSTAT_TYPE_IF_OTHER, 2447 DEVSTAT_PRIORITY_OTHER); 2448 2449 return (retval); 2450 2451 bailout_error: 2452 req->status = CTL_LUN_ERROR; 2453 2454 if (be_lun->io_taskqueue != NULL) 2455 taskqueue_free(be_lun->io_taskqueue); 2456 ctl_be_block_close(be_lun); 2457 if (be_lun->dev_path != NULL) 2458 free(be_lun->dev_path, M_CTLBLK); 2459 if (be_lun->lun_zone != NULL) 2460 uma_zdestroy(be_lun->lun_zone); 2461 nvlist_destroy(cbe_lun->options); 2462 mtx_destroy(&be_lun->queue_lock); 2463 mtx_destroy(&be_lun->io_lock); 2464 free(be_lun, M_CTLBLK); 2465 2466 return (retval); 2467 } 2468 2469 static int 2470 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2471 { 2472 struct ctl_lun_rm_params *params; 2473 struct ctl_be_block_lun *be_lun; 2474 struct ctl_be_lun *cbe_lun; 2475 int retval; 2476 2477 params = &req->reqdata.rm; 2478 2479 mtx_lock(&softc->lock); 2480 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2481 if (be_lun->cbe_lun.lun_id == params->lun_id) 2482 break; 2483 } 2484 mtx_unlock(&softc->lock); 2485 if (be_lun == NULL) { 2486 snprintf(req->error_str, sizeof(req->error_str), 2487 "LUN %u is not managed by the block backend", 2488 params->lun_id); 2489 goto bailout_error; 2490 } 2491 cbe_lun = &be_lun->cbe_lun; 2492 2493 retval = ctl_disable_lun(cbe_lun); 2494 if (retval != 0) { 2495 snprintf(req->error_str, sizeof(req->error_str), 2496 "error %d returned from ctl_disable_lun() for " 2497 "LUN %d", retval, params->lun_id); 2498 goto bailout_error; 2499 } 2500 2501 if (be_lun->vn != NULL) { 2502 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2503 ctl_lun_no_media(cbe_lun); 2504 taskqueue_drain_all(be_lun->io_taskqueue); 2505 ctl_be_block_close(be_lun); 2506 } 2507 2508 retval = ctl_invalidate_lun(cbe_lun); 2509 if (retval != 0) { 2510 snprintf(req->error_str, sizeof(req->error_str), 2511 "error %d returned from ctl_invalidate_lun() for " 2512 "LUN %d", retval, params->lun_id); 2513 goto bailout_error; 2514 } 2515 2516 mtx_lock(&softc->lock); 2517 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2518 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2519 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2520 if (retval == EINTR) 2521 break; 2522 } 2523 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2524 2525 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2526 snprintf(req->error_str, sizeof(req->error_str), 2527 "interrupted waiting for LUN to be freed"); 2528 mtx_unlock(&softc->lock); 2529 goto bailout_error; 2530 } 2531 2532 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2533 2534 softc->num_luns--; 2535 mtx_unlock(&softc->lock); 2536 2537 taskqueue_drain_all(be_lun->io_taskqueue); 2538 taskqueue_free(be_lun->io_taskqueue); 2539 2540 if (be_lun->disk_stats != NULL) 2541 devstat_remove_entry(be_lun->disk_stats); 2542 2543 uma_zdestroy(be_lun->lun_zone); 2544 2545 nvlist_destroy(cbe_lun->options); 2546 free(be_lun->dev_path, M_CTLBLK); 2547 mtx_destroy(&be_lun->queue_lock); 2548 mtx_destroy(&be_lun->io_lock); 2549 free(be_lun, M_CTLBLK); 2550 2551 req->status = CTL_LUN_OK; 2552 return (0); 2553 2554 bailout_error: 2555 req->status = CTL_LUN_ERROR; 2556 return (0); 2557 } 2558 2559 static int 2560 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2561 { 2562 struct ctl_lun_modify_params *params; 2563 struct ctl_be_block_lun *be_lun; 2564 struct ctl_be_lun *cbe_lun; 2565 const char *value; 2566 uint64_t oldsize; 2567 int error, wasprim; 2568 2569 params = &req->reqdata.modify; 2570 2571 mtx_lock(&softc->lock); 2572 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2573 if (be_lun->cbe_lun.lun_id == params->lun_id) 2574 break; 2575 } 2576 mtx_unlock(&softc->lock); 2577 if (be_lun == NULL) { 2578 snprintf(req->error_str, sizeof(req->error_str), 2579 "LUN %u is not managed by the block backend", 2580 params->lun_id); 2581 goto bailout_error; 2582 } 2583 cbe_lun = &be_lun->cbe_lun; 2584 2585 if (params->lun_size_bytes != 0) 2586 be_lun->params.lun_size_bytes = params->lun_size_bytes; 2587 2588 nvlist_destroy(cbe_lun->options); 2589 cbe_lun->options = nvlist_clone(req->args_nvl); 2590 2591 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2592 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 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 = be_lun; 2659 struct ctl_be_block_softc *softc = lun->softc; 2660 2661 mtx_lock(&softc->lock); 2662 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2663 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2664 wakeup(lun); 2665 mtx_unlock(&softc->lock); 2666 } 2667 2668 static void 2669 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2670 { 2671 struct ctl_be_block_lun *lun; 2672 struct ctl_be_block_softc *softc; 2673 2674 lun = (struct ctl_be_block_lun *)be_lun; 2675 softc = lun->softc; 2676 2677 if (status == CTL_LUN_CONFIG_OK) { 2678 mtx_lock(&softc->lock); 2679 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2680 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2681 wakeup(lun); 2682 mtx_unlock(&softc->lock); 2683 2684 /* 2685 * We successfully added the LUN, attempt to enable it. 2686 */ 2687 if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2688 printf("%s: ctl_enable_lun() failed!\n", __func__); 2689 if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2690 printf("%s: ctl_invalidate_lun() failed!\n", 2691 __func__); 2692 } 2693 } 2694 2695 return; 2696 } 2697 2698 2699 mtx_lock(&softc->lock); 2700 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2701 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2702 wakeup(lun); 2703 mtx_unlock(&softc->lock); 2704 } 2705 2706 2707 static int 2708 ctl_be_block_config_write(union ctl_io *io) 2709 { 2710 struct ctl_be_block_lun *be_lun; 2711 struct ctl_be_lun *cbe_lun; 2712 int retval; 2713 2714 DPRINTF("entered\n"); 2715 2716 cbe_lun = CTL_BACKEND_LUN(io); 2717 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2718 2719 retval = 0; 2720 switch (io->scsiio.cdb[0]) { 2721 case SYNCHRONIZE_CACHE: 2722 case SYNCHRONIZE_CACHE_16: 2723 case WRITE_SAME_10: 2724 case WRITE_SAME_16: 2725 case UNMAP: 2726 /* 2727 * The upper level CTL code will filter out any CDBs with 2728 * the immediate bit set and return the proper error. 2729 * 2730 * We don't really need to worry about what LBA range the 2731 * user asked to be synced out. When they issue a sync 2732 * cache command, we'll sync out the whole thing. 2733 */ 2734 mtx_lock(&be_lun->queue_lock); 2735 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2736 links); 2737 mtx_unlock(&be_lun->queue_lock); 2738 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2739 break; 2740 case START_STOP_UNIT: { 2741 struct scsi_start_stop_unit *cdb; 2742 struct ctl_lun_req req; 2743 2744 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2745 if ((cdb->how & SSS_PC_MASK) != 0) { 2746 ctl_set_success(&io->scsiio); 2747 ctl_config_write_done(io); 2748 break; 2749 } 2750 if (cdb->how & SSS_START) { 2751 if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) { 2752 retval = ctl_be_block_open(be_lun, &req); 2753 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 2754 if (retval == 0) { 2755 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2756 ctl_lun_has_media(cbe_lun); 2757 } else { 2758 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2759 ctl_lun_no_media(cbe_lun); 2760 } 2761 } 2762 ctl_start_lun(cbe_lun); 2763 } else { 2764 ctl_stop_lun(cbe_lun); 2765 if (cdb->how & SSS_LOEJ) { 2766 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2767 cbe_lun->flags |= CTL_LUN_FLAG_EJECTED; 2768 ctl_lun_ejected(cbe_lun); 2769 if (be_lun->vn != NULL) 2770 ctl_be_block_close(be_lun); 2771 } 2772 } 2773 2774 ctl_set_success(&io->scsiio); 2775 ctl_config_write_done(io); 2776 break; 2777 } 2778 case PREVENT_ALLOW: 2779 ctl_set_success(&io->scsiio); 2780 ctl_config_write_done(io); 2781 break; 2782 default: 2783 ctl_set_invalid_opcode(&io->scsiio); 2784 ctl_config_write_done(io); 2785 retval = CTL_RETVAL_COMPLETE; 2786 break; 2787 } 2788 2789 return (retval); 2790 } 2791 2792 static int 2793 ctl_be_block_config_read(union ctl_io *io) 2794 { 2795 struct ctl_be_block_lun *be_lun; 2796 struct ctl_be_lun *cbe_lun; 2797 int retval = 0; 2798 2799 DPRINTF("entered\n"); 2800 2801 cbe_lun = CTL_BACKEND_LUN(io); 2802 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2803 2804 switch (io->scsiio.cdb[0]) { 2805 case SERVICE_ACTION_IN: 2806 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2807 mtx_lock(&be_lun->queue_lock); 2808 STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2809 &io->io_hdr, links); 2810 mtx_unlock(&be_lun->queue_lock); 2811 taskqueue_enqueue(be_lun->io_taskqueue, 2812 &be_lun->io_task); 2813 retval = CTL_RETVAL_QUEUED; 2814 break; 2815 } 2816 ctl_set_invalid_field(&io->scsiio, 2817 /*sks_valid*/ 1, 2818 /*command*/ 1, 2819 /*field*/ 1, 2820 /*bit_valid*/ 1, 2821 /*bit*/ 4); 2822 ctl_config_read_done(io); 2823 retval = CTL_RETVAL_COMPLETE; 2824 break; 2825 default: 2826 ctl_set_invalid_opcode(&io->scsiio); 2827 ctl_config_read_done(io); 2828 retval = CTL_RETVAL_COMPLETE; 2829 break; 2830 } 2831 2832 return (retval); 2833 } 2834 2835 static int 2836 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2837 { 2838 struct ctl_be_block_lun *lun; 2839 int retval; 2840 2841 lun = (struct ctl_be_block_lun *)be_lun; 2842 2843 retval = sbuf_printf(sb, "\t<num_threads>"); 2844 if (retval != 0) 2845 goto bailout; 2846 retval = sbuf_printf(sb, "%d", lun->num_threads); 2847 if (retval != 0) 2848 goto bailout; 2849 retval = sbuf_printf(sb, "</num_threads>\n"); 2850 2851 bailout: 2852 return (retval); 2853 } 2854 2855 static uint64_t 2856 ctl_be_block_lun_attr(void *be_lun, const char *attrname) 2857 { 2858 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2859 2860 if (lun->getattr == NULL) 2861 return (UINT64_MAX); 2862 return (lun->getattr(lun, attrname)); 2863 } 2864 2865 static int 2866 ctl_be_block_init(void) 2867 { 2868 struct ctl_be_block_softc *softc = &backend_block_softc; 2869 2870 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2871 softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2872 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2873 STAILQ_INIT(&softc->lun_list); 2874 return (0); 2875 } 2876 2877 2878 static int 2879 ctl_be_block_shutdown(void) 2880 { 2881 struct ctl_be_block_softc *softc = &backend_block_softc; 2882 struct ctl_be_block_lun *lun, *next_lun; 2883 2884 mtx_lock(&softc->lock); 2885 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) { 2886 /* 2887 * Drop our lock here. Since ctl_invalidate_lun() can call 2888 * back into us, this could potentially lead to a recursive 2889 * lock of the same mutex, which would cause a hang. 2890 */ 2891 mtx_unlock(&softc->lock); 2892 ctl_disable_lun(&lun->cbe_lun); 2893 ctl_invalidate_lun(&lun->cbe_lun); 2894 mtx_lock(&softc->lock); 2895 } 2896 mtx_unlock(&softc->lock); 2897 2898 uma_zdestroy(softc->beio_zone); 2899 mtx_destroy(&softc->lock); 2900 return (0); 2901 } 2902