ctl_backend_block.c revision 287221
137446Srnordier/*- 237446Srnordier * Copyright (c) 2003 Silicon Graphics International Corp. 337446Srnordier * Copyright (c) 2009-2011 Spectra Logic Corporation 437446Srnordier * Copyright (c) 2012 The FreeBSD Foundation 537446Srnordier * All rights reserved. 637446Srnordier * 737446Srnordier * Portions of this software were developed by Edward Tomasz Napierala 837446Srnordier * under sponsorship from the FreeBSD Foundation. 937446Srnordier * 1037446Srnordier * Redistribution and use in source and binary forms, with or without 1137446Srnordier * modification, are permitted provided that the following conditions 1237446Srnordier * are met: 1337446Srnordier * 1. Redistributions of source code must retain the above copyright 1437446Srnordier * notice, this list of conditions, and the following disclaimer, 1537446Srnordier * without modification. 1637446Srnordier * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1737446Srnordier * substantially similar to the "NO WARRANTY" disclaimer below 1837446Srnordier * ("Disclaimer") and any redistribution must be conditioned upon 1937446Srnordier * including a substantially similar Disclaimer requirement for further 2037446Srnordier * binary redistribution. 2137446Srnordier * 2237446Srnordier * NO WARRANTY 2337446Srnordier * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2437446Srnordier * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2537446Srnordier * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 2637446Srnordier * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2737446Srnordier * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2837446Srnordier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2937446Srnordier * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3050476Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 3137446Srnordier * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 3237446Srnordier * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3337446Srnordier * POSSIBILITY OF SUCH DAMAGES. 34106372Sscottl * 35106372Sscottl * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 3637446Srnordier */ 3737446Srnordier/* 3866907Swollman * CAM Target Layer driver backend for block devices. 3966907Swollman * 4037446Srnordier * Author: Ken Merry <ken@FreeBSD.org> 4137446Srnordier */ 4237446Srnordier#include <sys/cdefs.h> 4337446Srnordier__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 287221 2015-08-27 21:16:24Z mav $"); 4437446Srnordier 45185594Smlaier#include <sys/param.h> 4637446Srnordier#include <sys/systm.h> 4737446Srnordier#include <sys/kernel.h> 4837446Srnordier#include <sys/types.h> 4937446Srnordier#include <sys/kthread.h> 5066907Swollman#include <sys/bio.h> 5137446Srnordier#include <sys/fcntl.h> 5237446Srnordier#include <sys/limits.h> 5337446Srnordier#include <sys/lock.h> 5437446Srnordier#include <sys/mutex.h> 5537446Srnordier#include <sys/condvar.h> 5637446Srnordier#include <sys/malloc.h> 5737446Srnordier#include <sys/conf.h> 58170166Strhodes#include <sys/ioccom.h> 5937446Srnordier#include <sys/queue.h> 6037446Srnordier#include <sys/sbuf.h> 6137446Srnordier#include <sys/endian.h> 6237446Srnordier#include <sys/uio.h> 6337446Srnordier#include <sys/buf.h> 6437446Srnordier#include <sys/taskqueue.h> 65203868Skib#include <sys/vnode.h> 66203868Skib#include <sys/namei.h> 67203868Skib#include <sys/mount.h> 68203868Skib#include <sys/disk.h> 69203868Skib#include <sys/fcntl.h> 70203868Skib#include <sys/filedesc.h> 7137446Srnordier#include <sys/filio.h> 7237446Srnordier#include <sys/proc.h> 7337446Srnordier#include <sys/pcpu.h> 7437446Srnordier#include <sys/module.h> 7537446Srnordier#include <sys/sdt.h> 7637446Srnordier#include <sys/devicestat.h> 7737446Srnordier#include <sys/sysctl.h> 7837446Srnordier 7937446Srnordier#include <geom/geom.h> 8037446Srnordier 8137446Srnordier#include <cam/cam.h> 8237446Srnordier#include <cam/scsi/scsi_all.h> 8337446Srnordier#include <cam/scsi/scsi_da.h> 8437446Srnordier#include <cam/ctl/ctl_io.h> 8537446Srnordier#include <cam/ctl/ctl.h> 8637446Srnordier#include <cam/ctl/ctl_backend.h> 8737446Srnordier#include <cam/ctl/ctl_ioctl.h> 8837446Srnordier#include <cam/ctl/ctl_scsi_all.h> 8937446Srnordier#include <cam/ctl/ctl_error.h> 9037446Srnordier 9137446Srnordier/* 9237446Srnordier * The idea here is that we'll allocate enough S/G space to hold a 1MB 9337446Srnordier * I/O. If we get an I/O larger than that, we'll split it. 9437446Srnordier */ 9537446Srnordier#define CTLBLK_HALF_IO_SIZE (512 * 1024) 9637446Srnordier#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 9737446Srnordier#define CTLBLK_MAX_SEG MAXPHYS 9837446Srnordier#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 99203869Skib#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 100203869Skib 10137446Srnordier#ifdef CTLBLK_DEBUG 10237446Srnordier#define DPRINTF(fmt, args...) \ 10337446Srnordier printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 104203869Skib#else 105203869Skib#define DPRINTF(fmt, args...) do {} while(0) 106203869Skib#endif 107203869Skib 108203869Skib#define PRIV(io) \ 109203869Skib ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 110203869Skib#define ARGS(io) \ 111203869Skib ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 112203869Skib 113203869SkibSDT_PROVIDER_DEFINE(cbb); 114203869Skib 115203869Skibtypedef enum { 11637446Srnordier CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 11737446Srnordier CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 11837446Srnordier CTL_BE_BLOCK_LUN_WAITING = 0x04, 119203869Skib CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 120203869Skib} ctl_be_block_lun_flags; 121203869Skib 122203869Skibtypedef enum { 123203869Skib CTL_BE_BLOCK_NONE, 124203869Skib CTL_BE_BLOCK_DEV, 125203869Skib CTL_BE_BLOCK_FILE 12637446Srnordier} ctl_be_block_type; 12737446Srnordier 12837446Srnordierstruct ctl_be_block_devdata { 129203869Skib struct cdev *cdev; 130203869Skib struct cdevsw *csw; 131203869Skib int dev_ref; 132203869Skib}; 133203869Skib 134203869Skibstruct ctl_be_block_filedata { 13537446Srnordier struct ucred *cred; 13637446Srnordier}; 13737446Srnordier 138203869Skibunion ctl_be_block_bedata { 139203869Skib struct ctl_be_block_devdata dev; 14037446Srnordier struct ctl_be_block_filedata file; 141203869Skib}; 142203869Skib 143203869Skibstruct ctl_be_block_io; 144203869Skibstruct ctl_be_block_lun; 14537446Srnordier 14637446Srnordiertypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 14737446Srnordier struct ctl_be_block_io *beio); 148203869Skibtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 149203869Skib const char *attrname); 150203869Skib 151203869Skib/* 152203869Skib * Backend LUN structure. There is a 1:1 mapping between a block device 153203869Skib * and a backend block LUN, and between a backend block LUN and a CTL LUN. 154203869Skib */ 155203869Skibstruct ctl_be_block_lun { 156203869Skib struct ctl_lun_create_params params; 157203869Skib struct ctl_block_disk *disk; 158203869Skib char lunname[32]; 159203869Skib char *dev_path; 160203869Skib ctl_be_block_type dev_type; 161203869Skib struct vnode *vn; 162203869Skib union ctl_be_block_bedata backend; 163203869Skib cbb_dispatch_t dispatch; 16437446Srnordier cbb_dispatch_t lun_flush; 16537446Srnordier cbb_dispatch_t unmap; 166140384Sdelphij cbb_dispatch_t get_lba_status; 167140384Sdelphij cbb_getattr_t getattr; 168203868Skib uma_zone_t lun_zone; 169203869Skib uint64_t size_blocks; 170203869Skib uint64_t size_bytes; 17137446Srnordier uint32_t blocksize; 17237446Srnordier uint16_t pblockexp; 17337446Srnordier uint16_t pblockoff; 174190927Sed uint16_t ublockexp; 175203868Skib uint16_t ublockoff; 176203868Skib uint32_t atomicblock; 177203868Skib uint32_t opttxferlen; 178203868Skib struct ctl_be_block_softc *softc; 179203868Skib struct devstat *disk_stats; 180203868Skib ctl_be_block_lun_flags flags; 181203868Skib STAILQ_ENTRY(ctl_be_block_lun) links; 182203868Skib struct ctl_be_lun ctl_be_lun; 183203868Skib struct taskqueue *io_taskqueue; 184203868Skib struct task io_task; 18537446Srnordier int num_threads; 18637446Srnordier STAILQ_HEAD(, ctl_io_hdr) input_queue; 187190927Sed STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 18837446Srnordier STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 18937446Srnordier STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 19037446Srnordier struct mtx_padalign io_lock; 19137446Srnordier struct mtx_padalign queue_lock; 19237446Srnordier}; 19337446Srnordier 19437446Srnordier/* 19537446Srnordier * Overall softc structure for the block backend module. 19637446Srnordier */ 19737446Srnordierstruct ctl_be_block_softc { 19837446Srnordier struct mtx lock; 19937446Srnordier int num_disks; 20037446Srnordier STAILQ_HEAD(, ctl_block_disk) disk_list; 20137446Srnordier int num_luns; 20237446Srnordier STAILQ_HEAD(, ctl_be_block_lun) lun_list; 20337446Srnordier}; 20437446Srnordier 20537446Srnordierstatic struct ctl_be_block_softc backend_block_softc; 20637446Srnordier 20737446Srnordier/* 20837446Srnordier * Per-I/O information. 20937446Srnordier */ 21037446Srnordierstruct ctl_be_block_io { 21137446Srnordier union ctl_io *io; 21237446Srnordier struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 21337446Srnordier struct iovec xiovecs[CTLBLK_MAX_SEGS]; 21437446Srnordier int bio_cmd; 21537446Srnordier int num_segs; 21637446Srnordier int num_bios_sent; 21737446Srnordier int num_bios_done; 21837446Srnordier int send_complete; 21937446Srnordier int num_errors; 22037446Srnordier struct bintime ds_t0; 22137446Srnordier devstat_tag_type ds_tag_type; 22237446Srnordier devstat_trans_flags ds_trans_type; 22337446Srnordier uint64_t io_len; 22437446Srnordier uint64_t io_offset; 22537446Srnordier int io_arg; 226185587Sluigi struct ctl_be_block_softc *softc; 22737446Srnordier struct ctl_be_block_lun *lun; 22837446Srnordier void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 22937446Srnordier}; 23037446Srnordier 23137446Srnordierstatic int cbb_num_threads = 14; 23237446SrnordierSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 233102231Strhodes "CAM Target Layer Block Backend"); 23437446SrnordierSYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 23537446Srnordier &cbb_num_threads, 0, "Number of threads per backing file"); 23637446Srnordier 23737446Srnordierstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 238190927Sedstatic void ctl_free_beio(struct ctl_be_block_io *beio); 239190927Sedstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 240190927Sedstatic int ctl_be_block_move_done(union ctl_io *io); 241190927Sedstatic void ctl_be_block_biodone(struct bio *bio); 242190927Sedstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 243190927Sed struct ctl_be_block_io *beio); 244190927Sedstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 24537446Srnordier struct ctl_be_block_io *beio); 24637446Srnordierstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 24737446Srnordier struct ctl_be_block_io *beio); 24837446Srnordierstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 24937446Srnordier const char *attrname); 25037446Srnordierstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 25137446Srnordier struct ctl_be_block_io *beio); 25237446Srnordierstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 25337446Srnordier struct ctl_be_block_io *beio); 25437446Srnordierstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 25537446Srnordier struct ctl_be_block_io *beio); 25637446Srnordierstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 25737446Srnordier const char *attrname); 25840487Sbdestatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 25937446Srnordier union ctl_io *io); 26037446Srnordierstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 261190927Sed union ctl_io *io); 26237446Srnordierstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 26337446Srnordier union ctl_io *io); 26437446Srnordierstatic void ctl_be_block_worker(void *context, int pending); 265185587Sluigistatic int ctl_be_block_submit(union ctl_io *io); 266185587Sluigistatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 267185587Sluigi int flag, struct thread *td); 26837446Srnordierstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 26937446Srnordier struct ctl_lun_req *req); 27037446Srnordierstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 27137446Srnordier struct ctl_lun_req *req); 27237446Srnordierstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 27337446Srnordierstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 274185587Sluigi struct ctl_be_block_lun *be_lun, 275185587Sluigi struct ctl_lun_req *req); 276185587Sluigistatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 27737446Srnordier struct ctl_lun_req *req); 27837446Srnordierstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 27937446Srnordier struct ctl_lun_req *req); 28037446Srnordierstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 28137446Srnordier struct ctl_lun_req *req); 28237446Srnordierstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 28337446Srnordier struct ctl_lun_req *req); 28437446Srnordierstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 28537446Srnordier struct ctl_lun_req *req); 28637446Srnordierstatic void ctl_be_block_lun_shutdown(void *be_lun); 28737446Srnordierstatic void ctl_be_block_lun_config_status(void *be_lun, 28837446Srnordier ctl_lun_config_status status); 28937446Srnordierstatic int ctl_be_block_config_write(union ctl_io *io); 29037446Srnordierstatic int ctl_be_block_config_read(union ctl_io *io); 29137446Srnordierstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 29237446Srnordierstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 29337446Srnordierint ctl_be_block_init(void); 29437446Srnordier 29537446Srnordierstatic struct ctl_backend_driver ctl_be_block_driver = 29637446Srnordier{ 29737446Srnordier .name = "block", 29837446Srnordier .flags = CTL_BE_FLAG_HAS_CONFIG, 29937446Srnordier .init = ctl_be_block_init, 30037446Srnordier .data_submit = ctl_be_block_submit, 30137446Srnordier .data_move_done = ctl_be_block_move_done, 30237446Srnordier .config_read = ctl_be_block_config_read, 30337446Srnordier .config_write = ctl_be_block_config_write, 30437446Srnordier .ioctl = ctl_be_block_ioctl, 30537446Srnordier .lun_info = ctl_be_block_lun_info, 30637446Srnordier .lun_attr = ctl_be_block_lun_attr 30737446Srnordier}; 30837446Srnordier 30937446SrnordierMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 31037446SrnordierCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 31137446Srnordier 31237446Srnordierstatic uma_zone_t beio_zone; 31337446Srnordier 31437446Srnordierstatic struct ctl_be_block_io * 31537446Srnordierctl_alloc_beio(struct ctl_be_block_softc *softc) 31637446Srnordier{ 31737446Srnordier struct ctl_be_block_io *beio; 31837446Srnordier 31937446Srnordier beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 32037446Srnordier beio->softc = softc; 32137446Srnordier return (beio); 32237446Srnordier} 32337446Srnordier 32437446Srnordierstatic void 32537446Srnordierctl_free_beio(struct ctl_be_block_io *beio) 32637446Srnordier{ 32737446Srnordier int duplicate_free; 32837446Srnordier int i; 32937446Srnordier 33037446Srnordier duplicate_free = 0; 33137446Srnordier 33237446Srnordier for (i = 0; i < beio->num_segs; i++) { 33337446Srnordier if (beio->sg_segs[i].addr == NULL) 33437446Srnordier duplicate_free++; 33537446Srnordier 33637446Srnordier uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 33737446Srnordier beio->sg_segs[i].addr = NULL; 33837446Srnordier 33937446Srnordier /* For compare we had two equal S/G lists. */ 34037446Srnordier if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 34137446Srnordier uma_zfree(beio->lun->lun_zone, 342102231Strhodes beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 34337446Srnordier beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 34437446Srnordier } 34537446Srnordier } 34637446Srnordier 34737446Srnordier if (duplicate_free > 0) { 34837446Srnordier printf("%s: %d duplicate frees out of %d segments\n", __func__, 34937446Srnordier duplicate_free, beio->num_segs); 35037446Srnordier } 35137446Srnordier 35237446Srnordier uma_zfree(beio_zone, beio); 35337446Srnordier} 35437446Srnordier 355190932Sedstatic void 35663892Sasmodaictl_complete_beio(struct ctl_be_block_io *beio) 35737446Srnordier{ 35837446Srnordier union ctl_io *io = beio->io; 35937446Srnordier 36037446Srnordier if (beio->beio_cont != NULL) { 361185587Sluigi beio->beio_cont(beio); 362185587Sluigi } else { 363185587Sluigi ctl_free_beio(beio); 364185587Sluigi ctl_data_submit_done(io); 365185587Sluigi } 366185587Sluigi} 367190929Sed 368185594Smlaierstatic int 369190930Sedctl_be_block_move_done(union ctl_io *io) 37037446Srnordier{ 371190930Sed struct ctl_be_block_io *beio; 372190930Sed struct ctl_be_block_lun *be_lun; 373190931Sed struct ctl_lba_len_flags *lbalen; 374190931Sed#ifdef CTL_TIME_IO 375190931Sed struct bintime cur_bt; 376190931Sed#endif 377190931Sed int i; 378190931Sed 379190931Sed beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 38037446Srnordier be_lun = beio->lun; 38137446Srnordier 382185587Sluigi DPRINTF("entered\n"); 383185594Smlaier 38437446Srnordier#ifdef CTL_TIME_IO 38537446Srnordier getbintime(&cur_bt); 38637446Srnordier bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 387203869Skib bintime_add(&io->io_hdr.dma_bt, &cur_bt); 388203869Skib io->io_hdr.num_dmas++; 389203869Skib#endif 390203869Skib io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 39137446Srnordier 39237446Srnordier /* 393203869Skib * We set status at this point for read commands, and write 39437446Srnordier * commands with errors. 395203869Skib */ 39637446Srnordier if (io->io_hdr.flags & CTL_FLAG_ABORT) { 397203869Skib ; 39837446Srnordier } else if ((io->io_hdr.port_status == 0) && 399203869Skib ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 40037446Srnordier lbalen = ARGS(beio->io); 401203869Skib if (lbalen->flags & CTL_LLF_READ) { 402185587Sluigi ctl_set_success(&io->scsiio); 403185587Sluigi } else if (lbalen->flags & CTL_LLF_COMPARE) { 40437446Srnordier /* We have two data blocks ready for comparison. */ 405203869Skib for (i = 0; i < beio->num_segs; i++) { 406203869Skib if (memcmp(beio->sg_segs[i].addr, 407185587Sluigi beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 408185587Sluigi beio->sg_segs[i].len) != 0) 409203869Skib break; 410203869Skib } 411185587Sluigi if (i < beio->num_segs) 412203869Skib ctl_set_sense(&io->scsiio, 413203869Skib /*current_error*/ 1, 414203869Skib /*sense_key*/ SSD_KEY_MISCOMPARE, 415203869Skib /*asc*/ 0x1D, 416203869Skib /*ascq*/ 0x00, 417203869Skib SSD_ELEM_NONE); 418203869Skib else 419203869Skib ctl_set_success(&io->scsiio); 420203869Skib } 421185587Sluigi } else if ((io->io_hdr.port_status != 0) && 422203869Skib ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 423185587Sluigi (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 424185587Sluigi /* 425203869Skib * For hardware error sense keys, the sense key 426203869Skib * specific value is defined to be a retry count, 427203869Skib * but we use it to pass back an internal FETD 42837446Srnordier * error code. XXX KDM Hopefully the FETD is only 429203869Skib * using 16 bits for an error code, since that's 43048954Sbillf * all the space we have in the sks field. 43137446Srnordier */ 43237446Srnordier ctl_set_internal_failure(&io->scsiio, 43337446Srnordier /*sks_valid*/ 1, 43437446Srnordier /*retry_count*/ 43548954Sbillf io->io_hdr.port_status); 43637446Srnordier } 43737446Srnordier 43837446Srnordier /* 43937446Srnordier * If this is a read, or a write with errors, it is done. 44037446Srnordier */ 441203869Skib if ((beio->bio_cmd == BIO_READ) 44237446Srnordier || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 44337446Srnordier || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 44437446Srnordier ctl_complete_beio(beio); 445203869Skib return (0); 44637446Srnordier } 447203869Skib 448203869Skib /* 44937446Srnordier * At this point, we have a write and the DMA completed 450203869Skib * successfully. We now have to queue it to the task queue to 451203869Skib * execute the backend I/O. That is because we do blocking 45237446Srnordier * memory allocations, and in the file backing case, blocking I/O. 45337446Srnordier * This move done routine is generally called in the SIM's 45437446Srnordier * interrupt context, and therefore we cannot block. 45537446Srnordier */ 456203869Skib mtx_lock(&be_lun->queue_lock); 45737446Srnordier /* 45837446Srnordier * XXX KDM make sure that links is okay to use at this point. 459203869Skib * Otherwise, we either need to add another field to ctl_io_hdr, 46037446Srnordier * or deal with resource allocation here. 46137446Srnordier */ 46237446Srnordier STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 46337446Srnordier mtx_unlock(&be_lun->queue_lock); 464203869Skib 46537446Srnordier taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 46637446Srnordier 467203869Skib return (0); 46837446Srnordier} 46937446Srnordier 47041586Srnordierstatic void 471203869Skibctl_be_block_biodone(struct bio *bio) 47237446Srnordier{ 47337446Srnordier struct ctl_be_block_io *beio; 474203869Skib struct ctl_be_block_lun *be_lun; 47537446Srnordier union ctl_io *io; 476203869Skib int error; 47737446Srnordier 478203869Skib beio = bio->bio_caller1; 47937446Srnordier be_lun = beio->lun; 48037446Srnordier io = beio->io; 48137446Srnordier 48237446Srnordier DPRINTF("entered\n"); 48337446Srnordier 48437446Srnordier error = bio->bio_error; 48542261Sjkh mtx_lock(&be_lun->io_lock); 48637446Srnordier if (error != 0) 48737446Srnordier beio->num_errors++; 48837446Srnordier 48937446Srnordier beio->num_bios_done++; 49037446Srnordier 491203869Skib /* 492203869Skib * XXX KDM will this cause WITNESS to complain? Holding a lock 49337446Srnordier * during the free might cause it to complain. 494203869Skib */ 49537446Srnordier g_destroy_bio(bio); 496203869Skib 497203869Skib /* 49848954Sbillf * If the send complete bit isn't set, or we aren't the last I/O to 499203869Skib * complete, then we're done. 500203869Skib */ 501203869Skib if ((beio->send_complete == 0) 502203869Skib || (beio->num_bios_done < beio->num_bios_sent)) { 503203869Skib mtx_unlock(&be_lun->io_lock); 504203869Skib return; 505203869Skib } 506203869Skib 50737446Srnordier /* 508203869Skib * At this point, we've verified that we are the last I/O to 509203869Skib * complete, so it's safe to drop the lock. 510203869Skib */ 511203869Skib devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 51237446Srnordier beio->ds_tag_type, beio->ds_trans_type, 513203869Skib /*now*/ NULL, /*then*/&beio->ds_t0); 51437446Srnordier mtx_unlock(&be_lun->io_lock); 51537446Srnordier 51637446Srnordier /* 51748954Sbillf * If there are any errors from the backing device, we fail the 51837446Srnordier * entire I/O with a medium error. 51937446Srnordier */ 520203869Skib if (beio->num_errors > 0) { 521203869Skib if (error == EOPNOTSUPP) { 52237446Srnordier ctl_set_invalid_opcode(&io->scsiio); 523203869Skib } else if (error == ENOSPC || error == EDQUOT) { 52437446Srnordier ctl_set_space_alloc_fail(&io->scsiio); 525203869Skib } else if (beio->bio_cmd == BIO_FLUSH) { 526203869Skib /* XXX KDM is there is a better error here? */ 527203869Skib ctl_set_internal_failure(&io->scsiio, 52837446Srnordier /*sks_valid*/ 1, 52937446Srnordier /*retry_count*/ 0xbad2); 530203869Skib } else 531203869Skib ctl_set_medium_error(&io->scsiio); 53237446Srnordier ctl_complete_beio(beio); 533203869Skib return; 534203869Skib } 53537446Srnordier 536203869Skib /* 537203869Skib * If this is a write, a flush, a delete or verify, we're all done. 538203869Skib * If this is a read, we can now send the data to the user. 539203869Skib */ 540203869Skib if ((beio->bio_cmd == BIO_WRITE) 541203869Skib || (beio->bio_cmd == BIO_FLUSH) 542203869Skib || (beio->bio_cmd == BIO_DELETE) 543203869Skib || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 544203869Skib ctl_set_success(&io->scsiio); 545203869Skib ctl_complete_beio(beio); 546203869Skib } else { 54737446Srnordier if ((ARGS(io)->flags & CTL_LLF_READ) && 548203869Skib beio->beio_cont == NULL) 54937446Srnordier ctl_set_success(&io->scsiio); 550203869Skib#ifdef CTL_TIME_IO 551203869Skib getbintime(&io->io_hdr.dma_start_bt); 552203869Skib#endif 55337446Srnordier ctl_datamove(io); 554203869Skib } 555203869Skib} 556203869Skib 557102231Strhodesstatic void 558203869Skibctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 559203869Skib struct ctl_be_block_io *beio) 560203869Skib{ 56137446Srnordier union ctl_io *io = beio->io; 562203869Skib struct mount *mountpoint; 563203869Skib int error, lock_flags; 564203869Skib 565203869Skib DPRINTF("entered\n"); 56637446Srnordier 567203869Skib binuptime(&beio->ds_t0); 568203869Skib mtx_lock(&be_lun->io_lock); 56937446Srnordier devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 57037446Srnordier mtx_unlock(&be_lun->io_lock); 571203869Skib 572102231Strhodes (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 57337446Srnordier 57437446Srnordier if (MNT_SHARED_WRITES(mountpoint) 57559215Simp || ((mountpoint == NULL) 57659215Simp && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 57737446Srnordier lock_flags = LK_SHARED; 57837446Srnordier else 579203869Skib lock_flags = LK_EXCLUSIVE; 580102231Strhodes 581203869Skib vn_lock(be_lun->vn, lock_flags | LK_RETRY); 58237446Srnordier 58337446Srnordier error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 584203869Skib curthread); 585203869Skib VOP_UNLOCK(be_lun->vn, 0); 586203869Skib 587203869Skib vn_finished_write(mountpoint); 588203869Skib 58937446Srnordier mtx_lock(&be_lun->io_lock); 590203869Skib devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 591203869Skib beio->ds_tag_type, beio->ds_trans_type, 592203869Skib /*now*/ NULL, /*then*/&beio->ds_t0); 593203869Skib mtx_unlock(&be_lun->io_lock); 59437446Srnordier 59537446Srnordier if (error == 0) 596203869Skib ctl_set_success(&io->scsiio); 597203869Skib else { 59837446Srnordier /* XXX KDM is there is a better error here? */ 59937446Srnordier ctl_set_internal_failure(&io->scsiio, 60037446Srnordier /*sks_valid*/ 1, 60137446Srnordier /*retry_count*/ 0xbad1); 60240487Sbde } 60340487Sbde 604203869Skib ctl_complete_beio(beio); 60537446Srnordier} 606203869Skib 607203869SkibSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 60837446SrnordierSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 60937446SrnordierSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 610203869SkibSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 611203869Skib 612203869Skibstatic void 613185587Sluigictl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 61437446Srnordier struct ctl_be_block_io *beio) 61537446Srnordier{ 61637446Srnordier struct ctl_be_block_filedata *file_data; 617203869Skib union ctl_io *io; 61837446Srnordier struct uio xuio; 619203869Skib struct iovec *xiovec; 62037446Srnordier int flags; 62137446Srnordier int error, i; 622203869Skib 62337446Srnordier DPRINTF("entered\n"); 624203869Skib 62537446Srnordier file_data = &be_lun->backend.file; 62637446Srnordier io = beio->io; 627203869Skib flags = 0; 628203869Skib if (ARGS(io)->flags & CTL_LLF_DPO) 629203869Skib flags |= IO_DIRECT; 630203869Skib if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 631203869Skib flags |= IO_SYNC; 632203869Skib 633203869Skib bzero(&xuio, sizeof(xuio)); 634203869Skib if (beio->bio_cmd == BIO_READ) { 635203869Skib SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 636203869Skib xuio.uio_rw = UIO_READ; 637203869Skib } else { 638203869Skib SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 63937446Srnordier xuio.uio_rw = UIO_WRITE; 64037446Srnordier } 64137446Srnordier xuio.uio_offset = beio->io_offset; 642203869Skib xuio.uio_resid = beio->io_len; 643203869Skib xuio.uio_segflg = UIO_SYSSPACE; 644203869Skib xuio.uio_iov = beio->xiovecs; 645203869Skib xuio.uio_iovcnt = beio->num_segs; 646203869Skib xuio.uio_td = curthread; 647203869Skib 64837446Srnordier for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 64937446Srnordier xiovec->iov_base = beio->sg_segs[i].addr; 65037446Srnordier xiovec->iov_len = beio->sg_segs[i].len; 651203869Skib } 65237446Srnordier 65337446Srnordier binuptime(&beio->ds_t0); 65437446Srnordier mtx_lock(&be_lun->io_lock); 65537446Srnordier devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 65637446Srnordier mtx_unlock(&be_lun->io_lock); 65737446Srnordier 65837446Srnordier if (beio->bio_cmd == BIO_READ) { 65937446Srnordier vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 66037446Srnordier 66137446Srnordier /* 662203869Skib * UFS pays attention to IO_DIRECT for reads. If the 663203869Skib * DIRECTIO option is configured into the kernel, it calls 66437446Srnordier * ffs_rawread(). But that only works for single-segment 665203869Skib * uios with user space addresses. In our case, with a 66637446Srnordier * kernel uio, it still reads into the buffer cache, but it 66737446Srnordier * will just try to release the buffer from the cache later 66837446Srnordier * on in ffs_read(). 669203869Skib * 670203869Skib * ZFS does not pay attention to IO_DIRECT for reads. 671203869Skib * 672203869Skib * UFS does not pay attention to IO_SYNC for reads. 673203869Skib * 67437446Srnordier * ZFS pays attention to IO_SYNC (which translates into the 675170166Strhodes * Solaris define FRSYNC for zfs_read()) for reads. It 67637446Srnordier * attempts to sync the file before reading. 677203869Skib */ 678203869Skib error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 679203869Skib 680203869Skib VOP_UNLOCK(be_lun->vn, 0); 68137446Srnordier SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 682170166Strhodes } else { 683170166Strhodes struct mount *mountpoint; 684203869Skib int lock_flags; 685170166Strhodes 686203869Skib (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 687203869Skib 688203869Skib if (MNT_SHARED_WRITES(mountpoint) 689203869Skib || ((mountpoint == NULL) 69037446Srnordier && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 69137446Srnordier lock_flags = LK_SHARED; 69237446Srnordier else 69337759Srnordier lock_flags = LK_EXCLUSIVE; 694203869Skib 695203869Skib vn_lock(be_lun->vn, lock_flags | LK_RETRY); 69637446Srnordier 69737446Srnordier /* 69837446Srnordier * UFS pays attention to IO_DIRECT for writes. The write 699203869Skib * is done asynchronously. (Normally the write would just 70037446Srnordier * get put into cache. 70137446Srnordier * 70237446Srnordier * UFS pays attention to IO_SYNC for writes. It will 703203869Skib * attempt to write the buffer out synchronously if that 70437446Srnordier * flag is set. 705203869Skib * 70637446Srnordier * ZFS does not pay attention to IO_DIRECT for writes. 707203869Skib * 70837446Srnordier * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 70937446Srnordier * for writes. It will flush the transaction from the 71037446Srnordier * cache before returning. 71137446Srnordier */ 71237446Srnordier error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 71337446Srnordier VOP_UNLOCK(be_lun->vn, 0); 71437446Srnordier 715102231Strhodes vn_finished_write(mountpoint); 71637446Srnordier SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 71737446Srnordier } 71837446Srnordier 71937446Srnordier mtx_lock(&be_lun->io_lock); 72037446Srnordier devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 72137446Srnordier beio->ds_tag_type, beio->ds_trans_type, 72237446Srnordier /*now*/ NULL, /*then*/&beio->ds_t0); 72337446Srnordier mtx_unlock(&be_lun->io_lock); 72437446Srnordier 72537446Srnordier /* 72637446Srnordier * If we got an error, set the sense data to "MEDIUM ERROR" and 72737446Srnordier * return the I/O to the user. 72837446Srnordier */ 72937446Srnordier if (error != 0) { 73037446Srnordier char path_str[32]; 73137446Srnordier 73237446Srnordier ctl_scsi_path_string(io, path_str, sizeof(path_str)); 73337446Srnordier printf("%s%s command returned errno %d\n", path_str, 73437446Srnordier (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 73537446Srnordier if (error == ENOSPC || error == EDQUOT) { 73637446Srnordier ctl_set_space_alloc_fail(&io->scsiio); 73737446Srnordier } else 73837446Srnordier ctl_set_medium_error(&io->scsiio); 73937446Srnordier ctl_complete_beio(beio); 74037446Srnordier return; 74137446Srnordier } 74237446Srnordier 74337446Srnordier /* 74437446Srnordier * If this is a write or a verify, we're all done. 74537446Srnordier * If this is a read, we can now send the data to the user. 74637446Srnordier */ 74737446Srnordier if ((beio->bio_cmd == BIO_WRITE) || 74837446Srnordier (ARGS(io)->flags & CTL_LLF_VERIFY)) { 74937446Srnordier ctl_set_success(&io->scsiio); 75037446Srnordier ctl_complete_beio(beio); 75137446Srnordier } else { 75237446Srnordier if ((ARGS(io)->flags & CTL_LLF_READ) && 75337446Srnordier beio->beio_cont == NULL) 75437446Srnordier ctl_set_success(&io->scsiio); 75537446Srnordier#ifdef CTL_TIME_IO 75637446Srnordier getbintime(&io->io_hdr.dma_start_bt); 75737446Srnordier#endif 75837446Srnordier ctl_datamove(io); 75937446Srnordier } 76037446Srnordier} 761140384Sdelphij 76237446Srnordierstatic void 76337446Srnordierctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 764106372Sscottl struct ctl_be_block_io *beio) 765106372Sscottl{ 766140384Sdelphij union ctl_io *io = beio->io; 76737446Srnordier struct ctl_lba_len_flags *lbalen = ARGS(io); 768106372Sscottl struct scsi_get_lba_status_data *data; 769106372Sscottl off_t roff, off; 770106372Sscottl int error, status; 771106372Sscottl 772106372Sscottl DPRINTF("entered\n"); 77337446Srnordier 774106372Sscottl off = roff = ((off_t)lbalen->lba) * be_lun->blocksize; 775106372Sscottl vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 776106372Sscottl error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 777185345Sluigi 0, curthread->td_ucred, curthread); 778185345Sluigi if (error == 0 && off > roff) 779185345Sluigi status = 0; /* mapped up to off */ 780185345Sluigi else { 781185345Sluigi error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 782185345Sluigi 0, curthread->td_ucred, curthread); 783185345Sluigi if (error == 0 && off > roff) 784185345Sluigi status = 1; /* deallocated up to off */ 785185587Sluigi else { 786185587Sluigi status = 0; /* unknown up to the end */ 787185345Sluigi off = be_lun->size_bytes; 788185345Sluigi } 789185345Sluigi } 790106372Sscottl VOP_UNLOCK(be_lun->vn, 0); 791106372Sscottl 792106372Sscottl data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 793106372Sscottl scsi_u64to8b(lbalen->lba, data->descr[0].addr); 794106372Sscottl scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->blocksize - lbalen->lba), 79537446Srnordier data->descr[0].length); 79637446Srnordier data->descr[0].status = status; 797106372Sscottl 798106372Sscottl ctl_complete_beio(beio); 799106372Sscottl} 800203869Skib 801203869Skibstatic uint64_t 802106372Sscottlctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 803203869Skib{ 804125916Stjr struct vattr vattr; 805189112Savg struct statfs statfs; 806106372Sscottl uint64_t val; 807189112Savg int error; 808203869Skib 809189112Savg val = UINT64_MAX; 810189112Savg if (be_lun->vn == NULL) 811189112Savg return (val); 812203869Skib vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 813189112Savg if (strcmp(attrname, "blocksused") == 0) { 814189112Savg error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 815189112Savg if (error == 0) 816189112Savg val = vattr.va_bytes / be_lun->blocksize; 817189112Savg } 818189112Savg if (strcmp(attrname, "blocksavail") == 0 && 819189112Savg (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 820189112Savg error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 82137446Srnordier if (error == 0) 822106372Sscottl val = statfs.f_bavail * statfs.f_bsize / 823106372Sscottl be_lun->blocksize; 824106372Sscottl } 82537446Srnordier VOP_UNLOCK(be_lun->vn, 0); 826106372Sscottl return (val); 827203869Skib} 828203869Skib 829203869Skibstatic void 830203869Skibctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 831203869Skib struct ctl_be_block_io *beio) 832203869Skib{ 833203869Skib struct ctl_be_block_devdata *dev_data; 834203869Skib union ctl_io *io; 835203869Skib struct uio xuio; 836203869Skib struct iovec *xiovec; 83737446Srnordier int flags; 83837446Srnordier int error, i; 83937446Srnordier 84037446Srnordier DPRINTF("entered\n"); 84137446Srnordier 84237446Srnordier dev_data = &be_lun->backend.dev; 84337446Srnordier io = beio->io; 84437446Srnordier flags = 0; 845203869Skib if (ARGS(io)->flags & CTL_LLF_DPO) 846203869Skib flags |= IO_DIRECT; 847203869Skib if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 848203869Skib flags |= IO_SYNC; 849203869Skib 850203869Skib bzero(&xuio, sizeof(xuio)); 851203869Skib if (beio->bio_cmd == BIO_READ) { 852203869Skib SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 853203869Skib xuio.uio_rw = UIO_READ; 854203869Skib } else { 855203869Skib SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 856203869Skib xuio.uio_rw = UIO_WRITE; 857203869Skib } 858203869Skib xuio.uio_offset = beio->io_offset; 859203869Skib xuio.uio_resid = beio->io_len; 860203869Skib xuio.uio_segflg = UIO_SYSSPACE; 861203869Skib xuio.uio_iov = beio->xiovecs; 862203869Skib xuio.uio_iovcnt = beio->num_segs; 86337446Srnordier xuio.uio_td = curthread; 86437446Srnordier 86537446Srnordier for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 86637446Srnordier xiovec->iov_base = beio->sg_segs[i].addr; 86737446Srnordier xiovec->iov_len = beio->sg_segs[i].len; 86837446Srnordier } 86937446Srnordier 87037446Srnordier binuptime(&beio->ds_t0); 87137446Srnordier mtx_lock(&be_lun->io_lock); 87237446Srnordier devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 87337446Srnordier mtx_unlock(&be_lun->io_lock); 87437446Srnordier 87537446Srnordier if (beio->bio_cmd == BIO_READ) { 876106372Sscottl error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags); 87737446Srnordier SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 87837446Srnordier } else { 87937446Srnordier error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags); 88037446Srnordier SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 88137446Srnordier } 88237446Srnordier 88337446Srnordier mtx_lock(&be_lun->io_lock); 88437446Srnordier devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 88537446Srnordier beio->ds_tag_type, beio->ds_trans_type, 88637446Srnordier /*now*/ NULL, /*then*/&beio->ds_t0); 88737446Srnordier mtx_unlock(&be_lun->io_lock); 88837446Srnordier 88937446Srnordier /* 89037446Srnordier * If we got an error, set the sense data to "MEDIUM ERROR" and 89137446Srnordier * return the I/O to the user. 89237446Srnordier */ 89337446Srnordier if (error != 0) { 89437446Srnordier if (error == ENOSPC || error == EDQUOT) { 89537446Srnordier ctl_set_space_alloc_fail(&io->scsiio); 89637446Srnordier } else 897185587Sluigi ctl_set_medium_error(&io->scsiio); 898185587Sluigi ctl_complete_beio(beio); 899185587Sluigi return; 900185587Sluigi } 901185587Sluigi 902185587Sluigi /* 903185587Sluigi * If this is a write or a verify, we're all done. 904185587Sluigi * If this is a read, we can now send the data to the user. 905185587Sluigi */ 906185587Sluigi if ((beio->bio_cmd == BIO_WRITE) || 907185587Sluigi (ARGS(io)->flags & CTL_LLF_VERIFY)) { 908185587Sluigi ctl_set_success(&io->scsiio); 909185587Sluigi ctl_complete_beio(beio); 910185587Sluigi } else { 911185587Sluigi if ((ARGS(io)->flags & CTL_LLF_READ) && 912185587Sluigi beio->beio_cont == NULL) 913185587Sluigi ctl_set_success(&io->scsiio); 914185587Sluigi#ifdef CTL_TIME_IO 915185587Sluigi getbintime(&io->io_hdr.dma_start_bt); 916185587Sluigi#endif 917185587Sluigi ctl_datamove(io); 918185587Sluigi } 919185587Sluigi} 920185587Sluigi 921185587Sluigistatic void 922185587Sluigictl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 923185587Sluigi struct ctl_be_block_io *beio) 924185587Sluigi{ 925185587Sluigi struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 926185587Sluigi union ctl_io *io = beio->io; 927185587Sluigi struct ctl_lba_len_flags *lbalen = ARGS(io); 928185587Sluigi struct scsi_get_lba_status_data *data; 929185587Sluigi off_t roff, off; 930185587Sluigi int error, status; 931185587Sluigi 932185587Sluigi DPRINTF("entered\n"); 933185587Sluigi 934185587Sluigi off = roff = ((off_t)lbalen->lba) * be_lun->blocksize; 935185587Sluigi error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE, 936185587Sluigi (caddr_t)&off, FREAD, curthread); 937185587Sluigi if (error == 0 && off > roff) 938185587Sluigi status = 0; /* mapped up to off */ 939185587Sluigi else { 940185587Sluigi error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA, 941185587Sluigi (caddr_t)&off, FREAD, curthread); 942185587Sluigi if (error == 0 && off > roff) 943185587Sluigi status = 1; /* deallocated up to off */ 944185587Sluigi else { 945185587Sluigi status = 0; /* unknown up to the end */ 946185587Sluigi off = be_lun->size_bytes; 94737446Srnordier } 94837446Srnordier } 94937446Srnordier 95037446Srnordier data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 95137446Srnordier scsi_u64to8b(lbalen->lba, data->descr[0].addr); 95237446Srnordier scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->blocksize - lbalen->lba), 95337446Srnordier data->descr[0].length); 95437446Srnordier data->descr[0].status = status; 95537446Srnordier 95637446Srnordier ctl_complete_beio(beio); 95737446Srnordier} 95837446Srnordier 95937446Srnordierstatic void 96037446Srnordierctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 96137446Srnordier struct ctl_be_block_io *beio) 96237446Srnordier{ 96337446Srnordier struct bio *bio; 96437446Srnordier union ctl_io *io; 96537446Srnordier struct ctl_be_block_devdata *dev_data; 96637446Srnordier 96737446Srnordier dev_data = &be_lun->backend.dev; 96837446Srnordier io = beio->io; 96937446Srnordier 97037446Srnordier DPRINTF("entered\n"); 97137446Srnordier 97237446Srnordier /* This can't fail, it's a blocking allocation. */ 97337446Srnordier bio = g_alloc_bio(); 97437446Srnordier 97537446Srnordier bio->bio_cmd = BIO_FLUSH; 97637446Srnordier bio->bio_dev = dev_data->cdev; 97737446Srnordier bio->bio_offset = 0; 97837446Srnordier bio->bio_data = 0; 97937446Srnordier bio->bio_done = ctl_be_block_biodone; 98037446Srnordier bio->bio_caller1 = beio; 98137446Srnordier bio->bio_pblkno = 0; 98237446Srnordier 98337446Srnordier /* 98437446Srnordier * We don't need to acquire the LUN lock here, because we are only 98537446Srnordier * sending one bio, and so there is no other context to synchronize 98637446Srnordier * with. 98737446Srnordier */ 98837446Srnordier beio->num_bios_sent = 1; 98937446Srnordier beio->send_complete = 1; 99037446Srnordier 99137446Srnordier binuptime(&beio->ds_t0); 992190924Sed mtx_lock(&be_lun->io_lock); 993190924Sed devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 994190924Sed mtx_unlock(&be_lun->io_lock); 995190925Sed 996190924Sed (*dev_data->csw->d_strategy)(bio); 997190925Sed} 998190924Sed 999190924Sedstatic void 1000190924Sedctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1001190924Sed struct ctl_be_block_io *beio, 1002190924Sed uint64_t off, uint64_t len, int last) 1003190924Sed{ 1004190924Sed struct bio *bio; 1005190924Sed struct ctl_be_block_devdata *dev_data; 1006190924Sed uint64_t maxlen; 1007190924Sed 1008190924Sed dev_data = &be_lun->backend.dev; 1009190924Sed maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize); 1010190924Sed while (len > 0) { 1011190924Sed bio = g_alloc_bio(); 1012190924Sed bio->bio_cmd = BIO_DELETE; 1013190924Sed bio->bio_dev = dev_data->cdev; 1014190924Sed bio->bio_offset = off; 1015190924Sed bio->bio_length = MIN(len, maxlen); 1016190924Sed bio->bio_data = 0; 1017190924Sed bio->bio_done = ctl_be_block_biodone; 1018190924Sed bio->bio_caller1 = beio; 101937446Srnordier bio->bio_pblkno = off / be_lun->blocksize; 1020 1021 off += bio->bio_length; 1022 len -= bio->bio_length; 1023 1024 mtx_lock(&be_lun->io_lock); 1025 beio->num_bios_sent++; 1026 if (last && len == 0) 1027 beio->send_complete = 1; 1028 mtx_unlock(&be_lun->io_lock); 1029 1030 (*dev_data->csw->d_strategy)(bio); 1031 } 1032} 1033 1034static void 1035ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1036 struct ctl_be_block_io *beio) 1037{ 1038 union ctl_io *io; 1039 struct ctl_be_block_devdata *dev_data; 1040 struct ctl_ptr_len_flags *ptrlen; 1041 struct scsi_unmap_desc *buf, *end; 1042 uint64_t len; 1043 1044 dev_data = &be_lun->backend.dev; 1045 io = beio->io; 1046 1047 DPRINTF("entered\n"); 1048 1049 binuptime(&beio->ds_t0); 1050 mtx_lock(&be_lun->io_lock); 1051 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1052 mtx_unlock(&be_lun->io_lock); 1053 1054 if (beio->io_offset == -1) { 1055 beio->io_len = 0; 1056 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1057 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1058 end = buf + ptrlen->len / sizeof(*buf); 1059 for (; buf < end; buf++) { 1060 len = (uint64_t)scsi_4btoul(buf->length) * 1061 be_lun->blocksize; 1062 beio->io_len += len; 1063 ctl_be_block_unmap_dev_range(be_lun, beio, 1064 scsi_8btou64(buf->lba) * be_lun->blocksize, len, 1065 (end - buf < 2) ? TRUE : FALSE); 1066 } 1067 } else 1068 ctl_be_block_unmap_dev_range(be_lun, beio, 1069 beio->io_offset, beio->io_len, TRUE); 1070} 1071 1072static void 1073ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1074 struct ctl_be_block_io *beio) 1075{ 1076 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1077 int i; 1078 struct bio *bio; 1079 struct ctl_be_block_devdata *dev_data; 1080 off_t cur_offset; 1081 int max_iosize; 1082 1083 DPRINTF("entered\n"); 1084 1085 dev_data = &be_lun->backend.dev; 1086 1087 /* 1088 * We have to limit our I/O size to the maximum supported by the 1089 * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1090 * set it properly, use DFLTPHYS. 1091 */ 1092 max_iosize = dev_data->cdev->si_iosize_max; 1093 if (max_iosize < PAGE_SIZE) 1094 max_iosize = DFLTPHYS; 1095 1096 cur_offset = beio->io_offset; 1097 for (i = 0; i < beio->num_segs; i++) { 1098 size_t cur_size; 1099 uint8_t *cur_ptr; 1100 1101 cur_size = beio->sg_segs[i].len; 1102 cur_ptr = beio->sg_segs[i].addr; 1103 1104 while (cur_size > 0) { 1105 /* This can't fail, it's a blocking allocation. */ 1106 bio = g_alloc_bio(); 1107 1108 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1109 1110 bio->bio_cmd = beio->bio_cmd; 1111 bio->bio_dev = dev_data->cdev; 1112 bio->bio_caller1 = beio; 1113 bio->bio_length = min(cur_size, max_iosize); 1114 bio->bio_offset = cur_offset; 1115 bio->bio_data = cur_ptr; 1116 bio->bio_done = ctl_be_block_biodone; 1117 bio->bio_pblkno = cur_offset / be_lun->blocksize; 1118 1119 cur_offset += bio->bio_length; 1120 cur_ptr += bio->bio_length; 1121 cur_size -= bio->bio_length; 1122 1123 TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1124 beio->num_bios_sent++; 1125 } 1126 } 1127 binuptime(&beio->ds_t0); 1128 mtx_lock(&be_lun->io_lock); 1129 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1130 beio->send_complete = 1; 1131 mtx_unlock(&be_lun->io_lock); 1132 1133 /* 1134 * Fire off all allocated requests! 1135 */ 1136 while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1137 TAILQ_REMOVE(&queue, bio, bio_queue); 1138 (*dev_data->csw->d_strategy)(bio); 1139 } 1140} 1141 1142static uint64_t 1143ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1144{ 1145 struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev; 1146 struct diocgattr_arg arg; 1147 int error; 1148 1149 if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL) 1150 return (UINT64_MAX); 1151 strlcpy(arg.name, attrname, sizeof(arg.name)); 1152 arg.len = sizeof(arg.value.off); 1153 error = dev_data->csw->d_ioctl(dev_data->cdev, 1154 DIOCGATTR, (caddr_t)&arg, FREAD, curthread); 1155 if (error != 0) 1156 return (UINT64_MAX); 1157 return (arg.value.off); 1158} 1159 1160static void 1161ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1162 union ctl_io *io) 1163{ 1164 struct ctl_be_block_io *beio; 1165 struct ctl_lba_len_flags *lbalen; 1166 1167 DPRINTF("entered\n"); 1168 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1169 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1170 1171 beio->io_len = lbalen->len * be_lun->blocksize; 1172 beio->io_offset = lbalen->lba * be_lun->blocksize; 1173 beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1174 beio->bio_cmd = BIO_FLUSH; 1175 beio->ds_trans_type = DEVSTAT_NO_DATA; 1176 DPRINTF("SYNC\n"); 1177 be_lun->lun_flush(be_lun, beio); 1178} 1179 1180static void 1181ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1182{ 1183 union ctl_io *io; 1184 1185 io = beio->io; 1186 ctl_free_beio(beio); 1187 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1188 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1189 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1190 ctl_config_write_done(io); 1191 return; 1192 } 1193 1194 ctl_be_block_config_write(io); 1195} 1196 1197static void 1198ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1199 union ctl_io *io) 1200{ 1201 struct ctl_be_block_io *beio; 1202 struct ctl_lba_len_flags *lbalen; 1203 uint64_t len_left, lba; 1204 uint32_t pb, pbo, adj; 1205 int i, seglen; 1206 uint8_t *buf, *end; 1207 1208 DPRINTF("entered\n"); 1209 1210 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1211 lbalen = ARGS(beio->io); 1212 1213 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1214 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1215 ctl_free_beio(beio); 1216 ctl_set_invalid_field(&io->scsiio, 1217 /*sks_valid*/ 1, 1218 /*command*/ 1, 1219 /*field*/ 1, 1220 /*bit_valid*/ 0, 1221 /*bit*/ 0); 1222 ctl_config_write_done(io); 1223 return; 1224 } 1225 1226 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1227 beio->io_offset = lbalen->lba * be_lun->blocksize; 1228 beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize; 1229 beio->bio_cmd = BIO_DELETE; 1230 beio->ds_trans_type = DEVSTAT_FREE; 1231 1232 be_lun->unmap(be_lun, beio); 1233 return; 1234 } 1235 1236 beio->bio_cmd = BIO_WRITE; 1237 beio->ds_trans_type = DEVSTAT_WRITE; 1238 1239 DPRINTF("WRITE SAME at LBA %jx len %u\n", 1240 (uintmax_t)lbalen->lba, lbalen->len); 1241 1242 pb = be_lun->blocksize << be_lun->pblockexp; 1243 if (be_lun->pblockoff > 0) 1244 pbo = pb - be_lun->blocksize * be_lun->pblockoff; 1245 else 1246 pbo = 0; 1247 len_left = (uint64_t)lbalen->len * be_lun->blocksize; 1248 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1249 1250 /* 1251 * Setup the S/G entry for this chunk. 1252 */ 1253 seglen = MIN(CTLBLK_MAX_SEG, len_left); 1254 if (pb > be_lun->blocksize) { 1255 adj = ((lbalen->lba + lba) * be_lun->blocksize + 1256 seglen - pbo) % pb; 1257 if (seglen > adj) 1258 seglen -= adj; 1259 else 1260 seglen -= seglen % be_lun->blocksize; 1261 } else 1262 seglen -= seglen % be_lun->blocksize; 1263 beio->sg_segs[i].len = seglen; 1264 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1265 1266 DPRINTF("segment %d addr %p len %zd\n", i, 1267 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1268 1269 beio->num_segs++; 1270 len_left -= seglen; 1271 1272 buf = beio->sg_segs[i].addr; 1273 end = buf + seglen; 1274 for (; buf < end; buf += be_lun->blocksize) { 1275 memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize); 1276 if (lbalen->flags & SWS_LBDATA) 1277 scsi_ulto4b(lbalen->lba + lba, buf); 1278 lba++; 1279 } 1280 } 1281 1282 beio->io_offset = lbalen->lba * be_lun->blocksize; 1283 beio->io_len = lba * be_lun->blocksize; 1284 1285 /* We can not do all in one run. Correct and schedule rerun. */ 1286 if (len_left > 0) { 1287 lbalen->lba += lba; 1288 lbalen->len -= lba; 1289 beio->beio_cont = ctl_be_block_cw_done_ws; 1290 } 1291 1292 be_lun->dispatch(be_lun, beio); 1293} 1294 1295static void 1296ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1297 union ctl_io *io) 1298{ 1299 struct ctl_be_block_io *beio; 1300 struct ctl_ptr_len_flags *ptrlen; 1301 1302 DPRINTF("entered\n"); 1303 1304 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1305 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1306 1307 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1308 ctl_free_beio(beio); 1309 ctl_set_invalid_field(&io->scsiio, 1310 /*sks_valid*/ 0, 1311 /*command*/ 1, 1312 /*field*/ 0, 1313 /*bit_valid*/ 0, 1314 /*bit*/ 0); 1315 ctl_config_write_done(io); 1316 return; 1317 } 1318 1319 beio->io_len = 0; 1320 beio->io_offset = -1; 1321 beio->bio_cmd = BIO_DELETE; 1322 beio->ds_trans_type = DEVSTAT_FREE; 1323 DPRINTF("UNMAP\n"); 1324 be_lun->unmap(be_lun, beio); 1325} 1326 1327static void 1328ctl_be_block_cr_done(struct ctl_be_block_io *beio) 1329{ 1330 union ctl_io *io; 1331 1332 io = beio->io; 1333 ctl_free_beio(beio); 1334 ctl_config_read_done(io); 1335} 1336 1337static void 1338ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1339 union ctl_io *io) 1340{ 1341 struct ctl_be_block_io *beio; 1342 struct ctl_be_block_softc *softc; 1343 1344 DPRINTF("entered\n"); 1345 1346 softc = be_lun->softc; 1347 beio = ctl_alloc_beio(softc); 1348 beio->io = io; 1349 beio->lun = be_lun; 1350 beio->beio_cont = ctl_be_block_cr_done; 1351 PRIV(io)->ptr = (void *)beio; 1352 1353 switch (io->scsiio.cdb[0]) { 1354 case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1355 beio->bio_cmd = -1; 1356 beio->ds_trans_type = DEVSTAT_NO_DATA; 1357 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1358 beio->io_len = 0; 1359 if (be_lun->get_lba_status) 1360 be_lun->get_lba_status(be_lun, beio); 1361 else 1362 ctl_be_block_cr_done(beio); 1363 break; 1364 default: 1365 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1366 break; 1367 } 1368} 1369 1370static void 1371ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1372{ 1373 union ctl_io *io; 1374 1375 io = beio->io; 1376 ctl_free_beio(beio); 1377 ctl_config_write_done(io); 1378} 1379 1380static void 1381ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1382 union ctl_io *io) 1383{ 1384 struct ctl_be_block_io *beio; 1385 struct ctl_be_block_softc *softc; 1386 1387 DPRINTF("entered\n"); 1388 1389 softc = be_lun->softc; 1390 beio = ctl_alloc_beio(softc); 1391 beio->io = io; 1392 beio->lun = be_lun; 1393 beio->beio_cont = ctl_be_block_cw_done; 1394 switch (io->scsiio.tag_type) { 1395 case CTL_TAG_ORDERED: 1396 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1397 break; 1398 case CTL_TAG_HEAD_OF_QUEUE: 1399 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1400 break; 1401 case CTL_TAG_UNTAGGED: 1402 case CTL_TAG_SIMPLE: 1403 case CTL_TAG_ACA: 1404 default: 1405 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1406 break; 1407 } 1408 PRIV(io)->ptr = (void *)beio; 1409 1410 switch (io->scsiio.cdb[0]) { 1411 case SYNCHRONIZE_CACHE: 1412 case SYNCHRONIZE_CACHE_16: 1413 ctl_be_block_cw_dispatch_sync(be_lun, io); 1414 break; 1415 case WRITE_SAME_10: 1416 case WRITE_SAME_16: 1417 ctl_be_block_cw_dispatch_ws(be_lun, io); 1418 break; 1419 case UNMAP: 1420 ctl_be_block_cw_dispatch_unmap(be_lun, io); 1421 break; 1422 default: 1423 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1424 break; 1425 } 1426} 1427 1428SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1429SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1430SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1431SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1432 1433static void 1434ctl_be_block_next(struct ctl_be_block_io *beio) 1435{ 1436 struct ctl_be_block_lun *be_lun; 1437 union ctl_io *io; 1438 1439 io = beio->io; 1440 be_lun = beio->lun; 1441 ctl_free_beio(beio); 1442 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1443 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1444 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1445 ctl_data_submit_done(io); 1446 return; 1447 } 1448 1449 io->io_hdr.status &= ~CTL_STATUS_MASK; 1450 io->io_hdr.status |= CTL_STATUS_NONE; 1451 1452 mtx_lock(&be_lun->queue_lock); 1453 /* 1454 * XXX KDM make sure that links is okay to use at this point. 1455 * Otherwise, we either need to add another field to ctl_io_hdr, 1456 * or deal with resource allocation here. 1457 */ 1458 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1459 mtx_unlock(&be_lun->queue_lock); 1460 1461 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1462} 1463 1464static void 1465ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1466 union ctl_io *io) 1467{ 1468 struct ctl_be_block_io *beio; 1469 struct ctl_be_block_softc *softc; 1470 struct ctl_lba_len_flags *lbalen; 1471 struct ctl_ptr_len_flags *bptrlen; 1472 uint64_t len_left, lbas; 1473 int i; 1474 1475 softc = be_lun->softc; 1476 1477 DPRINTF("entered\n"); 1478 1479 lbalen = ARGS(io); 1480 if (lbalen->flags & CTL_LLF_WRITE) { 1481 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1482 } else { 1483 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1484 } 1485 1486 beio = ctl_alloc_beio(softc); 1487 beio->io = io; 1488 beio->lun = be_lun; 1489 bptrlen = PRIV(io); 1490 bptrlen->ptr = (void *)beio; 1491 1492 switch (io->scsiio.tag_type) { 1493 case CTL_TAG_ORDERED: 1494 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1495 break; 1496 case CTL_TAG_HEAD_OF_QUEUE: 1497 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1498 break; 1499 case CTL_TAG_UNTAGGED: 1500 case CTL_TAG_SIMPLE: 1501 case CTL_TAG_ACA: 1502 default: 1503 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1504 break; 1505 } 1506 1507 if (lbalen->flags & CTL_LLF_WRITE) { 1508 beio->bio_cmd = BIO_WRITE; 1509 beio->ds_trans_type = DEVSTAT_WRITE; 1510 } else { 1511 beio->bio_cmd = BIO_READ; 1512 beio->ds_trans_type = DEVSTAT_READ; 1513 } 1514 1515 DPRINTF("%s at LBA %jx len %u @%ju\n", 1516 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1517 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1518 if (lbalen->flags & CTL_LLF_COMPARE) 1519 lbas = CTLBLK_HALF_IO_SIZE; 1520 else 1521 lbas = CTLBLK_MAX_IO_SIZE; 1522 lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize); 1523 beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize; 1524 beio->io_len = lbas * be_lun->blocksize; 1525 bptrlen->len += lbas; 1526 1527 for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1528 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1529 i, CTLBLK_MAX_SEGS)); 1530 1531 /* 1532 * Setup the S/G entry for this chunk. 1533 */ 1534 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1535 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1536 1537 DPRINTF("segment %d addr %p len %zd\n", i, 1538 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1539 1540 /* Set up second segment for compare operation. */ 1541 if (lbalen->flags & CTL_LLF_COMPARE) { 1542 beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1543 beio->sg_segs[i].len; 1544 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1545 uma_zalloc(be_lun->lun_zone, M_WAITOK); 1546 } 1547 1548 beio->num_segs++; 1549 len_left -= beio->sg_segs[i].len; 1550 } 1551 if (bptrlen->len < lbalen->len) 1552 beio->beio_cont = ctl_be_block_next; 1553 io->scsiio.be_move_done = ctl_be_block_move_done; 1554 /* For compare we have separate S/G lists for read and datamove. */ 1555 if (lbalen->flags & CTL_LLF_COMPARE) 1556 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1557 else 1558 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1559 io->scsiio.kern_data_len = beio->io_len; 1560 io->scsiio.kern_data_resid = 0; 1561 io->scsiio.kern_sg_entries = beio->num_segs; 1562 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1563 1564 /* 1565 * For the read case, we need to read the data into our buffers and 1566 * then we can send it back to the user. For the write case, we 1567 * need to get the data from the user first. 1568 */ 1569 if (beio->bio_cmd == BIO_READ) { 1570 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1571 be_lun->dispatch(be_lun, beio); 1572 } else { 1573 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1574#ifdef CTL_TIME_IO 1575 getbintime(&io->io_hdr.dma_start_bt); 1576#endif 1577 ctl_datamove(io); 1578 } 1579} 1580 1581static void 1582ctl_be_block_worker(void *context, int pending) 1583{ 1584 struct ctl_be_block_lun *be_lun; 1585 struct ctl_be_block_softc *softc; 1586 union ctl_io *io; 1587 1588 be_lun = (struct ctl_be_block_lun *)context; 1589 softc = be_lun->softc; 1590 1591 DPRINTF("entered\n"); 1592 1593 mtx_lock(&be_lun->queue_lock); 1594 for (;;) { 1595 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1596 if (io != NULL) { 1597 struct ctl_be_block_io *beio; 1598 1599 DPRINTF("datamove queue\n"); 1600 1601 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1602 ctl_io_hdr, links); 1603 1604 mtx_unlock(&be_lun->queue_lock); 1605 1606 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1607 1608 be_lun->dispatch(be_lun, beio); 1609 1610 mtx_lock(&be_lun->queue_lock); 1611 continue; 1612 } 1613 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1614 if (io != NULL) { 1615 DPRINTF("config write queue\n"); 1616 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1617 ctl_io_hdr, links); 1618 mtx_unlock(&be_lun->queue_lock); 1619 ctl_be_block_cw_dispatch(be_lun, io); 1620 mtx_lock(&be_lun->queue_lock); 1621 continue; 1622 } 1623 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1624 if (io != NULL) { 1625 DPRINTF("config read queue\n"); 1626 STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1627 ctl_io_hdr, links); 1628 mtx_unlock(&be_lun->queue_lock); 1629 ctl_be_block_cr_dispatch(be_lun, io); 1630 mtx_lock(&be_lun->queue_lock); 1631 continue; 1632 } 1633 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1634 if (io != NULL) { 1635 DPRINTF("input queue\n"); 1636 1637 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1638 ctl_io_hdr, links); 1639 mtx_unlock(&be_lun->queue_lock); 1640 1641 /* 1642 * We must drop the lock, since this routine and 1643 * its children may sleep. 1644 */ 1645 ctl_be_block_dispatch(be_lun, io); 1646 1647 mtx_lock(&be_lun->queue_lock); 1648 continue; 1649 } 1650 1651 /* 1652 * If we get here, there is no work left in the queues, so 1653 * just break out and let the task queue go to sleep. 1654 */ 1655 break; 1656 } 1657 mtx_unlock(&be_lun->queue_lock); 1658} 1659 1660/* 1661 * Entry point from CTL to the backend for I/O. We queue everything to a 1662 * work thread, so this just puts the I/O on a queue and wakes up the 1663 * thread. 1664 */ 1665static int 1666ctl_be_block_submit(union ctl_io *io) 1667{ 1668 struct ctl_be_block_lun *be_lun; 1669 struct ctl_be_lun *ctl_be_lun; 1670 1671 DPRINTF("entered\n"); 1672 1673 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1674 CTL_PRIV_BACKEND_LUN].ptr; 1675 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1676 1677 /* 1678 * Make sure we only get SCSI I/O. 1679 */ 1680 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1681 "%#x) encountered", io->io_hdr.io_type)); 1682 1683 PRIV(io)->len = 0; 1684 1685 mtx_lock(&be_lun->queue_lock); 1686 /* 1687 * XXX KDM make sure that links is okay to use at this point. 1688 * Otherwise, we either need to add another field to ctl_io_hdr, 1689 * or deal with resource allocation here. 1690 */ 1691 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1692 mtx_unlock(&be_lun->queue_lock); 1693 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1694 1695 return (CTL_RETVAL_COMPLETE); 1696} 1697 1698static int 1699ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1700 int flag, struct thread *td) 1701{ 1702 struct ctl_be_block_softc *softc; 1703 int error; 1704 1705 softc = &backend_block_softc; 1706 1707 error = 0; 1708 1709 switch (cmd) { 1710 case CTL_LUN_REQ: { 1711 struct ctl_lun_req *lun_req; 1712 1713 lun_req = (struct ctl_lun_req *)addr; 1714 1715 switch (lun_req->reqtype) { 1716 case CTL_LUNREQ_CREATE: 1717 error = ctl_be_block_create(softc, lun_req); 1718 break; 1719 case CTL_LUNREQ_RM: 1720 error = ctl_be_block_rm(softc, lun_req); 1721 break; 1722 case CTL_LUNREQ_MODIFY: 1723 error = ctl_be_block_modify(softc, lun_req); 1724 break; 1725 default: 1726 lun_req->status = CTL_LUN_ERROR; 1727 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1728 "invalid LUN request type %d", 1729 lun_req->reqtype); 1730 break; 1731 } 1732 break; 1733 } 1734 default: 1735 error = ENOTTY; 1736 break; 1737 } 1738 1739 return (error); 1740} 1741 1742static int 1743ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1744{ 1745 struct ctl_be_block_filedata *file_data; 1746 struct ctl_lun_create_params *params; 1747 char *value; 1748 struct vattr vattr; 1749 off_t ps, pss, po, pos, us, uss, uo, uos; 1750 int error; 1751 1752 error = 0; 1753 file_data = &be_lun->backend.file; 1754 params = &be_lun->params; 1755 1756 be_lun->dev_type = CTL_BE_BLOCK_FILE; 1757 be_lun->dispatch = ctl_be_block_dispatch_file; 1758 be_lun->lun_flush = ctl_be_block_flush_file; 1759 be_lun->get_lba_status = ctl_be_block_gls_file; 1760 be_lun->getattr = ctl_be_block_getattr_file; 1761 1762 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1763 if (error != 0) { 1764 snprintf(req->error_str, sizeof(req->error_str), 1765 "error calling VOP_GETATTR() for file %s", 1766 be_lun->dev_path); 1767 return (error); 1768 } 1769 1770 /* 1771 * Verify that we have the ability to upgrade to exclusive 1772 * access on this file so we can trap errors at open instead 1773 * of reporting them during first access. 1774 */ 1775 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1776 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1777 if (be_lun->vn->v_iflag & VI_DOOMED) { 1778 error = EBADF; 1779 snprintf(req->error_str, sizeof(req->error_str), 1780 "error locking file %s", be_lun->dev_path); 1781 return (error); 1782 } 1783 } 1784 1785 1786 file_data->cred = crhold(curthread->td_ucred); 1787 if (params->lun_size_bytes != 0) 1788 be_lun->size_bytes = params->lun_size_bytes; 1789 else 1790 be_lun->size_bytes = vattr.va_size; 1791 /* 1792 * We set the multi thread flag for file operations because all 1793 * filesystems (in theory) are capable of allowing multiple readers 1794 * of a file at once. So we want to get the maximum possible 1795 * concurrency. 1796 */ 1797 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1798 1799 /* 1800 * For files we can use any logical block size. Prefer 512 bytes 1801 * for compatibility reasons. If file's vattr.va_blocksize 1802 * (preferred I/O block size) is bigger and multiple to chosen 1803 * logical block size -- report it as physical block size. 1804 */ 1805 if (params->blocksize_bytes != 0) 1806 be_lun->blocksize = params->blocksize_bytes; 1807 else 1808 be_lun->blocksize = 512; 1809 1810 us = ps = vattr.va_blocksize; 1811 uo = po = 0; 1812 1813 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize"); 1814 if (value != NULL) 1815 ctl_expand_number(value, &ps); 1816 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset"); 1817 if (value != NULL) 1818 ctl_expand_number(value, &po); 1819 pss = ps / be_lun->blocksize; 1820 pos = po / be_lun->blocksize; 1821 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1822 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1823 be_lun->pblockexp = fls(pss) - 1; 1824 be_lun->pblockoff = (pss - pos) % pss; 1825 } 1826 1827 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize"); 1828 if (value != NULL) 1829 ctl_expand_number(value, &us); 1830 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset"); 1831 if (value != NULL) 1832 ctl_expand_number(value, &uo); 1833 uss = us / be_lun->blocksize; 1834 uos = uo / be_lun->blocksize; 1835 if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) && 1836 ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) { 1837 be_lun->ublockexp = fls(uss) - 1; 1838 be_lun->ublockoff = (uss - uos) % uss; 1839 } 1840 1841 /* 1842 * Sanity check. The media size has to be at least one 1843 * sector long. 1844 */ 1845 if (be_lun->size_bytes < be_lun->blocksize) { 1846 error = EINVAL; 1847 snprintf(req->error_str, sizeof(req->error_str), 1848 "file %s size %ju < block size %u", be_lun->dev_path, 1849 (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1850 } 1851 1852 be_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / be_lun->blocksize; 1853 return (error); 1854} 1855 1856static int 1857ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1858{ 1859 struct ctl_lun_create_params *params; 1860 struct vattr vattr; 1861 struct cdev *dev; 1862 struct cdevsw *devsw; 1863 char *value; 1864 int error, atomic, maxio, unmap, tmp; 1865 off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1866 1867 params = &be_lun->params; 1868 1869 be_lun->dev_type = CTL_BE_BLOCK_DEV; 1870 be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1871 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1872 &be_lun->backend.dev.dev_ref); 1873 if (be_lun->backend.dev.csw == NULL) 1874 panic("Unable to retrieve device switch"); 1875 if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) { 1876 be_lun->dispatch = ctl_be_block_dispatch_zvol; 1877 be_lun->get_lba_status = ctl_be_block_gls_zvol; 1878 atomic = maxio = CTLBLK_MAX_IO_SIZE; 1879 } else { 1880 be_lun->dispatch = ctl_be_block_dispatch_dev; 1881 atomic = 0; 1882 maxio = be_lun->backend.dev.cdev->si_iosize_max; 1883 if (maxio <= 0) 1884 maxio = DFLTPHYS; 1885 if (maxio > CTLBLK_MAX_IO_SIZE) 1886 maxio = CTLBLK_MAX_IO_SIZE; 1887 } 1888 be_lun->lun_flush = ctl_be_block_flush_dev; 1889 be_lun->getattr = ctl_be_block_getattr_dev; 1890 1891 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1892 if (error) { 1893 snprintf(req->error_str, sizeof(req->error_str), 1894 "error getting vnode attributes for device %s", 1895 be_lun->dev_path); 1896 return (error); 1897 } 1898 1899 dev = be_lun->vn->v_rdev; 1900 devsw = dev->si_devsw; 1901 if (!devsw->d_ioctl) { 1902 snprintf(req->error_str, sizeof(req->error_str), 1903 "no d_ioctl for device %s!", 1904 be_lun->dev_path); 1905 return (ENODEV); 1906 } 1907 1908 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1909 curthread); 1910 if (error) { 1911 snprintf(req->error_str, sizeof(req->error_str), 1912 "error %d returned for DIOCGSECTORSIZE ioctl " 1913 "on %s!", error, be_lun->dev_path); 1914 return (error); 1915 } 1916 1917 /* 1918 * If the user has asked for a blocksize that is greater than the 1919 * backing device's blocksize, we can do it only if the blocksize 1920 * the user is asking for is an even multiple of the underlying 1921 * device's blocksize. 1922 */ 1923 if ((params->blocksize_bytes != 0) && 1924 (params->blocksize_bytes >= tmp)) { 1925 if (params->blocksize_bytes % tmp == 0) { 1926 be_lun->blocksize = params->blocksize_bytes; 1927 } else { 1928 snprintf(req->error_str, sizeof(req->error_str), 1929 "requested blocksize %u is not an even " 1930 "multiple of backing device blocksize %u", 1931 params->blocksize_bytes, tmp); 1932 return (EINVAL); 1933 1934 } 1935 } else if (params->blocksize_bytes != 0) { 1936 snprintf(req->error_str, sizeof(req->error_str), 1937 "requested blocksize %u < backing device " 1938 "blocksize %u", params->blocksize_bytes, tmp); 1939 return (EINVAL); 1940 } else 1941 be_lun->blocksize = tmp; 1942 1943 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1944 curthread); 1945 if (error) { 1946 snprintf(req->error_str, sizeof(req->error_str), 1947 "error %d returned for DIOCGMEDIASIZE " 1948 " ioctl on %s!", error, 1949 be_lun->dev_path); 1950 return (error); 1951 } 1952 1953 if (params->lun_size_bytes != 0) { 1954 if (params->lun_size_bytes > otmp) { 1955 snprintf(req->error_str, sizeof(req->error_str), 1956 "requested LUN size %ju > backing device " 1957 "size %ju", 1958 (uintmax_t)params->lun_size_bytes, 1959 (uintmax_t)otmp); 1960 return (EINVAL); 1961 } 1962 1963 be_lun->size_bytes = params->lun_size_bytes; 1964 } else 1965 be_lun->size_bytes = otmp; 1966 1967 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE, 1968 (caddr_t)&ps, FREAD, curthread); 1969 if (error) 1970 ps = po = 0; 1971 else { 1972 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET, 1973 (caddr_t)&po, FREAD, curthread); 1974 if (error) 1975 po = 0; 1976 } 1977 us = ps; 1978 uo = po; 1979 1980 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize"); 1981 if (value != NULL) 1982 ctl_expand_number(value, &ps); 1983 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset"); 1984 if (value != NULL) 1985 ctl_expand_number(value, &po); 1986 pss = ps / be_lun->blocksize; 1987 pos = po / be_lun->blocksize; 1988 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) && 1989 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) { 1990 be_lun->pblockexp = fls(pss) - 1; 1991 be_lun->pblockoff = (pss - pos) % pss; 1992 } 1993 1994 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize"); 1995 if (value != NULL) 1996 ctl_expand_number(value, &us); 1997 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset"); 1998 if (value != NULL) 1999 ctl_expand_number(value, &uo); 2000 uss = us / be_lun->blocksize; 2001 uos = uo / be_lun->blocksize; 2002 if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) && 2003 ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) { 2004 be_lun->ublockexp = fls(uss) - 1; 2005 be_lun->ublockoff = (uss - uos) % uss; 2006 } 2007 2008 be_lun->atomicblock = atomic / be_lun->blocksize; 2009 be_lun->opttxferlen = maxio / be_lun->blocksize; 2010 2011 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2012 unmap = 1; 2013 } else { 2014 struct diocgattr_arg arg; 2015 2016 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2017 arg.len = sizeof(arg.value.i); 2018 error = devsw->d_ioctl(dev, DIOCGATTR, 2019 (caddr_t)&arg, FREAD, curthread); 2020 unmap = (error == 0) ? arg.value.i : 0; 2021 } 2022 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); 2023 if (value != NULL) 2024 unmap = (strcmp(value, "on") == 0); 2025 if (unmap) 2026 be_lun->unmap = ctl_be_block_unmap_dev; 2027 2028 return (0); 2029} 2030 2031static int 2032ctl_be_block_close(struct ctl_be_block_lun *be_lun) 2033{ 2034 DROP_GIANT(); 2035 if (be_lun->vn) { 2036 int flags = FREAD | FWRITE; 2037 2038 switch (be_lun->dev_type) { 2039 case CTL_BE_BLOCK_DEV: 2040 if (be_lun->backend.dev.csw) { 2041 dev_relthread(be_lun->backend.dev.cdev, 2042 be_lun->backend.dev.dev_ref); 2043 be_lun->backend.dev.csw = NULL; 2044 be_lun->backend.dev.cdev = NULL; 2045 } 2046 break; 2047 case CTL_BE_BLOCK_FILE: 2048 break; 2049 case CTL_BE_BLOCK_NONE: 2050 break; 2051 default: 2052 panic("Unexpected backend type."); 2053 break; 2054 } 2055 2056 (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2057 be_lun->vn = NULL; 2058 2059 switch (be_lun->dev_type) { 2060 case CTL_BE_BLOCK_DEV: 2061 break; 2062 case CTL_BE_BLOCK_FILE: 2063 if (be_lun->backend.file.cred != NULL) { 2064 crfree(be_lun->backend.file.cred); 2065 be_lun->backend.file.cred = NULL; 2066 } 2067 break; 2068 case CTL_BE_BLOCK_NONE: 2069 break; 2070 default: 2071 panic("Unexpected backend type."); 2072 break; 2073 } 2074 be_lun->dev_type = CTL_BE_BLOCK_NONE; 2075 } 2076 PICKUP_GIANT(); 2077 2078 return (0); 2079} 2080 2081static int 2082ctl_be_block_open(struct ctl_be_block_softc *softc, 2083 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2084{ 2085 struct nameidata nd; 2086 int flags; 2087 int error; 2088 2089 /* 2090 * XXX KDM allow a read-only option? 2091 */ 2092 flags = FREAD | FWRITE; 2093 error = 0; 2094 2095 if (rootvnode == NULL) { 2096 snprintf(req->error_str, sizeof(req->error_str), 2097 "Root filesystem is not mounted"); 2098 return (1); 2099 } 2100 2101 pwd_ensure_dirs(); 2102 2103 again: 2104 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2105 error = vn_open(&nd, &flags, 0, NULL); 2106 if (error) { 2107 /* 2108 * This is the only reasonable guess we can make as far as 2109 * path if the user doesn't give us a fully qualified path. 2110 * If they want to specify a file, they need to specify the 2111 * full path. 2112 */ 2113 if (be_lun->dev_path[0] != '/') { 2114 char *dev_path = "/dev/"; 2115 char *dev_name; 2116 2117 /* Try adding device path at beginning of name */ 2118 dev_name = malloc(strlen(be_lun->dev_path) 2119 + strlen(dev_path) + 1, 2120 M_CTLBLK, M_WAITOK); 2121 if (dev_name) { 2122 sprintf(dev_name, "%s%s", dev_path, 2123 be_lun->dev_path); 2124 free(be_lun->dev_path, M_CTLBLK); 2125 be_lun->dev_path = dev_name; 2126 goto again; 2127 } 2128 } 2129 snprintf(req->error_str, sizeof(req->error_str), 2130 "error opening %s: %d", be_lun->dev_path, error); 2131 return (error); 2132 } 2133 2134 NDFREE(&nd, NDF_ONLY_PNBUF); 2135 2136 be_lun->vn = nd.ni_vp; 2137 2138 /* We only support disks and files. */ 2139 if (vn_isdisk(be_lun->vn, &error)) { 2140 error = ctl_be_block_open_dev(be_lun, req); 2141 } else if (be_lun->vn->v_type == VREG) { 2142 error = ctl_be_block_open_file(be_lun, req); 2143 } else { 2144 error = EINVAL; 2145 snprintf(req->error_str, sizeof(req->error_str), 2146 "%s is not a disk or plain file", be_lun->dev_path); 2147 } 2148 VOP_UNLOCK(be_lun->vn, 0); 2149 2150 if (error != 0) 2151 ctl_be_block_close(be_lun); 2152 return (0); 2153} 2154 2155static int 2156ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2157{ 2158 struct ctl_be_block_lun *be_lun; 2159 struct ctl_lun_create_params *params; 2160 char num_thread_str[16]; 2161 char tmpstr[32]; 2162 char *value; 2163 int retval, num_threads; 2164 int tmp_num_threads; 2165 2166 params = &req->reqdata.create; 2167 retval = 0; 2168 req->status = CTL_LUN_OK; 2169 2170 num_threads = cbb_num_threads; 2171 2172 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2173 2174 be_lun->params = req->reqdata.create; 2175 be_lun->softc = softc; 2176 STAILQ_INIT(&be_lun->input_queue); 2177 STAILQ_INIT(&be_lun->config_read_queue); 2178 STAILQ_INIT(&be_lun->config_write_queue); 2179 STAILQ_INIT(&be_lun->datamove_queue); 2180 sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2181 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2182 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2183 ctl_init_opts(&be_lun->ctl_be_lun.options, 2184 req->num_be_args, req->kern_be_args); 2185 2186 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2187 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2188 2189 if (be_lun->lun_zone == NULL) { 2190 snprintf(req->error_str, sizeof(req->error_str), 2191 "error allocating UMA zone"); 2192 goto bailout_error; 2193 } 2194 2195 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2196 be_lun->ctl_be_lun.lun_type = params->device_type; 2197 else 2198 be_lun->ctl_be_lun.lun_type = T_DIRECT; 2199 2200 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 2201 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file"); 2202 if (value == NULL) { 2203 snprintf(req->error_str, sizeof(req->error_str), 2204 "no file argument specified"); 2205 goto bailout_error; 2206 } 2207 be_lun->dev_path = strdup(value, M_CTLBLK); 2208 be_lun->size_bytes = params->lun_size_bytes; 2209 if (params->blocksize_bytes != 0) 2210 be_lun->blocksize = params->blocksize_bytes; 2211 else 2212 be_lun->blocksize = 512; 2213 2214 retval = ctl_be_block_open(softc, be_lun, req); 2215 be_lun->size_blocks = be_lun->size_bytes / be_lun->blocksize; 2216 if (retval != 0) { 2217 retval = 0; 2218 req->status = CTL_LUN_WARNING; 2219 } 2220 } else { 2221 /* 2222 * For processor devices, we don't have any size. 2223 */ 2224 be_lun->blocksize = 0; 2225 be_lun->pblockexp = 0; 2226 be_lun->pblockoff = 0; 2227 be_lun->ublockexp = 0; 2228 be_lun->ublockoff = 0; 2229 be_lun->size_blocks = 0; 2230 be_lun->size_bytes = 0; 2231 be_lun->ctl_be_lun.maxlba = 0; 2232 2233 /* 2234 * Default to just 1 thread for processor devices. 2235 */ 2236 num_threads = 1; 2237 } 2238 2239 /* 2240 * XXX This searching loop might be refactored to be combined with 2241 * the loop above, 2242 */ 2243 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads"); 2244 if (value != NULL) { 2245 tmp_num_threads = strtol(value, NULL, 0); 2246 2247 /* 2248 * We don't let the user specify less than one 2249 * thread, but hope he's clueful enough not to 2250 * specify 1000 threads. 2251 */ 2252 if (tmp_num_threads < 1) { 2253 snprintf(req->error_str, sizeof(req->error_str), 2254 "invalid number of threads %s", 2255 num_thread_str); 2256 goto bailout_error; 2257 } 2258 num_threads = tmp_num_threads; 2259 } 2260 2261 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2262 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 2263 if (be_lun->vn == NULL) 2264 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE; 2265 if (be_lun->unmap != NULL) 2266 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2267 if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2268 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_SERSEQ_READ; 2269 be_lun->ctl_be_lun.be_lun = be_lun; 2270 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2271 0 : (be_lun->size_blocks - 1); 2272 be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2273 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2274 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2275 be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp; 2276 be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff; 2277 be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock; 2278 be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen; 2279 /* Tell the user the blocksize we ended up using */ 2280 params->lun_size_bytes = be_lun->size_bytes; 2281 params->blocksize_bytes = be_lun->blocksize; 2282 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2283 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 2284 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 2285 } else 2286 be_lun->ctl_be_lun.req_lun_id = 0; 2287 2288 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 2289 be_lun->ctl_be_lun.lun_config_status = 2290 ctl_be_block_lun_config_status; 2291 be_lun->ctl_be_lun.be = &ctl_be_block_driver; 2292 2293 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2294 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2295 softc->num_luns); 2296 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 2297 MIN(sizeof(be_lun->ctl_be_lun.serial_num), 2298 sizeof(tmpstr))); 2299 2300 /* Tell the user what we used for a serial number */ 2301 strncpy((char *)params->serial_num, tmpstr, 2302 MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2303 } else { 2304 strncpy((char *)be_lun->ctl_be_lun.serial_num, 2305 params->serial_num, 2306 MIN(sizeof(be_lun->ctl_be_lun.serial_num), 2307 sizeof(params->serial_num))); 2308 } 2309 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2310 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2311 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 2312 MIN(sizeof(be_lun->ctl_be_lun.device_id), 2313 sizeof(tmpstr))); 2314 2315 /* Tell the user what we used for a device ID */ 2316 strncpy((char *)params->device_id, tmpstr, 2317 MIN(sizeof(params->device_id), sizeof(tmpstr))); 2318 } else { 2319 strncpy((char *)be_lun->ctl_be_lun.device_id, 2320 params->device_id, 2321 MIN(sizeof(be_lun->ctl_be_lun.device_id), 2322 sizeof(params->device_id))); 2323 } 2324 2325 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2326 2327 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2328 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2329 2330 if (be_lun->io_taskqueue == NULL) { 2331 snprintf(req->error_str, sizeof(req->error_str), 2332 "unable to create taskqueue"); 2333 goto bailout_error; 2334 } 2335 2336 /* 2337 * Note that we start the same number of threads by default for 2338 * both the file case and the block device case. For the file 2339 * case, we need multiple threads to allow concurrency, because the 2340 * vnode interface is designed to be a blocking interface. For the 2341 * block device case, ZFS zvols at least will block the caller's 2342 * context in many instances, and so we need multiple threads to 2343 * overcome that problem. Other block devices don't need as many 2344 * threads, but they shouldn't cause too many problems. 2345 * 2346 * If the user wants to just have a single thread for a block 2347 * device, he can specify that when the LUN is created, or change 2348 * the tunable/sysctl to alter the default number of threads. 2349 */ 2350 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2351 /*num threads*/num_threads, 2352 /*priority*/PWAIT, 2353 /*thread name*/ 2354 "%s taskq", be_lun->lunname); 2355 2356 if (retval != 0) 2357 goto bailout_error; 2358 2359 be_lun->num_threads = num_threads; 2360 2361 mtx_lock(&softc->lock); 2362 softc->num_luns++; 2363 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2364 2365 mtx_unlock(&softc->lock); 2366 2367 retval = ctl_add_lun(&be_lun->ctl_be_lun); 2368 if (retval != 0) { 2369 mtx_lock(&softc->lock); 2370 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2371 links); 2372 softc->num_luns--; 2373 mtx_unlock(&softc->lock); 2374 snprintf(req->error_str, sizeof(req->error_str), 2375 "ctl_add_lun() returned error %d, see dmesg for " 2376 "details", retval); 2377 retval = 0; 2378 goto bailout_error; 2379 } 2380 2381 mtx_lock(&softc->lock); 2382 2383 /* 2384 * Tell the config_status routine that we're waiting so it won't 2385 * clean up the LUN in the event of an error. 2386 */ 2387 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2388 2389 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2390 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2391 if (retval == EINTR) 2392 break; 2393 } 2394 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2395 2396 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2397 snprintf(req->error_str, sizeof(req->error_str), 2398 "LUN configuration error, see dmesg for details"); 2399 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2400 links); 2401 softc->num_luns--; 2402 mtx_unlock(&softc->lock); 2403 goto bailout_error; 2404 } else { 2405 params->req_lun_id = be_lun->ctl_be_lun.lun_id; 2406 } 2407 2408 mtx_unlock(&softc->lock); 2409 2410 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2411 be_lun->blocksize, 2412 DEVSTAT_ALL_SUPPORTED, 2413 be_lun->ctl_be_lun.lun_type 2414 | DEVSTAT_TYPE_IF_OTHER, 2415 DEVSTAT_PRIORITY_OTHER); 2416 2417 return (retval); 2418 2419bailout_error: 2420 req->status = CTL_LUN_ERROR; 2421 2422 if (be_lun->io_taskqueue != NULL) 2423 taskqueue_free(be_lun->io_taskqueue); 2424 ctl_be_block_close(be_lun); 2425 if (be_lun->dev_path != NULL) 2426 free(be_lun->dev_path, M_CTLBLK); 2427 if (be_lun->lun_zone != NULL) 2428 uma_zdestroy(be_lun->lun_zone); 2429 ctl_free_opts(&be_lun->ctl_be_lun.options); 2430 mtx_destroy(&be_lun->queue_lock); 2431 mtx_destroy(&be_lun->io_lock); 2432 free(be_lun, M_CTLBLK); 2433 2434 return (retval); 2435} 2436 2437static int 2438ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2439{ 2440 struct ctl_lun_rm_params *params; 2441 struct ctl_be_block_lun *be_lun; 2442 int retval; 2443 2444 params = &req->reqdata.rm; 2445 2446 mtx_lock(&softc->lock); 2447 2448 be_lun = NULL; 2449 2450 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2451 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2452 break; 2453 } 2454 mtx_unlock(&softc->lock); 2455 2456 if (be_lun == NULL) { 2457 snprintf(req->error_str, sizeof(req->error_str), 2458 "LUN %u is not managed by the block backend", 2459 params->lun_id); 2460 goto bailout_error; 2461 } 2462 2463 retval = ctl_disable_lun(&be_lun->ctl_be_lun); 2464 2465 if (retval != 0) { 2466 snprintf(req->error_str, sizeof(req->error_str), 2467 "error %d returned from ctl_disable_lun() for " 2468 "LUN %d", retval, params->lun_id); 2469 goto bailout_error; 2470 2471 } 2472 2473 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 2474 if (retval != 0) { 2475 snprintf(req->error_str, sizeof(req->error_str), 2476 "error %d returned from ctl_invalidate_lun() for " 2477 "LUN %d", retval, params->lun_id); 2478 goto bailout_error; 2479 } 2480 2481 mtx_lock(&softc->lock); 2482 2483 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2484 2485 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2486 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2487 if (retval == EINTR) 2488 break; 2489 } 2490 2491 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2492 2493 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2494 snprintf(req->error_str, sizeof(req->error_str), 2495 "interrupted waiting for LUN to be freed"); 2496 mtx_unlock(&softc->lock); 2497 goto bailout_error; 2498 } 2499 2500 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2501 2502 softc->num_luns--; 2503 mtx_unlock(&softc->lock); 2504 2505 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2506 2507 taskqueue_free(be_lun->io_taskqueue); 2508 2509 ctl_be_block_close(be_lun); 2510 2511 if (be_lun->disk_stats != NULL) 2512 devstat_remove_entry(be_lun->disk_stats); 2513 2514 uma_zdestroy(be_lun->lun_zone); 2515 2516 ctl_free_opts(&be_lun->ctl_be_lun.options); 2517 free(be_lun->dev_path, M_CTLBLK); 2518 mtx_destroy(&be_lun->queue_lock); 2519 mtx_destroy(&be_lun->io_lock); 2520 free(be_lun, M_CTLBLK); 2521 2522 req->status = CTL_LUN_OK; 2523 2524 return (0); 2525 2526bailout_error: 2527 2528 req->status = CTL_LUN_ERROR; 2529 2530 return (0); 2531} 2532 2533static int 2534ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2535 struct ctl_lun_req *req) 2536{ 2537 struct vattr vattr; 2538 int error; 2539 struct ctl_lun_create_params *params = &be_lun->params; 2540 2541 if (params->lun_size_bytes != 0) { 2542 be_lun->size_bytes = params->lun_size_bytes; 2543 } else { 2544 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2545 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2546 VOP_UNLOCK(be_lun->vn, 0); 2547 if (error != 0) { 2548 snprintf(req->error_str, sizeof(req->error_str), 2549 "error calling VOP_GETATTR() for file %s", 2550 be_lun->dev_path); 2551 return (error); 2552 } 2553 2554 be_lun->size_bytes = vattr.va_size; 2555 } 2556 2557 return (0); 2558} 2559 2560static int 2561ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2562 struct ctl_lun_req *req) 2563{ 2564 struct ctl_be_block_devdata *dev_data; 2565 int error; 2566 struct ctl_lun_create_params *params = &be_lun->params; 2567 uint64_t size_bytes; 2568 2569 dev_data = &be_lun->backend.dev; 2570 if (!dev_data->csw->d_ioctl) { 2571 snprintf(req->error_str, sizeof(req->error_str), 2572 "no d_ioctl for device %s!", be_lun->dev_path); 2573 return (ENODEV); 2574 } 2575 2576 error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE, 2577 (caddr_t)&size_bytes, FREAD, 2578 curthread); 2579 if (error) { 2580 snprintf(req->error_str, sizeof(req->error_str), 2581 "error %d returned for DIOCGMEDIASIZE ioctl " 2582 "on %s!", error, be_lun->dev_path); 2583 return (error); 2584 } 2585 2586 if (params->lun_size_bytes != 0) { 2587 if (params->lun_size_bytes > size_bytes) { 2588 snprintf(req->error_str, sizeof(req->error_str), 2589 "requested LUN size %ju > backing device " 2590 "size %ju", 2591 (uintmax_t)params->lun_size_bytes, 2592 (uintmax_t)size_bytes); 2593 return (EINVAL); 2594 } 2595 2596 be_lun->size_bytes = params->lun_size_bytes; 2597 } else { 2598 be_lun->size_bytes = size_bytes; 2599 } 2600 2601 return (0); 2602} 2603 2604static int 2605ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2606{ 2607 struct ctl_lun_modify_params *params; 2608 struct ctl_be_block_lun *be_lun; 2609 uint64_t oldsize; 2610 int error; 2611 2612 params = &req->reqdata.modify; 2613 2614 mtx_lock(&softc->lock); 2615 be_lun = NULL; 2616 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2617 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 2618 break; 2619 } 2620 mtx_unlock(&softc->lock); 2621 2622 if (be_lun == NULL) { 2623 snprintf(req->error_str, sizeof(req->error_str), 2624 "LUN %u is not managed by the block backend", 2625 params->lun_id); 2626 goto bailout_error; 2627 } 2628 2629 be_lun->params.lun_size_bytes = params->lun_size_bytes; 2630 2631 oldsize = be_lun->size_bytes; 2632 if (be_lun->vn == NULL) 2633 error = ctl_be_block_open(softc, be_lun, req); 2634 else if (vn_isdisk(be_lun->vn, &error)) 2635 error = ctl_be_block_modify_dev(be_lun, req); 2636 else if (be_lun->vn->v_type == VREG) 2637 error = ctl_be_block_modify_file(be_lun, req); 2638 else 2639 error = EINVAL; 2640 be_lun->size_blocks = be_lun->size_bytes / be_lun->blocksize; 2641 2642 if (error == 0 && be_lun->size_bytes != oldsize) { 2643 2644 /* 2645 * The maximum LBA is the size - 1. 2646 * 2647 * XXX: Note that this field is being updated without locking, 2648 * which might cause problems on 32-bit architectures. 2649 */ 2650 if (be_lun->unmap != NULL) 2651 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 2652 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ? 2653 0 : (be_lun->size_blocks - 1); 2654 be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 2655 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp; 2656 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff; 2657 be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp; 2658 be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff; 2659 be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock; 2660 be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen; 2661 ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2662 if (oldsize == 0 && be_lun->size_blocks != 0) 2663 ctl_lun_online(&be_lun->ctl_be_lun); 2664 } 2665 2666 /* Tell the user the exact size we ended up using */ 2667 params->lun_size_bytes = be_lun->size_bytes; 2668 2669 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2670 2671 return (0); 2672 2673bailout_error: 2674 req->status = CTL_LUN_ERROR; 2675 2676 return (0); 2677} 2678 2679static void 2680ctl_be_block_lun_shutdown(void *be_lun) 2681{ 2682 struct ctl_be_block_lun *lun; 2683 struct ctl_be_block_softc *softc; 2684 2685 lun = (struct ctl_be_block_lun *)be_lun; 2686 2687 softc = lun->softc; 2688 2689 mtx_lock(&softc->lock); 2690 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2691 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2692 wakeup(lun); 2693 mtx_unlock(&softc->lock); 2694 2695} 2696 2697static void 2698ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2699{ 2700 struct ctl_be_block_lun *lun; 2701 struct ctl_be_block_softc *softc; 2702 2703 lun = (struct ctl_be_block_lun *)be_lun; 2704 softc = lun->softc; 2705 2706 if (status == CTL_LUN_CONFIG_OK) { 2707 mtx_lock(&softc->lock); 2708 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2709 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2710 wakeup(lun); 2711 mtx_unlock(&softc->lock); 2712 2713 /* 2714 * We successfully added the LUN, attempt to enable it. 2715 */ 2716 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2717 printf("%s: ctl_enable_lun() failed!\n", __func__); 2718 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2719 printf("%s: ctl_invalidate_lun() failed!\n", 2720 __func__); 2721 } 2722 } 2723 2724 return; 2725 } 2726 2727 2728 mtx_lock(&softc->lock); 2729 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2730 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2731 wakeup(lun); 2732 mtx_unlock(&softc->lock); 2733} 2734 2735 2736static int 2737ctl_be_block_config_write(union ctl_io *io) 2738{ 2739 struct ctl_be_block_lun *be_lun; 2740 struct ctl_be_lun *ctl_be_lun; 2741 int retval; 2742 2743 retval = 0; 2744 2745 DPRINTF("entered\n"); 2746 2747 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2748 CTL_PRIV_BACKEND_LUN].ptr; 2749 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2750 2751 switch (io->scsiio.cdb[0]) { 2752 case SYNCHRONIZE_CACHE: 2753 case SYNCHRONIZE_CACHE_16: 2754 case WRITE_SAME_10: 2755 case WRITE_SAME_16: 2756 case UNMAP: 2757 /* 2758 * The upper level CTL code will filter out any CDBs with 2759 * the immediate bit set and return the proper error. 2760 * 2761 * We don't really need to worry about what LBA range the 2762 * user asked to be synced out. When they issue a sync 2763 * cache command, we'll sync out the whole thing. 2764 */ 2765 mtx_lock(&be_lun->queue_lock); 2766 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2767 links); 2768 mtx_unlock(&be_lun->queue_lock); 2769 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2770 break; 2771 case START_STOP_UNIT: { 2772 struct scsi_start_stop_unit *cdb; 2773 2774 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2775 2776 if (cdb->how & SSS_START) 2777 retval = ctl_start_lun(ctl_be_lun); 2778 else { 2779 retval = ctl_stop_lun(ctl_be_lun); 2780 /* 2781 * XXX KDM Copan-specific offline behavior. 2782 * Figure out a reasonable way to port this? 2783 */ 2784#ifdef NEEDTOPORT 2785 if ((retval == 0) 2786 && (cdb->byte2 & SSS_ONOFFLINE)) 2787 retval = ctl_lun_offline(ctl_be_lun); 2788#endif 2789 } 2790 2791 /* 2792 * In general, the above routines should not fail. They 2793 * just set state for the LUN. So we've got something 2794 * pretty wrong here if we can't start or stop the LUN. 2795 */ 2796 if (retval != 0) { 2797 ctl_set_internal_failure(&io->scsiio, 2798 /*sks_valid*/ 1, 2799 /*retry_count*/ 0xf051); 2800 retval = CTL_RETVAL_COMPLETE; 2801 } else { 2802 ctl_set_success(&io->scsiio); 2803 } 2804 ctl_config_write_done(io); 2805 break; 2806 } 2807 default: 2808 ctl_set_invalid_opcode(&io->scsiio); 2809 ctl_config_write_done(io); 2810 retval = CTL_RETVAL_COMPLETE; 2811 break; 2812 } 2813 2814 return (retval); 2815} 2816 2817static int 2818ctl_be_block_config_read(union ctl_io *io) 2819{ 2820 struct ctl_be_block_lun *be_lun; 2821 struct ctl_be_lun *ctl_be_lun; 2822 int retval = 0; 2823 2824 DPRINTF("entered\n"); 2825 2826 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2827 CTL_PRIV_BACKEND_LUN].ptr; 2828 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2829 2830 switch (io->scsiio.cdb[0]) { 2831 case SERVICE_ACTION_IN: 2832 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2833 mtx_lock(&be_lun->queue_lock); 2834 STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2835 &io->io_hdr, links); 2836 mtx_unlock(&be_lun->queue_lock); 2837 taskqueue_enqueue(be_lun->io_taskqueue, 2838 &be_lun->io_task); 2839 retval = CTL_RETVAL_QUEUED; 2840 break; 2841 } 2842 ctl_set_invalid_field(&io->scsiio, 2843 /*sks_valid*/ 1, 2844 /*command*/ 1, 2845 /*field*/ 1, 2846 /*bit_valid*/ 1, 2847 /*bit*/ 4); 2848 ctl_config_read_done(io); 2849 retval = CTL_RETVAL_COMPLETE; 2850 break; 2851 default: 2852 ctl_set_invalid_opcode(&io->scsiio); 2853 ctl_config_read_done(io); 2854 retval = CTL_RETVAL_COMPLETE; 2855 break; 2856 } 2857 2858 return (retval); 2859} 2860 2861static int 2862ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2863{ 2864 struct ctl_be_block_lun *lun; 2865 int retval; 2866 2867 lun = (struct ctl_be_block_lun *)be_lun; 2868 retval = 0; 2869 2870 retval = sbuf_printf(sb, "\t<num_threads>"); 2871 2872 if (retval != 0) 2873 goto bailout; 2874 2875 retval = sbuf_printf(sb, "%d", lun->num_threads); 2876 2877 if (retval != 0) 2878 goto bailout; 2879 2880 retval = sbuf_printf(sb, "</num_threads>\n"); 2881 2882bailout: 2883 2884 return (retval); 2885} 2886 2887static uint64_t 2888ctl_be_block_lun_attr(void *be_lun, const char *attrname) 2889{ 2890 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2891 2892 if (lun->getattr == NULL) 2893 return (UINT64_MAX); 2894 return (lun->getattr(lun, attrname)); 2895} 2896 2897int 2898ctl_be_block_init(void) 2899{ 2900 struct ctl_be_block_softc *softc; 2901 int retval; 2902 2903 softc = &backend_block_softc; 2904 retval = 0; 2905 2906 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2907 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2908 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2909 STAILQ_INIT(&softc->disk_list); 2910 STAILQ_INIT(&softc->lun_list); 2911 2912 return (retval); 2913} 2914