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