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_CONFIG_ERR = 0x02,
106 CTL_BE_RAMDISK_LUN_WAITING = 0x04
107 } ctl_be_ramdisk_lun_flags;
109 struct ctl_be_ramdisk_lun {
110 struct ctl_lun_create_params params;
119 uint64_t size_blocks;
122 struct ctl_be_ramdisk_softc *softc;
123 ctl_be_ramdisk_lun_flags flags;
124 STAILQ_ENTRY(ctl_be_ramdisk_lun) links;
125 struct ctl_be_lun cbe_lun;
126 struct taskqueue *io_taskqueue;
128 STAILQ_HEAD(, ctl_io_hdr) cont_queue;
129 struct mtx_padalign queue_lock;
132 struct ctl_be_ramdisk_softc {
135 STAILQ_HEAD(, ctl_be_ramdisk_lun) lun_list;
138 static struct ctl_be_ramdisk_softc rd_softc;
139 extern struct ctl_softc *control_softc;
141 static int ctl_backend_ramdisk_init(void);
142 static int ctl_backend_ramdisk_shutdown(void);
143 static int ctl_backend_ramdisk_move_done(union ctl_io *io);
144 static void ctl_backend_ramdisk_compare(union ctl_io *io);
145 static void ctl_backend_ramdisk_rw(union ctl_io *io);
146 static int ctl_backend_ramdisk_submit(union ctl_io *io);
147 static void ctl_backend_ramdisk_worker(void *context, int pending);
148 static int ctl_backend_ramdisk_config_read(union ctl_io *io);
149 static int ctl_backend_ramdisk_config_write(union ctl_io *io);
150 static uint64_t ctl_backend_ramdisk_lun_attr(void *be_lun, const char *attrname);
151 static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd,
152 caddr_t addr, int flag, struct thread *td);
153 static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc,
154 struct ctl_lun_req *req);
155 static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
156 struct ctl_lun_req *req);
157 static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
158 struct ctl_lun_req *req);
159 static void ctl_backend_ramdisk_lun_shutdown(void *be_lun);
160 static void ctl_backend_ramdisk_lun_config_status(void *be_lun,
161 ctl_lun_config_status status);
163 static struct ctl_backend_driver ctl_be_ramdisk_driver =
166 .flags = CTL_BE_FLAG_HAS_CONFIG,
167 .init = ctl_backend_ramdisk_init,
168 .shutdown = ctl_backend_ramdisk_shutdown,
169 .data_submit = ctl_backend_ramdisk_submit,
170 .data_move_done = ctl_backend_ramdisk_move_done,
171 .config_read = ctl_backend_ramdisk_config_read,
172 .config_write = ctl_backend_ramdisk_config_write,
173 .ioctl = ctl_backend_ramdisk_ioctl,
174 .lun_attr = ctl_backend_ramdisk_lun_attr,
177 MALLOC_DEFINE(M_RAMDISK, "ramdisk", "Memory used for CTL RAMdisk");
178 CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver);
181 ctl_backend_ramdisk_init(void)
183 struct ctl_be_ramdisk_softc *softc = &rd_softc;
185 memset(softc, 0, sizeof(*softc));
186 mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF);
187 STAILQ_INIT(&softc->lun_list);
192 ctl_backend_ramdisk_shutdown(void)
194 struct ctl_be_ramdisk_softc *softc = &rd_softc;
195 struct ctl_be_ramdisk_lun *lun, *next_lun;
197 mtx_lock(&softc->lock);
198 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) {
200 * Drop our lock here. Since ctl_invalidate_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_disable_lun(&lun->cbe_lun);
206 ctl_invalidate_lun(&lun->cbe_lun);
207 mtx_lock(&softc->lock);
209 mtx_unlock(&softc->lock);
210 mtx_destroy(&softc->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 = cbe_lun->be_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_lun *cbe_lun = CTL_BACKEND_LUN(io);
408 struct ctl_be_ramdisk_lun *be_lun = cbe_lun->be_lun;
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 = cbe_lun->be_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 = cbe_lun->be_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 = cbe_lun->be_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 = cbe_lun->be_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(void *arg, const char *attrname)
828 struct ctl_be_ramdisk_lun *be_lun = arg;
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 mtx_lock(&softc->lock);
893 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
894 if (be_lun->cbe_lun.lun_id == params->lun_id)
897 mtx_unlock(&softc->lock);
898 if (be_lun == NULL) {
899 snprintf(req->error_str, sizeof(req->error_str),
900 "%s: LUN %u is not managed by the ramdisk backend",
901 __func__, params->lun_id);
905 retval = ctl_disable_lun(&be_lun->cbe_lun);
907 snprintf(req->error_str, sizeof(req->error_str),
908 "%s: error %d returned from ctl_disable_lun() for "
909 "LUN %d", __func__, retval, params->lun_id);
914 * Set the waiting flag before we invalidate the LUN. Our shutdown
915 * routine can be called any time after we invalidate the LUN,
916 * and can be called from our context.
918 * This tells the shutdown routine that we're waiting, or we're
919 * going to wait for the shutdown to happen.
921 mtx_lock(&softc->lock);
922 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
923 mtx_unlock(&softc->lock);
925 retval = ctl_invalidate_lun(&be_lun->cbe_lun);
927 snprintf(req->error_str, sizeof(req->error_str),
928 "%s: error %d returned from ctl_invalidate_lun() for "
929 "LUN %d", __func__, retval, params->lun_id);
930 mtx_lock(&softc->lock);
931 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
932 mtx_unlock(&softc->lock);
936 mtx_lock(&softc->lock);
937 while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) {
938 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
942 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
945 * We only remove this LUN from the list and free it (below) if
946 * retval == 0. If the user interrupted the wait, we just bail out
947 * without actually freeing the LUN. We let the shutdown routine
948 * free the LUN if that happens.
951 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
956 mtx_unlock(&softc->lock);
959 taskqueue_drain_all(be_lun->io_taskqueue);
960 taskqueue_free(be_lun->io_taskqueue);
961 nvlist_destroy(be_lun->cbe_lun.options);
962 free(be_lun->zero_page, M_RAMDISK);
963 ctl_backend_ramdisk_freeallpages(be_lun->pages, be_lun->indir);
964 sx_destroy(&be_lun->page_lock);
965 mtx_destroy(&be_lun->queue_lock);
966 free(be_lun, M_RAMDISK);
969 req->status = CTL_LUN_OK;
973 req->status = CTL_LUN_ERROR;
978 ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc,
979 struct ctl_lun_req *req)
981 struct ctl_be_ramdisk_lun *be_lun;
982 struct ctl_be_lun *cbe_lun;
983 struct ctl_lun_create_params *params;
990 params = &req->reqdata.create;
992 be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK);
993 cbe_lun = &be_lun->cbe_lun;
994 cbe_lun->be_lun = be_lun;
995 cbe_lun->options = nvlist_clone(req->args_nvl);
996 be_lun->params = req->reqdata.create;
997 be_lun->softc = softc;
998 sprintf(be_lun->lunname, "cram%d", softc->num_luns);
1000 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1001 cbe_lun->lun_type = params->device_type;
1003 cbe_lun->lun_type = T_DIRECT;
1004 be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED;
1006 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
1007 if (value != NULL) {
1008 if (strcmp(value, "primary") == 0)
1009 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
1010 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
1011 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
1013 be_lun->pblocksize = PAGE_SIZE;
1014 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
1015 if (value != NULL) {
1016 ctl_expand_number(value, &t);
1017 be_lun->pblocksize = t;
1019 if (be_lun->pblocksize < 512 || be_lun->pblocksize > 131072) {
1020 snprintf(req->error_str, sizeof(req->error_str),
1021 "%s: unsupported pblocksize %u", __func__,
1022 be_lun->pblocksize);
1026 if (cbe_lun->lun_type == T_DIRECT ||
1027 cbe_lun->lun_type == T_CDROM) {
1028 if (params->blocksize_bytes != 0)
1029 cbe_lun->blocksize = params->blocksize_bytes;
1030 else if (cbe_lun->lun_type == T_CDROM)
1031 cbe_lun->blocksize = 2048;
1033 cbe_lun->blocksize = 512;
1034 be_lun->pblockmul = be_lun->pblocksize / cbe_lun->blocksize;
1035 if (be_lun->pblockmul < 1 || !powerof2(be_lun->pblockmul)) {
1036 snprintf(req->error_str, sizeof(req->error_str),
1037 "%s: pblocksize %u not exp2 of blocksize %u",
1039 be_lun->pblocksize, cbe_lun->blocksize);
1042 if (params->lun_size_bytes < cbe_lun->blocksize) {
1043 snprintf(req->error_str, sizeof(req->error_str),
1044 "%s: LUN size %ju < blocksize %u", __func__,
1045 params->lun_size_bytes, cbe_lun->blocksize);
1048 be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize;
1049 be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize;
1051 t = be_lun->size_bytes / be_lun->pblocksize;
1056 cbe_lun->maxlba = be_lun->size_blocks - 1;
1057 cbe_lun->pblockexp = fls(be_lun->pblockmul) - 1;
1058 cbe_lun->pblockoff = 0;
1059 cbe_lun->ublockexp = cbe_lun->pblockexp;
1060 cbe_lun->ublockoff = 0;
1061 cbe_lun->atomicblock = be_lun->pblocksize;
1062 cbe_lun->opttxferlen = SGPP * be_lun->pblocksize;
1063 value = dnvlist_get_string(cbe_lun->options, "capacity", NULL);
1065 ctl_expand_number(value, &be_lun->cap_bytes);
1067 be_lun->pblockmul = 1;
1068 cbe_lun->pblockexp = 0;
1071 /* Tell the user the blocksize we ended up using */
1072 params->blocksize_bytes = cbe_lun->blocksize;
1073 params->lun_size_bytes = be_lun->size_bytes;
1075 value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
1076 if (value != NULL && strcmp(value, "off") != 0)
1077 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
1078 value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
1079 if (value != NULL) {
1080 if (strcmp(value, "on") == 0)
1081 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
1082 } else if (cbe_lun->lun_type != T_DIRECT)
1083 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
1084 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
1085 value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
1086 if (value != NULL && strcmp(value, "on") == 0)
1087 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
1088 else if (value != NULL && strcmp(value, "read") == 0)
1089 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
1090 else if (value != NULL && strcmp(value, "off") == 0)
1091 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
1093 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
1094 cbe_lun->req_lun_id = params->req_lun_id;
1095 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
1097 cbe_lun->req_lun_id = 0;
1099 cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown;
1100 cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status;
1101 cbe_lun->be = &ctl_be_ramdisk_driver;
1102 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
1103 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
1105 strncpy((char *)cbe_lun->serial_num, tmpstr,
1106 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
1108 /* Tell the user what we used for a serial number */
1109 strncpy((char *)params->serial_num, tmpstr,
1110 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
1112 strncpy((char *)cbe_lun->serial_num, params->serial_num,
1113 MIN(sizeof(cbe_lun->serial_num),
1114 sizeof(params->serial_num)));
1116 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
1117 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
1118 strncpy((char *)cbe_lun->device_id, tmpstr,
1119 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
1121 /* Tell the user what we used for a device ID */
1122 strncpy((char *)params->device_id, tmpstr,
1123 MIN(sizeof(params->device_id), sizeof(tmpstr)));
1125 strncpy((char *)cbe_lun->device_id, params->device_id,
1126 MIN(sizeof(cbe_lun->device_id),
1127 sizeof(params->device_id)));
1130 STAILQ_INIT(&be_lun->cont_queue);
1131 sx_init(&be_lun->page_lock, "cram page lock");
1132 if (be_lun->cap_bytes == 0) {
1134 be_lun->pages = malloc(be_lun->pblocksize, M_RAMDISK, M_WAITOK);
1136 be_lun->zero_page = malloc(be_lun->pblocksize, M_RAMDISK,
1138 mtx_init(&be_lun->queue_lock, "cram queue lock", NULL, MTX_DEF);
1139 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_backend_ramdisk_worker,
1142 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
1143 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
1144 if (be_lun->io_taskqueue == NULL) {
1145 snprintf(req->error_str, sizeof(req->error_str),
1146 "%s: Unable to create taskqueue", __func__);
1150 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
1154 "%s taskq", be_lun->lunname);
1158 mtx_lock(&softc->lock);
1160 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
1161 mtx_unlock(&softc->lock);
1163 retval = ctl_add_lun(&be_lun->cbe_lun);
1165 mtx_lock(&softc->lock);
1166 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
1169 mtx_unlock(&softc->lock);
1170 snprintf(req->error_str, sizeof(req->error_str),
1171 "%s: ctl_add_lun() returned error %d, see dmesg for "
1172 "details", __func__, retval);
1177 mtx_lock(&softc->lock);
1180 * Tell the config_status routine that we're waiting so it won't
1181 * clean up the LUN in the event of an error.
1183 be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING;
1185 while (be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) {
1186 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0);
1187 if (retval == EINTR)
1190 be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING;
1192 if (be_lun->flags & CTL_BE_RAMDISK_LUN_CONFIG_ERR) {
1193 snprintf(req->error_str, sizeof(req->error_str),
1194 "%s: LUN configuration error, see dmesg for details",
1196 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun,
1199 mtx_unlock(&softc->lock);
1202 params->req_lun_id = cbe_lun->lun_id;
1204 mtx_unlock(&softc->lock);
1206 req->status = CTL_LUN_OK;
1210 req->status = CTL_LUN_ERROR;
1211 if (be_lun != NULL) {
1212 if (be_lun->io_taskqueue != NULL)
1213 taskqueue_free(be_lun->io_taskqueue);
1214 nvlist_destroy(cbe_lun->options);
1215 free(be_lun->zero_page, M_RAMDISK);
1216 ctl_backend_ramdisk_freeallpages(be_lun->pages, be_lun->indir);
1217 sx_destroy(&be_lun->page_lock);
1218 mtx_destroy(&be_lun->queue_lock);
1219 free(be_lun, M_RAMDISK);
1225 ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc,
1226 struct ctl_lun_req *req)
1228 struct ctl_be_ramdisk_lun *be_lun;
1229 struct ctl_be_lun *cbe_lun;
1230 struct ctl_lun_modify_params *params;
1235 params = &req->reqdata.modify;
1237 mtx_lock(&softc->lock);
1238 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
1239 if (be_lun->cbe_lun.lun_id == params->lun_id)
1242 mtx_unlock(&softc->lock);
1243 if (be_lun == NULL) {
1244 snprintf(req->error_str, sizeof(req->error_str),
1245 "%s: LUN %u is not managed by the ramdisk backend",
1246 __func__, params->lun_id);
1249 cbe_lun = &be_lun->cbe_lun;
1251 if (params->lun_size_bytes != 0)
1252 be_lun->params.lun_size_bytes = params->lun_size_bytes;
1254 nvlist_destroy(cbe_lun->options);
1255 cbe_lun->options = nvlist_clone(req->args_nvl);
1257 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
1258 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
1259 if (value != NULL) {
1260 if (strcmp(value, "primary") == 0)
1261 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
1263 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
1264 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
1265 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
1267 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
1268 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
1269 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
1270 ctl_lun_primary(cbe_lun);
1272 ctl_lun_secondary(cbe_lun);
1275 blocksize = be_lun->cbe_lun.blocksize;
1276 if (be_lun->params.lun_size_bytes < blocksize) {
1277 snprintf(req->error_str, sizeof(req->error_str),
1278 "%s: LUN size %ju < blocksize %u", __func__,
1279 be_lun->params.lun_size_bytes, blocksize);
1282 be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize;
1283 be_lun->size_bytes = be_lun->size_blocks * blocksize;
1284 be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1;
1285 ctl_lun_capacity_changed(&be_lun->cbe_lun);
1287 /* Tell the user the exact size we ended up using */
1288 params->lun_size_bytes = be_lun->size_bytes;
1290 req->status = CTL_LUN_OK;
1294 req->status = CTL_LUN_ERROR;
1299 ctl_backend_ramdisk_lun_shutdown(void *be_lun)
1301 struct ctl_be_ramdisk_lun *lun = be_lun;
1302 struct ctl_be_ramdisk_softc *softc = lun->softc;
1304 mtx_lock(&softc->lock);
1305 lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED;
1306 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
1309 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
1312 free(be_lun, M_RAMDISK);
1314 mtx_unlock(&softc->lock);
1318 ctl_backend_ramdisk_lun_config_status(void *be_lun,
1319 ctl_lun_config_status status)
1321 struct ctl_be_ramdisk_lun *lun;
1322 struct ctl_be_ramdisk_softc *softc;
1324 lun = (struct ctl_be_ramdisk_lun *)be_lun;
1327 if (status == CTL_LUN_CONFIG_OK) {
1328 mtx_lock(&softc->lock);
1329 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
1330 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING)
1332 mtx_unlock(&softc->lock);
1335 * We successfully added the LUN, attempt to enable it.
1337 if (ctl_enable_lun(&lun->cbe_lun) != 0) {
1338 printf("%s: ctl_enable_lun() failed!\n", __func__);
1339 if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
1340 printf("%s: ctl_invalidate_lun() failed!\n",
1349 mtx_lock(&softc->lock);
1350 lun->flags &= ~CTL_BE_RAMDISK_LUN_UNCONFIGURED;
1353 * If we have a user waiting, let him handle the cleanup. If not,
1354 * clean things up here.
1356 if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) {
1357 lun->flags |= CTL_BE_RAMDISK_LUN_CONFIG_ERR;
1360 STAILQ_REMOVE(&softc->lun_list, lun, ctl_be_ramdisk_lun,
1363 free(lun, M_RAMDISK);
1365 mtx_unlock(&softc->lock);