1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2003, 2008 Silicon Graphics International Corp. 5 * Copyright (c) 2012 The FreeBSD Foundation 6 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org> 7 * All rights reserved. 8 * 9 * Portions of this software were developed by Edward Tomasz Napierala 10 * under sponsorship from the FreeBSD Foundation. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions, and the following disclaimer, 17 * without modification. 18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 19 * substantially similar to the "NO WARRANTY" disclaimer below 20 * ("Disclaimer") and any redistribution must be conditioned upon 21 * including a substantially similar Disclaimer requirement for further 22 * binary redistribution. 23 * 24 * NO WARRANTY 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 29 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGES. 36 * 37 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $ 38 */ 39 /* 40 * CAM Target Layer black hole and RAM disk backend. 41 * 42 * Author: Ken Merry <ken@FreeBSD.org> 43 */ 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/condvar.h> 52 #include <sys/types.h> 53 #include <sys/limits.h> 54 #include <sys/lock.h> 55 #include <sys/mutex.h> 56 #include <sys/malloc.h> 57 #include <sys/sx.h> 58 #include <sys/taskqueue.h> 59 #include <sys/time.h> 60 #include <sys/queue.h> 61 #include <sys/conf.h> 62 #include <sys/ioccom.h> 63 #include <sys/module.h> 64 #include <sys/sysctl.h> 65 #include <sys/nv.h> 66 #include <sys/dnv.h> 67 68 #include <cam/scsi/scsi_all.h> 69 #include <cam/scsi/scsi_da.h> 70 #include <cam/ctl/ctl_io.h> 71 #include <cam/ctl/ctl.h> 72 #include <cam/ctl/ctl_util.h> 73 #include <cam/ctl/ctl_backend.h> 74 #include <cam/ctl/ctl_debug.h> 75 #include <cam/ctl/ctl_ioctl.h> 76 #include <cam/ctl/ctl_ha.h> 77 #include <cam/ctl/ctl_private.h> 78 #include <cam/ctl/ctl_error.h> 79 80 #define PRIV(io) \ 81 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND]) 82 #define ARGS(io) \ 83 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]) 84 85 #define PPP (PAGE_SIZE / sizeof(uint8_t **)) 86 #ifdef __LP64__ 87 #define PPPS (PAGE_SHIFT - 3) 88 #else 89 #define PPPS (PAGE_SHIFT - 2) 90 #endif 91 #define SGPP (PAGE_SIZE / sizeof(struct ctl_sg_entry)) 92 93 #define P_UNMAPPED NULL /* Page is unmapped. */ 94 #define P_ANCHORED ((void *)(uintptr_t)1) /* Page is anchored. */ 95 96 typedef enum { 97 GP_READ, /* Return data page or zero page. */ 98 GP_WRITE, /* Return data page, try allocate if none. */ 99 GP_ANCHOR, /* Return data page, try anchor if none. */ 100 GP_OTHER, /* Return what present, do not allocate/anchor. */ 101 } getpage_op_t; 102 103 typedef enum { 104 CTL_BE_RAMDISK_LUN_UNCONFIGURED = 0x01, 105 CTL_BE_RAMDISK_LUN_WAITING = 0x04 106 } ctl_be_ramdisk_lun_flags; 107 108 struct ctl_be_ramdisk_lun { 109 struct ctl_be_lun cbe_lun; /* Must be first element. */ 110 struct ctl_lun_create_params params; 111 int indir; 112 uint8_t **pages; 113 uint8_t *zero_page; 114 struct sx page_lock; 115 u_int pblocksize; 116 u_int pblockmul; 117 uint64_t size_bytes; 118 uint64_t size_blocks; 119 uint64_t cap_bytes; 120 uint64_t cap_used; 121 struct ctl_be_ramdisk_softc *softc; 122 ctl_be_ramdisk_lun_flags flags; 123 SLIST_ENTRY(ctl_be_ramdisk_lun) links; 124 struct taskqueue *io_taskqueue; 125 struct task io_task; 126 STAILQ_HEAD(, ctl_io_hdr) cont_queue; 127 struct mtx_padalign queue_lock; 128 }; 129 130 struct ctl_be_ramdisk_softc { 131 struct sx modify_lock; 132 struct mtx lock; 133 int num_luns; 134 SLIST_HEAD(, ctl_be_ramdisk_lun) lun_list; 135 }; 136 137 static struct ctl_be_ramdisk_softc rd_softc; 138 extern struct ctl_softc *control_softc; 139 140 static int ctl_backend_ramdisk_init(void); 141 static int ctl_backend_ramdisk_shutdown(void); 142 static int ctl_backend_ramdisk_move_done(union ctl_io *io); 143 static void ctl_backend_ramdisk_compare(union ctl_io *io); 144 static void ctl_backend_ramdisk_rw(union ctl_io *io); 145 static int ctl_backend_ramdisk_submit(union ctl_io *io); 146 static void ctl_backend_ramdisk_worker(void *context, int pending); 147 static int ctl_backend_ramdisk_config_read(union ctl_io *io); 148 static int ctl_backend_ramdisk_config_write(union ctl_io *io); 149 static uint64_t ctl_backend_ramdisk_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname); 150 static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, 151 caddr_t addr, int flag, struct thread *td); 152 static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, 153 struct ctl_lun_req *req); 154 static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, 155 struct ctl_lun_req *req); 156 static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, 157 struct ctl_lun_req *req); 158 static void ctl_backend_ramdisk_lun_shutdown(struct ctl_be_lun *cbe_lun); 159 160 static struct ctl_backend_driver ctl_be_ramdisk_driver = 161 { 162 .name = "ramdisk", 163 .flags = CTL_BE_FLAG_HAS_CONFIG, 164 .init = ctl_backend_ramdisk_init, 165 .shutdown = ctl_backend_ramdisk_shutdown, 166 .data_submit = ctl_backend_ramdisk_submit, 167 .data_move_done = ctl_backend_ramdisk_move_done, 168 .config_read = ctl_backend_ramdisk_config_read, 169 .config_write = ctl_backend_ramdisk_config_write, 170 .ioctl = ctl_backend_ramdisk_ioctl, 171 .lun_attr = ctl_backend_ramdisk_lun_attr, 172 }; 173 174 MALLOC_DEFINE(M_RAMDISK, "ctlramdisk", "Memory used for CTL RAMdisk"); 175 CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver); 176 177 static int 178 ctl_backend_ramdisk_init(void) 179 { 180 struct ctl_be_ramdisk_softc *softc = &rd_softc; 181 182 memset(softc, 0, sizeof(*softc)); 183 sx_init(&softc->modify_lock, "ctlrammod"); 184 mtx_init(&softc->lock, "ctlram", NULL, MTX_DEF); 185 SLIST_INIT(&softc->lun_list); 186 return (0); 187 } 188 189 static int 190 ctl_backend_ramdisk_shutdown(void) 191 { 192 struct ctl_be_ramdisk_softc *softc = &rd_softc; 193 struct ctl_be_ramdisk_lun *lun; 194 195 mtx_lock(&softc->lock); 196 while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) { 197 SLIST_REMOVE_HEAD(&softc->lun_list, links); 198 softc->num_luns--; 199 /* 200 * Drop our lock here. Since ctl_remove_lun() can call 201 * back into us, this could potentially lead to a recursive 202 * lock of the same mutex, which would cause a hang. 203 */ 204 mtx_unlock(&softc->lock); 205 ctl_remove_lun(&lun->cbe_lun); 206 mtx_lock(&softc->lock); 207 } 208 mtx_unlock(&softc->lock); 209 mtx_destroy(&softc->lock); 210 sx_destroy(&softc->modify_lock); 211 return (0); 212 } 213 214 static uint8_t * 215 ctl_backend_ramdisk_getpage(struct ctl_be_ramdisk_lun *be_lun, off_t pn, 216 getpage_op_t op) 217 { 218 uint8_t **p, ***pp; 219 off_t i; 220 int s; 221 222 if (be_lun->cap_bytes == 0) { 223 switch (op) { 224 case GP_READ: 225 return (be_lun->zero_page); 226 case GP_WRITE: 227 return ((uint8_t *)be_lun->pages); 228 case GP_ANCHOR: 229 return (P_ANCHORED); 230 default: 231 return (P_UNMAPPED); 232 } 233 } 234 if (op == GP_WRITE || op == GP_ANCHOR) { 235 sx_xlock(&be_lun->page_lock); 236 pp = &be_lun->pages; 237 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) { 238 if (*pp == NULL) { 239 *pp = malloc(PAGE_SIZE, M_RAMDISK, 240 M_WAITOK|M_ZERO); 241 } 242 i = pn >> s; 243 pp = (uint8_t ***)&(*pp)[i]; 244 pn -= i << s; 245 } 246 if (*pp == P_UNMAPPED && be_lun->cap_used < be_lun->cap_bytes) { 247 if (op == GP_WRITE) { 248 *pp = malloc(be_lun->pblocksize, M_RAMDISK, 249 M_WAITOK|M_ZERO); 250 } else 251 *pp = P_ANCHORED; 252 be_lun->cap_used += be_lun->pblocksize; 253 } else if (*pp == P_ANCHORED && op == GP_WRITE) { 254 *pp = malloc(be_lun->pblocksize, M_RAMDISK, 255 M_WAITOK|M_ZERO); 256 } 257 sx_xunlock(&be_lun->page_lock); 258 return ((uint8_t *)*pp); 259 } else { 260 sx_slock(&be_lun->page_lock); 261 p = be_lun->pages; 262 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) { 263 if (p == NULL) 264 break; 265 i = pn >> s; 266 p = (uint8_t **)p[i]; 267 pn -= i << s; 268 } 269 sx_sunlock(&be_lun->page_lock); 270 if ((p == P_UNMAPPED || p == P_ANCHORED) && op == GP_READ) 271 return (be_lun->zero_page); 272 return ((uint8_t *)p); 273 } 274 }; 275 276 static void 277 ctl_backend_ramdisk_unmappage(struct ctl_be_ramdisk_lun *be_lun, off_t pn) 278 { 279 uint8_t ***pp; 280 off_t i; 281 int s; 282 283 if (be_lun->cap_bytes == 0) 284 return; 285 sx_xlock(&be_lun->page_lock); 286 pp = &be_lun->pages; 287 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) { 288 if (*pp == NULL) 289 goto noindir; 290 i = pn >> s; 291 pp = (uint8_t ***)&(*pp)[i]; 292 pn -= i << s; 293 } 294 if (*pp == P_ANCHORED) { 295 be_lun->cap_used -= be_lun->pblocksize; 296 *pp = P_UNMAPPED; 297 } else if (*pp != P_UNMAPPED) { 298 free(*pp, M_RAMDISK); 299 be_lun->cap_used -= be_lun->pblocksize; 300 *pp = P_UNMAPPED; 301 } 302 noindir: 303 sx_xunlock(&be_lun->page_lock); 304 }; 305 306 static void 307 ctl_backend_ramdisk_anchorpage(struct ctl_be_ramdisk_lun *be_lun, off_t pn) 308 { 309 uint8_t ***pp; 310 off_t i; 311 int s; 312 313 if (be_lun->cap_bytes == 0) 314 return; 315 sx_xlock(&be_lun->page_lock); 316 pp = &be_lun->pages; 317 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) { 318 if (*pp == NULL) 319 goto noindir; 320 i = pn >> s; 321 pp = (uint8_t ***)&(*pp)[i]; 322 pn -= i << s; 323 } 324 if (*pp == P_UNMAPPED && be_lun->cap_used < be_lun->cap_bytes) { 325 be_lun->cap_used += be_lun->pblocksize; 326 *pp = P_ANCHORED; 327 } else if (*pp != P_ANCHORED) { 328 free(*pp, M_RAMDISK); 329 *pp = P_ANCHORED; 330 } 331 noindir: 332 sx_xunlock(&be_lun->page_lock); 333 }; 334 335 static void 336 ctl_backend_ramdisk_freeallpages(uint8_t **p, int indir) 337 { 338 int i; 339 340 if (p == NULL) 341 return; 342 if (indir == 0) { 343 free(p, M_RAMDISK); 344 return; 345 } 346 for (i = 0; i < PPP; i++) { 347 if (p[i] == NULL) 348 continue; 349 ctl_backend_ramdisk_freeallpages((uint8_t **)p[i], indir - 1); 350 } 351 free(p, M_RAMDISK); 352 }; 353 354 static size_t 355 cmp(uint8_t *a, uint8_t *b, size_t size) 356 { 357 size_t i; 358 359 for (i = 0; i < size; i++) { 360 if (a[i] != b[i]) 361 break; 362 } 363 return (i); 364 } 365 366 static int 367 ctl_backend_ramdisk_cmp(union ctl_io *io) 368 { 369 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io); 370 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun; 371 uint8_t *page; 372 uint8_t info[8]; 373 uint64_t lba; 374 u_int lbaoff, lbas, res, off; 375 376 lbas = io->scsiio.kern_data_len / cbe_lun->blocksize; 377 lba = ARGS(io)->lba + PRIV(io)->len - lbas; 378 off = 0; 379 for (; lbas > 0; lbas--, lba++) { 380 page = ctl_backend_ramdisk_getpage(be_lun, 381 lba >> cbe_lun->pblockexp, GP_READ); 382 lbaoff = lba & ~(UINT_MAX << cbe_lun->pblockexp); 383 page += lbaoff * cbe_lun->blocksize; 384 res = cmp(io->scsiio.kern_data_ptr + off, page, 385 cbe_lun->blocksize); 386 off += res; 387 if (res < cbe_lun->blocksize) 388 break; 389 } 390 if (lbas > 0) { 391 off += io->scsiio.kern_rel_offset - io->scsiio.kern_data_len; 392 scsi_u64to8b(off, info); 393 ctl_set_sense(&io->scsiio, /*current_error*/ 1, 394 /*sense_key*/ SSD_KEY_MISCOMPARE, 395 /*asc*/ 0x1D, /*ascq*/ 0x00, 396 /*type*/ SSD_ELEM_INFO, 397 /*size*/ sizeof(info), /*data*/ &info, 398 /*type*/ SSD_ELEM_NONE); 399 return (1); 400 } 401 return (0); 402 } 403 404 static int 405 ctl_backend_ramdisk_move_done(union ctl_io *io) 406 { 407 struct ctl_be_ramdisk_lun *be_lun = 408 (struct ctl_be_ramdisk_lun *)CTL_BACKEND_LUN(io); 409 #ifdef CTL_TIME_IO 410 struct bintime cur_bt; 411 #endif 412 413 CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n")); 414 #ifdef CTL_TIME_IO 415 getbinuptime(&cur_bt); 416 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 417 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 418 #endif 419 io->io_hdr.num_dmas++; 420 if (io->scsiio.kern_sg_entries > 0) 421 free(io->scsiio.kern_data_ptr, M_RAMDISK); 422 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 423 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 424 ; 425 } else if (io->io_hdr.port_status != 0 && 426 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 427 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 428 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1, 429 /*retry_count*/ io->io_hdr.port_status); 430 } else if (io->scsiio.kern_data_resid != 0 && 431 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT && 432 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 433 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 434 ctl_set_invalid_field_ciu(&io->scsiio); 435 } else if ((io->io_hdr.port_status == 0) && 436 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 437 if (ARGS(io)->flags & CTL_LLF_COMPARE) { 438 /* We have data block ready for comparison. */ 439 if (ctl_backend_ramdisk_cmp(io)) 440 goto done; 441 } 442 if (ARGS(io)->len > PRIV(io)->len) { 443 mtx_lock(&be_lun->queue_lock); 444 STAILQ_INSERT_TAIL(&be_lun->cont_queue, 445 &io->io_hdr, links); 446 mtx_unlock(&be_lun->queue_lock); 447 taskqueue_enqueue(be_lun->io_taskqueue, 448 &be_lun->io_task); 449 return (0); 450 } 451 ctl_set_success(&io->scsiio); 452 } 453 done: 454 ctl_data_submit_done(io); 455 return(0); 456 } 457 458 static void 459 ctl_backend_ramdisk_compare(union ctl_io *io) 460 { 461 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io); 462 u_int lbas, len; 463 464 lbas = ARGS(io)->len - PRIV(io)->len; 465 lbas = MIN(lbas, 131072 / cbe_lun->blocksize); 466 len = lbas * cbe_lun->blocksize; 467 468 io->scsiio.be_move_done = ctl_backend_ramdisk_move_done; 469 io->scsiio.kern_data_ptr = malloc(len, M_RAMDISK, M_WAITOK); 470 io->scsiio.kern_data_len = len; 471 io->scsiio.kern_sg_entries = 0; 472 io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 473 PRIV(io)->len += lbas; 474 #ifdef CTL_TIME_IO 475 getbinuptime(&io->io_hdr.dma_start_bt); 476 #endif 477 ctl_datamove(io); 478 } 479 480 static void 481 ctl_backend_ramdisk_rw(union ctl_io *io) 482 { 483 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io); 484 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun; 485 struct ctl_sg_entry *sg_entries; 486 uint8_t *page; 487 uint64_t lba; 488 u_int i, len, lbaoff, lbas, sgs, off; 489 getpage_op_t op; 490 491 lba = ARGS(io)->lba + PRIV(io)->len; 492 lbaoff = lba & ~(UINT_MAX << cbe_lun->pblockexp); 493 lbas = ARGS(io)->len - PRIV(io)->len; 494 lbas = MIN(lbas, (SGPP << cbe_lun->pblockexp) - lbaoff); 495 sgs = (lbas + lbaoff + be_lun->pblockmul - 1) >> cbe_lun->pblockexp; 496 off = lbaoff * cbe_lun->blocksize; 497 op = (ARGS(io)->flags & CTL_LLF_WRITE) ? GP_WRITE : GP_READ; 498 if (sgs > 1) { 499 io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) * 500 sgs, M_RAMDISK, M_WAITOK); 501 sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 502 len = lbas * cbe_lun->blocksize; 503 for (i = 0; i < sgs; i++) { 504 page = ctl_backend_ramdisk_getpage(be_lun, 505 (lba >> cbe_lun->pblockexp) + i, op); 506 if (page == P_UNMAPPED || page == P_ANCHORED) { 507 free(io->scsiio.kern_data_ptr, M_RAMDISK); 508 nospc: 509 ctl_set_space_alloc_fail(&io->scsiio); 510 ctl_data_submit_done(io); 511 return; 512 } 513 sg_entries[i].addr = page + off; 514 sg_entries[i].len = MIN(len, be_lun->pblocksize - off); 515 len -= sg_entries[i].len; 516 off = 0; 517 } 518 } else { 519 page = ctl_backend_ramdisk_getpage(be_lun, 520 lba >> cbe_lun->pblockexp, op); 521 if (page == P_UNMAPPED || page == P_ANCHORED) 522 goto nospc; 523 sgs = 0; 524 io->scsiio.kern_data_ptr = page + off; 525 } 526 527 io->scsiio.be_move_done = ctl_backend_ramdisk_move_done; 528 io->scsiio.kern_data_len = lbas * cbe_lun->blocksize; 529 io->scsiio.kern_sg_entries = sgs; 530 io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 531 PRIV(io)->len += lbas; 532 if ((ARGS(io)->flags & CTL_LLF_READ) && 533 ARGS(io)->len <= PRIV(io)->len) { 534 ctl_set_success(&io->scsiio); 535 ctl_serseq_done(io); 536 } 537 #ifdef CTL_TIME_IO 538 getbinuptime(&io->io_hdr.dma_start_bt); 539 #endif 540 ctl_datamove(io); 541 } 542 543 static int 544 ctl_backend_ramdisk_submit(union ctl_io *io) 545 { 546 struct ctl_lba_len_flags *lbalen = ARGS(io); 547 548 if (lbalen->flags & CTL_LLF_VERIFY) { 549 ctl_set_success(&io->scsiio); 550 ctl_data_submit_done(io); 551 return (CTL_RETVAL_COMPLETE); 552 } 553 PRIV(io)->len = 0; 554 if (lbalen->flags & CTL_LLF_COMPARE) 555 ctl_backend_ramdisk_compare(io); 556 else 557 ctl_backend_ramdisk_rw(io); 558 return (CTL_RETVAL_COMPLETE); 559 } 560 561 static void 562 ctl_backend_ramdisk_worker(void *context, int pending) 563 { 564 struct ctl_be_ramdisk_lun *be_lun; 565 union ctl_io *io; 566 567 be_lun = (struct ctl_be_ramdisk_lun *)context; 568 mtx_lock(&be_lun->queue_lock); 569 for (;;) { 570 io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue); 571 if (io != NULL) { 572 STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr, 573 ctl_io_hdr, links); 574 mtx_unlock(&be_lun->queue_lock); 575 if (ARGS(io)->flags & CTL_LLF_COMPARE) 576 ctl_backend_ramdisk_compare(io); 577 else 578 ctl_backend_ramdisk_rw(io); 579 mtx_lock(&be_lun->queue_lock); 580 continue; 581 } 582 583 /* 584 * If we get here, there is no work left in the queues, so 585 * just break out and let the task queue go to sleep. 586 */ 587 break; 588 } 589 mtx_unlock(&be_lun->queue_lock); 590 } 591 592 static int 593 ctl_backend_ramdisk_gls(union ctl_io *io) 594 { 595 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io); 596 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun; 597 struct scsi_get_lba_status_data *data; 598 uint8_t *page; 599 u_int lbaoff; 600 601 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr; 602 scsi_u64to8b(ARGS(io)->lba, data->descr[0].addr); 603 lbaoff = ARGS(io)->lba & ~(UINT_MAX << cbe_lun->pblockexp); 604 scsi_ulto4b(be_lun->pblockmul - lbaoff, data->descr[0].length); 605 page = ctl_backend_ramdisk_getpage(be_lun, 606 ARGS(io)->lba >> cbe_lun->pblockexp, GP_OTHER); 607 if (page == P_UNMAPPED) 608 data->descr[0].status = 1; 609 else if (page == P_ANCHORED) 610 data->descr[0].status = 2; 611 else 612 data->descr[0].status = 0; 613 ctl_config_read_done(io); 614 return (CTL_RETVAL_COMPLETE); 615 } 616 617 static int 618 ctl_backend_ramdisk_config_read(union ctl_io *io) 619 { 620 int retval = 0; 621 622 switch (io->scsiio.cdb[0]) { 623 case SERVICE_ACTION_IN: 624 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) { 625 retval = ctl_backend_ramdisk_gls(io); 626 break; 627 } 628 ctl_set_invalid_field(&io->scsiio, 629 /*sks_valid*/ 1, 630 /*command*/ 1, 631 /*field*/ 1, 632 /*bit_valid*/ 1, 633 /*bit*/ 4); 634 ctl_config_read_done(io); 635 retval = CTL_RETVAL_COMPLETE; 636 break; 637 default: 638 ctl_set_invalid_opcode(&io->scsiio); 639 ctl_config_read_done(io); 640 retval = CTL_RETVAL_COMPLETE; 641 break; 642 } 643 return (retval); 644 } 645 646 static void 647 ctl_backend_ramdisk_delete(struct ctl_be_lun *cbe_lun, off_t lba, off_t len, 648 int anchor) 649 { 650 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun; 651 uint8_t *page; 652 uint64_t p, lp; 653 u_int lbaoff; 654 getpage_op_t op = anchor ? GP_ANCHOR : GP_OTHER; 655 656 /* Partially zero first partial page. */ 657 p = lba >> cbe_lun->pblockexp; 658 lbaoff = lba & ~(UINT_MAX << cbe_lun->pblockexp); 659 if (lbaoff != 0) { 660 page = ctl_backend_ramdisk_getpage(be_lun, p, op); 661 if (page != P_UNMAPPED && page != P_ANCHORED) { 662 memset(page + lbaoff * cbe_lun->blocksize, 0, 663 min(len, be_lun->pblockmul - lbaoff) * 664 cbe_lun->blocksize); 665 } 666 p++; 667 } 668 669 /* Partially zero last partial page. */ 670 lp = (lba + len) >> cbe_lun->pblockexp; 671 lbaoff = (lba + len) & ~(UINT_MAX << cbe_lun->pblockexp); 672 if (p <= lp && lbaoff != 0) { 673 page = ctl_backend_ramdisk_getpage(be_lun, lp, op); 674 if (page != P_UNMAPPED && page != P_ANCHORED) 675 memset(page, 0, lbaoff * cbe_lun->blocksize); 676 } 677 678 /* Delete remaining full pages. */ 679 if (anchor) { 680 for (; p < lp; p++) 681 ctl_backend_ramdisk_anchorpage(be_lun, p); 682 } else { 683 for (; p < lp; p++) 684 ctl_backend_ramdisk_unmappage(be_lun, p); 685 } 686 } 687 688 static void 689 ctl_backend_ramdisk_ws(union ctl_io *io) 690 { 691 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io); 692 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun; 693 struct ctl_lba_len_flags *lbalen = ARGS(io); 694 uint8_t *page; 695 uint64_t lba; 696 u_int lbaoff, lbas; 697 698 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB)) { 699 ctl_set_invalid_field(&io->scsiio, 700 /*sks_valid*/ 1, 701 /*command*/ 1, 702 /*field*/ 1, 703 /*bit_valid*/ 0, 704 /*bit*/ 0); 705 ctl_config_write_done(io); 706 return; 707 } 708 if (lbalen->flags & SWS_UNMAP) { 709 ctl_backend_ramdisk_delete(cbe_lun, lbalen->lba, lbalen->len, 710 (lbalen->flags & SWS_ANCHOR) != 0); 711 ctl_set_success(&io->scsiio); 712 ctl_config_write_done(io); 713 return; 714 } 715 716 for (lba = lbalen->lba, lbas = lbalen->len; lbas > 0; lba++, lbas--) { 717 page = ctl_backend_ramdisk_getpage(be_lun, 718 lba >> cbe_lun->pblockexp, GP_WRITE); 719 if (page == P_UNMAPPED || page == P_ANCHORED) { 720 ctl_set_space_alloc_fail(&io->scsiio); 721 ctl_data_submit_done(io); 722 return; 723 } 724 lbaoff = lba & ~(UINT_MAX << cbe_lun->pblockexp); 725 page += lbaoff * cbe_lun->blocksize; 726 if (lbalen->flags & SWS_NDOB) { 727 memset(page, 0, cbe_lun->blocksize); 728 } else { 729 memcpy(page, io->scsiio.kern_data_ptr, 730 cbe_lun->blocksize); 731 } 732 if (lbalen->flags & SWS_LBDATA) 733 scsi_ulto4b(lba, page); 734 } 735 ctl_set_success(&io->scsiio); 736 ctl_config_write_done(io); 737 } 738 739 static void 740 ctl_backend_ramdisk_unmap(union ctl_io *io) 741 { 742 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io); 743 struct ctl_ptr_len_flags *ptrlen = (struct ctl_ptr_len_flags *)ARGS(io); 744 struct scsi_unmap_desc *buf, *end; 745 746 if ((ptrlen->flags & ~SU_ANCHOR) != 0) { 747 ctl_set_invalid_field(&io->scsiio, 748 /*sks_valid*/ 0, 749 /*command*/ 0, 750 /*field*/ 0, 751 /*bit_valid*/ 0, 752 /*bit*/ 0); 753 ctl_config_write_done(io); 754 return; 755 } 756 757 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 758 end = buf + ptrlen->len / sizeof(*buf); 759 for (; buf < end; buf++) { 760 ctl_backend_ramdisk_delete(cbe_lun, 761 scsi_8btou64(buf->lba), scsi_4btoul(buf->length), 762 (ptrlen->flags & SU_ANCHOR) != 0); 763 } 764 765 ctl_set_success(&io->scsiio); 766 ctl_config_write_done(io); 767 } 768 769 static int 770 ctl_backend_ramdisk_config_write(union ctl_io *io) 771 { 772 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io); 773 int retval = 0; 774 775 switch (io->scsiio.cdb[0]) { 776 case SYNCHRONIZE_CACHE: 777 case SYNCHRONIZE_CACHE_16: 778 /* We have no cache to flush. */ 779 ctl_set_success(&io->scsiio); 780 ctl_config_write_done(io); 781 break; 782 case START_STOP_UNIT: { 783 struct scsi_start_stop_unit *cdb; 784 785 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 786 if ((cdb->how & SSS_PC_MASK) != 0) { 787 ctl_set_success(&io->scsiio); 788 ctl_config_write_done(io); 789 break; 790 } 791 if (cdb->how & SSS_START) { 792 if (cdb->how & SSS_LOEJ) 793 ctl_lun_has_media(cbe_lun); 794 ctl_start_lun(cbe_lun); 795 } else { 796 ctl_stop_lun(cbe_lun); 797 if (cdb->how & SSS_LOEJ) 798 ctl_lun_ejected(cbe_lun); 799 } 800 ctl_set_success(&io->scsiio); 801 ctl_config_write_done(io); 802 break; 803 } 804 case PREVENT_ALLOW: 805 ctl_set_success(&io->scsiio); 806 ctl_config_write_done(io); 807 break; 808 case WRITE_SAME_10: 809 case WRITE_SAME_16: 810 ctl_backend_ramdisk_ws(io); 811 break; 812 case UNMAP: 813 ctl_backend_ramdisk_unmap(io); 814 break; 815 default: 816 ctl_set_invalid_opcode(&io->scsiio); 817 ctl_config_write_done(io); 818 retval = CTL_RETVAL_COMPLETE; 819 break; 820 } 821 822 return (retval); 823 } 824 825 static uint64_t 826 ctl_backend_ramdisk_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname) 827 { 828 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun; 829 uint64_t val; 830 831 val = UINT64_MAX; 832 if (be_lun->cap_bytes == 0) 833 return (val); 834 sx_slock(&be_lun->page_lock); 835 if (strcmp(attrname, "blocksused") == 0) { 836 val = be_lun->cap_used / be_lun->cbe_lun.blocksize; 837 } else if (strcmp(attrname, "blocksavail") == 0) { 838 val = (be_lun->cap_bytes - be_lun->cap_used) / 839 be_lun->cbe_lun.blocksize; 840 } 841 sx_sunlock(&be_lun->page_lock); 842 return (val); 843 } 844 845 static int 846 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 847 int flag, struct thread *td) 848 { 849 struct ctl_be_ramdisk_softc *softc = &rd_softc; 850 struct ctl_lun_req *lun_req; 851 int retval; 852 853 retval = 0; 854 switch (cmd) { 855 case CTL_LUN_REQ: 856 lun_req = (struct ctl_lun_req *)addr; 857 switch (lun_req->reqtype) { 858 case CTL_LUNREQ_CREATE: 859 retval = ctl_backend_ramdisk_create(softc, lun_req); 860 break; 861 case CTL_LUNREQ_RM: 862 retval = ctl_backend_ramdisk_rm(softc, lun_req); 863 break; 864 case CTL_LUNREQ_MODIFY: 865 retval = ctl_backend_ramdisk_modify(softc, lun_req); 866 break; 867 default: 868 lun_req->status = CTL_LUN_ERROR; 869 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 870 "%s: invalid LUN request type %d", __func__, 871 lun_req->reqtype); 872 break; 873 } 874 break; 875 default: 876 retval = ENOTTY; 877 break; 878 } 879 880 return (retval); 881 } 882 883 static int 884 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, 885 struct ctl_lun_req *req) 886 { 887 struct ctl_be_ramdisk_lun *be_lun; 888 struct ctl_lun_rm_params *params; 889 int retval; 890 891 params = &req->reqdata.rm; 892 sx_xlock(&softc->modify_lock); 893 mtx_lock(&softc->lock); 894 SLIST_FOREACH(be_lun, &softc->lun_list, links) { 895 if (be_lun->cbe_lun.lun_id == params->lun_id) { 896 SLIST_REMOVE(&softc->lun_list, be_lun, 897 ctl_be_ramdisk_lun, links); 898 softc->num_luns--; 899 break; 900 } 901 } 902 mtx_unlock(&softc->lock); 903 sx_xunlock(&softc->modify_lock); 904 if (be_lun == NULL) { 905 snprintf(req->error_str, sizeof(req->error_str), 906 "%s: LUN %u is not managed by the ramdisk backend", 907 __func__, params->lun_id); 908 goto bailout_error; 909 } 910 911 /* 912 * Set the waiting flag before we invalidate the LUN. Our shutdown 913 * routine can be called any time after we invalidate the LUN, 914 * and can be called from our context. 915 * 916 * This tells the shutdown routine that we're waiting, or we're 917 * going to wait for the shutdown to happen. 918 */ 919 mtx_lock(&softc->lock); 920 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING; 921 mtx_unlock(&softc->lock); 922 923 retval = ctl_remove_lun(&be_lun->cbe_lun); 924 if (retval != 0) { 925 snprintf(req->error_str, sizeof(req->error_str), 926 "%s: error %d returned from ctl_remove_lun() for " 927 "LUN %d", __func__, retval, params->lun_id); 928 mtx_lock(&softc->lock); 929 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 930 mtx_unlock(&softc->lock); 931 goto bailout_error; 932 } 933 934 mtx_lock(&softc->lock); 935 while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) { 936 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlramrm", 0); 937 if (retval == EINTR) 938 break; 939 } 940 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 941 if (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) { 942 mtx_unlock(&softc->lock); 943 free(be_lun, M_RAMDISK); 944 } else { 945 mtx_unlock(&softc->lock); 946 return (EINTR); 947 } 948 949 req->status = CTL_LUN_OK; 950 return (retval); 951 952 bailout_error: 953 req->status = CTL_LUN_ERROR; 954 return (0); 955 } 956 957 static int 958 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, 959 struct ctl_lun_req *req) 960 { 961 struct ctl_be_ramdisk_lun *be_lun; 962 struct ctl_be_lun *cbe_lun; 963 struct ctl_lun_create_params *params; 964 const char *value; 965 char tmpstr[32]; 966 uint64_t t; 967 int retval; 968 969 retval = 0; 970 params = &req->reqdata.create; 971 972 be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK); 973 cbe_lun = &be_lun->cbe_lun; 974 cbe_lun->options = nvlist_clone(req->args_nvl); 975 be_lun->params = req->reqdata.create; 976 be_lun->softc = softc; 977 978 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 979 cbe_lun->lun_type = params->device_type; 980 else 981 cbe_lun->lun_type = T_DIRECT; 982 be_lun->flags = 0; 983 cbe_lun->flags = 0; 984 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 985 if (value != NULL) { 986 if (strcmp(value, "primary") == 0) 987 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 988 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 989 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 990 991 be_lun->pblocksize = PAGE_SIZE; 992 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL); 993 if (value != NULL) { 994 ctl_expand_number(value, &t); 995 be_lun->pblocksize = t; 996 } 997 if (be_lun->pblocksize < 512 || be_lun->pblocksize > 131072) { 998 snprintf(req->error_str, sizeof(req->error_str), 999 "%s: unsupported pblocksize %u", __func__, 1000 be_lun->pblocksize); 1001 goto bailout_error; 1002 } 1003 1004 if (cbe_lun->lun_type == T_DIRECT || 1005 cbe_lun->lun_type == T_CDROM) { 1006 if (params->blocksize_bytes != 0) 1007 cbe_lun->blocksize = params->blocksize_bytes; 1008 else if (cbe_lun->lun_type == T_CDROM) 1009 cbe_lun->blocksize = 2048; 1010 else 1011 cbe_lun->blocksize = 512; 1012 be_lun->pblockmul = be_lun->pblocksize / cbe_lun->blocksize; 1013 if (be_lun->pblockmul < 1 || !powerof2(be_lun->pblockmul)) { 1014 snprintf(req->error_str, sizeof(req->error_str), 1015 "%s: pblocksize %u not exp2 of blocksize %u", 1016 __func__, 1017 be_lun->pblocksize, cbe_lun->blocksize); 1018 goto bailout_error; 1019 } 1020 if (params->lun_size_bytes < cbe_lun->blocksize) { 1021 snprintf(req->error_str, sizeof(req->error_str), 1022 "%s: LUN size %ju < blocksize %u", __func__, 1023 params->lun_size_bytes, cbe_lun->blocksize); 1024 goto bailout_error; 1025 } 1026 be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize; 1027 be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize; 1028 be_lun->indir = 0; 1029 t = be_lun->size_bytes / be_lun->pblocksize; 1030 while (t > 1) { 1031 t /= PPP; 1032 be_lun->indir++; 1033 } 1034 cbe_lun->maxlba = be_lun->size_blocks - 1; 1035 cbe_lun->pblockexp = fls(be_lun->pblockmul) - 1; 1036 cbe_lun->pblockoff = 0; 1037 cbe_lun->ublockexp = cbe_lun->pblockexp; 1038 cbe_lun->ublockoff = 0; 1039 cbe_lun->atomicblock = be_lun->pblocksize; 1040 cbe_lun->opttxferlen = SGPP * be_lun->pblocksize; 1041 value = dnvlist_get_string(cbe_lun->options, "capacity", NULL); 1042 if (value != NULL) 1043 ctl_expand_number(value, &be_lun->cap_bytes); 1044 } else { 1045 be_lun->pblockmul = 1; 1046 cbe_lun->pblockexp = 0; 1047 } 1048 1049 /* Tell the user the blocksize we ended up using */ 1050 params->blocksize_bytes = cbe_lun->blocksize; 1051 params->lun_size_bytes = be_lun->size_bytes; 1052 1053 value = dnvlist_get_string(cbe_lun->options, "unmap", NULL); 1054 if (value == NULL || strcmp(value, "off") != 0) 1055 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; 1056 value = dnvlist_get_string(cbe_lun->options, "readonly", NULL); 1057 if (value != NULL) { 1058 if (strcmp(value, "on") == 0) 1059 cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 1060 } else if (cbe_lun->lun_type != T_DIRECT) 1061 cbe_lun->flags |= CTL_LUN_FLAG_READONLY; 1062 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 1063 value = dnvlist_get_string(cbe_lun->options, "serseq", NULL); 1064 if (value != NULL && strcmp(value, "on") == 0) 1065 cbe_lun->serseq = CTL_LUN_SERSEQ_ON; 1066 else if (value != NULL && strcmp(value, "read") == 0) 1067 cbe_lun->serseq = CTL_LUN_SERSEQ_READ; 1068 else if (value != NULL && strcmp(value, "off") == 0) 1069 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; 1070 1071 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 1072 cbe_lun->req_lun_id = params->req_lun_id; 1073 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; 1074 } else 1075 cbe_lun->req_lun_id = 0; 1076 1077 cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown; 1078 cbe_lun->be = &ctl_be_ramdisk_driver; 1079 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 1080 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d", 1081 softc->num_luns); 1082 strncpy((char *)cbe_lun->serial_num, tmpstr, 1083 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); 1084 1085 /* Tell the user what we used for a serial number */ 1086 strncpy((char *)params->serial_num, tmpstr, 1087 MIN(sizeof(params->serial_num), sizeof(tmpstr))); 1088 } else { 1089 strncpy((char *)cbe_lun->serial_num, params->serial_num, 1090 MIN(sizeof(cbe_lun->serial_num), 1091 sizeof(params->serial_num))); 1092 } 1093 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 1094 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns); 1095 strncpy((char *)cbe_lun->device_id, tmpstr, 1096 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); 1097 1098 /* Tell the user what we used for a device ID */ 1099 strncpy((char *)params->device_id, tmpstr, 1100 MIN(sizeof(params->device_id), sizeof(tmpstr))); 1101 } else { 1102 strncpy((char *)cbe_lun->device_id, params->device_id, 1103 MIN(sizeof(cbe_lun->device_id), 1104 sizeof(params->device_id))); 1105 } 1106 1107 STAILQ_INIT(&be_lun->cont_queue); 1108 sx_init(&be_lun->page_lock, "ctlram page"); 1109 if (be_lun->cap_bytes == 0) { 1110 be_lun->indir = 0; 1111 be_lun->pages = malloc(be_lun->pblocksize, M_RAMDISK, M_WAITOK); 1112 } 1113 be_lun->zero_page = malloc(be_lun->pblocksize, M_RAMDISK, 1114 M_WAITOK|M_ZERO); 1115 mtx_init(&be_lun->queue_lock, "ctlram queue", NULL, MTX_DEF); 1116 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker, 1117 be_lun); 1118 1119 be_lun->io_taskqueue = taskqueue_create("ctlramtq", M_WAITOK, 1120 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 1121 if (be_lun->io_taskqueue == NULL) { 1122 snprintf(req->error_str, sizeof(req->error_str), 1123 "%s: Unable to create taskqueue", __func__); 1124 goto bailout_error; 1125 } 1126 1127 retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue, 1128 /*num threads*/1, 1129 /*priority*/PUSER, 1130 /*proc*/control_softc->ctl_proc, 1131 /*thread name*/"ramdisk"); 1132 if (retval != 0) 1133 goto bailout_error; 1134 1135 retval = ctl_add_lun(&be_lun->cbe_lun); 1136 if (retval != 0) { 1137 snprintf(req->error_str, sizeof(req->error_str), 1138 "%s: ctl_add_lun() returned error %d, see dmesg for " 1139 "details", __func__, retval); 1140 retval = 0; 1141 goto bailout_error; 1142 } 1143 1144 mtx_lock(&softc->lock); 1145 softc->num_luns++; 1146 SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links); 1147 mtx_unlock(&softc->lock); 1148 1149 params->req_lun_id = cbe_lun->lun_id; 1150 1151 req->status = CTL_LUN_OK; 1152 return (retval); 1153 1154 bailout_error: 1155 req->status = CTL_LUN_ERROR; 1156 if (be_lun != NULL) { 1157 if (be_lun->io_taskqueue != NULL) 1158 taskqueue_free(be_lun->io_taskqueue); 1159 nvlist_destroy(cbe_lun->options); 1160 free(be_lun->zero_page, M_RAMDISK); 1161 ctl_backend_ramdisk_freeallpages(be_lun->pages, be_lun->indir); 1162 sx_destroy(&be_lun->page_lock); 1163 mtx_destroy(&be_lun->queue_lock); 1164 free(be_lun, M_RAMDISK); 1165 } 1166 return (retval); 1167 } 1168 1169 static int 1170 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, 1171 struct ctl_lun_req *req) 1172 { 1173 struct ctl_be_ramdisk_lun *be_lun; 1174 struct ctl_be_lun *cbe_lun; 1175 struct ctl_lun_modify_params *params; 1176 const char *value; 1177 uint32_t blocksize; 1178 int wasprim; 1179 1180 params = &req->reqdata.modify; 1181 sx_xlock(&softc->modify_lock); 1182 mtx_lock(&softc->lock); 1183 SLIST_FOREACH(be_lun, &softc->lun_list, links) { 1184 if (be_lun->cbe_lun.lun_id == params->lun_id) 1185 break; 1186 } 1187 mtx_unlock(&softc->lock); 1188 if (be_lun == NULL) { 1189 snprintf(req->error_str, sizeof(req->error_str), 1190 "%s: LUN %u is not managed by the ramdisk backend", 1191 __func__, params->lun_id); 1192 goto bailout_error; 1193 } 1194 cbe_lun = &be_lun->cbe_lun; 1195 1196 if (params->lun_size_bytes != 0) 1197 be_lun->params.lun_size_bytes = params->lun_size_bytes; 1198 1199 if (req->args_nvl != NULL) { 1200 nvlist_destroy(cbe_lun->options); 1201 cbe_lun->options = nvlist_clone(req->args_nvl); 1202 } 1203 1204 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); 1205 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL); 1206 if (value != NULL) { 1207 if (strcmp(value, "primary") == 0) 1208 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 1209 else 1210 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 1211 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) 1212 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; 1213 else 1214 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; 1215 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { 1216 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) 1217 ctl_lun_primary(cbe_lun); 1218 else 1219 ctl_lun_secondary(cbe_lun); 1220 } 1221 1222 blocksize = be_lun->cbe_lun.blocksize; 1223 if (be_lun->params.lun_size_bytes < blocksize) { 1224 snprintf(req->error_str, sizeof(req->error_str), 1225 "%s: LUN size %ju < blocksize %u", __func__, 1226 be_lun->params.lun_size_bytes, blocksize); 1227 goto bailout_error; 1228 } 1229 be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize; 1230 be_lun->size_bytes = be_lun->size_blocks * blocksize; 1231 be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1; 1232 ctl_lun_capacity_changed(&be_lun->cbe_lun); 1233 1234 /* Tell the user the exact size we ended up using */ 1235 params->lun_size_bytes = be_lun->size_bytes; 1236 1237 sx_xunlock(&softc->modify_lock); 1238 req->status = CTL_LUN_OK; 1239 return (0); 1240 1241 bailout_error: 1242 sx_xunlock(&softc->modify_lock); 1243 req->status = CTL_LUN_ERROR; 1244 return (0); 1245 } 1246 1247 static void 1248 ctl_backend_ramdisk_lun_shutdown(struct ctl_be_lun *cbe_lun) 1249 { 1250 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun; 1251 struct ctl_be_ramdisk_softc *softc = be_lun->softc; 1252 1253 taskqueue_drain_all(be_lun->io_taskqueue); 1254 taskqueue_free(be_lun->io_taskqueue); 1255 nvlist_destroy(be_lun->cbe_lun.options); 1256 free(be_lun->zero_page, M_RAMDISK); 1257 ctl_backend_ramdisk_freeallpages(be_lun->pages, be_lun->indir); 1258 sx_destroy(&be_lun->page_lock); 1259 mtx_destroy(&be_lun->queue_lock); 1260 1261 mtx_lock(&softc->lock); 1262 be_lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED; 1263 if (be_lun->flags & CTL_BE_RAMDISK_LUN_WAITING) 1264 wakeup(be_lun); 1265 else 1266 free(be_lun, M_RAMDISK); 1267 mtx_unlock(&softc->lock); 1268 } 1269