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/cdefs.h>
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
56 #include <sys/limits.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
65 #include <sys/endian.h>
68 #include <sys/taskqueue.h>
69 #include <sys/vnode.h>
70 #include <sys/namei.h>
71 #include <sys/mount.h>
73 #include <sys/fcntl.h>
74 #include <sys/filedesc.h>
75 #include <sys/filio.h>
78 #include <sys/module.h>
80 #include <sys/devicestat.h>
81 #include <sys/sysctl.h>
85 #include <sys/unistd.h>
87 #include <geom/geom.h>
90 #include <cam/scsi/scsi_all.h>
91 #include <cam/scsi/scsi_da.h>
92 #include <cam/ctl/ctl_io.h>
93 #include <cam/ctl/ctl.h>
94 #include <cam/ctl/ctl_backend.h>
95 #include <cam/ctl/ctl_ioctl.h>
96 #include <cam/ctl/ctl_ha.h>
97 #include <cam/ctl/ctl_scsi_all.h>
98 #include <cam/ctl/ctl_private.h>
99 #include <cam/ctl/ctl_error.h>
102 * The idea here is to allocate enough S/G space to handle at least 1MB I/Os.
103 * On systems with small maxphys it can be 8 128KB segments. On large systems
104 * it can be up to 8 1MB segments. I/Os larger than that we'll split.
106 #define CTLBLK_MAX_SEGS 8
107 #define CTLBLK_HALF_SEGS (CTLBLK_MAX_SEGS / 2)
108 #define CTLBLK_MIN_SEG (128 * 1024)
109 #define CTLBLK_MAX_SEG MIN(1024 * 1024, MAX(CTLBLK_MIN_SEG, maxphys))
110 #define CTLBLK_MAX_IO_SIZE (CTLBLK_MAX_SEG * CTLBLK_MAX_SEGS)
113 #define DPRINTF(fmt, args...) \
114 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
116 #define DPRINTF(fmt, args...) do {} while(0)
120 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
122 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
124 SDT_PROVIDER_DEFINE(cbb);
127 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01,
128 CTL_BE_BLOCK_LUN_WAITING = 0x04,
129 } ctl_be_block_lun_flags;
137 struct ctl_be_block_filedata {
141 union ctl_be_block_bedata {
142 struct ctl_be_block_filedata file;
145 struct ctl_be_block_io;
146 struct ctl_be_block_lun;
148 typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
149 struct ctl_be_block_io *beio);
150 typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
151 const char *attrname);
154 * Backend LUN structure. There is a 1:1 mapping between a block device
155 * and a backend block LUN, and between a backend block LUN and a CTL LUN.
157 struct ctl_be_block_lun {
158 struct ctl_be_lun cbe_lun; /* Must be first element. */
159 struct ctl_lun_create_params params;
161 ctl_be_block_type dev_type;
163 union ctl_be_block_bedata backend;
164 cbb_dispatch_t dispatch;
165 cbb_dispatch_t lun_flush;
166 cbb_dispatch_t unmap;
167 cbb_dispatch_t get_lba_status;
168 cbb_getattr_t getattr;
169 uint64_t size_blocks;
171 struct ctl_be_block_softc *softc;
172 struct devstat *disk_stats;
173 ctl_be_block_lun_flags flags;
174 SLIST_ENTRY(ctl_be_block_lun) links;
175 struct taskqueue *io_taskqueue;
178 STAILQ_HEAD(, ctl_io_hdr) input_queue;
179 STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
180 STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
181 STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
182 struct mtx_padalign io_lock;
183 struct mtx_padalign queue_lock;
187 * Overall softc structure for the block backend module.
189 struct ctl_be_block_softc {
190 struct sx modify_lock;
193 SLIST_HEAD(, ctl_be_block_lun) lun_list;
194 uma_zone_t beio_zone;
195 uma_zone_t bufmin_zone;
196 uma_zone_t bufmax_zone;
199 static struct ctl_be_block_softc backend_block_softc;
202 * Per-I/O information.
204 struct ctl_be_block_io {
206 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS];
207 struct iovec xiovecs[CTLBLK_MAX_SEGS];
216 uint64_t first_error_offset;
217 struct bintime ds_t0;
218 devstat_tag_type ds_tag_type;
219 devstat_trans_flags ds_trans_type;
223 struct ctl_be_block_softc *softc;
224 struct ctl_be_block_lun *lun;
225 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
228 extern struct ctl_softc *control_softc;
230 static int cbb_num_threads = 32;
231 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
232 "CAM Target Layer Block Backend");
233 SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
234 &cbb_num_threads, 0, "Number of threads per backing file");
236 static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
237 static void ctl_free_beio(struct ctl_be_block_io *beio);
238 static void ctl_complete_beio(struct ctl_be_block_io *beio);
239 static int ctl_be_block_move_done(union ctl_io *io, bool samethr);
240 static void ctl_be_block_biodone(struct bio *bio);
241 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
242 struct ctl_be_block_io *beio);
243 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
244 struct ctl_be_block_io *beio);
245 static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
246 struct ctl_be_block_io *beio);
247 static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
248 const char *attrname);
249 static void ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
250 struct ctl_be_block_io *beio);
251 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
252 struct ctl_be_block_io *beio);
253 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
254 struct ctl_be_block_io *beio);
255 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
256 struct ctl_be_block_io *beio);
257 static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
258 const char *attrname);
259 static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
261 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
263 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
265 static void ctl_be_block_worker(void *context, int pending);
266 static int ctl_be_block_submit(union ctl_io *io);
267 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
268 int flag, struct thread *td);
269 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
270 struct ctl_lun_req *req);
271 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
272 struct ctl_lun_req *req);
273 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
274 static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
275 struct ctl_lun_req *req);
276 static int ctl_be_block_create(struct ctl_be_block_softc *softc,
277 struct ctl_lun_req *req);
278 static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
279 struct ctl_lun_req *req);
280 static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
281 struct ctl_lun_req *req);
282 static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun);
283 static int ctl_be_block_config_write(union ctl_io *io);
284 static int ctl_be_block_config_read(union ctl_io *io);
285 static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb);
286 static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname);
287 static int ctl_be_block_init(void);
288 static int ctl_be_block_shutdown(void);
290 static struct ctl_backend_driver ctl_be_block_driver =
293 .flags = CTL_BE_FLAG_HAS_CONFIG,
294 .init = ctl_be_block_init,
295 .shutdown = ctl_be_block_shutdown,
296 .data_submit = ctl_be_block_submit,
297 .config_read = ctl_be_block_config_read,
298 .config_write = ctl_be_block_config_write,
299 .ioctl = ctl_be_block_ioctl,
300 .lun_info = ctl_be_block_lun_info,
301 .lun_attr = ctl_be_block_lun_attr
304 MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend");
305 CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
308 ctl_alloc_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg,
312 if (len <= CTLBLK_MIN_SEG) {
313 sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK);
315 KASSERT(len <= CTLBLK_MAX_SEG,
316 ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG));
317 sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK);
323 ctl_free_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg)
326 if (sg->len <= CTLBLK_MIN_SEG) {
327 uma_zfree(softc->bufmin_zone, sg->addr);
329 KASSERT(sg->len <= CTLBLK_MAX_SEG,
330 ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG));
331 uma_zfree(softc->bufmax_zone, sg->addr);
335 static struct ctl_be_block_io *
336 ctl_alloc_beio(struct ctl_be_block_softc *softc)
338 struct ctl_be_block_io *beio;
340 beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
347 ctl_real_free_beio(struct ctl_be_block_io *beio)
349 struct ctl_be_block_softc *softc = beio->softc;
352 for (i = 0; i < beio->num_segs; i++) {
353 ctl_free_seg(softc, &beio->sg_segs[i]);
355 /* For compare we had two equal S/G lists. */
356 if (beio->two_sglists) {
358 &beio->sg_segs[i + CTLBLK_HALF_SEGS]);
362 uma_zfree(softc->beio_zone, beio);
366 ctl_refcnt_beio(void *arg, int diff)
368 struct ctl_be_block_io *beio = arg;
370 if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0)
371 ctl_real_free_beio(beio);
375 ctl_free_beio(struct ctl_be_block_io *beio)
378 ctl_refcnt_beio(beio, -1);
382 ctl_complete_beio(struct ctl_be_block_io *beio)
384 union ctl_io *io = beio->io;
386 if (beio->beio_cont != NULL) {
387 beio->beio_cont(beio);
390 ctl_data_submit_done(io);
395 cmp(uint8_t *a, uint8_t *b, size_t size)
399 for (i = 0; i < size; i++) {
407 ctl_be_block_compare(union ctl_io *io)
409 struct ctl_be_block_io *beio;
414 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
416 for (i = 0; i < beio->num_segs; i++) {
417 res = cmp(beio->sg_segs[i].addr,
418 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
419 beio->sg_segs[i].len);
421 if (res < beio->sg_segs[i].len)
424 if (i < beio->num_segs) {
425 scsi_u64to8b(off, info);
426 ctl_set_sense(&io->scsiio, /*current_error*/ 1,
427 /*sense_key*/ SSD_KEY_MISCOMPARE,
428 /*asc*/ 0x1D, /*ascq*/ 0x00,
429 /*type*/ SSD_ELEM_INFO,
430 /*size*/ sizeof(info), /*data*/ &info,
431 /*type*/ SSD_ELEM_NONE);
433 ctl_set_success(&io->scsiio);
437 ctl_be_block_move_done(union ctl_io *io, bool samethr)
439 struct ctl_be_block_io *beio;
440 struct ctl_be_block_lun *be_lun;
441 struct ctl_lba_len_flags *lbalen;
443 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
445 DPRINTF("entered\n");
446 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
449 * We set status at this point for read and compare commands.
451 if ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
452 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) {
454 if (lbalen->flags & CTL_LLF_READ) {
455 ctl_set_success(&io->scsiio);
456 } else if (lbalen->flags & CTL_LLF_COMPARE) {
457 /* We have two data blocks ready for comparison. */
458 ctl_be_block_compare(io);
463 * If this is a read, or a write with errors, it is done.
465 if ((beio->bio_cmd == BIO_READ)
466 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
467 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
468 ctl_complete_beio(beio);
473 * At this point, we have a write and the DMA completed successfully.
474 * If we were called synchronously in the original thread then just
475 * dispatch, otherwise we now have to queue it to the task queue to
476 * execute the backend I/O. That is because we do blocking
477 * memory allocations, and in the file backing case, blocking I/O.
478 * This move done routine is generally called in the SIM's
479 * interrupt context, and therefore we cannot block.
481 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
483 be_lun->dispatch(be_lun, beio);
485 mtx_lock(&be_lun->queue_lock);
486 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
487 mtx_unlock(&be_lun->queue_lock);
488 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
494 ctl_be_block_biodone(struct bio *bio)
496 struct ctl_be_block_io *beio = bio->bio_caller1;
497 struct ctl_be_block_lun *be_lun = beio->lun;
498 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
504 DPRINTF("entered\n");
506 error = bio->bio_error;
507 mtx_lock(&be_lun->io_lock);
509 (beio->first_error == 0 ||
510 bio->bio_offset < beio->first_error_offset)) {
511 beio->first_error = error;
512 beio->first_error_offset = bio->bio_offset;
515 beio->num_bios_done++;
518 * XXX KDM will this cause WITNESS to complain? Holding a lock
519 * during the free might cause it to complain.
524 * If the send complete bit isn't set, or we aren't the last I/O to
525 * complete, then we're done.
527 if ((beio->send_complete == 0)
528 || (beio->num_bios_done < beio->num_bios_sent)) {
529 mtx_unlock(&be_lun->io_lock);
534 * At this point, we've verified that we are the last I/O to
535 * complete, so it's safe to drop the lock.
537 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
538 beio->ds_tag_type, beio->ds_trans_type,
539 /*now*/ NULL, /*then*/&beio->ds_t0);
540 mtx_unlock(&be_lun->io_lock);
543 * If there are any errors from the backing device, we fail the
544 * entire I/O with a medium error.
546 error = beio->first_error;
548 if (error == EOPNOTSUPP) {
549 ctl_set_invalid_opcode(&io->scsiio);
550 } else if (error == ENOSPC || error == EDQUOT) {
551 ctl_set_space_alloc_fail(&io->scsiio);
552 } else if (error == EROFS || error == EACCES) {
553 ctl_set_hw_write_protected(&io->scsiio);
554 } else if (beio->bio_cmd == BIO_FLUSH) {
555 /* XXX KDM is there is a better error here? */
556 ctl_set_internal_failure(&io->scsiio,
558 /*retry_count*/ 0xbad2);
560 ctl_set_medium_error(&io->scsiio,
561 beio->bio_cmd == BIO_READ);
563 ctl_complete_beio(beio);
568 * If this is a write, a flush, a delete or verify, we're all done.
569 * If this is a read, we can now send the data to the user.
571 if ((beio->bio_cmd == BIO_WRITE)
572 || (beio->bio_cmd == BIO_FLUSH)
573 || (beio->bio_cmd == BIO_DELETE)
574 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
575 ctl_set_success(&io->scsiio);
576 ctl_complete_beio(beio);
578 if ((ARGS(io)->flags & CTL_LLF_READ) &&
579 beio->beio_cont == NULL) {
580 ctl_set_success(&io->scsiio);
581 if (cbe_lun->serseq >= CTL_LUN_SERSEQ_SOFT)
589 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
590 struct ctl_be_block_io *beio)
592 union ctl_io *io = beio->io;
593 struct mount *mountpoint;
596 DPRINTF("entered\n");
598 binuptime(&beio->ds_t0);
599 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
601 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
603 vn_lock(be_lun->vn, vn_lktype_write(mountpoint, be_lun->vn) |
605 error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
607 VOP_UNLOCK(be_lun->vn);
609 vn_finished_write(mountpoint);
611 mtx_lock(&be_lun->io_lock);
612 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
613 beio->ds_tag_type, beio->ds_trans_type,
614 /*now*/ NULL, /*then*/&beio->ds_t0);
615 mtx_unlock(&be_lun->io_lock);
618 ctl_set_success(&io->scsiio);
620 /* XXX KDM is there is a better error here? */
621 ctl_set_internal_failure(&io->scsiio,
623 /*retry_count*/ 0xbad1);
626 ctl_complete_beio(beio);
629 SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
630 SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
631 SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
632 SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
635 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
636 struct ctl_be_block_io *beio)
638 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
639 struct ctl_be_block_filedata *file_data;
642 struct iovec *xiovec;
646 DPRINTF("entered\n");
648 file_data = &be_lun->backend.file;
651 if (ARGS(io)->flags & CTL_LLF_DPO)
653 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
656 bzero(&xuio, sizeof(xuio));
657 if (beio->bio_cmd == BIO_READ) {
658 SDT_PROBE0(cbb, , read, file_start);
659 xuio.uio_rw = UIO_READ;
661 SDT_PROBE0(cbb, , write, file_start);
662 xuio.uio_rw = UIO_WRITE;
664 xuio.uio_offset = beio->io_offset;
665 xuio.uio_resid = beio->io_len;
666 xuio.uio_segflg = UIO_SYSSPACE;
667 xuio.uio_iov = beio->xiovecs;
668 xuio.uio_iovcnt = beio->num_segs;
669 xuio.uio_td = curthread;
671 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
672 xiovec->iov_base = beio->sg_segs[i].addr;
673 xiovec->iov_len = beio->sg_segs[i].len;
676 binuptime(&beio->ds_t0);
677 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
679 if (beio->bio_cmd == BIO_READ) {
680 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
682 if (beio->beio_cont == NULL &&
683 cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
686 * UFS pays attention to IO_DIRECT for reads. If the
687 * DIRECTIO option is configured into the kernel, it calls
688 * ffs_rawread(). But that only works for single-segment
689 * uios with user space addresses. In our case, with a
690 * kernel uio, it still reads into the buffer cache, but it
691 * will just try to release the buffer from the cache later
694 * ZFS does not pay attention to IO_DIRECT for reads.
696 * UFS does not pay attention to IO_SYNC for reads.
698 * ZFS pays attention to IO_SYNC (which translates into the
699 * Solaris define FRSYNC for zfs_read()) for reads. It
700 * attempts to sync the file before reading.
702 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
704 VOP_UNLOCK(be_lun->vn);
705 SDT_PROBE0(cbb, , read, file_done);
706 if (error == 0 && xuio.uio_resid > 0) {
708 * If we red less then requested (EOF), then
709 * we should clean the rest of the buffer.
711 s = beio->io_len - xuio.uio_resid;
712 for (i = 0; i < beio->num_segs; i++) {
713 if (s >= beio->sg_segs[i].len) {
714 s -= beio->sg_segs[i].len;
717 bzero((uint8_t *)beio->sg_segs[i].addr + s,
718 beio->sg_segs[i].len - s);
723 struct mount *mountpoint;
725 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
726 vn_lock(be_lun->vn, vn_lktype_write(mountpoint,
727 be_lun->vn) | LK_RETRY);
730 * UFS pays attention to IO_DIRECT for writes. The write
731 * is done asynchronously. (Normally the write would just
732 * get put into cache.
734 * UFS pays attention to IO_SYNC for writes. It will
735 * attempt to write the buffer out synchronously if that
738 * ZFS does not pay attention to IO_DIRECT for writes.
740 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
741 * for writes. It will flush the transaction from the
742 * cache before returning.
744 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
745 VOP_UNLOCK(be_lun->vn);
747 vn_finished_write(mountpoint);
748 SDT_PROBE0(cbb, , write, file_done);
751 mtx_lock(&be_lun->io_lock);
752 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
753 beio->ds_tag_type, beio->ds_trans_type,
754 /*now*/ NULL, /*then*/&beio->ds_t0);
755 mtx_unlock(&be_lun->io_lock);
758 * If we got an error, set the sense data to "MEDIUM ERROR" and
759 * return the I/O to the user.
762 if (error == ENOSPC || error == EDQUOT) {
763 ctl_set_space_alloc_fail(&io->scsiio);
764 } else if (error == EROFS || error == EACCES) {
765 ctl_set_hw_write_protected(&io->scsiio);
767 ctl_set_medium_error(&io->scsiio,
768 beio->bio_cmd == BIO_READ);
770 ctl_complete_beio(beio);
775 * If this is a write or a verify, we're all done.
776 * If this is a read, we can now send the data to the user.
778 if ((beio->bio_cmd == BIO_WRITE) ||
779 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
780 ctl_set_success(&io->scsiio);
781 ctl_complete_beio(beio);
783 if ((ARGS(io)->flags & CTL_LLF_READ) &&
784 beio->beio_cont == NULL) {
785 ctl_set_success(&io->scsiio);
786 if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
794 ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
795 struct ctl_be_block_io *beio)
797 union ctl_io *io = beio->io;
798 struct ctl_lba_len_flags *lbalen = ARGS(io);
799 struct scsi_get_lba_status_data *data;
803 DPRINTF("entered\n");
805 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
806 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
807 error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
808 0, curthread->td_ucred, curthread);
809 if (error == 0 && off > roff)
810 status = 0; /* mapped up to off */
812 error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
813 0, curthread->td_ucred, curthread);
814 if (error == 0 && off > roff)
815 status = 1; /* deallocated up to off */
817 status = 0; /* unknown up to the end */
818 off = be_lun->size_bytes;
821 VOP_UNLOCK(be_lun->vn);
823 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
824 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
825 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
826 lbalen->lba), data->descr[0].length);
827 data->descr[0].status = status;
829 ctl_complete_beio(beio);
833 ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
836 struct statfs statfs;
841 if (be_lun->vn == NULL)
843 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
844 if (strcmp(attrname, "blocksused") == 0) {
845 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
847 val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
849 if (strcmp(attrname, "blocksavail") == 0 &&
850 !VN_IS_DOOMED(be_lun->vn)) {
851 error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
853 val = statfs.f_bavail * statfs.f_bsize /
854 be_lun->cbe_lun.blocksize;
856 VOP_UNLOCK(be_lun->vn);
861 ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
862 struct ctl_be_block_io *beio)
864 struct ctl_be_block_filedata *file_data;
866 struct ctl_ptr_len_flags *ptrlen;
867 struct scsi_unmap_desc *buf, *end;
873 file_data = &be_lun->backend.file;
877 binuptime(&beio->ds_t0);
878 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
880 (void)vn_start_write(be_lun->vn, &mp, V_WAIT);
881 vn_lock(be_lun->vn, vn_lktype_write(mp, be_lun->vn) | LK_RETRY);
882 if (beio->io_offset == -1) {
884 ptrlen = (struct ctl_ptr_len_flags *)
885 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
886 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
887 end = buf + ptrlen->len / sizeof(*buf);
888 for (; buf < end; buf++) {
889 off = (off_t)scsi_8btou64(buf->lba) *
890 be_lun->cbe_lun.blocksize;
891 len = (off_t)scsi_4btoul(buf->length) *
892 be_lun->cbe_lun.blocksize;
894 error = vn_deallocate(be_lun->vn, &off, &len,
895 0, IO_NOMACCHECK | IO_NODELOCKED, file_data->cred,
902 off = beio->io_offset;
904 error = vn_deallocate(be_lun->vn, &off, &len, 0,
905 IO_NOMACCHECK | IO_NODELOCKED, file_data->cred, NOCRED);
907 VOP_UNLOCK(be_lun->vn);
908 vn_finished_write(mp);
910 mtx_lock(&be_lun->io_lock);
911 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
912 beio->ds_tag_type, beio->ds_trans_type,
913 /*now*/ NULL, /*then*/&beio->ds_t0);
914 mtx_unlock(&be_lun->io_lock);
917 * If we got an error, set the sense data to "MEDIUM ERROR" and
918 * return the I/O to the user.
922 ctl_set_success(&io->scsiio);
926 ctl_set_space_alloc_fail(&io->scsiio);
930 ctl_set_hw_write_protected(&io->scsiio);
933 ctl_set_medium_error(&io->scsiio, false);
935 ctl_complete_beio(beio);
939 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
940 struct ctl_be_block_io *beio)
942 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
947 struct iovec *xiovec;
948 int error, flags, i, ref;
950 DPRINTF("entered\n");
954 if (ARGS(io)->flags & CTL_LLF_DPO)
956 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
959 bzero(&xuio, sizeof(xuio));
960 if (beio->bio_cmd == BIO_READ) {
961 SDT_PROBE0(cbb, , read, file_start);
962 xuio.uio_rw = UIO_READ;
964 SDT_PROBE0(cbb, , write, file_start);
965 xuio.uio_rw = UIO_WRITE;
967 xuio.uio_offset = beio->io_offset;
968 xuio.uio_resid = beio->io_len;
969 xuio.uio_segflg = UIO_SYSSPACE;
970 xuio.uio_iov = beio->xiovecs;
971 xuio.uio_iovcnt = beio->num_segs;
972 xuio.uio_td = curthread;
974 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
975 xiovec->iov_base = beio->sg_segs[i].addr;
976 xiovec->iov_len = beio->sg_segs[i].len;
979 binuptime(&beio->ds_t0);
980 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
982 csw = devvn_refthread(be_lun->vn, &dev, &ref);
984 if (beio->bio_cmd == BIO_READ) {
985 if (beio->beio_cont == NULL &&
986 cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
988 error = csw->d_read(dev, &xuio, flags);
990 error = csw->d_write(dev, &xuio, flags);
991 dev_relthread(dev, ref);
995 if (beio->bio_cmd == BIO_READ)
996 SDT_PROBE0(cbb, , read, file_done);
998 SDT_PROBE0(cbb, , write, file_done);
1000 mtx_lock(&be_lun->io_lock);
1001 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
1002 beio->ds_tag_type, beio->ds_trans_type,
1003 /*now*/ NULL, /*then*/&beio->ds_t0);
1004 mtx_unlock(&be_lun->io_lock);
1007 * If we got an error, set the sense data to "MEDIUM ERROR" and
1008 * return the I/O to the user.
1011 if (error == ENOSPC || error == EDQUOT) {
1012 ctl_set_space_alloc_fail(&io->scsiio);
1013 } else if (error == EROFS || error == EACCES) {
1014 ctl_set_hw_write_protected(&io->scsiio);
1016 ctl_set_medium_error(&io->scsiio,
1017 beio->bio_cmd == BIO_READ);
1019 ctl_complete_beio(beio);
1024 * If this is a write or a verify, we're all done.
1025 * If this is a read, we can now send the data to the user.
1027 if ((beio->bio_cmd == BIO_WRITE) ||
1028 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
1029 ctl_set_success(&io->scsiio);
1030 ctl_complete_beio(beio);
1032 if ((ARGS(io)->flags & CTL_LLF_READ) &&
1033 beio->beio_cont == NULL) {
1034 ctl_set_success(&io->scsiio);
1035 if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
1036 ctl_serseq_done(io);
1043 ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
1044 struct ctl_be_block_io *beio)
1046 union ctl_io *io = beio->io;
1049 struct ctl_lba_len_flags *lbalen = ARGS(io);
1050 struct scsi_get_lba_status_data *data;
1052 int error, ref, status;
1054 DPRINTF("entered\n");
1056 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1058 status = 0; /* unknown up to the end */
1059 off = be_lun->size_bytes;
1062 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
1063 error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
1065 if (error == 0 && off > roff)
1066 status = 0; /* mapped up to off */
1068 error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
1070 if (error == 0 && off > roff)
1071 status = 1; /* deallocated up to off */
1073 status = 0; /* unknown up to the end */
1074 off = be_lun->size_bytes;
1077 dev_relthread(dev, ref);
1080 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
1081 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
1082 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
1083 lbalen->lba), data->descr[0].length);
1084 data->descr[0].status = status;
1086 ctl_complete_beio(beio);
1090 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1091 struct ctl_be_block_io *beio)
1098 DPRINTF("entered\n");
1100 /* This can't fail, it's a blocking allocation. */
1101 bio = g_alloc_bio();
1103 bio->bio_cmd = BIO_FLUSH;
1104 bio->bio_offset = 0;
1106 bio->bio_done = ctl_be_block_biodone;
1107 bio->bio_caller1 = beio;
1108 bio->bio_pblkno = 0;
1111 * We don't need to acquire the LUN lock here, because we are only
1112 * sending one bio, and so there is no other context to synchronize
1115 beio->num_bios_sent = 1;
1116 beio->send_complete = 1;
1118 binuptime(&beio->ds_t0);
1119 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1121 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1124 csw->d_strategy(bio);
1125 dev_relthread(dev, ref);
1127 bio->bio_error = ENXIO;
1128 ctl_be_block_biodone(bio);
1133 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1134 struct ctl_be_block_io *beio,
1135 uint64_t off, uint64_t len, int last)
1143 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1144 maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1146 bio = g_alloc_bio();
1147 bio->bio_cmd = BIO_DELETE;
1149 bio->bio_offset = off;
1150 bio->bio_length = MIN(len, maxlen);
1152 bio->bio_done = ctl_be_block_biodone;
1153 bio->bio_caller1 = beio;
1154 bio->bio_pblkno = off / be_lun->cbe_lun.blocksize;
1156 off += bio->bio_length;
1157 len -= bio->bio_length;
1159 mtx_lock(&be_lun->io_lock);
1160 beio->num_bios_sent++;
1161 if (last && len == 0)
1162 beio->send_complete = 1;
1163 mtx_unlock(&be_lun->io_lock);
1166 csw->d_strategy(bio);
1168 bio->bio_error = ENXIO;
1169 ctl_be_block_biodone(bio);
1173 dev_relthread(dev, ref);
1177 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1178 struct ctl_be_block_io *beio)
1181 struct ctl_ptr_len_flags *ptrlen;
1182 struct scsi_unmap_desc *buf, *end;
1187 DPRINTF("entered\n");
1189 binuptime(&beio->ds_t0);
1190 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1192 if (beio->io_offset == -1) {
1194 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1195 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1196 end = buf + ptrlen->len / sizeof(*buf);
1197 for (; buf < end; buf++) {
1198 len = (uint64_t)scsi_4btoul(buf->length) *
1199 be_lun->cbe_lun.blocksize;
1200 beio->io_len += len;
1201 ctl_be_block_unmap_dev_range(be_lun, beio,
1202 scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
1203 len, (end - buf < 2) ? TRUE : FALSE);
1206 ctl_be_block_unmap_dev_range(be_lun, beio,
1207 beio->io_offset, beio->io_len, TRUE);
1211 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1212 struct ctl_be_block_io *beio)
1214 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1219 int i, max_iosize, ref;
1221 DPRINTF("entered\n");
1222 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1225 * We have to limit our I/O size to the maximum supported by the
1229 max_iosize = dev->si_iosize_max;
1230 if (max_iosize <= 0)
1231 max_iosize = DFLTPHYS;
1233 max_iosize = maxphys;
1235 cur_offset = beio->io_offset;
1236 for (i = 0; i < beio->num_segs; i++) {
1240 cur_size = beio->sg_segs[i].len;
1241 cur_ptr = beio->sg_segs[i].addr;
1243 while (cur_size > 0) {
1244 /* This can't fail, it's a blocking allocation. */
1245 bio = g_alloc_bio();
1247 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1249 bio->bio_cmd = beio->bio_cmd;
1251 bio->bio_caller1 = beio;
1252 bio->bio_length = min(cur_size, max_iosize);
1253 bio->bio_offset = cur_offset;
1254 bio->bio_data = cur_ptr;
1255 bio->bio_done = ctl_be_block_biodone;
1256 bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1258 cur_offset += bio->bio_length;
1259 cur_ptr += bio->bio_length;
1260 cur_size -= bio->bio_length;
1262 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1263 beio->num_bios_sent++;
1266 beio->send_complete = 1;
1267 binuptime(&beio->ds_t0);
1268 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1271 * Fire off all allocated requests!
1273 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1274 TAILQ_REMOVE(&queue, bio, bio_queue);
1276 csw->d_strategy(bio);
1278 bio->bio_error = ENXIO;
1279 ctl_be_block_biodone(bio);
1283 dev_relthread(dev, ref);
1287 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1289 struct diocgattr_arg arg;
1294 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1296 return (UINT64_MAX);
1297 strlcpy(arg.name, attrname, sizeof(arg.name));
1298 arg.len = sizeof(arg.value.off);
1300 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
1304 dev_relthread(dev, ref);
1306 return (UINT64_MAX);
1307 return (arg.value.off);
1311 ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1314 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1315 struct ctl_be_block_io *beio;
1316 struct ctl_lba_len_flags *lbalen;
1318 DPRINTF("entered\n");
1319 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1320 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1322 beio->io_len = lbalen->len * cbe_lun->blocksize;
1323 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1324 beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
1325 beio->bio_cmd = BIO_FLUSH;
1326 beio->ds_trans_type = DEVSTAT_NO_DATA;
1328 be_lun->lun_flush(be_lun, beio);
1332 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1337 ctl_free_beio(beio);
1338 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1339 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1340 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1341 ctl_config_write_done(io);
1345 ctl_be_block_config_write(io);
1349 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1352 struct ctl_be_block_softc *softc = be_lun->softc;
1353 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1354 struct ctl_be_block_io *beio;
1355 struct ctl_lba_len_flags *lbalen;
1356 uint64_t len_left, lba;
1357 uint32_t pb, pbo, adj;
1361 DPRINTF("entered\n");
1363 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1366 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1367 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1368 ctl_free_beio(beio);
1369 ctl_set_invalid_field(&io->scsiio,
1375 ctl_config_write_done(io);
1379 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1380 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1381 beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1382 beio->bio_cmd = BIO_DELETE;
1383 beio->ds_trans_type = DEVSTAT_FREE;
1385 be_lun->unmap(be_lun, beio);
1389 beio->bio_cmd = BIO_WRITE;
1390 beio->ds_trans_type = DEVSTAT_WRITE;
1392 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1393 (uintmax_t)lbalen->lba, lbalen->len);
1395 pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
1396 if (be_lun->cbe_lun.pblockoff > 0)
1397 pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1400 len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1401 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1403 * Setup the S/G entry for this chunk.
1405 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1406 if (pb > cbe_lun->blocksize) {
1407 adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
1412 seglen -= seglen % cbe_lun->blocksize;
1414 seglen -= seglen % cbe_lun->blocksize;
1415 ctl_alloc_seg(softc, &beio->sg_segs[i], seglen);
1417 DPRINTF("segment %d addr %p len %zd\n", i,
1418 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1423 buf = beio->sg_segs[i].addr;
1425 for (; buf < end; buf += cbe_lun->blocksize) {
1426 if (lbalen->flags & SWS_NDOB) {
1427 memset(buf, 0, cbe_lun->blocksize);
1429 memcpy(buf, io->scsiio.kern_data_ptr,
1430 cbe_lun->blocksize);
1432 if (lbalen->flags & SWS_LBDATA)
1433 scsi_ulto4b(lbalen->lba + lba, buf);
1438 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1439 beio->io_len = lba * cbe_lun->blocksize;
1441 /* We can not do all in one run. Correct and schedule rerun. */
1445 beio->beio_cont = ctl_be_block_cw_done_ws;
1448 be_lun->dispatch(be_lun, beio);
1452 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1455 struct ctl_be_block_io *beio;
1456 struct ctl_ptr_len_flags *ptrlen;
1458 DPRINTF("entered\n");
1460 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1461 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1463 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1464 ctl_free_beio(beio);
1465 ctl_set_invalid_field(&io->scsiio,
1471 ctl_config_write_done(io);
1476 beio->io_offset = -1;
1477 beio->bio_cmd = BIO_DELETE;
1478 beio->ds_trans_type = DEVSTAT_FREE;
1480 be_lun->unmap(be_lun, beio);
1484 ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1489 ctl_free_beio(beio);
1490 ctl_config_read_done(io);
1494 ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1497 struct ctl_be_block_io *beio;
1498 struct ctl_be_block_softc *softc;
1500 DPRINTF("entered\n");
1502 softc = be_lun->softc;
1503 beio = ctl_alloc_beio(softc);
1506 beio->beio_cont = ctl_be_block_cr_done;
1507 PRIV(io)->ptr = (void *)beio;
1509 switch (io->scsiio.cdb[0]) {
1510 case SERVICE_ACTION_IN: /* GET LBA STATUS */
1512 beio->ds_trans_type = DEVSTAT_NO_DATA;
1513 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1515 if (be_lun->get_lba_status)
1516 be_lun->get_lba_status(be_lun, beio);
1518 ctl_be_block_cr_done(beio);
1521 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1527 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1532 ctl_free_beio(beio);
1533 ctl_config_write_done(io);
1537 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1540 struct ctl_be_block_io *beio;
1541 struct ctl_be_block_softc *softc;
1543 DPRINTF("entered\n");
1545 softc = be_lun->softc;
1546 beio = ctl_alloc_beio(softc);
1549 beio->beio_cont = ctl_be_block_cw_done;
1550 switch (io->scsiio.tag_type) {
1551 case CTL_TAG_ORDERED:
1552 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1554 case CTL_TAG_HEAD_OF_QUEUE:
1555 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1557 case CTL_TAG_UNTAGGED:
1558 case CTL_TAG_SIMPLE:
1561 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1564 PRIV(io)->ptr = (void *)beio;
1566 switch (io->scsiio.cdb[0]) {
1567 case SYNCHRONIZE_CACHE:
1568 case SYNCHRONIZE_CACHE_16:
1569 ctl_be_block_cw_dispatch_sync(be_lun, io);
1573 ctl_be_block_cw_dispatch_ws(be_lun, io);
1576 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1579 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1584 SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
1585 SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
1586 SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
1587 SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1590 ctl_be_block_next(struct ctl_be_block_io *beio)
1592 struct ctl_be_block_lun *be_lun;
1597 ctl_free_beio(beio);
1598 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1599 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1600 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1601 ctl_data_submit_done(io);
1605 io->io_hdr.status &= ~CTL_STATUS_MASK;
1606 io->io_hdr.status |= CTL_STATUS_NONE;
1608 mtx_lock(&be_lun->queue_lock);
1609 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1610 mtx_unlock(&be_lun->queue_lock);
1611 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1615 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1618 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1619 struct ctl_be_block_io *beio;
1620 struct ctl_be_block_softc *softc;
1621 struct ctl_lba_len_flags *lbalen;
1622 struct ctl_ptr_len_flags *bptrlen;
1623 uint64_t len_left, lbas;
1626 softc = be_lun->softc;
1628 DPRINTF("entered\n");
1631 if (lbalen->flags & CTL_LLF_WRITE) {
1632 SDT_PROBE0(cbb, , write, start);
1634 SDT_PROBE0(cbb, , read, start);
1637 beio = ctl_alloc_beio(softc);
1641 bptrlen->ptr = (void *)beio;
1643 switch (io->scsiio.tag_type) {
1644 case CTL_TAG_ORDERED:
1645 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1647 case CTL_TAG_HEAD_OF_QUEUE:
1648 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1650 case CTL_TAG_UNTAGGED:
1651 case CTL_TAG_SIMPLE:
1654 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1658 if (lbalen->flags & CTL_LLF_WRITE) {
1659 beio->bio_cmd = BIO_WRITE;
1660 beio->ds_trans_type = DEVSTAT_WRITE;
1662 beio->bio_cmd = BIO_READ;
1663 beio->ds_trans_type = DEVSTAT_READ;
1666 DPRINTF("%s at LBA %jx len %u @%ju\n",
1667 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1668 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1669 lbas = CTLBLK_MAX_IO_SIZE;
1670 if (lbalen->flags & CTL_LLF_COMPARE) {
1671 beio->two_sglists = 1;
1674 lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
1675 beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
1676 beio->io_len = lbas * cbe_lun->blocksize;
1677 bptrlen->len += lbas;
1679 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1680 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1681 i, CTLBLK_MAX_SEGS));
1684 * Setup the S/G entry for this chunk.
1686 ctl_alloc_seg(softc, &beio->sg_segs[i],
1687 MIN(CTLBLK_MAX_SEG, len_left));
1689 DPRINTF("segment %d addr %p len %zd\n", i,
1690 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1692 /* Set up second segment for compare operation. */
1693 if (beio->two_sglists) {
1694 ctl_alloc_seg(softc,
1695 &beio->sg_segs[i + CTLBLK_HALF_SEGS],
1696 beio->sg_segs[i].len);
1700 len_left -= beio->sg_segs[i].len;
1702 if (bptrlen->len < lbalen->len)
1703 beio->beio_cont = ctl_be_block_next;
1704 io->scsiio.be_move_done = ctl_be_block_move_done;
1705 /* For compare we have separate S/G lists for read and datamove. */
1706 if (beio->two_sglists)
1707 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1709 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1710 io->scsiio.kern_data_len = beio->io_len;
1711 io->scsiio.kern_sg_entries = beio->num_segs;
1712 io->scsiio.kern_data_ref = ctl_refcnt_beio;
1713 io->scsiio.kern_data_arg = beio;
1714 io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1717 * For the read case, we need to read the data into our buffers and
1718 * then we can send it back to the user. For the write case, we
1719 * need to get the data from the user first.
1721 if (beio->bio_cmd == BIO_READ) {
1722 SDT_PROBE0(cbb, , read, alloc_done);
1723 be_lun->dispatch(be_lun, beio);
1725 SDT_PROBE0(cbb, , write, alloc_done);
1731 ctl_be_block_worker(void *context, int pending)
1733 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1734 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1736 struct ctl_be_block_io *beio;
1738 DPRINTF("entered\n");
1740 * Fetch and process I/Os from all queues. If we detect LUN
1741 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1742 * so make response maximally opaque to not confuse initiator.
1745 mtx_lock(&be_lun->queue_lock);
1746 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1748 DPRINTF("datamove queue\n");
1749 STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links);
1750 mtx_unlock(&be_lun->queue_lock);
1751 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1752 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1753 ctl_set_busy(&io->scsiio);
1754 ctl_complete_beio(beio);
1757 be_lun->dispatch(be_lun, beio);
1760 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1762 DPRINTF("config write queue\n");
1763 STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links);
1764 mtx_unlock(&be_lun->queue_lock);
1765 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1766 ctl_set_busy(&io->scsiio);
1767 ctl_config_write_done(io);
1770 ctl_be_block_cw_dispatch(be_lun, io);
1773 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1775 DPRINTF("config read queue\n");
1776 STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links);
1777 mtx_unlock(&be_lun->queue_lock);
1778 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1779 ctl_set_busy(&io->scsiio);
1780 ctl_config_read_done(io);
1783 ctl_be_block_cr_dispatch(be_lun, io);
1786 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1788 DPRINTF("input queue\n");
1789 STAILQ_REMOVE_HEAD(&be_lun->input_queue, links);
1790 mtx_unlock(&be_lun->queue_lock);
1791 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1792 ctl_set_busy(&io->scsiio);
1793 ctl_data_submit_done(io);
1796 ctl_be_block_dispatch(be_lun, io);
1801 * If we get here, there is no work left in the queues, so
1802 * just break out and let the task queue go to sleep.
1804 mtx_unlock(&be_lun->queue_lock);
1810 * Entry point from CTL to the backend for I/O. We queue everything to a
1811 * work thread, so this just puts the I/O on a queue and wakes up the
1815 ctl_be_block_submit(union ctl_io *io)
1817 struct ctl_be_block_lun *be_lun;
1819 DPRINTF("entered\n");
1821 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1823 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1824 ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
1828 mtx_lock(&be_lun->queue_lock);
1829 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1830 mtx_unlock(&be_lun->queue_lock);
1831 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1833 return (CTL_RETVAL_COMPLETE);
1837 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1838 int flag, struct thread *td)
1840 struct ctl_be_block_softc *softc = &backend_block_softc;
1846 struct ctl_lun_req *lun_req;
1848 lun_req = (struct ctl_lun_req *)addr;
1850 switch (lun_req->reqtype) {
1851 case CTL_LUNREQ_CREATE:
1852 error = ctl_be_block_create(softc, lun_req);
1855 error = ctl_be_block_rm(softc, lun_req);
1857 case CTL_LUNREQ_MODIFY:
1858 error = ctl_be_block_modify(softc, lun_req);
1861 lun_req->status = CTL_LUN_ERROR;
1862 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1863 "invalid LUN request type %d",
1878 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1880 struct ctl_be_lun *cbe_lun;
1881 struct ctl_be_block_filedata *file_data;
1882 struct ctl_lun_create_params *params;
1885 off_t ps, pss, po, pos, us, uss, uo, uos;
1889 cbe_lun = &be_lun->cbe_lun;
1890 file_data = &be_lun->backend.file;
1891 params = &be_lun->params;
1893 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1894 be_lun->dispatch = ctl_be_block_dispatch_file;
1895 be_lun->lun_flush = ctl_be_block_flush_file;
1896 be_lun->get_lba_status = ctl_be_block_gls_file;
1897 be_lun->getattr = ctl_be_block_getattr_file;
1898 be_lun->unmap = ctl_be_block_unmap_file;
1899 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1901 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1903 snprintf(req->error_str, sizeof(req->error_str),
1904 "error calling VOP_GETATTR() for file %s",
1909 error = VOP_PATHCONF(be_lun->vn, _PC_DEALLOC_PRESENT, &pconf);
1911 snprintf(req->error_str, sizeof(req->error_str),
1912 "error calling VOP_PATHCONF() for file %s",
1917 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
1919 file_data->cred = crhold(curthread->td_ucred);
1920 if (params->lun_size_bytes != 0)
1921 be_lun->size_bytes = params->lun_size_bytes;
1923 be_lun->size_bytes = vattr.va_size;
1926 * For files we can use any logical block size. Prefer 512 bytes
1927 * for compatibility reasons. If file's vattr.va_blocksize
1928 * (preferred I/O block size) is bigger and multiple to chosen
1929 * logical block size -- report it as physical block size.
1931 if (params->blocksize_bytes != 0)
1932 cbe_lun->blocksize = params->blocksize_bytes;
1933 else if (cbe_lun->lun_type == T_CDROM)
1934 cbe_lun->blocksize = 2048;
1936 cbe_lun->blocksize = 512;
1937 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1938 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1939 0 : (be_lun->size_blocks - 1);
1941 us = ps = vattr.va_blocksize;
1944 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
1946 ctl_expand_number(value, &ps);
1947 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
1949 ctl_expand_number(value, &po);
1950 pss = ps / cbe_lun->blocksize;
1951 pos = po / cbe_lun->blocksize;
1952 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
1953 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
1954 cbe_lun->pblockexp = fls(pss) - 1;
1955 cbe_lun->pblockoff = (pss - pos) % pss;
1958 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
1960 ctl_expand_number(value, &us);
1961 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
1963 ctl_expand_number(value, &uo);
1964 uss = us / cbe_lun->blocksize;
1965 uos = uo / cbe_lun->blocksize;
1966 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
1967 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
1968 cbe_lun->ublockexp = fls(uss) - 1;
1969 cbe_lun->ublockoff = (uss - uos) % uss;
1973 * Sanity check. The media size has to be at least one
1976 if (be_lun->size_bytes < cbe_lun->blocksize) {
1978 snprintf(req->error_str, sizeof(req->error_str),
1979 "file %s size %ju < block size %u", be_lun->dev_path,
1980 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1983 cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1988 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1990 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1991 struct ctl_lun_create_params *params;
1995 int error, atomic, maxio, ref, unmap, tmp;
1996 off_t ps, pss, po, pos, us, uss, uo, uos, otmp;
1998 params = &be_lun->params;
2000 be_lun->dev_type = CTL_BE_BLOCK_DEV;
2001 csw = devvn_refthread(be_lun->vn, &dev, &ref);
2004 if (strcmp(csw->d_name, "zvol") == 0) {
2005 be_lun->dispatch = ctl_be_block_dispatch_zvol;
2006 be_lun->get_lba_status = ctl_be_block_gls_zvol;
2007 atomic = maxio = CTLBLK_MAX_IO_SIZE;
2009 be_lun->dispatch = ctl_be_block_dispatch_dev;
2010 be_lun->get_lba_status = NULL;
2012 maxio = dev->si_iosize_max;
2015 if (maxio > CTLBLK_MAX_SEG)
2016 maxio = CTLBLK_MAX_SEG;
2018 be_lun->lun_flush = ctl_be_block_flush_dev;
2019 be_lun->getattr = ctl_be_block_getattr_dev;
2020 be_lun->unmap = ctl_be_block_unmap_dev;
2022 if (!csw->d_ioctl) {
2023 dev_relthread(dev, ref);
2024 snprintf(req->error_str, sizeof(req->error_str),
2025 "no d_ioctl for device %s!", be_lun->dev_path);
2029 error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
2032 dev_relthread(dev, ref);
2033 snprintf(req->error_str, sizeof(req->error_str),
2034 "error %d returned for DIOCGSECTORSIZE ioctl "
2035 "on %s!", error, be_lun->dev_path);
2040 * If the user has asked for a blocksize that is greater than the
2041 * backing device's blocksize, we can do it only if the blocksize
2042 * the user is asking for is an even multiple of the underlying
2043 * device's blocksize.
2045 if ((params->blocksize_bytes != 0) &&
2046 (params->blocksize_bytes >= tmp)) {
2047 if (params->blocksize_bytes % tmp == 0) {
2048 cbe_lun->blocksize = params->blocksize_bytes;
2050 dev_relthread(dev, ref);
2051 snprintf(req->error_str, sizeof(req->error_str),
2052 "requested blocksize %u is not an even "
2053 "multiple of backing device blocksize %u",
2054 params->blocksize_bytes, tmp);
2057 } else if (params->blocksize_bytes != 0) {
2058 dev_relthread(dev, ref);
2059 snprintf(req->error_str, sizeof(req->error_str),
2060 "requested blocksize %u < backing device "
2061 "blocksize %u", params->blocksize_bytes, tmp);
2063 } else if (cbe_lun->lun_type == T_CDROM)
2064 cbe_lun->blocksize = MAX(tmp, 2048);
2066 cbe_lun->blocksize = tmp;
2068 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
2071 dev_relthread(dev, ref);
2072 snprintf(req->error_str, sizeof(req->error_str),
2073 "error %d returned for DIOCGMEDIASIZE "
2074 " ioctl on %s!", error,
2079 if (params->lun_size_bytes != 0) {
2080 if (params->lun_size_bytes > otmp) {
2081 dev_relthread(dev, ref);
2082 snprintf(req->error_str, sizeof(req->error_str),
2083 "requested LUN size %ju > backing device "
2085 (uintmax_t)params->lun_size_bytes,
2090 be_lun->size_bytes = params->lun_size_bytes;
2092 be_lun->size_bytes = otmp;
2093 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2094 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2095 0 : (be_lun->size_blocks - 1);
2097 error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
2102 error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
2110 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
2112 ctl_expand_number(value, &ps);
2113 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
2115 ctl_expand_number(value, &po);
2116 pss = ps / cbe_lun->blocksize;
2117 pos = po / cbe_lun->blocksize;
2118 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
2119 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
2120 cbe_lun->pblockexp = fls(pss) - 1;
2121 cbe_lun->pblockoff = (pss - pos) % pss;
2124 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
2126 ctl_expand_number(value, &us);
2127 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
2129 ctl_expand_number(value, &uo);
2130 uss = us / cbe_lun->blocksize;
2131 uos = uo / cbe_lun->blocksize;
2132 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
2133 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
2134 cbe_lun->ublockexp = fls(uss) - 1;
2135 cbe_lun->ublockoff = (uss - uos) % uss;
2138 cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
2139 cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2141 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2144 struct diocgattr_arg arg;
2146 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2147 arg.len = sizeof(arg.value.i);
2148 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
2150 unmap = (error == 0) ? arg.value.i : 0;
2152 value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2154 unmap = (strcmp(value, "on") == 0);
2156 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
2158 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2160 dev_relthread(dev, ref);
2165 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2167 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2172 if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
2174 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2177 switch (be_lun->dev_type) {
2178 case CTL_BE_BLOCK_DEV:
2180 case CTL_BE_BLOCK_FILE:
2181 if (be_lun->backend.file.cred != NULL) {
2182 crfree(be_lun->backend.file.cred);
2183 be_lun->backend.file.cred = NULL;
2186 case CTL_BE_BLOCK_NONE:
2189 panic("Unexpected backend type %d", be_lun->dev_type);
2192 be_lun->dev_type = CTL_BE_BLOCK_NONE;
2198 ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2200 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2201 struct nameidata nd;
2206 if (rootvnode == NULL) {
2207 snprintf(req->error_str, sizeof(req->error_str),
2208 "Root filesystem is not mounted");
2213 value = dnvlist_get_string(cbe_lun->options, "file", NULL);
2214 if (value == NULL) {
2215 snprintf(req->error_str, sizeof(req->error_str),
2216 "no file argument specified");
2219 free(be_lun->dev_path, M_CTLBLK);
2220 be_lun->dev_path = strdup(value, M_CTLBLK);
2223 value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
2224 if (value != NULL) {
2225 if (strcmp(value, "on") != 0)
2227 } else if (cbe_lun->lun_type == T_DIRECT)
2231 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path);
2232 error = vn_open(&nd, &flags, 0, NULL);
2233 if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
2239 * This is the only reasonable guess we can make as far as
2240 * path if the user doesn't give us a fully qualified path.
2241 * If they want to specify a file, they need to specify the
2244 if (be_lun->dev_path[0] != '/') {
2247 asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2249 free(be_lun->dev_path, M_CTLBLK);
2250 be_lun->dev_path = dev_name;
2253 snprintf(req->error_str, sizeof(req->error_str),
2254 "error opening %s: %d", be_lun->dev_path, error);
2258 cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
2260 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2263 be_lun->vn = nd.ni_vp;
2265 /* We only support disks and files. */
2266 if (vn_isdisk_error(be_lun->vn, &error)) {
2267 error = ctl_be_block_open_dev(be_lun, req);
2268 } else if (be_lun->vn->v_type == VREG) {
2269 error = ctl_be_block_open_file(be_lun, req);
2272 snprintf(req->error_str, sizeof(req->error_str),
2273 "%s is not a disk or plain file", be_lun->dev_path);
2275 VOP_UNLOCK(be_lun->vn);
2278 ctl_be_block_close(be_lun);
2279 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2280 if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2281 cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
2282 value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
2283 if (value != NULL && strcmp(value, "on") == 0)
2284 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
2285 else if (value != NULL && strcmp(value, "read") == 0)
2286 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2287 else if (value != NULL && strcmp(value, "soft") == 0)
2288 cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
2289 else if (value != NULL && strcmp(value, "off") == 0)
2290 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2295 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2297 struct ctl_be_lun *cbe_lun;
2298 struct ctl_be_block_lun *be_lun;
2299 struct ctl_lun_create_params *params;
2300 char num_thread_str[16];
2303 int retval, num_threads;
2304 int tmp_num_threads;
2306 params = &req->reqdata.create;
2308 req->status = CTL_LUN_OK;
2310 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2311 cbe_lun = &be_lun->cbe_lun;
2312 be_lun->params = req->reqdata.create;
2313 be_lun->softc = softc;
2314 STAILQ_INIT(&be_lun->input_queue);
2315 STAILQ_INIT(&be_lun->config_read_queue);
2316 STAILQ_INIT(&be_lun->config_write_queue);
2317 STAILQ_INIT(&be_lun->datamove_queue);
2318 mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
2319 mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
2320 cbe_lun->options = nvlist_clone(req->args_nvl);
2322 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2323 cbe_lun->lun_type = params->device_type;
2325 cbe_lun->lun_type = T_DIRECT;
2328 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2329 if (value != NULL) {
2330 if (strcmp(value, "primary") == 0)
2331 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2332 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2333 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2335 if (cbe_lun->lun_type == T_DIRECT ||
2336 cbe_lun->lun_type == T_CDROM) {
2337 be_lun->size_bytes = params->lun_size_bytes;
2338 if (params->blocksize_bytes != 0)
2339 cbe_lun->blocksize = params->blocksize_bytes;
2340 else if (cbe_lun->lun_type == T_CDROM)
2341 cbe_lun->blocksize = 2048;
2343 cbe_lun->blocksize = 512;
2344 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2345 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2346 0 : (be_lun->size_blocks - 1);
2348 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2349 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2350 retval = ctl_be_block_open(be_lun, req);
2353 req->status = CTL_LUN_WARNING;
2356 num_threads = cbb_num_threads;
2361 value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
2362 if (value != NULL) {
2363 tmp_num_threads = strtol(value, NULL, 0);
2366 * We don't let the user specify less than one
2367 * thread, but hope he's clueful enough not to
2368 * specify 1000 threads.
2370 if (tmp_num_threads < 1) {
2371 snprintf(req->error_str, sizeof(req->error_str),
2372 "invalid number of threads %s",
2376 num_threads = tmp_num_threads;
2379 if (be_lun->vn == NULL)
2380 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2381 /* Tell the user the blocksize we ended up using */
2382 params->lun_size_bytes = be_lun->size_bytes;
2383 params->blocksize_bytes = cbe_lun->blocksize;
2384 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2385 cbe_lun->req_lun_id = params->req_lun_id;
2386 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2388 cbe_lun->req_lun_id = 0;
2390 cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
2391 cbe_lun->be = &ctl_be_block_driver;
2393 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2394 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2396 strncpy((char *)cbe_lun->serial_num, tmpstr,
2397 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2399 /* Tell the user what we used for a serial number */
2400 strncpy((char *)params->serial_num, tmpstr,
2401 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2403 strncpy((char *)cbe_lun->serial_num, params->serial_num,
2404 MIN(sizeof(cbe_lun->serial_num),
2405 sizeof(params->serial_num)));
2407 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2408 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
2409 strncpy((char *)cbe_lun->device_id, tmpstr,
2410 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2412 /* Tell the user what we used for a device ID */
2413 strncpy((char *)params->device_id, tmpstr,
2414 MIN(sizeof(params->device_id), sizeof(tmpstr)));
2416 strncpy((char *)cbe_lun->device_id, params->device_id,
2417 MIN(sizeof(cbe_lun->device_id),
2418 sizeof(params->device_id)));
2421 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2423 be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2424 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2426 if (be_lun->io_taskqueue == NULL) {
2427 snprintf(req->error_str, sizeof(req->error_str),
2428 "unable to create taskqueue");
2433 * Note that we start the same number of threads by default for
2434 * both the file case and the block device case. For the file
2435 * case, we need multiple threads to allow concurrency, because the
2436 * vnode interface is designed to be a blocking interface. For the
2437 * block device case, ZFS zvols at least will block the caller's
2438 * context in many instances, and so we need multiple threads to
2439 * overcome that problem. Other block devices don't need as many
2440 * threads, but they shouldn't cause too many problems.
2442 * If the user wants to just have a single thread for a block
2443 * device, he can specify that when the LUN is created, or change
2444 * the tunable/sysctl to alter the default number of threads.
2446 retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2447 /*num threads*/num_threads,
2449 /*proc*/control_softc->ctl_proc,
2450 /*thread name*/"block");
2455 be_lun->num_threads = num_threads;
2457 retval = ctl_add_lun(&be_lun->cbe_lun);
2459 snprintf(req->error_str, sizeof(req->error_str),
2460 "ctl_add_lun() returned error %d, see dmesg for "
2466 be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
2468 DEVSTAT_ALL_SUPPORTED,
2470 | DEVSTAT_TYPE_IF_OTHER,
2471 DEVSTAT_PRIORITY_OTHER);
2473 mtx_lock(&softc->lock);
2475 SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
2476 mtx_unlock(&softc->lock);
2478 params->req_lun_id = cbe_lun->lun_id;
2483 req->status = CTL_LUN_ERROR;
2485 if (be_lun->io_taskqueue != NULL)
2486 taskqueue_free(be_lun->io_taskqueue);
2487 ctl_be_block_close(be_lun);
2488 if (be_lun->dev_path != NULL)
2489 free(be_lun->dev_path, M_CTLBLK);
2490 nvlist_destroy(cbe_lun->options);
2491 mtx_destroy(&be_lun->queue_lock);
2492 mtx_destroy(&be_lun->io_lock);
2493 free(be_lun, M_CTLBLK);
2499 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2501 struct ctl_lun_rm_params *params;
2502 struct ctl_be_block_lun *be_lun;
2503 struct ctl_be_lun *cbe_lun;
2506 params = &req->reqdata.rm;
2508 sx_xlock(&softc->modify_lock);
2509 mtx_lock(&softc->lock);
2510 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2511 if (be_lun->cbe_lun.lun_id == params->lun_id) {
2512 SLIST_REMOVE(&softc->lun_list, be_lun,
2513 ctl_be_block_lun, links);
2518 mtx_unlock(&softc->lock);
2519 sx_xunlock(&softc->modify_lock);
2520 if (be_lun == NULL) {
2521 snprintf(req->error_str, sizeof(req->error_str),
2522 "LUN %u is not managed by the block backend",
2526 cbe_lun = &be_lun->cbe_lun;
2528 if (be_lun->vn != NULL) {
2529 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2530 ctl_lun_no_media(cbe_lun);
2531 taskqueue_drain_all(be_lun->io_taskqueue);
2532 ctl_be_block_close(be_lun);
2535 mtx_lock(&softc->lock);
2536 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2537 mtx_unlock(&softc->lock);
2539 retval = ctl_remove_lun(cbe_lun);
2541 snprintf(req->error_str, sizeof(req->error_str),
2542 "error %d returned from ctl_remove_lun() for "
2543 "LUN %d", retval, params->lun_id);
2544 mtx_lock(&softc->lock);
2545 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2546 mtx_unlock(&softc->lock);
2550 mtx_lock(&softc->lock);
2551 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2552 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2553 if (retval == EINTR)
2556 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2557 if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2558 mtx_unlock(&softc->lock);
2559 free(be_lun, M_CTLBLK);
2561 mtx_unlock(&softc->lock);
2565 req->status = CTL_LUN_OK;
2569 req->status = CTL_LUN_ERROR;
2574 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2576 struct ctl_lun_modify_params *params;
2577 struct ctl_be_block_lun *be_lun;
2578 struct ctl_be_lun *cbe_lun;
2583 params = &req->reqdata.modify;
2585 sx_xlock(&softc->modify_lock);
2586 mtx_lock(&softc->lock);
2587 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2588 if (be_lun->cbe_lun.lun_id == params->lun_id)
2591 mtx_unlock(&softc->lock);
2592 if (be_lun == NULL) {
2593 snprintf(req->error_str, sizeof(req->error_str),
2594 "LUN %u is not managed by the block backend",
2598 cbe_lun = &be_lun->cbe_lun;
2600 if (params->lun_size_bytes != 0)
2601 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2603 if (req->args_nvl != NULL) {
2604 nvlist_destroy(cbe_lun->options);
2605 cbe_lun->options = nvlist_clone(req->args_nvl);
2608 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
2609 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2610 if (value != NULL) {
2611 if (strcmp(value, "primary") == 0)
2612 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2614 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2615 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2616 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2618 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2619 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
2620 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
2621 ctl_lun_primary(cbe_lun);
2623 ctl_lun_secondary(cbe_lun);
2626 oldsize = be_lun->size_blocks;
2627 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2628 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2629 if (be_lun->vn == NULL)
2630 error = ctl_be_block_open(be_lun, req);
2631 else if (vn_isdisk_error(be_lun->vn, &error))
2632 error = ctl_be_block_open_dev(be_lun, req);
2633 else if (be_lun->vn->v_type == VREG) {
2634 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2635 error = ctl_be_block_open_file(be_lun, req);
2636 VOP_UNLOCK(be_lun->vn);
2639 if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
2640 be_lun->vn != NULL) {
2641 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2642 ctl_lun_has_media(cbe_lun);
2643 } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2644 be_lun->vn == NULL) {
2645 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2646 ctl_lun_no_media(cbe_lun);
2648 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2650 if (be_lun->vn != NULL) {
2651 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2652 ctl_lun_no_media(cbe_lun);
2653 taskqueue_drain_all(be_lun->io_taskqueue);
2654 error = ctl_be_block_close(be_lun);
2658 if (be_lun->size_blocks != oldsize)
2659 ctl_lun_capacity_changed(cbe_lun);
2661 /* Tell the user the exact size we ended up using */
2662 params->lun_size_bytes = be_lun->size_bytes;
2664 sx_xunlock(&softc->modify_lock);
2665 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2669 sx_xunlock(&softc->modify_lock);
2670 req->status = CTL_LUN_ERROR;
2675 ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2677 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
2678 struct ctl_be_block_softc *softc = be_lun->softc;
2680 taskqueue_drain_all(be_lun->io_taskqueue);
2681 taskqueue_free(be_lun->io_taskqueue);
2682 if (be_lun->disk_stats != NULL)
2683 devstat_remove_entry(be_lun->disk_stats);
2684 nvlist_destroy(be_lun->cbe_lun.options);
2685 free(be_lun->dev_path, M_CTLBLK);
2686 mtx_destroy(&be_lun->queue_lock);
2687 mtx_destroy(&be_lun->io_lock);
2689 mtx_lock(&softc->lock);
2690 be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2691 if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2694 free(be_lun, M_CTLBLK);
2695 mtx_unlock(&softc->lock);
2699 ctl_be_block_config_write(union ctl_io *io)
2701 struct ctl_be_block_lun *be_lun;
2702 struct ctl_be_lun *cbe_lun;
2705 DPRINTF("entered\n");
2707 cbe_lun = CTL_BACKEND_LUN(io);
2708 be_lun = (struct ctl_be_block_lun *)cbe_lun;
2711 switch (io->scsiio.cdb[0]) {
2712 case SYNCHRONIZE_CACHE:
2713 case SYNCHRONIZE_CACHE_16:
2718 * The upper level CTL code will filter out any CDBs with
2719 * the immediate bit set and return the proper error.
2721 * We don't really need to worry about what LBA range the
2722 * user asked to be synced out. When they issue a sync
2723 * cache command, we'll sync out the whole thing.
2725 mtx_lock(&be_lun->queue_lock);
2726 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2728 mtx_unlock(&be_lun->queue_lock);
2729 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2731 case START_STOP_UNIT: {
2732 struct scsi_start_stop_unit *cdb;
2733 struct ctl_lun_req req;
2735 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2736 if ((cdb->how & SSS_PC_MASK) != 0) {
2737 ctl_set_success(&io->scsiio);
2738 ctl_config_write_done(io);
2741 if (cdb->how & SSS_START) {
2742 if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2743 retval = ctl_be_block_open(be_lun, &req);
2744 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2746 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2747 ctl_lun_has_media(cbe_lun);
2749 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2750 ctl_lun_no_media(cbe_lun);
2753 ctl_start_lun(cbe_lun);
2755 ctl_stop_lun(cbe_lun);
2756 if (cdb->how & SSS_LOEJ) {
2757 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2758 cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2759 ctl_lun_ejected(cbe_lun);
2760 if (be_lun->vn != NULL)
2761 ctl_be_block_close(be_lun);
2765 ctl_set_success(&io->scsiio);
2766 ctl_config_write_done(io);
2770 ctl_set_success(&io->scsiio);
2771 ctl_config_write_done(io);
2774 ctl_set_invalid_opcode(&io->scsiio);
2775 ctl_config_write_done(io);
2776 retval = CTL_RETVAL_COMPLETE;
2784 ctl_be_block_config_read(union ctl_io *io)
2786 struct ctl_be_block_lun *be_lun;
2789 DPRINTF("entered\n");
2791 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2793 switch (io->scsiio.cdb[0]) {
2794 case SERVICE_ACTION_IN:
2795 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2796 mtx_lock(&be_lun->queue_lock);
2797 STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2798 &io->io_hdr, links);
2799 mtx_unlock(&be_lun->queue_lock);
2800 taskqueue_enqueue(be_lun->io_taskqueue,
2802 retval = CTL_RETVAL_QUEUED;
2805 ctl_set_invalid_field(&io->scsiio,
2811 ctl_config_read_done(io);
2812 retval = CTL_RETVAL_COMPLETE;
2815 ctl_set_invalid_opcode(&io->scsiio);
2816 ctl_config_read_done(io);
2817 retval = CTL_RETVAL_COMPLETE;
2825 ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2827 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2830 retval = sbuf_printf(sb, "\t<num_threads>");
2833 retval = sbuf_printf(sb, "%d", lun->num_threads);
2836 retval = sbuf_printf(sb, "</num_threads>\n");
2843 ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2845 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2847 if (lun->getattr == NULL)
2848 return (UINT64_MAX);
2849 return (lun->getattr(lun, attrname));
2853 ctl_be_block_init(void)
2855 struct ctl_be_block_softc *softc = &backend_block_softc;
2857 sx_init(&softc->modify_lock, "ctlblock modify");
2858 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2859 softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2860 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2861 softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG,
2862 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2863 if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2864 softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG,
2865 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2866 SLIST_INIT(&softc->lun_list);
2871 ctl_be_block_shutdown(void)
2873 struct ctl_be_block_softc *softc = &backend_block_softc;
2874 struct ctl_be_block_lun *lun;
2876 mtx_lock(&softc->lock);
2877 while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
2878 SLIST_REMOVE_HEAD(&softc->lun_list, links);
2881 * Drop our lock here. Since ctl_remove_lun() can call
2882 * back into us, this could potentially lead to a recursive
2883 * lock of the same mutex, which would cause a hang.
2885 mtx_unlock(&softc->lock);
2886 ctl_remove_lun(&lun->cbe_lun);
2887 mtx_lock(&softc->lock);
2889 mtx_unlock(&softc->lock);
2890 uma_zdestroy(softc->bufmin_zone);
2891 if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2892 uma_zdestroy(softc->bufmax_zone);
2893 uma_zdestroy(softc->beio_zone);
2894 mtx_destroy(&softc->lock);
2895 sx_destroy(&softc->modify_lock);