1130f4520SKenneth D. Merry /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3bec9534dSPedro F. Giffuni * 4130f4520SKenneth D. Merry * Copyright (c) 2003 Silicon Graphics International Corp. 5130f4520SKenneth D. Merry * Copyright (c) 2009-2011 Spectra Logic Corporation 649050613SKa Ho Ng * Copyright (c) 2012,2021 The FreeBSD Foundation 7b06771aaSAlexander Motin * Copyright (c) 2014-2021 Alexander Motin <mav@FreeBSD.org> 8130f4520SKenneth D. Merry * All rights reserved. 9130f4520SKenneth D. Merry * 1081177295SEdward Tomasz Napierala * Portions of this software were developed by Edward Tomasz Napierala 1181177295SEdward Tomasz Napierala * under sponsorship from the FreeBSD Foundation. 1281177295SEdward Tomasz Napierala * 1349050613SKa Ho Ng * Portions of this software were developed by Ka Ho Ng <khng@FreeBSD.org> 1449050613SKa Ho Ng * under sponsorship from the FreeBSD Foundation. 1549050613SKa Ho Ng * 16130f4520SKenneth D. Merry * Redistribution and use in source and binary forms, with or without 17130f4520SKenneth D. Merry * modification, are permitted provided that the following conditions 18130f4520SKenneth D. Merry * are met: 19130f4520SKenneth D. Merry * 1. Redistributions of source code must retain the above copyright 20130f4520SKenneth D. Merry * notice, this list of conditions, and the following disclaimer, 21130f4520SKenneth D. Merry * without modification. 22130f4520SKenneth D. Merry * 2. Redistributions in binary form must reproduce at minimum a disclaimer 23130f4520SKenneth D. Merry * substantially similar to the "NO WARRANTY" disclaimer below 24130f4520SKenneth D. Merry * ("Disclaimer") and any redistribution must be conditioned upon 25130f4520SKenneth D. Merry * including a substantially similar Disclaimer requirement for further 26130f4520SKenneth D. Merry * binary redistribution. 27130f4520SKenneth D. Merry * 28130f4520SKenneth D. Merry * NO WARRANTY 29130f4520SKenneth D. Merry * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 30130f4520SKenneth D. Merry * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 31130f4520SKenneth D. Merry * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 32130f4520SKenneth D. Merry * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 33130f4520SKenneth D. Merry * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34130f4520SKenneth D. Merry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35130f4520SKenneth D. Merry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36130f4520SKenneth D. Merry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37130f4520SKenneth D. Merry * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 38130f4520SKenneth D. Merry * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39130f4520SKenneth D. Merry * POSSIBILITY OF SUCH DAMAGES. 40130f4520SKenneth D. Merry * 41130f4520SKenneth D. Merry * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 42130f4520SKenneth D. Merry */ 43130f4520SKenneth D. Merry /* 44130f4520SKenneth D. Merry * CAM Target Layer driver backend for block devices. 45130f4520SKenneth D. Merry * 46130f4520SKenneth D. Merry * Author: Ken Merry <ken@FreeBSD.org> 47130f4520SKenneth D. Merry */ 48130f4520SKenneth D. Merry #include <sys/param.h> 49130f4520SKenneth D. Merry #include <sys/systm.h> 50130f4520SKenneth D. Merry #include <sys/kernel.h> 51130f4520SKenneth D. Merry #include <sys/types.h> 52130f4520SKenneth D. Merry #include <sys/kthread.h> 53130f4520SKenneth D. Merry #include <sys/bio.h> 54130f4520SKenneth D. Merry #include <sys/fcntl.h> 55ee7f31c0SAlexander Motin #include <sys/limits.h> 56130f4520SKenneth D. Merry #include <sys/lock.h> 57130f4520SKenneth D. Merry #include <sys/mutex.h> 58130f4520SKenneth D. Merry #include <sys/condvar.h> 59130f4520SKenneth D. Merry #include <sys/malloc.h> 60130f4520SKenneth D. Merry #include <sys/conf.h> 61130f4520SKenneth D. Merry #include <sys/ioccom.h> 62130f4520SKenneth D. Merry #include <sys/queue.h> 63130f4520SKenneth D. Merry #include <sys/sbuf.h> 64130f4520SKenneth D. Merry #include <sys/endian.h> 65130f4520SKenneth D. Merry #include <sys/uio.h> 66130f4520SKenneth D. Merry #include <sys/buf.h> 67130f4520SKenneth D. Merry #include <sys/taskqueue.h> 68130f4520SKenneth D. Merry #include <sys/vnode.h> 69130f4520SKenneth D. Merry #include <sys/namei.h> 70130f4520SKenneth D. Merry #include <sys/mount.h> 71130f4520SKenneth D. Merry #include <sys/disk.h> 72130f4520SKenneth D. Merry #include <sys/fcntl.h> 73130f4520SKenneth D. Merry #include <sys/filedesc.h> 74ef8daf3fSAlexander Motin #include <sys/filio.h> 75130f4520SKenneth D. Merry #include <sys/proc.h> 76130f4520SKenneth D. Merry #include <sys/pcpu.h> 77130f4520SKenneth D. Merry #include <sys/module.h> 78130f4520SKenneth D. Merry #include <sys/sdt.h> 79130f4520SKenneth D. Merry #include <sys/devicestat.h> 80130f4520SKenneth D. Merry #include <sys/sysctl.h> 818951f055SMarcelo Araujo #include <sys/nv.h> 828951f055SMarcelo Araujo #include <sys/dnv.h> 8334144c2cSAlexander Motin #include <sys/sx.h> 8449050613SKa Ho Ng #include <sys/unistd.h> 85130f4520SKenneth D. Merry 86130f4520SKenneth D. Merry #include <geom/geom.h> 87130f4520SKenneth D. Merry 88130f4520SKenneth D. Merry #include <cam/cam.h> 89130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h> 90130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h> 91130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h> 92130f4520SKenneth D. Merry #include <cam/ctl/ctl.h> 93130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h> 94130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h> 957ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h> 96130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h> 977ac58230SAlexander Motin #include <cam/ctl/ctl_private.h> 98130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h> 99130f4520SKenneth D. Merry 100130f4520SKenneth D. Merry /* 101b06771aaSAlexander Motin * The idea here is to allocate enough S/G space to handle at least 1MB I/Os. 102b06771aaSAlexander Motin * On systems with small maxphys it can be 8 128KB segments. On large systems 103b06771aaSAlexander Motin * it can be up to 8 1MB segments. I/Os larger than that we'll split. 104130f4520SKenneth D. Merry */ 105b06771aaSAlexander Motin #define CTLBLK_MAX_SEGS 8 106b06771aaSAlexander Motin #define CTLBLK_HALF_SEGS (CTLBLK_MAX_SEGS / 2) 107cd853791SKonstantin Belousov #define CTLBLK_MIN_SEG (128 * 1024) 108b06771aaSAlexander Motin #define CTLBLK_MAX_SEG MIN(1024 * 1024, MAX(CTLBLK_MIN_SEG, maxphys)) 109b06771aaSAlexander Motin #define CTLBLK_MAX_IO_SIZE (CTLBLK_MAX_SEG * CTLBLK_MAX_SEGS) 110130f4520SKenneth D. Merry 111130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG 112130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \ 113130f4520SKenneth D. Merry printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 114130f4520SKenneth D. Merry #else 115130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0) 116130f4520SKenneth D. Merry #endif 117130f4520SKenneth D. Merry 118e86a4142SAlexander Motin #define PRIV(io) \ 119e86a4142SAlexander Motin ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 12011b569f7SAlexander Motin #define ARGS(io) \ 12111b569f7SAlexander Motin ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 122374f12c5SJohn Baldwin #define DSM_RANGE(io) ((io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].integer) 123e86a4142SAlexander Motin 124130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb); 125130f4520SKenneth D. Merry 126130f4520SKenneth D. Merry typedef enum { 127130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 128130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_WAITING = 0x04, 129130f4520SKenneth D. Merry } ctl_be_block_lun_flags; 130130f4520SKenneth D. Merry 131130f4520SKenneth D. Merry typedef enum { 132130f4520SKenneth D. Merry CTL_BE_BLOCK_NONE, 133130f4520SKenneth D. Merry CTL_BE_BLOCK_DEV, 134130f4520SKenneth D. Merry CTL_BE_BLOCK_FILE 135130f4520SKenneth D. Merry } ctl_be_block_type; 136130f4520SKenneth D. Merry 137130f4520SKenneth D. Merry struct ctl_be_block_filedata { 138130f4520SKenneth D. Merry struct ucred *cred; 139130f4520SKenneth D. Merry }; 140130f4520SKenneth D. Merry 141130f4520SKenneth D. Merry union ctl_be_block_bedata { 142130f4520SKenneth D. Merry struct ctl_be_block_filedata file; 143130f4520SKenneth D. Merry }; 144130f4520SKenneth D. Merry 145130f4520SKenneth D. Merry struct ctl_be_block_io; 146130f4520SKenneth D. Merry struct ctl_be_block_lun; 147130f4520SKenneth D. Merry 148130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 149130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 150c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 151c3e7ba3eSAlexander Motin const char *attrname); 152130f4520SKenneth D. Merry 153130f4520SKenneth D. Merry /* 154130f4520SKenneth D. Merry * Backend LUN structure. There is a 1:1 mapping between a block device 155130f4520SKenneth D. Merry * and a backend block LUN, and between a backend block LUN and a CTL LUN. 156130f4520SKenneth D. Merry */ 157130f4520SKenneth D. Merry struct ctl_be_block_lun { 158767300e8SAlexander Motin struct ctl_be_lun cbe_lun; /* Must be first element. */ 15919720f41SAlexander Motin struct ctl_lun_create_params params; 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 uint64_t size_blocks; 170130f4520SKenneth D. Merry uint64_t size_bytes; 171130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 172130f4520SKenneth D. Merry struct devstat *disk_stats; 173130f4520SKenneth D. Merry ctl_be_block_lun_flags flags; 17434144c2cSAlexander Motin SLIST_ENTRY(ctl_be_block_lun) links; 175130f4520SKenneth D. Merry struct taskqueue *io_taskqueue; 176130f4520SKenneth D. Merry struct task io_task; 177130f4520SKenneth D. Merry int num_threads; 178130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) input_queue; 179ef8daf3fSAlexander Motin STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 180130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 181130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 18275c7a1d3SAlexander Motin struct mtx_padalign io_lock; 18375c7a1d3SAlexander Motin struct mtx_padalign queue_lock; 184130f4520SKenneth D. Merry }; 185130f4520SKenneth D. Merry 186130f4520SKenneth D. Merry /* 187130f4520SKenneth D. Merry * Overall softc structure for the block backend module. 188130f4520SKenneth D. Merry */ 189130f4520SKenneth D. Merry struct ctl_be_block_softc { 19034144c2cSAlexander Motin struct sx modify_lock; 191130f4520SKenneth D. Merry struct mtx lock; 192130f4520SKenneth D. Merry int num_luns; 19334144c2cSAlexander Motin SLIST_HEAD(, ctl_be_block_lun) lun_list; 1940d7fed74SAlexander Motin uma_zone_t beio_zone; 195cd853791SKonstantin Belousov uma_zone_t bufmin_zone; 196cd853791SKonstantin Belousov uma_zone_t bufmax_zone; 197130f4520SKenneth D. Merry }; 198130f4520SKenneth D. Merry 199130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc; 200130f4520SKenneth D. Merry 201130f4520SKenneth D. Merry /* 202130f4520SKenneth D. Merry * Per-I/O information. 203130f4520SKenneth D. Merry */ 204130f4520SKenneth D. Merry struct ctl_be_block_io { 205130f4520SKenneth D. Merry union ctl_io *io; 206130f4520SKenneth D. Merry struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 207130f4520SKenneth D. Merry struct iovec xiovecs[CTLBLK_MAX_SEGS]; 2089a4510acSAlexander Motin int refcnt; 209130f4520SKenneth D. Merry int bio_cmd; 2100d7fed74SAlexander Motin int two_sglists; 211130f4520SKenneth D. Merry int num_segs; 212130f4520SKenneth D. Merry int num_bios_sent; 213130f4520SKenneth D. Merry int num_bios_done; 214130f4520SKenneth D. Merry int send_complete; 2151f0694a6SAlexander Motin int first_error; 2161f0694a6SAlexander Motin uint64_t first_error_offset; 217130f4520SKenneth D. Merry struct bintime ds_t0; 218130f4520SKenneth D. Merry devstat_tag_type ds_tag_type; 219130f4520SKenneth D. Merry devstat_trans_flags ds_trans_type; 220130f4520SKenneth D. Merry uint64_t io_len; 221130f4520SKenneth D. Merry uint64_t io_offset; 2227d0d4342SAlexander Motin int io_arg; 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 2287d4c4443SAlexander Motin static int cbb_num_threads = 32; 2297029da5cSPawel Biernacki SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 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); 2372c7dc6baSAlexander Motin static int ctl_be_block_move_done(union ctl_io *io, bool samethr); 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); 24553c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 24653c146deSAlexander Motin const char *attrname); 24749050613SKa Ho Ng static void ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun, 24849050613SKa Ho Ng struct ctl_be_block_io *beio); 249130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 250130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 251ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 252ee7f31c0SAlexander Motin struct ctl_be_block_io *beio); 253130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 254130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 255c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 256c3e7ba3eSAlexander Motin const char *attrname); 257ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 258ef8daf3fSAlexander Motin union ctl_io *io); 259130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 260130f4520SKenneth D. Merry union ctl_io *io); 261130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 262130f4520SKenneth D. Merry union ctl_io *io); 263130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending); 264130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io); 265130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 266130f4520SKenneth D. Merry int flag, struct thread *td); 267130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 268130f4520SKenneth D. Merry struct ctl_lun_req *req); 269130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 270130f4520SKenneth D. Merry struct ctl_lun_req *req); 271130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 272648dfc1aSAlexander Motin static int ctl_be_block_open(struct ctl_be_block_lun *be_lun, 273130f4520SKenneth D. Merry struct ctl_lun_req *req); 274130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc, 275130f4520SKenneth D. Merry struct ctl_lun_req *req); 276130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 277130f4520SKenneth D. Merry struct ctl_lun_req *req); 27881177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 27981177295SEdward Tomasz Napierala struct ctl_lun_req *req); 280767300e8SAlexander Motin static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun); 281130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io); 282130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io); 283767300e8SAlexander Motin static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb); 284767300e8SAlexander Motin static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname); 2850c629e28SAlexander Motin static int ctl_be_block_init(void); 2860c629e28SAlexander Motin static int ctl_be_block_shutdown(void); 287130f4520SKenneth D. Merry 288130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver = 289130f4520SKenneth D. Merry { 2902a2443d8SKenneth D. Merry .name = "block", 2912a2443d8SKenneth D. Merry .flags = CTL_BE_FLAG_HAS_CONFIG, 2922a2443d8SKenneth D. Merry .init = ctl_be_block_init, 2930c629e28SAlexander Motin .shutdown = ctl_be_block_shutdown, 2942a2443d8SKenneth D. Merry .data_submit = ctl_be_block_submit, 2952a2443d8SKenneth D. Merry .config_read = ctl_be_block_config_read, 2962a2443d8SKenneth D. Merry .config_write = ctl_be_block_config_write, 2972a2443d8SKenneth D. Merry .ioctl = ctl_be_block_ioctl, 298c3e7ba3eSAlexander Motin .lun_info = ctl_be_block_lun_info, 299c3e7ba3eSAlexander Motin .lun_attr = ctl_be_block_lun_attr 300130f4520SKenneth D. Merry }; 301130f4520SKenneth D. Merry 30234144c2cSAlexander Motin MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend"); 303130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 304130f4520SKenneth D. Merry 3058054320eSAlexander Motin static void 3068054320eSAlexander Motin ctl_alloc_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg, 3078054320eSAlexander Motin size_t len) 3088054320eSAlexander Motin { 3098054320eSAlexander Motin 310cd853791SKonstantin Belousov if (len <= CTLBLK_MIN_SEG) { 311cd853791SKonstantin Belousov sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK); 312cd853791SKonstantin Belousov } else { 313cd853791SKonstantin Belousov KASSERT(len <= CTLBLK_MAX_SEG, 314cd853791SKonstantin Belousov ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG)); 315cd853791SKonstantin Belousov sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK); 316cd853791SKonstantin Belousov } 3178054320eSAlexander Motin sg->len = len; 3188054320eSAlexander Motin } 3198054320eSAlexander Motin 3208054320eSAlexander Motin static void 3218054320eSAlexander Motin ctl_free_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg) 3228054320eSAlexander Motin { 3238054320eSAlexander Motin 324cd853791SKonstantin Belousov if (sg->len <= CTLBLK_MIN_SEG) { 325cd853791SKonstantin Belousov uma_zfree(softc->bufmin_zone, sg->addr); 326cd853791SKonstantin Belousov } else { 327cd853791SKonstantin Belousov KASSERT(sg->len <= CTLBLK_MAX_SEG, 328cd853791SKonstantin Belousov ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG)); 329cd853791SKonstantin Belousov uma_zfree(softc->bufmax_zone, sg->addr); 330cd853791SKonstantin Belousov } 3318054320eSAlexander Motin } 3328054320eSAlexander Motin 333130f4520SKenneth D. Merry static struct ctl_be_block_io * 334130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc) 335130f4520SKenneth D. Merry { 336130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 337130f4520SKenneth D. Merry 3380c629e28SAlexander Motin beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO); 339130f4520SKenneth D. Merry beio->softc = softc; 3409a4510acSAlexander Motin beio->refcnt = 1; 341130f4520SKenneth D. Merry return (beio); 342130f4520SKenneth D. Merry } 343130f4520SKenneth D. Merry 344130f4520SKenneth D. Merry static void 3459a4510acSAlexander Motin ctl_real_free_beio(struct ctl_be_block_io *beio) 346130f4520SKenneth D. Merry { 3470d7fed74SAlexander Motin struct ctl_be_block_softc *softc = beio->softc; 348130f4520SKenneth D. Merry int i; 349130f4520SKenneth D. Merry 350130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 3518054320eSAlexander Motin ctl_free_seg(softc, &beio->sg_segs[i]); 35211b569f7SAlexander Motin 35311b569f7SAlexander Motin /* For compare we had two equal S/G lists. */ 3540d7fed74SAlexander Motin if (beio->two_sglists) { 3558054320eSAlexander Motin ctl_free_seg(softc, 3568054320eSAlexander Motin &beio->sg_segs[i + CTLBLK_HALF_SEGS]); 35711b569f7SAlexander Motin } 358130f4520SKenneth D. Merry } 359130f4520SKenneth D. Merry 3600d7fed74SAlexander Motin uma_zfree(softc->beio_zone, beio); 361130f4520SKenneth D. Merry } 362130f4520SKenneth D. Merry 363130f4520SKenneth D. Merry static void 3649a4510acSAlexander Motin ctl_refcnt_beio(void *arg, int diff) 3659a4510acSAlexander Motin { 3669a4510acSAlexander Motin struct ctl_be_block_io *beio = arg; 3679a4510acSAlexander Motin 3689a4510acSAlexander Motin if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0) 3699a4510acSAlexander Motin ctl_real_free_beio(beio); 3709a4510acSAlexander Motin } 3719a4510acSAlexander Motin 3729a4510acSAlexander Motin static void 3739a4510acSAlexander Motin ctl_free_beio(struct ctl_be_block_io *beio) 3749a4510acSAlexander Motin { 3759a4510acSAlexander Motin 3769a4510acSAlexander Motin ctl_refcnt_beio(beio, -1); 3779a4510acSAlexander Motin } 3789a4510acSAlexander Motin 3799a4510acSAlexander Motin static void 380130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio) 381130f4520SKenneth D. Merry { 38275c7a1d3SAlexander Motin union ctl_io *io = beio->io; 383130f4520SKenneth D. Merry 384ee7f31c0SAlexander Motin if (beio->beio_cont != NULL) { 385ee7f31c0SAlexander Motin beio->beio_cont(beio); 386ee7f31c0SAlexander Motin } else { 387130f4520SKenneth D. Merry ctl_free_beio(beio); 38811b569f7SAlexander Motin ctl_data_submit_done(io); 389130f4520SKenneth D. Merry } 390ee7f31c0SAlexander Motin } 391130f4520SKenneth D. Merry 39240a43590SJohn Baldwin static void 39340a43590SJohn Baldwin ctl_be_block_io_error(union ctl_io *io, int bio_cmd, uint16_t retry_count) 39440a43590SJohn Baldwin { 39540a43590SJohn Baldwin switch (io->io_hdr.io_type) { 39640a43590SJohn Baldwin case CTL_IO_SCSI: 39740a43590SJohn Baldwin if (bio_cmd == BIO_FLUSH) { 39840a43590SJohn Baldwin /* XXX KDM is there is a better error here? */ 39940a43590SJohn Baldwin ctl_set_internal_failure(&io->scsiio, 40040a43590SJohn Baldwin /*sks_valid*/ 1, 40140a43590SJohn Baldwin retry_count); 40240a43590SJohn Baldwin } else { 40340a43590SJohn Baldwin ctl_set_medium_error(&io->scsiio, bio_cmd == BIO_READ); 40440a43590SJohn Baldwin } 40540a43590SJohn Baldwin break; 40640a43590SJohn Baldwin case CTL_IO_NVME: 40740a43590SJohn Baldwin switch (bio_cmd) { 40840a43590SJohn Baldwin case BIO_FLUSH: 40940a43590SJohn Baldwin case BIO_WRITE: 41040a43590SJohn Baldwin ctl_nvme_set_write_fault(&io->nvmeio); 41140a43590SJohn Baldwin break; 41240a43590SJohn Baldwin case BIO_READ: 41340a43590SJohn Baldwin ctl_nvme_set_unrecoverable_read_error(&io->nvmeio); 41440a43590SJohn Baldwin break; 41540a43590SJohn Baldwin default: 41640a43590SJohn Baldwin ctl_nvme_set_internal_error(&io->nvmeio); 41740a43590SJohn Baldwin break; 41840a43590SJohn Baldwin } 41940a43590SJohn Baldwin break; 42040a43590SJohn Baldwin default: 42140a43590SJohn Baldwin __assert_unreachable(); 42240a43590SJohn Baldwin } 42340a43590SJohn Baldwin } 42440a43590SJohn Baldwin 425d6043e46SAlexander Motin static size_t 426d6043e46SAlexander Motin cmp(uint8_t *a, uint8_t *b, size_t size) 427d6043e46SAlexander Motin { 428d6043e46SAlexander Motin size_t i; 429d6043e46SAlexander Motin 430d6043e46SAlexander Motin for (i = 0; i < size; i++) { 431d6043e46SAlexander Motin if (a[i] != b[i]) 432d6043e46SAlexander Motin break; 433d6043e46SAlexander Motin } 434d6043e46SAlexander Motin return (i); 435d6043e46SAlexander Motin } 436d6043e46SAlexander Motin 437d6043e46SAlexander Motin static void 438d6043e46SAlexander Motin ctl_be_block_compare(union ctl_io *io) 439d6043e46SAlexander Motin { 440d6043e46SAlexander Motin struct ctl_be_block_io *beio; 441d6043e46SAlexander Motin uint64_t off, res; 442d6043e46SAlexander Motin int i; 443d6043e46SAlexander Motin 444d6043e46SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 445d6043e46SAlexander Motin off = 0; 446d6043e46SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 447d6043e46SAlexander Motin res = cmp(beio->sg_segs[i].addr, 448d6043e46SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 449d6043e46SAlexander Motin beio->sg_segs[i].len); 450d6043e46SAlexander Motin off += res; 451d6043e46SAlexander Motin if (res < beio->sg_segs[i].len) 452d6043e46SAlexander Motin break; 453d6043e46SAlexander Motin } 454d6043e46SAlexander Motin if (i < beio->num_segs) { 45540a43590SJohn Baldwin ctl_io_set_compare_failure(io, off); 456d6043e46SAlexander Motin } else 45740a43590SJohn Baldwin ctl_io_set_success(io); 458d6043e46SAlexander Motin } 459d6043e46SAlexander Motin 460130f4520SKenneth D. Merry static int 4612c7dc6baSAlexander Motin ctl_be_block_move_done(union ctl_io *io, bool samethr) 462130f4520SKenneth D. Merry { 463130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 464130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 46511b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 466130f4520SKenneth D. Merry 467e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 468130f4520SKenneth D. Merry 469130f4520SKenneth D. Merry DPRINTF("entered\n"); 47040a43590SJohn Baldwin ctl_add_kern_rel_offset(io, ctl_kern_data_len(io)); 471130f4520SKenneth D. Merry 472130f4520SKenneth D. Merry /* 4732c7dc6baSAlexander Motin * We set status at this point for read and compare commands. 474130f4520SKenneth D. Merry */ 4752c7dc6baSAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4762c7dc6baSAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) { 477a59e2982SAlexander Motin lbalen = ARGS(io); 47811b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_READ) { 47940a43590SJohn Baldwin ctl_io_set_success(io); 48011b569f7SAlexander Motin } else if (lbalen->flags & CTL_LLF_COMPARE) { 48111b569f7SAlexander Motin /* We have two data blocks ready for comparison. */ 482d6043e46SAlexander Motin ctl_be_block_compare(io); 48311b569f7SAlexander Motin } 484130f4520SKenneth D. Merry } 485130f4520SKenneth D. Merry 486130f4520SKenneth D. Merry /* 487130f4520SKenneth D. Merry * If this is a read, or a write with errors, it is done. 488130f4520SKenneth D. Merry */ 489130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_READ) 490130f4520SKenneth D. Merry || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 491130f4520SKenneth D. Merry || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 492130f4520SKenneth D. Merry ctl_complete_beio(beio); 493130f4520SKenneth D. Merry return (0); 494130f4520SKenneth D. Merry } 495130f4520SKenneth D. Merry 496130f4520SKenneth D. Merry /* 4972c7dc6baSAlexander Motin * At this point, we have a write and the DMA completed successfully. 4982c7dc6baSAlexander Motin * If we were called synchronously in the original thread then just 4992c7dc6baSAlexander Motin * dispatch, otherwise we now have to queue it to the task queue to 500130f4520SKenneth D. Merry * execute the backend I/O. That is because we do blocking 501130f4520SKenneth D. Merry * memory allocations, and in the file backing case, blocking I/O. 502130f4520SKenneth D. Merry * This move done routine is generally called in the SIM's 503130f4520SKenneth D. Merry * interrupt context, and therefore we cannot block. 504130f4520SKenneth D. Merry */ 505a59e2982SAlexander Motin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 5062c7dc6baSAlexander Motin if (samethr) { 5072c7dc6baSAlexander Motin be_lun->dispatch(be_lun, beio); 5082c7dc6baSAlexander Motin } else { 50975c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 510130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 51175c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 512130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 5132c7dc6baSAlexander Motin } 514130f4520SKenneth D. Merry return (0); 515130f4520SKenneth D. Merry } 516130f4520SKenneth D. Merry 517130f4520SKenneth D. Merry static void 518130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio) 519130f4520SKenneth D. Merry { 520ac503c19SAlexander Motin struct ctl_be_block_io *beio = bio->bio_caller1; 521ac503c19SAlexander Motin struct ctl_be_block_lun *be_lun = beio->lun; 522ac503c19SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 523130f4520SKenneth D. Merry union ctl_io *io; 524e0c2f975SAlexander Motin int error; 525130f4520SKenneth D. Merry 526130f4520SKenneth D. Merry io = beio->io; 527130f4520SKenneth D. Merry 528130f4520SKenneth D. Merry DPRINTF("entered\n"); 529130f4520SKenneth D. Merry 530e0c2f975SAlexander Motin error = bio->bio_error; 53175c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 5321f0694a6SAlexander Motin if (error != 0 && 5331f0694a6SAlexander Motin (beio->first_error == 0 || 5341f0694a6SAlexander Motin bio->bio_offset < beio->first_error_offset)) { 5351f0694a6SAlexander Motin beio->first_error = error; 5361f0694a6SAlexander Motin beio->first_error_offset = bio->bio_offset; 5371f0694a6SAlexander Motin } 538130f4520SKenneth D. Merry 539130f4520SKenneth D. Merry beio->num_bios_done++; 540130f4520SKenneth D. Merry 541130f4520SKenneth D. Merry /* 542130f4520SKenneth D. Merry * XXX KDM will this cause WITNESS to complain? Holding a lock 543130f4520SKenneth D. Merry * during the free might cause it to complain. 544130f4520SKenneth D. Merry */ 545130f4520SKenneth D. Merry g_destroy_bio(bio); 546130f4520SKenneth D. Merry 547130f4520SKenneth D. Merry /* 548130f4520SKenneth D. Merry * If the send complete bit isn't set, or we aren't the last I/O to 549130f4520SKenneth D. Merry * complete, then we're done. 550130f4520SKenneth D. Merry */ 551130f4520SKenneth D. Merry if ((beio->send_complete == 0) 552130f4520SKenneth D. Merry || (beio->num_bios_done < beio->num_bios_sent)) { 55375c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 554130f4520SKenneth D. Merry return; 555130f4520SKenneth D. Merry } 556130f4520SKenneth D. Merry 557130f4520SKenneth D. Merry /* 558130f4520SKenneth D. Merry * At this point, we've verified that we are the last I/O to 559130f4520SKenneth D. Merry * complete, so it's safe to drop the lock. 560130f4520SKenneth D. Merry */ 56175c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 56275c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 56375c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 56475c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 565130f4520SKenneth D. Merry 566130f4520SKenneth D. Merry /* 567130f4520SKenneth D. Merry * If there are any errors from the backing device, we fail the 568130f4520SKenneth D. Merry * entire I/O with a medium error. 569130f4520SKenneth D. Merry */ 5701f0694a6SAlexander Motin error = beio->first_error; 5711f0694a6SAlexander Motin if (error != 0) { 572e0c2f975SAlexander Motin if (error == EOPNOTSUPP) { 57340a43590SJohn Baldwin ctl_io_set_invalid_opcode(io); 5740631de4aSAlexander Motin } else if (error == ENOSPC || error == EDQUOT) { 57540a43590SJohn Baldwin ctl_io_set_space_alloc_fail(io); 5766187d472SAlexander Motin } else if (error == EROFS || error == EACCES) { 57740a43590SJohn Baldwin ctl_io_set_hw_write_protected(io); 5787f7bb97aSAlexander Motin } else { 57940a43590SJohn Baldwin ctl_be_block_io_error(io, beio->bio_cmd, 58040a43590SJohn Baldwin /*retry_count*/ 0xbad2); 5817f7bb97aSAlexander Motin } 582130f4520SKenneth D. Merry ctl_complete_beio(beio); 583130f4520SKenneth D. Merry return; 584130f4520SKenneth D. Merry } 585130f4520SKenneth D. Merry 586130f4520SKenneth D. Merry /* 58711b569f7SAlexander Motin * If this is a write, a flush, a delete or verify, we're all done. 588130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 589130f4520SKenneth D. Merry */ 590130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_WRITE) 591ee7f31c0SAlexander Motin || (beio->bio_cmd == BIO_FLUSH) 59211b569f7SAlexander Motin || (beio->bio_cmd == BIO_DELETE) 59311b569f7SAlexander Motin || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 59440a43590SJohn Baldwin ctl_io_set_success(io); 595130f4520SKenneth D. Merry ctl_complete_beio(beio); 596130f4520SKenneth D. Merry } else { 597f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 59875a3108eSAlexander Motin beio->beio_cont == NULL) { 59940a43590SJohn Baldwin ctl_io_set_success(io); 600ac503c19SAlexander Motin if (cbe_lun->serseq >= CTL_LUN_SERSEQ_SOFT) 60175a3108eSAlexander Motin ctl_serseq_done(io); 60275a3108eSAlexander Motin } 603130f4520SKenneth D. Merry ctl_datamove(io); 604130f4520SKenneth D. Merry } 605130f4520SKenneth D. Merry } 606130f4520SKenneth D. Merry 607130f4520SKenneth D. Merry static void 608130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 609130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 610130f4520SKenneth D. Merry { 61175c7a1d3SAlexander Motin union ctl_io *io = beio->io; 612130f4520SKenneth D. Merry struct mount *mountpoint; 6130ef5eee9SKonstantin Belousov int error; 614130f4520SKenneth D. Merry 615130f4520SKenneth D. Merry DPRINTF("entered\n"); 616130f4520SKenneth D. Merry 61775c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 61875c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 619130f4520SKenneth D. Merry 620130f4520SKenneth D. Merry (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 621130f4520SKenneth D. Merry 6220ef5eee9SKonstantin Belousov vn_lock(be_lun->vn, vn_lktype_write(mountpoint, be_lun->vn) | 6230ef5eee9SKonstantin Belousov LK_RETRY); 6247d0d4342SAlexander Motin error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 6257d0d4342SAlexander Motin curthread); 626b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 627130f4520SKenneth D. Merry 628130f4520SKenneth D. Merry vn_finished_write(mountpoint); 629130f4520SKenneth D. Merry 63075c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 63175c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 63275c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 63375c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 63475c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 63575c7a1d3SAlexander Motin 636130f4520SKenneth D. Merry if (error == 0) 63740a43590SJohn Baldwin ctl_io_set_success(io); 638130f4520SKenneth D. Merry else { 63940a43590SJohn Baldwin ctl_be_block_io_error(io, BIO_FLUSH, 640130f4520SKenneth D. Merry /*retry_count*/ 0xbad1); 641130f4520SKenneth D. Merry } 642130f4520SKenneth D. Merry 643130f4520SKenneth D. Merry ctl_complete_beio(beio); 644130f4520SKenneth D. Merry } 645130f4520SKenneth D. Merry 64636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t"); 64736160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t"); 64836160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t"); 64936160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t"); 650130f4520SKenneth D. Merry 651130f4520SKenneth D. Merry static void 652130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 653130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 654130f4520SKenneth D. Merry { 655ac503c19SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 656130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 657130f4520SKenneth D. Merry union ctl_io *io; 658130f4520SKenneth D. Merry struct uio xuio; 659130f4520SKenneth D. Merry struct iovec *xiovec; 66083981e31SAlexander Motin size_t s; 66183981e31SAlexander Motin int error, flags, i; 662130f4520SKenneth D. Merry 663130f4520SKenneth D. Merry DPRINTF("entered\n"); 664130f4520SKenneth D. Merry 665130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 666130f4520SKenneth D. Merry io = beio->io; 66755551d05SAlexander Motin flags = 0; 66855551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 66955551d05SAlexander Motin flags |= IO_DIRECT; 67055551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 67155551d05SAlexander Motin flags |= IO_SYNC; 672130f4520SKenneth D. Merry 67311b569f7SAlexander Motin bzero(&xuio, sizeof(xuio)); 674130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 67536160958SMark Johnston SDT_PROBE0(cbb, , read, file_start); 67611b569f7SAlexander Motin xuio.uio_rw = UIO_READ; 677130f4520SKenneth D. Merry } else { 67836160958SMark Johnston SDT_PROBE0(cbb, , write, file_start); 679130f4520SKenneth D. Merry xuio.uio_rw = UIO_WRITE; 68011b569f7SAlexander Motin } 681130f4520SKenneth D. Merry xuio.uio_offset = beio->io_offset; 682130f4520SKenneth D. Merry xuio.uio_resid = beio->io_len; 683130f4520SKenneth D. Merry xuio.uio_segflg = UIO_SYSSPACE; 684130f4520SKenneth D. Merry xuio.uio_iov = beio->xiovecs; 685130f4520SKenneth D. Merry xuio.uio_iovcnt = beio->num_segs; 686130f4520SKenneth D. Merry xuio.uio_td = curthread; 687130f4520SKenneth D. Merry 688130f4520SKenneth D. Merry for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 689130f4520SKenneth D. Merry xiovec->iov_base = beio->sg_segs[i].addr; 690130f4520SKenneth D. Merry xiovec->iov_len = beio->sg_segs[i].len; 691130f4520SKenneth D. Merry } 692130f4520SKenneth D. Merry 69375c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 69475c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 69575c7a1d3SAlexander Motin 696130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 697130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 698130f4520SKenneth D. Merry 699ac503c19SAlexander Motin if (beio->beio_cont == NULL && 700ac503c19SAlexander Motin cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT) 701ac503c19SAlexander Motin ctl_serseq_done(io); 702130f4520SKenneth D. Merry /* 703130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for reads. If the 704130f4520SKenneth D. Merry * DIRECTIO option is configured into the kernel, it calls 705130f4520SKenneth D. Merry * ffs_rawread(). But that only works for single-segment 706130f4520SKenneth D. Merry * uios with user space addresses. In our case, with a 707130f4520SKenneth D. Merry * kernel uio, it still reads into the buffer cache, but it 708130f4520SKenneth D. Merry * will just try to release the buffer from the cache later 709130f4520SKenneth D. Merry * on in ffs_read(). 710130f4520SKenneth D. Merry * 711130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for reads. 712130f4520SKenneth D. Merry * 713130f4520SKenneth D. Merry * UFS does not pay attention to IO_SYNC for reads. 714130f4520SKenneth D. Merry * 715130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (which translates into the 716130f4520SKenneth D. Merry * Solaris define FRSYNC for zfs_read()) for reads. It 717130f4520SKenneth D. Merry * attempts to sync the file before reading. 718130f4520SKenneth D. Merry */ 71955551d05SAlexander Motin error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 720130f4520SKenneth D. Merry 721b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 72236160958SMark Johnston SDT_PROBE0(cbb, , read, file_done); 72383981e31SAlexander Motin if (error == 0 && xuio.uio_resid > 0) { 72483981e31SAlexander Motin /* 72540a43590SJohn Baldwin * If we read less then requested (EOF), then 72683981e31SAlexander Motin * we should clean the rest of the buffer. 72783981e31SAlexander Motin */ 72883981e31SAlexander Motin s = beio->io_len - xuio.uio_resid; 72983981e31SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 73083981e31SAlexander Motin if (s >= beio->sg_segs[i].len) { 73183981e31SAlexander Motin s -= beio->sg_segs[i].len; 73283981e31SAlexander Motin continue; 73383981e31SAlexander Motin } 73483981e31SAlexander Motin bzero((uint8_t *)beio->sg_segs[i].addr + s, 73583981e31SAlexander Motin beio->sg_segs[i].len - s); 73683981e31SAlexander Motin s = 0; 73783981e31SAlexander Motin } 73883981e31SAlexander Motin } 739130f4520SKenneth D. Merry } else { 740130f4520SKenneth D. Merry struct mount *mountpoint; 741130f4520SKenneth D. Merry 742130f4520SKenneth D. Merry (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 7430ef5eee9SKonstantin Belousov vn_lock(be_lun->vn, vn_lktype_write(mountpoint, 7440ef5eee9SKonstantin Belousov be_lun->vn) | LK_RETRY); 745130f4520SKenneth D. Merry 746130f4520SKenneth D. Merry /* 747130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for writes. The write 748130f4520SKenneth D. Merry * is done asynchronously. (Normally the write would just 749130f4520SKenneth D. Merry * get put into cache. 750130f4520SKenneth D. Merry * 751130f4520SKenneth D. Merry * UFS pays attention to IO_SYNC for writes. It will 752130f4520SKenneth D. Merry * attempt to write the buffer out synchronously if that 753130f4520SKenneth D. Merry * flag is set. 754130f4520SKenneth D. Merry * 755130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for writes. 756130f4520SKenneth D. Merry * 757130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 758130f4520SKenneth D. Merry * for writes. It will flush the transaction from the 759130f4520SKenneth D. Merry * cache before returning. 760130f4520SKenneth D. Merry */ 76155551d05SAlexander Motin error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 762b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 763130f4520SKenneth D. Merry 764130f4520SKenneth D. Merry vn_finished_write(mountpoint); 76536160958SMark Johnston SDT_PROBE0(cbb, , write, file_done); 766130f4520SKenneth D. Merry } 767130f4520SKenneth D. Merry 76875c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 76975c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 77075c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 77175c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 77275c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 77375c7a1d3SAlexander Motin 774130f4520SKenneth D. Merry /* 775130f4520SKenneth D. Merry * If we got an error, set the sense data to "MEDIUM ERROR" and 776130f4520SKenneth D. Merry * return the I/O to the user. 777130f4520SKenneth D. Merry */ 778130f4520SKenneth D. Merry if (error != 0) { 7790631de4aSAlexander Motin if (error == ENOSPC || error == EDQUOT) { 78040a43590SJohn Baldwin ctl_io_set_space_alloc_fail(io); 7816187d472SAlexander Motin } else if (error == EROFS || error == EACCES) { 78240a43590SJohn Baldwin ctl_io_set_hw_write_protected(io); 7837f7bb97aSAlexander Motin } else { 78440a43590SJohn Baldwin ctl_be_block_io_error(io, beio->bio_cmd, 0); 7857f7bb97aSAlexander Motin } 786130f4520SKenneth D. Merry ctl_complete_beio(beio); 787130f4520SKenneth D. Merry return; 788130f4520SKenneth D. Merry } 789130f4520SKenneth D. Merry 790130f4520SKenneth D. Merry /* 791696297adSAlexander Motin * If this is a write or a verify, we're all done. 792130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 793130f4520SKenneth D. Merry */ 794696297adSAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 795696297adSAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 79640a43590SJohn Baldwin ctl_io_set_success(io); 797130f4520SKenneth D. Merry ctl_complete_beio(beio); 798130f4520SKenneth D. Merry } else { 799f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 80075a3108eSAlexander Motin beio->beio_cont == NULL) { 80140a43590SJohn Baldwin ctl_io_set_success(io); 802ac503c19SAlexander Motin if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT) 80375a3108eSAlexander Motin ctl_serseq_done(io); 80475a3108eSAlexander Motin } 805130f4520SKenneth D. Merry ctl_datamove(io); 806130f4520SKenneth D. Merry } 807130f4520SKenneth D. Merry } 808130f4520SKenneth D. Merry 809130f4520SKenneth D. Merry static void 810ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 811ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 812ef8daf3fSAlexander Motin { 813ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 814ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 815ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 816ef8daf3fSAlexander Motin off_t roff, off; 817ef8daf3fSAlexander Motin int error, status; 818ef8daf3fSAlexander Motin 819ef8daf3fSAlexander Motin DPRINTF("entered\n"); 820ef8daf3fSAlexander Motin 821374f12c5SJohn Baldwin CTL_IO_ASSERT(io, SCSI); 822374f12c5SJohn Baldwin 8230bcd4ab6SAlexander Motin off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 824ef8daf3fSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 825ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 826ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 827ef8daf3fSAlexander Motin if (error == 0 && off > roff) 828ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 829ef8daf3fSAlexander Motin else { 830ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 831ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 832ef8daf3fSAlexander Motin if (error == 0 && off > roff) 833ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 834ef8daf3fSAlexander Motin else { 835ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 836ef8daf3fSAlexander Motin off = be_lun->size_bytes; 837ef8daf3fSAlexander Motin } 838ef8daf3fSAlexander Motin } 839b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 840ef8daf3fSAlexander Motin 841ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 842ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 8430bcd4ab6SAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 8440bcd4ab6SAlexander Motin lbalen->lba), data->descr[0].length); 845ef8daf3fSAlexander Motin data->descr[0].status = status; 846ef8daf3fSAlexander Motin 847ef8daf3fSAlexander Motin ctl_complete_beio(beio); 848ef8daf3fSAlexander Motin } 849ef8daf3fSAlexander Motin 85053c146deSAlexander Motin static uint64_t 85153c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 85253c146deSAlexander Motin { 85353c146deSAlexander Motin struct vattr vattr; 85453c146deSAlexander Motin struct statfs statfs; 855b9b4269cSAlexander Motin uint64_t val; 85653c146deSAlexander Motin int error; 85753c146deSAlexander Motin 858b9b4269cSAlexander Motin val = UINT64_MAX; 85953c146deSAlexander Motin if (be_lun->vn == NULL) 860b9b4269cSAlexander Motin return (val); 861b9b4269cSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 86253c146deSAlexander Motin if (strcmp(attrname, "blocksused") == 0) { 86353c146deSAlexander Motin error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 864b9b4269cSAlexander Motin if (error == 0) 8650bcd4ab6SAlexander Motin val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 86653c146deSAlexander Motin } 867b9b4269cSAlexander Motin if (strcmp(attrname, "blocksavail") == 0 && 868abd80ddbSMateusz Guzik !VN_IS_DOOMED(be_lun->vn)) { 86953c146deSAlexander Motin error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 870b9b4269cSAlexander Motin if (error == 0) 871a15bbf15SAlexander Motin val = statfs.f_bavail * statfs.f_bsize / 8720bcd4ab6SAlexander Motin be_lun->cbe_lun.blocksize; 87353c146deSAlexander Motin } 874b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 875b9b4269cSAlexander Motin return (val); 87653c146deSAlexander Motin } 87753c146deSAlexander Motin 878ef8daf3fSAlexander Motin static void 87949050613SKa Ho Ng ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun, 88049050613SKa Ho Ng struct ctl_be_block_io *beio) 88149050613SKa Ho Ng { 88249050613SKa Ho Ng struct ctl_be_block_filedata *file_data; 88349050613SKa Ho Ng union ctl_io *io; 88449050613SKa Ho Ng struct ctl_ptr_len_flags *ptrlen; 88549050613SKa Ho Ng struct scsi_unmap_desc *buf, *end; 88649050613SKa Ho Ng struct mount *mp; 88749050613SKa Ho Ng off_t off, len; 88849050613SKa Ho Ng int error; 88949050613SKa Ho Ng 89049050613SKa Ho Ng io = beio->io; 89149050613SKa Ho Ng file_data = &be_lun->backend.file; 89249050613SKa Ho Ng mp = NULL; 89349050613SKa Ho Ng error = 0; 89449050613SKa Ho Ng 89549050613SKa Ho Ng binuptime(&beio->ds_t0); 89649050613SKa Ho Ng devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 89749050613SKa Ho Ng 89849050613SKa Ho Ng (void)vn_start_write(be_lun->vn, &mp, V_WAIT); 89949050613SKa Ho Ng vn_lock(be_lun->vn, vn_lktype_write(mp, be_lun->vn) | LK_RETRY); 90049050613SKa Ho Ng if (beio->io_offset == -1) { 90149050613SKa Ho Ng beio->io_len = 0; 90249050613SKa Ho Ng ptrlen = (struct ctl_ptr_len_flags *) 90349050613SKa Ho Ng &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 90449050613SKa Ho Ng buf = (struct scsi_unmap_desc *)ptrlen->ptr; 90549050613SKa Ho Ng end = buf + ptrlen->len / sizeof(*buf); 90649050613SKa Ho Ng for (; buf < end; buf++) { 90749050613SKa Ho Ng off = (off_t)scsi_8btou64(buf->lba) * 90849050613SKa Ho Ng be_lun->cbe_lun.blocksize; 90949050613SKa Ho Ng len = (off_t)scsi_4btoul(buf->length) * 91049050613SKa Ho Ng be_lun->cbe_lun.blocksize; 91149050613SKa Ho Ng beio->io_len += len; 91249050613SKa Ho Ng error = vn_deallocate(be_lun->vn, &off, &len, 91349050613SKa Ho Ng 0, IO_NOMACCHECK | IO_NODELOCKED, file_data->cred, 91449050613SKa Ho Ng NOCRED); 91549050613SKa Ho Ng if (error != 0) 91649050613SKa Ho Ng break; 91749050613SKa Ho Ng } 91849050613SKa Ho Ng } else { 91949050613SKa Ho Ng /* WRITE_SAME */ 92049050613SKa Ho Ng off = beio->io_offset; 92149050613SKa Ho Ng len = beio->io_len; 92249050613SKa Ho Ng error = vn_deallocate(be_lun->vn, &off, &len, 0, 92349050613SKa Ho Ng IO_NOMACCHECK | IO_NODELOCKED, file_data->cred, NOCRED); 92449050613SKa Ho Ng } 92549050613SKa Ho Ng VOP_UNLOCK(be_lun->vn); 92649050613SKa Ho Ng vn_finished_write(mp); 92749050613SKa Ho Ng 92849050613SKa Ho Ng mtx_lock(&be_lun->io_lock); 92949050613SKa Ho Ng devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 93049050613SKa Ho Ng beio->ds_tag_type, beio->ds_trans_type, 93149050613SKa Ho Ng /*now*/ NULL, /*then*/&beio->ds_t0); 93249050613SKa Ho Ng mtx_unlock(&be_lun->io_lock); 93349050613SKa Ho Ng 93449050613SKa Ho Ng /* 93549050613SKa Ho Ng * If we got an error, set the sense data to "MEDIUM ERROR" and 93649050613SKa Ho Ng * return the I/O to the user. 93749050613SKa Ho Ng */ 93849050613SKa Ho Ng switch (error) { 93949050613SKa Ho Ng case 0: 94040a43590SJohn Baldwin ctl_io_set_success(io); 94149050613SKa Ho Ng break; 94249050613SKa Ho Ng case ENOSPC: 94349050613SKa Ho Ng case EDQUOT: 94440a43590SJohn Baldwin ctl_io_set_space_alloc_fail(io); 94549050613SKa Ho Ng break; 94649050613SKa Ho Ng case EROFS: 94749050613SKa Ho Ng case EACCES: 94840a43590SJohn Baldwin ctl_io_set_hw_write_protected(io); 94949050613SKa Ho Ng break; 95049050613SKa Ho Ng default: 95140a43590SJohn Baldwin ctl_be_block_io_error(io, BIO_DELETE, 0); 95249050613SKa Ho Ng } 95349050613SKa Ho Ng ctl_complete_beio(beio); 95449050613SKa Ho Ng } 95549050613SKa Ho Ng 95649050613SKa Ho Ng static void 95767f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 95867f586a8SAlexander Motin struct ctl_be_block_io *beio) 95967f586a8SAlexander Motin { 960ac503c19SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 96167f586a8SAlexander Motin union ctl_io *io; 9623236151eSAlexander Motin struct cdevsw *csw; 9633236151eSAlexander Motin struct cdev *dev; 96467f586a8SAlexander Motin struct uio xuio; 96567f586a8SAlexander Motin struct iovec *xiovec; 9663236151eSAlexander Motin int error, flags, i, ref; 96767f586a8SAlexander Motin 96867f586a8SAlexander Motin DPRINTF("entered\n"); 96967f586a8SAlexander Motin 97067f586a8SAlexander Motin io = beio->io; 97155551d05SAlexander Motin flags = 0; 97255551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 97355551d05SAlexander Motin flags |= IO_DIRECT; 97455551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 97555551d05SAlexander Motin flags |= IO_SYNC; 97667f586a8SAlexander Motin 97767f586a8SAlexander Motin bzero(&xuio, sizeof(xuio)); 97867f586a8SAlexander Motin if (beio->bio_cmd == BIO_READ) { 97936160958SMark Johnston SDT_PROBE0(cbb, , read, file_start); 98067f586a8SAlexander Motin xuio.uio_rw = UIO_READ; 98167f586a8SAlexander Motin } else { 98236160958SMark Johnston SDT_PROBE0(cbb, , write, file_start); 98367f586a8SAlexander Motin xuio.uio_rw = UIO_WRITE; 98467f586a8SAlexander Motin } 98567f586a8SAlexander Motin xuio.uio_offset = beio->io_offset; 98667f586a8SAlexander Motin xuio.uio_resid = beio->io_len; 98767f586a8SAlexander Motin xuio.uio_segflg = UIO_SYSSPACE; 98867f586a8SAlexander Motin xuio.uio_iov = beio->xiovecs; 98967f586a8SAlexander Motin xuio.uio_iovcnt = beio->num_segs; 99067f586a8SAlexander Motin xuio.uio_td = curthread; 99167f586a8SAlexander Motin 99267f586a8SAlexander Motin for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 99367f586a8SAlexander Motin xiovec->iov_base = beio->sg_segs[i].addr; 99467f586a8SAlexander Motin xiovec->iov_len = beio->sg_segs[i].len; 99567f586a8SAlexander Motin } 99667f586a8SAlexander Motin 99767f586a8SAlexander Motin binuptime(&beio->ds_t0); 99867f586a8SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 99967f586a8SAlexander Motin 10003236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 10013236151eSAlexander Motin if (csw) { 1002ac503c19SAlexander Motin if (beio->bio_cmd == BIO_READ) { 1003ac503c19SAlexander Motin if (beio->beio_cont == NULL && 1004ac503c19SAlexander Motin cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT) 1005ac503c19SAlexander Motin ctl_serseq_done(io); 10063236151eSAlexander Motin error = csw->d_read(dev, &xuio, flags); 1007ac503c19SAlexander Motin } else 10083236151eSAlexander Motin error = csw->d_write(dev, &xuio, flags); 10093236151eSAlexander Motin dev_relthread(dev, ref); 10103236151eSAlexander Motin } else 10113236151eSAlexander Motin error = ENXIO; 10123236151eSAlexander Motin 10133236151eSAlexander Motin if (beio->bio_cmd == BIO_READ) 101436160958SMark Johnston SDT_PROBE0(cbb, , read, file_done); 10153236151eSAlexander Motin else 101636160958SMark Johnston SDT_PROBE0(cbb, , write, file_done); 101767f586a8SAlexander Motin 101867f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 101967f586a8SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 102067f586a8SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 102167f586a8SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 102267f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 102367f586a8SAlexander Motin 102467f586a8SAlexander Motin /* 102567f586a8SAlexander Motin * If we got an error, set the sense data to "MEDIUM ERROR" and 102667f586a8SAlexander Motin * return the I/O to the user. 102767f586a8SAlexander Motin */ 102867f586a8SAlexander Motin if (error != 0) { 10290631de4aSAlexander Motin if (error == ENOSPC || error == EDQUOT) { 103040a43590SJohn Baldwin ctl_io_set_space_alloc_fail(io); 10316187d472SAlexander Motin } else if (error == EROFS || error == EACCES) { 103240a43590SJohn Baldwin ctl_io_set_hw_write_protected(io); 10337f7bb97aSAlexander Motin } else { 103440a43590SJohn Baldwin ctl_be_block_io_error(io, beio->bio_cmd, 0); 10357f7bb97aSAlexander Motin } 103667f586a8SAlexander Motin ctl_complete_beio(beio); 103767f586a8SAlexander Motin return; 103867f586a8SAlexander Motin } 103967f586a8SAlexander Motin 104067f586a8SAlexander Motin /* 104167f586a8SAlexander Motin * If this is a write or a verify, we're all done. 104267f586a8SAlexander Motin * If this is a read, we can now send the data to the user. 104367f586a8SAlexander Motin */ 104467f586a8SAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 104567f586a8SAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 104640a43590SJohn Baldwin ctl_io_set_success(io); 104767f586a8SAlexander Motin ctl_complete_beio(beio); 104867f586a8SAlexander Motin } else { 1049f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 105075a3108eSAlexander Motin beio->beio_cont == NULL) { 105140a43590SJohn Baldwin ctl_io_set_success(io); 1052ac503c19SAlexander Motin if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT) 105375a3108eSAlexander Motin ctl_serseq_done(io); 105475a3108eSAlexander Motin } 105567f586a8SAlexander Motin ctl_datamove(io); 105667f586a8SAlexander Motin } 105767f586a8SAlexander Motin } 105867f586a8SAlexander Motin 105967f586a8SAlexander Motin static void 1060ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 1061ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 1062ef8daf3fSAlexander Motin { 1063ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 10643236151eSAlexander Motin struct cdevsw *csw; 10653236151eSAlexander Motin struct cdev *dev; 1066ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 1067ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 1068ef8daf3fSAlexander Motin off_t roff, off; 10693236151eSAlexander Motin int error, ref, status; 1070ef8daf3fSAlexander Motin 1071ef8daf3fSAlexander Motin DPRINTF("entered\n"); 1072ef8daf3fSAlexander Motin 1073374f12c5SJohn Baldwin CTL_IO_ASSERT(io, SCSI); 1074374f12c5SJohn Baldwin 10753236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 10763236151eSAlexander Motin if (csw == NULL) { 10773236151eSAlexander Motin status = 0; /* unknown up to the end */ 10783236151eSAlexander Motin off = be_lun->size_bytes; 10793236151eSAlexander Motin goto done; 10803236151eSAlexander Motin } 10810bcd4ab6SAlexander Motin off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 10823236151eSAlexander Motin error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 10833236151eSAlexander Motin curthread); 1084ef8daf3fSAlexander Motin if (error == 0 && off > roff) 1085ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 1086ef8daf3fSAlexander Motin else { 10873236151eSAlexander Motin error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 10883236151eSAlexander Motin curthread); 1089ef8daf3fSAlexander Motin if (error == 0 && off > roff) 1090ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 1091ef8daf3fSAlexander Motin else { 1092ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 1093ef8daf3fSAlexander Motin off = be_lun->size_bytes; 1094ef8daf3fSAlexander Motin } 1095ef8daf3fSAlexander Motin } 10963236151eSAlexander Motin dev_relthread(dev, ref); 1097ef8daf3fSAlexander Motin 10983236151eSAlexander Motin done: 1099ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 1100ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 11010bcd4ab6SAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 11020bcd4ab6SAlexander Motin lbalen->lba), data->descr[0].length); 1103ef8daf3fSAlexander Motin data->descr[0].status = status; 1104ef8daf3fSAlexander Motin 1105ef8daf3fSAlexander Motin ctl_complete_beio(beio); 1106ef8daf3fSAlexander Motin } 1107ef8daf3fSAlexander Motin 1108ef8daf3fSAlexander Motin static void 1109130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 1110130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 1111130f4520SKenneth D. Merry { 1112130f4520SKenneth D. Merry struct bio *bio; 11133236151eSAlexander Motin struct cdevsw *csw; 11143236151eSAlexander Motin struct cdev *dev; 11153236151eSAlexander Motin int ref; 1116130f4520SKenneth D. Merry 1117130f4520SKenneth D. Merry DPRINTF("entered\n"); 1118130f4520SKenneth D. Merry 1119130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 1120130f4520SKenneth D. Merry bio = g_alloc_bio(); 1121130f4520SKenneth D. Merry 1122130f4520SKenneth D. Merry bio->bio_cmd = BIO_FLUSH; 1123130f4520SKenneth D. Merry bio->bio_offset = 0; 1124130f4520SKenneth D. Merry bio->bio_data = 0; 1125130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 1126130f4520SKenneth D. Merry bio->bio_caller1 = beio; 1127130f4520SKenneth D. Merry bio->bio_pblkno = 0; 1128130f4520SKenneth D. Merry 1129130f4520SKenneth D. Merry /* 1130130f4520SKenneth D. Merry * We don't need to acquire the LUN lock here, because we are only 1131130f4520SKenneth D. Merry * sending one bio, and so there is no other context to synchronize 1132130f4520SKenneth D. Merry * with. 1133130f4520SKenneth D. Merry */ 1134130f4520SKenneth D. Merry beio->num_bios_sent = 1; 1135130f4520SKenneth D. Merry beio->send_complete = 1; 1136130f4520SKenneth D. Merry 1137130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 1138130f4520SKenneth D. Merry devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1139130f4520SKenneth D. Merry 11403236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 11413236151eSAlexander Motin if (csw) { 11423236151eSAlexander Motin bio->bio_dev = dev; 11433236151eSAlexander Motin csw->d_strategy(bio); 11443236151eSAlexander Motin dev_relthread(dev, ref); 11453236151eSAlexander Motin } else { 11463236151eSAlexander Motin bio->bio_error = ENXIO; 11473236151eSAlexander Motin ctl_be_block_biodone(bio); 11483236151eSAlexander Motin } 1149130f4520SKenneth D. Merry } 1150130f4520SKenneth D. Merry 1151130f4520SKenneth D. Merry static void 1152ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1153ee7f31c0SAlexander Motin struct ctl_be_block_io *beio, 1154ee7f31c0SAlexander Motin uint64_t off, uint64_t len, int last) 1155ee7f31c0SAlexander Motin { 1156ee7f31c0SAlexander Motin struct bio *bio; 11578f5a226aSAlexander Motin uint64_t maxlen; 11583236151eSAlexander Motin struct cdevsw *csw; 11593236151eSAlexander Motin struct cdev *dev; 11603236151eSAlexander Motin int ref; 1161ee7f31c0SAlexander Motin 11623236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 11630bcd4ab6SAlexander Motin maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1164ee7f31c0SAlexander Motin while (len > 0) { 1165ee7f31c0SAlexander Motin bio = g_alloc_bio(); 1166ee7f31c0SAlexander Motin bio->bio_cmd = BIO_DELETE; 11673236151eSAlexander Motin bio->bio_dev = dev; 1168ee7f31c0SAlexander Motin bio->bio_offset = off; 11698f5a226aSAlexander Motin bio->bio_length = MIN(len, maxlen); 1170ee7f31c0SAlexander Motin bio->bio_data = 0; 1171ee7f31c0SAlexander Motin bio->bio_done = ctl_be_block_biodone; 1172ee7f31c0SAlexander Motin bio->bio_caller1 = beio; 11730bcd4ab6SAlexander Motin bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1174ee7f31c0SAlexander Motin 1175ee7f31c0SAlexander Motin off += bio->bio_length; 1176ee7f31c0SAlexander Motin len -= bio->bio_length; 1177ee7f31c0SAlexander Motin 117875c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1179ee7f31c0SAlexander Motin beio->num_bios_sent++; 1180ee7f31c0SAlexander Motin if (last && len == 0) 1181ee7f31c0SAlexander Motin beio->send_complete = 1; 118275c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1183ee7f31c0SAlexander Motin 11843236151eSAlexander Motin if (csw) { 11853236151eSAlexander Motin csw->d_strategy(bio); 11863236151eSAlexander Motin } else { 11873236151eSAlexander Motin bio->bio_error = ENXIO; 11883236151eSAlexander Motin ctl_be_block_biodone(bio); 1189ee7f31c0SAlexander Motin } 1190ee7f31c0SAlexander Motin } 11913236151eSAlexander Motin if (csw) 11923236151eSAlexander Motin dev_relthread(dev, ref); 11933236151eSAlexander Motin } 1194ee7f31c0SAlexander Motin 1195ee7f31c0SAlexander Motin static void 1196ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1197ee7f31c0SAlexander Motin struct ctl_be_block_io *beio) 1198ee7f31c0SAlexander Motin { 1199ee7f31c0SAlexander Motin union ctl_io *io; 120066df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1201ee7f31c0SAlexander Motin struct scsi_unmap_desc *buf, *end; 1202ee7f31c0SAlexander Motin uint64_t len; 1203ee7f31c0SAlexander Motin 1204ee7f31c0SAlexander Motin io = beio->io; 1205ee7f31c0SAlexander Motin 1206ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1207ee7f31c0SAlexander Motin 1208ee7f31c0SAlexander Motin binuptime(&beio->ds_t0); 1209ee7f31c0SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1210ee7f31c0SAlexander Motin 1211ee7f31c0SAlexander Motin if (beio->io_offset == -1) { 1212ee7f31c0SAlexander Motin beio->io_len = 0; 121366df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 121466df9136SAlexander Motin buf = (struct scsi_unmap_desc *)ptrlen->ptr; 121566df9136SAlexander Motin end = buf + ptrlen->len / sizeof(*buf); 1216ee7f31c0SAlexander Motin for (; buf < end; buf++) { 1217ee7f31c0SAlexander Motin len = (uint64_t)scsi_4btoul(buf->length) * 12180bcd4ab6SAlexander Motin be_lun->cbe_lun.blocksize; 1219ee7f31c0SAlexander Motin beio->io_len += len; 1220ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 12210bcd4ab6SAlexander Motin scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 12220bcd4ab6SAlexander Motin len, (end - buf < 2) ? TRUE : FALSE); 1223ee7f31c0SAlexander Motin } 1224ee7f31c0SAlexander Motin } else 1225ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 1226ee7f31c0SAlexander Motin beio->io_offset, beio->io_len, TRUE); 1227ee7f31c0SAlexander Motin } 1228ee7f31c0SAlexander Motin 1229ee7f31c0SAlexander Motin static void 1230130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1231130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 1232130f4520SKenneth D. Merry { 123375c7a1d3SAlexander Motin TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1234130f4520SKenneth D. Merry struct bio *bio; 12353236151eSAlexander Motin struct cdevsw *csw; 12363236151eSAlexander Motin struct cdev *dev; 1237130f4520SKenneth D. Merry off_t cur_offset; 12383236151eSAlexander Motin int i, max_iosize, ref; 1239130f4520SKenneth D. Merry 1240130f4520SKenneth D. Merry DPRINTF("entered\n"); 12413236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 1242130f4520SKenneth D. Merry 1243130f4520SKenneth D. Merry /* 1244130f4520SKenneth D. Merry * We have to limit our I/O size to the maximum supported by the 12458054320eSAlexander Motin * backend device. 1246130f4520SKenneth D. Merry */ 12473236151eSAlexander Motin if (csw) { 12483236151eSAlexander Motin max_iosize = dev->si_iosize_max; 1249b06771aaSAlexander Motin if (max_iosize <= 0) 1250130f4520SKenneth D. Merry max_iosize = DFLTPHYS; 12513236151eSAlexander Motin } else 1252b06771aaSAlexander Motin max_iosize = maxphys; 1253130f4520SKenneth D. Merry 1254130f4520SKenneth D. Merry cur_offset = beio->io_offset; 1255130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 1256130f4520SKenneth D. Merry size_t cur_size; 1257130f4520SKenneth D. Merry uint8_t *cur_ptr; 1258130f4520SKenneth D. Merry 1259130f4520SKenneth D. Merry cur_size = beio->sg_segs[i].len; 1260130f4520SKenneth D. Merry cur_ptr = beio->sg_segs[i].addr; 1261130f4520SKenneth D. Merry 1262130f4520SKenneth D. Merry while (cur_size > 0) { 1263130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 1264130f4520SKenneth D. Merry bio = g_alloc_bio(); 1265130f4520SKenneth D. Merry 1266130f4520SKenneth D. Merry KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1267130f4520SKenneth D. Merry 1268130f4520SKenneth D. Merry bio->bio_cmd = beio->bio_cmd; 12693236151eSAlexander Motin bio->bio_dev = dev; 1270130f4520SKenneth D. Merry bio->bio_caller1 = beio; 1271130f4520SKenneth D. Merry bio->bio_length = min(cur_size, max_iosize); 1272130f4520SKenneth D. Merry bio->bio_offset = cur_offset; 1273130f4520SKenneth D. Merry bio->bio_data = cur_ptr; 1274130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 12750bcd4ab6SAlexander Motin bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1276130f4520SKenneth D. Merry 1277130f4520SKenneth D. Merry cur_offset += bio->bio_length; 1278130f4520SKenneth D. Merry cur_ptr += bio->bio_length; 1279130f4520SKenneth D. Merry cur_size -= bio->bio_length; 1280130f4520SKenneth D. Merry 128175c7a1d3SAlexander Motin TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1282130f4520SKenneth D. Merry beio->num_bios_sent++; 1283130f4520SKenneth D. Merry } 1284130f4520SKenneth D. Merry } 128575c7a1d3SAlexander Motin beio->send_complete = 1; 1286024932aaSAlexander Motin binuptime(&beio->ds_t0); 1287024932aaSAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 128875c7a1d3SAlexander Motin 128975c7a1d3SAlexander Motin /* 129075c7a1d3SAlexander Motin * Fire off all allocated requests! 129175c7a1d3SAlexander Motin */ 129275c7a1d3SAlexander Motin while ((bio = TAILQ_FIRST(&queue)) != NULL) { 129375c7a1d3SAlexander Motin TAILQ_REMOVE(&queue, bio, bio_queue); 12943236151eSAlexander Motin if (csw) 12953236151eSAlexander Motin csw->d_strategy(bio); 12963236151eSAlexander Motin else { 12973236151eSAlexander Motin bio->bio_error = ENXIO; 12983236151eSAlexander Motin ctl_be_block_biodone(bio); 129975c7a1d3SAlexander Motin } 1300130f4520SKenneth D. Merry } 13013236151eSAlexander Motin if (csw) 13023236151eSAlexander Motin dev_relthread(dev, ref); 13033236151eSAlexander Motin } 1304130f4520SKenneth D. Merry 1305c3e7ba3eSAlexander Motin static uint64_t 1306c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1307c3e7ba3eSAlexander Motin { 1308c3e7ba3eSAlexander Motin struct diocgattr_arg arg; 13093236151eSAlexander Motin struct cdevsw *csw; 13103236151eSAlexander Motin struct cdev *dev; 13113236151eSAlexander Motin int error, ref; 1312c3e7ba3eSAlexander Motin 13133236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 13143236151eSAlexander Motin if (csw == NULL) 1315c3e7ba3eSAlexander Motin return (UINT64_MAX); 1316c3e7ba3eSAlexander Motin strlcpy(arg.name, attrname, sizeof(arg.name)); 1317c3e7ba3eSAlexander Motin arg.len = sizeof(arg.value.off); 13183236151eSAlexander Motin if (csw->d_ioctl) { 13193236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 13203236151eSAlexander Motin curthread); 13213236151eSAlexander Motin } else 13223236151eSAlexander Motin error = ENODEV; 13233236151eSAlexander Motin dev_relthread(dev, ref); 1324c3e7ba3eSAlexander Motin if (error != 0) 1325c3e7ba3eSAlexander Motin return (UINT64_MAX); 1326c3e7ba3eSAlexander Motin return (arg.value.off); 1327c3e7ba3eSAlexander Motin } 1328c3e7ba3eSAlexander Motin 1329130f4520SKenneth D. Merry static void 1330374f12c5SJohn Baldwin ctl_be_block_namespace_data(struct ctl_be_block_lun *be_lun, 1331374f12c5SJohn Baldwin union ctl_io *io) 1332374f12c5SJohn Baldwin { 1333374f12c5SJohn Baldwin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1334374f12c5SJohn Baldwin struct nvme_namespace_data *nsdata; 1335374f12c5SJohn Baldwin 1336374f12c5SJohn Baldwin nsdata = (struct nvme_namespace_data *)io->nvmeio.kern_data_ptr; 1337374f12c5SJohn Baldwin memset(nsdata, 0, sizeof(*nsdata)); 1338374f12c5SJohn Baldwin nsdata->nsze = htole64(be_lun->size_blocks); 1339374f12c5SJohn Baldwin nsdata->ncap = nsdata->nsze; 1340*1f83483dSJohn Baldwin nsdata->nuse = nsdata->nsze; 1341374f12c5SJohn Baldwin nsdata->nlbaf = 1 - 1; 1342374f12c5SJohn Baldwin nsdata->dlfeat = NVMEM(NVME_NS_DATA_DLFEAT_DWZ) | 1343374f12c5SJohn Baldwin NVMEF(NVME_NS_DATA_DLFEAT_READ, NVME_NS_DATA_DLFEAT_READ_00); 1344374f12c5SJohn Baldwin nsdata->flbas = NVMEF(NVME_NS_DATA_FLBAS_FORMAT, 0); 1345374f12c5SJohn Baldwin nsdata->lbaf[0] = NVMEF(NVME_NS_DATA_LBAF_LBADS, 1346374f12c5SJohn Baldwin ffs(cbe_lun->blocksize) - 1); 1347374f12c5SJohn Baldwin 1348374f12c5SJohn Baldwin ctl_lun_nsdata_ids(cbe_lun, nsdata); 1349374f12c5SJohn Baldwin ctl_config_read_done(io); 1350374f12c5SJohn Baldwin } 1351374f12c5SJohn Baldwin 1352374f12c5SJohn Baldwin static void 1353374f12c5SJohn Baldwin ctl_be_block_nvme_ids(struct ctl_be_block_lun *be_lun, 1354374f12c5SJohn Baldwin union ctl_io *io) 1355374f12c5SJohn Baldwin { 1356374f12c5SJohn Baldwin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1357374f12c5SJohn Baldwin 1358374f12c5SJohn Baldwin ctl_lun_nvme_ids(cbe_lun, io->nvmeio.kern_data_ptr); 1359374f12c5SJohn Baldwin ctl_config_read_done(io); 1360374f12c5SJohn Baldwin } 1361374f12c5SJohn Baldwin 1362374f12c5SJohn Baldwin static void 13637d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 13647d0d4342SAlexander Motin union ctl_io *io) 13657d0d4342SAlexander Motin { 13660bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 13677d0d4342SAlexander Motin struct ctl_be_block_io *beio; 13687d0d4342SAlexander Motin struct ctl_lba_len_flags *lbalen; 13697d0d4342SAlexander Motin 13707d0d4342SAlexander Motin DPRINTF("entered\n"); 13717d0d4342SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 13727d0d4342SAlexander Motin lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 13737d0d4342SAlexander Motin 13740bcd4ab6SAlexander Motin beio->io_len = lbalen->len * cbe_lun->blocksize; 13750bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 13767d0d4342SAlexander Motin beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 13777d0d4342SAlexander Motin beio->bio_cmd = BIO_FLUSH; 13787d0d4342SAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 13797d0d4342SAlexander Motin DPRINTF("SYNC\n"); 13807d0d4342SAlexander Motin be_lun->lun_flush(be_lun, beio); 13817d0d4342SAlexander Motin } 13827d0d4342SAlexander Motin 13837d0d4342SAlexander Motin static void 1384ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1385ee7f31c0SAlexander Motin { 1386ee7f31c0SAlexander Motin union ctl_io *io; 1387ee7f31c0SAlexander Motin 1388ee7f31c0SAlexander Motin io = beio->io; 1389ee7f31c0SAlexander Motin ctl_free_beio(beio); 1390ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1391ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1392ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1393ee7f31c0SAlexander Motin ctl_config_write_done(io); 1394ee7f31c0SAlexander Motin return; 1395ee7f31c0SAlexander Motin } 1396ee7f31c0SAlexander Motin 1397ee7f31c0SAlexander Motin ctl_be_block_config_write(io); 1398ee7f31c0SAlexander Motin } 1399ee7f31c0SAlexander Motin 1400ee7f31c0SAlexander Motin static void 1401ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1402ee7f31c0SAlexander Motin union ctl_io *io) 1403ee7f31c0SAlexander Motin { 14040d7fed74SAlexander Motin struct ctl_be_block_softc *softc = be_lun->softc; 14050bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1406ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 140766df9136SAlexander Motin struct ctl_lba_len_flags *lbalen; 1408fee04ef7SAlexander Motin uint64_t len_left, lba; 1409fee04ef7SAlexander Motin uint32_t pb, pbo, adj; 1410ee7f31c0SAlexander Motin int i, seglen; 1411ee7f31c0SAlexander Motin uint8_t *buf, *end; 1412ee7f31c0SAlexander Motin 1413ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1414ee7f31c0SAlexander Motin 1415374f12c5SJohn Baldwin CTL_IO_ASSERT(io, SCSI); 1416374f12c5SJohn Baldwin 1417e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1418a59e2982SAlexander Motin lbalen = ARGS(io); 1419ee7f31c0SAlexander Motin 142064c5167cSAlexander Motin if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 14213406a2a0SAlexander Motin (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1422ee7f31c0SAlexander Motin ctl_free_beio(beio); 1423ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1424ee7f31c0SAlexander Motin /*sks_valid*/ 1, 1425ee7f31c0SAlexander Motin /*command*/ 1, 1426ee7f31c0SAlexander Motin /*field*/ 1, 1427ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1428ee7f31c0SAlexander Motin /*bit*/ 0); 1429ee7f31c0SAlexander Motin ctl_config_write_done(io); 1430ee7f31c0SAlexander Motin return; 1431ee7f31c0SAlexander Motin } 1432ee7f31c0SAlexander Motin 14333406a2a0SAlexander Motin if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 14340bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 14350bcd4ab6SAlexander Motin beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1436ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1437ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1438ee7f31c0SAlexander Motin 1439ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1440ee7f31c0SAlexander Motin return; 1441ee7f31c0SAlexander Motin } 1442ee7f31c0SAlexander Motin 1443ee7f31c0SAlexander Motin beio->bio_cmd = BIO_WRITE; 1444ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_WRITE; 1445ee7f31c0SAlexander Motin 1446ee7f31c0SAlexander Motin DPRINTF("WRITE SAME at LBA %jx len %u\n", 144766df9136SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len); 1448ee7f31c0SAlexander Motin 14490bcd4ab6SAlexander Motin pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 14500bcd4ab6SAlexander Motin if (be_lun->cbe_lun.pblockoff > 0) 14510bcd4ab6SAlexander Motin pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1452fee04ef7SAlexander Motin else 1453fee04ef7SAlexander Motin pbo = 0; 14540bcd4ab6SAlexander Motin len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1455b06771aaSAlexander Motin for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1456ee7f31c0SAlexander Motin /* 1457ee7f31c0SAlexander Motin * Setup the S/G entry for this chunk. 1458ee7f31c0SAlexander Motin */ 145908a7cce5SAlexander Motin seglen = MIN(CTLBLK_MAX_SEG, len_left); 14600bcd4ab6SAlexander Motin if (pb > cbe_lun->blocksize) { 14610bcd4ab6SAlexander Motin adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 146293b8c96cSAlexander Motin seglen - pbo) % pb; 146393b8c96cSAlexander Motin if (seglen > adj) 146493b8c96cSAlexander Motin seglen -= adj; 146593b8c96cSAlexander Motin else 14660bcd4ab6SAlexander Motin seglen -= seglen % cbe_lun->blocksize; 146793b8c96cSAlexander Motin } else 14680bcd4ab6SAlexander Motin seglen -= seglen % cbe_lun->blocksize; 14698054320eSAlexander Motin ctl_alloc_seg(softc, &beio->sg_segs[i], seglen); 1470ee7f31c0SAlexander Motin 1471ee7f31c0SAlexander Motin DPRINTF("segment %d addr %p len %zd\n", i, 1472ee7f31c0SAlexander Motin beio->sg_segs[i].addr, beio->sg_segs[i].len); 1473ee7f31c0SAlexander Motin 1474ee7f31c0SAlexander Motin beio->num_segs++; 1475ee7f31c0SAlexander Motin len_left -= seglen; 1476ee7f31c0SAlexander Motin 1477ee7f31c0SAlexander Motin buf = beio->sg_segs[i].addr; 1478ee7f31c0SAlexander Motin end = buf + seglen; 14790bcd4ab6SAlexander Motin for (; buf < end; buf += cbe_lun->blocksize) { 14806c2acea5SAlexander Motin if (lbalen->flags & SWS_NDOB) { 14816c2acea5SAlexander Motin memset(buf, 0, cbe_lun->blocksize); 14826c2acea5SAlexander Motin } else { 14836c2acea5SAlexander Motin memcpy(buf, io->scsiio.kern_data_ptr, 14846c2acea5SAlexander Motin cbe_lun->blocksize); 14856c2acea5SAlexander Motin } 148666df9136SAlexander Motin if (lbalen->flags & SWS_LBDATA) 148766df9136SAlexander Motin scsi_ulto4b(lbalen->lba + lba, buf); 1488ee7f31c0SAlexander Motin lba++; 1489ee7f31c0SAlexander Motin } 1490ee7f31c0SAlexander Motin } 1491ee7f31c0SAlexander Motin 14920bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 14930bcd4ab6SAlexander Motin beio->io_len = lba * cbe_lun->blocksize; 1494ee7f31c0SAlexander Motin 1495ee7f31c0SAlexander Motin /* We can not do all in one run. Correct and schedule rerun. */ 1496ee7f31c0SAlexander Motin if (len_left > 0) { 149766df9136SAlexander Motin lbalen->lba += lba; 149866df9136SAlexander Motin lbalen->len -= lba; 1499ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done_ws; 1500ee7f31c0SAlexander Motin } 1501ee7f31c0SAlexander Motin 1502ee7f31c0SAlexander Motin be_lun->dispatch(be_lun, beio); 1503ee7f31c0SAlexander Motin } 1504ee7f31c0SAlexander Motin 1505ee7f31c0SAlexander Motin static void 1506ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1507ee7f31c0SAlexander Motin union ctl_io *io) 1508ee7f31c0SAlexander Motin { 1509ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 151066df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1511ee7f31c0SAlexander Motin 1512ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1513ee7f31c0SAlexander Motin 1514374f12c5SJohn Baldwin CTL_IO_ASSERT(io, SCSI); 1515374f12c5SJohn Baldwin 1516e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 151766df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1518ee7f31c0SAlexander Motin 15193406a2a0SAlexander Motin if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1520ee7f31c0SAlexander Motin ctl_free_beio(beio); 1521ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1522ee7f31c0SAlexander Motin /*sks_valid*/ 0, 1523ee7f31c0SAlexander Motin /*command*/ 1, 1524ee7f31c0SAlexander Motin /*field*/ 0, 1525ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1526ee7f31c0SAlexander Motin /*bit*/ 0); 1527ee7f31c0SAlexander Motin ctl_config_write_done(io); 1528ee7f31c0SAlexander Motin return; 1529ee7f31c0SAlexander Motin } 1530ee7f31c0SAlexander Motin 1531ee7f31c0SAlexander Motin beio->io_len = 0; 1532ee7f31c0SAlexander Motin beio->io_offset = -1; 1533ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1534ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 153566df9136SAlexander Motin DPRINTF("UNMAP\n"); 1536ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1537ee7f31c0SAlexander Motin } 1538ee7f31c0SAlexander Motin 1539ee7f31c0SAlexander Motin static void 1540374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_flush(struct ctl_be_block_lun *be_lun, 1541374f12c5SJohn Baldwin union ctl_io *io) 1542374f12c5SJohn Baldwin { 1543374f12c5SJohn Baldwin struct ctl_be_block_io *beio; 1544374f12c5SJohn Baldwin 1545374f12c5SJohn Baldwin DPRINTF("entered\n"); 1546374f12c5SJohn Baldwin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1547374f12c5SJohn Baldwin 1548374f12c5SJohn Baldwin beio->io_len = be_lun->size_bytes; 1549374f12c5SJohn Baldwin beio->io_offset = 0; 1550374f12c5SJohn Baldwin beio->io_arg = 1; 1551374f12c5SJohn Baldwin beio->bio_cmd = BIO_FLUSH; 1552374f12c5SJohn Baldwin beio->ds_trans_type = DEVSTAT_NO_DATA; 1553374f12c5SJohn Baldwin DPRINTF("FLUSH\n"); 1554374f12c5SJohn Baldwin be_lun->lun_flush(be_lun, beio); 1555374f12c5SJohn Baldwin } 1556374f12c5SJohn Baldwin 1557374f12c5SJohn Baldwin static void 1558374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_wu(struct ctl_be_block_lun *be_lun, 1559374f12c5SJohn Baldwin union ctl_io *io) 1560374f12c5SJohn Baldwin { 1561374f12c5SJohn Baldwin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1562374f12c5SJohn Baldwin struct ctl_be_block_io *beio; 1563374f12c5SJohn Baldwin struct ctl_lba_len_flags *lbalen; 1564374f12c5SJohn Baldwin 1565374f12c5SJohn Baldwin CTL_IO_ASSERT(io, NVME); 1566374f12c5SJohn Baldwin 1567374f12c5SJohn Baldwin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1568374f12c5SJohn Baldwin lbalen = ARGS(io); 1569374f12c5SJohn Baldwin 1570374f12c5SJohn Baldwin /* 1571374f12c5SJohn Baldwin * XXX: Not quite right as reads will return zeroes rather 1572374f12c5SJohn Baldwin * than failing. 1573374f12c5SJohn Baldwin */ 1574374f12c5SJohn Baldwin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1575374f12c5SJohn Baldwin beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1576374f12c5SJohn Baldwin beio->bio_cmd = BIO_DELETE; 1577374f12c5SJohn Baldwin beio->ds_trans_type = DEVSTAT_FREE; 1578374f12c5SJohn Baldwin 1579374f12c5SJohn Baldwin be_lun->unmap(be_lun, beio); 1580374f12c5SJohn Baldwin } 1581374f12c5SJohn Baldwin 1582374f12c5SJohn Baldwin static void 1583374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_wz(struct ctl_be_block_lun *be_lun, 1584374f12c5SJohn Baldwin union ctl_io *io) 1585374f12c5SJohn Baldwin { 1586374f12c5SJohn Baldwin struct ctl_be_block_softc *softc = be_lun->softc; 1587374f12c5SJohn Baldwin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1588374f12c5SJohn Baldwin struct ctl_be_block_io *beio; 1589374f12c5SJohn Baldwin struct ctl_lba_len_flags *lbalen; 1590374f12c5SJohn Baldwin uint64_t len_left, lba; 1591374f12c5SJohn Baldwin uint32_t pb, pbo, adj; 1592374f12c5SJohn Baldwin int i, seglen; 1593374f12c5SJohn Baldwin 1594374f12c5SJohn Baldwin DPRINTF("entered\n"); 1595374f12c5SJohn Baldwin 1596374f12c5SJohn Baldwin CTL_IO_ASSERT(io, NVME); 1597374f12c5SJohn Baldwin 1598374f12c5SJohn Baldwin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1599374f12c5SJohn Baldwin lbalen = ARGS(io); 1600374f12c5SJohn Baldwin 1601374f12c5SJohn Baldwin if ((le32toh(io->nvmeio.cmd.cdw12) & (1U << 25)) != 0 && 1602374f12c5SJohn Baldwin be_lun->unmap != NULL) { 1603374f12c5SJohn Baldwin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1604374f12c5SJohn Baldwin beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1605374f12c5SJohn Baldwin beio->bio_cmd = BIO_DELETE; 1606374f12c5SJohn Baldwin beio->ds_trans_type = DEVSTAT_FREE; 1607374f12c5SJohn Baldwin 1608374f12c5SJohn Baldwin be_lun->unmap(be_lun, beio); 1609374f12c5SJohn Baldwin return; 1610374f12c5SJohn Baldwin } 1611374f12c5SJohn Baldwin 1612374f12c5SJohn Baldwin beio->bio_cmd = BIO_WRITE; 1613374f12c5SJohn Baldwin beio->ds_trans_type = DEVSTAT_WRITE; 1614374f12c5SJohn Baldwin 1615374f12c5SJohn Baldwin DPRINTF("WRITE ZEROES at LBA %jx len %u\n", 1616374f12c5SJohn Baldwin (uintmax_t)lbalen->lba, lbalen->len); 1617374f12c5SJohn Baldwin 1618374f12c5SJohn Baldwin pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1619374f12c5SJohn Baldwin if (be_lun->cbe_lun.pblockoff > 0) 1620374f12c5SJohn Baldwin pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1621374f12c5SJohn Baldwin else 1622374f12c5SJohn Baldwin pbo = 0; 1623374f12c5SJohn Baldwin len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1624374f12c5SJohn Baldwin for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1625374f12c5SJohn Baldwin /* 1626374f12c5SJohn Baldwin * Setup the S/G entry for this chunk. 1627374f12c5SJohn Baldwin */ 1628374f12c5SJohn Baldwin seglen = MIN(CTLBLK_MAX_SEG, len_left); 1629374f12c5SJohn Baldwin if (pb > cbe_lun->blocksize) { 1630374f12c5SJohn Baldwin adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1631374f12c5SJohn Baldwin seglen - pbo) % pb; 1632374f12c5SJohn Baldwin if (seglen > adj) 1633374f12c5SJohn Baldwin seglen -= adj; 1634374f12c5SJohn Baldwin else 1635374f12c5SJohn Baldwin seglen -= seglen % cbe_lun->blocksize; 1636374f12c5SJohn Baldwin } else 1637374f12c5SJohn Baldwin seglen -= seglen % cbe_lun->blocksize; 1638374f12c5SJohn Baldwin ctl_alloc_seg(softc, &beio->sg_segs[i], seglen); 1639374f12c5SJohn Baldwin 1640374f12c5SJohn Baldwin DPRINTF("segment %d addr %p len %zd\n", i, 1641374f12c5SJohn Baldwin beio->sg_segs[i].addr, beio->sg_segs[i].len); 1642374f12c5SJohn Baldwin 1643374f12c5SJohn Baldwin beio->num_segs++; 1644374f12c5SJohn Baldwin len_left -= seglen; 1645374f12c5SJohn Baldwin 1646374f12c5SJohn Baldwin memset(beio->sg_segs[i].addr, 0, seglen); 1647374f12c5SJohn Baldwin lba += seglen / cbe_lun->blocksize; 1648374f12c5SJohn Baldwin } 1649374f12c5SJohn Baldwin 1650374f12c5SJohn Baldwin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1651374f12c5SJohn Baldwin beio->io_len = lba * cbe_lun->blocksize; 1652374f12c5SJohn Baldwin 1653374f12c5SJohn Baldwin /* We can not do all in one run. Correct and schedule rerun. */ 1654374f12c5SJohn Baldwin if (len_left > 0) { 1655374f12c5SJohn Baldwin lbalen->lba += lba; 1656374f12c5SJohn Baldwin lbalen->len -= lba; 1657374f12c5SJohn Baldwin beio->beio_cont = ctl_be_block_cw_done_ws; 1658374f12c5SJohn Baldwin } 1659374f12c5SJohn Baldwin 1660374f12c5SJohn Baldwin be_lun->dispatch(be_lun, beio); 1661374f12c5SJohn Baldwin } 1662374f12c5SJohn Baldwin 1663374f12c5SJohn Baldwin static void 1664374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_dsm(struct ctl_be_block_lun *be_lun, 1665374f12c5SJohn Baldwin union ctl_io *io) 1666374f12c5SJohn Baldwin { 1667374f12c5SJohn Baldwin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1668374f12c5SJohn Baldwin struct ctl_be_block_io *beio; 1669374f12c5SJohn Baldwin struct nvme_dsm_range *r; 1670374f12c5SJohn Baldwin uint64_t lba; 1671374f12c5SJohn Baldwin uint32_t num_blocks; 1672374f12c5SJohn Baldwin u_int i, ranges; 1673374f12c5SJohn Baldwin 1674374f12c5SJohn Baldwin CTL_IO_ASSERT(io, NVME); 1675374f12c5SJohn Baldwin 1676374f12c5SJohn Baldwin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1677374f12c5SJohn Baldwin 1678374f12c5SJohn Baldwin if (be_lun->unmap == NULL) { 1679374f12c5SJohn Baldwin ctl_free_beio(beio); 1680374f12c5SJohn Baldwin ctl_nvme_set_success(&io->nvmeio); 1681374f12c5SJohn Baldwin ctl_config_write_done(io); 1682374f12c5SJohn Baldwin return; 1683374f12c5SJohn Baldwin } 1684374f12c5SJohn Baldwin 1685374f12c5SJohn Baldwin ranges = le32toh(io->nvmeio.cmd.cdw10) & 0xff; 1686374f12c5SJohn Baldwin r = (struct nvme_dsm_range *)io->nvmeio.kern_data_ptr; 1687374f12c5SJohn Baldwin 1688374f12c5SJohn Baldwin /* Find the next range to delete. */ 1689374f12c5SJohn Baldwin for (i = DSM_RANGE(io); i < ranges; i++) { 1690374f12c5SJohn Baldwin if ((le32toh(r[i].attributes) & (1U << 2)) != 0) 1691374f12c5SJohn Baldwin break; 1692374f12c5SJohn Baldwin } 1693374f12c5SJohn Baldwin 1694374f12c5SJohn Baldwin /* If no range to delete, complete the operation. */ 1695374f12c5SJohn Baldwin if (i == ranges) { 1696374f12c5SJohn Baldwin ctl_free_beio(beio); 1697374f12c5SJohn Baldwin ctl_nvme_set_success(&io->nvmeio); 1698374f12c5SJohn Baldwin ctl_config_write_done(io); 1699374f12c5SJohn Baldwin return; 1700374f12c5SJohn Baldwin } 1701374f12c5SJohn Baldwin 1702374f12c5SJohn Baldwin /* If this is not the last range, request a rerun after this range. */ 1703374f12c5SJohn Baldwin if (i + 1 < ranges) { 1704374f12c5SJohn Baldwin DSM_RANGE(io) = i + 1; 1705374f12c5SJohn Baldwin beio->beio_cont = ctl_be_block_cw_done_ws; 1706374f12c5SJohn Baldwin } 1707374f12c5SJohn Baldwin 1708374f12c5SJohn Baldwin lba = le64toh(r[i].starting_lba); 1709374f12c5SJohn Baldwin num_blocks = le32toh(r[i].length); 1710374f12c5SJohn Baldwin 1711374f12c5SJohn Baldwin beio->io_offset = lba * cbe_lun->blocksize; 1712374f12c5SJohn Baldwin beio->io_len = (uint64_t)num_blocks * cbe_lun->blocksize; 1713374f12c5SJohn Baldwin beio->bio_cmd = BIO_DELETE; 1714374f12c5SJohn Baldwin beio->ds_trans_type = DEVSTAT_FREE; 1715374f12c5SJohn Baldwin 1716374f12c5SJohn Baldwin be_lun->unmap(be_lun, beio); 1717374f12c5SJohn Baldwin } 1718374f12c5SJohn Baldwin 1719374f12c5SJohn Baldwin static void 1720374f12c5SJohn Baldwin ctl_be_block_scsi_cr_done(struct ctl_be_block_io *beio) 1721ef8daf3fSAlexander Motin { 1722ef8daf3fSAlexander Motin union ctl_io *io; 1723ef8daf3fSAlexander Motin 1724ef8daf3fSAlexander Motin io = beio->io; 1725ef8daf3fSAlexander Motin ctl_free_beio(beio); 1726ef8daf3fSAlexander Motin ctl_config_read_done(io); 1727ef8daf3fSAlexander Motin } 1728ef8daf3fSAlexander Motin 1729ef8daf3fSAlexander Motin static void 1730374f12c5SJohn Baldwin ctl_be_block_scsi_cr_dispatch(struct ctl_be_block_lun *be_lun, 1731ef8daf3fSAlexander Motin union ctl_io *io) 1732ef8daf3fSAlexander Motin { 1733ef8daf3fSAlexander Motin struct ctl_be_block_io *beio; 1734ef8daf3fSAlexander Motin struct ctl_be_block_softc *softc; 1735ef8daf3fSAlexander Motin 1736ef8daf3fSAlexander Motin DPRINTF("entered\n"); 1737ef8daf3fSAlexander Motin 1738ef8daf3fSAlexander Motin softc = be_lun->softc; 1739ef8daf3fSAlexander Motin beio = ctl_alloc_beio(softc); 1740ef8daf3fSAlexander Motin beio->io = io; 1741ef8daf3fSAlexander Motin beio->lun = be_lun; 1742374f12c5SJohn Baldwin beio->beio_cont = ctl_be_block_scsi_cr_done; 1743ef8daf3fSAlexander Motin PRIV(io)->ptr = (void *)beio; 1744ef8daf3fSAlexander Motin 1745ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 1746ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1747ef8daf3fSAlexander Motin beio->bio_cmd = -1; 1748ef8daf3fSAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 1749ef8daf3fSAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1750ef8daf3fSAlexander Motin beio->io_len = 0; 1751ef8daf3fSAlexander Motin if (be_lun->get_lba_status) 1752ef8daf3fSAlexander Motin be_lun->get_lba_status(be_lun, beio); 1753ef8daf3fSAlexander Motin else 1754374f12c5SJohn Baldwin ctl_be_block_scsi_cr_done(beio); 1755ef8daf3fSAlexander Motin break; 1756ef8daf3fSAlexander Motin default: 1757ef8daf3fSAlexander Motin panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1758ef8daf3fSAlexander Motin break; 1759ef8daf3fSAlexander Motin } 1760ef8daf3fSAlexander Motin } 1761ef8daf3fSAlexander Motin 1762ef8daf3fSAlexander Motin static void 1763374f12c5SJohn Baldwin ctl_be_block_nvme_cr_dispatch(struct ctl_be_block_lun *be_lun, 1764374f12c5SJohn Baldwin union ctl_io *io) 1765374f12c5SJohn Baldwin { 1766374f12c5SJohn Baldwin uint8_t cns; 1767374f12c5SJohn Baldwin 1768374f12c5SJohn Baldwin DPRINTF("entered\n"); 1769374f12c5SJohn Baldwin 1770374f12c5SJohn Baldwin MPASS(io->nvmeio.cmd.opc == NVME_OPC_IDENTIFY); 1771374f12c5SJohn Baldwin 1772374f12c5SJohn Baldwin cns = le32toh(io->nvmeio.cmd.cdw10) & 0xff; 1773374f12c5SJohn Baldwin switch (cns) { 1774374f12c5SJohn Baldwin case 0: 1775374f12c5SJohn Baldwin ctl_be_block_namespace_data(be_lun, io); 1776374f12c5SJohn Baldwin break; 1777374f12c5SJohn Baldwin case 3: 1778374f12c5SJohn Baldwin ctl_be_block_nvme_ids(be_lun, io); 1779374f12c5SJohn Baldwin break; 1780374f12c5SJohn Baldwin default: 1781374f12c5SJohn Baldwin __assert_unreachable(); 1782374f12c5SJohn Baldwin } 1783374f12c5SJohn Baldwin } 1784374f12c5SJohn Baldwin 1785374f12c5SJohn Baldwin static void 1786374f12c5SJohn Baldwin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1787374f12c5SJohn Baldwin union ctl_io *io) 1788374f12c5SJohn Baldwin { 1789374f12c5SJohn Baldwin switch (io->io_hdr.io_type) { 1790374f12c5SJohn Baldwin case CTL_IO_SCSI: 1791374f12c5SJohn Baldwin ctl_be_block_scsi_cr_dispatch(be_lun, io); 1792374f12c5SJohn Baldwin break; 1793374f12c5SJohn Baldwin case CTL_IO_NVME_ADMIN: 1794374f12c5SJohn Baldwin ctl_be_block_nvme_cr_dispatch(be_lun, io); 1795374f12c5SJohn Baldwin break; 1796374f12c5SJohn Baldwin default: 1797374f12c5SJohn Baldwin __assert_unreachable(); 1798374f12c5SJohn Baldwin } 1799374f12c5SJohn Baldwin } 1800374f12c5SJohn Baldwin 1801374f12c5SJohn Baldwin static void 1802ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1803ee7f31c0SAlexander Motin { 1804ee7f31c0SAlexander Motin union ctl_io *io; 1805ee7f31c0SAlexander Motin 1806ee7f31c0SAlexander Motin io = beio->io; 1807ee7f31c0SAlexander Motin ctl_free_beio(beio); 1808ee7f31c0SAlexander Motin ctl_config_write_done(io); 1809ee7f31c0SAlexander Motin } 1810ee7f31c0SAlexander Motin 1811ee7f31c0SAlexander Motin static void 1812374f12c5SJohn Baldwin ctl_be_block_scsi_cw_dispatch(struct ctl_be_block_lun *be_lun, 1813130f4520SKenneth D. Merry union ctl_io *io) 1814130f4520SKenneth D. Merry { 1815130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1816130f4520SKenneth D. Merry 1817130f4520SKenneth D. Merry DPRINTF("entered\n"); 1818130f4520SKenneth D. Merry 1819374f12c5SJohn Baldwin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1820374f12c5SJohn Baldwin 18217d0d4342SAlexander Motin switch (io->scsiio.tag_type) { 18227d0d4342SAlexander Motin case CTL_TAG_ORDERED: 18237d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 18247d0d4342SAlexander Motin break; 18257d0d4342SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 18267d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 18277d0d4342SAlexander Motin break; 18287d0d4342SAlexander Motin case CTL_TAG_UNTAGGED: 18297d0d4342SAlexander Motin case CTL_TAG_SIMPLE: 18307d0d4342SAlexander Motin case CTL_TAG_ACA: 18317d0d4342SAlexander Motin default: 18327d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 18337d0d4342SAlexander Motin break; 18347d0d4342SAlexander Motin } 1835130f4520SKenneth D. Merry 1836130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 1837130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 1838130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 18397d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(be_lun, io); 1840130f4520SKenneth D. Merry break; 1841ee7f31c0SAlexander Motin case WRITE_SAME_10: 1842ee7f31c0SAlexander Motin case WRITE_SAME_16: 1843ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(be_lun, io); 1844ee7f31c0SAlexander Motin break; 1845ee7f31c0SAlexander Motin case UNMAP: 1846ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(be_lun, io); 1847ee7f31c0SAlexander Motin break; 1848130f4520SKenneth D. Merry default: 1849130f4520SKenneth D. Merry panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1850130f4520SKenneth D. Merry break; 1851130f4520SKenneth D. Merry } 1852130f4520SKenneth D. Merry } 1853130f4520SKenneth D. Merry 1854374f12c5SJohn Baldwin static void 1855374f12c5SJohn Baldwin ctl_be_block_nvme_cw_dispatch(struct ctl_be_block_lun *be_lun, 1856374f12c5SJohn Baldwin union ctl_io *io) 1857374f12c5SJohn Baldwin { 1858374f12c5SJohn Baldwin struct ctl_be_block_io *beio; 1859374f12c5SJohn Baldwin 1860374f12c5SJohn Baldwin DPRINTF("entered\n"); 1861374f12c5SJohn Baldwin 1862374f12c5SJohn Baldwin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1863374f12c5SJohn Baldwin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1864374f12c5SJohn Baldwin 1865374f12c5SJohn Baldwin switch (io->nvmeio.cmd.opc) { 1866374f12c5SJohn Baldwin case NVME_OPC_FLUSH: 1867374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_flush(be_lun, io); 1868374f12c5SJohn Baldwin break; 1869374f12c5SJohn Baldwin case NVME_OPC_WRITE_UNCORRECTABLE: 1870374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_wu(be_lun, io); 1871374f12c5SJohn Baldwin break; 1872374f12c5SJohn Baldwin case NVME_OPC_WRITE_ZEROES: 1873374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_wz(be_lun, io); 1874374f12c5SJohn Baldwin break; 1875374f12c5SJohn Baldwin case NVME_OPC_DATASET_MANAGEMENT: 1876374f12c5SJohn Baldwin ctl_be_block_cw_dispatch_dsm(be_lun, io); 1877374f12c5SJohn Baldwin break; 1878374f12c5SJohn Baldwin default: 1879374f12c5SJohn Baldwin __assert_unreachable(); 1880374f12c5SJohn Baldwin } 1881374f12c5SJohn Baldwin } 1882374f12c5SJohn Baldwin 1883374f12c5SJohn Baldwin static void 1884374f12c5SJohn Baldwin ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1885374f12c5SJohn Baldwin union ctl_io *io) 1886374f12c5SJohn Baldwin { 1887374f12c5SJohn Baldwin struct ctl_be_block_io *beio; 1888374f12c5SJohn Baldwin struct ctl_be_block_softc *softc; 1889374f12c5SJohn Baldwin 1890374f12c5SJohn Baldwin softc = be_lun->softc; 1891374f12c5SJohn Baldwin beio = ctl_alloc_beio(softc); 1892374f12c5SJohn Baldwin beio->io = io; 1893374f12c5SJohn Baldwin beio->lun = be_lun; 1894374f12c5SJohn Baldwin beio->beio_cont = ctl_be_block_cw_done; 1895374f12c5SJohn Baldwin PRIV(io)->ptr = (void *)beio; 1896374f12c5SJohn Baldwin 1897374f12c5SJohn Baldwin switch (io->io_hdr.io_type) { 1898374f12c5SJohn Baldwin case CTL_IO_SCSI: 1899374f12c5SJohn Baldwin ctl_be_block_scsi_cw_dispatch(be_lun, io); 1900374f12c5SJohn Baldwin break; 1901374f12c5SJohn Baldwin case CTL_IO_NVME: 1902374f12c5SJohn Baldwin ctl_be_block_nvme_cw_dispatch(be_lun, io); 1903374f12c5SJohn Baldwin break; 1904374f12c5SJohn Baldwin default: 1905374f12c5SJohn Baldwin __assert_unreachable(); 1906374f12c5SJohn Baldwin } 1907374f12c5SJohn Baldwin } 1908374f12c5SJohn Baldwin 190936160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t"); 191036160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t"); 191136160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t"); 191236160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t"); 1913130f4520SKenneth D. Merry 1914130f4520SKenneth D. Merry static void 191508a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio) 191608a7cce5SAlexander Motin { 191708a7cce5SAlexander Motin struct ctl_be_block_lun *be_lun; 191808a7cce5SAlexander Motin union ctl_io *io; 191908a7cce5SAlexander Motin 192008a7cce5SAlexander Motin io = beio->io; 192108a7cce5SAlexander Motin be_lun = beio->lun; 192208a7cce5SAlexander Motin ctl_free_beio(beio); 1923ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1924ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1925ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 192611b569f7SAlexander Motin ctl_data_submit_done(io); 192708a7cce5SAlexander Motin return; 192808a7cce5SAlexander Motin } 192908a7cce5SAlexander Motin 193008a7cce5SAlexander Motin io->io_hdr.status &= ~CTL_STATUS_MASK; 193108a7cce5SAlexander Motin io->io_hdr.status |= CTL_STATUS_NONE; 193208a7cce5SAlexander Motin 193375c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 193408a7cce5SAlexander Motin STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 193575c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 193608a7cce5SAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 193708a7cce5SAlexander Motin } 193808a7cce5SAlexander Motin 193908a7cce5SAlexander Motin static void 1940130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1941130f4520SKenneth D. Merry union ctl_io *io) 1942130f4520SKenneth D. Merry { 19430bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1944130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1945130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 194611b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 1947e86a4142SAlexander Motin struct ctl_ptr_len_flags *bptrlen; 1948e86a4142SAlexander Motin uint64_t len_left, lbas; 1949130f4520SKenneth D. Merry int i; 1950130f4520SKenneth D. Merry 1951130f4520SKenneth D. Merry softc = be_lun->softc; 1952130f4520SKenneth D. Merry 1953130f4520SKenneth D. Merry DPRINTF("entered\n"); 1954130f4520SKenneth D. Merry 195511b569f7SAlexander Motin lbalen = ARGS(io); 195611b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 195736160958SMark Johnston SDT_PROBE0(cbb, , write, start); 195811b569f7SAlexander Motin } else { 195936160958SMark Johnston SDT_PROBE0(cbb, , read, start); 1960130f4520SKenneth D. Merry } 1961130f4520SKenneth D. Merry 1962130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1963130f4520SKenneth D. Merry beio->io = io; 1964130f4520SKenneth D. Merry beio->lun = be_lun; 1965e86a4142SAlexander Motin bptrlen = PRIV(io); 1966e86a4142SAlexander Motin bptrlen->ptr = (void *)beio; 1967130f4520SKenneth D. Merry 1968374f12c5SJohn Baldwin switch (io->io_hdr.io_type) { 1969374f12c5SJohn Baldwin case CTL_IO_SCSI: 1970130f4520SKenneth D. Merry switch (io->scsiio.tag_type) { 1971130f4520SKenneth D. Merry case CTL_TAG_ORDERED: 1972130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1973130f4520SKenneth D. Merry break; 1974130f4520SKenneth D. Merry case CTL_TAG_HEAD_OF_QUEUE: 1975130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1976130f4520SKenneth D. Merry break; 1977130f4520SKenneth D. Merry case CTL_TAG_UNTAGGED: 1978130f4520SKenneth D. Merry case CTL_TAG_SIMPLE: 1979130f4520SKenneth D. Merry case CTL_TAG_ACA: 1980130f4520SKenneth D. Merry default: 1981130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1982130f4520SKenneth D. Merry break; 1983130f4520SKenneth D. Merry } 1984374f12c5SJohn Baldwin break; 1985374f12c5SJohn Baldwin case CTL_IO_NVME: 1986374f12c5SJohn Baldwin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1987374f12c5SJohn Baldwin break; 1988374f12c5SJohn Baldwin default: 1989374f12c5SJohn Baldwin __assert_unreachable(); 1990374f12c5SJohn Baldwin } 1991130f4520SKenneth D. Merry 199211b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1993130f4520SKenneth D. Merry beio->bio_cmd = BIO_WRITE; 1994130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_WRITE; 199511b569f7SAlexander Motin } else { 199611b569f7SAlexander Motin beio->bio_cmd = BIO_READ; 199711b569f7SAlexander Motin beio->ds_trans_type = DEVSTAT_READ; 1998130f4520SKenneth D. Merry } 1999130f4520SKenneth D. Merry 200008a7cce5SAlexander Motin DPRINTF("%s at LBA %jx len %u @%ju\n", 2001130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 2002e86a4142SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 2003b06771aaSAlexander Motin lbas = CTLBLK_MAX_IO_SIZE; 20040d7fed74SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) { 20050d7fed74SAlexander Motin beio->two_sglists = 1; 2006b06771aaSAlexander Motin lbas /= 2; 20070d7fed74SAlexander Motin } 20080bcd4ab6SAlexander Motin lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 20090bcd4ab6SAlexander Motin beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 20100bcd4ab6SAlexander Motin beio->io_len = lbas * cbe_lun->blocksize; 2011e86a4142SAlexander Motin bptrlen->len += lbas; 2012130f4520SKenneth D. Merry 201308a7cce5SAlexander Motin for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 201408a7cce5SAlexander Motin KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 201508a7cce5SAlexander Motin i, CTLBLK_MAX_SEGS)); 2016130f4520SKenneth D. Merry 2017130f4520SKenneth D. Merry /* 2018130f4520SKenneth D. Merry * Setup the S/G entry for this chunk. 2019130f4520SKenneth D. Merry */ 20208054320eSAlexander Motin ctl_alloc_seg(softc, &beio->sg_segs[i], 2021cd853791SKonstantin Belousov MIN(CTLBLK_MAX_SEG, len_left)); 2022130f4520SKenneth D. Merry 2023130f4520SKenneth D. Merry DPRINTF("segment %d addr %p len %zd\n", i, 2024130f4520SKenneth D. Merry beio->sg_segs[i].addr, beio->sg_segs[i].len); 2025130f4520SKenneth D. Merry 202611b569f7SAlexander Motin /* Set up second segment for compare operation. */ 20270d7fed74SAlexander Motin if (beio->two_sglists) { 20288054320eSAlexander Motin ctl_alloc_seg(softc, 20298054320eSAlexander Motin &beio->sg_segs[i + CTLBLK_HALF_SEGS], 20308054320eSAlexander Motin beio->sg_segs[i].len); 203111b569f7SAlexander Motin } 203211b569f7SAlexander Motin 2033130f4520SKenneth D. Merry beio->num_segs++; 2034130f4520SKenneth D. Merry len_left -= beio->sg_segs[i].len; 2035130f4520SKenneth D. Merry } 2036e86a4142SAlexander Motin if (bptrlen->len < lbalen->len) 203708a7cce5SAlexander Motin beio->beio_cont = ctl_be_block_next; 203840a43590SJohn Baldwin ctl_set_be_move_done(io, ctl_be_block_move_done); 203911b569f7SAlexander Motin /* For compare we have separate S/G lists for read and datamove. */ 20400d7fed74SAlexander Motin if (beio->two_sglists) 204140a43590SJohn Baldwin ctl_set_kern_data_ptr(io, &beio->sg_segs[CTLBLK_HALF_SEGS]); 204211b569f7SAlexander Motin else 204340a43590SJohn Baldwin ctl_set_kern_data_ptr(io, beio->sg_segs); 204440a43590SJohn Baldwin ctl_set_kern_data_len(io, beio->io_len); 204540a43590SJohn Baldwin ctl_set_kern_sg_entries(io, beio->num_segs); 204640a43590SJohn Baldwin ctl_set_kern_data_ref(io, ctl_refcnt_beio); 204740a43590SJohn Baldwin ctl_set_kern_data_arg(io, beio); 2048b2221369SAlexander Motin io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 2049130f4520SKenneth D. Merry 2050130f4520SKenneth D. Merry /* 2051130f4520SKenneth D. Merry * For the read case, we need to read the data into our buffers and 2052130f4520SKenneth D. Merry * then we can send it back to the user. For the write case, we 2053130f4520SKenneth D. Merry * need to get the data from the user first. 2054130f4520SKenneth D. Merry */ 2055130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 205636160958SMark Johnston SDT_PROBE0(cbb, , read, alloc_done); 2057130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 2058130f4520SKenneth D. Merry } else { 205936160958SMark Johnston SDT_PROBE0(cbb, , write, alloc_done); 2060130f4520SKenneth D. Merry ctl_datamove(io); 2061130f4520SKenneth D. Merry } 2062130f4520SKenneth D. Merry } 2063130f4520SKenneth D. Merry 2064130f4520SKenneth D. Merry static void 2065130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending) 2066130f4520SKenneth D. Merry { 2067ee4ad294SAlexander Motin struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 2068ee4ad294SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2069130f4520SKenneth D. Merry union ctl_io *io; 2070130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 2071130f4520SKenneth D. Merry 2072ee4ad294SAlexander Motin DPRINTF("entered\n"); 2073ee4ad294SAlexander Motin /* 2074ee4ad294SAlexander Motin * Fetch and process I/Os from all queues. If we detect LUN 2075648dfc1aSAlexander Motin * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race, 2076ee4ad294SAlexander Motin * so make response maximally opaque to not confuse initiator. 2077ee4ad294SAlexander Motin */ 2078ee4ad294SAlexander Motin for (;;) { 2079ee4ad294SAlexander Motin mtx_lock(&be_lun->queue_lock); 2080ee4ad294SAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 2081ee4ad294SAlexander Motin if (io != NULL) { 2082130f4520SKenneth D. Merry DPRINTF("datamove queue\n"); 208305d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links); 208475c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2085e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 2086648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 208740a43590SJohn Baldwin ctl_io_set_busy(io); 2088ee4ad294SAlexander Motin ctl_complete_beio(beio); 20896ed39db2SAlexander Motin continue; 2090ee4ad294SAlexander Motin } 2091130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 2092130f4520SKenneth D. Merry continue; 2093130f4520SKenneth D. Merry } 2094130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 2095130f4520SKenneth D. Merry if (io != NULL) { 2096130f4520SKenneth D. Merry DPRINTF("config write queue\n"); 209705d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links); 209875c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2099648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 210040a43590SJohn Baldwin ctl_io_set_busy(io); 2101ee4ad294SAlexander Motin ctl_config_write_done(io); 21026ed39db2SAlexander Motin continue; 2103ee4ad294SAlexander Motin } 2104130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(be_lun, io); 2105ef8daf3fSAlexander Motin continue; 2106ef8daf3fSAlexander Motin } 2107ef8daf3fSAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 2108ef8daf3fSAlexander Motin if (io != NULL) { 2109ef8daf3fSAlexander Motin DPRINTF("config read queue\n"); 211005d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links); 2111ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 2112648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 211340a43590SJohn Baldwin ctl_io_set_busy(io); 2114ee4ad294SAlexander Motin ctl_config_read_done(io); 21156ed39db2SAlexander Motin continue; 2116ee4ad294SAlexander Motin } 2117ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(be_lun, io); 2118130f4520SKenneth D. Merry continue; 2119130f4520SKenneth D. Merry } 2120130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 2121130f4520SKenneth D. Merry if (io != NULL) { 2122130f4520SKenneth D. Merry DPRINTF("input queue\n"); 212305d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->input_queue, links); 212475c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2125648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 212640a43590SJohn Baldwin ctl_io_set_busy(io); 2127ee4ad294SAlexander Motin ctl_data_submit_done(io); 21286ed39db2SAlexander Motin continue; 2129ee4ad294SAlexander Motin } 2130130f4520SKenneth D. Merry ctl_be_block_dispatch(be_lun, io); 2131130f4520SKenneth D. Merry continue; 2132130f4520SKenneth D. Merry } 2133130f4520SKenneth D. Merry 2134130f4520SKenneth D. Merry /* 2135130f4520SKenneth D. Merry * If we get here, there is no work left in the queues, so 2136130f4520SKenneth D. Merry * just break out and let the task queue go to sleep. 2137130f4520SKenneth D. Merry */ 2138ee4ad294SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2139130f4520SKenneth D. Merry break; 2140130f4520SKenneth D. Merry } 2141130f4520SKenneth D. Merry } 2142130f4520SKenneth D. Merry 2143130f4520SKenneth D. Merry /* 2144130f4520SKenneth D. Merry * Entry point from CTL to the backend for I/O. We queue everything to a 2145130f4520SKenneth D. Merry * work thread, so this just puts the I/O on a queue and wakes up the 2146130f4520SKenneth D. Merry * thread. 2147130f4520SKenneth D. Merry */ 2148130f4520SKenneth D. Merry static int 2149130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io) 2150130f4520SKenneth D. Merry { 2151130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2152130f4520SKenneth D. Merry 2153130f4520SKenneth D. Merry DPRINTF("entered\n"); 2154130f4520SKenneth D. Merry 2155767300e8SAlexander Motin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 2156130f4520SKenneth D. Merry 2157374f12c5SJohn Baldwin CTL_IO_ASSERT(io, SCSI, NVME); 2158130f4520SKenneth D. Merry 2159e86a4142SAlexander Motin PRIV(io)->len = 0; 2160e86a4142SAlexander Motin 216175c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 2162130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 216375c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2164130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2165130f4520SKenneth D. Merry 21669c71cd5aSAlexander Motin return (CTL_RETVAL_COMPLETE); 2167130f4520SKenneth D. Merry } 2168130f4520SKenneth D. Merry 2169130f4520SKenneth D. Merry static int 2170130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 2171130f4520SKenneth D. Merry int flag, struct thread *td) 2172130f4520SKenneth D. Merry { 217334144c2cSAlexander Motin struct ctl_be_block_softc *softc = &backend_block_softc; 2174130f4520SKenneth D. Merry int error; 2175130f4520SKenneth D. Merry 2176130f4520SKenneth D. Merry error = 0; 2177130f4520SKenneth D. Merry switch (cmd) { 2178130f4520SKenneth D. Merry case CTL_LUN_REQ: { 2179130f4520SKenneth D. Merry struct ctl_lun_req *lun_req; 2180130f4520SKenneth D. Merry 2181130f4520SKenneth D. Merry lun_req = (struct ctl_lun_req *)addr; 2182130f4520SKenneth D. Merry 2183130f4520SKenneth D. Merry switch (lun_req->reqtype) { 2184130f4520SKenneth D. Merry case CTL_LUNREQ_CREATE: 2185130f4520SKenneth D. Merry error = ctl_be_block_create(softc, lun_req); 2186130f4520SKenneth D. Merry break; 2187130f4520SKenneth D. Merry case CTL_LUNREQ_RM: 2188130f4520SKenneth D. Merry error = ctl_be_block_rm(softc, lun_req); 2189130f4520SKenneth D. Merry break; 219081177295SEdward Tomasz Napierala case CTL_LUNREQ_MODIFY: 219181177295SEdward Tomasz Napierala error = ctl_be_block_modify(softc, lun_req); 219281177295SEdward Tomasz Napierala break; 2193130f4520SKenneth D. Merry default: 2194130f4520SKenneth D. Merry lun_req->status = CTL_LUN_ERROR; 2195130f4520SKenneth D. Merry snprintf(lun_req->error_str, sizeof(lun_req->error_str), 219619720f41SAlexander Motin "invalid LUN request type %d", 2197130f4520SKenneth D. Merry lun_req->reqtype); 2198130f4520SKenneth D. Merry break; 2199130f4520SKenneth D. Merry } 2200130f4520SKenneth D. Merry break; 2201130f4520SKenneth D. Merry } 2202130f4520SKenneth D. Merry default: 2203130f4520SKenneth D. Merry error = ENOTTY; 2204130f4520SKenneth D. Merry break; 2205130f4520SKenneth D. Merry } 2206130f4520SKenneth D. Merry 2207130f4520SKenneth D. Merry return (error); 2208130f4520SKenneth D. Merry } 2209130f4520SKenneth D. Merry 2210130f4520SKenneth D. Merry static int 2211130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2212130f4520SKenneth D. Merry { 22130bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 2214130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 2215130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 22168951f055SMarcelo Araujo const char *value; 2217130f4520SKenneth D. Merry struct vattr vattr; 221834961f40SAlexander Motin off_t ps, pss, po, pos, us, uss, uo, uos; 2219130f4520SKenneth D. Merry int error; 222049050613SKa Ho Ng long pconf; 2221130f4520SKenneth D. Merry 22220bcd4ab6SAlexander Motin cbe_lun = &be_lun->cbe_lun; 2223130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 222419720f41SAlexander Motin params = &be_lun->params; 2225130f4520SKenneth D. Merry 2226130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_FILE; 2227130f4520SKenneth D. Merry be_lun->dispatch = ctl_be_block_dispatch_file; 2228130f4520SKenneth D. Merry be_lun->lun_flush = ctl_be_block_flush_file; 2229ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_file; 223053c146deSAlexander Motin be_lun->getattr = ctl_be_block_getattr_file; 223149050613SKa Ho Ng be_lun->unmap = ctl_be_block_unmap_file; 22320bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2233130f4520SKenneth D. Merry 2234130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2235130f4520SKenneth D. Merry if (error != 0) { 2236130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2237130f4520SKenneth D. Merry "error calling VOP_GETATTR() for file %s", 2238130f4520SKenneth D. Merry be_lun->dev_path); 2239130f4520SKenneth D. Merry return (error); 2240130f4520SKenneth D. Merry } 2241130f4520SKenneth D. Merry 224249050613SKa Ho Ng error = VOP_PATHCONF(be_lun->vn, _PC_DEALLOC_PRESENT, &pconf); 224349050613SKa Ho Ng if (error != 0) { 224449050613SKa Ho Ng snprintf(req->error_str, sizeof(req->error_str), 224549050613SKa Ho Ng "error calling VOP_PATHCONF() for file %s", 224649050613SKa Ho Ng be_lun->dev_path); 224749050613SKa Ho Ng return (error); 224849050613SKa Ho Ng } 224949050613SKa Ho Ng if (pconf == 1) 225049050613SKa Ho Ng cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 225149050613SKa Ho Ng 2252130f4520SKenneth D. Merry file_data->cred = crhold(curthread->td_ucred); 225381177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) 225481177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 225581177295SEdward Tomasz Napierala else 2256130f4520SKenneth D. Merry be_lun->size_bytes = vattr.va_size; 2257130f4520SKenneth D. Merry 2258130f4520SKenneth D. Merry /* 225920a28d6cSAlexander Motin * For files we can use any logical block size. Prefer 512 bytes 226020a28d6cSAlexander Motin * for compatibility reasons. If file's vattr.va_blocksize 226120a28d6cSAlexander Motin * (preferred I/O block size) is bigger and multiple to chosen 226220a28d6cSAlexander Motin * logical block size -- report it as physical block size. 2263130f4520SKenneth D. Merry */ 2264130f4520SKenneth D. Merry if (params->blocksize_bytes != 0) 22650bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 226691be33dcSAlexander Motin else if (cbe_lun->lun_type == T_CDROM) 226791be33dcSAlexander Motin cbe_lun->blocksize = 2048; 2268130f4520SKenneth D. Merry else 22690bcd4ab6SAlexander Motin cbe_lun->blocksize = 512; 22700bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 22710bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 22720bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 227334961f40SAlexander Motin 227434961f40SAlexander Motin us = ps = vattr.va_blocksize; 227534961f40SAlexander Motin uo = po = 0; 227634961f40SAlexander Motin 22778951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL); 227834961f40SAlexander Motin if (value != NULL) 227934961f40SAlexander Motin ctl_expand_number(value, &ps); 22808951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL); 228134961f40SAlexander Motin if (value != NULL) 228234961f40SAlexander Motin ctl_expand_number(value, &po); 22830bcd4ab6SAlexander Motin pss = ps / cbe_lun->blocksize; 22840bcd4ab6SAlexander Motin pos = po / cbe_lun->blocksize; 22850bcd4ab6SAlexander Motin if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 22860bcd4ab6SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 22870bcd4ab6SAlexander Motin cbe_lun->pblockexp = fls(pss) - 1; 22880bcd4ab6SAlexander Motin cbe_lun->pblockoff = (pss - pos) % pss; 228934961f40SAlexander Motin } 229034961f40SAlexander Motin 22918951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL); 229234961f40SAlexander Motin if (value != NULL) 229334961f40SAlexander Motin ctl_expand_number(value, &us); 22948951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL); 229534961f40SAlexander Motin if (value != NULL) 229634961f40SAlexander Motin ctl_expand_number(value, &uo); 22970bcd4ab6SAlexander Motin uss = us / cbe_lun->blocksize; 22980bcd4ab6SAlexander Motin uos = uo / cbe_lun->blocksize; 22990bcd4ab6SAlexander Motin if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 23000bcd4ab6SAlexander Motin ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 23010bcd4ab6SAlexander Motin cbe_lun->ublockexp = fls(uss) - 1; 23020bcd4ab6SAlexander Motin cbe_lun->ublockoff = (uss - uos) % uss; 230320a28d6cSAlexander Motin } 2304130f4520SKenneth D. Merry 2305130f4520SKenneth D. Merry /* 2306130f4520SKenneth D. Merry * Sanity check. The media size has to be at least one 2307130f4520SKenneth D. Merry * sector long. 2308130f4520SKenneth D. Merry */ 23090bcd4ab6SAlexander Motin if (be_lun->size_bytes < cbe_lun->blocksize) { 2310130f4520SKenneth D. Merry error = EINVAL; 2311130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2312130f4520SKenneth D. Merry "file %s size %ju < block size %u", be_lun->dev_path, 23130bcd4ab6SAlexander Motin (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 2314130f4520SKenneth D. Merry } 2315cb8727e2SAlexander Motin 23160bcd4ab6SAlexander Motin cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 2317130f4520SKenneth D. Merry return (error); 2318130f4520SKenneth D. Merry } 2319130f4520SKenneth D. Merry 2320130f4520SKenneth D. Merry static int 2321130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2322130f4520SKenneth D. Merry { 23230bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2324130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 23253236151eSAlexander Motin struct cdevsw *csw; 2326130f4520SKenneth D. Merry struct cdev *dev; 23278951f055SMarcelo Araujo const char *value; 23283236151eSAlexander Motin int error, atomic, maxio, ref, unmap, tmp; 2329f6295033SAlexander Motin off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 2330130f4520SKenneth D. Merry 233119720f41SAlexander Motin params = &be_lun->params; 2332130f4520SKenneth D. Merry 2333130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_DEV; 23343236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 23353236151eSAlexander Motin if (csw == NULL) 23363236151eSAlexander Motin return (ENXIO); 23373236151eSAlexander Motin if (strcmp(csw->d_name, "zvol") == 0) { 233867f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_zvol; 2339ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_zvol; 2340cb8727e2SAlexander Motin atomic = maxio = CTLBLK_MAX_IO_SIZE; 2341cb8727e2SAlexander Motin } else { 234267f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_dev; 23430bcd4ab6SAlexander Motin be_lun->get_lba_status = NULL; 2344cb8727e2SAlexander Motin atomic = 0; 23453236151eSAlexander Motin maxio = dev->si_iosize_max; 2346cb8727e2SAlexander Motin if (maxio <= 0) 2347cb8727e2SAlexander Motin maxio = DFLTPHYS; 23488054320eSAlexander Motin if (maxio > CTLBLK_MAX_SEG) 23498054320eSAlexander Motin maxio = CTLBLK_MAX_SEG; 2350cb8727e2SAlexander Motin } 235167f586a8SAlexander Motin be_lun->lun_flush = ctl_be_block_flush_dev; 2352c3e7ba3eSAlexander Motin be_lun->getattr = ctl_be_block_getattr_dev; 23530bcd4ab6SAlexander Motin be_lun->unmap = ctl_be_block_unmap_dev; 2354130f4520SKenneth D. Merry 23553236151eSAlexander Motin if (!csw->d_ioctl) { 23563236151eSAlexander Motin dev_relthread(dev, ref); 2357130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 23583236151eSAlexander Motin "no d_ioctl for device %s!", be_lun->dev_path); 2359130f4520SKenneth D. Merry return (ENODEV); 2360130f4520SKenneth D. Merry } 2361130f4520SKenneth D. Merry 23623236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 2363130f4520SKenneth D. Merry curthread); 2364130f4520SKenneth D. Merry if (error) { 23653236151eSAlexander Motin dev_relthread(dev, ref); 2366130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 236719720f41SAlexander Motin "error %d returned for DIOCGSECTORSIZE ioctl " 236819720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 2369130f4520SKenneth D. Merry return (error); 2370130f4520SKenneth D. Merry } 2371130f4520SKenneth D. Merry 2372130f4520SKenneth D. Merry /* 2373130f4520SKenneth D. Merry * If the user has asked for a blocksize that is greater than the 2374130f4520SKenneth D. Merry * backing device's blocksize, we can do it only if the blocksize 2375130f4520SKenneth D. Merry * the user is asking for is an even multiple of the underlying 2376130f4520SKenneth D. Merry * device's blocksize. 2377130f4520SKenneth D. Merry */ 2378a15bbf15SAlexander Motin if ((params->blocksize_bytes != 0) && 2379a15bbf15SAlexander Motin (params->blocksize_bytes >= tmp)) { 2380a15bbf15SAlexander Motin if (params->blocksize_bytes % tmp == 0) { 23810bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 2382130f4520SKenneth D. Merry } else { 23833236151eSAlexander Motin dev_relthread(dev, ref); 2384130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 238519720f41SAlexander Motin "requested blocksize %u is not an even " 2386130f4520SKenneth D. Merry "multiple of backing device blocksize %u", 2387f6295033SAlexander Motin params->blocksize_bytes, tmp); 2388130f4520SKenneth D. Merry return (EINVAL); 2389130f4520SKenneth D. Merry } 2390a15bbf15SAlexander Motin } else if (params->blocksize_bytes != 0) { 23913236151eSAlexander Motin dev_relthread(dev, ref); 2392130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 239319720f41SAlexander Motin "requested blocksize %u < backing device " 2394f6295033SAlexander Motin "blocksize %u", params->blocksize_bytes, tmp); 2395130f4520SKenneth D. Merry return (EINVAL); 239691be33dcSAlexander Motin } else if (cbe_lun->lun_type == T_CDROM) 239791be33dcSAlexander Motin cbe_lun->blocksize = MAX(tmp, 2048); 239891be33dcSAlexander Motin else 23990bcd4ab6SAlexander Motin cbe_lun->blocksize = tmp; 2400130f4520SKenneth D. Merry 24013236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 2402130f4520SKenneth D. Merry curthread); 2403130f4520SKenneth D. Merry if (error) { 24043236151eSAlexander Motin dev_relthread(dev, ref); 2405130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 240619720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE " 240719720f41SAlexander Motin " ioctl on %s!", error, 240881177295SEdward Tomasz Napierala be_lun->dev_path); 2409130f4520SKenneth D. Merry return (error); 2410130f4520SKenneth D. Merry } 2411130f4520SKenneth D. Merry 241281177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 2413f6295033SAlexander Motin if (params->lun_size_bytes > otmp) { 24143236151eSAlexander Motin dev_relthread(dev, ref); 241581177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 241619720f41SAlexander Motin "requested LUN size %ju > backing device " 241719720f41SAlexander Motin "size %ju", 241881177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 2419f6295033SAlexander Motin (uintmax_t)otmp); 242081177295SEdward Tomasz Napierala return (EINVAL); 2421130f4520SKenneth D. Merry } 2422130f4520SKenneth D. Merry 242381177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 2424a15bbf15SAlexander Motin } else 2425f6295033SAlexander Motin be_lun->size_bytes = otmp; 24260bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 24270bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 24280bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 242981177295SEdward Tomasz Napierala 24303236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 24313236151eSAlexander Motin curthread); 2432f6012722SAlexander Motin if (error) 2433f6012722SAlexander Motin ps = po = 0; 2434f6012722SAlexander Motin else { 24353236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 24363236151eSAlexander Motin FREAD, curthread); 2437f6012722SAlexander Motin if (error) 2438f6012722SAlexander Motin po = 0; 2439f6012722SAlexander Motin } 244034961f40SAlexander Motin us = ps; 244134961f40SAlexander Motin uo = po; 244234961f40SAlexander Motin 24438951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL); 244434961f40SAlexander Motin if (value != NULL) 244534961f40SAlexander Motin ctl_expand_number(value, &ps); 24468951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL); 244734961f40SAlexander Motin if (value != NULL) 244834961f40SAlexander Motin ctl_expand_number(value, &po); 24490bcd4ab6SAlexander Motin pss = ps / cbe_lun->blocksize; 24500bcd4ab6SAlexander Motin pos = po / cbe_lun->blocksize; 24510bcd4ab6SAlexander Motin if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 24520bcd4ab6SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 24530bcd4ab6SAlexander Motin cbe_lun->pblockexp = fls(pss) - 1; 24540bcd4ab6SAlexander Motin cbe_lun->pblockoff = (pss - pos) % pss; 2455f6012722SAlexander Motin } 2456f6012722SAlexander Motin 24578951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL); 245834961f40SAlexander Motin if (value != NULL) 245934961f40SAlexander Motin ctl_expand_number(value, &us); 24608951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL); 246134961f40SAlexander Motin if (value != NULL) 246234961f40SAlexander Motin ctl_expand_number(value, &uo); 24630bcd4ab6SAlexander Motin uss = us / cbe_lun->blocksize; 24640bcd4ab6SAlexander Motin uos = uo / cbe_lun->blocksize; 24650bcd4ab6SAlexander Motin if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 24660bcd4ab6SAlexander Motin ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 24670bcd4ab6SAlexander Motin cbe_lun->ublockexp = fls(uss) - 1; 24680bcd4ab6SAlexander Motin cbe_lun->ublockoff = (uss - uos) % uss; 246934961f40SAlexander Motin } 247034961f40SAlexander Motin 24710bcd4ab6SAlexander Motin cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 24720bcd4ab6SAlexander Motin cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2473fbc8d4ffSAlexander Motin 2474fbc8d4ffSAlexander Motin if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2475fbc8d4ffSAlexander Motin unmap = 1; 2476fbc8d4ffSAlexander Motin } else { 2477fbc8d4ffSAlexander Motin struct diocgattr_arg arg; 2478fbc8d4ffSAlexander Motin 2479fbc8d4ffSAlexander Motin strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2480fbc8d4ffSAlexander Motin arg.len = sizeof(arg.value.i); 24813236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 24823236151eSAlexander Motin curthread); 2483fbc8d4ffSAlexander Motin unmap = (error == 0) ? arg.value.i : 0; 2484fbc8d4ffSAlexander Motin } 24858951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "unmap", NULL); 2486fbc8d4ffSAlexander Motin if (value != NULL) 2487fbc8d4ffSAlexander Motin unmap = (strcmp(value, "on") == 0); 2488fbc8d4ffSAlexander Motin if (unmap) 24890bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 24900bcd4ab6SAlexander Motin else 24910bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2492fbc8d4ffSAlexander Motin 24933236151eSAlexander Motin dev_relthread(dev, ref); 249481177295SEdward Tomasz Napierala return (0); 249581177295SEdward Tomasz Napierala } 2496130f4520SKenneth D. Merry 2497130f4520SKenneth D. Merry static int 2498130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun) 2499130f4520SKenneth D. Merry { 25000bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 25010bcd4ab6SAlexander Motin int flags; 2502130f4520SKenneth D. Merry 25030bcd4ab6SAlexander Motin if (be_lun->vn) { 25040bcd4ab6SAlexander Motin flags = FREAD; 25050bcd4ab6SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 25060bcd4ab6SAlexander Motin flags |= FWRITE; 2507130f4520SKenneth D. Merry (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2508130f4520SKenneth D. Merry be_lun->vn = NULL; 2509130f4520SKenneth D. Merry 2510130f4520SKenneth D. Merry switch (be_lun->dev_type) { 2511130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 2512130f4520SKenneth D. Merry break; 2513130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 2514130f4520SKenneth D. Merry if (be_lun->backend.file.cred != NULL) { 2515130f4520SKenneth D. Merry crfree(be_lun->backend.file.cred); 2516130f4520SKenneth D. Merry be_lun->backend.file.cred = NULL; 2517130f4520SKenneth D. Merry } 2518130f4520SKenneth D. Merry break; 2519130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 2520025a2301SEdward Tomasz Napierala break; 2521130f4520SKenneth D. Merry default: 25225124012aSAlexander Motin panic("Unexpected backend type %d", be_lun->dev_type); 2523130f4520SKenneth D. Merry break; 2524130f4520SKenneth D. Merry } 252519720f41SAlexander Motin be_lun->dev_type = CTL_BE_BLOCK_NONE; 2526130f4520SKenneth D. Merry } 2527130f4520SKenneth D. Merry return (0); 2528130f4520SKenneth D. Merry } 2529130f4520SKenneth D. Merry 2530130f4520SKenneth D. Merry static int 2531648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2532130f4520SKenneth D. Merry { 25330bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2534130f4520SKenneth D. Merry struct nameidata nd; 25358951f055SMarcelo Araujo const char *value; 25360bcd4ab6SAlexander Motin int error, flags; 2537130f4520SKenneth D. Merry 2538130f4520SKenneth D. Merry error = 0; 2539130f4520SKenneth D. Merry if (rootvnode == NULL) { 2540130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 254119720f41SAlexander Motin "Root filesystem is not mounted"); 2542130f4520SKenneth D. Merry return (1); 2543130f4520SKenneth D. Merry } 25448a08cec1SMateusz Guzik pwd_ensure_dirs(); 2545130f4520SKenneth D. Merry 25468951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "file", NULL); 25470bcd4ab6SAlexander Motin if (value == NULL) { 25480bcd4ab6SAlexander Motin snprintf(req->error_str, sizeof(req->error_str), 25490bcd4ab6SAlexander Motin "no file argument specified"); 25500bcd4ab6SAlexander Motin return (1); 25510bcd4ab6SAlexander Motin } 25520bcd4ab6SAlexander Motin free(be_lun->dev_path, M_CTLBLK); 25530bcd4ab6SAlexander Motin be_lun->dev_path = strdup(value, M_CTLBLK); 25540bcd4ab6SAlexander Motin 25550bcd4ab6SAlexander Motin flags = FREAD; 25568951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "readonly", NULL); 255791be33dcSAlexander Motin if (value != NULL) { 255891be33dcSAlexander Motin if (strcmp(value, "on") != 0) 255991be33dcSAlexander Motin flags |= FWRITE; 256091be33dcSAlexander Motin } else if (cbe_lun->lun_type == T_DIRECT) 25610bcd4ab6SAlexander Motin flags |= FWRITE; 25620bcd4ab6SAlexander Motin 2563130f4520SKenneth D. Merry again: 25647e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path); 2565130f4520SKenneth D. Merry error = vn_open(&nd, &flags, 0, NULL); 25660bcd4ab6SAlexander Motin if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 25670bcd4ab6SAlexander Motin flags &= ~FWRITE; 25680bcd4ab6SAlexander Motin goto again; 25690bcd4ab6SAlexander Motin } 2570130f4520SKenneth D. Merry if (error) { 2571130f4520SKenneth D. Merry /* 2572130f4520SKenneth D. Merry * This is the only reasonable guess we can make as far as 2573130f4520SKenneth D. Merry * path if the user doesn't give us a fully qualified path. 2574130f4520SKenneth D. Merry * If they want to specify a file, they need to specify the 2575130f4520SKenneth D. Merry * full path. 2576130f4520SKenneth D. Merry */ 2577130f4520SKenneth D. Merry if (be_lun->dev_path[0] != '/') { 2578130f4520SKenneth D. Merry char *dev_name; 2579130f4520SKenneth D. Merry 25800bcd4ab6SAlexander Motin asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2581130f4520SKenneth D. Merry be_lun->dev_path); 2582130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 2583130f4520SKenneth D. Merry be_lun->dev_path = dev_name; 2584130f4520SKenneth D. Merry goto again; 2585130f4520SKenneth D. Merry } 2586130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 258719720f41SAlexander Motin "error opening %s: %d", be_lun->dev_path, error); 2588130f4520SKenneth D. Merry return (error); 2589130f4520SKenneth D. Merry } 25900bcd4ab6SAlexander Motin if (flags & FWRITE) 25910bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 25920bcd4ab6SAlexander Motin else 25930bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2594130f4520SKenneth D. Merry 2595bb92cd7bSMateusz Guzik NDFREE_PNBUF(&nd); 2596130f4520SKenneth D. Merry be_lun->vn = nd.ni_vp; 2597130f4520SKenneth D. Merry 2598130f4520SKenneth D. Merry /* We only support disks and files. */ 25997ad2a82dSMateusz Guzik if (vn_isdisk_error(be_lun->vn, &error)) { 2600130f4520SKenneth D. Merry error = ctl_be_block_open_dev(be_lun, req); 2601130f4520SKenneth D. Merry } else if (be_lun->vn->v_type == VREG) { 2602130f4520SKenneth D. Merry error = ctl_be_block_open_file(be_lun, req); 2603130f4520SKenneth D. Merry } else { 2604130f4520SKenneth D. Merry error = EINVAL; 2605130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2606025a2301SEdward Tomasz Napierala "%s is not a disk or plain file", be_lun->dev_path); 2607130f4520SKenneth D. Merry } 2608b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 2609130f4520SKenneth D. Merry 2610a15bbf15SAlexander Motin if (error != 0) 2611130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 26120bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 26130bcd4ab6SAlexander Motin if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2614ac503c19SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT; 26158951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "serseq", NULL); 26160bcd4ab6SAlexander Motin if (value != NULL && strcmp(value, "on") == 0) 26170bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 26180bcd4ab6SAlexander Motin else if (value != NULL && strcmp(value, "read") == 0) 26190bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2620ac503c19SAlexander Motin else if (value != NULL && strcmp(value, "soft") == 0) 2621ac503c19SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT; 26220bcd4ab6SAlexander Motin else if (value != NULL && strcmp(value, "off") == 0) 26230bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2624130f4520SKenneth D. Merry return (0); 2625130f4520SKenneth D. Merry } 2626130f4520SKenneth D. Merry 2627130f4520SKenneth D. Merry static int 2628130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2629130f4520SKenneth D. Merry { 26300bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 2631130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2632130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 263357a5db13SAlexander Motin char num_thread_str[16]; 2634130f4520SKenneth D. Merry char tmpstr[32]; 26358951f055SMarcelo Araujo const char *value; 2636fbc8d4ffSAlexander Motin int retval, num_threads; 263757a5db13SAlexander Motin int tmp_num_threads; 2638130f4520SKenneth D. Merry 2639130f4520SKenneth D. Merry params = &req->reqdata.create; 2640130f4520SKenneth D. Merry retval = 0; 264119720f41SAlexander Motin req->status = CTL_LUN_OK; 2642130f4520SKenneth D. Merry 2643130f4520SKenneth D. Merry be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 26440bcd4ab6SAlexander Motin cbe_lun = &be_lun->cbe_lun; 264519720f41SAlexander Motin be_lun->params = req->reqdata.create; 2646130f4520SKenneth D. Merry be_lun->softc = softc; 2647130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->input_queue); 2648ef8daf3fSAlexander Motin STAILQ_INIT(&be_lun->config_read_queue); 2649130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->config_write_queue); 2650130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->datamove_queue); 265134144c2cSAlexander Motin mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF); 265234144c2cSAlexander Motin mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF); 26538951f055SMarcelo Araujo cbe_lun->options = nvlist_clone(req->args_nvl); 2654130f4520SKenneth D. Merry 2655130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 26560bcd4ab6SAlexander Motin cbe_lun->lun_type = params->device_type; 2657130f4520SKenneth D. Merry else 26580bcd4ab6SAlexander Motin cbe_lun->lun_type = T_DIRECT; 265934144c2cSAlexander Motin be_lun->flags = 0; 26607ac58230SAlexander Motin cbe_lun->flags = 0; 26618951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 26627ac58230SAlexander Motin if (value != NULL) { 26637ac58230SAlexander Motin if (strcmp(value, "primary") == 0) 26647ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 26657ac58230SAlexander Motin } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 26667ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2667130f4520SKenneth D. Merry 266891be33dcSAlexander Motin if (cbe_lun->lun_type == T_DIRECT || 266991be33dcSAlexander Motin cbe_lun->lun_type == T_CDROM) { 2670a15bbf15SAlexander Motin be_lun->size_bytes = params->lun_size_bytes; 2671a15bbf15SAlexander Motin if (params->blocksize_bytes != 0) 26720bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 267391be33dcSAlexander Motin else if (cbe_lun->lun_type == T_CDROM) 267491be33dcSAlexander Motin cbe_lun->blocksize = 2048; 2675a15bbf15SAlexander Motin else 26760bcd4ab6SAlexander Motin cbe_lun->blocksize = 512; 26770bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 26780bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 26790bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 2680130f4520SKenneth D. Merry 26817ac58230SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 26827ac58230SAlexander Motin control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2683648dfc1aSAlexander Motin retval = ctl_be_block_open(be_lun, req); 2684130f4520SKenneth D. Merry if (retval != 0) { 2685130f4520SKenneth D. Merry retval = 0; 268619720f41SAlexander Motin req->status = CTL_LUN_WARNING; 2687130f4520SKenneth D. Merry } 26887ac58230SAlexander Motin } 26890bcd4ab6SAlexander Motin num_threads = cbb_num_threads; 2690130f4520SKenneth D. Merry } else { 2691130f4520SKenneth D. Merry num_threads = 1; 2692130f4520SKenneth D. Merry } 2693130f4520SKenneth D. Merry 26948951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL); 269557a5db13SAlexander Motin if (value != NULL) { 269657a5db13SAlexander Motin tmp_num_threads = strtol(value, NULL, 0); 2697130f4520SKenneth D. Merry 2698130f4520SKenneth D. Merry /* 2699130f4520SKenneth D. Merry * We don't let the user specify less than one 2700130f4520SKenneth D. Merry * thread, but hope he's clueful enough not to 2701130f4520SKenneth D. Merry * specify 1000 threads. 2702130f4520SKenneth D. Merry */ 2703130f4520SKenneth D. Merry if (tmp_num_threads < 1) { 2704130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 270519720f41SAlexander Motin "invalid number of threads %s", 270619720f41SAlexander Motin num_thread_str); 2707130f4520SKenneth D. Merry goto bailout_error; 2708130f4520SKenneth D. Merry } 2709130f4520SKenneth D. Merry num_threads = tmp_num_threads; 271057a5db13SAlexander Motin } 2711130f4520SKenneth D. Merry 271219720f41SAlexander Motin if (be_lun->vn == NULL) 2713648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2714130f4520SKenneth D. Merry /* Tell the user the blocksize we ended up using */ 271519720f41SAlexander Motin params->lun_size_bytes = be_lun->size_bytes; 27160bcd4ab6SAlexander Motin params->blocksize_bytes = cbe_lun->blocksize; 2717130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_ID_REQ) { 27180bcd4ab6SAlexander Motin cbe_lun->req_lun_id = params->req_lun_id; 27190bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2720130f4520SKenneth D. Merry } else 27210bcd4ab6SAlexander Motin cbe_lun->req_lun_id = 0; 2722130f4520SKenneth D. Merry 27230bcd4ab6SAlexander Motin cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 27240bcd4ab6SAlexander Motin cbe_lun->be = &ctl_be_block_driver; 2725130f4520SKenneth D. Merry 2726130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 272771cd87c6SAlan Somers snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d", 2728130f4520SKenneth D. Merry softc->num_luns); 27290bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->serial_num, tmpstr, 27300bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2731130f4520SKenneth D. Merry 2732130f4520SKenneth D. Merry /* Tell the user what we used for a serial number */ 2733130f4520SKenneth D. Merry strncpy((char *)params->serial_num, tmpstr, 2734e7038eb7SAlexander Motin MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2735130f4520SKenneth D. Merry } else { 27360bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->serial_num, params->serial_num, 27370bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->serial_num), 2738130f4520SKenneth D. Merry sizeof(params->serial_num))); 2739130f4520SKenneth D. Merry } 2740130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 274171cd87c6SAlan Somers snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns); 27420bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->device_id, tmpstr, 27430bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2744130f4520SKenneth D. Merry 2745130f4520SKenneth D. Merry /* Tell the user what we used for a device ID */ 2746130f4520SKenneth D. Merry strncpy((char *)params->device_id, tmpstr, 2747e7038eb7SAlexander Motin MIN(sizeof(params->device_id), sizeof(tmpstr))); 2748130f4520SKenneth D. Merry } else { 27490bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->device_id, params->device_id, 27500bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->device_id), 2751130f4520SKenneth D. Merry sizeof(params->device_id))); 2752130f4520SKenneth D. Merry } 2753130f4520SKenneth D. Merry 2754130f4520SKenneth D. Merry TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2755130f4520SKenneth D. Merry 275634144c2cSAlexander Motin be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK, 2757130f4520SKenneth D. Merry taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2758130f4520SKenneth D. Merry 2759130f4520SKenneth D. Merry if (be_lun->io_taskqueue == NULL) { 2760130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 276119720f41SAlexander Motin "unable to create taskqueue"); 2762130f4520SKenneth D. Merry goto bailout_error; 2763130f4520SKenneth D. Merry } 2764130f4520SKenneth D. Merry 2765130f4520SKenneth D. Merry /* 2766130f4520SKenneth D. Merry * Note that we start the same number of threads by default for 2767130f4520SKenneth D. Merry * both the file case and the block device case. For the file 2768130f4520SKenneth D. Merry * case, we need multiple threads to allow concurrency, because the 2769130f4520SKenneth D. Merry * vnode interface is designed to be a blocking interface. For the 2770130f4520SKenneth D. Merry * block device case, ZFS zvols at least will block the caller's 2771130f4520SKenneth D. Merry * context in many instances, and so we need multiple threads to 2772130f4520SKenneth D. Merry * overcome that problem. Other block devices don't need as many 2773130f4520SKenneth D. Merry * threads, but they shouldn't cause too many problems. 2774130f4520SKenneth D. Merry * 2775130f4520SKenneth D. Merry * If the user wants to just have a single thread for a block 2776130f4520SKenneth D. Merry * device, he can specify that when the LUN is created, or change 2777130f4520SKenneth D. Merry * the tunable/sysctl to alter the default number of threads. 2778130f4520SKenneth D. Merry */ 277912373e95SAlexander Motin retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue, 2780130f4520SKenneth D. Merry /*num threads*/num_threads, 2781053db1feSAlexander Motin /*priority*/PUSER, 278212373e95SAlexander Motin /*proc*/control_softc->ctl_proc, 278334144c2cSAlexander Motin /*thread name*/"block"); 2784130f4520SKenneth D. Merry 2785130f4520SKenneth D. Merry if (retval != 0) 2786130f4520SKenneth D. Merry goto bailout_error; 2787130f4520SKenneth D. Merry 2788130f4520SKenneth D. Merry be_lun->num_threads = num_threads; 2789130f4520SKenneth D. Merry 27900bcd4ab6SAlexander Motin retval = ctl_add_lun(&be_lun->cbe_lun); 2791130f4520SKenneth D. Merry if (retval != 0) { 2792130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 279319720f41SAlexander Motin "ctl_add_lun() returned error %d, see dmesg for " 279419720f41SAlexander Motin "details", retval); 2795130f4520SKenneth D. Merry retval = 0; 2796130f4520SKenneth D. Merry goto bailout_error; 2797130f4520SKenneth D. Merry } 2798130f4520SKenneth D. Merry 279934144c2cSAlexander Motin be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id, 28000bcd4ab6SAlexander Motin cbe_lun->blocksize, 2801130f4520SKenneth D. Merry DEVSTAT_ALL_SUPPORTED, 28020bcd4ab6SAlexander Motin cbe_lun->lun_type 2803130f4520SKenneth D. Merry | DEVSTAT_TYPE_IF_OTHER, 2804130f4520SKenneth D. Merry DEVSTAT_PRIORITY_OTHER); 2805130f4520SKenneth D. Merry 280634144c2cSAlexander Motin mtx_lock(&softc->lock); 280734144c2cSAlexander Motin softc->num_luns++; 280834144c2cSAlexander Motin SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links); 280934144c2cSAlexander Motin mtx_unlock(&softc->lock); 281034144c2cSAlexander Motin 281134144c2cSAlexander Motin params->req_lun_id = cbe_lun->lun_id; 281234144c2cSAlexander Motin 2813130f4520SKenneth D. Merry return (retval); 2814130f4520SKenneth D. Merry 2815130f4520SKenneth D. Merry bailout_error: 2816130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2817130f4520SKenneth D. Merry 28189e005bbcSAlexander Motin if (be_lun->io_taskqueue != NULL) 28199e005bbcSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 2820130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 28219e005bbcSAlexander Motin if (be_lun->dev_path != NULL) 2822130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 28238951f055SMarcelo Araujo nvlist_destroy(cbe_lun->options); 282475c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 282575c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2826130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2827130f4520SKenneth D. Merry 2828130f4520SKenneth D. Merry return (retval); 2829130f4520SKenneth D. Merry } 2830130f4520SKenneth D. Merry 2831130f4520SKenneth D. Merry static int 2832130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2833130f4520SKenneth D. Merry { 2834130f4520SKenneth D. Merry struct ctl_lun_rm_params *params; 2835130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2836ee4ad294SAlexander Motin struct ctl_be_lun *cbe_lun; 2837130f4520SKenneth D. Merry int retval; 2838130f4520SKenneth D. Merry 2839130f4520SKenneth D. Merry params = &req->reqdata.rm; 2840130f4520SKenneth D. Merry 284134144c2cSAlexander Motin sx_xlock(&softc->modify_lock); 2842130f4520SKenneth D. Merry mtx_lock(&softc->lock); 284334144c2cSAlexander Motin SLIST_FOREACH(be_lun, &softc->lun_list, links) { 284434144c2cSAlexander Motin if (be_lun->cbe_lun.lun_id == params->lun_id) { 284534144c2cSAlexander Motin SLIST_REMOVE(&softc->lun_list, be_lun, 284634144c2cSAlexander Motin ctl_be_block_lun, links); 284734144c2cSAlexander Motin softc->num_luns--; 2848130f4520SKenneth D. Merry break; 2849130f4520SKenneth D. Merry } 285034144c2cSAlexander Motin } 2851130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 285234144c2cSAlexander Motin sx_xunlock(&softc->modify_lock); 2853130f4520SKenneth D. Merry if (be_lun == NULL) { 2854130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 285519720f41SAlexander Motin "LUN %u is not managed by the block backend", 285619720f41SAlexander Motin params->lun_id); 2857130f4520SKenneth D. Merry goto bailout_error; 2858130f4520SKenneth D. Merry } 2859ee4ad294SAlexander Motin cbe_lun = &be_lun->cbe_lun; 2860130f4520SKenneth D. Merry 2861ee4ad294SAlexander Motin if (be_lun->vn != NULL) { 2862648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2863648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 2864ee4ad294SAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 2865ee4ad294SAlexander Motin ctl_be_block_close(be_lun); 2866ee4ad294SAlexander Motin } 2867ee4ad294SAlexander Motin 286834144c2cSAlexander Motin mtx_lock(&softc->lock); 286934144c2cSAlexander Motin be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 287034144c2cSAlexander Motin mtx_unlock(&softc->lock); 287134144c2cSAlexander Motin 287234144c2cSAlexander Motin retval = ctl_remove_lun(cbe_lun); 2873130f4520SKenneth D. Merry if (retval != 0) { 2874130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 287534144c2cSAlexander Motin "error %d returned from ctl_remove_lun() for " 287619720f41SAlexander Motin "LUN %d", retval, params->lun_id); 287734144c2cSAlexander Motin mtx_lock(&softc->lock); 287834144c2cSAlexander Motin be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 287934144c2cSAlexander Motin mtx_unlock(&softc->lock); 2880130f4520SKenneth D. Merry goto bailout_error; 2881130f4520SKenneth D. Merry } 2882130f4520SKenneth D. Merry 2883130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2884130f4520SKenneth D. Merry while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 288534144c2cSAlexander Motin retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0); 2886130f4520SKenneth D. Merry if (retval == EINTR) 2887130f4520SKenneth D. Merry break; 2888130f4520SKenneth D. Merry } 2889130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 289034144c2cSAlexander Motin if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2891130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2892130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 289334144c2cSAlexander Motin } else { 289434144c2cSAlexander Motin mtx_unlock(&softc->lock); 289534144c2cSAlexander Motin return (EINTR); 289634144c2cSAlexander Motin } 2897130f4520SKenneth D. Merry 2898130f4520SKenneth D. Merry req->status = CTL_LUN_OK; 2899130f4520SKenneth D. Merry return (0); 2900130f4520SKenneth D. Merry 2901130f4520SKenneth D. Merry bailout_error: 2902130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2903130f4520SKenneth D. Merry return (0); 2904130f4520SKenneth D. Merry } 2905130f4520SKenneth D. Merry 290681177295SEdward Tomasz Napierala static int 290781177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 290881177295SEdward Tomasz Napierala { 290981177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 291081177295SEdward Tomasz Napierala struct ctl_be_block_lun *be_lun; 2911a3977beaSAlexander Motin struct ctl_be_lun *cbe_lun; 29128951f055SMarcelo Araujo const char *value; 291371d8e97eSAlexander Motin uint64_t oldsize; 29147ac58230SAlexander Motin int error, wasprim; 291581177295SEdward Tomasz Napierala 291681177295SEdward Tomasz Napierala params = &req->reqdata.modify; 291781177295SEdward Tomasz Napierala 291834144c2cSAlexander Motin sx_xlock(&softc->modify_lock); 291981177295SEdward Tomasz Napierala mtx_lock(&softc->lock); 292034144c2cSAlexander Motin SLIST_FOREACH(be_lun, &softc->lun_list, links) { 29210bcd4ab6SAlexander Motin if (be_lun->cbe_lun.lun_id == params->lun_id) 292281177295SEdward Tomasz Napierala break; 292381177295SEdward Tomasz Napierala } 292481177295SEdward Tomasz Napierala mtx_unlock(&softc->lock); 292581177295SEdward Tomasz Napierala if (be_lun == NULL) { 292681177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 292719720f41SAlexander Motin "LUN %u is not managed by the block backend", 292819720f41SAlexander Motin params->lun_id); 292981177295SEdward Tomasz Napierala goto bailout_error; 293081177295SEdward Tomasz Napierala } 2931a3977beaSAlexander Motin cbe_lun = &be_lun->cbe_lun; 293281177295SEdward Tomasz Napierala 2933a3977beaSAlexander Motin if (params->lun_size_bytes != 0) 293419720f41SAlexander Motin be_lun->params.lun_size_bytes = params->lun_size_bytes; 29358951f055SMarcelo Araujo 2936efeedddcSAlexander Motin if (req->args_nvl != NULL) { 29378951f055SMarcelo Araujo nvlist_destroy(cbe_lun->options); 29388951f055SMarcelo Araujo cbe_lun->options = nvlist_clone(req->args_nvl); 2939efeedddcSAlexander Motin } 294081177295SEdward Tomasz Napierala 29417ac58230SAlexander Motin wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 29428951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 29437ac58230SAlexander Motin if (value != NULL) { 29447ac58230SAlexander Motin if (strcmp(value, "primary") == 0) 29457ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 29467ac58230SAlexander Motin else 29477ac58230SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 29487ac58230SAlexander Motin } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 29497ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 29507ac58230SAlexander Motin else 29517ac58230SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 29527ac58230SAlexander Motin if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 29537ac58230SAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 29547ac58230SAlexander Motin ctl_lun_primary(cbe_lun); 29557ac58230SAlexander Motin else 29567ac58230SAlexander Motin ctl_lun_secondary(cbe_lun); 29577ac58230SAlexander Motin } 29587ac58230SAlexander Motin 29590bcd4ab6SAlexander Motin oldsize = be_lun->size_blocks; 29607ac58230SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 29617ac58230SAlexander Motin control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 296219720f41SAlexander Motin if (be_lun->vn == NULL) 2963648dfc1aSAlexander Motin error = ctl_be_block_open(be_lun, req); 29647ad2a82dSMateusz Guzik else if (vn_isdisk_error(be_lun->vn, &error)) 29654ce7a086SAlexander Motin error = ctl_be_block_open_dev(be_lun, req); 29663d5cb709SAlexander Motin else if (be_lun->vn->v_type == VREG) { 29673d5cb709SAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 29684ce7a086SAlexander Motin error = ctl_be_block_open_file(be_lun, req); 2969b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 29703d5cb709SAlexander Motin } else 2971b9b4269cSAlexander Motin error = EINVAL; 2972648dfc1aSAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) && 29730bcd4ab6SAlexander Motin be_lun->vn != NULL) { 2974648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2975648dfc1aSAlexander Motin ctl_lun_has_media(cbe_lun); 2976648dfc1aSAlexander Motin } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 && 2977648dfc1aSAlexander Motin be_lun->vn == NULL) { 2978648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2979648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 298071d8e97eSAlexander Motin } 2981648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 29827ac58230SAlexander Motin } else { 29837ac58230SAlexander Motin if (be_lun->vn != NULL) { 2984648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2985648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 2986ee4ad294SAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 29877ac58230SAlexander Motin error = ctl_be_block_close(be_lun); 29887ac58230SAlexander Motin } else 29897ac58230SAlexander Motin error = 0; 29907ac58230SAlexander Motin } 29917ac58230SAlexander Motin if (be_lun->size_blocks != oldsize) 29927ac58230SAlexander Motin ctl_lun_capacity_changed(cbe_lun); 299381177295SEdward Tomasz Napierala 299481177295SEdward Tomasz Napierala /* Tell the user the exact size we ended up using */ 299581177295SEdward Tomasz Napierala params->lun_size_bytes = be_lun->size_bytes; 299681177295SEdward Tomasz Napierala 299734144c2cSAlexander Motin sx_xunlock(&softc->modify_lock); 299819720f41SAlexander Motin req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 299981177295SEdward Tomasz Napierala return (0); 300081177295SEdward Tomasz Napierala 300181177295SEdward Tomasz Napierala bailout_error: 300234144c2cSAlexander Motin sx_xunlock(&softc->modify_lock); 300381177295SEdward Tomasz Napierala req->status = CTL_LUN_ERROR; 300481177295SEdward Tomasz Napierala return (0); 300581177295SEdward Tomasz Napierala } 300681177295SEdward Tomasz Napierala 3007130f4520SKenneth D. Merry static void 3008767300e8SAlexander Motin ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun) 3009130f4520SKenneth D. Merry { 3010767300e8SAlexander Motin struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun; 301134144c2cSAlexander Motin struct ctl_be_block_softc *softc = be_lun->softc; 301234144c2cSAlexander Motin 301334144c2cSAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 301434144c2cSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 301534144c2cSAlexander Motin if (be_lun->disk_stats != NULL) 301634144c2cSAlexander Motin devstat_remove_entry(be_lun->disk_stats); 301734144c2cSAlexander Motin nvlist_destroy(be_lun->cbe_lun.options); 301834144c2cSAlexander Motin free(be_lun->dev_path, M_CTLBLK); 301934144c2cSAlexander Motin mtx_destroy(&be_lun->queue_lock); 302034144c2cSAlexander Motin mtx_destroy(&be_lun->io_lock); 3021130f4520SKenneth D. Merry 3022130f4520SKenneth D. Merry mtx_lock(&softc->lock); 302334144c2cSAlexander Motin be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 302434144c2cSAlexander Motin if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING) 302534144c2cSAlexander Motin wakeup(be_lun); 302634144c2cSAlexander Motin else 302734144c2cSAlexander Motin free(be_lun, M_CTLBLK); 3028130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 3029130f4520SKenneth D. Merry } 3030130f4520SKenneth D. Merry 3031130f4520SKenneth D. Merry static int 3032374f12c5SJohn Baldwin ctl_be_block_scsi_config_write(union ctl_io *io) 3033130f4520SKenneth D. Merry { 3034130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 30350bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 3036130f4520SKenneth D. Merry int retval; 3037130f4520SKenneth D. Merry 3038130f4520SKenneth D. Merry DPRINTF("entered\n"); 3039130f4520SKenneth D. Merry 30409cbbfd2fSAlexander Motin cbe_lun = CTL_BACKEND_LUN(io); 3041767300e8SAlexander Motin be_lun = (struct ctl_be_block_lun *)cbe_lun; 3042130f4520SKenneth D. Merry 304367cc546dSAlexander Motin retval = 0; 3044130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 3045130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 3046130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 3047ee7f31c0SAlexander Motin case WRITE_SAME_10: 3048ee7f31c0SAlexander Motin case WRITE_SAME_16: 3049ee7f31c0SAlexander Motin case UNMAP: 3050130f4520SKenneth D. Merry /* 3051130f4520SKenneth D. Merry * The upper level CTL code will filter out any CDBs with 3052130f4520SKenneth D. Merry * the immediate bit set and return the proper error. 3053130f4520SKenneth D. Merry * 3054130f4520SKenneth D. Merry * We don't really need to worry about what LBA range the 3055130f4520SKenneth D. Merry * user asked to be synced out. When they issue a sync 3056130f4520SKenneth D. Merry * cache command, we'll sync out the whole thing. 3057130f4520SKenneth D. Merry */ 305875c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 3059130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 3060130f4520SKenneth D. Merry links); 306175c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 3062130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 3063130f4520SKenneth D. Merry break; 3064130f4520SKenneth D. Merry case START_STOP_UNIT: { 3065130f4520SKenneth D. Merry struct scsi_start_stop_unit *cdb; 3066648dfc1aSAlexander Motin struct ctl_lun_req req; 3067130f4520SKenneth D. Merry 3068130f4520SKenneth D. Merry cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 306966b69676SAlexander Motin if ((cdb->how & SSS_PC_MASK) != 0) { 307066b69676SAlexander Motin ctl_set_success(&io->scsiio); 307166b69676SAlexander Motin ctl_config_write_done(io); 307266b69676SAlexander Motin break; 307366b69676SAlexander Motin } 3074648dfc1aSAlexander Motin if (cdb->how & SSS_START) { 3075648dfc1aSAlexander Motin if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) { 3076648dfc1aSAlexander Motin retval = ctl_be_block_open(be_lun, &req); 3077648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 3078648dfc1aSAlexander Motin if (retval == 0) { 3079648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 3080648dfc1aSAlexander Motin ctl_lun_has_media(cbe_lun); 3081130f4520SKenneth D. Merry } else { 3082648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 3083648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 3084130f4520SKenneth D. Merry } 3085648dfc1aSAlexander Motin } 3086648dfc1aSAlexander Motin ctl_start_lun(cbe_lun); 3087648dfc1aSAlexander Motin } else { 3088648dfc1aSAlexander Motin ctl_stop_lun(cbe_lun); 3089648dfc1aSAlexander Motin if (cdb->how & SSS_LOEJ) { 3090648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 3091648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_EJECTED; 3092648dfc1aSAlexander Motin ctl_lun_ejected(cbe_lun); 3093648dfc1aSAlexander Motin if (be_lun->vn != NULL) 3094648dfc1aSAlexander Motin ctl_be_block_close(be_lun); 3095648dfc1aSAlexander Motin } 3096648dfc1aSAlexander Motin } 3097648dfc1aSAlexander Motin 3098648dfc1aSAlexander Motin ctl_set_success(&io->scsiio); 3099130f4520SKenneth D. Merry ctl_config_write_done(io); 3100130f4520SKenneth D. Merry break; 3101130f4520SKenneth D. Merry } 310291be33dcSAlexander Motin case PREVENT_ALLOW: 310391be33dcSAlexander Motin ctl_set_success(&io->scsiio); 310491be33dcSAlexander Motin ctl_config_write_done(io); 310591be33dcSAlexander Motin break; 3106130f4520SKenneth D. Merry default: 3107130f4520SKenneth D. Merry ctl_set_invalid_opcode(&io->scsiio); 3108130f4520SKenneth D. Merry ctl_config_write_done(io); 3109130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 3110130f4520SKenneth D. Merry break; 3111130f4520SKenneth D. Merry } 3112130f4520SKenneth D. Merry 3113130f4520SKenneth D. Merry return (retval); 3114130f4520SKenneth D. Merry } 3115130f4520SKenneth D. Merry 3116130f4520SKenneth D. Merry static int 3117374f12c5SJohn Baldwin ctl_be_block_nvme_config_write(union ctl_io *io) 3118374f12c5SJohn Baldwin { 3119374f12c5SJohn Baldwin struct ctl_be_block_lun *be_lun; 3120374f12c5SJohn Baldwin 3121374f12c5SJohn Baldwin DPRINTF("entered\n"); 3122374f12c5SJohn Baldwin 3123374f12c5SJohn Baldwin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 3124374f12c5SJohn Baldwin 3125374f12c5SJohn Baldwin switch (io->nvmeio.cmd.opc) { 3126374f12c5SJohn Baldwin case NVME_OPC_DATASET_MANAGEMENT: 3127374f12c5SJohn Baldwin DSM_RANGE(io) = 0; 3128374f12c5SJohn Baldwin /* FALLTHROUGH */ 3129374f12c5SJohn Baldwin case NVME_OPC_FLUSH: 3130374f12c5SJohn Baldwin case NVME_OPC_WRITE_UNCORRECTABLE: 3131374f12c5SJohn Baldwin case NVME_OPC_WRITE_ZEROES: 3132374f12c5SJohn Baldwin mtx_lock(&be_lun->queue_lock); 3133374f12c5SJohn Baldwin STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 3134374f12c5SJohn Baldwin links); 3135374f12c5SJohn Baldwin mtx_unlock(&be_lun->queue_lock); 3136374f12c5SJohn Baldwin taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 3137374f12c5SJohn Baldwin break; 3138374f12c5SJohn Baldwin default: 3139374f12c5SJohn Baldwin ctl_nvme_set_invalid_opcode(&io->nvmeio); 3140374f12c5SJohn Baldwin ctl_config_write_done(io); 3141374f12c5SJohn Baldwin break; 3142374f12c5SJohn Baldwin } 3143374f12c5SJohn Baldwin return (CTL_RETVAL_COMPLETE); 3144374f12c5SJohn Baldwin } 3145374f12c5SJohn Baldwin 3146374f12c5SJohn Baldwin static int 3147374f12c5SJohn Baldwin ctl_be_block_config_write(union ctl_io *io) 3148374f12c5SJohn Baldwin { 3149374f12c5SJohn Baldwin switch (io->io_hdr.io_type) { 3150374f12c5SJohn Baldwin case CTL_IO_SCSI: 3151374f12c5SJohn Baldwin return (ctl_be_block_scsi_config_write(io)); 3152374f12c5SJohn Baldwin case CTL_IO_NVME: 3153374f12c5SJohn Baldwin return (ctl_be_block_nvme_config_write(io)); 3154374f12c5SJohn Baldwin default: 3155374f12c5SJohn Baldwin __assert_unreachable(); 3156374f12c5SJohn Baldwin } 3157374f12c5SJohn Baldwin } 3158374f12c5SJohn Baldwin 3159374f12c5SJohn Baldwin static int 3160374f12c5SJohn Baldwin ctl_be_block_scsi_config_read(union ctl_io *io) 3161130f4520SKenneth D. Merry { 3162ef8daf3fSAlexander Motin struct ctl_be_block_lun *be_lun; 3163ef8daf3fSAlexander Motin int retval = 0; 3164ef8daf3fSAlexander Motin 3165ef8daf3fSAlexander Motin DPRINTF("entered\n"); 3166ef8daf3fSAlexander Motin 3167767300e8SAlexander Motin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 3168ef8daf3fSAlexander Motin 3169ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 3170ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: 3171ef8daf3fSAlexander Motin if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 3172ef8daf3fSAlexander Motin mtx_lock(&be_lun->queue_lock); 3173ef8daf3fSAlexander Motin STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 3174ef8daf3fSAlexander Motin &io->io_hdr, links); 3175ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 3176ef8daf3fSAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, 3177ef8daf3fSAlexander Motin &be_lun->io_task); 3178ef8daf3fSAlexander Motin retval = CTL_RETVAL_QUEUED; 3179ef8daf3fSAlexander Motin break; 3180ef8daf3fSAlexander Motin } 3181ef8daf3fSAlexander Motin ctl_set_invalid_field(&io->scsiio, 3182ef8daf3fSAlexander Motin /*sks_valid*/ 1, 3183ef8daf3fSAlexander Motin /*command*/ 1, 3184ef8daf3fSAlexander Motin /*field*/ 1, 3185ef8daf3fSAlexander Motin /*bit_valid*/ 1, 3186ef8daf3fSAlexander Motin /*bit*/ 4); 3187ef8daf3fSAlexander Motin ctl_config_read_done(io); 3188ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 3189ef8daf3fSAlexander Motin break; 3190ef8daf3fSAlexander Motin default: 3191ef8daf3fSAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 3192ef8daf3fSAlexander Motin ctl_config_read_done(io); 3193ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 3194ef8daf3fSAlexander Motin break; 3195ef8daf3fSAlexander Motin } 3196ef8daf3fSAlexander Motin 3197ef8daf3fSAlexander Motin return (retval); 3198130f4520SKenneth D. Merry } 3199130f4520SKenneth D. Merry 3200130f4520SKenneth D. Merry static int 3201374f12c5SJohn Baldwin ctl_be_block_nvme_config_read(union ctl_io *io) 3202374f12c5SJohn Baldwin { 3203374f12c5SJohn Baldwin struct ctl_be_block_lun *be_lun; 3204374f12c5SJohn Baldwin 3205374f12c5SJohn Baldwin DPRINTF("entered\n"); 3206374f12c5SJohn Baldwin 3207374f12c5SJohn Baldwin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 3208374f12c5SJohn Baldwin 3209374f12c5SJohn Baldwin switch (io->nvmeio.cmd.opc) { 3210374f12c5SJohn Baldwin case NVME_OPC_IDENTIFY: 3211374f12c5SJohn Baldwin { 3212374f12c5SJohn Baldwin uint8_t cns; 3213374f12c5SJohn Baldwin 3214374f12c5SJohn Baldwin cns = le32toh(io->nvmeio.cmd.cdw10) & 0xff; 3215374f12c5SJohn Baldwin switch (cns) { 3216374f12c5SJohn Baldwin case 0: 3217374f12c5SJohn Baldwin case 3: 3218374f12c5SJohn Baldwin mtx_lock(&be_lun->queue_lock); 3219374f12c5SJohn Baldwin STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 3220374f12c5SJohn Baldwin &io->io_hdr, links); 3221374f12c5SJohn Baldwin mtx_unlock(&be_lun->queue_lock); 3222374f12c5SJohn Baldwin taskqueue_enqueue(be_lun->io_taskqueue, 3223374f12c5SJohn Baldwin &be_lun->io_task); 3224374f12c5SJohn Baldwin return (CTL_RETVAL_QUEUED); 3225374f12c5SJohn Baldwin default: 3226374f12c5SJohn Baldwin ctl_nvme_set_invalid_field(&io->nvmeio); 3227374f12c5SJohn Baldwin ctl_config_read_done(io); 3228374f12c5SJohn Baldwin break; 3229374f12c5SJohn Baldwin } 3230374f12c5SJohn Baldwin break; 3231374f12c5SJohn Baldwin } 3232374f12c5SJohn Baldwin default: 3233374f12c5SJohn Baldwin ctl_nvme_set_invalid_opcode(&io->nvmeio); 3234374f12c5SJohn Baldwin ctl_config_read_done(io); 3235374f12c5SJohn Baldwin break; 3236374f12c5SJohn Baldwin } 3237374f12c5SJohn Baldwin return (CTL_RETVAL_COMPLETE); 3238374f12c5SJohn Baldwin } 3239374f12c5SJohn Baldwin 3240374f12c5SJohn Baldwin static int 3241374f12c5SJohn Baldwin ctl_be_block_config_read(union ctl_io *io) 3242374f12c5SJohn Baldwin { 3243374f12c5SJohn Baldwin switch (io->io_hdr.io_type) { 3244374f12c5SJohn Baldwin case CTL_IO_SCSI: 3245374f12c5SJohn Baldwin return (ctl_be_block_scsi_config_read(io)); 3246374f12c5SJohn Baldwin case CTL_IO_NVME_ADMIN: 3247374f12c5SJohn Baldwin return (ctl_be_block_nvme_config_read(io)); 3248374f12c5SJohn Baldwin default: 3249374f12c5SJohn Baldwin __assert_unreachable(); 3250374f12c5SJohn Baldwin } 3251374f12c5SJohn Baldwin } 3252374f12c5SJohn Baldwin 3253374f12c5SJohn Baldwin static int 3254767300e8SAlexander Motin ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb) 3255130f4520SKenneth D. Merry { 3256767300e8SAlexander Motin struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun; 3257130f4520SKenneth D. Merry int retval; 3258130f4520SKenneth D. Merry 3259519b24f0SAlexander Motin retval = sbuf_cat(sb, "\t<num_threads>"); 3260130f4520SKenneth D. Merry if (retval != 0) 3261130f4520SKenneth D. Merry goto bailout; 3262130f4520SKenneth D. Merry retval = sbuf_printf(sb, "%d", lun->num_threads); 3263130f4520SKenneth D. Merry if (retval != 0) 3264130f4520SKenneth D. Merry goto bailout; 3265519b24f0SAlexander Motin retval = sbuf_cat(sb, "</num_threads>\n"); 3266130f4520SKenneth D. Merry 3267130f4520SKenneth D. Merry bailout: 3268130f4520SKenneth D. Merry return (retval); 3269130f4520SKenneth D. Merry } 3270130f4520SKenneth D. Merry 3271c3e7ba3eSAlexander Motin static uint64_t 3272767300e8SAlexander Motin ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname) 3273c3e7ba3eSAlexander Motin { 3274767300e8SAlexander Motin struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun; 3275c3e7ba3eSAlexander Motin 3276c3e7ba3eSAlexander Motin if (lun->getattr == NULL) 3277c3e7ba3eSAlexander Motin return (UINT64_MAX); 3278c3e7ba3eSAlexander Motin return (lun->getattr(lun, attrname)); 3279c3e7ba3eSAlexander Motin } 3280c3e7ba3eSAlexander Motin 32810c629e28SAlexander Motin static int 3282130f4520SKenneth D. Merry ctl_be_block_init(void) 3283130f4520SKenneth D. Merry { 32840c629e28SAlexander Motin struct ctl_be_block_softc *softc = &backend_block_softc; 3285130f4520SKenneth D. Merry 328634144c2cSAlexander Motin sx_init(&softc->modify_lock, "ctlblock modify"); 328775c7a1d3SAlexander Motin mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 32880c629e28SAlexander Motin softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 3289a0e36aeeSEdward Tomasz Napierala NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 3290cd853791SKonstantin Belousov softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG, 32910d7fed74SAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 3292cd853791SKonstantin Belousov if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG) 3293cd853791SKonstantin Belousov softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG, 32948054320eSAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 329534144c2cSAlexander Motin SLIST_INIT(&softc->lun_list); 32960c629e28SAlexander Motin return (0); 32970c629e28SAlexander Motin } 3298130f4520SKenneth D. Merry 32990c629e28SAlexander Motin static int 33000c629e28SAlexander Motin ctl_be_block_shutdown(void) 33010c629e28SAlexander Motin { 33020c629e28SAlexander Motin struct ctl_be_block_softc *softc = &backend_block_softc; 330334144c2cSAlexander Motin struct ctl_be_block_lun *lun; 33040c629e28SAlexander Motin 33050c629e28SAlexander Motin mtx_lock(&softc->lock); 330634144c2cSAlexander Motin while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) { 330734144c2cSAlexander Motin SLIST_REMOVE_HEAD(&softc->lun_list, links); 330834144c2cSAlexander Motin softc->num_luns--; 33090c629e28SAlexander Motin /* 331034144c2cSAlexander Motin * Drop our lock here. Since ctl_remove_lun() can call 33110c629e28SAlexander Motin * back into us, this could potentially lead to a recursive 33120c629e28SAlexander Motin * lock of the same mutex, which would cause a hang. 33130c629e28SAlexander Motin */ 33140c629e28SAlexander Motin mtx_unlock(&softc->lock); 331534144c2cSAlexander Motin ctl_remove_lun(&lun->cbe_lun); 33160c629e28SAlexander Motin mtx_lock(&softc->lock); 33170c629e28SAlexander Motin } 33180c629e28SAlexander Motin mtx_unlock(&softc->lock); 3319cd853791SKonstantin Belousov uma_zdestroy(softc->bufmin_zone); 3320cd853791SKonstantin Belousov if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG) 3321cd853791SKonstantin Belousov uma_zdestroy(softc->bufmax_zone); 33220c629e28SAlexander Motin uma_zdestroy(softc->beio_zone); 33230c629e28SAlexander Motin mtx_destroy(&softc->lock); 332434144c2cSAlexander Motin sx_destroy(&softc->modify_lock); 33250c629e28SAlexander Motin return (0); 3326130f4520SKenneth D. Merry } 3327