ctl_backend_block.c revision 275474
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 275474 2014-12-04 11:34:19Z 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; 176229997Sken struct ctl_be_block_softc *softc; 177229997Sken struct devstat *disk_stats; 178229997Sken ctl_be_block_lun_flags flags; 179229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 180229997Sken struct ctl_be_lun ctl_be_lun; 181229997Sken struct taskqueue *io_taskqueue; 182229997Sken struct task io_task; 183229997Sken int num_threads; 184229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 185275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 186229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 187229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 188267877Smav struct mtx_padalign io_lock; 189267877Smav struct mtx_padalign queue_lock; 190229997Sken}; 191229997Sken 192229997Sken/* 193229997Sken * Overall softc structure for the block backend module. 194229997Sken */ 195229997Skenstruct ctl_be_block_softc { 196229997Sken struct mtx lock; 197229997Sken int num_disks; 198229997Sken STAILQ_HEAD(, ctl_block_disk) disk_list; 199229997Sken int num_luns; 200229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 201229997Sken}; 202229997Sken 203229997Skenstatic struct ctl_be_block_softc backend_block_softc; 204229997Sken 205229997Sken/* 206229997Sken * Per-I/O information. 207229997Sken */ 208229997Skenstruct ctl_be_block_io { 209229997Sken union ctl_io *io; 210229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 211229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 212229997Sken int bio_cmd; 213229997Sken int num_segs; 214229997Sken int num_bios_sent; 215229997Sken int num_bios_done; 216229997Sken int send_complete; 217229997Sken int num_errors; 218229997Sken struct bintime ds_t0; 219229997Sken devstat_tag_type ds_tag_type; 220229997Sken devstat_trans_flags ds_trans_type; 221229997Sken uint64_t io_len; 222229997Sken uint64_t io_offset; 223229997Sken struct ctl_be_block_softc *softc; 224229997Sken struct ctl_be_block_lun *lun; 225264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 226229997Sken}; 227229997Sken 228229997Skenstatic int cbb_num_threads = 14; 229229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 230229997Sken "CAM Target Layer Block Backend"); 231267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 232229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 233229997Sken 234229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 235229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 236229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 237229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 238229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 239229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 240229997Sken struct ctl_be_block_io *beio); 241229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 242229997Sken struct ctl_be_block_io *beio); 243275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 244275474Smav struct ctl_be_block_io *beio); 245229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 246229997Sken struct ctl_be_block_io *beio); 247264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 248264274Smav struct ctl_be_block_io *beio); 249229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 250229997Sken struct ctl_be_block_io *beio); 251274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 252274154Smav const char *attrname); 253275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 254275474Smav union ctl_io *io); 255229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 256229997Sken union ctl_io *io); 257229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 258229997Sken union ctl_io *io); 259229997Skenstatic void ctl_be_block_worker(void *context, int pending); 260229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 261229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 262229997Sken int flag, struct thread *td); 263229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 264229997Sken struct ctl_lun_req *req); 265229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 266229997Sken struct ctl_lun_req *req); 267229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 268229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 269229997Sken struct ctl_be_block_lun *be_lun, 270229997Sken struct ctl_lun_req *req); 271229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 272229997Sken struct ctl_lun_req *req); 273229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 274229997Sken struct ctl_lun_req *req); 275232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 276232604Strasz struct ctl_lun_req *req); 277232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 278232604Strasz struct ctl_lun_req *req); 279232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 280232604Strasz struct ctl_lun_req *req); 281229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 282229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 283229997Sken ctl_lun_config_status status); 284229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 285229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 286229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 287274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 288229997Skenint ctl_be_block_init(void); 289229997Sken 290229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 291229997Sken{ 292230334Sken .name = "block", 293230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 294230334Sken .init = ctl_be_block_init, 295230334Sken .data_submit = ctl_be_block_submit, 296230334Sken .data_move_done = ctl_be_block_move_done, 297230334Sken .config_read = ctl_be_block_config_read, 298230334Sken .config_write = ctl_be_block_config_write, 299230334Sken .ioctl = ctl_be_block_ioctl, 300274154Smav .lun_info = ctl_be_block_lun_info, 301274154Smav .lun_attr = ctl_be_block_lun_attr 302229997Sken}; 303229997Sken 304229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 305229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 306229997Sken 307264020Straszstatic uma_zone_t beio_zone; 308264020Strasz 309229997Skenstatic struct ctl_be_block_io * 310229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 311229997Sken{ 312229997Sken struct ctl_be_block_io *beio; 313229997Sken 314264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 315264020Strasz beio->softc = softc; 316229997Sken return (beio); 317229997Sken} 318229997Sken 319229997Skenstatic void 320229997Skenctl_free_beio(struct ctl_be_block_io *beio) 321229997Sken{ 322229997Sken int duplicate_free; 323229997Sken int i; 324229997Sken 325229997Sken duplicate_free = 0; 326229997Sken 327229997Sken for (i = 0; i < beio->num_segs; i++) { 328229997Sken if (beio->sg_segs[i].addr == NULL) 329229997Sken duplicate_free++; 330229997Sken 331229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 332229997Sken beio->sg_segs[i].addr = NULL; 333267537Smav 334267537Smav /* For compare we had two equal S/G lists. */ 335267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 336267537Smav uma_zfree(beio->lun->lun_zone, 337267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 338267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 339267537Smav } 340229997Sken } 341229997Sken 342229997Sken if (duplicate_free > 0) { 343229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 344229997Sken duplicate_free, beio->num_segs); 345229997Sken } 346229997Sken 347264020Strasz uma_zfree(beio_zone, beio); 348229997Sken} 349229997Sken 350229997Skenstatic void 351229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 352229997Sken{ 353267877Smav union ctl_io *io = beio->io; 354229997Sken 355264274Smav if (beio->beio_cont != NULL) { 356264274Smav beio->beio_cont(beio); 357264274Smav } else { 358264274Smav ctl_free_beio(beio); 359267537Smav ctl_data_submit_done(io); 360264274Smav } 361229997Sken} 362229997Sken 363229997Skenstatic int 364229997Skenctl_be_block_move_done(union ctl_io *io) 365229997Sken{ 366229997Sken struct ctl_be_block_io *beio; 367229997Sken struct ctl_be_block_lun *be_lun; 368267537Smav struct ctl_lba_len_flags *lbalen; 369229997Sken#ifdef CTL_TIME_IO 370229997Sken struct bintime cur_bt; 371267537Smav#endif 372267537Smav int i; 373229997Sken 374267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 375229997Sken be_lun = beio->lun; 376229997Sken 377229997Sken DPRINTF("entered\n"); 378229997Sken 379229997Sken#ifdef CTL_TIME_IO 380229997Sken getbintime(&cur_bt); 381229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 382229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 383229997Sken io->io_hdr.num_dmas++; 384229997Sken#endif 385267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 386229997Sken 387229997Sken /* 388229997Sken * We set status at this point for read commands, and write 389229997Sken * commands with errors. 390229997Sken */ 391275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 392275058Smav ; 393275058Smav } else if ((io->io_hdr.port_status == 0) && 394267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 395267537Smav lbalen = ARGS(beio->io); 396267537Smav if (lbalen->flags & CTL_LLF_READ) { 397267537Smav ctl_set_success(&io->scsiio); 398267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 399267537Smav /* We have two data blocks ready for comparison. */ 400267537Smav for (i = 0; i < beio->num_segs; i++) { 401267537Smav if (memcmp(beio->sg_segs[i].addr, 402267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 403267537Smav beio->sg_segs[i].len) != 0) 404267537Smav break; 405267537Smav } 406267537Smav if (i < beio->num_segs) 407267537Smav ctl_set_sense(&io->scsiio, 408267537Smav /*current_error*/ 1, 409267537Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 410267537Smav /*asc*/ 0x1D, 411267537Smav /*ascq*/ 0x00, 412267537Smav SSD_ELEM_NONE); 413267537Smav else 414267537Smav ctl_set_success(&io->scsiio); 415267537Smav } 416275058Smav } else if ((io->io_hdr.port_status != 0) && 417275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 418275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 419229997Sken /* 420229997Sken * For hardware error sense keys, the sense key 421229997Sken * specific value is defined to be a retry count, 422229997Sken * but we use it to pass back an internal FETD 423229997Sken * error code. XXX KDM Hopefully the FETD is only 424229997Sken * using 16 bits for an error code, since that's 425229997Sken * all the space we have in the sks field. 426229997Sken */ 427229997Sken ctl_set_internal_failure(&io->scsiio, 428229997Sken /*sks_valid*/ 1, 429229997Sken /*retry_count*/ 430229997Sken io->io_hdr.port_status); 431229997Sken } 432229997Sken 433229997Sken /* 434229997Sken * If this is a read, or a write with errors, it is done. 435229997Sken */ 436229997Sken if ((beio->bio_cmd == BIO_READ) 437229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 438229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 439229997Sken ctl_complete_beio(beio); 440229997Sken return (0); 441229997Sken } 442229997Sken 443229997Sken /* 444229997Sken * At this point, we have a write and the DMA completed 445229997Sken * successfully. We now have to queue it to the task queue to 446229997Sken * execute the backend I/O. That is because we do blocking 447229997Sken * memory allocations, and in the file backing case, blocking I/O. 448229997Sken * This move done routine is generally called in the SIM's 449229997Sken * interrupt context, and therefore we cannot block. 450229997Sken */ 451267877Smav mtx_lock(&be_lun->queue_lock); 452229997Sken /* 453229997Sken * XXX KDM make sure that links is okay to use at this point. 454229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 455229997Sken * or deal with resource allocation here. 456229997Sken */ 457229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 458267877Smav mtx_unlock(&be_lun->queue_lock); 459229997Sken 460229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 461229997Sken 462229997Sken return (0); 463229997Sken} 464229997Sken 465229997Skenstatic void 466229997Skenctl_be_block_biodone(struct bio *bio) 467229997Sken{ 468229997Sken struct ctl_be_block_io *beio; 469229997Sken struct ctl_be_block_lun *be_lun; 470229997Sken union ctl_io *io; 471261538Smav int error; 472229997Sken 473229997Sken beio = bio->bio_caller1; 474229997Sken be_lun = beio->lun; 475229997Sken io = beio->io; 476229997Sken 477229997Sken DPRINTF("entered\n"); 478229997Sken 479261538Smav error = bio->bio_error; 480267877Smav mtx_lock(&be_lun->io_lock); 481261538Smav if (error != 0) 482229997Sken beio->num_errors++; 483229997Sken 484229997Sken beio->num_bios_done++; 485229997Sken 486229997Sken /* 487229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 488229997Sken * during the free might cause it to complain. 489229997Sken */ 490229997Sken g_destroy_bio(bio); 491229997Sken 492229997Sken /* 493229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 494229997Sken * complete, then we're done. 495229997Sken */ 496229997Sken if ((beio->send_complete == 0) 497229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 498267877Smav mtx_unlock(&be_lun->io_lock); 499229997Sken return; 500229997Sken } 501229997Sken 502229997Sken /* 503229997Sken * At this point, we've verified that we are the last I/O to 504229997Sken * complete, so it's safe to drop the lock. 505229997Sken */ 506267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 507267877Smav beio->ds_tag_type, beio->ds_trans_type, 508267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 509267877Smav mtx_unlock(&be_lun->io_lock); 510229997Sken 511229997Sken /* 512229997Sken * If there are any errors from the backing device, we fail the 513229997Sken * entire I/O with a medium error. 514229997Sken */ 515229997Sken if (beio->num_errors > 0) { 516261538Smav if (error == EOPNOTSUPP) { 517261538Smav ctl_set_invalid_opcode(&io->scsiio); 518273809Smav } else if (error == ENOSPC) { 519273809Smav ctl_set_space_alloc_fail(&io->scsiio); 520261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 521229997Sken /* XXX KDM is there is a better error here? */ 522229997Sken ctl_set_internal_failure(&io->scsiio, 523229997Sken /*sks_valid*/ 1, 524229997Sken /*retry_count*/ 0xbad2); 525229997Sken } else 526229997Sken ctl_set_medium_error(&io->scsiio); 527229997Sken ctl_complete_beio(beio); 528229997Sken return; 529229997Sken } 530229997Sken 531229997Sken /* 532267537Smav * If this is a write, a flush, a delete or verify, we're all done. 533229997Sken * If this is a read, we can now send the data to the user. 534229997Sken */ 535229997Sken if ((beio->bio_cmd == BIO_WRITE) 536264274Smav || (beio->bio_cmd == BIO_FLUSH) 537267537Smav || (beio->bio_cmd == BIO_DELETE) 538267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 539229997Sken ctl_set_success(&io->scsiio); 540229997Sken ctl_complete_beio(beio); 541229997Sken } else { 542275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 543275058Smav beio->beio_cont == NULL) 544275058Smav ctl_set_success(&io->scsiio); 545229997Sken#ifdef CTL_TIME_IO 546229997Sken getbintime(&io->io_hdr.dma_start_bt); 547229997Sken#endif 548229997Sken ctl_datamove(io); 549229997Sken } 550229997Sken} 551229997Sken 552229997Skenstatic void 553229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 554229997Sken struct ctl_be_block_io *beio) 555229997Sken{ 556267877Smav union ctl_io *io = beio->io; 557229997Sken struct mount *mountpoint; 558241896Skib int error, lock_flags; 559229997Sken 560229997Sken DPRINTF("entered\n"); 561229997Sken 562267877Smav binuptime(&beio->ds_t0); 563267877Smav mtx_lock(&be_lun->io_lock); 564267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 565267877Smav mtx_unlock(&be_lun->io_lock); 566229997Sken 567267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 568229997Sken 569229997Sken if (MNT_SHARED_WRITES(mountpoint) 570229997Sken || ((mountpoint == NULL) 571229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 572229997Sken lock_flags = LK_SHARED; 573229997Sken else 574229997Sken lock_flags = LK_EXCLUSIVE; 575229997Sken 576229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 577229997Sken 578229997Sken error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 579229997Sken VOP_UNLOCK(be_lun->vn, 0); 580229997Sken 581229997Sken vn_finished_write(mountpoint); 582229997Sken 583267877Smav mtx_lock(&be_lun->io_lock); 584267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 585267877Smav beio->ds_tag_type, beio->ds_trans_type, 586267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 587267877Smav mtx_unlock(&be_lun->io_lock); 588267877Smav 589229997Sken if (error == 0) 590229997Sken ctl_set_success(&io->scsiio); 591229997Sken else { 592229997Sken /* XXX KDM is there is a better error here? */ 593229997Sken ctl_set_internal_failure(&io->scsiio, 594229997Sken /*sks_valid*/ 1, 595229997Sken /*retry_count*/ 0xbad1); 596229997Sken } 597229997Sken 598229997Sken ctl_complete_beio(beio); 599229997Sken} 600229997Sken 601258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 602258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 603258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 604258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 605229997Sken 606229997Skenstatic void 607229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 608229997Sken struct ctl_be_block_io *beio) 609229997Sken{ 610229997Sken struct ctl_be_block_filedata *file_data; 611229997Sken union ctl_io *io; 612229997Sken struct uio xuio; 613229997Sken struct iovec *xiovec; 614241896Skib int flags; 615229997Sken int error, i; 616229997Sken 617229997Sken DPRINTF("entered\n"); 618229997Sken 619229997Sken file_data = &be_lun->backend.file; 620229997Sken io = beio->io; 621271309Smav flags = 0; 622271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 623271309Smav flags |= IO_DIRECT; 624271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 625271309Smav flags |= IO_SYNC; 626229997Sken 627267537Smav bzero(&xuio, sizeof(xuio)); 628229997Sken if (beio->bio_cmd == BIO_READ) { 629229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 630267537Smav xuio.uio_rw = UIO_READ; 631229997Sken } else { 632229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 633267537Smav xuio.uio_rw = UIO_WRITE; 634229997Sken } 635229997Sken xuio.uio_offset = beio->io_offset; 636229997Sken xuio.uio_resid = beio->io_len; 637229997Sken xuio.uio_segflg = UIO_SYSSPACE; 638229997Sken xuio.uio_iov = beio->xiovecs; 639229997Sken xuio.uio_iovcnt = beio->num_segs; 640229997Sken xuio.uio_td = curthread; 641229997Sken 642229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 643229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 644229997Sken xiovec->iov_len = beio->sg_segs[i].len; 645229997Sken } 646229997Sken 647267877Smav binuptime(&beio->ds_t0); 648267877Smav mtx_lock(&be_lun->io_lock); 649267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 650267877Smav mtx_unlock(&be_lun->io_lock); 651267877Smav 652229997Sken if (beio->bio_cmd == BIO_READ) { 653229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 654229997Sken 655229997Sken /* 656229997Sken * UFS pays attention to IO_DIRECT for reads. If the 657229997Sken * DIRECTIO option is configured into the kernel, it calls 658229997Sken * ffs_rawread(). But that only works for single-segment 659229997Sken * uios with user space addresses. In our case, with a 660229997Sken * kernel uio, it still reads into the buffer cache, but it 661229997Sken * will just try to release the buffer from the cache later 662229997Sken * on in ffs_read(). 663229997Sken * 664229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 665229997Sken * 666229997Sken * UFS does not pay attention to IO_SYNC for reads. 667229997Sken * 668229997Sken * ZFS pays attention to IO_SYNC (which translates into the 669229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 670229997Sken * attempts to sync the file before reading. 671229997Sken * 672229997Sken * So, to attempt to provide some barrier semantics in the 673229997Sken * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 674229997Sken */ 675271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 676229997Sken 677229997Sken VOP_UNLOCK(be_lun->vn, 0); 678267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 679229997Sken } else { 680229997Sken struct mount *mountpoint; 681229997Sken int lock_flags; 682229997Sken 683229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 684229997Sken 685229997Sken if (MNT_SHARED_WRITES(mountpoint) 686229997Sken || ((mountpoint == NULL) 687229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 688229997Sken lock_flags = LK_SHARED; 689229997Sken else 690229997Sken lock_flags = LK_EXCLUSIVE; 691229997Sken 692229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 693229997Sken 694229997Sken /* 695229997Sken * UFS pays attention to IO_DIRECT for writes. The write 696229997Sken * is done asynchronously. (Normally the write would just 697229997Sken * get put into cache. 698229997Sken * 699229997Sken * UFS pays attention to IO_SYNC for writes. It will 700229997Sken * attempt to write the buffer out synchronously if that 701229997Sken * flag is set. 702229997Sken * 703229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 704229997Sken * 705229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 706229997Sken * for writes. It will flush the transaction from the 707229997Sken * cache before returning. 708229997Sken * 709229997Sken * So if we've got the BIO_ORDERED flag set, we want 710229997Sken * IO_SYNC in either the UFS or ZFS case. 711229997Sken */ 712271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 713229997Sken VOP_UNLOCK(be_lun->vn, 0); 714229997Sken 715229997Sken vn_finished_write(mountpoint); 716267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 717229997Sken } 718229997Sken 719267877Smav mtx_lock(&be_lun->io_lock); 720267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 721267877Smav beio->ds_tag_type, beio->ds_trans_type, 722267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 723267877Smav mtx_unlock(&be_lun->io_lock); 724267877Smav 725229997Sken /* 726229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 727229997Sken * return the I/O to the user. 728229997Sken */ 729229997Sken if (error != 0) { 730229997Sken char path_str[32]; 731229997Sken 732229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 733229997Sken printf("%s%s command returned errno %d\n", path_str, 734229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 735273809Smav if (error == ENOSPC) { 736273809Smav ctl_set_space_alloc_fail(&io->scsiio); 737273809Smav } else 738273809Smav ctl_set_medium_error(&io->scsiio); 739229997Sken ctl_complete_beio(beio); 740229997Sken return; 741229997Sken } 742229997Sken 743229997Sken /* 744269122Smav * If this is a write or a verify, we're all done. 745229997Sken * If this is a read, we can now send the data to the user. 746229997Sken */ 747269122Smav if ((beio->bio_cmd == BIO_WRITE) || 748269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 749229997Sken ctl_set_success(&io->scsiio); 750229997Sken ctl_complete_beio(beio); 751229997Sken } else { 752275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 753275058Smav beio->beio_cont == NULL) 754275058Smav ctl_set_success(&io->scsiio); 755229997Sken#ifdef CTL_TIME_IO 756229997Sken getbintime(&io->io_hdr.dma_start_bt); 757229997Sken#endif 758229997Sken ctl_datamove(io); 759229997Sken } 760229997Sken} 761229997Sken 762229997Skenstatic void 763275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 764275474Smav struct ctl_be_block_io *beio) 765275474Smav{ 766275474Smav union ctl_io *io = beio->io; 767275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 768275474Smav struct scsi_get_lba_status_data *data; 769275474Smav off_t roff, off; 770275474Smav int error, status; 771275474Smav 772275474Smav DPRINTF("entered\n"); 773275474Smav 774275474Smav off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 775275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 776275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 777275474Smav 0, curthread->td_ucred, curthread); 778275474Smav if (error == 0 && off > roff) 779275474Smav status = 0; /* mapped up to off */ 780275474Smav else { 781275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 782275474Smav 0, curthread->td_ucred, curthread); 783275474Smav if (error == 0 && off > roff) 784275474Smav status = 1; /* deallocated up to off */ 785275474Smav else { 786275474Smav status = 0; /* unknown up to the end */ 787275474Smav off = be_lun->size_bytes; 788275474Smav } 789275474Smav } 790275474Smav VOP_UNLOCK(be_lun->vn, 0); 791275474Smav 792275474Smav off >>= be_lun->blocksize_shift; 793275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 794275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 795275474Smav scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 796275474Smav data->descr[0].length); 797275474Smav data->descr[0].status = status; 798275474Smav 799275474Smav ctl_complete_beio(beio); 800275474Smav} 801275474Smav 802275474Smavstatic void 803269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 804269123Smav struct ctl_be_block_io *beio) 805269123Smav{ 806269123Smav struct ctl_be_block_devdata *dev_data; 807269123Smav union ctl_io *io; 808269123Smav struct uio xuio; 809269123Smav struct iovec *xiovec; 810269123Smav int flags; 811269123Smav int error, i; 812269123Smav 813269123Smav DPRINTF("entered\n"); 814269123Smav 815269123Smav dev_data = &be_lun->backend.dev; 816269123Smav io = beio->io; 817271309Smav flags = 0; 818271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 819271309Smav flags |= IO_DIRECT; 820271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 821271309Smav flags |= IO_SYNC; 822269123Smav 823269123Smav bzero(&xuio, sizeof(xuio)); 824269123Smav if (beio->bio_cmd == BIO_READ) { 825269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 826269123Smav xuio.uio_rw = UIO_READ; 827269123Smav } else { 828269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 829269123Smav xuio.uio_rw = UIO_WRITE; 830269123Smav } 831269123Smav xuio.uio_offset = beio->io_offset; 832269123Smav xuio.uio_resid = beio->io_len; 833269123Smav xuio.uio_segflg = UIO_SYSSPACE; 834269123Smav xuio.uio_iov = beio->xiovecs; 835269123Smav xuio.uio_iovcnt = beio->num_segs; 836269123Smav xuio.uio_td = curthread; 837269123Smav 838269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 839269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 840269123Smav xiovec->iov_len = beio->sg_segs[i].len; 841269123Smav } 842269123Smav 843269123Smav binuptime(&beio->ds_t0); 844269123Smav mtx_lock(&be_lun->io_lock); 845269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 846269123Smav mtx_unlock(&be_lun->io_lock); 847269123Smav 848269123Smav if (beio->bio_cmd == BIO_READ) { 849271309Smav error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 850269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 851269123Smav } else { 852271309Smav error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 853269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 854269123Smav } 855269123Smav 856269123Smav mtx_lock(&be_lun->io_lock); 857269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 858269123Smav beio->ds_tag_type, beio->ds_trans_type, 859269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 860269123Smav mtx_unlock(&be_lun->io_lock); 861269123Smav 862269123Smav /* 863269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 864269123Smav * return the I/O to the user. 865269123Smav */ 866269123Smav if (error != 0) { 867273809Smav if (error == ENOSPC) { 868273809Smav ctl_set_space_alloc_fail(&io->scsiio); 869273809Smav } else 870273809Smav ctl_set_medium_error(&io->scsiio); 871269123Smav ctl_complete_beio(beio); 872269123Smav return; 873269123Smav } 874269123Smav 875269123Smav /* 876269123Smav * If this is a write or a verify, we're all done. 877269123Smav * If this is a read, we can now send the data to the user. 878269123Smav */ 879269123Smav if ((beio->bio_cmd == BIO_WRITE) || 880269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 881269123Smav ctl_set_success(&io->scsiio); 882269123Smav ctl_complete_beio(beio); 883269123Smav } else { 884275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 885275058Smav beio->beio_cont == NULL) 886275058Smav ctl_set_success(&io->scsiio); 887269123Smav#ifdef CTL_TIME_IO 888269123Smav getbintime(&io->io_hdr.dma_start_bt); 889269123Smav#endif 890269123Smav ctl_datamove(io); 891269123Smav } 892269123Smav} 893269123Smav 894269123Smavstatic void 895275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 896275474Smav struct ctl_be_block_io *beio) 897275474Smav{ 898275474Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 899275474Smav union ctl_io *io = beio->io; 900275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 901275474Smav struct scsi_get_lba_status_data *data; 902275474Smav off_t roff, off; 903275474Smav int error, status; 904275474Smav 905275474Smav DPRINTF("entered\n"); 906275474Smav 907275474Smav off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 908275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE, 909275474Smav (caddr_t)&off, FREAD, curthread); 910275474Smav if (error == 0 && off > roff) 911275474Smav status = 0; /* mapped up to off */ 912275474Smav else { 913275474Smav error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA, 914275474Smav (caddr_t)&off, FREAD, curthread); 915275474Smav if (error == 0 && off > roff) 916275474Smav status = 1; /* deallocated up to off */ 917275474Smav else { 918275474Smav status = 0; /* unknown up to the end */ 919275474Smav off = be_lun->size_bytes; 920275474Smav } 921275474Smav } 922275474Smav 923275474Smav off >>= be_lun->blocksize_shift; 924275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 925275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 926275474Smav scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 927275474Smav data->descr[0].length); 928275474Smav data->descr[0].status = status; 929275474Smav 930275474Smav ctl_complete_beio(beio); 931275474Smav} 932275474Smav 933275474Smavstatic void 934229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 935229997Sken struct ctl_be_block_io *beio) 936229997Sken{ 937229997Sken struct bio *bio; 938229997Sken union ctl_io *io; 939229997Sken struct ctl_be_block_devdata *dev_data; 940229997Sken 941229997Sken dev_data = &be_lun->backend.dev; 942229997Sken io = beio->io; 943229997Sken 944229997Sken DPRINTF("entered\n"); 945229997Sken 946229997Sken /* This can't fail, it's a blocking allocation. */ 947229997Sken bio = g_alloc_bio(); 948229997Sken 949229997Sken bio->bio_cmd = BIO_FLUSH; 950229997Sken bio->bio_flags |= BIO_ORDERED; 951229997Sken bio->bio_dev = dev_data->cdev; 952229997Sken bio->bio_offset = 0; 953229997Sken bio->bio_data = 0; 954229997Sken bio->bio_done = ctl_be_block_biodone; 955229997Sken bio->bio_caller1 = beio; 956229997Sken bio->bio_pblkno = 0; 957229997Sken 958229997Sken /* 959229997Sken * We don't need to acquire the LUN lock here, because we are only 960229997Sken * sending one bio, and so there is no other context to synchronize 961229997Sken * with. 962229997Sken */ 963229997Sken beio->num_bios_sent = 1; 964229997Sken beio->send_complete = 1; 965229997Sken 966229997Sken binuptime(&beio->ds_t0); 967267877Smav mtx_lock(&be_lun->io_lock); 968229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 969267877Smav mtx_unlock(&be_lun->io_lock); 970229997Sken 971229997Sken (*dev_data->csw->d_strategy)(bio); 972229997Sken} 973229997Sken 974229997Skenstatic void 975264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 976264274Smav struct ctl_be_block_io *beio, 977264274Smav uint64_t off, uint64_t len, int last) 978264274Smav{ 979264274Smav struct bio *bio; 980264274Smav struct ctl_be_block_devdata *dev_data; 981264296Smav uint64_t maxlen; 982264274Smav 983264274Smav dev_data = &be_lun->backend.dev; 984264296Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 985264274Smav while (len > 0) { 986264274Smav bio = g_alloc_bio(); 987264274Smav bio->bio_cmd = BIO_DELETE; 988264274Smav bio->bio_dev = dev_data->cdev; 989264274Smav bio->bio_offset = off; 990264296Smav bio->bio_length = MIN(len, maxlen); 991264274Smav bio->bio_data = 0; 992264274Smav bio->bio_done = ctl_be_block_biodone; 993264274Smav bio->bio_caller1 = beio; 994264296Smav bio->bio_pblkno = off / be_lun->blocksize; 995264274Smav 996264274Smav off += bio->bio_length; 997264274Smav len -= bio->bio_length; 998264274Smav 999267877Smav mtx_lock(&be_lun->io_lock); 1000264274Smav beio->num_bios_sent++; 1001264274Smav if (last && len == 0) 1002264274Smav beio->send_complete = 1; 1003267877Smav mtx_unlock(&be_lun->io_lock); 1004264274Smav 1005264274Smav (*dev_data->csw->d_strategy)(bio); 1006264274Smav } 1007264274Smav} 1008264274Smav 1009264274Smavstatic void 1010264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1011264274Smav struct ctl_be_block_io *beio) 1012264274Smav{ 1013264274Smav union ctl_io *io; 1014264274Smav struct ctl_be_block_devdata *dev_data; 1015267515Smav struct ctl_ptr_len_flags *ptrlen; 1016264274Smav struct scsi_unmap_desc *buf, *end; 1017264274Smav uint64_t len; 1018264274Smav 1019264274Smav dev_data = &be_lun->backend.dev; 1020264274Smav io = beio->io; 1021264274Smav 1022264274Smav DPRINTF("entered\n"); 1023264274Smav 1024264274Smav binuptime(&beio->ds_t0); 1025267877Smav mtx_lock(&be_lun->io_lock); 1026264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1027267877Smav mtx_unlock(&be_lun->io_lock); 1028264274Smav 1029264274Smav if (beio->io_offset == -1) { 1030264274Smav beio->io_len = 0; 1031267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1032267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1033267515Smav end = buf + ptrlen->len / sizeof(*buf); 1034264274Smav for (; buf < end; buf++) { 1035264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1036264274Smav be_lun->blocksize; 1037264274Smav beio->io_len += len; 1038264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1039264274Smav scsi_8btou64(buf->lba) * be_lun->blocksize, len, 1040264283Smav (end - buf < 2) ? TRUE : FALSE); 1041264274Smav } 1042264274Smav } else 1043264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1044264274Smav beio->io_offset, beio->io_len, TRUE); 1045264274Smav} 1046264274Smav 1047264274Smavstatic void 1048229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1049229997Sken struct ctl_be_block_io *beio) 1050229997Sken{ 1051267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1052229997Sken int i; 1053229997Sken struct bio *bio; 1054229997Sken struct ctl_be_block_devdata *dev_data; 1055229997Sken off_t cur_offset; 1056229997Sken int max_iosize; 1057229997Sken 1058229997Sken DPRINTF("entered\n"); 1059229997Sken 1060229997Sken dev_data = &be_lun->backend.dev; 1061229997Sken 1062229997Sken /* 1063229997Sken * We have to limit our I/O size to the maximum supported by the 1064229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1065229997Sken * set it properly, use DFLTPHYS. 1066229997Sken */ 1067229997Sken max_iosize = dev_data->cdev->si_iosize_max; 1068229997Sken if (max_iosize < PAGE_SIZE) 1069229997Sken max_iosize = DFLTPHYS; 1070229997Sken 1071229997Sken cur_offset = beio->io_offset; 1072229997Sken for (i = 0; i < beio->num_segs; i++) { 1073229997Sken size_t cur_size; 1074229997Sken uint8_t *cur_ptr; 1075229997Sken 1076229997Sken cur_size = beio->sg_segs[i].len; 1077229997Sken cur_ptr = beio->sg_segs[i].addr; 1078229997Sken 1079229997Sken while (cur_size > 0) { 1080229997Sken /* This can't fail, it's a blocking allocation. */ 1081229997Sken bio = g_alloc_bio(); 1082229997Sken 1083229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1084229997Sken 1085229997Sken bio->bio_cmd = beio->bio_cmd; 1086229997Sken bio->bio_dev = dev_data->cdev; 1087229997Sken bio->bio_caller1 = beio; 1088229997Sken bio->bio_length = min(cur_size, max_iosize); 1089229997Sken bio->bio_offset = cur_offset; 1090229997Sken bio->bio_data = cur_ptr; 1091229997Sken bio->bio_done = ctl_be_block_biodone; 1092229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 1093229997Sken 1094229997Sken cur_offset += bio->bio_length; 1095229997Sken cur_ptr += bio->bio_length; 1096229997Sken cur_size -= bio->bio_length; 1097229997Sken 1098267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1099229997Sken beio->num_bios_sent++; 1100229997Sken } 1101229997Sken } 1102267877Smav binuptime(&beio->ds_t0); 1103267877Smav mtx_lock(&be_lun->io_lock); 1104267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1105267877Smav beio->send_complete = 1; 1106267877Smav mtx_unlock(&be_lun->io_lock); 1107267877Smav 1108267877Smav /* 1109267877Smav * Fire off all allocated requests! 1110267877Smav */ 1111267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1112267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1113267877Smav (*dev_data->csw->d_strategy)(bio); 1114267877Smav } 1115229997Sken} 1116229997Sken 1117274154Smavstatic uint64_t 1118274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1119274154Smav{ 1120274154Smav struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1121274154Smav struct diocgattr_arg arg; 1122274154Smav int error; 1123274154Smav 1124274154Smav if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1125274154Smav return (UINT64_MAX); 1126274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1127274154Smav arg.len = sizeof(arg.value.off); 1128274154Smav error = dev_data->csw->d_ioctl(dev_data->cdev, 1129274154Smav DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1130274154Smav if (error != 0) 1131274154Smav return (UINT64_MAX); 1132274154Smav return (arg.value.off); 1133274154Smav} 1134274154Smav 1135229997Skenstatic void 1136264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1137264274Smav{ 1138264274Smav union ctl_io *io; 1139264274Smav 1140264274Smav io = beio->io; 1141264274Smav ctl_free_beio(beio); 1142267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1143267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1144267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1145264274Smav ctl_config_write_done(io); 1146264274Smav return; 1147264274Smav } 1148264274Smav 1149264274Smav ctl_be_block_config_write(io); 1150264274Smav} 1151264274Smav 1152264274Smavstatic void 1153264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1154264274Smav union ctl_io *io) 1155264274Smav{ 1156264274Smav struct ctl_be_block_io *beio; 1157264274Smav struct ctl_be_block_softc *softc; 1158267515Smav struct ctl_lba_len_flags *lbalen; 1159264274Smav uint64_t len_left, lba; 1160264274Smav int i, seglen; 1161264274Smav uint8_t *buf, *end; 1162264274Smav 1163264274Smav DPRINTF("entered\n"); 1164264274Smav 1165267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1166264274Smav softc = be_lun->softc; 1167267537Smav lbalen = ARGS(beio->io); 1168264274Smav 1169271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1170269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1171264274Smav ctl_free_beio(beio); 1172264274Smav ctl_set_invalid_field(&io->scsiio, 1173264274Smav /*sks_valid*/ 1, 1174264274Smav /*command*/ 1, 1175264274Smav /*field*/ 1, 1176264274Smav /*bit_valid*/ 0, 1177264274Smav /*bit*/ 0); 1178264274Smav ctl_config_write_done(io); 1179264274Smav return; 1180264274Smav } 1181264274Smav 1182264274Smav switch (io->scsiio.tag_type) { 1183264274Smav case CTL_TAG_ORDERED: 1184264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1185264274Smav break; 1186264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1187264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1188264274Smav break; 1189264274Smav case CTL_TAG_UNTAGGED: 1190264274Smav case CTL_TAG_SIMPLE: 1191264274Smav case CTL_TAG_ACA: 1192264274Smav default: 1193264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1194264274Smav break; 1195264274Smav } 1196264274Smav 1197269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1198267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1199267515Smav beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1200264274Smav beio->bio_cmd = BIO_DELETE; 1201264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1202264274Smav 1203264274Smav be_lun->unmap(be_lun, beio); 1204264274Smav return; 1205264274Smav } 1206264274Smav 1207264274Smav beio->bio_cmd = BIO_WRITE; 1208264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1209264274Smav 1210264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1211267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1212264274Smav 1213267515Smav len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1214264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1215264274Smav 1216264274Smav /* 1217264274Smav * Setup the S/G entry for this chunk. 1218264274Smav */ 1219264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1220264274Smav seglen -= seglen % be_lun->blocksize; 1221264274Smav beio->sg_segs[i].len = seglen; 1222264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1223264274Smav 1224264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1225264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1226264274Smav 1227264274Smav beio->num_segs++; 1228264274Smav len_left -= seglen; 1229264274Smav 1230264274Smav buf = beio->sg_segs[i].addr; 1231264274Smav end = buf + seglen; 1232264274Smav for (; buf < end; buf += be_lun->blocksize) { 1233264274Smav memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1234267515Smav if (lbalen->flags & SWS_LBDATA) 1235267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1236264274Smav lba++; 1237264274Smav } 1238264274Smav } 1239264274Smav 1240267515Smav beio->io_offset = lbalen->lba * be_lun->blocksize; 1241264274Smav beio->io_len = lba * be_lun->blocksize; 1242264274Smav 1243264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1244264274Smav if (len_left > 0) { 1245267515Smav lbalen->lba += lba; 1246267515Smav lbalen->len -= lba; 1247264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1248264274Smav } 1249264274Smav 1250264274Smav be_lun->dispatch(be_lun, beio); 1251264274Smav} 1252264274Smav 1253264274Smavstatic void 1254264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1255264274Smav union ctl_io *io) 1256264274Smav{ 1257264274Smav struct ctl_be_block_io *beio; 1258264274Smav struct ctl_be_block_softc *softc; 1259267515Smav struct ctl_ptr_len_flags *ptrlen; 1260264274Smav 1261264274Smav DPRINTF("entered\n"); 1262264274Smav 1263267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1264264274Smav softc = be_lun->softc; 1265267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1266264274Smav 1267269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1268264274Smav ctl_free_beio(beio); 1269264274Smav ctl_set_invalid_field(&io->scsiio, 1270264274Smav /*sks_valid*/ 0, 1271264274Smav /*command*/ 1, 1272264274Smav /*field*/ 0, 1273264274Smav /*bit_valid*/ 0, 1274264274Smav /*bit*/ 0); 1275264274Smav ctl_config_write_done(io); 1276264274Smav return; 1277264274Smav } 1278264274Smav 1279264274Smav switch (io->scsiio.tag_type) { 1280264274Smav case CTL_TAG_ORDERED: 1281264274Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1282264274Smav break; 1283264274Smav case CTL_TAG_HEAD_OF_QUEUE: 1284264274Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1285264274Smav break; 1286264274Smav case CTL_TAG_UNTAGGED: 1287264274Smav case CTL_TAG_SIMPLE: 1288264274Smav case CTL_TAG_ACA: 1289264274Smav default: 1290264274Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1291264274Smav break; 1292264274Smav } 1293264274Smav 1294264274Smav beio->io_len = 0; 1295264274Smav beio->io_offset = -1; 1296264274Smav 1297264274Smav beio->bio_cmd = BIO_DELETE; 1298264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1299264274Smav 1300267515Smav DPRINTF("UNMAP\n"); 1301264274Smav 1302264274Smav be_lun->unmap(be_lun, beio); 1303264274Smav} 1304264274Smav 1305264274Smavstatic void 1306275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1307275474Smav{ 1308275474Smav union ctl_io *io; 1309275474Smav 1310275474Smav io = beio->io; 1311275474Smav ctl_free_beio(beio); 1312275474Smav ctl_config_read_done(io); 1313275474Smav} 1314275474Smav 1315275474Smavstatic void 1316275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1317275474Smav union ctl_io *io) 1318275474Smav{ 1319275474Smav struct ctl_be_block_io *beio; 1320275474Smav struct ctl_be_block_softc *softc; 1321275474Smav 1322275474Smav DPRINTF("entered\n"); 1323275474Smav 1324275474Smav softc = be_lun->softc; 1325275474Smav beio = ctl_alloc_beio(softc); 1326275474Smav beio->io = io; 1327275474Smav beio->lun = be_lun; 1328275474Smav beio->beio_cont = ctl_be_block_cr_done; 1329275474Smav PRIV(io)->ptr = (void *)beio; 1330275474Smav 1331275474Smav switch (io->scsiio.cdb[0]) { 1332275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1333275474Smav beio->bio_cmd = -1; 1334275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1335275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1336275474Smav beio->io_len = 0; 1337275474Smav if (be_lun->get_lba_status) 1338275474Smav be_lun->get_lba_status(be_lun, beio); 1339275474Smav else 1340275474Smav ctl_be_block_cr_done(beio); 1341275474Smav break; 1342275474Smav default: 1343275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1344275474Smav break; 1345275474Smav } 1346275474Smav} 1347275474Smav 1348275474Smavstatic void 1349264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1350264274Smav{ 1351264274Smav union ctl_io *io; 1352264274Smav 1353264274Smav io = beio->io; 1354264274Smav ctl_free_beio(beio); 1355264274Smav ctl_config_write_done(io); 1356264274Smav} 1357264274Smav 1358264274Smavstatic void 1359229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1360229997Sken union ctl_io *io) 1361229997Sken{ 1362229997Sken struct ctl_be_block_io *beio; 1363229997Sken struct ctl_be_block_softc *softc; 1364229997Sken 1365229997Sken DPRINTF("entered\n"); 1366229997Sken 1367229997Sken softc = be_lun->softc; 1368229997Sken beio = ctl_alloc_beio(softc); 1369229997Sken beio->io = io; 1370229997Sken beio->lun = be_lun; 1371264274Smav beio->beio_cont = ctl_be_block_cw_done; 1372267519Smav PRIV(io)->ptr = (void *)beio; 1373229997Sken 1374229997Sken switch (io->scsiio.cdb[0]) { 1375229997Sken case SYNCHRONIZE_CACHE: 1376229997Sken case SYNCHRONIZE_CACHE_16: 1377249194Strasz beio->bio_cmd = BIO_FLUSH; 1378229997Sken beio->ds_trans_type = DEVSTAT_NO_DATA; 1379229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1380229997Sken beio->io_len = 0; 1381229997Sken be_lun->lun_flush(be_lun, beio); 1382229997Sken break; 1383264274Smav case WRITE_SAME_10: 1384264274Smav case WRITE_SAME_16: 1385264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1386264274Smav break; 1387264274Smav case UNMAP: 1388264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1389264274Smav break; 1390229997Sken default: 1391229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1392229997Sken break; 1393229997Sken } 1394229997Sken} 1395229997Sken 1396258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1397258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1398258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1399258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1400229997Sken 1401229997Skenstatic void 1402264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1403264886Smav{ 1404264886Smav struct ctl_be_block_lun *be_lun; 1405264886Smav union ctl_io *io; 1406264886Smav 1407264886Smav io = beio->io; 1408264886Smav be_lun = beio->lun; 1409264886Smav ctl_free_beio(beio); 1410267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1411267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1412267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1413267537Smav ctl_data_submit_done(io); 1414264886Smav return; 1415264886Smav } 1416264886Smav 1417264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1418264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1419264886Smav 1420267877Smav mtx_lock(&be_lun->queue_lock); 1421264886Smav /* 1422264886Smav * XXX KDM make sure that links is okay to use at this point. 1423264886Smav * Otherwise, we either need to add another field to ctl_io_hdr, 1424264886Smav * or deal with resource allocation here. 1425264886Smav */ 1426264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1427267877Smav mtx_unlock(&be_lun->queue_lock); 1428264886Smav 1429264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1430264886Smav} 1431264886Smav 1432264886Smavstatic void 1433229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1434229997Sken union ctl_io *io) 1435229997Sken{ 1436229997Sken struct ctl_be_block_io *beio; 1437229997Sken struct ctl_be_block_softc *softc; 1438267537Smav struct ctl_lba_len_flags *lbalen; 1439267519Smav struct ctl_ptr_len_flags *bptrlen; 1440267519Smav uint64_t len_left, lbas; 1441229997Sken int i; 1442229997Sken 1443229997Sken softc = be_lun->softc; 1444229997Sken 1445229997Sken DPRINTF("entered\n"); 1446229997Sken 1447267537Smav lbalen = ARGS(io); 1448267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1449267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1450267537Smav } else { 1451229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1452229997Sken } 1453229997Sken 1454229997Sken beio = ctl_alloc_beio(softc); 1455229997Sken beio->io = io; 1456229997Sken beio->lun = be_lun; 1457267519Smav bptrlen = PRIV(io); 1458267519Smav bptrlen->ptr = (void *)beio; 1459229997Sken 1460229997Sken switch (io->scsiio.tag_type) { 1461229997Sken case CTL_TAG_ORDERED: 1462229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1463229997Sken break; 1464229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1465229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1466229997Sken break; 1467229997Sken case CTL_TAG_UNTAGGED: 1468229997Sken case CTL_TAG_SIMPLE: 1469229997Sken case CTL_TAG_ACA: 1470229997Sken default: 1471229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1472229997Sken break; 1473229997Sken } 1474229997Sken 1475267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1476267537Smav beio->bio_cmd = BIO_WRITE; 1477267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1478267537Smav } else { 1479229997Sken beio->bio_cmd = BIO_READ; 1480229997Sken beio->ds_trans_type = DEVSTAT_READ; 1481229997Sken } 1482229997Sken 1483264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1484229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1485267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1486267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1487267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1488267537Smav else 1489267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1490267537Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1491267519Smav beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1492267519Smav beio->io_len = lbas * be_lun->blocksize; 1493267519Smav bptrlen->len += lbas; 1494229997Sken 1495264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1496264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1497264886Smav i, CTLBLK_MAX_SEGS)); 1498229997Sken 1499229997Sken /* 1500229997Sken * Setup the S/G entry for this chunk. 1501229997Sken */ 1502264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1503229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1504229997Sken 1505229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1506229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1507229997Sken 1508267537Smav /* Set up second segment for compare operation. */ 1509267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1510267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1511267537Smav beio->sg_segs[i].len; 1512267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1513267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1514267537Smav } 1515267537Smav 1516229997Sken beio->num_segs++; 1517229997Sken len_left -= beio->sg_segs[i].len; 1518229997Sken } 1519267519Smav if (bptrlen->len < lbalen->len) 1520264886Smav beio->beio_cont = ctl_be_block_next; 1521264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1522267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1523267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1524267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1525267537Smav else 1526267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1527264886Smav io->scsiio.kern_data_len = beio->io_len; 1528264886Smav io->scsiio.kern_data_resid = 0; 1529264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1530264886Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1531229997Sken 1532229997Sken /* 1533229997Sken * For the read case, we need to read the data into our buffers and 1534229997Sken * then we can send it back to the user. For the write case, we 1535229997Sken * need to get the data from the user first. 1536229997Sken */ 1537229997Sken if (beio->bio_cmd == BIO_READ) { 1538229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1539229997Sken be_lun->dispatch(be_lun, beio); 1540229997Sken } else { 1541229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1542229997Sken#ifdef CTL_TIME_IO 1543229997Sken getbintime(&io->io_hdr.dma_start_bt); 1544229997Sken#endif 1545229997Sken ctl_datamove(io); 1546229997Sken } 1547229997Sken} 1548229997Sken 1549229997Skenstatic void 1550229997Skenctl_be_block_worker(void *context, int pending) 1551229997Sken{ 1552229997Sken struct ctl_be_block_lun *be_lun; 1553229997Sken struct ctl_be_block_softc *softc; 1554229997Sken union ctl_io *io; 1555229997Sken 1556229997Sken be_lun = (struct ctl_be_block_lun *)context; 1557229997Sken softc = be_lun->softc; 1558229997Sken 1559229997Sken DPRINTF("entered\n"); 1560229997Sken 1561267877Smav mtx_lock(&be_lun->queue_lock); 1562229997Sken for (;;) { 1563229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1564229997Sken if (io != NULL) { 1565229997Sken struct ctl_be_block_io *beio; 1566229997Sken 1567229997Sken DPRINTF("datamove queue\n"); 1568229997Sken 1569229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1570229997Sken ctl_io_hdr, links); 1571229997Sken 1572267877Smav mtx_unlock(&be_lun->queue_lock); 1573229997Sken 1574267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1575229997Sken 1576229997Sken be_lun->dispatch(be_lun, beio); 1577229997Sken 1578267877Smav mtx_lock(&be_lun->queue_lock); 1579229997Sken continue; 1580229997Sken } 1581229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1582229997Sken if (io != NULL) { 1583229997Sken DPRINTF("config write queue\n"); 1584229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1585229997Sken ctl_io_hdr, links); 1586267877Smav mtx_unlock(&be_lun->queue_lock); 1587229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1588267877Smav mtx_lock(&be_lun->queue_lock); 1589229997Sken continue; 1590229997Sken } 1591275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1592275474Smav if (io != NULL) { 1593275474Smav DPRINTF("config read queue\n"); 1594275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1595275474Smav ctl_io_hdr, links); 1596275474Smav mtx_unlock(&be_lun->queue_lock); 1597275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1598275474Smav mtx_lock(&be_lun->queue_lock); 1599275474Smav continue; 1600275474Smav } 1601229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1602229997Sken if (io != NULL) { 1603229997Sken DPRINTF("input queue\n"); 1604229997Sken 1605229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1606229997Sken ctl_io_hdr, links); 1607267877Smav mtx_unlock(&be_lun->queue_lock); 1608229997Sken 1609229997Sken /* 1610229997Sken * We must drop the lock, since this routine and 1611229997Sken * its children may sleep. 1612229997Sken */ 1613229997Sken ctl_be_block_dispatch(be_lun, io); 1614229997Sken 1615267877Smav mtx_lock(&be_lun->queue_lock); 1616229997Sken continue; 1617229997Sken } 1618229997Sken 1619229997Sken /* 1620229997Sken * If we get here, there is no work left in the queues, so 1621229997Sken * just break out and let the task queue go to sleep. 1622229997Sken */ 1623229997Sken break; 1624229997Sken } 1625267877Smav mtx_unlock(&be_lun->queue_lock); 1626229997Sken} 1627229997Sken 1628229997Sken/* 1629229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1630229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1631229997Sken * thread. 1632229997Sken */ 1633229997Skenstatic int 1634229997Skenctl_be_block_submit(union ctl_io *io) 1635229997Sken{ 1636229997Sken struct ctl_be_block_lun *be_lun; 1637229997Sken struct ctl_be_lun *ctl_be_lun; 1638229997Sken 1639229997Sken DPRINTF("entered\n"); 1640229997Sken 1641229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1642229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1643229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1644229997Sken 1645229997Sken /* 1646229997Sken * Make sure we only get SCSI I/O. 1647229997Sken */ 1648229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1649229997Sken "%#x) encountered", io->io_hdr.io_type)); 1650229997Sken 1651267519Smav PRIV(io)->len = 0; 1652267519Smav 1653267877Smav mtx_lock(&be_lun->queue_lock); 1654229997Sken /* 1655229997Sken * XXX KDM make sure that links is okay to use at this point. 1656229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1657229997Sken * or deal with resource allocation here. 1658229997Sken */ 1659229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1660267877Smav mtx_unlock(&be_lun->queue_lock); 1661229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1662229997Sken 1663267514Smav return (CTL_RETVAL_COMPLETE); 1664229997Sken} 1665229997Sken 1666229997Skenstatic int 1667229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1668229997Sken int flag, struct thread *td) 1669229997Sken{ 1670229997Sken struct ctl_be_block_softc *softc; 1671229997Sken int error; 1672229997Sken 1673229997Sken softc = &backend_block_softc; 1674229997Sken 1675229997Sken error = 0; 1676229997Sken 1677229997Sken switch (cmd) { 1678229997Sken case CTL_LUN_REQ: { 1679229997Sken struct ctl_lun_req *lun_req; 1680229997Sken 1681229997Sken lun_req = (struct ctl_lun_req *)addr; 1682229997Sken 1683229997Sken switch (lun_req->reqtype) { 1684229997Sken case CTL_LUNREQ_CREATE: 1685229997Sken error = ctl_be_block_create(softc, lun_req); 1686229997Sken break; 1687229997Sken case CTL_LUNREQ_RM: 1688229997Sken error = ctl_be_block_rm(softc, lun_req); 1689229997Sken break; 1690232604Strasz case CTL_LUNREQ_MODIFY: 1691232604Strasz error = ctl_be_block_modify(softc, lun_req); 1692232604Strasz break; 1693229997Sken default: 1694229997Sken lun_req->status = CTL_LUN_ERROR; 1695229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1696272911Smav "invalid LUN request type %d", 1697229997Sken lun_req->reqtype); 1698229997Sken break; 1699229997Sken } 1700229997Sken break; 1701229997Sken } 1702229997Sken default: 1703229997Sken error = ENOTTY; 1704229997Sken break; 1705229997Sken } 1706229997Sken 1707229997Sken return (error); 1708229997Sken} 1709229997Sken 1710229997Skenstatic int 1711229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1712229997Sken{ 1713229997Sken struct ctl_be_block_filedata *file_data; 1714229997Sken struct ctl_lun_create_params *params; 1715229997Sken struct vattr vattr; 1716273029Smav off_t pss; 1717229997Sken int error; 1718229997Sken 1719229997Sken error = 0; 1720229997Sken file_data = &be_lun->backend.file; 1721272911Smav params = &be_lun->params; 1722229997Sken 1723229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1724229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1725229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1726275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1727229997Sken 1728229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1729229997Sken if (error != 0) { 1730229997Sken snprintf(req->error_str, sizeof(req->error_str), 1731229997Sken "error calling VOP_GETATTR() for file %s", 1732229997Sken be_lun->dev_path); 1733229997Sken return (error); 1734229997Sken } 1735229997Sken 1736229997Sken /* 1737229997Sken * Verify that we have the ability to upgrade to exclusive 1738229997Sken * access on this file so we can trap errors at open instead 1739229997Sken * of reporting them during first access. 1740229997Sken */ 1741229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1742229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1743229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1744229997Sken error = EBADF; 1745229997Sken snprintf(req->error_str, sizeof(req->error_str), 1746229997Sken "error locking file %s", be_lun->dev_path); 1747229997Sken return (error); 1748229997Sken } 1749229997Sken } 1750229997Sken 1751229997Sken 1752229997Sken file_data->cred = crhold(curthread->td_ucred); 1753232604Strasz if (params->lun_size_bytes != 0) 1754232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1755232604Strasz else 1756232604Strasz be_lun->size_bytes = vattr.va_size; 1757229997Sken /* 1758229997Sken * We set the multi thread flag for file operations because all 1759229997Sken * filesystems (in theory) are capable of allowing multiple readers 1760229997Sken * of a file at once. So we want to get the maximum possible 1761229997Sken * concurrency. 1762229997Sken */ 1763229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1764229997Sken 1765229997Sken /* 1766273029Smav * For files we can use any logical block size. Prefer 512 bytes 1767273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1768273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1769273029Smav * logical block size -- report it as physical block size. 1770229997Sken */ 1771229997Sken if (params->blocksize_bytes != 0) 1772229997Sken be_lun->blocksize = params->blocksize_bytes; 1773229997Sken else 1774229997Sken be_lun->blocksize = 512; 1775273029Smav pss = vattr.va_blocksize / be_lun->blocksize; 1776273029Smav if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) && 1777273029Smav ((pss & (pss - 1)) == 0)) { 1778273029Smav be_lun->pblockexp = fls(pss) - 1; 1779273029Smav be_lun->pblockoff = 0; 1780273029Smav } 1781229997Sken 1782229997Sken /* 1783229997Sken * Sanity check. The media size has to be at least one 1784229997Sken * sector long. 1785229997Sken */ 1786229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1787229997Sken error = EINVAL; 1788229997Sken snprintf(req->error_str, sizeof(req->error_str), 1789229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1790229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1791229997Sken } 1792229997Sken return (error); 1793229997Sken} 1794229997Sken 1795229997Skenstatic int 1796229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1797229997Sken{ 1798229997Sken struct ctl_lun_create_params *params; 1799229997Sken struct vattr vattr; 1800229997Sken struct cdev *dev; 1801229997Sken struct cdevsw *devsw; 1802229997Sken int error; 1803264191Smav off_t ps, pss, po, pos; 1804229997Sken 1805272911Smav params = &be_lun->params; 1806229997Sken 1807229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1808229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1809229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1810229997Sken &be_lun->backend.dev.dev_ref); 1811229997Sken if (be_lun->backend.dev.csw == NULL) 1812229997Sken panic("Unable to retrieve device switch"); 1813275474Smav if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) { 1814269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1815275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1816275474Smav } else 1817269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1818269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1819269123Smav be_lun->unmap = ctl_be_block_unmap_dev; 1820274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1821229997Sken 1822229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1823229997Sken if (error) { 1824229997Sken snprintf(req->error_str, sizeof(req->error_str), 1825272911Smav "error getting vnode attributes for device %s", 1826272911Smav be_lun->dev_path); 1827229997Sken return (error); 1828229997Sken } 1829229997Sken 1830229997Sken dev = be_lun->vn->v_rdev; 1831229997Sken devsw = dev->si_devsw; 1832229997Sken if (!devsw->d_ioctl) { 1833229997Sken snprintf(req->error_str, sizeof(req->error_str), 1834272911Smav "no d_ioctl for device %s!", 1835229997Sken be_lun->dev_path); 1836229997Sken return (ENODEV); 1837229997Sken } 1838229997Sken 1839229997Sken error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1840229997Sken (caddr_t)&be_lun->blocksize, FREAD, 1841229997Sken curthread); 1842229997Sken if (error) { 1843229997Sken snprintf(req->error_str, sizeof(req->error_str), 1844272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1845272911Smav "on %s!", error, be_lun->dev_path); 1846229997Sken return (error); 1847229997Sken } 1848229997Sken 1849229997Sken /* 1850229997Sken * If the user has asked for a blocksize that is greater than the 1851229997Sken * backing device's blocksize, we can do it only if the blocksize 1852229997Sken * the user is asking for is an even multiple of the underlying 1853229997Sken * device's blocksize. 1854229997Sken */ 1855229997Sken if ((params->blocksize_bytes != 0) 1856229997Sken && (params->blocksize_bytes > be_lun->blocksize)) { 1857229997Sken uint32_t bs_multiple, tmp_blocksize; 1858229997Sken 1859229997Sken bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1860229997Sken 1861229997Sken tmp_blocksize = bs_multiple * be_lun->blocksize; 1862229997Sken 1863229997Sken if (tmp_blocksize == params->blocksize_bytes) { 1864229997Sken be_lun->blocksize = params->blocksize_bytes; 1865229997Sken } else { 1866229997Sken snprintf(req->error_str, sizeof(req->error_str), 1867272911Smav "requested blocksize %u is not an even " 1868229997Sken "multiple of backing device blocksize %u", 1869272911Smav params->blocksize_bytes, 1870229997Sken be_lun->blocksize); 1871229997Sken return (EINVAL); 1872229997Sken 1873229997Sken } 1874229997Sken } else if ((params->blocksize_bytes != 0) 1875229997Sken && (params->blocksize_bytes != be_lun->blocksize)) { 1876229997Sken snprintf(req->error_str, sizeof(req->error_str), 1877272911Smav "requested blocksize %u < backing device " 1878272911Smav "blocksize %u", params->blocksize_bytes, 1879229997Sken be_lun->blocksize); 1880229997Sken return (EINVAL); 1881229997Sken } 1882229997Sken 1883229997Sken error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1884229997Sken (caddr_t)&be_lun->size_bytes, FREAD, 1885229997Sken curthread); 1886229997Sken if (error) { 1887229997Sken snprintf(req->error_str, sizeof(req->error_str), 1888272911Smav "error %d returned for DIOCGMEDIASIZE " 1889272911Smav " ioctl on %s!", error, 1890232604Strasz be_lun->dev_path); 1891229997Sken return (error); 1892229997Sken } 1893229997Sken 1894232604Strasz if (params->lun_size_bytes != 0) { 1895232604Strasz if (params->lun_size_bytes > be_lun->size_bytes) { 1896232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1897272911Smav "requested LUN size %ju > backing device " 1898272911Smav "size %ju", 1899232604Strasz (uintmax_t)params->lun_size_bytes, 1900232604Strasz (uintmax_t)be_lun->size_bytes); 1901232604Strasz return (EINVAL); 1902232604Strasz } 1903232604Strasz 1904232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1905232604Strasz } 1906232604Strasz 1907264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1908264191Smav (caddr_t)&ps, FREAD, curthread); 1909264191Smav if (error) 1910264191Smav ps = po = 0; 1911264191Smav else { 1912264191Smav error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1913264191Smav (caddr_t)&po, FREAD, curthread); 1914264191Smav if (error) 1915264191Smav po = 0; 1916264191Smav } 1917264191Smav pss = ps / be_lun->blocksize; 1918264191Smav pos = po / be_lun->blocksize; 1919264191Smav if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1920264191Smav ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1921264191Smav be_lun->pblockexp = fls(pss) - 1; 1922264191Smav be_lun->pblockoff = (pss - pos) % pss; 1923264191Smav } 1924264191Smav 1925229997Sken return (0); 1926229997Sken} 1927229997Sken 1928229997Skenstatic int 1929229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 1930229997Sken{ 1931229997Sken DROP_GIANT(); 1932229997Sken if (be_lun->vn) { 1933229997Sken int flags = FREAD | FWRITE; 1934229997Sken 1935229997Sken switch (be_lun->dev_type) { 1936229997Sken case CTL_BE_BLOCK_DEV: 1937229997Sken if (be_lun->backend.dev.csw) { 1938229997Sken dev_relthread(be_lun->backend.dev.cdev, 1939229997Sken be_lun->backend.dev.dev_ref); 1940229997Sken be_lun->backend.dev.csw = NULL; 1941229997Sken be_lun->backend.dev.cdev = NULL; 1942229997Sken } 1943229997Sken break; 1944229997Sken case CTL_BE_BLOCK_FILE: 1945229997Sken break; 1946229997Sken case CTL_BE_BLOCK_NONE: 1947258871Strasz break; 1948229997Sken default: 1949229997Sken panic("Unexpected backend type."); 1950229997Sken break; 1951229997Sken } 1952229997Sken 1953229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1954229997Sken be_lun->vn = NULL; 1955229997Sken 1956229997Sken switch (be_lun->dev_type) { 1957229997Sken case CTL_BE_BLOCK_DEV: 1958229997Sken break; 1959229997Sken case CTL_BE_BLOCK_FILE: 1960229997Sken if (be_lun->backend.file.cred != NULL) { 1961229997Sken crfree(be_lun->backend.file.cred); 1962229997Sken be_lun->backend.file.cred = NULL; 1963229997Sken } 1964229997Sken break; 1965229997Sken case CTL_BE_BLOCK_NONE: 1966258871Strasz break; 1967229997Sken default: 1968229997Sken panic("Unexpected backend type."); 1969229997Sken break; 1970229997Sken } 1971272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 1972229997Sken } 1973229997Sken PICKUP_GIANT(); 1974229997Sken 1975229997Sken return (0); 1976229997Sken} 1977229997Sken 1978229997Skenstatic int 1979229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 1980229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1981229997Sken{ 1982229997Sken struct nameidata nd; 1983229997Sken int flags; 1984229997Sken int error; 1985229997Sken 1986229997Sken /* 1987229997Sken * XXX KDM allow a read-only option? 1988229997Sken */ 1989229997Sken flags = FREAD | FWRITE; 1990229997Sken error = 0; 1991229997Sken 1992229997Sken if (rootvnode == NULL) { 1993229997Sken snprintf(req->error_str, sizeof(req->error_str), 1994272911Smav "Root filesystem is not mounted"); 1995229997Sken return (1); 1996229997Sken } 1997229997Sken 1998229997Sken if (!curthread->td_proc->p_fd->fd_cdir) { 1999229997Sken curthread->td_proc->p_fd->fd_cdir = rootvnode; 2000229997Sken VREF(rootvnode); 2001229997Sken } 2002229997Sken if (!curthread->td_proc->p_fd->fd_rdir) { 2003229997Sken curthread->td_proc->p_fd->fd_rdir = rootvnode; 2004229997Sken VREF(rootvnode); 2005229997Sken } 2006229997Sken if (!curthread->td_proc->p_fd->fd_jdir) { 2007229997Sken curthread->td_proc->p_fd->fd_jdir = rootvnode; 2008229997Sken VREF(rootvnode); 2009229997Sken } 2010229997Sken 2011229997Sken again: 2012229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2013229997Sken error = vn_open(&nd, &flags, 0, NULL); 2014229997Sken if (error) { 2015229997Sken /* 2016229997Sken * This is the only reasonable guess we can make as far as 2017229997Sken * path if the user doesn't give us a fully qualified path. 2018229997Sken * If they want to specify a file, they need to specify the 2019229997Sken * full path. 2020229997Sken */ 2021229997Sken if (be_lun->dev_path[0] != '/') { 2022229997Sken char *dev_path = "/dev/"; 2023229997Sken char *dev_name; 2024229997Sken 2025229997Sken /* Try adding device path at beginning of name */ 2026229997Sken dev_name = malloc(strlen(be_lun->dev_path) 2027229997Sken + strlen(dev_path) + 1, 2028229997Sken M_CTLBLK, M_WAITOK); 2029229997Sken if (dev_name) { 2030229997Sken sprintf(dev_name, "%s%s", dev_path, 2031229997Sken be_lun->dev_path); 2032229997Sken free(be_lun->dev_path, M_CTLBLK); 2033229997Sken be_lun->dev_path = dev_name; 2034229997Sken goto again; 2035229997Sken } 2036229997Sken } 2037229997Sken snprintf(req->error_str, sizeof(req->error_str), 2038272911Smav "error opening %s: %d", be_lun->dev_path, error); 2039229997Sken return (error); 2040229997Sken } 2041229997Sken 2042229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2043229997Sken 2044229997Sken be_lun->vn = nd.ni_vp; 2045229997Sken 2046229997Sken /* We only support disks and files. */ 2047229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2048229997Sken error = ctl_be_block_open_dev(be_lun, req); 2049229997Sken } else if (be_lun->vn->v_type == VREG) { 2050229997Sken error = ctl_be_block_open_file(be_lun, req); 2051229997Sken } else { 2052229997Sken error = EINVAL; 2053229997Sken snprintf(req->error_str, sizeof(req->error_str), 2054258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2055229997Sken } 2056229997Sken VOP_UNLOCK(be_lun->vn, 0); 2057229997Sken 2058229997Sken if (error != 0) { 2059229997Sken ctl_be_block_close(be_lun); 2060229997Sken return (error); 2061229997Sken } 2062229997Sken 2063229997Sken be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2064229997Sken be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2065229997Sken 2066229997Sken return (0); 2067229997Sken} 2068229997Sken 2069229997Skenstatic int 2070229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2071229997Sken{ 2072229997Sken struct ctl_be_block_lun *be_lun; 2073229997Sken struct ctl_lun_create_params *params; 2074267481Smav char num_thread_str[16]; 2075229997Sken char tmpstr[32]; 2076267481Smav char *value; 2077264274Smav int retval, num_threads, unmap; 2078267481Smav int tmp_num_threads; 2079229997Sken 2080229997Sken params = &req->reqdata.create; 2081229997Sken retval = 0; 2082272911Smav req->status = CTL_LUN_OK; 2083229997Sken 2084229997Sken num_threads = cbb_num_threads; 2085229997Sken 2086229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2087229997Sken 2088272911Smav be_lun->params = req->reqdata.create; 2089229997Sken be_lun->softc = softc; 2090229997Sken STAILQ_INIT(&be_lun->input_queue); 2091275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2092229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2093229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2094229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2095267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2096267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2097268280Smav ctl_init_opts(&be_lun->ctl_be_lun.options, 2098268280Smav req->num_be_args, req->kern_be_args); 2099229997Sken 2100264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2101256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2102229997Sken 2103229997Sken if (be_lun->lun_zone == NULL) { 2104229997Sken snprintf(req->error_str, sizeof(req->error_str), 2105272911Smav "error allocating UMA zone"); 2106229997Sken goto bailout_error; 2107229997Sken } 2108229997Sken 2109229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2110229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 2111229997Sken else 2112229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 2113229997Sken 2114229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 2115268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 2116267499Smav if (value == NULL) { 2117229997Sken snprintf(req->error_str, sizeof(req->error_str), 2118272911Smav "no file argument specified"); 2119229997Sken goto bailout_error; 2120229997Sken } 2121267499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2122272911Smav be_lun->blocksize = 512; 2123272911Smav be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2124229997Sken 2125229997Sken retval = ctl_be_block_open(softc, be_lun, req); 2126229997Sken if (retval != 0) { 2127229997Sken retval = 0; 2128272911Smav req->status = CTL_LUN_WARNING; 2129229997Sken } 2130229997Sken } else { 2131229997Sken /* 2132229997Sken * For processor devices, we don't have any size. 2133229997Sken */ 2134229997Sken be_lun->blocksize = 0; 2135264191Smav be_lun->pblockexp = 0; 2136264191Smav be_lun->pblockoff = 0; 2137229997Sken be_lun->size_blocks = 0; 2138229997Sken be_lun->size_bytes = 0; 2139229997Sken be_lun->ctl_be_lun.maxlba = 0; 2140229997Sken 2141229997Sken /* 2142229997Sken * Default to just 1 thread for processor devices. 2143229997Sken */ 2144229997Sken num_threads = 1; 2145229997Sken } 2146229997Sken 2147229997Sken /* 2148229997Sken * XXX This searching loop might be refactored to be combined with 2149229997Sken * the loop above, 2150229997Sken */ 2151268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 2152267481Smav if (value != NULL) { 2153267481Smav tmp_num_threads = strtol(value, NULL, 0); 2154229997Sken 2155267481Smav /* 2156267481Smav * We don't let the user specify less than one 2157267481Smav * thread, but hope he's clueful enough not to 2158267481Smav * specify 1000 threads. 2159267481Smav */ 2160267481Smav if (tmp_num_threads < 1) { 2161267481Smav snprintf(req->error_str, sizeof(req->error_str), 2162272911Smav "invalid number of threads %s", 2163272911Smav num_thread_str); 2164267481Smav goto bailout_error; 2165229997Sken } 2166267481Smav num_threads = tmp_num_threads; 2167229997Sken } 2168274154Smav unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol); 2169268280Smav value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2170274154Smav if (value != NULL) 2171274154Smav unmap = (strcmp(value, "on") == 0); 2172229997Sken 2173229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2174229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 2175272911Smav if (be_lun->vn == NULL) 2176272911Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2177264274Smav if (unmap) 2178264274Smav be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2179229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 2180272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2181272911Smav 0 : (be_lun->size_blocks - 1); 2182229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2183264191Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2184264191Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2185272911Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2186272911Smav be_lun->blocksize != 0) 2187272911Smav be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2188272911Smav be_lun->blocksize; 2189229997Sken /* Tell the user the blocksize we ended up using */ 2190272911Smav params->lun_size_bytes = be_lun->size_bytes; 2191229997Sken params->blocksize_bytes = be_lun->blocksize; 2192229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2193229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2194229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2195229997Sken } else 2196229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 2197229997Sken 2198229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2199229997Sken be_lun->ctl_be_lun.lun_config_status = 2200229997Sken ctl_be_block_lun_config_status; 2201229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2202229997Sken 2203229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2204229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2205229997Sken softc->num_luns); 2206229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2207229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2208229997Sken sizeof(tmpstr))); 2209229997Sken 2210229997Sken /* Tell the user what we used for a serial number */ 2211229997Sken strncpy((char *)params->serial_num, tmpstr, 2212229997Sken ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 2213229997Sken } else { 2214229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 2215229997Sken params->serial_num, 2216229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2217229997Sken sizeof(params->serial_num))); 2218229997Sken } 2219229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2220229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2221229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2222229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2223229997Sken sizeof(tmpstr))); 2224229997Sken 2225229997Sken /* Tell the user what we used for a device ID */ 2226229997Sken strncpy((char *)params->device_id, tmpstr, 2227229997Sken ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2228229997Sken } else { 2229229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 2230229997Sken params->device_id, 2231229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2232229997Sken sizeof(params->device_id))); 2233229997Sken } 2234229997Sken 2235229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2236229997Sken 2237229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2238229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2239229997Sken 2240229997Sken if (be_lun->io_taskqueue == NULL) { 2241229997Sken snprintf(req->error_str, sizeof(req->error_str), 2242272911Smav "unable to create taskqueue"); 2243229997Sken goto bailout_error; 2244229997Sken } 2245229997Sken 2246229997Sken /* 2247229997Sken * Note that we start the same number of threads by default for 2248229997Sken * both the file case and the block device case. For the file 2249229997Sken * case, we need multiple threads to allow concurrency, because the 2250229997Sken * vnode interface is designed to be a blocking interface. For the 2251229997Sken * block device case, ZFS zvols at least will block the caller's 2252229997Sken * context in many instances, and so we need multiple threads to 2253229997Sken * overcome that problem. Other block devices don't need as many 2254229997Sken * threads, but they shouldn't cause too many problems. 2255229997Sken * 2256229997Sken * If the user wants to just have a single thread for a block 2257229997Sken * device, he can specify that when the LUN is created, or change 2258229997Sken * the tunable/sysctl to alter the default number of threads. 2259229997Sken */ 2260229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2261229997Sken /*num threads*/num_threads, 2262229997Sken /*priority*/PWAIT, 2263229997Sken /*thread name*/ 2264229997Sken "%s taskq", be_lun->lunname); 2265229997Sken 2266229997Sken if (retval != 0) 2267229997Sken goto bailout_error; 2268229997Sken 2269229997Sken be_lun->num_threads = num_threads; 2270229997Sken 2271229997Sken mtx_lock(&softc->lock); 2272229997Sken softc->num_luns++; 2273229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2274229997Sken 2275229997Sken mtx_unlock(&softc->lock); 2276229997Sken 2277229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 2278229997Sken if (retval != 0) { 2279229997Sken mtx_lock(&softc->lock); 2280229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2281229997Sken links); 2282229997Sken softc->num_luns--; 2283229997Sken mtx_unlock(&softc->lock); 2284229997Sken snprintf(req->error_str, sizeof(req->error_str), 2285272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2286272911Smav "details", retval); 2287229997Sken retval = 0; 2288229997Sken goto bailout_error; 2289229997Sken } 2290229997Sken 2291229997Sken mtx_lock(&softc->lock); 2292229997Sken 2293229997Sken /* 2294229997Sken * Tell the config_status routine that we're waiting so it won't 2295229997Sken * clean up the LUN in the event of an error. 2296229997Sken */ 2297229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2298229997Sken 2299229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2300229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2301229997Sken if (retval == EINTR) 2302229997Sken break; 2303229997Sken } 2304229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2305229997Sken 2306229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2307229997Sken snprintf(req->error_str, sizeof(req->error_str), 2308272911Smav "LUN configuration error, see dmesg for details"); 2309229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2310229997Sken links); 2311229997Sken softc->num_luns--; 2312229997Sken mtx_unlock(&softc->lock); 2313229997Sken goto bailout_error; 2314229997Sken } else { 2315229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2316229997Sken } 2317229997Sken 2318229997Sken mtx_unlock(&softc->lock); 2319229997Sken 2320229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2321229997Sken be_lun->blocksize, 2322229997Sken DEVSTAT_ALL_SUPPORTED, 2323229997Sken be_lun->ctl_be_lun.lun_type 2324229997Sken | DEVSTAT_TYPE_IF_OTHER, 2325229997Sken DEVSTAT_PRIORITY_OTHER); 2326229997Sken 2327229997Sken return (retval); 2328229997Sken 2329229997Skenbailout_error: 2330229997Sken req->status = CTL_LUN_ERROR; 2331229997Sken 2332267429Smav if (be_lun->io_taskqueue != NULL) 2333267429Smav taskqueue_free(be_lun->io_taskqueue); 2334229997Sken ctl_be_block_close(be_lun); 2335267429Smav if (be_lun->dev_path != NULL) 2336267429Smav free(be_lun->dev_path, M_CTLBLK); 2337267429Smav if (be_lun->lun_zone != NULL) 2338267429Smav uma_zdestroy(be_lun->lun_zone); 2339268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2340267877Smav mtx_destroy(&be_lun->queue_lock); 2341267877Smav mtx_destroy(&be_lun->io_lock); 2342229997Sken free(be_lun, M_CTLBLK); 2343229997Sken 2344229997Sken return (retval); 2345229997Sken} 2346229997Sken 2347229997Skenstatic int 2348229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2349229997Sken{ 2350229997Sken struct ctl_lun_rm_params *params; 2351229997Sken struct ctl_be_block_lun *be_lun; 2352229997Sken int retval; 2353229997Sken 2354229997Sken params = &req->reqdata.rm; 2355229997Sken 2356229997Sken mtx_lock(&softc->lock); 2357229997Sken 2358229997Sken be_lun = NULL; 2359229997Sken 2360229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2361229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2362229997Sken break; 2363229997Sken } 2364229997Sken mtx_unlock(&softc->lock); 2365229997Sken 2366229997Sken if (be_lun == NULL) { 2367229997Sken snprintf(req->error_str, sizeof(req->error_str), 2368272911Smav "LUN %u is not managed by the block backend", 2369272911Smav params->lun_id); 2370229997Sken goto bailout_error; 2371229997Sken } 2372229997Sken 2373229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2374229997Sken 2375229997Sken if (retval != 0) { 2376229997Sken snprintf(req->error_str, sizeof(req->error_str), 2377272911Smav "error %d returned from ctl_disable_lun() for " 2378272911Smav "LUN %d", retval, params->lun_id); 2379229997Sken goto bailout_error; 2380229997Sken 2381229997Sken } 2382229997Sken 2383229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2384229997Sken if (retval != 0) { 2385229997Sken snprintf(req->error_str, sizeof(req->error_str), 2386272911Smav "error %d returned from ctl_invalidate_lun() for " 2387272911Smav "LUN %d", retval, params->lun_id); 2388229997Sken goto bailout_error; 2389229997Sken } 2390229997Sken 2391229997Sken mtx_lock(&softc->lock); 2392229997Sken 2393229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2394229997Sken 2395229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2396229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2397229997Sken if (retval == EINTR) 2398229997Sken break; 2399229997Sken } 2400229997Sken 2401229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2402229997Sken 2403229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2404229997Sken snprintf(req->error_str, sizeof(req->error_str), 2405272911Smav "interrupted waiting for LUN to be freed"); 2406229997Sken mtx_unlock(&softc->lock); 2407229997Sken goto bailout_error; 2408229997Sken } 2409229997Sken 2410229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2411229997Sken 2412229997Sken softc->num_luns--; 2413229997Sken mtx_unlock(&softc->lock); 2414229997Sken 2415229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2416229997Sken 2417229997Sken taskqueue_free(be_lun->io_taskqueue); 2418229997Sken 2419229997Sken ctl_be_block_close(be_lun); 2420229997Sken 2421229997Sken if (be_lun->disk_stats != NULL) 2422229997Sken devstat_remove_entry(be_lun->disk_stats); 2423229997Sken 2424229997Sken uma_zdestroy(be_lun->lun_zone); 2425229997Sken 2426268280Smav ctl_free_opts(&be_lun->ctl_be_lun.options); 2427229997Sken free(be_lun->dev_path, M_CTLBLK); 2428267877Smav mtx_destroy(&be_lun->queue_lock); 2429267877Smav mtx_destroy(&be_lun->io_lock); 2430229997Sken free(be_lun, M_CTLBLK); 2431229997Sken 2432229997Sken req->status = CTL_LUN_OK; 2433229997Sken 2434229997Sken return (0); 2435229997Sken 2436229997Skenbailout_error: 2437229997Sken 2438229997Sken req->status = CTL_LUN_ERROR; 2439229997Sken 2440229997Sken return (0); 2441229997Sken} 2442229997Sken 2443232604Straszstatic int 2444232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2445232604Strasz struct ctl_lun_req *req) 2446232604Strasz{ 2447232604Strasz struct vattr vattr; 2448232604Strasz int error; 2449272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2450232604Strasz 2451232604Strasz if (params->lun_size_bytes != 0) { 2452232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2453232604Strasz } else { 2454271794Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2455232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2456271794Smav VOP_UNLOCK(be_lun->vn, 0); 2457232604Strasz if (error != 0) { 2458232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2459232604Strasz "error calling VOP_GETATTR() for file %s", 2460232604Strasz be_lun->dev_path); 2461232604Strasz return (error); 2462232604Strasz } 2463232604Strasz 2464232604Strasz be_lun->size_bytes = vattr.va_size; 2465232604Strasz } 2466232604Strasz 2467232604Strasz return (0); 2468232604Strasz} 2469232604Strasz 2470232604Straszstatic int 2471232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2472232604Strasz struct ctl_lun_req *req) 2473232604Strasz{ 2474271794Smav struct ctl_be_block_devdata *dev_data; 2475232604Strasz int error; 2476272911Smav struct ctl_lun_create_params *params = &be_lun->params; 2477232604Strasz uint64_t size_bytes; 2478232604Strasz 2479271794Smav dev_data = &be_lun->backend.dev; 2480271794Smav if (!dev_data->csw->d_ioctl) { 2481232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2482272911Smav "no d_ioctl for device %s!", be_lun->dev_path); 2483232604Strasz return (ENODEV); 2484232604Strasz } 2485232604Strasz 2486271794Smav error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2487232604Strasz (caddr_t)&size_bytes, FREAD, 2488232604Strasz curthread); 2489232604Strasz if (error) { 2490232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2491272911Smav "error %d returned for DIOCGMEDIASIZE ioctl " 2492272911Smav "on %s!", error, be_lun->dev_path); 2493232604Strasz return (error); 2494232604Strasz } 2495232604Strasz 2496232604Strasz if (params->lun_size_bytes != 0) { 2497232604Strasz if (params->lun_size_bytes > size_bytes) { 2498232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2499272911Smav "requested LUN size %ju > backing device " 2500272911Smav "size %ju", 2501232604Strasz (uintmax_t)params->lun_size_bytes, 2502232604Strasz (uintmax_t)size_bytes); 2503232604Strasz return (EINVAL); 2504232604Strasz } 2505232604Strasz 2506232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2507232604Strasz } else { 2508232604Strasz be_lun->size_bytes = size_bytes; 2509232604Strasz } 2510232604Strasz 2511232604Strasz return (0); 2512232604Strasz} 2513232604Strasz 2514232604Straszstatic int 2515232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2516232604Strasz{ 2517232604Strasz struct ctl_lun_modify_params *params; 2518232604Strasz struct ctl_be_block_lun *be_lun; 2519271794Smav uint64_t oldsize; 2520241896Skib int error; 2521232604Strasz 2522232604Strasz params = &req->reqdata.modify; 2523232604Strasz 2524232604Strasz mtx_lock(&softc->lock); 2525232604Strasz be_lun = NULL; 2526232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2527232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2528232604Strasz break; 2529232604Strasz } 2530232604Strasz mtx_unlock(&softc->lock); 2531232604Strasz 2532232604Strasz if (be_lun == NULL) { 2533232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2534272911Smav "LUN %u is not managed by the block backend", 2535272911Smav params->lun_id); 2536232604Strasz goto bailout_error; 2537232604Strasz } 2538232604Strasz 2539272911Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2540232604Strasz 2541274253Smav oldsize = be_lun->size_bytes; 2542272911Smav if (be_lun->vn == NULL) 2543272911Smav error = ctl_be_block_open(softc, be_lun, req); 2544272911Smav else if (be_lun->vn->v_type == VREG) 2545232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2546232604Strasz else 2547232604Strasz error = ctl_be_block_modify_dev(be_lun, req); 2548232604Strasz 2549274253Smav if (error == 0 && be_lun->size_bytes != oldsize) { 2550271794Smav be_lun->size_blocks = be_lun->size_bytes >> 2551271794Smav be_lun->blocksize_shift; 2552232604Strasz 2553271794Smav /* 2554271794Smav * The maximum LBA is the size - 1. 2555271794Smav * 2556271794Smav * XXX: Note that this field is being updated without locking, 2557271794Smav * which might cause problems on 32-bit architectures. 2558271794Smav */ 2559272911Smav be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2560272911Smav 0 : (be_lun->size_blocks - 1); 2561272911Smav be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2562272911Smav be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2563272911Smav be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2564272911Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 2565272911Smav be_lun->blocksize != 0) 2566272911Smav be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 2567272911Smav be_lun->blocksize; 2568271794Smav ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2569272911Smav if (oldsize == 0 && be_lun->size_blocks != 0) 2570272911Smav ctl_lun_online(&be_lun->ctl_be_lun); 2571271794Smav } 2572232604Strasz 2573232604Strasz /* Tell the user the exact size we ended up using */ 2574232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2575232604Strasz 2576272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2577232604Strasz 2578232604Strasz return (0); 2579232604Strasz 2580232604Straszbailout_error: 2581232604Strasz req->status = CTL_LUN_ERROR; 2582232604Strasz 2583232604Strasz return (0); 2584232604Strasz} 2585232604Strasz 2586229997Skenstatic void 2587229997Skenctl_be_block_lun_shutdown(void *be_lun) 2588229997Sken{ 2589229997Sken struct ctl_be_block_lun *lun; 2590229997Sken struct ctl_be_block_softc *softc; 2591229997Sken 2592229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2593229997Sken 2594229997Sken softc = lun->softc; 2595229997Sken 2596229997Sken mtx_lock(&softc->lock); 2597229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2598229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2599229997Sken wakeup(lun); 2600229997Sken mtx_unlock(&softc->lock); 2601229997Sken 2602229997Sken} 2603229997Sken 2604229997Skenstatic void 2605229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2606229997Sken{ 2607229997Sken struct ctl_be_block_lun *lun; 2608229997Sken struct ctl_be_block_softc *softc; 2609229997Sken 2610229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2611229997Sken softc = lun->softc; 2612229997Sken 2613229997Sken if (status == CTL_LUN_CONFIG_OK) { 2614229997Sken mtx_lock(&softc->lock); 2615229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2616229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2617229997Sken wakeup(lun); 2618229997Sken mtx_unlock(&softc->lock); 2619229997Sken 2620229997Sken /* 2621229997Sken * We successfully added the LUN, attempt to enable it. 2622229997Sken */ 2623229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2624229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2625229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2626229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2627229997Sken __func__); 2628229997Sken } 2629229997Sken } 2630229997Sken 2631229997Sken return; 2632229997Sken } 2633229997Sken 2634229997Sken 2635229997Sken mtx_lock(&softc->lock); 2636229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2637229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2638229997Sken wakeup(lun); 2639229997Sken mtx_unlock(&softc->lock); 2640229997Sken} 2641229997Sken 2642229997Sken 2643229997Skenstatic int 2644229997Skenctl_be_block_config_write(union ctl_io *io) 2645229997Sken{ 2646229997Sken struct ctl_be_block_lun *be_lun; 2647229997Sken struct ctl_be_lun *ctl_be_lun; 2648229997Sken int retval; 2649229997Sken 2650229997Sken retval = 0; 2651229997Sken 2652229997Sken DPRINTF("entered\n"); 2653229997Sken 2654229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2655229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2656229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2657229997Sken 2658229997Sken switch (io->scsiio.cdb[0]) { 2659229997Sken case SYNCHRONIZE_CACHE: 2660229997Sken case SYNCHRONIZE_CACHE_16: 2661264274Smav case WRITE_SAME_10: 2662264274Smav case WRITE_SAME_16: 2663264274Smav case UNMAP: 2664229997Sken /* 2665229997Sken * The upper level CTL code will filter out any CDBs with 2666229997Sken * the immediate bit set and return the proper error. 2667229997Sken * 2668229997Sken * We don't really need to worry about what LBA range the 2669229997Sken * user asked to be synced out. When they issue a sync 2670229997Sken * cache command, we'll sync out the whole thing. 2671229997Sken */ 2672267877Smav mtx_lock(&be_lun->queue_lock); 2673229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2674229997Sken links); 2675267877Smav mtx_unlock(&be_lun->queue_lock); 2676229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2677229997Sken break; 2678229997Sken case START_STOP_UNIT: { 2679229997Sken struct scsi_start_stop_unit *cdb; 2680229997Sken 2681229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2682229997Sken 2683229997Sken if (cdb->how & SSS_START) 2684229997Sken retval = ctl_start_lun(ctl_be_lun); 2685229997Sken else { 2686229997Sken retval = ctl_stop_lun(ctl_be_lun); 2687229997Sken /* 2688229997Sken * XXX KDM Copan-specific offline behavior. 2689229997Sken * Figure out a reasonable way to port this? 2690229997Sken */ 2691229997Sken#ifdef NEEDTOPORT 2692229997Sken if ((retval == 0) 2693229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2694229997Sken retval = ctl_lun_offline(ctl_be_lun); 2695229997Sken#endif 2696229997Sken } 2697229997Sken 2698229997Sken /* 2699229997Sken * In general, the above routines should not fail. They 2700229997Sken * just set state for the LUN. So we've got something 2701229997Sken * pretty wrong here if we can't start or stop the LUN. 2702229997Sken */ 2703229997Sken if (retval != 0) { 2704229997Sken ctl_set_internal_failure(&io->scsiio, 2705229997Sken /*sks_valid*/ 1, 2706229997Sken /*retry_count*/ 0xf051); 2707229997Sken retval = CTL_RETVAL_COMPLETE; 2708229997Sken } else { 2709229997Sken ctl_set_success(&io->scsiio); 2710229997Sken } 2711229997Sken ctl_config_write_done(io); 2712229997Sken break; 2713229997Sken } 2714229997Sken default: 2715229997Sken ctl_set_invalid_opcode(&io->scsiio); 2716229997Sken ctl_config_write_done(io); 2717229997Sken retval = CTL_RETVAL_COMPLETE; 2718229997Sken break; 2719229997Sken } 2720229997Sken 2721229997Sken return (retval); 2722229997Sken} 2723229997Sken 2724229997Skenstatic int 2725229997Skenctl_be_block_config_read(union ctl_io *io) 2726229997Sken{ 2727275474Smav struct ctl_be_block_lun *be_lun; 2728275474Smav struct ctl_be_lun *ctl_be_lun; 2729275474Smav int retval = 0; 2730275474Smav 2731275474Smav DPRINTF("entered\n"); 2732275474Smav 2733275474Smav ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2734275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2735275474Smav be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2736275474Smav 2737275474Smav switch (io->scsiio.cdb[0]) { 2738275474Smav case SERVICE_ACTION_IN: 2739275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2740275474Smav mtx_lock(&be_lun->queue_lock); 2741275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2742275474Smav &io->io_hdr, links); 2743275474Smav mtx_unlock(&be_lun->queue_lock); 2744275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2745275474Smav &be_lun->io_task); 2746275474Smav retval = CTL_RETVAL_QUEUED; 2747275474Smav break; 2748275474Smav } 2749275474Smav ctl_set_invalid_field(&io->scsiio, 2750275474Smav /*sks_valid*/ 1, 2751275474Smav /*command*/ 1, 2752275474Smav /*field*/ 1, 2753275474Smav /*bit_valid*/ 1, 2754275474Smav /*bit*/ 4); 2755275474Smav ctl_config_read_done(io); 2756275474Smav retval = CTL_RETVAL_COMPLETE; 2757275474Smav break; 2758275474Smav default: 2759275474Smav ctl_set_invalid_opcode(&io->scsiio); 2760275474Smav ctl_config_read_done(io); 2761275474Smav retval = CTL_RETVAL_COMPLETE; 2762275474Smav break; 2763275474Smav } 2764275474Smav 2765275474Smav return (retval); 2766229997Sken} 2767229997Sken 2768229997Skenstatic int 2769229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2770229997Sken{ 2771229997Sken struct ctl_be_block_lun *lun; 2772229997Sken int retval; 2773229997Sken 2774229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2775229997Sken retval = 0; 2776229997Sken 2777268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2778229997Sken 2779229997Sken if (retval != 0) 2780229997Sken goto bailout; 2781229997Sken 2782229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2783229997Sken 2784229997Sken if (retval != 0) 2785229997Sken goto bailout; 2786229997Sken 2787268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2788229997Sken 2789229997Skenbailout: 2790229997Sken 2791229997Sken return (retval); 2792229997Sken} 2793229997Sken 2794274154Smavstatic uint64_t 2795274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2796274154Smav{ 2797274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2798274154Smav 2799274154Smav if (lun->getattr == NULL) 2800274154Smav return (UINT64_MAX); 2801274154Smav return (lun->getattr(lun, attrname)); 2802274154Smav} 2803274154Smav 2804229997Skenint 2805229997Skenctl_be_block_init(void) 2806229997Sken{ 2807229997Sken struct ctl_be_block_softc *softc; 2808229997Sken int retval; 2809229997Sken 2810229997Sken softc = &backend_block_softc; 2811229997Sken retval = 0; 2812229997Sken 2813267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2814264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2815264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2816229997Sken STAILQ_INIT(&softc->disk_list); 2817229997Sken STAILQ_INIT(&softc->lun_list); 2818229997Sken 2819229997Sken return (retval); 2820229997Sken} 2821