diff options
Diffstat (limited to 'sys/cam/ctl/ctl_backend_ramdisk.c')
-rw-r--r-- | sys/cam/ctl/ctl_backend_ramdisk.c | 360 |
1 files changed, 152 insertions, 208 deletions
diff --git a/sys/cam/ctl/ctl_backend_ramdisk.c b/sys/cam/ctl/ctl_backend_ramdisk.c index ad90241..d170446 100644 --- a/sys/cam/ctl/ctl_backend_ramdisk.c +++ b/sys/cam/ctl/ctl_backend_ramdisk.c @@ -1,6 +1,7 @@ /*- * Copyright (c) 2003, 2008 Silicon Graphics International Corp. * Copyright (c) 2012 The FreeBSD Foundation + * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org> * All rights reserved. * * Portions of this software were developed by Edward Tomasz Napierala @@ -56,15 +57,18 @@ __FBSDID("$FreeBSD$"); #include <sys/conf.h> #include <sys/ioccom.h> #include <sys/module.h> +#include <sys/sysctl.h> #include <cam/scsi/scsi_all.h> +#include <cam/scsi/scsi_da.h> #include <cam/ctl/ctl_io.h> #include <cam/ctl/ctl.h> #include <cam/ctl/ctl_util.h> #include <cam/ctl/ctl_backend.h> -#include <cam/ctl/ctl_frontend_internal.h> #include <cam/ctl/ctl_debug.h> #include <cam/ctl/ctl_ioctl.h> +#include <cam/ctl/ctl_ha.h> +#include <cam/ctl/ctl_private.h> #include <cam/ctl/ctl_error.h> typedef enum { @@ -74,13 +78,14 @@ typedef enum { } ctl_be_ramdisk_lun_flags; struct ctl_be_ramdisk_lun { + struct ctl_lun_create_params params; char lunname[32]; uint64_t size_bytes; uint64_t size_blocks; struct ctl_be_ramdisk_softc *softc; ctl_be_ramdisk_lun_flags flags; STAILQ_ENTRY(ctl_be_ramdisk_lun) links; - struct ctl_be_lun ctl_be_lun; + struct ctl_be_lun cbe_lun; struct taskqueue *io_taskqueue; struct task io_task; STAILQ_HEAD(, ctl_io_hdr) cont_queue; @@ -101,6 +106,7 @@ struct ctl_be_ramdisk_softc { }; static struct ctl_be_ramdisk_softc rd_softc; +extern struct ctl_softc *control_softc; int ctl_backend_ramdisk_init(void); void ctl_backend_ramdisk_shutdown(void); @@ -112,7 +118,7 @@ static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, static int ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, struct ctl_lun_req *req); static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, - struct ctl_lun_req *req, int do_wait); + struct ctl_lun_req *req); static int ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, struct ctl_lun_req *req); static void ctl_backend_ramdisk_worker(void *context, int pending); @@ -140,18 +146,13 @@ CTL_BACKEND_DECLARE(cbr, ctl_be_ramdisk_driver); int ctl_backend_ramdisk_init(void) { - struct ctl_be_ramdisk_softc *softc; + struct ctl_be_ramdisk_softc *softc = &rd_softc; #ifdef CTL_RAMDISK_PAGES int i; #endif - - softc = &rd_softc; - memset(softc, 0, sizeof(*softc)); - mtx_init(&softc->lock, "ctlramdisk", NULL, MTX_DEF); - STAILQ_INIT(&softc->lun_list); softc->rd_size = 1024 * 1024; #ifdef CTL_RAMDISK_PAGES @@ -172,31 +173,22 @@ ctl_backend_ramdisk_init(void) void ctl_backend_ramdisk_shutdown(void) { - struct ctl_be_ramdisk_softc *softc; + struct ctl_be_ramdisk_softc *softc = &rd_softc; struct ctl_be_ramdisk_lun *lun, *next_lun; #ifdef CTL_RAMDISK_PAGES int i; #endif - softc = &rd_softc; - mtx_lock(&softc->lock); - for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ - /* - * Grab the next LUN. The current LUN may get removed by - * ctl_invalidate_lun(), which will call our LUN shutdown - * routine, if there is no outstanding I/O for this LUN. - */ - next_lun = STAILQ_NEXT(lun, links); - + STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) { /* * Drop our lock here. Since ctl_invalidate_lun() can call * back into us, this could potentially lead to a recursive * lock of the same mutex, which would cause a hang. */ mtx_unlock(&softc->lock); - ctl_disable_lun(&lun->ctl_be_lun); - ctl_invalidate_lun(&lun->ctl_be_lun); + ctl_disable_lun(&lun->cbe_lun); + ctl_invalidate_lun(&lun->cbe_lun); mtx_lock(&softc->lock); } mtx_unlock(&softc->lock); @@ -219,22 +211,22 @@ ctl_backend_ramdisk_shutdown(void) static int ctl_backend_ramdisk_move_done(union ctl_io *io) { - struct ctl_be_lun *ctl_be_lun; + struct ctl_be_lun *cbe_lun; struct ctl_be_ramdisk_lun *be_lun; #ifdef CTL_TIME_IO struct bintime cur_bt; #endif CTL_DEBUG_PRINT(("ctl_backend_ramdisk_move_done\n")); - ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ + cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ CTL_PRIV_BACKEND_LUN].ptr; - be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun; + be_lun = (struct ctl_be_ramdisk_lun *)cbe_lun->be_lun; #ifdef CTL_TIME_IO - getbintime(&cur_bt); + getbinuptime(&cur_bt); bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); bintime_add(&io->io_hdr.dma_bt, &cur_bt); - io->io_hdr.num_dmas++; #endif + io->io_hdr.num_dmas++; if (io->scsiio.kern_sg_entries > 0) free(io->scsiio.kern_data_ptr, M_RAMDISK); io->scsiio.kern_rel_offset += io->scsiio.kern_data_len; @@ -275,10 +267,10 @@ ctl_backend_ramdisk_move_done(union ctl_io *io) static int ctl_backend_ramdisk_submit(union ctl_io *io) { - struct ctl_be_lun *ctl_be_lun; + struct ctl_be_lun *cbe_lun; struct ctl_lba_len_flags *lbalen; - ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ + cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ CTL_PRIV_BACKEND_LUN].ptr; lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; if (lbalen->flags & CTL_LLF_VERIFY) { @@ -287,7 +279,7 @@ ctl_backend_ramdisk_submit(union ctl_io *io) return (CTL_RETVAL_COMPLETE); } io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer = - lbalen->len * ctl_be_lun->blocksize; + lbalen->len * cbe_lun->blocksize; ctl_backend_ramdisk_continue(io); return (CTL_RETVAL_COMPLETE); } @@ -316,7 +308,6 @@ ctl_backend_ramdisk_continue(union ctl_io *io) sg_entries[i].len = MIN(PAGE_SIZE, len - len_filled); len_filled += sg_entries[i].len; } - io->io_hdr.flags |= CTL_FLAG_KDPTR_SGLIST; } else { sg_filled = 0; len_filled = len; @@ -335,7 +326,7 @@ ctl_backend_ramdisk_continue(union ctl_io *io) io->io_hdr.flags |= CTL_FLAG_ALLOCATED; io->io_hdr.ctl_private[CTL_PRIV_BACKEND].integer -= len_filled; #ifdef CTL_TIME_IO - getbintime(&io->io_hdr.dma_start_bt); + getbinuptime(&io->io_hdr.dma_start_bt); #endif ctl_datamove(io); } @@ -343,12 +334,10 @@ ctl_backend_ramdisk_continue(union ctl_io *io) static void ctl_backend_ramdisk_worker(void *context, int pending) { - struct ctl_be_ramdisk_softc *softc; struct ctl_be_ramdisk_lun *be_lun; union ctl_io *io; be_lun = (struct ctl_be_ramdisk_lun *)context; - softc = be_lun->softc; mtx_lock(&be_lun->queue_lock); for (;;) { @@ -356,11 +345,8 @@ ctl_backend_ramdisk_worker(void *context, int pending) if (io != NULL) { STAILQ_REMOVE(&be_lun->cont_queue, &io->io_hdr, ctl_io_hdr, links); - mtx_unlock(&be_lun->queue_lock); - ctl_backend_ramdisk_continue(io); - mtx_lock(&be_lun->queue_lock); continue; } @@ -378,22 +364,17 @@ static int ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) { - struct ctl_be_ramdisk_softc *softc; + struct ctl_be_ramdisk_softc *softc = &rd_softc; + struct ctl_lun_req *lun_req; int retval; retval = 0; - softc = &rd_softc; - switch (cmd) { - case CTL_LUN_REQ: { - struct ctl_lun_req *lun_req; - + case CTL_LUN_REQ: lun_req = (struct ctl_lun_req *)addr; - switch (lun_req->reqtype) { case CTL_LUNREQ_CREATE: - retval = ctl_backend_ramdisk_create(softc, lun_req, - /*do_wait*/ 1); + retval = ctl_backend_ramdisk_create(softc, lun_req); break; case CTL_LUNREQ_RM: retval = ctl_backend_ramdisk_rm(softc, lun_req); @@ -409,7 +390,6 @@ ctl_backend_ramdisk_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, break; } break; - } default: retval = ENOTTY; break; @@ -426,20 +406,13 @@ ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, struct ctl_lun_rm_params *params; int retval; - - retval = 0; params = &req->reqdata.rm; - - be_lun = NULL; - mtx_lock(&softc->lock); - STAILQ_FOREACH(be_lun, &softc->lun_list, links) { - if (be_lun->ctl_be_lun.lun_id == params->lun_id) + if (be_lun->cbe_lun.lun_id == params->lun_id) break; } mtx_unlock(&softc->lock); - if (be_lun == NULL) { snprintf(req->error_str, sizeof(req->error_str), "%s: LUN %u is not managed by the ramdisk backend", @@ -447,8 +420,7 @@ ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, goto bailout_error; } - retval = ctl_disable_lun(&be_lun->ctl_be_lun); - + retval = ctl_disable_lun(&be_lun->cbe_lun); if (retval != 0) { snprintf(req->error_str, sizeof(req->error_str), "%s: error %d returned from ctl_disable_lun() for " @@ -468,7 +440,7 @@ ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, be_lun->flags |= CTL_BE_RAMDISK_LUN_WAITING; mtx_unlock(&softc->lock); - retval = ctl_invalidate_lun(&be_lun->ctl_be_lun); + retval = ctl_invalidate_lun(&be_lun->cbe_lun); if (retval != 0) { snprintf(req->error_str, sizeof(req->error_str), "%s: error %d returned from ctl_invalidate_lun() for " @@ -480,10 +452,9 @@ ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, } mtx_lock(&softc->lock); - while ((be_lun->flags & CTL_BE_RAMDISK_LUN_UNCONFIGURED) == 0) { retval = msleep(be_lun, &softc->lock, PCATCH, "ctlram", 0); - if (retval == EINTR) + if (retval == EINTR) break; } be_lun->flags &= ~CTL_BE_RAMDISK_LUN_WAITING; @@ -503,141 +474,133 @@ ctl_backend_ramdisk_rm(struct ctl_be_ramdisk_softc *softc, mtx_unlock(&softc->lock); if (retval == 0) { - taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task); + taskqueue_drain_all(be_lun->io_taskqueue); taskqueue_free(be_lun->io_taskqueue); - ctl_free_opts(&be_lun->ctl_be_lun.options); + ctl_free_opts(&be_lun->cbe_lun.options); mtx_destroy(&be_lun->queue_lock); free(be_lun, M_RAMDISK); } req->status = CTL_LUN_OK; - return (retval); bailout_error: req->status = CTL_LUN_ERROR; - return (0); } static int ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, - struct ctl_lun_req *req, int do_wait) + struct ctl_lun_req *req) { struct ctl_be_ramdisk_lun *be_lun; + struct ctl_be_lun *cbe_lun; struct ctl_lun_create_params *params; - uint32_t blocksize; char *value; char tmpstr[32]; - int retval, unmap; + int retval; retval = 0; params = &req->reqdata.create; - if (params->blocksize_bytes != 0) - blocksize = params->blocksize_bytes; - else - blocksize = 512; - - be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | (do_wait ? - M_WAITOK : M_NOWAIT)); - if (be_lun == NULL) { - snprintf(req->error_str, sizeof(req->error_str), - "%s: error allocating %zd bytes", __func__, - sizeof(*be_lun)); - goto bailout_error; - } + be_lun = malloc(sizeof(*be_lun), M_RAMDISK, M_ZERO | M_WAITOK); + cbe_lun = &be_lun->cbe_lun; + cbe_lun->be_lun = be_lun; + be_lun->params = req->reqdata.create; + be_lun->softc = softc; sprintf(be_lun->lunname, "cram%d", softc->num_luns); - ctl_init_opts(&be_lun->ctl_be_lun.options, - req->num_be_args, req->kern_be_args); + ctl_init_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); if (params->flags & CTL_LUN_FLAG_DEV_TYPE) - be_lun->ctl_be_lun.lun_type = params->device_type; + cbe_lun->lun_type = params->device_type; else - be_lun->ctl_be_lun.lun_type = T_DIRECT; - - if (be_lun->ctl_be_lun.lun_type == T_DIRECT) { - - if (params->lun_size_bytes < blocksize) { + cbe_lun->lun_type = T_DIRECT; + be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED; + cbe_lun->flags = 0; + value = ctl_get_opt(&cbe_lun->options, "ha_role"); + if (value != NULL) { + if (strcmp(value, "primary") == 0) + cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; + } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) + cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; + + if (cbe_lun->lun_type == T_DIRECT || + cbe_lun->lun_type == T_CDROM) { + if (params->blocksize_bytes != 0) + cbe_lun->blocksize = params->blocksize_bytes; + else if (cbe_lun->lun_type == T_CDROM) + cbe_lun->blocksize = 2048; + else + cbe_lun->blocksize = 512; + if (params->lun_size_bytes < cbe_lun->blocksize) { snprintf(req->error_str, sizeof(req->error_str), "%s: LUN size %ju < blocksize %u", __func__, - params->lun_size_bytes, blocksize); + params->lun_size_bytes, cbe_lun->blocksize); goto bailout_error; } - - be_lun->size_blocks = params->lun_size_bytes / blocksize; - be_lun->size_bytes = be_lun->size_blocks * blocksize; - - be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; - be_lun->ctl_be_lun.atomicblock = UINT32_MAX; - be_lun->ctl_be_lun.opttxferlen = softc->rd_size / blocksize; - } else { - be_lun->ctl_be_lun.maxlba = 0; - blocksize = 0; - be_lun->size_bytes = 0; - be_lun->size_blocks = 0; + be_lun->size_blocks = params->lun_size_bytes / cbe_lun->blocksize; + be_lun->size_bytes = be_lun->size_blocks * cbe_lun->blocksize; + cbe_lun->maxlba = be_lun->size_blocks - 1; + cbe_lun->atomicblock = UINT32_MAX; + cbe_lun->opttxferlen = softc->rd_size / cbe_lun->blocksize; } - be_lun->ctl_be_lun.blocksize = blocksize; - /* Tell the user the blocksize we ended up using */ - params->blocksize_bytes = blocksize; - - /* Tell the user the exact size we ended up using */ + params->blocksize_bytes = cbe_lun->blocksize; params->lun_size_bytes = be_lun->size_bytes; - be_lun->softc = softc; - - unmap = 1; - value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap"); + value = ctl_get_opt(&cbe_lun->options, "unmap"); if (value != NULL && strcmp(value, "on") == 0) - unmap = (strcmp(value, "on") == 0); - - be_lun->flags = CTL_BE_RAMDISK_LUN_UNCONFIGURED; - be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY; - if (unmap) - be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP; - be_lun->ctl_be_lun.be_lun = be_lun; + cbe_lun->flags |= CTL_LUN_FLAG_UNMAP; + value = ctl_get_opt(&cbe_lun->options, "readonly"); + if (value != NULL) { + if (strcmp(value, "on") == 0) + cbe_lun->flags |= CTL_LUN_FLAG_READONLY; + } else if (cbe_lun->lun_type != T_DIRECT) + cbe_lun->flags |= CTL_LUN_FLAG_READONLY; + cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; + value = ctl_get_opt(&cbe_lun->options, "serseq"); + if (value != NULL && strcmp(value, "on") == 0) + cbe_lun->serseq = CTL_LUN_SERSEQ_ON; + else if (value != NULL && strcmp(value, "read") == 0) + cbe_lun->serseq = CTL_LUN_SERSEQ_READ; + else if (value != NULL && strcmp(value, "off") == 0) + cbe_lun->serseq = CTL_LUN_SERSEQ_OFF; if (params->flags & CTL_LUN_FLAG_ID_REQ) { - be_lun->ctl_be_lun.req_lun_id = params->req_lun_id; - be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ; + cbe_lun->req_lun_id = params->req_lun_id; + cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ; } else - be_lun->ctl_be_lun.req_lun_id = 0; + cbe_lun->req_lun_id = 0; - be_lun->ctl_be_lun.lun_shutdown = ctl_backend_ramdisk_lun_shutdown; - be_lun->ctl_be_lun.lun_config_status = - ctl_backend_ramdisk_lun_config_status; - be_lun->ctl_be_lun.be = &ctl_be_ramdisk_driver; + cbe_lun->lun_shutdown = ctl_backend_ramdisk_lun_shutdown; + cbe_lun->lun_config_status = ctl_backend_ramdisk_lun_config_status; + cbe_lun->be = &ctl_be_ramdisk_driver; if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) { snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d", softc->num_luns); - strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr, - MIN(sizeof(be_lun->ctl_be_lun.serial_num), - sizeof(tmpstr))); + strncpy((char *)cbe_lun->serial_num, tmpstr, + MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr))); /* Tell the user what we used for a serial number */ strncpy((char *)params->serial_num, tmpstr, MIN(sizeof(params->serial_num), sizeof(tmpstr))); } else { - strncpy((char *)be_lun->ctl_be_lun.serial_num, - params->serial_num, - MIN(sizeof(be_lun->ctl_be_lun.serial_num), + strncpy((char *)cbe_lun->serial_num, params->serial_num, + MIN(sizeof(cbe_lun->serial_num), sizeof(params->serial_num))); } if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) { snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns); - strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr, - MIN(sizeof(be_lun->ctl_be_lun.device_id), - sizeof(tmpstr))); + strncpy((char *)cbe_lun->device_id, tmpstr, + MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr))); /* Tell the user what we used for a device ID */ strncpy((char *)params->device_id, tmpstr, MIN(sizeof(params->device_id), sizeof(tmpstr))); } else { - strncpy((char *)be_lun->ctl_be_lun.device_id, - params->device_id, - MIN(sizeof(be_lun->ctl_be_lun.device_id), + strncpy((char *)cbe_lun->device_id, params->device_id, + MIN(sizeof(cbe_lun->device_id), sizeof(params->device_id))); } @@ -665,10 +628,9 @@ ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, mtx_lock(&softc->lock); softc->num_luns++; STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links); - mtx_unlock(&softc->lock); - retval = ctl_add_lun(&be_lun->ctl_be_lun); + retval = ctl_add_lun(&be_lun->cbe_lun); if (retval != 0) { mtx_lock(&softc->lock); STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_ramdisk_lun, @@ -682,9 +644,6 @@ ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, goto bailout_error; } - if (do_wait == 0) - return (retval); - mtx_lock(&softc->lock); /* @@ -710,12 +669,11 @@ ctl_backend_ramdisk_create(struct ctl_be_ramdisk_softc *softc, mtx_unlock(&softc->lock); goto bailout_error; } else { - params->req_lun_id = be_lun->ctl_be_lun.lun_id; + params->req_lun_id = cbe_lun->lun_id; } mtx_unlock(&softc->lock); req->status = CTL_LUN_OK; - return (retval); bailout_error: @@ -724,11 +682,10 @@ bailout_error: if (be_lun->io_taskqueue != NULL) { taskqueue_free(be_lun->io_taskqueue); } - ctl_free_opts(&be_lun->ctl_be_lun.options); + ctl_free_opts(&cbe_lun->options); mtx_destroy(&be_lun->queue_lock); free(be_lun, M_RAMDISK); } - return (retval); } @@ -737,65 +694,70 @@ ctl_backend_ramdisk_modify(struct ctl_be_ramdisk_softc *softc, struct ctl_lun_req *req) { struct ctl_be_ramdisk_lun *be_lun; + struct ctl_be_lun *cbe_lun; struct ctl_lun_modify_params *params; + char *value; uint32_t blocksize; + int wasprim; params = &req->reqdata.modify; - be_lun = NULL; - mtx_lock(&softc->lock); STAILQ_FOREACH(be_lun, &softc->lun_list, links) { - if (be_lun->ctl_be_lun.lun_id == params->lun_id) + if (be_lun->cbe_lun.lun_id == params->lun_id) break; } mtx_unlock(&softc->lock); - if (be_lun == NULL) { snprintf(req->error_str, sizeof(req->error_str), "%s: LUN %u is not managed by the ramdisk backend", __func__, params->lun_id); goto bailout_error; } - - if (params->lun_size_bytes == 0) { - snprintf(req->error_str, sizeof(req->error_str), - "%s: LUN size \"auto\" not supported " - "by the ramdisk backend", __func__); - goto bailout_error; + cbe_lun = &be_lun->cbe_lun; + + if (params->lun_size_bytes != 0) + be_lun->params.lun_size_bytes = params->lun_size_bytes; + ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args); + + wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY); + value = ctl_get_opt(&cbe_lun->options, "ha_role"); + if (value != NULL) { + if (strcmp(value, "primary") == 0) + cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; + else + cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; + } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF) + cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY; + else + cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY; + if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) { + if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) + ctl_lun_primary(cbe_lun); + else + ctl_lun_secondary(cbe_lun); } - blocksize = be_lun->ctl_be_lun.blocksize; - - if (params->lun_size_bytes < blocksize) { + blocksize = be_lun->cbe_lun.blocksize; + if (be_lun->params.lun_size_bytes < blocksize) { snprintf(req->error_str, sizeof(req->error_str), "%s: LUN size %ju < blocksize %u", __func__, - params->lun_size_bytes, blocksize); + be_lun->params.lun_size_bytes, blocksize); goto bailout_error; } - - be_lun->size_blocks = params->lun_size_bytes / blocksize; + be_lun->size_blocks = be_lun->params.lun_size_bytes / blocksize; be_lun->size_bytes = be_lun->size_blocks * blocksize; - - /* - * The maximum LBA is the size - 1. - * - * XXX: Note that this field is being updated without locking, - * which might cause problems on 32-bit architectures. - */ - be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1; - ctl_lun_capacity_changed(&be_lun->ctl_be_lun); + be_lun->cbe_lun.maxlba = be_lun->size_blocks - 1; + ctl_lun_capacity_changed(&be_lun->cbe_lun); /* Tell the user the exact size we ended up using */ params->lun_size_bytes = be_lun->size_bytes; req->status = CTL_LUN_OK; - return (0); bailout_error: req->status = CTL_LUN_ERROR; - return (0); } @@ -811,9 +773,7 @@ ctl_backend_ramdisk_lun_shutdown(void *be_lun) do_free = 0; mtx_lock(&softc->lock); - lun->flags |= CTL_BE_RAMDISK_LUN_UNCONFIGURED; - if (lun->flags & CTL_BE_RAMDISK_LUN_WAITING) { wakeup(lun); } else { @@ -822,7 +782,6 @@ ctl_backend_ramdisk_lun_shutdown(void *be_lun) softc->num_luns--; do_free = 1; } - mtx_unlock(&softc->lock); if (do_free != 0) @@ -849,9 +808,9 @@ ctl_backend_ramdisk_lun_config_status(void *be_lun, /* * We successfully added the LUN, attempt to enable it. */ - if (ctl_enable_lun(&lun->ctl_be_lun) != 0) { + if (ctl_enable_lun(&lun->cbe_lun) != 0) { printf("%s: ctl_enable_lun() failed!\n", __func__); - if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) { + if (ctl_invalidate_lun(&lun->cbe_lun) != 0) { printf("%s: ctl_invalidate_lun() failed!\n", __func__); } @@ -883,12 +842,12 @@ ctl_backend_ramdisk_lun_config_status(void *be_lun, static int ctl_backend_ramdisk_config_write(union ctl_io *io) { - struct ctl_be_ramdisk_softc *softc; + struct ctl_be_lun *cbe_lun; int retval; + cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ + CTL_PRIV_BACKEND_LUN].ptr; retval = 0; - softc = &rd_softc; - switch (io->scsiio.cdb[0]) { case SYNCHRONIZE_CACHE: case SYNCHRONIZE_CACHE_16: @@ -912,42 +871,27 @@ ctl_backend_ramdisk_config_write(union ctl_io *io) break; case START_STOP_UNIT: { struct scsi_start_stop_unit *cdb; - struct ctl_be_lun *ctl_be_lun; - struct ctl_be_ramdisk_lun *be_lun; cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb; - - ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[ - CTL_PRIV_BACKEND_LUN].ptr; - be_lun = (struct ctl_be_ramdisk_lun *)ctl_be_lun->be_lun; - - if (cdb->how & SSS_START) - retval = ctl_start_lun(ctl_be_lun); - else { - retval = ctl_stop_lun(ctl_be_lun); -#ifdef NEEDTOPORT - if ((retval == 0) - && (cdb->byte2 & SSS_ONOFFLINE)) - retval = ctl_lun_offline(ctl_be_lun); -#endif + if ((cdb->how & SSS_PC_MASK) != 0) { + ctl_set_success(&io->scsiio); + ctl_config_write_done(io); + break; } - - /* - * In general, the above routines should not fail. They - * just set state for the LUN. So we've got something - * pretty wrong here if we can't start or stop the LUN. - */ - if (retval != 0) { - ctl_set_internal_failure(&io->scsiio, - /*sks_valid*/ 1, - /*retry_count*/ 0xf051); - retval = CTL_RETVAL_COMPLETE; + if (cdb->how & SSS_START) { + if (cdb->how & SSS_LOEJ) + ctl_lun_has_media(cbe_lun); + ctl_start_lun(cbe_lun); } else { - ctl_set_success(&io->scsiio); + ctl_stop_lun(cbe_lun); + if (cdb->how & SSS_LOEJ) + ctl_lun_ejected(cbe_lun); } + ctl_set_success(&io->scsiio); ctl_config_write_done(io); break; } + case PREVENT_ALLOW: case WRITE_SAME_10: case WRITE_SAME_16: case UNMAP: |