1130f4520SKenneth D. Merry /*- 2bec9534dSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3bec9534dSPedro F. Giffuni * 4130f4520SKenneth D. Merry * Copyright (c) 2003 Silicon Graphics International Corp. 5130f4520SKenneth D. Merry * Copyright (c) 2009-2011 Spectra Logic Corporation 681177295SEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 7648dfc1aSAlexander Motin * Copyright (c) 2014-2015 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 * 13130f4520SKenneth D. Merry * Redistribution and use in source and binary forms, with or without 14130f4520SKenneth D. Merry * modification, are permitted provided that the following conditions 15130f4520SKenneth D. Merry * are met: 16130f4520SKenneth D. Merry * 1. Redistributions of source code must retain the above copyright 17130f4520SKenneth D. Merry * notice, this list of conditions, and the following disclaimer, 18130f4520SKenneth D. Merry * without modification. 19130f4520SKenneth D. Merry * 2. Redistributions in binary form must reproduce at minimum a disclaimer 20130f4520SKenneth D. Merry * substantially similar to the "NO WARRANTY" disclaimer below 21130f4520SKenneth D. Merry * ("Disclaimer") and any redistribution must be conditioned upon 22130f4520SKenneth D. Merry * including a substantially similar Disclaimer requirement for further 23130f4520SKenneth D. Merry * binary redistribution. 24130f4520SKenneth D. Merry * 25130f4520SKenneth D. Merry * NO WARRANTY 26130f4520SKenneth D. Merry * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27130f4520SKenneth D. Merry * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28130f4520SKenneth D. Merry * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 29130f4520SKenneth D. Merry * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30130f4520SKenneth D. Merry * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31130f4520SKenneth D. Merry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32130f4520SKenneth D. Merry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33130f4520SKenneth D. Merry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 34130f4520SKenneth D. Merry * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 35130f4520SKenneth D. Merry * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36130f4520SKenneth D. Merry * POSSIBILITY OF SUCH DAMAGES. 37130f4520SKenneth D. Merry * 38130f4520SKenneth D. Merry * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 39130f4520SKenneth D. Merry */ 40130f4520SKenneth D. Merry /* 41130f4520SKenneth D. Merry * CAM Target Layer driver backend for block devices. 42130f4520SKenneth D. Merry * 43130f4520SKenneth D. Merry * Author: Ken Merry <ken@FreeBSD.org> 44130f4520SKenneth D. Merry */ 45130f4520SKenneth D. Merry #include <sys/cdefs.h> 46130f4520SKenneth D. Merry __FBSDID("$FreeBSD$"); 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> 84130f4520SKenneth D. Merry 85130f4520SKenneth D. Merry #include <geom/geom.h> 86130f4520SKenneth D. Merry 87130f4520SKenneth D. Merry #include <cam/cam.h> 88130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h> 89130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h> 90130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h> 91130f4520SKenneth D. Merry #include <cam/ctl/ctl.h> 92130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h> 93130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h> 947ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h> 95130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h> 967ac58230SAlexander Motin #include <cam/ctl/ctl_private.h> 97130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h> 98130f4520SKenneth D. Merry 99130f4520SKenneth D. Merry /* 10008a7cce5SAlexander Motin * The idea here is that we'll allocate enough S/G space to hold a 1MB 10108a7cce5SAlexander Motin * I/O. If we get an I/O larger than that, we'll split it. 102130f4520SKenneth D. Merry */ 10311b569f7SAlexander Motin #define CTLBLK_HALF_IO_SIZE (512 * 1024) 10411b569f7SAlexander Motin #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 105cd853791SKonstantin Belousov #define CTLBLK_MIN_SEG (128 * 1024) 106cd853791SKonstantin Belousov #define CTLBLK_MAX_SEG MIN(CTLBLK_HALF_IO_SIZE, maxphys) 107cd853791SKonstantin Belousov #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MIN_SEG, 1) 10811b569f7SAlexander Motin #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 109cd853791SKonstantin Belousov #define CTLBLK_NUM_SEGS (CTLBLK_MAX_IO_SIZE / CTLBLK_MAX_SEG) 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]) 122e86a4142SAlexander Motin 123130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb); 124130f4520SKenneth D. Merry 125130f4520SKenneth D. Merry typedef enum { 126130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 127130f4520SKenneth D. Merry CTL_BE_BLOCK_LUN_WAITING = 0x04, 128130f4520SKenneth D. Merry } ctl_be_block_lun_flags; 129130f4520SKenneth D. Merry 130130f4520SKenneth D. Merry typedef enum { 131130f4520SKenneth D. Merry CTL_BE_BLOCK_NONE, 132130f4520SKenneth D. Merry CTL_BE_BLOCK_DEV, 133130f4520SKenneth D. Merry CTL_BE_BLOCK_FILE 134130f4520SKenneth D. Merry } ctl_be_block_type; 135130f4520SKenneth D. Merry 136130f4520SKenneth D. Merry struct ctl_be_block_filedata { 137130f4520SKenneth D. Merry struct ucred *cred; 138130f4520SKenneth D. Merry }; 139130f4520SKenneth D. Merry 140130f4520SKenneth D. Merry union ctl_be_block_bedata { 141130f4520SKenneth D. Merry struct ctl_be_block_filedata file; 142130f4520SKenneth D. Merry }; 143130f4520SKenneth D. Merry 144130f4520SKenneth D. Merry struct ctl_be_block_io; 145130f4520SKenneth D. Merry struct ctl_be_block_lun; 146130f4520SKenneth D. Merry 147130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 148130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 149c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 150c3e7ba3eSAlexander Motin const char *attrname); 151130f4520SKenneth D. Merry 152130f4520SKenneth D. Merry /* 153130f4520SKenneth D. Merry * Backend LUN structure. There is a 1:1 mapping between a block device 154130f4520SKenneth D. Merry * and a backend block LUN, and between a backend block LUN and a CTL LUN. 155130f4520SKenneth D. Merry */ 156130f4520SKenneth D. Merry struct ctl_be_block_lun { 157767300e8SAlexander Motin struct ctl_be_lun cbe_lun; /* Must be first element. */ 15819720f41SAlexander Motin struct ctl_lun_create_params params; 159130f4520SKenneth D. Merry char *dev_path; 160130f4520SKenneth D. Merry ctl_be_block_type dev_type; 161130f4520SKenneth D. Merry struct vnode *vn; 162130f4520SKenneth D. Merry union ctl_be_block_bedata backend; 163130f4520SKenneth D. Merry cbb_dispatch_t dispatch; 164130f4520SKenneth D. Merry cbb_dispatch_t lun_flush; 165ee7f31c0SAlexander Motin cbb_dispatch_t unmap; 166ef8daf3fSAlexander Motin cbb_dispatch_t get_lba_status; 167c3e7ba3eSAlexander Motin cbb_getattr_t getattr; 168130f4520SKenneth D. Merry uint64_t size_blocks; 169130f4520SKenneth D. Merry uint64_t size_bytes; 170130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 171130f4520SKenneth D. Merry struct devstat *disk_stats; 172130f4520SKenneth D. Merry ctl_be_block_lun_flags flags; 17334144c2cSAlexander Motin SLIST_ENTRY(ctl_be_block_lun) links; 174130f4520SKenneth D. Merry struct taskqueue *io_taskqueue; 175130f4520SKenneth D. Merry struct task io_task; 176130f4520SKenneth D. Merry int num_threads; 177130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) input_queue; 178ef8daf3fSAlexander Motin STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 179130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 180130f4520SKenneth D. Merry STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 18175c7a1d3SAlexander Motin struct mtx_padalign io_lock; 18275c7a1d3SAlexander Motin struct mtx_padalign queue_lock; 183130f4520SKenneth D. Merry }; 184130f4520SKenneth D. Merry 185130f4520SKenneth D. Merry /* 186130f4520SKenneth D. Merry * Overall softc structure for the block backend module. 187130f4520SKenneth D. Merry */ 188130f4520SKenneth D. Merry struct ctl_be_block_softc { 18934144c2cSAlexander Motin struct sx modify_lock; 190130f4520SKenneth D. Merry struct mtx lock; 191130f4520SKenneth D. Merry int num_luns; 19234144c2cSAlexander Motin SLIST_HEAD(, ctl_be_block_lun) lun_list; 1930d7fed74SAlexander Motin uma_zone_t beio_zone; 194cd853791SKonstantin Belousov uma_zone_t bufmin_zone; 195cd853791SKonstantin Belousov uma_zone_t bufmax_zone; 196130f4520SKenneth D. Merry }; 197130f4520SKenneth D. Merry 198130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc; 199130f4520SKenneth D. Merry 200130f4520SKenneth D. Merry /* 201130f4520SKenneth D. Merry * Per-I/O information. 202130f4520SKenneth D. Merry */ 203130f4520SKenneth D. Merry struct ctl_be_block_io { 204130f4520SKenneth D. Merry union ctl_io *io; 205130f4520SKenneth D. Merry struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 206130f4520SKenneth D. Merry struct iovec xiovecs[CTLBLK_MAX_SEGS]; 2079a4510acSAlexander Motin int refcnt; 208130f4520SKenneth D. Merry int bio_cmd; 2090d7fed74SAlexander Motin int two_sglists; 210130f4520SKenneth D. Merry int num_segs; 211130f4520SKenneth D. Merry int num_bios_sent; 212130f4520SKenneth D. Merry int num_bios_done; 213130f4520SKenneth D. Merry int send_complete; 2141f0694a6SAlexander Motin int first_error; 2151f0694a6SAlexander Motin uint64_t first_error_offset; 216130f4520SKenneth D. Merry struct bintime ds_t0; 217130f4520SKenneth D. Merry devstat_tag_type ds_tag_type; 218130f4520SKenneth D. Merry devstat_trans_flags ds_trans_type; 219130f4520SKenneth D. Merry uint64_t io_len; 220130f4520SKenneth D. Merry uint64_t io_offset; 2217d0d4342SAlexander Motin int io_arg; 222130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 223130f4520SKenneth D. Merry struct ctl_be_block_lun *lun; 224ee7f31c0SAlexander Motin void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 225130f4520SKenneth D. Merry }; 226130f4520SKenneth D. Merry 2277ac58230SAlexander Motin extern struct ctl_softc *control_softc; 2287ac58230SAlexander Motin 2297d4c4443SAlexander Motin static int cbb_num_threads = 32; 2307029da5cSPawel Biernacki SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 231130f4520SKenneth D. Merry "CAM Target Layer Block Backend"); 232af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 233130f4520SKenneth D. Merry &cbb_num_threads, 0, "Number of threads per backing file"); 234130f4520SKenneth D. Merry 235130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 236130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio); 237130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio); 2382c7dc6baSAlexander Motin static int ctl_be_block_move_done(union ctl_io *io, bool samethr); 239130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio); 240130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 241130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 242130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 243130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 244ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 245ef8daf3fSAlexander Motin struct ctl_be_block_io *beio); 24653c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 24753c146deSAlexander Motin const char *attrname); 248130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 249130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 250ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 251ee7f31c0SAlexander Motin struct ctl_be_block_io *beio); 252130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 253130f4520SKenneth D. Merry struct ctl_be_block_io *beio); 254c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 255c3e7ba3eSAlexander Motin const char *attrname); 256ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 257ef8daf3fSAlexander Motin union ctl_io *io); 258130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 259130f4520SKenneth D. Merry union ctl_io *io); 260130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 261130f4520SKenneth D. Merry union ctl_io *io); 262130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending); 263130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io); 264130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 265130f4520SKenneth D. Merry int flag, struct thread *td); 266130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 267130f4520SKenneth D. Merry struct ctl_lun_req *req); 268130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 269130f4520SKenneth D. Merry struct ctl_lun_req *req); 270130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 271648dfc1aSAlexander Motin static int ctl_be_block_open(struct ctl_be_block_lun *be_lun, 272130f4520SKenneth D. Merry struct ctl_lun_req *req); 273130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc, 274130f4520SKenneth D. Merry struct ctl_lun_req *req); 275130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 276130f4520SKenneth D. Merry struct ctl_lun_req *req); 27781177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 27881177295SEdward Tomasz Napierala struct ctl_lun_req *req); 279767300e8SAlexander Motin static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun); 280130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io); 281130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io); 282767300e8SAlexander Motin static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb); 283767300e8SAlexander Motin static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname); 2840c629e28SAlexander Motin static int ctl_be_block_init(void); 2850c629e28SAlexander Motin static int ctl_be_block_shutdown(void); 286130f4520SKenneth D. Merry 287130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver = 288130f4520SKenneth D. Merry { 2892a2443d8SKenneth D. Merry .name = "block", 2902a2443d8SKenneth D. Merry .flags = CTL_BE_FLAG_HAS_CONFIG, 2912a2443d8SKenneth D. Merry .init = ctl_be_block_init, 2920c629e28SAlexander Motin .shutdown = ctl_be_block_shutdown, 2932a2443d8SKenneth D. Merry .data_submit = ctl_be_block_submit, 2942a2443d8SKenneth D. Merry .config_read = ctl_be_block_config_read, 2952a2443d8SKenneth D. Merry .config_write = ctl_be_block_config_write, 2962a2443d8SKenneth D. Merry .ioctl = ctl_be_block_ioctl, 297c3e7ba3eSAlexander Motin .lun_info = ctl_be_block_lun_info, 298c3e7ba3eSAlexander Motin .lun_attr = ctl_be_block_lun_attr 299130f4520SKenneth D. Merry }; 300130f4520SKenneth D. Merry 30134144c2cSAlexander Motin MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend"); 302130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 303130f4520SKenneth D. Merry 3048054320eSAlexander Motin static void 3058054320eSAlexander Motin ctl_alloc_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg, 3068054320eSAlexander Motin size_t len) 3078054320eSAlexander Motin { 3088054320eSAlexander Motin 309cd853791SKonstantin Belousov if (len <= CTLBLK_MIN_SEG) { 310cd853791SKonstantin Belousov sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK); 311cd853791SKonstantin Belousov } else { 312cd853791SKonstantin Belousov KASSERT(len <= CTLBLK_MAX_SEG, 313cd853791SKonstantin Belousov ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG)); 314cd853791SKonstantin Belousov sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK); 315cd853791SKonstantin Belousov } 3168054320eSAlexander Motin sg->len = len; 3178054320eSAlexander Motin } 3188054320eSAlexander Motin 3198054320eSAlexander Motin static void 3208054320eSAlexander Motin ctl_free_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg) 3218054320eSAlexander Motin { 3228054320eSAlexander Motin 323cd853791SKonstantin Belousov if (sg->len <= CTLBLK_MIN_SEG) { 324cd853791SKonstantin Belousov uma_zfree(softc->bufmin_zone, sg->addr); 325cd853791SKonstantin Belousov } else { 326cd853791SKonstantin Belousov KASSERT(sg->len <= CTLBLK_MAX_SEG, 327cd853791SKonstantin Belousov ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG)); 328cd853791SKonstantin Belousov uma_zfree(softc->bufmax_zone, sg->addr); 329cd853791SKonstantin Belousov } 3308054320eSAlexander Motin } 3318054320eSAlexander Motin 332130f4520SKenneth D. Merry static struct ctl_be_block_io * 333130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc) 334130f4520SKenneth D. Merry { 335130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 336130f4520SKenneth D. Merry 3370c629e28SAlexander Motin beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO); 338130f4520SKenneth D. Merry beio->softc = softc; 3399a4510acSAlexander Motin beio->refcnt = 1; 340130f4520SKenneth D. Merry return (beio); 341130f4520SKenneth D. Merry } 342130f4520SKenneth D. Merry 343130f4520SKenneth D. Merry static void 3449a4510acSAlexander Motin ctl_real_free_beio(struct ctl_be_block_io *beio) 345130f4520SKenneth D. Merry { 3460d7fed74SAlexander Motin struct ctl_be_block_softc *softc = beio->softc; 347130f4520SKenneth D. Merry int i; 348130f4520SKenneth D. Merry 349130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 3508054320eSAlexander Motin ctl_free_seg(softc, &beio->sg_segs[i]); 35111b569f7SAlexander Motin 35211b569f7SAlexander Motin /* For compare we had two equal S/G lists. */ 3530d7fed74SAlexander Motin if (beio->two_sglists) { 3548054320eSAlexander Motin ctl_free_seg(softc, 3558054320eSAlexander Motin &beio->sg_segs[i + CTLBLK_HALF_SEGS]); 35611b569f7SAlexander Motin } 357130f4520SKenneth D. Merry } 358130f4520SKenneth D. Merry 3590d7fed74SAlexander Motin uma_zfree(softc->beio_zone, beio); 360130f4520SKenneth D. Merry } 361130f4520SKenneth D. Merry 362130f4520SKenneth D. Merry static void 3639a4510acSAlexander Motin ctl_refcnt_beio(void *arg, int diff) 3649a4510acSAlexander Motin { 3659a4510acSAlexander Motin struct ctl_be_block_io *beio = arg; 3669a4510acSAlexander Motin 3679a4510acSAlexander Motin if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0) 3689a4510acSAlexander Motin ctl_real_free_beio(beio); 3699a4510acSAlexander Motin } 3709a4510acSAlexander Motin 3719a4510acSAlexander Motin static void 3729a4510acSAlexander Motin ctl_free_beio(struct ctl_be_block_io *beio) 3739a4510acSAlexander Motin { 3749a4510acSAlexander Motin 3759a4510acSAlexander Motin ctl_refcnt_beio(beio, -1); 3769a4510acSAlexander Motin } 3779a4510acSAlexander Motin 3789a4510acSAlexander Motin static void 379130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio) 380130f4520SKenneth D. Merry { 38175c7a1d3SAlexander Motin union ctl_io *io = beio->io; 382130f4520SKenneth D. Merry 383ee7f31c0SAlexander Motin if (beio->beio_cont != NULL) { 384ee7f31c0SAlexander Motin beio->beio_cont(beio); 385ee7f31c0SAlexander Motin } else { 386130f4520SKenneth D. Merry ctl_free_beio(beio); 38711b569f7SAlexander Motin ctl_data_submit_done(io); 388130f4520SKenneth D. Merry } 389ee7f31c0SAlexander Motin } 390130f4520SKenneth D. Merry 391d6043e46SAlexander Motin static size_t 392d6043e46SAlexander Motin cmp(uint8_t *a, uint8_t *b, size_t size) 393d6043e46SAlexander Motin { 394d6043e46SAlexander Motin size_t i; 395d6043e46SAlexander Motin 396d6043e46SAlexander Motin for (i = 0; i < size; i++) { 397d6043e46SAlexander Motin if (a[i] != b[i]) 398d6043e46SAlexander Motin break; 399d6043e46SAlexander Motin } 400d6043e46SAlexander Motin return (i); 401d6043e46SAlexander Motin } 402d6043e46SAlexander Motin 403d6043e46SAlexander Motin static void 404d6043e46SAlexander Motin ctl_be_block_compare(union ctl_io *io) 405d6043e46SAlexander Motin { 406d6043e46SAlexander Motin struct ctl_be_block_io *beio; 407d6043e46SAlexander Motin uint64_t off, res; 408d6043e46SAlexander Motin int i; 409d6043e46SAlexander Motin uint8_t info[8]; 410d6043e46SAlexander Motin 411d6043e46SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 412d6043e46SAlexander Motin off = 0; 413d6043e46SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 414d6043e46SAlexander Motin res = cmp(beio->sg_segs[i].addr, 415d6043e46SAlexander Motin beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 416d6043e46SAlexander Motin beio->sg_segs[i].len); 417d6043e46SAlexander Motin off += res; 418d6043e46SAlexander Motin if (res < beio->sg_segs[i].len) 419d6043e46SAlexander Motin break; 420d6043e46SAlexander Motin } 421d6043e46SAlexander Motin if (i < beio->num_segs) { 422d6043e46SAlexander Motin scsi_u64to8b(off, info); 423d6043e46SAlexander Motin ctl_set_sense(&io->scsiio, /*current_error*/ 1, 424d6043e46SAlexander Motin /*sense_key*/ SSD_KEY_MISCOMPARE, 425d6043e46SAlexander Motin /*asc*/ 0x1D, /*ascq*/ 0x00, 426d6043e46SAlexander Motin /*type*/ SSD_ELEM_INFO, 427d6043e46SAlexander Motin /*size*/ sizeof(info), /*data*/ &info, 428d6043e46SAlexander Motin /*type*/ SSD_ELEM_NONE); 429d6043e46SAlexander Motin } else 430d6043e46SAlexander Motin ctl_set_success(&io->scsiio); 431d6043e46SAlexander Motin } 432d6043e46SAlexander Motin 433130f4520SKenneth D. Merry static int 4342c7dc6baSAlexander Motin ctl_be_block_move_done(union ctl_io *io, bool samethr) 435130f4520SKenneth D. Merry { 436130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 437130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 43811b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 439130f4520SKenneth D. Merry 440e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 441130f4520SKenneth D. Merry 442130f4520SKenneth D. Merry DPRINTF("entered\n"); 44311b569f7SAlexander Motin io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 444130f4520SKenneth D. Merry 445130f4520SKenneth D. Merry /* 4462c7dc6baSAlexander Motin * We set status at this point for read and compare commands. 447130f4520SKenneth D. Merry */ 4482c7dc6baSAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4492c7dc6baSAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) { 450*a59e2982SAlexander Motin lbalen = ARGS(io); 45111b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_READ) { 452130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 45311b569f7SAlexander Motin } else if (lbalen->flags & CTL_LLF_COMPARE) { 45411b569f7SAlexander Motin /* We have two data blocks ready for comparison. */ 455d6043e46SAlexander Motin ctl_be_block_compare(io); 45611b569f7SAlexander Motin } 457130f4520SKenneth D. Merry } 458130f4520SKenneth D. Merry 459130f4520SKenneth D. Merry /* 460130f4520SKenneth D. Merry * If this is a read, or a write with errors, it is done. 461130f4520SKenneth D. Merry */ 462130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_READ) 463130f4520SKenneth D. Merry || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 464130f4520SKenneth D. Merry || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 465130f4520SKenneth D. Merry ctl_complete_beio(beio); 466130f4520SKenneth D. Merry return (0); 467130f4520SKenneth D. Merry } 468130f4520SKenneth D. Merry 469130f4520SKenneth D. Merry /* 4702c7dc6baSAlexander Motin * At this point, we have a write and the DMA completed successfully. 4712c7dc6baSAlexander Motin * If we were called synchronously in the original thread then just 4722c7dc6baSAlexander Motin * dispatch, otherwise we now have to queue it to the task queue to 473130f4520SKenneth D. Merry * execute the backend I/O. That is because we do blocking 474130f4520SKenneth D. Merry * memory allocations, and in the file backing case, blocking I/O. 475130f4520SKenneth D. Merry * This move done routine is generally called in the SIM's 476130f4520SKenneth D. Merry * interrupt context, and therefore we cannot block. 477130f4520SKenneth D. Merry */ 478*a59e2982SAlexander Motin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 4792c7dc6baSAlexander Motin if (samethr) { 4802c7dc6baSAlexander Motin be_lun->dispatch(be_lun, beio); 4812c7dc6baSAlexander Motin } else { 48275c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 483130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 48475c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 485130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 4862c7dc6baSAlexander Motin } 487130f4520SKenneth D. Merry return (0); 488130f4520SKenneth D. Merry } 489130f4520SKenneth D. Merry 490130f4520SKenneth D. Merry static void 491130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio) 492130f4520SKenneth D. Merry { 493130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 494130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 495130f4520SKenneth D. Merry union ctl_io *io; 496e0c2f975SAlexander Motin int error; 497130f4520SKenneth D. Merry 498130f4520SKenneth D. Merry beio = bio->bio_caller1; 499130f4520SKenneth D. Merry be_lun = beio->lun; 500130f4520SKenneth D. Merry io = beio->io; 501130f4520SKenneth D. Merry 502130f4520SKenneth D. Merry DPRINTF("entered\n"); 503130f4520SKenneth D. Merry 504e0c2f975SAlexander Motin error = bio->bio_error; 50575c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 5061f0694a6SAlexander Motin if (error != 0 && 5071f0694a6SAlexander Motin (beio->first_error == 0 || 5081f0694a6SAlexander Motin bio->bio_offset < beio->first_error_offset)) { 5091f0694a6SAlexander Motin beio->first_error = error; 5101f0694a6SAlexander Motin beio->first_error_offset = bio->bio_offset; 5111f0694a6SAlexander Motin } 512130f4520SKenneth D. Merry 513130f4520SKenneth D. Merry beio->num_bios_done++; 514130f4520SKenneth D. Merry 515130f4520SKenneth D. Merry /* 516130f4520SKenneth D. Merry * XXX KDM will this cause WITNESS to complain? Holding a lock 517130f4520SKenneth D. Merry * during the free might cause it to complain. 518130f4520SKenneth D. Merry */ 519130f4520SKenneth D. Merry g_destroy_bio(bio); 520130f4520SKenneth D. Merry 521130f4520SKenneth D. Merry /* 522130f4520SKenneth D. Merry * If the send complete bit isn't set, or we aren't the last I/O to 523130f4520SKenneth D. Merry * complete, then we're done. 524130f4520SKenneth D. Merry */ 525130f4520SKenneth D. Merry if ((beio->send_complete == 0) 526130f4520SKenneth D. Merry || (beio->num_bios_done < beio->num_bios_sent)) { 52775c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 528130f4520SKenneth D. Merry return; 529130f4520SKenneth D. Merry } 530130f4520SKenneth D. Merry 531130f4520SKenneth D. Merry /* 532130f4520SKenneth D. Merry * At this point, we've verified that we are the last I/O to 533130f4520SKenneth D. Merry * complete, so it's safe to drop the lock. 534130f4520SKenneth D. Merry */ 53575c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 53675c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 53775c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 53875c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 539130f4520SKenneth D. Merry 540130f4520SKenneth D. Merry /* 541130f4520SKenneth D. Merry * If there are any errors from the backing device, we fail the 542130f4520SKenneth D. Merry * entire I/O with a medium error. 543130f4520SKenneth D. Merry */ 5441f0694a6SAlexander Motin error = beio->first_error; 5451f0694a6SAlexander Motin if (error != 0) { 546e0c2f975SAlexander Motin if (error == EOPNOTSUPP) { 547e0c2f975SAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 5480631de4aSAlexander Motin } else if (error == ENOSPC || error == EDQUOT) { 5494fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 5506187d472SAlexander Motin } else if (error == EROFS || error == EACCES) { 5516187d472SAlexander Motin ctl_set_hw_write_protected(&io->scsiio); 552e0c2f975SAlexander Motin } else if (beio->bio_cmd == BIO_FLUSH) { 553130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 554130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 555130f4520SKenneth D. Merry /*sks_valid*/ 1, 556130f4520SKenneth D. Merry /*retry_count*/ 0xbad2); 5577f7bb97aSAlexander Motin } else { 5587f7bb97aSAlexander Motin ctl_set_medium_error(&io->scsiio, 5597f7bb97aSAlexander Motin beio->bio_cmd == BIO_READ); 5607f7bb97aSAlexander Motin } 561130f4520SKenneth D. Merry ctl_complete_beio(beio); 562130f4520SKenneth D. Merry return; 563130f4520SKenneth D. Merry } 564130f4520SKenneth D. Merry 565130f4520SKenneth D. Merry /* 56611b569f7SAlexander Motin * If this is a write, a flush, a delete or verify, we're all done. 567130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 568130f4520SKenneth D. Merry */ 569130f4520SKenneth D. Merry if ((beio->bio_cmd == BIO_WRITE) 570ee7f31c0SAlexander Motin || (beio->bio_cmd == BIO_FLUSH) 57111b569f7SAlexander Motin || (beio->bio_cmd == BIO_DELETE) 57211b569f7SAlexander Motin || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 573130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 574130f4520SKenneth D. Merry ctl_complete_beio(beio); 575130f4520SKenneth D. Merry } else { 576f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 57775a3108eSAlexander Motin beio->beio_cont == NULL) { 578f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 57975a3108eSAlexander Motin ctl_serseq_done(io); 58075a3108eSAlexander Motin } 581130f4520SKenneth D. Merry ctl_datamove(io); 582130f4520SKenneth D. Merry } 583130f4520SKenneth D. Merry } 584130f4520SKenneth D. Merry 585130f4520SKenneth D. Merry static void 586130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 587130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 588130f4520SKenneth D. Merry { 58975c7a1d3SAlexander Motin union ctl_io *io = beio->io; 590130f4520SKenneth D. Merry struct mount *mountpoint; 5915050aa86SKonstantin Belousov int error, lock_flags; 592130f4520SKenneth D. Merry 593130f4520SKenneth D. Merry DPRINTF("entered\n"); 594130f4520SKenneth D. Merry 59575c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 59675c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 597130f4520SKenneth D. Merry 598130f4520SKenneth D. Merry (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 599130f4520SKenneth D. Merry 60067cc546dSAlexander Motin if (MNT_SHARED_WRITES(mountpoint) || 60167cc546dSAlexander Motin ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 602130f4520SKenneth D. Merry lock_flags = LK_SHARED; 603130f4520SKenneth D. Merry else 604130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 605130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 6067d0d4342SAlexander Motin error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 6077d0d4342SAlexander Motin curthread); 608b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 609130f4520SKenneth D. Merry 610130f4520SKenneth D. Merry vn_finished_write(mountpoint); 611130f4520SKenneth D. Merry 61275c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 61375c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 61475c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 61575c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 61675c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 61775c7a1d3SAlexander Motin 618130f4520SKenneth D. Merry if (error == 0) 619130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 620130f4520SKenneth D. Merry else { 621130f4520SKenneth D. Merry /* XXX KDM is there is a better error here? */ 622130f4520SKenneth D. Merry ctl_set_internal_failure(&io->scsiio, 623130f4520SKenneth D. Merry /*sks_valid*/ 1, 624130f4520SKenneth D. Merry /*retry_count*/ 0xbad1); 625130f4520SKenneth D. Merry } 626130f4520SKenneth D. Merry 627130f4520SKenneth D. Merry ctl_complete_beio(beio); 628130f4520SKenneth D. Merry } 629130f4520SKenneth D. Merry 63036160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t"); 63136160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t"); 63236160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t"); 63336160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t"); 634130f4520SKenneth D. Merry 635130f4520SKenneth D. Merry static void 636130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 637130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 638130f4520SKenneth D. Merry { 639130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 640130f4520SKenneth D. Merry union ctl_io *io; 641130f4520SKenneth D. Merry struct uio xuio; 642130f4520SKenneth D. Merry struct iovec *xiovec; 64383981e31SAlexander Motin size_t s; 64483981e31SAlexander Motin int error, flags, i; 645130f4520SKenneth D. Merry 646130f4520SKenneth D. Merry DPRINTF("entered\n"); 647130f4520SKenneth D. Merry 648130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 649130f4520SKenneth D. Merry io = beio->io; 65055551d05SAlexander Motin flags = 0; 65155551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 65255551d05SAlexander Motin flags |= IO_DIRECT; 65355551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 65455551d05SAlexander Motin flags |= IO_SYNC; 655130f4520SKenneth D. Merry 65611b569f7SAlexander Motin bzero(&xuio, sizeof(xuio)); 657130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 65836160958SMark Johnston SDT_PROBE0(cbb, , read, file_start); 65911b569f7SAlexander Motin xuio.uio_rw = UIO_READ; 660130f4520SKenneth D. Merry } else { 66136160958SMark Johnston SDT_PROBE0(cbb, , write, file_start); 662130f4520SKenneth D. Merry xuio.uio_rw = UIO_WRITE; 66311b569f7SAlexander Motin } 664130f4520SKenneth D. Merry xuio.uio_offset = beio->io_offset; 665130f4520SKenneth D. Merry xuio.uio_resid = beio->io_len; 666130f4520SKenneth D. Merry xuio.uio_segflg = UIO_SYSSPACE; 667130f4520SKenneth D. Merry xuio.uio_iov = beio->xiovecs; 668130f4520SKenneth D. Merry xuio.uio_iovcnt = beio->num_segs; 669130f4520SKenneth D. Merry xuio.uio_td = curthread; 670130f4520SKenneth D. Merry 671130f4520SKenneth D. Merry for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 672130f4520SKenneth D. Merry xiovec->iov_base = beio->sg_segs[i].addr; 673130f4520SKenneth D. Merry xiovec->iov_len = beio->sg_segs[i].len; 674130f4520SKenneth D. Merry } 675130f4520SKenneth D. Merry 67675c7a1d3SAlexander Motin binuptime(&beio->ds_t0); 67775c7a1d3SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 67875c7a1d3SAlexander Motin 679130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 680130f4520SKenneth D. Merry vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 681130f4520SKenneth D. Merry 682130f4520SKenneth D. Merry /* 683130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for reads. If the 684130f4520SKenneth D. Merry * DIRECTIO option is configured into the kernel, it calls 685130f4520SKenneth D. Merry * ffs_rawread(). But that only works for single-segment 686130f4520SKenneth D. Merry * uios with user space addresses. In our case, with a 687130f4520SKenneth D. Merry * kernel uio, it still reads into the buffer cache, but it 688130f4520SKenneth D. Merry * will just try to release the buffer from the cache later 689130f4520SKenneth D. Merry * on in ffs_read(). 690130f4520SKenneth D. Merry * 691130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for reads. 692130f4520SKenneth D. Merry * 693130f4520SKenneth D. Merry * UFS does not pay attention to IO_SYNC for reads. 694130f4520SKenneth D. Merry * 695130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (which translates into the 696130f4520SKenneth D. Merry * Solaris define FRSYNC for zfs_read()) for reads. It 697130f4520SKenneth D. Merry * attempts to sync the file before reading. 698130f4520SKenneth D. Merry */ 69955551d05SAlexander Motin error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 700130f4520SKenneth D. Merry 701b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 70236160958SMark Johnston SDT_PROBE0(cbb, , read, file_done); 70383981e31SAlexander Motin if (error == 0 && xuio.uio_resid > 0) { 70483981e31SAlexander Motin /* 70583981e31SAlexander Motin * If we red less then requested (EOF), then 70683981e31SAlexander Motin * we should clean the rest of the buffer. 70783981e31SAlexander Motin */ 70883981e31SAlexander Motin s = beio->io_len - xuio.uio_resid; 70983981e31SAlexander Motin for (i = 0; i < beio->num_segs; i++) { 71083981e31SAlexander Motin if (s >= beio->sg_segs[i].len) { 71183981e31SAlexander Motin s -= beio->sg_segs[i].len; 71283981e31SAlexander Motin continue; 71383981e31SAlexander Motin } 71483981e31SAlexander Motin bzero((uint8_t *)beio->sg_segs[i].addr + s, 71583981e31SAlexander Motin beio->sg_segs[i].len - s); 71683981e31SAlexander Motin s = 0; 71783981e31SAlexander Motin } 71883981e31SAlexander Motin } 719130f4520SKenneth D. Merry } else { 720130f4520SKenneth D. Merry struct mount *mountpoint; 721130f4520SKenneth D. Merry int lock_flags; 722130f4520SKenneth D. Merry 723130f4520SKenneth D. Merry (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 724130f4520SKenneth D. Merry 72567cc546dSAlexander Motin if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL) 726130f4520SKenneth D. Merry && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 727130f4520SKenneth D. Merry lock_flags = LK_SHARED; 728130f4520SKenneth D. Merry else 729130f4520SKenneth D. Merry lock_flags = LK_EXCLUSIVE; 730130f4520SKenneth D. Merry vn_lock(be_lun->vn, lock_flags | LK_RETRY); 731130f4520SKenneth D. Merry 732130f4520SKenneth D. Merry /* 733130f4520SKenneth D. Merry * UFS pays attention to IO_DIRECT for writes. The write 734130f4520SKenneth D. Merry * is done asynchronously. (Normally the write would just 735130f4520SKenneth D. Merry * get put into cache. 736130f4520SKenneth D. Merry * 737130f4520SKenneth D. Merry * UFS pays attention to IO_SYNC for writes. It will 738130f4520SKenneth D. Merry * attempt to write the buffer out synchronously if that 739130f4520SKenneth D. Merry * flag is set. 740130f4520SKenneth D. Merry * 741130f4520SKenneth D. Merry * ZFS does not pay attention to IO_DIRECT for writes. 742130f4520SKenneth D. Merry * 743130f4520SKenneth D. Merry * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 744130f4520SKenneth D. Merry * for writes. It will flush the transaction from the 745130f4520SKenneth D. Merry * cache before returning. 746130f4520SKenneth D. Merry */ 74755551d05SAlexander Motin error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 748b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 749130f4520SKenneth D. Merry 750130f4520SKenneth D. Merry vn_finished_write(mountpoint); 75136160958SMark Johnston SDT_PROBE0(cbb, , write, file_done); 752130f4520SKenneth D. Merry } 753130f4520SKenneth D. Merry 75475c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 75575c7a1d3SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 75675c7a1d3SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 75775c7a1d3SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 75875c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 75975c7a1d3SAlexander Motin 760130f4520SKenneth D. Merry /* 761130f4520SKenneth D. Merry * If we got an error, set the sense data to "MEDIUM ERROR" and 762130f4520SKenneth D. Merry * return the I/O to the user. 763130f4520SKenneth D. Merry */ 764130f4520SKenneth D. Merry if (error != 0) { 7650631de4aSAlexander Motin if (error == ENOSPC || error == EDQUOT) { 7664fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 7676187d472SAlexander Motin } else if (error == EROFS || error == EACCES) { 7686187d472SAlexander Motin ctl_set_hw_write_protected(&io->scsiio); 7697f7bb97aSAlexander Motin } else { 7707f7bb97aSAlexander Motin ctl_set_medium_error(&io->scsiio, 7717f7bb97aSAlexander Motin beio->bio_cmd == BIO_READ); 7727f7bb97aSAlexander Motin } 773130f4520SKenneth D. Merry ctl_complete_beio(beio); 774130f4520SKenneth D. Merry return; 775130f4520SKenneth D. Merry } 776130f4520SKenneth D. Merry 777130f4520SKenneth D. Merry /* 778696297adSAlexander Motin * If this is a write or a verify, we're all done. 779130f4520SKenneth D. Merry * If this is a read, we can now send the data to the user. 780130f4520SKenneth D. Merry */ 781696297adSAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 782696297adSAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 783130f4520SKenneth D. Merry ctl_set_success(&io->scsiio); 784130f4520SKenneth D. Merry ctl_complete_beio(beio); 785130f4520SKenneth D. Merry } else { 786f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 78775a3108eSAlexander Motin beio->beio_cont == NULL) { 788f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 78975a3108eSAlexander Motin ctl_serseq_done(io); 79075a3108eSAlexander Motin } 791130f4520SKenneth D. Merry ctl_datamove(io); 792130f4520SKenneth D. Merry } 793130f4520SKenneth D. Merry } 794130f4520SKenneth D. Merry 795130f4520SKenneth D. Merry static void 796ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 797ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 798ef8daf3fSAlexander Motin { 799ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 800ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 801ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 802ef8daf3fSAlexander Motin off_t roff, off; 803ef8daf3fSAlexander Motin int error, status; 804ef8daf3fSAlexander Motin 805ef8daf3fSAlexander Motin DPRINTF("entered\n"); 806ef8daf3fSAlexander Motin 8070bcd4ab6SAlexander Motin off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 808ef8daf3fSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 809ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 810ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 811ef8daf3fSAlexander Motin if (error == 0 && off > roff) 812ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 813ef8daf3fSAlexander Motin else { 814ef8daf3fSAlexander Motin error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 815ef8daf3fSAlexander Motin 0, curthread->td_ucred, curthread); 816ef8daf3fSAlexander Motin if (error == 0 && off > roff) 817ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 818ef8daf3fSAlexander Motin else { 819ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 820ef8daf3fSAlexander Motin off = be_lun->size_bytes; 821ef8daf3fSAlexander Motin } 822ef8daf3fSAlexander Motin } 823b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 824ef8daf3fSAlexander Motin 825ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 826ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 8270bcd4ab6SAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 8280bcd4ab6SAlexander Motin lbalen->lba), data->descr[0].length); 829ef8daf3fSAlexander Motin data->descr[0].status = status; 830ef8daf3fSAlexander Motin 831ef8daf3fSAlexander Motin ctl_complete_beio(beio); 832ef8daf3fSAlexander Motin } 833ef8daf3fSAlexander Motin 83453c146deSAlexander Motin static uint64_t 83553c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 83653c146deSAlexander Motin { 83753c146deSAlexander Motin struct vattr vattr; 83853c146deSAlexander Motin struct statfs statfs; 839b9b4269cSAlexander Motin uint64_t val; 84053c146deSAlexander Motin int error; 84153c146deSAlexander Motin 842b9b4269cSAlexander Motin val = UINT64_MAX; 84353c146deSAlexander Motin if (be_lun->vn == NULL) 844b9b4269cSAlexander Motin return (val); 845b9b4269cSAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 84653c146deSAlexander Motin if (strcmp(attrname, "blocksused") == 0) { 84753c146deSAlexander Motin error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 848b9b4269cSAlexander Motin if (error == 0) 8490bcd4ab6SAlexander Motin val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 85053c146deSAlexander Motin } 851b9b4269cSAlexander Motin if (strcmp(attrname, "blocksavail") == 0 && 852abd80ddbSMateusz Guzik !VN_IS_DOOMED(be_lun->vn)) { 85353c146deSAlexander Motin error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 854b9b4269cSAlexander Motin if (error == 0) 855a15bbf15SAlexander Motin val = statfs.f_bavail * statfs.f_bsize / 8560bcd4ab6SAlexander Motin be_lun->cbe_lun.blocksize; 85753c146deSAlexander Motin } 858b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 859b9b4269cSAlexander Motin return (val); 86053c146deSAlexander Motin } 86153c146deSAlexander Motin 862ef8daf3fSAlexander Motin static void 86367f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 86467f586a8SAlexander Motin struct ctl_be_block_io *beio) 86567f586a8SAlexander Motin { 86667f586a8SAlexander Motin union ctl_io *io; 8673236151eSAlexander Motin struct cdevsw *csw; 8683236151eSAlexander Motin struct cdev *dev; 86967f586a8SAlexander Motin struct uio xuio; 87067f586a8SAlexander Motin struct iovec *xiovec; 8713236151eSAlexander Motin int error, flags, i, ref; 87267f586a8SAlexander Motin 87367f586a8SAlexander Motin DPRINTF("entered\n"); 87467f586a8SAlexander Motin 87567f586a8SAlexander Motin io = beio->io; 87655551d05SAlexander Motin flags = 0; 87755551d05SAlexander Motin if (ARGS(io)->flags & CTL_LLF_DPO) 87855551d05SAlexander Motin flags |= IO_DIRECT; 87955551d05SAlexander Motin if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 88055551d05SAlexander Motin flags |= IO_SYNC; 88167f586a8SAlexander Motin 88267f586a8SAlexander Motin bzero(&xuio, sizeof(xuio)); 88367f586a8SAlexander Motin if (beio->bio_cmd == BIO_READ) { 88436160958SMark Johnston SDT_PROBE0(cbb, , read, file_start); 88567f586a8SAlexander Motin xuio.uio_rw = UIO_READ; 88667f586a8SAlexander Motin } else { 88736160958SMark Johnston SDT_PROBE0(cbb, , write, file_start); 88867f586a8SAlexander Motin xuio.uio_rw = UIO_WRITE; 88967f586a8SAlexander Motin } 89067f586a8SAlexander Motin xuio.uio_offset = beio->io_offset; 89167f586a8SAlexander Motin xuio.uio_resid = beio->io_len; 89267f586a8SAlexander Motin xuio.uio_segflg = UIO_SYSSPACE; 89367f586a8SAlexander Motin xuio.uio_iov = beio->xiovecs; 89467f586a8SAlexander Motin xuio.uio_iovcnt = beio->num_segs; 89567f586a8SAlexander Motin xuio.uio_td = curthread; 89667f586a8SAlexander Motin 89767f586a8SAlexander Motin for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 89867f586a8SAlexander Motin xiovec->iov_base = beio->sg_segs[i].addr; 89967f586a8SAlexander Motin xiovec->iov_len = beio->sg_segs[i].len; 90067f586a8SAlexander Motin } 90167f586a8SAlexander Motin 90267f586a8SAlexander Motin binuptime(&beio->ds_t0); 90367f586a8SAlexander Motin devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 90467f586a8SAlexander Motin 9053236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 9063236151eSAlexander Motin if (csw) { 9073236151eSAlexander Motin if (beio->bio_cmd == BIO_READ) 9083236151eSAlexander Motin error = csw->d_read(dev, &xuio, flags); 9093236151eSAlexander Motin else 9103236151eSAlexander Motin error = csw->d_write(dev, &xuio, flags); 9113236151eSAlexander Motin dev_relthread(dev, ref); 9123236151eSAlexander Motin } else 9133236151eSAlexander Motin error = ENXIO; 9143236151eSAlexander Motin 9153236151eSAlexander Motin if (beio->bio_cmd == BIO_READ) 91636160958SMark Johnston SDT_PROBE0(cbb, , read, file_done); 9173236151eSAlexander Motin else 91836160958SMark Johnston SDT_PROBE0(cbb, , write, file_done); 91967f586a8SAlexander Motin 92067f586a8SAlexander Motin mtx_lock(&be_lun->io_lock); 92167f586a8SAlexander Motin devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 92267f586a8SAlexander Motin beio->ds_tag_type, beio->ds_trans_type, 92367f586a8SAlexander Motin /*now*/ NULL, /*then*/&beio->ds_t0); 92467f586a8SAlexander Motin mtx_unlock(&be_lun->io_lock); 92567f586a8SAlexander Motin 92667f586a8SAlexander Motin /* 92767f586a8SAlexander Motin * If we got an error, set the sense data to "MEDIUM ERROR" and 92867f586a8SAlexander Motin * return the I/O to the user. 92967f586a8SAlexander Motin */ 93067f586a8SAlexander Motin if (error != 0) { 9310631de4aSAlexander Motin if (error == ENOSPC || error == EDQUOT) { 9324fc18ff9SAlexander Motin ctl_set_space_alloc_fail(&io->scsiio); 9336187d472SAlexander Motin } else if (error == EROFS || error == EACCES) { 9346187d472SAlexander Motin ctl_set_hw_write_protected(&io->scsiio); 9357f7bb97aSAlexander Motin } else { 9367f7bb97aSAlexander Motin ctl_set_medium_error(&io->scsiio, 9377f7bb97aSAlexander Motin beio->bio_cmd == BIO_READ); 9387f7bb97aSAlexander Motin } 93967f586a8SAlexander Motin ctl_complete_beio(beio); 94067f586a8SAlexander Motin return; 94167f586a8SAlexander Motin } 94267f586a8SAlexander Motin 94367f586a8SAlexander Motin /* 94467f586a8SAlexander Motin * If this is a write or a verify, we're all done. 94567f586a8SAlexander Motin * If this is a read, we can now send the data to the user. 94667f586a8SAlexander Motin */ 94767f586a8SAlexander Motin if ((beio->bio_cmd == BIO_WRITE) || 94867f586a8SAlexander Motin (ARGS(io)->flags & CTL_LLF_VERIFY)) { 94967f586a8SAlexander Motin ctl_set_success(&io->scsiio); 95067f586a8SAlexander Motin ctl_complete_beio(beio); 95167f586a8SAlexander Motin } else { 952f7241cceSAlexander Motin if ((ARGS(io)->flags & CTL_LLF_READ) && 95375a3108eSAlexander Motin beio->beio_cont == NULL) { 954f7241cceSAlexander Motin ctl_set_success(&io->scsiio); 95575a3108eSAlexander Motin ctl_serseq_done(io); 95675a3108eSAlexander Motin } 95767f586a8SAlexander Motin ctl_datamove(io); 95867f586a8SAlexander Motin } 95967f586a8SAlexander Motin } 96067f586a8SAlexander Motin 96167f586a8SAlexander Motin static void 962ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 963ef8daf3fSAlexander Motin struct ctl_be_block_io *beio) 964ef8daf3fSAlexander Motin { 965ef8daf3fSAlexander Motin union ctl_io *io = beio->io; 9663236151eSAlexander Motin struct cdevsw *csw; 9673236151eSAlexander Motin struct cdev *dev; 968ef8daf3fSAlexander Motin struct ctl_lba_len_flags *lbalen = ARGS(io); 969ef8daf3fSAlexander Motin struct scsi_get_lba_status_data *data; 970ef8daf3fSAlexander Motin off_t roff, off; 9713236151eSAlexander Motin int error, ref, status; 972ef8daf3fSAlexander Motin 973ef8daf3fSAlexander Motin DPRINTF("entered\n"); 974ef8daf3fSAlexander Motin 9753236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 9763236151eSAlexander Motin if (csw == NULL) { 9773236151eSAlexander Motin status = 0; /* unknown up to the end */ 9783236151eSAlexander Motin off = be_lun->size_bytes; 9793236151eSAlexander Motin goto done; 9803236151eSAlexander Motin } 9810bcd4ab6SAlexander Motin off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 9823236151eSAlexander Motin error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 9833236151eSAlexander Motin curthread); 984ef8daf3fSAlexander Motin if (error == 0 && off > roff) 985ef8daf3fSAlexander Motin status = 0; /* mapped up to off */ 986ef8daf3fSAlexander Motin else { 9873236151eSAlexander Motin error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 9883236151eSAlexander Motin curthread); 989ef8daf3fSAlexander Motin if (error == 0 && off > roff) 990ef8daf3fSAlexander Motin status = 1; /* deallocated up to off */ 991ef8daf3fSAlexander Motin else { 992ef8daf3fSAlexander Motin status = 0; /* unknown up to the end */ 993ef8daf3fSAlexander Motin off = be_lun->size_bytes; 994ef8daf3fSAlexander Motin } 995ef8daf3fSAlexander Motin } 9963236151eSAlexander Motin dev_relthread(dev, ref); 997ef8daf3fSAlexander Motin 9983236151eSAlexander Motin done: 999ef8daf3fSAlexander Motin data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 1000ef8daf3fSAlexander Motin scsi_u64to8b(lbalen->lba, data->descr[0].addr); 10010bcd4ab6SAlexander Motin scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 10020bcd4ab6SAlexander Motin lbalen->lba), data->descr[0].length); 1003ef8daf3fSAlexander Motin data->descr[0].status = status; 1004ef8daf3fSAlexander Motin 1005ef8daf3fSAlexander Motin ctl_complete_beio(beio); 1006ef8daf3fSAlexander Motin } 1007ef8daf3fSAlexander Motin 1008ef8daf3fSAlexander Motin static void 1009130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 1010130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 1011130f4520SKenneth D. Merry { 1012130f4520SKenneth D. Merry struct bio *bio; 10133236151eSAlexander Motin struct cdevsw *csw; 10143236151eSAlexander Motin struct cdev *dev; 10153236151eSAlexander Motin int ref; 1016130f4520SKenneth D. Merry 1017130f4520SKenneth D. Merry DPRINTF("entered\n"); 1018130f4520SKenneth D. Merry 1019130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 1020130f4520SKenneth D. Merry bio = g_alloc_bio(); 1021130f4520SKenneth D. Merry 1022130f4520SKenneth D. Merry bio->bio_cmd = BIO_FLUSH; 1023130f4520SKenneth D. Merry bio->bio_offset = 0; 1024130f4520SKenneth D. Merry bio->bio_data = 0; 1025130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 1026130f4520SKenneth D. Merry bio->bio_caller1 = beio; 1027130f4520SKenneth D. Merry bio->bio_pblkno = 0; 1028130f4520SKenneth D. Merry 1029130f4520SKenneth D. Merry /* 1030130f4520SKenneth D. Merry * We don't need to acquire the LUN lock here, because we are only 1031130f4520SKenneth D. Merry * sending one bio, and so there is no other context to synchronize 1032130f4520SKenneth D. Merry * with. 1033130f4520SKenneth D. Merry */ 1034130f4520SKenneth D. Merry beio->num_bios_sent = 1; 1035130f4520SKenneth D. Merry beio->send_complete = 1; 1036130f4520SKenneth D. Merry 1037130f4520SKenneth D. Merry binuptime(&beio->ds_t0); 1038130f4520SKenneth D. Merry devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1039130f4520SKenneth D. Merry 10403236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 10413236151eSAlexander Motin if (csw) { 10423236151eSAlexander Motin bio->bio_dev = dev; 10433236151eSAlexander Motin csw->d_strategy(bio); 10443236151eSAlexander Motin dev_relthread(dev, ref); 10453236151eSAlexander Motin } else { 10463236151eSAlexander Motin bio->bio_error = ENXIO; 10473236151eSAlexander Motin ctl_be_block_biodone(bio); 10483236151eSAlexander Motin } 1049130f4520SKenneth D. Merry } 1050130f4520SKenneth D. Merry 1051130f4520SKenneth D. Merry static void 1052ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1053ee7f31c0SAlexander Motin struct ctl_be_block_io *beio, 1054ee7f31c0SAlexander Motin uint64_t off, uint64_t len, int last) 1055ee7f31c0SAlexander Motin { 1056ee7f31c0SAlexander Motin struct bio *bio; 10578f5a226aSAlexander Motin uint64_t maxlen; 10583236151eSAlexander Motin struct cdevsw *csw; 10593236151eSAlexander Motin struct cdev *dev; 10603236151eSAlexander Motin int ref; 1061ee7f31c0SAlexander Motin 10623236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 10630bcd4ab6SAlexander Motin maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1064ee7f31c0SAlexander Motin while (len > 0) { 1065ee7f31c0SAlexander Motin bio = g_alloc_bio(); 1066ee7f31c0SAlexander Motin bio->bio_cmd = BIO_DELETE; 10673236151eSAlexander Motin bio->bio_dev = dev; 1068ee7f31c0SAlexander Motin bio->bio_offset = off; 10698f5a226aSAlexander Motin bio->bio_length = MIN(len, maxlen); 1070ee7f31c0SAlexander Motin bio->bio_data = 0; 1071ee7f31c0SAlexander Motin bio->bio_done = ctl_be_block_biodone; 1072ee7f31c0SAlexander Motin bio->bio_caller1 = beio; 10730bcd4ab6SAlexander Motin bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1074ee7f31c0SAlexander Motin 1075ee7f31c0SAlexander Motin off += bio->bio_length; 1076ee7f31c0SAlexander Motin len -= bio->bio_length; 1077ee7f31c0SAlexander Motin 107875c7a1d3SAlexander Motin mtx_lock(&be_lun->io_lock); 1079ee7f31c0SAlexander Motin beio->num_bios_sent++; 1080ee7f31c0SAlexander Motin if (last && len == 0) 1081ee7f31c0SAlexander Motin beio->send_complete = 1; 108275c7a1d3SAlexander Motin mtx_unlock(&be_lun->io_lock); 1083ee7f31c0SAlexander Motin 10843236151eSAlexander Motin if (csw) { 10853236151eSAlexander Motin csw->d_strategy(bio); 10863236151eSAlexander Motin } else { 10873236151eSAlexander Motin bio->bio_error = ENXIO; 10883236151eSAlexander Motin ctl_be_block_biodone(bio); 1089ee7f31c0SAlexander Motin } 1090ee7f31c0SAlexander Motin } 10913236151eSAlexander Motin if (csw) 10923236151eSAlexander Motin dev_relthread(dev, ref); 10933236151eSAlexander Motin } 1094ee7f31c0SAlexander Motin 1095ee7f31c0SAlexander Motin static void 1096ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1097ee7f31c0SAlexander Motin struct ctl_be_block_io *beio) 1098ee7f31c0SAlexander Motin { 1099ee7f31c0SAlexander Motin union ctl_io *io; 110066df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1101ee7f31c0SAlexander Motin struct scsi_unmap_desc *buf, *end; 1102ee7f31c0SAlexander Motin uint64_t len; 1103ee7f31c0SAlexander Motin 1104ee7f31c0SAlexander Motin io = beio->io; 1105ee7f31c0SAlexander Motin 1106ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1107ee7f31c0SAlexander Motin 1108ee7f31c0SAlexander Motin binuptime(&beio->ds_t0); 1109ee7f31c0SAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1110ee7f31c0SAlexander Motin 1111ee7f31c0SAlexander Motin if (beio->io_offset == -1) { 1112ee7f31c0SAlexander Motin beio->io_len = 0; 111366df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 111466df9136SAlexander Motin buf = (struct scsi_unmap_desc *)ptrlen->ptr; 111566df9136SAlexander Motin end = buf + ptrlen->len / sizeof(*buf); 1116ee7f31c0SAlexander Motin for (; buf < end; buf++) { 1117ee7f31c0SAlexander Motin len = (uint64_t)scsi_4btoul(buf->length) * 11180bcd4ab6SAlexander Motin be_lun->cbe_lun.blocksize; 1119ee7f31c0SAlexander Motin beio->io_len += len; 1120ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 11210bcd4ab6SAlexander Motin scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 11220bcd4ab6SAlexander Motin len, (end - buf < 2) ? TRUE : FALSE); 1123ee7f31c0SAlexander Motin } 1124ee7f31c0SAlexander Motin } else 1125ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(be_lun, beio, 1126ee7f31c0SAlexander Motin beio->io_offset, beio->io_len, TRUE); 1127ee7f31c0SAlexander Motin } 1128ee7f31c0SAlexander Motin 1129ee7f31c0SAlexander Motin static void 1130130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1131130f4520SKenneth D. Merry struct ctl_be_block_io *beio) 1132130f4520SKenneth D. Merry { 113375c7a1d3SAlexander Motin TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1134130f4520SKenneth D. Merry struct bio *bio; 11353236151eSAlexander Motin struct cdevsw *csw; 11363236151eSAlexander Motin struct cdev *dev; 1137130f4520SKenneth D. Merry off_t cur_offset; 11383236151eSAlexander Motin int i, max_iosize, ref; 1139130f4520SKenneth D. Merry 1140130f4520SKenneth D. Merry DPRINTF("entered\n"); 11413236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 1142130f4520SKenneth D. Merry 1143130f4520SKenneth D. Merry /* 1144130f4520SKenneth D. Merry * We have to limit our I/O size to the maximum supported by the 11458054320eSAlexander Motin * backend device. 1146130f4520SKenneth D. Merry */ 11473236151eSAlexander Motin if (csw) { 11483236151eSAlexander Motin max_iosize = dev->si_iosize_max; 1149130f4520SKenneth D. Merry if (max_iosize < PAGE_SIZE) 1150130f4520SKenneth D. Merry max_iosize = DFLTPHYS; 11513236151eSAlexander Motin } else 11523236151eSAlexander Motin max_iosize = DFLTPHYS; 1153130f4520SKenneth D. Merry 1154130f4520SKenneth D. Merry cur_offset = beio->io_offset; 1155130f4520SKenneth D. Merry for (i = 0; i < beio->num_segs; i++) { 1156130f4520SKenneth D. Merry size_t cur_size; 1157130f4520SKenneth D. Merry uint8_t *cur_ptr; 1158130f4520SKenneth D. Merry 1159130f4520SKenneth D. Merry cur_size = beio->sg_segs[i].len; 1160130f4520SKenneth D. Merry cur_ptr = beio->sg_segs[i].addr; 1161130f4520SKenneth D. Merry 1162130f4520SKenneth D. Merry while (cur_size > 0) { 1163130f4520SKenneth D. Merry /* This can't fail, it's a blocking allocation. */ 1164130f4520SKenneth D. Merry bio = g_alloc_bio(); 1165130f4520SKenneth D. Merry 1166130f4520SKenneth D. Merry KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1167130f4520SKenneth D. Merry 1168130f4520SKenneth D. Merry bio->bio_cmd = beio->bio_cmd; 11693236151eSAlexander Motin bio->bio_dev = dev; 1170130f4520SKenneth D. Merry bio->bio_caller1 = beio; 1171130f4520SKenneth D. Merry bio->bio_length = min(cur_size, max_iosize); 1172130f4520SKenneth D. Merry bio->bio_offset = cur_offset; 1173130f4520SKenneth D. Merry bio->bio_data = cur_ptr; 1174130f4520SKenneth D. Merry bio->bio_done = ctl_be_block_biodone; 11750bcd4ab6SAlexander Motin bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1176130f4520SKenneth D. Merry 1177130f4520SKenneth D. Merry cur_offset += bio->bio_length; 1178130f4520SKenneth D. Merry cur_ptr += bio->bio_length; 1179130f4520SKenneth D. Merry cur_size -= bio->bio_length; 1180130f4520SKenneth D. Merry 118175c7a1d3SAlexander Motin TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1182130f4520SKenneth D. Merry beio->num_bios_sent++; 1183130f4520SKenneth D. Merry } 1184130f4520SKenneth D. Merry } 118575c7a1d3SAlexander Motin beio->send_complete = 1; 1186024932aaSAlexander Motin binuptime(&beio->ds_t0); 1187024932aaSAlexander Motin devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 118875c7a1d3SAlexander Motin 118975c7a1d3SAlexander Motin /* 119075c7a1d3SAlexander Motin * Fire off all allocated requests! 119175c7a1d3SAlexander Motin */ 119275c7a1d3SAlexander Motin while ((bio = TAILQ_FIRST(&queue)) != NULL) { 119375c7a1d3SAlexander Motin TAILQ_REMOVE(&queue, bio, bio_queue); 11943236151eSAlexander Motin if (csw) 11953236151eSAlexander Motin csw->d_strategy(bio); 11963236151eSAlexander Motin else { 11973236151eSAlexander Motin bio->bio_error = ENXIO; 11983236151eSAlexander Motin ctl_be_block_biodone(bio); 119975c7a1d3SAlexander Motin } 1200130f4520SKenneth D. Merry } 12013236151eSAlexander Motin if (csw) 12023236151eSAlexander Motin dev_relthread(dev, ref); 12033236151eSAlexander Motin } 1204130f4520SKenneth D. Merry 1205c3e7ba3eSAlexander Motin static uint64_t 1206c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1207c3e7ba3eSAlexander Motin { 1208c3e7ba3eSAlexander Motin struct diocgattr_arg arg; 12093236151eSAlexander Motin struct cdevsw *csw; 12103236151eSAlexander Motin struct cdev *dev; 12113236151eSAlexander Motin int error, ref; 1212c3e7ba3eSAlexander Motin 12133236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 12143236151eSAlexander Motin if (csw == NULL) 1215c3e7ba3eSAlexander Motin return (UINT64_MAX); 1216c3e7ba3eSAlexander Motin strlcpy(arg.name, attrname, sizeof(arg.name)); 1217c3e7ba3eSAlexander Motin arg.len = sizeof(arg.value.off); 12183236151eSAlexander Motin if (csw->d_ioctl) { 12193236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 12203236151eSAlexander Motin curthread); 12213236151eSAlexander Motin } else 12223236151eSAlexander Motin error = ENODEV; 12233236151eSAlexander Motin dev_relthread(dev, ref); 1224c3e7ba3eSAlexander Motin if (error != 0) 1225c3e7ba3eSAlexander Motin return (UINT64_MAX); 1226c3e7ba3eSAlexander Motin return (arg.value.off); 1227c3e7ba3eSAlexander Motin } 1228c3e7ba3eSAlexander Motin 1229130f4520SKenneth D. Merry static void 12307d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 12317d0d4342SAlexander Motin union ctl_io *io) 12327d0d4342SAlexander Motin { 12330bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 12347d0d4342SAlexander Motin struct ctl_be_block_io *beio; 12357d0d4342SAlexander Motin struct ctl_lba_len_flags *lbalen; 12367d0d4342SAlexander Motin 12377d0d4342SAlexander Motin DPRINTF("entered\n"); 12387d0d4342SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 12397d0d4342SAlexander Motin lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 12407d0d4342SAlexander Motin 12410bcd4ab6SAlexander Motin beio->io_len = lbalen->len * cbe_lun->blocksize; 12420bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 12437d0d4342SAlexander Motin beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 12447d0d4342SAlexander Motin beio->bio_cmd = BIO_FLUSH; 12457d0d4342SAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 12467d0d4342SAlexander Motin DPRINTF("SYNC\n"); 12477d0d4342SAlexander Motin be_lun->lun_flush(be_lun, beio); 12487d0d4342SAlexander Motin } 12497d0d4342SAlexander Motin 12507d0d4342SAlexander Motin static void 1251ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1252ee7f31c0SAlexander Motin { 1253ee7f31c0SAlexander Motin union ctl_io *io; 1254ee7f31c0SAlexander Motin 1255ee7f31c0SAlexander Motin io = beio->io; 1256ee7f31c0SAlexander Motin ctl_free_beio(beio); 1257ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1258ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1259ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1260ee7f31c0SAlexander Motin ctl_config_write_done(io); 1261ee7f31c0SAlexander Motin return; 1262ee7f31c0SAlexander Motin } 1263ee7f31c0SAlexander Motin 1264ee7f31c0SAlexander Motin ctl_be_block_config_write(io); 1265ee7f31c0SAlexander Motin } 1266ee7f31c0SAlexander Motin 1267ee7f31c0SAlexander Motin static void 1268ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1269ee7f31c0SAlexander Motin union ctl_io *io) 1270ee7f31c0SAlexander Motin { 12710d7fed74SAlexander Motin struct ctl_be_block_softc *softc = be_lun->softc; 12720bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1273ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 127466df9136SAlexander Motin struct ctl_lba_len_flags *lbalen; 1275fee04ef7SAlexander Motin uint64_t len_left, lba; 1276fee04ef7SAlexander Motin uint32_t pb, pbo, adj; 1277ee7f31c0SAlexander Motin int i, seglen; 1278ee7f31c0SAlexander Motin uint8_t *buf, *end; 1279ee7f31c0SAlexander Motin 1280ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1281ee7f31c0SAlexander Motin 1282e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1283*a59e2982SAlexander Motin lbalen = ARGS(io); 1284ee7f31c0SAlexander Motin 128564c5167cSAlexander Motin if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 12863406a2a0SAlexander Motin (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1287ee7f31c0SAlexander Motin ctl_free_beio(beio); 1288ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1289ee7f31c0SAlexander Motin /*sks_valid*/ 1, 1290ee7f31c0SAlexander Motin /*command*/ 1, 1291ee7f31c0SAlexander Motin /*field*/ 1, 1292ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1293ee7f31c0SAlexander Motin /*bit*/ 0); 1294ee7f31c0SAlexander Motin ctl_config_write_done(io); 1295ee7f31c0SAlexander Motin return; 1296ee7f31c0SAlexander Motin } 1297ee7f31c0SAlexander Motin 12983406a2a0SAlexander Motin if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 12990bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 13000bcd4ab6SAlexander Motin beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1301ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1302ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 1303ee7f31c0SAlexander Motin 1304ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1305ee7f31c0SAlexander Motin return; 1306ee7f31c0SAlexander Motin } 1307ee7f31c0SAlexander Motin 1308ee7f31c0SAlexander Motin beio->bio_cmd = BIO_WRITE; 1309ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_WRITE; 1310ee7f31c0SAlexander Motin 1311ee7f31c0SAlexander Motin DPRINTF("WRITE SAME at LBA %jx len %u\n", 131266df9136SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len); 1313ee7f31c0SAlexander Motin 13140bcd4ab6SAlexander Motin pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 13150bcd4ab6SAlexander Motin if (be_lun->cbe_lun.pblockoff > 0) 13160bcd4ab6SAlexander Motin pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1317fee04ef7SAlexander Motin else 1318fee04ef7SAlexander Motin pbo = 0; 13190bcd4ab6SAlexander Motin len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1320cd853791SKonstantin Belousov for (i = 0, lba = 0; i < CTLBLK_NUM_SEGS && len_left > 0; i++) { 1321ee7f31c0SAlexander Motin /* 1322ee7f31c0SAlexander Motin * Setup the S/G entry for this chunk. 1323ee7f31c0SAlexander Motin */ 132408a7cce5SAlexander Motin seglen = MIN(CTLBLK_MAX_SEG, len_left); 13250bcd4ab6SAlexander Motin if (pb > cbe_lun->blocksize) { 13260bcd4ab6SAlexander Motin adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 132793b8c96cSAlexander Motin seglen - pbo) % pb; 132893b8c96cSAlexander Motin if (seglen > adj) 132993b8c96cSAlexander Motin seglen -= adj; 133093b8c96cSAlexander Motin else 13310bcd4ab6SAlexander Motin seglen -= seglen % cbe_lun->blocksize; 133293b8c96cSAlexander Motin } else 13330bcd4ab6SAlexander Motin seglen -= seglen % cbe_lun->blocksize; 13348054320eSAlexander Motin ctl_alloc_seg(softc, &beio->sg_segs[i], seglen); 1335ee7f31c0SAlexander Motin 1336ee7f31c0SAlexander Motin DPRINTF("segment %d addr %p len %zd\n", i, 1337ee7f31c0SAlexander Motin beio->sg_segs[i].addr, beio->sg_segs[i].len); 1338ee7f31c0SAlexander Motin 1339ee7f31c0SAlexander Motin beio->num_segs++; 1340ee7f31c0SAlexander Motin len_left -= seglen; 1341ee7f31c0SAlexander Motin 1342ee7f31c0SAlexander Motin buf = beio->sg_segs[i].addr; 1343ee7f31c0SAlexander Motin end = buf + seglen; 13440bcd4ab6SAlexander Motin for (; buf < end; buf += cbe_lun->blocksize) { 13456c2acea5SAlexander Motin if (lbalen->flags & SWS_NDOB) { 13466c2acea5SAlexander Motin memset(buf, 0, cbe_lun->blocksize); 13476c2acea5SAlexander Motin } else { 13486c2acea5SAlexander Motin memcpy(buf, io->scsiio.kern_data_ptr, 13496c2acea5SAlexander Motin cbe_lun->blocksize); 13506c2acea5SAlexander Motin } 135166df9136SAlexander Motin if (lbalen->flags & SWS_LBDATA) 135266df9136SAlexander Motin scsi_ulto4b(lbalen->lba + lba, buf); 1353ee7f31c0SAlexander Motin lba++; 1354ee7f31c0SAlexander Motin } 1355ee7f31c0SAlexander Motin } 1356ee7f31c0SAlexander Motin 13570bcd4ab6SAlexander Motin beio->io_offset = lbalen->lba * cbe_lun->blocksize; 13580bcd4ab6SAlexander Motin beio->io_len = lba * cbe_lun->blocksize; 1359ee7f31c0SAlexander Motin 1360ee7f31c0SAlexander Motin /* We can not do all in one run. Correct and schedule rerun. */ 1361ee7f31c0SAlexander Motin if (len_left > 0) { 136266df9136SAlexander Motin lbalen->lba += lba; 136366df9136SAlexander Motin lbalen->len -= lba; 1364ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done_ws; 1365ee7f31c0SAlexander Motin } 1366ee7f31c0SAlexander Motin 1367ee7f31c0SAlexander Motin be_lun->dispatch(be_lun, beio); 1368ee7f31c0SAlexander Motin } 1369ee7f31c0SAlexander Motin 1370ee7f31c0SAlexander Motin static void 1371ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1372ee7f31c0SAlexander Motin union ctl_io *io) 1373ee7f31c0SAlexander Motin { 1374ee7f31c0SAlexander Motin struct ctl_be_block_io *beio; 137566df9136SAlexander Motin struct ctl_ptr_len_flags *ptrlen; 1376ee7f31c0SAlexander Motin 1377ee7f31c0SAlexander Motin DPRINTF("entered\n"); 1378ee7f31c0SAlexander Motin 1379e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 138066df9136SAlexander Motin ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1381ee7f31c0SAlexander Motin 13823406a2a0SAlexander Motin if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1383ee7f31c0SAlexander Motin ctl_free_beio(beio); 1384ee7f31c0SAlexander Motin ctl_set_invalid_field(&io->scsiio, 1385ee7f31c0SAlexander Motin /*sks_valid*/ 0, 1386ee7f31c0SAlexander Motin /*command*/ 1, 1387ee7f31c0SAlexander Motin /*field*/ 0, 1388ee7f31c0SAlexander Motin /*bit_valid*/ 0, 1389ee7f31c0SAlexander Motin /*bit*/ 0); 1390ee7f31c0SAlexander Motin ctl_config_write_done(io); 1391ee7f31c0SAlexander Motin return; 1392ee7f31c0SAlexander Motin } 1393ee7f31c0SAlexander Motin 1394ee7f31c0SAlexander Motin beio->io_len = 0; 1395ee7f31c0SAlexander Motin beio->io_offset = -1; 1396ee7f31c0SAlexander Motin beio->bio_cmd = BIO_DELETE; 1397ee7f31c0SAlexander Motin beio->ds_trans_type = DEVSTAT_FREE; 139866df9136SAlexander Motin DPRINTF("UNMAP\n"); 1399ee7f31c0SAlexander Motin be_lun->unmap(be_lun, beio); 1400ee7f31c0SAlexander Motin } 1401ee7f31c0SAlexander Motin 1402ee7f31c0SAlexander Motin static void 1403ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio) 1404ef8daf3fSAlexander Motin { 1405ef8daf3fSAlexander Motin union ctl_io *io; 1406ef8daf3fSAlexander Motin 1407ef8daf3fSAlexander Motin io = beio->io; 1408ef8daf3fSAlexander Motin ctl_free_beio(beio); 1409ef8daf3fSAlexander Motin ctl_config_read_done(io); 1410ef8daf3fSAlexander Motin } 1411ef8daf3fSAlexander Motin 1412ef8daf3fSAlexander Motin static void 1413ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1414ef8daf3fSAlexander Motin union ctl_io *io) 1415ef8daf3fSAlexander Motin { 1416ef8daf3fSAlexander Motin struct ctl_be_block_io *beio; 1417ef8daf3fSAlexander Motin struct ctl_be_block_softc *softc; 1418ef8daf3fSAlexander Motin 1419ef8daf3fSAlexander Motin DPRINTF("entered\n"); 1420ef8daf3fSAlexander Motin 1421ef8daf3fSAlexander Motin softc = be_lun->softc; 1422ef8daf3fSAlexander Motin beio = ctl_alloc_beio(softc); 1423ef8daf3fSAlexander Motin beio->io = io; 1424ef8daf3fSAlexander Motin beio->lun = be_lun; 1425ef8daf3fSAlexander Motin beio->beio_cont = ctl_be_block_cr_done; 1426ef8daf3fSAlexander Motin PRIV(io)->ptr = (void *)beio; 1427ef8daf3fSAlexander Motin 1428ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 1429ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1430ef8daf3fSAlexander Motin beio->bio_cmd = -1; 1431ef8daf3fSAlexander Motin beio->ds_trans_type = DEVSTAT_NO_DATA; 1432ef8daf3fSAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1433ef8daf3fSAlexander Motin beio->io_len = 0; 1434ef8daf3fSAlexander Motin if (be_lun->get_lba_status) 1435ef8daf3fSAlexander Motin be_lun->get_lba_status(be_lun, beio); 1436ef8daf3fSAlexander Motin else 1437ef8daf3fSAlexander Motin ctl_be_block_cr_done(beio); 1438ef8daf3fSAlexander Motin break; 1439ef8daf3fSAlexander Motin default: 1440ef8daf3fSAlexander Motin panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1441ef8daf3fSAlexander Motin break; 1442ef8daf3fSAlexander Motin } 1443ef8daf3fSAlexander Motin } 1444ef8daf3fSAlexander Motin 1445ef8daf3fSAlexander Motin static void 1446ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1447ee7f31c0SAlexander Motin { 1448ee7f31c0SAlexander Motin union ctl_io *io; 1449ee7f31c0SAlexander Motin 1450ee7f31c0SAlexander Motin io = beio->io; 1451ee7f31c0SAlexander Motin ctl_free_beio(beio); 1452ee7f31c0SAlexander Motin ctl_config_write_done(io); 1453ee7f31c0SAlexander Motin } 1454ee7f31c0SAlexander Motin 1455ee7f31c0SAlexander Motin static void 1456130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1457130f4520SKenneth D. Merry union ctl_io *io) 1458130f4520SKenneth D. Merry { 1459130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1460130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 1461130f4520SKenneth D. Merry 1462130f4520SKenneth D. Merry DPRINTF("entered\n"); 1463130f4520SKenneth D. Merry 1464130f4520SKenneth D. Merry softc = be_lun->softc; 1465130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1466130f4520SKenneth D. Merry beio->io = io; 1467130f4520SKenneth D. Merry beio->lun = be_lun; 1468ee7f31c0SAlexander Motin beio->beio_cont = ctl_be_block_cw_done; 14697d0d4342SAlexander Motin switch (io->scsiio.tag_type) { 14707d0d4342SAlexander Motin case CTL_TAG_ORDERED: 14717d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 14727d0d4342SAlexander Motin break; 14737d0d4342SAlexander Motin case CTL_TAG_HEAD_OF_QUEUE: 14747d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_HEAD; 14757d0d4342SAlexander Motin break; 14767d0d4342SAlexander Motin case CTL_TAG_UNTAGGED: 14777d0d4342SAlexander Motin case CTL_TAG_SIMPLE: 14787d0d4342SAlexander Motin case CTL_TAG_ACA: 14797d0d4342SAlexander Motin default: 14807d0d4342SAlexander Motin beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 14817d0d4342SAlexander Motin break; 14827d0d4342SAlexander Motin } 1483e86a4142SAlexander Motin PRIV(io)->ptr = (void *)beio; 1484130f4520SKenneth D. Merry 1485130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 1486130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 1487130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 14887d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(be_lun, io); 1489130f4520SKenneth D. Merry break; 1490ee7f31c0SAlexander Motin case WRITE_SAME_10: 1491ee7f31c0SAlexander Motin case WRITE_SAME_16: 1492ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(be_lun, io); 1493ee7f31c0SAlexander Motin break; 1494ee7f31c0SAlexander Motin case UNMAP: 1495ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(be_lun, io); 1496ee7f31c0SAlexander Motin break; 1497130f4520SKenneth D. Merry default: 1498130f4520SKenneth D. Merry panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1499130f4520SKenneth D. Merry break; 1500130f4520SKenneth D. Merry } 1501130f4520SKenneth D. Merry } 1502130f4520SKenneth D. Merry 150336160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t"); 150436160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t"); 150536160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t"); 150636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t"); 1507130f4520SKenneth D. Merry 1508130f4520SKenneth D. Merry static void 150908a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio) 151008a7cce5SAlexander Motin { 151108a7cce5SAlexander Motin struct ctl_be_block_lun *be_lun; 151208a7cce5SAlexander Motin union ctl_io *io; 151308a7cce5SAlexander Motin 151408a7cce5SAlexander Motin io = beio->io; 151508a7cce5SAlexander Motin be_lun = beio->lun; 151608a7cce5SAlexander Motin ctl_free_beio(beio); 1517ead2f117SAlexander Motin if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1518ead2f117SAlexander Motin ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1519ead2f117SAlexander Motin (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 152011b569f7SAlexander Motin ctl_data_submit_done(io); 152108a7cce5SAlexander Motin return; 152208a7cce5SAlexander Motin } 152308a7cce5SAlexander Motin 152408a7cce5SAlexander Motin io->io_hdr.status &= ~CTL_STATUS_MASK; 152508a7cce5SAlexander Motin io->io_hdr.status |= CTL_STATUS_NONE; 152608a7cce5SAlexander Motin 152775c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 152808a7cce5SAlexander Motin STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 152975c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 153008a7cce5SAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 153108a7cce5SAlexander Motin } 153208a7cce5SAlexander Motin 153308a7cce5SAlexander Motin static void 1534130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1535130f4520SKenneth D. Merry union ctl_io *io) 1536130f4520SKenneth D. Merry { 15370bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1538130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1539130f4520SKenneth D. Merry struct ctl_be_block_softc *softc; 154011b569f7SAlexander Motin struct ctl_lba_len_flags *lbalen; 1541e86a4142SAlexander Motin struct ctl_ptr_len_flags *bptrlen; 1542e86a4142SAlexander Motin uint64_t len_left, lbas; 1543130f4520SKenneth D. Merry int i; 1544130f4520SKenneth D. Merry 1545130f4520SKenneth D. Merry softc = be_lun->softc; 1546130f4520SKenneth D. Merry 1547130f4520SKenneth D. Merry DPRINTF("entered\n"); 1548130f4520SKenneth D. Merry 154911b569f7SAlexander Motin lbalen = ARGS(io); 155011b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 155136160958SMark Johnston SDT_PROBE0(cbb, , write, start); 155211b569f7SAlexander Motin } else { 155336160958SMark Johnston SDT_PROBE0(cbb, , read, start); 1554130f4520SKenneth D. Merry } 1555130f4520SKenneth D. Merry 1556130f4520SKenneth D. Merry beio = ctl_alloc_beio(softc); 1557130f4520SKenneth D. Merry beio->io = io; 1558130f4520SKenneth D. Merry beio->lun = be_lun; 1559e86a4142SAlexander Motin bptrlen = PRIV(io); 1560e86a4142SAlexander Motin bptrlen->ptr = (void *)beio; 1561130f4520SKenneth D. Merry 1562130f4520SKenneth D. Merry switch (io->scsiio.tag_type) { 1563130f4520SKenneth D. Merry case CTL_TAG_ORDERED: 1564130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1565130f4520SKenneth D. Merry break; 1566130f4520SKenneth D. Merry case CTL_TAG_HEAD_OF_QUEUE: 1567130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1568130f4520SKenneth D. Merry break; 1569130f4520SKenneth D. Merry case CTL_TAG_UNTAGGED: 1570130f4520SKenneth D. Merry case CTL_TAG_SIMPLE: 1571130f4520SKenneth D. Merry case CTL_TAG_ACA: 1572130f4520SKenneth D. Merry default: 1573130f4520SKenneth D. Merry beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1574130f4520SKenneth D. Merry break; 1575130f4520SKenneth D. Merry } 1576130f4520SKenneth D. Merry 157711b569f7SAlexander Motin if (lbalen->flags & CTL_LLF_WRITE) { 1578130f4520SKenneth D. Merry beio->bio_cmd = BIO_WRITE; 1579130f4520SKenneth D. Merry beio->ds_trans_type = DEVSTAT_WRITE; 158011b569f7SAlexander Motin } else { 158111b569f7SAlexander Motin beio->bio_cmd = BIO_READ; 158211b569f7SAlexander Motin beio->ds_trans_type = DEVSTAT_READ; 1583130f4520SKenneth D. Merry } 1584130f4520SKenneth D. Merry 158508a7cce5SAlexander Motin DPRINTF("%s at LBA %jx len %u @%ju\n", 1586130f4520SKenneth D. Merry (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1587e86a4142SAlexander Motin (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 15880d7fed74SAlexander Motin if (lbalen->flags & CTL_LLF_COMPARE) { 15890d7fed74SAlexander Motin beio->two_sglists = 1; 159011b569f7SAlexander Motin lbas = CTLBLK_HALF_IO_SIZE; 15910d7fed74SAlexander Motin } else { 159211b569f7SAlexander Motin lbas = CTLBLK_MAX_IO_SIZE; 15930d7fed74SAlexander Motin } 15940bcd4ab6SAlexander Motin lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 15950bcd4ab6SAlexander Motin beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 15960bcd4ab6SAlexander Motin beio->io_len = lbas * cbe_lun->blocksize; 1597e86a4142SAlexander Motin bptrlen->len += lbas; 1598130f4520SKenneth D. Merry 159908a7cce5SAlexander Motin for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 160008a7cce5SAlexander Motin KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 160108a7cce5SAlexander Motin i, CTLBLK_MAX_SEGS)); 1602130f4520SKenneth D. Merry 1603130f4520SKenneth D. Merry /* 1604130f4520SKenneth D. Merry * Setup the S/G entry for this chunk. 1605130f4520SKenneth D. Merry */ 16068054320eSAlexander Motin ctl_alloc_seg(softc, &beio->sg_segs[i], 1607cd853791SKonstantin Belousov MIN(CTLBLK_MAX_SEG, len_left)); 1608130f4520SKenneth D. Merry 1609130f4520SKenneth D. Merry DPRINTF("segment %d addr %p len %zd\n", i, 1610130f4520SKenneth D. Merry beio->sg_segs[i].addr, beio->sg_segs[i].len); 1611130f4520SKenneth D. Merry 161211b569f7SAlexander Motin /* Set up second segment for compare operation. */ 16130d7fed74SAlexander Motin if (beio->two_sglists) { 16148054320eSAlexander Motin ctl_alloc_seg(softc, 16158054320eSAlexander Motin &beio->sg_segs[i + CTLBLK_HALF_SEGS], 16168054320eSAlexander Motin beio->sg_segs[i].len); 161711b569f7SAlexander Motin } 161811b569f7SAlexander Motin 1619130f4520SKenneth D. Merry beio->num_segs++; 1620130f4520SKenneth D. Merry len_left -= beio->sg_segs[i].len; 1621130f4520SKenneth D. Merry } 1622e86a4142SAlexander Motin if (bptrlen->len < lbalen->len) 162308a7cce5SAlexander Motin beio->beio_cont = ctl_be_block_next; 162408a7cce5SAlexander Motin io->scsiio.be_move_done = ctl_be_block_move_done; 162511b569f7SAlexander Motin /* For compare we have separate S/G lists for read and datamove. */ 16260d7fed74SAlexander Motin if (beio->two_sglists) 162711b569f7SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 162811b569f7SAlexander Motin else 162908a7cce5SAlexander Motin io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 163008a7cce5SAlexander Motin io->scsiio.kern_data_len = beio->io_len; 163108a7cce5SAlexander Motin io->scsiio.kern_sg_entries = beio->num_segs; 16329a4510acSAlexander Motin io->scsiio.kern_data_ref = ctl_refcnt_beio; 16339a4510acSAlexander Motin io->scsiio.kern_data_arg = beio; 1634b2221369SAlexander Motin io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 1635130f4520SKenneth D. Merry 1636130f4520SKenneth D. Merry /* 1637130f4520SKenneth D. Merry * For the read case, we need to read the data into our buffers and 1638130f4520SKenneth D. Merry * then we can send it back to the user. For the write case, we 1639130f4520SKenneth D. Merry * need to get the data from the user first. 1640130f4520SKenneth D. Merry */ 1641130f4520SKenneth D. Merry if (beio->bio_cmd == BIO_READ) { 164236160958SMark Johnston SDT_PROBE0(cbb, , read, alloc_done); 1643130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1644130f4520SKenneth D. Merry } else { 164536160958SMark Johnston SDT_PROBE0(cbb, , write, alloc_done); 1646130f4520SKenneth D. Merry ctl_datamove(io); 1647130f4520SKenneth D. Merry } 1648130f4520SKenneth D. Merry } 1649130f4520SKenneth D. Merry 1650130f4520SKenneth D. Merry static void 1651130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending) 1652130f4520SKenneth D. Merry { 1653ee4ad294SAlexander Motin struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1654ee4ad294SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1655130f4520SKenneth D. Merry union ctl_io *io; 1656130f4520SKenneth D. Merry struct ctl_be_block_io *beio; 1657130f4520SKenneth D. Merry 1658ee4ad294SAlexander Motin DPRINTF("entered\n"); 1659ee4ad294SAlexander Motin /* 1660ee4ad294SAlexander Motin * Fetch and process I/Os from all queues. If we detect LUN 1661648dfc1aSAlexander Motin * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race, 1662ee4ad294SAlexander Motin * so make response maximally opaque to not confuse initiator. 1663ee4ad294SAlexander Motin */ 1664ee4ad294SAlexander Motin for (;;) { 1665ee4ad294SAlexander Motin mtx_lock(&be_lun->queue_lock); 1666ee4ad294SAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1667ee4ad294SAlexander Motin if (io != NULL) { 1668130f4520SKenneth D. Merry DPRINTF("datamove queue\n"); 166905d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links); 167075c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1671e86a4142SAlexander Motin beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1672648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1673ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1674ee4ad294SAlexander Motin ctl_complete_beio(beio); 1675ee4ad294SAlexander Motin return; 1676ee4ad294SAlexander Motin } 1677130f4520SKenneth D. Merry be_lun->dispatch(be_lun, beio); 1678130f4520SKenneth D. Merry continue; 1679130f4520SKenneth D. Merry } 1680130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1681130f4520SKenneth D. Merry if (io != NULL) { 1682130f4520SKenneth D. Merry DPRINTF("config write queue\n"); 168305d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links); 168475c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1685648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1686ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1687ee4ad294SAlexander Motin ctl_config_write_done(io); 1688ee4ad294SAlexander Motin return; 1689ee4ad294SAlexander Motin } 1690130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(be_lun, io); 1691ef8daf3fSAlexander Motin continue; 1692ef8daf3fSAlexander Motin } 1693ef8daf3fSAlexander Motin io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1694ef8daf3fSAlexander Motin if (io != NULL) { 1695ef8daf3fSAlexander Motin DPRINTF("config read queue\n"); 169605d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links); 1697ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 1698648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1699ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1700ee4ad294SAlexander Motin ctl_config_read_done(io); 1701ee4ad294SAlexander Motin return; 1702ee4ad294SAlexander Motin } 1703ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(be_lun, io); 1704130f4520SKenneth D. Merry continue; 1705130f4520SKenneth D. Merry } 1706130f4520SKenneth D. Merry io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1707130f4520SKenneth D. Merry if (io != NULL) { 1708130f4520SKenneth D. Merry DPRINTF("input queue\n"); 170905d882b7SAlexander Motin STAILQ_REMOVE_HEAD(&be_lun->input_queue, links); 171075c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1711648dfc1aSAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1712ee4ad294SAlexander Motin ctl_set_busy(&io->scsiio); 1713ee4ad294SAlexander Motin ctl_data_submit_done(io); 1714ee4ad294SAlexander Motin return; 1715ee4ad294SAlexander Motin } 1716130f4520SKenneth D. Merry ctl_be_block_dispatch(be_lun, io); 1717130f4520SKenneth D. Merry continue; 1718130f4520SKenneth D. Merry } 1719130f4520SKenneth D. Merry 1720130f4520SKenneth D. Merry /* 1721130f4520SKenneth D. Merry * If we get here, there is no work left in the queues, so 1722130f4520SKenneth D. Merry * just break out and let the task queue go to sleep. 1723130f4520SKenneth D. Merry */ 1724ee4ad294SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1725130f4520SKenneth D. Merry break; 1726130f4520SKenneth D. Merry } 1727130f4520SKenneth D. Merry } 1728130f4520SKenneth D. Merry 1729130f4520SKenneth D. Merry /* 1730130f4520SKenneth D. Merry * Entry point from CTL to the backend for I/O. We queue everything to a 1731130f4520SKenneth D. Merry * work thread, so this just puts the I/O on a queue and wakes up the 1732130f4520SKenneth D. Merry * thread. 1733130f4520SKenneth D. Merry */ 1734130f4520SKenneth D. Merry static int 1735130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io) 1736130f4520SKenneth D. Merry { 1737130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 1738130f4520SKenneth D. Merry 1739130f4520SKenneth D. Merry DPRINTF("entered\n"); 1740130f4520SKenneth D. Merry 1741767300e8SAlexander Motin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 1742130f4520SKenneth D. Merry 174305d882b7SAlexander Motin KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 174405d882b7SAlexander Motin ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type)); 1745130f4520SKenneth D. Merry 1746e86a4142SAlexander Motin PRIV(io)->len = 0; 1747e86a4142SAlexander Motin 174875c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 1749130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 175075c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 1751130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1752130f4520SKenneth D. Merry 17539c71cd5aSAlexander Motin return (CTL_RETVAL_COMPLETE); 1754130f4520SKenneth D. Merry } 1755130f4520SKenneth D. Merry 1756130f4520SKenneth D. Merry static int 1757130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1758130f4520SKenneth D. Merry int flag, struct thread *td) 1759130f4520SKenneth D. Merry { 176034144c2cSAlexander Motin struct ctl_be_block_softc *softc = &backend_block_softc; 1761130f4520SKenneth D. Merry int error; 1762130f4520SKenneth D. Merry 1763130f4520SKenneth D. Merry error = 0; 1764130f4520SKenneth D. Merry switch (cmd) { 1765130f4520SKenneth D. Merry case CTL_LUN_REQ: { 1766130f4520SKenneth D. Merry struct ctl_lun_req *lun_req; 1767130f4520SKenneth D. Merry 1768130f4520SKenneth D. Merry lun_req = (struct ctl_lun_req *)addr; 1769130f4520SKenneth D. Merry 1770130f4520SKenneth D. Merry switch (lun_req->reqtype) { 1771130f4520SKenneth D. Merry case CTL_LUNREQ_CREATE: 1772130f4520SKenneth D. Merry error = ctl_be_block_create(softc, lun_req); 1773130f4520SKenneth D. Merry break; 1774130f4520SKenneth D. Merry case CTL_LUNREQ_RM: 1775130f4520SKenneth D. Merry error = ctl_be_block_rm(softc, lun_req); 1776130f4520SKenneth D. Merry break; 177781177295SEdward Tomasz Napierala case CTL_LUNREQ_MODIFY: 177881177295SEdward Tomasz Napierala error = ctl_be_block_modify(softc, lun_req); 177981177295SEdward Tomasz Napierala break; 1780130f4520SKenneth D. Merry default: 1781130f4520SKenneth D. Merry lun_req->status = CTL_LUN_ERROR; 1782130f4520SKenneth D. Merry snprintf(lun_req->error_str, sizeof(lun_req->error_str), 178319720f41SAlexander Motin "invalid LUN request type %d", 1784130f4520SKenneth D. Merry lun_req->reqtype); 1785130f4520SKenneth D. Merry break; 1786130f4520SKenneth D. Merry } 1787130f4520SKenneth D. Merry break; 1788130f4520SKenneth D. Merry } 1789130f4520SKenneth D. Merry default: 1790130f4520SKenneth D. Merry error = ENOTTY; 1791130f4520SKenneth D. Merry break; 1792130f4520SKenneth D. Merry } 1793130f4520SKenneth D. Merry 1794130f4520SKenneth D. Merry return (error); 1795130f4520SKenneth D. Merry } 1796130f4520SKenneth D. Merry 1797130f4520SKenneth D. Merry static int 1798130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1799130f4520SKenneth D. Merry { 18000bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 1801130f4520SKenneth D. Merry struct ctl_be_block_filedata *file_data; 1802130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 18038951f055SMarcelo Araujo const char *value; 1804130f4520SKenneth D. Merry struct vattr vattr; 180534961f40SAlexander Motin off_t ps, pss, po, pos, us, uss, uo, uos; 1806130f4520SKenneth D. Merry int error; 1807130f4520SKenneth D. Merry 18080bcd4ab6SAlexander Motin cbe_lun = &be_lun->cbe_lun; 1809130f4520SKenneth D. Merry file_data = &be_lun->backend.file; 181019720f41SAlexander Motin params = &be_lun->params; 1811130f4520SKenneth D. Merry 1812130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_FILE; 1813130f4520SKenneth D. Merry be_lun->dispatch = ctl_be_block_dispatch_file; 1814130f4520SKenneth D. Merry be_lun->lun_flush = ctl_be_block_flush_file; 1815ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_file; 181653c146deSAlexander Motin be_lun->getattr = ctl_be_block_getattr_file; 18170bcd4ab6SAlexander Motin be_lun->unmap = NULL; 18180bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1819130f4520SKenneth D. Merry 1820130f4520SKenneth D. Merry error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1821130f4520SKenneth D. Merry if (error != 0) { 1822130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1823130f4520SKenneth D. Merry "error calling VOP_GETATTR() for file %s", 1824130f4520SKenneth D. Merry be_lun->dev_path); 1825130f4520SKenneth D. Merry return (error); 1826130f4520SKenneth D. Merry } 1827130f4520SKenneth D. Merry 1828130f4520SKenneth D. Merry file_data->cred = crhold(curthread->td_ucred); 182981177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) 183081177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 183181177295SEdward Tomasz Napierala else 1832130f4520SKenneth D. Merry be_lun->size_bytes = vattr.va_size; 1833130f4520SKenneth D. Merry 1834130f4520SKenneth D. Merry /* 183520a28d6cSAlexander Motin * For files we can use any logical block size. Prefer 512 bytes 183620a28d6cSAlexander Motin * for compatibility reasons. If file's vattr.va_blocksize 183720a28d6cSAlexander Motin * (preferred I/O block size) is bigger and multiple to chosen 183820a28d6cSAlexander Motin * logical block size -- report it as physical block size. 1839130f4520SKenneth D. Merry */ 1840130f4520SKenneth D. Merry if (params->blocksize_bytes != 0) 18410bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 184291be33dcSAlexander Motin else if (cbe_lun->lun_type == T_CDROM) 184391be33dcSAlexander Motin cbe_lun->blocksize = 2048; 1844130f4520SKenneth D. Merry else 18450bcd4ab6SAlexander Motin cbe_lun->blocksize = 512; 18460bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 18470bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 18480bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 184934961f40SAlexander Motin 185034961f40SAlexander Motin us = ps = vattr.va_blocksize; 185134961f40SAlexander Motin uo = po = 0; 185234961f40SAlexander Motin 18538951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL); 185434961f40SAlexander Motin if (value != NULL) 185534961f40SAlexander Motin ctl_expand_number(value, &ps); 18568951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL); 185734961f40SAlexander Motin if (value != NULL) 185834961f40SAlexander Motin ctl_expand_number(value, &po); 18590bcd4ab6SAlexander Motin pss = ps / cbe_lun->blocksize; 18600bcd4ab6SAlexander Motin pos = po / cbe_lun->blocksize; 18610bcd4ab6SAlexander Motin if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 18620bcd4ab6SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 18630bcd4ab6SAlexander Motin cbe_lun->pblockexp = fls(pss) - 1; 18640bcd4ab6SAlexander Motin cbe_lun->pblockoff = (pss - pos) % pss; 186534961f40SAlexander Motin } 186634961f40SAlexander Motin 18678951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL); 186834961f40SAlexander Motin if (value != NULL) 186934961f40SAlexander Motin ctl_expand_number(value, &us); 18708951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL); 187134961f40SAlexander Motin if (value != NULL) 187234961f40SAlexander Motin ctl_expand_number(value, &uo); 18730bcd4ab6SAlexander Motin uss = us / cbe_lun->blocksize; 18740bcd4ab6SAlexander Motin uos = uo / cbe_lun->blocksize; 18750bcd4ab6SAlexander Motin if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 18760bcd4ab6SAlexander Motin ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 18770bcd4ab6SAlexander Motin cbe_lun->ublockexp = fls(uss) - 1; 18780bcd4ab6SAlexander Motin cbe_lun->ublockoff = (uss - uos) % uss; 187920a28d6cSAlexander Motin } 1880130f4520SKenneth D. Merry 1881130f4520SKenneth D. Merry /* 1882130f4520SKenneth D. Merry * Sanity check. The media size has to be at least one 1883130f4520SKenneth D. Merry * sector long. 1884130f4520SKenneth D. Merry */ 18850bcd4ab6SAlexander Motin if (be_lun->size_bytes < cbe_lun->blocksize) { 1886130f4520SKenneth D. Merry error = EINVAL; 1887130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 1888130f4520SKenneth D. Merry "file %s size %ju < block size %u", be_lun->dev_path, 18890bcd4ab6SAlexander Motin (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1890130f4520SKenneth D. Merry } 1891cb8727e2SAlexander Motin 18920bcd4ab6SAlexander Motin cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1893130f4520SKenneth D. Merry return (error); 1894130f4520SKenneth D. Merry } 1895130f4520SKenneth D. Merry 1896130f4520SKenneth D. Merry static int 1897130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1898130f4520SKenneth D. Merry { 18990bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1900130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 19013236151eSAlexander Motin struct cdevsw *csw; 1902130f4520SKenneth D. Merry struct cdev *dev; 19038951f055SMarcelo Araujo const char *value; 19043236151eSAlexander Motin int error, atomic, maxio, ref, unmap, tmp; 1905f6295033SAlexander Motin off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1906130f4520SKenneth D. Merry 190719720f41SAlexander Motin params = &be_lun->params; 1908130f4520SKenneth D. Merry 1909130f4520SKenneth D. Merry be_lun->dev_type = CTL_BE_BLOCK_DEV; 19103236151eSAlexander Motin csw = devvn_refthread(be_lun->vn, &dev, &ref); 19113236151eSAlexander Motin if (csw == NULL) 19123236151eSAlexander Motin return (ENXIO); 19133236151eSAlexander Motin if (strcmp(csw->d_name, "zvol") == 0) { 191467f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_zvol; 1915ef8daf3fSAlexander Motin be_lun->get_lba_status = ctl_be_block_gls_zvol; 1916cb8727e2SAlexander Motin atomic = maxio = CTLBLK_MAX_IO_SIZE; 1917cb8727e2SAlexander Motin } else { 191867f586a8SAlexander Motin be_lun->dispatch = ctl_be_block_dispatch_dev; 19190bcd4ab6SAlexander Motin be_lun->get_lba_status = NULL; 1920cb8727e2SAlexander Motin atomic = 0; 19213236151eSAlexander Motin maxio = dev->si_iosize_max; 1922cb8727e2SAlexander Motin if (maxio <= 0) 1923cb8727e2SAlexander Motin maxio = DFLTPHYS; 19248054320eSAlexander Motin if (maxio > CTLBLK_MAX_SEG) 19258054320eSAlexander Motin maxio = CTLBLK_MAX_SEG; 1926cb8727e2SAlexander Motin } 192767f586a8SAlexander Motin be_lun->lun_flush = ctl_be_block_flush_dev; 1928c3e7ba3eSAlexander Motin be_lun->getattr = ctl_be_block_getattr_dev; 19290bcd4ab6SAlexander Motin be_lun->unmap = ctl_be_block_unmap_dev; 1930130f4520SKenneth D. Merry 19313236151eSAlexander Motin if (!csw->d_ioctl) { 19323236151eSAlexander Motin dev_relthread(dev, ref); 1933130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 19343236151eSAlexander Motin "no d_ioctl for device %s!", be_lun->dev_path); 1935130f4520SKenneth D. Merry return (ENODEV); 1936130f4520SKenneth D. Merry } 1937130f4520SKenneth D. Merry 19383236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1939130f4520SKenneth D. Merry curthread); 1940130f4520SKenneth D. Merry if (error) { 19413236151eSAlexander Motin dev_relthread(dev, ref); 1942130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 194319720f41SAlexander Motin "error %d returned for DIOCGSECTORSIZE ioctl " 194419720f41SAlexander Motin "on %s!", error, be_lun->dev_path); 1945130f4520SKenneth D. Merry return (error); 1946130f4520SKenneth D. Merry } 1947130f4520SKenneth D. Merry 1948130f4520SKenneth D. Merry /* 1949130f4520SKenneth D. Merry * If the user has asked for a blocksize that is greater than the 1950130f4520SKenneth D. Merry * backing device's blocksize, we can do it only if the blocksize 1951130f4520SKenneth D. Merry * the user is asking for is an even multiple of the underlying 1952130f4520SKenneth D. Merry * device's blocksize. 1953130f4520SKenneth D. Merry */ 1954a15bbf15SAlexander Motin if ((params->blocksize_bytes != 0) && 1955a15bbf15SAlexander Motin (params->blocksize_bytes >= tmp)) { 1956a15bbf15SAlexander Motin if (params->blocksize_bytes % tmp == 0) { 19570bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 1958130f4520SKenneth D. Merry } else { 19593236151eSAlexander Motin dev_relthread(dev, ref); 1960130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 196119720f41SAlexander Motin "requested blocksize %u is not an even " 1962130f4520SKenneth D. Merry "multiple of backing device blocksize %u", 1963f6295033SAlexander Motin params->blocksize_bytes, tmp); 1964130f4520SKenneth D. Merry return (EINVAL); 1965130f4520SKenneth D. Merry } 1966a15bbf15SAlexander Motin } else if (params->blocksize_bytes != 0) { 19673236151eSAlexander Motin dev_relthread(dev, ref); 1968130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 196919720f41SAlexander Motin "requested blocksize %u < backing device " 1970f6295033SAlexander Motin "blocksize %u", params->blocksize_bytes, tmp); 1971130f4520SKenneth D. Merry return (EINVAL); 197291be33dcSAlexander Motin } else if (cbe_lun->lun_type == T_CDROM) 197391be33dcSAlexander Motin cbe_lun->blocksize = MAX(tmp, 2048); 197491be33dcSAlexander Motin else 19750bcd4ab6SAlexander Motin cbe_lun->blocksize = tmp; 1976130f4520SKenneth D. Merry 19773236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1978130f4520SKenneth D. Merry curthread); 1979130f4520SKenneth D. Merry if (error) { 19803236151eSAlexander Motin dev_relthread(dev, ref); 1981130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 198219720f41SAlexander Motin "error %d returned for DIOCGMEDIASIZE " 198319720f41SAlexander Motin " ioctl on %s!", error, 198481177295SEdward Tomasz Napierala be_lun->dev_path); 1985130f4520SKenneth D. Merry return (error); 1986130f4520SKenneth D. Merry } 1987130f4520SKenneth D. Merry 198881177295SEdward Tomasz Napierala if (params->lun_size_bytes != 0) { 1989f6295033SAlexander Motin if (params->lun_size_bytes > otmp) { 19903236151eSAlexander Motin dev_relthread(dev, ref); 199181177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 199219720f41SAlexander Motin "requested LUN size %ju > backing device " 199319720f41SAlexander Motin "size %ju", 199481177295SEdward Tomasz Napierala (uintmax_t)params->lun_size_bytes, 1995f6295033SAlexander Motin (uintmax_t)otmp); 199681177295SEdward Tomasz Napierala return (EINVAL); 1997130f4520SKenneth D. Merry } 1998130f4520SKenneth D. Merry 199981177295SEdward Tomasz Napierala be_lun->size_bytes = params->lun_size_bytes; 2000a15bbf15SAlexander Motin } else 2001f6295033SAlexander Motin be_lun->size_bytes = otmp; 20020bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 20030bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 20040bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 200581177295SEdward Tomasz Napierala 20063236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 20073236151eSAlexander Motin curthread); 2008f6012722SAlexander Motin if (error) 2009f6012722SAlexander Motin ps = po = 0; 2010f6012722SAlexander Motin else { 20113236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 20123236151eSAlexander Motin FREAD, curthread); 2013f6012722SAlexander Motin if (error) 2014f6012722SAlexander Motin po = 0; 2015f6012722SAlexander Motin } 201634961f40SAlexander Motin us = ps; 201734961f40SAlexander Motin uo = po; 201834961f40SAlexander Motin 20198951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL); 202034961f40SAlexander Motin if (value != NULL) 202134961f40SAlexander Motin ctl_expand_number(value, &ps); 20228951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL); 202334961f40SAlexander Motin if (value != NULL) 202434961f40SAlexander Motin ctl_expand_number(value, &po); 20250bcd4ab6SAlexander Motin pss = ps / cbe_lun->blocksize; 20260bcd4ab6SAlexander Motin pos = po / cbe_lun->blocksize; 20270bcd4ab6SAlexander Motin if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 20280bcd4ab6SAlexander Motin ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 20290bcd4ab6SAlexander Motin cbe_lun->pblockexp = fls(pss) - 1; 20300bcd4ab6SAlexander Motin cbe_lun->pblockoff = (pss - pos) % pss; 2031f6012722SAlexander Motin } 2032f6012722SAlexander Motin 20338951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL); 203434961f40SAlexander Motin if (value != NULL) 203534961f40SAlexander Motin ctl_expand_number(value, &us); 20368951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL); 203734961f40SAlexander Motin if (value != NULL) 203834961f40SAlexander Motin ctl_expand_number(value, &uo); 20390bcd4ab6SAlexander Motin uss = us / cbe_lun->blocksize; 20400bcd4ab6SAlexander Motin uos = uo / cbe_lun->blocksize; 20410bcd4ab6SAlexander Motin if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 20420bcd4ab6SAlexander Motin ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 20430bcd4ab6SAlexander Motin cbe_lun->ublockexp = fls(uss) - 1; 20440bcd4ab6SAlexander Motin cbe_lun->ublockoff = (uss - uos) % uss; 204534961f40SAlexander Motin } 204634961f40SAlexander Motin 20470bcd4ab6SAlexander Motin cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 20480bcd4ab6SAlexander Motin cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2049fbc8d4ffSAlexander Motin 2050fbc8d4ffSAlexander Motin if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2051fbc8d4ffSAlexander Motin unmap = 1; 2052fbc8d4ffSAlexander Motin } else { 2053fbc8d4ffSAlexander Motin struct diocgattr_arg arg; 2054fbc8d4ffSAlexander Motin 2055fbc8d4ffSAlexander Motin strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2056fbc8d4ffSAlexander Motin arg.len = sizeof(arg.value.i); 20573236151eSAlexander Motin error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 20583236151eSAlexander Motin curthread); 2059fbc8d4ffSAlexander Motin unmap = (error == 0) ? arg.value.i : 0; 2060fbc8d4ffSAlexander Motin } 20618951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "unmap", NULL); 2062fbc8d4ffSAlexander Motin if (value != NULL) 2063fbc8d4ffSAlexander Motin unmap = (strcmp(value, "on") == 0); 2064fbc8d4ffSAlexander Motin if (unmap) 20650bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 20660bcd4ab6SAlexander Motin else 20670bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2068fbc8d4ffSAlexander Motin 20693236151eSAlexander Motin dev_relthread(dev, ref); 207081177295SEdward Tomasz Napierala return (0); 207181177295SEdward Tomasz Napierala } 2072130f4520SKenneth D. Merry 2073130f4520SKenneth D. Merry static int 2074130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun) 2075130f4520SKenneth D. Merry { 20760bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 20770bcd4ab6SAlexander Motin int flags; 2078130f4520SKenneth D. Merry 20790bcd4ab6SAlexander Motin if (be_lun->vn) { 20800bcd4ab6SAlexander Motin flags = FREAD; 20810bcd4ab6SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 20820bcd4ab6SAlexander Motin flags |= FWRITE; 2083130f4520SKenneth D. Merry (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2084130f4520SKenneth D. Merry be_lun->vn = NULL; 2085130f4520SKenneth D. Merry 2086130f4520SKenneth D. Merry switch (be_lun->dev_type) { 2087130f4520SKenneth D. Merry case CTL_BE_BLOCK_DEV: 2088130f4520SKenneth D. Merry break; 2089130f4520SKenneth D. Merry case CTL_BE_BLOCK_FILE: 2090130f4520SKenneth D. Merry if (be_lun->backend.file.cred != NULL) { 2091130f4520SKenneth D. Merry crfree(be_lun->backend.file.cred); 2092130f4520SKenneth D. Merry be_lun->backend.file.cred = NULL; 2093130f4520SKenneth D. Merry } 2094130f4520SKenneth D. Merry break; 2095130f4520SKenneth D. Merry case CTL_BE_BLOCK_NONE: 2096025a2301SEdward Tomasz Napierala break; 2097130f4520SKenneth D. Merry default: 20985124012aSAlexander Motin panic("Unexpected backend type %d", be_lun->dev_type); 2099130f4520SKenneth D. Merry break; 2100130f4520SKenneth D. Merry } 210119720f41SAlexander Motin be_lun->dev_type = CTL_BE_BLOCK_NONE; 2102130f4520SKenneth D. Merry } 2103130f4520SKenneth D. Merry return (0); 2104130f4520SKenneth D. Merry } 2105130f4520SKenneth D. Merry 2106130f4520SKenneth D. Merry static int 2107648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2108130f4520SKenneth D. Merry { 21090bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2110130f4520SKenneth D. Merry struct nameidata nd; 21118951f055SMarcelo Araujo const char *value; 21120bcd4ab6SAlexander Motin int error, flags; 2113130f4520SKenneth D. Merry 2114130f4520SKenneth D. Merry error = 0; 2115130f4520SKenneth D. Merry if (rootvnode == NULL) { 2116130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 211719720f41SAlexander Motin "Root filesystem is not mounted"); 2118130f4520SKenneth D. Merry return (1); 2119130f4520SKenneth D. Merry } 21208a08cec1SMateusz Guzik pwd_ensure_dirs(); 2121130f4520SKenneth D. Merry 21228951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "file", NULL); 21230bcd4ab6SAlexander Motin if (value == NULL) { 21240bcd4ab6SAlexander Motin snprintf(req->error_str, sizeof(req->error_str), 21250bcd4ab6SAlexander Motin "no file argument specified"); 21260bcd4ab6SAlexander Motin return (1); 21270bcd4ab6SAlexander Motin } 21280bcd4ab6SAlexander Motin free(be_lun->dev_path, M_CTLBLK); 21290bcd4ab6SAlexander Motin be_lun->dev_path = strdup(value, M_CTLBLK); 21300bcd4ab6SAlexander Motin 21310bcd4ab6SAlexander Motin flags = FREAD; 21328951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "readonly", NULL); 213391be33dcSAlexander Motin if (value != NULL) { 213491be33dcSAlexander Motin if (strcmp(value, "on") != 0) 213591be33dcSAlexander Motin flags |= FWRITE; 213691be33dcSAlexander Motin } else if (cbe_lun->lun_type == T_DIRECT) 21370bcd4ab6SAlexander Motin flags |= FWRITE; 21380bcd4ab6SAlexander Motin 2139130f4520SKenneth D. Merry again: 2140130f4520SKenneth D. Merry NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2141130f4520SKenneth D. Merry error = vn_open(&nd, &flags, 0, NULL); 21420bcd4ab6SAlexander Motin if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 21430bcd4ab6SAlexander Motin flags &= ~FWRITE; 21440bcd4ab6SAlexander Motin goto again; 21450bcd4ab6SAlexander Motin } 2146130f4520SKenneth D. Merry if (error) { 2147130f4520SKenneth D. Merry /* 2148130f4520SKenneth D. Merry * This is the only reasonable guess we can make as far as 2149130f4520SKenneth D. Merry * path if the user doesn't give us a fully qualified path. 2150130f4520SKenneth D. Merry * If they want to specify a file, they need to specify the 2151130f4520SKenneth D. Merry * full path. 2152130f4520SKenneth D. Merry */ 2153130f4520SKenneth D. Merry if (be_lun->dev_path[0] != '/') { 2154130f4520SKenneth D. Merry char *dev_name; 2155130f4520SKenneth D. Merry 21560bcd4ab6SAlexander Motin asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2157130f4520SKenneth D. Merry be_lun->dev_path); 2158130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 2159130f4520SKenneth D. Merry be_lun->dev_path = dev_name; 2160130f4520SKenneth D. Merry goto again; 2161130f4520SKenneth D. Merry } 2162130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 216319720f41SAlexander Motin "error opening %s: %d", be_lun->dev_path, error); 2164130f4520SKenneth D. Merry return (error); 2165130f4520SKenneth D. Merry } 21660bcd4ab6SAlexander Motin if (flags & FWRITE) 21670bcd4ab6SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 21680bcd4ab6SAlexander Motin else 21690bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2170130f4520SKenneth D. Merry 2171130f4520SKenneth D. Merry NDFREE(&nd, NDF_ONLY_PNBUF); 2172130f4520SKenneth D. Merry be_lun->vn = nd.ni_vp; 2173130f4520SKenneth D. Merry 2174130f4520SKenneth D. Merry /* We only support disks and files. */ 21757ad2a82dSMateusz Guzik if (vn_isdisk_error(be_lun->vn, &error)) { 2176130f4520SKenneth D. Merry error = ctl_be_block_open_dev(be_lun, req); 2177130f4520SKenneth D. Merry } else if (be_lun->vn->v_type == VREG) { 2178130f4520SKenneth D. Merry error = ctl_be_block_open_file(be_lun, req); 2179130f4520SKenneth D. Merry } else { 2180130f4520SKenneth D. Merry error = EINVAL; 2181130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 2182025a2301SEdward Tomasz Napierala "%s is not a disk or plain file", be_lun->dev_path); 2183130f4520SKenneth D. Merry } 2184b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 2185130f4520SKenneth D. Merry 2186a15bbf15SAlexander Motin if (error != 0) 2187130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 21880bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 21890bcd4ab6SAlexander Motin if (be_lun->dispatch != ctl_be_block_dispatch_dev) 21900bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 21918951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "serseq", NULL); 21920bcd4ab6SAlexander Motin if (value != NULL && strcmp(value, "on") == 0) 21930bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 21940bcd4ab6SAlexander Motin else if (value != NULL && strcmp(value, "read") == 0) 21950bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 21960bcd4ab6SAlexander Motin else if (value != NULL && strcmp(value, "off") == 0) 21970bcd4ab6SAlexander Motin cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2198130f4520SKenneth D. Merry return (0); 2199130f4520SKenneth D. Merry } 2200130f4520SKenneth D. Merry 2201130f4520SKenneth D. Merry static int 2202130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2203130f4520SKenneth D. Merry { 22040bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 2205130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2206130f4520SKenneth D. Merry struct ctl_lun_create_params *params; 220757a5db13SAlexander Motin char num_thread_str[16]; 2208130f4520SKenneth D. Merry char tmpstr[32]; 22098951f055SMarcelo Araujo const char *value; 2210fbc8d4ffSAlexander Motin int retval, num_threads; 221157a5db13SAlexander Motin int tmp_num_threads; 2212130f4520SKenneth D. Merry 2213130f4520SKenneth D. Merry params = &req->reqdata.create; 2214130f4520SKenneth D. Merry retval = 0; 221519720f41SAlexander Motin req->status = CTL_LUN_OK; 2216130f4520SKenneth D. Merry 2217130f4520SKenneth D. Merry be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 22180bcd4ab6SAlexander Motin cbe_lun = &be_lun->cbe_lun; 221919720f41SAlexander Motin be_lun->params = req->reqdata.create; 2220130f4520SKenneth D. Merry be_lun->softc = softc; 2221130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->input_queue); 2222ef8daf3fSAlexander Motin STAILQ_INIT(&be_lun->config_read_queue); 2223130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->config_write_queue); 2224130f4520SKenneth D. Merry STAILQ_INIT(&be_lun->datamove_queue); 222534144c2cSAlexander Motin mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF); 222634144c2cSAlexander Motin mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF); 22278951f055SMarcelo Araujo cbe_lun->options = nvlist_clone(req->args_nvl); 2228130f4520SKenneth D. Merry 2229130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 22300bcd4ab6SAlexander Motin cbe_lun->lun_type = params->device_type; 2231130f4520SKenneth D. Merry else 22320bcd4ab6SAlexander Motin cbe_lun->lun_type = T_DIRECT; 223334144c2cSAlexander Motin be_lun->flags = 0; 22347ac58230SAlexander Motin cbe_lun->flags = 0; 22358951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 22367ac58230SAlexander Motin if (value != NULL) { 22377ac58230SAlexander Motin if (strcmp(value, "primary") == 0) 22387ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 22397ac58230SAlexander Motin } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 22407ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2241130f4520SKenneth D. Merry 224291be33dcSAlexander Motin if (cbe_lun->lun_type == T_DIRECT || 224391be33dcSAlexander Motin cbe_lun->lun_type == T_CDROM) { 2244a15bbf15SAlexander Motin be_lun->size_bytes = params->lun_size_bytes; 2245a15bbf15SAlexander Motin if (params->blocksize_bytes != 0) 22460bcd4ab6SAlexander Motin cbe_lun->blocksize = params->blocksize_bytes; 224791be33dcSAlexander Motin else if (cbe_lun->lun_type == T_CDROM) 224891be33dcSAlexander Motin cbe_lun->blocksize = 2048; 2249a15bbf15SAlexander Motin else 22500bcd4ab6SAlexander Motin cbe_lun->blocksize = 512; 22510bcd4ab6SAlexander Motin be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 22520bcd4ab6SAlexander Motin cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 22530bcd4ab6SAlexander Motin 0 : (be_lun->size_blocks - 1); 2254130f4520SKenneth D. Merry 22557ac58230SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 22567ac58230SAlexander Motin control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2257648dfc1aSAlexander Motin retval = ctl_be_block_open(be_lun, req); 2258130f4520SKenneth D. Merry if (retval != 0) { 2259130f4520SKenneth D. Merry retval = 0; 226019720f41SAlexander Motin req->status = CTL_LUN_WARNING; 2261130f4520SKenneth D. Merry } 22627ac58230SAlexander Motin } 22630bcd4ab6SAlexander Motin num_threads = cbb_num_threads; 2264130f4520SKenneth D. Merry } else { 2265130f4520SKenneth D. Merry num_threads = 1; 2266130f4520SKenneth D. Merry } 2267130f4520SKenneth D. Merry 22688951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL); 226957a5db13SAlexander Motin if (value != NULL) { 227057a5db13SAlexander Motin tmp_num_threads = strtol(value, NULL, 0); 2271130f4520SKenneth D. Merry 2272130f4520SKenneth D. Merry /* 2273130f4520SKenneth D. Merry * We don't let the user specify less than one 2274130f4520SKenneth D. Merry * thread, but hope he's clueful enough not to 2275130f4520SKenneth D. Merry * specify 1000 threads. 2276130f4520SKenneth D. Merry */ 2277130f4520SKenneth D. Merry if (tmp_num_threads < 1) { 2278130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 227919720f41SAlexander Motin "invalid number of threads %s", 228019720f41SAlexander Motin num_thread_str); 2281130f4520SKenneth D. Merry goto bailout_error; 2282130f4520SKenneth D. Merry } 2283130f4520SKenneth D. Merry num_threads = tmp_num_threads; 228457a5db13SAlexander Motin } 2285130f4520SKenneth D. Merry 228619720f41SAlexander Motin if (be_lun->vn == NULL) 2287648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2288130f4520SKenneth D. Merry /* Tell the user the blocksize we ended up using */ 228919720f41SAlexander Motin params->lun_size_bytes = be_lun->size_bytes; 22900bcd4ab6SAlexander Motin params->blocksize_bytes = cbe_lun->blocksize; 2291130f4520SKenneth D. Merry if (params->flags & CTL_LUN_FLAG_ID_REQ) { 22920bcd4ab6SAlexander Motin cbe_lun->req_lun_id = params->req_lun_id; 22930bcd4ab6SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2294130f4520SKenneth D. Merry } else 22950bcd4ab6SAlexander Motin cbe_lun->req_lun_id = 0; 2296130f4520SKenneth D. Merry 22970bcd4ab6SAlexander Motin cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 22980bcd4ab6SAlexander Motin cbe_lun->be = &ctl_be_block_driver; 2299130f4520SKenneth D. Merry 2300130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 230171cd87c6SAlan Somers snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d", 2302130f4520SKenneth D. Merry softc->num_luns); 23030bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->serial_num, tmpstr, 23040bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2305130f4520SKenneth D. Merry 2306130f4520SKenneth D. Merry /* Tell the user what we used for a serial number */ 2307130f4520SKenneth D. Merry strncpy((char *)params->serial_num, tmpstr, 2308e7038eb7SAlexander Motin MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2309130f4520SKenneth D. Merry } else { 23100bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->serial_num, params->serial_num, 23110bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->serial_num), 2312130f4520SKenneth D. Merry sizeof(params->serial_num))); 2313130f4520SKenneth D. Merry } 2314130f4520SKenneth D. Merry if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 231571cd87c6SAlan Somers snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns); 23160bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->device_id, tmpstr, 23170bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2318130f4520SKenneth D. Merry 2319130f4520SKenneth D. Merry /* Tell the user what we used for a device ID */ 2320130f4520SKenneth D. Merry strncpy((char *)params->device_id, tmpstr, 2321e7038eb7SAlexander Motin MIN(sizeof(params->device_id), sizeof(tmpstr))); 2322130f4520SKenneth D. Merry } else { 23230bcd4ab6SAlexander Motin strncpy((char *)cbe_lun->device_id, params->device_id, 23240bcd4ab6SAlexander Motin MIN(sizeof(cbe_lun->device_id), 2325130f4520SKenneth D. Merry sizeof(params->device_id))); 2326130f4520SKenneth D. Merry } 2327130f4520SKenneth D. Merry 2328130f4520SKenneth D. Merry TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2329130f4520SKenneth D. Merry 233034144c2cSAlexander Motin be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK, 2331130f4520SKenneth D. Merry taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2332130f4520SKenneth D. Merry 2333130f4520SKenneth D. Merry if (be_lun->io_taskqueue == NULL) { 2334130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 233519720f41SAlexander Motin "unable to create taskqueue"); 2336130f4520SKenneth D. Merry goto bailout_error; 2337130f4520SKenneth D. Merry } 2338130f4520SKenneth D. Merry 2339130f4520SKenneth D. Merry /* 2340130f4520SKenneth D. Merry * Note that we start the same number of threads by default for 2341130f4520SKenneth D. Merry * both the file case and the block device case. For the file 2342130f4520SKenneth D. Merry * case, we need multiple threads to allow concurrency, because the 2343130f4520SKenneth D. Merry * vnode interface is designed to be a blocking interface. For the 2344130f4520SKenneth D. Merry * block device case, ZFS zvols at least will block the caller's 2345130f4520SKenneth D. Merry * context in many instances, and so we need multiple threads to 2346130f4520SKenneth D. Merry * overcome that problem. Other block devices don't need as many 2347130f4520SKenneth D. Merry * threads, but they shouldn't cause too many problems. 2348130f4520SKenneth D. Merry * 2349130f4520SKenneth D. Merry * If the user wants to just have a single thread for a block 2350130f4520SKenneth D. Merry * device, he can specify that when the LUN is created, or change 2351130f4520SKenneth D. Merry * the tunable/sysctl to alter the default number of threads. 2352130f4520SKenneth D. Merry */ 235312373e95SAlexander Motin retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue, 2354130f4520SKenneth D. Merry /*num threads*/num_threads, 2355053db1feSAlexander Motin /*priority*/PUSER, 235612373e95SAlexander Motin /*proc*/control_softc->ctl_proc, 235734144c2cSAlexander Motin /*thread name*/"block"); 2358130f4520SKenneth D. Merry 2359130f4520SKenneth D. Merry if (retval != 0) 2360130f4520SKenneth D. Merry goto bailout_error; 2361130f4520SKenneth D. Merry 2362130f4520SKenneth D. Merry be_lun->num_threads = num_threads; 2363130f4520SKenneth D. Merry 23640bcd4ab6SAlexander Motin retval = ctl_add_lun(&be_lun->cbe_lun); 2365130f4520SKenneth D. Merry if (retval != 0) { 2366130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 236719720f41SAlexander Motin "ctl_add_lun() returned error %d, see dmesg for " 236819720f41SAlexander Motin "details", retval); 2369130f4520SKenneth D. Merry retval = 0; 2370130f4520SKenneth D. Merry goto bailout_error; 2371130f4520SKenneth D. Merry } 2372130f4520SKenneth D. Merry 237334144c2cSAlexander Motin be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id, 23740bcd4ab6SAlexander Motin cbe_lun->blocksize, 2375130f4520SKenneth D. Merry DEVSTAT_ALL_SUPPORTED, 23760bcd4ab6SAlexander Motin cbe_lun->lun_type 2377130f4520SKenneth D. Merry | DEVSTAT_TYPE_IF_OTHER, 2378130f4520SKenneth D. Merry DEVSTAT_PRIORITY_OTHER); 2379130f4520SKenneth D. Merry 238034144c2cSAlexander Motin mtx_lock(&softc->lock); 238134144c2cSAlexander Motin softc->num_luns++; 238234144c2cSAlexander Motin SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links); 238334144c2cSAlexander Motin mtx_unlock(&softc->lock); 238434144c2cSAlexander Motin 238534144c2cSAlexander Motin params->req_lun_id = cbe_lun->lun_id; 238634144c2cSAlexander Motin 2387130f4520SKenneth D. Merry return (retval); 2388130f4520SKenneth D. Merry 2389130f4520SKenneth D. Merry bailout_error: 2390130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2391130f4520SKenneth D. Merry 23929e005bbcSAlexander Motin if (be_lun->io_taskqueue != NULL) 23939e005bbcSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 2394130f4520SKenneth D. Merry ctl_be_block_close(be_lun); 23959e005bbcSAlexander Motin if (be_lun->dev_path != NULL) 2396130f4520SKenneth D. Merry free(be_lun->dev_path, M_CTLBLK); 23978951f055SMarcelo Araujo nvlist_destroy(cbe_lun->options); 239875c7a1d3SAlexander Motin mtx_destroy(&be_lun->queue_lock); 239975c7a1d3SAlexander Motin mtx_destroy(&be_lun->io_lock); 2400130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 2401130f4520SKenneth D. Merry 2402130f4520SKenneth D. Merry return (retval); 2403130f4520SKenneth D. Merry } 2404130f4520SKenneth D. Merry 2405130f4520SKenneth D. Merry static int 2406130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2407130f4520SKenneth D. Merry { 2408130f4520SKenneth D. Merry struct ctl_lun_rm_params *params; 2409130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 2410ee4ad294SAlexander Motin struct ctl_be_lun *cbe_lun; 2411130f4520SKenneth D. Merry int retval; 2412130f4520SKenneth D. Merry 2413130f4520SKenneth D. Merry params = &req->reqdata.rm; 2414130f4520SKenneth D. Merry 241534144c2cSAlexander Motin sx_xlock(&softc->modify_lock); 2416130f4520SKenneth D. Merry mtx_lock(&softc->lock); 241734144c2cSAlexander Motin SLIST_FOREACH(be_lun, &softc->lun_list, links) { 241834144c2cSAlexander Motin if (be_lun->cbe_lun.lun_id == params->lun_id) { 241934144c2cSAlexander Motin SLIST_REMOVE(&softc->lun_list, be_lun, 242034144c2cSAlexander Motin ctl_be_block_lun, links); 242134144c2cSAlexander Motin softc->num_luns--; 2422130f4520SKenneth D. Merry break; 2423130f4520SKenneth D. Merry } 242434144c2cSAlexander Motin } 2425130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 242634144c2cSAlexander Motin sx_xunlock(&softc->modify_lock); 2427130f4520SKenneth D. Merry if (be_lun == NULL) { 2428130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 242919720f41SAlexander Motin "LUN %u is not managed by the block backend", 243019720f41SAlexander Motin params->lun_id); 2431130f4520SKenneth D. Merry goto bailout_error; 2432130f4520SKenneth D. Merry } 2433ee4ad294SAlexander Motin cbe_lun = &be_lun->cbe_lun; 2434130f4520SKenneth D. Merry 2435ee4ad294SAlexander Motin if (be_lun->vn != NULL) { 2436648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2437648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 2438ee4ad294SAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 2439ee4ad294SAlexander Motin ctl_be_block_close(be_lun); 2440ee4ad294SAlexander Motin } 2441ee4ad294SAlexander Motin 244234144c2cSAlexander Motin mtx_lock(&softc->lock); 244334144c2cSAlexander Motin be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 244434144c2cSAlexander Motin mtx_unlock(&softc->lock); 244534144c2cSAlexander Motin 244634144c2cSAlexander Motin retval = ctl_remove_lun(cbe_lun); 2447130f4520SKenneth D. Merry if (retval != 0) { 2448130f4520SKenneth D. Merry snprintf(req->error_str, sizeof(req->error_str), 244934144c2cSAlexander Motin "error %d returned from ctl_remove_lun() for " 245019720f41SAlexander Motin "LUN %d", retval, params->lun_id); 245134144c2cSAlexander Motin mtx_lock(&softc->lock); 245234144c2cSAlexander Motin be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 245334144c2cSAlexander Motin mtx_unlock(&softc->lock); 2454130f4520SKenneth D. Merry goto bailout_error; 2455130f4520SKenneth D. Merry } 2456130f4520SKenneth D. Merry 2457130f4520SKenneth D. Merry mtx_lock(&softc->lock); 2458130f4520SKenneth D. Merry while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 245934144c2cSAlexander Motin retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0); 2460130f4520SKenneth D. Merry if (retval == EINTR) 2461130f4520SKenneth D. Merry break; 2462130f4520SKenneth D. Merry } 2463130f4520SKenneth D. Merry be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 246434144c2cSAlexander Motin if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2465130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2466130f4520SKenneth D. Merry free(be_lun, M_CTLBLK); 246734144c2cSAlexander Motin } else { 246834144c2cSAlexander Motin mtx_unlock(&softc->lock); 246934144c2cSAlexander Motin return (EINTR); 247034144c2cSAlexander Motin } 2471130f4520SKenneth D. Merry 2472130f4520SKenneth D. Merry req->status = CTL_LUN_OK; 2473130f4520SKenneth D. Merry return (0); 2474130f4520SKenneth D. Merry 2475130f4520SKenneth D. Merry bailout_error: 2476130f4520SKenneth D. Merry req->status = CTL_LUN_ERROR; 2477130f4520SKenneth D. Merry return (0); 2478130f4520SKenneth D. Merry } 2479130f4520SKenneth D. Merry 248081177295SEdward Tomasz Napierala static int 248181177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 248281177295SEdward Tomasz Napierala { 248381177295SEdward Tomasz Napierala struct ctl_lun_modify_params *params; 248481177295SEdward Tomasz Napierala struct ctl_be_block_lun *be_lun; 2485a3977beaSAlexander Motin struct ctl_be_lun *cbe_lun; 24868951f055SMarcelo Araujo const char *value; 248771d8e97eSAlexander Motin uint64_t oldsize; 24887ac58230SAlexander Motin int error, wasprim; 248981177295SEdward Tomasz Napierala 249081177295SEdward Tomasz Napierala params = &req->reqdata.modify; 249181177295SEdward Tomasz Napierala 249234144c2cSAlexander Motin sx_xlock(&softc->modify_lock); 249381177295SEdward Tomasz Napierala mtx_lock(&softc->lock); 249434144c2cSAlexander Motin SLIST_FOREACH(be_lun, &softc->lun_list, links) { 24950bcd4ab6SAlexander Motin if (be_lun->cbe_lun.lun_id == params->lun_id) 249681177295SEdward Tomasz Napierala break; 249781177295SEdward Tomasz Napierala } 249881177295SEdward Tomasz Napierala mtx_unlock(&softc->lock); 249981177295SEdward Tomasz Napierala if (be_lun == NULL) { 250081177295SEdward Tomasz Napierala snprintf(req->error_str, sizeof(req->error_str), 250119720f41SAlexander Motin "LUN %u is not managed by the block backend", 250219720f41SAlexander Motin params->lun_id); 250381177295SEdward Tomasz Napierala goto bailout_error; 250481177295SEdward Tomasz Napierala } 2505a3977beaSAlexander Motin cbe_lun = &be_lun->cbe_lun; 250681177295SEdward Tomasz Napierala 2507a3977beaSAlexander Motin if (params->lun_size_bytes != 0) 250819720f41SAlexander Motin be_lun->params.lun_size_bytes = params->lun_size_bytes; 25098951f055SMarcelo Araujo 2510efeedddcSAlexander Motin if (req->args_nvl != NULL) { 25118951f055SMarcelo Araujo nvlist_destroy(cbe_lun->options); 25128951f055SMarcelo Araujo cbe_lun->options = nvlist_clone(req->args_nvl); 2513efeedddcSAlexander Motin } 251481177295SEdward Tomasz Napierala 25157ac58230SAlexander Motin wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 25168951f055SMarcelo Araujo value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 25177ac58230SAlexander Motin if (value != NULL) { 25187ac58230SAlexander Motin if (strcmp(value, "primary") == 0) 25197ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 25207ac58230SAlexander Motin else 25217ac58230SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 25227ac58230SAlexander Motin } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 25237ac58230SAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 25247ac58230SAlexander Motin else 25257ac58230SAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 25267ac58230SAlexander Motin if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 25277ac58230SAlexander Motin if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 25287ac58230SAlexander Motin ctl_lun_primary(cbe_lun); 25297ac58230SAlexander Motin else 25307ac58230SAlexander Motin ctl_lun_secondary(cbe_lun); 25317ac58230SAlexander Motin } 25327ac58230SAlexander Motin 25330bcd4ab6SAlexander Motin oldsize = be_lun->size_blocks; 25347ac58230SAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 25357ac58230SAlexander Motin control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 253619720f41SAlexander Motin if (be_lun->vn == NULL) 2537648dfc1aSAlexander Motin error = ctl_be_block_open(be_lun, req); 25387ad2a82dSMateusz Guzik else if (vn_isdisk_error(be_lun->vn, &error)) 25394ce7a086SAlexander Motin error = ctl_be_block_open_dev(be_lun, req); 25403d5cb709SAlexander Motin else if (be_lun->vn->v_type == VREG) { 25413d5cb709SAlexander Motin vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 25424ce7a086SAlexander Motin error = ctl_be_block_open_file(be_lun, req); 2543b249ce48SMateusz Guzik VOP_UNLOCK(be_lun->vn); 25443d5cb709SAlexander Motin } else 2545b9b4269cSAlexander Motin error = EINVAL; 2546648dfc1aSAlexander Motin if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) && 25470bcd4ab6SAlexander Motin be_lun->vn != NULL) { 2548648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2549648dfc1aSAlexander Motin ctl_lun_has_media(cbe_lun); 2550648dfc1aSAlexander Motin } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 && 2551648dfc1aSAlexander Motin be_lun->vn == NULL) { 2552648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2553648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 255471d8e97eSAlexander Motin } 2555648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 25567ac58230SAlexander Motin } else { 25577ac58230SAlexander Motin if (be_lun->vn != NULL) { 2558648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2559648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 2560ee4ad294SAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 25617ac58230SAlexander Motin error = ctl_be_block_close(be_lun); 25627ac58230SAlexander Motin } else 25637ac58230SAlexander Motin error = 0; 25647ac58230SAlexander Motin } 25657ac58230SAlexander Motin if (be_lun->size_blocks != oldsize) 25667ac58230SAlexander Motin ctl_lun_capacity_changed(cbe_lun); 256781177295SEdward Tomasz Napierala 256881177295SEdward Tomasz Napierala /* Tell the user the exact size we ended up using */ 256981177295SEdward Tomasz Napierala params->lun_size_bytes = be_lun->size_bytes; 257081177295SEdward Tomasz Napierala 257134144c2cSAlexander Motin sx_xunlock(&softc->modify_lock); 257219720f41SAlexander Motin req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 257381177295SEdward Tomasz Napierala return (0); 257481177295SEdward Tomasz Napierala 257581177295SEdward Tomasz Napierala bailout_error: 257634144c2cSAlexander Motin sx_xunlock(&softc->modify_lock); 257781177295SEdward Tomasz Napierala req->status = CTL_LUN_ERROR; 257881177295SEdward Tomasz Napierala return (0); 257981177295SEdward Tomasz Napierala } 258081177295SEdward Tomasz Napierala 2581130f4520SKenneth D. Merry static void 2582767300e8SAlexander Motin ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun) 2583130f4520SKenneth D. Merry { 2584767300e8SAlexander Motin struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun; 258534144c2cSAlexander Motin struct ctl_be_block_softc *softc = be_lun->softc; 258634144c2cSAlexander Motin 258734144c2cSAlexander Motin taskqueue_drain_all(be_lun->io_taskqueue); 258834144c2cSAlexander Motin taskqueue_free(be_lun->io_taskqueue); 258934144c2cSAlexander Motin if (be_lun->disk_stats != NULL) 259034144c2cSAlexander Motin devstat_remove_entry(be_lun->disk_stats); 259134144c2cSAlexander Motin nvlist_destroy(be_lun->cbe_lun.options); 259234144c2cSAlexander Motin free(be_lun->dev_path, M_CTLBLK); 259334144c2cSAlexander Motin mtx_destroy(&be_lun->queue_lock); 259434144c2cSAlexander Motin mtx_destroy(&be_lun->io_lock); 2595130f4520SKenneth D. Merry 2596130f4520SKenneth D. Merry mtx_lock(&softc->lock); 259734144c2cSAlexander Motin be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 259834144c2cSAlexander Motin if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING) 259934144c2cSAlexander Motin wakeup(be_lun); 260034144c2cSAlexander Motin else 260134144c2cSAlexander Motin free(be_lun, M_CTLBLK); 2602130f4520SKenneth D. Merry mtx_unlock(&softc->lock); 2603130f4520SKenneth D. Merry } 2604130f4520SKenneth D. Merry 2605130f4520SKenneth D. Merry static int 2606130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io) 2607130f4520SKenneth D. Merry { 2608130f4520SKenneth D. Merry struct ctl_be_block_lun *be_lun; 26090bcd4ab6SAlexander Motin struct ctl_be_lun *cbe_lun; 2610130f4520SKenneth D. Merry int retval; 2611130f4520SKenneth D. Merry 2612130f4520SKenneth D. Merry DPRINTF("entered\n"); 2613130f4520SKenneth D. Merry 26149cbbfd2fSAlexander Motin cbe_lun = CTL_BACKEND_LUN(io); 2615767300e8SAlexander Motin be_lun = (struct ctl_be_block_lun *)cbe_lun; 2616130f4520SKenneth D. Merry 261767cc546dSAlexander Motin retval = 0; 2618130f4520SKenneth D. Merry switch (io->scsiio.cdb[0]) { 2619130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE: 2620130f4520SKenneth D. Merry case SYNCHRONIZE_CACHE_16: 2621ee7f31c0SAlexander Motin case WRITE_SAME_10: 2622ee7f31c0SAlexander Motin case WRITE_SAME_16: 2623ee7f31c0SAlexander Motin case UNMAP: 2624130f4520SKenneth D. Merry /* 2625130f4520SKenneth D. Merry * The upper level CTL code will filter out any CDBs with 2626130f4520SKenneth D. Merry * the immediate bit set and return the proper error. 2627130f4520SKenneth D. Merry * 2628130f4520SKenneth D. Merry * We don't really need to worry about what LBA range the 2629130f4520SKenneth D. Merry * user asked to be synced out. When they issue a sync 2630130f4520SKenneth D. Merry * cache command, we'll sync out the whole thing. 2631130f4520SKenneth D. Merry */ 263275c7a1d3SAlexander Motin mtx_lock(&be_lun->queue_lock); 2633130f4520SKenneth D. Merry STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2634130f4520SKenneth D. Merry links); 263575c7a1d3SAlexander Motin mtx_unlock(&be_lun->queue_lock); 2636130f4520SKenneth D. Merry taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2637130f4520SKenneth D. Merry break; 2638130f4520SKenneth D. Merry case START_STOP_UNIT: { 2639130f4520SKenneth D. Merry struct scsi_start_stop_unit *cdb; 2640648dfc1aSAlexander Motin struct ctl_lun_req req; 2641130f4520SKenneth D. Merry 2642130f4520SKenneth D. Merry cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 264366b69676SAlexander Motin if ((cdb->how & SSS_PC_MASK) != 0) { 264466b69676SAlexander Motin ctl_set_success(&io->scsiio); 264566b69676SAlexander Motin ctl_config_write_done(io); 264666b69676SAlexander Motin break; 264766b69676SAlexander Motin } 2648648dfc1aSAlexander Motin if (cdb->how & SSS_START) { 2649648dfc1aSAlexander Motin if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) { 2650648dfc1aSAlexander Motin retval = ctl_be_block_open(be_lun, &req); 2651648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 2652648dfc1aSAlexander Motin if (retval == 0) { 2653648dfc1aSAlexander Motin cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2654648dfc1aSAlexander Motin ctl_lun_has_media(cbe_lun); 2655130f4520SKenneth D. Merry } else { 2656648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2657648dfc1aSAlexander Motin ctl_lun_no_media(cbe_lun); 2658130f4520SKenneth D. Merry } 2659648dfc1aSAlexander Motin } 2660648dfc1aSAlexander Motin ctl_start_lun(cbe_lun); 2661648dfc1aSAlexander Motin } else { 2662648dfc1aSAlexander Motin ctl_stop_lun(cbe_lun); 2663648dfc1aSAlexander Motin if (cdb->how & SSS_LOEJ) { 2664648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2665648dfc1aSAlexander Motin cbe_lun->flags |= CTL_LUN_FLAG_EJECTED; 2666648dfc1aSAlexander Motin ctl_lun_ejected(cbe_lun); 2667648dfc1aSAlexander Motin if (be_lun->vn != NULL) 2668648dfc1aSAlexander Motin ctl_be_block_close(be_lun); 2669648dfc1aSAlexander Motin } 2670648dfc1aSAlexander Motin } 2671648dfc1aSAlexander Motin 2672648dfc1aSAlexander Motin ctl_set_success(&io->scsiio); 2673130f4520SKenneth D. Merry ctl_config_write_done(io); 2674130f4520SKenneth D. Merry break; 2675130f4520SKenneth D. Merry } 267691be33dcSAlexander Motin case PREVENT_ALLOW: 267791be33dcSAlexander Motin ctl_set_success(&io->scsiio); 267891be33dcSAlexander Motin ctl_config_write_done(io); 267991be33dcSAlexander Motin break; 2680130f4520SKenneth D. Merry default: 2681130f4520SKenneth D. Merry ctl_set_invalid_opcode(&io->scsiio); 2682130f4520SKenneth D. Merry ctl_config_write_done(io); 2683130f4520SKenneth D. Merry retval = CTL_RETVAL_COMPLETE; 2684130f4520SKenneth D. Merry break; 2685130f4520SKenneth D. Merry } 2686130f4520SKenneth D. Merry 2687130f4520SKenneth D. Merry return (retval); 2688130f4520SKenneth D. Merry } 2689130f4520SKenneth D. Merry 2690130f4520SKenneth D. Merry static int 2691130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io) 2692130f4520SKenneth D. Merry { 2693ef8daf3fSAlexander Motin struct ctl_be_block_lun *be_lun; 2694ef8daf3fSAlexander Motin int retval = 0; 2695ef8daf3fSAlexander Motin 2696ef8daf3fSAlexander Motin DPRINTF("entered\n"); 2697ef8daf3fSAlexander Motin 2698767300e8SAlexander Motin be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io); 2699ef8daf3fSAlexander Motin 2700ef8daf3fSAlexander Motin switch (io->scsiio.cdb[0]) { 2701ef8daf3fSAlexander Motin case SERVICE_ACTION_IN: 2702ef8daf3fSAlexander Motin if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2703ef8daf3fSAlexander Motin mtx_lock(&be_lun->queue_lock); 2704ef8daf3fSAlexander Motin STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2705ef8daf3fSAlexander Motin &io->io_hdr, links); 2706ef8daf3fSAlexander Motin mtx_unlock(&be_lun->queue_lock); 2707ef8daf3fSAlexander Motin taskqueue_enqueue(be_lun->io_taskqueue, 2708ef8daf3fSAlexander Motin &be_lun->io_task); 2709ef8daf3fSAlexander Motin retval = CTL_RETVAL_QUEUED; 2710ef8daf3fSAlexander Motin break; 2711ef8daf3fSAlexander Motin } 2712ef8daf3fSAlexander Motin ctl_set_invalid_field(&io->scsiio, 2713ef8daf3fSAlexander Motin /*sks_valid*/ 1, 2714ef8daf3fSAlexander Motin /*command*/ 1, 2715ef8daf3fSAlexander Motin /*field*/ 1, 2716ef8daf3fSAlexander Motin /*bit_valid*/ 1, 2717ef8daf3fSAlexander Motin /*bit*/ 4); 2718ef8daf3fSAlexander Motin ctl_config_read_done(io); 2719ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2720ef8daf3fSAlexander Motin break; 2721ef8daf3fSAlexander Motin default: 2722ef8daf3fSAlexander Motin ctl_set_invalid_opcode(&io->scsiio); 2723ef8daf3fSAlexander Motin ctl_config_read_done(io); 2724ef8daf3fSAlexander Motin retval = CTL_RETVAL_COMPLETE; 2725ef8daf3fSAlexander Motin break; 2726ef8daf3fSAlexander Motin } 2727ef8daf3fSAlexander Motin 2728ef8daf3fSAlexander Motin return (retval); 2729130f4520SKenneth D. Merry } 2730130f4520SKenneth D. Merry 2731130f4520SKenneth D. Merry static int 2732767300e8SAlexander Motin ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb) 2733130f4520SKenneth D. Merry { 2734767300e8SAlexander Motin struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun; 2735130f4520SKenneth D. Merry int retval; 2736130f4520SKenneth D. Merry 27372cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "\t<num_threads>"); 2738130f4520SKenneth D. Merry if (retval != 0) 2739130f4520SKenneth D. Merry goto bailout; 2740130f4520SKenneth D. Merry retval = sbuf_printf(sb, "%d", lun->num_threads); 2741130f4520SKenneth D. Merry if (retval != 0) 2742130f4520SKenneth D. Merry goto bailout; 27432cfbcb9bSAlexander Motin retval = sbuf_printf(sb, "</num_threads>\n"); 2744130f4520SKenneth D. Merry 2745130f4520SKenneth D. Merry bailout: 2746130f4520SKenneth D. Merry return (retval); 2747130f4520SKenneth D. Merry } 2748130f4520SKenneth D. Merry 2749c3e7ba3eSAlexander Motin static uint64_t 2750767300e8SAlexander Motin ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname) 2751c3e7ba3eSAlexander Motin { 2752767300e8SAlexander Motin struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun; 2753c3e7ba3eSAlexander Motin 2754c3e7ba3eSAlexander Motin if (lun->getattr == NULL) 2755c3e7ba3eSAlexander Motin return (UINT64_MAX); 2756c3e7ba3eSAlexander Motin return (lun->getattr(lun, attrname)); 2757c3e7ba3eSAlexander Motin } 2758c3e7ba3eSAlexander Motin 27590c629e28SAlexander Motin static int 2760130f4520SKenneth D. Merry ctl_be_block_init(void) 2761130f4520SKenneth D. Merry { 27620c629e28SAlexander Motin struct ctl_be_block_softc *softc = &backend_block_softc; 2763130f4520SKenneth D. Merry 276434144c2cSAlexander Motin sx_init(&softc->modify_lock, "ctlblock modify"); 276575c7a1d3SAlexander Motin mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 27660c629e28SAlexander Motin softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2767a0e36aeeSEdward Tomasz Napierala NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2768cd853791SKonstantin Belousov softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG, 27690d7fed74SAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2770cd853791SKonstantin Belousov if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG) 2771cd853791SKonstantin Belousov softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG, 27728054320eSAlexander Motin NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 277334144c2cSAlexander Motin SLIST_INIT(&softc->lun_list); 27740c629e28SAlexander Motin return (0); 27750c629e28SAlexander Motin } 2776130f4520SKenneth D. Merry 27770c629e28SAlexander Motin static int 27780c629e28SAlexander Motin ctl_be_block_shutdown(void) 27790c629e28SAlexander Motin { 27800c629e28SAlexander Motin struct ctl_be_block_softc *softc = &backend_block_softc; 278134144c2cSAlexander Motin struct ctl_be_block_lun *lun; 27820c629e28SAlexander Motin 27830c629e28SAlexander Motin mtx_lock(&softc->lock); 278434144c2cSAlexander Motin while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) { 278534144c2cSAlexander Motin SLIST_REMOVE_HEAD(&softc->lun_list, links); 278634144c2cSAlexander Motin softc->num_luns--; 27870c629e28SAlexander Motin /* 278834144c2cSAlexander Motin * Drop our lock here. Since ctl_remove_lun() can call 27890c629e28SAlexander Motin * back into us, this could potentially lead to a recursive 27900c629e28SAlexander Motin * lock of the same mutex, which would cause a hang. 27910c629e28SAlexander Motin */ 27920c629e28SAlexander Motin mtx_unlock(&softc->lock); 279334144c2cSAlexander Motin ctl_remove_lun(&lun->cbe_lun); 27940c629e28SAlexander Motin mtx_lock(&softc->lock); 27950c629e28SAlexander Motin } 27960c629e28SAlexander Motin mtx_unlock(&softc->lock); 2797cd853791SKonstantin Belousov uma_zdestroy(softc->bufmin_zone); 2798cd853791SKonstantin Belousov if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG) 2799cd853791SKonstantin Belousov uma_zdestroy(softc->bufmax_zone); 28000c629e28SAlexander Motin uma_zdestroy(softc->beio_zone); 28010c629e28SAlexander Motin mtx_destroy(&softc->lock); 280234144c2cSAlexander Motin sx_destroy(&softc->modify_lock); 28030c629e28SAlexander Motin return (0); 2804130f4520SKenneth D. Merry } 2805