ctl_backend_block.c revision 312834
1229997Sken/*- 2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp. 3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation 4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation 5288348Smav * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org> 6229997Sken * All rights reserved. 7229997Sken * 8232604Strasz * Portions of this software were developed by Edward Tomasz Napierala 9232604Strasz * under sponsorship from the FreeBSD Foundation. 10232604Strasz * 11229997Sken * Redistribution and use in source and binary forms, with or without 12229997Sken * modification, are permitted provided that the following conditions 13229997Sken * are met: 14229997Sken * 1. Redistributions of source code must retain the above copyright 15229997Sken * notice, this list of conditions, and the following disclaimer, 16229997Sken * without modification. 17229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18229997Sken * substantially similar to the "NO WARRANTY" disclaimer below 19229997Sken * ("Disclaimer") and any redistribution must be conditioned upon 20229997Sken * including a substantially similar Disclaimer requirement for further 21229997Sken * binary redistribution. 22229997Sken * 23229997Sken * NO WARRANTY 24229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 27229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 33229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34229997Sken * POSSIBILITY OF SUCH DAMAGES. 35229997Sken * 36229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 37229997Sken */ 38229997Sken/* 39229997Sken * CAM Target Layer driver backend for block devices. 40229997Sken * 41229997Sken * Author: Ken Merry <ken@FreeBSD.org> 42229997Sken */ 43229997Sken#include <sys/cdefs.h> 44229997Sken__FBSDID("$FreeBSD: stable/11/sys/cam/ctl/ctl_backend_block.c 312834 2017-01-26 20:49:19Z mav $"); 45229997Sken 46229997Sken#include <sys/param.h> 47229997Sken#include <sys/systm.h> 48229997Sken#include <sys/kernel.h> 49229997Sken#include <sys/types.h> 50229997Sken#include <sys/kthread.h> 51229997Sken#include <sys/bio.h> 52229997Sken#include <sys/fcntl.h> 53264274Smav#include <sys/limits.h> 54229997Sken#include <sys/lock.h> 55229997Sken#include <sys/mutex.h> 56229997Sken#include <sys/condvar.h> 57229997Sken#include <sys/malloc.h> 58229997Sken#include <sys/conf.h> 59229997Sken#include <sys/ioccom.h> 60229997Sken#include <sys/queue.h> 61229997Sken#include <sys/sbuf.h> 62229997Sken#include <sys/endian.h> 63229997Sken#include <sys/uio.h> 64229997Sken#include <sys/buf.h> 65229997Sken#include <sys/taskqueue.h> 66229997Sken#include <sys/vnode.h> 67229997Sken#include <sys/namei.h> 68229997Sken#include <sys/mount.h> 69229997Sken#include <sys/disk.h> 70229997Sken#include <sys/fcntl.h> 71229997Sken#include <sys/filedesc.h> 72275474Smav#include <sys/filio.h> 73229997Sken#include <sys/proc.h> 74229997Sken#include <sys/pcpu.h> 75229997Sken#include <sys/module.h> 76229997Sken#include <sys/sdt.h> 77229997Sken#include <sys/devicestat.h> 78229997Sken#include <sys/sysctl.h> 79229997Sken 80229997Sken#include <geom/geom.h> 81229997Sken 82229997Sken#include <cam/cam.h> 83229997Sken#include <cam/scsi/scsi_all.h> 84229997Sken#include <cam/scsi/scsi_da.h> 85229997Sken#include <cam/ctl/ctl_io.h> 86229997Sken#include <cam/ctl/ctl.h> 87229997Sken#include <cam/ctl/ctl_backend.h> 88229997Sken#include <cam/ctl/ctl_ioctl.h> 89287621Smav#include <cam/ctl/ctl_ha.h> 90229997Sken#include <cam/ctl/ctl_scsi_all.h> 91287621Smav#include <cam/ctl/ctl_private.h> 92229997Sken#include <cam/ctl/ctl_error.h> 93229997Sken 94229997Sken/* 95264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 96264886Smav * I/O. If we get an I/O larger than that, we'll split it. 97229997Sken */ 98267537Smav#define CTLBLK_HALF_IO_SIZE (512 * 1024) 99267537Smav#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 100264886Smav#define CTLBLK_MAX_SEG MAXPHYS 101267537Smav#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 102267537Smav#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 103229997Sken 104229997Sken#ifdef CTLBLK_DEBUG 105229997Sken#define DPRINTF(fmt, args...) \ 106229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 107229997Sken#else 108229997Sken#define DPRINTF(fmt, args...) do {} while(0) 109229997Sken#endif 110229997Sken 111267519Smav#define PRIV(io) \ 112267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 113267537Smav#define ARGS(io) \ 114267537Smav ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 115267519Smav 116229997SkenSDT_PROVIDER_DEFINE(cbb); 117229997Sken 118229997Skentypedef enum { 119229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 120229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 121229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 122229997Sken} ctl_be_block_lun_flags; 123229997Sken 124229997Skentypedef enum { 125229997Sken CTL_BE_BLOCK_NONE, 126229997Sken CTL_BE_BLOCK_DEV, 127229997Sken CTL_BE_BLOCK_FILE 128229997Sken} ctl_be_block_type; 129229997Sken 130229997Skenstruct ctl_be_block_filedata { 131229997Sken struct ucred *cred; 132229997Sken}; 133229997Sken 134229997Skenunion ctl_be_block_bedata { 135229997Sken struct ctl_be_block_filedata file; 136229997Sken}; 137229997Sken 138229997Skenstruct ctl_be_block_io; 139229997Skenstruct ctl_be_block_lun; 140229997Sken 141229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 142229997Sken struct ctl_be_block_io *beio); 143274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 144274154Smav const char *attrname); 145229997Sken 146229997Sken/* 147229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 148229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 149229997Sken */ 150229997Skenstruct ctl_be_block_lun { 151272911Smav struct ctl_lun_create_params params; 152229997Sken char lunname[32]; 153229997Sken char *dev_path; 154229997Sken ctl_be_block_type dev_type; 155229997Sken struct vnode *vn; 156229997Sken union ctl_be_block_bedata backend; 157229997Sken cbb_dispatch_t dispatch; 158229997Sken cbb_dispatch_t lun_flush; 159264274Smav cbb_dispatch_t unmap; 160275474Smav cbb_dispatch_t get_lba_status; 161274154Smav cbb_getattr_t getattr; 162229997Sken uma_zone_t lun_zone; 163229997Sken uint64_t size_blocks; 164229997Sken uint64_t size_bytes; 165229997Sken struct ctl_be_block_softc *softc; 166229997Sken struct devstat *disk_stats; 167229997Sken ctl_be_block_lun_flags flags; 168229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 169287499Smav struct ctl_be_lun cbe_lun; 170229997Sken struct taskqueue *io_taskqueue; 171229997Sken struct task io_task; 172229997Sken int num_threads; 173229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 174275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 175229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 176229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 177267877Smav struct mtx_padalign io_lock; 178267877Smav struct mtx_padalign queue_lock; 179229997Sken}; 180229997Sken 181229997Sken/* 182229997Sken * Overall softc structure for the block backend module. 183229997Sken */ 184229997Skenstruct ctl_be_block_softc { 185229997Sken struct mtx lock; 186229997Sken int num_luns; 187229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 188229997Sken}; 189229997Sken 190229997Skenstatic struct ctl_be_block_softc backend_block_softc; 191229997Sken 192229997Sken/* 193229997Sken * Per-I/O information. 194229997Sken */ 195229997Skenstruct ctl_be_block_io { 196229997Sken union ctl_io *io; 197229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 198229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 199229997Sken int bio_cmd; 200229997Sken int num_segs; 201229997Sken int num_bios_sent; 202229997Sken int num_bios_done; 203229997Sken int send_complete; 204311418Smav int first_error; 205311418Smav uint64_t first_error_offset; 206229997Sken struct bintime ds_t0; 207229997Sken devstat_tag_type ds_tag_type; 208229997Sken devstat_trans_flags ds_trans_type; 209229997Sken uint64_t io_len; 210229997Sken uint64_t io_offset; 211286353Smav int io_arg; 212229997Sken struct ctl_be_block_softc *softc; 213229997Sken struct ctl_be_block_lun *lun; 214264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 215229997Sken}; 216229997Sken 217287621Smavextern struct ctl_softc *control_softc; 218287621Smav 219229997Skenstatic int cbb_num_threads = 14; 220229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 221229997Sken "CAM Target Layer Block Backend"); 222267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 223229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 224229997Sken 225229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 226229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 227229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 228229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 229229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 230229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 231229997Sken struct ctl_be_block_io *beio); 232229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 233229997Sken struct ctl_be_block_io *beio); 234275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 235275474Smav struct ctl_be_block_io *beio); 236275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 237275481Smav const char *attrname); 238229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 239229997Sken struct ctl_be_block_io *beio); 240264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 241264274Smav struct ctl_be_block_io *beio); 242229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 243229997Sken struct ctl_be_block_io *beio); 244274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 245274154Smav const char *attrname); 246275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 247275474Smav union ctl_io *io); 248229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 249229997Sken union ctl_io *io); 250229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 251229997Sken union ctl_io *io); 252229997Skenstatic void ctl_be_block_worker(void *context, int pending); 253229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 254229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 255229997Sken int flag, struct thread *td); 256229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 257229997Sken struct ctl_lun_req *req); 258229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 259229997Sken struct ctl_lun_req *req); 260229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 261288348Smavstatic int ctl_be_block_open(struct ctl_be_block_lun *be_lun, 262229997Sken struct ctl_lun_req *req); 263229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 264229997Sken struct ctl_lun_req *req); 265229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 266229997Sken struct ctl_lun_req *req); 267232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 268232604Strasz struct ctl_lun_req *req); 269229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 270229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 271229997Sken ctl_lun_config_status status); 272229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 273229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 274229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 275274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 276229997Skenint ctl_be_block_init(void); 277229997Sken 278229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 279229997Sken{ 280230334Sken .name = "block", 281230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 282230334Sken .init = ctl_be_block_init, 283230334Sken .data_submit = ctl_be_block_submit, 284230334Sken .data_move_done = ctl_be_block_move_done, 285230334Sken .config_read = ctl_be_block_config_read, 286230334Sken .config_write = ctl_be_block_config_write, 287230334Sken .ioctl = ctl_be_block_ioctl, 288274154Smav .lun_info = ctl_be_block_lun_info, 289274154Smav .lun_attr = ctl_be_block_lun_attr 290229997Sken}; 291229997Sken 292229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 293229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 294229997Sken 295264020Straszstatic uma_zone_t beio_zone; 296264020Strasz 297229997Skenstatic struct ctl_be_block_io * 298229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 299229997Sken{ 300229997Sken struct ctl_be_block_io *beio; 301229997Sken 302264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 303264020Strasz beio->softc = softc; 304229997Sken return (beio); 305229997Sken} 306229997Sken 307229997Skenstatic void 308229997Skenctl_free_beio(struct ctl_be_block_io *beio) 309229997Sken{ 310229997Sken int duplicate_free; 311229997Sken int i; 312229997Sken 313229997Sken duplicate_free = 0; 314229997Sken 315229997Sken for (i = 0; i < beio->num_segs; i++) { 316229997Sken if (beio->sg_segs[i].addr == NULL) 317229997Sken duplicate_free++; 318229997Sken 319229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 320229997Sken beio->sg_segs[i].addr = NULL; 321267537Smav 322267537Smav /* For compare we had two equal S/G lists. */ 323267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 324267537Smav uma_zfree(beio->lun->lun_zone, 325267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 326267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 327267537Smav } 328229997Sken } 329229997Sken 330229997Sken if (duplicate_free > 0) { 331229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 332229997Sken duplicate_free, beio->num_segs); 333229997Sken } 334229997Sken 335264020Strasz uma_zfree(beio_zone, beio); 336229997Sken} 337229997Sken 338229997Skenstatic void 339229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 340229997Sken{ 341267877Smav union ctl_io *io = beio->io; 342229997Sken 343264274Smav if (beio->beio_cont != NULL) { 344264274Smav beio->beio_cont(beio); 345264274Smav } else { 346264274Smav ctl_free_beio(beio); 347267537Smav ctl_data_submit_done(io); 348264274Smav } 349229997Sken} 350229997Sken 351287868Smavstatic size_t 352287868Smavcmp(uint8_t *a, uint8_t *b, size_t size) 353287868Smav{ 354287868Smav size_t i; 355287868Smav 356287868Smav for (i = 0; i < size; i++) { 357287868Smav if (a[i] != b[i]) 358287868Smav break; 359287868Smav } 360287868Smav return (i); 361287868Smav} 362287868Smav 363287868Smavstatic void 364287868Smavctl_be_block_compare(union ctl_io *io) 365287868Smav{ 366287868Smav struct ctl_be_block_io *beio; 367287868Smav uint64_t off, res; 368287868Smav int i; 369287868Smav uint8_t info[8]; 370287868Smav 371287868Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 372287868Smav off = 0; 373287868Smav for (i = 0; i < beio->num_segs; i++) { 374287868Smav res = cmp(beio->sg_segs[i].addr, 375287868Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 376287868Smav beio->sg_segs[i].len); 377287868Smav off += res; 378287868Smav if (res < beio->sg_segs[i].len) 379287868Smav break; 380287868Smav } 381287868Smav if (i < beio->num_segs) { 382287868Smav scsi_u64to8b(off, info); 383287868Smav ctl_set_sense(&io->scsiio, /*current_error*/ 1, 384287868Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 385287868Smav /*asc*/ 0x1D, /*ascq*/ 0x00, 386287868Smav /*type*/ SSD_ELEM_INFO, 387287868Smav /*size*/ sizeof(info), /*data*/ &info, 388287868Smav /*type*/ SSD_ELEM_NONE); 389287868Smav } else 390287868Smav ctl_set_success(&io->scsiio); 391287868Smav} 392287868Smav 393229997Skenstatic int 394229997Skenctl_be_block_move_done(union ctl_io *io) 395229997Sken{ 396229997Sken struct ctl_be_block_io *beio; 397229997Sken struct ctl_be_block_lun *be_lun; 398267537Smav struct ctl_lba_len_flags *lbalen; 399229997Sken#ifdef CTL_TIME_IO 400229997Sken struct bintime cur_bt; 401267537Smav#endif 402229997Sken 403267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 404229997Sken be_lun = beio->lun; 405229997Sken 406229997Sken DPRINTF("entered\n"); 407229997Sken 408229997Sken#ifdef CTL_TIME_IO 409288215Smav getbinuptime(&cur_bt); 410229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 411229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 412288215Smav#endif 413229997Sken io->io_hdr.num_dmas++; 414267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 415229997Sken 416229997Sken /* 417229997Sken * We set status at this point for read commands, and write 418229997Sken * commands with errors. 419229997Sken */ 420275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 421275058Smav ; 422275058Smav } else if ((io->io_hdr.port_status == 0) && 423267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 424267537Smav lbalen = ARGS(beio->io); 425267537Smav if (lbalen->flags & CTL_LLF_READ) { 426267537Smav ctl_set_success(&io->scsiio); 427267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 428267537Smav /* We have two data blocks ready for comparison. */ 429287868Smav ctl_be_block_compare(io); 430267537Smav } 431275058Smav } else if ((io->io_hdr.port_status != 0) && 432275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 433275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 434229997Sken /* 435229997Sken * For hardware error sense keys, the sense key 436229997Sken * specific value is defined to be a retry count, 437229997Sken * but we use it to pass back an internal FETD 438229997Sken * error code. XXX KDM Hopefully the FETD is only 439229997Sken * using 16 bits for an error code, since that's 440229997Sken * all the space we have in the sks field. 441229997Sken */ 442229997Sken ctl_set_internal_failure(&io->scsiio, 443229997Sken /*sks_valid*/ 1, 444229997Sken /*retry_count*/ 445229997Sken io->io_hdr.port_status); 446229997Sken } 447229997Sken 448229997Sken /* 449229997Sken * If this is a read, or a write with errors, it is done. 450229997Sken */ 451229997Sken if ((beio->bio_cmd == BIO_READ) 452229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 453229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 454229997Sken ctl_complete_beio(beio); 455229997Sken return (0); 456229997Sken } 457229997Sken 458229997Sken /* 459229997Sken * At this point, we have a write and the DMA completed 460229997Sken * successfully. We now have to queue it to the task queue to 461229997Sken * execute the backend I/O. That is because we do blocking 462229997Sken * memory allocations, and in the file backing case, blocking I/O. 463229997Sken * This move done routine is generally called in the SIM's 464229997Sken * interrupt context, and therefore we cannot block. 465229997Sken */ 466267877Smav mtx_lock(&be_lun->queue_lock); 467229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 468267877Smav mtx_unlock(&be_lun->queue_lock); 469229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 470229997Sken 471229997Sken return (0); 472229997Sken} 473229997Sken 474229997Skenstatic void 475229997Skenctl_be_block_biodone(struct bio *bio) 476229997Sken{ 477229997Sken struct ctl_be_block_io *beio; 478229997Sken struct ctl_be_block_lun *be_lun; 479229997Sken union ctl_io *io; 480261538Smav int error; 481229997Sken 482229997Sken beio = bio->bio_caller1; 483229997Sken be_lun = beio->lun; 484229997Sken io = beio->io; 485229997Sken 486229997Sken DPRINTF("entered\n"); 487229997Sken 488261538Smav error = bio->bio_error; 489267877Smav mtx_lock(&be_lun->io_lock); 490311418Smav if (error != 0 && 491311418Smav (beio->first_error == 0 || 492311418Smav bio->bio_offset < beio->first_error_offset)) { 493311418Smav beio->first_error = error; 494311418Smav beio->first_error_offset = bio->bio_offset; 495311418Smav } 496229997Sken 497229997Sken beio->num_bios_done++; 498229997Sken 499229997Sken /* 500229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 501229997Sken * during the free might cause it to complain. 502229997Sken */ 503229997Sken g_destroy_bio(bio); 504229997Sken 505229997Sken /* 506229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 507229997Sken * complete, then we're done. 508229997Sken */ 509229997Sken if ((beio->send_complete == 0) 510229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 511267877Smav mtx_unlock(&be_lun->io_lock); 512229997Sken return; 513229997Sken } 514229997Sken 515229997Sken /* 516229997Sken * At this point, we've verified that we are the last I/O to 517229997Sken * complete, so it's safe to drop the lock. 518229997Sken */ 519267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 520267877Smav beio->ds_tag_type, beio->ds_trans_type, 521267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 522267877Smav mtx_unlock(&be_lun->io_lock); 523229997Sken 524229997Sken /* 525229997Sken * If there are any errors from the backing device, we fail the 526229997Sken * entire I/O with a medium error. 527229997Sken */ 528311418Smav error = beio->first_error; 529311418Smav if (error != 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 589288220Smav if (MNT_SHARED_WRITES(mountpoint) || 590288220Smav ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 591229997Sken lock_flags = LK_SHARED; 592229997Sken else 593229997Sken lock_flags = LK_EXCLUSIVE; 594229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 595286353Smav error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 596286353Smav curthread); 597229997Sken VOP_UNLOCK(be_lun->vn, 0); 598229997Sken 599229997Sken vn_finished_write(mountpoint); 600229997Sken 601267877Smav mtx_lock(&be_lun->io_lock); 602267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 603267877Smav beio->ds_tag_type, beio->ds_trans_type, 604267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 605267877Smav mtx_unlock(&be_lun->io_lock); 606267877Smav 607229997Sken if (error == 0) 608229997Sken ctl_set_success(&io->scsiio); 609229997Sken else { 610229997Sken /* XXX KDM is there is a better error here? */ 611229997Sken ctl_set_internal_failure(&io->scsiio, 612229997Sken /*sks_valid*/ 1, 613229997Sken /*retry_count*/ 0xbad1); 614229997Sken } 615229997Sken 616229997Sken ctl_complete_beio(beio); 617229997Sken} 618229997Sken 619292384SmarkjSDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t"); 620292384SmarkjSDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t"); 621292384SmarkjSDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t"); 622292384SmarkjSDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t"); 623229997Sken 624229997Skenstatic void 625229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 626229997Sken struct ctl_be_block_io *beio) 627229997Sken{ 628229997Sken struct ctl_be_block_filedata *file_data; 629229997Sken union ctl_io *io; 630229997Sken struct uio xuio; 631229997Sken struct iovec *xiovec; 632287875Smav size_t s; 633287875Smav int error, flags, i; 634229997Sken 635229997Sken DPRINTF("entered\n"); 636229997Sken 637229997Sken file_data = &be_lun->backend.file; 638229997Sken io = beio->io; 639271309Smav flags = 0; 640271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 641271309Smav flags |= IO_DIRECT; 642271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 643271309Smav flags |= IO_SYNC; 644229997Sken 645267537Smav bzero(&xuio, sizeof(xuio)); 646229997Sken if (beio->bio_cmd == BIO_READ) { 647292384Smarkj SDT_PROBE0(cbb, , read, file_start); 648267537Smav xuio.uio_rw = UIO_READ; 649229997Sken } else { 650292384Smarkj SDT_PROBE0(cbb, , write, file_start); 651267537Smav xuio.uio_rw = UIO_WRITE; 652229997Sken } 653229997Sken xuio.uio_offset = beio->io_offset; 654229997Sken xuio.uio_resid = beio->io_len; 655229997Sken xuio.uio_segflg = UIO_SYSSPACE; 656229997Sken xuio.uio_iov = beio->xiovecs; 657229997Sken xuio.uio_iovcnt = beio->num_segs; 658229997Sken xuio.uio_td = curthread; 659229997Sken 660229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 661229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 662229997Sken xiovec->iov_len = beio->sg_segs[i].len; 663229997Sken } 664229997Sken 665267877Smav binuptime(&beio->ds_t0); 666267877Smav mtx_lock(&be_lun->io_lock); 667267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 668267877Smav mtx_unlock(&be_lun->io_lock); 669267877Smav 670229997Sken if (beio->bio_cmd == BIO_READ) { 671229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 672229997Sken 673229997Sken /* 674229997Sken * UFS pays attention to IO_DIRECT for reads. If the 675229997Sken * DIRECTIO option is configured into the kernel, it calls 676229997Sken * ffs_rawread(). But that only works for single-segment 677229997Sken * uios with user space addresses. In our case, with a 678229997Sken * kernel uio, it still reads into the buffer cache, but it 679229997Sken * will just try to release the buffer from the cache later 680229997Sken * on in ffs_read(). 681229997Sken * 682229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 683229997Sken * 684229997Sken * UFS does not pay attention to IO_SYNC for reads. 685229997Sken * 686229997Sken * ZFS pays attention to IO_SYNC (which translates into the 687229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 688229997Sken * attempts to sync the file before reading. 689229997Sken */ 690271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 691229997Sken 692229997Sken VOP_UNLOCK(be_lun->vn, 0); 693292384Smarkj SDT_PROBE0(cbb, , read, file_done); 694287875Smav if (error == 0 && xuio.uio_resid > 0) { 695287875Smav /* 696287875Smav * If we red less then requested (EOF), then 697287875Smav * we should clean the rest of the buffer. 698287875Smav */ 699287875Smav s = beio->io_len - xuio.uio_resid; 700287875Smav for (i = 0; i < beio->num_segs; i++) { 701287875Smav if (s >= beio->sg_segs[i].len) { 702287875Smav s -= beio->sg_segs[i].len; 703287875Smav continue; 704287875Smav } 705287875Smav bzero((uint8_t *)beio->sg_segs[i].addr + s, 706287875Smav beio->sg_segs[i].len - s); 707287875Smav s = 0; 708287875Smav } 709287875Smav } 710229997Sken } else { 711229997Sken struct mount *mountpoint; 712229997Sken int lock_flags; 713229997Sken 714229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 715229997Sken 716288220Smav if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL) 717229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 718229997Sken lock_flags = LK_SHARED; 719229997Sken else 720229997Sken lock_flags = LK_EXCLUSIVE; 721229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 722229997Sken 723229997Sken /* 724229997Sken * UFS pays attention to IO_DIRECT for writes. The write 725229997Sken * is done asynchronously. (Normally the write would just 726229997Sken * get put into cache. 727229997Sken * 728229997Sken * UFS pays attention to IO_SYNC for writes. It will 729229997Sken * attempt to write the buffer out synchronously if that 730229997Sken * flag is set. 731229997Sken * 732229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 733229997Sken * 734229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 735229997Sken * for writes. It will flush the transaction from the 736229997Sken * cache before returning. 737229997Sken */ 738271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 739229997Sken VOP_UNLOCK(be_lun->vn, 0); 740229997Sken 741229997Sken vn_finished_write(mountpoint); 742292384Smarkj SDT_PROBE0(cbb, , write, file_done); 743229997Sken } 744229997Sken 745267877Smav mtx_lock(&be_lun->io_lock); 746267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 747267877Smav beio->ds_tag_type, beio->ds_trans_type, 748267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 749267877Smav mtx_unlock(&be_lun->io_lock); 750267877Smav 751229997Sken /* 752229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 753229997Sken * return the I/O to the user. 754229997Sken */ 755229997Sken if (error != 0) { 756282565Smav if (error == ENOSPC || error == EDQUOT) { 757273809Smav ctl_set_space_alloc_fail(&io->scsiio); 758287760Smav } else if (error == EROFS || error == EACCES) { 759287760Smav ctl_set_hw_write_protected(&io->scsiio); 760287912Smav } else { 761287912Smav ctl_set_medium_error(&io->scsiio, 762287912Smav beio->bio_cmd == BIO_READ); 763287912Smav } 764229997Sken ctl_complete_beio(beio); 765229997Sken return; 766229997Sken } 767229997Sken 768229997Sken /* 769269122Smav * If this is a write or a verify, we're all done. 770229997Sken * If this is a read, we can now send the data to the user. 771229997Sken */ 772269122Smav if ((beio->bio_cmd == BIO_WRITE) || 773269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 774229997Sken ctl_set_success(&io->scsiio); 775229997Sken ctl_complete_beio(beio); 776229997Sken } else { 777275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 778287967Smav beio->beio_cont == NULL) { 779275058Smav ctl_set_success(&io->scsiio); 780287967Smav ctl_serseq_done(io); 781287967Smav } 782229997Sken#ifdef CTL_TIME_IO 783288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 784288215Smav#endif 785229997Sken ctl_datamove(io); 786229997Sken } 787229997Sken} 788229997Sken 789229997Skenstatic void 790275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 791275474Smav struct ctl_be_block_io *beio) 792275474Smav{ 793275474Smav union ctl_io *io = beio->io; 794275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 795275474Smav struct scsi_get_lba_status_data *data; 796275474Smav off_t roff, off; 797275474Smav int error, status; 798275474Smav 799275474Smav DPRINTF("entered\n"); 800275474Smav 801287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 802275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 803275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 804275474Smav 0, curthread->td_ucred, curthread); 805275474Smav if (error == 0 && off > roff) 806275474Smav status = 0; /* mapped up to off */ 807275474Smav else { 808275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 809275474Smav 0, curthread->td_ucred, curthread); 810275474Smav if (error == 0 && off > roff) 811275474Smav status = 1; /* deallocated up to off */ 812275474Smav else { 813275474Smav status = 0; /* unknown up to the end */ 814275474Smav off = be_lun->size_bytes; 815275474Smav } 816275474Smav } 817275474Smav VOP_UNLOCK(be_lun->vn, 0); 818275474Smav 819275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 820275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 821287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 822287499Smav lbalen->lba), data->descr[0].length); 823275474Smav data->descr[0].status = status; 824275474Smav 825275474Smav ctl_complete_beio(beio); 826275474Smav} 827275474Smav 828275481Smavstatic uint64_t 829275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 830275481Smav{ 831275481Smav struct vattr vattr; 832275481Smav struct statfs statfs; 833285030Smav uint64_t val; 834275481Smav int error; 835275481Smav 836285030Smav val = UINT64_MAX; 837275481Smav if (be_lun->vn == NULL) 838285030Smav return (val); 839285030Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 840275481Smav if (strcmp(attrname, "blocksused") == 0) { 841275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 842285030Smav if (error == 0) 843287499Smav val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 844275481Smav } 845285030Smav if (strcmp(attrname, "blocksavail") == 0 && 846285030Smav (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 847275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 848285030Smav if (error == 0) 849286811Smav val = statfs.f_bavail * statfs.f_bsize / 850287499Smav be_lun->cbe_lun.blocksize; 851275481Smav } 852285030Smav VOP_UNLOCK(be_lun->vn, 0); 853285030Smav return (val); 854275481Smav} 855275481Smav 856275474Smavstatic void 857269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 858269123Smav struct ctl_be_block_io *beio) 859269123Smav{ 860269123Smav union ctl_io *io; 861287664Smav struct cdevsw *csw; 862287664Smav struct cdev *dev; 863269123Smav struct uio xuio; 864269123Smav struct iovec *xiovec; 865287664Smav int error, flags, i, ref; 866269123Smav 867269123Smav DPRINTF("entered\n"); 868269123Smav 869269123Smav io = beio->io; 870271309Smav flags = 0; 871271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 872271309Smav flags |= IO_DIRECT; 873271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 874271309Smav flags |= IO_SYNC; 875269123Smav 876269123Smav bzero(&xuio, sizeof(xuio)); 877269123Smav if (beio->bio_cmd == BIO_READ) { 878292384Smarkj SDT_PROBE0(cbb, , read, file_start); 879269123Smav xuio.uio_rw = UIO_READ; 880269123Smav } else { 881292384Smarkj SDT_PROBE0(cbb, , write, file_start); 882269123Smav xuio.uio_rw = UIO_WRITE; 883269123Smav } 884269123Smav xuio.uio_offset = beio->io_offset; 885269123Smav xuio.uio_resid = beio->io_len; 886269123Smav xuio.uio_segflg = UIO_SYSSPACE; 887269123Smav xuio.uio_iov = beio->xiovecs; 888269123Smav xuio.uio_iovcnt = beio->num_segs; 889269123Smav xuio.uio_td = curthread; 890269123Smav 891269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 892269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 893269123Smav xiovec->iov_len = beio->sg_segs[i].len; 894269123Smav } 895269123Smav 896269123Smav binuptime(&beio->ds_t0); 897269123Smav mtx_lock(&be_lun->io_lock); 898269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 899269123Smav mtx_unlock(&be_lun->io_lock); 900269123Smav 901287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 902287664Smav if (csw) { 903287664Smav if (beio->bio_cmd == BIO_READ) 904287664Smav error = csw->d_read(dev, &xuio, flags); 905287664Smav else 906287664Smav error = csw->d_write(dev, &xuio, flags); 907287664Smav dev_relthread(dev, ref); 908287664Smav } else 909287664Smav error = ENXIO; 910287664Smav 911287664Smav if (beio->bio_cmd == BIO_READ) 912292384Smarkj SDT_PROBE0(cbb, , read, file_done); 913287664Smav else 914292384Smarkj SDT_PROBE0(cbb, , write, file_done); 915269123Smav 916269123Smav mtx_lock(&be_lun->io_lock); 917269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 918269123Smav beio->ds_tag_type, beio->ds_trans_type, 919269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 920269123Smav mtx_unlock(&be_lun->io_lock); 921269123Smav 922269123Smav /* 923269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 924269123Smav * return the I/O to the user. 925269123Smav */ 926269123Smav if (error != 0) { 927282565Smav if (error == ENOSPC || error == EDQUOT) { 928273809Smav ctl_set_space_alloc_fail(&io->scsiio); 929287760Smav } else if (error == EROFS || error == EACCES) { 930287760Smav ctl_set_hw_write_protected(&io->scsiio); 931287912Smav } else { 932287912Smav ctl_set_medium_error(&io->scsiio, 933287912Smav beio->bio_cmd == BIO_READ); 934287912Smav } 935269123Smav ctl_complete_beio(beio); 936269123Smav return; 937269123Smav } 938269123Smav 939269123Smav /* 940269123Smav * If this is a write or a verify, we're all done. 941269123Smav * If this is a read, we can now send the data to the user. 942269123Smav */ 943269123Smav if ((beio->bio_cmd == BIO_WRITE) || 944269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 945269123Smav ctl_set_success(&io->scsiio); 946269123Smav ctl_complete_beio(beio); 947269123Smav } else { 948275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 949287967Smav beio->beio_cont == NULL) { 950275058Smav ctl_set_success(&io->scsiio); 951287967Smav ctl_serseq_done(io); 952287967Smav } 953269123Smav#ifdef CTL_TIME_IO 954288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 955288215Smav#endif 956269123Smav ctl_datamove(io); 957269123Smav } 958269123Smav} 959269123Smav 960269123Smavstatic void 961275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 962275474Smav struct ctl_be_block_io *beio) 963275474Smav{ 964275474Smav union ctl_io *io = beio->io; 965287664Smav struct cdevsw *csw; 966287664Smav struct cdev *dev; 967275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 968275474Smav struct scsi_get_lba_status_data *data; 969275474Smav off_t roff, off; 970287664Smav int error, ref, status; 971275474Smav 972275474Smav DPRINTF("entered\n"); 973275474Smav 974287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 975287664Smav if (csw == NULL) { 976287664Smav status = 0; /* unknown up to the end */ 977287664Smav off = be_lun->size_bytes; 978287664Smav goto done; 979287664Smav } 980287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 981287664Smav error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 982287664Smav curthread); 983275474Smav if (error == 0 && off > roff) 984275474Smav status = 0; /* mapped up to off */ 985275474Smav else { 986287664Smav error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 987287664Smav curthread); 988275474Smav if (error == 0 && off > roff) 989275474Smav status = 1; /* deallocated up to off */ 990275474Smav else { 991275474Smav status = 0; /* unknown up to the end */ 992275474Smav off = be_lun->size_bytes; 993275474Smav } 994275474Smav } 995287664Smav dev_relthread(dev, ref); 996275474Smav 997287664Smavdone: 998275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 999275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 1000287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 1001287499Smav lbalen->lba), data->descr[0].length); 1002275474Smav data->descr[0].status = status; 1003275474Smav 1004275474Smav ctl_complete_beio(beio); 1005275474Smav} 1006275474Smav 1007275474Smavstatic void 1008229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 1009229997Sken struct ctl_be_block_io *beio) 1010229997Sken{ 1011229997Sken struct bio *bio; 1012287664Smav struct cdevsw *csw; 1013287664Smav struct cdev *dev; 1014287664Smav int ref; 1015229997Sken 1016229997Sken DPRINTF("entered\n"); 1017229997Sken 1018229997Sken /* This can't fail, it's a blocking allocation. */ 1019229997Sken bio = g_alloc_bio(); 1020229997Sken 1021229997Sken bio->bio_cmd = BIO_FLUSH; 1022229997Sken bio->bio_offset = 0; 1023229997Sken bio->bio_data = 0; 1024229997Sken bio->bio_done = ctl_be_block_biodone; 1025229997Sken bio->bio_caller1 = beio; 1026229997Sken bio->bio_pblkno = 0; 1027229997Sken 1028229997Sken /* 1029229997Sken * We don't need to acquire the LUN lock here, because we are only 1030229997Sken * sending one bio, and so there is no other context to synchronize 1031229997Sken * with. 1032229997Sken */ 1033229997Sken beio->num_bios_sent = 1; 1034229997Sken beio->send_complete = 1; 1035229997Sken 1036229997Sken binuptime(&beio->ds_t0); 1037267877Smav mtx_lock(&be_lun->io_lock); 1038229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1039267877Smav mtx_unlock(&be_lun->io_lock); 1040229997Sken 1041287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1042287664Smav if (csw) { 1043287664Smav bio->bio_dev = dev; 1044287664Smav csw->d_strategy(bio); 1045287664Smav dev_relthread(dev, ref); 1046287664Smav } else { 1047287664Smav bio->bio_error = ENXIO; 1048287664Smav ctl_be_block_biodone(bio); 1049287664Smav } 1050229997Sken} 1051229997Sken 1052229997Skenstatic void 1053264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1054264274Smav struct ctl_be_block_io *beio, 1055264274Smav uint64_t off, uint64_t len, int last) 1056264274Smav{ 1057264274Smav struct bio *bio; 1058264296Smav uint64_t maxlen; 1059287664Smav struct cdevsw *csw; 1060287664Smav struct cdev *dev; 1061287664Smav int ref; 1062264274Smav 1063287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1064287499Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1065264274Smav while (len > 0) { 1066264274Smav bio = g_alloc_bio(); 1067264274Smav bio->bio_cmd = BIO_DELETE; 1068287664Smav bio->bio_dev = dev; 1069264274Smav bio->bio_offset = off; 1070264296Smav bio->bio_length = MIN(len, maxlen); 1071264274Smav bio->bio_data = 0; 1072264274Smav bio->bio_done = ctl_be_block_biodone; 1073264274Smav bio->bio_caller1 = beio; 1074287499Smav bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1075264274Smav 1076264274Smav off += bio->bio_length; 1077264274Smav len -= bio->bio_length; 1078264274Smav 1079267877Smav mtx_lock(&be_lun->io_lock); 1080264274Smav beio->num_bios_sent++; 1081264274Smav if (last && len == 0) 1082264274Smav beio->send_complete = 1; 1083267877Smav mtx_unlock(&be_lun->io_lock); 1084264274Smav 1085287664Smav if (csw) { 1086287664Smav csw->d_strategy(bio); 1087287664Smav } else { 1088287664Smav bio->bio_error = ENXIO; 1089287664Smav ctl_be_block_biodone(bio); 1090287664Smav } 1091264274Smav } 1092287664Smav if (csw) 1093287664Smav dev_relthread(dev, ref); 1094264274Smav} 1095264274Smav 1096264274Smavstatic void 1097264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1098264274Smav struct ctl_be_block_io *beio) 1099264274Smav{ 1100264274Smav union ctl_io *io; 1101267515Smav struct ctl_ptr_len_flags *ptrlen; 1102264274Smav struct scsi_unmap_desc *buf, *end; 1103264274Smav uint64_t len; 1104264274Smav 1105264274Smav io = beio->io; 1106264274Smav 1107264274Smav DPRINTF("entered\n"); 1108264274Smav 1109264274Smav binuptime(&beio->ds_t0); 1110267877Smav mtx_lock(&be_lun->io_lock); 1111264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1112267877Smav mtx_unlock(&be_lun->io_lock); 1113264274Smav 1114264274Smav if (beio->io_offset == -1) { 1115264274Smav beio->io_len = 0; 1116267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1117267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1118267515Smav end = buf + ptrlen->len / sizeof(*buf); 1119264274Smav for (; buf < end; buf++) { 1120264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1121287499Smav be_lun->cbe_lun.blocksize; 1122264274Smav beio->io_len += len; 1123264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1124287499Smav scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 1125287499Smav len, (end - buf < 2) ? TRUE : FALSE); 1126264274Smav } 1127264274Smav } else 1128264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1129264274Smav beio->io_offset, beio->io_len, TRUE); 1130264274Smav} 1131264274Smav 1132264274Smavstatic void 1133229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1134229997Sken struct ctl_be_block_io *beio) 1135229997Sken{ 1136267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1137229997Sken struct bio *bio; 1138287664Smav struct cdevsw *csw; 1139287664Smav struct cdev *dev; 1140229997Sken off_t cur_offset; 1141287664Smav int i, max_iosize, ref; 1142229997Sken 1143229997Sken DPRINTF("entered\n"); 1144287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1145229997Sken 1146229997Sken /* 1147229997Sken * We have to limit our I/O size to the maximum supported by the 1148229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1149229997Sken * set it properly, use DFLTPHYS. 1150229997Sken */ 1151287664Smav if (csw) { 1152287664Smav max_iosize = dev->si_iosize_max; 1153287664Smav if (max_iosize < PAGE_SIZE) 1154287664Smav max_iosize = DFLTPHYS; 1155287664Smav } else 1156229997Sken max_iosize = DFLTPHYS; 1157229997Sken 1158229997Sken cur_offset = beio->io_offset; 1159229997Sken for (i = 0; i < beio->num_segs; i++) { 1160229997Sken size_t cur_size; 1161229997Sken uint8_t *cur_ptr; 1162229997Sken 1163229997Sken cur_size = beio->sg_segs[i].len; 1164229997Sken cur_ptr = beio->sg_segs[i].addr; 1165229997Sken 1166229997Sken while (cur_size > 0) { 1167229997Sken /* This can't fail, it's a blocking allocation. */ 1168229997Sken bio = g_alloc_bio(); 1169229997Sken 1170229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1171229997Sken 1172229997Sken bio->bio_cmd = beio->bio_cmd; 1173287664Smav bio->bio_dev = dev; 1174229997Sken bio->bio_caller1 = beio; 1175229997Sken bio->bio_length = min(cur_size, max_iosize); 1176229997Sken bio->bio_offset = cur_offset; 1177229997Sken bio->bio_data = cur_ptr; 1178229997Sken bio->bio_done = ctl_be_block_biodone; 1179287499Smav bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1180229997Sken 1181229997Sken cur_offset += bio->bio_length; 1182229997Sken cur_ptr += bio->bio_length; 1183229997Sken cur_size -= bio->bio_length; 1184229997Sken 1185267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1186229997Sken beio->num_bios_sent++; 1187229997Sken } 1188229997Sken } 1189267877Smav binuptime(&beio->ds_t0); 1190267877Smav mtx_lock(&be_lun->io_lock); 1191267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1192267877Smav beio->send_complete = 1; 1193267877Smav mtx_unlock(&be_lun->io_lock); 1194267877Smav 1195267877Smav /* 1196267877Smav * Fire off all allocated requests! 1197267877Smav */ 1198267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1199267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1200287664Smav if (csw) 1201287664Smav csw->d_strategy(bio); 1202287664Smav else { 1203287664Smav bio->bio_error = ENXIO; 1204287664Smav ctl_be_block_biodone(bio); 1205287664Smav } 1206267877Smav } 1207287664Smav if (csw) 1208287664Smav dev_relthread(dev, ref); 1209229997Sken} 1210229997Sken 1211274154Smavstatic uint64_t 1212274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1213274154Smav{ 1214274154Smav struct diocgattr_arg arg; 1215287664Smav struct cdevsw *csw; 1216287664Smav struct cdev *dev; 1217287664Smav int error, ref; 1218274154Smav 1219287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1220287664Smav if (csw == NULL) 1221274154Smav return (UINT64_MAX); 1222274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1223274154Smav arg.len = sizeof(arg.value.off); 1224287664Smav if (csw->d_ioctl) { 1225287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 1226287664Smav curthread); 1227287664Smav } else 1228287664Smav error = ENODEV; 1229287664Smav dev_relthread(dev, ref); 1230274154Smav if (error != 0) 1231274154Smav return (UINT64_MAX); 1232274154Smav return (arg.value.off); 1233274154Smav} 1234274154Smav 1235229997Skenstatic void 1236286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1237286353Smav union ctl_io *io) 1238286353Smav{ 1239287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1240286353Smav struct ctl_be_block_io *beio; 1241286353Smav struct ctl_lba_len_flags *lbalen; 1242286353Smav 1243286353Smav DPRINTF("entered\n"); 1244286353Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1245286353Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1246286353Smav 1247287499Smav beio->io_len = lbalen->len * cbe_lun->blocksize; 1248287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1249286353Smav beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1250286353Smav beio->bio_cmd = BIO_FLUSH; 1251286353Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1252286353Smav DPRINTF("SYNC\n"); 1253286353Smav be_lun->lun_flush(be_lun, beio); 1254286353Smav} 1255286353Smav 1256286353Smavstatic void 1257264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1258264274Smav{ 1259264274Smav union ctl_io *io; 1260264274Smav 1261264274Smav io = beio->io; 1262264274Smav ctl_free_beio(beio); 1263267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1264267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1265267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1266264274Smav ctl_config_write_done(io); 1267264274Smav return; 1268264274Smav } 1269264274Smav 1270264274Smav ctl_be_block_config_write(io); 1271264274Smav} 1272264274Smav 1273264274Smavstatic void 1274264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1275264274Smav union ctl_io *io) 1276264274Smav{ 1277287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1278264274Smav struct ctl_be_block_io *beio; 1279267515Smav struct ctl_lba_len_flags *lbalen; 1280278625Smav uint64_t len_left, lba; 1281278625Smav uint32_t pb, pbo, adj; 1282264274Smav int i, seglen; 1283264274Smav uint8_t *buf, *end; 1284264274Smav 1285264274Smav DPRINTF("entered\n"); 1286264274Smav 1287267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1288267537Smav lbalen = ARGS(beio->io); 1289264274Smav 1290271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1291269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1292264274Smav ctl_free_beio(beio); 1293264274Smav ctl_set_invalid_field(&io->scsiio, 1294264274Smav /*sks_valid*/ 1, 1295264274Smav /*command*/ 1, 1296264274Smav /*field*/ 1, 1297264274Smav /*bit_valid*/ 0, 1298264274Smav /*bit*/ 0); 1299264274Smav ctl_config_write_done(io); 1300264274Smav return; 1301264274Smav } 1302264274Smav 1303269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1304287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1305287499Smav beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1306264274Smav beio->bio_cmd = BIO_DELETE; 1307264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1308264274Smav 1309264274Smav be_lun->unmap(be_lun, beio); 1310264274Smav return; 1311264274Smav } 1312264274Smav 1313264274Smav beio->bio_cmd = BIO_WRITE; 1314264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1315264274Smav 1316264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1317267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1318264274Smav 1319287499Smav pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1320287499Smav if (be_lun->cbe_lun.pblockoff > 0) 1321287499Smav pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1322278625Smav else 1323278625Smav pbo = 0; 1324287499Smav len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1325264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1326264274Smav 1327264274Smav /* 1328264274Smav * Setup the S/G entry for this chunk. 1329264274Smav */ 1330264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1331287499Smav if (pb > cbe_lun->blocksize) { 1332287499Smav adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1333278619Smav seglen - pbo) % pb; 1334278619Smav if (seglen > adj) 1335278619Smav seglen -= adj; 1336278619Smav else 1337287499Smav seglen -= seglen % cbe_lun->blocksize; 1338278619Smav } else 1339287499Smav seglen -= seglen % cbe_lun->blocksize; 1340264274Smav beio->sg_segs[i].len = seglen; 1341264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1342264274Smav 1343264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1344264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1345264274Smav 1346264274Smav beio->num_segs++; 1347264274Smav len_left -= seglen; 1348264274Smav 1349264274Smav buf = beio->sg_segs[i].addr; 1350264274Smav end = buf + seglen; 1351287499Smav for (; buf < end; buf += cbe_lun->blocksize) { 1352288175Smav if (lbalen->flags & SWS_NDOB) { 1353288175Smav memset(buf, 0, cbe_lun->blocksize); 1354288175Smav } else { 1355288175Smav memcpy(buf, io->scsiio.kern_data_ptr, 1356288175Smav cbe_lun->blocksize); 1357288175Smav } 1358267515Smav if (lbalen->flags & SWS_LBDATA) 1359267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1360264274Smav lba++; 1361264274Smav } 1362264274Smav } 1363264274Smav 1364287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1365287499Smav beio->io_len = lba * cbe_lun->blocksize; 1366264274Smav 1367264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1368264274Smav if (len_left > 0) { 1369267515Smav lbalen->lba += lba; 1370267515Smav lbalen->len -= lba; 1371264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1372264274Smav } 1373264274Smav 1374264274Smav be_lun->dispatch(be_lun, beio); 1375264274Smav} 1376264274Smav 1377264274Smavstatic void 1378264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1379264274Smav union ctl_io *io) 1380264274Smav{ 1381264274Smav struct ctl_be_block_io *beio; 1382267515Smav struct ctl_ptr_len_flags *ptrlen; 1383264274Smav 1384264274Smav DPRINTF("entered\n"); 1385264274Smav 1386267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1387267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1388264274Smav 1389269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1390264274Smav ctl_free_beio(beio); 1391264274Smav ctl_set_invalid_field(&io->scsiio, 1392264274Smav /*sks_valid*/ 0, 1393264274Smav /*command*/ 1, 1394264274Smav /*field*/ 0, 1395264274Smav /*bit_valid*/ 0, 1396264274Smav /*bit*/ 0); 1397264274Smav ctl_config_write_done(io); 1398264274Smav return; 1399264274Smav } 1400264274Smav 1401264274Smav beio->io_len = 0; 1402264274Smav beio->io_offset = -1; 1403264274Smav beio->bio_cmd = BIO_DELETE; 1404264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1405267515Smav DPRINTF("UNMAP\n"); 1406264274Smav be_lun->unmap(be_lun, beio); 1407264274Smav} 1408264274Smav 1409264274Smavstatic void 1410275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1411275474Smav{ 1412275474Smav union ctl_io *io; 1413275474Smav 1414275474Smav io = beio->io; 1415275474Smav ctl_free_beio(beio); 1416275474Smav ctl_config_read_done(io); 1417275474Smav} 1418275474Smav 1419275474Smavstatic void 1420275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1421275474Smav union ctl_io *io) 1422275474Smav{ 1423275474Smav struct ctl_be_block_io *beio; 1424275474Smav struct ctl_be_block_softc *softc; 1425275474Smav 1426275474Smav DPRINTF("entered\n"); 1427275474Smav 1428275474Smav softc = be_lun->softc; 1429275474Smav beio = ctl_alloc_beio(softc); 1430275474Smav beio->io = io; 1431275474Smav beio->lun = be_lun; 1432275474Smav beio->beio_cont = ctl_be_block_cr_done; 1433275474Smav PRIV(io)->ptr = (void *)beio; 1434275474Smav 1435275474Smav switch (io->scsiio.cdb[0]) { 1436275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1437275474Smav beio->bio_cmd = -1; 1438275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1439275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1440275474Smav beio->io_len = 0; 1441275474Smav if (be_lun->get_lba_status) 1442275474Smav be_lun->get_lba_status(be_lun, beio); 1443275474Smav else 1444275474Smav ctl_be_block_cr_done(beio); 1445275474Smav break; 1446275474Smav default: 1447275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1448275474Smav break; 1449275474Smav } 1450275474Smav} 1451275474Smav 1452275474Smavstatic void 1453264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1454264274Smav{ 1455264274Smav union ctl_io *io; 1456264274Smav 1457264274Smav io = beio->io; 1458264274Smav ctl_free_beio(beio); 1459264274Smav ctl_config_write_done(io); 1460264274Smav} 1461264274Smav 1462264274Smavstatic void 1463229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1464229997Sken union ctl_io *io) 1465229997Sken{ 1466229997Sken struct ctl_be_block_io *beio; 1467229997Sken struct ctl_be_block_softc *softc; 1468229997Sken 1469229997Sken DPRINTF("entered\n"); 1470229997Sken 1471229997Sken softc = be_lun->softc; 1472229997Sken beio = ctl_alloc_beio(softc); 1473229997Sken beio->io = io; 1474229997Sken beio->lun = be_lun; 1475264274Smav beio->beio_cont = ctl_be_block_cw_done; 1476286353Smav switch (io->scsiio.tag_type) { 1477286353Smav case CTL_TAG_ORDERED: 1478286353Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1479286353Smav break; 1480286353Smav case CTL_TAG_HEAD_OF_QUEUE: 1481286353Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1482286353Smav break; 1483286353Smav case CTL_TAG_UNTAGGED: 1484286353Smav case CTL_TAG_SIMPLE: 1485286353Smav case CTL_TAG_ACA: 1486286353Smav default: 1487286353Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1488286353Smav break; 1489286353Smav } 1490267519Smav PRIV(io)->ptr = (void *)beio; 1491229997Sken 1492229997Sken switch (io->scsiio.cdb[0]) { 1493229997Sken case SYNCHRONIZE_CACHE: 1494229997Sken case SYNCHRONIZE_CACHE_16: 1495286353Smav ctl_be_block_cw_dispatch_sync(be_lun, io); 1496229997Sken break; 1497264274Smav case WRITE_SAME_10: 1498264274Smav case WRITE_SAME_16: 1499264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1500264274Smav break; 1501264274Smav case UNMAP: 1502264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1503264274Smav break; 1504229997Sken default: 1505229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1506229997Sken break; 1507229997Sken } 1508229997Sken} 1509229997Sken 1510292384SmarkjSDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t"); 1511292384SmarkjSDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t"); 1512292384SmarkjSDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t"); 1513292384SmarkjSDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t"); 1514229997Sken 1515229997Skenstatic void 1516264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1517264886Smav{ 1518264886Smav struct ctl_be_block_lun *be_lun; 1519264886Smav union ctl_io *io; 1520264886Smav 1521264886Smav io = beio->io; 1522264886Smav be_lun = beio->lun; 1523264886Smav ctl_free_beio(beio); 1524267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1525267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1526267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1527267537Smav ctl_data_submit_done(io); 1528264886Smav return; 1529264886Smav } 1530264886Smav 1531264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1532264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1533264886Smav 1534267877Smav mtx_lock(&be_lun->queue_lock); 1535264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1536267877Smav mtx_unlock(&be_lun->queue_lock); 1537264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1538264886Smav} 1539264886Smav 1540264886Smavstatic void 1541229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1542229997Sken union ctl_io *io) 1543229997Sken{ 1544287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1545229997Sken struct ctl_be_block_io *beio; 1546229997Sken struct ctl_be_block_softc *softc; 1547267537Smav struct ctl_lba_len_flags *lbalen; 1548267519Smav struct ctl_ptr_len_flags *bptrlen; 1549267519Smav uint64_t len_left, lbas; 1550229997Sken int i; 1551229997Sken 1552229997Sken softc = be_lun->softc; 1553229997Sken 1554229997Sken DPRINTF("entered\n"); 1555229997Sken 1556267537Smav lbalen = ARGS(io); 1557267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1558292384Smarkj SDT_PROBE0(cbb, , write, start); 1559267537Smav } else { 1560292384Smarkj SDT_PROBE0(cbb, , read, start); 1561229997Sken } 1562229997Sken 1563229997Sken beio = ctl_alloc_beio(softc); 1564229997Sken beio->io = io; 1565229997Sken beio->lun = be_lun; 1566267519Smav bptrlen = PRIV(io); 1567267519Smav bptrlen->ptr = (void *)beio; 1568229997Sken 1569229997Sken switch (io->scsiio.tag_type) { 1570229997Sken case CTL_TAG_ORDERED: 1571229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1572229997Sken break; 1573229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1574229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1575229997Sken break; 1576229997Sken case CTL_TAG_UNTAGGED: 1577229997Sken case CTL_TAG_SIMPLE: 1578229997Sken case CTL_TAG_ACA: 1579229997Sken default: 1580229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1581229997Sken break; 1582229997Sken } 1583229997Sken 1584267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1585267537Smav beio->bio_cmd = BIO_WRITE; 1586267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1587267537Smav } else { 1588229997Sken beio->bio_cmd = BIO_READ; 1589229997Sken beio->ds_trans_type = DEVSTAT_READ; 1590229997Sken } 1591229997Sken 1592264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1593229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1594267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1595267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1596267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1597267537Smav else 1598267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1599287499Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 1600287499Smav beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 1601287499Smav beio->io_len = lbas * cbe_lun->blocksize; 1602267519Smav bptrlen->len += lbas; 1603229997Sken 1604264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1605264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1606264886Smav i, CTLBLK_MAX_SEGS)); 1607229997Sken 1608229997Sken /* 1609229997Sken * Setup the S/G entry for this chunk. 1610229997Sken */ 1611264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1612229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1613229997Sken 1614229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1615229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1616229997Sken 1617267537Smav /* Set up second segment for compare operation. */ 1618267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1619267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1620267537Smav beio->sg_segs[i].len; 1621267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1622267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1623267537Smav } 1624267537Smav 1625229997Sken beio->num_segs++; 1626229997Sken len_left -= beio->sg_segs[i].len; 1627229997Sken } 1628267519Smav if (bptrlen->len < lbalen->len) 1629264886Smav beio->beio_cont = ctl_be_block_next; 1630264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1631267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1632267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1633267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1634267537Smav else 1635267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1636264886Smav io->scsiio.kern_data_len = beio->io_len; 1637264886Smav io->scsiio.kern_data_resid = 0; 1638264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1639288020Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 1640229997Sken 1641229997Sken /* 1642229997Sken * For the read case, we need to read the data into our buffers and 1643229997Sken * then we can send it back to the user. For the write case, we 1644229997Sken * need to get the data from the user first. 1645229997Sken */ 1646229997Sken if (beio->bio_cmd == BIO_READ) { 1647292384Smarkj SDT_PROBE0(cbb, , read, alloc_done); 1648229997Sken be_lun->dispatch(be_lun, beio); 1649229997Sken } else { 1650292384Smarkj SDT_PROBE0(cbb, , write, alloc_done); 1651229997Sken#ifdef CTL_TIME_IO 1652288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 1653288215Smav#endif 1654229997Sken ctl_datamove(io); 1655229997Sken } 1656229997Sken} 1657229997Sken 1658229997Skenstatic void 1659229997Skenctl_be_block_worker(void *context, int pending) 1660229997Sken{ 1661287670Smav struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1662287670Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1663229997Sken union ctl_io *io; 1664287670Smav struct ctl_be_block_io *beio; 1665229997Sken 1666229997Sken DPRINTF("entered\n"); 1667287670Smav /* 1668287670Smav * Fetch and process I/Os from all queues. If we detect LUN 1669288348Smav * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race, 1670287670Smav * so make response maximally opaque to not confuse initiator. 1671287670Smav */ 1672229997Sken for (;;) { 1673287670Smav mtx_lock(&be_lun->queue_lock); 1674229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1675229997Sken if (io != NULL) { 1676229997Sken DPRINTF("datamove queue\n"); 1677229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1678229997Sken ctl_io_hdr, links); 1679267877Smav mtx_unlock(&be_lun->queue_lock); 1680267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1681288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1682287670Smav ctl_set_busy(&io->scsiio); 1683287670Smav ctl_complete_beio(beio); 1684287670Smav return; 1685287670Smav } 1686229997Sken be_lun->dispatch(be_lun, beio); 1687229997Sken continue; 1688229997Sken } 1689229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1690229997Sken if (io != NULL) { 1691229997Sken DPRINTF("config write queue\n"); 1692229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1693229997Sken ctl_io_hdr, links); 1694267877Smav mtx_unlock(&be_lun->queue_lock); 1695288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1696287670Smav ctl_set_busy(&io->scsiio); 1697287670Smav ctl_config_write_done(io); 1698287670Smav return; 1699287670Smav } 1700229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1701229997Sken continue; 1702229997Sken } 1703275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1704275474Smav if (io != NULL) { 1705275474Smav DPRINTF("config read queue\n"); 1706275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1707275474Smav ctl_io_hdr, links); 1708275474Smav mtx_unlock(&be_lun->queue_lock); 1709288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1710287670Smav ctl_set_busy(&io->scsiio); 1711287670Smav ctl_config_read_done(io); 1712287670Smav return; 1713287670Smav } 1714275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1715275474Smav continue; 1716275474Smav } 1717229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1718229997Sken if (io != NULL) { 1719229997Sken DPRINTF("input queue\n"); 1720229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1721229997Sken ctl_io_hdr, links); 1722267877Smav mtx_unlock(&be_lun->queue_lock); 1723288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1724287670Smav ctl_set_busy(&io->scsiio); 1725287670Smav ctl_data_submit_done(io); 1726287670Smav return; 1727287670Smav } 1728229997Sken ctl_be_block_dispatch(be_lun, io); 1729229997Sken continue; 1730229997Sken } 1731229997Sken 1732229997Sken /* 1733229997Sken * If we get here, there is no work left in the queues, so 1734229997Sken * just break out and let the task queue go to sleep. 1735229997Sken */ 1736287670Smav mtx_unlock(&be_lun->queue_lock); 1737229997Sken break; 1738229997Sken } 1739229997Sken} 1740229997Sken 1741229997Sken/* 1742229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1743229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1744229997Sken * thread. 1745229997Sken */ 1746229997Skenstatic int 1747229997Skenctl_be_block_submit(union ctl_io *io) 1748229997Sken{ 1749229997Sken struct ctl_be_block_lun *be_lun; 1750287499Smav struct ctl_be_lun *cbe_lun; 1751229997Sken 1752229997Sken DPRINTF("entered\n"); 1753229997Sken 1754312834Smav cbe_lun = CTL_BACKEND_LUN(io); 1755287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1756229997Sken 1757229997Sken /* 1758229997Sken * Make sure we only get SCSI I/O. 1759229997Sken */ 1760229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1761229997Sken "%#x) encountered", io->io_hdr.io_type)); 1762229997Sken 1763267519Smav PRIV(io)->len = 0; 1764267519Smav 1765267877Smav mtx_lock(&be_lun->queue_lock); 1766229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1767267877Smav mtx_unlock(&be_lun->queue_lock); 1768229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1769229997Sken 1770267514Smav return (CTL_RETVAL_COMPLETE); 1771229997Sken} 1772229997Sken 1773229997Skenstatic int 1774229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1775229997Sken int flag, struct thread *td) 1776229997Sken{ 1777229997Sken struct ctl_be_block_softc *softc; 1778229997Sken int error; 1779229997Sken 1780229997Sken softc = &backend_block_softc; 1781229997Sken 1782229997Sken error = 0; 1783229997Sken 1784229997Sken switch (cmd) { 1785229997Sken case CTL_LUN_REQ: { 1786229997Sken struct ctl_lun_req *lun_req; 1787229997Sken 1788229997Sken lun_req = (struct ctl_lun_req *)addr; 1789229997Sken 1790229997Sken switch (lun_req->reqtype) { 1791229997Sken case CTL_LUNREQ_CREATE: 1792229997Sken error = ctl_be_block_create(softc, lun_req); 1793229997Sken break; 1794229997Sken case CTL_LUNREQ_RM: 1795229997Sken error = ctl_be_block_rm(softc, lun_req); 1796229997Sken break; 1797232604Strasz case CTL_LUNREQ_MODIFY: 1798232604Strasz error = ctl_be_block_modify(softc, lun_req); 1799232604Strasz break; 1800229997Sken default: 1801229997Sken lun_req->status = CTL_LUN_ERROR; 1802229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1803272911Smav "invalid LUN request type %d", 1804229997Sken lun_req->reqtype); 1805229997Sken break; 1806229997Sken } 1807229997Sken break; 1808229997Sken } 1809229997Sken default: 1810229997Sken error = ENOTTY; 1811229997Sken break; 1812229997Sken } 1813229997Sken 1814229997Sken return (error); 1815229997Sken} 1816229997Sken 1817229997Skenstatic int 1818229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1819229997Sken{ 1820287499Smav struct ctl_be_lun *cbe_lun; 1821229997Sken struct ctl_be_block_filedata *file_data; 1822229997Sken struct ctl_lun_create_params *params; 1823275865Smav char *value; 1824229997Sken struct vattr vattr; 1825275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1826229997Sken int error; 1827229997Sken 1828287499Smav cbe_lun = &be_lun->cbe_lun; 1829229997Sken file_data = &be_lun->backend.file; 1830272911Smav params = &be_lun->params; 1831229997Sken 1832229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1833229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1834229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1835275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1836275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1837287499Smav be_lun->unmap = NULL; 1838287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1839229997Sken 1840229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1841229997Sken if (error != 0) { 1842229997Sken snprintf(req->error_str, sizeof(req->error_str), 1843229997Sken "error calling VOP_GETATTR() for file %s", 1844229997Sken be_lun->dev_path); 1845229997Sken return (error); 1846229997Sken } 1847229997Sken 1848229997Sken file_data->cred = crhold(curthread->td_ucred); 1849232604Strasz if (params->lun_size_bytes != 0) 1850232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1851232604Strasz else 1852232604Strasz be_lun->size_bytes = vattr.va_size; 1853229997Sken 1854229997Sken /* 1855273029Smav * For files we can use any logical block size. Prefer 512 bytes 1856273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1857273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1858273029Smav * logical block size -- report it as physical block size. 1859229997Sken */ 1860229997Sken if (params->blocksize_bytes != 0) 1861287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1862288310Smav else if (cbe_lun->lun_type == T_CDROM) 1863288310Smav cbe_lun->blocksize = 2048; 1864229997Sken else 1865287499Smav cbe_lun->blocksize = 512; 1866287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1867287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1868287499Smav 0 : (be_lun->size_blocks - 1); 1869275865Smav 1870275865Smav us = ps = vattr.va_blocksize; 1871275865Smav uo = po = 0; 1872275865Smav 1873287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1874275865Smav if (value != NULL) 1875275865Smav ctl_expand_number(value, &ps); 1876287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1877275865Smav if (value != NULL) 1878275865Smav ctl_expand_number(value, &po); 1879287499Smav pss = ps / cbe_lun->blocksize; 1880287499Smav pos = po / cbe_lun->blocksize; 1881287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1882287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1883287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1884287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1885273029Smav } 1886229997Sken 1887287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1888275865Smav if (value != NULL) 1889275865Smav ctl_expand_number(value, &us); 1890287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1891275865Smav if (value != NULL) 1892275865Smav ctl_expand_number(value, &uo); 1893287499Smav uss = us / cbe_lun->blocksize; 1894287499Smav uos = uo / cbe_lun->blocksize; 1895287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1896287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1897287499Smav cbe_lun->ublockexp = fls(uss) - 1; 1898287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 1899275865Smav } 1900275865Smav 1901229997Sken /* 1902229997Sken * Sanity check. The media size has to be at least one 1903229997Sken * sector long. 1904229997Sken */ 1905287499Smav if (be_lun->size_bytes < cbe_lun->blocksize) { 1906229997Sken error = EINVAL; 1907229997Sken snprintf(req->error_str, sizeof(req->error_str), 1908229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1909287499Smav (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1910229997Sken } 1911275920Smav 1912287499Smav cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1913229997Sken return (error); 1914229997Sken} 1915229997Sken 1916229997Skenstatic int 1917229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1918229997Sken{ 1919287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1920229997Sken struct ctl_lun_create_params *params; 1921287664Smav struct cdevsw *csw; 1922229997Sken struct cdev *dev; 1923275865Smav char *value; 1924287664Smav int error, atomic, maxio, ref, unmap, tmp; 1925287221Smav off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1926229997Sken 1927272911Smav params = &be_lun->params; 1928229997Sken 1929229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1930287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1931287664Smav if (csw == NULL) 1932287664Smav return (ENXIO); 1933287664Smav if (strcmp(csw->d_name, "zvol") == 0) { 1934269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1935275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1936275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1937275920Smav } else { 1938269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1939287499Smav be_lun->get_lba_status = NULL; 1940275920Smav atomic = 0; 1941287664Smav maxio = dev->si_iosize_max; 1942275920Smav if (maxio <= 0) 1943275920Smav maxio = DFLTPHYS; 1944275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1945275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1946275920Smav } 1947269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1948274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1949287499Smav be_lun->unmap = ctl_be_block_unmap_dev; 1950229997Sken 1951287664Smav if (!csw->d_ioctl) { 1952287664Smav dev_relthread(dev, ref); 1953229997Sken snprintf(req->error_str, sizeof(req->error_str), 1954287664Smav "no d_ioctl for device %s!", be_lun->dev_path); 1955229997Sken return (ENODEV); 1956229997Sken } 1957229997Sken 1958287664Smav error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1959229997Sken curthread); 1960229997Sken if (error) { 1961287664Smav dev_relthread(dev, ref); 1962229997Sken snprintf(req->error_str, sizeof(req->error_str), 1963272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1964272911Smav "on %s!", error, be_lun->dev_path); 1965229997Sken return (error); 1966229997Sken } 1967229997Sken 1968229997Sken /* 1969229997Sken * If the user has asked for a blocksize that is greater than the 1970229997Sken * backing device's blocksize, we can do it only if the blocksize 1971229997Sken * the user is asking for is an even multiple of the underlying 1972229997Sken * device's blocksize. 1973229997Sken */ 1974286811Smav if ((params->blocksize_bytes != 0) && 1975286811Smav (params->blocksize_bytes >= tmp)) { 1976286811Smav if (params->blocksize_bytes % tmp == 0) { 1977287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1978229997Sken } else { 1979287664Smav dev_relthread(dev, ref); 1980229997Sken snprintf(req->error_str, sizeof(req->error_str), 1981272911Smav "requested blocksize %u is not an even " 1982229997Sken "multiple of backing device blocksize %u", 1983287221Smav params->blocksize_bytes, tmp); 1984229997Sken return (EINVAL); 1985229997Sken } 1986286811Smav } else if (params->blocksize_bytes != 0) { 1987287664Smav dev_relthread(dev, ref); 1988229997Sken snprintf(req->error_str, sizeof(req->error_str), 1989272911Smav "requested blocksize %u < backing device " 1990287221Smav "blocksize %u", params->blocksize_bytes, tmp); 1991229997Sken return (EINVAL); 1992288310Smav } else if (cbe_lun->lun_type == T_CDROM) 1993288310Smav cbe_lun->blocksize = MAX(tmp, 2048); 1994288310Smav else 1995287499Smav cbe_lun->blocksize = tmp; 1996229997Sken 1997287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1998287664Smav curthread); 1999229997Sken if (error) { 2000287664Smav dev_relthread(dev, ref); 2001229997Sken snprintf(req->error_str, sizeof(req->error_str), 2002272911Smav "error %d returned for DIOCGMEDIASIZE " 2003272911Smav " ioctl on %s!", error, 2004232604Strasz be_lun->dev_path); 2005229997Sken return (error); 2006229997Sken } 2007229997Sken 2008232604Strasz if (params->lun_size_bytes != 0) { 2009287221Smav if (params->lun_size_bytes > otmp) { 2010287664Smav dev_relthread(dev, ref); 2011232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2012272911Smav "requested LUN size %ju > backing device " 2013272911Smav "size %ju", 2014232604Strasz (uintmax_t)params->lun_size_bytes, 2015287221Smav (uintmax_t)otmp); 2016232604Strasz return (EINVAL); 2017232604Strasz } 2018232604Strasz 2019232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2020286811Smav } else 2021287221Smav be_lun->size_bytes = otmp; 2022287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2023287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2024287499Smav 0 : (be_lun->size_blocks - 1); 2025232604Strasz 2026287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 2027287664Smav curthread); 2028264191Smav if (error) 2029264191Smav ps = po = 0; 2030264191Smav else { 2031287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2032287664Smav FREAD, curthread); 2033264191Smav if (error) 2034264191Smav po = 0; 2035264191Smav } 2036275865Smav us = ps; 2037275865Smav uo = po; 2038275865Smav 2039287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 2040275865Smav if (value != NULL) 2041275865Smav ctl_expand_number(value, &ps); 2042287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 2043275865Smav if (value != NULL) 2044275865Smav ctl_expand_number(value, &po); 2045287499Smav pss = ps / cbe_lun->blocksize; 2046287499Smav pos = po / cbe_lun->blocksize; 2047287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2048287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2049287499Smav cbe_lun->pblockexp = fls(pss) - 1; 2050287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 2051264191Smav } 2052264191Smav 2053287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 2054275865Smav if (value != NULL) 2055275865Smav ctl_expand_number(value, &us); 2056287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 2057275865Smav if (value != NULL) 2058275865Smav ctl_expand_number(value, &uo); 2059287499Smav uss = us / cbe_lun->blocksize; 2060287499Smav uos = uo / cbe_lun->blocksize; 2061287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2062287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2063287499Smav cbe_lun->ublockexp = fls(uss) - 1; 2064287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 2065275865Smav } 2066275865Smav 2067287499Smav cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2068287499Smav cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2069278672Smav 2070278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2071278672Smav unmap = 1; 2072278672Smav } else { 2073278672Smav struct diocgattr_arg arg; 2074278672Smav 2075278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2076278672Smav arg.len = sizeof(arg.value.i); 2077287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2078287664Smav curthread); 2079278672Smav unmap = (error == 0) ? arg.value.i : 0; 2080278672Smav } 2081287499Smav value = ctl_get_opt(&cbe_lun->options, "unmap"); 2082278672Smav if (value != NULL) 2083278672Smav unmap = (strcmp(value, "on") == 0); 2084278672Smav if (unmap) 2085287499Smav cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2086287499Smav else 2087287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2088278672Smav 2089287664Smav dev_relthread(dev, ref); 2090229997Sken return (0); 2091229997Sken} 2092229997Sken 2093229997Skenstatic int 2094229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2095229997Sken{ 2096287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2097287499Smav int flags; 2098287499Smav 2099229997Sken if (be_lun->vn) { 2100287499Smav flags = FREAD; 2101287499Smav if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2102287499Smav flags |= FWRITE; 2103229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2104229997Sken be_lun->vn = NULL; 2105229997Sken 2106229997Sken switch (be_lun->dev_type) { 2107229997Sken case CTL_BE_BLOCK_DEV: 2108229997Sken break; 2109229997Sken case CTL_BE_BLOCK_FILE: 2110229997Sken if (be_lun->backend.file.cred != NULL) { 2111229997Sken crfree(be_lun->backend.file.cred); 2112229997Sken be_lun->backend.file.cred = NULL; 2113229997Sken } 2114229997Sken break; 2115229997Sken case CTL_BE_BLOCK_NONE: 2116258871Strasz break; 2117229997Sken default: 2118289702Smav panic("Unexpected backend type %d", be_lun->dev_type); 2119229997Sken break; 2120229997Sken } 2121272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2122229997Sken } 2123229997Sken return (0); 2124229997Sken} 2125229997Sken 2126229997Skenstatic int 2127288348Smavctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2128229997Sken{ 2129287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2130229997Sken struct nameidata nd; 2131287499Smav char *value; 2132287499Smav int error, flags; 2133229997Sken 2134229997Sken error = 0; 2135229997Sken if (rootvnode == NULL) { 2136229997Sken snprintf(req->error_str, sizeof(req->error_str), 2137272911Smav "Root filesystem is not mounted"); 2138229997Sken return (1); 2139229997Sken } 2140285391Smjg pwd_ensure_dirs(); 2141229997Sken 2142287499Smav value = ctl_get_opt(&cbe_lun->options, "file"); 2143287499Smav if (value == NULL) { 2144287499Smav snprintf(req->error_str, sizeof(req->error_str), 2145287499Smav "no file argument specified"); 2146287499Smav return (1); 2147287499Smav } 2148287499Smav free(be_lun->dev_path, M_CTLBLK); 2149287499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2150287499Smav 2151287499Smav flags = FREAD; 2152287499Smav value = ctl_get_opt(&cbe_lun->options, "readonly"); 2153288310Smav if (value != NULL) { 2154288310Smav if (strcmp(value, "on") != 0) 2155288310Smav flags |= FWRITE; 2156288310Smav } else if (cbe_lun->lun_type == T_DIRECT) 2157287499Smav flags |= FWRITE; 2158287499Smav 2159287499Smavagain: 2160229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2161229997Sken error = vn_open(&nd, &flags, 0, NULL); 2162287499Smav if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2163287499Smav flags &= ~FWRITE; 2164287499Smav goto again; 2165287499Smav } 2166229997Sken if (error) { 2167229997Sken /* 2168229997Sken * This is the only reasonable guess we can make as far as 2169229997Sken * path if the user doesn't give us a fully qualified path. 2170229997Sken * If they want to specify a file, they need to specify the 2171229997Sken * full path. 2172229997Sken */ 2173229997Sken if (be_lun->dev_path[0] != '/') { 2174229997Sken char *dev_name; 2175229997Sken 2176287499Smav asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2177287499Smav be_lun->dev_path); 2178287499Smav free(be_lun->dev_path, M_CTLBLK); 2179287499Smav be_lun->dev_path = dev_name; 2180287499Smav goto again; 2181229997Sken } 2182229997Sken snprintf(req->error_str, sizeof(req->error_str), 2183272911Smav "error opening %s: %d", be_lun->dev_path, error); 2184229997Sken return (error); 2185229997Sken } 2186287499Smav if (flags & FWRITE) 2187287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2188287499Smav else 2189287499Smav cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2190229997Sken 2191229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2192229997Sken be_lun->vn = nd.ni_vp; 2193229997Sken 2194229997Sken /* We only support disks and files. */ 2195229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2196229997Sken error = ctl_be_block_open_dev(be_lun, req); 2197229997Sken } else if (be_lun->vn->v_type == VREG) { 2198229997Sken error = ctl_be_block_open_file(be_lun, req); 2199229997Sken } else { 2200229997Sken error = EINVAL; 2201229997Sken snprintf(req->error_str, sizeof(req->error_str), 2202258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2203229997Sken } 2204229997Sken VOP_UNLOCK(be_lun->vn, 0); 2205229997Sken 2206286811Smav if (error != 0) 2207229997Sken ctl_be_block_close(be_lun); 2208287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2209287499Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2210287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2211287499Smav value = ctl_get_opt(&cbe_lun->options, "serseq"); 2212287499Smav if (value != NULL && strcmp(value, "on") == 0) 2213287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2214287499Smav else if (value != NULL && strcmp(value, "read") == 0) 2215287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2216287499Smav else if (value != NULL && strcmp(value, "off") == 0) 2217287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2218229997Sken return (0); 2219229997Sken} 2220229997Sken 2221229997Skenstatic int 2222229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2223229997Sken{ 2224287499Smav struct ctl_be_lun *cbe_lun; 2225229997Sken struct ctl_be_block_lun *be_lun; 2226229997Sken struct ctl_lun_create_params *params; 2227267481Smav char num_thread_str[16]; 2228229997Sken char tmpstr[32]; 2229267481Smav char *value; 2230278672Smav int retval, num_threads; 2231267481Smav int tmp_num_threads; 2232229997Sken 2233229997Sken params = &req->reqdata.create; 2234229997Sken retval = 0; 2235272911Smav req->status = CTL_LUN_OK; 2236229997Sken 2237229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2238287499Smav cbe_lun = &be_lun->cbe_lun; 2239287499Smav cbe_lun->be_lun = be_lun; 2240272911Smav be_lun->params = req->reqdata.create; 2241229997Sken be_lun->softc = softc; 2242229997Sken STAILQ_INIT(&be_lun->input_queue); 2243275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2244229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2245229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2246229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2247267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2248267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2249287499Smav ctl_init_opts(&cbe_lun->options, 2250268280Smav req->num_be_args, req->kern_be_args); 2251264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2252256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2253229997Sken if (be_lun->lun_zone == NULL) { 2254229997Sken snprintf(req->error_str, sizeof(req->error_str), 2255272911Smav "error allocating UMA zone"); 2256229997Sken goto bailout_error; 2257229997Sken } 2258229997Sken 2259229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2260287499Smav cbe_lun->lun_type = params->device_type; 2261229997Sken else 2262287499Smav cbe_lun->lun_type = T_DIRECT; 2263287499Smav be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2264287621Smav cbe_lun->flags = 0; 2265287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2266287621Smav if (value != NULL) { 2267287621Smav if (strcmp(value, "primary") == 0) 2268287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2269287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2270287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2271229997Sken 2272288310Smav if (cbe_lun->lun_type == T_DIRECT || 2273288310Smav cbe_lun->lun_type == T_CDROM) { 2274286811Smav be_lun->size_bytes = params->lun_size_bytes; 2275286811Smav if (params->blocksize_bytes != 0) 2276287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2277288310Smav else if (cbe_lun->lun_type == T_CDROM) 2278288310Smav cbe_lun->blocksize = 2048; 2279286811Smav else 2280287499Smav cbe_lun->blocksize = 512; 2281287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2282287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2283287499Smav 0 : (be_lun->size_blocks - 1); 2284229997Sken 2285287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2286287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2287288348Smav retval = ctl_be_block_open(be_lun, req); 2288287621Smav if (retval != 0) { 2289287621Smav retval = 0; 2290287621Smav req->status = CTL_LUN_WARNING; 2291287621Smav } 2292229997Sken } 2293287499Smav num_threads = cbb_num_threads; 2294229997Sken } else { 2295229997Sken num_threads = 1; 2296229997Sken } 2297229997Sken 2298287499Smav value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2299267481Smav if (value != NULL) { 2300267481Smav tmp_num_threads = strtol(value, NULL, 0); 2301229997Sken 2302267481Smav /* 2303267481Smav * We don't let the user specify less than one 2304267481Smav * thread, but hope he's clueful enough not to 2305267481Smav * specify 1000 threads. 2306267481Smav */ 2307267481Smav if (tmp_num_threads < 1) { 2308267481Smav snprintf(req->error_str, sizeof(req->error_str), 2309272911Smav "invalid number of threads %s", 2310272911Smav num_thread_str); 2311267481Smav goto bailout_error; 2312229997Sken } 2313267481Smav num_threads = tmp_num_threads; 2314229997Sken } 2315229997Sken 2316272911Smav if (be_lun->vn == NULL) 2317288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2318229997Sken /* Tell the user the blocksize we ended up using */ 2319272911Smav params->lun_size_bytes = be_lun->size_bytes; 2320287499Smav params->blocksize_bytes = cbe_lun->blocksize; 2321229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2322287499Smav cbe_lun->req_lun_id = params->req_lun_id; 2323287499Smav cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2324229997Sken } else 2325287499Smav cbe_lun->req_lun_id = 0; 2326229997Sken 2327287499Smav cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2328287499Smav cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2329287499Smav cbe_lun->be = &ctl_be_block_driver; 2330229997Sken 2331229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2332229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2333229997Sken softc->num_luns); 2334287499Smav strncpy((char *)cbe_lun->serial_num, tmpstr, 2335287499Smav MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2336229997Sken 2337229997Sken /* Tell the user what we used for a serial number */ 2338229997Sken strncpy((char *)params->serial_num, tmpstr, 2339275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2340229997Sken } else { 2341287499Smav strncpy((char *)cbe_lun->serial_num, params->serial_num, 2342287499Smav MIN(sizeof(cbe_lun->serial_num), 2343229997Sken sizeof(params->serial_num))); 2344229997Sken } 2345229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2346229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2347287499Smav strncpy((char *)cbe_lun->device_id, tmpstr, 2348287499Smav MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2349229997Sken 2350229997Sken /* Tell the user what we used for a device ID */ 2351229997Sken strncpy((char *)params->device_id, tmpstr, 2352275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2353229997Sken } else { 2354287499Smav strncpy((char *)cbe_lun->device_id, params->device_id, 2355287499Smav MIN(sizeof(cbe_lun->device_id), 2356275953Smav sizeof(params->device_id))); 2357229997Sken } 2358229997Sken 2359229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2360229997Sken 2361229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2362229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2363229997Sken 2364229997Sken if (be_lun->io_taskqueue == NULL) { 2365229997Sken snprintf(req->error_str, sizeof(req->error_str), 2366272911Smav "unable to create taskqueue"); 2367229997Sken goto bailout_error; 2368229997Sken } 2369229997Sken 2370229997Sken /* 2371229997Sken * Note that we start the same number of threads by default for 2372229997Sken * both the file case and the block device case. For the file 2373229997Sken * case, we need multiple threads to allow concurrency, because the 2374229997Sken * vnode interface is designed to be a blocking interface. For the 2375229997Sken * block device case, ZFS zvols at least will block the caller's 2376229997Sken * context in many instances, and so we need multiple threads to 2377229997Sken * overcome that problem. Other block devices don't need as many 2378229997Sken * threads, but they shouldn't cause too many problems. 2379229997Sken * 2380229997Sken * If the user wants to just have a single thread for a block 2381229997Sken * device, he can specify that when the LUN is created, or change 2382229997Sken * the tunable/sysctl to alter the default number of threads. 2383229997Sken */ 2384229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2385229997Sken /*num threads*/num_threads, 2386229997Sken /*priority*/PWAIT, 2387229997Sken /*thread name*/ 2388229997Sken "%s taskq", be_lun->lunname); 2389229997Sken 2390229997Sken if (retval != 0) 2391229997Sken goto bailout_error; 2392229997Sken 2393229997Sken be_lun->num_threads = num_threads; 2394229997Sken 2395229997Sken mtx_lock(&softc->lock); 2396229997Sken softc->num_luns++; 2397229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2398229997Sken 2399229997Sken mtx_unlock(&softc->lock); 2400229997Sken 2401287499Smav retval = ctl_add_lun(&be_lun->cbe_lun); 2402229997Sken if (retval != 0) { 2403229997Sken mtx_lock(&softc->lock); 2404229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2405229997Sken links); 2406229997Sken softc->num_luns--; 2407229997Sken mtx_unlock(&softc->lock); 2408229997Sken snprintf(req->error_str, sizeof(req->error_str), 2409272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2410272911Smav "details", retval); 2411229997Sken retval = 0; 2412229997Sken goto bailout_error; 2413229997Sken } 2414229997Sken 2415229997Sken mtx_lock(&softc->lock); 2416229997Sken 2417229997Sken /* 2418229997Sken * Tell the config_status routine that we're waiting so it won't 2419229997Sken * clean up the LUN in the event of an error. 2420229997Sken */ 2421229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2422229997Sken 2423229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2424229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2425229997Sken if (retval == EINTR) 2426229997Sken break; 2427229997Sken } 2428229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2429229997Sken 2430229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2431229997Sken snprintf(req->error_str, sizeof(req->error_str), 2432272911Smav "LUN configuration error, see dmesg for details"); 2433229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2434229997Sken links); 2435229997Sken softc->num_luns--; 2436229997Sken mtx_unlock(&softc->lock); 2437229997Sken goto bailout_error; 2438229997Sken } else { 2439287499Smav params->req_lun_id = cbe_lun->lun_id; 2440229997Sken } 2441229997Sken 2442229997Sken mtx_unlock(&softc->lock); 2443229997Sken 2444229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2445287499Smav cbe_lun->blocksize, 2446229997Sken DEVSTAT_ALL_SUPPORTED, 2447287499Smav cbe_lun->lun_type 2448229997Sken | DEVSTAT_TYPE_IF_OTHER, 2449229997Sken DEVSTAT_PRIORITY_OTHER); 2450229997Sken 2451229997Sken return (retval); 2452229997Sken 2453229997Skenbailout_error: 2454229997Sken req->status = CTL_LUN_ERROR; 2455229997Sken 2456267429Smav if (be_lun->io_taskqueue != NULL) 2457267429Smav taskqueue_free(be_lun->io_taskqueue); 2458229997Sken ctl_be_block_close(be_lun); 2459267429Smav if (be_lun->dev_path != NULL) 2460267429Smav free(be_lun->dev_path, M_CTLBLK); 2461267429Smav if (be_lun->lun_zone != NULL) 2462267429Smav uma_zdestroy(be_lun->lun_zone); 2463287499Smav ctl_free_opts(&cbe_lun->options); 2464267877Smav mtx_destroy(&be_lun->queue_lock); 2465267877Smav mtx_destroy(&be_lun->io_lock); 2466229997Sken free(be_lun, M_CTLBLK); 2467229997Sken 2468229997Sken return (retval); 2469229997Sken} 2470229997Sken 2471229997Skenstatic int 2472229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2473229997Sken{ 2474229997Sken struct ctl_lun_rm_params *params; 2475229997Sken struct ctl_be_block_lun *be_lun; 2476287670Smav struct ctl_be_lun *cbe_lun; 2477229997Sken int retval; 2478229997Sken 2479229997Sken params = &req->reqdata.rm; 2480229997Sken 2481229997Sken mtx_lock(&softc->lock); 2482229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2483287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2484229997Sken break; 2485229997Sken } 2486229997Sken mtx_unlock(&softc->lock); 2487229997Sken if (be_lun == NULL) { 2488229997Sken snprintf(req->error_str, sizeof(req->error_str), 2489272911Smav "LUN %u is not managed by the block backend", 2490272911Smav params->lun_id); 2491229997Sken goto bailout_error; 2492229997Sken } 2493287670Smav cbe_lun = &be_lun->cbe_lun; 2494229997Sken 2495287670Smav retval = ctl_disable_lun(cbe_lun); 2496229997Sken if (retval != 0) { 2497229997Sken snprintf(req->error_str, sizeof(req->error_str), 2498272911Smav "error %d returned from ctl_disable_lun() for " 2499272911Smav "LUN %d", retval, params->lun_id); 2500229997Sken goto bailout_error; 2501287670Smav } 2502229997Sken 2503287670Smav if (be_lun->vn != NULL) { 2504288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2505288348Smav ctl_lun_no_media(cbe_lun); 2506287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2507287670Smav ctl_be_block_close(be_lun); 2508229997Sken } 2509229997Sken 2510287670Smav retval = ctl_invalidate_lun(cbe_lun); 2511229997Sken if (retval != 0) { 2512229997Sken snprintf(req->error_str, sizeof(req->error_str), 2513272911Smav "error %d returned from ctl_invalidate_lun() for " 2514272911Smav "LUN %d", retval, params->lun_id); 2515229997Sken goto bailout_error; 2516229997Sken } 2517229997Sken 2518229997Sken mtx_lock(&softc->lock); 2519229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2520229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2521229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2522229997Sken if (retval == EINTR) 2523229997Sken break; 2524229997Sken } 2525229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2526229997Sken 2527229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2528229997Sken snprintf(req->error_str, sizeof(req->error_str), 2529272911Smav "interrupted waiting for LUN to be freed"); 2530229997Sken mtx_unlock(&softc->lock); 2531229997Sken goto bailout_error; 2532229997Sken } 2533229997Sken 2534229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2535229997Sken 2536229997Sken softc->num_luns--; 2537229997Sken mtx_unlock(&softc->lock); 2538229997Sken 2539287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2540229997Sken taskqueue_free(be_lun->io_taskqueue); 2541229997Sken 2542229997Sken if (be_lun->disk_stats != NULL) 2543229997Sken devstat_remove_entry(be_lun->disk_stats); 2544229997Sken 2545229997Sken uma_zdestroy(be_lun->lun_zone); 2546229997Sken 2547287670Smav ctl_free_opts(&cbe_lun->options); 2548229997Sken free(be_lun->dev_path, M_CTLBLK); 2549267877Smav mtx_destroy(&be_lun->queue_lock); 2550267877Smav mtx_destroy(&be_lun->io_lock); 2551229997Sken free(be_lun, M_CTLBLK); 2552229997Sken 2553229997Sken req->status = CTL_LUN_OK; 2554229997Sken return (0); 2555229997Sken 2556229997Skenbailout_error: 2557229997Sken req->status = CTL_LUN_ERROR; 2558229997Sken return (0); 2559229997Sken} 2560229997Sken 2561232604Straszstatic int 2562232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2563232604Strasz{ 2564232604Strasz struct ctl_lun_modify_params *params; 2565232604Strasz struct ctl_be_block_lun *be_lun; 2566287500Smav struct ctl_be_lun *cbe_lun; 2567287621Smav char *value; 2568271794Smav uint64_t oldsize; 2569287621Smav int error, wasprim; 2570232604Strasz 2571232604Strasz params = &req->reqdata.modify; 2572232604Strasz 2573232604Strasz mtx_lock(&softc->lock); 2574232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2575287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2576232604Strasz break; 2577232604Strasz } 2578232604Strasz mtx_unlock(&softc->lock); 2579232604Strasz if (be_lun == NULL) { 2580232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2581272911Smav "LUN %u is not managed by the block backend", 2582272911Smav params->lun_id); 2583232604Strasz goto bailout_error; 2584232604Strasz } 2585287500Smav cbe_lun = &be_lun->cbe_lun; 2586232604Strasz 2587287500Smav if (params->lun_size_bytes != 0) 2588287500Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2589287500Smav ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2590232604Strasz 2591287621Smav wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2592287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2593287621Smav if (value != NULL) { 2594287621Smav if (strcmp(value, "primary") == 0) 2595287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2596287621Smav else 2597287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2598287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2599287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2600232604Strasz else 2601287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2602287621Smav if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 2603287621Smav if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 2604287621Smav ctl_lun_primary(cbe_lun); 2605287621Smav else 2606287621Smav ctl_lun_secondary(cbe_lun); 2607287621Smav } 2608232604Strasz 2609287621Smav oldsize = be_lun->size_blocks; 2610287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2611287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2612287621Smav if (be_lun->vn == NULL) 2613288348Smav error = ctl_be_block_open(be_lun, req); 2614287621Smav else if (vn_isdisk(be_lun->vn, &error)) 2615288104Smav error = ctl_be_block_open_dev(be_lun, req); 2616289017Smav else if (be_lun->vn->v_type == VREG) { 2617289017Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2618288104Smav error = ctl_be_block_open_file(be_lun, req); 2619289017Smav VOP_UNLOCK(be_lun->vn, 0); 2620289017Smav } else 2621287621Smav error = EINVAL; 2622288348Smav if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) && 2623287621Smav be_lun->vn != NULL) { 2624288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2625288348Smav ctl_lun_has_media(cbe_lun); 2626288348Smav } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 && 2627288348Smav be_lun->vn == NULL) { 2628288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2629288348Smav ctl_lun_no_media(cbe_lun); 2630287621Smav } 2631288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 2632287621Smav } else { 2633287621Smav if (be_lun->vn != NULL) { 2634288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2635288348Smav ctl_lun_no_media(cbe_lun); 2636287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2637287621Smav error = ctl_be_block_close(be_lun); 2638287621Smav } else 2639287621Smav error = 0; 2640287621Smav } 2641287499Smav if (be_lun->size_blocks != oldsize) 2642287500Smav ctl_lun_capacity_changed(cbe_lun); 2643232604Strasz 2644232604Strasz /* Tell the user the exact size we ended up using */ 2645232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2646232604Strasz 2647272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2648232604Strasz return (0); 2649232604Strasz 2650232604Straszbailout_error: 2651232604Strasz req->status = CTL_LUN_ERROR; 2652232604Strasz return (0); 2653232604Strasz} 2654232604Strasz 2655229997Skenstatic void 2656229997Skenctl_be_block_lun_shutdown(void *be_lun) 2657229997Sken{ 2658229997Sken struct ctl_be_block_lun *lun; 2659229997Sken struct ctl_be_block_softc *softc; 2660229997Sken 2661229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2662229997Sken softc = lun->softc; 2663229997Sken 2664229997Sken mtx_lock(&softc->lock); 2665229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2666229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2667229997Sken wakeup(lun); 2668229997Sken mtx_unlock(&softc->lock); 2669229997Sken} 2670229997Sken 2671229997Skenstatic void 2672229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2673229997Sken{ 2674229997Sken struct ctl_be_block_lun *lun; 2675229997Sken struct ctl_be_block_softc *softc; 2676229997Sken 2677229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2678229997Sken softc = lun->softc; 2679229997Sken 2680229997Sken if (status == CTL_LUN_CONFIG_OK) { 2681229997Sken mtx_lock(&softc->lock); 2682229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2683229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2684229997Sken wakeup(lun); 2685229997Sken mtx_unlock(&softc->lock); 2686229997Sken 2687229997Sken /* 2688229997Sken * We successfully added the LUN, attempt to enable it. 2689229997Sken */ 2690287499Smav if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2691229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2692287499Smav if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2693229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2694229997Sken __func__); 2695229997Sken } 2696229997Sken } 2697229997Sken 2698229997Sken return; 2699229997Sken } 2700229997Sken 2701229997Sken 2702229997Sken mtx_lock(&softc->lock); 2703229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2704229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2705229997Sken wakeup(lun); 2706229997Sken mtx_unlock(&softc->lock); 2707229997Sken} 2708229997Sken 2709229997Sken 2710229997Skenstatic int 2711229997Skenctl_be_block_config_write(union ctl_io *io) 2712229997Sken{ 2713229997Sken struct ctl_be_block_lun *be_lun; 2714287499Smav struct ctl_be_lun *cbe_lun; 2715229997Sken int retval; 2716229997Sken 2717229997Sken DPRINTF("entered\n"); 2718229997Sken 2719312834Smav cbe_lun = CTL_BACKEND_LUN(io); 2720287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2721229997Sken 2722288220Smav retval = 0; 2723229997Sken switch (io->scsiio.cdb[0]) { 2724229997Sken case SYNCHRONIZE_CACHE: 2725229997Sken case SYNCHRONIZE_CACHE_16: 2726264274Smav case WRITE_SAME_10: 2727264274Smav case WRITE_SAME_16: 2728264274Smav case UNMAP: 2729229997Sken /* 2730229997Sken * The upper level CTL code will filter out any CDBs with 2731229997Sken * the immediate bit set and return the proper error. 2732229997Sken * 2733229997Sken * We don't really need to worry about what LBA range the 2734229997Sken * user asked to be synced out. When they issue a sync 2735229997Sken * cache command, we'll sync out the whole thing. 2736229997Sken */ 2737267877Smav mtx_lock(&be_lun->queue_lock); 2738229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2739229997Sken links); 2740267877Smav mtx_unlock(&be_lun->queue_lock); 2741229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2742229997Sken break; 2743229997Sken case START_STOP_UNIT: { 2744229997Sken struct scsi_start_stop_unit *cdb; 2745288348Smav struct ctl_lun_req req; 2746229997Sken 2747229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2748288369Smav if ((cdb->how & SSS_PC_MASK) != 0) { 2749288369Smav ctl_set_success(&io->scsiio); 2750288369Smav ctl_config_write_done(io); 2751288369Smav break; 2752288369Smav } 2753288348Smav if (cdb->how & SSS_START) { 2754288348Smav if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) { 2755288348Smav retval = ctl_be_block_open(be_lun, &req); 2756288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 2757288348Smav if (retval == 0) { 2758288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2759288348Smav ctl_lun_has_media(cbe_lun); 2760288348Smav } else { 2761288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2762288348Smav ctl_lun_no_media(cbe_lun); 2763288348Smav } 2764288348Smav } 2765288348Smav ctl_start_lun(cbe_lun); 2766229997Sken } else { 2767288348Smav ctl_stop_lun(cbe_lun); 2768288348Smav if (cdb->how & SSS_LOEJ) { 2769288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2770288348Smav cbe_lun->flags |= CTL_LUN_FLAG_EJECTED; 2771288348Smav ctl_lun_ejected(cbe_lun); 2772288348Smav if (be_lun->vn != NULL) 2773288348Smav ctl_be_block_close(be_lun); 2774288348Smav } 2775229997Sken } 2776288348Smav 2777288348Smav ctl_set_success(&io->scsiio); 2778229997Sken ctl_config_write_done(io); 2779229997Sken break; 2780229997Sken } 2781288310Smav case PREVENT_ALLOW: 2782288310Smav ctl_set_success(&io->scsiio); 2783288310Smav ctl_config_write_done(io); 2784288310Smav break; 2785229997Sken default: 2786229997Sken ctl_set_invalid_opcode(&io->scsiio); 2787229997Sken ctl_config_write_done(io); 2788229997Sken retval = CTL_RETVAL_COMPLETE; 2789229997Sken break; 2790229997Sken } 2791229997Sken 2792229997Sken return (retval); 2793229997Sken} 2794229997Sken 2795229997Skenstatic int 2796229997Skenctl_be_block_config_read(union ctl_io *io) 2797229997Sken{ 2798275474Smav struct ctl_be_block_lun *be_lun; 2799287499Smav struct ctl_be_lun *cbe_lun; 2800275474Smav int retval = 0; 2801275474Smav 2802275474Smav DPRINTF("entered\n"); 2803275474Smav 2804312834Smav cbe_lun = CTL_BACKEND_LUN(io); 2805287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2806275474Smav 2807275474Smav switch (io->scsiio.cdb[0]) { 2808275474Smav case SERVICE_ACTION_IN: 2809275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2810275474Smav mtx_lock(&be_lun->queue_lock); 2811275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2812275474Smav &io->io_hdr, links); 2813275474Smav mtx_unlock(&be_lun->queue_lock); 2814275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2815275474Smav &be_lun->io_task); 2816275474Smav retval = CTL_RETVAL_QUEUED; 2817275474Smav break; 2818275474Smav } 2819275474Smav ctl_set_invalid_field(&io->scsiio, 2820275474Smav /*sks_valid*/ 1, 2821275474Smav /*command*/ 1, 2822275474Smav /*field*/ 1, 2823275474Smav /*bit_valid*/ 1, 2824275474Smav /*bit*/ 4); 2825275474Smav ctl_config_read_done(io); 2826275474Smav retval = CTL_RETVAL_COMPLETE; 2827275474Smav break; 2828275474Smav default: 2829275474Smav ctl_set_invalid_opcode(&io->scsiio); 2830275474Smav ctl_config_read_done(io); 2831275474Smav retval = CTL_RETVAL_COMPLETE; 2832275474Smav break; 2833275474Smav } 2834275474Smav 2835275474Smav return (retval); 2836229997Sken} 2837229997Sken 2838229997Skenstatic int 2839229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2840229997Sken{ 2841229997Sken struct ctl_be_block_lun *lun; 2842229997Sken int retval; 2843229997Sken 2844229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2845229997Sken 2846268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2847229997Sken if (retval != 0) 2848229997Sken goto bailout; 2849229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2850229997Sken if (retval != 0) 2851229997Sken goto bailout; 2852268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2853229997Sken 2854229997Skenbailout: 2855229997Sken return (retval); 2856229997Sken} 2857229997Sken 2858274154Smavstatic uint64_t 2859274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2860274154Smav{ 2861274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2862274154Smav 2863274154Smav if (lun->getattr == NULL) 2864274154Smav return (UINT64_MAX); 2865274154Smav return (lun->getattr(lun, attrname)); 2866274154Smav} 2867274154Smav 2868229997Skenint 2869229997Skenctl_be_block_init(void) 2870229997Sken{ 2871229997Sken struct ctl_be_block_softc *softc; 2872229997Sken int retval; 2873229997Sken 2874229997Sken softc = &backend_block_softc; 2875229997Sken retval = 0; 2876229997Sken 2877267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2878264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2879264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2880229997Sken STAILQ_INIT(&softc->lun_list); 2881229997Sken 2882229997Sken return (retval); 2883229997Sken} 2884