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