ctl_backend_block.c revision 289702
1229997Sken/*- 2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp. 3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation 4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation 5288348Smav * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org> 6229997Sken * All rights reserved. 7229997Sken * 8232604Strasz * Portions of this software were developed by Edward Tomasz Napierala 9232604Strasz * under sponsorship from the FreeBSD Foundation. 10232604Strasz * 11229997Sken * Redistribution and use in source and binary forms, with or without 12229997Sken * modification, are permitted provided that the following conditions 13229997Sken * are met: 14229997Sken * 1. Redistributions of source code must retain the above copyright 15229997Sken * notice, this list of conditions, and the following disclaimer, 16229997Sken * without modification. 17229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 18229997Sken * substantially similar to the "NO WARRANTY" disclaimer below 19229997Sken * ("Disclaimer") and any redistribution must be conditioned upon 20229997Sken * including a substantially similar Disclaimer requirement for further 21229997Sken * binary redistribution. 22229997Sken * 23229997Sken * NO WARRANTY 24229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 25229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 26229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 27229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 28229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 32229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 33229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34229997Sken * POSSIBILITY OF SUCH DAMAGES. 35229997Sken * 36229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 37229997Sken */ 38229997Sken/* 39229997Sken * CAM Target Layer driver backend for block devices. 40229997Sken * 41229997Sken * Author: Ken Merry <ken@FreeBSD.org> 42229997Sken */ 43229997Sken#include <sys/cdefs.h> 44229997Sken__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 289702 2015-10-21 15:31:26Z mav $"); 45229997Sken 46229997Sken#include <sys/param.h> 47229997Sken#include <sys/systm.h> 48229997Sken#include <sys/kernel.h> 49229997Sken#include <sys/types.h> 50229997Sken#include <sys/kthread.h> 51229997Sken#include <sys/bio.h> 52229997Sken#include <sys/fcntl.h> 53264274Smav#include <sys/limits.h> 54229997Sken#include <sys/lock.h> 55229997Sken#include <sys/mutex.h> 56229997Sken#include <sys/condvar.h> 57229997Sken#include <sys/malloc.h> 58229997Sken#include <sys/conf.h> 59229997Sken#include <sys/ioccom.h> 60229997Sken#include <sys/queue.h> 61229997Sken#include <sys/sbuf.h> 62229997Sken#include <sys/endian.h> 63229997Sken#include <sys/uio.h> 64229997Sken#include <sys/buf.h> 65229997Sken#include <sys/taskqueue.h> 66229997Sken#include <sys/vnode.h> 67229997Sken#include <sys/namei.h> 68229997Sken#include <sys/mount.h> 69229997Sken#include <sys/disk.h> 70229997Sken#include <sys/fcntl.h> 71229997Sken#include <sys/filedesc.h> 72275474Smav#include <sys/filio.h> 73229997Sken#include <sys/proc.h> 74229997Sken#include <sys/pcpu.h> 75229997Sken#include <sys/module.h> 76229997Sken#include <sys/sdt.h> 77229997Sken#include <sys/devicestat.h> 78229997Sken#include <sys/sysctl.h> 79229997Sken 80229997Sken#include <geom/geom.h> 81229997Sken 82229997Sken#include <cam/cam.h> 83229997Sken#include <cam/scsi/scsi_all.h> 84229997Sken#include <cam/scsi/scsi_da.h> 85229997Sken#include <cam/ctl/ctl_io.h> 86229997Sken#include <cam/ctl/ctl.h> 87229997Sken#include <cam/ctl/ctl_backend.h> 88229997Sken#include <cam/ctl/ctl_ioctl.h> 89287621Smav#include <cam/ctl/ctl_ha.h> 90229997Sken#include <cam/ctl/ctl_scsi_all.h> 91287621Smav#include <cam/ctl/ctl_private.h> 92229997Sken#include <cam/ctl/ctl_error.h> 93229997Sken 94229997Sken/* 95264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 96264886Smav * I/O. If we get an I/O larger than that, we'll split it. 97229997Sken */ 98267537Smav#define CTLBLK_HALF_IO_SIZE (512 * 1024) 99267537Smav#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 100264886Smav#define CTLBLK_MAX_SEG MAXPHYS 101267537Smav#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 102267537Smav#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 103229997Sken 104229997Sken#ifdef CTLBLK_DEBUG 105229997Sken#define DPRINTF(fmt, args...) \ 106229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 107229997Sken#else 108229997Sken#define DPRINTF(fmt, args...) do {} while(0) 109229997Sken#endif 110229997Sken 111267519Smav#define PRIV(io) \ 112267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 113267537Smav#define ARGS(io) \ 114267537Smav ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 115267519Smav 116229997SkenSDT_PROVIDER_DEFINE(cbb); 117229997Sken 118229997Skentypedef enum { 119229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 120229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 121229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 122229997Sken} ctl_be_block_lun_flags; 123229997Sken 124229997Skentypedef enum { 125229997Sken CTL_BE_BLOCK_NONE, 126229997Sken CTL_BE_BLOCK_DEV, 127229997Sken CTL_BE_BLOCK_FILE 128229997Sken} ctl_be_block_type; 129229997Sken 130229997Skenstruct ctl_be_block_filedata { 131229997Sken struct ucred *cred; 132229997Sken}; 133229997Sken 134229997Skenunion ctl_be_block_bedata { 135229997Sken struct ctl_be_block_filedata file; 136229997Sken}; 137229997Sken 138229997Skenstruct ctl_be_block_io; 139229997Skenstruct ctl_be_block_lun; 140229997Sken 141229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 142229997Sken struct ctl_be_block_io *beio); 143274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 144274154Smav const char *attrname); 145229997Sken 146229997Sken/* 147229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 148229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 149229997Sken */ 150229997Skenstruct ctl_be_block_lun { 151272911Smav struct ctl_lun_create_params params; 152229997Sken char lunname[32]; 153229997Sken char *dev_path; 154229997Sken ctl_be_block_type dev_type; 155229997Sken struct vnode *vn; 156229997Sken union ctl_be_block_bedata backend; 157229997Sken cbb_dispatch_t dispatch; 158229997Sken cbb_dispatch_t lun_flush; 159264274Smav cbb_dispatch_t unmap; 160275474Smav cbb_dispatch_t get_lba_status; 161274154Smav cbb_getattr_t getattr; 162229997Sken uma_zone_t lun_zone; 163229997Sken uint64_t size_blocks; 164229997Sken uint64_t size_bytes; 165229997Sken struct ctl_be_block_softc *softc; 166229997Sken struct devstat *disk_stats; 167229997Sken ctl_be_block_lun_flags flags; 168229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 169287499Smav struct ctl_be_lun cbe_lun; 170229997Sken struct taskqueue *io_taskqueue; 171229997Sken struct task io_task; 172229997Sken int num_threads; 173229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 174275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 175229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 176229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 177267877Smav struct mtx_padalign io_lock; 178267877Smav struct mtx_padalign queue_lock; 179229997Sken}; 180229997Sken 181229997Sken/* 182229997Sken * Overall softc structure for the block backend module. 183229997Sken */ 184229997Skenstruct ctl_be_block_softc { 185229997Sken struct mtx lock; 186229997Sken int num_luns; 187229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 188229997Sken}; 189229997Sken 190229997Skenstatic struct ctl_be_block_softc backend_block_softc; 191229997Sken 192229997Sken/* 193229997Sken * Per-I/O information. 194229997Sken */ 195229997Skenstruct ctl_be_block_io { 196229997Sken union ctl_io *io; 197229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 198229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 199229997Sken int bio_cmd; 200229997Sken int num_segs; 201229997Sken int num_bios_sent; 202229997Sken int num_bios_done; 203229997Sken int send_complete; 204229997Sken int num_errors; 205229997Sken struct bintime ds_t0; 206229997Sken devstat_tag_type ds_tag_type; 207229997Sken devstat_trans_flags ds_trans_type; 208229997Sken uint64_t io_len; 209229997Sken uint64_t io_offset; 210286353Smav int io_arg; 211229997Sken struct ctl_be_block_softc *softc; 212229997Sken struct ctl_be_block_lun *lun; 213264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 214229997Sken}; 215229997Sken 216287621Smavextern struct ctl_softc *control_softc; 217287621Smav 218229997Skenstatic int cbb_num_threads = 14; 219229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 220229997Sken "CAM Target Layer Block Backend"); 221267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 222229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 223229997Sken 224229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 225229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 226229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 227229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 228229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 229229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 230229997Sken struct ctl_be_block_io *beio); 231229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 232229997Sken struct ctl_be_block_io *beio); 233275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 234275474Smav struct ctl_be_block_io *beio); 235275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 236275481Smav const char *attrname); 237229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 238229997Sken struct ctl_be_block_io *beio); 239264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 240264274Smav struct ctl_be_block_io *beio); 241229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 242229997Sken struct ctl_be_block_io *beio); 243274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 244274154Smav const char *attrname); 245275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 246275474Smav union ctl_io *io); 247229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 248229997Sken union ctl_io *io); 249229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 250229997Sken union ctl_io *io); 251229997Skenstatic void ctl_be_block_worker(void *context, int pending); 252229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 253229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 254229997Sken int flag, struct thread *td); 255229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 256229997Sken struct ctl_lun_req *req); 257229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 258229997Sken struct ctl_lun_req *req); 259229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 260288348Smavstatic int ctl_be_block_open(struct ctl_be_block_lun *be_lun, 261229997Sken struct ctl_lun_req *req); 262229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 263229997Sken struct ctl_lun_req *req); 264229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 265229997Sken struct ctl_lun_req *req); 266232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 267232604Strasz struct ctl_lun_req *req); 268229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 269229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 270229997Sken ctl_lun_config_status status); 271229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 272229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 273229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 274274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 275229997Skenint ctl_be_block_init(void); 276229997Sken 277229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 278229997Sken{ 279230334Sken .name = "block", 280230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 281230334Sken .init = ctl_be_block_init, 282230334Sken .data_submit = ctl_be_block_submit, 283230334Sken .data_move_done = ctl_be_block_move_done, 284230334Sken .config_read = ctl_be_block_config_read, 285230334Sken .config_write = ctl_be_block_config_write, 286230334Sken .ioctl = ctl_be_block_ioctl, 287274154Smav .lun_info = ctl_be_block_lun_info, 288274154Smav .lun_attr = ctl_be_block_lun_attr 289229997Sken}; 290229997Sken 291229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 292229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 293229997Sken 294264020Straszstatic uma_zone_t beio_zone; 295264020Strasz 296229997Skenstatic struct ctl_be_block_io * 297229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 298229997Sken{ 299229997Sken struct ctl_be_block_io *beio; 300229997Sken 301264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 302264020Strasz beio->softc = softc; 303229997Sken return (beio); 304229997Sken} 305229997Sken 306229997Skenstatic void 307229997Skenctl_free_beio(struct ctl_be_block_io *beio) 308229997Sken{ 309229997Sken int duplicate_free; 310229997Sken int i; 311229997Sken 312229997Sken duplicate_free = 0; 313229997Sken 314229997Sken for (i = 0; i < beio->num_segs; i++) { 315229997Sken if (beio->sg_segs[i].addr == NULL) 316229997Sken duplicate_free++; 317229997Sken 318229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 319229997Sken beio->sg_segs[i].addr = NULL; 320267537Smav 321267537Smav /* For compare we had two equal S/G lists. */ 322267537Smav if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 323267537Smav uma_zfree(beio->lun->lun_zone, 324267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 325267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 326267537Smav } 327229997Sken } 328229997Sken 329229997Sken if (duplicate_free > 0) { 330229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 331229997Sken duplicate_free, beio->num_segs); 332229997Sken } 333229997Sken 334264020Strasz uma_zfree(beio_zone, beio); 335229997Sken} 336229997Sken 337229997Skenstatic void 338229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 339229997Sken{ 340267877Smav union ctl_io *io = beio->io; 341229997Sken 342264274Smav if (beio->beio_cont != NULL) { 343264274Smav beio->beio_cont(beio); 344264274Smav } else { 345264274Smav ctl_free_beio(beio); 346267537Smav ctl_data_submit_done(io); 347264274Smav } 348229997Sken} 349229997Sken 350287868Smavstatic size_t 351287868Smavcmp(uint8_t *a, uint8_t *b, size_t size) 352287868Smav{ 353287868Smav size_t i; 354287868Smav 355287868Smav for (i = 0; i < size; i++) { 356287868Smav if (a[i] != b[i]) 357287868Smav break; 358287868Smav } 359287868Smav return (i); 360287868Smav} 361287868Smav 362287868Smavstatic void 363287868Smavctl_be_block_compare(union ctl_io *io) 364287868Smav{ 365287868Smav struct ctl_be_block_io *beio; 366287868Smav uint64_t off, res; 367287868Smav int i; 368287868Smav uint8_t info[8]; 369287868Smav 370287868Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 371287868Smav off = 0; 372287868Smav for (i = 0; i < beio->num_segs; i++) { 373287868Smav res = cmp(beio->sg_segs[i].addr, 374287868Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 375287868Smav beio->sg_segs[i].len); 376287868Smav off += res; 377287868Smav if (res < beio->sg_segs[i].len) 378287868Smav break; 379287868Smav } 380287868Smav if (i < beio->num_segs) { 381287868Smav scsi_u64to8b(off, info); 382287868Smav ctl_set_sense(&io->scsiio, /*current_error*/ 1, 383287868Smav /*sense_key*/ SSD_KEY_MISCOMPARE, 384287868Smav /*asc*/ 0x1D, /*ascq*/ 0x00, 385287868Smav /*type*/ SSD_ELEM_INFO, 386287868Smav /*size*/ sizeof(info), /*data*/ &info, 387287868Smav /*type*/ SSD_ELEM_NONE); 388287868Smav } else 389287868Smav ctl_set_success(&io->scsiio); 390287868Smav} 391287868Smav 392229997Skenstatic int 393229997Skenctl_be_block_move_done(union ctl_io *io) 394229997Sken{ 395229997Sken struct ctl_be_block_io *beio; 396229997Sken struct ctl_be_block_lun *be_lun; 397267537Smav struct ctl_lba_len_flags *lbalen; 398229997Sken#ifdef CTL_TIME_IO 399229997Sken struct bintime cur_bt; 400267537Smav#endif 401229997Sken 402267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 403229997Sken be_lun = beio->lun; 404229997Sken 405229997Sken DPRINTF("entered\n"); 406229997Sken 407229997Sken#ifdef CTL_TIME_IO 408288215Smav getbinuptime(&cur_bt); 409229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 410229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 411288215Smav#endif 412229997Sken io->io_hdr.num_dmas++; 413267537Smav io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 414229997Sken 415229997Sken /* 416229997Sken * We set status at this point for read commands, and write 417229997Sken * commands with errors. 418229997Sken */ 419275058Smav if (io->io_hdr.flags & CTL_FLAG_ABORT) { 420275058Smav ; 421275058Smav } else if ((io->io_hdr.port_status == 0) && 422267537Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 423267537Smav lbalen = ARGS(beio->io); 424267537Smav if (lbalen->flags & CTL_LLF_READ) { 425267537Smav ctl_set_success(&io->scsiio); 426267537Smav } else if (lbalen->flags & CTL_LLF_COMPARE) { 427267537Smav /* We have two data blocks ready for comparison. */ 428287868Smav ctl_be_block_compare(io); 429267537Smav } 430275058Smav } else if ((io->io_hdr.port_status != 0) && 431275058Smav ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 432275058Smav (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 433229997Sken /* 434229997Sken * For hardware error sense keys, the sense key 435229997Sken * specific value is defined to be a retry count, 436229997Sken * but we use it to pass back an internal FETD 437229997Sken * error code. XXX KDM Hopefully the FETD is only 438229997Sken * using 16 bits for an error code, since that's 439229997Sken * all the space we have in the sks field. 440229997Sken */ 441229997Sken ctl_set_internal_failure(&io->scsiio, 442229997Sken /*sks_valid*/ 1, 443229997Sken /*retry_count*/ 444229997Sken io->io_hdr.port_status); 445229997Sken } 446229997Sken 447229997Sken /* 448229997Sken * If this is a read, or a write with errors, it is done. 449229997Sken */ 450229997Sken if ((beio->bio_cmd == BIO_READ) 451229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 452229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 453229997Sken ctl_complete_beio(beio); 454229997Sken return (0); 455229997Sken } 456229997Sken 457229997Sken /* 458229997Sken * At this point, we have a write and the DMA completed 459229997Sken * successfully. We now have to queue it to the task queue to 460229997Sken * execute the backend I/O. That is because we do blocking 461229997Sken * memory allocations, and in the file backing case, blocking I/O. 462229997Sken * This move done routine is generally called in the SIM's 463229997Sken * interrupt context, and therefore we cannot block. 464229997Sken */ 465267877Smav mtx_lock(&be_lun->queue_lock); 466229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 467267877Smav mtx_unlock(&be_lun->queue_lock); 468229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 469229997Sken 470229997Sken return (0); 471229997Sken} 472229997Sken 473229997Skenstatic void 474229997Skenctl_be_block_biodone(struct bio *bio) 475229997Sken{ 476229997Sken struct ctl_be_block_io *beio; 477229997Sken struct ctl_be_block_lun *be_lun; 478229997Sken union ctl_io *io; 479261538Smav int error; 480229997Sken 481229997Sken beio = bio->bio_caller1; 482229997Sken be_lun = beio->lun; 483229997Sken io = beio->io; 484229997Sken 485229997Sken DPRINTF("entered\n"); 486229997Sken 487261538Smav error = bio->bio_error; 488267877Smav mtx_lock(&be_lun->io_lock); 489261538Smav if (error != 0) 490229997Sken beio->num_errors++; 491229997Sken 492229997Sken beio->num_bios_done++; 493229997Sken 494229997Sken /* 495229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 496229997Sken * during the free might cause it to complain. 497229997Sken */ 498229997Sken g_destroy_bio(bio); 499229997Sken 500229997Sken /* 501229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 502229997Sken * complete, then we're done. 503229997Sken */ 504229997Sken if ((beio->send_complete == 0) 505229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 506267877Smav mtx_unlock(&be_lun->io_lock); 507229997Sken return; 508229997Sken } 509229997Sken 510229997Sken /* 511229997Sken * At this point, we've verified that we are the last I/O to 512229997Sken * complete, so it's safe to drop the lock. 513229997Sken */ 514267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 515267877Smav beio->ds_tag_type, beio->ds_trans_type, 516267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 517267877Smav mtx_unlock(&be_lun->io_lock); 518229997Sken 519229997Sken /* 520229997Sken * If there are any errors from the backing device, we fail the 521229997Sken * entire I/O with a medium error. 522229997Sken */ 523229997Sken if (beio->num_errors > 0) { 524261538Smav if (error == EOPNOTSUPP) { 525261538Smav ctl_set_invalid_opcode(&io->scsiio); 526282565Smav } else if (error == ENOSPC || error == EDQUOT) { 527273809Smav ctl_set_space_alloc_fail(&io->scsiio); 528287760Smav } else if (error == EROFS || error == EACCES) { 529287760Smav ctl_set_hw_write_protected(&io->scsiio); 530261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 531229997Sken /* XXX KDM is there is a better error here? */ 532229997Sken ctl_set_internal_failure(&io->scsiio, 533229997Sken /*sks_valid*/ 1, 534229997Sken /*retry_count*/ 0xbad2); 535287912Smav } else { 536287912Smav ctl_set_medium_error(&io->scsiio, 537287912Smav beio->bio_cmd == BIO_READ); 538287912Smav } 539229997Sken ctl_complete_beio(beio); 540229997Sken return; 541229997Sken } 542229997Sken 543229997Sken /* 544267537Smav * If this is a write, a flush, a delete or verify, we're all done. 545229997Sken * If this is a read, we can now send the data to the user. 546229997Sken */ 547229997Sken if ((beio->bio_cmd == BIO_WRITE) 548264274Smav || (beio->bio_cmd == BIO_FLUSH) 549267537Smav || (beio->bio_cmd == BIO_DELETE) 550267537Smav || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 551229997Sken ctl_set_success(&io->scsiio); 552229997Sken ctl_complete_beio(beio); 553229997Sken } else { 554275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 555287967Smav beio->beio_cont == NULL) { 556275058Smav ctl_set_success(&io->scsiio); 557287967Smav ctl_serseq_done(io); 558287967Smav } 559229997Sken#ifdef CTL_TIME_IO 560288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 561288215Smav#endif 562229997Sken ctl_datamove(io); 563229997Sken } 564229997Sken} 565229997Sken 566229997Skenstatic void 567229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 568229997Sken struct ctl_be_block_io *beio) 569229997Sken{ 570267877Smav union ctl_io *io = beio->io; 571229997Sken struct mount *mountpoint; 572241896Skib int error, lock_flags; 573229997Sken 574229997Sken DPRINTF("entered\n"); 575229997Sken 576267877Smav binuptime(&beio->ds_t0); 577267877Smav mtx_lock(&be_lun->io_lock); 578267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 579267877Smav mtx_unlock(&be_lun->io_lock); 580229997Sken 581267877Smav (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 582229997Sken 583288220Smav if (MNT_SHARED_WRITES(mountpoint) || 584288220Smav ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 585229997Sken lock_flags = LK_SHARED; 586229997Sken else 587229997Sken lock_flags = LK_EXCLUSIVE; 588229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 589286353Smav error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 590286353Smav curthread); 591229997Sken VOP_UNLOCK(be_lun->vn, 0); 592229997Sken 593229997Sken vn_finished_write(mountpoint); 594229997Sken 595267877Smav mtx_lock(&be_lun->io_lock); 596267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 597267877Smav beio->ds_tag_type, beio->ds_trans_type, 598267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 599267877Smav mtx_unlock(&be_lun->io_lock); 600267877Smav 601229997Sken if (error == 0) 602229997Sken ctl_set_success(&io->scsiio); 603229997Sken else { 604229997Sken /* XXX KDM is there is a better error here? */ 605229997Sken ctl_set_internal_failure(&io->scsiio, 606229997Sken /*sks_valid*/ 1, 607229997Sken /*retry_count*/ 0xbad1); 608229997Sken } 609229997Sken 610229997Sken ctl_complete_beio(beio); 611229997Sken} 612229997Sken 613258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 614258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 615258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 616258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 617229997Sken 618229997Skenstatic void 619229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 620229997Sken struct ctl_be_block_io *beio) 621229997Sken{ 622229997Sken struct ctl_be_block_filedata *file_data; 623229997Sken union ctl_io *io; 624229997Sken struct uio xuio; 625229997Sken struct iovec *xiovec; 626287875Smav size_t s; 627287875Smav int error, flags, i; 628229997Sken 629229997Sken DPRINTF("entered\n"); 630229997Sken 631229997Sken file_data = &be_lun->backend.file; 632229997Sken io = beio->io; 633271309Smav flags = 0; 634271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 635271309Smav flags |= IO_DIRECT; 636271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 637271309Smav flags |= IO_SYNC; 638229997Sken 639267537Smav bzero(&xuio, sizeof(xuio)); 640229997Sken if (beio->bio_cmd == BIO_READ) { 641229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 642267537Smav xuio.uio_rw = UIO_READ; 643229997Sken } else { 644229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 645267537Smav xuio.uio_rw = UIO_WRITE; 646229997Sken } 647229997Sken xuio.uio_offset = beio->io_offset; 648229997Sken xuio.uio_resid = beio->io_len; 649229997Sken xuio.uio_segflg = UIO_SYSSPACE; 650229997Sken xuio.uio_iov = beio->xiovecs; 651229997Sken xuio.uio_iovcnt = beio->num_segs; 652229997Sken xuio.uio_td = curthread; 653229997Sken 654229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 655229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 656229997Sken xiovec->iov_len = beio->sg_segs[i].len; 657229997Sken } 658229997Sken 659267877Smav binuptime(&beio->ds_t0); 660267877Smav mtx_lock(&be_lun->io_lock); 661267877Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 662267877Smav mtx_unlock(&be_lun->io_lock); 663267877Smav 664229997Sken if (beio->bio_cmd == BIO_READ) { 665229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 666229997Sken 667229997Sken /* 668229997Sken * UFS pays attention to IO_DIRECT for reads. If the 669229997Sken * DIRECTIO option is configured into the kernel, it calls 670229997Sken * ffs_rawread(). But that only works for single-segment 671229997Sken * uios with user space addresses. In our case, with a 672229997Sken * kernel uio, it still reads into the buffer cache, but it 673229997Sken * will just try to release the buffer from the cache later 674229997Sken * on in ffs_read(). 675229997Sken * 676229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 677229997Sken * 678229997Sken * UFS does not pay attention to IO_SYNC for reads. 679229997Sken * 680229997Sken * ZFS pays attention to IO_SYNC (which translates into the 681229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 682229997Sken * attempts to sync the file before reading. 683229997Sken */ 684271309Smav error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 685229997Sken 686229997Sken VOP_UNLOCK(be_lun->vn, 0); 687267537Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 688287875Smav if (error == 0 && xuio.uio_resid > 0) { 689287875Smav /* 690287875Smav * If we red less then requested (EOF), then 691287875Smav * we should clean the rest of the buffer. 692287875Smav */ 693287875Smav s = beio->io_len - xuio.uio_resid; 694287875Smav for (i = 0; i < beio->num_segs; i++) { 695287875Smav if (s >= beio->sg_segs[i].len) { 696287875Smav s -= beio->sg_segs[i].len; 697287875Smav continue; 698287875Smav } 699287875Smav bzero((uint8_t *)beio->sg_segs[i].addr + s, 700287875Smav beio->sg_segs[i].len - s); 701287875Smav s = 0; 702287875Smav } 703287875Smav } 704229997Sken } else { 705229997Sken struct mount *mountpoint; 706229997Sken int lock_flags; 707229997Sken 708229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 709229997Sken 710288220Smav if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL) 711229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 712229997Sken lock_flags = LK_SHARED; 713229997Sken else 714229997Sken lock_flags = LK_EXCLUSIVE; 715229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 716229997Sken 717229997Sken /* 718229997Sken * UFS pays attention to IO_DIRECT for writes. The write 719229997Sken * is done asynchronously. (Normally the write would just 720229997Sken * get put into cache. 721229997Sken * 722229997Sken * UFS pays attention to IO_SYNC for writes. It will 723229997Sken * attempt to write the buffer out synchronously if that 724229997Sken * flag is set. 725229997Sken * 726229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 727229997Sken * 728229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 729229997Sken * for writes. It will flush the transaction from the 730229997Sken * cache before returning. 731229997Sken */ 732271309Smav error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 733229997Sken VOP_UNLOCK(be_lun->vn, 0); 734229997Sken 735229997Sken vn_finished_write(mountpoint); 736267537Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 737229997Sken } 738229997Sken 739267877Smav mtx_lock(&be_lun->io_lock); 740267877Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 741267877Smav beio->ds_tag_type, beio->ds_trans_type, 742267877Smav /*now*/ NULL, /*then*/&beio->ds_t0); 743267877Smav mtx_unlock(&be_lun->io_lock); 744267877Smav 745229997Sken /* 746229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 747229997Sken * return the I/O to the user. 748229997Sken */ 749229997Sken if (error != 0) { 750282565Smav if (error == ENOSPC || error == EDQUOT) { 751273809Smav ctl_set_space_alloc_fail(&io->scsiio); 752287760Smav } else if (error == EROFS || error == EACCES) { 753287760Smav ctl_set_hw_write_protected(&io->scsiio); 754287912Smav } else { 755287912Smav ctl_set_medium_error(&io->scsiio, 756287912Smav beio->bio_cmd == BIO_READ); 757287912Smav } 758229997Sken ctl_complete_beio(beio); 759229997Sken return; 760229997Sken } 761229997Sken 762229997Sken /* 763269122Smav * If this is a write or a verify, we're all done. 764229997Sken * If this is a read, we can now send the data to the user. 765229997Sken */ 766269122Smav if ((beio->bio_cmd == BIO_WRITE) || 767269122Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 768229997Sken ctl_set_success(&io->scsiio); 769229997Sken ctl_complete_beio(beio); 770229997Sken } else { 771275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 772287967Smav beio->beio_cont == NULL) { 773275058Smav ctl_set_success(&io->scsiio); 774287967Smav ctl_serseq_done(io); 775287967Smav } 776229997Sken#ifdef CTL_TIME_IO 777288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 778288215Smav#endif 779229997Sken ctl_datamove(io); 780229997Sken } 781229997Sken} 782229997Sken 783229997Skenstatic void 784275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 785275474Smav struct ctl_be_block_io *beio) 786275474Smav{ 787275474Smav union ctl_io *io = beio->io; 788275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 789275474Smav struct scsi_get_lba_status_data *data; 790275474Smav off_t roff, off; 791275474Smav int error, status; 792275474Smav 793275474Smav DPRINTF("entered\n"); 794275474Smav 795287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 796275474Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 797275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 798275474Smav 0, curthread->td_ucred, curthread); 799275474Smav if (error == 0 && off > roff) 800275474Smav status = 0; /* mapped up to off */ 801275474Smav else { 802275474Smav error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 803275474Smav 0, curthread->td_ucred, curthread); 804275474Smav if (error == 0 && off > roff) 805275474Smav status = 1; /* deallocated up to off */ 806275474Smav else { 807275474Smav status = 0; /* unknown up to the end */ 808275474Smav off = be_lun->size_bytes; 809275474Smav } 810275474Smav } 811275474Smav VOP_UNLOCK(be_lun->vn, 0); 812275474Smav 813275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 814275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 815287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 816287499Smav lbalen->lba), data->descr[0].length); 817275474Smav data->descr[0].status = status; 818275474Smav 819275474Smav ctl_complete_beio(beio); 820275474Smav} 821275474Smav 822275481Smavstatic uint64_t 823275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 824275481Smav{ 825275481Smav struct vattr vattr; 826275481Smav struct statfs statfs; 827285030Smav uint64_t val; 828275481Smav int error; 829275481Smav 830285030Smav val = UINT64_MAX; 831275481Smav if (be_lun->vn == NULL) 832285030Smav return (val); 833285030Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 834275481Smav if (strcmp(attrname, "blocksused") == 0) { 835275481Smav error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 836285030Smav if (error == 0) 837287499Smav val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 838275481Smav } 839285030Smav if (strcmp(attrname, "blocksavail") == 0 && 840285030Smav (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 841275481Smav error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 842285030Smav if (error == 0) 843286811Smav val = statfs.f_bavail * statfs.f_bsize / 844287499Smav be_lun->cbe_lun.blocksize; 845275481Smav } 846285030Smav VOP_UNLOCK(be_lun->vn, 0); 847285030Smav return (val); 848275481Smav} 849275481Smav 850275474Smavstatic void 851269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 852269123Smav struct ctl_be_block_io *beio) 853269123Smav{ 854269123Smav union ctl_io *io; 855287664Smav struct cdevsw *csw; 856287664Smav struct cdev *dev; 857269123Smav struct uio xuio; 858269123Smav struct iovec *xiovec; 859287664Smav int error, flags, i, ref; 860269123Smav 861269123Smav DPRINTF("entered\n"); 862269123Smav 863269123Smav io = beio->io; 864271309Smav flags = 0; 865271309Smav if (ARGS(io)->flags & CTL_LLF_DPO) 866271309Smav flags |= IO_DIRECT; 867271309Smav if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 868271309Smav flags |= IO_SYNC; 869269123Smav 870269123Smav bzero(&xuio, sizeof(xuio)); 871269123Smav if (beio->bio_cmd == BIO_READ) { 872269123Smav SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 873269123Smav xuio.uio_rw = UIO_READ; 874269123Smav } else { 875269123Smav SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 876269123Smav xuio.uio_rw = UIO_WRITE; 877269123Smav } 878269123Smav xuio.uio_offset = beio->io_offset; 879269123Smav xuio.uio_resid = beio->io_len; 880269123Smav xuio.uio_segflg = UIO_SYSSPACE; 881269123Smav xuio.uio_iov = beio->xiovecs; 882269123Smav xuio.uio_iovcnt = beio->num_segs; 883269123Smav xuio.uio_td = curthread; 884269123Smav 885269123Smav for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 886269123Smav xiovec->iov_base = beio->sg_segs[i].addr; 887269123Smav xiovec->iov_len = beio->sg_segs[i].len; 888269123Smav } 889269123Smav 890269123Smav binuptime(&beio->ds_t0); 891269123Smav mtx_lock(&be_lun->io_lock); 892269123Smav devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 893269123Smav mtx_unlock(&be_lun->io_lock); 894269123Smav 895287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 896287664Smav if (csw) { 897287664Smav if (beio->bio_cmd == BIO_READ) 898287664Smav error = csw->d_read(dev, &xuio, flags); 899287664Smav else 900287664Smav error = csw->d_write(dev, &xuio, flags); 901287664Smav dev_relthread(dev, ref); 902287664Smav } else 903287664Smav error = ENXIO; 904287664Smav 905287664Smav if (beio->bio_cmd == BIO_READ) 906269123Smav SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 907287664Smav else 908269123Smav SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 909269123Smav 910269123Smav mtx_lock(&be_lun->io_lock); 911269123Smav devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 912269123Smav beio->ds_tag_type, beio->ds_trans_type, 913269123Smav /*now*/ NULL, /*then*/&beio->ds_t0); 914269123Smav mtx_unlock(&be_lun->io_lock); 915269123Smav 916269123Smav /* 917269123Smav * If we got an error, set the sense data to "MEDIUM ERROR" and 918269123Smav * return the I/O to the user. 919269123Smav */ 920269123Smav if (error != 0) { 921282565Smav if (error == ENOSPC || error == EDQUOT) { 922273809Smav ctl_set_space_alloc_fail(&io->scsiio); 923287760Smav } else if (error == EROFS || error == EACCES) { 924287760Smav ctl_set_hw_write_protected(&io->scsiio); 925287912Smav } else { 926287912Smav ctl_set_medium_error(&io->scsiio, 927287912Smav beio->bio_cmd == BIO_READ); 928287912Smav } 929269123Smav ctl_complete_beio(beio); 930269123Smav return; 931269123Smav } 932269123Smav 933269123Smav /* 934269123Smav * If this is a write or a verify, we're all done. 935269123Smav * If this is a read, we can now send the data to the user. 936269123Smav */ 937269123Smav if ((beio->bio_cmd == BIO_WRITE) || 938269123Smav (ARGS(io)->flags & CTL_LLF_VERIFY)) { 939269123Smav ctl_set_success(&io->scsiio); 940269123Smav ctl_complete_beio(beio); 941269123Smav } else { 942275058Smav if ((ARGS(io)->flags & CTL_LLF_READ) && 943287967Smav beio->beio_cont == NULL) { 944275058Smav ctl_set_success(&io->scsiio); 945287967Smav ctl_serseq_done(io); 946287967Smav } 947269123Smav#ifdef CTL_TIME_IO 948288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 949288215Smav#endif 950269123Smav ctl_datamove(io); 951269123Smav } 952269123Smav} 953269123Smav 954269123Smavstatic void 955275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 956275474Smav struct ctl_be_block_io *beio) 957275474Smav{ 958275474Smav union ctl_io *io = beio->io; 959287664Smav struct cdevsw *csw; 960287664Smav struct cdev *dev; 961275474Smav struct ctl_lba_len_flags *lbalen = ARGS(io); 962275474Smav struct scsi_get_lba_status_data *data; 963275474Smav off_t roff, off; 964287664Smav int error, ref, status; 965275474Smav 966275474Smav DPRINTF("entered\n"); 967275474Smav 968287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 969287664Smav if (csw == NULL) { 970287664Smav status = 0; /* unknown up to the end */ 971287664Smav off = be_lun->size_bytes; 972287664Smav goto done; 973287664Smav } 974287499Smav off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 975287664Smav error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 976287664Smav curthread); 977275474Smav if (error == 0 && off > roff) 978275474Smav status = 0; /* mapped up to off */ 979275474Smav else { 980287664Smav error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 981287664Smav curthread); 982275474Smav if (error == 0 && off > roff) 983275474Smav status = 1; /* deallocated up to off */ 984275474Smav else { 985275474Smav status = 0; /* unknown up to the end */ 986275474Smav off = be_lun->size_bytes; 987275474Smav } 988275474Smav } 989287664Smav dev_relthread(dev, ref); 990275474Smav 991287664Smavdone: 992275474Smav data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 993275474Smav scsi_u64to8b(lbalen->lba, data->descr[0].addr); 994287499Smav scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 995287499Smav lbalen->lba), data->descr[0].length); 996275474Smav data->descr[0].status = status; 997275474Smav 998275474Smav ctl_complete_beio(beio); 999275474Smav} 1000275474Smav 1001275474Smavstatic void 1002229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 1003229997Sken struct ctl_be_block_io *beio) 1004229997Sken{ 1005229997Sken struct bio *bio; 1006287664Smav struct cdevsw *csw; 1007287664Smav struct cdev *dev; 1008287664Smav int ref; 1009229997Sken 1010229997Sken DPRINTF("entered\n"); 1011229997Sken 1012229997Sken /* This can't fail, it's a blocking allocation. */ 1013229997Sken bio = g_alloc_bio(); 1014229997Sken 1015229997Sken bio->bio_cmd = BIO_FLUSH; 1016229997Sken bio->bio_offset = 0; 1017229997Sken bio->bio_data = 0; 1018229997Sken bio->bio_done = ctl_be_block_biodone; 1019229997Sken bio->bio_caller1 = beio; 1020229997Sken bio->bio_pblkno = 0; 1021229997Sken 1022229997Sken /* 1023229997Sken * We don't need to acquire the LUN lock here, because we are only 1024229997Sken * sending one bio, and so there is no other context to synchronize 1025229997Sken * with. 1026229997Sken */ 1027229997Sken beio->num_bios_sent = 1; 1028229997Sken beio->send_complete = 1; 1029229997Sken 1030229997Sken binuptime(&beio->ds_t0); 1031267877Smav mtx_lock(&be_lun->io_lock); 1032229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1033267877Smav mtx_unlock(&be_lun->io_lock); 1034229997Sken 1035287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1036287664Smav if (csw) { 1037287664Smav bio->bio_dev = dev; 1038287664Smav csw->d_strategy(bio); 1039287664Smav dev_relthread(dev, ref); 1040287664Smav } else { 1041287664Smav bio->bio_error = ENXIO; 1042287664Smav ctl_be_block_biodone(bio); 1043287664Smav } 1044229997Sken} 1045229997Sken 1046229997Skenstatic void 1047264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1048264274Smav struct ctl_be_block_io *beio, 1049264274Smav uint64_t off, uint64_t len, int last) 1050264274Smav{ 1051264274Smav struct bio *bio; 1052264296Smav uint64_t maxlen; 1053287664Smav struct cdevsw *csw; 1054287664Smav struct cdev *dev; 1055287664Smav int ref; 1056264274Smav 1057287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1058287499Smav maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1059264274Smav while (len > 0) { 1060264274Smav bio = g_alloc_bio(); 1061264274Smav bio->bio_cmd = BIO_DELETE; 1062287664Smav bio->bio_dev = dev; 1063264274Smav bio->bio_offset = off; 1064264296Smav bio->bio_length = MIN(len, maxlen); 1065264274Smav bio->bio_data = 0; 1066264274Smav bio->bio_done = ctl_be_block_biodone; 1067264274Smav bio->bio_caller1 = beio; 1068287499Smav bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1069264274Smav 1070264274Smav off += bio->bio_length; 1071264274Smav len -= bio->bio_length; 1072264274Smav 1073267877Smav mtx_lock(&be_lun->io_lock); 1074264274Smav beio->num_bios_sent++; 1075264274Smav if (last && len == 0) 1076264274Smav beio->send_complete = 1; 1077267877Smav mtx_unlock(&be_lun->io_lock); 1078264274Smav 1079287664Smav if (csw) { 1080287664Smav csw->d_strategy(bio); 1081287664Smav } else { 1082287664Smav bio->bio_error = ENXIO; 1083287664Smav ctl_be_block_biodone(bio); 1084287664Smav } 1085264274Smav } 1086287664Smav if (csw) 1087287664Smav dev_relthread(dev, ref); 1088264274Smav} 1089264274Smav 1090264274Smavstatic void 1091264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1092264274Smav struct ctl_be_block_io *beio) 1093264274Smav{ 1094264274Smav union ctl_io *io; 1095267515Smav struct ctl_ptr_len_flags *ptrlen; 1096264274Smav struct scsi_unmap_desc *buf, *end; 1097264274Smav uint64_t len; 1098264274Smav 1099264274Smav io = beio->io; 1100264274Smav 1101264274Smav DPRINTF("entered\n"); 1102264274Smav 1103264274Smav binuptime(&beio->ds_t0); 1104267877Smav mtx_lock(&be_lun->io_lock); 1105264274Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1106267877Smav mtx_unlock(&be_lun->io_lock); 1107264274Smav 1108264274Smav if (beio->io_offset == -1) { 1109264274Smav beio->io_len = 0; 1110267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1111267515Smav buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1112267515Smav end = buf + ptrlen->len / sizeof(*buf); 1113264274Smav for (; buf < end; buf++) { 1114264274Smav len = (uint64_t)scsi_4btoul(buf->length) * 1115287499Smav be_lun->cbe_lun.blocksize; 1116264274Smav beio->io_len += len; 1117264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1118287499Smav scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 1119287499Smav len, (end - buf < 2) ? TRUE : FALSE); 1120264274Smav } 1121264274Smav } else 1122264274Smav ctl_be_block_unmap_dev_range(be_lun, beio, 1123264274Smav beio->io_offset, beio->io_len, TRUE); 1124264274Smav} 1125264274Smav 1126264274Smavstatic void 1127229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1128229997Sken struct ctl_be_block_io *beio) 1129229997Sken{ 1130267877Smav TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1131229997Sken struct bio *bio; 1132287664Smav struct cdevsw *csw; 1133287664Smav struct cdev *dev; 1134229997Sken off_t cur_offset; 1135287664Smav int i, max_iosize, ref; 1136229997Sken 1137229997Sken DPRINTF("entered\n"); 1138287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1139229997Sken 1140229997Sken /* 1141229997Sken * We have to limit our I/O size to the maximum supported by the 1142229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1143229997Sken * set it properly, use DFLTPHYS. 1144229997Sken */ 1145287664Smav if (csw) { 1146287664Smav max_iosize = dev->si_iosize_max; 1147287664Smav if (max_iosize < PAGE_SIZE) 1148287664Smav max_iosize = DFLTPHYS; 1149287664Smav } else 1150229997Sken max_iosize = DFLTPHYS; 1151229997Sken 1152229997Sken cur_offset = beio->io_offset; 1153229997Sken for (i = 0; i < beio->num_segs; i++) { 1154229997Sken size_t cur_size; 1155229997Sken uint8_t *cur_ptr; 1156229997Sken 1157229997Sken cur_size = beio->sg_segs[i].len; 1158229997Sken cur_ptr = beio->sg_segs[i].addr; 1159229997Sken 1160229997Sken while (cur_size > 0) { 1161229997Sken /* This can't fail, it's a blocking allocation. */ 1162229997Sken bio = g_alloc_bio(); 1163229997Sken 1164229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1165229997Sken 1166229997Sken bio->bio_cmd = beio->bio_cmd; 1167287664Smav bio->bio_dev = dev; 1168229997Sken bio->bio_caller1 = beio; 1169229997Sken bio->bio_length = min(cur_size, max_iosize); 1170229997Sken bio->bio_offset = cur_offset; 1171229997Sken bio->bio_data = cur_ptr; 1172229997Sken bio->bio_done = ctl_be_block_biodone; 1173287499Smav bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1174229997Sken 1175229997Sken cur_offset += bio->bio_length; 1176229997Sken cur_ptr += bio->bio_length; 1177229997Sken cur_size -= bio->bio_length; 1178229997Sken 1179267877Smav TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1180229997Sken beio->num_bios_sent++; 1181229997Sken } 1182229997Sken } 1183267877Smav binuptime(&beio->ds_t0); 1184267877Smav mtx_lock(&be_lun->io_lock); 1185267877Smav devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1186267877Smav beio->send_complete = 1; 1187267877Smav mtx_unlock(&be_lun->io_lock); 1188267877Smav 1189267877Smav /* 1190267877Smav * Fire off all allocated requests! 1191267877Smav */ 1192267877Smav while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1193267877Smav TAILQ_REMOVE(&queue, bio, bio_queue); 1194287664Smav if (csw) 1195287664Smav csw->d_strategy(bio); 1196287664Smav else { 1197287664Smav bio->bio_error = ENXIO; 1198287664Smav ctl_be_block_biodone(bio); 1199287664Smav } 1200267877Smav } 1201287664Smav if (csw) 1202287664Smav dev_relthread(dev, ref); 1203229997Sken} 1204229997Sken 1205274154Smavstatic uint64_t 1206274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1207274154Smav{ 1208274154Smav struct diocgattr_arg arg; 1209287664Smav struct cdevsw *csw; 1210287664Smav struct cdev *dev; 1211287664Smav int error, ref; 1212274154Smav 1213287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1214287664Smav if (csw == NULL) 1215274154Smav return (UINT64_MAX); 1216274154Smav strlcpy(arg.name, attrname, sizeof(arg.name)); 1217274154Smav arg.len = sizeof(arg.value.off); 1218287664Smav if (csw->d_ioctl) { 1219287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 1220287664Smav curthread); 1221287664Smav } else 1222287664Smav error = ENODEV; 1223287664Smav dev_relthread(dev, ref); 1224274154Smav if (error != 0) 1225274154Smav return (UINT64_MAX); 1226274154Smav return (arg.value.off); 1227274154Smav} 1228274154Smav 1229229997Skenstatic void 1230286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1231286353Smav union ctl_io *io) 1232286353Smav{ 1233287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1234286353Smav struct ctl_be_block_io *beio; 1235286353Smav struct ctl_lba_len_flags *lbalen; 1236286353Smav 1237286353Smav DPRINTF("entered\n"); 1238286353Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1239286353Smav lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1240286353Smav 1241287499Smav beio->io_len = lbalen->len * cbe_lun->blocksize; 1242287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1243286353Smav beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1244286353Smav beio->bio_cmd = BIO_FLUSH; 1245286353Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1246286353Smav DPRINTF("SYNC\n"); 1247286353Smav be_lun->lun_flush(be_lun, beio); 1248286353Smav} 1249286353Smav 1250286353Smavstatic void 1251264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1252264274Smav{ 1253264274Smav union ctl_io *io; 1254264274Smav 1255264274Smav io = beio->io; 1256264274Smav ctl_free_beio(beio); 1257267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1258267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1259267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1260264274Smav ctl_config_write_done(io); 1261264274Smav return; 1262264274Smav } 1263264274Smav 1264264274Smav ctl_be_block_config_write(io); 1265264274Smav} 1266264274Smav 1267264274Smavstatic void 1268264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1269264274Smav union ctl_io *io) 1270264274Smav{ 1271287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1272264274Smav struct ctl_be_block_io *beio; 1273267515Smav struct ctl_lba_len_flags *lbalen; 1274278625Smav uint64_t len_left, lba; 1275278625Smav uint32_t pb, pbo, adj; 1276264274Smav int i, seglen; 1277264274Smav uint8_t *buf, *end; 1278264274Smav 1279264274Smav DPRINTF("entered\n"); 1280264274Smav 1281267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1282267537Smav lbalen = ARGS(beio->io); 1283264274Smav 1284271839Smav if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1285269622Smav (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1286264274Smav ctl_free_beio(beio); 1287264274Smav ctl_set_invalid_field(&io->scsiio, 1288264274Smav /*sks_valid*/ 1, 1289264274Smav /*command*/ 1, 1290264274Smav /*field*/ 1, 1291264274Smav /*bit_valid*/ 0, 1292264274Smav /*bit*/ 0); 1293264274Smav ctl_config_write_done(io); 1294264274Smav return; 1295264274Smav } 1296264274Smav 1297269622Smav if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1298287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1299287499Smav beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1300264274Smav beio->bio_cmd = BIO_DELETE; 1301264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1302264274Smav 1303264274Smav be_lun->unmap(be_lun, beio); 1304264274Smav return; 1305264274Smav } 1306264274Smav 1307264274Smav beio->bio_cmd = BIO_WRITE; 1308264274Smav beio->ds_trans_type = DEVSTAT_WRITE; 1309264274Smav 1310264274Smav DPRINTF("WRITE SAME at LBA %jx len %u\n", 1311267515Smav (uintmax_t)lbalen->lba, lbalen->len); 1312264274Smav 1313287499Smav pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1314287499Smav if (be_lun->cbe_lun.pblockoff > 0) 1315287499Smav pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1316278625Smav else 1317278625Smav pbo = 0; 1318287499Smav len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1319264274Smav for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1320264274Smav 1321264274Smav /* 1322264274Smav * Setup the S/G entry for this chunk. 1323264274Smav */ 1324264886Smav seglen = MIN(CTLBLK_MAX_SEG, len_left); 1325287499Smav if (pb > cbe_lun->blocksize) { 1326287499Smav adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1327278619Smav seglen - pbo) % pb; 1328278619Smav if (seglen > adj) 1329278619Smav seglen -= adj; 1330278619Smav else 1331287499Smav seglen -= seglen % cbe_lun->blocksize; 1332278619Smav } else 1333287499Smav seglen -= seglen % cbe_lun->blocksize; 1334264274Smav beio->sg_segs[i].len = seglen; 1335264274Smav beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1336264274Smav 1337264274Smav DPRINTF("segment %d addr %p len %zd\n", i, 1338264274Smav beio->sg_segs[i].addr, beio->sg_segs[i].len); 1339264274Smav 1340264274Smav beio->num_segs++; 1341264274Smav len_left -= seglen; 1342264274Smav 1343264274Smav buf = beio->sg_segs[i].addr; 1344264274Smav end = buf + seglen; 1345287499Smav for (; buf < end; buf += cbe_lun->blocksize) { 1346288175Smav if (lbalen->flags & SWS_NDOB) { 1347288175Smav memset(buf, 0, cbe_lun->blocksize); 1348288175Smav } else { 1349288175Smav memcpy(buf, io->scsiio.kern_data_ptr, 1350288175Smav cbe_lun->blocksize); 1351288175Smav } 1352267515Smav if (lbalen->flags & SWS_LBDATA) 1353267515Smav scsi_ulto4b(lbalen->lba + lba, buf); 1354264274Smav lba++; 1355264274Smav } 1356264274Smav } 1357264274Smav 1358287499Smav beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1359287499Smav beio->io_len = lba * cbe_lun->blocksize; 1360264274Smav 1361264274Smav /* We can not do all in one run. Correct and schedule rerun. */ 1362264274Smav if (len_left > 0) { 1363267515Smav lbalen->lba += lba; 1364267515Smav lbalen->len -= lba; 1365264274Smav beio->beio_cont = ctl_be_block_cw_done_ws; 1366264274Smav } 1367264274Smav 1368264274Smav be_lun->dispatch(be_lun, beio); 1369264274Smav} 1370264274Smav 1371264274Smavstatic void 1372264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1373264274Smav union ctl_io *io) 1374264274Smav{ 1375264274Smav struct ctl_be_block_io *beio; 1376267515Smav struct ctl_ptr_len_flags *ptrlen; 1377264274Smav 1378264274Smav DPRINTF("entered\n"); 1379264274Smav 1380267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1381267515Smav ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1382264274Smav 1383269622Smav if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1384264274Smav ctl_free_beio(beio); 1385264274Smav ctl_set_invalid_field(&io->scsiio, 1386264274Smav /*sks_valid*/ 0, 1387264274Smav /*command*/ 1, 1388264274Smav /*field*/ 0, 1389264274Smav /*bit_valid*/ 0, 1390264274Smav /*bit*/ 0); 1391264274Smav ctl_config_write_done(io); 1392264274Smav return; 1393264274Smav } 1394264274Smav 1395264274Smav beio->io_len = 0; 1396264274Smav beio->io_offset = -1; 1397264274Smav beio->bio_cmd = BIO_DELETE; 1398264274Smav beio->ds_trans_type = DEVSTAT_FREE; 1399267515Smav DPRINTF("UNMAP\n"); 1400264274Smav be_lun->unmap(be_lun, beio); 1401264274Smav} 1402264274Smav 1403264274Smavstatic void 1404275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio) 1405275474Smav{ 1406275474Smav union ctl_io *io; 1407275474Smav 1408275474Smav io = beio->io; 1409275474Smav ctl_free_beio(beio); 1410275474Smav ctl_config_read_done(io); 1411275474Smav} 1412275474Smav 1413275474Smavstatic void 1414275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1415275474Smav union ctl_io *io) 1416275474Smav{ 1417275474Smav struct ctl_be_block_io *beio; 1418275474Smav struct ctl_be_block_softc *softc; 1419275474Smav 1420275474Smav DPRINTF("entered\n"); 1421275474Smav 1422275474Smav softc = be_lun->softc; 1423275474Smav beio = ctl_alloc_beio(softc); 1424275474Smav beio->io = io; 1425275474Smav beio->lun = be_lun; 1426275474Smav beio->beio_cont = ctl_be_block_cr_done; 1427275474Smav PRIV(io)->ptr = (void *)beio; 1428275474Smav 1429275474Smav switch (io->scsiio.cdb[0]) { 1430275474Smav case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1431275474Smav beio->bio_cmd = -1; 1432275474Smav beio->ds_trans_type = DEVSTAT_NO_DATA; 1433275474Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1434275474Smav beio->io_len = 0; 1435275474Smav if (be_lun->get_lba_status) 1436275474Smav be_lun->get_lba_status(be_lun, beio); 1437275474Smav else 1438275474Smav ctl_be_block_cr_done(beio); 1439275474Smav break; 1440275474Smav default: 1441275474Smav panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1442275474Smav break; 1443275474Smav } 1444275474Smav} 1445275474Smav 1446275474Smavstatic void 1447264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio) 1448264274Smav{ 1449264274Smav union ctl_io *io; 1450264274Smav 1451264274Smav io = beio->io; 1452264274Smav ctl_free_beio(beio); 1453264274Smav ctl_config_write_done(io); 1454264274Smav} 1455264274Smav 1456264274Smavstatic void 1457229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1458229997Sken union ctl_io *io) 1459229997Sken{ 1460229997Sken struct ctl_be_block_io *beio; 1461229997Sken struct ctl_be_block_softc *softc; 1462229997Sken 1463229997Sken DPRINTF("entered\n"); 1464229997Sken 1465229997Sken softc = be_lun->softc; 1466229997Sken beio = ctl_alloc_beio(softc); 1467229997Sken beio->io = io; 1468229997Sken beio->lun = be_lun; 1469264274Smav beio->beio_cont = ctl_be_block_cw_done; 1470286353Smav switch (io->scsiio.tag_type) { 1471286353Smav case CTL_TAG_ORDERED: 1472286353Smav beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1473286353Smav break; 1474286353Smav case CTL_TAG_HEAD_OF_QUEUE: 1475286353Smav beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1476286353Smav break; 1477286353Smav case CTL_TAG_UNTAGGED: 1478286353Smav case CTL_TAG_SIMPLE: 1479286353Smav case CTL_TAG_ACA: 1480286353Smav default: 1481286353Smav beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1482286353Smav break; 1483286353Smav } 1484267519Smav PRIV(io)->ptr = (void *)beio; 1485229997Sken 1486229997Sken switch (io->scsiio.cdb[0]) { 1487229997Sken case SYNCHRONIZE_CACHE: 1488229997Sken case SYNCHRONIZE_CACHE_16: 1489286353Smav ctl_be_block_cw_dispatch_sync(be_lun, io); 1490229997Sken break; 1491264274Smav case WRITE_SAME_10: 1492264274Smav case WRITE_SAME_16: 1493264274Smav ctl_be_block_cw_dispatch_ws(be_lun, io); 1494264274Smav break; 1495264274Smav case UNMAP: 1496264274Smav ctl_be_block_cw_dispatch_unmap(be_lun, io); 1497264274Smav break; 1498229997Sken default: 1499229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1500229997Sken break; 1501229997Sken } 1502229997Sken} 1503229997Sken 1504258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1505258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1506258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1507258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1508229997Sken 1509229997Skenstatic void 1510264886Smavctl_be_block_next(struct ctl_be_block_io *beio) 1511264886Smav{ 1512264886Smav struct ctl_be_block_lun *be_lun; 1513264886Smav union ctl_io *io; 1514264886Smav 1515264886Smav io = beio->io; 1516264886Smav be_lun = beio->lun; 1517264886Smav ctl_free_beio(beio); 1518267641Smav if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1519267641Smav ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1520267641Smav (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1521267537Smav ctl_data_submit_done(io); 1522264886Smav return; 1523264886Smav } 1524264886Smav 1525264886Smav io->io_hdr.status &= ~CTL_STATUS_MASK; 1526264886Smav io->io_hdr.status |= CTL_STATUS_NONE; 1527264886Smav 1528267877Smav mtx_lock(&be_lun->queue_lock); 1529264886Smav STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1530267877Smav mtx_unlock(&be_lun->queue_lock); 1531264886Smav taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1532264886Smav} 1533264886Smav 1534264886Smavstatic void 1535229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1536229997Sken union ctl_io *io) 1537229997Sken{ 1538287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1539229997Sken struct ctl_be_block_io *beio; 1540229997Sken struct ctl_be_block_softc *softc; 1541267537Smav struct ctl_lba_len_flags *lbalen; 1542267519Smav struct ctl_ptr_len_flags *bptrlen; 1543267519Smav uint64_t len_left, lbas; 1544229997Sken int i; 1545229997Sken 1546229997Sken softc = be_lun->softc; 1547229997Sken 1548229997Sken DPRINTF("entered\n"); 1549229997Sken 1550267537Smav lbalen = ARGS(io); 1551267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1552267537Smav SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1553267537Smav } else { 1554229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1555229997Sken } 1556229997Sken 1557229997Sken beio = ctl_alloc_beio(softc); 1558229997Sken beio->io = io; 1559229997Sken beio->lun = be_lun; 1560267519Smav bptrlen = PRIV(io); 1561267519Smav bptrlen->ptr = (void *)beio; 1562229997Sken 1563229997Sken switch (io->scsiio.tag_type) { 1564229997Sken case CTL_TAG_ORDERED: 1565229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1566229997Sken break; 1567229997Sken case CTL_TAG_HEAD_OF_QUEUE: 1568229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1569229997Sken break; 1570229997Sken case CTL_TAG_UNTAGGED: 1571229997Sken case CTL_TAG_SIMPLE: 1572229997Sken case CTL_TAG_ACA: 1573229997Sken default: 1574229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1575229997Sken break; 1576229997Sken } 1577229997Sken 1578267537Smav if (lbalen->flags & CTL_LLF_WRITE) { 1579267537Smav beio->bio_cmd = BIO_WRITE; 1580267537Smav beio->ds_trans_type = DEVSTAT_WRITE; 1581267537Smav } else { 1582229997Sken beio->bio_cmd = BIO_READ; 1583229997Sken beio->ds_trans_type = DEVSTAT_READ; 1584229997Sken } 1585229997Sken 1586264886Smav DPRINTF("%s at LBA %jx len %u @%ju\n", 1587229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1588267519Smav (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1589267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1590267537Smav lbas = CTLBLK_HALF_IO_SIZE; 1591267537Smav else 1592267537Smav lbas = CTLBLK_MAX_IO_SIZE; 1593287499Smav lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 1594287499Smav beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 1595287499Smav beio->io_len = lbas * cbe_lun->blocksize; 1596267519Smav bptrlen->len += lbas; 1597229997Sken 1598264886Smav for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1599264886Smav KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1600264886Smav i, CTLBLK_MAX_SEGS)); 1601229997Sken 1602229997Sken /* 1603229997Sken * Setup the S/G entry for this chunk. 1604229997Sken */ 1605264886Smav beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1606229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1607229997Sken 1608229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 1609229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 1610229997Sken 1611267537Smav /* Set up second segment for compare operation. */ 1612267537Smav if (lbalen->flags & CTL_LLF_COMPARE) { 1613267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1614267537Smav beio->sg_segs[i].len; 1615267537Smav beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1616267537Smav uma_zalloc(be_lun->lun_zone, M_WAITOK); 1617267537Smav } 1618267537Smav 1619229997Sken beio->num_segs++; 1620229997Sken len_left -= beio->sg_segs[i].len; 1621229997Sken } 1622267519Smav if (bptrlen->len < lbalen->len) 1623264886Smav beio->beio_cont = ctl_be_block_next; 1624264886Smav io->scsiio.be_move_done = ctl_be_block_move_done; 1625267537Smav /* For compare we have separate S/G lists for read and datamove. */ 1626267537Smav if (lbalen->flags & CTL_LLF_COMPARE) 1627267537Smav io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1628267537Smav else 1629267537Smav io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1630264886Smav io->scsiio.kern_data_len = beio->io_len; 1631264886Smav io->scsiio.kern_data_resid = 0; 1632264886Smav io->scsiio.kern_sg_entries = beio->num_segs; 1633288020Smav io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 1634229997Sken 1635229997Sken /* 1636229997Sken * For the read case, we need to read the data into our buffers and 1637229997Sken * then we can send it back to the user. For the write case, we 1638229997Sken * need to get the data from the user first. 1639229997Sken */ 1640229997Sken if (beio->bio_cmd == BIO_READ) { 1641229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1642229997Sken be_lun->dispatch(be_lun, beio); 1643229997Sken } else { 1644229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1645229997Sken#ifdef CTL_TIME_IO 1646288215Smav getbinuptime(&io->io_hdr.dma_start_bt); 1647288215Smav#endif 1648229997Sken ctl_datamove(io); 1649229997Sken } 1650229997Sken} 1651229997Sken 1652229997Skenstatic void 1653229997Skenctl_be_block_worker(void *context, int pending) 1654229997Sken{ 1655287670Smav struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context; 1656287670Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1657229997Sken union ctl_io *io; 1658287670Smav struct ctl_be_block_io *beio; 1659229997Sken 1660229997Sken DPRINTF("entered\n"); 1661287670Smav /* 1662287670Smav * Fetch and process I/Os from all queues. If we detect LUN 1663288348Smav * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race, 1664287670Smav * so make response maximally opaque to not confuse initiator. 1665287670Smav */ 1666229997Sken for (;;) { 1667287670Smav mtx_lock(&be_lun->queue_lock); 1668229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1669229997Sken if (io != NULL) { 1670229997Sken DPRINTF("datamove queue\n"); 1671229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1672229997Sken ctl_io_hdr, links); 1673267877Smav mtx_unlock(&be_lun->queue_lock); 1674267519Smav beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1675288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1676287670Smav ctl_set_busy(&io->scsiio); 1677287670Smav ctl_complete_beio(beio); 1678287670Smav return; 1679287670Smav } 1680229997Sken be_lun->dispatch(be_lun, beio); 1681229997Sken continue; 1682229997Sken } 1683229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1684229997Sken if (io != NULL) { 1685229997Sken DPRINTF("config write queue\n"); 1686229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1687229997Sken ctl_io_hdr, links); 1688267877Smav mtx_unlock(&be_lun->queue_lock); 1689288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1690287670Smav ctl_set_busy(&io->scsiio); 1691287670Smav ctl_config_write_done(io); 1692287670Smav return; 1693287670Smav } 1694229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1695229997Sken continue; 1696229997Sken } 1697275474Smav io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1698275474Smav if (io != NULL) { 1699275474Smav DPRINTF("config read queue\n"); 1700275474Smav STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1701275474Smav ctl_io_hdr, links); 1702275474Smav mtx_unlock(&be_lun->queue_lock); 1703288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1704287670Smav ctl_set_busy(&io->scsiio); 1705287670Smav ctl_config_read_done(io); 1706287670Smav return; 1707287670Smav } 1708275474Smav ctl_be_block_cr_dispatch(be_lun, io); 1709275474Smav continue; 1710275474Smav } 1711229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1712229997Sken if (io != NULL) { 1713229997Sken DPRINTF("input queue\n"); 1714229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1715229997Sken ctl_io_hdr, links); 1716267877Smav mtx_unlock(&be_lun->queue_lock); 1717288348Smav if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) { 1718287670Smav ctl_set_busy(&io->scsiio); 1719287670Smav ctl_data_submit_done(io); 1720287670Smav return; 1721287670Smav } 1722229997Sken ctl_be_block_dispatch(be_lun, io); 1723229997Sken continue; 1724229997Sken } 1725229997Sken 1726229997Sken /* 1727229997Sken * If we get here, there is no work left in the queues, so 1728229997Sken * just break out and let the task queue go to sleep. 1729229997Sken */ 1730287670Smav mtx_unlock(&be_lun->queue_lock); 1731229997Sken break; 1732229997Sken } 1733229997Sken} 1734229997Sken 1735229997Sken/* 1736229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1737229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1738229997Sken * thread. 1739229997Sken */ 1740229997Skenstatic int 1741229997Skenctl_be_block_submit(union ctl_io *io) 1742229997Sken{ 1743229997Sken struct ctl_be_block_lun *be_lun; 1744287499Smav struct ctl_be_lun *cbe_lun; 1745229997Sken 1746229997Sken DPRINTF("entered\n"); 1747229997Sken 1748287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1749229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1750287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1751229997Sken 1752229997Sken /* 1753229997Sken * Make sure we only get SCSI I/O. 1754229997Sken */ 1755229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1756229997Sken "%#x) encountered", io->io_hdr.io_type)); 1757229997Sken 1758267519Smav PRIV(io)->len = 0; 1759267519Smav 1760267877Smav mtx_lock(&be_lun->queue_lock); 1761229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1762267877Smav mtx_unlock(&be_lun->queue_lock); 1763229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1764229997Sken 1765267514Smav return (CTL_RETVAL_COMPLETE); 1766229997Sken} 1767229997Sken 1768229997Skenstatic int 1769229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1770229997Sken int flag, struct thread *td) 1771229997Sken{ 1772229997Sken struct ctl_be_block_softc *softc; 1773229997Sken int error; 1774229997Sken 1775229997Sken softc = &backend_block_softc; 1776229997Sken 1777229997Sken error = 0; 1778229997Sken 1779229997Sken switch (cmd) { 1780229997Sken case CTL_LUN_REQ: { 1781229997Sken struct ctl_lun_req *lun_req; 1782229997Sken 1783229997Sken lun_req = (struct ctl_lun_req *)addr; 1784229997Sken 1785229997Sken switch (lun_req->reqtype) { 1786229997Sken case CTL_LUNREQ_CREATE: 1787229997Sken error = ctl_be_block_create(softc, lun_req); 1788229997Sken break; 1789229997Sken case CTL_LUNREQ_RM: 1790229997Sken error = ctl_be_block_rm(softc, lun_req); 1791229997Sken break; 1792232604Strasz case CTL_LUNREQ_MODIFY: 1793232604Strasz error = ctl_be_block_modify(softc, lun_req); 1794232604Strasz break; 1795229997Sken default: 1796229997Sken lun_req->status = CTL_LUN_ERROR; 1797229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1798272911Smav "invalid LUN request type %d", 1799229997Sken lun_req->reqtype); 1800229997Sken break; 1801229997Sken } 1802229997Sken break; 1803229997Sken } 1804229997Sken default: 1805229997Sken error = ENOTTY; 1806229997Sken break; 1807229997Sken } 1808229997Sken 1809229997Sken return (error); 1810229997Sken} 1811229997Sken 1812229997Skenstatic int 1813229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1814229997Sken{ 1815287499Smav struct ctl_be_lun *cbe_lun; 1816229997Sken struct ctl_be_block_filedata *file_data; 1817229997Sken struct ctl_lun_create_params *params; 1818275865Smav char *value; 1819229997Sken struct vattr vattr; 1820275865Smav off_t ps, pss, po, pos, us, uss, uo, uos; 1821229997Sken int error; 1822229997Sken 1823287499Smav cbe_lun = &be_lun->cbe_lun; 1824229997Sken file_data = &be_lun->backend.file; 1825272911Smav params = &be_lun->params; 1826229997Sken 1827229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1828229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1829229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1830275474Smav be_lun->get_lba_status = ctl_be_block_gls_file; 1831275481Smav be_lun->getattr = ctl_be_block_getattr_file; 1832287499Smav be_lun->unmap = NULL; 1833287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1834229997Sken 1835229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1836229997Sken if (error != 0) { 1837229997Sken snprintf(req->error_str, sizeof(req->error_str), 1838229997Sken "error calling VOP_GETATTR() for file %s", 1839229997Sken be_lun->dev_path); 1840229997Sken return (error); 1841229997Sken } 1842229997Sken 1843229997Sken file_data->cred = crhold(curthread->td_ucred); 1844232604Strasz if (params->lun_size_bytes != 0) 1845232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1846232604Strasz else 1847232604Strasz be_lun->size_bytes = vattr.va_size; 1848229997Sken 1849229997Sken /* 1850273029Smav * For files we can use any logical block size. Prefer 512 bytes 1851273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1852273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1853273029Smav * logical block size -- report it as physical block size. 1854229997Sken */ 1855229997Sken if (params->blocksize_bytes != 0) 1856287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1857288310Smav else if (cbe_lun->lun_type == T_CDROM) 1858288310Smav cbe_lun->blocksize = 2048; 1859229997Sken else 1860287499Smav cbe_lun->blocksize = 512; 1861287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1862287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1863287499Smav 0 : (be_lun->size_blocks - 1); 1864275865Smav 1865275865Smav us = ps = vattr.va_blocksize; 1866275865Smav uo = po = 0; 1867275865Smav 1868287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1869275865Smav if (value != NULL) 1870275865Smav ctl_expand_number(value, &ps); 1871287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1872275865Smav if (value != NULL) 1873275865Smav ctl_expand_number(value, &po); 1874287499Smav pss = ps / cbe_lun->blocksize; 1875287499Smav pos = po / cbe_lun->blocksize; 1876287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1877287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1878287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1879287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1880273029Smav } 1881229997Sken 1882287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1883275865Smav if (value != NULL) 1884275865Smav ctl_expand_number(value, &us); 1885287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1886275865Smav if (value != NULL) 1887275865Smav ctl_expand_number(value, &uo); 1888287499Smav uss = us / cbe_lun->blocksize; 1889287499Smav uos = uo / cbe_lun->blocksize; 1890287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1891287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1892287499Smav cbe_lun->ublockexp = fls(uss) - 1; 1893287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 1894275865Smav } 1895275865Smav 1896229997Sken /* 1897229997Sken * Sanity check. The media size has to be at least one 1898229997Sken * sector long. 1899229997Sken */ 1900287499Smav if (be_lun->size_bytes < cbe_lun->blocksize) { 1901229997Sken error = EINVAL; 1902229997Sken snprintf(req->error_str, sizeof(req->error_str), 1903229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1904287499Smav (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1905229997Sken } 1906275920Smav 1907287499Smav cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1908229997Sken return (error); 1909229997Sken} 1910229997Sken 1911229997Skenstatic int 1912229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1913229997Sken{ 1914287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1915229997Sken struct ctl_lun_create_params *params; 1916287664Smav struct cdevsw *csw; 1917229997Sken struct cdev *dev; 1918275865Smav char *value; 1919287664Smav int error, atomic, maxio, ref, unmap, tmp; 1920287221Smav off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1921229997Sken 1922272911Smav params = &be_lun->params; 1923229997Sken 1924229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1925287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1926287664Smav if (csw == NULL) 1927287664Smav return (ENXIO); 1928287664Smav if (strcmp(csw->d_name, "zvol") == 0) { 1929269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1930275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1931275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1932275920Smav } else { 1933269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1934287499Smav be_lun->get_lba_status = NULL; 1935275920Smav atomic = 0; 1936287664Smav maxio = dev->si_iosize_max; 1937275920Smav if (maxio <= 0) 1938275920Smav maxio = DFLTPHYS; 1939275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1940275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1941275920Smav } 1942269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1943274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1944287499Smav be_lun->unmap = ctl_be_block_unmap_dev; 1945229997Sken 1946287664Smav if (!csw->d_ioctl) { 1947287664Smav dev_relthread(dev, ref); 1948229997Sken snprintf(req->error_str, sizeof(req->error_str), 1949287664Smav "no d_ioctl for device %s!", be_lun->dev_path); 1950229997Sken return (ENODEV); 1951229997Sken } 1952229997Sken 1953287664Smav error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1954229997Sken curthread); 1955229997Sken if (error) { 1956287664Smav dev_relthread(dev, ref); 1957229997Sken snprintf(req->error_str, sizeof(req->error_str), 1958272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1959272911Smav "on %s!", error, be_lun->dev_path); 1960229997Sken return (error); 1961229997Sken } 1962229997Sken 1963229997Sken /* 1964229997Sken * If the user has asked for a blocksize that is greater than the 1965229997Sken * backing device's blocksize, we can do it only if the blocksize 1966229997Sken * the user is asking for is an even multiple of the underlying 1967229997Sken * device's blocksize. 1968229997Sken */ 1969286811Smav if ((params->blocksize_bytes != 0) && 1970286811Smav (params->blocksize_bytes >= tmp)) { 1971286811Smav if (params->blocksize_bytes % tmp == 0) { 1972287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1973229997Sken } else { 1974287664Smav dev_relthread(dev, ref); 1975229997Sken snprintf(req->error_str, sizeof(req->error_str), 1976272911Smav "requested blocksize %u is not an even " 1977229997Sken "multiple of backing device blocksize %u", 1978287221Smav params->blocksize_bytes, tmp); 1979229997Sken return (EINVAL); 1980229997Sken } 1981286811Smav } else if (params->blocksize_bytes != 0) { 1982287664Smav dev_relthread(dev, ref); 1983229997Sken snprintf(req->error_str, sizeof(req->error_str), 1984272911Smav "requested blocksize %u < backing device " 1985287221Smav "blocksize %u", params->blocksize_bytes, tmp); 1986229997Sken return (EINVAL); 1987288310Smav } else if (cbe_lun->lun_type == T_CDROM) 1988288310Smav cbe_lun->blocksize = MAX(tmp, 2048); 1989288310Smav else 1990287499Smav cbe_lun->blocksize = tmp; 1991229997Sken 1992287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1993287664Smav curthread); 1994229997Sken if (error) { 1995287664Smav dev_relthread(dev, ref); 1996229997Sken snprintf(req->error_str, sizeof(req->error_str), 1997272911Smav "error %d returned for DIOCGMEDIASIZE " 1998272911Smav " ioctl on %s!", error, 1999232604Strasz be_lun->dev_path); 2000229997Sken return (error); 2001229997Sken } 2002229997Sken 2003232604Strasz if (params->lun_size_bytes != 0) { 2004287221Smav if (params->lun_size_bytes > otmp) { 2005287664Smav dev_relthread(dev, ref); 2006232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2007272911Smav "requested LUN size %ju > backing device " 2008272911Smav "size %ju", 2009232604Strasz (uintmax_t)params->lun_size_bytes, 2010287221Smav (uintmax_t)otmp); 2011232604Strasz return (EINVAL); 2012232604Strasz } 2013232604Strasz 2014232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2015286811Smav } else 2016287221Smav be_lun->size_bytes = otmp; 2017287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2018287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2019287499Smav 0 : (be_lun->size_blocks - 1); 2020232604Strasz 2021287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 2022287664Smav curthread); 2023264191Smav if (error) 2024264191Smav ps = po = 0; 2025264191Smav else { 2026287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2027287664Smav FREAD, curthread); 2028264191Smav if (error) 2029264191Smav po = 0; 2030264191Smav } 2031275865Smav us = ps; 2032275865Smav uo = po; 2033275865Smav 2034287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 2035275865Smav if (value != NULL) 2036275865Smav ctl_expand_number(value, &ps); 2037287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 2038275865Smav if (value != NULL) 2039275865Smav ctl_expand_number(value, &po); 2040287499Smav pss = ps / cbe_lun->blocksize; 2041287499Smav pos = po / cbe_lun->blocksize; 2042287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2043287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2044287499Smav cbe_lun->pblockexp = fls(pss) - 1; 2045287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 2046264191Smav } 2047264191Smav 2048287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 2049275865Smav if (value != NULL) 2050275865Smav ctl_expand_number(value, &us); 2051287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 2052275865Smav if (value != NULL) 2053275865Smav ctl_expand_number(value, &uo); 2054287499Smav uss = us / cbe_lun->blocksize; 2055287499Smav uos = uo / cbe_lun->blocksize; 2056287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2057287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2058287499Smav cbe_lun->ublockexp = fls(uss) - 1; 2059287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 2060275865Smav } 2061275865Smav 2062287499Smav cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2063287499Smav cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2064278672Smav 2065278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2066278672Smav unmap = 1; 2067278672Smav } else { 2068278672Smav struct diocgattr_arg arg; 2069278672Smav 2070278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2071278672Smav arg.len = sizeof(arg.value.i); 2072287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2073287664Smav curthread); 2074278672Smav unmap = (error == 0) ? arg.value.i : 0; 2075278672Smav } 2076287499Smav value = ctl_get_opt(&cbe_lun->options, "unmap"); 2077278672Smav if (value != NULL) 2078278672Smav unmap = (strcmp(value, "on") == 0); 2079278672Smav if (unmap) 2080287499Smav cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2081287499Smav else 2082287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2083278672Smav 2084287664Smav dev_relthread(dev, ref); 2085229997Sken return (0); 2086229997Sken} 2087229997Sken 2088229997Skenstatic int 2089229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2090229997Sken{ 2091287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2092287499Smav int flags; 2093287499Smav 2094229997Sken if (be_lun->vn) { 2095287499Smav flags = FREAD; 2096287499Smav if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2097287499Smav flags |= FWRITE; 2098229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2099229997Sken be_lun->vn = NULL; 2100229997Sken 2101229997Sken switch (be_lun->dev_type) { 2102229997Sken case CTL_BE_BLOCK_DEV: 2103229997Sken break; 2104229997Sken case CTL_BE_BLOCK_FILE: 2105229997Sken if (be_lun->backend.file.cred != NULL) { 2106229997Sken crfree(be_lun->backend.file.cred); 2107229997Sken be_lun->backend.file.cred = NULL; 2108229997Sken } 2109229997Sken break; 2110229997Sken case CTL_BE_BLOCK_NONE: 2111258871Strasz break; 2112229997Sken default: 2113289702Smav panic("Unexpected backend type %d", be_lun->dev_type); 2114229997Sken break; 2115229997Sken } 2116272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2117229997Sken } 2118229997Sken return (0); 2119229997Sken} 2120229997Sken 2121229997Skenstatic int 2122288348Smavctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2123229997Sken{ 2124287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2125229997Sken struct nameidata nd; 2126287499Smav char *value; 2127287499Smav int error, flags; 2128229997Sken 2129229997Sken error = 0; 2130229997Sken if (rootvnode == NULL) { 2131229997Sken snprintf(req->error_str, sizeof(req->error_str), 2132272911Smav "Root filesystem is not mounted"); 2133229997Sken return (1); 2134229997Sken } 2135285391Smjg pwd_ensure_dirs(); 2136229997Sken 2137287499Smav value = ctl_get_opt(&cbe_lun->options, "file"); 2138287499Smav if (value == NULL) { 2139287499Smav snprintf(req->error_str, sizeof(req->error_str), 2140287499Smav "no file argument specified"); 2141287499Smav return (1); 2142287499Smav } 2143287499Smav free(be_lun->dev_path, M_CTLBLK); 2144287499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2145287499Smav 2146287499Smav flags = FREAD; 2147287499Smav value = ctl_get_opt(&cbe_lun->options, "readonly"); 2148288310Smav if (value != NULL) { 2149288310Smav if (strcmp(value, "on") != 0) 2150288310Smav flags |= FWRITE; 2151288310Smav } else if (cbe_lun->lun_type == T_DIRECT) 2152287499Smav flags |= FWRITE; 2153287499Smav 2154287499Smavagain: 2155229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2156229997Sken error = vn_open(&nd, &flags, 0, NULL); 2157287499Smav if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2158287499Smav flags &= ~FWRITE; 2159287499Smav goto again; 2160287499Smav } 2161229997Sken if (error) { 2162229997Sken /* 2163229997Sken * This is the only reasonable guess we can make as far as 2164229997Sken * path if the user doesn't give us a fully qualified path. 2165229997Sken * If they want to specify a file, they need to specify the 2166229997Sken * full path. 2167229997Sken */ 2168229997Sken if (be_lun->dev_path[0] != '/') { 2169229997Sken char *dev_name; 2170229997Sken 2171287499Smav asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2172287499Smav be_lun->dev_path); 2173287499Smav free(be_lun->dev_path, M_CTLBLK); 2174287499Smav be_lun->dev_path = dev_name; 2175287499Smav goto again; 2176229997Sken } 2177229997Sken snprintf(req->error_str, sizeof(req->error_str), 2178272911Smav "error opening %s: %d", be_lun->dev_path, error); 2179229997Sken return (error); 2180229997Sken } 2181287499Smav if (flags & FWRITE) 2182287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2183287499Smav else 2184287499Smav cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2185229997Sken 2186229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2187229997Sken be_lun->vn = nd.ni_vp; 2188229997Sken 2189229997Sken /* We only support disks and files. */ 2190229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2191229997Sken error = ctl_be_block_open_dev(be_lun, req); 2192229997Sken } else if (be_lun->vn->v_type == VREG) { 2193229997Sken error = ctl_be_block_open_file(be_lun, req); 2194229997Sken } else { 2195229997Sken error = EINVAL; 2196229997Sken snprintf(req->error_str, sizeof(req->error_str), 2197258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2198229997Sken } 2199229997Sken VOP_UNLOCK(be_lun->vn, 0); 2200229997Sken 2201286811Smav if (error != 0) 2202229997Sken ctl_be_block_close(be_lun); 2203287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2204287499Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2205287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2206287499Smav value = ctl_get_opt(&cbe_lun->options, "serseq"); 2207287499Smav if (value != NULL && strcmp(value, "on") == 0) 2208287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2209287499Smav else if (value != NULL && strcmp(value, "read") == 0) 2210287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2211287499Smav else if (value != NULL && strcmp(value, "off") == 0) 2212287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2213229997Sken return (0); 2214229997Sken} 2215229997Sken 2216229997Skenstatic int 2217229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2218229997Sken{ 2219287499Smav struct ctl_be_lun *cbe_lun; 2220229997Sken struct ctl_be_block_lun *be_lun; 2221229997Sken struct ctl_lun_create_params *params; 2222267481Smav char num_thread_str[16]; 2223229997Sken char tmpstr[32]; 2224267481Smav char *value; 2225278672Smav int retval, num_threads; 2226267481Smav int tmp_num_threads; 2227229997Sken 2228229997Sken params = &req->reqdata.create; 2229229997Sken retval = 0; 2230272911Smav req->status = CTL_LUN_OK; 2231229997Sken 2232229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2233287499Smav cbe_lun = &be_lun->cbe_lun; 2234287499Smav cbe_lun->be_lun = be_lun; 2235272911Smav be_lun->params = req->reqdata.create; 2236229997Sken be_lun->softc = softc; 2237229997Sken STAILQ_INIT(&be_lun->input_queue); 2238275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2239229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2240229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2241229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2242267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2243267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2244287499Smav ctl_init_opts(&cbe_lun->options, 2245268280Smav req->num_be_args, req->kern_be_args); 2246264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2247256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2248229997Sken if (be_lun->lun_zone == NULL) { 2249229997Sken snprintf(req->error_str, sizeof(req->error_str), 2250272911Smav "error allocating UMA zone"); 2251229997Sken goto bailout_error; 2252229997Sken } 2253229997Sken 2254229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2255287499Smav cbe_lun->lun_type = params->device_type; 2256229997Sken else 2257287499Smav cbe_lun->lun_type = T_DIRECT; 2258287499Smav be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2259287621Smav cbe_lun->flags = 0; 2260287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2261287621Smav if (value != NULL) { 2262287621Smav if (strcmp(value, "primary") == 0) 2263287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2264287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2265287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2266229997Sken 2267288310Smav if (cbe_lun->lun_type == T_DIRECT || 2268288310Smav cbe_lun->lun_type == T_CDROM) { 2269286811Smav be_lun->size_bytes = params->lun_size_bytes; 2270286811Smav if (params->blocksize_bytes != 0) 2271287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2272288310Smav else if (cbe_lun->lun_type == T_CDROM) 2273288310Smav cbe_lun->blocksize = 2048; 2274286811Smav else 2275287499Smav cbe_lun->blocksize = 512; 2276287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2277287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2278287499Smav 0 : (be_lun->size_blocks - 1); 2279229997Sken 2280287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2281287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2282288348Smav retval = ctl_be_block_open(be_lun, req); 2283287621Smav if (retval != 0) { 2284287621Smav retval = 0; 2285287621Smav req->status = CTL_LUN_WARNING; 2286287621Smav } 2287229997Sken } 2288287499Smav num_threads = cbb_num_threads; 2289229997Sken } else { 2290229997Sken num_threads = 1; 2291229997Sken } 2292229997Sken 2293287499Smav value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2294267481Smav if (value != NULL) { 2295267481Smav tmp_num_threads = strtol(value, NULL, 0); 2296229997Sken 2297267481Smav /* 2298267481Smav * We don't let the user specify less than one 2299267481Smav * thread, but hope he's clueful enough not to 2300267481Smav * specify 1000 threads. 2301267481Smav */ 2302267481Smav if (tmp_num_threads < 1) { 2303267481Smav snprintf(req->error_str, sizeof(req->error_str), 2304272911Smav "invalid number of threads %s", 2305272911Smav num_thread_str); 2306267481Smav goto bailout_error; 2307229997Sken } 2308267481Smav num_threads = tmp_num_threads; 2309229997Sken } 2310229997Sken 2311272911Smav if (be_lun->vn == NULL) 2312288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2313229997Sken /* Tell the user the blocksize we ended up using */ 2314272911Smav params->lun_size_bytes = be_lun->size_bytes; 2315287499Smav params->blocksize_bytes = cbe_lun->blocksize; 2316229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2317287499Smav cbe_lun->req_lun_id = params->req_lun_id; 2318287499Smav cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2319229997Sken } else 2320287499Smav cbe_lun->req_lun_id = 0; 2321229997Sken 2322287499Smav cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2323287499Smav cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2324287499Smav cbe_lun->be = &ctl_be_block_driver; 2325229997Sken 2326229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2327229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2328229997Sken softc->num_luns); 2329287499Smav strncpy((char *)cbe_lun->serial_num, tmpstr, 2330287499Smav MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2331229997Sken 2332229997Sken /* Tell the user what we used for a serial number */ 2333229997Sken strncpy((char *)params->serial_num, tmpstr, 2334275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2335229997Sken } else { 2336287499Smav strncpy((char *)cbe_lun->serial_num, params->serial_num, 2337287499Smav MIN(sizeof(cbe_lun->serial_num), 2338229997Sken sizeof(params->serial_num))); 2339229997Sken } 2340229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2341229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2342287499Smav strncpy((char *)cbe_lun->device_id, tmpstr, 2343287499Smav MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2344229997Sken 2345229997Sken /* Tell the user what we used for a device ID */ 2346229997Sken strncpy((char *)params->device_id, tmpstr, 2347275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2348229997Sken } else { 2349287499Smav strncpy((char *)cbe_lun->device_id, params->device_id, 2350287499Smav MIN(sizeof(cbe_lun->device_id), 2351275953Smav sizeof(params->device_id))); 2352229997Sken } 2353229997Sken 2354229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2355229997Sken 2356229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2357229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2358229997Sken 2359229997Sken if (be_lun->io_taskqueue == NULL) { 2360229997Sken snprintf(req->error_str, sizeof(req->error_str), 2361272911Smav "unable to create taskqueue"); 2362229997Sken goto bailout_error; 2363229997Sken } 2364229997Sken 2365229997Sken /* 2366229997Sken * Note that we start the same number of threads by default for 2367229997Sken * both the file case and the block device case. For the file 2368229997Sken * case, we need multiple threads to allow concurrency, because the 2369229997Sken * vnode interface is designed to be a blocking interface. For the 2370229997Sken * block device case, ZFS zvols at least will block the caller's 2371229997Sken * context in many instances, and so we need multiple threads to 2372229997Sken * overcome that problem. Other block devices don't need as many 2373229997Sken * threads, but they shouldn't cause too many problems. 2374229997Sken * 2375229997Sken * If the user wants to just have a single thread for a block 2376229997Sken * device, he can specify that when the LUN is created, or change 2377229997Sken * the tunable/sysctl to alter the default number of threads. 2378229997Sken */ 2379229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2380229997Sken /*num threads*/num_threads, 2381229997Sken /*priority*/PWAIT, 2382229997Sken /*thread name*/ 2383229997Sken "%s taskq", be_lun->lunname); 2384229997Sken 2385229997Sken if (retval != 0) 2386229997Sken goto bailout_error; 2387229997Sken 2388229997Sken be_lun->num_threads = num_threads; 2389229997Sken 2390229997Sken mtx_lock(&softc->lock); 2391229997Sken softc->num_luns++; 2392229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2393229997Sken 2394229997Sken mtx_unlock(&softc->lock); 2395229997Sken 2396287499Smav retval = ctl_add_lun(&be_lun->cbe_lun); 2397229997Sken if (retval != 0) { 2398229997Sken mtx_lock(&softc->lock); 2399229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2400229997Sken links); 2401229997Sken softc->num_luns--; 2402229997Sken mtx_unlock(&softc->lock); 2403229997Sken snprintf(req->error_str, sizeof(req->error_str), 2404272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2405272911Smav "details", retval); 2406229997Sken retval = 0; 2407229997Sken goto bailout_error; 2408229997Sken } 2409229997Sken 2410229997Sken mtx_lock(&softc->lock); 2411229997Sken 2412229997Sken /* 2413229997Sken * Tell the config_status routine that we're waiting so it won't 2414229997Sken * clean up the LUN in the event of an error. 2415229997Sken */ 2416229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2417229997Sken 2418229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2419229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2420229997Sken if (retval == EINTR) 2421229997Sken break; 2422229997Sken } 2423229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2424229997Sken 2425229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2426229997Sken snprintf(req->error_str, sizeof(req->error_str), 2427272911Smav "LUN configuration error, see dmesg for details"); 2428229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2429229997Sken links); 2430229997Sken softc->num_luns--; 2431229997Sken mtx_unlock(&softc->lock); 2432229997Sken goto bailout_error; 2433229997Sken } else { 2434287499Smav params->req_lun_id = cbe_lun->lun_id; 2435229997Sken } 2436229997Sken 2437229997Sken mtx_unlock(&softc->lock); 2438229997Sken 2439229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2440287499Smav cbe_lun->blocksize, 2441229997Sken DEVSTAT_ALL_SUPPORTED, 2442287499Smav cbe_lun->lun_type 2443229997Sken | DEVSTAT_TYPE_IF_OTHER, 2444229997Sken DEVSTAT_PRIORITY_OTHER); 2445229997Sken 2446229997Sken return (retval); 2447229997Sken 2448229997Skenbailout_error: 2449229997Sken req->status = CTL_LUN_ERROR; 2450229997Sken 2451267429Smav if (be_lun->io_taskqueue != NULL) 2452267429Smav taskqueue_free(be_lun->io_taskqueue); 2453229997Sken ctl_be_block_close(be_lun); 2454267429Smav if (be_lun->dev_path != NULL) 2455267429Smav free(be_lun->dev_path, M_CTLBLK); 2456267429Smav if (be_lun->lun_zone != NULL) 2457267429Smav uma_zdestroy(be_lun->lun_zone); 2458287499Smav ctl_free_opts(&cbe_lun->options); 2459267877Smav mtx_destroy(&be_lun->queue_lock); 2460267877Smav mtx_destroy(&be_lun->io_lock); 2461229997Sken free(be_lun, M_CTLBLK); 2462229997Sken 2463229997Sken return (retval); 2464229997Sken} 2465229997Sken 2466229997Skenstatic int 2467229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2468229997Sken{ 2469229997Sken struct ctl_lun_rm_params *params; 2470229997Sken struct ctl_be_block_lun *be_lun; 2471287670Smav struct ctl_be_lun *cbe_lun; 2472229997Sken int retval; 2473229997Sken 2474229997Sken params = &req->reqdata.rm; 2475229997Sken 2476229997Sken mtx_lock(&softc->lock); 2477229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2478287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2479229997Sken break; 2480229997Sken } 2481229997Sken mtx_unlock(&softc->lock); 2482229997Sken if (be_lun == NULL) { 2483229997Sken snprintf(req->error_str, sizeof(req->error_str), 2484272911Smav "LUN %u is not managed by the block backend", 2485272911Smav params->lun_id); 2486229997Sken goto bailout_error; 2487229997Sken } 2488287670Smav cbe_lun = &be_lun->cbe_lun; 2489229997Sken 2490287670Smav retval = ctl_disable_lun(cbe_lun); 2491229997Sken if (retval != 0) { 2492229997Sken snprintf(req->error_str, sizeof(req->error_str), 2493272911Smav "error %d returned from ctl_disable_lun() for " 2494272911Smav "LUN %d", retval, params->lun_id); 2495229997Sken goto bailout_error; 2496287670Smav } 2497229997Sken 2498287670Smav if (be_lun->vn != NULL) { 2499288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2500288348Smav ctl_lun_no_media(cbe_lun); 2501287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2502287670Smav ctl_be_block_close(be_lun); 2503229997Sken } 2504229997Sken 2505287670Smav retval = ctl_invalidate_lun(cbe_lun); 2506229997Sken if (retval != 0) { 2507229997Sken snprintf(req->error_str, sizeof(req->error_str), 2508272911Smav "error %d returned from ctl_invalidate_lun() for " 2509272911Smav "LUN %d", retval, params->lun_id); 2510229997Sken goto bailout_error; 2511229997Sken } 2512229997Sken 2513229997Sken mtx_lock(&softc->lock); 2514229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2515229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2516229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2517229997Sken if (retval == EINTR) 2518229997Sken break; 2519229997Sken } 2520229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2521229997Sken 2522229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2523229997Sken snprintf(req->error_str, sizeof(req->error_str), 2524272911Smav "interrupted waiting for LUN to be freed"); 2525229997Sken mtx_unlock(&softc->lock); 2526229997Sken goto bailout_error; 2527229997Sken } 2528229997Sken 2529229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2530229997Sken 2531229997Sken softc->num_luns--; 2532229997Sken mtx_unlock(&softc->lock); 2533229997Sken 2534287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2535229997Sken taskqueue_free(be_lun->io_taskqueue); 2536229997Sken 2537229997Sken if (be_lun->disk_stats != NULL) 2538229997Sken devstat_remove_entry(be_lun->disk_stats); 2539229997Sken 2540229997Sken uma_zdestroy(be_lun->lun_zone); 2541229997Sken 2542287670Smav ctl_free_opts(&cbe_lun->options); 2543229997Sken free(be_lun->dev_path, M_CTLBLK); 2544267877Smav mtx_destroy(&be_lun->queue_lock); 2545267877Smav mtx_destroy(&be_lun->io_lock); 2546229997Sken free(be_lun, M_CTLBLK); 2547229997Sken 2548229997Sken req->status = CTL_LUN_OK; 2549229997Sken return (0); 2550229997Sken 2551229997Skenbailout_error: 2552229997Sken req->status = CTL_LUN_ERROR; 2553229997Sken return (0); 2554229997Sken} 2555229997Sken 2556232604Straszstatic int 2557232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2558232604Strasz{ 2559232604Strasz struct ctl_lun_modify_params *params; 2560232604Strasz struct ctl_be_block_lun *be_lun; 2561287500Smav struct ctl_be_lun *cbe_lun; 2562287621Smav char *value; 2563271794Smav uint64_t oldsize; 2564287621Smav int error, wasprim; 2565232604Strasz 2566232604Strasz params = &req->reqdata.modify; 2567232604Strasz 2568232604Strasz mtx_lock(&softc->lock); 2569232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2570287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2571232604Strasz break; 2572232604Strasz } 2573232604Strasz mtx_unlock(&softc->lock); 2574232604Strasz if (be_lun == NULL) { 2575232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2576272911Smav "LUN %u is not managed by the block backend", 2577272911Smav params->lun_id); 2578232604Strasz goto bailout_error; 2579232604Strasz } 2580287500Smav cbe_lun = &be_lun->cbe_lun; 2581232604Strasz 2582287500Smav if (params->lun_size_bytes != 0) 2583287500Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2584287500Smav ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2585232604Strasz 2586287621Smav wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2587287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2588287621Smav if (value != NULL) { 2589287621Smav if (strcmp(value, "primary") == 0) 2590287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2591287621Smav else 2592287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2593287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2594287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2595232604Strasz else 2596287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2597287621Smav if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 2598287621Smav if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 2599287621Smav ctl_lun_primary(cbe_lun); 2600287621Smav else 2601287621Smav ctl_lun_secondary(cbe_lun); 2602287621Smav } 2603232604Strasz 2604287621Smav oldsize = be_lun->size_blocks; 2605287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2606287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2607287621Smav if (be_lun->vn == NULL) 2608288348Smav error = ctl_be_block_open(be_lun, req); 2609287621Smav else if (vn_isdisk(be_lun->vn, &error)) 2610288104Smav error = ctl_be_block_open_dev(be_lun, req); 2611289017Smav else if (be_lun->vn->v_type == VREG) { 2612289017Smav vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2613288104Smav error = ctl_be_block_open_file(be_lun, req); 2614289017Smav VOP_UNLOCK(be_lun->vn, 0); 2615289017Smav } else 2616287621Smav error = EINVAL; 2617288348Smav if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) && 2618287621Smav be_lun->vn != NULL) { 2619288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2620288348Smav ctl_lun_has_media(cbe_lun); 2621288348Smav } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 && 2622288348Smav be_lun->vn == NULL) { 2623288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2624288348Smav ctl_lun_no_media(cbe_lun); 2625287621Smav } 2626288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 2627287621Smav } else { 2628287621Smav if (be_lun->vn != NULL) { 2629288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2630288348Smav ctl_lun_no_media(cbe_lun); 2631287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2632287621Smav error = ctl_be_block_close(be_lun); 2633287621Smav } else 2634287621Smav error = 0; 2635287621Smav } 2636287499Smav if (be_lun->size_blocks != oldsize) 2637287500Smav ctl_lun_capacity_changed(cbe_lun); 2638232604Strasz 2639232604Strasz /* Tell the user the exact size we ended up using */ 2640232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2641232604Strasz 2642272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2643232604Strasz return (0); 2644232604Strasz 2645232604Straszbailout_error: 2646232604Strasz req->status = CTL_LUN_ERROR; 2647232604Strasz return (0); 2648232604Strasz} 2649232604Strasz 2650229997Skenstatic void 2651229997Skenctl_be_block_lun_shutdown(void *be_lun) 2652229997Sken{ 2653229997Sken struct ctl_be_block_lun *lun; 2654229997Sken struct ctl_be_block_softc *softc; 2655229997Sken 2656229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2657229997Sken softc = lun->softc; 2658229997Sken 2659229997Sken mtx_lock(&softc->lock); 2660229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2661229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2662229997Sken wakeup(lun); 2663229997Sken mtx_unlock(&softc->lock); 2664229997Sken} 2665229997Sken 2666229997Skenstatic void 2667229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2668229997Sken{ 2669229997Sken struct ctl_be_block_lun *lun; 2670229997Sken struct ctl_be_block_softc *softc; 2671229997Sken 2672229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2673229997Sken softc = lun->softc; 2674229997Sken 2675229997Sken if (status == CTL_LUN_CONFIG_OK) { 2676229997Sken mtx_lock(&softc->lock); 2677229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2678229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2679229997Sken wakeup(lun); 2680229997Sken mtx_unlock(&softc->lock); 2681229997Sken 2682229997Sken /* 2683229997Sken * We successfully added the LUN, attempt to enable it. 2684229997Sken */ 2685287499Smav if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2686229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2687287499Smav if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2688229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2689229997Sken __func__); 2690229997Sken } 2691229997Sken } 2692229997Sken 2693229997Sken return; 2694229997Sken } 2695229997Sken 2696229997Sken 2697229997Sken mtx_lock(&softc->lock); 2698229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2699229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2700229997Sken wakeup(lun); 2701229997Sken mtx_unlock(&softc->lock); 2702229997Sken} 2703229997Sken 2704229997Sken 2705229997Skenstatic int 2706229997Skenctl_be_block_config_write(union ctl_io *io) 2707229997Sken{ 2708229997Sken struct ctl_be_block_lun *be_lun; 2709287499Smav struct ctl_be_lun *cbe_lun; 2710229997Sken int retval; 2711229997Sken 2712229997Sken DPRINTF("entered\n"); 2713229997Sken 2714287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2715229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2716287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2717229997Sken 2718288220Smav retval = 0; 2719229997Sken switch (io->scsiio.cdb[0]) { 2720229997Sken case SYNCHRONIZE_CACHE: 2721229997Sken case SYNCHRONIZE_CACHE_16: 2722264274Smav case WRITE_SAME_10: 2723264274Smav case WRITE_SAME_16: 2724264274Smav case UNMAP: 2725229997Sken /* 2726229997Sken * The upper level CTL code will filter out any CDBs with 2727229997Sken * the immediate bit set and return the proper error. 2728229997Sken * 2729229997Sken * We don't really need to worry about what LBA range the 2730229997Sken * user asked to be synced out. When they issue a sync 2731229997Sken * cache command, we'll sync out the whole thing. 2732229997Sken */ 2733267877Smav mtx_lock(&be_lun->queue_lock); 2734229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2735229997Sken links); 2736267877Smav mtx_unlock(&be_lun->queue_lock); 2737229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2738229997Sken break; 2739229997Sken case START_STOP_UNIT: { 2740229997Sken struct scsi_start_stop_unit *cdb; 2741288348Smav struct ctl_lun_req req; 2742229997Sken 2743229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2744288369Smav if ((cdb->how & SSS_PC_MASK) != 0) { 2745288369Smav ctl_set_success(&io->scsiio); 2746288369Smav ctl_config_write_done(io); 2747288369Smav break; 2748288369Smav } 2749288348Smav if (cdb->how & SSS_START) { 2750288348Smav if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) { 2751288348Smav retval = ctl_be_block_open(be_lun, &req); 2752288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED; 2753288348Smav if (retval == 0) { 2754288348Smav cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA; 2755288348Smav ctl_lun_has_media(cbe_lun); 2756288348Smav } else { 2757288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2758288348Smav ctl_lun_no_media(cbe_lun); 2759288348Smav } 2760288348Smav } 2761288348Smav ctl_start_lun(cbe_lun); 2762229997Sken } else { 2763288348Smav ctl_stop_lun(cbe_lun); 2764288348Smav if (cdb->how & SSS_LOEJ) { 2765288348Smav cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA; 2766288348Smav cbe_lun->flags |= CTL_LUN_FLAG_EJECTED; 2767288348Smav ctl_lun_ejected(cbe_lun); 2768288348Smav if (be_lun->vn != NULL) 2769288348Smav ctl_be_block_close(be_lun); 2770288348Smav } 2771229997Sken } 2772288348Smav 2773288348Smav ctl_set_success(&io->scsiio); 2774229997Sken ctl_config_write_done(io); 2775229997Sken break; 2776229997Sken } 2777288310Smav case PREVENT_ALLOW: 2778288310Smav ctl_set_success(&io->scsiio); 2779288310Smav ctl_config_write_done(io); 2780288310Smav break; 2781229997Sken default: 2782229997Sken ctl_set_invalid_opcode(&io->scsiio); 2783229997Sken ctl_config_write_done(io); 2784229997Sken retval = CTL_RETVAL_COMPLETE; 2785229997Sken break; 2786229997Sken } 2787229997Sken 2788229997Sken return (retval); 2789229997Sken} 2790229997Sken 2791229997Skenstatic int 2792229997Skenctl_be_block_config_read(union ctl_io *io) 2793229997Sken{ 2794275474Smav struct ctl_be_block_lun *be_lun; 2795287499Smav struct ctl_be_lun *cbe_lun; 2796275474Smav int retval = 0; 2797275474Smav 2798275474Smav DPRINTF("entered\n"); 2799275474Smav 2800287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2801275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2802287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2803275474Smav 2804275474Smav switch (io->scsiio.cdb[0]) { 2805275474Smav case SERVICE_ACTION_IN: 2806275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2807275474Smav mtx_lock(&be_lun->queue_lock); 2808275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2809275474Smav &io->io_hdr, links); 2810275474Smav mtx_unlock(&be_lun->queue_lock); 2811275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2812275474Smav &be_lun->io_task); 2813275474Smav retval = CTL_RETVAL_QUEUED; 2814275474Smav break; 2815275474Smav } 2816275474Smav ctl_set_invalid_field(&io->scsiio, 2817275474Smav /*sks_valid*/ 1, 2818275474Smav /*command*/ 1, 2819275474Smav /*field*/ 1, 2820275474Smav /*bit_valid*/ 1, 2821275474Smav /*bit*/ 4); 2822275474Smav ctl_config_read_done(io); 2823275474Smav retval = CTL_RETVAL_COMPLETE; 2824275474Smav break; 2825275474Smav default: 2826275474Smav ctl_set_invalid_opcode(&io->scsiio); 2827275474Smav ctl_config_read_done(io); 2828275474Smav retval = CTL_RETVAL_COMPLETE; 2829275474Smav break; 2830275474Smav } 2831275474Smav 2832275474Smav return (retval); 2833229997Sken} 2834229997Sken 2835229997Skenstatic int 2836229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2837229997Sken{ 2838229997Sken struct ctl_be_block_lun *lun; 2839229997Sken int retval; 2840229997Sken 2841229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2842229997Sken 2843268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2844229997Sken if (retval != 0) 2845229997Sken goto bailout; 2846229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2847229997Sken if (retval != 0) 2848229997Sken goto bailout; 2849268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2850229997Sken 2851229997Skenbailout: 2852229997Sken return (retval); 2853229997Sken} 2854229997Sken 2855274154Smavstatic uint64_t 2856274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2857274154Smav{ 2858274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2859274154Smav 2860274154Smav if (lun->getattr == NULL) 2861274154Smav return (UINT64_MAX); 2862274154Smav return (lun->getattr(lun, attrname)); 2863274154Smav} 2864274154Smav 2865229997Skenint 2866229997Skenctl_be_block_init(void) 2867229997Sken{ 2868229997Sken struct ctl_be_block_softc *softc; 2869229997Sken int retval; 2870229997Sken 2871229997Sken softc = &backend_block_softc; 2872229997Sken retval = 0; 2873229997Sken 2874267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2875264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2876264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2877229997Sken STAILQ_INIT(&softc->lun_list); 2878229997Sken 2879229997Sken return (retval); 2880229997Sken} 2881