ctl_backend_block.c revision 288310
1229997Sken/*- 2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp. 3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation 4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation 5229997Sken * All rights reserved. 6229997Sken * 7232604Strasz * Portions of this software were developed by Edward Tomasz Napierala 8232604Strasz * under sponsorship from the FreeBSD Foundation. 9232604Strasz * 10229997Sken * Redistribution and use in source and binary forms, with or without 11229997Sken * modification, are permitted provided that the following conditions 12229997Sken * are met: 13229997Sken * 1. Redistributions of source code must retain the above copyright 14229997Sken * notice, this list of conditions, and the following disclaimer, 15229997Sken * without modification. 16229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17229997Sken * substantially similar to the "NO WARRANTY" disclaimer below 18229997Sken * ("Disclaimer") and any redistribution must be conditioned upon 19229997Sken * including a substantially similar Disclaimer requirement for further 20229997Sken * binary redistribution. 21229997Sken * 22229997Sken * NO WARRANTY 23229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33229997Sken * POSSIBILITY OF SUCH DAMAGES. 34229997Sken * 35229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 36229997Sken */ 37229997Sken/* 38229997Sken * CAM Target Layer driver backend for block devices. 39229997Sken * 40229997Sken * Author: Ken Merry <ken@FreeBSD.org> 41229997Sken */ 42229997Sken#include <sys/cdefs.h> 43229997Sken__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 288310 2015-09-27 13:47:28Z mav $"); 44229997Sken 45229997Sken#include <sys/param.h> 46229997Sken#include <sys/systm.h> 47229997Sken#include <sys/kernel.h> 48229997Sken#include <sys/types.h> 49229997Sken#include <sys/kthread.h> 50229997Sken#include <sys/bio.h> 51229997Sken#include <sys/fcntl.h> 52264274Smav#include <sys/limits.h> 53229997Sken#include <sys/lock.h> 54229997Sken#include <sys/mutex.h> 55229997Sken#include <sys/condvar.h> 56229997Sken#include <sys/malloc.h> 57229997Sken#include <sys/conf.h> 58229997Sken#include <sys/ioccom.h> 59229997Sken#include <sys/queue.h> 60229997Sken#include <sys/sbuf.h> 61229997Sken#include <sys/endian.h> 62229997Sken#include <sys/uio.h> 63229997Sken#include <sys/buf.h> 64229997Sken#include <sys/taskqueue.h> 65229997Sken#include <sys/vnode.h> 66229997Sken#include <sys/namei.h> 67229997Sken#include <sys/mount.h> 68229997Sken#include <sys/disk.h> 69229997Sken#include <sys/fcntl.h> 70229997Sken#include <sys/filedesc.h> 71275474Smav#include <sys/filio.h> 72229997Sken#include <sys/proc.h> 73229997Sken#include <sys/pcpu.h> 74229997Sken#include <sys/module.h> 75229997Sken#include <sys/sdt.h> 76229997Sken#include <sys/devicestat.h> 77229997Sken#include <sys/sysctl.h> 78229997Sken 79229997Sken#include <geom/geom.h> 80229997Sken 81229997Sken#include <cam/cam.h> 82229997Sken#include <cam/scsi/scsi_all.h> 83229997Sken#include <cam/scsi/scsi_da.h> 84229997Sken#include <cam/ctl/ctl_io.h> 85229997Sken#include <cam/ctl/ctl.h> 86229997Sken#include <cam/ctl/ctl_backend.h> 87229997Sken#include <cam/ctl/ctl_ioctl.h> 88287621Smav#include <cam/ctl/ctl_ha.h> 89229997Sken#include <cam/ctl/ctl_scsi_all.h> 90287621Smav#include <cam/ctl/ctl_private.h> 91229997Sken#include <cam/ctl/ctl_error.h> 92229997Sken 93229997Sken/* 94264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB 95264886Smav * I/O. If we get an I/O larger than that, we'll split it. 96229997Sken */ 97267537Smav#define CTLBLK_HALF_IO_SIZE (512 * 1024) 98267537Smav#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 99264886Smav#define CTLBLK_MAX_SEG MAXPHYS 100267537Smav#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 101267537Smav#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 102229997Sken 103229997Sken#ifdef CTLBLK_DEBUG 104229997Sken#define DPRINTF(fmt, args...) \ 105229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 106229997Sken#else 107229997Sken#define DPRINTF(fmt, args...) do {} while(0) 108229997Sken#endif 109229997Sken 110267519Smav#define PRIV(io) \ 111267519Smav ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 112267537Smav#define ARGS(io) \ 113267537Smav ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 114267519Smav 115229997SkenSDT_PROVIDER_DEFINE(cbb); 116229997Sken 117229997Skentypedef enum { 118229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 119229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 120229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 121229997Sken} ctl_be_block_lun_flags; 122229997Sken 123229997Skentypedef enum { 124229997Sken CTL_BE_BLOCK_NONE, 125229997Sken CTL_BE_BLOCK_DEV, 126229997Sken CTL_BE_BLOCK_FILE 127229997Sken} ctl_be_block_type; 128229997Sken 129229997Skenstruct ctl_be_block_filedata { 130229997Sken struct ucred *cred; 131229997Sken}; 132229997Sken 133229997Skenunion ctl_be_block_bedata { 134229997Sken struct ctl_be_block_filedata file; 135229997Sken}; 136229997Sken 137229997Skenstruct ctl_be_block_io; 138229997Skenstruct ctl_be_block_lun; 139229997Sken 140229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 141229997Sken struct ctl_be_block_io *beio); 142274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 143274154Smav const char *attrname); 144229997Sken 145229997Sken/* 146229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 147229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 148229997Sken */ 149229997Skenstruct ctl_be_block_lun { 150272911Smav struct ctl_lun_create_params params; 151229997Sken char lunname[32]; 152229997Sken char *dev_path; 153229997Sken ctl_be_block_type dev_type; 154229997Sken struct vnode *vn; 155229997Sken union ctl_be_block_bedata backend; 156229997Sken cbb_dispatch_t dispatch; 157229997Sken cbb_dispatch_t lun_flush; 158264274Smav cbb_dispatch_t unmap; 159275474Smav cbb_dispatch_t get_lba_status; 160274154Smav cbb_getattr_t getattr; 161229997Sken uma_zone_t lun_zone; 162229997Sken uint64_t size_blocks; 163229997Sken uint64_t size_bytes; 164229997Sken struct ctl_be_block_softc *softc; 165229997Sken struct devstat *disk_stats; 166229997Sken ctl_be_block_lun_flags flags; 167229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 168287499Smav struct ctl_be_lun cbe_lun; 169229997Sken struct taskqueue *io_taskqueue; 170229997Sken struct task io_task; 171229997Sken int num_threads; 172229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 173275474Smav STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 174229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 175229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 176267877Smav struct mtx_padalign io_lock; 177267877Smav struct mtx_padalign queue_lock; 178229997Sken}; 179229997Sken 180229997Sken/* 181229997Sken * Overall softc structure for the block backend module. 182229997Sken */ 183229997Skenstruct ctl_be_block_softc { 184229997Sken struct mtx lock; 185229997Sken int num_luns; 186229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 187229997Sken}; 188229997Sken 189229997Skenstatic struct ctl_be_block_softc backend_block_softc; 190229997Sken 191229997Sken/* 192229997Sken * Per-I/O information. 193229997Sken */ 194229997Skenstruct ctl_be_block_io { 195229997Sken union ctl_io *io; 196229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 197229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 198229997Sken int bio_cmd; 199229997Sken int num_segs; 200229997Sken int num_bios_sent; 201229997Sken int num_bios_done; 202229997Sken int send_complete; 203229997Sken int num_errors; 204229997Sken struct bintime ds_t0; 205229997Sken devstat_tag_type ds_tag_type; 206229997Sken devstat_trans_flags ds_trans_type; 207229997Sken uint64_t io_len; 208229997Sken uint64_t io_offset; 209286353Smav int io_arg; 210229997Sken struct ctl_be_block_softc *softc; 211229997Sken struct ctl_be_block_lun *lun; 212264274Smav void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 213229997Sken}; 214229997Sken 215287621Smavextern struct ctl_softc *control_softc; 216287621Smav 217229997Skenstatic int cbb_num_threads = 14; 218229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 219229997Sken "CAM Target Layer Block Backend"); 220267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 221229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 222229997Sken 223229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 224229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 225229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 226229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 227229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 228229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 229229997Sken struct ctl_be_block_io *beio); 230229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 231229997Sken struct ctl_be_block_io *beio); 232275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 233275474Smav struct ctl_be_block_io *beio); 234275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 235275481Smav const char *attrname); 236229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 237229997Sken struct ctl_be_block_io *beio); 238264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 239264274Smav struct ctl_be_block_io *beio); 240229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 241229997Sken struct ctl_be_block_io *beio); 242274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 243274154Smav const char *attrname); 244275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 245275474Smav union ctl_io *io); 246229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 247229997Sken union ctl_io *io); 248229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 249229997Sken union ctl_io *io); 250229997Skenstatic void ctl_be_block_worker(void *context, int pending); 251229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 252229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 253229997Sken int flag, struct thread *td); 254229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 255229997Sken struct ctl_lun_req *req); 256229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 257229997Sken struct ctl_lun_req *req); 258229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 259229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 260229997Sken 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 1663287670Smav * CTL_LUN_FLAG_OFFLINE 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; 1675287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 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); 1689287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 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); 1703287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 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); 1717287670Smav if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) { 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 /* 1844229997Sken * Verify that we have the ability to upgrade to exclusive 1845229997Sken * access on this file so we can trap errors at open instead 1846229997Sken * of reporting them during first access. 1847229997Sken */ 1848229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1849229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1850229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1851229997Sken error = EBADF; 1852229997Sken snprintf(req->error_str, sizeof(req->error_str), 1853229997Sken "error locking file %s", be_lun->dev_path); 1854229997Sken return (error); 1855229997Sken } 1856229997Sken } 1857229997Sken 1858229997Sken file_data->cred = crhold(curthread->td_ucred); 1859232604Strasz if (params->lun_size_bytes != 0) 1860232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1861232604Strasz else 1862232604Strasz be_lun->size_bytes = vattr.va_size; 1863229997Sken 1864229997Sken /* 1865273029Smav * For files we can use any logical block size. Prefer 512 bytes 1866273029Smav * for compatibility reasons. If file's vattr.va_blocksize 1867273029Smav * (preferred I/O block size) is bigger and multiple to chosen 1868273029Smav * logical block size -- report it as physical block size. 1869229997Sken */ 1870229997Sken if (params->blocksize_bytes != 0) 1871287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1872288310Smav else if (cbe_lun->lun_type == T_CDROM) 1873288310Smav cbe_lun->blocksize = 2048; 1874229997Sken else 1875287499Smav cbe_lun->blocksize = 512; 1876287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1877287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1878287499Smav 0 : (be_lun->size_blocks - 1); 1879275865Smav 1880275865Smav us = ps = vattr.va_blocksize; 1881275865Smav uo = po = 0; 1882275865Smav 1883287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1884275865Smav if (value != NULL) 1885275865Smav ctl_expand_number(value, &ps); 1886287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1887275865Smav if (value != NULL) 1888275865Smav ctl_expand_number(value, &po); 1889287499Smav pss = ps / cbe_lun->blocksize; 1890287499Smav pos = po / cbe_lun->blocksize; 1891287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1892287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1893287499Smav cbe_lun->pblockexp = fls(pss) - 1; 1894287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 1895273029Smav } 1896229997Sken 1897287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1898275865Smav if (value != NULL) 1899275865Smav ctl_expand_number(value, &us); 1900287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1901275865Smav if (value != NULL) 1902275865Smav ctl_expand_number(value, &uo); 1903287499Smav uss = us / cbe_lun->blocksize; 1904287499Smav uos = uo / cbe_lun->blocksize; 1905287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1906287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1907287499Smav cbe_lun->ublockexp = fls(uss) - 1; 1908287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 1909275865Smav } 1910275865Smav 1911229997Sken /* 1912229997Sken * Sanity check. The media size has to be at least one 1913229997Sken * sector long. 1914229997Sken */ 1915287499Smav if (be_lun->size_bytes < cbe_lun->blocksize) { 1916229997Sken error = EINVAL; 1917229997Sken snprintf(req->error_str, sizeof(req->error_str), 1918229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1919287499Smav (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1920229997Sken } 1921275920Smav 1922287499Smav cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1923229997Sken return (error); 1924229997Sken} 1925229997Sken 1926229997Skenstatic int 1927229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1928229997Sken{ 1929287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1930229997Sken struct ctl_lun_create_params *params; 1931287664Smav struct cdevsw *csw; 1932229997Sken struct cdev *dev; 1933275865Smav char *value; 1934287664Smav int error, atomic, maxio, ref, unmap, tmp; 1935287221Smav off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1936229997Sken 1937272911Smav params = &be_lun->params; 1938229997Sken 1939229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1940287664Smav csw = devvn_refthread(be_lun->vn, &dev, &ref); 1941287664Smav if (csw == NULL) 1942287664Smav return (ENXIO); 1943287664Smav if (strcmp(csw->d_name, "zvol") == 0) { 1944269123Smav be_lun->dispatch = ctl_be_block_dispatch_zvol; 1945275474Smav be_lun->get_lba_status = ctl_be_block_gls_zvol; 1946275920Smav atomic = maxio = CTLBLK_MAX_IO_SIZE; 1947275920Smav } else { 1948269123Smav be_lun->dispatch = ctl_be_block_dispatch_dev; 1949287499Smav be_lun->get_lba_status = NULL; 1950275920Smav atomic = 0; 1951287664Smav maxio = dev->si_iosize_max; 1952275920Smav if (maxio <= 0) 1953275920Smav maxio = DFLTPHYS; 1954275920Smav if (maxio > CTLBLK_MAX_IO_SIZE) 1955275920Smav maxio = CTLBLK_MAX_IO_SIZE; 1956275920Smav } 1957269123Smav be_lun->lun_flush = ctl_be_block_flush_dev; 1958274154Smav be_lun->getattr = ctl_be_block_getattr_dev; 1959287499Smav be_lun->unmap = ctl_be_block_unmap_dev; 1960229997Sken 1961287664Smav if (!csw->d_ioctl) { 1962287664Smav dev_relthread(dev, ref); 1963229997Sken snprintf(req->error_str, sizeof(req->error_str), 1964287664Smav "no d_ioctl for device %s!", be_lun->dev_path); 1965229997Sken return (ENODEV); 1966229997Sken } 1967229997Sken 1968287664Smav error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1969229997Sken curthread); 1970229997Sken if (error) { 1971287664Smav dev_relthread(dev, ref); 1972229997Sken snprintf(req->error_str, sizeof(req->error_str), 1973272911Smav "error %d returned for DIOCGSECTORSIZE ioctl " 1974272911Smav "on %s!", error, be_lun->dev_path); 1975229997Sken return (error); 1976229997Sken } 1977229997Sken 1978229997Sken /* 1979229997Sken * If the user has asked for a blocksize that is greater than the 1980229997Sken * backing device's blocksize, we can do it only if the blocksize 1981229997Sken * the user is asking for is an even multiple of the underlying 1982229997Sken * device's blocksize. 1983229997Sken */ 1984286811Smav if ((params->blocksize_bytes != 0) && 1985286811Smav (params->blocksize_bytes >= tmp)) { 1986286811Smav if (params->blocksize_bytes % tmp == 0) { 1987287499Smav cbe_lun->blocksize = params->blocksize_bytes; 1988229997Sken } else { 1989287664Smav dev_relthread(dev, ref); 1990229997Sken snprintf(req->error_str, sizeof(req->error_str), 1991272911Smav "requested blocksize %u is not an even " 1992229997Sken "multiple of backing device blocksize %u", 1993287221Smav params->blocksize_bytes, tmp); 1994229997Sken return (EINVAL); 1995229997Sken } 1996286811Smav } else if (params->blocksize_bytes != 0) { 1997287664Smav dev_relthread(dev, ref); 1998229997Sken snprintf(req->error_str, sizeof(req->error_str), 1999272911Smav "requested blocksize %u < backing device " 2000287221Smav "blocksize %u", params->blocksize_bytes, tmp); 2001229997Sken return (EINVAL); 2002288310Smav } else if (cbe_lun->lun_type == T_CDROM) 2003288310Smav cbe_lun->blocksize = MAX(tmp, 2048); 2004288310Smav else 2005287499Smav cbe_lun->blocksize = tmp; 2006229997Sken 2007287664Smav error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 2008287664Smav curthread); 2009229997Sken if (error) { 2010287664Smav dev_relthread(dev, ref); 2011229997Sken snprintf(req->error_str, sizeof(req->error_str), 2012272911Smav "error %d returned for DIOCGMEDIASIZE " 2013272911Smav " ioctl on %s!", error, 2014232604Strasz be_lun->dev_path); 2015229997Sken return (error); 2016229997Sken } 2017229997Sken 2018232604Strasz if (params->lun_size_bytes != 0) { 2019287221Smav if (params->lun_size_bytes > otmp) { 2020287664Smav dev_relthread(dev, ref); 2021232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2022272911Smav "requested LUN size %ju > backing device " 2023272911Smav "size %ju", 2024232604Strasz (uintmax_t)params->lun_size_bytes, 2025287221Smav (uintmax_t)otmp); 2026232604Strasz return (EINVAL); 2027232604Strasz } 2028232604Strasz 2029232604Strasz be_lun->size_bytes = params->lun_size_bytes; 2030286811Smav } else 2031287221Smav be_lun->size_bytes = otmp; 2032287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2033287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2034287499Smav 0 : (be_lun->size_blocks - 1); 2035232604Strasz 2036287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 2037287664Smav curthread); 2038264191Smav if (error) 2039264191Smav ps = po = 0; 2040264191Smav else { 2041287664Smav error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2042287664Smav FREAD, curthread); 2043264191Smav if (error) 2044264191Smav po = 0; 2045264191Smav } 2046275865Smav us = ps; 2047275865Smav uo = po; 2048275865Smav 2049287499Smav value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 2050275865Smav if (value != NULL) 2051275865Smav ctl_expand_number(value, &ps); 2052287499Smav value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 2053275865Smav if (value != NULL) 2054275865Smav ctl_expand_number(value, &po); 2055287499Smav pss = ps / cbe_lun->blocksize; 2056287499Smav pos = po / cbe_lun->blocksize; 2057287499Smav if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2058287499Smav ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2059287499Smav cbe_lun->pblockexp = fls(pss) - 1; 2060287499Smav cbe_lun->pblockoff = (pss - pos) % pss; 2061264191Smav } 2062264191Smav 2063287499Smav value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 2064275865Smav if (value != NULL) 2065275865Smav ctl_expand_number(value, &us); 2066287499Smav value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 2067275865Smav if (value != NULL) 2068275865Smav ctl_expand_number(value, &uo); 2069287499Smav uss = us / cbe_lun->blocksize; 2070287499Smav uos = uo / cbe_lun->blocksize; 2071287499Smav if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2072287499Smav ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2073287499Smav cbe_lun->ublockexp = fls(uss) - 1; 2074287499Smav cbe_lun->ublockoff = (uss - uos) % uss; 2075275865Smav } 2076275865Smav 2077287499Smav cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2078287499Smav cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2079278672Smav 2080278672Smav if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2081278672Smav unmap = 1; 2082278672Smav } else { 2083278672Smav struct diocgattr_arg arg; 2084278672Smav 2085278672Smav strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2086278672Smav arg.len = sizeof(arg.value.i); 2087287664Smav error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2088287664Smav curthread); 2089278672Smav unmap = (error == 0) ? arg.value.i : 0; 2090278672Smav } 2091287499Smav value = ctl_get_opt(&cbe_lun->options, "unmap"); 2092278672Smav if (value != NULL) 2093278672Smav unmap = (strcmp(value, "on") == 0); 2094278672Smav if (unmap) 2095287499Smav cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2096287499Smav else 2097287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2098278672Smav 2099287664Smav dev_relthread(dev, ref); 2100229997Sken return (0); 2101229997Sken} 2102229997Sken 2103229997Skenstatic int 2104229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 2105229997Sken{ 2106287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2107287499Smav int flags; 2108287499Smav 2109229997Sken if (be_lun->vn) { 2110287499Smav flags = FREAD; 2111287499Smav if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2112287499Smav flags |= FWRITE; 2113229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2114229997Sken be_lun->vn = NULL; 2115229997Sken 2116229997Sken switch (be_lun->dev_type) { 2117229997Sken case CTL_BE_BLOCK_DEV: 2118229997Sken break; 2119229997Sken case CTL_BE_BLOCK_FILE: 2120229997Sken if (be_lun->backend.file.cred != NULL) { 2121229997Sken crfree(be_lun->backend.file.cred); 2122229997Sken be_lun->backend.file.cred = NULL; 2123229997Sken } 2124229997Sken break; 2125229997Sken case CTL_BE_BLOCK_NONE: 2126258871Strasz break; 2127229997Sken default: 2128229997Sken panic("Unexpected backend type."); 2129229997Sken break; 2130229997Sken } 2131272911Smav be_lun->dev_type = CTL_BE_BLOCK_NONE; 2132229997Sken } 2133229997Sken return (0); 2134229997Sken} 2135229997Sken 2136229997Skenstatic int 2137229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 2138287499Smav struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2139229997Sken{ 2140287499Smav struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2141229997Sken struct nameidata nd; 2142287499Smav char *value; 2143287499Smav int error, flags; 2144229997Sken 2145229997Sken error = 0; 2146229997Sken if (rootvnode == NULL) { 2147229997Sken snprintf(req->error_str, sizeof(req->error_str), 2148272911Smav "Root filesystem is not mounted"); 2149229997Sken return (1); 2150229997Sken } 2151285391Smjg pwd_ensure_dirs(); 2152229997Sken 2153287499Smav value = ctl_get_opt(&cbe_lun->options, "file"); 2154287499Smav if (value == NULL) { 2155287499Smav snprintf(req->error_str, sizeof(req->error_str), 2156287499Smav "no file argument specified"); 2157287499Smav return (1); 2158287499Smav } 2159287499Smav free(be_lun->dev_path, M_CTLBLK); 2160287499Smav be_lun->dev_path = strdup(value, M_CTLBLK); 2161287499Smav 2162287499Smav flags = FREAD; 2163287499Smav value = ctl_get_opt(&cbe_lun->options, "readonly"); 2164288310Smav if (value != NULL) { 2165288310Smav if (strcmp(value, "on") != 0) 2166288310Smav flags |= FWRITE; 2167288310Smav } else if (cbe_lun->lun_type == T_DIRECT) 2168287499Smav flags |= FWRITE; 2169287499Smav 2170287499Smavagain: 2171229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2172229997Sken error = vn_open(&nd, &flags, 0, NULL); 2173287499Smav if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2174287499Smav flags &= ~FWRITE; 2175287499Smav goto again; 2176287499Smav } 2177229997Sken if (error) { 2178229997Sken /* 2179229997Sken * This is the only reasonable guess we can make as far as 2180229997Sken * path if the user doesn't give us a fully qualified path. 2181229997Sken * If they want to specify a file, they need to specify the 2182229997Sken * full path. 2183229997Sken */ 2184229997Sken if (be_lun->dev_path[0] != '/') { 2185229997Sken char *dev_name; 2186229997Sken 2187287499Smav asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2188287499Smav be_lun->dev_path); 2189287499Smav free(be_lun->dev_path, M_CTLBLK); 2190287499Smav be_lun->dev_path = dev_name; 2191287499Smav goto again; 2192229997Sken } 2193229997Sken snprintf(req->error_str, sizeof(req->error_str), 2194272911Smav "error opening %s: %d", be_lun->dev_path, error); 2195229997Sken return (error); 2196229997Sken } 2197287499Smav if (flags & FWRITE) 2198287499Smav cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2199287499Smav else 2200287499Smav cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2201229997Sken 2202229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 2203229997Sken be_lun->vn = nd.ni_vp; 2204229997Sken 2205229997Sken /* We only support disks and files. */ 2206229997Sken if (vn_isdisk(be_lun->vn, &error)) { 2207229997Sken error = ctl_be_block_open_dev(be_lun, req); 2208229997Sken } else if (be_lun->vn->v_type == VREG) { 2209229997Sken error = ctl_be_block_open_file(be_lun, req); 2210229997Sken } else { 2211229997Sken error = EINVAL; 2212229997Sken snprintf(req->error_str, sizeof(req->error_str), 2213258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 2214229997Sken } 2215229997Sken VOP_UNLOCK(be_lun->vn, 0); 2216229997Sken 2217286811Smav if (error != 0) 2218229997Sken ctl_be_block_close(be_lun); 2219287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2220287499Smav if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2221287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2222287499Smav value = ctl_get_opt(&cbe_lun->options, "serseq"); 2223287499Smav if (value != NULL && strcmp(value, "on") == 0) 2224287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2225287499Smav else if (value != NULL && strcmp(value, "read") == 0) 2226287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2227287499Smav else if (value != NULL && strcmp(value, "off") == 0) 2228287499Smav cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2229229997Sken return (0); 2230229997Sken} 2231229997Sken 2232229997Skenstatic int 2233229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2234229997Sken{ 2235287499Smav struct ctl_be_lun *cbe_lun; 2236229997Sken struct ctl_be_block_lun *be_lun; 2237229997Sken struct ctl_lun_create_params *params; 2238267481Smav char num_thread_str[16]; 2239229997Sken char tmpstr[32]; 2240267481Smav char *value; 2241278672Smav int retval, num_threads; 2242267481Smav int tmp_num_threads; 2243229997Sken 2244229997Sken params = &req->reqdata.create; 2245229997Sken retval = 0; 2246272911Smav req->status = CTL_LUN_OK; 2247229997Sken 2248229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2249287499Smav cbe_lun = &be_lun->cbe_lun; 2250287499Smav cbe_lun->be_lun = be_lun; 2251272911Smav be_lun->params = req->reqdata.create; 2252229997Sken be_lun->softc = softc; 2253229997Sken STAILQ_INIT(&be_lun->input_queue); 2254275474Smav STAILQ_INIT(&be_lun->config_read_queue); 2255229997Sken STAILQ_INIT(&be_lun->config_write_queue); 2256229997Sken STAILQ_INIT(&be_lun->datamove_queue); 2257229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2258267877Smav mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2259267877Smav mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2260287499Smav ctl_init_opts(&cbe_lun->options, 2261268280Smav req->num_be_args, req->kern_be_args); 2262264886Smav be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2263256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2264229997Sken if (be_lun->lun_zone == NULL) { 2265229997Sken snprintf(req->error_str, sizeof(req->error_str), 2266272911Smav "error allocating UMA zone"); 2267229997Sken goto bailout_error; 2268229997Sken } 2269229997Sken 2270229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2271287499Smav cbe_lun->lun_type = params->device_type; 2272229997Sken else 2273287499Smav cbe_lun->lun_type = T_DIRECT; 2274287499Smav be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2275287621Smav cbe_lun->flags = 0; 2276287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2277287621Smav if (value != NULL) { 2278287621Smav if (strcmp(value, "primary") == 0) 2279287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2280287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2281287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2282229997Sken 2283288310Smav if (cbe_lun->lun_type == T_DIRECT || 2284288310Smav cbe_lun->lun_type == T_CDROM) { 2285286811Smav be_lun->size_bytes = params->lun_size_bytes; 2286286811Smav if (params->blocksize_bytes != 0) 2287287499Smav cbe_lun->blocksize = params->blocksize_bytes; 2288288310Smav else if (cbe_lun->lun_type == T_CDROM) 2289288310Smav cbe_lun->blocksize = 2048; 2290286811Smav else 2291287499Smav cbe_lun->blocksize = 512; 2292287499Smav be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2293287499Smav cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2294287499Smav 0 : (be_lun->size_blocks - 1); 2295229997Sken 2296287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2297287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2298287621Smav retval = ctl_be_block_open(softc, be_lun, req); 2299287621Smav if (retval != 0) { 2300287621Smav retval = 0; 2301287621Smav req->status = CTL_LUN_WARNING; 2302287621Smav } 2303229997Sken } 2304287499Smav num_threads = cbb_num_threads; 2305229997Sken } else { 2306229997Sken num_threads = 1; 2307229997Sken } 2308229997Sken 2309287499Smav value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2310267481Smav if (value != NULL) { 2311267481Smav tmp_num_threads = strtol(value, NULL, 0); 2312229997Sken 2313267481Smav /* 2314267481Smav * We don't let the user specify less than one 2315267481Smav * thread, but hope he's clueful enough not to 2316267481Smav * specify 1000 threads. 2317267481Smav */ 2318267481Smav if (tmp_num_threads < 1) { 2319267481Smav snprintf(req->error_str, sizeof(req->error_str), 2320272911Smav "invalid number of threads %s", 2321272911Smav num_thread_str); 2322267481Smav goto bailout_error; 2323229997Sken } 2324267481Smav num_threads = tmp_num_threads; 2325229997Sken } 2326229997Sken 2327272911Smav if (be_lun->vn == NULL) 2328287499Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2329229997Sken /* Tell the user the blocksize we ended up using */ 2330272911Smav params->lun_size_bytes = be_lun->size_bytes; 2331287499Smav params->blocksize_bytes = cbe_lun->blocksize; 2332229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2333287499Smav cbe_lun->req_lun_id = params->req_lun_id; 2334287499Smav cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2335229997Sken } else 2336287499Smav cbe_lun->req_lun_id = 0; 2337229997Sken 2338287499Smav cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2339287499Smav cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2340287499Smav cbe_lun->be = &ctl_be_block_driver; 2341229997Sken 2342229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2343229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2344229997Sken softc->num_luns); 2345287499Smav strncpy((char *)cbe_lun->serial_num, tmpstr, 2346287499Smav MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2347229997Sken 2348229997Sken /* Tell the user what we used for a serial number */ 2349229997Sken strncpy((char *)params->serial_num, tmpstr, 2350275953Smav MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2351229997Sken } else { 2352287499Smav strncpy((char *)cbe_lun->serial_num, params->serial_num, 2353287499Smav MIN(sizeof(cbe_lun->serial_num), 2354229997Sken sizeof(params->serial_num))); 2355229997Sken } 2356229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2357229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2358287499Smav strncpy((char *)cbe_lun->device_id, tmpstr, 2359287499Smav MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2360229997Sken 2361229997Sken /* Tell the user what we used for a device ID */ 2362229997Sken strncpy((char *)params->device_id, tmpstr, 2363275953Smav MIN(sizeof(params->device_id), sizeof(tmpstr))); 2364229997Sken } else { 2365287499Smav strncpy((char *)cbe_lun->device_id, params->device_id, 2366287499Smav MIN(sizeof(cbe_lun->device_id), 2367275953Smav sizeof(params->device_id))); 2368229997Sken } 2369229997Sken 2370229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2371229997Sken 2372229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2373229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2374229997Sken 2375229997Sken if (be_lun->io_taskqueue == NULL) { 2376229997Sken snprintf(req->error_str, sizeof(req->error_str), 2377272911Smav "unable to create taskqueue"); 2378229997Sken goto bailout_error; 2379229997Sken } 2380229997Sken 2381229997Sken /* 2382229997Sken * Note that we start the same number of threads by default for 2383229997Sken * both the file case and the block device case. For the file 2384229997Sken * case, we need multiple threads to allow concurrency, because the 2385229997Sken * vnode interface is designed to be a blocking interface. For the 2386229997Sken * block device case, ZFS zvols at least will block the caller's 2387229997Sken * context in many instances, and so we need multiple threads to 2388229997Sken * overcome that problem. Other block devices don't need as many 2389229997Sken * threads, but they shouldn't cause too many problems. 2390229997Sken * 2391229997Sken * If the user wants to just have a single thread for a block 2392229997Sken * device, he can specify that when the LUN is created, or change 2393229997Sken * the tunable/sysctl to alter the default number of threads. 2394229997Sken */ 2395229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2396229997Sken /*num threads*/num_threads, 2397229997Sken /*priority*/PWAIT, 2398229997Sken /*thread name*/ 2399229997Sken "%s taskq", be_lun->lunname); 2400229997Sken 2401229997Sken if (retval != 0) 2402229997Sken goto bailout_error; 2403229997Sken 2404229997Sken be_lun->num_threads = num_threads; 2405229997Sken 2406229997Sken mtx_lock(&softc->lock); 2407229997Sken softc->num_luns++; 2408229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2409229997Sken 2410229997Sken mtx_unlock(&softc->lock); 2411229997Sken 2412287499Smav retval = ctl_add_lun(&be_lun->cbe_lun); 2413229997Sken if (retval != 0) { 2414229997Sken mtx_lock(&softc->lock); 2415229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2416229997Sken links); 2417229997Sken softc->num_luns--; 2418229997Sken mtx_unlock(&softc->lock); 2419229997Sken snprintf(req->error_str, sizeof(req->error_str), 2420272911Smav "ctl_add_lun() returned error %d, see dmesg for " 2421272911Smav "details", retval); 2422229997Sken retval = 0; 2423229997Sken goto bailout_error; 2424229997Sken } 2425229997Sken 2426229997Sken mtx_lock(&softc->lock); 2427229997Sken 2428229997Sken /* 2429229997Sken * Tell the config_status routine that we're waiting so it won't 2430229997Sken * clean up the LUN in the event of an error. 2431229997Sken */ 2432229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2433229997Sken 2434229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2435229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2436229997Sken if (retval == EINTR) 2437229997Sken break; 2438229997Sken } 2439229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2440229997Sken 2441229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2442229997Sken snprintf(req->error_str, sizeof(req->error_str), 2443272911Smav "LUN configuration error, see dmesg for details"); 2444229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2445229997Sken links); 2446229997Sken softc->num_luns--; 2447229997Sken mtx_unlock(&softc->lock); 2448229997Sken goto bailout_error; 2449229997Sken } else { 2450287499Smav params->req_lun_id = cbe_lun->lun_id; 2451229997Sken } 2452229997Sken 2453229997Sken mtx_unlock(&softc->lock); 2454229997Sken 2455229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2456287499Smav cbe_lun->blocksize, 2457229997Sken DEVSTAT_ALL_SUPPORTED, 2458287499Smav cbe_lun->lun_type 2459229997Sken | DEVSTAT_TYPE_IF_OTHER, 2460229997Sken DEVSTAT_PRIORITY_OTHER); 2461229997Sken 2462229997Sken return (retval); 2463229997Sken 2464229997Skenbailout_error: 2465229997Sken req->status = CTL_LUN_ERROR; 2466229997Sken 2467267429Smav if (be_lun->io_taskqueue != NULL) 2468267429Smav taskqueue_free(be_lun->io_taskqueue); 2469229997Sken ctl_be_block_close(be_lun); 2470267429Smav if (be_lun->dev_path != NULL) 2471267429Smav free(be_lun->dev_path, M_CTLBLK); 2472267429Smav if (be_lun->lun_zone != NULL) 2473267429Smav uma_zdestroy(be_lun->lun_zone); 2474287499Smav ctl_free_opts(&cbe_lun->options); 2475267877Smav mtx_destroy(&be_lun->queue_lock); 2476267877Smav mtx_destroy(&be_lun->io_lock); 2477229997Sken free(be_lun, M_CTLBLK); 2478229997Sken 2479229997Sken return (retval); 2480229997Sken} 2481229997Sken 2482229997Skenstatic int 2483229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2484229997Sken{ 2485229997Sken struct ctl_lun_rm_params *params; 2486229997Sken struct ctl_be_block_lun *be_lun; 2487287670Smav struct ctl_be_lun *cbe_lun; 2488229997Sken int retval; 2489229997Sken 2490229997Sken params = &req->reqdata.rm; 2491229997Sken 2492229997Sken mtx_lock(&softc->lock); 2493229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2494287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2495229997Sken break; 2496229997Sken } 2497229997Sken mtx_unlock(&softc->lock); 2498229997Sken if (be_lun == NULL) { 2499229997Sken snprintf(req->error_str, sizeof(req->error_str), 2500272911Smav "LUN %u is not managed by the block backend", 2501272911Smav params->lun_id); 2502229997Sken goto bailout_error; 2503229997Sken } 2504287670Smav cbe_lun = &be_lun->cbe_lun; 2505229997Sken 2506287670Smav retval = ctl_disable_lun(cbe_lun); 2507229997Sken if (retval != 0) { 2508229997Sken snprintf(req->error_str, sizeof(req->error_str), 2509272911Smav "error %d returned from ctl_disable_lun() for " 2510272911Smav "LUN %d", retval, params->lun_id); 2511229997Sken goto bailout_error; 2512287670Smav } 2513229997Sken 2514287670Smav if (be_lun->vn != NULL) { 2515287670Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2516287670Smav ctl_lun_offline(cbe_lun); 2517287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2518287670Smav ctl_be_block_close(be_lun); 2519229997Sken } 2520229997Sken 2521287670Smav retval = ctl_invalidate_lun(cbe_lun); 2522229997Sken if (retval != 0) { 2523229997Sken snprintf(req->error_str, sizeof(req->error_str), 2524272911Smav "error %d returned from ctl_invalidate_lun() for " 2525272911Smav "LUN %d", retval, params->lun_id); 2526229997Sken goto bailout_error; 2527229997Sken } 2528229997Sken 2529229997Sken mtx_lock(&softc->lock); 2530229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2531229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2532229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2533229997Sken if (retval == EINTR) 2534229997Sken break; 2535229997Sken } 2536229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2537229997Sken 2538229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2539229997Sken snprintf(req->error_str, sizeof(req->error_str), 2540272911Smav "interrupted waiting for LUN to be freed"); 2541229997Sken mtx_unlock(&softc->lock); 2542229997Sken goto bailout_error; 2543229997Sken } 2544229997Sken 2545229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2546229997Sken 2547229997Sken softc->num_luns--; 2548229997Sken mtx_unlock(&softc->lock); 2549229997Sken 2550287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2551229997Sken taskqueue_free(be_lun->io_taskqueue); 2552229997Sken 2553229997Sken if (be_lun->disk_stats != NULL) 2554229997Sken devstat_remove_entry(be_lun->disk_stats); 2555229997Sken 2556229997Sken uma_zdestroy(be_lun->lun_zone); 2557229997Sken 2558287670Smav ctl_free_opts(&cbe_lun->options); 2559229997Sken free(be_lun->dev_path, M_CTLBLK); 2560267877Smav mtx_destroy(&be_lun->queue_lock); 2561267877Smav mtx_destroy(&be_lun->io_lock); 2562229997Sken free(be_lun, M_CTLBLK); 2563229997Sken 2564229997Sken req->status = CTL_LUN_OK; 2565229997Sken return (0); 2566229997Sken 2567229997Skenbailout_error: 2568229997Sken req->status = CTL_LUN_ERROR; 2569229997Sken return (0); 2570229997Sken} 2571229997Sken 2572232604Straszstatic int 2573232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2574232604Strasz{ 2575232604Strasz struct ctl_lun_modify_params *params; 2576232604Strasz struct ctl_be_block_lun *be_lun; 2577287500Smav struct ctl_be_lun *cbe_lun; 2578287621Smav char *value; 2579271794Smav uint64_t oldsize; 2580287621Smav int error, wasprim; 2581232604Strasz 2582232604Strasz params = &req->reqdata.modify; 2583232604Strasz 2584232604Strasz mtx_lock(&softc->lock); 2585232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2586287499Smav if (be_lun->cbe_lun.lun_id == params->lun_id) 2587232604Strasz break; 2588232604Strasz } 2589232604Strasz mtx_unlock(&softc->lock); 2590232604Strasz if (be_lun == NULL) { 2591232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2592272911Smav "LUN %u is not managed by the block backend", 2593272911Smav params->lun_id); 2594232604Strasz goto bailout_error; 2595232604Strasz } 2596287500Smav cbe_lun = &be_lun->cbe_lun; 2597232604Strasz 2598287500Smav if (params->lun_size_bytes != 0) 2599287500Smav be_lun->params.lun_size_bytes = params->lun_size_bytes; 2600287500Smav ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2601232604Strasz 2602287621Smav wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2603287621Smav value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2604287621Smav if (value != NULL) { 2605287621Smav if (strcmp(value, "primary") == 0) 2606287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2607287621Smav else 2608287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2609287621Smav } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2610287621Smav cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2611232604Strasz else 2612287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2613287621Smav if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 2614287621Smav if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 2615287621Smav ctl_lun_primary(cbe_lun); 2616287621Smav else 2617287621Smav ctl_lun_secondary(cbe_lun); 2618287621Smav } 2619232604Strasz 2620287621Smav oldsize = be_lun->size_blocks; 2621287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2622287621Smav control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2623287621Smav if (be_lun->vn == NULL) 2624287621Smav error = ctl_be_block_open(softc, be_lun, req); 2625287621Smav else if (vn_isdisk(be_lun->vn, &error)) 2626288104Smav error = ctl_be_block_open_dev(be_lun, req); 2627287621Smav else if (be_lun->vn->v_type == VREG) 2628288104Smav error = ctl_be_block_open_file(be_lun, req); 2629287621Smav else 2630287621Smav error = EINVAL; 2631287621Smav if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) && 2632287621Smav be_lun->vn != NULL) { 2633287621Smav cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE; 2634287621Smav ctl_lun_online(cbe_lun); 2635287621Smav } 2636287621Smav } else { 2637287621Smav if (be_lun->vn != NULL) { 2638287621Smav cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2639287621Smav ctl_lun_offline(cbe_lun); 2640287670Smav taskqueue_drain_all(be_lun->io_taskqueue); 2641287621Smav error = ctl_be_block_close(be_lun); 2642287621Smav } else 2643287621Smav error = 0; 2644287621Smav } 2645287499Smav if (be_lun->size_blocks != oldsize) 2646287500Smav ctl_lun_capacity_changed(cbe_lun); 2647232604Strasz 2648232604Strasz /* Tell the user the exact size we ended up using */ 2649232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2650232604Strasz 2651272911Smav req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2652232604Strasz return (0); 2653232604Strasz 2654232604Straszbailout_error: 2655232604Strasz req->status = CTL_LUN_ERROR; 2656232604Strasz return (0); 2657232604Strasz} 2658232604Strasz 2659229997Skenstatic void 2660229997Skenctl_be_block_lun_shutdown(void *be_lun) 2661229997Sken{ 2662229997Sken struct ctl_be_block_lun *lun; 2663229997Sken struct ctl_be_block_softc *softc; 2664229997Sken 2665229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2666229997Sken softc = lun->softc; 2667229997Sken 2668229997Sken mtx_lock(&softc->lock); 2669229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2670229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2671229997Sken wakeup(lun); 2672229997Sken mtx_unlock(&softc->lock); 2673229997Sken} 2674229997Sken 2675229997Skenstatic void 2676229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2677229997Sken{ 2678229997Sken struct ctl_be_block_lun *lun; 2679229997Sken struct ctl_be_block_softc *softc; 2680229997Sken 2681229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2682229997Sken softc = lun->softc; 2683229997Sken 2684229997Sken if (status == CTL_LUN_CONFIG_OK) { 2685229997Sken mtx_lock(&softc->lock); 2686229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2687229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2688229997Sken wakeup(lun); 2689229997Sken mtx_unlock(&softc->lock); 2690229997Sken 2691229997Sken /* 2692229997Sken * We successfully added the LUN, attempt to enable it. 2693229997Sken */ 2694287499Smav if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2695229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2696287499Smav if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2697229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2698229997Sken __func__); 2699229997Sken } 2700229997Sken } 2701229997Sken 2702229997Sken return; 2703229997Sken } 2704229997Sken 2705229997Sken 2706229997Sken mtx_lock(&softc->lock); 2707229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2708229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2709229997Sken wakeup(lun); 2710229997Sken mtx_unlock(&softc->lock); 2711229997Sken} 2712229997Sken 2713229997Sken 2714229997Skenstatic int 2715229997Skenctl_be_block_config_write(union ctl_io *io) 2716229997Sken{ 2717229997Sken struct ctl_be_block_lun *be_lun; 2718287499Smav struct ctl_be_lun *cbe_lun; 2719229997Sken int retval; 2720229997Sken 2721229997Sken DPRINTF("entered\n"); 2722229997Sken 2723287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2724229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2725287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2726229997Sken 2727288220Smav retval = 0; 2728229997Sken switch (io->scsiio.cdb[0]) { 2729229997Sken case SYNCHRONIZE_CACHE: 2730229997Sken case SYNCHRONIZE_CACHE_16: 2731264274Smav case WRITE_SAME_10: 2732264274Smav case WRITE_SAME_16: 2733264274Smav case UNMAP: 2734229997Sken /* 2735229997Sken * The upper level CTL code will filter out any CDBs with 2736229997Sken * the immediate bit set and return the proper error. 2737229997Sken * 2738229997Sken * We don't really need to worry about what LBA range the 2739229997Sken * user asked to be synced out. When they issue a sync 2740229997Sken * cache command, we'll sync out the whole thing. 2741229997Sken */ 2742267877Smav mtx_lock(&be_lun->queue_lock); 2743229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2744229997Sken links); 2745267877Smav mtx_unlock(&be_lun->queue_lock); 2746229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2747229997Sken break; 2748229997Sken case START_STOP_UNIT: { 2749229997Sken struct scsi_start_stop_unit *cdb; 2750229997Sken 2751229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2752229997Sken 2753229997Sken if (cdb->how & SSS_START) 2754287499Smav retval = ctl_start_lun(cbe_lun); 2755288259Smav else 2756287499Smav retval = ctl_stop_lun(cbe_lun); 2757229997Sken 2758229997Sken /* 2759229997Sken * In general, the above routines should not fail. They 2760229997Sken * just set state for the LUN. So we've got something 2761229997Sken * pretty wrong here if we can't start or stop the LUN. 2762229997Sken */ 2763229997Sken if (retval != 0) { 2764229997Sken ctl_set_internal_failure(&io->scsiio, 2765229997Sken /*sks_valid*/ 1, 2766229997Sken /*retry_count*/ 0xf051); 2767229997Sken retval = CTL_RETVAL_COMPLETE; 2768229997Sken } else { 2769229997Sken ctl_set_success(&io->scsiio); 2770229997Sken } 2771229997Sken ctl_config_write_done(io); 2772229997Sken break; 2773229997Sken } 2774288310Smav case PREVENT_ALLOW: 2775288310Smav ctl_set_success(&io->scsiio); 2776288310Smav ctl_config_write_done(io); 2777288310Smav break; 2778229997Sken default: 2779229997Sken ctl_set_invalid_opcode(&io->scsiio); 2780229997Sken ctl_config_write_done(io); 2781229997Sken retval = CTL_RETVAL_COMPLETE; 2782229997Sken break; 2783229997Sken } 2784229997Sken 2785229997Sken return (retval); 2786229997Sken} 2787229997Sken 2788229997Skenstatic int 2789229997Skenctl_be_block_config_read(union ctl_io *io) 2790229997Sken{ 2791275474Smav struct ctl_be_block_lun *be_lun; 2792287499Smav struct ctl_be_lun *cbe_lun; 2793275474Smav int retval = 0; 2794275474Smav 2795275474Smav DPRINTF("entered\n"); 2796275474Smav 2797287499Smav cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2798275474Smav CTL_PRIV_BACKEND_LUN].ptr; 2799287499Smav be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2800275474Smav 2801275474Smav switch (io->scsiio.cdb[0]) { 2802275474Smav case SERVICE_ACTION_IN: 2803275474Smav if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2804275474Smav mtx_lock(&be_lun->queue_lock); 2805275474Smav STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2806275474Smav &io->io_hdr, links); 2807275474Smav mtx_unlock(&be_lun->queue_lock); 2808275474Smav taskqueue_enqueue(be_lun->io_taskqueue, 2809275474Smav &be_lun->io_task); 2810275474Smav retval = CTL_RETVAL_QUEUED; 2811275474Smav break; 2812275474Smav } 2813275474Smav ctl_set_invalid_field(&io->scsiio, 2814275474Smav /*sks_valid*/ 1, 2815275474Smav /*command*/ 1, 2816275474Smav /*field*/ 1, 2817275474Smav /*bit_valid*/ 1, 2818275474Smav /*bit*/ 4); 2819275474Smav ctl_config_read_done(io); 2820275474Smav retval = CTL_RETVAL_COMPLETE; 2821275474Smav break; 2822275474Smav default: 2823275474Smav ctl_set_invalid_opcode(&io->scsiio); 2824275474Smav ctl_config_read_done(io); 2825275474Smav retval = CTL_RETVAL_COMPLETE; 2826275474Smav break; 2827275474Smav } 2828275474Smav 2829275474Smav return (retval); 2830229997Sken} 2831229997Sken 2832229997Skenstatic int 2833229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2834229997Sken{ 2835229997Sken struct ctl_be_block_lun *lun; 2836229997Sken int retval; 2837229997Sken 2838229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2839229997Sken 2840268283Smav retval = sbuf_printf(sb, "\t<num_threads>"); 2841229997Sken if (retval != 0) 2842229997Sken goto bailout; 2843229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2844229997Sken if (retval != 0) 2845229997Sken goto bailout; 2846268283Smav retval = sbuf_printf(sb, "</num_threads>\n"); 2847229997Sken 2848229997Skenbailout: 2849229997Sken return (retval); 2850229997Sken} 2851229997Sken 2852274154Smavstatic uint64_t 2853274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname) 2854274154Smav{ 2855274154Smav struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2856274154Smav 2857274154Smav if (lun->getattr == NULL) 2858274154Smav return (UINT64_MAX); 2859274154Smav return (lun->getattr(lun, attrname)); 2860274154Smav} 2861274154Smav 2862229997Skenint 2863229997Skenctl_be_block_init(void) 2864229997Sken{ 2865229997Sken struct ctl_be_block_softc *softc; 2866229997Sken int retval; 2867229997Sken 2868229997Sken softc = &backend_block_softc; 2869229997Sken retval = 0; 2870229997Sken 2871267877Smav mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2872264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2873264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2874229997Sken STAILQ_INIT(&softc->lun_list); 2875229997Sken 2876229997Sken return (retval); 2877229997Sken} 2878