ctl_backend_block.c revision 286811
1229997Sken/*- 2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp. 3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation 4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation 5229997Sken * All rights reserved. 6229997Sken * 7232604Strasz * Portions of this software were developed by Edward Tomasz Napierala 8232604Strasz * under sponsorship from the FreeBSD Foundation. 9232604Strasz * 10229997Sken * Redistribution and use in source and binary forms, with or without 11229997Sken * modification, are permitted provided that the following conditions 12229997Sken * are met: 13229997Sken * 1. Redistributions of source code must retain the above copyright 14229997Sken * notice, this list of conditions, and the following disclaimer, 15229997Sken * without modification. 16229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17229997Sken * substantially similar to the "NO WARRANTY" disclaimer below 18229997Sken * ("Disclaimer") and any redistribution must be conditioned upon 19229997Sken * including a substantially similar Disclaimer requirement for further 20229997Sken * binary redistribution. 21229997Sken * 22229997Sken * NO WARRANTY 23229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33229997Sken * POSSIBILITY OF SUCH DAMAGES. 34229997Sken * 35229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 36229997Sken */ 37229997Sken/* 38229997Sken * CAM Target Layer driver backend for block devices. 39229997Sken * 40229997Sken * Author: Ken Merry <ken@FreeBSD.org> 41229997Sken */ 42229997Sken#include <sys/cdefs.h> 43229997Sken__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 286811 2015-08-15 18:22:16Z mav $"); 44229997Sken 45229997Sken#include <sys/param.h> 46229997Sken#include <sys/systm.h> 47229997Sken#include <sys/kernel.h> 48229997Sken#include <sys/types.h> 49229997Sken#include <sys/kthread.h> 50229997Sken#include <sys/bio.h> 51229997Sken#include <sys/fcntl.h> 52264274Smav#include <sys/limits.h> 53229997Sken#include <sys/lock.h> 54229997Sken#include <sys/mutex.h> 55229997Sken#include <sys/condvar.h> 56229997Sken#include <sys/malloc.h> 57229997Sken#include <sys/conf.h> 58229997Sken#include <sys/ioccom.h> 59229997Sken#include <sys/queue.h> 60229997Sken#include <sys/sbuf.h> 61229997Sken#include <sys/endian.h> 62229997Sken#include <sys/uio.h> 63229997Sken#include <sys/buf.h> 64229997Sken#include <sys/taskqueue.h> 65229997Sken#include <sys/vnode.h> 66229997Sken#include <sys/namei.h> 67229997Sken#include <sys/mount.h> 68229997Sken#include <sys/disk.h> 69229997Sken#include <sys/fcntl.h> 70229997Sken#include <sys/filedesc.h> 71275474Smav#include <sys/filio.h> 72229997Sken#include <sys/proc.h> 73229997Sken#include <sys/pcpu.h> 74229997Sken#include <sys/module.h> 75229997Sken#include <sys/sdt.h> 76229997Sken#include <sys/devicestat.h> 77229997Sken#include <sys/sysctl.h> 78229997Sken 79229997Sken#include <geom/geom.h> 80229997Sken 81229997Sken#include <cam/cam.h> 82229997Sken#include <cam/scsi/scsi_all.h> 83229997Sken#include <cam/scsi/scsi_da.h> 84229997Sken#include <cam/ctl/ctl_io.h> 85229997Sken#include <cam/ctl/ctl.h> 86229997Sken#include <cam/ctl/ctl_backend.h> 87229997Sken#include <cam/ctl/ctl_ioctl.h> 88229997Sken#include <cam/ctl/ctl_scsi_all.h> 89229997Sken#include <cam/ctl/ctl_error.h> 90229997Sken 91229997Sken/* 92264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 93264886Smav * I/O. If we get an I/O larger than that, we'll split it. 94229997Sken */ 95267537Smav#define CTLBLK_HALF_IO_SIZE (512 * 1024) 96267537Smav#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 97264886Smav#define CTLBLK_MAX_SEG MAXPHYS 98267537Smav#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 99267537Smav#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 100229997Sken 101229997Sken#ifdef CTLBLK_DEBUG 102229997Sken#define DPRINTF(fmt, args...) \ 103229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 104229997Sken#else 105229997Sken#define DPRINTF(fmt, args...) do {} while(0) 106229997Sken#endif 107229997Sken 108267519Smav#define PRIV(io) \ 109267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 110267537Smav#define ARGS(io) \ 111267537Smav ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 112267519Smav 113229997SkenSDT_PROVIDER_DEFINE(cbb); 114229997Sken 115229997Skentypedef enum { 116229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 117229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 118229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 119229997Sken CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 120229997Sken} ctl_be_block_lun_flags; 121229997Sken 122229997Skentypedef enum { 123229997Sken CTL_BE_BLOCK_NONE, 124229997Sken CTL_BE_BLOCK_DEV, 125229997Sken CTL_BE_BLOCK_FILE 126229997Sken} ctl_be_block_type; 127229997Sken 128229997Skenstruct ctl_be_block_devdata { 129229997Sken struct cdev *cdev; 130229997Sken struct cdevsw *csw; 131229997Sken int dev_ref; 132229997Sken}; 133229997Sken 134229997Skenstruct ctl_be_block_filedata { 135229997Sken struct ucred *cred; 136229997Sken}; 137229997Sken 138229997Skenunion ctl_be_block_bedata { 139229997Sken struct ctl_be_block_devdata dev; 140229997Sken struct ctl_be_block_filedata file; 141229997Sken}; 142229997Sken 143229997Skenstruct ctl_be_block_io; 144229997Skenstruct ctl_be_block_lun; 145229997Sken 146229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 147229997Sken struct ctl_be_block_io *beio); 148274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 149274154Smav const char *attrname); 150229997Sken 151229997Sken/* 152229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 153229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 154229997Sken */ 155229997Skenstruct ctl_be_block_lun { 156272911Smav struct ctl_lun_create_params params; 157229997Sken struct ctl_block_disk *disk; 158229997Sken char lunname[32]; 159229997Sken char *dev_path; 160229997Sken ctl_be_block_type dev_type; 161229997Sken struct vnode *vn; 162229997Sken union ctl_be_block_bedata backend; 163229997Sken cbb_dispatch_t dispatch; 164229997Sken cbb_dispatch_t lun_flush; 165264274Smav cbb_dispatch_t unmap; 166275474Smav cbb_dispatch_t get_lba_status; 167274154Smav cbb_getattr_t getattr; 168229997Sken uma_zone_t lun_zone; 169229997Sken uint64_t size_blocks; 170229997Sken uint64_t size_bytes; 171229997Sken uint32_t blocksize; 172264191Smav uint16_t pblockexp; 173264191Smav uint16_t pblockoff; 174275865Smav uint16_t ublockexp; 175275865Smav uint16_t ublockoff; 176275920Smav uint32_t atomicblock; 177275920Smav uint32_t opttxferlen; 178229997Sken struct ctl_be_block_softc *softc; 179229997Sken struct devstat *disk_stats; 180229997Sken ctl_be_block_lun_flags flags; 181229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 182229997Sken struct ctl_be_lun ctl_be_lun; 183229997Sken struct taskqueue *io_taskqueue; 184229997Sken struct task io_task; 185229997Sken int num_threads; 186229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 187275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 188229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 189229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 190267877Smav struct mtx_padalign io_lock; 191267877Smav struct mtx_padalign queue_lock; 192229997Sken}; 193229997Sken 194229997Sken/* 195229997Sken * Overall softc structure for the block backend module. 196229997Sken */ 197229997Skenstruct ctl_be_block_softc { 198229997Sken struct mtx lock; 199229997Sken int num_disks; 200229997Sken STAILQ_HEAD(, ctl_block_disk) disk_list; 201229997Sken int num_luns; 202229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 203229997Sken}; 204229997Sken 205229997Skenstatic struct ctl_be_block_softc backend_block_softc; 206229997Sken 207229997Sken/* 208229997Sken * Per-I/O information. 209229997Sken */ 210229997Skenstruct ctl_be_block_io { 211229997Sken union ctl_io *io; 212229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 213229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 214229997Sken int bio_cmd; 215229997Sken int num_segs; 216229997Sken int num_bios_sent; 217229997Sken int num_bios_done; 218229997Sken int send_complete; 219229997Sken int num_errors; 220229997Sken struct bintime ds_t0; 221229997Sken devstat_tag_type ds_tag_type; 222229997Sken devstat_trans_flags ds_trans_type; 223229997Sken uint64_t io_len; 224229997Sken uint64_t io_offset; 225286353Smav int io_arg; 226229997Sken struct ctl_be_block_softc *softc; 227229997Sken struct ctl_be_block_lun *lun; 228264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 229229997Sken}; 230229997Sken 231229997Skenstatic int cbb_num_threads = 14; 232229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 233229997Sken "CAM Target Layer Block Backend"); 234267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 235229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 236229997Sken 237229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 238229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 239229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 240229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 241229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 242229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 243229997Sken struct ctl_be_block_io *beio); 244229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 245229997Sken struct ctl_be_block_io *beio); 246275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 247275474Smav struct ctl_be_block_io *beio); 248275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 249275481Smav const char *attrname); 250229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 251229997Sken struct ctl_be_block_io *beio); 252264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 253264274Smav struct ctl_be_block_io *beio); 254229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 255229997Sken struct ctl_be_block_io *beio); 256274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 257274154Smav const char *attrname); 258275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 259275474Smav union ctl_io *io); 260229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 261229997Sken union ctl_io *io); 262229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 263229997Sken union ctl_io *io); 264229997Skenstatic void ctl_be_block_worker(void *context, int pending); 265229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 266229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 267229997Sken int flag, struct thread *td); 268229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 269229997Sken struct ctl_lun_req *req); 270229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 271229997Sken struct ctl_lun_req *req); 272229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 273229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 274229997Sken struct ctl_be_block_lun *be_lun, 275229997Sken struct ctl_lun_req *req); 276229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 277229997Sken struct ctl_lun_req *req); 278229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 279229997Sken struct ctl_lun_req *req); 280232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 281232604Strasz struct ctl_lun_req *req); 282232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 283232604Strasz struct ctl_lun_req *req); 284232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 285232604Strasz struct ctl_lun_req *req); 286229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 287229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 288229997Sken ctl_lun_config_status status); 289229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 290229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 291229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 292274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 293229997Skenint ctl_be_block_init(void); 294229997Sken 295229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 296229997Sken{ 297230334Sken .name = "block", 298230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 299230334Sken .init = ctl_be_block_init, 300230334Sken .data_submit = ctl_be_block_submit, 301230334Sken .data_move_done = ctl_be_block_move_done, 302230334Sken .config_read = ctl_be_block_config_read, 303230334Sken .config_write = ctl_be_block_config_write, 304230334Sken .ioctl = ctl_be_block_ioctl, 305274154Smav .lun_info = ctl_be_block_lun_info, 306274154Smav .lun_attr = ctl_be_block_lun_attr 307229997Sken}; 308229997Sken 309229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 310229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 311229997Sken 312264020Straszstatic uma_zone_t beio_zone; 313264020Strasz 314229997Skenstatic struct ctl_be_block_io * 315229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 316229997Sken{ 317229997Sken struct ctl_be_block_io *beio; 318229997Sken 319264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 320264020Strasz beio->softc = softc; 321229997Sken return (beio); 322229997Sken} 323229997Sken 324229997Skenstatic void 325229997Skenctl_free_beio(struct ctl_be_block_io *beio) 326229997Sken{ 327229997Sken int duplicate_free; 328229997Sken int i; 329229997Sken 330229997Sken duplicate_free = 0; 331229997Sken 332229997Sken for (i = 0; i < beio->num_segs; i++) { 333229997Sken if (beio->sg_segs[i].addr == NULL) 334229997Sken duplicate_free++; 335229997Sken 336229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 337229997Sken beio->sg_segs[i].addr = NULL; 338267537Smav 339267537Smav /* For compare we had two equal S/G lists. */ 340267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 341267537Smav uma_zfree(beio->lun->lun_zone, 342267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 343267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 344267537Smav } 345229997Sken } 346229997Sken 347229997Sken if (duplicate_free > 0) { 348229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 349229997Sken duplicate_free, beio->num_segs); 350229997Sken } 351229997Sken 352264020Strasz uma_zfree(beio_zone, beio); 353229997Sken} 354229997Sken 355229997Skenstatic void 356229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 357229997Sken{ 358267877Smav union ctl_io *io = beio->io; 359229997Sken 360264274Smav if (beio->beio_cont != NULL) { 361264274Smav beio->beio_cont(beio); 362264274Smav } else { 363264274Smav ctl_free_beio(beio); 364267537Smav ctl_data_submit_done(io); 365264274Smav } 366229997Sken} 367229997Sken 368229997Skenstatic int 369229997Skenctl_be_block_move_done(union ctl_io *io) 370229997Sken{ 371229997Sken struct ctl_be_block_io *beio; 372229997Sken struct ctl_be_block_lun *be_lun; 373267537Smav struct ctl_lba_len_flags *lbalen; 374229997Sken#ifdef CTL_TIME_IO 375229997Sken struct bintime cur_bt; 376267537Smav#endif 377267537Smav int i; 378229997Sken 379267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 380229997Sken be_lun = beio->lun; 381229997Sken 382229997Sken DPRINTF("entered\n"); 383229997Sken 384229997Sken#ifdef CTL_TIME_IO 385229997Sken getbintime(&cur_bt); 386229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 387229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 388229997Sken io->io_hdr.num_dmas++; 389229997Sken#endif 390267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 391229997Sken 392229997Sken /* 393229997Sken * We set status at this point for read commands, and write 394229997Sken * commands with errors. 395229997Sken */ 396275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 397275058Smav ; 398275058Smav } else if ((io->io_hdr.port_status == 0) && 399267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 400267537Smav lbalen = ARGS(beio->io); 401267537Smav if (lbalen->flags & CTL_LLF_READ) { 402267537Smav ctl_set_success(&io->scsiio); 403267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 404267537Smav /* We have two data blocks ready for comparison. */ 405267537Smav for (i = 0; i < beio->num_segs; i++) { 406267537Smav if (memcmp(beio->sg_segs[i].addr, 407267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 408267537Smav beio->sg_segs[i].len) != 0) 409267537Smav break; 410267537Smav } 411267537Smav if (i < beio->num_segs) 412267537Smav ctl_set_sense(&io->scsiio, 413267537Smav /*current_error*/ 1, 414267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 415267537Smav /*asc*/ 0x1D, 416267537Smav /*ascq*/ 0x00, 417267537Smav SSD_ELEM_NONE); 418267537Smav else 419267537Smav ctl_set_success(&io->scsiio); 420267537Smav } 421275058Smav } else if ((io->io_hdr.port_status != 0) && 422275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 423275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 424229997Sken /* 425229997Sken * For hardware error sense keys, the sense key 426229997Sken * specific value is defined to be a retry count, 427229997Sken * but we use it to pass back an internal FETD 428229997Sken * error code. XXX KDM Hopefully the FETD is only 429229997Sken * using 16 bits for an error code, since that's 430229997Sken * all the space we have in the sks field. 431229997Sken */ 432229997Sken ctl_set_internal_failure(&io->scsiio, 433229997Sken /*sks_valid*/ 1, 434229997Sken /*retry_count*/ 435229997Sken io->io_hdr.port_status); 436229997Sken } 437229997Sken 438229997Sken /* 439229997Sken * If this is a read, or a write with errors, it is done. 440229997Sken */ 441229997Sken if ((beio->bio_cmd == BIO_READ) 442229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 443229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 444229997Sken ctl_complete_beio(beio); 445229997Sken return (0); 446229997Sken } 447229997Sken 448229997Sken /* 449229997Sken * At this point, we have a write and the DMA completed 450229997Sken * successfully. We now have to queue it to the task queue to 451229997Sken * execute the backend I/O. That is because we do blocking 452229997Sken * memory allocations, and in the file backing case, blocking I/O. 453229997Sken * This move done routine is generally called in the SIM's 454229997Sken * interrupt context, and therefore we cannot block. 455229997Sken */ 456267877Smav mtx_lock(&be_lun->queue_lock); 457229997Sken /* 458229997Sken * XXX KDM make sure that links is okay to use at this point. 459229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 460229997Sken * or deal with resource allocation here. 461229997Sken */ 462229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 463267877Smav mtx_unlock(&be_lun->queue_lock); 464229997Sken 465229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 466229997Sken 467229997Sken return (0); 468229997Sken} 469229997Sken 470229997Skenstatic void 471229997Skenctl_be_block_biodone(struct bio *bio) 472229997Sken{ 473229997Sken struct ctl_be_block_io *beio; 474229997Sken struct ctl_be_block_lun *be_lun; 475229997Sken union ctl_io *io; 476261538Smav int error; 477229997Sken 478229997Sken beio = bio->bio_caller1; 479229997Sken be_lun = beio->lun; 480229997Sken io = beio->io; 481229997Sken 482229997Sken DPRINTF("entered\n"); 483229997Sken 484261538Smav error = bio->bio_error; 485267877Smav mtx_lock(&be_lun->io_lock); 486261538Smav if (error != 0) 487229997Sken beio->num_errors++; 488229997Sken 489229997Sken beio->num_bios_done++; 490229997Sken 491229997Sken /* 492229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 493229997Sken * during the free might cause it to complain. 494229997Sken */ 495229997Sken g_destroy_bio(bio); 496229997Sken 497229997Sken /* 498229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 499229997Sken * complete, then we're done. 500229997Sken */ 501229997Sken if ((beio->send_complete == 0) 502229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 503267877Smav mtx_unlock(&be_lun->io_lock); 504229997Sken return; 505229997Sken } 506229997Sken 507229997Sken /* 508229997Sken * At this point, we've verified that we are the last I/O to 509229997Sken * complete, so it's safe to drop the lock. 510229997Sken */ 511267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 512267877Smav beio->ds_tag_type, beio->ds_trans_type, 513267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 514267877Smav mtx_unlock(&be_lun->io_lock); 515229997Sken 516229997Sken /* 517229997Sken * If there are any errors from the backing device, we fail the 518229997Sken * entire I/O with a medium error. 519229997Sken */ 520229997Sken if (beio->num_errors > 0) { 521261538Smav if (error == EOPNOTSUPP) { 522261538Smav ctl_set_invalid_opcode(&io->scsiio); 523282565Smav } else if (error == ENOSPC || error == EDQUOT) { 524273809Smav ctl_set_space_alloc_fail(&io->scsiio); 525261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 526229997Sken /* XXX KDM is there is a better error here? */ 527229997Sken ctl_set_internal_failure(&io->scsiio, 528229997Sken /*sks_valid*/ 1, 529229997Sken /*retry_count*/ 0xbad2); 530229997Sken } else 531229997Sken ctl_set_medium_error(&io->scsiio); 532229997Sken ctl_complete_beio(beio); 533229997Sken return; 534229997Sken } 535229997Sken 536229997Sken /* 537267537Smav * If this is a write, a flush, a delete or verify, we're all done. 538229997Sken * If this is a read, we can now send the data to the user. 539229997Sken */ 540229997Sken if ((beio->bio_cmd == BIO_WRITE) 541264274Smav || (beio->bio_cmd == BIO_FLUSH) 542267537Smav || (beio->bio_cmd == BIO_DELETE) 543267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 544229997Sken ctl_set_success(&io->scsiio); 545229997Sken ctl_complete_beio(beio); 546229997Sken } else { 547275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 548275058Smav beio->beio_cont == NULL) 549275058Smav ctl_set_success(&io->scsiio); 550229997Sken#ifdef CTL_TIME_IO 551229997Sken getbintime(&io->io_hdr.dma_start_bt); 552229997Sken#endif 553229997Sken ctl_datamove(io); 554229997Sken } 555229997Sken} 556229997Sken 557229997Skenstatic void 558229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 559229997Sken struct ctl_be_block_io *beio) 560229997Sken{ 561267877Smav union ctl_io *io = beio->io; 562229997Sken struct mount *mountpoint; 563241896Skib int error, lock_flags; 564229997Sken 565229997Sken DPRINTF("entered\n"); 566229997Sken 567267877Smav binuptime(&beio->ds_t0); 568267877Smav mtx_lock(&be_lun->io_lock); 569267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 570267877Smav mtx_unlock(&be_lun->io_lock); 571229997Sken 572267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 573229997Sken 574229997Sken if (MNT_SHARED_WRITES(mountpoint) 575229997Sken || ((mountpoint == NULL) 576229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 577229997Sken lock_flags = LK_SHARED; 578229997Sken else 579229997Sken lock_flags = LK_EXCLUSIVE; 580229997Sken 581229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 582229997Sken 583286353Smav error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 584286353Smav curthread); 585229997Sken VOP_UNLOCK(be_lun->vn, 0); 586229997Sken 587229997Sken vn_finished_write(mountpoint); 588229997Sken 589267877Smav mtx_lock(&be_lun->io_lock); 590267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 591267877Smav beio->ds_tag_type, beio->ds_trans_type, 592267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 593267877Smav mtx_unlock(&be_lun->io_lock); 594267877Smav 595229997Sken if (error == 0) 596229997Sken ctl_set_success(&io->scsiio); 597229997Sken else { 598229997Sken /* XXX KDM is there is a better error here? */ 599229997Sken ctl_set_internal_failure(&io->scsiio, 600229997Sken /*sks_valid*/ 1, 601229997Sken /*retry_count*/ 0xbad1); 602229997Sken } 603229997Sken 604229997Sken ctl_complete_beio(beio); 605229997Sken} 606229997Sken 607258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 608258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 609258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 610258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 611229997Sken 612229997Skenstatic void 613229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 614229997Sken struct ctl_be_block_io *beio) 615229997Sken{ 616229997Sken struct ctl_be_block_filedata *file_data; 617229997Sken union ctl_io *io; 618229997Sken struct uio xuio; 619229997Sken struct iovec *xiovec; 620241896Skib int flags; 621229997Sken int error, i; 622229997Sken 623229997Sken DPRINTF("entered\n"); 624229997Sken 625229997Sken file_data = &be_lun->backend.file; 626229997Sken io = beio->io; 627271309Smav flags = 0; 628271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 629271309Smav flags |= IO_DIRECT; 630271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 631271309Smav flags |= IO_SYNC; 632229997Sken 633267537Smav bzero(&xuio, sizeof(xuio)); 634229997Sken if (beio->bio_cmd == BIO_READ) { 635229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 636267537Smav xuio.uio_rw = UIO_READ; 637229997Sken } else { 638229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 639267537Smav xuio.uio_rw = UIO_WRITE; 640229997Sken } 641229997Sken xuio.uio_offset = beio->io_offset; 642229997Sken xuio.uio_resid = beio->io_len; 643229997Sken xuio.uio_segflg = UIO_SYSSPACE; 644229997Sken xuio.uio_iov = beio->xiovecs; 645229997Sken xuio.uio_iovcnt = beio->num_segs; 646229997Sken xuio.uio_td = curthread; 647229997Sken 648229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 649229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 650229997Sken xiovec->iov_len = beio->sg_segs[i].len; 651229997Sken } 652229997Sken 653267877Smav binuptime(&beio->ds_t0); 654267877Smav mtx_lock(&be_lun->io_lock); 655267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 656267877Smav mtx_unlock(&be_lun->io_lock); 657267877Smav 658229997Sken if (beio->bio_cmd == BIO_READ) { 659229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 660229997Sken 661229997Sken /* 662229997Sken * UFS pays attention to IO_DIRECT for reads. If the 663229997Sken * DIRECTIO option is configured into the kernel, it calls 664229997Sken * ffs_rawread(). But that only works for single-segment 665229997Sken * uios with user space addresses. In our case, with a 666229997Sken * kernel uio, it still reads into the buffer cache, but it 667229997Sken * will just try to release the buffer from the cache later 668229997Sken * on in ffs_read(). 669229997Sken * 670229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 671229997Sken * 672229997Sken * UFS does not pay attention to IO_SYNC for reads. 673229997Sken * 674229997Sken * ZFS pays attention to IO_SYNC (which translates into the 675229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 676229997Sken * attempts to sync the file before reading. 677229997Sken */ 678271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 679229997Sken 680229997Sken VOP_UNLOCK(be_lun->vn, 0); 681267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 682229997Sken } else { 683229997Sken struct mount *mountpoint; 684229997Sken int lock_flags; 685229997Sken 686229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 687229997Sken 688229997Sken if (MNT_SHARED_WRITES(mountpoint) 689229997Sken || ((mountpoint == NULL) 690229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 691229997Sken lock_flags = LK_SHARED; 692229997Sken else 693229997Sken lock_flags = LK_EXCLUSIVE; 694229997Sken 695229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 696229997Sken 697229997Sken /* 698229997Sken * UFS pays attention to IO_DIRECT for writes. The write 699229997Sken * is done asynchronously. (Normally the write would just 700229997Sken * get put into cache. 701229997Sken * 702229997Sken * UFS pays attention to IO_SYNC for writes. It will 703229997Sken * attempt to write the buffer out synchronously if that 704229997Sken * flag is set. 705229997Sken * 706229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 707229997Sken * 708229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 709229997Sken * for writes. It will flush the transaction from the 710229997Sken * cache before returning. 711229997Sken */ 712271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 713229997Sken VOP_UNLOCK(be_lun->vn, 0); 714229997Sken 715229997Sken vn_finished_write(mountpoint); 716267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 717229997Sken } 718229997Sken 719267877Smav mtx_lock(&be_lun->io_lock); 720267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 721267877Smav beio->ds_tag_type, beio->ds_trans_type, 722267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 723267877Smav mtx_unlock(&be_lun->io_lock); 724267877Smav 725229997Sken /* 726229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 727229997Sken * return the I/O to the user. 728229997Sken */ 729229997Sken if (error != 0) { 730229997Sken char path_str[32]; 731229997Sken 732229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 733229997Sken printf("%s%s command returned errno %d\n", path_str, 734229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 735282565Smav if (error == ENOSPC || error == EDQUOT) { 736273809Smav ctl_set_space_alloc_fail(&io->scsiio); 737273809Smav } else 738273809Smav ctl_set_medium_error(&io->scsiio); 739229997Sken ctl_complete_beio(beio); 740229997Sken return; 741229997Sken } 742229997Sken 743229997Sken /* 744269122Smav * If this is a write or a verify, we're all done. 745229997Sken * If this is a read, we can now send the data to the user. 746229997Sken */ 747269122Smav if ((beio->bio_cmd == BIO_WRITE) || 748269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 749229997Sken ctl_set_success(&io->scsiio); 750229997Sken ctl_complete_beio(beio); 751229997Sken } else { 752275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 753275058Smav beio->beio_cont == NULL) 754275058Smav ctl_set_success(&io->scsiio); 755229997Sken#ifdef CTL_TIME_IO 756229997Sken getbintime(&io->io_hdr.dma_start_bt); 757229997Sken#endif 758229997Sken ctl_datamove(io); 759229997Sken } 760229997Sken} 761229997Sken 762229997Skenstatic void 763275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 764275474Smav struct ctl_be_block_io *beio) 765275474Smav{ 766275474Smav union ctl_io *io = beio->io; 767275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 768275474Smav struct scsi_get_lba_status_data *data; 769275474Smav off_t roff, off; 770275474Smav int error, status; 771275474Smav 772275474Smav DPRINTF("entered\n"); 773275474Smav 774286811Smav off = roff = ((off_t)lbalen->lba) * be_lun->blocksize; 775275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 776275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 777275474Smav 0, curthread->td_ucred, curthread); 778275474Smav if (error == 0 && off > roff) 779275474Smav status = 0; /* mapped up to off */ 780275474Smav else { 781275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 782275474Smav 0, curthread->td_ucred, curthread); 783275474Smav if (error == 0 && off > roff) 784275474Smav status = 1; /* deallocated up to off */ 785275474Smav else { 786275474Smav status = 0; /* unknown up to the end */ 787275474Smav off = be_lun->size_bytes; 788275474Smav } 789275474Smav } 790275474Smav VOP_UNLOCK(be_lun->vn, 0); 791275474Smav 792275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 793275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 794286811Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->blocksize - lbalen->lba), 795275474Smav data->descr[0].length); 796275474Smav data->descr[0].status = status; 797275474Smav 798275474Smav ctl_complete_beio(beio); 799275474Smav} 800275474Smav 801275481Smavstatic uint64_t 802275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 803275481Smav{ 804275481Smav struct vattr vattr; 805275481Smav struct statfs statfs; 806285030Smav uint64_t val; 807275481Smav int error; 808275481Smav 809285030Smav val = UINT64_MAX; 810275481Smav if (be_lun->vn == NULL) 811285030Smav return (val); 812285030Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 813275481Smav if (strcmp(attrname, "blocksused") == 0) { 814275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 815285030Smav if (error == 0) 816286811Smav val = vattr.va_bytes / be_lun->blocksize; 817275481Smav } 818285030Smav if (strcmp(attrname, "blocksavail") == 0 && 819285030Smav (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 820275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 821285030Smav if (error == 0) 822286811Smav val = statfs.f_bavail * statfs.f_bsize / 823286811Smav be_lun->blocksize; 824275481Smav } 825285030Smav VOP_UNLOCK(be_lun->vn, 0); 826285030Smav return (val); 827275481Smav} 828275481Smav 829275474Smavstatic void 830269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 831269123Smav struct ctl_be_block_io *beio) 832269123Smav{ 833269123Smav struct ctl_be_block_devdata *dev_data; 834269123Smav union ctl_io *io; 835269123Smav struct uio xuio; 836269123Smav struct iovec *xiovec; 837269123Smav int flags; 838269123Smav int error, i; 839269123Smav 840269123Smav DPRINTF("entered\n"); 841269123Smav 842269123Smav dev_data = &be_lun->backend.dev; 843269123Smav io = beio->io; 844271309Smav flags = 0; 845271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 846271309Smav flags |= IO_DIRECT; 847271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 848271309Smav flags |= IO_SYNC; 849269123Smav 850269123Smav bzero(&xuio, sizeof(xuio)); 851269123Smav if (beio->bio_cmd == BIO_READ) { 852269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 853269123Smav xuio.uio_rw = UIO_READ; 854269123Smav } else { 855269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 856269123Smav xuio.uio_rw = UIO_WRITE; 857269123Smav } 858269123Smav xuio.uio_offset = beio->io_offset; 859269123Smav xuio.uio_resid = beio->io_len; 860269123Smav xuio.uio_segflg = UIO_SYSSPACE; 861269123Smav xuio.uio_iov = beio->xiovecs; 862269123Smav xuio.uio_iovcnt = beio->num_segs; 863269123Smav xuio.uio_td = curthread; 864269123Smav 865269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 866269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 867269123Smav xiovec->iov_len = beio->sg_segs[i].len; 868269123Smav } 869269123Smav 870269123Smav binuptime(&beio->ds_t0); 871269123Smav mtx_lock(&be_lun->io_lock); 872269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 873269123Smav mtx_unlock(&be_lun->io_lock); 874269123Smav 875269123Smav if (beio->bio_cmd == BIO_READ) { 876271309Smav error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 877269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 878269123Smav } else { 879271309Smav error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 880269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 881269123Smav } 882269123Smav 883269123Smav mtx_lock(&be_lun->io_lock); 884269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 885269123Smav beio->ds_tag_type, beio->ds_trans_type, 886269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 887269123Smav mtx_unlock(&be_lun->io_lock); 888269123Smav 889269123Smav /* 890269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 891269123Smav * return the I/O to the user. 892269123Smav */ 893269123Smav if (error != 0) { 894282565Smav if (error == ENOSPC || error == EDQUOT) { 895273809Smav ctl_set_space_alloc_fail(&io->scsiio); 896273809Smav } else 897273809Smav ctl_set_medium_error(&io->scsiio); 898269123Smav ctl_complete_beio(beio); 899269123Smav return; 900269123Smav } 901269123Smav 902269123Smav /* 903269123Smav * If this is a write or a verify, we're all done. 904269123Smav * If this is a read, we can now send the data to the user. 905269123Smav */ 906269123Smav if ((beio->bio_cmd == BIO_WRITE) || 907269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 908269123Smav ctl_set_success(&io->scsiio); 909269123Smav ctl_complete_beio(beio); 910269123Smav } else { 911275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 912275058Smav beio->beio_cont == NULL) 913275058Smav ctl_set_success(&io->scsiio); 914269123Smav#ifdef CTL_TIME_IO 915269123Smav getbintime(&io->io_hdr.dma_start_bt); 916269123Smav#endif 917269123Smav ctl_datamove(io); 918269123Smav } 919269123Smav} 920269123Smav 921269123Smavstatic void 922275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 923275474Smav struct ctl_be_block_io *beio) 924275474Smav{ 925275474Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 926275474Smav union ctl_io *io = beio->io; 927275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 928275474Smav struct scsi_get_lba_status_data *data; 929275474Smav off_t roff, off; 930275474Smav int error, status; 931275474Smav 932275474Smav DPRINTF("entered\n"); 933275474Smav 934286811Smav off = roff = ((off_t)lbalen->lba) * be_lun->blocksize; 935275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE, 936275474Smav (caddr_t)&off, FREAD, curthread); 937275474Smav if (error == 0 && off > roff) 938275474Smav status = 0; /* mapped up to off */ 939275474Smav else { 940275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA, 941275474Smav (caddr_t)&off, FREAD, curthread); 942275474Smav if (error == 0 && off > roff) 943275474Smav status = 1; /* deallocated up to off */ 944275474Smav else { 945275474Smav status = 0; /* unknown up to the end */ 946275474Smav off = be_lun->size_bytes; 947275474Smav } 948275474Smav } 949275474Smav 950275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 951275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 952286811Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->blocksize - lbalen->lba), 953275474Smav data->descr[0].length); 954275474Smav data->descr[0].status = status; 955275474Smav 956275474Smav ctl_complete_beio(beio); 957275474Smav} 958275474Smav 959275474Smavstatic void 960229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 961229997Sken struct ctl_be_block_io *beio) 962229997Sken{ 963229997Sken struct bio *bio; 964229997Sken union ctl_io *io; 965229997Sken struct ctl_be_block_devdata *dev_data; 966229997Sken 967229997Sken dev_data = &be_lun->backend.dev; 968229997Sken io = beio->io; 969229997Sken 970229997Sken DPRINTF("entered\n"); 971229997Sken 972229997Sken /* This can't fail, it's a blocking allocation. */ 973229997Sken bio = g_alloc_bio(); 974229997Sken 975229997Sken bio->bio_cmd = BIO_FLUSH; 976229997Sken bio->bio_dev = dev_data->cdev; 977229997Sken bio->bio_offset = 0; 978229997Sken bio->bio_data = 0; 979229997Sken bio->bio_done = ctl_be_block_biodone; 980229997Sken bio->bio_caller1 = beio; 981229997Sken bio->bio_pblkno = 0; 982229997Sken 983229997Sken /* 984229997Sken * We don't need to acquire the LUN lock here, because we are only 985229997Sken * sending one bio, and so there is no other context to synchronize 986229997Sken * with. 987229997Sken */ 988229997Sken beio->num_bios_sent = 1; 989229997Sken beio->send_complete = 1; 990229997Sken 991229997Sken binuptime(&beio->ds_t0); 992267877Smav mtx_lock(&be_lun->io_lock); 993229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 994267877Smav mtx_unlock(&be_lun->io_lock); 995229997Sken 996229997Sken (*dev_data->csw->d_strategy)(bio); 997229997Sken} 998229997Sken 999229997Skenstatic void 1000264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1001264274Smav struct ctl_be_block_io *beio, 1002264274Smav uint64_t off, uint64_t len, int last) 1003264274Smav{ 1004264274Smav struct bio *bio; 1005264274Smav struct ctl_be_block_devdata *dev_data; 1006264296Smav uint64_t maxlen; 1007264274Smav 1008264274Smav dev_data = &be_lun->backend.dev; 1009264296Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 1010264274Smav while (len > 0) { 1011264274Smav bio = g_alloc_bio(); 1012264274Smav bio->bio_cmd = BIO_DELETE; 1013264274Smav bio->bio_dev = dev_data->cdev; 1014264274Smav bio->bio_offset = off; 1015264296Smav bio->bio_length = MIN(len, maxlen); 1016264274Smav bio->bio_data = 0; 1017264274Smav bio->bio_done = ctl_be_block_biodone; 1018264274Smav bio->bio_caller1 = beio; 1019264296Smav bio->bio_pblkno = off / be_lun->blocksize; 1020264274Smav 1021264274Smav off += bio->bio_length; 1022264274Smav len -= bio->bio_length; 1023264274Smav 1024267877Smav mtx_lock(&be_lun->io_lock); 1025264274Smav beio->num_bios_sent++; 1026264274Smav if (last && len == 0) 1027264274Smav beio->send_complete = 1; 1028267877Smav mtx_unlock(&be_lun->io_lock); 1029264274Smav 1030264274Smav (*dev_data->csw->d_strategy)(bio); 1031264274Smav } 1032264274Smav} 1033264274Smav 1034264274Smavstatic void 1035264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1036264274Smav struct ctl_be_block_io *beio) 1037264274Smav{ 1038264274Smav union ctl_io *io; 1039264274Smav struct ctl_be_block_devdata *dev_data; 1040267515Smav struct ctl_ptr_len_flags *ptrlen; 1041264274Smav struct scsi_unmap_desc *buf, *end; 1042264274Smav uint64_t len; 1043264274Smav 1044264274Smav dev_data = &be_lun->backend.dev; 1045264274Smav io = beio->io; 1046264274Smav 1047264274Smav DPRINTF("entered\n"); 1048264274Smav 1049264274Smav binuptime(&beio->ds_t0); 1050267877Smav mtx_lock(&be_lun->io_lock); 1051264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1052267877Smav mtx_unlock(&be_lun->io_lock); 1053264274Smav 1054264274Smav if (beio->io_offset == -1) { 1055264274Smav beio->io_len = 0; 1056267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1057267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1058267515Smav end = buf + ptrlen->len / sizeof(*buf); 1059264274Smav for (; buf < end; buf++) { 1060264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1061264274Smav be_lun->blocksize; 1062264274Smav beio->io_len += len; 1063264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1064264274Smav scsi_8btou64(buf->lba) * be_lun->blocksize, len, 1065264283Smav (end - buf < 2) ? TRUE : FALSE); 1066264274Smav } 1067264274Smav } else 1068264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1069264274Smav beio->io_offset, beio->io_len, TRUE); 1070264274Smav} 1071264274Smav 1072264274Smavstatic void 1073229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1074229997Sken struct ctl_be_block_io *beio) 1075229997Sken{ 1076267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1077229997Sken int i; 1078229997Sken struct bio *bio; 1079229997Sken struct ctl_be_block_devdata *dev_data; 1080229997Sken off_t cur_offset; 1081229997Sken int max_iosize; 1082229997Sken 1083229997Sken DPRINTF("entered\n"); 1084229997Sken 1085229997Sken dev_data = &be_lun->backend.dev; 1086229997Sken 1087229997Sken /* 1088229997Sken * We have to limit our I/O size to the maximum supported by the 1089229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1090229997Sken * set it properly, use DFLTPHYS. 1091229997Sken */ 1092229997Sken max_iosize = dev_data->cdev->si_iosize_max; 1093229997Sken if (max_iosize < PAGE_SIZE) 1094229997Sken max_iosize = DFLTPHYS; 1095229997Sken 1096229997Sken cur_offset = beio->io_offset; 1097229997Sken for (i = 0; i < beio->num_segs; i++) { 1098229997Sken size_t cur_size; 1099229997Sken uint8_t *cur_ptr; 1100229997Sken 1101229997Sken cur_size = beio->sg_segs[i].len; 1102229997Sken cur_ptr = beio->sg_segs[i].addr; 1103229997Sken 1104229997Sken while (cur_size > 0) { 1105229997Sken /* This can't fail, it's a blocking allocation. */ 1106229997Sken bio = g_alloc_bio(); 1107229997Sken 1108229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1109229997Sken 1110229997Sken bio->bio_cmd = beio->bio_cmd; 1111229997Sken bio->bio_dev = dev_data->cdev; 1112229997Sken bio->bio_caller1 = beio; 1113229997Sken bio->bio_length = min(cur_size, max_iosize); 1114229997Sken bio->bio_offset = cur_offset; 1115229997Sken bio->bio_data = cur_ptr; 1116229997Sken bio->bio_done = ctl_be_block_biodone; 1117229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 1118229997Sken 1119229997Sken cur_offset += bio->bio_length; 1120229997Sken cur_ptr += bio->bio_length; 1121229997Sken cur_size -= bio->bio_length; 1122229997Sken 1123267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1124229997Sken beio->num_bios_sent++; 1125229997Sken } 1126229997Sken } 1127267877Smav binuptime(&beio->ds_t0); 1128267877Smav mtx_lock(&be_lun->io_lock); 1129267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1130267877Smav beio->send_complete = 1; 1131267877Smav mtx_unlock(&be_lun->io_lock); 1132267877Smav 1133267877Smav /* 1134267877Smav * Fire off all allocated requests! 1135267877Smav */ 1136267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1137267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1138267877Smav (*dev_data->csw->d_strategy)(bio); 1139267877Smav } 1140229997Sken} 1141229997Sken 1142274154Smavstatic uint64_t 1143274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1144274154Smav{ 1145274154Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1146274154Smav struct diocgattr_arg arg; 1147274154Smav int error; 1148274154Smav 1149274154Smav if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1150274154Smav return (UINT64_MAX); 1151274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1152274154Smav arg.len = sizeof(arg.value.off); 1153274154Smav error = dev_data->csw->d_ioctl(dev_data->cdev, 1154274154Smav DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1155274154Smav if (error != 0) 1156274154Smav return (UINT64_MAX); 1157274154Smav return (arg.value.off); 1158274154Smav} 1159274154Smav 1160229997Skenstatic void 1161286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1162286353Smav union ctl_io *io) 1163286353Smav{ 1164286353Smav struct ctl_be_block_io *beio; 1165286353Smav struct ctl_lba_len_flags *lbalen; 1166286353Smav 1167286353Smav DPRINTF("entered\n"); 1168286353Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1169286353Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1170286353Smav 1171286353Smav beio->io_len = lbalen->len * be_lun->blocksize; 1172286353Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1173286353Smav beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1174286353Smav beio->bio_cmd = BIO_FLUSH; 1175286353Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1176286353Smav DPRINTF("SYNC\n"); 1177286353Smav be_lun->lun_flush(be_lun, beio); 1178286353Smav} 1179286353Smav 1180286353Smavstatic void 1181264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1182264274Smav{ 1183264274Smav union ctl_io *io; 1184264274Smav 1185264274Smav io = beio->io; 1186264274Smav ctl_free_beio(beio); 1187267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1188267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1189267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1190264274Smav ctl_config_write_done(io); 1191264274Smav return; 1192264274Smav } 1193264274Smav 1194264274Smav ctl_be_block_config_write(io); 1195264274Smav} 1196264274Smav 1197264274Smavstatic void 1198264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1199264274Smav union ctl_io *io) 1200264274Smav{ 1201264274Smav struct ctl_be_block_io *beio; 1202267515Smav struct ctl_lba_len_flags *lbalen; 1203278625Smav uint64_t len_left, lba; 1204278625Smav uint32_t pb, pbo, adj; 1205264274Smav int i, seglen; 1206264274Smav uint8_t *buf, *end; 1207264274Smav 1208264274Smav DPRINTF("entered\n"); 1209264274Smav 1210267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1211267537Smav lbalen = ARGS(beio->io); 1212264274Smav 1213271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1214269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1215264274Smav ctl_free_beio(beio); 1216264274Smav ctl_set_invalid_field(&io->scsiio, 1217264274Smav /*sks_valid*/ 1, 1218264274Smav /*command*/ 1, 1219264274Smav /*field*/ 1, 1220264274Smav /*bit_valid*/ 0, 1221264274Smav /*bit*/ 0); 1222264274Smav ctl_config_write_done(io); 1223264274Smav return; 1224264274Smav } 1225264274Smav 1226269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1227267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1228267515Smav beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1229264274Smav beio->bio_cmd = BIO_DELETE; 1230264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1231264274Smav 1232264274Smav be_lun->unmap(be_lun, beio); 1233264274Smav return; 1234264274Smav } 1235264274Smav 1236264274Smav beio->bio_cmd = BIO_WRITE; 1237264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1238264274Smav 1239264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1240267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1241264274Smav 1242278625Smav pb = be_lun->blocksize << be_lun->pblockexp; 1243278625Smav if (be_lun->pblockoff > 0) 1244278625Smav pbo = pb - be_lun->blocksize * be_lun->pblockoff; 1245278625Smav else 1246278625Smav pbo = 0; 1247267515Smav len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1248264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1249264274Smav 1250264274Smav /* 1251264274Smav * Setup the S/G entry for this chunk. 1252264274Smav */ 1253264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1254278619Smav if (pb > be_lun->blocksize) { 1255278619Smav adj = ((lbalen->lba + lba) * be_lun->blocksize + 1256278619Smav seglen - pbo) % pb; 1257278619Smav if (seglen > adj) 1258278619Smav seglen -= adj; 1259278619Smav else 1260278619Smav seglen -= seglen % be_lun->blocksize; 1261278619Smav } else 1262278619Smav seglen -= seglen % be_lun->blocksize; 1263264274Smav beio->sg_segs[i].len = seglen; 1264264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1265264274Smav 1266264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1267264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1268264274Smav 1269264274Smav beio->num_segs++; 1270264274Smav len_left -= seglen; 1271264274Smav 1272264274Smav buf = beio->sg_segs[i].addr; 1273264274Smav end = buf + seglen; 1274264274Smav for (; buf < end; buf += be_lun->blocksize) { 1275264274Smav memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1276267515Smav if (lbalen->flags & SWS_LBDATA) 1277267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1278264274Smav lba++; 1279264274Smav } 1280264274Smav } 1281264274Smav 1282267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1283264274Smav beio->io_len = lba * be_lun->blocksize; 1284264274Smav 1285264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1286264274Smav if (len_left > 0) { 1287267515Smav lbalen->lba += lba; 1288267515Smav lbalen->len -= lba; 1289264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1290264274Smav } 1291264274Smav 1292264274Smav be_lun->dispatch(be_lun, beio); 1293264274Smav} 1294264274Smav 1295264274Smavstatic void 1296264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1297264274Smav union ctl_io *io) 1298264274Smav{ 1299264274Smav struct ctl_be_block_io *beio; 1300267515Smav struct ctl_ptr_len_flags *ptrlen; 1301264274Smav 1302264274Smav DPRINTF("entered\n"); 1303264274Smav 1304267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1305267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1306264274Smav 1307269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1308264274Smav ctl_free_beio(beio); 1309264274Smav ctl_set_invalid_field(&io->scsiio, 1310264274Smav /*sks_valid*/ 0, 1311264274Smav /*command*/ 1, 1312264274Smav /*field*/ 0, 1313264274Smav /*bit_valid*/ 0, 1314264274Smav /*bit*/ 0); 1315264274Smav ctl_config_write_done(io); 1316264274Smav return; 1317264274Smav } 1318264274Smav 1319264274Smav beio->io_len = 0; 1320264274Smav beio->io_offset = -1; 1321264274Smav beio->bio_cmd = BIO_DELETE; 1322264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1323267515Smav DPRINTF("UNMAP\n"); 1324264274Smav be_lun->unmap(be_lun, beio); 1325264274Smav} 1326264274Smav 1327264274Smavstatic void 1328275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1329275474Smav{ 1330275474Smav union ctl_io *io; 1331275474Smav 1332275474Smav io = beio->io; 1333275474Smav ctl_free_beio(beio); 1334275474Smav ctl_config_read_done(io); 1335275474Smav} 1336275474Smav 1337275474Smavstatic void 1338275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1339275474Smav union ctl_io *io) 1340275474Smav{ 1341275474Smav struct ctl_be_block_io *beio; 1342275474Smav struct ctl_be_block_softc *softc; 1343275474Smav 1344275474Smav DPRINTF("entered\n"); 1345275474Smav 1346275474Smav softc = be_lun->softc; 1347275474Smav beio = ctl_alloc_beio(softc); 1348275474Smav beio->io = io; 1349275474Smav beio->lun = be_lun; 1350275474Smav beio->beio_cont = ctl_be_block_cr_done; 1351275474Smav PRIV(io)->ptr = (void *)beio; 1352275474Smav 1353275474Smav switch (io->scsiio.cdb[0]) { 1354275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1355275474Smav beio->bio_cmd = -1; 1356275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1357275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1358275474Smav beio->io_len = 0; 1359275474Smav if (be_lun->get_lba_status) 1360275474Smav be_lun->get_lba_status(be_lun, beio); 1361275474Smav else 1362275474Smav ctl_be_block_cr_done(beio); 1363275474Smav break; 1364275474Smav default: 1365275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1366275474Smav break; 1367275474Smav } 1368275474Smav} 1369275474Smav 1370275474Smavstatic void 1371264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1372264274Smav{ 1373264274Smav union ctl_io *io; 1374264274Smav 1375264274Smav io = beio->io; 1376264274Smav ctl_free_beio(beio); 1377264274Smav ctl_config_write_done(io); 1378264274Smav} 1379264274Smav 1380264274Smavstatic void 1381229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1382229997Sken union ctl_io *io) 1383229997Sken{ 1384229997Sken struct ctl_be_block_io *beio; 1385229997Sken struct ctl_be_block_softc *softc; 1386229997Sken 1387229997Sken DPRINTF("entered\n"); 1388229997Sken 1389229997Sken softc = be_lun->softc; 1390229997Sken beio = ctl_alloc_beio(softc); 1391229997Sken beio->io = io; 1392229997Sken beio->lun = be_lun; 1393264274Smav beio->beio_cont = ctl_be_block_cw_done; 1394286353Smav switch (io->scsiio.tag_type) { 1395286353Smav case CTL_TAG_ORDERED: 1396286353Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1397286353Smav break; 1398286353Smav case CTL_TAG_HEAD_OF_QUEUE: 1399286353Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1400286353Smav break; 1401286353Smav case CTL_TAG_UNTAGGED: 1402286353Smav case CTL_TAG_SIMPLE: 1403286353Smav case CTL_TAG_ACA: 1404286353Smav default: 1405286353Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1406286353Smav break; 1407286353Smav } 1408267519Smav PRIV(io)->ptr = (void *)beio; 1409229997Sken 1410229997Sken switch (io->scsiio.cdb[0]) { 1411229997Sken case SYNCHRONIZE_CACHE: 1412229997Sken case SYNCHRONIZE_CACHE_16: 1413286353Smav ctl_be_block_cw_dispatch_sync(be_lun, io); 1414229997Sken break; 1415264274Smav case WRITE_SAME_10: 1416264274Smav case WRITE_SAME_16: 1417264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1418264274Smav break; 1419264274Smav case UNMAP: 1420264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1421264274Smav break; 1422229997Sken default: 1423229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1424229997Sken break; 1425229997Sken } 1426229997Sken} 1427229997Sken 1428258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1429258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1430258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1431258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1432229997Sken 1433229997Skenstatic void 1434264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1435264886Smav{ 1436264886Smav struct ctl_be_block_lun *be_lun; 1437264886Smav union ctl_io *io; 1438264886Smav 1439264886Smav io = beio->io; 1440264886Smav be_lun = beio->lun; 1441264886Smav ctl_free_beio(beio); 1442267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1443267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1444267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1445267537Smav ctl_data_submit_done(io); 1446264886Smav return; 1447264886Smav } 1448264886Smav 1449264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1450264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1451264886Smav 1452267877Smav mtx_lock(&be_lun->queue_lock); 1453264886Smav /* 1454264886Smav * XXX KDM make sure that links is okay to use at this point. 1455264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1456264886Smav * or deal with resource allocation here. 1457264886Smav */ 1458264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1459267877Smav mtx_unlock(&be_lun->queue_lock); 1460264886Smav 1461264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1462264886Smav} 1463264886Smav 1464264886Smavstatic void 1465229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1466229997Sken union ctl_io *io) 1467229997Sken{ 1468229997Sken struct ctl_be_block_io *beio; 1469229997Sken struct ctl_be_block_softc *softc; 1470267537Smav struct ctl_lba_len_flags *lbalen; 1471267519Smav struct ctl_ptr_len_flags *bptrlen; 1472267519Smav uint64_t len_left, lbas; 1473229997Sken int i; 1474229997Sken 1475229997Sken softc = be_lun->softc; 1476229997Sken 1477229997Sken DPRINTF("entered\n"); 1478229997Sken 1479267537Smav lbalen = ARGS(io); 1480267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1481267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1482267537Smav } else { 1483229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1484229997Sken } 1485229997Sken 1486229997Sken beio = ctl_alloc_beio(softc); 1487229997Sken beio->io = io; 1488229997Sken beio->lun = be_lun; 1489267519Smav bptrlen = PRIV(io); 1490267519Smav bptrlen->ptr = (void *)beio; 1491229997Sken 1492229997Sken switch (io->scsiio.tag_type) { 1493229997Sken case CTL_TAG_ORDERED: 1494229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1495229997Sken break; 1496229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1497229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1498229997Sken break; 1499229997Sken case CTL_TAG_UNTAGGED: 1500229997Sken case CTL_TAG_SIMPLE: 1501229997Sken case CTL_TAG_ACA: 1502229997Sken default: 1503229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1504229997Sken break; 1505229997Sken } 1506229997Sken 1507267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1508267537Smav beio->bio_cmd = BIO_WRITE; 1509267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1510267537Smav } else { 1511229997Sken beio->bio_cmd = BIO_READ; 1512229997Sken beio->ds_trans_type = DEVSTAT_READ; 1513229997Sken } 1514229997Sken 1515264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1516229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1517267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1518267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1519267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1520267537Smav else 1521267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1522267537Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1523267519Smav beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1524267519Smav beio->io_len = lbas * be_lun->blocksize; 1525267519Smav bptrlen->len += lbas; 1526229997Sken 1527264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1528264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1529264886Smav i, CTLBLK_MAX_SEGS)); 1530229997Sken 1531229997Sken /* 1532229997Sken * Setup the S/G entry for this chunk. 1533229997Sken */ 1534264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1535229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1536229997Sken 1537229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1538229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1539229997Sken 1540267537Smav /* Set up second segment for compare operation. */ 1541267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1542267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1543267537Smav beio->sg_segs[i].len; 1544267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1545267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1546267537Smav } 1547267537Smav 1548229997Sken beio->num_segs++; 1549229997Sken len_left -= beio->sg_segs[i].len; 1550229997Sken } 1551267519Smav if (bptrlen->len < lbalen->len) 1552264886Smav beio->beio_cont = ctl_be_block_next; 1553264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1554267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1555267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1556267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1557267537Smav else 1558267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1559264886Smav io->scsiio.kern_data_len = beio->io_len; 1560264886Smav io->scsiio.kern_data_resid = 0; 1561264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1562264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1563229997Sken 1564229997Sken /* 1565229997Sken * For the read case, we need to read the data into our buffers and 1566229997Sken * then we can send it back to the user. For the write case, we 1567229997Sken * need to get the data from the user first. 1568229997Sken */ 1569229997Sken if (beio->bio_cmd == BIO_READ) { 1570229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1571229997Sken be_lun->dispatch(be_lun, beio); 1572229997Sken } else { 1573229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1574229997Sken#ifdef CTL_TIME_IO 1575229997Sken getbintime(&io->io_hdr.dma_start_bt); 1576229997Sken#endif 1577229997Sken ctl_datamove(io); 1578229997Sken } 1579229997Sken} 1580229997Sken 1581229997Skenstatic void 1582229997Skenctl_be_block_worker(void *context, int pending) 1583229997Sken{ 1584229997Sken struct ctl_be_block_lun *be_lun; 1585229997Sken struct ctl_be_block_softc *softc; 1586229997Sken union ctl_io *io; 1587229997Sken 1588229997Sken be_lun = (struct ctl_be_block_lun *)context; 1589229997Sken softc = be_lun->softc; 1590229997Sken 1591229997Sken DPRINTF("entered\n"); 1592229997Sken 1593267877Smav mtx_lock(&be_lun->queue_lock); 1594229997Sken for (;;) { 1595229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1596229997Sken if (io != NULL) { 1597229997Sken struct ctl_be_block_io *beio; 1598229997Sken 1599229997Sken DPRINTF("datamove queue\n"); 1600229997Sken 1601229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1602229997Sken ctl_io_hdr, links); 1603229997Sken 1604267877Smav mtx_unlock(&be_lun->queue_lock); 1605229997Sken 1606267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1607229997Sken 1608229997Sken be_lun->dispatch(be_lun, beio); 1609229997Sken 1610267877Smav mtx_lock(&be_lun->queue_lock); 1611229997Sken continue; 1612229997Sken } 1613229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1614229997Sken if (io != NULL) { 1615229997Sken DPRINTF("config write queue\n"); 1616229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1617229997Sken ctl_io_hdr, links); 1618267877Smav mtx_unlock(&be_lun->queue_lock); 1619229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1620267877Smav mtx_lock(&be_lun->queue_lock); 1621229997Sken continue; 1622229997Sken } 1623275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1624275474Smav if (io != NULL) { 1625275474Smav DPRINTF("config read queue\n"); 1626275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1627275474Smav ctl_io_hdr, links); 1628275474Smav mtx_unlock(&be_lun->queue_lock); 1629275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1630275474Smav mtx_lock(&be_lun->queue_lock); 1631275474Smav continue; 1632275474Smav } 1633229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1634229997Sken if (io != NULL) { 1635229997Sken DPRINTF("input queue\n"); 1636229997Sken 1637229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1638229997Sken ctl_io_hdr, links); 1639267877Smav mtx_unlock(&be_lun->queue_lock); 1640229997Sken 1641229997Sken /* 1642229997Sken * We must drop the lock, since this routine and 1643229997Sken * its children may sleep. 1644229997Sken */ 1645229997Sken ctl_be_block_dispatch(be_lun, io); 1646229997Sken 1647267877Smav mtx_lock(&be_lun->queue_lock); 1648229997Sken continue; 1649229997Sken } 1650229997Sken 1651229997Sken /* 1652229997Sken * If we get here, there is no work left in the queues, so 1653229997Sken * just break out and let the task queue go to sleep. 1654229997Sken */ 1655229997Sken break; 1656229997Sken } 1657267877Smav mtx_unlock(&be_lun->queue_lock); 1658229997Sken} 1659229997Sken 1660229997Sken/* 1661229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1662229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1663229997Sken * thread. 1664229997Sken */ 1665229997Skenstatic int 1666229997Skenctl_be_block_submit(union ctl_io *io) 1667229997Sken{ 1668229997Sken struct ctl_be_block_lun *be_lun; 1669229997Sken struct ctl_be_lun *ctl_be_lun; 1670229997Sken 1671229997Sken DPRINTF("entered\n"); 1672229997Sken 1673229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1674229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1675229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1676229997Sken 1677229997Sken /* 1678229997Sken * Make sure we only get SCSI I/O. 1679229997Sken */ 1680229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1681229997Sken "%#x) encountered", io->io_hdr.io_type)); 1682229997Sken 1683267519Smav PRIV(io)->len = 0; 1684267519Smav 1685267877Smav mtx_lock(&be_lun->queue_lock); 1686229997Sken /* 1687229997Sken * XXX KDM make sure that links is okay to use at this point. 1688229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1689229997Sken * or deal with resource allocation here. 1690229997Sken */ 1691229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1692267877Smav mtx_unlock(&be_lun->queue_lock); 1693229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1694229997Sken 1695267514Smav return (CTL_RETVAL_COMPLETE); 1696229997Sken} 1697229997Sken 1698229997Skenstatic int 1699229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1700229997Sken int flag, struct thread *td) 1701229997Sken{ 1702229997Sken struct ctl_be_block_softc *softc; 1703229997Sken int error; 1704229997Sken 1705229997Sken softc = &backend_block_softc; 1706229997Sken 1707229997Sken error = 0; 1708229997Sken 1709229997Sken switch (cmd) { 1710229997Sken case CTL_LUN_REQ: { 1711229997Sken struct ctl_lun_req *lun_req; 1712229997Sken 1713229997Sken lun_req = (struct ctl_lun_req *)addr; 1714229997Sken 1715229997Sken switch (lun_req->reqtype) { 1716229997Sken case CTL_LUNREQ_CREATE: 1717229997Sken error = ctl_be_block_create(softc, lun_req); 1718229997Sken break; 1719229997Sken case CTL_LUNREQ_RM: 1720229997Sken error = ctl_be_block_rm(softc, lun_req); 1721229997Sken break; 1722232604Strasz case CTL_LUNREQ_MODIFY: 1723232604Strasz error = ctl_be_block_modify(softc, lun_req); 1724232604Strasz break; 1725229997Sken default: 1726229997Sken lun_req->status = CTL_LUN_ERROR; 1727229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1728272911Smav "invalid LUN request type %d", 1729229997Sken lun_req->reqtype); 1730229997Sken break; 1731229997Sken } 1732229997Sken break; 1733229997Sken } 1734229997Sken default: 1735229997Sken error = ENOTTY; 1736229997Sken break; 1737229997Sken } 1738229997Sken 1739229997Sken return (error); 1740229997Sken} 1741229997Sken 1742229997Skenstatic int 1743229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1744229997Sken{ 1745229997Sken struct ctl_be_block_filedata *file_data; 1746229997Sken struct ctl_lun_create_params *params; 1747275865Smav char *value; 1748229997Sken struct vattr vattr; 1749275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1750229997Sken int error; 1751229997Sken 1752229997Sken error = 0; 1753229997Sken file_data = &be_lun->backend.file; 1754272911Smav params = &be_lun->params; 1755229997Sken 1756229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1757229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1758229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1759275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1760275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1761229997Sken 1762229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1763229997Sken if (error != 0) { 1764229997Sken snprintf(req->error_str, sizeof(req->error_str), 1765229997Sken "error calling VOP_GETATTR() for file %s", 1766229997Sken be_lun->dev_path); 1767229997Sken return (error); 1768229997Sken } 1769229997Sken 1770229997Sken /* 1771229997Sken * Verify that we have the ability to upgrade to exclusive 1772229997Sken * access on this file so we can trap errors at open instead 1773229997Sken * of reporting them during first access. 1774229997Sken */ 1775229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1776229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1777229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1778229997Sken error = EBADF; 1779229997Sken snprintf(req->error_str, sizeof(req->error_str), 1780229997Sken "error locking file %s", be_lun->dev_path); 1781229997Sken return (error); 1782229997Sken } 1783229997Sken } 1784229997Sken 1785229997Sken 1786229997Sken file_data->cred = crhold(curthread->td_ucred); 1787232604Strasz if (params->lun_size_bytes != 0) 1788232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1789232604Strasz else 1790232604Strasz be_lun->size_bytes = vattr.va_size; 1791229997Sken /* 1792229997Sken * We set the multi thread flag for file operations because all 1793229997Sken * filesystems (in theory) are capable of allowing multiple readers 1794229997Sken * of a file at once. So we want to get the maximum possible 1795229997Sken * concurrency. 1796229997Sken */ 1797229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1798229997Sken 1799229997Sken /* 1800273029Smav * For files we can use any logical block size. Prefer 512 bytes 1801273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1802273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1803273029Smav * logical block size -- report it as physical block size. 1804229997Sken */ 1805229997Sken if (params->blocksize_bytes != 0) 1806229997Sken be_lun->blocksize = params->blocksize_bytes; 1807229997Sken else 1808229997Sken be_lun->blocksize = 512; 1809275865Smav 1810275865Smav us = ps = vattr.va_blocksize; 1811275865Smav uo = po = 0; 1812275865Smav 1813275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize"); 1814275865Smav if (value != NULL) 1815275865Smav ctl_expand_number(value, &ps); 1816275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset"); 1817275865Smav if (value != NULL) 1818275865Smav ctl_expand_number(value, &po); 1819275865Smav pss = ps / be_lun->blocksize; 1820275865Smav pos = po / be_lun->blocksize; 1821275865Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1822275865Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1823273029Smav be_lun->pblockexp = fls(pss) - 1; 1824275865Smav be_lun->pblockoff = (pss - pos) % pss; 1825273029Smav } 1826229997Sken 1827275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize"); 1828275865Smav if (value != NULL) 1829275865Smav ctl_expand_number(value, &us); 1830275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset"); 1831275865Smav if (value != NULL) 1832275865Smav ctl_expand_number(value, &uo); 1833275865Smav uss = us / be_lun->blocksize; 1834275865Smav uos = uo / be_lun->blocksize; 1835275865Smav if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) && 1836275865Smav ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) { 1837275865Smav be_lun->ublockexp = fls(uss) - 1; 1838275865Smav be_lun->ublockoff = (uss - uos) % uss; 1839275865Smav } 1840275865Smav 1841229997Sken /* 1842229997Sken * Sanity check. The media size has to be at least one 1843229997Sken * sector long. 1844229997Sken */ 1845229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1846229997Sken error = EINVAL; 1847229997Sken snprintf(req->error_str, sizeof(req->error_str), 1848229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1849229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1850229997Sken } 1851275920Smav 1852275920Smav be_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / be_lun->blocksize; 1853229997Sken return (error); 1854229997Sken} 1855229997Sken 1856229997Skenstatic int 1857229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1858229997Sken{ 1859229997Sken struct ctl_lun_create_params *params; 1860229997Sken struct vattr vattr; 1861229997Sken struct cdev *dev; 1862229997Sken struct cdevsw *devsw; 1863275865Smav char *value; 1864278672Smav int error, atomic, maxio, unmap; 1865286811Smav off_t ps, pss, po, pos, us, uss, uo, uos, tmp; 1866229997Sken 1867272911Smav params = &be_lun->params; 1868229997Sken 1869229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1870229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1871229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1872229997Sken &be_lun->backend.dev.dev_ref); 1873229997Sken if (be_lun->backend.dev.csw == NULL) 1874229997Sken panic("Unable to retrieve device switch"); 1875275474Smav if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) { 1876269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1877275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1878275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1879275920Smav } else { 1880269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1881275920Smav atomic = 0; 1882275920Smav maxio = be_lun->backend.dev.cdev->si_iosize_max; 1883275920Smav if (maxio <= 0) 1884275920Smav maxio = DFLTPHYS; 1885275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1886275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1887275920Smav } 1888269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1889274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1890229997Sken 1891229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1892229997Sken if (error) { 1893229997Sken snprintf(req->error_str, sizeof(req->error_str), 1894272911Smav "error getting vnode attributes for device %s", 1895272911Smav be_lun->dev_path); 1896229997Sken return (error); 1897229997Sken } 1898229997Sken 1899229997Sken dev = be_lun->vn->v_rdev; 1900229997Sken devsw = dev->si_devsw; 1901229997Sken if (!devsw->d_ioctl) { 1902229997Sken snprintf(req->error_str, sizeof(req->error_str), 1903272911Smav "no d_ioctl for device %s!", 1904229997Sken be_lun->dev_path); 1905229997Sken return (ENODEV); 1906229997Sken } 1907229997Sken 1908286811Smav error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1909229997Sken curthread); 1910229997Sken if (error) { 1911229997Sken snprintf(req->error_str, sizeof(req->error_str), 1912272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1913272911Smav "on %s!", error, be_lun->dev_path); 1914229997Sken return (error); 1915229997Sken } 1916229997Sken 1917229997Sken /* 1918229997Sken * If the user has asked for a blocksize that is greater than the 1919229997Sken * backing device's blocksize, we can do it only if the blocksize 1920229997Sken * the user is asking for is an even multiple of the underlying 1921229997Sken * device's blocksize. 1922229997Sken */ 1923286811Smav if ((params->blocksize_bytes != 0) && 1924286811Smav (params->blocksize_bytes >= tmp)) { 1925286811Smav if (params->blocksize_bytes % tmp == 0) { 1926229997Sken be_lun->blocksize = params->blocksize_bytes; 1927229997Sken } else { 1928229997Sken snprintf(req->error_str, sizeof(req->error_str), 1929272911Smav "requested blocksize %u is not an even " 1930229997Sken "multiple of backing device blocksize %u", 1931272911Smav params->blocksize_bytes, 1932229997Sken be_lun->blocksize); 1933229997Sken return (EINVAL); 1934229997Sken 1935229997Sken } 1936286811Smav } else if (params->blocksize_bytes != 0) { 1937229997Sken snprintf(req->error_str, sizeof(req->error_str), 1938272911Smav "requested blocksize %u < backing device " 1939272911Smav "blocksize %u", params->blocksize_bytes, 1940229997Sken be_lun->blocksize); 1941229997Sken return (EINVAL); 1942286811Smav } else 1943286811Smav be_lun->blocksize = tmp; 1944229997Sken 1945286811Smav error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&tmp, FREAD, 1946229997Sken curthread); 1947229997Sken if (error) { 1948229997Sken snprintf(req->error_str, sizeof(req->error_str), 1949272911Smav "error %d returned for DIOCGMEDIASIZE " 1950272911Smav " ioctl on %s!", error, 1951232604Strasz be_lun->dev_path); 1952229997Sken return (error); 1953229997Sken } 1954229997Sken 1955232604Strasz if (params->lun_size_bytes != 0) { 1956286811Smav if (params->lun_size_bytes > tmp) { 1957232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1958272911Smav "requested LUN size %ju > backing device " 1959272911Smav "size %ju", 1960232604Strasz (uintmax_t)params->lun_size_bytes, 1961232604Strasz (uintmax_t)be_lun->size_bytes); 1962232604Strasz return (EINVAL); 1963232604Strasz } 1964232604Strasz 1965232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1966286811Smav } else 1967286811Smav be_lun->size_bytes = tmp; 1968232604Strasz 1969264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1970264191Smav (caddr_t)&ps, FREAD, curthread); 1971264191Smav if (error) 1972264191Smav ps = po = 0; 1973264191Smav else { 1974264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1975264191Smav (caddr_t)&po, FREAD, curthread); 1976264191Smav if (error) 1977264191Smav po = 0; 1978264191Smav } 1979275865Smav us = ps; 1980275865Smav uo = po; 1981275865Smav 1982275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize"); 1983275865Smav if (value != NULL) 1984275865Smav ctl_expand_number(value, &ps); 1985275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset"); 1986275865Smav if (value != NULL) 1987275865Smav ctl_expand_number(value, &po); 1988264191Smav pss = ps / be_lun->blocksize; 1989264191Smav pos = po / be_lun->blocksize; 1990264191Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1991264191Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1992264191Smav be_lun->pblockexp = fls(pss) - 1; 1993264191Smav be_lun->pblockoff = (pss - pos) % pss; 1994264191Smav } 1995264191Smav 1996275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize"); 1997275865Smav if (value != NULL) 1998275865Smav ctl_expand_number(value, &us); 1999275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset"); 2000275865Smav if (value != NULL) 2001275865Smav ctl_expand_number(value, &uo); 2002275865Smav uss = us / be_lun->blocksize; 2003275865Smav uos = uo / be_lun->blocksize; 2004275865Smav if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) && 2005275865Smav ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) { 2006275865Smav be_lun->ublockexp = fls(uss) - 1; 2007275865Smav be_lun->ublockoff = (uss - uos) % uss; 2008275865Smav } 2009275865Smav 2010275920Smav be_lun->atomicblock = atomic / be_lun->blocksize; 2011275920Smav be_lun->opttxferlen = maxio / be_lun->blocksize; 2012278672Smav 2013278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2014278672Smav unmap = 1; 2015278672Smav } else { 2016278672Smav struct diocgattr_arg arg; 2017278672Smav 2018278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2019278672Smav arg.len = sizeof(arg.value.i); 2020278672Smav error = devsw->d_ioctl(dev, DIOCGATTR, 2021278672Smav (caddr_t)&arg, FREAD, curthread); 2022278672Smav unmap = (error == 0) ? arg.value.i : 0; 2023278672Smav } 2024278672Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2025278672Smav if (value != NULL) 2026278672Smav unmap = (strcmp(value, "on") == 0); 2027278672Smav if (unmap) 2028278672Smav be_lun->unmap = ctl_be_block_unmap_dev; 2029278672Smav 2030229997Sken return (0); 2031229997Sken} 2032229997Sken 2033229997Skenstatic int 2034229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2035229997Sken{ 2036229997Sken DROP_GIANT(); 2037229997Sken if (be_lun->vn) { 2038229997Sken int flags = FREAD | FWRITE; 2039229997Sken 2040229997Sken switch (be_lun->dev_type) { 2041229997Sken case CTL_BE_BLOCK_DEV: 2042229997Sken if (be_lun->backend.dev.csw) { 2043229997Sken dev_relthread(be_lun->backend.dev.cdev, 2044229997Sken be_lun->backend.dev.dev_ref); 2045229997Sken be_lun->backend.dev.csw = NULL; 2046229997Sken be_lun->backend.dev.cdev = NULL; 2047229997Sken } 2048229997Sken break; 2049229997Sken case CTL_BE_BLOCK_FILE: 2050229997Sken break; 2051229997Sken case CTL_BE_BLOCK_NONE: 2052258871Strasz break; 2053229997Sken default: 2054229997Sken panic("Unexpected backend type."); 2055229997Sken break; 2056229997Sken } 2057229997Sken 2058229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2059229997Sken be_lun->vn = NULL; 2060229997Sken 2061229997Sken switch (be_lun->dev_type) { 2062229997Sken case CTL_BE_BLOCK_DEV: 2063229997Sken break; 2064229997Sken case CTL_BE_BLOCK_FILE: 2065229997Sken if (be_lun->backend.file.cred != NULL) { 2066229997Sken crfree(be_lun->backend.file.cred); 2067229997Sken be_lun->backend.file.cred = NULL; 2068229997Sken } 2069229997Sken break; 2070229997Sken case CTL_BE_BLOCK_NONE: 2071258871Strasz break; 2072229997Sken default: 2073229997Sken panic("Unexpected backend type."); 2074229997Sken break; 2075229997Sken } 2076272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2077229997Sken } 2078229997Sken PICKUP_GIANT(); 2079229997Sken 2080229997Sken return (0); 2081229997Sken} 2082229997Sken 2083229997Skenstatic int 2084229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 2085229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2086229997Sken{ 2087229997Sken struct nameidata nd; 2088229997Sken int flags; 2089229997Sken int error; 2090229997Sken 2091229997Sken /* 2092229997Sken * XXX KDM allow a read-only option? 2093229997Sken */ 2094229997Sken flags = FREAD | FWRITE; 2095229997Sken error = 0; 2096229997Sken 2097229997Sken if (rootvnode == NULL) { 2098229997Sken snprintf(req->error_str, sizeof(req->error_str), 2099272911Smav "Root filesystem is not mounted"); 2100229997Sken return (1); 2101229997Sken } 2102229997Sken 2103285391Smjg pwd_ensure_dirs(); 2104229997Sken 2105229997Sken again: 2106229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2107229997Sken error = vn_open(&nd, &flags, 0, NULL); 2108229997Sken if (error) { 2109229997Sken /* 2110229997Sken * This is the only reasonable guess we can make as far as 2111229997Sken * path if the user doesn't give us a fully qualified path. 2112229997Sken * If they want to specify a file, they need to specify the 2113229997Sken * full path. 2114229997Sken */ 2115229997Sken if (be_lun->dev_path[0] != '/') { 2116229997Sken char *dev_path = "/dev/"; 2117229997Sken char *dev_name; 2118229997Sken 2119229997Sken /* Try adding device path at beginning of name */ 2120229997Sken dev_name = malloc(strlen(be_lun->dev_path) 2121229997Sken + strlen(dev_path) + 1, 2122229997Sken M_CTLBLK, M_WAITOK); 2123229997Sken if (dev_name) { 2124229997Sken sprintf(dev_name, "%s%s", dev_path, 2125229997Sken be_lun->dev_path); 2126229997Sken free(be_lun->dev_path, M_CTLBLK); 2127229997Sken be_lun->dev_path = dev_name; 2128229997Sken goto again; 2129229997Sken } 2130229997Sken } 2131229997Sken snprintf(req->error_str, sizeof(req->error_str), 2132272911Smav "error opening %s: %d", be_lun->dev_path, error); 2133229997Sken return (error); 2134229997Sken } 2135229997Sken 2136229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2137229997Sken 2138229997Sken be_lun->vn = nd.ni_vp; 2139229997Sken 2140229997Sken /* We only support disks and files. */ 2141229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2142229997Sken error = ctl_be_block_open_dev(be_lun, req); 2143229997Sken } else if (be_lun->vn->v_type == VREG) { 2144229997Sken error = ctl_be_block_open_file(be_lun, req); 2145229997Sken } else { 2146229997Sken error = EINVAL; 2147229997Sken snprintf(req->error_str, sizeof(req->error_str), 2148258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2149229997Sken } 2150229997Sken VOP_UNLOCK(be_lun->vn, 0); 2151229997Sken 2152286811Smav if (error != 0) 2153229997Sken ctl_be_block_close(be_lun); 2154229997Sken return (0); 2155229997Sken} 2156229997Sken 2157229997Skenstatic int 2158229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2159229997Sken{ 2160229997Sken struct ctl_be_block_lun *be_lun; 2161229997Sken struct ctl_lun_create_params *params; 2162267481Smav char num_thread_str[16]; 2163229997Sken char tmpstr[32]; 2164267481Smav char *value; 2165278672Smav int retval, num_threads; 2166267481Smav int tmp_num_threads; 2167229997Sken 2168229997Sken params = &req->reqdata.create; 2169229997Sken retval = 0; 2170272911Smav req->status = CTL_LUN_OK; 2171229997Sken 2172229997Sken num_threads = cbb_num_threads; 2173229997Sken 2174229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2175229997Sken 2176272911Smav be_lun->params = req->reqdata.create; 2177229997Sken be_lun->softc = softc; 2178229997Sken STAILQ_INIT(&be_lun->input_queue); 2179275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2180229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2181229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2182229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2183267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2184267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2185268280Smav ctl_init_opts(&be_lun->ctl_be_lun.options, 2186268280Smav req->num_be_args, req->kern_be_args); 2187229997Sken 2188264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2189256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2190229997Sken 2191229997Sken if (be_lun->lun_zone == NULL) { 2192229997Sken snprintf(req->error_str, sizeof(req->error_str), 2193272911Smav "error allocating UMA zone"); 2194229997Sken goto bailout_error; 2195229997Sken } 2196229997Sken 2197229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2198229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 2199229997Sken else 2200229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 2201229997Sken 2202229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 2203268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 2204267499Smav if (value == NULL) { 2205229997Sken snprintf(req->error_str, sizeof(req->error_str), 2206272911Smav "no file argument specified"); 2207229997Sken goto bailout_error; 2208229997Sken } 2209267499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2210286811Smav be_lun->size_bytes = params->lun_size_bytes; 2211286811Smav if (params->blocksize_bytes != 0) 2212286811Smav be_lun->blocksize = params->blocksize_bytes; 2213286811Smav else 2214286811Smav be_lun->blocksize = 512; 2215229997Sken 2216229997Sken retval = ctl_be_block_open(softc, be_lun, req); 2217286811Smav be_lun->size_blocks = be_lun->size_bytes / be_lun->blocksize; 2218229997Sken if (retval != 0) { 2219229997Sken retval = 0; 2220272911Smav req->status = CTL_LUN_WARNING; 2221229997Sken } 2222229997Sken } else { 2223229997Sken /* 2224229997Sken * For processor devices, we don't have any size. 2225229997Sken */ 2226229997Sken be_lun->blocksize = 0; 2227264191Smav be_lun->pblockexp = 0; 2228264191Smav be_lun->pblockoff = 0; 2229275865Smav be_lun->ublockexp = 0; 2230275865Smav be_lun->ublockoff = 0; 2231229997Sken be_lun->size_blocks = 0; 2232229997Sken be_lun->size_bytes = 0; 2233229997Sken be_lun->ctl_be_lun.maxlba = 0; 2234229997Sken 2235229997Sken /* 2236229997Sken * Default to just 1 thread for processor devices. 2237229997Sken */ 2238229997Sken num_threads = 1; 2239229997Sken } 2240229997Sken 2241229997Sken /* 2242229997Sken * XXX This searching loop might be refactored to be combined with 2243229997Sken * the loop above, 2244229997Sken */ 2245268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 2246267481Smav if (value != NULL) { 2247267481Smav tmp_num_threads = strtol(value, NULL, 0); 2248229997Sken 2249267481Smav /* 2250267481Smav * We don't let the user specify less than one 2251267481Smav * thread, but hope he's clueful enough not to 2252267481Smav * specify 1000 threads. 2253267481Smav */ 2254267481Smav if (tmp_num_threads < 1) { 2255267481Smav snprintf(req->error_str, sizeof(req->error_str), 2256272911Smav "invalid number of threads %s", 2257272911Smav num_thread_str); 2258267481Smav goto bailout_error; 2259229997Sken } 2260267481Smav num_threads = tmp_num_threads; 2261229997Sken } 2262229997Sken 2263229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2264229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 2265272911Smav if (be_lun->vn == NULL) 2266272911Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2267278672Smav if (be_lun->unmap != NULL) 2268264274Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2269275568Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2270275568Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_SERSEQ_READ; 2271229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 2272272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2273272911Smav 0 : (be_lun->size_blocks - 1); 2274229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2275264191Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2276264191Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2277275865Smav be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp; 2278275865Smav be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff; 2279275920Smav be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock; 2280275920Smav be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen; 2281229997Sken /* Tell the user the blocksize we ended up using */ 2282272911Smav params->lun_size_bytes = be_lun->size_bytes; 2283229997Sken params->blocksize_bytes = be_lun->blocksize; 2284229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2285229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2286229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2287229997Sken } else 2288229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 2289229997Sken 2290229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2291229997Sken be_lun->ctl_be_lun.lun_config_status = 2292229997Sken ctl_be_block_lun_config_status; 2293229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2294229997Sken 2295229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2296229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2297229997Sken softc->num_luns); 2298229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2299275953Smav MIN(sizeof(be_lun->ctl_be_lun.serial_num), 2300229997Sken sizeof(tmpstr))); 2301229997Sken 2302229997Sken /* Tell the user what we used for a serial number */ 2303229997Sken strncpy((char *)params->serial_num, tmpstr, 2304275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2305229997Sken } else { 2306229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 2307229997Sken params->serial_num, 2308275953Smav MIN(sizeof(be_lun->ctl_be_lun.serial_num), 2309229997Sken sizeof(params->serial_num))); 2310229997Sken } 2311229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2312229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2313229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2314275953Smav MIN(sizeof(be_lun->ctl_be_lun.device_id), 2315229997Sken sizeof(tmpstr))); 2316229997Sken 2317229997Sken /* Tell the user what we used for a device ID */ 2318229997Sken strncpy((char *)params->device_id, tmpstr, 2319275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2320229997Sken } else { 2321229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 2322229997Sken params->device_id, 2323275953Smav MIN(sizeof(be_lun->ctl_be_lun.device_id), 2324275953Smav sizeof(params->device_id))); 2325229997Sken } 2326229997Sken 2327229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2328229997Sken 2329229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2330229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2331229997Sken 2332229997Sken if (be_lun->io_taskqueue == NULL) { 2333229997Sken snprintf(req->error_str, sizeof(req->error_str), 2334272911Smav "unable to create taskqueue"); 2335229997Sken goto bailout_error; 2336229997Sken } 2337229997Sken 2338229997Sken /* 2339229997Sken * Note that we start the same number of threads by default for 2340229997Sken * both the file case and the block device case. For the file 2341229997Sken * case, we need multiple threads to allow concurrency, because the 2342229997Sken * vnode interface is designed to be a blocking interface. For the 2343229997Sken * block device case, ZFS zvols at least will block the caller's 2344229997Sken * context in many instances, and so we need multiple threads to 2345229997Sken * overcome that problem. Other block devices don't need as many 2346229997Sken * threads, but they shouldn't cause too many problems. 2347229997Sken * 2348229997Sken * If the user wants to just have a single thread for a block 2349229997Sken * device, he can specify that when the LUN is created, or change 2350229997Sken * the tunable/sysctl to alter the default number of threads. 2351229997Sken */ 2352229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2353229997Sken /*num threads*/num_threads, 2354229997Sken /*priority*/PWAIT, 2355229997Sken /*thread name*/ 2356229997Sken "%s taskq", be_lun->lunname); 2357229997Sken 2358229997Sken if (retval != 0) 2359229997Sken goto bailout_error; 2360229997Sken 2361229997Sken be_lun->num_threads = num_threads; 2362229997Sken 2363229997Sken mtx_lock(&softc->lock); 2364229997Sken softc->num_luns++; 2365229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2366229997Sken 2367229997Sken mtx_unlock(&softc->lock); 2368229997Sken 2369229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 2370229997Sken if (retval != 0) { 2371229997Sken mtx_lock(&softc->lock); 2372229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2373229997Sken links); 2374229997Sken softc->num_luns--; 2375229997Sken mtx_unlock(&softc->lock); 2376229997Sken snprintf(req->error_str, sizeof(req->error_str), 2377272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2378272911Smav "details", retval); 2379229997Sken retval = 0; 2380229997Sken goto bailout_error; 2381229997Sken } 2382229997Sken 2383229997Sken mtx_lock(&softc->lock); 2384229997Sken 2385229997Sken /* 2386229997Sken * Tell the config_status routine that we're waiting so it won't 2387229997Sken * clean up the LUN in the event of an error. 2388229997Sken */ 2389229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2390229997Sken 2391229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2392229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2393229997Sken if (retval == EINTR) 2394229997Sken break; 2395229997Sken } 2396229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2397229997Sken 2398229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2399229997Sken snprintf(req->error_str, sizeof(req->error_str), 2400272911Smav "LUN configuration error, see dmesg for details"); 2401229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2402229997Sken links); 2403229997Sken softc->num_luns--; 2404229997Sken mtx_unlock(&softc->lock); 2405229997Sken goto bailout_error; 2406229997Sken } else { 2407229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2408229997Sken } 2409229997Sken 2410229997Sken mtx_unlock(&softc->lock); 2411229997Sken 2412229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2413229997Sken be_lun->blocksize, 2414229997Sken DEVSTAT_ALL_SUPPORTED, 2415229997Sken be_lun->ctl_be_lun.lun_type 2416229997Sken | DEVSTAT_TYPE_IF_OTHER, 2417229997Sken DEVSTAT_PRIORITY_OTHER); 2418229997Sken 2419229997Sken return (retval); 2420229997Sken 2421229997Skenbailout_error: 2422229997Sken req->status = CTL_LUN_ERROR; 2423229997Sken 2424267429Smav if (be_lun->io_taskqueue != NULL) 2425267429Smav taskqueue_free(be_lun->io_taskqueue); 2426229997Sken ctl_be_block_close(be_lun); 2427267429Smav if (be_lun->dev_path != NULL) 2428267429Smav free(be_lun->dev_path, M_CTLBLK); 2429267429Smav if (be_lun->lun_zone != NULL) 2430267429Smav uma_zdestroy(be_lun->lun_zone); 2431268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2432267877Smav mtx_destroy(&be_lun->queue_lock); 2433267877Smav mtx_destroy(&be_lun->io_lock); 2434229997Sken free(be_lun, M_CTLBLK); 2435229997Sken 2436229997Sken return (retval); 2437229997Sken} 2438229997Sken 2439229997Skenstatic int 2440229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2441229997Sken{ 2442229997Sken struct ctl_lun_rm_params *params; 2443229997Sken struct ctl_be_block_lun *be_lun; 2444229997Sken int retval; 2445229997Sken 2446229997Sken params = &req->reqdata.rm; 2447229997Sken 2448229997Sken mtx_lock(&softc->lock); 2449229997Sken 2450229997Sken be_lun = NULL; 2451229997Sken 2452229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2453229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2454229997Sken break; 2455229997Sken } 2456229997Sken mtx_unlock(&softc->lock); 2457229997Sken 2458229997Sken if (be_lun == NULL) { 2459229997Sken snprintf(req->error_str, sizeof(req->error_str), 2460272911Smav "LUN %u is not managed by the block backend", 2461272911Smav params->lun_id); 2462229997Sken goto bailout_error; 2463229997Sken } 2464229997Sken 2465229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2466229997Sken 2467229997Sken if (retval != 0) { 2468229997Sken snprintf(req->error_str, sizeof(req->error_str), 2469272911Smav "error %d returned from ctl_disable_lun() for " 2470272911Smav "LUN %d", retval, params->lun_id); 2471229997Sken goto bailout_error; 2472229997Sken 2473229997Sken } 2474229997Sken 2475229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2476229997Sken if (retval != 0) { 2477229997Sken snprintf(req->error_str, sizeof(req->error_str), 2478272911Smav "error %d returned from ctl_invalidate_lun() for " 2479272911Smav "LUN %d", retval, params->lun_id); 2480229997Sken goto bailout_error; 2481229997Sken } 2482229997Sken 2483229997Sken mtx_lock(&softc->lock); 2484229997Sken 2485229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2486229997Sken 2487229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2488229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2489229997Sken if (retval == EINTR) 2490229997Sken break; 2491229997Sken } 2492229997Sken 2493229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2494229997Sken 2495229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2496229997Sken snprintf(req->error_str, sizeof(req->error_str), 2497272911Smav "interrupted waiting for LUN to be freed"); 2498229997Sken mtx_unlock(&softc->lock); 2499229997Sken goto bailout_error; 2500229997Sken } 2501229997Sken 2502229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2503229997Sken 2504229997Sken softc->num_luns--; 2505229997Sken mtx_unlock(&softc->lock); 2506229997Sken 2507229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2508229997Sken 2509229997Sken taskqueue_free(be_lun->io_taskqueue); 2510229997Sken 2511229997Sken ctl_be_block_close(be_lun); 2512229997Sken 2513229997Sken if (be_lun->disk_stats != NULL) 2514229997Sken devstat_remove_entry(be_lun->disk_stats); 2515229997Sken 2516229997Sken uma_zdestroy(be_lun->lun_zone); 2517229997Sken 2518268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2519229997Sken free(be_lun->dev_path, M_CTLBLK); 2520267877Smav mtx_destroy(&be_lun->queue_lock); 2521267877Smav mtx_destroy(&be_lun->io_lock); 2522229997Sken free(be_lun, M_CTLBLK); 2523229997Sken 2524229997Sken req->status = CTL_LUN_OK; 2525229997Sken 2526229997Sken return (0); 2527229997Sken 2528229997Skenbailout_error: 2529229997Sken 2530229997Sken req->status = CTL_LUN_ERROR; 2531229997Sken 2532229997Sken return (0); 2533229997Sken} 2534229997Sken 2535232604Straszstatic int 2536232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2537232604Strasz struct ctl_lun_req *req) 2538232604Strasz{ 2539232604Strasz struct vattr vattr; 2540232604Strasz int error; 2541272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2542232604Strasz 2543232604Strasz if (params->lun_size_bytes != 0) { 2544232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2545232604Strasz } else { 2546271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2547232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2548271794Smav VOP_UNLOCK(be_lun->vn, 0); 2549232604Strasz if (error != 0) { 2550232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2551232604Strasz "error calling VOP_GETATTR() for file %s", 2552232604Strasz be_lun->dev_path); 2553232604Strasz return (error); 2554232604Strasz } 2555232604Strasz 2556232604Strasz be_lun->size_bytes = vattr.va_size; 2557232604Strasz } 2558232604Strasz 2559232604Strasz return (0); 2560232604Strasz} 2561232604Strasz 2562232604Straszstatic int 2563232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2564232604Strasz struct ctl_lun_req *req) 2565232604Strasz{ 2566271794Smav struct ctl_be_block_devdata *dev_data; 2567232604Strasz int error; 2568272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2569232604Strasz uint64_t size_bytes; 2570232604Strasz 2571271794Smav dev_data = &be_lun->backend.dev; 2572271794Smav if (!dev_data->csw->d_ioctl) { 2573232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2574272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2575232604Strasz return (ENODEV); 2576232604Strasz } 2577232604Strasz 2578271794Smav error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2579232604Strasz (caddr_t)&size_bytes, FREAD, 2580232604Strasz curthread); 2581232604Strasz if (error) { 2582232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2583272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2584272911Smav "on %s!", error, be_lun->dev_path); 2585232604Strasz return (error); 2586232604Strasz } 2587232604Strasz 2588232604Strasz if (params->lun_size_bytes != 0) { 2589232604Strasz if (params->lun_size_bytes > size_bytes) { 2590232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2591272911Smav "requested LUN size %ju > backing device " 2592272911Smav "size %ju", 2593232604Strasz (uintmax_t)params->lun_size_bytes, 2594232604Strasz (uintmax_t)size_bytes); 2595232604Strasz return (EINVAL); 2596232604Strasz } 2597232604Strasz 2598232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2599232604Strasz } else { 2600232604Strasz be_lun->size_bytes = size_bytes; 2601232604Strasz } 2602232604Strasz 2603232604Strasz return (0); 2604232604Strasz} 2605232604Strasz 2606232604Straszstatic int 2607232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2608232604Strasz{ 2609232604Strasz struct ctl_lun_modify_params *params; 2610232604Strasz struct ctl_be_block_lun *be_lun; 2611271794Smav uint64_t oldsize; 2612241896Skib int error; 2613232604Strasz 2614232604Strasz params = &req->reqdata.modify; 2615232604Strasz 2616232604Strasz mtx_lock(&softc->lock); 2617232604Strasz be_lun = NULL; 2618232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2619232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2620232604Strasz break; 2621232604Strasz } 2622232604Strasz mtx_unlock(&softc->lock); 2623232604Strasz 2624232604Strasz if (be_lun == NULL) { 2625232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2626272911Smav "LUN %u is not managed by the block backend", 2627272911Smav params->lun_id); 2628232604Strasz goto bailout_error; 2629232604Strasz } 2630232604Strasz 2631272911Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2632232604Strasz 2633274253Smav oldsize = be_lun->size_bytes; 2634272911Smav if (be_lun->vn == NULL) 2635272911Smav error = ctl_be_block_open(softc, be_lun, req); 2636285030Smav else if (vn_isdisk(be_lun->vn, &error)) 2637285030Smav error = ctl_be_block_modify_dev(be_lun, req); 2638272911Smav else if (be_lun->vn->v_type == VREG) 2639232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2640232604Strasz else 2641285030Smav error = EINVAL; 2642286811Smav be_lun->size_blocks = be_lun->size_bytes / be_lun->blocksize; 2643232604Strasz 2644274253Smav if (error == 0 && be_lun->size_bytes != oldsize) { 2645232604Strasz 2646271794Smav /* 2647271794Smav * The maximum LBA is the size - 1. 2648271794Smav * 2649271794Smav * XXX: Note that this field is being updated without locking, 2650271794Smav * which might cause problems on 32-bit architectures. 2651271794Smav */ 2652278672Smav if (be_lun->unmap != NULL) 2653278672Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2654272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2655272911Smav 0 : (be_lun->size_blocks - 1); 2656272911Smav be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2657272911Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2658272911Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2659275865Smav be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp; 2660275865Smav be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff; 2661275920Smav be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock; 2662275920Smav be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen; 2663271794Smav ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2664272911Smav if (oldsize == 0 && be_lun->size_blocks != 0) 2665272911Smav ctl_lun_online(&be_lun->ctl_be_lun); 2666271794Smav } 2667232604Strasz 2668232604Strasz /* Tell the user the exact size we ended up using */ 2669232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2670232604Strasz 2671272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2672232604Strasz 2673232604Strasz return (0); 2674232604Strasz 2675232604Straszbailout_error: 2676232604Strasz req->status = CTL_LUN_ERROR; 2677232604Strasz 2678232604Strasz return (0); 2679232604Strasz} 2680232604Strasz 2681229997Skenstatic void 2682229997Skenctl_be_block_lun_shutdown(void *be_lun) 2683229997Sken{ 2684229997Sken struct ctl_be_block_lun *lun; 2685229997Sken struct ctl_be_block_softc *softc; 2686229997Sken 2687229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2688229997Sken 2689229997Sken softc = lun->softc; 2690229997Sken 2691229997Sken mtx_lock(&softc->lock); 2692229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2693229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2694229997Sken wakeup(lun); 2695229997Sken mtx_unlock(&softc->lock); 2696229997Sken 2697229997Sken} 2698229997Sken 2699229997Skenstatic void 2700229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2701229997Sken{ 2702229997Sken struct ctl_be_block_lun *lun; 2703229997Sken struct ctl_be_block_softc *softc; 2704229997Sken 2705229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2706229997Sken softc = lun->softc; 2707229997Sken 2708229997Sken if (status == CTL_LUN_CONFIG_OK) { 2709229997Sken mtx_lock(&softc->lock); 2710229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2711229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2712229997Sken wakeup(lun); 2713229997Sken mtx_unlock(&softc->lock); 2714229997Sken 2715229997Sken /* 2716229997Sken * We successfully added the LUN, attempt to enable it. 2717229997Sken */ 2718229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2719229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2720229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2721229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2722229997Sken __func__); 2723229997Sken } 2724229997Sken } 2725229997Sken 2726229997Sken return; 2727229997Sken } 2728229997Sken 2729229997Sken 2730229997Sken mtx_lock(&softc->lock); 2731229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2732229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2733229997Sken wakeup(lun); 2734229997Sken mtx_unlock(&softc->lock); 2735229997Sken} 2736229997Sken 2737229997Sken 2738229997Skenstatic int 2739229997Skenctl_be_block_config_write(union ctl_io *io) 2740229997Sken{ 2741229997Sken struct ctl_be_block_lun *be_lun; 2742229997Sken struct ctl_be_lun *ctl_be_lun; 2743229997Sken int retval; 2744229997Sken 2745229997Sken retval = 0; 2746229997Sken 2747229997Sken DPRINTF("entered\n"); 2748229997Sken 2749229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2750229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2751229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2752229997Sken 2753229997Sken switch (io->scsiio.cdb[0]) { 2754229997Sken case SYNCHRONIZE_CACHE: 2755229997Sken case SYNCHRONIZE_CACHE_16: 2756264274Smav case WRITE_SAME_10: 2757264274Smav case WRITE_SAME_16: 2758264274Smav case UNMAP: 2759229997Sken /* 2760229997Sken * The upper level CTL code will filter out any CDBs with 2761229997Sken * the immediate bit set and return the proper error. 2762229997Sken * 2763229997Sken * We don't really need to worry about what LBA range the 2764229997Sken * user asked to be synced out. When they issue a sync 2765229997Sken * cache command, we'll sync out the whole thing. 2766229997Sken */ 2767267877Smav mtx_lock(&be_lun->queue_lock); 2768229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2769229997Sken links); 2770267877Smav mtx_unlock(&be_lun->queue_lock); 2771229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2772229997Sken break; 2773229997Sken case START_STOP_UNIT: { 2774229997Sken struct scsi_start_stop_unit *cdb; 2775229997Sken 2776229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2777229997Sken 2778229997Sken if (cdb->how & SSS_START) 2779229997Sken retval = ctl_start_lun(ctl_be_lun); 2780229997Sken else { 2781229997Sken retval = ctl_stop_lun(ctl_be_lun); 2782229997Sken /* 2783229997Sken * XXX KDM Copan-specific offline behavior. 2784229997Sken * Figure out a reasonable way to port this? 2785229997Sken */ 2786229997Sken#ifdef NEEDTOPORT 2787229997Sken if ((retval == 0) 2788229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2789229997Sken retval = ctl_lun_offline(ctl_be_lun); 2790229997Sken#endif 2791229997Sken } 2792229997Sken 2793229997Sken /* 2794229997Sken * In general, the above routines should not fail. They 2795229997Sken * just set state for the LUN. So we've got something 2796229997Sken * pretty wrong here if we can't start or stop the LUN. 2797229997Sken */ 2798229997Sken if (retval != 0) { 2799229997Sken ctl_set_internal_failure(&io->scsiio, 2800229997Sken /*sks_valid*/ 1, 2801229997Sken /*retry_count*/ 0xf051); 2802229997Sken retval = CTL_RETVAL_COMPLETE; 2803229997Sken } else { 2804229997Sken ctl_set_success(&io->scsiio); 2805229997Sken } 2806229997Sken ctl_config_write_done(io); 2807229997Sken break; 2808229997Sken } 2809229997Sken default: 2810229997Sken ctl_set_invalid_opcode(&io->scsiio); 2811229997Sken ctl_config_write_done(io); 2812229997Sken retval = CTL_RETVAL_COMPLETE; 2813229997Sken break; 2814229997Sken } 2815229997Sken 2816229997Sken return (retval); 2817229997Sken} 2818229997Sken 2819229997Skenstatic int 2820229997Skenctl_be_block_config_read(union ctl_io *io) 2821229997Sken{ 2822275474Smav struct ctl_be_block_lun *be_lun; 2823275474Smav struct ctl_be_lun *ctl_be_lun; 2824275474Smav int retval = 0; 2825275474Smav 2826275474Smav DPRINTF("entered\n"); 2827275474Smav 2828275474Smav ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2829275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2830275474Smav be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2831275474Smav 2832275474Smav switch (io->scsiio.cdb[0]) { 2833275474Smav case SERVICE_ACTION_IN: 2834275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2835275474Smav mtx_lock(&be_lun->queue_lock); 2836275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2837275474Smav &io->io_hdr, links); 2838275474Smav mtx_unlock(&be_lun->queue_lock); 2839275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2840275474Smav &be_lun->io_task); 2841275474Smav retval = CTL_RETVAL_QUEUED; 2842275474Smav break; 2843275474Smav } 2844275474Smav ctl_set_invalid_field(&io->scsiio, 2845275474Smav /*sks_valid*/ 1, 2846275474Smav /*command*/ 1, 2847275474Smav /*field*/ 1, 2848275474Smav /*bit_valid*/ 1, 2849275474Smav /*bit*/ 4); 2850275474Smav ctl_config_read_done(io); 2851275474Smav retval = CTL_RETVAL_COMPLETE; 2852275474Smav break; 2853275474Smav default: 2854275474Smav ctl_set_invalid_opcode(&io->scsiio); 2855275474Smav ctl_config_read_done(io); 2856275474Smav retval = CTL_RETVAL_COMPLETE; 2857275474Smav break; 2858275474Smav } 2859275474Smav 2860275474Smav return (retval); 2861229997Sken} 2862229997Sken 2863229997Skenstatic int 2864229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2865229997Sken{ 2866229997Sken struct ctl_be_block_lun *lun; 2867229997Sken int retval; 2868229997Sken 2869229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2870229997Sken retval = 0; 2871229997Sken 2872268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2873229997Sken 2874229997Sken if (retval != 0) 2875229997Sken goto bailout; 2876229997Sken 2877229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2878229997Sken 2879229997Sken if (retval != 0) 2880229997Sken goto bailout; 2881229997Sken 2882268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2883229997Sken 2884229997Skenbailout: 2885229997Sken 2886229997Sken return (retval); 2887229997Sken} 2888229997Sken 2889274154Smavstatic uint64_t 2890274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2891274154Smav{ 2892274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2893274154Smav 2894274154Smav if (lun->getattr == NULL) 2895274154Smav return (UINT64_MAX); 2896274154Smav return (lun->getattr(lun, attrname)); 2897274154Smav} 2898274154Smav 2899229997Skenint 2900229997Skenctl_be_block_init(void) 2901229997Sken{ 2902229997Sken struct ctl_be_block_softc *softc; 2903229997Sken int retval; 2904229997Sken 2905229997Sken softc = &backend_block_softc; 2906229997Sken retval = 0; 2907229997Sken 2908267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2909264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2910264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2911229997Sken STAILQ_INIT(&softc->disk_list); 2912229997Sken STAILQ_INIT(&softc->lun_list); 2913229997Sken 2914229997Sken return (retval); 2915229997Sken} 2916