ctl_backend_block.c revision 287670
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 287670 2015-09-11 14:33:05Z mav $"); 44229997Sken 45229997Sken#include <sys/param.h> 46229997Sken#include <sys/systm.h> 47229997Sken#include <sys/kernel.h> 48229997Sken#include <sys/types.h> 49229997Sken#include <sys/kthread.h> 50229997Sken#include <sys/bio.h> 51229997Sken#include <sys/fcntl.h> 52264274Smav#include <sys/limits.h> 53229997Sken#include <sys/lock.h> 54229997Sken#include <sys/mutex.h> 55229997Sken#include <sys/condvar.h> 56229997Sken#include <sys/malloc.h> 57229997Sken#include <sys/conf.h> 58229997Sken#include <sys/ioccom.h> 59229997Sken#include <sys/queue.h> 60229997Sken#include <sys/sbuf.h> 61229997Sken#include <sys/endian.h> 62229997Sken#include <sys/uio.h> 63229997Sken#include <sys/buf.h> 64229997Sken#include <sys/taskqueue.h> 65229997Sken#include <sys/vnode.h> 66229997Sken#include <sys/namei.h> 67229997Sken#include <sys/mount.h> 68229997Sken#include <sys/disk.h> 69229997Sken#include <sys/fcntl.h> 70229997Sken#include <sys/filedesc.h> 71275474Smav#include <sys/filio.h> 72229997Sken#include <sys/proc.h> 73229997Sken#include <sys/pcpu.h> 74229997Sken#include <sys/module.h> 75229997Sken#include <sys/sdt.h> 76229997Sken#include <sys/devicestat.h> 77229997Sken#include <sys/sysctl.h> 78229997Sken 79229997Sken#include <geom/geom.h> 80229997Sken 81229997Sken#include <cam/cam.h> 82229997Sken#include <cam/scsi/scsi_all.h> 83229997Sken#include <cam/scsi/scsi_da.h> 84229997Sken#include <cam/ctl/ctl_io.h> 85229997Sken#include <cam/ctl/ctl.h> 86229997Sken#include <cam/ctl/ctl_backend.h> 87229997Sken#include <cam/ctl/ctl_ioctl.h> 88287621Smav#include <cam/ctl/ctl_ha.h> 89229997Sken#include <cam/ctl/ctl_scsi_all.h> 90287621Smav#include <cam/ctl/ctl_private.h> 91229997Sken#include <cam/ctl/ctl_error.h> 92229997Sken 93229997Sken/* 94264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 95264886Smav * I/O. If we get an I/O larger than that, we'll split it. 96229997Sken */ 97267537Smav#define CTLBLK_HALF_IO_SIZE (512 * 1024) 98267537Smav#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 99264886Smav#define CTLBLK_MAX_SEG MAXPHYS 100267537Smav#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 101267537Smav#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 102229997Sken 103229997Sken#ifdef CTLBLK_DEBUG 104229997Sken#define DPRINTF(fmt, args...) \ 105229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 106229997Sken#else 107229997Sken#define DPRINTF(fmt, args...) do {} while(0) 108229997Sken#endif 109229997Sken 110267519Smav#define PRIV(io) \ 111267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 112267537Smav#define ARGS(io) \ 113267537Smav ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 114267519Smav 115229997SkenSDT_PROVIDER_DEFINE(cbb); 116229997Sken 117229997Skentypedef enum { 118229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 119229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 120229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 121229997Sken} ctl_be_block_lun_flags; 122229997Sken 123229997Skentypedef enum { 124229997Sken CTL_BE_BLOCK_NONE, 125229997Sken CTL_BE_BLOCK_DEV, 126229997Sken CTL_BE_BLOCK_FILE 127229997Sken} ctl_be_block_type; 128229997Sken 129229997Skenstruct ctl_be_block_filedata { 130229997Sken struct ucred *cred; 131229997Sken}; 132229997Sken 133229997Skenunion ctl_be_block_bedata { 134229997Sken struct ctl_be_block_filedata file; 135229997Sken}; 136229997Sken 137229997Skenstruct ctl_be_block_io; 138229997Skenstruct ctl_be_block_lun; 139229997Sken 140229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 141229997Sken struct ctl_be_block_io *beio); 142274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 143274154Smav const char *attrname); 144229997Sken 145229997Sken/* 146229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 147229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 148229997Sken */ 149229997Skenstruct ctl_be_block_lun { 150272911Smav struct ctl_lun_create_params params; 151229997Sken char lunname[32]; 152229997Sken char *dev_path; 153229997Sken ctl_be_block_type dev_type; 154229997Sken struct vnode *vn; 155229997Sken union ctl_be_block_bedata backend; 156229997Sken cbb_dispatch_t dispatch; 157229997Sken cbb_dispatch_t lun_flush; 158264274Smav cbb_dispatch_t unmap; 159275474Smav cbb_dispatch_t get_lba_status; 160274154Smav cbb_getattr_t getattr; 161229997Sken uma_zone_t lun_zone; 162229997Sken uint64_t size_blocks; 163229997Sken uint64_t size_bytes; 164229997Sken struct ctl_be_block_softc *softc; 165229997Sken struct devstat *disk_stats; 166229997Sken ctl_be_block_lun_flags flags; 167229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 168287499Smav struct ctl_be_lun cbe_lun; 169229997Sken struct taskqueue *io_taskqueue; 170229997Sken struct task io_task; 171229997Sken int num_threads; 172229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 173275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 174229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 175229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 176267877Smav struct mtx_padalign io_lock; 177267877Smav struct mtx_padalign queue_lock; 178229997Sken}; 179229997Sken 180229997Sken/* 181229997Sken * Overall softc structure for the block backend module. 182229997Sken */ 183229997Skenstruct ctl_be_block_softc { 184229997Sken struct mtx lock; 185229997Sken int num_luns; 186229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 187229997Sken}; 188229997Sken 189229997Skenstatic struct ctl_be_block_softc backend_block_softc; 190229997Sken 191229997Sken/* 192229997Sken * Per-I/O information. 193229997Sken */ 194229997Skenstruct ctl_be_block_io { 195229997Sken union ctl_io *io; 196229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 197229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 198229997Sken int bio_cmd; 199229997Sken int num_segs; 200229997Sken int num_bios_sent; 201229997Sken int num_bios_done; 202229997Sken int send_complete; 203229997Sken int num_errors; 204229997Sken struct bintime ds_t0; 205229997Sken devstat_tag_type ds_tag_type; 206229997Sken devstat_trans_flags ds_trans_type; 207229997Sken uint64_t io_len; 208229997Sken uint64_t io_offset; 209286353Smav int io_arg; 210229997Sken struct ctl_be_block_softc *softc; 211229997Sken struct ctl_be_block_lun *lun; 212264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 213229997Sken}; 214229997Sken 215287621Smavextern struct ctl_softc *control_softc; 216287621Smav 217229997Skenstatic int cbb_num_threads = 14; 218229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 219229997Sken "CAM Target Layer Block Backend"); 220267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 221229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 222229997Sken 223229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 224229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 225229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 226229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 227229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 228229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 229229997Sken struct ctl_be_block_io *beio); 230229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 231229997Sken struct ctl_be_block_io *beio); 232275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 233275474Smav struct ctl_be_block_io *beio); 234275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 235275481Smav const char *attrname); 236229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 237229997Sken struct ctl_be_block_io *beio); 238264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 239264274Smav struct ctl_be_block_io *beio); 240229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 241229997Sken struct ctl_be_block_io *beio); 242274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 243274154Smav const char *attrname); 244275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 245275474Smav union ctl_io *io); 246229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 247229997Sken union ctl_io *io); 248229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 249229997Sken union ctl_io *io); 250229997Skenstatic void ctl_be_block_worker(void *context, int pending); 251229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 252229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 253229997Sken int flag, struct thread *td); 254229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 255229997Sken struct ctl_lun_req *req); 256229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 257229997Sken struct ctl_lun_req *req); 258229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 259229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 260229997Sken struct ctl_be_block_lun *be_lun, 261229997Sken struct ctl_lun_req *req); 262229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 263229997Sken struct ctl_lun_req *req); 264229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 265229997Sken struct ctl_lun_req *req); 266232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 267232604Strasz struct ctl_lun_req *req); 268232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 269232604Strasz struct ctl_lun_req *req); 270232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 271232604Strasz struct ctl_lun_req *req); 272229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 273229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 274229997Sken ctl_lun_config_status status); 275229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 276229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 277229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 278274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 279229997Skenint ctl_be_block_init(void); 280229997Sken 281229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 282229997Sken{ 283230334Sken .name = "block", 284230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 285230334Sken .init = ctl_be_block_init, 286230334Sken .data_submit = ctl_be_block_submit, 287230334Sken .data_move_done = ctl_be_block_move_done, 288230334Sken .config_read = ctl_be_block_config_read, 289230334Sken .config_write = ctl_be_block_config_write, 290230334Sken .ioctl = ctl_be_block_ioctl, 291274154Smav .lun_info = ctl_be_block_lun_info, 292274154Smav .lun_attr = ctl_be_block_lun_attr 293229997Sken}; 294229997Sken 295229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 296229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 297229997Sken 298264020Straszstatic uma_zone_t beio_zone; 299264020Strasz 300229997Skenstatic struct ctl_be_block_io * 301229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 302229997Sken{ 303229997Sken struct ctl_be_block_io *beio; 304229997Sken 305264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 306264020Strasz beio->softc = softc; 307229997Sken return (beio); 308229997Sken} 309229997Sken 310229997Skenstatic void 311229997Skenctl_free_beio(struct ctl_be_block_io *beio) 312229997Sken{ 313229997Sken int duplicate_free; 314229997Sken int i; 315229997Sken 316229997Sken duplicate_free = 0; 317229997Sken 318229997Sken for (i = 0; i < beio->num_segs; i++) { 319229997Sken if (beio->sg_segs[i].addr == NULL) 320229997Sken duplicate_free++; 321229997Sken 322229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 323229997Sken beio->sg_segs[i].addr = NULL; 324267537Smav 325267537Smav /* For compare we had two equal S/G lists. */ 326267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 327267537Smav uma_zfree(beio->lun->lun_zone, 328267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 329267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 330267537Smav } 331229997Sken } 332229997Sken 333229997Sken if (duplicate_free > 0) { 334229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 335229997Sken duplicate_free, beio->num_segs); 336229997Sken } 337229997Sken 338264020Strasz uma_zfree(beio_zone, beio); 339229997Sken} 340229997Sken 341229997Skenstatic void 342229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 343229997Sken{ 344267877Smav union ctl_io *io = beio->io; 345229997Sken 346264274Smav if (beio->beio_cont != NULL) { 347264274Smav beio->beio_cont(beio); 348264274Smav } else { 349264274Smav ctl_free_beio(beio); 350267537Smav ctl_data_submit_done(io); 351264274Smav } 352229997Sken} 353229997Sken 354229997Skenstatic int 355229997Skenctl_be_block_move_done(union ctl_io *io) 356229997Sken{ 357229997Sken struct ctl_be_block_io *beio; 358229997Sken struct ctl_be_block_lun *be_lun; 359267537Smav struct ctl_lba_len_flags *lbalen; 360229997Sken#ifdef CTL_TIME_IO 361229997Sken struct bintime cur_bt; 362267537Smav#endif 363267537Smav int i; 364229997Sken 365267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 366229997Sken be_lun = beio->lun; 367229997Sken 368229997Sken DPRINTF("entered\n"); 369229997Sken 370229997Sken#ifdef CTL_TIME_IO 371229997Sken getbintime(&cur_bt); 372229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 373229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 374229997Sken io->io_hdr.num_dmas++; 375229997Sken#endif 376267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 377229997Sken 378229997Sken /* 379229997Sken * We set status at this point for read commands, and write 380229997Sken * commands with errors. 381229997Sken */ 382275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 383275058Smav ; 384275058Smav } else if ((io->io_hdr.port_status == 0) && 385267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 386267537Smav lbalen = ARGS(beio->io); 387267537Smav if (lbalen->flags & CTL_LLF_READ) { 388267537Smav ctl_set_success(&io->scsiio); 389267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 390267537Smav /* We have two data blocks ready for comparison. */ 391267537Smav for (i = 0; i < beio->num_segs; i++) { 392267537Smav if (memcmp(beio->sg_segs[i].addr, 393267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 394267537Smav beio->sg_segs[i].len) != 0) 395267537Smav break; 396267537Smav } 397267537Smav if (i < beio->num_segs) 398267537Smav ctl_set_sense(&io->scsiio, 399267537Smav /*current_error*/ 1, 400267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 401267537Smav /*asc*/ 0x1D, 402267537Smav /*ascq*/ 0x00, 403267537Smav SSD_ELEM_NONE); 404267537Smav else 405267537Smav ctl_set_success(&io->scsiio); 406267537Smav } 407275058Smav } else if ((io->io_hdr.port_status != 0) && 408275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 409275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 410229997Sken /* 411229997Sken * For hardware error sense keys, the sense key 412229997Sken * specific value is defined to be a retry count, 413229997Sken * but we use it to pass back an internal FETD 414229997Sken * error code. XXX KDM Hopefully the FETD is only 415229997Sken * using 16 bits for an error code, since that's 416229997Sken * all the space we have in the sks field. 417229997Sken */ 418229997Sken ctl_set_internal_failure(&io->scsiio, 419229997Sken /*sks_valid*/ 1, 420229997Sken /*retry_count*/ 421229997Sken io->io_hdr.port_status); 422229997Sken } 423229997Sken 424229997Sken /* 425229997Sken * If this is a read, or a write with errors, it is done. 426229997Sken */ 427229997Sken if ((beio->bio_cmd == BIO_READ) 428229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 429229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 430229997Sken ctl_complete_beio(beio); 431229997Sken return (0); 432229997Sken } 433229997Sken 434229997Sken /* 435229997Sken * At this point, we have a write and the DMA completed 436229997Sken * successfully. We now have to queue it to the task queue to 437229997Sken * execute the backend I/O. That is because we do blocking 438229997Sken * memory allocations, and in the file backing case, blocking I/O. 439229997Sken * This move done routine is generally called in the SIM's 440229997Sken * interrupt context, and therefore we cannot block. 441229997Sken */ 442267877Smav mtx_lock(&be_lun->queue_lock); 443229997Sken /* 444229997Sken * XXX KDM make sure that links is okay to use at this point. 445229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 446229997Sken * or deal with resource allocation here. 447229997Sken */ 448229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 449267877Smav mtx_unlock(&be_lun->queue_lock); 450229997Sken 451229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 452229997Sken 453229997Sken return (0); 454229997Sken} 455229997Sken 456229997Skenstatic void 457229997Skenctl_be_block_biodone(struct bio *bio) 458229997Sken{ 459229997Sken struct ctl_be_block_io *beio; 460229997Sken struct ctl_be_block_lun *be_lun; 461229997Sken union ctl_io *io; 462261538Smav int error; 463229997Sken 464229997Sken beio = bio->bio_caller1; 465229997Sken be_lun = beio->lun; 466229997Sken io = beio->io; 467229997Sken 468229997Sken DPRINTF("entered\n"); 469229997Sken 470261538Smav error = bio->bio_error; 471267877Smav mtx_lock(&be_lun->io_lock); 472261538Smav if (error != 0) 473229997Sken beio->num_errors++; 474229997Sken 475229997Sken beio->num_bios_done++; 476229997Sken 477229997Sken /* 478229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 479229997Sken * during the free might cause it to complain. 480229997Sken */ 481229997Sken g_destroy_bio(bio); 482229997Sken 483229997Sken /* 484229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 485229997Sken * complete, then we're done. 486229997Sken */ 487229997Sken if ((beio->send_complete == 0) 488229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 489267877Smav mtx_unlock(&be_lun->io_lock); 490229997Sken return; 491229997Sken } 492229997Sken 493229997Sken /* 494229997Sken * At this point, we've verified that we are the last I/O to 495229997Sken * complete, so it's safe to drop the lock. 496229997Sken */ 497267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 498267877Smav beio->ds_tag_type, beio->ds_trans_type, 499267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 500267877Smav mtx_unlock(&be_lun->io_lock); 501229997Sken 502229997Sken /* 503229997Sken * If there are any errors from the backing device, we fail the 504229997Sken * entire I/O with a medium error. 505229997Sken */ 506229997Sken if (beio->num_errors > 0) { 507261538Smav if (error == EOPNOTSUPP) { 508261538Smav ctl_set_invalid_opcode(&io->scsiio); 509282565Smav } else if (error == ENOSPC || error == EDQUOT) { 510273809Smav ctl_set_space_alloc_fail(&io->scsiio); 511261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 512229997Sken /* XXX KDM is there is a better error here? */ 513229997Sken ctl_set_internal_failure(&io->scsiio, 514229997Sken /*sks_valid*/ 1, 515229997Sken /*retry_count*/ 0xbad2); 516229997Sken } else 517229997Sken ctl_set_medium_error(&io->scsiio); 518229997Sken ctl_complete_beio(beio); 519229997Sken return; 520229997Sken } 521229997Sken 522229997Sken /* 523267537Smav * If this is a write, a flush, a delete or verify, we're all done. 524229997Sken * If this is a read, we can now send the data to the user. 525229997Sken */ 526229997Sken if ((beio->bio_cmd == BIO_WRITE) 527264274Smav || (beio->bio_cmd == BIO_FLUSH) 528267537Smav || (beio->bio_cmd == BIO_DELETE) 529267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 530229997Sken ctl_set_success(&io->scsiio); 531229997Sken ctl_complete_beio(beio); 532229997Sken } else { 533275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 534275058Smav beio->beio_cont == NULL) 535275058Smav ctl_set_success(&io->scsiio); 536229997Sken#ifdef CTL_TIME_IO 537229997Sken getbintime(&io->io_hdr.dma_start_bt); 538229997Sken#endif 539229997Sken ctl_datamove(io); 540229997Sken } 541229997Sken} 542229997Sken 543229997Skenstatic void 544229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 545229997Sken struct ctl_be_block_io *beio) 546229997Sken{ 547267877Smav union ctl_io *io = beio->io; 548229997Sken struct mount *mountpoint; 549241896Skib int error, lock_flags; 550229997Sken 551229997Sken DPRINTF("entered\n"); 552229997Sken 553267877Smav binuptime(&beio->ds_t0); 554267877Smav mtx_lock(&be_lun->io_lock); 555267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 556267877Smav mtx_unlock(&be_lun->io_lock); 557229997Sken 558267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 559229997Sken 560229997Sken if (MNT_SHARED_WRITES(mountpoint) 561229997Sken || ((mountpoint == NULL) 562229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 563229997Sken lock_flags = LK_SHARED; 564229997Sken else 565229997Sken lock_flags = LK_EXCLUSIVE; 566229997Sken 567229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 568229997Sken 569286353Smav error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 570286353Smav curthread); 571229997Sken VOP_UNLOCK(be_lun->vn, 0); 572229997Sken 573229997Sken vn_finished_write(mountpoint); 574229997Sken 575267877Smav mtx_lock(&be_lun->io_lock); 576267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 577267877Smav beio->ds_tag_type, beio->ds_trans_type, 578267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 579267877Smav mtx_unlock(&be_lun->io_lock); 580267877Smav 581229997Sken if (error == 0) 582229997Sken ctl_set_success(&io->scsiio); 583229997Sken else { 584229997Sken /* XXX KDM is there is a better error here? */ 585229997Sken ctl_set_internal_failure(&io->scsiio, 586229997Sken /*sks_valid*/ 1, 587229997Sken /*retry_count*/ 0xbad1); 588229997Sken } 589229997Sken 590229997Sken ctl_complete_beio(beio); 591229997Sken} 592229997Sken 593258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 594258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 595258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 596258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 597229997Sken 598229997Skenstatic void 599229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 600229997Sken struct ctl_be_block_io *beio) 601229997Sken{ 602229997Sken struct ctl_be_block_filedata *file_data; 603229997Sken union ctl_io *io; 604229997Sken struct uio xuio; 605229997Sken struct iovec *xiovec; 606241896Skib int flags; 607229997Sken int error, i; 608229997Sken 609229997Sken DPRINTF("entered\n"); 610229997Sken 611229997Sken file_data = &be_lun->backend.file; 612229997Sken io = beio->io; 613271309Smav flags = 0; 614271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 615271309Smav flags |= IO_DIRECT; 616271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 617271309Smav flags |= IO_SYNC; 618229997Sken 619267537Smav bzero(&xuio, sizeof(xuio)); 620229997Sken if (beio->bio_cmd == BIO_READ) { 621229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 622267537Smav xuio.uio_rw = UIO_READ; 623229997Sken } else { 624229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 625267537Smav xuio.uio_rw = UIO_WRITE; 626229997Sken } 627229997Sken xuio.uio_offset = beio->io_offset; 628229997Sken xuio.uio_resid = beio->io_len; 629229997Sken xuio.uio_segflg = UIO_SYSSPACE; 630229997Sken xuio.uio_iov = beio->xiovecs; 631229997Sken xuio.uio_iovcnt = beio->num_segs; 632229997Sken xuio.uio_td = curthread; 633229997Sken 634229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 635229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 636229997Sken xiovec->iov_len = beio->sg_segs[i].len; 637229997Sken } 638229997Sken 639267877Smav binuptime(&beio->ds_t0); 640267877Smav mtx_lock(&be_lun->io_lock); 641267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 642267877Smav mtx_unlock(&be_lun->io_lock); 643267877Smav 644229997Sken if (beio->bio_cmd == BIO_READ) { 645229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 646229997Sken 647229997Sken /* 648229997Sken * UFS pays attention to IO_DIRECT for reads. If the 649229997Sken * DIRECTIO option is configured into the kernel, it calls 650229997Sken * ffs_rawread(). But that only works for single-segment 651229997Sken * uios with user space addresses. In our case, with a 652229997Sken * kernel uio, it still reads into the buffer cache, but it 653229997Sken * will just try to release the buffer from the cache later 654229997Sken * on in ffs_read(). 655229997Sken * 656229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 657229997Sken * 658229997Sken * UFS does not pay attention to IO_SYNC for reads. 659229997Sken * 660229997Sken * ZFS pays attention to IO_SYNC (which translates into the 661229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 662229997Sken * attempts to sync the file before reading. 663229997Sken */ 664271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 665229997Sken 666229997Sken VOP_UNLOCK(be_lun->vn, 0); 667267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 668229997Sken } else { 669229997Sken struct mount *mountpoint; 670229997Sken int lock_flags; 671229997Sken 672229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 673229997Sken 674229997Sken if (MNT_SHARED_WRITES(mountpoint) 675229997Sken || ((mountpoint == NULL) 676229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 677229997Sken lock_flags = LK_SHARED; 678229997Sken else 679229997Sken lock_flags = LK_EXCLUSIVE; 680229997Sken 681229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 682229997Sken 683229997Sken /* 684229997Sken * UFS pays attention to IO_DIRECT for writes. The write 685229997Sken * is done asynchronously. (Normally the write would just 686229997Sken * get put into cache. 687229997Sken * 688229997Sken * UFS pays attention to IO_SYNC for writes. It will 689229997Sken * attempt to write the buffer out synchronously if that 690229997Sken * flag is set. 691229997Sken * 692229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 693229997Sken * 694229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 695229997Sken * for writes. It will flush the transaction from the 696229997Sken * cache before returning. 697229997Sken */ 698271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, 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 705267877Smav mtx_lock(&be_lun->io_lock); 706267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 707267877Smav beio->ds_tag_type, beio->ds_trans_type, 708267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 709267877Smav mtx_unlock(&be_lun->io_lock); 710267877Smav 711229997Sken /* 712229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 713229997Sken * return the I/O to the user. 714229997Sken */ 715229997Sken if (error != 0) { 716229997Sken char path_str[32]; 717229997Sken 718229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 719229997Sken printf("%s%s command returned errno %d\n", path_str, 720229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 721282565Smav if (error == ENOSPC || error == EDQUOT) { 722273809Smav ctl_set_space_alloc_fail(&io->scsiio); 723273809Smav } else 724273809Smav ctl_set_medium_error(&io->scsiio); 725229997Sken ctl_complete_beio(beio); 726229997Sken return; 727229997Sken } 728229997Sken 729229997Sken /* 730269122Smav * If this is a write or a verify, we're all done. 731229997Sken * If this is a read, we can now send the data to the user. 732229997Sken */ 733269122Smav if ((beio->bio_cmd == BIO_WRITE) || 734269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 735229997Sken ctl_set_success(&io->scsiio); 736229997Sken ctl_complete_beio(beio); 737229997Sken } else { 738275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 739275058Smav beio->beio_cont == NULL) 740275058Smav ctl_set_success(&io->scsiio); 741229997Sken#ifdef CTL_TIME_IO 742229997Sken getbintime(&io->io_hdr.dma_start_bt); 743229997Sken#endif 744229997Sken ctl_datamove(io); 745229997Sken } 746229997Sken} 747229997Sken 748229997Skenstatic void 749275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 750275474Smav struct ctl_be_block_io *beio) 751275474Smav{ 752275474Smav union ctl_io *io = beio->io; 753275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 754275474Smav struct scsi_get_lba_status_data *data; 755275474Smav off_t roff, off; 756275474Smav int error, status; 757275474Smav 758275474Smav DPRINTF("entered\n"); 759275474Smav 760287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 761275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 762275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 763275474Smav 0, curthread->td_ucred, curthread); 764275474Smav if (error == 0 && off > roff) 765275474Smav status = 0; /* mapped up to off */ 766275474Smav else { 767275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 768275474Smav 0, curthread->td_ucred, curthread); 769275474Smav if (error == 0 && off > roff) 770275474Smav status = 1; /* deallocated up to off */ 771275474Smav else { 772275474Smav status = 0; /* unknown up to the end */ 773275474Smav off = be_lun->size_bytes; 774275474Smav } 775275474Smav } 776275474Smav VOP_UNLOCK(be_lun->vn, 0); 777275474Smav 778275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 779275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 780287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 781287499Smav lbalen->lba), data->descr[0].length); 782275474Smav data->descr[0].status = status; 783275474Smav 784275474Smav ctl_complete_beio(beio); 785275474Smav} 786275474Smav 787275481Smavstatic uint64_t 788275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 789275481Smav{ 790275481Smav struct vattr vattr; 791275481Smav struct statfs statfs; 792285030Smav uint64_t val; 793275481Smav int error; 794275481Smav 795285030Smav val = UINT64_MAX; 796275481Smav if (be_lun->vn == NULL) 797285030Smav return (val); 798285030Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 799275481Smav if (strcmp(attrname, "blocksused") == 0) { 800275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 801285030Smav if (error == 0) 802287499Smav val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 803275481Smav } 804285030Smav if (strcmp(attrname, "blocksavail") == 0 && 805285030Smav (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 806275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 807285030Smav if (error == 0) 808286811Smav val = statfs.f_bavail * statfs.f_bsize / 809287499Smav be_lun->cbe_lun.blocksize; 810275481Smav } 811285030Smav VOP_UNLOCK(be_lun->vn, 0); 812285030Smav return (val); 813275481Smav} 814275481Smav 815275474Smavstatic void 816269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 817269123Smav struct ctl_be_block_io *beio) 818269123Smav{ 819269123Smav union ctl_io *io; 820287664Smav struct cdevsw *csw; 821287664Smav struct cdev *dev; 822269123Smav struct uio xuio; 823269123Smav struct iovec *xiovec; 824287664Smav int error, flags, i, ref; 825269123Smav 826269123Smav DPRINTF("entered\n"); 827269123Smav 828269123Smav io = beio->io; 829271309Smav flags = 0; 830271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 831271309Smav flags |= IO_DIRECT; 832271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 833271309Smav flags |= IO_SYNC; 834269123Smav 835269123Smav bzero(&xuio, sizeof(xuio)); 836269123Smav if (beio->bio_cmd == BIO_READ) { 837269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 838269123Smav xuio.uio_rw = UIO_READ; 839269123Smav } else { 840269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 841269123Smav xuio.uio_rw = UIO_WRITE; 842269123Smav } 843269123Smav xuio.uio_offset = beio->io_offset; 844269123Smav xuio.uio_resid = beio->io_len; 845269123Smav xuio.uio_segflg = UIO_SYSSPACE; 846269123Smav xuio.uio_iov = beio->xiovecs; 847269123Smav xuio.uio_iovcnt = beio->num_segs; 848269123Smav xuio.uio_td = curthread; 849269123Smav 850269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 851269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 852269123Smav xiovec->iov_len = beio->sg_segs[i].len; 853269123Smav } 854269123Smav 855269123Smav binuptime(&beio->ds_t0); 856269123Smav mtx_lock(&be_lun->io_lock); 857269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 858269123Smav mtx_unlock(&be_lun->io_lock); 859269123Smav 860287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 861287664Smav if (csw) { 862287664Smav if (beio->bio_cmd == BIO_READ) 863287664Smav error = csw->d_read(dev, &xuio, flags); 864287664Smav else 865287664Smav error = csw->d_write(dev, &xuio, flags); 866287664Smav dev_relthread(dev, ref); 867287664Smav } else 868287664Smav error = ENXIO; 869287664Smav 870287664Smav if (beio->bio_cmd == BIO_READ) 871269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 872287664Smav else 873269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 874269123Smav 875269123Smav mtx_lock(&be_lun->io_lock); 876269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 877269123Smav beio->ds_tag_type, beio->ds_trans_type, 878269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 879269123Smav mtx_unlock(&be_lun->io_lock); 880269123Smav 881269123Smav /* 882269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 883269123Smav * return the I/O to the user. 884269123Smav */ 885269123Smav if (error != 0) { 886282565Smav if (error == ENOSPC || error == EDQUOT) { 887273809Smav ctl_set_space_alloc_fail(&io->scsiio); 888273809Smav } else 889273809Smav ctl_set_medium_error(&io->scsiio); 890269123Smav ctl_complete_beio(beio); 891269123Smav return; 892269123Smav } 893269123Smav 894269123Smav /* 895269123Smav * If this is a write or a verify, we're all done. 896269123Smav * If this is a read, we can now send the data to the user. 897269123Smav */ 898269123Smav if ((beio->bio_cmd == BIO_WRITE) || 899269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 900269123Smav ctl_set_success(&io->scsiio); 901269123Smav ctl_complete_beio(beio); 902269123Smav } else { 903275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 904275058Smav beio->beio_cont == NULL) 905275058Smav ctl_set_success(&io->scsiio); 906269123Smav#ifdef CTL_TIME_IO 907269123Smav getbintime(&io->io_hdr.dma_start_bt); 908269123Smav#endif 909269123Smav ctl_datamove(io); 910269123Smav } 911269123Smav} 912269123Smav 913269123Smavstatic void 914275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 915275474Smav struct ctl_be_block_io *beio) 916275474Smav{ 917275474Smav union ctl_io *io = beio->io; 918287664Smav struct cdevsw *csw; 919287664Smav struct cdev *dev; 920275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 921275474Smav struct scsi_get_lba_status_data *data; 922275474Smav off_t roff, off; 923287664Smav int error, ref, status; 924275474Smav 925275474Smav DPRINTF("entered\n"); 926275474Smav 927287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 928287664Smav if (csw == NULL) { 929287664Smav status = 0; /* unknown up to the end */ 930287664Smav off = be_lun->size_bytes; 931287664Smav goto done; 932287664Smav } 933287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 934287664Smav error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 935287664Smav curthread); 936275474Smav if (error == 0 && off > roff) 937275474Smav status = 0; /* mapped up to off */ 938275474Smav else { 939287664Smav error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 940287664Smav curthread); 941275474Smav if (error == 0 && off > roff) 942275474Smav status = 1; /* deallocated up to off */ 943275474Smav else { 944275474Smav status = 0; /* unknown up to the end */ 945275474Smav off = be_lun->size_bytes; 946275474Smav } 947275474Smav } 948287664Smav dev_relthread(dev, ref); 949275474Smav 950287664Smavdone: 951275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 952275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 953287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 954287499Smav lbalen->lba), data->descr[0].length); 955275474Smav data->descr[0].status = status; 956275474Smav 957275474Smav ctl_complete_beio(beio); 958275474Smav} 959275474Smav 960275474Smavstatic void 961229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 962229997Sken struct ctl_be_block_io *beio) 963229997Sken{ 964229997Sken struct bio *bio; 965229997Sken union ctl_io *io; 966287664Smav struct cdevsw *csw; 967287664Smav struct cdev *dev; 968287664Smav int ref; 969229997Sken 970229997Sken io = beio->io; 971229997Sken 972229997Sken DPRINTF("entered\n"); 973229997Sken 974229997Sken /* This can't fail, it's a blocking allocation. */ 975229997Sken bio = g_alloc_bio(); 976229997Sken 977229997Sken bio->bio_cmd = BIO_FLUSH; 978229997Sken bio->bio_offset = 0; 979229997Sken bio->bio_data = 0; 980229997Sken bio->bio_done = ctl_be_block_biodone; 981229997Sken bio->bio_caller1 = beio; 982229997Sken bio->bio_pblkno = 0; 983229997Sken 984229997Sken /* 985229997Sken * We don't need to acquire the LUN lock here, because we are only 986229997Sken * sending one bio, and so there is no other context to synchronize 987229997Sken * with. 988229997Sken */ 989229997Sken beio->num_bios_sent = 1; 990229997Sken beio->send_complete = 1; 991229997Sken 992229997Sken binuptime(&beio->ds_t0); 993267877Smav mtx_lock(&be_lun->io_lock); 994229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 995267877Smav mtx_unlock(&be_lun->io_lock); 996229997Sken 997287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 998287664Smav if (csw) { 999287664Smav bio->bio_dev = dev; 1000287664Smav csw->d_strategy(bio); 1001287664Smav dev_relthread(dev, ref); 1002287664Smav } else { 1003287664Smav bio->bio_error = ENXIO; 1004287664Smav ctl_be_block_biodone(bio); 1005287664Smav } 1006229997Sken} 1007229997Sken 1008229997Skenstatic void 1009264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1010264274Smav struct ctl_be_block_io *beio, 1011264274Smav uint64_t off, uint64_t len, int last) 1012264274Smav{ 1013264274Smav struct bio *bio; 1014264296Smav uint64_t maxlen; 1015287664Smav struct cdevsw *csw; 1016287664Smav struct cdev *dev; 1017287664Smav int ref; 1018264274Smav 1019287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1020287499Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1021264274Smav while (len > 0) { 1022264274Smav bio = g_alloc_bio(); 1023264274Smav bio->bio_cmd = BIO_DELETE; 1024287664Smav bio->bio_dev = dev; 1025264274Smav bio->bio_offset = off; 1026264296Smav bio->bio_length = MIN(len, maxlen); 1027264274Smav bio->bio_data = 0; 1028264274Smav bio->bio_done = ctl_be_block_biodone; 1029264274Smav bio->bio_caller1 = beio; 1030287499Smav bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1031264274Smav 1032264274Smav off += bio->bio_length; 1033264274Smav len -= bio->bio_length; 1034264274Smav 1035267877Smav mtx_lock(&be_lun->io_lock); 1036264274Smav beio->num_bios_sent++; 1037264274Smav if (last && len == 0) 1038264274Smav beio->send_complete = 1; 1039267877Smav mtx_unlock(&be_lun->io_lock); 1040264274Smav 1041287664Smav if (csw) { 1042287664Smav csw->d_strategy(bio); 1043287664Smav } else { 1044287664Smav bio->bio_error = ENXIO; 1045287664Smav ctl_be_block_biodone(bio); 1046287664Smav } 1047264274Smav } 1048287664Smav if (csw) 1049287664Smav dev_relthread(dev, ref); 1050264274Smav} 1051264274Smav 1052264274Smavstatic void 1053264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1054264274Smav struct ctl_be_block_io *beio) 1055264274Smav{ 1056264274Smav union ctl_io *io; 1057267515Smav struct ctl_ptr_len_flags *ptrlen; 1058264274Smav struct scsi_unmap_desc *buf, *end; 1059264274Smav uint64_t len; 1060264274Smav 1061264274Smav io = beio->io; 1062264274Smav 1063264274Smav DPRINTF("entered\n"); 1064264274Smav 1065264274Smav binuptime(&beio->ds_t0); 1066267877Smav mtx_lock(&be_lun->io_lock); 1067264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1068267877Smav mtx_unlock(&be_lun->io_lock); 1069264274Smav 1070264274Smav if (beio->io_offset == -1) { 1071264274Smav beio->io_len = 0; 1072267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1073267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1074267515Smav end = buf + ptrlen->len / sizeof(*buf); 1075264274Smav for (; buf < end; buf++) { 1076264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1077287499Smav be_lun->cbe_lun.blocksize; 1078264274Smav beio->io_len += len; 1079264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1080287499Smav scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 1081287499Smav len, (end - buf < 2) ? TRUE : FALSE); 1082264274Smav } 1083264274Smav } else 1084264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1085264274Smav beio->io_offset, beio->io_len, TRUE); 1086264274Smav} 1087264274Smav 1088264274Smavstatic void 1089229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1090229997Sken struct ctl_be_block_io *beio) 1091229997Sken{ 1092267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1093229997Sken struct bio *bio; 1094287664Smav struct cdevsw *csw; 1095287664Smav struct cdev *dev; 1096229997Sken off_t cur_offset; 1097287664Smav int i, max_iosize, ref; 1098229997Sken 1099229997Sken DPRINTF("entered\n"); 1100287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1101229997Sken 1102229997Sken /* 1103229997Sken * We have to limit our I/O size to the maximum supported by the 1104229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1105229997Sken * set it properly, use DFLTPHYS. 1106229997Sken */ 1107287664Smav if (csw) { 1108287664Smav max_iosize = dev->si_iosize_max; 1109287664Smav if (max_iosize < PAGE_SIZE) 1110287664Smav max_iosize = DFLTPHYS; 1111287664Smav } else 1112229997Sken max_iosize = DFLTPHYS; 1113229997Sken 1114229997Sken cur_offset = beio->io_offset; 1115229997Sken for (i = 0; i < beio->num_segs; i++) { 1116229997Sken size_t cur_size; 1117229997Sken uint8_t *cur_ptr; 1118229997Sken 1119229997Sken cur_size = beio->sg_segs[i].len; 1120229997Sken cur_ptr = beio->sg_segs[i].addr; 1121229997Sken 1122229997Sken while (cur_size > 0) { 1123229997Sken /* This can't fail, it's a blocking allocation. */ 1124229997Sken bio = g_alloc_bio(); 1125229997Sken 1126229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1127229997Sken 1128229997Sken bio->bio_cmd = beio->bio_cmd; 1129287664Smav bio->bio_dev = dev; 1130229997Sken bio->bio_caller1 = beio; 1131229997Sken bio->bio_length = min(cur_size, max_iosize); 1132229997Sken bio->bio_offset = cur_offset; 1133229997Sken bio->bio_data = cur_ptr; 1134229997Sken bio->bio_done = ctl_be_block_biodone; 1135287499Smav bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1136229997Sken 1137229997Sken cur_offset += bio->bio_length; 1138229997Sken cur_ptr += bio->bio_length; 1139229997Sken cur_size -= bio->bio_length; 1140229997Sken 1141267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1142229997Sken beio->num_bios_sent++; 1143229997Sken } 1144229997Sken } 1145267877Smav binuptime(&beio->ds_t0); 1146267877Smav mtx_lock(&be_lun->io_lock); 1147267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1148267877Smav beio->send_complete = 1; 1149267877Smav mtx_unlock(&be_lun->io_lock); 1150267877Smav 1151267877Smav /* 1152267877Smav * Fire off all allocated requests! 1153267877Smav */ 1154267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1155267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1156287664Smav if (csw) 1157287664Smav csw->d_strategy(bio); 1158287664Smav else { 1159287664Smav bio->bio_error = ENXIO; 1160287664Smav ctl_be_block_biodone(bio); 1161287664Smav } 1162267877Smav } 1163287664Smav if (csw) 1164287664Smav dev_relthread(dev, ref); 1165229997Sken} 1166229997Sken 1167274154Smavstatic uint64_t 1168274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1169274154Smav{ 1170274154Smav struct diocgattr_arg arg; 1171287664Smav struct cdevsw *csw; 1172287664Smav struct cdev *dev; 1173287664Smav int error, ref; 1174274154Smav 1175287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1176287664Smav if (csw == NULL) 1177274154Smav return (UINT64_MAX); 1178274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1179274154Smav arg.len = sizeof(arg.value.off); 1180287664Smav if (csw->d_ioctl) { 1181287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 1182287664Smav curthread); 1183287664Smav } else 1184287664Smav error = ENODEV; 1185287664Smav dev_relthread(dev, ref); 1186274154Smav if (error != 0) 1187274154Smav return (UINT64_MAX); 1188274154Smav return (arg.value.off); 1189274154Smav} 1190274154Smav 1191229997Skenstatic void 1192286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1193286353Smav union ctl_io *io) 1194286353Smav{ 1195287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1196286353Smav struct ctl_be_block_io *beio; 1197286353Smav struct ctl_lba_len_flags *lbalen; 1198286353Smav 1199286353Smav DPRINTF("entered\n"); 1200286353Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1201286353Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1202286353Smav 1203287499Smav beio->io_len = lbalen->len * cbe_lun->blocksize; 1204287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1205286353Smav beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1206286353Smav beio->bio_cmd = BIO_FLUSH; 1207286353Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1208286353Smav DPRINTF("SYNC\n"); 1209286353Smav be_lun->lun_flush(be_lun, beio); 1210286353Smav} 1211286353Smav 1212286353Smavstatic void 1213264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1214264274Smav{ 1215264274Smav union ctl_io *io; 1216264274Smav 1217264274Smav io = beio->io; 1218264274Smav ctl_free_beio(beio); 1219267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1220267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1221267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1222264274Smav ctl_config_write_done(io); 1223264274Smav return; 1224264274Smav } 1225264274Smav 1226264274Smav ctl_be_block_config_write(io); 1227264274Smav} 1228264274Smav 1229264274Smavstatic void 1230264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1231264274Smav union ctl_io *io) 1232264274Smav{ 1233287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1234264274Smav struct ctl_be_block_io *beio; 1235267515Smav struct ctl_lba_len_flags *lbalen; 1236278625Smav uint64_t len_left, lba; 1237278625Smav uint32_t pb, pbo, adj; 1238264274Smav int i, seglen; 1239264274Smav uint8_t *buf, *end; 1240264274Smav 1241264274Smav DPRINTF("entered\n"); 1242264274Smav 1243267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1244267537Smav lbalen = ARGS(beio->io); 1245264274Smav 1246271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1247269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1248264274Smav ctl_free_beio(beio); 1249264274Smav ctl_set_invalid_field(&io->scsiio, 1250264274Smav /*sks_valid*/ 1, 1251264274Smav /*command*/ 1, 1252264274Smav /*field*/ 1, 1253264274Smav /*bit_valid*/ 0, 1254264274Smav /*bit*/ 0); 1255264274Smav ctl_config_write_done(io); 1256264274Smav return; 1257264274Smav } 1258264274Smav 1259269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1260287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1261287499Smav beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1262264274Smav beio->bio_cmd = BIO_DELETE; 1263264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1264264274Smav 1265264274Smav be_lun->unmap(be_lun, beio); 1266264274Smav return; 1267264274Smav } 1268264274Smav 1269264274Smav beio->bio_cmd = BIO_WRITE; 1270264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1271264274Smav 1272264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1273267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1274264274Smav 1275287499Smav pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1276287499Smav if (be_lun->cbe_lun.pblockoff > 0) 1277287499Smav pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1278278625Smav else 1279278625Smav pbo = 0; 1280287499Smav len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1281264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1282264274Smav 1283264274Smav /* 1284264274Smav * Setup the S/G entry for this chunk. 1285264274Smav */ 1286264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1287287499Smav if (pb > cbe_lun->blocksize) { 1288287499Smav adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1289278619Smav seglen - pbo) % pb; 1290278619Smav if (seglen > adj) 1291278619Smav seglen -= adj; 1292278619Smav else 1293287499Smav seglen -= seglen % cbe_lun->blocksize; 1294278619Smav } else 1295287499Smav seglen -= seglen % cbe_lun->blocksize; 1296264274Smav beio->sg_segs[i].len = seglen; 1297264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1298264274Smav 1299264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1300264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1301264274Smav 1302264274Smav beio->num_segs++; 1303264274Smav len_left -= seglen; 1304264274Smav 1305264274Smav buf = beio->sg_segs[i].addr; 1306264274Smav end = buf + seglen; 1307287499Smav for (; buf < end; buf += cbe_lun->blocksize) { 1308287499Smav memcpy(buf, io->scsiio.kern_data_ptr, cbe_lun->blocksize); 1309267515Smav if (lbalen->flags & SWS_LBDATA) 1310267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1311264274Smav lba++; 1312264274Smav } 1313264274Smav } 1314264274Smav 1315287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1316287499Smav beio->io_len = lba * cbe_lun->blocksize; 1317264274Smav 1318264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1319264274Smav if (len_left > 0) { 1320267515Smav lbalen->lba += lba; 1321267515Smav lbalen->len -= lba; 1322264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1323264274Smav } 1324264274Smav 1325264274Smav be_lun->dispatch(be_lun, beio); 1326264274Smav} 1327264274Smav 1328264274Smavstatic void 1329264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1330264274Smav union ctl_io *io) 1331264274Smav{ 1332264274Smav struct ctl_be_block_io *beio; 1333267515Smav struct ctl_ptr_len_flags *ptrlen; 1334264274Smav 1335264274Smav DPRINTF("entered\n"); 1336264274Smav 1337267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1338267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1339264274Smav 1340269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1341264274Smav ctl_free_beio(beio); 1342264274Smav ctl_set_invalid_field(&io->scsiio, 1343264274Smav /*sks_valid*/ 0, 1344264274Smav /*command*/ 1, 1345264274Smav /*field*/ 0, 1346264274Smav /*bit_valid*/ 0, 1347264274Smav /*bit*/ 0); 1348264274Smav ctl_config_write_done(io); 1349264274Smav return; 1350264274Smav } 1351264274Smav 1352264274Smav beio->io_len = 0; 1353264274Smav beio->io_offset = -1; 1354264274Smav beio->bio_cmd = BIO_DELETE; 1355264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1356267515Smav DPRINTF("UNMAP\n"); 1357264274Smav be_lun->unmap(be_lun, beio); 1358264274Smav} 1359264274Smav 1360264274Smavstatic void 1361275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1362275474Smav{ 1363275474Smav union ctl_io *io; 1364275474Smav 1365275474Smav io = beio->io; 1366275474Smav ctl_free_beio(beio); 1367275474Smav ctl_config_read_done(io); 1368275474Smav} 1369275474Smav 1370275474Smavstatic void 1371275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1372275474Smav union ctl_io *io) 1373275474Smav{ 1374275474Smav struct ctl_be_block_io *beio; 1375275474Smav struct ctl_be_block_softc *softc; 1376275474Smav 1377275474Smav DPRINTF("entered\n"); 1378275474Smav 1379275474Smav softc = be_lun->softc; 1380275474Smav beio = ctl_alloc_beio(softc); 1381275474Smav beio->io = io; 1382275474Smav beio->lun = be_lun; 1383275474Smav beio->beio_cont = ctl_be_block_cr_done; 1384275474Smav PRIV(io)->ptr = (void *)beio; 1385275474Smav 1386275474Smav switch (io->scsiio.cdb[0]) { 1387275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1388275474Smav beio->bio_cmd = -1; 1389275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1390275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1391275474Smav beio->io_len = 0; 1392275474Smav if (be_lun->get_lba_status) 1393275474Smav be_lun->get_lba_status(be_lun, beio); 1394275474Smav else 1395275474Smav ctl_be_block_cr_done(beio); 1396275474Smav break; 1397275474Smav default: 1398275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1399275474Smav break; 1400275474Smav } 1401275474Smav} 1402275474Smav 1403275474Smavstatic void 1404264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1405264274Smav{ 1406264274Smav union ctl_io *io; 1407264274Smav 1408264274Smav io = beio->io; 1409264274Smav ctl_free_beio(beio); 1410264274Smav ctl_config_write_done(io); 1411264274Smav} 1412264274Smav 1413264274Smavstatic void 1414229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1415229997Sken union ctl_io *io) 1416229997Sken{ 1417229997Sken struct ctl_be_block_io *beio; 1418229997Sken struct ctl_be_block_softc *softc; 1419229997Sken 1420229997Sken DPRINTF("entered\n"); 1421229997Sken 1422229997Sken softc = be_lun->softc; 1423229997Sken beio = ctl_alloc_beio(softc); 1424229997Sken beio->io = io; 1425229997Sken beio->lun = be_lun; 1426264274Smav beio->beio_cont = ctl_be_block_cw_done; 1427286353Smav switch (io->scsiio.tag_type) { 1428286353Smav case CTL_TAG_ORDERED: 1429286353Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1430286353Smav break; 1431286353Smav case CTL_TAG_HEAD_OF_QUEUE: 1432286353Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1433286353Smav break; 1434286353Smav case CTL_TAG_UNTAGGED: 1435286353Smav case CTL_TAG_SIMPLE: 1436286353Smav case CTL_TAG_ACA: 1437286353Smav default: 1438286353Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1439286353Smav break; 1440286353Smav } 1441267519Smav PRIV(io)->ptr = (void *)beio; 1442229997Sken 1443229997Sken switch (io->scsiio.cdb[0]) { 1444229997Sken case SYNCHRONIZE_CACHE: 1445229997Sken case SYNCHRONIZE_CACHE_16: 1446286353Smav ctl_be_block_cw_dispatch_sync(be_lun, io); 1447229997Sken break; 1448264274Smav case WRITE_SAME_10: 1449264274Smav case WRITE_SAME_16: 1450264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1451264274Smav break; 1452264274Smav case UNMAP: 1453264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1454264274Smav break; 1455229997Sken default: 1456229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1457229997Sken break; 1458229997Sken } 1459229997Sken} 1460229997Sken 1461258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1462258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1463258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1464258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1465229997Sken 1466229997Skenstatic void 1467264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1468264886Smav{ 1469264886Smav struct ctl_be_block_lun *be_lun; 1470264886Smav union ctl_io *io; 1471264886Smav 1472264886Smav io = beio->io; 1473264886Smav be_lun = beio->lun; 1474264886Smav ctl_free_beio(beio); 1475267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1476267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1477267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1478267537Smav ctl_data_submit_done(io); 1479264886Smav return; 1480264886Smav } 1481264886Smav 1482264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1483264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1484264886Smav 1485267877Smav mtx_lock(&be_lun->queue_lock); 1486264886Smav /* 1487264886Smav * XXX KDM make sure that links is okay to use at this point. 1488264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1489264886Smav * or deal with resource allocation here. 1490264886Smav */ 1491264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1492267877Smav mtx_unlock(&be_lun->queue_lock); 1493264886Smav 1494264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1495264886Smav} 1496264886Smav 1497264886Smavstatic void 1498229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1499229997Sken union ctl_io *io) 1500229997Sken{ 1501287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1502229997Sken struct ctl_be_block_io *beio; 1503229997Sken struct ctl_be_block_softc *softc; 1504267537Smav struct ctl_lba_len_flags *lbalen; 1505267519Smav struct ctl_ptr_len_flags *bptrlen; 1506267519Smav uint64_t len_left, lbas; 1507229997Sken int i; 1508229997Sken 1509229997Sken softc = be_lun->softc; 1510229997Sken 1511229997Sken DPRINTF("entered\n"); 1512229997Sken 1513267537Smav lbalen = ARGS(io); 1514267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1515267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1516267537Smav } else { 1517229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1518229997Sken } 1519229997Sken 1520229997Sken beio = ctl_alloc_beio(softc); 1521229997Sken beio->io = io; 1522229997Sken beio->lun = be_lun; 1523267519Smav bptrlen = PRIV(io); 1524267519Smav bptrlen->ptr = (void *)beio; 1525229997Sken 1526229997Sken switch (io->scsiio.tag_type) { 1527229997Sken case CTL_TAG_ORDERED: 1528229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1529229997Sken break; 1530229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1531229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1532229997Sken break; 1533229997Sken case CTL_TAG_UNTAGGED: 1534229997Sken case CTL_TAG_SIMPLE: 1535229997Sken case CTL_TAG_ACA: 1536229997Sken default: 1537229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1538229997Sken break; 1539229997Sken } 1540229997Sken 1541267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1542267537Smav beio->bio_cmd = BIO_WRITE; 1543267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1544267537Smav } else { 1545229997Sken beio->bio_cmd = BIO_READ; 1546229997Sken beio->ds_trans_type = DEVSTAT_READ; 1547229997Sken } 1548229997Sken 1549264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1550229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1551267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1552267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1553267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1554267537Smav else 1555267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1556287499Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 1557287499Smav beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 1558287499Smav beio->io_len = lbas * cbe_lun->blocksize; 1559267519Smav bptrlen->len += lbas; 1560229997Sken 1561264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1562264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1563264886Smav i, CTLBLK_MAX_SEGS)); 1564229997Sken 1565229997Sken /* 1566229997Sken * Setup the S/G entry for this chunk. 1567229997Sken */ 1568264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1569229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1570229997Sken 1571229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1572229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1573229997Sken 1574267537Smav /* Set up second segment for compare operation. */ 1575267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1576267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1577267537Smav beio->sg_segs[i].len; 1578267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1579267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1580267537Smav } 1581267537Smav 1582229997Sken beio->num_segs++; 1583229997Sken len_left -= beio->sg_segs[i].len; 1584229997Sken } 1585267519Smav if (bptrlen->len < lbalen->len) 1586264886Smav beio->beio_cont = ctl_be_block_next; 1587264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1588267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1589267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1590267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1591267537Smav else 1592267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1593264886Smav io->scsiio.kern_data_len = beio->io_len; 1594264886Smav io->scsiio.kern_data_resid = 0; 1595264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1596264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1597229997Sken 1598229997Sken /* 1599229997Sken * For the read case, we need to read the data into our buffers and 1600229997Sken * then we can send it back to the user. For the write case, we 1601229997Sken * need to get the data from the user first. 1602229997Sken */ 1603229997Sken if (beio->bio_cmd == BIO_READ) { 1604229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1605229997Sken be_lun->dispatch(be_lun, beio); 1606229997Sken } else { 1607229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1608229997Sken#ifdef CTL_TIME_IO 1609229997Sken getbintime(&io->io_hdr.dma_start_bt); 1610229997Sken#endif 1611229997Sken ctl_datamove(io); 1612229997Sken } 1613229997Sken} 1614229997Sken 1615229997Skenstatic void 1616229997Skenctl_be_block_worker(void *context, int pending) 1617229997Sken{ 1618287670Smav struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1619287670Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1620229997Sken union ctl_io *io; 1621287670Smav struct ctl_be_block_io *beio; 1622229997Sken 1623229997Sken DPRINTF("entered\n"); 1624287670Smav /* 1625287670Smav * Fetch and process I/Os from all queues. If we detect LUN 1626287670Smav * CTL_LUN_FLAG_OFFLINE status here -- it is result of a race, 1627287670Smav * so make response maximally opaque to not confuse initiator. 1628287670Smav */ 1629229997Sken for (;;) { 1630287670Smav mtx_lock(&be_lun->queue_lock); 1631229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1632229997Sken if (io != NULL) { 1633229997Sken DPRINTF("datamove queue\n"); 1634229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1635229997Sken ctl_io_hdr, links); 1636267877Smav mtx_unlock(&be_lun->queue_lock); 1637267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1638287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1639287670Smav ctl_set_busy(&io->scsiio); 1640287670Smav ctl_complete_beio(beio); 1641287670Smav return; 1642287670Smav } 1643229997Sken be_lun->dispatch(be_lun, beio); 1644229997Sken continue; 1645229997Sken } 1646229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1647229997Sken if (io != NULL) { 1648229997Sken DPRINTF("config write queue\n"); 1649229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1650229997Sken ctl_io_hdr, links); 1651267877Smav mtx_unlock(&be_lun->queue_lock); 1652287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1653287670Smav ctl_set_busy(&io->scsiio); 1654287670Smav ctl_config_write_done(io); 1655287670Smav return; 1656287670Smav } 1657229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1658229997Sken continue; 1659229997Sken } 1660275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1661275474Smav if (io != NULL) { 1662275474Smav DPRINTF("config read queue\n"); 1663275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1664275474Smav ctl_io_hdr, links); 1665275474Smav mtx_unlock(&be_lun->queue_lock); 1666287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1667287670Smav ctl_set_busy(&io->scsiio); 1668287670Smav ctl_config_read_done(io); 1669287670Smav return; 1670287670Smav } 1671275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1672275474Smav continue; 1673275474Smav } 1674229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1675229997Sken if (io != NULL) { 1676229997Sken DPRINTF("input queue\n"); 1677229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1678229997Sken ctl_io_hdr, links); 1679267877Smav mtx_unlock(&be_lun->queue_lock); 1680287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1681287670Smav ctl_set_busy(&io->scsiio); 1682287670Smav ctl_data_submit_done(io); 1683287670Smav return; 1684287670Smav } 1685229997Sken ctl_be_block_dispatch(be_lun, io); 1686229997Sken continue; 1687229997Sken } 1688229997Sken 1689229997Sken /* 1690229997Sken * If we get here, there is no work left in the queues, so 1691229997Sken * just break out and let the task queue go to sleep. 1692229997Sken */ 1693287670Smav mtx_unlock(&be_lun->queue_lock); 1694229997Sken break; 1695229997Sken } 1696229997Sken} 1697229997Sken 1698229997Sken/* 1699229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1700229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1701229997Sken * thread. 1702229997Sken */ 1703229997Skenstatic int 1704229997Skenctl_be_block_submit(union ctl_io *io) 1705229997Sken{ 1706229997Sken struct ctl_be_block_lun *be_lun; 1707287499Smav struct ctl_be_lun *cbe_lun; 1708229997Sken 1709229997Sken DPRINTF("entered\n"); 1710229997Sken 1711287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1712229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1713287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1714229997Sken 1715229997Sken /* 1716229997Sken * Make sure we only get SCSI I/O. 1717229997Sken */ 1718229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1719229997Sken "%#x) encountered", io->io_hdr.io_type)); 1720229997Sken 1721267519Smav PRIV(io)->len = 0; 1722267519Smav 1723267877Smav mtx_lock(&be_lun->queue_lock); 1724229997Sken /* 1725229997Sken * XXX KDM make sure that links is okay to use at this point. 1726229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1727229997Sken * or deal with resource allocation here. 1728229997Sken */ 1729229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1730267877Smav mtx_unlock(&be_lun->queue_lock); 1731229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1732229997Sken 1733267514Smav return (CTL_RETVAL_COMPLETE); 1734229997Sken} 1735229997Sken 1736229997Skenstatic int 1737229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1738229997Sken int flag, struct thread *td) 1739229997Sken{ 1740229997Sken struct ctl_be_block_softc *softc; 1741229997Sken int error; 1742229997Sken 1743229997Sken softc = &backend_block_softc; 1744229997Sken 1745229997Sken error = 0; 1746229997Sken 1747229997Sken switch (cmd) { 1748229997Sken case CTL_LUN_REQ: { 1749229997Sken struct ctl_lun_req *lun_req; 1750229997Sken 1751229997Sken lun_req = (struct ctl_lun_req *)addr; 1752229997Sken 1753229997Sken switch (lun_req->reqtype) { 1754229997Sken case CTL_LUNREQ_CREATE: 1755229997Sken error = ctl_be_block_create(softc, lun_req); 1756229997Sken break; 1757229997Sken case CTL_LUNREQ_RM: 1758229997Sken error = ctl_be_block_rm(softc, lun_req); 1759229997Sken break; 1760232604Strasz case CTL_LUNREQ_MODIFY: 1761232604Strasz error = ctl_be_block_modify(softc, lun_req); 1762232604Strasz break; 1763229997Sken default: 1764229997Sken lun_req->status = CTL_LUN_ERROR; 1765229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1766272911Smav "invalid LUN request type %d", 1767229997Sken lun_req->reqtype); 1768229997Sken break; 1769229997Sken } 1770229997Sken break; 1771229997Sken } 1772229997Sken default: 1773229997Sken error = ENOTTY; 1774229997Sken break; 1775229997Sken } 1776229997Sken 1777229997Sken return (error); 1778229997Sken} 1779229997Sken 1780229997Skenstatic int 1781229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1782229997Sken{ 1783287499Smav struct ctl_be_lun *cbe_lun; 1784229997Sken struct ctl_be_block_filedata *file_data; 1785229997Sken struct ctl_lun_create_params *params; 1786275865Smav char *value; 1787229997Sken struct vattr vattr; 1788275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1789229997Sken int error; 1790229997Sken 1791229997Sken error = 0; 1792287499Smav cbe_lun = &be_lun->cbe_lun; 1793229997Sken file_data = &be_lun->backend.file; 1794272911Smav params = &be_lun->params; 1795229997Sken 1796229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1797229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1798229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1799275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1800275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1801287499Smav be_lun->unmap = NULL; 1802287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1803229997Sken 1804229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1805229997Sken if (error != 0) { 1806229997Sken snprintf(req->error_str, sizeof(req->error_str), 1807229997Sken "error calling VOP_GETATTR() for file %s", 1808229997Sken be_lun->dev_path); 1809229997Sken return (error); 1810229997Sken } 1811229997Sken 1812229997Sken /* 1813229997Sken * Verify that we have the ability to upgrade to exclusive 1814229997Sken * access on this file so we can trap errors at open instead 1815229997Sken * of reporting them during first access. 1816229997Sken */ 1817229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1818229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1819229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1820229997Sken error = EBADF; 1821229997Sken snprintf(req->error_str, sizeof(req->error_str), 1822229997Sken "error locking file %s", be_lun->dev_path); 1823229997Sken return (error); 1824229997Sken } 1825229997Sken } 1826229997Sken 1827229997Sken file_data->cred = crhold(curthread->td_ucred); 1828232604Strasz if (params->lun_size_bytes != 0) 1829232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1830232604Strasz else 1831232604Strasz be_lun->size_bytes = vattr.va_size; 1832229997Sken 1833229997Sken /* 1834273029Smav * For files we can use any logical block size. Prefer 512 bytes 1835273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1836273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1837273029Smav * logical block size -- report it as physical block size. 1838229997Sken */ 1839229997Sken if (params->blocksize_bytes != 0) 1840287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1841229997Sken else 1842287499Smav cbe_lun->blocksize = 512; 1843287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1844287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1845287499Smav 0 : (be_lun->size_blocks - 1); 1846275865Smav 1847275865Smav us = ps = vattr.va_blocksize; 1848275865Smav uo = po = 0; 1849275865Smav 1850287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1851275865Smav if (value != NULL) 1852275865Smav ctl_expand_number(value, &ps); 1853287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1854275865Smav if (value != NULL) 1855275865Smav ctl_expand_number(value, &po); 1856287499Smav pss = ps / cbe_lun->blocksize; 1857287499Smav pos = po / cbe_lun->blocksize; 1858287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1859287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1860287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1861287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1862273029Smav } 1863229997Sken 1864287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1865275865Smav if (value != NULL) 1866275865Smav ctl_expand_number(value, &us); 1867287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1868275865Smav if (value != NULL) 1869275865Smav ctl_expand_number(value, &uo); 1870287499Smav uss = us / cbe_lun->blocksize; 1871287499Smav uos = uo / cbe_lun->blocksize; 1872287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1873287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1874287499Smav cbe_lun->ublockexp = fls(uss) - 1; 1875287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 1876275865Smav } 1877275865Smav 1878229997Sken /* 1879229997Sken * Sanity check. The media size has to be at least one 1880229997Sken * sector long. 1881229997Sken */ 1882287499Smav if (be_lun->size_bytes < cbe_lun->blocksize) { 1883229997Sken error = EINVAL; 1884229997Sken snprintf(req->error_str, sizeof(req->error_str), 1885229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1886287499Smav (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1887229997Sken } 1888275920Smav 1889287499Smav cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1890229997Sken return (error); 1891229997Sken} 1892229997Sken 1893229997Skenstatic int 1894229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1895229997Sken{ 1896287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1897229997Sken struct ctl_lun_create_params *params; 1898287664Smav struct cdevsw *csw; 1899229997Sken struct cdev *dev; 1900275865Smav char *value; 1901287664Smav int error, atomic, maxio, ref, unmap, tmp; 1902287221Smav off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1903229997Sken 1904272911Smav params = &be_lun->params; 1905229997Sken 1906229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1907287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1908287664Smav if (csw == NULL) 1909287664Smav return (ENXIO); 1910287664Smav if (strcmp(csw->d_name, "zvol") == 0) { 1911269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1912275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1913275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1914275920Smav } else { 1915269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1916287499Smav be_lun->get_lba_status = NULL; 1917275920Smav atomic = 0; 1918287664Smav maxio = dev->si_iosize_max; 1919275920Smav if (maxio <= 0) 1920275920Smav maxio = DFLTPHYS; 1921275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1922275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1923275920Smav } 1924269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1925274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1926287499Smav be_lun->unmap = ctl_be_block_unmap_dev; 1927229997Sken 1928287664Smav if (!csw->d_ioctl) { 1929287664Smav dev_relthread(dev, ref); 1930229997Sken snprintf(req->error_str, sizeof(req->error_str), 1931287664Smav "no d_ioctl for device %s!", be_lun->dev_path); 1932229997Sken return (ENODEV); 1933229997Sken } 1934229997Sken 1935287664Smav error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1936229997Sken curthread); 1937229997Sken if (error) { 1938287664Smav dev_relthread(dev, ref); 1939229997Sken snprintf(req->error_str, sizeof(req->error_str), 1940272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1941272911Smav "on %s!", error, be_lun->dev_path); 1942229997Sken return (error); 1943229997Sken } 1944229997Sken 1945229997Sken /* 1946229997Sken * If the user has asked for a blocksize that is greater than the 1947229997Sken * backing device's blocksize, we can do it only if the blocksize 1948229997Sken * the user is asking for is an even multiple of the underlying 1949229997Sken * device's blocksize. 1950229997Sken */ 1951286811Smav if ((params->blocksize_bytes != 0) && 1952286811Smav (params->blocksize_bytes >= tmp)) { 1953286811Smav if (params->blocksize_bytes % tmp == 0) { 1954287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1955229997Sken } else { 1956287664Smav dev_relthread(dev, ref); 1957229997Sken snprintf(req->error_str, sizeof(req->error_str), 1958272911Smav "requested blocksize %u is not an even " 1959229997Sken "multiple of backing device blocksize %u", 1960287221Smav params->blocksize_bytes, tmp); 1961229997Sken return (EINVAL); 1962229997Sken } 1963286811Smav } else if (params->blocksize_bytes != 0) { 1964287664Smav dev_relthread(dev, ref); 1965229997Sken snprintf(req->error_str, sizeof(req->error_str), 1966272911Smav "requested blocksize %u < backing device " 1967287221Smav "blocksize %u", params->blocksize_bytes, tmp); 1968229997Sken return (EINVAL); 1969286811Smav } else 1970287499Smav cbe_lun->blocksize = tmp; 1971229997Sken 1972287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1973287664Smav curthread); 1974229997Sken if (error) { 1975287664Smav dev_relthread(dev, ref); 1976229997Sken snprintf(req->error_str, sizeof(req->error_str), 1977272911Smav "error %d returned for DIOCGMEDIASIZE " 1978272911Smav " ioctl on %s!", error, 1979232604Strasz be_lun->dev_path); 1980229997Sken return (error); 1981229997Sken } 1982229997Sken 1983232604Strasz if (params->lun_size_bytes != 0) { 1984287221Smav if (params->lun_size_bytes > otmp) { 1985287664Smav dev_relthread(dev, ref); 1986232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1987272911Smav "requested LUN size %ju > backing device " 1988272911Smav "size %ju", 1989232604Strasz (uintmax_t)params->lun_size_bytes, 1990287221Smav (uintmax_t)otmp); 1991232604Strasz return (EINVAL); 1992232604Strasz } 1993232604Strasz 1994232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1995286811Smav } else 1996287221Smav be_lun->size_bytes = otmp; 1997287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1998287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1999287499Smav 0 : (be_lun->size_blocks - 1); 2000232604Strasz 2001287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 2002287664Smav curthread); 2003264191Smav if (error) 2004264191Smav ps = po = 0; 2005264191Smav else { 2006287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2007287664Smav FREAD, curthread); 2008264191Smav if (error) 2009264191Smav po = 0; 2010264191Smav } 2011275865Smav us = ps; 2012275865Smav uo = po; 2013275865Smav 2014287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 2015275865Smav if (value != NULL) 2016275865Smav ctl_expand_number(value, &ps); 2017287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 2018275865Smav if (value != NULL) 2019275865Smav ctl_expand_number(value, &po); 2020287499Smav pss = ps / cbe_lun->blocksize; 2021287499Smav pos = po / cbe_lun->blocksize; 2022287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2023287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2024287499Smav cbe_lun->pblockexp = fls(pss) - 1; 2025287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 2026264191Smav } 2027264191Smav 2028287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 2029275865Smav if (value != NULL) 2030275865Smav ctl_expand_number(value, &us); 2031287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 2032275865Smav if (value != NULL) 2033275865Smav ctl_expand_number(value, &uo); 2034287499Smav uss = us / cbe_lun->blocksize; 2035287499Smav uos = uo / cbe_lun->blocksize; 2036287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2037287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2038287499Smav cbe_lun->ublockexp = fls(uss) - 1; 2039287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 2040275865Smav } 2041275865Smav 2042287499Smav cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2043287499Smav cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2044278672Smav 2045278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2046278672Smav unmap = 1; 2047278672Smav } else { 2048278672Smav struct diocgattr_arg arg; 2049278672Smav 2050278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2051278672Smav arg.len = sizeof(arg.value.i); 2052287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2053287664Smav curthread); 2054278672Smav unmap = (error == 0) ? arg.value.i : 0; 2055278672Smav } 2056287499Smav value = ctl_get_opt(&cbe_lun->options, "unmap"); 2057278672Smav if (value != NULL) 2058278672Smav unmap = (strcmp(value, "on") == 0); 2059278672Smav if (unmap) 2060287499Smav cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2061287499Smav else 2062287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2063278672Smav 2064287664Smav dev_relthread(dev, ref); 2065229997Sken return (0); 2066229997Sken} 2067229997Sken 2068229997Skenstatic int 2069229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2070229997Sken{ 2071287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2072287499Smav int flags; 2073287499Smav 2074229997Sken if (be_lun->vn) { 2075287499Smav flags = FREAD; 2076287499Smav if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2077287499Smav flags |= FWRITE; 2078229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2079229997Sken be_lun->vn = NULL; 2080229997Sken 2081229997Sken switch (be_lun->dev_type) { 2082229997Sken case CTL_BE_BLOCK_DEV: 2083229997Sken break; 2084229997Sken case CTL_BE_BLOCK_FILE: 2085229997Sken if (be_lun->backend.file.cred != NULL) { 2086229997Sken crfree(be_lun->backend.file.cred); 2087229997Sken be_lun->backend.file.cred = NULL; 2088229997Sken } 2089229997Sken break; 2090229997Sken case CTL_BE_BLOCK_NONE: 2091258871Strasz break; 2092229997Sken default: 2093229997Sken panic("Unexpected backend type."); 2094229997Sken break; 2095229997Sken } 2096272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2097229997Sken } 2098229997Sken return (0); 2099229997Sken} 2100229997Sken 2101229997Skenstatic int 2102229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 2103287499Smav struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2104229997Sken{ 2105287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2106229997Sken struct nameidata nd; 2107287499Smav char *value; 2108287499Smav int error, flags; 2109229997Sken 2110229997Sken error = 0; 2111229997Sken if (rootvnode == NULL) { 2112229997Sken snprintf(req->error_str, sizeof(req->error_str), 2113272911Smav "Root filesystem is not mounted"); 2114229997Sken return (1); 2115229997Sken } 2116285391Smjg pwd_ensure_dirs(); 2117229997Sken 2118287499Smav value = ctl_get_opt(&cbe_lun->options, "file"); 2119287499Smav if (value == NULL) { 2120287499Smav snprintf(req->error_str, sizeof(req->error_str), 2121287499Smav "no file argument specified"); 2122287499Smav return (1); 2123287499Smav } 2124287499Smav free(be_lun->dev_path, M_CTLBLK); 2125287499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2126287499Smav 2127287499Smav flags = FREAD; 2128287499Smav value = ctl_get_opt(&cbe_lun->options, "readonly"); 2129287499Smav if (value == NULL || strcmp(value, "on") != 0) 2130287499Smav flags |= FWRITE; 2131287499Smav 2132287499Smavagain: 2133229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2134229997Sken error = vn_open(&nd, &flags, 0, NULL); 2135287499Smav if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2136287499Smav flags &= ~FWRITE; 2137287499Smav goto again; 2138287499Smav } 2139229997Sken if (error) { 2140229997Sken /* 2141229997Sken * This is the only reasonable guess we can make as far as 2142229997Sken * path if the user doesn't give us a fully qualified path. 2143229997Sken * If they want to specify a file, they need to specify the 2144229997Sken * full path. 2145229997Sken */ 2146229997Sken if (be_lun->dev_path[0] != '/') { 2147229997Sken char *dev_name; 2148229997Sken 2149287499Smav asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2150287499Smav be_lun->dev_path); 2151287499Smav free(be_lun->dev_path, M_CTLBLK); 2152287499Smav be_lun->dev_path = dev_name; 2153287499Smav goto again; 2154229997Sken } 2155229997Sken snprintf(req->error_str, sizeof(req->error_str), 2156272911Smav "error opening %s: %d", be_lun->dev_path, error); 2157229997Sken return (error); 2158229997Sken } 2159287499Smav if (flags & FWRITE) 2160287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2161287499Smav else 2162287499Smav cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2163229997Sken 2164229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2165229997Sken be_lun->vn = nd.ni_vp; 2166229997Sken 2167229997Sken /* We only support disks and files. */ 2168229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2169229997Sken error = ctl_be_block_open_dev(be_lun, req); 2170229997Sken } else if (be_lun->vn->v_type == VREG) { 2171229997Sken error = ctl_be_block_open_file(be_lun, req); 2172229997Sken } else { 2173229997Sken error = EINVAL; 2174229997Sken snprintf(req->error_str, sizeof(req->error_str), 2175258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2176229997Sken } 2177229997Sken VOP_UNLOCK(be_lun->vn, 0); 2178229997Sken 2179286811Smav if (error != 0) 2180229997Sken ctl_be_block_close(be_lun); 2181287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2182287499Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2183287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2184287499Smav value = ctl_get_opt(&cbe_lun->options, "serseq"); 2185287499Smav if (value != NULL && strcmp(value, "on") == 0) 2186287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2187287499Smav else if (value != NULL && strcmp(value, "read") == 0) 2188287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2189287499Smav else if (value != NULL && strcmp(value, "off") == 0) 2190287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2191229997Sken return (0); 2192229997Sken} 2193229997Sken 2194229997Skenstatic int 2195229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2196229997Sken{ 2197287499Smav struct ctl_be_lun *cbe_lun; 2198229997Sken struct ctl_be_block_lun *be_lun; 2199229997Sken struct ctl_lun_create_params *params; 2200267481Smav char num_thread_str[16]; 2201229997Sken char tmpstr[32]; 2202267481Smav char *value; 2203278672Smav int retval, num_threads; 2204267481Smav int tmp_num_threads; 2205229997Sken 2206229997Sken params = &req->reqdata.create; 2207229997Sken retval = 0; 2208272911Smav req->status = CTL_LUN_OK; 2209229997Sken 2210229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2211287499Smav cbe_lun = &be_lun->cbe_lun; 2212287499Smav cbe_lun->be_lun = be_lun; 2213272911Smav be_lun->params = req->reqdata.create; 2214229997Sken be_lun->softc = softc; 2215229997Sken STAILQ_INIT(&be_lun->input_queue); 2216275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2217229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2218229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2219229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2220267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2221267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2222287499Smav ctl_init_opts(&cbe_lun->options, 2223268280Smav req->num_be_args, req->kern_be_args); 2224264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2225256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2226229997Sken if (be_lun->lun_zone == NULL) { 2227229997Sken snprintf(req->error_str, sizeof(req->error_str), 2228272911Smav "error allocating UMA zone"); 2229229997Sken goto bailout_error; 2230229997Sken } 2231229997Sken 2232229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2233287499Smav cbe_lun->lun_type = params->device_type; 2234229997Sken else 2235287499Smav cbe_lun->lun_type = T_DIRECT; 2236287499Smav be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2237287621Smav cbe_lun->flags = 0; 2238287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2239287621Smav if (value != NULL) { 2240287621Smav if (strcmp(value, "primary") == 0) 2241287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2242287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2243287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2244229997Sken 2245287499Smav if (cbe_lun->lun_type == T_DIRECT) { 2246286811Smav be_lun->size_bytes = params->lun_size_bytes; 2247286811Smav if (params->blocksize_bytes != 0) 2248287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2249286811Smav else 2250287499Smav cbe_lun->blocksize = 512; 2251287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2252287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2253287499Smav 0 : (be_lun->size_blocks - 1); 2254229997Sken 2255287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2256287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2257287621Smav retval = ctl_be_block_open(softc, be_lun, req); 2258287621Smav if (retval != 0) { 2259287621Smav retval = 0; 2260287621Smav req->status = CTL_LUN_WARNING; 2261287621Smav } 2262229997Sken } 2263287499Smav num_threads = cbb_num_threads; 2264229997Sken } else { 2265229997Sken num_threads = 1; 2266229997Sken } 2267229997Sken 2268229997Sken /* 2269229997Sken * XXX This searching loop might be refactored to be combined with 2270229997Sken * the loop above, 2271229997Sken */ 2272287499Smav value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2273267481Smav if (value != NULL) { 2274267481Smav tmp_num_threads = strtol(value, NULL, 0); 2275229997Sken 2276267481Smav /* 2277267481Smav * We don't let the user specify less than one 2278267481Smav * thread, but hope he's clueful enough not to 2279267481Smav * specify 1000 threads. 2280267481Smav */ 2281267481Smav if (tmp_num_threads < 1) { 2282267481Smav snprintf(req->error_str, sizeof(req->error_str), 2283272911Smav "invalid number of threads %s", 2284272911Smav num_thread_str); 2285267481Smav goto bailout_error; 2286229997Sken } 2287267481Smav num_threads = tmp_num_threads; 2288229997Sken } 2289229997Sken 2290272911Smav if (be_lun->vn == NULL) 2291287499Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2292229997Sken /* Tell the user the blocksize we ended up using */ 2293272911Smav params->lun_size_bytes = be_lun->size_bytes; 2294287499Smav params->blocksize_bytes = cbe_lun->blocksize; 2295229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2296287499Smav cbe_lun->req_lun_id = params->req_lun_id; 2297287499Smav cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2298229997Sken } else 2299287499Smav cbe_lun->req_lun_id = 0; 2300229997Sken 2301287499Smav cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2302287499Smav cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2303287499Smav cbe_lun->be = &ctl_be_block_driver; 2304229997Sken 2305229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2306229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2307229997Sken softc->num_luns); 2308287499Smav strncpy((char *)cbe_lun->serial_num, tmpstr, 2309287499Smav MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2310229997Sken 2311229997Sken /* Tell the user what we used for a serial number */ 2312229997Sken strncpy((char *)params->serial_num, tmpstr, 2313275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2314229997Sken } else { 2315287499Smav strncpy((char *)cbe_lun->serial_num, params->serial_num, 2316287499Smav MIN(sizeof(cbe_lun->serial_num), 2317229997Sken sizeof(params->serial_num))); 2318229997Sken } 2319229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2320229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2321287499Smav strncpy((char *)cbe_lun->device_id, tmpstr, 2322287499Smav MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2323229997Sken 2324229997Sken /* Tell the user what we used for a device ID */ 2325229997Sken strncpy((char *)params->device_id, tmpstr, 2326275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2327229997Sken } else { 2328287499Smav strncpy((char *)cbe_lun->device_id, params->device_id, 2329287499Smav MIN(sizeof(cbe_lun->device_id), 2330275953Smav sizeof(params->device_id))); 2331229997Sken } 2332229997Sken 2333229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2334229997Sken 2335229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2336229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2337229997Sken 2338229997Sken if (be_lun->io_taskqueue == NULL) { 2339229997Sken snprintf(req->error_str, sizeof(req->error_str), 2340272911Smav "unable to create taskqueue"); 2341229997Sken goto bailout_error; 2342229997Sken } 2343229997Sken 2344229997Sken /* 2345229997Sken * Note that we start the same number of threads by default for 2346229997Sken * both the file case and the block device case. For the file 2347229997Sken * case, we need multiple threads to allow concurrency, because the 2348229997Sken * vnode interface is designed to be a blocking interface. For the 2349229997Sken * block device case, ZFS zvols at least will block the caller's 2350229997Sken * context in many instances, and so we need multiple threads to 2351229997Sken * overcome that problem. Other block devices don't need as many 2352229997Sken * threads, but they shouldn't cause too many problems. 2353229997Sken * 2354229997Sken * If the user wants to just have a single thread for a block 2355229997Sken * device, he can specify that when the LUN is created, or change 2356229997Sken * the tunable/sysctl to alter the default number of threads. 2357229997Sken */ 2358229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2359229997Sken /*num threads*/num_threads, 2360229997Sken /*priority*/PWAIT, 2361229997Sken /*thread name*/ 2362229997Sken "%s taskq", be_lun->lunname); 2363229997Sken 2364229997Sken if (retval != 0) 2365229997Sken goto bailout_error; 2366229997Sken 2367229997Sken be_lun->num_threads = num_threads; 2368229997Sken 2369229997Sken mtx_lock(&softc->lock); 2370229997Sken softc->num_luns++; 2371229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2372229997Sken 2373229997Sken mtx_unlock(&softc->lock); 2374229997Sken 2375287499Smav retval = ctl_add_lun(&be_lun->cbe_lun); 2376229997Sken if (retval != 0) { 2377229997Sken mtx_lock(&softc->lock); 2378229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2379229997Sken links); 2380229997Sken softc->num_luns--; 2381229997Sken mtx_unlock(&softc->lock); 2382229997Sken snprintf(req->error_str, sizeof(req->error_str), 2383272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2384272911Smav "details", retval); 2385229997Sken retval = 0; 2386229997Sken goto bailout_error; 2387229997Sken } 2388229997Sken 2389229997Sken mtx_lock(&softc->lock); 2390229997Sken 2391229997Sken /* 2392229997Sken * Tell the config_status routine that we're waiting so it won't 2393229997Sken * clean up the LUN in the event of an error. 2394229997Sken */ 2395229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2396229997Sken 2397229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2398229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2399229997Sken if (retval == EINTR) 2400229997Sken break; 2401229997Sken } 2402229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2403229997Sken 2404229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2405229997Sken snprintf(req->error_str, sizeof(req->error_str), 2406272911Smav "LUN configuration error, see dmesg for details"); 2407229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2408229997Sken links); 2409229997Sken softc->num_luns--; 2410229997Sken mtx_unlock(&softc->lock); 2411229997Sken goto bailout_error; 2412229997Sken } else { 2413287499Smav params->req_lun_id = cbe_lun->lun_id; 2414229997Sken } 2415229997Sken 2416229997Sken mtx_unlock(&softc->lock); 2417229997Sken 2418229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2419287499Smav cbe_lun->blocksize, 2420229997Sken DEVSTAT_ALL_SUPPORTED, 2421287499Smav cbe_lun->lun_type 2422229997Sken | DEVSTAT_TYPE_IF_OTHER, 2423229997Sken DEVSTAT_PRIORITY_OTHER); 2424229997Sken 2425229997Sken return (retval); 2426229997Sken 2427229997Skenbailout_error: 2428229997Sken req->status = CTL_LUN_ERROR; 2429229997Sken 2430267429Smav if (be_lun->io_taskqueue != NULL) 2431267429Smav taskqueue_free(be_lun->io_taskqueue); 2432229997Sken ctl_be_block_close(be_lun); 2433267429Smav if (be_lun->dev_path != NULL) 2434267429Smav free(be_lun->dev_path, M_CTLBLK); 2435267429Smav if (be_lun->lun_zone != NULL) 2436267429Smav uma_zdestroy(be_lun->lun_zone); 2437287499Smav ctl_free_opts(&cbe_lun->options); 2438267877Smav mtx_destroy(&be_lun->queue_lock); 2439267877Smav mtx_destroy(&be_lun->io_lock); 2440229997Sken free(be_lun, M_CTLBLK); 2441229997Sken 2442229997Sken return (retval); 2443229997Sken} 2444229997Sken 2445229997Skenstatic int 2446229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2447229997Sken{ 2448229997Sken struct ctl_lun_rm_params *params; 2449229997Sken struct ctl_be_block_lun *be_lun; 2450287670Smav struct ctl_be_lun *cbe_lun; 2451229997Sken int retval; 2452229997Sken 2453229997Sken params = &req->reqdata.rm; 2454229997Sken 2455229997Sken mtx_lock(&softc->lock); 2456229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2457287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2458229997Sken break; 2459229997Sken } 2460229997Sken mtx_unlock(&softc->lock); 2461229997Sken 2462229997Sken if (be_lun == NULL) { 2463229997Sken snprintf(req->error_str, sizeof(req->error_str), 2464272911Smav "LUN %u is not managed by the block backend", 2465272911Smav params->lun_id); 2466229997Sken goto bailout_error; 2467229997Sken } 2468287670Smav cbe_lun = &be_lun->cbe_lun; 2469229997Sken 2470287670Smav retval = ctl_disable_lun(cbe_lun); 2471229997Sken if (retval != 0) { 2472229997Sken snprintf(req->error_str, sizeof(req->error_str), 2473272911Smav "error %d returned from ctl_disable_lun() for " 2474272911Smav "LUN %d", retval, params->lun_id); 2475229997Sken goto bailout_error; 2476287670Smav } 2477229997Sken 2478287670Smav if (be_lun->vn != NULL) { 2479287670Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2480287670Smav ctl_lun_offline(cbe_lun); 2481287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2482287670Smav ctl_be_block_close(be_lun); 2483229997Sken } 2484229997Sken 2485287670Smav retval = ctl_invalidate_lun(cbe_lun); 2486229997Sken if (retval != 0) { 2487229997Sken snprintf(req->error_str, sizeof(req->error_str), 2488272911Smav "error %d returned from ctl_invalidate_lun() for " 2489272911Smav "LUN %d", retval, params->lun_id); 2490229997Sken goto bailout_error; 2491229997Sken } 2492229997Sken 2493229997Sken mtx_lock(&softc->lock); 2494229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2495229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2496229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2497229997Sken if (retval == EINTR) 2498229997Sken break; 2499229997Sken } 2500229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2501229997Sken 2502229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2503229997Sken snprintf(req->error_str, sizeof(req->error_str), 2504272911Smav "interrupted waiting for LUN to be freed"); 2505229997Sken mtx_unlock(&softc->lock); 2506229997Sken goto bailout_error; 2507229997Sken } 2508229997Sken 2509229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2510229997Sken 2511229997Sken softc->num_luns--; 2512229997Sken mtx_unlock(&softc->lock); 2513229997Sken 2514287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2515229997Sken taskqueue_free(be_lun->io_taskqueue); 2516229997Sken 2517229997Sken if (be_lun->disk_stats != NULL) 2518229997Sken devstat_remove_entry(be_lun->disk_stats); 2519229997Sken 2520229997Sken uma_zdestroy(be_lun->lun_zone); 2521229997Sken 2522287670Smav ctl_free_opts(&cbe_lun->options); 2523229997Sken free(be_lun->dev_path, M_CTLBLK); 2524267877Smav mtx_destroy(&be_lun->queue_lock); 2525267877Smav mtx_destroy(&be_lun->io_lock); 2526229997Sken free(be_lun, M_CTLBLK); 2527229997Sken 2528229997Sken req->status = CTL_LUN_OK; 2529229997Sken 2530229997Sken return (0); 2531229997Sken 2532229997Skenbailout_error: 2533229997Sken 2534229997Sken req->status = CTL_LUN_ERROR; 2535229997Sken 2536229997Sken return (0); 2537229997Sken} 2538229997Sken 2539232604Straszstatic int 2540232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2541232604Strasz struct ctl_lun_req *req) 2542232604Strasz{ 2543287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2544232604Strasz struct vattr vattr; 2545232604Strasz int error; 2546272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2547232604Strasz 2548232604Strasz if (params->lun_size_bytes != 0) { 2549232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2550232604Strasz } else { 2551271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2552232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2553271794Smav VOP_UNLOCK(be_lun->vn, 0); 2554232604Strasz if (error != 0) { 2555232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2556232604Strasz "error calling VOP_GETATTR() for file %s", 2557232604Strasz be_lun->dev_path); 2558232604Strasz return (error); 2559232604Strasz } 2560232604Strasz be_lun->size_bytes = vattr.va_size; 2561232604Strasz } 2562287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2563287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2564287499Smav 0 : (be_lun->size_blocks - 1); 2565232604Strasz return (0); 2566232604Strasz} 2567232604Strasz 2568232604Straszstatic int 2569232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2570232604Strasz struct ctl_lun_req *req) 2571232604Strasz{ 2572287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2573272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2574287664Smav struct cdevsw *csw; 2575287664Smav struct cdev *dev; 2576232604Strasz uint64_t size_bytes; 2577287664Smav int error, ref; 2578232604Strasz 2579287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 2580287664Smav if (csw == NULL) 2581287664Smav return (ENXIO); 2582287664Smav if (csw->d_ioctl == NULL) { 2583287664Smav dev_relthread(dev, ref); 2584232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2585272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2586232604Strasz return (ENODEV); 2587232604Strasz } 2588232604Strasz 2589287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&size_bytes, FREAD, 2590287664Smav curthread); 2591287664Smav dev_relthread(dev, ref); 2592232604Strasz if (error) { 2593232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2594272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2595272911Smav "on %s!", error, be_lun->dev_path); 2596232604Strasz return (error); 2597232604Strasz } 2598232604Strasz 2599232604Strasz if (params->lun_size_bytes != 0) { 2600232604Strasz if (params->lun_size_bytes > size_bytes) { 2601232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2602272911Smav "requested LUN size %ju > backing device " 2603272911Smav "size %ju", 2604232604Strasz (uintmax_t)params->lun_size_bytes, 2605232604Strasz (uintmax_t)size_bytes); 2606232604Strasz return (EINVAL); 2607232604Strasz } 2608232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2609232604Strasz } else { 2610232604Strasz be_lun->size_bytes = size_bytes; 2611232604Strasz } 2612287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2613287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2614287499Smav 0 : (be_lun->size_blocks - 1); 2615232604Strasz return (0); 2616232604Strasz} 2617232604Strasz 2618232604Straszstatic int 2619232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2620232604Strasz{ 2621232604Strasz struct ctl_lun_modify_params *params; 2622232604Strasz struct ctl_be_block_lun *be_lun; 2623287500Smav struct ctl_be_lun *cbe_lun; 2624287621Smav char *value; 2625271794Smav uint64_t oldsize; 2626287621Smav int error, wasprim; 2627232604Strasz 2628232604Strasz params = &req->reqdata.modify; 2629232604Strasz 2630232604Strasz mtx_lock(&softc->lock); 2631232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2632287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2633232604Strasz break; 2634232604Strasz } 2635232604Strasz mtx_unlock(&softc->lock); 2636232604Strasz 2637232604Strasz if (be_lun == NULL) { 2638232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2639272911Smav "LUN %u is not managed by the block backend", 2640272911Smav params->lun_id); 2641232604Strasz goto bailout_error; 2642232604Strasz } 2643287500Smav cbe_lun = &be_lun->cbe_lun; 2644232604Strasz 2645287500Smav if (params->lun_size_bytes != 0) 2646287500Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2647287500Smav ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2648232604Strasz 2649287621Smav wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2650287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2651287621Smav if (value != NULL) { 2652287621Smav if (strcmp(value, "primary") == 0) 2653287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2654287621Smav else 2655287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2656287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2657287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2658232604Strasz else 2659287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2660287621Smav if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 2661287621Smav if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 2662287621Smav ctl_lun_primary(cbe_lun); 2663287621Smav else 2664287621Smav ctl_lun_secondary(cbe_lun); 2665287621Smav } 2666232604Strasz 2667287621Smav oldsize = be_lun->size_blocks; 2668287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2669287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2670287621Smav if (be_lun->vn == NULL) 2671287621Smav error = ctl_be_block_open(softc, be_lun, req); 2672287621Smav else if (vn_isdisk(be_lun->vn, &error)) 2673287621Smav error = ctl_be_block_modify_dev(be_lun, req); 2674287621Smav else if (be_lun->vn->v_type == VREG) 2675287621Smav error = ctl_be_block_modify_file(be_lun, req); 2676287621Smav else 2677287621Smav error = EINVAL; 2678287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) && 2679287621Smav be_lun->vn != NULL) { 2680287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE; 2681287621Smav ctl_lun_online(cbe_lun); 2682287621Smav } 2683287621Smav } else { 2684287621Smav if (be_lun->vn != NULL) { 2685287621Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2686287621Smav ctl_lun_offline(cbe_lun); 2687287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2688287621Smav error = ctl_be_block_close(be_lun); 2689287621Smav } else 2690287621Smav error = 0; 2691287621Smav } 2692287499Smav if (be_lun->size_blocks != oldsize) 2693287500Smav ctl_lun_capacity_changed(cbe_lun); 2694232604Strasz 2695232604Strasz /* Tell the user the exact size we ended up using */ 2696232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2697232604Strasz 2698272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2699232604Strasz return (0); 2700232604Strasz 2701232604Straszbailout_error: 2702232604Strasz req->status = CTL_LUN_ERROR; 2703232604Strasz return (0); 2704232604Strasz} 2705232604Strasz 2706229997Skenstatic void 2707229997Skenctl_be_block_lun_shutdown(void *be_lun) 2708229997Sken{ 2709229997Sken struct ctl_be_block_lun *lun; 2710229997Sken struct ctl_be_block_softc *softc; 2711229997Sken 2712229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2713229997Sken 2714229997Sken softc = lun->softc; 2715229997Sken 2716229997Sken mtx_lock(&softc->lock); 2717229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2718229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2719229997Sken wakeup(lun); 2720229997Sken mtx_unlock(&softc->lock); 2721229997Sken 2722229997Sken} 2723229997Sken 2724229997Skenstatic void 2725229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2726229997Sken{ 2727229997Sken struct ctl_be_block_lun *lun; 2728229997Sken struct ctl_be_block_softc *softc; 2729229997Sken 2730229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2731229997Sken softc = lun->softc; 2732229997Sken 2733229997Sken if (status == CTL_LUN_CONFIG_OK) { 2734229997Sken mtx_lock(&softc->lock); 2735229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2736229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2737229997Sken wakeup(lun); 2738229997Sken mtx_unlock(&softc->lock); 2739229997Sken 2740229997Sken /* 2741229997Sken * We successfully added the LUN, attempt to enable it. 2742229997Sken */ 2743287499Smav if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2744229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2745287499Smav if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2746229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2747229997Sken __func__); 2748229997Sken } 2749229997Sken } 2750229997Sken 2751229997Sken return; 2752229997Sken } 2753229997Sken 2754229997Sken 2755229997Sken mtx_lock(&softc->lock); 2756229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2757229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2758229997Sken wakeup(lun); 2759229997Sken mtx_unlock(&softc->lock); 2760229997Sken} 2761229997Sken 2762229997Sken 2763229997Skenstatic int 2764229997Skenctl_be_block_config_write(union ctl_io *io) 2765229997Sken{ 2766229997Sken struct ctl_be_block_lun *be_lun; 2767287499Smav struct ctl_be_lun *cbe_lun; 2768229997Sken int retval; 2769229997Sken 2770229997Sken retval = 0; 2771229997Sken 2772229997Sken DPRINTF("entered\n"); 2773229997Sken 2774287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2775229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2776287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2777229997Sken 2778229997Sken switch (io->scsiio.cdb[0]) { 2779229997Sken case SYNCHRONIZE_CACHE: 2780229997Sken case SYNCHRONIZE_CACHE_16: 2781264274Smav case WRITE_SAME_10: 2782264274Smav case WRITE_SAME_16: 2783264274Smav case UNMAP: 2784229997Sken /* 2785229997Sken * The upper level CTL code will filter out any CDBs with 2786229997Sken * the immediate bit set and return the proper error. 2787229997Sken * 2788229997Sken * We don't really need to worry about what LBA range the 2789229997Sken * user asked to be synced out. When they issue a sync 2790229997Sken * cache command, we'll sync out the whole thing. 2791229997Sken */ 2792267877Smav mtx_lock(&be_lun->queue_lock); 2793229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2794229997Sken links); 2795267877Smav mtx_unlock(&be_lun->queue_lock); 2796229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2797229997Sken break; 2798229997Sken case START_STOP_UNIT: { 2799229997Sken struct scsi_start_stop_unit *cdb; 2800229997Sken 2801229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2802229997Sken 2803229997Sken if (cdb->how & SSS_START) 2804287499Smav retval = ctl_start_lun(cbe_lun); 2805229997Sken else { 2806287499Smav retval = ctl_stop_lun(cbe_lun); 2807229997Sken /* 2808229997Sken * XXX KDM Copan-specific offline behavior. 2809229997Sken * Figure out a reasonable way to port this? 2810229997Sken */ 2811229997Sken#ifdef NEEDTOPORT 2812229997Sken if ((retval == 0) 2813229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2814287499Smav retval = ctl_lun_offline(cbe_lun); 2815229997Sken#endif 2816229997Sken } 2817229997Sken 2818229997Sken /* 2819229997Sken * In general, the above routines should not fail. They 2820229997Sken * just set state for the LUN. So we've got something 2821229997Sken * pretty wrong here if we can't start or stop the LUN. 2822229997Sken */ 2823229997Sken if (retval != 0) { 2824229997Sken ctl_set_internal_failure(&io->scsiio, 2825229997Sken /*sks_valid*/ 1, 2826229997Sken /*retry_count*/ 0xf051); 2827229997Sken retval = CTL_RETVAL_COMPLETE; 2828229997Sken } else { 2829229997Sken ctl_set_success(&io->scsiio); 2830229997Sken } 2831229997Sken ctl_config_write_done(io); 2832229997Sken break; 2833229997Sken } 2834229997Sken default: 2835229997Sken ctl_set_invalid_opcode(&io->scsiio); 2836229997Sken ctl_config_write_done(io); 2837229997Sken retval = CTL_RETVAL_COMPLETE; 2838229997Sken break; 2839229997Sken } 2840229997Sken 2841229997Sken return (retval); 2842229997Sken} 2843229997Sken 2844229997Skenstatic int 2845229997Skenctl_be_block_config_read(union ctl_io *io) 2846229997Sken{ 2847275474Smav struct ctl_be_block_lun *be_lun; 2848287499Smav struct ctl_be_lun *cbe_lun; 2849275474Smav int retval = 0; 2850275474Smav 2851275474Smav DPRINTF("entered\n"); 2852275474Smav 2853287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2854275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2855287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2856275474Smav 2857275474Smav switch (io->scsiio.cdb[0]) { 2858275474Smav case SERVICE_ACTION_IN: 2859275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2860275474Smav mtx_lock(&be_lun->queue_lock); 2861275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2862275474Smav &io->io_hdr, links); 2863275474Smav mtx_unlock(&be_lun->queue_lock); 2864275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2865275474Smav &be_lun->io_task); 2866275474Smav retval = CTL_RETVAL_QUEUED; 2867275474Smav break; 2868275474Smav } 2869275474Smav ctl_set_invalid_field(&io->scsiio, 2870275474Smav /*sks_valid*/ 1, 2871275474Smav /*command*/ 1, 2872275474Smav /*field*/ 1, 2873275474Smav /*bit_valid*/ 1, 2874275474Smav /*bit*/ 4); 2875275474Smav ctl_config_read_done(io); 2876275474Smav retval = CTL_RETVAL_COMPLETE; 2877275474Smav break; 2878275474Smav default: 2879275474Smav ctl_set_invalid_opcode(&io->scsiio); 2880275474Smav ctl_config_read_done(io); 2881275474Smav retval = CTL_RETVAL_COMPLETE; 2882275474Smav break; 2883275474Smav } 2884275474Smav 2885275474Smav return (retval); 2886229997Sken} 2887229997Sken 2888229997Skenstatic int 2889229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2890229997Sken{ 2891229997Sken struct ctl_be_block_lun *lun; 2892229997Sken int retval; 2893229997Sken 2894229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2895229997Sken retval = 0; 2896229997Sken 2897268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2898229997Sken 2899229997Sken if (retval != 0) 2900229997Sken goto bailout; 2901229997Sken 2902229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2903229997Sken 2904229997Sken if (retval != 0) 2905229997Sken goto bailout; 2906229997Sken 2907268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2908229997Sken 2909229997Skenbailout: 2910229997Sken 2911229997Sken return (retval); 2912229997Sken} 2913229997Sken 2914274154Smavstatic uint64_t 2915274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2916274154Smav{ 2917274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2918274154Smav 2919274154Smav if (lun->getattr == NULL) 2920274154Smav return (UINT64_MAX); 2921274154Smav return (lun->getattr(lun, attrname)); 2922274154Smav} 2923274154Smav 2924229997Skenint 2925229997Skenctl_be_block_init(void) 2926229997Sken{ 2927229997Sken struct ctl_be_block_softc *softc; 2928229997Sken int retval; 2929229997Sken 2930229997Sken softc = &backend_block_softc; 2931229997Sken retval = 0; 2932229997Sken 2933267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2934264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2935264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2936229997Sken STAILQ_INIT(&softc->lun_list); 2937229997Sken 2938229997Sken return (retval); 2939229997Sken} 2940