2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Silicon Graphics International Corp.
5 * Copyright (c) 2009-2011 Spectra Logic Corporation
6 * Copyright (c) 2012 The FreeBSD Foundation
7 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
10 * Portions of this software were developed by Edward Tomasz Napierala
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions, and the following disclaimer,
18 * without modification.
19 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20 * substantially similar to the "NO WARRANTY" disclaimer below
21 * ("Disclaimer") and any redistribution must be conditioned upon
22 * including a substantially similar Disclaimer requirement for further
23 * binary redistribution.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGES.
38 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
41 * CAM Target Layer driver backend for block devices.
43 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/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 <geom/geom.h>
87 #include <cam/scsi/scsi_all.h>
88 #include <cam/scsi/scsi_da.h>
89 #include <cam/ctl/ctl_io.h>
90 #include <cam/ctl/ctl.h>
91 #include <cam/ctl/ctl_backend.h>
92 #include <cam/ctl/ctl_ioctl.h>
93 #include <cam/ctl/ctl_ha.h>
94 #include <cam/ctl/ctl_scsi_all.h>
95 #include <cam/ctl/ctl_private.h>
96 #include <cam/ctl/ctl_error.h>
99 * The idea here is that we'll allocate enough S/G space to hold a 1MB
100 * I/O. If we get an I/O larger than that, we'll split it.
102 #define CTLBLK_HALF_IO_SIZE (512 * 1024)
103 #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2)
104 #define CTLBLK_MAX_SEG MAXPHYS
105 #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
106 #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2)
109 #define DPRINTF(fmt, args...) \
110 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
112 #define DPRINTF(fmt, args...) do {} while(0)
116 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
118 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
120 SDT_PROVIDER_DEFINE(cbb);
123 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01,
124 CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02,
125 CTL_BE_BLOCK_LUN_WAITING = 0x04,
126 } ctl_be_block_lun_flags;
134 struct ctl_be_block_filedata {
138 union ctl_be_block_bedata {
139 struct ctl_be_block_filedata file;
142 struct ctl_be_block_io;
143 struct ctl_be_block_lun;
145 typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
146 struct ctl_be_block_io *beio);
147 typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
148 const char *attrname);
151 * Backend LUN structure. There is a 1:1 mapping between a block device
152 * and a backend block LUN, and between a backend block LUN and a CTL LUN.
154 struct ctl_be_block_lun {
155 struct ctl_lun_create_params params;
158 ctl_be_block_type dev_type;
160 union ctl_be_block_bedata backend;
161 cbb_dispatch_t dispatch;
162 cbb_dispatch_t lun_flush;
163 cbb_dispatch_t unmap;
164 cbb_dispatch_t get_lba_status;
165 cbb_getattr_t getattr;
167 uint64_t size_blocks;
169 struct ctl_be_block_softc *softc;
170 struct devstat *disk_stats;
171 ctl_be_block_lun_flags flags;
172 STAILQ_ENTRY(ctl_be_block_lun) links;
173 struct ctl_be_lun cbe_lun;
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 {
190 uma_zone_t beio_zone;
192 STAILQ_HEAD(, ctl_be_block_lun) lun_list;
195 static struct ctl_be_block_softc backend_block_softc;
198 * Per-I/O information.
200 struct ctl_be_block_io {
202 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS];
203 struct iovec xiovecs[CTLBLK_MAX_SEGS];
210 uint64_t first_error_offset;
211 struct bintime ds_t0;
212 devstat_tag_type ds_tag_type;
213 devstat_trans_flags ds_trans_type;
217 struct ctl_be_block_softc *softc;
218 struct ctl_be_block_lun *lun;
219 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
222 extern struct ctl_softc *control_softc;
224 static int cbb_num_threads = 14;
225 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
226 "CAM Target Layer Block Backend");
227 SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
228 &cbb_num_threads, 0, "Number of threads per backing file");
230 static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
231 static void ctl_free_beio(struct ctl_be_block_io *beio);
232 static void ctl_complete_beio(struct ctl_be_block_io *beio);
233 static int ctl_be_block_move_done(union ctl_io *io);
234 static void ctl_be_block_biodone(struct bio *bio);
235 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
236 struct ctl_be_block_io *beio);
237 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
238 struct ctl_be_block_io *beio);
239 static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
240 struct ctl_be_block_io *beio);
241 static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
242 const char *attrname);
243 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
244 struct ctl_be_block_io *beio);
245 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
246 struct ctl_be_block_io *beio);
247 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
248 struct ctl_be_block_io *beio);
249 static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
250 const char *attrname);
251 static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
253 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
255 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
257 static void ctl_be_block_worker(void *context, int pending);
258 static int ctl_be_block_submit(union ctl_io *io);
259 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
260 int flag, struct thread *td);
261 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
262 struct ctl_lun_req *req);
263 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
264 struct ctl_lun_req *req);
265 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
266 static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
267 struct ctl_lun_req *req);
268 static int ctl_be_block_create(struct ctl_be_block_softc *softc,
269 struct ctl_lun_req *req);
270 static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
271 struct ctl_lun_req *req);
272 static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
273 struct ctl_lun_req *req);
274 static void ctl_be_block_lun_shutdown(void *be_lun);
275 static void ctl_be_block_lun_config_status(void *be_lun,
276 ctl_lun_config_status status);
277 static int ctl_be_block_config_write(union ctl_io *io);
278 static int ctl_be_block_config_read(union ctl_io *io);
279 static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
280 static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
281 static int ctl_be_block_init(void);
282 static int ctl_be_block_shutdown(void);
284 static struct ctl_backend_driver ctl_be_block_driver =
287 .flags = CTL_BE_FLAG_HAS_CONFIG,
288 .init = ctl_be_block_init,
289 .shutdown = ctl_be_block_shutdown,
290 .data_submit = ctl_be_block_submit,
291 .data_move_done = ctl_be_block_move_done,
292 .config_read = ctl_be_block_config_read,
293 .config_write = ctl_be_block_config_write,
294 .ioctl = ctl_be_block_ioctl,
295 .lun_info = ctl_be_block_lun_info,
296 .lun_attr = ctl_be_block_lun_attr
299 MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
300 CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
302 static struct ctl_be_block_io *
303 ctl_alloc_beio(struct ctl_be_block_softc *softc)
305 struct ctl_be_block_io *beio;
307 beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
313 ctl_free_beio(struct ctl_be_block_io *beio)
320 for (i = 0; i < beio->num_segs; i++) {
321 if (beio->sg_segs[i].addr == NULL)
324 uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
325 beio->sg_segs[i].addr = NULL;
327 /* For compare we had two equal S/G lists. */
328 if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
329 uma_zfree(beio->lun->lun_zone,
330 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
331 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
335 if (duplicate_free > 0) {
336 printf("%s: %d duplicate frees out of %d segments\n", __func__,
337 duplicate_free, beio->num_segs);
340 uma_zfree(beio->softc->beio_zone, beio);
344 ctl_complete_beio(struct ctl_be_block_io *beio)
346 union ctl_io *io = beio->io;
348 if (beio->beio_cont != NULL) {
349 beio->beio_cont(beio);
352 ctl_data_submit_done(io);
357 cmp(uint8_t *a, uint8_t *b, size_t size)
361 for (i = 0; i < size; i++) {
369 ctl_be_block_compare(union ctl_io *io)
371 struct ctl_be_block_io *beio;
376 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
378 for (i = 0; i < beio->num_segs; i++) {
379 res = cmp(beio->sg_segs[i].addr,
380 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
381 beio->sg_segs[i].len);
383 if (res < beio->sg_segs[i].len)
386 if (i < beio->num_segs) {
387 scsi_u64to8b(off, info);
388 ctl_set_sense(&io->scsiio, /*current_error*/ 1,
389 /*sense_key*/ SSD_KEY_MISCOMPARE,
390 /*asc*/ 0x1D, /*ascq*/ 0x00,
391 /*type*/ SSD_ELEM_INFO,
392 /*size*/ sizeof(info), /*data*/ &info,
393 /*type*/ SSD_ELEM_NONE);
395 ctl_set_success(&io->scsiio);
399 ctl_be_block_move_done(union ctl_io *io)
401 struct ctl_be_block_io *beio;
402 struct ctl_be_block_lun *be_lun;
403 struct ctl_lba_len_flags *lbalen;
405 struct bintime cur_bt;
408 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
411 DPRINTF("entered\n");
414 getbinuptime(&cur_bt);
415 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
416 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
418 io->io_hdr.num_dmas++;
419 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
422 * We set status at this point for read commands, and write
423 * commands with errors.
425 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
427 } else if ((io->io_hdr.port_status != 0) &&
428 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
429 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
430 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
431 /*retry_count*/ io->io_hdr.port_status);
432 } else if (io->scsiio.kern_data_resid != 0 &&
433 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
434 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
435 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
436 ctl_set_invalid_field_ciu(&io->scsiio);
437 } else if ((io->io_hdr.port_status == 0) &&
438 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
439 lbalen = ARGS(beio->io);
440 if (lbalen->flags & CTL_LLF_READ) {
441 ctl_set_success(&io->scsiio);
442 } else if (lbalen->flags & CTL_LLF_COMPARE) {
443 /* We have two data blocks ready for comparison. */
444 ctl_be_block_compare(io);
449 * If this is a read, or a write with errors, it is done.
451 if ((beio->bio_cmd == BIO_READ)
452 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
453 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
454 ctl_complete_beio(beio);
459 * At this point, we have a write and the DMA completed
460 * successfully. We now have to queue it to the task queue to
461 * execute the backend I/O. That is because we do blocking
462 * memory allocations, and in the file backing case, blocking I/O.
463 * This move done routine is generally called in the SIM's
464 * interrupt context, and therefore we cannot block.
466 mtx_lock(&be_lun->queue_lock);
467 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
468 mtx_unlock(&be_lun->queue_lock);
469 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
475 ctl_be_block_biodone(struct bio *bio)
477 struct ctl_be_block_io *beio;
478 struct ctl_be_block_lun *be_lun;
482 beio = bio->bio_caller1;
486 DPRINTF("entered\n");
488 error = bio->bio_error;
489 mtx_lock(&be_lun->io_lock);
491 (beio->first_error == 0 ||
492 bio->bio_offset < beio->first_error_offset)) {
493 beio->first_error = error;
494 beio->first_error_offset = bio->bio_offset;
497 beio->num_bios_done++;
500 * XXX KDM will this cause WITNESS to complain? Holding a lock
501 * during the free might cause it to complain.
506 * If the send complete bit isn't set, or we aren't the last I/O to
507 * complete, then we're done.
509 if ((beio->send_complete == 0)
510 || (beio->num_bios_done < beio->num_bios_sent)) {
511 mtx_unlock(&be_lun->io_lock);
516 * At this point, we've verified that we are the last I/O to
517 * complete, so it's safe to drop the lock.
519 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
520 beio->ds_tag_type, beio->ds_trans_type,
521 /*now*/ NULL, /*then*/&beio->ds_t0);
522 mtx_unlock(&be_lun->io_lock);
525 * If there are any errors from the backing device, we fail the
526 * entire I/O with a medium error.
528 error = beio->first_error;
530 if (error == EOPNOTSUPP) {
531 ctl_set_invalid_opcode(&io->scsiio);
532 } else if (error == ENOSPC || error == EDQUOT) {
533 ctl_set_space_alloc_fail(&io->scsiio);
534 } else if (error == EROFS || error == EACCES) {
535 ctl_set_hw_write_protected(&io->scsiio);
536 } else if (beio->bio_cmd == BIO_FLUSH) {
537 /* XXX KDM is there is a better error here? */
538 ctl_set_internal_failure(&io->scsiio,
540 /*retry_count*/ 0xbad2);
542 ctl_set_medium_error(&io->scsiio,
543 beio->bio_cmd == BIO_READ);
545 ctl_complete_beio(beio);
550 * If this is a write, a flush, a delete or verify, we're all done.
551 * If this is a read, we can now send the data to the user.
553 if ((beio->bio_cmd == BIO_WRITE)
554 || (beio->bio_cmd == BIO_FLUSH)
555 || (beio->bio_cmd == BIO_DELETE)
556 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
557 ctl_set_success(&io->scsiio);
558 ctl_complete_beio(beio);
560 if ((ARGS(io)->flags & CTL_LLF_READ) &&
561 beio->beio_cont == NULL) {
562 ctl_set_success(&io->scsiio);
566 getbinuptime(&io->io_hdr.dma_start_bt);
573 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
574 struct ctl_be_block_io *beio)
576 union ctl_io *io = beio->io;
577 struct mount *mountpoint;
578 int error, lock_flags;
580 DPRINTF("entered\n");
582 binuptime(&beio->ds_t0);
583 mtx_lock(&be_lun->io_lock);
584 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
585 mtx_unlock(&be_lun->io_lock);
587 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
589 if (MNT_SHARED_WRITES(mountpoint) ||
590 ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
591 lock_flags = LK_SHARED;
593 lock_flags = LK_EXCLUSIVE;
594 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
595 error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
597 VOP_UNLOCK(be_lun->vn, 0);
599 vn_finished_write(mountpoint);
601 mtx_lock(&be_lun->io_lock);
602 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
603 beio->ds_tag_type, beio->ds_trans_type,
604 /*now*/ NULL, /*then*/&beio->ds_t0);
605 mtx_unlock(&be_lun->io_lock);
608 ctl_set_success(&io->scsiio);
610 /* XXX KDM is there is a better error here? */
611 ctl_set_internal_failure(&io->scsiio,
613 /*retry_count*/ 0xbad1);
616 ctl_complete_beio(beio);
619 SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
620 SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
621 SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
622 SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
625 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
626 struct ctl_be_block_io *beio)
628 struct ctl_be_block_filedata *file_data;
631 struct iovec *xiovec;
635 DPRINTF("entered\n");
637 file_data = &be_lun->backend.file;
640 if (ARGS(io)->flags & CTL_LLF_DPO)
642 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
645 bzero(&xuio, sizeof(xuio));
646 if (beio->bio_cmd == BIO_READ) {
647 SDT_PROBE0(cbb, , read, file_start);
648 xuio.uio_rw = UIO_READ;
650 SDT_PROBE0(cbb, , write, file_start);
651 xuio.uio_rw = UIO_WRITE;
653 xuio.uio_offset = beio->io_offset;
654 xuio.uio_resid = beio->io_len;
655 xuio.uio_segflg = UIO_SYSSPACE;
656 xuio.uio_iov = beio->xiovecs;
657 xuio.uio_iovcnt = beio->num_segs;
658 xuio.uio_td = curthread;
660 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
661 xiovec->iov_base = beio->sg_segs[i].addr;
662 xiovec->iov_len = beio->sg_segs[i].len;
665 binuptime(&beio->ds_t0);
666 mtx_lock(&be_lun->io_lock);
667 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
668 mtx_unlock(&be_lun->io_lock);
670 if (beio->bio_cmd == BIO_READ) {
671 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
674 * UFS pays attention to IO_DIRECT for reads. If the
675 * DIRECTIO option is configured into the kernel, it calls
676 * ffs_rawread(). But that only works for single-segment
677 * uios with user space addresses. In our case, with a
678 * kernel uio, it still reads into the buffer cache, but it
679 * will just try to release the buffer from the cache later
682 * ZFS does not pay attention to IO_DIRECT for reads.
684 * UFS does not pay attention to IO_SYNC for reads.
686 * ZFS pays attention to IO_SYNC (which translates into the
687 * Solaris define FRSYNC for zfs_read()) for reads. It
688 * attempts to sync the file before reading.
690 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
692 VOP_UNLOCK(be_lun->vn, 0);
693 SDT_PROBE0(cbb, , read, file_done);
694 if (error == 0 && xuio.uio_resid > 0) {
696 * If we red less then requested (EOF), then
697 * we should clean the rest of the buffer.
699 s = beio->io_len - xuio.uio_resid;
700 for (i = 0; i < beio->num_segs; i++) {
701 if (s >= beio->sg_segs[i].len) {
702 s -= beio->sg_segs[i].len;
705 bzero((uint8_t *)beio->sg_segs[i].addr + s,
706 beio->sg_segs[i].len - s);
711 struct mount *mountpoint;
714 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
716 if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL)
717 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
718 lock_flags = LK_SHARED;
720 lock_flags = LK_EXCLUSIVE;
721 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
724 * UFS pays attention to IO_DIRECT for writes. The write
725 * is done asynchronously. (Normally the write would just
726 * get put into cache.
728 * UFS pays attention to IO_SYNC for writes. It will
729 * attempt to write the buffer out synchronously if that
732 * ZFS does not pay attention to IO_DIRECT for writes.
734 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
735 * for writes. It will flush the transaction from the
736 * cache before returning.
738 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
739 VOP_UNLOCK(be_lun->vn, 0);
741 vn_finished_write(mountpoint);
742 SDT_PROBE0(cbb, , write, file_done);
745 mtx_lock(&be_lun->io_lock);
746 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
747 beio->ds_tag_type, beio->ds_trans_type,
748 /*now*/ NULL, /*then*/&beio->ds_t0);
749 mtx_unlock(&be_lun->io_lock);
752 * If we got an error, set the sense data to "MEDIUM ERROR" and
753 * return the I/O to the user.
756 if (error == ENOSPC || error == EDQUOT) {
757 ctl_set_space_alloc_fail(&io->scsiio);
758 } else if (error == EROFS || error == EACCES) {
759 ctl_set_hw_write_protected(&io->scsiio);
761 ctl_set_medium_error(&io->scsiio,
762 beio->bio_cmd == BIO_READ);
764 ctl_complete_beio(beio);
769 * If this is a write or a verify, we're all done.
770 * If this is a read, we can now send the data to the user.
772 if ((beio->bio_cmd == BIO_WRITE) ||
773 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
774 ctl_set_success(&io->scsiio);
775 ctl_complete_beio(beio);
777 if ((ARGS(io)->flags & CTL_LLF_READ) &&
778 beio->beio_cont == NULL) {
779 ctl_set_success(&io->scsiio);
783 getbinuptime(&io->io_hdr.dma_start_bt);
790 ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
791 struct ctl_be_block_io *beio)
793 union ctl_io *io = beio->io;
794 struct ctl_lba_len_flags *lbalen = ARGS(io);
795 struct scsi_get_lba_status_data *data;
799 DPRINTF("entered\n");
801 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
802 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
803 error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
804 0, curthread->td_ucred, curthread);
805 if (error == 0 && off > roff)
806 status = 0; /* mapped up to off */
808 error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
809 0, curthread->td_ucred, curthread);
810 if (error == 0 && off > roff)
811 status = 1; /* deallocated up to off */
813 status = 0; /* unknown up to the end */
814 off = be_lun->size_bytes;
817 VOP_UNLOCK(be_lun->vn, 0);
819 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
820 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
821 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
822 lbalen->lba), data->descr[0].length);
823 data->descr[0].status = status;
825 ctl_complete_beio(beio);
829 ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
832 struct statfs statfs;
837 if (be_lun->vn == NULL)
839 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
840 if (strcmp(attrname, "blocksused") == 0) {
841 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
843 val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
845 if (strcmp(attrname, "blocksavail") == 0 &&
846 (be_lun->vn->v_iflag & VI_DOOMED) == 0) {
847 error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
849 val = statfs.f_bavail * statfs.f_bsize /
850 be_lun->cbe_lun.blocksize;
852 VOP_UNLOCK(be_lun->vn, 0);
857 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
858 struct ctl_be_block_io *beio)
864 struct iovec *xiovec;
865 int error, flags, i, ref;
867 DPRINTF("entered\n");
871 if (ARGS(io)->flags & CTL_LLF_DPO)
873 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
876 bzero(&xuio, sizeof(xuio));
877 if (beio->bio_cmd == BIO_READ) {
878 SDT_PROBE0(cbb, , read, file_start);
879 xuio.uio_rw = UIO_READ;
881 SDT_PROBE0(cbb, , write, file_start);
882 xuio.uio_rw = UIO_WRITE;
884 xuio.uio_offset = beio->io_offset;
885 xuio.uio_resid = beio->io_len;
886 xuio.uio_segflg = UIO_SYSSPACE;
887 xuio.uio_iov = beio->xiovecs;
888 xuio.uio_iovcnt = beio->num_segs;
889 xuio.uio_td = curthread;
891 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
892 xiovec->iov_base = beio->sg_segs[i].addr;
893 xiovec->iov_len = beio->sg_segs[i].len;
896 binuptime(&beio->ds_t0);
897 mtx_lock(&be_lun->io_lock);
898 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
899 mtx_unlock(&be_lun->io_lock);
901 csw = devvn_refthread(be_lun->vn, &dev, &ref);
903 if (beio->bio_cmd == BIO_READ)
904 error = csw->d_read(dev, &xuio, flags);
906 error = csw->d_write(dev, &xuio, flags);
907 dev_relthread(dev, ref);
911 if (beio->bio_cmd == BIO_READ)
912 SDT_PROBE0(cbb, , read, file_done);
914 SDT_PROBE0(cbb, , write, file_done);
916 mtx_lock(&be_lun->io_lock);
917 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
918 beio->ds_tag_type, beio->ds_trans_type,
919 /*now*/ NULL, /*then*/&beio->ds_t0);
920 mtx_unlock(&be_lun->io_lock);
923 * If we got an error, set the sense data to "MEDIUM ERROR" and
924 * return the I/O to the user.
927 if (error == ENOSPC || error == EDQUOT) {
928 ctl_set_space_alloc_fail(&io->scsiio);
929 } else if (error == EROFS || error == EACCES) {
930 ctl_set_hw_write_protected(&io->scsiio);
932 ctl_set_medium_error(&io->scsiio,
933 beio->bio_cmd == BIO_READ);
935 ctl_complete_beio(beio);
940 * If this is a write or a verify, we're all done.
941 * If this is a read, we can now send the data to the user.
943 if ((beio->bio_cmd == BIO_WRITE) ||
944 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
945 ctl_set_success(&io->scsiio);
946 ctl_complete_beio(beio);
948 if ((ARGS(io)->flags & CTL_LLF_READ) &&
949 beio->beio_cont == NULL) {
950 ctl_set_success(&io->scsiio);
954 getbinuptime(&io->io_hdr.dma_start_bt);
961 ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
962 struct ctl_be_block_io *beio)
964 union ctl_io *io = beio->io;
967 struct ctl_lba_len_flags *lbalen = ARGS(io);
968 struct scsi_get_lba_status_data *data;
970 int error, ref, status;
972 DPRINTF("entered\n");
974 csw = devvn_refthread(be_lun->vn, &dev, &ref);
976 status = 0; /* unknown up to the end */
977 off = be_lun->size_bytes;
980 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
981 error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
983 if (error == 0 && off > roff)
984 status = 0; /* mapped up to off */
986 error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
988 if (error == 0 && off > roff)
989 status = 1; /* deallocated up to off */
991 status = 0; /* unknown up to the end */
992 off = be_lun->size_bytes;
995 dev_relthread(dev, ref);
998 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
999 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
1000 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
1001 lbalen->lba), data->descr[0].length);
1002 data->descr[0].status = status;
1004 ctl_complete_beio(beio);
1008 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1009 struct ctl_be_block_io *beio)
1016 DPRINTF("entered\n");
1018 /* This can't fail, it's a blocking allocation. */
1019 bio = g_alloc_bio();
1021 bio->bio_cmd = BIO_FLUSH;
1022 bio->bio_offset = 0;
1024 bio->bio_done = ctl_be_block_biodone;
1025 bio->bio_caller1 = beio;
1026 bio->bio_pblkno = 0;
1029 * We don't need to acquire the LUN lock here, because we are only
1030 * sending one bio, and so there is no other context to synchronize
1033 beio->num_bios_sent = 1;
1034 beio->send_complete = 1;
1036 binuptime(&beio->ds_t0);
1037 mtx_lock(&be_lun->io_lock);
1038 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1039 mtx_unlock(&be_lun->io_lock);
1041 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1044 csw->d_strategy(bio);
1045 dev_relthread(dev, ref);
1047 bio->bio_error = ENXIO;
1048 ctl_be_block_biodone(bio);
1053 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1054 struct ctl_be_block_io *beio,
1055 uint64_t off, uint64_t len, int last)
1063 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1064 maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1066 bio = g_alloc_bio();
1067 bio->bio_cmd = BIO_DELETE;
1069 bio->bio_offset = off;
1070 bio->bio_length = MIN(len, maxlen);
1072 bio->bio_done = ctl_be_block_biodone;
1073 bio->bio_caller1 = beio;
1074 bio->bio_pblkno = off / be_lun->cbe_lun.blocksize;
1076 off += bio->bio_length;
1077 len -= bio->bio_length;
1079 mtx_lock(&be_lun->io_lock);
1080 beio->num_bios_sent++;
1081 if (last && len == 0)
1082 beio->send_complete = 1;
1083 mtx_unlock(&be_lun->io_lock);
1086 csw->d_strategy(bio);
1088 bio->bio_error = ENXIO;
1089 ctl_be_block_biodone(bio);
1093 dev_relthread(dev, ref);
1097 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1098 struct ctl_be_block_io *beio)
1101 struct ctl_ptr_len_flags *ptrlen;
1102 struct scsi_unmap_desc *buf, *end;
1107 DPRINTF("entered\n");
1109 binuptime(&beio->ds_t0);
1110 mtx_lock(&be_lun->io_lock);
1111 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1112 mtx_unlock(&be_lun->io_lock);
1114 if (beio->io_offset == -1) {
1116 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1117 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1118 end = buf + ptrlen->len / sizeof(*buf);
1119 for (; buf < end; buf++) {
1120 len = (uint64_t)scsi_4btoul(buf->length) *
1121 be_lun->cbe_lun.blocksize;
1122 beio->io_len += len;
1123 ctl_be_block_unmap_dev_range(be_lun, beio,
1124 scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
1125 len, (end - buf < 2) ? TRUE : FALSE);
1128 ctl_be_block_unmap_dev_range(be_lun, beio,
1129 beio->io_offset, beio->io_len, TRUE);
1133 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1134 struct ctl_be_block_io *beio)
1136 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1141 int i, max_iosize, ref;
1143 DPRINTF("entered\n");
1144 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1147 * We have to limit our I/O size to the maximum supported by the
1148 * backend device. Hopefully it is MAXPHYS. If the driver doesn't
1149 * set it properly, use DFLTPHYS.
1152 max_iosize = dev->si_iosize_max;
1153 if (max_iosize < PAGE_SIZE)
1154 max_iosize = DFLTPHYS;
1156 max_iosize = DFLTPHYS;
1158 cur_offset = beio->io_offset;
1159 for (i = 0; i < beio->num_segs; i++) {
1163 cur_size = beio->sg_segs[i].len;
1164 cur_ptr = beio->sg_segs[i].addr;
1166 while (cur_size > 0) {
1167 /* This can't fail, it's a blocking allocation. */
1168 bio = g_alloc_bio();
1170 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1172 bio->bio_cmd = beio->bio_cmd;
1174 bio->bio_caller1 = beio;
1175 bio->bio_length = min(cur_size, max_iosize);
1176 bio->bio_offset = cur_offset;
1177 bio->bio_data = cur_ptr;
1178 bio->bio_done = ctl_be_block_biodone;
1179 bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1181 cur_offset += bio->bio_length;
1182 cur_ptr += bio->bio_length;
1183 cur_size -= bio->bio_length;
1185 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1186 beio->num_bios_sent++;
1189 binuptime(&beio->ds_t0);
1190 mtx_lock(&be_lun->io_lock);
1191 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1192 beio->send_complete = 1;
1193 mtx_unlock(&be_lun->io_lock);
1196 * Fire off all allocated requests!
1198 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1199 TAILQ_REMOVE(&queue, bio, bio_queue);
1201 csw->d_strategy(bio);
1203 bio->bio_error = ENXIO;
1204 ctl_be_block_biodone(bio);
1208 dev_relthread(dev, ref);
1212 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1214 struct diocgattr_arg arg;
1219 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1221 return (UINT64_MAX);
1222 strlcpy(arg.name, attrname, sizeof(arg.name));
1223 arg.len = sizeof(arg.value.off);
1225 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
1229 dev_relthread(dev, ref);
1231 return (UINT64_MAX);
1232 return (arg.value.off);
1236 ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1239 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1240 struct ctl_be_block_io *beio;
1241 struct ctl_lba_len_flags *lbalen;
1243 DPRINTF("entered\n");
1244 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1245 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1247 beio->io_len = lbalen->len * cbe_lun->blocksize;
1248 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1249 beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
1250 beio->bio_cmd = BIO_FLUSH;
1251 beio->ds_trans_type = DEVSTAT_NO_DATA;
1253 be_lun->lun_flush(be_lun, beio);
1257 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1262 ctl_free_beio(beio);
1263 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1264 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1265 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1266 ctl_config_write_done(io);
1270 ctl_be_block_config_write(io);
1274 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1277 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1278 struct ctl_be_block_io *beio;
1279 struct ctl_lba_len_flags *lbalen;
1280 uint64_t len_left, lba;
1281 uint32_t pb, pbo, adj;
1285 DPRINTF("entered\n");
1287 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1288 lbalen = ARGS(beio->io);
1290 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1291 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1292 ctl_free_beio(beio);
1293 ctl_set_invalid_field(&io->scsiio,
1299 ctl_config_write_done(io);
1303 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1304 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1305 beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1306 beio->bio_cmd = BIO_DELETE;
1307 beio->ds_trans_type = DEVSTAT_FREE;
1309 be_lun->unmap(be_lun, beio);
1313 beio->bio_cmd = BIO_WRITE;
1314 beio->ds_trans_type = DEVSTAT_WRITE;
1316 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1317 (uintmax_t)lbalen->lba, lbalen->len);
1319 pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
1320 if (be_lun->cbe_lun.pblockoff > 0)
1321 pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1324 len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1325 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1328 * Setup the S/G entry for this chunk.
1330 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1331 if (pb > cbe_lun->blocksize) {
1332 adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
1337 seglen -= seglen % cbe_lun->blocksize;
1339 seglen -= seglen % cbe_lun->blocksize;
1340 beio->sg_segs[i].len = seglen;
1341 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1343 DPRINTF("segment %d addr %p len %zd\n", i,
1344 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1349 buf = beio->sg_segs[i].addr;
1351 for (; buf < end; buf += cbe_lun->blocksize) {
1352 if (lbalen->flags & SWS_NDOB) {
1353 memset(buf, 0, cbe_lun->blocksize);
1355 memcpy(buf, io->scsiio.kern_data_ptr,
1356 cbe_lun->blocksize);
1358 if (lbalen->flags & SWS_LBDATA)
1359 scsi_ulto4b(lbalen->lba + lba, buf);
1364 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1365 beio->io_len = lba * cbe_lun->blocksize;
1367 /* We can not do all in one run. Correct and schedule rerun. */
1371 beio->beio_cont = ctl_be_block_cw_done_ws;
1374 be_lun->dispatch(be_lun, beio);
1378 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1381 struct ctl_be_block_io *beio;
1382 struct ctl_ptr_len_flags *ptrlen;
1384 DPRINTF("entered\n");
1386 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1387 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1389 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1390 ctl_free_beio(beio);
1391 ctl_set_invalid_field(&io->scsiio,
1397 ctl_config_write_done(io);
1402 beio->io_offset = -1;
1403 beio->bio_cmd = BIO_DELETE;
1404 beio->ds_trans_type = DEVSTAT_FREE;
1406 be_lun->unmap(be_lun, beio);
1410 ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1415 ctl_free_beio(beio);
1416 ctl_config_read_done(io);
1420 ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1423 struct ctl_be_block_io *beio;
1424 struct ctl_be_block_softc *softc;
1426 DPRINTF("entered\n");
1428 softc = be_lun->softc;
1429 beio = ctl_alloc_beio(softc);
1432 beio->beio_cont = ctl_be_block_cr_done;
1433 PRIV(io)->ptr = (void *)beio;
1435 switch (io->scsiio.cdb[0]) {
1436 case SERVICE_ACTION_IN: /* GET LBA STATUS */
1438 beio->ds_trans_type = DEVSTAT_NO_DATA;
1439 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1441 if (be_lun->get_lba_status)
1442 be_lun->get_lba_status(be_lun, beio);
1444 ctl_be_block_cr_done(beio);
1447 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1453 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1458 ctl_free_beio(beio);
1459 ctl_config_write_done(io);
1463 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1466 struct ctl_be_block_io *beio;
1467 struct ctl_be_block_softc *softc;
1469 DPRINTF("entered\n");
1471 softc = be_lun->softc;
1472 beio = ctl_alloc_beio(softc);
1475 beio->beio_cont = ctl_be_block_cw_done;
1476 switch (io->scsiio.tag_type) {
1477 case CTL_TAG_ORDERED:
1478 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1480 case CTL_TAG_HEAD_OF_QUEUE:
1481 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1483 case CTL_TAG_UNTAGGED:
1484 case CTL_TAG_SIMPLE:
1487 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1490 PRIV(io)->ptr = (void *)beio;
1492 switch (io->scsiio.cdb[0]) {
1493 case SYNCHRONIZE_CACHE:
1494 case SYNCHRONIZE_CACHE_16:
1495 ctl_be_block_cw_dispatch_sync(be_lun, io);
1499 ctl_be_block_cw_dispatch_ws(be_lun, io);
1502 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1505 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1510 SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
1511 SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
1512 SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
1513 SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1516 ctl_be_block_next(struct ctl_be_block_io *beio)
1518 struct ctl_be_block_lun *be_lun;
1523 ctl_free_beio(beio);
1524 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1525 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1526 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1527 ctl_data_submit_done(io);
1531 io->io_hdr.status &= ~CTL_STATUS_MASK;
1532 io->io_hdr.status |= CTL_STATUS_NONE;
1534 mtx_lock(&be_lun->queue_lock);
1535 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1536 mtx_unlock(&be_lun->queue_lock);
1537 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1541 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1544 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1545 struct ctl_be_block_io *beio;
1546 struct ctl_be_block_softc *softc;
1547 struct ctl_lba_len_flags *lbalen;
1548 struct ctl_ptr_len_flags *bptrlen;
1549 uint64_t len_left, lbas;
1552 softc = be_lun->softc;
1554 DPRINTF("entered\n");
1557 if (lbalen->flags & CTL_LLF_WRITE) {
1558 SDT_PROBE0(cbb, , write, start);
1560 SDT_PROBE0(cbb, , read, start);
1563 beio = ctl_alloc_beio(softc);
1567 bptrlen->ptr = (void *)beio;
1569 switch (io->scsiio.tag_type) {
1570 case CTL_TAG_ORDERED:
1571 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1573 case CTL_TAG_HEAD_OF_QUEUE:
1574 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1576 case CTL_TAG_UNTAGGED:
1577 case CTL_TAG_SIMPLE:
1580 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1584 if (lbalen->flags & CTL_LLF_WRITE) {
1585 beio->bio_cmd = BIO_WRITE;
1586 beio->ds_trans_type = DEVSTAT_WRITE;
1588 beio->bio_cmd = BIO_READ;
1589 beio->ds_trans_type = DEVSTAT_READ;
1592 DPRINTF("%s at LBA %jx len %u @%ju\n",
1593 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1594 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1595 if (lbalen->flags & CTL_LLF_COMPARE)
1596 lbas = CTLBLK_HALF_IO_SIZE;
1598 lbas = CTLBLK_MAX_IO_SIZE;
1599 lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
1600 beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
1601 beio->io_len = lbas * cbe_lun->blocksize;
1602 bptrlen->len += lbas;
1604 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1605 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1606 i, CTLBLK_MAX_SEGS));
1609 * Setup the S/G entry for this chunk.
1611 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1612 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1614 DPRINTF("segment %d addr %p len %zd\n", i,
1615 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1617 /* Set up second segment for compare operation. */
1618 if (lbalen->flags & CTL_LLF_COMPARE) {
1619 beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1620 beio->sg_segs[i].len;
1621 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1622 uma_zalloc(be_lun->lun_zone, M_WAITOK);
1626 len_left -= beio->sg_segs[i].len;
1628 if (bptrlen->len < lbalen->len)
1629 beio->beio_cont = ctl_be_block_next;
1630 io->scsiio.be_move_done = ctl_be_block_move_done;
1631 /* For compare we have separate S/G lists for read and datamove. */
1632 if (lbalen->flags & CTL_LLF_COMPARE)
1633 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1635 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1636 io->scsiio.kern_data_len = beio->io_len;
1637 io->scsiio.kern_sg_entries = beio->num_segs;
1638 io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1641 * For the read case, we need to read the data into our buffers and
1642 * then we can send it back to the user. For the write case, we
1643 * need to get the data from the user first.
1645 if (beio->bio_cmd == BIO_READ) {
1646 SDT_PROBE0(cbb, , read, alloc_done);
1647 be_lun->dispatch(be_lun, beio);
1649 SDT_PROBE0(cbb, , write, alloc_done);
1651 getbinuptime(&io->io_hdr.dma_start_bt);
1658 ctl_be_block_worker(void *context, int pending)
1660 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1661 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1663 struct ctl_be_block_io *beio;
1665 DPRINTF("entered\n");
1667 * Fetch and process I/Os from all queues. If we detect LUN
1668 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1669 * so make response maximally opaque to not confuse initiator.
1672 mtx_lock(&be_lun->queue_lock);
1673 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1675 DPRINTF("datamove queue\n");
1676 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1678 mtx_unlock(&be_lun->queue_lock);
1679 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1680 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1681 ctl_set_busy(&io->scsiio);
1682 ctl_complete_beio(beio);
1685 be_lun->dispatch(be_lun, beio);
1688 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1690 DPRINTF("config write queue\n");
1691 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1693 mtx_unlock(&be_lun->queue_lock);
1694 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1695 ctl_set_busy(&io->scsiio);
1696 ctl_config_write_done(io);
1699 ctl_be_block_cw_dispatch(be_lun, io);
1702 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1704 DPRINTF("config read queue\n");
1705 STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1707 mtx_unlock(&be_lun->queue_lock);
1708 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1709 ctl_set_busy(&io->scsiio);
1710 ctl_config_read_done(io);
1713 ctl_be_block_cr_dispatch(be_lun, io);
1716 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1718 DPRINTF("input queue\n");
1719 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1721 mtx_unlock(&be_lun->queue_lock);
1722 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1723 ctl_set_busy(&io->scsiio);
1724 ctl_data_submit_done(io);
1727 ctl_be_block_dispatch(be_lun, io);
1732 * If we get here, there is no work left in the queues, so
1733 * just break out and let the task queue go to sleep.
1735 mtx_unlock(&be_lun->queue_lock);
1741 * Entry point from CTL to the backend for I/O. We queue everything to a
1742 * work thread, so this just puts the I/O on a queue and wakes up the
1746 ctl_be_block_submit(union ctl_io *io)
1748 struct ctl_be_block_lun *be_lun;
1749 struct ctl_be_lun *cbe_lun;
1751 DPRINTF("entered\n");
1753 cbe_lun = CTL_BACKEND_LUN(io);
1754 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
1757 * Make sure we only get SCSI I/O.
1759 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1760 "%#x) encountered", io->io_hdr.io_type));
1764 mtx_lock(&be_lun->queue_lock);
1765 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1766 mtx_unlock(&be_lun->queue_lock);
1767 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1769 return (CTL_RETVAL_COMPLETE);
1773 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1774 int flag, struct thread *td)
1776 struct ctl_be_block_softc *softc;
1779 softc = &backend_block_softc;
1785 struct ctl_lun_req *lun_req;
1787 lun_req = (struct ctl_lun_req *)addr;
1789 switch (lun_req->reqtype) {
1790 case CTL_LUNREQ_CREATE:
1791 error = ctl_be_block_create(softc, lun_req);
1794 error = ctl_be_block_rm(softc, lun_req);
1796 case CTL_LUNREQ_MODIFY:
1797 error = ctl_be_block_modify(softc, lun_req);
1800 lun_req->status = CTL_LUN_ERROR;
1801 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1802 "invalid LUN request type %d",
1817 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1819 struct ctl_be_lun *cbe_lun;
1820 struct ctl_be_block_filedata *file_data;
1821 struct ctl_lun_create_params *params;
1824 off_t ps, pss, po, pos, us, uss, uo, uos;
1827 cbe_lun = &be_lun->cbe_lun;
1828 file_data = &be_lun->backend.file;
1829 params = &be_lun->params;
1831 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1832 be_lun->dispatch = ctl_be_block_dispatch_file;
1833 be_lun->lun_flush = ctl_be_block_flush_file;
1834 be_lun->get_lba_status = ctl_be_block_gls_file;
1835 be_lun->getattr = ctl_be_block_getattr_file;
1836 be_lun->unmap = NULL;
1837 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1839 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1841 snprintf(req->error_str, sizeof(req->error_str),
1842 "error calling VOP_GETATTR() for file %s",
1847 file_data->cred = crhold(curthread->td_ucred);
1848 if (params->lun_size_bytes != 0)
1849 be_lun->size_bytes = params->lun_size_bytes;
1851 be_lun->size_bytes = vattr.va_size;
1854 * For files we can use any logical block size. Prefer 512 bytes
1855 * for compatibility reasons. If file's vattr.va_blocksize
1856 * (preferred I/O block size) is bigger and multiple to chosen
1857 * logical block size -- report it as physical block size.
1859 if (params->blocksize_bytes != 0)
1860 cbe_lun->blocksize = params->blocksize_bytes;
1861 else if (cbe_lun->lun_type == T_CDROM)
1862 cbe_lun->blocksize = 2048;
1864 cbe_lun->blocksize = 512;
1865 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1866 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1867 0 : (be_lun->size_blocks - 1);
1869 us = ps = vattr.va_blocksize;
1872 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
1874 ctl_expand_number(value, &ps);
1875 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
1877 ctl_expand_number(value, &po);
1878 pss = ps / cbe_lun->blocksize;
1879 pos = po / cbe_lun->blocksize;
1880 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
1881 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
1882 cbe_lun->pblockexp = fls(pss) - 1;
1883 cbe_lun->pblockoff = (pss - pos) % pss;
1886 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
1888 ctl_expand_number(value, &us);
1889 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
1891 ctl_expand_number(value, &uo);
1892 uss = us / cbe_lun->blocksize;
1893 uos = uo / cbe_lun->blocksize;
1894 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
1895 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
1896 cbe_lun->ublockexp = fls(uss) - 1;
1897 cbe_lun->ublockoff = (uss - uos) % uss;
1901 * Sanity check. The media size has to be at least one
1904 if (be_lun->size_bytes < cbe_lun->blocksize) {
1906 snprintf(req->error_str, sizeof(req->error_str),
1907 "file %s size %ju < block size %u", be_lun->dev_path,
1908 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1911 cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1916 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1918 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1919 struct ctl_lun_create_params *params;
1923 int error, atomic, maxio, ref, unmap, tmp;
1924 off_t ps, pss, po, pos, us, uss, uo, uos, otmp;
1926 params = &be_lun->params;
1928 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1929 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1932 if (strcmp(csw->d_name, "zvol") == 0) {
1933 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1934 be_lun->get_lba_status = ctl_be_block_gls_zvol;
1935 atomic = maxio = CTLBLK_MAX_IO_SIZE;
1937 be_lun->dispatch = ctl_be_block_dispatch_dev;
1938 be_lun->get_lba_status = NULL;
1940 maxio = dev->si_iosize_max;
1943 if (maxio > CTLBLK_MAX_IO_SIZE)
1944 maxio = CTLBLK_MAX_IO_SIZE;
1946 be_lun->lun_flush = ctl_be_block_flush_dev;
1947 be_lun->getattr = ctl_be_block_getattr_dev;
1948 be_lun->unmap = ctl_be_block_unmap_dev;
1950 if (!csw->d_ioctl) {
1951 dev_relthread(dev, ref);
1952 snprintf(req->error_str, sizeof(req->error_str),
1953 "no d_ioctl for device %s!", be_lun->dev_path);
1957 error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1960 dev_relthread(dev, ref);
1961 snprintf(req->error_str, sizeof(req->error_str),
1962 "error %d returned for DIOCGSECTORSIZE ioctl "
1963 "on %s!", error, be_lun->dev_path);
1968 * If the user has asked for a blocksize that is greater than the
1969 * backing device's blocksize, we can do it only if the blocksize
1970 * the user is asking for is an even multiple of the underlying
1971 * device's blocksize.
1973 if ((params->blocksize_bytes != 0) &&
1974 (params->blocksize_bytes >= tmp)) {
1975 if (params->blocksize_bytes % tmp == 0) {
1976 cbe_lun->blocksize = params->blocksize_bytes;
1978 dev_relthread(dev, ref);
1979 snprintf(req->error_str, sizeof(req->error_str),
1980 "requested blocksize %u is not an even "
1981 "multiple of backing device blocksize %u",
1982 params->blocksize_bytes, tmp);
1985 } else if (params->blocksize_bytes != 0) {
1986 dev_relthread(dev, ref);
1987 snprintf(req->error_str, sizeof(req->error_str),
1988 "requested blocksize %u < backing device "
1989 "blocksize %u", params->blocksize_bytes, tmp);
1991 } else if (cbe_lun->lun_type == T_CDROM)
1992 cbe_lun->blocksize = MAX(tmp, 2048);
1994 cbe_lun->blocksize = tmp;
1996 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
1999 dev_relthread(dev, ref);
2000 snprintf(req->error_str, sizeof(req->error_str),
2001 "error %d returned for DIOCGMEDIASIZE "
2002 " ioctl on %s!", error,
2007 if (params->lun_size_bytes != 0) {
2008 if (params->lun_size_bytes > otmp) {
2009 dev_relthread(dev, ref);
2010 snprintf(req->error_str, sizeof(req->error_str),
2011 "requested LUN size %ju > backing device "
2013 (uintmax_t)params->lun_size_bytes,
2018 be_lun->size_bytes = params->lun_size_bytes;
2020 be_lun->size_bytes = otmp;
2021 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2022 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2023 0 : (be_lun->size_blocks - 1);
2025 error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
2030 error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
2038 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
2040 ctl_expand_number(value, &ps);
2041 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
2043 ctl_expand_number(value, &po);
2044 pss = ps / cbe_lun->blocksize;
2045 pos = po / cbe_lun->blocksize;
2046 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
2047 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
2048 cbe_lun->pblockexp = fls(pss) - 1;
2049 cbe_lun->pblockoff = (pss - pos) % pss;
2052 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
2054 ctl_expand_number(value, &us);
2055 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
2057 ctl_expand_number(value, &uo);
2058 uss = us / cbe_lun->blocksize;
2059 uos = uo / cbe_lun->blocksize;
2060 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
2061 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
2062 cbe_lun->ublockexp = fls(uss) - 1;
2063 cbe_lun->ublockoff = (uss - uos) % uss;
2066 cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
2067 cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2069 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2072 struct diocgattr_arg arg;
2074 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2075 arg.len = sizeof(arg.value.i);
2076 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
2078 unmap = (error == 0) ? arg.value.i : 0;
2080 value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2082 unmap = (strcmp(value, "on") == 0);
2084 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
2086 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2088 dev_relthread(dev, ref);
2093 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2095 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2100 if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
2102 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2105 switch (be_lun->dev_type) {
2106 case CTL_BE_BLOCK_DEV:
2108 case CTL_BE_BLOCK_FILE:
2109 if (be_lun->backend.file.cred != NULL) {
2110 crfree(be_lun->backend.file.cred);
2111 be_lun->backend.file.cred = NULL;
2114 case CTL_BE_BLOCK_NONE:
2117 panic("Unexpected backend type %d", be_lun->dev_type);
2120 be_lun->dev_type = CTL_BE_BLOCK_NONE;
2126 ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2128 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2129 struct nameidata nd;
2134 if (rootvnode == NULL) {
2135 snprintf(req->error_str, sizeof(req->error_str),
2136 "Root filesystem is not mounted");
2141 value = dnvlist_get_string(cbe_lun->options, "file", NULL);
2142 if (value == NULL) {
2143 snprintf(req->error_str, sizeof(req->error_str),
2144 "no file argument specified");
2147 free(be_lun->dev_path, M_CTLBLK);
2148 be_lun->dev_path = strdup(value, M_CTLBLK);
2151 value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
2152 if (value != NULL) {
2153 if (strcmp(value, "on") != 0)
2155 } else if (cbe_lun->lun_type == T_DIRECT)
2159 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2160 error = vn_open(&nd, &flags, 0, NULL);
2161 if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
2167 * This is the only reasonable guess we can make as far as
2168 * path if the user doesn't give us a fully qualified path.
2169 * If they want to specify a file, they need to specify the
2172 if (be_lun->dev_path[0] != '/') {
2175 asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2177 free(be_lun->dev_path, M_CTLBLK);
2178 be_lun->dev_path = dev_name;
2181 snprintf(req->error_str, sizeof(req->error_str),
2182 "error opening %s: %d", be_lun->dev_path, error);
2186 cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
2188 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2190 NDFREE(&nd, NDF_ONLY_PNBUF);
2191 be_lun->vn = nd.ni_vp;
2193 /* We only support disks and files. */
2194 if (vn_isdisk(be_lun->vn, &error)) {
2195 error = ctl_be_block_open_dev(be_lun, req);
2196 } else if (be_lun->vn->v_type == VREG) {
2197 error = ctl_be_block_open_file(be_lun, req);
2200 snprintf(req->error_str, sizeof(req->error_str),
2201 "%s is not a disk or plain file", be_lun->dev_path);
2203 VOP_UNLOCK(be_lun->vn, 0);
2206 ctl_be_block_close(be_lun);
2207 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2208 if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2209 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2210 value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
2211 if (value != NULL && strcmp(value, "on") == 0)
2212 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
2213 else if (value != NULL && strcmp(value, "read") == 0)
2214 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2215 else if (value != NULL && strcmp(value, "off") == 0)
2216 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2221 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2223 struct ctl_be_lun *cbe_lun;
2224 struct ctl_be_block_lun *be_lun;
2225 struct ctl_lun_create_params *params;
2226 char num_thread_str[16];
2229 int retval, num_threads;
2230 int tmp_num_threads;
2232 params = &req->reqdata.create;
2234 req->status = CTL_LUN_OK;
2236 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2237 cbe_lun = &be_lun->cbe_lun;
2238 cbe_lun->be_lun = be_lun;
2239 be_lun->params = req->reqdata.create;
2240 be_lun->softc = softc;
2241 STAILQ_INIT(&be_lun->input_queue);
2242 STAILQ_INIT(&be_lun->config_read_queue);
2243 STAILQ_INIT(&be_lun->config_write_queue);
2244 STAILQ_INIT(&be_lun->datamove_queue);
2245 sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
2246 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
2247 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
2248 cbe_lun->options = nvlist_clone(req->args_nvl);
2249 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
2250 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2251 if (be_lun->lun_zone == NULL) {
2252 snprintf(req->error_str, sizeof(req->error_str),
2253 "error allocating UMA zone");
2257 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2258 cbe_lun->lun_type = params->device_type;
2260 cbe_lun->lun_type = T_DIRECT;
2261 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2263 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2264 if (value != NULL) {
2265 if (strcmp(value, "primary") == 0)
2266 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2267 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2268 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2270 if (cbe_lun->lun_type == T_DIRECT ||
2271 cbe_lun->lun_type == T_CDROM) {
2272 be_lun->size_bytes = params->lun_size_bytes;
2273 if (params->blocksize_bytes != 0)
2274 cbe_lun->blocksize = params->blocksize_bytes;
2275 else if (cbe_lun->lun_type == T_CDROM)
2276 cbe_lun->blocksize = 2048;
2278 cbe_lun->blocksize = 512;
2279 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2280 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2281 0 : (be_lun->size_blocks - 1);
2283 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2284 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2285 retval = ctl_be_block_open(be_lun, req);
2288 req->status = CTL_LUN_WARNING;
2291 num_threads = cbb_num_threads;
2296 value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
2297 if (value != NULL) {
2298 tmp_num_threads = strtol(value, NULL, 0);
2301 * We don't let the user specify less than one
2302 * thread, but hope he's clueful enough not to
2303 * specify 1000 threads.
2305 if (tmp_num_threads < 1) {
2306 snprintf(req->error_str, sizeof(req->error_str),
2307 "invalid number of threads %s",
2311 num_threads = tmp_num_threads;
2314 if (be_lun->vn == NULL)
2315 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2316 /* Tell the user the blocksize we ended up using */
2317 params->lun_size_bytes = be_lun->size_bytes;
2318 params->blocksize_bytes = cbe_lun->blocksize;
2319 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2320 cbe_lun->req_lun_id = params->req_lun_id;
2321 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2323 cbe_lun->req_lun_id = 0;
2325 cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
2326 cbe_lun->lun_config_status = ctl_be_block_lun_config_status;
2327 cbe_lun->be = &ctl_be_block_driver;
2329 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2330 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2332 strncpy((char *)cbe_lun->serial_num, tmpstr,
2333 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2335 /* Tell the user what we used for a serial number */
2336 strncpy((char *)params->serial_num, tmpstr,
2337 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2339 strncpy((char *)cbe_lun->serial_num, params->serial_num,
2340 MIN(sizeof(cbe_lun->serial_num),
2341 sizeof(params->serial_num)));
2343 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2344 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
2345 strncpy((char *)cbe_lun->device_id, tmpstr,
2346 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2348 /* Tell the user what we used for a device ID */
2349 strncpy((char *)params->device_id, tmpstr,
2350 MIN(sizeof(params->device_id), sizeof(tmpstr)));
2352 strncpy((char *)cbe_lun->device_id, params->device_id,
2353 MIN(sizeof(cbe_lun->device_id),
2354 sizeof(params->device_id)));
2357 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2359 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2360 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2362 if (be_lun->io_taskqueue == NULL) {
2363 snprintf(req->error_str, sizeof(req->error_str),
2364 "unable to create taskqueue");
2369 * Note that we start the same number of threads by default for
2370 * both the file case and the block device case. For the file
2371 * case, we need multiple threads to allow concurrency, because the
2372 * vnode interface is designed to be a blocking interface. For the
2373 * block device case, ZFS zvols at least will block the caller's
2374 * context in many instances, and so we need multiple threads to
2375 * overcome that problem. Other block devices don't need as many
2376 * threads, but they shouldn't cause too many problems.
2378 * If the user wants to just have a single thread for a block
2379 * device, he can specify that when the LUN is created, or change
2380 * the tunable/sysctl to alter the default number of threads.
2382 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2383 /*num threads*/num_threads,
2386 "%s taskq", be_lun->lunname);
2391 be_lun->num_threads = num_threads;
2393 mtx_lock(&softc->lock);
2395 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2397 mtx_unlock(&softc->lock);
2399 retval = ctl_add_lun(&be_lun->cbe_lun);
2401 mtx_lock(&softc->lock);
2402 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2405 mtx_unlock(&softc->lock);
2406 snprintf(req->error_str, sizeof(req->error_str),
2407 "ctl_add_lun() returned error %d, see dmesg for "
2413 mtx_lock(&softc->lock);
2416 * Tell the config_status routine that we're waiting so it won't
2417 * clean up the LUN in the event of an error.
2419 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2421 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2422 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2423 if (retval == EINTR)
2426 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2428 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2429 snprintf(req->error_str, sizeof(req->error_str),
2430 "LUN configuration error, see dmesg for details");
2431 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2434 mtx_unlock(&softc->lock);
2437 params->req_lun_id = cbe_lun->lun_id;
2440 mtx_unlock(&softc->lock);
2442 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2444 DEVSTAT_ALL_SUPPORTED,
2446 | DEVSTAT_TYPE_IF_OTHER,
2447 DEVSTAT_PRIORITY_OTHER);
2452 req->status = CTL_LUN_ERROR;
2454 if (be_lun->io_taskqueue != NULL)
2455 taskqueue_free(be_lun->io_taskqueue);
2456 ctl_be_block_close(be_lun);
2457 if (be_lun->dev_path != NULL)
2458 free(be_lun->dev_path, M_CTLBLK);
2459 if (be_lun->lun_zone != NULL)
2460 uma_zdestroy(be_lun->lun_zone);
2461 nvlist_destroy(cbe_lun->options);
2462 mtx_destroy(&be_lun->queue_lock);
2463 mtx_destroy(&be_lun->io_lock);
2464 free(be_lun, M_CTLBLK);
2470 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2472 struct ctl_lun_rm_params *params;
2473 struct ctl_be_block_lun *be_lun;
2474 struct ctl_be_lun *cbe_lun;
2477 params = &req->reqdata.rm;
2479 mtx_lock(&softc->lock);
2480 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2481 if (be_lun->cbe_lun.lun_id == params->lun_id)
2484 mtx_unlock(&softc->lock);
2485 if (be_lun == NULL) {
2486 snprintf(req->error_str, sizeof(req->error_str),
2487 "LUN %u is not managed by the block backend",
2491 cbe_lun = &be_lun->cbe_lun;
2493 retval = ctl_disable_lun(cbe_lun);
2495 snprintf(req->error_str, sizeof(req->error_str),
2496 "error %d returned from ctl_disable_lun() for "
2497 "LUN %d", retval, params->lun_id);
2501 if (be_lun->vn != NULL) {
2502 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2503 ctl_lun_no_media(cbe_lun);
2504 taskqueue_drain_all(be_lun->io_taskqueue);
2505 ctl_be_block_close(be_lun);
2508 retval = ctl_invalidate_lun(cbe_lun);
2510 snprintf(req->error_str, sizeof(req->error_str),
2511 "error %d returned from ctl_invalidate_lun() for "
2512 "LUN %d", retval, params->lun_id);
2516 mtx_lock(&softc->lock);
2517 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2518 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2519 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2520 if (retval == EINTR)
2523 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2525 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2526 snprintf(req->error_str, sizeof(req->error_str),
2527 "interrupted waiting for LUN to be freed");
2528 mtx_unlock(&softc->lock);
2532 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2535 mtx_unlock(&softc->lock);
2537 taskqueue_drain_all(be_lun->io_taskqueue);
2538 taskqueue_free(be_lun->io_taskqueue);
2540 if (be_lun->disk_stats != NULL)
2541 devstat_remove_entry(be_lun->disk_stats);
2543 uma_zdestroy(be_lun->lun_zone);
2545 nvlist_destroy(cbe_lun->options);
2546 free(be_lun->dev_path, M_CTLBLK);
2547 mtx_destroy(&be_lun->queue_lock);
2548 mtx_destroy(&be_lun->io_lock);
2549 free(be_lun, M_CTLBLK);
2551 req->status = CTL_LUN_OK;
2555 req->status = CTL_LUN_ERROR;
2560 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2562 struct ctl_lun_modify_params *params;
2563 struct ctl_be_block_lun *be_lun;
2564 struct ctl_be_lun *cbe_lun;
2569 params = &req->reqdata.modify;
2571 mtx_lock(&softc->lock);
2572 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2573 if (be_lun->cbe_lun.lun_id == params->lun_id)
2576 mtx_unlock(&softc->lock);
2577 if (be_lun == NULL) {
2578 snprintf(req->error_str, sizeof(req->error_str),
2579 "LUN %u is not managed by the block backend",
2583 cbe_lun = &be_lun->cbe_lun;
2585 if (params->lun_size_bytes != 0)
2586 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2588 nvlist_destroy(cbe_lun->options);
2589 cbe_lun->options = nvlist_clone(req->args_nvl);
2591 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
2592 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2593 if (value != NULL) {
2594 if (strcmp(value, "primary") == 0)
2595 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2597 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2598 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2599 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2601 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2602 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
2603 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
2604 ctl_lun_primary(cbe_lun);
2606 ctl_lun_secondary(cbe_lun);
2609 oldsize = be_lun->size_blocks;
2610 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2611 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2612 if (be_lun->vn == NULL)
2613 error = ctl_be_block_open(be_lun, req);
2614 else if (vn_isdisk(be_lun->vn, &error))
2615 error = ctl_be_block_open_dev(be_lun, req);
2616 else if (be_lun->vn->v_type == VREG) {
2617 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2618 error = ctl_be_block_open_file(be_lun, req);
2619 VOP_UNLOCK(be_lun->vn, 0);
2622 if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
2623 be_lun->vn != NULL) {
2624 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2625 ctl_lun_has_media(cbe_lun);
2626 } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2627 be_lun->vn == NULL) {
2628 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2629 ctl_lun_no_media(cbe_lun);
2631 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2633 if (be_lun->vn != NULL) {
2634 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2635 ctl_lun_no_media(cbe_lun);
2636 taskqueue_drain_all(be_lun->io_taskqueue);
2637 error = ctl_be_block_close(be_lun);
2641 if (be_lun->size_blocks != oldsize)
2642 ctl_lun_capacity_changed(cbe_lun);
2644 /* Tell the user the exact size we ended up using */
2645 params->lun_size_bytes = be_lun->size_bytes;
2647 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2651 req->status = CTL_LUN_ERROR;
2656 ctl_be_block_lun_shutdown(void *be_lun)
2658 struct ctl_be_block_lun *lun = be_lun;
2659 struct ctl_be_block_softc *softc = lun->softc;
2661 mtx_lock(&softc->lock);
2662 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2663 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2665 mtx_unlock(&softc->lock);
2669 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2671 struct ctl_be_block_lun *lun;
2672 struct ctl_be_block_softc *softc;
2674 lun = (struct ctl_be_block_lun *)be_lun;
2677 if (status == CTL_LUN_CONFIG_OK) {
2678 mtx_lock(&softc->lock);
2679 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2680 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2682 mtx_unlock(&softc->lock);
2685 * We successfully added the LUN, attempt to enable it.
2687 if (ctl_enable_lun(&lun->cbe_lun) != 0) {
2688 printf("%s: ctl_enable_lun() failed!\n", __func__);
2689 if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
2690 printf("%s: ctl_invalidate_lun() failed!\n",
2699 mtx_lock(&softc->lock);
2700 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2701 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2703 mtx_unlock(&softc->lock);
2708 ctl_be_block_config_write(union ctl_io *io)
2710 struct ctl_be_block_lun *be_lun;
2711 struct ctl_be_lun *cbe_lun;
2714 DPRINTF("entered\n");
2716 cbe_lun = CTL_BACKEND_LUN(io);
2717 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2720 switch (io->scsiio.cdb[0]) {
2721 case SYNCHRONIZE_CACHE:
2722 case SYNCHRONIZE_CACHE_16:
2727 * The upper level CTL code will filter out any CDBs with
2728 * the immediate bit set and return the proper error.
2730 * We don't really need to worry about what LBA range the
2731 * user asked to be synced out. When they issue a sync
2732 * cache command, we'll sync out the whole thing.
2734 mtx_lock(&be_lun->queue_lock);
2735 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2737 mtx_unlock(&be_lun->queue_lock);
2738 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2740 case START_STOP_UNIT: {
2741 struct scsi_start_stop_unit *cdb;
2742 struct ctl_lun_req req;
2744 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2745 if ((cdb->how & SSS_PC_MASK) != 0) {
2746 ctl_set_success(&io->scsiio);
2747 ctl_config_write_done(io);
2750 if (cdb->how & SSS_START) {
2751 if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2752 retval = ctl_be_block_open(be_lun, &req);
2753 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2755 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2756 ctl_lun_has_media(cbe_lun);
2758 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2759 ctl_lun_no_media(cbe_lun);
2762 ctl_start_lun(cbe_lun);
2764 ctl_stop_lun(cbe_lun);
2765 if (cdb->how & SSS_LOEJ) {
2766 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2767 cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2768 ctl_lun_ejected(cbe_lun);
2769 if (be_lun->vn != NULL)
2770 ctl_be_block_close(be_lun);
2774 ctl_set_success(&io->scsiio);
2775 ctl_config_write_done(io);
2779 ctl_set_success(&io->scsiio);
2780 ctl_config_write_done(io);
2783 ctl_set_invalid_opcode(&io->scsiio);
2784 ctl_config_write_done(io);
2785 retval = CTL_RETVAL_COMPLETE;
2793 ctl_be_block_config_read(union ctl_io *io)
2795 struct ctl_be_block_lun *be_lun;
2796 struct ctl_be_lun *cbe_lun;
2799 DPRINTF("entered\n");
2801 cbe_lun = CTL_BACKEND_LUN(io);
2802 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2804 switch (io->scsiio.cdb[0]) {
2805 case SERVICE_ACTION_IN:
2806 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2807 mtx_lock(&be_lun->queue_lock);
2808 STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2809 &io->io_hdr, links);
2810 mtx_unlock(&be_lun->queue_lock);
2811 taskqueue_enqueue(be_lun->io_taskqueue,
2813 retval = CTL_RETVAL_QUEUED;
2816 ctl_set_invalid_field(&io->scsiio,
2822 ctl_config_read_done(io);
2823 retval = CTL_RETVAL_COMPLETE;
2826 ctl_set_invalid_opcode(&io->scsiio);
2827 ctl_config_read_done(io);
2828 retval = CTL_RETVAL_COMPLETE;
2836 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2838 struct ctl_be_block_lun *lun;
2841 lun = (struct ctl_be_block_lun *)be_lun;
2843 retval = sbuf_printf(sb, "\t<num_threads>");
2846 retval = sbuf_printf(sb, "%d", lun->num_threads);
2849 retval = sbuf_printf(sb, "</num_threads>\n");
2856 ctl_be_block_lun_attr(void *be_lun, const char *attrname)
2858 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2860 if (lun->getattr == NULL)
2861 return (UINT64_MAX);
2862 return (lun->getattr(lun, attrname));
2866 ctl_be_block_init(void)
2868 struct ctl_be_block_softc *softc = &backend_block_softc;
2870 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2871 softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2872 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2873 STAILQ_INIT(&softc->lun_list);
2879 ctl_be_block_shutdown(void)
2881 struct ctl_be_block_softc *softc = &backend_block_softc;
2882 struct ctl_be_block_lun *lun, *next_lun;
2884 mtx_lock(&softc->lock);
2885 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) {
2887 * Drop our lock here. Since ctl_invalidate_lun() can call
2888 * back into us, this could potentially lead to a recursive
2889 * lock of the same mutex, which would cause a hang.
2891 mtx_unlock(&softc->lock);
2892 ctl_disable_lun(&lun->cbe_lun);
2893 ctl_invalidate_lun(&lun->cbe_lun);
2894 mtx_lock(&softc->lock);
2896 mtx_unlock(&softc->lock);
2898 uma_zdestroy(softc->beio_zone);
2899 mtx_destroy(&softc->lock);