ctl_backend_block.c revision 275058
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 275058 2014-11-25 17:53:35Z 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 */ 384275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 385275058Smav ; 386275058Smav } else if ((io->io_hdr.port_status == 0) && 387267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 388267537Smav lbalen = ARGS(beio->io); 389267537Smav if (lbalen->flags & CTL_LLF_READ) { 390267537Smav ctl_set_success(&io->scsiio); 391267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 392267537Smav /* We have two data blocks ready for comparison. */ 393267537Smav for (i = 0; i < beio->num_segs; i++) { 394267537Smav if (memcmp(beio->sg_segs[i].addr, 395267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 396267537Smav beio->sg_segs[i].len) != 0) 397267537Smav break; 398267537Smav } 399267537Smav if (i < beio->num_segs) 400267537Smav ctl_set_sense(&io->scsiio, 401267537Smav /*current_error*/ 1, 402267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 403267537Smav /*asc*/ 0x1D, 404267537Smav /*ascq*/ 0x00, 405267537Smav SSD_ELEM_NONE); 406267537Smav else 407267537Smav ctl_set_success(&io->scsiio); 408267537Smav } 409275058Smav } else if ((io->io_hdr.port_status != 0) && 410275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 411275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 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 { 535275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 536275058Smav beio->beio_cont == NULL) 537275058Smav ctl_set_success(&io->scsiio); 538229997Sken#ifdef CTL_TIME_IO 539229997Sken getbintime(&io->io_hdr.dma_start_bt); 540229997Sken#endif 541229997Sken ctl_datamove(io); 542229997Sken } 543229997Sken} 544229997Sken 545229997Skenstatic void 546229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 547229997Sken struct ctl_be_block_io *beio) 548229997Sken{ 549267877Smav union ctl_io *io = beio->io; 550229997Sken struct mount *mountpoint; 551241896Skib int error, lock_flags; 552229997Sken 553229997Sken DPRINTF("entered\n"); 554229997Sken 555267877Smav binuptime(&beio->ds_t0); 556267877Smav mtx_lock(&be_lun->io_lock); 557267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 558267877Smav mtx_unlock(&be_lun->io_lock); 559229997Sken 560267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 561229997Sken 562229997Sken if (MNT_SHARED_WRITES(mountpoint) 563229997Sken || ((mountpoint == NULL) 564229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 565229997Sken lock_flags = LK_SHARED; 566229997Sken else 567229997Sken lock_flags = LK_EXCLUSIVE; 568229997Sken 569229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 570229997Sken 571229997Sken error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 572229997Sken VOP_UNLOCK(be_lun->vn, 0); 573229997Sken 574229997Sken vn_finished_write(mountpoint); 575229997Sken 576267877Smav mtx_lock(&be_lun->io_lock); 577267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 578267877Smav beio->ds_tag_type, beio->ds_trans_type, 579267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 580267877Smav mtx_unlock(&be_lun->io_lock); 581267877Smav 582229997Sken if (error == 0) 583229997Sken ctl_set_success(&io->scsiio); 584229997Sken else { 585229997Sken /* XXX KDM is there is a better error here? */ 586229997Sken ctl_set_internal_failure(&io->scsiio, 587229997Sken /*sks_valid*/ 1, 588229997Sken /*retry_count*/ 0xbad1); 589229997Sken } 590229997Sken 591229997Sken ctl_complete_beio(beio); 592229997Sken} 593229997Sken 594258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 595258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 596258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 597258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 598229997Sken 599229997Skenstatic void 600229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 601229997Sken struct ctl_be_block_io *beio) 602229997Sken{ 603229997Sken struct ctl_be_block_filedata *file_data; 604229997Sken union ctl_io *io; 605229997Sken struct uio xuio; 606229997Sken struct iovec *xiovec; 607241896Skib int flags; 608229997Sken int error, i; 609229997Sken 610229997Sken DPRINTF("entered\n"); 611229997Sken 612229997Sken file_data = &be_lun->backend.file; 613229997Sken io = beio->io; 614271309Smav flags = 0; 615271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 616271309Smav flags |= IO_DIRECT; 617271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 618271309Smav flags |= IO_SYNC; 619229997Sken 620267537Smav bzero(&xuio, sizeof(xuio)); 621229997Sken if (beio->bio_cmd == BIO_READ) { 622229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 623267537Smav xuio.uio_rw = UIO_READ; 624229997Sken } else { 625229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 626267537Smav xuio.uio_rw = UIO_WRITE; 627229997Sken } 628229997Sken xuio.uio_offset = beio->io_offset; 629229997Sken xuio.uio_resid = beio->io_len; 630229997Sken xuio.uio_segflg = UIO_SYSSPACE; 631229997Sken xuio.uio_iov = beio->xiovecs; 632229997Sken xuio.uio_iovcnt = beio->num_segs; 633229997Sken xuio.uio_td = curthread; 634229997Sken 635229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 636229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 637229997Sken xiovec->iov_len = beio->sg_segs[i].len; 638229997Sken } 639229997Sken 640267877Smav binuptime(&beio->ds_t0); 641267877Smav mtx_lock(&be_lun->io_lock); 642267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 643267877Smav mtx_unlock(&be_lun->io_lock); 644267877Smav 645229997Sken if (beio->bio_cmd == BIO_READ) { 646229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 647229997Sken 648229997Sken /* 649229997Sken * UFS pays attention to IO_DIRECT for reads. If the 650229997Sken * DIRECTIO option is configured into the kernel, it calls 651229997Sken * ffs_rawread(). But that only works for single-segment 652229997Sken * uios with user space addresses. In our case, with a 653229997Sken * kernel uio, it still reads into the buffer cache, but it 654229997Sken * will just try to release the buffer from the cache later 655229997Sken * on in ffs_read(). 656229997Sken * 657229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 658229997Sken * 659229997Sken * UFS does not pay attention to IO_SYNC for reads. 660229997Sken * 661229997Sken * ZFS pays attention to IO_SYNC (which translates into the 662229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 663229997Sken * attempts to sync the file before reading. 664229997Sken * 665229997Sken * So, to attempt to provide some barrier semantics in the 666229997Sken * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 667229997Sken */ 668271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 669229997Sken 670229997Sken VOP_UNLOCK(be_lun->vn, 0); 671267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 672229997Sken } else { 673229997Sken struct mount *mountpoint; 674229997Sken int lock_flags; 675229997Sken 676229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 677229997Sken 678229997Sken if (MNT_SHARED_WRITES(mountpoint) 679229997Sken || ((mountpoint == NULL) 680229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 681229997Sken lock_flags = LK_SHARED; 682229997Sken else 683229997Sken lock_flags = LK_EXCLUSIVE; 684229997Sken 685229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 686229997Sken 687229997Sken /* 688229997Sken * UFS pays attention to IO_DIRECT for writes. The write 689229997Sken * is done asynchronously. (Normally the write would just 690229997Sken * get put into cache. 691229997Sken * 692229997Sken * UFS pays attention to IO_SYNC for writes. It will 693229997Sken * attempt to write the buffer out synchronously if that 694229997Sken * flag is set. 695229997Sken * 696229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 697229997Sken * 698229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 699229997Sken * for writes. It will flush the transaction from the 700229997Sken * cache before returning. 701229997Sken * 702229997Sken * So if we've got the BIO_ORDERED flag set, we want 703229997Sken * IO_SYNC in either the UFS or ZFS case. 704229997Sken */ 705271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 706229997Sken VOP_UNLOCK(be_lun->vn, 0); 707229997Sken 708229997Sken vn_finished_write(mountpoint); 709267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 710229997Sken } 711229997Sken 712267877Smav mtx_lock(&be_lun->io_lock); 713267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 714267877Smav beio->ds_tag_type, beio->ds_trans_type, 715267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 716267877Smav mtx_unlock(&be_lun->io_lock); 717267877Smav 718229997Sken /* 719229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 720229997Sken * return the I/O to the user. 721229997Sken */ 722229997Sken if (error != 0) { 723229997Sken char path_str[32]; 724229997Sken 725229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 726229997Sken printf("%s%s command returned errno %d\n", path_str, 727229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 728273809Smav if (error == ENOSPC) { 729273809Smav ctl_set_space_alloc_fail(&io->scsiio); 730273809Smav } else 731273809Smav ctl_set_medium_error(&io->scsiio); 732229997Sken ctl_complete_beio(beio); 733229997Sken return; 734229997Sken } 735229997Sken 736229997Sken /* 737269122Smav * If this is a write or a verify, we're all done. 738229997Sken * If this is a read, we can now send the data to the user. 739229997Sken */ 740269122Smav if ((beio->bio_cmd == BIO_WRITE) || 741269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 742229997Sken ctl_set_success(&io->scsiio); 743229997Sken ctl_complete_beio(beio); 744229997Sken } else { 745275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 746275058Smav beio->beio_cont == NULL) 747275058Smav ctl_set_success(&io->scsiio); 748229997Sken#ifdef CTL_TIME_IO 749229997Sken getbintime(&io->io_hdr.dma_start_bt); 750229997Sken#endif 751229997Sken ctl_datamove(io); 752229997Sken } 753229997Sken} 754229997Sken 755229997Skenstatic void 756269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 757269123Smav struct ctl_be_block_io *beio) 758269123Smav{ 759269123Smav struct ctl_be_block_devdata *dev_data; 760269123Smav union ctl_io *io; 761269123Smav struct uio xuio; 762269123Smav struct iovec *xiovec; 763269123Smav int flags; 764269123Smav int error, i; 765269123Smav 766269123Smav DPRINTF("entered\n"); 767269123Smav 768269123Smav dev_data = &be_lun->backend.dev; 769269123Smav io = beio->io; 770271309Smav flags = 0; 771271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 772271309Smav flags |= IO_DIRECT; 773271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 774271309Smav flags |= IO_SYNC; 775269123Smav 776269123Smav bzero(&xuio, sizeof(xuio)); 777269123Smav if (beio->bio_cmd == BIO_READ) { 778269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 779269123Smav xuio.uio_rw = UIO_READ; 780269123Smav } else { 781269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 782269123Smav xuio.uio_rw = UIO_WRITE; 783269123Smav } 784269123Smav xuio.uio_offset = beio->io_offset; 785269123Smav xuio.uio_resid = beio->io_len; 786269123Smav xuio.uio_segflg = UIO_SYSSPACE; 787269123Smav xuio.uio_iov = beio->xiovecs; 788269123Smav xuio.uio_iovcnt = beio->num_segs; 789269123Smav xuio.uio_td = curthread; 790269123Smav 791269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 792269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 793269123Smav xiovec->iov_len = beio->sg_segs[i].len; 794269123Smav } 795269123Smav 796269123Smav binuptime(&beio->ds_t0); 797269123Smav mtx_lock(&be_lun->io_lock); 798269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 799269123Smav mtx_unlock(&be_lun->io_lock); 800269123Smav 801269123Smav if (beio->bio_cmd == BIO_READ) { 802271309Smav error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 803269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 804269123Smav } else { 805271309Smav error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 806269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 807269123Smav } 808269123Smav 809269123Smav mtx_lock(&be_lun->io_lock); 810269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 811269123Smav beio->ds_tag_type, beio->ds_trans_type, 812269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 813269123Smav mtx_unlock(&be_lun->io_lock); 814269123Smav 815269123Smav /* 816269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 817269123Smav * return the I/O to the user. 818269123Smav */ 819269123Smav if (error != 0) { 820273809Smav if (error == ENOSPC) { 821273809Smav ctl_set_space_alloc_fail(&io->scsiio); 822273809Smav } else 823273809Smav ctl_set_medium_error(&io->scsiio); 824269123Smav ctl_complete_beio(beio); 825269123Smav return; 826269123Smav } 827269123Smav 828269123Smav /* 829269123Smav * If this is a write or a verify, we're all done. 830269123Smav * If this is a read, we can now send the data to the user. 831269123Smav */ 832269123Smav if ((beio->bio_cmd == BIO_WRITE) || 833269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 834269123Smav ctl_set_success(&io->scsiio); 835269123Smav ctl_complete_beio(beio); 836269123Smav } else { 837275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 838275058Smav beio->beio_cont == NULL) 839275058Smav ctl_set_success(&io->scsiio); 840269123Smav#ifdef CTL_TIME_IO 841269123Smav getbintime(&io->io_hdr.dma_start_bt); 842269123Smav#endif 843269123Smav ctl_datamove(io); 844269123Smav } 845269123Smav} 846269123Smav 847269123Smavstatic void 848229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 849229997Sken struct ctl_be_block_io *beio) 850229997Sken{ 851229997Sken struct bio *bio; 852229997Sken union ctl_io *io; 853229997Sken struct ctl_be_block_devdata *dev_data; 854229997Sken 855229997Sken dev_data = &be_lun->backend.dev; 856229997Sken io = beio->io; 857229997Sken 858229997Sken DPRINTF("entered\n"); 859229997Sken 860229997Sken /* This can't fail, it's a blocking allocation. */ 861229997Sken bio = g_alloc_bio(); 862229997Sken 863229997Sken bio->bio_cmd = BIO_FLUSH; 864229997Sken bio->bio_flags |= BIO_ORDERED; 865229997Sken bio->bio_dev = dev_data->cdev; 866229997Sken bio->bio_offset = 0; 867229997Sken bio->bio_data = 0; 868229997Sken bio->bio_done = ctl_be_block_biodone; 869229997Sken bio->bio_caller1 = beio; 870229997Sken bio->bio_pblkno = 0; 871229997Sken 872229997Sken /* 873229997Sken * We don't need to acquire the LUN lock here, because we are only 874229997Sken * sending one bio, and so there is no other context to synchronize 875229997Sken * with. 876229997Sken */ 877229997Sken beio->num_bios_sent = 1; 878229997Sken beio->send_complete = 1; 879229997Sken 880229997Sken binuptime(&beio->ds_t0); 881267877Smav mtx_lock(&be_lun->io_lock); 882229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 883267877Smav mtx_unlock(&be_lun->io_lock); 884229997Sken 885229997Sken (*dev_data->csw->d_strategy)(bio); 886229997Sken} 887229997Sken 888229997Skenstatic void 889264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 890264274Smav struct ctl_be_block_io *beio, 891264274Smav uint64_t off, uint64_t len, int last) 892264274Smav{ 893264274Smav struct bio *bio; 894264274Smav struct ctl_be_block_devdata *dev_data; 895264296Smav uint64_t maxlen; 896264274Smav 897264274Smav dev_data = &be_lun->backend.dev; 898264296Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 899264274Smav while (len > 0) { 900264274Smav bio = g_alloc_bio(); 901264274Smav bio->bio_cmd = BIO_DELETE; 902264274Smav bio->bio_dev = dev_data->cdev; 903264274Smav bio->bio_offset = off; 904264296Smav bio->bio_length = MIN(len, maxlen); 905264274Smav bio->bio_data = 0; 906264274Smav bio->bio_done = ctl_be_block_biodone; 907264274Smav bio->bio_caller1 = beio; 908264296Smav bio->bio_pblkno = off / be_lun->blocksize; 909264274Smav 910264274Smav off += bio->bio_length; 911264274Smav len -= bio->bio_length; 912264274Smav 913267877Smav mtx_lock(&be_lun->io_lock); 914264274Smav beio->num_bios_sent++; 915264274Smav if (last && len == 0) 916264274Smav beio->send_complete = 1; 917267877Smav mtx_unlock(&be_lun->io_lock); 918264274Smav 919264274Smav (*dev_data->csw->d_strategy)(bio); 920264274Smav } 921264274Smav} 922264274Smav 923264274Smavstatic void 924264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 925264274Smav struct ctl_be_block_io *beio) 926264274Smav{ 927264274Smav union ctl_io *io; 928264274Smav struct ctl_be_block_devdata *dev_data; 929267515Smav struct ctl_ptr_len_flags *ptrlen; 930264274Smav struct scsi_unmap_desc *buf, *end; 931264274Smav uint64_t len; 932264274Smav 933264274Smav dev_data = &be_lun->backend.dev; 934264274Smav io = beio->io; 935264274Smav 936264274Smav DPRINTF("entered\n"); 937264274Smav 938264274Smav binuptime(&beio->ds_t0); 939267877Smav mtx_lock(&be_lun->io_lock); 940264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 941267877Smav mtx_unlock(&be_lun->io_lock); 942264274Smav 943264274Smav if (beio->io_offset == -1) { 944264274Smav beio->io_len = 0; 945267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 946267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 947267515Smav end = buf + ptrlen->len / sizeof(*buf); 948264274Smav for (; buf < end; buf++) { 949264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 950264274Smav be_lun->blocksize; 951264274Smav beio->io_len += len; 952264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 953264274Smav scsi_8btou64(buf->lba) * be_lun->blocksize, len, 954264283Smav (end - buf < 2) ? TRUE : FALSE); 955264274Smav } 956264274Smav } else 957264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 958264274Smav beio->io_offset, beio->io_len, TRUE); 959264274Smav} 960264274Smav 961264274Smavstatic void 962229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 963229997Sken struct ctl_be_block_io *beio) 964229997Sken{ 965267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 966229997Sken int i; 967229997Sken struct bio *bio; 968229997Sken struct ctl_be_block_devdata *dev_data; 969229997Sken off_t cur_offset; 970229997Sken int max_iosize; 971229997Sken 972229997Sken DPRINTF("entered\n"); 973229997Sken 974229997Sken dev_data = &be_lun->backend.dev; 975229997Sken 976229997Sken /* 977229997Sken * We have to limit our I/O size to the maximum supported by the 978229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 979229997Sken * set it properly, use DFLTPHYS. 980229997Sken */ 981229997Sken max_iosize = dev_data->cdev->si_iosize_max; 982229997Sken if (max_iosize < PAGE_SIZE) 983229997Sken max_iosize = DFLTPHYS; 984229997Sken 985229997Sken cur_offset = beio->io_offset; 986229997Sken for (i = 0; i < beio->num_segs; i++) { 987229997Sken size_t cur_size; 988229997Sken uint8_t *cur_ptr; 989229997Sken 990229997Sken cur_size = beio->sg_segs[i].len; 991229997Sken cur_ptr = beio->sg_segs[i].addr; 992229997Sken 993229997Sken while (cur_size > 0) { 994229997Sken /* This can't fail, it's a blocking allocation. */ 995229997Sken bio = g_alloc_bio(); 996229997Sken 997229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 998229997Sken 999229997Sken bio->bio_cmd = beio->bio_cmd; 1000229997Sken bio->bio_dev = dev_data->cdev; 1001229997Sken bio->bio_caller1 = beio; 1002229997Sken bio->bio_length = min(cur_size, max_iosize); 1003229997Sken bio->bio_offset = cur_offset; 1004229997Sken bio->bio_data = cur_ptr; 1005229997Sken bio->bio_done = ctl_be_block_biodone; 1006229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 1007229997Sken 1008229997Sken cur_offset += bio->bio_length; 1009229997Sken cur_ptr += bio->bio_length; 1010229997Sken cur_size -= bio->bio_length; 1011229997Sken 1012267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1013229997Sken beio->num_bios_sent++; 1014229997Sken } 1015229997Sken } 1016267877Smav binuptime(&beio->ds_t0); 1017267877Smav mtx_lock(&be_lun->io_lock); 1018267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1019267877Smav beio->send_complete = 1; 1020267877Smav mtx_unlock(&be_lun->io_lock); 1021267877Smav 1022267877Smav /* 1023267877Smav * Fire off all allocated requests! 1024267877Smav */ 1025267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1026267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1027267877Smav (*dev_data->csw->d_strategy)(bio); 1028267877Smav } 1029229997Sken} 1030229997Sken 1031274154Smavstatic uint64_t 1032274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1033274154Smav{ 1034274154Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1035274154Smav struct diocgattr_arg arg; 1036274154Smav int error; 1037274154Smav 1038274154Smav if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1039274154Smav return (UINT64_MAX); 1040274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1041274154Smav arg.len = sizeof(arg.value.off); 1042274154Smav error = dev_data->csw->d_ioctl(dev_data->cdev, 1043274154Smav DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1044274154Smav if (error != 0) 1045274154Smav return (UINT64_MAX); 1046274154Smav return (arg.value.off); 1047274154Smav} 1048274154Smav 1049229997Skenstatic void 1050264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1051264274Smav{ 1052264274Smav union ctl_io *io; 1053264274Smav 1054264274Smav io = beio->io; 1055264274Smav ctl_free_beio(beio); 1056267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1057267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1058267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1059264274Smav ctl_config_write_done(io); 1060264274Smav return; 1061264274Smav } 1062264274Smav 1063264274Smav ctl_be_block_config_write(io); 1064264274Smav} 1065264274Smav 1066264274Smavstatic void 1067264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1068264274Smav union ctl_io *io) 1069264274Smav{ 1070264274Smav struct ctl_be_block_io *beio; 1071264274Smav struct ctl_be_block_softc *softc; 1072267515Smav struct ctl_lba_len_flags *lbalen; 1073264274Smav uint64_t len_left, lba; 1074264274Smav int i, seglen; 1075264274Smav uint8_t *buf, *end; 1076264274Smav 1077264274Smav DPRINTF("entered\n"); 1078264274Smav 1079267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1080264274Smav softc = be_lun->softc; 1081267537Smav lbalen = ARGS(beio->io); 1082264274Smav 1083271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1084269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1085264274Smav ctl_free_beio(beio); 1086264274Smav ctl_set_invalid_field(&io->scsiio, 1087264274Smav /*sks_valid*/ 1, 1088264274Smav /*command*/ 1, 1089264274Smav /*field*/ 1, 1090264274Smav /*bit_valid*/ 0, 1091264274Smav /*bit*/ 0); 1092264274Smav ctl_config_write_done(io); 1093264274Smav return; 1094264274Smav } 1095264274Smav 1096264274Smav switch (io->scsiio.tag_type) { 1097264274Smav case CTL_TAG_ORDERED: 1098264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1099264274Smav break; 1100264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1101264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1102264274Smav break; 1103264274Smav case CTL_TAG_UNTAGGED: 1104264274Smav case CTL_TAG_SIMPLE: 1105264274Smav case CTL_TAG_ACA: 1106264274Smav default: 1107264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1108264274Smav break; 1109264274Smav } 1110264274Smav 1111269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1112267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1113267515Smav beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1114264274Smav beio->bio_cmd = BIO_DELETE; 1115264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1116264274Smav 1117264274Smav be_lun->unmap(be_lun, beio); 1118264274Smav return; 1119264274Smav } 1120264274Smav 1121264274Smav beio->bio_cmd = BIO_WRITE; 1122264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1123264274Smav 1124264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1125267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1126264274Smav 1127267515Smav len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1128264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1129264274Smav 1130264274Smav /* 1131264274Smav * Setup the S/G entry for this chunk. 1132264274Smav */ 1133264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1134264274Smav seglen -= seglen % be_lun->blocksize; 1135264274Smav beio->sg_segs[i].len = seglen; 1136264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1137264274Smav 1138264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1139264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1140264274Smav 1141264274Smav beio->num_segs++; 1142264274Smav len_left -= seglen; 1143264274Smav 1144264274Smav buf = beio->sg_segs[i].addr; 1145264274Smav end = buf + seglen; 1146264274Smav for (; buf < end; buf += be_lun->blocksize) { 1147264274Smav memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1148267515Smav if (lbalen->flags & SWS_LBDATA) 1149267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1150264274Smav lba++; 1151264274Smav } 1152264274Smav } 1153264274Smav 1154267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1155264274Smav beio->io_len = lba * be_lun->blocksize; 1156264274Smav 1157264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1158264274Smav if (len_left > 0) { 1159267515Smav lbalen->lba += lba; 1160267515Smav lbalen->len -= lba; 1161264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1162264274Smav } 1163264274Smav 1164264274Smav be_lun->dispatch(be_lun, beio); 1165264274Smav} 1166264274Smav 1167264274Smavstatic void 1168264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1169264274Smav union ctl_io *io) 1170264274Smav{ 1171264274Smav struct ctl_be_block_io *beio; 1172264274Smav struct ctl_be_block_softc *softc; 1173267515Smav struct ctl_ptr_len_flags *ptrlen; 1174264274Smav 1175264274Smav DPRINTF("entered\n"); 1176264274Smav 1177267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1178264274Smav softc = be_lun->softc; 1179267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1180264274Smav 1181269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1182264274Smav ctl_free_beio(beio); 1183264274Smav ctl_set_invalid_field(&io->scsiio, 1184264274Smav /*sks_valid*/ 0, 1185264274Smav /*command*/ 1, 1186264274Smav /*field*/ 0, 1187264274Smav /*bit_valid*/ 0, 1188264274Smav /*bit*/ 0); 1189264274Smav ctl_config_write_done(io); 1190264274Smav return; 1191264274Smav } 1192264274Smav 1193264274Smav switch (io->scsiio.tag_type) { 1194264274Smav case CTL_TAG_ORDERED: 1195264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1196264274Smav break; 1197264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1198264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1199264274Smav break; 1200264274Smav case CTL_TAG_UNTAGGED: 1201264274Smav case CTL_TAG_SIMPLE: 1202264274Smav case CTL_TAG_ACA: 1203264274Smav default: 1204264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1205264274Smav break; 1206264274Smav } 1207264274Smav 1208264274Smav beio->io_len = 0; 1209264274Smav beio->io_offset = -1; 1210264274Smav 1211264274Smav beio->bio_cmd = BIO_DELETE; 1212264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1213264274Smav 1214267515Smav DPRINTF("UNMAP\n"); 1215264274Smav 1216264274Smav be_lun->unmap(be_lun, beio); 1217264274Smav} 1218264274Smav 1219264274Smavstatic void 1220264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1221264274Smav{ 1222264274Smav union ctl_io *io; 1223264274Smav 1224264274Smav io = beio->io; 1225264274Smav ctl_free_beio(beio); 1226264274Smav ctl_config_write_done(io); 1227264274Smav} 1228264274Smav 1229264274Smavstatic void 1230229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1231229997Sken union ctl_io *io) 1232229997Sken{ 1233229997Sken struct ctl_be_block_io *beio; 1234229997Sken struct ctl_be_block_softc *softc; 1235229997Sken 1236229997Sken DPRINTF("entered\n"); 1237229997Sken 1238229997Sken softc = be_lun->softc; 1239229997Sken beio = ctl_alloc_beio(softc); 1240229997Sken beio->io = io; 1241229997Sken beio->lun = be_lun; 1242264274Smav beio->beio_cont = ctl_be_block_cw_done; 1243267519Smav PRIV(io)->ptr = (void *)beio; 1244229997Sken 1245229997Sken switch (io->scsiio.cdb[0]) { 1246229997Sken case SYNCHRONIZE_CACHE: 1247229997Sken case SYNCHRONIZE_CACHE_16: 1248249194Strasz beio->bio_cmd = BIO_FLUSH; 1249229997Sken beio->ds_trans_type = DEVSTAT_NO_DATA; 1250229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1251229997Sken beio->io_len = 0; 1252229997Sken be_lun->lun_flush(be_lun, beio); 1253229997Sken break; 1254264274Smav case WRITE_SAME_10: 1255264274Smav case WRITE_SAME_16: 1256264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1257264274Smav break; 1258264274Smav case UNMAP: 1259264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1260264274Smav break; 1261229997Sken default: 1262229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1263229997Sken break; 1264229997Sken } 1265229997Sken} 1266229997Sken 1267258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1268258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1269258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1270258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1271229997Sken 1272229997Skenstatic void 1273264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1274264886Smav{ 1275264886Smav struct ctl_be_block_lun *be_lun; 1276264886Smav union ctl_io *io; 1277264886Smav 1278264886Smav io = beio->io; 1279264886Smav be_lun = beio->lun; 1280264886Smav ctl_free_beio(beio); 1281267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1282267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1283267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1284267537Smav ctl_data_submit_done(io); 1285264886Smav return; 1286264886Smav } 1287264886Smav 1288264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1289264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1290264886Smav 1291267877Smav mtx_lock(&be_lun->queue_lock); 1292264886Smav /* 1293264886Smav * XXX KDM make sure that links is okay to use at this point. 1294264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1295264886Smav * or deal with resource allocation here. 1296264886Smav */ 1297264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1298267877Smav mtx_unlock(&be_lun->queue_lock); 1299264886Smav 1300264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1301264886Smav} 1302264886Smav 1303264886Smavstatic void 1304229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1305229997Sken union ctl_io *io) 1306229997Sken{ 1307229997Sken struct ctl_be_block_io *beio; 1308229997Sken struct ctl_be_block_softc *softc; 1309267537Smav struct ctl_lba_len_flags *lbalen; 1310267519Smav struct ctl_ptr_len_flags *bptrlen; 1311267519Smav uint64_t len_left, lbas; 1312229997Sken int i; 1313229997Sken 1314229997Sken softc = be_lun->softc; 1315229997Sken 1316229997Sken DPRINTF("entered\n"); 1317229997Sken 1318267537Smav lbalen = ARGS(io); 1319267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1320267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1321267537Smav } else { 1322229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1323229997Sken } 1324229997Sken 1325229997Sken beio = ctl_alloc_beio(softc); 1326229997Sken beio->io = io; 1327229997Sken beio->lun = be_lun; 1328267519Smav bptrlen = PRIV(io); 1329267519Smav bptrlen->ptr = (void *)beio; 1330229997Sken 1331229997Sken switch (io->scsiio.tag_type) { 1332229997Sken case CTL_TAG_ORDERED: 1333229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1334229997Sken break; 1335229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1336229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1337229997Sken break; 1338229997Sken case CTL_TAG_UNTAGGED: 1339229997Sken case CTL_TAG_SIMPLE: 1340229997Sken case CTL_TAG_ACA: 1341229997Sken default: 1342229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1343229997Sken break; 1344229997Sken } 1345229997Sken 1346267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1347267537Smav beio->bio_cmd = BIO_WRITE; 1348267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1349267537Smav } else { 1350229997Sken beio->bio_cmd = BIO_READ; 1351229997Sken beio->ds_trans_type = DEVSTAT_READ; 1352229997Sken } 1353229997Sken 1354264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1355229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1356267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1357267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1358267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1359267537Smav else 1360267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1361267537Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1362267519Smav beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1363267519Smav beio->io_len = lbas * be_lun->blocksize; 1364267519Smav bptrlen->len += lbas; 1365229997Sken 1366264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1367264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1368264886Smav i, CTLBLK_MAX_SEGS)); 1369229997Sken 1370229997Sken /* 1371229997Sken * Setup the S/G entry for this chunk. 1372229997Sken */ 1373264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1374229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1375229997Sken 1376229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1377229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1378229997Sken 1379267537Smav /* Set up second segment for compare operation. */ 1380267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1381267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1382267537Smav beio->sg_segs[i].len; 1383267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1384267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1385267537Smav } 1386267537Smav 1387229997Sken beio->num_segs++; 1388229997Sken len_left -= beio->sg_segs[i].len; 1389229997Sken } 1390267519Smav if (bptrlen->len < lbalen->len) 1391264886Smav beio->beio_cont = ctl_be_block_next; 1392264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1393267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1394267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1395267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1396267537Smav else 1397267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1398264886Smav io->scsiio.kern_data_len = beio->io_len; 1399264886Smav io->scsiio.kern_data_resid = 0; 1400264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1401264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1402229997Sken 1403229997Sken /* 1404229997Sken * For the read case, we need to read the data into our buffers and 1405229997Sken * then we can send it back to the user. For the write case, we 1406229997Sken * need to get the data from the user first. 1407229997Sken */ 1408229997Sken if (beio->bio_cmd == BIO_READ) { 1409229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1410229997Sken be_lun->dispatch(be_lun, beio); 1411229997Sken } else { 1412229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1413229997Sken#ifdef CTL_TIME_IO 1414229997Sken getbintime(&io->io_hdr.dma_start_bt); 1415229997Sken#endif 1416229997Sken ctl_datamove(io); 1417229997Sken } 1418229997Sken} 1419229997Sken 1420229997Skenstatic void 1421229997Skenctl_be_block_worker(void *context, int pending) 1422229997Sken{ 1423229997Sken struct ctl_be_block_lun *be_lun; 1424229997Sken struct ctl_be_block_softc *softc; 1425229997Sken union ctl_io *io; 1426229997Sken 1427229997Sken be_lun = (struct ctl_be_block_lun *)context; 1428229997Sken softc = be_lun->softc; 1429229997Sken 1430229997Sken DPRINTF("entered\n"); 1431229997Sken 1432267877Smav mtx_lock(&be_lun->queue_lock); 1433229997Sken for (;;) { 1434229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1435229997Sken if (io != NULL) { 1436229997Sken struct ctl_be_block_io *beio; 1437229997Sken 1438229997Sken DPRINTF("datamove queue\n"); 1439229997Sken 1440229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1441229997Sken ctl_io_hdr, links); 1442229997Sken 1443267877Smav mtx_unlock(&be_lun->queue_lock); 1444229997Sken 1445267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1446229997Sken 1447229997Sken be_lun->dispatch(be_lun, beio); 1448229997Sken 1449267877Smav mtx_lock(&be_lun->queue_lock); 1450229997Sken continue; 1451229997Sken } 1452229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1453229997Sken if (io != NULL) { 1454229997Sken 1455229997Sken DPRINTF("config write queue\n"); 1456229997Sken 1457229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1458229997Sken ctl_io_hdr, links); 1459229997Sken 1460267877Smav mtx_unlock(&be_lun->queue_lock); 1461229997Sken 1462229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1463229997Sken 1464267877Smav mtx_lock(&be_lun->queue_lock); 1465229997Sken continue; 1466229997Sken } 1467229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1468229997Sken if (io != NULL) { 1469229997Sken DPRINTF("input queue\n"); 1470229997Sken 1471229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1472229997Sken ctl_io_hdr, links); 1473267877Smav mtx_unlock(&be_lun->queue_lock); 1474229997Sken 1475229997Sken /* 1476229997Sken * We must drop the lock, since this routine and 1477229997Sken * its children may sleep. 1478229997Sken */ 1479229997Sken ctl_be_block_dispatch(be_lun, io); 1480229997Sken 1481267877Smav mtx_lock(&be_lun->queue_lock); 1482229997Sken continue; 1483229997Sken } 1484229997Sken 1485229997Sken /* 1486229997Sken * If we get here, there is no work left in the queues, so 1487229997Sken * just break out and let the task queue go to sleep. 1488229997Sken */ 1489229997Sken break; 1490229997Sken } 1491267877Smav mtx_unlock(&be_lun->queue_lock); 1492229997Sken} 1493229997Sken 1494229997Sken/* 1495229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1496229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1497229997Sken * thread. 1498229997Sken */ 1499229997Skenstatic int 1500229997Skenctl_be_block_submit(union ctl_io *io) 1501229997Sken{ 1502229997Sken struct ctl_be_block_lun *be_lun; 1503229997Sken struct ctl_be_lun *ctl_be_lun; 1504229997Sken 1505229997Sken DPRINTF("entered\n"); 1506229997Sken 1507229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1508229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1509229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1510229997Sken 1511229997Sken /* 1512229997Sken * Make sure we only get SCSI I/O. 1513229997Sken */ 1514229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1515229997Sken "%#x) encountered", io->io_hdr.io_type)); 1516229997Sken 1517267519Smav PRIV(io)->len = 0; 1518267519Smav 1519267877Smav mtx_lock(&be_lun->queue_lock); 1520229997Sken /* 1521229997Sken * XXX KDM make sure that links is okay to use at this point. 1522229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1523229997Sken * or deal with resource allocation here. 1524229997Sken */ 1525229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1526267877Smav mtx_unlock(&be_lun->queue_lock); 1527229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1528229997Sken 1529267514Smav return (CTL_RETVAL_COMPLETE); 1530229997Sken} 1531229997Sken 1532229997Skenstatic int 1533229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1534229997Sken int flag, struct thread *td) 1535229997Sken{ 1536229997Sken struct ctl_be_block_softc *softc; 1537229997Sken int error; 1538229997Sken 1539229997Sken softc = &backend_block_softc; 1540229997Sken 1541229997Sken error = 0; 1542229997Sken 1543229997Sken switch (cmd) { 1544229997Sken case CTL_LUN_REQ: { 1545229997Sken struct ctl_lun_req *lun_req; 1546229997Sken 1547229997Sken lun_req = (struct ctl_lun_req *)addr; 1548229997Sken 1549229997Sken switch (lun_req->reqtype) { 1550229997Sken case CTL_LUNREQ_CREATE: 1551229997Sken error = ctl_be_block_create(softc, lun_req); 1552229997Sken break; 1553229997Sken case CTL_LUNREQ_RM: 1554229997Sken error = ctl_be_block_rm(softc, lun_req); 1555229997Sken break; 1556232604Strasz case CTL_LUNREQ_MODIFY: 1557232604Strasz error = ctl_be_block_modify(softc, lun_req); 1558232604Strasz break; 1559229997Sken default: 1560229997Sken lun_req->status = CTL_LUN_ERROR; 1561229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1562272911Smav "invalid LUN request type %d", 1563229997Sken lun_req->reqtype); 1564229997Sken break; 1565229997Sken } 1566229997Sken break; 1567229997Sken } 1568229997Sken default: 1569229997Sken error = ENOTTY; 1570229997Sken break; 1571229997Sken } 1572229997Sken 1573229997Sken return (error); 1574229997Sken} 1575229997Sken 1576229997Skenstatic int 1577229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1578229997Sken{ 1579229997Sken struct ctl_be_block_filedata *file_data; 1580229997Sken struct ctl_lun_create_params *params; 1581229997Sken struct vattr vattr; 1582273029Smav off_t pss; 1583229997Sken int error; 1584229997Sken 1585229997Sken error = 0; 1586229997Sken file_data = &be_lun->backend.file; 1587272911Smav params = &be_lun->params; 1588229997Sken 1589229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1590229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1591229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1592229997Sken 1593229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1594229997Sken if (error != 0) { 1595229997Sken snprintf(req->error_str, sizeof(req->error_str), 1596229997Sken "error calling VOP_GETATTR() for file %s", 1597229997Sken be_lun->dev_path); 1598229997Sken return (error); 1599229997Sken } 1600229997Sken 1601229997Sken /* 1602229997Sken * Verify that we have the ability to upgrade to exclusive 1603229997Sken * access on this file so we can trap errors at open instead 1604229997Sken * of reporting them during first access. 1605229997Sken */ 1606229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1607229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1608229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1609229997Sken error = EBADF; 1610229997Sken snprintf(req->error_str, sizeof(req->error_str), 1611229997Sken "error locking file %s", be_lun->dev_path); 1612229997Sken return (error); 1613229997Sken } 1614229997Sken } 1615229997Sken 1616229997Sken 1617229997Sken file_data->cred = crhold(curthread->td_ucred); 1618232604Strasz if (params->lun_size_bytes != 0) 1619232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1620232604Strasz else 1621232604Strasz be_lun->size_bytes = vattr.va_size; 1622229997Sken /* 1623229997Sken * We set the multi thread flag for file operations because all 1624229997Sken * filesystems (in theory) are capable of allowing multiple readers 1625229997Sken * of a file at once. So we want to get the maximum possible 1626229997Sken * concurrency. 1627229997Sken */ 1628229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1629229997Sken 1630229997Sken /* 1631273029Smav * For files we can use any logical block size. Prefer 512 bytes 1632273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1633273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1634273029Smav * logical block size -- report it as physical block size. 1635229997Sken */ 1636229997Sken if (params->blocksize_bytes != 0) 1637229997Sken be_lun->blocksize = params->blocksize_bytes; 1638229997Sken else 1639229997Sken be_lun->blocksize = 512; 1640273029Smav pss = vattr.va_blocksize / be_lun->blocksize; 1641273029Smav if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) && 1642273029Smav ((pss & (pss - 1)) == 0)) { 1643273029Smav be_lun->pblockexp = fls(pss) - 1; 1644273029Smav be_lun->pblockoff = 0; 1645273029Smav } 1646229997Sken 1647229997Sken /* 1648229997Sken * Sanity check. The media size has to be at least one 1649229997Sken * sector long. 1650229997Sken */ 1651229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1652229997Sken error = EINVAL; 1653229997Sken snprintf(req->error_str, sizeof(req->error_str), 1654229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1655229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1656229997Sken } 1657229997Sken return (error); 1658229997Sken} 1659229997Sken 1660229997Skenstatic int 1661229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1662229997Sken{ 1663229997Sken struct ctl_lun_create_params *params; 1664229997Sken struct vattr vattr; 1665229997Sken struct cdev *dev; 1666229997Sken struct cdevsw *devsw; 1667229997Sken int error; 1668264191Smav off_t ps, pss, po, pos; 1669229997Sken 1670272911Smav params = &be_lun->params; 1671229997Sken 1672229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1673229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1674229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1675229997Sken &be_lun->backend.dev.dev_ref); 1676229997Sken if (be_lun->backend.dev.csw == NULL) 1677229997Sken panic("Unable to retrieve device switch"); 1678269123Smav if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) 1679269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1680269123Smav else 1681269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1682269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1683269123Smav be_lun->unmap = ctl_be_block_unmap_dev; 1684274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1685229997Sken 1686229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1687229997Sken if (error) { 1688229997Sken snprintf(req->error_str, sizeof(req->error_str), 1689272911Smav "error getting vnode attributes for device %s", 1690272911Smav be_lun->dev_path); 1691229997Sken return (error); 1692229997Sken } 1693229997Sken 1694229997Sken dev = be_lun->vn->v_rdev; 1695229997Sken devsw = dev->si_devsw; 1696229997Sken if (!devsw->d_ioctl) { 1697229997Sken snprintf(req->error_str, sizeof(req->error_str), 1698272911Smav "no d_ioctl for device %s!", 1699229997Sken be_lun->dev_path); 1700229997Sken return (ENODEV); 1701229997Sken } 1702229997Sken 1703229997Sken error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1704229997Sken (caddr_t)&be_lun->blocksize, FREAD, 1705229997Sken curthread); 1706229997Sken if (error) { 1707229997Sken snprintf(req->error_str, sizeof(req->error_str), 1708272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1709272911Smav "on %s!", error, be_lun->dev_path); 1710229997Sken return (error); 1711229997Sken } 1712229997Sken 1713229997Sken /* 1714229997Sken * If the user has asked for a blocksize that is greater than the 1715229997Sken * backing device's blocksize, we can do it only if the blocksize 1716229997Sken * the user is asking for is an even multiple of the underlying 1717229997Sken * device's blocksize. 1718229997Sken */ 1719229997Sken if ((params->blocksize_bytes != 0) 1720229997Sken && (params->blocksize_bytes > be_lun->blocksize)) { 1721229997Sken uint32_t bs_multiple, tmp_blocksize; 1722229997Sken 1723229997Sken bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1724229997Sken 1725229997Sken tmp_blocksize = bs_multiple * be_lun->blocksize; 1726229997Sken 1727229997Sken if (tmp_blocksize == params->blocksize_bytes) { 1728229997Sken be_lun->blocksize = params->blocksize_bytes; 1729229997Sken } else { 1730229997Sken snprintf(req->error_str, sizeof(req->error_str), 1731272911Smav "requested blocksize %u is not an even " 1732229997Sken "multiple of backing device blocksize %u", 1733272911Smav params->blocksize_bytes, 1734229997Sken be_lun->blocksize); 1735229997Sken return (EINVAL); 1736229997Sken 1737229997Sken } 1738229997Sken } else if ((params->blocksize_bytes != 0) 1739229997Sken && (params->blocksize_bytes != be_lun->blocksize)) { 1740229997Sken snprintf(req->error_str, sizeof(req->error_str), 1741272911Smav "requested blocksize %u < backing device " 1742272911Smav "blocksize %u", params->blocksize_bytes, 1743229997Sken be_lun->blocksize); 1744229997Sken return (EINVAL); 1745229997Sken } 1746229997Sken 1747229997Sken error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1748229997Sken (caddr_t)&be_lun->size_bytes, FREAD, 1749229997Sken curthread); 1750229997Sken if (error) { 1751229997Sken snprintf(req->error_str, sizeof(req->error_str), 1752272911Smav "error %d returned for DIOCGMEDIASIZE " 1753272911Smav " ioctl on %s!", error, 1754232604Strasz be_lun->dev_path); 1755229997Sken return (error); 1756229997Sken } 1757229997Sken 1758232604Strasz if (params->lun_size_bytes != 0) { 1759232604Strasz if (params->lun_size_bytes > be_lun->size_bytes) { 1760232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1761272911Smav "requested LUN size %ju > backing device " 1762272911Smav "size %ju", 1763232604Strasz (uintmax_t)params->lun_size_bytes, 1764232604Strasz (uintmax_t)be_lun->size_bytes); 1765232604Strasz return (EINVAL); 1766232604Strasz } 1767232604Strasz 1768232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1769232604Strasz } 1770232604Strasz 1771264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1772264191Smav (caddr_t)&ps, FREAD, curthread); 1773264191Smav if (error) 1774264191Smav ps = po = 0; 1775264191Smav else { 1776264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1777264191Smav (caddr_t)&po, FREAD, curthread); 1778264191Smav if (error) 1779264191Smav po = 0; 1780264191Smav } 1781264191Smav pss = ps / be_lun->blocksize; 1782264191Smav pos = po / be_lun->blocksize; 1783264191Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1784264191Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1785264191Smav be_lun->pblockexp = fls(pss) - 1; 1786264191Smav be_lun->pblockoff = (pss - pos) % pss; 1787264191Smav } 1788264191Smav 1789229997Sken return (0); 1790229997Sken} 1791229997Sken 1792229997Skenstatic int 1793229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 1794229997Sken{ 1795229997Sken DROP_GIANT(); 1796229997Sken if (be_lun->vn) { 1797229997Sken int flags = FREAD | FWRITE; 1798229997Sken 1799229997Sken switch (be_lun->dev_type) { 1800229997Sken case CTL_BE_BLOCK_DEV: 1801229997Sken if (be_lun->backend.dev.csw) { 1802229997Sken dev_relthread(be_lun->backend.dev.cdev, 1803229997Sken be_lun->backend.dev.dev_ref); 1804229997Sken be_lun->backend.dev.csw = NULL; 1805229997Sken be_lun->backend.dev.cdev = NULL; 1806229997Sken } 1807229997Sken break; 1808229997Sken case CTL_BE_BLOCK_FILE: 1809229997Sken break; 1810229997Sken case CTL_BE_BLOCK_NONE: 1811258871Strasz break; 1812229997Sken default: 1813229997Sken panic("Unexpected backend type."); 1814229997Sken break; 1815229997Sken } 1816229997Sken 1817229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1818229997Sken be_lun->vn = NULL; 1819229997Sken 1820229997Sken switch (be_lun->dev_type) { 1821229997Sken case CTL_BE_BLOCK_DEV: 1822229997Sken break; 1823229997Sken case CTL_BE_BLOCK_FILE: 1824229997Sken if (be_lun->backend.file.cred != NULL) { 1825229997Sken crfree(be_lun->backend.file.cred); 1826229997Sken be_lun->backend.file.cred = NULL; 1827229997Sken } 1828229997Sken break; 1829229997Sken case CTL_BE_BLOCK_NONE: 1830258871Strasz break; 1831229997Sken default: 1832229997Sken panic("Unexpected backend type."); 1833229997Sken break; 1834229997Sken } 1835272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 1836229997Sken } 1837229997Sken PICKUP_GIANT(); 1838229997Sken 1839229997Sken return (0); 1840229997Sken} 1841229997Sken 1842229997Skenstatic int 1843229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 1844229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1845229997Sken{ 1846229997Sken struct nameidata nd; 1847229997Sken int flags; 1848229997Sken int error; 1849229997Sken 1850229997Sken /* 1851229997Sken * XXX KDM allow a read-only option? 1852229997Sken */ 1853229997Sken flags = FREAD | FWRITE; 1854229997Sken error = 0; 1855229997Sken 1856229997Sken if (rootvnode == NULL) { 1857229997Sken snprintf(req->error_str, sizeof(req->error_str), 1858272911Smav "Root filesystem is not mounted"); 1859229997Sken return (1); 1860229997Sken } 1861229997Sken 1862229997Sken if (!curthread->td_proc->p_fd->fd_cdir) { 1863229997Sken curthread->td_proc->p_fd->fd_cdir = rootvnode; 1864229997Sken VREF(rootvnode); 1865229997Sken } 1866229997Sken if (!curthread->td_proc->p_fd->fd_rdir) { 1867229997Sken curthread->td_proc->p_fd->fd_rdir = rootvnode; 1868229997Sken VREF(rootvnode); 1869229997Sken } 1870229997Sken if (!curthread->td_proc->p_fd->fd_jdir) { 1871229997Sken curthread->td_proc->p_fd->fd_jdir = rootvnode; 1872229997Sken VREF(rootvnode); 1873229997Sken } 1874229997Sken 1875229997Sken again: 1876229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1877229997Sken error = vn_open(&nd, &flags, 0, NULL); 1878229997Sken if (error) { 1879229997Sken /* 1880229997Sken * This is the only reasonable guess we can make as far as 1881229997Sken * path if the user doesn't give us a fully qualified path. 1882229997Sken * If they want to specify a file, they need to specify the 1883229997Sken * full path. 1884229997Sken */ 1885229997Sken if (be_lun->dev_path[0] != '/') { 1886229997Sken char *dev_path = "/dev/"; 1887229997Sken char *dev_name; 1888229997Sken 1889229997Sken /* Try adding device path at beginning of name */ 1890229997Sken dev_name = malloc(strlen(be_lun->dev_path) 1891229997Sken + strlen(dev_path) + 1, 1892229997Sken M_CTLBLK, M_WAITOK); 1893229997Sken if (dev_name) { 1894229997Sken sprintf(dev_name, "%s%s", dev_path, 1895229997Sken be_lun->dev_path); 1896229997Sken free(be_lun->dev_path, M_CTLBLK); 1897229997Sken be_lun->dev_path = dev_name; 1898229997Sken goto again; 1899229997Sken } 1900229997Sken } 1901229997Sken snprintf(req->error_str, sizeof(req->error_str), 1902272911Smav "error opening %s: %d", be_lun->dev_path, error); 1903229997Sken return (error); 1904229997Sken } 1905229997Sken 1906229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 1907229997Sken 1908229997Sken be_lun->vn = nd.ni_vp; 1909229997Sken 1910229997Sken /* We only support disks and files. */ 1911229997Sken if (vn_isdisk(be_lun->vn, &error)) { 1912229997Sken error = ctl_be_block_open_dev(be_lun, req); 1913229997Sken } else if (be_lun->vn->v_type == VREG) { 1914229997Sken error = ctl_be_block_open_file(be_lun, req); 1915229997Sken } else { 1916229997Sken error = EINVAL; 1917229997Sken snprintf(req->error_str, sizeof(req->error_str), 1918258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 1919229997Sken } 1920229997Sken VOP_UNLOCK(be_lun->vn, 0); 1921229997Sken 1922229997Sken if (error != 0) { 1923229997Sken ctl_be_block_close(be_lun); 1924229997Sken return (error); 1925229997Sken } 1926229997Sken 1927229997Sken be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1928229997Sken be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1929229997Sken 1930229997Sken return (0); 1931229997Sken} 1932229997Sken 1933229997Skenstatic int 1934229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1935229997Sken{ 1936229997Sken struct ctl_be_block_lun *be_lun; 1937229997Sken struct ctl_lun_create_params *params; 1938267481Smav char num_thread_str[16]; 1939229997Sken char tmpstr[32]; 1940267481Smav char *value; 1941264274Smav int retval, num_threads, unmap; 1942267481Smav int tmp_num_threads; 1943229997Sken 1944229997Sken params = &req->reqdata.create; 1945229997Sken retval = 0; 1946272911Smav req->status = CTL_LUN_OK; 1947229997Sken 1948229997Sken num_threads = cbb_num_threads; 1949229997Sken 1950229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1951229997Sken 1952272911Smav be_lun->params = req->reqdata.create; 1953229997Sken be_lun->softc = softc; 1954229997Sken STAILQ_INIT(&be_lun->input_queue); 1955229997Sken STAILQ_INIT(&be_lun->config_write_queue); 1956229997Sken STAILQ_INIT(&be_lun->datamove_queue); 1957229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1958267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 1959267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 1960268280Smav ctl_init_opts(&be_lun->ctl_be_lun.options, 1961268280Smav req->num_be_args, req->kern_be_args); 1962229997Sken 1963264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1964256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1965229997Sken 1966229997Sken if (be_lun->lun_zone == NULL) { 1967229997Sken snprintf(req->error_str, sizeof(req->error_str), 1968272911Smav "error allocating UMA zone"); 1969229997Sken goto bailout_error; 1970229997Sken } 1971229997Sken 1972229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1973229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 1974229997Sken else 1975229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 1976229997Sken 1977229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1978268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 1979267499Smav if (value == NULL) { 1980229997Sken snprintf(req->error_str, sizeof(req->error_str), 1981272911Smav "no file argument specified"); 1982229997Sken goto bailout_error; 1983229997Sken } 1984267499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 1985272911Smav be_lun->blocksize = 512; 1986272911Smav be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1987229997Sken 1988229997Sken retval = ctl_be_block_open(softc, be_lun, req); 1989229997Sken if (retval != 0) { 1990229997Sken retval = 0; 1991272911Smav req->status = CTL_LUN_WARNING; 1992229997Sken } 1993229997Sken } else { 1994229997Sken /* 1995229997Sken * For processor devices, we don't have any size. 1996229997Sken */ 1997229997Sken be_lun->blocksize = 0; 1998264191Smav be_lun->pblockexp = 0; 1999264191Smav be_lun->pblockoff = 0; 2000229997Sken be_lun->size_blocks = 0; 2001229997Sken be_lun->size_bytes = 0; 2002229997Sken be_lun->ctl_be_lun.maxlba = 0; 2003229997Sken 2004229997Sken /* 2005229997Sken * Default to just 1 thread for processor devices. 2006229997Sken */ 2007229997Sken num_threads = 1; 2008229997Sken } 2009229997Sken 2010229997Sken /* 2011229997Sken * XXX This searching loop might be refactored to be combined with 2012229997Sken * the loop above, 2013229997Sken */ 2014268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 2015267481Smav if (value != NULL) { 2016267481Smav tmp_num_threads = strtol(value, NULL, 0); 2017229997Sken 2018267481Smav /* 2019267481Smav * We don't let the user specify less than one 2020267481Smav * thread, but hope he's clueful enough not to 2021267481Smav * specify 1000 threads. 2022267481Smav */ 2023267481Smav if (tmp_num_threads < 1) { 2024267481Smav snprintf(req->error_str, sizeof(req->error_str), 2025272911Smav "invalid number of threads %s", 2026272911Smav num_thread_str); 2027267481Smav goto bailout_error; 2028229997Sken } 2029267481Smav num_threads = tmp_num_threads; 2030229997Sken } 2031274154Smav unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol); 2032268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2033274154Smav if (value != NULL) 2034274154Smav unmap = (strcmp(value, "on") == 0); 2035229997Sken 2036229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2037229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 2038272911Smav if (be_lun->vn == NULL) 2039272911Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2040264274Smav if (unmap) 2041264274Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2042229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 2043272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2044272911Smav 0 : (be_lun->size_blocks - 1); 2045229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2046264191Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2047264191Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2048272911Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2049272911Smav be_lun->blocksize != 0) 2050272911Smav be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2051272911Smav be_lun->blocksize; 2052229997Sken /* Tell the user the blocksize we ended up using */ 2053272911Smav params->lun_size_bytes = be_lun->size_bytes; 2054229997Sken params->blocksize_bytes = be_lun->blocksize; 2055229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2056229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2057229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2058229997Sken } else 2059229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 2060229997Sken 2061229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2062229997Sken be_lun->ctl_be_lun.lun_config_status = 2063229997Sken ctl_be_block_lun_config_status; 2064229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2065229997Sken 2066229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2067229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2068229997Sken softc->num_luns); 2069229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2070229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2071229997Sken sizeof(tmpstr))); 2072229997Sken 2073229997Sken /* Tell the user what we used for a serial number */ 2074229997Sken strncpy((char *)params->serial_num, tmpstr, 2075229997Sken ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 2076229997Sken } else { 2077229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 2078229997Sken params->serial_num, 2079229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2080229997Sken sizeof(params->serial_num))); 2081229997Sken } 2082229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2083229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2084229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2085229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2086229997Sken sizeof(tmpstr))); 2087229997Sken 2088229997Sken /* Tell the user what we used for a device ID */ 2089229997Sken strncpy((char *)params->device_id, tmpstr, 2090229997Sken ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2091229997Sken } else { 2092229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 2093229997Sken params->device_id, 2094229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2095229997Sken sizeof(params->device_id))); 2096229997Sken } 2097229997Sken 2098229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2099229997Sken 2100229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2101229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2102229997Sken 2103229997Sken if (be_lun->io_taskqueue == NULL) { 2104229997Sken snprintf(req->error_str, sizeof(req->error_str), 2105272911Smav "unable to create taskqueue"); 2106229997Sken goto bailout_error; 2107229997Sken } 2108229997Sken 2109229997Sken /* 2110229997Sken * Note that we start the same number of threads by default for 2111229997Sken * both the file case and the block device case. For the file 2112229997Sken * case, we need multiple threads to allow concurrency, because the 2113229997Sken * vnode interface is designed to be a blocking interface. For the 2114229997Sken * block device case, ZFS zvols at least will block the caller's 2115229997Sken * context in many instances, and so we need multiple threads to 2116229997Sken * overcome that problem. Other block devices don't need as many 2117229997Sken * threads, but they shouldn't cause too many problems. 2118229997Sken * 2119229997Sken * If the user wants to just have a single thread for a block 2120229997Sken * device, he can specify that when the LUN is created, or change 2121229997Sken * the tunable/sysctl to alter the default number of threads. 2122229997Sken */ 2123229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2124229997Sken /*num threads*/num_threads, 2125229997Sken /*priority*/PWAIT, 2126229997Sken /*thread name*/ 2127229997Sken "%s taskq", be_lun->lunname); 2128229997Sken 2129229997Sken if (retval != 0) 2130229997Sken goto bailout_error; 2131229997Sken 2132229997Sken be_lun->num_threads = num_threads; 2133229997Sken 2134229997Sken mtx_lock(&softc->lock); 2135229997Sken softc->num_luns++; 2136229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2137229997Sken 2138229997Sken mtx_unlock(&softc->lock); 2139229997Sken 2140229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 2141229997Sken if (retval != 0) { 2142229997Sken mtx_lock(&softc->lock); 2143229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2144229997Sken links); 2145229997Sken softc->num_luns--; 2146229997Sken mtx_unlock(&softc->lock); 2147229997Sken snprintf(req->error_str, sizeof(req->error_str), 2148272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2149272911Smav "details", retval); 2150229997Sken retval = 0; 2151229997Sken goto bailout_error; 2152229997Sken } 2153229997Sken 2154229997Sken mtx_lock(&softc->lock); 2155229997Sken 2156229997Sken /* 2157229997Sken * Tell the config_status routine that we're waiting so it won't 2158229997Sken * clean up the LUN in the event of an error. 2159229997Sken */ 2160229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2161229997Sken 2162229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2163229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2164229997Sken if (retval == EINTR) 2165229997Sken break; 2166229997Sken } 2167229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2168229997Sken 2169229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2170229997Sken snprintf(req->error_str, sizeof(req->error_str), 2171272911Smav "LUN configuration error, see dmesg for details"); 2172229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2173229997Sken links); 2174229997Sken softc->num_luns--; 2175229997Sken mtx_unlock(&softc->lock); 2176229997Sken goto bailout_error; 2177229997Sken } else { 2178229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2179229997Sken } 2180229997Sken 2181229997Sken mtx_unlock(&softc->lock); 2182229997Sken 2183229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2184229997Sken be_lun->blocksize, 2185229997Sken DEVSTAT_ALL_SUPPORTED, 2186229997Sken be_lun->ctl_be_lun.lun_type 2187229997Sken | DEVSTAT_TYPE_IF_OTHER, 2188229997Sken DEVSTAT_PRIORITY_OTHER); 2189229997Sken 2190229997Sken return (retval); 2191229997Sken 2192229997Skenbailout_error: 2193229997Sken req->status = CTL_LUN_ERROR; 2194229997Sken 2195267429Smav if (be_lun->io_taskqueue != NULL) 2196267429Smav taskqueue_free(be_lun->io_taskqueue); 2197229997Sken ctl_be_block_close(be_lun); 2198267429Smav if (be_lun->dev_path != NULL) 2199267429Smav free(be_lun->dev_path, M_CTLBLK); 2200267429Smav if (be_lun->lun_zone != NULL) 2201267429Smav uma_zdestroy(be_lun->lun_zone); 2202268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2203267877Smav mtx_destroy(&be_lun->queue_lock); 2204267877Smav mtx_destroy(&be_lun->io_lock); 2205229997Sken free(be_lun, M_CTLBLK); 2206229997Sken 2207229997Sken return (retval); 2208229997Sken} 2209229997Sken 2210229997Skenstatic int 2211229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2212229997Sken{ 2213229997Sken struct ctl_lun_rm_params *params; 2214229997Sken struct ctl_be_block_lun *be_lun; 2215229997Sken int retval; 2216229997Sken 2217229997Sken params = &req->reqdata.rm; 2218229997Sken 2219229997Sken mtx_lock(&softc->lock); 2220229997Sken 2221229997Sken be_lun = NULL; 2222229997Sken 2223229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2224229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2225229997Sken break; 2226229997Sken } 2227229997Sken mtx_unlock(&softc->lock); 2228229997Sken 2229229997Sken if (be_lun == NULL) { 2230229997Sken snprintf(req->error_str, sizeof(req->error_str), 2231272911Smav "LUN %u is not managed by the block backend", 2232272911Smav params->lun_id); 2233229997Sken goto bailout_error; 2234229997Sken } 2235229997Sken 2236229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2237229997Sken 2238229997Sken if (retval != 0) { 2239229997Sken snprintf(req->error_str, sizeof(req->error_str), 2240272911Smav "error %d returned from ctl_disable_lun() for " 2241272911Smav "LUN %d", retval, params->lun_id); 2242229997Sken goto bailout_error; 2243229997Sken 2244229997Sken } 2245229997Sken 2246229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2247229997Sken if (retval != 0) { 2248229997Sken snprintf(req->error_str, sizeof(req->error_str), 2249272911Smav "error %d returned from ctl_invalidate_lun() for " 2250272911Smav "LUN %d", retval, params->lun_id); 2251229997Sken goto bailout_error; 2252229997Sken } 2253229997Sken 2254229997Sken mtx_lock(&softc->lock); 2255229997Sken 2256229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2257229997Sken 2258229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2259229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2260229997Sken if (retval == EINTR) 2261229997Sken break; 2262229997Sken } 2263229997Sken 2264229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2265229997Sken 2266229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2267229997Sken snprintf(req->error_str, sizeof(req->error_str), 2268272911Smav "interrupted waiting for LUN to be freed"); 2269229997Sken mtx_unlock(&softc->lock); 2270229997Sken goto bailout_error; 2271229997Sken } 2272229997Sken 2273229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2274229997Sken 2275229997Sken softc->num_luns--; 2276229997Sken mtx_unlock(&softc->lock); 2277229997Sken 2278229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2279229997Sken 2280229997Sken taskqueue_free(be_lun->io_taskqueue); 2281229997Sken 2282229997Sken ctl_be_block_close(be_lun); 2283229997Sken 2284229997Sken if (be_lun->disk_stats != NULL) 2285229997Sken devstat_remove_entry(be_lun->disk_stats); 2286229997Sken 2287229997Sken uma_zdestroy(be_lun->lun_zone); 2288229997Sken 2289268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2290229997Sken free(be_lun->dev_path, M_CTLBLK); 2291267877Smav mtx_destroy(&be_lun->queue_lock); 2292267877Smav mtx_destroy(&be_lun->io_lock); 2293229997Sken free(be_lun, M_CTLBLK); 2294229997Sken 2295229997Sken req->status = CTL_LUN_OK; 2296229997Sken 2297229997Sken return (0); 2298229997Sken 2299229997Skenbailout_error: 2300229997Sken 2301229997Sken req->status = CTL_LUN_ERROR; 2302229997Sken 2303229997Sken return (0); 2304229997Sken} 2305229997Sken 2306232604Straszstatic int 2307232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2308232604Strasz struct ctl_lun_req *req) 2309232604Strasz{ 2310232604Strasz struct vattr vattr; 2311232604Strasz int error; 2312272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2313232604Strasz 2314232604Strasz if (params->lun_size_bytes != 0) { 2315232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2316232604Strasz } else { 2317271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2318232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2319271794Smav VOP_UNLOCK(be_lun->vn, 0); 2320232604Strasz if (error != 0) { 2321232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2322232604Strasz "error calling VOP_GETATTR() for file %s", 2323232604Strasz be_lun->dev_path); 2324232604Strasz return (error); 2325232604Strasz } 2326232604Strasz 2327232604Strasz be_lun->size_bytes = vattr.va_size; 2328232604Strasz } 2329232604Strasz 2330232604Strasz return (0); 2331232604Strasz} 2332232604Strasz 2333232604Straszstatic int 2334232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2335232604Strasz struct ctl_lun_req *req) 2336232604Strasz{ 2337271794Smav struct ctl_be_block_devdata *dev_data; 2338232604Strasz int error; 2339272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2340232604Strasz uint64_t size_bytes; 2341232604Strasz 2342271794Smav dev_data = &be_lun->backend.dev; 2343271794Smav if (!dev_data->csw->d_ioctl) { 2344232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2345272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2346232604Strasz return (ENODEV); 2347232604Strasz } 2348232604Strasz 2349271794Smav error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2350232604Strasz (caddr_t)&size_bytes, FREAD, 2351232604Strasz curthread); 2352232604Strasz if (error) { 2353232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2354272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2355272911Smav "on %s!", error, be_lun->dev_path); 2356232604Strasz return (error); 2357232604Strasz } 2358232604Strasz 2359232604Strasz if (params->lun_size_bytes != 0) { 2360232604Strasz if (params->lun_size_bytes > size_bytes) { 2361232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2362272911Smav "requested LUN size %ju > backing device " 2363272911Smav "size %ju", 2364232604Strasz (uintmax_t)params->lun_size_bytes, 2365232604Strasz (uintmax_t)size_bytes); 2366232604Strasz return (EINVAL); 2367232604Strasz } 2368232604Strasz 2369232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2370232604Strasz } else { 2371232604Strasz be_lun->size_bytes = size_bytes; 2372232604Strasz } 2373232604Strasz 2374232604Strasz return (0); 2375232604Strasz} 2376232604Strasz 2377232604Straszstatic int 2378232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2379232604Strasz{ 2380232604Strasz struct ctl_lun_modify_params *params; 2381232604Strasz struct ctl_be_block_lun *be_lun; 2382271794Smav uint64_t oldsize; 2383241896Skib int error; 2384232604Strasz 2385232604Strasz params = &req->reqdata.modify; 2386232604Strasz 2387232604Strasz mtx_lock(&softc->lock); 2388232604Strasz be_lun = NULL; 2389232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2390232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2391232604Strasz break; 2392232604Strasz } 2393232604Strasz mtx_unlock(&softc->lock); 2394232604Strasz 2395232604Strasz if (be_lun == NULL) { 2396232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2397272911Smav "LUN %u is not managed by the block backend", 2398272911Smav params->lun_id); 2399232604Strasz goto bailout_error; 2400232604Strasz } 2401232604Strasz 2402272911Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2403232604Strasz 2404274253Smav oldsize = be_lun->size_bytes; 2405272911Smav if (be_lun->vn == NULL) 2406272911Smav error = ctl_be_block_open(softc, be_lun, req); 2407272911Smav else if (be_lun->vn->v_type == VREG) 2408232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2409232604Strasz else 2410232604Strasz error = ctl_be_block_modify_dev(be_lun, req); 2411232604Strasz 2412274253Smav if (error == 0 && be_lun->size_bytes != oldsize) { 2413271794Smav be_lun->size_blocks = be_lun->size_bytes >> 2414271794Smav be_lun->blocksize_shift; 2415232604Strasz 2416271794Smav /* 2417271794Smav * The maximum LBA is the size - 1. 2418271794Smav * 2419271794Smav * XXX: Note that this field is being updated without locking, 2420271794Smav * which might cause problems on 32-bit architectures. 2421271794Smav */ 2422272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2423272911Smav 0 : (be_lun->size_blocks - 1); 2424272911Smav be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2425272911Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2426272911Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2427272911Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2428272911Smav be_lun->blocksize != 0) 2429272911Smav be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2430272911Smav be_lun->blocksize; 2431271794Smav ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2432272911Smav if (oldsize == 0 && be_lun->size_blocks != 0) 2433272911Smav ctl_lun_online(&be_lun->ctl_be_lun); 2434271794Smav } 2435232604Strasz 2436232604Strasz /* Tell the user the exact size we ended up using */ 2437232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2438232604Strasz 2439272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2440232604Strasz 2441232604Strasz return (0); 2442232604Strasz 2443232604Straszbailout_error: 2444232604Strasz req->status = CTL_LUN_ERROR; 2445232604Strasz 2446232604Strasz return (0); 2447232604Strasz} 2448232604Strasz 2449229997Skenstatic void 2450229997Skenctl_be_block_lun_shutdown(void *be_lun) 2451229997Sken{ 2452229997Sken struct ctl_be_block_lun *lun; 2453229997Sken struct ctl_be_block_softc *softc; 2454229997Sken 2455229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2456229997Sken 2457229997Sken softc = lun->softc; 2458229997Sken 2459229997Sken mtx_lock(&softc->lock); 2460229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2461229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2462229997Sken wakeup(lun); 2463229997Sken mtx_unlock(&softc->lock); 2464229997Sken 2465229997Sken} 2466229997Sken 2467229997Skenstatic void 2468229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2469229997Sken{ 2470229997Sken struct ctl_be_block_lun *lun; 2471229997Sken struct ctl_be_block_softc *softc; 2472229997Sken 2473229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2474229997Sken softc = lun->softc; 2475229997Sken 2476229997Sken if (status == CTL_LUN_CONFIG_OK) { 2477229997Sken mtx_lock(&softc->lock); 2478229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2479229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2480229997Sken wakeup(lun); 2481229997Sken mtx_unlock(&softc->lock); 2482229997Sken 2483229997Sken /* 2484229997Sken * We successfully added the LUN, attempt to enable it. 2485229997Sken */ 2486229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2487229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2488229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2489229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2490229997Sken __func__); 2491229997Sken } 2492229997Sken } 2493229997Sken 2494229997Sken return; 2495229997Sken } 2496229997Sken 2497229997Sken 2498229997Sken mtx_lock(&softc->lock); 2499229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2500229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2501229997Sken wakeup(lun); 2502229997Sken mtx_unlock(&softc->lock); 2503229997Sken} 2504229997Sken 2505229997Sken 2506229997Skenstatic int 2507229997Skenctl_be_block_config_write(union ctl_io *io) 2508229997Sken{ 2509229997Sken struct ctl_be_block_lun *be_lun; 2510229997Sken struct ctl_be_lun *ctl_be_lun; 2511229997Sken int retval; 2512229997Sken 2513229997Sken retval = 0; 2514229997Sken 2515229997Sken DPRINTF("entered\n"); 2516229997Sken 2517229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2518229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2519229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2520229997Sken 2521229997Sken switch (io->scsiio.cdb[0]) { 2522229997Sken case SYNCHRONIZE_CACHE: 2523229997Sken case SYNCHRONIZE_CACHE_16: 2524264274Smav case WRITE_SAME_10: 2525264274Smav case WRITE_SAME_16: 2526264274Smav case UNMAP: 2527229997Sken /* 2528229997Sken * The upper level CTL code will filter out any CDBs with 2529229997Sken * the immediate bit set and return the proper error. 2530229997Sken * 2531229997Sken * We don't really need to worry about what LBA range the 2532229997Sken * user asked to be synced out. When they issue a sync 2533229997Sken * cache command, we'll sync out the whole thing. 2534229997Sken */ 2535267877Smav mtx_lock(&be_lun->queue_lock); 2536229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2537229997Sken links); 2538267877Smav mtx_unlock(&be_lun->queue_lock); 2539229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2540229997Sken break; 2541229997Sken case START_STOP_UNIT: { 2542229997Sken struct scsi_start_stop_unit *cdb; 2543229997Sken 2544229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2545229997Sken 2546229997Sken if (cdb->how & SSS_START) 2547229997Sken retval = ctl_start_lun(ctl_be_lun); 2548229997Sken else { 2549229997Sken retval = ctl_stop_lun(ctl_be_lun); 2550229997Sken /* 2551229997Sken * XXX KDM Copan-specific offline behavior. 2552229997Sken * Figure out a reasonable way to port this? 2553229997Sken */ 2554229997Sken#ifdef NEEDTOPORT 2555229997Sken if ((retval == 0) 2556229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2557229997Sken retval = ctl_lun_offline(ctl_be_lun); 2558229997Sken#endif 2559229997Sken } 2560229997Sken 2561229997Sken /* 2562229997Sken * In general, the above routines should not fail. They 2563229997Sken * just set state for the LUN. So we've got something 2564229997Sken * pretty wrong here if we can't start or stop the LUN. 2565229997Sken */ 2566229997Sken if (retval != 0) { 2567229997Sken ctl_set_internal_failure(&io->scsiio, 2568229997Sken /*sks_valid*/ 1, 2569229997Sken /*retry_count*/ 0xf051); 2570229997Sken retval = CTL_RETVAL_COMPLETE; 2571229997Sken } else { 2572229997Sken ctl_set_success(&io->scsiio); 2573229997Sken } 2574229997Sken ctl_config_write_done(io); 2575229997Sken break; 2576229997Sken } 2577229997Sken default: 2578229997Sken ctl_set_invalid_opcode(&io->scsiio); 2579229997Sken ctl_config_write_done(io); 2580229997Sken retval = CTL_RETVAL_COMPLETE; 2581229997Sken break; 2582229997Sken } 2583229997Sken 2584229997Sken return (retval); 2585229997Sken 2586229997Sken} 2587229997Sken 2588229997Skenstatic int 2589229997Skenctl_be_block_config_read(union ctl_io *io) 2590229997Sken{ 2591229997Sken return (0); 2592229997Sken} 2593229997Sken 2594229997Skenstatic int 2595229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2596229997Sken{ 2597229997Sken struct ctl_be_block_lun *lun; 2598229997Sken int retval; 2599229997Sken 2600229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2601229997Sken retval = 0; 2602229997Sken 2603268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2604229997Sken 2605229997Sken if (retval != 0) 2606229997Sken goto bailout; 2607229997Sken 2608229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2609229997Sken 2610229997Sken if (retval != 0) 2611229997Sken goto bailout; 2612229997Sken 2613268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2614229997Sken 2615229997Skenbailout: 2616229997Sken 2617229997Sken return (retval); 2618229997Sken} 2619229997Sken 2620274154Smavstatic uint64_t 2621274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2622274154Smav{ 2623274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2624274154Smav 2625274154Smav if (lun->getattr == NULL) 2626274154Smav return (UINT64_MAX); 2627274154Smav return (lun->getattr(lun, attrname)); 2628274154Smav} 2629274154Smav 2630229997Skenint 2631229997Skenctl_be_block_init(void) 2632229997Sken{ 2633229997Sken struct ctl_be_block_softc *softc; 2634229997Sken int retval; 2635229997Sken 2636229997Sken softc = &backend_block_softc; 2637229997Sken retval = 0; 2638229997Sken 2639267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2640264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2641264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2642229997Sken STAILQ_INIT(&softc->disk_list); 2643229997Sken STAILQ_INIT(&softc->lun_list); 2644229997Sken 2645229997Sken return (retval); 2646229997Sken} 2647