ctl_backend_block.c revision 264020
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 264020 2014-04-01 21:13:05Z trasz $"); 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> 52229997Sken#include <sys/lock.h> 53229997Sken#include <sys/mutex.h> 54229997Sken#include <sys/condvar.h> 55229997Sken#include <sys/malloc.h> 56229997Sken#include <sys/conf.h> 57229997Sken#include <sys/ioccom.h> 58229997Sken#include <sys/queue.h> 59229997Sken#include <sys/sbuf.h> 60229997Sken#include <sys/endian.h> 61229997Sken#include <sys/uio.h> 62229997Sken#include <sys/buf.h> 63229997Sken#include <sys/taskqueue.h> 64229997Sken#include <sys/vnode.h> 65229997Sken#include <sys/namei.h> 66229997Sken#include <sys/mount.h> 67229997Sken#include <sys/disk.h> 68229997Sken#include <sys/fcntl.h> 69229997Sken#include <sys/filedesc.h> 70229997Sken#include <sys/proc.h> 71229997Sken#include <sys/pcpu.h> 72229997Sken#include <sys/module.h> 73229997Sken#include <sys/sdt.h> 74229997Sken#include <sys/devicestat.h> 75229997Sken#include <sys/sysctl.h> 76229997Sken 77229997Sken#include <geom/geom.h> 78229997Sken 79229997Sken#include <cam/cam.h> 80229997Sken#include <cam/scsi/scsi_all.h> 81229997Sken#include <cam/scsi/scsi_da.h> 82229997Sken#include <cam/ctl/ctl_io.h> 83229997Sken#include <cam/ctl/ctl.h> 84229997Sken#include <cam/ctl/ctl_backend.h> 85229997Sken#include <cam/ctl/ctl_frontend_internal.h> 86229997Sken#include <cam/ctl/ctl_ioctl.h> 87229997Sken#include <cam/ctl/ctl_scsi_all.h> 88229997Sken#include <cam/ctl/ctl_error.h> 89229997Sken 90229997Sken/* 91229997Sken * The idea here is that we'll allocate enough S/G space to hold a 16MB 92229997Sken * I/O. If we get an I/O larger than that, we'll reject it. 93229997Sken */ 94229997Sken#define CTLBLK_MAX_IO_SIZE (16 * 1024 * 1024) 95229997Sken#define CTLBLK_MAX_SEGS (CTLBLK_MAX_IO_SIZE / MAXPHYS) + 1 96229997Sken 97229997Sken#ifdef CTLBLK_DEBUG 98229997Sken#define DPRINTF(fmt, args...) \ 99229997Sken printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 100229997Sken#else 101229997Sken#define DPRINTF(fmt, args...) do {} while(0) 102229997Sken#endif 103229997Sken 104229997SkenSDT_PROVIDER_DEFINE(cbb); 105229997Sken 106229997Skentypedef enum { 107229997Sken CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 108229997Sken CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 109229997Sken CTL_BE_BLOCK_LUN_WAITING = 0x04, 110229997Sken CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08 111229997Sken} ctl_be_block_lun_flags; 112229997Sken 113229997Skentypedef enum { 114229997Sken CTL_BE_BLOCK_NONE, 115229997Sken CTL_BE_BLOCK_DEV, 116229997Sken CTL_BE_BLOCK_FILE 117229997Sken} ctl_be_block_type; 118229997Sken 119229997Skenstruct ctl_be_block_devdata { 120229997Sken struct cdev *cdev; 121229997Sken struct cdevsw *csw; 122229997Sken int dev_ref; 123229997Sken}; 124229997Sken 125229997Skenstruct ctl_be_block_filedata { 126229997Sken struct ucred *cred; 127229997Sken}; 128229997Sken 129229997Skenunion ctl_be_block_bedata { 130229997Sken struct ctl_be_block_devdata dev; 131229997Sken struct ctl_be_block_filedata file; 132229997Sken}; 133229997Sken 134229997Skenstruct ctl_be_block_io; 135229997Skenstruct ctl_be_block_lun; 136229997Sken 137229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 138229997Sken struct ctl_be_block_io *beio); 139229997Sken 140229997Sken/* 141229997Sken * Backend LUN structure. There is a 1:1 mapping between a block device 142229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN. 143229997Sken */ 144229997Skenstruct ctl_be_block_lun { 145229997Sken struct ctl_block_disk *disk; 146229997Sken char lunname[32]; 147229997Sken char *dev_path; 148229997Sken ctl_be_block_type dev_type; 149229997Sken struct vnode *vn; 150229997Sken union ctl_be_block_bedata backend; 151229997Sken cbb_dispatch_t dispatch; 152229997Sken cbb_dispatch_t lun_flush; 153229997Sken struct mtx lock; 154229997Sken uma_zone_t lun_zone; 155229997Sken uint64_t size_blocks; 156229997Sken uint64_t size_bytes; 157229997Sken uint32_t blocksize; 158229997Sken int blocksize_shift; 159229997Sken struct ctl_be_block_softc *softc; 160229997Sken struct devstat *disk_stats; 161229997Sken ctl_be_block_lun_flags flags; 162229997Sken STAILQ_ENTRY(ctl_be_block_lun) links; 163229997Sken struct ctl_be_lun ctl_be_lun; 164229997Sken struct taskqueue *io_taskqueue; 165229997Sken struct task io_task; 166229997Sken int num_threads; 167229997Sken STAILQ_HEAD(, ctl_io_hdr) input_queue; 168229997Sken STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 169229997Sken STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 170229997Sken}; 171229997Sken 172229997Sken/* 173229997Sken * Overall softc structure for the block backend module. 174229997Sken */ 175229997Skenstruct ctl_be_block_softc { 176229997Sken struct mtx lock; 177229997Sken int num_disks; 178229997Sken STAILQ_HEAD(, ctl_block_disk) disk_list; 179229997Sken int num_luns; 180229997Sken STAILQ_HEAD(, ctl_be_block_lun) lun_list; 181229997Sken}; 182229997Sken 183229997Skenstatic struct ctl_be_block_softc backend_block_softc; 184229997Sken 185229997Sken/* 186229997Sken * Per-I/O information. 187229997Sken */ 188229997Skenstruct ctl_be_block_io { 189229997Sken union ctl_io *io; 190229997Sken struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 191229997Sken struct iovec xiovecs[CTLBLK_MAX_SEGS]; 192229997Sken int bio_cmd; 193229997Sken int bio_flags; 194229997Sken int num_segs; 195229997Sken int num_bios_sent; 196229997Sken int num_bios_done; 197229997Sken int send_complete; 198229997Sken int num_errors; 199229997Sken struct bintime ds_t0; 200229997Sken devstat_tag_type ds_tag_type; 201229997Sken devstat_trans_flags ds_trans_type; 202229997Sken uint64_t io_len; 203229997Sken uint64_t io_offset; 204229997Sken struct ctl_be_block_softc *softc; 205229997Sken struct ctl_be_block_lun *lun; 206229997Sken}; 207229997Sken 208229997Skenstatic int cbb_num_threads = 14; 209229997SkenTUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads); 210229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 211229997Sken "CAM Target Layer Block Backend"); 212229997SkenSYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW, 213229997Sken &cbb_num_threads, 0, "Number of threads per backing file"); 214229997Sken 215229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 216229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio); 217229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio); 218229997Skenstatic int ctl_be_block_move_done(union ctl_io *io); 219229997Skenstatic void ctl_be_block_biodone(struct bio *bio); 220229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 221229997Sken struct ctl_be_block_io *beio); 222229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 223229997Sken struct ctl_be_block_io *beio); 224229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 225229997Sken struct ctl_be_block_io *beio); 226229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 227229997Sken struct ctl_be_block_io *beio); 228229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 229229997Sken union ctl_io *io); 230229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 231229997Sken union ctl_io *io); 232229997Skenstatic void ctl_be_block_worker(void *context, int pending); 233229997Skenstatic int ctl_be_block_submit(union ctl_io *io); 234229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 235229997Sken int flag, struct thread *td); 236229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 237229997Sken struct ctl_lun_req *req); 238229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 239229997Sken struct ctl_lun_req *req); 240229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 241229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc, 242229997Sken struct ctl_be_block_lun *be_lun, 243229997Sken struct ctl_lun_req *req); 244229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc, 245229997Sken struct ctl_lun_req *req); 246229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc, 247229997Sken struct ctl_lun_req *req); 248232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 249232604Strasz struct ctl_lun_req *req); 250232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 251232604Strasz struct ctl_lun_req *req); 252232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc, 253232604Strasz struct ctl_lun_req *req); 254229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun); 255229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun, 256229997Sken ctl_lun_config_status status); 257229997Skenstatic int ctl_be_block_config_write(union ctl_io *io); 258229997Skenstatic int ctl_be_block_config_read(union ctl_io *io); 259229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 260229997Skenint ctl_be_block_init(void); 261229997Sken 262229997Skenstatic struct ctl_backend_driver ctl_be_block_driver = 263229997Sken{ 264230334Sken .name = "block", 265230334Sken .flags = CTL_BE_FLAG_HAS_CONFIG, 266230334Sken .init = ctl_be_block_init, 267230334Sken .data_submit = ctl_be_block_submit, 268230334Sken .data_move_done = ctl_be_block_move_done, 269230334Sken .config_read = ctl_be_block_config_read, 270230334Sken .config_write = ctl_be_block_config_write, 271230334Sken .ioctl = ctl_be_block_ioctl, 272230334Sken .lun_info = ctl_be_block_lun_info 273229997Sken}; 274229997Sken 275229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 276229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 277229997Sken 278264020Straszstatic uma_zone_t beio_zone; 279264020Strasz 280229997Skenstatic struct ctl_be_block_io * 281229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc) 282229997Sken{ 283229997Sken struct ctl_be_block_io *beio; 284229997Sken 285264020Strasz beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 286264020Strasz beio->softc = softc; 287229997Sken return (beio); 288229997Sken} 289229997Sken 290229997Skenstatic void 291229997Skenctl_free_beio(struct ctl_be_block_io *beio) 292229997Sken{ 293229997Sken int duplicate_free; 294229997Sken int i; 295229997Sken 296229997Sken duplicate_free = 0; 297229997Sken 298229997Sken for (i = 0; i < beio->num_segs; i++) { 299229997Sken if (beio->sg_segs[i].addr == NULL) 300229997Sken duplicate_free++; 301229997Sken 302229997Sken uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 303229997Sken beio->sg_segs[i].addr = NULL; 304229997Sken } 305229997Sken 306229997Sken if (duplicate_free > 0) { 307229997Sken printf("%s: %d duplicate frees out of %d segments\n", __func__, 308229997Sken duplicate_free, beio->num_segs); 309229997Sken } 310229997Sken 311264020Strasz uma_zfree(beio_zone, beio); 312229997Sken} 313229997Sken 314229997Skenstatic void 315229997Skenctl_complete_beio(struct ctl_be_block_io *beio) 316229997Sken{ 317229997Sken union ctl_io *io; 318229997Sken int io_len; 319229997Sken 320229997Sken io = beio->io; 321229997Sken 322229997Sken if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 323229997Sken io_len = beio->io_len; 324229997Sken else 325229997Sken io_len = 0; 326229997Sken 327229997Sken devstat_end_transaction(beio->lun->disk_stats, 328229997Sken /*bytes*/ io_len, 329229997Sken beio->ds_tag_type, 330229997Sken beio->ds_trans_type, 331229997Sken /*now*/ NULL, 332229997Sken /*then*/&beio->ds_t0); 333229997Sken 334229997Sken ctl_free_beio(beio); 335229997Sken ctl_done(io); 336229997Sken} 337229997Sken 338229997Skenstatic int 339229997Skenctl_be_block_move_done(union ctl_io *io) 340229997Sken{ 341229997Sken struct ctl_be_block_io *beio; 342229997Sken struct ctl_be_block_lun *be_lun; 343229997Sken#ifdef CTL_TIME_IO 344229997Sken struct bintime cur_bt; 345229997Sken#endif 346229997Sken 347229997Sken beio = (struct ctl_be_block_io *) 348229997Sken io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr; 349229997Sken 350229997Sken be_lun = beio->lun; 351229997Sken 352229997Sken DPRINTF("entered\n"); 353229997Sken 354229997Sken#ifdef CTL_TIME_IO 355229997Sken getbintime(&cur_bt); 356229997Sken bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 357229997Sken bintime_add(&io->io_hdr.dma_bt, &cur_bt); 358229997Sken io->io_hdr.num_dmas++; 359229997Sken#endif 360229997Sken 361229997Sken /* 362229997Sken * We set status at this point for read commands, and write 363229997Sken * commands with errors. 364229997Sken */ 365229997Sken if ((beio->bio_cmd == BIO_READ) 366229997Sken && (io->io_hdr.port_status == 0) 367229997Sken && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 368229997Sken && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 369229997Sken ctl_set_success(&io->scsiio); 370229997Sken else if ((io->io_hdr.port_status != 0) 371229997Sken && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 372229997Sken && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 373229997Sken /* 374229997Sken * For hardware error sense keys, the sense key 375229997Sken * specific value is defined to be a retry count, 376229997Sken * but we use it to pass back an internal FETD 377229997Sken * error code. XXX KDM Hopefully the FETD is only 378229997Sken * using 16 bits for an error code, since that's 379229997Sken * all the space we have in the sks field. 380229997Sken */ 381229997Sken ctl_set_internal_failure(&io->scsiio, 382229997Sken /*sks_valid*/ 1, 383229997Sken /*retry_count*/ 384229997Sken io->io_hdr.port_status); 385229997Sken } 386229997Sken 387229997Sken /* 388229997Sken * If this is a read, or a write with errors, it is done. 389229997Sken */ 390229997Sken if ((beio->bio_cmd == BIO_READ) 391229997Sken || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 392229997Sken || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 393229997Sken ctl_complete_beio(beio); 394229997Sken return (0); 395229997Sken } 396229997Sken 397229997Sken /* 398229997Sken * At this point, we have a write and the DMA completed 399229997Sken * successfully. We now have to queue it to the task queue to 400229997Sken * execute the backend I/O. That is because we do blocking 401229997Sken * memory allocations, and in the file backing case, blocking I/O. 402229997Sken * This move done routine is generally called in the SIM's 403229997Sken * interrupt context, and therefore we cannot block. 404229997Sken */ 405229997Sken mtx_lock(&be_lun->lock); 406229997Sken /* 407229997Sken * XXX KDM make sure that links is okay to use at this point. 408229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 409229997Sken * or deal with resource allocation here. 410229997Sken */ 411229997Sken STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 412229997Sken mtx_unlock(&be_lun->lock); 413229997Sken 414229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 415229997Sken 416229997Sken return (0); 417229997Sken} 418229997Sken 419229997Skenstatic void 420229997Skenctl_be_block_biodone(struct bio *bio) 421229997Sken{ 422229997Sken struct ctl_be_block_io *beio; 423229997Sken struct ctl_be_block_lun *be_lun; 424229997Sken union ctl_io *io; 425261538Smav int error; 426229997Sken 427229997Sken beio = bio->bio_caller1; 428229997Sken be_lun = beio->lun; 429229997Sken io = beio->io; 430229997Sken 431229997Sken DPRINTF("entered\n"); 432229997Sken 433261538Smav error = bio->bio_error; 434229997Sken mtx_lock(&be_lun->lock); 435261538Smav if (error != 0) 436229997Sken beio->num_errors++; 437229997Sken 438229997Sken beio->num_bios_done++; 439229997Sken 440229997Sken /* 441229997Sken * XXX KDM will this cause WITNESS to complain? Holding a lock 442229997Sken * during the free might cause it to complain. 443229997Sken */ 444229997Sken g_destroy_bio(bio); 445229997Sken 446229997Sken /* 447229997Sken * If the send complete bit isn't set, or we aren't the last I/O to 448229997Sken * complete, then we're done. 449229997Sken */ 450229997Sken if ((beio->send_complete == 0) 451229997Sken || (beio->num_bios_done < beio->num_bios_sent)) { 452229997Sken mtx_unlock(&be_lun->lock); 453229997Sken return; 454229997Sken } 455229997Sken 456229997Sken /* 457229997Sken * At this point, we've verified that we are the last I/O to 458229997Sken * complete, so it's safe to drop the lock. 459229997Sken */ 460229997Sken mtx_unlock(&be_lun->lock); 461229997Sken 462229997Sken /* 463229997Sken * If there are any errors from the backing device, we fail the 464229997Sken * entire I/O with a medium error. 465229997Sken */ 466229997Sken if (beio->num_errors > 0) { 467261538Smav if (error == EOPNOTSUPP) { 468261538Smav ctl_set_invalid_opcode(&io->scsiio); 469261538Smav } else if (beio->bio_cmd == BIO_FLUSH) { 470229997Sken /* XXX KDM is there is a better error here? */ 471229997Sken ctl_set_internal_failure(&io->scsiio, 472229997Sken /*sks_valid*/ 1, 473229997Sken /*retry_count*/ 0xbad2); 474229997Sken } else 475229997Sken ctl_set_medium_error(&io->scsiio); 476229997Sken ctl_complete_beio(beio); 477229997Sken return; 478229997Sken } 479229997Sken 480229997Sken /* 481229997Sken * If this is a write or a flush, we're all done. 482229997Sken * If this is a read, we can now send the data to the user. 483229997Sken */ 484229997Sken if ((beio->bio_cmd == BIO_WRITE) 485229997Sken || (beio->bio_cmd == BIO_FLUSH)) { 486229997Sken ctl_set_success(&io->scsiio); 487229997Sken ctl_complete_beio(beio); 488229997Sken } else { 489229997Sken io->scsiio.be_move_done = ctl_be_block_move_done; 490229997Sken io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 491229997Sken io->scsiio.kern_data_len = beio->io_len; 492229997Sken io->scsiio.kern_total_len = beio->io_len; 493229997Sken io->scsiio.kern_rel_offset = 0; 494229997Sken io->scsiio.kern_data_resid = 0; 495229997Sken io->scsiio.kern_sg_entries = beio->num_segs; 496229997Sken io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 497229997Sken#ifdef CTL_TIME_IO 498229997Sken getbintime(&io->io_hdr.dma_start_bt); 499229997Sken#endif 500229997Sken ctl_datamove(io); 501229997Sken } 502229997Sken} 503229997Sken 504229997Skenstatic void 505229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 506229997Sken struct ctl_be_block_io *beio) 507229997Sken{ 508229997Sken union ctl_io *io; 509229997Sken struct mount *mountpoint; 510241896Skib int error, lock_flags; 511229997Sken 512229997Sken DPRINTF("entered\n"); 513229997Sken 514229997Sken io = beio->io; 515229997Sken 516229997Sken (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 517229997Sken 518229997Sken if (MNT_SHARED_WRITES(mountpoint) 519229997Sken || ((mountpoint == NULL) 520229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 521229997Sken lock_flags = LK_SHARED; 522229997Sken else 523229997Sken lock_flags = LK_EXCLUSIVE; 524229997Sken 525229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 526229997Sken 527229997Sken binuptime(&beio->ds_t0); 528229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 529229997Sken 530229997Sken error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread); 531229997Sken VOP_UNLOCK(be_lun->vn, 0); 532229997Sken 533229997Sken vn_finished_write(mountpoint); 534229997Sken 535229997Sken if (error == 0) 536229997Sken ctl_set_success(&io->scsiio); 537229997Sken else { 538229997Sken /* XXX KDM is there is a better error here? */ 539229997Sken ctl_set_internal_failure(&io->scsiio, 540229997Sken /*sks_valid*/ 1, 541229997Sken /*retry_count*/ 0xbad1); 542229997Sken } 543229997Sken 544229997Sken ctl_complete_beio(beio); 545229997Sken} 546229997Sken 547258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 548258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 549258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 550258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 551229997Sken 552229997Skenstatic void 553229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 554229997Sken struct ctl_be_block_io *beio) 555229997Sken{ 556229997Sken struct ctl_be_block_filedata *file_data; 557229997Sken union ctl_io *io; 558229997Sken struct uio xuio; 559229997Sken struct iovec *xiovec; 560241896Skib int flags; 561229997Sken int error, i; 562229997Sken 563229997Sken DPRINTF("entered\n"); 564229997Sken 565229997Sken file_data = &be_lun->backend.file; 566229997Sken io = beio->io; 567229997Sken flags = beio->bio_flags; 568229997Sken 569229997Sken if (beio->bio_cmd == BIO_READ) { 570229997Sken SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 571229997Sken } else { 572229997Sken SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 573229997Sken } 574229997Sken 575229997Sken bzero(&xuio, sizeof(xuio)); 576229997Sken if (beio->bio_cmd == BIO_READ) 577229997Sken xuio.uio_rw = UIO_READ; 578229997Sken else 579229997Sken xuio.uio_rw = UIO_WRITE; 580229997Sken 581229997Sken xuio.uio_offset = beio->io_offset; 582229997Sken xuio.uio_resid = beio->io_len; 583229997Sken xuio.uio_segflg = UIO_SYSSPACE; 584229997Sken xuio.uio_iov = beio->xiovecs; 585229997Sken xuio.uio_iovcnt = beio->num_segs; 586229997Sken xuio.uio_td = curthread; 587229997Sken 588229997Sken for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 589229997Sken xiovec->iov_base = beio->sg_segs[i].addr; 590229997Sken xiovec->iov_len = beio->sg_segs[i].len; 591229997Sken } 592229997Sken 593229997Sken if (beio->bio_cmd == BIO_READ) { 594229997Sken vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 595229997Sken 596229997Sken binuptime(&beio->ds_t0); 597229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 598229997Sken 599229997Sken /* 600229997Sken * UFS pays attention to IO_DIRECT for reads. If the 601229997Sken * DIRECTIO option is configured into the kernel, it calls 602229997Sken * ffs_rawread(). But that only works for single-segment 603229997Sken * uios with user space addresses. In our case, with a 604229997Sken * kernel uio, it still reads into the buffer cache, but it 605229997Sken * will just try to release the buffer from the cache later 606229997Sken * on in ffs_read(). 607229997Sken * 608229997Sken * ZFS does not pay attention to IO_DIRECT for reads. 609229997Sken * 610229997Sken * UFS does not pay attention to IO_SYNC for reads. 611229997Sken * 612229997Sken * ZFS pays attention to IO_SYNC (which translates into the 613229997Sken * Solaris define FRSYNC for zfs_read()) for reads. It 614229997Sken * attempts to sync the file before reading. 615229997Sken * 616229997Sken * So, to attempt to provide some barrier semantics in the 617229997Sken * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC. 618229997Sken */ 619229997Sken error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 620229997Sken (IO_DIRECT|IO_SYNC) : 0, file_data->cred); 621229997Sken 622229997Sken VOP_UNLOCK(be_lun->vn, 0); 623229997Sken } else { 624229997Sken struct mount *mountpoint; 625229997Sken int lock_flags; 626229997Sken 627229997Sken (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 628229997Sken 629229997Sken if (MNT_SHARED_WRITES(mountpoint) 630229997Sken || ((mountpoint == NULL) 631229997Sken && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 632229997Sken lock_flags = LK_SHARED; 633229997Sken else 634229997Sken lock_flags = LK_EXCLUSIVE; 635229997Sken 636229997Sken vn_lock(be_lun->vn, lock_flags | LK_RETRY); 637229997Sken 638229997Sken binuptime(&beio->ds_t0); 639229997Sken devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 640229997Sken 641229997Sken /* 642229997Sken * UFS pays attention to IO_DIRECT for writes. The write 643229997Sken * is done asynchronously. (Normally the write would just 644229997Sken * get put into cache. 645229997Sken * 646229997Sken * UFS pays attention to IO_SYNC for writes. It will 647229997Sken * attempt to write the buffer out synchronously if that 648229997Sken * flag is set. 649229997Sken * 650229997Sken * ZFS does not pay attention to IO_DIRECT for writes. 651229997Sken * 652229997Sken * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 653229997Sken * for writes. It will flush the transaction from the 654229997Sken * cache before returning. 655229997Sken * 656229997Sken * So if we've got the BIO_ORDERED flag set, we want 657229997Sken * IO_SYNC in either the UFS or ZFS case. 658229997Sken */ 659229997Sken error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ? 660229997Sken IO_SYNC : 0, file_data->cred); 661229997Sken VOP_UNLOCK(be_lun->vn, 0); 662229997Sken 663229997Sken vn_finished_write(mountpoint); 664229997Sken } 665229997Sken 666229997Sken /* 667229997Sken * If we got an error, set the sense data to "MEDIUM ERROR" and 668229997Sken * return the I/O to the user. 669229997Sken */ 670229997Sken if (error != 0) { 671229997Sken char path_str[32]; 672229997Sken 673229997Sken ctl_scsi_path_string(io, path_str, sizeof(path_str)); 674229997Sken /* 675229997Sken * XXX KDM ZFS returns ENOSPC when the underlying 676229997Sken * filesystem fills up. What kind of SCSI error should we 677229997Sken * return for that? 678229997Sken */ 679229997Sken printf("%s%s command returned errno %d\n", path_str, 680229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 681229997Sken ctl_set_medium_error(&io->scsiio); 682229997Sken ctl_complete_beio(beio); 683229997Sken return; 684229997Sken } 685229997Sken 686229997Sken /* 687229997Sken * If this is a write, we're all done. 688229997Sken * If this is a read, we can now send the data to the user. 689229997Sken */ 690229997Sken if (beio->bio_cmd == BIO_WRITE) { 691229997Sken ctl_set_success(&io->scsiio); 692229997Sken SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 693229997Sken ctl_complete_beio(beio); 694229997Sken } else { 695229997Sken SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 696229997Sken io->scsiio.be_move_done = ctl_be_block_move_done; 697229997Sken io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 698229997Sken io->scsiio.kern_data_len = beio->io_len; 699229997Sken io->scsiio.kern_total_len = beio->io_len; 700229997Sken io->scsiio.kern_rel_offset = 0; 701229997Sken io->scsiio.kern_data_resid = 0; 702229997Sken io->scsiio.kern_sg_entries = beio->num_segs; 703229997Sken io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 704229997Sken#ifdef CTL_TIME_IO 705229997Sken getbintime(&io->io_hdr.dma_start_bt); 706229997Sken#endif 707229997Sken ctl_datamove(io); 708229997Sken } 709229997Sken} 710229997Sken 711229997Skenstatic void 712229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 713229997Sken struct ctl_be_block_io *beio) 714229997Sken{ 715229997Sken struct bio *bio; 716229997Sken union ctl_io *io; 717229997Sken struct ctl_be_block_devdata *dev_data; 718229997Sken 719229997Sken dev_data = &be_lun->backend.dev; 720229997Sken io = beio->io; 721229997Sken 722229997Sken DPRINTF("entered\n"); 723229997Sken 724229997Sken /* This can't fail, it's a blocking allocation. */ 725229997Sken bio = g_alloc_bio(); 726229997Sken 727229997Sken bio->bio_cmd = BIO_FLUSH; 728229997Sken bio->bio_flags |= BIO_ORDERED; 729229997Sken bio->bio_dev = dev_data->cdev; 730229997Sken bio->bio_offset = 0; 731229997Sken bio->bio_data = 0; 732229997Sken bio->bio_done = ctl_be_block_biodone; 733229997Sken bio->bio_caller1 = beio; 734229997Sken bio->bio_pblkno = 0; 735229997Sken 736229997Sken /* 737229997Sken * We don't need to acquire the LUN lock here, because we are only 738229997Sken * sending one bio, and so there is no other context to synchronize 739229997Sken * with. 740229997Sken */ 741229997Sken beio->num_bios_sent = 1; 742229997Sken beio->send_complete = 1; 743229997Sken 744229997Sken binuptime(&beio->ds_t0); 745229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 746229997Sken 747229997Sken (*dev_data->csw->d_strategy)(bio); 748229997Sken} 749229997Sken 750229997Skenstatic void 751229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 752229997Sken struct ctl_be_block_io *beio) 753229997Sken{ 754229997Sken int i; 755229997Sken struct bio *bio; 756229997Sken struct ctl_be_block_devdata *dev_data; 757229997Sken off_t cur_offset; 758229997Sken int max_iosize; 759229997Sken 760229997Sken DPRINTF("entered\n"); 761229997Sken 762229997Sken dev_data = &be_lun->backend.dev; 763229997Sken 764229997Sken /* 765229997Sken * We have to limit our I/O size to the maximum supported by the 766229997Sken * backend device. Hopefully it is MAXPHYS. If the driver doesn't 767229997Sken * set it properly, use DFLTPHYS. 768229997Sken */ 769229997Sken max_iosize = dev_data->cdev->si_iosize_max; 770229997Sken if (max_iosize < PAGE_SIZE) 771229997Sken max_iosize = DFLTPHYS; 772229997Sken 773229997Sken cur_offset = beio->io_offset; 774229997Sken 775229997Sken /* 776229997Sken * XXX KDM need to accurately reflect the number of I/Os outstanding 777229997Sken * to a device. 778229997Sken */ 779229997Sken binuptime(&beio->ds_t0); 780229997Sken devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 781229997Sken 782229997Sken for (i = 0; i < beio->num_segs; i++) { 783229997Sken size_t cur_size; 784229997Sken uint8_t *cur_ptr; 785229997Sken 786229997Sken cur_size = beio->sg_segs[i].len; 787229997Sken cur_ptr = beio->sg_segs[i].addr; 788229997Sken 789229997Sken while (cur_size > 0) { 790229997Sken /* This can't fail, it's a blocking allocation. */ 791229997Sken bio = g_alloc_bio(); 792229997Sken 793229997Sken KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 794229997Sken 795229997Sken bio->bio_cmd = beio->bio_cmd; 796229997Sken bio->bio_flags |= beio->bio_flags; 797229997Sken bio->bio_dev = dev_data->cdev; 798229997Sken bio->bio_caller1 = beio; 799229997Sken bio->bio_length = min(cur_size, max_iosize); 800229997Sken bio->bio_offset = cur_offset; 801229997Sken bio->bio_data = cur_ptr; 802229997Sken bio->bio_done = ctl_be_block_biodone; 803229997Sken bio->bio_pblkno = cur_offset / be_lun->blocksize; 804229997Sken 805229997Sken cur_offset += bio->bio_length; 806229997Sken cur_ptr += bio->bio_length; 807229997Sken cur_size -= bio->bio_length; 808229997Sken 809229997Sken /* 810229997Sken * Make sure we set the complete bit just before we 811229997Sken * issue the last bio so we don't wind up with a 812229997Sken * race. 813229997Sken * 814229997Sken * Use the LUN mutex here instead of a combination 815229997Sken * of atomic variables for simplicity. 816229997Sken * 817229997Sken * XXX KDM we could have a per-IO lock, but that 818229997Sken * would cause additional per-IO setup and teardown 819229997Sken * overhead. Hopefully there won't be too much 820229997Sken * contention on the LUN lock. 821229997Sken */ 822229997Sken mtx_lock(&be_lun->lock); 823229997Sken 824229997Sken beio->num_bios_sent++; 825229997Sken 826229997Sken if ((i == beio->num_segs - 1) 827229997Sken && (cur_size == 0)) 828229997Sken beio->send_complete = 1; 829229997Sken 830229997Sken mtx_unlock(&be_lun->lock); 831229997Sken 832229997Sken (*dev_data->csw->d_strategy)(bio); 833229997Sken } 834229997Sken } 835229997Sken} 836229997Sken 837229997Skenstatic void 838229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 839229997Sken union ctl_io *io) 840229997Sken{ 841229997Sken struct ctl_be_block_io *beio; 842229997Sken struct ctl_be_block_softc *softc; 843229997Sken 844229997Sken DPRINTF("entered\n"); 845229997Sken 846229997Sken softc = be_lun->softc; 847229997Sken beio = ctl_alloc_beio(softc); 848264020Strasz KASSERT(beio != NULL, ("ctl_alloc_beio() failed")); 849229997Sken 850229997Sken beio->io = io; 851229997Sken beio->softc = softc; 852229997Sken beio->lun = be_lun; 853229997Sken io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr = beio; 854229997Sken 855229997Sken switch (io->scsiio.cdb[0]) { 856229997Sken case SYNCHRONIZE_CACHE: 857229997Sken case SYNCHRONIZE_CACHE_16: 858249194Strasz beio->bio_cmd = BIO_FLUSH; 859229997Sken beio->ds_trans_type = DEVSTAT_NO_DATA; 860229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 861229997Sken beio->io_len = 0; 862229997Sken be_lun->lun_flush(be_lun, beio); 863229997Sken break; 864229997Sken default: 865229997Sken panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 866229997Sken break; 867229997Sken } 868229997Sken} 869229997Sken 870258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 871258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 872258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 873258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 874229997Sken 875229997Skenstatic void 876229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 877229997Sken union ctl_io *io) 878229997Sken{ 879229997Sken struct ctl_be_block_io *beio; 880229997Sken struct ctl_be_block_softc *softc; 881229997Sken struct ctl_lba_len lbalen; 882229997Sken uint64_t len_left, io_size_bytes; 883229997Sken int i; 884229997Sken 885229997Sken softc = be_lun->softc; 886229997Sken 887229997Sken DPRINTF("entered\n"); 888229997Sken 889229997Sken if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) { 890229997Sken SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 891229997Sken } else { 892229997Sken SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 893229997Sken } 894229997Sken 895229997Sken memcpy(&lbalen, io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 896229997Sken sizeof(lbalen)); 897229997Sken 898229997Sken io_size_bytes = lbalen.len * be_lun->blocksize; 899229997Sken 900229997Sken /* 901229997Sken * XXX KDM this is temporary, until we implement chaining of beio 902229997Sken * structures and multiple datamove calls to move all the data in 903229997Sken * or out. 904229997Sken */ 905229997Sken if (io_size_bytes > CTLBLK_MAX_IO_SIZE) { 906229997Sken printf("%s: IO length %ju > max io size %u\n", __func__, 907229997Sken io_size_bytes, CTLBLK_MAX_IO_SIZE); 908229997Sken ctl_set_invalid_field(&io->scsiio, 909229997Sken /*sks_valid*/ 0, 910229997Sken /*command*/ 1, 911229997Sken /*field*/ 0, 912229997Sken /*bit_valid*/ 0, 913229997Sken /*bit*/ 0); 914229997Sken ctl_done(io); 915229997Sken return; 916229997Sken } 917229997Sken 918229997Sken beio = ctl_alloc_beio(softc); 919264020Strasz KASSERT(beio != NULL, ("ctl_alloc_beio() failed")); 920229997Sken 921229997Sken beio->io = io; 922229997Sken beio->softc = softc; 923229997Sken beio->lun = be_lun; 924229997Sken io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr = beio; 925229997Sken 926229997Sken /* 927229997Sken * If the I/O came down with an ordered or head of queue tag, set 928229997Sken * the BIO_ORDERED attribute. For head of queue tags, that's 929229997Sken * pretty much the best we can do. 930229997Sken * 931229997Sken * XXX KDM we don't have a great way to easily know about the FUA 932229997Sken * bit right now (it is decoded in ctl_read_write(), but we don't 933229997Sken * pass that knowledge to the backend), and in any case we would 934229997Sken * need to determine how to handle it. 935229997Sken */ 936229997Sken if ((io->scsiio.tag_type == CTL_TAG_ORDERED) 937229997Sken || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)) 938229997Sken beio->bio_flags = BIO_ORDERED; 939229997Sken 940229997Sken switch (io->scsiio.tag_type) { 941229997Sken case CTL_TAG_ORDERED: 942229997Sken beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 943229997Sken break; 944229997Sken case CTL_TAG_HEAD_OF_QUEUE: 945229997Sken beio->ds_tag_type = DEVSTAT_TAG_HEAD; 946229997Sken break; 947229997Sken case CTL_TAG_UNTAGGED: 948229997Sken case CTL_TAG_SIMPLE: 949229997Sken case CTL_TAG_ACA: 950229997Sken default: 951229997Sken beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 952229997Sken break; 953229997Sken } 954229997Sken 955229997Sken /* 956229997Sken * This path handles read and write only. The config write path 957229997Sken * handles flush operations. 958229997Sken */ 959229997Sken if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) { 960229997Sken beio->bio_cmd = BIO_READ; 961229997Sken beio->ds_trans_type = DEVSTAT_READ; 962229997Sken } else { 963229997Sken beio->bio_cmd = BIO_WRITE; 964229997Sken beio->ds_trans_type = DEVSTAT_WRITE; 965229997Sken } 966229997Sken 967229997Sken beio->io_len = lbalen.len * be_lun->blocksize; 968229997Sken beio->io_offset = lbalen.lba * be_lun->blocksize; 969229997Sken 970229997Sken DPRINTF("%s at LBA %jx len %u\n", 971229997Sken (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 972229997Sken (uintmax_t)lbalen.lba, lbalen.len); 973229997Sken 974229997Sken for (i = 0, len_left = io_size_bytes; i < CTLBLK_MAX_SEGS && 975229997Sken len_left > 0; i++) { 976229997Sken 977229997Sken /* 978229997Sken * Setup the S/G entry for this chunk. 979229997Sken */ 980229997Sken beio->sg_segs[i].len = min(MAXPHYS, len_left); 981229997Sken beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 982229997Sken 983229997Sken DPRINTF("segment %d addr %p len %zd\n", i, 984229997Sken beio->sg_segs[i].addr, beio->sg_segs[i].len); 985229997Sken 986229997Sken beio->num_segs++; 987229997Sken len_left -= beio->sg_segs[i].len; 988229997Sken } 989229997Sken 990229997Sken /* 991229997Sken * For the read case, we need to read the data into our buffers and 992229997Sken * then we can send it back to the user. For the write case, we 993229997Sken * need to get the data from the user first. 994229997Sken */ 995229997Sken if (beio->bio_cmd == BIO_READ) { 996229997Sken SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 997229997Sken be_lun->dispatch(be_lun, beio); 998229997Sken } else { 999229997Sken SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1000229997Sken io->scsiio.be_move_done = ctl_be_block_move_done; 1001229997Sken io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1002229997Sken io->scsiio.kern_data_len = beio->io_len; 1003229997Sken io->scsiio.kern_total_len = beio->io_len; 1004229997Sken io->scsiio.kern_rel_offset = 0; 1005229997Sken io->scsiio.kern_data_resid = 0; 1006229997Sken io->scsiio.kern_sg_entries = beio->num_segs; 1007229997Sken io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1008229997Sken#ifdef CTL_TIME_IO 1009229997Sken getbintime(&io->io_hdr.dma_start_bt); 1010229997Sken#endif 1011229997Sken ctl_datamove(io); 1012229997Sken } 1013229997Sken} 1014229997Sken 1015229997Skenstatic void 1016229997Skenctl_be_block_worker(void *context, int pending) 1017229997Sken{ 1018229997Sken struct ctl_be_block_lun *be_lun; 1019229997Sken struct ctl_be_block_softc *softc; 1020229997Sken union ctl_io *io; 1021229997Sken 1022229997Sken be_lun = (struct ctl_be_block_lun *)context; 1023229997Sken softc = be_lun->softc; 1024229997Sken 1025229997Sken DPRINTF("entered\n"); 1026229997Sken 1027229997Sken mtx_lock(&be_lun->lock); 1028229997Sken for (;;) { 1029229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1030229997Sken if (io != NULL) { 1031229997Sken struct ctl_be_block_io *beio; 1032229997Sken 1033229997Sken DPRINTF("datamove queue\n"); 1034229997Sken 1035229997Sken STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1036229997Sken ctl_io_hdr, links); 1037229997Sken 1038229997Sken mtx_unlock(&be_lun->lock); 1039229997Sken 1040229997Sken beio = (struct ctl_be_block_io *) 1041229997Sken io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr; 1042229997Sken 1043229997Sken be_lun->dispatch(be_lun, beio); 1044229997Sken 1045229997Sken mtx_lock(&be_lun->lock); 1046229997Sken continue; 1047229997Sken } 1048229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1049229997Sken if (io != NULL) { 1050229997Sken 1051229997Sken DPRINTF("config write queue\n"); 1052229997Sken 1053229997Sken STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1054229997Sken ctl_io_hdr, links); 1055229997Sken 1056229997Sken mtx_unlock(&be_lun->lock); 1057229997Sken 1058229997Sken ctl_be_block_cw_dispatch(be_lun, io); 1059229997Sken 1060229997Sken mtx_lock(&be_lun->lock); 1061229997Sken continue; 1062229997Sken } 1063229997Sken io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1064229997Sken if (io != NULL) { 1065229997Sken DPRINTF("input queue\n"); 1066229997Sken 1067229997Sken STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1068229997Sken ctl_io_hdr, links); 1069229997Sken mtx_unlock(&be_lun->lock); 1070229997Sken 1071229997Sken /* 1072229997Sken * We must drop the lock, since this routine and 1073229997Sken * its children may sleep. 1074229997Sken */ 1075229997Sken ctl_be_block_dispatch(be_lun, io); 1076229997Sken 1077229997Sken mtx_lock(&be_lun->lock); 1078229997Sken continue; 1079229997Sken } 1080229997Sken 1081229997Sken /* 1082229997Sken * If we get here, there is no work left in the queues, so 1083229997Sken * just break out and let the task queue go to sleep. 1084229997Sken */ 1085229997Sken break; 1086229997Sken } 1087229997Sken mtx_unlock(&be_lun->lock); 1088229997Sken} 1089229997Sken 1090229997Sken/* 1091229997Sken * Entry point from CTL to the backend for I/O. We queue everything to a 1092229997Sken * work thread, so this just puts the I/O on a queue and wakes up the 1093229997Sken * thread. 1094229997Sken */ 1095229997Skenstatic int 1096229997Skenctl_be_block_submit(union ctl_io *io) 1097229997Sken{ 1098229997Sken struct ctl_be_block_lun *be_lun; 1099229997Sken struct ctl_be_lun *ctl_be_lun; 1100229997Sken int retval; 1101229997Sken 1102229997Sken DPRINTF("entered\n"); 1103229997Sken 1104229997Sken retval = CTL_RETVAL_COMPLETE; 1105229997Sken 1106229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1107229997Sken CTL_PRIV_BACKEND_LUN].ptr; 1108229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 1109229997Sken 1110229997Sken /* 1111229997Sken * Make sure we only get SCSI I/O. 1112229997Sken */ 1113229997Sken KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1114229997Sken "%#x) encountered", io->io_hdr.io_type)); 1115229997Sken 1116229997Sken mtx_lock(&be_lun->lock); 1117229997Sken /* 1118229997Sken * XXX KDM make sure that links is okay to use at this point. 1119229997Sken * Otherwise, we either need to add another field to ctl_io_hdr, 1120229997Sken * or deal with resource allocation here. 1121229997Sken */ 1122229997Sken STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1123229997Sken mtx_unlock(&be_lun->lock); 1124229997Sken 1125229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1126229997Sken 1127229997Sken return (retval); 1128229997Sken} 1129229997Sken 1130229997Skenstatic int 1131229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1132229997Sken int flag, struct thread *td) 1133229997Sken{ 1134229997Sken struct ctl_be_block_softc *softc; 1135229997Sken int error; 1136229997Sken 1137229997Sken softc = &backend_block_softc; 1138229997Sken 1139229997Sken error = 0; 1140229997Sken 1141229997Sken switch (cmd) { 1142229997Sken case CTL_LUN_REQ: { 1143229997Sken struct ctl_lun_req *lun_req; 1144229997Sken 1145229997Sken lun_req = (struct ctl_lun_req *)addr; 1146229997Sken 1147229997Sken switch (lun_req->reqtype) { 1148229997Sken case CTL_LUNREQ_CREATE: 1149229997Sken error = ctl_be_block_create(softc, lun_req); 1150229997Sken break; 1151229997Sken case CTL_LUNREQ_RM: 1152229997Sken error = ctl_be_block_rm(softc, lun_req); 1153229997Sken break; 1154232604Strasz case CTL_LUNREQ_MODIFY: 1155232604Strasz error = ctl_be_block_modify(softc, lun_req); 1156232604Strasz break; 1157229997Sken default: 1158229997Sken lun_req->status = CTL_LUN_ERROR; 1159229997Sken snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1160229997Sken "%s: invalid LUN request type %d", __func__, 1161229997Sken lun_req->reqtype); 1162229997Sken break; 1163229997Sken } 1164229997Sken break; 1165229997Sken } 1166229997Sken default: 1167229997Sken error = ENOTTY; 1168229997Sken break; 1169229997Sken } 1170229997Sken 1171229997Sken return (error); 1172229997Sken} 1173229997Sken 1174229997Skenstatic int 1175229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1176229997Sken{ 1177229997Sken struct ctl_be_block_filedata *file_data; 1178229997Sken struct ctl_lun_create_params *params; 1179229997Sken struct vattr vattr; 1180229997Sken int error; 1181229997Sken 1182229997Sken error = 0; 1183229997Sken file_data = &be_lun->backend.file; 1184229997Sken params = &req->reqdata.create; 1185229997Sken 1186229997Sken be_lun->dev_type = CTL_BE_BLOCK_FILE; 1187229997Sken be_lun->dispatch = ctl_be_block_dispatch_file; 1188229997Sken be_lun->lun_flush = ctl_be_block_flush_file; 1189229997Sken 1190229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1191229997Sken if (error != 0) { 1192229997Sken snprintf(req->error_str, sizeof(req->error_str), 1193229997Sken "error calling VOP_GETATTR() for file %s", 1194229997Sken be_lun->dev_path); 1195229997Sken return (error); 1196229997Sken } 1197229997Sken 1198229997Sken /* 1199229997Sken * Verify that we have the ability to upgrade to exclusive 1200229997Sken * access on this file so we can trap errors at open instead 1201229997Sken * of reporting them during first access. 1202229997Sken */ 1203229997Sken if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1204229997Sken vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1205229997Sken if (be_lun->vn->v_iflag & VI_DOOMED) { 1206229997Sken error = EBADF; 1207229997Sken snprintf(req->error_str, sizeof(req->error_str), 1208229997Sken "error locking file %s", be_lun->dev_path); 1209229997Sken return (error); 1210229997Sken } 1211229997Sken } 1212229997Sken 1213229997Sken 1214229997Sken file_data->cred = crhold(curthread->td_ucred); 1215232604Strasz if (params->lun_size_bytes != 0) 1216232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1217232604Strasz else 1218232604Strasz be_lun->size_bytes = vattr.va_size; 1219229997Sken /* 1220229997Sken * We set the multi thread flag for file operations because all 1221229997Sken * filesystems (in theory) are capable of allowing multiple readers 1222229997Sken * of a file at once. So we want to get the maximum possible 1223229997Sken * concurrency. 1224229997Sken */ 1225229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD; 1226229997Sken 1227229997Sken /* 1228229997Sken * XXX KDM vattr.va_blocksize may be larger than 512 bytes here. 1229229997Sken * With ZFS, it is 131072 bytes. Block sizes that large don't work 1230229997Sken * with disklabel and UFS on FreeBSD at least. Large block sizes 1231229997Sken * may not work with other OSes as well. So just export a sector 1232229997Sken * size of 512 bytes, which should work with any OS or 1233229997Sken * application. Since our backing is a file, any block size will 1234229997Sken * work fine for the backing store. 1235229997Sken */ 1236229997Sken#if 0 1237229997Sken be_lun->blocksize= vattr.va_blocksize; 1238229997Sken#endif 1239229997Sken if (params->blocksize_bytes != 0) 1240229997Sken be_lun->blocksize = params->blocksize_bytes; 1241229997Sken else 1242229997Sken be_lun->blocksize = 512; 1243229997Sken 1244229997Sken /* 1245229997Sken * Sanity check. The media size has to be at least one 1246229997Sken * sector long. 1247229997Sken */ 1248229997Sken if (be_lun->size_bytes < be_lun->blocksize) { 1249229997Sken error = EINVAL; 1250229997Sken snprintf(req->error_str, sizeof(req->error_str), 1251229997Sken "file %s size %ju < block size %u", be_lun->dev_path, 1252229997Sken (uintmax_t)be_lun->size_bytes, be_lun->blocksize); 1253229997Sken } 1254229997Sken return (error); 1255229997Sken} 1256229997Sken 1257229997Skenstatic int 1258229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1259229997Sken{ 1260229997Sken struct ctl_lun_create_params *params; 1261229997Sken struct vattr vattr; 1262229997Sken struct cdev *dev; 1263229997Sken struct cdevsw *devsw; 1264229997Sken int error; 1265229997Sken 1266229997Sken params = &req->reqdata.create; 1267229997Sken 1268229997Sken be_lun->dev_type = CTL_BE_BLOCK_DEV; 1269229997Sken be_lun->dispatch = ctl_be_block_dispatch_dev; 1270229997Sken be_lun->lun_flush = ctl_be_block_flush_dev; 1271229997Sken be_lun->backend.dev.cdev = be_lun->vn->v_rdev; 1272229997Sken be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev, 1273229997Sken &be_lun->backend.dev.dev_ref); 1274229997Sken if (be_lun->backend.dev.csw == NULL) 1275229997Sken panic("Unable to retrieve device switch"); 1276229997Sken 1277229997Sken error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED); 1278229997Sken if (error) { 1279229997Sken snprintf(req->error_str, sizeof(req->error_str), 1280229997Sken "%s: error getting vnode attributes for device %s", 1281229997Sken __func__, be_lun->dev_path); 1282229997Sken return (error); 1283229997Sken } 1284229997Sken 1285229997Sken dev = be_lun->vn->v_rdev; 1286229997Sken devsw = dev->si_devsw; 1287229997Sken if (!devsw->d_ioctl) { 1288229997Sken snprintf(req->error_str, sizeof(req->error_str), 1289229997Sken "%s: no d_ioctl for device %s!", __func__, 1290229997Sken be_lun->dev_path); 1291229997Sken return (ENODEV); 1292229997Sken } 1293229997Sken 1294229997Sken error = devsw->d_ioctl(dev, DIOCGSECTORSIZE, 1295229997Sken (caddr_t)&be_lun->blocksize, FREAD, 1296229997Sken curthread); 1297229997Sken if (error) { 1298229997Sken snprintf(req->error_str, sizeof(req->error_str), 1299229997Sken "%s: error %d returned for DIOCGSECTORSIZE ioctl " 1300229997Sken "on %s!", __func__, error, be_lun->dev_path); 1301229997Sken return (error); 1302229997Sken } 1303229997Sken 1304229997Sken /* 1305229997Sken * If the user has asked for a blocksize that is greater than the 1306229997Sken * backing device's blocksize, we can do it only if the blocksize 1307229997Sken * the user is asking for is an even multiple of the underlying 1308229997Sken * device's blocksize. 1309229997Sken */ 1310229997Sken if ((params->blocksize_bytes != 0) 1311229997Sken && (params->blocksize_bytes > be_lun->blocksize)) { 1312229997Sken uint32_t bs_multiple, tmp_blocksize; 1313229997Sken 1314229997Sken bs_multiple = params->blocksize_bytes / be_lun->blocksize; 1315229997Sken 1316229997Sken tmp_blocksize = bs_multiple * be_lun->blocksize; 1317229997Sken 1318229997Sken if (tmp_blocksize == params->blocksize_bytes) { 1319229997Sken be_lun->blocksize = params->blocksize_bytes; 1320229997Sken } else { 1321229997Sken snprintf(req->error_str, sizeof(req->error_str), 1322229997Sken "%s: requested blocksize %u is not an even " 1323229997Sken "multiple of backing device blocksize %u", 1324229997Sken __func__, params->blocksize_bytes, 1325229997Sken be_lun->blocksize); 1326229997Sken return (EINVAL); 1327229997Sken 1328229997Sken } 1329229997Sken } else if ((params->blocksize_bytes != 0) 1330229997Sken && (params->blocksize_bytes != be_lun->blocksize)) { 1331229997Sken snprintf(req->error_str, sizeof(req->error_str), 1332229997Sken "%s: requested blocksize %u < backing device " 1333229997Sken "blocksize %u", __func__, params->blocksize_bytes, 1334229997Sken be_lun->blocksize); 1335229997Sken return (EINVAL); 1336229997Sken } 1337229997Sken 1338229997Sken error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1339229997Sken (caddr_t)&be_lun->size_bytes, FREAD, 1340229997Sken curthread); 1341229997Sken if (error) { 1342229997Sken snprintf(req->error_str, sizeof(req->error_str), 1343232604Strasz "%s: error %d returned for DIOCGMEDIASIZE " 1344232604Strasz " ioctl on %s!", __func__, error, 1345232604Strasz be_lun->dev_path); 1346229997Sken return (error); 1347229997Sken } 1348229997Sken 1349232604Strasz if (params->lun_size_bytes != 0) { 1350232604Strasz if (params->lun_size_bytes > be_lun->size_bytes) { 1351232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1352232604Strasz "%s: requested LUN size %ju > backing device " 1353232604Strasz "size %ju", __func__, 1354232604Strasz (uintmax_t)params->lun_size_bytes, 1355232604Strasz (uintmax_t)be_lun->size_bytes); 1356232604Strasz return (EINVAL); 1357232604Strasz } 1358232604Strasz 1359232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1360232604Strasz } 1361232604Strasz 1362229997Sken return (0); 1363229997Sken} 1364229997Sken 1365229997Skenstatic int 1366229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun) 1367229997Sken{ 1368229997Sken DROP_GIANT(); 1369229997Sken if (be_lun->vn) { 1370229997Sken int flags = FREAD | FWRITE; 1371229997Sken 1372229997Sken switch (be_lun->dev_type) { 1373229997Sken case CTL_BE_BLOCK_DEV: 1374229997Sken if (be_lun->backend.dev.csw) { 1375229997Sken dev_relthread(be_lun->backend.dev.cdev, 1376229997Sken be_lun->backend.dev.dev_ref); 1377229997Sken be_lun->backend.dev.csw = NULL; 1378229997Sken be_lun->backend.dev.cdev = NULL; 1379229997Sken } 1380229997Sken break; 1381229997Sken case CTL_BE_BLOCK_FILE: 1382229997Sken break; 1383229997Sken case CTL_BE_BLOCK_NONE: 1384258871Strasz break; 1385229997Sken default: 1386229997Sken panic("Unexpected backend type."); 1387229997Sken break; 1388229997Sken } 1389229997Sken 1390229997Sken (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 1391229997Sken be_lun->vn = NULL; 1392229997Sken 1393229997Sken switch (be_lun->dev_type) { 1394229997Sken case CTL_BE_BLOCK_DEV: 1395229997Sken break; 1396229997Sken case CTL_BE_BLOCK_FILE: 1397229997Sken if (be_lun->backend.file.cred != NULL) { 1398229997Sken crfree(be_lun->backend.file.cred); 1399229997Sken be_lun->backend.file.cred = NULL; 1400229997Sken } 1401229997Sken break; 1402229997Sken case CTL_BE_BLOCK_NONE: 1403258871Strasz break; 1404229997Sken default: 1405229997Sken panic("Unexpected backend type."); 1406229997Sken break; 1407229997Sken } 1408229997Sken } 1409229997Sken PICKUP_GIANT(); 1410229997Sken 1411229997Sken return (0); 1412229997Sken} 1413229997Sken 1414229997Skenstatic int 1415229997Skenctl_be_block_open(struct ctl_be_block_softc *softc, 1416229997Sken struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1417229997Sken{ 1418229997Sken struct nameidata nd; 1419229997Sken int flags; 1420229997Sken int error; 1421229997Sken 1422229997Sken /* 1423229997Sken * XXX KDM allow a read-only option? 1424229997Sken */ 1425229997Sken flags = FREAD | FWRITE; 1426229997Sken error = 0; 1427229997Sken 1428229997Sken if (rootvnode == NULL) { 1429229997Sken snprintf(req->error_str, sizeof(req->error_str), 1430229997Sken "%s: Root filesystem is not mounted", __func__); 1431229997Sken return (1); 1432229997Sken } 1433229997Sken 1434229997Sken if (!curthread->td_proc->p_fd->fd_cdir) { 1435229997Sken curthread->td_proc->p_fd->fd_cdir = rootvnode; 1436229997Sken VREF(rootvnode); 1437229997Sken } 1438229997Sken if (!curthread->td_proc->p_fd->fd_rdir) { 1439229997Sken curthread->td_proc->p_fd->fd_rdir = rootvnode; 1440229997Sken VREF(rootvnode); 1441229997Sken } 1442229997Sken if (!curthread->td_proc->p_fd->fd_jdir) { 1443229997Sken curthread->td_proc->p_fd->fd_jdir = rootvnode; 1444229997Sken VREF(rootvnode); 1445229997Sken } 1446229997Sken 1447229997Sken again: 1448229997Sken NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 1449229997Sken error = vn_open(&nd, &flags, 0, NULL); 1450229997Sken if (error) { 1451229997Sken /* 1452229997Sken * This is the only reasonable guess we can make as far as 1453229997Sken * path if the user doesn't give us a fully qualified path. 1454229997Sken * If they want to specify a file, they need to specify the 1455229997Sken * full path. 1456229997Sken */ 1457229997Sken if (be_lun->dev_path[0] != '/') { 1458229997Sken char *dev_path = "/dev/"; 1459229997Sken char *dev_name; 1460229997Sken 1461229997Sken /* Try adding device path at beginning of name */ 1462229997Sken dev_name = malloc(strlen(be_lun->dev_path) 1463229997Sken + strlen(dev_path) + 1, 1464229997Sken M_CTLBLK, M_WAITOK); 1465229997Sken if (dev_name) { 1466229997Sken sprintf(dev_name, "%s%s", dev_path, 1467229997Sken be_lun->dev_path); 1468229997Sken free(be_lun->dev_path, M_CTLBLK); 1469229997Sken be_lun->dev_path = dev_name; 1470229997Sken goto again; 1471229997Sken } 1472229997Sken } 1473229997Sken snprintf(req->error_str, sizeof(req->error_str), 1474229997Sken "%s: error opening %s", __func__, be_lun->dev_path); 1475229997Sken return (error); 1476229997Sken } 1477229997Sken 1478229997Sken NDFREE(&nd, NDF_ONLY_PNBUF); 1479229997Sken 1480229997Sken be_lun->vn = nd.ni_vp; 1481229997Sken 1482229997Sken /* We only support disks and files. */ 1483229997Sken if (vn_isdisk(be_lun->vn, &error)) { 1484229997Sken error = ctl_be_block_open_dev(be_lun, req); 1485229997Sken } else if (be_lun->vn->v_type == VREG) { 1486229997Sken error = ctl_be_block_open_file(be_lun, req); 1487229997Sken } else { 1488229997Sken error = EINVAL; 1489229997Sken snprintf(req->error_str, sizeof(req->error_str), 1490258871Strasz "%s is not a disk or plain file", be_lun->dev_path); 1491229997Sken } 1492229997Sken VOP_UNLOCK(be_lun->vn, 0); 1493229997Sken 1494229997Sken if (error != 0) { 1495229997Sken ctl_be_block_close(be_lun); 1496229997Sken return (error); 1497229997Sken } 1498229997Sken 1499229997Sken be_lun->blocksize_shift = fls(be_lun->blocksize) - 1; 1500229997Sken be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 1501229997Sken 1502229997Sken return (0); 1503229997Sken} 1504229997Sken 1505229997Skenstatic int 1506229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1507229997Sken{ 1508229997Sken struct ctl_be_block_lun *be_lun; 1509229997Sken struct ctl_lun_create_params *params; 1510229997Sken struct ctl_be_arg *file_arg; 1511229997Sken char tmpstr[32]; 1512229997Sken int retval, num_threads; 1513229997Sken int i; 1514229997Sken 1515229997Sken params = &req->reqdata.create; 1516229997Sken retval = 0; 1517229997Sken 1518229997Sken num_threads = cbb_num_threads; 1519229997Sken 1520229997Sken file_arg = NULL; 1521229997Sken 1522229997Sken be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 1523229997Sken 1524229997Sken be_lun->softc = softc; 1525229997Sken STAILQ_INIT(&be_lun->input_queue); 1526229997Sken STAILQ_INIT(&be_lun->config_write_queue); 1527229997Sken STAILQ_INIT(&be_lun->datamove_queue); 1528254759Strasz STAILQ_INIT(&be_lun->ctl_be_lun.options); 1529229997Sken sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 1530229997Sken mtx_init(&be_lun->lock, be_lun->lunname, NULL, MTX_DEF); 1531229997Sken 1532229997Sken be_lun->lun_zone = uma_zcreate(be_lun->lunname, MAXPHYS, 1533256995Smav NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 1534229997Sken 1535229997Sken if (be_lun->lun_zone == NULL) { 1536229997Sken snprintf(req->error_str, sizeof(req->error_str), 1537229997Sken "%s: error allocating UMA zone", __func__); 1538229997Sken goto bailout_error; 1539229997Sken } 1540229997Sken 1541229997Sken if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 1542229997Sken be_lun->ctl_be_lun.lun_type = params->device_type; 1543229997Sken else 1544229997Sken be_lun->ctl_be_lun.lun_type = T_DIRECT; 1545229997Sken 1546229997Sken if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 1547229997Sken for (i = 0; i < req->num_be_args; i++) { 1548249026Strasz if (strcmp(req->kern_be_args[i].kname, "file") == 0) { 1549229997Sken file_arg = &req->kern_be_args[i]; 1550229997Sken break; 1551229997Sken } 1552229997Sken } 1553229997Sken 1554229997Sken if (file_arg == NULL) { 1555229997Sken snprintf(req->error_str, sizeof(req->error_str), 1556229997Sken "%s: no file argument specified", __func__); 1557229997Sken goto bailout_error; 1558229997Sken } 1559229997Sken 1560229997Sken be_lun->dev_path = malloc(file_arg->vallen, M_CTLBLK, 1561229997Sken M_WAITOK | M_ZERO); 1562229997Sken 1563249026Strasz strlcpy(be_lun->dev_path, (char *)file_arg->kvalue, 1564229997Sken file_arg->vallen); 1565229997Sken 1566229997Sken retval = ctl_be_block_open(softc, be_lun, req); 1567229997Sken if (retval != 0) { 1568229997Sken retval = 0; 1569229997Sken goto bailout_error; 1570229997Sken } 1571229997Sken 1572229997Sken /* 1573229997Sken * Tell the user the size of the file/device. 1574229997Sken */ 1575229997Sken params->lun_size_bytes = be_lun->size_bytes; 1576229997Sken 1577229997Sken /* 1578229997Sken * The maximum LBA is the size - 1. 1579229997Sken */ 1580229997Sken be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 1581229997Sken } else { 1582229997Sken /* 1583229997Sken * For processor devices, we don't have any size. 1584229997Sken */ 1585229997Sken be_lun->blocksize = 0; 1586229997Sken be_lun->size_blocks = 0; 1587229997Sken be_lun->size_bytes = 0; 1588229997Sken be_lun->ctl_be_lun.maxlba = 0; 1589229997Sken params->lun_size_bytes = 0; 1590229997Sken 1591229997Sken /* 1592229997Sken * Default to just 1 thread for processor devices. 1593229997Sken */ 1594229997Sken num_threads = 1; 1595229997Sken } 1596229997Sken 1597229997Sken /* 1598229997Sken * XXX This searching loop might be refactored to be combined with 1599229997Sken * the loop above, 1600229997Sken */ 1601229997Sken for (i = 0; i < req->num_be_args; i++) { 1602249026Strasz if (strcmp(req->kern_be_args[i].kname, "num_threads") == 0) { 1603229997Sken struct ctl_be_arg *thread_arg; 1604229997Sken char num_thread_str[16]; 1605229997Sken int tmp_num_threads; 1606229997Sken 1607229997Sken 1608229997Sken thread_arg = &req->kern_be_args[i]; 1609229997Sken 1610249026Strasz strlcpy(num_thread_str, (char *)thread_arg->kvalue, 1611229997Sken min(thread_arg->vallen, 1612229997Sken sizeof(num_thread_str))); 1613229997Sken 1614229997Sken tmp_num_threads = strtol(num_thread_str, NULL, 0); 1615229997Sken 1616229997Sken /* 1617229997Sken * We don't let the user specify less than one 1618229997Sken * thread, but hope he's clueful enough not to 1619229997Sken * specify 1000 threads. 1620229997Sken */ 1621229997Sken if (tmp_num_threads < 1) { 1622229997Sken snprintf(req->error_str, sizeof(req->error_str), 1623229997Sken "%s: invalid number of threads %s", 1624229997Sken __func__, num_thread_str); 1625229997Sken goto bailout_error; 1626229997Sken } 1627229997Sken 1628229997Sken num_threads = tmp_num_threads; 1629254759Strasz } else if (strcmp(req->kern_be_args[i].kname, "file") != 0 && 1630254759Strasz strcmp(req->kern_be_args[i].kname, "dev") != 0) { 1631254759Strasz struct ctl_be_lun_option *opt; 1632254759Strasz 1633254759Strasz opt = malloc(sizeof(*opt), M_CTLBLK, M_WAITOK); 1634254759Strasz opt->name = malloc(strlen(req->kern_be_args[i].kname) + 1, M_CTLBLK, M_WAITOK); 1635254759Strasz strcpy(opt->name, req->kern_be_args[i].kname); 1636254759Strasz opt->value = malloc(strlen(req->kern_be_args[i].kvalue) + 1, M_CTLBLK, M_WAITOK); 1637254759Strasz strcpy(opt->value, req->kern_be_args[i].kvalue); 1638254759Strasz STAILQ_INSERT_TAIL(&be_lun->ctl_be_lun.options, opt, links); 1639229997Sken } 1640229997Sken } 1641229997Sken 1642229997Sken be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 1643229997Sken be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 1644229997Sken be_lun->ctl_be_lun.be_lun = be_lun; 1645229997Sken be_lun->ctl_be_lun.blocksize = be_lun->blocksize; 1646229997Sken /* Tell the user the blocksize we ended up using */ 1647229997Sken params->blocksize_bytes = be_lun->blocksize; 1648229997Sken if (params->flags & CTL_LUN_FLAG_ID_REQ) { 1649229997Sken be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 1650229997Sken be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 1651229997Sken } else 1652229997Sken be_lun->ctl_be_lun.req_lun_id = 0; 1653229997Sken 1654229997Sken be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown; 1655229997Sken be_lun->ctl_be_lun.lun_config_status = 1656229997Sken ctl_be_block_lun_config_status; 1657229997Sken be_lun->ctl_be_lun.be = &ctl_be_block_driver; 1658229997Sken 1659229997Sken if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 1660229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 1661229997Sken softc->num_luns); 1662229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 1663229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1664229997Sken sizeof(tmpstr))); 1665229997Sken 1666229997Sken /* Tell the user what we used for a serial number */ 1667229997Sken strncpy((char *)params->serial_num, tmpstr, 1668229997Sken ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 1669229997Sken } else { 1670229997Sken strncpy((char *)be_lun->ctl_be_lun.serial_num, 1671229997Sken params->serial_num, 1672229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 1673229997Sken sizeof(params->serial_num))); 1674229997Sken } 1675229997Sken if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 1676229997Sken snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 1677229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 1678229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1679229997Sken sizeof(tmpstr))); 1680229997Sken 1681229997Sken /* Tell the user what we used for a device ID */ 1682229997Sken strncpy((char *)params->device_id, tmpstr, 1683229997Sken ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 1684229997Sken } else { 1685229997Sken strncpy((char *)be_lun->ctl_be_lun.device_id, 1686229997Sken params->device_id, 1687229997Sken ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 1688229997Sken sizeof(params->device_id))); 1689229997Sken } 1690229997Sken 1691229997Sken TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 1692229997Sken 1693229997Sken be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 1694229997Sken taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 1695229997Sken 1696229997Sken if (be_lun->io_taskqueue == NULL) { 1697229997Sken snprintf(req->error_str, sizeof(req->error_str), 1698229997Sken "%s: Unable to create taskqueue", __func__); 1699229997Sken goto bailout_error; 1700229997Sken } 1701229997Sken 1702229997Sken /* 1703229997Sken * Note that we start the same number of threads by default for 1704229997Sken * both the file case and the block device case. For the file 1705229997Sken * case, we need multiple threads to allow concurrency, because the 1706229997Sken * vnode interface is designed to be a blocking interface. For the 1707229997Sken * block device case, ZFS zvols at least will block the caller's 1708229997Sken * context in many instances, and so we need multiple threads to 1709229997Sken * overcome that problem. Other block devices don't need as many 1710229997Sken * threads, but they shouldn't cause too many problems. 1711229997Sken * 1712229997Sken * If the user wants to just have a single thread for a block 1713229997Sken * device, he can specify that when the LUN is created, or change 1714229997Sken * the tunable/sysctl to alter the default number of threads. 1715229997Sken */ 1716229997Sken retval = taskqueue_start_threads(&be_lun->io_taskqueue, 1717229997Sken /*num threads*/num_threads, 1718229997Sken /*priority*/PWAIT, 1719229997Sken /*thread name*/ 1720229997Sken "%s taskq", be_lun->lunname); 1721229997Sken 1722229997Sken if (retval != 0) 1723229997Sken goto bailout_error; 1724229997Sken 1725229997Sken be_lun->num_threads = num_threads; 1726229997Sken 1727229997Sken mtx_lock(&softc->lock); 1728229997Sken softc->num_luns++; 1729229997Sken STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 1730229997Sken 1731229997Sken mtx_unlock(&softc->lock); 1732229997Sken 1733229997Sken retval = ctl_add_lun(&be_lun->ctl_be_lun); 1734229997Sken if (retval != 0) { 1735229997Sken mtx_lock(&softc->lock); 1736229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 1737229997Sken links); 1738229997Sken softc->num_luns--; 1739229997Sken mtx_unlock(&softc->lock); 1740229997Sken snprintf(req->error_str, sizeof(req->error_str), 1741229997Sken "%s: ctl_add_lun() returned error %d, see dmesg for " 1742229997Sken "details", __func__, retval); 1743229997Sken retval = 0; 1744229997Sken goto bailout_error; 1745229997Sken } 1746229997Sken 1747229997Sken mtx_lock(&softc->lock); 1748229997Sken 1749229997Sken /* 1750229997Sken * Tell the config_status routine that we're waiting so it won't 1751229997Sken * clean up the LUN in the event of an error. 1752229997Sken */ 1753229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 1754229997Sken 1755229997Sken while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 1756229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 1757229997Sken if (retval == EINTR) 1758229997Sken break; 1759229997Sken } 1760229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 1761229997Sken 1762229997Sken if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 1763229997Sken snprintf(req->error_str, sizeof(req->error_str), 1764229997Sken "%s: LUN configuration error, see dmesg for details", 1765229997Sken __func__); 1766229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 1767229997Sken links); 1768229997Sken softc->num_luns--; 1769229997Sken mtx_unlock(&softc->lock); 1770229997Sken goto bailout_error; 1771229997Sken } else { 1772229997Sken params->req_lun_id = be_lun->ctl_be_lun.lun_id; 1773229997Sken } 1774229997Sken 1775229997Sken mtx_unlock(&softc->lock); 1776229997Sken 1777229997Sken be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 1778229997Sken be_lun->blocksize, 1779229997Sken DEVSTAT_ALL_SUPPORTED, 1780229997Sken be_lun->ctl_be_lun.lun_type 1781229997Sken | DEVSTAT_TYPE_IF_OTHER, 1782229997Sken DEVSTAT_PRIORITY_OTHER); 1783229997Sken 1784229997Sken 1785229997Sken req->status = CTL_LUN_OK; 1786229997Sken 1787229997Sken return (retval); 1788229997Sken 1789229997Skenbailout_error: 1790229997Sken req->status = CTL_LUN_ERROR; 1791229997Sken 1792229997Sken ctl_be_block_close(be_lun); 1793229997Sken 1794229997Sken free(be_lun->dev_path, M_CTLBLK); 1795229997Sken free(be_lun, M_CTLBLK); 1796229997Sken 1797229997Sken return (retval); 1798229997Sken} 1799229997Sken 1800229997Skenstatic int 1801229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1802229997Sken{ 1803229997Sken struct ctl_lun_rm_params *params; 1804229997Sken struct ctl_be_block_lun *be_lun; 1805229997Sken int retval; 1806229997Sken 1807229997Sken params = &req->reqdata.rm; 1808229997Sken 1809229997Sken mtx_lock(&softc->lock); 1810229997Sken 1811229997Sken be_lun = NULL; 1812229997Sken 1813229997Sken STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 1814229997Sken if (be_lun->ctl_be_lun.lun_id == params->lun_id) 1815229997Sken break; 1816229997Sken } 1817229997Sken mtx_unlock(&softc->lock); 1818229997Sken 1819229997Sken if (be_lun == NULL) { 1820229997Sken snprintf(req->error_str, sizeof(req->error_str), 1821229997Sken "%s: LUN %u is not managed by the block backend", 1822229997Sken __func__, params->lun_id); 1823229997Sken goto bailout_error; 1824229997Sken } 1825229997Sken 1826229997Sken retval = ctl_disable_lun(&be_lun->ctl_be_lun); 1827229997Sken 1828229997Sken if (retval != 0) { 1829229997Sken snprintf(req->error_str, sizeof(req->error_str), 1830229997Sken "%s: error %d returned from ctl_disable_lun() for " 1831229997Sken "LUN %d", __func__, retval, params->lun_id); 1832229997Sken goto bailout_error; 1833229997Sken 1834229997Sken } 1835229997Sken 1836229997Sken retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 1837229997Sken if (retval != 0) { 1838229997Sken snprintf(req->error_str, sizeof(req->error_str), 1839229997Sken "%s: error %d returned from ctl_invalidate_lun() for " 1840229997Sken "LUN %d", __func__, retval, params->lun_id); 1841229997Sken goto bailout_error; 1842229997Sken } 1843229997Sken 1844229997Sken mtx_lock(&softc->lock); 1845229997Sken 1846229997Sken be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 1847229997Sken 1848229997Sken while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 1849229997Sken retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 1850229997Sken if (retval == EINTR) 1851229997Sken break; 1852229997Sken } 1853229997Sken 1854229997Sken be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 1855229997Sken 1856229997Sken if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 1857229997Sken snprintf(req->error_str, sizeof(req->error_str), 1858229997Sken "%s: interrupted waiting for LUN to be freed", 1859229997Sken __func__); 1860229997Sken mtx_unlock(&softc->lock); 1861229997Sken goto bailout_error; 1862229997Sken } 1863229997Sken 1864229997Sken STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 1865229997Sken 1866229997Sken softc->num_luns--; 1867229997Sken mtx_unlock(&softc->lock); 1868229997Sken 1869229997Sken taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 1870229997Sken 1871229997Sken taskqueue_free(be_lun->io_taskqueue); 1872229997Sken 1873229997Sken ctl_be_block_close(be_lun); 1874229997Sken 1875229997Sken if (be_lun->disk_stats != NULL) 1876229997Sken devstat_remove_entry(be_lun->disk_stats); 1877229997Sken 1878229997Sken uma_zdestroy(be_lun->lun_zone); 1879229997Sken 1880229997Sken free(be_lun->dev_path, M_CTLBLK); 1881229997Sken 1882229997Sken free(be_lun, M_CTLBLK); 1883229997Sken 1884229997Sken req->status = CTL_LUN_OK; 1885229997Sken 1886229997Sken return (0); 1887229997Sken 1888229997Skenbailout_error: 1889229997Sken 1890229997Sken req->status = CTL_LUN_ERROR; 1891229997Sken 1892229997Sken return (0); 1893229997Sken} 1894229997Sken 1895232604Straszstatic int 1896232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 1897232604Strasz struct ctl_lun_req *req) 1898232604Strasz{ 1899232604Strasz struct vattr vattr; 1900232604Strasz int error; 1901232604Strasz struct ctl_lun_modify_params *params; 1902232604Strasz 1903232604Strasz params = &req->reqdata.modify; 1904232604Strasz 1905232604Strasz if (params->lun_size_bytes != 0) { 1906232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1907232604Strasz } else { 1908232604Strasz error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1909232604Strasz if (error != 0) { 1910232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1911232604Strasz "error calling VOP_GETATTR() for file %s", 1912232604Strasz be_lun->dev_path); 1913232604Strasz return (error); 1914232604Strasz } 1915232604Strasz 1916232604Strasz be_lun->size_bytes = vattr.va_size; 1917232604Strasz } 1918232604Strasz 1919232604Strasz return (0); 1920232604Strasz} 1921232604Strasz 1922232604Straszstatic int 1923232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 1924232604Strasz struct ctl_lun_req *req) 1925232604Strasz{ 1926232604Strasz struct cdev *dev; 1927232604Strasz struct cdevsw *devsw; 1928232604Strasz int error; 1929232604Strasz struct ctl_lun_modify_params *params; 1930232604Strasz uint64_t size_bytes; 1931232604Strasz 1932232604Strasz params = &req->reqdata.modify; 1933232604Strasz 1934232604Strasz dev = be_lun->vn->v_rdev; 1935232604Strasz devsw = dev->si_devsw; 1936232604Strasz if (!devsw->d_ioctl) { 1937232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1938232604Strasz "%s: no d_ioctl for device %s!", __func__, 1939232604Strasz be_lun->dev_path); 1940232604Strasz return (ENODEV); 1941232604Strasz } 1942232604Strasz 1943232604Strasz error = devsw->d_ioctl(dev, DIOCGMEDIASIZE, 1944232604Strasz (caddr_t)&size_bytes, FREAD, 1945232604Strasz curthread); 1946232604Strasz if (error) { 1947232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1948232604Strasz "%s: error %d returned for DIOCGMEDIASIZE ioctl " 1949232604Strasz "on %s!", __func__, error, be_lun->dev_path); 1950232604Strasz return (error); 1951232604Strasz } 1952232604Strasz 1953232604Strasz if (params->lun_size_bytes != 0) { 1954232604Strasz if (params->lun_size_bytes > size_bytes) { 1955232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1956232604Strasz "%s: requested LUN size %ju > backing device " 1957232604Strasz "size %ju", __func__, 1958232604Strasz (uintmax_t)params->lun_size_bytes, 1959232604Strasz (uintmax_t)size_bytes); 1960232604Strasz return (EINVAL); 1961232604Strasz } 1962232604Strasz 1963232604Strasz be_lun->size_bytes = params->lun_size_bytes; 1964232604Strasz } else { 1965232604Strasz be_lun->size_bytes = size_bytes; 1966232604Strasz } 1967232604Strasz 1968232604Strasz return (0); 1969232604Strasz} 1970232604Strasz 1971232604Straszstatic int 1972232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 1973232604Strasz{ 1974232604Strasz struct ctl_lun_modify_params *params; 1975232604Strasz struct ctl_be_block_lun *be_lun; 1976241896Skib int error; 1977232604Strasz 1978232604Strasz params = &req->reqdata.modify; 1979232604Strasz 1980232604Strasz mtx_lock(&softc->lock); 1981232604Strasz 1982232604Strasz be_lun = NULL; 1983232604Strasz 1984232604Strasz STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 1985232604Strasz if (be_lun->ctl_be_lun.lun_id == params->lun_id) 1986232604Strasz break; 1987232604Strasz } 1988232604Strasz mtx_unlock(&softc->lock); 1989232604Strasz 1990232604Strasz if (be_lun == NULL) { 1991232604Strasz snprintf(req->error_str, sizeof(req->error_str), 1992232604Strasz "%s: LUN %u is not managed by the block backend", 1993232604Strasz __func__, params->lun_id); 1994232604Strasz goto bailout_error; 1995232604Strasz } 1996232604Strasz 1997232604Strasz if (params->lun_size_bytes != 0) { 1998232604Strasz if (params->lun_size_bytes < be_lun->blocksize) { 1999232604Strasz snprintf(req->error_str, sizeof(req->error_str), 2000232604Strasz "%s: LUN size %ju < blocksize %u", __func__, 2001232604Strasz params->lun_size_bytes, be_lun->blocksize); 2002232604Strasz goto bailout_error; 2003232604Strasz } 2004232604Strasz } 2005232604Strasz 2006232604Strasz vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2007232604Strasz 2008232604Strasz if (be_lun->vn->v_type == VREG) 2009232604Strasz error = ctl_be_block_modify_file(be_lun, req); 2010232604Strasz else 2011232604Strasz error = ctl_be_block_modify_dev(be_lun, req); 2012232604Strasz 2013232604Strasz VOP_UNLOCK(be_lun->vn, 0); 2014232604Strasz 2015232604Strasz if (error != 0) 2016232604Strasz goto bailout_error; 2017232604Strasz 2018232604Strasz be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift; 2019232604Strasz 2020232604Strasz /* 2021232604Strasz * The maximum LBA is the size - 1. 2022232604Strasz * 2023232604Strasz * XXX: Note that this field is being updated without locking, 2024232604Strasz * which might cause problems on 32-bit architectures. 2025232604Strasz */ 2026232604Strasz be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 2027232604Strasz ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 2028232604Strasz 2029232604Strasz /* Tell the user the exact size we ended up using */ 2030232604Strasz params->lun_size_bytes = be_lun->size_bytes; 2031232604Strasz 2032232604Strasz req->status = CTL_LUN_OK; 2033232604Strasz 2034232604Strasz return (0); 2035232604Strasz 2036232604Straszbailout_error: 2037232604Strasz req->status = CTL_LUN_ERROR; 2038232604Strasz 2039232604Strasz return (0); 2040232604Strasz} 2041232604Strasz 2042229997Skenstatic void 2043229997Skenctl_be_block_lun_shutdown(void *be_lun) 2044229997Sken{ 2045229997Sken struct ctl_be_block_lun *lun; 2046229997Sken struct ctl_be_block_softc *softc; 2047229997Sken 2048229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2049229997Sken 2050229997Sken softc = lun->softc; 2051229997Sken 2052229997Sken mtx_lock(&softc->lock); 2053229997Sken lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2054229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2055229997Sken wakeup(lun); 2056229997Sken mtx_unlock(&softc->lock); 2057229997Sken 2058229997Sken} 2059229997Sken 2060229997Skenstatic void 2061229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2062229997Sken{ 2063229997Sken struct ctl_be_block_lun *lun; 2064229997Sken struct ctl_be_block_softc *softc; 2065229997Sken 2066229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2067229997Sken softc = lun->softc; 2068229997Sken 2069229997Sken if (status == CTL_LUN_CONFIG_OK) { 2070229997Sken mtx_lock(&softc->lock); 2071229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2072229997Sken if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2073229997Sken wakeup(lun); 2074229997Sken mtx_unlock(&softc->lock); 2075229997Sken 2076229997Sken /* 2077229997Sken * We successfully added the LUN, attempt to enable it. 2078229997Sken */ 2079229997Sken if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 2080229997Sken printf("%s: ctl_enable_lun() failed!\n", __func__); 2081229997Sken if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 2082229997Sken printf("%s: ctl_invalidate_lun() failed!\n", 2083229997Sken __func__); 2084229997Sken } 2085229997Sken } 2086229997Sken 2087229997Sken return; 2088229997Sken } 2089229997Sken 2090229997Sken 2091229997Sken mtx_lock(&softc->lock); 2092229997Sken lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2093229997Sken lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2094229997Sken wakeup(lun); 2095229997Sken mtx_unlock(&softc->lock); 2096229997Sken} 2097229997Sken 2098229997Sken 2099229997Skenstatic int 2100229997Skenctl_be_block_config_write(union ctl_io *io) 2101229997Sken{ 2102229997Sken struct ctl_be_block_lun *be_lun; 2103229997Sken struct ctl_be_lun *ctl_be_lun; 2104229997Sken int retval; 2105229997Sken 2106229997Sken retval = 0; 2107229997Sken 2108229997Sken DPRINTF("entered\n"); 2109229997Sken 2110229997Sken ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2111229997Sken CTL_PRIV_BACKEND_LUN].ptr; 2112229997Sken be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun; 2113229997Sken 2114229997Sken switch (io->scsiio.cdb[0]) { 2115229997Sken case SYNCHRONIZE_CACHE: 2116229997Sken case SYNCHRONIZE_CACHE_16: 2117229997Sken /* 2118229997Sken * The upper level CTL code will filter out any CDBs with 2119229997Sken * the immediate bit set and return the proper error. 2120229997Sken * 2121229997Sken * We don't really need to worry about what LBA range the 2122229997Sken * user asked to be synced out. When they issue a sync 2123229997Sken * cache command, we'll sync out the whole thing. 2124229997Sken */ 2125229997Sken mtx_lock(&be_lun->lock); 2126229997Sken STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2127229997Sken links); 2128229997Sken mtx_unlock(&be_lun->lock); 2129229997Sken taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2130229997Sken break; 2131229997Sken case START_STOP_UNIT: { 2132229997Sken struct scsi_start_stop_unit *cdb; 2133229997Sken 2134229997Sken cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2135229997Sken 2136229997Sken if (cdb->how & SSS_START) 2137229997Sken retval = ctl_start_lun(ctl_be_lun); 2138229997Sken else { 2139229997Sken retval = ctl_stop_lun(ctl_be_lun); 2140229997Sken /* 2141229997Sken * XXX KDM Copan-specific offline behavior. 2142229997Sken * Figure out a reasonable way to port this? 2143229997Sken */ 2144229997Sken#ifdef NEEDTOPORT 2145229997Sken if ((retval == 0) 2146229997Sken && (cdb->byte2 & SSS_ONOFFLINE)) 2147229997Sken retval = ctl_lun_offline(ctl_be_lun); 2148229997Sken#endif 2149229997Sken } 2150229997Sken 2151229997Sken /* 2152229997Sken * In general, the above routines should not fail. They 2153229997Sken * just set state for the LUN. So we've got something 2154229997Sken * pretty wrong here if we can't start or stop the LUN. 2155229997Sken */ 2156229997Sken if (retval != 0) { 2157229997Sken ctl_set_internal_failure(&io->scsiio, 2158229997Sken /*sks_valid*/ 1, 2159229997Sken /*retry_count*/ 0xf051); 2160229997Sken retval = CTL_RETVAL_COMPLETE; 2161229997Sken } else { 2162229997Sken ctl_set_success(&io->scsiio); 2163229997Sken } 2164229997Sken ctl_config_write_done(io); 2165229997Sken break; 2166229997Sken } 2167229997Sken default: 2168229997Sken ctl_set_invalid_opcode(&io->scsiio); 2169229997Sken ctl_config_write_done(io); 2170229997Sken retval = CTL_RETVAL_COMPLETE; 2171229997Sken break; 2172229997Sken } 2173229997Sken 2174229997Sken return (retval); 2175229997Sken 2176229997Sken} 2177229997Sken 2178229997Skenstatic int 2179229997Skenctl_be_block_config_read(union ctl_io *io) 2180229997Sken{ 2181229997Sken return (0); 2182229997Sken} 2183229997Sken 2184229997Skenstatic int 2185229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2186229997Sken{ 2187229997Sken struct ctl_be_block_lun *lun; 2188229997Sken int retval; 2189229997Sken 2190229997Sken lun = (struct ctl_be_block_lun *)be_lun; 2191229997Sken retval = 0; 2192229997Sken 2193229997Sken retval = sbuf_printf(sb, "<num_threads>"); 2194229997Sken 2195229997Sken if (retval != 0) 2196229997Sken goto bailout; 2197229997Sken 2198229997Sken retval = sbuf_printf(sb, "%d", lun->num_threads); 2199229997Sken 2200229997Sken if (retval != 0) 2201229997Sken goto bailout; 2202229997Sken 2203229997Sken retval = sbuf_printf(sb, "</num_threads>"); 2204229997Sken 2205229997Sken /* 2206229997Sken * For processor devices, we don't have a path variable. 2207229997Sken */ 2208229997Sken if ((retval != 0) 2209229997Sken || (lun->dev_path == NULL)) 2210229997Sken goto bailout; 2211229997Sken 2212229997Sken retval = sbuf_printf(sb, "<file>"); 2213229997Sken 2214229997Sken if (retval != 0) 2215229997Sken goto bailout; 2216229997Sken 2217229997Sken retval = ctl_sbuf_printf_esc(sb, lun->dev_path); 2218229997Sken 2219229997Sken if (retval != 0) 2220229997Sken goto bailout; 2221229997Sken 2222229997Sken retval = sbuf_printf(sb, "</file>\n"); 2223229997Sken 2224229997Skenbailout: 2225229997Sken 2226229997Sken return (retval); 2227229997Sken} 2228229997Sken 2229229997Skenint 2230229997Skenctl_be_block_init(void) 2231229997Sken{ 2232229997Sken struct ctl_be_block_softc *softc; 2233229997Sken int retval; 2234229997Sken 2235229997Sken softc = &backend_block_softc; 2236229997Sken retval = 0; 2237229997Sken 2238229997Sken mtx_init(&softc->lock, "ctlblk", NULL, MTX_DEF); 2239264020Strasz beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2240264020Strasz NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2241229997Sken STAILQ_INIT(&softc->disk_list); 2242229997Sken STAILQ_INIT(&softc->lun_list); 2243229997Sken 2244229997Sken return (retval); 2245229997Sken} 2246