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_frontend_internal.h> 88130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h> 89130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h> 90130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h> 91130f4520SKenneth D. Merry 92130f4520SKenneth D. Merry /* 9308a7cce5SAlexander Motin * The idea here is that we'll allocate enough S/G space to hold a 1MB 9408a7cce5SAlexander Motin * I/O. If we get an I/O larger than that, we'll split it. 95130f4520SKenneth D. Merry */ 9611b569f7SAlexander Motin #define CTLBLK_HALF_IO_SIZE (512 * 1024) 9711b569f7SAlexander Motin #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 9808a7cce5SAlexander Motin #define CTLBLK_MAX_SEG MAXPHYS 9911b569f7SAlexander Motin #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 10011b569f7SAlexander Motin #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 101130f4520SKenneth D. Merry 102130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG 103130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \ 104130f4520SKenneth D. Merry printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 105130f4520SKenneth D. Merry #else 106130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0) 107130f4520SKenneth D. Merry #endif 108130f4520SKenneth D. Merry 109e86a4142SAlexander Motin #define PRIV(io) \ 110e86a4142SAlexander Motin ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 11111b569f7SAlexander Motin #define ARGS(io) \ 11211b569f7SAlexander Motin ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 113e86a4142SAlexander Motin 114130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb); 115130f4520SKenneth D. Merry 116130f4520SKenneth D. Merry typedef enum { 117130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 118130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 119130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_WAITING = 0x04, 120130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 121130f4520SKenneth D. Merry } ctl_be_block_lun_flags; 122130f4520SKenneth D. Merry 123130f4520SKenneth D. Merry typedef enum { 124130f4520SKenneth D. Merry CTL_BE_BLOCK_NONE, 125130f4520SKenneth D. Merry CTL_BE_BLOCK_DEV, 126130f4520SKenneth D. Merry CTL_BE_BLOCK_FILE 127130f4520SKenneth D. Merry } ctl_be_block_type; 128130f4520SKenneth D. Merry 129130f4520SKenneth D. Merry struct ctl_be_block_devdata { 130130f4520SKenneth D. Merry struct cdev *cdev; 131130f4520SKenneth D. Merry struct cdevsw *csw; 132130f4520SKenneth D. Merry int dev_ref; 133130f4520SKenneth D. Merry }; 134130f4520SKenneth D. Merry 135130f4520SKenneth D. Merry struct ctl_be_block_filedata { 136130f4520SKenneth D. Merry struct ucred *cred; 137130f4520SKenneth D. Merry }; 138130f4520SKenneth D. Merry 139130f4520SKenneth D. Merry union ctl_be_block_bedata { 140130f4520SKenneth D. Merry struct ctl_be_block_devdata dev; 141130f4520SKenneth D. Merry struct ctl_be_block_filedata file; 142130f4520SKenneth D. Merry }; 143130f4520SKenneth D. Merry 144130f4520SKenneth D. Merry struct ctl_be_block_io; 145130f4520SKenneth D. Merry struct ctl_be_block_lun; 146130f4520SKenneth D. Merry 147130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 148130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 149c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 150c3e7ba3eSAlexander Motin const char *attrname); 151130f4520SKenneth D. Merry 152130f4520SKenneth D. Merry /* 153130f4520SKenneth D. Merry * Backend LUN structure. There is a 1:1 mapping between a block device 154130f4520SKenneth D. Merry * and a backend block LUN, and between a backend block LUN and a CTL LUN. 155130f4520SKenneth D. Merry */ 156130f4520SKenneth D. Merry struct ctl_be_block_lun { 15719720f41SAlexander Motin struct ctl_lun_create_params params; 158130f4520SKenneth D. Merry struct ctl_block_disk *disk; 159130f4520SKenneth D. Merry char lunname[32]; 160130f4520SKenneth D. Merry char *dev_path; 161130f4520SKenneth D. Merry ctl_be_block_type dev_type; 162130f4520SKenneth D. Merry struct vnode *vn; 163130f4520SKenneth D. Merry union ctl_be_block_bedata backend; 164130f4520SKenneth D. Merry cbb_dispatch_t dispatch; 165130f4520SKenneth D. Merry cbb_dispatch_t lun_flush; 166ee7f31c0SAlexander Motin cbb_dispatch_t unmap; 167ef8daf3fSAlexander Motin cbb_dispatch_t get_lba_status; 168c3e7ba3eSAlexander Motin cbb_getattr_t getattr; 169130f4520SKenneth D. Merry uma_zone_t lun_zone; 170130f4520SKenneth D. Merry uint64_t size_blocks; 171130f4520SKenneth D. Merry uint64_t size_bytes; 172130f4520SKenneth D. Merry uint32_t blocksize; 173130f4520SKenneth D. Merry int blocksize_shift; 174f6012722SAlexander Motin uint16_t pblockexp; 175f6012722SAlexander Motin uint16_t pblockoff; 176130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 177130f4520SKenneth D. Merry struct devstat *disk_stats; 178130f4520SKenneth D. Merry ctl_be_block_lun_flags flags; 179130f4520SKenneth D. Merry STAILQ_ENTRY(ctl_be_block_lun) links; 180130f4520SKenneth D. Merry struct ctl_be_lun ctl_be_lun; 181130f4520SKenneth D. Merry struct taskqueue *io_taskqueue; 182130f4520SKenneth D. Merry struct task io_task; 183130f4520SKenneth D. Merry int num_threads; 184130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) input_queue; 185ef8daf3fSAlexander Motin STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 186130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 187130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 18875c7a1d3SAlexander Motin struct mtx_padalign io_lock; 18975c7a1d3SAlexander Motin struct mtx_padalign queue_lock; 190130f4520SKenneth D. Merry }; 191130f4520SKenneth D. Merry 192130f4520SKenneth D. Merry /* 193130f4520SKenneth D. Merry * Overall softc structure for the block backend module. 194130f4520SKenneth D. Merry */ 195130f4520SKenneth D. Merry struct ctl_be_block_softc { 196130f4520SKenneth D. Merry struct mtx lock; 197130f4520SKenneth D. Merry int num_disks; 198130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_block_disk) disk_list; 199130f4520SKenneth D. Merry int num_luns; 200130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_be_block_lun) lun_list; 201130f4520SKenneth D. Merry }; 202130f4520SKenneth D. Merry 203130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc; 204130f4520SKenneth D. Merry 205130f4520SKenneth D. Merry /* 206130f4520SKenneth D. Merry * Per-I/O information. 207130f4520SKenneth D. Merry */ 208130f4520SKenneth D. Merry struct ctl_be_block_io { 209130f4520SKenneth D. Merry union ctl_io *io; 210130f4520SKenneth D. Merry struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 211130f4520SKenneth D. Merry struct iovec xiovecs[CTLBLK_MAX_SEGS]; 212130f4520SKenneth D. Merry int bio_cmd; 213130f4520SKenneth D. Merry int num_segs; 214130f4520SKenneth D. Merry int num_bios_sent; 215130f4520SKenneth D. Merry int num_bios_done; 216130f4520SKenneth D. Merry int send_complete; 217130f4520SKenneth D. Merry int num_errors; 218130f4520SKenneth D. Merry struct bintime ds_t0; 219130f4520SKenneth D. Merry devstat_tag_type ds_tag_type; 220130f4520SKenneth D. Merry devstat_trans_flags ds_trans_type; 221130f4520SKenneth D. Merry uint64_t io_len; 222130f4520SKenneth D. Merry uint64_t io_offset; 223130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 224130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 225ee7f31c0SAlexander Motin void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 226130f4520SKenneth D. Merry }; 227130f4520SKenneth D. Merry 228130f4520SKenneth D. Merry static int cbb_num_threads = 14; 229130f4520SKenneth D. Merry SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 230130f4520SKenneth D. Merry "CAM Target Layer Block Backend"); 231af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 232130f4520SKenneth D. Merry &cbb_num_threads, 0, "Number of threads per backing file"); 233130f4520SKenneth D. Merry 234130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 235130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio); 236130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio); 237130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io); 238130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio); 239130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 240130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 241130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 242130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 243ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 244ef8daf3fSAlexander Motin struct ctl_be_block_io *beio); 245*53c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 246*53c146deSAlexander Motin const char *attrname); 247130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 248130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 249ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 250ee7f31c0SAlexander Motin struct ctl_be_block_io *beio); 251130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 252130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 253c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 254c3e7ba3eSAlexander Motin const char *attrname); 255ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 256ef8daf3fSAlexander Motin union ctl_io *io); 257130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 258130f4520SKenneth D. Merry union ctl_io *io); 259130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 260130f4520SKenneth D. Merry union ctl_io *io); 261130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending); 262130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io); 263130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 264130f4520SKenneth D. Merry int flag, struct thread *td); 265130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 266130f4520SKenneth D. Merry struct ctl_lun_req *req); 267130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 268130f4520SKenneth D. Merry struct ctl_lun_req *req); 269130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 270130f4520SKenneth D. Merry static int ctl_be_block_open(struct ctl_be_block_softc *softc, 271130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, 272130f4520SKenneth D. Merry struct ctl_lun_req *req); 273130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc, 274130f4520SKenneth D. Merry struct ctl_lun_req *req); 275130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 276130f4520SKenneth D. Merry struct ctl_lun_req *req); 27781177295SEdward Tomasz Napierala static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 27881177295SEdward Tomasz Napierala struct ctl_lun_req *req); 27981177295SEdward Tomasz Napierala static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 28081177295SEdward Tomasz Napierala struct ctl_lun_req *req); 28181177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 28281177295SEdward Tomasz Napierala struct ctl_lun_req *req); 283130f4520SKenneth D. Merry static void ctl_be_block_lun_shutdown(void *be_lun); 284130f4520SKenneth D. Merry static void ctl_be_block_lun_config_status(void *be_lun, 285130f4520SKenneth D. Merry ctl_lun_config_status status); 286130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io); 287130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io); 288130f4520SKenneth D. Merry static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 289c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 290130f4520SKenneth D. Merry int ctl_be_block_init(void); 291130f4520SKenneth D. Merry 292130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver = 293130f4520SKenneth D. Merry { 2942a2443d8SKenneth D. Merry .name = "block", 2952a2443d8SKenneth D. Merry .flags = CTL_BE_FLAG_HAS_CONFIG, 2962a2443d8SKenneth D. Merry .init = ctl_be_block_init, 2972a2443d8SKenneth D. Merry .data_submit = ctl_be_block_submit, 2982a2443d8SKenneth D. Merry .data_move_done = ctl_be_block_move_done, 2992a2443d8SKenneth D. Merry .config_read = ctl_be_block_config_read, 3002a2443d8SKenneth D. Merry .config_write = ctl_be_block_config_write, 3012a2443d8SKenneth D. Merry .ioctl = ctl_be_block_ioctl, 302c3e7ba3eSAlexander Motin .lun_info = ctl_be_block_lun_info, 303c3e7ba3eSAlexander Motin .lun_attr = ctl_be_block_lun_attr 304130f4520SKenneth D. Merry }; 305130f4520SKenneth D. Merry 306130f4520SKenneth D. Merry MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 307130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 308130f4520SKenneth D. Merry 309a0e36aeeSEdward Tomasz Napierala static uma_zone_t beio_zone; 310a0e36aeeSEdward Tomasz Napierala 311130f4520SKenneth D. Merry static struct ctl_be_block_io * 312130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc) 313130f4520SKenneth D. Merry { 314130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 315130f4520SKenneth D. Merry 316a0e36aeeSEdward Tomasz Napierala beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 317130f4520SKenneth D. Merry beio->softc = softc; 318130f4520SKenneth D. Merry return (beio); 319130f4520SKenneth D. Merry } 320130f4520SKenneth D. Merry 321130f4520SKenneth D. Merry static void 322130f4520SKenneth D. Merry ctl_free_beio(struct ctl_be_block_io *beio) 323130f4520SKenneth D. Merry { 324130f4520SKenneth D. Merry int duplicate_free; 325130f4520SKenneth D. Merry int i; 326130f4520SKenneth D. Merry 327130f4520SKenneth D. Merry duplicate_free = 0; 328130f4520SKenneth D. Merry 329130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 330130f4520SKenneth D. Merry if (beio->sg_segs[i].addr == NULL) 331130f4520SKenneth D. Merry duplicate_free++; 332130f4520SKenneth D. Merry 333130f4520SKenneth D. Merry uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 334130f4520SKenneth D. Merry beio->sg_segs[i].addr = NULL; 33511b569f7SAlexander Motin 33611b569f7SAlexander Motin /* For compare we had two equal S/G lists. */ 33711b569f7SAlexander Motin if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 33811b569f7SAlexander Motin uma_zfree(beio->lun->lun_zone, 33911b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 34011b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 34111b569f7SAlexander Motin } 342130f4520SKenneth D. Merry } 343130f4520SKenneth D. Merry 344130f4520SKenneth D. Merry if (duplicate_free > 0) { 345130f4520SKenneth D. Merry printf("%s: %d duplicate frees out of %d segments\n", __func__, 346130f4520SKenneth D. Merry duplicate_free, beio->num_segs); 347130f4520SKenneth D. Merry } 348a0e36aeeSEdward Tomasz Napierala 349a0e36aeeSEdward Tomasz Napierala uma_zfree(beio_zone, beio); 350130f4520SKenneth D. Merry } 351130f4520SKenneth D. Merry 352130f4520SKenneth D. Merry static void 353130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio) 354130f4520SKenneth D. Merry { 35575c7a1d3SAlexander Motin union ctl_io *io = beio->io; 356130f4520SKenneth D. Merry 357ee7f31c0SAlexander Motin if (beio->beio_cont != NULL) { 358ee7f31c0SAlexander Motin beio->beio_cont(beio); 359ee7f31c0SAlexander Motin } else { 360130f4520SKenneth D. Merry ctl_free_beio(beio); 36111b569f7SAlexander Motin ctl_data_submit_done(io); 362130f4520SKenneth D. Merry } 363ee7f31c0SAlexander Motin } 364130f4520SKenneth D. Merry 365130f4520SKenneth D. Merry static int 366130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io) 367130f4520SKenneth D. Merry { 368130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 369130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 37011b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 371130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 372130f4520SKenneth D. Merry struct bintime cur_bt; 373130f4520SKenneth D. Merry #endif 37411b569f7SAlexander Motin int i; 375130f4520SKenneth D. Merry 376e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 377130f4520SKenneth D. Merry be_lun = beio->lun; 378130f4520SKenneth D. Merry 379130f4520SKenneth D. Merry DPRINTF("entered\n"); 380130f4520SKenneth D. Merry 381130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 382130f4520SKenneth D. Merry getbintime(&cur_bt); 383130f4520SKenneth D. Merry bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 384130f4520SKenneth D. Merry bintime_add(&io->io_hdr.dma_bt, &cur_bt); 385130f4520SKenneth D. Merry io->io_hdr.num_dmas++; 386130f4520SKenneth D. Merry #endif 38711b569f7SAlexander Motin io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 388130f4520SKenneth D. Merry 389130f4520SKenneth D. Merry /* 390130f4520SKenneth D. Merry * We set status at this point for read commands, and write 391130f4520SKenneth D. Merry * commands with errors. 392130f4520SKenneth D. Merry */ 393f7241cceSAlexander Motin if (io->io_hdr.flags & CTL_FLAG_ABORT) { 394f7241cceSAlexander Motin ; 395f7241cceSAlexander Motin } else if ((io->io_hdr.port_status == 0) && 39611b569f7SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 39711b569f7SAlexander Motin lbalen = ARGS(beio->io); 39811b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_READ) { 399130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 40011b569f7SAlexander Motin } else if (lbalen->flags & CTL_LLF_COMPARE) { 40111b569f7SAlexander Motin /* We have two data blocks ready for comparison. */ 40211b569f7SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 40311b569f7SAlexander Motin if (memcmp(beio->sg_segs[i].addr, 40411b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 40511b569f7SAlexander Motin beio->sg_segs[i].len) != 0) 40611b569f7SAlexander Motin break; 40711b569f7SAlexander Motin } 40811b569f7SAlexander Motin if (i < beio->num_segs) 40911b569f7SAlexander Motin ctl_set_sense(&io->scsiio, 41011b569f7SAlexander Motin /*current_error*/ 1, 41111b569f7SAlexander Motin /*sense_key*/ SSD_KEY_MISCOMPARE, 41211b569f7SAlexander Motin /*asc*/ 0x1D, 41311b569f7SAlexander Motin /*ascq*/ 0x00, 41411b569f7SAlexander Motin SSD_ELEM_NONE); 41511b569f7SAlexander Motin else 41611b569f7SAlexander Motin ctl_set_success(&io->scsiio); 41711b569f7SAlexander Motin } 418f7241cceSAlexander Motin } else if ((io->io_hdr.port_status != 0) && 419f7241cceSAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 420f7241cceSAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 421130f4520SKenneth D. Merry /* 422130f4520SKenneth D. Merry * For hardware error sense keys, the sense key 423130f4520SKenneth D. Merry * specific value is defined to be a retry count, 424130f4520SKenneth D. Merry * but we use it to pass back an internal FETD 425130f4520SKenneth D. Merry * error code. XXX KDM Hopefully the FETD is only 426130f4520SKenneth D. Merry * using 16 bits for an error code, since that's 427130f4520SKenneth D. Merry * all the space we have in the sks field. 428130f4520SKenneth D. Merry */ 429130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 430130f4520SKenneth D. Merry /*sks_valid*/ 1, 431130f4520SKenneth D. Merry /*retry_count*/ 432130f4520SKenneth D. Merry io->io_hdr.port_status); 433130f4520SKenneth D. Merry } 434130f4520SKenneth D. Merry 435130f4520SKenneth D. Merry /* 436130f4520SKenneth D. Merry * If this is a read, or a write with errors, it is done. 437130f4520SKenneth D. Merry */ 438130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_READ) 439130f4520SKenneth D. Merry || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 440130f4520SKenneth D. Merry || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 441130f4520SKenneth D. Merry ctl_complete_beio(beio); 442130f4520SKenneth D. Merry return (0); 443130f4520SKenneth D. Merry } 444130f4520SKenneth D. Merry 445130f4520SKenneth D. Merry /* 446130f4520SKenneth D. Merry * At this point, we have a write and the DMA completed 447130f4520SKenneth D. Merry * successfully. We now have to queue it to the task queue to 448130f4520SKenneth D. Merry * execute the backend I/O. That is because we do blocking 449130f4520SKenneth D. Merry * memory allocations, and in the file backing case, blocking I/O. 450130f4520SKenneth D. Merry * This move done routine is generally called in the SIM's 451130f4520SKenneth D. Merry * interrupt context, and therefore we cannot block. 452130f4520SKenneth D. Merry */ 45375c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 454130f4520SKenneth D. Merry /* 455130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 456130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 457130f4520SKenneth D. Merry * or deal with resource allocation here. 458130f4520SKenneth D. Merry */ 459130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 46075c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 461130f4520SKenneth D. Merry 462130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 463130f4520SKenneth D. Merry 464130f4520SKenneth D. Merry return (0); 465130f4520SKenneth D. Merry } 466130f4520SKenneth D. Merry 467130f4520SKenneth D. Merry static void 468130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio) 469130f4520SKenneth D. Merry { 470130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 471130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 472130f4520SKenneth D. Merry union ctl_io *io; 473e0c2f975SAlexander Motin int error; 474130f4520SKenneth D. Merry 475130f4520SKenneth D. Merry beio = bio->bio_caller1; 476130f4520SKenneth D. Merry be_lun = beio->lun; 477130f4520SKenneth D. Merry io = beio->io; 478130f4520SKenneth D. Merry 479130f4520SKenneth D. Merry DPRINTF("entered\n"); 480130f4520SKenneth D. Merry 481e0c2f975SAlexander Motin error = bio->bio_error; 48275c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 483e0c2f975SAlexander Motin if (error != 0) 484130f4520SKenneth D. Merry beio->num_errors++; 485130f4520SKenneth D. Merry 486130f4520SKenneth D. Merry beio->num_bios_done++; 487130f4520SKenneth D. Merry 488130f4520SKenneth D. Merry /* 489130f4520SKenneth D. Merry * XXX KDM will this cause WITNESS to complain? Holding a lock 490130f4520SKenneth D. Merry * during the free might cause it to complain. 491130f4520SKenneth D. Merry */ 492130f4520SKenneth D. Merry g_destroy_bio(bio); 493130f4520SKenneth D. Merry 494130f4520SKenneth D. Merry /* 495130f4520SKenneth D. Merry * If the send complete bit isn't set, or we aren't the last I/O to 496130f4520SKenneth D. Merry * complete, then we're done. 497130f4520SKenneth D. Merry */ 498130f4520SKenneth D. Merry if ((beio->send_complete == 0) 499130f4520SKenneth D. Merry || (beio->num_bios_done < beio->num_bios_sent)) { 50075c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 501130f4520SKenneth D. Merry return; 502130f4520SKenneth D. Merry } 503130f4520SKenneth D. Merry 504130f4520SKenneth D. Merry /* 505130f4520SKenneth D. Merry * At this point, we've verified that we are the last I/O to 506130f4520SKenneth D. Merry * complete, so it's safe to drop the lock. 507130f4520SKenneth D. Merry */ 50875c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 50975c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 51075c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 51175c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 512130f4520SKenneth D. Merry 513130f4520SKenneth D. Merry /* 514130f4520SKenneth D. Merry * If there are any errors from the backing device, we fail the 515130f4520SKenneth D. Merry * entire I/O with a medium error. 516130f4520SKenneth D. Merry */ 517130f4520SKenneth D. Merry if (beio->num_errors > 0) { 518e0c2f975SAlexander Motin if (error == EOPNOTSUPP) { 519e0c2f975SAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 5204fc18ff9SAlexander Motin } else if (error == ENOSPC) { 5214fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 522e0c2f975SAlexander Motin } else if (beio->bio_cmd == BIO_FLUSH) { 523130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 524130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 525130f4520SKenneth D. Merry /*sks_valid*/ 1, 526130f4520SKenneth D. Merry /*retry_count*/ 0xbad2); 527130f4520SKenneth D. Merry } else 528130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 529130f4520SKenneth D. Merry ctl_complete_beio(beio); 530130f4520SKenneth D. Merry return; 531130f4520SKenneth D. Merry } 532130f4520SKenneth D. Merry 533130f4520SKenneth D. Merry /* 53411b569f7SAlexander Motin * If this is a write, a flush, a delete or verify, we're all done. 535130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 536130f4520SKenneth D. Merry */ 537130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_WRITE) 538ee7f31c0SAlexander Motin || (beio->bio_cmd == BIO_FLUSH) 53911b569f7SAlexander Motin || (beio->bio_cmd == BIO_DELETE) 54011b569f7SAlexander Motin || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 541130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 542130f4520SKenneth D. Merry ctl_complete_beio(beio); 543130f4520SKenneth D. Merry } else { 544f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 545f7241cceSAlexander Motin beio->beio_cont == NULL) 546f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 547130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 548130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 549130f4520SKenneth D. Merry #endif 550130f4520SKenneth D. Merry ctl_datamove(io); 551130f4520SKenneth D. Merry } 552130f4520SKenneth D. Merry } 553130f4520SKenneth D. Merry 554130f4520SKenneth D. Merry static void 555130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 556130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 557130f4520SKenneth D. Merry { 55875c7a1d3SAlexander Motin union ctl_io *io = beio->io; 559130f4520SKenneth D. Merry struct mount *mountpoint; 5605050aa86SKonstantin Belousov int error, lock_flags; 561130f4520SKenneth D. Merry 562130f4520SKenneth D. Merry DPRINTF("entered\n"); 563130f4520SKenneth D. Merry 56475c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 56575c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 56675c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 56775c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 568130f4520SKenneth D. Merry 569130f4520SKenneth D. Merry (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 570130f4520SKenneth D. Merry 571130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 572130f4520SKenneth D. Merry || ((mountpoint == NULL) 573130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 574130f4520SKenneth D. Merry lock_flags = LK_SHARED; 575130f4520SKenneth D. Merry else 576130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 577130f4520SKenneth D. Merry 578130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 579130f4520SKenneth D. Merry 580130f4520SKenneth D. Merry error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 581130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 582130f4520SKenneth D. Merry 583130f4520SKenneth D. Merry vn_finished_write(mountpoint); 584130f4520SKenneth D. Merry 58575c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 58675c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 58775c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 58875c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 58975c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 59075c7a1d3SAlexander Motin 591130f4520SKenneth D. Merry if (error == 0) 592130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 593130f4520SKenneth D. Merry else { 594130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 595130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 596130f4520SKenneth D. Merry /*sks_valid*/ 1, 597130f4520SKenneth D. Merry /*retry_count*/ 0xbad1); 598130f4520SKenneth D. Merry } 599130f4520SKenneth D. Merry 600130f4520SKenneth D. Merry ctl_complete_beio(beio); 601130f4520SKenneth D. Merry } 602130f4520SKenneth D. Merry 603d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 604d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 605d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 606d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 607130f4520SKenneth D. Merry 608130f4520SKenneth D. Merry static void 609130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 610130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 611130f4520SKenneth D. Merry { 612130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 613130f4520SKenneth D. Merry union ctl_io *io; 614130f4520SKenneth D. Merry struct uio xuio; 615130f4520SKenneth D. Merry struct iovec *xiovec; 6165050aa86SKonstantin Belousov int flags; 617130f4520SKenneth D. Merry int error, i; 618130f4520SKenneth D. Merry 619130f4520SKenneth D. Merry DPRINTF("entered\n"); 620130f4520SKenneth D. Merry 621130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 622130f4520SKenneth D. Merry io = beio->io; 62355551d05SAlexander Motin flags = 0; 62455551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 62555551d05SAlexander Motin flags |= IO_DIRECT; 62655551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 62755551d05SAlexander Motin flags |= IO_SYNC; 628130f4520SKenneth D. Merry 62911b569f7SAlexander Motin bzero(&xuio, sizeof(xuio)); 630130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 631130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 63211b569f7SAlexander Motin xuio.uio_rw = UIO_READ; 633130f4520SKenneth D. Merry } else { 634130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 635130f4520SKenneth D. Merry xuio.uio_rw = UIO_WRITE; 63611b569f7SAlexander Motin } 637130f4520SKenneth D. Merry xuio.uio_offset = beio->io_offset; 638130f4520SKenneth D. Merry xuio.uio_resid = beio->io_len; 639130f4520SKenneth D. Merry xuio.uio_segflg = UIO_SYSSPACE; 640130f4520SKenneth D. Merry xuio.uio_iov = beio->xiovecs; 641130f4520SKenneth D. Merry xuio.uio_iovcnt = beio->num_segs; 642130f4520SKenneth D. Merry xuio.uio_td = curthread; 643130f4520SKenneth D. Merry 644130f4520SKenneth D. Merry for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 645130f4520SKenneth D. Merry xiovec->iov_base = beio->sg_segs[i].addr; 646130f4520SKenneth D. Merry xiovec->iov_len = beio->sg_segs[i].len; 647130f4520SKenneth D. Merry } 648130f4520SKenneth D. Merry 64975c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 65075c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 65175c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 65275c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 65375c7a1d3SAlexander Motin 654130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 655130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 656130f4520SKenneth D. Merry 657130f4520SKenneth D. Merry /* 658130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for reads. If the 659130f4520SKenneth D. Merry * DIRECTIO option is configured into the kernel, it calls 660130f4520SKenneth D. Merry * ffs_rawread(). But that only works for single-segment 661130f4520SKenneth D. Merry * uios with user space addresses. In our case, with a 662130f4520SKenneth D. Merry * kernel uio, it still reads into the buffer cache, but it 663130f4520SKenneth D. Merry * will just try to release the buffer from the cache later 664130f4520SKenneth D. Merry * on in ffs_read(). 665130f4520SKenneth D. Merry * 666130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for reads. 667130f4520SKenneth D. Merry * 668130f4520SKenneth D. Merry * UFS does not pay attention to IO_SYNC for reads. 669130f4520SKenneth D. Merry * 670130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (which translates into the 671130f4520SKenneth D. Merry * Solaris define FRSYNC for zfs_read()) for reads. It 672130f4520SKenneth D. Merry * attempts to sync the file before reading. 673130f4520SKenneth D. Merry * 674130f4520SKenneth D. Merry * So, to attempt to provide some barrier semantics in the 675130f4520SKenneth D. Merry * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 676130f4520SKenneth D. Merry */ 67755551d05SAlexander Motin error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 678130f4520SKenneth D. Merry 679130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 68011b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 681130f4520SKenneth D. Merry } else { 682130f4520SKenneth D. Merry struct mount *mountpoint; 683130f4520SKenneth D. Merry int lock_flags; 684130f4520SKenneth D. Merry 685130f4520SKenneth D. Merry (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 686130f4520SKenneth D. Merry 687130f4520SKenneth D. Merry if (MNT_SHARED_WRITES(mountpoint) 688130f4520SKenneth D. Merry || ((mountpoint == NULL) 689130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 690130f4520SKenneth D. Merry lock_flags = LK_SHARED; 691130f4520SKenneth D. Merry else 692130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 693130f4520SKenneth D. Merry 694130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 695130f4520SKenneth D. Merry 696130f4520SKenneth D. Merry /* 697130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for writes. The write 698130f4520SKenneth D. Merry * is done asynchronously. (Normally the write would just 699130f4520SKenneth D. Merry * get put into cache. 700130f4520SKenneth D. Merry * 701130f4520SKenneth D. Merry * UFS pays attention to IO_SYNC for writes. It will 702130f4520SKenneth D. Merry * attempt to write the buffer out synchronously if that 703130f4520SKenneth D. Merry * flag is set. 704130f4520SKenneth D. Merry * 705130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for writes. 706130f4520SKenneth D. Merry * 707130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 708130f4520SKenneth D. Merry * for writes. It will flush the transaction from the 709130f4520SKenneth D. Merry * cache before returning. 710130f4520SKenneth D. Merry * 711130f4520SKenneth D. Merry * So if we've got the BIO_ORDERED flag set, we want 712130f4520SKenneth D. Merry * IO_SYNC in either the UFS or ZFS case. 713130f4520SKenneth D. Merry */ 71455551d05SAlexander Motin error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 715130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 716130f4520SKenneth D. Merry 717130f4520SKenneth D. Merry vn_finished_write(mountpoint); 71811b569f7SAlexander Motin SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 719130f4520SKenneth D. Merry } 720130f4520SKenneth D. Merry 72175c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 72275c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 72375c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 72475c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 72575c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 72675c7a1d3SAlexander Motin 727130f4520SKenneth D. Merry /* 728130f4520SKenneth D. Merry * If we got an error, set the sense data to "MEDIUM ERROR" and 729130f4520SKenneth D. Merry * return the I/O to the user. 730130f4520SKenneth D. Merry */ 731130f4520SKenneth D. Merry if (error != 0) { 732130f4520SKenneth D. Merry char path_str[32]; 733130f4520SKenneth D. Merry 734130f4520SKenneth D. Merry ctl_scsi_path_string(io, path_str, sizeof(path_str)); 735130f4520SKenneth D. Merry printf("%s%s command returned errno %d\n", path_str, 736130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 7374fc18ff9SAlexander Motin if (error == ENOSPC) { 7384fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 7394fc18ff9SAlexander Motin } else 740130f4520SKenneth D. Merry ctl_set_medium_error(&io->scsiio); 741130f4520SKenneth D. Merry ctl_complete_beio(beio); 742130f4520SKenneth D. Merry return; 743130f4520SKenneth D. Merry } 744130f4520SKenneth D. Merry 745130f4520SKenneth D. Merry /* 746696297adSAlexander Motin * If this is a write or a verify, we're all done. 747130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 748130f4520SKenneth D. Merry */ 749696297adSAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 750696297adSAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 751130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 752130f4520SKenneth D. Merry ctl_complete_beio(beio); 753130f4520SKenneth D. Merry } else { 754f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 755f7241cceSAlexander Motin beio->beio_cont == NULL) 756f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 757130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 758130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 759130f4520SKenneth D. Merry #endif 760130f4520SKenneth D. Merry ctl_datamove(io); 761130f4520SKenneth D. Merry } 762130f4520SKenneth D. Merry } 763130f4520SKenneth D. Merry 764130f4520SKenneth D. Merry static void 765ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 766ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 767ef8daf3fSAlexander Motin { 768ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 769ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 770ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 771ef8daf3fSAlexander Motin off_t roff, off; 772ef8daf3fSAlexander Motin int error, status; 773ef8daf3fSAlexander Motin 774ef8daf3fSAlexander Motin DPRINTF("entered\n"); 775ef8daf3fSAlexander Motin 776ef8daf3fSAlexander Motin off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 777ef8daf3fSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 778ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 779ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 780ef8daf3fSAlexander Motin if (error == 0 && off > roff) 781ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 782ef8daf3fSAlexander Motin else { 783ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 784ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 785ef8daf3fSAlexander Motin if (error == 0 && off > roff) 786ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 787ef8daf3fSAlexander Motin else { 788ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 789ef8daf3fSAlexander Motin off = be_lun->size_bytes; 790ef8daf3fSAlexander Motin } 791ef8daf3fSAlexander Motin } 792ef8daf3fSAlexander Motin VOP_UNLOCK(be_lun->vn, 0); 793ef8daf3fSAlexander Motin 794ef8daf3fSAlexander Motin off >>= be_lun->blocksize_shift; 795ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 796ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 797ef8daf3fSAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 798ef8daf3fSAlexander Motin data->descr[0].length); 799ef8daf3fSAlexander Motin data->descr[0].status = status; 800ef8daf3fSAlexander Motin 801ef8daf3fSAlexander Motin ctl_complete_beio(beio); 802ef8daf3fSAlexander Motin } 803ef8daf3fSAlexander Motin 804*53c146deSAlexander Motin static uint64_t 805*53c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 806*53c146deSAlexander Motin { 807*53c146deSAlexander Motin struct vattr vattr; 808*53c146deSAlexander Motin struct statfs statfs; 809*53c146deSAlexander Motin int error; 810*53c146deSAlexander Motin 811*53c146deSAlexander Motin if (be_lun->vn == NULL) 812*53c146deSAlexander Motin return (UINT64_MAX); 813*53c146deSAlexander Motin if (strcmp(attrname, "blocksused") == 0) { 814*53c146deSAlexander Motin error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 815*53c146deSAlexander Motin if (error != 0) 816*53c146deSAlexander Motin return (UINT64_MAX); 817*53c146deSAlexander Motin return (vattr.va_bytes >> be_lun->blocksize_shift); 818*53c146deSAlexander Motin } 819*53c146deSAlexander Motin if (strcmp(attrname, "blocksavail") == 0) { 820*53c146deSAlexander Motin error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 821*53c146deSAlexander Motin if (error != 0) 822*53c146deSAlexander Motin return (UINT64_MAX); 823*53c146deSAlexander Motin return ((statfs.f_bavail * statfs.f_bsize) >> 824*53c146deSAlexander Motin be_lun->blocksize_shift); 825*53c146deSAlexander Motin } 826*53c146deSAlexander Motin return (UINT64_MAX); 827*53c146deSAlexander Motin } 828*53c146deSAlexander Motin 829ef8daf3fSAlexander Motin static void 83067f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 83167f586a8SAlexander Motin struct ctl_be_block_io *beio) 83267f586a8SAlexander Motin { 83367f586a8SAlexander Motin struct ctl_be_block_devdata *dev_data; 83467f586a8SAlexander Motin union ctl_io *io; 83567f586a8SAlexander Motin struct uio xuio; 83667f586a8SAlexander Motin struct iovec *xiovec; 83767f586a8SAlexander Motin int flags; 83867f586a8SAlexander Motin int error, i; 83967f586a8SAlexander Motin 84067f586a8SAlexander Motin DPRINTF("entered\n"); 84167f586a8SAlexander Motin 84267f586a8SAlexander Motin dev_data = &be_lun->backend.dev; 84367f586a8SAlexander Motin io = beio->io; 84455551d05SAlexander Motin flags = 0; 84555551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 84655551d05SAlexander Motin flags |= IO_DIRECT; 84755551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 84855551d05SAlexander Motin flags |= IO_SYNC; 84967f586a8SAlexander Motin 85067f586a8SAlexander Motin bzero(&xuio, sizeof(xuio)); 85167f586a8SAlexander Motin if (beio->bio_cmd == BIO_READ) { 85267f586a8SAlexander Motin SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 85367f586a8SAlexander Motin xuio.uio_rw = UIO_READ; 85467f586a8SAlexander Motin } else { 85567f586a8SAlexander Motin SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 85667f586a8SAlexander Motin xuio.uio_rw = UIO_WRITE; 85767f586a8SAlexander Motin } 85867f586a8SAlexander Motin xuio.uio_offset = beio->io_offset; 85967f586a8SAlexander Motin xuio.uio_resid = beio->io_len; 86067f586a8SAlexander Motin xuio.uio_segflg = UIO_SYSSPACE; 86167f586a8SAlexander Motin xuio.uio_iov = beio->xiovecs; 86267f586a8SAlexander Motin xuio.uio_iovcnt = beio->num_segs; 86367f586a8SAlexander Motin xuio.uio_td = curthread; 86467f586a8SAlexander Motin 86567f586a8SAlexander Motin for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 86667f586a8SAlexander Motin xiovec->iov_base = beio->sg_segs[i].addr; 86767f586a8SAlexander Motin xiovec->iov_len = beio->sg_segs[i].len; 86867f586a8SAlexander Motin } 86967f586a8SAlexander Motin 87067f586a8SAlexander Motin binuptime(&beio->ds_t0); 87167f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 87267f586a8SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 87367f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 87467f586a8SAlexander Motin 87567f586a8SAlexander Motin if (beio->bio_cmd == BIO_READ) { 87655551d05SAlexander Motin error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 87767f586a8SAlexander Motin SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 87867f586a8SAlexander Motin } else { 87955551d05SAlexander Motin error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 88067f586a8SAlexander Motin SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 88167f586a8SAlexander Motin } 88267f586a8SAlexander Motin 88367f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 88467f586a8SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 88567f586a8SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 88667f586a8SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 88767f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 88867f586a8SAlexander Motin 88967f586a8SAlexander Motin /* 89067f586a8SAlexander Motin * If we got an error, set the sense data to "MEDIUM ERROR" and 89167f586a8SAlexander Motin * return the I/O to the user. 89267f586a8SAlexander Motin */ 89367f586a8SAlexander Motin if (error != 0) { 8944fc18ff9SAlexander Motin if (error == ENOSPC) { 8954fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 8964fc18ff9SAlexander Motin } else 89767f586a8SAlexander Motin ctl_set_medium_error(&io->scsiio); 89867f586a8SAlexander Motin ctl_complete_beio(beio); 89967f586a8SAlexander Motin return; 90067f586a8SAlexander Motin } 90167f586a8SAlexander Motin 90267f586a8SAlexander Motin /* 90367f586a8SAlexander Motin * If this is a write or a verify, we're all done. 90467f586a8SAlexander Motin * If this is a read, we can now send the data to the user. 90567f586a8SAlexander Motin */ 90667f586a8SAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 90767f586a8SAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 90867f586a8SAlexander Motin ctl_set_success(&io->scsiio); 90967f586a8SAlexander Motin ctl_complete_beio(beio); 91067f586a8SAlexander Motin } else { 911f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 912f7241cceSAlexander Motin beio->beio_cont == NULL) 913f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 91467f586a8SAlexander Motin #ifdef CTL_TIME_IO 91567f586a8SAlexander Motin getbintime(&io->io_hdr.dma_start_bt); 91667f586a8SAlexander Motin #endif 91767f586a8SAlexander Motin ctl_datamove(io); 91867f586a8SAlexander Motin } 91967f586a8SAlexander Motin } 92067f586a8SAlexander Motin 92167f586a8SAlexander Motin static void 922ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 923ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 924ef8daf3fSAlexander Motin { 925ef8daf3fSAlexander Motin struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 926ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 927ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 928ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 929ef8daf3fSAlexander Motin off_t roff, off; 930ef8daf3fSAlexander Motin int error, status; 931ef8daf3fSAlexander Motin 932ef8daf3fSAlexander Motin DPRINTF("entered\n"); 933ef8daf3fSAlexander Motin 934ef8daf3fSAlexander Motin off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift; 935ef8daf3fSAlexander Motin error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE, 936ef8daf3fSAlexander Motin (caddr_t)&off, FREAD, curthread); 937ef8daf3fSAlexander Motin if (error == 0 && off > roff) 938ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 939ef8daf3fSAlexander Motin else { 940ef8daf3fSAlexander Motin error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA, 941ef8daf3fSAlexander Motin (caddr_t)&off, FREAD, curthread); 942ef8daf3fSAlexander Motin if (error == 0 && off > roff) 943ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 944ef8daf3fSAlexander Motin else { 945ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 946ef8daf3fSAlexander Motin off = be_lun->size_bytes; 947ef8daf3fSAlexander Motin } 948ef8daf3fSAlexander Motin } 949ef8daf3fSAlexander Motin 950ef8daf3fSAlexander Motin off >>= be_lun->blocksize_shift; 951ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 952ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 953ef8daf3fSAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba), 954ef8daf3fSAlexander Motin 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; 966130f4520SKenneth D. Merry struct ctl_be_block_devdata *dev_data; 967130f4520SKenneth D. Merry 968130f4520SKenneth D. Merry dev_data = &be_lun->backend.dev; 969130f4520SKenneth D. Merry io = beio->io; 970130f4520SKenneth D. Merry 971130f4520SKenneth D. Merry DPRINTF("entered\n"); 972130f4520SKenneth D. Merry 973130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 974130f4520SKenneth D. Merry bio = g_alloc_bio(); 975130f4520SKenneth D. Merry 976130f4520SKenneth D. Merry bio->bio_cmd = BIO_FLUSH; 977130f4520SKenneth D. Merry bio->bio_flags |= BIO_ORDERED; 978130f4520SKenneth D. Merry bio->bio_dev = dev_data->cdev; 979130f4520SKenneth D. Merry bio->bio_offset = 0; 980130f4520SKenneth D. Merry bio->bio_data = 0; 981130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 982130f4520SKenneth D. Merry bio->bio_caller1 = beio; 983130f4520SKenneth D. Merry bio->bio_pblkno = 0; 984130f4520SKenneth D. Merry 985130f4520SKenneth D. Merry /* 986130f4520SKenneth D. Merry * We don't need to acquire the LUN lock here, because we are only 987130f4520SKenneth D. Merry * sending one bio, and so there is no other context to synchronize 988130f4520SKenneth D. Merry * with. 989130f4520SKenneth D. Merry */ 990130f4520SKenneth D. Merry beio->num_bios_sent = 1; 991130f4520SKenneth D. Merry beio->send_complete = 1; 992130f4520SKenneth D. Merry 993130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 99475c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 995130f4520SKenneth D. Merry devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 99675c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 997130f4520SKenneth D. Merry 998130f4520SKenneth D. Merry (*dev_data->csw->d_strategy)(bio); 999130f4520SKenneth D. Merry } 1000130f4520SKenneth D. Merry 1001130f4520SKenneth D. Merry static void 1002ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1003ee7f31c0SAlexander Motin struct ctl_be_block_io *beio, 1004ee7f31c0SAlexander Motin uint64_t off, uint64_t len, int last) 1005ee7f31c0SAlexander Motin { 1006ee7f31c0SAlexander Motin struct bio *bio; 1007ee7f31c0SAlexander Motin struct ctl_be_block_devdata *dev_data; 10088f5a226aSAlexander Motin uint64_t maxlen; 1009ee7f31c0SAlexander Motin 1010ee7f31c0SAlexander Motin dev_data = &be_lun->backend.dev; 10118f5a226aSAlexander Motin maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 1012ee7f31c0SAlexander Motin while (len > 0) { 1013ee7f31c0SAlexander Motin bio = g_alloc_bio(); 1014ee7f31c0SAlexander Motin bio->bio_cmd = BIO_DELETE; 1015ee7f31c0SAlexander Motin bio->bio_dev = dev_data->cdev; 1016ee7f31c0SAlexander Motin bio->bio_offset = off; 10178f5a226aSAlexander Motin bio->bio_length = MIN(len, maxlen); 1018ee7f31c0SAlexander Motin bio->bio_data = 0; 1019ee7f31c0SAlexander Motin bio->bio_done = ctl_be_block_biodone; 1020ee7f31c0SAlexander Motin bio->bio_caller1 = beio; 10218f5a226aSAlexander Motin bio->bio_pblkno = off / be_lun->blocksize; 1022ee7f31c0SAlexander Motin 1023ee7f31c0SAlexander Motin off += bio->bio_length; 1024ee7f31c0SAlexander Motin len -= bio->bio_length; 1025ee7f31c0SAlexander Motin 102675c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1027ee7f31c0SAlexander Motin beio->num_bios_sent++; 1028ee7f31c0SAlexander Motin if (last && len == 0) 1029ee7f31c0SAlexander Motin beio->send_complete = 1; 103075c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1031ee7f31c0SAlexander Motin 1032ee7f31c0SAlexander Motin (*dev_data->csw->d_strategy)(bio); 1033ee7f31c0SAlexander Motin } 1034ee7f31c0SAlexander Motin } 1035ee7f31c0SAlexander Motin 1036ee7f31c0SAlexander Motin static void 1037ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1038ee7f31c0SAlexander Motin struct ctl_be_block_io *beio) 1039ee7f31c0SAlexander Motin { 1040ee7f31c0SAlexander Motin union ctl_io *io; 1041ee7f31c0SAlexander Motin struct ctl_be_block_devdata *dev_data; 104266df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1043ee7f31c0SAlexander Motin struct scsi_unmap_desc *buf, *end; 1044ee7f31c0SAlexander Motin uint64_t len; 1045ee7f31c0SAlexander Motin 1046ee7f31c0SAlexander Motin dev_data = &be_lun->backend.dev; 1047ee7f31c0SAlexander Motin io = beio->io; 1048ee7f31c0SAlexander Motin 1049ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1050ee7f31c0SAlexander Motin 1051ee7f31c0SAlexander Motin binuptime(&beio->ds_t0); 105275c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1053ee7f31c0SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 105475c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1055ee7f31c0SAlexander Motin 1056ee7f31c0SAlexander Motin if (beio->io_offset == -1) { 1057ee7f31c0SAlexander Motin beio->io_len = 0; 105866df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 105966df9136SAlexander Motin buf = (struct scsi_unmap_desc *)ptrlen->ptr; 106066df9136SAlexander Motin end = buf + ptrlen->len / sizeof(*buf); 1061ee7f31c0SAlexander Motin for (; buf < end; buf++) { 1062ee7f31c0SAlexander Motin len = (uint64_t)scsi_4btoul(buf->length) * 1063ee7f31c0SAlexander Motin be_lun->blocksize; 1064ee7f31c0SAlexander Motin beio->io_len += len; 1065ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 1066ee7f31c0SAlexander Motin scsi_8btou64(buf->lba) * be_lun->blocksize, len, 10677e0be022SAlexander Motin (end - buf < 2) ? TRUE : FALSE); 1068ee7f31c0SAlexander Motin } 1069ee7f31c0SAlexander Motin } else 1070ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 1071ee7f31c0SAlexander Motin beio->io_offset, beio->io_len, TRUE); 1072ee7f31c0SAlexander Motin } 1073ee7f31c0SAlexander Motin 1074ee7f31c0SAlexander Motin static void 1075130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1076130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 1077130f4520SKenneth D. Merry { 107875c7a1d3SAlexander Motin TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1079130f4520SKenneth D. Merry int i; 1080130f4520SKenneth D. Merry struct bio *bio; 1081130f4520SKenneth D. Merry struct ctl_be_block_devdata *dev_data; 1082130f4520SKenneth D. Merry off_t cur_offset; 1083130f4520SKenneth D. Merry int max_iosize; 1084130f4520SKenneth D. Merry 1085130f4520SKenneth D. Merry DPRINTF("entered\n"); 1086130f4520SKenneth D. Merry 1087130f4520SKenneth D. Merry dev_data = &be_lun->backend.dev; 1088130f4520SKenneth D. Merry 1089130f4520SKenneth D. Merry /* 1090130f4520SKenneth D. Merry * We have to limit our I/O size to the maximum supported by the 1091130f4520SKenneth D. Merry * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1092130f4520SKenneth D. Merry * set it properly, use DFLTPHYS. 1093130f4520SKenneth D. Merry */ 1094130f4520SKenneth D. Merry max_iosize = dev_data->cdev->si_iosize_max; 1095130f4520SKenneth D. Merry if (max_iosize < PAGE_SIZE) 1096130f4520SKenneth D. Merry max_iosize = DFLTPHYS; 1097130f4520SKenneth D. Merry 1098130f4520SKenneth D. Merry cur_offset = beio->io_offset; 1099130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 1100130f4520SKenneth D. Merry size_t cur_size; 1101130f4520SKenneth D. Merry uint8_t *cur_ptr; 1102130f4520SKenneth D. Merry 1103130f4520SKenneth D. Merry cur_size = beio->sg_segs[i].len; 1104130f4520SKenneth D. Merry cur_ptr = beio->sg_segs[i].addr; 1105130f4520SKenneth D. Merry 1106130f4520SKenneth D. Merry while (cur_size > 0) { 1107130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 1108130f4520SKenneth D. Merry bio = g_alloc_bio(); 1109130f4520SKenneth D. Merry 1110130f4520SKenneth D. Merry KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1111130f4520SKenneth D. Merry 1112130f4520SKenneth D. Merry bio->bio_cmd = beio->bio_cmd; 1113130f4520SKenneth D. Merry bio->bio_dev = dev_data->cdev; 1114130f4520SKenneth D. Merry bio->bio_caller1 = beio; 1115130f4520SKenneth D. Merry bio->bio_length = min(cur_size, max_iosize); 1116130f4520SKenneth D. Merry bio->bio_offset = cur_offset; 1117130f4520SKenneth D. Merry bio->bio_data = cur_ptr; 1118130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 1119130f4520SKenneth D. Merry bio->bio_pblkno = cur_offset / be_lun->blocksize; 1120130f4520SKenneth D. Merry 1121130f4520SKenneth D. Merry cur_offset += bio->bio_length; 1122130f4520SKenneth D. Merry cur_ptr += bio->bio_length; 1123130f4520SKenneth D. Merry cur_size -= bio->bio_length; 1124130f4520SKenneth D. Merry 112575c7a1d3SAlexander Motin TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1126130f4520SKenneth D. Merry beio->num_bios_sent++; 1127130f4520SKenneth D. Merry } 1128130f4520SKenneth D. Merry } 112975c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 113075c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 113175c7a1d3SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 113275c7a1d3SAlexander Motin beio->send_complete = 1; 113375c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 113475c7a1d3SAlexander Motin 113575c7a1d3SAlexander Motin /* 113675c7a1d3SAlexander Motin * Fire off all allocated requests! 113775c7a1d3SAlexander Motin */ 113875c7a1d3SAlexander Motin while ((bio = TAILQ_FIRST(&queue)) != NULL) { 113975c7a1d3SAlexander Motin TAILQ_REMOVE(&queue, bio, bio_queue); 114075c7a1d3SAlexander Motin (*dev_data->csw->d_strategy)(bio); 114175c7a1d3SAlexander Motin } 1142130f4520SKenneth D. Merry } 1143130f4520SKenneth D. Merry 1144c3e7ba3eSAlexander Motin static uint64_t 1145c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1146c3e7ba3eSAlexander Motin { 1147c3e7ba3eSAlexander Motin struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1148c3e7ba3eSAlexander Motin struct diocgattr_arg arg; 1149c3e7ba3eSAlexander Motin int error; 1150c3e7ba3eSAlexander Motin 1151c3e7ba3eSAlexander Motin if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1152c3e7ba3eSAlexander Motin return (UINT64_MAX); 1153c3e7ba3eSAlexander Motin strlcpy(arg.name, attrname, sizeof(arg.name)); 1154c3e7ba3eSAlexander Motin arg.len = sizeof(arg.value.off); 1155c3e7ba3eSAlexander Motin error = dev_data->csw->d_ioctl(dev_data->cdev, 1156c3e7ba3eSAlexander Motin DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1157c3e7ba3eSAlexander Motin if (error != 0) 1158c3e7ba3eSAlexander Motin return (UINT64_MAX); 1159c3e7ba3eSAlexander Motin return (arg.value.off); 1160c3e7ba3eSAlexander Motin } 1161c3e7ba3eSAlexander Motin 1162130f4520SKenneth D. Merry static void 1163ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1164ee7f31c0SAlexander Motin { 1165ee7f31c0SAlexander Motin union ctl_io *io; 1166ee7f31c0SAlexander Motin 1167ee7f31c0SAlexander Motin io = beio->io; 1168ee7f31c0SAlexander Motin ctl_free_beio(beio); 1169ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1170ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1171ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1172ee7f31c0SAlexander Motin ctl_config_write_done(io); 1173ee7f31c0SAlexander Motin return; 1174ee7f31c0SAlexander Motin } 1175ee7f31c0SAlexander Motin 1176ee7f31c0SAlexander Motin ctl_be_block_config_write(io); 1177ee7f31c0SAlexander Motin } 1178ee7f31c0SAlexander Motin 1179ee7f31c0SAlexander Motin static void 1180ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1181ee7f31c0SAlexander Motin union ctl_io *io) 1182ee7f31c0SAlexander Motin { 1183ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 1184ee7f31c0SAlexander Motin struct ctl_be_block_softc *softc; 118566df9136SAlexander Motin struct ctl_lba_len_flags *lbalen; 1186ee7f31c0SAlexander Motin uint64_t len_left, lba; 1187ee7f31c0SAlexander Motin int i, seglen; 1188ee7f31c0SAlexander Motin uint8_t *buf, *end; 1189ee7f31c0SAlexander Motin 1190ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1191ee7f31c0SAlexander Motin 1192e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1193ee7f31c0SAlexander Motin softc = be_lun->softc; 119411b569f7SAlexander Motin lbalen = ARGS(beio->io); 1195ee7f31c0SAlexander Motin 119664c5167cSAlexander Motin if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 11973406a2a0SAlexander Motin (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1198ee7f31c0SAlexander Motin ctl_free_beio(beio); 1199ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1200ee7f31c0SAlexander Motin /*sks_valid*/ 1, 1201ee7f31c0SAlexander Motin /*command*/ 1, 1202ee7f31c0SAlexander Motin /*field*/ 1, 1203ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1204ee7f31c0SAlexander Motin /*bit*/ 0); 1205ee7f31c0SAlexander Motin ctl_config_write_done(io); 1206ee7f31c0SAlexander Motin return; 1207ee7f31c0SAlexander Motin } 1208ee7f31c0SAlexander Motin 1209ee7f31c0SAlexander Motin switch (io->scsiio.tag_type) { 1210ee7f31c0SAlexander Motin case CTL_TAG_ORDERED: 1211ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1212ee7f31c0SAlexander Motin break; 1213ee7f31c0SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 1214ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1215ee7f31c0SAlexander Motin break; 1216ee7f31c0SAlexander Motin case CTL_TAG_UNTAGGED: 1217ee7f31c0SAlexander Motin case CTL_TAG_SIMPLE: 1218ee7f31c0SAlexander Motin case CTL_TAG_ACA: 1219ee7f31c0SAlexander Motin default: 1220ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1221ee7f31c0SAlexander Motin break; 1222ee7f31c0SAlexander Motin } 1223ee7f31c0SAlexander Motin 12243406a2a0SAlexander Motin if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 122566df9136SAlexander Motin beio->io_offset = lbalen->lba * be_lun->blocksize; 122666df9136SAlexander Motin beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1227ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1228ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1229ee7f31c0SAlexander Motin 1230ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1231ee7f31c0SAlexander Motin return; 1232ee7f31c0SAlexander Motin } 1233ee7f31c0SAlexander Motin 1234ee7f31c0SAlexander Motin beio->bio_cmd = BIO_WRITE; 1235ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_WRITE; 1236ee7f31c0SAlexander Motin 1237ee7f31c0SAlexander Motin DPRINTF("WRITE SAME at LBA %jx len %u\n", 123866df9136SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len); 1239ee7f31c0SAlexander Motin 124066df9136SAlexander Motin len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1241ee7f31c0SAlexander Motin for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1242ee7f31c0SAlexander Motin 1243ee7f31c0SAlexander Motin /* 1244ee7f31c0SAlexander Motin * Setup the S/G entry for this chunk. 1245ee7f31c0SAlexander Motin */ 124608a7cce5SAlexander Motin seglen = MIN(CTLBLK_MAX_SEG, len_left); 1247ee7f31c0SAlexander Motin seglen -= seglen % be_lun->blocksize; 1248ee7f31c0SAlexander Motin beio->sg_segs[i].len = seglen; 1249ee7f31c0SAlexander Motin beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1250ee7f31c0SAlexander Motin 1251ee7f31c0SAlexander Motin DPRINTF("segment %d addr %p len %zd\n", i, 1252ee7f31c0SAlexander Motin beio->sg_segs[i].addr, beio->sg_segs[i].len); 1253ee7f31c0SAlexander Motin 1254ee7f31c0SAlexander Motin beio->num_segs++; 1255ee7f31c0SAlexander Motin len_left -= seglen; 1256ee7f31c0SAlexander Motin 1257ee7f31c0SAlexander Motin buf = beio->sg_segs[i].addr; 1258ee7f31c0SAlexander Motin end = buf + seglen; 1259ee7f31c0SAlexander Motin for (; buf < end; buf += be_lun->blocksize) { 1260ee7f31c0SAlexander Motin memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 126166df9136SAlexander Motin if (lbalen->flags & SWS_LBDATA) 126266df9136SAlexander Motin scsi_ulto4b(lbalen->lba + lba, buf); 1263ee7f31c0SAlexander Motin lba++; 1264ee7f31c0SAlexander Motin } 1265ee7f31c0SAlexander Motin } 1266ee7f31c0SAlexander Motin 126766df9136SAlexander Motin beio->io_offset = lbalen->lba * be_lun->blocksize; 1268ee7f31c0SAlexander Motin beio->io_len = lba * be_lun->blocksize; 1269ee7f31c0SAlexander Motin 1270ee7f31c0SAlexander Motin /* We can not do all in one run. Correct and schedule rerun. */ 1271ee7f31c0SAlexander Motin if (len_left > 0) { 127266df9136SAlexander Motin lbalen->lba += lba; 127366df9136SAlexander Motin lbalen->len -= lba; 1274ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done_ws; 1275ee7f31c0SAlexander Motin } 1276ee7f31c0SAlexander Motin 1277ee7f31c0SAlexander Motin be_lun->dispatch(be_lun, beio); 1278ee7f31c0SAlexander Motin } 1279ee7f31c0SAlexander Motin 1280ee7f31c0SAlexander Motin static void 1281ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1282ee7f31c0SAlexander Motin union ctl_io *io) 1283ee7f31c0SAlexander Motin { 1284ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 1285ee7f31c0SAlexander Motin struct ctl_be_block_softc *softc; 128666df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1287ee7f31c0SAlexander Motin 1288ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1289ee7f31c0SAlexander Motin 1290e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1291ee7f31c0SAlexander Motin softc = be_lun->softc; 129266df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1293ee7f31c0SAlexander Motin 12943406a2a0SAlexander Motin if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1295ee7f31c0SAlexander Motin ctl_free_beio(beio); 1296ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1297ee7f31c0SAlexander Motin /*sks_valid*/ 0, 1298ee7f31c0SAlexander Motin /*command*/ 1, 1299ee7f31c0SAlexander Motin /*field*/ 0, 1300ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1301ee7f31c0SAlexander Motin /*bit*/ 0); 1302ee7f31c0SAlexander Motin ctl_config_write_done(io); 1303ee7f31c0SAlexander Motin return; 1304ee7f31c0SAlexander Motin } 1305ee7f31c0SAlexander Motin 1306ee7f31c0SAlexander Motin switch (io->scsiio.tag_type) { 1307ee7f31c0SAlexander Motin case CTL_TAG_ORDERED: 1308ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1309ee7f31c0SAlexander Motin break; 1310ee7f31c0SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 1311ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1312ee7f31c0SAlexander Motin break; 1313ee7f31c0SAlexander Motin case CTL_TAG_UNTAGGED: 1314ee7f31c0SAlexander Motin case CTL_TAG_SIMPLE: 1315ee7f31c0SAlexander Motin case CTL_TAG_ACA: 1316ee7f31c0SAlexander Motin default: 1317ee7f31c0SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1318ee7f31c0SAlexander Motin break; 1319ee7f31c0SAlexander Motin } 1320ee7f31c0SAlexander Motin 1321ee7f31c0SAlexander Motin beio->io_len = 0; 1322ee7f31c0SAlexander Motin beio->io_offset = -1; 1323ee7f31c0SAlexander Motin 1324ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1325ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1326ee7f31c0SAlexander Motin 132766df9136SAlexander Motin DPRINTF("UNMAP\n"); 1328ee7f31c0SAlexander Motin 1329ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1330ee7f31c0SAlexander Motin } 1331ee7f31c0SAlexander Motin 1332ee7f31c0SAlexander Motin static void 1333ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio) 1334ef8daf3fSAlexander Motin { 1335ef8daf3fSAlexander Motin union ctl_io *io; 1336ef8daf3fSAlexander Motin 1337ef8daf3fSAlexander Motin io = beio->io; 1338ef8daf3fSAlexander Motin ctl_free_beio(beio); 1339ef8daf3fSAlexander Motin ctl_config_read_done(io); 1340ef8daf3fSAlexander Motin } 1341ef8daf3fSAlexander Motin 1342ef8daf3fSAlexander Motin static void 1343ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1344ef8daf3fSAlexander Motin union ctl_io *io) 1345ef8daf3fSAlexander Motin { 1346ef8daf3fSAlexander Motin struct ctl_be_block_io *beio; 1347ef8daf3fSAlexander Motin struct ctl_be_block_softc *softc; 1348ef8daf3fSAlexander Motin 1349ef8daf3fSAlexander Motin DPRINTF("entered\n"); 1350ef8daf3fSAlexander Motin 1351ef8daf3fSAlexander Motin softc = be_lun->softc; 1352ef8daf3fSAlexander Motin beio = ctl_alloc_beio(softc); 1353ef8daf3fSAlexander Motin beio->io = io; 1354ef8daf3fSAlexander Motin beio->lun = be_lun; 1355ef8daf3fSAlexander Motin beio->beio_cont = ctl_be_block_cr_done; 1356ef8daf3fSAlexander Motin PRIV(io)->ptr = (void *)beio; 1357ef8daf3fSAlexander Motin 1358ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 1359ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1360ef8daf3fSAlexander Motin beio->bio_cmd = -1; 1361ef8daf3fSAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 1362ef8daf3fSAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1363ef8daf3fSAlexander Motin beio->io_len = 0; 1364ef8daf3fSAlexander Motin if (be_lun->get_lba_status) 1365ef8daf3fSAlexander Motin be_lun->get_lba_status(be_lun, beio); 1366ef8daf3fSAlexander Motin else 1367ef8daf3fSAlexander Motin ctl_be_block_cr_done(beio); 1368ef8daf3fSAlexander Motin break; 1369ef8daf3fSAlexander Motin default: 1370ef8daf3fSAlexander Motin panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1371ef8daf3fSAlexander Motin break; 1372ef8daf3fSAlexander Motin } 1373ef8daf3fSAlexander Motin } 1374ef8daf3fSAlexander Motin 1375ef8daf3fSAlexander Motin static void 1376ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1377ee7f31c0SAlexander Motin { 1378ee7f31c0SAlexander Motin union ctl_io *io; 1379ee7f31c0SAlexander Motin 1380ee7f31c0SAlexander Motin io = beio->io; 1381ee7f31c0SAlexander Motin ctl_free_beio(beio); 1382ee7f31c0SAlexander Motin ctl_config_write_done(io); 1383ee7f31c0SAlexander Motin } 1384ee7f31c0SAlexander Motin 1385ee7f31c0SAlexander Motin static void 1386130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1387130f4520SKenneth D. Merry union ctl_io *io) 1388130f4520SKenneth D. Merry { 1389130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1390130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1391130f4520SKenneth D. Merry 1392130f4520SKenneth D. Merry DPRINTF("entered\n"); 1393130f4520SKenneth D. Merry 1394130f4520SKenneth D. Merry softc = be_lun->softc; 1395130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1396130f4520SKenneth D. Merry beio->io = io; 1397130f4520SKenneth D. Merry beio->lun = be_lun; 1398ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done; 1399e86a4142SAlexander Motin PRIV(io)->ptr = (void *)beio; 1400130f4520SKenneth D. Merry 1401130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 1402130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 1403130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 1404d2a0972dSEdward Tomasz Napierala beio->bio_cmd = BIO_FLUSH; 1405130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_NO_DATA; 1406130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1407130f4520SKenneth D. Merry beio->io_len = 0; 1408130f4520SKenneth D. Merry be_lun->lun_flush(be_lun, beio); 1409130f4520SKenneth D. Merry break; 1410ee7f31c0SAlexander Motin case WRITE_SAME_10: 1411ee7f31c0SAlexander Motin case WRITE_SAME_16: 1412ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(be_lun, io); 1413ee7f31c0SAlexander Motin break; 1414ee7f31c0SAlexander Motin case UNMAP: 1415ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(be_lun, io); 1416ee7f31c0SAlexander Motin break; 1417130f4520SKenneth D. Merry default: 1418130f4520SKenneth D. Merry panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1419130f4520SKenneth D. Merry break; 1420130f4520SKenneth D. Merry } 1421130f4520SKenneth D. Merry } 1422130f4520SKenneth D. Merry 1423d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1424d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1425d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1426d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1427130f4520SKenneth D. Merry 1428130f4520SKenneth D. Merry static void 142908a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio) 143008a7cce5SAlexander Motin { 143108a7cce5SAlexander Motin struct ctl_be_block_lun *be_lun; 143208a7cce5SAlexander Motin union ctl_io *io; 143308a7cce5SAlexander Motin 143408a7cce5SAlexander Motin io = beio->io; 143508a7cce5SAlexander Motin be_lun = beio->lun; 143608a7cce5SAlexander Motin ctl_free_beio(beio); 1437ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1438ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1439ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 144011b569f7SAlexander Motin ctl_data_submit_done(io); 144108a7cce5SAlexander Motin return; 144208a7cce5SAlexander Motin } 144308a7cce5SAlexander Motin 144408a7cce5SAlexander Motin io->io_hdr.status &= ~CTL_STATUS_MASK; 144508a7cce5SAlexander Motin io->io_hdr.status |= CTL_STATUS_NONE; 144608a7cce5SAlexander Motin 144775c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 144808a7cce5SAlexander Motin /* 144908a7cce5SAlexander Motin * XXX KDM make sure that links is okay to use at this point. 145008a7cce5SAlexander Motin * Otherwise, we either need to add another field to ctl_io_hdr, 145108a7cce5SAlexander Motin * or deal with resource allocation here. 145208a7cce5SAlexander Motin */ 145308a7cce5SAlexander Motin STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 145475c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 145508a7cce5SAlexander Motin 145608a7cce5SAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 145708a7cce5SAlexander Motin } 145808a7cce5SAlexander Motin 145908a7cce5SAlexander Motin static void 1460130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1461130f4520SKenneth D. Merry union ctl_io *io) 1462130f4520SKenneth D. Merry { 1463130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1464130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 146511b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 1466e86a4142SAlexander Motin struct ctl_ptr_len_flags *bptrlen; 1467e86a4142SAlexander Motin uint64_t len_left, lbas; 1468130f4520SKenneth D. Merry int i; 1469130f4520SKenneth D. Merry 1470130f4520SKenneth D. Merry softc = be_lun->softc; 1471130f4520SKenneth D. Merry 1472130f4520SKenneth D. Merry DPRINTF("entered\n"); 1473130f4520SKenneth D. Merry 147411b569f7SAlexander Motin lbalen = ARGS(io); 147511b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1476130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 147711b569f7SAlexander Motin } else { 147811b569f7SAlexander Motin SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1479130f4520SKenneth D. Merry } 1480130f4520SKenneth D. Merry 1481130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1482130f4520SKenneth D. Merry beio->io = io; 1483130f4520SKenneth D. Merry beio->lun = be_lun; 1484e86a4142SAlexander Motin bptrlen = PRIV(io); 1485e86a4142SAlexander Motin bptrlen->ptr = (void *)beio; 1486130f4520SKenneth D. Merry 1487130f4520SKenneth D. Merry switch (io->scsiio.tag_type) { 1488130f4520SKenneth D. Merry case CTL_TAG_ORDERED: 1489130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1490130f4520SKenneth D. Merry break; 1491130f4520SKenneth D. Merry case CTL_TAG_HEAD_OF_QUEUE: 1492130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1493130f4520SKenneth D. Merry break; 1494130f4520SKenneth D. Merry case CTL_TAG_UNTAGGED: 1495130f4520SKenneth D. Merry case CTL_TAG_SIMPLE: 1496130f4520SKenneth D. Merry case CTL_TAG_ACA: 1497130f4520SKenneth D. Merry default: 1498130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1499130f4520SKenneth D. Merry break; 1500130f4520SKenneth D. Merry } 1501130f4520SKenneth D. Merry 150211b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1503130f4520SKenneth D. Merry beio->bio_cmd = BIO_WRITE; 1504130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_WRITE; 150511b569f7SAlexander Motin } else { 150611b569f7SAlexander Motin beio->bio_cmd = BIO_READ; 150711b569f7SAlexander Motin beio->ds_trans_type = DEVSTAT_READ; 1508130f4520SKenneth D. Merry } 1509130f4520SKenneth D. Merry 151008a7cce5SAlexander Motin DPRINTF("%s at LBA %jx len %u @%ju\n", 1511130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1512e86a4142SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 151311b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 151411b569f7SAlexander Motin lbas = CTLBLK_HALF_IO_SIZE; 151511b569f7SAlexander Motin else 151611b569f7SAlexander Motin lbas = CTLBLK_MAX_IO_SIZE; 151711b569f7SAlexander Motin lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1518e86a4142SAlexander Motin beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1519e86a4142SAlexander Motin beio->io_len = lbas * be_lun->blocksize; 1520e86a4142SAlexander Motin bptrlen->len += lbas; 1521130f4520SKenneth D. Merry 152208a7cce5SAlexander Motin for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 152308a7cce5SAlexander Motin KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 152408a7cce5SAlexander Motin i, CTLBLK_MAX_SEGS)); 1525130f4520SKenneth D. Merry 1526130f4520SKenneth D. Merry /* 1527130f4520SKenneth D. Merry * Setup the S/G entry for this chunk. 1528130f4520SKenneth D. Merry */ 152908a7cce5SAlexander Motin beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1530130f4520SKenneth D. Merry beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1531130f4520SKenneth D. Merry 1532130f4520SKenneth D. Merry DPRINTF("segment %d addr %p len %zd\n", i, 1533130f4520SKenneth D. Merry beio->sg_segs[i].addr, beio->sg_segs[i].len); 1534130f4520SKenneth D. Merry 153511b569f7SAlexander Motin /* Set up second segment for compare operation. */ 153611b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) { 153711b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 153811b569f7SAlexander Motin beio->sg_segs[i].len; 153911b569f7SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 154011b569f7SAlexander Motin uma_zalloc(be_lun->lun_zone, M_WAITOK); 154111b569f7SAlexander Motin } 154211b569f7SAlexander Motin 1543130f4520SKenneth D. Merry beio->num_segs++; 1544130f4520SKenneth D. Merry len_left -= beio->sg_segs[i].len; 1545130f4520SKenneth D. Merry } 1546e86a4142SAlexander Motin if (bptrlen->len < lbalen->len) 154708a7cce5SAlexander Motin beio->beio_cont = ctl_be_block_next; 154808a7cce5SAlexander Motin io->scsiio.be_move_done = ctl_be_block_move_done; 154911b569f7SAlexander Motin /* For compare we have separate S/G lists for read and datamove. */ 155011b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) 155111b569f7SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 155211b569f7SAlexander Motin else 155308a7cce5SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 155408a7cce5SAlexander Motin io->scsiio.kern_data_len = beio->io_len; 155508a7cce5SAlexander Motin io->scsiio.kern_data_resid = 0; 155608a7cce5SAlexander Motin io->scsiio.kern_sg_entries = beio->num_segs; 155708a7cce5SAlexander Motin io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1558130f4520SKenneth D. Merry 1559130f4520SKenneth D. Merry /* 1560130f4520SKenneth D. Merry * For the read case, we need to read the data into our buffers and 1561130f4520SKenneth D. Merry * then we can send it back to the user. For the write case, we 1562130f4520SKenneth D. Merry * need to get the data from the user first. 1563130f4520SKenneth D. Merry */ 1564130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 1565130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1566130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1567130f4520SKenneth D. Merry } else { 1568130f4520SKenneth D. Merry SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1569130f4520SKenneth D. Merry #ifdef CTL_TIME_IO 1570130f4520SKenneth D. Merry getbintime(&io->io_hdr.dma_start_bt); 1571130f4520SKenneth D. Merry #endif 1572130f4520SKenneth D. Merry ctl_datamove(io); 1573130f4520SKenneth D. Merry } 1574130f4520SKenneth D. Merry } 1575130f4520SKenneth D. Merry 1576130f4520SKenneth D. Merry static void 1577130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending) 1578130f4520SKenneth D. Merry { 1579130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1580130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1581130f4520SKenneth D. Merry union ctl_io *io; 1582130f4520SKenneth D. Merry 1583130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)context; 1584130f4520SKenneth D. Merry softc = be_lun->softc; 1585130f4520SKenneth D. Merry 1586130f4520SKenneth D. Merry DPRINTF("entered\n"); 1587130f4520SKenneth D. Merry 158875c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1589130f4520SKenneth D. Merry for (;;) { 1590130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1591130f4520SKenneth D. Merry if (io != NULL) { 1592130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1593130f4520SKenneth D. Merry 1594130f4520SKenneth D. Merry DPRINTF("datamove queue\n"); 1595130f4520SKenneth D. Merry 1596130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1597130f4520SKenneth D. Merry ctl_io_hdr, links); 1598130f4520SKenneth D. Merry 159975c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1600130f4520SKenneth D. Merry 1601e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1602130f4520SKenneth D. Merry 1603130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1604130f4520SKenneth D. Merry 160575c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1606130f4520SKenneth D. Merry continue; 1607130f4520SKenneth D. Merry } 1608130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1609130f4520SKenneth D. Merry if (io != NULL) { 1610130f4520SKenneth D. Merry DPRINTF("config write queue\n"); 1611130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1612130f4520SKenneth D. Merry ctl_io_hdr, links); 161375c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1614130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(be_lun, io); 1615ef8daf3fSAlexander Motin mtx_lock(&be_lun->queue_lock); 1616ef8daf3fSAlexander Motin continue; 1617ef8daf3fSAlexander Motin } 1618ef8daf3fSAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1619ef8daf3fSAlexander Motin if (io != NULL) { 1620ef8daf3fSAlexander Motin DPRINTF("config read queue\n"); 1621ef8daf3fSAlexander Motin STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1622ef8daf3fSAlexander Motin ctl_io_hdr, links); 1623ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 1624ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(be_lun, io); 162575c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1626130f4520SKenneth D. Merry continue; 1627130f4520SKenneth D. Merry } 1628130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1629130f4520SKenneth D. Merry if (io != NULL) { 1630130f4520SKenneth D. Merry DPRINTF("input queue\n"); 1631130f4520SKenneth D. Merry 1632130f4520SKenneth D. Merry STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1633130f4520SKenneth D. Merry ctl_io_hdr, links); 163475c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1635130f4520SKenneth D. Merry 1636130f4520SKenneth D. Merry /* 1637130f4520SKenneth D. Merry * We must drop the lock, since this routine and 1638130f4520SKenneth D. Merry * its children may sleep. 1639130f4520SKenneth D. Merry */ 1640130f4520SKenneth D. Merry ctl_be_block_dispatch(be_lun, io); 1641130f4520SKenneth D. Merry 164275c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1643130f4520SKenneth D. Merry continue; 1644130f4520SKenneth D. Merry } 1645130f4520SKenneth D. Merry 1646130f4520SKenneth D. Merry /* 1647130f4520SKenneth D. Merry * If we get here, there is no work left in the queues, so 1648130f4520SKenneth D. Merry * just break out and let the task queue go to sleep. 1649130f4520SKenneth D. Merry */ 1650130f4520SKenneth D. Merry break; 1651130f4520SKenneth D. Merry } 165275c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1653130f4520SKenneth D. Merry } 1654130f4520SKenneth D. Merry 1655130f4520SKenneth D. Merry /* 1656130f4520SKenneth D. Merry * Entry point from CTL to the backend for I/O. We queue everything to a 1657130f4520SKenneth D. Merry * work thread, so this just puts the I/O on a queue and wakes up the 1658130f4520SKenneth D. Merry * thread. 1659130f4520SKenneth D. Merry */ 1660130f4520SKenneth D. Merry static int 1661130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io) 1662130f4520SKenneth D. Merry { 1663130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1664130f4520SKenneth D. Merry struct ctl_be_lun *ctl_be_lun; 1665130f4520SKenneth D. Merry 1666130f4520SKenneth D. Merry DPRINTF("entered\n"); 1667130f4520SKenneth D. Merry 1668130f4520SKenneth D. Merry ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1669130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 1670130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1671130f4520SKenneth D. Merry 1672130f4520SKenneth D. Merry /* 1673130f4520SKenneth D. Merry * Make sure we only get SCSI I/O. 1674130f4520SKenneth D. Merry */ 1675130f4520SKenneth D. Merry KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1676130f4520SKenneth D. Merry "%#x) encountered", io->io_hdr.io_type)); 1677130f4520SKenneth D. Merry 1678e86a4142SAlexander Motin PRIV(io)->len = 0; 1679e86a4142SAlexander Motin 168075c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1681130f4520SKenneth D. Merry /* 1682130f4520SKenneth D. Merry * XXX KDM make sure that links is okay to use at this point. 1683130f4520SKenneth D. Merry * Otherwise, we either need to add another field to ctl_io_hdr, 1684130f4520SKenneth D. Merry * or deal with resource allocation here. 1685130f4520SKenneth D. Merry */ 1686130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 168775c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1688130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1689130f4520SKenneth D. Merry 16909c71cd5aSAlexander Motin return (CTL_RETVAL_COMPLETE); 1691130f4520SKenneth D. Merry } 1692130f4520SKenneth D. Merry 1693130f4520SKenneth D. Merry static int 1694130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1695130f4520SKenneth D. Merry int flag, struct thread *td) 1696130f4520SKenneth D. Merry { 1697130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1698130f4520SKenneth D. Merry int error; 1699130f4520SKenneth D. Merry 1700130f4520SKenneth D. Merry softc = &backend_block_softc; 1701130f4520SKenneth D. Merry 1702130f4520SKenneth D. Merry error = 0; 1703130f4520SKenneth D. Merry 1704130f4520SKenneth D. Merry switch (cmd) { 1705130f4520SKenneth D. Merry case CTL_LUN_REQ: { 1706130f4520SKenneth D. Merry struct ctl_lun_req *lun_req; 1707130f4520SKenneth D. Merry 1708130f4520SKenneth D. Merry lun_req = (struct ctl_lun_req *)addr; 1709130f4520SKenneth D. Merry 1710130f4520SKenneth D. Merry switch (lun_req->reqtype) { 1711130f4520SKenneth D. Merry case CTL_LUNREQ_CREATE: 1712130f4520SKenneth D. Merry error = ctl_be_block_create(softc, lun_req); 1713130f4520SKenneth D. Merry break; 1714130f4520SKenneth D. Merry case CTL_LUNREQ_RM: 1715130f4520SKenneth D. Merry error = ctl_be_block_rm(softc, lun_req); 1716130f4520SKenneth D. Merry break; 171781177295SEdward Tomasz Napierala case CTL_LUNREQ_MODIFY: 171881177295SEdward Tomasz Napierala error = ctl_be_block_modify(softc, lun_req); 171981177295SEdward Tomasz Napierala break; 1720130f4520SKenneth D. Merry default: 1721130f4520SKenneth D. Merry lun_req->status = CTL_LUN_ERROR; 1722130f4520SKenneth D. Merry snprintf(lun_req->error_str, sizeof(lun_req->error_str), 172319720f41SAlexander Motin "invalid LUN request type %d", 1724130f4520SKenneth D. Merry lun_req->reqtype); 1725130f4520SKenneth D. Merry break; 1726130f4520SKenneth D. Merry } 1727130f4520SKenneth D. Merry break; 1728130f4520SKenneth D. Merry } 1729130f4520SKenneth D. Merry default: 1730130f4520SKenneth D. Merry error = ENOTTY; 1731130f4520SKenneth D. Merry break; 1732130f4520SKenneth D. Merry } 1733130f4520SKenneth D. Merry 1734130f4520SKenneth D. Merry return (error); 1735130f4520SKenneth D. Merry } 1736130f4520SKenneth D. Merry 1737130f4520SKenneth D. Merry static int 1738130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1739130f4520SKenneth D. Merry { 1740130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 1741130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 1742130f4520SKenneth D. Merry struct vattr vattr; 174320a28d6cSAlexander Motin off_t pss; 1744130f4520SKenneth D. Merry int error; 1745130f4520SKenneth D. Merry 1746130f4520SKenneth D. Merry error = 0; 1747130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 174819720f41SAlexander Motin params = &be_lun->params; 1749130f4520SKenneth D. Merry 1750130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_FILE; 1751130f4520SKenneth D. Merry be_lun->dispatch = ctl_be_block_dispatch_file; 1752130f4520SKenneth D. Merry be_lun->lun_flush = ctl_be_block_flush_file; 1753ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_file; 1754*53c146deSAlexander Motin be_lun->getattr = ctl_be_block_getattr_file; 1755130f4520SKenneth D. Merry 1756130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1757130f4520SKenneth D. Merry if (error != 0) { 1758130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1759130f4520SKenneth D. Merry "error calling VOP_GETATTR() for file %s", 1760130f4520SKenneth D. Merry be_lun->dev_path); 1761130f4520SKenneth D. Merry return (error); 1762130f4520SKenneth D. Merry } 1763130f4520SKenneth D. Merry 1764130f4520SKenneth D. Merry /* 1765130f4520SKenneth D. Merry * Verify that we have the ability to upgrade to exclusive 1766130f4520SKenneth D. Merry * access on this file so we can trap errors at open instead 1767130f4520SKenneth D. Merry * of reporting them during first access. 1768130f4520SKenneth D. Merry */ 1769130f4520SKenneth D. Merry if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1770130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1771130f4520SKenneth D. Merry if (be_lun->vn->v_iflag & VI_DOOMED) { 1772130f4520SKenneth D. Merry error = EBADF; 1773130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1774130f4520SKenneth D. Merry "error locking file %s", be_lun->dev_path); 1775130f4520SKenneth D. Merry return (error); 1776130f4520SKenneth D. Merry } 1777130f4520SKenneth D. Merry } 1778130f4520SKenneth D. Merry 1779130f4520SKenneth D. Merry 1780130f4520SKenneth D. Merry file_data->cred = crhold(curthread->td_ucred); 178181177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) 178281177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 178381177295SEdward Tomasz Napierala else 1784130f4520SKenneth D. Merry be_lun->size_bytes = vattr.va_size; 1785130f4520SKenneth D. Merry /* 1786130f4520SKenneth D. Merry * We set the multi thread flag for file operations because all 1787130f4520SKenneth D. Merry * filesystems (in theory) are capable of allowing multiple readers 1788130f4520SKenneth D. Merry * of a file at once. So we want to get the maximum possible 1789130f4520SKenneth D. Merry * concurrency. 1790130f4520SKenneth D. Merry */ 1791130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1792130f4520SKenneth D. Merry 1793130f4520SKenneth D. Merry /* 179420a28d6cSAlexander Motin * For files we can use any logical block size. Prefer 512 bytes 179520a28d6cSAlexander Motin * for compatibility reasons. If file's vattr.va_blocksize 179620a28d6cSAlexander Motin * (preferred I/O block size) is bigger and multiple to chosen 179720a28d6cSAlexander Motin * logical block size -- report it as physical block size. 1798130f4520SKenneth D. Merry */ 1799130f4520SKenneth D. Merry if (params->blocksize_bytes != 0) 1800130f4520SKenneth D. Merry be_lun->blocksize = params->blocksize_bytes; 1801130f4520SKenneth D. Merry else 1802130f4520SKenneth D. Merry be_lun->blocksize = 512; 180320a28d6cSAlexander Motin pss = vattr.va_blocksize / be_lun->blocksize; 180420a28d6cSAlexander Motin if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) && 180520a28d6cSAlexander Motin ((pss & (pss - 1)) == 0)) { 180620a28d6cSAlexander Motin be_lun->pblockexp = fls(pss) - 1; 180720a28d6cSAlexander Motin be_lun->pblockoff = 0; 180820a28d6cSAlexander Motin } 1809130f4520SKenneth D. Merry 1810130f4520SKenneth D. Merry /* 1811130f4520SKenneth D. Merry * Sanity check. The media size has to be at least one 1812130f4520SKenneth D. Merry * sector long. 1813130f4520SKenneth D. Merry */ 1814130f4520SKenneth D. Merry if (be_lun->size_bytes < be_lun->blocksize) { 1815130f4520SKenneth D. Merry error = EINVAL; 1816130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1817130f4520SKenneth D. Merry "file %s size %ju < block size %u", be_lun->dev_path, 1818130f4520SKenneth D. Merry (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1819130f4520SKenneth D. Merry } 1820130f4520SKenneth D. Merry return (error); 1821130f4520SKenneth D. Merry } 1822130f4520SKenneth D. Merry 1823130f4520SKenneth D. Merry static int 1824130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1825130f4520SKenneth D. Merry { 1826130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 1827130f4520SKenneth D. Merry struct vattr vattr; 1828130f4520SKenneth D. Merry struct cdev *dev; 1829130f4520SKenneth D. Merry struct cdevsw *devsw; 1830130f4520SKenneth D. Merry int error; 1831f6012722SAlexander Motin off_t ps, pss, po, pos; 1832130f4520SKenneth D. Merry 183319720f41SAlexander Motin params = &be_lun->params; 1834130f4520SKenneth D. Merry 1835130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_DEV; 1836130f4520SKenneth D. Merry be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1837130f4520SKenneth D. Merry be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1838130f4520SKenneth D. Merry &be_lun->backend.dev.dev_ref); 1839130f4520SKenneth D. Merry if (be_lun->backend.dev.csw == NULL) 1840130f4520SKenneth D. Merry panic("Unable to retrieve device switch"); 1841ef8daf3fSAlexander Motin if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) { 184267f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_zvol; 1843ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_zvol; 1844ef8daf3fSAlexander Motin } else 184567f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_dev; 184667f586a8SAlexander Motin be_lun->lun_flush = ctl_be_block_flush_dev; 184767f586a8SAlexander Motin be_lun->unmap = ctl_be_block_unmap_dev; 1848c3e7ba3eSAlexander Motin be_lun->getattr = ctl_be_block_getattr_dev; 1849130f4520SKenneth D. Merry 1850130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1851130f4520SKenneth D. Merry if (error) { 1852130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 185319720f41SAlexander Motin "error getting vnode attributes for device %s", 185419720f41SAlexander Motin be_lun->dev_path); 1855130f4520SKenneth D. Merry return (error); 1856130f4520SKenneth D. Merry } 1857130f4520SKenneth D. Merry 1858130f4520SKenneth D. Merry dev = be_lun->vn->v_rdev; 1859130f4520SKenneth D. Merry devsw = dev->si_devsw; 1860130f4520SKenneth D. Merry if (!devsw->d_ioctl) { 1861130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 186219720f41SAlexander Motin "no d_ioctl for device %s!", 1863130f4520SKenneth D. Merry be_lun->dev_path); 1864130f4520SKenneth D. Merry return (ENODEV); 1865130f4520SKenneth D. Merry } 1866130f4520SKenneth D. Merry 1867130f4520SKenneth D. Merry error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1868130f4520SKenneth D. Merry (caddr_t)&be_lun->blocksize, FREAD, 1869130f4520SKenneth D. Merry curthread); 1870130f4520SKenneth D. Merry if (error) { 1871130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 187219720f41SAlexander Motin "error %d returned for DIOCGSECTORSIZE ioctl " 187319720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 1874130f4520SKenneth D. Merry return (error); 1875130f4520SKenneth D. Merry } 1876130f4520SKenneth D. Merry 1877130f4520SKenneth D. Merry /* 1878130f4520SKenneth D. Merry * If the user has asked for a blocksize that is greater than the 1879130f4520SKenneth D. Merry * backing device's blocksize, we can do it only if the blocksize 1880130f4520SKenneth D. Merry * the user is asking for is an even multiple of the underlying 1881130f4520SKenneth D. Merry * device's blocksize. 1882130f4520SKenneth D. Merry */ 1883130f4520SKenneth D. Merry if ((params->blocksize_bytes != 0) 1884130f4520SKenneth D. Merry && (params->blocksize_bytes > be_lun->blocksize)) { 1885130f4520SKenneth D. Merry uint32_t bs_multiple, tmp_blocksize; 1886130f4520SKenneth D. Merry 1887130f4520SKenneth D. Merry bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1888130f4520SKenneth D. Merry 1889130f4520SKenneth D. Merry tmp_blocksize = bs_multiple * be_lun->blocksize; 1890130f4520SKenneth D. Merry 1891130f4520SKenneth D. Merry if (tmp_blocksize == params->blocksize_bytes) { 1892130f4520SKenneth D. Merry be_lun->blocksize = params->blocksize_bytes; 1893130f4520SKenneth D. Merry } else { 1894130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 189519720f41SAlexander Motin "requested blocksize %u is not an even " 1896130f4520SKenneth D. Merry "multiple of backing device blocksize %u", 189719720f41SAlexander Motin params->blocksize_bytes, 1898130f4520SKenneth D. Merry be_lun->blocksize); 1899130f4520SKenneth D. Merry return (EINVAL); 1900130f4520SKenneth D. Merry 1901130f4520SKenneth D. Merry } 1902130f4520SKenneth D. Merry } else if ((params->blocksize_bytes != 0) 1903130f4520SKenneth D. Merry && (params->blocksize_bytes != be_lun->blocksize)) { 1904130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 190519720f41SAlexander Motin "requested blocksize %u < backing device " 190619720f41SAlexander Motin "blocksize %u", params->blocksize_bytes, 1907130f4520SKenneth D. Merry be_lun->blocksize); 1908130f4520SKenneth D. Merry return (EINVAL); 1909130f4520SKenneth D. Merry } 1910130f4520SKenneth D. Merry 1911130f4520SKenneth D. Merry error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1912130f4520SKenneth D. Merry (caddr_t)&be_lun->size_bytes, FREAD, 1913130f4520SKenneth D. Merry curthread); 1914130f4520SKenneth D. Merry if (error) { 1915130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 191619720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE " 191719720f41SAlexander Motin " ioctl on %s!", error, 191881177295SEdward Tomasz Napierala be_lun->dev_path); 1919130f4520SKenneth D. Merry return (error); 1920130f4520SKenneth D. Merry } 1921130f4520SKenneth D. Merry 192281177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 192381177295SEdward Tomasz Napierala if (params->lun_size_bytes > be_lun->size_bytes) { 192481177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 192519720f41SAlexander Motin "requested LUN size %ju > backing device " 192619720f41SAlexander Motin "size %ju", 192781177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 192881177295SEdward Tomasz Napierala (uintmax_t)be_lun->size_bytes); 192981177295SEdward Tomasz Napierala return (EINVAL); 1930130f4520SKenneth D. Merry } 1931130f4520SKenneth D. Merry 193281177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 193381177295SEdward Tomasz Napierala } 193481177295SEdward Tomasz Napierala 1935f6012722SAlexander Motin error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1936f6012722SAlexander Motin (caddr_t)&ps, FREAD, curthread); 1937f6012722SAlexander Motin if (error) 1938f6012722SAlexander Motin ps = po = 0; 1939f6012722SAlexander Motin else { 1940f6012722SAlexander Motin error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1941f6012722SAlexander Motin (caddr_t)&po, FREAD, curthread); 1942f6012722SAlexander Motin if (error) 1943f6012722SAlexander Motin po = 0; 1944f6012722SAlexander Motin } 1945f6012722SAlexander Motin pss = ps / be_lun->blocksize; 1946f6012722SAlexander Motin pos = po / be_lun->blocksize; 1947f6012722SAlexander Motin if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1948f6012722SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1949f6012722SAlexander Motin be_lun->pblockexp = fls(pss) - 1; 1950f6012722SAlexander Motin be_lun->pblockoff = (pss - pos) % pss; 1951f6012722SAlexander Motin } 1952f6012722SAlexander Motin 195381177295SEdward Tomasz Napierala return (0); 195481177295SEdward Tomasz Napierala } 1955130f4520SKenneth D. Merry 1956130f4520SKenneth D. Merry static int 1957130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun) 1958130f4520SKenneth D. Merry { 1959130f4520SKenneth D. Merry DROP_GIANT(); 1960130f4520SKenneth D. Merry if (be_lun->vn) { 1961130f4520SKenneth D. Merry int flags = FREAD | FWRITE; 1962130f4520SKenneth D. Merry 1963130f4520SKenneth D. Merry switch (be_lun->dev_type) { 1964130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 1965130f4520SKenneth D. Merry if (be_lun->backend.dev.csw) { 1966130f4520SKenneth D. Merry dev_relthread(be_lun->backend.dev.cdev, 1967130f4520SKenneth D. Merry be_lun->backend.dev.dev_ref); 1968130f4520SKenneth D. Merry be_lun->backend.dev.csw = NULL; 1969130f4520SKenneth D. Merry be_lun->backend.dev.cdev = NULL; 1970130f4520SKenneth D. Merry } 1971130f4520SKenneth D. Merry break; 1972130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 1973130f4520SKenneth D. Merry break; 1974130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 1975025a2301SEdward Tomasz Napierala break; 1976130f4520SKenneth D. Merry default: 1977130f4520SKenneth D. Merry panic("Unexpected backend type."); 1978130f4520SKenneth D. Merry break; 1979130f4520SKenneth D. Merry } 1980130f4520SKenneth D. Merry 1981130f4520SKenneth D. Merry (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1982130f4520SKenneth D. Merry be_lun->vn = NULL; 1983130f4520SKenneth D. Merry 1984130f4520SKenneth D. Merry switch (be_lun->dev_type) { 1985130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 1986130f4520SKenneth D. Merry break; 1987130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 1988130f4520SKenneth D. Merry if (be_lun->backend.file.cred != NULL) { 1989130f4520SKenneth D. Merry crfree(be_lun->backend.file.cred); 1990130f4520SKenneth D. Merry be_lun->backend.file.cred = NULL; 1991130f4520SKenneth D. Merry } 1992130f4520SKenneth D. Merry break; 1993130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 1994025a2301SEdward Tomasz Napierala break; 1995130f4520SKenneth D. Merry default: 1996130f4520SKenneth D. Merry panic("Unexpected backend type."); 1997130f4520SKenneth D. Merry break; 1998130f4520SKenneth D. Merry } 199919720f41SAlexander Motin be_lun->dev_type = CTL_BE_BLOCK_NONE; 2000130f4520SKenneth D. Merry } 2001130f4520SKenneth D. Merry PICKUP_GIANT(); 2002130f4520SKenneth D. Merry 2003130f4520SKenneth D. Merry return (0); 2004130f4520SKenneth D. Merry } 2005130f4520SKenneth D. Merry 2006130f4520SKenneth D. Merry static int 2007130f4520SKenneth D. Merry ctl_be_block_open(struct ctl_be_block_softc *softc, 2008130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2009130f4520SKenneth D. Merry { 2010130f4520SKenneth D. Merry struct nameidata nd; 2011130f4520SKenneth D. Merry int flags; 2012130f4520SKenneth D. Merry int error; 2013130f4520SKenneth D. Merry 2014130f4520SKenneth D. Merry /* 2015130f4520SKenneth D. Merry * XXX KDM allow a read-only option? 2016130f4520SKenneth D. Merry */ 2017130f4520SKenneth D. Merry flags = FREAD | FWRITE; 2018130f4520SKenneth D. Merry error = 0; 2019130f4520SKenneth D. Merry 2020130f4520SKenneth D. Merry if (rootvnode == NULL) { 2021130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 202219720f41SAlexander Motin "Root filesystem is not mounted"); 2023130f4520SKenneth D. Merry return (1); 2024130f4520SKenneth D. Merry } 2025130f4520SKenneth D. Merry 2026130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_cdir) { 2027130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_cdir = rootvnode; 2028130f4520SKenneth D. Merry VREF(rootvnode); 2029130f4520SKenneth D. Merry } 2030130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_rdir) { 2031130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_rdir = rootvnode; 2032130f4520SKenneth D. Merry VREF(rootvnode); 2033130f4520SKenneth D. Merry } 2034130f4520SKenneth D. Merry if (!curthread->td_proc->p_fd->fd_jdir) { 2035130f4520SKenneth D. Merry curthread->td_proc->p_fd->fd_jdir = rootvnode; 2036130f4520SKenneth D. Merry VREF(rootvnode); 2037130f4520SKenneth D. Merry } 2038130f4520SKenneth D. Merry 2039130f4520SKenneth D. Merry again: 2040130f4520SKenneth D. Merry NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2041130f4520SKenneth D. Merry error = vn_open(&nd, &flags, 0, NULL); 2042130f4520SKenneth D. Merry if (error) { 2043130f4520SKenneth D. Merry /* 2044130f4520SKenneth D. Merry * This is the only reasonable guess we can make as far as 2045130f4520SKenneth D. Merry * path if the user doesn't give us a fully qualified path. 2046130f4520SKenneth D. Merry * If they want to specify a file, they need to specify the 2047130f4520SKenneth D. Merry * full path. 2048130f4520SKenneth D. Merry */ 2049130f4520SKenneth D. Merry if (be_lun->dev_path[0] != '/') { 2050130f4520SKenneth D. Merry char *dev_path = "/dev/"; 2051130f4520SKenneth D. Merry char *dev_name; 2052130f4520SKenneth D. Merry 2053130f4520SKenneth D. Merry /* Try adding device path at beginning of name */ 2054130f4520SKenneth D. Merry dev_name = malloc(strlen(be_lun->dev_path) 2055130f4520SKenneth D. Merry + strlen(dev_path) + 1, 2056130f4520SKenneth D. Merry M_CTLBLK, M_WAITOK); 2057130f4520SKenneth D. Merry if (dev_name) { 2058130f4520SKenneth D. Merry sprintf(dev_name, "%s%s", dev_path, 2059130f4520SKenneth D. Merry be_lun->dev_path); 2060130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 2061130f4520SKenneth D. Merry be_lun->dev_path = dev_name; 2062130f4520SKenneth D. Merry goto again; 2063130f4520SKenneth D. Merry } 2064130f4520SKenneth D. Merry } 2065130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 206619720f41SAlexander Motin "error opening %s: %d", be_lun->dev_path, error); 2067130f4520SKenneth D. Merry return (error); 2068130f4520SKenneth D. Merry } 2069130f4520SKenneth D. Merry 2070130f4520SKenneth D. Merry NDFREE(&nd, NDF_ONLY_PNBUF); 2071130f4520SKenneth D. Merry 2072130f4520SKenneth D. Merry be_lun->vn = nd.ni_vp; 2073130f4520SKenneth D. Merry 2074130f4520SKenneth D. Merry /* We only support disks and files. */ 2075130f4520SKenneth D. Merry if (vn_isdisk(be_lun->vn, &error)) { 2076130f4520SKenneth D. Merry error = ctl_be_block_open_dev(be_lun, req); 2077130f4520SKenneth D. Merry } else if (be_lun->vn->v_type == VREG) { 2078130f4520SKenneth D. Merry error = ctl_be_block_open_file(be_lun, req); 2079130f4520SKenneth D. Merry } else { 2080130f4520SKenneth D. Merry error = EINVAL; 2081130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2082025a2301SEdward Tomasz Napierala "%s is not a disk or plain file", be_lun->dev_path); 2083130f4520SKenneth D. Merry } 2084130f4520SKenneth D. Merry VOP_UNLOCK(be_lun->vn, 0); 2085130f4520SKenneth D. Merry 2086130f4520SKenneth D. Merry if (error != 0) { 2087130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 2088130f4520SKenneth D. Merry return (error); 2089130f4520SKenneth D. Merry } 2090130f4520SKenneth D. Merry 2091130f4520SKenneth D. Merry be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2092130f4520SKenneth D. Merry be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2093130f4520SKenneth D. Merry 2094130f4520SKenneth D. Merry return (0); 2095130f4520SKenneth D. Merry } 2096130f4520SKenneth D. Merry 2097130f4520SKenneth D. Merry static int 2098130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2099130f4520SKenneth D. Merry { 2100130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2101130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 210257a5db13SAlexander Motin char num_thread_str[16]; 2103130f4520SKenneth D. Merry char tmpstr[32]; 210457a5db13SAlexander Motin char *value; 2105ee7f31c0SAlexander Motin int retval, num_threads, unmap; 210657a5db13SAlexander Motin int tmp_num_threads; 2107130f4520SKenneth D. Merry 2108130f4520SKenneth D. Merry params = &req->reqdata.create; 2109130f4520SKenneth D. Merry retval = 0; 211019720f41SAlexander Motin req->status = CTL_LUN_OK; 2111130f4520SKenneth D. Merry 2112130f4520SKenneth D. Merry num_threads = cbb_num_threads; 2113130f4520SKenneth D. Merry 2114130f4520SKenneth D. Merry be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2115130f4520SKenneth D. Merry 211619720f41SAlexander Motin be_lun->params = req->reqdata.create; 2117130f4520SKenneth D. Merry be_lun->softc = softc; 2118130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->input_queue); 2119ef8daf3fSAlexander Motin STAILQ_INIT(&be_lun->config_read_queue); 2120130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->config_write_queue); 2121130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->datamove_queue); 2122130f4520SKenneth D. Merry sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 212375c7a1d3SAlexander Motin mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 212475c7a1d3SAlexander Motin mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 212543fb3a65SAlexander Motin ctl_init_opts(&be_lun->ctl_be_lun.options, 212643fb3a65SAlexander Motin req->num_be_args, req->kern_be_args); 2127130f4520SKenneth D. Merry 212808a7cce5SAlexander Motin be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2129eeb94054SAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2130130f4520SKenneth D. Merry 2131130f4520SKenneth D. Merry if (be_lun->lun_zone == NULL) { 2132130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 213319720f41SAlexander Motin "error allocating UMA zone"); 2134130f4520SKenneth D. Merry goto bailout_error; 2135130f4520SKenneth D. Merry } 2136130f4520SKenneth D. Merry 2137130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2138130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type = params->device_type; 2139130f4520SKenneth D. Merry else 2140130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type = T_DIRECT; 2141130f4520SKenneth D. Merry 2142130f4520SKenneth D. Merry if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 214343fb3a65SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 2144eb3687a6SAlexander Motin if (value == NULL) { 2145130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 214619720f41SAlexander Motin "no file argument specified"); 2147130f4520SKenneth D. Merry goto bailout_error; 2148130f4520SKenneth D. Merry } 2149eb3687a6SAlexander Motin be_lun->dev_path = strdup(value, M_CTLBLK); 215019720f41SAlexander Motin be_lun->blocksize = 512; 215119720f41SAlexander Motin be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 2152130f4520SKenneth D. Merry 2153130f4520SKenneth D. Merry retval = ctl_be_block_open(softc, be_lun, req); 2154130f4520SKenneth D. Merry if (retval != 0) { 2155130f4520SKenneth D. Merry retval = 0; 215619720f41SAlexander Motin req->status = CTL_LUN_WARNING; 2157130f4520SKenneth D. Merry } 2158130f4520SKenneth D. Merry } else { 2159130f4520SKenneth D. Merry /* 2160130f4520SKenneth D. Merry * For processor devices, we don't have any size. 2161130f4520SKenneth D. Merry */ 2162130f4520SKenneth D. Merry be_lun->blocksize = 0; 2163f6012722SAlexander Motin be_lun->pblockexp = 0; 2164f6012722SAlexander Motin be_lun->pblockoff = 0; 2165130f4520SKenneth D. Merry be_lun->size_blocks = 0; 2166130f4520SKenneth D. Merry be_lun->size_bytes = 0; 2167130f4520SKenneth D. Merry be_lun->ctl_be_lun.maxlba = 0; 2168130f4520SKenneth D. Merry 2169130f4520SKenneth D. Merry /* 2170130f4520SKenneth D. Merry * Default to just 1 thread for processor devices. 2171130f4520SKenneth D. Merry */ 2172130f4520SKenneth D. Merry num_threads = 1; 2173130f4520SKenneth D. Merry } 2174130f4520SKenneth D. Merry 2175130f4520SKenneth D. Merry /* 2176130f4520SKenneth D. Merry * XXX This searching loop might be refactored to be combined with 2177130f4520SKenneth D. Merry * the loop above, 2178130f4520SKenneth D. Merry */ 217943fb3a65SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 218057a5db13SAlexander Motin if (value != NULL) { 218157a5db13SAlexander Motin tmp_num_threads = strtol(value, NULL, 0); 2182130f4520SKenneth D. Merry 2183130f4520SKenneth D. Merry /* 2184130f4520SKenneth D. Merry * We don't let the user specify less than one 2185130f4520SKenneth D. Merry * thread, but hope he's clueful enough not to 2186130f4520SKenneth D. Merry * specify 1000 threads. 2187130f4520SKenneth D. Merry */ 2188130f4520SKenneth D. Merry if (tmp_num_threads < 1) { 2189130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 219019720f41SAlexander Motin "invalid number of threads %s", 219119720f41SAlexander Motin num_thread_str); 2192130f4520SKenneth D. Merry goto bailout_error; 2193130f4520SKenneth D. Merry } 2194130f4520SKenneth D. Merry num_threads = tmp_num_threads; 219557a5db13SAlexander Motin } 2196c3e7ba3eSAlexander Motin unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol); 219743fb3a65SAlexander Motin value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2198c3e7ba3eSAlexander Motin if (value != NULL) 2199c3e7ba3eSAlexander Motin unmap = (strcmp(value, "on") == 0); 2200130f4520SKenneth D. Merry 2201130f4520SKenneth D. Merry be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2202130f4520SKenneth D. Merry be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 220319720f41SAlexander Motin if (be_lun->vn == NULL) 220419720f41SAlexander Motin be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2205ee7f31c0SAlexander Motin if (unmap) 2206ee7f31c0SAlexander Motin be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2207130f4520SKenneth D. Merry be_lun->ctl_be_lun.be_lun = be_lun; 220819720f41SAlexander Motin be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 220919720f41SAlexander Motin 0 : (be_lun->size_blocks - 1); 2210130f4520SKenneth D. Merry be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2211f6012722SAlexander Motin be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2212f6012722SAlexander Motin be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 221319720f41SAlexander Motin if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 221419720f41SAlexander Motin be_lun->blocksize != 0) 221519720f41SAlexander Motin be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 221619720f41SAlexander Motin be_lun->blocksize; 2217130f4520SKenneth D. Merry /* Tell the user the blocksize we ended up using */ 221819720f41SAlexander Motin params->lun_size_bytes = be_lun->size_bytes; 2219130f4520SKenneth D. Merry params->blocksize_bytes = be_lun->blocksize; 2220130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2221130f4520SKenneth D. Merry be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2222130f4520SKenneth D. Merry be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2223130f4520SKenneth D. Merry } else 2224130f4520SKenneth D. Merry be_lun->ctl_be_lun.req_lun_id = 0; 2225130f4520SKenneth D. Merry 2226130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2227130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_config_status = 2228130f4520SKenneth D. Merry ctl_be_block_lun_config_status; 2229130f4520SKenneth D. Merry be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2230130f4520SKenneth D. Merry 2231130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2232130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2233130f4520SKenneth D. Merry softc->num_luns); 2234130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2235130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2236130f4520SKenneth D. Merry sizeof(tmpstr))); 2237130f4520SKenneth D. Merry 2238130f4520SKenneth D. Merry /* Tell the user what we used for a serial number */ 2239130f4520SKenneth D. Merry strncpy((char *)params->serial_num, tmpstr, 2240130f4520SKenneth D. Merry ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 2241130f4520SKenneth D. Merry } else { 2242130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.serial_num, 2243130f4520SKenneth D. Merry params->serial_num, 2244130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 2245130f4520SKenneth D. Merry sizeof(params->serial_num))); 2246130f4520SKenneth D. Merry } 2247130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2248130f4520SKenneth D. Merry snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2249130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2250130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2251130f4520SKenneth D. Merry sizeof(tmpstr))); 2252130f4520SKenneth D. Merry 2253130f4520SKenneth D. Merry /* Tell the user what we used for a device ID */ 2254130f4520SKenneth D. Merry strncpy((char *)params->device_id, tmpstr, 2255130f4520SKenneth D. Merry ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 2256130f4520SKenneth D. Merry } else { 2257130f4520SKenneth D. Merry strncpy((char *)be_lun->ctl_be_lun.device_id, 2258130f4520SKenneth D. Merry params->device_id, 2259130f4520SKenneth D. Merry ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 2260130f4520SKenneth D. Merry sizeof(params->device_id))); 2261130f4520SKenneth D. Merry } 2262130f4520SKenneth D. Merry 2263130f4520SKenneth D. Merry TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2264130f4520SKenneth D. Merry 2265130f4520SKenneth D. Merry be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2266130f4520SKenneth D. Merry taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2267130f4520SKenneth D. Merry 2268130f4520SKenneth D. Merry if (be_lun->io_taskqueue == NULL) { 2269130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 227019720f41SAlexander Motin "unable to create taskqueue"); 2271130f4520SKenneth D. Merry goto bailout_error; 2272130f4520SKenneth D. Merry } 2273130f4520SKenneth D. Merry 2274130f4520SKenneth D. Merry /* 2275130f4520SKenneth D. Merry * Note that we start the same number of threads by default for 2276130f4520SKenneth D. Merry * both the file case and the block device case. For the file 2277130f4520SKenneth D. Merry * case, we need multiple threads to allow concurrency, because the 2278130f4520SKenneth D. Merry * vnode interface is designed to be a blocking interface. For the 2279130f4520SKenneth D. Merry * block device case, ZFS zvols at least will block the caller's 2280130f4520SKenneth D. Merry * context in many instances, and so we need multiple threads to 2281130f4520SKenneth D. Merry * overcome that problem. Other block devices don't need as many 2282130f4520SKenneth D. Merry * threads, but they shouldn't cause too many problems. 2283130f4520SKenneth D. Merry * 2284130f4520SKenneth D. Merry * If the user wants to just have a single thread for a block 2285130f4520SKenneth D. Merry * device, he can specify that when the LUN is created, or change 2286130f4520SKenneth D. Merry * the tunable/sysctl to alter the default number of threads. 2287130f4520SKenneth D. Merry */ 2288130f4520SKenneth D. Merry retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2289130f4520SKenneth D. Merry /*num threads*/num_threads, 2290130f4520SKenneth D. Merry /*priority*/PWAIT, 2291130f4520SKenneth D. Merry /*thread name*/ 2292130f4520SKenneth D. Merry "%s taskq", be_lun->lunname); 2293130f4520SKenneth D. Merry 2294130f4520SKenneth D. Merry if (retval != 0) 2295130f4520SKenneth D. Merry goto bailout_error; 2296130f4520SKenneth D. Merry 2297130f4520SKenneth D. Merry be_lun->num_threads = num_threads; 2298130f4520SKenneth D. Merry 2299130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2300130f4520SKenneth D. Merry softc->num_luns++; 2301130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2302130f4520SKenneth D. Merry 2303130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2304130f4520SKenneth D. Merry 2305130f4520SKenneth D. Merry retval = ctl_add_lun(&be_lun->ctl_be_lun); 2306130f4520SKenneth D. Merry if (retval != 0) { 2307130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2308130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2309130f4520SKenneth D. Merry links); 2310130f4520SKenneth D. Merry softc->num_luns--; 2311130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2312130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 231319720f41SAlexander Motin "ctl_add_lun() returned error %d, see dmesg for " 231419720f41SAlexander Motin "details", retval); 2315130f4520SKenneth D. Merry retval = 0; 2316130f4520SKenneth D. Merry goto bailout_error; 2317130f4520SKenneth D. Merry } 2318130f4520SKenneth D. Merry 2319130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2320130f4520SKenneth D. Merry 2321130f4520SKenneth D. Merry /* 2322130f4520SKenneth D. Merry * Tell the config_status routine that we're waiting so it won't 2323130f4520SKenneth D. Merry * clean up the LUN in the event of an error. 2324130f4520SKenneth D. Merry */ 2325130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2326130f4520SKenneth D. Merry 2327130f4520SKenneth D. Merry while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2328130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2329130f4520SKenneth D. Merry if (retval == EINTR) 2330130f4520SKenneth D. Merry break; 2331130f4520SKenneth D. Merry } 2332130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2333130f4520SKenneth D. Merry 2334130f4520SKenneth D. Merry if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2335130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 233619720f41SAlexander Motin "LUN configuration error, see dmesg for details"); 2337130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2338130f4520SKenneth D. Merry links); 2339130f4520SKenneth D. Merry softc->num_luns--; 2340130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2341130f4520SKenneth D. Merry goto bailout_error; 2342130f4520SKenneth D. Merry } else { 2343130f4520SKenneth D. Merry params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2344130f4520SKenneth D. Merry } 2345130f4520SKenneth D. Merry 2346130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2347130f4520SKenneth D. Merry 2348130f4520SKenneth D. Merry be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2349130f4520SKenneth D. Merry be_lun->blocksize, 2350130f4520SKenneth D. Merry DEVSTAT_ALL_SUPPORTED, 2351130f4520SKenneth D. Merry be_lun->ctl_be_lun.lun_type 2352130f4520SKenneth D. Merry | DEVSTAT_TYPE_IF_OTHER, 2353130f4520SKenneth D. Merry DEVSTAT_PRIORITY_OTHER); 2354130f4520SKenneth D. Merry 2355130f4520SKenneth D. Merry return (retval); 2356130f4520SKenneth D. Merry 2357130f4520SKenneth D. Merry bailout_error: 2358130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2359130f4520SKenneth D. Merry 23609e005bbcSAlexander Motin if (be_lun->io_taskqueue != NULL) 23619e005bbcSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 2362130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 23639e005bbcSAlexander Motin if (be_lun->dev_path != NULL) 2364130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 23659e005bbcSAlexander Motin if (be_lun->lun_zone != NULL) 23669e005bbcSAlexander Motin uma_zdestroy(be_lun->lun_zone); 236743fb3a65SAlexander Motin ctl_free_opts(&be_lun->ctl_be_lun.options); 236875c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 236975c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2370130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2371130f4520SKenneth D. Merry 2372130f4520SKenneth D. Merry return (retval); 2373130f4520SKenneth D. Merry } 2374130f4520SKenneth D. Merry 2375130f4520SKenneth D. Merry static int 2376130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2377130f4520SKenneth D. Merry { 2378130f4520SKenneth D. Merry struct ctl_lun_rm_params *params; 2379130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2380130f4520SKenneth D. Merry int retval; 2381130f4520SKenneth D. Merry 2382130f4520SKenneth D. Merry params = &req->reqdata.rm; 2383130f4520SKenneth D. Merry 2384130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2385130f4520SKenneth D. Merry 2386130f4520SKenneth D. Merry be_lun = NULL; 2387130f4520SKenneth D. Merry 2388130f4520SKenneth D. Merry STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2389130f4520SKenneth D. Merry if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2390130f4520SKenneth D. Merry break; 2391130f4520SKenneth D. Merry } 2392130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2393130f4520SKenneth D. Merry 2394130f4520SKenneth D. Merry if (be_lun == NULL) { 2395130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 239619720f41SAlexander Motin "LUN %u is not managed by the block backend", 239719720f41SAlexander Motin params->lun_id); 2398130f4520SKenneth D. Merry goto bailout_error; 2399130f4520SKenneth D. Merry } 2400130f4520SKenneth D. Merry 2401130f4520SKenneth D. Merry retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2402130f4520SKenneth D. Merry 2403130f4520SKenneth D. Merry if (retval != 0) { 2404130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 240519720f41SAlexander Motin "error %d returned from ctl_disable_lun() for " 240619720f41SAlexander Motin "LUN %d", retval, params->lun_id); 2407130f4520SKenneth D. Merry goto bailout_error; 2408130f4520SKenneth D. Merry 2409130f4520SKenneth D. Merry } 2410130f4520SKenneth D. Merry 2411130f4520SKenneth D. Merry retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2412130f4520SKenneth D. Merry if (retval != 0) { 2413130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 241419720f41SAlexander Motin "error %d returned from ctl_invalidate_lun() for " 241519720f41SAlexander Motin "LUN %d", retval, params->lun_id); 2416130f4520SKenneth D. Merry goto bailout_error; 2417130f4520SKenneth D. Merry } 2418130f4520SKenneth D. Merry 2419130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2420130f4520SKenneth D. Merry 2421130f4520SKenneth D. Merry be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2422130f4520SKenneth D. Merry 2423130f4520SKenneth D. Merry while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2424130f4520SKenneth D. Merry retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2425130f4520SKenneth D. Merry if (retval == EINTR) 2426130f4520SKenneth D. Merry break; 2427130f4520SKenneth D. Merry } 2428130f4520SKenneth D. Merry 2429130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2430130f4520SKenneth D. Merry 2431130f4520SKenneth D. Merry if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2432130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 243319720f41SAlexander Motin "interrupted waiting for LUN to be freed"); 2434130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2435130f4520SKenneth D. Merry goto bailout_error; 2436130f4520SKenneth D. Merry } 2437130f4520SKenneth D. Merry 2438130f4520SKenneth D. Merry STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2439130f4520SKenneth D. Merry 2440130f4520SKenneth D. Merry softc->num_luns--; 2441130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2442130f4520SKenneth D. Merry 2443130f4520SKenneth D. Merry taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2444130f4520SKenneth D. Merry 2445130f4520SKenneth D. Merry taskqueue_free(be_lun->io_taskqueue); 2446130f4520SKenneth D. Merry 2447130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 2448130f4520SKenneth D. Merry 2449130f4520SKenneth D. Merry if (be_lun->disk_stats != NULL) 2450130f4520SKenneth D. Merry devstat_remove_entry(be_lun->disk_stats); 2451130f4520SKenneth D. Merry 2452130f4520SKenneth D. Merry uma_zdestroy(be_lun->lun_zone); 2453130f4520SKenneth D. Merry 245443fb3a65SAlexander Motin ctl_free_opts(&be_lun->ctl_be_lun.options); 2455130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 245675c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 245775c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2458130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2459130f4520SKenneth D. Merry 2460130f4520SKenneth D. Merry req->status = CTL_LUN_OK; 2461130f4520SKenneth D. Merry 2462130f4520SKenneth D. Merry return (0); 2463130f4520SKenneth D. Merry 2464130f4520SKenneth D. Merry bailout_error: 2465130f4520SKenneth D. Merry 2466130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2467130f4520SKenneth D. Merry 2468130f4520SKenneth D. Merry return (0); 2469130f4520SKenneth D. Merry } 2470130f4520SKenneth D. Merry 247181177295SEdward Tomasz Napierala static int 247281177295SEdward Tomasz Napierala ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 247381177295SEdward Tomasz Napierala struct ctl_lun_req *req) 247481177295SEdward Tomasz Napierala { 247581177295SEdward Tomasz Napierala struct vattr vattr; 247681177295SEdward Tomasz Napierala int error; 247719720f41SAlexander Motin struct ctl_lun_create_params *params = &be_lun->params; 247881177295SEdward Tomasz Napierala 247981177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 248081177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 248181177295SEdward Tomasz Napierala } else { 248271d8e97eSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 248381177295SEdward Tomasz Napierala error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 248471d8e97eSAlexander Motin VOP_UNLOCK(be_lun->vn, 0); 248581177295SEdward Tomasz Napierala if (error != 0) { 248681177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 248781177295SEdward Tomasz Napierala "error calling VOP_GETATTR() for file %s", 248881177295SEdward Tomasz Napierala be_lun->dev_path); 248981177295SEdward Tomasz Napierala return (error); 249081177295SEdward Tomasz Napierala } 249181177295SEdward Tomasz Napierala 249281177295SEdward Tomasz Napierala be_lun->size_bytes = vattr.va_size; 249381177295SEdward Tomasz Napierala } 249481177295SEdward Tomasz Napierala 249581177295SEdward Tomasz Napierala return (0); 249681177295SEdward Tomasz Napierala } 249781177295SEdward Tomasz Napierala 249881177295SEdward Tomasz Napierala static int 249981177295SEdward Tomasz Napierala ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 250081177295SEdward Tomasz Napierala struct ctl_lun_req *req) 250181177295SEdward Tomasz Napierala { 250271d8e97eSAlexander Motin struct ctl_be_block_devdata *dev_data; 250381177295SEdward Tomasz Napierala int error; 250419720f41SAlexander Motin struct ctl_lun_create_params *params = &be_lun->params; 250581177295SEdward Tomasz Napierala uint64_t size_bytes; 250681177295SEdward Tomasz Napierala 250771d8e97eSAlexander Motin dev_data = &be_lun->backend.dev; 250871d8e97eSAlexander Motin if (!dev_data->csw->d_ioctl) { 250981177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 251019720f41SAlexander Motin "no d_ioctl for device %s!", be_lun->dev_path); 251181177295SEdward Tomasz Napierala return (ENODEV); 251281177295SEdward Tomasz Napierala } 251381177295SEdward Tomasz Napierala 251471d8e97eSAlexander Motin error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 251581177295SEdward Tomasz Napierala (caddr_t)&size_bytes, FREAD, 251681177295SEdward Tomasz Napierala curthread); 251781177295SEdward Tomasz Napierala if (error) { 251881177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 251919720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE ioctl " 252019720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 252181177295SEdward Tomasz Napierala return (error); 252281177295SEdward Tomasz Napierala } 252381177295SEdward Tomasz Napierala 252481177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 252581177295SEdward Tomasz Napierala if (params->lun_size_bytes > size_bytes) { 252681177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 252719720f41SAlexander Motin "requested LUN size %ju > backing device " 252819720f41SAlexander Motin "size %ju", 252981177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 253081177295SEdward Tomasz Napierala (uintmax_t)size_bytes); 253181177295SEdward Tomasz Napierala return (EINVAL); 253281177295SEdward Tomasz Napierala } 253381177295SEdward Tomasz Napierala 253481177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 253581177295SEdward Tomasz Napierala } else { 253681177295SEdward Tomasz Napierala be_lun->size_bytes = size_bytes; 253781177295SEdward Tomasz Napierala } 253881177295SEdward Tomasz Napierala 253981177295SEdward Tomasz Napierala return (0); 254081177295SEdward Tomasz Napierala } 254181177295SEdward Tomasz Napierala 254281177295SEdward Tomasz Napierala static int 254381177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 254481177295SEdward Tomasz Napierala { 254581177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 254681177295SEdward Tomasz Napierala struct ctl_be_block_lun *be_lun; 254771d8e97eSAlexander Motin uint64_t oldsize; 25485050aa86SKonstantin Belousov int error; 254981177295SEdward Tomasz Napierala 255081177295SEdward Tomasz Napierala params = &req->reqdata.modify; 255181177295SEdward Tomasz Napierala 255281177295SEdward Tomasz Napierala mtx_lock(&softc->lock); 255381177295SEdward Tomasz Napierala be_lun = NULL; 255481177295SEdward Tomasz Napierala STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 255581177295SEdward Tomasz Napierala if (be_lun->ctl_be_lun.lun_id == params->lun_id) 255681177295SEdward Tomasz Napierala break; 255781177295SEdward Tomasz Napierala } 255881177295SEdward Tomasz Napierala mtx_unlock(&softc->lock); 255981177295SEdward Tomasz Napierala 256081177295SEdward Tomasz Napierala if (be_lun == NULL) { 256181177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 256219720f41SAlexander Motin "LUN %u is not managed by the block backend", 256319720f41SAlexander Motin params->lun_id); 256481177295SEdward Tomasz Napierala goto bailout_error; 256581177295SEdward Tomasz Napierala } 256681177295SEdward Tomasz Napierala 256719720f41SAlexander Motin be_lun->params.lun_size_bytes = params->lun_size_bytes; 256881177295SEdward Tomasz Napierala 25693f829b0cSAlexander Motin oldsize = be_lun->size_bytes; 257019720f41SAlexander Motin if (be_lun->vn == NULL) 257119720f41SAlexander Motin error = ctl_be_block_open(softc, be_lun, req); 257219720f41SAlexander Motin else if (be_lun->vn->v_type == VREG) 257381177295SEdward Tomasz Napierala error = ctl_be_block_modify_file(be_lun, req); 257481177295SEdward Tomasz Napierala else 257581177295SEdward Tomasz Napierala error = ctl_be_block_modify_dev(be_lun, req); 257681177295SEdward Tomasz Napierala 25773f829b0cSAlexander Motin if (error == 0 && be_lun->size_bytes != oldsize) { 257871d8e97eSAlexander Motin be_lun->size_blocks = be_lun->size_bytes >> 257971d8e97eSAlexander Motin be_lun->blocksize_shift; 258081177295SEdward Tomasz Napierala 258181177295SEdward Tomasz Napierala /* 258281177295SEdward Tomasz Napierala * The maximum LBA is the size - 1. 258381177295SEdward Tomasz Napierala * 258481177295SEdward Tomasz Napierala * XXX: Note that this field is being updated without locking, 258581177295SEdward Tomasz Napierala * which might cause problems on 32-bit architectures. 258681177295SEdward Tomasz Napierala */ 258719720f41SAlexander Motin be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 258819720f41SAlexander Motin 0 : (be_lun->size_blocks - 1); 258919720f41SAlexander Motin be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 259019720f41SAlexander Motin be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 259119720f41SAlexander Motin be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 259219720f41SAlexander Motin if (be_lun->dispatch == ctl_be_block_dispatch_zvol && 259319720f41SAlexander Motin be_lun->blocksize != 0) 259419720f41SAlexander Motin be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE / 259519720f41SAlexander Motin be_lun->blocksize; 259681177295SEdward Tomasz Napierala ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 259719720f41SAlexander Motin if (oldsize == 0 && be_lun->size_blocks != 0) 259819720f41SAlexander Motin ctl_lun_online(&be_lun->ctl_be_lun); 259971d8e97eSAlexander Motin } 260081177295SEdward Tomasz Napierala 260181177295SEdward Tomasz Napierala /* Tell the user the exact size we ended up using */ 260281177295SEdward Tomasz Napierala params->lun_size_bytes = be_lun->size_bytes; 260381177295SEdward Tomasz Napierala 260419720f41SAlexander Motin req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 260581177295SEdward Tomasz Napierala 260681177295SEdward Tomasz Napierala return (0); 260781177295SEdward Tomasz Napierala 260881177295SEdward Tomasz Napierala bailout_error: 260981177295SEdward Tomasz Napierala req->status = CTL_LUN_ERROR; 261081177295SEdward Tomasz Napierala 261181177295SEdward Tomasz Napierala return (0); 261281177295SEdward Tomasz Napierala } 261381177295SEdward Tomasz Napierala 2614130f4520SKenneth D. Merry static void 2615130f4520SKenneth D. Merry ctl_be_block_lun_shutdown(void *be_lun) 2616130f4520SKenneth D. Merry { 2617130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2618130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2619130f4520SKenneth D. Merry 2620130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2621130f4520SKenneth D. Merry 2622130f4520SKenneth D. Merry softc = lun->softc; 2623130f4520SKenneth D. Merry 2624130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2625130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2626130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2627130f4520SKenneth D. Merry wakeup(lun); 2628130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2629130f4520SKenneth D. Merry 2630130f4520SKenneth D. Merry } 2631130f4520SKenneth D. Merry 2632130f4520SKenneth D. Merry static void 2633130f4520SKenneth D. Merry ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2634130f4520SKenneth D. Merry { 2635130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2636130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2637130f4520SKenneth D. Merry 2638130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2639130f4520SKenneth D. Merry softc = lun->softc; 2640130f4520SKenneth D. Merry 2641130f4520SKenneth D. Merry if (status == CTL_LUN_CONFIG_OK) { 2642130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2643130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2644130f4520SKenneth D. Merry if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2645130f4520SKenneth D. Merry wakeup(lun); 2646130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2647130f4520SKenneth D. Merry 2648130f4520SKenneth D. Merry /* 2649130f4520SKenneth D. Merry * We successfully added the LUN, attempt to enable it. 2650130f4520SKenneth D. Merry */ 2651130f4520SKenneth D. Merry if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2652130f4520SKenneth D. Merry printf("%s: ctl_enable_lun() failed!\n", __func__); 2653130f4520SKenneth D. Merry if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2654130f4520SKenneth D. Merry printf("%s: ctl_invalidate_lun() failed!\n", 2655130f4520SKenneth D. Merry __func__); 2656130f4520SKenneth D. Merry } 2657130f4520SKenneth D. Merry } 2658130f4520SKenneth D. Merry 2659130f4520SKenneth D. Merry return; 2660130f4520SKenneth D. Merry } 2661130f4520SKenneth D. Merry 2662130f4520SKenneth D. Merry 2663130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2664130f4520SKenneth D. Merry lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2665130f4520SKenneth D. Merry lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2666130f4520SKenneth D. Merry wakeup(lun); 2667130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2668130f4520SKenneth D. Merry } 2669130f4520SKenneth D. Merry 2670130f4520SKenneth D. Merry 2671130f4520SKenneth D. Merry static int 2672130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io) 2673130f4520SKenneth D. Merry { 2674130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2675130f4520SKenneth D. Merry struct ctl_be_lun *ctl_be_lun; 2676130f4520SKenneth D. Merry int retval; 2677130f4520SKenneth D. Merry 2678130f4520SKenneth D. Merry retval = 0; 2679130f4520SKenneth D. Merry 2680130f4520SKenneth D. Merry DPRINTF("entered\n"); 2681130f4520SKenneth D. Merry 2682130f4520SKenneth D. Merry ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2683130f4520SKenneth D. Merry CTL_PRIV_BACKEND_LUN].ptr; 2684130f4520SKenneth D. Merry be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2685130f4520SKenneth D. Merry 2686130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 2687130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 2688130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 2689ee7f31c0SAlexander Motin case WRITE_SAME_10: 2690ee7f31c0SAlexander Motin case WRITE_SAME_16: 2691ee7f31c0SAlexander Motin case UNMAP: 2692130f4520SKenneth D. Merry /* 2693130f4520SKenneth D. Merry * The upper level CTL code will filter out any CDBs with 2694130f4520SKenneth D. Merry * the immediate bit set and return the proper error. 2695130f4520SKenneth D. Merry * 2696130f4520SKenneth D. Merry * We don't really need to worry about what LBA range the 2697130f4520SKenneth D. Merry * user asked to be synced out. When they issue a sync 2698130f4520SKenneth D. Merry * cache command, we'll sync out the whole thing. 2699130f4520SKenneth D. Merry */ 270075c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 2701130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2702130f4520SKenneth D. Merry links); 270375c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2704130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2705130f4520SKenneth D. Merry break; 2706130f4520SKenneth D. Merry case START_STOP_UNIT: { 2707130f4520SKenneth D. Merry struct scsi_start_stop_unit *cdb; 2708130f4520SKenneth D. Merry 2709130f4520SKenneth D. Merry cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2710130f4520SKenneth D. Merry 2711130f4520SKenneth D. Merry if (cdb->how & SSS_START) 2712130f4520SKenneth D. Merry retval = ctl_start_lun(ctl_be_lun); 2713130f4520SKenneth D. Merry else { 2714130f4520SKenneth D. Merry retval = ctl_stop_lun(ctl_be_lun); 2715130f4520SKenneth D. Merry /* 2716130f4520SKenneth D. Merry * XXX KDM Copan-specific offline behavior. 2717130f4520SKenneth D. Merry * Figure out a reasonable way to port this? 2718130f4520SKenneth D. Merry */ 2719130f4520SKenneth D. Merry #ifdef NEEDTOPORT 2720130f4520SKenneth D. Merry if ((retval == 0) 2721130f4520SKenneth D. Merry && (cdb->byte2 & SSS_ONOFFLINE)) 2722130f4520SKenneth D. Merry retval = ctl_lun_offline(ctl_be_lun); 2723130f4520SKenneth D. Merry #endif 2724130f4520SKenneth D. Merry } 2725130f4520SKenneth D. Merry 2726130f4520SKenneth D. Merry /* 2727130f4520SKenneth D. Merry * In general, the above routines should not fail. They 2728130f4520SKenneth D. Merry * just set state for the LUN. So we've got something 2729130f4520SKenneth D. Merry * pretty wrong here if we can't start or stop the LUN. 2730130f4520SKenneth D. Merry */ 2731130f4520SKenneth D. Merry if (retval != 0) { 2732130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 2733130f4520SKenneth D. Merry /*sks_valid*/ 1, 2734130f4520SKenneth D. Merry /*retry_count*/ 0xf051); 2735130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2736130f4520SKenneth D. Merry } else { 2737130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 2738130f4520SKenneth D. Merry } 2739130f4520SKenneth D. Merry ctl_config_write_done(io); 2740130f4520SKenneth D. Merry break; 2741130f4520SKenneth D. Merry } 2742130f4520SKenneth D. Merry default: 2743130f4520SKenneth D. Merry ctl_set_invalid_opcode(&io->scsiio); 2744130f4520SKenneth D. Merry ctl_config_write_done(io); 2745130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2746130f4520SKenneth D. Merry break; 2747130f4520SKenneth D. Merry } 2748130f4520SKenneth D. Merry 2749130f4520SKenneth D. Merry return (retval); 2750130f4520SKenneth D. Merry } 2751130f4520SKenneth D. Merry 2752130f4520SKenneth D. Merry static int 2753130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io) 2754130f4520SKenneth D. Merry { 2755ef8daf3fSAlexander Motin struct ctl_be_block_lun *be_lun; 2756ef8daf3fSAlexander Motin struct ctl_be_lun *ctl_be_lun; 2757ef8daf3fSAlexander Motin int retval = 0; 2758ef8daf3fSAlexander Motin 2759ef8daf3fSAlexander Motin DPRINTF("entered\n"); 2760ef8daf3fSAlexander Motin 2761ef8daf3fSAlexander Motin ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2762ef8daf3fSAlexander Motin CTL_PRIV_BACKEND_LUN].ptr; 2763ef8daf3fSAlexander Motin be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2764ef8daf3fSAlexander Motin 2765ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 2766ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: 2767ef8daf3fSAlexander Motin if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2768ef8daf3fSAlexander Motin mtx_lock(&be_lun->queue_lock); 2769ef8daf3fSAlexander Motin STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2770ef8daf3fSAlexander Motin &io->io_hdr, links); 2771ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 2772ef8daf3fSAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, 2773ef8daf3fSAlexander Motin &be_lun->io_task); 2774ef8daf3fSAlexander Motin retval = CTL_RETVAL_QUEUED; 2775ef8daf3fSAlexander Motin break; 2776ef8daf3fSAlexander Motin } 2777ef8daf3fSAlexander Motin ctl_set_invalid_field(&io->scsiio, 2778ef8daf3fSAlexander Motin /*sks_valid*/ 1, 2779ef8daf3fSAlexander Motin /*command*/ 1, 2780ef8daf3fSAlexander Motin /*field*/ 1, 2781ef8daf3fSAlexander Motin /*bit_valid*/ 1, 2782ef8daf3fSAlexander Motin /*bit*/ 4); 2783ef8daf3fSAlexander Motin ctl_config_read_done(io); 2784ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2785ef8daf3fSAlexander Motin break; 2786ef8daf3fSAlexander Motin default: 2787ef8daf3fSAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 2788ef8daf3fSAlexander Motin ctl_config_read_done(io); 2789ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2790ef8daf3fSAlexander Motin break; 2791ef8daf3fSAlexander Motin } 2792ef8daf3fSAlexander Motin 2793ef8daf3fSAlexander Motin return (retval); 2794130f4520SKenneth D. Merry } 2795130f4520SKenneth D. Merry 2796130f4520SKenneth D. Merry static int 2797130f4520SKenneth D. Merry ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2798130f4520SKenneth D. Merry { 2799130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 2800130f4520SKenneth D. Merry int retval; 2801130f4520SKenneth D. Merry 2802130f4520SKenneth D. Merry lun = (struct ctl_be_block_lun *)be_lun; 2803130f4520SKenneth D. Merry retval = 0; 2804130f4520SKenneth D. Merry 28052cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "\t<num_threads>"); 2806130f4520SKenneth D. Merry 2807130f4520SKenneth D. Merry if (retval != 0) 2808130f4520SKenneth D. Merry goto bailout; 2809130f4520SKenneth D. Merry 2810130f4520SKenneth D. Merry retval = sbuf_printf(sb, "%d", lun->num_threads); 2811130f4520SKenneth D. Merry 2812130f4520SKenneth D. Merry if (retval != 0) 2813130f4520SKenneth D. Merry goto bailout; 2814130f4520SKenneth D. Merry 28152cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "</num_threads>\n"); 2816130f4520SKenneth D. Merry 2817130f4520SKenneth D. Merry bailout: 2818130f4520SKenneth D. Merry 2819130f4520SKenneth D. Merry return (retval); 2820130f4520SKenneth D. Merry } 2821130f4520SKenneth D. Merry 2822c3e7ba3eSAlexander Motin static uint64_t 2823c3e7ba3eSAlexander Motin ctl_be_block_lun_attr(void *be_lun, const char *attrname) 2824c3e7ba3eSAlexander Motin { 2825c3e7ba3eSAlexander Motin struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2826c3e7ba3eSAlexander Motin 2827c3e7ba3eSAlexander Motin if (lun->getattr == NULL) 2828c3e7ba3eSAlexander Motin return (UINT64_MAX); 2829c3e7ba3eSAlexander Motin return (lun->getattr(lun, attrname)); 2830c3e7ba3eSAlexander Motin } 2831c3e7ba3eSAlexander Motin 2832130f4520SKenneth D. Merry int 2833130f4520SKenneth D. Merry ctl_be_block_init(void) 2834130f4520SKenneth D. Merry { 2835130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 2836130f4520SKenneth D. Merry int retval; 2837130f4520SKenneth D. Merry 2838130f4520SKenneth D. Merry softc = &backend_block_softc; 2839130f4520SKenneth D. Merry retval = 0; 2840130f4520SKenneth D. Merry 284175c7a1d3SAlexander Motin mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2842a0e36aeeSEdward Tomasz Napierala beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2843a0e36aeeSEdward Tomasz Napierala NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2844130f4520SKenneth D. Merry STAILQ_INIT(&softc->disk_list); 2845130f4520SKenneth D. Merry STAILQ_INIT(&softc->lun_list); 2846130f4520SKenneth D. Merry 2847130f4520SKenneth D. Merry return (retval); 2848130f4520SKenneth D. Merry } 2849