2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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>
9 * Portions of this software were developed by Edward Tomasz Napierala
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
37 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_ramdisk.c#3 $
40 * CAM Target Layer black hole and RAM disk backend.
42 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
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>
55 #include <sys/mutex.h>
56 #include <sys/malloc.h>
58 #include <sys/taskqueue.h>
60 #include <sys/queue.h>
62 #include <sys/ioccom.h>
63 #include <sys/module.h>
64 #include <sys/sysctl.h>
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>
81 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
83 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
85 #define PPP (PAGE_SIZE / sizeof(uint8_t **))
87 #define PPPS (PAGE_SHIFT - 3)
89 #define PPPS (PAGE_SHIFT - 2)
91 #define SGPP (PAGE_SIZE / sizeof(struct ctl_sg_entry))
93 #define P_UNMAPPED NULL /* Page is unmapped. */
94 #define P_ANCHORED ((void *)(uintptr_t)1) /* Page is anchored. */
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. */
104 CTL_BE_RAMDISK_LUN_UNCONFIGURED = 0x01,
105 CTL_BE_RAMDISK_LUN_WAITING = 0x04
106 } ctl_be_ramdisk_lun_flags;
108 struct ctl_be_ramdisk_lun {
109 struct ctl_be_lun cbe_lun; /* Must be first element. */
110 struct ctl_lun_create_params params;
118 uint64_t size_blocks;
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;
126 STAILQ_HEAD(, ctl_io_hdr) cont_queue;
127 struct mtx_padalign queue_lock;
130 struct ctl_be_ramdisk_softc {
131 struct sx modify_lock;
134 SLIST_HEAD(, ctl_be_ramdisk_lun) lun_list;
137 static struct ctl_be_ramdisk_softc rd_softc;
138 extern struct ctl_softc *control_softc;
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);
160 static struct ctl_backend_driver ctl_be_ramdisk_driver =
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,
174 MALLOC_DEFINE(M_RAMDISK, "ctlramdisk", "Memory used for CTL RAMdisk");
175 CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
178 ctl_backend_ramdisk_init(void)
180 struct ctl_be_ramdisk_softc *softc = &rd_softc;
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);
190 ctl_backend_ramdisk_shutdown(void)
192 struct ctl_be_ramdisk_softc *softc = &rd_softc;
193 struct ctl_be_ramdisk_lun *lun;
195 mtx_lock(&softc->lock);
196 while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
197 SLIST_REMOVE_HEAD(&softc->lun_list, links);
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.
204 mtx_unlock(&softc->lock);
205 ctl_remove_lun(&lun->cbe_lun);
206 mtx_lock(&softc->lock);
208 mtx_unlock(&softc->lock);
209 mtx_destroy(&softc->lock);
210 sx_destroy(&softc->modify_lock);
215 ctl_backend_ramdisk_getpage(struct ctl_be_ramdisk_lun *be_lun, off_t pn,
222 if (be_lun->cap_bytes == 0) {
225 return (be_lun->zero_page);
227 return ((uint8_t *)be_lun->pages);
234 if (op == GP_WRITE || op == GP_ANCHOR) {
235 sx_xlock(&be_lun->page_lock);
237 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) {
239 *pp = malloc(PAGE_SIZE, M_RAMDISK,
243 pp = (uint8_t ***)&(*pp)[i];
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,
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,
257 sx_xunlock(&be_lun->page_lock);
258 return ((uint8_t *)*pp);
260 sx_slock(&be_lun->page_lock);
262 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) {
266 p = (uint8_t **)p[i];
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);
277 ctl_backend_ramdisk_unmappage(struct ctl_be_ramdisk_lun *be_lun, off_t pn)
283 if (be_lun->cap_bytes == 0)
285 sx_xlock(&be_lun->page_lock);
287 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) {
291 pp = (uint8_t ***)&(*pp)[i];
294 if (*pp == P_ANCHORED) {
295 be_lun->cap_used -= be_lun->pblocksize;
297 } else if (*pp != P_UNMAPPED) {
298 free(*pp, M_RAMDISK);
299 be_lun->cap_used -= be_lun->pblocksize;
303 sx_xunlock(&be_lun->page_lock);
307 ctl_backend_ramdisk_anchorpage(struct ctl_be_ramdisk_lun *be_lun, off_t pn)
313 if (be_lun->cap_bytes == 0)
315 sx_xlock(&be_lun->page_lock);
317 for (s = (be_lun->indir - 1) * PPPS; s >= 0; s -= PPPS) {
321 pp = (uint8_t ***)&(*pp)[i];
324 if (*pp == P_UNMAPPED && be_lun->cap_used < be_lun->cap_bytes) {
325 be_lun->cap_used += be_lun->pblocksize;
327 } else if (*pp != P_ANCHORED) {
328 free(*pp, M_RAMDISK);
332 sx_xunlock(&be_lun->page_lock);
336 ctl_backend_ramdisk_freeallpages(uint8_t **p, int indir)
346 for (i = 0; i < PPP; i++) {
349 ctl_backend_ramdisk_freeallpages((uint8_t **)p[i], indir - 1);
355 cmp(uint8_t *a, uint8_t *b, size_t size)
359 for (i = 0; i < size; i++) {
367 ctl_backend_ramdisk_cmp(union ctl_io *io)
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;
374 u_int lbaoff, lbas, res, off;
376 lbas = io->scsiio.kern_data_len / cbe_lun->blocksize;
377 lba = ARGS(io)->lba + PRIV(io)->len - lbas;
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,
387 if (res < cbe_lun->blocksize)
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);
405 ctl_backend_ramdisk_move_done(union ctl_io *io)
407 struct ctl_be_ramdisk_lun *be_lun =
408 (struct ctl_be_ramdisk_lun *)CTL_BACKEND_LUN(io);
410 struct bintime cur_bt;
413 CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n"));
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);
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) {
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))
442 if (ARGS(io)->len > PRIV(io)->len) {
443 mtx_lock(&be_lun->queue_lock);
444 STAILQ_INSERT_TAIL(&be_lun->cont_queue,
446 mtx_unlock(&be_lun->queue_lock);
447 taskqueue_enqueue(be_lun->io_taskqueue,
451 ctl_set_success(&io->scsiio);
454 ctl_data_submit_done(io);
459 ctl_backend_ramdisk_compare(union ctl_io *io)
461 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io);
464 lbas = ARGS(io)->len - PRIV(io)->len;
465 lbas = MIN(lbas, 131072 / cbe_lun->blocksize);
466 len = lbas * cbe_lun->blocksize;
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;
475 getbinuptime(&io->io_hdr.dma_start_bt);
481 ctl_backend_ramdisk_rw(union ctl_io *io)
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;
488 u_int i, len, lbaoff, lbas, sgs, off;
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;
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);
509 ctl_set_space_alloc_fail(&io->scsiio);
510 ctl_data_submit_done(io);
513 sg_entries[i].addr = page + off;
514 sg_entries[i].len = MIN(len, be_lun->pblocksize - off);
515 len -= sg_entries[i].len;
519 page = ctl_backend_ramdisk_getpage(be_lun,
520 lba >> cbe_lun->pblockexp, op);
521 if (page == P_UNMAPPED || page == P_ANCHORED)
524 io->scsiio.kern_data_ptr = page + off;
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);
538 getbinuptime(&io->io_hdr.dma_start_bt);
544 ctl_backend_ramdisk_submit(union ctl_io *io)
546 struct ctl_lba_len_flags *lbalen = ARGS(io);
548 if (lbalen->flags & CTL_LLF_VERIFY) {
549 ctl_set_success(&io->scsiio);
550 ctl_data_submit_done(io);
551 return (CTL_RETVAL_COMPLETE);
554 if (lbalen->flags & CTL_LLF_COMPARE)
555 ctl_backend_ramdisk_compare(io);
557 ctl_backend_ramdisk_rw(io);
558 return (CTL_RETVAL_COMPLETE);
562 ctl_backend_ramdisk_worker(void *context, int pending)
564 struct ctl_be_ramdisk_lun *be_lun;
567 be_lun = (struct ctl_be_ramdisk_lun *)context;
568 mtx_lock(&be_lun->queue_lock);
570 io = (union ctl_io *)STAILQ_FIRST(&be_lun->cont_queue);
572 STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr,
574 mtx_unlock(&be_lun->queue_lock);
575 if (ARGS(io)->flags & CTL_LLF_COMPARE)
576 ctl_backend_ramdisk_compare(io);
578 ctl_backend_ramdisk_rw(io);
579 mtx_lock(&be_lun->queue_lock);
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.
589 mtx_unlock(&be_lun->queue_lock);
593 ctl_backend_ramdisk_gls(union ctl_io *io)
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;
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;
612 data->descr[0].status = 0;
613 ctl_config_read_done(io);
614 return (CTL_RETVAL_COMPLETE);
618 ctl_backend_ramdisk_config_read(union ctl_io *io)
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);
628 ctl_set_invalid_field(&io->scsiio,
634 ctl_config_read_done(io);
635 retval = CTL_RETVAL_COMPLETE;
638 ctl_set_invalid_opcode(&io->scsiio);
639 ctl_config_read_done(io);
640 retval = CTL_RETVAL_COMPLETE;
647 ctl_backend_ramdisk_delete(struct ctl_be_lun *cbe_lun, off_t lba, off_t len,
650 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun;
654 getpage_op_t op = anchor ? GP_ANCHOR : GP_OTHER;
656 /* Partially zero first partial page. */
657 p = lba >> cbe_lun->pblockexp;
658 lbaoff = lba & ~(UINT_MAX << cbe_lun->pblockexp);
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) *
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);
678 /* Delete remaining full pages. */
681 ctl_backend_ramdisk_anchorpage(be_lun, p);
684 ctl_backend_ramdisk_unmappage(be_lun, p);
689 ctl_backend_ramdisk_ws(union ctl_io *io)
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);
698 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB)) {
699 ctl_set_invalid_field(&io->scsiio,
705 ctl_config_write_done(io);
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);
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);
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);
729 memcpy(page, io->scsiio.kern_data_ptr,
732 if (lbalen->flags & SWS_LBDATA)
733 scsi_ulto4b(lba, page);
735 ctl_set_success(&io->scsiio);
736 ctl_config_write_done(io);
740 ctl_backend_ramdisk_unmap(union ctl_io *io)
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;
746 if ((ptrlen->flags & ~SU_ANCHOR) != 0) {
747 ctl_set_invalid_field(&io->scsiio,
753 ctl_config_write_done(io);
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);
765 ctl_set_success(&io->scsiio);
766 ctl_config_write_done(io);
770 ctl_backend_ramdisk_config_write(union ctl_io *io)
772 struct ctl_be_lun *cbe_lun = CTL_BACKEND_LUN(io);
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);
782 case START_STOP_UNIT: {
783 struct scsi_start_stop_unit *cdb;
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);
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);
796 ctl_stop_lun(cbe_lun);
797 if (cdb->how & SSS_LOEJ)
798 ctl_lun_ejected(cbe_lun);
800 ctl_set_success(&io->scsiio);
801 ctl_config_write_done(io);
805 ctl_set_success(&io->scsiio);
806 ctl_config_write_done(io);
810 ctl_backend_ramdisk_ws(io);
813 ctl_backend_ramdisk_unmap(io);
816 ctl_set_invalid_opcode(&io->scsiio);
817 ctl_config_write_done(io);
818 retval = CTL_RETVAL_COMPLETE;
826 ctl_backend_ramdisk_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
828 struct ctl_be_ramdisk_lun *be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun;
832 if (be_lun->cap_bytes == 0)
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;
841 sx_sunlock(&be_lun->page_lock);
846 ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
847 int flag, struct thread *td)
849 struct ctl_be_ramdisk_softc *softc = &rd_softc;
850 struct ctl_lun_req *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);
862 retval = ctl_backend_ramdisk_rm(softc, lun_req);
864 case CTL_LUNREQ_MODIFY:
865 retval = ctl_backend_ramdisk_modify(softc, lun_req);
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__,
884 ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
885 struct ctl_lun_req *req)
887 struct ctl_be_ramdisk_lun *be_lun;
888 struct ctl_lun_rm_params *params;
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);
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);
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.
916 * This tells the shutdown routine that we're waiting, or we're
917 * going to wait for the shutdown to happen.
919 mtx_lock(&softc->lock);
920 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
921 mtx_unlock(&softc->lock);
923 retval = ctl_remove_lun(&be_lun->cbe_lun);
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);
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);
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);
945 mtx_unlock(&softc->lock);
949 req->status = CTL_LUN_OK;
953 req->status = CTL_LUN_ERROR;
958 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
959 struct ctl_lun_req *req)
961 struct ctl_be_ramdisk_lun *be_lun;
962 struct ctl_be_lun *cbe_lun;
963 struct ctl_lun_create_params *params;
970 params = &req->reqdata.create;
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;
978 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
979 cbe_lun->lun_type = params->device_type;
981 cbe_lun->lun_type = T_DIRECT;
984 value = dnvlist_get_string(cbe_lun->options, "ha_role", 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;
991 be_lun->pblocksize = PAGE_SIZE;
992 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
994 ctl_expand_number(value, &t);
995 be_lun->pblocksize = t;
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);
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;
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",
1017 be_lun->pblocksize, cbe_lun->blocksize);
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);
1026 be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
1027 be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
1029 t = be_lun->size_bytes / be_lun->pblocksize;
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);
1043 ctl_expand_number(value, &be_lun->cap_bytes);
1045 be_lun->pblockmul = 1;
1046 cbe_lun->pblockexp = 0;
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;
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;
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;
1075 cbe_lun->req_lun_id = 0;
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",
1082 strncpy((char *)cbe_lun->serial_num, tmpstr,
1083 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
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)));
1089 strncpy((char *)cbe_lun->serial_num, params->serial_num,
1090 MIN(sizeof(cbe_lun->serial_num),
1091 sizeof(params->serial_num)));
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)));
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)));
1102 strncpy((char *)cbe_lun->device_id, params->device_id,
1103 MIN(sizeof(cbe_lun->device_id),
1104 sizeof(params->device_id)));
1107 STAILQ_INIT(&be_lun->cont_queue);
1108 sx_init(&be_lun->page_lock, "ctlram page");
1109 if (be_lun->cap_bytes == 0) {
1111 be_lun->pages = malloc(be_lun->pblocksize, M_RAMDISK, M_WAITOK);
1113 be_lun->zero_page = malloc(be_lun->pblocksize, M_RAMDISK,
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,
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__);
1127 retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
1130 /*proc*/control_softc->ctl_proc,
1131 /*thread name*/"ramdisk");
1135 retval = ctl_add_lun(&be_lun->cbe_lun);
1137 snprintf(req->error_str, sizeof(req->error_str),
1138 "%s: ctl_add_lun() returned error %d, see dmesg for "
1139 "details", __func__, retval);
1144 mtx_lock(&softc->lock);
1146 SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
1147 mtx_unlock(&softc->lock);
1149 params->req_lun_id = cbe_lun->lun_id;
1151 req->status = CTL_LUN_OK;
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);
1170 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
1171 struct ctl_lun_req *req)
1173 struct ctl_be_ramdisk_lun *be_lun;
1174 struct ctl_be_lun *cbe_lun;
1175 struct ctl_lun_modify_params *params;
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)
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);
1194 cbe_lun = &be_lun->cbe_lun;
1196 if (params->lun_size_bytes != 0)
1197 be_lun->params.lun_size_bytes = params->lun_size_bytes;
1199 if (req->args_nvl != NULL) {
1200 nvlist_destroy(cbe_lun->options);
1201 cbe_lun->options = nvlist_clone(req->args_nvl);
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;
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;
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);
1219 ctl_lun_secondary(cbe_lun);
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);
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);
1234 /* Tell the user the exact size we ended up using */
1235 params->lun_size_bytes = be_lun->size_bytes;
1237 sx_xunlock(&softc->modify_lock);
1238 req->status = CTL_LUN_OK;
1242 sx_xunlock(&softc->modify_lock);
1243 req->status = CTL_LUN_ERROR;
1248 ctl_backend_ramdisk_lun_shutdown(struct ctl_be_lun *cbe_lun)
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;
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);
1261 mtx_lock(&softc->lock);
1262 be_lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
1263 if (be_lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
1266 free(be_lun, M_RAMDISK);
1267 mtx_unlock(&softc->lock);