ctl_backend_block.c revision 287875
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 287875 2015-09-16 21:43:51Z 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 354287868Smavstatic size_t 355287868Smavcmp(uint8_t *a, uint8_t *b, size_t size) 356287868Smav{ 357287868Smav size_t i; 358287868Smav 359287868Smav for (i = 0; i < size; i++) { 360287868Smav if (a[i] != b[i]) 361287868Smav break; 362287868Smav } 363287868Smav return (i); 364287868Smav} 365287868Smav 366287868Smavstatic void 367287868Smavctl_be_block_compare(union ctl_io *io) 368287868Smav{ 369287868Smav struct ctl_be_block_io *beio; 370287868Smav uint64_t off, res; 371287868Smav int i; 372287868Smav uint8_t info[8]; 373287868Smav 374287868Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 375287868Smav off = 0; 376287868Smav for (i = 0; i < beio->num_segs; i++) { 377287868Smav res = cmp(beio->sg_segs[i].addr, 378287868Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 379287868Smav beio->sg_segs[i].len); 380287868Smav off += res; 381287868Smav if (res < beio->sg_segs[i].len) 382287868Smav break; 383287868Smav } 384287868Smav if (i < beio->num_segs) { 385287868Smav scsi_u64to8b(off, info); 386287868Smav ctl_set_sense(&io->scsiio, /*current_error*/ 1, 387287868Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 388287868Smav /*asc*/ 0x1D, /*ascq*/ 0x00, 389287868Smav /*type*/ SSD_ELEM_INFO, 390287868Smav /*size*/ sizeof(info), /*data*/ &info, 391287868Smav /*type*/ SSD_ELEM_NONE); 392287868Smav } else 393287868Smav ctl_set_success(&io->scsiio); 394287868Smav} 395287868Smav 396229997Skenstatic int 397229997Skenctl_be_block_move_done(union ctl_io *io) 398229997Sken{ 399229997Sken struct ctl_be_block_io *beio; 400229997Sken struct ctl_be_block_lun *be_lun; 401267537Smav struct ctl_lba_len_flags *lbalen; 402229997Sken#ifdef CTL_TIME_IO 403229997Sken struct bintime cur_bt; 404267537Smav#endif 405229997Sken 406267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 407229997Sken be_lun = beio->lun; 408229997Sken 409229997Sken DPRINTF("entered\n"); 410229997Sken 411229997Sken#ifdef CTL_TIME_IO 412229997Sken getbintime(&cur_bt); 413229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 414229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 415229997Sken io->io_hdr.num_dmas++; 416229997Sken#endif 417267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 418229997Sken 419229997Sken /* 420229997Sken * We set status at this point for read commands, and write 421229997Sken * commands with errors. 422229997Sken */ 423275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 424275058Smav ; 425275058Smav } else if ((io->io_hdr.port_status == 0) && 426267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 427267537Smav lbalen = ARGS(beio->io); 428267537Smav if (lbalen->flags & CTL_LLF_READ) { 429267537Smav ctl_set_success(&io->scsiio); 430267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 431267537Smav /* We have two data blocks ready for comparison. */ 432287868Smav ctl_be_block_compare(io); 433267537Smav } 434275058Smav } else if ((io->io_hdr.port_status != 0) && 435275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 436275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 437229997Sken /* 438229997Sken * For hardware error sense keys, the sense key 439229997Sken * specific value is defined to be a retry count, 440229997Sken * but we use it to pass back an internal FETD 441229997Sken * error code. XXX KDM Hopefully the FETD is only 442229997Sken * using 16 bits for an error code, since that's 443229997Sken * all the space we have in the sks field. 444229997Sken */ 445229997Sken ctl_set_internal_failure(&io->scsiio, 446229997Sken /*sks_valid*/ 1, 447229997Sken /*retry_count*/ 448229997Sken io->io_hdr.port_status); 449229997Sken } 450229997Sken 451229997Sken /* 452229997Sken * If this is a read, or a write with errors, it is done. 453229997Sken */ 454229997Sken if ((beio->bio_cmd == BIO_READ) 455229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 456229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 457229997Sken ctl_complete_beio(beio); 458229997Sken return (0); 459229997Sken } 460229997Sken 461229997Sken /* 462229997Sken * At this point, we have a write and the DMA completed 463229997Sken * successfully. We now have to queue it to the task queue to 464229997Sken * execute the backend I/O. That is because we do blocking 465229997Sken * memory allocations, and in the file backing case, blocking I/O. 466229997Sken * This move done routine is generally called in the SIM's 467229997Sken * interrupt context, and therefore we cannot block. 468229997Sken */ 469267877Smav mtx_lock(&be_lun->queue_lock); 470229997Sken /* 471229997Sken * XXX KDM make sure that links is okay to use at this point. 472229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 473229997Sken * or deal with resource allocation here. 474229997Sken */ 475229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 476267877Smav mtx_unlock(&be_lun->queue_lock); 477229997Sken 478229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 479229997Sken 480229997Sken return (0); 481229997Sken} 482229997Sken 483229997Skenstatic void 484229997Skenctl_be_block_biodone(struct bio *bio) 485229997Sken{ 486229997Sken struct ctl_be_block_io *beio; 487229997Sken struct ctl_be_block_lun *be_lun; 488229997Sken union ctl_io *io; 489261538Smav int error; 490229997Sken 491229997Sken beio = bio->bio_caller1; 492229997Sken be_lun = beio->lun; 493229997Sken io = beio->io; 494229997Sken 495229997Sken DPRINTF("entered\n"); 496229997Sken 497261538Smav error = bio->bio_error; 498267877Smav mtx_lock(&be_lun->io_lock); 499261538Smav if (error != 0) 500229997Sken beio->num_errors++; 501229997Sken 502229997Sken beio->num_bios_done++; 503229997Sken 504229997Sken /* 505229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 506229997Sken * during the free might cause it to complain. 507229997Sken */ 508229997Sken g_destroy_bio(bio); 509229997Sken 510229997Sken /* 511229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 512229997Sken * complete, then we're done. 513229997Sken */ 514229997Sken if ((beio->send_complete == 0) 515229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 516267877Smav mtx_unlock(&be_lun->io_lock); 517229997Sken return; 518229997Sken } 519229997Sken 520229997Sken /* 521229997Sken * At this point, we've verified that we are the last I/O to 522229997Sken * complete, so it's safe to drop the lock. 523229997Sken */ 524267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 525267877Smav beio->ds_tag_type, beio->ds_trans_type, 526267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 527267877Smav mtx_unlock(&be_lun->io_lock); 528229997Sken 529229997Sken /* 530229997Sken * If there are any errors from the backing device, we fail the 531229997Sken * entire I/O with a medium error. 532229997Sken */ 533229997Sken if (beio->num_errors > 0) { 534261538Smav if (error == EOPNOTSUPP) { 535261538Smav ctl_set_invalid_opcode(&io->scsiio); 536282565Smav } else if (error == ENOSPC || error == EDQUOT) { 537273809Smav ctl_set_space_alloc_fail(&io->scsiio); 538287760Smav } else if (error == EROFS || error == EACCES) { 539287760Smav ctl_set_hw_write_protected(&io->scsiio); 540261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 541229997Sken /* XXX KDM is there is a better error here? */ 542229997Sken ctl_set_internal_failure(&io->scsiio, 543229997Sken /*sks_valid*/ 1, 544229997Sken /*retry_count*/ 0xbad2); 545229997Sken } else 546229997Sken ctl_set_medium_error(&io->scsiio); 547229997Sken ctl_complete_beio(beio); 548229997Sken return; 549229997Sken } 550229997Sken 551229997Sken /* 552267537Smav * If this is a write, a flush, a delete or verify, we're all done. 553229997Sken * If this is a read, we can now send the data to the user. 554229997Sken */ 555229997Sken if ((beio->bio_cmd == BIO_WRITE) 556264274Smav || (beio->bio_cmd == BIO_FLUSH) 557267537Smav || (beio->bio_cmd == BIO_DELETE) 558267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 559229997Sken ctl_set_success(&io->scsiio); 560229997Sken ctl_complete_beio(beio); 561229997Sken } else { 562275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 563275058Smav beio->beio_cont == NULL) 564275058Smav ctl_set_success(&io->scsiio); 565229997Sken#ifdef CTL_TIME_IO 566229997Sken getbintime(&io->io_hdr.dma_start_bt); 567229997Sken#endif 568229997Sken ctl_datamove(io); 569229997Sken } 570229997Sken} 571229997Sken 572229997Skenstatic void 573229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 574229997Sken struct ctl_be_block_io *beio) 575229997Sken{ 576267877Smav union ctl_io *io = beio->io; 577229997Sken struct mount *mountpoint; 578241896Skib int error, lock_flags; 579229997Sken 580229997Sken DPRINTF("entered\n"); 581229997Sken 582267877Smav binuptime(&beio->ds_t0); 583267877Smav mtx_lock(&be_lun->io_lock); 584267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 585267877Smav mtx_unlock(&be_lun->io_lock); 586229997Sken 587267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 588229997Sken 589229997Sken if (MNT_SHARED_WRITES(mountpoint) 590229997Sken || ((mountpoint == NULL) 591229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 592229997Sken lock_flags = LK_SHARED; 593229997Sken else 594229997Sken lock_flags = LK_EXCLUSIVE; 595229997Sken 596229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 597229997Sken 598286353Smav error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 599286353Smav curthread); 600229997Sken VOP_UNLOCK(be_lun->vn, 0); 601229997Sken 602229997Sken vn_finished_write(mountpoint); 603229997Sken 604267877Smav mtx_lock(&be_lun->io_lock); 605267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 606267877Smav beio->ds_tag_type, beio->ds_trans_type, 607267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 608267877Smav mtx_unlock(&be_lun->io_lock); 609267877Smav 610229997Sken if (error == 0) 611229997Sken ctl_set_success(&io->scsiio); 612229997Sken else { 613229997Sken /* XXX KDM is there is a better error here? */ 614229997Sken ctl_set_internal_failure(&io->scsiio, 615229997Sken /*sks_valid*/ 1, 616229997Sken /*retry_count*/ 0xbad1); 617229997Sken } 618229997Sken 619229997Sken ctl_complete_beio(beio); 620229997Sken} 621229997Sken 622258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 623258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 624258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 625258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 626229997Sken 627229997Skenstatic void 628229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 629229997Sken struct ctl_be_block_io *beio) 630229997Sken{ 631229997Sken struct ctl_be_block_filedata *file_data; 632229997Sken union ctl_io *io; 633229997Sken struct uio xuio; 634229997Sken struct iovec *xiovec; 635287875Smav size_t s; 636287875Smav int error, flags, i; 637229997Sken 638229997Sken DPRINTF("entered\n"); 639229997Sken 640229997Sken file_data = &be_lun->backend.file; 641229997Sken io = beio->io; 642271309Smav flags = 0; 643271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 644271309Smav flags |= IO_DIRECT; 645271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 646271309Smav flags |= IO_SYNC; 647229997Sken 648267537Smav bzero(&xuio, sizeof(xuio)); 649229997Sken if (beio->bio_cmd == BIO_READ) { 650229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 651267537Smav xuio.uio_rw = UIO_READ; 652229997Sken } else { 653229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 654267537Smav xuio.uio_rw = UIO_WRITE; 655229997Sken } 656229997Sken xuio.uio_offset = beio->io_offset; 657229997Sken xuio.uio_resid = beio->io_len; 658229997Sken xuio.uio_segflg = UIO_SYSSPACE; 659229997Sken xuio.uio_iov = beio->xiovecs; 660229997Sken xuio.uio_iovcnt = beio->num_segs; 661229997Sken xuio.uio_td = curthread; 662229997Sken 663229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 664229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 665229997Sken xiovec->iov_len = beio->sg_segs[i].len; 666229997Sken } 667229997Sken 668267877Smav binuptime(&beio->ds_t0); 669267877Smav mtx_lock(&be_lun->io_lock); 670267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 671267877Smav mtx_unlock(&be_lun->io_lock); 672267877Smav 673229997Sken if (beio->bio_cmd == BIO_READ) { 674229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 675229997Sken 676229997Sken /* 677229997Sken * UFS pays attention to IO_DIRECT for reads. If the 678229997Sken * DIRECTIO option is configured into the kernel, it calls 679229997Sken * ffs_rawread(). But that only works for single-segment 680229997Sken * uios with user space addresses. In our case, with a 681229997Sken * kernel uio, it still reads into the buffer cache, but it 682229997Sken * will just try to release the buffer from the cache later 683229997Sken * on in ffs_read(). 684229997Sken * 685229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 686229997Sken * 687229997Sken * UFS does not pay attention to IO_SYNC for reads. 688229997Sken * 689229997Sken * ZFS pays attention to IO_SYNC (which translates into the 690229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 691229997Sken * attempts to sync the file before reading. 692229997Sken */ 693271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 694229997Sken 695229997Sken VOP_UNLOCK(be_lun->vn, 0); 696267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 697287875Smav if (error == 0 && xuio.uio_resid > 0) { 698287875Smav /* 699287875Smav * If we red less then requested (EOF), then 700287875Smav * we should clean the rest of the buffer. 701287875Smav */ 702287875Smav s = beio->io_len - xuio.uio_resid; 703287875Smav for (i = 0; i < beio->num_segs; i++) { 704287875Smav if (s >= beio->sg_segs[i].len) { 705287875Smav s -= beio->sg_segs[i].len; 706287875Smav continue; 707287875Smav } 708287875Smav bzero((uint8_t *)beio->sg_segs[i].addr + s, 709287875Smav beio->sg_segs[i].len - s); 710287875Smav s = 0; 711287875Smav } 712287875Smav } 713229997Sken } else { 714229997Sken struct mount *mountpoint; 715229997Sken int lock_flags; 716229997Sken 717229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 718229997Sken 719229997Sken if (MNT_SHARED_WRITES(mountpoint) 720229997Sken || ((mountpoint == NULL) 721229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 722229997Sken lock_flags = LK_SHARED; 723229997Sken else 724229997Sken lock_flags = LK_EXCLUSIVE; 725229997Sken 726229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 727229997Sken 728229997Sken /* 729229997Sken * UFS pays attention to IO_DIRECT for writes. The write 730229997Sken * is done asynchronously. (Normally the write would just 731229997Sken * get put into cache. 732229997Sken * 733229997Sken * UFS pays attention to IO_SYNC for writes. It will 734229997Sken * attempt to write the buffer out synchronously if that 735229997Sken * flag is set. 736229997Sken * 737229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 738229997Sken * 739229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 740229997Sken * for writes. It will flush the transaction from the 741229997Sken * cache before returning. 742229997Sken */ 743271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 744229997Sken VOP_UNLOCK(be_lun->vn, 0); 745229997Sken 746229997Sken vn_finished_write(mountpoint); 747267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 748229997Sken } 749229997Sken 750267877Smav mtx_lock(&be_lun->io_lock); 751267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 752267877Smav beio->ds_tag_type, beio->ds_trans_type, 753267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 754267877Smav mtx_unlock(&be_lun->io_lock); 755267877Smav 756229997Sken /* 757229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 758229997Sken * return the I/O to the user. 759229997Sken */ 760229997Sken if (error != 0) { 761229997Sken char path_str[32]; 762229997Sken 763229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 764229997Sken printf("%s%s command returned errno %d\n", path_str, 765229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 766282565Smav if (error == ENOSPC || error == EDQUOT) { 767273809Smav ctl_set_space_alloc_fail(&io->scsiio); 768287760Smav } else if (error == EROFS || error == EACCES) { 769287760Smav ctl_set_hw_write_protected(&io->scsiio); 770273809Smav } else 771273809Smav ctl_set_medium_error(&io->scsiio); 772229997Sken ctl_complete_beio(beio); 773229997Sken return; 774229997Sken } 775229997Sken 776229997Sken /* 777269122Smav * If this is a write or a verify, we're all done. 778229997Sken * If this is a read, we can now send the data to the user. 779229997Sken */ 780269122Smav if ((beio->bio_cmd == BIO_WRITE) || 781269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 782229997Sken ctl_set_success(&io->scsiio); 783229997Sken ctl_complete_beio(beio); 784229997Sken } else { 785275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 786275058Smav beio->beio_cont == NULL) 787275058Smav ctl_set_success(&io->scsiio); 788229997Sken#ifdef CTL_TIME_IO 789229997Sken getbintime(&io->io_hdr.dma_start_bt); 790229997Sken#endif 791229997Sken ctl_datamove(io); 792229997Sken } 793229997Sken} 794229997Sken 795229997Skenstatic void 796275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 797275474Smav struct ctl_be_block_io *beio) 798275474Smav{ 799275474Smav union ctl_io *io = beio->io; 800275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 801275474Smav struct scsi_get_lba_status_data *data; 802275474Smav off_t roff, off; 803275474Smav int error, status; 804275474Smav 805275474Smav DPRINTF("entered\n"); 806275474Smav 807287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 808275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 809275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 810275474Smav 0, curthread->td_ucred, curthread); 811275474Smav if (error == 0 && off > roff) 812275474Smav status = 0; /* mapped up to off */ 813275474Smav else { 814275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 815275474Smav 0, curthread->td_ucred, curthread); 816275474Smav if (error == 0 && off > roff) 817275474Smav status = 1; /* deallocated up to off */ 818275474Smav else { 819275474Smav status = 0; /* unknown up to the end */ 820275474Smav off = be_lun->size_bytes; 821275474Smav } 822275474Smav } 823275474Smav VOP_UNLOCK(be_lun->vn, 0); 824275474Smav 825275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 826275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 827287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 828287499Smav lbalen->lba), data->descr[0].length); 829275474Smav data->descr[0].status = status; 830275474Smav 831275474Smav ctl_complete_beio(beio); 832275474Smav} 833275474Smav 834275481Smavstatic uint64_t 835275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 836275481Smav{ 837275481Smav struct vattr vattr; 838275481Smav struct statfs statfs; 839285030Smav uint64_t val; 840275481Smav int error; 841275481Smav 842285030Smav val = UINT64_MAX; 843275481Smav if (be_lun->vn == NULL) 844285030Smav return (val); 845285030Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 846275481Smav if (strcmp(attrname, "blocksused") == 0) { 847275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 848285030Smav if (error == 0) 849287499Smav val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 850275481Smav } 851285030Smav if (strcmp(attrname, "blocksavail") == 0 && 852285030Smav (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 853275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 854285030Smav if (error == 0) 855286811Smav val = statfs.f_bavail * statfs.f_bsize / 856287499Smav be_lun->cbe_lun.blocksize; 857275481Smav } 858285030Smav VOP_UNLOCK(be_lun->vn, 0); 859285030Smav return (val); 860275481Smav} 861275481Smav 862275474Smavstatic void 863269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 864269123Smav struct ctl_be_block_io *beio) 865269123Smav{ 866269123Smav union ctl_io *io; 867287664Smav struct cdevsw *csw; 868287664Smav struct cdev *dev; 869269123Smav struct uio xuio; 870269123Smav struct iovec *xiovec; 871287664Smav int error, flags, i, ref; 872269123Smav 873269123Smav DPRINTF("entered\n"); 874269123Smav 875269123Smav io = beio->io; 876271309Smav flags = 0; 877271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 878271309Smav flags |= IO_DIRECT; 879271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 880271309Smav flags |= IO_SYNC; 881269123Smav 882269123Smav bzero(&xuio, sizeof(xuio)); 883269123Smav if (beio->bio_cmd == BIO_READ) { 884269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 885269123Smav xuio.uio_rw = UIO_READ; 886269123Smav } else { 887269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 888269123Smav xuio.uio_rw = UIO_WRITE; 889269123Smav } 890269123Smav xuio.uio_offset = beio->io_offset; 891269123Smav xuio.uio_resid = beio->io_len; 892269123Smav xuio.uio_segflg = UIO_SYSSPACE; 893269123Smav xuio.uio_iov = beio->xiovecs; 894269123Smav xuio.uio_iovcnt = beio->num_segs; 895269123Smav xuio.uio_td = curthread; 896269123Smav 897269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 898269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 899269123Smav xiovec->iov_len = beio->sg_segs[i].len; 900269123Smav } 901269123Smav 902269123Smav binuptime(&beio->ds_t0); 903269123Smav mtx_lock(&be_lun->io_lock); 904269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 905269123Smav mtx_unlock(&be_lun->io_lock); 906269123Smav 907287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 908287664Smav if (csw) { 909287664Smav if (beio->bio_cmd == BIO_READ) 910287664Smav error = csw->d_read(dev, &xuio, flags); 911287664Smav else 912287664Smav error = csw->d_write(dev, &xuio, flags); 913287664Smav dev_relthread(dev, ref); 914287664Smav } else 915287664Smav error = ENXIO; 916287664Smav 917287664Smav if (beio->bio_cmd == BIO_READ) 918269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 919287664Smav else 920269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 921269123Smav 922269123Smav mtx_lock(&be_lun->io_lock); 923269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 924269123Smav beio->ds_tag_type, beio->ds_trans_type, 925269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 926269123Smav mtx_unlock(&be_lun->io_lock); 927269123Smav 928269123Smav /* 929269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 930269123Smav * return the I/O to the user. 931269123Smav */ 932269123Smav if (error != 0) { 933282565Smav if (error == ENOSPC || error == EDQUOT) { 934273809Smav ctl_set_space_alloc_fail(&io->scsiio); 935287760Smav } else if (error == EROFS || error == EACCES) { 936287760Smav ctl_set_hw_write_protected(&io->scsiio); 937273809Smav } else 938273809Smav ctl_set_medium_error(&io->scsiio); 939269123Smav ctl_complete_beio(beio); 940269123Smav return; 941269123Smav } 942269123Smav 943269123Smav /* 944269123Smav * If this is a write or a verify, we're all done. 945269123Smav * If this is a read, we can now send the data to the user. 946269123Smav */ 947269123Smav if ((beio->bio_cmd == BIO_WRITE) || 948269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 949269123Smav ctl_set_success(&io->scsiio); 950269123Smav ctl_complete_beio(beio); 951269123Smav } else { 952275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 953275058Smav beio->beio_cont == NULL) 954275058Smav ctl_set_success(&io->scsiio); 955269123Smav#ifdef CTL_TIME_IO 956269123Smav getbintime(&io->io_hdr.dma_start_bt); 957269123Smav#endif 958269123Smav ctl_datamove(io); 959269123Smav } 960269123Smav} 961269123Smav 962269123Smavstatic void 963275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 964275474Smav struct ctl_be_block_io *beio) 965275474Smav{ 966275474Smav union ctl_io *io = beio->io; 967287664Smav struct cdevsw *csw; 968287664Smav struct cdev *dev; 969275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 970275474Smav struct scsi_get_lba_status_data *data; 971275474Smav off_t roff, off; 972287664Smav int error, ref, status; 973275474Smav 974275474Smav DPRINTF("entered\n"); 975275474Smav 976287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 977287664Smav if (csw == NULL) { 978287664Smav status = 0; /* unknown up to the end */ 979287664Smav off = be_lun->size_bytes; 980287664Smav goto done; 981287664Smav } 982287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 983287664Smav error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 984287664Smav curthread); 985275474Smav if (error == 0 && off > roff) 986275474Smav status = 0; /* mapped up to off */ 987275474Smav else { 988287664Smav error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 989287664Smav curthread); 990275474Smav if (error == 0 && off > roff) 991275474Smav status = 1; /* deallocated up to off */ 992275474Smav else { 993275474Smav status = 0; /* unknown up to the end */ 994275474Smav off = be_lun->size_bytes; 995275474Smav } 996275474Smav } 997287664Smav dev_relthread(dev, ref); 998275474Smav 999287664Smavdone: 1000275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 1001275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 1002287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 1003287499Smav lbalen->lba), data->descr[0].length); 1004275474Smav data->descr[0].status = status; 1005275474Smav 1006275474Smav ctl_complete_beio(beio); 1007275474Smav} 1008275474Smav 1009275474Smavstatic void 1010229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 1011229997Sken struct ctl_be_block_io *beio) 1012229997Sken{ 1013229997Sken struct bio *bio; 1014229997Sken union ctl_io *io; 1015287664Smav struct cdevsw *csw; 1016287664Smav struct cdev *dev; 1017287664Smav int ref; 1018229997Sken 1019229997Sken io = beio->io; 1020229997Sken 1021229997Sken DPRINTF("entered\n"); 1022229997Sken 1023229997Sken /* This can't fail, it's a blocking allocation. */ 1024229997Sken bio = g_alloc_bio(); 1025229997Sken 1026229997Sken bio->bio_cmd = BIO_FLUSH; 1027229997Sken bio->bio_offset = 0; 1028229997Sken bio->bio_data = 0; 1029229997Sken bio->bio_done = ctl_be_block_biodone; 1030229997Sken bio->bio_caller1 = beio; 1031229997Sken bio->bio_pblkno = 0; 1032229997Sken 1033229997Sken /* 1034229997Sken * We don't need to acquire the LUN lock here, because we are only 1035229997Sken * sending one bio, and so there is no other context to synchronize 1036229997Sken * with. 1037229997Sken */ 1038229997Sken beio->num_bios_sent = 1; 1039229997Sken beio->send_complete = 1; 1040229997Sken 1041229997Sken binuptime(&beio->ds_t0); 1042267877Smav mtx_lock(&be_lun->io_lock); 1043229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1044267877Smav mtx_unlock(&be_lun->io_lock); 1045229997Sken 1046287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1047287664Smav if (csw) { 1048287664Smav bio->bio_dev = dev; 1049287664Smav csw->d_strategy(bio); 1050287664Smav dev_relthread(dev, ref); 1051287664Smav } else { 1052287664Smav bio->bio_error = ENXIO; 1053287664Smav ctl_be_block_biodone(bio); 1054287664Smav } 1055229997Sken} 1056229997Sken 1057229997Skenstatic void 1058264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1059264274Smav struct ctl_be_block_io *beio, 1060264274Smav uint64_t off, uint64_t len, int last) 1061264274Smav{ 1062264274Smav struct bio *bio; 1063264296Smav uint64_t maxlen; 1064287664Smav struct cdevsw *csw; 1065287664Smav struct cdev *dev; 1066287664Smav int ref; 1067264274Smav 1068287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1069287499Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1070264274Smav while (len > 0) { 1071264274Smav bio = g_alloc_bio(); 1072264274Smav bio->bio_cmd = BIO_DELETE; 1073287664Smav bio->bio_dev = dev; 1074264274Smav bio->bio_offset = off; 1075264296Smav bio->bio_length = MIN(len, maxlen); 1076264274Smav bio->bio_data = 0; 1077264274Smav bio->bio_done = ctl_be_block_biodone; 1078264274Smav bio->bio_caller1 = beio; 1079287499Smav bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1080264274Smav 1081264274Smav off += bio->bio_length; 1082264274Smav len -= bio->bio_length; 1083264274Smav 1084267877Smav mtx_lock(&be_lun->io_lock); 1085264274Smav beio->num_bios_sent++; 1086264274Smav if (last && len == 0) 1087264274Smav beio->send_complete = 1; 1088267877Smav mtx_unlock(&be_lun->io_lock); 1089264274Smav 1090287664Smav if (csw) { 1091287664Smav csw->d_strategy(bio); 1092287664Smav } else { 1093287664Smav bio->bio_error = ENXIO; 1094287664Smav ctl_be_block_biodone(bio); 1095287664Smav } 1096264274Smav } 1097287664Smav if (csw) 1098287664Smav dev_relthread(dev, ref); 1099264274Smav} 1100264274Smav 1101264274Smavstatic void 1102264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1103264274Smav struct ctl_be_block_io *beio) 1104264274Smav{ 1105264274Smav union ctl_io *io; 1106267515Smav struct ctl_ptr_len_flags *ptrlen; 1107264274Smav struct scsi_unmap_desc *buf, *end; 1108264274Smav uint64_t len; 1109264274Smav 1110264274Smav io = beio->io; 1111264274Smav 1112264274Smav DPRINTF("entered\n"); 1113264274Smav 1114264274Smav binuptime(&beio->ds_t0); 1115267877Smav mtx_lock(&be_lun->io_lock); 1116264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1117267877Smav mtx_unlock(&be_lun->io_lock); 1118264274Smav 1119264274Smav if (beio->io_offset == -1) { 1120264274Smav beio->io_len = 0; 1121267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1122267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1123267515Smav end = buf + ptrlen->len / sizeof(*buf); 1124264274Smav for (; buf < end; buf++) { 1125264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1126287499Smav be_lun->cbe_lun.blocksize; 1127264274Smav beio->io_len += len; 1128264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1129287499Smav scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 1130287499Smav len, (end - buf < 2) ? TRUE : FALSE); 1131264274Smav } 1132264274Smav } else 1133264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1134264274Smav beio->io_offset, beio->io_len, TRUE); 1135264274Smav} 1136264274Smav 1137264274Smavstatic void 1138229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1139229997Sken struct ctl_be_block_io *beio) 1140229997Sken{ 1141267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1142229997Sken struct bio *bio; 1143287664Smav struct cdevsw *csw; 1144287664Smav struct cdev *dev; 1145229997Sken off_t cur_offset; 1146287664Smav int i, max_iosize, ref; 1147229997Sken 1148229997Sken DPRINTF("entered\n"); 1149287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1150229997Sken 1151229997Sken /* 1152229997Sken * We have to limit our I/O size to the maximum supported by the 1153229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1154229997Sken * set it properly, use DFLTPHYS. 1155229997Sken */ 1156287664Smav if (csw) { 1157287664Smav max_iosize = dev->si_iosize_max; 1158287664Smav if (max_iosize < PAGE_SIZE) 1159287664Smav max_iosize = DFLTPHYS; 1160287664Smav } else 1161229997Sken max_iosize = DFLTPHYS; 1162229997Sken 1163229997Sken cur_offset = beio->io_offset; 1164229997Sken for (i = 0; i < beio->num_segs; i++) { 1165229997Sken size_t cur_size; 1166229997Sken uint8_t *cur_ptr; 1167229997Sken 1168229997Sken cur_size = beio->sg_segs[i].len; 1169229997Sken cur_ptr = beio->sg_segs[i].addr; 1170229997Sken 1171229997Sken while (cur_size > 0) { 1172229997Sken /* This can't fail, it's a blocking allocation. */ 1173229997Sken bio = g_alloc_bio(); 1174229997Sken 1175229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1176229997Sken 1177229997Sken bio->bio_cmd = beio->bio_cmd; 1178287664Smav bio->bio_dev = dev; 1179229997Sken bio->bio_caller1 = beio; 1180229997Sken bio->bio_length = min(cur_size, max_iosize); 1181229997Sken bio->bio_offset = cur_offset; 1182229997Sken bio->bio_data = cur_ptr; 1183229997Sken bio->bio_done = ctl_be_block_biodone; 1184287499Smav bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1185229997Sken 1186229997Sken cur_offset += bio->bio_length; 1187229997Sken cur_ptr += bio->bio_length; 1188229997Sken cur_size -= bio->bio_length; 1189229997Sken 1190267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1191229997Sken beio->num_bios_sent++; 1192229997Sken } 1193229997Sken } 1194267877Smav binuptime(&beio->ds_t0); 1195267877Smav mtx_lock(&be_lun->io_lock); 1196267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1197267877Smav beio->send_complete = 1; 1198267877Smav mtx_unlock(&be_lun->io_lock); 1199267877Smav 1200267877Smav /* 1201267877Smav * Fire off all allocated requests! 1202267877Smav */ 1203267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1204267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1205287664Smav if (csw) 1206287664Smav csw->d_strategy(bio); 1207287664Smav else { 1208287664Smav bio->bio_error = ENXIO; 1209287664Smav ctl_be_block_biodone(bio); 1210287664Smav } 1211267877Smav } 1212287664Smav if (csw) 1213287664Smav dev_relthread(dev, ref); 1214229997Sken} 1215229997Sken 1216274154Smavstatic uint64_t 1217274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1218274154Smav{ 1219274154Smav struct diocgattr_arg arg; 1220287664Smav struct cdevsw *csw; 1221287664Smav struct cdev *dev; 1222287664Smav int error, ref; 1223274154Smav 1224287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1225287664Smav if (csw == NULL) 1226274154Smav return (UINT64_MAX); 1227274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1228274154Smav arg.len = sizeof(arg.value.off); 1229287664Smav if (csw->d_ioctl) { 1230287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 1231287664Smav curthread); 1232287664Smav } else 1233287664Smav error = ENODEV; 1234287664Smav dev_relthread(dev, ref); 1235274154Smav if (error != 0) 1236274154Smav return (UINT64_MAX); 1237274154Smav return (arg.value.off); 1238274154Smav} 1239274154Smav 1240229997Skenstatic void 1241286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1242286353Smav union ctl_io *io) 1243286353Smav{ 1244287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1245286353Smav struct ctl_be_block_io *beio; 1246286353Smav struct ctl_lba_len_flags *lbalen; 1247286353Smav 1248286353Smav DPRINTF("entered\n"); 1249286353Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1250286353Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1251286353Smav 1252287499Smav beio->io_len = lbalen->len * cbe_lun->blocksize; 1253287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1254286353Smav beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1255286353Smav beio->bio_cmd = BIO_FLUSH; 1256286353Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1257286353Smav DPRINTF("SYNC\n"); 1258286353Smav be_lun->lun_flush(be_lun, beio); 1259286353Smav} 1260286353Smav 1261286353Smavstatic void 1262264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1263264274Smav{ 1264264274Smav union ctl_io *io; 1265264274Smav 1266264274Smav io = beio->io; 1267264274Smav ctl_free_beio(beio); 1268267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1269267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1270267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1271264274Smav ctl_config_write_done(io); 1272264274Smav return; 1273264274Smav } 1274264274Smav 1275264274Smav ctl_be_block_config_write(io); 1276264274Smav} 1277264274Smav 1278264274Smavstatic void 1279264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1280264274Smav union ctl_io *io) 1281264274Smav{ 1282287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1283264274Smav struct ctl_be_block_io *beio; 1284267515Smav struct ctl_lba_len_flags *lbalen; 1285278625Smav uint64_t len_left, lba; 1286278625Smav uint32_t pb, pbo, adj; 1287264274Smav int i, seglen; 1288264274Smav uint8_t *buf, *end; 1289264274Smav 1290264274Smav DPRINTF("entered\n"); 1291264274Smav 1292267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1293267537Smav lbalen = ARGS(beio->io); 1294264274Smav 1295271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1296269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1297264274Smav ctl_free_beio(beio); 1298264274Smav ctl_set_invalid_field(&io->scsiio, 1299264274Smav /*sks_valid*/ 1, 1300264274Smav /*command*/ 1, 1301264274Smav /*field*/ 1, 1302264274Smav /*bit_valid*/ 0, 1303264274Smav /*bit*/ 0); 1304264274Smav ctl_config_write_done(io); 1305264274Smav return; 1306264274Smav } 1307264274Smav 1308269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1309287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1310287499Smav beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1311264274Smav beio->bio_cmd = BIO_DELETE; 1312264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1313264274Smav 1314264274Smav be_lun->unmap(be_lun, beio); 1315264274Smav return; 1316264274Smav } 1317264274Smav 1318264274Smav beio->bio_cmd = BIO_WRITE; 1319264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1320264274Smav 1321264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1322267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1323264274Smav 1324287499Smav pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1325287499Smav if (be_lun->cbe_lun.pblockoff > 0) 1326287499Smav pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1327278625Smav else 1328278625Smav pbo = 0; 1329287499Smav len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1330264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1331264274Smav 1332264274Smav /* 1333264274Smav * Setup the S/G entry for this chunk. 1334264274Smav */ 1335264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1336287499Smav if (pb > cbe_lun->blocksize) { 1337287499Smav adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1338278619Smav seglen - pbo) % pb; 1339278619Smav if (seglen > adj) 1340278619Smav seglen -= adj; 1341278619Smav else 1342287499Smav seglen -= seglen % cbe_lun->blocksize; 1343278619Smav } else 1344287499Smav seglen -= seglen % cbe_lun->blocksize; 1345264274Smav beio->sg_segs[i].len = seglen; 1346264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1347264274Smav 1348264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1349264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1350264274Smav 1351264274Smav beio->num_segs++; 1352264274Smav len_left -= seglen; 1353264274Smav 1354264274Smav buf = beio->sg_segs[i].addr; 1355264274Smav end = buf + seglen; 1356287499Smav for (; buf < end; buf += cbe_lun->blocksize) { 1357287499Smav memcpy(buf, io->scsiio.kern_data_ptr, cbe_lun->blocksize); 1358267515Smav if (lbalen->flags & SWS_LBDATA) 1359267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1360264274Smav lba++; 1361264274Smav } 1362264274Smav } 1363264274Smav 1364287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1365287499Smav beio->io_len = lba * cbe_lun->blocksize; 1366264274Smav 1367264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1368264274Smav if (len_left > 0) { 1369267515Smav lbalen->lba += lba; 1370267515Smav lbalen->len -= lba; 1371264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1372264274Smav } 1373264274Smav 1374264274Smav be_lun->dispatch(be_lun, beio); 1375264274Smav} 1376264274Smav 1377264274Smavstatic void 1378264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1379264274Smav union ctl_io *io) 1380264274Smav{ 1381264274Smav struct ctl_be_block_io *beio; 1382267515Smav struct ctl_ptr_len_flags *ptrlen; 1383264274Smav 1384264274Smav DPRINTF("entered\n"); 1385264274Smav 1386267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1387267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1388264274Smav 1389269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1390264274Smav ctl_free_beio(beio); 1391264274Smav ctl_set_invalid_field(&io->scsiio, 1392264274Smav /*sks_valid*/ 0, 1393264274Smav /*command*/ 1, 1394264274Smav /*field*/ 0, 1395264274Smav /*bit_valid*/ 0, 1396264274Smav /*bit*/ 0); 1397264274Smav ctl_config_write_done(io); 1398264274Smav return; 1399264274Smav } 1400264274Smav 1401264274Smav beio->io_len = 0; 1402264274Smav beio->io_offset = -1; 1403264274Smav beio->bio_cmd = BIO_DELETE; 1404264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1405267515Smav DPRINTF("UNMAP\n"); 1406264274Smav be_lun->unmap(be_lun, beio); 1407264274Smav} 1408264274Smav 1409264274Smavstatic void 1410275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1411275474Smav{ 1412275474Smav union ctl_io *io; 1413275474Smav 1414275474Smav io = beio->io; 1415275474Smav ctl_free_beio(beio); 1416275474Smav ctl_config_read_done(io); 1417275474Smav} 1418275474Smav 1419275474Smavstatic void 1420275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1421275474Smav union ctl_io *io) 1422275474Smav{ 1423275474Smav struct ctl_be_block_io *beio; 1424275474Smav struct ctl_be_block_softc *softc; 1425275474Smav 1426275474Smav DPRINTF("entered\n"); 1427275474Smav 1428275474Smav softc = be_lun->softc; 1429275474Smav beio = ctl_alloc_beio(softc); 1430275474Smav beio->io = io; 1431275474Smav beio->lun = be_lun; 1432275474Smav beio->beio_cont = ctl_be_block_cr_done; 1433275474Smav PRIV(io)->ptr = (void *)beio; 1434275474Smav 1435275474Smav switch (io->scsiio.cdb[0]) { 1436275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1437275474Smav beio->bio_cmd = -1; 1438275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1439275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1440275474Smav beio->io_len = 0; 1441275474Smav if (be_lun->get_lba_status) 1442275474Smav be_lun->get_lba_status(be_lun, beio); 1443275474Smav else 1444275474Smav ctl_be_block_cr_done(beio); 1445275474Smav break; 1446275474Smav default: 1447275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1448275474Smav break; 1449275474Smav } 1450275474Smav} 1451275474Smav 1452275474Smavstatic void 1453264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1454264274Smav{ 1455264274Smav union ctl_io *io; 1456264274Smav 1457264274Smav io = beio->io; 1458264274Smav ctl_free_beio(beio); 1459264274Smav ctl_config_write_done(io); 1460264274Smav} 1461264274Smav 1462264274Smavstatic void 1463229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1464229997Sken union ctl_io *io) 1465229997Sken{ 1466229997Sken struct ctl_be_block_io *beio; 1467229997Sken struct ctl_be_block_softc *softc; 1468229997Sken 1469229997Sken DPRINTF("entered\n"); 1470229997Sken 1471229997Sken softc = be_lun->softc; 1472229997Sken beio = ctl_alloc_beio(softc); 1473229997Sken beio->io = io; 1474229997Sken beio->lun = be_lun; 1475264274Smav beio->beio_cont = ctl_be_block_cw_done; 1476286353Smav switch (io->scsiio.tag_type) { 1477286353Smav case CTL_TAG_ORDERED: 1478286353Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1479286353Smav break; 1480286353Smav case CTL_TAG_HEAD_OF_QUEUE: 1481286353Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1482286353Smav break; 1483286353Smav case CTL_TAG_UNTAGGED: 1484286353Smav case CTL_TAG_SIMPLE: 1485286353Smav case CTL_TAG_ACA: 1486286353Smav default: 1487286353Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1488286353Smav break; 1489286353Smav } 1490267519Smav PRIV(io)->ptr = (void *)beio; 1491229997Sken 1492229997Sken switch (io->scsiio.cdb[0]) { 1493229997Sken case SYNCHRONIZE_CACHE: 1494229997Sken case SYNCHRONIZE_CACHE_16: 1495286353Smav ctl_be_block_cw_dispatch_sync(be_lun, io); 1496229997Sken break; 1497264274Smav case WRITE_SAME_10: 1498264274Smav case WRITE_SAME_16: 1499264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1500264274Smav break; 1501264274Smav case UNMAP: 1502264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1503264274Smav break; 1504229997Sken default: 1505229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1506229997Sken break; 1507229997Sken } 1508229997Sken} 1509229997Sken 1510258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1511258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1512258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1513258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1514229997Sken 1515229997Skenstatic void 1516264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1517264886Smav{ 1518264886Smav struct ctl_be_block_lun *be_lun; 1519264886Smav union ctl_io *io; 1520264886Smav 1521264886Smav io = beio->io; 1522264886Smav be_lun = beio->lun; 1523264886Smav ctl_free_beio(beio); 1524267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1525267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1526267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1527267537Smav ctl_data_submit_done(io); 1528264886Smav return; 1529264886Smav } 1530264886Smav 1531264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1532264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1533264886Smav 1534267877Smav mtx_lock(&be_lun->queue_lock); 1535264886Smav /* 1536264886Smav * XXX KDM make sure that links is okay to use at this point. 1537264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1538264886Smav * or deal with resource allocation here. 1539264886Smav */ 1540264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1541267877Smav mtx_unlock(&be_lun->queue_lock); 1542264886Smav 1543264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1544264886Smav} 1545264886Smav 1546264886Smavstatic void 1547229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1548229997Sken union ctl_io *io) 1549229997Sken{ 1550287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1551229997Sken struct ctl_be_block_io *beio; 1552229997Sken struct ctl_be_block_softc *softc; 1553267537Smav struct ctl_lba_len_flags *lbalen; 1554267519Smav struct ctl_ptr_len_flags *bptrlen; 1555267519Smav uint64_t len_left, lbas; 1556229997Sken int i; 1557229997Sken 1558229997Sken softc = be_lun->softc; 1559229997Sken 1560229997Sken DPRINTF("entered\n"); 1561229997Sken 1562267537Smav lbalen = ARGS(io); 1563267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1564267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1565267537Smav } else { 1566229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1567229997Sken } 1568229997Sken 1569229997Sken beio = ctl_alloc_beio(softc); 1570229997Sken beio->io = io; 1571229997Sken beio->lun = be_lun; 1572267519Smav bptrlen = PRIV(io); 1573267519Smav bptrlen->ptr = (void *)beio; 1574229997Sken 1575229997Sken switch (io->scsiio.tag_type) { 1576229997Sken case CTL_TAG_ORDERED: 1577229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1578229997Sken break; 1579229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1580229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1581229997Sken break; 1582229997Sken case CTL_TAG_UNTAGGED: 1583229997Sken case CTL_TAG_SIMPLE: 1584229997Sken case CTL_TAG_ACA: 1585229997Sken default: 1586229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1587229997Sken break; 1588229997Sken } 1589229997Sken 1590267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1591267537Smav beio->bio_cmd = BIO_WRITE; 1592267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1593267537Smav } else { 1594229997Sken beio->bio_cmd = BIO_READ; 1595229997Sken beio->ds_trans_type = DEVSTAT_READ; 1596229997Sken } 1597229997Sken 1598264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1599229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1600267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1601267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1602267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1603267537Smav else 1604267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1605287499Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 1606287499Smav beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 1607287499Smav beio->io_len = lbas * cbe_lun->blocksize; 1608267519Smav bptrlen->len += lbas; 1609229997Sken 1610264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1611264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1612264886Smav i, CTLBLK_MAX_SEGS)); 1613229997Sken 1614229997Sken /* 1615229997Sken * Setup the S/G entry for this chunk. 1616229997Sken */ 1617264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1618229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1619229997Sken 1620229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1621229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1622229997Sken 1623267537Smav /* Set up second segment for compare operation. */ 1624267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1625267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1626267537Smav beio->sg_segs[i].len; 1627267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1628267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1629267537Smav } 1630267537Smav 1631229997Sken beio->num_segs++; 1632229997Sken len_left -= beio->sg_segs[i].len; 1633229997Sken } 1634267519Smav if (bptrlen->len < lbalen->len) 1635264886Smav beio->beio_cont = ctl_be_block_next; 1636264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1637267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1638267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1639267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1640267537Smav else 1641267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1642264886Smav io->scsiio.kern_data_len = beio->io_len; 1643264886Smav io->scsiio.kern_data_resid = 0; 1644264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1645264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1646229997Sken 1647229997Sken /* 1648229997Sken * For the read case, we need to read the data into our buffers and 1649229997Sken * then we can send it back to the user. For the write case, we 1650229997Sken * need to get the data from the user first. 1651229997Sken */ 1652229997Sken if (beio->bio_cmd == BIO_READ) { 1653229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1654229997Sken be_lun->dispatch(be_lun, beio); 1655229997Sken } else { 1656229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1657229997Sken#ifdef CTL_TIME_IO 1658229997Sken getbintime(&io->io_hdr.dma_start_bt); 1659229997Sken#endif 1660229997Sken ctl_datamove(io); 1661229997Sken } 1662229997Sken} 1663229997Sken 1664229997Skenstatic void 1665229997Skenctl_be_block_worker(void *context, int pending) 1666229997Sken{ 1667287670Smav struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1668287670Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1669229997Sken union ctl_io *io; 1670287670Smav struct ctl_be_block_io *beio; 1671229997Sken 1672229997Sken DPRINTF("entered\n"); 1673287670Smav /* 1674287670Smav * Fetch and process I/Os from all queues. If we detect LUN 1675287670Smav * CTL_LUN_FLAG_OFFLINE status here -- it is result of a race, 1676287670Smav * so make response maximally opaque to not confuse initiator. 1677287670Smav */ 1678229997Sken for (;;) { 1679287670Smav mtx_lock(&be_lun->queue_lock); 1680229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1681229997Sken if (io != NULL) { 1682229997Sken DPRINTF("datamove queue\n"); 1683229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1684229997Sken ctl_io_hdr, links); 1685267877Smav mtx_unlock(&be_lun->queue_lock); 1686267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1687287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1688287670Smav ctl_set_busy(&io->scsiio); 1689287670Smav ctl_complete_beio(beio); 1690287670Smav return; 1691287670Smav } 1692229997Sken be_lun->dispatch(be_lun, beio); 1693229997Sken continue; 1694229997Sken } 1695229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1696229997Sken if (io != NULL) { 1697229997Sken DPRINTF("config write queue\n"); 1698229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1699229997Sken ctl_io_hdr, links); 1700267877Smav mtx_unlock(&be_lun->queue_lock); 1701287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1702287670Smav ctl_set_busy(&io->scsiio); 1703287670Smav ctl_config_write_done(io); 1704287670Smav return; 1705287670Smav } 1706229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1707229997Sken continue; 1708229997Sken } 1709275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1710275474Smav if (io != NULL) { 1711275474Smav DPRINTF("config read queue\n"); 1712275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1713275474Smav ctl_io_hdr, links); 1714275474Smav mtx_unlock(&be_lun->queue_lock); 1715287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1716287670Smav ctl_set_busy(&io->scsiio); 1717287670Smav ctl_config_read_done(io); 1718287670Smav return; 1719287670Smav } 1720275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1721275474Smav continue; 1722275474Smav } 1723229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1724229997Sken if (io != NULL) { 1725229997Sken DPRINTF("input queue\n"); 1726229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1727229997Sken ctl_io_hdr, links); 1728267877Smav mtx_unlock(&be_lun->queue_lock); 1729287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1730287670Smav ctl_set_busy(&io->scsiio); 1731287670Smav ctl_data_submit_done(io); 1732287670Smav return; 1733287670Smav } 1734229997Sken ctl_be_block_dispatch(be_lun, io); 1735229997Sken continue; 1736229997Sken } 1737229997Sken 1738229997Sken /* 1739229997Sken * If we get here, there is no work left in the queues, so 1740229997Sken * just break out and let the task queue go to sleep. 1741229997Sken */ 1742287670Smav mtx_unlock(&be_lun->queue_lock); 1743229997Sken break; 1744229997Sken } 1745229997Sken} 1746229997Sken 1747229997Sken/* 1748229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1749229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1750229997Sken * thread. 1751229997Sken */ 1752229997Skenstatic int 1753229997Skenctl_be_block_submit(union ctl_io *io) 1754229997Sken{ 1755229997Sken struct ctl_be_block_lun *be_lun; 1756287499Smav struct ctl_be_lun *cbe_lun; 1757229997Sken 1758229997Sken DPRINTF("entered\n"); 1759229997Sken 1760287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1761229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1762287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1763229997Sken 1764229997Sken /* 1765229997Sken * Make sure we only get SCSI I/O. 1766229997Sken */ 1767229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1768229997Sken "%#x) encountered", io->io_hdr.io_type)); 1769229997Sken 1770267519Smav PRIV(io)->len = 0; 1771267519Smav 1772267877Smav mtx_lock(&be_lun->queue_lock); 1773229997Sken /* 1774229997Sken * XXX KDM make sure that links is okay to use at this point. 1775229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1776229997Sken * or deal with resource allocation here. 1777229997Sken */ 1778229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1779267877Smav mtx_unlock(&be_lun->queue_lock); 1780229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1781229997Sken 1782267514Smav return (CTL_RETVAL_COMPLETE); 1783229997Sken} 1784229997Sken 1785229997Skenstatic int 1786229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1787229997Sken int flag, struct thread *td) 1788229997Sken{ 1789229997Sken struct ctl_be_block_softc *softc; 1790229997Sken int error; 1791229997Sken 1792229997Sken softc = &backend_block_softc; 1793229997Sken 1794229997Sken error = 0; 1795229997Sken 1796229997Sken switch (cmd) { 1797229997Sken case CTL_LUN_REQ: { 1798229997Sken struct ctl_lun_req *lun_req; 1799229997Sken 1800229997Sken lun_req = (struct ctl_lun_req *)addr; 1801229997Sken 1802229997Sken switch (lun_req->reqtype) { 1803229997Sken case CTL_LUNREQ_CREATE: 1804229997Sken error = ctl_be_block_create(softc, lun_req); 1805229997Sken break; 1806229997Sken case CTL_LUNREQ_RM: 1807229997Sken error = ctl_be_block_rm(softc, lun_req); 1808229997Sken break; 1809232604Strasz case CTL_LUNREQ_MODIFY: 1810232604Strasz error = ctl_be_block_modify(softc, lun_req); 1811232604Strasz break; 1812229997Sken default: 1813229997Sken lun_req->status = CTL_LUN_ERROR; 1814229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1815272911Smav "invalid LUN request type %d", 1816229997Sken lun_req->reqtype); 1817229997Sken break; 1818229997Sken } 1819229997Sken break; 1820229997Sken } 1821229997Sken default: 1822229997Sken error = ENOTTY; 1823229997Sken break; 1824229997Sken } 1825229997Sken 1826229997Sken return (error); 1827229997Sken} 1828229997Sken 1829229997Skenstatic int 1830229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1831229997Sken{ 1832287499Smav struct ctl_be_lun *cbe_lun; 1833229997Sken struct ctl_be_block_filedata *file_data; 1834229997Sken struct ctl_lun_create_params *params; 1835275865Smav char *value; 1836229997Sken struct vattr vattr; 1837275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1838229997Sken int error; 1839229997Sken 1840229997Sken error = 0; 1841287499Smav cbe_lun = &be_lun->cbe_lun; 1842229997Sken file_data = &be_lun->backend.file; 1843272911Smav params = &be_lun->params; 1844229997Sken 1845229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1846229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1847229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1848275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1849275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1850287499Smav be_lun->unmap = NULL; 1851287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1852229997Sken 1853229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1854229997Sken if (error != 0) { 1855229997Sken snprintf(req->error_str, sizeof(req->error_str), 1856229997Sken "error calling VOP_GETATTR() for file %s", 1857229997Sken be_lun->dev_path); 1858229997Sken return (error); 1859229997Sken } 1860229997Sken 1861229997Sken /* 1862229997Sken * Verify that we have the ability to upgrade to exclusive 1863229997Sken * access on this file so we can trap errors at open instead 1864229997Sken * of reporting them during first access. 1865229997Sken */ 1866229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1867229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1868229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1869229997Sken error = EBADF; 1870229997Sken snprintf(req->error_str, sizeof(req->error_str), 1871229997Sken "error locking file %s", be_lun->dev_path); 1872229997Sken return (error); 1873229997Sken } 1874229997Sken } 1875229997Sken 1876229997Sken file_data->cred = crhold(curthread->td_ucred); 1877232604Strasz if (params->lun_size_bytes != 0) 1878232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1879232604Strasz else 1880232604Strasz be_lun->size_bytes = vattr.va_size; 1881229997Sken 1882229997Sken /* 1883273029Smav * For files we can use any logical block size. Prefer 512 bytes 1884273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1885273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1886273029Smav * logical block size -- report it as physical block size. 1887229997Sken */ 1888229997Sken if (params->blocksize_bytes != 0) 1889287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1890229997Sken else 1891287499Smav cbe_lun->blocksize = 512; 1892287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1893287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1894287499Smav 0 : (be_lun->size_blocks - 1); 1895275865Smav 1896275865Smav us = ps = vattr.va_blocksize; 1897275865Smav uo = po = 0; 1898275865Smav 1899287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1900275865Smav if (value != NULL) 1901275865Smav ctl_expand_number(value, &ps); 1902287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1903275865Smav if (value != NULL) 1904275865Smav ctl_expand_number(value, &po); 1905287499Smav pss = ps / cbe_lun->blocksize; 1906287499Smav pos = po / cbe_lun->blocksize; 1907287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1908287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1909287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1910287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1911273029Smav } 1912229997Sken 1913287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1914275865Smav if (value != NULL) 1915275865Smav ctl_expand_number(value, &us); 1916287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1917275865Smav if (value != NULL) 1918275865Smav ctl_expand_number(value, &uo); 1919287499Smav uss = us / cbe_lun->blocksize; 1920287499Smav uos = uo / cbe_lun->blocksize; 1921287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1922287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1923287499Smav cbe_lun->ublockexp = fls(uss) - 1; 1924287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 1925275865Smav } 1926275865Smav 1927229997Sken /* 1928229997Sken * Sanity check. The media size has to be at least one 1929229997Sken * sector long. 1930229997Sken */ 1931287499Smav if (be_lun->size_bytes < cbe_lun->blocksize) { 1932229997Sken error = EINVAL; 1933229997Sken snprintf(req->error_str, sizeof(req->error_str), 1934229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1935287499Smav (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1936229997Sken } 1937275920Smav 1938287499Smav cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1939229997Sken return (error); 1940229997Sken} 1941229997Sken 1942229997Skenstatic int 1943229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1944229997Sken{ 1945287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1946229997Sken struct ctl_lun_create_params *params; 1947287664Smav struct cdevsw *csw; 1948229997Sken struct cdev *dev; 1949275865Smav char *value; 1950287664Smav int error, atomic, maxio, ref, unmap, tmp; 1951287221Smav off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1952229997Sken 1953272911Smav params = &be_lun->params; 1954229997Sken 1955229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1956287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1957287664Smav if (csw == NULL) 1958287664Smav return (ENXIO); 1959287664Smav if (strcmp(csw->d_name, "zvol") == 0) { 1960269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1961275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1962275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1963275920Smav } else { 1964269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1965287499Smav be_lun->get_lba_status = NULL; 1966275920Smav atomic = 0; 1967287664Smav maxio = dev->si_iosize_max; 1968275920Smav if (maxio <= 0) 1969275920Smav maxio = DFLTPHYS; 1970275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1971275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1972275920Smav } 1973269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1974274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1975287499Smav be_lun->unmap = ctl_be_block_unmap_dev; 1976229997Sken 1977287664Smav if (!csw->d_ioctl) { 1978287664Smav dev_relthread(dev, ref); 1979229997Sken snprintf(req->error_str, sizeof(req->error_str), 1980287664Smav "no d_ioctl for device %s!", be_lun->dev_path); 1981229997Sken return (ENODEV); 1982229997Sken } 1983229997Sken 1984287664Smav error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1985229997Sken curthread); 1986229997Sken if (error) { 1987287664Smav dev_relthread(dev, ref); 1988229997Sken snprintf(req->error_str, sizeof(req->error_str), 1989272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1990272911Smav "on %s!", error, be_lun->dev_path); 1991229997Sken return (error); 1992229997Sken } 1993229997Sken 1994229997Sken /* 1995229997Sken * If the user has asked for a blocksize that is greater than the 1996229997Sken * backing device's blocksize, we can do it only if the blocksize 1997229997Sken * the user is asking for is an even multiple of the underlying 1998229997Sken * device's blocksize. 1999229997Sken */ 2000286811Smav if ((params->blocksize_bytes != 0) && 2001286811Smav (params->blocksize_bytes >= tmp)) { 2002286811Smav if (params->blocksize_bytes % tmp == 0) { 2003287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2004229997Sken } else { 2005287664Smav dev_relthread(dev, ref); 2006229997Sken snprintf(req->error_str, sizeof(req->error_str), 2007272911Smav "requested blocksize %u is not an even " 2008229997Sken "multiple of backing device blocksize %u", 2009287221Smav params->blocksize_bytes, tmp); 2010229997Sken return (EINVAL); 2011229997Sken } 2012286811Smav } else if (params->blocksize_bytes != 0) { 2013287664Smav dev_relthread(dev, ref); 2014229997Sken snprintf(req->error_str, sizeof(req->error_str), 2015272911Smav "requested blocksize %u < backing device " 2016287221Smav "blocksize %u", params->blocksize_bytes, tmp); 2017229997Sken return (EINVAL); 2018286811Smav } else 2019287499Smav cbe_lun->blocksize = tmp; 2020229997Sken 2021287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 2022287664Smav curthread); 2023229997Sken if (error) { 2024287664Smav dev_relthread(dev, ref); 2025229997Sken snprintf(req->error_str, sizeof(req->error_str), 2026272911Smav "error %d returned for DIOCGMEDIASIZE " 2027272911Smav " ioctl on %s!", error, 2028232604Strasz be_lun->dev_path); 2029229997Sken return (error); 2030229997Sken } 2031229997Sken 2032232604Strasz if (params->lun_size_bytes != 0) { 2033287221Smav if (params->lun_size_bytes > otmp) { 2034287664Smav dev_relthread(dev, ref); 2035232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2036272911Smav "requested LUN size %ju > backing device " 2037272911Smav "size %ju", 2038232604Strasz (uintmax_t)params->lun_size_bytes, 2039287221Smav (uintmax_t)otmp); 2040232604Strasz return (EINVAL); 2041232604Strasz } 2042232604Strasz 2043232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2044286811Smav } else 2045287221Smav be_lun->size_bytes = otmp; 2046287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2047287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2048287499Smav 0 : (be_lun->size_blocks - 1); 2049232604Strasz 2050287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 2051287664Smav curthread); 2052264191Smav if (error) 2053264191Smav ps = po = 0; 2054264191Smav else { 2055287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2056287664Smav FREAD, curthread); 2057264191Smav if (error) 2058264191Smav po = 0; 2059264191Smav } 2060275865Smav us = ps; 2061275865Smav uo = po; 2062275865Smav 2063287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 2064275865Smav if (value != NULL) 2065275865Smav ctl_expand_number(value, &ps); 2066287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 2067275865Smav if (value != NULL) 2068275865Smav ctl_expand_number(value, &po); 2069287499Smav pss = ps / cbe_lun->blocksize; 2070287499Smav pos = po / cbe_lun->blocksize; 2071287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2072287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2073287499Smav cbe_lun->pblockexp = fls(pss) - 1; 2074287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 2075264191Smav } 2076264191Smav 2077287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 2078275865Smav if (value != NULL) 2079275865Smav ctl_expand_number(value, &us); 2080287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 2081275865Smav if (value != NULL) 2082275865Smav ctl_expand_number(value, &uo); 2083287499Smav uss = us / cbe_lun->blocksize; 2084287499Smav uos = uo / cbe_lun->blocksize; 2085287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2086287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2087287499Smav cbe_lun->ublockexp = fls(uss) - 1; 2088287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 2089275865Smav } 2090275865Smav 2091287499Smav cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2092287499Smav cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2093278672Smav 2094278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2095278672Smav unmap = 1; 2096278672Smav } else { 2097278672Smav struct diocgattr_arg arg; 2098278672Smav 2099278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2100278672Smav arg.len = sizeof(arg.value.i); 2101287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2102287664Smav curthread); 2103278672Smav unmap = (error == 0) ? arg.value.i : 0; 2104278672Smav } 2105287499Smav value = ctl_get_opt(&cbe_lun->options, "unmap"); 2106278672Smav if (value != NULL) 2107278672Smav unmap = (strcmp(value, "on") == 0); 2108278672Smav if (unmap) 2109287499Smav cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2110287499Smav else 2111287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2112278672Smav 2113287664Smav dev_relthread(dev, ref); 2114229997Sken return (0); 2115229997Sken} 2116229997Sken 2117229997Skenstatic int 2118229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2119229997Sken{ 2120287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2121287499Smav int flags; 2122287499Smav 2123229997Sken if (be_lun->vn) { 2124287499Smav flags = FREAD; 2125287499Smav if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2126287499Smav flags |= FWRITE; 2127229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2128229997Sken be_lun->vn = NULL; 2129229997Sken 2130229997Sken switch (be_lun->dev_type) { 2131229997Sken case CTL_BE_BLOCK_DEV: 2132229997Sken break; 2133229997Sken case CTL_BE_BLOCK_FILE: 2134229997Sken if (be_lun->backend.file.cred != NULL) { 2135229997Sken crfree(be_lun->backend.file.cred); 2136229997Sken be_lun->backend.file.cred = NULL; 2137229997Sken } 2138229997Sken break; 2139229997Sken case CTL_BE_BLOCK_NONE: 2140258871Strasz break; 2141229997Sken default: 2142229997Sken panic("Unexpected backend type."); 2143229997Sken break; 2144229997Sken } 2145272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2146229997Sken } 2147229997Sken return (0); 2148229997Sken} 2149229997Sken 2150229997Skenstatic int 2151229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 2152287499Smav struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2153229997Sken{ 2154287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2155229997Sken struct nameidata nd; 2156287499Smav char *value; 2157287499Smav int error, flags; 2158229997Sken 2159229997Sken error = 0; 2160229997Sken if (rootvnode == NULL) { 2161229997Sken snprintf(req->error_str, sizeof(req->error_str), 2162272911Smav "Root filesystem is not mounted"); 2163229997Sken return (1); 2164229997Sken } 2165285391Smjg pwd_ensure_dirs(); 2166229997Sken 2167287499Smav value = ctl_get_opt(&cbe_lun->options, "file"); 2168287499Smav if (value == NULL) { 2169287499Smav snprintf(req->error_str, sizeof(req->error_str), 2170287499Smav "no file argument specified"); 2171287499Smav return (1); 2172287499Smav } 2173287499Smav free(be_lun->dev_path, M_CTLBLK); 2174287499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2175287499Smav 2176287499Smav flags = FREAD; 2177287499Smav value = ctl_get_opt(&cbe_lun->options, "readonly"); 2178287499Smav if (value == NULL || strcmp(value, "on") != 0) 2179287499Smav flags |= FWRITE; 2180287499Smav 2181287499Smavagain: 2182229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2183229997Sken error = vn_open(&nd, &flags, 0, NULL); 2184287499Smav if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2185287499Smav flags &= ~FWRITE; 2186287499Smav goto again; 2187287499Smav } 2188229997Sken if (error) { 2189229997Sken /* 2190229997Sken * This is the only reasonable guess we can make as far as 2191229997Sken * path if the user doesn't give us a fully qualified path. 2192229997Sken * If they want to specify a file, they need to specify the 2193229997Sken * full path. 2194229997Sken */ 2195229997Sken if (be_lun->dev_path[0] != '/') { 2196229997Sken char *dev_name; 2197229997Sken 2198287499Smav asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2199287499Smav be_lun->dev_path); 2200287499Smav free(be_lun->dev_path, M_CTLBLK); 2201287499Smav be_lun->dev_path = dev_name; 2202287499Smav goto again; 2203229997Sken } 2204229997Sken snprintf(req->error_str, sizeof(req->error_str), 2205272911Smav "error opening %s: %d", be_lun->dev_path, error); 2206229997Sken return (error); 2207229997Sken } 2208287499Smav if (flags & FWRITE) 2209287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2210287499Smav else 2211287499Smav cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2212229997Sken 2213229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2214229997Sken be_lun->vn = nd.ni_vp; 2215229997Sken 2216229997Sken /* We only support disks and files. */ 2217229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2218229997Sken error = ctl_be_block_open_dev(be_lun, req); 2219229997Sken } else if (be_lun->vn->v_type == VREG) { 2220229997Sken error = ctl_be_block_open_file(be_lun, req); 2221229997Sken } else { 2222229997Sken error = EINVAL; 2223229997Sken snprintf(req->error_str, sizeof(req->error_str), 2224258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2225229997Sken } 2226229997Sken VOP_UNLOCK(be_lun->vn, 0); 2227229997Sken 2228286811Smav if (error != 0) 2229229997Sken ctl_be_block_close(be_lun); 2230287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2231287499Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2232287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2233287499Smav value = ctl_get_opt(&cbe_lun->options, "serseq"); 2234287499Smav if (value != NULL && strcmp(value, "on") == 0) 2235287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2236287499Smav else if (value != NULL && strcmp(value, "read") == 0) 2237287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2238287499Smav else if (value != NULL && strcmp(value, "off") == 0) 2239287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2240229997Sken return (0); 2241229997Sken} 2242229997Sken 2243229997Skenstatic int 2244229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2245229997Sken{ 2246287499Smav struct ctl_be_lun *cbe_lun; 2247229997Sken struct ctl_be_block_lun *be_lun; 2248229997Sken struct ctl_lun_create_params *params; 2249267481Smav char num_thread_str[16]; 2250229997Sken char tmpstr[32]; 2251267481Smav char *value; 2252278672Smav int retval, num_threads; 2253267481Smav int tmp_num_threads; 2254229997Sken 2255229997Sken params = &req->reqdata.create; 2256229997Sken retval = 0; 2257272911Smav req->status = CTL_LUN_OK; 2258229997Sken 2259229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2260287499Smav cbe_lun = &be_lun->cbe_lun; 2261287499Smav cbe_lun->be_lun = be_lun; 2262272911Smav be_lun->params = req->reqdata.create; 2263229997Sken be_lun->softc = softc; 2264229997Sken STAILQ_INIT(&be_lun->input_queue); 2265275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2266229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2267229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2268229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2269267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2270267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2271287499Smav ctl_init_opts(&cbe_lun->options, 2272268280Smav req->num_be_args, req->kern_be_args); 2273264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2274256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2275229997Sken if (be_lun->lun_zone == NULL) { 2276229997Sken snprintf(req->error_str, sizeof(req->error_str), 2277272911Smav "error allocating UMA zone"); 2278229997Sken goto bailout_error; 2279229997Sken } 2280229997Sken 2281229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2282287499Smav cbe_lun->lun_type = params->device_type; 2283229997Sken else 2284287499Smav cbe_lun->lun_type = T_DIRECT; 2285287499Smav be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2286287621Smav cbe_lun->flags = 0; 2287287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2288287621Smav if (value != NULL) { 2289287621Smav if (strcmp(value, "primary") == 0) 2290287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2291287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2292287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2293229997Sken 2294287499Smav if (cbe_lun->lun_type == T_DIRECT) { 2295286811Smav be_lun->size_bytes = params->lun_size_bytes; 2296286811Smav if (params->blocksize_bytes != 0) 2297287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2298286811Smav else 2299287499Smav cbe_lun->blocksize = 512; 2300287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2301287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2302287499Smav 0 : (be_lun->size_blocks - 1); 2303229997Sken 2304287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2305287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2306287621Smav retval = ctl_be_block_open(softc, be_lun, req); 2307287621Smav if (retval != 0) { 2308287621Smav retval = 0; 2309287621Smav req->status = CTL_LUN_WARNING; 2310287621Smav } 2311229997Sken } 2312287499Smav num_threads = cbb_num_threads; 2313229997Sken } else { 2314229997Sken num_threads = 1; 2315229997Sken } 2316229997Sken 2317229997Sken /* 2318229997Sken * XXX This searching loop might be refactored to be combined with 2319229997Sken * the loop above, 2320229997Sken */ 2321287499Smav value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2322267481Smav if (value != NULL) { 2323267481Smav tmp_num_threads = strtol(value, NULL, 0); 2324229997Sken 2325267481Smav /* 2326267481Smav * We don't let the user specify less than one 2327267481Smav * thread, but hope he's clueful enough not to 2328267481Smav * specify 1000 threads. 2329267481Smav */ 2330267481Smav if (tmp_num_threads < 1) { 2331267481Smav snprintf(req->error_str, sizeof(req->error_str), 2332272911Smav "invalid number of threads %s", 2333272911Smav num_thread_str); 2334267481Smav goto bailout_error; 2335229997Sken } 2336267481Smav num_threads = tmp_num_threads; 2337229997Sken } 2338229997Sken 2339272911Smav if (be_lun->vn == NULL) 2340287499Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2341229997Sken /* Tell the user the blocksize we ended up using */ 2342272911Smav params->lun_size_bytes = be_lun->size_bytes; 2343287499Smav params->blocksize_bytes = cbe_lun->blocksize; 2344229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2345287499Smav cbe_lun->req_lun_id = params->req_lun_id; 2346287499Smav cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2347229997Sken } else 2348287499Smav cbe_lun->req_lun_id = 0; 2349229997Sken 2350287499Smav cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2351287499Smav cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2352287499Smav cbe_lun->be = &ctl_be_block_driver; 2353229997Sken 2354229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2355229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2356229997Sken softc->num_luns); 2357287499Smav strncpy((char *)cbe_lun->serial_num, tmpstr, 2358287499Smav MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2359229997Sken 2360229997Sken /* Tell the user what we used for a serial number */ 2361229997Sken strncpy((char *)params->serial_num, tmpstr, 2362275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2363229997Sken } else { 2364287499Smav strncpy((char *)cbe_lun->serial_num, params->serial_num, 2365287499Smav MIN(sizeof(cbe_lun->serial_num), 2366229997Sken sizeof(params->serial_num))); 2367229997Sken } 2368229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2369229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2370287499Smav strncpy((char *)cbe_lun->device_id, tmpstr, 2371287499Smav MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2372229997Sken 2373229997Sken /* Tell the user what we used for a device ID */ 2374229997Sken strncpy((char *)params->device_id, tmpstr, 2375275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2376229997Sken } else { 2377287499Smav strncpy((char *)cbe_lun->device_id, params->device_id, 2378287499Smav MIN(sizeof(cbe_lun->device_id), 2379275953Smav sizeof(params->device_id))); 2380229997Sken } 2381229997Sken 2382229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2383229997Sken 2384229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2385229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2386229997Sken 2387229997Sken if (be_lun->io_taskqueue == NULL) { 2388229997Sken snprintf(req->error_str, sizeof(req->error_str), 2389272911Smav "unable to create taskqueue"); 2390229997Sken goto bailout_error; 2391229997Sken } 2392229997Sken 2393229997Sken /* 2394229997Sken * Note that we start the same number of threads by default for 2395229997Sken * both the file case and the block device case. For the file 2396229997Sken * case, we need multiple threads to allow concurrency, because the 2397229997Sken * vnode interface is designed to be a blocking interface. For the 2398229997Sken * block device case, ZFS zvols at least will block the caller's 2399229997Sken * context in many instances, and so we need multiple threads to 2400229997Sken * overcome that problem. Other block devices don't need as many 2401229997Sken * threads, but they shouldn't cause too many problems. 2402229997Sken * 2403229997Sken * If the user wants to just have a single thread for a block 2404229997Sken * device, he can specify that when the LUN is created, or change 2405229997Sken * the tunable/sysctl to alter the default number of threads. 2406229997Sken */ 2407229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2408229997Sken /*num threads*/num_threads, 2409229997Sken /*priority*/PWAIT, 2410229997Sken /*thread name*/ 2411229997Sken "%s taskq", be_lun->lunname); 2412229997Sken 2413229997Sken if (retval != 0) 2414229997Sken goto bailout_error; 2415229997Sken 2416229997Sken be_lun->num_threads = num_threads; 2417229997Sken 2418229997Sken mtx_lock(&softc->lock); 2419229997Sken softc->num_luns++; 2420229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2421229997Sken 2422229997Sken mtx_unlock(&softc->lock); 2423229997Sken 2424287499Smav retval = ctl_add_lun(&be_lun->cbe_lun); 2425229997Sken if (retval != 0) { 2426229997Sken mtx_lock(&softc->lock); 2427229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2428229997Sken links); 2429229997Sken softc->num_luns--; 2430229997Sken mtx_unlock(&softc->lock); 2431229997Sken snprintf(req->error_str, sizeof(req->error_str), 2432272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2433272911Smav "details", retval); 2434229997Sken retval = 0; 2435229997Sken goto bailout_error; 2436229997Sken } 2437229997Sken 2438229997Sken mtx_lock(&softc->lock); 2439229997Sken 2440229997Sken /* 2441229997Sken * Tell the config_status routine that we're waiting so it won't 2442229997Sken * clean up the LUN in the event of an error. 2443229997Sken */ 2444229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2445229997Sken 2446229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2447229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2448229997Sken if (retval == EINTR) 2449229997Sken break; 2450229997Sken } 2451229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2452229997Sken 2453229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2454229997Sken snprintf(req->error_str, sizeof(req->error_str), 2455272911Smav "LUN configuration error, see dmesg for details"); 2456229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2457229997Sken links); 2458229997Sken softc->num_luns--; 2459229997Sken mtx_unlock(&softc->lock); 2460229997Sken goto bailout_error; 2461229997Sken } else { 2462287499Smav params->req_lun_id = cbe_lun->lun_id; 2463229997Sken } 2464229997Sken 2465229997Sken mtx_unlock(&softc->lock); 2466229997Sken 2467229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2468287499Smav cbe_lun->blocksize, 2469229997Sken DEVSTAT_ALL_SUPPORTED, 2470287499Smav cbe_lun->lun_type 2471229997Sken | DEVSTAT_TYPE_IF_OTHER, 2472229997Sken DEVSTAT_PRIORITY_OTHER); 2473229997Sken 2474229997Sken return (retval); 2475229997Sken 2476229997Skenbailout_error: 2477229997Sken req->status = CTL_LUN_ERROR; 2478229997Sken 2479267429Smav if (be_lun->io_taskqueue != NULL) 2480267429Smav taskqueue_free(be_lun->io_taskqueue); 2481229997Sken ctl_be_block_close(be_lun); 2482267429Smav if (be_lun->dev_path != NULL) 2483267429Smav free(be_lun->dev_path, M_CTLBLK); 2484267429Smav if (be_lun->lun_zone != NULL) 2485267429Smav uma_zdestroy(be_lun->lun_zone); 2486287499Smav ctl_free_opts(&cbe_lun->options); 2487267877Smav mtx_destroy(&be_lun->queue_lock); 2488267877Smav mtx_destroy(&be_lun->io_lock); 2489229997Sken free(be_lun, M_CTLBLK); 2490229997Sken 2491229997Sken return (retval); 2492229997Sken} 2493229997Sken 2494229997Skenstatic int 2495229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2496229997Sken{ 2497229997Sken struct ctl_lun_rm_params *params; 2498229997Sken struct ctl_be_block_lun *be_lun; 2499287670Smav struct ctl_be_lun *cbe_lun; 2500229997Sken int retval; 2501229997Sken 2502229997Sken params = &req->reqdata.rm; 2503229997Sken 2504229997Sken mtx_lock(&softc->lock); 2505229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2506287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2507229997Sken break; 2508229997Sken } 2509229997Sken mtx_unlock(&softc->lock); 2510229997Sken 2511229997Sken if (be_lun == NULL) { 2512229997Sken snprintf(req->error_str, sizeof(req->error_str), 2513272911Smav "LUN %u is not managed by the block backend", 2514272911Smav params->lun_id); 2515229997Sken goto bailout_error; 2516229997Sken } 2517287670Smav cbe_lun = &be_lun->cbe_lun; 2518229997Sken 2519287670Smav retval = ctl_disable_lun(cbe_lun); 2520229997Sken if (retval != 0) { 2521229997Sken snprintf(req->error_str, sizeof(req->error_str), 2522272911Smav "error %d returned from ctl_disable_lun() for " 2523272911Smav "LUN %d", retval, params->lun_id); 2524229997Sken goto bailout_error; 2525287670Smav } 2526229997Sken 2527287670Smav if (be_lun->vn != NULL) { 2528287670Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2529287670Smav ctl_lun_offline(cbe_lun); 2530287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2531287670Smav ctl_be_block_close(be_lun); 2532229997Sken } 2533229997Sken 2534287670Smav retval = ctl_invalidate_lun(cbe_lun); 2535229997Sken if (retval != 0) { 2536229997Sken snprintf(req->error_str, sizeof(req->error_str), 2537272911Smav "error %d returned from ctl_invalidate_lun() for " 2538272911Smav "LUN %d", retval, params->lun_id); 2539229997Sken goto bailout_error; 2540229997Sken } 2541229997Sken 2542229997Sken mtx_lock(&softc->lock); 2543229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2544229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2545229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2546229997Sken if (retval == EINTR) 2547229997Sken break; 2548229997Sken } 2549229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2550229997Sken 2551229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2552229997Sken snprintf(req->error_str, sizeof(req->error_str), 2553272911Smav "interrupted waiting for LUN to be freed"); 2554229997Sken mtx_unlock(&softc->lock); 2555229997Sken goto bailout_error; 2556229997Sken } 2557229997Sken 2558229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2559229997Sken 2560229997Sken softc->num_luns--; 2561229997Sken mtx_unlock(&softc->lock); 2562229997Sken 2563287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2564229997Sken taskqueue_free(be_lun->io_taskqueue); 2565229997Sken 2566229997Sken if (be_lun->disk_stats != NULL) 2567229997Sken devstat_remove_entry(be_lun->disk_stats); 2568229997Sken 2569229997Sken uma_zdestroy(be_lun->lun_zone); 2570229997Sken 2571287670Smav ctl_free_opts(&cbe_lun->options); 2572229997Sken free(be_lun->dev_path, M_CTLBLK); 2573267877Smav mtx_destroy(&be_lun->queue_lock); 2574267877Smav mtx_destroy(&be_lun->io_lock); 2575229997Sken free(be_lun, M_CTLBLK); 2576229997Sken 2577229997Sken req->status = CTL_LUN_OK; 2578229997Sken 2579229997Sken return (0); 2580229997Sken 2581229997Skenbailout_error: 2582229997Sken 2583229997Sken req->status = CTL_LUN_ERROR; 2584229997Sken 2585229997Sken return (0); 2586229997Sken} 2587229997Sken 2588232604Straszstatic int 2589232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2590232604Strasz struct ctl_lun_req *req) 2591232604Strasz{ 2592287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2593232604Strasz struct vattr vattr; 2594232604Strasz int error; 2595272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2596232604Strasz 2597232604Strasz if (params->lun_size_bytes != 0) { 2598232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2599232604Strasz } else { 2600271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2601232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2602271794Smav VOP_UNLOCK(be_lun->vn, 0); 2603232604Strasz if (error != 0) { 2604232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2605232604Strasz "error calling VOP_GETATTR() for file %s", 2606232604Strasz be_lun->dev_path); 2607232604Strasz return (error); 2608232604Strasz } 2609232604Strasz be_lun->size_bytes = vattr.va_size; 2610232604Strasz } 2611287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2612287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2613287499Smav 0 : (be_lun->size_blocks - 1); 2614232604Strasz return (0); 2615232604Strasz} 2616232604Strasz 2617232604Straszstatic int 2618232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2619232604Strasz struct ctl_lun_req *req) 2620232604Strasz{ 2621287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2622272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2623287664Smav struct cdevsw *csw; 2624287664Smav struct cdev *dev; 2625232604Strasz uint64_t size_bytes; 2626287664Smav int error, ref; 2627232604Strasz 2628287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 2629287664Smav if (csw == NULL) 2630287664Smav return (ENXIO); 2631287664Smav if (csw->d_ioctl == NULL) { 2632287664Smav dev_relthread(dev, ref); 2633232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2634272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2635232604Strasz return (ENODEV); 2636232604Strasz } 2637232604Strasz 2638287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&size_bytes, FREAD, 2639287664Smav curthread); 2640287664Smav dev_relthread(dev, ref); 2641232604Strasz if (error) { 2642232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2643272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2644272911Smav "on %s!", error, be_lun->dev_path); 2645232604Strasz return (error); 2646232604Strasz } 2647232604Strasz 2648232604Strasz if (params->lun_size_bytes != 0) { 2649232604Strasz if (params->lun_size_bytes > size_bytes) { 2650232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2651272911Smav "requested LUN size %ju > backing device " 2652272911Smav "size %ju", 2653232604Strasz (uintmax_t)params->lun_size_bytes, 2654232604Strasz (uintmax_t)size_bytes); 2655232604Strasz return (EINVAL); 2656232604Strasz } 2657232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2658232604Strasz } else { 2659232604Strasz be_lun->size_bytes = size_bytes; 2660232604Strasz } 2661287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2662287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2663287499Smav 0 : (be_lun->size_blocks - 1); 2664232604Strasz return (0); 2665232604Strasz} 2666232604Strasz 2667232604Straszstatic int 2668232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2669232604Strasz{ 2670232604Strasz struct ctl_lun_modify_params *params; 2671232604Strasz struct ctl_be_block_lun *be_lun; 2672287500Smav struct ctl_be_lun *cbe_lun; 2673287621Smav char *value; 2674271794Smav uint64_t oldsize; 2675287621Smav int error, wasprim; 2676232604Strasz 2677232604Strasz params = &req->reqdata.modify; 2678232604Strasz 2679232604Strasz mtx_lock(&softc->lock); 2680232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2681287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2682232604Strasz break; 2683232604Strasz } 2684232604Strasz mtx_unlock(&softc->lock); 2685232604Strasz 2686232604Strasz if (be_lun == NULL) { 2687232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2688272911Smav "LUN %u is not managed by the block backend", 2689272911Smav params->lun_id); 2690232604Strasz goto bailout_error; 2691232604Strasz } 2692287500Smav cbe_lun = &be_lun->cbe_lun; 2693232604Strasz 2694287500Smav if (params->lun_size_bytes != 0) 2695287500Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2696287500Smav ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2697232604Strasz 2698287621Smav wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2699287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2700287621Smav if (value != NULL) { 2701287621Smav if (strcmp(value, "primary") == 0) 2702287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2703287621Smav else 2704287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2705287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2706287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2707232604Strasz else 2708287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2709287621Smav if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 2710287621Smav if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 2711287621Smav ctl_lun_primary(cbe_lun); 2712287621Smav else 2713287621Smav ctl_lun_secondary(cbe_lun); 2714287621Smav } 2715232604Strasz 2716287621Smav oldsize = be_lun->size_blocks; 2717287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2718287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2719287621Smav if (be_lun->vn == NULL) 2720287621Smav error = ctl_be_block_open(softc, be_lun, req); 2721287621Smav else if (vn_isdisk(be_lun->vn, &error)) 2722287621Smav error = ctl_be_block_modify_dev(be_lun, req); 2723287621Smav else if (be_lun->vn->v_type == VREG) 2724287621Smav error = ctl_be_block_modify_file(be_lun, req); 2725287621Smav else 2726287621Smav error = EINVAL; 2727287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) && 2728287621Smav be_lun->vn != NULL) { 2729287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE; 2730287621Smav ctl_lun_online(cbe_lun); 2731287621Smav } 2732287621Smav } else { 2733287621Smav if (be_lun->vn != NULL) { 2734287621Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2735287621Smav ctl_lun_offline(cbe_lun); 2736287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2737287621Smav error = ctl_be_block_close(be_lun); 2738287621Smav } else 2739287621Smav error = 0; 2740287621Smav } 2741287499Smav if (be_lun->size_blocks != oldsize) 2742287500Smav ctl_lun_capacity_changed(cbe_lun); 2743232604Strasz 2744232604Strasz /* Tell the user the exact size we ended up using */ 2745232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2746232604Strasz 2747272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2748232604Strasz return (0); 2749232604Strasz 2750232604Straszbailout_error: 2751232604Strasz req->status = CTL_LUN_ERROR; 2752232604Strasz return (0); 2753232604Strasz} 2754232604Strasz 2755229997Skenstatic void 2756229997Skenctl_be_block_lun_shutdown(void *be_lun) 2757229997Sken{ 2758229997Sken struct ctl_be_block_lun *lun; 2759229997Sken struct ctl_be_block_softc *softc; 2760229997Sken 2761229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2762229997Sken 2763229997Sken softc = lun->softc; 2764229997Sken 2765229997Sken mtx_lock(&softc->lock); 2766229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2767229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2768229997Sken wakeup(lun); 2769229997Sken mtx_unlock(&softc->lock); 2770229997Sken 2771229997Sken} 2772229997Sken 2773229997Skenstatic void 2774229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2775229997Sken{ 2776229997Sken struct ctl_be_block_lun *lun; 2777229997Sken struct ctl_be_block_softc *softc; 2778229997Sken 2779229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2780229997Sken softc = lun->softc; 2781229997Sken 2782229997Sken if (status == CTL_LUN_CONFIG_OK) { 2783229997Sken mtx_lock(&softc->lock); 2784229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2785229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2786229997Sken wakeup(lun); 2787229997Sken mtx_unlock(&softc->lock); 2788229997Sken 2789229997Sken /* 2790229997Sken * We successfully added the LUN, attempt to enable it. 2791229997Sken */ 2792287499Smav if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2793229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2794287499Smav if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2795229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2796229997Sken __func__); 2797229997Sken } 2798229997Sken } 2799229997Sken 2800229997Sken return; 2801229997Sken } 2802229997Sken 2803229997Sken 2804229997Sken mtx_lock(&softc->lock); 2805229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2806229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2807229997Sken wakeup(lun); 2808229997Sken mtx_unlock(&softc->lock); 2809229997Sken} 2810229997Sken 2811229997Sken 2812229997Skenstatic int 2813229997Skenctl_be_block_config_write(union ctl_io *io) 2814229997Sken{ 2815229997Sken struct ctl_be_block_lun *be_lun; 2816287499Smav struct ctl_be_lun *cbe_lun; 2817229997Sken int retval; 2818229997Sken 2819229997Sken retval = 0; 2820229997Sken 2821229997Sken DPRINTF("entered\n"); 2822229997Sken 2823287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2824229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2825287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2826229997Sken 2827229997Sken switch (io->scsiio.cdb[0]) { 2828229997Sken case SYNCHRONIZE_CACHE: 2829229997Sken case SYNCHRONIZE_CACHE_16: 2830264274Smav case WRITE_SAME_10: 2831264274Smav case WRITE_SAME_16: 2832264274Smav case UNMAP: 2833229997Sken /* 2834229997Sken * The upper level CTL code will filter out any CDBs with 2835229997Sken * the immediate bit set and return the proper error. 2836229997Sken * 2837229997Sken * We don't really need to worry about what LBA range the 2838229997Sken * user asked to be synced out. When they issue a sync 2839229997Sken * cache command, we'll sync out the whole thing. 2840229997Sken */ 2841267877Smav mtx_lock(&be_lun->queue_lock); 2842229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2843229997Sken links); 2844267877Smav mtx_unlock(&be_lun->queue_lock); 2845229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2846229997Sken break; 2847229997Sken case START_STOP_UNIT: { 2848229997Sken struct scsi_start_stop_unit *cdb; 2849229997Sken 2850229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2851229997Sken 2852229997Sken if (cdb->how & SSS_START) 2853287499Smav retval = ctl_start_lun(cbe_lun); 2854229997Sken else { 2855287499Smav retval = ctl_stop_lun(cbe_lun); 2856229997Sken /* 2857229997Sken * XXX KDM Copan-specific offline behavior. 2858229997Sken * Figure out a reasonable way to port this? 2859229997Sken */ 2860229997Sken#ifdef NEEDTOPORT 2861229997Sken if ((retval == 0) 2862229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2863287499Smav retval = ctl_lun_offline(cbe_lun); 2864229997Sken#endif 2865229997Sken } 2866229997Sken 2867229997Sken /* 2868229997Sken * In general, the above routines should not fail. They 2869229997Sken * just set state for the LUN. So we've got something 2870229997Sken * pretty wrong here if we can't start or stop the LUN. 2871229997Sken */ 2872229997Sken if (retval != 0) { 2873229997Sken ctl_set_internal_failure(&io->scsiio, 2874229997Sken /*sks_valid*/ 1, 2875229997Sken /*retry_count*/ 0xf051); 2876229997Sken retval = CTL_RETVAL_COMPLETE; 2877229997Sken } else { 2878229997Sken ctl_set_success(&io->scsiio); 2879229997Sken } 2880229997Sken ctl_config_write_done(io); 2881229997Sken break; 2882229997Sken } 2883229997Sken default: 2884229997Sken ctl_set_invalid_opcode(&io->scsiio); 2885229997Sken ctl_config_write_done(io); 2886229997Sken retval = CTL_RETVAL_COMPLETE; 2887229997Sken break; 2888229997Sken } 2889229997Sken 2890229997Sken return (retval); 2891229997Sken} 2892229997Sken 2893229997Skenstatic int 2894229997Skenctl_be_block_config_read(union ctl_io *io) 2895229997Sken{ 2896275474Smav struct ctl_be_block_lun *be_lun; 2897287499Smav struct ctl_be_lun *cbe_lun; 2898275474Smav int retval = 0; 2899275474Smav 2900275474Smav DPRINTF("entered\n"); 2901275474Smav 2902287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2903275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2904287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2905275474Smav 2906275474Smav switch (io->scsiio.cdb[0]) { 2907275474Smav case SERVICE_ACTION_IN: 2908275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2909275474Smav mtx_lock(&be_lun->queue_lock); 2910275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2911275474Smav &io->io_hdr, links); 2912275474Smav mtx_unlock(&be_lun->queue_lock); 2913275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2914275474Smav &be_lun->io_task); 2915275474Smav retval = CTL_RETVAL_QUEUED; 2916275474Smav break; 2917275474Smav } 2918275474Smav ctl_set_invalid_field(&io->scsiio, 2919275474Smav /*sks_valid*/ 1, 2920275474Smav /*command*/ 1, 2921275474Smav /*field*/ 1, 2922275474Smav /*bit_valid*/ 1, 2923275474Smav /*bit*/ 4); 2924275474Smav ctl_config_read_done(io); 2925275474Smav retval = CTL_RETVAL_COMPLETE; 2926275474Smav break; 2927275474Smav default: 2928275474Smav ctl_set_invalid_opcode(&io->scsiio); 2929275474Smav ctl_config_read_done(io); 2930275474Smav retval = CTL_RETVAL_COMPLETE; 2931275474Smav break; 2932275474Smav } 2933275474Smav 2934275474Smav return (retval); 2935229997Sken} 2936229997Sken 2937229997Skenstatic int 2938229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2939229997Sken{ 2940229997Sken struct ctl_be_block_lun *lun; 2941229997Sken int retval; 2942229997Sken 2943229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2944229997Sken retval = 0; 2945229997Sken 2946268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2947229997Sken 2948229997Sken if (retval != 0) 2949229997Sken goto bailout; 2950229997Sken 2951229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2952229997Sken 2953229997Sken if (retval != 0) 2954229997Sken goto bailout; 2955229997Sken 2956268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2957229997Sken 2958229997Skenbailout: 2959229997Sken 2960229997Sken return (retval); 2961229997Sken} 2962229997Sken 2963274154Smavstatic uint64_t 2964274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2965274154Smav{ 2966274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2967274154Smav 2968274154Smav if (lun->getattr == NULL) 2969274154Smav return (UINT64_MAX); 2970274154Smav return (lun->getattr(lun, attrname)); 2971274154Smav} 2972274154Smav 2973229997Skenint 2974229997Skenctl_be_block_init(void) 2975229997Sken{ 2976229997Sken struct ctl_be_block_softc *softc; 2977229997Sken int retval; 2978229997Sken 2979229997Sken softc = &backend_block_softc; 2980229997Sken retval = 0; 2981229997Sken 2982267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2983264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2984264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2985229997Sken STAILQ_INIT(&softc->lun_list); 2986229997Sken 2987229997Sken return (retval); 2988229997Sken} 2989