ctl_backend_block.c revision 274154
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 274154 2014-11-06 00:48:36Z mav $"); 44229997Sken 45229997Sken#include <sys/param.h> 46229997Sken#include <sys/systm.h> 47229997Sken#include <sys/kernel.h> 48229997Sken#include <sys/types.h> 49229997Sken#include <sys/kthread.h> 50229997Sken#include <sys/bio.h> 51229997Sken#include <sys/fcntl.h> 52264274Smav#include <sys/limits.h> 53229997Sken#include <sys/lock.h> 54229997Sken#include <sys/mutex.h> 55229997Sken#include <sys/condvar.h> 56229997Sken#include <sys/malloc.h> 57229997Sken#include <sys/conf.h> 58229997Sken#include <sys/ioccom.h> 59229997Sken#include <sys/queue.h> 60229997Sken#include <sys/sbuf.h> 61229997Sken#include <sys/endian.h> 62229997Sken#include <sys/uio.h> 63229997Sken#include <sys/buf.h> 64229997Sken#include <sys/taskqueue.h> 65229997Sken#include <sys/vnode.h> 66229997Sken#include <sys/namei.h> 67229997Sken#include <sys/mount.h> 68229997Sken#include <sys/disk.h> 69229997Sken#include <sys/fcntl.h> 70229997Sken#include <sys/filedesc.h> 71229997Sken#include <sys/proc.h> 72229997Sken#include <sys/pcpu.h> 73229997Sken#include <sys/module.h> 74229997Sken#include <sys/sdt.h> 75229997Sken#include <sys/devicestat.h> 76229997Sken#include <sys/sysctl.h> 77229997Sken 78229997Sken#include <geom/geom.h> 79229997Sken 80229997Sken#include <cam/cam.h> 81229997Sken#include <cam/scsi/scsi_all.h> 82229997Sken#include <cam/scsi/scsi_da.h> 83229997Sken#include <cam/ctl/ctl_io.h> 84229997Sken#include <cam/ctl/ctl.h> 85229997Sken#include <cam/ctl/ctl_backend.h> 86229997Sken#include <cam/ctl/ctl_frontend_internal.h> 87229997Sken#include <cam/ctl/ctl_ioctl.h> 88229997Sken#include <cam/ctl/ctl_scsi_all.h> 89229997Sken#include <cam/ctl/ctl_error.h> 90229997Sken 91229997Sken/* 92264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 93264886Smav * I/O. If we get an I/O larger than that, we'll split it. 94229997Sken */ 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_MULTI_THREAD = 0x08 120229997Sken} ctl_be_block_lun_flags; 121229997Sken 122229997Skentypedef enum { 123229997Sken CTL_BE_BLOCK_NONE, 124229997Sken CTL_BE_BLOCK_DEV, 125229997Sken CTL_BE_BLOCK_FILE 126229997Sken} ctl_be_block_type; 127229997Sken 128229997Skenstruct ctl_be_block_devdata { 129229997Sken struct cdev *cdev; 130229997Sken struct cdevsw *csw; 131229997Sken int dev_ref; 132229997Sken}; 133229997Sken 134229997Skenstruct ctl_be_block_filedata { 135229997Sken struct ucred *cred; 136229997Sken}; 137229997Sken 138229997Skenunion ctl_be_block_bedata { 139229997Sken struct ctl_be_block_devdata dev; 140229997Sken struct ctl_be_block_filedata file; 141229997Sken}; 142229997Sken 143229997Skenstruct ctl_be_block_io; 144229997Skenstruct ctl_be_block_lun; 145229997Sken 146229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 147229997Sken struct ctl_be_block_io *beio); 148274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 149274154Smav const char *attrname); 150229997Sken 151229997Sken/* 152229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 153229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 154229997Sken */ 155229997Skenstruct ctl_be_block_lun { 156272911Smav struct ctl_lun_create_params params; 157229997Sken struct ctl_block_disk *disk; 158229997Sken char lunname[32]; 159229997Sken char *dev_path; 160229997Sken ctl_be_block_type dev_type; 161229997Sken struct vnode *vn; 162229997Sken union ctl_be_block_bedata backend; 163229997Sken cbb_dispatch_t dispatch; 164229997Sken cbb_dispatch_t lun_flush; 165264274Smav cbb_dispatch_t unmap; 166274154Smav cbb_getattr_t getattr; 167229997Sken uma_zone_t lun_zone; 168229997Sken uint64_t size_blocks; 169229997Sken uint64_t size_bytes; 170229997Sken uint32_t blocksize; 171229997Sken int blocksize_shift; 172264191Smav uint16_t pblockexp; 173264191Smav uint16_t pblockoff; 174229997Sken struct ctl_be_block_softc *softc; 175229997Sken struct devstat *disk_stats; 176229997Sken ctl_be_block_lun_flags flags; 177229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 178229997Sken struct ctl_be_lun ctl_be_lun; 179229997Sken struct taskqueue *io_taskqueue; 180229997Sken struct task io_task; 181229997Sken int num_threads; 182229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 183229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 184229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 185267877Smav struct mtx_padalign io_lock; 186267877Smav struct mtx_padalign queue_lock; 187229997Sken}; 188229997Sken 189229997Sken/* 190229997Sken * Overall softc structure for the block backend module. 191229997Sken */ 192229997Skenstruct ctl_be_block_softc { 193229997Sken struct mtx lock; 194229997Sken int num_disks; 195229997Sken STAILQ_HEAD(, ctl_block_disk) disk_list; 196229997Sken int num_luns; 197229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 198229997Sken}; 199229997Sken 200229997Skenstatic struct ctl_be_block_softc backend_block_softc; 201229997Sken 202229997Sken/* 203229997Sken * Per-I/O information. 204229997Sken */ 205229997Skenstruct ctl_be_block_io { 206229997Sken union ctl_io *io; 207229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 208229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 209229997Sken int bio_cmd; 210229997Sken int num_segs; 211229997Sken int num_bios_sent; 212229997Sken int num_bios_done; 213229997Sken int send_complete; 214229997Sken int num_errors; 215229997Sken struct bintime ds_t0; 216229997Sken devstat_tag_type ds_tag_type; 217229997Sken devstat_trans_flags ds_trans_type; 218229997Sken uint64_t io_len; 219229997Sken uint64_t io_offset; 220229997Sken struct ctl_be_block_softc *softc; 221229997Sken struct ctl_be_block_lun *lun; 222264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 223229997Sken}; 224229997Sken 225229997Skenstatic int cbb_num_threads = 14; 226229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 227229997Sken "CAM Target Layer Block Backend"); 228267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 229229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 230229997Sken 231229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 232229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 233229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 234229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 235229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 236229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 237229997Sken struct ctl_be_block_io *beio); 238229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 239229997Sken struct ctl_be_block_io *beio); 240229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 241229997Sken struct ctl_be_block_io *beio); 242264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 243264274Smav struct ctl_be_block_io *beio); 244229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 245229997Sken struct ctl_be_block_io *beio); 246274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 247274154Smav const char *attrname); 248229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 249229997Sken union ctl_io *io); 250229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 251229997Sken union ctl_io *io); 252229997Skenstatic void ctl_be_block_worker(void *context, int pending); 253229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 254229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 255229997Sken int flag, struct thread *td); 256229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 257229997Sken struct ctl_lun_req *req); 258229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 259229997Sken struct ctl_lun_req *req); 260229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 261229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 262229997Sken struct ctl_be_block_lun *be_lun, 263229997Sken struct ctl_lun_req *req); 264229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 265229997Sken struct ctl_lun_req *req); 266229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 267229997Sken struct ctl_lun_req *req); 268232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 269232604Strasz struct ctl_lun_req *req); 270232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 271232604Strasz struct ctl_lun_req *req); 272232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 273232604Strasz struct ctl_lun_req *req); 274229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 275229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 276229997Sken ctl_lun_config_status status); 277229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 278229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 279229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 280274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 281229997Skenint ctl_be_block_init(void); 282229997Sken 283229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 284229997Sken{ 285230334Sken .name = "block", 286230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 287230334Sken .init = ctl_be_block_init, 288230334Sken .data_submit = ctl_be_block_submit, 289230334Sken .data_move_done = ctl_be_block_move_done, 290230334Sken .config_read = ctl_be_block_config_read, 291230334Sken .config_write = ctl_be_block_config_write, 292230334Sken .ioctl = ctl_be_block_ioctl, 293274154Smav .lun_info = ctl_be_block_lun_info, 294274154Smav .lun_attr = ctl_be_block_lun_attr 295229997Sken}; 296229997Sken 297229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 298229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 299229997Sken 300264020Straszstatic uma_zone_t beio_zone; 301264020Strasz 302229997Skenstatic struct ctl_be_block_io * 303229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 304229997Sken{ 305229997Sken struct ctl_be_block_io *beio; 306229997Sken 307264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 308264020Strasz beio->softc = softc; 309229997Sken return (beio); 310229997Sken} 311229997Sken 312229997Skenstatic void 313229997Skenctl_free_beio(struct ctl_be_block_io *beio) 314229997Sken{ 315229997Sken int duplicate_free; 316229997Sken int i; 317229997Sken 318229997Sken duplicate_free = 0; 319229997Sken 320229997Sken for (i = 0; i < beio->num_segs; i++) { 321229997Sken if (beio->sg_segs[i].addr == NULL) 322229997Sken duplicate_free++; 323229997Sken 324229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 325229997Sken beio->sg_segs[i].addr = NULL; 326267537Smav 327267537Smav /* For compare we had two equal S/G lists. */ 328267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 329267537Smav uma_zfree(beio->lun->lun_zone, 330267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 331267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 332267537Smav } 333229997Sken } 334229997Sken 335229997Sken if (duplicate_free > 0) { 336229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 337229997Sken duplicate_free, beio->num_segs); 338229997Sken } 339229997Sken 340264020Strasz uma_zfree(beio_zone, beio); 341229997Sken} 342229997Sken 343229997Skenstatic void 344229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 345229997Sken{ 346267877Smav union ctl_io *io = beio->io; 347229997Sken 348264274Smav if (beio->beio_cont != NULL) { 349264274Smav beio->beio_cont(beio); 350264274Smav } else { 351264274Smav ctl_free_beio(beio); 352267537Smav ctl_data_submit_done(io); 353264274Smav } 354229997Sken} 355229997Sken 356229997Skenstatic int 357229997Skenctl_be_block_move_done(union ctl_io *io) 358229997Sken{ 359229997Sken struct ctl_be_block_io *beio; 360229997Sken struct ctl_be_block_lun *be_lun; 361267537Smav struct ctl_lba_len_flags *lbalen; 362229997Sken#ifdef CTL_TIME_IO 363229997Sken struct bintime cur_bt; 364267537Smav#endif 365267537Smav int i; 366229997Sken 367267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 368229997Sken be_lun = beio->lun; 369229997Sken 370229997Sken DPRINTF("entered\n"); 371229997Sken 372229997Sken#ifdef CTL_TIME_IO 373229997Sken getbintime(&cur_bt); 374229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 375229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 376229997Sken io->io_hdr.num_dmas++; 377229997Sken#endif 378267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 379229997Sken 380229997Sken /* 381229997Sken * We set status at this point for read commands, and write 382229997Sken * commands with errors. 383229997Sken */ 384267537Smav if ((io->io_hdr.port_status == 0) && 385267537Smav ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) && 386267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 387267537Smav lbalen = ARGS(beio->io); 388267537Smav if (lbalen->flags & CTL_LLF_READ) { 389267537Smav ctl_set_success(&io->scsiio); 390267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 391267537Smav /* We have two data blocks ready for comparison. */ 392267537Smav for (i = 0; i < beio->num_segs; i++) { 393267537Smav if (memcmp(beio->sg_segs[i].addr, 394267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 395267537Smav beio->sg_segs[i].len) != 0) 396267537Smav break; 397267537Smav } 398267537Smav if (i < beio->num_segs) 399267537Smav ctl_set_sense(&io->scsiio, 400267537Smav /*current_error*/ 1, 401267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 402267537Smav /*asc*/ 0x1D, 403267537Smav /*ascq*/ 0x00, 404267537Smav SSD_ELEM_NONE); 405267537Smav else 406267537Smav ctl_set_success(&io->scsiio); 407267537Smav } 408267537Smav } 409229997Sken else if ((io->io_hdr.port_status != 0) 410229997Sken && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 411229997Sken && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 412229997Sken /* 413229997Sken * For hardware error sense keys, the sense key 414229997Sken * specific value is defined to be a retry count, 415229997Sken * but we use it to pass back an internal FETD 416229997Sken * error code. XXX KDM Hopefully the FETD is only 417229997Sken * using 16 bits for an error code, since that's 418229997Sken * all the space we have in the sks field. 419229997Sken */ 420229997Sken ctl_set_internal_failure(&io->scsiio, 421229997Sken /*sks_valid*/ 1, 422229997Sken /*retry_count*/ 423229997Sken io->io_hdr.port_status); 424229997Sken } 425229997Sken 426229997Sken /* 427229997Sken * If this is a read, or a write with errors, it is done. 428229997Sken */ 429229997Sken if ((beio->bio_cmd == BIO_READ) 430229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 431229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 432229997Sken ctl_complete_beio(beio); 433229997Sken return (0); 434229997Sken } 435229997Sken 436229997Sken /* 437229997Sken * At this point, we have a write and the DMA completed 438229997Sken * successfully. We now have to queue it to the task queue to 439229997Sken * execute the backend I/O. That is because we do blocking 440229997Sken * memory allocations, and in the file backing case, blocking I/O. 441229997Sken * This move done routine is generally called in the SIM's 442229997Sken * interrupt context, and therefore we cannot block. 443229997Sken */ 444267877Smav mtx_lock(&be_lun->queue_lock); 445229997Sken /* 446229997Sken * XXX KDM make sure that links is okay to use at this point. 447229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 448229997Sken * or deal with resource allocation here. 449229997Sken */ 450229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 451267877Smav mtx_unlock(&be_lun->queue_lock); 452229997Sken 453229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 454229997Sken 455229997Sken return (0); 456229997Sken} 457229997Sken 458229997Skenstatic void 459229997Skenctl_be_block_biodone(struct bio *bio) 460229997Sken{ 461229997Sken struct ctl_be_block_io *beio; 462229997Sken struct ctl_be_block_lun *be_lun; 463229997Sken union ctl_io *io; 464261538Smav int error; 465229997Sken 466229997Sken beio = bio->bio_caller1; 467229997Sken be_lun = beio->lun; 468229997Sken io = beio->io; 469229997Sken 470229997Sken DPRINTF("entered\n"); 471229997Sken 472261538Smav error = bio->bio_error; 473267877Smav mtx_lock(&be_lun->io_lock); 474261538Smav if (error != 0) 475229997Sken beio->num_errors++; 476229997Sken 477229997Sken beio->num_bios_done++; 478229997Sken 479229997Sken /* 480229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 481229997Sken * during the free might cause it to complain. 482229997Sken */ 483229997Sken g_destroy_bio(bio); 484229997Sken 485229997Sken /* 486229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 487229997Sken * complete, then we're done. 488229997Sken */ 489229997Sken if ((beio->send_complete == 0) 490229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 491267877Smav mtx_unlock(&be_lun->io_lock); 492229997Sken return; 493229997Sken } 494229997Sken 495229997Sken /* 496229997Sken * At this point, we've verified that we are the last I/O to 497229997Sken * complete, so it's safe to drop the lock. 498229997Sken */ 499267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 500267877Smav beio->ds_tag_type, beio->ds_trans_type, 501267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 502267877Smav mtx_unlock(&be_lun->io_lock); 503229997Sken 504229997Sken /* 505229997Sken * If there are any errors from the backing device, we fail the 506229997Sken * entire I/O with a medium error. 507229997Sken */ 508229997Sken if (beio->num_errors > 0) { 509261538Smav if (error == EOPNOTSUPP) { 510261538Smav ctl_set_invalid_opcode(&io->scsiio); 511273809Smav } else if (error == ENOSPC) { 512273809Smav ctl_set_space_alloc_fail(&io->scsiio); 513261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 514229997Sken /* XXX KDM is there is a better error here? */ 515229997Sken ctl_set_internal_failure(&io->scsiio, 516229997Sken /*sks_valid*/ 1, 517229997Sken /*retry_count*/ 0xbad2); 518229997Sken } else 519229997Sken ctl_set_medium_error(&io->scsiio); 520229997Sken ctl_complete_beio(beio); 521229997Sken return; 522229997Sken } 523229997Sken 524229997Sken /* 525267537Smav * If this is a write, a flush, a delete or verify, we're all done. 526229997Sken * If this is a read, we can now send the data to the user. 527229997Sken */ 528229997Sken if ((beio->bio_cmd == BIO_WRITE) 529264274Smav || (beio->bio_cmd == BIO_FLUSH) 530267537Smav || (beio->bio_cmd == BIO_DELETE) 531267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 532229997Sken ctl_set_success(&io->scsiio); 533229997Sken ctl_complete_beio(beio); 534229997Sken } else { 535229997Sken#ifdef CTL_TIME_IO 536229997Sken getbintime(&io->io_hdr.dma_start_bt); 537229997Sken#endif 538229997Sken ctl_datamove(io); 539229997Sken } 540229997Sken} 541229997Sken 542229997Skenstatic void 543229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 544229997Sken struct ctl_be_block_io *beio) 545229997Sken{ 546267877Smav union ctl_io *io = beio->io; 547229997Sken struct mount *mountpoint; 548241896Skib int error, lock_flags; 549229997Sken 550229997Sken DPRINTF("entered\n"); 551229997Sken 552267877Smav binuptime(&beio->ds_t0); 553267877Smav mtx_lock(&be_lun->io_lock); 554267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 555267877Smav mtx_unlock(&be_lun->io_lock); 556229997Sken 557267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 558229997Sken 559229997Sken if (MNT_SHARED_WRITES(mountpoint) 560229997Sken || ((mountpoint == NULL) 561229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 562229997Sken lock_flags = LK_SHARED; 563229997Sken else 564229997Sken lock_flags = LK_EXCLUSIVE; 565229997Sken 566229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 567229997Sken 568229997Sken error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 569229997Sken VOP_UNLOCK(be_lun->vn, 0); 570229997Sken 571229997Sken vn_finished_write(mountpoint); 572229997Sken 573267877Smav mtx_lock(&be_lun->io_lock); 574267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 575267877Smav beio->ds_tag_type, beio->ds_trans_type, 576267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 577267877Smav mtx_unlock(&be_lun->io_lock); 578267877Smav 579229997Sken if (error == 0) 580229997Sken ctl_set_success(&io->scsiio); 581229997Sken else { 582229997Sken /* XXX KDM is there is a better error here? */ 583229997Sken ctl_set_internal_failure(&io->scsiio, 584229997Sken /*sks_valid*/ 1, 585229997Sken /*retry_count*/ 0xbad1); 586229997Sken } 587229997Sken 588229997Sken ctl_complete_beio(beio); 589229997Sken} 590229997Sken 591258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 592258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 593258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 594258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 595229997Sken 596229997Skenstatic void 597229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 598229997Sken struct ctl_be_block_io *beio) 599229997Sken{ 600229997Sken struct ctl_be_block_filedata *file_data; 601229997Sken union ctl_io *io; 602229997Sken struct uio xuio; 603229997Sken struct iovec *xiovec; 604241896Skib int flags; 605229997Sken int error, i; 606229997Sken 607229997Sken DPRINTF("entered\n"); 608229997Sken 609229997Sken file_data = &be_lun->backend.file; 610229997Sken io = beio->io; 611271309Smav flags = 0; 612271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 613271309Smav flags |= IO_DIRECT; 614271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 615271309Smav flags |= IO_SYNC; 616229997Sken 617267537Smav bzero(&xuio, sizeof(xuio)); 618229997Sken if (beio->bio_cmd == BIO_READ) { 619229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 620267537Smav xuio.uio_rw = UIO_READ; 621229997Sken } else { 622229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 623267537Smav xuio.uio_rw = UIO_WRITE; 624229997Sken } 625229997Sken xuio.uio_offset = beio->io_offset; 626229997Sken xuio.uio_resid = beio->io_len; 627229997Sken xuio.uio_segflg = UIO_SYSSPACE; 628229997Sken xuio.uio_iov = beio->xiovecs; 629229997Sken xuio.uio_iovcnt = beio->num_segs; 630229997Sken xuio.uio_td = curthread; 631229997Sken 632229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 633229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 634229997Sken xiovec->iov_len = beio->sg_segs[i].len; 635229997Sken } 636229997Sken 637267877Smav binuptime(&beio->ds_t0); 638267877Smav mtx_lock(&be_lun->io_lock); 639267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 640267877Smav mtx_unlock(&be_lun->io_lock); 641267877Smav 642229997Sken if (beio->bio_cmd == BIO_READ) { 643229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 644229997Sken 645229997Sken /* 646229997Sken * UFS pays attention to IO_DIRECT for reads. If the 647229997Sken * DIRECTIO option is configured into the kernel, it calls 648229997Sken * ffs_rawread(). But that only works for single-segment 649229997Sken * uios with user space addresses. In our case, with a 650229997Sken * kernel uio, it still reads into the buffer cache, but it 651229997Sken * will just try to release the buffer from the cache later 652229997Sken * on in ffs_read(). 653229997Sken * 654229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 655229997Sken * 656229997Sken * UFS does not pay attention to IO_SYNC for reads. 657229997Sken * 658229997Sken * ZFS pays attention to IO_SYNC (which translates into the 659229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 660229997Sken * attempts to sync the file before reading. 661229997Sken * 662229997Sken * So, to attempt to provide some barrier semantics in the 663229997Sken * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 664229997Sken */ 665271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 666229997Sken 667229997Sken VOP_UNLOCK(be_lun->vn, 0); 668267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 669229997Sken } else { 670229997Sken struct mount *mountpoint; 671229997Sken int lock_flags; 672229997Sken 673229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 674229997Sken 675229997Sken if (MNT_SHARED_WRITES(mountpoint) 676229997Sken || ((mountpoint == NULL) 677229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 678229997Sken lock_flags = LK_SHARED; 679229997Sken else 680229997Sken lock_flags = LK_EXCLUSIVE; 681229997Sken 682229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 683229997Sken 684229997Sken /* 685229997Sken * UFS pays attention to IO_DIRECT for writes. The write 686229997Sken * is done asynchronously. (Normally the write would just 687229997Sken * get put into cache. 688229997Sken * 689229997Sken * UFS pays attention to IO_SYNC for writes. It will 690229997Sken * attempt to write the buffer out synchronously if that 691229997Sken * flag is set. 692229997Sken * 693229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 694229997Sken * 695229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 696229997Sken * for writes. It will flush the transaction from the 697229997Sken * cache before returning. 698229997Sken * 699229997Sken * So if we've got the BIO_ORDERED flag set, we want 700229997Sken * IO_SYNC in either the UFS or ZFS case. 701229997Sken */ 702271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 703229997Sken VOP_UNLOCK(be_lun->vn, 0); 704229997Sken 705229997Sken vn_finished_write(mountpoint); 706267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 707229997Sken } 708229997Sken 709267877Smav mtx_lock(&be_lun->io_lock); 710267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 711267877Smav beio->ds_tag_type, beio->ds_trans_type, 712267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 713267877Smav mtx_unlock(&be_lun->io_lock); 714267877Smav 715229997Sken /* 716229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 717229997Sken * return the I/O to the user. 718229997Sken */ 719229997Sken if (error != 0) { 720229997Sken char path_str[32]; 721229997Sken 722229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 723229997Sken printf("%s%s command returned errno %d\n", path_str, 724229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 725273809Smav if (error == ENOSPC) { 726273809Smav ctl_set_space_alloc_fail(&io->scsiio); 727273809Smav } else 728273809Smav ctl_set_medium_error(&io->scsiio); 729229997Sken ctl_complete_beio(beio); 730229997Sken return; 731229997Sken } 732229997Sken 733229997Sken /* 734269122Smav * If this is a write or a verify, we're all done. 735229997Sken * If this is a read, we can now send the data to the user. 736229997Sken */ 737269122Smav if ((beio->bio_cmd == BIO_WRITE) || 738269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 739229997Sken ctl_set_success(&io->scsiio); 740229997Sken ctl_complete_beio(beio); 741229997Sken } else { 742229997Sken#ifdef CTL_TIME_IO 743229997Sken getbintime(&io->io_hdr.dma_start_bt); 744229997Sken#endif 745229997Sken ctl_datamove(io); 746229997Sken } 747229997Sken} 748229997Sken 749229997Skenstatic void 750269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 751269123Smav struct ctl_be_block_io *beio) 752269123Smav{ 753269123Smav struct ctl_be_block_devdata *dev_data; 754269123Smav union ctl_io *io; 755269123Smav struct uio xuio; 756269123Smav struct iovec *xiovec; 757269123Smav int flags; 758269123Smav int error, i; 759269123Smav 760269123Smav DPRINTF("entered\n"); 761269123Smav 762269123Smav dev_data = &be_lun->backend.dev; 763269123Smav io = beio->io; 764271309Smav flags = 0; 765271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 766271309Smav flags |= IO_DIRECT; 767271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 768271309Smav flags |= IO_SYNC; 769269123Smav 770269123Smav bzero(&xuio, sizeof(xuio)); 771269123Smav if (beio->bio_cmd == BIO_READ) { 772269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 773269123Smav xuio.uio_rw = UIO_READ; 774269123Smav } else { 775269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 776269123Smav xuio.uio_rw = UIO_WRITE; 777269123Smav } 778269123Smav xuio.uio_offset = beio->io_offset; 779269123Smav xuio.uio_resid = beio->io_len; 780269123Smav xuio.uio_segflg = UIO_SYSSPACE; 781269123Smav xuio.uio_iov = beio->xiovecs; 782269123Smav xuio.uio_iovcnt = beio->num_segs; 783269123Smav xuio.uio_td = curthread; 784269123Smav 785269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 786269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 787269123Smav xiovec->iov_len = beio->sg_segs[i].len; 788269123Smav } 789269123Smav 790269123Smav binuptime(&beio->ds_t0); 791269123Smav mtx_lock(&be_lun->io_lock); 792269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 793269123Smav mtx_unlock(&be_lun->io_lock); 794269123Smav 795269123Smav if (beio->bio_cmd == BIO_READ) { 796271309Smav error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 797269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 798269123Smav } else { 799271309Smav error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 800269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 801269123Smav } 802269123Smav 803269123Smav mtx_lock(&be_lun->io_lock); 804269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 805269123Smav beio->ds_tag_type, beio->ds_trans_type, 806269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 807269123Smav mtx_unlock(&be_lun->io_lock); 808269123Smav 809269123Smav /* 810269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 811269123Smav * return the I/O to the user. 812269123Smav */ 813269123Smav if (error != 0) { 814273809Smav if (error == ENOSPC) { 815273809Smav ctl_set_space_alloc_fail(&io->scsiio); 816273809Smav } else 817273809Smav ctl_set_medium_error(&io->scsiio); 818269123Smav ctl_complete_beio(beio); 819269123Smav return; 820269123Smav } 821269123Smav 822269123Smav /* 823269123Smav * If this is a write or a verify, we're all done. 824269123Smav * If this is a read, we can now send the data to the user. 825269123Smav */ 826269123Smav if ((beio->bio_cmd == BIO_WRITE) || 827269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 828269123Smav ctl_set_success(&io->scsiio); 829269123Smav ctl_complete_beio(beio); 830269123Smav } else { 831269123Smav#ifdef CTL_TIME_IO 832269123Smav getbintime(&io->io_hdr.dma_start_bt); 833269123Smav#endif 834269123Smav ctl_datamove(io); 835269123Smav } 836269123Smav} 837269123Smav 838269123Smavstatic void 839229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 840229997Sken struct ctl_be_block_io *beio) 841229997Sken{ 842229997Sken struct bio *bio; 843229997Sken union ctl_io *io; 844229997Sken struct ctl_be_block_devdata *dev_data; 845229997Sken 846229997Sken dev_data = &be_lun->backend.dev; 847229997Sken io = beio->io; 848229997Sken 849229997Sken DPRINTF("entered\n"); 850229997Sken 851229997Sken /* This can't fail, it's a blocking allocation. */ 852229997Sken bio = g_alloc_bio(); 853229997Sken 854229997Sken bio->bio_cmd = BIO_FLUSH; 855229997Sken bio->bio_flags |= BIO_ORDERED; 856229997Sken bio->bio_dev = dev_data->cdev; 857229997Sken bio->bio_offset = 0; 858229997Sken bio->bio_data = 0; 859229997Sken bio->bio_done = ctl_be_block_biodone; 860229997Sken bio->bio_caller1 = beio; 861229997Sken bio->bio_pblkno = 0; 862229997Sken 863229997Sken /* 864229997Sken * We don't need to acquire the LUN lock here, because we are only 865229997Sken * sending one bio, and so there is no other context to synchronize 866229997Sken * with. 867229997Sken */ 868229997Sken beio->num_bios_sent = 1; 869229997Sken beio->send_complete = 1; 870229997Sken 871229997Sken binuptime(&beio->ds_t0); 872267877Smav mtx_lock(&be_lun->io_lock); 873229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 874267877Smav mtx_unlock(&be_lun->io_lock); 875229997Sken 876229997Sken (*dev_data->csw->d_strategy)(bio); 877229997Sken} 878229997Sken 879229997Skenstatic void 880264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 881264274Smav struct ctl_be_block_io *beio, 882264274Smav uint64_t off, uint64_t len, int last) 883264274Smav{ 884264274Smav struct bio *bio; 885264274Smav struct ctl_be_block_devdata *dev_data; 886264296Smav uint64_t maxlen; 887264274Smav 888264274Smav dev_data = &be_lun->backend.dev; 889264296Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 890264274Smav while (len > 0) { 891264274Smav bio = g_alloc_bio(); 892264274Smav bio->bio_cmd = BIO_DELETE; 893264274Smav bio->bio_dev = dev_data->cdev; 894264274Smav bio->bio_offset = off; 895264296Smav bio->bio_length = MIN(len, maxlen); 896264274Smav bio->bio_data = 0; 897264274Smav bio->bio_done = ctl_be_block_biodone; 898264274Smav bio->bio_caller1 = beio; 899264296Smav bio->bio_pblkno = off / be_lun->blocksize; 900264274Smav 901264274Smav off += bio->bio_length; 902264274Smav len -= bio->bio_length; 903264274Smav 904267877Smav mtx_lock(&be_lun->io_lock); 905264274Smav beio->num_bios_sent++; 906264274Smav if (last && len == 0) 907264274Smav beio->send_complete = 1; 908267877Smav mtx_unlock(&be_lun->io_lock); 909264274Smav 910264274Smav (*dev_data->csw->d_strategy)(bio); 911264274Smav } 912264274Smav} 913264274Smav 914264274Smavstatic void 915264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 916264274Smav struct ctl_be_block_io *beio) 917264274Smav{ 918264274Smav union ctl_io *io; 919264274Smav struct ctl_be_block_devdata *dev_data; 920267515Smav struct ctl_ptr_len_flags *ptrlen; 921264274Smav struct scsi_unmap_desc *buf, *end; 922264274Smav uint64_t len; 923264274Smav 924264274Smav dev_data = &be_lun->backend.dev; 925264274Smav io = beio->io; 926264274Smav 927264274Smav DPRINTF("entered\n"); 928264274Smav 929264274Smav binuptime(&beio->ds_t0); 930267877Smav mtx_lock(&be_lun->io_lock); 931264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 932267877Smav mtx_unlock(&be_lun->io_lock); 933264274Smav 934264274Smav if (beio->io_offset == -1) { 935264274Smav beio->io_len = 0; 936267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 937267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 938267515Smav end = buf + ptrlen->len / sizeof(*buf); 939264274Smav for (; buf < end; buf++) { 940264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 941264274Smav be_lun->blocksize; 942264274Smav beio->io_len += len; 943264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 944264274Smav scsi_8btou64(buf->lba) * be_lun->blocksize, len, 945264283Smav (end - buf < 2) ? TRUE : FALSE); 946264274Smav } 947264274Smav } else 948264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 949264274Smav beio->io_offset, beio->io_len, TRUE); 950264274Smav} 951264274Smav 952264274Smavstatic void 953229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 954229997Sken struct ctl_be_block_io *beio) 955229997Sken{ 956267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 957229997Sken int i; 958229997Sken struct bio *bio; 959229997Sken struct ctl_be_block_devdata *dev_data; 960229997Sken off_t cur_offset; 961229997Sken int max_iosize; 962229997Sken 963229997Sken DPRINTF("entered\n"); 964229997Sken 965229997Sken dev_data = &be_lun->backend.dev; 966229997Sken 967229997Sken /* 968229997Sken * We have to limit our I/O size to the maximum supported by the 969229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 970229997Sken * set it properly, use DFLTPHYS. 971229997Sken */ 972229997Sken max_iosize = dev_data->cdev->si_iosize_max; 973229997Sken if (max_iosize < PAGE_SIZE) 974229997Sken max_iosize = DFLTPHYS; 975229997Sken 976229997Sken cur_offset = beio->io_offset; 977229997Sken for (i = 0; i < beio->num_segs; i++) { 978229997Sken size_t cur_size; 979229997Sken uint8_t *cur_ptr; 980229997Sken 981229997Sken cur_size = beio->sg_segs[i].len; 982229997Sken cur_ptr = beio->sg_segs[i].addr; 983229997Sken 984229997Sken while (cur_size > 0) { 985229997Sken /* This can't fail, it's a blocking allocation. */ 986229997Sken bio = g_alloc_bio(); 987229997Sken 988229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 989229997Sken 990229997Sken bio->bio_cmd = beio->bio_cmd; 991229997Sken bio->bio_dev = dev_data->cdev; 992229997Sken bio->bio_caller1 = beio; 993229997Sken bio->bio_length = min(cur_size, max_iosize); 994229997Sken bio->bio_offset = cur_offset; 995229997Sken bio->bio_data = cur_ptr; 996229997Sken bio->bio_done = ctl_be_block_biodone; 997229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 998229997Sken 999229997Sken cur_offset += bio->bio_length; 1000229997Sken cur_ptr += bio->bio_length; 1001229997Sken cur_size -= bio->bio_length; 1002229997Sken 1003267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1004229997Sken beio->num_bios_sent++; 1005229997Sken } 1006229997Sken } 1007267877Smav binuptime(&beio->ds_t0); 1008267877Smav mtx_lock(&be_lun->io_lock); 1009267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1010267877Smav beio->send_complete = 1; 1011267877Smav mtx_unlock(&be_lun->io_lock); 1012267877Smav 1013267877Smav /* 1014267877Smav * Fire off all allocated requests! 1015267877Smav */ 1016267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1017267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1018267877Smav (*dev_data->csw->d_strategy)(bio); 1019267877Smav } 1020229997Sken} 1021229997Sken 1022274154Smavstatic uint64_t 1023274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1024274154Smav{ 1025274154Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1026274154Smav struct diocgattr_arg arg; 1027274154Smav int error; 1028274154Smav 1029274154Smav if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1030274154Smav return (UINT64_MAX); 1031274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1032274154Smav arg.len = sizeof(arg.value.off); 1033274154Smav error = dev_data->csw->d_ioctl(dev_data->cdev, 1034274154Smav DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1035274154Smav if (error != 0) 1036274154Smav return (UINT64_MAX); 1037274154Smav return (arg.value.off); 1038274154Smav} 1039274154Smav 1040229997Skenstatic void 1041264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1042264274Smav{ 1043264274Smav union ctl_io *io; 1044264274Smav 1045264274Smav io = beio->io; 1046264274Smav ctl_free_beio(beio); 1047267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1048267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1049267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1050264274Smav ctl_config_write_done(io); 1051264274Smav return; 1052264274Smav } 1053264274Smav 1054264274Smav ctl_be_block_config_write(io); 1055264274Smav} 1056264274Smav 1057264274Smavstatic void 1058264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1059264274Smav union ctl_io *io) 1060264274Smav{ 1061264274Smav struct ctl_be_block_io *beio; 1062264274Smav struct ctl_be_block_softc *softc; 1063267515Smav struct ctl_lba_len_flags *lbalen; 1064264274Smav uint64_t len_left, lba; 1065264274Smav int i, seglen; 1066264274Smav uint8_t *buf, *end; 1067264274Smav 1068264274Smav DPRINTF("entered\n"); 1069264274Smav 1070267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1071264274Smav softc = be_lun->softc; 1072267537Smav lbalen = ARGS(beio->io); 1073264274Smav 1074271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1075269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1076264274Smav ctl_free_beio(beio); 1077264274Smav ctl_set_invalid_field(&io->scsiio, 1078264274Smav /*sks_valid*/ 1, 1079264274Smav /*command*/ 1, 1080264274Smav /*field*/ 1, 1081264274Smav /*bit_valid*/ 0, 1082264274Smav /*bit*/ 0); 1083264274Smav ctl_config_write_done(io); 1084264274Smav return; 1085264274Smav } 1086264274Smav 1087264274Smav switch (io->scsiio.tag_type) { 1088264274Smav case CTL_TAG_ORDERED: 1089264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1090264274Smav break; 1091264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1092264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1093264274Smav break; 1094264274Smav case CTL_TAG_UNTAGGED: 1095264274Smav case CTL_TAG_SIMPLE: 1096264274Smav case CTL_TAG_ACA: 1097264274Smav default: 1098264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1099264274Smav break; 1100264274Smav } 1101264274Smav 1102269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1103267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1104267515Smav beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1105264274Smav beio->bio_cmd = BIO_DELETE; 1106264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1107264274Smav 1108264274Smav be_lun->unmap(be_lun, beio); 1109264274Smav return; 1110264274Smav } 1111264274Smav 1112264274Smav beio->bio_cmd = BIO_WRITE; 1113264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1114264274Smav 1115264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1116267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1117264274Smav 1118267515Smav len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1119264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1120264274Smav 1121264274Smav /* 1122264274Smav * Setup the S/G entry for this chunk. 1123264274Smav */ 1124264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1125264274Smav seglen -= seglen % be_lun->blocksize; 1126264274Smav beio->sg_segs[i].len = seglen; 1127264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1128264274Smav 1129264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1130264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1131264274Smav 1132264274Smav beio->num_segs++; 1133264274Smav len_left -= seglen; 1134264274Smav 1135264274Smav buf = beio->sg_segs[i].addr; 1136264274Smav end = buf + seglen; 1137264274Smav for (; buf < end; buf += be_lun->blocksize) { 1138264274Smav memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1139267515Smav if (lbalen->flags & SWS_LBDATA) 1140267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1141264274Smav lba++; 1142264274Smav } 1143264274Smav } 1144264274Smav 1145267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1146264274Smav beio->io_len = lba * be_lun->blocksize; 1147264274Smav 1148264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1149264274Smav if (len_left > 0) { 1150267515Smav lbalen->lba += lba; 1151267515Smav lbalen->len -= lba; 1152264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1153264274Smav } 1154264274Smav 1155264274Smav be_lun->dispatch(be_lun, beio); 1156264274Smav} 1157264274Smav 1158264274Smavstatic void 1159264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1160264274Smav union ctl_io *io) 1161264274Smav{ 1162264274Smav struct ctl_be_block_io *beio; 1163264274Smav struct ctl_be_block_softc *softc; 1164267515Smav struct ctl_ptr_len_flags *ptrlen; 1165264274Smav 1166264274Smav DPRINTF("entered\n"); 1167264274Smav 1168267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1169264274Smav softc = be_lun->softc; 1170267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1171264274Smav 1172269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1173264274Smav ctl_free_beio(beio); 1174264274Smav ctl_set_invalid_field(&io->scsiio, 1175264274Smav /*sks_valid*/ 0, 1176264274Smav /*command*/ 1, 1177264274Smav /*field*/ 0, 1178264274Smav /*bit_valid*/ 0, 1179264274Smav /*bit*/ 0); 1180264274Smav ctl_config_write_done(io); 1181264274Smav return; 1182264274Smav } 1183264274Smav 1184264274Smav switch (io->scsiio.tag_type) { 1185264274Smav case CTL_TAG_ORDERED: 1186264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1187264274Smav break; 1188264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1189264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1190264274Smav break; 1191264274Smav case CTL_TAG_UNTAGGED: 1192264274Smav case CTL_TAG_SIMPLE: 1193264274Smav case CTL_TAG_ACA: 1194264274Smav default: 1195264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1196264274Smav break; 1197264274Smav } 1198264274Smav 1199264274Smav beio->io_len = 0; 1200264274Smav beio->io_offset = -1; 1201264274Smav 1202264274Smav beio->bio_cmd = BIO_DELETE; 1203264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1204264274Smav 1205267515Smav DPRINTF("UNMAP\n"); 1206264274Smav 1207264274Smav be_lun->unmap(be_lun, beio); 1208264274Smav} 1209264274Smav 1210264274Smavstatic void 1211264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1212264274Smav{ 1213264274Smav union ctl_io *io; 1214264274Smav 1215264274Smav io = beio->io; 1216264274Smav ctl_free_beio(beio); 1217264274Smav ctl_config_write_done(io); 1218264274Smav} 1219264274Smav 1220264274Smavstatic void 1221229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1222229997Sken union ctl_io *io) 1223229997Sken{ 1224229997Sken struct ctl_be_block_io *beio; 1225229997Sken struct ctl_be_block_softc *softc; 1226229997Sken 1227229997Sken DPRINTF("entered\n"); 1228229997Sken 1229229997Sken softc = be_lun->softc; 1230229997Sken beio = ctl_alloc_beio(softc); 1231229997Sken beio->io = io; 1232229997Sken beio->lun = be_lun; 1233264274Smav beio->beio_cont = ctl_be_block_cw_done; 1234267519Smav PRIV(io)->ptr = (void *)beio; 1235229997Sken 1236229997Sken switch (io->scsiio.cdb[0]) { 1237229997Sken case SYNCHRONIZE_CACHE: 1238229997Sken case SYNCHRONIZE_CACHE_16: 1239249194Strasz beio->bio_cmd = BIO_FLUSH; 1240229997Sken beio->ds_trans_type = DEVSTAT_NO_DATA; 1241229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1242229997Sken beio->io_len = 0; 1243229997Sken be_lun->lun_flush(be_lun, beio); 1244229997Sken break; 1245264274Smav case WRITE_SAME_10: 1246264274Smav case WRITE_SAME_16: 1247264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1248264274Smav break; 1249264274Smav case UNMAP: 1250264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1251264274Smav break; 1252229997Sken default: 1253229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1254229997Sken break; 1255229997Sken } 1256229997Sken} 1257229997Sken 1258258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1259258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1260258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1261258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1262229997Sken 1263229997Skenstatic void 1264264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1265264886Smav{ 1266264886Smav struct ctl_be_block_lun *be_lun; 1267264886Smav union ctl_io *io; 1268264886Smav 1269264886Smav io = beio->io; 1270264886Smav be_lun = beio->lun; 1271264886Smav ctl_free_beio(beio); 1272267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1273267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1274267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1275267537Smav ctl_data_submit_done(io); 1276264886Smav return; 1277264886Smav } 1278264886Smav 1279264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1280264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1281264886Smav 1282267877Smav mtx_lock(&be_lun->queue_lock); 1283264886Smav /* 1284264886Smav * XXX KDM make sure that links is okay to use at this point. 1285264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1286264886Smav * or deal with resource allocation here. 1287264886Smav */ 1288264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1289267877Smav mtx_unlock(&be_lun->queue_lock); 1290264886Smav 1291264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1292264886Smav} 1293264886Smav 1294264886Smavstatic void 1295229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1296229997Sken union ctl_io *io) 1297229997Sken{ 1298229997Sken struct ctl_be_block_io *beio; 1299229997Sken struct ctl_be_block_softc *softc; 1300267537Smav struct ctl_lba_len_flags *lbalen; 1301267519Smav struct ctl_ptr_len_flags *bptrlen; 1302267519Smav uint64_t len_left, lbas; 1303229997Sken int i; 1304229997Sken 1305229997Sken softc = be_lun->softc; 1306229997Sken 1307229997Sken DPRINTF("entered\n"); 1308229997Sken 1309267537Smav lbalen = ARGS(io); 1310267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1311267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1312267537Smav } else { 1313229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1314229997Sken } 1315229997Sken 1316229997Sken beio = ctl_alloc_beio(softc); 1317229997Sken beio->io = io; 1318229997Sken beio->lun = be_lun; 1319267519Smav bptrlen = PRIV(io); 1320267519Smav bptrlen->ptr = (void *)beio; 1321229997Sken 1322229997Sken switch (io->scsiio.tag_type) { 1323229997Sken case CTL_TAG_ORDERED: 1324229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1325229997Sken break; 1326229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1327229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1328229997Sken break; 1329229997Sken case CTL_TAG_UNTAGGED: 1330229997Sken case CTL_TAG_SIMPLE: 1331229997Sken case CTL_TAG_ACA: 1332229997Sken default: 1333229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1334229997Sken break; 1335229997Sken } 1336229997Sken 1337267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1338267537Smav beio->bio_cmd = BIO_WRITE; 1339267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1340267537Smav } else { 1341229997Sken beio->bio_cmd = BIO_READ; 1342229997Sken beio->ds_trans_type = DEVSTAT_READ; 1343229997Sken } 1344229997Sken 1345264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1346229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1347267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1348267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1349267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1350267537Smav else 1351267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1352267537Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1353267519Smav beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1354267519Smav beio->io_len = lbas * be_lun->blocksize; 1355267519Smav bptrlen->len += lbas; 1356229997Sken 1357264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1358264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1359264886Smav i, CTLBLK_MAX_SEGS)); 1360229997Sken 1361229997Sken /* 1362229997Sken * Setup the S/G entry for this chunk. 1363229997Sken */ 1364264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1365229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1366229997Sken 1367229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1368229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1369229997Sken 1370267537Smav /* Set up second segment for compare operation. */ 1371267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1372267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1373267537Smav beio->sg_segs[i].len; 1374267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1375267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1376267537Smav } 1377267537Smav 1378229997Sken beio->num_segs++; 1379229997Sken len_left -= beio->sg_segs[i].len; 1380229997Sken } 1381267519Smav if (bptrlen->len < lbalen->len) 1382264886Smav beio->beio_cont = ctl_be_block_next; 1383264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1384267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1385267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1386267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1387267537Smav else 1388267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1389264886Smav io->scsiio.kern_data_len = beio->io_len; 1390264886Smav io->scsiio.kern_data_resid = 0; 1391264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1392264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1393229997Sken 1394229997Sken /* 1395229997Sken * For the read case, we need to read the data into our buffers and 1396229997Sken * then we can send it back to the user. For the write case, we 1397229997Sken * need to get the data from the user first. 1398229997Sken */ 1399229997Sken if (beio->bio_cmd == BIO_READ) { 1400229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1401229997Sken be_lun->dispatch(be_lun, beio); 1402229997Sken } else { 1403229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1404229997Sken#ifdef CTL_TIME_IO 1405229997Sken getbintime(&io->io_hdr.dma_start_bt); 1406229997Sken#endif 1407229997Sken ctl_datamove(io); 1408229997Sken } 1409229997Sken} 1410229997Sken 1411229997Skenstatic void 1412229997Skenctl_be_block_worker(void *context, int pending) 1413229997Sken{ 1414229997Sken struct ctl_be_block_lun *be_lun; 1415229997Sken struct ctl_be_block_softc *softc; 1416229997Sken union ctl_io *io; 1417229997Sken 1418229997Sken be_lun = (struct ctl_be_block_lun *)context; 1419229997Sken softc = be_lun->softc; 1420229997Sken 1421229997Sken DPRINTF("entered\n"); 1422229997Sken 1423267877Smav mtx_lock(&be_lun->queue_lock); 1424229997Sken for (;;) { 1425229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1426229997Sken if (io != NULL) { 1427229997Sken struct ctl_be_block_io *beio; 1428229997Sken 1429229997Sken DPRINTF("datamove queue\n"); 1430229997Sken 1431229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1432229997Sken ctl_io_hdr, links); 1433229997Sken 1434267877Smav mtx_unlock(&be_lun->queue_lock); 1435229997Sken 1436267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1437229997Sken 1438229997Sken be_lun->dispatch(be_lun, beio); 1439229997Sken 1440267877Smav mtx_lock(&be_lun->queue_lock); 1441229997Sken continue; 1442229997Sken } 1443229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1444229997Sken if (io != NULL) { 1445229997Sken 1446229997Sken DPRINTF("config write queue\n"); 1447229997Sken 1448229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1449229997Sken ctl_io_hdr, links); 1450229997Sken 1451267877Smav mtx_unlock(&be_lun->queue_lock); 1452229997Sken 1453229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1454229997Sken 1455267877Smav mtx_lock(&be_lun->queue_lock); 1456229997Sken continue; 1457229997Sken } 1458229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1459229997Sken if (io != NULL) { 1460229997Sken DPRINTF("input queue\n"); 1461229997Sken 1462229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1463229997Sken ctl_io_hdr, links); 1464267877Smav mtx_unlock(&be_lun->queue_lock); 1465229997Sken 1466229997Sken /* 1467229997Sken * We must drop the lock, since this routine and 1468229997Sken * its children may sleep. 1469229997Sken */ 1470229997Sken ctl_be_block_dispatch(be_lun, io); 1471229997Sken 1472267877Smav mtx_lock(&be_lun->queue_lock); 1473229997Sken continue; 1474229997Sken } 1475229997Sken 1476229997Sken /* 1477229997Sken * If we get here, there is no work left in the queues, so 1478229997Sken * just break out and let the task queue go to sleep. 1479229997Sken */ 1480229997Sken break; 1481229997Sken } 1482267877Smav mtx_unlock(&be_lun->queue_lock); 1483229997Sken} 1484229997Sken 1485229997Sken/* 1486229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1487229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1488229997Sken * thread. 1489229997Sken */ 1490229997Skenstatic int 1491229997Skenctl_be_block_submit(union ctl_io *io) 1492229997Sken{ 1493229997Sken struct ctl_be_block_lun *be_lun; 1494229997Sken struct ctl_be_lun *ctl_be_lun; 1495229997Sken 1496229997Sken DPRINTF("entered\n"); 1497229997Sken 1498229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1499229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1500229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1501229997Sken 1502229997Sken /* 1503229997Sken * Make sure we only get SCSI I/O. 1504229997Sken */ 1505229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1506229997Sken "%#x) encountered", io->io_hdr.io_type)); 1507229997Sken 1508267519Smav PRIV(io)->len = 0; 1509267519Smav 1510267877Smav mtx_lock(&be_lun->queue_lock); 1511229997Sken /* 1512229997Sken * XXX KDM make sure that links is okay to use at this point. 1513229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1514229997Sken * or deal with resource allocation here. 1515229997Sken */ 1516229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1517267877Smav mtx_unlock(&be_lun->queue_lock); 1518229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1519229997Sken 1520267514Smav return (CTL_RETVAL_COMPLETE); 1521229997Sken} 1522229997Sken 1523229997Skenstatic int 1524229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1525229997Sken int flag, struct thread *td) 1526229997Sken{ 1527229997Sken struct ctl_be_block_softc *softc; 1528229997Sken int error; 1529229997Sken 1530229997Sken softc = &backend_block_softc; 1531229997Sken 1532229997Sken error = 0; 1533229997Sken 1534229997Sken switch (cmd) { 1535229997Sken case CTL_LUN_REQ: { 1536229997Sken struct ctl_lun_req *lun_req; 1537229997Sken 1538229997Sken lun_req = (struct ctl_lun_req *)addr; 1539229997Sken 1540229997Sken switch (lun_req->reqtype) { 1541229997Sken case CTL_LUNREQ_CREATE: 1542229997Sken error = ctl_be_block_create(softc, lun_req); 1543229997Sken break; 1544229997Sken case CTL_LUNREQ_RM: 1545229997Sken error = ctl_be_block_rm(softc, lun_req); 1546229997Sken break; 1547232604Strasz case CTL_LUNREQ_MODIFY: 1548232604Strasz error = ctl_be_block_modify(softc, lun_req); 1549232604Strasz break; 1550229997Sken default: 1551229997Sken lun_req->status = CTL_LUN_ERROR; 1552229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1553272911Smav "invalid LUN request type %d", 1554229997Sken lun_req->reqtype); 1555229997Sken break; 1556229997Sken } 1557229997Sken break; 1558229997Sken } 1559229997Sken default: 1560229997Sken error = ENOTTY; 1561229997Sken break; 1562229997Sken } 1563229997Sken 1564229997Sken return (error); 1565229997Sken} 1566229997Sken 1567229997Skenstatic int 1568229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1569229997Sken{ 1570229997Sken struct ctl_be_block_filedata *file_data; 1571229997Sken struct ctl_lun_create_params *params; 1572229997Sken struct vattr vattr; 1573273029Smav off_t pss; 1574229997Sken int error; 1575229997Sken 1576229997Sken error = 0; 1577229997Sken file_data = &be_lun->backend.file; 1578272911Smav params = &be_lun->params; 1579229997Sken 1580229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1581229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1582229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1583229997Sken 1584229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1585229997Sken if (error != 0) { 1586229997Sken snprintf(req->error_str, sizeof(req->error_str), 1587229997Sken "error calling VOP_GETATTR() for file %s", 1588229997Sken be_lun->dev_path); 1589229997Sken return (error); 1590229997Sken } 1591229997Sken 1592229997Sken /* 1593229997Sken * Verify that we have the ability to upgrade to exclusive 1594229997Sken * access on this file so we can trap errors at open instead 1595229997Sken * of reporting them during first access. 1596229997Sken */ 1597229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1598229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1599229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1600229997Sken error = EBADF; 1601229997Sken snprintf(req->error_str, sizeof(req->error_str), 1602229997Sken "error locking file %s", be_lun->dev_path); 1603229997Sken return (error); 1604229997Sken } 1605229997Sken } 1606229997Sken 1607229997Sken 1608229997Sken file_data->cred = crhold(curthread->td_ucred); 1609232604Strasz if (params->lun_size_bytes != 0) 1610232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1611232604Strasz else 1612232604Strasz be_lun->size_bytes = vattr.va_size; 1613229997Sken /* 1614229997Sken * We set the multi thread flag for file operations because all 1615229997Sken * filesystems (in theory) are capable of allowing multiple readers 1616229997Sken * of a file at once. So we want to get the maximum possible 1617229997Sken * concurrency. 1618229997Sken */ 1619229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1620229997Sken 1621229997Sken /* 1622273029Smav * For files we can use any logical block size. Prefer 512 bytes 1623273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1624273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1625273029Smav * logical block size -- report it as physical block size. 1626229997Sken */ 1627229997Sken if (params->blocksize_bytes != 0) 1628229997Sken be_lun->blocksize = params->blocksize_bytes; 1629229997Sken else 1630229997Sken be_lun->blocksize = 512; 1631273029Smav pss = vattr.va_blocksize / be_lun->blocksize; 1632273029Smav if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) && 1633273029Smav ((pss & (pss - 1)) == 0)) { 1634273029Smav be_lun->pblockexp = fls(pss) - 1; 1635273029Smav be_lun->pblockoff = 0; 1636273029Smav } 1637229997Sken 1638229997Sken /* 1639229997Sken * Sanity check. The media size has to be at least one 1640229997Sken * sector long. 1641229997Sken */ 1642229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1643229997Sken error = EINVAL; 1644229997Sken snprintf(req->error_str, sizeof(req->error_str), 1645229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1646229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1647229997Sken } 1648229997Sken return (error); 1649229997Sken} 1650229997Sken 1651229997Skenstatic int 1652229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1653229997Sken{ 1654229997Sken struct ctl_lun_create_params *params; 1655229997Sken struct vattr vattr; 1656229997Sken struct cdev *dev; 1657229997Sken struct cdevsw *devsw; 1658229997Sken int error; 1659264191Smav off_t ps, pss, po, pos; 1660229997Sken 1661272911Smav params = &be_lun->params; 1662229997Sken 1663229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1664229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1665229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1666229997Sken &be_lun->backend.dev.dev_ref); 1667229997Sken if (be_lun->backend.dev.csw == NULL) 1668229997Sken panic("Unable to retrieve device switch"); 1669269123Smav if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) 1670269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1671269123Smav else 1672269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1673269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1674269123Smav be_lun->unmap = ctl_be_block_unmap_dev; 1675274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1676229997Sken 1677229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1678229997Sken if (error) { 1679229997Sken snprintf(req->error_str, sizeof(req->error_str), 1680272911Smav "error getting vnode attributes for device %s", 1681272911Smav be_lun->dev_path); 1682229997Sken return (error); 1683229997Sken } 1684229997Sken 1685229997Sken dev = be_lun->vn->v_rdev; 1686229997Sken devsw = dev->si_devsw; 1687229997Sken if (!devsw->d_ioctl) { 1688229997Sken snprintf(req->error_str, sizeof(req->error_str), 1689272911Smav "no d_ioctl for device %s!", 1690229997Sken be_lun->dev_path); 1691229997Sken return (ENODEV); 1692229997Sken } 1693229997Sken 1694229997Sken error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1695229997Sken (caddr_t)&be_lun->blocksize, FREAD, 1696229997Sken curthread); 1697229997Sken if (error) { 1698229997Sken snprintf(req->error_str, sizeof(req->error_str), 1699272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1700272911Smav "on %s!", error, be_lun->dev_path); 1701229997Sken return (error); 1702229997Sken } 1703229997Sken 1704229997Sken /* 1705229997Sken * If the user has asked for a blocksize that is greater than the 1706229997Sken * backing device's blocksize, we can do it only if the blocksize 1707229997Sken * the user is asking for is an even multiple of the underlying 1708229997Sken * device's blocksize. 1709229997Sken */ 1710229997Sken if ((params->blocksize_bytes != 0) 1711229997Sken && (params->blocksize_bytes > be_lun->blocksize)) { 1712229997Sken uint32_t bs_multiple, tmp_blocksize; 1713229997Sken 1714229997Sken bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1715229997Sken 1716229997Sken tmp_blocksize = bs_multiple * be_lun->blocksize; 1717229997Sken 1718229997Sken if (tmp_blocksize == params->blocksize_bytes) { 1719229997Sken be_lun->blocksize = params->blocksize_bytes; 1720229997Sken } else { 1721229997Sken snprintf(req->error_str, sizeof(req->error_str), 1722272911Smav "requested blocksize %u is not an even " 1723229997Sken "multiple of backing device blocksize %u", 1724272911Smav params->blocksize_bytes, 1725229997Sken be_lun->blocksize); 1726229997Sken return (EINVAL); 1727229997Sken 1728229997Sken } 1729229997Sken } else if ((params->blocksize_bytes != 0) 1730229997Sken && (params->blocksize_bytes != be_lun->blocksize)) { 1731229997Sken snprintf(req->error_str, sizeof(req->error_str), 1732272911Smav "requested blocksize %u < backing device " 1733272911Smav "blocksize %u", params->blocksize_bytes, 1734229997Sken be_lun->blocksize); 1735229997Sken return (EINVAL); 1736229997Sken } 1737229997Sken 1738229997Sken error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1739229997Sken (caddr_t)&be_lun->size_bytes, FREAD, 1740229997Sken curthread); 1741229997Sken if (error) { 1742229997Sken snprintf(req->error_str, sizeof(req->error_str), 1743272911Smav "error %d returned for DIOCGMEDIASIZE " 1744272911Smav " ioctl on %s!", error, 1745232604Strasz be_lun->dev_path); 1746229997Sken return (error); 1747229997Sken } 1748229997Sken 1749232604Strasz if (params->lun_size_bytes != 0) { 1750232604Strasz if (params->lun_size_bytes > be_lun->size_bytes) { 1751232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1752272911Smav "requested LUN size %ju > backing device " 1753272911Smav "size %ju", 1754232604Strasz (uintmax_t)params->lun_size_bytes, 1755232604Strasz (uintmax_t)be_lun->size_bytes); 1756232604Strasz return (EINVAL); 1757232604Strasz } 1758232604Strasz 1759232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1760232604Strasz } 1761232604Strasz 1762264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1763264191Smav (caddr_t)&ps, FREAD, curthread); 1764264191Smav if (error) 1765264191Smav ps = po = 0; 1766264191Smav else { 1767264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1768264191Smav (caddr_t)&po, FREAD, curthread); 1769264191Smav if (error) 1770264191Smav po = 0; 1771264191Smav } 1772264191Smav pss = ps / be_lun->blocksize; 1773264191Smav pos = po / be_lun->blocksize; 1774264191Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1775264191Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1776264191Smav be_lun->pblockexp = fls(pss) - 1; 1777264191Smav be_lun->pblockoff = (pss - pos) % pss; 1778264191Smav } 1779264191Smav 1780229997Sken return (0); 1781229997Sken} 1782229997Sken 1783229997Skenstatic int 1784229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 1785229997Sken{ 1786229997Sken DROP_GIANT(); 1787229997Sken if (be_lun->vn) { 1788229997Sken int flags = FREAD | FWRITE; 1789229997Sken 1790229997Sken switch (be_lun->dev_type) { 1791229997Sken case CTL_BE_BLOCK_DEV: 1792229997Sken if (be_lun->backend.dev.csw) { 1793229997Sken dev_relthread(be_lun->backend.dev.cdev, 1794229997Sken be_lun->backend.dev.dev_ref); 1795229997Sken be_lun->backend.dev.csw = NULL; 1796229997Sken be_lun->backend.dev.cdev = NULL; 1797229997Sken } 1798229997Sken break; 1799229997Sken case CTL_BE_BLOCK_FILE: 1800229997Sken break; 1801229997Sken case CTL_BE_BLOCK_NONE: 1802258871Strasz break; 1803229997Sken default: 1804229997Sken panic("Unexpected backend type."); 1805229997Sken break; 1806229997Sken } 1807229997Sken 1808229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1809229997Sken be_lun->vn = NULL; 1810229997Sken 1811229997Sken switch (be_lun->dev_type) { 1812229997Sken case CTL_BE_BLOCK_DEV: 1813229997Sken break; 1814229997Sken case CTL_BE_BLOCK_FILE: 1815229997Sken if (be_lun->backend.file.cred != NULL) { 1816229997Sken crfree(be_lun->backend.file.cred); 1817229997Sken be_lun->backend.file.cred = NULL; 1818229997Sken } 1819229997Sken break; 1820229997Sken case CTL_BE_BLOCK_NONE: 1821258871Strasz break; 1822229997Sken default: 1823229997Sken panic("Unexpected backend type."); 1824229997Sken break; 1825229997Sken } 1826272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 1827229997Sken } 1828229997Sken PICKUP_GIANT(); 1829229997Sken 1830229997Sken return (0); 1831229997Sken} 1832229997Sken 1833229997Skenstatic int 1834229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 1835229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1836229997Sken{ 1837229997Sken struct nameidata nd; 1838229997Sken int flags; 1839229997Sken int error; 1840229997Sken 1841229997Sken /* 1842229997Sken * XXX KDM allow a read-only option? 1843229997Sken */ 1844229997Sken flags = FREAD | FWRITE; 1845229997Sken error = 0; 1846229997Sken 1847229997Sken if (rootvnode == NULL) { 1848229997Sken snprintf(req->error_str, sizeof(req->error_str), 1849272911Smav "Root filesystem is not mounted"); 1850229997Sken return (1); 1851229997Sken } 1852229997Sken 1853229997Sken if (!curthread->td_proc->p_fd->fd_cdir) { 1854229997Sken curthread->td_proc->p_fd->fd_cdir = rootvnode; 1855229997Sken VREF(rootvnode); 1856229997Sken } 1857229997Sken if (!curthread->td_proc->p_fd->fd_rdir) { 1858229997Sken curthread->td_proc->p_fd->fd_rdir = rootvnode; 1859229997Sken VREF(rootvnode); 1860229997Sken } 1861229997Sken if (!curthread->td_proc->p_fd->fd_jdir) { 1862229997Sken curthread->td_proc->p_fd->fd_jdir = rootvnode; 1863229997Sken VREF(rootvnode); 1864229997Sken } 1865229997Sken 1866229997Sken again: 1867229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1868229997Sken error = vn_open(&nd, &flags, 0, NULL); 1869229997Sken if (error) { 1870229997Sken /* 1871229997Sken * This is the only reasonable guess we can make as far as 1872229997Sken * path if the user doesn't give us a fully qualified path. 1873229997Sken * If they want to specify a file, they need to specify the 1874229997Sken * full path. 1875229997Sken */ 1876229997Sken if (be_lun->dev_path[0] != '/') { 1877229997Sken char *dev_path = "/dev/"; 1878229997Sken char *dev_name; 1879229997Sken 1880229997Sken /* Try adding device path at beginning of name */ 1881229997Sken dev_name = malloc(strlen(be_lun->dev_path) 1882229997Sken + strlen(dev_path) + 1, 1883229997Sken M_CTLBLK, M_WAITOK); 1884229997Sken if (dev_name) { 1885229997Sken sprintf(dev_name, "%s%s", dev_path, 1886229997Sken be_lun->dev_path); 1887229997Sken free(be_lun->dev_path, M_CTLBLK); 1888229997Sken be_lun->dev_path = dev_name; 1889229997Sken goto again; 1890229997Sken } 1891229997Sken } 1892229997Sken snprintf(req->error_str, sizeof(req->error_str), 1893272911Smav "error opening %s: %d", be_lun->dev_path, error); 1894229997Sken return (error); 1895229997Sken } 1896229997Sken 1897229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 1898229997Sken 1899229997Sken be_lun->vn = nd.ni_vp; 1900229997Sken 1901229997Sken /* We only support disks and files. */ 1902229997Sken if (vn_isdisk(be_lun->vn, &error)) { 1903229997Sken error = ctl_be_block_open_dev(be_lun, req); 1904229997Sken } else if (be_lun->vn->v_type == VREG) { 1905229997Sken error = ctl_be_block_open_file(be_lun, req); 1906229997Sken } else { 1907229997Sken error = EINVAL; 1908229997Sken snprintf(req->error_str, sizeof(req->error_str), 1909258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 1910229997Sken } 1911229997Sken VOP_UNLOCK(be_lun->vn, 0); 1912229997Sken 1913229997Sken if (error != 0) { 1914229997Sken ctl_be_block_close(be_lun); 1915229997Sken return (error); 1916229997Sken } 1917229997Sken 1918229997Sken be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1919229997Sken be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1920229997Sken 1921229997Sken return (0); 1922229997Sken} 1923229997Sken 1924229997Skenstatic int 1925229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1926229997Sken{ 1927229997Sken struct ctl_be_block_lun *be_lun; 1928229997Sken struct ctl_lun_create_params *params; 1929267481Smav char num_thread_str[16]; 1930229997Sken char tmpstr[32]; 1931267481Smav char *value; 1932264274Smav int retval, num_threads, unmap; 1933267481Smav int tmp_num_threads; 1934229997Sken 1935229997Sken params = &req->reqdata.create; 1936229997Sken retval = 0; 1937272911Smav req->status = CTL_LUN_OK; 1938229997Sken 1939229997Sken num_threads = cbb_num_threads; 1940229997Sken 1941229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1942229997Sken 1943272911Smav be_lun->params = req->reqdata.create; 1944229997Sken be_lun->softc = softc; 1945229997Sken STAILQ_INIT(&be_lun->input_queue); 1946229997Sken STAILQ_INIT(&be_lun->config_write_queue); 1947229997Sken STAILQ_INIT(&be_lun->datamove_queue); 1948229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1949267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 1950267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 1951268280Smav ctl_init_opts(&be_lun->ctl_be_lun.options, 1952268280Smav req->num_be_args, req->kern_be_args); 1953229997Sken 1954264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1955256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1956229997Sken 1957229997Sken if (be_lun->lun_zone == NULL) { 1958229997Sken snprintf(req->error_str, sizeof(req->error_str), 1959272911Smav "error allocating UMA zone"); 1960229997Sken goto bailout_error; 1961229997Sken } 1962229997Sken 1963229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1964229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 1965229997Sken else 1966229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 1967229997Sken 1968229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1969268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 1970267499Smav if (value == NULL) { 1971229997Sken snprintf(req->error_str, sizeof(req->error_str), 1972272911Smav "no file argument specified"); 1973229997Sken goto bailout_error; 1974229997Sken } 1975267499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 1976272911Smav be_lun->blocksize = 512; 1977272911Smav be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1978229997Sken 1979229997Sken retval = ctl_be_block_open(softc, be_lun, req); 1980229997Sken if (retval != 0) { 1981229997Sken retval = 0; 1982272911Smav req->status = CTL_LUN_WARNING; 1983229997Sken } 1984229997Sken } else { 1985229997Sken /* 1986229997Sken * For processor devices, we don't have any size. 1987229997Sken */ 1988229997Sken be_lun->blocksize = 0; 1989264191Smav be_lun->pblockexp = 0; 1990264191Smav be_lun->pblockoff = 0; 1991229997Sken be_lun->size_blocks = 0; 1992229997Sken be_lun->size_bytes = 0; 1993229997Sken be_lun->ctl_be_lun.maxlba = 0; 1994229997Sken 1995229997Sken /* 1996229997Sken * Default to just 1 thread for processor devices. 1997229997Sken */ 1998229997Sken num_threads = 1; 1999229997Sken } 2000229997Sken 2001229997Sken /* 2002229997Sken * XXX This searching loop might be refactored to be combined with 2003229997Sken * the loop above, 2004229997Sken */ 2005268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 2006267481Smav if (value != NULL) { 2007267481Smav tmp_num_threads = strtol(value, NULL, 0); 2008229997Sken 2009267481Smav /* 2010267481Smav * We don't let the user specify less than one 2011267481Smav * thread, but hope he's clueful enough not to 2012267481Smav * specify 1000 threads. 2013267481Smav */ 2014267481Smav if (tmp_num_threads < 1) { 2015267481Smav snprintf(req->error_str, sizeof(req->error_str), 2016272911Smav "invalid number of threads %s", 2017272911Smav num_thread_str); 2018267481Smav goto bailout_error; 2019229997Sken } 2020267481Smav num_threads = tmp_num_threads; 2021229997Sken } 2022274154Smav unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol); 2023268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2024274154Smav if (value != NULL) 2025274154Smav unmap = (strcmp(value, "on") == 0); 2026229997Sken 2027229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2028229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 2029272911Smav if (be_lun->vn == NULL) 2030272911Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2031264274Smav if (unmap) 2032264274Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2033229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 2034272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2035272911Smav 0 : (be_lun->size_blocks - 1); 2036229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2037264191Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2038264191Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2039272911Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2040272911Smav be_lun->blocksize != 0) 2041272911Smav be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2042272911Smav be_lun->blocksize; 2043229997Sken /* Tell the user the blocksize we ended up using */ 2044272911Smav params->lun_size_bytes = be_lun->size_bytes; 2045229997Sken params->blocksize_bytes = be_lun->blocksize; 2046229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2047229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2048229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2049229997Sken } else 2050229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 2051229997Sken 2052229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2053229997Sken be_lun->ctl_be_lun.lun_config_status = 2054229997Sken ctl_be_block_lun_config_status; 2055229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2056229997Sken 2057229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2058229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2059229997Sken softc->num_luns); 2060229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2061229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2062229997Sken sizeof(tmpstr))); 2063229997Sken 2064229997Sken /* Tell the user what we used for a serial number */ 2065229997Sken strncpy((char *)params->serial_num, tmpstr, 2066229997Sken ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 2067229997Sken } else { 2068229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 2069229997Sken params->serial_num, 2070229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2071229997Sken sizeof(params->serial_num))); 2072229997Sken } 2073229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2074229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2075229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2076229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2077229997Sken sizeof(tmpstr))); 2078229997Sken 2079229997Sken /* Tell the user what we used for a device ID */ 2080229997Sken strncpy((char *)params->device_id, tmpstr, 2081229997Sken ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2082229997Sken } else { 2083229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 2084229997Sken params->device_id, 2085229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2086229997Sken sizeof(params->device_id))); 2087229997Sken } 2088229997Sken 2089229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2090229997Sken 2091229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2092229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2093229997Sken 2094229997Sken if (be_lun->io_taskqueue == NULL) { 2095229997Sken snprintf(req->error_str, sizeof(req->error_str), 2096272911Smav "unable to create taskqueue"); 2097229997Sken goto bailout_error; 2098229997Sken } 2099229997Sken 2100229997Sken /* 2101229997Sken * Note that we start the same number of threads by default for 2102229997Sken * both the file case and the block device case. For the file 2103229997Sken * case, we need multiple threads to allow concurrency, because the 2104229997Sken * vnode interface is designed to be a blocking interface. For the 2105229997Sken * block device case, ZFS zvols at least will block the caller's 2106229997Sken * context in many instances, and so we need multiple threads to 2107229997Sken * overcome that problem. Other block devices don't need as many 2108229997Sken * threads, but they shouldn't cause too many problems. 2109229997Sken * 2110229997Sken * If the user wants to just have a single thread for a block 2111229997Sken * device, he can specify that when the LUN is created, or change 2112229997Sken * the tunable/sysctl to alter the default number of threads. 2113229997Sken */ 2114229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2115229997Sken /*num threads*/num_threads, 2116229997Sken /*priority*/PWAIT, 2117229997Sken /*thread name*/ 2118229997Sken "%s taskq", be_lun->lunname); 2119229997Sken 2120229997Sken if (retval != 0) 2121229997Sken goto bailout_error; 2122229997Sken 2123229997Sken be_lun->num_threads = num_threads; 2124229997Sken 2125229997Sken mtx_lock(&softc->lock); 2126229997Sken softc->num_luns++; 2127229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2128229997Sken 2129229997Sken mtx_unlock(&softc->lock); 2130229997Sken 2131229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 2132229997Sken if (retval != 0) { 2133229997Sken mtx_lock(&softc->lock); 2134229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2135229997Sken links); 2136229997Sken softc->num_luns--; 2137229997Sken mtx_unlock(&softc->lock); 2138229997Sken snprintf(req->error_str, sizeof(req->error_str), 2139272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2140272911Smav "details", retval); 2141229997Sken retval = 0; 2142229997Sken goto bailout_error; 2143229997Sken } 2144229997Sken 2145229997Sken mtx_lock(&softc->lock); 2146229997Sken 2147229997Sken /* 2148229997Sken * Tell the config_status routine that we're waiting so it won't 2149229997Sken * clean up the LUN in the event of an error. 2150229997Sken */ 2151229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2152229997Sken 2153229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2154229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2155229997Sken if (retval == EINTR) 2156229997Sken break; 2157229997Sken } 2158229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2159229997Sken 2160229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2161229997Sken snprintf(req->error_str, sizeof(req->error_str), 2162272911Smav "LUN configuration error, see dmesg for details"); 2163229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2164229997Sken links); 2165229997Sken softc->num_luns--; 2166229997Sken mtx_unlock(&softc->lock); 2167229997Sken goto bailout_error; 2168229997Sken } else { 2169229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2170229997Sken } 2171229997Sken 2172229997Sken mtx_unlock(&softc->lock); 2173229997Sken 2174229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2175229997Sken be_lun->blocksize, 2176229997Sken DEVSTAT_ALL_SUPPORTED, 2177229997Sken be_lun->ctl_be_lun.lun_type 2178229997Sken | DEVSTAT_TYPE_IF_OTHER, 2179229997Sken DEVSTAT_PRIORITY_OTHER); 2180229997Sken 2181229997Sken return (retval); 2182229997Sken 2183229997Skenbailout_error: 2184229997Sken req->status = CTL_LUN_ERROR; 2185229997Sken 2186267429Smav if (be_lun->io_taskqueue != NULL) 2187267429Smav taskqueue_free(be_lun->io_taskqueue); 2188229997Sken ctl_be_block_close(be_lun); 2189267429Smav if (be_lun->dev_path != NULL) 2190267429Smav free(be_lun->dev_path, M_CTLBLK); 2191267429Smav if (be_lun->lun_zone != NULL) 2192267429Smav uma_zdestroy(be_lun->lun_zone); 2193268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2194267877Smav mtx_destroy(&be_lun->queue_lock); 2195267877Smav mtx_destroy(&be_lun->io_lock); 2196229997Sken free(be_lun, M_CTLBLK); 2197229997Sken 2198229997Sken return (retval); 2199229997Sken} 2200229997Sken 2201229997Skenstatic int 2202229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2203229997Sken{ 2204229997Sken struct ctl_lun_rm_params *params; 2205229997Sken struct ctl_be_block_lun *be_lun; 2206229997Sken int retval; 2207229997Sken 2208229997Sken params = &req->reqdata.rm; 2209229997Sken 2210229997Sken mtx_lock(&softc->lock); 2211229997Sken 2212229997Sken be_lun = NULL; 2213229997Sken 2214229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2215229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2216229997Sken break; 2217229997Sken } 2218229997Sken mtx_unlock(&softc->lock); 2219229997Sken 2220229997Sken if (be_lun == NULL) { 2221229997Sken snprintf(req->error_str, sizeof(req->error_str), 2222272911Smav "LUN %u is not managed by the block backend", 2223272911Smav params->lun_id); 2224229997Sken goto bailout_error; 2225229997Sken } 2226229997Sken 2227229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2228229997Sken 2229229997Sken if (retval != 0) { 2230229997Sken snprintf(req->error_str, sizeof(req->error_str), 2231272911Smav "error %d returned from ctl_disable_lun() for " 2232272911Smav "LUN %d", retval, params->lun_id); 2233229997Sken goto bailout_error; 2234229997Sken 2235229997Sken } 2236229997Sken 2237229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2238229997Sken if (retval != 0) { 2239229997Sken snprintf(req->error_str, sizeof(req->error_str), 2240272911Smav "error %d returned from ctl_invalidate_lun() for " 2241272911Smav "LUN %d", retval, params->lun_id); 2242229997Sken goto bailout_error; 2243229997Sken } 2244229997Sken 2245229997Sken mtx_lock(&softc->lock); 2246229997Sken 2247229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2248229997Sken 2249229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2250229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2251229997Sken if (retval == EINTR) 2252229997Sken break; 2253229997Sken } 2254229997Sken 2255229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2256229997Sken 2257229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2258229997Sken snprintf(req->error_str, sizeof(req->error_str), 2259272911Smav "interrupted waiting for LUN to be freed"); 2260229997Sken mtx_unlock(&softc->lock); 2261229997Sken goto bailout_error; 2262229997Sken } 2263229997Sken 2264229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2265229997Sken 2266229997Sken softc->num_luns--; 2267229997Sken mtx_unlock(&softc->lock); 2268229997Sken 2269229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2270229997Sken 2271229997Sken taskqueue_free(be_lun->io_taskqueue); 2272229997Sken 2273229997Sken ctl_be_block_close(be_lun); 2274229997Sken 2275229997Sken if (be_lun->disk_stats != NULL) 2276229997Sken devstat_remove_entry(be_lun->disk_stats); 2277229997Sken 2278229997Sken uma_zdestroy(be_lun->lun_zone); 2279229997Sken 2280268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2281229997Sken free(be_lun->dev_path, M_CTLBLK); 2282267877Smav mtx_destroy(&be_lun->queue_lock); 2283267877Smav mtx_destroy(&be_lun->io_lock); 2284229997Sken free(be_lun, M_CTLBLK); 2285229997Sken 2286229997Sken req->status = CTL_LUN_OK; 2287229997Sken 2288229997Sken return (0); 2289229997Sken 2290229997Skenbailout_error: 2291229997Sken 2292229997Sken req->status = CTL_LUN_ERROR; 2293229997Sken 2294229997Sken return (0); 2295229997Sken} 2296229997Sken 2297232604Straszstatic int 2298232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2299232604Strasz struct ctl_lun_req *req) 2300232604Strasz{ 2301232604Strasz struct vattr vattr; 2302232604Strasz int error; 2303272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2304232604Strasz 2305232604Strasz if (params->lun_size_bytes != 0) { 2306232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2307232604Strasz } else { 2308271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2309232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2310271794Smav VOP_UNLOCK(be_lun->vn, 0); 2311232604Strasz if (error != 0) { 2312232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2313232604Strasz "error calling VOP_GETATTR() for file %s", 2314232604Strasz be_lun->dev_path); 2315232604Strasz return (error); 2316232604Strasz } 2317232604Strasz 2318232604Strasz be_lun->size_bytes = vattr.va_size; 2319232604Strasz } 2320232604Strasz 2321232604Strasz return (0); 2322232604Strasz} 2323232604Strasz 2324232604Straszstatic int 2325232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2326232604Strasz struct ctl_lun_req *req) 2327232604Strasz{ 2328271794Smav struct ctl_be_block_devdata *dev_data; 2329232604Strasz int error; 2330272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2331232604Strasz uint64_t size_bytes; 2332232604Strasz 2333271794Smav dev_data = &be_lun->backend.dev; 2334271794Smav if (!dev_data->csw->d_ioctl) { 2335232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2336272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2337232604Strasz return (ENODEV); 2338232604Strasz } 2339232604Strasz 2340271794Smav error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2341232604Strasz (caddr_t)&size_bytes, FREAD, 2342232604Strasz curthread); 2343232604Strasz if (error) { 2344232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2345272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2346272911Smav "on %s!", error, be_lun->dev_path); 2347232604Strasz return (error); 2348232604Strasz } 2349232604Strasz 2350232604Strasz if (params->lun_size_bytes != 0) { 2351232604Strasz if (params->lun_size_bytes > size_bytes) { 2352232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2353272911Smav "requested LUN size %ju > backing device " 2354272911Smav "size %ju", 2355232604Strasz (uintmax_t)params->lun_size_bytes, 2356232604Strasz (uintmax_t)size_bytes); 2357232604Strasz return (EINVAL); 2358232604Strasz } 2359232604Strasz 2360232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2361232604Strasz } else { 2362232604Strasz be_lun->size_bytes = size_bytes; 2363232604Strasz } 2364232604Strasz 2365232604Strasz return (0); 2366232604Strasz} 2367232604Strasz 2368232604Straszstatic int 2369232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2370232604Strasz{ 2371232604Strasz struct ctl_lun_modify_params *params; 2372232604Strasz struct ctl_be_block_lun *be_lun; 2373271794Smav uint64_t oldsize; 2374241896Skib int error; 2375232604Strasz 2376232604Strasz params = &req->reqdata.modify; 2377232604Strasz 2378232604Strasz mtx_lock(&softc->lock); 2379232604Strasz be_lun = NULL; 2380232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2381232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2382232604Strasz break; 2383232604Strasz } 2384232604Strasz mtx_unlock(&softc->lock); 2385232604Strasz 2386232604Strasz if (be_lun == NULL) { 2387232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2388272911Smav "LUN %u is not managed by the block backend", 2389272911Smav params->lun_id); 2390232604Strasz goto bailout_error; 2391232604Strasz } 2392232604Strasz 2393272911Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2394232604Strasz 2395272911Smav oldsize = be_lun->size_blocks; 2396272911Smav if (be_lun->vn == NULL) 2397272911Smav error = ctl_be_block_open(softc, be_lun, req); 2398272911Smav else if (be_lun->vn->v_type == VREG) 2399232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2400232604Strasz else 2401232604Strasz error = ctl_be_block_modify_dev(be_lun, req); 2402232604Strasz 2403272911Smav if (error == 0 && be_lun->size_blocks != oldsize) { 2404271794Smav be_lun->size_blocks = be_lun->size_bytes >> 2405271794Smav be_lun->blocksize_shift; 2406232604Strasz 2407271794Smav /* 2408271794Smav * The maximum LBA is the size - 1. 2409271794Smav * 2410271794Smav * XXX: Note that this field is being updated without locking, 2411271794Smav * which might cause problems on 32-bit architectures. 2412271794Smav */ 2413272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2414272911Smav 0 : (be_lun->size_blocks - 1); 2415272911Smav be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2416272911Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2417272911Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2418272911Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2419272911Smav be_lun->blocksize != 0) 2420272911Smav be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2421272911Smav be_lun->blocksize; 2422271794Smav ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2423272911Smav if (oldsize == 0 && be_lun->size_blocks != 0) 2424272911Smav ctl_lun_online(&be_lun->ctl_be_lun); 2425271794Smav } 2426232604Strasz 2427232604Strasz /* Tell the user the exact size we ended up using */ 2428232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2429232604Strasz 2430272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2431232604Strasz 2432232604Strasz return (0); 2433232604Strasz 2434232604Straszbailout_error: 2435232604Strasz req->status = CTL_LUN_ERROR; 2436232604Strasz 2437232604Strasz return (0); 2438232604Strasz} 2439232604Strasz 2440229997Skenstatic void 2441229997Skenctl_be_block_lun_shutdown(void *be_lun) 2442229997Sken{ 2443229997Sken struct ctl_be_block_lun *lun; 2444229997Sken struct ctl_be_block_softc *softc; 2445229997Sken 2446229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2447229997Sken 2448229997Sken softc = lun->softc; 2449229997Sken 2450229997Sken mtx_lock(&softc->lock); 2451229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2452229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2453229997Sken wakeup(lun); 2454229997Sken mtx_unlock(&softc->lock); 2455229997Sken 2456229997Sken} 2457229997Sken 2458229997Skenstatic void 2459229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2460229997Sken{ 2461229997Sken struct ctl_be_block_lun *lun; 2462229997Sken struct ctl_be_block_softc *softc; 2463229997Sken 2464229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2465229997Sken softc = lun->softc; 2466229997Sken 2467229997Sken if (status == CTL_LUN_CONFIG_OK) { 2468229997Sken mtx_lock(&softc->lock); 2469229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2470229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2471229997Sken wakeup(lun); 2472229997Sken mtx_unlock(&softc->lock); 2473229997Sken 2474229997Sken /* 2475229997Sken * We successfully added the LUN, attempt to enable it. 2476229997Sken */ 2477229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2478229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2479229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2480229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2481229997Sken __func__); 2482229997Sken } 2483229997Sken } 2484229997Sken 2485229997Sken return; 2486229997Sken } 2487229997Sken 2488229997Sken 2489229997Sken mtx_lock(&softc->lock); 2490229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2491229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2492229997Sken wakeup(lun); 2493229997Sken mtx_unlock(&softc->lock); 2494229997Sken} 2495229997Sken 2496229997Sken 2497229997Skenstatic int 2498229997Skenctl_be_block_config_write(union ctl_io *io) 2499229997Sken{ 2500229997Sken struct ctl_be_block_lun *be_lun; 2501229997Sken struct ctl_be_lun *ctl_be_lun; 2502229997Sken int retval; 2503229997Sken 2504229997Sken retval = 0; 2505229997Sken 2506229997Sken DPRINTF("entered\n"); 2507229997Sken 2508229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2509229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2510229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2511229997Sken 2512229997Sken switch (io->scsiio.cdb[0]) { 2513229997Sken case SYNCHRONIZE_CACHE: 2514229997Sken case SYNCHRONIZE_CACHE_16: 2515264274Smav case WRITE_SAME_10: 2516264274Smav case WRITE_SAME_16: 2517264274Smav case UNMAP: 2518229997Sken /* 2519229997Sken * The upper level CTL code will filter out any CDBs with 2520229997Sken * the immediate bit set and return the proper error. 2521229997Sken * 2522229997Sken * We don't really need to worry about what LBA range the 2523229997Sken * user asked to be synced out. When they issue a sync 2524229997Sken * cache command, we'll sync out the whole thing. 2525229997Sken */ 2526267877Smav mtx_lock(&be_lun->queue_lock); 2527229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2528229997Sken links); 2529267877Smav mtx_unlock(&be_lun->queue_lock); 2530229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2531229997Sken break; 2532229997Sken case START_STOP_UNIT: { 2533229997Sken struct scsi_start_stop_unit *cdb; 2534229997Sken 2535229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2536229997Sken 2537229997Sken if (cdb->how & SSS_START) 2538229997Sken retval = ctl_start_lun(ctl_be_lun); 2539229997Sken else { 2540229997Sken retval = ctl_stop_lun(ctl_be_lun); 2541229997Sken /* 2542229997Sken * XXX KDM Copan-specific offline behavior. 2543229997Sken * Figure out a reasonable way to port this? 2544229997Sken */ 2545229997Sken#ifdef NEEDTOPORT 2546229997Sken if ((retval == 0) 2547229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2548229997Sken retval = ctl_lun_offline(ctl_be_lun); 2549229997Sken#endif 2550229997Sken } 2551229997Sken 2552229997Sken /* 2553229997Sken * In general, the above routines should not fail. They 2554229997Sken * just set state for the LUN. So we've got something 2555229997Sken * pretty wrong here if we can't start or stop the LUN. 2556229997Sken */ 2557229997Sken if (retval != 0) { 2558229997Sken ctl_set_internal_failure(&io->scsiio, 2559229997Sken /*sks_valid*/ 1, 2560229997Sken /*retry_count*/ 0xf051); 2561229997Sken retval = CTL_RETVAL_COMPLETE; 2562229997Sken } else { 2563229997Sken ctl_set_success(&io->scsiio); 2564229997Sken } 2565229997Sken ctl_config_write_done(io); 2566229997Sken break; 2567229997Sken } 2568229997Sken default: 2569229997Sken ctl_set_invalid_opcode(&io->scsiio); 2570229997Sken ctl_config_write_done(io); 2571229997Sken retval = CTL_RETVAL_COMPLETE; 2572229997Sken break; 2573229997Sken } 2574229997Sken 2575229997Sken return (retval); 2576229997Sken 2577229997Sken} 2578229997Sken 2579229997Skenstatic int 2580229997Skenctl_be_block_config_read(union ctl_io *io) 2581229997Sken{ 2582229997Sken return (0); 2583229997Sken} 2584229997Sken 2585229997Skenstatic int 2586229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2587229997Sken{ 2588229997Sken struct ctl_be_block_lun *lun; 2589229997Sken int retval; 2590229997Sken 2591229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2592229997Sken retval = 0; 2593229997Sken 2594268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2595229997Sken 2596229997Sken if (retval != 0) 2597229997Sken goto bailout; 2598229997Sken 2599229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2600229997Sken 2601229997Sken if (retval != 0) 2602229997Sken goto bailout; 2603229997Sken 2604268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2605229997Sken 2606229997Skenbailout: 2607229997Sken 2608229997Sken return (retval); 2609229997Sken} 2610229997Sken 2611274154Smavstatic uint64_t 2612274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2613274154Smav{ 2614274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2615274154Smav 2616274154Smav if (lun->getattr == NULL) 2617274154Smav return (UINT64_MAX); 2618274154Smav return (lun->getattr(lun, attrname)); 2619274154Smav} 2620274154Smav 2621229997Skenint 2622229997Skenctl_be_block_init(void) 2623229997Sken{ 2624229997Sken struct ctl_be_block_softc *softc; 2625229997Sken int retval; 2626229997Sken 2627229997Sken softc = &backend_block_softc; 2628229997Sken retval = 0; 2629229997Sken 2630267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2631264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2632264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2633229997Sken STAILQ_INIT(&softc->disk_list); 2634229997Sken STAILQ_INIT(&softc->lun_list); 2635229997Sken 2636229997Sken return (retval); 2637229997Sken} 2638