ctl_backend_block.c revision 278625
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 278625 2015-02-12 15:46:44Z 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_frontend_internal.h> 88229997Sken#include <cam/ctl/ctl_ioctl.h> 89229997Sken#include <cam/ctl/ctl_scsi_all.h> 90229997Sken#include <cam/ctl/ctl_error.h> 91229997Sken 92229997Sken/* 93264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 94264886Smav * I/O. If we get an I/O larger than that, we'll split it. 95229997Sken */ 96267537Smav#define CTLBLK_HALF_IO_SIZE (512 * 1024) 97267537Smav#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 98264886Smav#define CTLBLK_MAX_SEG MAXPHYS 99267537Smav#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 100267537Smav#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 101229997Sken 102229997Sken#ifdef CTLBLK_DEBUG 103229997Sken#define DPRINTF(fmt, args...) \ 104229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 105229997Sken#else 106229997Sken#define DPRINTF(fmt, args...) do {} while(0) 107229997Sken#endif 108229997Sken 109267519Smav#define PRIV(io) \ 110267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 111267537Smav#define ARGS(io) \ 112267537Smav ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 113267519Smav 114229997SkenSDT_PROVIDER_DEFINE(cbb); 115229997Sken 116229997Skentypedef enum { 117229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 118229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 119229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 120229997Sken CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 121229997Sken} ctl_be_block_lun_flags; 122229997Sken 123229997Skentypedef enum { 124229997Sken CTL_BE_BLOCK_NONE, 125229997Sken CTL_BE_BLOCK_DEV, 126229997Sken CTL_BE_BLOCK_FILE 127229997Sken} ctl_be_block_type; 128229997Sken 129229997Skenstruct ctl_be_block_devdata { 130229997Sken struct cdev *cdev; 131229997Sken struct cdevsw *csw; 132229997Sken int dev_ref; 133229997Sken}; 134229997Sken 135229997Skenstruct ctl_be_block_filedata { 136229997Sken struct ucred *cred; 137229997Sken}; 138229997Sken 139229997Skenunion ctl_be_block_bedata { 140229997Sken struct ctl_be_block_devdata dev; 141229997Sken struct ctl_be_block_filedata file; 142229997Sken}; 143229997Sken 144229997Skenstruct ctl_be_block_io; 145229997Skenstruct ctl_be_block_lun; 146229997Sken 147229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 148229997Sken struct ctl_be_block_io *beio); 149274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 150274154Smav const char *attrname); 151229997Sken 152229997Sken/* 153229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 154229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 155229997Sken */ 156229997Skenstruct ctl_be_block_lun { 157272911Smav struct ctl_lun_create_params params; 158229997Sken struct ctl_block_disk *disk; 159229997Sken char lunname[32]; 160229997Sken char *dev_path; 161229997Sken ctl_be_block_type dev_type; 162229997Sken struct vnode *vn; 163229997Sken union ctl_be_block_bedata backend; 164229997Sken cbb_dispatch_t dispatch; 165229997Sken cbb_dispatch_t lun_flush; 166264274Smav cbb_dispatch_t unmap; 167275474Smav cbb_dispatch_t get_lba_status; 168274154Smav cbb_getattr_t getattr; 169229997Sken uma_zone_t lun_zone; 170229997Sken uint64_t size_blocks; 171229997Sken uint64_t size_bytes; 172229997Sken uint32_t blocksize; 173229997Sken int blocksize_shift; 174264191Smav uint16_t pblockexp; 175264191Smav uint16_t pblockoff; 176275865Smav uint16_t ublockexp; 177275865Smav uint16_t ublockoff; 178275920Smav uint32_t atomicblock; 179275920Smav uint32_t opttxferlen; 180229997Sken struct ctl_be_block_softc *softc; 181229997Sken struct devstat *disk_stats; 182229997Sken ctl_be_block_lun_flags flags; 183229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 184229997Sken struct ctl_be_lun ctl_be_lun; 185229997Sken struct taskqueue *io_taskqueue; 186229997Sken struct task io_task; 187229997Sken int num_threads; 188229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 189275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 190229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 191229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 192267877Smav struct mtx_padalign io_lock; 193267877Smav struct mtx_padalign queue_lock; 194229997Sken}; 195229997Sken 196229997Sken/* 197229997Sken * Overall softc structure for the block backend module. 198229997Sken */ 199229997Skenstruct ctl_be_block_softc { 200229997Sken struct mtx lock; 201229997Sken int num_disks; 202229997Sken STAILQ_HEAD(, ctl_block_disk) disk_list; 203229997Sken int num_luns; 204229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 205229997Sken}; 206229997Sken 207229997Skenstatic struct ctl_be_block_softc backend_block_softc; 208229997Sken 209229997Sken/* 210229997Sken * Per-I/O information. 211229997Sken */ 212229997Skenstruct ctl_be_block_io { 213229997Sken union ctl_io *io; 214229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 215229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 216229997Sken int bio_cmd; 217229997Sken int num_segs; 218229997Sken int num_bios_sent; 219229997Sken int num_bios_done; 220229997Sken int send_complete; 221229997Sken int num_errors; 222229997Sken struct bintime ds_t0; 223229997Sken devstat_tag_type ds_tag_type; 224229997Sken devstat_trans_flags ds_trans_type; 225229997Sken uint64_t io_len; 226229997Sken uint64_t io_offset; 227229997Sken struct ctl_be_block_softc *softc; 228229997Sken struct ctl_be_block_lun *lun; 229264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 230229997Sken}; 231229997Sken 232229997Skenstatic int cbb_num_threads = 14; 233229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 234229997Sken "CAM Target Layer Block Backend"); 235267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 236229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 237229997Sken 238229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 239229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 240229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 241229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 242229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 243229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 244229997Sken struct ctl_be_block_io *beio); 245229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 246229997Sken struct ctl_be_block_io *beio); 247275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 248275474Smav struct ctl_be_block_io *beio); 249275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 250275481Smav const char *attrname); 251229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 252229997Sken struct ctl_be_block_io *beio); 253264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 254264274Smav struct ctl_be_block_io *beio); 255229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 256229997Sken struct ctl_be_block_io *beio); 257274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 258274154Smav const char *attrname); 259275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 260275474Smav union ctl_io *io); 261229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 262229997Sken union ctl_io *io); 263229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 264229997Sken union ctl_io *io); 265229997Skenstatic void ctl_be_block_worker(void *context, int pending); 266229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 267229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 268229997Sken int flag, struct thread *td); 269229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 270229997Sken struct ctl_lun_req *req); 271229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 272229997Sken struct ctl_lun_req *req); 273229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 274229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 275229997Sken struct ctl_be_block_lun *be_lun, 276229997Sken struct ctl_lun_req *req); 277229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 278229997Sken struct ctl_lun_req *req); 279229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 280229997Sken struct ctl_lun_req *req); 281232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 282232604Strasz struct ctl_lun_req *req); 283232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 284232604Strasz struct ctl_lun_req *req); 285232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 286232604Strasz struct ctl_lun_req *req); 287229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 288229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 289229997Sken ctl_lun_config_status status); 290229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 291229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 292229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 293274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 294229997Skenint ctl_be_block_init(void); 295229997Sken 296229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 297229997Sken{ 298230334Sken .name = "block", 299230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 300230334Sken .init = ctl_be_block_init, 301230334Sken .data_submit = ctl_be_block_submit, 302230334Sken .data_move_done = ctl_be_block_move_done, 303230334Sken .config_read = ctl_be_block_config_read, 304230334Sken .config_write = ctl_be_block_config_write, 305230334Sken .ioctl = ctl_be_block_ioctl, 306274154Smav .lun_info = ctl_be_block_lun_info, 307274154Smav .lun_attr = ctl_be_block_lun_attr 308229997Sken}; 309229997Sken 310229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 311229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 312229997Sken 313264020Straszstatic uma_zone_t beio_zone; 314264020Strasz 315229997Skenstatic struct ctl_be_block_io * 316229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 317229997Sken{ 318229997Sken struct ctl_be_block_io *beio; 319229997Sken 320264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 321264020Strasz beio->softc = softc; 322229997Sken return (beio); 323229997Sken} 324229997Sken 325229997Skenstatic void 326229997Skenctl_free_beio(struct ctl_be_block_io *beio) 327229997Sken{ 328229997Sken int duplicate_free; 329229997Sken int i; 330229997Sken 331229997Sken duplicate_free = 0; 332229997Sken 333229997Sken for (i = 0; i < beio->num_segs; i++) { 334229997Sken if (beio->sg_segs[i].addr == NULL) 335229997Sken duplicate_free++; 336229997Sken 337229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 338229997Sken beio->sg_segs[i].addr = NULL; 339267537Smav 340267537Smav /* For compare we had two equal S/G lists. */ 341267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 342267537Smav uma_zfree(beio->lun->lun_zone, 343267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 344267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 345267537Smav } 346229997Sken } 347229997Sken 348229997Sken if (duplicate_free > 0) { 349229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 350229997Sken duplicate_free, beio->num_segs); 351229997Sken } 352229997Sken 353264020Strasz uma_zfree(beio_zone, beio); 354229997Sken} 355229997Sken 356229997Skenstatic void 357229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 358229997Sken{ 359267877Smav union ctl_io *io = beio->io; 360229997Sken 361264274Smav if (beio->beio_cont != NULL) { 362264274Smav beio->beio_cont(beio); 363264274Smav } else { 364264274Smav ctl_free_beio(beio); 365267537Smav ctl_data_submit_done(io); 366264274Smav } 367229997Sken} 368229997Sken 369229997Skenstatic int 370229997Skenctl_be_block_move_done(union ctl_io *io) 371229997Sken{ 372229997Sken struct ctl_be_block_io *beio; 373229997Sken struct ctl_be_block_lun *be_lun; 374267537Smav struct ctl_lba_len_flags *lbalen; 375229997Sken#ifdef CTL_TIME_IO 376229997Sken struct bintime cur_bt; 377267537Smav#endif 378267537Smav int i; 379229997Sken 380267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 381229997Sken be_lun = beio->lun; 382229997Sken 383229997Sken DPRINTF("entered\n"); 384229997Sken 385229997Sken#ifdef CTL_TIME_IO 386229997Sken getbintime(&cur_bt); 387229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 388229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 389229997Sken io->io_hdr.num_dmas++; 390229997Sken#endif 391267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 392229997Sken 393229997Sken /* 394229997Sken * We set status at this point for read commands, and write 395229997Sken * commands with errors. 396229997Sken */ 397275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 398275058Smav ; 399275058Smav } else if ((io->io_hdr.port_status == 0) && 400267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 401267537Smav lbalen = ARGS(beio->io); 402267537Smav if (lbalen->flags & CTL_LLF_READ) { 403267537Smav ctl_set_success(&io->scsiio); 404267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 405267537Smav /* We have two data blocks ready for comparison. */ 406267537Smav for (i = 0; i < beio->num_segs; i++) { 407267537Smav if (memcmp(beio->sg_segs[i].addr, 408267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 409267537Smav beio->sg_segs[i].len) != 0) 410267537Smav break; 411267537Smav } 412267537Smav if (i < beio->num_segs) 413267537Smav ctl_set_sense(&io->scsiio, 414267537Smav /*current_error*/ 1, 415267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 416267537Smav /*asc*/ 0x1D, 417267537Smav /*ascq*/ 0x00, 418267537Smav SSD_ELEM_NONE); 419267537Smav else 420267537Smav ctl_set_success(&io->scsiio); 421267537Smav } 422275058Smav } else if ((io->io_hdr.port_status != 0) && 423275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 424275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 425229997Sken /* 426229997Sken * For hardware error sense keys, the sense key 427229997Sken * specific value is defined to be a retry count, 428229997Sken * but we use it to pass back an internal FETD 429229997Sken * error code. XXX KDM Hopefully the FETD is only 430229997Sken * using 16 bits for an error code, since that's 431229997Sken * all the space we have in the sks field. 432229997Sken */ 433229997Sken ctl_set_internal_failure(&io->scsiio, 434229997Sken /*sks_valid*/ 1, 435229997Sken /*retry_count*/ 436229997Sken io->io_hdr.port_status); 437229997Sken } 438229997Sken 439229997Sken /* 440229997Sken * If this is a read, or a write with errors, it is done. 441229997Sken */ 442229997Sken if ((beio->bio_cmd == BIO_READ) 443229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 444229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 445229997Sken ctl_complete_beio(beio); 446229997Sken return (0); 447229997Sken } 448229997Sken 449229997Sken /* 450229997Sken * At this point, we have a write and the DMA completed 451229997Sken * successfully. We now have to queue it to the task queue to 452229997Sken * execute the backend I/O. That is because we do blocking 453229997Sken * memory allocations, and in the file backing case, blocking I/O. 454229997Sken * This move done routine is generally called in the SIM's 455229997Sken * interrupt context, and therefore we cannot block. 456229997Sken */ 457267877Smav mtx_lock(&be_lun->queue_lock); 458229997Sken /* 459229997Sken * XXX KDM make sure that links is okay to use at this point. 460229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 461229997Sken * or deal with resource allocation here. 462229997Sken */ 463229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 464267877Smav mtx_unlock(&be_lun->queue_lock); 465229997Sken 466229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 467229997Sken 468229997Sken return (0); 469229997Sken} 470229997Sken 471229997Skenstatic void 472229997Skenctl_be_block_biodone(struct bio *bio) 473229997Sken{ 474229997Sken struct ctl_be_block_io *beio; 475229997Sken struct ctl_be_block_lun *be_lun; 476229997Sken union ctl_io *io; 477261538Smav int error; 478229997Sken 479229997Sken beio = bio->bio_caller1; 480229997Sken be_lun = beio->lun; 481229997Sken io = beio->io; 482229997Sken 483229997Sken DPRINTF("entered\n"); 484229997Sken 485261538Smav error = bio->bio_error; 486267877Smav mtx_lock(&be_lun->io_lock); 487261538Smav if (error != 0) 488229997Sken beio->num_errors++; 489229997Sken 490229997Sken beio->num_bios_done++; 491229997Sken 492229997Sken /* 493229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 494229997Sken * during the free might cause it to complain. 495229997Sken */ 496229997Sken g_destroy_bio(bio); 497229997Sken 498229997Sken /* 499229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 500229997Sken * complete, then we're done. 501229997Sken */ 502229997Sken if ((beio->send_complete == 0) 503229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 504267877Smav mtx_unlock(&be_lun->io_lock); 505229997Sken return; 506229997Sken } 507229997Sken 508229997Sken /* 509229997Sken * At this point, we've verified that we are the last I/O to 510229997Sken * complete, so it's safe to drop the lock. 511229997Sken */ 512267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 513267877Smav beio->ds_tag_type, beio->ds_trans_type, 514267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 515267877Smav mtx_unlock(&be_lun->io_lock); 516229997Sken 517229997Sken /* 518229997Sken * If there are any errors from the backing device, we fail the 519229997Sken * entire I/O with a medium error. 520229997Sken */ 521229997Sken if (beio->num_errors > 0) { 522261538Smav if (error == EOPNOTSUPP) { 523261538Smav ctl_set_invalid_opcode(&io->scsiio); 524273809Smav } else if (error == ENOSPC) { 525273809Smav ctl_set_space_alloc_fail(&io->scsiio); 526261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 527229997Sken /* XXX KDM is there is a better error here? */ 528229997Sken ctl_set_internal_failure(&io->scsiio, 529229997Sken /*sks_valid*/ 1, 530229997Sken /*retry_count*/ 0xbad2); 531229997Sken } else 532229997Sken ctl_set_medium_error(&io->scsiio); 533229997Sken ctl_complete_beio(beio); 534229997Sken return; 535229997Sken } 536229997Sken 537229997Sken /* 538267537Smav * If this is a write, a flush, a delete or verify, we're all done. 539229997Sken * If this is a read, we can now send the data to the user. 540229997Sken */ 541229997Sken if ((beio->bio_cmd == BIO_WRITE) 542264274Smav || (beio->bio_cmd == BIO_FLUSH) 543267537Smav || (beio->bio_cmd == BIO_DELETE) 544267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 545229997Sken ctl_set_success(&io->scsiio); 546229997Sken ctl_complete_beio(beio); 547229997Sken } else { 548275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 549275058Smav beio->beio_cont == NULL) 550275058Smav ctl_set_success(&io->scsiio); 551229997Sken#ifdef CTL_TIME_IO 552229997Sken getbintime(&io->io_hdr.dma_start_bt); 553229997Sken#endif 554229997Sken ctl_datamove(io); 555229997Sken } 556229997Sken} 557229997Sken 558229997Skenstatic void 559229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 560229997Sken struct ctl_be_block_io *beio) 561229997Sken{ 562267877Smav union ctl_io *io = beio->io; 563229997Sken struct mount *mountpoint; 564241896Skib int error, lock_flags; 565229997Sken 566229997Sken DPRINTF("entered\n"); 567229997Sken 568267877Smav binuptime(&beio->ds_t0); 569267877Smav mtx_lock(&be_lun->io_lock); 570267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 571267877Smav mtx_unlock(&be_lun->io_lock); 572229997Sken 573267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 574229997Sken 575229997Sken if (MNT_SHARED_WRITES(mountpoint) 576229997Sken || ((mountpoint == NULL) 577229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 578229997Sken lock_flags = LK_SHARED; 579229997Sken else 580229997Sken lock_flags = LK_EXCLUSIVE; 581229997Sken 582229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 583229997Sken 584229997Sken error = VOP_FSYNC(be_lun->vn, MNT_WAIT, 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 * 678229997Sken * So, to attempt to provide some barrier semantics in the 679229997Sken * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 680229997Sken */ 681271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 682229997Sken 683229997Sken VOP_UNLOCK(be_lun->vn, 0); 684267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 685229997Sken } else { 686229997Sken struct mount *mountpoint; 687229997Sken int lock_flags; 688229997Sken 689229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 690229997Sken 691229997Sken if (MNT_SHARED_WRITES(mountpoint) 692229997Sken || ((mountpoint == NULL) 693229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 694229997Sken lock_flags = LK_SHARED; 695229997Sken else 696229997Sken lock_flags = LK_EXCLUSIVE; 697229997Sken 698229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 699229997Sken 700229997Sken /* 701229997Sken * UFS pays attention to IO_DIRECT for writes. The write 702229997Sken * is done asynchronously. (Normally the write would just 703229997Sken * get put into cache. 704229997Sken * 705229997Sken * UFS pays attention to IO_SYNC for writes. It will 706229997Sken * attempt to write the buffer out synchronously if that 707229997Sken * flag is set. 708229997Sken * 709229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 710229997Sken * 711229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 712229997Sken * for writes. It will flush the transaction from the 713229997Sken * cache before returning. 714229997Sken * 715229997Sken * So if we've got the BIO_ORDERED flag set, we want 716229997Sken * IO_SYNC in either the UFS or ZFS case. 717229997Sken */ 718271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 719229997Sken VOP_UNLOCK(be_lun->vn, 0); 720229997Sken 721229997Sken vn_finished_write(mountpoint); 722267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 723229997Sken } 724229997Sken 725267877Smav mtx_lock(&be_lun->io_lock); 726267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 727267877Smav beio->ds_tag_type, beio->ds_trans_type, 728267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 729267877Smav mtx_unlock(&be_lun->io_lock); 730267877Smav 731229997Sken /* 732229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 733229997Sken * return the I/O to the user. 734229997Sken */ 735229997Sken if (error != 0) { 736229997Sken char path_str[32]; 737229997Sken 738229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 739229997Sken printf("%s%s command returned errno %d\n", path_str, 740229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 741273809Smav if (error == ENOSPC) { 742273809Smav ctl_set_space_alloc_fail(&io->scsiio); 743273809Smav } else 744273809Smav ctl_set_medium_error(&io->scsiio); 745229997Sken ctl_complete_beio(beio); 746229997Sken return; 747229997Sken } 748229997Sken 749229997Sken /* 750269122Smav * If this is a write or a verify, we're all done. 751229997Sken * If this is a read, we can now send the data to the user. 752229997Sken */ 753269122Smav if ((beio->bio_cmd == BIO_WRITE) || 754269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 755229997Sken ctl_set_success(&io->scsiio); 756229997Sken ctl_complete_beio(beio); 757229997Sken } else { 758275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 759275058Smav beio->beio_cont == NULL) 760275058Smav ctl_set_success(&io->scsiio); 761229997Sken#ifdef CTL_TIME_IO 762229997Sken getbintime(&io->io_hdr.dma_start_bt); 763229997Sken#endif 764229997Sken ctl_datamove(io); 765229997Sken } 766229997Sken} 767229997Sken 768229997Skenstatic void 769275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 770275474Smav struct ctl_be_block_io *beio) 771275474Smav{ 772275474Smav union ctl_io *io = beio->io; 773275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 774275474Smav struct scsi_get_lba_status_data *data; 775275474Smav off_t roff, off; 776275474Smav int error, status; 777275474Smav 778275474Smav DPRINTF("entered\n"); 779275474Smav 780275474Smav off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 781275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 782275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 783275474Smav 0, curthread->td_ucred, curthread); 784275474Smav if (error == 0 && off > roff) 785275474Smav status = 0; /* mapped up to off */ 786275474Smav else { 787275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 788275474Smav 0, curthread->td_ucred, curthread); 789275474Smav if (error == 0 && off > roff) 790275474Smav status = 1; /* deallocated up to off */ 791275474Smav else { 792275474Smav status = 0; /* unknown up to the end */ 793275474Smav off = be_lun->size_bytes; 794275474Smav } 795275474Smav } 796275474Smav VOP_UNLOCK(be_lun->vn, 0); 797275474Smav 798275474Smav off >>= be_lun->blocksize_shift; 799275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 800275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 801275474Smav scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 802275474Smav data->descr[0].length); 803275474Smav data->descr[0].status = status; 804275474Smav 805275474Smav ctl_complete_beio(beio); 806275474Smav} 807275474Smav 808275481Smavstatic uint64_t 809275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 810275481Smav{ 811275481Smav struct vattr vattr; 812275481Smav struct statfs statfs; 813275481Smav int error; 814275481Smav 815275481Smav if (be_lun->vn == NULL) 816275481Smav return (UINT64_MAX); 817275481Smav if (strcmp(attrname, "blocksused") == 0) { 818275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 819275481Smav if (error != 0) 820275481Smav return (UINT64_MAX); 821275481Smav return (vattr.va_bytes >> be_lun->blocksize_shift); 822275481Smav } 823275481Smav if (strcmp(attrname, "blocksavail") == 0) { 824275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 825275481Smav if (error != 0) 826275481Smav return (UINT64_MAX); 827275481Smav return ((statfs.f_bavail * statfs.f_bsize) >> 828275481Smav be_lun->blocksize_shift); 829275481Smav } 830275481Smav return (UINT64_MAX); 831275481Smav} 832275481Smav 833275474Smavstatic void 834269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 835269123Smav struct ctl_be_block_io *beio) 836269123Smav{ 837269123Smav struct ctl_be_block_devdata *dev_data; 838269123Smav union ctl_io *io; 839269123Smav struct uio xuio; 840269123Smav struct iovec *xiovec; 841269123Smav int flags; 842269123Smav int error, i; 843269123Smav 844269123Smav DPRINTF("entered\n"); 845269123Smav 846269123Smav dev_data = &be_lun->backend.dev; 847269123Smav io = beio->io; 848271309Smav flags = 0; 849271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 850271309Smav flags |= IO_DIRECT; 851271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 852271309Smav flags |= IO_SYNC; 853269123Smav 854269123Smav bzero(&xuio, sizeof(xuio)); 855269123Smav if (beio->bio_cmd == BIO_READ) { 856269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 857269123Smav xuio.uio_rw = UIO_READ; 858269123Smav } else { 859269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 860269123Smav xuio.uio_rw = UIO_WRITE; 861269123Smav } 862269123Smav xuio.uio_offset = beio->io_offset; 863269123Smav xuio.uio_resid = beio->io_len; 864269123Smav xuio.uio_segflg = UIO_SYSSPACE; 865269123Smav xuio.uio_iov = beio->xiovecs; 866269123Smav xuio.uio_iovcnt = beio->num_segs; 867269123Smav xuio.uio_td = curthread; 868269123Smav 869269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 870269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 871269123Smav xiovec->iov_len = beio->sg_segs[i].len; 872269123Smav } 873269123Smav 874269123Smav binuptime(&beio->ds_t0); 875269123Smav mtx_lock(&be_lun->io_lock); 876269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 877269123Smav mtx_unlock(&be_lun->io_lock); 878269123Smav 879269123Smav if (beio->bio_cmd == BIO_READ) { 880271309Smav error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 881269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 882269123Smav } else { 883271309Smav error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 884269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 885269123Smav } 886269123Smav 887269123Smav mtx_lock(&be_lun->io_lock); 888269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 889269123Smav beio->ds_tag_type, beio->ds_trans_type, 890269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 891269123Smav mtx_unlock(&be_lun->io_lock); 892269123Smav 893269123Smav /* 894269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 895269123Smav * return the I/O to the user. 896269123Smav */ 897269123Smav if (error != 0) { 898273809Smav if (error == ENOSPC) { 899273809Smav ctl_set_space_alloc_fail(&io->scsiio); 900273809Smav } else 901273809Smav ctl_set_medium_error(&io->scsiio); 902269123Smav ctl_complete_beio(beio); 903269123Smav return; 904269123Smav } 905269123Smav 906269123Smav /* 907269123Smav * If this is a write or a verify, we're all done. 908269123Smav * If this is a read, we can now send the data to the user. 909269123Smav */ 910269123Smav if ((beio->bio_cmd == BIO_WRITE) || 911269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 912269123Smav ctl_set_success(&io->scsiio); 913269123Smav ctl_complete_beio(beio); 914269123Smav } else { 915275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 916275058Smav beio->beio_cont == NULL) 917275058Smav ctl_set_success(&io->scsiio); 918269123Smav#ifdef CTL_TIME_IO 919269123Smav getbintime(&io->io_hdr.dma_start_bt); 920269123Smav#endif 921269123Smav ctl_datamove(io); 922269123Smav } 923269123Smav} 924269123Smav 925269123Smavstatic void 926275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 927275474Smav struct ctl_be_block_io *beio) 928275474Smav{ 929275474Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 930275474Smav union ctl_io *io = beio->io; 931275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 932275474Smav struct scsi_get_lba_status_data *data; 933275474Smav off_t roff, off; 934275474Smav int error, status; 935275474Smav 936275474Smav DPRINTF("entered\n"); 937275474Smav 938275474Smav off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 939275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE, 940275474Smav (caddr_t)&off, FREAD, curthread); 941275474Smav if (error == 0 && off > roff) 942275474Smav status = 0; /* mapped up to off */ 943275474Smav else { 944275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA, 945275474Smav (caddr_t)&off, FREAD, curthread); 946275474Smav if (error == 0 && off > roff) 947275474Smav status = 1; /* deallocated up to off */ 948275474Smav else { 949275474Smav status = 0; /* unknown up to the end */ 950275474Smav off = be_lun->size_bytes; 951275474Smav } 952275474Smav } 953275474Smav 954275474Smav off >>= be_lun->blocksize_shift; 955275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 956275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 957275474Smav scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 958275474Smav data->descr[0].length); 959275474Smav data->descr[0].status = status; 960275474Smav 961275474Smav ctl_complete_beio(beio); 962275474Smav} 963275474Smav 964275474Smavstatic void 965229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 966229997Sken struct ctl_be_block_io *beio) 967229997Sken{ 968229997Sken struct bio *bio; 969229997Sken union ctl_io *io; 970229997Sken struct ctl_be_block_devdata *dev_data; 971229997Sken 972229997Sken dev_data = &be_lun->backend.dev; 973229997Sken io = beio->io; 974229997Sken 975229997Sken DPRINTF("entered\n"); 976229997Sken 977229997Sken /* This can't fail, it's a blocking allocation. */ 978229997Sken bio = g_alloc_bio(); 979229997Sken 980229997Sken bio->bio_cmd = BIO_FLUSH; 981229997Sken bio->bio_flags |= BIO_ORDERED; 982229997Sken bio->bio_dev = dev_data->cdev; 983229997Sken bio->bio_offset = 0; 984229997Sken bio->bio_data = 0; 985229997Sken bio->bio_done = ctl_be_block_biodone; 986229997Sken bio->bio_caller1 = beio; 987229997Sken bio->bio_pblkno = 0; 988229997Sken 989229997Sken /* 990229997Sken * We don't need to acquire the LUN lock here, because we are only 991229997Sken * sending one bio, and so there is no other context to synchronize 992229997Sken * with. 993229997Sken */ 994229997Sken beio->num_bios_sent = 1; 995229997Sken beio->send_complete = 1; 996229997Sken 997229997Sken binuptime(&beio->ds_t0); 998267877Smav mtx_lock(&be_lun->io_lock); 999229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1000267877Smav mtx_unlock(&be_lun->io_lock); 1001229997Sken 1002229997Sken (*dev_data->csw->d_strategy)(bio); 1003229997Sken} 1004229997Sken 1005229997Skenstatic void 1006264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1007264274Smav struct ctl_be_block_io *beio, 1008264274Smav uint64_t off, uint64_t len, int last) 1009264274Smav{ 1010264274Smav struct bio *bio; 1011264274Smav struct ctl_be_block_devdata *dev_data; 1012264296Smav uint64_t maxlen; 1013264274Smav 1014264274Smav dev_data = &be_lun->backend.dev; 1015264296Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 1016264274Smav while (len > 0) { 1017264274Smav bio = g_alloc_bio(); 1018264274Smav bio->bio_cmd = BIO_DELETE; 1019264274Smav bio->bio_dev = dev_data->cdev; 1020264274Smav bio->bio_offset = off; 1021264296Smav bio->bio_length = MIN(len, maxlen); 1022264274Smav bio->bio_data = 0; 1023264274Smav bio->bio_done = ctl_be_block_biodone; 1024264274Smav bio->bio_caller1 = beio; 1025264296Smav bio->bio_pblkno = off / be_lun->blocksize; 1026264274Smav 1027264274Smav off += bio->bio_length; 1028264274Smav len -= bio->bio_length; 1029264274Smav 1030267877Smav mtx_lock(&be_lun->io_lock); 1031264274Smav beio->num_bios_sent++; 1032264274Smav if (last && len == 0) 1033264274Smav beio->send_complete = 1; 1034267877Smav mtx_unlock(&be_lun->io_lock); 1035264274Smav 1036264274Smav (*dev_data->csw->d_strategy)(bio); 1037264274Smav } 1038264274Smav} 1039264274Smav 1040264274Smavstatic void 1041264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1042264274Smav struct ctl_be_block_io *beio) 1043264274Smav{ 1044264274Smav union ctl_io *io; 1045264274Smav struct ctl_be_block_devdata *dev_data; 1046267515Smav struct ctl_ptr_len_flags *ptrlen; 1047264274Smav struct scsi_unmap_desc *buf, *end; 1048264274Smav uint64_t len; 1049264274Smav 1050264274Smav dev_data = &be_lun->backend.dev; 1051264274Smav io = beio->io; 1052264274Smav 1053264274Smav DPRINTF("entered\n"); 1054264274Smav 1055264274Smav binuptime(&beio->ds_t0); 1056267877Smav mtx_lock(&be_lun->io_lock); 1057264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1058267877Smav mtx_unlock(&be_lun->io_lock); 1059264274Smav 1060264274Smav if (beio->io_offset == -1) { 1061264274Smav beio->io_len = 0; 1062267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1063267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1064267515Smav end = buf + ptrlen->len / sizeof(*buf); 1065264274Smav for (; buf < end; buf++) { 1066264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1067264274Smav be_lun->blocksize; 1068264274Smav beio->io_len += len; 1069264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1070264274Smav scsi_8btou64(buf->lba) * be_lun->blocksize, len, 1071264283Smav (end - buf < 2) ? TRUE : FALSE); 1072264274Smav } 1073264274Smav } else 1074264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1075264274Smav beio->io_offset, beio->io_len, TRUE); 1076264274Smav} 1077264274Smav 1078264274Smavstatic void 1079229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1080229997Sken struct ctl_be_block_io *beio) 1081229997Sken{ 1082267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1083229997Sken int i; 1084229997Sken struct bio *bio; 1085229997Sken struct ctl_be_block_devdata *dev_data; 1086229997Sken off_t cur_offset; 1087229997Sken int max_iosize; 1088229997Sken 1089229997Sken DPRINTF("entered\n"); 1090229997Sken 1091229997Sken dev_data = &be_lun->backend.dev; 1092229997Sken 1093229997Sken /* 1094229997Sken * We have to limit our I/O size to the maximum supported by the 1095229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1096229997Sken * set it properly, use DFLTPHYS. 1097229997Sken */ 1098229997Sken max_iosize = dev_data->cdev->si_iosize_max; 1099229997Sken if (max_iosize < PAGE_SIZE) 1100229997Sken max_iosize = DFLTPHYS; 1101229997Sken 1102229997Sken cur_offset = beio->io_offset; 1103229997Sken for (i = 0; i < beio->num_segs; i++) { 1104229997Sken size_t cur_size; 1105229997Sken uint8_t *cur_ptr; 1106229997Sken 1107229997Sken cur_size = beio->sg_segs[i].len; 1108229997Sken cur_ptr = beio->sg_segs[i].addr; 1109229997Sken 1110229997Sken while (cur_size > 0) { 1111229997Sken /* This can't fail, it's a blocking allocation. */ 1112229997Sken bio = g_alloc_bio(); 1113229997Sken 1114229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1115229997Sken 1116229997Sken bio->bio_cmd = beio->bio_cmd; 1117229997Sken bio->bio_dev = dev_data->cdev; 1118229997Sken bio->bio_caller1 = beio; 1119229997Sken bio->bio_length = min(cur_size, max_iosize); 1120229997Sken bio->bio_offset = cur_offset; 1121229997Sken bio->bio_data = cur_ptr; 1122229997Sken bio->bio_done = ctl_be_block_biodone; 1123229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 1124229997Sken 1125229997Sken cur_offset += bio->bio_length; 1126229997Sken cur_ptr += bio->bio_length; 1127229997Sken cur_size -= bio->bio_length; 1128229997Sken 1129267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1130229997Sken beio->num_bios_sent++; 1131229997Sken } 1132229997Sken } 1133267877Smav binuptime(&beio->ds_t0); 1134267877Smav mtx_lock(&be_lun->io_lock); 1135267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1136267877Smav beio->send_complete = 1; 1137267877Smav mtx_unlock(&be_lun->io_lock); 1138267877Smav 1139267877Smav /* 1140267877Smav * Fire off all allocated requests! 1141267877Smav */ 1142267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1143267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1144267877Smav (*dev_data->csw->d_strategy)(bio); 1145267877Smav } 1146229997Sken} 1147229997Sken 1148274154Smavstatic uint64_t 1149274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1150274154Smav{ 1151274154Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1152274154Smav struct diocgattr_arg arg; 1153274154Smav int error; 1154274154Smav 1155274154Smav if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1156274154Smav return (UINT64_MAX); 1157274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1158274154Smav arg.len = sizeof(arg.value.off); 1159274154Smav error = dev_data->csw->d_ioctl(dev_data->cdev, 1160274154Smav DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1161274154Smav if (error != 0) 1162274154Smav return (UINT64_MAX); 1163274154Smav return (arg.value.off); 1164274154Smav} 1165274154Smav 1166229997Skenstatic void 1167264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1168264274Smav{ 1169264274Smav union ctl_io *io; 1170264274Smav 1171264274Smav io = beio->io; 1172264274Smav ctl_free_beio(beio); 1173267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1174267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1175267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1176264274Smav ctl_config_write_done(io); 1177264274Smav return; 1178264274Smav } 1179264274Smav 1180264274Smav ctl_be_block_config_write(io); 1181264274Smav} 1182264274Smav 1183264274Smavstatic void 1184264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1185264274Smav union ctl_io *io) 1186264274Smav{ 1187264274Smav struct ctl_be_block_io *beio; 1188264274Smav struct ctl_be_block_softc *softc; 1189267515Smav struct ctl_lba_len_flags *lbalen; 1190278625Smav uint64_t len_left, lba; 1191278625Smav uint32_t pb, pbo, adj; 1192264274Smav int i, seglen; 1193264274Smav uint8_t *buf, *end; 1194264274Smav 1195264274Smav DPRINTF("entered\n"); 1196264274Smav 1197267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1198264274Smav softc = be_lun->softc; 1199267537Smav lbalen = ARGS(beio->io); 1200264274Smav 1201271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1202269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1203264274Smav ctl_free_beio(beio); 1204264274Smav ctl_set_invalid_field(&io->scsiio, 1205264274Smav /*sks_valid*/ 1, 1206264274Smav /*command*/ 1, 1207264274Smav /*field*/ 1, 1208264274Smav /*bit_valid*/ 0, 1209264274Smav /*bit*/ 0); 1210264274Smav ctl_config_write_done(io); 1211264274Smav return; 1212264274Smav } 1213264274Smav 1214264274Smav switch (io->scsiio.tag_type) { 1215264274Smav case CTL_TAG_ORDERED: 1216264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1217264274Smav break; 1218264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1219264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1220264274Smav break; 1221264274Smav case CTL_TAG_UNTAGGED: 1222264274Smav case CTL_TAG_SIMPLE: 1223264274Smav case CTL_TAG_ACA: 1224264274Smav default: 1225264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1226264274Smav break; 1227264274Smav } 1228264274Smav 1229269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1230267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1231267515Smav beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1232264274Smav beio->bio_cmd = BIO_DELETE; 1233264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1234264274Smav 1235264274Smav be_lun->unmap(be_lun, beio); 1236264274Smav return; 1237264274Smav } 1238264274Smav 1239264274Smav beio->bio_cmd = BIO_WRITE; 1240264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1241264274Smav 1242264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1243267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1244264274Smav 1245278625Smav pb = be_lun->blocksize << be_lun->pblockexp; 1246278625Smav if (be_lun->pblockoff > 0) 1247278625Smav pbo = pb - be_lun->blocksize * be_lun->pblockoff; 1248278625Smav else 1249278625Smav pbo = 0; 1250267515Smav len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1251264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1252264274Smav 1253264274Smav /* 1254264274Smav * Setup the S/G entry for this chunk. 1255264274Smav */ 1256264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1257278619Smav if (pb > be_lun->blocksize) { 1258278619Smav adj = ((lbalen->lba + lba) * be_lun->blocksize + 1259278619Smav seglen - pbo) % pb; 1260278619Smav if (seglen > adj) 1261278619Smav seglen -= adj; 1262278619Smav else 1263278619Smav seglen -= seglen % be_lun->blocksize; 1264278619Smav } else 1265278619Smav seglen -= seglen % be_lun->blocksize; 1266264274Smav beio->sg_segs[i].len = seglen; 1267264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1268264274Smav 1269264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1270264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1271264274Smav 1272264274Smav beio->num_segs++; 1273264274Smav len_left -= seglen; 1274264274Smav 1275264274Smav buf = beio->sg_segs[i].addr; 1276264274Smav end = buf + seglen; 1277264274Smav for (; buf < end; buf += be_lun->blocksize) { 1278264274Smav memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1279267515Smav if (lbalen->flags & SWS_LBDATA) 1280267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1281264274Smav lba++; 1282264274Smav } 1283264274Smav } 1284264274Smav 1285267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1286264274Smav beio->io_len = lba * be_lun->blocksize; 1287264274Smav 1288264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1289264274Smav if (len_left > 0) { 1290267515Smav lbalen->lba += lba; 1291267515Smav lbalen->len -= lba; 1292264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1293264274Smav } 1294264274Smav 1295264274Smav be_lun->dispatch(be_lun, beio); 1296264274Smav} 1297264274Smav 1298264274Smavstatic void 1299264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1300264274Smav union ctl_io *io) 1301264274Smav{ 1302264274Smav struct ctl_be_block_io *beio; 1303264274Smav struct ctl_be_block_softc *softc; 1304267515Smav struct ctl_ptr_len_flags *ptrlen; 1305264274Smav 1306264274Smav DPRINTF("entered\n"); 1307264274Smav 1308267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1309264274Smav softc = be_lun->softc; 1310267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1311264274Smav 1312269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1313264274Smav ctl_free_beio(beio); 1314264274Smav ctl_set_invalid_field(&io->scsiio, 1315264274Smav /*sks_valid*/ 0, 1316264274Smav /*command*/ 1, 1317264274Smav /*field*/ 0, 1318264274Smav /*bit_valid*/ 0, 1319264274Smav /*bit*/ 0); 1320264274Smav ctl_config_write_done(io); 1321264274Smav return; 1322264274Smav } 1323264274Smav 1324264274Smav switch (io->scsiio.tag_type) { 1325264274Smav case CTL_TAG_ORDERED: 1326264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1327264274Smav break; 1328264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1329264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1330264274Smav break; 1331264274Smav case CTL_TAG_UNTAGGED: 1332264274Smav case CTL_TAG_SIMPLE: 1333264274Smav case CTL_TAG_ACA: 1334264274Smav default: 1335264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1336264274Smav break; 1337264274Smav } 1338264274Smav 1339264274Smav beio->io_len = 0; 1340264274Smav beio->io_offset = -1; 1341264274Smav 1342264274Smav beio->bio_cmd = BIO_DELETE; 1343264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1344264274Smav 1345267515Smav DPRINTF("UNMAP\n"); 1346264274Smav 1347264274Smav be_lun->unmap(be_lun, beio); 1348264274Smav} 1349264274Smav 1350264274Smavstatic void 1351275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1352275474Smav{ 1353275474Smav union ctl_io *io; 1354275474Smav 1355275474Smav io = beio->io; 1356275474Smav ctl_free_beio(beio); 1357275474Smav ctl_config_read_done(io); 1358275474Smav} 1359275474Smav 1360275474Smavstatic void 1361275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1362275474Smav union ctl_io *io) 1363275474Smav{ 1364275474Smav struct ctl_be_block_io *beio; 1365275474Smav struct ctl_be_block_softc *softc; 1366275474Smav 1367275474Smav DPRINTF("entered\n"); 1368275474Smav 1369275474Smav softc = be_lun->softc; 1370275474Smav beio = ctl_alloc_beio(softc); 1371275474Smav beio->io = io; 1372275474Smav beio->lun = be_lun; 1373275474Smav beio->beio_cont = ctl_be_block_cr_done; 1374275474Smav PRIV(io)->ptr = (void *)beio; 1375275474Smav 1376275474Smav switch (io->scsiio.cdb[0]) { 1377275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1378275474Smav beio->bio_cmd = -1; 1379275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1380275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1381275474Smav beio->io_len = 0; 1382275474Smav if (be_lun->get_lba_status) 1383275474Smav be_lun->get_lba_status(be_lun, beio); 1384275474Smav else 1385275474Smav ctl_be_block_cr_done(beio); 1386275474Smav break; 1387275474Smav default: 1388275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1389275474Smav break; 1390275474Smav } 1391275474Smav} 1392275474Smav 1393275474Smavstatic void 1394264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1395264274Smav{ 1396264274Smav union ctl_io *io; 1397264274Smav 1398264274Smav io = beio->io; 1399264274Smav ctl_free_beio(beio); 1400264274Smav ctl_config_write_done(io); 1401264274Smav} 1402264274Smav 1403264274Smavstatic void 1404229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1405229997Sken union ctl_io *io) 1406229997Sken{ 1407229997Sken struct ctl_be_block_io *beio; 1408229997Sken struct ctl_be_block_softc *softc; 1409229997Sken 1410229997Sken DPRINTF("entered\n"); 1411229997Sken 1412229997Sken softc = be_lun->softc; 1413229997Sken beio = ctl_alloc_beio(softc); 1414229997Sken beio->io = io; 1415229997Sken beio->lun = be_lun; 1416264274Smav beio->beio_cont = ctl_be_block_cw_done; 1417267519Smav PRIV(io)->ptr = (void *)beio; 1418229997Sken 1419229997Sken switch (io->scsiio.cdb[0]) { 1420229997Sken case SYNCHRONIZE_CACHE: 1421229997Sken case SYNCHRONIZE_CACHE_16: 1422249194Strasz beio->bio_cmd = BIO_FLUSH; 1423229997Sken beio->ds_trans_type = DEVSTAT_NO_DATA; 1424229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1425229997Sken beio->io_len = 0; 1426229997Sken be_lun->lun_flush(be_lun, beio); 1427229997Sken break; 1428264274Smav case WRITE_SAME_10: 1429264274Smav case WRITE_SAME_16: 1430264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1431264274Smav break; 1432264274Smav case UNMAP: 1433264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1434264274Smav break; 1435229997Sken default: 1436229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1437229997Sken break; 1438229997Sken } 1439229997Sken} 1440229997Sken 1441258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1442258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1443258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1444258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1445229997Sken 1446229997Skenstatic void 1447264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1448264886Smav{ 1449264886Smav struct ctl_be_block_lun *be_lun; 1450264886Smav union ctl_io *io; 1451264886Smav 1452264886Smav io = beio->io; 1453264886Smav be_lun = beio->lun; 1454264886Smav ctl_free_beio(beio); 1455267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1456267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1457267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1458267537Smav ctl_data_submit_done(io); 1459264886Smav return; 1460264886Smav } 1461264886Smav 1462264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1463264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1464264886Smav 1465267877Smav mtx_lock(&be_lun->queue_lock); 1466264886Smav /* 1467264886Smav * XXX KDM make sure that links is okay to use at this point. 1468264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1469264886Smav * or deal with resource allocation here. 1470264886Smav */ 1471264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1472267877Smav mtx_unlock(&be_lun->queue_lock); 1473264886Smav 1474264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1475264886Smav} 1476264886Smav 1477264886Smavstatic void 1478229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1479229997Sken union ctl_io *io) 1480229997Sken{ 1481229997Sken struct ctl_be_block_io *beio; 1482229997Sken struct ctl_be_block_softc *softc; 1483267537Smav struct ctl_lba_len_flags *lbalen; 1484267519Smav struct ctl_ptr_len_flags *bptrlen; 1485267519Smav uint64_t len_left, lbas; 1486229997Sken int i; 1487229997Sken 1488229997Sken softc = be_lun->softc; 1489229997Sken 1490229997Sken DPRINTF("entered\n"); 1491229997Sken 1492267537Smav lbalen = ARGS(io); 1493267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1494267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1495267537Smav } else { 1496229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1497229997Sken } 1498229997Sken 1499229997Sken beio = ctl_alloc_beio(softc); 1500229997Sken beio->io = io; 1501229997Sken beio->lun = be_lun; 1502267519Smav bptrlen = PRIV(io); 1503267519Smav bptrlen->ptr = (void *)beio; 1504229997Sken 1505229997Sken switch (io->scsiio.tag_type) { 1506229997Sken case CTL_TAG_ORDERED: 1507229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1508229997Sken break; 1509229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1510229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1511229997Sken break; 1512229997Sken case CTL_TAG_UNTAGGED: 1513229997Sken case CTL_TAG_SIMPLE: 1514229997Sken case CTL_TAG_ACA: 1515229997Sken default: 1516229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1517229997Sken break; 1518229997Sken } 1519229997Sken 1520267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1521267537Smav beio->bio_cmd = BIO_WRITE; 1522267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1523267537Smav } else { 1524229997Sken beio->bio_cmd = BIO_READ; 1525229997Sken beio->ds_trans_type = DEVSTAT_READ; 1526229997Sken } 1527229997Sken 1528264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1529229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1530267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1531267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1532267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1533267537Smav else 1534267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1535267537Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1536267519Smav beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1537267519Smav beio->io_len = lbas * be_lun->blocksize; 1538267519Smav bptrlen->len += lbas; 1539229997Sken 1540264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1541264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1542264886Smav i, CTLBLK_MAX_SEGS)); 1543229997Sken 1544229997Sken /* 1545229997Sken * Setup the S/G entry for this chunk. 1546229997Sken */ 1547264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1548229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1549229997Sken 1550229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1551229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1552229997Sken 1553267537Smav /* Set up second segment for compare operation. */ 1554267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1555267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1556267537Smav beio->sg_segs[i].len; 1557267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1558267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1559267537Smav } 1560267537Smav 1561229997Sken beio->num_segs++; 1562229997Sken len_left -= beio->sg_segs[i].len; 1563229997Sken } 1564267519Smav if (bptrlen->len < lbalen->len) 1565264886Smav beio->beio_cont = ctl_be_block_next; 1566264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1567267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1568267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1569267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1570267537Smav else 1571267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1572264886Smav io->scsiio.kern_data_len = beio->io_len; 1573264886Smav io->scsiio.kern_data_resid = 0; 1574264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1575264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1576229997Sken 1577229997Sken /* 1578229997Sken * For the read case, we need to read the data into our buffers and 1579229997Sken * then we can send it back to the user. For the write case, we 1580229997Sken * need to get the data from the user first. 1581229997Sken */ 1582229997Sken if (beio->bio_cmd == BIO_READ) { 1583229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1584229997Sken be_lun->dispatch(be_lun, beio); 1585229997Sken } else { 1586229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1587229997Sken#ifdef CTL_TIME_IO 1588229997Sken getbintime(&io->io_hdr.dma_start_bt); 1589229997Sken#endif 1590229997Sken ctl_datamove(io); 1591229997Sken } 1592229997Sken} 1593229997Sken 1594229997Skenstatic void 1595229997Skenctl_be_block_worker(void *context, int pending) 1596229997Sken{ 1597229997Sken struct ctl_be_block_lun *be_lun; 1598229997Sken struct ctl_be_block_softc *softc; 1599229997Sken union ctl_io *io; 1600229997Sken 1601229997Sken be_lun = (struct ctl_be_block_lun *)context; 1602229997Sken softc = be_lun->softc; 1603229997Sken 1604229997Sken DPRINTF("entered\n"); 1605229997Sken 1606267877Smav mtx_lock(&be_lun->queue_lock); 1607229997Sken for (;;) { 1608229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1609229997Sken if (io != NULL) { 1610229997Sken struct ctl_be_block_io *beio; 1611229997Sken 1612229997Sken DPRINTF("datamove queue\n"); 1613229997Sken 1614229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1615229997Sken ctl_io_hdr, links); 1616229997Sken 1617267877Smav mtx_unlock(&be_lun->queue_lock); 1618229997Sken 1619267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1620229997Sken 1621229997Sken be_lun->dispatch(be_lun, beio); 1622229997Sken 1623267877Smav mtx_lock(&be_lun->queue_lock); 1624229997Sken continue; 1625229997Sken } 1626229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1627229997Sken if (io != NULL) { 1628229997Sken DPRINTF("config write queue\n"); 1629229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1630229997Sken ctl_io_hdr, links); 1631267877Smav mtx_unlock(&be_lun->queue_lock); 1632229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1633267877Smav mtx_lock(&be_lun->queue_lock); 1634229997Sken continue; 1635229997Sken } 1636275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1637275474Smav if (io != NULL) { 1638275474Smav DPRINTF("config read queue\n"); 1639275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1640275474Smav ctl_io_hdr, links); 1641275474Smav mtx_unlock(&be_lun->queue_lock); 1642275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1643275474Smav mtx_lock(&be_lun->queue_lock); 1644275474Smav continue; 1645275474Smav } 1646229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1647229997Sken if (io != NULL) { 1648229997Sken DPRINTF("input queue\n"); 1649229997Sken 1650229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1651229997Sken ctl_io_hdr, links); 1652267877Smav mtx_unlock(&be_lun->queue_lock); 1653229997Sken 1654229997Sken /* 1655229997Sken * We must drop the lock, since this routine and 1656229997Sken * its children may sleep. 1657229997Sken */ 1658229997Sken ctl_be_block_dispatch(be_lun, io); 1659229997Sken 1660267877Smav mtx_lock(&be_lun->queue_lock); 1661229997Sken continue; 1662229997Sken } 1663229997Sken 1664229997Sken /* 1665229997Sken * If we get here, there is no work left in the queues, so 1666229997Sken * just break out and let the task queue go to sleep. 1667229997Sken */ 1668229997Sken break; 1669229997Sken } 1670267877Smav mtx_unlock(&be_lun->queue_lock); 1671229997Sken} 1672229997Sken 1673229997Sken/* 1674229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1675229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1676229997Sken * thread. 1677229997Sken */ 1678229997Skenstatic int 1679229997Skenctl_be_block_submit(union ctl_io *io) 1680229997Sken{ 1681229997Sken struct ctl_be_block_lun *be_lun; 1682229997Sken struct ctl_be_lun *ctl_be_lun; 1683229997Sken 1684229997Sken DPRINTF("entered\n"); 1685229997Sken 1686229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1687229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1688229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1689229997Sken 1690229997Sken /* 1691229997Sken * Make sure we only get SCSI I/O. 1692229997Sken */ 1693229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1694229997Sken "%#x) encountered", io->io_hdr.io_type)); 1695229997Sken 1696267519Smav PRIV(io)->len = 0; 1697267519Smav 1698267877Smav mtx_lock(&be_lun->queue_lock); 1699229997Sken /* 1700229997Sken * XXX KDM make sure that links is okay to use at this point. 1701229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1702229997Sken * or deal with resource allocation here. 1703229997Sken */ 1704229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1705267877Smav mtx_unlock(&be_lun->queue_lock); 1706229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1707229997Sken 1708267514Smav return (CTL_RETVAL_COMPLETE); 1709229997Sken} 1710229997Sken 1711229997Skenstatic int 1712229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1713229997Sken int flag, struct thread *td) 1714229997Sken{ 1715229997Sken struct ctl_be_block_softc *softc; 1716229997Sken int error; 1717229997Sken 1718229997Sken softc = &backend_block_softc; 1719229997Sken 1720229997Sken error = 0; 1721229997Sken 1722229997Sken switch (cmd) { 1723229997Sken case CTL_LUN_REQ: { 1724229997Sken struct ctl_lun_req *lun_req; 1725229997Sken 1726229997Sken lun_req = (struct ctl_lun_req *)addr; 1727229997Sken 1728229997Sken switch (lun_req->reqtype) { 1729229997Sken case CTL_LUNREQ_CREATE: 1730229997Sken error = ctl_be_block_create(softc, lun_req); 1731229997Sken break; 1732229997Sken case CTL_LUNREQ_RM: 1733229997Sken error = ctl_be_block_rm(softc, lun_req); 1734229997Sken break; 1735232604Strasz case CTL_LUNREQ_MODIFY: 1736232604Strasz error = ctl_be_block_modify(softc, lun_req); 1737232604Strasz break; 1738229997Sken default: 1739229997Sken lun_req->status = CTL_LUN_ERROR; 1740229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1741272911Smav "invalid LUN request type %d", 1742229997Sken lun_req->reqtype); 1743229997Sken break; 1744229997Sken } 1745229997Sken break; 1746229997Sken } 1747229997Sken default: 1748229997Sken error = ENOTTY; 1749229997Sken break; 1750229997Sken } 1751229997Sken 1752229997Sken return (error); 1753229997Sken} 1754229997Sken 1755229997Skenstatic int 1756229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1757229997Sken{ 1758229997Sken struct ctl_be_block_filedata *file_data; 1759229997Sken struct ctl_lun_create_params *params; 1760275865Smav char *value; 1761229997Sken struct vattr vattr; 1762275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1763229997Sken int error; 1764229997Sken 1765229997Sken error = 0; 1766229997Sken file_data = &be_lun->backend.file; 1767272911Smav params = &be_lun->params; 1768229997Sken 1769229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1770229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1771229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1772275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1773275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1774229997Sken 1775229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1776229997Sken if (error != 0) { 1777229997Sken snprintf(req->error_str, sizeof(req->error_str), 1778229997Sken "error calling VOP_GETATTR() for file %s", 1779229997Sken be_lun->dev_path); 1780229997Sken return (error); 1781229997Sken } 1782229997Sken 1783229997Sken /* 1784229997Sken * Verify that we have the ability to upgrade to exclusive 1785229997Sken * access on this file so we can trap errors at open instead 1786229997Sken * of reporting them during first access. 1787229997Sken */ 1788229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1789229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1790229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1791229997Sken error = EBADF; 1792229997Sken snprintf(req->error_str, sizeof(req->error_str), 1793229997Sken "error locking file %s", be_lun->dev_path); 1794229997Sken return (error); 1795229997Sken } 1796229997Sken } 1797229997Sken 1798229997Sken 1799229997Sken file_data->cred = crhold(curthread->td_ucred); 1800232604Strasz if (params->lun_size_bytes != 0) 1801232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1802232604Strasz else 1803232604Strasz be_lun->size_bytes = vattr.va_size; 1804229997Sken /* 1805229997Sken * We set the multi thread flag for file operations because all 1806229997Sken * filesystems (in theory) are capable of allowing multiple readers 1807229997Sken * of a file at once. So we want to get the maximum possible 1808229997Sken * concurrency. 1809229997Sken */ 1810229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1811229997Sken 1812229997Sken /* 1813273029Smav * For files we can use any logical block size. Prefer 512 bytes 1814273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1815273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1816273029Smav * logical block size -- report it as physical block size. 1817229997Sken */ 1818229997Sken if (params->blocksize_bytes != 0) 1819229997Sken be_lun->blocksize = params->blocksize_bytes; 1820229997Sken else 1821229997Sken be_lun->blocksize = 512; 1822275865Smav 1823275865Smav us = ps = vattr.va_blocksize; 1824275865Smav uo = po = 0; 1825275865Smav 1826275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize"); 1827275865Smav if (value != NULL) 1828275865Smav ctl_expand_number(value, &ps); 1829275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset"); 1830275865Smav if (value != NULL) 1831275865Smav ctl_expand_number(value, &po); 1832275865Smav pss = ps / be_lun->blocksize; 1833275865Smav pos = po / be_lun->blocksize; 1834275865Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1835275865Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1836273029Smav be_lun->pblockexp = fls(pss) - 1; 1837275865Smav be_lun->pblockoff = (pss - pos) % pss; 1838273029Smav } 1839229997Sken 1840275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize"); 1841275865Smav if (value != NULL) 1842275865Smav ctl_expand_number(value, &us); 1843275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset"); 1844275865Smav if (value != NULL) 1845275865Smav ctl_expand_number(value, &uo); 1846275865Smav uss = us / be_lun->blocksize; 1847275865Smav uos = uo / be_lun->blocksize; 1848275865Smav if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) && 1849275865Smav ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) { 1850275865Smav be_lun->ublockexp = fls(uss) - 1; 1851275865Smav be_lun->ublockoff = (uss - uos) % uss; 1852275865Smav } 1853275865Smav 1854229997Sken /* 1855229997Sken * Sanity check. The media size has to be at least one 1856229997Sken * sector long. 1857229997Sken */ 1858229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1859229997Sken error = EINVAL; 1860229997Sken snprintf(req->error_str, sizeof(req->error_str), 1861229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1862229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1863229997Sken } 1864275920Smav 1865275920Smav be_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / be_lun->blocksize; 1866229997Sken return (error); 1867229997Sken} 1868229997Sken 1869229997Skenstatic int 1870229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1871229997Sken{ 1872229997Sken struct ctl_lun_create_params *params; 1873229997Sken struct vattr vattr; 1874229997Sken struct cdev *dev; 1875229997Sken struct cdevsw *devsw; 1876275865Smav char *value; 1877275920Smav int error, atomic, maxio; 1878275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1879229997Sken 1880272911Smav params = &be_lun->params; 1881229997Sken 1882229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1883229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1884229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1885229997Sken &be_lun->backend.dev.dev_ref); 1886229997Sken if (be_lun->backend.dev.csw == NULL) 1887229997Sken panic("Unable to retrieve device switch"); 1888275474Smav if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) { 1889269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1890275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1891275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1892275920Smav } else { 1893269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1894275920Smav atomic = 0; 1895275920Smav maxio = be_lun->backend.dev.cdev->si_iosize_max; 1896275920Smav if (maxio <= 0) 1897275920Smav maxio = DFLTPHYS; 1898275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1899275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1900275920Smav } 1901269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1902269123Smav be_lun->unmap = ctl_be_block_unmap_dev; 1903274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1904229997Sken 1905229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1906229997Sken if (error) { 1907229997Sken snprintf(req->error_str, sizeof(req->error_str), 1908272911Smav "error getting vnode attributes for device %s", 1909272911Smav be_lun->dev_path); 1910229997Sken return (error); 1911229997Sken } 1912229997Sken 1913229997Sken dev = be_lun->vn->v_rdev; 1914229997Sken devsw = dev->si_devsw; 1915229997Sken if (!devsw->d_ioctl) { 1916229997Sken snprintf(req->error_str, sizeof(req->error_str), 1917272911Smav "no d_ioctl for device %s!", 1918229997Sken be_lun->dev_path); 1919229997Sken return (ENODEV); 1920229997Sken } 1921229997Sken 1922229997Sken error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1923229997Sken (caddr_t)&be_lun->blocksize, FREAD, 1924229997Sken curthread); 1925229997Sken if (error) { 1926229997Sken snprintf(req->error_str, sizeof(req->error_str), 1927272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1928272911Smav "on %s!", error, be_lun->dev_path); 1929229997Sken return (error); 1930229997Sken } 1931229997Sken 1932229997Sken /* 1933229997Sken * If the user has asked for a blocksize that is greater than the 1934229997Sken * backing device's blocksize, we can do it only if the blocksize 1935229997Sken * the user is asking for is an even multiple of the underlying 1936229997Sken * device's blocksize. 1937229997Sken */ 1938229997Sken if ((params->blocksize_bytes != 0) 1939229997Sken && (params->blocksize_bytes > be_lun->blocksize)) { 1940229997Sken uint32_t bs_multiple, tmp_blocksize; 1941229997Sken 1942229997Sken bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1943229997Sken 1944229997Sken tmp_blocksize = bs_multiple * be_lun->blocksize; 1945229997Sken 1946229997Sken if (tmp_blocksize == params->blocksize_bytes) { 1947229997Sken be_lun->blocksize = params->blocksize_bytes; 1948229997Sken } else { 1949229997Sken snprintf(req->error_str, sizeof(req->error_str), 1950272911Smav "requested blocksize %u is not an even " 1951229997Sken "multiple of backing device blocksize %u", 1952272911Smav params->blocksize_bytes, 1953229997Sken be_lun->blocksize); 1954229997Sken return (EINVAL); 1955229997Sken 1956229997Sken } 1957229997Sken } else if ((params->blocksize_bytes != 0) 1958229997Sken && (params->blocksize_bytes != be_lun->blocksize)) { 1959229997Sken snprintf(req->error_str, sizeof(req->error_str), 1960272911Smav "requested blocksize %u < backing device " 1961272911Smav "blocksize %u", params->blocksize_bytes, 1962229997Sken be_lun->blocksize); 1963229997Sken return (EINVAL); 1964229997Sken } 1965229997Sken 1966229997Sken error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1967229997Sken (caddr_t)&be_lun->size_bytes, FREAD, 1968229997Sken curthread); 1969229997Sken if (error) { 1970229997Sken snprintf(req->error_str, sizeof(req->error_str), 1971272911Smav "error %d returned for DIOCGMEDIASIZE " 1972272911Smav " ioctl on %s!", error, 1973232604Strasz be_lun->dev_path); 1974229997Sken return (error); 1975229997Sken } 1976229997Sken 1977232604Strasz if (params->lun_size_bytes != 0) { 1978232604Strasz if (params->lun_size_bytes > be_lun->size_bytes) { 1979232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1980272911Smav "requested LUN size %ju > backing device " 1981272911Smav "size %ju", 1982232604Strasz (uintmax_t)params->lun_size_bytes, 1983232604Strasz (uintmax_t)be_lun->size_bytes); 1984232604Strasz return (EINVAL); 1985232604Strasz } 1986232604Strasz 1987232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1988232604Strasz } 1989232604Strasz 1990264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1991264191Smav (caddr_t)&ps, FREAD, curthread); 1992264191Smav if (error) 1993264191Smav ps = po = 0; 1994264191Smav else { 1995264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1996264191Smav (caddr_t)&po, FREAD, curthread); 1997264191Smav if (error) 1998264191Smav po = 0; 1999264191Smav } 2000275865Smav us = ps; 2001275865Smav uo = po; 2002275865Smav 2003275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize"); 2004275865Smav if (value != NULL) 2005275865Smav ctl_expand_number(value, &ps); 2006275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset"); 2007275865Smav if (value != NULL) 2008275865Smav ctl_expand_number(value, &po); 2009264191Smav pss = ps / be_lun->blocksize; 2010264191Smav pos = po / be_lun->blocksize; 2011264191Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 2012264191Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 2013264191Smav be_lun->pblockexp = fls(pss) - 1; 2014264191Smav be_lun->pblockoff = (pss - pos) % pss; 2015264191Smav } 2016264191Smav 2017275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize"); 2018275865Smav if (value != NULL) 2019275865Smav ctl_expand_number(value, &us); 2020275865Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset"); 2021275865Smav if (value != NULL) 2022275865Smav ctl_expand_number(value, &uo); 2023275865Smav uss = us / be_lun->blocksize; 2024275865Smav uos = uo / be_lun->blocksize; 2025275865Smav if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) && 2026275865Smav ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) { 2027275865Smav be_lun->ublockexp = fls(uss) - 1; 2028275865Smav be_lun->ublockoff = (uss - uos) % uss; 2029275865Smav } 2030275865Smav 2031275920Smav be_lun->atomicblock = atomic / be_lun->blocksize; 2032275920Smav be_lun->opttxferlen = maxio / be_lun->blocksize; 2033229997Sken return (0); 2034229997Sken} 2035229997Sken 2036229997Skenstatic int 2037229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2038229997Sken{ 2039229997Sken DROP_GIANT(); 2040229997Sken if (be_lun->vn) { 2041229997Sken int flags = FREAD | FWRITE; 2042229997Sken 2043229997Sken switch (be_lun->dev_type) { 2044229997Sken case CTL_BE_BLOCK_DEV: 2045229997Sken if (be_lun->backend.dev.csw) { 2046229997Sken dev_relthread(be_lun->backend.dev.cdev, 2047229997Sken be_lun->backend.dev.dev_ref); 2048229997Sken be_lun->backend.dev.csw = NULL; 2049229997Sken be_lun->backend.dev.cdev = NULL; 2050229997Sken } 2051229997Sken break; 2052229997Sken case CTL_BE_BLOCK_FILE: 2053229997Sken break; 2054229997Sken case CTL_BE_BLOCK_NONE: 2055258871Strasz break; 2056229997Sken default: 2057229997Sken panic("Unexpected backend type."); 2058229997Sken break; 2059229997Sken } 2060229997Sken 2061229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2062229997Sken be_lun->vn = NULL; 2063229997Sken 2064229997Sken switch (be_lun->dev_type) { 2065229997Sken case CTL_BE_BLOCK_DEV: 2066229997Sken break; 2067229997Sken case CTL_BE_BLOCK_FILE: 2068229997Sken if (be_lun->backend.file.cred != NULL) { 2069229997Sken crfree(be_lun->backend.file.cred); 2070229997Sken be_lun->backend.file.cred = NULL; 2071229997Sken } 2072229997Sken break; 2073229997Sken case CTL_BE_BLOCK_NONE: 2074258871Strasz break; 2075229997Sken default: 2076229997Sken panic("Unexpected backend type."); 2077229997Sken break; 2078229997Sken } 2079272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2080229997Sken } 2081229997Sken PICKUP_GIANT(); 2082229997Sken 2083229997Sken return (0); 2084229997Sken} 2085229997Sken 2086229997Skenstatic int 2087229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 2088229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2089229997Sken{ 2090229997Sken struct nameidata nd; 2091229997Sken int flags; 2092229997Sken int error; 2093229997Sken 2094229997Sken /* 2095229997Sken * XXX KDM allow a read-only option? 2096229997Sken */ 2097229997Sken flags = FREAD | FWRITE; 2098229997Sken error = 0; 2099229997Sken 2100229997Sken if (rootvnode == NULL) { 2101229997Sken snprintf(req->error_str, sizeof(req->error_str), 2102272911Smav "Root filesystem is not mounted"); 2103229997Sken return (1); 2104229997Sken } 2105229997Sken 2106229997Sken if (!curthread->td_proc->p_fd->fd_cdir) { 2107229997Sken curthread->td_proc->p_fd->fd_cdir = rootvnode; 2108229997Sken VREF(rootvnode); 2109229997Sken } 2110229997Sken if (!curthread->td_proc->p_fd->fd_rdir) { 2111229997Sken curthread->td_proc->p_fd->fd_rdir = rootvnode; 2112229997Sken VREF(rootvnode); 2113229997Sken } 2114229997Sken if (!curthread->td_proc->p_fd->fd_jdir) { 2115229997Sken curthread->td_proc->p_fd->fd_jdir = rootvnode; 2116229997Sken VREF(rootvnode); 2117229997Sken } 2118229997Sken 2119229997Sken again: 2120229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2121229997Sken error = vn_open(&nd, &flags, 0, NULL); 2122229997Sken if (error) { 2123229997Sken /* 2124229997Sken * This is the only reasonable guess we can make as far as 2125229997Sken * path if the user doesn't give us a fully qualified path. 2126229997Sken * If they want to specify a file, they need to specify the 2127229997Sken * full path. 2128229997Sken */ 2129229997Sken if (be_lun->dev_path[0] != '/') { 2130229997Sken char *dev_path = "/dev/"; 2131229997Sken char *dev_name; 2132229997Sken 2133229997Sken /* Try adding device path at beginning of name */ 2134229997Sken dev_name = malloc(strlen(be_lun->dev_path) 2135229997Sken + strlen(dev_path) + 1, 2136229997Sken M_CTLBLK, M_WAITOK); 2137229997Sken if (dev_name) { 2138229997Sken sprintf(dev_name, "%s%s", dev_path, 2139229997Sken be_lun->dev_path); 2140229997Sken free(be_lun->dev_path, M_CTLBLK); 2141229997Sken be_lun->dev_path = dev_name; 2142229997Sken goto again; 2143229997Sken } 2144229997Sken } 2145229997Sken snprintf(req->error_str, sizeof(req->error_str), 2146272911Smav "error opening %s: %d", be_lun->dev_path, error); 2147229997Sken return (error); 2148229997Sken } 2149229997Sken 2150229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2151229997Sken 2152229997Sken be_lun->vn = nd.ni_vp; 2153229997Sken 2154229997Sken /* We only support disks and files. */ 2155229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2156229997Sken error = ctl_be_block_open_dev(be_lun, req); 2157229997Sken } else if (be_lun->vn->v_type == VREG) { 2158229997Sken error = ctl_be_block_open_file(be_lun, req); 2159229997Sken } else { 2160229997Sken error = EINVAL; 2161229997Sken snprintf(req->error_str, sizeof(req->error_str), 2162258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2163229997Sken } 2164229997Sken VOP_UNLOCK(be_lun->vn, 0); 2165229997Sken 2166229997Sken if (error != 0) { 2167229997Sken ctl_be_block_close(be_lun); 2168229997Sken return (error); 2169229997Sken } 2170229997Sken 2171229997Sken be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2172229997Sken be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2173229997Sken 2174229997Sken return (0); 2175229997Sken} 2176229997Sken 2177229997Skenstatic int 2178229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2179229997Sken{ 2180229997Sken struct ctl_be_block_lun *be_lun; 2181229997Sken struct ctl_lun_create_params *params; 2182267481Smav char num_thread_str[16]; 2183229997Sken char tmpstr[32]; 2184267481Smav char *value; 2185264274Smav int retval, num_threads, unmap; 2186267481Smav int tmp_num_threads; 2187229997Sken 2188229997Sken params = &req->reqdata.create; 2189229997Sken retval = 0; 2190272911Smav req->status = CTL_LUN_OK; 2191229997Sken 2192229997Sken num_threads = cbb_num_threads; 2193229997Sken 2194229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2195229997Sken 2196272911Smav be_lun->params = req->reqdata.create; 2197229997Sken be_lun->softc = softc; 2198229997Sken STAILQ_INIT(&be_lun->input_queue); 2199275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2200229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2201229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2202229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2203267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2204267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2205268280Smav ctl_init_opts(&be_lun->ctl_be_lun.options, 2206268280Smav req->num_be_args, req->kern_be_args); 2207229997Sken 2208264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2209256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2210229997Sken 2211229997Sken if (be_lun->lun_zone == NULL) { 2212229997Sken snprintf(req->error_str, sizeof(req->error_str), 2213272911Smav "error allocating UMA zone"); 2214229997Sken goto bailout_error; 2215229997Sken } 2216229997Sken 2217229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2218229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 2219229997Sken else 2220229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 2221229997Sken 2222229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 2223268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 2224267499Smav if (value == NULL) { 2225229997Sken snprintf(req->error_str, sizeof(req->error_str), 2226272911Smav "no file argument specified"); 2227229997Sken goto bailout_error; 2228229997Sken } 2229267499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2230272911Smav be_lun->blocksize = 512; 2231272911Smav be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2232229997Sken 2233229997Sken retval = ctl_be_block_open(softc, be_lun, req); 2234229997Sken if (retval != 0) { 2235229997Sken retval = 0; 2236272911Smav req->status = CTL_LUN_WARNING; 2237229997Sken } 2238229997Sken } else { 2239229997Sken /* 2240229997Sken * For processor devices, we don't have any size. 2241229997Sken */ 2242229997Sken be_lun->blocksize = 0; 2243264191Smav be_lun->pblockexp = 0; 2244264191Smav be_lun->pblockoff = 0; 2245275865Smav be_lun->ublockexp = 0; 2246275865Smav be_lun->ublockoff = 0; 2247229997Sken be_lun->size_blocks = 0; 2248229997Sken be_lun->size_bytes = 0; 2249229997Sken be_lun->ctl_be_lun.maxlba = 0; 2250229997Sken 2251229997Sken /* 2252229997Sken * Default to just 1 thread for processor devices. 2253229997Sken */ 2254229997Sken num_threads = 1; 2255229997Sken } 2256229997Sken 2257229997Sken /* 2258229997Sken * XXX This searching loop might be refactored to be combined with 2259229997Sken * the loop above, 2260229997Sken */ 2261268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 2262267481Smav if (value != NULL) { 2263267481Smav tmp_num_threads = strtol(value, NULL, 0); 2264229997Sken 2265267481Smav /* 2266267481Smav * We don't let the user specify less than one 2267267481Smav * thread, but hope he's clueful enough not to 2268267481Smav * specify 1000 threads. 2269267481Smav */ 2270267481Smav if (tmp_num_threads < 1) { 2271267481Smav snprintf(req->error_str, sizeof(req->error_str), 2272272911Smav "invalid number of threads %s", 2273272911Smav num_thread_str); 2274267481Smav goto bailout_error; 2275229997Sken } 2276267481Smav num_threads = tmp_num_threads; 2277229997Sken } 2278274154Smav unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol); 2279268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2280274154Smav if (value != NULL) 2281274154Smav unmap = (strcmp(value, "on") == 0); 2282229997Sken 2283229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2284229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 2285272911Smav if (be_lun->vn == NULL) 2286272911Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2287264274Smav if (unmap) 2288264274Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2289275568Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2290275568Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_SERSEQ_READ; 2291229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 2292272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2293272911Smav 0 : (be_lun->size_blocks - 1); 2294229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2295264191Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2296264191Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2297275865Smav be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp; 2298275865Smav be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff; 2299275920Smav be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock; 2300275920Smav be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen; 2301229997Sken /* Tell the user the blocksize we ended up using */ 2302272911Smav params->lun_size_bytes = be_lun->size_bytes; 2303229997Sken params->blocksize_bytes = be_lun->blocksize; 2304229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2305229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2306229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2307229997Sken } else 2308229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 2309229997Sken 2310229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2311229997Sken be_lun->ctl_be_lun.lun_config_status = 2312229997Sken ctl_be_block_lun_config_status; 2313229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2314229997Sken 2315229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2316229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2317229997Sken softc->num_luns); 2318229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2319275953Smav MIN(sizeof(be_lun->ctl_be_lun.serial_num), 2320229997Sken sizeof(tmpstr))); 2321229997Sken 2322229997Sken /* Tell the user what we used for a serial number */ 2323229997Sken strncpy((char *)params->serial_num, tmpstr, 2324275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2325229997Sken } else { 2326229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 2327229997Sken params->serial_num, 2328275953Smav MIN(sizeof(be_lun->ctl_be_lun.serial_num), 2329229997Sken sizeof(params->serial_num))); 2330229997Sken } 2331229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2332229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2333229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2334275953Smav MIN(sizeof(be_lun->ctl_be_lun.device_id), 2335229997Sken sizeof(tmpstr))); 2336229997Sken 2337229997Sken /* Tell the user what we used for a device ID */ 2338229997Sken strncpy((char *)params->device_id, tmpstr, 2339275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2340229997Sken } else { 2341229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 2342229997Sken params->device_id, 2343275953Smav MIN(sizeof(be_lun->ctl_be_lun.device_id), 2344275953Smav sizeof(params->device_id))); 2345229997Sken } 2346229997Sken 2347229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2348229997Sken 2349229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2350229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2351229997Sken 2352229997Sken if (be_lun->io_taskqueue == NULL) { 2353229997Sken snprintf(req->error_str, sizeof(req->error_str), 2354272911Smav "unable to create taskqueue"); 2355229997Sken goto bailout_error; 2356229997Sken } 2357229997Sken 2358229997Sken /* 2359229997Sken * Note that we start the same number of threads by default for 2360229997Sken * both the file case and the block device case. For the file 2361229997Sken * case, we need multiple threads to allow concurrency, because the 2362229997Sken * vnode interface is designed to be a blocking interface. For the 2363229997Sken * block device case, ZFS zvols at least will block the caller's 2364229997Sken * context in many instances, and so we need multiple threads to 2365229997Sken * overcome that problem. Other block devices don't need as many 2366229997Sken * threads, but they shouldn't cause too many problems. 2367229997Sken * 2368229997Sken * If the user wants to just have a single thread for a block 2369229997Sken * device, he can specify that when the LUN is created, or change 2370229997Sken * the tunable/sysctl to alter the default number of threads. 2371229997Sken */ 2372229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2373229997Sken /*num threads*/num_threads, 2374229997Sken /*priority*/PWAIT, 2375229997Sken /*thread name*/ 2376229997Sken "%s taskq", be_lun->lunname); 2377229997Sken 2378229997Sken if (retval != 0) 2379229997Sken goto bailout_error; 2380229997Sken 2381229997Sken be_lun->num_threads = num_threads; 2382229997Sken 2383229997Sken mtx_lock(&softc->lock); 2384229997Sken softc->num_luns++; 2385229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2386229997Sken 2387229997Sken mtx_unlock(&softc->lock); 2388229997Sken 2389229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 2390229997Sken if (retval != 0) { 2391229997Sken mtx_lock(&softc->lock); 2392229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2393229997Sken links); 2394229997Sken softc->num_luns--; 2395229997Sken mtx_unlock(&softc->lock); 2396229997Sken snprintf(req->error_str, sizeof(req->error_str), 2397272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2398272911Smav "details", retval); 2399229997Sken retval = 0; 2400229997Sken goto bailout_error; 2401229997Sken } 2402229997Sken 2403229997Sken mtx_lock(&softc->lock); 2404229997Sken 2405229997Sken /* 2406229997Sken * Tell the config_status routine that we're waiting so it won't 2407229997Sken * clean up the LUN in the event of an error. 2408229997Sken */ 2409229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2410229997Sken 2411229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2412229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2413229997Sken if (retval == EINTR) 2414229997Sken break; 2415229997Sken } 2416229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2417229997Sken 2418229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2419229997Sken snprintf(req->error_str, sizeof(req->error_str), 2420272911Smav "LUN configuration error, see dmesg for details"); 2421229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2422229997Sken links); 2423229997Sken softc->num_luns--; 2424229997Sken mtx_unlock(&softc->lock); 2425229997Sken goto bailout_error; 2426229997Sken } else { 2427229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2428229997Sken } 2429229997Sken 2430229997Sken mtx_unlock(&softc->lock); 2431229997Sken 2432229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2433229997Sken be_lun->blocksize, 2434229997Sken DEVSTAT_ALL_SUPPORTED, 2435229997Sken be_lun->ctl_be_lun.lun_type 2436229997Sken | DEVSTAT_TYPE_IF_OTHER, 2437229997Sken DEVSTAT_PRIORITY_OTHER); 2438229997Sken 2439229997Sken return (retval); 2440229997Sken 2441229997Skenbailout_error: 2442229997Sken req->status = CTL_LUN_ERROR; 2443229997Sken 2444267429Smav if (be_lun->io_taskqueue != NULL) 2445267429Smav taskqueue_free(be_lun->io_taskqueue); 2446229997Sken ctl_be_block_close(be_lun); 2447267429Smav if (be_lun->dev_path != NULL) 2448267429Smav free(be_lun->dev_path, M_CTLBLK); 2449267429Smav if (be_lun->lun_zone != NULL) 2450267429Smav uma_zdestroy(be_lun->lun_zone); 2451268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2452267877Smav mtx_destroy(&be_lun->queue_lock); 2453267877Smav mtx_destroy(&be_lun->io_lock); 2454229997Sken free(be_lun, M_CTLBLK); 2455229997Sken 2456229997Sken return (retval); 2457229997Sken} 2458229997Sken 2459229997Skenstatic int 2460229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2461229997Sken{ 2462229997Sken struct ctl_lun_rm_params *params; 2463229997Sken struct ctl_be_block_lun *be_lun; 2464229997Sken int retval; 2465229997Sken 2466229997Sken params = &req->reqdata.rm; 2467229997Sken 2468229997Sken mtx_lock(&softc->lock); 2469229997Sken 2470229997Sken be_lun = NULL; 2471229997Sken 2472229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2473229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2474229997Sken break; 2475229997Sken } 2476229997Sken mtx_unlock(&softc->lock); 2477229997Sken 2478229997Sken if (be_lun == NULL) { 2479229997Sken snprintf(req->error_str, sizeof(req->error_str), 2480272911Smav "LUN %u is not managed by the block backend", 2481272911Smav params->lun_id); 2482229997Sken goto bailout_error; 2483229997Sken } 2484229997Sken 2485229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2486229997Sken 2487229997Sken if (retval != 0) { 2488229997Sken snprintf(req->error_str, sizeof(req->error_str), 2489272911Smav "error %d returned from ctl_disable_lun() for " 2490272911Smav "LUN %d", retval, params->lun_id); 2491229997Sken goto bailout_error; 2492229997Sken 2493229997Sken } 2494229997Sken 2495229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2496229997Sken if (retval != 0) { 2497229997Sken snprintf(req->error_str, sizeof(req->error_str), 2498272911Smav "error %d returned from ctl_invalidate_lun() for " 2499272911Smav "LUN %d", retval, params->lun_id); 2500229997Sken goto bailout_error; 2501229997Sken } 2502229997Sken 2503229997Sken mtx_lock(&softc->lock); 2504229997Sken 2505229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2506229997Sken 2507229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2508229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2509229997Sken if (retval == EINTR) 2510229997Sken break; 2511229997Sken } 2512229997Sken 2513229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2514229997Sken 2515229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2516229997Sken snprintf(req->error_str, sizeof(req->error_str), 2517272911Smav "interrupted waiting for LUN to be freed"); 2518229997Sken mtx_unlock(&softc->lock); 2519229997Sken goto bailout_error; 2520229997Sken } 2521229997Sken 2522229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2523229997Sken 2524229997Sken softc->num_luns--; 2525229997Sken mtx_unlock(&softc->lock); 2526229997Sken 2527229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2528229997Sken 2529229997Sken taskqueue_free(be_lun->io_taskqueue); 2530229997Sken 2531229997Sken ctl_be_block_close(be_lun); 2532229997Sken 2533229997Sken if (be_lun->disk_stats != NULL) 2534229997Sken devstat_remove_entry(be_lun->disk_stats); 2535229997Sken 2536229997Sken uma_zdestroy(be_lun->lun_zone); 2537229997Sken 2538268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2539229997Sken free(be_lun->dev_path, M_CTLBLK); 2540267877Smav mtx_destroy(&be_lun->queue_lock); 2541267877Smav mtx_destroy(&be_lun->io_lock); 2542229997Sken free(be_lun, M_CTLBLK); 2543229997Sken 2544229997Sken req->status = CTL_LUN_OK; 2545229997Sken 2546229997Sken return (0); 2547229997Sken 2548229997Skenbailout_error: 2549229997Sken 2550229997Sken req->status = CTL_LUN_ERROR; 2551229997Sken 2552229997Sken return (0); 2553229997Sken} 2554229997Sken 2555232604Straszstatic int 2556232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2557232604Strasz struct ctl_lun_req *req) 2558232604Strasz{ 2559232604Strasz struct vattr vattr; 2560232604Strasz int error; 2561272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2562232604Strasz 2563232604Strasz if (params->lun_size_bytes != 0) { 2564232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2565232604Strasz } else { 2566271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2567232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2568271794Smav VOP_UNLOCK(be_lun->vn, 0); 2569232604Strasz if (error != 0) { 2570232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2571232604Strasz "error calling VOP_GETATTR() for file %s", 2572232604Strasz be_lun->dev_path); 2573232604Strasz return (error); 2574232604Strasz } 2575232604Strasz 2576232604Strasz be_lun->size_bytes = vattr.va_size; 2577232604Strasz } 2578232604Strasz 2579232604Strasz return (0); 2580232604Strasz} 2581232604Strasz 2582232604Straszstatic int 2583232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2584232604Strasz struct ctl_lun_req *req) 2585232604Strasz{ 2586271794Smav struct ctl_be_block_devdata *dev_data; 2587232604Strasz int error; 2588272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2589232604Strasz uint64_t size_bytes; 2590232604Strasz 2591271794Smav dev_data = &be_lun->backend.dev; 2592271794Smav if (!dev_data->csw->d_ioctl) { 2593232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2594272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2595232604Strasz return (ENODEV); 2596232604Strasz } 2597232604Strasz 2598271794Smav error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2599232604Strasz (caddr_t)&size_bytes, FREAD, 2600232604Strasz curthread); 2601232604Strasz if (error) { 2602232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2603272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2604272911Smav "on %s!", error, be_lun->dev_path); 2605232604Strasz return (error); 2606232604Strasz } 2607232604Strasz 2608232604Strasz if (params->lun_size_bytes != 0) { 2609232604Strasz if (params->lun_size_bytes > size_bytes) { 2610232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2611272911Smav "requested LUN size %ju > backing device " 2612272911Smav "size %ju", 2613232604Strasz (uintmax_t)params->lun_size_bytes, 2614232604Strasz (uintmax_t)size_bytes); 2615232604Strasz return (EINVAL); 2616232604Strasz } 2617232604Strasz 2618232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2619232604Strasz } else { 2620232604Strasz be_lun->size_bytes = size_bytes; 2621232604Strasz } 2622232604Strasz 2623232604Strasz return (0); 2624232604Strasz} 2625232604Strasz 2626232604Straszstatic int 2627232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2628232604Strasz{ 2629232604Strasz struct ctl_lun_modify_params *params; 2630232604Strasz struct ctl_be_block_lun *be_lun; 2631271794Smav uint64_t oldsize; 2632241896Skib int error; 2633232604Strasz 2634232604Strasz params = &req->reqdata.modify; 2635232604Strasz 2636232604Strasz mtx_lock(&softc->lock); 2637232604Strasz be_lun = NULL; 2638232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2639232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2640232604Strasz break; 2641232604Strasz } 2642232604Strasz mtx_unlock(&softc->lock); 2643232604Strasz 2644232604Strasz if (be_lun == NULL) { 2645232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2646272911Smav "LUN %u is not managed by the block backend", 2647272911Smav params->lun_id); 2648232604Strasz goto bailout_error; 2649232604Strasz } 2650232604Strasz 2651272911Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2652232604Strasz 2653274253Smav oldsize = be_lun->size_bytes; 2654272911Smav if (be_lun->vn == NULL) 2655272911Smav error = ctl_be_block_open(softc, be_lun, req); 2656272911Smav else if (be_lun->vn->v_type == VREG) 2657232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2658232604Strasz else 2659232604Strasz error = ctl_be_block_modify_dev(be_lun, req); 2660232604Strasz 2661274253Smav if (error == 0 && be_lun->size_bytes != oldsize) { 2662271794Smav be_lun->size_blocks = be_lun->size_bytes >> 2663271794Smav be_lun->blocksize_shift; 2664232604Strasz 2665271794Smav /* 2666271794Smav * The maximum LBA is the size - 1. 2667271794Smav * 2668271794Smav * XXX: Note that this field is being updated without locking, 2669271794Smav * which might cause problems on 32-bit architectures. 2670271794Smav */ 2671272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2672272911Smav 0 : (be_lun->size_blocks - 1); 2673272911Smav be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2674272911Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2675272911Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2676275865Smav be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp; 2677275865Smav be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff; 2678275920Smav be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock; 2679275920Smav be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen; 2680271794Smav ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2681272911Smav if (oldsize == 0 && be_lun->size_blocks != 0) 2682272911Smav ctl_lun_online(&be_lun->ctl_be_lun); 2683271794Smav } 2684232604Strasz 2685232604Strasz /* Tell the user the exact size we ended up using */ 2686232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2687232604Strasz 2688272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2689232604Strasz 2690232604Strasz return (0); 2691232604Strasz 2692232604Straszbailout_error: 2693232604Strasz req->status = CTL_LUN_ERROR; 2694232604Strasz 2695232604Strasz return (0); 2696232604Strasz} 2697232604Strasz 2698229997Skenstatic void 2699229997Skenctl_be_block_lun_shutdown(void *be_lun) 2700229997Sken{ 2701229997Sken struct ctl_be_block_lun *lun; 2702229997Sken struct ctl_be_block_softc *softc; 2703229997Sken 2704229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2705229997Sken 2706229997Sken softc = lun->softc; 2707229997Sken 2708229997Sken mtx_lock(&softc->lock); 2709229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2710229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2711229997Sken wakeup(lun); 2712229997Sken mtx_unlock(&softc->lock); 2713229997Sken 2714229997Sken} 2715229997Sken 2716229997Skenstatic void 2717229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2718229997Sken{ 2719229997Sken struct ctl_be_block_lun *lun; 2720229997Sken struct ctl_be_block_softc *softc; 2721229997Sken 2722229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2723229997Sken softc = lun->softc; 2724229997Sken 2725229997Sken if (status == CTL_LUN_CONFIG_OK) { 2726229997Sken mtx_lock(&softc->lock); 2727229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2728229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2729229997Sken wakeup(lun); 2730229997Sken mtx_unlock(&softc->lock); 2731229997Sken 2732229997Sken /* 2733229997Sken * We successfully added the LUN, attempt to enable it. 2734229997Sken */ 2735229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2736229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2737229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2738229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2739229997Sken __func__); 2740229997Sken } 2741229997Sken } 2742229997Sken 2743229997Sken return; 2744229997Sken } 2745229997Sken 2746229997Sken 2747229997Sken mtx_lock(&softc->lock); 2748229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2749229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2750229997Sken wakeup(lun); 2751229997Sken mtx_unlock(&softc->lock); 2752229997Sken} 2753229997Sken 2754229997Sken 2755229997Skenstatic int 2756229997Skenctl_be_block_config_write(union ctl_io *io) 2757229997Sken{ 2758229997Sken struct ctl_be_block_lun *be_lun; 2759229997Sken struct ctl_be_lun *ctl_be_lun; 2760229997Sken int retval; 2761229997Sken 2762229997Sken retval = 0; 2763229997Sken 2764229997Sken DPRINTF("entered\n"); 2765229997Sken 2766229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2767229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2768229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2769229997Sken 2770229997Sken switch (io->scsiio.cdb[0]) { 2771229997Sken case SYNCHRONIZE_CACHE: 2772229997Sken case SYNCHRONIZE_CACHE_16: 2773264274Smav case WRITE_SAME_10: 2774264274Smav case WRITE_SAME_16: 2775264274Smav case UNMAP: 2776229997Sken /* 2777229997Sken * The upper level CTL code will filter out any CDBs with 2778229997Sken * the immediate bit set and return the proper error. 2779229997Sken * 2780229997Sken * We don't really need to worry about what LBA range the 2781229997Sken * user asked to be synced out. When they issue a sync 2782229997Sken * cache command, we'll sync out the whole thing. 2783229997Sken */ 2784267877Smav mtx_lock(&be_lun->queue_lock); 2785229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2786229997Sken links); 2787267877Smav mtx_unlock(&be_lun->queue_lock); 2788229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2789229997Sken break; 2790229997Sken case START_STOP_UNIT: { 2791229997Sken struct scsi_start_stop_unit *cdb; 2792229997Sken 2793229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2794229997Sken 2795229997Sken if (cdb->how & SSS_START) 2796229997Sken retval = ctl_start_lun(ctl_be_lun); 2797229997Sken else { 2798229997Sken retval = ctl_stop_lun(ctl_be_lun); 2799229997Sken /* 2800229997Sken * XXX KDM Copan-specific offline behavior. 2801229997Sken * Figure out a reasonable way to port this? 2802229997Sken */ 2803229997Sken#ifdef NEEDTOPORT 2804229997Sken if ((retval == 0) 2805229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2806229997Sken retval = ctl_lun_offline(ctl_be_lun); 2807229997Sken#endif 2808229997Sken } 2809229997Sken 2810229997Sken /* 2811229997Sken * In general, the above routines should not fail. They 2812229997Sken * just set state for the LUN. So we've got something 2813229997Sken * pretty wrong here if we can't start or stop the LUN. 2814229997Sken */ 2815229997Sken if (retval != 0) { 2816229997Sken ctl_set_internal_failure(&io->scsiio, 2817229997Sken /*sks_valid*/ 1, 2818229997Sken /*retry_count*/ 0xf051); 2819229997Sken retval = CTL_RETVAL_COMPLETE; 2820229997Sken } else { 2821229997Sken ctl_set_success(&io->scsiio); 2822229997Sken } 2823229997Sken ctl_config_write_done(io); 2824229997Sken break; 2825229997Sken } 2826229997Sken default: 2827229997Sken ctl_set_invalid_opcode(&io->scsiio); 2828229997Sken ctl_config_write_done(io); 2829229997Sken retval = CTL_RETVAL_COMPLETE; 2830229997Sken break; 2831229997Sken } 2832229997Sken 2833229997Sken return (retval); 2834229997Sken} 2835229997Sken 2836229997Skenstatic int 2837229997Skenctl_be_block_config_read(union ctl_io *io) 2838229997Sken{ 2839275474Smav struct ctl_be_block_lun *be_lun; 2840275474Smav struct ctl_be_lun *ctl_be_lun; 2841275474Smav int retval = 0; 2842275474Smav 2843275474Smav DPRINTF("entered\n"); 2844275474Smav 2845275474Smav ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2846275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2847275474Smav be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2848275474Smav 2849275474Smav switch (io->scsiio.cdb[0]) { 2850275474Smav case SERVICE_ACTION_IN: 2851275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2852275474Smav mtx_lock(&be_lun->queue_lock); 2853275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2854275474Smav &io->io_hdr, links); 2855275474Smav mtx_unlock(&be_lun->queue_lock); 2856275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2857275474Smav &be_lun->io_task); 2858275474Smav retval = CTL_RETVAL_QUEUED; 2859275474Smav break; 2860275474Smav } 2861275474Smav ctl_set_invalid_field(&io->scsiio, 2862275474Smav /*sks_valid*/ 1, 2863275474Smav /*command*/ 1, 2864275474Smav /*field*/ 1, 2865275474Smav /*bit_valid*/ 1, 2866275474Smav /*bit*/ 4); 2867275474Smav ctl_config_read_done(io); 2868275474Smav retval = CTL_RETVAL_COMPLETE; 2869275474Smav break; 2870275474Smav default: 2871275474Smav ctl_set_invalid_opcode(&io->scsiio); 2872275474Smav ctl_config_read_done(io); 2873275474Smav retval = CTL_RETVAL_COMPLETE; 2874275474Smav break; 2875275474Smav } 2876275474Smav 2877275474Smav return (retval); 2878229997Sken} 2879229997Sken 2880229997Skenstatic int 2881229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2882229997Sken{ 2883229997Sken struct ctl_be_block_lun *lun; 2884229997Sken int retval; 2885229997Sken 2886229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2887229997Sken retval = 0; 2888229997Sken 2889268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2890229997Sken 2891229997Sken if (retval != 0) 2892229997Sken goto bailout; 2893229997Sken 2894229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2895229997Sken 2896229997Sken if (retval != 0) 2897229997Sken goto bailout; 2898229997Sken 2899268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2900229997Sken 2901229997Skenbailout: 2902229997Sken 2903229997Sken return (retval); 2904229997Sken} 2905229997Sken 2906274154Smavstatic uint64_t 2907274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2908274154Smav{ 2909274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2910274154Smav 2911274154Smav if (lun->getattr == NULL) 2912274154Smav return (UINT64_MAX); 2913274154Smav return (lun->getattr(lun, attrname)); 2914274154Smav} 2915274154Smav 2916229997Skenint 2917229997Skenctl_be_block_init(void) 2918229997Sken{ 2919229997Sken struct ctl_be_block_softc *softc; 2920229997Sken int retval; 2921229997Sken 2922229997Sken softc = &backend_block_softc; 2923229997Sken retval = 0; 2924229997Sken 2925267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2926264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2927264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2928229997Sken STAILQ_INIT(&softc->disk_list); 2929229997Sken STAILQ_INIT(&softc->lun_list); 2930229997Sken 2931229997Sken return (retval); 2932229997Sken} 2933