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