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> 71*ef8daf3fSAlexander Motin #include <sys/filio.h> 72130f4520SKenneth D. Merry #include <sys/proc.h> 73130f4520SKenneth D. Merry #include <sys/pcpu.h> 74130f4520SKenneth D. Merry #include <sys/module.h> 75130f4520SKenneth D. Merry #include <sys/sdt.h> 76130f4520SKenneth D. Merry #include <sys/devicestat.h> 77130f4520SKenneth D. Merry #include <sys/sysctl.h> 78130f4520SKenneth D. Merry 79130f4520SKenneth D. Merry #include <geom/geom.h> 80130f4520SKenneth D. Merry 81130f4520SKenneth D. Merry #include <cam/cam.h> 82130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h> 83130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h> 84130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h> 85130f4520SKenneth D. Merry #include <cam/ctl/ctl.h> 86130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h> 87130f4520SKenneth D. Merry #include <cam/ctl/ctl_frontend_internal.h> 88130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h> 89130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h> 90130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h> 91130f4520SKenneth D. Merry 92130f4520SKenneth D. Merry /* 9308a7cce5SAlexander Motin * The idea here is that we'll allocate enough S/G space to hold a 1MB 9408a7cce5SAlexander Motin * I/O. If we get an I/O larger than that, we'll split it. 95130f4520SKenneth D. Merry */ 9611b569f7SAlexander Motin #define CTLBLK_HALF_IO_SIZE (512 * 1024) 9711b569f7SAlexander Motin #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 9808a7cce5SAlexander Motin #define CTLBLK_MAX_SEG MAXPHYS 9911b569f7SAlexander Motin #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 10011b569f7SAlexander Motin #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 101130f4520SKenneth D. Merry 102130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG 103130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \ 104130f4520SKenneth D. Merry printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 105130f4520SKenneth D. Merry #else 106130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0) 107130f4520SKenneth D. Merry #endif 108130f4520SKenneth D. Merry 109e86a4142SAlexander Motin #define PRIV(io) \ 110e86a4142SAlexander Motin ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 11111b569f7SAlexander Motin #define ARGS(io) \ 11211b569f7SAlexander Motin ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 113e86a4142SAlexander Motin 114130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb); 115130f4520SKenneth D. Merry 116130f4520SKenneth D. Merry typedef enum { 117130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 118130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 119130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_WAITING = 0x04, 120130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 121130f4520SKenneth D. Merry } ctl_be_block_lun_flags; 122130f4520SKenneth D. Merry 123130f4520SKenneth D. Merry typedef enum { 124130f4520SKenneth D. Merry CTL_BE_BLOCK_NONE, 125130f4520SKenneth D. Merry CTL_BE_BLOCK_DEV, 126130f4520SKenneth D. Merry CTL_BE_BLOCK_FILE 127130f4520SKenneth D. Merry } ctl_be_block_type; 128130f4520SKenneth D. Merry 129130f4520SKenneth D. Merry struct ctl_be_block_devdata { 130130f4520SKenneth D. Merry struct cdev *cdev; 131130f4520SKenneth D. Merry struct cdevsw *csw; 132130f4520SKenneth D. Merry int dev_ref; 133130f4520SKenneth D. Merry }; 134130f4520SKenneth D. Merry 135130f4520SKenneth D. Merry struct ctl_be_block_filedata { 136130f4520SKenneth D. Merry struct ucred *cred; 137130f4520SKenneth D. Merry }; 138130f4520SKenneth D. Merry 139130f4520SKenneth D. Merry union ctl_be_block_bedata { 140130f4520SKenneth D. Merry struct ctl_be_block_devdata dev; 141130f4520SKenneth D. Merry struct ctl_be_block_filedata file; 142130f4520SKenneth D. Merry }; 143130f4520SKenneth D. Merry 144130f4520SKenneth D. Merry struct ctl_be_block_io; 145130f4520SKenneth D. Merry struct ctl_be_block_lun; 146130f4520SKenneth D. Merry 147130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 148130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 149c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 150c3e7ba3eSAlexander Motin const char *attrname); 151130f4520SKenneth D. Merry 152130f4520SKenneth D. Merry /* 153130f4520SKenneth D. Merry * Backend LUN structure. There is a 1:1 mapping between a block device 154130f4520SKenneth D. Merry * and a backend block LUN, and between a backend block LUN and a CTL LUN. 155130f4520SKenneth D. Merry */ 156130f4520SKenneth D. Merry struct ctl_be_block_lun { 15719720f41SAlexander Motin struct ctl_lun_create_params params; 158130f4520SKenneth D. Merry struct ctl_block_disk *disk; 159130f4520SKenneth D. Merry char lunname[32]; 160130f4520SKenneth D. Merry char *dev_path; 161130f4520SKenneth D. Merry ctl_be_block_type dev_type; 162130f4520SKenneth D. Merry struct vnode *vn; 163130f4520SKenneth D. Merry union ctl_be_block_bedata backend; 164130f4520SKenneth D. Merry cbb_dispatch_t dispatch; 165130f4520SKenneth D. Merry cbb_dispatch_t lun_flush; 166ee7f31c0SAlexander Motin cbb_dispatch_t unmap; 167*ef8daf3fSAlexander Motin cbb_dispatch_t get_lba_status; 168c3e7ba3eSAlexander Motin cbb_getattr_t getattr; 169130f4520SKenneth D. Merry uma_zone_t lun_zone; 170130f4520SKenneth D. Merry uint64_t size_blocks; 171130f4520SKenneth D. Merry uint64_t size_bytes; 172130f4520SKenneth D. Merry uint32_t blocksize; 173130f4520SKenneth D. Merry int blocksize_shift; 174f6012722SAlexander Motin uint16_t pblockexp; 175f6012722SAlexander Motin uint16_t pblockoff; 176130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 177130f4520SKenneth D. Merry struct devstat *disk_stats; 178130f4520SKenneth D. Merry ctl_be_block_lun_flags flags; 179130f4520SKenneth D. Merry STAILQ_ENTRY(ctl_be_block_lun) links; 180130f4520SKenneth D. Merry struct ctl_be_lun ctl_be_lun; 181130f4520SKenneth D. Merry struct taskqueue *io_taskqueue; 182130f4520SKenneth D. Merry struct task io_task; 183130f4520SKenneth D. Merry int num_threads; 184130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) input_queue; 185*ef8daf3fSAlexander Motin STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 186130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 187130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 18875c7a1d3SAlexander Motin struct mtx_padalign io_lock; 18975c7a1d3SAlexander Motin struct mtx_padalign queue_lock; 190130f4520SKenneth D. Merry }; 191130f4520SKenneth D. Merry 192130f4520SKenneth D. Merry /* 193130f4520SKenneth D. Merry * Overall softc structure for the block backend module. 194130f4520SKenneth D. Merry */ 195130f4520SKenneth D. Merry struct ctl_be_block_softc { 196130f4520SKenneth D. Merry struct mtx lock; 197130f4520SKenneth D. Merry int num_disks; 198130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_block_disk) disk_list; 199130f4520SKenneth D. Merry int num_luns; 200130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_be_block_lun) lun_list; 201130f4520SKenneth D. Merry }; 202130f4520SKenneth D. Merry 203130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc; 204130f4520SKenneth D. Merry 205130f4520SKenneth D. Merry /* 206130f4520SKenneth D. Merry * Per-I/O information. 207130f4520SKenneth D. Merry */ 208130f4520SKenneth D. Merry struct ctl_be_block_io { 209130f4520SKenneth D. Merry union ctl_io *io; 210130f4520SKenneth D. Merry struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 211130f4520SKenneth D. Merry struct iovec xiovecs[CTLBLK_MAX_SEGS]; 212130f4520SKenneth D. Merry int bio_cmd; 213130f4520SKenneth D. Merry int num_segs; 214130f4520SKenneth D. Merry int num_bios_sent; 215130f4520SKenneth D. Merry int num_bios_done; 216130f4520SKenneth D. Merry int send_complete; 217130f4520SKenneth D. Merry int num_errors; 218130f4520SKenneth D. Merry struct bintime ds_t0; 219130f4520SKenneth D. Merry devstat_tag_type ds_tag_type; 220130f4520SKenneth D. Merry devstat_trans_flags ds_trans_type; 221130f4520SKenneth D. Merry uint64_t io_len; 222130f4520SKenneth D. Merry uint64_t io_offset; 223130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 224130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 225ee7f31c0SAlexander Motin void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 226130f4520SKenneth D. Merry }; 227130f4520SKenneth D. Merry 228130f4520SKenneth D. Merry static int cbb_num_threads = 14; 229130f4520SKenneth D. Merry SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 230130f4520SKenneth D. Merry "CAM Target Layer Block Backend"); 231af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 232130f4520SKenneth D. Merry &cbb_num_threads, 0, "Number of threads per backing file"); 233130f4520SKenneth D. Merry 234130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 235130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio); 236130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio); 237130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io); 238130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio); 239130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 240130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 241130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 242130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 243*ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 244*ef8daf3fSAlexander Motin struct ctl_be_block_io *beio); 245130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 246130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 247ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 248ee7f31c0SAlexander Motin struct ctl_be_block_io *beio); 249130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 250130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 251c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 252c3e7ba3eSAlexander Motin const char *attrname); 253*ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 254*ef8daf3fSAlexander Motin union ctl_io *io); 255130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 256130f4520SKenneth D. Merry union ctl_io *io); 257130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 258130f4520SKenneth D. Merry union ctl_io *io); 259130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending); 260130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io); 261130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 262130f4520SKenneth D. Merry int flag, struct thread *td); 263130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 264130f4520SKenneth D. Merry struct ctl_lun_req *req); 265130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 266130f4520SKenneth D. Merry struct ctl_lun_req *req); 267130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 268130f4520SKenneth D. Merry static int ctl_be_block_open(struct ctl_be_block_softc *softc, 269130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, 270130f4520SKenneth D. Merry struct ctl_lun_req *req); 271130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc, 272130f4520SKenneth D. Merry struct ctl_lun_req *req); 273130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 274130f4520SKenneth D. Merry struct ctl_lun_req *req); 27581177295SEdward Tomasz Napierala static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 27681177295SEdward Tomasz Napierala struct ctl_lun_req *req); 27781177295SEdward Tomasz Napierala static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 27881177295SEdward Tomasz Napierala struct ctl_lun_req *req); 27981177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 28081177295SEdward Tomasz Napierala struct ctl_lun_req *req); 281130f4520SKenneth D. Merry static void ctl_be_block_lun_shutdown(void *be_lun); 282130f4520SKenneth D. Merry static void ctl_be_block_lun_config_status(void *be_lun, 283130f4520SKenneth D. Merry ctl_lun_config_status status); 284130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io); 285130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io); 286130f4520SKenneth D. Merry static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 287c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 288130f4520SKenneth D. Merry int ctl_be_block_init(void); 289130f4520SKenneth D. Merry 290130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver = 291130f4520SKenneth D. Merry { 2922a2443d8SKenneth D. Merry .name = "block", 2932a2443d8SKenneth D. Merry .flags = CTL_BE_FLAG_HAS_CONFIG, 2942a2443d8SKenneth D. Merry .init = ctl_be_block_init, 2952a2443d8SKenneth D. Merry .data_submit = ctl_be_block_submit, 2962a2443d8SKenneth D. Merry .data_move_done = ctl_be_block_move_done, 2972a2443d8SKenneth D. Merry .config_read = ctl_be_block_config_read, 2982a2443d8SKenneth D. Merry .config_write = ctl_be_block_config_write, 2992a2443d8SKenneth D. Merry .ioctl = ctl_be_block_ioctl, 300c3e7ba3eSAlexander Motin .lun_info = ctl_be_block_lun_info, 301c3e7ba3eSAlexander Motin .lun_attr = ctl_be_block_lun_attr 302130f4520SKenneth D. Merry }; 303130f4520SKenneth D. Merry 304130f4520SKenneth D. Merry MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 305130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 306130f4520SKenneth D. Merry 307a0e36aeeSEdward Tomasz Napierala static uma_zone_t beio_zone; 308a0e36aeeSEdward Tomasz Napierala 309130f4520SKenneth D. Merry static struct ctl_be_block_io * 310130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc) 311130f4520SKenneth D. Merry { 312130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 313130f4520SKenneth D. Merry 314a0e36aeeSEdward Tomasz Napierala beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 315130f4520SKenneth D. Merry beio->softc = softc; 316130f4520SKenneth D. Merry return (beio); 317130f4520SKenneth D. Merry } 318130f4520SKenneth D. Merry 319130f4520SKenneth D. Merry static void 320130f4520SKenneth D. Merry ctl_free_beio(struct ctl_be_block_io *beio) 321130f4520SKenneth D. Merry { 322130f4520SKenneth D. Merry int duplicate_free; 323130f4520SKenneth D. Merry int i; 324130f4520SKenneth D. Merry 325130f4520SKenneth D. Merry duplicate_free = 0; 326130f4520SKenneth D. Merry 327130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 328130f4520SKenneth D. Merry if (beio->sg_segs[i].addr == NULL) 329130f4520SKenneth D. Merry duplicate_free++; 330130f4520SKenneth D. Merry 331130f4520SKenneth D. Merry uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 332130f4520SKenneth D. Merry beio->sg_segs[i].addr = NULL; 33311b569f7SAlexander Motin 33411b569f7SAlexander Motin /* For compare we had two equal S/G lists. */ 33511b569f7SAlexander Motin if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 33611b569f7SAlexander Motin uma_zfree(beio->lun->lun_zone, 33711b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 33811b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 33911b569f7SAlexander Motin } 340130f4520SKenneth D. Merry } 341130f4520SKenneth D. Merry 342130f4520SKenneth D. Merry if (duplicate_free > 0) { 343130f4520SKenneth D. Merry printf("%s: %d duplicate frees out of %d segments\n", __func__, 344130f4520SKenneth D. Merry duplicate_free, beio->num_segs); 345130f4520SKenneth D. Merry } 346a0e36aeeSEdward Tomasz Napierala 347a0e36aeeSEdward Tomasz Napierala uma_zfree(beio_zone, beio); 348130f4520SKenneth D. Merry } 349130f4520SKenneth D. Merry 350130f4520SKenneth D. Merry static void 351130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio) 352130f4520SKenneth D. Merry { 35375c7a1d3SAlexander Motin union ctl_io *io = beio->io; 354130f4520SKenneth D. Merry 355ee7f31c0SAlexander Motin if (beio->beio_cont != NULL) { 356ee7f31c0SAlexander Motin beio->beio_cont(beio); 357ee7f31c0SAlexander Motin } else { 358130f4520SKenneth D. Merry ctl_free_beio(beio); 35911b569f7SAlexander Motin ctl_data_submit_done(io); 360130f4520SKenneth D. Merry } 361ee7f31c0SAlexander Motin } 362130f4520SKenneth D. Merry 363130f4520SKenneth D. Merry static int 364130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io) 365130f4520SKenneth D. Merry { 366130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 367130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 36811b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 369130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 370130f4520SKenneth D. Merry struct bintime cur_bt; 371130f4520SKenneth D. Merry #endif 37211b569f7SAlexander Motin int i; 373130f4520SKenneth D. Merry 374e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 375130f4520SKenneth D. Merry be_lun = beio->lun; 376130f4520SKenneth D. Merry 377130f4520SKenneth D. Merry DPRINTF("entered\n"); 378130f4520SKenneth D. Merry 379130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 380130f4520SKenneth D. Merry getbintime(&cur_bt); 381130f4520SKenneth D. Merry bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 382130f4520SKenneth D. Merry bintime_add(&io->io_hdr.dma_bt, &cur_bt); 383130f4520SKenneth D. Merry io->io_hdr.num_dmas++; 384130f4520SKenneth D. Merry #endif 38511b569f7SAlexander Motin io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 386130f4520SKenneth D. Merry 387130f4520SKenneth D. Merry /* 388130f4520SKenneth D. Merry * We set status at this point for read commands, and write 389130f4520SKenneth D. Merry * commands with errors. 390130f4520SKenneth D. Merry */ 391f7241cceSAlexander Motin if (io->io_hdr.flags & CTL_FLAG_ABORT) { 392f7241cceSAlexander Motin ; 393f7241cceSAlexander Motin } else if ((io->io_hdr.port_status == 0) && 39411b569f7SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 39511b569f7SAlexander Motin lbalen = ARGS(beio->io); 39611b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_READ) { 397130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 39811b569f7SAlexander Motin } else if (lbalen->flags & CTL_LLF_COMPARE) { 39911b569f7SAlexander Motin /* We have two data blocks ready for comparison. */ 40011b569f7SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 40111b569f7SAlexander Motin if (memcmp(beio->sg_segs[i].addr, 40211b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 40311b569f7SAlexander Motin beio->sg_segs[i].len) != 0) 40411b569f7SAlexander Motin break; 40511b569f7SAlexander Motin } 40611b569f7SAlexander Motin if (i < beio->num_segs) 40711b569f7SAlexander Motin ctl_set_sense(&io->scsiio, 40811b569f7SAlexander Motin /*current_error*/ 1, 40911b569f7SAlexander Motin /*sense_key*/ SSD_KEY_MISCOMPARE, 41011b569f7SAlexander Motin /*asc*/ 0x1D, 41111b569f7SAlexander Motin /*ascq*/ 0x00, 41211b569f7SAlexander Motin SSD_ELEM_NONE); 41311b569f7SAlexander Motin else 41411b569f7SAlexander Motin ctl_set_success(&io->scsiio); 41511b569f7SAlexander Motin } 416f7241cceSAlexander Motin } else if ((io->io_hdr.port_status != 0) && 417f7241cceSAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 418f7241cceSAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 419130f4520SKenneth D. Merry /* 420130f4520SKenneth D. Merry * For hardware error sense keys, the sense key 421130f4520SKenneth D. Merry * specific value is defined to be a retry count, 422130f4520SKenneth D. Merry * but we use it to pass back an internal FETD 423130f4520SKenneth D. Merry * error code. XXX KDM Hopefully the FETD is only 424130f4520SKenneth D. Merry * using 16 bits for an error code, since that's 425130f4520SKenneth D. Merry * all the space we have in the sks field. 426130f4520SKenneth D. Merry */ 427130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 428130f4520SKenneth D. Merry /*sks_valid*/ 1, 429130f4520SKenneth D. Merry /*retry_count*/ 430130f4520SKenneth D. Merry io->io_hdr.port_status); 431130f4520SKenneth D. Merry } 432130f4520SKenneth D. Merry 433130f4520SKenneth D. Merry /* 434130f4520SKenneth D. Merry * If this is a read, or a write with errors, it is done. 435130f4520SKenneth D. Merry */ 436130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_READ) 437130f4520SKenneth D. Merry || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 438130f4520SKenneth D. Merry || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 439130f4520SKenneth D. Merry ctl_complete_beio(beio); 440130f4520SKenneth D. Merry return (0); 441130f4520SKenneth D. Merry } 442130f4520SKenneth D. Merry 443130f4520SKenneth D. Merry /* 444130f4520SKenneth D. Merry * At this point, we have a write and the DMA completed 445130f4520SKenneth D. Merry * successfully. We now have to queue it to the task queue to 446130f4520SKenneth D. Merry * execute the backend I/O. That is because we do blocking 447130f4520SKenneth D. Merry * memory allocations, and in the file backing case, blocking I/O. 448130f4520SKenneth D. Merry * This move done routine is generally called in the SIM's 449130f4520SKenneth D. Merry * interrupt context, and therefore we cannot block. 450130f4520SKenneth D. Merry */ 45175c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 452130f4520SKenneth D. Merry /* 453130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 454130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 455130f4520SKenneth D. Merry * or deal with resource allocation here. 456130f4520SKenneth D. Merry */ 457130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 45875c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 459130f4520SKenneth D. Merry 460130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 461130f4520SKenneth D. Merry 462130f4520SKenneth D. Merry return (0); 463130f4520SKenneth D. Merry } 464130f4520SKenneth D. Merry 465130f4520SKenneth D. Merry static void 466130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio) 467130f4520SKenneth D. Merry { 468130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 469130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 470130f4520SKenneth D. Merry union ctl_io *io; 471e0c2f975SAlexander Motin int error; 472130f4520SKenneth D. Merry 473130f4520SKenneth D. Merry beio = bio->bio_caller1; 474130f4520SKenneth D. Merry be_lun = beio->lun; 475130f4520SKenneth D. Merry io = beio->io; 476130f4520SKenneth D. Merry 477130f4520SKenneth D. Merry DPRINTF("entered\n"); 478130f4520SKenneth D. Merry 479e0c2f975SAlexander Motin error = bio->bio_error; 48075c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 481e0c2f975SAlexander Motin if (error != 0) 482130f4520SKenneth D. Merry beio->num_errors++; 483130f4520SKenneth D. Merry 484130f4520SKenneth D. Merry beio->num_bios_done++; 485130f4520SKenneth D. Merry 486130f4520SKenneth D. Merry /* 487130f4520SKenneth D. Merry * XXX KDM will this cause WITNESS to complain? Holding a lock 488130f4520SKenneth D. Merry * during the free might cause it to complain. 489130f4520SKenneth D. Merry */ 490130f4520SKenneth D. Merry g_destroy_bio(bio); 491130f4520SKenneth D. Merry 492130f4520SKenneth D. Merry /* 493130f4520SKenneth D. Merry * If the send complete bit isn't set, or we aren't the last I/O to 494130f4520SKenneth D. Merry * complete, then we're done. 495130f4520SKenneth D. Merry */ 496130f4520SKenneth D. Merry if ((beio->send_complete == 0) 497130f4520SKenneth D. Merry || (beio->num_bios_done < beio->num_bios_sent)) { 49875c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 499130f4520SKenneth D. Merry return; 500130f4520SKenneth D. Merry } 501130f4520SKenneth D. Merry 502130f4520SKenneth D. Merry /* 503130f4520SKenneth D. Merry * At this point, we've verified that we are the last I/O to 504130f4520SKenneth D. Merry * complete, so it's safe to drop the lock. 505130f4520SKenneth D. Merry */ 50675c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 50775c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 50875c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 50975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 510130f4520SKenneth D. Merry 511130f4520SKenneth D. Merry /* 512130f4520SKenneth D. Merry * If there are any errors from the backing device, we fail the 513130f4520SKenneth D. Merry * entire I/O with a medium error. 514130f4520SKenneth D. Merry */ 515130f4520SKenneth D. Merry if (beio->num_errors > 0) { 516e0c2f975SAlexander Motin if (error == EOPNOTSUPP) { 517e0c2f975SAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 5184fc18ff9SAlexander Motin } else if (error == ENOSPC) { 5194fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 520e0c2f975SAlexander Motin } else if (beio->bio_cmd == BIO_FLUSH) { 521130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 522130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 523130f4520SKenneth D. Merry /*sks_valid*/ 1, 524130f4520SKenneth D. Merry /*retry_count*/ 0xbad2); 525130f4520SKenneth D. Merry } else 526130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 527130f4520SKenneth D. Merry ctl_complete_beio(beio); 528130f4520SKenneth D. Merry return; 529130f4520SKenneth D. Merry } 530130f4520SKenneth D. Merry 531130f4520SKenneth D. Merry /* 53211b569f7SAlexander Motin * If this is a write, a flush, a delete or verify, we're all done. 533130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 534130f4520SKenneth D. Merry */ 535130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_WRITE) 536ee7f31c0SAlexander Motin || (beio->bio_cmd == BIO_FLUSH) 53711b569f7SAlexander Motin || (beio->bio_cmd == BIO_DELETE) 53811b569f7SAlexander Motin || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 539130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 540130f4520SKenneth D. Merry ctl_complete_beio(beio); 541130f4520SKenneth D. Merry } else { 542f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 543f7241cceSAlexander Motin beio->beio_cont == NULL) 544f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 545130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 546130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 547130f4520SKenneth D. Merry #endif 548130f4520SKenneth D. Merry ctl_datamove(io); 549130f4520SKenneth D. Merry } 550130f4520SKenneth D. Merry } 551130f4520SKenneth D. Merry 552130f4520SKenneth D. Merry static void 553130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 554130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 555130f4520SKenneth D. Merry { 55675c7a1d3SAlexander Motin union ctl_io *io = beio->io; 557130f4520SKenneth D. Merry struct mount *mountpoint; 5585050aa86SKonstantin Belousov int error, lock_flags; 559130f4520SKenneth D. Merry 560130f4520SKenneth D. Merry DPRINTF("entered\n"); 561130f4520SKenneth D. Merry 56275c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 56375c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 56475c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 56575c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 566130f4520SKenneth D. Merry 567130f4520SKenneth D. Merry (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 568130f4520SKenneth D. Merry 569130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 570130f4520SKenneth D. Merry || ((mountpoint == NULL) 571130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 572130f4520SKenneth D. Merry lock_flags = LK_SHARED; 573130f4520SKenneth D. Merry else 574130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 575130f4520SKenneth D. Merry 576130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 577130f4520SKenneth D. Merry 578130f4520SKenneth D. Merry error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 579130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 580130f4520SKenneth D. Merry 581130f4520SKenneth D. Merry vn_finished_write(mountpoint); 582130f4520SKenneth D. Merry 58375c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 58475c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 58575c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 58675c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 58775c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 58875c7a1d3SAlexander Motin 589130f4520SKenneth D. Merry if (error == 0) 590130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 591130f4520SKenneth D. Merry else { 592130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 593130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 594130f4520SKenneth D. Merry /*sks_valid*/ 1, 595130f4520SKenneth D. Merry /*retry_count*/ 0xbad1); 596130f4520SKenneth D. Merry } 597130f4520SKenneth D. Merry 598130f4520SKenneth D. Merry ctl_complete_beio(beio); 599130f4520SKenneth D. Merry } 600130f4520SKenneth D. Merry 601d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 602d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 603d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 604d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 605130f4520SKenneth D. Merry 606130f4520SKenneth D. Merry static void 607130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 608130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 609130f4520SKenneth D. Merry { 610130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 611130f4520SKenneth D. Merry union ctl_io *io; 612130f4520SKenneth D. Merry struct uio xuio; 613130f4520SKenneth D. Merry struct iovec *xiovec; 6145050aa86SKonstantin Belousov int flags; 615130f4520SKenneth D. Merry int error, i; 616130f4520SKenneth D. Merry 617130f4520SKenneth D. Merry DPRINTF("entered\n"); 618130f4520SKenneth D. Merry 619130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 620130f4520SKenneth D. Merry io = beio->io; 62155551d05SAlexander Motin flags = 0; 62255551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 62355551d05SAlexander Motin flags |= IO_DIRECT; 62455551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 62555551d05SAlexander Motin flags |= IO_SYNC; 626130f4520SKenneth D. Merry 62711b569f7SAlexander Motin bzero(&xuio, sizeof(xuio)); 628130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 629130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 63011b569f7SAlexander Motin xuio.uio_rw = UIO_READ; 631130f4520SKenneth D. Merry } else { 632130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 633130f4520SKenneth D. Merry xuio.uio_rw = UIO_WRITE; 63411b569f7SAlexander Motin } 635130f4520SKenneth D. Merry xuio.uio_offset = beio->io_offset; 636130f4520SKenneth D. Merry xuio.uio_resid = beio->io_len; 637130f4520SKenneth D. Merry xuio.uio_segflg = UIO_SYSSPACE; 638130f4520SKenneth D. Merry xuio.uio_iov = beio->xiovecs; 639130f4520SKenneth D. Merry xuio.uio_iovcnt = beio->num_segs; 640130f4520SKenneth D. Merry xuio.uio_td = curthread; 641130f4520SKenneth D. Merry 642130f4520SKenneth D. Merry for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 643130f4520SKenneth D. Merry xiovec->iov_base = beio->sg_segs[i].addr; 644130f4520SKenneth D. Merry xiovec->iov_len = beio->sg_segs[i].len; 645130f4520SKenneth D. Merry } 646130f4520SKenneth D. Merry 64775c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 64875c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 64975c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 65075c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 65175c7a1d3SAlexander Motin 652130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 653130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 654130f4520SKenneth D. Merry 655130f4520SKenneth D. Merry /* 656130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for reads. If the 657130f4520SKenneth D. Merry * DIRECTIO option is configured into the kernel, it calls 658130f4520SKenneth D. Merry * ffs_rawread(). But that only works for single-segment 659130f4520SKenneth D. Merry * uios with user space addresses. In our case, with a 660130f4520SKenneth D. Merry * kernel uio, it still reads into the buffer cache, but it 661130f4520SKenneth D. Merry * will just try to release the buffer from the cache later 662130f4520SKenneth D. Merry * on in ffs_read(). 663130f4520SKenneth D. Merry * 664130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for reads. 665130f4520SKenneth D. Merry * 666130f4520SKenneth D. Merry * UFS does not pay attention to IO_SYNC for reads. 667130f4520SKenneth D. Merry * 668130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (which translates into the 669130f4520SKenneth D. Merry * Solaris define FRSYNC for zfs_read()) for reads. It 670130f4520SKenneth D. Merry * attempts to sync the file before reading. 671130f4520SKenneth D. Merry * 672130f4520SKenneth D. Merry * So, to attempt to provide some barrier semantics in the 673130f4520SKenneth D. Merry * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 674130f4520SKenneth D. Merry */ 67555551d05SAlexander Motin error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 676130f4520SKenneth D. Merry 677130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 67811b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 679130f4520SKenneth D. Merry } else { 680130f4520SKenneth D. Merry struct mount *mountpoint; 681130f4520SKenneth D. Merry int lock_flags; 682130f4520SKenneth D. Merry 683130f4520SKenneth D. Merry (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 684130f4520SKenneth D. Merry 685130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 686130f4520SKenneth D. Merry || ((mountpoint == NULL) 687130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 688130f4520SKenneth D. Merry lock_flags = LK_SHARED; 689130f4520SKenneth D. Merry else 690130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 691130f4520SKenneth D. Merry 692130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 693130f4520SKenneth D. Merry 694130f4520SKenneth D. Merry /* 695130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for writes. The write 696130f4520SKenneth D. Merry * is done asynchronously. (Normally the write would just 697130f4520SKenneth D. Merry * get put into cache. 698130f4520SKenneth D. Merry * 699130f4520SKenneth D. Merry * UFS pays attention to IO_SYNC for writes. It will 700130f4520SKenneth D. Merry * attempt to write the buffer out synchronously if that 701130f4520SKenneth D. Merry * flag is set. 702130f4520SKenneth D. Merry * 703130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for writes. 704130f4520SKenneth D. Merry * 705130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 706130f4520SKenneth D. Merry * for writes. It will flush the transaction from the 707130f4520SKenneth D. Merry * cache before returning. 708130f4520SKenneth D. Merry * 709130f4520SKenneth D. Merry * So if we've got the BIO_ORDERED flag set, we want 710130f4520SKenneth D. Merry * IO_SYNC in either the UFS or ZFS case. 711130f4520SKenneth D. Merry */ 71255551d05SAlexander Motin error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 713130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 714130f4520SKenneth D. Merry 715130f4520SKenneth D. Merry vn_finished_write(mountpoint); 71611b569f7SAlexander Motin SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 717130f4520SKenneth D. Merry } 718130f4520SKenneth D. Merry 71975c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 72075c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 72175c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 72275c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 72375c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 72475c7a1d3SAlexander Motin 725130f4520SKenneth D. Merry /* 726130f4520SKenneth D. Merry * If we got an error, set the sense data to "MEDIUM ERROR" and 727130f4520SKenneth D. Merry * return the I/O to the user. 728130f4520SKenneth D. Merry */ 729130f4520SKenneth D. Merry if (error != 0) { 730130f4520SKenneth D. Merry char path_str[32]; 731130f4520SKenneth D. Merry 732130f4520SKenneth D. Merry ctl_scsi_path_string(io, path_str, sizeof(path_str)); 733130f4520SKenneth D. Merry printf("%s%s command returned errno %d\n", path_str, 734130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 7354fc18ff9SAlexander Motin if (error == ENOSPC) { 7364fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 7374fc18ff9SAlexander Motin } else 738130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 739130f4520SKenneth D. Merry ctl_complete_beio(beio); 740130f4520SKenneth D. Merry return; 741130f4520SKenneth D. Merry } 742130f4520SKenneth D. Merry 743130f4520SKenneth D. Merry /* 744696297adSAlexander Motin * If this is a write or a verify, we're all done. 745130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 746130f4520SKenneth D. Merry */ 747696297adSAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 748696297adSAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 749130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 750130f4520SKenneth D. Merry ctl_complete_beio(beio); 751130f4520SKenneth D. Merry } else { 752f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 753f7241cceSAlexander Motin beio->beio_cont == NULL) 754f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 755130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 756130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 757130f4520SKenneth D. Merry #endif 758130f4520SKenneth D. Merry ctl_datamove(io); 759130f4520SKenneth D. Merry } 760130f4520SKenneth D. Merry } 761130f4520SKenneth D. Merry 762130f4520SKenneth D. Merry static void 763*ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 764*ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 765*ef8daf3fSAlexander Motin { 766*ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 767*ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 768*ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 769*ef8daf3fSAlexander Motin off_t roff, off; 770*ef8daf3fSAlexander Motin int error, status; 771*ef8daf3fSAlexander Motin 772*ef8daf3fSAlexander Motin DPRINTF("entered\n"); 773*ef8daf3fSAlexander Motin 774*ef8daf3fSAlexander Motin off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 775*ef8daf3fSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 776*ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 777*ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 778*ef8daf3fSAlexander Motin if (error == 0 && off > roff) 779*ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 780*ef8daf3fSAlexander Motin else { 781*ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 782*ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 783*ef8daf3fSAlexander Motin if (error == 0 && off > roff) 784*ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 785*ef8daf3fSAlexander Motin else { 786*ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 787*ef8daf3fSAlexander Motin off = be_lun->size_bytes; 788*ef8daf3fSAlexander Motin } 789*ef8daf3fSAlexander Motin } 790*ef8daf3fSAlexander Motin VOP_UNLOCK(be_lun->vn, 0); 791*ef8daf3fSAlexander Motin 792*ef8daf3fSAlexander Motin off >>= be_lun->blocksize_shift; 793*ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 794*ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 795*ef8daf3fSAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 796*ef8daf3fSAlexander Motin data->descr[0].length); 797*ef8daf3fSAlexander Motin data->descr[0].status = status; 798*ef8daf3fSAlexander Motin 799*ef8daf3fSAlexander Motin ctl_complete_beio(beio); 800*ef8daf3fSAlexander Motin } 801*ef8daf3fSAlexander Motin 802*ef8daf3fSAlexander Motin static void 80367f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 80467f586a8SAlexander Motin struct ctl_be_block_io *beio) 80567f586a8SAlexander Motin { 80667f586a8SAlexander Motin struct ctl_be_block_devdata *dev_data; 80767f586a8SAlexander Motin union ctl_io *io; 80867f586a8SAlexander Motin struct uio xuio; 80967f586a8SAlexander Motin struct iovec *xiovec; 81067f586a8SAlexander Motin int flags; 81167f586a8SAlexander Motin int error, i; 81267f586a8SAlexander Motin 81367f586a8SAlexander Motin DPRINTF("entered\n"); 81467f586a8SAlexander Motin 81567f586a8SAlexander Motin dev_data = &be_lun->backend.dev; 81667f586a8SAlexander Motin io = beio->io; 81755551d05SAlexander Motin flags = 0; 81855551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 81955551d05SAlexander Motin flags |= IO_DIRECT; 82055551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 82155551d05SAlexander Motin flags |= IO_SYNC; 82267f586a8SAlexander Motin 82367f586a8SAlexander Motin bzero(&xuio, sizeof(xuio)); 82467f586a8SAlexander Motin if (beio->bio_cmd == BIO_READ) { 82567f586a8SAlexander Motin SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 82667f586a8SAlexander Motin xuio.uio_rw = UIO_READ; 82767f586a8SAlexander Motin } else { 82867f586a8SAlexander Motin SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 82967f586a8SAlexander Motin xuio.uio_rw = UIO_WRITE; 83067f586a8SAlexander Motin } 83167f586a8SAlexander Motin xuio.uio_offset = beio->io_offset; 83267f586a8SAlexander Motin xuio.uio_resid = beio->io_len; 83367f586a8SAlexander Motin xuio.uio_segflg = UIO_SYSSPACE; 83467f586a8SAlexander Motin xuio.uio_iov = beio->xiovecs; 83567f586a8SAlexander Motin xuio.uio_iovcnt = beio->num_segs; 83667f586a8SAlexander Motin xuio.uio_td = curthread; 83767f586a8SAlexander Motin 83867f586a8SAlexander Motin for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 83967f586a8SAlexander Motin xiovec->iov_base = beio->sg_segs[i].addr; 84067f586a8SAlexander Motin xiovec->iov_len = beio->sg_segs[i].len; 84167f586a8SAlexander Motin } 84267f586a8SAlexander Motin 84367f586a8SAlexander Motin binuptime(&beio->ds_t0); 84467f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 84567f586a8SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 84667f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 84767f586a8SAlexander Motin 84867f586a8SAlexander Motin if (beio->bio_cmd == BIO_READ) { 84955551d05SAlexander Motin error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 85067f586a8SAlexander Motin SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 85167f586a8SAlexander Motin } else { 85255551d05SAlexander Motin error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 85367f586a8SAlexander Motin SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 85467f586a8SAlexander Motin } 85567f586a8SAlexander Motin 85667f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 85767f586a8SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 85867f586a8SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 85967f586a8SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 86067f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 86167f586a8SAlexander Motin 86267f586a8SAlexander Motin /* 86367f586a8SAlexander Motin * If we got an error, set the sense data to "MEDIUM ERROR" and 86467f586a8SAlexander Motin * return the I/O to the user. 86567f586a8SAlexander Motin */ 86667f586a8SAlexander Motin if (error != 0) { 8674fc18ff9SAlexander Motin if (error == ENOSPC) { 8684fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 8694fc18ff9SAlexander Motin } else 87067f586a8SAlexander Motin ctl_set_medium_error(&io->scsiio); 87167f586a8SAlexander Motin ctl_complete_beio(beio); 87267f586a8SAlexander Motin return; 87367f586a8SAlexander Motin } 87467f586a8SAlexander Motin 87567f586a8SAlexander Motin /* 87667f586a8SAlexander Motin * If this is a write or a verify, we're all done. 87767f586a8SAlexander Motin * If this is a read, we can now send the data to the user. 87867f586a8SAlexander Motin */ 87967f586a8SAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 88067f586a8SAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 88167f586a8SAlexander Motin ctl_set_success(&io->scsiio); 88267f586a8SAlexander Motin ctl_complete_beio(beio); 88367f586a8SAlexander Motin } else { 884f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 885f7241cceSAlexander Motin beio->beio_cont == NULL) 886f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 88767f586a8SAlexander Motin #ifdef CTL_TIME_IO 88867f586a8SAlexander Motin getbintime(&io->io_hdr.dma_start_bt); 88967f586a8SAlexander Motin #endif 89067f586a8SAlexander Motin ctl_datamove(io); 89167f586a8SAlexander Motin } 89267f586a8SAlexander Motin } 89367f586a8SAlexander Motin 89467f586a8SAlexander Motin static void 895*ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 896*ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 897*ef8daf3fSAlexander Motin { 898*ef8daf3fSAlexander Motin struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 899*ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 900*ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 901*ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 902*ef8daf3fSAlexander Motin off_t roff, off; 903*ef8daf3fSAlexander Motin int error, status; 904*ef8daf3fSAlexander Motin 905*ef8daf3fSAlexander Motin DPRINTF("entered\n"); 906*ef8daf3fSAlexander Motin 907*ef8daf3fSAlexander Motin off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 908*ef8daf3fSAlexander Motin error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE, 909*ef8daf3fSAlexander Motin (caddr_t)&off, FREAD, curthread); 910*ef8daf3fSAlexander Motin if (error == 0 && off > roff) 911*ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 912*ef8daf3fSAlexander Motin else { 913*ef8daf3fSAlexander Motin error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA, 914*ef8daf3fSAlexander Motin (caddr_t)&off, FREAD, curthread); 915*ef8daf3fSAlexander Motin if (error == 0 && off > roff) 916*ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 917*ef8daf3fSAlexander Motin else { 918*ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 919*ef8daf3fSAlexander Motin off = be_lun->size_bytes; 920*ef8daf3fSAlexander Motin } 921*ef8daf3fSAlexander Motin } 922*ef8daf3fSAlexander Motin 923*ef8daf3fSAlexander Motin off >>= be_lun->blocksize_shift; 924*ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 925*ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 926*ef8daf3fSAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 927*ef8daf3fSAlexander Motin data->descr[0].length); 928*ef8daf3fSAlexander Motin data->descr[0].status = status; 929*ef8daf3fSAlexander Motin 930*ef8daf3fSAlexander Motin ctl_complete_beio(beio); 931*ef8daf3fSAlexander Motin } 932*ef8daf3fSAlexander Motin 933*ef8daf3fSAlexander Motin static void 934130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 935130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 936130f4520SKenneth D. Merry { 937130f4520SKenneth D. Merry struct bio *bio; 938130f4520SKenneth D. Merry union ctl_io *io; 939130f4520SKenneth D. Merry struct ctl_be_block_devdata *dev_data; 940130f4520SKenneth D. Merry 941130f4520SKenneth D. Merry dev_data = &be_lun->backend.dev; 942130f4520SKenneth D. Merry io = beio->io; 943130f4520SKenneth D. Merry 944130f4520SKenneth D. Merry DPRINTF("entered\n"); 945130f4520SKenneth D. Merry 946130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 947130f4520SKenneth D. Merry bio = g_alloc_bio(); 948130f4520SKenneth D. Merry 949130f4520SKenneth D. Merry bio->bio_cmd = BIO_FLUSH; 950130f4520SKenneth D. Merry bio->bio_flags |= BIO_ORDERED; 951130f4520SKenneth D. Merry bio->bio_dev = dev_data->cdev; 952130f4520SKenneth D. Merry bio->bio_offset = 0; 953130f4520SKenneth D. Merry bio->bio_data = 0; 954130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 955130f4520SKenneth D. Merry bio->bio_caller1 = beio; 956130f4520SKenneth D. Merry bio->bio_pblkno = 0; 957130f4520SKenneth D. Merry 958130f4520SKenneth D. Merry /* 959130f4520SKenneth D. Merry * We don't need to acquire the LUN lock here, because we are only 960130f4520SKenneth D. Merry * sending one bio, and so there is no other context to synchronize 961130f4520SKenneth D. Merry * with. 962130f4520SKenneth D. Merry */ 963130f4520SKenneth D. Merry beio->num_bios_sent = 1; 964130f4520SKenneth D. Merry beio->send_complete = 1; 965130f4520SKenneth D. Merry 966130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 96775c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 968130f4520SKenneth D. Merry devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 96975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 970130f4520SKenneth D. Merry 971130f4520SKenneth D. Merry (*dev_data->csw->d_strategy)(bio); 972130f4520SKenneth D. Merry } 973130f4520SKenneth D. Merry 974130f4520SKenneth D. Merry static void 975ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 976ee7f31c0SAlexander Motin struct ctl_be_block_io *beio, 977ee7f31c0SAlexander Motin uint64_t off, uint64_t len, int last) 978ee7f31c0SAlexander Motin { 979ee7f31c0SAlexander Motin struct bio *bio; 980ee7f31c0SAlexander Motin struct ctl_be_block_devdata *dev_data; 9818f5a226aSAlexander Motin uint64_t maxlen; 982ee7f31c0SAlexander Motin 983ee7f31c0SAlexander Motin dev_data = &be_lun->backend.dev; 9848f5a226aSAlexander Motin maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 985ee7f31c0SAlexander Motin while (len > 0) { 986ee7f31c0SAlexander Motin bio = g_alloc_bio(); 987ee7f31c0SAlexander Motin bio->bio_cmd = BIO_DELETE; 988ee7f31c0SAlexander Motin bio->bio_dev = dev_data->cdev; 989ee7f31c0SAlexander Motin bio->bio_offset = off; 9908f5a226aSAlexander Motin bio->bio_length = MIN(len, maxlen); 991ee7f31c0SAlexander Motin bio->bio_data = 0; 992ee7f31c0SAlexander Motin bio->bio_done = ctl_be_block_biodone; 993ee7f31c0SAlexander Motin bio->bio_caller1 = beio; 9948f5a226aSAlexander Motin bio->bio_pblkno = off / be_lun->blocksize; 995ee7f31c0SAlexander Motin 996ee7f31c0SAlexander Motin off += bio->bio_length; 997ee7f31c0SAlexander Motin len -= bio->bio_length; 998ee7f31c0SAlexander Motin 99975c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1000ee7f31c0SAlexander Motin beio->num_bios_sent++; 1001ee7f31c0SAlexander Motin if (last && len == 0) 1002ee7f31c0SAlexander Motin beio->send_complete = 1; 100375c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1004ee7f31c0SAlexander Motin 1005ee7f31c0SAlexander Motin (*dev_data->csw->d_strategy)(bio); 1006ee7f31c0SAlexander Motin } 1007ee7f31c0SAlexander Motin } 1008ee7f31c0SAlexander Motin 1009ee7f31c0SAlexander Motin static void 1010ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1011ee7f31c0SAlexander Motin struct ctl_be_block_io *beio) 1012ee7f31c0SAlexander Motin { 1013ee7f31c0SAlexander Motin union ctl_io *io; 1014ee7f31c0SAlexander Motin struct ctl_be_block_devdata *dev_data; 101566df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1016ee7f31c0SAlexander Motin struct scsi_unmap_desc *buf, *end; 1017ee7f31c0SAlexander Motin uint64_t len; 1018ee7f31c0SAlexander Motin 1019ee7f31c0SAlexander Motin dev_data = &be_lun->backend.dev; 1020ee7f31c0SAlexander Motin io = beio->io; 1021ee7f31c0SAlexander Motin 1022ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1023ee7f31c0SAlexander Motin 1024ee7f31c0SAlexander Motin binuptime(&beio->ds_t0); 102575c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1026ee7f31c0SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 102775c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1028ee7f31c0SAlexander Motin 1029ee7f31c0SAlexander Motin if (beio->io_offset == -1) { 1030ee7f31c0SAlexander Motin beio->io_len = 0; 103166df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 103266df9136SAlexander Motin buf = (struct scsi_unmap_desc *)ptrlen->ptr; 103366df9136SAlexander Motin end = buf + ptrlen->len / sizeof(*buf); 1034ee7f31c0SAlexander Motin for (; buf < end; buf++) { 1035ee7f31c0SAlexander Motin len = (uint64_t)scsi_4btoul(buf->length) * 1036ee7f31c0SAlexander Motin be_lun->blocksize; 1037ee7f31c0SAlexander Motin beio->io_len += len; 1038ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 1039ee7f31c0SAlexander Motin scsi_8btou64(buf->lba) * be_lun->blocksize, len, 10407e0be022SAlexander Motin (end - buf < 2) ? TRUE : FALSE); 1041ee7f31c0SAlexander Motin } 1042ee7f31c0SAlexander Motin } else 1043ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 1044ee7f31c0SAlexander Motin beio->io_offset, beio->io_len, TRUE); 1045ee7f31c0SAlexander Motin } 1046ee7f31c0SAlexander Motin 1047ee7f31c0SAlexander Motin static void 1048130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1049130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 1050130f4520SKenneth D. Merry { 105175c7a1d3SAlexander Motin TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1052130f4520SKenneth D. Merry int i; 1053130f4520SKenneth D. Merry struct bio *bio; 1054130f4520SKenneth D. Merry struct ctl_be_block_devdata *dev_data; 1055130f4520SKenneth D. Merry off_t cur_offset; 1056130f4520SKenneth D. Merry int max_iosize; 1057130f4520SKenneth D. Merry 1058130f4520SKenneth D. Merry DPRINTF("entered\n"); 1059130f4520SKenneth D. Merry 1060130f4520SKenneth D. Merry dev_data = &be_lun->backend.dev; 1061130f4520SKenneth D. Merry 1062130f4520SKenneth D. Merry /* 1063130f4520SKenneth D. Merry * We have to limit our I/O size to the maximum supported by the 1064130f4520SKenneth D. Merry * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1065130f4520SKenneth D. Merry * set it properly, use DFLTPHYS. 1066130f4520SKenneth D. Merry */ 1067130f4520SKenneth D. Merry max_iosize = dev_data->cdev->si_iosize_max; 1068130f4520SKenneth D. Merry if (max_iosize < PAGE_SIZE) 1069130f4520SKenneth D. Merry max_iosize = DFLTPHYS; 1070130f4520SKenneth D. Merry 1071130f4520SKenneth D. Merry cur_offset = beio->io_offset; 1072130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 1073130f4520SKenneth D. Merry size_t cur_size; 1074130f4520SKenneth D. Merry uint8_t *cur_ptr; 1075130f4520SKenneth D. Merry 1076130f4520SKenneth D. Merry cur_size = beio->sg_segs[i].len; 1077130f4520SKenneth D. Merry cur_ptr = beio->sg_segs[i].addr; 1078130f4520SKenneth D. Merry 1079130f4520SKenneth D. Merry while (cur_size > 0) { 1080130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 1081130f4520SKenneth D. Merry bio = g_alloc_bio(); 1082130f4520SKenneth D. Merry 1083130f4520SKenneth D. Merry KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1084130f4520SKenneth D. Merry 1085130f4520SKenneth D. Merry bio->bio_cmd = beio->bio_cmd; 1086130f4520SKenneth D. Merry bio->bio_dev = dev_data->cdev; 1087130f4520SKenneth D. Merry bio->bio_caller1 = beio; 1088130f4520SKenneth D. Merry bio->bio_length = min(cur_size, max_iosize); 1089130f4520SKenneth D. Merry bio->bio_offset = cur_offset; 1090130f4520SKenneth D. Merry bio->bio_data = cur_ptr; 1091130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 1092130f4520SKenneth D. Merry bio->bio_pblkno = cur_offset / be_lun->blocksize; 1093130f4520SKenneth D. Merry 1094130f4520SKenneth D. Merry cur_offset += bio->bio_length; 1095130f4520SKenneth D. Merry cur_ptr += bio->bio_length; 1096130f4520SKenneth D. Merry cur_size -= bio->bio_length; 1097130f4520SKenneth D. Merry 109875c7a1d3SAlexander Motin TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1099130f4520SKenneth D. Merry beio->num_bios_sent++; 1100130f4520SKenneth D. Merry } 1101130f4520SKenneth D. Merry } 110275c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 110375c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 110475c7a1d3SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 110575c7a1d3SAlexander Motin beio->send_complete = 1; 110675c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 110775c7a1d3SAlexander Motin 110875c7a1d3SAlexander Motin /* 110975c7a1d3SAlexander Motin * Fire off all allocated requests! 111075c7a1d3SAlexander Motin */ 111175c7a1d3SAlexander Motin while ((bio = TAILQ_FIRST(&queue)) != NULL) { 111275c7a1d3SAlexander Motin TAILQ_REMOVE(&queue, bio, bio_queue); 111375c7a1d3SAlexander Motin (*dev_data->csw->d_strategy)(bio); 111475c7a1d3SAlexander Motin } 1115130f4520SKenneth D. Merry } 1116130f4520SKenneth D. Merry 1117c3e7ba3eSAlexander Motin static uint64_t 1118c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1119c3e7ba3eSAlexander Motin { 1120c3e7ba3eSAlexander Motin struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1121c3e7ba3eSAlexander Motin struct diocgattr_arg arg; 1122c3e7ba3eSAlexander Motin int error; 1123c3e7ba3eSAlexander Motin 1124c3e7ba3eSAlexander Motin if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1125c3e7ba3eSAlexander Motin return (UINT64_MAX); 1126c3e7ba3eSAlexander Motin strlcpy(arg.name, attrname, sizeof(arg.name)); 1127c3e7ba3eSAlexander Motin arg.len = sizeof(arg.value.off); 1128c3e7ba3eSAlexander Motin error = dev_data->csw->d_ioctl(dev_data->cdev, 1129c3e7ba3eSAlexander Motin DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1130c3e7ba3eSAlexander Motin if (error != 0) 1131c3e7ba3eSAlexander Motin return (UINT64_MAX); 1132c3e7ba3eSAlexander Motin return (arg.value.off); 1133c3e7ba3eSAlexander Motin } 1134c3e7ba3eSAlexander Motin 1135130f4520SKenneth D. Merry static void 1136ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1137ee7f31c0SAlexander Motin { 1138ee7f31c0SAlexander Motin union ctl_io *io; 1139ee7f31c0SAlexander Motin 1140ee7f31c0SAlexander Motin io = beio->io; 1141ee7f31c0SAlexander Motin ctl_free_beio(beio); 1142ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1143ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1144ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1145ee7f31c0SAlexander Motin ctl_config_write_done(io); 1146ee7f31c0SAlexander Motin return; 1147ee7f31c0SAlexander Motin } 1148ee7f31c0SAlexander Motin 1149ee7f31c0SAlexander Motin ctl_be_block_config_write(io); 1150ee7f31c0SAlexander Motin } 1151ee7f31c0SAlexander Motin 1152ee7f31c0SAlexander Motin static void 1153ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1154ee7f31c0SAlexander Motin union ctl_io *io) 1155ee7f31c0SAlexander Motin { 1156ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 1157ee7f31c0SAlexander Motin struct ctl_be_block_softc *softc; 115866df9136SAlexander Motin struct ctl_lba_len_flags *lbalen; 1159ee7f31c0SAlexander Motin uint64_t len_left, lba; 1160ee7f31c0SAlexander Motin int i, seglen; 1161ee7f31c0SAlexander Motin uint8_t *buf, *end; 1162ee7f31c0SAlexander Motin 1163ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1164ee7f31c0SAlexander Motin 1165e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1166ee7f31c0SAlexander Motin softc = be_lun->softc; 116711b569f7SAlexander Motin lbalen = ARGS(beio->io); 1168ee7f31c0SAlexander Motin 116964c5167cSAlexander Motin if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 11703406a2a0SAlexander Motin (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1171ee7f31c0SAlexander Motin ctl_free_beio(beio); 1172ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1173ee7f31c0SAlexander Motin /*sks_valid*/ 1, 1174ee7f31c0SAlexander Motin /*command*/ 1, 1175ee7f31c0SAlexander Motin /*field*/ 1, 1176ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1177ee7f31c0SAlexander Motin /*bit*/ 0); 1178ee7f31c0SAlexander Motin ctl_config_write_done(io); 1179ee7f31c0SAlexander Motin return; 1180ee7f31c0SAlexander Motin } 1181ee7f31c0SAlexander Motin 1182ee7f31c0SAlexander Motin switch (io->scsiio.tag_type) { 1183ee7f31c0SAlexander Motin case CTL_TAG_ORDERED: 1184ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1185ee7f31c0SAlexander Motin break; 1186ee7f31c0SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 1187ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1188ee7f31c0SAlexander Motin break; 1189ee7f31c0SAlexander Motin case CTL_TAG_UNTAGGED: 1190ee7f31c0SAlexander Motin case CTL_TAG_SIMPLE: 1191ee7f31c0SAlexander Motin case CTL_TAG_ACA: 1192ee7f31c0SAlexander Motin default: 1193ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1194ee7f31c0SAlexander Motin break; 1195ee7f31c0SAlexander Motin } 1196ee7f31c0SAlexander Motin 11973406a2a0SAlexander Motin if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 119866df9136SAlexander Motin beio->io_offset = lbalen->lba * be_lun->blocksize; 119966df9136SAlexander Motin beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1200ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1201ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1202ee7f31c0SAlexander Motin 1203ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1204ee7f31c0SAlexander Motin return; 1205ee7f31c0SAlexander Motin } 1206ee7f31c0SAlexander Motin 1207ee7f31c0SAlexander Motin beio->bio_cmd = BIO_WRITE; 1208ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_WRITE; 1209ee7f31c0SAlexander Motin 1210ee7f31c0SAlexander Motin DPRINTF("WRITE SAME at LBA %jx len %u\n", 121166df9136SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len); 1212ee7f31c0SAlexander Motin 121366df9136SAlexander Motin len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1214ee7f31c0SAlexander Motin for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1215ee7f31c0SAlexander Motin 1216ee7f31c0SAlexander Motin /* 1217ee7f31c0SAlexander Motin * Setup the S/G entry for this chunk. 1218ee7f31c0SAlexander Motin */ 121908a7cce5SAlexander Motin seglen = MIN(CTLBLK_MAX_SEG, len_left); 1220ee7f31c0SAlexander Motin seglen -= seglen % be_lun->blocksize; 1221ee7f31c0SAlexander Motin beio->sg_segs[i].len = seglen; 1222ee7f31c0SAlexander Motin beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1223ee7f31c0SAlexander Motin 1224ee7f31c0SAlexander Motin DPRINTF("segment %d addr %p len %zd\n", i, 1225ee7f31c0SAlexander Motin beio->sg_segs[i].addr, beio->sg_segs[i].len); 1226ee7f31c0SAlexander Motin 1227ee7f31c0SAlexander Motin beio->num_segs++; 1228ee7f31c0SAlexander Motin len_left -= seglen; 1229ee7f31c0SAlexander Motin 1230ee7f31c0SAlexander Motin buf = beio->sg_segs[i].addr; 1231ee7f31c0SAlexander Motin end = buf + seglen; 1232ee7f31c0SAlexander Motin for (; buf < end; buf += be_lun->blocksize) { 1233ee7f31c0SAlexander Motin memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 123466df9136SAlexander Motin if (lbalen->flags & SWS_LBDATA) 123566df9136SAlexander Motin scsi_ulto4b(lbalen->lba + lba, buf); 1236ee7f31c0SAlexander Motin lba++; 1237ee7f31c0SAlexander Motin } 1238ee7f31c0SAlexander Motin } 1239ee7f31c0SAlexander Motin 124066df9136SAlexander Motin beio->io_offset = lbalen->lba * be_lun->blocksize; 1241ee7f31c0SAlexander Motin beio->io_len = lba * be_lun->blocksize; 1242ee7f31c0SAlexander Motin 1243ee7f31c0SAlexander Motin /* We can not do all in one run. Correct and schedule rerun. */ 1244ee7f31c0SAlexander Motin if (len_left > 0) { 124566df9136SAlexander Motin lbalen->lba += lba; 124666df9136SAlexander Motin lbalen->len -= lba; 1247ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done_ws; 1248ee7f31c0SAlexander Motin } 1249ee7f31c0SAlexander Motin 1250ee7f31c0SAlexander Motin be_lun->dispatch(be_lun, beio); 1251ee7f31c0SAlexander Motin } 1252ee7f31c0SAlexander Motin 1253ee7f31c0SAlexander Motin static void 1254ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1255ee7f31c0SAlexander Motin union ctl_io *io) 1256ee7f31c0SAlexander Motin { 1257ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 1258ee7f31c0SAlexander Motin struct ctl_be_block_softc *softc; 125966df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1260ee7f31c0SAlexander Motin 1261ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1262ee7f31c0SAlexander Motin 1263e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1264ee7f31c0SAlexander Motin softc = be_lun->softc; 126566df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1266ee7f31c0SAlexander Motin 12673406a2a0SAlexander Motin if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1268ee7f31c0SAlexander Motin ctl_free_beio(beio); 1269ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1270ee7f31c0SAlexander Motin /*sks_valid*/ 0, 1271ee7f31c0SAlexander Motin /*command*/ 1, 1272ee7f31c0SAlexander Motin /*field*/ 0, 1273ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1274ee7f31c0SAlexander Motin /*bit*/ 0); 1275ee7f31c0SAlexander Motin ctl_config_write_done(io); 1276ee7f31c0SAlexander Motin return; 1277ee7f31c0SAlexander Motin } 1278ee7f31c0SAlexander Motin 1279ee7f31c0SAlexander Motin switch (io->scsiio.tag_type) { 1280ee7f31c0SAlexander Motin case CTL_TAG_ORDERED: 1281ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1282ee7f31c0SAlexander Motin break; 1283ee7f31c0SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 1284ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1285ee7f31c0SAlexander Motin break; 1286ee7f31c0SAlexander Motin case CTL_TAG_UNTAGGED: 1287ee7f31c0SAlexander Motin case CTL_TAG_SIMPLE: 1288ee7f31c0SAlexander Motin case CTL_TAG_ACA: 1289ee7f31c0SAlexander Motin default: 1290ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1291ee7f31c0SAlexander Motin break; 1292ee7f31c0SAlexander Motin } 1293ee7f31c0SAlexander Motin 1294ee7f31c0SAlexander Motin beio->io_len = 0; 1295ee7f31c0SAlexander Motin beio->io_offset = -1; 1296ee7f31c0SAlexander Motin 1297ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1298ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1299ee7f31c0SAlexander Motin 130066df9136SAlexander Motin DPRINTF("UNMAP\n"); 1301ee7f31c0SAlexander Motin 1302ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1303ee7f31c0SAlexander Motin } 1304ee7f31c0SAlexander Motin 1305ee7f31c0SAlexander Motin static void 1306*ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio) 1307*ef8daf3fSAlexander Motin { 1308*ef8daf3fSAlexander Motin union ctl_io *io; 1309*ef8daf3fSAlexander Motin 1310*ef8daf3fSAlexander Motin io = beio->io; 1311*ef8daf3fSAlexander Motin ctl_free_beio(beio); 1312*ef8daf3fSAlexander Motin ctl_config_read_done(io); 1313*ef8daf3fSAlexander Motin } 1314*ef8daf3fSAlexander Motin 1315*ef8daf3fSAlexander Motin static void 1316*ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1317*ef8daf3fSAlexander Motin union ctl_io *io) 1318*ef8daf3fSAlexander Motin { 1319*ef8daf3fSAlexander Motin struct ctl_be_block_io *beio; 1320*ef8daf3fSAlexander Motin struct ctl_be_block_softc *softc; 1321*ef8daf3fSAlexander Motin 1322*ef8daf3fSAlexander Motin DPRINTF("entered\n"); 1323*ef8daf3fSAlexander Motin 1324*ef8daf3fSAlexander Motin softc = be_lun->softc; 1325*ef8daf3fSAlexander Motin beio = ctl_alloc_beio(softc); 1326*ef8daf3fSAlexander Motin beio->io = io; 1327*ef8daf3fSAlexander Motin beio->lun = be_lun; 1328*ef8daf3fSAlexander Motin beio->beio_cont = ctl_be_block_cr_done; 1329*ef8daf3fSAlexander Motin PRIV(io)->ptr = (void *)beio; 1330*ef8daf3fSAlexander Motin 1331*ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 1332*ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1333*ef8daf3fSAlexander Motin beio->bio_cmd = -1; 1334*ef8daf3fSAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 1335*ef8daf3fSAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1336*ef8daf3fSAlexander Motin beio->io_len = 0; 1337*ef8daf3fSAlexander Motin if (be_lun->get_lba_status) 1338*ef8daf3fSAlexander Motin be_lun->get_lba_status(be_lun, beio); 1339*ef8daf3fSAlexander Motin else 1340*ef8daf3fSAlexander Motin ctl_be_block_cr_done(beio); 1341*ef8daf3fSAlexander Motin break; 1342*ef8daf3fSAlexander Motin default: 1343*ef8daf3fSAlexander Motin panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1344*ef8daf3fSAlexander Motin break; 1345*ef8daf3fSAlexander Motin } 1346*ef8daf3fSAlexander Motin } 1347*ef8daf3fSAlexander Motin 1348*ef8daf3fSAlexander Motin static void 1349ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1350ee7f31c0SAlexander Motin { 1351ee7f31c0SAlexander Motin union ctl_io *io; 1352ee7f31c0SAlexander Motin 1353ee7f31c0SAlexander Motin io = beio->io; 1354ee7f31c0SAlexander Motin ctl_free_beio(beio); 1355ee7f31c0SAlexander Motin ctl_config_write_done(io); 1356ee7f31c0SAlexander Motin } 1357ee7f31c0SAlexander Motin 1358ee7f31c0SAlexander Motin static void 1359130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1360130f4520SKenneth D. Merry union ctl_io *io) 1361130f4520SKenneth D. Merry { 1362130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1363130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1364130f4520SKenneth D. Merry 1365130f4520SKenneth D. Merry DPRINTF("entered\n"); 1366130f4520SKenneth D. Merry 1367130f4520SKenneth D. Merry softc = be_lun->softc; 1368130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1369130f4520SKenneth D. Merry beio->io = io; 1370130f4520SKenneth D. Merry beio->lun = be_lun; 1371ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done; 1372e86a4142SAlexander Motin PRIV(io)->ptr = (void *)beio; 1373130f4520SKenneth D. Merry 1374130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 1375130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 1376130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 1377d2a0972dSEdward Tomasz Napierala beio->bio_cmd = BIO_FLUSH; 1378130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_NO_DATA; 1379130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1380130f4520SKenneth D. Merry beio->io_len = 0; 1381130f4520SKenneth D. Merry be_lun->lun_flush(be_lun, beio); 1382130f4520SKenneth D. Merry break; 1383ee7f31c0SAlexander Motin case WRITE_SAME_10: 1384ee7f31c0SAlexander Motin case WRITE_SAME_16: 1385ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(be_lun, io); 1386ee7f31c0SAlexander Motin break; 1387ee7f31c0SAlexander Motin case UNMAP: 1388ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(be_lun, io); 1389ee7f31c0SAlexander Motin break; 1390130f4520SKenneth D. Merry default: 1391130f4520SKenneth D. Merry panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1392130f4520SKenneth D. Merry break; 1393130f4520SKenneth D. Merry } 1394130f4520SKenneth D. Merry } 1395130f4520SKenneth D. Merry 1396d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1397d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1398d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1399d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1400130f4520SKenneth D. Merry 1401130f4520SKenneth D. Merry static void 140208a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio) 140308a7cce5SAlexander Motin { 140408a7cce5SAlexander Motin struct ctl_be_block_lun *be_lun; 140508a7cce5SAlexander Motin union ctl_io *io; 140608a7cce5SAlexander Motin 140708a7cce5SAlexander Motin io = beio->io; 140808a7cce5SAlexander Motin be_lun = beio->lun; 140908a7cce5SAlexander Motin ctl_free_beio(beio); 1410ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1411ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1412ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 141311b569f7SAlexander Motin ctl_data_submit_done(io); 141408a7cce5SAlexander Motin return; 141508a7cce5SAlexander Motin } 141608a7cce5SAlexander Motin 141708a7cce5SAlexander Motin io->io_hdr.status &= ~CTL_STATUS_MASK; 141808a7cce5SAlexander Motin io->io_hdr.status |= CTL_STATUS_NONE; 141908a7cce5SAlexander Motin 142075c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 142108a7cce5SAlexander Motin /* 142208a7cce5SAlexander Motin * XXX KDM make sure that links is okay to use at this point. 142308a7cce5SAlexander Motin * Otherwise, we either need to add another field to ctl_io_hdr, 142408a7cce5SAlexander Motin * or deal with resource allocation here. 142508a7cce5SAlexander Motin */ 142608a7cce5SAlexander Motin STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 142775c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 142808a7cce5SAlexander Motin 142908a7cce5SAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 143008a7cce5SAlexander Motin } 143108a7cce5SAlexander Motin 143208a7cce5SAlexander Motin static void 1433130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1434130f4520SKenneth D. Merry union ctl_io *io) 1435130f4520SKenneth D. Merry { 1436130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1437130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 143811b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 1439e86a4142SAlexander Motin struct ctl_ptr_len_flags *bptrlen; 1440e86a4142SAlexander Motin uint64_t len_left, lbas; 1441130f4520SKenneth D. Merry int i; 1442130f4520SKenneth D. Merry 1443130f4520SKenneth D. Merry softc = be_lun->softc; 1444130f4520SKenneth D. Merry 1445130f4520SKenneth D. Merry DPRINTF("entered\n"); 1446130f4520SKenneth D. Merry 144711b569f7SAlexander Motin lbalen = ARGS(io); 144811b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1449130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 145011b569f7SAlexander Motin } else { 145111b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1452130f4520SKenneth D. Merry } 1453130f4520SKenneth D. Merry 1454130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1455130f4520SKenneth D. Merry beio->io = io; 1456130f4520SKenneth D. Merry beio->lun = be_lun; 1457e86a4142SAlexander Motin bptrlen = PRIV(io); 1458e86a4142SAlexander Motin bptrlen->ptr = (void *)beio; 1459130f4520SKenneth D. Merry 1460130f4520SKenneth D. Merry switch (io->scsiio.tag_type) { 1461130f4520SKenneth D. Merry case CTL_TAG_ORDERED: 1462130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1463130f4520SKenneth D. Merry break; 1464130f4520SKenneth D. Merry case CTL_TAG_HEAD_OF_QUEUE: 1465130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1466130f4520SKenneth D. Merry break; 1467130f4520SKenneth D. Merry case CTL_TAG_UNTAGGED: 1468130f4520SKenneth D. Merry case CTL_TAG_SIMPLE: 1469130f4520SKenneth D. Merry case CTL_TAG_ACA: 1470130f4520SKenneth D. Merry default: 1471130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1472130f4520SKenneth D. Merry break; 1473130f4520SKenneth D. Merry } 1474130f4520SKenneth D. Merry 147511b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1476130f4520SKenneth D. Merry beio->bio_cmd = BIO_WRITE; 1477130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_WRITE; 147811b569f7SAlexander Motin } else { 147911b569f7SAlexander Motin beio->bio_cmd = BIO_READ; 148011b569f7SAlexander Motin beio->ds_trans_type = DEVSTAT_READ; 1481130f4520SKenneth D. Merry } 1482130f4520SKenneth D. Merry 148308a7cce5SAlexander Motin DPRINTF("%s at LBA %jx len %u @%ju\n", 1484130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1485e86a4142SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 148611b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 148711b569f7SAlexander Motin lbas = CTLBLK_HALF_IO_SIZE; 148811b569f7SAlexander Motin else 148911b569f7SAlexander Motin lbas = CTLBLK_MAX_IO_SIZE; 149011b569f7SAlexander Motin lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1491e86a4142SAlexander Motin beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1492e86a4142SAlexander Motin beio->io_len = lbas * be_lun->blocksize; 1493e86a4142SAlexander Motin bptrlen->len += lbas; 1494130f4520SKenneth D. Merry 149508a7cce5SAlexander Motin for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 149608a7cce5SAlexander Motin KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 149708a7cce5SAlexander Motin i, CTLBLK_MAX_SEGS)); 1498130f4520SKenneth D. Merry 1499130f4520SKenneth D. Merry /* 1500130f4520SKenneth D. Merry * Setup the S/G entry for this chunk. 1501130f4520SKenneth D. Merry */ 150208a7cce5SAlexander Motin beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1503130f4520SKenneth D. Merry beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1504130f4520SKenneth D. Merry 1505130f4520SKenneth D. Merry DPRINTF("segment %d addr %p len %zd\n", i, 1506130f4520SKenneth D. Merry beio->sg_segs[i].addr, beio->sg_segs[i].len); 1507130f4520SKenneth D. Merry 150811b569f7SAlexander Motin /* Set up second segment for compare operation. */ 150911b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) { 151011b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 151111b569f7SAlexander Motin beio->sg_segs[i].len; 151211b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 151311b569f7SAlexander Motin uma_zalloc(be_lun->lun_zone, M_WAITOK); 151411b569f7SAlexander Motin } 151511b569f7SAlexander Motin 1516130f4520SKenneth D. Merry beio->num_segs++; 1517130f4520SKenneth D. Merry len_left -= beio->sg_segs[i].len; 1518130f4520SKenneth D. Merry } 1519e86a4142SAlexander Motin if (bptrlen->len < lbalen->len) 152008a7cce5SAlexander Motin beio->beio_cont = ctl_be_block_next; 152108a7cce5SAlexander Motin io->scsiio.be_move_done = ctl_be_block_move_done; 152211b569f7SAlexander Motin /* For compare we have separate S/G lists for read and datamove. */ 152311b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 152411b569f7SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 152511b569f7SAlexander Motin else 152608a7cce5SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 152708a7cce5SAlexander Motin io->scsiio.kern_data_len = beio->io_len; 152808a7cce5SAlexander Motin io->scsiio.kern_data_resid = 0; 152908a7cce5SAlexander Motin io->scsiio.kern_sg_entries = beio->num_segs; 153008a7cce5SAlexander Motin io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1531130f4520SKenneth D. Merry 1532130f4520SKenneth D. Merry /* 1533130f4520SKenneth D. Merry * For the read case, we need to read the data into our buffers and 1534130f4520SKenneth D. Merry * then we can send it back to the user. For the write case, we 1535130f4520SKenneth D. Merry * need to get the data from the user first. 1536130f4520SKenneth D. Merry */ 1537130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 1538130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1539130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1540130f4520SKenneth D. Merry } else { 1541130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1542130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 1543130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 1544130f4520SKenneth D. Merry #endif 1545130f4520SKenneth D. Merry ctl_datamove(io); 1546130f4520SKenneth D. Merry } 1547130f4520SKenneth D. Merry } 1548130f4520SKenneth D. Merry 1549130f4520SKenneth D. Merry static void 1550130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending) 1551130f4520SKenneth D. Merry { 1552130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1553130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1554130f4520SKenneth D. Merry union ctl_io *io; 1555130f4520SKenneth D. Merry 1556130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)context; 1557130f4520SKenneth D. Merry softc = be_lun->softc; 1558130f4520SKenneth D. Merry 1559130f4520SKenneth D. Merry DPRINTF("entered\n"); 1560130f4520SKenneth D. Merry 156175c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1562130f4520SKenneth D. Merry for (;;) { 1563130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1564130f4520SKenneth D. Merry if (io != NULL) { 1565130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1566130f4520SKenneth D. Merry 1567130f4520SKenneth D. Merry DPRINTF("datamove queue\n"); 1568130f4520SKenneth D. Merry 1569130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1570130f4520SKenneth D. Merry ctl_io_hdr, links); 1571130f4520SKenneth D. Merry 157275c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1573130f4520SKenneth D. Merry 1574e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1575130f4520SKenneth D. Merry 1576130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1577130f4520SKenneth D. Merry 157875c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1579130f4520SKenneth D. Merry continue; 1580130f4520SKenneth D. Merry } 1581130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1582130f4520SKenneth D. Merry if (io != NULL) { 1583130f4520SKenneth D. Merry DPRINTF("config write queue\n"); 1584130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1585130f4520SKenneth D. Merry ctl_io_hdr, links); 158675c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1587130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(be_lun, io); 1588*ef8daf3fSAlexander Motin mtx_lock(&be_lun->queue_lock); 1589*ef8daf3fSAlexander Motin continue; 1590*ef8daf3fSAlexander Motin } 1591*ef8daf3fSAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1592*ef8daf3fSAlexander Motin if (io != NULL) { 1593*ef8daf3fSAlexander Motin DPRINTF("config read queue\n"); 1594*ef8daf3fSAlexander Motin STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1595*ef8daf3fSAlexander Motin ctl_io_hdr, links); 1596*ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 1597*ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(be_lun, io); 159875c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1599130f4520SKenneth D. Merry continue; 1600130f4520SKenneth D. Merry } 1601130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1602130f4520SKenneth D. Merry if (io != NULL) { 1603130f4520SKenneth D. Merry DPRINTF("input queue\n"); 1604130f4520SKenneth D. Merry 1605130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1606130f4520SKenneth D. Merry ctl_io_hdr, links); 160775c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1608130f4520SKenneth D. Merry 1609130f4520SKenneth D. Merry /* 1610130f4520SKenneth D. Merry * We must drop the lock, since this routine and 1611130f4520SKenneth D. Merry * its children may sleep. 1612130f4520SKenneth D. Merry */ 1613130f4520SKenneth D. Merry ctl_be_block_dispatch(be_lun, io); 1614130f4520SKenneth D. Merry 161575c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1616130f4520SKenneth D. Merry continue; 1617130f4520SKenneth D. Merry } 1618130f4520SKenneth D. Merry 1619130f4520SKenneth D. Merry /* 1620130f4520SKenneth D. Merry * If we get here, there is no work left in the queues, so 1621130f4520SKenneth D. Merry * just break out and let the task queue go to sleep. 1622130f4520SKenneth D. Merry */ 1623130f4520SKenneth D. Merry break; 1624130f4520SKenneth D. Merry } 162575c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1626130f4520SKenneth D. Merry } 1627130f4520SKenneth D. Merry 1628130f4520SKenneth D. Merry /* 1629130f4520SKenneth D. Merry * Entry point from CTL to the backend for I/O. We queue everything to a 1630130f4520SKenneth D. Merry * work thread, so this just puts the I/O on a queue and wakes up the 1631130f4520SKenneth D. Merry * thread. 1632130f4520SKenneth D. Merry */ 1633130f4520SKenneth D. Merry static int 1634130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io) 1635130f4520SKenneth D. Merry { 1636130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1637130f4520SKenneth D. Merry struct ctl_be_lun *ctl_be_lun; 1638130f4520SKenneth D. Merry 1639130f4520SKenneth D. Merry DPRINTF("entered\n"); 1640130f4520SKenneth D. Merry 1641130f4520SKenneth D. Merry ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1642130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 1643130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1644130f4520SKenneth D. Merry 1645130f4520SKenneth D. Merry /* 1646130f4520SKenneth D. Merry * Make sure we only get SCSI I/O. 1647130f4520SKenneth D. Merry */ 1648130f4520SKenneth D. Merry KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1649130f4520SKenneth D. Merry "%#x) encountered", io->io_hdr.io_type)); 1650130f4520SKenneth D. Merry 1651e86a4142SAlexander Motin PRIV(io)->len = 0; 1652e86a4142SAlexander Motin 165375c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1654130f4520SKenneth D. Merry /* 1655130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 1656130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 1657130f4520SKenneth D. Merry * or deal with resource allocation here. 1658130f4520SKenneth D. Merry */ 1659130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 166075c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1661130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1662130f4520SKenneth D. Merry 16639c71cd5aSAlexander Motin return (CTL_RETVAL_COMPLETE); 1664130f4520SKenneth D. Merry } 1665130f4520SKenneth D. Merry 1666130f4520SKenneth D. Merry static int 1667130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1668130f4520SKenneth D. Merry int flag, struct thread *td) 1669130f4520SKenneth D. Merry { 1670130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1671130f4520SKenneth D. Merry int error; 1672130f4520SKenneth D. Merry 1673130f4520SKenneth D. Merry softc = &backend_block_softc; 1674130f4520SKenneth D. Merry 1675130f4520SKenneth D. Merry error = 0; 1676130f4520SKenneth D. Merry 1677130f4520SKenneth D. Merry switch (cmd) { 1678130f4520SKenneth D. Merry case CTL_LUN_REQ: { 1679130f4520SKenneth D. Merry struct ctl_lun_req *lun_req; 1680130f4520SKenneth D. Merry 1681130f4520SKenneth D. Merry lun_req = (struct ctl_lun_req *)addr; 1682130f4520SKenneth D. Merry 1683130f4520SKenneth D. Merry switch (lun_req->reqtype) { 1684130f4520SKenneth D. Merry case CTL_LUNREQ_CREATE: 1685130f4520SKenneth D. Merry error = ctl_be_block_create(softc, lun_req); 1686130f4520SKenneth D. Merry break; 1687130f4520SKenneth D. Merry case CTL_LUNREQ_RM: 1688130f4520SKenneth D. Merry error = ctl_be_block_rm(softc, lun_req); 1689130f4520SKenneth D. Merry break; 169081177295SEdward Tomasz Napierala case CTL_LUNREQ_MODIFY: 169181177295SEdward Tomasz Napierala error = ctl_be_block_modify(softc, lun_req); 169281177295SEdward Tomasz Napierala break; 1693130f4520SKenneth D. Merry default: 1694130f4520SKenneth D. Merry lun_req->status = CTL_LUN_ERROR; 1695130f4520SKenneth D. Merry snprintf(lun_req->error_str, sizeof(lun_req->error_str), 169619720f41SAlexander Motin "invalid LUN request type %d", 1697130f4520SKenneth D. Merry lun_req->reqtype); 1698130f4520SKenneth D. Merry break; 1699130f4520SKenneth D. Merry } 1700130f4520SKenneth D. Merry break; 1701130f4520SKenneth D. Merry } 1702130f4520SKenneth D. Merry default: 1703130f4520SKenneth D. Merry error = ENOTTY; 1704130f4520SKenneth D. Merry break; 1705130f4520SKenneth D. Merry } 1706130f4520SKenneth D. Merry 1707130f4520SKenneth D. Merry return (error); 1708130f4520SKenneth D. Merry } 1709130f4520SKenneth D. Merry 1710130f4520SKenneth D. Merry static int 1711130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1712130f4520SKenneth D. Merry { 1713130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 1714130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 1715130f4520SKenneth D. Merry struct vattr vattr; 171620a28d6cSAlexander Motin off_t pss; 1717130f4520SKenneth D. Merry int error; 1718130f4520SKenneth D. Merry 1719130f4520SKenneth D. Merry error = 0; 1720130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 172119720f41SAlexander Motin params = &be_lun->params; 1722130f4520SKenneth D. Merry 1723130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_FILE; 1724130f4520SKenneth D. Merry be_lun->dispatch = ctl_be_block_dispatch_file; 1725130f4520SKenneth D. Merry be_lun->lun_flush = ctl_be_block_flush_file; 1726*ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_file; 1727130f4520SKenneth D. Merry 1728130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1729130f4520SKenneth D. Merry if (error != 0) { 1730130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1731130f4520SKenneth D. Merry "error calling VOP_GETATTR() for file %s", 1732130f4520SKenneth D. Merry be_lun->dev_path); 1733130f4520SKenneth D. Merry return (error); 1734130f4520SKenneth D. Merry } 1735130f4520SKenneth D. Merry 1736130f4520SKenneth D. Merry /* 1737130f4520SKenneth D. Merry * Verify that we have the ability to upgrade to exclusive 1738130f4520SKenneth D. Merry * access on this file so we can trap errors at open instead 1739130f4520SKenneth D. Merry * of reporting them during first access. 1740130f4520SKenneth D. Merry */ 1741130f4520SKenneth D. Merry if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1742130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1743130f4520SKenneth D. Merry if (be_lun->vn->v_iflag & VI_DOOMED) { 1744130f4520SKenneth D. Merry error = EBADF; 1745130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1746130f4520SKenneth D. Merry "error locking file %s", be_lun->dev_path); 1747130f4520SKenneth D. Merry return (error); 1748130f4520SKenneth D. Merry } 1749130f4520SKenneth D. Merry } 1750130f4520SKenneth D. Merry 1751130f4520SKenneth D. Merry 1752130f4520SKenneth D. Merry file_data->cred = crhold(curthread->td_ucred); 175381177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) 175481177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 175581177295SEdward Tomasz Napierala else 1756130f4520SKenneth D. Merry be_lun->size_bytes = vattr.va_size; 1757130f4520SKenneth D. Merry /* 1758130f4520SKenneth D. Merry * We set the multi thread flag for file operations because all 1759130f4520SKenneth D. Merry * filesystems (in theory) are capable of allowing multiple readers 1760130f4520SKenneth D. Merry * of a file at once. So we want to get the maximum possible 1761130f4520SKenneth D. Merry * concurrency. 1762130f4520SKenneth D. Merry */ 1763130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1764130f4520SKenneth D. Merry 1765130f4520SKenneth D. Merry /* 176620a28d6cSAlexander Motin * For files we can use any logical block size. Prefer 512 bytes 176720a28d6cSAlexander Motin * for compatibility reasons. If file's vattr.va_blocksize 176820a28d6cSAlexander Motin * (preferred I/O block size) is bigger and multiple to chosen 176920a28d6cSAlexander Motin * logical block size -- report it as physical block size. 1770130f4520SKenneth D. Merry */ 1771130f4520SKenneth D. Merry if (params->blocksize_bytes != 0) 1772130f4520SKenneth D. Merry be_lun->blocksize = params->blocksize_bytes; 1773130f4520SKenneth D. Merry else 1774130f4520SKenneth D. Merry be_lun->blocksize = 512; 177520a28d6cSAlexander Motin pss = vattr.va_blocksize / be_lun->blocksize; 177620a28d6cSAlexander Motin if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) && 177720a28d6cSAlexander Motin ((pss & (pss - 1)) == 0)) { 177820a28d6cSAlexander Motin be_lun->pblockexp = fls(pss) - 1; 177920a28d6cSAlexander Motin be_lun->pblockoff = 0; 178020a28d6cSAlexander Motin } 1781130f4520SKenneth D. Merry 1782130f4520SKenneth D. Merry /* 1783130f4520SKenneth D. Merry * Sanity check. The media size has to be at least one 1784130f4520SKenneth D. Merry * sector long. 1785130f4520SKenneth D. Merry */ 1786130f4520SKenneth D. Merry if (be_lun->size_bytes < be_lun->blocksize) { 1787130f4520SKenneth D. Merry error = EINVAL; 1788130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1789130f4520SKenneth D. Merry "file %s size %ju < block size %u", be_lun->dev_path, 1790130f4520SKenneth D. Merry (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1791130f4520SKenneth D. Merry } 1792130f4520SKenneth D. Merry return (error); 1793130f4520SKenneth D. Merry } 1794130f4520SKenneth D. Merry 1795130f4520SKenneth D. Merry static int 1796130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1797130f4520SKenneth D. Merry { 1798130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 1799130f4520SKenneth D. Merry struct vattr vattr; 1800130f4520SKenneth D. Merry struct cdev *dev; 1801130f4520SKenneth D. Merry struct cdevsw *devsw; 1802130f4520SKenneth D. Merry int error; 1803f6012722SAlexander Motin off_t ps, pss, po, pos; 1804130f4520SKenneth D. Merry 180519720f41SAlexander Motin params = &be_lun->params; 1806130f4520SKenneth D. Merry 1807130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_DEV; 1808130f4520SKenneth D. Merry be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1809130f4520SKenneth D. Merry be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1810130f4520SKenneth D. Merry &be_lun->backend.dev.dev_ref); 1811130f4520SKenneth D. Merry if (be_lun->backend.dev.csw == NULL) 1812130f4520SKenneth D. Merry panic("Unable to retrieve device switch"); 1813*ef8daf3fSAlexander Motin if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) { 181467f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_zvol; 1815*ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_zvol; 1816*ef8daf3fSAlexander Motin } else 181767f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_dev; 181867f586a8SAlexander Motin be_lun->lun_flush = ctl_be_block_flush_dev; 181967f586a8SAlexander Motin be_lun->unmap = ctl_be_block_unmap_dev; 1820c3e7ba3eSAlexander Motin be_lun->getattr = ctl_be_block_getattr_dev; 1821130f4520SKenneth D. Merry 1822130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1823130f4520SKenneth D. Merry if (error) { 1824130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 182519720f41SAlexander Motin "error getting vnode attributes for device %s", 182619720f41SAlexander Motin be_lun->dev_path); 1827130f4520SKenneth D. Merry return (error); 1828130f4520SKenneth D. Merry } 1829130f4520SKenneth D. Merry 1830130f4520SKenneth D. Merry dev = be_lun->vn->v_rdev; 1831130f4520SKenneth D. Merry devsw = dev->si_devsw; 1832130f4520SKenneth D. Merry if (!devsw->d_ioctl) { 1833130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 183419720f41SAlexander Motin "no d_ioctl for device %s!", 1835130f4520SKenneth D. Merry be_lun->dev_path); 1836130f4520SKenneth D. Merry return (ENODEV); 1837130f4520SKenneth D. Merry } 1838130f4520SKenneth D. Merry 1839130f4520SKenneth D. Merry error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1840130f4520SKenneth D. Merry (caddr_t)&be_lun->blocksize, FREAD, 1841130f4520SKenneth D. Merry curthread); 1842130f4520SKenneth D. Merry if (error) { 1843130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 184419720f41SAlexander Motin "error %d returned for DIOCGSECTORSIZE ioctl " 184519720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 1846130f4520SKenneth D. Merry return (error); 1847130f4520SKenneth D. Merry } 1848130f4520SKenneth D. Merry 1849130f4520SKenneth D. Merry /* 1850130f4520SKenneth D. Merry * If the user has asked for a blocksize that is greater than the 1851130f4520SKenneth D. Merry * backing device's blocksize, we can do it only if the blocksize 1852130f4520SKenneth D. Merry * the user is asking for is an even multiple of the underlying 1853130f4520SKenneth D. Merry * device's blocksize. 1854130f4520SKenneth D. Merry */ 1855130f4520SKenneth D. Merry if ((params->blocksize_bytes != 0) 1856130f4520SKenneth D. Merry && (params->blocksize_bytes > be_lun->blocksize)) { 1857130f4520SKenneth D. Merry uint32_t bs_multiple, tmp_blocksize; 1858130f4520SKenneth D. Merry 1859130f4520SKenneth D. Merry bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1860130f4520SKenneth D. Merry 1861130f4520SKenneth D. Merry tmp_blocksize = bs_multiple * be_lun->blocksize; 1862130f4520SKenneth D. Merry 1863130f4520SKenneth D. Merry if (tmp_blocksize == params->blocksize_bytes) { 1864130f4520SKenneth D. Merry be_lun->blocksize = params->blocksize_bytes; 1865130f4520SKenneth D. Merry } else { 1866130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 186719720f41SAlexander Motin "requested blocksize %u is not an even " 1868130f4520SKenneth D. Merry "multiple of backing device blocksize %u", 186919720f41SAlexander Motin params->blocksize_bytes, 1870130f4520SKenneth D. Merry be_lun->blocksize); 1871130f4520SKenneth D. Merry return (EINVAL); 1872130f4520SKenneth D. Merry 1873130f4520SKenneth D. Merry } 1874130f4520SKenneth D. Merry } else if ((params->blocksize_bytes != 0) 1875130f4520SKenneth D. Merry && (params->blocksize_bytes != be_lun->blocksize)) { 1876130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 187719720f41SAlexander Motin "requested blocksize %u < backing device " 187819720f41SAlexander Motin "blocksize %u", params->blocksize_bytes, 1879130f4520SKenneth D. Merry be_lun->blocksize); 1880130f4520SKenneth D. Merry return (EINVAL); 1881130f4520SKenneth D. Merry } 1882130f4520SKenneth D. Merry 1883130f4520SKenneth D. Merry error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1884130f4520SKenneth D. Merry (caddr_t)&be_lun->size_bytes, FREAD, 1885130f4520SKenneth D. Merry curthread); 1886130f4520SKenneth D. Merry if (error) { 1887130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 188819720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE " 188919720f41SAlexander Motin " ioctl on %s!", error, 189081177295SEdward Tomasz Napierala be_lun->dev_path); 1891130f4520SKenneth D. Merry return (error); 1892130f4520SKenneth D. Merry } 1893130f4520SKenneth D. Merry 189481177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 189581177295SEdward Tomasz Napierala if (params->lun_size_bytes > be_lun->size_bytes) { 189681177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 189719720f41SAlexander Motin "requested LUN size %ju > backing device " 189819720f41SAlexander Motin "size %ju", 189981177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 190081177295SEdward Tomasz Napierala (uintmax_t)be_lun->size_bytes); 190181177295SEdward Tomasz Napierala return (EINVAL); 1902130f4520SKenneth D. Merry } 1903130f4520SKenneth D. Merry 190481177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 190581177295SEdward Tomasz Napierala } 190681177295SEdward Tomasz Napierala 1907f6012722SAlexander Motin error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1908f6012722SAlexander Motin (caddr_t)&ps, FREAD, curthread); 1909f6012722SAlexander Motin if (error) 1910f6012722SAlexander Motin ps = po = 0; 1911f6012722SAlexander Motin else { 1912f6012722SAlexander Motin error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1913f6012722SAlexander Motin (caddr_t)&po, FREAD, curthread); 1914f6012722SAlexander Motin if (error) 1915f6012722SAlexander Motin po = 0; 1916f6012722SAlexander Motin } 1917f6012722SAlexander Motin pss = ps / be_lun->blocksize; 1918f6012722SAlexander Motin pos = po / be_lun->blocksize; 1919f6012722SAlexander Motin if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1920f6012722SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1921f6012722SAlexander Motin be_lun->pblockexp = fls(pss) - 1; 1922f6012722SAlexander Motin be_lun->pblockoff = (pss - pos) % pss; 1923f6012722SAlexander Motin } 1924f6012722SAlexander Motin 192581177295SEdward Tomasz Napierala return (0); 192681177295SEdward Tomasz Napierala } 1927130f4520SKenneth D. Merry 1928130f4520SKenneth D. Merry static int 1929130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun) 1930130f4520SKenneth D. Merry { 1931130f4520SKenneth D. Merry DROP_GIANT(); 1932130f4520SKenneth D. Merry if (be_lun->vn) { 1933130f4520SKenneth D. Merry int flags = FREAD | FWRITE; 1934130f4520SKenneth D. Merry 1935130f4520SKenneth D. Merry switch (be_lun->dev_type) { 1936130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 1937130f4520SKenneth D. Merry if (be_lun->backend.dev.csw) { 1938130f4520SKenneth D. Merry dev_relthread(be_lun->backend.dev.cdev, 1939130f4520SKenneth D. Merry be_lun->backend.dev.dev_ref); 1940130f4520SKenneth D. Merry be_lun->backend.dev.csw = NULL; 1941130f4520SKenneth D. Merry be_lun->backend.dev.cdev = NULL; 1942130f4520SKenneth D. Merry } 1943130f4520SKenneth D. Merry break; 1944130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 1945130f4520SKenneth D. Merry break; 1946130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 1947025a2301SEdward Tomasz Napierala break; 1948130f4520SKenneth D. Merry default: 1949130f4520SKenneth D. Merry panic("Unexpected backend type."); 1950130f4520SKenneth D. Merry break; 1951130f4520SKenneth D. Merry } 1952130f4520SKenneth D. Merry 1953130f4520SKenneth D. Merry (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1954130f4520SKenneth D. Merry be_lun->vn = NULL; 1955130f4520SKenneth D. Merry 1956130f4520SKenneth D. Merry switch (be_lun->dev_type) { 1957130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 1958130f4520SKenneth D. Merry break; 1959130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 1960130f4520SKenneth D. Merry if (be_lun->backend.file.cred != NULL) { 1961130f4520SKenneth D. Merry crfree(be_lun->backend.file.cred); 1962130f4520SKenneth D. Merry be_lun->backend.file.cred = NULL; 1963130f4520SKenneth D. Merry } 1964130f4520SKenneth D. Merry break; 1965130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 1966025a2301SEdward Tomasz Napierala break; 1967130f4520SKenneth D. Merry default: 1968130f4520SKenneth D. Merry panic("Unexpected backend type."); 1969130f4520SKenneth D. Merry break; 1970130f4520SKenneth D. Merry } 197119720f41SAlexander Motin be_lun->dev_type = CTL_BE_BLOCK_NONE; 1972130f4520SKenneth D. Merry } 1973130f4520SKenneth D. Merry PICKUP_GIANT(); 1974130f4520SKenneth D. Merry 1975130f4520SKenneth D. Merry return (0); 1976130f4520SKenneth D. Merry } 1977130f4520SKenneth D. Merry 1978130f4520SKenneth D. Merry static int 1979130f4520SKenneth D. Merry ctl_be_block_open(struct ctl_be_block_softc *softc, 1980130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1981130f4520SKenneth D. Merry { 1982130f4520SKenneth D. Merry struct nameidata nd; 1983130f4520SKenneth D. Merry int flags; 1984130f4520SKenneth D. Merry int error; 1985130f4520SKenneth D. Merry 1986130f4520SKenneth D. Merry /* 1987130f4520SKenneth D. Merry * XXX KDM allow a read-only option? 1988130f4520SKenneth D. Merry */ 1989130f4520SKenneth D. Merry flags = FREAD | FWRITE; 1990130f4520SKenneth D. Merry error = 0; 1991130f4520SKenneth D. Merry 1992130f4520SKenneth D. Merry if (rootvnode == NULL) { 1993130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 199419720f41SAlexander Motin "Root filesystem is not mounted"); 1995130f4520SKenneth D. Merry return (1); 1996130f4520SKenneth D. Merry } 1997130f4520SKenneth D. Merry 1998130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_cdir) { 1999130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_cdir = rootvnode; 2000130f4520SKenneth D. Merry VREF(rootvnode); 2001130f4520SKenneth D. Merry } 2002130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_rdir) { 2003130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_rdir = rootvnode; 2004130f4520SKenneth D. Merry VREF(rootvnode); 2005130f4520SKenneth D. Merry } 2006130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_jdir) { 2007130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_jdir = rootvnode; 2008130f4520SKenneth D. Merry VREF(rootvnode); 2009130f4520SKenneth D. Merry } 2010130f4520SKenneth D. Merry 2011130f4520SKenneth D. Merry again: 2012130f4520SKenneth D. Merry NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2013130f4520SKenneth D. Merry error = vn_open(&nd, &flags, 0, NULL); 2014130f4520SKenneth D. Merry if (error) { 2015130f4520SKenneth D. Merry /* 2016130f4520SKenneth D. Merry * This is the only reasonable guess we can make as far as 2017130f4520SKenneth D. Merry * path if the user doesn't give us a fully qualified path. 2018130f4520SKenneth D. Merry * If they want to specify a file, they need to specify the 2019130f4520SKenneth D. Merry * full path. 2020130f4520SKenneth D. Merry */ 2021130f4520SKenneth D. Merry if (be_lun->dev_path[0] != '/') { 2022130f4520SKenneth D. Merry char *dev_path = "/dev/"; 2023130f4520SKenneth D. Merry char *dev_name; 2024130f4520SKenneth D. Merry 2025130f4520SKenneth D. Merry /* Try adding device path at beginning of name */ 2026130f4520SKenneth D. Merry dev_name = malloc(strlen(be_lun->dev_path) 2027130f4520SKenneth D. Merry + strlen(dev_path) + 1, 2028130f4520SKenneth D. Merry M_CTLBLK, M_WAITOK); 2029130f4520SKenneth D. Merry if (dev_name) { 2030130f4520SKenneth D. Merry sprintf(dev_name, "%s%s", dev_path, 2031130f4520SKenneth D. Merry be_lun->dev_path); 2032130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 2033130f4520SKenneth D. Merry be_lun->dev_path = dev_name; 2034130f4520SKenneth D. Merry goto again; 2035130f4520SKenneth D. Merry } 2036130f4520SKenneth D. Merry } 2037130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 203819720f41SAlexander Motin "error opening %s: %d", be_lun->dev_path, error); 2039130f4520SKenneth D. Merry return (error); 2040130f4520SKenneth D. Merry } 2041130f4520SKenneth D. Merry 2042130f4520SKenneth D. Merry NDFREE(&nd, NDF_ONLY_PNBUF); 2043130f4520SKenneth D. Merry 2044130f4520SKenneth D. Merry be_lun->vn = nd.ni_vp; 2045130f4520SKenneth D. Merry 2046130f4520SKenneth D. Merry /* We only support disks and files. */ 2047130f4520SKenneth D. Merry if (vn_isdisk(be_lun->vn, &error)) { 2048130f4520SKenneth D. Merry error = ctl_be_block_open_dev(be_lun, req); 2049130f4520SKenneth D. Merry } else if (be_lun->vn->v_type == VREG) { 2050130f4520SKenneth D. Merry error = ctl_be_block_open_file(be_lun, req); 2051130f4520SKenneth D. Merry } else { 2052130f4520SKenneth D. Merry error = EINVAL; 2053130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2054025a2301SEdward Tomasz Napierala "%s is not a disk or plain file", be_lun->dev_path); 2055130f4520SKenneth D. Merry } 2056130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 2057130f4520SKenneth D. Merry 2058130f4520SKenneth D. Merry if (error != 0) { 2059130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 2060130f4520SKenneth D. Merry return (error); 2061130f4520SKenneth D. Merry } 2062130f4520SKenneth D. Merry 2063130f4520SKenneth D. Merry be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2064130f4520SKenneth D. Merry be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2065130f4520SKenneth D. Merry 2066130f4520SKenneth D. Merry return (0); 2067130f4520SKenneth D. Merry } 2068130f4520SKenneth D. Merry 2069130f4520SKenneth D. Merry static int 2070130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2071130f4520SKenneth D. Merry { 2072130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2073130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 207457a5db13SAlexander Motin char num_thread_str[16]; 2075130f4520SKenneth D. Merry char tmpstr[32]; 207657a5db13SAlexander Motin char *value; 2077ee7f31c0SAlexander Motin int retval, num_threads, unmap; 207857a5db13SAlexander Motin int tmp_num_threads; 2079130f4520SKenneth D. Merry 2080130f4520SKenneth D. Merry params = &req->reqdata.create; 2081130f4520SKenneth D. Merry retval = 0; 208219720f41SAlexander Motin req->status = CTL_LUN_OK; 2083130f4520SKenneth D. Merry 2084130f4520SKenneth D. Merry num_threads = cbb_num_threads; 2085130f4520SKenneth D. Merry 2086130f4520SKenneth D. Merry be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2087130f4520SKenneth D. Merry 208819720f41SAlexander Motin be_lun->params = req->reqdata.create; 2089130f4520SKenneth D. Merry be_lun->softc = softc; 2090130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->input_queue); 2091*ef8daf3fSAlexander Motin STAILQ_INIT(&be_lun->config_read_queue); 2092130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->config_write_queue); 2093130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->datamove_queue); 2094130f4520SKenneth D. Merry sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 209575c7a1d3SAlexander Motin mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 209675c7a1d3SAlexander Motin mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 209743fb3a65SAlexander Motin ctl_init_opts(&be_lun->ctl_be_lun.options, 209843fb3a65SAlexander Motin req->num_be_args, req->kern_be_args); 2099130f4520SKenneth D. Merry 210008a7cce5SAlexander Motin be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2101eeb94054SAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2102130f4520SKenneth D. Merry 2103130f4520SKenneth D. Merry if (be_lun->lun_zone == NULL) { 2104130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 210519720f41SAlexander Motin "error allocating UMA zone"); 2106130f4520SKenneth D. Merry goto bailout_error; 2107130f4520SKenneth D. Merry } 2108130f4520SKenneth D. Merry 2109130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2110130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type = params->device_type; 2111130f4520SKenneth D. Merry else 2112130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type = T_DIRECT; 2113130f4520SKenneth D. Merry 2114130f4520SKenneth D. Merry if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 211543fb3a65SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 2116eb3687a6SAlexander Motin if (value == NULL) { 2117130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 211819720f41SAlexander Motin "no file argument specified"); 2119130f4520SKenneth D. Merry goto bailout_error; 2120130f4520SKenneth D. Merry } 2121eb3687a6SAlexander Motin be_lun->dev_path = strdup(value, M_CTLBLK); 212219720f41SAlexander Motin be_lun->blocksize = 512; 212319720f41SAlexander Motin be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2124130f4520SKenneth D. Merry 2125130f4520SKenneth D. Merry retval = ctl_be_block_open(softc, be_lun, req); 2126130f4520SKenneth D. Merry if (retval != 0) { 2127130f4520SKenneth D. Merry retval = 0; 212819720f41SAlexander Motin req->status = CTL_LUN_WARNING; 2129130f4520SKenneth D. Merry } 2130130f4520SKenneth D. Merry } else { 2131130f4520SKenneth D. Merry /* 2132130f4520SKenneth D. Merry * For processor devices, we don't have any size. 2133130f4520SKenneth D. Merry */ 2134130f4520SKenneth D. Merry be_lun->blocksize = 0; 2135f6012722SAlexander Motin be_lun->pblockexp = 0; 2136f6012722SAlexander Motin be_lun->pblockoff = 0; 2137130f4520SKenneth D. Merry be_lun->size_blocks = 0; 2138130f4520SKenneth D. Merry be_lun->size_bytes = 0; 2139130f4520SKenneth D. Merry be_lun->ctl_be_lun.maxlba = 0; 2140130f4520SKenneth D. Merry 2141130f4520SKenneth D. Merry /* 2142130f4520SKenneth D. Merry * Default to just 1 thread for processor devices. 2143130f4520SKenneth D. Merry */ 2144130f4520SKenneth D. Merry num_threads = 1; 2145130f4520SKenneth D. Merry } 2146130f4520SKenneth D. Merry 2147130f4520SKenneth D. Merry /* 2148130f4520SKenneth D. Merry * XXX This searching loop might be refactored to be combined with 2149130f4520SKenneth D. Merry * the loop above, 2150130f4520SKenneth D. Merry */ 215143fb3a65SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 215257a5db13SAlexander Motin if (value != NULL) { 215357a5db13SAlexander Motin tmp_num_threads = strtol(value, NULL, 0); 2154130f4520SKenneth D. Merry 2155130f4520SKenneth D. Merry /* 2156130f4520SKenneth D. Merry * We don't let the user specify less than one 2157130f4520SKenneth D. Merry * thread, but hope he's clueful enough not to 2158130f4520SKenneth D. Merry * specify 1000 threads. 2159130f4520SKenneth D. Merry */ 2160130f4520SKenneth D. Merry if (tmp_num_threads < 1) { 2161130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 216219720f41SAlexander Motin "invalid number of threads %s", 216319720f41SAlexander Motin num_thread_str); 2164130f4520SKenneth D. Merry goto bailout_error; 2165130f4520SKenneth D. Merry } 2166130f4520SKenneth D. Merry num_threads = tmp_num_threads; 216757a5db13SAlexander Motin } 2168c3e7ba3eSAlexander Motin unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol); 216943fb3a65SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2170c3e7ba3eSAlexander Motin if (value != NULL) 2171c3e7ba3eSAlexander Motin unmap = (strcmp(value, "on") == 0); 2172130f4520SKenneth D. Merry 2173130f4520SKenneth D. Merry be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2174130f4520SKenneth D. Merry be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 217519720f41SAlexander Motin if (be_lun->vn == NULL) 217619720f41SAlexander Motin be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2177ee7f31c0SAlexander Motin if (unmap) 2178ee7f31c0SAlexander Motin be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2179130f4520SKenneth D. Merry be_lun->ctl_be_lun.be_lun = be_lun; 218019720f41SAlexander Motin be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 218119720f41SAlexander Motin 0 : (be_lun->size_blocks - 1); 2182130f4520SKenneth D. Merry be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2183f6012722SAlexander Motin be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2184f6012722SAlexander Motin be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 218519720f41SAlexander Motin if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 218619720f41SAlexander Motin be_lun->blocksize != 0) 218719720f41SAlexander Motin be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 218819720f41SAlexander Motin be_lun->blocksize; 2189130f4520SKenneth D. Merry /* Tell the user the blocksize we ended up using */ 219019720f41SAlexander Motin params->lun_size_bytes = be_lun->size_bytes; 2191130f4520SKenneth D. Merry params->blocksize_bytes = be_lun->blocksize; 2192130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2193130f4520SKenneth D. Merry be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2194130f4520SKenneth D. Merry be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2195130f4520SKenneth D. Merry } else 2196130f4520SKenneth D. Merry be_lun->ctl_be_lun.req_lun_id = 0; 2197130f4520SKenneth D. Merry 2198130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2199130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_config_status = 2200130f4520SKenneth D. Merry ctl_be_block_lun_config_status; 2201130f4520SKenneth D. Merry be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2202130f4520SKenneth D. Merry 2203130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2204130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2205130f4520SKenneth D. Merry softc->num_luns); 2206130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2207130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2208130f4520SKenneth D. Merry sizeof(tmpstr))); 2209130f4520SKenneth D. Merry 2210130f4520SKenneth D. Merry /* Tell the user what we used for a serial number */ 2211130f4520SKenneth D. Merry strncpy((char *)params->serial_num, tmpstr, 2212130f4520SKenneth D. Merry ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 2213130f4520SKenneth D. Merry } else { 2214130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.serial_num, 2215130f4520SKenneth D. Merry params->serial_num, 2216130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2217130f4520SKenneth D. Merry sizeof(params->serial_num))); 2218130f4520SKenneth D. Merry } 2219130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2220130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2221130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2222130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2223130f4520SKenneth D. Merry sizeof(tmpstr))); 2224130f4520SKenneth D. Merry 2225130f4520SKenneth D. Merry /* Tell the user what we used for a device ID */ 2226130f4520SKenneth D. Merry strncpy((char *)params->device_id, tmpstr, 2227130f4520SKenneth D. Merry ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2228130f4520SKenneth D. Merry } else { 2229130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.device_id, 2230130f4520SKenneth D. Merry params->device_id, 2231130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2232130f4520SKenneth D. Merry sizeof(params->device_id))); 2233130f4520SKenneth D. Merry } 2234130f4520SKenneth D. Merry 2235130f4520SKenneth D. Merry TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2236130f4520SKenneth D. Merry 2237130f4520SKenneth D. Merry be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2238130f4520SKenneth D. Merry taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2239130f4520SKenneth D. Merry 2240130f4520SKenneth D. Merry if (be_lun->io_taskqueue == NULL) { 2241130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 224219720f41SAlexander Motin "unable to create taskqueue"); 2243130f4520SKenneth D. Merry goto bailout_error; 2244130f4520SKenneth D. Merry } 2245130f4520SKenneth D. Merry 2246130f4520SKenneth D. Merry /* 2247130f4520SKenneth D. Merry * Note that we start the same number of threads by default for 2248130f4520SKenneth D. Merry * both the file case and the block device case. For the file 2249130f4520SKenneth D. Merry * case, we need multiple threads to allow concurrency, because the 2250130f4520SKenneth D. Merry * vnode interface is designed to be a blocking interface. For the 2251130f4520SKenneth D. Merry * block device case, ZFS zvols at least will block the caller's 2252130f4520SKenneth D. Merry * context in many instances, and so we need multiple threads to 2253130f4520SKenneth D. Merry * overcome that problem. Other block devices don't need as many 2254130f4520SKenneth D. Merry * threads, but they shouldn't cause too many problems. 2255130f4520SKenneth D. Merry * 2256130f4520SKenneth D. Merry * If the user wants to just have a single thread for a block 2257130f4520SKenneth D. Merry * device, he can specify that when the LUN is created, or change 2258130f4520SKenneth D. Merry * the tunable/sysctl to alter the default number of threads. 2259130f4520SKenneth D. Merry */ 2260130f4520SKenneth D. Merry retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2261130f4520SKenneth D. Merry /*num threads*/num_threads, 2262130f4520SKenneth D. Merry /*priority*/PWAIT, 2263130f4520SKenneth D. Merry /*thread name*/ 2264130f4520SKenneth D. Merry "%s taskq", be_lun->lunname); 2265130f4520SKenneth D. Merry 2266130f4520SKenneth D. Merry if (retval != 0) 2267130f4520SKenneth D. Merry goto bailout_error; 2268130f4520SKenneth D. Merry 2269130f4520SKenneth D. Merry be_lun->num_threads = num_threads; 2270130f4520SKenneth D. Merry 2271130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2272130f4520SKenneth D. Merry softc->num_luns++; 2273130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2274130f4520SKenneth D. Merry 2275130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2276130f4520SKenneth D. Merry 2277130f4520SKenneth D. Merry retval = ctl_add_lun(&be_lun->ctl_be_lun); 2278130f4520SKenneth D. Merry if (retval != 0) { 2279130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2280130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2281130f4520SKenneth D. Merry links); 2282130f4520SKenneth D. Merry softc->num_luns--; 2283130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2284130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 228519720f41SAlexander Motin "ctl_add_lun() returned error %d, see dmesg for " 228619720f41SAlexander Motin "details", retval); 2287130f4520SKenneth D. Merry retval = 0; 2288130f4520SKenneth D. Merry goto bailout_error; 2289130f4520SKenneth D. Merry } 2290130f4520SKenneth D. Merry 2291130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2292130f4520SKenneth D. Merry 2293130f4520SKenneth D. Merry /* 2294130f4520SKenneth D. Merry * Tell the config_status routine that we're waiting so it won't 2295130f4520SKenneth D. Merry * clean up the LUN in the event of an error. 2296130f4520SKenneth D. Merry */ 2297130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2298130f4520SKenneth D. Merry 2299130f4520SKenneth D. Merry while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2300130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2301130f4520SKenneth D. Merry if (retval == EINTR) 2302130f4520SKenneth D. Merry break; 2303130f4520SKenneth D. Merry } 2304130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2305130f4520SKenneth D. Merry 2306130f4520SKenneth D. Merry if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2307130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 230819720f41SAlexander Motin "LUN configuration error, see dmesg for details"); 2309130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2310130f4520SKenneth D. Merry links); 2311130f4520SKenneth D. Merry softc->num_luns--; 2312130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2313130f4520SKenneth D. Merry goto bailout_error; 2314130f4520SKenneth D. Merry } else { 2315130f4520SKenneth D. Merry params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2316130f4520SKenneth D. Merry } 2317130f4520SKenneth D. Merry 2318130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2319130f4520SKenneth D. Merry 2320130f4520SKenneth D. Merry be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2321130f4520SKenneth D. Merry be_lun->blocksize, 2322130f4520SKenneth D. Merry DEVSTAT_ALL_SUPPORTED, 2323130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type 2324130f4520SKenneth D. Merry | DEVSTAT_TYPE_IF_OTHER, 2325130f4520SKenneth D. Merry DEVSTAT_PRIORITY_OTHER); 2326130f4520SKenneth D. Merry 2327130f4520SKenneth D. Merry return (retval); 2328130f4520SKenneth D. Merry 2329130f4520SKenneth D. Merry bailout_error: 2330130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2331130f4520SKenneth D. Merry 23329e005bbcSAlexander Motin if (be_lun->io_taskqueue != NULL) 23339e005bbcSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 2334130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 23359e005bbcSAlexander Motin if (be_lun->dev_path != NULL) 2336130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 23379e005bbcSAlexander Motin if (be_lun->lun_zone != NULL) 23389e005bbcSAlexander Motin uma_zdestroy(be_lun->lun_zone); 233943fb3a65SAlexander Motin ctl_free_opts(&be_lun->ctl_be_lun.options); 234075c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 234175c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2342130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2343130f4520SKenneth D. Merry 2344130f4520SKenneth D. Merry return (retval); 2345130f4520SKenneth D. Merry } 2346130f4520SKenneth D. Merry 2347130f4520SKenneth D. Merry static int 2348130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2349130f4520SKenneth D. Merry { 2350130f4520SKenneth D. Merry struct ctl_lun_rm_params *params; 2351130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2352130f4520SKenneth D. Merry int retval; 2353130f4520SKenneth D. Merry 2354130f4520SKenneth D. Merry params = &req->reqdata.rm; 2355130f4520SKenneth D. Merry 2356130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2357130f4520SKenneth D. Merry 2358130f4520SKenneth D. Merry be_lun = NULL; 2359130f4520SKenneth D. Merry 2360130f4520SKenneth D. Merry STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2361130f4520SKenneth D. Merry if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2362130f4520SKenneth D. Merry break; 2363130f4520SKenneth D. Merry } 2364130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2365130f4520SKenneth D. Merry 2366130f4520SKenneth D. Merry if (be_lun == NULL) { 2367130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 236819720f41SAlexander Motin "LUN %u is not managed by the block backend", 236919720f41SAlexander Motin params->lun_id); 2370130f4520SKenneth D. Merry goto bailout_error; 2371130f4520SKenneth D. Merry } 2372130f4520SKenneth D. Merry 2373130f4520SKenneth D. Merry retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2374130f4520SKenneth D. Merry 2375130f4520SKenneth D. Merry if (retval != 0) { 2376130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 237719720f41SAlexander Motin "error %d returned from ctl_disable_lun() for " 237819720f41SAlexander Motin "LUN %d", retval, params->lun_id); 2379130f4520SKenneth D. Merry goto bailout_error; 2380130f4520SKenneth D. Merry 2381130f4520SKenneth D. Merry } 2382130f4520SKenneth D. Merry 2383130f4520SKenneth D. Merry retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2384130f4520SKenneth D. Merry if (retval != 0) { 2385130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 238619720f41SAlexander Motin "error %d returned from ctl_invalidate_lun() for " 238719720f41SAlexander Motin "LUN %d", retval, params->lun_id); 2388130f4520SKenneth D. Merry goto bailout_error; 2389130f4520SKenneth D. Merry } 2390130f4520SKenneth D. Merry 2391130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2392130f4520SKenneth D. Merry 2393130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2394130f4520SKenneth D. Merry 2395130f4520SKenneth D. Merry while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2396130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2397130f4520SKenneth D. Merry if (retval == EINTR) 2398130f4520SKenneth D. Merry break; 2399130f4520SKenneth D. Merry } 2400130f4520SKenneth D. Merry 2401130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2402130f4520SKenneth D. Merry 2403130f4520SKenneth D. Merry if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2404130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 240519720f41SAlexander Motin "interrupted waiting for LUN to be freed"); 2406130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2407130f4520SKenneth D. Merry goto bailout_error; 2408130f4520SKenneth D. Merry } 2409130f4520SKenneth D. Merry 2410130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2411130f4520SKenneth D. Merry 2412130f4520SKenneth D. Merry softc->num_luns--; 2413130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2414130f4520SKenneth D. Merry 2415130f4520SKenneth D. Merry taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2416130f4520SKenneth D. Merry 2417130f4520SKenneth D. Merry taskqueue_free(be_lun->io_taskqueue); 2418130f4520SKenneth D. Merry 2419130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 2420130f4520SKenneth D. Merry 2421130f4520SKenneth D. Merry if (be_lun->disk_stats != NULL) 2422130f4520SKenneth D. Merry devstat_remove_entry(be_lun->disk_stats); 2423130f4520SKenneth D. Merry 2424130f4520SKenneth D. Merry uma_zdestroy(be_lun->lun_zone); 2425130f4520SKenneth D. Merry 242643fb3a65SAlexander Motin ctl_free_opts(&be_lun->ctl_be_lun.options); 2427130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 242875c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 242975c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2430130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2431130f4520SKenneth D. Merry 2432130f4520SKenneth D. Merry req->status = CTL_LUN_OK; 2433130f4520SKenneth D. Merry 2434130f4520SKenneth D. Merry return (0); 2435130f4520SKenneth D. Merry 2436130f4520SKenneth D. Merry bailout_error: 2437130f4520SKenneth D. Merry 2438130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2439130f4520SKenneth D. Merry 2440130f4520SKenneth D. Merry return (0); 2441130f4520SKenneth D. Merry } 2442130f4520SKenneth D. Merry 244381177295SEdward Tomasz Napierala static int 244481177295SEdward Tomasz Napierala ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 244581177295SEdward Tomasz Napierala struct ctl_lun_req *req) 244681177295SEdward Tomasz Napierala { 244781177295SEdward Tomasz Napierala struct vattr vattr; 244881177295SEdward Tomasz Napierala int error; 244919720f41SAlexander Motin struct ctl_lun_create_params *params = &be_lun->params; 245081177295SEdward Tomasz Napierala 245181177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 245281177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 245381177295SEdward Tomasz Napierala } else { 245471d8e97eSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 245581177295SEdward Tomasz Napierala error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 245671d8e97eSAlexander Motin VOP_UNLOCK(be_lun->vn, 0); 245781177295SEdward Tomasz Napierala if (error != 0) { 245881177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 245981177295SEdward Tomasz Napierala "error calling VOP_GETATTR() for file %s", 246081177295SEdward Tomasz Napierala be_lun->dev_path); 246181177295SEdward Tomasz Napierala return (error); 246281177295SEdward Tomasz Napierala } 246381177295SEdward Tomasz Napierala 246481177295SEdward Tomasz Napierala be_lun->size_bytes = vattr.va_size; 246581177295SEdward Tomasz Napierala } 246681177295SEdward Tomasz Napierala 246781177295SEdward Tomasz Napierala return (0); 246881177295SEdward Tomasz Napierala } 246981177295SEdward Tomasz Napierala 247081177295SEdward Tomasz Napierala static int 247181177295SEdward Tomasz Napierala ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 247281177295SEdward Tomasz Napierala struct ctl_lun_req *req) 247381177295SEdward Tomasz Napierala { 247471d8e97eSAlexander Motin struct ctl_be_block_devdata *dev_data; 247581177295SEdward Tomasz Napierala int error; 247619720f41SAlexander Motin struct ctl_lun_create_params *params = &be_lun->params; 247781177295SEdward Tomasz Napierala uint64_t size_bytes; 247881177295SEdward Tomasz Napierala 247971d8e97eSAlexander Motin dev_data = &be_lun->backend.dev; 248071d8e97eSAlexander Motin if (!dev_data->csw->d_ioctl) { 248181177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 248219720f41SAlexander Motin "no d_ioctl for device %s!", be_lun->dev_path); 248381177295SEdward Tomasz Napierala return (ENODEV); 248481177295SEdward Tomasz Napierala } 248581177295SEdward Tomasz Napierala 248671d8e97eSAlexander Motin error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 248781177295SEdward Tomasz Napierala (caddr_t)&size_bytes, FREAD, 248881177295SEdward Tomasz Napierala curthread); 248981177295SEdward Tomasz Napierala if (error) { 249081177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 249119720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE ioctl " 249219720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 249381177295SEdward Tomasz Napierala return (error); 249481177295SEdward Tomasz Napierala } 249581177295SEdward Tomasz Napierala 249681177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 249781177295SEdward Tomasz Napierala if (params->lun_size_bytes > size_bytes) { 249881177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 249919720f41SAlexander Motin "requested LUN size %ju > backing device " 250019720f41SAlexander Motin "size %ju", 250181177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 250281177295SEdward Tomasz Napierala (uintmax_t)size_bytes); 250381177295SEdward Tomasz Napierala return (EINVAL); 250481177295SEdward Tomasz Napierala } 250581177295SEdward Tomasz Napierala 250681177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 250781177295SEdward Tomasz Napierala } else { 250881177295SEdward Tomasz Napierala be_lun->size_bytes = size_bytes; 250981177295SEdward Tomasz Napierala } 251081177295SEdward Tomasz Napierala 251181177295SEdward Tomasz Napierala return (0); 251281177295SEdward Tomasz Napierala } 251381177295SEdward Tomasz Napierala 251481177295SEdward Tomasz Napierala static int 251581177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 251681177295SEdward Tomasz Napierala { 251781177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 251881177295SEdward Tomasz Napierala struct ctl_be_block_lun *be_lun; 251971d8e97eSAlexander Motin uint64_t oldsize; 25205050aa86SKonstantin Belousov int error; 252181177295SEdward Tomasz Napierala 252281177295SEdward Tomasz Napierala params = &req->reqdata.modify; 252381177295SEdward Tomasz Napierala 252481177295SEdward Tomasz Napierala mtx_lock(&softc->lock); 252581177295SEdward Tomasz Napierala be_lun = NULL; 252681177295SEdward Tomasz Napierala STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 252781177295SEdward Tomasz Napierala if (be_lun->ctl_be_lun.lun_id == params->lun_id) 252881177295SEdward Tomasz Napierala break; 252981177295SEdward Tomasz Napierala } 253081177295SEdward Tomasz Napierala mtx_unlock(&softc->lock); 253181177295SEdward Tomasz Napierala 253281177295SEdward Tomasz Napierala if (be_lun == NULL) { 253381177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 253419720f41SAlexander Motin "LUN %u is not managed by the block backend", 253519720f41SAlexander Motin params->lun_id); 253681177295SEdward Tomasz Napierala goto bailout_error; 253781177295SEdward Tomasz Napierala } 253881177295SEdward Tomasz Napierala 253919720f41SAlexander Motin be_lun->params.lun_size_bytes = params->lun_size_bytes; 254081177295SEdward Tomasz Napierala 25413f829b0cSAlexander Motin oldsize = be_lun->size_bytes; 254219720f41SAlexander Motin if (be_lun->vn == NULL) 254319720f41SAlexander Motin error = ctl_be_block_open(softc, be_lun, req); 254419720f41SAlexander Motin else if (be_lun->vn->v_type == VREG) 254581177295SEdward Tomasz Napierala error = ctl_be_block_modify_file(be_lun, req); 254681177295SEdward Tomasz Napierala else 254781177295SEdward Tomasz Napierala error = ctl_be_block_modify_dev(be_lun, req); 254881177295SEdward Tomasz Napierala 25493f829b0cSAlexander Motin if (error == 0 && be_lun->size_bytes != oldsize) { 255071d8e97eSAlexander Motin be_lun->size_blocks = be_lun->size_bytes >> 255171d8e97eSAlexander Motin be_lun->blocksize_shift; 255281177295SEdward Tomasz Napierala 255381177295SEdward Tomasz Napierala /* 255481177295SEdward Tomasz Napierala * The maximum LBA is the size - 1. 255581177295SEdward Tomasz Napierala * 255681177295SEdward Tomasz Napierala * XXX: Note that this field is being updated without locking, 255781177295SEdward Tomasz Napierala * which might cause problems on 32-bit architectures. 255881177295SEdward Tomasz Napierala */ 255919720f41SAlexander Motin be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 256019720f41SAlexander Motin 0 : (be_lun->size_blocks - 1); 256119720f41SAlexander Motin be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 256219720f41SAlexander Motin be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 256319720f41SAlexander Motin be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 256419720f41SAlexander Motin if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 256519720f41SAlexander Motin be_lun->blocksize != 0) 256619720f41SAlexander Motin be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 256719720f41SAlexander Motin be_lun->blocksize; 256881177295SEdward Tomasz Napierala ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 256919720f41SAlexander Motin if (oldsize == 0 && be_lun->size_blocks != 0) 257019720f41SAlexander Motin ctl_lun_online(&be_lun->ctl_be_lun); 257171d8e97eSAlexander Motin } 257281177295SEdward Tomasz Napierala 257381177295SEdward Tomasz Napierala /* Tell the user the exact size we ended up using */ 257481177295SEdward Tomasz Napierala params->lun_size_bytes = be_lun->size_bytes; 257581177295SEdward Tomasz Napierala 257619720f41SAlexander Motin req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 257781177295SEdward Tomasz Napierala 257881177295SEdward Tomasz Napierala return (0); 257981177295SEdward Tomasz Napierala 258081177295SEdward Tomasz Napierala bailout_error: 258181177295SEdward Tomasz Napierala req->status = CTL_LUN_ERROR; 258281177295SEdward Tomasz Napierala 258381177295SEdward Tomasz Napierala return (0); 258481177295SEdward Tomasz Napierala } 258581177295SEdward Tomasz Napierala 2586130f4520SKenneth D. Merry static void 2587130f4520SKenneth D. Merry ctl_be_block_lun_shutdown(void *be_lun) 2588130f4520SKenneth D. Merry { 2589130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2590130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2591130f4520SKenneth D. Merry 2592130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2593130f4520SKenneth D. Merry 2594130f4520SKenneth D. Merry softc = lun->softc; 2595130f4520SKenneth D. Merry 2596130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2597130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2598130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2599130f4520SKenneth D. Merry wakeup(lun); 2600130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2601130f4520SKenneth D. Merry 2602130f4520SKenneth D. Merry } 2603130f4520SKenneth D. Merry 2604130f4520SKenneth D. Merry static void 2605130f4520SKenneth D. Merry ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2606130f4520SKenneth D. Merry { 2607130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2608130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2609130f4520SKenneth D. Merry 2610130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2611130f4520SKenneth D. Merry softc = lun->softc; 2612130f4520SKenneth D. Merry 2613130f4520SKenneth D. Merry if (status == CTL_LUN_CONFIG_OK) { 2614130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2615130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2616130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2617130f4520SKenneth D. Merry wakeup(lun); 2618130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2619130f4520SKenneth D. Merry 2620130f4520SKenneth D. Merry /* 2621130f4520SKenneth D. Merry * We successfully added the LUN, attempt to enable it. 2622130f4520SKenneth D. Merry */ 2623130f4520SKenneth D. Merry if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2624130f4520SKenneth D. Merry printf("%s: ctl_enable_lun() failed!\n", __func__); 2625130f4520SKenneth D. Merry if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2626130f4520SKenneth D. Merry printf("%s: ctl_invalidate_lun() failed!\n", 2627130f4520SKenneth D. Merry __func__); 2628130f4520SKenneth D. Merry } 2629130f4520SKenneth D. Merry } 2630130f4520SKenneth D. Merry 2631130f4520SKenneth D. Merry return; 2632130f4520SKenneth D. Merry } 2633130f4520SKenneth D. Merry 2634130f4520SKenneth D. Merry 2635130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2636130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2637130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2638130f4520SKenneth D. Merry wakeup(lun); 2639130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2640130f4520SKenneth D. Merry } 2641130f4520SKenneth D. Merry 2642130f4520SKenneth D. Merry 2643130f4520SKenneth D. Merry static int 2644130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io) 2645130f4520SKenneth D. Merry { 2646130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2647130f4520SKenneth D. Merry struct ctl_be_lun *ctl_be_lun; 2648130f4520SKenneth D. Merry int retval; 2649130f4520SKenneth D. Merry 2650130f4520SKenneth D. Merry retval = 0; 2651130f4520SKenneth D. Merry 2652130f4520SKenneth D. Merry DPRINTF("entered\n"); 2653130f4520SKenneth D. Merry 2654130f4520SKenneth D. Merry ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2655130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 2656130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2657130f4520SKenneth D. Merry 2658130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 2659130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 2660130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 2661ee7f31c0SAlexander Motin case WRITE_SAME_10: 2662ee7f31c0SAlexander Motin case WRITE_SAME_16: 2663ee7f31c0SAlexander Motin case UNMAP: 2664130f4520SKenneth D. Merry /* 2665130f4520SKenneth D. Merry * The upper level CTL code will filter out any CDBs with 2666130f4520SKenneth D. Merry * the immediate bit set and return the proper error. 2667130f4520SKenneth D. Merry * 2668130f4520SKenneth D. Merry * We don't really need to worry about what LBA range the 2669130f4520SKenneth D. Merry * user asked to be synced out. When they issue a sync 2670130f4520SKenneth D. Merry * cache command, we'll sync out the whole thing. 2671130f4520SKenneth D. Merry */ 267275c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 2673130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2674130f4520SKenneth D. Merry links); 267575c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2676130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2677130f4520SKenneth D. Merry break; 2678130f4520SKenneth D. Merry case START_STOP_UNIT: { 2679130f4520SKenneth D. Merry struct scsi_start_stop_unit *cdb; 2680130f4520SKenneth D. Merry 2681130f4520SKenneth D. Merry cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2682130f4520SKenneth D. Merry 2683130f4520SKenneth D. Merry if (cdb->how & SSS_START) 2684130f4520SKenneth D. Merry retval = ctl_start_lun(ctl_be_lun); 2685130f4520SKenneth D. Merry else { 2686130f4520SKenneth D. Merry retval = ctl_stop_lun(ctl_be_lun); 2687130f4520SKenneth D. Merry /* 2688130f4520SKenneth D. Merry * XXX KDM Copan-specific offline behavior. 2689130f4520SKenneth D. Merry * Figure out a reasonable way to port this? 2690130f4520SKenneth D. Merry */ 2691130f4520SKenneth D. Merry #ifdef NEEDTOPORT 2692130f4520SKenneth D. Merry if ((retval == 0) 2693130f4520SKenneth D. Merry && (cdb->byte2 & SSS_ONOFFLINE)) 2694130f4520SKenneth D. Merry retval = ctl_lun_offline(ctl_be_lun); 2695130f4520SKenneth D. Merry #endif 2696130f4520SKenneth D. Merry } 2697130f4520SKenneth D. Merry 2698130f4520SKenneth D. Merry /* 2699130f4520SKenneth D. Merry * In general, the above routines should not fail. They 2700130f4520SKenneth D. Merry * just set state for the LUN. So we've got something 2701130f4520SKenneth D. Merry * pretty wrong here if we can't start or stop the LUN. 2702130f4520SKenneth D. Merry */ 2703130f4520SKenneth D. Merry if (retval != 0) { 2704130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 2705130f4520SKenneth D. Merry /*sks_valid*/ 1, 2706130f4520SKenneth D. Merry /*retry_count*/ 0xf051); 2707130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2708130f4520SKenneth D. Merry } else { 2709130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 2710130f4520SKenneth D. Merry } 2711130f4520SKenneth D. Merry ctl_config_write_done(io); 2712130f4520SKenneth D. Merry break; 2713130f4520SKenneth D. Merry } 2714130f4520SKenneth D. Merry default: 2715130f4520SKenneth D. Merry ctl_set_invalid_opcode(&io->scsiio); 2716130f4520SKenneth D. Merry ctl_config_write_done(io); 2717130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2718130f4520SKenneth D. Merry break; 2719130f4520SKenneth D. Merry } 2720130f4520SKenneth D. Merry 2721130f4520SKenneth D. Merry return (retval); 2722130f4520SKenneth D. Merry } 2723130f4520SKenneth D. Merry 2724130f4520SKenneth D. Merry static int 2725130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io) 2726130f4520SKenneth D. Merry { 2727*ef8daf3fSAlexander Motin struct ctl_be_block_lun *be_lun; 2728*ef8daf3fSAlexander Motin struct ctl_be_lun *ctl_be_lun; 2729*ef8daf3fSAlexander Motin int retval = 0; 2730*ef8daf3fSAlexander Motin 2731*ef8daf3fSAlexander Motin DPRINTF("entered\n"); 2732*ef8daf3fSAlexander Motin 2733*ef8daf3fSAlexander Motin ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2734*ef8daf3fSAlexander Motin CTL_PRIV_BACKEND_LUN].ptr; 2735*ef8daf3fSAlexander Motin be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2736*ef8daf3fSAlexander Motin 2737*ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 2738*ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: 2739*ef8daf3fSAlexander Motin if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2740*ef8daf3fSAlexander Motin mtx_lock(&be_lun->queue_lock); 2741*ef8daf3fSAlexander Motin STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2742*ef8daf3fSAlexander Motin &io->io_hdr, links); 2743*ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 2744*ef8daf3fSAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, 2745*ef8daf3fSAlexander Motin &be_lun->io_task); 2746*ef8daf3fSAlexander Motin retval = CTL_RETVAL_QUEUED; 2747*ef8daf3fSAlexander Motin break; 2748*ef8daf3fSAlexander Motin } 2749*ef8daf3fSAlexander Motin ctl_set_invalid_field(&io->scsiio, 2750*ef8daf3fSAlexander Motin /*sks_valid*/ 1, 2751*ef8daf3fSAlexander Motin /*command*/ 1, 2752*ef8daf3fSAlexander Motin /*field*/ 1, 2753*ef8daf3fSAlexander Motin /*bit_valid*/ 1, 2754*ef8daf3fSAlexander Motin /*bit*/ 4); 2755*ef8daf3fSAlexander Motin ctl_config_read_done(io); 2756*ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2757*ef8daf3fSAlexander Motin break; 2758*ef8daf3fSAlexander Motin default: 2759*ef8daf3fSAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 2760*ef8daf3fSAlexander Motin ctl_config_read_done(io); 2761*ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2762*ef8daf3fSAlexander Motin break; 2763*ef8daf3fSAlexander Motin } 2764*ef8daf3fSAlexander Motin 2765*ef8daf3fSAlexander Motin return (retval); 2766130f4520SKenneth D. Merry } 2767130f4520SKenneth D. Merry 2768130f4520SKenneth D. Merry static int 2769130f4520SKenneth D. Merry ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2770130f4520SKenneth D. Merry { 2771130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2772130f4520SKenneth D. Merry int retval; 2773130f4520SKenneth D. Merry 2774130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2775130f4520SKenneth D. Merry retval = 0; 2776130f4520SKenneth D. Merry 27772cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "\t<num_threads>"); 2778130f4520SKenneth D. Merry 2779130f4520SKenneth D. Merry if (retval != 0) 2780130f4520SKenneth D. Merry goto bailout; 2781130f4520SKenneth D. Merry 2782130f4520SKenneth D. Merry retval = sbuf_printf(sb, "%d", lun->num_threads); 2783130f4520SKenneth D. Merry 2784130f4520SKenneth D. Merry if (retval != 0) 2785130f4520SKenneth D. Merry goto bailout; 2786130f4520SKenneth D. Merry 27872cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "</num_threads>\n"); 2788130f4520SKenneth D. Merry 2789130f4520SKenneth D. Merry bailout: 2790130f4520SKenneth D. Merry 2791130f4520SKenneth D. Merry return (retval); 2792130f4520SKenneth D. Merry } 2793130f4520SKenneth D. Merry 2794c3e7ba3eSAlexander Motin static uint64_t 2795c3e7ba3eSAlexander Motin ctl_be_block_lun_attr(void *be_lun, const char *attrname) 2796c3e7ba3eSAlexander Motin { 2797c3e7ba3eSAlexander Motin struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2798c3e7ba3eSAlexander Motin 2799c3e7ba3eSAlexander Motin if (lun->getattr == NULL) 2800c3e7ba3eSAlexander Motin return (UINT64_MAX); 2801c3e7ba3eSAlexander Motin return (lun->getattr(lun, attrname)); 2802c3e7ba3eSAlexander Motin } 2803c3e7ba3eSAlexander Motin 2804130f4520SKenneth D. Merry int 2805130f4520SKenneth D. Merry ctl_be_block_init(void) 2806130f4520SKenneth D. Merry { 2807130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2808130f4520SKenneth D. Merry int retval; 2809130f4520SKenneth D. Merry 2810130f4520SKenneth D. Merry softc = &backend_block_softc; 2811130f4520SKenneth D. Merry retval = 0; 2812130f4520SKenneth D. Merry 281375c7a1d3SAlexander Motin mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2814a0e36aeeSEdward Tomasz Napierala beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2815a0e36aeeSEdward Tomasz Napierala NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2816130f4520SKenneth D. Merry STAILQ_INIT(&softc->disk_list); 2817130f4520SKenneth D. Merry STAILQ_INIT(&softc->lun_list); 2818130f4520SKenneth D. Merry 2819130f4520SKenneth D. Merry return (retval); 2820130f4520SKenneth D. Merry } 2821