ctl_backend_ramdisk.c revision 268150
1/*- 2 * Copyright (c) 2003, 2008 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $ 35 */ 36/* 37 * CAM Target Layer backend for a "fake" ramdisk. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#include <sys/cdefs.h> 43__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_backend_ramdisk.c 268150 2014-07-02 10:43:53Z mav $"); 44 45#include <sys/param.h> 46#include <sys/systm.h> 47#include <sys/kernel.h> 48#include <sys/condvar.h> 49#include <sys/types.h> 50#include <sys/lock.h> 51#include <sys/mutex.h> 52#include <sys/malloc.h> 53#include <sys/taskqueue.h> 54#include <sys/time.h> 55#include <sys/queue.h> 56#include <sys/conf.h> 57#include <sys/ioccom.h> 58#include <sys/module.h> 59 60#include <cam/scsi/scsi_all.h> 61#include <cam/ctl/ctl_io.h> 62#include <cam/ctl/ctl.h> 63#include <cam/ctl/ctl_util.h> 64#include <cam/ctl/ctl_backend.h> 65#include <cam/ctl/ctl_frontend_internal.h> 66#include <cam/ctl/ctl_debug.h> 67#include <cam/ctl/ctl_ioctl.h> 68#include <cam/ctl/ctl_error.h> 69 70typedef enum { 71 CTL_BE_RAMDISK_LUN_UNCONFIGURED = 0x01, 72 CTL_BE_RAMDISK_LUN_CONFIG_ERR = 0x02, 73 CTL_BE_RAMDISK_LUN_WAITING = 0x04 74} ctl_be_ramdisk_lun_flags; 75 76struct ctl_be_ramdisk_lun { 77 char lunname[32]; 78 uint64_t size_bytes; 79 uint64_t size_blocks; 80 struct ctl_be_ramdisk_softc *softc; 81 ctl_be_ramdisk_lun_flags flags; 82 STAILQ_ENTRY(ctl_be_ramdisk_lun) links; 83 struct ctl_be_lun ctl_be_lun; 84 struct taskqueue *io_taskqueue; 85 struct task io_task; 86 STAILQ_HEAD(, ctl_io_hdr) cont_queue; 87 struct mtx lock; 88}; 89 90struct ctl_be_ramdisk_softc { 91 struct mtx lock; 92 int rd_size; 93#ifdef CTL_RAMDISK_PAGES 94 uint8_t **ramdisk_pages; 95 int num_pages; 96#else 97 uint8_t *ramdisk_buffer; 98#endif 99 int num_luns; 100 STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list; 101}; 102 103static struct ctl_be_ramdisk_softc rd_softc; 104 105int ctl_backend_ramdisk_init(void); 106void ctl_backend_ramdisk_shutdown(void); 107static int ctl_backend_ramdisk_move_done(union ctl_io *io); 108static int ctl_backend_ramdisk_submit(union ctl_io *io); 109static void ctl_backend_ramdisk_continue(union ctl_io *io); 110static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, 111 caddr_t addr, int flag, struct thread *td); 112static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, 113 struct ctl_lun_req *req); 114static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, 115 struct ctl_lun_req *req, int do_wait); 116static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, 117 struct ctl_lun_req *req); 118static void ctl_backend_ramdisk_worker(void *context, int pending); 119static void ctl_backend_ramdisk_lun_shutdown(void *be_lun); 120static void ctl_backend_ramdisk_lun_config_status(void *be_lun, 121 ctl_lun_config_status status); 122static int ctl_backend_ramdisk_config_write(union ctl_io *io); 123static int ctl_backend_ramdisk_config_read(union ctl_io *io); 124 125static struct ctl_backend_driver ctl_be_ramdisk_driver = 126{ 127 .name = "ramdisk", 128 .flags = CTL_BE_FLAG_HAS_CONFIG, 129 .init = ctl_backend_ramdisk_init, 130 .data_submit = ctl_backend_ramdisk_submit, 131 .data_move_done = ctl_backend_ramdisk_move_done, 132 .config_read = ctl_backend_ramdisk_config_read, 133 .config_write = ctl_backend_ramdisk_config_write, 134 .ioctl = ctl_backend_ramdisk_ioctl 135}; 136 137MALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk"); 138CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver); 139 140int 141ctl_backend_ramdisk_init(void) 142{ 143 struct ctl_be_ramdisk_softc *softc; 144#ifdef CTL_RAMDISK_PAGES 145 int i; 146#endif 147 148 149 softc = &rd_softc; 150 151 memset(softc, 0, sizeof(*softc)); 152 153 mtx_init(&softc->lock, "ramdisk", NULL, MTX_DEF); 154 155 STAILQ_INIT(&softc->lun_list); 156 softc->rd_size = 1024 * 1024; 157#ifdef CTL_RAMDISK_PAGES 158 softc->num_pages = softc->rd_size / PAGE_SIZE; 159 softc->ramdisk_pages = (uint8_t **)malloc(sizeof(uint8_t *) * 160 softc->num_pages, M_RAMDISK, 161 M_WAITOK); 162 for (i = 0; i < softc->num_pages; i++) 163 softc->ramdisk_pages[i] = malloc(PAGE_SIZE, M_RAMDISK,M_WAITOK); 164#else 165 softc->ramdisk_buffer = (uint8_t *)malloc(softc->rd_size, M_RAMDISK, 166 M_WAITOK); 167#endif 168 169 return (0); 170} 171 172void 173ctl_backend_ramdisk_shutdown(void) 174{ 175 struct ctl_be_ramdisk_softc *softc; 176 struct ctl_be_ramdisk_lun *lun, *next_lun; 177#ifdef CTL_RAMDISK_PAGES 178 int i; 179#endif 180 181 softc = &rd_softc; 182 183 mtx_lock(&softc->lock); 184 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 185 /* 186 * Grab the next LUN. The current LUN may get removed by 187 * ctl_invalidate_lun(), which will call our LUN shutdown 188 * routine, if there is no outstanding I/O for this LUN. 189 */ 190 next_lun = STAILQ_NEXT(lun, links); 191 192 /* 193 * Drop our lock here. Since ctl_invalidate_lun() can call 194 * back into us, this could potentially lead to a recursive 195 * lock of the same mutex, which would cause a hang. 196 */ 197 mtx_unlock(&softc->lock); 198 ctl_disable_lun(&lun->ctl_be_lun); 199 ctl_invalidate_lun(&lun->ctl_be_lun); 200 mtx_lock(&softc->lock); 201 } 202 mtx_unlock(&softc->lock); 203 204#ifdef CTL_RAMDISK_PAGES 205 for (i = 0; i < softc->num_pages; i++) 206 free(softc->ramdisk_pages[i], M_RAMDISK); 207 208 free(softc->ramdisk_pages, M_RAMDISK); 209#else 210 free(softc->ramdisk_buffer, M_RAMDISK); 211#endif 212 213 if (ctl_backend_deregister(&ctl_be_ramdisk_driver) != 0) { 214 printf("ctl_backend_ramdisk_shutdown: " 215 "ctl_backend_deregister() failed!\n"); 216 } 217} 218 219static int 220ctl_backend_ramdisk_move_done(union ctl_io *io) 221{ 222 struct ctl_be_lun *ctl_be_lun; 223 struct ctl_be_ramdisk_lun *be_lun; 224#ifdef CTL_TIME_IO 225 struct bintime cur_bt; 226#endif 227 228 CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n")); 229 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 230 CTL_PRIV_BACKEND_LUN].ptr; 231 be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun; 232#ifdef CTL_TIME_IO 233 getbintime(&cur_bt); 234 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 235 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 236 io->io_hdr.num_dmas++; 237#endif 238 if (io->scsiio.kern_sg_entries > 0) 239 free(io->scsiio.kern_data_ptr, M_RAMDISK); 240 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; 241 if ((io->io_hdr.port_status == 0) 242 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 243 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) { 244 if (io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer > 0) { 245 mtx_lock(&be_lun->lock); 246 STAILQ_INSERT_TAIL(&be_lun->cont_queue, 247 &io->io_hdr, links); 248 mtx_unlock(&be_lun->lock); 249 taskqueue_enqueue(be_lun->io_taskqueue, 250 &be_lun->io_task); 251 return (0); 252 } 253 io->io_hdr.status = CTL_SUCCESS; 254 } else if ((io->io_hdr.port_status != 0) 255 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 256 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 257 /* 258 * For hardware error sense keys, the sense key 259 * specific value is defined to be a retry count, 260 * but we use it to pass back an internal FETD 261 * error code. XXX KDM Hopefully the FETD is only 262 * using 16 bits for an error code, since that's 263 * all the space we have in the sks field. 264 */ 265 ctl_set_internal_failure(&io->scsiio, 266 /*sks_valid*/ 1, 267 /*retry_count*/ 268 io->io_hdr.port_status); 269 } 270 ctl_done(io); 271 return(0); 272} 273 274static int 275ctl_backend_ramdisk_submit(union ctl_io *io) 276{ 277 struct ctl_be_lun *ctl_be_lun; 278 struct ctl_lba_len *lbalen; 279 280 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 281 CTL_PRIV_BACKEND_LUN].ptr; 282 lbalen = (struct ctl_lba_len *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 283 io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer = 284 lbalen->len * ctl_be_lun->blocksize; 285 ctl_backend_ramdisk_continue(io); 286 return (CTL_RETVAL_COMPLETE); 287} 288 289static void 290ctl_backend_ramdisk_continue(union ctl_io *io) 291{ 292 struct ctl_be_ramdisk_softc *softc; 293 int len, len_filled, sg_filled; 294#ifdef CTL_RAMDISK_PAGES 295 struct ctl_sg_entry *sg_entries; 296 int i; 297#endif 298 299 softc = &rd_softc; 300 len = io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer; 301#ifdef CTL_RAMDISK_PAGES 302 sg_filled = min(btoc(len), softc->num_pages); 303 if (sg_filled > 1) { 304 io->scsiio.kern_data_ptr = malloc(sizeof(struct ctl_sg_entry) * 305 sg_filled, M_RAMDISK, 306 M_WAITOK); 307 sg_entries = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 308 for (i = 0, len_filled = 0; i < sg_filled; i++) { 309 sg_entries[i].addr = softc->ramdisk_pages[i]; 310 sg_entries[i].len = ctl_min(PAGE_SIZE, 311 len - len_filled); 312 len_filled += sg_entries[i].len; 313 } 314 io->io_hdr.flags |= CTL_FLAG_KDPTR_SGLIST; 315 } else { 316 sg_filled = 0; 317 len_filled = len; 318 io->scsiio.kern_data_ptr = softc->ramdisk_pages[0]; 319 } 320#else 321 sg_filled = 0; 322 len_filled = min(len, softc->rd_size); 323 io->scsiio.kern_data_ptr = softc->ramdisk_buffer; 324#endif /* CTL_RAMDISK_PAGES */ 325 326 io->scsiio.be_move_done = ctl_backend_ramdisk_move_done; 327 io->scsiio.kern_data_resid = 0; 328 io->scsiio.kern_data_len = len_filled; 329 io->scsiio.kern_sg_entries = sg_filled; 330 io->io_hdr.flags |= CTL_FLAG_ALLOCATED; 331 io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled; 332#ifdef CTL_TIME_IO 333 getbintime(&io->io_hdr.dma_start_bt); 334#endif 335 ctl_datamove(io); 336} 337 338static void 339ctl_backend_ramdisk_worker(void *context, int pending) 340{ 341 struct ctl_be_ramdisk_softc *softc; 342 struct ctl_be_ramdisk_lun *be_lun; 343 union ctl_io *io; 344 345 be_lun = (struct ctl_be_ramdisk_lun *)context; 346 softc = be_lun->softc; 347 348 mtx_lock(&be_lun->lock); 349 for (;;) { 350 io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue); 351 if (io != NULL) { 352 STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr, 353 ctl_io_hdr, links); 354 355 mtx_unlock(&be_lun->lock); 356 357 ctl_backend_ramdisk_continue(io); 358 359 mtx_lock(&be_lun->lock); 360 continue; 361 } 362 363 /* 364 * If we get here, there is no work left in the queues, so 365 * just break out and let the task queue go to sleep. 366 */ 367 break; 368 } 369 mtx_unlock(&be_lun->lock); 370} 371 372static int 373ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, 374 int flag, struct thread *td) 375{ 376 struct ctl_be_ramdisk_softc *softc; 377 int retval; 378 379 retval = 0; 380 softc = &rd_softc; 381 382 switch (cmd) { 383 case CTL_LUN_REQ: { 384 struct ctl_lun_req *lun_req; 385 386 lun_req = (struct ctl_lun_req *)addr; 387 388 switch (lun_req->reqtype) { 389 case CTL_LUNREQ_CREATE: 390 retval = ctl_backend_ramdisk_create(softc, lun_req, 391 /*do_wait*/ 1); 392 break; 393 case CTL_LUNREQ_RM: 394 retval = ctl_backend_ramdisk_rm(softc, lun_req); 395 break; 396 case CTL_LUNREQ_MODIFY: 397 retval = ctl_backend_ramdisk_modify(softc, lun_req); 398 break; 399 default: 400 lun_req->status = CTL_LUN_ERROR; 401 snprintf(lun_req->error_str, sizeof(lun_req->error_str), 402 "%s: invalid LUN request type %d", __func__, 403 lun_req->reqtype); 404 break; 405 } 406 break; 407 } 408 default: 409 retval = ENOTTY; 410 break; 411 } 412 413 return (retval); 414} 415 416static int 417ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, 418 struct ctl_lun_req *req) 419{ 420 struct ctl_be_ramdisk_lun *be_lun; 421 struct ctl_lun_rm_params *params; 422 int retval; 423 424 425 retval = 0; 426 params = &req->reqdata.rm; 427 428 be_lun = NULL; 429 430 mtx_lock(&softc->lock); 431 432 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 433 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 434 break; 435 } 436 mtx_unlock(&softc->lock); 437 438 if (be_lun == NULL) { 439 snprintf(req->error_str, sizeof(req->error_str), 440 "%s: LUN %u is not managed by the ramdisk backend", 441 __func__, params->lun_id); 442 goto bailout_error; 443 } 444 445 retval = ctl_disable_lun(&be_lun->ctl_be_lun); 446 447 if (retval != 0) { 448 snprintf(req->error_str, sizeof(req->error_str), 449 "%s: error %d returned from ctl_disable_lun() for " 450 "LUN %d", __func__, retval, params->lun_id); 451 goto bailout_error; 452 } 453 454 /* 455 * Set the waiting flag before we invalidate the LUN. Our shutdown 456 * routine can be called any time after we invalidate the LUN, 457 * and can be called from our context. 458 * 459 * This tells the shutdown routine that we're waiting, or we're 460 * going to wait for the shutdown to happen. 461 */ 462 mtx_lock(&softc->lock); 463 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING; 464 mtx_unlock(&softc->lock); 465 466 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); 467 if (retval != 0) { 468 snprintf(req->error_str, sizeof(req->error_str), 469 "%s: error %d returned from ctl_invalidate_lun() for " 470 "LUN %d", __func__, retval, params->lun_id); 471 mtx_lock(&softc->lock); 472 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 473 mtx_unlock(&softc->lock); 474 goto bailout_error; 475 } 476 477 mtx_lock(&softc->lock); 478 479 while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) { 480 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0); 481 if (retval == EINTR) 482 break; 483 } 484 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 485 486 /* 487 * We only remove this LUN from the list and free it (below) if 488 * retval == 0. If the user interrupted the wait, we just bail out 489 * without actually freeing the LUN. We let the shutdown routine 490 * free the LUN if that happens. 491 */ 492 if (retval == 0) { 493 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 494 links); 495 softc->num_luns--; 496 } 497 498 mtx_unlock(&softc->lock); 499 500 if (retval == 0) { 501 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); 502 taskqueue_free(be_lun->io_taskqueue); 503 ctl_free_opts(&be_lun->ctl_be_lun); 504 mtx_destroy(&be_lun->lock); 505 free(be_lun, M_RAMDISK); 506 } 507 508 req->status = CTL_LUN_OK; 509 510 return (retval); 511 512bailout_error: 513 req->status = CTL_LUN_ERROR; 514 515 return (0); 516} 517 518static int 519ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, 520 struct ctl_lun_req *req, int do_wait) 521{ 522 struct ctl_be_ramdisk_lun *be_lun; 523 struct ctl_lun_create_params *params; 524 uint32_t blocksize; 525 char *value; 526 char tmpstr[32]; 527 int retval, unmap; 528 529 retval = 0; 530 params = &req->reqdata.create; 531 if (params->blocksize_bytes != 0) 532 blocksize = params->blocksize_bytes; 533 else 534 blocksize = 512; 535 536 be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ? 537 M_WAITOK : M_NOWAIT)); 538 539 if (be_lun == NULL) { 540 snprintf(req->error_str, sizeof(req->error_str), 541 "%s: error allocating %zd bytes", __func__, 542 sizeof(*be_lun)); 543 goto bailout_error; 544 } 545 sprintf(be_lun->lunname, "cram%d", softc->num_luns); 546 ctl_init_opts(&be_lun->ctl_be_lun, req); 547 548 if (params->flags & CTL_LUN_FLAG_DEV_TYPE) 549 be_lun->ctl_be_lun.lun_type = params->device_type; 550 else 551 be_lun->ctl_be_lun.lun_type = T_DIRECT; 552 553 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { 554 555 if (params->lun_size_bytes < blocksize) { 556 snprintf(req->error_str, sizeof(req->error_str), 557 "%s: LUN size %ju < blocksize %u", __func__, 558 params->lun_size_bytes, blocksize); 559 goto bailout_error; 560 } 561 562 be_lun->size_blocks = params->lun_size_bytes / blocksize; 563 be_lun->size_bytes = be_lun->size_blocks * blocksize; 564 565 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 566 } else { 567 be_lun->ctl_be_lun.maxlba = 0; 568 blocksize = 0; 569 be_lun->size_bytes = 0; 570 be_lun->size_blocks = 0; 571 } 572 573 be_lun->ctl_be_lun.blocksize = blocksize; 574 575 /* Tell the user the blocksize we ended up using */ 576 params->blocksize_bytes = blocksize; 577 578 /* Tell the user the exact size we ended up using */ 579 params->lun_size_bytes = be_lun->size_bytes; 580 581 be_lun->softc = softc; 582 583 unmap = 0; 584 value = ctl_get_opt(&be_lun->ctl_be_lun, "unmap"); 585 if (value != NULL && strcmp(value, "on") == 0) 586 unmap = 1; 587 588 be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED; 589 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; 590 if (unmap) 591 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; 592 be_lun->ctl_be_lun.be_lun = be_lun; 593 594 if (params->flags & CTL_LUN_FLAG_ID_REQ) { 595 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; 596 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; 597 } else 598 be_lun->ctl_be_lun.req_lun_id = 0; 599 600 be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown; 601 be_lun->ctl_be_lun.lun_config_status = 602 ctl_backend_ramdisk_lun_config_status; 603 be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver; 604 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { 605 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", 606 softc->num_luns); 607 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, 608 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 609 sizeof(tmpstr))); 610 611 /* Tell the user what we used for a serial number */ 612 strncpy((char *)params->serial_num, tmpstr, 613 ctl_min(sizeof(params->serial_num), sizeof(tmpstr))); 614 } else { 615 strncpy((char *)be_lun->ctl_be_lun.serial_num, 616 params->serial_num, 617 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num), 618 sizeof(params->serial_num))); 619 } 620 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { 621 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); 622 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, 623 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 624 sizeof(tmpstr))); 625 626 /* Tell the user what we used for a device ID */ 627 strncpy((char *)params->device_id, tmpstr, 628 ctl_min(sizeof(params->device_id), sizeof(tmpstr))); 629 } else { 630 strncpy((char *)be_lun->ctl_be_lun.device_id, 631 params->device_id, 632 ctl_min(sizeof(be_lun->ctl_be_lun.device_id), 633 sizeof(params->device_id))); 634 } 635 636 STAILQ_INIT(&be_lun->cont_queue); 637 mtx_init(&be_lun->lock, "CTL ramdisk", NULL, MTX_DEF); 638 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker, 639 be_lun); 640 641 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK, 642 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue); 643 if (be_lun->io_taskqueue == NULL) { 644 snprintf(req->error_str, sizeof(req->error_str), 645 "%s: Unable to create taskqueue", __func__); 646 goto bailout_error; 647 } 648 649 retval = taskqueue_start_threads(&be_lun->io_taskqueue, 650 /*num threads*/1, 651 /*priority*/PWAIT, 652 /*thread name*/ 653 "%s taskq", be_lun->lunname); 654 if (retval != 0) 655 goto bailout_error; 656 657 mtx_lock(&softc->lock); 658 softc->num_luns++; 659 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); 660 661 mtx_unlock(&softc->lock); 662 663 retval = ctl_add_lun(&be_lun->ctl_be_lun); 664 if (retval != 0) { 665 mtx_lock(&softc->lock); 666 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 667 links); 668 softc->num_luns--; 669 mtx_unlock(&softc->lock); 670 snprintf(req->error_str, sizeof(req->error_str), 671 "%s: ctl_add_lun() returned error %d, see dmesg for " 672 "details", __func__, retval); 673 retval = 0; 674 goto bailout_error; 675 } 676 677 if (do_wait == 0) 678 return (retval); 679 680 mtx_lock(&softc->lock); 681 682 /* 683 * Tell the config_status routine that we're waiting so it won't 684 * clean up the LUN in the event of an error. 685 */ 686 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING; 687 688 while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) { 689 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0); 690 if (retval == EINTR) 691 break; 692 } 693 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; 694 695 if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) { 696 snprintf(req->error_str, sizeof(req->error_str), 697 "%s: LUN configuration error, see dmesg for details", 698 __func__); 699 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 700 links); 701 softc->num_luns--; 702 mtx_unlock(&softc->lock); 703 goto bailout_error; 704 } else { 705 params->req_lun_id = be_lun->ctl_be_lun.lun_id; 706 } 707 mtx_unlock(&softc->lock); 708 709 req->status = CTL_LUN_OK; 710 711 return (retval); 712 713bailout_error: 714 req->status = CTL_LUN_ERROR; 715 if (be_lun != NULL) { 716 if (be_lun->io_taskqueue != NULL) { 717 taskqueue_free(be_lun->io_taskqueue); 718 } 719 ctl_free_opts(&be_lun->ctl_be_lun); 720 mtx_destroy(&be_lun->lock); 721 free(be_lun, M_RAMDISK); 722 } 723 724 return (retval); 725} 726 727static int 728ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, 729 struct ctl_lun_req *req) 730{ 731 struct ctl_be_ramdisk_lun *be_lun; 732 struct ctl_lun_modify_params *params; 733 uint32_t blocksize; 734 735 params = &req->reqdata.modify; 736 737 be_lun = NULL; 738 739 mtx_lock(&softc->lock); 740 STAILQ_FOREACH(be_lun, &softc->lun_list, links) { 741 if (be_lun->ctl_be_lun.lun_id == params->lun_id) 742 break; 743 } 744 mtx_unlock(&softc->lock); 745 746 if (be_lun == NULL) { 747 snprintf(req->error_str, sizeof(req->error_str), 748 "%s: LUN %u is not managed by the ramdisk backend", 749 __func__, params->lun_id); 750 goto bailout_error; 751 } 752 753 if (params->lun_size_bytes == 0) { 754 snprintf(req->error_str, sizeof(req->error_str), 755 "%s: LUN size \"auto\" not supported " 756 "by the ramdisk backend", __func__); 757 goto bailout_error; 758 } 759 760 blocksize = be_lun->ctl_be_lun.blocksize; 761 762 if (params->lun_size_bytes < blocksize) { 763 snprintf(req->error_str, sizeof(req->error_str), 764 "%s: LUN size %ju < blocksize %u", __func__, 765 params->lun_size_bytes, blocksize); 766 goto bailout_error; 767 } 768 769 be_lun->size_blocks = params->lun_size_bytes / blocksize; 770 be_lun->size_bytes = be_lun->size_blocks * blocksize; 771 772 /* 773 * The maximum LBA is the size - 1. 774 * 775 * XXX: Note that this field is being updated without locking, 776 * which might cause problems on 32-bit architectures. 777 */ 778 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; 779 ctl_lun_capacity_changed(&be_lun->ctl_be_lun); 780 781 /* Tell the user the exact size we ended up using */ 782 params->lun_size_bytes = be_lun->size_bytes; 783 784 req->status = CTL_LUN_OK; 785 786 return (0); 787 788bailout_error: 789 req->status = CTL_LUN_ERROR; 790 791 return (0); 792} 793 794static void 795ctl_backend_ramdisk_lun_shutdown(void *be_lun) 796{ 797 struct ctl_be_ramdisk_lun *lun; 798 struct ctl_be_ramdisk_softc *softc; 799 int do_free; 800 801 lun = (struct ctl_be_ramdisk_lun *)be_lun; 802 softc = lun->softc; 803 do_free = 0; 804 805 mtx_lock(&softc->lock); 806 807 lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED; 808 809 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) { 810 wakeup(lun); 811 } else { 812 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, 813 links); 814 softc->num_luns--; 815 do_free = 1; 816 } 817 818 mtx_unlock(&softc->lock); 819 820 if (do_free != 0) 821 free(be_lun, M_RAMDISK); 822} 823 824static void 825ctl_backend_ramdisk_lun_config_status(void *be_lun, 826 ctl_lun_config_status status) 827{ 828 struct ctl_be_ramdisk_lun *lun; 829 struct ctl_be_ramdisk_softc *softc; 830 831 lun = (struct ctl_be_ramdisk_lun *)be_lun; 832 softc = lun->softc; 833 834 if (status == CTL_LUN_CONFIG_OK) { 835 mtx_lock(&softc->lock); 836 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED; 837 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) 838 wakeup(lun); 839 mtx_unlock(&softc->lock); 840 841 /* 842 * We successfully added the LUN, attempt to enable it. 843 */ 844 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { 845 printf("%s: ctl_enable_lun() failed!\n", __func__); 846 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { 847 printf("%s: ctl_invalidate_lun() failed!\n", 848 __func__); 849 } 850 } 851 852 return; 853 } 854 855 856 mtx_lock(&softc->lock); 857 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED; 858 859 /* 860 * If we have a user waiting, let him handle the cleanup. If not, 861 * clean things up here. 862 */ 863 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) { 864 lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR; 865 wakeup(lun); 866 } else { 867 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun, 868 links); 869 softc->num_luns--; 870 free(lun, M_RAMDISK); 871 } 872 mtx_unlock(&softc->lock); 873} 874 875static int 876ctl_backend_ramdisk_config_write(union ctl_io *io) 877{ 878 struct ctl_be_ramdisk_softc *softc; 879 int retval; 880 881 retval = 0; 882 softc = &rd_softc; 883 884 switch (io->scsiio.cdb[0]) { 885 case SYNCHRONIZE_CACHE: 886 case SYNCHRONIZE_CACHE_16: 887 /* 888 * The upper level CTL code will filter out any CDBs with 889 * the immediate bit set and return the proper error. It 890 * will also not allow a sync cache command to go to a LUN 891 * that is powered down. 892 * 893 * We don't really need to worry about what LBA range the 894 * user asked to be synced out. When they issue a sync 895 * cache command, we'll sync out the whole thing. 896 * 897 * This is obviously just a stubbed out implementation. 898 * The real implementation will be in the RAIDCore/CTL 899 * interface, and can only really happen when RAIDCore 900 * implements a per-array cache sync. 901 */ 902 ctl_set_success(&io->scsiio); 903 ctl_config_write_done(io); 904 break; 905 case START_STOP_UNIT: { 906 struct scsi_start_stop_unit *cdb; 907 struct ctl_be_lun *ctl_be_lun; 908 struct ctl_be_ramdisk_lun *be_lun; 909 910 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; 911 912 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ 913 CTL_PRIV_BACKEND_LUN].ptr; 914 be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun; 915 916 if (cdb->how & SSS_START) 917 retval = ctl_start_lun(ctl_be_lun); 918 else { 919 retval = ctl_stop_lun(ctl_be_lun); 920#ifdef NEEDTOPORT 921 if ((retval == 0) 922 && (cdb->byte2 & SSS_ONOFFLINE)) 923 retval = ctl_lun_offline(ctl_be_lun); 924#endif 925 } 926 927 /* 928 * In general, the above routines should not fail. They 929 * just set state for the LUN. So we've got something 930 * pretty wrong here if we can't start or stop the LUN. 931 */ 932 if (retval != 0) { 933 ctl_set_internal_failure(&io->scsiio, 934 /*sks_valid*/ 1, 935 /*retry_count*/ 0xf051); 936 retval = CTL_RETVAL_COMPLETE; 937 } else { 938 ctl_set_success(&io->scsiio); 939 } 940 ctl_config_write_done(io); 941 break; 942 } 943 case WRITE_SAME_10: 944 case WRITE_SAME_16: 945 case UNMAP: 946 ctl_set_success(&io->scsiio); 947 ctl_config_write_done(io); 948 break; 949 default: 950 ctl_set_invalid_opcode(&io->scsiio); 951 ctl_config_write_done(io); 952 retval = CTL_RETVAL_COMPLETE; 953 break; 954 } 955 956 return (retval); 957} 958 959static int 960ctl_backend_ramdisk_config_read(union ctl_io *io) 961{ 962 /* 963 * XXX KDM need to implement!! 964 */ 965 return (0); 966} 967