ctl_backend_block.c revision 267519
1229997Sken/*- 2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp. 3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation 4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation 5229997Sken * All rights reserved. 6229997Sken * 7232604Strasz * Portions of this software were developed by Edward Tomasz Napierala 8232604Strasz * under sponsorship from the FreeBSD Foundation. 9232604Strasz * 10229997Sken * Redistribution and use in source and binary forms, with or without 11229997Sken * modification, are permitted provided that the following conditions 12229997Sken * are met: 13229997Sken * 1. Redistributions of source code must retain the above copyright 14229997Sken * notice, this list of conditions, and the following disclaimer, 15229997Sken * without modification. 16229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17229997Sken * substantially similar to the "NO WARRANTY" disclaimer below 18229997Sken * ("Disclaimer") and any redistribution must be conditioned upon 19229997Sken * including a substantially similar Disclaimer requirement for further 20229997Sken * binary redistribution. 21229997Sken * 22229997Sken * NO WARRANTY 23229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33229997Sken * POSSIBILITY OF SUCH DAMAGES. 34229997Sken * 35229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 36229997Sken */ 37229997Sken/* 38229997Sken * CAM Target Layer driver backend for block devices. 39229997Sken * 40229997Sken * Author: Ken Merry <ken@FreeBSD.org> 41229997Sken */ 42229997Sken#include <sys/cdefs.h> 43229997Sken__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 267519 2014-06-15 20:14:11Z mav $"); 44229997Sken 45229997Sken#include <sys/param.h> 46229997Sken#include <sys/systm.h> 47229997Sken#include <sys/kernel.h> 48229997Sken#include <sys/types.h> 49229997Sken#include <sys/kthread.h> 50229997Sken#include <sys/bio.h> 51229997Sken#include <sys/fcntl.h> 52264274Smav#include <sys/limits.h> 53229997Sken#include <sys/lock.h> 54229997Sken#include <sys/mutex.h> 55229997Sken#include <sys/condvar.h> 56229997Sken#include <sys/malloc.h> 57229997Sken#include <sys/conf.h> 58229997Sken#include <sys/ioccom.h> 59229997Sken#include <sys/queue.h> 60229997Sken#include <sys/sbuf.h> 61229997Sken#include <sys/endian.h> 62229997Sken#include <sys/uio.h> 63229997Sken#include <sys/buf.h> 64229997Sken#include <sys/taskqueue.h> 65229997Sken#include <sys/vnode.h> 66229997Sken#include <sys/namei.h> 67229997Sken#include <sys/mount.h> 68229997Sken#include <sys/disk.h> 69229997Sken#include <sys/fcntl.h> 70229997Sken#include <sys/filedesc.h> 71229997Sken#include <sys/proc.h> 72229997Sken#include <sys/pcpu.h> 73229997Sken#include <sys/module.h> 74229997Sken#include <sys/sdt.h> 75229997Sken#include <sys/devicestat.h> 76229997Sken#include <sys/sysctl.h> 77229997Sken 78229997Sken#include <geom/geom.h> 79229997Sken 80229997Sken#include <cam/cam.h> 81229997Sken#include <cam/scsi/scsi_all.h> 82229997Sken#include <cam/scsi/scsi_da.h> 83229997Sken#include <cam/ctl/ctl_io.h> 84229997Sken#include <cam/ctl/ctl.h> 85229997Sken#include <cam/ctl/ctl_backend.h> 86229997Sken#include <cam/ctl/ctl_frontend_internal.h> 87229997Sken#include <cam/ctl/ctl_ioctl.h> 88229997Sken#include <cam/ctl/ctl_scsi_all.h> 89229997Sken#include <cam/ctl/ctl_error.h> 90229997Sken 91229997Sken/* 92264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 93264886Smav * I/O. If we get an I/O larger than that, we'll split it. 94229997Sken */ 95264886Smav#define CTLBLK_MAX_IO_SIZE (1024 * 1024) 96264886Smav#define CTLBLK_MAX_SEG MAXPHYS 97264886Smav#define CTLBLK_MAX_SEGS MAX(CTLBLK_MAX_IO_SIZE / CTLBLK_MAX_SEG, 1) 98229997Sken 99229997Sken#ifdef CTLBLK_DEBUG 100229997Sken#define DPRINTF(fmt, args...) \ 101229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 102229997Sken#else 103229997Sken#define DPRINTF(fmt, args...) do {} while(0) 104229997Sken#endif 105229997Sken 106267519Smav#define PRIV(io) \ 107267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 108267519Smav 109229997SkenSDT_PROVIDER_DEFINE(cbb); 110229997Sken 111229997Skentypedef enum { 112229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 113229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 114229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 115229997Sken CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 116229997Sken} ctl_be_block_lun_flags; 117229997Sken 118229997Skentypedef enum { 119229997Sken CTL_BE_BLOCK_NONE, 120229997Sken CTL_BE_BLOCK_DEV, 121229997Sken CTL_BE_BLOCK_FILE 122229997Sken} ctl_be_block_type; 123229997Sken 124229997Skenstruct ctl_be_block_devdata { 125229997Sken struct cdev *cdev; 126229997Sken struct cdevsw *csw; 127229997Sken int dev_ref; 128229997Sken}; 129229997Sken 130229997Skenstruct ctl_be_block_filedata { 131229997Sken struct ucred *cred; 132229997Sken}; 133229997Sken 134229997Skenunion ctl_be_block_bedata { 135229997Sken struct ctl_be_block_devdata dev; 136229997Sken struct ctl_be_block_filedata file; 137229997Sken}; 138229997Sken 139229997Skenstruct ctl_be_block_io; 140229997Skenstruct ctl_be_block_lun; 141229997Sken 142229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 143229997Sken struct ctl_be_block_io *beio); 144229997Sken 145229997Sken/* 146229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 147229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 148229997Sken */ 149229997Skenstruct ctl_be_block_lun { 150229997Sken struct ctl_block_disk *disk; 151229997Sken char lunname[32]; 152229997Sken char *dev_path; 153229997Sken ctl_be_block_type dev_type; 154229997Sken struct vnode *vn; 155229997Sken union ctl_be_block_bedata backend; 156229997Sken cbb_dispatch_t dispatch; 157229997Sken cbb_dispatch_t lun_flush; 158264274Smav cbb_dispatch_t unmap; 159229997Sken struct mtx lock; 160229997Sken uma_zone_t lun_zone; 161229997Sken uint64_t size_blocks; 162229997Sken uint64_t size_bytes; 163229997Sken uint32_t blocksize; 164229997Sken int blocksize_shift; 165264191Smav uint16_t pblockexp; 166264191Smav uint16_t pblockoff; 167229997Sken struct ctl_be_block_softc *softc; 168229997Sken struct devstat *disk_stats; 169229997Sken ctl_be_block_lun_flags flags; 170229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 171229997Sken struct ctl_be_lun ctl_be_lun; 172229997Sken struct taskqueue *io_taskqueue; 173229997Sken struct task io_task; 174229997Sken int num_threads; 175229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 176229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 177229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 178229997Sken}; 179229997Sken 180229997Sken/* 181229997Sken * Overall softc structure for the block backend module. 182229997Sken */ 183229997Skenstruct ctl_be_block_softc { 184229997Sken struct mtx lock; 185229997Sken int num_disks; 186229997Sken STAILQ_HEAD(, ctl_block_disk) disk_list; 187229997Sken int num_luns; 188229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 189229997Sken}; 190229997Sken 191229997Skenstatic struct ctl_be_block_softc backend_block_softc; 192229997Sken 193229997Sken/* 194229997Sken * Per-I/O information. 195229997Sken */ 196229997Skenstruct ctl_be_block_io { 197229997Sken union ctl_io *io; 198229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 199229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 200229997Sken int bio_cmd; 201229997Sken int bio_flags; 202229997Sken int num_segs; 203229997Sken int num_bios_sent; 204229997Sken int num_bios_done; 205229997Sken int send_complete; 206229997Sken int num_errors; 207229997Sken struct bintime ds_t0; 208229997Sken devstat_tag_type ds_tag_type; 209229997Sken devstat_trans_flags ds_trans_type; 210229997Sken uint64_t io_len; 211229997Sken uint64_t io_offset; 212229997Sken struct ctl_be_block_softc *softc; 213229997Sken struct ctl_be_block_lun *lun; 214264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 215229997Sken}; 216229997Sken 217229997Skenstatic int cbb_num_threads = 14; 218229997SkenTUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads); 219229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 220229997Sken "CAM Target Layer Block Backend"); 221229997SkenSYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW, 222229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 223229997Sken 224229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 225229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 226229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 227229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 228229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 229229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 230229997Sken struct ctl_be_block_io *beio); 231229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 232229997Sken struct ctl_be_block_io *beio); 233229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 234229997Sken struct ctl_be_block_io *beio); 235264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 236264274Smav struct ctl_be_block_io *beio); 237229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 238229997Sken struct ctl_be_block_io *beio); 239229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 240229997Sken union ctl_io *io); 241229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 242229997Sken union ctl_io *io); 243229997Skenstatic void ctl_be_block_worker(void *context, int pending); 244229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 245229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 246229997Sken int flag, struct thread *td); 247229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 248229997Sken struct ctl_lun_req *req); 249229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 250229997Sken struct ctl_lun_req *req); 251229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 252229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 253229997Sken struct ctl_be_block_lun *be_lun, 254229997Sken struct ctl_lun_req *req); 255229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 256229997Sken struct ctl_lun_req *req); 257229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 258229997Sken struct ctl_lun_req *req); 259232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 260232604Strasz struct ctl_lun_req *req); 261232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 262232604Strasz struct ctl_lun_req *req); 263232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 264232604Strasz struct ctl_lun_req *req); 265229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 266229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 267229997Sken ctl_lun_config_status status); 268229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 269229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 270229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 271229997Skenint ctl_be_block_init(void); 272229997Sken 273229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 274229997Sken{ 275230334Sken .name = "block", 276230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 277230334Sken .init = ctl_be_block_init, 278230334Sken .data_submit = ctl_be_block_submit, 279230334Sken .data_move_done = ctl_be_block_move_done, 280230334Sken .config_read = ctl_be_block_config_read, 281230334Sken .config_write = ctl_be_block_config_write, 282230334Sken .ioctl = ctl_be_block_ioctl, 283230334Sken .lun_info = ctl_be_block_lun_info 284229997Sken}; 285229997Sken 286229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 287229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 288229997Sken 289264020Straszstatic uma_zone_t beio_zone; 290264020Strasz 291229997Skenstatic struct ctl_be_block_io * 292229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 293229997Sken{ 294229997Sken struct ctl_be_block_io *beio; 295229997Sken 296264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 297264020Strasz beio->softc = softc; 298229997Sken return (beio); 299229997Sken} 300229997Sken 301229997Skenstatic void 302229997Skenctl_free_beio(struct ctl_be_block_io *beio) 303229997Sken{ 304229997Sken int duplicate_free; 305229997Sken int i; 306229997Sken 307229997Sken duplicate_free = 0; 308229997Sken 309229997Sken for (i = 0; i < beio->num_segs; i++) { 310229997Sken if (beio->sg_segs[i].addr == NULL) 311229997Sken duplicate_free++; 312229997Sken 313229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 314229997Sken beio->sg_segs[i].addr = NULL; 315229997Sken } 316229997Sken 317229997Sken if (duplicate_free > 0) { 318229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 319229997Sken duplicate_free, beio->num_segs); 320229997Sken } 321229997Sken 322264020Strasz uma_zfree(beio_zone, beio); 323229997Sken} 324229997Sken 325229997Skenstatic void 326229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 327229997Sken{ 328229997Sken union ctl_io *io; 329229997Sken int io_len; 330229997Sken 331229997Sken io = beio->io; 332229997Sken 333229997Sken if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 334229997Sken io_len = beio->io_len; 335229997Sken else 336229997Sken io_len = 0; 337229997Sken 338229997Sken devstat_end_transaction(beio->lun->disk_stats, 339229997Sken /*bytes*/ io_len, 340229997Sken beio->ds_tag_type, 341229997Sken beio->ds_trans_type, 342229997Sken /*now*/ NULL, 343229997Sken /*then*/&beio->ds_t0); 344229997Sken 345264274Smav if (beio->beio_cont != NULL) { 346264274Smav beio->beio_cont(beio); 347264274Smav } else { 348264274Smav ctl_free_beio(beio); 349264274Smav ctl_done(io); 350264274Smav } 351229997Sken} 352229997Sken 353229997Skenstatic int 354229997Skenctl_be_block_move_done(union ctl_io *io) 355229997Sken{ 356229997Sken struct ctl_be_block_io *beio; 357229997Sken struct ctl_be_block_lun *be_lun; 358229997Sken#ifdef CTL_TIME_IO 359229997Sken struct bintime cur_bt; 360229997Sken#endif 361229997Sken 362267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 363229997Sken be_lun = beio->lun; 364229997Sken 365229997Sken DPRINTF("entered\n"); 366229997Sken 367229997Sken#ifdef CTL_TIME_IO 368229997Sken getbintime(&cur_bt); 369229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 370229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 371229997Sken io->io_hdr.num_dmas++; 372229997Sken#endif 373229997Sken 374229997Sken /* 375229997Sken * We set status at this point for read commands, and write 376229997Sken * commands with errors. 377229997Sken */ 378229997Sken if ((beio->bio_cmd == BIO_READ) 379229997Sken && (io->io_hdr.port_status == 0) 380229997Sken && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 381229997Sken && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 382229997Sken ctl_set_success(&io->scsiio); 383229997Sken else if ((io->io_hdr.port_status != 0) 384229997Sken && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 385229997Sken && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 386229997Sken /* 387229997Sken * For hardware error sense keys, the sense key 388229997Sken * specific value is defined to be a retry count, 389229997Sken * but we use it to pass back an internal FETD 390229997Sken * error code. XXX KDM Hopefully the FETD is only 391229997Sken * using 16 bits for an error code, since that's 392229997Sken * all the space we have in the sks field. 393229997Sken */ 394229997Sken ctl_set_internal_failure(&io->scsiio, 395229997Sken /*sks_valid*/ 1, 396229997Sken /*retry_count*/ 397229997Sken io->io_hdr.port_status); 398229997Sken } 399229997Sken 400229997Sken /* 401229997Sken * If this is a read, or a write with errors, it is done. 402229997Sken */ 403229997Sken if ((beio->bio_cmd == BIO_READ) 404229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 405229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 406229997Sken ctl_complete_beio(beio); 407229997Sken return (0); 408229997Sken } 409229997Sken 410229997Sken /* 411229997Sken * At this point, we have a write and the DMA completed 412229997Sken * successfully. We now have to queue it to the task queue to 413229997Sken * execute the backend I/O. That is because we do blocking 414229997Sken * memory allocations, and in the file backing case, blocking I/O. 415229997Sken * This move done routine is generally called in the SIM's 416229997Sken * interrupt context, and therefore we cannot block. 417229997Sken */ 418229997Sken mtx_lock(&be_lun->lock); 419229997Sken /* 420229997Sken * XXX KDM make sure that links is okay to use at this point. 421229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 422229997Sken * or deal with resource allocation here. 423229997Sken */ 424229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 425229997Sken mtx_unlock(&be_lun->lock); 426229997Sken 427229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 428229997Sken 429229997Sken return (0); 430229997Sken} 431229997Sken 432229997Skenstatic void 433229997Skenctl_be_block_biodone(struct bio *bio) 434229997Sken{ 435229997Sken struct ctl_be_block_io *beio; 436229997Sken struct ctl_be_block_lun *be_lun; 437229997Sken union ctl_io *io; 438261538Smav int error; 439229997Sken 440229997Sken beio = bio->bio_caller1; 441229997Sken be_lun = beio->lun; 442229997Sken io = beio->io; 443229997Sken 444229997Sken DPRINTF("entered\n"); 445229997Sken 446261538Smav error = bio->bio_error; 447229997Sken mtx_lock(&be_lun->lock); 448261538Smav if (error != 0) 449229997Sken beio->num_errors++; 450229997Sken 451229997Sken beio->num_bios_done++; 452229997Sken 453229997Sken /* 454229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 455229997Sken * during the free might cause it to complain. 456229997Sken */ 457229997Sken g_destroy_bio(bio); 458229997Sken 459229997Sken /* 460229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 461229997Sken * complete, then we're done. 462229997Sken */ 463229997Sken if ((beio->send_complete == 0) 464229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 465229997Sken mtx_unlock(&be_lun->lock); 466229997Sken return; 467229997Sken } 468229997Sken 469229997Sken /* 470229997Sken * At this point, we've verified that we are the last I/O to 471229997Sken * complete, so it's safe to drop the lock. 472229997Sken */ 473229997Sken mtx_unlock(&be_lun->lock); 474229997Sken 475229997Sken /* 476229997Sken * If there are any errors from the backing device, we fail the 477229997Sken * entire I/O with a medium error. 478229997Sken */ 479229997Sken if (beio->num_errors > 0) { 480261538Smav if (error == EOPNOTSUPP) { 481261538Smav ctl_set_invalid_opcode(&io->scsiio); 482261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 483229997Sken /* XXX KDM is there is a better error here? */ 484229997Sken ctl_set_internal_failure(&io->scsiio, 485229997Sken /*sks_valid*/ 1, 486229997Sken /*retry_count*/ 0xbad2); 487229997Sken } else 488229997Sken ctl_set_medium_error(&io->scsiio); 489229997Sken ctl_complete_beio(beio); 490229997Sken return; 491229997Sken } 492229997Sken 493229997Sken /* 494264274Smav * If this is a write, a flush or a delete, we're all done. 495229997Sken * If this is a read, we can now send the data to the user. 496229997Sken */ 497229997Sken if ((beio->bio_cmd == BIO_WRITE) 498264274Smav || (beio->bio_cmd == BIO_FLUSH) 499264274Smav || (beio->bio_cmd == BIO_DELETE)) { 500229997Sken ctl_set_success(&io->scsiio); 501229997Sken ctl_complete_beio(beio); 502229997Sken } else { 503229997Sken#ifdef CTL_TIME_IO 504229997Sken getbintime(&io->io_hdr.dma_start_bt); 505229997Sken#endif 506229997Sken ctl_datamove(io); 507229997Sken } 508229997Sken} 509229997Sken 510229997Skenstatic void 511229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 512229997Sken struct ctl_be_block_io *beio) 513229997Sken{ 514229997Sken union ctl_io *io; 515229997Sken struct mount *mountpoint; 516241896Skib int error, lock_flags; 517229997Sken 518229997Sken DPRINTF("entered\n"); 519229997Sken 520229997Sken io = beio->io; 521229997Sken 522229997Sken (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 523229997Sken 524229997Sken if (MNT_SHARED_WRITES(mountpoint) 525229997Sken || ((mountpoint == NULL) 526229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 527229997Sken lock_flags = LK_SHARED; 528229997Sken else 529229997Sken lock_flags = LK_EXCLUSIVE; 530229997Sken 531229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 532229997Sken 533229997Sken binuptime(&beio->ds_t0); 534229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 535229997Sken 536229997Sken error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 537229997Sken VOP_UNLOCK(be_lun->vn, 0); 538229997Sken 539229997Sken vn_finished_write(mountpoint); 540229997Sken 541229997Sken if (error == 0) 542229997Sken ctl_set_success(&io->scsiio); 543229997Sken else { 544229997Sken /* XXX KDM is there is a better error here? */ 545229997Sken ctl_set_internal_failure(&io->scsiio, 546229997Sken /*sks_valid*/ 1, 547229997Sken /*retry_count*/ 0xbad1); 548229997Sken } 549229997Sken 550229997Sken ctl_complete_beio(beio); 551229997Sken} 552229997Sken 553258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 554258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 555258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 556258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 557229997Sken 558229997Skenstatic void 559229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 560229997Sken struct ctl_be_block_io *beio) 561229997Sken{ 562229997Sken struct ctl_be_block_filedata *file_data; 563229997Sken union ctl_io *io; 564229997Sken struct uio xuio; 565229997Sken struct iovec *xiovec; 566241896Skib int flags; 567229997Sken int error, i; 568229997Sken 569229997Sken DPRINTF("entered\n"); 570229997Sken 571229997Sken file_data = &be_lun->backend.file; 572229997Sken io = beio->io; 573229997Sken flags = beio->bio_flags; 574229997Sken 575229997Sken if (beio->bio_cmd == BIO_READ) { 576229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 577229997Sken } else { 578229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 579229997Sken } 580229997Sken 581229997Sken bzero(&xuio, sizeof(xuio)); 582229997Sken if (beio->bio_cmd == BIO_READ) 583229997Sken xuio.uio_rw = UIO_READ; 584229997Sken else 585229997Sken xuio.uio_rw = UIO_WRITE; 586229997Sken 587229997Sken xuio.uio_offset = beio->io_offset; 588229997Sken xuio.uio_resid = beio->io_len; 589229997Sken xuio.uio_segflg = UIO_SYSSPACE; 590229997Sken xuio.uio_iov = beio->xiovecs; 591229997Sken xuio.uio_iovcnt = beio->num_segs; 592229997Sken xuio.uio_td = curthread; 593229997Sken 594229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 595229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 596229997Sken xiovec->iov_len = beio->sg_segs[i].len; 597229997Sken } 598229997Sken 599229997Sken if (beio->bio_cmd == BIO_READ) { 600229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 601229997Sken 602229997Sken binuptime(&beio->ds_t0); 603229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 604229997Sken 605229997Sken /* 606229997Sken * UFS pays attention to IO_DIRECT for reads. If the 607229997Sken * DIRECTIO option is configured into the kernel, it calls 608229997Sken * ffs_rawread(). But that only works for single-segment 609229997Sken * uios with user space addresses. In our case, with a 610229997Sken * kernel uio, it still reads into the buffer cache, but it 611229997Sken * will just try to release the buffer from the cache later 612229997Sken * on in ffs_read(). 613229997Sken * 614229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 615229997Sken * 616229997Sken * UFS does not pay attention to IO_SYNC for reads. 617229997Sken * 618229997Sken * ZFS pays attention to IO_SYNC (which translates into the 619229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 620229997Sken * attempts to sync the file before reading. 621229997Sken * 622229997Sken * So, to attempt to provide some barrier semantics in the 623229997Sken * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 624229997Sken */ 625229997Sken error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 626229997Sken (IO_DIRECT|IO_SYNC) : 0, file_data->cred); 627229997Sken 628229997Sken VOP_UNLOCK(be_lun->vn, 0); 629229997Sken } else { 630229997Sken struct mount *mountpoint; 631229997Sken int lock_flags; 632229997Sken 633229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 634229997Sken 635229997Sken if (MNT_SHARED_WRITES(mountpoint) 636229997Sken || ((mountpoint == NULL) 637229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 638229997Sken lock_flags = LK_SHARED; 639229997Sken else 640229997Sken lock_flags = LK_EXCLUSIVE; 641229997Sken 642229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 643229997Sken 644229997Sken binuptime(&beio->ds_t0); 645229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 646229997Sken 647229997Sken /* 648229997Sken * UFS pays attention to IO_DIRECT for writes. The write 649229997Sken * is done asynchronously. (Normally the write would just 650229997Sken * get put into cache. 651229997Sken * 652229997Sken * UFS pays attention to IO_SYNC for writes. It will 653229997Sken * attempt to write the buffer out synchronously if that 654229997Sken * flag is set. 655229997Sken * 656229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 657229997Sken * 658229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 659229997Sken * for writes. It will flush the transaction from the 660229997Sken * cache before returning. 661229997Sken * 662229997Sken * So if we've got the BIO_ORDERED flag set, we want 663229997Sken * IO_SYNC in either the UFS or ZFS case. 664229997Sken */ 665229997Sken error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 666229997Sken IO_SYNC : 0, file_data->cred); 667229997Sken VOP_UNLOCK(be_lun->vn, 0); 668229997Sken 669229997Sken vn_finished_write(mountpoint); 670229997Sken } 671229997Sken 672229997Sken /* 673229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 674229997Sken * return the I/O to the user. 675229997Sken */ 676229997Sken if (error != 0) { 677229997Sken char path_str[32]; 678229997Sken 679229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 680229997Sken /* 681229997Sken * XXX KDM ZFS returns ENOSPC when the underlying 682229997Sken * filesystem fills up. What kind of SCSI error should we 683229997Sken * return for that? 684229997Sken */ 685229997Sken printf("%s%s command returned errno %d\n", path_str, 686229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 687229997Sken ctl_set_medium_error(&io->scsiio); 688229997Sken ctl_complete_beio(beio); 689229997Sken return; 690229997Sken } 691229997Sken 692229997Sken /* 693229997Sken * If this is a write, we're all done. 694229997Sken * If this is a read, we can now send the data to the user. 695229997Sken */ 696229997Sken if (beio->bio_cmd == BIO_WRITE) { 697229997Sken ctl_set_success(&io->scsiio); 698229997Sken SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 699229997Sken ctl_complete_beio(beio); 700229997Sken } else { 701229997Sken SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 702229997Sken#ifdef CTL_TIME_IO 703229997Sken getbintime(&io->io_hdr.dma_start_bt); 704229997Sken#endif 705229997Sken ctl_datamove(io); 706229997Sken } 707229997Sken} 708229997Sken 709229997Skenstatic void 710229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 711229997Sken struct ctl_be_block_io *beio) 712229997Sken{ 713229997Sken struct bio *bio; 714229997Sken union ctl_io *io; 715229997Sken struct ctl_be_block_devdata *dev_data; 716229997Sken 717229997Sken dev_data = &be_lun->backend.dev; 718229997Sken io = beio->io; 719229997Sken 720229997Sken DPRINTF("entered\n"); 721229997Sken 722229997Sken /* This can't fail, it's a blocking allocation. */ 723229997Sken bio = g_alloc_bio(); 724229997Sken 725229997Sken bio->bio_cmd = BIO_FLUSH; 726229997Sken bio->bio_flags |= BIO_ORDERED; 727229997Sken bio->bio_dev = dev_data->cdev; 728229997Sken bio->bio_offset = 0; 729229997Sken bio->bio_data = 0; 730229997Sken bio->bio_done = ctl_be_block_biodone; 731229997Sken bio->bio_caller1 = beio; 732229997Sken bio->bio_pblkno = 0; 733229997Sken 734229997Sken /* 735229997Sken * We don't need to acquire the LUN lock here, because we are only 736229997Sken * sending one bio, and so there is no other context to synchronize 737229997Sken * with. 738229997Sken */ 739229997Sken beio->num_bios_sent = 1; 740229997Sken beio->send_complete = 1; 741229997Sken 742229997Sken binuptime(&beio->ds_t0); 743229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 744229997Sken 745229997Sken (*dev_data->csw->d_strategy)(bio); 746229997Sken} 747229997Sken 748229997Skenstatic void 749264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 750264274Smav struct ctl_be_block_io *beio, 751264274Smav uint64_t off, uint64_t len, int last) 752264274Smav{ 753264274Smav struct bio *bio; 754264274Smav struct ctl_be_block_devdata *dev_data; 755264296Smav uint64_t maxlen; 756264274Smav 757264274Smav dev_data = &be_lun->backend.dev; 758264296Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 759264274Smav while (len > 0) { 760264274Smav bio = g_alloc_bio(); 761264274Smav bio->bio_cmd = BIO_DELETE; 762264274Smav bio->bio_flags |= beio->bio_flags; 763264274Smav bio->bio_dev = dev_data->cdev; 764264274Smav bio->bio_offset = off; 765264296Smav bio->bio_length = MIN(len, maxlen); 766264274Smav bio->bio_data = 0; 767264274Smav bio->bio_done = ctl_be_block_biodone; 768264274Smav bio->bio_caller1 = beio; 769264296Smav bio->bio_pblkno = off / be_lun->blocksize; 770264274Smav 771264274Smav off += bio->bio_length; 772264274Smav len -= bio->bio_length; 773264274Smav 774264274Smav mtx_lock(&be_lun->lock); 775264274Smav beio->num_bios_sent++; 776264274Smav if (last && len == 0) 777264274Smav beio->send_complete = 1; 778264274Smav mtx_unlock(&be_lun->lock); 779264274Smav 780264274Smav (*dev_data->csw->d_strategy)(bio); 781264274Smav } 782264274Smav} 783264274Smav 784264274Smavstatic void 785264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 786264274Smav struct ctl_be_block_io *beio) 787264274Smav{ 788264274Smav union ctl_io *io; 789264274Smav struct ctl_be_block_devdata *dev_data; 790267515Smav struct ctl_ptr_len_flags *ptrlen; 791264274Smav struct scsi_unmap_desc *buf, *end; 792264274Smav uint64_t len; 793264274Smav 794264274Smav dev_data = &be_lun->backend.dev; 795264274Smav io = beio->io; 796264274Smav 797264274Smav DPRINTF("entered\n"); 798264274Smav 799264274Smav binuptime(&beio->ds_t0); 800264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 801264274Smav 802264274Smav if (beio->io_offset == -1) { 803264274Smav beio->io_len = 0; 804267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 805267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 806267515Smav end = buf + ptrlen->len / sizeof(*buf); 807264274Smav for (; buf < end; buf++) { 808264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 809264274Smav be_lun->blocksize; 810264274Smav beio->io_len += len; 811264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 812264274Smav scsi_8btou64(buf->lba) * be_lun->blocksize, len, 813264283Smav (end - buf < 2) ? TRUE : FALSE); 814264274Smav } 815264274Smav } else 816264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 817264274Smav beio->io_offset, beio->io_len, TRUE); 818264274Smav} 819264274Smav 820264274Smavstatic void 821229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 822229997Sken struct ctl_be_block_io *beio) 823229997Sken{ 824229997Sken int i; 825229997Sken struct bio *bio; 826229997Sken struct ctl_be_block_devdata *dev_data; 827229997Sken off_t cur_offset; 828229997Sken int max_iosize; 829229997Sken 830229997Sken DPRINTF("entered\n"); 831229997Sken 832229997Sken dev_data = &be_lun->backend.dev; 833229997Sken 834229997Sken /* 835229997Sken * We have to limit our I/O size to the maximum supported by the 836229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 837229997Sken * set it properly, use DFLTPHYS. 838229997Sken */ 839229997Sken max_iosize = dev_data->cdev->si_iosize_max; 840229997Sken if (max_iosize < PAGE_SIZE) 841229997Sken max_iosize = DFLTPHYS; 842229997Sken 843229997Sken cur_offset = beio->io_offset; 844229997Sken 845229997Sken /* 846229997Sken * XXX KDM need to accurately reflect the number of I/Os outstanding 847229997Sken * to a device. 848229997Sken */ 849229997Sken binuptime(&beio->ds_t0); 850229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 851229997Sken 852229997Sken for (i = 0; i < beio->num_segs; i++) { 853229997Sken size_t cur_size; 854229997Sken uint8_t *cur_ptr; 855229997Sken 856229997Sken cur_size = beio->sg_segs[i].len; 857229997Sken cur_ptr = beio->sg_segs[i].addr; 858229997Sken 859229997Sken while (cur_size > 0) { 860229997Sken /* This can't fail, it's a blocking allocation. */ 861229997Sken bio = g_alloc_bio(); 862229997Sken 863229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 864229997Sken 865229997Sken bio->bio_cmd = beio->bio_cmd; 866229997Sken bio->bio_flags |= beio->bio_flags; 867229997Sken bio->bio_dev = dev_data->cdev; 868229997Sken bio->bio_caller1 = beio; 869229997Sken bio->bio_length = min(cur_size, max_iosize); 870229997Sken bio->bio_offset = cur_offset; 871229997Sken bio->bio_data = cur_ptr; 872229997Sken bio->bio_done = ctl_be_block_biodone; 873229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 874229997Sken 875229997Sken cur_offset += bio->bio_length; 876229997Sken cur_ptr += bio->bio_length; 877229997Sken cur_size -= bio->bio_length; 878229997Sken 879229997Sken /* 880229997Sken * Make sure we set the complete bit just before we 881229997Sken * issue the last bio so we don't wind up with a 882229997Sken * race. 883229997Sken * 884229997Sken * Use the LUN mutex here instead of a combination 885229997Sken * of atomic variables for simplicity. 886229997Sken * 887229997Sken * XXX KDM we could have a per-IO lock, but that 888229997Sken * would cause additional per-IO setup and teardown 889229997Sken * overhead. Hopefully there won't be too much 890229997Sken * contention on the LUN lock. 891229997Sken */ 892229997Sken mtx_lock(&be_lun->lock); 893229997Sken 894229997Sken beio->num_bios_sent++; 895229997Sken 896229997Sken if ((i == beio->num_segs - 1) 897229997Sken && (cur_size == 0)) 898229997Sken beio->send_complete = 1; 899229997Sken 900229997Sken mtx_unlock(&be_lun->lock); 901229997Sken 902229997Sken (*dev_data->csw->d_strategy)(bio); 903229997Sken } 904229997Sken } 905229997Sken} 906229997Sken 907229997Skenstatic void 908264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 909264274Smav{ 910264274Smav union ctl_io *io; 911264274Smav 912264274Smav io = beio->io; 913264274Smav ctl_free_beio(beio); 914264274Smav if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 915264274Smav && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 916264274Smav ctl_config_write_done(io); 917264274Smav return; 918264274Smav } 919264274Smav 920264274Smav ctl_be_block_config_write(io); 921264274Smav} 922264274Smav 923264274Smavstatic void 924264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 925264274Smav union ctl_io *io) 926264274Smav{ 927264274Smav struct ctl_be_block_io *beio; 928264274Smav struct ctl_be_block_softc *softc; 929267515Smav struct ctl_lba_len_flags *lbalen; 930264274Smav uint64_t len_left, lba; 931264274Smav int i, seglen; 932264274Smav uint8_t *buf, *end; 933264274Smav 934264274Smav DPRINTF("entered\n"); 935264274Smav 936267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 937264274Smav softc = be_lun->softc; 938267515Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 939264274Smav 940267515Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP) || 941267515Smav (lbalen->flags & SWS_UNMAP && be_lun->unmap == NULL)) { 942264274Smav ctl_free_beio(beio); 943264274Smav ctl_set_invalid_field(&io->scsiio, 944264274Smav /*sks_valid*/ 1, 945264274Smav /*command*/ 1, 946264274Smav /*field*/ 1, 947264274Smav /*bit_valid*/ 0, 948264274Smav /*bit*/ 0); 949264274Smav ctl_config_write_done(io); 950264274Smav return; 951264274Smav } 952264274Smav 953264274Smav /* 954264274Smav * If the I/O came down with an ordered or head of queue tag, set 955264274Smav * the BIO_ORDERED attribute. For head of queue tags, that's 956264274Smav * pretty much the best we can do. 957264274Smav */ 958264274Smav if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 959264274Smav || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 960264274Smav beio->bio_flags = BIO_ORDERED; 961264274Smav 962264274Smav switch (io->scsiio.tag_type) { 963264274Smav case CTL_TAG_ORDERED: 964264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 965264274Smav break; 966264274Smav case CTL_TAG_HEAD_OF_QUEUE: 967264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 968264274Smav break; 969264274Smav case CTL_TAG_UNTAGGED: 970264274Smav case CTL_TAG_SIMPLE: 971264274Smav case CTL_TAG_ACA: 972264274Smav default: 973264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 974264274Smav break; 975264274Smav } 976264274Smav 977267515Smav if (lbalen->flags & SWS_UNMAP) { 978267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 979267515Smav beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 980264274Smav beio->bio_cmd = BIO_DELETE; 981264274Smav beio->ds_trans_type = DEVSTAT_FREE; 982264274Smav 983264274Smav be_lun->unmap(be_lun, beio); 984264274Smav return; 985264274Smav } 986264274Smav 987264274Smav beio->bio_cmd = BIO_WRITE; 988264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 989264274Smav 990264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 991267515Smav (uintmax_t)lbalen->lba, lbalen->len); 992264274Smav 993267515Smav len_left = (uint64_t)lbalen->len * be_lun->blocksize; 994264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 995264274Smav 996264274Smav /* 997264274Smav * Setup the S/G entry for this chunk. 998264274Smav */ 999264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1000264274Smav seglen -= seglen % be_lun->blocksize; 1001264274Smav beio->sg_segs[i].len = seglen; 1002264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1003264274Smav 1004264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1005264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1006264274Smav 1007264274Smav beio->num_segs++; 1008264274Smav len_left -= seglen; 1009264274Smav 1010264274Smav buf = beio->sg_segs[i].addr; 1011264274Smav end = buf + seglen; 1012264274Smav for (; buf < end; buf += be_lun->blocksize) { 1013264274Smav memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1014267515Smav if (lbalen->flags & SWS_LBDATA) 1015267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1016264274Smav lba++; 1017264274Smav } 1018264274Smav } 1019264274Smav 1020267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1021264274Smav beio->io_len = lba * be_lun->blocksize; 1022264274Smav 1023264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1024264274Smav if (len_left > 0) { 1025267515Smav lbalen->lba += lba; 1026267515Smav lbalen->len -= lba; 1027264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1028264274Smav } 1029264274Smav 1030264274Smav be_lun->dispatch(be_lun, beio); 1031264274Smav} 1032264274Smav 1033264274Smavstatic void 1034264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1035264274Smav union ctl_io *io) 1036264274Smav{ 1037264274Smav struct ctl_be_block_io *beio; 1038264274Smav struct ctl_be_block_softc *softc; 1039267515Smav struct ctl_ptr_len_flags *ptrlen; 1040264274Smav 1041264274Smav DPRINTF("entered\n"); 1042264274Smav 1043267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1044264274Smav softc = be_lun->softc; 1045267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1046264274Smav 1047267515Smav if (ptrlen->flags != 0 || be_lun->unmap == NULL) { 1048264274Smav ctl_free_beio(beio); 1049264274Smav ctl_set_invalid_field(&io->scsiio, 1050264274Smav /*sks_valid*/ 0, 1051264274Smav /*command*/ 1, 1052264274Smav /*field*/ 0, 1053264274Smav /*bit_valid*/ 0, 1054264274Smav /*bit*/ 0); 1055264274Smav ctl_config_write_done(io); 1056264274Smav return; 1057264274Smav } 1058264274Smav 1059264274Smav /* 1060264274Smav * If the I/O came down with an ordered or head of queue tag, set 1061264274Smav * the BIO_ORDERED attribute. For head of queue tags, that's 1062264274Smav * pretty much the best we can do. 1063264274Smav */ 1064264274Smav if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1065264274Smav || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1066264274Smav beio->bio_flags = BIO_ORDERED; 1067264274Smav 1068264274Smav switch (io->scsiio.tag_type) { 1069264274Smav case CTL_TAG_ORDERED: 1070264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1071264274Smav break; 1072264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1073264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1074264274Smav break; 1075264274Smav case CTL_TAG_UNTAGGED: 1076264274Smav case CTL_TAG_SIMPLE: 1077264274Smav case CTL_TAG_ACA: 1078264274Smav default: 1079264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1080264274Smav break; 1081264274Smav } 1082264274Smav 1083264274Smav beio->io_len = 0; 1084264274Smav beio->io_offset = -1; 1085264274Smav 1086264274Smav beio->bio_cmd = BIO_DELETE; 1087264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1088264274Smav 1089267515Smav DPRINTF("UNMAP\n"); 1090264274Smav 1091264274Smav be_lun->unmap(be_lun, beio); 1092264274Smav} 1093264274Smav 1094264274Smavstatic void 1095264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1096264274Smav{ 1097264274Smav union ctl_io *io; 1098264274Smav 1099264274Smav io = beio->io; 1100264274Smav ctl_free_beio(beio); 1101264274Smav ctl_config_write_done(io); 1102264274Smav} 1103264274Smav 1104264274Smavstatic void 1105229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1106229997Sken union ctl_io *io) 1107229997Sken{ 1108229997Sken struct ctl_be_block_io *beio; 1109229997Sken struct ctl_be_block_softc *softc; 1110229997Sken 1111229997Sken DPRINTF("entered\n"); 1112229997Sken 1113229997Sken softc = be_lun->softc; 1114229997Sken beio = ctl_alloc_beio(softc); 1115229997Sken beio->io = io; 1116229997Sken beio->lun = be_lun; 1117264274Smav beio->beio_cont = ctl_be_block_cw_done; 1118267519Smav PRIV(io)->ptr = (void *)beio; 1119229997Sken 1120229997Sken switch (io->scsiio.cdb[0]) { 1121229997Sken case SYNCHRONIZE_CACHE: 1122229997Sken case SYNCHRONIZE_CACHE_16: 1123249194Strasz beio->bio_cmd = BIO_FLUSH; 1124229997Sken beio->ds_trans_type = DEVSTAT_NO_DATA; 1125229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1126229997Sken beio->io_len = 0; 1127229997Sken be_lun->lun_flush(be_lun, beio); 1128229997Sken break; 1129264274Smav case WRITE_SAME_10: 1130264274Smav case WRITE_SAME_16: 1131264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1132264274Smav break; 1133264274Smav case UNMAP: 1134264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1135264274Smav break; 1136229997Sken default: 1137229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1138229997Sken break; 1139229997Sken } 1140229997Sken} 1141229997Sken 1142258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1143258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1144258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1145258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1146229997Sken 1147229997Skenstatic void 1148264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1149264886Smav{ 1150264886Smav struct ctl_be_block_lun *be_lun; 1151264886Smav union ctl_io *io; 1152264886Smav 1153264886Smav io = beio->io; 1154264886Smav be_lun = beio->lun; 1155264886Smav ctl_free_beio(beio); 1156264886Smav if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 1157264886Smav && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1158264886Smav ctl_done(io); 1159264886Smav return; 1160264886Smav } 1161264886Smav 1162264886Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 1163264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1164264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1165264886Smav 1166264886Smav mtx_lock(&be_lun->lock); 1167264886Smav /* 1168264886Smav * XXX KDM make sure that links is okay to use at this point. 1169264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1170264886Smav * or deal with resource allocation here. 1171264886Smav */ 1172264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1173264886Smav mtx_unlock(&be_lun->lock); 1174264886Smav 1175264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1176264886Smav} 1177264886Smav 1178264886Smavstatic void 1179229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1180229997Sken union ctl_io *io) 1181229997Sken{ 1182229997Sken struct ctl_be_block_io *beio; 1183229997Sken struct ctl_be_block_softc *softc; 1184267515Smav struct ctl_lba_len *lbalen; 1185267519Smav struct ctl_ptr_len_flags *bptrlen; 1186267519Smav uint64_t len_left, lbas; 1187229997Sken int i; 1188229997Sken 1189229997Sken softc = be_lun->softc; 1190229997Sken 1191229997Sken DPRINTF("entered\n"); 1192229997Sken 1193229997Sken if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) { 1194229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1195229997Sken } else { 1196229997Sken SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1197229997Sken } 1198229997Sken 1199229997Sken beio = ctl_alloc_beio(softc); 1200229997Sken beio->io = io; 1201229997Sken beio->lun = be_lun; 1202267519Smav bptrlen = PRIV(io); 1203267519Smav bptrlen->ptr = (void *)beio; 1204229997Sken 1205229997Sken /* 1206229997Sken * If the I/O came down with an ordered or head of queue tag, set 1207229997Sken * the BIO_ORDERED attribute. For head of queue tags, that's 1208229997Sken * pretty much the best we can do. 1209229997Sken * 1210229997Sken * XXX KDM we don't have a great way to easily know about the FUA 1211229997Sken * bit right now (it is decoded in ctl_read_write(), but we don't 1212229997Sken * pass that knowledge to the backend), and in any case we would 1213229997Sken * need to determine how to handle it. 1214229997Sken */ 1215229997Sken if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1216229997Sken || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1217229997Sken beio->bio_flags = BIO_ORDERED; 1218229997Sken 1219229997Sken switch (io->scsiio.tag_type) { 1220229997Sken case CTL_TAG_ORDERED: 1221229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1222229997Sken break; 1223229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1224229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1225229997Sken break; 1226229997Sken case CTL_TAG_UNTAGGED: 1227229997Sken case CTL_TAG_SIMPLE: 1228229997Sken case CTL_TAG_ACA: 1229229997Sken default: 1230229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1231229997Sken break; 1232229997Sken } 1233229997Sken 1234229997Sken /* 1235229997Sken * This path handles read and write only. The config write path 1236229997Sken * handles flush operations. 1237229997Sken */ 1238229997Sken if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) { 1239229997Sken beio->bio_cmd = BIO_READ; 1240229997Sken beio->ds_trans_type = DEVSTAT_READ; 1241229997Sken } else { 1242229997Sken beio->bio_cmd = BIO_WRITE; 1243229997Sken beio->ds_trans_type = DEVSTAT_WRITE; 1244229997Sken } 1245229997Sken 1246267515Smav lbalen = (struct ctl_lba_len *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1247264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1248229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1249267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1250267519Smav lbas = MIN(lbalen->len - bptrlen->len, 1251267519Smav CTLBLK_MAX_IO_SIZE / be_lun->blocksize); 1252267519Smav beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1253267519Smav beio->io_len = lbas * be_lun->blocksize; 1254267519Smav bptrlen->len += lbas; 1255229997Sken 1256264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1257264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1258264886Smav i, CTLBLK_MAX_SEGS)); 1259229997Sken 1260229997Sken /* 1261229997Sken * Setup the S/G entry for this chunk. 1262229997Sken */ 1263264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1264229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1265229997Sken 1266229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1267229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1268229997Sken 1269229997Sken beio->num_segs++; 1270229997Sken len_left -= beio->sg_segs[i].len; 1271229997Sken } 1272267519Smav if (bptrlen->len < lbalen->len) 1273264886Smav beio->beio_cont = ctl_be_block_next; 1274264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1275264886Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1276264886Smav io->scsiio.kern_data_len = beio->io_len; 1277264886Smav io->scsiio.kern_data_resid = 0; 1278264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1279264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1280229997Sken 1281229997Sken /* 1282229997Sken * For the read case, we need to read the data into our buffers and 1283229997Sken * then we can send it back to the user. For the write case, we 1284229997Sken * need to get the data from the user first. 1285229997Sken */ 1286229997Sken if (beio->bio_cmd == BIO_READ) { 1287229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1288229997Sken be_lun->dispatch(be_lun, beio); 1289229997Sken } else { 1290229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1291229997Sken#ifdef CTL_TIME_IO 1292229997Sken getbintime(&io->io_hdr.dma_start_bt); 1293229997Sken#endif 1294229997Sken ctl_datamove(io); 1295229997Sken } 1296229997Sken} 1297229997Sken 1298229997Skenstatic void 1299229997Skenctl_be_block_worker(void *context, int pending) 1300229997Sken{ 1301229997Sken struct ctl_be_block_lun *be_lun; 1302229997Sken struct ctl_be_block_softc *softc; 1303229997Sken union ctl_io *io; 1304229997Sken 1305229997Sken be_lun = (struct ctl_be_block_lun *)context; 1306229997Sken softc = be_lun->softc; 1307229997Sken 1308229997Sken DPRINTF("entered\n"); 1309229997Sken 1310229997Sken mtx_lock(&be_lun->lock); 1311229997Sken for (;;) { 1312229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1313229997Sken if (io != NULL) { 1314229997Sken struct ctl_be_block_io *beio; 1315229997Sken 1316229997Sken DPRINTF("datamove queue\n"); 1317229997Sken 1318229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1319229997Sken ctl_io_hdr, links); 1320229997Sken 1321229997Sken mtx_unlock(&be_lun->lock); 1322229997Sken 1323267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1324229997Sken 1325229997Sken be_lun->dispatch(be_lun, beio); 1326229997Sken 1327229997Sken mtx_lock(&be_lun->lock); 1328229997Sken continue; 1329229997Sken } 1330229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1331229997Sken if (io != NULL) { 1332229997Sken 1333229997Sken DPRINTF("config write queue\n"); 1334229997Sken 1335229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1336229997Sken ctl_io_hdr, links); 1337229997Sken 1338229997Sken mtx_unlock(&be_lun->lock); 1339229997Sken 1340229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1341229997Sken 1342229997Sken mtx_lock(&be_lun->lock); 1343229997Sken continue; 1344229997Sken } 1345229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1346229997Sken if (io != NULL) { 1347229997Sken DPRINTF("input queue\n"); 1348229997Sken 1349229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1350229997Sken ctl_io_hdr, links); 1351229997Sken mtx_unlock(&be_lun->lock); 1352229997Sken 1353229997Sken /* 1354229997Sken * We must drop the lock, since this routine and 1355229997Sken * its children may sleep. 1356229997Sken */ 1357229997Sken ctl_be_block_dispatch(be_lun, io); 1358229997Sken 1359229997Sken mtx_lock(&be_lun->lock); 1360229997Sken continue; 1361229997Sken } 1362229997Sken 1363229997Sken /* 1364229997Sken * If we get here, there is no work left in the queues, so 1365229997Sken * just break out and let the task queue go to sleep. 1366229997Sken */ 1367229997Sken break; 1368229997Sken } 1369229997Sken mtx_unlock(&be_lun->lock); 1370229997Sken} 1371229997Sken 1372229997Sken/* 1373229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1374229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1375229997Sken * thread. 1376229997Sken */ 1377229997Skenstatic int 1378229997Skenctl_be_block_submit(union ctl_io *io) 1379229997Sken{ 1380229997Sken struct ctl_be_block_lun *be_lun; 1381229997Sken struct ctl_be_lun *ctl_be_lun; 1382229997Sken 1383229997Sken DPRINTF("entered\n"); 1384229997Sken 1385229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1386229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1387229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1388229997Sken 1389229997Sken /* 1390229997Sken * Make sure we only get SCSI I/O. 1391229997Sken */ 1392229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1393229997Sken "%#x) encountered", io->io_hdr.io_type)); 1394229997Sken 1395267519Smav PRIV(io)->len = 0; 1396267519Smav 1397229997Sken mtx_lock(&be_lun->lock); 1398229997Sken /* 1399229997Sken * XXX KDM make sure that links is okay to use at this point. 1400229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1401229997Sken * or deal with resource allocation here. 1402229997Sken */ 1403229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1404229997Sken mtx_unlock(&be_lun->lock); 1405229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1406229997Sken 1407267514Smav return (CTL_RETVAL_COMPLETE); 1408229997Sken} 1409229997Sken 1410229997Skenstatic int 1411229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1412229997Sken int flag, struct thread *td) 1413229997Sken{ 1414229997Sken struct ctl_be_block_softc *softc; 1415229997Sken int error; 1416229997Sken 1417229997Sken softc = &backend_block_softc; 1418229997Sken 1419229997Sken error = 0; 1420229997Sken 1421229997Sken switch (cmd) { 1422229997Sken case CTL_LUN_REQ: { 1423229997Sken struct ctl_lun_req *lun_req; 1424229997Sken 1425229997Sken lun_req = (struct ctl_lun_req *)addr; 1426229997Sken 1427229997Sken switch (lun_req->reqtype) { 1428229997Sken case CTL_LUNREQ_CREATE: 1429229997Sken error = ctl_be_block_create(softc, lun_req); 1430229997Sken break; 1431229997Sken case CTL_LUNREQ_RM: 1432229997Sken error = ctl_be_block_rm(softc, lun_req); 1433229997Sken break; 1434232604Strasz case CTL_LUNREQ_MODIFY: 1435232604Strasz error = ctl_be_block_modify(softc, lun_req); 1436232604Strasz break; 1437229997Sken default: 1438229997Sken lun_req->status = CTL_LUN_ERROR; 1439229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1440229997Sken "%s: invalid LUN request type %d", __func__, 1441229997Sken lun_req->reqtype); 1442229997Sken break; 1443229997Sken } 1444229997Sken break; 1445229997Sken } 1446229997Sken default: 1447229997Sken error = ENOTTY; 1448229997Sken break; 1449229997Sken } 1450229997Sken 1451229997Sken return (error); 1452229997Sken} 1453229997Sken 1454229997Skenstatic int 1455229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1456229997Sken{ 1457229997Sken struct ctl_be_block_filedata *file_data; 1458229997Sken struct ctl_lun_create_params *params; 1459229997Sken struct vattr vattr; 1460229997Sken int error; 1461229997Sken 1462229997Sken error = 0; 1463229997Sken file_data = &be_lun->backend.file; 1464229997Sken params = &req->reqdata.create; 1465229997Sken 1466229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1467229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1468229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1469229997Sken 1470229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1471229997Sken if (error != 0) { 1472229997Sken snprintf(req->error_str, sizeof(req->error_str), 1473229997Sken "error calling VOP_GETATTR() for file %s", 1474229997Sken be_lun->dev_path); 1475229997Sken return (error); 1476229997Sken } 1477229997Sken 1478229997Sken /* 1479229997Sken * Verify that we have the ability to upgrade to exclusive 1480229997Sken * access on this file so we can trap errors at open instead 1481229997Sken * of reporting them during first access. 1482229997Sken */ 1483229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1484229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1485229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1486229997Sken error = EBADF; 1487229997Sken snprintf(req->error_str, sizeof(req->error_str), 1488229997Sken "error locking file %s", be_lun->dev_path); 1489229997Sken return (error); 1490229997Sken } 1491229997Sken } 1492229997Sken 1493229997Sken 1494229997Sken file_data->cred = crhold(curthread->td_ucred); 1495232604Strasz if (params->lun_size_bytes != 0) 1496232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1497232604Strasz else 1498232604Strasz be_lun->size_bytes = vattr.va_size; 1499229997Sken /* 1500229997Sken * We set the multi thread flag for file operations because all 1501229997Sken * filesystems (in theory) are capable of allowing multiple readers 1502229997Sken * of a file at once. So we want to get the maximum possible 1503229997Sken * concurrency. 1504229997Sken */ 1505229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1506229997Sken 1507229997Sken /* 1508229997Sken * XXX KDM vattr.va_blocksize may be larger than 512 bytes here. 1509229997Sken * With ZFS, it is 131072 bytes. Block sizes that large don't work 1510229997Sken * with disklabel and UFS on FreeBSD at least. Large block sizes 1511229997Sken * may not work with other OSes as well. So just export a sector 1512229997Sken * size of 512 bytes, which should work with any OS or 1513229997Sken * application. Since our backing is a file, any block size will 1514229997Sken * work fine for the backing store. 1515229997Sken */ 1516229997Sken#if 0 1517229997Sken be_lun->blocksize= vattr.va_blocksize; 1518229997Sken#endif 1519229997Sken if (params->blocksize_bytes != 0) 1520229997Sken be_lun->blocksize = params->blocksize_bytes; 1521229997Sken else 1522229997Sken be_lun->blocksize = 512; 1523229997Sken 1524229997Sken /* 1525229997Sken * Sanity check. The media size has to be at least one 1526229997Sken * sector long. 1527229997Sken */ 1528229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1529229997Sken error = EINVAL; 1530229997Sken snprintf(req->error_str, sizeof(req->error_str), 1531229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1532229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1533229997Sken } 1534229997Sken return (error); 1535229997Sken} 1536229997Sken 1537229997Skenstatic int 1538229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1539229997Sken{ 1540229997Sken struct ctl_lun_create_params *params; 1541229997Sken struct vattr vattr; 1542229997Sken struct cdev *dev; 1543229997Sken struct cdevsw *devsw; 1544229997Sken int error; 1545264191Smav off_t ps, pss, po, pos; 1546229997Sken 1547229997Sken params = &req->reqdata.create; 1548229997Sken 1549229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1550229997Sken be_lun->dispatch = ctl_be_block_dispatch_dev; 1551229997Sken be_lun->lun_flush = ctl_be_block_flush_dev; 1552264274Smav be_lun->unmap = ctl_be_block_unmap_dev; 1553229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1554229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1555229997Sken &be_lun->backend.dev.dev_ref); 1556229997Sken if (be_lun->backend.dev.csw == NULL) 1557229997Sken panic("Unable to retrieve device switch"); 1558229997Sken 1559229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1560229997Sken if (error) { 1561229997Sken snprintf(req->error_str, sizeof(req->error_str), 1562229997Sken "%s: error getting vnode attributes for device %s", 1563229997Sken __func__, be_lun->dev_path); 1564229997Sken return (error); 1565229997Sken } 1566229997Sken 1567229997Sken dev = be_lun->vn->v_rdev; 1568229997Sken devsw = dev->si_devsw; 1569229997Sken if (!devsw->d_ioctl) { 1570229997Sken snprintf(req->error_str, sizeof(req->error_str), 1571229997Sken "%s: no d_ioctl for device %s!", __func__, 1572229997Sken be_lun->dev_path); 1573229997Sken return (ENODEV); 1574229997Sken } 1575229997Sken 1576229997Sken error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1577229997Sken (caddr_t)&be_lun->blocksize, FREAD, 1578229997Sken curthread); 1579229997Sken if (error) { 1580229997Sken snprintf(req->error_str, sizeof(req->error_str), 1581229997Sken "%s: error %d returned for DIOCGSECTORSIZE ioctl " 1582229997Sken "on %s!", __func__, error, be_lun->dev_path); 1583229997Sken return (error); 1584229997Sken } 1585229997Sken 1586229997Sken /* 1587229997Sken * If the user has asked for a blocksize that is greater than the 1588229997Sken * backing device's blocksize, we can do it only if the blocksize 1589229997Sken * the user is asking for is an even multiple of the underlying 1590229997Sken * device's blocksize. 1591229997Sken */ 1592229997Sken if ((params->blocksize_bytes != 0) 1593229997Sken && (params->blocksize_bytes > be_lun->blocksize)) { 1594229997Sken uint32_t bs_multiple, tmp_blocksize; 1595229997Sken 1596229997Sken bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1597229997Sken 1598229997Sken tmp_blocksize = bs_multiple * be_lun->blocksize; 1599229997Sken 1600229997Sken if (tmp_blocksize == params->blocksize_bytes) { 1601229997Sken be_lun->blocksize = params->blocksize_bytes; 1602229997Sken } else { 1603229997Sken snprintf(req->error_str, sizeof(req->error_str), 1604229997Sken "%s: requested blocksize %u is not an even " 1605229997Sken "multiple of backing device blocksize %u", 1606229997Sken __func__, params->blocksize_bytes, 1607229997Sken be_lun->blocksize); 1608229997Sken return (EINVAL); 1609229997Sken 1610229997Sken } 1611229997Sken } else if ((params->blocksize_bytes != 0) 1612229997Sken && (params->blocksize_bytes != be_lun->blocksize)) { 1613229997Sken snprintf(req->error_str, sizeof(req->error_str), 1614229997Sken "%s: requested blocksize %u < backing device " 1615229997Sken "blocksize %u", __func__, params->blocksize_bytes, 1616229997Sken be_lun->blocksize); 1617229997Sken return (EINVAL); 1618229997Sken } 1619229997Sken 1620229997Sken error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1621229997Sken (caddr_t)&be_lun->size_bytes, FREAD, 1622229997Sken curthread); 1623229997Sken if (error) { 1624229997Sken snprintf(req->error_str, sizeof(req->error_str), 1625232604Strasz "%s: error %d returned for DIOCGMEDIASIZE " 1626232604Strasz " ioctl on %s!", __func__, error, 1627232604Strasz be_lun->dev_path); 1628229997Sken return (error); 1629229997Sken } 1630229997Sken 1631232604Strasz if (params->lun_size_bytes != 0) { 1632232604Strasz if (params->lun_size_bytes > be_lun->size_bytes) { 1633232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1634232604Strasz "%s: requested LUN size %ju > backing device " 1635232604Strasz "size %ju", __func__, 1636232604Strasz (uintmax_t)params->lun_size_bytes, 1637232604Strasz (uintmax_t)be_lun->size_bytes); 1638232604Strasz return (EINVAL); 1639232604Strasz } 1640232604Strasz 1641232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1642232604Strasz } 1643232604Strasz 1644264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1645264191Smav (caddr_t)&ps, FREAD, curthread); 1646264191Smav if (error) 1647264191Smav ps = po = 0; 1648264191Smav else { 1649264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1650264191Smav (caddr_t)&po, FREAD, curthread); 1651264191Smav if (error) 1652264191Smav po = 0; 1653264191Smav } 1654264191Smav pss = ps / be_lun->blocksize; 1655264191Smav pos = po / be_lun->blocksize; 1656264191Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1657264191Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1658264191Smav be_lun->pblockexp = fls(pss) - 1; 1659264191Smav be_lun->pblockoff = (pss - pos) % pss; 1660264191Smav } 1661264191Smav 1662229997Sken return (0); 1663229997Sken} 1664229997Sken 1665229997Skenstatic int 1666229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 1667229997Sken{ 1668229997Sken DROP_GIANT(); 1669229997Sken if (be_lun->vn) { 1670229997Sken int flags = FREAD | FWRITE; 1671229997Sken 1672229997Sken switch (be_lun->dev_type) { 1673229997Sken case CTL_BE_BLOCK_DEV: 1674229997Sken if (be_lun->backend.dev.csw) { 1675229997Sken dev_relthread(be_lun->backend.dev.cdev, 1676229997Sken be_lun->backend.dev.dev_ref); 1677229997Sken be_lun->backend.dev.csw = NULL; 1678229997Sken be_lun->backend.dev.cdev = NULL; 1679229997Sken } 1680229997Sken break; 1681229997Sken case CTL_BE_BLOCK_FILE: 1682229997Sken break; 1683229997Sken case CTL_BE_BLOCK_NONE: 1684258871Strasz break; 1685229997Sken default: 1686229997Sken panic("Unexpected backend type."); 1687229997Sken break; 1688229997Sken } 1689229997Sken 1690229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1691229997Sken be_lun->vn = NULL; 1692229997Sken 1693229997Sken switch (be_lun->dev_type) { 1694229997Sken case CTL_BE_BLOCK_DEV: 1695229997Sken break; 1696229997Sken case CTL_BE_BLOCK_FILE: 1697229997Sken if (be_lun->backend.file.cred != NULL) { 1698229997Sken crfree(be_lun->backend.file.cred); 1699229997Sken be_lun->backend.file.cred = NULL; 1700229997Sken } 1701229997Sken break; 1702229997Sken case CTL_BE_BLOCK_NONE: 1703258871Strasz break; 1704229997Sken default: 1705229997Sken panic("Unexpected backend type."); 1706229997Sken break; 1707229997Sken } 1708229997Sken } 1709229997Sken PICKUP_GIANT(); 1710229997Sken 1711229997Sken return (0); 1712229997Sken} 1713229997Sken 1714229997Skenstatic int 1715229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 1716229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1717229997Sken{ 1718229997Sken struct nameidata nd; 1719229997Sken int flags; 1720229997Sken int error; 1721229997Sken 1722229997Sken /* 1723229997Sken * XXX KDM allow a read-only option? 1724229997Sken */ 1725229997Sken flags = FREAD | FWRITE; 1726229997Sken error = 0; 1727229997Sken 1728229997Sken if (rootvnode == NULL) { 1729229997Sken snprintf(req->error_str, sizeof(req->error_str), 1730229997Sken "%s: Root filesystem is not mounted", __func__); 1731229997Sken return (1); 1732229997Sken } 1733229997Sken 1734229997Sken if (!curthread->td_proc->p_fd->fd_cdir) { 1735229997Sken curthread->td_proc->p_fd->fd_cdir = rootvnode; 1736229997Sken VREF(rootvnode); 1737229997Sken } 1738229997Sken if (!curthread->td_proc->p_fd->fd_rdir) { 1739229997Sken curthread->td_proc->p_fd->fd_rdir = rootvnode; 1740229997Sken VREF(rootvnode); 1741229997Sken } 1742229997Sken if (!curthread->td_proc->p_fd->fd_jdir) { 1743229997Sken curthread->td_proc->p_fd->fd_jdir = rootvnode; 1744229997Sken VREF(rootvnode); 1745229997Sken } 1746229997Sken 1747229997Sken again: 1748229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1749229997Sken error = vn_open(&nd, &flags, 0, NULL); 1750229997Sken if (error) { 1751229997Sken /* 1752229997Sken * This is the only reasonable guess we can make as far as 1753229997Sken * path if the user doesn't give us a fully qualified path. 1754229997Sken * If they want to specify a file, they need to specify the 1755229997Sken * full path. 1756229997Sken */ 1757229997Sken if (be_lun->dev_path[0] != '/') { 1758229997Sken char *dev_path = "/dev/"; 1759229997Sken char *dev_name; 1760229997Sken 1761229997Sken /* Try adding device path at beginning of name */ 1762229997Sken dev_name = malloc(strlen(be_lun->dev_path) 1763229997Sken + strlen(dev_path) + 1, 1764229997Sken M_CTLBLK, M_WAITOK); 1765229997Sken if (dev_name) { 1766229997Sken sprintf(dev_name, "%s%s", dev_path, 1767229997Sken be_lun->dev_path); 1768229997Sken free(be_lun->dev_path, M_CTLBLK); 1769229997Sken be_lun->dev_path = dev_name; 1770229997Sken goto again; 1771229997Sken } 1772229997Sken } 1773229997Sken snprintf(req->error_str, sizeof(req->error_str), 1774229997Sken "%s: error opening %s", __func__, be_lun->dev_path); 1775229997Sken return (error); 1776229997Sken } 1777229997Sken 1778229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 1779229997Sken 1780229997Sken be_lun->vn = nd.ni_vp; 1781229997Sken 1782229997Sken /* We only support disks and files. */ 1783229997Sken if (vn_isdisk(be_lun->vn, &error)) { 1784229997Sken error = ctl_be_block_open_dev(be_lun, req); 1785229997Sken } else if (be_lun->vn->v_type == VREG) { 1786229997Sken error = ctl_be_block_open_file(be_lun, req); 1787229997Sken } else { 1788229997Sken error = EINVAL; 1789229997Sken snprintf(req->error_str, sizeof(req->error_str), 1790258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 1791229997Sken } 1792229997Sken VOP_UNLOCK(be_lun->vn, 0); 1793229997Sken 1794229997Sken if (error != 0) { 1795229997Sken ctl_be_block_close(be_lun); 1796229997Sken return (error); 1797229997Sken } 1798229997Sken 1799229997Sken be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1800229997Sken be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1801229997Sken 1802229997Sken return (0); 1803229997Sken} 1804229997Sken 1805229997Skenstatic int 1806229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1807229997Sken{ 1808229997Sken struct ctl_be_block_lun *be_lun; 1809229997Sken struct ctl_lun_create_params *params; 1810267481Smav char num_thread_str[16]; 1811229997Sken char tmpstr[32]; 1812267481Smav char *value; 1813264274Smav int retval, num_threads, unmap; 1814267481Smav int tmp_num_threads; 1815229997Sken 1816229997Sken params = &req->reqdata.create; 1817229997Sken retval = 0; 1818229997Sken 1819229997Sken num_threads = cbb_num_threads; 1820229997Sken 1821229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1822229997Sken 1823229997Sken be_lun->softc = softc; 1824229997Sken STAILQ_INIT(&be_lun->input_queue); 1825229997Sken STAILQ_INIT(&be_lun->config_write_queue); 1826229997Sken STAILQ_INIT(&be_lun->datamove_queue); 1827229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1828229997Sken mtx_init(&be_lun->lock, be_lun->lunname, NULL, MTX_DEF); 1829267481Smav ctl_init_opts(&be_lun->ctl_be_lun, req); 1830229997Sken 1831264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1832256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1833229997Sken 1834229997Sken if (be_lun->lun_zone == NULL) { 1835229997Sken snprintf(req->error_str, sizeof(req->error_str), 1836229997Sken "%s: error allocating UMA zone", __func__); 1837229997Sken goto bailout_error; 1838229997Sken } 1839229997Sken 1840229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1841229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 1842229997Sken else 1843229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 1844229997Sken 1845229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1846267499Smav value = ctl_get_opt(&be_lun->ctl_be_lun, "file"); 1847267499Smav if (value == NULL) { 1848229997Sken snprintf(req->error_str, sizeof(req->error_str), 1849229997Sken "%s: no file argument specified", __func__); 1850229997Sken goto bailout_error; 1851229997Sken } 1852267499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 1853229997Sken 1854229997Sken retval = ctl_be_block_open(softc, be_lun, req); 1855229997Sken if (retval != 0) { 1856229997Sken retval = 0; 1857229997Sken goto bailout_error; 1858229997Sken } 1859229997Sken 1860229997Sken /* 1861229997Sken * Tell the user the size of the file/device. 1862229997Sken */ 1863229997Sken params->lun_size_bytes = be_lun->size_bytes; 1864229997Sken 1865229997Sken /* 1866229997Sken * The maximum LBA is the size - 1. 1867229997Sken */ 1868229997Sken be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 1869229997Sken } else { 1870229997Sken /* 1871229997Sken * For processor devices, we don't have any size. 1872229997Sken */ 1873229997Sken be_lun->blocksize = 0; 1874264191Smav be_lun->pblockexp = 0; 1875264191Smav be_lun->pblockoff = 0; 1876229997Sken be_lun->size_blocks = 0; 1877229997Sken be_lun->size_bytes = 0; 1878229997Sken be_lun->ctl_be_lun.maxlba = 0; 1879229997Sken params->lun_size_bytes = 0; 1880229997Sken 1881229997Sken /* 1882229997Sken * Default to just 1 thread for processor devices. 1883229997Sken */ 1884229997Sken num_threads = 1; 1885229997Sken } 1886229997Sken 1887229997Sken /* 1888229997Sken * XXX This searching loop might be refactored to be combined with 1889229997Sken * the loop above, 1890229997Sken */ 1891267481Smav value = ctl_get_opt(&be_lun->ctl_be_lun, "num_threads"); 1892267481Smav if (value != NULL) { 1893267481Smav tmp_num_threads = strtol(value, NULL, 0); 1894229997Sken 1895267481Smav /* 1896267481Smav * We don't let the user specify less than one 1897267481Smav * thread, but hope he's clueful enough not to 1898267481Smav * specify 1000 threads. 1899267481Smav */ 1900267481Smav if (tmp_num_threads < 1) { 1901267481Smav snprintf(req->error_str, sizeof(req->error_str), 1902267481Smav "%s: invalid number of threads %s", 1903267481Smav __func__, num_thread_str); 1904267481Smav goto bailout_error; 1905229997Sken } 1906267481Smav num_threads = tmp_num_threads; 1907229997Sken } 1908267481Smav unmap = 0; 1909267481Smav value = ctl_get_opt(&be_lun->ctl_be_lun, "unmap"); 1910267481Smav if (value != NULL && strcmp(value, "on") == 0) 1911267481Smav unmap = 1; 1912229997Sken 1913229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 1914229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 1915264274Smav if (unmap) 1916264274Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 1917229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 1918229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 1919264191Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 1920264191Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 1921229997Sken /* Tell the user the blocksize we ended up using */ 1922229997Sken params->blocksize_bytes = be_lun->blocksize; 1923229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 1924229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 1925229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 1926229997Sken } else 1927229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 1928229997Sken 1929229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 1930229997Sken be_lun->ctl_be_lun.lun_config_status = 1931229997Sken ctl_be_block_lun_config_status; 1932229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 1933229997Sken 1934229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 1935229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 1936229997Sken softc->num_luns); 1937229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 1938229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1939229997Sken sizeof(tmpstr))); 1940229997Sken 1941229997Sken /* Tell the user what we used for a serial number */ 1942229997Sken strncpy((char *)params->serial_num, tmpstr, 1943229997Sken ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 1944229997Sken } else { 1945229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 1946229997Sken params->serial_num, 1947229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1948229997Sken sizeof(params->serial_num))); 1949229997Sken } 1950229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 1951229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 1952229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 1953229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1954229997Sken sizeof(tmpstr))); 1955229997Sken 1956229997Sken /* Tell the user what we used for a device ID */ 1957229997Sken strncpy((char *)params->device_id, tmpstr, 1958229997Sken ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 1959229997Sken } else { 1960229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 1961229997Sken params->device_id, 1962229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1963229997Sken sizeof(params->device_id))); 1964229997Sken } 1965229997Sken 1966229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 1967229997Sken 1968229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 1969229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 1970229997Sken 1971229997Sken if (be_lun->io_taskqueue == NULL) { 1972229997Sken snprintf(req->error_str, sizeof(req->error_str), 1973229997Sken "%s: Unable to create taskqueue", __func__); 1974229997Sken goto bailout_error; 1975229997Sken } 1976229997Sken 1977229997Sken /* 1978229997Sken * Note that we start the same number of threads by default for 1979229997Sken * both the file case and the block device case. For the file 1980229997Sken * case, we need multiple threads to allow concurrency, because the 1981229997Sken * vnode interface is designed to be a blocking interface. For the 1982229997Sken * block device case, ZFS zvols at least will block the caller's 1983229997Sken * context in many instances, and so we need multiple threads to 1984229997Sken * overcome that problem. Other block devices don't need as many 1985229997Sken * threads, but they shouldn't cause too many problems. 1986229997Sken * 1987229997Sken * If the user wants to just have a single thread for a block 1988229997Sken * device, he can specify that when the LUN is created, or change 1989229997Sken * the tunable/sysctl to alter the default number of threads. 1990229997Sken */ 1991229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 1992229997Sken /*num threads*/num_threads, 1993229997Sken /*priority*/PWAIT, 1994229997Sken /*thread name*/ 1995229997Sken "%s taskq", be_lun->lunname); 1996229997Sken 1997229997Sken if (retval != 0) 1998229997Sken goto bailout_error; 1999229997Sken 2000229997Sken be_lun->num_threads = num_threads; 2001229997Sken 2002229997Sken mtx_lock(&softc->lock); 2003229997Sken softc->num_luns++; 2004229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2005229997Sken 2006229997Sken mtx_unlock(&softc->lock); 2007229997Sken 2008229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 2009229997Sken if (retval != 0) { 2010229997Sken mtx_lock(&softc->lock); 2011229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2012229997Sken links); 2013229997Sken softc->num_luns--; 2014229997Sken mtx_unlock(&softc->lock); 2015229997Sken snprintf(req->error_str, sizeof(req->error_str), 2016229997Sken "%s: ctl_add_lun() returned error %d, see dmesg for " 2017229997Sken "details", __func__, retval); 2018229997Sken retval = 0; 2019229997Sken goto bailout_error; 2020229997Sken } 2021229997Sken 2022229997Sken mtx_lock(&softc->lock); 2023229997Sken 2024229997Sken /* 2025229997Sken * Tell the config_status routine that we're waiting so it won't 2026229997Sken * clean up the LUN in the event of an error. 2027229997Sken */ 2028229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2029229997Sken 2030229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2031229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2032229997Sken if (retval == EINTR) 2033229997Sken break; 2034229997Sken } 2035229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2036229997Sken 2037229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2038229997Sken snprintf(req->error_str, sizeof(req->error_str), 2039229997Sken "%s: LUN configuration error, see dmesg for details", 2040229997Sken __func__); 2041229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2042229997Sken links); 2043229997Sken softc->num_luns--; 2044229997Sken mtx_unlock(&softc->lock); 2045229997Sken goto bailout_error; 2046229997Sken } else { 2047229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2048229997Sken } 2049229997Sken 2050229997Sken mtx_unlock(&softc->lock); 2051229997Sken 2052229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2053229997Sken be_lun->blocksize, 2054229997Sken DEVSTAT_ALL_SUPPORTED, 2055229997Sken be_lun->ctl_be_lun.lun_type 2056229997Sken | DEVSTAT_TYPE_IF_OTHER, 2057229997Sken DEVSTAT_PRIORITY_OTHER); 2058229997Sken 2059229997Sken 2060229997Sken req->status = CTL_LUN_OK; 2061229997Sken 2062229997Sken return (retval); 2063229997Sken 2064229997Skenbailout_error: 2065229997Sken req->status = CTL_LUN_ERROR; 2066229997Sken 2067267429Smav if (be_lun->io_taskqueue != NULL) 2068267429Smav taskqueue_free(be_lun->io_taskqueue); 2069229997Sken ctl_be_block_close(be_lun); 2070267429Smav if (be_lun->dev_path != NULL) 2071267429Smav free(be_lun->dev_path, M_CTLBLK); 2072267429Smav if (be_lun->lun_zone != NULL) 2073267429Smav uma_zdestroy(be_lun->lun_zone); 2074267481Smav ctl_free_opts(&be_lun->ctl_be_lun); 2075267429Smav mtx_destroy(&be_lun->lock); 2076229997Sken free(be_lun, M_CTLBLK); 2077229997Sken 2078229997Sken return (retval); 2079229997Sken} 2080229997Sken 2081229997Skenstatic int 2082229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2083229997Sken{ 2084229997Sken struct ctl_lun_rm_params *params; 2085229997Sken struct ctl_be_block_lun *be_lun; 2086229997Sken int retval; 2087229997Sken 2088229997Sken params = &req->reqdata.rm; 2089229997Sken 2090229997Sken mtx_lock(&softc->lock); 2091229997Sken 2092229997Sken be_lun = NULL; 2093229997Sken 2094229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2095229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2096229997Sken break; 2097229997Sken } 2098229997Sken mtx_unlock(&softc->lock); 2099229997Sken 2100229997Sken if (be_lun == NULL) { 2101229997Sken snprintf(req->error_str, sizeof(req->error_str), 2102229997Sken "%s: LUN %u is not managed by the block backend", 2103229997Sken __func__, params->lun_id); 2104229997Sken goto bailout_error; 2105229997Sken } 2106229997Sken 2107229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2108229997Sken 2109229997Sken if (retval != 0) { 2110229997Sken snprintf(req->error_str, sizeof(req->error_str), 2111229997Sken "%s: error %d returned from ctl_disable_lun() for " 2112229997Sken "LUN %d", __func__, retval, params->lun_id); 2113229997Sken goto bailout_error; 2114229997Sken 2115229997Sken } 2116229997Sken 2117229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2118229997Sken if (retval != 0) { 2119229997Sken snprintf(req->error_str, sizeof(req->error_str), 2120229997Sken "%s: error %d returned from ctl_invalidate_lun() for " 2121229997Sken "LUN %d", __func__, retval, params->lun_id); 2122229997Sken goto bailout_error; 2123229997Sken } 2124229997Sken 2125229997Sken mtx_lock(&softc->lock); 2126229997Sken 2127229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2128229997Sken 2129229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2130229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2131229997Sken if (retval == EINTR) 2132229997Sken break; 2133229997Sken } 2134229997Sken 2135229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2136229997Sken 2137229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2138229997Sken snprintf(req->error_str, sizeof(req->error_str), 2139229997Sken "%s: interrupted waiting for LUN to be freed", 2140229997Sken __func__); 2141229997Sken mtx_unlock(&softc->lock); 2142229997Sken goto bailout_error; 2143229997Sken } 2144229997Sken 2145229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2146229997Sken 2147229997Sken softc->num_luns--; 2148229997Sken mtx_unlock(&softc->lock); 2149229997Sken 2150229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2151229997Sken 2152229997Sken taskqueue_free(be_lun->io_taskqueue); 2153229997Sken 2154229997Sken ctl_be_block_close(be_lun); 2155229997Sken 2156229997Sken if (be_lun->disk_stats != NULL) 2157229997Sken devstat_remove_entry(be_lun->disk_stats); 2158229997Sken 2159229997Sken uma_zdestroy(be_lun->lun_zone); 2160229997Sken 2161267481Smav ctl_free_opts(&be_lun->ctl_be_lun); 2162229997Sken free(be_lun->dev_path, M_CTLBLK); 2163229997Sken 2164229997Sken free(be_lun, M_CTLBLK); 2165229997Sken 2166229997Sken req->status = CTL_LUN_OK; 2167229997Sken 2168229997Sken return (0); 2169229997Sken 2170229997Skenbailout_error: 2171229997Sken 2172229997Sken req->status = CTL_LUN_ERROR; 2173229997Sken 2174229997Sken return (0); 2175229997Sken} 2176229997Sken 2177232604Straszstatic int 2178232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2179232604Strasz struct ctl_lun_req *req) 2180232604Strasz{ 2181232604Strasz struct vattr vattr; 2182232604Strasz int error; 2183232604Strasz struct ctl_lun_modify_params *params; 2184232604Strasz 2185232604Strasz params = &req->reqdata.modify; 2186232604Strasz 2187232604Strasz if (params->lun_size_bytes != 0) { 2188232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2189232604Strasz } else { 2190232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2191232604Strasz if (error != 0) { 2192232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2193232604Strasz "error calling VOP_GETATTR() for file %s", 2194232604Strasz be_lun->dev_path); 2195232604Strasz return (error); 2196232604Strasz } 2197232604Strasz 2198232604Strasz be_lun->size_bytes = vattr.va_size; 2199232604Strasz } 2200232604Strasz 2201232604Strasz return (0); 2202232604Strasz} 2203232604Strasz 2204232604Straszstatic int 2205232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2206232604Strasz struct ctl_lun_req *req) 2207232604Strasz{ 2208232604Strasz struct cdev *dev; 2209232604Strasz struct cdevsw *devsw; 2210232604Strasz int error; 2211232604Strasz struct ctl_lun_modify_params *params; 2212232604Strasz uint64_t size_bytes; 2213232604Strasz 2214232604Strasz params = &req->reqdata.modify; 2215232604Strasz 2216232604Strasz dev = be_lun->vn->v_rdev; 2217232604Strasz devsw = dev->si_devsw; 2218232604Strasz if (!devsw->d_ioctl) { 2219232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2220232604Strasz "%s: no d_ioctl for device %s!", __func__, 2221232604Strasz be_lun->dev_path); 2222232604Strasz return (ENODEV); 2223232604Strasz } 2224232604Strasz 2225232604Strasz error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 2226232604Strasz (caddr_t)&size_bytes, FREAD, 2227232604Strasz curthread); 2228232604Strasz if (error) { 2229232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2230232604Strasz "%s: error %d returned for DIOCGMEDIASIZE ioctl " 2231232604Strasz "on %s!", __func__, error, be_lun->dev_path); 2232232604Strasz return (error); 2233232604Strasz } 2234232604Strasz 2235232604Strasz if (params->lun_size_bytes != 0) { 2236232604Strasz if (params->lun_size_bytes > size_bytes) { 2237232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2238232604Strasz "%s: requested LUN size %ju > backing device " 2239232604Strasz "size %ju", __func__, 2240232604Strasz (uintmax_t)params->lun_size_bytes, 2241232604Strasz (uintmax_t)size_bytes); 2242232604Strasz return (EINVAL); 2243232604Strasz } 2244232604Strasz 2245232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2246232604Strasz } else { 2247232604Strasz be_lun->size_bytes = size_bytes; 2248232604Strasz } 2249232604Strasz 2250232604Strasz return (0); 2251232604Strasz} 2252232604Strasz 2253232604Straszstatic int 2254232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2255232604Strasz{ 2256232604Strasz struct ctl_lun_modify_params *params; 2257232604Strasz struct ctl_be_block_lun *be_lun; 2258241896Skib int error; 2259232604Strasz 2260232604Strasz params = &req->reqdata.modify; 2261232604Strasz 2262232604Strasz mtx_lock(&softc->lock); 2263232604Strasz 2264232604Strasz be_lun = NULL; 2265232604Strasz 2266232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2267232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2268232604Strasz break; 2269232604Strasz } 2270232604Strasz mtx_unlock(&softc->lock); 2271232604Strasz 2272232604Strasz if (be_lun == NULL) { 2273232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2274232604Strasz "%s: LUN %u is not managed by the block backend", 2275232604Strasz __func__, params->lun_id); 2276232604Strasz goto bailout_error; 2277232604Strasz } 2278232604Strasz 2279232604Strasz if (params->lun_size_bytes != 0) { 2280232604Strasz if (params->lun_size_bytes < be_lun->blocksize) { 2281232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2282232604Strasz "%s: LUN size %ju < blocksize %u", __func__, 2283232604Strasz params->lun_size_bytes, be_lun->blocksize); 2284232604Strasz goto bailout_error; 2285232604Strasz } 2286232604Strasz } 2287232604Strasz 2288232604Strasz vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2289232604Strasz 2290232604Strasz if (be_lun->vn->v_type == VREG) 2291232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2292232604Strasz else 2293232604Strasz error = ctl_be_block_modify_dev(be_lun, req); 2294232604Strasz 2295232604Strasz VOP_UNLOCK(be_lun->vn, 0); 2296232604Strasz 2297232604Strasz if (error != 0) 2298232604Strasz goto bailout_error; 2299232604Strasz 2300232604Strasz be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2301232604Strasz 2302232604Strasz /* 2303232604Strasz * The maximum LBA is the size - 1. 2304232604Strasz * 2305232604Strasz * XXX: Note that this field is being updated without locking, 2306232604Strasz * which might cause problems on 32-bit architectures. 2307232604Strasz */ 2308232604Strasz be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 2309232604Strasz ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2310232604Strasz 2311232604Strasz /* Tell the user the exact size we ended up using */ 2312232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2313232604Strasz 2314232604Strasz req->status = CTL_LUN_OK; 2315232604Strasz 2316232604Strasz return (0); 2317232604Strasz 2318232604Straszbailout_error: 2319232604Strasz req->status = CTL_LUN_ERROR; 2320232604Strasz 2321232604Strasz return (0); 2322232604Strasz} 2323232604Strasz 2324229997Skenstatic void 2325229997Skenctl_be_block_lun_shutdown(void *be_lun) 2326229997Sken{ 2327229997Sken struct ctl_be_block_lun *lun; 2328229997Sken struct ctl_be_block_softc *softc; 2329229997Sken 2330229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2331229997Sken 2332229997Sken softc = lun->softc; 2333229997Sken 2334229997Sken mtx_lock(&softc->lock); 2335229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2336229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2337229997Sken wakeup(lun); 2338229997Sken mtx_unlock(&softc->lock); 2339229997Sken 2340229997Sken} 2341229997Sken 2342229997Skenstatic void 2343229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2344229997Sken{ 2345229997Sken struct ctl_be_block_lun *lun; 2346229997Sken struct ctl_be_block_softc *softc; 2347229997Sken 2348229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2349229997Sken softc = lun->softc; 2350229997Sken 2351229997Sken if (status == CTL_LUN_CONFIG_OK) { 2352229997Sken mtx_lock(&softc->lock); 2353229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2354229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2355229997Sken wakeup(lun); 2356229997Sken mtx_unlock(&softc->lock); 2357229997Sken 2358229997Sken /* 2359229997Sken * We successfully added the LUN, attempt to enable it. 2360229997Sken */ 2361229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2362229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2363229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2364229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2365229997Sken __func__); 2366229997Sken } 2367229997Sken } 2368229997Sken 2369229997Sken return; 2370229997Sken } 2371229997Sken 2372229997Sken 2373229997Sken mtx_lock(&softc->lock); 2374229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2375229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2376229997Sken wakeup(lun); 2377229997Sken mtx_unlock(&softc->lock); 2378229997Sken} 2379229997Sken 2380229997Sken 2381229997Skenstatic int 2382229997Skenctl_be_block_config_write(union ctl_io *io) 2383229997Sken{ 2384229997Sken struct ctl_be_block_lun *be_lun; 2385229997Sken struct ctl_be_lun *ctl_be_lun; 2386229997Sken int retval; 2387229997Sken 2388229997Sken retval = 0; 2389229997Sken 2390229997Sken DPRINTF("entered\n"); 2391229997Sken 2392229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2393229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2394229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2395229997Sken 2396229997Sken switch (io->scsiio.cdb[0]) { 2397229997Sken case SYNCHRONIZE_CACHE: 2398229997Sken case SYNCHRONIZE_CACHE_16: 2399264274Smav case WRITE_SAME_10: 2400264274Smav case WRITE_SAME_16: 2401264274Smav case UNMAP: 2402229997Sken /* 2403229997Sken * The upper level CTL code will filter out any CDBs with 2404229997Sken * the immediate bit set and return the proper error. 2405229997Sken * 2406229997Sken * We don't really need to worry about what LBA range the 2407229997Sken * user asked to be synced out. When they issue a sync 2408229997Sken * cache command, we'll sync out the whole thing. 2409229997Sken */ 2410229997Sken mtx_lock(&be_lun->lock); 2411229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2412229997Sken links); 2413229997Sken mtx_unlock(&be_lun->lock); 2414229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2415229997Sken break; 2416229997Sken case START_STOP_UNIT: { 2417229997Sken struct scsi_start_stop_unit *cdb; 2418229997Sken 2419229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2420229997Sken 2421229997Sken if (cdb->how & SSS_START) 2422229997Sken retval = ctl_start_lun(ctl_be_lun); 2423229997Sken else { 2424229997Sken retval = ctl_stop_lun(ctl_be_lun); 2425229997Sken /* 2426229997Sken * XXX KDM Copan-specific offline behavior. 2427229997Sken * Figure out a reasonable way to port this? 2428229997Sken */ 2429229997Sken#ifdef NEEDTOPORT 2430229997Sken if ((retval == 0) 2431229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2432229997Sken retval = ctl_lun_offline(ctl_be_lun); 2433229997Sken#endif 2434229997Sken } 2435229997Sken 2436229997Sken /* 2437229997Sken * In general, the above routines should not fail. They 2438229997Sken * just set state for the LUN. So we've got something 2439229997Sken * pretty wrong here if we can't start or stop the LUN. 2440229997Sken */ 2441229997Sken if (retval != 0) { 2442229997Sken ctl_set_internal_failure(&io->scsiio, 2443229997Sken /*sks_valid*/ 1, 2444229997Sken /*retry_count*/ 0xf051); 2445229997Sken retval = CTL_RETVAL_COMPLETE; 2446229997Sken } else { 2447229997Sken ctl_set_success(&io->scsiio); 2448229997Sken } 2449229997Sken ctl_config_write_done(io); 2450229997Sken break; 2451229997Sken } 2452229997Sken default: 2453229997Sken ctl_set_invalid_opcode(&io->scsiio); 2454229997Sken ctl_config_write_done(io); 2455229997Sken retval = CTL_RETVAL_COMPLETE; 2456229997Sken break; 2457229997Sken } 2458229997Sken 2459229997Sken return (retval); 2460229997Sken 2461229997Sken} 2462229997Sken 2463229997Skenstatic int 2464229997Skenctl_be_block_config_read(union ctl_io *io) 2465229997Sken{ 2466229997Sken return (0); 2467229997Sken} 2468229997Sken 2469229997Skenstatic int 2470229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2471229997Sken{ 2472229997Sken struct ctl_be_block_lun *lun; 2473229997Sken int retval; 2474229997Sken 2475229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2476229997Sken retval = 0; 2477229997Sken 2478229997Sken retval = sbuf_printf(sb, "<num_threads>"); 2479229997Sken 2480229997Sken if (retval != 0) 2481229997Sken goto bailout; 2482229997Sken 2483229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2484229997Sken 2485229997Sken if (retval != 0) 2486229997Sken goto bailout; 2487229997Sken 2488229997Sken retval = sbuf_printf(sb, "</num_threads>"); 2489229997Sken 2490229997Skenbailout: 2491229997Sken 2492229997Sken return (retval); 2493229997Sken} 2494229997Sken 2495229997Skenint 2496229997Skenctl_be_block_init(void) 2497229997Sken{ 2498229997Sken struct ctl_be_block_softc *softc; 2499229997Sken int retval; 2500229997Sken 2501229997Sken softc = &backend_block_softc; 2502229997Sken retval = 0; 2503229997Sken 2504229997Sken mtx_init(&softc->lock, "ctlblk", NULL, MTX_DEF); 2505264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2506264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2507229997Sken STAILQ_INIT(&softc->disk_list); 2508229997Sken STAILQ_INIT(&softc->lun_list); 2509229997Sken 2510229997Sken return (retval); 2511229997Sken} 2512