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