ctl_backend_block.c revision 287664
1/*- 2 * Copyright (c) 2003 Silicon Graphics International Corp. 3 * Copyright (c) 2009-2011 Spectra Logic Corporation 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $ 36 */ 37/* 38 * CAM Target Layer driver backend for block devices. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 287664 2015-09-11 12:50:52Z mav $"); 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/types.h> 49#include <sys/kthread.h> 50#include <sys/bio.h> 51#include <sys/fcntl.h> 52#include <sys/limits.h> 53#include <sys/lock.h> 54#include <sys/mutex.h> 55#include <sys/condvar.h> 56#include <sys/malloc.h> 57#include <sys/conf.h> 58#include <sys/ioccom.h> 59#include <sys/queue.h> 60#include <sys/sbuf.h> 61#include <sys/endian.h> 62#include <sys/uio.h> 63#include <sys/buf.h> 64#include <sys/taskqueue.h> 65#include <sys/vnode.h> 66#include <sys/namei.h> 67#include <sys/mount.h> 68#include <sys/disk.h> 69#include <sys/fcntl.h> 70#include <sys/filedesc.h> 71#include <sys/filio.h> 72#include <sys/proc.h> 73#include <sys/pcpu.h> 74#include <sys/module.h> 75#include <sys/sdt.h> 76#include <sys/devicestat.h> 77#include <sys/sysctl.h> 78 79#include <geom/geom.h> 80 81#include <cam/cam.h> 82#include <cam/scsi/scsi_all.h> 83#include <cam/scsi/scsi_da.h> 84#include <cam/ctl/ctl_io.h> 85#include <cam/ctl/ctl.h> 86#include <cam/ctl/ctl_backend.h> 87#include <cam/ctl/ctl_ioctl.h> 88#include <cam/ctl/ctl_ha.h> 89#include <cam/ctl/ctl_scsi_all.h> 90#include <cam/ctl/ctl_private.h> 91#include <cam/ctl/ctl_error.h> 92 93/* 94 * The idea here is that we'll allocate enough S/G space to hold a 1MB 95 * I/O. If we get an I/O larger than that, we'll split it. 96 */ 97#define CTLBLK_HALF_IO_SIZE (512 * 1024) 98#define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2) 99#define CTLBLK_MAX_SEG MAXPHYS 100#define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1) 101#define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2) 102 103#ifdef CTLBLK_DEBUG 104#define DPRINTF(fmt, args...) \ 105 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args) 106#else 107#define DPRINTF(fmt, args...) do {} while(0) 108#endif 109 110#define PRIV(io) \ 111 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 112#define ARGS(io) \ 113 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 114 115SDT_PROVIDER_DEFINE(cbb); 116 117typedef enum { 118 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01, 119 CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02, 120 CTL_BE_BLOCK_LUN_WAITING = 0x04, 121} ctl_be_block_lun_flags; 122 123typedef enum { 124 CTL_BE_BLOCK_NONE, 125 CTL_BE_BLOCK_DEV, 126 CTL_BE_BLOCK_FILE 127} ctl_be_block_type; 128 129struct ctl_be_block_filedata { 130 struct ucred *cred; 131}; 132 133union ctl_be_block_bedata { 134 struct ctl_be_block_filedata file; 135}; 136 137struct ctl_be_block_io; 138struct ctl_be_block_lun; 139 140typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun, 141 struct ctl_be_block_io *beio); 142typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun, 143 const char *attrname); 144 145/* 146 * Backend LUN structure. There is a 1:1 mapping between a block device 147 * and a backend block LUN, and between a backend block LUN and a CTL LUN. 148 */ 149struct ctl_be_block_lun { 150 struct ctl_lun_create_params params; 151 char lunname[32]; 152 char *dev_path; 153 ctl_be_block_type dev_type; 154 struct vnode *vn; 155 union ctl_be_block_bedata backend; 156 cbb_dispatch_t dispatch; 157 cbb_dispatch_t lun_flush; 158 cbb_dispatch_t unmap; 159 cbb_dispatch_t get_lba_status; 160 cbb_getattr_t getattr; 161 uma_zone_t lun_zone; 162 uint64_t size_blocks; 163 uint64_t size_bytes; 164 struct ctl_be_block_softc *softc; 165 struct devstat *disk_stats; 166 ctl_be_block_lun_flags flags; 167 STAILQ_ENTRY(ctl_be_block_lun) links; 168 struct ctl_be_lun cbe_lun; 169 struct taskqueue *io_taskqueue; 170 struct task io_task; 171 int num_threads; 172 STAILQ_HEAD(, ctl_io_hdr) input_queue; 173 STAILQ_HEAD(, ctl_io_hdr) config_read_queue; 174 STAILQ_HEAD(, ctl_io_hdr) config_write_queue; 175 STAILQ_HEAD(, ctl_io_hdr) datamove_queue; 176 struct mtx_padalign io_lock; 177 struct mtx_padalign queue_lock; 178}; 179 180/* 181 * Overall softc structure for the block backend module. 182 */ 183struct ctl_be_block_softc { 184 struct mtx lock; 185 int num_luns; 186 STAILQ_HEAD(, ctl_be_block_lun) lun_list; 187}; 188 189static struct ctl_be_block_softc backend_block_softc; 190 191/* 192 * Per-I/O information. 193 */ 194struct ctl_be_block_io { 195 union ctl_io *io; 196 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS]; 197 struct iovec xiovecs[CTLBLK_MAX_SEGS]; 198 int bio_cmd; 199 int num_segs; 200 int num_bios_sent; 201 int num_bios_done; 202 int send_complete; 203 int num_errors; 204 struct bintime ds_t0; 205 devstat_tag_type ds_tag_type; 206 devstat_trans_flags ds_trans_type; 207 uint64_t io_len; 208 uint64_t io_offset; 209 int io_arg; 210 struct ctl_be_block_softc *softc; 211 struct ctl_be_block_lun *lun; 212 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */ 213}; 214 215extern struct ctl_softc *control_softc; 216 217static int cbb_num_threads = 14; 218SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0, 219 "CAM Target Layer Block Backend"); 220SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN, 221 &cbb_num_threads, 0, "Number of threads per backing file"); 222 223static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc); 224static void ctl_free_beio(struct ctl_be_block_io *beio); 225static void ctl_complete_beio(struct ctl_be_block_io *beio); 226static int ctl_be_block_move_done(union ctl_io *io); 227static void ctl_be_block_biodone(struct bio *bio); 228static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 229 struct ctl_be_block_io *beio); 230static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 231 struct ctl_be_block_io *beio); 232static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 233 struct ctl_be_block_io *beio); 234static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, 235 const char *attrname); 236static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 237 struct ctl_be_block_io *beio); 238static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 239 struct ctl_be_block_io *beio); 240static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 241 struct ctl_be_block_io *beio); 242static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, 243 const char *attrname); 244static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 245 union ctl_io *io); 246static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 247 union ctl_io *io); 248static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 249 union ctl_io *io); 250static void ctl_be_block_worker(void *context, int pending); 251static int ctl_be_block_submit(union ctl_io *io); 252static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 253 int flag, struct thread *td); 254static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, 255 struct ctl_lun_req *req); 256static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, 257 struct ctl_lun_req *req); 258static int ctl_be_block_close(struct ctl_be_block_lun *be_lun); 259static int ctl_be_block_open(struct ctl_be_block_softc *softc, 260 struct ctl_be_block_lun *be_lun, 261 struct ctl_lun_req *req); 262static int ctl_be_block_create(struct ctl_be_block_softc *softc, 263 struct ctl_lun_req *req); 264static int ctl_be_block_rm(struct ctl_be_block_softc *softc, 265 struct ctl_lun_req *req); 266static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 267 struct ctl_lun_req *req); 268static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 269 struct ctl_lun_req *req); 270static int ctl_be_block_modify(struct ctl_be_block_softc *softc, 271 struct ctl_lun_req *req); 272static void ctl_be_block_lun_shutdown(void *be_lun); 273static void ctl_be_block_lun_config_status(void *be_lun, 274 ctl_lun_config_status status); 275static int ctl_be_block_config_write(union ctl_io *io); 276static int ctl_be_block_config_read(union ctl_io *io); 277static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb); 278static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname); 279int ctl_be_block_init(void); 280 281static struct ctl_backend_driver ctl_be_block_driver = 282{ 283 .name = "block", 284 .flags = CTL_BE_FLAG_HAS_CONFIG, 285 .init = ctl_be_block_init, 286 .data_submit = ctl_be_block_submit, 287 .data_move_done = ctl_be_block_move_done, 288 .config_read = ctl_be_block_config_read, 289 .config_write = ctl_be_block_config_write, 290 .ioctl = ctl_be_block_ioctl, 291 .lun_info = ctl_be_block_lun_info, 292 .lun_attr = ctl_be_block_lun_attr 293}; 294 295MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend"); 296CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver); 297 298static uma_zone_t beio_zone; 299 300static struct ctl_be_block_io * 301ctl_alloc_beio(struct ctl_be_block_softc *softc) 302{ 303 struct ctl_be_block_io *beio; 304 305 beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO); 306 beio->softc = softc; 307 return (beio); 308} 309 310static void 311ctl_free_beio(struct ctl_be_block_io *beio) 312{ 313 int duplicate_free; 314 int i; 315 316 duplicate_free = 0; 317 318 for (i = 0; i < beio->num_segs; i++) { 319 if (beio->sg_segs[i].addr == NULL) 320 duplicate_free++; 321 322 uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr); 323 beio->sg_segs[i].addr = NULL; 324 325 /* For compare we had two equal S/G lists. */ 326 if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) { 327 uma_zfree(beio->lun->lun_zone, 328 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr); 329 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL; 330 } 331 } 332 333 if (duplicate_free > 0) { 334 printf("%s: %d duplicate frees out of %d segments\n", __func__, 335 duplicate_free, beio->num_segs); 336 } 337 338 uma_zfree(beio_zone, beio); 339} 340 341static void 342ctl_complete_beio(struct ctl_be_block_io *beio) 343{ 344 union ctl_io *io = beio->io; 345 346 if (beio->beio_cont != NULL) { 347 beio->beio_cont(beio); 348 } else { 349 ctl_free_beio(beio); 350 ctl_data_submit_done(io); 351 } 352} 353 354static int 355ctl_be_block_move_done(union ctl_io *io) 356{ 357 struct ctl_be_block_io *beio; 358 struct ctl_be_block_lun *be_lun; 359 struct ctl_lba_len_flags *lbalen; 360#ifdef CTL_TIME_IO 361 struct bintime cur_bt; 362#endif 363 int i; 364 365 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 366 be_lun = beio->lun; 367 368 DPRINTF("entered\n"); 369 370#ifdef CTL_TIME_IO 371 getbintime(&cur_bt); 372 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 373 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 374 io->io_hdr.num_dmas++; 375#endif 376 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 377 378 /* 379 * We set status at this point for read commands, and write 380 * commands with errors. 381 */ 382 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 383 ; 384 } else if ((io->io_hdr.port_status == 0) && 385 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 386 lbalen = ARGS(beio->io); 387 if (lbalen->flags & CTL_LLF_READ) { 388 ctl_set_success(&io->scsiio); 389 } else if (lbalen->flags & CTL_LLF_COMPARE) { 390 /* We have two data blocks ready for comparison. */ 391 for (i = 0; i < beio->num_segs; i++) { 392 if (memcmp(beio->sg_segs[i].addr, 393 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr, 394 beio->sg_segs[i].len) != 0) 395 break; 396 } 397 if (i < beio->num_segs) 398 ctl_set_sense(&io->scsiio, 399 /*current_error*/ 1, 400 /*sense_key*/ SSD_KEY_MISCOMPARE, 401 /*asc*/ 0x1D, 402 /*ascq*/ 0x00, 403 SSD_ELEM_NONE); 404 else 405 ctl_set_success(&io->scsiio); 406 } 407 } else if ((io->io_hdr.port_status != 0) && 408 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 409 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 410 /* 411 * For hardware error sense keys, the sense key 412 * specific value is defined to be a retry count, 413 * but we use it to pass back an internal FETD 414 * error code. XXX KDM Hopefully the FETD is only 415 * using 16 bits for an error code, since that's 416 * all the space we have in the sks field. 417 */ 418 ctl_set_internal_failure(&io->scsiio, 419 /*sks_valid*/ 1, 420 /*retry_count*/ 421 io->io_hdr.port_status); 422 } 423 424 /* 425 * If this is a read, or a write with errors, it is done. 426 */ 427 if ((beio->bio_cmd == BIO_READ) 428 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0) 429 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) { 430 ctl_complete_beio(beio); 431 return (0); 432 } 433 434 /* 435 * At this point, we have a write and the DMA completed 436 * successfully. We now have to queue it to the task queue to 437 * execute the backend I/O. That is because we do blocking 438 * memory allocations, and in the file backing case, blocking I/O. 439 * This move done routine is generally called in the SIM's 440 * interrupt context, and therefore we cannot block. 441 */ 442 mtx_lock(&be_lun->queue_lock); 443 /* 444 * XXX KDM make sure that links is okay to use at this point. 445 * Otherwise, we either need to add another field to ctl_io_hdr, 446 * or deal with resource allocation here. 447 */ 448 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links); 449 mtx_unlock(&be_lun->queue_lock); 450 451 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 452 453 return (0); 454} 455 456static void 457ctl_be_block_biodone(struct bio *bio) 458{ 459 struct ctl_be_block_io *beio; 460 struct ctl_be_block_lun *be_lun; 461 union ctl_io *io; 462 int error; 463 464 beio = bio->bio_caller1; 465 be_lun = beio->lun; 466 io = beio->io; 467 468 DPRINTF("entered\n"); 469 470 error = bio->bio_error; 471 mtx_lock(&be_lun->io_lock); 472 if (error != 0) 473 beio->num_errors++; 474 475 beio->num_bios_done++; 476 477 /* 478 * XXX KDM will this cause WITNESS to complain? Holding a lock 479 * during the free might cause it to complain. 480 */ 481 g_destroy_bio(bio); 482 483 /* 484 * If the send complete bit isn't set, or we aren't the last I/O to 485 * complete, then we're done. 486 */ 487 if ((beio->send_complete == 0) 488 || (beio->num_bios_done < beio->num_bios_sent)) { 489 mtx_unlock(&be_lun->io_lock); 490 return; 491 } 492 493 /* 494 * At this point, we've verified that we are the last I/O to 495 * complete, so it's safe to drop the lock. 496 */ 497 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 498 beio->ds_tag_type, beio->ds_trans_type, 499 /*now*/ NULL, /*then*/&beio->ds_t0); 500 mtx_unlock(&be_lun->io_lock); 501 502 /* 503 * If there are any errors from the backing device, we fail the 504 * entire I/O with a medium error. 505 */ 506 if (beio->num_errors > 0) { 507 if (error == EOPNOTSUPP) { 508 ctl_set_invalid_opcode(&io->scsiio); 509 } else if (error == ENOSPC || error == EDQUOT) { 510 ctl_set_space_alloc_fail(&io->scsiio); 511 } else if (beio->bio_cmd == BIO_FLUSH) { 512 /* XXX KDM is there is a better error here? */ 513 ctl_set_internal_failure(&io->scsiio, 514 /*sks_valid*/ 1, 515 /*retry_count*/ 0xbad2); 516 } else 517 ctl_set_medium_error(&io->scsiio); 518 ctl_complete_beio(beio); 519 return; 520 } 521 522 /* 523 * If this is a write, a flush, a delete or verify, we're all done. 524 * If this is a read, we can now send the data to the user. 525 */ 526 if ((beio->bio_cmd == BIO_WRITE) 527 || (beio->bio_cmd == BIO_FLUSH) 528 || (beio->bio_cmd == BIO_DELETE) 529 || (ARGS(io)->flags & CTL_LLF_VERIFY)) { 530 ctl_set_success(&io->scsiio); 531 ctl_complete_beio(beio); 532 } else { 533 if ((ARGS(io)->flags & CTL_LLF_READ) && 534 beio->beio_cont == NULL) 535 ctl_set_success(&io->scsiio); 536#ifdef CTL_TIME_IO 537 getbintime(&io->io_hdr.dma_start_bt); 538#endif 539 ctl_datamove(io); 540 } 541} 542 543static void 544ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun, 545 struct ctl_be_block_io *beio) 546{ 547 union ctl_io *io = beio->io; 548 struct mount *mountpoint; 549 int error, lock_flags; 550 551 DPRINTF("entered\n"); 552 553 binuptime(&beio->ds_t0); 554 mtx_lock(&be_lun->io_lock); 555 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 556 mtx_unlock(&be_lun->io_lock); 557 558 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 559 560 if (MNT_SHARED_WRITES(mountpoint) 561 || ((mountpoint == NULL) 562 && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 563 lock_flags = LK_SHARED; 564 else 565 lock_flags = LK_EXCLUSIVE; 566 567 vn_lock(be_lun->vn, lock_flags | LK_RETRY); 568 569 error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT, 570 curthread); 571 VOP_UNLOCK(be_lun->vn, 0); 572 573 vn_finished_write(mountpoint); 574 575 mtx_lock(&be_lun->io_lock); 576 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 577 beio->ds_tag_type, beio->ds_trans_type, 578 /*now*/ NULL, /*then*/&beio->ds_t0); 579 mtx_unlock(&be_lun->io_lock); 580 581 if (error == 0) 582 ctl_set_success(&io->scsiio); 583 else { 584 /* XXX KDM is there is a better error here? */ 585 ctl_set_internal_failure(&io->scsiio, 586 /*sks_valid*/ 1, 587 /*retry_count*/ 0xbad1); 588 } 589 590 ctl_complete_beio(beio); 591} 592 593SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t"); 594SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t"); 595SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t"); 596SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t"); 597 598static void 599ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun, 600 struct ctl_be_block_io *beio) 601{ 602 struct ctl_be_block_filedata *file_data; 603 union ctl_io *io; 604 struct uio xuio; 605 struct iovec *xiovec; 606 int flags; 607 int error, i; 608 609 DPRINTF("entered\n"); 610 611 file_data = &be_lun->backend.file; 612 io = beio->io; 613 flags = 0; 614 if (ARGS(io)->flags & CTL_LLF_DPO) 615 flags |= IO_DIRECT; 616 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 617 flags |= IO_SYNC; 618 619 bzero(&xuio, sizeof(xuio)); 620 if (beio->bio_cmd == BIO_READ) { 621 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 622 xuio.uio_rw = UIO_READ; 623 } else { 624 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 625 xuio.uio_rw = UIO_WRITE; 626 } 627 xuio.uio_offset = beio->io_offset; 628 xuio.uio_resid = beio->io_len; 629 xuio.uio_segflg = UIO_SYSSPACE; 630 xuio.uio_iov = beio->xiovecs; 631 xuio.uio_iovcnt = beio->num_segs; 632 xuio.uio_td = curthread; 633 634 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 635 xiovec->iov_base = beio->sg_segs[i].addr; 636 xiovec->iov_len = beio->sg_segs[i].len; 637 } 638 639 binuptime(&beio->ds_t0); 640 mtx_lock(&be_lun->io_lock); 641 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 642 mtx_unlock(&be_lun->io_lock); 643 644 if (beio->bio_cmd == BIO_READ) { 645 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 646 647 /* 648 * UFS pays attention to IO_DIRECT for reads. If the 649 * DIRECTIO option is configured into the kernel, it calls 650 * ffs_rawread(). But that only works for single-segment 651 * uios with user space addresses. In our case, with a 652 * kernel uio, it still reads into the buffer cache, but it 653 * will just try to release the buffer from the cache later 654 * on in ffs_read(). 655 * 656 * ZFS does not pay attention to IO_DIRECT for reads. 657 * 658 * UFS does not pay attention to IO_SYNC for reads. 659 * 660 * ZFS pays attention to IO_SYNC (which translates into the 661 * Solaris define FRSYNC for zfs_read()) for reads. It 662 * attempts to sync the file before reading. 663 */ 664 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred); 665 666 VOP_UNLOCK(be_lun->vn, 0); 667 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 668 } else { 669 struct mount *mountpoint; 670 int lock_flags; 671 672 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT); 673 674 if (MNT_SHARED_WRITES(mountpoint) 675 || ((mountpoint == NULL) 676 && MNT_SHARED_WRITES(be_lun->vn->v_mount))) 677 lock_flags = LK_SHARED; 678 else 679 lock_flags = LK_EXCLUSIVE; 680 681 vn_lock(be_lun->vn, lock_flags | LK_RETRY); 682 683 /* 684 * UFS pays attention to IO_DIRECT for writes. The write 685 * is done asynchronously. (Normally the write would just 686 * get put into cache. 687 * 688 * UFS pays attention to IO_SYNC for writes. It will 689 * attempt to write the buffer out synchronously if that 690 * flag is set. 691 * 692 * ZFS does not pay attention to IO_DIRECT for writes. 693 * 694 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC) 695 * for writes. It will flush the transaction from the 696 * cache before returning. 697 */ 698 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred); 699 VOP_UNLOCK(be_lun->vn, 0); 700 701 vn_finished_write(mountpoint); 702 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 703 } 704 705 mtx_lock(&be_lun->io_lock); 706 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 707 beio->ds_tag_type, beio->ds_trans_type, 708 /*now*/ NULL, /*then*/&beio->ds_t0); 709 mtx_unlock(&be_lun->io_lock); 710 711 /* 712 * If we got an error, set the sense data to "MEDIUM ERROR" and 713 * return the I/O to the user. 714 */ 715 if (error != 0) { 716 char path_str[32]; 717 718 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 719 printf("%s%s command returned errno %d\n", path_str, 720 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error); 721 if (error == ENOSPC || error == EDQUOT) { 722 ctl_set_space_alloc_fail(&io->scsiio); 723 } else 724 ctl_set_medium_error(&io->scsiio); 725 ctl_complete_beio(beio); 726 return; 727 } 728 729 /* 730 * If this is a write or a verify, we're all done. 731 * If this is a read, we can now send the data to the user. 732 */ 733 if ((beio->bio_cmd == BIO_WRITE) || 734 (ARGS(io)->flags & CTL_LLF_VERIFY)) { 735 ctl_set_success(&io->scsiio); 736 ctl_complete_beio(beio); 737 } else { 738 if ((ARGS(io)->flags & CTL_LLF_READ) && 739 beio->beio_cont == NULL) 740 ctl_set_success(&io->scsiio); 741#ifdef CTL_TIME_IO 742 getbintime(&io->io_hdr.dma_start_bt); 743#endif 744 ctl_datamove(io); 745 } 746} 747 748static void 749ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun, 750 struct ctl_be_block_io *beio) 751{ 752 union ctl_io *io = beio->io; 753 struct ctl_lba_len_flags *lbalen = ARGS(io); 754 struct scsi_get_lba_status_data *data; 755 off_t roff, off; 756 int error, status; 757 758 DPRINTF("entered\n"); 759 760 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 761 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 762 error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off, 763 0, curthread->td_ucred, curthread); 764 if (error == 0 && off > roff) 765 status = 0; /* mapped up to off */ 766 else { 767 error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off, 768 0, curthread->td_ucred, curthread); 769 if (error == 0 && off > roff) 770 status = 1; /* deallocated up to off */ 771 else { 772 status = 0; /* unknown up to the end */ 773 off = be_lun->size_bytes; 774 } 775 } 776 VOP_UNLOCK(be_lun->vn, 0); 777 778 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 779 scsi_u64to8b(lbalen->lba, data->descr[0].addr); 780 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 781 lbalen->lba), data->descr[0].length); 782 data->descr[0].status = status; 783 784 ctl_complete_beio(beio); 785} 786 787static uint64_t 788ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname) 789{ 790 struct vattr vattr; 791 struct statfs statfs; 792 uint64_t val; 793 int error; 794 795 val = UINT64_MAX; 796 if (be_lun->vn == NULL) 797 return (val); 798 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 799 if (strcmp(attrname, "blocksused") == 0) { 800 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 801 if (error == 0) 802 val = vattr.va_bytes / be_lun->cbe_lun.blocksize; 803 } 804 if (strcmp(attrname, "blocksavail") == 0 && 805 (be_lun->vn->v_iflag & VI_DOOMED) == 0) { 806 error = VFS_STATFS(be_lun->vn->v_mount, &statfs); 807 if (error == 0) 808 val = statfs.f_bavail * statfs.f_bsize / 809 be_lun->cbe_lun.blocksize; 810 } 811 VOP_UNLOCK(be_lun->vn, 0); 812 return (val); 813} 814 815static void 816ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun, 817 struct ctl_be_block_io *beio) 818{ 819 union ctl_io *io; 820 struct cdevsw *csw; 821 struct cdev *dev; 822 struct uio xuio; 823 struct iovec *xiovec; 824 int error, flags, i, ref; 825 826 DPRINTF("entered\n"); 827 828 io = beio->io; 829 flags = 0; 830 if (ARGS(io)->flags & CTL_LLF_DPO) 831 flags |= IO_DIRECT; 832 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA) 833 flags |= IO_SYNC; 834 835 bzero(&xuio, sizeof(xuio)); 836 if (beio->bio_cmd == BIO_READ) { 837 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0); 838 xuio.uio_rw = UIO_READ; 839 } else { 840 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0); 841 xuio.uio_rw = UIO_WRITE; 842 } 843 xuio.uio_offset = beio->io_offset; 844 xuio.uio_resid = beio->io_len; 845 xuio.uio_segflg = UIO_SYSSPACE; 846 xuio.uio_iov = beio->xiovecs; 847 xuio.uio_iovcnt = beio->num_segs; 848 xuio.uio_td = curthread; 849 850 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) { 851 xiovec->iov_base = beio->sg_segs[i].addr; 852 xiovec->iov_len = beio->sg_segs[i].len; 853 } 854 855 binuptime(&beio->ds_t0); 856 mtx_lock(&be_lun->io_lock); 857 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0); 858 mtx_unlock(&be_lun->io_lock); 859 860 csw = devvn_refthread(be_lun->vn, &dev, &ref); 861 if (csw) { 862 if (beio->bio_cmd == BIO_READ) 863 error = csw->d_read(dev, &xuio, flags); 864 else 865 error = csw->d_write(dev, &xuio, flags); 866 dev_relthread(dev, ref); 867 } else 868 error = ENXIO; 869 870 if (beio->bio_cmd == BIO_READ) 871 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0); 872 else 873 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0); 874 875 mtx_lock(&be_lun->io_lock); 876 devstat_end_transaction(beio->lun->disk_stats, beio->io_len, 877 beio->ds_tag_type, beio->ds_trans_type, 878 /*now*/ NULL, /*then*/&beio->ds_t0); 879 mtx_unlock(&be_lun->io_lock); 880 881 /* 882 * If we got an error, set the sense data to "MEDIUM ERROR" and 883 * return the I/O to the user. 884 */ 885 if (error != 0) { 886 if (error == ENOSPC || error == EDQUOT) { 887 ctl_set_space_alloc_fail(&io->scsiio); 888 } else 889 ctl_set_medium_error(&io->scsiio); 890 ctl_complete_beio(beio); 891 return; 892 } 893 894 /* 895 * If this is a write or a verify, we're all done. 896 * If this is a read, we can now send the data to the user. 897 */ 898 if ((beio->bio_cmd == BIO_WRITE) || 899 (ARGS(io)->flags & CTL_LLF_VERIFY)) { 900 ctl_set_success(&io->scsiio); 901 ctl_complete_beio(beio); 902 } else { 903 if ((ARGS(io)->flags & CTL_LLF_READ) && 904 beio->beio_cont == NULL) 905 ctl_set_success(&io->scsiio); 906#ifdef CTL_TIME_IO 907 getbintime(&io->io_hdr.dma_start_bt); 908#endif 909 ctl_datamove(io); 910 } 911} 912 913static void 914ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun, 915 struct ctl_be_block_io *beio) 916{ 917 union ctl_io *io = beio->io; 918 struct cdevsw *csw; 919 struct cdev *dev; 920 struct ctl_lba_len_flags *lbalen = ARGS(io); 921 struct scsi_get_lba_status_data *data; 922 off_t roff, off; 923 int error, ref, status; 924 925 DPRINTF("entered\n"); 926 927 csw = devvn_refthread(be_lun->vn, &dev, &ref); 928 if (csw == NULL) { 929 status = 0; /* unknown up to the end */ 930 off = be_lun->size_bytes; 931 goto done; 932 } 933 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize; 934 error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD, 935 curthread); 936 if (error == 0 && off > roff) 937 status = 0; /* mapped up to off */ 938 else { 939 error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD, 940 curthread); 941 if (error == 0 && off > roff) 942 status = 1; /* deallocated up to off */ 943 else { 944 status = 0; /* unknown up to the end */ 945 off = be_lun->size_bytes; 946 } 947 } 948 dev_relthread(dev, ref); 949 950done: 951 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 952 scsi_u64to8b(lbalen->lba, data->descr[0].addr); 953 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize - 954 lbalen->lba), data->descr[0].length); 955 data->descr[0].status = status; 956 957 ctl_complete_beio(beio); 958} 959 960static void 961ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun, 962 struct ctl_be_block_io *beio) 963{ 964 struct bio *bio; 965 union ctl_io *io; 966 struct cdevsw *csw; 967 struct cdev *dev; 968 int ref; 969 970 io = beio->io; 971 972 DPRINTF("entered\n"); 973 974 /* This can't fail, it's a blocking allocation. */ 975 bio = g_alloc_bio(); 976 977 bio->bio_cmd = BIO_FLUSH; 978 bio->bio_offset = 0; 979 bio->bio_data = 0; 980 bio->bio_done = ctl_be_block_biodone; 981 bio->bio_caller1 = beio; 982 bio->bio_pblkno = 0; 983 984 /* 985 * We don't need to acquire the LUN lock here, because we are only 986 * sending one bio, and so there is no other context to synchronize 987 * with. 988 */ 989 beio->num_bios_sent = 1; 990 beio->send_complete = 1; 991 992 binuptime(&beio->ds_t0); 993 mtx_lock(&be_lun->io_lock); 994 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 995 mtx_unlock(&be_lun->io_lock); 996 997 csw = devvn_refthread(be_lun->vn, &dev, &ref); 998 if (csw) { 999 bio->bio_dev = dev; 1000 csw->d_strategy(bio); 1001 dev_relthread(dev, ref); 1002 } else { 1003 bio->bio_error = ENXIO; 1004 ctl_be_block_biodone(bio); 1005 } 1006} 1007 1008static void 1009ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun, 1010 struct ctl_be_block_io *beio, 1011 uint64_t off, uint64_t len, int last) 1012{ 1013 struct bio *bio; 1014 uint64_t maxlen; 1015 struct cdevsw *csw; 1016 struct cdev *dev; 1017 int ref; 1018 1019 csw = devvn_refthread(be_lun->vn, &dev, &ref); 1020 maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize); 1021 while (len > 0) { 1022 bio = g_alloc_bio(); 1023 bio->bio_cmd = BIO_DELETE; 1024 bio->bio_dev = dev; 1025 bio->bio_offset = off; 1026 bio->bio_length = MIN(len, maxlen); 1027 bio->bio_data = 0; 1028 bio->bio_done = ctl_be_block_biodone; 1029 bio->bio_caller1 = beio; 1030 bio->bio_pblkno = off / be_lun->cbe_lun.blocksize; 1031 1032 off += bio->bio_length; 1033 len -= bio->bio_length; 1034 1035 mtx_lock(&be_lun->io_lock); 1036 beio->num_bios_sent++; 1037 if (last && len == 0) 1038 beio->send_complete = 1; 1039 mtx_unlock(&be_lun->io_lock); 1040 1041 if (csw) { 1042 csw->d_strategy(bio); 1043 } else { 1044 bio->bio_error = ENXIO; 1045 ctl_be_block_biodone(bio); 1046 } 1047 } 1048 if (csw) 1049 dev_relthread(dev, ref); 1050} 1051 1052static void 1053ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun, 1054 struct ctl_be_block_io *beio) 1055{ 1056 union ctl_io *io; 1057 struct ctl_ptr_len_flags *ptrlen; 1058 struct scsi_unmap_desc *buf, *end; 1059 uint64_t len; 1060 1061 io = beio->io; 1062 1063 DPRINTF("entered\n"); 1064 1065 binuptime(&beio->ds_t0); 1066 mtx_lock(&be_lun->io_lock); 1067 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1068 mtx_unlock(&be_lun->io_lock); 1069 1070 if (beio->io_offset == -1) { 1071 beio->io_len = 0; 1072 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1073 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 1074 end = buf + ptrlen->len / sizeof(*buf); 1075 for (; buf < end; buf++) { 1076 len = (uint64_t)scsi_4btoul(buf->length) * 1077 be_lun->cbe_lun.blocksize; 1078 beio->io_len += len; 1079 ctl_be_block_unmap_dev_range(be_lun, beio, 1080 scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize, 1081 len, (end - buf < 2) ? TRUE : FALSE); 1082 } 1083 } else 1084 ctl_be_block_unmap_dev_range(be_lun, beio, 1085 beio->io_offset, beio->io_len, TRUE); 1086} 1087 1088static void 1089ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun, 1090 struct ctl_be_block_io *beio) 1091{ 1092 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue); 1093 struct bio *bio; 1094 struct cdevsw *csw; 1095 struct cdev *dev; 1096 off_t cur_offset; 1097 int i, max_iosize, ref; 1098 1099 DPRINTF("entered\n"); 1100 csw = devvn_refthread(be_lun->vn, &dev, &ref); 1101 1102 /* 1103 * We have to limit our I/O size to the maximum supported by the 1104 * backend device. Hopefully it is MAXPHYS. If the driver doesn't 1105 * set it properly, use DFLTPHYS. 1106 */ 1107 if (csw) { 1108 max_iosize = dev->si_iosize_max; 1109 if (max_iosize < PAGE_SIZE) 1110 max_iosize = DFLTPHYS; 1111 } else 1112 max_iosize = DFLTPHYS; 1113 1114 cur_offset = beio->io_offset; 1115 for (i = 0; i < beio->num_segs; i++) { 1116 size_t cur_size; 1117 uint8_t *cur_ptr; 1118 1119 cur_size = beio->sg_segs[i].len; 1120 cur_ptr = beio->sg_segs[i].addr; 1121 1122 while (cur_size > 0) { 1123 /* This can't fail, it's a blocking allocation. */ 1124 bio = g_alloc_bio(); 1125 1126 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n")); 1127 1128 bio->bio_cmd = beio->bio_cmd; 1129 bio->bio_dev = dev; 1130 bio->bio_caller1 = beio; 1131 bio->bio_length = min(cur_size, max_iosize); 1132 bio->bio_offset = cur_offset; 1133 bio->bio_data = cur_ptr; 1134 bio->bio_done = ctl_be_block_biodone; 1135 bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize; 1136 1137 cur_offset += bio->bio_length; 1138 cur_ptr += bio->bio_length; 1139 cur_size -= bio->bio_length; 1140 1141 TAILQ_INSERT_TAIL(&queue, bio, bio_queue); 1142 beio->num_bios_sent++; 1143 } 1144 } 1145 binuptime(&beio->ds_t0); 1146 mtx_lock(&be_lun->io_lock); 1147 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0); 1148 beio->send_complete = 1; 1149 mtx_unlock(&be_lun->io_lock); 1150 1151 /* 1152 * Fire off all allocated requests! 1153 */ 1154 while ((bio = TAILQ_FIRST(&queue)) != NULL) { 1155 TAILQ_REMOVE(&queue, bio, bio_queue); 1156 if (csw) 1157 csw->d_strategy(bio); 1158 else { 1159 bio->bio_error = ENXIO; 1160 ctl_be_block_biodone(bio); 1161 } 1162 } 1163 if (csw) 1164 dev_relthread(dev, ref); 1165} 1166 1167static uint64_t 1168ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname) 1169{ 1170 struct diocgattr_arg arg; 1171 struct cdevsw *csw; 1172 struct cdev *dev; 1173 int error, ref; 1174 1175 csw = devvn_refthread(be_lun->vn, &dev, &ref); 1176 if (csw == NULL) 1177 return (UINT64_MAX); 1178 strlcpy(arg.name, attrname, sizeof(arg.name)); 1179 arg.len = sizeof(arg.value.off); 1180 if (csw->d_ioctl) { 1181 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 1182 curthread); 1183 } else 1184 error = ENODEV; 1185 dev_relthread(dev, ref); 1186 if (error != 0) 1187 return (UINT64_MAX); 1188 return (arg.value.off); 1189} 1190 1191static void 1192ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun, 1193 union ctl_io *io) 1194{ 1195 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1196 struct ctl_be_block_io *beio; 1197 struct ctl_lba_len_flags *lbalen; 1198 1199 DPRINTF("entered\n"); 1200 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1201 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1202 1203 beio->io_len = lbalen->len * cbe_lun->blocksize; 1204 beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1205 beio->io_arg = (lbalen->flags & SSC_IMMED) != 0; 1206 beio->bio_cmd = BIO_FLUSH; 1207 beio->ds_trans_type = DEVSTAT_NO_DATA; 1208 DPRINTF("SYNC\n"); 1209 be_lun->lun_flush(be_lun, beio); 1210} 1211 1212static void 1213ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio) 1214{ 1215 union ctl_io *io; 1216 1217 io = beio->io; 1218 ctl_free_beio(beio); 1219 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1220 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1221 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1222 ctl_config_write_done(io); 1223 return; 1224 } 1225 1226 ctl_be_block_config_write(io); 1227} 1228 1229static void 1230ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun, 1231 union ctl_io *io) 1232{ 1233 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1234 struct ctl_be_block_io *beio; 1235 struct ctl_lba_len_flags *lbalen; 1236 uint64_t len_left, lba; 1237 uint32_t pb, pbo, adj; 1238 int i, seglen; 1239 uint8_t *buf, *end; 1240 1241 DPRINTF("entered\n"); 1242 1243 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1244 lbalen = ARGS(beio->io); 1245 1246 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) || 1247 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) { 1248 ctl_free_beio(beio); 1249 ctl_set_invalid_field(&io->scsiio, 1250 /*sks_valid*/ 1, 1251 /*command*/ 1, 1252 /*field*/ 1, 1253 /*bit_valid*/ 0, 1254 /*bit*/ 0); 1255 ctl_config_write_done(io); 1256 return; 1257 } 1258 1259 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) { 1260 beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1261 beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize; 1262 beio->bio_cmd = BIO_DELETE; 1263 beio->ds_trans_type = DEVSTAT_FREE; 1264 1265 be_lun->unmap(be_lun, beio); 1266 return; 1267 } 1268 1269 beio->bio_cmd = BIO_WRITE; 1270 beio->ds_trans_type = DEVSTAT_WRITE; 1271 1272 DPRINTF("WRITE SAME at LBA %jx len %u\n", 1273 (uintmax_t)lbalen->lba, lbalen->len); 1274 1275 pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp; 1276 if (be_lun->cbe_lun.pblockoff > 0) 1277 pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff; 1278 else 1279 pbo = 0; 1280 len_left = (uint64_t)lbalen->len * cbe_lun->blocksize; 1281 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) { 1282 1283 /* 1284 * Setup the S/G entry for this chunk. 1285 */ 1286 seglen = MIN(CTLBLK_MAX_SEG, len_left); 1287 if (pb > cbe_lun->blocksize) { 1288 adj = ((lbalen->lba + lba) * cbe_lun->blocksize + 1289 seglen - pbo) % pb; 1290 if (seglen > adj) 1291 seglen -= adj; 1292 else 1293 seglen -= seglen % cbe_lun->blocksize; 1294 } else 1295 seglen -= seglen % cbe_lun->blocksize; 1296 beio->sg_segs[i].len = seglen; 1297 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1298 1299 DPRINTF("segment %d addr %p len %zd\n", i, 1300 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1301 1302 beio->num_segs++; 1303 len_left -= seglen; 1304 1305 buf = beio->sg_segs[i].addr; 1306 end = buf + seglen; 1307 for (; buf < end; buf += cbe_lun->blocksize) { 1308 memcpy(buf, io->scsiio.kern_data_ptr, cbe_lun->blocksize); 1309 if (lbalen->flags & SWS_LBDATA) 1310 scsi_ulto4b(lbalen->lba + lba, buf); 1311 lba++; 1312 } 1313 } 1314 1315 beio->io_offset = lbalen->lba * cbe_lun->blocksize; 1316 beio->io_len = lba * cbe_lun->blocksize; 1317 1318 /* We can not do all in one run. Correct and schedule rerun. */ 1319 if (len_left > 0) { 1320 lbalen->lba += lba; 1321 lbalen->len -= lba; 1322 beio->beio_cont = ctl_be_block_cw_done_ws; 1323 } 1324 1325 be_lun->dispatch(be_lun, beio); 1326} 1327 1328static void 1329ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun, 1330 union ctl_io *io) 1331{ 1332 struct ctl_be_block_io *beio; 1333 struct ctl_ptr_len_flags *ptrlen; 1334 1335 DPRINTF("entered\n"); 1336 1337 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1338 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 1339 1340 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) { 1341 ctl_free_beio(beio); 1342 ctl_set_invalid_field(&io->scsiio, 1343 /*sks_valid*/ 0, 1344 /*command*/ 1, 1345 /*field*/ 0, 1346 /*bit_valid*/ 0, 1347 /*bit*/ 0); 1348 ctl_config_write_done(io); 1349 return; 1350 } 1351 1352 beio->io_len = 0; 1353 beio->io_offset = -1; 1354 beio->bio_cmd = BIO_DELETE; 1355 beio->ds_trans_type = DEVSTAT_FREE; 1356 DPRINTF("UNMAP\n"); 1357 be_lun->unmap(be_lun, beio); 1358} 1359 1360static void 1361ctl_be_block_cr_done(struct ctl_be_block_io *beio) 1362{ 1363 union ctl_io *io; 1364 1365 io = beio->io; 1366 ctl_free_beio(beio); 1367 ctl_config_read_done(io); 1368} 1369 1370static void 1371ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun, 1372 union ctl_io *io) 1373{ 1374 struct ctl_be_block_io *beio; 1375 struct ctl_be_block_softc *softc; 1376 1377 DPRINTF("entered\n"); 1378 1379 softc = be_lun->softc; 1380 beio = ctl_alloc_beio(softc); 1381 beio->io = io; 1382 beio->lun = be_lun; 1383 beio->beio_cont = ctl_be_block_cr_done; 1384 PRIV(io)->ptr = (void *)beio; 1385 1386 switch (io->scsiio.cdb[0]) { 1387 case SERVICE_ACTION_IN: /* GET LBA STATUS */ 1388 beio->bio_cmd = -1; 1389 beio->ds_trans_type = DEVSTAT_NO_DATA; 1390 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1391 beio->io_len = 0; 1392 if (be_lun->get_lba_status) 1393 be_lun->get_lba_status(be_lun, beio); 1394 else 1395 ctl_be_block_cr_done(beio); 1396 break; 1397 default: 1398 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1399 break; 1400 } 1401} 1402 1403static void 1404ctl_be_block_cw_done(struct ctl_be_block_io *beio) 1405{ 1406 union ctl_io *io; 1407 1408 io = beio->io; 1409 ctl_free_beio(beio); 1410 ctl_config_write_done(io); 1411} 1412 1413static void 1414ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun, 1415 union ctl_io *io) 1416{ 1417 struct ctl_be_block_io *beio; 1418 struct ctl_be_block_softc *softc; 1419 1420 DPRINTF("entered\n"); 1421 1422 softc = be_lun->softc; 1423 beio = ctl_alloc_beio(softc); 1424 beio->io = io; 1425 beio->lun = be_lun; 1426 beio->beio_cont = ctl_be_block_cw_done; 1427 switch (io->scsiio.tag_type) { 1428 case CTL_TAG_ORDERED: 1429 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1430 break; 1431 case CTL_TAG_HEAD_OF_QUEUE: 1432 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1433 break; 1434 case CTL_TAG_UNTAGGED: 1435 case CTL_TAG_SIMPLE: 1436 case CTL_TAG_ACA: 1437 default: 1438 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1439 break; 1440 } 1441 PRIV(io)->ptr = (void *)beio; 1442 1443 switch (io->scsiio.cdb[0]) { 1444 case SYNCHRONIZE_CACHE: 1445 case SYNCHRONIZE_CACHE_16: 1446 ctl_be_block_cw_dispatch_sync(be_lun, io); 1447 break; 1448 case WRITE_SAME_10: 1449 case WRITE_SAME_16: 1450 ctl_be_block_cw_dispatch_ws(be_lun, io); 1451 break; 1452 case UNMAP: 1453 ctl_be_block_cw_dispatch_unmap(be_lun, io); 1454 break; 1455 default: 1456 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]); 1457 break; 1458 } 1459} 1460 1461SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t"); 1462SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t"); 1463SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t"); 1464SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t"); 1465 1466static void 1467ctl_be_block_next(struct ctl_be_block_io *beio) 1468{ 1469 struct ctl_be_block_lun *be_lun; 1470 union ctl_io *io; 1471 1472 io = beio->io; 1473 be_lun = beio->lun; 1474 ctl_free_beio(beio); 1475 if ((io->io_hdr.flags & CTL_FLAG_ABORT) || 1476 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 1477 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 1478 ctl_data_submit_done(io); 1479 return; 1480 } 1481 1482 io->io_hdr.status &= ~CTL_STATUS_MASK; 1483 io->io_hdr.status |= CTL_STATUS_NONE; 1484 1485 mtx_lock(&be_lun->queue_lock); 1486 /* 1487 * XXX KDM make sure that links is okay to use at this point. 1488 * Otherwise, we either need to add another field to ctl_io_hdr, 1489 * or deal with resource allocation here. 1490 */ 1491 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1492 mtx_unlock(&be_lun->queue_lock); 1493 1494 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1495} 1496 1497static void 1498ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun, 1499 union ctl_io *io) 1500{ 1501 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1502 struct ctl_be_block_io *beio; 1503 struct ctl_be_block_softc *softc; 1504 struct ctl_lba_len_flags *lbalen; 1505 struct ctl_ptr_len_flags *bptrlen; 1506 uint64_t len_left, lbas; 1507 int i; 1508 1509 softc = be_lun->softc; 1510 1511 DPRINTF("entered\n"); 1512 1513 lbalen = ARGS(io); 1514 if (lbalen->flags & CTL_LLF_WRITE) { 1515 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0); 1516 } else { 1517 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0); 1518 } 1519 1520 beio = ctl_alloc_beio(softc); 1521 beio->io = io; 1522 beio->lun = be_lun; 1523 bptrlen = PRIV(io); 1524 bptrlen->ptr = (void *)beio; 1525 1526 switch (io->scsiio.tag_type) { 1527 case CTL_TAG_ORDERED: 1528 beio->ds_tag_type = DEVSTAT_TAG_ORDERED; 1529 break; 1530 case CTL_TAG_HEAD_OF_QUEUE: 1531 beio->ds_tag_type = DEVSTAT_TAG_HEAD; 1532 break; 1533 case CTL_TAG_UNTAGGED: 1534 case CTL_TAG_SIMPLE: 1535 case CTL_TAG_ACA: 1536 default: 1537 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE; 1538 break; 1539 } 1540 1541 if (lbalen->flags & CTL_LLF_WRITE) { 1542 beio->bio_cmd = BIO_WRITE; 1543 beio->ds_trans_type = DEVSTAT_WRITE; 1544 } else { 1545 beio->bio_cmd = BIO_READ; 1546 beio->ds_trans_type = DEVSTAT_READ; 1547 } 1548 1549 DPRINTF("%s at LBA %jx len %u @%ju\n", 1550 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", 1551 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len); 1552 if (lbalen->flags & CTL_LLF_COMPARE) 1553 lbas = CTLBLK_HALF_IO_SIZE; 1554 else 1555 lbas = CTLBLK_MAX_IO_SIZE; 1556 lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize); 1557 beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize; 1558 beio->io_len = lbas * cbe_lun->blocksize; 1559 bptrlen->len += lbas; 1560 1561 for (i = 0, len_left = beio->io_len; len_left > 0; i++) { 1562 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)", 1563 i, CTLBLK_MAX_SEGS)); 1564 1565 /* 1566 * Setup the S/G entry for this chunk. 1567 */ 1568 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left); 1569 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK); 1570 1571 DPRINTF("segment %d addr %p len %zd\n", i, 1572 beio->sg_segs[i].addr, beio->sg_segs[i].len); 1573 1574 /* Set up second segment for compare operation. */ 1575 if (lbalen->flags & CTL_LLF_COMPARE) { 1576 beio->sg_segs[i + CTLBLK_HALF_SEGS].len = 1577 beio->sg_segs[i].len; 1578 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = 1579 uma_zalloc(be_lun->lun_zone, M_WAITOK); 1580 } 1581 1582 beio->num_segs++; 1583 len_left -= beio->sg_segs[i].len; 1584 } 1585 if (bptrlen->len < lbalen->len) 1586 beio->beio_cont = ctl_be_block_next; 1587 io->scsiio.be_move_done = ctl_be_block_move_done; 1588 /* For compare we have separate S/G lists for read and datamove. */ 1589 if (lbalen->flags & CTL_LLF_COMPARE) 1590 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS]; 1591 else 1592 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs; 1593 io->scsiio.kern_data_len = beio->io_len; 1594 io->scsiio.kern_data_resid = 0; 1595 io->scsiio.kern_sg_entries = beio->num_segs; 1596 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST; 1597 1598 /* 1599 * For the read case, we need to read the data into our buffers and 1600 * then we can send it back to the user. For the write case, we 1601 * need to get the data from the user first. 1602 */ 1603 if (beio->bio_cmd == BIO_READ) { 1604 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0); 1605 be_lun->dispatch(be_lun, beio); 1606 } else { 1607 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0); 1608#ifdef CTL_TIME_IO 1609 getbintime(&io->io_hdr.dma_start_bt); 1610#endif 1611 ctl_datamove(io); 1612 } 1613} 1614 1615static void 1616ctl_be_block_worker(void *context, int pending) 1617{ 1618 struct ctl_be_block_lun *be_lun; 1619 struct ctl_be_block_softc *softc; 1620 union ctl_io *io; 1621 1622 be_lun = (struct ctl_be_block_lun *)context; 1623 softc = be_lun->softc; 1624 1625 DPRINTF("entered\n"); 1626 1627 mtx_lock(&be_lun->queue_lock); 1628 for (;;) { 1629 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue); 1630 if (io != NULL) { 1631 struct ctl_be_block_io *beio; 1632 1633 DPRINTF("datamove queue\n"); 1634 1635 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr, 1636 ctl_io_hdr, links); 1637 1638 mtx_unlock(&be_lun->queue_lock); 1639 1640 beio = (struct ctl_be_block_io *)PRIV(io)->ptr; 1641 1642 be_lun->dispatch(be_lun, beio); 1643 1644 mtx_lock(&be_lun->queue_lock); 1645 continue; 1646 } 1647 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue); 1648 if (io != NULL) { 1649 DPRINTF("config write queue\n"); 1650 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr, 1651 ctl_io_hdr, links); 1652 mtx_unlock(&be_lun->queue_lock); 1653 ctl_be_block_cw_dispatch(be_lun, io); 1654 mtx_lock(&be_lun->queue_lock); 1655 continue; 1656 } 1657 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue); 1658 if (io != NULL) { 1659 DPRINTF("config read queue\n"); 1660 STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr, 1661 ctl_io_hdr, links); 1662 mtx_unlock(&be_lun->queue_lock); 1663 ctl_be_block_cr_dispatch(be_lun, io); 1664 mtx_lock(&be_lun->queue_lock); 1665 continue; 1666 } 1667 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue); 1668 if (io != NULL) { 1669 DPRINTF("input queue\n"); 1670 1671 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr, 1672 ctl_io_hdr, links); 1673 mtx_unlock(&be_lun->queue_lock); 1674 1675 /* 1676 * We must drop the lock, since this routine and 1677 * its children may sleep. 1678 */ 1679 ctl_be_block_dispatch(be_lun, io); 1680 1681 mtx_lock(&be_lun->queue_lock); 1682 continue; 1683 } 1684 1685 /* 1686 * If we get here, there is no work left in the queues, so 1687 * just break out and let the task queue go to sleep. 1688 */ 1689 break; 1690 } 1691 mtx_unlock(&be_lun->queue_lock); 1692} 1693 1694/* 1695 * Entry point from CTL to the backend for I/O. We queue everything to a 1696 * work thread, so this just puts the I/O on a queue and wakes up the 1697 * thread. 1698 */ 1699static int 1700ctl_be_block_submit(union ctl_io *io) 1701{ 1702 struct ctl_be_block_lun *be_lun; 1703 struct ctl_be_lun *cbe_lun; 1704 1705 DPRINTF("entered\n"); 1706 1707 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 1708 CTL_PRIV_BACKEND_LUN].ptr; 1709 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 1710 1711 /* 1712 * Make sure we only get SCSI I/O. 1713 */ 1714 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type " 1715 "%#x) encountered", io->io_hdr.io_type)); 1716 1717 PRIV(io)->len = 0; 1718 1719 mtx_lock(&be_lun->queue_lock); 1720 /* 1721 * XXX KDM make sure that links is okay to use at this point. 1722 * Otherwise, we either need to add another field to ctl_io_hdr, 1723 * or deal with resource allocation here. 1724 */ 1725 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links); 1726 mtx_unlock(&be_lun->queue_lock); 1727 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 1728 1729 return (CTL_RETVAL_COMPLETE); 1730} 1731 1732static int 1733ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 1734 int flag, struct thread *td) 1735{ 1736 struct ctl_be_block_softc *softc; 1737 int error; 1738 1739 softc = &backend_block_softc; 1740 1741 error = 0; 1742 1743 switch (cmd) { 1744 case CTL_LUN_REQ: { 1745 struct ctl_lun_req *lun_req; 1746 1747 lun_req = (struct ctl_lun_req *)addr; 1748 1749 switch (lun_req->reqtype) { 1750 case CTL_LUNREQ_CREATE: 1751 error = ctl_be_block_create(softc, lun_req); 1752 break; 1753 case CTL_LUNREQ_RM: 1754 error = ctl_be_block_rm(softc, lun_req); 1755 break; 1756 case CTL_LUNREQ_MODIFY: 1757 error = ctl_be_block_modify(softc, lun_req); 1758 break; 1759 default: 1760 lun_req->status = CTL_LUN_ERROR; 1761 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 1762 "invalid LUN request type %d", 1763 lun_req->reqtype); 1764 break; 1765 } 1766 break; 1767 } 1768 default: 1769 error = ENOTTY; 1770 break; 1771 } 1772 1773 return (error); 1774} 1775 1776static int 1777ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1778{ 1779 struct ctl_be_lun *cbe_lun; 1780 struct ctl_be_block_filedata *file_data; 1781 struct ctl_lun_create_params *params; 1782 char *value; 1783 struct vattr vattr; 1784 off_t ps, pss, po, pos, us, uss, uo, uos; 1785 int error; 1786 1787 error = 0; 1788 cbe_lun = &be_lun->cbe_lun; 1789 file_data = &be_lun->backend.file; 1790 params = &be_lun->params; 1791 1792 be_lun->dev_type = CTL_BE_BLOCK_FILE; 1793 be_lun->dispatch = ctl_be_block_dispatch_file; 1794 be_lun->lun_flush = ctl_be_block_flush_file; 1795 be_lun->get_lba_status = ctl_be_block_gls_file; 1796 be_lun->getattr = ctl_be_block_getattr_file; 1797 be_lun->unmap = NULL; 1798 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 1799 1800 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 1801 if (error != 0) { 1802 snprintf(req->error_str, sizeof(req->error_str), 1803 "error calling VOP_GETATTR() for file %s", 1804 be_lun->dev_path); 1805 return (error); 1806 } 1807 1808 /* 1809 * Verify that we have the ability to upgrade to exclusive 1810 * access on this file so we can trap errors at open instead 1811 * of reporting them during first access. 1812 */ 1813 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) { 1814 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY); 1815 if (be_lun->vn->v_iflag & VI_DOOMED) { 1816 error = EBADF; 1817 snprintf(req->error_str, sizeof(req->error_str), 1818 "error locking file %s", be_lun->dev_path); 1819 return (error); 1820 } 1821 } 1822 1823 file_data->cred = crhold(curthread->td_ucred); 1824 if (params->lun_size_bytes != 0) 1825 be_lun->size_bytes = params->lun_size_bytes; 1826 else 1827 be_lun->size_bytes = vattr.va_size; 1828 1829 /* 1830 * For files we can use any logical block size. Prefer 512 bytes 1831 * for compatibility reasons. If file's vattr.va_blocksize 1832 * (preferred I/O block size) is bigger and multiple to chosen 1833 * logical block size -- report it as physical block size. 1834 */ 1835 if (params->blocksize_bytes != 0) 1836 cbe_lun->blocksize = params->blocksize_bytes; 1837 else 1838 cbe_lun->blocksize = 512; 1839 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1840 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1841 0 : (be_lun->size_blocks - 1); 1842 1843 us = ps = vattr.va_blocksize; 1844 uo = po = 0; 1845 1846 value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 1847 if (value != NULL) 1848 ctl_expand_number(value, &ps); 1849 value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 1850 if (value != NULL) 1851 ctl_expand_number(value, &po); 1852 pss = ps / cbe_lun->blocksize; 1853 pos = po / cbe_lun->blocksize; 1854 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 1855 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 1856 cbe_lun->pblockexp = fls(pss) - 1; 1857 cbe_lun->pblockoff = (pss - pos) % pss; 1858 } 1859 1860 value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 1861 if (value != NULL) 1862 ctl_expand_number(value, &us); 1863 value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 1864 if (value != NULL) 1865 ctl_expand_number(value, &uo); 1866 uss = us / cbe_lun->blocksize; 1867 uos = uo / cbe_lun->blocksize; 1868 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 1869 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 1870 cbe_lun->ublockexp = fls(uss) - 1; 1871 cbe_lun->ublockoff = (uss - uos) % uss; 1872 } 1873 1874 /* 1875 * Sanity check. The media size has to be at least one 1876 * sector long. 1877 */ 1878 if (be_lun->size_bytes < cbe_lun->blocksize) { 1879 error = EINVAL; 1880 snprintf(req->error_str, sizeof(req->error_str), 1881 "file %s size %ju < block size %u", be_lun->dev_path, 1882 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize); 1883 } 1884 1885 cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize; 1886 return (error); 1887} 1888 1889static int 1890ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 1891{ 1892 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 1893 struct ctl_lun_create_params *params; 1894 struct cdevsw *csw; 1895 struct cdev *dev; 1896 char *value; 1897 int error, atomic, maxio, ref, unmap, tmp; 1898 off_t ps, pss, po, pos, us, uss, uo, uos, otmp; 1899 1900 params = &be_lun->params; 1901 1902 be_lun->dev_type = CTL_BE_BLOCK_DEV; 1903 csw = devvn_refthread(be_lun->vn, &dev, &ref); 1904 if (csw == NULL) 1905 return (ENXIO); 1906 if (strcmp(csw->d_name, "zvol") == 0) { 1907 be_lun->dispatch = ctl_be_block_dispatch_zvol; 1908 be_lun->get_lba_status = ctl_be_block_gls_zvol; 1909 atomic = maxio = CTLBLK_MAX_IO_SIZE; 1910 } else { 1911 be_lun->dispatch = ctl_be_block_dispatch_dev; 1912 be_lun->get_lba_status = NULL; 1913 atomic = 0; 1914 maxio = dev->si_iosize_max; 1915 if (maxio <= 0) 1916 maxio = DFLTPHYS; 1917 if (maxio > CTLBLK_MAX_IO_SIZE) 1918 maxio = CTLBLK_MAX_IO_SIZE; 1919 } 1920 be_lun->lun_flush = ctl_be_block_flush_dev; 1921 be_lun->getattr = ctl_be_block_getattr_dev; 1922 be_lun->unmap = ctl_be_block_unmap_dev; 1923 1924 if (!csw->d_ioctl) { 1925 dev_relthread(dev, ref); 1926 snprintf(req->error_str, sizeof(req->error_str), 1927 "no d_ioctl for device %s!", be_lun->dev_path); 1928 return (ENODEV); 1929 } 1930 1931 error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD, 1932 curthread); 1933 if (error) { 1934 dev_relthread(dev, ref); 1935 snprintf(req->error_str, sizeof(req->error_str), 1936 "error %d returned for DIOCGSECTORSIZE ioctl " 1937 "on %s!", error, be_lun->dev_path); 1938 return (error); 1939 } 1940 1941 /* 1942 * If the user has asked for a blocksize that is greater than the 1943 * backing device's blocksize, we can do it only if the blocksize 1944 * the user is asking for is an even multiple of the underlying 1945 * device's blocksize. 1946 */ 1947 if ((params->blocksize_bytes != 0) && 1948 (params->blocksize_bytes >= tmp)) { 1949 if (params->blocksize_bytes % tmp == 0) { 1950 cbe_lun->blocksize = params->blocksize_bytes; 1951 } else { 1952 dev_relthread(dev, ref); 1953 snprintf(req->error_str, sizeof(req->error_str), 1954 "requested blocksize %u is not an even " 1955 "multiple of backing device blocksize %u", 1956 params->blocksize_bytes, tmp); 1957 return (EINVAL); 1958 } 1959 } else if (params->blocksize_bytes != 0) { 1960 dev_relthread(dev, ref); 1961 snprintf(req->error_str, sizeof(req->error_str), 1962 "requested blocksize %u < backing device " 1963 "blocksize %u", params->blocksize_bytes, tmp); 1964 return (EINVAL); 1965 } else 1966 cbe_lun->blocksize = tmp; 1967 1968 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD, 1969 curthread); 1970 if (error) { 1971 dev_relthread(dev, ref); 1972 snprintf(req->error_str, sizeof(req->error_str), 1973 "error %d returned for DIOCGMEDIASIZE " 1974 " ioctl on %s!", error, 1975 be_lun->dev_path); 1976 return (error); 1977 } 1978 1979 if (params->lun_size_bytes != 0) { 1980 if (params->lun_size_bytes > otmp) { 1981 dev_relthread(dev, ref); 1982 snprintf(req->error_str, sizeof(req->error_str), 1983 "requested LUN size %ju > backing device " 1984 "size %ju", 1985 (uintmax_t)params->lun_size_bytes, 1986 (uintmax_t)otmp); 1987 return (EINVAL); 1988 } 1989 1990 be_lun->size_bytes = params->lun_size_bytes; 1991 } else 1992 be_lun->size_bytes = otmp; 1993 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 1994 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 1995 0 : (be_lun->size_blocks - 1); 1996 1997 error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD, 1998 curthread); 1999 if (error) 2000 ps = po = 0; 2001 else { 2002 error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po, 2003 FREAD, curthread); 2004 if (error) 2005 po = 0; 2006 } 2007 us = ps; 2008 uo = po; 2009 2010 value = ctl_get_opt(&cbe_lun->options, "pblocksize"); 2011 if (value != NULL) 2012 ctl_expand_number(value, &ps); 2013 value = ctl_get_opt(&cbe_lun->options, "pblockoffset"); 2014 if (value != NULL) 2015 ctl_expand_number(value, &po); 2016 pss = ps / cbe_lun->blocksize; 2017 pos = po / cbe_lun->blocksize; 2018 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) && 2019 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) { 2020 cbe_lun->pblockexp = fls(pss) - 1; 2021 cbe_lun->pblockoff = (pss - pos) % pss; 2022 } 2023 2024 value = ctl_get_opt(&cbe_lun->options, "ublocksize"); 2025 if (value != NULL) 2026 ctl_expand_number(value, &us); 2027 value = ctl_get_opt(&cbe_lun->options, "ublockoffset"); 2028 if (value != NULL) 2029 ctl_expand_number(value, &uo); 2030 uss = us / cbe_lun->blocksize; 2031 uos = uo / cbe_lun->blocksize; 2032 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) && 2033 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) { 2034 cbe_lun->ublockexp = fls(uss) - 1; 2035 cbe_lun->ublockoff = (uss - uos) % uss; 2036 } 2037 2038 cbe_lun->atomicblock = atomic / cbe_lun->blocksize; 2039 cbe_lun->opttxferlen = maxio / cbe_lun->blocksize; 2040 2041 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) { 2042 unmap = 1; 2043 } else { 2044 struct diocgattr_arg arg; 2045 2046 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name)); 2047 arg.len = sizeof(arg.value.i); 2048 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD, 2049 curthread); 2050 unmap = (error == 0) ? arg.value.i : 0; 2051 } 2052 value = ctl_get_opt(&cbe_lun->options, "unmap"); 2053 if (value != NULL) 2054 unmap = (strcmp(value, "on") == 0); 2055 if (unmap) 2056 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 2057 else 2058 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP; 2059 2060 dev_relthread(dev, ref); 2061 return (0); 2062} 2063 2064static int 2065ctl_be_block_close(struct ctl_be_block_lun *be_lun) 2066{ 2067 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2068 int flags; 2069 2070 if (be_lun->vn) { 2071 flags = FREAD; 2072 if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0) 2073 flags |= FWRITE; 2074 (void)vn_close(be_lun->vn, flags, NOCRED, curthread); 2075 be_lun->vn = NULL; 2076 2077 switch (be_lun->dev_type) { 2078 case CTL_BE_BLOCK_DEV: 2079 break; 2080 case CTL_BE_BLOCK_FILE: 2081 if (be_lun->backend.file.cred != NULL) { 2082 crfree(be_lun->backend.file.cred); 2083 be_lun->backend.file.cred = NULL; 2084 } 2085 break; 2086 case CTL_BE_BLOCK_NONE: 2087 break; 2088 default: 2089 panic("Unexpected backend type."); 2090 break; 2091 } 2092 be_lun->dev_type = CTL_BE_BLOCK_NONE; 2093 } 2094 return (0); 2095} 2096 2097static int 2098ctl_be_block_open(struct ctl_be_block_softc *softc, 2099 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req) 2100{ 2101 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2102 struct nameidata nd; 2103 char *value; 2104 int error, flags; 2105 2106 error = 0; 2107 if (rootvnode == NULL) { 2108 snprintf(req->error_str, sizeof(req->error_str), 2109 "Root filesystem is not mounted"); 2110 return (1); 2111 } 2112 pwd_ensure_dirs(); 2113 2114 value = ctl_get_opt(&cbe_lun->options, "file"); 2115 if (value == NULL) { 2116 snprintf(req->error_str, sizeof(req->error_str), 2117 "no file argument specified"); 2118 return (1); 2119 } 2120 free(be_lun->dev_path, M_CTLBLK); 2121 be_lun->dev_path = strdup(value, M_CTLBLK); 2122 2123 flags = FREAD; 2124 value = ctl_get_opt(&cbe_lun->options, "readonly"); 2125 if (value == NULL || strcmp(value, "on") != 0) 2126 flags |= FWRITE; 2127 2128again: 2129 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread); 2130 error = vn_open(&nd, &flags, 0, NULL); 2131 if ((error == EROFS || error == EACCES) && (flags & FWRITE)) { 2132 flags &= ~FWRITE; 2133 goto again; 2134 } 2135 if (error) { 2136 /* 2137 * This is the only reasonable guess we can make as far as 2138 * path if the user doesn't give us a fully qualified path. 2139 * If they want to specify a file, they need to specify the 2140 * full path. 2141 */ 2142 if (be_lun->dev_path[0] != '/') { 2143 char *dev_name; 2144 2145 asprintf(&dev_name, M_CTLBLK, "/dev/%s", 2146 be_lun->dev_path); 2147 free(be_lun->dev_path, M_CTLBLK); 2148 be_lun->dev_path = dev_name; 2149 goto again; 2150 } 2151 snprintf(req->error_str, sizeof(req->error_str), 2152 "error opening %s: %d", be_lun->dev_path, error); 2153 return (error); 2154 } 2155 if (flags & FWRITE) 2156 cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY; 2157 else 2158 cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 2159 2160 NDFREE(&nd, NDF_ONLY_PNBUF); 2161 be_lun->vn = nd.ni_vp; 2162 2163 /* We only support disks and files. */ 2164 if (vn_isdisk(be_lun->vn, &error)) { 2165 error = ctl_be_block_open_dev(be_lun, req); 2166 } else if (be_lun->vn->v_type == VREG) { 2167 error = ctl_be_block_open_file(be_lun, req); 2168 } else { 2169 error = EINVAL; 2170 snprintf(req->error_str, sizeof(req->error_str), 2171 "%s is not a disk or plain file", be_lun->dev_path); 2172 } 2173 VOP_UNLOCK(be_lun->vn, 0); 2174 2175 if (error != 0) 2176 ctl_be_block_close(be_lun); 2177 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2178 if (be_lun->dispatch != ctl_be_block_dispatch_dev) 2179 cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2180 value = ctl_get_opt(&cbe_lun->options, "serseq"); 2181 if (value != NULL && strcmp(value, "on") == 0) 2182 cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 2183 else if (value != NULL && strcmp(value, "read") == 0) 2184 cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 2185 else if (value != NULL && strcmp(value, "off") == 0) 2186 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 2187 return (0); 2188} 2189 2190static int 2191ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2192{ 2193 struct ctl_be_lun *cbe_lun; 2194 struct ctl_be_block_lun *be_lun; 2195 struct ctl_lun_create_params *params; 2196 char num_thread_str[16]; 2197 char tmpstr[32]; 2198 char *value; 2199 int retval, num_threads; 2200 int tmp_num_threads; 2201 2202 params = &req->reqdata.create; 2203 retval = 0; 2204 req->status = CTL_LUN_OK; 2205 2206 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK); 2207 cbe_lun = &be_lun->cbe_lun; 2208 cbe_lun->be_lun = be_lun; 2209 be_lun->params = req->reqdata.create; 2210 be_lun->softc = softc; 2211 STAILQ_INIT(&be_lun->input_queue); 2212 STAILQ_INIT(&be_lun->config_read_queue); 2213 STAILQ_INIT(&be_lun->config_write_queue); 2214 STAILQ_INIT(&be_lun->datamove_queue); 2215 sprintf(be_lun->lunname, "cblk%d", softc->num_luns); 2216 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF); 2217 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF); 2218 ctl_init_opts(&cbe_lun->options, 2219 req->num_be_args, req->kern_be_args); 2220 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG, 2221 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0); 2222 if (be_lun->lun_zone == NULL) { 2223 snprintf(req->error_str, sizeof(req->error_str), 2224 "error allocating UMA zone"); 2225 goto bailout_error; 2226 } 2227 2228 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 2229 cbe_lun->lun_type = params->device_type; 2230 else 2231 cbe_lun->lun_type = T_DIRECT; 2232 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED; 2233 cbe_lun->flags = 0; 2234 value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2235 if (value != NULL) { 2236 if (strcmp(value, "primary") == 0) 2237 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2238 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2239 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2240 2241 if (cbe_lun->lun_type == T_DIRECT) { 2242 be_lun->size_bytes = params->lun_size_bytes; 2243 if (params->blocksize_bytes != 0) 2244 cbe_lun->blocksize = params->blocksize_bytes; 2245 else 2246 cbe_lun->blocksize = 512; 2247 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2248 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2249 0 : (be_lun->size_blocks - 1); 2250 2251 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2252 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2253 retval = ctl_be_block_open(softc, be_lun, req); 2254 if (retval != 0) { 2255 retval = 0; 2256 req->status = CTL_LUN_WARNING; 2257 } 2258 } 2259 num_threads = cbb_num_threads; 2260 } else { 2261 num_threads = 1; 2262 } 2263 2264 /* 2265 * XXX This searching loop might be refactored to be combined with 2266 * the loop above, 2267 */ 2268 value = ctl_get_opt(&cbe_lun->options, "num_threads"); 2269 if (value != NULL) { 2270 tmp_num_threads = strtol(value, NULL, 0); 2271 2272 /* 2273 * We don't let the user specify less than one 2274 * thread, but hope he's clueful enough not to 2275 * specify 1000 threads. 2276 */ 2277 if (tmp_num_threads < 1) { 2278 snprintf(req->error_str, sizeof(req->error_str), 2279 "invalid number of threads %s", 2280 num_thread_str); 2281 goto bailout_error; 2282 } 2283 num_threads = tmp_num_threads; 2284 } 2285 2286 if (be_lun->vn == NULL) 2287 cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2288 /* Tell the user the blocksize we ended up using */ 2289 params->lun_size_bytes = be_lun->size_bytes; 2290 params->blocksize_bytes = cbe_lun->blocksize; 2291 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 2292 cbe_lun->req_lun_id = params->req_lun_id; 2293 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 2294 } else 2295 cbe_lun->req_lun_id = 0; 2296 2297 cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown; 2298 cbe_lun->lun_config_status = ctl_be_block_lun_config_status; 2299 cbe_lun->be = &ctl_be_block_driver; 2300 2301 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 2302 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 2303 softc->num_luns); 2304 strncpy((char *)cbe_lun->serial_num, tmpstr, 2305 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 2306 2307 /* Tell the user what we used for a serial number */ 2308 strncpy((char *)params->serial_num, tmpstr, 2309 MIN(sizeof(params->serial_num), sizeof(tmpstr))); 2310 } else { 2311 strncpy((char *)cbe_lun->serial_num, params->serial_num, 2312 MIN(sizeof(cbe_lun->serial_num), 2313 sizeof(params->serial_num))); 2314 } 2315 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 2316 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 2317 strncpy((char *)cbe_lun->device_id, tmpstr, 2318 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 2319 2320 /* Tell the user what we used for a device ID */ 2321 strncpy((char *)params->device_id, tmpstr, 2322 MIN(sizeof(params->device_id), sizeof(tmpstr))); 2323 } else { 2324 strncpy((char *)cbe_lun->device_id, params->device_id, 2325 MIN(sizeof(cbe_lun->device_id), 2326 sizeof(params->device_id))); 2327 } 2328 2329 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun); 2330 2331 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 2332 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 2333 2334 if (be_lun->io_taskqueue == NULL) { 2335 snprintf(req->error_str, sizeof(req->error_str), 2336 "unable to create taskqueue"); 2337 goto bailout_error; 2338 } 2339 2340 /* 2341 * Note that we start the same number of threads by default for 2342 * both the file case and the block device case. For the file 2343 * case, we need multiple threads to allow concurrency, because the 2344 * vnode interface is designed to be a blocking interface. For the 2345 * block device case, ZFS zvols at least will block the caller's 2346 * context in many instances, and so we need multiple threads to 2347 * overcome that problem. Other block devices don't need as many 2348 * threads, but they shouldn't cause too many problems. 2349 * 2350 * If the user wants to just have a single thread for a block 2351 * device, he can specify that when the LUN is created, or change 2352 * the tunable/sysctl to alter the default number of threads. 2353 */ 2354 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 2355 /*num threads*/num_threads, 2356 /*priority*/PWAIT, 2357 /*thread name*/ 2358 "%s taskq", be_lun->lunname); 2359 2360 if (retval != 0) 2361 goto bailout_error; 2362 2363 be_lun->num_threads = num_threads; 2364 2365 mtx_lock(&softc->lock); 2366 softc->num_luns++; 2367 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 2368 2369 mtx_unlock(&softc->lock); 2370 2371 retval = ctl_add_lun(&be_lun->cbe_lun); 2372 if (retval != 0) { 2373 mtx_lock(&softc->lock); 2374 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2375 links); 2376 softc->num_luns--; 2377 mtx_unlock(&softc->lock); 2378 snprintf(req->error_str, sizeof(req->error_str), 2379 "ctl_add_lun() returned error %d, see dmesg for " 2380 "details", retval); 2381 retval = 0; 2382 goto bailout_error; 2383 } 2384 2385 mtx_lock(&softc->lock); 2386 2387 /* 2388 * Tell the config_status routine that we're waiting so it won't 2389 * clean up the LUN in the event of an error. 2390 */ 2391 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2392 2393 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) { 2394 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2395 if (retval == EINTR) 2396 break; 2397 } 2398 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2399 2400 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) { 2401 snprintf(req->error_str, sizeof(req->error_str), 2402 "LUN configuration error, see dmesg for details"); 2403 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, 2404 links); 2405 softc->num_luns--; 2406 mtx_unlock(&softc->lock); 2407 goto bailout_error; 2408 } else { 2409 params->req_lun_id = cbe_lun->lun_id; 2410 } 2411 2412 mtx_unlock(&softc->lock); 2413 2414 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id, 2415 cbe_lun->blocksize, 2416 DEVSTAT_ALL_SUPPORTED, 2417 cbe_lun->lun_type 2418 | DEVSTAT_TYPE_IF_OTHER, 2419 DEVSTAT_PRIORITY_OTHER); 2420 2421 return (retval); 2422 2423bailout_error: 2424 req->status = CTL_LUN_ERROR; 2425 2426 if (be_lun->io_taskqueue != NULL) 2427 taskqueue_free(be_lun->io_taskqueue); 2428 ctl_be_block_close(be_lun); 2429 if (be_lun->dev_path != NULL) 2430 free(be_lun->dev_path, M_CTLBLK); 2431 if (be_lun->lun_zone != NULL) 2432 uma_zdestroy(be_lun->lun_zone); 2433 ctl_free_opts(&cbe_lun->options); 2434 mtx_destroy(&be_lun->queue_lock); 2435 mtx_destroy(&be_lun->io_lock); 2436 free(be_lun, M_CTLBLK); 2437 2438 return (retval); 2439} 2440 2441static int 2442ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2443{ 2444 struct ctl_lun_rm_params *params; 2445 struct ctl_be_block_lun *be_lun; 2446 int retval; 2447 2448 params = &req->reqdata.rm; 2449 2450 mtx_lock(&softc->lock); 2451 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2452 if (be_lun->cbe_lun.lun_id == params->lun_id) 2453 break; 2454 } 2455 mtx_unlock(&softc->lock); 2456 2457 if (be_lun == NULL) { 2458 snprintf(req->error_str, sizeof(req->error_str), 2459 "LUN %u is not managed by the block backend", 2460 params->lun_id); 2461 goto bailout_error; 2462 } 2463 2464 retval = ctl_disable_lun(&be_lun->cbe_lun); 2465 2466 if (retval != 0) { 2467 snprintf(req->error_str, sizeof(req->error_str), 2468 "error %d returned from ctl_disable_lun() for " 2469 "LUN %d", retval, params->lun_id); 2470 goto bailout_error; 2471 2472 } 2473 2474 retval = ctl_invalidate_lun(&be_lun->cbe_lun); 2475 if (retval != 0) { 2476 snprintf(req->error_str, sizeof(req->error_str), 2477 "error %d returned from ctl_invalidate_lun() for " 2478 "LUN %d", retval, params->lun_id); 2479 goto bailout_error; 2480 } 2481 2482 mtx_lock(&softc->lock); 2483 2484 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING; 2485 2486 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2487 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0); 2488 if (retval == EINTR) 2489 break; 2490 } 2491 2492 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING; 2493 2494 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) { 2495 snprintf(req->error_str, sizeof(req->error_str), 2496 "interrupted waiting for LUN to be freed"); 2497 mtx_unlock(&softc->lock); 2498 goto bailout_error; 2499 } 2500 2501 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links); 2502 2503 softc->num_luns--; 2504 mtx_unlock(&softc->lock); 2505 2506 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 2507 2508 taskqueue_free(be_lun->io_taskqueue); 2509 2510 ctl_be_block_close(be_lun); 2511 2512 if (be_lun->disk_stats != NULL) 2513 devstat_remove_entry(be_lun->disk_stats); 2514 2515 uma_zdestroy(be_lun->lun_zone); 2516 2517 ctl_free_opts(&be_lun->cbe_lun.options); 2518 free(be_lun->dev_path, M_CTLBLK); 2519 mtx_destroy(&be_lun->queue_lock); 2520 mtx_destroy(&be_lun->io_lock); 2521 free(be_lun, M_CTLBLK); 2522 2523 req->status = CTL_LUN_OK; 2524 2525 return (0); 2526 2527bailout_error: 2528 2529 req->status = CTL_LUN_ERROR; 2530 2531 return (0); 2532} 2533 2534static int 2535ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun, 2536 struct ctl_lun_req *req) 2537{ 2538 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2539 struct vattr vattr; 2540 int error; 2541 struct ctl_lun_create_params *params = &be_lun->params; 2542 2543 if (params->lun_size_bytes != 0) { 2544 be_lun->size_bytes = params->lun_size_bytes; 2545 } else { 2546 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY); 2547 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred); 2548 VOP_UNLOCK(be_lun->vn, 0); 2549 if (error != 0) { 2550 snprintf(req->error_str, sizeof(req->error_str), 2551 "error calling VOP_GETATTR() for file %s", 2552 be_lun->dev_path); 2553 return (error); 2554 } 2555 be_lun->size_bytes = vattr.va_size; 2556 } 2557 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2558 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2559 0 : (be_lun->size_blocks - 1); 2560 return (0); 2561} 2562 2563static int 2564ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun, 2565 struct ctl_lun_req *req) 2566{ 2567 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun; 2568 struct ctl_lun_create_params *params = &be_lun->params; 2569 struct cdevsw *csw; 2570 struct cdev *dev; 2571 uint64_t size_bytes; 2572 int error, ref; 2573 2574 csw = devvn_refthread(be_lun->vn, &dev, &ref); 2575 if (csw == NULL) 2576 return (ENXIO); 2577 if (csw->d_ioctl == NULL) { 2578 dev_relthread(dev, ref); 2579 snprintf(req->error_str, sizeof(req->error_str), 2580 "no d_ioctl for device %s!", be_lun->dev_path); 2581 return (ENODEV); 2582 } 2583 2584 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&size_bytes, FREAD, 2585 curthread); 2586 dev_relthread(dev, ref); 2587 if (error) { 2588 snprintf(req->error_str, sizeof(req->error_str), 2589 "error %d returned for DIOCGMEDIASIZE ioctl " 2590 "on %s!", error, be_lun->dev_path); 2591 return (error); 2592 } 2593 2594 if (params->lun_size_bytes != 0) { 2595 if (params->lun_size_bytes > size_bytes) { 2596 snprintf(req->error_str, sizeof(req->error_str), 2597 "requested LUN size %ju > backing device " 2598 "size %ju", 2599 (uintmax_t)params->lun_size_bytes, 2600 (uintmax_t)size_bytes); 2601 return (EINVAL); 2602 } 2603 be_lun->size_bytes = params->lun_size_bytes; 2604 } else { 2605 be_lun->size_bytes = size_bytes; 2606 } 2607 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize; 2608 cbe_lun->maxlba = (be_lun->size_blocks == 0) ? 2609 0 : (be_lun->size_blocks - 1); 2610 return (0); 2611} 2612 2613static int 2614ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req) 2615{ 2616 struct ctl_lun_modify_params *params; 2617 struct ctl_be_block_lun *be_lun; 2618 struct ctl_be_lun *cbe_lun; 2619 char *value; 2620 uint64_t oldsize; 2621 int error, wasprim; 2622 2623 params = &req->reqdata.modify; 2624 2625 mtx_lock(&softc->lock); 2626 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 2627 if (be_lun->cbe_lun.lun_id == params->lun_id) 2628 break; 2629 } 2630 mtx_unlock(&softc->lock); 2631 2632 if (be_lun == NULL) { 2633 snprintf(req->error_str, sizeof(req->error_str), 2634 "LUN %u is not managed by the block backend", 2635 params->lun_id); 2636 goto bailout_error; 2637 } 2638 cbe_lun = &be_lun->cbe_lun; 2639 2640 if (params->lun_size_bytes != 0) 2641 be_lun->params.lun_size_bytes = params->lun_size_bytes; 2642 ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); 2643 2644 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 2645 value = ctl_get_opt(&cbe_lun->options, "ha_role"); 2646 if (value != NULL) { 2647 if (strcmp(value, "primary") == 0) 2648 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2649 else 2650 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2651 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 2652 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 2653 else 2654 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 2655 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 2656 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 2657 ctl_lun_primary(cbe_lun); 2658 else 2659 ctl_lun_secondary(cbe_lun); 2660 } 2661 2662 oldsize = be_lun->size_blocks; 2663 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) || 2664 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) { 2665 if (be_lun->vn == NULL) 2666 error = ctl_be_block_open(softc, be_lun, req); 2667 else if (vn_isdisk(be_lun->vn, &error)) 2668 error = ctl_be_block_modify_dev(be_lun, req); 2669 else if (be_lun->vn->v_type == VREG) 2670 error = ctl_be_block_modify_file(be_lun, req); 2671 else 2672 error = EINVAL; 2673 if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) && 2674 be_lun->vn != NULL) { 2675 cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE; 2676 ctl_lun_online(cbe_lun); 2677 } 2678 } else { 2679 if (be_lun->vn != NULL) { 2680 cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE; 2681 ctl_lun_offline(cbe_lun); 2682 pause("CTL LUN offline", hz / 8); // XXX 2683 error = ctl_be_block_close(be_lun); 2684 } else 2685 error = 0; 2686 } 2687 if (be_lun->size_blocks != oldsize) 2688 ctl_lun_capacity_changed(cbe_lun); 2689 2690 /* Tell the user the exact size we ended up using */ 2691 params->lun_size_bytes = be_lun->size_bytes; 2692 2693 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK; 2694 return (0); 2695 2696bailout_error: 2697 req->status = CTL_LUN_ERROR; 2698 return (0); 2699} 2700 2701static void 2702ctl_be_block_lun_shutdown(void *be_lun) 2703{ 2704 struct ctl_be_block_lun *lun; 2705 struct ctl_be_block_softc *softc; 2706 2707 lun = (struct ctl_be_block_lun *)be_lun; 2708 2709 softc = lun->softc; 2710 2711 mtx_lock(&softc->lock); 2712 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED; 2713 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2714 wakeup(lun); 2715 mtx_unlock(&softc->lock); 2716 2717} 2718 2719static void 2720ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status) 2721{ 2722 struct ctl_be_block_lun *lun; 2723 struct ctl_be_block_softc *softc; 2724 2725 lun = (struct ctl_be_block_lun *)be_lun; 2726 softc = lun->softc; 2727 2728 if (status == CTL_LUN_CONFIG_OK) { 2729 mtx_lock(&softc->lock); 2730 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2731 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING) 2732 wakeup(lun); 2733 mtx_unlock(&softc->lock); 2734 2735 /* 2736 * We successfully added the LUN, attempt to enable it. 2737 */ 2738 if (ctl_enable_lun(&lun->cbe_lun) != 0) { 2739 printf("%s: ctl_enable_lun() failed!\n", __func__); 2740 if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { 2741 printf("%s: ctl_invalidate_lun() failed!\n", 2742 __func__); 2743 } 2744 } 2745 2746 return; 2747 } 2748 2749 2750 mtx_lock(&softc->lock); 2751 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED; 2752 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR; 2753 wakeup(lun); 2754 mtx_unlock(&softc->lock); 2755} 2756 2757 2758static int 2759ctl_be_block_config_write(union ctl_io *io) 2760{ 2761 struct ctl_be_block_lun *be_lun; 2762 struct ctl_be_lun *cbe_lun; 2763 int retval; 2764 2765 retval = 0; 2766 2767 DPRINTF("entered\n"); 2768 2769 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2770 CTL_PRIV_BACKEND_LUN].ptr; 2771 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2772 2773 switch (io->scsiio.cdb[0]) { 2774 case SYNCHRONIZE_CACHE: 2775 case SYNCHRONIZE_CACHE_16: 2776 case WRITE_SAME_10: 2777 case WRITE_SAME_16: 2778 case UNMAP: 2779 /* 2780 * The upper level CTL code will filter out any CDBs with 2781 * the immediate bit set and return the proper error. 2782 * 2783 * We don't really need to worry about what LBA range the 2784 * user asked to be synced out. When they issue a sync 2785 * cache command, we'll sync out the whole thing. 2786 */ 2787 mtx_lock(&be_lun->queue_lock); 2788 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr, 2789 links); 2790 mtx_unlock(&be_lun->queue_lock); 2791 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task); 2792 break; 2793 case START_STOP_UNIT: { 2794 struct scsi_start_stop_unit *cdb; 2795 2796 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 2797 2798 if (cdb->how & SSS_START) 2799 retval = ctl_start_lun(cbe_lun); 2800 else { 2801 retval = ctl_stop_lun(cbe_lun); 2802 /* 2803 * XXX KDM Copan-specific offline behavior. 2804 * Figure out a reasonable way to port this? 2805 */ 2806#ifdef NEEDTOPORT 2807 if ((retval == 0) 2808 && (cdb->byte2 & SSS_ONOFFLINE)) 2809 retval = ctl_lun_offline(cbe_lun); 2810#endif 2811 } 2812 2813 /* 2814 * In general, the above routines should not fail. They 2815 * just set state for the LUN. So we've got something 2816 * pretty wrong here if we can't start or stop the LUN. 2817 */ 2818 if (retval != 0) { 2819 ctl_set_internal_failure(&io->scsiio, 2820 /*sks_valid*/ 1, 2821 /*retry_count*/ 0xf051); 2822 retval = CTL_RETVAL_COMPLETE; 2823 } else { 2824 ctl_set_success(&io->scsiio); 2825 } 2826 ctl_config_write_done(io); 2827 break; 2828 } 2829 default: 2830 ctl_set_invalid_opcode(&io->scsiio); 2831 ctl_config_write_done(io); 2832 retval = CTL_RETVAL_COMPLETE; 2833 break; 2834 } 2835 2836 return (retval); 2837} 2838 2839static int 2840ctl_be_block_config_read(union ctl_io *io) 2841{ 2842 struct ctl_be_block_lun *be_lun; 2843 struct ctl_be_lun *cbe_lun; 2844 int retval = 0; 2845 2846 DPRINTF("entered\n"); 2847 2848 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 2849 CTL_PRIV_BACKEND_LUN].ptr; 2850 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun; 2851 2852 switch (io->scsiio.cdb[0]) { 2853 case SERVICE_ACTION_IN: 2854 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 2855 mtx_lock(&be_lun->queue_lock); 2856 STAILQ_INSERT_TAIL(&be_lun->config_read_queue, 2857 &io->io_hdr, links); 2858 mtx_unlock(&be_lun->queue_lock); 2859 taskqueue_enqueue(be_lun->io_taskqueue, 2860 &be_lun->io_task); 2861 retval = CTL_RETVAL_QUEUED; 2862 break; 2863 } 2864 ctl_set_invalid_field(&io->scsiio, 2865 /*sks_valid*/ 1, 2866 /*command*/ 1, 2867 /*field*/ 1, 2868 /*bit_valid*/ 1, 2869 /*bit*/ 4); 2870 ctl_config_read_done(io); 2871 retval = CTL_RETVAL_COMPLETE; 2872 break; 2873 default: 2874 ctl_set_invalid_opcode(&io->scsiio); 2875 ctl_config_read_done(io); 2876 retval = CTL_RETVAL_COMPLETE; 2877 break; 2878 } 2879 2880 return (retval); 2881} 2882 2883static int 2884ctl_be_block_lun_info(void *be_lun, struct sbuf *sb) 2885{ 2886 struct ctl_be_block_lun *lun; 2887 int retval; 2888 2889 lun = (struct ctl_be_block_lun *)be_lun; 2890 retval = 0; 2891 2892 retval = sbuf_printf(sb, "\t<num_threads>"); 2893 2894 if (retval != 0) 2895 goto bailout; 2896 2897 retval = sbuf_printf(sb, "%d", lun->num_threads); 2898 2899 if (retval != 0) 2900 goto bailout; 2901 2902 retval = sbuf_printf(sb, "</num_threads>\n"); 2903 2904bailout: 2905 2906 return (retval); 2907} 2908 2909static uint64_t 2910ctl_be_block_lun_attr(void *be_lun, const char *attrname) 2911{ 2912 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun; 2913 2914 if (lun->getattr == NULL) 2915 return (UINT64_MAX); 2916 return (lun->getattr(lun, attrname)); 2917} 2918 2919int 2920ctl_be_block_init(void) 2921{ 2922 struct ctl_be_block_softc *softc; 2923 int retval; 2924 2925 softc = &backend_block_softc; 2926 retval = 0; 2927 2928 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF); 2929 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io), 2930 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2931 STAILQ_INIT(&softc->lun_list); 2932 2933 return (retval); 2934} 2935