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> 71ef8daf3fSAlexander 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_ioctl.h> 887ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h> 89130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h> 907ac58230SAlexander Motin #include <cam/ctl/ctl_private.h> 91130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h> 92130f4520SKenneth D. Merry 93130f4520SKenneth D. Merry /* 9408a7cce5SAlexander Motin * The idea here is that we'll allocate enough S/G space to hold a 1MB 9508a7cce5SAlexander Motin * I/O. If we get an I/O larger than that, we'll split it. 96130f4520SKenneth D. Merry */ 9711b569f7SAlexander Motin #define CTLBLK_HALF_IO_SIZE (512 * 1024) 9811b569f7SAlexander Motin #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 9908a7cce5SAlexander Motin #define CTLBLK_MAX_SEG MAXPHYS 10011b569f7SAlexander Motin #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 10111b569f7SAlexander Motin #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 102130f4520SKenneth D. Merry 103130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG 104130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \ 105130f4520SKenneth D. Merry printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 106130f4520SKenneth D. Merry #else 107130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0) 108130f4520SKenneth D. Merry #endif 109130f4520SKenneth D. Merry 110e86a4142SAlexander Motin #define PRIV(io) \ 111e86a4142SAlexander Motin ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 11211b569f7SAlexander Motin #define ARGS(io) \ 11311b569f7SAlexander Motin ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 114e86a4142SAlexander Motin 115130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb); 116130f4520SKenneth D. Merry 117130f4520SKenneth D. Merry typedef enum { 118130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 119130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 120130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_WAITING = 0x04, 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_filedata { 130130f4520SKenneth D. Merry struct ucred *cred; 131130f4520SKenneth D. Merry }; 132130f4520SKenneth D. Merry 133130f4520SKenneth D. Merry union ctl_be_block_bedata { 134130f4520SKenneth D. Merry struct ctl_be_block_filedata file; 135130f4520SKenneth D. Merry }; 136130f4520SKenneth D. Merry 137130f4520SKenneth D. Merry struct ctl_be_block_io; 138130f4520SKenneth D. Merry struct ctl_be_block_lun; 139130f4520SKenneth D. Merry 140130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 141130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 142c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 143c3e7ba3eSAlexander Motin const char *attrname); 144130f4520SKenneth D. Merry 145130f4520SKenneth D. Merry /* 146130f4520SKenneth D. Merry * Backend LUN structure. There is a 1:1 mapping between a block device 147130f4520SKenneth D. Merry * and a backend block LUN, and between a backend block LUN and a CTL LUN. 148130f4520SKenneth D. Merry */ 149130f4520SKenneth D. Merry struct ctl_be_block_lun { 15019720f41SAlexander Motin struct ctl_lun_create_params params; 151130f4520SKenneth D. Merry char lunname[32]; 152130f4520SKenneth D. Merry char *dev_path; 153130f4520SKenneth D. Merry ctl_be_block_type dev_type; 154130f4520SKenneth D. Merry struct vnode *vn; 155130f4520SKenneth D. Merry union ctl_be_block_bedata backend; 156130f4520SKenneth D. Merry cbb_dispatch_t dispatch; 157130f4520SKenneth D. Merry cbb_dispatch_t lun_flush; 158ee7f31c0SAlexander Motin cbb_dispatch_t unmap; 159ef8daf3fSAlexander Motin cbb_dispatch_t get_lba_status; 160c3e7ba3eSAlexander Motin cbb_getattr_t getattr; 161130f4520SKenneth D. Merry uma_zone_t lun_zone; 162130f4520SKenneth D. Merry uint64_t size_blocks; 163130f4520SKenneth D. Merry uint64_t size_bytes; 164130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 165130f4520SKenneth D. Merry struct devstat *disk_stats; 166130f4520SKenneth D. Merry ctl_be_block_lun_flags flags; 167130f4520SKenneth D. Merry STAILQ_ENTRY(ctl_be_block_lun) links; 1680bcd4ab6SAlexander Motin struct ctl_be_lun cbe_lun; 169130f4520SKenneth D. Merry struct taskqueue *io_taskqueue; 170130f4520SKenneth D. Merry struct task io_task; 171130f4520SKenneth D. Merry int num_threads; 172130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) input_queue; 173ef8daf3fSAlexander Motin STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 174130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 175130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 17675c7a1d3SAlexander Motin struct mtx_padalign io_lock; 17775c7a1d3SAlexander Motin struct mtx_padalign queue_lock; 178130f4520SKenneth D. Merry }; 179130f4520SKenneth D. Merry 180130f4520SKenneth D. Merry /* 181130f4520SKenneth D. Merry * Overall softc structure for the block backend module. 182130f4520SKenneth D. Merry */ 183130f4520SKenneth D. Merry struct ctl_be_block_softc { 184130f4520SKenneth D. Merry struct mtx lock; 185130f4520SKenneth D. Merry int num_luns; 186130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_be_block_lun) lun_list; 187130f4520SKenneth D. Merry }; 188130f4520SKenneth D. Merry 189130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc; 190130f4520SKenneth D. Merry 191130f4520SKenneth D. Merry /* 192130f4520SKenneth D. Merry * Per-I/O information. 193130f4520SKenneth D. Merry */ 194130f4520SKenneth D. Merry struct ctl_be_block_io { 195130f4520SKenneth D. Merry union ctl_io *io; 196130f4520SKenneth D. Merry struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 197130f4520SKenneth D. Merry struct iovec xiovecs[CTLBLK_MAX_SEGS]; 198130f4520SKenneth D. Merry int bio_cmd; 199130f4520SKenneth D. Merry int num_segs; 200130f4520SKenneth D. Merry int num_bios_sent; 201130f4520SKenneth D. Merry int num_bios_done; 202130f4520SKenneth D. Merry int send_complete; 203130f4520SKenneth D. Merry int num_errors; 204130f4520SKenneth D. Merry struct bintime ds_t0; 205130f4520SKenneth D. Merry devstat_tag_type ds_tag_type; 206130f4520SKenneth D. Merry devstat_trans_flags ds_trans_type; 207130f4520SKenneth D. Merry uint64_t io_len; 208130f4520SKenneth D. Merry uint64_t io_offset; 2097d0d4342SAlexander Motin int io_arg; 210130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 211130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 212ee7f31c0SAlexander Motin void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 213130f4520SKenneth D. Merry }; 214130f4520SKenneth D. Merry 2157ac58230SAlexander Motin extern struct ctl_softc *control_softc; 2167ac58230SAlexander Motin 217130f4520SKenneth D. Merry static int cbb_num_threads = 14; 218130f4520SKenneth D. Merry SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 219130f4520SKenneth D. Merry "CAM Target Layer Block Backend"); 220af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 221130f4520SKenneth D. Merry &cbb_num_threads, 0, "Number of threads per backing file"); 222130f4520SKenneth D. Merry 223130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 224130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio); 225130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio); 226130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io); 227130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio); 228130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 229130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 230130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 231130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 232ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 233ef8daf3fSAlexander Motin struct ctl_be_block_io *beio); 23453c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 23553c146deSAlexander Motin const char *attrname); 236130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 237130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 238ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 239ee7f31c0SAlexander Motin struct ctl_be_block_io *beio); 240130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 241130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 242c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 243c3e7ba3eSAlexander Motin const char *attrname); 244ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 245ef8daf3fSAlexander Motin union ctl_io *io); 246130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 247130f4520SKenneth D. Merry union ctl_io *io); 248130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 249130f4520SKenneth D. Merry union ctl_io *io); 250130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending); 251130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io); 252130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 253130f4520SKenneth D. Merry int flag, struct thread *td); 254130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 255130f4520SKenneth D. Merry struct ctl_lun_req *req); 256130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 257130f4520SKenneth D. Merry struct ctl_lun_req *req); 258130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 259130f4520SKenneth D. Merry static int ctl_be_block_open(struct ctl_be_block_softc *softc, 260130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, 261130f4520SKenneth D. Merry struct ctl_lun_req *req); 262130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc, 263130f4520SKenneth D. Merry struct ctl_lun_req *req); 264130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 265130f4520SKenneth D. Merry struct ctl_lun_req *req); 26681177295SEdward Tomasz Napierala static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 26781177295SEdward Tomasz Napierala struct ctl_lun_req *req); 26881177295SEdward Tomasz Napierala static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 26981177295SEdward Tomasz Napierala struct ctl_lun_req *req); 27081177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 27181177295SEdward Tomasz Napierala struct ctl_lun_req *req); 272130f4520SKenneth D. Merry static void ctl_be_block_lun_shutdown(void *be_lun); 273130f4520SKenneth D. Merry static void ctl_be_block_lun_config_status(void *be_lun, 274130f4520SKenneth D. Merry ctl_lun_config_status status); 275130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io); 276130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io); 277130f4520SKenneth D. Merry static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 278c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 279130f4520SKenneth D. Merry int ctl_be_block_init(void); 280130f4520SKenneth D. Merry 281130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver = 282130f4520SKenneth D. Merry { 2832a2443d8SKenneth D. Merry .name = "block", 2842a2443d8SKenneth D. Merry .flags = CTL_BE_FLAG_HAS_CONFIG, 2852a2443d8SKenneth D. Merry .init = ctl_be_block_init, 2862a2443d8SKenneth D. Merry .data_submit = ctl_be_block_submit, 2872a2443d8SKenneth D. Merry .data_move_done = ctl_be_block_move_done, 2882a2443d8SKenneth D. Merry .config_read = ctl_be_block_config_read, 2892a2443d8SKenneth D. Merry .config_write = ctl_be_block_config_write, 2902a2443d8SKenneth D. Merry .ioctl = ctl_be_block_ioctl, 291c3e7ba3eSAlexander Motin .lun_info = ctl_be_block_lun_info, 292c3e7ba3eSAlexander Motin .lun_attr = ctl_be_block_lun_attr 293130f4520SKenneth D. Merry }; 294130f4520SKenneth D. Merry 295130f4520SKenneth D. Merry MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 296130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 297130f4520SKenneth D. Merry 298a0e36aeeSEdward Tomasz Napierala static uma_zone_t beio_zone; 299a0e36aeeSEdward Tomasz Napierala 300130f4520SKenneth D. Merry static struct ctl_be_block_io * 301130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc) 302130f4520SKenneth D. Merry { 303130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 304130f4520SKenneth D. Merry 305a0e36aeeSEdward Tomasz Napierala beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 306130f4520SKenneth D. Merry beio->softc = softc; 307130f4520SKenneth D. Merry return (beio); 308130f4520SKenneth D. Merry } 309130f4520SKenneth D. Merry 310130f4520SKenneth D. Merry static void 311130f4520SKenneth D. Merry ctl_free_beio(struct ctl_be_block_io *beio) 312130f4520SKenneth D. Merry { 313130f4520SKenneth D. Merry int duplicate_free; 314130f4520SKenneth D. Merry int i; 315130f4520SKenneth D. Merry 316130f4520SKenneth D. Merry duplicate_free = 0; 317130f4520SKenneth D. Merry 318130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 319130f4520SKenneth D. Merry if (beio->sg_segs[i].addr == NULL) 320130f4520SKenneth D. Merry duplicate_free++; 321130f4520SKenneth D. Merry 322130f4520SKenneth D. Merry uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 323130f4520SKenneth D. Merry beio->sg_segs[i].addr = NULL; 32411b569f7SAlexander Motin 32511b569f7SAlexander Motin /* For compare we had two equal S/G lists. */ 32611b569f7SAlexander Motin if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 32711b569f7SAlexander Motin uma_zfree(beio->lun->lun_zone, 32811b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 32911b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 33011b569f7SAlexander Motin } 331130f4520SKenneth D. Merry } 332130f4520SKenneth D. Merry 333130f4520SKenneth D. Merry if (duplicate_free > 0) { 334130f4520SKenneth D. Merry printf("%s: %d duplicate frees out of %d segments\n", __func__, 335130f4520SKenneth D. Merry duplicate_free, beio->num_segs); 336130f4520SKenneth D. Merry } 337a0e36aeeSEdward Tomasz Napierala 338a0e36aeeSEdward Tomasz Napierala uma_zfree(beio_zone, beio); 339130f4520SKenneth D. Merry } 340130f4520SKenneth D. Merry 341130f4520SKenneth D. Merry static void 342130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio) 343130f4520SKenneth D. Merry { 34475c7a1d3SAlexander Motin union ctl_io *io = beio->io; 345130f4520SKenneth D. Merry 346ee7f31c0SAlexander Motin if (beio->beio_cont != NULL) { 347ee7f31c0SAlexander Motin beio->beio_cont(beio); 348ee7f31c0SAlexander Motin } else { 349130f4520SKenneth D. Merry ctl_free_beio(beio); 35011b569f7SAlexander Motin ctl_data_submit_done(io); 351130f4520SKenneth D. Merry } 352ee7f31c0SAlexander Motin } 353130f4520SKenneth D. Merry 354130f4520SKenneth D. Merry static int 355130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io) 356130f4520SKenneth D. Merry { 357130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 358130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 35911b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 360130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 361130f4520SKenneth D. Merry struct bintime cur_bt; 362130f4520SKenneth D. Merry #endif 36311b569f7SAlexander Motin int i; 364130f4520SKenneth D. Merry 365e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 366130f4520SKenneth D. Merry be_lun = beio->lun; 367130f4520SKenneth D. Merry 368130f4520SKenneth D. Merry DPRINTF("entered\n"); 369130f4520SKenneth D. Merry 370130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 371130f4520SKenneth D. Merry getbintime(&cur_bt); 372130f4520SKenneth D. Merry bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 373130f4520SKenneth D. Merry bintime_add(&io->io_hdr.dma_bt, &cur_bt); 374130f4520SKenneth D. Merry io->io_hdr.num_dmas++; 375130f4520SKenneth D. Merry #endif 37611b569f7SAlexander Motin io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 377130f4520SKenneth D. Merry 378130f4520SKenneth D. Merry /* 379130f4520SKenneth D. Merry * We set status at this point for read commands, and write 380130f4520SKenneth D. Merry * commands with errors. 381130f4520SKenneth D. Merry */ 382f7241cceSAlexander Motin if (io->io_hdr.flags & CTL_FLAG_ABORT) { 383f7241cceSAlexander Motin ; 384f7241cceSAlexander Motin } else if ((io->io_hdr.port_status == 0) && 38511b569f7SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 38611b569f7SAlexander Motin lbalen = ARGS(beio->io); 38711b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_READ) { 388130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 38911b569f7SAlexander Motin } else if (lbalen->flags & CTL_LLF_COMPARE) { 39011b569f7SAlexander Motin /* We have two data blocks ready for comparison. */ 39111b569f7SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 39211b569f7SAlexander Motin if (memcmp(beio->sg_segs[i].addr, 39311b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 39411b569f7SAlexander Motin beio->sg_segs[i].len) != 0) 39511b569f7SAlexander Motin break; 39611b569f7SAlexander Motin } 39711b569f7SAlexander Motin if (i < beio->num_segs) 39811b569f7SAlexander Motin ctl_set_sense(&io->scsiio, 39911b569f7SAlexander Motin /*current_error*/ 1, 40011b569f7SAlexander Motin /*sense_key*/ SSD_KEY_MISCOMPARE, 40111b569f7SAlexander Motin /*asc*/ 0x1D, 40211b569f7SAlexander Motin /*ascq*/ 0x00, 40311b569f7SAlexander Motin SSD_ELEM_NONE); 40411b569f7SAlexander Motin else 40511b569f7SAlexander Motin ctl_set_success(&io->scsiio); 40611b569f7SAlexander Motin } 407f7241cceSAlexander Motin } else if ((io->io_hdr.port_status != 0) && 408f7241cceSAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 409f7241cceSAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 410130f4520SKenneth D. Merry /* 411130f4520SKenneth D. Merry * For hardware error sense keys, the sense key 412130f4520SKenneth D. Merry * specific value is defined to be a retry count, 413130f4520SKenneth D. Merry * but we use it to pass back an internal FETD 414130f4520SKenneth D. Merry * error code. XXX KDM Hopefully the FETD is only 415130f4520SKenneth D. Merry * using 16 bits for an error code, since that's 416130f4520SKenneth D. Merry * all the space we have in the sks field. 417130f4520SKenneth D. Merry */ 418130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 419130f4520SKenneth D. Merry /*sks_valid*/ 1, 420130f4520SKenneth D. Merry /*retry_count*/ 421130f4520SKenneth D. Merry io->io_hdr.port_status); 422130f4520SKenneth D. Merry } 423130f4520SKenneth D. Merry 424130f4520SKenneth D. Merry /* 425130f4520SKenneth D. Merry * If this is a read, or a write with errors, it is done. 426130f4520SKenneth D. Merry */ 427130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_READ) 428130f4520SKenneth D. Merry || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 429130f4520SKenneth D. Merry || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 430130f4520SKenneth D. Merry ctl_complete_beio(beio); 431130f4520SKenneth D. Merry return (0); 432130f4520SKenneth D. Merry } 433130f4520SKenneth D. Merry 434130f4520SKenneth D. Merry /* 435130f4520SKenneth D. Merry * At this point, we have a write and the DMA completed 436130f4520SKenneth D. Merry * successfully. We now have to queue it to the task queue to 437130f4520SKenneth D. Merry * execute the backend I/O. That is because we do blocking 438130f4520SKenneth D. Merry * memory allocations, and in the file backing case, blocking I/O. 439130f4520SKenneth D. Merry * This move done routine is generally called in the SIM's 440130f4520SKenneth D. Merry * interrupt context, and therefore we cannot block. 441130f4520SKenneth D. Merry */ 44275c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 443130f4520SKenneth D. Merry /* 444130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 445130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 446130f4520SKenneth D. Merry * or deal with resource allocation here. 447130f4520SKenneth D. Merry */ 448130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 44975c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 450130f4520SKenneth D. Merry 451130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 452130f4520SKenneth D. Merry 453130f4520SKenneth D. Merry return (0); 454130f4520SKenneth D. Merry } 455130f4520SKenneth D. Merry 456130f4520SKenneth D. Merry static void 457130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio) 458130f4520SKenneth D. Merry { 459130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 460130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 461130f4520SKenneth D. Merry union ctl_io *io; 462e0c2f975SAlexander Motin int error; 463130f4520SKenneth D. Merry 464130f4520SKenneth D. Merry beio = bio->bio_caller1; 465130f4520SKenneth D. Merry be_lun = beio->lun; 466130f4520SKenneth D. Merry io = beio->io; 467130f4520SKenneth D. Merry 468130f4520SKenneth D. Merry DPRINTF("entered\n"); 469130f4520SKenneth D. Merry 470e0c2f975SAlexander Motin error = bio->bio_error; 47175c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 472e0c2f975SAlexander Motin if (error != 0) 473130f4520SKenneth D. Merry beio->num_errors++; 474130f4520SKenneth D. Merry 475130f4520SKenneth D. Merry beio->num_bios_done++; 476130f4520SKenneth D. Merry 477130f4520SKenneth D. Merry /* 478130f4520SKenneth D. Merry * XXX KDM will this cause WITNESS to complain? Holding a lock 479130f4520SKenneth D. Merry * during the free might cause it to complain. 480130f4520SKenneth D. Merry */ 481130f4520SKenneth D. Merry g_destroy_bio(bio); 482130f4520SKenneth D. Merry 483130f4520SKenneth D. Merry /* 484130f4520SKenneth D. Merry * If the send complete bit isn't set, or we aren't the last I/O to 485130f4520SKenneth D. Merry * complete, then we're done. 486130f4520SKenneth D. Merry */ 487130f4520SKenneth D. Merry if ((beio->send_complete == 0) 488130f4520SKenneth D. Merry || (beio->num_bios_done < beio->num_bios_sent)) { 48975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 490130f4520SKenneth D. Merry return; 491130f4520SKenneth D. Merry } 492130f4520SKenneth D. Merry 493130f4520SKenneth D. Merry /* 494130f4520SKenneth D. Merry * At this point, we've verified that we are the last I/O to 495130f4520SKenneth D. Merry * complete, so it's safe to drop the lock. 496130f4520SKenneth D. Merry */ 49775c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 49875c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 49975c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 50075c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 501130f4520SKenneth D. Merry 502130f4520SKenneth D. Merry /* 503130f4520SKenneth D. Merry * If there are any errors from the backing device, we fail the 504130f4520SKenneth D. Merry * entire I/O with a medium error. 505130f4520SKenneth D. Merry */ 506130f4520SKenneth D. Merry if (beio->num_errors > 0) { 507e0c2f975SAlexander Motin if (error == EOPNOTSUPP) { 508e0c2f975SAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 5090631de4aSAlexander Motin } else if (error == ENOSPC || error == EDQUOT) { 5104fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 511e0c2f975SAlexander Motin } else if (beio->bio_cmd == BIO_FLUSH) { 512130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 513130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 514130f4520SKenneth D. Merry /*sks_valid*/ 1, 515130f4520SKenneth D. Merry /*retry_count*/ 0xbad2); 516130f4520SKenneth D. Merry } else 517130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 518130f4520SKenneth D. Merry ctl_complete_beio(beio); 519130f4520SKenneth D. Merry return; 520130f4520SKenneth D. Merry } 521130f4520SKenneth D. Merry 522130f4520SKenneth D. Merry /* 52311b569f7SAlexander Motin * If this is a write, a flush, a delete or verify, we're all done. 524130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 525130f4520SKenneth D. Merry */ 526130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_WRITE) 527ee7f31c0SAlexander Motin || (beio->bio_cmd == BIO_FLUSH) 52811b569f7SAlexander Motin || (beio->bio_cmd == BIO_DELETE) 52911b569f7SAlexander Motin || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 530130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 531130f4520SKenneth D. Merry ctl_complete_beio(beio); 532130f4520SKenneth D. Merry } else { 533f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 534f7241cceSAlexander Motin beio->beio_cont == NULL) 535f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 536130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 537130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 538130f4520SKenneth D. Merry #endif 539130f4520SKenneth D. Merry ctl_datamove(io); 540130f4520SKenneth D. Merry } 541130f4520SKenneth D. Merry } 542130f4520SKenneth D. Merry 543130f4520SKenneth D. Merry static void 544130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 545130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 546130f4520SKenneth D. Merry { 54775c7a1d3SAlexander Motin union ctl_io *io = beio->io; 548130f4520SKenneth D. Merry struct mount *mountpoint; 5495050aa86SKonstantin Belousov int error, lock_flags; 550130f4520SKenneth D. Merry 551130f4520SKenneth D. Merry DPRINTF("entered\n"); 552130f4520SKenneth D. Merry 55375c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 55475c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 55575c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 55675c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 557130f4520SKenneth D. Merry 558130f4520SKenneth D. Merry (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 559130f4520SKenneth D. Merry 560130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 561130f4520SKenneth D. Merry || ((mountpoint == NULL) 562130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 563130f4520SKenneth D. Merry lock_flags = LK_SHARED; 564130f4520SKenneth D. Merry else 565130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 566130f4520SKenneth D. Merry 567130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 568130f4520SKenneth D. Merry 5697d0d4342SAlexander Motin error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 5707d0d4342SAlexander Motin curthread); 571130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 572130f4520SKenneth D. Merry 573130f4520SKenneth D. Merry vn_finished_write(mountpoint); 574130f4520SKenneth D. Merry 57575c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 57675c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 57775c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 57875c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 57975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 58075c7a1d3SAlexander Motin 581130f4520SKenneth D. Merry if (error == 0) 582130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 583130f4520SKenneth D. Merry else { 584130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 585130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 586130f4520SKenneth D. Merry /*sks_valid*/ 1, 587130f4520SKenneth D. Merry /*retry_count*/ 0xbad1); 588130f4520SKenneth D. Merry } 589130f4520SKenneth D. Merry 590130f4520SKenneth D. Merry ctl_complete_beio(beio); 591130f4520SKenneth D. Merry } 592130f4520SKenneth D. Merry 593d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 594d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 595d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 596d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 597130f4520SKenneth D. Merry 598130f4520SKenneth D. Merry static void 599130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 600130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 601130f4520SKenneth D. Merry { 602130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 603130f4520SKenneth D. Merry union ctl_io *io; 604130f4520SKenneth D. Merry struct uio xuio; 605130f4520SKenneth D. Merry struct iovec *xiovec; 6065050aa86SKonstantin Belousov int flags; 607130f4520SKenneth D. Merry int error, i; 608130f4520SKenneth D. Merry 609130f4520SKenneth D. Merry DPRINTF("entered\n"); 610130f4520SKenneth D. Merry 611130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 612130f4520SKenneth D. Merry io = beio->io; 61355551d05SAlexander Motin flags = 0; 61455551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 61555551d05SAlexander Motin flags |= IO_DIRECT; 61655551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 61755551d05SAlexander Motin flags |= IO_SYNC; 618130f4520SKenneth D. Merry 61911b569f7SAlexander Motin bzero(&xuio, sizeof(xuio)); 620130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 621130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 62211b569f7SAlexander Motin xuio.uio_rw = UIO_READ; 623130f4520SKenneth D. Merry } else { 624130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 625130f4520SKenneth D. Merry xuio.uio_rw = UIO_WRITE; 62611b569f7SAlexander Motin } 627130f4520SKenneth D. Merry xuio.uio_offset = beio->io_offset; 628130f4520SKenneth D. Merry xuio.uio_resid = beio->io_len; 629130f4520SKenneth D. Merry xuio.uio_segflg = UIO_SYSSPACE; 630130f4520SKenneth D. Merry xuio.uio_iov = beio->xiovecs; 631130f4520SKenneth D. Merry xuio.uio_iovcnt = beio->num_segs; 632130f4520SKenneth D. Merry xuio.uio_td = curthread; 633130f4520SKenneth D. Merry 634130f4520SKenneth D. Merry for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 635130f4520SKenneth D. Merry xiovec->iov_base = beio->sg_segs[i].addr; 636130f4520SKenneth D. Merry xiovec->iov_len = beio->sg_segs[i].len; 637130f4520SKenneth D. Merry } 638130f4520SKenneth D. Merry 63975c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 64075c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 64175c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 64275c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 64375c7a1d3SAlexander Motin 644130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 645130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 646130f4520SKenneth D. Merry 647130f4520SKenneth D. Merry /* 648130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for reads. If the 649130f4520SKenneth D. Merry * DIRECTIO option is configured into the kernel, it calls 650130f4520SKenneth D. Merry * ffs_rawread(). But that only works for single-segment 651130f4520SKenneth D. Merry * uios with user space addresses. In our case, with a 652130f4520SKenneth D. Merry * kernel uio, it still reads into the buffer cache, but it 653130f4520SKenneth D. Merry * will just try to release the buffer from the cache later 654130f4520SKenneth D. Merry * on in ffs_read(). 655130f4520SKenneth D. Merry * 656130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for reads. 657130f4520SKenneth D. Merry * 658130f4520SKenneth D. Merry * UFS does not pay attention to IO_SYNC for reads. 659130f4520SKenneth D. Merry * 660130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (which translates into the 661130f4520SKenneth D. Merry * Solaris define FRSYNC for zfs_read()) for reads. It 662130f4520SKenneth D. Merry * attempts to sync the file before reading. 663130f4520SKenneth D. Merry */ 66455551d05SAlexander Motin error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 665130f4520SKenneth D. Merry 666130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 66711b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 668130f4520SKenneth D. Merry } else { 669130f4520SKenneth D. Merry struct mount *mountpoint; 670130f4520SKenneth D. Merry int lock_flags; 671130f4520SKenneth D. Merry 672130f4520SKenneth D. Merry (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 673130f4520SKenneth D. Merry 674130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 675130f4520SKenneth D. Merry || ((mountpoint == NULL) 676130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 677130f4520SKenneth D. Merry lock_flags = LK_SHARED; 678130f4520SKenneth D. Merry else 679130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 680130f4520SKenneth D. Merry 681130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 682130f4520SKenneth D. Merry 683130f4520SKenneth D. Merry /* 684130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for writes. The write 685130f4520SKenneth D. Merry * is done asynchronously. (Normally the write would just 686130f4520SKenneth D. Merry * get put into cache. 687130f4520SKenneth D. Merry * 688130f4520SKenneth D. Merry * UFS pays attention to IO_SYNC for writes. It will 689130f4520SKenneth D. Merry * attempt to write the buffer out synchronously if that 690130f4520SKenneth D. Merry * flag is set. 691130f4520SKenneth D. Merry * 692130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for writes. 693130f4520SKenneth D. Merry * 694130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 695130f4520SKenneth D. Merry * for writes. It will flush the transaction from the 696130f4520SKenneth D. Merry * cache before returning. 697130f4520SKenneth D. Merry */ 69855551d05SAlexander Motin error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 699130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 700130f4520SKenneth D. Merry 701130f4520SKenneth D. Merry vn_finished_write(mountpoint); 70211b569f7SAlexander Motin SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 703130f4520SKenneth D. Merry } 704130f4520SKenneth D. Merry 70575c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 70675c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 70775c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 70875c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 70975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 71075c7a1d3SAlexander Motin 711130f4520SKenneth D. Merry /* 712130f4520SKenneth D. Merry * If we got an error, set the sense data to "MEDIUM ERROR" and 713130f4520SKenneth D. Merry * return the I/O to the user. 714130f4520SKenneth D. Merry */ 715130f4520SKenneth D. Merry if (error != 0) { 716130f4520SKenneth D. Merry char path_str[32]; 717130f4520SKenneth D. Merry 718130f4520SKenneth D. Merry ctl_scsi_path_string(io, path_str, sizeof(path_str)); 719130f4520SKenneth D. Merry printf("%s%s command returned errno %d\n", path_str, 720130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 7210631de4aSAlexander Motin if (error == ENOSPC || error == EDQUOT) { 7224fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 7234fc18ff9SAlexander Motin } else 724130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 725130f4520SKenneth D. Merry ctl_complete_beio(beio); 726130f4520SKenneth D. Merry return; 727130f4520SKenneth D. Merry } 728130f4520SKenneth D. Merry 729130f4520SKenneth D. Merry /* 730696297adSAlexander Motin * If this is a write or a verify, we're all done. 731130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 732130f4520SKenneth D. Merry */ 733696297adSAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 734696297adSAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 735130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 736130f4520SKenneth D. Merry ctl_complete_beio(beio); 737130f4520SKenneth D. Merry } else { 738f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 739f7241cceSAlexander Motin beio->beio_cont == NULL) 740f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 741130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 742130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 743130f4520SKenneth D. Merry #endif 744130f4520SKenneth D. Merry ctl_datamove(io); 745130f4520SKenneth D. Merry } 746130f4520SKenneth D. Merry } 747130f4520SKenneth D. Merry 748130f4520SKenneth D. Merry static void 749ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 750ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 751ef8daf3fSAlexander Motin { 752ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 753ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 754ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 755ef8daf3fSAlexander Motin off_t roff, off; 756ef8daf3fSAlexander Motin int error, status; 757ef8daf3fSAlexander Motin 758ef8daf3fSAlexander Motin DPRINTF("entered\n"); 759ef8daf3fSAlexander Motin 7600bcd4ab6SAlexander Motin off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 761ef8daf3fSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 762ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 763ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 764ef8daf3fSAlexander Motin if (error == 0 && off > roff) 765ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 766ef8daf3fSAlexander Motin else { 767ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 768ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 769ef8daf3fSAlexander Motin if (error == 0 && off > roff) 770ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 771ef8daf3fSAlexander Motin else { 772ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 773ef8daf3fSAlexander Motin off = be_lun->size_bytes; 774ef8daf3fSAlexander Motin } 775ef8daf3fSAlexander Motin } 776ef8daf3fSAlexander Motin VOP_UNLOCK(be_lun->vn, 0); 777ef8daf3fSAlexander Motin 778ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 779ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 7800bcd4ab6SAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 7810bcd4ab6SAlexander Motin lbalen->lba), data->descr[0].length); 782ef8daf3fSAlexander Motin data->descr[0].status = status; 783ef8daf3fSAlexander Motin 784ef8daf3fSAlexander Motin ctl_complete_beio(beio); 785ef8daf3fSAlexander Motin } 786ef8daf3fSAlexander Motin 78753c146deSAlexander Motin static uint64_t 78853c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 78953c146deSAlexander Motin { 79053c146deSAlexander Motin struct vattr vattr; 79153c146deSAlexander Motin struct statfs statfs; 792b9b4269cSAlexander Motin uint64_t val; 79353c146deSAlexander Motin int error; 79453c146deSAlexander Motin 795b9b4269cSAlexander Motin val = UINT64_MAX; 79653c146deSAlexander Motin if (be_lun->vn == NULL) 797b9b4269cSAlexander Motin return (val); 798b9b4269cSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 79953c146deSAlexander Motin if (strcmp(attrname, "blocksused") == 0) { 80053c146deSAlexander Motin error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 801b9b4269cSAlexander Motin if (error == 0) 8020bcd4ab6SAlexander Motin val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 80353c146deSAlexander Motin } 804b9b4269cSAlexander Motin if (strcmp(attrname, "blocksavail") == 0 && 805b9b4269cSAlexander Motin (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 80653c146deSAlexander Motin error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 807b9b4269cSAlexander Motin if (error == 0) 808a15bbf15SAlexander Motin val = statfs.f_bavail * statfs.f_bsize / 8090bcd4ab6SAlexander Motin be_lun->cbe_lun.blocksize; 81053c146deSAlexander Motin } 811b9b4269cSAlexander Motin VOP_UNLOCK(be_lun->vn, 0); 812b9b4269cSAlexander Motin return (val); 81353c146deSAlexander Motin } 81453c146deSAlexander Motin 815ef8daf3fSAlexander Motin static void 81667f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 81767f586a8SAlexander Motin struct ctl_be_block_io *beio) 81867f586a8SAlexander Motin { 81967f586a8SAlexander Motin union ctl_io *io; 8203236151eSAlexander Motin struct cdevsw *csw; 8213236151eSAlexander Motin struct cdev *dev; 82267f586a8SAlexander Motin struct uio xuio; 82367f586a8SAlexander Motin struct iovec *xiovec; 8243236151eSAlexander Motin int error, flags, i, ref; 82567f586a8SAlexander Motin 82667f586a8SAlexander Motin DPRINTF("entered\n"); 82767f586a8SAlexander Motin 82867f586a8SAlexander Motin io = beio->io; 82955551d05SAlexander Motin flags = 0; 83055551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 83155551d05SAlexander Motin flags |= IO_DIRECT; 83255551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 83355551d05SAlexander Motin flags |= IO_SYNC; 83467f586a8SAlexander Motin 83567f586a8SAlexander Motin bzero(&xuio, sizeof(xuio)); 83667f586a8SAlexander Motin if (beio->bio_cmd == BIO_READ) { 83767f586a8SAlexander Motin SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 83867f586a8SAlexander Motin xuio.uio_rw = UIO_READ; 83967f586a8SAlexander Motin } else { 84067f586a8SAlexander Motin SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 84167f586a8SAlexander Motin xuio.uio_rw = UIO_WRITE; 84267f586a8SAlexander Motin } 84367f586a8SAlexander Motin xuio.uio_offset = beio->io_offset; 84467f586a8SAlexander Motin xuio.uio_resid = beio->io_len; 84567f586a8SAlexander Motin xuio.uio_segflg = UIO_SYSSPACE; 84667f586a8SAlexander Motin xuio.uio_iov = beio->xiovecs; 84767f586a8SAlexander Motin xuio.uio_iovcnt = beio->num_segs; 84867f586a8SAlexander Motin xuio.uio_td = curthread; 84967f586a8SAlexander Motin 85067f586a8SAlexander Motin for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 85167f586a8SAlexander Motin xiovec->iov_base = beio->sg_segs[i].addr; 85267f586a8SAlexander Motin xiovec->iov_len = beio->sg_segs[i].len; 85367f586a8SAlexander Motin } 85467f586a8SAlexander Motin 85567f586a8SAlexander Motin binuptime(&beio->ds_t0); 85667f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 85767f586a8SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 85867f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 85967f586a8SAlexander Motin 8603236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 8613236151eSAlexander Motin if (csw) { 8623236151eSAlexander Motin if (beio->bio_cmd == BIO_READ) 8633236151eSAlexander Motin error = csw->d_read(dev, &xuio, flags); 8643236151eSAlexander Motin else 8653236151eSAlexander Motin error = csw->d_write(dev, &xuio, flags); 8663236151eSAlexander Motin dev_relthread(dev, ref); 8673236151eSAlexander Motin } else 8683236151eSAlexander Motin error = ENXIO; 8693236151eSAlexander Motin 8703236151eSAlexander Motin if (beio->bio_cmd == BIO_READ) 87167f586a8SAlexander Motin SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 8723236151eSAlexander Motin else 87367f586a8SAlexander Motin SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 87467f586a8SAlexander Motin 87567f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 87667f586a8SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 87767f586a8SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 87867f586a8SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 87967f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 88067f586a8SAlexander Motin 88167f586a8SAlexander Motin /* 88267f586a8SAlexander Motin * If we got an error, set the sense data to "MEDIUM ERROR" and 88367f586a8SAlexander Motin * return the I/O to the user. 88467f586a8SAlexander Motin */ 88567f586a8SAlexander Motin if (error != 0) { 8860631de4aSAlexander Motin if (error == ENOSPC || error == EDQUOT) { 8874fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 8884fc18ff9SAlexander Motin } else 88967f586a8SAlexander Motin ctl_set_medium_error(&io->scsiio); 89067f586a8SAlexander Motin ctl_complete_beio(beio); 89167f586a8SAlexander Motin return; 89267f586a8SAlexander Motin } 89367f586a8SAlexander Motin 89467f586a8SAlexander Motin /* 89567f586a8SAlexander Motin * If this is a write or a verify, we're all done. 89667f586a8SAlexander Motin * If this is a read, we can now send the data to the user. 89767f586a8SAlexander Motin */ 89867f586a8SAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 89967f586a8SAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 90067f586a8SAlexander Motin ctl_set_success(&io->scsiio); 90167f586a8SAlexander Motin ctl_complete_beio(beio); 90267f586a8SAlexander Motin } else { 903f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 904f7241cceSAlexander Motin beio->beio_cont == NULL) 905f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 90667f586a8SAlexander Motin #ifdef CTL_TIME_IO 90767f586a8SAlexander Motin getbintime(&io->io_hdr.dma_start_bt); 90867f586a8SAlexander Motin #endif 90967f586a8SAlexander Motin ctl_datamove(io); 91067f586a8SAlexander Motin } 91167f586a8SAlexander Motin } 91267f586a8SAlexander Motin 91367f586a8SAlexander Motin static void 914ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 915ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 916ef8daf3fSAlexander Motin { 917ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 9183236151eSAlexander Motin struct cdevsw *csw; 9193236151eSAlexander Motin struct cdev *dev; 920ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 921ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 922ef8daf3fSAlexander Motin off_t roff, off; 9233236151eSAlexander Motin int error, ref, status; 924ef8daf3fSAlexander Motin 925ef8daf3fSAlexander Motin DPRINTF("entered\n"); 926ef8daf3fSAlexander Motin 9273236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 9283236151eSAlexander Motin if (csw == NULL) { 9293236151eSAlexander Motin status = 0; /* unknown up to the end */ 9303236151eSAlexander Motin off = be_lun->size_bytes; 9313236151eSAlexander Motin goto done; 9323236151eSAlexander Motin } 9330bcd4ab6SAlexander Motin off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 9343236151eSAlexander Motin error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 9353236151eSAlexander Motin curthread); 936ef8daf3fSAlexander Motin if (error == 0 && off > roff) 937ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 938ef8daf3fSAlexander Motin else { 9393236151eSAlexander Motin error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 9403236151eSAlexander Motin curthread); 941ef8daf3fSAlexander Motin if (error == 0 && off > roff) 942ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 943ef8daf3fSAlexander Motin else { 944ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 945ef8daf3fSAlexander Motin off = be_lun->size_bytes; 946ef8daf3fSAlexander Motin } 947ef8daf3fSAlexander Motin } 9483236151eSAlexander Motin dev_relthread(dev, ref); 949ef8daf3fSAlexander Motin 9503236151eSAlexander Motin done: 951ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 952ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 9530bcd4ab6SAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 9540bcd4ab6SAlexander Motin lbalen->lba), data->descr[0].length); 955ef8daf3fSAlexander Motin data->descr[0].status = status; 956ef8daf3fSAlexander Motin 957ef8daf3fSAlexander Motin ctl_complete_beio(beio); 958ef8daf3fSAlexander Motin } 959ef8daf3fSAlexander Motin 960ef8daf3fSAlexander Motin static void 961130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 962130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 963130f4520SKenneth D. Merry { 964130f4520SKenneth D. Merry struct bio *bio; 965130f4520SKenneth D. Merry union ctl_io *io; 9663236151eSAlexander Motin struct cdevsw *csw; 9673236151eSAlexander Motin struct cdev *dev; 9683236151eSAlexander Motin int ref; 969130f4520SKenneth D. Merry 970130f4520SKenneth D. Merry io = beio->io; 971130f4520SKenneth D. Merry 972130f4520SKenneth D. Merry DPRINTF("entered\n"); 973130f4520SKenneth D. Merry 974130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 975130f4520SKenneth D. Merry bio = g_alloc_bio(); 976130f4520SKenneth D. Merry 977130f4520SKenneth D. Merry bio->bio_cmd = BIO_FLUSH; 978130f4520SKenneth D. Merry bio->bio_offset = 0; 979130f4520SKenneth D. Merry bio->bio_data = 0; 980130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 981130f4520SKenneth D. Merry bio->bio_caller1 = beio; 982130f4520SKenneth D. Merry bio->bio_pblkno = 0; 983130f4520SKenneth D. Merry 984130f4520SKenneth D. Merry /* 985130f4520SKenneth D. Merry * We don't need to acquire the LUN lock here, because we are only 986130f4520SKenneth D. Merry * sending one bio, and so there is no other context to synchronize 987130f4520SKenneth D. Merry * with. 988130f4520SKenneth D. Merry */ 989130f4520SKenneth D. Merry beio->num_bios_sent = 1; 990130f4520SKenneth D. Merry beio->send_complete = 1; 991130f4520SKenneth D. Merry 992130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 99375c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 994130f4520SKenneth D. Merry devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 99575c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 996130f4520SKenneth D. Merry 9973236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 9983236151eSAlexander Motin if (csw) { 9993236151eSAlexander Motin bio->bio_dev = dev; 10003236151eSAlexander Motin csw->d_strategy(bio); 10013236151eSAlexander Motin dev_relthread(dev, ref); 10023236151eSAlexander Motin } else { 10033236151eSAlexander Motin bio->bio_error = ENXIO; 10043236151eSAlexander Motin ctl_be_block_biodone(bio); 10053236151eSAlexander Motin } 1006130f4520SKenneth D. Merry } 1007130f4520SKenneth D. Merry 1008130f4520SKenneth D. Merry static void 1009ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1010ee7f31c0SAlexander Motin struct ctl_be_block_io *beio, 1011ee7f31c0SAlexander Motin uint64_t off, uint64_t len, int last) 1012ee7f31c0SAlexander Motin { 1013ee7f31c0SAlexander Motin struct bio *bio; 10148f5a226aSAlexander Motin uint64_t maxlen; 10153236151eSAlexander Motin struct cdevsw *csw; 10163236151eSAlexander Motin struct cdev *dev; 10173236151eSAlexander Motin int ref; 1018ee7f31c0SAlexander Motin 10193236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 10200bcd4ab6SAlexander Motin maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1021ee7f31c0SAlexander Motin while (len > 0) { 1022ee7f31c0SAlexander Motin bio = g_alloc_bio(); 1023ee7f31c0SAlexander Motin bio->bio_cmd = BIO_DELETE; 10243236151eSAlexander Motin bio->bio_dev = dev; 1025ee7f31c0SAlexander Motin bio->bio_offset = off; 10268f5a226aSAlexander Motin bio->bio_length = MIN(len, maxlen); 1027ee7f31c0SAlexander Motin bio->bio_data = 0; 1028ee7f31c0SAlexander Motin bio->bio_done = ctl_be_block_biodone; 1029ee7f31c0SAlexander Motin bio->bio_caller1 = beio; 10300bcd4ab6SAlexander Motin bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1031ee7f31c0SAlexander Motin 1032ee7f31c0SAlexander Motin off += bio->bio_length; 1033ee7f31c0SAlexander Motin len -= bio->bio_length; 1034ee7f31c0SAlexander Motin 103575c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1036ee7f31c0SAlexander Motin beio->num_bios_sent++; 1037ee7f31c0SAlexander Motin if (last && len == 0) 1038ee7f31c0SAlexander Motin beio->send_complete = 1; 103975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1040ee7f31c0SAlexander Motin 10413236151eSAlexander Motin if (csw) { 10423236151eSAlexander Motin csw->d_strategy(bio); 10433236151eSAlexander Motin } else { 10443236151eSAlexander Motin bio->bio_error = ENXIO; 10453236151eSAlexander Motin ctl_be_block_biodone(bio); 1046ee7f31c0SAlexander Motin } 1047ee7f31c0SAlexander Motin } 10483236151eSAlexander Motin if (csw) 10493236151eSAlexander Motin dev_relthread(dev, ref); 10503236151eSAlexander Motin } 1051ee7f31c0SAlexander Motin 1052ee7f31c0SAlexander Motin static void 1053ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1054ee7f31c0SAlexander Motin struct ctl_be_block_io *beio) 1055ee7f31c0SAlexander Motin { 1056ee7f31c0SAlexander Motin union ctl_io *io; 105766df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1058ee7f31c0SAlexander Motin struct scsi_unmap_desc *buf, *end; 1059ee7f31c0SAlexander Motin uint64_t len; 1060ee7f31c0SAlexander Motin 1061ee7f31c0SAlexander Motin io = beio->io; 1062ee7f31c0SAlexander Motin 1063ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1064ee7f31c0SAlexander Motin 1065ee7f31c0SAlexander Motin binuptime(&beio->ds_t0); 106675c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1067ee7f31c0SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 106875c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1069ee7f31c0SAlexander Motin 1070ee7f31c0SAlexander Motin if (beio->io_offset == -1) { 1071ee7f31c0SAlexander Motin beio->io_len = 0; 107266df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 107366df9136SAlexander Motin buf = (struct scsi_unmap_desc *)ptrlen->ptr; 107466df9136SAlexander Motin end = buf + ptrlen->len / sizeof(*buf); 1075ee7f31c0SAlexander Motin for (; buf < end; buf++) { 1076ee7f31c0SAlexander Motin len = (uint64_t)scsi_4btoul(buf->length) * 10770bcd4ab6SAlexander Motin be_lun->cbe_lun.blocksize; 1078ee7f31c0SAlexander Motin beio->io_len += len; 1079ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 10800bcd4ab6SAlexander Motin scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 10810bcd4ab6SAlexander Motin len, (end - buf < 2) ? TRUE : FALSE); 1082ee7f31c0SAlexander Motin } 1083ee7f31c0SAlexander Motin } else 1084ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 1085ee7f31c0SAlexander Motin beio->io_offset, beio->io_len, TRUE); 1086ee7f31c0SAlexander Motin } 1087ee7f31c0SAlexander Motin 1088ee7f31c0SAlexander Motin static void 1089130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1090130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 1091130f4520SKenneth D. Merry { 109275c7a1d3SAlexander Motin TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1093130f4520SKenneth D. Merry struct bio *bio; 10943236151eSAlexander Motin struct cdevsw *csw; 10953236151eSAlexander Motin struct cdev *dev; 1096130f4520SKenneth D. Merry off_t cur_offset; 10973236151eSAlexander Motin int i, max_iosize, ref; 1098130f4520SKenneth D. Merry 1099130f4520SKenneth D. Merry DPRINTF("entered\n"); 11003236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 1101130f4520SKenneth D. Merry 1102130f4520SKenneth D. Merry /* 1103130f4520SKenneth D. Merry * We have to limit our I/O size to the maximum supported by the 1104130f4520SKenneth D. Merry * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1105130f4520SKenneth D. Merry * set it properly, use DFLTPHYS. 1106130f4520SKenneth D. Merry */ 11073236151eSAlexander Motin if (csw) { 11083236151eSAlexander Motin max_iosize = dev->si_iosize_max; 1109130f4520SKenneth D. Merry if (max_iosize < PAGE_SIZE) 1110130f4520SKenneth D. Merry max_iosize = DFLTPHYS; 11113236151eSAlexander Motin } else 11123236151eSAlexander Motin max_iosize = DFLTPHYS; 1113130f4520SKenneth D. Merry 1114130f4520SKenneth D. Merry cur_offset = beio->io_offset; 1115130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 1116130f4520SKenneth D. Merry size_t cur_size; 1117130f4520SKenneth D. Merry uint8_t *cur_ptr; 1118130f4520SKenneth D. Merry 1119130f4520SKenneth D. Merry cur_size = beio->sg_segs[i].len; 1120130f4520SKenneth D. Merry cur_ptr = beio->sg_segs[i].addr; 1121130f4520SKenneth D. Merry 1122130f4520SKenneth D. Merry while (cur_size > 0) { 1123130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 1124130f4520SKenneth D. Merry bio = g_alloc_bio(); 1125130f4520SKenneth D. Merry 1126130f4520SKenneth D. Merry KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1127130f4520SKenneth D. Merry 1128130f4520SKenneth D. Merry bio->bio_cmd = beio->bio_cmd; 11293236151eSAlexander Motin bio->bio_dev = dev; 1130130f4520SKenneth D. Merry bio->bio_caller1 = beio; 1131130f4520SKenneth D. Merry bio->bio_length = min(cur_size, max_iosize); 1132130f4520SKenneth D. Merry bio->bio_offset = cur_offset; 1133130f4520SKenneth D. Merry bio->bio_data = cur_ptr; 1134130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 11350bcd4ab6SAlexander Motin bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1136130f4520SKenneth D. Merry 1137130f4520SKenneth D. Merry cur_offset += bio->bio_length; 1138130f4520SKenneth D. Merry cur_ptr += bio->bio_length; 1139130f4520SKenneth D. Merry cur_size -= bio->bio_length; 1140130f4520SKenneth D. Merry 114175c7a1d3SAlexander Motin TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1142130f4520SKenneth D. Merry beio->num_bios_sent++; 1143130f4520SKenneth D. Merry } 1144130f4520SKenneth D. Merry } 114575c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 114675c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 114775c7a1d3SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 114875c7a1d3SAlexander Motin beio->send_complete = 1; 114975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 115075c7a1d3SAlexander Motin 115175c7a1d3SAlexander Motin /* 115275c7a1d3SAlexander Motin * Fire off all allocated requests! 115375c7a1d3SAlexander Motin */ 115475c7a1d3SAlexander Motin while ((bio = TAILQ_FIRST(&queue)) != NULL) { 115575c7a1d3SAlexander Motin TAILQ_REMOVE(&queue, bio, bio_queue); 11563236151eSAlexander Motin if (csw) 11573236151eSAlexander Motin csw->d_strategy(bio); 11583236151eSAlexander Motin else { 11593236151eSAlexander Motin bio->bio_error = ENXIO; 11603236151eSAlexander Motin ctl_be_block_biodone(bio); 116175c7a1d3SAlexander Motin } 1162130f4520SKenneth D. Merry } 11633236151eSAlexander Motin if (csw) 11643236151eSAlexander Motin dev_relthread(dev, ref); 11653236151eSAlexander Motin } 1166130f4520SKenneth D. Merry 1167c3e7ba3eSAlexander Motin static uint64_t 1168c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1169c3e7ba3eSAlexander Motin { 1170c3e7ba3eSAlexander Motin struct diocgattr_arg arg; 11713236151eSAlexander Motin struct cdevsw *csw; 11723236151eSAlexander Motin struct cdev *dev; 11733236151eSAlexander Motin int error, ref; 1174c3e7ba3eSAlexander Motin 11753236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 11763236151eSAlexander Motin if (csw == NULL) 1177c3e7ba3eSAlexander Motin return (UINT64_MAX); 1178c3e7ba3eSAlexander Motin strlcpy(arg.name, attrname, sizeof(arg.name)); 1179c3e7ba3eSAlexander Motin arg.len = sizeof(arg.value.off); 11803236151eSAlexander Motin if (csw->d_ioctl) { 11813236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 11823236151eSAlexander Motin curthread); 11833236151eSAlexander Motin } else 11843236151eSAlexander Motin error = ENODEV; 11853236151eSAlexander Motin dev_relthread(dev, ref); 1186c3e7ba3eSAlexander Motin if (error != 0) 1187c3e7ba3eSAlexander Motin return (UINT64_MAX); 1188c3e7ba3eSAlexander Motin return (arg.value.off); 1189c3e7ba3eSAlexander Motin } 1190c3e7ba3eSAlexander Motin 1191130f4520SKenneth D. Merry static void 11927d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 11937d0d4342SAlexander Motin union ctl_io *io) 11947d0d4342SAlexander Motin { 11950bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 11967d0d4342SAlexander Motin struct ctl_be_block_io *beio; 11977d0d4342SAlexander Motin struct ctl_lba_len_flags *lbalen; 11987d0d4342SAlexander Motin 11997d0d4342SAlexander Motin DPRINTF("entered\n"); 12007d0d4342SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 12017d0d4342SAlexander Motin lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 12027d0d4342SAlexander Motin 12030bcd4ab6SAlexander Motin beio->io_len = lbalen->len * cbe_lun->blocksize; 12040bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 12057d0d4342SAlexander Motin beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 12067d0d4342SAlexander Motin beio->bio_cmd = BIO_FLUSH; 12077d0d4342SAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 12087d0d4342SAlexander Motin DPRINTF("SYNC\n"); 12097d0d4342SAlexander Motin be_lun->lun_flush(be_lun, beio); 12107d0d4342SAlexander Motin } 12117d0d4342SAlexander Motin 12127d0d4342SAlexander Motin static void 1213ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1214ee7f31c0SAlexander Motin { 1215ee7f31c0SAlexander Motin union ctl_io *io; 1216ee7f31c0SAlexander Motin 1217ee7f31c0SAlexander Motin io = beio->io; 1218ee7f31c0SAlexander Motin ctl_free_beio(beio); 1219ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1220ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1221ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1222ee7f31c0SAlexander Motin ctl_config_write_done(io); 1223ee7f31c0SAlexander Motin return; 1224ee7f31c0SAlexander Motin } 1225ee7f31c0SAlexander Motin 1226ee7f31c0SAlexander Motin ctl_be_block_config_write(io); 1227ee7f31c0SAlexander Motin } 1228ee7f31c0SAlexander Motin 1229ee7f31c0SAlexander Motin static void 1230ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1231ee7f31c0SAlexander Motin union ctl_io *io) 1232ee7f31c0SAlexander Motin { 12330bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1234ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 123566df9136SAlexander Motin struct ctl_lba_len_flags *lbalen; 1236fee04ef7SAlexander Motin uint64_t len_left, lba; 1237fee04ef7SAlexander Motin uint32_t pb, pbo, adj; 1238ee7f31c0SAlexander Motin int i, seglen; 1239ee7f31c0SAlexander Motin uint8_t *buf, *end; 1240ee7f31c0SAlexander Motin 1241ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1242ee7f31c0SAlexander Motin 1243e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 124411b569f7SAlexander Motin lbalen = ARGS(beio->io); 1245ee7f31c0SAlexander Motin 124664c5167cSAlexander Motin if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 12473406a2a0SAlexander Motin (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1248ee7f31c0SAlexander Motin ctl_free_beio(beio); 1249ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1250ee7f31c0SAlexander Motin /*sks_valid*/ 1, 1251ee7f31c0SAlexander Motin /*command*/ 1, 1252ee7f31c0SAlexander Motin /*field*/ 1, 1253ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1254ee7f31c0SAlexander Motin /*bit*/ 0); 1255ee7f31c0SAlexander Motin ctl_config_write_done(io); 1256ee7f31c0SAlexander Motin return; 1257ee7f31c0SAlexander Motin } 1258ee7f31c0SAlexander Motin 12593406a2a0SAlexander Motin if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 12600bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 12610bcd4ab6SAlexander Motin beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1262ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1263ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1264ee7f31c0SAlexander Motin 1265ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1266ee7f31c0SAlexander Motin return; 1267ee7f31c0SAlexander Motin } 1268ee7f31c0SAlexander Motin 1269ee7f31c0SAlexander Motin beio->bio_cmd = BIO_WRITE; 1270ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_WRITE; 1271ee7f31c0SAlexander Motin 1272ee7f31c0SAlexander Motin DPRINTF("WRITE SAME at LBA %jx len %u\n", 127366df9136SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len); 1274ee7f31c0SAlexander Motin 12750bcd4ab6SAlexander Motin pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 12760bcd4ab6SAlexander Motin if (be_lun->cbe_lun.pblockoff > 0) 12770bcd4ab6SAlexander Motin pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1278fee04ef7SAlexander Motin else 1279fee04ef7SAlexander Motin pbo = 0; 12800bcd4ab6SAlexander Motin len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1281ee7f31c0SAlexander Motin for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1282ee7f31c0SAlexander Motin 1283ee7f31c0SAlexander Motin /* 1284ee7f31c0SAlexander Motin * Setup the S/G entry for this chunk. 1285ee7f31c0SAlexander Motin */ 128608a7cce5SAlexander Motin seglen = MIN(CTLBLK_MAX_SEG, len_left); 12870bcd4ab6SAlexander Motin if (pb > cbe_lun->blocksize) { 12880bcd4ab6SAlexander Motin adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 128993b8c96cSAlexander Motin seglen - pbo) % pb; 129093b8c96cSAlexander Motin if (seglen > adj) 129193b8c96cSAlexander Motin seglen -= adj; 129293b8c96cSAlexander Motin else 12930bcd4ab6SAlexander Motin seglen -= seglen % cbe_lun->blocksize; 129493b8c96cSAlexander Motin } else 12950bcd4ab6SAlexander Motin seglen -= seglen % cbe_lun->blocksize; 1296ee7f31c0SAlexander Motin beio->sg_segs[i].len = seglen; 1297ee7f31c0SAlexander Motin beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1298ee7f31c0SAlexander Motin 1299ee7f31c0SAlexander Motin DPRINTF("segment %d addr %p len %zd\n", i, 1300ee7f31c0SAlexander Motin beio->sg_segs[i].addr, beio->sg_segs[i].len); 1301ee7f31c0SAlexander Motin 1302ee7f31c0SAlexander Motin beio->num_segs++; 1303ee7f31c0SAlexander Motin len_left -= seglen; 1304ee7f31c0SAlexander Motin 1305ee7f31c0SAlexander Motin buf = beio->sg_segs[i].addr; 1306ee7f31c0SAlexander Motin end = buf + seglen; 13070bcd4ab6SAlexander Motin for (; buf < end; buf += cbe_lun->blocksize) { 13080bcd4ab6SAlexander Motin memcpy(buf, io->scsiio.kern_data_ptr, cbe_lun->blocksize); 130966df9136SAlexander Motin if (lbalen->flags & SWS_LBDATA) 131066df9136SAlexander Motin scsi_ulto4b(lbalen->lba + lba, buf); 1311ee7f31c0SAlexander Motin lba++; 1312ee7f31c0SAlexander Motin } 1313ee7f31c0SAlexander Motin } 1314ee7f31c0SAlexander Motin 13150bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 13160bcd4ab6SAlexander Motin beio->io_len = lba * cbe_lun->blocksize; 1317ee7f31c0SAlexander Motin 1318ee7f31c0SAlexander Motin /* We can not do all in one run. Correct and schedule rerun. */ 1319ee7f31c0SAlexander Motin if (len_left > 0) { 132066df9136SAlexander Motin lbalen->lba += lba; 132166df9136SAlexander Motin lbalen->len -= lba; 1322ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done_ws; 1323ee7f31c0SAlexander Motin } 1324ee7f31c0SAlexander Motin 1325ee7f31c0SAlexander Motin be_lun->dispatch(be_lun, beio); 1326ee7f31c0SAlexander Motin } 1327ee7f31c0SAlexander Motin 1328ee7f31c0SAlexander Motin static void 1329ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1330ee7f31c0SAlexander Motin union ctl_io *io) 1331ee7f31c0SAlexander Motin { 1332ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 133366df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1334ee7f31c0SAlexander Motin 1335ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1336ee7f31c0SAlexander Motin 1337e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 133866df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1339ee7f31c0SAlexander Motin 13403406a2a0SAlexander Motin if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1341ee7f31c0SAlexander Motin ctl_free_beio(beio); 1342ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1343ee7f31c0SAlexander Motin /*sks_valid*/ 0, 1344ee7f31c0SAlexander Motin /*command*/ 1, 1345ee7f31c0SAlexander Motin /*field*/ 0, 1346ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1347ee7f31c0SAlexander Motin /*bit*/ 0); 1348ee7f31c0SAlexander Motin ctl_config_write_done(io); 1349ee7f31c0SAlexander Motin return; 1350ee7f31c0SAlexander Motin } 1351ee7f31c0SAlexander Motin 1352ee7f31c0SAlexander Motin beio->io_len = 0; 1353ee7f31c0SAlexander Motin beio->io_offset = -1; 1354ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1355ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 135666df9136SAlexander Motin DPRINTF("UNMAP\n"); 1357ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1358ee7f31c0SAlexander Motin } 1359ee7f31c0SAlexander Motin 1360ee7f31c0SAlexander Motin static void 1361ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio) 1362ef8daf3fSAlexander Motin { 1363ef8daf3fSAlexander Motin union ctl_io *io; 1364ef8daf3fSAlexander Motin 1365ef8daf3fSAlexander Motin io = beio->io; 1366ef8daf3fSAlexander Motin ctl_free_beio(beio); 1367ef8daf3fSAlexander Motin ctl_config_read_done(io); 1368ef8daf3fSAlexander Motin } 1369ef8daf3fSAlexander Motin 1370ef8daf3fSAlexander Motin static void 1371ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1372ef8daf3fSAlexander Motin union ctl_io *io) 1373ef8daf3fSAlexander Motin { 1374ef8daf3fSAlexander Motin struct ctl_be_block_io *beio; 1375ef8daf3fSAlexander Motin struct ctl_be_block_softc *softc; 1376ef8daf3fSAlexander Motin 1377ef8daf3fSAlexander Motin DPRINTF("entered\n"); 1378ef8daf3fSAlexander Motin 1379ef8daf3fSAlexander Motin softc = be_lun->softc; 1380ef8daf3fSAlexander Motin beio = ctl_alloc_beio(softc); 1381ef8daf3fSAlexander Motin beio->io = io; 1382ef8daf3fSAlexander Motin beio->lun = be_lun; 1383ef8daf3fSAlexander Motin beio->beio_cont = ctl_be_block_cr_done; 1384ef8daf3fSAlexander Motin PRIV(io)->ptr = (void *)beio; 1385ef8daf3fSAlexander Motin 1386ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 1387ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1388ef8daf3fSAlexander Motin beio->bio_cmd = -1; 1389ef8daf3fSAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 1390ef8daf3fSAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1391ef8daf3fSAlexander Motin beio->io_len = 0; 1392ef8daf3fSAlexander Motin if (be_lun->get_lba_status) 1393ef8daf3fSAlexander Motin be_lun->get_lba_status(be_lun, beio); 1394ef8daf3fSAlexander Motin else 1395ef8daf3fSAlexander Motin ctl_be_block_cr_done(beio); 1396ef8daf3fSAlexander Motin break; 1397ef8daf3fSAlexander Motin default: 1398ef8daf3fSAlexander Motin panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1399ef8daf3fSAlexander Motin break; 1400ef8daf3fSAlexander Motin } 1401ef8daf3fSAlexander Motin } 1402ef8daf3fSAlexander Motin 1403ef8daf3fSAlexander Motin static void 1404ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1405ee7f31c0SAlexander Motin { 1406ee7f31c0SAlexander Motin union ctl_io *io; 1407ee7f31c0SAlexander Motin 1408ee7f31c0SAlexander Motin io = beio->io; 1409ee7f31c0SAlexander Motin ctl_free_beio(beio); 1410ee7f31c0SAlexander Motin ctl_config_write_done(io); 1411ee7f31c0SAlexander Motin } 1412ee7f31c0SAlexander Motin 1413ee7f31c0SAlexander Motin static void 1414130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1415130f4520SKenneth D. Merry union ctl_io *io) 1416130f4520SKenneth D. Merry { 1417130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1418130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1419130f4520SKenneth D. Merry 1420130f4520SKenneth D. Merry DPRINTF("entered\n"); 1421130f4520SKenneth D. Merry 1422130f4520SKenneth D. Merry softc = be_lun->softc; 1423130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1424130f4520SKenneth D. Merry beio->io = io; 1425130f4520SKenneth D. Merry beio->lun = be_lun; 1426ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done; 14277d0d4342SAlexander Motin switch (io->scsiio.tag_type) { 14287d0d4342SAlexander Motin case CTL_TAG_ORDERED: 14297d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 14307d0d4342SAlexander Motin break; 14317d0d4342SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 14327d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 14337d0d4342SAlexander Motin break; 14347d0d4342SAlexander Motin case CTL_TAG_UNTAGGED: 14357d0d4342SAlexander Motin case CTL_TAG_SIMPLE: 14367d0d4342SAlexander Motin case CTL_TAG_ACA: 14377d0d4342SAlexander Motin default: 14387d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 14397d0d4342SAlexander Motin break; 14407d0d4342SAlexander Motin } 1441e86a4142SAlexander Motin PRIV(io)->ptr = (void *)beio; 1442130f4520SKenneth D. Merry 1443130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 1444130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 1445130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 14467d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(be_lun, io); 1447130f4520SKenneth D. Merry break; 1448ee7f31c0SAlexander Motin case WRITE_SAME_10: 1449ee7f31c0SAlexander Motin case WRITE_SAME_16: 1450ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(be_lun, io); 1451ee7f31c0SAlexander Motin break; 1452ee7f31c0SAlexander Motin case UNMAP: 1453ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(be_lun, io); 1454ee7f31c0SAlexander Motin break; 1455130f4520SKenneth D. Merry default: 1456130f4520SKenneth D. Merry panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1457130f4520SKenneth D. Merry break; 1458130f4520SKenneth D. Merry } 1459130f4520SKenneth D. Merry } 1460130f4520SKenneth D. Merry 1461d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1462d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1463d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1464d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1465130f4520SKenneth D. Merry 1466130f4520SKenneth D. Merry static void 146708a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio) 146808a7cce5SAlexander Motin { 146908a7cce5SAlexander Motin struct ctl_be_block_lun *be_lun; 147008a7cce5SAlexander Motin union ctl_io *io; 147108a7cce5SAlexander Motin 147208a7cce5SAlexander Motin io = beio->io; 147308a7cce5SAlexander Motin be_lun = beio->lun; 147408a7cce5SAlexander Motin ctl_free_beio(beio); 1475ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1476ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1477ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 147811b569f7SAlexander Motin ctl_data_submit_done(io); 147908a7cce5SAlexander Motin return; 148008a7cce5SAlexander Motin } 148108a7cce5SAlexander Motin 148208a7cce5SAlexander Motin io->io_hdr.status &= ~CTL_STATUS_MASK; 148308a7cce5SAlexander Motin io->io_hdr.status |= CTL_STATUS_NONE; 148408a7cce5SAlexander Motin 148575c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 148608a7cce5SAlexander Motin /* 148708a7cce5SAlexander Motin * XXX KDM make sure that links is okay to use at this point. 148808a7cce5SAlexander Motin * Otherwise, we either need to add another field to ctl_io_hdr, 148908a7cce5SAlexander Motin * or deal with resource allocation here. 149008a7cce5SAlexander Motin */ 149108a7cce5SAlexander Motin STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 149275c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 149308a7cce5SAlexander Motin 149408a7cce5SAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 149508a7cce5SAlexander Motin } 149608a7cce5SAlexander Motin 149708a7cce5SAlexander Motin static void 1498130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1499130f4520SKenneth D. Merry union ctl_io *io) 1500130f4520SKenneth D. Merry { 15010bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1502130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1503130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 150411b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 1505e86a4142SAlexander Motin struct ctl_ptr_len_flags *bptrlen; 1506e86a4142SAlexander Motin uint64_t len_left, lbas; 1507130f4520SKenneth D. Merry int i; 1508130f4520SKenneth D. Merry 1509130f4520SKenneth D. Merry softc = be_lun->softc; 1510130f4520SKenneth D. Merry 1511130f4520SKenneth D. Merry DPRINTF("entered\n"); 1512130f4520SKenneth D. Merry 151311b569f7SAlexander Motin lbalen = ARGS(io); 151411b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1515130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 151611b569f7SAlexander Motin } else { 151711b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1518130f4520SKenneth D. Merry } 1519130f4520SKenneth D. Merry 1520130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1521130f4520SKenneth D. Merry beio->io = io; 1522130f4520SKenneth D. Merry beio->lun = be_lun; 1523e86a4142SAlexander Motin bptrlen = PRIV(io); 1524e86a4142SAlexander Motin bptrlen->ptr = (void *)beio; 1525130f4520SKenneth D. Merry 1526130f4520SKenneth D. Merry switch (io->scsiio.tag_type) { 1527130f4520SKenneth D. Merry case CTL_TAG_ORDERED: 1528130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1529130f4520SKenneth D. Merry break; 1530130f4520SKenneth D. Merry case CTL_TAG_HEAD_OF_QUEUE: 1531130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1532130f4520SKenneth D. Merry break; 1533130f4520SKenneth D. Merry case CTL_TAG_UNTAGGED: 1534130f4520SKenneth D. Merry case CTL_TAG_SIMPLE: 1535130f4520SKenneth D. Merry case CTL_TAG_ACA: 1536130f4520SKenneth D. Merry default: 1537130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1538130f4520SKenneth D. Merry break; 1539130f4520SKenneth D. Merry } 1540130f4520SKenneth D. Merry 154111b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1542130f4520SKenneth D. Merry beio->bio_cmd = BIO_WRITE; 1543130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_WRITE; 154411b569f7SAlexander Motin } else { 154511b569f7SAlexander Motin beio->bio_cmd = BIO_READ; 154611b569f7SAlexander Motin beio->ds_trans_type = DEVSTAT_READ; 1547130f4520SKenneth D. Merry } 1548130f4520SKenneth D. Merry 154908a7cce5SAlexander Motin DPRINTF("%s at LBA %jx len %u @%ju\n", 1550130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1551e86a4142SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 155211b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 155311b569f7SAlexander Motin lbas = CTLBLK_HALF_IO_SIZE; 155411b569f7SAlexander Motin else 155511b569f7SAlexander Motin lbas = CTLBLK_MAX_IO_SIZE; 15560bcd4ab6SAlexander Motin lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 15570bcd4ab6SAlexander Motin beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 15580bcd4ab6SAlexander Motin beio->io_len = lbas * cbe_lun->blocksize; 1559e86a4142SAlexander Motin bptrlen->len += lbas; 1560130f4520SKenneth D. Merry 156108a7cce5SAlexander Motin for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 156208a7cce5SAlexander Motin KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 156308a7cce5SAlexander Motin i, CTLBLK_MAX_SEGS)); 1564130f4520SKenneth D. Merry 1565130f4520SKenneth D. Merry /* 1566130f4520SKenneth D. Merry * Setup the S/G entry for this chunk. 1567130f4520SKenneth D. Merry */ 156808a7cce5SAlexander Motin beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1569130f4520SKenneth D. Merry beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1570130f4520SKenneth D. Merry 1571130f4520SKenneth D. Merry DPRINTF("segment %d addr %p len %zd\n", i, 1572130f4520SKenneth D. Merry beio->sg_segs[i].addr, beio->sg_segs[i].len); 1573130f4520SKenneth D. Merry 157411b569f7SAlexander Motin /* Set up second segment for compare operation. */ 157511b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) { 157611b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 157711b569f7SAlexander Motin beio->sg_segs[i].len; 157811b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 157911b569f7SAlexander Motin uma_zalloc(be_lun->lun_zone, M_WAITOK); 158011b569f7SAlexander Motin } 158111b569f7SAlexander Motin 1582130f4520SKenneth D. Merry beio->num_segs++; 1583130f4520SKenneth D. Merry len_left -= beio->sg_segs[i].len; 1584130f4520SKenneth D. Merry } 1585e86a4142SAlexander Motin if (bptrlen->len < lbalen->len) 158608a7cce5SAlexander Motin beio->beio_cont = ctl_be_block_next; 158708a7cce5SAlexander Motin io->scsiio.be_move_done = ctl_be_block_move_done; 158811b569f7SAlexander Motin /* For compare we have separate S/G lists for read and datamove. */ 158911b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 159011b569f7SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 159111b569f7SAlexander Motin else 159208a7cce5SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 159308a7cce5SAlexander Motin io->scsiio.kern_data_len = beio->io_len; 159408a7cce5SAlexander Motin io->scsiio.kern_data_resid = 0; 159508a7cce5SAlexander Motin io->scsiio.kern_sg_entries = beio->num_segs; 159608a7cce5SAlexander Motin io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1597130f4520SKenneth D. Merry 1598130f4520SKenneth D. Merry /* 1599130f4520SKenneth D. Merry * For the read case, we need to read the data into our buffers and 1600130f4520SKenneth D. Merry * then we can send it back to the user. For the write case, we 1601130f4520SKenneth D. Merry * need to get the data from the user first. 1602130f4520SKenneth D. Merry */ 1603130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 1604130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1605130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1606130f4520SKenneth D. Merry } else { 1607130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1608130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 1609130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 1610130f4520SKenneth D. Merry #endif 1611130f4520SKenneth D. Merry ctl_datamove(io); 1612130f4520SKenneth D. Merry } 1613130f4520SKenneth D. Merry } 1614130f4520SKenneth D. Merry 1615130f4520SKenneth D. Merry static void 1616130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending) 1617130f4520SKenneth D. Merry { 1618*ee4ad294SAlexander Motin struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1619*ee4ad294SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1620130f4520SKenneth D. Merry union ctl_io *io; 1621130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1622130f4520SKenneth D. Merry 1623*ee4ad294SAlexander Motin DPRINTF("entered\n"); 1624*ee4ad294SAlexander Motin /* 1625*ee4ad294SAlexander Motin * Fetch and process I/Os from all queues. If we detect LUN 1626*ee4ad294SAlexander Motin * CTL_LUN_FLAG_OFFLINE status here -- it is result of a race, 1627*ee4ad294SAlexander Motin * so make response maximally opaque to not confuse initiator. 1628*ee4ad294SAlexander Motin */ 1629*ee4ad294SAlexander Motin for (;;) { 1630*ee4ad294SAlexander Motin mtx_lock(&be_lun->queue_lock); 1631*ee4ad294SAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1632*ee4ad294SAlexander Motin if (io != NULL) { 1633130f4520SKenneth D. Merry DPRINTF("datamove queue\n"); 1634130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1635130f4520SKenneth D. Merry ctl_io_hdr, links); 163675c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1637e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1638*ee4ad294SAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1639*ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1640*ee4ad294SAlexander Motin ctl_complete_beio(beio); 1641*ee4ad294SAlexander Motin return; 1642*ee4ad294SAlexander Motin } 1643130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1644130f4520SKenneth D. Merry continue; 1645130f4520SKenneth D. Merry } 1646130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1647130f4520SKenneth D. Merry if (io != NULL) { 1648130f4520SKenneth D. Merry DPRINTF("config write queue\n"); 1649130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1650130f4520SKenneth D. Merry ctl_io_hdr, links); 165175c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1652*ee4ad294SAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1653*ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1654*ee4ad294SAlexander Motin ctl_config_write_done(io); 1655*ee4ad294SAlexander Motin return; 1656*ee4ad294SAlexander Motin } 1657130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(be_lun, io); 1658ef8daf3fSAlexander Motin continue; 1659ef8daf3fSAlexander Motin } 1660ef8daf3fSAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1661ef8daf3fSAlexander Motin if (io != NULL) { 1662ef8daf3fSAlexander Motin DPRINTF("config read queue\n"); 1663ef8daf3fSAlexander Motin STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1664ef8daf3fSAlexander Motin ctl_io_hdr, links); 1665ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 1666*ee4ad294SAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1667*ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1668*ee4ad294SAlexander Motin ctl_config_read_done(io); 1669*ee4ad294SAlexander Motin return; 1670*ee4ad294SAlexander Motin } 1671ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(be_lun, io); 1672130f4520SKenneth D. Merry continue; 1673130f4520SKenneth D. Merry } 1674130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1675130f4520SKenneth D. Merry if (io != NULL) { 1676130f4520SKenneth D. Merry DPRINTF("input queue\n"); 1677130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1678130f4520SKenneth D. Merry ctl_io_hdr, links); 167975c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1680*ee4ad294SAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 1681*ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1682*ee4ad294SAlexander Motin ctl_data_submit_done(io); 1683*ee4ad294SAlexander Motin return; 1684*ee4ad294SAlexander Motin } 1685130f4520SKenneth D. Merry ctl_be_block_dispatch(be_lun, io); 1686130f4520SKenneth D. Merry continue; 1687130f4520SKenneth D. Merry } 1688130f4520SKenneth D. Merry 1689130f4520SKenneth D. Merry /* 1690130f4520SKenneth D. Merry * If we get here, there is no work left in the queues, so 1691130f4520SKenneth D. Merry * just break out and let the task queue go to sleep. 1692130f4520SKenneth D. Merry */ 1693*ee4ad294SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1694130f4520SKenneth D. Merry break; 1695130f4520SKenneth D. Merry } 1696130f4520SKenneth D. Merry } 1697130f4520SKenneth D. Merry 1698130f4520SKenneth D. Merry /* 1699130f4520SKenneth D. Merry * Entry point from CTL to the backend for I/O. We queue everything to a 1700130f4520SKenneth D. Merry * work thread, so this just puts the I/O on a queue and wakes up the 1701130f4520SKenneth D. Merry * thread. 1702130f4520SKenneth D. Merry */ 1703130f4520SKenneth D. Merry static int 1704130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io) 1705130f4520SKenneth D. Merry { 1706130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 17070bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 1708130f4520SKenneth D. Merry 1709130f4520SKenneth D. Merry DPRINTF("entered\n"); 1710130f4520SKenneth D. Merry 17110bcd4ab6SAlexander Motin cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1712130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 17130bcd4ab6SAlexander Motin be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1714130f4520SKenneth D. Merry 1715130f4520SKenneth D. Merry /* 1716130f4520SKenneth D. Merry * Make sure we only get SCSI I/O. 1717130f4520SKenneth D. Merry */ 1718130f4520SKenneth D. Merry KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1719130f4520SKenneth D. Merry "%#x) encountered", io->io_hdr.io_type)); 1720130f4520SKenneth D. Merry 1721e86a4142SAlexander Motin PRIV(io)->len = 0; 1722e86a4142SAlexander Motin 172375c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1724130f4520SKenneth D. Merry /* 1725130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 1726130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 1727130f4520SKenneth D. Merry * or deal with resource allocation here. 1728130f4520SKenneth D. Merry */ 1729130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 173075c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1731130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1732130f4520SKenneth D. Merry 17339c71cd5aSAlexander Motin return (CTL_RETVAL_COMPLETE); 1734130f4520SKenneth D. Merry } 1735130f4520SKenneth D. Merry 1736130f4520SKenneth D. Merry static int 1737130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1738130f4520SKenneth D. Merry int flag, struct thread *td) 1739130f4520SKenneth D. Merry { 1740130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1741130f4520SKenneth D. Merry int error; 1742130f4520SKenneth D. Merry 1743130f4520SKenneth D. Merry softc = &backend_block_softc; 1744130f4520SKenneth D. Merry 1745130f4520SKenneth D. Merry error = 0; 1746130f4520SKenneth D. Merry 1747130f4520SKenneth D. Merry switch (cmd) { 1748130f4520SKenneth D. Merry case CTL_LUN_REQ: { 1749130f4520SKenneth D. Merry struct ctl_lun_req *lun_req; 1750130f4520SKenneth D. Merry 1751130f4520SKenneth D. Merry lun_req = (struct ctl_lun_req *)addr; 1752130f4520SKenneth D. Merry 1753130f4520SKenneth D. Merry switch (lun_req->reqtype) { 1754130f4520SKenneth D. Merry case CTL_LUNREQ_CREATE: 1755130f4520SKenneth D. Merry error = ctl_be_block_create(softc, lun_req); 1756130f4520SKenneth D. Merry break; 1757130f4520SKenneth D. Merry case CTL_LUNREQ_RM: 1758130f4520SKenneth D. Merry error = ctl_be_block_rm(softc, lun_req); 1759130f4520SKenneth D. Merry break; 176081177295SEdward Tomasz Napierala case CTL_LUNREQ_MODIFY: 176181177295SEdward Tomasz Napierala error = ctl_be_block_modify(softc, lun_req); 176281177295SEdward Tomasz Napierala break; 1763130f4520SKenneth D. Merry default: 1764130f4520SKenneth D. Merry lun_req->status = CTL_LUN_ERROR; 1765130f4520SKenneth D. Merry snprintf(lun_req->error_str, sizeof(lun_req->error_str), 176619720f41SAlexander Motin "invalid LUN request type %d", 1767130f4520SKenneth D. Merry lun_req->reqtype); 1768130f4520SKenneth D. Merry break; 1769130f4520SKenneth D. Merry } 1770130f4520SKenneth D. Merry break; 1771130f4520SKenneth D. Merry } 1772130f4520SKenneth D. Merry default: 1773130f4520SKenneth D. Merry error = ENOTTY; 1774130f4520SKenneth D. Merry break; 1775130f4520SKenneth D. Merry } 1776130f4520SKenneth D. Merry 1777130f4520SKenneth D. Merry return (error); 1778130f4520SKenneth D. Merry } 1779130f4520SKenneth D. Merry 1780130f4520SKenneth D. Merry static int 1781130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1782130f4520SKenneth D. Merry { 17830bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 1784130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 1785130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 178634961f40SAlexander Motin char *value; 1787130f4520SKenneth D. Merry struct vattr vattr; 178834961f40SAlexander Motin off_t ps, pss, po, pos, us, uss, uo, uos; 1789130f4520SKenneth D. Merry int error; 1790130f4520SKenneth D. Merry 1791130f4520SKenneth D. Merry error = 0; 17920bcd4ab6SAlexander Motin cbe_lun = &be_lun->cbe_lun; 1793130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 179419720f41SAlexander Motin params = &be_lun->params; 1795130f4520SKenneth D. Merry 1796130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_FILE; 1797130f4520SKenneth D. Merry be_lun->dispatch = ctl_be_block_dispatch_file; 1798130f4520SKenneth D. Merry be_lun->lun_flush = ctl_be_block_flush_file; 1799ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_file; 180053c146deSAlexander Motin be_lun->getattr = ctl_be_block_getattr_file; 18010bcd4ab6SAlexander Motin be_lun->unmap = NULL; 18020bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1803130f4520SKenneth D. Merry 1804130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1805130f4520SKenneth D. Merry if (error != 0) { 1806130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1807130f4520SKenneth D. Merry "error calling VOP_GETATTR() for file %s", 1808130f4520SKenneth D. Merry be_lun->dev_path); 1809130f4520SKenneth D. Merry return (error); 1810130f4520SKenneth D. Merry } 1811130f4520SKenneth D. Merry 1812130f4520SKenneth D. Merry /* 1813130f4520SKenneth D. Merry * Verify that we have the ability to upgrade to exclusive 1814130f4520SKenneth D. Merry * access on this file so we can trap errors at open instead 1815130f4520SKenneth D. Merry * of reporting them during first access. 1816130f4520SKenneth D. Merry */ 1817130f4520SKenneth D. Merry if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1818130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1819130f4520SKenneth D. Merry if (be_lun->vn->v_iflag & VI_DOOMED) { 1820130f4520SKenneth D. Merry error = EBADF; 1821130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1822130f4520SKenneth D. Merry "error locking file %s", be_lun->dev_path); 1823130f4520SKenneth D. Merry return (error); 1824130f4520SKenneth D. Merry } 1825130f4520SKenneth D. Merry } 1826130f4520SKenneth D. Merry 1827130f4520SKenneth D. Merry file_data->cred = crhold(curthread->td_ucred); 182881177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) 182981177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 183081177295SEdward Tomasz Napierala else 1831130f4520SKenneth D. Merry be_lun->size_bytes = vattr.va_size; 1832130f4520SKenneth D. Merry 1833130f4520SKenneth D. Merry /* 183420a28d6cSAlexander Motin * For files we can use any logical block size. Prefer 512 bytes 183520a28d6cSAlexander Motin * for compatibility reasons. If file's vattr.va_blocksize 183620a28d6cSAlexander Motin * (preferred I/O block size) is bigger and multiple to chosen 183720a28d6cSAlexander Motin * logical block size -- report it as physical block size. 1838130f4520SKenneth D. Merry */ 1839130f4520SKenneth D. Merry if (params->blocksize_bytes != 0) 18400bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 1841130f4520SKenneth D. Merry else 18420bcd4ab6SAlexander Motin cbe_lun->blocksize = 512; 18430bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 18440bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 18450bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 184634961f40SAlexander Motin 184734961f40SAlexander Motin us = ps = vattr.va_blocksize; 184834961f40SAlexander Motin uo = po = 0; 184934961f40SAlexander Motin 18500bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 185134961f40SAlexander Motin if (value != NULL) 185234961f40SAlexander Motin ctl_expand_number(value, &ps); 18530bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 185434961f40SAlexander Motin if (value != NULL) 185534961f40SAlexander Motin ctl_expand_number(value, &po); 18560bcd4ab6SAlexander Motin pss = ps / cbe_lun->blocksize; 18570bcd4ab6SAlexander Motin pos = po / cbe_lun->blocksize; 18580bcd4ab6SAlexander Motin if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 18590bcd4ab6SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 18600bcd4ab6SAlexander Motin cbe_lun->pblockexp = fls(pss) - 1; 18610bcd4ab6SAlexander Motin cbe_lun->pblockoff = (pss - pos) % pss; 186234961f40SAlexander Motin } 186334961f40SAlexander Motin 18640bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 186534961f40SAlexander Motin if (value != NULL) 186634961f40SAlexander Motin ctl_expand_number(value, &us); 18670bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 186834961f40SAlexander Motin if (value != NULL) 186934961f40SAlexander Motin ctl_expand_number(value, &uo); 18700bcd4ab6SAlexander Motin uss = us / cbe_lun->blocksize; 18710bcd4ab6SAlexander Motin uos = uo / cbe_lun->blocksize; 18720bcd4ab6SAlexander Motin if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 18730bcd4ab6SAlexander Motin ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 18740bcd4ab6SAlexander Motin cbe_lun->ublockexp = fls(uss) - 1; 18750bcd4ab6SAlexander Motin cbe_lun->ublockoff = (uss - uos) % uss; 187620a28d6cSAlexander Motin } 1877130f4520SKenneth D. Merry 1878130f4520SKenneth D. Merry /* 1879130f4520SKenneth D. Merry * Sanity check. The media size has to be at least one 1880130f4520SKenneth D. Merry * sector long. 1881130f4520SKenneth D. Merry */ 18820bcd4ab6SAlexander Motin if (be_lun->size_bytes < cbe_lun->blocksize) { 1883130f4520SKenneth D. Merry error = EINVAL; 1884130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1885130f4520SKenneth D. Merry "file %s size %ju < block size %u", be_lun->dev_path, 18860bcd4ab6SAlexander Motin (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1887130f4520SKenneth D. Merry } 1888cb8727e2SAlexander Motin 18890bcd4ab6SAlexander Motin cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1890130f4520SKenneth D. Merry return (error); 1891130f4520SKenneth D. Merry } 1892130f4520SKenneth D. Merry 1893130f4520SKenneth D. Merry static int 1894130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1895130f4520SKenneth D. Merry { 18960bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1897130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 18983236151eSAlexander Motin struct cdevsw *csw; 1899130f4520SKenneth D. Merry struct cdev *dev; 190034961f40SAlexander Motin char *value; 19013236151eSAlexander Motin int error, atomic, maxio, ref, unmap, tmp; 1902f6295033SAlexander Motin off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1903130f4520SKenneth D. Merry 190419720f41SAlexander Motin params = &be_lun->params; 1905130f4520SKenneth D. Merry 1906130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_DEV; 19073236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 19083236151eSAlexander Motin if (csw == NULL) 19093236151eSAlexander Motin return (ENXIO); 19103236151eSAlexander Motin if (strcmp(csw->d_name, "zvol") == 0) { 191167f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_zvol; 1912ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_zvol; 1913cb8727e2SAlexander Motin atomic = maxio = CTLBLK_MAX_IO_SIZE; 1914cb8727e2SAlexander Motin } else { 191567f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_dev; 19160bcd4ab6SAlexander Motin be_lun->get_lba_status = NULL; 1917cb8727e2SAlexander Motin atomic = 0; 19183236151eSAlexander Motin maxio = dev->si_iosize_max; 1919cb8727e2SAlexander Motin if (maxio <= 0) 1920cb8727e2SAlexander Motin maxio = DFLTPHYS; 1921cb8727e2SAlexander Motin if (maxio > CTLBLK_MAX_IO_SIZE) 1922cb8727e2SAlexander Motin maxio = CTLBLK_MAX_IO_SIZE; 1923cb8727e2SAlexander Motin } 192467f586a8SAlexander Motin be_lun->lun_flush = ctl_be_block_flush_dev; 1925c3e7ba3eSAlexander Motin be_lun->getattr = ctl_be_block_getattr_dev; 19260bcd4ab6SAlexander Motin be_lun->unmap = ctl_be_block_unmap_dev; 1927130f4520SKenneth D. Merry 19283236151eSAlexander Motin if (!csw->d_ioctl) { 19293236151eSAlexander Motin dev_relthread(dev, ref); 1930130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 19313236151eSAlexander Motin "no d_ioctl for device %s!", be_lun->dev_path); 1932130f4520SKenneth D. Merry return (ENODEV); 1933130f4520SKenneth D. Merry } 1934130f4520SKenneth D. Merry 19353236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1936130f4520SKenneth D. Merry curthread); 1937130f4520SKenneth D. Merry if (error) { 19383236151eSAlexander Motin dev_relthread(dev, ref); 1939130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 194019720f41SAlexander Motin "error %d returned for DIOCGSECTORSIZE ioctl " 194119720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 1942130f4520SKenneth D. Merry return (error); 1943130f4520SKenneth D. Merry } 1944130f4520SKenneth D. Merry 1945130f4520SKenneth D. Merry /* 1946130f4520SKenneth D. Merry * If the user has asked for a blocksize that is greater than the 1947130f4520SKenneth D. Merry * backing device's blocksize, we can do it only if the blocksize 1948130f4520SKenneth D. Merry * the user is asking for is an even multiple of the underlying 1949130f4520SKenneth D. Merry * device's blocksize. 1950130f4520SKenneth D. Merry */ 1951a15bbf15SAlexander Motin if ((params->blocksize_bytes != 0) && 1952a15bbf15SAlexander Motin (params->blocksize_bytes >= tmp)) { 1953a15bbf15SAlexander Motin if (params->blocksize_bytes % tmp == 0) { 19540bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 1955130f4520SKenneth D. Merry } else { 19563236151eSAlexander Motin dev_relthread(dev, ref); 1957130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 195819720f41SAlexander Motin "requested blocksize %u is not an even " 1959130f4520SKenneth D. Merry "multiple of backing device blocksize %u", 1960f6295033SAlexander Motin params->blocksize_bytes, tmp); 1961130f4520SKenneth D. Merry return (EINVAL); 1962130f4520SKenneth D. Merry } 1963a15bbf15SAlexander Motin } else if (params->blocksize_bytes != 0) { 19643236151eSAlexander Motin dev_relthread(dev, ref); 1965130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 196619720f41SAlexander Motin "requested blocksize %u < backing device " 1967f6295033SAlexander Motin "blocksize %u", params->blocksize_bytes, tmp); 1968130f4520SKenneth D. Merry return (EINVAL); 1969a15bbf15SAlexander Motin } else 19700bcd4ab6SAlexander Motin cbe_lun->blocksize = tmp; 1971130f4520SKenneth D. Merry 19723236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1973130f4520SKenneth D. Merry curthread); 1974130f4520SKenneth D. Merry if (error) { 19753236151eSAlexander Motin dev_relthread(dev, ref); 1976130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 197719720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE " 197819720f41SAlexander Motin " ioctl on %s!", error, 197981177295SEdward Tomasz Napierala be_lun->dev_path); 1980130f4520SKenneth D. Merry return (error); 1981130f4520SKenneth D. Merry } 1982130f4520SKenneth D. Merry 198381177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 1984f6295033SAlexander Motin if (params->lun_size_bytes > otmp) { 19853236151eSAlexander Motin dev_relthread(dev, ref); 198681177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 198719720f41SAlexander Motin "requested LUN size %ju > backing device " 198819720f41SAlexander Motin "size %ju", 198981177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 1990f6295033SAlexander Motin (uintmax_t)otmp); 199181177295SEdward Tomasz Napierala return (EINVAL); 1992130f4520SKenneth D. Merry } 1993130f4520SKenneth D. Merry 199481177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 1995a15bbf15SAlexander Motin } else 1996f6295033SAlexander Motin be_lun->size_bytes = otmp; 19970bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 19980bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 19990bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 200081177295SEdward Tomasz Napierala 20013236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 20023236151eSAlexander Motin curthread); 2003f6012722SAlexander Motin if (error) 2004f6012722SAlexander Motin ps = po = 0; 2005f6012722SAlexander Motin else { 20063236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 20073236151eSAlexander Motin FREAD, curthread); 2008f6012722SAlexander Motin if (error) 2009f6012722SAlexander Motin po = 0; 2010f6012722SAlexander Motin } 201134961f40SAlexander Motin us = ps; 201234961f40SAlexander Motin uo = po; 201334961f40SAlexander Motin 20140bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 201534961f40SAlexander Motin if (value != NULL) 201634961f40SAlexander Motin ctl_expand_number(value, &ps); 20170bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 201834961f40SAlexander Motin if (value != NULL) 201934961f40SAlexander Motin ctl_expand_number(value, &po); 20200bcd4ab6SAlexander Motin pss = ps / cbe_lun->blocksize; 20210bcd4ab6SAlexander Motin pos = po / cbe_lun->blocksize; 20220bcd4ab6SAlexander Motin if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 20230bcd4ab6SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 20240bcd4ab6SAlexander Motin cbe_lun->pblockexp = fls(pss) - 1; 20250bcd4ab6SAlexander Motin cbe_lun->pblockoff = (pss - pos) % pss; 2026f6012722SAlexander Motin } 2027f6012722SAlexander Motin 20280bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 202934961f40SAlexander Motin if (value != NULL) 203034961f40SAlexander Motin ctl_expand_number(value, &us); 20310bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 203234961f40SAlexander Motin if (value != NULL) 203334961f40SAlexander Motin ctl_expand_number(value, &uo); 20340bcd4ab6SAlexander Motin uss = us / cbe_lun->blocksize; 20350bcd4ab6SAlexander Motin uos = uo / cbe_lun->blocksize; 20360bcd4ab6SAlexander Motin if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 20370bcd4ab6SAlexander Motin ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 20380bcd4ab6SAlexander Motin cbe_lun->ublockexp = fls(uss) - 1; 20390bcd4ab6SAlexander Motin cbe_lun->ublockoff = (uss - uos) % uss; 204034961f40SAlexander Motin } 204134961f40SAlexander Motin 20420bcd4ab6SAlexander Motin cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 20430bcd4ab6SAlexander Motin cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2044fbc8d4ffSAlexander Motin 2045fbc8d4ffSAlexander Motin if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2046fbc8d4ffSAlexander Motin unmap = 1; 2047fbc8d4ffSAlexander Motin } else { 2048fbc8d4ffSAlexander Motin struct diocgattr_arg arg; 2049fbc8d4ffSAlexander Motin 2050fbc8d4ffSAlexander Motin strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2051fbc8d4ffSAlexander Motin arg.len = sizeof(arg.value.i); 20523236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 20533236151eSAlexander Motin curthread); 2054fbc8d4ffSAlexander Motin unmap = (error == 0) ? arg.value.i : 0; 2055fbc8d4ffSAlexander Motin } 20560bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "unmap"); 2057fbc8d4ffSAlexander Motin if (value != NULL) 2058fbc8d4ffSAlexander Motin unmap = (strcmp(value, "on") == 0); 2059fbc8d4ffSAlexander Motin if (unmap) 20600bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 20610bcd4ab6SAlexander Motin else 20620bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2063fbc8d4ffSAlexander Motin 20643236151eSAlexander Motin dev_relthread(dev, ref); 206581177295SEdward Tomasz Napierala return (0); 206681177295SEdward Tomasz Napierala } 2067130f4520SKenneth D. Merry 2068130f4520SKenneth D. Merry static int 2069130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun) 2070130f4520SKenneth D. Merry { 20710bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 20720bcd4ab6SAlexander Motin int flags; 2073130f4520SKenneth D. Merry 20740bcd4ab6SAlexander Motin if (be_lun->vn) { 20750bcd4ab6SAlexander Motin flags = FREAD; 20760bcd4ab6SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 20770bcd4ab6SAlexander Motin flags |= FWRITE; 2078130f4520SKenneth D. Merry (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2079130f4520SKenneth D. Merry be_lun->vn = NULL; 2080130f4520SKenneth D. Merry 2081130f4520SKenneth D. Merry switch (be_lun->dev_type) { 2082130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 2083130f4520SKenneth D. Merry break; 2084130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 2085130f4520SKenneth D. Merry if (be_lun->backend.file.cred != NULL) { 2086130f4520SKenneth D. Merry crfree(be_lun->backend.file.cred); 2087130f4520SKenneth D. Merry be_lun->backend.file.cred = NULL; 2088130f4520SKenneth D. Merry } 2089130f4520SKenneth D. Merry break; 2090130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 2091025a2301SEdward Tomasz Napierala break; 2092130f4520SKenneth D. Merry default: 2093130f4520SKenneth D. Merry panic("Unexpected backend type."); 2094130f4520SKenneth D. Merry break; 2095130f4520SKenneth D. Merry } 209619720f41SAlexander Motin be_lun->dev_type = CTL_BE_BLOCK_NONE; 2097130f4520SKenneth D. Merry } 2098130f4520SKenneth D. Merry return (0); 2099130f4520SKenneth D. Merry } 2100130f4520SKenneth D. Merry 2101130f4520SKenneth D. Merry static int 2102130f4520SKenneth D. Merry ctl_be_block_open(struct ctl_be_block_softc *softc, 2103130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2104130f4520SKenneth D. Merry { 21050bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2106130f4520SKenneth D. Merry struct nameidata nd; 21070bcd4ab6SAlexander Motin char *value; 21080bcd4ab6SAlexander Motin int error, flags; 2109130f4520SKenneth D. Merry 2110130f4520SKenneth D. Merry error = 0; 2111130f4520SKenneth D. Merry if (rootvnode == NULL) { 2112130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 211319720f41SAlexander Motin "Root filesystem is not mounted"); 2114130f4520SKenneth D. Merry return (1); 2115130f4520SKenneth D. Merry } 21168a08cec1SMateusz Guzik pwd_ensure_dirs(); 2117130f4520SKenneth D. Merry 21180bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "file"); 21190bcd4ab6SAlexander Motin if (value == NULL) { 21200bcd4ab6SAlexander Motin snprintf(req->error_str, sizeof(req->error_str), 21210bcd4ab6SAlexander Motin "no file argument specified"); 21220bcd4ab6SAlexander Motin return (1); 21230bcd4ab6SAlexander Motin } 21240bcd4ab6SAlexander Motin free(be_lun->dev_path, M_CTLBLK); 21250bcd4ab6SAlexander Motin be_lun->dev_path = strdup(value, M_CTLBLK); 21260bcd4ab6SAlexander Motin 21270bcd4ab6SAlexander Motin flags = FREAD; 21280bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "readonly"); 21290bcd4ab6SAlexander Motin if (value == NULL || strcmp(value, "on") != 0) 21300bcd4ab6SAlexander Motin flags |= FWRITE; 21310bcd4ab6SAlexander Motin 2132130f4520SKenneth D. Merry again: 2133130f4520SKenneth D. Merry NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2134130f4520SKenneth D. Merry error = vn_open(&nd, &flags, 0, NULL); 21350bcd4ab6SAlexander Motin if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 21360bcd4ab6SAlexander Motin flags &= ~FWRITE; 21370bcd4ab6SAlexander Motin goto again; 21380bcd4ab6SAlexander Motin } 2139130f4520SKenneth D. Merry if (error) { 2140130f4520SKenneth D. Merry /* 2141130f4520SKenneth D. Merry * This is the only reasonable guess we can make as far as 2142130f4520SKenneth D. Merry * path if the user doesn't give us a fully qualified path. 2143130f4520SKenneth D. Merry * If they want to specify a file, they need to specify the 2144130f4520SKenneth D. Merry * full path. 2145130f4520SKenneth D. Merry */ 2146130f4520SKenneth D. Merry if (be_lun->dev_path[0] != '/') { 2147130f4520SKenneth D. Merry char *dev_name; 2148130f4520SKenneth D. Merry 21490bcd4ab6SAlexander Motin asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2150130f4520SKenneth D. Merry be_lun->dev_path); 2151130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 2152130f4520SKenneth D. Merry be_lun->dev_path = dev_name; 2153130f4520SKenneth D. Merry goto again; 2154130f4520SKenneth D. Merry } 2155130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 215619720f41SAlexander Motin "error opening %s: %d", be_lun->dev_path, error); 2157130f4520SKenneth D. Merry return (error); 2158130f4520SKenneth D. Merry } 21590bcd4ab6SAlexander Motin if (flags & FWRITE) 21600bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 21610bcd4ab6SAlexander Motin else 21620bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2163130f4520SKenneth D. Merry 2164130f4520SKenneth D. Merry NDFREE(&nd, NDF_ONLY_PNBUF); 2165130f4520SKenneth D. Merry be_lun->vn = nd.ni_vp; 2166130f4520SKenneth D. Merry 2167130f4520SKenneth D. Merry /* We only support disks and files. */ 2168130f4520SKenneth D. Merry if (vn_isdisk(be_lun->vn, &error)) { 2169130f4520SKenneth D. Merry error = ctl_be_block_open_dev(be_lun, req); 2170130f4520SKenneth D. Merry } else if (be_lun->vn->v_type == VREG) { 2171130f4520SKenneth D. Merry error = ctl_be_block_open_file(be_lun, req); 2172130f4520SKenneth D. Merry } else { 2173130f4520SKenneth D. Merry error = EINVAL; 2174130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2175025a2301SEdward Tomasz Napierala "%s is not a disk or plain file", be_lun->dev_path); 2176130f4520SKenneth D. Merry } 2177130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 2178130f4520SKenneth D. Merry 2179a15bbf15SAlexander Motin if (error != 0) 2180130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 21810bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 21820bcd4ab6SAlexander Motin if (be_lun->dispatch != ctl_be_block_dispatch_dev) 21830bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 21840bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "serseq"); 21850bcd4ab6SAlexander Motin if (value != NULL && strcmp(value, "on") == 0) 21860bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 21870bcd4ab6SAlexander Motin else if (value != NULL && strcmp(value, "read") == 0) 21880bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 21890bcd4ab6SAlexander Motin else if (value != NULL && strcmp(value, "off") == 0) 21900bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2191130f4520SKenneth D. Merry return (0); 2192130f4520SKenneth D. Merry } 2193130f4520SKenneth D. Merry 2194130f4520SKenneth D. Merry static int 2195130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2196130f4520SKenneth D. Merry { 21970bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 2198130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2199130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 220057a5db13SAlexander Motin char num_thread_str[16]; 2201130f4520SKenneth D. Merry char tmpstr[32]; 220257a5db13SAlexander Motin char *value; 2203fbc8d4ffSAlexander Motin int retval, num_threads; 220457a5db13SAlexander Motin int tmp_num_threads; 2205130f4520SKenneth D. Merry 2206130f4520SKenneth D. Merry params = &req->reqdata.create; 2207130f4520SKenneth D. Merry retval = 0; 220819720f41SAlexander Motin req->status = CTL_LUN_OK; 2209130f4520SKenneth D. Merry 2210130f4520SKenneth D. Merry be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 22110bcd4ab6SAlexander Motin cbe_lun = &be_lun->cbe_lun; 22120bcd4ab6SAlexander Motin cbe_lun->be_lun = be_lun; 221319720f41SAlexander Motin be_lun->params = req->reqdata.create; 2214130f4520SKenneth D. Merry be_lun->softc = softc; 2215130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->input_queue); 2216ef8daf3fSAlexander Motin STAILQ_INIT(&be_lun->config_read_queue); 2217130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->config_write_queue); 2218130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->datamove_queue); 2219130f4520SKenneth D. Merry sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 222075c7a1d3SAlexander Motin mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 222175c7a1d3SAlexander Motin mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 22220bcd4ab6SAlexander Motin ctl_init_opts(&cbe_lun->options, 222343fb3a65SAlexander Motin req->num_be_args, req->kern_be_args); 222408a7cce5SAlexander Motin be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2225eeb94054SAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2226130f4520SKenneth D. Merry if (be_lun->lun_zone == NULL) { 2227130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 222819720f41SAlexander Motin "error allocating UMA zone"); 2229130f4520SKenneth D. Merry goto bailout_error; 2230130f4520SKenneth D. Merry } 2231130f4520SKenneth D. Merry 2232130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 22330bcd4ab6SAlexander Motin cbe_lun->lun_type = params->device_type; 2234130f4520SKenneth D. Merry else 22350bcd4ab6SAlexander Motin cbe_lun->lun_type = T_DIRECT; 22360bcd4ab6SAlexander Motin be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 22377ac58230SAlexander Motin cbe_lun->flags = 0; 22387ac58230SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "ha_role"); 22397ac58230SAlexander Motin if (value != NULL) { 22407ac58230SAlexander Motin if (strcmp(value, "primary") == 0) 22417ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 22427ac58230SAlexander Motin } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 22437ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2244130f4520SKenneth D. Merry 22450bcd4ab6SAlexander Motin if (cbe_lun->lun_type == T_DIRECT) { 2246a15bbf15SAlexander Motin be_lun->size_bytes = params->lun_size_bytes; 2247a15bbf15SAlexander Motin if (params->blocksize_bytes != 0) 22480bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 2249a15bbf15SAlexander Motin else 22500bcd4ab6SAlexander Motin cbe_lun->blocksize = 512; 22510bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 22520bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 22530bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 2254130f4520SKenneth D. Merry 22557ac58230SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 22567ac58230SAlexander Motin control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2257130f4520SKenneth D. Merry retval = ctl_be_block_open(softc, be_lun, req); 2258130f4520SKenneth D. Merry if (retval != 0) { 2259130f4520SKenneth D. Merry retval = 0; 226019720f41SAlexander Motin req->status = CTL_LUN_WARNING; 2261130f4520SKenneth D. Merry } 22627ac58230SAlexander Motin } 22630bcd4ab6SAlexander Motin num_threads = cbb_num_threads; 2264130f4520SKenneth D. Merry } else { 2265130f4520SKenneth D. Merry num_threads = 1; 2266130f4520SKenneth D. Merry } 2267130f4520SKenneth D. Merry 2268130f4520SKenneth D. Merry /* 2269130f4520SKenneth D. Merry * XXX This searching loop might be refactored to be combined with 2270130f4520SKenneth D. Merry * the loop above, 2271130f4520SKenneth D. Merry */ 22720bcd4ab6SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "num_threads"); 227357a5db13SAlexander Motin if (value != NULL) { 227457a5db13SAlexander Motin tmp_num_threads = strtol(value, NULL, 0); 2275130f4520SKenneth D. Merry 2276130f4520SKenneth D. Merry /* 2277130f4520SKenneth D. Merry * We don't let the user specify less than one 2278130f4520SKenneth D. Merry * thread, but hope he's clueful enough not to 2279130f4520SKenneth D. Merry * specify 1000 threads. 2280130f4520SKenneth D. Merry */ 2281130f4520SKenneth D. Merry if (tmp_num_threads < 1) { 2282130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 228319720f41SAlexander Motin "invalid number of threads %s", 228419720f41SAlexander Motin num_thread_str); 2285130f4520SKenneth D. Merry goto bailout_error; 2286130f4520SKenneth D. Merry } 2287130f4520SKenneth D. Merry num_threads = tmp_num_threads; 228857a5db13SAlexander Motin } 2289130f4520SKenneth D. Merry 229019720f41SAlexander Motin if (be_lun->vn == NULL) 22910bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2292130f4520SKenneth D. Merry /* Tell the user the blocksize we ended up using */ 229319720f41SAlexander Motin params->lun_size_bytes = be_lun->size_bytes; 22940bcd4ab6SAlexander Motin params->blocksize_bytes = cbe_lun->blocksize; 2295130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_ID_REQ) { 22960bcd4ab6SAlexander Motin cbe_lun->req_lun_id = params->req_lun_id; 22970bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2298130f4520SKenneth D. Merry } else 22990bcd4ab6SAlexander Motin cbe_lun->req_lun_id = 0; 2300130f4520SKenneth D. Merry 23010bcd4ab6SAlexander Motin cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 23020bcd4ab6SAlexander Motin cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 23030bcd4ab6SAlexander Motin cbe_lun->be = &ctl_be_block_driver; 2304130f4520SKenneth D. Merry 2305130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2306130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2307130f4520SKenneth D. Merry softc->num_luns); 23080bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->serial_num, tmpstr, 23090bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2310130f4520SKenneth D. Merry 2311130f4520SKenneth D. Merry /* Tell the user what we used for a serial number */ 2312130f4520SKenneth D. Merry strncpy((char *)params->serial_num, tmpstr, 2313e7038eb7SAlexander Motin MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2314130f4520SKenneth D. Merry } else { 23150bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->serial_num, params->serial_num, 23160bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->serial_num), 2317130f4520SKenneth D. Merry sizeof(params->serial_num))); 2318130f4520SKenneth D. Merry } 2319130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2320130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 23210bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->device_id, tmpstr, 23220bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2323130f4520SKenneth D. Merry 2324130f4520SKenneth D. Merry /* Tell the user what we used for a device ID */ 2325130f4520SKenneth D. Merry strncpy((char *)params->device_id, tmpstr, 2326e7038eb7SAlexander Motin MIN(sizeof(params->device_id), sizeof(tmpstr))); 2327130f4520SKenneth D. Merry } else { 23280bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->device_id, params->device_id, 23290bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->device_id), 2330130f4520SKenneth D. Merry sizeof(params->device_id))); 2331130f4520SKenneth D. Merry } 2332130f4520SKenneth D. Merry 2333130f4520SKenneth D. Merry TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2334130f4520SKenneth D. Merry 2335130f4520SKenneth D. Merry be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2336130f4520SKenneth D. Merry taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2337130f4520SKenneth D. Merry 2338130f4520SKenneth D. Merry if (be_lun->io_taskqueue == NULL) { 2339130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 234019720f41SAlexander Motin "unable to create taskqueue"); 2341130f4520SKenneth D. Merry goto bailout_error; 2342130f4520SKenneth D. Merry } 2343130f4520SKenneth D. Merry 2344130f4520SKenneth D. Merry /* 2345130f4520SKenneth D. Merry * Note that we start the same number of threads by default for 2346130f4520SKenneth D. Merry * both the file case and the block device case. For the file 2347130f4520SKenneth D. Merry * case, we need multiple threads to allow concurrency, because the 2348130f4520SKenneth D. Merry * vnode interface is designed to be a blocking interface. For the 2349130f4520SKenneth D. Merry * block device case, ZFS zvols at least will block the caller's 2350130f4520SKenneth D. Merry * context in many instances, and so we need multiple threads to 2351130f4520SKenneth D. Merry * overcome that problem. Other block devices don't need as many 2352130f4520SKenneth D. Merry * threads, but they shouldn't cause too many problems. 2353130f4520SKenneth D. Merry * 2354130f4520SKenneth D. Merry * If the user wants to just have a single thread for a block 2355130f4520SKenneth D. Merry * device, he can specify that when the LUN is created, or change 2356130f4520SKenneth D. Merry * the tunable/sysctl to alter the default number of threads. 2357130f4520SKenneth D. Merry */ 2358130f4520SKenneth D. Merry retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2359130f4520SKenneth D. Merry /*num threads*/num_threads, 2360130f4520SKenneth D. Merry /*priority*/PWAIT, 2361130f4520SKenneth D. Merry /*thread name*/ 2362130f4520SKenneth D. Merry "%s taskq", be_lun->lunname); 2363130f4520SKenneth D. Merry 2364130f4520SKenneth D. Merry if (retval != 0) 2365130f4520SKenneth D. Merry goto bailout_error; 2366130f4520SKenneth D. Merry 2367130f4520SKenneth D. Merry be_lun->num_threads = num_threads; 2368130f4520SKenneth D. Merry 2369130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2370130f4520SKenneth D. Merry softc->num_luns++; 2371130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2372130f4520SKenneth D. Merry 2373130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2374130f4520SKenneth D. Merry 23750bcd4ab6SAlexander Motin retval = ctl_add_lun(&be_lun->cbe_lun); 2376130f4520SKenneth D. Merry if (retval != 0) { 2377130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2378130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2379130f4520SKenneth D. Merry links); 2380130f4520SKenneth D. Merry softc->num_luns--; 2381130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2382130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 238319720f41SAlexander Motin "ctl_add_lun() returned error %d, see dmesg for " 238419720f41SAlexander Motin "details", retval); 2385130f4520SKenneth D. Merry retval = 0; 2386130f4520SKenneth D. Merry goto bailout_error; 2387130f4520SKenneth D. Merry } 2388130f4520SKenneth D. Merry 2389130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2390130f4520SKenneth D. Merry 2391130f4520SKenneth D. Merry /* 2392130f4520SKenneth D. Merry * Tell the config_status routine that we're waiting so it won't 2393130f4520SKenneth D. Merry * clean up the LUN in the event of an error. 2394130f4520SKenneth D. Merry */ 2395130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2396130f4520SKenneth D. Merry 2397130f4520SKenneth D. Merry while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2398130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2399130f4520SKenneth D. Merry if (retval == EINTR) 2400130f4520SKenneth D. Merry break; 2401130f4520SKenneth D. Merry } 2402130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2403130f4520SKenneth D. Merry 2404130f4520SKenneth D. Merry if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2405130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 240619720f41SAlexander Motin "LUN configuration error, see dmesg for details"); 2407130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2408130f4520SKenneth D. Merry links); 2409130f4520SKenneth D. Merry softc->num_luns--; 2410130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2411130f4520SKenneth D. Merry goto bailout_error; 2412130f4520SKenneth D. Merry } else { 24130bcd4ab6SAlexander Motin params->req_lun_id = cbe_lun->lun_id; 2414130f4520SKenneth D. Merry } 2415130f4520SKenneth D. Merry 2416130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2417130f4520SKenneth D. Merry 2418130f4520SKenneth D. Merry be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 24190bcd4ab6SAlexander Motin cbe_lun->blocksize, 2420130f4520SKenneth D. Merry DEVSTAT_ALL_SUPPORTED, 24210bcd4ab6SAlexander Motin cbe_lun->lun_type 2422130f4520SKenneth D. Merry | DEVSTAT_TYPE_IF_OTHER, 2423130f4520SKenneth D. Merry DEVSTAT_PRIORITY_OTHER); 2424130f4520SKenneth D. Merry 2425130f4520SKenneth D. Merry return (retval); 2426130f4520SKenneth D. Merry 2427130f4520SKenneth D. Merry bailout_error: 2428130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2429130f4520SKenneth D. Merry 24309e005bbcSAlexander Motin if (be_lun->io_taskqueue != NULL) 24319e005bbcSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 2432130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 24339e005bbcSAlexander Motin if (be_lun->dev_path != NULL) 2434130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 24359e005bbcSAlexander Motin if (be_lun->lun_zone != NULL) 24369e005bbcSAlexander Motin uma_zdestroy(be_lun->lun_zone); 24370bcd4ab6SAlexander Motin ctl_free_opts(&cbe_lun->options); 243875c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 243975c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2440130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2441130f4520SKenneth D. Merry 2442130f4520SKenneth D. Merry return (retval); 2443130f4520SKenneth D. Merry } 2444130f4520SKenneth D. Merry 2445130f4520SKenneth D. Merry static int 2446130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2447130f4520SKenneth D. Merry { 2448130f4520SKenneth D. Merry struct ctl_lun_rm_params *params; 2449130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2450*ee4ad294SAlexander Motin struct ctl_be_lun *cbe_lun; 2451130f4520SKenneth D. Merry int retval; 2452130f4520SKenneth D. Merry 2453130f4520SKenneth D. Merry params = &req->reqdata.rm; 2454130f4520SKenneth D. Merry 2455130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2456130f4520SKenneth D. Merry STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 24570bcd4ab6SAlexander Motin if (be_lun->cbe_lun.lun_id == params->lun_id) 2458130f4520SKenneth D. Merry break; 2459130f4520SKenneth D. Merry } 2460130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2461130f4520SKenneth D. Merry 2462130f4520SKenneth D. Merry if (be_lun == NULL) { 2463130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 246419720f41SAlexander Motin "LUN %u is not managed by the block backend", 246519720f41SAlexander Motin params->lun_id); 2466130f4520SKenneth D. Merry goto bailout_error; 2467130f4520SKenneth D. Merry } 2468*ee4ad294SAlexander Motin cbe_lun = &be_lun->cbe_lun; 2469130f4520SKenneth D. Merry 2470*ee4ad294SAlexander Motin retval = ctl_disable_lun(cbe_lun); 2471130f4520SKenneth D. Merry if (retval != 0) { 2472130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 247319720f41SAlexander Motin "error %d returned from ctl_disable_lun() for " 247419720f41SAlexander Motin "LUN %d", retval, params->lun_id); 2475130f4520SKenneth D. Merry goto bailout_error; 2476130f4520SKenneth D. Merry } 2477130f4520SKenneth D. Merry 2478*ee4ad294SAlexander Motin if (be_lun->vn != NULL) { 2479*ee4ad294SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2480*ee4ad294SAlexander Motin ctl_lun_offline(cbe_lun); 2481*ee4ad294SAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 2482*ee4ad294SAlexander Motin ctl_be_block_close(be_lun); 2483*ee4ad294SAlexander Motin } 2484*ee4ad294SAlexander Motin 2485*ee4ad294SAlexander Motin retval = ctl_invalidate_lun(cbe_lun); 2486130f4520SKenneth D. Merry if (retval != 0) { 2487130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 248819720f41SAlexander Motin "error %d returned from ctl_invalidate_lun() for " 248919720f41SAlexander Motin "LUN %d", retval, params->lun_id); 2490130f4520SKenneth D. Merry goto bailout_error; 2491130f4520SKenneth D. Merry } 2492130f4520SKenneth D. Merry 2493130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2494130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2495130f4520SKenneth D. Merry while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2496130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2497130f4520SKenneth D. Merry if (retval == EINTR) 2498130f4520SKenneth D. Merry break; 2499130f4520SKenneth D. Merry } 2500130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2501130f4520SKenneth D. Merry 2502130f4520SKenneth D. Merry if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2503130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 250419720f41SAlexander Motin "interrupted waiting for LUN to be freed"); 2505130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2506130f4520SKenneth D. Merry goto bailout_error; 2507130f4520SKenneth D. Merry } 2508130f4520SKenneth D. Merry 2509130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2510130f4520SKenneth D. Merry 2511130f4520SKenneth D. Merry softc->num_luns--; 2512130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2513130f4520SKenneth D. Merry 2514*ee4ad294SAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 2515130f4520SKenneth D. Merry taskqueue_free(be_lun->io_taskqueue); 2516130f4520SKenneth D. Merry 2517130f4520SKenneth D. Merry if (be_lun->disk_stats != NULL) 2518130f4520SKenneth D. Merry devstat_remove_entry(be_lun->disk_stats); 2519130f4520SKenneth D. Merry 2520130f4520SKenneth D. Merry uma_zdestroy(be_lun->lun_zone); 2521130f4520SKenneth D. Merry 2522*ee4ad294SAlexander Motin ctl_free_opts(&cbe_lun->options); 2523130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 252475c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 252575c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2526130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2527130f4520SKenneth D. Merry 2528130f4520SKenneth D. Merry req->status = CTL_LUN_OK; 2529130f4520SKenneth D. Merry 2530130f4520SKenneth D. Merry return (0); 2531130f4520SKenneth D. Merry 2532130f4520SKenneth D. Merry bailout_error: 2533130f4520SKenneth D. Merry 2534130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2535130f4520SKenneth D. Merry 2536130f4520SKenneth D. Merry return (0); 2537130f4520SKenneth D. Merry } 2538130f4520SKenneth D. Merry 253981177295SEdward Tomasz Napierala static int 254081177295SEdward Tomasz Napierala ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 254181177295SEdward Tomasz Napierala struct ctl_lun_req *req) 254281177295SEdward Tomasz Napierala { 25430bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 254481177295SEdward Tomasz Napierala struct vattr vattr; 254581177295SEdward Tomasz Napierala int error; 254619720f41SAlexander Motin struct ctl_lun_create_params *params = &be_lun->params; 254781177295SEdward Tomasz Napierala 254881177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 254981177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 255081177295SEdward Tomasz Napierala } else { 255171d8e97eSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 255281177295SEdward Tomasz Napierala error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 255371d8e97eSAlexander Motin VOP_UNLOCK(be_lun->vn, 0); 255481177295SEdward Tomasz Napierala if (error != 0) { 255581177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 255681177295SEdward Tomasz Napierala "error calling VOP_GETATTR() for file %s", 255781177295SEdward Tomasz Napierala be_lun->dev_path); 255881177295SEdward Tomasz Napierala return (error); 255981177295SEdward Tomasz Napierala } 256081177295SEdward Tomasz Napierala be_lun->size_bytes = vattr.va_size; 256181177295SEdward Tomasz Napierala } 25620bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 25630bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 25640bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 256581177295SEdward Tomasz Napierala return (0); 256681177295SEdward Tomasz Napierala } 256781177295SEdward Tomasz Napierala 256881177295SEdward Tomasz Napierala static int 256981177295SEdward Tomasz Napierala ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 257081177295SEdward Tomasz Napierala struct ctl_lun_req *req) 257181177295SEdward Tomasz Napierala { 25720bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 257319720f41SAlexander Motin struct ctl_lun_create_params *params = &be_lun->params; 25743236151eSAlexander Motin struct cdevsw *csw; 25753236151eSAlexander Motin struct cdev *dev; 257681177295SEdward Tomasz Napierala uint64_t size_bytes; 25773236151eSAlexander Motin int error, ref; 257881177295SEdward Tomasz Napierala 25793236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 25803236151eSAlexander Motin if (csw == NULL) 25813236151eSAlexander Motin return (ENXIO); 25823236151eSAlexander Motin if (csw->d_ioctl == NULL) { 25833236151eSAlexander Motin dev_relthread(dev, ref); 258481177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 258519720f41SAlexander Motin "no d_ioctl for device %s!", be_lun->dev_path); 258681177295SEdward Tomasz Napierala return (ENODEV); 258781177295SEdward Tomasz Napierala } 258881177295SEdward Tomasz Napierala 25893236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&size_bytes, FREAD, 259081177295SEdward Tomasz Napierala curthread); 25913236151eSAlexander Motin dev_relthread(dev, ref); 259281177295SEdward Tomasz Napierala if (error) { 259381177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 259419720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE ioctl " 259519720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 259681177295SEdward Tomasz Napierala return (error); 259781177295SEdward Tomasz Napierala } 259881177295SEdward Tomasz Napierala 259981177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 260081177295SEdward Tomasz Napierala if (params->lun_size_bytes > size_bytes) { 260181177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 260219720f41SAlexander Motin "requested LUN size %ju > backing device " 260319720f41SAlexander Motin "size %ju", 260481177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 260581177295SEdward Tomasz Napierala (uintmax_t)size_bytes); 260681177295SEdward Tomasz Napierala return (EINVAL); 260781177295SEdward Tomasz Napierala } 260881177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 260981177295SEdward Tomasz Napierala } else { 261081177295SEdward Tomasz Napierala be_lun->size_bytes = size_bytes; 261181177295SEdward Tomasz Napierala } 26120bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 26130bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 26140bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 261581177295SEdward Tomasz Napierala return (0); 261681177295SEdward Tomasz Napierala } 261781177295SEdward Tomasz Napierala 261881177295SEdward Tomasz Napierala static int 261981177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 262081177295SEdward Tomasz Napierala { 262181177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 262281177295SEdward Tomasz Napierala struct ctl_be_block_lun *be_lun; 2623a3977beaSAlexander Motin struct ctl_be_lun *cbe_lun; 26247ac58230SAlexander Motin char *value; 262571d8e97eSAlexander Motin uint64_t oldsize; 26267ac58230SAlexander Motin int error, wasprim; 262781177295SEdward Tomasz Napierala 262881177295SEdward Tomasz Napierala params = &req->reqdata.modify; 262981177295SEdward Tomasz Napierala 263081177295SEdward Tomasz Napierala mtx_lock(&softc->lock); 263181177295SEdward Tomasz Napierala STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 26320bcd4ab6SAlexander Motin if (be_lun->cbe_lun.lun_id == params->lun_id) 263381177295SEdward Tomasz Napierala break; 263481177295SEdward Tomasz Napierala } 263581177295SEdward Tomasz Napierala mtx_unlock(&softc->lock); 263681177295SEdward Tomasz Napierala 263781177295SEdward Tomasz Napierala if (be_lun == NULL) { 263881177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 263919720f41SAlexander Motin "LUN %u is not managed by the block backend", 264019720f41SAlexander Motin params->lun_id); 264181177295SEdward Tomasz Napierala goto bailout_error; 264281177295SEdward Tomasz Napierala } 2643a3977beaSAlexander Motin cbe_lun = &be_lun->cbe_lun; 264481177295SEdward Tomasz Napierala 2645a3977beaSAlexander Motin if (params->lun_size_bytes != 0) 264619720f41SAlexander Motin be_lun->params.lun_size_bytes = params->lun_size_bytes; 2647a3977beaSAlexander Motin ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 264881177295SEdward Tomasz Napierala 26497ac58230SAlexander Motin wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 26507ac58230SAlexander Motin value = ctl_get_opt(&cbe_lun->options, "ha_role"); 26517ac58230SAlexander Motin if (value != NULL) { 26527ac58230SAlexander Motin if (strcmp(value, "primary") == 0) 26537ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 26547ac58230SAlexander Motin else 26557ac58230SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 26567ac58230SAlexander Motin } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 26577ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 26587ac58230SAlexander Motin else 26597ac58230SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 26607ac58230SAlexander Motin if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 26617ac58230SAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 26627ac58230SAlexander Motin ctl_lun_primary(cbe_lun); 26637ac58230SAlexander Motin else 26647ac58230SAlexander Motin ctl_lun_secondary(cbe_lun); 26657ac58230SAlexander Motin } 26667ac58230SAlexander Motin 26670bcd4ab6SAlexander Motin oldsize = be_lun->size_blocks; 26687ac58230SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 26697ac58230SAlexander Motin control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 267019720f41SAlexander Motin if (be_lun->vn == NULL) 267119720f41SAlexander Motin error = ctl_be_block_open(softc, be_lun, req); 2672b9b4269cSAlexander Motin else if (vn_isdisk(be_lun->vn, &error)) 2673b9b4269cSAlexander Motin error = ctl_be_block_modify_dev(be_lun, req); 267419720f41SAlexander Motin else if (be_lun->vn->v_type == VREG) 267581177295SEdward Tomasz Napierala error = ctl_be_block_modify_file(be_lun, req); 267681177295SEdward Tomasz Napierala else 2677b9b4269cSAlexander Motin error = EINVAL; 2678a3977beaSAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) && 26790bcd4ab6SAlexander Motin be_lun->vn != NULL) { 2680a3977beaSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE; 2681a3977beaSAlexander Motin ctl_lun_online(cbe_lun); 268271d8e97eSAlexander Motin } 26837ac58230SAlexander Motin } else { 26847ac58230SAlexander Motin if (be_lun->vn != NULL) { 26857ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 26867ac58230SAlexander Motin ctl_lun_offline(cbe_lun); 2687*ee4ad294SAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 26887ac58230SAlexander Motin error = ctl_be_block_close(be_lun); 26897ac58230SAlexander Motin } else 26907ac58230SAlexander Motin error = 0; 26917ac58230SAlexander Motin } 26927ac58230SAlexander Motin if (be_lun->size_blocks != oldsize) 26937ac58230SAlexander Motin ctl_lun_capacity_changed(cbe_lun); 269481177295SEdward Tomasz Napierala 269581177295SEdward Tomasz Napierala /* Tell the user the exact size we ended up using */ 269681177295SEdward Tomasz Napierala params->lun_size_bytes = be_lun->size_bytes; 269781177295SEdward Tomasz Napierala 269819720f41SAlexander Motin req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 269981177295SEdward Tomasz Napierala return (0); 270081177295SEdward Tomasz Napierala 270181177295SEdward Tomasz Napierala bailout_error: 270281177295SEdward Tomasz Napierala req->status = CTL_LUN_ERROR; 270381177295SEdward Tomasz Napierala return (0); 270481177295SEdward Tomasz Napierala } 270581177295SEdward Tomasz Napierala 2706130f4520SKenneth D. Merry static void 2707130f4520SKenneth D. Merry ctl_be_block_lun_shutdown(void *be_lun) 2708130f4520SKenneth D. Merry { 2709130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2710130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2711130f4520SKenneth D. Merry 2712130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2713130f4520SKenneth D. Merry 2714130f4520SKenneth D. Merry softc = lun->softc; 2715130f4520SKenneth D. Merry 2716130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2717130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2718130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2719130f4520SKenneth D. Merry wakeup(lun); 2720130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2721130f4520SKenneth D. Merry 2722130f4520SKenneth D. Merry } 2723130f4520SKenneth D. Merry 2724130f4520SKenneth D. Merry static void 2725130f4520SKenneth D. Merry ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2726130f4520SKenneth D. Merry { 2727130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2728130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2729130f4520SKenneth D. Merry 2730130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2731130f4520SKenneth D. Merry softc = lun->softc; 2732130f4520SKenneth D. Merry 2733130f4520SKenneth D. Merry if (status == CTL_LUN_CONFIG_OK) { 2734130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2735130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2736130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2737130f4520SKenneth D. Merry wakeup(lun); 2738130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2739130f4520SKenneth D. Merry 2740130f4520SKenneth D. Merry /* 2741130f4520SKenneth D. Merry * We successfully added the LUN, attempt to enable it. 2742130f4520SKenneth D. Merry */ 27430bcd4ab6SAlexander Motin if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2744130f4520SKenneth D. Merry printf("%s: ctl_enable_lun() failed!\n", __func__); 27450bcd4ab6SAlexander Motin if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2746130f4520SKenneth D. Merry printf("%s: ctl_invalidate_lun() failed!\n", 2747130f4520SKenneth D. Merry __func__); 2748130f4520SKenneth D. Merry } 2749130f4520SKenneth D. Merry } 2750130f4520SKenneth D. Merry 2751130f4520SKenneth D. Merry return; 2752130f4520SKenneth D. Merry } 2753130f4520SKenneth D. Merry 2754130f4520SKenneth D. Merry 2755130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2756130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2757130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2758130f4520SKenneth D. Merry wakeup(lun); 2759130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2760130f4520SKenneth D. Merry } 2761130f4520SKenneth D. Merry 2762130f4520SKenneth D. Merry 2763130f4520SKenneth D. Merry static int 2764130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io) 2765130f4520SKenneth D. Merry { 2766130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 27670bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 2768130f4520SKenneth D. Merry int retval; 2769130f4520SKenneth D. Merry 2770130f4520SKenneth D. Merry retval = 0; 2771130f4520SKenneth D. Merry 2772130f4520SKenneth D. Merry DPRINTF("entered\n"); 2773130f4520SKenneth D. Merry 27740bcd4ab6SAlexander Motin cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2775130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 27760bcd4ab6SAlexander Motin be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2777130f4520SKenneth D. Merry 2778130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 2779130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 2780130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 2781ee7f31c0SAlexander Motin case WRITE_SAME_10: 2782ee7f31c0SAlexander Motin case WRITE_SAME_16: 2783ee7f31c0SAlexander Motin case UNMAP: 2784130f4520SKenneth D. Merry /* 2785130f4520SKenneth D. Merry * The upper level CTL code will filter out any CDBs with 2786130f4520SKenneth D. Merry * the immediate bit set and return the proper error. 2787130f4520SKenneth D. Merry * 2788130f4520SKenneth D. Merry * We don't really need to worry about what LBA range the 2789130f4520SKenneth D. Merry * user asked to be synced out. When they issue a sync 2790130f4520SKenneth D. Merry * cache command, we'll sync out the whole thing. 2791130f4520SKenneth D. Merry */ 279275c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 2793130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2794130f4520SKenneth D. Merry links); 279575c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2796130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2797130f4520SKenneth D. Merry break; 2798130f4520SKenneth D. Merry case START_STOP_UNIT: { 2799130f4520SKenneth D. Merry struct scsi_start_stop_unit *cdb; 2800130f4520SKenneth D. Merry 2801130f4520SKenneth D. Merry cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2802130f4520SKenneth D. Merry 2803130f4520SKenneth D. Merry if (cdb->how & SSS_START) 28040bcd4ab6SAlexander Motin retval = ctl_start_lun(cbe_lun); 2805130f4520SKenneth D. Merry else { 28060bcd4ab6SAlexander Motin retval = ctl_stop_lun(cbe_lun); 2807130f4520SKenneth D. Merry /* 2808130f4520SKenneth D. Merry * XXX KDM Copan-specific offline behavior. 2809130f4520SKenneth D. Merry * Figure out a reasonable way to port this? 2810130f4520SKenneth D. Merry */ 2811130f4520SKenneth D. Merry #ifdef NEEDTOPORT 2812130f4520SKenneth D. Merry if ((retval == 0) 2813130f4520SKenneth D. Merry && (cdb->byte2 & SSS_ONOFFLINE)) 28140bcd4ab6SAlexander Motin retval = ctl_lun_offline(cbe_lun); 2815130f4520SKenneth D. Merry #endif 2816130f4520SKenneth D. Merry } 2817130f4520SKenneth D. Merry 2818130f4520SKenneth D. Merry /* 2819130f4520SKenneth D. Merry * In general, the above routines should not fail. They 2820130f4520SKenneth D. Merry * just set state for the LUN. So we've got something 2821130f4520SKenneth D. Merry * pretty wrong here if we can't start or stop the LUN. 2822130f4520SKenneth D. Merry */ 2823130f4520SKenneth D. Merry if (retval != 0) { 2824130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 2825130f4520SKenneth D. Merry /*sks_valid*/ 1, 2826130f4520SKenneth D. Merry /*retry_count*/ 0xf051); 2827130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2828130f4520SKenneth D. Merry } else { 2829130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 2830130f4520SKenneth D. Merry } 2831130f4520SKenneth D. Merry ctl_config_write_done(io); 2832130f4520SKenneth D. Merry break; 2833130f4520SKenneth D. Merry } 2834130f4520SKenneth D. Merry default: 2835130f4520SKenneth D. Merry ctl_set_invalid_opcode(&io->scsiio); 2836130f4520SKenneth D. Merry ctl_config_write_done(io); 2837130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2838130f4520SKenneth D. Merry break; 2839130f4520SKenneth D. Merry } 2840130f4520SKenneth D. Merry 2841130f4520SKenneth D. Merry return (retval); 2842130f4520SKenneth D. Merry } 2843130f4520SKenneth D. Merry 2844130f4520SKenneth D. Merry static int 2845130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io) 2846130f4520SKenneth D. Merry { 2847ef8daf3fSAlexander Motin struct ctl_be_block_lun *be_lun; 28480bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 2849ef8daf3fSAlexander Motin int retval = 0; 2850ef8daf3fSAlexander Motin 2851ef8daf3fSAlexander Motin DPRINTF("entered\n"); 2852ef8daf3fSAlexander Motin 28530bcd4ab6SAlexander Motin cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2854ef8daf3fSAlexander Motin CTL_PRIV_BACKEND_LUN].ptr; 28550bcd4ab6SAlexander Motin be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2856ef8daf3fSAlexander Motin 2857ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 2858ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: 2859ef8daf3fSAlexander Motin if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2860ef8daf3fSAlexander Motin mtx_lock(&be_lun->queue_lock); 2861ef8daf3fSAlexander Motin STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2862ef8daf3fSAlexander Motin &io->io_hdr, links); 2863ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 2864ef8daf3fSAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, 2865ef8daf3fSAlexander Motin &be_lun->io_task); 2866ef8daf3fSAlexander Motin retval = CTL_RETVAL_QUEUED; 2867ef8daf3fSAlexander Motin break; 2868ef8daf3fSAlexander Motin } 2869ef8daf3fSAlexander Motin ctl_set_invalid_field(&io->scsiio, 2870ef8daf3fSAlexander Motin /*sks_valid*/ 1, 2871ef8daf3fSAlexander Motin /*command*/ 1, 2872ef8daf3fSAlexander Motin /*field*/ 1, 2873ef8daf3fSAlexander Motin /*bit_valid*/ 1, 2874ef8daf3fSAlexander Motin /*bit*/ 4); 2875ef8daf3fSAlexander Motin ctl_config_read_done(io); 2876ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2877ef8daf3fSAlexander Motin break; 2878ef8daf3fSAlexander Motin default: 2879ef8daf3fSAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 2880ef8daf3fSAlexander Motin ctl_config_read_done(io); 2881ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2882ef8daf3fSAlexander Motin break; 2883ef8daf3fSAlexander Motin } 2884ef8daf3fSAlexander Motin 2885ef8daf3fSAlexander Motin return (retval); 2886130f4520SKenneth D. Merry } 2887130f4520SKenneth D. Merry 2888130f4520SKenneth D. Merry static int 2889130f4520SKenneth D. Merry ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2890130f4520SKenneth D. Merry { 2891130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2892130f4520SKenneth D. Merry int retval; 2893130f4520SKenneth D. Merry 2894130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2895130f4520SKenneth D. Merry retval = 0; 2896130f4520SKenneth D. Merry 28972cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "\t<num_threads>"); 2898130f4520SKenneth D. Merry 2899130f4520SKenneth D. Merry if (retval != 0) 2900130f4520SKenneth D. Merry goto bailout; 2901130f4520SKenneth D. Merry 2902130f4520SKenneth D. Merry retval = sbuf_printf(sb, "%d", lun->num_threads); 2903130f4520SKenneth D. Merry 2904130f4520SKenneth D. Merry if (retval != 0) 2905130f4520SKenneth D. Merry goto bailout; 2906130f4520SKenneth D. Merry 29072cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "</num_threads>\n"); 2908130f4520SKenneth D. Merry 2909130f4520SKenneth D. Merry bailout: 2910130f4520SKenneth D. Merry 2911130f4520SKenneth D. Merry return (retval); 2912130f4520SKenneth D. Merry } 2913130f4520SKenneth D. Merry 2914c3e7ba3eSAlexander Motin static uint64_t 2915c3e7ba3eSAlexander Motin ctl_be_block_lun_attr(void *be_lun, const char *attrname) 2916c3e7ba3eSAlexander Motin { 2917c3e7ba3eSAlexander Motin struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2918c3e7ba3eSAlexander Motin 2919c3e7ba3eSAlexander Motin if (lun->getattr == NULL) 2920c3e7ba3eSAlexander Motin return (UINT64_MAX); 2921c3e7ba3eSAlexander Motin return (lun->getattr(lun, attrname)); 2922c3e7ba3eSAlexander Motin } 2923c3e7ba3eSAlexander Motin 2924130f4520SKenneth D. Merry int 2925130f4520SKenneth D. Merry ctl_be_block_init(void) 2926130f4520SKenneth D. Merry { 2927130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2928130f4520SKenneth D. Merry int retval; 2929130f4520SKenneth D. Merry 2930130f4520SKenneth D. Merry softc = &backend_block_softc; 2931130f4520SKenneth D. Merry retval = 0; 2932130f4520SKenneth D. Merry 293375c7a1d3SAlexander Motin mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2934a0e36aeeSEdward Tomasz Napierala beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2935a0e36aeeSEdward Tomasz Napierala NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2936130f4520SKenneth D. Merry STAILQ_INIT(&softc->lun_list); 2937130f4520SKenneth D. Merry 2938130f4520SKenneth D. Merry return (retval); 2939130f4520SKenneth D. Merry } 2940