ctl_backend_block.c revision 267641
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 267641 2014-06-19 12:43:41Z 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); 148229997Sken 149229997Sken/* 150229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 151229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 152229997Sken */ 153229997Skenstruct ctl_be_block_lun { 154229997Sken struct ctl_block_disk *disk; 155229997Sken char lunname[32]; 156229997Sken char *dev_path; 157229997Sken ctl_be_block_type dev_type; 158229997Sken struct vnode *vn; 159229997Sken union ctl_be_block_bedata backend; 160229997Sken cbb_dispatch_t dispatch; 161229997Sken cbb_dispatch_t lun_flush; 162264274Smav cbb_dispatch_t unmap; 163229997Sken struct mtx lock; 164229997Sken uma_zone_t lun_zone; 165229997Sken uint64_t size_blocks; 166229997Sken uint64_t size_bytes; 167229997Sken uint32_t blocksize; 168229997Sken int blocksize_shift; 169264191Smav uint16_t pblockexp; 170264191Smav uint16_t pblockoff; 171229997Sken struct ctl_be_block_softc *softc; 172229997Sken struct devstat *disk_stats; 173229997Sken ctl_be_block_lun_flags flags; 174229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 175229997Sken struct ctl_be_lun ctl_be_lun; 176229997Sken struct taskqueue *io_taskqueue; 177229997Sken struct task io_task; 178229997Sken int num_threads; 179229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 180229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 181229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 182229997Sken}; 183229997Sken 184229997Sken/* 185229997Sken * Overall softc structure for the block backend module. 186229997Sken */ 187229997Skenstruct ctl_be_block_softc { 188229997Sken struct mtx lock; 189229997Sken int num_disks; 190229997Sken STAILQ_HEAD(, ctl_block_disk) disk_list; 191229997Sken int num_luns; 192229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 193229997Sken}; 194229997Sken 195229997Skenstatic struct ctl_be_block_softc backend_block_softc; 196229997Sken 197229997Sken/* 198229997Sken * Per-I/O information. 199229997Sken */ 200229997Skenstruct ctl_be_block_io { 201229997Sken union ctl_io *io; 202229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 203229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 204229997Sken int bio_cmd; 205229997Sken int bio_flags; 206229997Sken int num_segs; 207229997Sken int num_bios_sent; 208229997Sken int num_bios_done; 209229997Sken int send_complete; 210229997Sken int num_errors; 211229997Sken struct bintime ds_t0; 212229997Sken devstat_tag_type ds_tag_type; 213229997Sken devstat_trans_flags ds_trans_type; 214229997Sken uint64_t io_len; 215229997Sken uint64_t io_offset; 216229997Sken struct ctl_be_block_softc *softc; 217229997Sken struct ctl_be_block_lun *lun; 218264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 219229997Sken}; 220229997Sken 221229997Skenstatic int cbb_num_threads = 14; 222229997SkenTUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads); 223229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 224229997Sken "CAM Target Layer Block Backend"); 225229997SkenSYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW, 226229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 227229997Sken 228229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 229229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 230229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 231229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 232229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 233229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 234229997Sken struct ctl_be_block_io *beio); 235229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 236229997Sken struct ctl_be_block_io *beio); 237229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 238229997Sken struct ctl_be_block_io *beio); 239264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 240264274Smav struct ctl_be_block_io *beio); 241229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 242229997Sken struct ctl_be_block_io *beio); 243229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 244229997Sken union ctl_io *io); 245229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 246229997Sken union ctl_io *io); 247229997Skenstatic void ctl_be_block_worker(void *context, int pending); 248229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 249229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 250229997Sken int flag, struct thread *td); 251229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 252229997Sken struct ctl_lun_req *req); 253229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 254229997Sken struct ctl_lun_req *req); 255229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 256229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 257229997Sken struct ctl_be_block_lun *be_lun, 258229997Sken struct ctl_lun_req *req); 259229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 260229997Sken struct ctl_lun_req *req); 261229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 262229997Sken struct ctl_lun_req *req); 263232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 264232604Strasz struct ctl_lun_req *req); 265232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 266232604Strasz struct ctl_lun_req *req); 267232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 268232604Strasz struct ctl_lun_req *req); 269229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 270229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 271229997Sken ctl_lun_config_status status); 272229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 273229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 274229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 275229997Skenint ctl_be_block_init(void); 276229997Sken 277229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 278229997Sken{ 279230334Sken .name = "block", 280230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 281230334Sken .init = ctl_be_block_init, 282230334Sken .data_submit = ctl_be_block_submit, 283230334Sken .data_move_done = ctl_be_block_move_done, 284230334Sken .config_read = ctl_be_block_config_read, 285230334Sken .config_write = ctl_be_block_config_write, 286230334Sken .ioctl = ctl_be_block_ioctl, 287230334Sken .lun_info = ctl_be_block_lun_info 288229997Sken}; 289229997Sken 290229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 291229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 292229997Sken 293264020Straszstatic uma_zone_t beio_zone; 294264020Strasz 295229997Skenstatic struct ctl_be_block_io * 296229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 297229997Sken{ 298229997Sken struct ctl_be_block_io *beio; 299229997Sken 300264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 301264020Strasz beio->softc = softc; 302229997Sken return (beio); 303229997Sken} 304229997Sken 305229997Skenstatic void 306229997Skenctl_free_beio(struct ctl_be_block_io *beio) 307229997Sken{ 308229997Sken int duplicate_free; 309229997Sken int i; 310229997Sken 311229997Sken duplicate_free = 0; 312229997Sken 313229997Sken for (i = 0; i < beio->num_segs; i++) { 314229997Sken if (beio->sg_segs[i].addr == NULL) 315229997Sken duplicate_free++; 316229997Sken 317229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 318229997Sken beio->sg_segs[i].addr = NULL; 319267537Smav 320267537Smav /* For compare we had two equal S/G lists. */ 321267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 322267537Smav uma_zfree(beio->lun->lun_zone, 323267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 324267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 325267537Smav } 326229997Sken } 327229997Sken 328229997Sken if (duplicate_free > 0) { 329229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 330229997Sken duplicate_free, beio->num_segs); 331229997Sken } 332229997Sken 333264020Strasz uma_zfree(beio_zone, beio); 334229997Sken} 335229997Sken 336229997Skenstatic void 337229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 338229997Sken{ 339229997Sken union ctl_io *io; 340229997Sken int io_len; 341229997Sken 342229997Sken io = beio->io; 343229997Sken 344229997Sken if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 345229997Sken io_len = beio->io_len; 346229997Sken else 347229997Sken io_len = 0; 348229997Sken 349229997Sken devstat_end_transaction(beio->lun->disk_stats, 350229997Sken /*bytes*/ io_len, 351229997Sken beio->ds_tag_type, 352229997Sken beio->ds_trans_type, 353229997Sken /*now*/ NULL, 354229997Sken /*then*/&beio->ds_t0); 355229997Sken 356264274Smav if (beio->beio_cont != NULL) { 357264274Smav beio->beio_cont(beio); 358264274Smav } else { 359264274Smav ctl_free_beio(beio); 360267537Smav ctl_data_submit_done(io); 361264274Smav } 362229997Sken} 363229997Sken 364229997Skenstatic int 365229997Skenctl_be_block_move_done(union ctl_io *io) 366229997Sken{ 367229997Sken struct ctl_be_block_io *beio; 368229997Sken struct ctl_be_block_lun *be_lun; 369267537Smav struct ctl_lba_len_flags *lbalen; 370229997Sken#ifdef CTL_TIME_IO 371229997Sken struct bintime cur_bt; 372267537Smav#endif 373267537Smav int i; 374229997Sken 375267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 376229997Sken be_lun = beio->lun; 377229997Sken 378229997Sken DPRINTF("entered\n"); 379229997Sken 380229997Sken#ifdef CTL_TIME_IO 381229997Sken getbintime(&cur_bt); 382229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 383229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 384229997Sken io->io_hdr.num_dmas++; 385229997Sken#endif 386267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 387229997Sken 388229997Sken /* 389229997Sken * We set status at this point for read commands, and write 390229997Sken * commands with errors. 391229997Sken */ 392267537Smav if ((io->io_hdr.port_status == 0) && 393267537Smav ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) && 394267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 395267537Smav lbalen = ARGS(beio->io); 396267537Smav if (lbalen->flags & CTL_LLF_READ) { 397267537Smav ctl_set_success(&io->scsiio); 398267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 399267537Smav /* We have two data blocks ready for comparison. */ 400267537Smav for (i = 0; i < beio->num_segs; i++) { 401267537Smav if (memcmp(beio->sg_segs[i].addr, 402267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 403267537Smav beio->sg_segs[i].len) != 0) 404267537Smav break; 405267537Smav } 406267537Smav if (i < beio->num_segs) 407267537Smav ctl_set_sense(&io->scsiio, 408267537Smav /*current_error*/ 1, 409267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 410267537Smav /*asc*/ 0x1D, 411267537Smav /*ascq*/ 0x00, 412267537Smav SSD_ELEM_NONE); 413267537Smav else 414267537Smav ctl_set_success(&io->scsiio); 415267537Smav } 416267537Smav } 417229997Sken else if ((io->io_hdr.port_status != 0) 418229997Sken && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 419229997Sken && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 420229997Sken /* 421229997Sken * For hardware error sense keys, the sense key 422229997Sken * specific value is defined to be a retry count, 423229997Sken * but we use it to pass back an internal FETD 424229997Sken * error code. XXX KDM Hopefully the FETD is only 425229997Sken * using 16 bits for an error code, since that's 426229997Sken * all the space we have in the sks field. 427229997Sken */ 428229997Sken ctl_set_internal_failure(&io->scsiio, 429229997Sken /*sks_valid*/ 1, 430229997Sken /*retry_count*/ 431229997Sken io->io_hdr.port_status); 432229997Sken } 433229997Sken 434229997Sken /* 435229997Sken * If this is a read, or a write with errors, it is done. 436229997Sken */ 437229997Sken if ((beio->bio_cmd == BIO_READ) 438229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 439229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 440229997Sken ctl_complete_beio(beio); 441229997Sken return (0); 442229997Sken } 443229997Sken 444229997Sken /* 445229997Sken * At this point, we have a write and the DMA completed 446229997Sken * successfully. We now have to queue it to the task queue to 447229997Sken * execute the backend I/O. That is because we do blocking 448229997Sken * memory allocations, and in the file backing case, blocking I/O. 449229997Sken * This move done routine is generally called in the SIM's 450229997Sken * interrupt context, and therefore we cannot block. 451229997Sken */ 452229997Sken mtx_lock(&be_lun->lock); 453229997Sken /* 454229997Sken * XXX KDM make sure that links is okay to use at this point. 455229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 456229997Sken * or deal with resource allocation here. 457229997Sken */ 458229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 459229997Sken mtx_unlock(&be_lun->lock); 460229997Sken 461229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 462229997Sken 463229997Sken return (0); 464229997Sken} 465229997Sken 466229997Skenstatic void 467229997Skenctl_be_block_biodone(struct bio *bio) 468229997Sken{ 469229997Sken struct ctl_be_block_io *beio; 470229997Sken struct ctl_be_block_lun *be_lun; 471229997Sken union ctl_io *io; 472261538Smav int error; 473229997Sken 474229997Sken beio = bio->bio_caller1; 475229997Sken be_lun = beio->lun; 476229997Sken io = beio->io; 477229997Sken 478229997Sken DPRINTF("entered\n"); 479229997Sken 480261538Smav error = bio->bio_error; 481229997Sken mtx_lock(&be_lun->lock); 482261538Smav if (error != 0) 483229997Sken beio->num_errors++; 484229997Sken 485229997Sken beio->num_bios_done++; 486229997Sken 487229997Sken /* 488229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 489229997Sken * during the free might cause it to complain. 490229997Sken */ 491229997Sken g_destroy_bio(bio); 492229997Sken 493229997Sken /* 494229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 495229997Sken * complete, then we're done. 496229997Sken */ 497229997Sken if ((beio->send_complete == 0) 498229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 499229997Sken mtx_unlock(&be_lun->lock); 500229997Sken return; 501229997Sken } 502229997Sken 503229997Sken /* 504229997Sken * At this point, we've verified that we are the last I/O to 505229997Sken * complete, so it's safe to drop the lock. 506229997Sken */ 507229997Sken mtx_unlock(&be_lun->lock); 508229997Sken 509229997Sken /* 510229997Sken * If there are any errors from the backing device, we fail the 511229997Sken * entire I/O with a medium error. 512229997Sken */ 513229997Sken if (beio->num_errors > 0) { 514261538Smav if (error == EOPNOTSUPP) { 515261538Smav ctl_set_invalid_opcode(&io->scsiio); 516261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 517229997Sken /* XXX KDM is there is a better error here? */ 518229997Sken ctl_set_internal_failure(&io->scsiio, 519229997Sken /*sks_valid*/ 1, 520229997Sken /*retry_count*/ 0xbad2); 521229997Sken } else 522229997Sken ctl_set_medium_error(&io->scsiio); 523229997Sken ctl_complete_beio(beio); 524229997Sken return; 525229997Sken } 526229997Sken 527229997Sken /* 528267537Smav * If this is a write, a flush, a delete or verify, we're all done. 529229997Sken * If this is a read, we can now send the data to the user. 530229997Sken */ 531229997Sken if ((beio->bio_cmd == BIO_WRITE) 532264274Smav || (beio->bio_cmd == BIO_FLUSH) 533267537Smav || (beio->bio_cmd == BIO_DELETE) 534267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 535229997Sken ctl_set_success(&io->scsiio); 536229997Sken ctl_complete_beio(beio); 537229997Sken } else { 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{ 549229997Sken union ctl_io *io; 550229997Sken struct mount *mountpoint; 551241896Skib int error, lock_flags; 552229997Sken 553229997Sken DPRINTF("entered\n"); 554229997Sken 555229997Sken io = beio->io; 556229997Sken 557229997Sken (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 558229997Sken 559229997Sken if (MNT_SHARED_WRITES(mountpoint) 560229997Sken || ((mountpoint == NULL) 561229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 562229997Sken lock_flags = LK_SHARED; 563229997Sken else 564229997Sken lock_flags = LK_EXCLUSIVE; 565229997Sken 566229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 567229997Sken 568229997Sken binuptime(&beio->ds_t0); 569229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 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 576229997Sken if (error == 0) 577229997Sken ctl_set_success(&io->scsiio); 578229997Sken else { 579229997Sken /* XXX KDM is there is a better error here? */ 580229997Sken ctl_set_internal_failure(&io->scsiio, 581229997Sken /*sks_valid*/ 1, 582229997Sken /*retry_count*/ 0xbad1); 583229997Sken } 584229997Sken 585229997Sken ctl_complete_beio(beio); 586229997Sken} 587229997Sken 588258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 589258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 590258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 591258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 592229997Sken 593229997Skenstatic void 594229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 595229997Sken struct ctl_be_block_io *beio) 596229997Sken{ 597229997Sken struct ctl_be_block_filedata *file_data; 598229997Sken union ctl_io *io; 599229997Sken struct uio xuio; 600229997Sken struct iovec *xiovec; 601241896Skib int flags; 602229997Sken int error, i; 603229997Sken 604229997Sken DPRINTF("entered\n"); 605229997Sken 606229997Sken file_data = &be_lun->backend.file; 607229997Sken io = beio->io; 608229997Sken flags = beio->bio_flags; 609229997Sken 610267537Smav bzero(&xuio, sizeof(xuio)); 611229997Sken if (beio->bio_cmd == BIO_READ) { 612229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 613267537Smav xuio.uio_rw = UIO_READ; 614229997Sken } else { 615229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 616267537Smav xuio.uio_rw = UIO_WRITE; 617229997Sken } 618229997Sken xuio.uio_offset = beio->io_offset; 619229997Sken xuio.uio_resid = beio->io_len; 620229997Sken xuio.uio_segflg = UIO_SYSSPACE; 621229997Sken xuio.uio_iov = beio->xiovecs; 622229997Sken xuio.uio_iovcnt = beio->num_segs; 623229997Sken xuio.uio_td = curthread; 624229997Sken 625229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 626229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 627229997Sken xiovec->iov_len = beio->sg_segs[i].len; 628229997Sken } 629229997Sken 630229997Sken if (beio->bio_cmd == BIO_READ) { 631229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 632229997Sken 633229997Sken binuptime(&beio->ds_t0); 634229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 635229997Sken 636229997Sken /* 637229997Sken * UFS pays attention to IO_DIRECT for reads. If the 638229997Sken * DIRECTIO option is configured into the kernel, it calls 639229997Sken * ffs_rawread(). But that only works for single-segment 640229997Sken * uios with user space addresses. In our case, with a 641229997Sken * kernel uio, it still reads into the buffer cache, but it 642229997Sken * will just try to release the buffer from the cache later 643229997Sken * on in ffs_read(). 644229997Sken * 645229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 646229997Sken * 647229997Sken * UFS does not pay attention to IO_SYNC for reads. 648229997Sken * 649229997Sken * ZFS pays attention to IO_SYNC (which translates into the 650229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 651229997Sken * attempts to sync the file before reading. 652229997Sken * 653229997Sken * So, to attempt to provide some barrier semantics in the 654229997Sken * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 655229997Sken */ 656229997Sken error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 657229997Sken (IO_DIRECT|IO_SYNC) : 0, file_data->cred); 658229997Sken 659229997Sken VOP_UNLOCK(be_lun->vn, 0); 660267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 661229997Sken } else { 662229997Sken struct mount *mountpoint; 663229997Sken int lock_flags; 664229997Sken 665229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 666229997Sken 667229997Sken if (MNT_SHARED_WRITES(mountpoint) 668229997Sken || ((mountpoint == NULL) 669229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 670229997Sken lock_flags = LK_SHARED; 671229997Sken else 672229997Sken lock_flags = LK_EXCLUSIVE; 673229997Sken 674229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 675229997Sken 676229997Sken binuptime(&beio->ds_t0); 677229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 678229997Sken 679229997Sken /* 680229997Sken * UFS pays attention to IO_DIRECT for writes. The write 681229997Sken * is done asynchronously. (Normally the write would just 682229997Sken * get put into cache. 683229997Sken * 684229997Sken * UFS pays attention to IO_SYNC for writes. It will 685229997Sken * attempt to write the buffer out synchronously if that 686229997Sken * flag is set. 687229997Sken * 688229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 689229997Sken * 690229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 691229997Sken * for writes. It will flush the transaction from the 692229997Sken * cache before returning. 693229997Sken * 694229997Sken * So if we've got the BIO_ORDERED flag set, we want 695229997Sken * IO_SYNC in either the UFS or ZFS case. 696229997Sken */ 697229997Sken error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 698229997Sken IO_SYNC : 0, file_data->cred); 699229997Sken VOP_UNLOCK(be_lun->vn, 0); 700229997Sken 701229997Sken vn_finished_write(mountpoint); 702267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 703229997Sken } 704229997Sken 705229997Sken /* 706229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 707229997Sken * return the I/O to the user. 708229997Sken */ 709229997Sken if (error != 0) { 710229997Sken char path_str[32]; 711229997Sken 712229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 713229997Sken /* 714229997Sken * XXX KDM ZFS returns ENOSPC when the underlying 715229997Sken * filesystem fills up. What kind of SCSI error should we 716229997Sken * return for that? 717229997Sken */ 718229997Sken printf("%s%s command returned errno %d\n", path_str, 719229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 720229997Sken ctl_set_medium_error(&io->scsiio); 721229997Sken ctl_complete_beio(beio); 722229997Sken return; 723229997Sken } 724229997Sken 725229997Sken /* 726229997Sken * If this is a write, we're all done. 727229997Sken * If this is a read, we can now send the data to the user. 728229997Sken */ 729267537Smav if (ARGS(io)->flags & (CTL_LLF_WRITE | CTL_LLF_VERIFY)) { 730229997Sken ctl_set_success(&io->scsiio); 731229997Sken ctl_complete_beio(beio); 732229997Sken } else { 733229997Sken#ifdef CTL_TIME_IO 734229997Sken getbintime(&io->io_hdr.dma_start_bt); 735229997Sken#endif 736229997Sken ctl_datamove(io); 737229997Sken } 738229997Sken} 739229997Sken 740229997Skenstatic void 741229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 742229997Sken struct ctl_be_block_io *beio) 743229997Sken{ 744229997Sken struct bio *bio; 745229997Sken union ctl_io *io; 746229997Sken struct ctl_be_block_devdata *dev_data; 747229997Sken 748229997Sken dev_data = &be_lun->backend.dev; 749229997Sken io = beio->io; 750229997Sken 751229997Sken DPRINTF("entered\n"); 752229997Sken 753229997Sken /* This can't fail, it's a blocking allocation. */ 754229997Sken bio = g_alloc_bio(); 755229997Sken 756229997Sken bio->bio_cmd = BIO_FLUSH; 757229997Sken bio->bio_flags |= BIO_ORDERED; 758229997Sken bio->bio_dev = dev_data->cdev; 759229997Sken bio->bio_offset = 0; 760229997Sken bio->bio_data = 0; 761229997Sken bio->bio_done = ctl_be_block_biodone; 762229997Sken bio->bio_caller1 = beio; 763229997Sken bio->bio_pblkno = 0; 764229997Sken 765229997Sken /* 766229997Sken * We don't need to acquire the LUN lock here, because we are only 767229997Sken * sending one bio, and so there is no other context to synchronize 768229997Sken * with. 769229997Sken */ 770229997Sken beio->num_bios_sent = 1; 771229997Sken beio->send_complete = 1; 772229997Sken 773229997Sken binuptime(&beio->ds_t0); 774229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 775229997Sken 776229997Sken (*dev_data->csw->d_strategy)(bio); 777229997Sken} 778229997Sken 779229997Skenstatic void 780264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 781264274Smav struct ctl_be_block_io *beio, 782264274Smav uint64_t off, uint64_t len, int last) 783264274Smav{ 784264274Smav struct bio *bio; 785264274Smav struct ctl_be_block_devdata *dev_data; 786264296Smav uint64_t maxlen; 787264274Smav 788264274Smav dev_data = &be_lun->backend.dev; 789264296Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 790264274Smav while (len > 0) { 791264274Smav bio = g_alloc_bio(); 792264274Smav bio->bio_cmd = BIO_DELETE; 793264274Smav bio->bio_flags |= beio->bio_flags; 794264274Smav bio->bio_dev = dev_data->cdev; 795264274Smav bio->bio_offset = off; 796264296Smav bio->bio_length = MIN(len, maxlen); 797264274Smav bio->bio_data = 0; 798264274Smav bio->bio_done = ctl_be_block_biodone; 799264274Smav bio->bio_caller1 = beio; 800264296Smav bio->bio_pblkno = off / be_lun->blocksize; 801264274Smav 802264274Smav off += bio->bio_length; 803264274Smav len -= bio->bio_length; 804264274Smav 805264274Smav mtx_lock(&be_lun->lock); 806264274Smav beio->num_bios_sent++; 807264274Smav if (last && len == 0) 808264274Smav beio->send_complete = 1; 809264274Smav mtx_unlock(&be_lun->lock); 810264274Smav 811264274Smav (*dev_data->csw->d_strategy)(bio); 812264274Smav } 813264274Smav} 814264274Smav 815264274Smavstatic void 816264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 817264274Smav struct ctl_be_block_io *beio) 818264274Smav{ 819264274Smav union ctl_io *io; 820264274Smav struct ctl_be_block_devdata *dev_data; 821267515Smav struct ctl_ptr_len_flags *ptrlen; 822264274Smav struct scsi_unmap_desc *buf, *end; 823264274Smav uint64_t len; 824264274Smav 825264274Smav dev_data = &be_lun->backend.dev; 826264274Smav io = beio->io; 827264274Smav 828264274Smav DPRINTF("entered\n"); 829264274Smav 830264274Smav binuptime(&beio->ds_t0); 831264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 832264274Smav 833264274Smav if (beio->io_offset == -1) { 834264274Smav beio->io_len = 0; 835267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 836267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 837267515Smav end = buf + ptrlen->len / sizeof(*buf); 838264274Smav for (; buf < end; buf++) { 839264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 840264274Smav be_lun->blocksize; 841264274Smav beio->io_len += len; 842264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 843264274Smav scsi_8btou64(buf->lba) * be_lun->blocksize, len, 844264283Smav (end - buf < 2) ? TRUE : FALSE); 845264274Smav } 846264274Smav } else 847264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 848264274Smav beio->io_offset, beio->io_len, TRUE); 849264274Smav} 850264274Smav 851264274Smavstatic void 852229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 853229997Sken struct ctl_be_block_io *beio) 854229997Sken{ 855229997Sken int i; 856229997Sken struct bio *bio; 857229997Sken struct ctl_be_block_devdata *dev_data; 858229997Sken off_t cur_offset; 859229997Sken int max_iosize; 860229997Sken 861229997Sken DPRINTF("entered\n"); 862229997Sken 863229997Sken dev_data = &be_lun->backend.dev; 864229997Sken 865229997Sken /* 866229997Sken * We have to limit our I/O size to the maximum supported by the 867229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 868229997Sken * set it properly, use DFLTPHYS. 869229997Sken */ 870229997Sken max_iosize = dev_data->cdev->si_iosize_max; 871229997Sken if (max_iosize < PAGE_SIZE) 872229997Sken max_iosize = DFLTPHYS; 873229997Sken 874229997Sken cur_offset = beio->io_offset; 875229997Sken 876229997Sken /* 877229997Sken * XXX KDM need to accurately reflect the number of I/Os outstanding 878229997Sken * to a device. 879229997Sken */ 880229997Sken binuptime(&beio->ds_t0); 881229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 882229997Sken 883229997Sken for (i = 0; i < beio->num_segs; i++) { 884229997Sken size_t cur_size; 885229997Sken uint8_t *cur_ptr; 886229997Sken 887229997Sken cur_size = beio->sg_segs[i].len; 888229997Sken cur_ptr = beio->sg_segs[i].addr; 889229997Sken 890229997Sken while (cur_size > 0) { 891229997Sken /* This can't fail, it's a blocking allocation. */ 892229997Sken bio = g_alloc_bio(); 893229997Sken 894229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 895229997Sken 896229997Sken bio->bio_cmd = beio->bio_cmd; 897229997Sken bio->bio_flags |= beio->bio_flags; 898229997Sken bio->bio_dev = dev_data->cdev; 899229997Sken bio->bio_caller1 = beio; 900229997Sken bio->bio_length = min(cur_size, max_iosize); 901229997Sken bio->bio_offset = cur_offset; 902229997Sken bio->bio_data = cur_ptr; 903229997Sken bio->bio_done = ctl_be_block_biodone; 904229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 905229997Sken 906229997Sken cur_offset += bio->bio_length; 907229997Sken cur_ptr += bio->bio_length; 908229997Sken cur_size -= bio->bio_length; 909229997Sken 910229997Sken /* 911229997Sken * Make sure we set the complete bit just before we 912229997Sken * issue the last bio so we don't wind up with a 913229997Sken * race. 914229997Sken * 915229997Sken * Use the LUN mutex here instead of a combination 916229997Sken * of atomic variables for simplicity. 917229997Sken * 918229997Sken * XXX KDM we could have a per-IO lock, but that 919229997Sken * would cause additional per-IO setup and teardown 920229997Sken * overhead. Hopefully there won't be too much 921229997Sken * contention on the LUN lock. 922229997Sken */ 923229997Sken mtx_lock(&be_lun->lock); 924229997Sken 925229997Sken beio->num_bios_sent++; 926229997Sken 927229997Sken if ((i == beio->num_segs - 1) 928229997Sken && (cur_size == 0)) 929229997Sken beio->send_complete = 1; 930229997Sken 931229997Sken mtx_unlock(&be_lun->lock); 932229997Sken 933229997Sken (*dev_data->csw->d_strategy)(bio); 934229997Sken } 935229997Sken } 936229997Sken} 937229997Sken 938229997Skenstatic void 939264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 940264274Smav{ 941264274Smav union ctl_io *io; 942264274Smav 943264274Smav io = beio->io; 944264274Smav ctl_free_beio(beio); 945267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 946267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 947267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 948264274Smav ctl_config_write_done(io); 949264274Smav return; 950264274Smav } 951264274Smav 952264274Smav ctl_be_block_config_write(io); 953264274Smav} 954264274Smav 955264274Smavstatic void 956264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 957264274Smav union ctl_io *io) 958264274Smav{ 959264274Smav struct ctl_be_block_io *beio; 960264274Smav struct ctl_be_block_softc *softc; 961267515Smav struct ctl_lba_len_flags *lbalen; 962264274Smav uint64_t len_left, lba; 963264274Smav int i, seglen; 964264274Smav uint8_t *buf, *end; 965264274Smav 966264274Smav DPRINTF("entered\n"); 967264274Smav 968267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 969264274Smav softc = be_lun->softc; 970267537Smav lbalen = ARGS(beio->io); 971264274Smav 972267515Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP) || 973267515Smav (lbalen->flags & SWS_UNMAP && be_lun->unmap == NULL)) { 974264274Smav ctl_free_beio(beio); 975264274Smav ctl_set_invalid_field(&io->scsiio, 976264274Smav /*sks_valid*/ 1, 977264274Smav /*command*/ 1, 978264274Smav /*field*/ 1, 979264274Smav /*bit_valid*/ 0, 980264274Smav /*bit*/ 0); 981264274Smav ctl_config_write_done(io); 982264274Smav return; 983264274Smav } 984264274Smav 985264274Smav /* 986264274Smav * If the I/O came down with an ordered or head of queue tag, set 987264274Smav * the BIO_ORDERED attribute. For head of queue tags, that's 988264274Smav * pretty much the best we can do. 989264274Smav */ 990264274Smav if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 991264274Smav || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 992264274Smav beio->bio_flags = BIO_ORDERED; 993264274Smav 994264274Smav switch (io->scsiio.tag_type) { 995264274Smav case CTL_TAG_ORDERED: 996264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 997264274Smav break; 998264274Smav case CTL_TAG_HEAD_OF_QUEUE: 999264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1000264274Smav break; 1001264274Smav case CTL_TAG_UNTAGGED: 1002264274Smav case CTL_TAG_SIMPLE: 1003264274Smav case CTL_TAG_ACA: 1004264274Smav default: 1005264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1006264274Smav break; 1007264274Smav } 1008264274Smav 1009267515Smav if (lbalen->flags & SWS_UNMAP) { 1010267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1011267515Smav beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1012264274Smav beio->bio_cmd = BIO_DELETE; 1013264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1014264274Smav 1015264274Smav be_lun->unmap(be_lun, beio); 1016264274Smav return; 1017264274Smav } 1018264274Smav 1019264274Smav beio->bio_cmd = BIO_WRITE; 1020264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1021264274Smav 1022264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1023267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1024264274Smav 1025267515Smav len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1026264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1027264274Smav 1028264274Smav /* 1029264274Smav * Setup the S/G entry for this chunk. 1030264274Smav */ 1031264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1032264274Smav seglen -= seglen % be_lun->blocksize; 1033264274Smav beio->sg_segs[i].len = seglen; 1034264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1035264274Smav 1036264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1037264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1038264274Smav 1039264274Smav beio->num_segs++; 1040264274Smav len_left -= seglen; 1041264274Smav 1042264274Smav buf = beio->sg_segs[i].addr; 1043264274Smav end = buf + seglen; 1044264274Smav for (; buf < end; buf += be_lun->blocksize) { 1045264274Smav memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1046267515Smav if (lbalen->flags & SWS_LBDATA) 1047267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1048264274Smav lba++; 1049264274Smav } 1050264274Smav } 1051264274Smav 1052267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1053264274Smav beio->io_len = lba * be_lun->blocksize; 1054264274Smav 1055264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1056264274Smav if (len_left > 0) { 1057267515Smav lbalen->lba += lba; 1058267515Smav lbalen->len -= lba; 1059264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1060264274Smav } 1061264274Smav 1062264274Smav be_lun->dispatch(be_lun, beio); 1063264274Smav} 1064264274Smav 1065264274Smavstatic void 1066264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1067264274Smav union ctl_io *io) 1068264274Smav{ 1069264274Smav struct ctl_be_block_io *beio; 1070264274Smav struct ctl_be_block_softc *softc; 1071267515Smav struct ctl_ptr_len_flags *ptrlen; 1072264274Smav 1073264274Smav DPRINTF("entered\n"); 1074264274Smav 1075267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1076264274Smav softc = be_lun->softc; 1077267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1078264274Smav 1079267515Smav if (ptrlen->flags != 0 || be_lun->unmap == NULL) { 1080264274Smav ctl_free_beio(beio); 1081264274Smav ctl_set_invalid_field(&io->scsiio, 1082264274Smav /*sks_valid*/ 0, 1083264274Smav /*command*/ 1, 1084264274Smav /*field*/ 0, 1085264274Smav /*bit_valid*/ 0, 1086264274Smav /*bit*/ 0); 1087264274Smav ctl_config_write_done(io); 1088264274Smav return; 1089264274Smav } 1090264274Smav 1091264274Smav /* 1092264274Smav * If the I/O came down with an ordered or head of queue tag, set 1093264274Smav * the BIO_ORDERED attribute. For head of queue tags, that's 1094264274Smav * pretty much the best we can do. 1095264274Smav */ 1096264274Smav if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1097264274Smav || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1098264274Smav beio->bio_flags = BIO_ORDERED; 1099264274Smav 1100264274Smav switch (io->scsiio.tag_type) { 1101264274Smav case CTL_TAG_ORDERED: 1102264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1103264274Smav break; 1104264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1105264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1106264274Smav break; 1107264274Smav case CTL_TAG_UNTAGGED: 1108264274Smav case CTL_TAG_SIMPLE: 1109264274Smav case CTL_TAG_ACA: 1110264274Smav default: 1111264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1112264274Smav break; 1113264274Smav } 1114264274Smav 1115264274Smav beio->io_len = 0; 1116264274Smav beio->io_offset = -1; 1117264274Smav 1118264274Smav beio->bio_cmd = BIO_DELETE; 1119264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1120264274Smav 1121267515Smav DPRINTF("UNMAP\n"); 1122264274Smav 1123264274Smav be_lun->unmap(be_lun, beio); 1124264274Smav} 1125264274Smav 1126264274Smavstatic void 1127264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1128264274Smav{ 1129264274Smav union ctl_io *io; 1130264274Smav 1131264274Smav io = beio->io; 1132264274Smav ctl_free_beio(beio); 1133264274Smav ctl_config_write_done(io); 1134264274Smav} 1135264274Smav 1136264274Smavstatic void 1137229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1138229997Sken union ctl_io *io) 1139229997Sken{ 1140229997Sken struct ctl_be_block_io *beio; 1141229997Sken struct ctl_be_block_softc *softc; 1142229997Sken 1143229997Sken DPRINTF("entered\n"); 1144229997Sken 1145229997Sken softc = be_lun->softc; 1146229997Sken beio = ctl_alloc_beio(softc); 1147229997Sken beio->io = io; 1148229997Sken beio->lun = be_lun; 1149264274Smav beio->beio_cont = ctl_be_block_cw_done; 1150267519Smav PRIV(io)->ptr = (void *)beio; 1151229997Sken 1152229997Sken switch (io->scsiio.cdb[0]) { 1153229997Sken case SYNCHRONIZE_CACHE: 1154229997Sken case SYNCHRONIZE_CACHE_16: 1155249194Strasz beio->bio_cmd = BIO_FLUSH; 1156229997Sken beio->ds_trans_type = DEVSTAT_NO_DATA; 1157229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1158229997Sken beio->io_len = 0; 1159229997Sken be_lun->lun_flush(be_lun, beio); 1160229997Sken break; 1161264274Smav case WRITE_SAME_10: 1162264274Smav case WRITE_SAME_16: 1163264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1164264274Smav break; 1165264274Smav case UNMAP: 1166264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1167264274Smav break; 1168229997Sken default: 1169229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1170229997Sken break; 1171229997Sken } 1172229997Sken} 1173229997Sken 1174258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1175258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1176258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1177258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1178229997Sken 1179229997Skenstatic void 1180264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1181264886Smav{ 1182264886Smav struct ctl_be_block_lun *be_lun; 1183264886Smav union ctl_io *io; 1184264886Smav 1185264886Smav io = beio->io; 1186264886Smav be_lun = beio->lun; 1187264886Smav ctl_free_beio(beio); 1188267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1189267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1190267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1191267537Smav ctl_data_submit_done(io); 1192264886Smav return; 1193264886Smav } 1194264886Smav 1195264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1196264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1197264886Smav 1198264886Smav mtx_lock(&be_lun->lock); 1199264886Smav /* 1200264886Smav * XXX KDM make sure that links is okay to use at this point. 1201264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1202264886Smav * or deal with resource allocation here. 1203264886Smav */ 1204264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1205264886Smav mtx_unlock(&be_lun->lock); 1206264886Smav 1207264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1208264886Smav} 1209264886Smav 1210264886Smavstatic void 1211229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1212229997Sken union ctl_io *io) 1213229997Sken{ 1214229997Sken struct ctl_be_block_io *beio; 1215229997Sken struct ctl_be_block_softc *softc; 1216267537Smav struct ctl_lba_len_flags *lbalen; 1217267519Smav struct ctl_ptr_len_flags *bptrlen; 1218267519Smav uint64_t len_left, lbas; 1219229997Sken int i; 1220229997Sken 1221229997Sken softc = be_lun->softc; 1222229997Sken 1223229997Sken DPRINTF("entered\n"); 1224229997Sken 1225267537Smav lbalen = ARGS(io); 1226267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1227267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1228267537Smav } else { 1229229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1230229997Sken } 1231229997Sken 1232229997Sken beio = ctl_alloc_beio(softc); 1233229997Sken beio->io = io; 1234229997Sken beio->lun = be_lun; 1235267519Smav bptrlen = PRIV(io); 1236267519Smav bptrlen->ptr = (void *)beio; 1237229997Sken 1238229997Sken /* 1239229997Sken * If the I/O came down with an ordered or head of queue tag, set 1240229997Sken * the BIO_ORDERED attribute. For head of queue tags, that's 1241229997Sken * pretty much the best we can do. 1242229997Sken * 1243229997Sken * XXX KDM we don't have a great way to easily know about the FUA 1244229997Sken * bit right now (it is decoded in ctl_read_write(), but we don't 1245229997Sken * pass that knowledge to the backend), and in any case we would 1246229997Sken * need to determine how to handle it. 1247229997Sken */ 1248229997Sken if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1249229997Sken || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1250229997Sken beio->bio_flags = BIO_ORDERED; 1251229997Sken 1252229997Sken switch (io->scsiio.tag_type) { 1253229997Sken case CTL_TAG_ORDERED: 1254229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1255229997Sken break; 1256229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1257229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1258229997Sken break; 1259229997Sken case CTL_TAG_UNTAGGED: 1260229997Sken case CTL_TAG_SIMPLE: 1261229997Sken case CTL_TAG_ACA: 1262229997Sken default: 1263229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1264229997Sken break; 1265229997Sken } 1266229997Sken 1267267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1268267537Smav beio->bio_cmd = BIO_WRITE; 1269267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1270267537Smav } else { 1271229997Sken beio->bio_cmd = BIO_READ; 1272229997Sken beio->ds_trans_type = DEVSTAT_READ; 1273229997Sken } 1274229997Sken 1275264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1276229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1277267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1278267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1279267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1280267537Smav else 1281267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1282267537Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1283267519Smav beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1284267519Smav beio->io_len = lbas * be_lun->blocksize; 1285267519Smav bptrlen->len += lbas; 1286229997Sken 1287264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1288264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1289264886Smav i, CTLBLK_MAX_SEGS)); 1290229997Sken 1291229997Sken /* 1292229997Sken * Setup the S/G entry for this chunk. 1293229997Sken */ 1294264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1295229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1296229997Sken 1297229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1298229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1299229997Sken 1300267537Smav /* Set up second segment for compare operation. */ 1301267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1302267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1303267537Smav beio->sg_segs[i].len; 1304267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1305267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1306267537Smav } 1307267537Smav 1308229997Sken beio->num_segs++; 1309229997Sken len_left -= beio->sg_segs[i].len; 1310229997Sken } 1311267519Smav if (bptrlen->len < lbalen->len) 1312264886Smav beio->beio_cont = ctl_be_block_next; 1313264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1314267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1315267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1316267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1317267537Smav else 1318267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1319264886Smav io->scsiio.kern_data_len = beio->io_len; 1320264886Smav io->scsiio.kern_data_resid = 0; 1321264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1322264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1323229997Sken 1324229997Sken /* 1325229997Sken * For the read case, we need to read the data into our buffers and 1326229997Sken * then we can send it back to the user. For the write case, we 1327229997Sken * need to get the data from the user first. 1328229997Sken */ 1329229997Sken if (beio->bio_cmd == BIO_READ) { 1330229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1331229997Sken be_lun->dispatch(be_lun, beio); 1332229997Sken } else { 1333229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1334229997Sken#ifdef CTL_TIME_IO 1335229997Sken getbintime(&io->io_hdr.dma_start_bt); 1336229997Sken#endif 1337229997Sken ctl_datamove(io); 1338229997Sken } 1339229997Sken} 1340229997Sken 1341229997Skenstatic void 1342229997Skenctl_be_block_worker(void *context, int pending) 1343229997Sken{ 1344229997Sken struct ctl_be_block_lun *be_lun; 1345229997Sken struct ctl_be_block_softc *softc; 1346229997Sken union ctl_io *io; 1347229997Sken 1348229997Sken be_lun = (struct ctl_be_block_lun *)context; 1349229997Sken softc = be_lun->softc; 1350229997Sken 1351229997Sken DPRINTF("entered\n"); 1352229997Sken 1353229997Sken mtx_lock(&be_lun->lock); 1354229997Sken for (;;) { 1355229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1356229997Sken if (io != NULL) { 1357229997Sken struct ctl_be_block_io *beio; 1358229997Sken 1359229997Sken DPRINTF("datamove queue\n"); 1360229997Sken 1361229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1362229997Sken ctl_io_hdr, links); 1363229997Sken 1364229997Sken mtx_unlock(&be_lun->lock); 1365229997Sken 1366267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1367229997Sken 1368229997Sken be_lun->dispatch(be_lun, beio); 1369229997Sken 1370229997Sken mtx_lock(&be_lun->lock); 1371229997Sken continue; 1372229997Sken } 1373229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1374229997Sken if (io != NULL) { 1375229997Sken 1376229997Sken DPRINTF("config write queue\n"); 1377229997Sken 1378229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1379229997Sken ctl_io_hdr, links); 1380229997Sken 1381229997Sken mtx_unlock(&be_lun->lock); 1382229997Sken 1383229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1384229997Sken 1385229997Sken mtx_lock(&be_lun->lock); 1386229997Sken continue; 1387229997Sken } 1388229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1389229997Sken if (io != NULL) { 1390229997Sken DPRINTF("input queue\n"); 1391229997Sken 1392229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1393229997Sken ctl_io_hdr, links); 1394229997Sken mtx_unlock(&be_lun->lock); 1395229997Sken 1396229997Sken /* 1397229997Sken * We must drop the lock, since this routine and 1398229997Sken * its children may sleep. 1399229997Sken */ 1400229997Sken ctl_be_block_dispatch(be_lun, io); 1401229997Sken 1402229997Sken mtx_lock(&be_lun->lock); 1403229997Sken continue; 1404229997Sken } 1405229997Sken 1406229997Sken /* 1407229997Sken * If we get here, there is no work left in the queues, so 1408229997Sken * just break out and let the task queue go to sleep. 1409229997Sken */ 1410229997Sken break; 1411229997Sken } 1412229997Sken mtx_unlock(&be_lun->lock); 1413229997Sken} 1414229997Sken 1415229997Sken/* 1416229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1417229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1418229997Sken * thread. 1419229997Sken */ 1420229997Skenstatic int 1421229997Skenctl_be_block_submit(union ctl_io *io) 1422229997Sken{ 1423229997Sken struct ctl_be_block_lun *be_lun; 1424229997Sken struct ctl_be_lun *ctl_be_lun; 1425229997Sken 1426229997Sken DPRINTF("entered\n"); 1427229997Sken 1428229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1429229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1430229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1431229997Sken 1432229997Sken /* 1433229997Sken * Make sure we only get SCSI I/O. 1434229997Sken */ 1435229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1436229997Sken "%#x) encountered", io->io_hdr.io_type)); 1437229997Sken 1438267519Smav PRIV(io)->len = 0; 1439267519Smav 1440229997Sken mtx_lock(&be_lun->lock); 1441229997Sken /* 1442229997Sken * XXX KDM make sure that links is okay to use at this point. 1443229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1444229997Sken * or deal with resource allocation here. 1445229997Sken */ 1446229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1447229997Sken mtx_unlock(&be_lun->lock); 1448229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1449229997Sken 1450267514Smav return (CTL_RETVAL_COMPLETE); 1451229997Sken} 1452229997Sken 1453229997Skenstatic int 1454229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1455229997Sken int flag, struct thread *td) 1456229997Sken{ 1457229997Sken struct ctl_be_block_softc *softc; 1458229997Sken int error; 1459229997Sken 1460229997Sken softc = &backend_block_softc; 1461229997Sken 1462229997Sken error = 0; 1463229997Sken 1464229997Sken switch (cmd) { 1465229997Sken case CTL_LUN_REQ: { 1466229997Sken struct ctl_lun_req *lun_req; 1467229997Sken 1468229997Sken lun_req = (struct ctl_lun_req *)addr; 1469229997Sken 1470229997Sken switch (lun_req->reqtype) { 1471229997Sken case CTL_LUNREQ_CREATE: 1472229997Sken error = ctl_be_block_create(softc, lun_req); 1473229997Sken break; 1474229997Sken case CTL_LUNREQ_RM: 1475229997Sken error = ctl_be_block_rm(softc, lun_req); 1476229997Sken break; 1477232604Strasz case CTL_LUNREQ_MODIFY: 1478232604Strasz error = ctl_be_block_modify(softc, lun_req); 1479232604Strasz break; 1480229997Sken default: 1481229997Sken lun_req->status = CTL_LUN_ERROR; 1482229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1483229997Sken "%s: invalid LUN request type %d", __func__, 1484229997Sken lun_req->reqtype); 1485229997Sken break; 1486229997Sken } 1487229997Sken break; 1488229997Sken } 1489229997Sken default: 1490229997Sken error = ENOTTY; 1491229997Sken break; 1492229997Sken } 1493229997Sken 1494229997Sken return (error); 1495229997Sken} 1496229997Sken 1497229997Skenstatic int 1498229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1499229997Sken{ 1500229997Sken struct ctl_be_block_filedata *file_data; 1501229997Sken struct ctl_lun_create_params *params; 1502229997Sken struct vattr vattr; 1503229997Sken int error; 1504229997Sken 1505229997Sken error = 0; 1506229997Sken file_data = &be_lun->backend.file; 1507229997Sken params = &req->reqdata.create; 1508229997Sken 1509229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1510229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1511229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1512229997Sken 1513229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1514229997Sken if (error != 0) { 1515229997Sken snprintf(req->error_str, sizeof(req->error_str), 1516229997Sken "error calling VOP_GETATTR() for file %s", 1517229997Sken be_lun->dev_path); 1518229997Sken return (error); 1519229997Sken } 1520229997Sken 1521229997Sken /* 1522229997Sken * Verify that we have the ability to upgrade to exclusive 1523229997Sken * access on this file so we can trap errors at open instead 1524229997Sken * of reporting them during first access. 1525229997Sken */ 1526229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1527229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1528229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1529229997Sken error = EBADF; 1530229997Sken snprintf(req->error_str, sizeof(req->error_str), 1531229997Sken "error locking file %s", be_lun->dev_path); 1532229997Sken return (error); 1533229997Sken } 1534229997Sken } 1535229997Sken 1536229997Sken 1537229997Sken file_data->cred = crhold(curthread->td_ucred); 1538232604Strasz if (params->lun_size_bytes != 0) 1539232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1540232604Strasz else 1541232604Strasz be_lun->size_bytes = vattr.va_size; 1542229997Sken /* 1543229997Sken * We set the multi thread flag for file operations because all 1544229997Sken * filesystems (in theory) are capable of allowing multiple readers 1545229997Sken * of a file at once. So we want to get the maximum possible 1546229997Sken * concurrency. 1547229997Sken */ 1548229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1549229997Sken 1550229997Sken /* 1551229997Sken * XXX KDM vattr.va_blocksize may be larger than 512 bytes here. 1552229997Sken * With ZFS, it is 131072 bytes. Block sizes that large don't work 1553229997Sken * with disklabel and UFS on FreeBSD at least. Large block sizes 1554229997Sken * may not work with other OSes as well. So just export a sector 1555229997Sken * size of 512 bytes, which should work with any OS or 1556229997Sken * application. Since our backing is a file, any block size will 1557229997Sken * work fine for the backing store. 1558229997Sken */ 1559229997Sken#if 0 1560229997Sken be_lun->blocksize= vattr.va_blocksize; 1561229997Sken#endif 1562229997Sken if (params->blocksize_bytes != 0) 1563229997Sken be_lun->blocksize = params->blocksize_bytes; 1564229997Sken else 1565229997Sken be_lun->blocksize = 512; 1566229997Sken 1567229997Sken /* 1568229997Sken * Sanity check. The media size has to be at least one 1569229997Sken * sector long. 1570229997Sken */ 1571229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1572229997Sken error = EINVAL; 1573229997Sken snprintf(req->error_str, sizeof(req->error_str), 1574229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1575229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1576229997Sken } 1577229997Sken return (error); 1578229997Sken} 1579229997Sken 1580229997Skenstatic int 1581229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1582229997Sken{ 1583229997Sken struct ctl_lun_create_params *params; 1584229997Sken struct vattr vattr; 1585229997Sken struct cdev *dev; 1586229997Sken struct cdevsw *devsw; 1587229997Sken int error; 1588264191Smav off_t ps, pss, po, pos; 1589229997Sken 1590229997Sken params = &req->reqdata.create; 1591229997Sken 1592229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1593229997Sken be_lun->dispatch = ctl_be_block_dispatch_dev; 1594229997Sken be_lun->lun_flush = ctl_be_block_flush_dev; 1595264274Smav be_lun->unmap = ctl_be_block_unmap_dev; 1596229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1597229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1598229997Sken &be_lun->backend.dev.dev_ref); 1599229997Sken if (be_lun->backend.dev.csw == NULL) 1600229997Sken panic("Unable to retrieve device switch"); 1601229997Sken 1602229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1603229997Sken if (error) { 1604229997Sken snprintf(req->error_str, sizeof(req->error_str), 1605229997Sken "%s: error getting vnode attributes for device %s", 1606229997Sken __func__, be_lun->dev_path); 1607229997Sken return (error); 1608229997Sken } 1609229997Sken 1610229997Sken dev = be_lun->vn->v_rdev; 1611229997Sken devsw = dev->si_devsw; 1612229997Sken if (!devsw->d_ioctl) { 1613229997Sken snprintf(req->error_str, sizeof(req->error_str), 1614229997Sken "%s: no d_ioctl for device %s!", __func__, 1615229997Sken be_lun->dev_path); 1616229997Sken return (ENODEV); 1617229997Sken } 1618229997Sken 1619229997Sken error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1620229997Sken (caddr_t)&be_lun->blocksize, FREAD, 1621229997Sken curthread); 1622229997Sken if (error) { 1623229997Sken snprintf(req->error_str, sizeof(req->error_str), 1624229997Sken "%s: error %d returned for DIOCGSECTORSIZE ioctl " 1625229997Sken "on %s!", __func__, error, be_lun->dev_path); 1626229997Sken return (error); 1627229997Sken } 1628229997Sken 1629229997Sken /* 1630229997Sken * If the user has asked for a blocksize that is greater than the 1631229997Sken * backing device's blocksize, we can do it only if the blocksize 1632229997Sken * the user is asking for is an even multiple of the underlying 1633229997Sken * device's blocksize. 1634229997Sken */ 1635229997Sken if ((params->blocksize_bytes != 0) 1636229997Sken && (params->blocksize_bytes > be_lun->blocksize)) { 1637229997Sken uint32_t bs_multiple, tmp_blocksize; 1638229997Sken 1639229997Sken bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1640229997Sken 1641229997Sken tmp_blocksize = bs_multiple * be_lun->blocksize; 1642229997Sken 1643229997Sken if (tmp_blocksize == params->blocksize_bytes) { 1644229997Sken be_lun->blocksize = params->blocksize_bytes; 1645229997Sken } else { 1646229997Sken snprintf(req->error_str, sizeof(req->error_str), 1647229997Sken "%s: requested blocksize %u is not an even " 1648229997Sken "multiple of backing device blocksize %u", 1649229997Sken __func__, params->blocksize_bytes, 1650229997Sken be_lun->blocksize); 1651229997Sken return (EINVAL); 1652229997Sken 1653229997Sken } 1654229997Sken } else if ((params->blocksize_bytes != 0) 1655229997Sken && (params->blocksize_bytes != be_lun->blocksize)) { 1656229997Sken snprintf(req->error_str, sizeof(req->error_str), 1657229997Sken "%s: requested blocksize %u < backing device " 1658229997Sken "blocksize %u", __func__, params->blocksize_bytes, 1659229997Sken be_lun->blocksize); 1660229997Sken return (EINVAL); 1661229997Sken } 1662229997Sken 1663229997Sken error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1664229997Sken (caddr_t)&be_lun->size_bytes, FREAD, 1665229997Sken curthread); 1666229997Sken if (error) { 1667229997Sken snprintf(req->error_str, sizeof(req->error_str), 1668232604Strasz "%s: error %d returned for DIOCGMEDIASIZE " 1669232604Strasz " ioctl on %s!", __func__, error, 1670232604Strasz be_lun->dev_path); 1671229997Sken return (error); 1672229997Sken } 1673229997Sken 1674232604Strasz if (params->lun_size_bytes != 0) { 1675232604Strasz if (params->lun_size_bytes > be_lun->size_bytes) { 1676232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1677232604Strasz "%s: requested LUN size %ju > backing device " 1678232604Strasz "size %ju", __func__, 1679232604Strasz (uintmax_t)params->lun_size_bytes, 1680232604Strasz (uintmax_t)be_lun->size_bytes); 1681232604Strasz return (EINVAL); 1682232604Strasz } 1683232604Strasz 1684232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1685232604Strasz } 1686232604Strasz 1687264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1688264191Smav (caddr_t)&ps, FREAD, curthread); 1689264191Smav if (error) 1690264191Smav ps = po = 0; 1691264191Smav else { 1692264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1693264191Smav (caddr_t)&po, FREAD, curthread); 1694264191Smav if (error) 1695264191Smav po = 0; 1696264191Smav } 1697264191Smav pss = ps / be_lun->blocksize; 1698264191Smav pos = po / be_lun->blocksize; 1699264191Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1700264191Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1701264191Smav be_lun->pblockexp = fls(pss) - 1; 1702264191Smav be_lun->pblockoff = (pss - pos) % pss; 1703264191Smav } 1704264191Smav 1705229997Sken return (0); 1706229997Sken} 1707229997Sken 1708229997Skenstatic int 1709229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 1710229997Sken{ 1711229997Sken DROP_GIANT(); 1712229997Sken if (be_lun->vn) { 1713229997Sken int flags = FREAD | FWRITE; 1714229997Sken 1715229997Sken switch (be_lun->dev_type) { 1716229997Sken case CTL_BE_BLOCK_DEV: 1717229997Sken if (be_lun->backend.dev.csw) { 1718229997Sken dev_relthread(be_lun->backend.dev.cdev, 1719229997Sken be_lun->backend.dev.dev_ref); 1720229997Sken be_lun->backend.dev.csw = NULL; 1721229997Sken be_lun->backend.dev.cdev = NULL; 1722229997Sken } 1723229997Sken break; 1724229997Sken case CTL_BE_BLOCK_FILE: 1725229997Sken break; 1726229997Sken case CTL_BE_BLOCK_NONE: 1727258871Strasz break; 1728229997Sken default: 1729229997Sken panic("Unexpected backend type."); 1730229997Sken break; 1731229997Sken } 1732229997Sken 1733229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1734229997Sken be_lun->vn = NULL; 1735229997Sken 1736229997Sken switch (be_lun->dev_type) { 1737229997Sken case CTL_BE_BLOCK_DEV: 1738229997Sken break; 1739229997Sken case CTL_BE_BLOCK_FILE: 1740229997Sken if (be_lun->backend.file.cred != NULL) { 1741229997Sken crfree(be_lun->backend.file.cred); 1742229997Sken be_lun->backend.file.cred = NULL; 1743229997Sken } 1744229997Sken break; 1745229997Sken case CTL_BE_BLOCK_NONE: 1746258871Strasz break; 1747229997Sken default: 1748229997Sken panic("Unexpected backend type."); 1749229997Sken break; 1750229997Sken } 1751229997Sken } 1752229997Sken PICKUP_GIANT(); 1753229997Sken 1754229997Sken return (0); 1755229997Sken} 1756229997Sken 1757229997Skenstatic int 1758229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 1759229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1760229997Sken{ 1761229997Sken struct nameidata nd; 1762229997Sken int flags; 1763229997Sken int error; 1764229997Sken 1765229997Sken /* 1766229997Sken * XXX KDM allow a read-only option? 1767229997Sken */ 1768229997Sken flags = FREAD | FWRITE; 1769229997Sken error = 0; 1770229997Sken 1771229997Sken if (rootvnode == NULL) { 1772229997Sken snprintf(req->error_str, sizeof(req->error_str), 1773229997Sken "%s: Root filesystem is not mounted", __func__); 1774229997Sken return (1); 1775229997Sken } 1776229997Sken 1777229997Sken if (!curthread->td_proc->p_fd->fd_cdir) { 1778229997Sken curthread->td_proc->p_fd->fd_cdir = rootvnode; 1779229997Sken VREF(rootvnode); 1780229997Sken } 1781229997Sken if (!curthread->td_proc->p_fd->fd_rdir) { 1782229997Sken curthread->td_proc->p_fd->fd_rdir = rootvnode; 1783229997Sken VREF(rootvnode); 1784229997Sken } 1785229997Sken if (!curthread->td_proc->p_fd->fd_jdir) { 1786229997Sken curthread->td_proc->p_fd->fd_jdir = rootvnode; 1787229997Sken VREF(rootvnode); 1788229997Sken } 1789229997Sken 1790229997Sken again: 1791229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1792229997Sken error = vn_open(&nd, &flags, 0, NULL); 1793229997Sken if (error) { 1794229997Sken /* 1795229997Sken * This is the only reasonable guess we can make as far as 1796229997Sken * path if the user doesn't give us a fully qualified path. 1797229997Sken * If they want to specify a file, they need to specify the 1798229997Sken * full path. 1799229997Sken */ 1800229997Sken if (be_lun->dev_path[0] != '/') { 1801229997Sken char *dev_path = "/dev/"; 1802229997Sken char *dev_name; 1803229997Sken 1804229997Sken /* Try adding device path at beginning of name */ 1805229997Sken dev_name = malloc(strlen(be_lun->dev_path) 1806229997Sken + strlen(dev_path) + 1, 1807229997Sken M_CTLBLK, M_WAITOK); 1808229997Sken if (dev_name) { 1809229997Sken sprintf(dev_name, "%s%s", dev_path, 1810229997Sken be_lun->dev_path); 1811229997Sken free(be_lun->dev_path, M_CTLBLK); 1812229997Sken be_lun->dev_path = dev_name; 1813229997Sken goto again; 1814229997Sken } 1815229997Sken } 1816229997Sken snprintf(req->error_str, sizeof(req->error_str), 1817229997Sken "%s: error opening %s", __func__, be_lun->dev_path); 1818229997Sken return (error); 1819229997Sken } 1820229997Sken 1821229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 1822229997Sken 1823229997Sken be_lun->vn = nd.ni_vp; 1824229997Sken 1825229997Sken /* We only support disks and files. */ 1826229997Sken if (vn_isdisk(be_lun->vn, &error)) { 1827229997Sken error = ctl_be_block_open_dev(be_lun, req); 1828229997Sken } else if (be_lun->vn->v_type == VREG) { 1829229997Sken error = ctl_be_block_open_file(be_lun, req); 1830229997Sken } else { 1831229997Sken error = EINVAL; 1832229997Sken snprintf(req->error_str, sizeof(req->error_str), 1833258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 1834229997Sken } 1835229997Sken VOP_UNLOCK(be_lun->vn, 0); 1836229997Sken 1837229997Sken if (error != 0) { 1838229997Sken ctl_be_block_close(be_lun); 1839229997Sken return (error); 1840229997Sken } 1841229997Sken 1842229997Sken be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1843229997Sken be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1844229997Sken 1845229997Sken return (0); 1846229997Sken} 1847229997Sken 1848229997Skenstatic int 1849229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1850229997Sken{ 1851229997Sken struct ctl_be_block_lun *be_lun; 1852229997Sken struct ctl_lun_create_params *params; 1853267481Smav char num_thread_str[16]; 1854229997Sken char tmpstr[32]; 1855267481Smav char *value; 1856264274Smav int retval, num_threads, unmap; 1857267481Smav int tmp_num_threads; 1858229997Sken 1859229997Sken params = &req->reqdata.create; 1860229997Sken retval = 0; 1861229997Sken 1862229997Sken num_threads = cbb_num_threads; 1863229997Sken 1864229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1865229997Sken 1866229997Sken be_lun->softc = softc; 1867229997Sken STAILQ_INIT(&be_lun->input_queue); 1868229997Sken STAILQ_INIT(&be_lun->config_write_queue); 1869229997Sken STAILQ_INIT(&be_lun->datamove_queue); 1870229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1871229997Sken mtx_init(&be_lun->lock, be_lun->lunname, NULL, MTX_DEF); 1872267481Smav ctl_init_opts(&be_lun->ctl_be_lun, req); 1873229997Sken 1874264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1875256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1876229997Sken 1877229997Sken if (be_lun->lun_zone == NULL) { 1878229997Sken snprintf(req->error_str, sizeof(req->error_str), 1879229997Sken "%s: error allocating UMA zone", __func__); 1880229997Sken goto bailout_error; 1881229997Sken } 1882229997Sken 1883229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1884229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 1885229997Sken else 1886229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 1887229997Sken 1888229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1889267499Smav value = ctl_get_opt(&be_lun->ctl_be_lun, "file"); 1890267499Smav if (value == NULL) { 1891229997Sken snprintf(req->error_str, sizeof(req->error_str), 1892229997Sken "%s: no file argument specified", __func__); 1893229997Sken goto bailout_error; 1894229997Sken } 1895267499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 1896229997Sken 1897229997Sken retval = ctl_be_block_open(softc, be_lun, req); 1898229997Sken if (retval != 0) { 1899229997Sken retval = 0; 1900229997Sken goto bailout_error; 1901229997Sken } 1902229997Sken 1903229997Sken /* 1904229997Sken * Tell the user the size of the file/device. 1905229997Sken */ 1906229997Sken params->lun_size_bytes = be_lun->size_bytes; 1907229997Sken 1908229997Sken /* 1909229997Sken * The maximum LBA is the size - 1. 1910229997Sken */ 1911229997Sken be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 1912229997Sken } else { 1913229997Sken /* 1914229997Sken * For processor devices, we don't have any size. 1915229997Sken */ 1916229997Sken be_lun->blocksize = 0; 1917264191Smav be_lun->pblockexp = 0; 1918264191Smav be_lun->pblockoff = 0; 1919229997Sken be_lun->size_blocks = 0; 1920229997Sken be_lun->size_bytes = 0; 1921229997Sken be_lun->ctl_be_lun.maxlba = 0; 1922229997Sken params->lun_size_bytes = 0; 1923229997Sken 1924229997Sken /* 1925229997Sken * Default to just 1 thread for processor devices. 1926229997Sken */ 1927229997Sken num_threads = 1; 1928229997Sken } 1929229997Sken 1930229997Sken /* 1931229997Sken * XXX This searching loop might be refactored to be combined with 1932229997Sken * the loop above, 1933229997Sken */ 1934267481Smav value = ctl_get_opt(&be_lun->ctl_be_lun, "num_threads"); 1935267481Smav if (value != NULL) { 1936267481Smav tmp_num_threads = strtol(value, NULL, 0); 1937229997Sken 1938267481Smav /* 1939267481Smav * We don't let the user specify less than one 1940267481Smav * thread, but hope he's clueful enough not to 1941267481Smav * specify 1000 threads. 1942267481Smav */ 1943267481Smav if (tmp_num_threads < 1) { 1944267481Smav snprintf(req->error_str, sizeof(req->error_str), 1945267481Smav "%s: invalid number of threads %s", 1946267481Smav __func__, num_thread_str); 1947267481Smav goto bailout_error; 1948229997Sken } 1949267481Smav num_threads = tmp_num_threads; 1950229997Sken } 1951267481Smav unmap = 0; 1952267481Smav value = ctl_get_opt(&be_lun->ctl_be_lun, "unmap"); 1953267481Smav if (value != NULL && strcmp(value, "on") == 0) 1954267481Smav unmap = 1; 1955229997Sken 1956229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 1957229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 1958264274Smav if (unmap) 1959264274Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 1960229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 1961229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 1962264191Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 1963264191Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 1964229997Sken /* Tell the user the blocksize we ended up using */ 1965229997Sken params->blocksize_bytes = be_lun->blocksize; 1966229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 1967229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 1968229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 1969229997Sken } else 1970229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 1971229997Sken 1972229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 1973229997Sken be_lun->ctl_be_lun.lun_config_status = 1974229997Sken ctl_be_block_lun_config_status; 1975229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 1976229997Sken 1977229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 1978229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 1979229997Sken softc->num_luns); 1980229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 1981229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1982229997Sken sizeof(tmpstr))); 1983229997Sken 1984229997Sken /* Tell the user what we used for a serial number */ 1985229997Sken strncpy((char *)params->serial_num, tmpstr, 1986229997Sken ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 1987229997Sken } else { 1988229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 1989229997Sken params->serial_num, 1990229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1991229997Sken sizeof(params->serial_num))); 1992229997Sken } 1993229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 1994229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 1995229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 1996229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1997229997Sken sizeof(tmpstr))); 1998229997Sken 1999229997Sken /* Tell the user what we used for a device ID */ 2000229997Sken strncpy((char *)params->device_id, tmpstr, 2001229997Sken ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2002229997Sken } else { 2003229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 2004229997Sken params->device_id, 2005229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2006229997Sken sizeof(params->device_id))); 2007229997Sken } 2008229997Sken 2009229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2010229997Sken 2011229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2012229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2013229997Sken 2014229997Sken if (be_lun->io_taskqueue == NULL) { 2015229997Sken snprintf(req->error_str, sizeof(req->error_str), 2016229997Sken "%s: Unable to create taskqueue", __func__); 2017229997Sken goto bailout_error; 2018229997Sken } 2019229997Sken 2020229997Sken /* 2021229997Sken * Note that we start the same number of threads by default for 2022229997Sken * both the file case and the block device case. For the file 2023229997Sken * case, we need multiple threads to allow concurrency, because the 2024229997Sken * vnode interface is designed to be a blocking interface. For the 2025229997Sken * block device case, ZFS zvols at least will block the caller's 2026229997Sken * context in many instances, and so we need multiple threads to 2027229997Sken * overcome that problem. Other block devices don't need as many 2028229997Sken * threads, but they shouldn't cause too many problems. 2029229997Sken * 2030229997Sken * If the user wants to just have a single thread for a block 2031229997Sken * device, he can specify that when the LUN is created, or change 2032229997Sken * the tunable/sysctl to alter the default number of threads. 2033229997Sken */ 2034229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2035229997Sken /*num threads*/num_threads, 2036229997Sken /*priority*/PWAIT, 2037229997Sken /*thread name*/ 2038229997Sken "%s taskq", be_lun->lunname); 2039229997Sken 2040229997Sken if (retval != 0) 2041229997Sken goto bailout_error; 2042229997Sken 2043229997Sken be_lun->num_threads = num_threads; 2044229997Sken 2045229997Sken mtx_lock(&softc->lock); 2046229997Sken softc->num_luns++; 2047229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2048229997Sken 2049229997Sken mtx_unlock(&softc->lock); 2050229997Sken 2051229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 2052229997Sken if (retval != 0) { 2053229997Sken mtx_lock(&softc->lock); 2054229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2055229997Sken links); 2056229997Sken softc->num_luns--; 2057229997Sken mtx_unlock(&softc->lock); 2058229997Sken snprintf(req->error_str, sizeof(req->error_str), 2059229997Sken "%s: ctl_add_lun() returned error %d, see dmesg for " 2060229997Sken "details", __func__, retval); 2061229997Sken retval = 0; 2062229997Sken goto bailout_error; 2063229997Sken } 2064229997Sken 2065229997Sken mtx_lock(&softc->lock); 2066229997Sken 2067229997Sken /* 2068229997Sken * Tell the config_status routine that we're waiting so it won't 2069229997Sken * clean up the LUN in the event of an error. 2070229997Sken */ 2071229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2072229997Sken 2073229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2074229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2075229997Sken if (retval == EINTR) 2076229997Sken break; 2077229997Sken } 2078229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2079229997Sken 2080229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2081229997Sken snprintf(req->error_str, sizeof(req->error_str), 2082229997Sken "%s: LUN configuration error, see dmesg for details", 2083229997Sken __func__); 2084229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2085229997Sken links); 2086229997Sken softc->num_luns--; 2087229997Sken mtx_unlock(&softc->lock); 2088229997Sken goto bailout_error; 2089229997Sken } else { 2090229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2091229997Sken } 2092229997Sken 2093229997Sken mtx_unlock(&softc->lock); 2094229997Sken 2095229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2096229997Sken be_lun->blocksize, 2097229997Sken DEVSTAT_ALL_SUPPORTED, 2098229997Sken be_lun->ctl_be_lun.lun_type 2099229997Sken | DEVSTAT_TYPE_IF_OTHER, 2100229997Sken DEVSTAT_PRIORITY_OTHER); 2101229997Sken 2102229997Sken 2103229997Sken req->status = CTL_LUN_OK; 2104229997Sken 2105229997Sken return (retval); 2106229997Sken 2107229997Skenbailout_error: 2108229997Sken req->status = CTL_LUN_ERROR; 2109229997Sken 2110267429Smav if (be_lun->io_taskqueue != NULL) 2111267429Smav taskqueue_free(be_lun->io_taskqueue); 2112229997Sken ctl_be_block_close(be_lun); 2113267429Smav if (be_lun->dev_path != NULL) 2114267429Smav free(be_lun->dev_path, M_CTLBLK); 2115267429Smav if (be_lun->lun_zone != NULL) 2116267429Smav uma_zdestroy(be_lun->lun_zone); 2117267481Smav ctl_free_opts(&be_lun->ctl_be_lun); 2118267429Smav mtx_destroy(&be_lun->lock); 2119229997Sken free(be_lun, M_CTLBLK); 2120229997Sken 2121229997Sken return (retval); 2122229997Sken} 2123229997Sken 2124229997Skenstatic int 2125229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2126229997Sken{ 2127229997Sken struct ctl_lun_rm_params *params; 2128229997Sken struct ctl_be_block_lun *be_lun; 2129229997Sken int retval; 2130229997Sken 2131229997Sken params = &req->reqdata.rm; 2132229997Sken 2133229997Sken mtx_lock(&softc->lock); 2134229997Sken 2135229997Sken be_lun = NULL; 2136229997Sken 2137229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2138229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2139229997Sken break; 2140229997Sken } 2141229997Sken mtx_unlock(&softc->lock); 2142229997Sken 2143229997Sken if (be_lun == NULL) { 2144229997Sken snprintf(req->error_str, sizeof(req->error_str), 2145229997Sken "%s: LUN %u is not managed by the block backend", 2146229997Sken __func__, params->lun_id); 2147229997Sken goto bailout_error; 2148229997Sken } 2149229997Sken 2150229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2151229997Sken 2152229997Sken if (retval != 0) { 2153229997Sken snprintf(req->error_str, sizeof(req->error_str), 2154229997Sken "%s: error %d returned from ctl_disable_lun() for " 2155229997Sken "LUN %d", __func__, retval, params->lun_id); 2156229997Sken goto bailout_error; 2157229997Sken 2158229997Sken } 2159229997Sken 2160229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2161229997Sken if (retval != 0) { 2162229997Sken snprintf(req->error_str, sizeof(req->error_str), 2163229997Sken "%s: error %d returned from ctl_invalidate_lun() for " 2164229997Sken "LUN %d", __func__, retval, params->lun_id); 2165229997Sken goto bailout_error; 2166229997Sken } 2167229997Sken 2168229997Sken mtx_lock(&softc->lock); 2169229997Sken 2170229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2171229997Sken 2172229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2173229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2174229997Sken if (retval == EINTR) 2175229997Sken break; 2176229997Sken } 2177229997Sken 2178229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2179229997Sken 2180229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2181229997Sken snprintf(req->error_str, sizeof(req->error_str), 2182229997Sken "%s: interrupted waiting for LUN to be freed", 2183229997Sken __func__); 2184229997Sken mtx_unlock(&softc->lock); 2185229997Sken goto bailout_error; 2186229997Sken } 2187229997Sken 2188229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2189229997Sken 2190229997Sken softc->num_luns--; 2191229997Sken mtx_unlock(&softc->lock); 2192229997Sken 2193229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2194229997Sken 2195229997Sken taskqueue_free(be_lun->io_taskqueue); 2196229997Sken 2197229997Sken ctl_be_block_close(be_lun); 2198229997Sken 2199229997Sken if (be_lun->disk_stats != NULL) 2200229997Sken devstat_remove_entry(be_lun->disk_stats); 2201229997Sken 2202229997Sken uma_zdestroy(be_lun->lun_zone); 2203229997Sken 2204267481Smav ctl_free_opts(&be_lun->ctl_be_lun); 2205229997Sken free(be_lun->dev_path, M_CTLBLK); 2206229997Sken 2207229997Sken free(be_lun, M_CTLBLK); 2208229997Sken 2209229997Sken req->status = CTL_LUN_OK; 2210229997Sken 2211229997Sken return (0); 2212229997Sken 2213229997Skenbailout_error: 2214229997Sken 2215229997Sken req->status = CTL_LUN_ERROR; 2216229997Sken 2217229997Sken return (0); 2218229997Sken} 2219229997Sken 2220232604Straszstatic int 2221232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2222232604Strasz struct ctl_lun_req *req) 2223232604Strasz{ 2224232604Strasz struct vattr vattr; 2225232604Strasz int error; 2226232604Strasz struct ctl_lun_modify_params *params; 2227232604Strasz 2228232604Strasz params = &req->reqdata.modify; 2229232604Strasz 2230232604Strasz if (params->lun_size_bytes != 0) { 2231232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2232232604Strasz } else { 2233232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2234232604Strasz if (error != 0) { 2235232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2236232604Strasz "error calling VOP_GETATTR() for file %s", 2237232604Strasz be_lun->dev_path); 2238232604Strasz return (error); 2239232604Strasz } 2240232604Strasz 2241232604Strasz be_lun->size_bytes = vattr.va_size; 2242232604Strasz } 2243232604Strasz 2244232604Strasz return (0); 2245232604Strasz} 2246232604Strasz 2247232604Straszstatic int 2248232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2249232604Strasz struct ctl_lun_req *req) 2250232604Strasz{ 2251232604Strasz struct cdev *dev; 2252232604Strasz struct cdevsw *devsw; 2253232604Strasz int error; 2254232604Strasz struct ctl_lun_modify_params *params; 2255232604Strasz uint64_t size_bytes; 2256232604Strasz 2257232604Strasz params = &req->reqdata.modify; 2258232604Strasz 2259232604Strasz dev = be_lun->vn->v_rdev; 2260232604Strasz devsw = dev->si_devsw; 2261232604Strasz if (!devsw->d_ioctl) { 2262232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2263232604Strasz "%s: no d_ioctl for device %s!", __func__, 2264232604Strasz be_lun->dev_path); 2265232604Strasz return (ENODEV); 2266232604Strasz } 2267232604Strasz 2268232604Strasz error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 2269232604Strasz (caddr_t)&size_bytes, FREAD, 2270232604Strasz curthread); 2271232604Strasz if (error) { 2272232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2273232604Strasz "%s: error %d returned for DIOCGMEDIASIZE ioctl " 2274232604Strasz "on %s!", __func__, error, be_lun->dev_path); 2275232604Strasz return (error); 2276232604Strasz } 2277232604Strasz 2278232604Strasz if (params->lun_size_bytes != 0) { 2279232604Strasz if (params->lun_size_bytes > size_bytes) { 2280232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2281232604Strasz "%s: requested LUN size %ju > backing device " 2282232604Strasz "size %ju", __func__, 2283232604Strasz (uintmax_t)params->lun_size_bytes, 2284232604Strasz (uintmax_t)size_bytes); 2285232604Strasz return (EINVAL); 2286232604Strasz } 2287232604Strasz 2288232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2289232604Strasz } else { 2290232604Strasz be_lun->size_bytes = size_bytes; 2291232604Strasz } 2292232604Strasz 2293232604Strasz return (0); 2294232604Strasz} 2295232604Strasz 2296232604Straszstatic int 2297232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2298232604Strasz{ 2299232604Strasz struct ctl_lun_modify_params *params; 2300232604Strasz struct ctl_be_block_lun *be_lun; 2301241896Skib int error; 2302232604Strasz 2303232604Strasz params = &req->reqdata.modify; 2304232604Strasz 2305232604Strasz mtx_lock(&softc->lock); 2306232604Strasz 2307232604Strasz be_lun = NULL; 2308232604Strasz 2309232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2310232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2311232604Strasz break; 2312232604Strasz } 2313232604Strasz mtx_unlock(&softc->lock); 2314232604Strasz 2315232604Strasz if (be_lun == NULL) { 2316232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2317232604Strasz "%s: LUN %u is not managed by the block backend", 2318232604Strasz __func__, params->lun_id); 2319232604Strasz goto bailout_error; 2320232604Strasz } 2321232604Strasz 2322232604Strasz if (params->lun_size_bytes != 0) { 2323232604Strasz if (params->lun_size_bytes < be_lun->blocksize) { 2324232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2325232604Strasz "%s: LUN size %ju < blocksize %u", __func__, 2326232604Strasz params->lun_size_bytes, be_lun->blocksize); 2327232604Strasz goto bailout_error; 2328232604Strasz } 2329232604Strasz } 2330232604Strasz 2331232604Strasz vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2332232604Strasz 2333232604Strasz if (be_lun->vn->v_type == VREG) 2334232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2335232604Strasz else 2336232604Strasz error = ctl_be_block_modify_dev(be_lun, req); 2337232604Strasz 2338232604Strasz VOP_UNLOCK(be_lun->vn, 0); 2339232604Strasz 2340232604Strasz if (error != 0) 2341232604Strasz goto bailout_error; 2342232604Strasz 2343232604Strasz be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2344232604Strasz 2345232604Strasz /* 2346232604Strasz * The maximum LBA is the size - 1. 2347232604Strasz * 2348232604Strasz * XXX: Note that this field is being updated without locking, 2349232604Strasz * which might cause problems on 32-bit architectures. 2350232604Strasz */ 2351232604Strasz be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 2352232604Strasz ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2353232604Strasz 2354232604Strasz /* Tell the user the exact size we ended up using */ 2355232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2356232604Strasz 2357232604Strasz req->status = CTL_LUN_OK; 2358232604Strasz 2359232604Strasz return (0); 2360232604Strasz 2361232604Straszbailout_error: 2362232604Strasz req->status = CTL_LUN_ERROR; 2363232604Strasz 2364232604Strasz return (0); 2365232604Strasz} 2366232604Strasz 2367229997Skenstatic void 2368229997Skenctl_be_block_lun_shutdown(void *be_lun) 2369229997Sken{ 2370229997Sken struct ctl_be_block_lun *lun; 2371229997Sken struct ctl_be_block_softc *softc; 2372229997Sken 2373229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2374229997Sken 2375229997Sken softc = lun->softc; 2376229997Sken 2377229997Sken mtx_lock(&softc->lock); 2378229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2379229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2380229997Sken wakeup(lun); 2381229997Sken mtx_unlock(&softc->lock); 2382229997Sken 2383229997Sken} 2384229997Sken 2385229997Skenstatic void 2386229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2387229997Sken{ 2388229997Sken struct ctl_be_block_lun *lun; 2389229997Sken struct ctl_be_block_softc *softc; 2390229997Sken 2391229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2392229997Sken softc = lun->softc; 2393229997Sken 2394229997Sken if (status == CTL_LUN_CONFIG_OK) { 2395229997Sken mtx_lock(&softc->lock); 2396229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2397229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2398229997Sken wakeup(lun); 2399229997Sken mtx_unlock(&softc->lock); 2400229997Sken 2401229997Sken /* 2402229997Sken * We successfully added the LUN, attempt to enable it. 2403229997Sken */ 2404229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2405229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2406229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2407229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2408229997Sken __func__); 2409229997Sken } 2410229997Sken } 2411229997Sken 2412229997Sken return; 2413229997Sken } 2414229997Sken 2415229997Sken 2416229997Sken mtx_lock(&softc->lock); 2417229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2418229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2419229997Sken wakeup(lun); 2420229997Sken mtx_unlock(&softc->lock); 2421229997Sken} 2422229997Sken 2423229997Sken 2424229997Skenstatic int 2425229997Skenctl_be_block_config_write(union ctl_io *io) 2426229997Sken{ 2427229997Sken struct ctl_be_block_lun *be_lun; 2428229997Sken struct ctl_be_lun *ctl_be_lun; 2429229997Sken int retval; 2430229997Sken 2431229997Sken retval = 0; 2432229997Sken 2433229997Sken DPRINTF("entered\n"); 2434229997Sken 2435229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2436229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2437229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2438229997Sken 2439229997Sken switch (io->scsiio.cdb[0]) { 2440229997Sken case SYNCHRONIZE_CACHE: 2441229997Sken case SYNCHRONIZE_CACHE_16: 2442264274Smav case WRITE_SAME_10: 2443264274Smav case WRITE_SAME_16: 2444264274Smav case UNMAP: 2445229997Sken /* 2446229997Sken * The upper level CTL code will filter out any CDBs with 2447229997Sken * the immediate bit set and return the proper error. 2448229997Sken * 2449229997Sken * We don't really need to worry about what LBA range the 2450229997Sken * user asked to be synced out. When they issue a sync 2451229997Sken * cache command, we'll sync out the whole thing. 2452229997Sken */ 2453229997Sken mtx_lock(&be_lun->lock); 2454229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2455229997Sken links); 2456229997Sken mtx_unlock(&be_lun->lock); 2457229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2458229997Sken break; 2459229997Sken case START_STOP_UNIT: { 2460229997Sken struct scsi_start_stop_unit *cdb; 2461229997Sken 2462229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2463229997Sken 2464229997Sken if (cdb->how & SSS_START) 2465229997Sken retval = ctl_start_lun(ctl_be_lun); 2466229997Sken else { 2467229997Sken retval = ctl_stop_lun(ctl_be_lun); 2468229997Sken /* 2469229997Sken * XXX KDM Copan-specific offline behavior. 2470229997Sken * Figure out a reasonable way to port this? 2471229997Sken */ 2472229997Sken#ifdef NEEDTOPORT 2473229997Sken if ((retval == 0) 2474229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2475229997Sken retval = ctl_lun_offline(ctl_be_lun); 2476229997Sken#endif 2477229997Sken } 2478229997Sken 2479229997Sken /* 2480229997Sken * In general, the above routines should not fail. They 2481229997Sken * just set state for the LUN. So we've got something 2482229997Sken * pretty wrong here if we can't start or stop the LUN. 2483229997Sken */ 2484229997Sken if (retval != 0) { 2485229997Sken ctl_set_internal_failure(&io->scsiio, 2486229997Sken /*sks_valid*/ 1, 2487229997Sken /*retry_count*/ 0xf051); 2488229997Sken retval = CTL_RETVAL_COMPLETE; 2489229997Sken } else { 2490229997Sken ctl_set_success(&io->scsiio); 2491229997Sken } 2492229997Sken ctl_config_write_done(io); 2493229997Sken break; 2494229997Sken } 2495229997Sken default: 2496229997Sken ctl_set_invalid_opcode(&io->scsiio); 2497229997Sken ctl_config_write_done(io); 2498229997Sken retval = CTL_RETVAL_COMPLETE; 2499229997Sken break; 2500229997Sken } 2501229997Sken 2502229997Sken return (retval); 2503229997Sken 2504229997Sken} 2505229997Sken 2506229997Skenstatic int 2507229997Skenctl_be_block_config_read(union ctl_io *io) 2508229997Sken{ 2509229997Sken return (0); 2510229997Sken} 2511229997Sken 2512229997Skenstatic int 2513229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2514229997Sken{ 2515229997Sken struct ctl_be_block_lun *lun; 2516229997Sken int retval; 2517229997Sken 2518229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2519229997Sken retval = 0; 2520229997Sken 2521229997Sken retval = sbuf_printf(sb, "<num_threads>"); 2522229997Sken 2523229997Sken if (retval != 0) 2524229997Sken goto bailout; 2525229997Sken 2526229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2527229997Sken 2528229997Sken if (retval != 0) 2529229997Sken goto bailout; 2530229997Sken 2531229997Sken retval = sbuf_printf(sb, "</num_threads>"); 2532229997Sken 2533229997Skenbailout: 2534229997Sken 2535229997Sken return (retval); 2536229997Sken} 2537229997Sken 2538229997Skenint 2539229997Skenctl_be_block_init(void) 2540229997Sken{ 2541229997Sken struct ctl_be_block_softc *softc; 2542229997Sken int retval; 2543229997Sken 2544229997Sken softc = &backend_block_softc; 2545229997Sken retval = 0; 2546229997Sken 2547229997Sken mtx_init(&softc->lock, "ctlblk", NULL, MTX_DEF); 2548264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2549264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2550229997Sken STAILQ_INIT(&softc->disk_list); 2551229997Sken STAILQ_INIT(&softc->lun_list); 2552229997Sken 2553229997Sken return (retval); 2554229997Sken} 2555