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