1130f4520SKenneth D. Merry /*- 2130f4520SKenneth D. Merry * Copyright (c) 2003 Silicon Graphics International Corp. 3130f4520SKenneth D. Merry * Copyright (c) 2009-2011 Spectra Logic Corporation 481177295SEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 5130f4520SKenneth D. Merry * All rights reserved. 6130f4520SKenneth D. Merry * 781177295SEdward Tomasz Napierala * Portions of this software were developed by Edward Tomasz Napierala 881177295SEdward Tomasz Napierala * under sponsorship from the FreeBSD Foundation. 981177295SEdward Tomasz Napierala * 10130f4520SKenneth D. Merry * Redistribution and use in source and binary forms, with or without 11130f4520SKenneth D. Merry * modification, are permitted provided that the following conditions 12130f4520SKenneth D. Merry * are met: 13130f4520SKenneth D. Merry * 1. Redistributions of source code must retain the above copyright 14130f4520SKenneth D. Merry * notice, this list of conditions, and the following disclaimer, 15130f4520SKenneth D. Merry * without modification. 16130f4520SKenneth D. Merry * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17130f4520SKenneth D. Merry * substantially similar to the "NO WARRANTY" disclaimer below 18130f4520SKenneth D. Merry * ("Disclaimer") and any redistribution must be conditioned upon 19130f4520SKenneth D. Merry * including a substantially similar Disclaimer requirement for further 20130f4520SKenneth D. Merry * binary redistribution. 21130f4520SKenneth D. Merry * 22130f4520SKenneth D. Merry * NO WARRANTY 23130f4520SKenneth D. Merry * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24130f4520SKenneth D. Merry * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25130f4520SKenneth D. Merry * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26130f4520SKenneth D. Merry * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27130f4520SKenneth D. Merry * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28130f4520SKenneth D. Merry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29130f4520SKenneth D. Merry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30130f4520SKenneth D. Merry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31130f4520SKenneth D. Merry * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32130f4520SKenneth D. Merry * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33130f4520SKenneth D. Merry * POSSIBILITY OF SUCH DAMAGES. 34130f4520SKenneth D. Merry * 35130f4520SKenneth D. Merry * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 36130f4520SKenneth D. Merry */ 37130f4520SKenneth D. Merry /* 38130f4520SKenneth D. Merry * CAM Target Layer driver backend for block devices. 39130f4520SKenneth D. Merry * 40130f4520SKenneth D. Merry * Author: Ken Merry <ken@FreeBSD.org> 41130f4520SKenneth D. Merry */ 42130f4520SKenneth D. Merry #include <sys/cdefs.h> 43130f4520SKenneth D. Merry __FBSDID("$FreeBSD$"); 44130f4520SKenneth D. Merry 45130f4520SKenneth D. Merry #include <sys/param.h> 46130f4520SKenneth D. Merry #include <sys/systm.h> 47130f4520SKenneth D. Merry #include <sys/kernel.h> 48130f4520SKenneth D. Merry #include <sys/types.h> 49130f4520SKenneth D. Merry #include <sys/kthread.h> 50130f4520SKenneth D. Merry #include <sys/bio.h> 51130f4520SKenneth D. Merry #include <sys/fcntl.h> 52ee7f31c0SAlexander Motin #include <sys/limits.h> 53130f4520SKenneth D. Merry #include <sys/lock.h> 54130f4520SKenneth D. Merry #include <sys/mutex.h> 55130f4520SKenneth D. Merry #include <sys/condvar.h> 56130f4520SKenneth D. Merry #include <sys/malloc.h> 57130f4520SKenneth D. Merry #include <sys/conf.h> 58130f4520SKenneth D. Merry #include <sys/ioccom.h> 59130f4520SKenneth D. Merry #include <sys/queue.h> 60130f4520SKenneth D. Merry #include <sys/sbuf.h> 61130f4520SKenneth D. Merry #include <sys/endian.h> 62130f4520SKenneth D. Merry #include <sys/uio.h> 63130f4520SKenneth D. Merry #include <sys/buf.h> 64130f4520SKenneth D. Merry #include <sys/taskqueue.h> 65130f4520SKenneth D. Merry #include <sys/vnode.h> 66130f4520SKenneth D. Merry #include <sys/namei.h> 67130f4520SKenneth D. Merry #include <sys/mount.h> 68130f4520SKenneth D. Merry #include <sys/disk.h> 69130f4520SKenneth D. Merry #include <sys/fcntl.h> 70130f4520SKenneth D. Merry #include <sys/filedesc.h> 71130f4520SKenneth D. Merry #include <sys/proc.h> 72130f4520SKenneth D. Merry #include <sys/pcpu.h> 73130f4520SKenneth D. Merry #include <sys/module.h> 74130f4520SKenneth D. Merry #include <sys/sdt.h> 75130f4520SKenneth D. Merry #include <sys/devicestat.h> 76130f4520SKenneth D. Merry #include <sys/sysctl.h> 77130f4520SKenneth D. Merry 78130f4520SKenneth D. Merry #include <geom/geom.h> 79130f4520SKenneth D. Merry 80130f4520SKenneth D. Merry #include <cam/cam.h> 81130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h> 82130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h> 83130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h> 84130f4520SKenneth D. Merry #include <cam/ctl/ctl.h> 85130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h> 86130f4520SKenneth D. Merry #include <cam/ctl/ctl_frontend_internal.h> 87130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h> 88130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h> 89130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h> 90130f4520SKenneth D. Merry 91130f4520SKenneth D. Merry /* 9208a7cce5SAlexander Motin * The idea here is that we'll allocate enough S/G space to hold a 1MB 9308a7cce5SAlexander Motin * I/O. If we get an I/O larger than that, we'll split it. 94130f4520SKenneth D. Merry */ 95*11b569f7SAlexander Motin #define CTLBLK_HALF_IO_SIZE (512 * 1024) 96*11b569f7SAlexander Motin #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 9708a7cce5SAlexander Motin #define CTLBLK_MAX_SEG MAXPHYS 98*11b569f7SAlexander Motin #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 99*11b569f7SAlexander Motin #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 100130f4520SKenneth D. Merry 101130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG 102130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \ 103130f4520SKenneth D. Merry printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 104130f4520SKenneth D. Merry #else 105130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0) 106130f4520SKenneth D. Merry #endif 107130f4520SKenneth D. Merry 108e86a4142SAlexander Motin #define PRIV(io) \ 109e86a4142SAlexander Motin ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 110*11b569f7SAlexander Motin #define ARGS(io) \ 111*11b569f7SAlexander Motin ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 112e86a4142SAlexander Motin 113130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb); 114130f4520SKenneth D. Merry 115130f4520SKenneth D. Merry typedef enum { 116130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 117130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 118130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_WAITING = 0x04, 119130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 120130f4520SKenneth D. Merry } ctl_be_block_lun_flags; 121130f4520SKenneth D. Merry 122130f4520SKenneth D. Merry typedef enum { 123130f4520SKenneth D. Merry CTL_BE_BLOCK_NONE, 124130f4520SKenneth D. Merry CTL_BE_BLOCK_DEV, 125130f4520SKenneth D. Merry CTL_BE_BLOCK_FILE 126130f4520SKenneth D. Merry } ctl_be_block_type; 127130f4520SKenneth D. Merry 128130f4520SKenneth D. Merry struct ctl_be_block_devdata { 129130f4520SKenneth D. Merry struct cdev *cdev; 130130f4520SKenneth D. Merry struct cdevsw *csw; 131130f4520SKenneth D. Merry int dev_ref; 132130f4520SKenneth D. Merry }; 133130f4520SKenneth D. Merry 134130f4520SKenneth D. Merry struct ctl_be_block_filedata { 135130f4520SKenneth D. Merry struct ucred *cred; 136130f4520SKenneth D. Merry }; 137130f4520SKenneth D. Merry 138130f4520SKenneth D. Merry union ctl_be_block_bedata { 139130f4520SKenneth D. Merry struct ctl_be_block_devdata dev; 140130f4520SKenneth D. Merry struct ctl_be_block_filedata file; 141130f4520SKenneth D. Merry }; 142130f4520SKenneth D. Merry 143130f4520SKenneth D. Merry struct ctl_be_block_io; 144130f4520SKenneth D. Merry struct ctl_be_block_lun; 145130f4520SKenneth D. Merry 146130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 147130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 148130f4520SKenneth D. Merry 149130f4520SKenneth D. Merry /* 150130f4520SKenneth D. Merry * Backend LUN structure. There is a 1:1 mapping between a block device 151130f4520SKenneth D. Merry * and a backend block LUN, and between a backend block LUN and a CTL LUN. 152130f4520SKenneth D. Merry */ 153130f4520SKenneth D. Merry struct ctl_be_block_lun { 154130f4520SKenneth D. Merry struct ctl_block_disk *disk; 155130f4520SKenneth D. Merry char lunname[32]; 156130f4520SKenneth D. Merry char *dev_path; 157130f4520SKenneth D. Merry ctl_be_block_type dev_type; 158130f4520SKenneth D. Merry struct vnode *vn; 159130f4520SKenneth D. Merry union ctl_be_block_bedata backend; 160130f4520SKenneth D. Merry cbb_dispatch_t dispatch; 161130f4520SKenneth D. Merry cbb_dispatch_t lun_flush; 162ee7f31c0SAlexander Motin cbb_dispatch_t unmap; 163130f4520SKenneth D. Merry struct mtx lock; 164130f4520SKenneth D. Merry uma_zone_t lun_zone; 165130f4520SKenneth D. Merry uint64_t size_blocks; 166130f4520SKenneth D. Merry uint64_t size_bytes; 167130f4520SKenneth D. Merry uint32_t blocksize; 168130f4520SKenneth D. Merry int blocksize_shift; 169f6012722SAlexander Motin uint16_t pblockexp; 170f6012722SAlexander Motin uint16_t pblockoff; 171130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 172130f4520SKenneth D. Merry struct devstat *disk_stats; 173130f4520SKenneth D. Merry ctl_be_block_lun_flags flags; 174130f4520SKenneth D. Merry STAILQ_ENTRY(ctl_be_block_lun) links; 175130f4520SKenneth D. Merry struct ctl_be_lun ctl_be_lun; 176130f4520SKenneth D. Merry struct taskqueue *io_taskqueue; 177130f4520SKenneth D. Merry struct task io_task; 178130f4520SKenneth D. Merry int num_threads; 179130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) input_queue; 180130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 181130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 182130f4520SKenneth D. Merry }; 183130f4520SKenneth D. Merry 184130f4520SKenneth D. Merry /* 185130f4520SKenneth D. Merry * Overall softc structure for the block backend module. 186130f4520SKenneth D. Merry */ 187130f4520SKenneth D. Merry struct ctl_be_block_softc { 188130f4520SKenneth D. Merry struct mtx lock; 189130f4520SKenneth D. Merry int num_disks; 190130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_block_disk) disk_list; 191130f4520SKenneth D. Merry int num_luns; 192130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_be_block_lun) lun_list; 193130f4520SKenneth D. Merry }; 194130f4520SKenneth D. Merry 195130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc; 196130f4520SKenneth D. Merry 197130f4520SKenneth D. Merry /* 198130f4520SKenneth D. Merry * Per-I/O information. 199130f4520SKenneth D. Merry */ 200130f4520SKenneth D. Merry struct ctl_be_block_io { 201130f4520SKenneth D. Merry union ctl_io *io; 202130f4520SKenneth D. Merry struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 203130f4520SKenneth D. Merry struct iovec xiovecs[CTLBLK_MAX_SEGS]; 204130f4520SKenneth D. Merry int bio_cmd; 205130f4520SKenneth D. Merry int bio_flags; 206130f4520SKenneth D. Merry int num_segs; 207130f4520SKenneth D. Merry int num_bios_sent; 208130f4520SKenneth D. Merry int num_bios_done; 209130f4520SKenneth D. Merry int send_complete; 210130f4520SKenneth D. Merry int num_errors; 211130f4520SKenneth D. Merry struct bintime ds_t0; 212130f4520SKenneth D. Merry devstat_tag_type ds_tag_type; 213130f4520SKenneth D. Merry devstat_trans_flags ds_trans_type; 214130f4520SKenneth D. Merry uint64_t io_len; 215130f4520SKenneth D. Merry uint64_t io_offset; 216130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 217130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 218ee7f31c0SAlexander Motin void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 219130f4520SKenneth D. Merry }; 220130f4520SKenneth D. Merry 221130f4520SKenneth D. Merry static int cbb_num_threads = 14; 222130f4520SKenneth D. Merry TUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads); 223130f4520SKenneth D. Merry SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 224130f4520SKenneth D. Merry "CAM Target Layer Block Backend"); 225130f4520SKenneth D. Merry SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW, 226130f4520SKenneth D. Merry &cbb_num_threads, 0, "Number of threads per backing file"); 227130f4520SKenneth D. Merry 228130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 229130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio); 230130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio); 231130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io); 232130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio); 233130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 234130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 235130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 236130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 237130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 238130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 239ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 240ee7f31c0SAlexander Motin struct ctl_be_block_io *beio); 241130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 242130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 243130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 244130f4520SKenneth D. Merry union ctl_io *io); 245130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 246130f4520SKenneth D. Merry union ctl_io *io); 247130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending); 248130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io); 249130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 250130f4520SKenneth D. Merry int flag, struct thread *td); 251130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 252130f4520SKenneth D. Merry struct ctl_lun_req *req); 253130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 254130f4520SKenneth D. Merry struct ctl_lun_req *req); 255130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 256130f4520SKenneth D. Merry static int ctl_be_block_open(struct ctl_be_block_softc *softc, 257130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, 258130f4520SKenneth D. Merry struct ctl_lun_req *req); 259130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc, 260130f4520SKenneth D. Merry struct ctl_lun_req *req); 261130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 262130f4520SKenneth D. Merry struct ctl_lun_req *req); 26381177295SEdward Tomasz Napierala static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 26481177295SEdward Tomasz Napierala struct ctl_lun_req *req); 26581177295SEdward Tomasz Napierala static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 26681177295SEdward Tomasz Napierala struct ctl_lun_req *req); 26781177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 26881177295SEdward Tomasz Napierala struct ctl_lun_req *req); 269130f4520SKenneth D. Merry static void ctl_be_block_lun_shutdown(void *be_lun); 270130f4520SKenneth D. Merry static void ctl_be_block_lun_config_status(void *be_lun, 271130f4520SKenneth D. Merry ctl_lun_config_status status); 272130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io); 273130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io); 274130f4520SKenneth D. Merry static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 275130f4520SKenneth D. Merry int ctl_be_block_init(void); 276130f4520SKenneth D. Merry 277130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver = 278130f4520SKenneth D. Merry { 2792a2443d8SKenneth D. Merry .name = "block", 2802a2443d8SKenneth D. Merry .flags = CTL_BE_FLAG_HAS_CONFIG, 2812a2443d8SKenneth D. Merry .init = ctl_be_block_init, 2822a2443d8SKenneth D. Merry .data_submit = ctl_be_block_submit, 2832a2443d8SKenneth D. Merry .data_move_done = ctl_be_block_move_done, 2842a2443d8SKenneth D. Merry .config_read = ctl_be_block_config_read, 2852a2443d8SKenneth D. Merry .config_write = ctl_be_block_config_write, 2862a2443d8SKenneth D. Merry .ioctl = ctl_be_block_ioctl, 2872a2443d8SKenneth D. Merry .lun_info = ctl_be_block_lun_info 288130f4520SKenneth D. Merry }; 289130f4520SKenneth D. Merry 290130f4520SKenneth D. Merry MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 291130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 292130f4520SKenneth D. Merry 293a0e36aeeSEdward Tomasz Napierala static uma_zone_t beio_zone; 294a0e36aeeSEdward Tomasz Napierala 295130f4520SKenneth D. Merry static struct ctl_be_block_io * 296130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc) 297130f4520SKenneth D. Merry { 298130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 299130f4520SKenneth D. Merry 300a0e36aeeSEdward Tomasz Napierala beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 301130f4520SKenneth D. Merry beio->softc = softc; 302130f4520SKenneth D. Merry return (beio); 303130f4520SKenneth D. Merry } 304130f4520SKenneth D. Merry 305130f4520SKenneth D. Merry static void 306130f4520SKenneth D. Merry ctl_free_beio(struct ctl_be_block_io *beio) 307130f4520SKenneth D. Merry { 308130f4520SKenneth D. Merry int duplicate_free; 309130f4520SKenneth D. Merry int i; 310130f4520SKenneth D. Merry 311130f4520SKenneth D. Merry duplicate_free = 0; 312130f4520SKenneth D. Merry 313130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 314130f4520SKenneth D. Merry if (beio->sg_segs[i].addr == NULL) 315130f4520SKenneth D. Merry duplicate_free++; 316130f4520SKenneth D. Merry 317130f4520SKenneth D. Merry uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 318130f4520SKenneth D. Merry beio->sg_segs[i].addr = NULL; 319*11b569f7SAlexander Motin 320*11b569f7SAlexander Motin /* For compare we had two equal S/G lists. */ 321*11b569f7SAlexander Motin if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 322*11b569f7SAlexander Motin uma_zfree(beio->lun->lun_zone, 323*11b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 324*11b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 325*11b569f7SAlexander Motin } 326130f4520SKenneth D. Merry } 327130f4520SKenneth D. Merry 328130f4520SKenneth D. Merry if (duplicate_free > 0) { 329130f4520SKenneth D. Merry printf("%s: %d duplicate frees out of %d segments\n", __func__, 330130f4520SKenneth D. Merry duplicate_free, beio->num_segs); 331130f4520SKenneth D. Merry } 332a0e36aeeSEdward Tomasz Napierala 333a0e36aeeSEdward Tomasz Napierala uma_zfree(beio_zone, beio); 334130f4520SKenneth D. Merry } 335130f4520SKenneth D. Merry 336130f4520SKenneth D. Merry static void 337130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio) 338130f4520SKenneth D. Merry { 339130f4520SKenneth D. Merry union ctl_io *io; 340130f4520SKenneth D. Merry int io_len; 341130f4520SKenneth D. Merry 342130f4520SKenneth D. Merry io = beio->io; 343130f4520SKenneth D. Merry 344130f4520SKenneth D. Merry if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 345130f4520SKenneth D. Merry io_len = beio->io_len; 346130f4520SKenneth D. Merry else 347130f4520SKenneth D. Merry io_len = 0; 348130f4520SKenneth D. Merry 349130f4520SKenneth D. Merry devstat_end_transaction(beio->lun->disk_stats, 350130f4520SKenneth D. Merry /*bytes*/ io_len, 351130f4520SKenneth D. Merry beio->ds_tag_type, 352130f4520SKenneth D. Merry beio->ds_trans_type, 353130f4520SKenneth D. Merry /*now*/ NULL, 354130f4520SKenneth D. Merry /*then*/&beio->ds_t0); 355130f4520SKenneth D. Merry 356ee7f31c0SAlexander Motin if (beio->beio_cont != NULL) { 357ee7f31c0SAlexander Motin beio->beio_cont(beio); 358ee7f31c0SAlexander Motin } else { 359130f4520SKenneth D. Merry ctl_free_beio(beio); 360*11b569f7SAlexander Motin ctl_data_submit_done(io); 361130f4520SKenneth D. Merry } 362ee7f31c0SAlexander Motin } 363130f4520SKenneth D. Merry 364130f4520SKenneth D. Merry static int 365130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io) 366130f4520SKenneth D. Merry { 367130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 368130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 369*11b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 370130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 371130f4520SKenneth D. Merry struct bintime cur_bt; 372130f4520SKenneth D. Merry #endif 373*11b569f7SAlexander Motin int i; 374130f4520SKenneth D. Merry 375e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 376130f4520SKenneth D. Merry be_lun = beio->lun; 377130f4520SKenneth D. Merry 378130f4520SKenneth D. Merry DPRINTF("entered\n"); 379130f4520SKenneth D. Merry 380130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 381130f4520SKenneth D. Merry getbintime(&cur_bt); 382130f4520SKenneth D. Merry bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 383130f4520SKenneth D. Merry bintime_add(&io->io_hdr.dma_bt, &cur_bt); 384130f4520SKenneth D. Merry io->io_hdr.num_dmas++; 385130f4520SKenneth D. Merry #endif 386*11b569f7SAlexander Motin io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 387130f4520SKenneth D. Merry 388130f4520SKenneth D. Merry /* 389130f4520SKenneth D. Merry * We set status at this point for read commands, and write 390130f4520SKenneth D. Merry * commands with errors. 391130f4520SKenneth D. Merry */ 392*11b569f7SAlexander Motin if ((io->io_hdr.port_status == 0) && 393*11b569f7SAlexander Motin ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) && 394*11b569f7SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 395*11b569f7SAlexander Motin lbalen = ARGS(beio->io); 396*11b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_READ) { 397130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 398*11b569f7SAlexander Motin } else if (lbalen->flags & CTL_LLF_COMPARE) { 399*11b569f7SAlexander Motin /* We have two data blocks ready for comparison. */ 400*11b569f7SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 401*11b569f7SAlexander Motin if (memcmp(beio->sg_segs[i].addr, 402*11b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 403*11b569f7SAlexander Motin beio->sg_segs[i].len) != 0) 404*11b569f7SAlexander Motin break; 405*11b569f7SAlexander Motin } 406*11b569f7SAlexander Motin if (i < beio->num_segs) 407*11b569f7SAlexander Motin ctl_set_sense(&io->scsiio, 408*11b569f7SAlexander Motin /*current_error*/ 1, 409*11b569f7SAlexander Motin /*sense_key*/ SSD_KEY_MISCOMPARE, 410*11b569f7SAlexander Motin /*asc*/ 0x1D, 411*11b569f7SAlexander Motin /*ascq*/ 0x00, 412*11b569f7SAlexander Motin SSD_ELEM_NONE); 413*11b569f7SAlexander Motin else 414*11b569f7SAlexander Motin ctl_set_success(&io->scsiio); 415*11b569f7SAlexander Motin } 416*11b569f7SAlexander Motin } 417130f4520SKenneth D. Merry else if ((io->io_hdr.port_status != 0) 418130f4520SKenneth D. Merry && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 419130f4520SKenneth D. Merry && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 420130f4520SKenneth D. Merry /* 421130f4520SKenneth D. Merry * For hardware error sense keys, the sense key 422130f4520SKenneth D. Merry * specific value is defined to be a retry count, 423130f4520SKenneth D. Merry * but we use it to pass back an internal FETD 424130f4520SKenneth D. Merry * error code. XXX KDM Hopefully the FETD is only 425130f4520SKenneth D. Merry * using 16 bits for an error code, since that's 426130f4520SKenneth D. Merry * all the space we have in the sks field. 427130f4520SKenneth D. Merry */ 428130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 429130f4520SKenneth D. Merry /*sks_valid*/ 1, 430130f4520SKenneth D. Merry /*retry_count*/ 431130f4520SKenneth D. Merry io->io_hdr.port_status); 432130f4520SKenneth D. Merry } 433130f4520SKenneth D. Merry 434130f4520SKenneth D. Merry /* 435130f4520SKenneth D. Merry * If this is a read, or a write with errors, it is done. 436130f4520SKenneth D. Merry */ 437130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_READ) 438130f4520SKenneth D. Merry || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 439130f4520SKenneth D. Merry || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 440130f4520SKenneth D. Merry ctl_complete_beio(beio); 441130f4520SKenneth D. Merry return (0); 442130f4520SKenneth D. Merry } 443130f4520SKenneth D. Merry 444130f4520SKenneth D. Merry /* 445130f4520SKenneth D. Merry * At this point, we have a write and the DMA completed 446130f4520SKenneth D. Merry * successfully. We now have to queue it to the task queue to 447130f4520SKenneth D. Merry * execute the backend I/O. That is because we do blocking 448130f4520SKenneth D. Merry * memory allocations, and in the file backing case, blocking I/O. 449130f4520SKenneth D. Merry * This move done routine is generally called in the SIM's 450130f4520SKenneth D. Merry * interrupt context, and therefore we cannot block. 451130f4520SKenneth D. Merry */ 452130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 453130f4520SKenneth D. Merry /* 454130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 455130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 456130f4520SKenneth D. Merry * or deal with resource allocation here. 457130f4520SKenneth D. Merry */ 458130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 459130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 460130f4520SKenneth D. Merry 461130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 462130f4520SKenneth D. Merry 463130f4520SKenneth D. Merry return (0); 464130f4520SKenneth D. Merry } 465130f4520SKenneth D. Merry 466130f4520SKenneth D. Merry static void 467130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio) 468130f4520SKenneth D. Merry { 469130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 470130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 471130f4520SKenneth D. Merry union ctl_io *io; 472e0c2f975SAlexander Motin int error; 473130f4520SKenneth D. Merry 474130f4520SKenneth D. Merry beio = bio->bio_caller1; 475130f4520SKenneth D. Merry be_lun = beio->lun; 476130f4520SKenneth D. Merry io = beio->io; 477130f4520SKenneth D. Merry 478130f4520SKenneth D. Merry DPRINTF("entered\n"); 479130f4520SKenneth D. Merry 480e0c2f975SAlexander Motin error = bio->bio_error; 481130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 482e0c2f975SAlexander Motin if (error != 0) 483130f4520SKenneth D. Merry beio->num_errors++; 484130f4520SKenneth D. Merry 485130f4520SKenneth D. Merry beio->num_bios_done++; 486130f4520SKenneth D. Merry 487130f4520SKenneth D. Merry /* 488130f4520SKenneth D. Merry * XXX KDM will this cause WITNESS to complain? Holding a lock 489130f4520SKenneth D. Merry * during the free might cause it to complain. 490130f4520SKenneth D. Merry */ 491130f4520SKenneth D. Merry g_destroy_bio(bio); 492130f4520SKenneth D. Merry 493130f4520SKenneth D. Merry /* 494130f4520SKenneth D. Merry * If the send complete bit isn't set, or we aren't the last I/O to 495130f4520SKenneth D. Merry * complete, then we're done. 496130f4520SKenneth D. Merry */ 497130f4520SKenneth D. Merry if ((beio->send_complete == 0) 498130f4520SKenneth D. Merry || (beio->num_bios_done < beio->num_bios_sent)) { 499130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 500130f4520SKenneth D. Merry return; 501130f4520SKenneth D. Merry } 502130f4520SKenneth D. Merry 503130f4520SKenneth D. Merry /* 504130f4520SKenneth D. Merry * At this point, we've verified that we are the last I/O to 505130f4520SKenneth D. Merry * complete, so it's safe to drop the lock. 506130f4520SKenneth D. Merry */ 507130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 508130f4520SKenneth D. Merry 509130f4520SKenneth D. Merry /* 510130f4520SKenneth D. Merry * If there are any errors from the backing device, we fail the 511130f4520SKenneth D. Merry * entire I/O with a medium error. 512130f4520SKenneth D. Merry */ 513130f4520SKenneth D. Merry if (beio->num_errors > 0) { 514e0c2f975SAlexander Motin if (error == EOPNOTSUPP) { 515e0c2f975SAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 516e0c2f975SAlexander Motin } else if (beio->bio_cmd == BIO_FLUSH) { 517130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 518130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 519130f4520SKenneth D. Merry /*sks_valid*/ 1, 520130f4520SKenneth D. Merry /*retry_count*/ 0xbad2); 521130f4520SKenneth D. Merry } else 522130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 523130f4520SKenneth D. Merry ctl_complete_beio(beio); 524130f4520SKenneth D. Merry return; 525130f4520SKenneth D. Merry } 526130f4520SKenneth D. Merry 527130f4520SKenneth D. Merry /* 528*11b569f7SAlexander Motin * If this is a write, a flush, a delete or verify, we're all done. 529130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 530130f4520SKenneth D. Merry */ 531130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_WRITE) 532ee7f31c0SAlexander Motin || (beio->bio_cmd == BIO_FLUSH) 533*11b569f7SAlexander Motin || (beio->bio_cmd == BIO_DELETE) 534*11b569f7SAlexander Motin || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 535130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 536130f4520SKenneth D. Merry ctl_complete_beio(beio); 537130f4520SKenneth D. Merry } else { 538130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 539130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 540130f4520SKenneth D. Merry #endif 541130f4520SKenneth D. Merry ctl_datamove(io); 542130f4520SKenneth D. Merry } 543130f4520SKenneth D. Merry } 544130f4520SKenneth D. Merry 545130f4520SKenneth D. Merry static void 546130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 547130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 548130f4520SKenneth D. Merry { 549130f4520SKenneth D. Merry union ctl_io *io; 550130f4520SKenneth D. Merry struct mount *mountpoint; 5515050aa86SKonstantin Belousov int error, lock_flags; 552130f4520SKenneth D. Merry 553130f4520SKenneth D. Merry DPRINTF("entered\n"); 554130f4520SKenneth D. Merry 555130f4520SKenneth D. Merry io = beio->io; 556130f4520SKenneth D. Merry 557130f4520SKenneth D. Merry (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 558130f4520SKenneth D. Merry 559130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 560130f4520SKenneth D. Merry || ((mountpoint == NULL) 561130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 562130f4520SKenneth D. Merry lock_flags = LK_SHARED; 563130f4520SKenneth D. Merry else 564130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 565130f4520SKenneth D. Merry 566130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 567130f4520SKenneth D. Merry 568130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 569130f4520SKenneth D. Merry devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 570130f4520SKenneth D. Merry 571130f4520SKenneth D. Merry error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 572130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 573130f4520SKenneth D. Merry 574130f4520SKenneth D. Merry vn_finished_write(mountpoint); 575130f4520SKenneth D. Merry 576130f4520SKenneth D. Merry if (error == 0) 577130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 578130f4520SKenneth D. Merry else { 579130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 580130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 581130f4520SKenneth D. Merry /*sks_valid*/ 1, 582130f4520SKenneth D. Merry /*retry_count*/ 0xbad1); 583130f4520SKenneth D. Merry } 584130f4520SKenneth D. Merry 585130f4520SKenneth D. Merry ctl_complete_beio(beio); 586130f4520SKenneth D. Merry } 587130f4520SKenneth D. Merry 588d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 589d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 590d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 591d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 592130f4520SKenneth D. Merry 593130f4520SKenneth D. Merry static void 594130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 595130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 596130f4520SKenneth D. Merry { 597130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 598130f4520SKenneth D. Merry union ctl_io *io; 599130f4520SKenneth D. Merry struct uio xuio; 600130f4520SKenneth D. Merry struct iovec *xiovec; 6015050aa86SKonstantin Belousov int flags; 602130f4520SKenneth D. Merry int error, i; 603130f4520SKenneth D. Merry 604130f4520SKenneth D. Merry DPRINTF("entered\n"); 605130f4520SKenneth D. Merry 606130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 607130f4520SKenneth D. Merry io = beio->io; 608130f4520SKenneth D. Merry flags = beio->bio_flags; 609130f4520SKenneth D. Merry 610*11b569f7SAlexander Motin bzero(&xuio, sizeof(xuio)); 611130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 612130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 613*11b569f7SAlexander Motin xuio.uio_rw = UIO_READ; 614130f4520SKenneth D. Merry } else { 615130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 616130f4520SKenneth D. Merry xuio.uio_rw = UIO_WRITE; 617*11b569f7SAlexander Motin } 618130f4520SKenneth D. Merry xuio.uio_offset = beio->io_offset; 619130f4520SKenneth D. Merry xuio.uio_resid = beio->io_len; 620130f4520SKenneth D. Merry xuio.uio_segflg = UIO_SYSSPACE; 621130f4520SKenneth D. Merry xuio.uio_iov = beio->xiovecs; 622130f4520SKenneth D. Merry xuio.uio_iovcnt = beio->num_segs; 623130f4520SKenneth D. Merry xuio.uio_td = curthread; 624130f4520SKenneth D. Merry 625130f4520SKenneth D. Merry for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 626130f4520SKenneth D. Merry xiovec->iov_base = beio->sg_segs[i].addr; 627130f4520SKenneth D. Merry xiovec->iov_len = beio->sg_segs[i].len; 628130f4520SKenneth D. Merry } 629130f4520SKenneth D. Merry 630130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 631130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 632130f4520SKenneth D. Merry 633130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 634130f4520SKenneth D. Merry devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 635130f4520SKenneth D. Merry 636130f4520SKenneth D. Merry /* 637130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for reads. If the 638130f4520SKenneth D. Merry * DIRECTIO option is configured into the kernel, it calls 639130f4520SKenneth D. Merry * ffs_rawread(). But that only works for single-segment 640130f4520SKenneth D. Merry * uios with user space addresses. In our case, with a 641130f4520SKenneth D. Merry * kernel uio, it still reads into the buffer cache, but it 642130f4520SKenneth D. Merry * will just try to release the buffer from the cache later 643130f4520SKenneth D. Merry * on in ffs_read(). 644130f4520SKenneth D. Merry * 645130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for reads. 646130f4520SKenneth D. Merry * 647130f4520SKenneth D. Merry * UFS does not pay attention to IO_SYNC for reads. 648130f4520SKenneth D. Merry * 649130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (which translates into the 650130f4520SKenneth D. Merry * Solaris define FRSYNC for zfs_read()) for reads. It 651130f4520SKenneth D. Merry * attempts to sync the file before reading. 652130f4520SKenneth D. Merry * 653130f4520SKenneth D. Merry * So, to attempt to provide some barrier semantics in the 654130f4520SKenneth D. Merry * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 655130f4520SKenneth D. Merry */ 656130f4520SKenneth D. Merry error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 657130f4520SKenneth D. Merry (IO_DIRECT|IO_SYNC) : 0, file_data->cred); 658130f4520SKenneth D. Merry 659130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 660*11b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 661130f4520SKenneth D. Merry } else { 662130f4520SKenneth D. Merry struct mount *mountpoint; 663130f4520SKenneth D. Merry int lock_flags; 664130f4520SKenneth D. Merry 665130f4520SKenneth D. Merry (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 666130f4520SKenneth D. Merry 667130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 668130f4520SKenneth D. Merry || ((mountpoint == NULL) 669130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 670130f4520SKenneth D. Merry lock_flags = LK_SHARED; 671130f4520SKenneth D. Merry else 672130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 673130f4520SKenneth D. Merry 674130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 675130f4520SKenneth D. Merry 676130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 677130f4520SKenneth D. Merry devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 678130f4520SKenneth D. Merry 679130f4520SKenneth D. Merry /* 680130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for writes. The write 681130f4520SKenneth D. Merry * is done asynchronously. (Normally the write would just 682130f4520SKenneth D. Merry * get put into cache. 683130f4520SKenneth D. Merry * 684130f4520SKenneth D. Merry * UFS pays attention to IO_SYNC for writes. It will 685130f4520SKenneth D. Merry * attempt to write the buffer out synchronously if that 686130f4520SKenneth D. Merry * flag is set. 687130f4520SKenneth D. Merry * 688130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for writes. 689130f4520SKenneth D. Merry * 690130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 691130f4520SKenneth D. Merry * for writes. It will flush the transaction from the 692130f4520SKenneth D. Merry * cache before returning. 693130f4520SKenneth D. Merry * 694130f4520SKenneth D. Merry * So if we've got the BIO_ORDERED flag set, we want 695130f4520SKenneth D. Merry * IO_SYNC in either the UFS or ZFS case. 696130f4520SKenneth D. Merry */ 697130f4520SKenneth D. Merry error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 698130f4520SKenneth D. Merry IO_SYNC : 0, file_data->cred); 699130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 700130f4520SKenneth D. Merry 701130f4520SKenneth D. Merry vn_finished_write(mountpoint); 702*11b569f7SAlexander Motin SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 703130f4520SKenneth D. Merry } 704130f4520SKenneth D. Merry 705130f4520SKenneth D. Merry /* 706130f4520SKenneth D. Merry * If we got an error, set the sense data to "MEDIUM ERROR" and 707130f4520SKenneth D. Merry * return the I/O to the user. 708130f4520SKenneth D. Merry */ 709130f4520SKenneth D. Merry if (error != 0) { 710130f4520SKenneth D. Merry char path_str[32]; 711130f4520SKenneth D. Merry 712130f4520SKenneth D. Merry ctl_scsi_path_string(io, path_str, sizeof(path_str)); 713130f4520SKenneth D. Merry /* 714130f4520SKenneth D. Merry * XXX KDM ZFS returns ENOSPC when the underlying 715130f4520SKenneth D. Merry * filesystem fills up. What kind of SCSI error should we 716130f4520SKenneth D. Merry * return for that? 717130f4520SKenneth D. Merry */ 718130f4520SKenneth D. Merry printf("%s%s command returned errno %d\n", path_str, 719130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 720130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 721130f4520SKenneth D. Merry ctl_complete_beio(beio); 722130f4520SKenneth D. Merry return; 723130f4520SKenneth D. Merry } 724130f4520SKenneth D. Merry 725130f4520SKenneth D. Merry /* 726130f4520SKenneth D. Merry * If this is a write, we're all done. 727130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 728130f4520SKenneth D. Merry */ 729*11b569f7SAlexander Motin if (ARGS(io)->flags & (CTL_LLF_WRITE | CTL_LLF_VERIFY)) { 730130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 731130f4520SKenneth D. Merry ctl_complete_beio(beio); 732130f4520SKenneth D. Merry } else { 733130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 734130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 735130f4520SKenneth D. Merry #endif 736130f4520SKenneth D. Merry ctl_datamove(io); 737130f4520SKenneth D. Merry } 738130f4520SKenneth D. Merry } 739130f4520SKenneth D. Merry 740130f4520SKenneth D. Merry static void 741130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 742130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 743130f4520SKenneth D. Merry { 744130f4520SKenneth D. Merry struct bio *bio; 745130f4520SKenneth D. Merry union ctl_io *io; 746130f4520SKenneth D. Merry struct ctl_be_block_devdata *dev_data; 747130f4520SKenneth D. Merry 748130f4520SKenneth D. Merry dev_data = &be_lun->backend.dev; 749130f4520SKenneth D. Merry io = beio->io; 750130f4520SKenneth D. Merry 751130f4520SKenneth D. Merry DPRINTF("entered\n"); 752130f4520SKenneth D. Merry 753130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 754130f4520SKenneth D. Merry bio = g_alloc_bio(); 755130f4520SKenneth D. Merry 756130f4520SKenneth D. Merry bio->bio_cmd = BIO_FLUSH; 757130f4520SKenneth D. Merry bio->bio_flags |= BIO_ORDERED; 758130f4520SKenneth D. Merry bio->bio_dev = dev_data->cdev; 759130f4520SKenneth D. Merry bio->bio_offset = 0; 760130f4520SKenneth D. Merry bio->bio_data = 0; 761130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 762130f4520SKenneth D. Merry bio->bio_caller1 = beio; 763130f4520SKenneth D. Merry bio->bio_pblkno = 0; 764130f4520SKenneth D. Merry 765130f4520SKenneth D. Merry /* 766130f4520SKenneth D. Merry * We don't need to acquire the LUN lock here, because we are only 767130f4520SKenneth D. Merry * sending one bio, and so there is no other context to synchronize 768130f4520SKenneth D. Merry * with. 769130f4520SKenneth D. Merry */ 770130f4520SKenneth D. Merry beio->num_bios_sent = 1; 771130f4520SKenneth D. Merry beio->send_complete = 1; 772130f4520SKenneth D. Merry 773130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 774130f4520SKenneth D. Merry devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 775130f4520SKenneth D. Merry 776130f4520SKenneth D. Merry (*dev_data->csw->d_strategy)(bio); 777130f4520SKenneth D. Merry } 778130f4520SKenneth D. Merry 779130f4520SKenneth D. Merry static void 780ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 781ee7f31c0SAlexander Motin struct ctl_be_block_io *beio, 782ee7f31c0SAlexander Motin uint64_t off, uint64_t len, int last) 783ee7f31c0SAlexander Motin { 784ee7f31c0SAlexander Motin struct bio *bio; 785ee7f31c0SAlexander Motin struct ctl_be_block_devdata *dev_data; 7868f5a226aSAlexander Motin uint64_t maxlen; 787ee7f31c0SAlexander Motin 788ee7f31c0SAlexander Motin dev_data = &be_lun->backend.dev; 7898f5a226aSAlexander Motin maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 790ee7f31c0SAlexander Motin while (len > 0) { 791ee7f31c0SAlexander Motin bio = g_alloc_bio(); 792ee7f31c0SAlexander Motin bio->bio_cmd = BIO_DELETE; 793ee7f31c0SAlexander Motin bio->bio_flags |= beio->bio_flags; 794ee7f31c0SAlexander Motin bio->bio_dev = dev_data->cdev; 795ee7f31c0SAlexander Motin bio->bio_offset = off; 7968f5a226aSAlexander Motin bio->bio_length = MIN(len, maxlen); 797ee7f31c0SAlexander Motin bio->bio_data = 0; 798ee7f31c0SAlexander Motin bio->bio_done = ctl_be_block_biodone; 799ee7f31c0SAlexander Motin bio->bio_caller1 = beio; 8008f5a226aSAlexander Motin bio->bio_pblkno = off / be_lun->blocksize; 801ee7f31c0SAlexander Motin 802ee7f31c0SAlexander Motin off += bio->bio_length; 803ee7f31c0SAlexander Motin len -= bio->bio_length; 804ee7f31c0SAlexander Motin 805ee7f31c0SAlexander Motin mtx_lock(&be_lun->lock); 806ee7f31c0SAlexander Motin beio->num_bios_sent++; 807ee7f31c0SAlexander Motin if (last && len == 0) 808ee7f31c0SAlexander Motin beio->send_complete = 1; 809ee7f31c0SAlexander Motin mtx_unlock(&be_lun->lock); 810ee7f31c0SAlexander Motin 811ee7f31c0SAlexander Motin (*dev_data->csw->d_strategy)(bio); 812ee7f31c0SAlexander Motin } 813ee7f31c0SAlexander Motin } 814ee7f31c0SAlexander Motin 815ee7f31c0SAlexander Motin static void 816ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 817ee7f31c0SAlexander Motin struct ctl_be_block_io *beio) 818ee7f31c0SAlexander Motin { 819ee7f31c0SAlexander Motin union ctl_io *io; 820ee7f31c0SAlexander Motin struct ctl_be_block_devdata *dev_data; 82166df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 822ee7f31c0SAlexander Motin struct scsi_unmap_desc *buf, *end; 823ee7f31c0SAlexander Motin uint64_t len; 824ee7f31c0SAlexander Motin 825ee7f31c0SAlexander Motin dev_data = &be_lun->backend.dev; 826ee7f31c0SAlexander Motin io = beio->io; 827ee7f31c0SAlexander Motin 828ee7f31c0SAlexander Motin DPRINTF("entered\n"); 829ee7f31c0SAlexander Motin 830ee7f31c0SAlexander Motin binuptime(&beio->ds_t0); 831ee7f31c0SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 832ee7f31c0SAlexander Motin 833ee7f31c0SAlexander Motin if (beio->io_offset == -1) { 834ee7f31c0SAlexander Motin beio->io_len = 0; 83566df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 83666df9136SAlexander Motin buf = (struct scsi_unmap_desc *)ptrlen->ptr; 83766df9136SAlexander Motin end = buf + ptrlen->len / sizeof(*buf); 838ee7f31c0SAlexander Motin for (; buf < end; buf++) { 839ee7f31c0SAlexander Motin len = (uint64_t)scsi_4btoul(buf->length) * 840ee7f31c0SAlexander Motin be_lun->blocksize; 841ee7f31c0SAlexander Motin beio->io_len += len; 842ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 843ee7f31c0SAlexander Motin scsi_8btou64(buf->lba) * be_lun->blocksize, len, 8447e0be022SAlexander Motin (end - buf < 2) ? TRUE : FALSE); 845ee7f31c0SAlexander Motin } 846ee7f31c0SAlexander Motin } else 847ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 848ee7f31c0SAlexander Motin beio->io_offset, beio->io_len, TRUE); 849ee7f31c0SAlexander Motin } 850ee7f31c0SAlexander Motin 851ee7f31c0SAlexander Motin static void 852130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 853130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 854130f4520SKenneth D. Merry { 855130f4520SKenneth D. Merry int i; 856130f4520SKenneth D. Merry struct bio *bio; 857130f4520SKenneth D. Merry struct ctl_be_block_devdata *dev_data; 858130f4520SKenneth D. Merry off_t cur_offset; 859130f4520SKenneth D. Merry int max_iosize; 860130f4520SKenneth D. Merry 861130f4520SKenneth D. Merry DPRINTF("entered\n"); 862130f4520SKenneth D. Merry 863130f4520SKenneth D. Merry dev_data = &be_lun->backend.dev; 864130f4520SKenneth D. Merry 865130f4520SKenneth D. Merry /* 866130f4520SKenneth D. Merry * We have to limit our I/O size to the maximum supported by the 867130f4520SKenneth D. Merry * backend device. Hopefully it is MAXPHYS. If the driver doesn't 868130f4520SKenneth D. Merry * set it properly, use DFLTPHYS. 869130f4520SKenneth D. Merry */ 870130f4520SKenneth D. Merry max_iosize = dev_data->cdev->si_iosize_max; 871130f4520SKenneth D. Merry if (max_iosize < PAGE_SIZE) 872130f4520SKenneth D. Merry max_iosize = DFLTPHYS; 873130f4520SKenneth D. Merry 874130f4520SKenneth D. Merry cur_offset = beio->io_offset; 875130f4520SKenneth D. Merry 876130f4520SKenneth D. Merry /* 877130f4520SKenneth D. Merry * XXX KDM need to accurately reflect the number of I/Os outstanding 878130f4520SKenneth D. Merry * to a device. 879130f4520SKenneth D. Merry */ 880130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 881130f4520SKenneth D. Merry devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 882130f4520SKenneth D. Merry 883130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 884130f4520SKenneth D. Merry size_t cur_size; 885130f4520SKenneth D. Merry uint8_t *cur_ptr; 886130f4520SKenneth D. Merry 887130f4520SKenneth D. Merry cur_size = beio->sg_segs[i].len; 888130f4520SKenneth D. Merry cur_ptr = beio->sg_segs[i].addr; 889130f4520SKenneth D. Merry 890130f4520SKenneth D. Merry while (cur_size > 0) { 891130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 892130f4520SKenneth D. Merry bio = g_alloc_bio(); 893130f4520SKenneth D. Merry 894130f4520SKenneth D. Merry KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 895130f4520SKenneth D. Merry 896130f4520SKenneth D. Merry bio->bio_cmd = beio->bio_cmd; 897130f4520SKenneth D. Merry bio->bio_flags |= beio->bio_flags; 898130f4520SKenneth D. Merry bio->bio_dev = dev_data->cdev; 899130f4520SKenneth D. Merry bio->bio_caller1 = beio; 900130f4520SKenneth D. Merry bio->bio_length = min(cur_size, max_iosize); 901130f4520SKenneth D. Merry bio->bio_offset = cur_offset; 902130f4520SKenneth D. Merry bio->bio_data = cur_ptr; 903130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 904130f4520SKenneth D. Merry bio->bio_pblkno = cur_offset / be_lun->blocksize; 905130f4520SKenneth D. Merry 906130f4520SKenneth D. Merry cur_offset += bio->bio_length; 907130f4520SKenneth D. Merry cur_ptr += bio->bio_length; 908130f4520SKenneth D. Merry cur_size -= bio->bio_length; 909130f4520SKenneth D. Merry 910130f4520SKenneth D. Merry /* 911130f4520SKenneth D. Merry * Make sure we set the complete bit just before we 912130f4520SKenneth D. Merry * issue the last bio so we don't wind up with a 913130f4520SKenneth D. Merry * race. 914130f4520SKenneth D. Merry * 915130f4520SKenneth D. Merry * Use the LUN mutex here instead of a combination 916130f4520SKenneth D. Merry * of atomic variables for simplicity. 917130f4520SKenneth D. Merry * 918130f4520SKenneth D. Merry * XXX KDM we could have a per-IO lock, but that 919130f4520SKenneth D. Merry * would cause additional per-IO setup and teardown 920130f4520SKenneth D. Merry * overhead. Hopefully there won't be too much 921130f4520SKenneth D. Merry * contention on the LUN lock. 922130f4520SKenneth D. Merry */ 923130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 924130f4520SKenneth D. Merry 925130f4520SKenneth D. Merry beio->num_bios_sent++; 926130f4520SKenneth D. Merry 927130f4520SKenneth D. Merry if ((i == beio->num_segs - 1) 928130f4520SKenneth D. Merry && (cur_size == 0)) 929130f4520SKenneth D. Merry beio->send_complete = 1; 930130f4520SKenneth D. Merry 931130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 932130f4520SKenneth D. Merry 933130f4520SKenneth D. Merry (*dev_data->csw->d_strategy)(bio); 934130f4520SKenneth D. Merry } 935130f4520SKenneth D. Merry } 936130f4520SKenneth D. Merry } 937130f4520SKenneth D. Merry 938130f4520SKenneth D. Merry static void 939ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 940ee7f31c0SAlexander Motin { 941ee7f31c0SAlexander Motin union ctl_io *io; 942ee7f31c0SAlexander Motin 943ee7f31c0SAlexander Motin io = beio->io; 944ee7f31c0SAlexander Motin ctl_free_beio(beio); 945ee7f31c0SAlexander Motin if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 946ee7f31c0SAlexander Motin && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 947ee7f31c0SAlexander Motin ctl_config_write_done(io); 948ee7f31c0SAlexander Motin return; 949ee7f31c0SAlexander Motin } 950ee7f31c0SAlexander Motin 951ee7f31c0SAlexander Motin ctl_be_block_config_write(io); 952ee7f31c0SAlexander Motin } 953ee7f31c0SAlexander Motin 954ee7f31c0SAlexander Motin static void 955ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 956ee7f31c0SAlexander Motin union ctl_io *io) 957ee7f31c0SAlexander Motin { 958ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 959ee7f31c0SAlexander Motin struct ctl_be_block_softc *softc; 96066df9136SAlexander Motin struct ctl_lba_len_flags *lbalen; 961ee7f31c0SAlexander Motin uint64_t len_left, lba; 962ee7f31c0SAlexander Motin int i, seglen; 963ee7f31c0SAlexander Motin uint8_t *buf, *end; 964ee7f31c0SAlexander Motin 965ee7f31c0SAlexander Motin DPRINTF("entered\n"); 966ee7f31c0SAlexander Motin 967e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 968ee7f31c0SAlexander Motin softc = be_lun->softc; 969*11b569f7SAlexander Motin lbalen = ARGS(beio->io); 970ee7f31c0SAlexander Motin 97166df9136SAlexander Motin if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP) || 97266df9136SAlexander Motin (lbalen->flags & SWS_UNMAP && be_lun->unmap == NULL)) { 973ee7f31c0SAlexander Motin ctl_free_beio(beio); 974ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 975ee7f31c0SAlexander Motin /*sks_valid*/ 1, 976ee7f31c0SAlexander Motin /*command*/ 1, 977ee7f31c0SAlexander Motin /*field*/ 1, 978ee7f31c0SAlexander Motin /*bit_valid*/ 0, 979ee7f31c0SAlexander Motin /*bit*/ 0); 980ee7f31c0SAlexander Motin ctl_config_write_done(io); 981ee7f31c0SAlexander Motin return; 982ee7f31c0SAlexander Motin } 983ee7f31c0SAlexander Motin 984ee7f31c0SAlexander Motin /* 985ee7f31c0SAlexander Motin * If the I/O came down with an ordered or head of queue tag, set 986ee7f31c0SAlexander Motin * the BIO_ORDERED attribute. For head of queue tags, that's 987ee7f31c0SAlexander Motin * pretty much the best we can do. 988ee7f31c0SAlexander Motin */ 989ee7f31c0SAlexander Motin if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 990ee7f31c0SAlexander Motin || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 991ee7f31c0SAlexander Motin beio->bio_flags = BIO_ORDERED; 992ee7f31c0SAlexander Motin 993ee7f31c0SAlexander Motin switch (io->scsiio.tag_type) { 994ee7f31c0SAlexander Motin case CTL_TAG_ORDERED: 995ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 996ee7f31c0SAlexander Motin break; 997ee7f31c0SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 998ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 999ee7f31c0SAlexander Motin break; 1000ee7f31c0SAlexander Motin case CTL_TAG_UNTAGGED: 1001ee7f31c0SAlexander Motin case CTL_TAG_SIMPLE: 1002ee7f31c0SAlexander Motin case CTL_TAG_ACA: 1003ee7f31c0SAlexander Motin default: 1004ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1005ee7f31c0SAlexander Motin break; 1006ee7f31c0SAlexander Motin } 1007ee7f31c0SAlexander Motin 100866df9136SAlexander Motin if (lbalen->flags & SWS_UNMAP) { 100966df9136SAlexander Motin beio->io_offset = lbalen->lba * be_lun->blocksize; 101066df9136SAlexander Motin beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1011ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1012ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1013ee7f31c0SAlexander Motin 1014ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1015ee7f31c0SAlexander Motin return; 1016ee7f31c0SAlexander Motin } 1017ee7f31c0SAlexander Motin 1018ee7f31c0SAlexander Motin beio->bio_cmd = BIO_WRITE; 1019ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_WRITE; 1020ee7f31c0SAlexander Motin 1021ee7f31c0SAlexander Motin DPRINTF("WRITE SAME at LBA %jx len %u\n", 102266df9136SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len); 1023ee7f31c0SAlexander Motin 102466df9136SAlexander Motin len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1025ee7f31c0SAlexander Motin for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1026ee7f31c0SAlexander Motin 1027ee7f31c0SAlexander Motin /* 1028ee7f31c0SAlexander Motin * Setup the S/G entry for this chunk. 1029ee7f31c0SAlexander Motin */ 103008a7cce5SAlexander Motin seglen = MIN(CTLBLK_MAX_SEG, len_left); 1031ee7f31c0SAlexander Motin seglen -= seglen % be_lun->blocksize; 1032ee7f31c0SAlexander Motin beio->sg_segs[i].len = seglen; 1033ee7f31c0SAlexander Motin beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1034ee7f31c0SAlexander Motin 1035ee7f31c0SAlexander Motin DPRINTF("segment %d addr %p len %zd\n", i, 1036ee7f31c0SAlexander Motin beio->sg_segs[i].addr, beio->sg_segs[i].len); 1037ee7f31c0SAlexander Motin 1038ee7f31c0SAlexander Motin beio->num_segs++; 1039ee7f31c0SAlexander Motin len_left -= seglen; 1040ee7f31c0SAlexander Motin 1041ee7f31c0SAlexander Motin buf = beio->sg_segs[i].addr; 1042ee7f31c0SAlexander Motin end = buf + seglen; 1043ee7f31c0SAlexander Motin for (; buf < end; buf += be_lun->blocksize) { 1044ee7f31c0SAlexander Motin memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 104566df9136SAlexander Motin if (lbalen->flags & SWS_LBDATA) 104666df9136SAlexander Motin scsi_ulto4b(lbalen->lba + lba, buf); 1047ee7f31c0SAlexander Motin lba++; 1048ee7f31c0SAlexander Motin } 1049ee7f31c0SAlexander Motin } 1050ee7f31c0SAlexander Motin 105166df9136SAlexander Motin beio->io_offset = lbalen->lba * be_lun->blocksize; 1052ee7f31c0SAlexander Motin beio->io_len = lba * be_lun->blocksize; 1053ee7f31c0SAlexander Motin 1054ee7f31c0SAlexander Motin /* We can not do all in one run. Correct and schedule rerun. */ 1055ee7f31c0SAlexander Motin if (len_left > 0) { 105666df9136SAlexander Motin lbalen->lba += lba; 105766df9136SAlexander Motin lbalen->len -= lba; 1058ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done_ws; 1059ee7f31c0SAlexander Motin } 1060ee7f31c0SAlexander Motin 1061ee7f31c0SAlexander Motin be_lun->dispatch(be_lun, beio); 1062ee7f31c0SAlexander Motin } 1063ee7f31c0SAlexander Motin 1064ee7f31c0SAlexander Motin static void 1065ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1066ee7f31c0SAlexander Motin union ctl_io *io) 1067ee7f31c0SAlexander Motin { 1068ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 1069ee7f31c0SAlexander Motin struct ctl_be_block_softc *softc; 107066df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1071ee7f31c0SAlexander Motin 1072ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1073ee7f31c0SAlexander Motin 1074e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1075ee7f31c0SAlexander Motin softc = be_lun->softc; 107666df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1077ee7f31c0SAlexander Motin 107866df9136SAlexander Motin if (ptrlen->flags != 0 || be_lun->unmap == NULL) { 1079ee7f31c0SAlexander Motin ctl_free_beio(beio); 1080ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1081ee7f31c0SAlexander Motin /*sks_valid*/ 0, 1082ee7f31c0SAlexander Motin /*command*/ 1, 1083ee7f31c0SAlexander Motin /*field*/ 0, 1084ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1085ee7f31c0SAlexander Motin /*bit*/ 0); 1086ee7f31c0SAlexander Motin ctl_config_write_done(io); 1087ee7f31c0SAlexander Motin return; 1088ee7f31c0SAlexander Motin } 1089ee7f31c0SAlexander Motin 1090ee7f31c0SAlexander Motin /* 1091ee7f31c0SAlexander Motin * If the I/O came down with an ordered or head of queue tag, set 1092ee7f31c0SAlexander Motin * the BIO_ORDERED attribute. For head of queue tags, that's 1093ee7f31c0SAlexander Motin * pretty much the best we can do. 1094ee7f31c0SAlexander Motin */ 1095ee7f31c0SAlexander Motin if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1096ee7f31c0SAlexander Motin || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1097ee7f31c0SAlexander Motin beio->bio_flags = BIO_ORDERED; 1098ee7f31c0SAlexander Motin 1099ee7f31c0SAlexander Motin switch (io->scsiio.tag_type) { 1100ee7f31c0SAlexander Motin case CTL_TAG_ORDERED: 1101ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1102ee7f31c0SAlexander Motin break; 1103ee7f31c0SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 1104ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1105ee7f31c0SAlexander Motin break; 1106ee7f31c0SAlexander Motin case CTL_TAG_UNTAGGED: 1107ee7f31c0SAlexander Motin case CTL_TAG_SIMPLE: 1108ee7f31c0SAlexander Motin case CTL_TAG_ACA: 1109ee7f31c0SAlexander Motin default: 1110ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1111ee7f31c0SAlexander Motin break; 1112ee7f31c0SAlexander Motin } 1113ee7f31c0SAlexander Motin 1114ee7f31c0SAlexander Motin beio->io_len = 0; 1115ee7f31c0SAlexander Motin beio->io_offset = -1; 1116ee7f31c0SAlexander Motin 1117ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1118ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1119ee7f31c0SAlexander Motin 112066df9136SAlexander Motin DPRINTF("UNMAP\n"); 1121ee7f31c0SAlexander Motin 1122ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1123ee7f31c0SAlexander Motin } 1124ee7f31c0SAlexander Motin 1125ee7f31c0SAlexander Motin static void 1126ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1127ee7f31c0SAlexander Motin { 1128ee7f31c0SAlexander Motin union ctl_io *io; 1129ee7f31c0SAlexander Motin 1130ee7f31c0SAlexander Motin io = beio->io; 1131ee7f31c0SAlexander Motin ctl_free_beio(beio); 1132ee7f31c0SAlexander Motin ctl_config_write_done(io); 1133ee7f31c0SAlexander Motin } 1134ee7f31c0SAlexander Motin 1135ee7f31c0SAlexander Motin static void 1136130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1137130f4520SKenneth D. Merry union ctl_io *io) 1138130f4520SKenneth D. Merry { 1139130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1140130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1141130f4520SKenneth D. Merry 1142130f4520SKenneth D. Merry DPRINTF("entered\n"); 1143130f4520SKenneth D. Merry 1144130f4520SKenneth D. Merry softc = be_lun->softc; 1145130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1146130f4520SKenneth D. Merry beio->io = io; 1147130f4520SKenneth D. Merry beio->lun = be_lun; 1148ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done; 1149e86a4142SAlexander Motin PRIV(io)->ptr = (void *)beio; 1150130f4520SKenneth D. Merry 1151130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 1152130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 1153130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 1154d2a0972dSEdward Tomasz Napierala beio->bio_cmd = BIO_FLUSH; 1155130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_NO_DATA; 1156130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1157130f4520SKenneth D. Merry beio->io_len = 0; 1158130f4520SKenneth D. Merry be_lun->lun_flush(be_lun, beio); 1159130f4520SKenneth D. Merry break; 1160ee7f31c0SAlexander Motin case WRITE_SAME_10: 1161ee7f31c0SAlexander Motin case WRITE_SAME_16: 1162ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(be_lun, io); 1163ee7f31c0SAlexander Motin break; 1164ee7f31c0SAlexander Motin case UNMAP: 1165ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(be_lun, io); 1166ee7f31c0SAlexander Motin break; 1167130f4520SKenneth D. Merry default: 1168130f4520SKenneth D. Merry panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1169130f4520SKenneth D. Merry break; 1170130f4520SKenneth D. Merry } 1171130f4520SKenneth D. Merry } 1172130f4520SKenneth D. Merry 1173d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1174d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1175d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1176d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1177130f4520SKenneth D. Merry 1178130f4520SKenneth D. Merry static void 117908a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio) 118008a7cce5SAlexander Motin { 118108a7cce5SAlexander Motin struct ctl_be_block_lun *be_lun; 118208a7cce5SAlexander Motin union ctl_io *io; 118308a7cce5SAlexander Motin 118408a7cce5SAlexander Motin io = beio->io; 118508a7cce5SAlexander Motin be_lun = beio->lun; 118608a7cce5SAlexander Motin ctl_free_beio(beio); 118708a7cce5SAlexander Motin if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) 118808a7cce5SAlexander Motin && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1189*11b569f7SAlexander Motin ctl_data_submit_done(io); 119008a7cce5SAlexander Motin return; 119108a7cce5SAlexander Motin } 119208a7cce5SAlexander Motin 119308a7cce5SAlexander Motin io->io_hdr.status &= ~CTL_STATUS_MASK; 119408a7cce5SAlexander Motin io->io_hdr.status |= CTL_STATUS_NONE; 119508a7cce5SAlexander Motin 119608a7cce5SAlexander Motin mtx_lock(&be_lun->lock); 119708a7cce5SAlexander Motin /* 119808a7cce5SAlexander Motin * XXX KDM make sure that links is okay to use at this point. 119908a7cce5SAlexander Motin * Otherwise, we either need to add another field to ctl_io_hdr, 120008a7cce5SAlexander Motin * or deal with resource allocation here. 120108a7cce5SAlexander Motin */ 120208a7cce5SAlexander Motin STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 120308a7cce5SAlexander Motin mtx_unlock(&be_lun->lock); 120408a7cce5SAlexander Motin 120508a7cce5SAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 120608a7cce5SAlexander Motin } 120708a7cce5SAlexander Motin 120808a7cce5SAlexander Motin static void 1209130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1210130f4520SKenneth D. Merry union ctl_io *io) 1211130f4520SKenneth D. Merry { 1212130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1213130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1214*11b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 1215e86a4142SAlexander Motin struct ctl_ptr_len_flags *bptrlen; 1216e86a4142SAlexander Motin uint64_t len_left, lbas; 1217130f4520SKenneth D. Merry int i; 1218130f4520SKenneth D. Merry 1219130f4520SKenneth D. Merry softc = be_lun->softc; 1220130f4520SKenneth D. Merry 1221130f4520SKenneth D. Merry DPRINTF("entered\n"); 1222130f4520SKenneth D. Merry 1223*11b569f7SAlexander Motin lbalen = ARGS(io); 1224*11b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1225130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1226*11b569f7SAlexander Motin } else { 1227*11b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1228130f4520SKenneth D. Merry } 1229130f4520SKenneth D. Merry 1230130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1231130f4520SKenneth D. Merry beio->io = io; 1232130f4520SKenneth D. Merry beio->lun = be_lun; 1233e86a4142SAlexander Motin bptrlen = PRIV(io); 1234e86a4142SAlexander Motin bptrlen->ptr = (void *)beio; 1235130f4520SKenneth D. Merry 1236130f4520SKenneth D. Merry /* 1237130f4520SKenneth D. Merry * If the I/O came down with an ordered or head of queue tag, set 1238130f4520SKenneth D. Merry * the BIO_ORDERED attribute. For head of queue tags, that's 1239130f4520SKenneth D. Merry * pretty much the best we can do. 1240130f4520SKenneth D. Merry * 1241130f4520SKenneth D. Merry * XXX KDM we don't have a great way to easily know about the FUA 1242130f4520SKenneth D. Merry * bit right now (it is decoded in ctl_read_write(), but we don't 1243130f4520SKenneth D. Merry * pass that knowledge to the backend), and in any case we would 1244130f4520SKenneth D. Merry * need to determine how to handle it. 1245130f4520SKenneth D. Merry */ 1246130f4520SKenneth D. Merry if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 1247130f4520SKenneth D. Merry || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 1248130f4520SKenneth D. Merry beio->bio_flags = BIO_ORDERED; 1249130f4520SKenneth D. Merry 1250130f4520SKenneth D. Merry switch (io->scsiio.tag_type) { 1251130f4520SKenneth D. Merry case CTL_TAG_ORDERED: 1252130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1253130f4520SKenneth D. Merry break; 1254130f4520SKenneth D. Merry case CTL_TAG_HEAD_OF_QUEUE: 1255130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1256130f4520SKenneth D. Merry break; 1257130f4520SKenneth D. Merry case CTL_TAG_UNTAGGED: 1258130f4520SKenneth D. Merry case CTL_TAG_SIMPLE: 1259130f4520SKenneth D. Merry case CTL_TAG_ACA: 1260130f4520SKenneth D. Merry default: 1261130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1262130f4520SKenneth D. Merry break; 1263130f4520SKenneth D. Merry } 1264130f4520SKenneth D. Merry 1265*11b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1266130f4520SKenneth D. Merry beio->bio_cmd = BIO_WRITE; 1267130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_WRITE; 1268*11b569f7SAlexander Motin } else { 1269*11b569f7SAlexander Motin beio->bio_cmd = BIO_READ; 1270*11b569f7SAlexander Motin beio->ds_trans_type = DEVSTAT_READ; 1271130f4520SKenneth D. Merry } 1272130f4520SKenneth D. Merry 127308a7cce5SAlexander Motin DPRINTF("%s at LBA %jx len %u @%ju\n", 1274130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1275e86a4142SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1276*11b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 1277*11b569f7SAlexander Motin lbas = CTLBLK_HALF_IO_SIZE; 1278*11b569f7SAlexander Motin else 1279*11b569f7SAlexander Motin lbas = CTLBLK_MAX_IO_SIZE; 1280*11b569f7SAlexander Motin lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1281e86a4142SAlexander Motin beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1282e86a4142SAlexander Motin beio->io_len = lbas * be_lun->blocksize; 1283e86a4142SAlexander Motin bptrlen->len += lbas; 1284130f4520SKenneth D. Merry 128508a7cce5SAlexander Motin for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 128608a7cce5SAlexander Motin KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 128708a7cce5SAlexander Motin i, CTLBLK_MAX_SEGS)); 1288130f4520SKenneth D. Merry 1289130f4520SKenneth D. Merry /* 1290130f4520SKenneth D. Merry * Setup the S/G entry for this chunk. 1291130f4520SKenneth D. Merry */ 129208a7cce5SAlexander Motin beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1293130f4520SKenneth D. Merry beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1294130f4520SKenneth D. Merry 1295130f4520SKenneth D. Merry DPRINTF("segment %d addr %p len %zd\n", i, 1296130f4520SKenneth D. Merry beio->sg_segs[i].addr, beio->sg_segs[i].len); 1297130f4520SKenneth D. Merry 1298*11b569f7SAlexander Motin /* Set up second segment for compare operation. */ 1299*11b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) { 1300*11b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1301*11b569f7SAlexander Motin beio->sg_segs[i].len; 1302*11b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1303*11b569f7SAlexander Motin uma_zalloc(be_lun->lun_zone, M_WAITOK); 1304*11b569f7SAlexander Motin } 1305*11b569f7SAlexander Motin 1306130f4520SKenneth D. Merry beio->num_segs++; 1307130f4520SKenneth D. Merry len_left -= beio->sg_segs[i].len; 1308130f4520SKenneth D. Merry } 1309e86a4142SAlexander Motin if (bptrlen->len < lbalen->len) 131008a7cce5SAlexander Motin beio->beio_cont = ctl_be_block_next; 131108a7cce5SAlexander Motin io->scsiio.be_move_done = ctl_be_block_move_done; 1312*11b569f7SAlexander Motin /* For compare we have separate S/G lists for read and datamove. */ 1313*11b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 1314*11b569f7SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1315*11b569f7SAlexander Motin else 131608a7cce5SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 131708a7cce5SAlexander Motin io->scsiio.kern_data_len = beio->io_len; 131808a7cce5SAlexander Motin io->scsiio.kern_data_resid = 0; 131908a7cce5SAlexander Motin io->scsiio.kern_sg_entries = beio->num_segs; 132008a7cce5SAlexander Motin io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1321130f4520SKenneth D. Merry 1322130f4520SKenneth D. Merry /* 1323130f4520SKenneth D. Merry * For the read case, we need to read the data into our buffers and 1324130f4520SKenneth D. Merry * then we can send it back to the user. For the write case, we 1325130f4520SKenneth D. Merry * need to get the data from the user first. 1326130f4520SKenneth D. Merry */ 1327130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 1328130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1329130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1330130f4520SKenneth D. Merry } else { 1331130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1332130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 1333130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 1334130f4520SKenneth D. Merry #endif 1335130f4520SKenneth D. Merry ctl_datamove(io); 1336130f4520SKenneth D. Merry } 1337130f4520SKenneth D. Merry } 1338130f4520SKenneth D. Merry 1339130f4520SKenneth D. Merry static void 1340130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending) 1341130f4520SKenneth D. Merry { 1342130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1343130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1344130f4520SKenneth D. Merry union ctl_io *io; 1345130f4520SKenneth D. Merry 1346130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)context; 1347130f4520SKenneth D. Merry softc = be_lun->softc; 1348130f4520SKenneth D. Merry 1349130f4520SKenneth D. Merry DPRINTF("entered\n"); 1350130f4520SKenneth D. Merry 1351130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 1352130f4520SKenneth D. Merry for (;;) { 1353130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1354130f4520SKenneth D. Merry if (io != NULL) { 1355130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1356130f4520SKenneth D. Merry 1357130f4520SKenneth D. Merry DPRINTF("datamove queue\n"); 1358130f4520SKenneth D. Merry 1359130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1360130f4520SKenneth D. Merry ctl_io_hdr, links); 1361130f4520SKenneth D. Merry 1362130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 1363130f4520SKenneth D. Merry 1364e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1365130f4520SKenneth D. Merry 1366130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1367130f4520SKenneth D. Merry 1368130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 1369130f4520SKenneth D. Merry continue; 1370130f4520SKenneth D. Merry } 1371130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1372130f4520SKenneth D. Merry if (io != NULL) { 1373130f4520SKenneth D. Merry 1374130f4520SKenneth D. Merry DPRINTF("config write queue\n"); 1375130f4520SKenneth D. Merry 1376130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1377130f4520SKenneth D. Merry ctl_io_hdr, links); 1378130f4520SKenneth D. Merry 1379130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 1380130f4520SKenneth D. Merry 1381130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(be_lun, io); 1382130f4520SKenneth D. Merry 1383130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 1384130f4520SKenneth D. Merry continue; 1385130f4520SKenneth D. Merry } 1386130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1387130f4520SKenneth D. Merry if (io != NULL) { 1388130f4520SKenneth D. Merry DPRINTF("input queue\n"); 1389130f4520SKenneth D. Merry 1390130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1391130f4520SKenneth D. Merry ctl_io_hdr, links); 1392130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 1393130f4520SKenneth D. Merry 1394130f4520SKenneth D. Merry /* 1395130f4520SKenneth D. Merry * We must drop the lock, since this routine and 1396130f4520SKenneth D. Merry * its children may sleep. 1397130f4520SKenneth D. Merry */ 1398130f4520SKenneth D. Merry ctl_be_block_dispatch(be_lun, io); 1399130f4520SKenneth D. Merry 1400130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 1401130f4520SKenneth D. Merry continue; 1402130f4520SKenneth D. Merry } 1403130f4520SKenneth D. Merry 1404130f4520SKenneth D. Merry /* 1405130f4520SKenneth D. Merry * If we get here, there is no work left in the queues, so 1406130f4520SKenneth D. Merry * just break out and let the task queue go to sleep. 1407130f4520SKenneth D. Merry */ 1408130f4520SKenneth D. Merry break; 1409130f4520SKenneth D. Merry } 1410130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 1411130f4520SKenneth D. Merry } 1412130f4520SKenneth D. Merry 1413130f4520SKenneth D. Merry /* 1414130f4520SKenneth D. Merry * Entry point from CTL to the backend for I/O. We queue everything to a 1415130f4520SKenneth D. Merry * work thread, so this just puts the I/O on a queue and wakes up the 1416130f4520SKenneth D. Merry * thread. 1417130f4520SKenneth D. Merry */ 1418130f4520SKenneth D. Merry static int 1419130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io) 1420130f4520SKenneth D. Merry { 1421130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1422130f4520SKenneth D. Merry struct ctl_be_lun *ctl_be_lun; 1423130f4520SKenneth D. Merry 1424130f4520SKenneth D. Merry DPRINTF("entered\n"); 1425130f4520SKenneth D. Merry 1426130f4520SKenneth D. Merry ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1427130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 1428130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1429130f4520SKenneth D. Merry 1430130f4520SKenneth D. Merry /* 1431130f4520SKenneth D. Merry * Make sure we only get SCSI I/O. 1432130f4520SKenneth D. Merry */ 1433130f4520SKenneth D. Merry KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1434130f4520SKenneth D. Merry "%#x) encountered", io->io_hdr.io_type)); 1435130f4520SKenneth D. Merry 1436e86a4142SAlexander Motin PRIV(io)->len = 0; 1437e86a4142SAlexander Motin 1438130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 1439130f4520SKenneth D. Merry /* 1440130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 1441130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 1442130f4520SKenneth D. Merry * or deal with resource allocation here. 1443130f4520SKenneth D. Merry */ 1444130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1445130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 1446130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1447130f4520SKenneth D. Merry 14489c71cd5aSAlexander Motin return (CTL_RETVAL_COMPLETE); 1449130f4520SKenneth D. Merry } 1450130f4520SKenneth D. Merry 1451130f4520SKenneth D. Merry static int 1452130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1453130f4520SKenneth D. Merry int flag, struct thread *td) 1454130f4520SKenneth D. Merry { 1455130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1456130f4520SKenneth D. Merry int error; 1457130f4520SKenneth D. Merry 1458130f4520SKenneth D. Merry softc = &backend_block_softc; 1459130f4520SKenneth D. Merry 1460130f4520SKenneth D. Merry error = 0; 1461130f4520SKenneth D. Merry 1462130f4520SKenneth D. Merry switch (cmd) { 1463130f4520SKenneth D. Merry case CTL_LUN_REQ: { 1464130f4520SKenneth D. Merry struct ctl_lun_req *lun_req; 1465130f4520SKenneth D. Merry 1466130f4520SKenneth D. Merry lun_req = (struct ctl_lun_req *)addr; 1467130f4520SKenneth D. Merry 1468130f4520SKenneth D. Merry switch (lun_req->reqtype) { 1469130f4520SKenneth D. Merry case CTL_LUNREQ_CREATE: 1470130f4520SKenneth D. Merry error = ctl_be_block_create(softc, lun_req); 1471130f4520SKenneth D. Merry break; 1472130f4520SKenneth D. Merry case CTL_LUNREQ_RM: 1473130f4520SKenneth D. Merry error = ctl_be_block_rm(softc, lun_req); 1474130f4520SKenneth D. Merry break; 147581177295SEdward Tomasz Napierala case CTL_LUNREQ_MODIFY: 147681177295SEdward Tomasz Napierala error = ctl_be_block_modify(softc, lun_req); 147781177295SEdward Tomasz Napierala break; 1478130f4520SKenneth D. Merry default: 1479130f4520SKenneth D. Merry lun_req->status = CTL_LUN_ERROR; 1480130f4520SKenneth D. Merry snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1481130f4520SKenneth D. Merry "%s: invalid LUN request type %d", __func__, 1482130f4520SKenneth D. Merry lun_req->reqtype); 1483130f4520SKenneth D. Merry break; 1484130f4520SKenneth D. Merry } 1485130f4520SKenneth D. Merry break; 1486130f4520SKenneth D. Merry } 1487130f4520SKenneth D. Merry default: 1488130f4520SKenneth D. Merry error = ENOTTY; 1489130f4520SKenneth D. Merry break; 1490130f4520SKenneth D. Merry } 1491130f4520SKenneth D. Merry 1492130f4520SKenneth D. Merry return (error); 1493130f4520SKenneth D. Merry } 1494130f4520SKenneth D. Merry 1495130f4520SKenneth D. Merry static int 1496130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1497130f4520SKenneth D. Merry { 1498130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 1499130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 1500130f4520SKenneth D. Merry struct vattr vattr; 1501130f4520SKenneth D. Merry int error; 1502130f4520SKenneth D. Merry 1503130f4520SKenneth D. Merry error = 0; 1504130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 1505130f4520SKenneth D. Merry params = &req->reqdata.create; 1506130f4520SKenneth D. Merry 1507130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_FILE; 1508130f4520SKenneth D. Merry be_lun->dispatch = ctl_be_block_dispatch_file; 1509130f4520SKenneth D. Merry be_lun->lun_flush = ctl_be_block_flush_file; 1510130f4520SKenneth D. Merry 1511130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1512130f4520SKenneth D. Merry if (error != 0) { 1513130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1514130f4520SKenneth D. Merry "error calling VOP_GETATTR() for file %s", 1515130f4520SKenneth D. Merry be_lun->dev_path); 1516130f4520SKenneth D. Merry return (error); 1517130f4520SKenneth D. Merry } 1518130f4520SKenneth D. Merry 1519130f4520SKenneth D. Merry /* 1520130f4520SKenneth D. Merry * Verify that we have the ability to upgrade to exclusive 1521130f4520SKenneth D. Merry * access on this file so we can trap errors at open instead 1522130f4520SKenneth D. Merry * of reporting them during first access. 1523130f4520SKenneth D. Merry */ 1524130f4520SKenneth D. Merry if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1525130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1526130f4520SKenneth D. Merry if (be_lun->vn->v_iflag & VI_DOOMED) { 1527130f4520SKenneth D. Merry error = EBADF; 1528130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1529130f4520SKenneth D. Merry "error locking file %s", be_lun->dev_path); 1530130f4520SKenneth D. Merry return (error); 1531130f4520SKenneth D. Merry } 1532130f4520SKenneth D. Merry } 1533130f4520SKenneth D. Merry 1534130f4520SKenneth D. Merry 1535130f4520SKenneth D. Merry file_data->cred = crhold(curthread->td_ucred); 153681177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) 153781177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 153881177295SEdward Tomasz Napierala else 1539130f4520SKenneth D. Merry be_lun->size_bytes = vattr.va_size; 1540130f4520SKenneth D. Merry /* 1541130f4520SKenneth D. Merry * We set the multi thread flag for file operations because all 1542130f4520SKenneth D. Merry * filesystems (in theory) are capable of allowing multiple readers 1543130f4520SKenneth D. Merry * of a file at once. So we want to get the maximum possible 1544130f4520SKenneth D. Merry * concurrency. 1545130f4520SKenneth D. Merry */ 1546130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1547130f4520SKenneth D. Merry 1548130f4520SKenneth D. Merry /* 1549130f4520SKenneth D. Merry * XXX KDM vattr.va_blocksize may be larger than 512 bytes here. 1550130f4520SKenneth D. Merry * With ZFS, it is 131072 bytes. Block sizes that large don't work 1551130f4520SKenneth D. Merry * with disklabel and UFS on FreeBSD at least. Large block sizes 1552130f4520SKenneth D. Merry * may not work with other OSes as well. So just export a sector 1553130f4520SKenneth D. Merry * size of 512 bytes, which should work with any OS or 1554130f4520SKenneth D. Merry * application. Since our backing is a file, any block size will 1555130f4520SKenneth D. Merry * work fine for the backing store. 1556130f4520SKenneth D. Merry */ 1557130f4520SKenneth D. Merry #if 0 1558130f4520SKenneth D. Merry be_lun->blocksize= vattr.va_blocksize; 1559130f4520SKenneth D. Merry #endif 1560130f4520SKenneth D. Merry if (params->blocksize_bytes != 0) 1561130f4520SKenneth D. Merry be_lun->blocksize = params->blocksize_bytes; 1562130f4520SKenneth D. Merry else 1563130f4520SKenneth D. Merry be_lun->blocksize = 512; 1564130f4520SKenneth D. Merry 1565130f4520SKenneth D. Merry /* 1566130f4520SKenneth D. Merry * Sanity check. The media size has to be at least one 1567130f4520SKenneth D. Merry * sector long. 1568130f4520SKenneth D. Merry */ 1569130f4520SKenneth D. Merry if (be_lun->size_bytes < be_lun->blocksize) { 1570130f4520SKenneth D. Merry error = EINVAL; 1571130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1572130f4520SKenneth D. Merry "file %s size %ju < block size %u", be_lun->dev_path, 1573130f4520SKenneth D. Merry (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1574130f4520SKenneth D. Merry } 1575130f4520SKenneth D. Merry return (error); 1576130f4520SKenneth D. Merry } 1577130f4520SKenneth D. Merry 1578130f4520SKenneth D. Merry static int 1579130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1580130f4520SKenneth D. Merry { 1581130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 1582130f4520SKenneth D. Merry struct vattr vattr; 1583130f4520SKenneth D. Merry struct cdev *dev; 1584130f4520SKenneth D. Merry struct cdevsw *devsw; 1585130f4520SKenneth D. Merry int error; 1586f6012722SAlexander Motin off_t ps, pss, po, pos; 1587130f4520SKenneth D. Merry 1588130f4520SKenneth D. Merry params = &req->reqdata.create; 1589130f4520SKenneth D. Merry 1590130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_DEV; 1591130f4520SKenneth D. Merry be_lun->dispatch = ctl_be_block_dispatch_dev; 1592130f4520SKenneth D. Merry be_lun->lun_flush = ctl_be_block_flush_dev; 1593ee7f31c0SAlexander Motin be_lun->unmap = ctl_be_block_unmap_dev; 1594130f4520SKenneth D. Merry be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1595130f4520SKenneth D. Merry be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1596130f4520SKenneth D. Merry &be_lun->backend.dev.dev_ref); 1597130f4520SKenneth D. Merry if (be_lun->backend.dev.csw == NULL) 1598130f4520SKenneth D. Merry panic("Unable to retrieve device switch"); 1599130f4520SKenneth D. Merry 1600130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1601130f4520SKenneth D. Merry if (error) { 1602130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1603130f4520SKenneth D. Merry "%s: error getting vnode attributes for device %s", 1604130f4520SKenneth D. Merry __func__, be_lun->dev_path); 1605130f4520SKenneth D. Merry return (error); 1606130f4520SKenneth D. Merry } 1607130f4520SKenneth D. Merry 1608130f4520SKenneth D. Merry dev = be_lun->vn->v_rdev; 1609130f4520SKenneth D. Merry devsw = dev->si_devsw; 1610130f4520SKenneth D. Merry if (!devsw->d_ioctl) { 1611130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1612130f4520SKenneth D. Merry "%s: no d_ioctl for device %s!", __func__, 1613130f4520SKenneth D. Merry be_lun->dev_path); 1614130f4520SKenneth D. Merry return (ENODEV); 1615130f4520SKenneth D. Merry } 1616130f4520SKenneth D. Merry 1617130f4520SKenneth D. Merry error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1618130f4520SKenneth D. Merry (caddr_t)&be_lun->blocksize, FREAD, 1619130f4520SKenneth D. Merry curthread); 1620130f4520SKenneth D. Merry if (error) { 1621130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1622130f4520SKenneth D. Merry "%s: error %d returned for DIOCGSECTORSIZE ioctl " 1623130f4520SKenneth D. Merry "on %s!", __func__, error, be_lun->dev_path); 1624130f4520SKenneth D. Merry return (error); 1625130f4520SKenneth D. Merry } 1626130f4520SKenneth D. Merry 1627130f4520SKenneth D. Merry /* 1628130f4520SKenneth D. Merry * If the user has asked for a blocksize that is greater than the 1629130f4520SKenneth D. Merry * backing device's blocksize, we can do it only if the blocksize 1630130f4520SKenneth D. Merry * the user is asking for is an even multiple of the underlying 1631130f4520SKenneth D. Merry * device's blocksize. 1632130f4520SKenneth D. Merry */ 1633130f4520SKenneth D. Merry if ((params->blocksize_bytes != 0) 1634130f4520SKenneth D. Merry && (params->blocksize_bytes > be_lun->blocksize)) { 1635130f4520SKenneth D. Merry uint32_t bs_multiple, tmp_blocksize; 1636130f4520SKenneth D. Merry 1637130f4520SKenneth D. Merry bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1638130f4520SKenneth D. Merry 1639130f4520SKenneth D. Merry tmp_blocksize = bs_multiple * be_lun->blocksize; 1640130f4520SKenneth D. Merry 1641130f4520SKenneth D. Merry if (tmp_blocksize == params->blocksize_bytes) { 1642130f4520SKenneth D. Merry be_lun->blocksize = params->blocksize_bytes; 1643130f4520SKenneth D. Merry } else { 1644130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1645130f4520SKenneth D. Merry "%s: requested blocksize %u is not an even " 1646130f4520SKenneth D. Merry "multiple of backing device blocksize %u", 1647130f4520SKenneth D. Merry __func__, params->blocksize_bytes, 1648130f4520SKenneth D. Merry be_lun->blocksize); 1649130f4520SKenneth D. Merry return (EINVAL); 1650130f4520SKenneth D. Merry 1651130f4520SKenneth D. Merry } 1652130f4520SKenneth D. Merry } else if ((params->blocksize_bytes != 0) 1653130f4520SKenneth D. Merry && (params->blocksize_bytes != be_lun->blocksize)) { 1654130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1655130f4520SKenneth D. Merry "%s: requested blocksize %u < backing device " 1656130f4520SKenneth D. Merry "blocksize %u", __func__, params->blocksize_bytes, 1657130f4520SKenneth D. Merry be_lun->blocksize); 1658130f4520SKenneth D. Merry return (EINVAL); 1659130f4520SKenneth D. Merry } 1660130f4520SKenneth D. Merry 1661130f4520SKenneth D. Merry error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1662130f4520SKenneth D. Merry (caddr_t)&be_lun->size_bytes, FREAD, 1663130f4520SKenneth D. Merry curthread); 1664130f4520SKenneth D. Merry if (error) { 1665130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 166681177295SEdward Tomasz Napierala "%s: error %d returned for DIOCGMEDIASIZE " 166781177295SEdward Tomasz Napierala " ioctl on %s!", __func__, error, 166881177295SEdward Tomasz Napierala be_lun->dev_path); 1669130f4520SKenneth D. Merry return (error); 1670130f4520SKenneth D. Merry } 1671130f4520SKenneth D. Merry 167281177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 167381177295SEdward Tomasz Napierala if (params->lun_size_bytes > be_lun->size_bytes) { 167481177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 167581177295SEdward Tomasz Napierala "%s: requested LUN size %ju > backing device " 167681177295SEdward Tomasz Napierala "size %ju", __func__, 167781177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 167881177295SEdward Tomasz Napierala (uintmax_t)be_lun->size_bytes); 167981177295SEdward Tomasz Napierala return (EINVAL); 1680130f4520SKenneth D. Merry } 1681130f4520SKenneth D. Merry 168281177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 168381177295SEdward Tomasz Napierala } 168481177295SEdward Tomasz Napierala 1685f6012722SAlexander Motin error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1686f6012722SAlexander Motin (caddr_t)&ps, FREAD, curthread); 1687f6012722SAlexander Motin if (error) 1688f6012722SAlexander Motin ps = po = 0; 1689f6012722SAlexander Motin else { 1690f6012722SAlexander Motin error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1691f6012722SAlexander Motin (caddr_t)&po, FREAD, curthread); 1692f6012722SAlexander Motin if (error) 1693f6012722SAlexander Motin po = 0; 1694f6012722SAlexander Motin } 1695f6012722SAlexander Motin pss = ps / be_lun->blocksize; 1696f6012722SAlexander Motin pos = po / be_lun->blocksize; 1697f6012722SAlexander Motin if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1698f6012722SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1699f6012722SAlexander Motin be_lun->pblockexp = fls(pss) - 1; 1700f6012722SAlexander Motin be_lun->pblockoff = (pss - pos) % pss; 1701f6012722SAlexander Motin } 1702f6012722SAlexander Motin 170381177295SEdward Tomasz Napierala return (0); 170481177295SEdward Tomasz Napierala } 1705130f4520SKenneth D. Merry 1706130f4520SKenneth D. Merry static int 1707130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun) 1708130f4520SKenneth D. Merry { 1709130f4520SKenneth D. Merry DROP_GIANT(); 1710130f4520SKenneth D. Merry if (be_lun->vn) { 1711130f4520SKenneth D. Merry int flags = FREAD | FWRITE; 1712130f4520SKenneth D. Merry 1713130f4520SKenneth D. Merry switch (be_lun->dev_type) { 1714130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 1715130f4520SKenneth D. Merry if (be_lun->backend.dev.csw) { 1716130f4520SKenneth D. Merry dev_relthread(be_lun->backend.dev.cdev, 1717130f4520SKenneth D. Merry be_lun->backend.dev.dev_ref); 1718130f4520SKenneth D. Merry be_lun->backend.dev.csw = NULL; 1719130f4520SKenneth D. Merry be_lun->backend.dev.cdev = NULL; 1720130f4520SKenneth D. Merry } 1721130f4520SKenneth D. Merry break; 1722130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 1723130f4520SKenneth D. Merry break; 1724130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 1725025a2301SEdward Tomasz Napierala break; 1726130f4520SKenneth D. Merry default: 1727130f4520SKenneth D. Merry panic("Unexpected backend type."); 1728130f4520SKenneth D. Merry break; 1729130f4520SKenneth D. Merry } 1730130f4520SKenneth D. Merry 1731130f4520SKenneth D. Merry (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1732130f4520SKenneth D. Merry be_lun->vn = NULL; 1733130f4520SKenneth D. Merry 1734130f4520SKenneth D. Merry switch (be_lun->dev_type) { 1735130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 1736130f4520SKenneth D. Merry break; 1737130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 1738130f4520SKenneth D. Merry if (be_lun->backend.file.cred != NULL) { 1739130f4520SKenneth D. Merry crfree(be_lun->backend.file.cred); 1740130f4520SKenneth D. Merry be_lun->backend.file.cred = NULL; 1741130f4520SKenneth D. Merry } 1742130f4520SKenneth D. Merry break; 1743130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 1744025a2301SEdward Tomasz Napierala break; 1745130f4520SKenneth D. Merry default: 1746130f4520SKenneth D. Merry panic("Unexpected backend type."); 1747130f4520SKenneth D. Merry break; 1748130f4520SKenneth D. Merry } 1749130f4520SKenneth D. Merry } 1750130f4520SKenneth D. Merry PICKUP_GIANT(); 1751130f4520SKenneth D. Merry 1752130f4520SKenneth D. Merry return (0); 1753130f4520SKenneth D. Merry } 1754130f4520SKenneth D. Merry 1755130f4520SKenneth D. Merry static int 1756130f4520SKenneth D. Merry ctl_be_block_open(struct ctl_be_block_softc *softc, 1757130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1758130f4520SKenneth D. Merry { 1759130f4520SKenneth D. Merry struct nameidata nd; 1760130f4520SKenneth D. Merry int flags; 1761130f4520SKenneth D. Merry int error; 1762130f4520SKenneth D. Merry 1763130f4520SKenneth D. Merry /* 1764130f4520SKenneth D. Merry * XXX KDM allow a read-only option? 1765130f4520SKenneth D. Merry */ 1766130f4520SKenneth D. Merry flags = FREAD | FWRITE; 1767130f4520SKenneth D. Merry error = 0; 1768130f4520SKenneth D. Merry 1769130f4520SKenneth D. Merry if (rootvnode == NULL) { 1770130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1771130f4520SKenneth D. Merry "%s: Root filesystem is not mounted", __func__); 1772130f4520SKenneth D. Merry return (1); 1773130f4520SKenneth D. Merry } 1774130f4520SKenneth D. Merry 1775130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_cdir) { 1776130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_cdir = rootvnode; 1777130f4520SKenneth D. Merry VREF(rootvnode); 1778130f4520SKenneth D. Merry } 1779130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_rdir) { 1780130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_rdir = rootvnode; 1781130f4520SKenneth D. Merry VREF(rootvnode); 1782130f4520SKenneth D. Merry } 1783130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_jdir) { 1784130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_jdir = rootvnode; 1785130f4520SKenneth D. Merry VREF(rootvnode); 1786130f4520SKenneth D. Merry } 1787130f4520SKenneth D. Merry 1788130f4520SKenneth D. Merry again: 1789130f4520SKenneth D. Merry NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1790130f4520SKenneth D. Merry error = vn_open(&nd, &flags, 0, NULL); 1791130f4520SKenneth D. Merry if (error) { 1792130f4520SKenneth D. Merry /* 1793130f4520SKenneth D. Merry * This is the only reasonable guess we can make as far as 1794130f4520SKenneth D. Merry * path if the user doesn't give us a fully qualified path. 1795130f4520SKenneth D. Merry * If they want to specify a file, they need to specify the 1796130f4520SKenneth D. Merry * full path. 1797130f4520SKenneth D. Merry */ 1798130f4520SKenneth D. Merry if (be_lun->dev_path[0] != '/') { 1799130f4520SKenneth D. Merry char *dev_path = "/dev/"; 1800130f4520SKenneth D. Merry char *dev_name; 1801130f4520SKenneth D. Merry 1802130f4520SKenneth D. Merry /* Try adding device path at beginning of name */ 1803130f4520SKenneth D. Merry dev_name = malloc(strlen(be_lun->dev_path) 1804130f4520SKenneth D. Merry + strlen(dev_path) + 1, 1805130f4520SKenneth D. Merry M_CTLBLK, M_WAITOK); 1806130f4520SKenneth D. Merry if (dev_name) { 1807130f4520SKenneth D. Merry sprintf(dev_name, "%s%s", dev_path, 1808130f4520SKenneth D. Merry be_lun->dev_path); 1809130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 1810130f4520SKenneth D. Merry be_lun->dev_path = dev_name; 1811130f4520SKenneth D. Merry goto again; 1812130f4520SKenneth D. Merry } 1813130f4520SKenneth D. Merry } 1814130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1815130f4520SKenneth D. Merry "%s: error opening %s", __func__, be_lun->dev_path); 1816130f4520SKenneth D. Merry return (error); 1817130f4520SKenneth D. Merry } 1818130f4520SKenneth D. Merry 1819130f4520SKenneth D. Merry NDFREE(&nd, NDF_ONLY_PNBUF); 1820130f4520SKenneth D. Merry 1821130f4520SKenneth D. Merry be_lun->vn = nd.ni_vp; 1822130f4520SKenneth D. Merry 1823130f4520SKenneth D. Merry /* We only support disks and files. */ 1824130f4520SKenneth D. Merry if (vn_isdisk(be_lun->vn, &error)) { 1825130f4520SKenneth D. Merry error = ctl_be_block_open_dev(be_lun, req); 1826130f4520SKenneth D. Merry } else if (be_lun->vn->v_type == VREG) { 1827130f4520SKenneth D. Merry error = ctl_be_block_open_file(be_lun, req); 1828130f4520SKenneth D. Merry } else { 1829130f4520SKenneth D. Merry error = EINVAL; 1830130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1831025a2301SEdward Tomasz Napierala "%s is not a disk or plain file", be_lun->dev_path); 1832130f4520SKenneth D. Merry } 1833130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 1834130f4520SKenneth D. Merry 1835130f4520SKenneth D. Merry if (error != 0) { 1836130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 1837130f4520SKenneth D. Merry return (error); 1838130f4520SKenneth D. Merry } 1839130f4520SKenneth D. Merry 1840130f4520SKenneth D. Merry be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1841130f4520SKenneth D. Merry be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1842130f4520SKenneth D. Merry 1843130f4520SKenneth D. Merry return (0); 1844130f4520SKenneth D. Merry } 1845130f4520SKenneth D. Merry 1846130f4520SKenneth D. Merry static int 1847130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1848130f4520SKenneth D. Merry { 1849130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1850130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 185157a5db13SAlexander Motin char num_thread_str[16]; 1852130f4520SKenneth D. Merry char tmpstr[32]; 185357a5db13SAlexander Motin char *value; 1854ee7f31c0SAlexander Motin int retval, num_threads, unmap; 185557a5db13SAlexander Motin int tmp_num_threads; 1856130f4520SKenneth D. Merry 1857130f4520SKenneth D. Merry params = &req->reqdata.create; 1858130f4520SKenneth D. Merry retval = 0; 1859130f4520SKenneth D. Merry 1860130f4520SKenneth D. Merry num_threads = cbb_num_threads; 1861130f4520SKenneth D. Merry 1862130f4520SKenneth D. Merry be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1863130f4520SKenneth D. Merry 1864130f4520SKenneth D. Merry be_lun->softc = softc; 1865130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->input_queue); 1866130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->config_write_queue); 1867130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->datamove_queue); 1868130f4520SKenneth D. Merry sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1869130f4520SKenneth D. Merry mtx_init(&be_lun->lock, be_lun->lunname, NULL, MTX_DEF); 187057a5db13SAlexander Motin ctl_init_opts(&be_lun->ctl_be_lun, req); 1871130f4520SKenneth D. Merry 187208a7cce5SAlexander Motin be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 1873eeb94054SAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1874130f4520SKenneth D. Merry 1875130f4520SKenneth D. Merry if (be_lun->lun_zone == NULL) { 1876130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1877130f4520SKenneth D. Merry "%s: error allocating UMA zone", __func__); 1878130f4520SKenneth D. Merry goto bailout_error; 1879130f4520SKenneth D. Merry } 1880130f4520SKenneth D. Merry 1881130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1882130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type = params->device_type; 1883130f4520SKenneth D. Merry else 1884130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type = T_DIRECT; 1885130f4520SKenneth D. Merry 1886130f4520SKenneth D. Merry if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1887eb3687a6SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun, "file"); 1888eb3687a6SAlexander Motin if (value == NULL) { 1889130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1890130f4520SKenneth D. Merry "%s: no file argument specified", __func__); 1891130f4520SKenneth D. Merry goto bailout_error; 1892130f4520SKenneth D. Merry } 1893eb3687a6SAlexander Motin be_lun->dev_path = strdup(value, M_CTLBLK); 1894130f4520SKenneth D. Merry 1895130f4520SKenneth D. Merry retval = ctl_be_block_open(softc, be_lun, req); 1896130f4520SKenneth D. Merry if (retval != 0) { 1897130f4520SKenneth D. Merry retval = 0; 1898130f4520SKenneth D. Merry goto bailout_error; 1899130f4520SKenneth D. Merry } 1900130f4520SKenneth D. Merry 1901130f4520SKenneth D. Merry /* 1902130f4520SKenneth D. Merry * Tell the user the size of the file/device. 1903130f4520SKenneth D. Merry */ 1904130f4520SKenneth D. Merry params->lun_size_bytes = be_lun->size_bytes; 1905130f4520SKenneth D. Merry 1906130f4520SKenneth D. Merry /* 1907130f4520SKenneth D. Merry * The maximum LBA is the size - 1. 1908130f4520SKenneth D. Merry */ 1909130f4520SKenneth D. Merry be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 1910130f4520SKenneth D. Merry } else { 1911130f4520SKenneth D. Merry /* 1912130f4520SKenneth D. Merry * For processor devices, we don't have any size. 1913130f4520SKenneth D. Merry */ 1914130f4520SKenneth D. Merry be_lun->blocksize = 0; 1915f6012722SAlexander Motin be_lun->pblockexp = 0; 1916f6012722SAlexander Motin be_lun->pblockoff = 0; 1917130f4520SKenneth D. Merry be_lun->size_blocks = 0; 1918130f4520SKenneth D. Merry be_lun->size_bytes = 0; 1919130f4520SKenneth D. Merry be_lun->ctl_be_lun.maxlba = 0; 1920130f4520SKenneth D. Merry params->lun_size_bytes = 0; 1921130f4520SKenneth D. Merry 1922130f4520SKenneth D. Merry /* 1923130f4520SKenneth D. Merry * Default to just 1 thread for processor devices. 1924130f4520SKenneth D. Merry */ 1925130f4520SKenneth D. Merry num_threads = 1; 1926130f4520SKenneth D. Merry } 1927130f4520SKenneth D. Merry 1928130f4520SKenneth D. Merry /* 1929130f4520SKenneth D. Merry * XXX This searching loop might be refactored to be combined with 1930130f4520SKenneth D. Merry * the loop above, 1931130f4520SKenneth D. Merry */ 193257a5db13SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun, "num_threads"); 193357a5db13SAlexander Motin if (value != NULL) { 193457a5db13SAlexander Motin tmp_num_threads = strtol(value, NULL, 0); 1935130f4520SKenneth D. Merry 1936130f4520SKenneth D. Merry /* 1937130f4520SKenneth D. Merry * We don't let the user specify less than one 1938130f4520SKenneth D. Merry * thread, but hope he's clueful enough not to 1939130f4520SKenneth D. Merry * specify 1000 threads. 1940130f4520SKenneth D. Merry */ 1941130f4520SKenneth D. Merry if (tmp_num_threads < 1) { 1942130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1943130f4520SKenneth D. Merry "%s: invalid number of threads %s", 1944130f4520SKenneth D. Merry __func__, num_thread_str); 1945130f4520SKenneth D. Merry goto bailout_error; 1946130f4520SKenneth D. Merry } 1947130f4520SKenneth D. Merry num_threads = tmp_num_threads; 194857a5db13SAlexander Motin } 194957a5db13SAlexander Motin unmap = 0; 195057a5db13SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun, "unmap"); 195157a5db13SAlexander Motin if (value != NULL && strcmp(value, "on") == 0) 1952ee7f31c0SAlexander Motin unmap = 1; 1953130f4520SKenneth D. Merry 1954130f4520SKenneth D. Merry be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 1955130f4520SKenneth D. Merry be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 1956ee7f31c0SAlexander Motin if (unmap) 1957ee7f31c0SAlexander Motin be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 1958130f4520SKenneth D. Merry be_lun->ctl_be_lun.be_lun = be_lun; 1959130f4520SKenneth D. Merry be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 1960f6012722SAlexander Motin be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 1961f6012722SAlexander Motin be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 1962130f4520SKenneth D. Merry /* Tell the user the blocksize we ended up using */ 1963130f4520SKenneth D. Merry params->blocksize_bytes = be_lun->blocksize; 1964130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_ID_REQ) { 1965130f4520SKenneth D. Merry be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 1966130f4520SKenneth D. Merry be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 1967130f4520SKenneth D. Merry } else 1968130f4520SKenneth D. Merry be_lun->ctl_be_lun.req_lun_id = 0; 1969130f4520SKenneth D. Merry 1970130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 1971130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_config_status = 1972130f4520SKenneth D. Merry ctl_be_block_lun_config_status; 1973130f4520SKenneth D. Merry be_lun->ctl_be_lun.be = &ctl_be_block_driver; 1974130f4520SKenneth D. Merry 1975130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 1976130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 1977130f4520SKenneth D. Merry softc->num_luns); 1978130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 1979130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1980130f4520SKenneth D. Merry sizeof(tmpstr))); 1981130f4520SKenneth D. Merry 1982130f4520SKenneth D. Merry /* Tell the user what we used for a serial number */ 1983130f4520SKenneth D. Merry strncpy((char *)params->serial_num, tmpstr, 1984130f4520SKenneth D. Merry ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 1985130f4520SKenneth D. Merry } else { 1986130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.serial_num, 1987130f4520SKenneth D. Merry params->serial_num, 1988130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1989130f4520SKenneth D. Merry sizeof(params->serial_num))); 1990130f4520SKenneth D. Merry } 1991130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 1992130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 1993130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 1994130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1995130f4520SKenneth D. Merry sizeof(tmpstr))); 1996130f4520SKenneth D. Merry 1997130f4520SKenneth D. Merry /* Tell the user what we used for a device ID */ 1998130f4520SKenneth D. Merry strncpy((char *)params->device_id, tmpstr, 1999130f4520SKenneth D. Merry ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2000130f4520SKenneth D. Merry } else { 2001130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.device_id, 2002130f4520SKenneth D. Merry params->device_id, 2003130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2004130f4520SKenneth D. Merry sizeof(params->device_id))); 2005130f4520SKenneth D. Merry } 2006130f4520SKenneth D. Merry 2007130f4520SKenneth D. Merry TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2008130f4520SKenneth D. Merry 2009130f4520SKenneth D. Merry be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2010130f4520SKenneth D. Merry taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2011130f4520SKenneth D. Merry 2012130f4520SKenneth D. Merry if (be_lun->io_taskqueue == NULL) { 2013130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2014130f4520SKenneth D. Merry "%s: Unable to create taskqueue", __func__); 2015130f4520SKenneth D. Merry goto bailout_error; 2016130f4520SKenneth D. Merry } 2017130f4520SKenneth D. Merry 2018130f4520SKenneth D. Merry /* 2019130f4520SKenneth D. Merry * Note that we start the same number of threads by default for 2020130f4520SKenneth D. Merry * both the file case and the block device case. For the file 2021130f4520SKenneth D. Merry * case, we need multiple threads to allow concurrency, because the 2022130f4520SKenneth D. Merry * vnode interface is designed to be a blocking interface. For the 2023130f4520SKenneth D. Merry * block device case, ZFS zvols at least will block the caller's 2024130f4520SKenneth D. Merry * context in many instances, and so we need multiple threads to 2025130f4520SKenneth D. Merry * overcome that problem. Other block devices don't need as many 2026130f4520SKenneth D. Merry * threads, but they shouldn't cause too many problems. 2027130f4520SKenneth D. Merry * 2028130f4520SKenneth D. Merry * If the user wants to just have a single thread for a block 2029130f4520SKenneth D. Merry * device, he can specify that when the LUN is created, or change 2030130f4520SKenneth D. Merry * the tunable/sysctl to alter the default number of threads. 2031130f4520SKenneth D. Merry */ 2032130f4520SKenneth D. Merry retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2033130f4520SKenneth D. Merry /*num threads*/num_threads, 2034130f4520SKenneth D. Merry /*priority*/PWAIT, 2035130f4520SKenneth D. Merry /*thread name*/ 2036130f4520SKenneth D. Merry "%s taskq", be_lun->lunname); 2037130f4520SKenneth D. Merry 2038130f4520SKenneth D. Merry if (retval != 0) 2039130f4520SKenneth D. Merry goto bailout_error; 2040130f4520SKenneth D. Merry 2041130f4520SKenneth D. Merry be_lun->num_threads = num_threads; 2042130f4520SKenneth D. Merry 2043130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2044130f4520SKenneth D. Merry softc->num_luns++; 2045130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2046130f4520SKenneth D. Merry 2047130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2048130f4520SKenneth D. Merry 2049130f4520SKenneth D. Merry retval = ctl_add_lun(&be_lun->ctl_be_lun); 2050130f4520SKenneth D. Merry if (retval != 0) { 2051130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2052130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2053130f4520SKenneth D. Merry links); 2054130f4520SKenneth D. Merry softc->num_luns--; 2055130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2056130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2057130f4520SKenneth D. Merry "%s: ctl_add_lun() returned error %d, see dmesg for " 2058130f4520SKenneth D. Merry "details", __func__, retval); 2059130f4520SKenneth D. Merry retval = 0; 2060130f4520SKenneth D. Merry goto bailout_error; 2061130f4520SKenneth D. Merry } 2062130f4520SKenneth D. Merry 2063130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2064130f4520SKenneth D. Merry 2065130f4520SKenneth D. Merry /* 2066130f4520SKenneth D. Merry * Tell the config_status routine that we're waiting so it won't 2067130f4520SKenneth D. Merry * clean up the LUN in the event of an error. 2068130f4520SKenneth D. Merry */ 2069130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2070130f4520SKenneth D. Merry 2071130f4520SKenneth D. Merry while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2072130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2073130f4520SKenneth D. Merry if (retval == EINTR) 2074130f4520SKenneth D. Merry break; 2075130f4520SKenneth D. Merry } 2076130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2077130f4520SKenneth D. Merry 2078130f4520SKenneth D. Merry if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2079130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2080130f4520SKenneth D. Merry "%s: LUN configuration error, see dmesg for details", 2081130f4520SKenneth D. Merry __func__); 2082130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2083130f4520SKenneth D. Merry links); 2084130f4520SKenneth D. Merry softc->num_luns--; 2085130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2086130f4520SKenneth D. Merry goto bailout_error; 2087130f4520SKenneth D. Merry } else { 2088130f4520SKenneth D. Merry params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2089130f4520SKenneth D. Merry } 2090130f4520SKenneth D. Merry 2091130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2092130f4520SKenneth D. Merry 2093130f4520SKenneth D. Merry be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2094130f4520SKenneth D. Merry be_lun->blocksize, 2095130f4520SKenneth D. Merry DEVSTAT_ALL_SUPPORTED, 2096130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type 2097130f4520SKenneth D. Merry | DEVSTAT_TYPE_IF_OTHER, 2098130f4520SKenneth D. Merry DEVSTAT_PRIORITY_OTHER); 2099130f4520SKenneth D. Merry 2100130f4520SKenneth D. Merry 2101130f4520SKenneth D. Merry req->status = CTL_LUN_OK; 2102130f4520SKenneth D. Merry 2103130f4520SKenneth D. Merry return (retval); 2104130f4520SKenneth D. Merry 2105130f4520SKenneth D. Merry bailout_error: 2106130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2107130f4520SKenneth D. Merry 21089e005bbcSAlexander Motin if (be_lun->io_taskqueue != NULL) 21099e005bbcSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 2110130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 21119e005bbcSAlexander Motin if (be_lun->dev_path != NULL) 2112130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 21139e005bbcSAlexander Motin if (be_lun->lun_zone != NULL) 21149e005bbcSAlexander Motin uma_zdestroy(be_lun->lun_zone); 211557a5db13SAlexander Motin ctl_free_opts(&be_lun->ctl_be_lun); 21169e005bbcSAlexander Motin mtx_destroy(&be_lun->lock); 2117130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2118130f4520SKenneth D. Merry 2119130f4520SKenneth D. Merry return (retval); 2120130f4520SKenneth D. Merry } 2121130f4520SKenneth D. Merry 2122130f4520SKenneth D. Merry static int 2123130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2124130f4520SKenneth D. Merry { 2125130f4520SKenneth D. Merry struct ctl_lun_rm_params *params; 2126130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2127130f4520SKenneth D. Merry int retval; 2128130f4520SKenneth D. Merry 2129130f4520SKenneth D. Merry params = &req->reqdata.rm; 2130130f4520SKenneth D. Merry 2131130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2132130f4520SKenneth D. Merry 2133130f4520SKenneth D. Merry be_lun = NULL; 2134130f4520SKenneth D. Merry 2135130f4520SKenneth D. Merry STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2136130f4520SKenneth D. Merry if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2137130f4520SKenneth D. Merry break; 2138130f4520SKenneth D. Merry } 2139130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2140130f4520SKenneth D. Merry 2141130f4520SKenneth D. Merry if (be_lun == NULL) { 2142130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2143130f4520SKenneth D. Merry "%s: LUN %u is not managed by the block backend", 2144130f4520SKenneth D. Merry __func__, params->lun_id); 2145130f4520SKenneth D. Merry goto bailout_error; 2146130f4520SKenneth D. Merry } 2147130f4520SKenneth D. Merry 2148130f4520SKenneth D. Merry retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2149130f4520SKenneth D. Merry 2150130f4520SKenneth D. Merry if (retval != 0) { 2151130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2152130f4520SKenneth D. Merry "%s: error %d returned from ctl_disable_lun() for " 2153130f4520SKenneth D. Merry "LUN %d", __func__, retval, params->lun_id); 2154130f4520SKenneth D. Merry goto bailout_error; 2155130f4520SKenneth D. Merry 2156130f4520SKenneth D. Merry } 2157130f4520SKenneth D. Merry 2158130f4520SKenneth D. Merry retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2159130f4520SKenneth D. Merry if (retval != 0) { 2160130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2161130f4520SKenneth D. Merry "%s: error %d returned from ctl_invalidate_lun() for " 2162130f4520SKenneth D. Merry "LUN %d", __func__, retval, params->lun_id); 2163130f4520SKenneth D. Merry goto bailout_error; 2164130f4520SKenneth D. Merry } 2165130f4520SKenneth D. Merry 2166130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2167130f4520SKenneth D. Merry 2168130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2169130f4520SKenneth D. Merry 2170130f4520SKenneth D. Merry while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2171130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2172130f4520SKenneth D. Merry if (retval == EINTR) 2173130f4520SKenneth D. Merry break; 2174130f4520SKenneth D. Merry } 2175130f4520SKenneth D. Merry 2176130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2177130f4520SKenneth D. Merry 2178130f4520SKenneth D. Merry if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2179130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2180130f4520SKenneth D. Merry "%s: interrupted waiting for LUN to be freed", 2181130f4520SKenneth D. Merry __func__); 2182130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2183130f4520SKenneth D. Merry goto bailout_error; 2184130f4520SKenneth D. Merry } 2185130f4520SKenneth D. Merry 2186130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2187130f4520SKenneth D. Merry 2188130f4520SKenneth D. Merry softc->num_luns--; 2189130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2190130f4520SKenneth D. Merry 2191130f4520SKenneth D. Merry taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2192130f4520SKenneth D. Merry 2193130f4520SKenneth D. Merry taskqueue_free(be_lun->io_taskqueue); 2194130f4520SKenneth D. Merry 2195130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 2196130f4520SKenneth D. Merry 2197130f4520SKenneth D. Merry if (be_lun->disk_stats != NULL) 2198130f4520SKenneth D. Merry devstat_remove_entry(be_lun->disk_stats); 2199130f4520SKenneth D. Merry 2200130f4520SKenneth D. Merry uma_zdestroy(be_lun->lun_zone); 2201130f4520SKenneth D. Merry 220257a5db13SAlexander Motin ctl_free_opts(&be_lun->ctl_be_lun); 2203130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 2204130f4520SKenneth D. Merry 2205130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2206130f4520SKenneth D. Merry 2207130f4520SKenneth D. Merry req->status = CTL_LUN_OK; 2208130f4520SKenneth D. Merry 2209130f4520SKenneth D. Merry return (0); 2210130f4520SKenneth D. Merry 2211130f4520SKenneth D. Merry bailout_error: 2212130f4520SKenneth D. Merry 2213130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2214130f4520SKenneth D. Merry 2215130f4520SKenneth D. Merry return (0); 2216130f4520SKenneth D. Merry } 2217130f4520SKenneth D. Merry 221881177295SEdward Tomasz Napierala static int 221981177295SEdward Tomasz Napierala ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 222081177295SEdward Tomasz Napierala struct ctl_lun_req *req) 222181177295SEdward Tomasz Napierala { 222281177295SEdward Tomasz Napierala struct vattr vattr; 222381177295SEdward Tomasz Napierala int error; 222481177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 222581177295SEdward Tomasz Napierala 222681177295SEdward Tomasz Napierala params = &req->reqdata.modify; 222781177295SEdward Tomasz Napierala 222881177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 222981177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 223081177295SEdward Tomasz Napierala } else { 223181177295SEdward Tomasz Napierala error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 223281177295SEdward Tomasz Napierala if (error != 0) { 223381177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 223481177295SEdward Tomasz Napierala "error calling VOP_GETATTR() for file %s", 223581177295SEdward Tomasz Napierala be_lun->dev_path); 223681177295SEdward Tomasz Napierala return (error); 223781177295SEdward Tomasz Napierala } 223881177295SEdward Tomasz Napierala 223981177295SEdward Tomasz Napierala be_lun->size_bytes = vattr.va_size; 224081177295SEdward Tomasz Napierala } 224181177295SEdward Tomasz Napierala 224281177295SEdward Tomasz Napierala return (0); 224381177295SEdward Tomasz Napierala } 224481177295SEdward Tomasz Napierala 224581177295SEdward Tomasz Napierala static int 224681177295SEdward Tomasz Napierala ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 224781177295SEdward Tomasz Napierala struct ctl_lun_req *req) 224881177295SEdward Tomasz Napierala { 224981177295SEdward Tomasz Napierala struct cdev *dev; 225081177295SEdward Tomasz Napierala struct cdevsw *devsw; 225181177295SEdward Tomasz Napierala int error; 225281177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 225381177295SEdward Tomasz Napierala uint64_t size_bytes; 225481177295SEdward Tomasz Napierala 225581177295SEdward Tomasz Napierala params = &req->reqdata.modify; 225681177295SEdward Tomasz Napierala 225781177295SEdward Tomasz Napierala dev = be_lun->vn->v_rdev; 225881177295SEdward Tomasz Napierala devsw = dev->si_devsw; 225981177295SEdward Tomasz Napierala if (!devsw->d_ioctl) { 226081177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 226181177295SEdward Tomasz Napierala "%s: no d_ioctl for device %s!", __func__, 226281177295SEdward Tomasz Napierala be_lun->dev_path); 226381177295SEdward Tomasz Napierala return (ENODEV); 226481177295SEdward Tomasz Napierala } 226581177295SEdward Tomasz Napierala 226681177295SEdward Tomasz Napierala error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 226781177295SEdward Tomasz Napierala (caddr_t)&size_bytes, FREAD, 226881177295SEdward Tomasz Napierala curthread); 226981177295SEdward Tomasz Napierala if (error) { 227081177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 227181177295SEdward Tomasz Napierala "%s: error %d returned for DIOCGMEDIASIZE ioctl " 227281177295SEdward Tomasz Napierala "on %s!", __func__, error, be_lun->dev_path); 227381177295SEdward Tomasz Napierala return (error); 227481177295SEdward Tomasz Napierala } 227581177295SEdward Tomasz Napierala 227681177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 227781177295SEdward Tomasz Napierala if (params->lun_size_bytes > size_bytes) { 227881177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 227981177295SEdward Tomasz Napierala "%s: requested LUN size %ju > backing device " 228081177295SEdward Tomasz Napierala "size %ju", __func__, 228181177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 228281177295SEdward Tomasz Napierala (uintmax_t)size_bytes); 228381177295SEdward Tomasz Napierala return (EINVAL); 228481177295SEdward Tomasz Napierala } 228581177295SEdward Tomasz Napierala 228681177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 228781177295SEdward Tomasz Napierala } else { 228881177295SEdward Tomasz Napierala be_lun->size_bytes = size_bytes; 228981177295SEdward Tomasz Napierala } 229081177295SEdward Tomasz Napierala 229181177295SEdward Tomasz Napierala return (0); 229281177295SEdward Tomasz Napierala } 229381177295SEdward Tomasz Napierala 229481177295SEdward Tomasz Napierala static int 229581177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 229681177295SEdward Tomasz Napierala { 229781177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 229881177295SEdward Tomasz Napierala struct ctl_be_block_lun *be_lun; 22995050aa86SKonstantin Belousov int error; 230081177295SEdward Tomasz Napierala 230181177295SEdward Tomasz Napierala params = &req->reqdata.modify; 230281177295SEdward Tomasz Napierala 230381177295SEdward Tomasz Napierala mtx_lock(&softc->lock); 230481177295SEdward Tomasz Napierala 230581177295SEdward Tomasz Napierala be_lun = NULL; 230681177295SEdward Tomasz Napierala 230781177295SEdward Tomasz Napierala STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 230881177295SEdward Tomasz Napierala if (be_lun->ctl_be_lun.lun_id == params->lun_id) 230981177295SEdward Tomasz Napierala break; 231081177295SEdward Tomasz Napierala } 231181177295SEdward Tomasz Napierala mtx_unlock(&softc->lock); 231281177295SEdward Tomasz Napierala 231381177295SEdward Tomasz Napierala if (be_lun == NULL) { 231481177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 231581177295SEdward Tomasz Napierala "%s: LUN %u is not managed by the block backend", 231681177295SEdward Tomasz Napierala __func__, params->lun_id); 231781177295SEdward Tomasz Napierala goto bailout_error; 231881177295SEdward Tomasz Napierala } 231981177295SEdward Tomasz Napierala 232081177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 232181177295SEdward Tomasz Napierala if (params->lun_size_bytes < be_lun->blocksize) { 232281177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 232381177295SEdward Tomasz Napierala "%s: LUN size %ju < blocksize %u", __func__, 232481177295SEdward Tomasz Napierala params->lun_size_bytes, be_lun->blocksize); 232581177295SEdward Tomasz Napierala goto bailout_error; 232681177295SEdward Tomasz Napierala } 232781177295SEdward Tomasz Napierala } 232881177295SEdward Tomasz Napierala 232981177295SEdward Tomasz Napierala vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 233081177295SEdward Tomasz Napierala 233181177295SEdward Tomasz Napierala if (be_lun->vn->v_type == VREG) 233281177295SEdward Tomasz Napierala error = ctl_be_block_modify_file(be_lun, req); 233381177295SEdward Tomasz Napierala else 233481177295SEdward Tomasz Napierala error = ctl_be_block_modify_dev(be_lun, req); 233581177295SEdward Tomasz Napierala 233681177295SEdward Tomasz Napierala VOP_UNLOCK(be_lun->vn, 0); 233781177295SEdward Tomasz Napierala 233881177295SEdward Tomasz Napierala if (error != 0) 233981177295SEdward Tomasz Napierala goto bailout_error; 234081177295SEdward Tomasz Napierala 234181177295SEdward Tomasz Napierala be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 234281177295SEdward Tomasz Napierala 234381177295SEdward Tomasz Napierala /* 234481177295SEdward Tomasz Napierala * The maximum LBA is the size - 1. 234581177295SEdward Tomasz Napierala * 234681177295SEdward Tomasz Napierala * XXX: Note that this field is being updated without locking, 234781177295SEdward Tomasz Napierala * which might cause problems on 32-bit architectures. 234881177295SEdward Tomasz Napierala */ 234981177295SEdward Tomasz Napierala be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 235081177295SEdward Tomasz Napierala ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 235181177295SEdward Tomasz Napierala 235281177295SEdward Tomasz Napierala /* Tell the user the exact size we ended up using */ 235381177295SEdward Tomasz Napierala params->lun_size_bytes = be_lun->size_bytes; 235481177295SEdward Tomasz Napierala 235581177295SEdward Tomasz Napierala req->status = CTL_LUN_OK; 235681177295SEdward Tomasz Napierala 235781177295SEdward Tomasz Napierala return (0); 235881177295SEdward Tomasz Napierala 235981177295SEdward Tomasz Napierala bailout_error: 236081177295SEdward Tomasz Napierala req->status = CTL_LUN_ERROR; 236181177295SEdward Tomasz Napierala 236281177295SEdward Tomasz Napierala return (0); 236381177295SEdward Tomasz Napierala } 236481177295SEdward Tomasz Napierala 2365130f4520SKenneth D. Merry static void 2366130f4520SKenneth D. Merry ctl_be_block_lun_shutdown(void *be_lun) 2367130f4520SKenneth D. Merry { 2368130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2369130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2370130f4520SKenneth D. Merry 2371130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2372130f4520SKenneth D. Merry 2373130f4520SKenneth D. Merry softc = lun->softc; 2374130f4520SKenneth D. Merry 2375130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2376130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2377130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2378130f4520SKenneth D. Merry wakeup(lun); 2379130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2380130f4520SKenneth D. Merry 2381130f4520SKenneth D. Merry } 2382130f4520SKenneth D. Merry 2383130f4520SKenneth D. Merry static void 2384130f4520SKenneth D. Merry ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2385130f4520SKenneth D. Merry { 2386130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2387130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2388130f4520SKenneth D. Merry 2389130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2390130f4520SKenneth D. Merry softc = lun->softc; 2391130f4520SKenneth D. Merry 2392130f4520SKenneth D. Merry if (status == CTL_LUN_CONFIG_OK) { 2393130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2394130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2395130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2396130f4520SKenneth D. Merry wakeup(lun); 2397130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2398130f4520SKenneth D. Merry 2399130f4520SKenneth D. Merry /* 2400130f4520SKenneth D. Merry * We successfully added the LUN, attempt to enable it. 2401130f4520SKenneth D. Merry */ 2402130f4520SKenneth D. Merry if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2403130f4520SKenneth D. Merry printf("%s: ctl_enable_lun() failed!\n", __func__); 2404130f4520SKenneth D. Merry if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2405130f4520SKenneth D. Merry printf("%s: ctl_invalidate_lun() failed!\n", 2406130f4520SKenneth D. Merry __func__); 2407130f4520SKenneth D. Merry } 2408130f4520SKenneth D. Merry } 2409130f4520SKenneth D. Merry 2410130f4520SKenneth D. Merry return; 2411130f4520SKenneth D. Merry } 2412130f4520SKenneth D. Merry 2413130f4520SKenneth D. Merry 2414130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2415130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2416130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2417130f4520SKenneth D. Merry wakeup(lun); 2418130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2419130f4520SKenneth D. Merry } 2420130f4520SKenneth D. Merry 2421130f4520SKenneth D. Merry 2422130f4520SKenneth D. Merry static int 2423130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io) 2424130f4520SKenneth D. Merry { 2425130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2426130f4520SKenneth D. Merry struct ctl_be_lun *ctl_be_lun; 2427130f4520SKenneth D. Merry int retval; 2428130f4520SKenneth D. Merry 2429130f4520SKenneth D. Merry retval = 0; 2430130f4520SKenneth D. Merry 2431130f4520SKenneth D. Merry DPRINTF("entered\n"); 2432130f4520SKenneth D. Merry 2433130f4520SKenneth D. Merry ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2434130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 2435130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2436130f4520SKenneth D. Merry 2437130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 2438130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 2439130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 2440ee7f31c0SAlexander Motin case WRITE_SAME_10: 2441ee7f31c0SAlexander Motin case WRITE_SAME_16: 2442ee7f31c0SAlexander Motin case UNMAP: 2443130f4520SKenneth D. Merry /* 2444130f4520SKenneth D. Merry * The upper level CTL code will filter out any CDBs with 2445130f4520SKenneth D. Merry * the immediate bit set and return the proper error. 2446130f4520SKenneth D. Merry * 2447130f4520SKenneth D. Merry * We don't really need to worry about what LBA range the 2448130f4520SKenneth D. Merry * user asked to be synced out. When they issue a sync 2449130f4520SKenneth D. Merry * cache command, we'll sync out the whole thing. 2450130f4520SKenneth D. Merry */ 2451130f4520SKenneth D. Merry mtx_lock(&be_lun->lock); 2452130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2453130f4520SKenneth D. Merry links); 2454130f4520SKenneth D. Merry mtx_unlock(&be_lun->lock); 2455130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2456130f4520SKenneth D. Merry break; 2457130f4520SKenneth D. Merry case START_STOP_UNIT: { 2458130f4520SKenneth D. Merry struct scsi_start_stop_unit *cdb; 2459130f4520SKenneth D. Merry 2460130f4520SKenneth D. Merry cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2461130f4520SKenneth D. Merry 2462130f4520SKenneth D. Merry if (cdb->how & SSS_START) 2463130f4520SKenneth D. Merry retval = ctl_start_lun(ctl_be_lun); 2464130f4520SKenneth D. Merry else { 2465130f4520SKenneth D. Merry retval = ctl_stop_lun(ctl_be_lun); 2466130f4520SKenneth D. Merry /* 2467130f4520SKenneth D. Merry * XXX KDM Copan-specific offline behavior. 2468130f4520SKenneth D. Merry * Figure out a reasonable way to port this? 2469130f4520SKenneth D. Merry */ 2470130f4520SKenneth D. Merry #ifdef NEEDTOPORT 2471130f4520SKenneth D. Merry if ((retval == 0) 2472130f4520SKenneth D. Merry && (cdb->byte2 & SSS_ONOFFLINE)) 2473130f4520SKenneth D. Merry retval = ctl_lun_offline(ctl_be_lun); 2474130f4520SKenneth D. Merry #endif 2475130f4520SKenneth D. Merry } 2476130f4520SKenneth D. Merry 2477130f4520SKenneth D. Merry /* 2478130f4520SKenneth D. Merry * In general, the above routines should not fail. They 2479130f4520SKenneth D. Merry * just set state for the LUN. So we've got something 2480130f4520SKenneth D. Merry * pretty wrong here if we can't start or stop the LUN. 2481130f4520SKenneth D. Merry */ 2482130f4520SKenneth D. Merry if (retval != 0) { 2483130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 2484130f4520SKenneth D. Merry /*sks_valid*/ 1, 2485130f4520SKenneth D. Merry /*retry_count*/ 0xf051); 2486130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2487130f4520SKenneth D. Merry } else { 2488130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 2489130f4520SKenneth D. Merry } 2490130f4520SKenneth D. Merry ctl_config_write_done(io); 2491130f4520SKenneth D. Merry break; 2492130f4520SKenneth D. Merry } 2493130f4520SKenneth D. Merry default: 2494130f4520SKenneth D. Merry ctl_set_invalid_opcode(&io->scsiio); 2495130f4520SKenneth D. Merry ctl_config_write_done(io); 2496130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2497130f4520SKenneth D. Merry break; 2498130f4520SKenneth D. Merry } 2499130f4520SKenneth D. Merry 2500130f4520SKenneth D. Merry return (retval); 2501130f4520SKenneth D. Merry 2502130f4520SKenneth D. Merry } 2503130f4520SKenneth D. Merry 2504130f4520SKenneth D. Merry static int 2505130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io) 2506130f4520SKenneth D. Merry { 2507130f4520SKenneth D. Merry return (0); 2508130f4520SKenneth D. Merry } 2509130f4520SKenneth D. Merry 2510130f4520SKenneth D. Merry static int 2511130f4520SKenneth D. Merry ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2512130f4520SKenneth D. Merry { 2513130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2514130f4520SKenneth D. Merry int retval; 2515130f4520SKenneth D. Merry 2516130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2517130f4520SKenneth D. Merry retval = 0; 2518130f4520SKenneth D. Merry 2519130f4520SKenneth D. Merry retval = sbuf_printf(sb, "<num_threads>"); 2520130f4520SKenneth D. Merry 2521130f4520SKenneth D. Merry if (retval != 0) 2522130f4520SKenneth D. Merry goto bailout; 2523130f4520SKenneth D. Merry 2524130f4520SKenneth D. Merry retval = sbuf_printf(sb, "%d", lun->num_threads); 2525130f4520SKenneth D. Merry 2526130f4520SKenneth D. Merry if (retval != 0) 2527130f4520SKenneth D. Merry goto bailout; 2528130f4520SKenneth D. Merry 2529130f4520SKenneth D. Merry retval = sbuf_printf(sb, "</num_threads>"); 2530130f4520SKenneth D. Merry 2531130f4520SKenneth D. Merry bailout: 2532130f4520SKenneth D. Merry 2533130f4520SKenneth D. Merry return (retval); 2534130f4520SKenneth D. Merry } 2535130f4520SKenneth D. Merry 2536130f4520SKenneth D. Merry int 2537130f4520SKenneth D. Merry ctl_be_block_init(void) 2538130f4520SKenneth D. Merry { 2539130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2540130f4520SKenneth D. Merry int retval; 2541130f4520SKenneth D. Merry 2542130f4520SKenneth D. Merry softc = &backend_block_softc; 2543130f4520SKenneth D. Merry retval = 0; 2544130f4520SKenneth D. Merry 2545130f4520SKenneth D. Merry mtx_init(&softc->lock, "ctlblk", NULL, MTX_DEF); 2546a0e36aeeSEdward Tomasz Napierala beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2547a0e36aeeSEdward Tomasz Napierala NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2548130f4520SKenneth D. Merry STAILQ_INIT(&softc->disk_list); 2549130f4520SKenneth D. Merry STAILQ_INIT(&softc->lun_list); 2550130f4520SKenneth D. Merry 2551130f4520SKenneth D. Merry return (retval); 2552130f4520SKenneth D. Merry } 2553