ctl_backend_block.c revision 287500
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 287500 2015-09-06 11:23:01Z 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> 71275474Smav#include <sys/filio.h> 72229997Sken#include <sys/proc.h> 73229997Sken#include <sys/pcpu.h> 74229997Sken#include <sys/module.h> 75229997Sken#include <sys/sdt.h> 76229997Sken#include <sys/devicestat.h> 77229997Sken#include <sys/sysctl.h> 78229997Sken 79229997Sken#include <geom/geom.h> 80229997Sken 81229997Sken#include <cam/cam.h> 82229997Sken#include <cam/scsi/scsi_all.h> 83229997Sken#include <cam/scsi/scsi_da.h> 84229997Sken#include <cam/ctl/ctl_io.h> 85229997Sken#include <cam/ctl/ctl.h> 86229997Sken#include <cam/ctl/ctl_backend.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 */ 95267537Smav#define CTLBLK_HALF_IO_SIZE (512 * 1024) 96267537Smav#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 97264886Smav#define CTLBLK_MAX_SEG MAXPHYS 98267537Smav#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 99267537Smav#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 100229997Sken 101229997Sken#ifdef CTLBLK_DEBUG 102229997Sken#define DPRINTF(fmt, args...) \ 103229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 104229997Sken#else 105229997Sken#define DPRINTF(fmt, args...) do {} while(0) 106229997Sken#endif 107229997Sken 108267519Smav#define PRIV(io) \ 109267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 110267537Smav#define ARGS(io) \ 111267537Smav ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 112267519Smav 113229997SkenSDT_PROVIDER_DEFINE(cbb); 114229997Sken 115229997Skentypedef enum { 116229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 117229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 118229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 119229997Sken} ctl_be_block_lun_flags; 120229997Sken 121229997Skentypedef enum { 122229997Sken CTL_BE_BLOCK_NONE, 123229997Sken CTL_BE_BLOCK_DEV, 124229997Sken CTL_BE_BLOCK_FILE 125229997Sken} ctl_be_block_type; 126229997Sken 127229997Skenstruct ctl_be_block_devdata { 128229997Sken struct cdev *cdev; 129229997Sken struct cdevsw *csw; 130229997Sken int dev_ref; 131229997Sken}; 132229997Sken 133229997Skenstruct ctl_be_block_filedata { 134229997Sken struct ucred *cred; 135229997Sken}; 136229997Sken 137229997Skenunion ctl_be_block_bedata { 138229997Sken struct ctl_be_block_devdata dev; 139229997Sken struct ctl_be_block_filedata file; 140229997Sken}; 141229997Sken 142229997Skenstruct ctl_be_block_io; 143229997Skenstruct ctl_be_block_lun; 144229997Sken 145229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 146229997Sken struct ctl_be_block_io *beio); 147274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 148274154Smav const char *attrname); 149229997Sken 150229997Sken/* 151229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 152229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 153229997Sken */ 154229997Skenstruct ctl_be_block_lun { 155272911Smav struct ctl_lun_create_params params; 156229997Sken char lunname[32]; 157229997Sken char *dev_path; 158229997Sken ctl_be_block_type dev_type; 159229997Sken struct vnode *vn; 160229997Sken union ctl_be_block_bedata backend; 161229997Sken cbb_dispatch_t dispatch; 162229997Sken cbb_dispatch_t lun_flush; 163264274Smav cbb_dispatch_t unmap; 164275474Smav cbb_dispatch_t get_lba_status; 165274154Smav cbb_getattr_t getattr; 166229997Sken uma_zone_t lun_zone; 167229997Sken uint64_t size_blocks; 168229997Sken uint64_t size_bytes; 169229997Sken struct ctl_be_block_softc *softc; 170229997Sken struct devstat *disk_stats; 171229997Sken ctl_be_block_lun_flags flags; 172229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 173287499Smav struct ctl_be_lun cbe_lun; 174229997Sken struct taskqueue *io_taskqueue; 175229997Sken struct task io_task; 176229997Sken int num_threads; 177229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 178275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 179229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 180229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 181267877Smav struct mtx_padalign io_lock; 182267877Smav struct mtx_padalign queue_lock; 183229997Sken}; 184229997Sken 185229997Sken/* 186229997Sken * Overall softc structure for the block backend module. 187229997Sken */ 188229997Skenstruct ctl_be_block_softc { 189229997Sken struct mtx lock; 190229997Sken int num_luns; 191229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 192229997Sken}; 193229997Sken 194229997Skenstatic struct ctl_be_block_softc backend_block_softc; 195229997Sken 196229997Sken/* 197229997Sken * Per-I/O information. 198229997Sken */ 199229997Skenstruct ctl_be_block_io { 200229997Sken union ctl_io *io; 201229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 202229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 203229997Sken int bio_cmd; 204229997Sken int num_segs; 205229997Sken int num_bios_sent; 206229997Sken int num_bios_done; 207229997Sken int send_complete; 208229997Sken int num_errors; 209229997Sken struct bintime ds_t0; 210229997Sken devstat_tag_type ds_tag_type; 211229997Sken devstat_trans_flags ds_trans_type; 212229997Sken uint64_t io_len; 213229997Sken uint64_t io_offset; 214286353Smav int io_arg; 215229997Sken struct ctl_be_block_softc *softc; 216229997Sken struct ctl_be_block_lun *lun; 217264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 218229997Sken}; 219229997Sken 220229997Skenstatic int cbb_num_threads = 14; 221229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 222229997Sken "CAM Target Layer Block Backend"); 223267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 224229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 225229997Sken 226229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 227229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 228229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 229229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 230229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 231229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 232229997Sken struct ctl_be_block_io *beio); 233229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 234229997Sken struct ctl_be_block_io *beio); 235275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 236275474Smav struct ctl_be_block_io *beio); 237275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 238275481Smav const char *attrname); 239229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 240229997Sken struct ctl_be_block_io *beio); 241264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 242264274Smav struct ctl_be_block_io *beio); 243229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 244229997Sken struct ctl_be_block_io *beio); 245274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 246274154Smav const char *attrname); 247275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 248275474Smav union ctl_io *io); 249229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 250229997Sken union ctl_io *io); 251229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 252229997Sken union ctl_io *io); 253229997Skenstatic void ctl_be_block_worker(void *context, int pending); 254229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 255229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 256229997Sken int flag, struct thread *td); 257229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 258229997Sken struct ctl_lun_req *req); 259229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 260229997Sken struct ctl_lun_req *req); 261229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 262229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 263229997Sken struct ctl_be_block_lun *be_lun, 264229997Sken struct ctl_lun_req *req); 265229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 266229997Sken struct ctl_lun_req *req); 267229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 268229997Sken struct ctl_lun_req *req); 269232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 270232604Strasz struct ctl_lun_req *req); 271232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 272232604Strasz struct ctl_lun_req *req); 273232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 274232604Strasz struct ctl_lun_req *req); 275229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 276229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 277229997Sken ctl_lun_config_status status); 278229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 279229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 280229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 281274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 282229997Skenint ctl_be_block_init(void); 283229997Sken 284229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 285229997Sken{ 286230334Sken .name = "block", 287230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 288230334Sken .init = ctl_be_block_init, 289230334Sken .data_submit = ctl_be_block_submit, 290230334Sken .data_move_done = ctl_be_block_move_done, 291230334Sken .config_read = ctl_be_block_config_read, 292230334Sken .config_write = ctl_be_block_config_write, 293230334Sken .ioctl = ctl_be_block_ioctl, 294274154Smav .lun_info = ctl_be_block_lun_info, 295274154Smav .lun_attr = ctl_be_block_lun_attr 296229997Sken}; 297229997Sken 298229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 299229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 300229997Sken 301264020Straszstatic uma_zone_t beio_zone; 302264020Strasz 303229997Skenstatic struct ctl_be_block_io * 304229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 305229997Sken{ 306229997Sken struct ctl_be_block_io *beio; 307229997Sken 308264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 309264020Strasz beio->softc = softc; 310229997Sken return (beio); 311229997Sken} 312229997Sken 313229997Skenstatic void 314229997Skenctl_free_beio(struct ctl_be_block_io *beio) 315229997Sken{ 316229997Sken int duplicate_free; 317229997Sken int i; 318229997Sken 319229997Sken duplicate_free = 0; 320229997Sken 321229997Sken for (i = 0; i < beio->num_segs; i++) { 322229997Sken if (beio->sg_segs[i].addr == NULL) 323229997Sken duplicate_free++; 324229997Sken 325229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 326229997Sken beio->sg_segs[i].addr = NULL; 327267537Smav 328267537Smav /* For compare we had two equal S/G lists. */ 329267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 330267537Smav uma_zfree(beio->lun->lun_zone, 331267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 332267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 333267537Smav } 334229997Sken } 335229997Sken 336229997Sken if (duplicate_free > 0) { 337229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 338229997Sken duplicate_free, beio->num_segs); 339229997Sken } 340229997Sken 341264020Strasz uma_zfree(beio_zone, beio); 342229997Sken} 343229997Sken 344229997Skenstatic void 345229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 346229997Sken{ 347267877Smav union ctl_io *io = beio->io; 348229997Sken 349264274Smav if (beio->beio_cont != NULL) { 350264274Smav beio->beio_cont(beio); 351264274Smav } else { 352264274Smav ctl_free_beio(beio); 353267537Smav ctl_data_submit_done(io); 354264274Smav } 355229997Sken} 356229997Sken 357229997Skenstatic int 358229997Skenctl_be_block_move_done(union ctl_io *io) 359229997Sken{ 360229997Sken struct ctl_be_block_io *beio; 361229997Sken struct ctl_be_block_lun *be_lun; 362267537Smav struct ctl_lba_len_flags *lbalen; 363229997Sken#ifdef CTL_TIME_IO 364229997Sken struct bintime cur_bt; 365267537Smav#endif 366267537Smav int i; 367229997Sken 368267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 369229997Sken be_lun = beio->lun; 370229997Sken 371229997Sken DPRINTF("entered\n"); 372229997Sken 373229997Sken#ifdef CTL_TIME_IO 374229997Sken getbintime(&cur_bt); 375229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 376229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 377229997Sken io->io_hdr.num_dmas++; 378229997Sken#endif 379267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 380229997Sken 381229997Sken /* 382229997Sken * We set status at this point for read commands, and write 383229997Sken * commands with errors. 384229997Sken */ 385275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 386275058Smav ; 387275058Smav } else if ((io->io_hdr.port_status == 0) && 388267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 389267537Smav lbalen = ARGS(beio->io); 390267537Smav if (lbalen->flags & CTL_LLF_READ) { 391267537Smav ctl_set_success(&io->scsiio); 392267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 393267537Smav /* We have two data blocks ready for comparison. */ 394267537Smav for (i = 0; i < beio->num_segs; i++) { 395267537Smav if (memcmp(beio->sg_segs[i].addr, 396267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 397267537Smav beio->sg_segs[i].len) != 0) 398267537Smav break; 399267537Smav } 400267537Smav if (i < beio->num_segs) 401267537Smav ctl_set_sense(&io->scsiio, 402267537Smav /*current_error*/ 1, 403267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 404267537Smav /*asc*/ 0x1D, 405267537Smav /*ascq*/ 0x00, 406267537Smav SSD_ELEM_NONE); 407267537Smav else 408267537Smav ctl_set_success(&io->scsiio); 409267537Smav } 410275058Smav } else if ((io->io_hdr.port_status != 0) && 411275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 412275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 413229997Sken /* 414229997Sken * For hardware error sense keys, the sense key 415229997Sken * specific value is defined to be a retry count, 416229997Sken * but we use it to pass back an internal FETD 417229997Sken * error code. XXX KDM Hopefully the FETD is only 418229997Sken * using 16 bits for an error code, since that's 419229997Sken * all the space we have in the sks field. 420229997Sken */ 421229997Sken ctl_set_internal_failure(&io->scsiio, 422229997Sken /*sks_valid*/ 1, 423229997Sken /*retry_count*/ 424229997Sken io->io_hdr.port_status); 425229997Sken } 426229997Sken 427229997Sken /* 428229997Sken * If this is a read, or a write with errors, it is done. 429229997Sken */ 430229997Sken if ((beio->bio_cmd == BIO_READ) 431229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 432229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 433229997Sken ctl_complete_beio(beio); 434229997Sken return (0); 435229997Sken } 436229997Sken 437229997Sken /* 438229997Sken * At this point, we have a write and the DMA completed 439229997Sken * successfully. We now have to queue it to the task queue to 440229997Sken * execute the backend I/O. That is because we do blocking 441229997Sken * memory allocations, and in the file backing case, blocking I/O. 442229997Sken * This move done routine is generally called in the SIM's 443229997Sken * interrupt context, and therefore we cannot block. 444229997Sken */ 445267877Smav mtx_lock(&be_lun->queue_lock); 446229997Sken /* 447229997Sken * XXX KDM make sure that links is okay to use at this point. 448229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 449229997Sken * or deal with resource allocation here. 450229997Sken */ 451229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 452267877Smav mtx_unlock(&be_lun->queue_lock); 453229997Sken 454229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 455229997Sken 456229997Sken return (0); 457229997Sken} 458229997Sken 459229997Skenstatic void 460229997Skenctl_be_block_biodone(struct bio *bio) 461229997Sken{ 462229997Sken struct ctl_be_block_io *beio; 463229997Sken struct ctl_be_block_lun *be_lun; 464229997Sken union ctl_io *io; 465261538Smav int error; 466229997Sken 467229997Sken beio = bio->bio_caller1; 468229997Sken be_lun = beio->lun; 469229997Sken io = beio->io; 470229997Sken 471229997Sken DPRINTF("entered\n"); 472229997Sken 473261538Smav error = bio->bio_error; 474267877Smav mtx_lock(&be_lun->io_lock); 475261538Smav if (error != 0) 476229997Sken beio->num_errors++; 477229997Sken 478229997Sken beio->num_bios_done++; 479229997Sken 480229997Sken /* 481229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 482229997Sken * during the free might cause it to complain. 483229997Sken */ 484229997Sken g_destroy_bio(bio); 485229997Sken 486229997Sken /* 487229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 488229997Sken * complete, then we're done. 489229997Sken */ 490229997Sken if ((beio->send_complete == 0) 491229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 492267877Smav mtx_unlock(&be_lun->io_lock); 493229997Sken return; 494229997Sken } 495229997Sken 496229997Sken /* 497229997Sken * At this point, we've verified that we are the last I/O to 498229997Sken * complete, so it's safe to drop the lock. 499229997Sken */ 500267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 501267877Smav beio->ds_tag_type, beio->ds_trans_type, 502267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 503267877Smav mtx_unlock(&be_lun->io_lock); 504229997Sken 505229997Sken /* 506229997Sken * If there are any errors from the backing device, we fail the 507229997Sken * entire I/O with a medium error. 508229997Sken */ 509229997Sken if (beio->num_errors > 0) { 510261538Smav if (error == EOPNOTSUPP) { 511261538Smav ctl_set_invalid_opcode(&io->scsiio); 512282565Smav } else if (error == ENOSPC || error == EDQUOT) { 513273809Smav ctl_set_space_alloc_fail(&io->scsiio); 514261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 515229997Sken /* XXX KDM is there is a better error here? */ 516229997Sken ctl_set_internal_failure(&io->scsiio, 517229997Sken /*sks_valid*/ 1, 518229997Sken /*retry_count*/ 0xbad2); 519229997Sken } else 520229997Sken ctl_set_medium_error(&io->scsiio); 521229997Sken ctl_complete_beio(beio); 522229997Sken return; 523229997Sken } 524229997Sken 525229997Sken /* 526267537Smav * If this is a write, a flush, a delete or verify, we're all done. 527229997Sken * If this is a read, we can now send the data to the user. 528229997Sken */ 529229997Sken if ((beio->bio_cmd == BIO_WRITE) 530264274Smav || (beio->bio_cmd == BIO_FLUSH) 531267537Smav || (beio->bio_cmd == BIO_DELETE) 532267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 533229997Sken ctl_set_success(&io->scsiio); 534229997Sken ctl_complete_beio(beio); 535229997Sken } else { 536275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 537275058Smav beio->beio_cont == NULL) 538275058Smav ctl_set_success(&io->scsiio); 539229997Sken#ifdef CTL_TIME_IO 540229997Sken getbintime(&io->io_hdr.dma_start_bt); 541229997Sken#endif 542229997Sken ctl_datamove(io); 543229997Sken } 544229997Sken} 545229997Sken 546229997Skenstatic void 547229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 548229997Sken struct ctl_be_block_io *beio) 549229997Sken{ 550267877Smav union ctl_io *io = beio->io; 551229997Sken struct mount *mountpoint; 552241896Skib int error, lock_flags; 553229997Sken 554229997Sken DPRINTF("entered\n"); 555229997Sken 556267877Smav binuptime(&beio->ds_t0); 557267877Smav mtx_lock(&be_lun->io_lock); 558267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 559267877Smav mtx_unlock(&be_lun->io_lock); 560229997Sken 561267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 562229997Sken 563229997Sken if (MNT_SHARED_WRITES(mountpoint) 564229997Sken || ((mountpoint == NULL) 565229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 566229997Sken lock_flags = LK_SHARED; 567229997Sken else 568229997Sken lock_flags = LK_EXCLUSIVE; 569229997Sken 570229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 571229997Sken 572286353Smav error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 573286353Smav curthread); 574229997Sken VOP_UNLOCK(be_lun->vn, 0); 575229997Sken 576229997Sken vn_finished_write(mountpoint); 577229997Sken 578267877Smav mtx_lock(&be_lun->io_lock); 579267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 580267877Smav beio->ds_tag_type, beio->ds_trans_type, 581267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 582267877Smav mtx_unlock(&be_lun->io_lock); 583267877Smav 584229997Sken if (error == 0) 585229997Sken ctl_set_success(&io->scsiio); 586229997Sken else { 587229997Sken /* XXX KDM is there is a better error here? */ 588229997Sken ctl_set_internal_failure(&io->scsiio, 589229997Sken /*sks_valid*/ 1, 590229997Sken /*retry_count*/ 0xbad1); 591229997Sken } 592229997Sken 593229997Sken ctl_complete_beio(beio); 594229997Sken} 595229997Sken 596258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 597258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 598258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 599258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 600229997Sken 601229997Skenstatic void 602229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 603229997Sken struct ctl_be_block_io *beio) 604229997Sken{ 605229997Sken struct ctl_be_block_filedata *file_data; 606229997Sken union ctl_io *io; 607229997Sken struct uio xuio; 608229997Sken struct iovec *xiovec; 609241896Skib int flags; 610229997Sken int error, i; 611229997Sken 612229997Sken DPRINTF("entered\n"); 613229997Sken 614229997Sken file_data = &be_lun->backend.file; 615229997Sken io = beio->io; 616271309Smav flags = 0; 617271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 618271309Smav flags |= IO_DIRECT; 619271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 620271309Smav flags |= IO_SYNC; 621229997Sken 622267537Smav bzero(&xuio, sizeof(xuio)); 623229997Sken if (beio->bio_cmd == BIO_READ) { 624229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 625267537Smav xuio.uio_rw = UIO_READ; 626229997Sken } else { 627229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 628267537Smav xuio.uio_rw = UIO_WRITE; 629229997Sken } 630229997Sken xuio.uio_offset = beio->io_offset; 631229997Sken xuio.uio_resid = beio->io_len; 632229997Sken xuio.uio_segflg = UIO_SYSSPACE; 633229997Sken xuio.uio_iov = beio->xiovecs; 634229997Sken xuio.uio_iovcnt = beio->num_segs; 635229997Sken xuio.uio_td = curthread; 636229997Sken 637229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 638229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 639229997Sken xiovec->iov_len = beio->sg_segs[i].len; 640229997Sken } 641229997Sken 642267877Smav binuptime(&beio->ds_t0); 643267877Smav mtx_lock(&be_lun->io_lock); 644267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 645267877Smav mtx_unlock(&be_lun->io_lock); 646267877Smav 647229997Sken if (beio->bio_cmd == BIO_READ) { 648229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 649229997Sken 650229997Sken /* 651229997Sken * UFS pays attention to IO_DIRECT for reads. If the 652229997Sken * DIRECTIO option is configured into the kernel, it calls 653229997Sken * ffs_rawread(). But that only works for single-segment 654229997Sken * uios with user space addresses. In our case, with a 655229997Sken * kernel uio, it still reads into the buffer cache, but it 656229997Sken * will just try to release the buffer from the cache later 657229997Sken * on in ffs_read(). 658229997Sken * 659229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 660229997Sken * 661229997Sken * UFS does not pay attention to IO_SYNC for reads. 662229997Sken * 663229997Sken * ZFS pays attention to IO_SYNC (which translates into the 664229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 665229997Sken * attempts to sync the file before reading. 666229997Sken */ 667271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 668229997Sken 669229997Sken VOP_UNLOCK(be_lun->vn, 0); 670267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 671229997Sken } else { 672229997Sken struct mount *mountpoint; 673229997Sken int lock_flags; 674229997Sken 675229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 676229997Sken 677229997Sken if (MNT_SHARED_WRITES(mountpoint) 678229997Sken || ((mountpoint == NULL) 679229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 680229997Sken lock_flags = LK_SHARED; 681229997Sken else 682229997Sken lock_flags = LK_EXCLUSIVE; 683229997Sken 684229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 685229997Sken 686229997Sken /* 687229997Sken * UFS pays attention to IO_DIRECT for writes. The write 688229997Sken * is done asynchronously. (Normally the write would just 689229997Sken * get put into cache. 690229997Sken * 691229997Sken * UFS pays attention to IO_SYNC for writes. It will 692229997Sken * attempt to write the buffer out synchronously if that 693229997Sken * flag is set. 694229997Sken * 695229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 696229997Sken * 697229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 698229997Sken * for writes. It will flush the transaction from the 699229997Sken * cache before returning. 700229997Sken */ 701271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 702229997Sken VOP_UNLOCK(be_lun->vn, 0); 703229997Sken 704229997Sken vn_finished_write(mountpoint); 705267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 706229997Sken } 707229997Sken 708267877Smav mtx_lock(&be_lun->io_lock); 709267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 710267877Smav beio->ds_tag_type, beio->ds_trans_type, 711267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 712267877Smav mtx_unlock(&be_lun->io_lock); 713267877Smav 714229997Sken /* 715229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 716229997Sken * return the I/O to the user. 717229997Sken */ 718229997Sken if (error != 0) { 719229997Sken char path_str[32]; 720229997Sken 721229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 722229997Sken printf("%s%s command returned errno %d\n", path_str, 723229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 724282565Smav if (error == ENOSPC || error == EDQUOT) { 725273809Smav ctl_set_space_alloc_fail(&io->scsiio); 726273809Smav } else 727273809Smav ctl_set_medium_error(&io->scsiio); 728229997Sken ctl_complete_beio(beio); 729229997Sken return; 730229997Sken } 731229997Sken 732229997Sken /* 733269122Smav * If this is a write or a verify, we're all done. 734229997Sken * If this is a read, we can now send the data to the user. 735229997Sken */ 736269122Smav if ((beio->bio_cmd == BIO_WRITE) || 737269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 738229997Sken ctl_set_success(&io->scsiio); 739229997Sken ctl_complete_beio(beio); 740229997Sken } else { 741275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 742275058Smav beio->beio_cont == NULL) 743275058Smav ctl_set_success(&io->scsiio); 744229997Sken#ifdef CTL_TIME_IO 745229997Sken getbintime(&io->io_hdr.dma_start_bt); 746229997Sken#endif 747229997Sken ctl_datamove(io); 748229997Sken } 749229997Sken} 750229997Sken 751229997Skenstatic void 752275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 753275474Smav struct ctl_be_block_io *beio) 754275474Smav{ 755275474Smav union ctl_io *io = beio->io; 756275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 757275474Smav struct scsi_get_lba_status_data *data; 758275474Smav off_t roff, off; 759275474Smav int error, status; 760275474Smav 761275474Smav DPRINTF("entered\n"); 762275474Smav 763287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 764275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 765275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 766275474Smav 0, curthread->td_ucred, curthread); 767275474Smav if (error == 0 && off > roff) 768275474Smav status = 0; /* mapped up to off */ 769275474Smav else { 770275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 771275474Smav 0, curthread->td_ucred, curthread); 772275474Smav if (error == 0 && off > roff) 773275474Smav status = 1; /* deallocated up to off */ 774275474Smav else { 775275474Smav status = 0; /* unknown up to the end */ 776275474Smav off = be_lun->size_bytes; 777275474Smav } 778275474Smav } 779275474Smav VOP_UNLOCK(be_lun->vn, 0); 780275474Smav 781275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 782275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 783287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 784287499Smav lbalen->lba), data->descr[0].length); 785275474Smav data->descr[0].status = status; 786275474Smav 787275474Smav ctl_complete_beio(beio); 788275474Smav} 789275474Smav 790275481Smavstatic uint64_t 791275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 792275481Smav{ 793275481Smav struct vattr vattr; 794275481Smav struct statfs statfs; 795285030Smav uint64_t val; 796275481Smav int error; 797275481Smav 798285030Smav val = UINT64_MAX; 799275481Smav if (be_lun->vn == NULL) 800285030Smav return (val); 801285030Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 802275481Smav if (strcmp(attrname, "blocksused") == 0) { 803275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 804285030Smav if (error == 0) 805287499Smav val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 806275481Smav } 807285030Smav if (strcmp(attrname, "blocksavail") == 0 && 808285030Smav (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 809275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 810285030Smav if (error == 0) 811286811Smav val = statfs.f_bavail * statfs.f_bsize / 812287499Smav be_lun->cbe_lun.blocksize; 813275481Smav } 814285030Smav VOP_UNLOCK(be_lun->vn, 0); 815285030Smav return (val); 816275481Smav} 817275481Smav 818275474Smavstatic void 819269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 820269123Smav struct ctl_be_block_io *beio) 821269123Smav{ 822269123Smav struct ctl_be_block_devdata *dev_data; 823269123Smav union ctl_io *io; 824269123Smav struct uio xuio; 825269123Smav struct iovec *xiovec; 826269123Smav int flags; 827269123Smav int error, i; 828269123Smav 829269123Smav DPRINTF("entered\n"); 830269123Smav 831269123Smav dev_data = &be_lun->backend.dev; 832269123Smav io = beio->io; 833271309Smav flags = 0; 834271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 835271309Smav flags |= IO_DIRECT; 836271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 837271309Smav flags |= IO_SYNC; 838269123Smav 839269123Smav bzero(&xuio, sizeof(xuio)); 840269123Smav if (beio->bio_cmd == BIO_READ) { 841269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 842269123Smav xuio.uio_rw = UIO_READ; 843269123Smav } else { 844269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 845269123Smav xuio.uio_rw = UIO_WRITE; 846269123Smav } 847269123Smav xuio.uio_offset = beio->io_offset; 848269123Smav xuio.uio_resid = beio->io_len; 849269123Smav xuio.uio_segflg = UIO_SYSSPACE; 850269123Smav xuio.uio_iov = beio->xiovecs; 851269123Smav xuio.uio_iovcnt = beio->num_segs; 852269123Smav xuio.uio_td = curthread; 853269123Smav 854269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 855269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 856269123Smav xiovec->iov_len = beio->sg_segs[i].len; 857269123Smav } 858269123Smav 859269123Smav binuptime(&beio->ds_t0); 860269123Smav mtx_lock(&be_lun->io_lock); 861269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 862269123Smav mtx_unlock(&be_lun->io_lock); 863269123Smav 864269123Smav if (beio->bio_cmd == BIO_READ) { 865271309Smav error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 866269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 867269123Smav } else { 868271309Smav error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 869269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 870269123Smav } 871269123Smav 872269123Smav mtx_lock(&be_lun->io_lock); 873269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 874269123Smav beio->ds_tag_type, beio->ds_trans_type, 875269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 876269123Smav mtx_unlock(&be_lun->io_lock); 877269123Smav 878269123Smav /* 879269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 880269123Smav * return the I/O to the user. 881269123Smav */ 882269123Smav if (error != 0) { 883282565Smav if (error == ENOSPC || error == EDQUOT) { 884273809Smav ctl_set_space_alloc_fail(&io->scsiio); 885273809Smav } else 886273809Smav ctl_set_medium_error(&io->scsiio); 887269123Smav ctl_complete_beio(beio); 888269123Smav return; 889269123Smav } 890269123Smav 891269123Smav /* 892269123Smav * If this is a write or a verify, we're all done. 893269123Smav * If this is a read, we can now send the data to the user. 894269123Smav */ 895269123Smav if ((beio->bio_cmd == BIO_WRITE) || 896269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 897269123Smav ctl_set_success(&io->scsiio); 898269123Smav ctl_complete_beio(beio); 899269123Smav } else { 900275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 901275058Smav beio->beio_cont == NULL) 902275058Smav ctl_set_success(&io->scsiio); 903269123Smav#ifdef CTL_TIME_IO 904269123Smav getbintime(&io->io_hdr.dma_start_bt); 905269123Smav#endif 906269123Smav ctl_datamove(io); 907269123Smav } 908269123Smav} 909269123Smav 910269123Smavstatic void 911275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 912275474Smav struct ctl_be_block_io *beio) 913275474Smav{ 914275474Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 915275474Smav union ctl_io *io = beio->io; 916275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 917275474Smav struct scsi_get_lba_status_data *data; 918275474Smav off_t roff, off; 919275474Smav int error, status; 920275474Smav 921275474Smav DPRINTF("entered\n"); 922275474Smav 923287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 924275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE, 925275474Smav (caddr_t)&off, FREAD, curthread); 926275474Smav if (error == 0 && off > roff) 927275474Smav status = 0; /* mapped up to off */ 928275474Smav else { 929275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA, 930275474Smav (caddr_t)&off, FREAD, curthread); 931275474Smav if (error == 0 && off > roff) 932275474Smav status = 1; /* deallocated up to off */ 933275474Smav else { 934275474Smav status = 0; /* unknown up to the end */ 935275474Smav off = be_lun->size_bytes; 936275474Smav } 937275474Smav } 938275474Smav 939275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 940275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 941287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 942287499Smav lbalen->lba), data->descr[0].length); 943275474Smav data->descr[0].status = status; 944275474Smav 945275474Smav ctl_complete_beio(beio); 946275474Smav} 947275474Smav 948275474Smavstatic void 949229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 950229997Sken struct ctl_be_block_io *beio) 951229997Sken{ 952229997Sken struct bio *bio; 953229997Sken union ctl_io *io; 954229997Sken struct ctl_be_block_devdata *dev_data; 955229997Sken 956229997Sken dev_data = &be_lun->backend.dev; 957229997Sken io = beio->io; 958229997Sken 959229997Sken DPRINTF("entered\n"); 960229997Sken 961229997Sken /* This can't fail, it's a blocking allocation. */ 962229997Sken bio = g_alloc_bio(); 963229997Sken 964229997Sken bio->bio_cmd = BIO_FLUSH; 965229997Sken bio->bio_dev = dev_data->cdev; 966229997Sken bio->bio_offset = 0; 967229997Sken bio->bio_data = 0; 968229997Sken bio->bio_done = ctl_be_block_biodone; 969229997Sken bio->bio_caller1 = beio; 970229997Sken bio->bio_pblkno = 0; 971229997Sken 972229997Sken /* 973229997Sken * We don't need to acquire the LUN lock here, because we are only 974229997Sken * sending one bio, and so there is no other context to synchronize 975229997Sken * with. 976229997Sken */ 977229997Sken beio->num_bios_sent = 1; 978229997Sken beio->send_complete = 1; 979229997Sken 980229997Sken binuptime(&beio->ds_t0); 981267877Smav mtx_lock(&be_lun->io_lock); 982229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 983267877Smav mtx_unlock(&be_lun->io_lock); 984229997Sken 985229997Sken (*dev_data->csw->d_strategy)(bio); 986229997Sken} 987229997Sken 988229997Skenstatic void 989264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 990264274Smav struct ctl_be_block_io *beio, 991264274Smav uint64_t off, uint64_t len, int last) 992264274Smav{ 993264274Smav struct bio *bio; 994264274Smav struct ctl_be_block_devdata *dev_data; 995264296Smav uint64_t maxlen; 996264274Smav 997264274Smav dev_data = &be_lun->backend.dev; 998287499Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 999264274Smav while (len > 0) { 1000264274Smav bio = g_alloc_bio(); 1001264274Smav bio->bio_cmd = BIO_DELETE; 1002264274Smav bio->bio_dev = dev_data->cdev; 1003264274Smav bio->bio_offset = off; 1004264296Smav bio->bio_length = MIN(len, maxlen); 1005264274Smav bio->bio_data = 0; 1006264274Smav bio->bio_done = ctl_be_block_biodone; 1007264274Smav bio->bio_caller1 = beio; 1008287499Smav bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1009264274Smav 1010264274Smav off += bio->bio_length; 1011264274Smav len -= bio->bio_length; 1012264274Smav 1013267877Smav mtx_lock(&be_lun->io_lock); 1014264274Smav beio->num_bios_sent++; 1015264274Smav if (last && len == 0) 1016264274Smav beio->send_complete = 1; 1017267877Smav mtx_unlock(&be_lun->io_lock); 1018264274Smav 1019264274Smav (*dev_data->csw->d_strategy)(bio); 1020264274Smav } 1021264274Smav} 1022264274Smav 1023264274Smavstatic void 1024264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1025264274Smav struct ctl_be_block_io *beio) 1026264274Smav{ 1027264274Smav union ctl_io *io; 1028264274Smav struct ctl_be_block_devdata *dev_data; 1029267515Smav struct ctl_ptr_len_flags *ptrlen; 1030264274Smav struct scsi_unmap_desc *buf, *end; 1031264274Smav uint64_t len; 1032264274Smav 1033264274Smav dev_data = &be_lun->backend.dev; 1034264274Smav io = beio->io; 1035264274Smav 1036264274Smav DPRINTF("entered\n"); 1037264274Smav 1038264274Smav binuptime(&beio->ds_t0); 1039267877Smav mtx_lock(&be_lun->io_lock); 1040264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1041267877Smav mtx_unlock(&be_lun->io_lock); 1042264274Smav 1043264274Smav if (beio->io_offset == -1) { 1044264274Smav beio->io_len = 0; 1045267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1046267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1047267515Smav end = buf + ptrlen->len / sizeof(*buf); 1048264274Smav for (; buf < end; buf++) { 1049264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1050287499Smav be_lun->cbe_lun.blocksize; 1051264274Smav beio->io_len += len; 1052264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1053287499Smav scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 1054287499Smav len, (end - buf < 2) ? TRUE : FALSE); 1055264274Smav } 1056264274Smav } else 1057264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1058264274Smav beio->io_offset, beio->io_len, TRUE); 1059264274Smav} 1060264274Smav 1061264274Smavstatic void 1062229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1063229997Sken struct ctl_be_block_io *beio) 1064229997Sken{ 1065267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1066229997Sken int i; 1067229997Sken struct bio *bio; 1068229997Sken struct ctl_be_block_devdata *dev_data; 1069229997Sken off_t cur_offset; 1070229997Sken int max_iosize; 1071229997Sken 1072229997Sken DPRINTF("entered\n"); 1073229997Sken 1074229997Sken dev_data = &be_lun->backend.dev; 1075229997Sken 1076229997Sken /* 1077229997Sken * We have to limit our I/O size to the maximum supported by the 1078229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1079229997Sken * set it properly, use DFLTPHYS. 1080229997Sken */ 1081229997Sken max_iosize = dev_data->cdev->si_iosize_max; 1082229997Sken if (max_iosize < PAGE_SIZE) 1083229997Sken max_iosize = DFLTPHYS; 1084229997Sken 1085229997Sken cur_offset = beio->io_offset; 1086229997Sken for (i = 0; i < beio->num_segs; i++) { 1087229997Sken size_t cur_size; 1088229997Sken uint8_t *cur_ptr; 1089229997Sken 1090229997Sken cur_size = beio->sg_segs[i].len; 1091229997Sken cur_ptr = beio->sg_segs[i].addr; 1092229997Sken 1093229997Sken while (cur_size > 0) { 1094229997Sken /* This can't fail, it's a blocking allocation. */ 1095229997Sken bio = g_alloc_bio(); 1096229997Sken 1097229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1098229997Sken 1099229997Sken bio->bio_cmd = beio->bio_cmd; 1100229997Sken bio->bio_dev = dev_data->cdev; 1101229997Sken bio->bio_caller1 = beio; 1102229997Sken bio->bio_length = min(cur_size, max_iosize); 1103229997Sken bio->bio_offset = cur_offset; 1104229997Sken bio->bio_data = cur_ptr; 1105229997Sken bio->bio_done = ctl_be_block_biodone; 1106287499Smav bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1107229997Sken 1108229997Sken cur_offset += bio->bio_length; 1109229997Sken cur_ptr += bio->bio_length; 1110229997Sken cur_size -= bio->bio_length; 1111229997Sken 1112267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1113229997Sken beio->num_bios_sent++; 1114229997Sken } 1115229997Sken } 1116267877Smav binuptime(&beio->ds_t0); 1117267877Smav mtx_lock(&be_lun->io_lock); 1118267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1119267877Smav beio->send_complete = 1; 1120267877Smav mtx_unlock(&be_lun->io_lock); 1121267877Smav 1122267877Smav /* 1123267877Smav * Fire off all allocated requests! 1124267877Smav */ 1125267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1126267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1127267877Smav (*dev_data->csw->d_strategy)(bio); 1128267877Smav } 1129229997Sken} 1130229997Sken 1131274154Smavstatic uint64_t 1132274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1133274154Smav{ 1134274154Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1135274154Smav struct diocgattr_arg arg; 1136274154Smav int error; 1137274154Smav 1138274154Smav if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1139274154Smav return (UINT64_MAX); 1140274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1141274154Smav arg.len = sizeof(arg.value.off); 1142274154Smav error = dev_data->csw->d_ioctl(dev_data->cdev, 1143274154Smav DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1144274154Smav if (error != 0) 1145274154Smav return (UINT64_MAX); 1146274154Smav return (arg.value.off); 1147274154Smav} 1148274154Smav 1149229997Skenstatic void 1150286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1151286353Smav union ctl_io *io) 1152286353Smav{ 1153287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1154286353Smav struct ctl_be_block_io *beio; 1155286353Smav struct ctl_lba_len_flags *lbalen; 1156286353Smav 1157286353Smav DPRINTF("entered\n"); 1158286353Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1159286353Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1160286353Smav 1161287499Smav beio->io_len = lbalen->len * cbe_lun->blocksize; 1162287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1163286353Smav beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1164286353Smav beio->bio_cmd = BIO_FLUSH; 1165286353Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1166286353Smav DPRINTF("SYNC\n"); 1167286353Smav be_lun->lun_flush(be_lun, beio); 1168286353Smav} 1169286353Smav 1170286353Smavstatic void 1171264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1172264274Smav{ 1173264274Smav union ctl_io *io; 1174264274Smav 1175264274Smav io = beio->io; 1176264274Smav ctl_free_beio(beio); 1177267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1178267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1179267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1180264274Smav ctl_config_write_done(io); 1181264274Smav return; 1182264274Smav } 1183264274Smav 1184264274Smav ctl_be_block_config_write(io); 1185264274Smav} 1186264274Smav 1187264274Smavstatic void 1188264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1189264274Smav union ctl_io *io) 1190264274Smav{ 1191287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1192264274Smav struct ctl_be_block_io *beio; 1193267515Smav struct ctl_lba_len_flags *lbalen; 1194278625Smav uint64_t len_left, lba; 1195278625Smav uint32_t pb, pbo, adj; 1196264274Smav int i, seglen; 1197264274Smav uint8_t *buf, *end; 1198264274Smav 1199264274Smav DPRINTF("entered\n"); 1200264274Smav 1201267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1202267537Smav lbalen = ARGS(beio->io); 1203264274Smav 1204271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1205269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1206264274Smav ctl_free_beio(beio); 1207264274Smav ctl_set_invalid_field(&io->scsiio, 1208264274Smav /*sks_valid*/ 1, 1209264274Smav /*command*/ 1, 1210264274Smav /*field*/ 1, 1211264274Smav /*bit_valid*/ 0, 1212264274Smav /*bit*/ 0); 1213264274Smav ctl_config_write_done(io); 1214264274Smav return; 1215264274Smav } 1216264274Smav 1217269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1218287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1219287499Smav beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1220264274Smav beio->bio_cmd = BIO_DELETE; 1221264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1222264274Smav 1223264274Smav be_lun->unmap(be_lun, beio); 1224264274Smav return; 1225264274Smav } 1226264274Smav 1227264274Smav beio->bio_cmd = BIO_WRITE; 1228264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1229264274Smav 1230264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1231267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1232264274Smav 1233287499Smav pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1234287499Smav if (be_lun->cbe_lun.pblockoff > 0) 1235287499Smav pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1236278625Smav else 1237278625Smav pbo = 0; 1238287499Smav len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1239264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1240264274Smav 1241264274Smav /* 1242264274Smav * Setup the S/G entry for this chunk. 1243264274Smav */ 1244264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1245287499Smav if (pb > cbe_lun->blocksize) { 1246287499Smav adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1247278619Smav seglen - pbo) % pb; 1248278619Smav if (seglen > adj) 1249278619Smav seglen -= adj; 1250278619Smav else 1251287499Smav seglen -= seglen % cbe_lun->blocksize; 1252278619Smav } else 1253287499Smav seglen -= seglen % cbe_lun->blocksize; 1254264274Smav beio->sg_segs[i].len = seglen; 1255264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1256264274Smav 1257264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1258264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1259264274Smav 1260264274Smav beio->num_segs++; 1261264274Smav len_left -= seglen; 1262264274Smav 1263264274Smav buf = beio->sg_segs[i].addr; 1264264274Smav end = buf + seglen; 1265287499Smav for (; buf < end; buf += cbe_lun->blocksize) { 1266287499Smav memcpy(buf, io->scsiio.kern_data_ptr, cbe_lun->blocksize); 1267267515Smav if (lbalen->flags & SWS_LBDATA) 1268267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1269264274Smav lba++; 1270264274Smav } 1271264274Smav } 1272264274Smav 1273287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1274287499Smav beio->io_len = lba * cbe_lun->blocksize; 1275264274Smav 1276264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1277264274Smav if (len_left > 0) { 1278267515Smav lbalen->lba += lba; 1279267515Smav lbalen->len -= lba; 1280264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1281264274Smav } 1282264274Smav 1283264274Smav be_lun->dispatch(be_lun, beio); 1284264274Smav} 1285264274Smav 1286264274Smavstatic void 1287264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1288264274Smav union ctl_io *io) 1289264274Smav{ 1290264274Smav struct ctl_be_block_io *beio; 1291267515Smav struct ctl_ptr_len_flags *ptrlen; 1292264274Smav 1293264274Smav DPRINTF("entered\n"); 1294264274Smav 1295267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1296267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1297264274Smav 1298269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1299264274Smav ctl_free_beio(beio); 1300264274Smav ctl_set_invalid_field(&io->scsiio, 1301264274Smav /*sks_valid*/ 0, 1302264274Smav /*command*/ 1, 1303264274Smav /*field*/ 0, 1304264274Smav /*bit_valid*/ 0, 1305264274Smav /*bit*/ 0); 1306264274Smav ctl_config_write_done(io); 1307264274Smav return; 1308264274Smav } 1309264274Smav 1310264274Smav beio->io_len = 0; 1311264274Smav beio->io_offset = -1; 1312264274Smav beio->bio_cmd = BIO_DELETE; 1313264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1314267515Smav DPRINTF("UNMAP\n"); 1315264274Smav be_lun->unmap(be_lun, beio); 1316264274Smav} 1317264274Smav 1318264274Smavstatic void 1319275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1320275474Smav{ 1321275474Smav union ctl_io *io; 1322275474Smav 1323275474Smav io = beio->io; 1324275474Smav ctl_free_beio(beio); 1325275474Smav ctl_config_read_done(io); 1326275474Smav} 1327275474Smav 1328275474Smavstatic void 1329275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1330275474Smav union ctl_io *io) 1331275474Smav{ 1332275474Smav struct ctl_be_block_io *beio; 1333275474Smav struct ctl_be_block_softc *softc; 1334275474Smav 1335275474Smav DPRINTF("entered\n"); 1336275474Smav 1337275474Smav softc = be_lun->softc; 1338275474Smav beio = ctl_alloc_beio(softc); 1339275474Smav beio->io = io; 1340275474Smav beio->lun = be_lun; 1341275474Smav beio->beio_cont = ctl_be_block_cr_done; 1342275474Smav PRIV(io)->ptr = (void *)beio; 1343275474Smav 1344275474Smav switch (io->scsiio.cdb[0]) { 1345275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1346275474Smav beio->bio_cmd = -1; 1347275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1348275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1349275474Smav beio->io_len = 0; 1350275474Smav if (be_lun->get_lba_status) 1351275474Smav be_lun->get_lba_status(be_lun, beio); 1352275474Smav else 1353275474Smav ctl_be_block_cr_done(beio); 1354275474Smav break; 1355275474Smav default: 1356275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1357275474Smav break; 1358275474Smav } 1359275474Smav} 1360275474Smav 1361275474Smavstatic void 1362264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1363264274Smav{ 1364264274Smav union ctl_io *io; 1365264274Smav 1366264274Smav io = beio->io; 1367264274Smav ctl_free_beio(beio); 1368264274Smav ctl_config_write_done(io); 1369264274Smav} 1370264274Smav 1371264274Smavstatic void 1372229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1373229997Sken union ctl_io *io) 1374229997Sken{ 1375229997Sken struct ctl_be_block_io *beio; 1376229997Sken struct ctl_be_block_softc *softc; 1377229997Sken 1378229997Sken DPRINTF("entered\n"); 1379229997Sken 1380229997Sken softc = be_lun->softc; 1381229997Sken beio = ctl_alloc_beio(softc); 1382229997Sken beio->io = io; 1383229997Sken beio->lun = be_lun; 1384264274Smav beio->beio_cont = ctl_be_block_cw_done; 1385286353Smav switch (io->scsiio.tag_type) { 1386286353Smav case CTL_TAG_ORDERED: 1387286353Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1388286353Smav break; 1389286353Smav case CTL_TAG_HEAD_OF_QUEUE: 1390286353Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1391286353Smav break; 1392286353Smav case CTL_TAG_UNTAGGED: 1393286353Smav case CTL_TAG_SIMPLE: 1394286353Smav case CTL_TAG_ACA: 1395286353Smav default: 1396286353Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1397286353Smav break; 1398286353Smav } 1399267519Smav PRIV(io)->ptr = (void *)beio; 1400229997Sken 1401229997Sken switch (io->scsiio.cdb[0]) { 1402229997Sken case SYNCHRONIZE_CACHE: 1403229997Sken case SYNCHRONIZE_CACHE_16: 1404286353Smav ctl_be_block_cw_dispatch_sync(be_lun, io); 1405229997Sken break; 1406264274Smav case WRITE_SAME_10: 1407264274Smav case WRITE_SAME_16: 1408264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1409264274Smav break; 1410264274Smav case UNMAP: 1411264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1412264274Smav break; 1413229997Sken default: 1414229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1415229997Sken break; 1416229997Sken } 1417229997Sken} 1418229997Sken 1419258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1420258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1421258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1422258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1423229997Sken 1424229997Skenstatic void 1425264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1426264886Smav{ 1427264886Smav struct ctl_be_block_lun *be_lun; 1428264886Smav union ctl_io *io; 1429264886Smav 1430264886Smav io = beio->io; 1431264886Smav be_lun = beio->lun; 1432264886Smav ctl_free_beio(beio); 1433267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1434267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1435267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1436267537Smav ctl_data_submit_done(io); 1437264886Smav return; 1438264886Smav } 1439264886Smav 1440264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1441264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1442264886Smav 1443267877Smav mtx_lock(&be_lun->queue_lock); 1444264886Smav /* 1445264886Smav * XXX KDM make sure that links is okay to use at this point. 1446264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1447264886Smav * or deal with resource allocation here. 1448264886Smav */ 1449264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1450267877Smav mtx_unlock(&be_lun->queue_lock); 1451264886Smav 1452264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1453264886Smav} 1454264886Smav 1455264886Smavstatic void 1456229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1457229997Sken union ctl_io *io) 1458229997Sken{ 1459287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1460229997Sken struct ctl_be_block_io *beio; 1461229997Sken struct ctl_be_block_softc *softc; 1462267537Smav struct ctl_lba_len_flags *lbalen; 1463267519Smav struct ctl_ptr_len_flags *bptrlen; 1464267519Smav uint64_t len_left, lbas; 1465229997Sken int i; 1466229997Sken 1467229997Sken softc = be_lun->softc; 1468229997Sken 1469229997Sken DPRINTF("entered\n"); 1470229997Sken 1471267537Smav lbalen = ARGS(io); 1472267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1473267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1474267537Smav } else { 1475229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1476229997Sken } 1477229997Sken 1478229997Sken beio = ctl_alloc_beio(softc); 1479229997Sken beio->io = io; 1480229997Sken beio->lun = be_lun; 1481267519Smav bptrlen = PRIV(io); 1482267519Smav bptrlen->ptr = (void *)beio; 1483229997Sken 1484229997Sken switch (io->scsiio.tag_type) { 1485229997Sken case CTL_TAG_ORDERED: 1486229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1487229997Sken break; 1488229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1489229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1490229997Sken break; 1491229997Sken case CTL_TAG_UNTAGGED: 1492229997Sken case CTL_TAG_SIMPLE: 1493229997Sken case CTL_TAG_ACA: 1494229997Sken default: 1495229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1496229997Sken break; 1497229997Sken } 1498229997Sken 1499267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1500267537Smav beio->bio_cmd = BIO_WRITE; 1501267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1502267537Smav } else { 1503229997Sken beio->bio_cmd = BIO_READ; 1504229997Sken beio->ds_trans_type = DEVSTAT_READ; 1505229997Sken } 1506229997Sken 1507264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1508229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1509267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1510267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1511267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1512267537Smav else 1513267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1514287499Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 1515287499Smav beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 1516287499Smav beio->io_len = lbas * cbe_lun->blocksize; 1517267519Smav bptrlen->len += lbas; 1518229997Sken 1519264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1520264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1521264886Smav i, CTLBLK_MAX_SEGS)); 1522229997Sken 1523229997Sken /* 1524229997Sken * Setup the S/G entry for this chunk. 1525229997Sken */ 1526264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1527229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1528229997Sken 1529229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1530229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1531229997Sken 1532267537Smav /* Set up second segment for compare operation. */ 1533267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1534267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1535267537Smav beio->sg_segs[i].len; 1536267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1537267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1538267537Smav } 1539267537Smav 1540229997Sken beio->num_segs++; 1541229997Sken len_left -= beio->sg_segs[i].len; 1542229997Sken } 1543267519Smav if (bptrlen->len < lbalen->len) 1544264886Smav beio->beio_cont = ctl_be_block_next; 1545264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1546267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1547267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1548267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1549267537Smav else 1550267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1551264886Smav io->scsiio.kern_data_len = beio->io_len; 1552264886Smav io->scsiio.kern_data_resid = 0; 1553264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1554264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1555229997Sken 1556229997Sken /* 1557229997Sken * For the read case, we need to read the data into our buffers and 1558229997Sken * then we can send it back to the user. For the write case, we 1559229997Sken * need to get the data from the user first. 1560229997Sken */ 1561229997Sken if (beio->bio_cmd == BIO_READ) { 1562229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1563229997Sken be_lun->dispatch(be_lun, beio); 1564229997Sken } else { 1565229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1566229997Sken#ifdef CTL_TIME_IO 1567229997Sken getbintime(&io->io_hdr.dma_start_bt); 1568229997Sken#endif 1569229997Sken ctl_datamove(io); 1570229997Sken } 1571229997Sken} 1572229997Sken 1573229997Skenstatic void 1574229997Skenctl_be_block_worker(void *context, int pending) 1575229997Sken{ 1576229997Sken struct ctl_be_block_lun *be_lun; 1577229997Sken struct ctl_be_block_softc *softc; 1578229997Sken union ctl_io *io; 1579229997Sken 1580229997Sken be_lun = (struct ctl_be_block_lun *)context; 1581229997Sken softc = be_lun->softc; 1582229997Sken 1583229997Sken DPRINTF("entered\n"); 1584229997Sken 1585267877Smav mtx_lock(&be_lun->queue_lock); 1586229997Sken for (;;) { 1587229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1588229997Sken if (io != NULL) { 1589229997Sken struct ctl_be_block_io *beio; 1590229997Sken 1591229997Sken DPRINTF("datamove queue\n"); 1592229997Sken 1593229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1594229997Sken ctl_io_hdr, links); 1595229997Sken 1596267877Smav mtx_unlock(&be_lun->queue_lock); 1597229997Sken 1598267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1599229997Sken 1600229997Sken be_lun->dispatch(be_lun, beio); 1601229997Sken 1602267877Smav mtx_lock(&be_lun->queue_lock); 1603229997Sken continue; 1604229997Sken } 1605229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1606229997Sken if (io != NULL) { 1607229997Sken DPRINTF("config write queue\n"); 1608229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1609229997Sken ctl_io_hdr, links); 1610267877Smav mtx_unlock(&be_lun->queue_lock); 1611229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1612267877Smav mtx_lock(&be_lun->queue_lock); 1613229997Sken continue; 1614229997Sken } 1615275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1616275474Smav if (io != NULL) { 1617275474Smav DPRINTF("config read queue\n"); 1618275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1619275474Smav ctl_io_hdr, links); 1620275474Smav mtx_unlock(&be_lun->queue_lock); 1621275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1622275474Smav mtx_lock(&be_lun->queue_lock); 1623275474Smav continue; 1624275474Smav } 1625229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1626229997Sken if (io != NULL) { 1627229997Sken DPRINTF("input queue\n"); 1628229997Sken 1629229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1630229997Sken ctl_io_hdr, links); 1631267877Smav mtx_unlock(&be_lun->queue_lock); 1632229997Sken 1633229997Sken /* 1634229997Sken * We must drop the lock, since this routine and 1635229997Sken * its children may sleep. 1636229997Sken */ 1637229997Sken ctl_be_block_dispatch(be_lun, io); 1638229997Sken 1639267877Smav mtx_lock(&be_lun->queue_lock); 1640229997Sken continue; 1641229997Sken } 1642229997Sken 1643229997Sken /* 1644229997Sken * If we get here, there is no work left in the queues, so 1645229997Sken * just break out and let the task queue go to sleep. 1646229997Sken */ 1647229997Sken break; 1648229997Sken } 1649267877Smav mtx_unlock(&be_lun->queue_lock); 1650229997Sken} 1651229997Sken 1652229997Sken/* 1653229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1654229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1655229997Sken * thread. 1656229997Sken */ 1657229997Skenstatic int 1658229997Skenctl_be_block_submit(union ctl_io *io) 1659229997Sken{ 1660229997Sken struct ctl_be_block_lun *be_lun; 1661287499Smav struct ctl_be_lun *cbe_lun; 1662229997Sken 1663229997Sken DPRINTF("entered\n"); 1664229997Sken 1665287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1666229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1667287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1668229997Sken 1669229997Sken /* 1670229997Sken * Make sure we only get SCSI I/O. 1671229997Sken */ 1672229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1673229997Sken "%#x) encountered", io->io_hdr.io_type)); 1674229997Sken 1675267519Smav PRIV(io)->len = 0; 1676267519Smav 1677267877Smav mtx_lock(&be_lun->queue_lock); 1678229997Sken /* 1679229997Sken * XXX KDM make sure that links is okay to use at this point. 1680229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1681229997Sken * or deal with resource allocation here. 1682229997Sken */ 1683229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1684267877Smav mtx_unlock(&be_lun->queue_lock); 1685229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1686229997Sken 1687267514Smav return (CTL_RETVAL_COMPLETE); 1688229997Sken} 1689229997Sken 1690229997Skenstatic int 1691229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1692229997Sken int flag, struct thread *td) 1693229997Sken{ 1694229997Sken struct ctl_be_block_softc *softc; 1695229997Sken int error; 1696229997Sken 1697229997Sken softc = &backend_block_softc; 1698229997Sken 1699229997Sken error = 0; 1700229997Sken 1701229997Sken switch (cmd) { 1702229997Sken case CTL_LUN_REQ: { 1703229997Sken struct ctl_lun_req *lun_req; 1704229997Sken 1705229997Sken lun_req = (struct ctl_lun_req *)addr; 1706229997Sken 1707229997Sken switch (lun_req->reqtype) { 1708229997Sken case CTL_LUNREQ_CREATE: 1709229997Sken error = ctl_be_block_create(softc, lun_req); 1710229997Sken break; 1711229997Sken case CTL_LUNREQ_RM: 1712229997Sken error = ctl_be_block_rm(softc, lun_req); 1713229997Sken break; 1714232604Strasz case CTL_LUNREQ_MODIFY: 1715232604Strasz error = ctl_be_block_modify(softc, lun_req); 1716232604Strasz break; 1717229997Sken default: 1718229997Sken lun_req->status = CTL_LUN_ERROR; 1719229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1720272911Smav "invalid LUN request type %d", 1721229997Sken lun_req->reqtype); 1722229997Sken break; 1723229997Sken } 1724229997Sken break; 1725229997Sken } 1726229997Sken default: 1727229997Sken error = ENOTTY; 1728229997Sken break; 1729229997Sken } 1730229997Sken 1731229997Sken return (error); 1732229997Sken} 1733229997Sken 1734229997Skenstatic int 1735229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1736229997Sken{ 1737287499Smav struct ctl_be_lun *cbe_lun; 1738229997Sken struct ctl_be_block_filedata *file_data; 1739229997Sken struct ctl_lun_create_params *params; 1740275865Smav char *value; 1741229997Sken struct vattr vattr; 1742275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1743229997Sken int error; 1744229997Sken 1745229997Sken error = 0; 1746287499Smav cbe_lun = &be_lun->cbe_lun; 1747229997Sken file_data = &be_lun->backend.file; 1748272911Smav params = &be_lun->params; 1749229997Sken 1750229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1751229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1752229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1753275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1754275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1755287499Smav be_lun->unmap = NULL; 1756287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1757229997Sken 1758229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1759229997Sken if (error != 0) { 1760229997Sken snprintf(req->error_str, sizeof(req->error_str), 1761229997Sken "error calling VOP_GETATTR() for file %s", 1762229997Sken be_lun->dev_path); 1763229997Sken return (error); 1764229997Sken } 1765229997Sken 1766229997Sken /* 1767229997Sken * Verify that we have the ability to upgrade to exclusive 1768229997Sken * access on this file so we can trap errors at open instead 1769229997Sken * of reporting them during first access. 1770229997Sken */ 1771229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1772229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1773229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1774229997Sken error = EBADF; 1775229997Sken snprintf(req->error_str, sizeof(req->error_str), 1776229997Sken "error locking file %s", be_lun->dev_path); 1777229997Sken return (error); 1778229997Sken } 1779229997Sken } 1780229997Sken 1781229997Sken file_data->cred = crhold(curthread->td_ucred); 1782232604Strasz if (params->lun_size_bytes != 0) 1783232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1784232604Strasz else 1785232604Strasz be_lun->size_bytes = vattr.va_size; 1786229997Sken 1787229997Sken /* 1788273029Smav * For files we can use any logical block size. Prefer 512 bytes 1789273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1790273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1791273029Smav * logical block size -- report it as physical block size. 1792229997Sken */ 1793229997Sken if (params->blocksize_bytes != 0) 1794287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1795229997Sken else 1796287499Smav cbe_lun->blocksize = 512; 1797287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1798287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1799287499Smav 0 : (be_lun->size_blocks - 1); 1800275865Smav 1801275865Smav us = ps = vattr.va_blocksize; 1802275865Smav uo = po = 0; 1803275865Smav 1804287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1805275865Smav if (value != NULL) 1806275865Smav ctl_expand_number(value, &ps); 1807287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1808275865Smav if (value != NULL) 1809275865Smav ctl_expand_number(value, &po); 1810287499Smav pss = ps / cbe_lun->blocksize; 1811287499Smav pos = po / cbe_lun->blocksize; 1812287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1813287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1814287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1815287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1816273029Smav } 1817229997Sken 1818287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1819275865Smav if (value != NULL) 1820275865Smav ctl_expand_number(value, &us); 1821287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1822275865Smav if (value != NULL) 1823275865Smav ctl_expand_number(value, &uo); 1824287499Smav uss = us / cbe_lun->blocksize; 1825287499Smav uos = uo / cbe_lun->blocksize; 1826287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1827287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1828287499Smav cbe_lun->ublockexp = fls(uss) - 1; 1829287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 1830275865Smav } 1831275865Smav 1832229997Sken /* 1833229997Sken * Sanity check. The media size has to be at least one 1834229997Sken * sector long. 1835229997Sken */ 1836287499Smav if (be_lun->size_bytes < cbe_lun->blocksize) { 1837229997Sken error = EINVAL; 1838229997Sken snprintf(req->error_str, sizeof(req->error_str), 1839229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1840287499Smav (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1841229997Sken } 1842275920Smav 1843287499Smav cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1844229997Sken return (error); 1845229997Sken} 1846229997Sken 1847229997Skenstatic int 1848229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1849229997Sken{ 1850287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1851229997Sken struct ctl_lun_create_params *params; 1852229997Sken struct vattr vattr; 1853229997Sken struct cdev *dev; 1854229997Sken struct cdevsw *devsw; 1855275865Smav char *value; 1856287221Smav int error, atomic, maxio, unmap, tmp; 1857287221Smav off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1858229997Sken 1859272911Smav params = &be_lun->params; 1860229997Sken 1861229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1862229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1863229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1864229997Sken &be_lun->backend.dev.dev_ref); 1865229997Sken if (be_lun->backend.dev.csw == NULL) 1866229997Sken panic("Unable to retrieve device switch"); 1867275474Smav if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) { 1868269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1869275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1870275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1871275920Smav } else { 1872269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1873287499Smav be_lun->get_lba_status = NULL; 1874275920Smav atomic = 0; 1875275920Smav maxio = be_lun->backend.dev.cdev->si_iosize_max; 1876275920Smav if (maxio <= 0) 1877275920Smav maxio = DFLTPHYS; 1878275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1879275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1880275920Smav } 1881269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1882274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1883287499Smav be_lun->unmap = ctl_be_block_unmap_dev; 1884229997Sken 1885229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1886229997Sken if (error) { 1887229997Sken snprintf(req->error_str, sizeof(req->error_str), 1888272911Smav "error getting vnode attributes for device %s", 1889272911Smav be_lun->dev_path); 1890229997Sken return (error); 1891229997Sken } 1892229997Sken 1893229997Sken dev = be_lun->vn->v_rdev; 1894229997Sken devsw = dev->si_devsw; 1895229997Sken if (!devsw->d_ioctl) { 1896229997Sken snprintf(req->error_str, sizeof(req->error_str), 1897272911Smav "no d_ioctl for device %s!", 1898229997Sken be_lun->dev_path); 1899229997Sken return (ENODEV); 1900229997Sken } 1901229997Sken 1902286811Smav error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1903229997Sken curthread); 1904229997Sken if (error) { 1905229997Sken snprintf(req->error_str, sizeof(req->error_str), 1906272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1907272911Smav "on %s!", error, be_lun->dev_path); 1908229997Sken return (error); 1909229997Sken } 1910229997Sken 1911229997Sken /* 1912229997Sken * If the user has asked for a blocksize that is greater than the 1913229997Sken * backing device's blocksize, we can do it only if the blocksize 1914229997Sken * the user is asking for is an even multiple of the underlying 1915229997Sken * device's blocksize. 1916229997Sken */ 1917286811Smav if ((params->blocksize_bytes != 0) && 1918286811Smav (params->blocksize_bytes >= tmp)) { 1919286811Smav if (params->blocksize_bytes % tmp == 0) { 1920287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1921229997Sken } else { 1922229997Sken snprintf(req->error_str, sizeof(req->error_str), 1923272911Smav "requested blocksize %u is not an even " 1924229997Sken "multiple of backing device blocksize %u", 1925287221Smav params->blocksize_bytes, tmp); 1926229997Sken return (EINVAL); 1927229997Sken 1928229997Sken } 1929286811Smav } else if (params->blocksize_bytes != 0) { 1930229997Sken snprintf(req->error_str, sizeof(req->error_str), 1931272911Smav "requested blocksize %u < backing device " 1932287221Smav "blocksize %u", params->blocksize_bytes, tmp); 1933229997Sken return (EINVAL); 1934286811Smav } else 1935287499Smav cbe_lun->blocksize = tmp; 1936229997Sken 1937287221Smav error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1938229997Sken curthread); 1939229997Sken if (error) { 1940229997Sken snprintf(req->error_str, sizeof(req->error_str), 1941272911Smav "error %d returned for DIOCGMEDIASIZE " 1942272911Smav " ioctl on %s!", error, 1943232604Strasz be_lun->dev_path); 1944229997Sken return (error); 1945229997Sken } 1946229997Sken 1947232604Strasz if (params->lun_size_bytes != 0) { 1948287221Smav if (params->lun_size_bytes > otmp) { 1949232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1950272911Smav "requested LUN size %ju > backing device " 1951272911Smav "size %ju", 1952232604Strasz (uintmax_t)params->lun_size_bytes, 1953287221Smav (uintmax_t)otmp); 1954232604Strasz return (EINVAL); 1955232604Strasz } 1956232604Strasz 1957232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1958286811Smav } else 1959287221Smav be_lun->size_bytes = otmp; 1960287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1961287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1962287499Smav 0 : (be_lun->size_blocks - 1); 1963232604Strasz 1964264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1965264191Smav (caddr_t)&ps, FREAD, curthread); 1966264191Smav if (error) 1967264191Smav ps = po = 0; 1968264191Smav else { 1969264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1970264191Smav (caddr_t)&po, FREAD, curthread); 1971264191Smav if (error) 1972264191Smav po = 0; 1973264191Smav } 1974275865Smav us = ps; 1975275865Smav uo = po; 1976275865Smav 1977287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1978275865Smav if (value != NULL) 1979275865Smav ctl_expand_number(value, &ps); 1980287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1981275865Smav if (value != NULL) 1982275865Smav ctl_expand_number(value, &po); 1983287499Smav pss = ps / cbe_lun->blocksize; 1984287499Smav pos = po / cbe_lun->blocksize; 1985287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1986287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1987287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1988287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1989264191Smav } 1990264191Smav 1991287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1992275865Smav if (value != NULL) 1993275865Smav ctl_expand_number(value, &us); 1994287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1995275865Smav if (value != NULL) 1996275865Smav ctl_expand_number(value, &uo); 1997287499Smav uss = us / cbe_lun->blocksize; 1998287499Smav uos = uo / cbe_lun->blocksize; 1999287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2000287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2001287499Smav cbe_lun->ublockexp = fls(uss) - 1; 2002287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 2003275865Smav } 2004275865Smav 2005287499Smav cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2006287499Smav cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2007278672Smav 2008278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2009278672Smav unmap = 1; 2010278672Smav } else { 2011278672Smav struct diocgattr_arg arg; 2012278672Smav 2013278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2014278672Smav arg.len = sizeof(arg.value.i); 2015278672Smav error = devsw->d_ioctl(dev, DIOCGATTR, 2016278672Smav (caddr_t)&arg, FREAD, curthread); 2017278672Smav unmap = (error == 0) ? arg.value.i : 0; 2018278672Smav } 2019287499Smav value = ctl_get_opt(&cbe_lun->options, "unmap"); 2020278672Smav if (value != NULL) 2021278672Smav unmap = (strcmp(value, "on") == 0); 2022278672Smav if (unmap) 2023287499Smav cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2024287499Smav else 2025287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2026278672Smav 2027229997Sken return (0); 2028229997Sken} 2029229997Sken 2030229997Skenstatic int 2031229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2032229997Sken{ 2033287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2034287499Smav int flags; 2035287499Smav 2036229997Sken if (be_lun->vn) { 2037229997Sken switch (be_lun->dev_type) { 2038229997Sken case CTL_BE_BLOCK_DEV: 2039229997Sken if (be_lun->backend.dev.csw) { 2040229997Sken dev_relthread(be_lun->backend.dev.cdev, 2041229997Sken be_lun->backend.dev.dev_ref); 2042229997Sken be_lun->backend.dev.csw = NULL; 2043229997Sken be_lun->backend.dev.cdev = NULL; 2044229997Sken } 2045229997Sken break; 2046229997Sken case CTL_BE_BLOCK_FILE: 2047229997Sken break; 2048229997Sken case CTL_BE_BLOCK_NONE: 2049258871Strasz break; 2050229997Sken default: 2051229997Sken panic("Unexpected backend type."); 2052229997Sken break; 2053229997Sken } 2054229997Sken 2055287499Smav flags = FREAD; 2056287499Smav if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2057287499Smav flags |= FWRITE; 2058229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2059229997Sken be_lun->vn = NULL; 2060229997Sken 2061229997Sken switch (be_lun->dev_type) { 2062229997Sken case CTL_BE_BLOCK_DEV: 2063229997Sken break; 2064229997Sken case CTL_BE_BLOCK_FILE: 2065229997Sken if (be_lun->backend.file.cred != NULL) { 2066229997Sken crfree(be_lun->backend.file.cred); 2067229997Sken be_lun->backend.file.cred = NULL; 2068229997Sken } 2069229997Sken break; 2070229997Sken case CTL_BE_BLOCK_NONE: 2071258871Strasz break; 2072229997Sken default: 2073229997Sken panic("Unexpected backend type."); 2074229997Sken break; 2075229997Sken } 2076272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2077229997Sken } 2078229997Sken return (0); 2079229997Sken} 2080229997Sken 2081229997Skenstatic int 2082229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 2083287499Smav struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2084229997Sken{ 2085287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2086229997Sken struct nameidata nd; 2087287499Smav char *value; 2088287499Smav int error, flags; 2089229997Sken 2090229997Sken error = 0; 2091229997Sken if (rootvnode == NULL) { 2092229997Sken snprintf(req->error_str, sizeof(req->error_str), 2093272911Smav "Root filesystem is not mounted"); 2094229997Sken return (1); 2095229997Sken } 2096285391Smjg pwd_ensure_dirs(); 2097229997Sken 2098287499Smav value = ctl_get_opt(&cbe_lun->options, "file"); 2099287499Smav if (value == NULL) { 2100287499Smav snprintf(req->error_str, sizeof(req->error_str), 2101287499Smav "no file argument specified"); 2102287499Smav return (1); 2103287499Smav } 2104287499Smav free(be_lun->dev_path, M_CTLBLK); 2105287499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2106287499Smav 2107287499Smav flags = FREAD; 2108287499Smav value = ctl_get_opt(&cbe_lun->options, "readonly"); 2109287499Smav if (value == NULL || strcmp(value, "on") != 0) 2110287499Smav flags |= FWRITE; 2111287499Smav 2112287499Smavagain: 2113229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2114229997Sken error = vn_open(&nd, &flags, 0, NULL); 2115287499Smav if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2116287499Smav flags &= ~FWRITE; 2117287499Smav goto again; 2118287499Smav } 2119229997Sken if (error) { 2120229997Sken /* 2121229997Sken * This is the only reasonable guess we can make as far as 2122229997Sken * path if the user doesn't give us a fully qualified path. 2123229997Sken * If they want to specify a file, they need to specify the 2124229997Sken * full path. 2125229997Sken */ 2126229997Sken if (be_lun->dev_path[0] != '/') { 2127229997Sken char *dev_name; 2128229997Sken 2129287499Smav asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2130287499Smav be_lun->dev_path); 2131287499Smav free(be_lun->dev_path, M_CTLBLK); 2132287499Smav be_lun->dev_path = dev_name; 2133287499Smav goto again; 2134229997Sken } 2135229997Sken snprintf(req->error_str, sizeof(req->error_str), 2136272911Smav "error opening %s: %d", be_lun->dev_path, error); 2137229997Sken return (error); 2138229997Sken } 2139287499Smav if (flags & FWRITE) 2140287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2141287499Smav else 2142287499Smav cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2143229997Sken 2144229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2145229997Sken be_lun->vn = nd.ni_vp; 2146229997Sken 2147229997Sken /* We only support disks and files. */ 2148229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2149229997Sken error = ctl_be_block_open_dev(be_lun, req); 2150229997Sken } else if (be_lun->vn->v_type == VREG) { 2151229997Sken error = ctl_be_block_open_file(be_lun, req); 2152229997Sken } else { 2153229997Sken error = EINVAL; 2154229997Sken snprintf(req->error_str, sizeof(req->error_str), 2155258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2156229997Sken } 2157229997Sken VOP_UNLOCK(be_lun->vn, 0); 2158229997Sken 2159286811Smav if (error != 0) 2160229997Sken ctl_be_block_close(be_lun); 2161287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2162287499Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2163287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2164287499Smav value = ctl_get_opt(&cbe_lun->options, "serseq"); 2165287499Smav if (value != NULL && strcmp(value, "on") == 0) 2166287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2167287499Smav else if (value != NULL && strcmp(value, "read") == 0) 2168287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2169287499Smav else if (value != NULL && strcmp(value, "off") == 0) 2170287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2171229997Sken return (0); 2172229997Sken} 2173229997Sken 2174229997Skenstatic int 2175229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2176229997Sken{ 2177287499Smav struct ctl_be_lun *cbe_lun; 2178229997Sken struct ctl_be_block_lun *be_lun; 2179229997Sken struct ctl_lun_create_params *params; 2180267481Smav char num_thread_str[16]; 2181229997Sken char tmpstr[32]; 2182267481Smav char *value; 2183278672Smav int retval, num_threads; 2184267481Smav int tmp_num_threads; 2185229997Sken 2186229997Sken params = &req->reqdata.create; 2187229997Sken retval = 0; 2188272911Smav req->status = CTL_LUN_OK; 2189229997Sken 2190229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2191287499Smav cbe_lun = &be_lun->cbe_lun; 2192287499Smav cbe_lun->be_lun = be_lun; 2193272911Smav be_lun->params = req->reqdata.create; 2194229997Sken be_lun->softc = softc; 2195229997Sken STAILQ_INIT(&be_lun->input_queue); 2196275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2197229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2198229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2199229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2200267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2201267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2202287499Smav ctl_init_opts(&cbe_lun->options, 2203268280Smav req->num_be_args, req->kern_be_args); 2204264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2205256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2206229997Sken if (be_lun->lun_zone == NULL) { 2207229997Sken snprintf(req->error_str, sizeof(req->error_str), 2208272911Smav "error allocating UMA zone"); 2209229997Sken goto bailout_error; 2210229997Sken } 2211229997Sken 2212229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2213287499Smav cbe_lun->lun_type = params->device_type; 2214229997Sken else 2215287499Smav cbe_lun->lun_type = T_DIRECT; 2216287499Smav be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2217287499Smav cbe_lun->flags = CTL_LUN_FLAG_PRIMARY; 2218229997Sken 2219287499Smav if (cbe_lun->lun_type == T_DIRECT) { 2220286811Smav be_lun->size_bytes = params->lun_size_bytes; 2221286811Smav if (params->blocksize_bytes != 0) 2222287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2223286811Smav else 2224287499Smav cbe_lun->blocksize = 512; 2225287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2226287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2227287499Smav 0 : (be_lun->size_blocks - 1); 2228229997Sken 2229229997Sken retval = ctl_be_block_open(softc, be_lun, req); 2230229997Sken if (retval != 0) { 2231229997Sken retval = 0; 2232272911Smav req->status = CTL_LUN_WARNING; 2233229997Sken } 2234287499Smav num_threads = cbb_num_threads; 2235229997Sken } else { 2236229997Sken num_threads = 1; 2237229997Sken } 2238229997Sken 2239229997Sken /* 2240229997Sken * XXX This searching loop might be refactored to be combined with 2241229997Sken * the loop above, 2242229997Sken */ 2243287499Smav value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2244267481Smav if (value != NULL) { 2245267481Smav tmp_num_threads = strtol(value, NULL, 0); 2246229997Sken 2247267481Smav /* 2248267481Smav * We don't let the user specify less than one 2249267481Smav * thread, but hope he's clueful enough not to 2250267481Smav * specify 1000 threads. 2251267481Smav */ 2252267481Smav if (tmp_num_threads < 1) { 2253267481Smav snprintf(req->error_str, sizeof(req->error_str), 2254272911Smav "invalid number of threads %s", 2255272911Smav num_thread_str); 2256267481Smav goto bailout_error; 2257229997Sken } 2258267481Smav num_threads = tmp_num_threads; 2259229997Sken } 2260229997Sken 2261272911Smav if (be_lun->vn == NULL) 2262287499Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2263229997Sken /* Tell the user the blocksize we ended up using */ 2264272911Smav params->lun_size_bytes = be_lun->size_bytes; 2265287499Smav params->blocksize_bytes = cbe_lun->blocksize; 2266229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2267287499Smav cbe_lun->req_lun_id = params->req_lun_id; 2268287499Smav cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2269229997Sken } else 2270287499Smav cbe_lun->req_lun_id = 0; 2271229997Sken 2272287499Smav cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2273287499Smav cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2274287499Smav cbe_lun->be = &ctl_be_block_driver; 2275229997Sken 2276229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2277229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2278229997Sken softc->num_luns); 2279287499Smav strncpy((char *)cbe_lun->serial_num, tmpstr, 2280287499Smav MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2281229997Sken 2282229997Sken /* Tell the user what we used for a serial number */ 2283229997Sken strncpy((char *)params->serial_num, tmpstr, 2284275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2285229997Sken } else { 2286287499Smav strncpy((char *)cbe_lun->serial_num, params->serial_num, 2287287499Smav MIN(sizeof(cbe_lun->serial_num), 2288229997Sken sizeof(params->serial_num))); 2289229997Sken } 2290229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2291229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2292287499Smav strncpy((char *)cbe_lun->device_id, tmpstr, 2293287499Smav MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2294229997Sken 2295229997Sken /* Tell the user what we used for a device ID */ 2296229997Sken strncpy((char *)params->device_id, tmpstr, 2297275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2298229997Sken } else { 2299287499Smav strncpy((char *)cbe_lun->device_id, params->device_id, 2300287499Smav MIN(sizeof(cbe_lun->device_id), 2301275953Smav sizeof(params->device_id))); 2302229997Sken } 2303229997Sken 2304229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2305229997Sken 2306229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2307229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2308229997Sken 2309229997Sken if (be_lun->io_taskqueue == NULL) { 2310229997Sken snprintf(req->error_str, sizeof(req->error_str), 2311272911Smav "unable to create taskqueue"); 2312229997Sken goto bailout_error; 2313229997Sken } 2314229997Sken 2315229997Sken /* 2316229997Sken * Note that we start the same number of threads by default for 2317229997Sken * both the file case and the block device case. For the file 2318229997Sken * case, we need multiple threads to allow concurrency, because the 2319229997Sken * vnode interface is designed to be a blocking interface. For the 2320229997Sken * block device case, ZFS zvols at least will block the caller's 2321229997Sken * context in many instances, and so we need multiple threads to 2322229997Sken * overcome that problem. Other block devices don't need as many 2323229997Sken * threads, but they shouldn't cause too many problems. 2324229997Sken * 2325229997Sken * If the user wants to just have a single thread for a block 2326229997Sken * device, he can specify that when the LUN is created, or change 2327229997Sken * the tunable/sysctl to alter the default number of threads. 2328229997Sken */ 2329229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2330229997Sken /*num threads*/num_threads, 2331229997Sken /*priority*/PWAIT, 2332229997Sken /*thread name*/ 2333229997Sken "%s taskq", be_lun->lunname); 2334229997Sken 2335229997Sken if (retval != 0) 2336229997Sken goto bailout_error; 2337229997Sken 2338229997Sken be_lun->num_threads = num_threads; 2339229997Sken 2340229997Sken mtx_lock(&softc->lock); 2341229997Sken softc->num_luns++; 2342229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2343229997Sken 2344229997Sken mtx_unlock(&softc->lock); 2345229997Sken 2346287499Smav retval = ctl_add_lun(&be_lun->cbe_lun); 2347229997Sken if (retval != 0) { 2348229997Sken mtx_lock(&softc->lock); 2349229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2350229997Sken links); 2351229997Sken softc->num_luns--; 2352229997Sken mtx_unlock(&softc->lock); 2353229997Sken snprintf(req->error_str, sizeof(req->error_str), 2354272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2355272911Smav "details", retval); 2356229997Sken retval = 0; 2357229997Sken goto bailout_error; 2358229997Sken } 2359229997Sken 2360229997Sken mtx_lock(&softc->lock); 2361229997Sken 2362229997Sken /* 2363229997Sken * Tell the config_status routine that we're waiting so it won't 2364229997Sken * clean up the LUN in the event of an error. 2365229997Sken */ 2366229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2367229997Sken 2368229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2369229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2370229997Sken if (retval == EINTR) 2371229997Sken break; 2372229997Sken } 2373229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2374229997Sken 2375229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2376229997Sken snprintf(req->error_str, sizeof(req->error_str), 2377272911Smav "LUN configuration error, see dmesg for details"); 2378229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2379229997Sken links); 2380229997Sken softc->num_luns--; 2381229997Sken mtx_unlock(&softc->lock); 2382229997Sken goto bailout_error; 2383229997Sken } else { 2384287499Smav params->req_lun_id = cbe_lun->lun_id; 2385229997Sken } 2386229997Sken 2387229997Sken mtx_unlock(&softc->lock); 2388229997Sken 2389229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2390287499Smav cbe_lun->blocksize, 2391229997Sken DEVSTAT_ALL_SUPPORTED, 2392287499Smav cbe_lun->lun_type 2393229997Sken | DEVSTAT_TYPE_IF_OTHER, 2394229997Sken DEVSTAT_PRIORITY_OTHER); 2395229997Sken 2396229997Sken return (retval); 2397229997Sken 2398229997Skenbailout_error: 2399229997Sken req->status = CTL_LUN_ERROR; 2400229997Sken 2401267429Smav if (be_lun->io_taskqueue != NULL) 2402267429Smav taskqueue_free(be_lun->io_taskqueue); 2403229997Sken ctl_be_block_close(be_lun); 2404267429Smav if (be_lun->dev_path != NULL) 2405267429Smav free(be_lun->dev_path, M_CTLBLK); 2406267429Smav if (be_lun->lun_zone != NULL) 2407267429Smav uma_zdestroy(be_lun->lun_zone); 2408287499Smav ctl_free_opts(&cbe_lun->options); 2409267877Smav mtx_destroy(&be_lun->queue_lock); 2410267877Smav mtx_destroy(&be_lun->io_lock); 2411229997Sken free(be_lun, M_CTLBLK); 2412229997Sken 2413229997Sken return (retval); 2414229997Sken} 2415229997Sken 2416229997Skenstatic int 2417229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2418229997Sken{ 2419229997Sken struct ctl_lun_rm_params *params; 2420229997Sken struct ctl_be_block_lun *be_lun; 2421229997Sken int retval; 2422229997Sken 2423229997Sken params = &req->reqdata.rm; 2424229997Sken 2425229997Sken mtx_lock(&softc->lock); 2426229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2427287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2428229997Sken break; 2429229997Sken } 2430229997Sken mtx_unlock(&softc->lock); 2431229997Sken 2432229997Sken if (be_lun == NULL) { 2433229997Sken snprintf(req->error_str, sizeof(req->error_str), 2434272911Smav "LUN %u is not managed by the block backend", 2435272911Smav params->lun_id); 2436229997Sken goto bailout_error; 2437229997Sken } 2438229997Sken 2439287499Smav retval = ctl_disable_lun(&be_lun->cbe_lun); 2440229997Sken 2441229997Sken if (retval != 0) { 2442229997Sken snprintf(req->error_str, sizeof(req->error_str), 2443272911Smav "error %d returned from ctl_disable_lun() for " 2444272911Smav "LUN %d", retval, params->lun_id); 2445229997Sken goto bailout_error; 2446229997Sken 2447229997Sken } 2448229997Sken 2449287499Smav retval = ctl_invalidate_lun(&be_lun->cbe_lun); 2450229997Sken if (retval != 0) { 2451229997Sken snprintf(req->error_str, sizeof(req->error_str), 2452272911Smav "error %d returned from ctl_invalidate_lun() for " 2453272911Smav "LUN %d", retval, params->lun_id); 2454229997Sken goto bailout_error; 2455229997Sken } 2456229997Sken 2457229997Sken mtx_lock(&softc->lock); 2458229997Sken 2459229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2460229997Sken 2461229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2462229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2463229997Sken if (retval == EINTR) 2464229997Sken break; 2465229997Sken } 2466229997Sken 2467229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2468229997Sken 2469229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2470229997Sken snprintf(req->error_str, sizeof(req->error_str), 2471272911Smav "interrupted waiting for LUN to be freed"); 2472229997Sken mtx_unlock(&softc->lock); 2473229997Sken goto bailout_error; 2474229997Sken } 2475229997Sken 2476229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2477229997Sken 2478229997Sken softc->num_luns--; 2479229997Sken mtx_unlock(&softc->lock); 2480229997Sken 2481229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2482229997Sken 2483229997Sken taskqueue_free(be_lun->io_taskqueue); 2484229997Sken 2485229997Sken ctl_be_block_close(be_lun); 2486229997Sken 2487229997Sken if (be_lun->disk_stats != NULL) 2488229997Sken devstat_remove_entry(be_lun->disk_stats); 2489229997Sken 2490229997Sken uma_zdestroy(be_lun->lun_zone); 2491229997Sken 2492287499Smav ctl_free_opts(&be_lun->cbe_lun.options); 2493229997Sken free(be_lun->dev_path, M_CTLBLK); 2494267877Smav mtx_destroy(&be_lun->queue_lock); 2495267877Smav mtx_destroy(&be_lun->io_lock); 2496229997Sken free(be_lun, M_CTLBLK); 2497229997Sken 2498229997Sken req->status = CTL_LUN_OK; 2499229997Sken 2500229997Sken return (0); 2501229997Sken 2502229997Skenbailout_error: 2503229997Sken 2504229997Sken req->status = CTL_LUN_ERROR; 2505229997Sken 2506229997Sken return (0); 2507229997Sken} 2508229997Sken 2509232604Straszstatic int 2510232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2511232604Strasz struct ctl_lun_req *req) 2512232604Strasz{ 2513287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2514232604Strasz struct vattr vattr; 2515232604Strasz int error; 2516272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2517232604Strasz 2518232604Strasz if (params->lun_size_bytes != 0) { 2519232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2520232604Strasz } else { 2521271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2522232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2523271794Smav VOP_UNLOCK(be_lun->vn, 0); 2524232604Strasz if (error != 0) { 2525232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2526232604Strasz "error calling VOP_GETATTR() for file %s", 2527232604Strasz be_lun->dev_path); 2528232604Strasz return (error); 2529232604Strasz } 2530232604Strasz be_lun->size_bytes = vattr.va_size; 2531232604Strasz } 2532287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2533287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2534287499Smav 0 : (be_lun->size_blocks - 1); 2535232604Strasz return (0); 2536232604Strasz} 2537232604Strasz 2538232604Straszstatic int 2539232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2540232604Strasz struct ctl_lun_req *req) 2541232604Strasz{ 2542287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2543271794Smav struct ctl_be_block_devdata *dev_data; 2544232604Strasz int error; 2545272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2546232604Strasz uint64_t size_bytes; 2547232604Strasz 2548271794Smav dev_data = &be_lun->backend.dev; 2549271794Smav if (!dev_data->csw->d_ioctl) { 2550232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2551272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2552232604Strasz return (ENODEV); 2553232604Strasz } 2554232604Strasz 2555271794Smav error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2556232604Strasz (caddr_t)&size_bytes, FREAD, 2557232604Strasz curthread); 2558232604Strasz if (error) { 2559232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2560272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2561272911Smav "on %s!", error, be_lun->dev_path); 2562232604Strasz return (error); 2563232604Strasz } 2564232604Strasz 2565232604Strasz if (params->lun_size_bytes != 0) { 2566232604Strasz if (params->lun_size_bytes > size_bytes) { 2567232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2568272911Smav "requested LUN size %ju > backing device " 2569272911Smav "size %ju", 2570232604Strasz (uintmax_t)params->lun_size_bytes, 2571232604Strasz (uintmax_t)size_bytes); 2572232604Strasz return (EINVAL); 2573232604Strasz } 2574232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2575232604Strasz } else { 2576232604Strasz be_lun->size_bytes = size_bytes; 2577232604Strasz } 2578287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2579287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2580287499Smav 0 : (be_lun->size_blocks - 1); 2581232604Strasz return (0); 2582232604Strasz} 2583232604Strasz 2584232604Straszstatic int 2585232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2586232604Strasz{ 2587232604Strasz struct ctl_lun_modify_params *params; 2588232604Strasz struct ctl_be_block_lun *be_lun; 2589287500Smav struct ctl_be_lun *cbe_lun; 2590271794Smav uint64_t oldsize; 2591241896Skib int error; 2592232604Strasz 2593232604Strasz params = &req->reqdata.modify; 2594232604Strasz 2595232604Strasz mtx_lock(&softc->lock); 2596232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2597287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2598232604Strasz break; 2599232604Strasz } 2600232604Strasz mtx_unlock(&softc->lock); 2601232604Strasz 2602232604Strasz if (be_lun == NULL) { 2603232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2604272911Smav "LUN %u is not managed by the block backend", 2605272911Smav params->lun_id); 2606232604Strasz goto bailout_error; 2607232604Strasz } 2608287500Smav cbe_lun = &be_lun->cbe_lun; 2609232604Strasz 2610287500Smav if (params->lun_size_bytes != 0) 2611287500Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2612287500Smav ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2613232604Strasz 2614287499Smav oldsize = be_lun->size_blocks; 2615272911Smav if (be_lun->vn == NULL) 2616272911Smav error = ctl_be_block_open(softc, be_lun, req); 2617285030Smav else if (vn_isdisk(be_lun->vn, &error)) 2618285030Smav error = ctl_be_block_modify_dev(be_lun, req); 2619272911Smav else if (be_lun->vn->v_type == VREG) 2620232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2621232604Strasz else 2622285030Smav error = EINVAL; 2623232604Strasz 2624287499Smav if (be_lun->size_blocks != oldsize) 2625287500Smav ctl_lun_capacity_changed(cbe_lun); 2626287500Smav if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) && 2627287499Smav be_lun->vn != NULL) { 2628287500Smav cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE; 2629287500Smav ctl_lun_online(cbe_lun); 2630271794Smav } 2631232604Strasz 2632232604Strasz /* Tell the user the exact size we ended up using */ 2633232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2634232604Strasz 2635272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2636232604Strasz return (0); 2637232604Strasz 2638232604Straszbailout_error: 2639232604Strasz req->status = CTL_LUN_ERROR; 2640232604Strasz return (0); 2641232604Strasz} 2642232604Strasz 2643229997Skenstatic void 2644229997Skenctl_be_block_lun_shutdown(void *be_lun) 2645229997Sken{ 2646229997Sken struct ctl_be_block_lun *lun; 2647229997Sken struct ctl_be_block_softc *softc; 2648229997Sken 2649229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2650229997Sken 2651229997Sken softc = lun->softc; 2652229997Sken 2653229997Sken mtx_lock(&softc->lock); 2654229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2655229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2656229997Sken wakeup(lun); 2657229997Sken mtx_unlock(&softc->lock); 2658229997Sken 2659229997Sken} 2660229997Sken 2661229997Skenstatic void 2662229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2663229997Sken{ 2664229997Sken struct ctl_be_block_lun *lun; 2665229997Sken struct ctl_be_block_softc *softc; 2666229997Sken 2667229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2668229997Sken softc = lun->softc; 2669229997Sken 2670229997Sken if (status == CTL_LUN_CONFIG_OK) { 2671229997Sken mtx_lock(&softc->lock); 2672229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2673229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2674229997Sken wakeup(lun); 2675229997Sken mtx_unlock(&softc->lock); 2676229997Sken 2677229997Sken /* 2678229997Sken * We successfully added the LUN, attempt to enable it. 2679229997Sken */ 2680287499Smav if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2681229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2682287499Smav if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2683229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2684229997Sken __func__); 2685229997Sken } 2686229997Sken } 2687229997Sken 2688229997Sken return; 2689229997Sken } 2690229997Sken 2691229997Sken 2692229997Sken mtx_lock(&softc->lock); 2693229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2694229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2695229997Sken wakeup(lun); 2696229997Sken mtx_unlock(&softc->lock); 2697229997Sken} 2698229997Sken 2699229997Sken 2700229997Skenstatic int 2701229997Skenctl_be_block_config_write(union ctl_io *io) 2702229997Sken{ 2703229997Sken struct ctl_be_block_lun *be_lun; 2704287499Smav struct ctl_be_lun *cbe_lun; 2705229997Sken int retval; 2706229997Sken 2707229997Sken retval = 0; 2708229997Sken 2709229997Sken DPRINTF("entered\n"); 2710229997Sken 2711287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2712229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2713287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2714229997Sken 2715229997Sken switch (io->scsiio.cdb[0]) { 2716229997Sken case SYNCHRONIZE_CACHE: 2717229997Sken case SYNCHRONIZE_CACHE_16: 2718264274Smav case WRITE_SAME_10: 2719264274Smav case WRITE_SAME_16: 2720264274Smav case UNMAP: 2721229997Sken /* 2722229997Sken * The upper level CTL code will filter out any CDBs with 2723229997Sken * the immediate bit set and return the proper error. 2724229997Sken * 2725229997Sken * We don't really need to worry about what LBA range the 2726229997Sken * user asked to be synced out. When they issue a sync 2727229997Sken * cache command, we'll sync out the whole thing. 2728229997Sken */ 2729267877Smav mtx_lock(&be_lun->queue_lock); 2730229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2731229997Sken links); 2732267877Smav mtx_unlock(&be_lun->queue_lock); 2733229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2734229997Sken break; 2735229997Sken case START_STOP_UNIT: { 2736229997Sken struct scsi_start_stop_unit *cdb; 2737229997Sken 2738229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2739229997Sken 2740229997Sken if (cdb->how & SSS_START) 2741287499Smav retval = ctl_start_lun(cbe_lun); 2742229997Sken else { 2743287499Smav retval = ctl_stop_lun(cbe_lun); 2744229997Sken /* 2745229997Sken * XXX KDM Copan-specific offline behavior. 2746229997Sken * Figure out a reasonable way to port this? 2747229997Sken */ 2748229997Sken#ifdef NEEDTOPORT 2749229997Sken if ((retval == 0) 2750229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2751287499Smav retval = ctl_lun_offline(cbe_lun); 2752229997Sken#endif 2753229997Sken } 2754229997Sken 2755229997Sken /* 2756229997Sken * In general, the above routines should not fail. They 2757229997Sken * just set state for the LUN. So we've got something 2758229997Sken * pretty wrong here if we can't start or stop the LUN. 2759229997Sken */ 2760229997Sken if (retval != 0) { 2761229997Sken ctl_set_internal_failure(&io->scsiio, 2762229997Sken /*sks_valid*/ 1, 2763229997Sken /*retry_count*/ 0xf051); 2764229997Sken retval = CTL_RETVAL_COMPLETE; 2765229997Sken } else { 2766229997Sken ctl_set_success(&io->scsiio); 2767229997Sken } 2768229997Sken ctl_config_write_done(io); 2769229997Sken break; 2770229997Sken } 2771229997Sken default: 2772229997Sken ctl_set_invalid_opcode(&io->scsiio); 2773229997Sken ctl_config_write_done(io); 2774229997Sken retval = CTL_RETVAL_COMPLETE; 2775229997Sken break; 2776229997Sken } 2777229997Sken 2778229997Sken return (retval); 2779229997Sken} 2780229997Sken 2781229997Skenstatic int 2782229997Skenctl_be_block_config_read(union ctl_io *io) 2783229997Sken{ 2784275474Smav struct ctl_be_block_lun *be_lun; 2785287499Smav struct ctl_be_lun *cbe_lun; 2786275474Smav int retval = 0; 2787275474Smav 2788275474Smav DPRINTF("entered\n"); 2789275474Smav 2790287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2791275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2792287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2793275474Smav 2794275474Smav switch (io->scsiio.cdb[0]) { 2795275474Smav case SERVICE_ACTION_IN: 2796275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2797275474Smav mtx_lock(&be_lun->queue_lock); 2798275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2799275474Smav &io->io_hdr, links); 2800275474Smav mtx_unlock(&be_lun->queue_lock); 2801275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2802275474Smav &be_lun->io_task); 2803275474Smav retval = CTL_RETVAL_QUEUED; 2804275474Smav break; 2805275474Smav } 2806275474Smav ctl_set_invalid_field(&io->scsiio, 2807275474Smav /*sks_valid*/ 1, 2808275474Smav /*command*/ 1, 2809275474Smav /*field*/ 1, 2810275474Smav /*bit_valid*/ 1, 2811275474Smav /*bit*/ 4); 2812275474Smav ctl_config_read_done(io); 2813275474Smav retval = CTL_RETVAL_COMPLETE; 2814275474Smav break; 2815275474Smav default: 2816275474Smav ctl_set_invalid_opcode(&io->scsiio); 2817275474Smav ctl_config_read_done(io); 2818275474Smav retval = CTL_RETVAL_COMPLETE; 2819275474Smav break; 2820275474Smav } 2821275474Smav 2822275474Smav return (retval); 2823229997Sken} 2824229997Sken 2825229997Skenstatic int 2826229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2827229997Sken{ 2828229997Sken struct ctl_be_block_lun *lun; 2829229997Sken int retval; 2830229997Sken 2831229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2832229997Sken retval = 0; 2833229997Sken 2834268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2835229997Sken 2836229997Sken if (retval != 0) 2837229997Sken goto bailout; 2838229997Sken 2839229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2840229997Sken 2841229997Sken if (retval != 0) 2842229997Sken goto bailout; 2843229997Sken 2844268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2845229997Sken 2846229997Skenbailout: 2847229997Sken 2848229997Sken return (retval); 2849229997Sken} 2850229997Sken 2851274154Smavstatic uint64_t 2852274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2853274154Smav{ 2854274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2855274154Smav 2856274154Smav if (lun->getattr == NULL) 2857274154Smav return (UINT64_MAX); 2858274154Smav return (lun->getattr(lun, attrname)); 2859274154Smav} 2860274154Smav 2861229997Skenint 2862229997Skenctl_be_block_init(void) 2863229997Sken{ 2864229997Sken struct ctl_be_block_softc *softc; 2865229997Sken int retval; 2866229997Sken 2867229997Sken softc = &backend_block_softc; 2868229997Sken retval = 0; 2869229997Sken 2870267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2871264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2872264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2873229997Sken STAILQ_INIT(&softc->lun_list); 2874229997Sken 2875229997Sken return (retval); 2876229997Sken} 2877