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