2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2003 Silicon Graphics International Corp.
5 * Copyright (c) 2009-2011 Spectra Logic Corporation
6 * Copyright (c) 2012,2021 The FreeBSD Foundation
7 * Copyright (c) 2014-2021 Alexander Motin <mav@FreeBSD.org>
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
13 * Portions of this software were developed by Ka Ho Ng <khng@FreeBSD.org>
14 * under sponsorship from the FreeBSD Foundation.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions, and the following disclaimer,
21 * without modification.
22 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
23 * substantially similar to the "NO WARRANTY" disclaimer below
24 * ("Disclaimer") and any redistribution must be conditioned upon
25 * including a substantially similar Disclaimer requirement for further
26 * binary redistribution.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
38 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGES.
41 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
44 * CAM Target Layer driver backend for block devices.
46 * Author: Ken Merry <ken@FreeBSD.org>
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
55 #include <sys/limits.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
64 #include <sys/endian.h>
67 #include <sys/taskqueue.h>
68 #include <sys/vnode.h>
69 #include <sys/namei.h>
70 #include <sys/mount.h>
72 #include <sys/fcntl.h>
73 #include <sys/filedesc.h>
74 #include <sys/filio.h>
77 #include <sys/module.h>
79 #include <sys/devicestat.h>
80 #include <sys/sysctl.h>
84 #include <sys/unistd.h>
86 #include <geom/geom.h>
89 #include <cam/scsi/scsi_all.h>
90 #include <cam/scsi/scsi_da.h>
91 #include <cam/ctl/ctl_io.h>
92 #include <cam/ctl/ctl.h>
93 #include <cam/ctl/ctl_backend.h>
94 #include <cam/ctl/ctl_ioctl.h>
95 #include <cam/ctl/ctl_ha.h>
96 #include <cam/ctl/ctl_scsi_all.h>
97 #include <cam/ctl/ctl_private.h>
98 #include <cam/ctl/ctl_error.h>
101 * The idea here is to allocate enough S/G space to handle at least 1MB I/Os.
102 * On systems with small maxphys it can be 8 128KB segments. On large systems
103 * it can be up to 8 1MB segments. I/Os larger than that we'll split.
105 #define CTLBLK_MAX_SEGS 8
106 #define CTLBLK_HALF_SEGS (CTLBLK_MAX_SEGS / 2)
107 #define CTLBLK_MIN_SEG (128 * 1024)
108 #define CTLBLK_MAX_SEG MIN(1024 * 1024, MAX(CTLBLK_MIN_SEG, maxphys))
109 #define CTLBLK_MAX_IO_SIZE (CTLBLK_MAX_SEG * CTLBLK_MAX_SEGS)
112 #define DPRINTF(fmt, args...) \
113 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
115 #define DPRINTF(fmt, args...) do {} while(0)
119 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
121 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
123 SDT_PROVIDER_DEFINE(cbb);
126 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01,
127 CTL_BE_BLOCK_LUN_WAITING = 0x04,
128 } ctl_be_block_lun_flags;
136 struct ctl_be_block_filedata {
140 union ctl_be_block_bedata {
141 struct ctl_be_block_filedata file;
144 struct ctl_be_block_io;
145 struct ctl_be_block_lun;
147 typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
148 struct ctl_be_block_io *beio);
149 typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
150 const char *attrname);
153 * Backend LUN structure. There is a 1:1 mapping between a block device
154 * and a backend block LUN, and between a backend block LUN and a CTL LUN.
156 struct ctl_be_block_lun {
157 struct ctl_be_lun cbe_lun; /* Must be first element. */
158 struct ctl_lun_create_params params;
160 ctl_be_block_type dev_type;
162 union ctl_be_block_bedata backend;
163 cbb_dispatch_t dispatch;
164 cbb_dispatch_t lun_flush;
165 cbb_dispatch_t unmap;
166 cbb_dispatch_t get_lba_status;
167 cbb_getattr_t getattr;
168 uint64_t size_blocks;
170 struct ctl_be_block_softc *softc;
171 struct devstat *disk_stats;
172 ctl_be_block_lun_flags flags;
173 SLIST_ENTRY(ctl_be_block_lun) links;
174 struct taskqueue *io_taskqueue;
177 STAILQ_HEAD(, ctl_io_hdr) input_queue;
178 STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
179 STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
180 STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
181 struct mtx_padalign io_lock;
182 struct mtx_padalign queue_lock;
186 * Overall softc structure for the block backend module.
188 struct ctl_be_block_softc {
189 struct sx modify_lock;
192 SLIST_HEAD(, ctl_be_block_lun) lun_list;
193 uma_zone_t beio_zone;
194 uma_zone_t bufmin_zone;
195 uma_zone_t bufmax_zone;
198 static struct ctl_be_block_softc backend_block_softc;
201 * Per-I/O information.
203 struct ctl_be_block_io {
205 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS];
206 struct iovec xiovecs[CTLBLK_MAX_SEGS];
215 uint64_t first_error_offset;
216 struct bintime ds_t0;
217 devstat_tag_type ds_tag_type;
218 devstat_trans_flags ds_trans_type;
222 struct ctl_be_block_softc *softc;
223 struct ctl_be_block_lun *lun;
224 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
227 extern struct ctl_softc *control_softc;
229 static int cbb_num_threads = 32;
230 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
231 "CAM Target Layer Block Backend");
232 SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
233 &cbb_num_threads, 0, "Number of threads per backing file");
235 static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
236 static void ctl_free_beio(struct ctl_be_block_io *beio);
237 static void ctl_complete_beio(struct ctl_be_block_io *beio);
238 static int ctl_be_block_move_done(union ctl_io *io, bool samethr);
239 static void ctl_be_block_biodone(struct bio *bio);
240 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
241 struct ctl_be_block_io *beio);
242 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
243 struct ctl_be_block_io *beio);
244 static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
245 struct ctl_be_block_io *beio);
246 static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
247 const char *attrname);
248 static void ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
249 struct ctl_be_block_io *beio);
250 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
251 struct ctl_be_block_io *beio);
252 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
253 struct ctl_be_block_io *beio);
254 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
255 struct ctl_be_block_io *beio);
256 static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
257 const char *attrname);
258 static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
260 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
262 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
264 static void ctl_be_block_worker(void *context, int pending);
265 static int ctl_be_block_submit(union ctl_io *io);
266 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
267 int flag, struct thread *td);
268 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
269 struct ctl_lun_req *req);
270 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
271 struct ctl_lun_req *req);
272 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
273 static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
274 struct ctl_lun_req *req);
275 static int ctl_be_block_create(struct ctl_be_block_softc *softc,
276 struct ctl_lun_req *req);
277 static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
278 struct ctl_lun_req *req);
279 static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
280 struct ctl_lun_req *req);
281 static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun);
282 static int ctl_be_block_config_write(union ctl_io *io);
283 static int ctl_be_block_config_read(union ctl_io *io);
284 static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb);
285 static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname);
286 static int ctl_be_block_init(void);
287 static int ctl_be_block_shutdown(void);
289 static struct ctl_backend_driver ctl_be_block_driver =
292 .flags = CTL_BE_FLAG_HAS_CONFIG,
293 .init = ctl_be_block_init,
294 .shutdown = ctl_be_block_shutdown,
295 .data_submit = ctl_be_block_submit,
296 .config_read = ctl_be_block_config_read,
297 .config_write = ctl_be_block_config_write,
298 .ioctl = ctl_be_block_ioctl,
299 .lun_info = ctl_be_block_lun_info,
300 .lun_attr = ctl_be_block_lun_attr
303 MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend");
304 CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
307 ctl_alloc_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg,
311 if (len <= CTLBLK_MIN_SEG) {
312 sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK);
314 KASSERT(len <= CTLBLK_MAX_SEG,
315 ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG));
316 sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK);
322 ctl_free_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg)
325 if (sg->len <= CTLBLK_MIN_SEG) {
326 uma_zfree(softc->bufmin_zone, sg->addr);
328 KASSERT(sg->len <= CTLBLK_MAX_SEG,
329 ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG));
330 uma_zfree(softc->bufmax_zone, sg->addr);
334 static struct ctl_be_block_io *
335 ctl_alloc_beio(struct ctl_be_block_softc *softc)
337 struct ctl_be_block_io *beio;
339 beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
346 ctl_real_free_beio(struct ctl_be_block_io *beio)
348 struct ctl_be_block_softc *softc = beio->softc;
351 for (i = 0; i < beio->num_segs; i++) {
352 ctl_free_seg(softc, &beio->sg_segs[i]);
354 /* For compare we had two equal S/G lists. */
355 if (beio->two_sglists) {
357 &beio->sg_segs[i + CTLBLK_HALF_SEGS]);
361 uma_zfree(softc->beio_zone, beio);
365 ctl_refcnt_beio(void *arg, int diff)
367 struct ctl_be_block_io *beio = arg;
369 if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0)
370 ctl_real_free_beio(beio);
374 ctl_free_beio(struct ctl_be_block_io *beio)
377 ctl_refcnt_beio(beio, -1);
381 ctl_complete_beio(struct ctl_be_block_io *beio)
383 union ctl_io *io = beio->io;
385 if (beio->beio_cont != NULL) {
386 beio->beio_cont(beio);
389 ctl_data_submit_done(io);
394 cmp(uint8_t *a, uint8_t *b, size_t size)
398 for (i = 0; i < size; i++) {
406 ctl_be_block_compare(union ctl_io *io)
408 struct ctl_be_block_io *beio;
413 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
415 for (i = 0; i < beio->num_segs; i++) {
416 res = cmp(beio->sg_segs[i].addr,
417 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
418 beio->sg_segs[i].len);
420 if (res < beio->sg_segs[i].len)
423 if (i < beio->num_segs) {
424 scsi_u64to8b(off, info);
425 ctl_set_sense(&io->scsiio, /*current_error*/ 1,
426 /*sense_key*/ SSD_KEY_MISCOMPARE,
427 /*asc*/ 0x1D, /*ascq*/ 0x00,
428 /*type*/ SSD_ELEM_INFO,
429 /*size*/ sizeof(info), /*data*/ &info,
430 /*type*/ SSD_ELEM_NONE);
432 ctl_set_success(&io->scsiio);
436 ctl_be_block_move_done(union ctl_io *io, bool samethr)
438 struct ctl_be_block_io *beio;
439 struct ctl_be_block_lun *be_lun;
440 struct ctl_lba_len_flags *lbalen;
442 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
444 DPRINTF("entered\n");
445 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
448 * We set status at this point for read and compare commands.
450 if ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
451 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) {
453 if (lbalen->flags & CTL_LLF_READ) {
454 ctl_set_success(&io->scsiio);
455 } else if (lbalen->flags & CTL_LLF_COMPARE) {
456 /* We have two data blocks ready for comparison. */
457 ctl_be_block_compare(io);
462 * If this is a read, or a write with errors, it is done.
464 if ((beio->bio_cmd == BIO_READ)
465 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
466 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
467 ctl_complete_beio(beio);
472 * At this point, we have a write and the DMA completed successfully.
473 * If we were called synchronously in the original thread then just
474 * dispatch, otherwise we now have to queue it to the task queue to
475 * execute the backend I/O. That is because we do blocking
476 * memory allocations, and in the file backing case, blocking I/O.
477 * This move done routine is generally called in the SIM's
478 * interrupt context, and therefore we cannot block.
480 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
482 be_lun->dispatch(be_lun, beio);
484 mtx_lock(&be_lun->queue_lock);
485 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
486 mtx_unlock(&be_lun->queue_lock);
487 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
493 ctl_be_block_biodone(struct bio *bio)
495 struct ctl_be_block_io *beio = bio->bio_caller1;
496 struct ctl_be_block_lun *be_lun = beio->lun;
497 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
503 DPRINTF("entered\n");
505 error = bio->bio_error;
506 mtx_lock(&be_lun->io_lock);
508 (beio->first_error == 0 ||
509 bio->bio_offset < beio->first_error_offset)) {
510 beio->first_error = error;
511 beio->first_error_offset = bio->bio_offset;
514 beio->num_bios_done++;
517 * XXX KDM will this cause WITNESS to complain? Holding a lock
518 * during the free might cause it to complain.
523 * If the send complete bit isn't set, or we aren't the last I/O to
524 * complete, then we're done.
526 if ((beio->send_complete == 0)
527 || (beio->num_bios_done < beio->num_bios_sent)) {
528 mtx_unlock(&be_lun->io_lock);
533 * At this point, we've verified that we are the last I/O to
534 * complete, so it's safe to drop the lock.
536 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
537 beio->ds_tag_type, beio->ds_trans_type,
538 /*now*/ NULL, /*then*/&beio->ds_t0);
539 mtx_unlock(&be_lun->io_lock);
542 * If there are any errors from the backing device, we fail the
543 * entire I/O with a medium error.
545 error = beio->first_error;
547 if (error == EOPNOTSUPP) {
548 ctl_set_invalid_opcode(&io->scsiio);
549 } else if (error == ENOSPC || error == EDQUOT) {
550 ctl_set_space_alloc_fail(&io->scsiio);
551 } else if (error == EROFS || error == EACCES) {
552 ctl_set_hw_write_protected(&io->scsiio);
553 } else if (beio->bio_cmd == BIO_FLUSH) {
554 /* XXX KDM is there is a better error here? */
555 ctl_set_internal_failure(&io->scsiio,
557 /*retry_count*/ 0xbad2);
559 ctl_set_medium_error(&io->scsiio,
560 beio->bio_cmd == BIO_READ);
562 ctl_complete_beio(beio);
567 * If this is a write, a flush, a delete or verify, we're all done.
568 * If this is a read, we can now send the data to the user.
570 if ((beio->bio_cmd == BIO_WRITE)
571 || (beio->bio_cmd == BIO_FLUSH)
572 || (beio->bio_cmd == BIO_DELETE)
573 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
574 ctl_set_success(&io->scsiio);
575 ctl_complete_beio(beio);
577 if ((ARGS(io)->flags & CTL_LLF_READ) &&
578 beio->beio_cont == NULL) {
579 ctl_set_success(&io->scsiio);
580 if (cbe_lun->serseq >= CTL_LUN_SERSEQ_SOFT)
588 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
589 struct ctl_be_block_io *beio)
591 union ctl_io *io = beio->io;
592 struct mount *mountpoint;
595 DPRINTF("entered\n");
597 binuptime(&beio->ds_t0);
598 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
600 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
602 vn_lock(be_lun->vn, vn_lktype_write(mountpoint, be_lun->vn) |
604 error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
606 VOP_UNLOCK(be_lun->vn);
608 vn_finished_write(mountpoint);
610 mtx_lock(&be_lun->io_lock);
611 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
612 beio->ds_tag_type, beio->ds_trans_type,
613 /*now*/ NULL, /*then*/&beio->ds_t0);
614 mtx_unlock(&be_lun->io_lock);
617 ctl_set_success(&io->scsiio);
619 /* XXX KDM is there is a better error here? */
620 ctl_set_internal_failure(&io->scsiio,
622 /*retry_count*/ 0xbad1);
625 ctl_complete_beio(beio);
628 SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
629 SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
630 SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
631 SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
634 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
635 struct ctl_be_block_io *beio)
637 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
638 struct ctl_be_block_filedata *file_data;
641 struct iovec *xiovec;
645 DPRINTF("entered\n");
647 file_data = &be_lun->backend.file;
650 if (ARGS(io)->flags & CTL_LLF_DPO)
652 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
655 bzero(&xuio, sizeof(xuio));
656 if (beio->bio_cmd == BIO_READ) {
657 SDT_PROBE0(cbb, , read, file_start);
658 xuio.uio_rw = UIO_READ;
660 SDT_PROBE0(cbb, , write, file_start);
661 xuio.uio_rw = UIO_WRITE;
663 xuio.uio_offset = beio->io_offset;
664 xuio.uio_resid = beio->io_len;
665 xuio.uio_segflg = UIO_SYSSPACE;
666 xuio.uio_iov = beio->xiovecs;
667 xuio.uio_iovcnt = beio->num_segs;
668 xuio.uio_td = curthread;
670 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
671 xiovec->iov_base = beio->sg_segs[i].addr;
672 xiovec->iov_len = beio->sg_segs[i].len;
675 binuptime(&beio->ds_t0);
676 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
678 if (beio->bio_cmd == BIO_READ) {
679 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
681 if (beio->beio_cont == NULL &&
682 cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
685 * UFS pays attention to IO_DIRECT for reads. If the
686 * DIRECTIO option is configured into the kernel, it calls
687 * ffs_rawread(). But that only works for single-segment
688 * uios with user space addresses. In our case, with a
689 * kernel uio, it still reads into the buffer cache, but it
690 * will just try to release the buffer from the cache later
693 * ZFS does not pay attention to IO_DIRECT for reads.
695 * UFS does not pay attention to IO_SYNC for reads.
697 * ZFS pays attention to IO_SYNC (which translates into the
698 * Solaris define FRSYNC for zfs_read()) for reads. It
699 * attempts to sync the file before reading.
701 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
703 VOP_UNLOCK(be_lun->vn);
704 SDT_PROBE0(cbb, , read, file_done);
705 if (error == 0 && xuio.uio_resid > 0) {
707 * If we red less then requested (EOF), then
708 * we should clean the rest of the buffer.
710 s = beio->io_len - xuio.uio_resid;
711 for (i = 0; i < beio->num_segs; i++) {
712 if (s >= beio->sg_segs[i].len) {
713 s -= beio->sg_segs[i].len;
716 bzero((uint8_t *)beio->sg_segs[i].addr + s,
717 beio->sg_segs[i].len - s);
722 struct mount *mountpoint;
724 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
725 vn_lock(be_lun->vn, vn_lktype_write(mountpoint,
726 be_lun->vn) | LK_RETRY);
729 * UFS pays attention to IO_DIRECT for writes. The write
730 * is done asynchronously. (Normally the write would just
731 * get put into cache.
733 * UFS pays attention to IO_SYNC for writes. It will
734 * attempt to write the buffer out synchronously if that
737 * ZFS does not pay attention to IO_DIRECT for writes.
739 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
740 * for writes. It will flush the transaction from the
741 * cache before returning.
743 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
744 VOP_UNLOCK(be_lun->vn);
746 vn_finished_write(mountpoint);
747 SDT_PROBE0(cbb, , write, file_done);
750 mtx_lock(&be_lun->io_lock);
751 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
752 beio->ds_tag_type, beio->ds_trans_type,
753 /*now*/ NULL, /*then*/&beio->ds_t0);
754 mtx_unlock(&be_lun->io_lock);
757 * If we got an error, set the sense data to "MEDIUM ERROR" and
758 * return the I/O to the user.
761 if (error == ENOSPC || error == EDQUOT) {
762 ctl_set_space_alloc_fail(&io->scsiio);
763 } else if (error == EROFS || error == EACCES) {
764 ctl_set_hw_write_protected(&io->scsiio);
766 ctl_set_medium_error(&io->scsiio,
767 beio->bio_cmd == BIO_READ);
769 ctl_complete_beio(beio);
774 * If this is a write or a verify, we're all done.
775 * If this is a read, we can now send the data to the user.
777 if ((beio->bio_cmd == BIO_WRITE) ||
778 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
779 ctl_set_success(&io->scsiio);
780 ctl_complete_beio(beio);
782 if ((ARGS(io)->flags & CTL_LLF_READ) &&
783 beio->beio_cont == NULL) {
784 ctl_set_success(&io->scsiio);
785 if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
793 ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
794 struct ctl_be_block_io *beio)
796 union ctl_io *io = beio->io;
797 struct ctl_lba_len_flags *lbalen = ARGS(io);
798 struct scsi_get_lba_status_data *data;
802 DPRINTF("entered\n");
804 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
805 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
806 error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
807 0, curthread->td_ucred, curthread);
808 if (error == 0 && off > roff)
809 status = 0; /* mapped up to off */
811 error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
812 0, curthread->td_ucred, curthread);
813 if (error == 0 && off > roff)
814 status = 1; /* deallocated up to off */
816 status = 0; /* unknown up to the end */
817 off = be_lun->size_bytes;
820 VOP_UNLOCK(be_lun->vn);
822 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
823 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
824 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
825 lbalen->lba), data->descr[0].length);
826 data->descr[0].status = status;
828 ctl_complete_beio(beio);
832 ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
835 struct statfs statfs;
840 if (be_lun->vn == NULL)
842 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
843 if (strcmp(attrname, "blocksused") == 0) {
844 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
846 val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
848 if (strcmp(attrname, "blocksavail") == 0 &&
849 !VN_IS_DOOMED(be_lun->vn)) {
850 error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
852 val = statfs.f_bavail * statfs.f_bsize /
853 be_lun->cbe_lun.blocksize;
855 VOP_UNLOCK(be_lun->vn);
860 ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
861 struct ctl_be_block_io *beio)
863 struct ctl_be_block_filedata *file_data;
865 struct ctl_ptr_len_flags *ptrlen;
866 struct scsi_unmap_desc *buf, *end;
872 file_data = &be_lun->backend.file;
876 binuptime(&beio->ds_t0);
877 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
879 (void)vn_start_write(be_lun->vn, &mp, V_WAIT);
880 vn_lock(be_lun->vn, vn_lktype_write(mp, be_lun->vn) | LK_RETRY);
881 if (beio->io_offset == -1) {
883 ptrlen = (struct ctl_ptr_len_flags *)
884 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
885 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
886 end = buf + ptrlen->len / sizeof(*buf);
887 for (; buf < end; buf++) {
888 off = (off_t)scsi_8btou64(buf->lba) *
889 be_lun->cbe_lun.blocksize;
890 len = (off_t)scsi_4btoul(buf->length) *
891 be_lun->cbe_lun.blocksize;
893 error = vn_deallocate(be_lun->vn, &off, &len,
894 0, IO_NOMACCHECK | IO_NODELOCKED, file_data->cred,
901 off = beio->io_offset;
903 error = vn_deallocate(be_lun->vn, &off, &len, 0,
904 IO_NOMACCHECK | IO_NODELOCKED, file_data->cred, NOCRED);
906 VOP_UNLOCK(be_lun->vn);
907 vn_finished_write(mp);
909 mtx_lock(&be_lun->io_lock);
910 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
911 beio->ds_tag_type, beio->ds_trans_type,
912 /*now*/ NULL, /*then*/&beio->ds_t0);
913 mtx_unlock(&be_lun->io_lock);
916 * If we got an error, set the sense data to "MEDIUM ERROR" and
917 * return the I/O to the user.
921 ctl_set_success(&io->scsiio);
925 ctl_set_space_alloc_fail(&io->scsiio);
929 ctl_set_hw_write_protected(&io->scsiio);
932 ctl_set_medium_error(&io->scsiio, false);
934 ctl_complete_beio(beio);
938 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
939 struct ctl_be_block_io *beio)
941 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
946 struct iovec *xiovec;
947 int error, flags, i, ref;
949 DPRINTF("entered\n");
953 if (ARGS(io)->flags & CTL_LLF_DPO)
955 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
958 bzero(&xuio, sizeof(xuio));
959 if (beio->bio_cmd == BIO_READ) {
960 SDT_PROBE0(cbb, , read, file_start);
961 xuio.uio_rw = UIO_READ;
963 SDT_PROBE0(cbb, , write, file_start);
964 xuio.uio_rw = UIO_WRITE;
966 xuio.uio_offset = beio->io_offset;
967 xuio.uio_resid = beio->io_len;
968 xuio.uio_segflg = UIO_SYSSPACE;
969 xuio.uio_iov = beio->xiovecs;
970 xuio.uio_iovcnt = beio->num_segs;
971 xuio.uio_td = curthread;
973 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
974 xiovec->iov_base = beio->sg_segs[i].addr;
975 xiovec->iov_len = beio->sg_segs[i].len;
978 binuptime(&beio->ds_t0);
979 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
981 csw = devvn_refthread(be_lun->vn, &dev, &ref);
983 if (beio->bio_cmd == BIO_READ) {
984 if (beio->beio_cont == NULL &&
985 cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
987 error = csw->d_read(dev, &xuio, flags);
989 error = csw->d_write(dev, &xuio, flags);
990 dev_relthread(dev, ref);
994 if (beio->bio_cmd == BIO_READ)
995 SDT_PROBE0(cbb, , read, file_done);
997 SDT_PROBE0(cbb, , write, file_done);
999 mtx_lock(&be_lun->io_lock);
1000 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
1001 beio->ds_tag_type, beio->ds_trans_type,
1002 /*now*/ NULL, /*then*/&beio->ds_t0);
1003 mtx_unlock(&be_lun->io_lock);
1006 * If we got an error, set the sense data to "MEDIUM ERROR" and
1007 * return the I/O to the user.
1010 if (error == ENOSPC || error == EDQUOT) {
1011 ctl_set_space_alloc_fail(&io->scsiio);
1012 } else if (error == EROFS || error == EACCES) {
1013 ctl_set_hw_write_protected(&io->scsiio);
1015 ctl_set_medium_error(&io->scsiio,
1016 beio->bio_cmd == BIO_READ);
1018 ctl_complete_beio(beio);
1023 * If this is a write or a verify, we're all done.
1024 * If this is a read, we can now send the data to the user.
1026 if ((beio->bio_cmd == BIO_WRITE) ||
1027 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
1028 ctl_set_success(&io->scsiio);
1029 ctl_complete_beio(beio);
1031 if ((ARGS(io)->flags & CTL_LLF_READ) &&
1032 beio->beio_cont == NULL) {
1033 ctl_set_success(&io->scsiio);
1034 if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
1035 ctl_serseq_done(io);
1042 ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
1043 struct ctl_be_block_io *beio)
1045 union ctl_io *io = beio->io;
1048 struct ctl_lba_len_flags *lbalen = ARGS(io);
1049 struct scsi_get_lba_status_data *data;
1051 int error, ref, status;
1053 DPRINTF("entered\n");
1055 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1057 status = 0; /* unknown up to the end */
1058 off = be_lun->size_bytes;
1061 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
1062 error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
1064 if (error == 0 && off > roff)
1065 status = 0; /* mapped up to off */
1067 error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
1069 if (error == 0 && off > roff)
1070 status = 1; /* deallocated up to off */
1072 status = 0; /* unknown up to the end */
1073 off = be_lun->size_bytes;
1076 dev_relthread(dev, ref);
1079 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
1080 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
1081 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
1082 lbalen->lba), data->descr[0].length);
1083 data->descr[0].status = status;
1085 ctl_complete_beio(beio);
1089 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1090 struct ctl_be_block_io *beio)
1097 DPRINTF("entered\n");
1099 /* This can't fail, it's a blocking allocation. */
1100 bio = g_alloc_bio();
1102 bio->bio_cmd = BIO_FLUSH;
1103 bio->bio_offset = 0;
1105 bio->bio_done = ctl_be_block_biodone;
1106 bio->bio_caller1 = beio;
1107 bio->bio_pblkno = 0;
1110 * We don't need to acquire the LUN lock here, because we are only
1111 * sending one bio, and so there is no other context to synchronize
1114 beio->num_bios_sent = 1;
1115 beio->send_complete = 1;
1117 binuptime(&beio->ds_t0);
1118 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1120 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1123 csw->d_strategy(bio);
1124 dev_relthread(dev, ref);
1126 bio->bio_error = ENXIO;
1127 ctl_be_block_biodone(bio);
1132 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1133 struct ctl_be_block_io *beio,
1134 uint64_t off, uint64_t len, int last)
1142 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1143 maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1145 bio = g_alloc_bio();
1146 bio->bio_cmd = BIO_DELETE;
1148 bio->bio_offset = off;
1149 bio->bio_length = MIN(len, maxlen);
1151 bio->bio_done = ctl_be_block_biodone;
1152 bio->bio_caller1 = beio;
1153 bio->bio_pblkno = off / be_lun->cbe_lun.blocksize;
1155 off += bio->bio_length;
1156 len -= bio->bio_length;
1158 mtx_lock(&be_lun->io_lock);
1159 beio->num_bios_sent++;
1160 if (last && len == 0)
1161 beio->send_complete = 1;
1162 mtx_unlock(&be_lun->io_lock);
1165 csw->d_strategy(bio);
1167 bio->bio_error = ENXIO;
1168 ctl_be_block_biodone(bio);
1172 dev_relthread(dev, ref);
1176 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1177 struct ctl_be_block_io *beio)
1180 struct ctl_ptr_len_flags *ptrlen;
1181 struct scsi_unmap_desc *buf, *end;
1186 DPRINTF("entered\n");
1188 binuptime(&beio->ds_t0);
1189 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1191 if (beio->io_offset == -1) {
1193 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1194 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1195 end = buf + ptrlen->len / sizeof(*buf);
1196 for (; buf < end; buf++) {
1197 len = (uint64_t)scsi_4btoul(buf->length) *
1198 be_lun->cbe_lun.blocksize;
1199 beio->io_len += len;
1200 ctl_be_block_unmap_dev_range(be_lun, beio,
1201 scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
1202 len, (end - buf < 2) ? TRUE : FALSE);
1205 ctl_be_block_unmap_dev_range(be_lun, beio,
1206 beio->io_offset, beio->io_len, TRUE);
1210 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1211 struct ctl_be_block_io *beio)
1213 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1218 int i, max_iosize, ref;
1220 DPRINTF("entered\n");
1221 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1224 * We have to limit our I/O size to the maximum supported by the
1228 max_iosize = dev->si_iosize_max;
1229 if (max_iosize <= 0)
1230 max_iosize = DFLTPHYS;
1232 max_iosize = maxphys;
1234 cur_offset = beio->io_offset;
1235 for (i = 0; i < beio->num_segs; i++) {
1239 cur_size = beio->sg_segs[i].len;
1240 cur_ptr = beio->sg_segs[i].addr;
1242 while (cur_size > 0) {
1243 /* This can't fail, it's a blocking allocation. */
1244 bio = g_alloc_bio();
1246 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1248 bio->bio_cmd = beio->bio_cmd;
1250 bio->bio_caller1 = beio;
1251 bio->bio_length = min(cur_size, max_iosize);
1252 bio->bio_offset = cur_offset;
1253 bio->bio_data = cur_ptr;
1254 bio->bio_done = ctl_be_block_biodone;
1255 bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1257 cur_offset += bio->bio_length;
1258 cur_ptr += bio->bio_length;
1259 cur_size -= bio->bio_length;
1261 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1262 beio->num_bios_sent++;
1265 beio->send_complete = 1;
1266 binuptime(&beio->ds_t0);
1267 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1270 * Fire off all allocated requests!
1272 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1273 TAILQ_REMOVE(&queue, bio, bio_queue);
1275 csw->d_strategy(bio);
1277 bio->bio_error = ENXIO;
1278 ctl_be_block_biodone(bio);
1282 dev_relthread(dev, ref);
1286 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1288 struct diocgattr_arg arg;
1293 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1295 return (UINT64_MAX);
1296 strlcpy(arg.name, attrname, sizeof(arg.name));
1297 arg.len = sizeof(arg.value.off);
1299 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
1303 dev_relthread(dev, ref);
1305 return (UINT64_MAX);
1306 return (arg.value.off);
1310 ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1313 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1314 struct ctl_be_block_io *beio;
1315 struct ctl_lba_len_flags *lbalen;
1317 DPRINTF("entered\n");
1318 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1319 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1321 beio->io_len = lbalen->len * cbe_lun->blocksize;
1322 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1323 beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
1324 beio->bio_cmd = BIO_FLUSH;
1325 beio->ds_trans_type = DEVSTAT_NO_DATA;
1327 be_lun->lun_flush(be_lun, beio);
1331 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1336 ctl_free_beio(beio);
1337 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1338 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1339 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1340 ctl_config_write_done(io);
1344 ctl_be_block_config_write(io);
1348 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1351 struct ctl_be_block_softc *softc = be_lun->softc;
1352 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1353 struct ctl_be_block_io *beio;
1354 struct ctl_lba_len_flags *lbalen;
1355 uint64_t len_left, lba;
1356 uint32_t pb, pbo, adj;
1360 DPRINTF("entered\n");
1362 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1365 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1366 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1367 ctl_free_beio(beio);
1368 ctl_set_invalid_field(&io->scsiio,
1374 ctl_config_write_done(io);
1378 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1379 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1380 beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1381 beio->bio_cmd = BIO_DELETE;
1382 beio->ds_trans_type = DEVSTAT_FREE;
1384 be_lun->unmap(be_lun, beio);
1388 beio->bio_cmd = BIO_WRITE;
1389 beio->ds_trans_type = DEVSTAT_WRITE;
1391 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1392 (uintmax_t)lbalen->lba, lbalen->len);
1394 pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
1395 if (be_lun->cbe_lun.pblockoff > 0)
1396 pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1399 len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1400 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1402 * Setup the S/G entry for this chunk.
1404 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1405 if (pb > cbe_lun->blocksize) {
1406 adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
1411 seglen -= seglen % cbe_lun->blocksize;
1413 seglen -= seglen % cbe_lun->blocksize;
1414 ctl_alloc_seg(softc, &beio->sg_segs[i], seglen);
1416 DPRINTF("segment %d addr %p len %zd\n", i,
1417 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1422 buf = beio->sg_segs[i].addr;
1424 for (; buf < end; buf += cbe_lun->blocksize) {
1425 if (lbalen->flags & SWS_NDOB) {
1426 memset(buf, 0, cbe_lun->blocksize);
1428 memcpy(buf, io->scsiio.kern_data_ptr,
1429 cbe_lun->blocksize);
1431 if (lbalen->flags & SWS_LBDATA)
1432 scsi_ulto4b(lbalen->lba + lba, buf);
1437 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1438 beio->io_len = lba * cbe_lun->blocksize;
1440 /* We can not do all in one run. Correct and schedule rerun. */
1444 beio->beio_cont = ctl_be_block_cw_done_ws;
1447 be_lun->dispatch(be_lun, beio);
1451 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1454 struct ctl_be_block_io *beio;
1455 struct ctl_ptr_len_flags *ptrlen;
1457 DPRINTF("entered\n");
1459 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1460 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1462 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1463 ctl_free_beio(beio);
1464 ctl_set_invalid_field(&io->scsiio,
1470 ctl_config_write_done(io);
1475 beio->io_offset = -1;
1476 beio->bio_cmd = BIO_DELETE;
1477 beio->ds_trans_type = DEVSTAT_FREE;
1479 be_lun->unmap(be_lun, beio);
1483 ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1488 ctl_free_beio(beio);
1489 ctl_config_read_done(io);
1493 ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1496 struct ctl_be_block_io *beio;
1497 struct ctl_be_block_softc *softc;
1499 DPRINTF("entered\n");
1501 softc = be_lun->softc;
1502 beio = ctl_alloc_beio(softc);
1505 beio->beio_cont = ctl_be_block_cr_done;
1506 PRIV(io)->ptr = (void *)beio;
1508 switch (io->scsiio.cdb[0]) {
1509 case SERVICE_ACTION_IN: /* GET LBA STATUS */
1511 beio->ds_trans_type = DEVSTAT_NO_DATA;
1512 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1514 if (be_lun->get_lba_status)
1515 be_lun->get_lba_status(be_lun, beio);
1517 ctl_be_block_cr_done(beio);
1520 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1526 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1531 ctl_free_beio(beio);
1532 ctl_config_write_done(io);
1536 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1539 struct ctl_be_block_io *beio;
1540 struct ctl_be_block_softc *softc;
1542 DPRINTF("entered\n");
1544 softc = be_lun->softc;
1545 beio = ctl_alloc_beio(softc);
1548 beio->beio_cont = ctl_be_block_cw_done;
1549 switch (io->scsiio.tag_type) {
1550 case CTL_TAG_ORDERED:
1551 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1553 case CTL_TAG_HEAD_OF_QUEUE:
1554 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1556 case CTL_TAG_UNTAGGED:
1557 case CTL_TAG_SIMPLE:
1560 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1563 PRIV(io)->ptr = (void *)beio;
1565 switch (io->scsiio.cdb[0]) {
1566 case SYNCHRONIZE_CACHE:
1567 case SYNCHRONIZE_CACHE_16:
1568 ctl_be_block_cw_dispatch_sync(be_lun, io);
1572 ctl_be_block_cw_dispatch_ws(be_lun, io);
1575 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1578 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1583 SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
1584 SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
1585 SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
1586 SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1589 ctl_be_block_next(struct ctl_be_block_io *beio)
1591 struct ctl_be_block_lun *be_lun;
1596 ctl_free_beio(beio);
1597 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1598 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1599 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1600 ctl_data_submit_done(io);
1604 io->io_hdr.status &= ~CTL_STATUS_MASK;
1605 io->io_hdr.status |= CTL_STATUS_NONE;
1607 mtx_lock(&be_lun->queue_lock);
1608 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1609 mtx_unlock(&be_lun->queue_lock);
1610 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1614 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1617 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1618 struct ctl_be_block_io *beio;
1619 struct ctl_be_block_softc *softc;
1620 struct ctl_lba_len_flags *lbalen;
1621 struct ctl_ptr_len_flags *bptrlen;
1622 uint64_t len_left, lbas;
1625 softc = be_lun->softc;
1627 DPRINTF("entered\n");
1630 if (lbalen->flags & CTL_LLF_WRITE) {
1631 SDT_PROBE0(cbb, , write, start);
1633 SDT_PROBE0(cbb, , read, start);
1636 beio = ctl_alloc_beio(softc);
1640 bptrlen->ptr = (void *)beio;
1642 switch (io->scsiio.tag_type) {
1643 case CTL_TAG_ORDERED:
1644 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1646 case CTL_TAG_HEAD_OF_QUEUE:
1647 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1649 case CTL_TAG_UNTAGGED:
1650 case CTL_TAG_SIMPLE:
1653 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1657 if (lbalen->flags & CTL_LLF_WRITE) {
1658 beio->bio_cmd = BIO_WRITE;
1659 beio->ds_trans_type = DEVSTAT_WRITE;
1661 beio->bio_cmd = BIO_READ;
1662 beio->ds_trans_type = DEVSTAT_READ;
1665 DPRINTF("%s at LBA %jx len %u @%ju\n",
1666 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1667 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1668 lbas = CTLBLK_MAX_IO_SIZE;
1669 if (lbalen->flags & CTL_LLF_COMPARE) {
1670 beio->two_sglists = 1;
1673 lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
1674 beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
1675 beio->io_len = lbas * cbe_lun->blocksize;
1676 bptrlen->len += lbas;
1678 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1679 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1680 i, CTLBLK_MAX_SEGS));
1683 * Setup the S/G entry for this chunk.
1685 ctl_alloc_seg(softc, &beio->sg_segs[i],
1686 MIN(CTLBLK_MAX_SEG, len_left));
1688 DPRINTF("segment %d addr %p len %zd\n", i,
1689 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1691 /* Set up second segment for compare operation. */
1692 if (beio->two_sglists) {
1693 ctl_alloc_seg(softc,
1694 &beio->sg_segs[i + CTLBLK_HALF_SEGS],
1695 beio->sg_segs[i].len);
1699 len_left -= beio->sg_segs[i].len;
1701 if (bptrlen->len < lbalen->len)
1702 beio->beio_cont = ctl_be_block_next;
1703 io->scsiio.be_move_done = ctl_be_block_move_done;
1704 /* For compare we have separate S/G lists for read and datamove. */
1705 if (beio->two_sglists)
1706 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1708 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1709 io->scsiio.kern_data_len = beio->io_len;
1710 io->scsiio.kern_sg_entries = beio->num_segs;
1711 io->scsiio.kern_data_ref = ctl_refcnt_beio;
1712 io->scsiio.kern_data_arg = beio;
1713 io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1716 * For the read case, we need to read the data into our buffers and
1717 * then we can send it back to the user. For the write case, we
1718 * need to get the data from the user first.
1720 if (beio->bio_cmd == BIO_READ) {
1721 SDT_PROBE0(cbb, , read, alloc_done);
1722 be_lun->dispatch(be_lun, beio);
1724 SDT_PROBE0(cbb, , write, alloc_done);
1730 ctl_be_block_worker(void *context, int pending)
1732 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1733 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1735 struct ctl_be_block_io *beio;
1737 DPRINTF("entered\n");
1739 * Fetch and process I/Os from all queues. If we detect LUN
1740 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1741 * so make response maximally opaque to not confuse initiator.
1744 mtx_lock(&be_lun->queue_lock);
1745 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1747 DPRINTF("datamove queue\n");
1748 STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links);
1749 mtx_unlock(&be_lun->queue_lock);
1750 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1751 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1752 ctl_set_busy(&io->scsiio);
1753 ctl_complete_beio(beio);
1756 be_lun->dispatch(be_lun, beio);
1759 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1761 DPRINTF("config write queue\n");
1762 STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links);
1763 mtx_unlock(&be_lun->queue_lock);
1764 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1765 ctl_set_busy(&io->scsiio);
1766 ctl_config_write_done(io);
1769 ctl_be_block_cw_dispatch(be_lun, io);
1772 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1774 DPRINTF("config read queue\n");
1775 STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links);
1776 mtx_unlock(&be_lun->queue_lock);
1777 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1778 ctl_set_busy(&io->scsiio);
1779 ctl_config_read_done(io);
1782 ctl_be_block_cr_dispatch(be_lun, io);
1785 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1787 DPRINTF("input queue\n");
1788 STAILQ_REMOVE_HEAD(&be_lun->input_queue, links);
1789 mtx_unlock(&be_lun->queue_lock);
1790 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1791 ctl_set_busy(&io->scsiio);
1792 ctl_data_submit_done(io);
1795 ctl_be_block_dispatch(be_lun, io);
1800 * If we get here, there is no work left in the queues, so
1801 * just break out and let the task queue go to sleep.
1803 mtx_unlock(&be_lun->queue_lock);
1809 * Entry point from CTL to the backend for I/O. We queue everything to a
1810 * work thread, so this just puts the I/O on a queue and wakes up the
1814 ctl_be_block_submit(union ctl_io *io)
1816 struct ctl_be_block_lun *be_lun;
1818 DPRINTF("entered\n");
1820 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1822 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1823 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
1827 mtx_lock(&be_lun->queue_lock);
1828 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1829 mtx_unlock(&be_lun->queue_lock);
1830 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1832 return (CTL_RETVAL_COMPLETE);
1836 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1837 int flag, struct thread *td)
1839 struct ctl_be_block_softc *softc = &backend_block_softc;
1845 struct ctl_lun_req *lun_req;
1847 lun_req = (struct ctl_lun_req *)addr;
1849 switch (lun_req->reqtype) {
1850 case CTL_LUNREQ_CREATE:
1851 error = ctl_be_block_create(softc, lun_req);
1854 error = ctl_be_block_rm(softc, lun_req);
1856 case CTL_LUNREQ_MODIFY:
1857 error = ctl_be_block_modify(softc, lun_req);
1860 lun_req->status = CTL_LUN_ERROR;
1861 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1862 "invalid LUN request type %d",
1877 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1879 struct ctl_be_lun *cbe_lun;
1880 struct ctl_be_block_filedata *file_data;
1881 struct ctl_lun_create_params *params;
1884 off_t ps, pss, po, pos, us, uss, uo, uos;
1888 cbe_lun = &be_lun->cbe_lun;
1889 file_data = &be_lun->backend.file;
1890 params = &be_lun->params;
1892 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1893 be_lun->dispatch = ctl_be_block_dispatch_file;
1894 be_lun->lun_flush = ctl_be_block_flush_file;
1895 be_lun->get_lba_status = ctl_be_block_gls_file;
1896 be_lun->getattr = ctl_be_block_getattr_file;
1897 be_lun->unmap = ctl_be_block_unmap_file;
1898 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1900 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1902 snprintf(req->error_str, sizeof(req->error_str),
1903 "error calling VOP_GETATTR() for file %s",
1908 error = VOP_PATHCONF(be_lun->vn, _PC_DEALLOC_PRESENT, &pconf);
1910 snprintf(req->error_str, sizeof(req->error_str),
1911 "error calling VOP_PATHCONF() for file %s",
1916 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
1918 file_data->cred = crhold(curthread->td_ucred);
1919 if (params->lun_size_bytes != 0)
1920 be_lun->size_bytes = params->lun_size_bytes;
1922 be_lun->size_bytes = vattr.va_size;
1925 * For files we can use any logical block size. Prefer 512 bytes
1926 * for compatibility reasons. If file's vattr.va_blocksize
1927 * (preferred I/O block size) is bigger and multiple to chosen
1928 * logical block size -- report it as physical block size.
1930 if (params->blocksize_bytes != 0)
1931 cbe_lun->blocksize = params->blocksize_bytes;
1932 else if (cbe_lun->lun_type == T_CDROM)
1933 cbe_lun->blocksize = 2048;
1935 cbe_lun->blocksize = 512;
1936 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1937 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1938 0 : (be_lun->size_blocks - 1);
1940 us = ps = vattr.va_blocksize;
1943 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
1945 ctl_expand_number(value, &ps);
1946 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
1948 ctl_expand_number(value, &po);
1949 pss = ps / cbe_lun->blocksize;
1950 pos = po / cbe_lun->blocksize;
1951 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
1952 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
1953 cbe_lun->pblockexp = fls(pss) - 1;
1954 cbe_lun->pblockoff = (pss - pos) % pss;
1957 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
1959 ctl_expand_number(value, &us);
1960 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
1962 ctl_expand_number(value, &uo);
1963 uss = us / cbe_lun->blocksize;
1964 uos = uo / cbe_lun->blocksize;
1965 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
1966 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
1967 cbe_lun->ublockexp = fls(uss) - 1;
1968 cbe_lun->ublockoff = (uss - uos) % uss;
1972 * Sanity check. The media size has to be at least one
1975 if (be_lun->size_bytes < cbe_lun->blocksize) {
1977 snprintf(req->error_str, sizeof(req->error_str),
1978 "file %s size %ju < block size %u", be_lun->dev_path,
1979 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1982 cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1987 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1989 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1990 struct ctl_lun_create_params *params;
1994 int error, atomic, maxio, ref, unmap, tmp;
1995 off_t ps, pss, po, pos, us, uss, uo, uos, otmp;
1997 params = &be_lun->params;
1999 be_lun->dev_type = CTL_BE_BLOCK_DEV;
2000 csw = devvn_refthread(be_lun->vn, &dev, &ref);
2003 if (strcmp(csw->d_name, "zvol") == 0) {
2004 be_lun->dispatch = ctl_be_block_dispatch_zvol;
2005 be_lun->get_lba_status = ctl_be_block_gls_zvol;
2006 atomic = maxio = CTLBLK_MAX_IO_SIZE;
2008 be_lun->dispatch = ctl_be_block_dispatch_dev;
2009 be_lun->get_lba_status = NULL;
2011 maxio = dev->si_iosize_max;
2014 if (maxio > CTLBLK_MAX_SEG)
2015 maxio = CTLBLK_MAX_SEG;
2017 be_lun->lun_flush = ctl_be_block_flush_dev;
2018 be_lun->getattr = ctl_be_block_getattr_dev;
2019 be_lun->unmap = ctl_be_block_unmap_dev;
2021 if (!csw->d_ioctl) {
2022 dev_relthread(dev, ref);
2023 snprintf(req->error_str, sizeof(req->error_str),
2024 "no d_ioctl for device %s!", be_lun->dev_path);
2028 error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
2031 dev_relthread(dev, ref);
2032 snprintf(req->error_str, sizeof(req->error_str),
2033 "error %d returned for DIOCGSECTORSIZE ioctl "
2034 "on %s!", error, be_lun->dev_path);
2039 * If the user has asked for a blocksize that is greater than the
2040 * backing device's blocksize, we can do it only if the blocksize
2041 * the user is asking for is an even multiple of the underlying
2042 * device's blocksize.
2044 if ((params->blocksize_bytes != 0) &&
2045 (params->blocksize_bytes >= tmp)) {
2046 if (params->blocksize_bytes % tmp == 0) {
2047 cbe_lun->blocksize = params->blocksize_bytes;
2049 dev_relthread(dev, ref);
2050 snprintf(req->error_str, sizeof(req->error_str),
2051 "requested blocksize %u is not an even "
2052 "multiple of backing device blocksize %u",
2053 params->blocksize_bytes, tmp);
2056 } else if (params->blocksize_bytes != 0) {
2057 dev_relthread(dev, ref);
2058 snprintf(req->error_str, sizeof(req->error_str),
2059 "requested blocksize %u < backing device "
2060 "blocksize %u", params->blocksize_bytes, tmp);
2062 } else if (cbe_lun->lun_type == T_CDROM)
2063 cbe_lun->blocksize = MAX(tmp, 2048);
2065 cbe_lun->blocksize = tmp;
2067 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
2070 dev_relthread(dev, ref);
2071 snprintf(req->error_str, sizeof(req->error_str),
2072 "error %d returned for DIOCGMEDIASIZE "
2073 " ioctl on %s!", error,
2078 if (params->lun_size_bytes != 0) {
2079 if (params->lun_size_bytes > otmp) {
2080 dev_relthread(dev, ref);
2081 snprintf(req->error_str, sizeof(req->error_str),
2082 "requested LUN size %ju > backing device "
2084 (uintmax_t)params->lun_size_bytes,
2089 be_lun->size_bytes = params->lun_size_bytes;
2091 be_lun->size_bytes = otmp;
2092 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2093 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2094 0 : (be_lun->size_blocks - 1);
2096 error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
2101 error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
2109 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
2111 ctl_expand_number(value, &ps);
2112 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
2114 ctl_expand_number(value, &po);
2115 pss = ps / cbe_lun->blocksize;
2116 pos = po / cbe_lun->blocksize;
2117 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
2118 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
2119 cbe_lun->pblockexp = fls(pss) - 1;
2120 cbe_lun->pblockoff = (pss - pos) % pss;
2123 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
2125 ctl_expand_number(value, &us);
2126 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
2128 ctl_expand_number(value, &uo);
2129 uss = us / cbe_lun->blocksize;
2130 uos = uo / cbe_lun->blocksize;
2131 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
2132 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
2133 cbe_lun->ublockexp = fls(uss) - 1;
2134 cbe_lun->ublockoff = (uss - uos) % uss;
2137 cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
2138 cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2140 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2143 struct diocgattr_arg arg;
2145 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2146 arg.len = sizeof(arg.value.i);
2147 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
2149 unmap = (error == 0) ? arg.value.i : 0;
2151 value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2153 unmap = (strcmp(value, "on") == 0);
2155 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
2157 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2159 dev_relthread(dev, ref);
2164 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2166 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2171 if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
2173 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2176 switch (be_lun->dev_type) {
2177 case CTL_BE_BLOCK_DEV:
2179 case CTL_BE_BLOCK_FILE:
2180 if (be_lun->backend.file.cred != NULL) {
2181 crfree(be_lun->backend.file.cred);
2182 be_lun->backend.file.cred = NULL;
2185 case CTL_BE_BLOCK_NONE:
2188 panic("Unexpected backend type %d", be_lun->dev_type);
2191 be_lun->dev_type = CTL_BE_BLOCK_NONE;
2197 ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2199 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2200 struct nameidata nd;
2205 if (rootvnode == NULL) {
2206 snprintf(req->error_str, sizeof(req->error_str),
2207 "Root filesystem is not mounted");
2212 value = dnvlist_get_string(cbe_lun->options, "file", NULL);
2213 if (value == NULL) {
2214 snprintf(req->error_str, sizeof(req->error_str),
2215 "no file argument specified");
2218 free(be_lun->dev_path, M_CTLBLK);
2219 be_lun->dev_path = strdup(value, M_CTLBLK);
2222 value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
2223 if (value != NULL) {
2224 if (strcmp(value, "on") != 0)
2226 } else if (cbe_lun->lun_type == T_DIRECT)
2230 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path);
2231 error = vn_open(&nd, &flags, 0, NULL);
2232 if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
2238 * This is the only reasonable guess we can make as far as
2239 * path if the user doesn't give us a fully qualified path.
2240 * If they want to specify a file, they need to specify the
2243 if (be_lun->dev_path[0] != '/') {
2246 asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2248 free(be_lun->dev_path, M_CTLBLK);
2249 be_lun->dev_path = dev_name;
2252 snprintf(req->error_str, sizeof(req->error_str),
2253 "error opening %s: %d", be_lun->dev_path, error);
2257 cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
2259 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2262 be_lun->vn = nd.ni_vp;
2264 /* We only support disks and files. */
2265 if (vn_isdisk_error(be_lun->vn, &error)) {
2266 error = ctl_be_block_open_dev(be_lun, req);
2267 } else if (be_lun->vn->v_type == VREG) {
2268 error = ctl_be_block_open_file(be_lun, req);
2271 snprintf(req->error_str, sizeof(req->error_str),
2272 "%s is not a disk or plain file", be_lun->dev_path);
2274 VOP_UNLOCK(be_lun->vn);
2277 ctl_be_block_close(be_lun);
2278 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2279 if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2280 cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
2281 value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
2282 if (value != NULL && strcmp(value, "on") == 0)
2283 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
2284 else if (value != NULL && strcmp(value, "read") == 0)
2285 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2286 else if (value != NULL && strcmp(value, "soft") == 0)
2287 cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
2288 else if (value != NULL && strcmp(value, "off") == 0)
2289 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2294 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2296 struct ctl_be_lun *cbe_lun;
2297 struct ctl_be_block_lun *be_lun;
2298 struct ctl_lun_create_params *params;
2299 char num_thread_str[16];
2302 int retval, num_threads;
2303 int tmp_num_threads;
2305 params = &req->reqdata.create;
2307 req->status = CTL_LUN_OK;
2309 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2310 cbe_lun = &be_lun->cbe_lun;
2311 be_lun->params = req->reqdata.create;
2312 be_lun->softc = softc;
2313 STAILQ_INIT(&be_lun->input_queue);
2314 STAILQ_INIT(&be_lun->config_read_queue);
2315 STAILQ_INIT(&be_lun->config_write_queue);
2316 STAILQ_INIT(&be_lun->datamove_queue);
2317 mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
2318 mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
2319 cbe_lun->options = nvlist_clone(req->args_nvl);
2321 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2322 cbe_lun->lun_type = params->device_type;
2324 cbe_lun->lun_type = T_DIRECT;
2327 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2328 if (value != NULL) {
2329 if (strcmp(value, "primary") == 0)
2330 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2331 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2332 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2334 if (cbe_lun->lun_type == T_DIRECT ||
2335 cbe_lun->lun_type == T_CDROM) {
2336 be_lun->size_bytes = params->lun_size_bytes;
2337 if (params->blocksize_bytes != 0)
2338 cbe_lun->blocksize = params->blocksize_bytes;
2339 else if (cbe_lun->lun_type == T_CDROM)
2340 cbe_lun->blocksize = 2048;
2342 cbe_lun->blocksize = 512;
2343 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2344 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2345 0 : (be_lun->size_blocks - 1);
2347 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2348 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2349 retval = ctl_be_block_open(be_lun, req);
2352 req->status = CTL_LUN_WARNING;
2355 num_threads = cbb_num_threads;
2360 value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
2361 if (value != NULL) {
2362 tmp_num_threads = strtol(value, NULL, 0);
2365 * We don't let the user specify less than one
2366 * thread, but hope he's clueful enough not to
2367 * specify 1000 threads.
2369 if (tmp_num_threads < 1) {
2370 snprintf(req->error_str, sizeof(req->error_str),
2371 "invalid number of threads %s",
2375 num_threads = tmp_num_threads;
2378 if (be_lun->vn == NULL)
2379 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2380 /* Tell the user the blocksize we ended up using */
2381 params->lun_size_bytes = be_lun->size_bytes;
2382 params->blocksize_bytes = cbe_lun->blocksize;
2383 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2384 cbe_lun->req_lun_id = params->req_lun_id;
2385 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2387 cbe_lun->req_lun_id = 0;
2389 cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
2390 cbe_lun->be = &ctl_be_block_driver;
2392 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2393 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2395 strncpy((char *)cbe_lun->serial_num, tmpstr,
2396 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2398 /* Tell the user what we used for a serial number */
2399 strncpy((char *)params->serial_num, tmpstr,
2400 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2402 strncpy((char *)cbe_lun->serial_num, params->serial_num,
2403 MIN(sizeof(cbe_lun->serial_num),
2404 sizeof(params->serial_num)));
2406 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2407 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
2408 strncpy((char *)cbe_lun->device_id, tmpstr,
2409 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2411 /* Tell the user what we used for a device ID */
2412 strncpy((char *)params->device_id, tmpstr,
2413 MIN(sizeof(params->device_id), sizeof(tmpstr)));
2415 strncpy((char *)cbe_lun->device_id, params->device_id,
2416 MIN(sizeof(cbe_lun->device_id),
2417 sizeof(params->device_id)));
2420 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2422 be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2423 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2425 if (be_lun->io_taskqueue == NULL) {
2426 snprintf(req->error_str, sizeof(req->error_str),
2427 "unable to create taskqueue");
2432 * Note that we start the same number of threads by default for
2433 * both the file case and the block device case. For the file
2434 * case, we need multiple threads to allow concurrency, because the
2435 * vnode interface is designed to be a blocking interface. For the
2436 * block device case, ZFS zvols at least will block the caller's
2437 * context in many instances, and so we need multiple threads to
2438 * overcome that problem. Other block devices don't need as many
2439 * threads, but they shouldn't cause too many problems.
2441 * If the user wants to just have a single thread for a block
2442 * device, he can specify that when the LUN is created, or change
2443 * the tunable/sysctl to alter the default number of threads.
2445 retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2446 /*num threads*/num_threads,
2448 /*proc*/control_softc->ctl_proc,
2449 /*thread name*/"block");
2454 be_lun->num_threads = num_threads;
2456 retval = ctl_add_lun(&be_lun->cbe_lun);
2458 snprintf(req->error_str, sizeof(req->error_str),
2459 "ctl_add_lun() returned error %d, see dmesg for "
2465 be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
2467 DEVSTAT_ALL_SUPPORTED,
2469 | DEVSTAT_TYPE_IF_OTHER,
2470 DEVSTAT_PRIORITY_OTHER);
2472 mtx_lock(&softc->lock);
2474 SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
2475 mtx_unlock(&softc->lock);
2477 params->req_lun_id = cbe_lun->lun_id;
2482 req->status = CTL_LUN_ERROR;
2484 if (be_lun->io_taskqueue != NULL)
2485 taskqueue_free(be_lun->io_taskqueue);
2486 ctl_be_block_close(be_lun);
2487 if (be_lun->dev_path != NULL)
2488 free(be_lun->dev_path, M_CTLBLK);
2489 nvlist_destroy(cbe_lun->options);
2490 mtx_destroy(&be_lun->queue_lock);
2491 mtx_destroy(&be_lun->io_lock);
2492 free(be_lun, M_CTLBLK);
2498 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2500 struct ctl_lun_rm_params *params;
2501 struct ctl_be_block_lun *be_lun;
2502 struct ctl_be_lun *cbe_lun;
2505 params = &req->reqdata.rm;
2507 sx_xlock(&softc->modify_lock);
2508 mtx_lock(&softc->lock);
2509 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2510 if (be_lun->cbe_lun.lun_id == params->lun_id) {
2511 SLIST_REMOVE(&softc->lun_list, be_lun,
2512 ctl_be_block_lun, links);
2517 mtx_unlock(&softc->lock);
2518 sx_xunlock(&softc->modify_lock);
2519 if (be_lun == NULL) {
2520 snprintf(req->error_str, sizeof(req->error_str),
2521 "LUN %u is not managed by the block backend",
2525 cbe_lun = &be_lun->cbe_lun;
2527 if (be_lun->vn != NULL) {
2528 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2529 ctl_lun_no_media(cbe_lun);
2530 taskqueue_drain_all(be_lun->io_taskqueue);
2531 ctl_be_block_close(be_lun);
2534 mtx_lock(&softc->lock);
2535 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2536 mtx_unlock(&softc->lock);
2538 retval = ctl_remove_lun(cbe_lun);
2540 snprintf(req->error_str, sizeof(req->error_str),
2541 "error %d returned from ctl_remove_lun() for "
2542 "LUN %d", retval, params->lun_id);
2543 mtx_lock(&softc->lock);
2544 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2545 mtx_unlock(&softc->lock);
2549 mtx_lock(&softc->lock);
2550 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2551 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2552 if (retval == EINTR)
2555 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2556 if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2557 mtx_unlock(&softc->lock);
2558 free(be_lun, M_CTLBLK);
2560 mtx_unlock(&softc->lock);
2564 req->status = CTL_LUN_OK;
2568 req->status = CTL_LUN_ERROR;
2573 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2575 struct ctl_lun_modify_params *params;
2576 struct ctl_be_block_lun *be_lun;
2577 struct ctl_be_lun *cbe_lun;
2582 params = &req->reqdata.modify;
2584 sx_xlock(&softc->modify_lock);
2585 mtx_lock(&softc->lock);
2586 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2587 if (be_lun->cbe_lun.lun_id == params->lun_id)
2590 mtx_unlock(&softc->lock);
2591 if (be_lun == NULL) {
2592 snprintf(req->error_str, sizeof(req->error_str),
2593 "LUN %u is not managed by the block backend",
2597 cbe_lun = &be_lun->cbe_lun;
2599 if (params->lun_size_bytes != 0)
2600 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2602 if (req->args_nvl != NULL) {
2603 nvlist_destroy(cbe_lun->options);
2604 cbe_lun->options = nvlist_clone(req->args_nvl);
2607 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
2608 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2609 if (value != NULL) {
2610 if (strcmp(value, "primary") == 0)
2611 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2613 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2614 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2615 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2617 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2618 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
2619 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
2620 ctl_lun_primary(cbe_lun);
2622 ctl_lun_secondary(cbe_lun);
2625 oldsize = be_lun->size_blocks;
2626 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2627 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2628 if (be_lun->vn == NULL)
2629 error = ctl_be_block_open(be_lun, req);
2630 else if (vn_isdisk_error(be_lun->vn, &error))
2631 error = ctl_be_block_open_dev(be_lun, req);
2632 else if (be_lun->vn->v_type == VREG) {
2633 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2634 error = ctl_be_block_open_file(be_lun, req);
2635 VOP_UNLOCK(be_lun->vn);
2638 if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
2639 be_lun->vn != NULL) {
2640 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2641 ctl_lun_has_media(cbe_lun);
2642 } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2643 be_lun->vn == NULL) {
2644 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2645 ctl_lun_no_media(cbe_lun);
2647 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2649 if (be_lun->vn != NULL) {
2650 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2651 ctl_lun_no_media(cbe_lun);
2652 taskqueue_drain_all(be_lun->io_taskqueue);
2653 error = ctl_be_block_close(be_lun);
2657 if (be_lun->size_blocks != oldsize)
2658 ctl_lun_capacity_changed(cbe_lun);
2660 /* Tell the user the exact size we ended up using */
2661 params->lun_size_bytes = be_lun->size_bytes;
2663 sx_xunlock(&softc->modify_lock);
2664 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2668 sx_xunlock(&softc->modify_lock);
2669 req->status = CTL_LUN_ERROR;
2674 ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2676 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
2677 struct ctl_be_block_softc *softc = be_lun->softc;
2679 taskqueue_drain_all(be_lun->io_taskqueue);
2680 taskqueue_free(be_lun->io_taskqueue);
2681 if (be_lun->disk_stats != NULL)
2682 devstat_remove_entry(be_lun->disk_stats);
2683 nvlist_destroy(be_lun->cbe_lun.options);
2684 free(be_lun->dev_path, M_CTLBLK);
2685 mtx_destroy(&be_lun->queue_lock);
2686 mtx_destroy(&be_lun->io_lock);
2688 mtx_lock(&softc->lock);
2689 be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2690 if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2693 free(be_lun, M_CTLBLK);
2694 mtx_unlock(&softc->lock);
2698 ctl_be_block_config_write(union ctl_io *io)
2700 struct ctl_be_block_lun *be_lun;
2701 struct ctl_be_lun *cbe_lun;
2704 DPRINTF("entered\n");
2706 cbe_lun = CTL_BACKEND_LUN(io);
2707 be_lun = (struct ctl_be_block_lun *)cbe_lun;
2710 switch (io->scsiio.cdb[0]) {
2711 case SYNCHRONIZE_CACHE:
2712 case SYNCHRONIZE_CACHE_16:
2717 * The upper level CTL code will filter out any CDBs with
2718 * the immediate bit set and return the proper error.
2720 * We don't really need to worry about what LBA range the
2721 * user asked to be synced out. When they issue a sync
2722 * cache command, we'll sync out the whole thing.
2724 mtx_lock(&be_lun->queue_lock);
2725 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2727 mtx_unlock(&be_lun->queue_lock);
2728 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2730 case START_STOP_UNIT: {
2731 struct scsi_start_stop_unit *cdb;
2732 struct ctl_lun_req req;
2734 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2735 if ((cdb->how & SSS_PC_MASK) != 0) {
2736 ctl_set_success(&io->scsiio);
2737 ctl_config_write_done(io);
2740 if (cdb->how & SSS_START) {
2741 if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2742 retval = ctl_be_block_open(be_lun, &req);
2743 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2745 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2746 ctl_lun_has_media(cbe_lun);
2748 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2749 ctl_lun_no_media(cbe_lun);
2752 ctl_start_lun(cbe_lun);
2754 ctl_stop_lun(cbe_lun);
2755 if (cdb->how & SSS_LOEJ) {
2756 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2757 cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2758 ctl_lun_ejected(cbe_lun);
2759 if (be_lun->vn != NULL)
2760 ctl_be_block_close(be_lun);
2764 ctl_set_success(&io->scsiio);
2765 ctl_config_write_done(io);
2769 ctl_set_success(&io->scsiio);
2770 ctl_config_write_done(io);
2773 ctl_set_invalid_opcode(&io->scsiio);
2774 ctl_config_write_done(io);
2775 retval = CTL_RETVAL_COMPLETE;
2783 ctl_be_block_config_read(union ctl_io *io)
2785 struct ctl_be_block_lun *be_lun;
2788 DPRINTF("entered\n");
2790 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2792 switch (io->scsiio.cdb[0]) {
2793 case SERVICE_ACTION_IN:
2794 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2795 mtx_lock(&be_lun->queue_lock);
2796 STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2797 &io->io_hdr, links);
2798 mtx_unlock(&be_lun->queue_lock);
2799 taskqueue_enqueue(be_lun->io_taskqueue,
2801 retval = CTL_RETVAL_QUEUED;
2804 ctl_set_invalid_field(&io->scsiio,
2810 ctl_config_read_done(io);
2811 retval = CTL_RETVAL_COMPLETE;
2814 ctl_set_invalid_opcode(&io->scsiio);
2815 ctl_config_read_done(io);
2816 retval = CTL_RETVAL_COMPLETE;
2824 ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2826 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2829 retval = sbuf_cat(sb, "\t<num_threads>");
2832 retval = sbuf_printf(sb, "%d", lun->num_threads);
2835 retval = sbuf_cat(sb, "</num_threads>\n");
2842 ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2844 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2846 if (lun->getattr == NULL)
2847 return (UINT64_MAX);
2848 return (lun->getattr(lun, attrname));
2852 ctl_be_block_init(void)
2854 struct ctl_be_block_softc *softc = &backend_block_softc;
2856 sx_init(&softc->modify_lock, "ctlblock modify");
2857 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2858 softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2859 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2860 softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG,
2861 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2862 if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2863 softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG,
2864 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2865 SLIST_INIT(&softc->lun_list);
2870 ctl_be_block_shutdown(void)
2872 struct ctl_be_block_softc *softc = &backend_block_softc;
2873 struct ctl_be_block_lun *lun;
2875 mtx_lock(&softc->lock);
2876 while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
2877 SLIST_REMOVE_HEAD(&softc->lun_list, links);
2880 * Drop our lock here. Since ctl_remove_lun() can call
2881 * back into us, this could potentially lead to a recursive
2882 * lock of the same mutex, which would cause a hang.
2884 mtx_unlock(&softc->lock);
2885 ctl_remove_lun(&lun->cbe_lun);
2886 mtx_lock(&softc->lock);
2888 mtx_unlock(&softc->lock);
2889 uma_zdestroy(softc->bufmin_zone);
2890 if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2891 uma_zdestroy(softc->bufmax_zone);
2892 uma_zdestroy(softc->beio_zone);
2893 mtx_destroy(&softc->lock);
2894 sx_destroy(&softc->modify_lock);