ctl_backend_block.c revision 288215
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 288215 2015-09-25 10:14:39Z 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(struct ctl_be_block_softc *softc, 267232604Strasz struct ctl_lun_req *req); 268229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 269229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 270229997Sken ctl_lun_config_status status); 271229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 272229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 273229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 274274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 275229997Skenint ctl_be_block_init(void); 276229997Sken 277229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 278229997Sken{ 279230334Sken .name = "block", 280230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 281230334Sken .init = ctl_be_block_init, 282230334Sken .data_submit = ctl_be_block_submit, 283230334Sken .data_move_done = ctl_be_block_move_done, 284230334Sken .config_read = ctl_be_block_config_read, 285230334Sken .config_write = ctl_be_block_config_write, 286230334Sken .ioctl = ctl_be_block_ioctl, 287274154Smav .lun_info = ctl_be_block_lun_info, 288274154Smav .lun_attr = ctl_be_block_lun_attr 289229997Sken}; 290229997Sken 291229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 292229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 293229997Sken 294264020Straszstatic uma_zone_t beio_zone; 295264020Strasz 296229997Skenstatic struct ctl_be_block_io * 297229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 298229997Sken{ 299229997Sken struct ctl_be_block_io *beio; 300229997Sken 301264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 302264020Strasz beio->softc = softc; 303229997Sken return (beio); 304229997Sken} 305229997Sken 306229997Skenstatic void 307229997Skenctl_free_beio(struct ctl_be_block_io *beio) 308229997Sken{ 309229997Sken int duplicate_free; 310229997Sken int i; 311229997Sken 312229997Sken duplicate_free = 0; 313229997Sken 314229997Sken for (i = 0; i < beio->num_segs; i++) { 315229997Sken if (beio->sg_segs[i].addr == NULL) 316229997Sken duplicate_free++; 317229997Sken 318229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 319229997Sken beio->sg_segs[i].addr = NULL; 320267537Smav 321267537Smav /* For compare we had two equal S/G lists. */ 322267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 323267537Smav uma_zfree(beio->lun->lun_zone, 324267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 325267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 326267537Smav } 327229997Sken } 328229997Sken 329229997Sken if (duplicate_free > 0) { 330229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 331229997Sken duplicate_free, beio->num_segs); 332229997Sken } 333229997Sken 334264020Strasz uma_zfree(beio_zone, beio); 335229997Sken} 336229997Sken 337229997Skenstatic void 338229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 339229997Sken{ 340267877Smav union ctl_io *io = beio->io; 341229997Sken 342264274Smav if (beio->beio_cont != NULL) { 343264274Smav beio->beio_cont(beio); 344264274Smav } else { 345264274Smav ctl_free_beio(beio); 346267537Smav ctl_data_submit_done(io); 347264274Smav } 348229997Sken} 349229997Sken 350287868Smavstatic size_t 351287868Smavcmp(uint8_t *a, uint8_t *b, size_t size) 352287868Smav{ 353287868Smav size_t i; 354287868Smav 355287868Smav for (i = 0; i < size; i++) { 356287868Smav if (a[i] != b[i]) 357287868Smav break; 358287868Smav } 359287868Smav return (i); 360287868Smav} 361287868Smav 362287868Smavstatic void 363287868Smavctl_be_block_compare(union ctl_io *io) 364287868Smav{ 365287868Smav struct ctl_be_block_io *beio; 366287868Smav uint64_t off, res; 367287868Smav int i; 368287868Smav uint8_t info[8]; 369287868Smav 370287868Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 371287868Smav off = 0; 372287868Smav for (i = 0; i < beio->num_segs; i++) { 373287868Smav res = cmp(beio->sg_segs[i].addr, 374287868Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 375287868Smav beio->sg_segs[i].len); 376287868Smav off += res; 377287868Smav if (res < beio->sg_segs[i].len) 378287868Smav break; 379287868Smav } 380287868Smav if (i < beio->num_segs) { 381287868Smav scsi_u64to8b(off, info); 382287868Smav ctl_set_sense(&io->scsiio, /*current_error*/ 1, 383287868Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 384287868Smav /*asc*/ 0x1D, /*ascq*/ 0x00, 385287868Smav /*type*/ SSD_ELEM_INFO, 386287868Smav /*size*/ sizeof(info), /*data*/ &info, 387287868Smav /*type*/ SSD_ELEM_NONE); 388287868Smav } else 389287868Smav ctl_set_success(&io->scsiio); 390287868Smav} 391287868Smav 392229997Skenstatic int 393229997Skenctl_be_block_move_done(union ctl_io *io) 394229997Sken{ 395229997Sken struct ctl_be_block_io *beio; 396229997Sken struct ctl_be_block_lun *be_lun; 397267537Smav struct ctl_lba_len_flags *lbalen; 398229997Sken#ifdef CTL_TIME_IO 399229997Sken struct bintime cur_bt; 400267537Smav#endif 401229997Sken 402267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 403229997Sken be_lun = beio->lun; 404229997Sken 405229997Sken DPRINTF("entered\n"); 406229997Sken 407229997Sken#ifdef CTL_TIME_IO 408288215Smav getbinuptime(&cur_bt); 409229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 410229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 411288215Smav#endif 412229997Sken io->io_hdr.num_dmas++; 413267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 414229997Sken 415229997Sken /* 416229997Sken * We set status at this point for read commands, and write 417229997Sken * commands with errors. 418229997Sken */ 419275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 420275058Smav ; 421275058Smav } else if ((io->io_hdr.port_status == 0) && 422267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 423267537Smav lbalen = ARGS(beio->io); 424267537Smav if (lbalen->flags & CTL_LLF_READ) { 425267537Smav ctl_set_success(&io->scsiio); 426267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 427267537Smav /* We have two data blocks ready for comparison. */ 428287868Smav ctl_be_block_compare(io); 429267537Smav } 430275058Smav } else if ((io->io_hdr.port_status != 0) && 431275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 432275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 433229997Sken /* 434229997Sken * For hardware error sense keys, the sense key 435229997Sken * specific value is defined to be a retry count, 436229997Sken * but we use it to pass back an internal FETD 437229997Sken * error code. XXX KDM Hopefully the FETD is only 438229997Sken * using 16 bits for an error code, since that's 439229997Sken * all the space we have in the sks field. 440229997Sken */ 441229997Sken ctl_set_internal_failure(&io->scsiio, 442229997Sken /*sks_valid*/ 1, 443229997Sken /*retry_count*/ 444229997Sken io->io_hdr.port_status); 445229997Sken } 446229997Sken 447229997Sken /* 448229997Sken * If this is a read, or a write with errors, it is done. 449229997Sken */ 450229997Sken if ((beio->bio_cmd == BIO_READ) 451229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 452229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 453229997Sken ctl_complete_beio(beio); 454229997Sken return (0); 455229997Sken } 456229997Sken 457229997Sken /* 458229997Sken * At this point, we have a write and the DMA completed 459229997Sken * successfully. We now have to queue it to the task queue to 460229997Sken * execute the backend I/O. That is because we do blocking 461229997Sken * memory allocations, and in the file backing case, blocking I/O. 462229997Sken * This move done routine is generally called in the SIM's 463229997Sken * interrupt context, and therefore we cannot block. 464229997Sken */ 465267877Smav mtx_lock(&be_lun->queue_lock); 466229997Sken /* 467229997Sken * XXX KDM make sure that links is okay to use at this point. 468229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 469229997Sken * or deal with resource allocation here. 470229997Sken */ 471229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 472267877Smav mtx_unlock(&be_lun->queue_lock); 473229997Sken 474229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 475229997Sken 476229997Sken return (0); 477229997Sken} 478229997Sken 479229997Skenstatic void 480229997Skenctl_be_block_biodone(struct bio *bio) 481229997Sken{ 482229997Sken struct ctl_be_block_io *beio; 483229997Sken struct ctl_be_block_lun *be_lun; 484229997Sken union ctl_io *io; 485261538Smav int error; 486229997Sken 487229997Sken beio = bio->bio_caller1; 488229997Sken be_lun = beio->lun; 489229997Sken io = beio->io; 490229997Sken 491229997Sken DPRINTF("entered\n"); 492229997Sken 493261538Smav error = bio->bio_error; 494267877Smav mtx_lock(&be_lun->io_lock); 495261538Smav if (error != 0) 496229997Sken beio->num_errors++; 497229997Sken 498229997Sken beio->num_bios_done++; 499229997Sken 500229997Sken /* 501229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 502229997Sken * during the free might cause it to complain. 503229997Sken */ 504229997Sken g_destroy_bio(bio); 505229997Sken 506229997Sken /* 507229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 508229997Sken * complete, then we're done. 509229997Sken */ 510229997Sken if ((beio->send_complete == 0) 511229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 512267877Smav mtx_unlock(&be_lun->io_lock); 513229997Sken return; 514229997Sken } 515229997Sken 516229997Sken /* 517229997Sken * At this point, we've verified that we are the last I/O to 518229997Sken * complete, so it's safe to drop the lock. 519229997Sken */ 520267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 521267877Smav beio->ds_tag_type, beio->ds_trans_type, 522267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 523267877Smav mtx_unlock(&be_lun->io_lock); 524229997Sken 525229997Sken /* 526229997Sken * If there are any errors from the backing device, we fail the 527229997Sken * entire I/O with a medium error. 528229997Sken */ 529229997Sken if (beio->num_errors > 0) { 530261538Smav if (error == EOPNOTSUPP) { 531261538Smav ctl_set_invalid_opcode(&io->scsiio); 532282565Smav } else if (error == ENOSPC || error == EDQUOT) { 533273809Smav ctl_set_space_alloc_fail(&io->scsiio); 534287760Smav } else if (error == EROFS || error == EACCES) { 535287760Smav ctl_set_hw_write_protected(&io->scsiio); 536261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 537229997Sken /* XXX KDM is there is a better error here? */ 538229997Sken ctl_set_internal_failure(&io->scsiio, 539229997Sken /*sks_valid*/ 1, 540229997Sken /*retry_count*/ 0xbad2); 541287912Smav } else { 542287912Smav ctl_set_medium_error(&io->scsiio, 543287912Smav beio->bio_cmd == BIO_READ); 544287912Smav } 545229997Sken ctl_complete_beio(beio); 546229997Sken return; 547229997Sken } 548229997Sken 549229997Sken /* 550267537Smav * If this is a write, a flush, a delete or verify, we're all done. 551229997Sken * If this is a read, we can now send the data to the user. 552229997Sken */ 553229997Sken if ((beio->bio_cmd == BIO_WRITE) 554264274Smav || (beio->bio_cmd == BIO_FLUSH) 555267537Smav || (beio->bio_cmd == BIO_DELETE) 556267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 557229997Sken ctl_set_success(&io->scsiio); 558229997Sken ctl_complete_beio(beio); 559229997Sken } else { 560275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 561287967Smav beio->beio_cont == NULL) { 562275058Smav ctl_set_success(&io->scsiio); 563287967Smav ctl_serseq_done(io); 564287967Smav } 565229997Sken#ifdef CTL_TIME_IO 566288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 567288215Smav#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) { 761282565Smav if (error == ENOSPC || error == EDQUOT) { 762273809Smav ctl_set_space_alloc_fail(&io->scsiio); 763287760Smav } else if (error == EROFS || error == EACCES) { 764287760Smav ctl_set_hw_write_protected(&io->scsiio); 765287912Smav } else { 766287912Smav ctl_set_medium_error(&io->scsiio, 767287912Smav beio->bio_cmd == BIO_READ); 768287912Smav } 769229997Sken ctl_complete_beio(beio); 770229997Sken return; 771229997Sken } 772229997Sken 773229997Sken /* 774269122Smav * If this is a write or a verify, we're all done. 775229997Sken * If this is a read, we can now send the data to the user. 776229997Sken */ 777269122Smav if ((beio->bio_cmd == BIO_WRITE) || 778269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 779229997Sken ctl_set_success(&io->scsiio); 780229997Sken ctl_complete_beio(beio); 781229997Sken } else { 782275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 783287967Smav beio->beio_cont == NULL) { 784275058Smav ctl_set_success(&io->scsiio); 785287967Smav ctl_serseq_done(io); 786287967Smav } 787229997Sken#ifdef CTL_TIME_IO 788288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 789288215Smav#endif 790229997Sken ctl_datamove(io); 791229997Sken } 792229997Sken} 793229997Sken 794229997Skenstatic void 795275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 796275474Smav struct ctl_be_block_io *beio) 797275474Smav{ 798275474Smav union ctl_io *io = beio->io; 799275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 800275474Smav struct scsi_get_lba_status_data *data; 801275474Smav off_t roff, off; 802275474Smav int error, status; 803275474Smav 804275474Smav DPRINTF("entered\n"); 805275474Smav 806287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 807275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 808275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 809275474Smav 0, curthread->td_ucred, curthread); 810275474Smav if (error == 0 && off > roff) 811275474Smav status = 0; /* mapped up to off */ 812275474Smav else { 813275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 814275474Smav 0, curthread->td_ucred, curthread); 815275474Smav if (error == 0 && off > roff) 816275474Smav status = 1; /* deallocated up to off */ 817275474Smav else { 818275474Smav status = 0; /* unknown up to the end */ 819275474Smav off = be_lun->size_bytes; 820275474Smav } 821275474Smav } 822275474Smav VOP_UNLOCK(be_lun->vn, 0); 823275474Smav 824275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 825275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 826287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 827287499Smav lbalen->lba), data->descr[0].length); 828275474Smav data->descr[0].status = status; 829275474Smav 830275474Smav ctl_complete_beio(beio); 831275474Smav} 832275474Smav 833275481Smavstatic uint64_t 834275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 835275481Smav{ 836275481Smav struct vattr vattr; 837275481Smav struct statfs statfs; 838285030Smav uint64_t val; 839275481Smav int error; 840275481Smav 841285030Smav val = UINT64_MAX; 842275481Smav if (be_lun->vn == NULL) 843285030Smav return (val); 844285030Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 845275481Smav if (strcmp(attrname, "blocksused") == 0) { 846275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 847285030Smav if (error == 0) 848287499Smav val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 849275481Smav } 850285030Smav if (strcmp(attrname, "blocksavail") == 0 && 851285030Smav (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 852275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 853285030Smav if (error == 0) 854286811Smav val = statfs.f_bavail * statfs.f_bsize / 855287499Smav be_lun->cbe_lun.blocksize; 856275481Smav } 857285030Smav VOP_UNLOCK(be_lun->vn, 0); 858285030Smav return (val); 859275481Smav} 860275481Smav 861275474Smavstatic void 862269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 863269123Smav struct ctl_be_block_io *beio) 864269123Smav{ 865269123Smav union ctl_io *io; 866287664Smav struct cdevsw *csw; 867287664Smav struct cdev *dev; 868269123Smav struct uio xuio; 869269123Smav struct iovec *xiovec; 870287664Smav int error, flags, i, ref; 871269123Smav 872269123Smav DPRINTF("entered\n"); 873269123Smav 874269123Smav io = beio->io; 875271309Smav flags = 0; 876271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 877271309Smav flags |= IO_DIRECT; 878271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 879271309Smav flags |= IO_SYNC; 880269123Smav 881269123Smav bzero(&xuio, sizeof(xuio)); 882269123Smav if (beio->bio_cmd == BIO_READ) { 883269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 884269123Smav xuio.uio_rw = UIO_READ; 885269123Smav } else { 886269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 887269123Smav xuio.uio_rw = UIO_WRITE; 888269123Smav } 889269123Smav xuio.uio_offset = beio->io_offset; 890269123Smav xuio.uio_resid = beio->io_len; 891269123Smav xuio.uio_segflg = UIO_SYSSPACE; 892269123Smav xuio.uio_iov = beio->xiovecs; 893269123Smav xuio.uio_iovcnt = beio->num_segs; 894269123Smav xuio.uio_td = curthread; 895269123Smav 896269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 897269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 898269123Smav xiovec->iov_len = beio->sg_segs[i].len; 899269123Smav } 900269123Smav 901269123Smav binuptime(&beio->ds_t0); 902269123Smav mtx_lock(&be_lun->io_lock); 903269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 904269123Smav mtx_unlock(&be_lun->io_lock); 905269123Smav 906287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 907287664Smav if (csw) { 908287664Smav if (beio->bio_cmd == BIO_READ) 909287664Smav error = csw->d_read(dev, &xuio, flags); 910287664Smav else 911287664Smav error = csw->d_write(dev, &xuio, flags); 912287664Smav dev_relthread(dev, ref); 913287664Smav } else 914287664Smav error = ENXIO; 915287664Smav 916287664Smav if (beio->bio_cmd == BIO_READ) 917269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 918287664Smav else 919269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 920269123Smav 921269123Smav mtx_lock(&be_lun->io_lock); 922269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 923269123Smav beio->ds_tag_type, beio->ds_trans_type, 924269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 925269123Smav mtx_unlock(&be_lun->io_lock); 926269123Smav 927269123Smav /* 928269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 929269123Smav * return the I/O to the user. 930269123Smav */ 931269123Smav if (error != 0) { 932282565Smav if (error == ENOSPC || error == EDQUOT) { 933273809Smav ctl_set_space_alloc_fail(&io->scsiio); 934287760Smav } else if (error == EROFS || error == EACCES) { 935287760Smav ctl_set_hw_write_protected(&io->scsiio); 936287912Smav } else { 937287912Smav ctl_set_medium_error(&io->scsiio, 938287912Smav beio->bio_cmd == BIO_READ); 939287912Smav } 940269123Smav ctl_complete_beio(beio); 941269123Smav return; 942269123Smav } 943269123Smav 944269123Smav /* 945269123Smav * If this is a write or a verify, we're all done. 946269123Smav * If this is a read, we can now send the data to the user. 947269123Smav */ 948269123Smav if ((beio->bio_cmd == BIO_WRITE) || 949269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 950269123Smav ctl_set_success(&io->scsiio); 951269123Smav ctl_complete_beio(beio); 952269123Smav } else { 953275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 954287967Smav beio->beio_cont == NULL) { 955275058Smav ctl_set_success(&io->scsiio); 956287967Smav ctl_serseq_done(io); 957287967Smav } 958269123Smav#ifdef CTL_TIME_IO 959288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 960288215Smav#endif 961269123Smav ctl_datamove(io); 962269123Smav } 963269123Smav} 964269123Smav 965269123Smavstatic void 966275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 967275474Smav struct ctl_be_block_io *beio) 968275474Smav{ 969275474Smav union ctl_io *io = beio->io; 970287664Smav struct cdevsw *csw; 971287664Smav struct cdev *dev; 972275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 973275474Smav struct scsi_get_lba_status_data *data; 974275474Smav off_t roff, off; 975287664Smav int error, ref, status; 976275474Smav 977275474Smav DPRINTF("entered\n"); 978275474Smav 979287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 980287664Smav if (csw == NULL) { 981287664Smav status = 0; /* unknown up to the end */ 982287664Smav off = be_lun->size_bytes; 983287664Smav goto done; 984287664Smav } 985287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 986287664Smav error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 987287664Smav curthread); 988275474Smav if (error == 0 && off > roff) 989275474Smav status = 0; /* mapped up to off */ 990275474Smav else { 991287664Smav error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 992287664Smav curthread); 993275474Smav if (error == 0 && off > roff) 994275474Smav status = 1; /* deallocated up to off */ 995275474Smav else { 996275474Smav status = 0; /* unknown up to the end */ 997275474Smav off = be_lun->size_bytes; 998275474Smav } 999275474Smav } 1000287664Smav dev_relthread(dev, ref); 1001275474Smav 1002287664Smavdone: 1003275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 1004275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 1005287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 1006287499Smav lbalen->lba), data->descr[0].length); 1007275474Smav data->descr[0].status = status; 1008275474Smav 1009275474Smav ctl_complete_beio(beio); 1010275474Smav} 1011275474Smav 1012275474Smavstatic void 1013229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 1014229997Sken struct ctl_be_block_io *beio) 1015229997Sken{ 1016229997Sken struct bio *bio; 1017229997Sken union ctl_io *io; 1018287664Smav struct cdevsw *csw; 1019287664Smav struct cdev *dev; 1020287664Smav int ref; 1021229997Sken 1022229997Sken io = beio->io; 1023229997Sken 1024229997Sken DPRINTF("entered\n"); 1025229997Sken 1026229997Sken /* This can't fail, it's a blocking allocation. */ 1027229997Sken bio = g_alloc_bio(); 1028229997Sken 1029229997Sken bio->bio_cmd = BIO_FLUSH; 1030229997Sken bio->bio_offset = 0; 1031229997Sken bio->bio_data = 0; 1032229997Sken bio->bio_done = ctl_be_block_biodone; 1033229997Sken bio->bio_caller1 = beio; 1034229997Sken bio->bio_pblkno = 0; 1035229997Sken 1036229997Sken /* 1037229997Sken * We don't need to acquire the LUN lock here, because we are only 1038229997Sken * sending one bio, and so there is no other context to synchronize 1039229997Sken * with. 1040229997Sken */ 1041229997Sken beio->num_bios_sent = 1; 1042229997Sken beio->send_complete = 1; 1043229997Sken 1044229997Sken binuptime(&beio->ds_t0); 1045267877Smav mtx_lock(&be_lun->io_lock); 1046229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1047267877Smav mtx_unlock(&be_lun->io_lock); 1048229997Sken 1049287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1050287664Smav if (csw) { 1051287664Smav bio->bio_dev = dev; 1052287664Smav csw->d_strategy(bio); 1053287664Smav dev_relthread(dev, ref); 1054287664Smav } else { 1055287664Smav bio->bio_error = ENXIO; 1056287664Smav ctl_be_block_biodone(bio); 1057287664Smav } 1058229997Sken} 1059229997Sken 1060229997Skenstatic void 1061264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1062264274Smav struct ctl_be_block_io *beio, 1063264274Smav uint64_t off, uint64_t len, int last) 1064264274Smav{ 1065264274Smav struct bio *bio; 1066264296Smav uint64_t maxlen; 1067287664Smav struct cdevsw *csw; 1068287664Smav struct cdev *dev; 1069287664Smav int ref; 1070264274Smav 1071287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1072287499Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1073264274Smav while (len > 0) { 1074264274Smav bio = g_alloc_bio(); 1075264274Smav bio->bio_cmd = BIO_DELETE; 1076287664Smav bio->bio_dev = dev; 1077264274Smav bio->bio_offset = off; 1078264296Smav bio->bio_length = MIN(len, maxlen); 1079264274Smav bio->bio_data = 0; 1080264274Smav bio->bio_done = ctl_be_block_biodone; 1081264274Smav bio->bio_caller1 = beio; 1082287499Smav bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1083264274Smav 1084264274Smav off += bio->bio_length; 1085264274Smav len -= bio->bio_length; 1086264274Smav 1087267877Smav mtx_lock(&be_lun->io_lock); 1088264274Smav beio->num_bios_sent++; 1089264274Smav if (last && len == 0) 1090264274Smav beio->send_complete = 1; 1091267877Smav mtx_unlock(&be_lun->io_lock); 1092264274Smav 1093287664Smav if (csw) { 1094287664Smav csw->d_strategy(bio); 1095287664Smav } else { 1096287664Smav bio->bio_error = ENXIO; 1097287664Smav ctl_be_block_biodone(bio); 1098287664Smav } 1099264274Smav } 1100287664Smav if (csw) 1101287664Smav dev_relthread(dev, ref); 1102264274Smav} 1103264274Smav 1104264274Smavstatic void 1105264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1106264274Smav struct ctl_be_block_io *beio) 1107264274Smav{ 1108264274Smav union ctl_io *io; 1109267515Smav struct ctl_ptr_len_flags *ptrlen; 1110264274Smav struct scsi_unmap_desc *buf, *end; 1111264274Smav uint64_t len; 1112264274Smav 1113264274Smav io = beio->io; 1114264274Smav 1115264274Smav DPRINTF("entered\n"); 1116264274Smav 1117264274Smav binuptime(&beio->ds_t0); 1118267877Smav mtx_lock(&be_lun->io_lock); 1119264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1120267877Smav mtx_unlock(&be_lun->io_lock); 1121264274Smav 1122264274Smav if (beio->io_offset == -1) { 1123264274Smav beio->io_len = 0; 1124267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1125267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1126267515Smav end = buf + ptrlen->len / sizeof(*buf); 1127264274Smav for (; buf < end; buf++) { 1128264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1129287499Smav be_lun->cbe_lun.blocksize; 1130264274Smav beio->io_len += len; 1131264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1132287499Smav scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 1133287499Smav len, (end - buf < 2) ? TRUE : FALSE); 1134264274Smav } 1135264274Smav } else 1136264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1137264274Smav beio->io_offset, beio->io_len, TRUE); 1138264274Smav} 1139264274Smav 1140264274Smavstatic void 1141229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1142229997Sken struct ctl_be_block_io *beio) 1143229997Sken{ 1144267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1145229997Sken struct bio *bio; 1146287664Smav struct cdevsw *csw; 1147287664Smav struct cdev *dev; 1148229997Sken off_t cur_offset; 1149287664Smav int i, max_iosize, ref; 1150229997Sken 1151229997Sken DPRINTF("entered\n"); 1152287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1153229997Sken 1154229997Sken /* 1155229997Sken * We have to limit our I/O size to the maximum supported by the 1156229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1157229997Sken * set it properly, use DFLTPHYS. 1158229997Sken */ 1159287664Smav if (csw) { 1160287664Smav max_iosize = dev->si_iosize_max; 1161287664Smav if (max_iosize < PAGE_SIZE) 1162287664Smav max_iosize = DFLTPHYS; 1163287664Smav } else 1164229997Sken max_iosize = DFLTPHYS; 1165229997Sken 1166229997Sken cur_offset = beio->io_offset; 1167229997Sken for (i = 0; i < beio->num_segs; i++) { 1168229997Sken size_t cur_size; 1169229997Sken uint8_t *cur_ptr; 1170229997Sken 1171229997Sken cur_size = beio->sg_segs[i].len; 1172229997Sken cur_ptr = beio->sg_segs[i].addr; 1173229997Sken 1174229997Sken while (cur_size > 0) { 1175229997Sken /* This can't fail, it's a blocking allocation. */ 1176229997Sken bio = g_alloc_bio(); 1177229997Sken 1178229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1179229997Sken 1180229997Sken bio->bio_cmd = beio->bio_cmd; 1181287664Smav bio->bio_dev = dev; 1182229997Sken bio->bio_caller1 = beio; 1183229997Sken bio->bio_length = min(cur_size, max_iosize); 1184229997Sken bio->bio_offset = cur_offset; 1185229997Sken bio->bio_data = cur_ptr; 1186229997Sken bio->bio_done = ctl_be_block_biodone; 1187287499Smav bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1188229997Sken 1189229997Sken cur_offset += bio->bio_length; 1190229997Sken cur_ptr += bio->bio_length; 1191229997Sken cur_size -= bio->bio_length; 1192229997Sken 1193267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1194229997Sken beio->num_bios_sent++; 1195229997Sken } 1196229997Sken } 1197267877Smav binuptime(&beio->ds_t0); 1198267877Smav mtx_lock(&be_lun->io_lock); 1199267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1200267877Smav beio->send_complete = 1; 1201267877Smav mtx_unlock(&be_lun->io_lock); 1202267877Smav 1203267877Smav /* 1204267877Smav * Fire off all allocated requests! 1205267877Smav */ 1206267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1207267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1208287664Smav if (csw) 1209287664Smav csw->d_strategy(bio); 1210287664Smav else { 1211287664Smav bio->bio_error = ENXIO; 1212287664Smav ctl_be_block_biodone(bio); 1213287664Smav } 1214267877Smav } 1215287664Smav if (csw) 1216287664Smav dev_relthread(dev, ref); 1217229997Sken} 1218229997Sken 1219274154Smavstatic uint64_t 1220274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1221274154Smav{ 1222274154Smav struct diocgattr_arg arg; 1223287664Smav struct cdevsw *csw; 1224287664Smav struct cdev *dev; 1225287664Smav int error, ref; 1226274154Smav 1227287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1228287664Smav if (csw == NULL) 1229274154Smav return (UINT64_MAX); 1230274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1231274154Smav arg.len = sizeof(arg.value.off); 1232287664Smav if (csw->d_ioctl) { 1233287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 1234287664Smav curthread); 1235287664Smav } else 1236287664Smav error = ENODEV; 1237287664Smav dev_relthread(dev, ref); 1238274154Smav if (error != 0) 1239274154Smav return (UINT64_MAX); 1240274154Smav return (arg.value.off); 1241274154Smav} 1242274154Smav 1243229997Skenstatic void 1244286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1245286353Smav union ctl_io *io) 1246286353Smav{ 1247287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1248286353Smav struct ctl_be_block_io *beio; 1249286353Smav struct ctl_lba_len_flags *lbalen; 1250286353Smav 1251286353Smav DPRINTF("entered\n"); 1252286353Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1253286353Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1254286353Smav 1255287499Smav beio->io_len = lbalen->len * cbe_lun->blocksize; 1256287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1257286353Smav beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1258286353Smav beio->bio_cmd = BIO_FLUSH; 1259286353Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1260286353Smav DPRINTF("SYNC\n"); 1261286353Smav be_lun->lun_flush(be_lun, beio); 1262286353Smav} 1263286353Smav 1264286353Smavstatic void 1265264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1266264274Smav{ 1267264274Smav union ctl_io *io; 1268264274Smav 1269264274Smav io = beio->io; 1270264274Smav ctl_free_beio(beio); 1271267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1272267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1273267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1274264274Smav ctl_config_write_done(io); 1275264274Smav return; 1276264274Smav } 1277264274Smav 1278264274Smav ctl_be_block_config_write(io); 1279264274Smav} 1280264274Smav 1281264274Smavstatic void 1282264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1283264274Smav union ctl_io *io) 1284264274Smav{ 1285287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1286264274Smav struct ctl_be_block_io *beio; 1287267515Smav struct ctl_lba_len_flags *lbalen; 1288278625Smav uint64_t len_left, lba; 1289278625Smav uint32_t pb, pbo, adj; 1290264274Smav int i, seglen; 1291264274Smav uint8_t *buf, *end; 1292264274Smav 1293264274Smav DPRINTF("entered\n"); 1294264274Smav 1295267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1296267537Smav lbalen = ARGS(beio->io); 1297264274Smav 1298271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1299269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1300264274Smav ctl_free_beio(beio); 1301264274Smav ctl_set_invalid_field(&io->scsiio, 1302264274Smav /*sks_valid*/ 1, 1303264274Smav /*command*/ 1, 1304264274Smav /*field*/ 1, 1305264274Smav /*bit_valid*/ 0, 1306264274Smav /*bit*/ 0); 1307264274Smav ctl_config_write_done(io); 1308264274Smav return; 1309264274Smav } 1310264274Smav 1311269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1312287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1313287499Smav beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1314264274Smav beio->bio_cmd = BIO_DELETE; 1315264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1316264274Smav 1317264274Smav be_lun->unmap(be_lun, beio); 1318264274Smav return; 1319264274Smav } 1320264274Smav 1321264274Smav beio->bio_cmd = BIO_WRITE; 1322264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1323264274Smav 1324264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1325267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1326264274Smav 1327287499Smav pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1328287499Smav if (be_lun->cbe_lun.pblockoff > 0) 1329287499Smav pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1330278625Smav else 1331278625Smav pbo = 0; 1332287499Smav len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1333264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1334264274Smav 1335264274Smav /* 1336264274Smav * Setup the S/G entry for this chunk. 1337264274Smav */ 1338264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1339287499Smav if (pb > cbe_lun->blocksize) { 1340287499Smav adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1341278619Smav seglen - pbo) % pb; 1342278619Smav if (seglen > adj) 1343278619Smav seglen -= adj; 1344278619Smav else 1345287499Smav seglen -= seglen % cbe_lun->blocksize; 1346278619Smav } else 1347287499Smav seglen -= seglen % cbe_lun->blocksize; 1348264274Smav beio->sg_segs[i].len = seglen; 1349264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1350264274Smav 1351264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1352264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1353264274Smav 1354264274Smav beio->num_segs++; 1355264274Smav len_left -= seglen; 1356264274Smav 1357264274Smav buf = beio->sg_segs[i].addr; 1358264274Smav end = buf + seglen; 1359287499Smav for (; buf < end; buf += cbe_lun->blocksize) { 1360288175Smav if (lbalen->flags & SWS_NDOB) { 1361288175Smav memset(buf, 0, cbe_lun->blocksize); 1362288175Smav } else { 1363288175Smav memcpy(buf, io->scsiio.kern_data_ptr, 1364288175Smav cbe_lun->blocksize); 1365288175Smav } 1366267515Smav if (lbalen->flags & SWS_LBDATA) 1367267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1368264274Smav lba++; 1369264274Smav } 1370264274Smav } 1371264274Smav 1372287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1373287499Smav beio->io_len = lba * cbe_lun->blocksize; 1374264274Smav 1375264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1376264274Smav if (len_left > 0) { 1377267515Smav lbalen->lba += lba; 1378267515Smav lbalen->len -= lba; 1379264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1380264274Smav } 1381264274Smav 1382264274Smav be_lun->dispatch(be_lun, beio); 1383264274Smav} 1384264274Smav 1385264274Smavstatic void 1386264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1387264274Smav union ctl_io *io) 1388264274Smav{ 1389264274Smav struct ctl_be_block_io *beio; 1390267515Smav struct ctl_ptr_len_flags *ptrlen; 1391264274Smav 1392264274Smav DPRINTF("entered\n"); 1393264274Smav 1394267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1395267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1396264274Smav 1397269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1398264274Smav ctl_free_beio(beio); 1399264274Smav ctl_set_invalid_field(&io->scsiio, 1400264274Smav /*sks_valid*/ 0, 1401264274Smav /*command*/ 1, 1402264274Smav /*field*/ 0, 1403264274Smav /*bit_valid*/ 0, 1404264274Smav /*bit*/ 0); 1405264274Smav ctl_config_write_done(io); 1406264274Smav return; 1407264274Smav } 1408264274Smav 1409264274Smav beio->io_len = 0; 1410264274Smav beio->io_offset = -1; 1411264274Smav beio->bio_cmd = BIO_DELETE; 1412264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1413267515Smav DPRINTF("UNMAP\n"); 1414264274Smav be_lun->unmap(be_lun, beio); 1415264274Smav} 1416264274Smav 1417264274Smavstatic void 1418275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1419275474Smav{ 1420275474Smav union ctl_io *io; 1421275474Smav 1422275474Smav io = beio->io; 1423275474Smav ctl_free_beio(beio); 1424275474Smav ctl_config_read_done(io); 1425275474Smav} 1426275474Smav 1427275474Smavstatic void 1428275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1429275474Smav union ctl_io *io) 1430275474Smav{ 1431275474Smav struct ctl_be_block_io *beio; 1432275474Smav struct ctl_be_block_softc *softc; 1433275474Smav 1434275474Smav DPRINTF("entered\n"); 1435275474Smav 1436275474Smav softc = be_lun->softc; 1437275474Smav beio = ctl_alloc_beio(softc); 1438275474Smav beio->io = io; 1439275474Smav beio->lun = be_lun; 1440275474Smav beio->beio_cont = ctl_be_block_cr_done; 1441275474Smav PRIV(io)->ptr = (void *)beio; 1442275474Smav 1443275474Smav switch (io->scsiio.cdb[0]) { 1444275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1445275474Smav beio->bio_cmd = -1; 1446275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1447275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1448275474Smav beio->io_len = 0; 1449275474Smav if (be_lun->get_lba_status) 1450275474Smav be_lun->get_lba_status(be_lun, beio); 1451275474Smav else 1452275474Smav ctl_be_block_cr_done(beio); 1453275474Smav break; 1454275474Smav default: 1455275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1456275474Smav break; 1457275474Smav } 1458275474Smav} 1459275474Smav 1460275474Smavstatic void 1461264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1462264274Smav{ 1463264274Smav union ctl_io *io; 1464264274Smav 1465264274Smav io = beio->io; 1466264274Smav ctl_free_beio(beio); 1467264274Smav ctl_config_write_done(io); 1468264274Smav} 1469264274Smav 1470264274Smavstatic void 1471229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1472229997Sken union ctl_io *io) 1473229997Sken{ 1474229997Sken struct ctl_be_block_io *beio; 1475229997Sken struct ctl_be_block_softc *softc; 1476229997Sken 1477229997Sken DPRINTF("entered\n"); 1478229997Sken 1479229997Sken softc = be_lun->softc; 1480229997Sken beio = ctl_alloc_beio(softc); 1481229997Sken beio->io = io; 1482229997Sken beio->lun = be_lun; 1483264274Smav beio->beio_cont = ctl_be_block_cw_done; 1484286353Smav switch (io->scsiio.tag_type) { 1485286353Smav case CTL_TAG_ORDERED: 1486286353Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1487286353Smav break; 1488286353Smav case CTL_TAG_HEAD_OF_QUEUE: 1489286353Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1490286353Smav break; 1491286353Smav case CTL_TAG_UNTAGGED: 1492286353Smav case CTL_TAG_SIMPLE: 1493286353Smav case CTL_TAG_ACA: 1494286353Smav default: 1495286353Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1496286353Smav break; 1497286353Smav } 1498267519Smav PRIV(io)->ptr = (void *)beio; 1499229997Sken 1500229997Sken switch (io->scsiio.cdb[0]) { 1501229997Sken case SYNCHRONIZE_CACHE: 1502229997Sken case SYNCHRONIZE_CACHE_16: 1503286353Smav ctl_be_block_cw_dispatch_sync(be_lun, io); 1504229997Sken break; 1505264274Smav case WRITE_SAME_10: 1506264274Smav case WRITE_SAME_16: 1507264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1508264274Smav break; 1509264274Smav case UNMAP: 1510264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1511264274Smav break; 1512229997Sken default: 1513229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1514229997Sken break; 1515229997Sken } 1516229997Sken} 1517229997Sken 1518258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1519258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1520258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1521258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1522229997Sken 1523229997Skenstatic void 1524264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1525264886Smav{ 1526264886Smav struct ctl_be_block_lun *be_lun; 1527264886Smav union ctl_io *io; 1528264886Smav 1529264886Smav io = beio->io; 1530264886Smav be_lun = beio->lun; 1531264886Smav ctl_free_beio(beio); 1532267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1533267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1534267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1535267537Smav ctl_data_submit_done(io); 1536264886Smav return; 1537264886Smav } 1538264886Smav 1539264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1540264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1541264886Smav 1542267877Smav mtx_lock(&be_lun->queue_lock); 1543264886Smav /* 1544264886Smav * XXX KDM make sure that links is okay to use at this point. 1545264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1546264886Smav * or deal with resource allocation here. 1547264886Smav */ 1548264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1549267877Smav mtx_unlock(&be_lun->queue_lock); 1550264886Smav 1551264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1552264886Smav} 1553264886Smav 1554264886Smavstatic void 1555229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1556229997Sken union ctl_io *io) 1557229997Sken{ 1558287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1559229997Sken struct ctl_be_block_io *beio; 1560229997Sken struct ctl_be_block_softc *softc; 1561267537Smav struct ctl_lba_len_flags *lbalen; 1562267519Smav struct ctl_ptr_len_flags *bptrlen; 1563267519Smav uint64_t len_left, lbas; 1564229997Sken int i; 1565229997Sken 1566229997Sken softc = be_lun->softc; 1567229997Sken 1568229997Sken DPRINTF("entered\n"); 1569229997Sken 1570267537Smav lbalen = ARGS(io); 1571267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1572267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1573267537Smav } else { 1574229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1575229997Sken } 1576229997Sken 1577229997Sken beio = ctl_alloc_beio(softc); 1578229997Sken beio->io = io; 1579229997Sken beio->lun = be_lun; 1580267519Smav bptrlen = PRIV(io); 1581267519Smav bptrlen->ptr = (void *)beio; 1582229997Sken 1583229997Sken switch (io->scsiio.tag_type) { 1584229997Sken case CTL_TAG_ORDERED: 1585229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1586229997Sken break; 1587229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1588229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1589229997Sken break; 1590229997Sken case CTL_TAG_UNTAGGED: 1591229997Sken case CTL_TAG_SIMPLE: 1592229997Sken case CTL_TAG_ACA: 1593229997Sken default: 1594229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1595229997Sken break; 1596229997Sken } 1597229997Sken 1598267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1599267537Smav beio->bio_cmd = BIO_WRITE; 1600267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1601267537Smav } else { 1602229997Sken beio->bio_cmd = BIO_READ; 1603229997Sken beio->ds_trans_type = DEVSTAT_READ; 1604229997Sken } 1605229997Sken 1606264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1607229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1608267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1609267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1610267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1611267537Smav else 1612267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1613287499Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 1614287499Smav beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 1615287499Smav beio->io_len = lbas * cbe_lun->blocksize; 1616267519Smav bptrlen->len += lbas; 1617229997Sken 1618264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1619264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1620264886Smav i, CTLBLK_MAX_SEGS)); 1621229997Sken 1622229997Sken /* 1623229997Sken * Setup the S/G entry for this chunk. 1624229997Sken */ 1625264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1626229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1627229997Sken 1628229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1629229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1630229997Sken 1631267537Smav /* Set up second segment for compare operation. */ 1632267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1633267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1634267537Smav beio->sg_segs[i].len; 1635267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1636267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1637267537Smav } 1638267537Smav 1639229997Sken beio->num_segs++; 1640229997Sken len_left -= beio->sg_segs[i].len; 1641229997Sken } 1642267519Smav if (bptrlen->len < lbalen->len) 1643264886Smav beio->beio_cont = ctl_be_block_next; 1644264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1645267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1646267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1647267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1648267537Smav else 1649267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1650264886Smav io->scsiio.kern_data_len = beio->io_len; 1651264886Smav io->scsiio.kern_data_resid = 0; 1652264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1653288020Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 1654229997Sken 1655229997Sken /* 1656229997Sken * For the read case, we need to read the data into our buffers and 1657229997Sken * then we can send it back to the user. For the write case, we 1658229997Sken * need to get the data from the user first. 1659229997Sken */ 1660229997Sken if (beio->bio_cmd == BIO_READ) { 1661229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1662229997Sken be_lun->dispatch(be_lun, beio); 1663229997Sken } else { 1664229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1665229997Sken#ifdef CTL_TIME_IO 1666288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 1667288215Smav#endif 1668229997Sken ctl_datamove(io); 1669229997Sken } 1670229997Sken} 1671229997Sken 1672229997Skenstatic void 1673229997Skenctl_be_block_worker(void *context, int pending) 1674229997Sken{ 1675287670Smav struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1676287670Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1677229997Sken union ctl_io *io; 1678287670Smav struct ctl_be_block_io *beio; 1679229997Sken 1680229997Sken DPRINTF("entered\n"); 1681287670Smav /* 1682287670Smav * Fetch and process I/Os from all queues. If we detect LUN 1683287670Smav * CTL_LUN_FLAG_OFFLINE status here -- it is result of a race, 1684287670Smav * so make response maximally opaque to not confuse initiator. 1685287670Smav */ 1686229997Sken for (;;) { 1687287670Smav mtx_lock(&be_lun->queue_lock); 1688229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1689229997Sken if (io != NULL) { 1690229997Sken DPRINTF("datamove queue\n"); 1691229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1692229997Sken ctl_io_hdr, links); 1693267877Smav mtx_unlock(&be_lun->queue_lock); 1694267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1695287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1696287670Smav ctl_set_busy(&io->scsiio); 1697287670Smav ctl_complete_beio(beio); 1698287670Smav return; 1699287670Smav } 1700229997Sken be_lun->dispatch(be_lun, beio); 1701229997Sken continue; 1702229997Sken } 1703229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1704229997Sken if (io != NULL) { 1705229997Sken DPRINTF("config write queue\n"); 1706229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1707229997Sken ctl_io_hdr, links); 1708267877Smav mtx_unlock(&be_lun->queue_lock); 1709287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1710287670Smav ctl_set_busy(&io->scsiio); 1711287670Smav ctl_config_write_done(io); 1712287670Smav return; 1713287670Smav } 1714229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1715229997Sken continue; 1716229997Sken } 1717275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1718275474Smav if (io != NULL) { 1719275474Smav DPRINTF("config read queue\n"); 1720275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1721275474Smav ctl_io_hdr, links); 1722275474Smav mtx_unlock(&be_lun->queue_lock); 1723287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1724287670Smav ctl_set_busy(&io->scsiio); 1725287670Smav ctl_config_read_done(io); 1726287670Smav return; 1727287670Smav } 1728275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1729275474Smav continue; 1730275474Smav } 1731229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1732229997Sken if (io != NULL) { 1733229997Sken DPRINTF("input queue\n"); 1734229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1735229997Sken ctl_io_hdr, links); 1736267877Smav mtx_unlock(&be_lun->queue_lock); 1737287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1738287670Smav ctl_set_busy(&io->scsiio); 1739287670Smav ctl_data_submit_done(io); 1740287670Smav return; 1741287670Smav } 1742229997Sken ctl_be_block_dispatch(be_lun, io); 1743229997Sken continue; 1744229997Sken } 1745229997Sken 1746229997Sken /* 1747229997Sken * If we get here, there is no work left in the queues, so 1748229997Sken * just break out and let the task queue go to sleep. 1749229997Sken */ 1750287670Smav mtx_unlock(&be_lun->queue_lock); 1751229997Sken break; 1752229997Sken } 1753229997Sken} 1754229997Sken 1755229997Sken/* 1756229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1757229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1758229997Sken * thread. 1759229997Sken */ 1760229997Skenstatic int 1761229997Skenctl_be_block_submit(union ctl_io *io) 1762229997Sken{ 1763229997Sken struct ctl_be_block_lun *be_lun; 1764287499Smav struct ctl_be_lun *cbe_lun; 1765229997Sken 1766229997Sken DPRINTF("entered\n"); 1767229997Sken 1768287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1769229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1770287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1771229997Sken 1772229997Sken /* 1773229997Sken * Make sure we only get SCSI I/O. 1774229997Sken */ 1775229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1776229997Sken "%#x) encountered", io->io_hdr.io_type)); 1777229997Sken 1778267519Smav PRIV(io)->len = 0; 1779267519Smav 1780267877Smav mtx_lock(&be_lun->queue_lock); 1781229997Sken /* 1782229997Sken * XXX KDM make sure that links is okay to use at this point. 1783229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1784229997Sken * or deal with resource allocation here. 1785229997Sken */ 1786229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1787267877Smav mtx_unlock(&be_lun->queue_lock); 1788229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1789229997Sken 1790267514Smav return (CTL_RETVAL_COMPLETE); 1791229997Sken} 1792229997Sken 1793229997Skenstatic int 1794229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1795229997Sken int flag, struct thread *td) 1796229997Sken{ 1797229997Sken struct ctl_be_block_softc *softc; 1798229997Sken int error; 1799229997Sken 1800229997Sken softc = &backend_block_softc; 1801229997Sken 1802229997Sken error = 0; 1803229997Sken 1804229997Sken switch (cmd) { 1805229997Sken case CTL_LUN_REQ: { 1806229997Sken struct ctl_lun_req *lun_req; 1807229997Sken 1808229997Sken lun_req = (struct ctl_lun_req *)addr; 1809229997Sken 1810229997Sken switch (lun_req->reqtype) { 1811229997Sken case CTL_LUNREQ_CREATE: 1812229997Sken error = ctl_be_block_create(softc, lun_req); 1813229997Sken break; 1814229997Sken case CTL_LUNREQ_RM: 1815229997Sken error = ctl_be_block_rm(softc, lun_req); 1816229997Sken break; 1817232604Strasz case CTL_LUNREQ_MODIFY: 1818232604Strasz error = ctl_be_block_modify(softc, lun_req); 1819232604Strasz break; 1820229997Sken default: 1821229997Sken lun_req->status = CTL_LUN_ERROR; 1822229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1823272911Smav "invalid LUN request type %d", 1824229997Sken lun_req->reqtype); 1825229997Sken break; 1826229997Sken } 1827229997Sken break; 1828229997Sken } 1829229997Sken default: 1830229997Sken error = ENOTTY; 1831229997Sken break; 1832229997Sken } 1833229997Sken 1834229997Sken return (error); 1835229997Sken} 1836229997Sken 1837229997Skenstatic int 1838229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1839229997Sken{ 1840287499Smav struct ctl_be_lun *cbe_lun; 1841229997Sken struct ctl_be_block_filedata *file_data; 1842229997Sken struct ctl_lun_create_params *params; 1843275865Smav char *value; 1844229997Sken struct vattr vattr; 1845275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1846229997Sken int error; 1847229997Sken 1848229997Sken error = 0; 1849287499Smav cbe_lun = &be_lun->cbe_lun; 1850229997Sken file_data = &be_lun->backend.file; 1851272911Smav params = &be_lun->params; 1852229997Sken 1853229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1854229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1855229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1856275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1857275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1858287499Smav be_lun->unmap = NULL; 1859287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1860229997Sken 1861229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1862229997Sken if (error != 0) { 1863229997Sken snprintf(req->error_str, sizeof(req->error_str), 1864229997Sken "error calling VOP_GETATTR() for file %s", 1865229997Sken be_lun->dev_path); 1866229997Sken return (error); 1867229997Sken } 1868229997Sken 1869229997Sken /* 1870229997Sken * Verify that we have the ability to upgrade to exclusive 1871229997Sken * access on this file so we can trap errors at open instead 1872229997Sken * of reporting them during first access. 1873229997Sken */ 1874229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1875229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1876229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1877229997Sken error = EBADF; 1878229997Sken snprintf(req->error_str, sizeof(req->error_str), 1879229997Sken "error locking file %s", be_lun->dev_path); 1880229997Sken return (error); 1881229997Sken } 1882229997Sken } 1883229997Sken 1884229997Sken file_data->cred = crhold(curthread->td_ucred); 1885232604Strasz if (params->lun_size_bytes != 0) 1886232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1887232604Strasz else 1888232604Strasz be_lun->size_bytes = vattr.va_size; 1889229997Sken 1890229997Sken /* 1891273029Smav * For files we can use any logical block size. Prefer 512 bytes 1892273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1893273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1894273029Smav * logical block size -- report it as physical block size. 1895229997Sken */ 1896229997Sken if (params->blocksize_bytes != 0) 1897287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1898229997Sken else 1899287499Smav cbe_lun->blocksize = 512; 1900287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1901287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1902287499Smav 0 : (be_lun->size_blocks - 1); 1903275865Smav 1904275865Smav us = ps = vattr.va_blocksize; 1905275865Smav uo = po = 0; 1906275865Smav 1907287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1908275865Smav if (value != NULL) 1909275865Smav ctl_expand_number(value, &ps); 1910287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1911275865Smav if (value != NULL) 1912275865Smav ctl_expand_number(value, &po); 1913287499Smav pss = ps / cbe_lun->blocksize; 1914287499Smav pos = po / cbe_lun->blocksize; 1915287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1916287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1917287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1918287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1919273029Smav } 1920229997Sken 1921287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1922275865Smav if (value != NULL) 1923275865Smav ctl_expand_number(value, &us); 1924287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1925275865Smav if (value != NULL) 1926275865Smav ctl_expand_number(value, &uo); 1927287499Smav uss = us / cbe_lun->blocksize; 1928287499Smav uos = uo / cbe_lun->blocksize; 1929287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1930287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1931287499Smav cbe_lun->ublockexp = fls(uss) - 1; 1932287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 1933275865Smav } 1934275865Smav 1935229997Sken /* 1936229997Sken * Sanity check. The media size has to be at least one 1937229997Sken * sector long. 1938229997Sken */ 1939287499Smav if (be_lun->size_bytes < cbe_lun->blocksize) { 1940229997Sken error = EINVAL; 1941229997Sken snprintf(req->error_str, sizeof(req->error_str), 1942229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1943287499Smav (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1944229997Sken } 1945275920Smav 1946287499Smav cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1947229997Sken return (error); 1948229997Sken} 1949229997Sken 1950229997Skenstatic int 1951229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1952229997Sken{ 1953287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1954229997Sken struct ctl_lun_create_params *params; 1955287664Smav struct cdevsw *csw; 1956229997Sken struct cdev *dev; 1957275865Smav char *value; 1958287664Smav int error, atomic, maxio, ref, unmap, tmp; 1959287221Smav off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1960229997Sken 1961272911Smav params = &be_lun->params; 1962229997Sken 1963229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1964287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1965287664Smav if (csw == NULL) 1966287664Smav return (ENXIO); 1967287664Smav if (strcmp(csw->d_name, "zvol") == 0) { 1968269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1969275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1970275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1971275920Smav } else { 1972269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1973287499Smav be_lun->get_lba_status = NULL; 1974275920Smav atomic = 0; 1975287664Smav maxio = dev->si_iosize_max; 1976275920Smav if (maxio <= 0) 1977275920Smav maxio = DFLTPHYS; 1978275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1979275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1980275920Smav } 1981269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1982274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1983287499Smav be_lun->unmap = ctl_be_block_unmap_dev; 1984229997Sken 1985287664Smav if (!csw->d_ioctl) { 1986287664Smav dev_relthread(dev, ref); 1987229997Sken snprintf(req->error_str, sizeof(req->error_str), 1988287664Smav "no d_ioctl for device %s!", be_lun->dev_path); 1989229997Sken return (ENODEV); 1990229997Sken } 1991229997Sken 1992287664Smav error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1993229997Sken curthread); 1994229997Sken if (error) { 1995287664Smav dev_relthread(dev, ref); 1996229997Sken snprintf(req->error_str, sizeof(req->error_str), 1997272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1998272911Smav "on %s!", error, be_lun->dev_path); 1999229997Sken return (error); 2000229997Sken } 2001229997Sken 2002229997Sken /* 2003229997Sken * If the user has asked for a blocksize that is greater than the 2004229997Sken * backing device's blocksize, we can do it only if the blocksize 2005229997Sken * the user is asking for is an even multiple of the underlying 2006229997Sken * device's blocksize. 2007229997Sken */ 2008286811Smav if ((params->blocksize_bytes != 0) && 2009286811Smav (params->blocksize_bytes >= tmp)) { 2010286811Smav if (params->blocksize_bytes % tmp == 0) { 2011287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2012229997Sken } else { 2013287664Smav dev_relthread(dev, ref); 2014229997Sken snprintf(req->error_str, sizeof(req->error_str), 2015272911Smav "requested blocksize %u is not an even " 2016229997Sken "multiple of backing device blocksize %u", 2017287221Smav params->blocksize_bytes, tmp); 2018229997Sken return (EINVAL); 2019229997Sken } 2020286811Smav } else if (params->blocksize_bytes != 0) { 2021287664Smav dev_relthread(dev, ref); 2022229997Sken snprintf(req->error_str, sizeof(req->error_str), 2023272911Smav "requested blocksize %u < backing device " 2024287221Smav "blocksize %u", params->blocksize_bytes, tmp); 2025229997Sken return (EINVAL); 2026286811Smav } else 2027287499Smav cbe_lun->blocksize = tmp; 2028229997Sken 2029287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 2030287664Smav curthread); 2031229997Sken if (error) { 2032287664Smav dev_relthread(dev, ref); 2033229997Sken snprintf(req->error_str, sizeof(req->error_str), 2034272911Smav "error %d returned for DIOCGMEDIASIZE " 2035272911Smav " ioctl on %s!", error, 2036232604Strasz be_lun->dev_path); 2037229997Sken return (error); 2038229997Sken } 2039229997Sken 2040232604Strasz if (params->lun_size_bytes != 0) { 2041287221Smav if (params->lun_size_bytes > otmp) { 2042287664Smav dev_relthread(dev, ref); 2043232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2044272911Smav "requested LUN size %ju > backing device " 2045272911Smav "size %ju", 2046232604Strasz (uintmax_t)params->lun_size_bytes, 2047287221Smav (uintmax_t)otmp); 2048232604Strasz return (EINVAL); 2049232604Strasz } 2050232604Strasz 2051232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2052286811Smav } else 2053287221Smav be_lun->size_bytes = otmp; 2054287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2055287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2056287499Smav 0 : (be_lun->size_blocks - 1); 2057232604Strasz 2058287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 2059287664Smav curthread); 2060264191Smav if (error) 2061264191Smav ps = po = 0; 2062264191Smav else { 2063287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2064287664Smav FREAD, curthread); 2065264191Smav if (error) 2066264191Smav po = 0; 2067264191Smav } 2068275865Smav us = ps; 2069275865Smav uo = po; 2070275865Smav 2071287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 2072275865Smav if (value != NULL) 2073275865Smav ctl_expand_number(value, &ps); 2074287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 2075275865Smav if (value != NULL) 2076275865Smav ctl_expand_number(value, &po); 2077287499Smav pss = ps / cbe_lun->blocksize; 2078287499Smav pos = po / cbe_lun->blocksize; 2079287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2080287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2081287499Smav cbe_lun->pblockexp = fls(pss) - 1; 2082287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 2083264191Smav } 2084264191Smav 2085287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 2086275865Smav if (value != NULL) 2087275865Smav ctl_expand_number(value, &us); 2088287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 2089275865Smav if (value != NULL) 2090275865Smav ctl_expand_number(value, &uo); 2091287499Smav uss = us / cbe_lun->blocksize; 2092287499Smav uos = uo / cbe_lun->blocksize; 2093287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2094287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2095287499Smav cbe_lun->ublockexp = fls(uss) - 1; 2096287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 2097275865Smav } 2098275865Smav 2099287499Smav cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2100287499Smav cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2101278672Smav 2102278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2103278672Smav unmap = 1; 2104278672Smav } else { 2105278672Smav struct diocgattr_arg arg; 2106278672Smav 2107278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2108278672Smav arg.len = sizeof(arg.value.i); 2109287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2110287664Smav curthread); 2111278672Smav unmap = (error == 0) ? arg.value.i : 0; 2112278672Smav } 2113287499Smav value = ctl_get_opt(&cbe_lun->options, "unmap"); 2114278672Smav if (value != NULL) 2115278672Smav unmap = (strcmp(value, "on") == 0); 2116278672Smav if (unmap) 2117287499Smav cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2118287499Smav else 2119287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2120278672Smav 2121287664Smav dev_relthread(dev, ref); 2122229997Sken return (0); 2123229997Sken} 2124229997Sken 2125229997Skenstatic int 2126229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2127229997Sken{ 2128287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2129287499Smav int flags; 2130287499Smav 2131229997Sken if (be_lun->vn) { 2132287499Smav flags = FREAD; 2133287499Smav if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2134287499Smav flags |= FWRITE; 2135229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2136229997Sken be_lun->vn = NULL; 2137229997Sken 2138229997Sken switch (be_lun->dev_type) { 2139229997Sken case CTL_BE_BLOCK_DEV: 2140229997Sken break; 2141229997Sken case CTL_BE_BLOCK_FILE: 2142229997Sken if (be_lun->backend.file.cred != NULL) { 2143229997Sken crfree(be_lun->backend.file.cred); 2144229997Sken be_lun->backend.file.cred = NULL; 2145229997Sken } 2146229997Sken break; 2147229997Sken case CTL_BE_BLOCK_NONE: 2148258871Strasz break; 2149229997Sken default: 2150229997Sken panic("Unexpected backend type."); 2151229997Sken break; 2152229997Sken } 2153272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2154229997Sken } 2155229997Sken return (0); 2156229997Sken} 2157229997Sken 2158229997Skenstatic int 2159229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 2160287499Smav struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2161229997Sken{ 2162287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2163229997Sken struct nameidata nd; 2164287499Smav char *value; 2165287499Smav int error, flags; 2166229997Sken 2167229997Sken error = 0; 2168229997Sken if (rootvnode == NULL) { 2169229997Sken snprintf(req->error_str, sizeof(req->error_str), 2170272911Smav "Root filesystem is not mounted"); 2171229997Sken return (1); 2172229997Sken } 2173285391Smjg pwd_ensure_dirs(); 2174229997Sken 2175287499Smav value = ctl_get_opt(&cbe_lun->options, "file"); 2176287499Smav if (value == NULL) { 2177287499Smav snprintf(req->error_str, sizeof(req->error_str), 2178287499Smav "no file argument specified"); 2179287499Smav return (1); 2180287499Smav } 2181287499Smav free(be_lun->dev_path, M_CTLBLK); 2182287499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2183287499Smav 2184287499Smav flags = FREAD; 2185287499Smav value = ctl_get_opt(&cbe_lun->options, "readonly"); 2186287499Smav if (value == NULL || strcmp(value, "on") != 0) 2187287499Smav flags |= FWRITE; 2188287499Smav 2189287499Smavagain: 2190229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2191229997Sken error = vn_open(&nd, &flags, 0, NULL); 2192287499Smav if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2193287499Smav flags &= ~FWRITE; 2194287499Smav goto again; 2195287499Smav } 2196229997Sken if (error) { 2197229997Sken /* 2198229997Sken * This is the only reasonable guess we can make as far as 2199229997Sken * path if the user doesn't give us a fully qualified path. 2200229997Sken * If they want to specify a file, they need to specify the 2201229997Sken * full path. 2202229997Sken */ 2203229997Sken if (be_lun->dev_path[0] != '/') { 2204229997Sken char *dev_name; 2205229997Sken 2206287499Smav asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2207287499Smav be_lun->dev_path); 2208287499Smav free(be_lun->dev_path, M_CTLBLK); 2209287499Smav be_lun->dev_path = dev_name; 2210287499Smav goto again; 2211229997Sken } 2212229997Sken snprintf(req->error_str, sizeof(req->error_str), 2213272911Smav "error opening %s: %d", be_lun->dev_path, error); 2214229997Sken return (error); 2215229997Sken } 2216287499Smav if (flags & FWRITE) 2217287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2218287499Smav else 2219287499Smav cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2220229997Sken 2221229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2222229997Sken be_lun->vn = nd.ni_vp; 2223229997Sken 2224229997Sken /* We only support disks and files. */ 2225229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2226229997Sken error = ctl_be_block_open_dev(be_lun, req); 2227229997Sken } else if (be_lun->vn->v_type == VREG) { 2228229997Sken error = ctl_be_block_open_file(be_lun, req); 2229229997Sken } else { 2230229997Sken error = EINVAL; 2231229997Sken snprintf(req->error_str, sizeof(req->error_str), 2232258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2233229997Sken } 2234229997Sken VOP_UNLOCK(be_lun->vn, 0); 2235229997Sken 2236286811Smav if (error != 0) 2237229997Sken ctl_be_block_close(be_lun); 2238287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2239287499Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2240287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2241287499Smav value = ctl_get_opt(&cbe_lun->options, "serseq"); 2242287499Smav if (value != NULL && strcmp(value, "on") == 0) 2243287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2244287499Smav else if (value != NULL && strcmp(value, "read") == 0) 2245287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2246287499Smav else if (value != NULL && strcmp(value, "off") == 0) 2247287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2248229997Sken return (0); 2249229997Sken} 2250229997Sken 2251229997Skenstatic int 2252229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2253229997Sken{ 2254287499Smav struct ctl_be_lun *cbe_lun; 2255229997Sken struct ctl_be_block_lun *be_lun; 2256229997Sken struct ctl_lun_create_params *params; 2257267481Smav char num_thread_str[16]; 2258229997Sken char tmpstr[32]; 2259267481Smav char *value; 2260278672Smav int retval, num_threads; 2261267481Smav int tmp_num_threads; 2262229997Sken 2263229997Sken params = &req->reqdata.create; 2264229997Sken retval = 0; 2265272911Smav req->status = CTL_LUN_OK; 2266229997Sken 2267229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2268287499Smav cbe_lun = &be_lun->cbe_lun; 2269287499Smav cbe_lun->be_lun = be_lun; 2270272911Smav be_lun->params = req->reqdata.create; 2271229997Sken be_lun->softc = softc; 2272229997Sken STAILQ_INIT(&be_lun->input_queue); 2273275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2274229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2275229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2276229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2277267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2278267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2279287499Smav ctl_init_opts(&cbe_lun->options, 2280268280Smav req->num_be_args, req->kern_be_args); 2281264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2282256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2283229997Sken if (be_lun->lun_zone == NULL) { 2284229997Sken snprintf(req->error_str, sizeof(req->error_str), 2285272911Smav "error allocating UMA zone"); 2286229997Sken goto bailout_error; 2287229997Sken } 2288229997Sken 2289229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2290287499Smav cbe_lun->lun_type = params->device_type; 2291229997Sken else 2292287499Smav cbe_lun->lun_type = T_DIRECT; 2293287499Smav be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2294287621Smav cbe_lun->flags = 0; 2295287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2296287621Smav if (value != NULL) { 2297287621Smav if (strcmp(value, "primary") == 0) 2298287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2299287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2300287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2301229997Sken 2302287499Smav if (cbe_lun->lun_type == T_DIRECT) { 2303286811Smav be_lun->size_bytes = params->lun_size_bytes; 2304286811Smav if (params->blocksize_bytes != 0) 2305287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2306286811Smav else 2307287499Smav cbe_lun->blocksize = 512; 2308287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2309287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2310287499Smav 0 : (be_lun->size_blocks - 1); 2311229997Sken 2312287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2313287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2314287621Smav retval = ctl_be_block_open(softc, be_lun, req); 2315287621Smav if (retval != 0) { 2316287621Smav retval = 0; 2317287621Smav req->status = CTL_LUN_WARNING; 2318287621Smav } 2319229997Sken } 2320287499Smav num_threads = cbb_num_threads; 2321229997Sken } else { 2322229997Sken num_threads = 1; 2323229997Sken } 2324229997Sken 2325229997Sken /* 2326229997Sken * XXX This searching loop might be refactored to be combined with 2327229997Sken * the loop above, 2328229997Sken */ 2329287499Smav value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2330267481Smav if (value != NULL) { 2331267481Smav tmp_num_threads = strtol(value, NULL, 0); 2332229997Sken 2333267481Smav /* 2334267481Smav * We don't let the user specify less than one 2335267481Smav * thread, but hope he's clueful enough not to 2336267481Smav * specify 1000 threads. 2337267481Smav */ 2338267481Smav if (tmp_num_threads < 1) { 2339267481Smav snprintf(req->error_str, sizeof(req->error_str), 2340272911Smav "invalid number of threads %s", 2341272911Smav num_thread_str); 2342267481Smav goto bailout_error; 2343229997Sken } 2344267481Smav num_threads = tmp_num_threads; 2345229997Sken } 2346229997Sken 2347272911Smav if (be_lun->vn == NULL) 2348287499Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2349229997Sken /* Tell the user the blocksize we ended up using */ 2350272911Smav params->lun_size_bytes = be_lun->size_bytes; 2351287499Smav params->blocksize_bytes = cbe_lun->blocksize; 2352229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2353287499Smav cbe_lun->req_lun_id = params->req_lun_id; 2354287499Smav cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2355229997Sken } else 2356287499Smav cbe_lun->req_lun_id = 0; 2357229997Sken 2358287499Smav cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2359287499Smav cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2360287499Smav cbe_lun->be = &ctl_be_block_driver; 2361229997Sken 2362229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2363229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2364229997Sken softc->num_luns); 2365287499Smav strncpy((char *)cbe_lun->serial_num, tmpstr, 2366287499Smav MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2367229997Sken 2368229997Sken /* Tell the user what we used for a serial number */ 2369229997Sken strncpy((char *)params->serial_num, tmpstr, 2370275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2371229997Sken } else { 2372287499Smav strncpy((char *)cbe_lun->serial_num, params->serial_num, 2373287499Smav MIN(sizeof(cbe_lun->serial_num), 2374229997Sken sizeof(params->serial_num))); 2375229997Sken } 2376229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2377229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2378287499Smav strncpy((char *)cbe_lun->device_id, tmpstr, 2379287499Smav MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2380229997Sken 2381229997Sken /* Tell the user what we used for a device ID */ 2382229997Sken strncpy((char *)params->device_id, tmpstr, 2383275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2384229997Sken } else { 2385287499Smav strncpy((char *)cbe_lun->device_id, params->device_id, 2386287499Smav MIN(sizeof(cbe_lun->device_id), 2387275953Smav sizeof(params->device_id))); 2388229997Sken } 2389229997Sken 2390229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2391229997Sken 2392229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2393229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2394229997Sken 2395229997Sken if (be_lun->io_taskqueue == NULL) { 2396229997Sken snprintf(req->error_str, sizeof(req->error_str), 2397272911Smav "unable to create taskqueue"); 2398229997Sken goto bailout_error; 2399229997Sken } 2400229997Sken 2401229997Sken /* 2402229997Sken * Note that we start the same number of threads by default for 2403229997Sken * both the file case and the block device case. For the file 2404229997Sken * case, we need multiple threads to allow concurrency, because the 2405229997Sken * vnode interface is designed to be a blocking interface. For the 2406229997Sken * block device case, ZFS zvols at least will block the caller's 2407229997Sken * context in many instances, and so we need multiple threads to 2408229997Sken * overcome that problem. Other block devices don't need as many 2409229997Sken * threads, but they shouldn't cause too many problems. 2410229997Sken * 2411229997Sken * If the user wants to just have a single thread for a block 2412229997Sken * device, he can specify that when the LUN is created, or change 2413229997Sken * the tunable/sysctl to alter the default number of threads. 2414229997Sken */ 2415229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2416229997Sken /*num threads*/num_threads, 2417229997Sken /*priority*/PWAIT, 2418229997Sken /*thread name*/ 2419229997Sken "%s taskq", be_lun->lunname); 2420229997Sken 2421229997Sken if (retval != 0) 2422229997Sken goto bailout_error; 2423229997Sken 2424229997Sken be_lun->num_threads = num_threads; 2425229997Sken 2426229997Sken mtx_lock(&softc->lock); 2427229997Sken softc->num_luns++; 2428229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2429229997Sken 2430229997Sken mtx_unlock(&softc->lock); 2431229997Sken 2432287499Smav retval = ctl_add_lun(&be_lun->cbe_lun); 2433229997Sken if (retval != 0) { 2434229997Sken mtx_lock(&softc->lock); 2435229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2436229997Sken links); 2437229997Sken softc->num_luns--; 2438229997Sken mtx_unlock(&softc->lock); 2439229997Sken snprintf(req->error_str, sizeof(req->error_str), 2440272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2441272911Smav "details", retval); 2442229997Sken retval = 0; 2443229997Sken goto bailout_error; 2444229997Sken } 2445229997Sken 2446229997Sken mtx_lock(&softc->lock); 2447229997Sken 2448229997Sken /* 2449229997Sken * Tell the config_status routine that we're waiting so it won't 2450229997Sken * clean up the LUN in the event of an error. 2451229997Sken */ 2452229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2453229997Sken 2454229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2455229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2456229997Sken if (retval == EINTR) 2457229997Sken break; 2458229997Sken } 2459229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2460229997Sken 2461229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2462229997Sken snprintf(req->error_str, sizeof(req->error_str), 2463272911Smav "LUN configuration error, see dmesg for details"); 2464229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2465229997Sken links); 2466229997Sken softc->num_luns--; 2467229997Sken mtx_unlock(&softc->lock); 2468229997Sken goto bailout_error; 2469229997Sken } else { 2470287499Smav params->req_lun_id = cbe_lun->lun_id; 2471229997Sken } 2472229997Sken 2473229997Sken mtx_unlock(&softc->lock); 2474229997Sken 2475229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2476287499Smav cbe_lun->blocksize, 2477229997Sken DEVSTAT_ALL_SUPPORTED, 2478287499Smav cbe_lun->lun_type 2479229997Sken | DEVSTAT_TYPE_IF_OTHER, 2480229997Sken DEVSTAT_PRIORITY_OTHER); 2481229997Sken 2482229997Sken return (retval); 2483229997Sken 2484229997Skenbailout_error: 2485229997Sken req->status = CTL_LUN_ERROR; 2486229997Sken 2487267429Smav if (be_lun->io_taskqueue != NULL) 2488267429Smav taskqueue_free(be_lun->io_taskqueue); 2489229997Sken ctl_be_block_close(be_lun); 2490267429Smav if (be_lun->dev_path != NULL) 2491267429Smav free(be_lun->dev_path, M_CTLBLK); 2492267429Smav if (be_lun->lun_zone != NULL) 2493267429Smav uma_zdestroy(be_lun->lun_zone); 2494287499Smav ctl_free_opts(&cbe_lun->options); 2495267877Smav mtx_destroy(&be_lun->queue_lock); 2496267877Smav mtx_destroy(&be_lun->io_lock); 2497229997Sken free(be_lun, M_CTLBLK); 2498229997Sken 2499229997Sken return (retval); 2500229997Sken} 2501229997Sken 2502229997Skenstatic int 2503229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2504229997Sken{ 2505229997Sken struct ctl_lun_rm_params *params; 2506229997Sken struct ctl_be_block_lun *be_lun; 2507287670Smav struct ctl_be_lun *cbe_lun; 2508229997Sken int retval; 2509229997Sken 2510229997Sken params = &req->reqdata.rm; 2511229997Sken 2512229997Sken mtx_lock(&softc->lock); 2513229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2514287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2515229997Sken break; 2516229997Sken } 2517229997Sken mtx_unlock(&softc->lock); 2518229997Sken 2519229997Sken if (be_lun == NULL) { 2520229997Sken snprintf(req->error_str, sizeof(req->error_str), 2521272911Smav "LUN %u is not managed by the block backend", 2522272911Smav params->lun_id); 2523229997Sken goto bailout_error; 2524229997Sken } 2525287670Smav cbe_lun = &be_lun->cbe_lun; 2526229997Sken 2527287670Smav retval = ctl_disable_lun(cbe_lun); 2528229997Sken if (retval != 0) { 2529229997Sken snprintf(req->error_str, sizeof(req->error_str), 2530272911Smav "error %d returned from ctl_disable_lun() for " 2531272911Smav "LUN %d", retval, params->lun_id); 2532229997Sken goto bailout_error; 2533287670Smav } 2534229997Sken 2535287670Smav if (be_lun->vn != NULL) { 2536287670Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2537287670Smav ctl_lun_offline(cbe_lun); 2538287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2539287670Smav ctl_be_block_close(be_lun); 2540229997Sken } 2541229997Sken 2542287670Smav retval = ctl_invalidate_lun(cbe_lun); 2543229997Sken if (retval != 0) { 2544229997Sken snprintf(req->error_str, sizeof(req->error_str), 2545272911Smav "error %d returned from ctl_invalidate_lun() for " 2546272911Smav "LUN %d", retval, params->lun_id); 2547229997Sken goto bailout_error; 2548229997Sken } 2549229997Sken 2550229997Sken mtx_lock(&softc->lock); 2551229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2552229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2553229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2554229997Sken if (retval == EINTR) 2555229997Sken break; 2556229997Sken } 2557229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2558229997Sken 2559229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2560229997Sken snprintf(req->error_str, sizeof(req->error_str), 2561272911Smav "interrupted waiting for LUN to be freed"); 2562229997Sken mtx_unlock(&softc->lock); 2563229997Sken goto bailout_error; 2564229997Sken } 2565229997Sken 2566229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2567229997Sken 2568229997Sken softc->num_luns--; 2569229997Sken mtx_unlock(&softc->lock); 2570229997Sken 2571287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2572229997Sken taskqueue_free(be_lun->io_taskqueue); 2573229997Sken 2574229997Sken if (be_lun->disk_stats != NULL) 2575229997Sken devstat_remove_entry(be_lun->disk_stats); 2576229997Sken 2577229997Sken uma_zdestroy(be_lun->lun_zone); 2578229997Sken 2579287670Smav ctl_free_opts(&cbe_lun->options); 2580229997Sken free(be_lun->dev_path, M_CTLBLK); 2581267877Smav mtx_destroy(&be_lun->queue_lock); 2582267877Smav mtx_destroy(&be_lun->io_lock); 2583229997Sken free(be_lun, M_CTLBLK); 2584229997Sken 2585229997Sken req->status = CTL_LUN_OK; 2586229997Sken 2587229997Sken return (0); 2588229997Sken 2589229997Skenbailout_error: 2590229997Sken 2591229997Sken req->status = CTL_LUN_ERROR; 2592229997Sken 2593229997Sken return (0); 2594229997Sken} 2595229997Sken 2596232604Straszstatic int 2597232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2598232604Strasz{ 2599232604Strasz struct ctl_lun_modify_params *params; 2600232604Strasz struct ctl_be_block_lun *be_lun; 2601287500Smav struct ctl_be_lun *cbe_lun; 2602287621Smav char *value; 2603271794Smav uint64_t oldsize; 2604287621Smav int error, wasprim; 2605232604Strasz 2606232604Strasz params = &req->reqdata.modify; 2607232604Strasz 2608232604Strasz mtx_lock(&softc->lock); 2609232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2610287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2611232604Strasz break; 2612232604Strasz } 2613232604Strasz mtx_unlock(&softc->lock); 2614232604Strasz 2615232604Strasz if (be_lun == NULL) { 2616232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2617272911Smav "LUN %u is not managed by the block backend", 2618272911Smav params->lun_id); 2619232604Strasz goto bailout_error; 2620232604Strasz } 2621287500Smav cbe_lun = &be_lun->cbe_lun; 2622232604Strasz 2623287500Smav if (params->lun_size_bytes != 0) 2624287500Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2625287500Smav ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2626232604Strasz 2627287621Smav wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2628287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2629287621Smav if (value != NULL) { 2630287621Smav if (strcmp(value, "primary") == 0) 2631287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2632287621Smav else 2633287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2634287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2635287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2636232604Strasz else 2637287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2638287621Smav if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 2639287621Smav if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 2640287621Smav ctl_lun_primary(cbe_lun); 2641287621Smav else 2642287621Smav ctl_lun_secondary(cbe_lun); 2643287621Smav } 2644232604Strasz 2645287621Smav oldsize = be_lun->size_blocks; 2646287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2647287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2648287621Smav if (be_lun->vn == NULL) 2649287621Smav error = ctl_be_block_open(softc, be_lun, req); 2650287621Smav else if (vn_isdisk(be_lun->vn, &error)) 2651288104Smav error = ctl_be_block_open_dev(be_lun, req); 2652287621Smav else if (be_lun->vn->v_type == VREG) 2653288104Smav error = ctl_be_block_open_file(be_lun, req); 2654287621Smav else 2655287621Smav error = EINVAL; 2656287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) && 2657287621Smav be_lun->vn != NULL) { 2658287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE; 2659287621Smav ctl_lun_online(cbe_lun); 2660287621Smav } 2661287621Smav } else { 2662287621Smav if (be_lun->vn != NULL) { 2663287621Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2664287621Smav ctl_lun_offline(cbe_lun); 2665287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2666287621Smav error = ctl_be_block_close(be_lun); 2667287621Smav } else 2668287621Smav error = 0; 2669287621Smav } 2670287499Smav if (be_lun->size_blocks != oldsize) 2671287500Smav ctl_lun_capacity_changed(cbe_lun); 2672232604Strasz 2673232604Strasz /* Tell the user the exact size we ended up using */ 2674232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2675232604Strasz 2676272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2677232604Strasz return (0); 2678232604Strasz 2679232604Straszbailout_error: 2680232604Strasz req->status = CTL_LUN_ERROR; 2681232604Strasz return (0); 2682232604Strasz} 2683232604Strasz 2684229997Skenstatic void 2685229997Skenctl_be_block_lun_shutdown(void *be_lun) 2686229997Sken{ 2687229997Sken struct ctl_be_block_lun *lun; 2688229997Sken struct ctl_be_block_softc *softc; 2689229997Sken 2690229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2691229997Sken 2692229997Sken softc = lun->softc; 2693229997Sken 2694229997Sken mtx_lock(&softc->lock); 2695229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2696229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2697229997Sken wakeup(lun); 2698229997Sken mtx_unlock(&softc->lock); 2699229997Sken 2700229997Sken} 2701229997Sken 2702229997Skenstatic void 2703229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2704229997Sken{ 2705229997Sken struct ctl_be_block_lun *lun; 2706229997Sken struct ctl_be_block_softc *softc; 2707229997Sken 2708229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2709229997Sken softc = lun->softc; 2710229997Sken 2711229997Sken if (status == CTL_LUN_CONFIG_OK) { 2712229997Sken mtx_lock(&softc->lock); 2713229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2714229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2715229997Sken wakeup(lun); 2716229997Sken mtx_unlock(&softc->lock); 2717229997Sken 2718229997Sken /* 2719229997Sken * We successfully added the LUN, attempt to enable it. 2720229997Sken */ 2721287499Smav if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2722229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2723287499Smav if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2724229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2725229997Sken __func__); 2726229997Sken } 2727229997Sken } 2728229997Sken 2729229997Sken return; 2730229997Sken } 2731229997Sken 2732229997Sken 2733229997Sken mtx_lock(&softc->lock); 2734229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2735229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2736229997Sken wakeup(lun); 2737229997Sken mtx_unlock(&softc->lock); 2738229997Sken} 2739229997Sken 2740229997Sken 2741229997Skenstatic int 2742229997Skenctl_be_block_config_write(union ctl_io *io) 2743229997Sken{ 2744229997Sken struct ctl_be_block_lun *be_lun; 2745287499Smav struct ctl_be_lun *cbe_lun; 2746229997Sken int retval; 2747229997Sken 2748229997Sken retval = 0; 2749229997Sken 2750229997Sken DPRINTF("entered\n"); 2751229997Sken 2752287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2753229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2754287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2755229997Sken 2756229997Sken switch (io->scsiio.cdb[0]) { 2757229997Sken case SYNCHRONIZE_CACHE: 2758229997Sken case SYNCHRONIZE_CACHE_16: 2759264274Smav case WRITE_SAME_10: 2760264274Smav case WRITE_SAME_16: 2761264274Smav case UNMAP: 2762229997Sken /* 2763229997Sken * The upper level CTL code will filter out any CDBs with 2764229997Sken * the immediate bit set and return the proper error. 2765229997Sken * 2766229997Sken * We don't really need to worry about what LBA range the 2767229997Sken * user asked to be synced out. When they issue a sync 2768229997Sken * cache command, we'll sync out the whole thing. 2769229997Sken */ 2770267877Smav mtx_lock(&be_lun->queue_lock); 2771229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2772229997Sken links); 2773267877Smav mtx_unlock(&be_lun->queue_lock); 2774229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2775229997Sken break; 2776229997Sken case START_STOP_UNIT: { 2777229997Sken struct scsi_start_stop_unit *cdb; 2778229997Sken 2779229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2780229997Sken 2781229997Sken if (cdb->how & SSS_START) 2782287499Smav retval = ctl_start_lun(cbe_lun); 2783229997Sken else { 2784287499Smav retval = ctl_stop_lun(cbe_lun); 2785229997Sken /* 2786229997Sken * XXX KDM Copan-specific offline behavior. 2787229997Sken * Figure out a reasonable way to port this? 2788229997Sken */ 2789229997Sken#ifdef NEEDTOPORT 2790229997Sken if ((retval == 0) 2791229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2792287499Smav retval = ctl_lun_offline(cbe_lun); 2793229997Sken#endif 2794229997Sken } 2795229997Sken 2796229997Sken /* 2797229997Sken * In general, the above routines should not fail. They 2798229997Sken * just set state for the LUN. So we've got something 2799229997Sken * pretty wrong here if we can't start or stop the LUN. 2800229997Sken */ 2801229997Sken if (retval != 0) { 2802229997Sken ctl_set_internal_failure(&io->scsiio, 2803229997Sken /*sks_valid*/ 1, 2804229997Sken /*retry_count*/ 0xf051); 2805229997Sken retval = CTL_RETVAL_COMPLETE; 2806229997Sken } else { 2807229997Sken ctl_set_success(&io->scsiio); 2808229997Sken } 2809229997Sken ctl_config_write_done(io); 2810229997Sken break; 2811229997Sken } 2812229997Sken default: 2813229997Sken ctl_set_invalid_opcode(&io->scsiio); 2814229997Sken ctl_config_write_done(io); 2815229997Sken retval = CTL_RETVAL_COMPLETE; 2816229997Sken break; 2817229997Sken } 2818229997Sken 2819229997Sken return (retval); 2820229997Sken} 2821229997Sken 2822229997Skenstatic int 2823229997Skenctl_be_block_config_read(union ctl_io *io) 2824229997Sken{ 2825275474Smav struct ctl_be_block_lun *be_lun; 2826287499Smav struct ctl_be_lun *cbe_lun; 2827275474Smav int retval = 0; 2828275474Smav 2829275474Smav DPRINTF("entered\n"); 2830275474Smav 2831287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2832275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2833287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2834275474Smav 2835275474Smav switch (io->scsiio.cdb[0]) { 2836275474Smav case SERVICE_ACTION_IN: 2837275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2838275474Smav mtx_lock(&be_lun->queue_lock); 2839275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2840275474Smav &io->io_hdr, links); 2841275474Smav mtx_unlock(&be_lun->queue_lock); 2842275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2843275474Smav &be_lun->io_task); 2844275474Smav retval = CTL_RETVAL_QUEUED; 2845275474Smav break; 2846275474Smav } 2847275474Smav ctl_set_invalid_field(&io->scsiio, 2848275474Smav /*sks_valid*/ 1, 2849275474Smav /*command*/ 1, 2850275474Smav /*field*/ 1, 2851275474Smav /*bit_valid*/ 1, 2852275474Smav /*bit*/ 4); 2853275474Smav ctl_config_read_done(io); 2854275474Smav retval = CTL_RETVAL_COMPLETE; 2855275474Smav break; 2856275474Smav default: 2857275474Smav ctl_set_invalid_opcode(&io->scsiio); 2858275474Smav ctl_config_read_done(io); 2859275474Smav retval = CTL_RETVAL_COMPLETE; 2860275474Smav break; 2861275474Smav } 2862275474Smav 2863275474Smav return (retval); 2864229997Sken} 2865229997Sken 2866229997Skenstatic int 2867229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2868229997Sken{ 2869229997Sken struct ctl_be_block_lun *lun; 2870229997Sken int retval; 2871229997Sken 2872229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2873229997Sken retval = 0; 2874229997Sken 2875268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2876229997Sken 2877229997Sken if (retval != 0) 2878229997Sken goto bailout; 2879229997Sken 2880229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2881229997Sken 2882229997Sken if (retval != 0) 2883229997Sken goto bailout; 2884229997Sken 2885268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2886229997Sken 2887229997Skenbailout: 2888229997Sken 2889229997Sken return (retval); 2890229997Sken} 2891229997Sken 2892274154Smavstatic uint64_t 2893274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2894274154Smav{ 2895274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2896274154Smav 2897274154Smav if (lun->getattr == NULL) 2898274154Smav return (UINT64_MAX); 2899274154Smav return (lun->getattr(lun, attrname)); 2900274154Smav} 2901274154Smav 2902229997Skenint 2903229997Skenctl_be_block_init(void) 2904229997Sken{ 2905229997Sken struct ctl_be_block_softc *softc; 2906229997Sken int retval; 2907229997Sken 2908229997Sken softc = &backend_block_softc; 2909229997Sken retval = 0; 2910229997Sken 2911267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2912264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2913264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2914229997Sken STAILQ_INIT(&softc->lun_list); 2915229997Sken 2916229997Sken return (retval); 2917229997Sken} 2918