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>
85 #include <geom/geom.h>
88 #include <cam/scsi/scsi_all.h>
89 #include <cam/scsi/scsi_da.h>
90 #include <cam/ctl/ctl_io.h>
91 #include <cam/ctl/ctl.h>
92 #include <cam/ctl/ctl_backend.h>
93 #include <cam/ctl/ctl_ioctl.h>
94 #include <cam/ctl/ctl_ha.h>
95 #include <cam/ctl/ctl_scsi_all.h>
96 #include <cam/ctl/ctl_private.h>
97 #include <cam/ctl/ctl_error.h>
100 * The idea here is that we'll allocate enough S/G space to hold a 1MB
101 * I/O. If we get an I/O larger than that, we'll split it.
103 #define CTLBLK_HALF_IO_SIZE (512 * 1024)
104 #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2)
105 #define CTLBLK_MAX_SEG MAXPHYS
106 #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
107 #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2)
110 #define DPRINTF(fmt, args...) \
111 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
113 #define DPRINTF(fmt, args...) do {} while(0)
117 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
119 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
121 SDT_PROVIDER_DEFINE(cbb);
124 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01,
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_be_lun cbe_lun; /* Must be first element. */
156 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;
166 uint64_t size_blocks;
168 struct ctl_be_block_softc *softc;
169 struct devstat *disk_stats;
170 ctl_be_block_lun_flags flags;
171 SLIST_ENTRY(ctl_be_block_lun) links;
172 struct taskqueue *io_taskqueue;
175 STAILQ_HEAD(, ctl_io_hdr) input_queue;
176 STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
177 STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
178 STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
179 struct mtx_padalign io_lock;
180 struct mtx_padalign queue_lock;
184 * Overall softc structure for the block backend module.
186 struct ctl_be_block_softc {
187 struct sx modify_lock;
190 SLIST_HEAD(, ctl_be_block_lun) lun_list;
191 uma_zone_t beio_zone;
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];
212 uint64_t first_error_offset;
213 struct bintime ds_t0;
214 devstat_tag_type ds_tag_type;
215 devstat_trans_flags ds_trans_type;
219 struct ctl_be_block_softc *softc;
220 struct ctl_be_block_lun *lun;
221 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
224 extern struct ctl_softc *control_softc;
226 static int cbb_num_threads = 14;
227 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
228 "CAM Target Layer Block Backend");
229 SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
230 &cbb_num_threads, 0, "Number of threads per backing file");
232 static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
233 static void ctl_free_beio(struct ctl_be_block_io *beio);
234 static void ctl_complete_beio(struct ctl_be_block_io *beio);
235 static int ctl_be_block_move_done(union ctl_io *io);
236 static void ctl_be_block_biodone(struct bio *bio);
237 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
238 struct ctl_be_block_io *beio);
239 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
240 struct ctl_be_block_io *beio);
241 static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
242 struct ctl_be_block_io *beio);
243 static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
244 const char *attrname);
245 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
246 struct ctl_be_block_io *beio);
247 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
248 struct ctl_be_block_io *beio);
249 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
250 struct ctl_be_block_io *beio);
251 static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
252 const char *attrname);
253 static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
255 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
257 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
259 static void ctl_be_block_worker(void *context, int pending);
260 static int ctl_be_block_submit(union ctl_io *io);
261 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
262 int flag, struct thread *td);
263 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
264 struct ctl_lun_req *req);
265 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
266 struct ctl_lun_req *req);
267 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
268 static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
269 struct ctl_lun_req *req);
270 static int ctl_be_block_create(struct ctl_be_block_softc *softc,
271 struct ctl_lun_req *req);
272 static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
273 struct ctl_lun_req *req);
274 static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
275 struct ctl_lun_req *req);
276 static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun);
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(struct ctl_be_lun *cbe_lun, struct sbuf *sb);
280 static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_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, "ctlblock", "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);
314 ctl_real_free_beio(struct ctl_be_block_io *beio)
316 struct ctl_be_block_softc *softc = beio->softc;
319 for (i = 0; i < beio->num_segs; i++) {
320 uma_zfree(softc->buf_zone, beio->sg_segs[i].addr);
322 /* For compare we had two equal S/G lists. */
323 if (beio->two_sglists) {
324 uma_zfree(softc->buf_zone,
325 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
329 uma_zfree(softc->beio_zone, beio);
333 ctl_refcnt_beio(void *arg, int diff)
335 struct ctl_be_block_io *beio = arg;
337 if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0)
338 ctl_real_free_beio(beio);
342 ctl_free_beio(struct ctl_be_block_io *beio)
345 ctl_refcnt_beio(beio, -1);
349 ctl_complete_beio(struct ctl_be_block_io *beio)
351 union ctl_io *io = beio->io;
353 if (beio->beio_cont != NULL) {
354 beio->beio_cont(beio);
357 ctl_data_submit_done(io);
362 cmp(uint8_t *a, uint8_t *b, size_t size)
366 for (i = 0; i < size; i++) {
374 ctl_be_block_compare(union ctl_io *io)
376 struct ctl_be_block_io *beio;
381 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
383 for (i = 0; i < beio->num_segs; i++) {
384 res = cmp(beio->sg_segs[i].addr,
385 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
386 beio->sg_segs[i].len);
388 if (res < beio->sg_segs[i].len)
391 if (i < beio->num_segs) {
392 scsi_u64to8b(off, info);
393 ctl_set_sense(&io->scsiio, /*current_error*/ 1,
394 /*sense_key*/ SSD_KEY_MISCOMPARE,
395 /*asc*/ 0x1D, /*ascq*/ 0x00,
396 /*type*/ SSD_ELEM_INFO,
397 /*size*/ sizeof(info), /*data*/ &info,
398 /*type*/ SSD_ELEM_NONE);
400 ctl_set_success(&io->scsiio);
404 ctl_be_block_move_done(union ctl_io *io)
406 struct ctl_be_block_io *beio;
407 struct ctl_be_block_lun *be_lun;
408 struct ctl_lba_len_flags *lbalen;
410 struct bintime cur_bt;
413 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
416 DPRINTF("entered\n");
419 getbinuptime(&cur_bt);
420 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
421 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
423 io->io_hdr.num_dmas++;
424 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
427 * We set status at this point for read commands, and write
428 * commands with errors.
430 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
432 } else if ((io->io_hdr.port_status != 0) &&
433 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
434 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
435 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
436 /*retry_count*/ io->io_hdr.port_status);
437 } else if (io->scsiio.kern_data_resid != 0 &&
438 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
439 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
440 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
441 ctl_set_invalid_field_ciu(&io->scsiio);
442 } else if ((io->io_hdr.port_status == 0) &&
443 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
444 lbalen = ARGS(beio->io);
445 if (lbalen->flags & CTL_LLF_READ) {
446 ctl_set_success(&io->scsiio);
447 } else if (lbalen->flags & CTL_LLF_COMPARE) {
448 /* We have two data blocks ready for comparison. */
449 ctl_be_block_compare(io);
454 * If this is a read, or a write with errors, it is done.
456 if ((beio->bio_cmd == BIO_READ)
457 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
458 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
459 ctl_complete_beio(beio);
464 * At this point, we have a write and the DMA completed
465 * successfully. We now have to queue it to the task queue to
466 * execute the backend I/O. That is because we do blocking
467 * memory allocations, and in the file backing case, blocking I/O.
468 * This move done routine is generally called in the SIM's
469 * interrupt context, and therefore we cannot block.
471 mtx_lock(&be_lun->queue_lock);
472 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
473 mtx_unlock(&be_lun->queue_lock);
474 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
480 ctl_be_block_biodone(struct bio *bio)
482 struct ctl_be_block_io *beio;
483 struct ctl_be_block_lun *be_lun;
487 beio = bio->bio_caller1;
491 DPRINTF("entered\n");
493 error = bio->bio_error;
494 mtx_lock(&be_lun->io_lock);
496 (beio->first_error == 0 ||
497 bio->bio_offset < beio->first_error_offset)) {
498 beio->first_error = error;
499 beio->first_error_offset = bio->bio_offset;
502 beio->num_bios_done++;
505 * XXX KDM will this cause WITNESS to complain? Holding a lock
506 * during the free might cause it to complain.
511 * If the send complete bit isn't set, or we aren't the last I/O to
512 * complete, then we're done.
514 if ((beio->send_complete == 0)
515 || (beio->num_bios_done < beio->num_bios_sent)) {
516 mtx_unlock(&be_lun->io_lock);
521 * At this point, we've verified that we are the last I/O to
522 * complete, so it's safe to drop the lock.
524 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
525 beio->ds_tag_type, beio->ds_trans_type,
526 /*now*/ NULL, /*then*/&beio->ds_t0);
527 mtx_unlock(&be_lun->io_lock);
530 * If there are any errors from the backing device, we fail the
531 * entire I/O with a medium error.
533 error = beio->first_error;
535 if (error == EOPNOTSUPP) {
536 ctl_set_invalid_opcode(&io->scsiio);
537 } else if (error == ENOSPC || error == EDQUOT) {
538 ctl_set_space_alloc_fail(&io->scsiio);
539 } else if (error == EROFS || error == EACCES) {
540 ctl_set_hw_write_protected(&io->scsiio);
541 } else if (beio->bio_cmd == BIO_FLUSH) {
542 /* XXX KDM is there is a better error here? */
543 ctl_set_internal_failure(&io->scsiio,
545 /*retry_count*/ 0xbad2);
547 ctl_set_medium_error(&io->scsiio,
548 beio->bio_cmd == BIO_READ);
550 ctl_complete_beio(beio);
555 * If this is a write, a flush, a delete or verify, we're all done.
556 * If this is a read, we can now send the data to the user.
558 if ((beio->bio_cmd == BIO_WRITE)
559 || (beio->bio_cmd == BIO_FLUSH)
560 || (beio->bio_cmd == BIO_DELETE)
561 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
562 ctl_set_success(&io->scsiio);
563 ctl_complete_beio(beio);
565 if ((ARGS(io)->flags & CTL_LLF_READ) &&
566 beio->beio_cont == NULL) {
567 ctl_set_success(&io->scsiio);
571 getbinuptime(&io->io_hdr.dma_start_bt);
578 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
579 struct ctl_be_block_io *beio)
581 union ctl_io *io = beio->io;
582 struct mount *mountpoint;
583 int error, lock_flags;
585 DPRINTF("entered\n");
587 binuptime(&beio->ds_t0);
588 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
590 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
592 if (MNT_SHARED_WRITES(mountpoint) ||
593 ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
594 lock_flags = LK_SHARED;
596 lock_flags = LK_EXCLUSIVE;
597 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
598 error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
600 VOP_UNLOCK(be_lun->vn);
602 vn_finished_write(mountpoint);
604 mtx_lock(&be_lun->io_lock);
605 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
606 beio->ds_tag_type, beio->ds_trans_type,
607 /*now*/ NULL, /*then*/&beio->ds_t0);
608 mtx_unlock(&be_lun->io_lock);
611 ctl_set_success(&io->scsiio);
613 /* XXX KDM is there is a better error here? */
614 ctl_set_internal_failure(&io->scsiio,
616 /*retry_count*/ 0xbad1);
619 ctl_complete_beio(beio);
622 SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
623 SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
624 SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
625 SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
628 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
629 struct ctl_be_block_io *beio)
631 struct ctl_be_block_filedata *file_data;
634 struct iovec *xiovec;
638 DPRINTF("entered\n");
640 file_data = &be_lun->backend.file;
643 if (ARGS(io)->flags & CTL_LLF_DPO)
645 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
648 bzero(&xuio, sizeof(xuio));
649 if (beio->bio_cmd == BIO_READ) {
650 SDT_PROBE0(cbb, , read, file_start);
651 xuio.uio_rw = UIO_READ;
653 SDT_PROBE0(cbb, , write, file_start);
654 xuio.uio_rw = UIO_WRITE;
656 xuio.uio_offset = beio->io_offset;
657 xuio.uio_resid = beio->io_len;
658 xuio.uio_segflg = UIO_SYSSPACE;
659 xuio.uio_iov = beio->xiovecs;
660 xuio.uio_iovcnt = beio->num_segs;
661 xuio.uio_td = curthread;
663 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
664 xiovec->iov_base = beio->sg_segs[i].addr;
665 xiovec->iov_len = beio->sg_segs[i].len;
668 binuptime(&beio->ds_t0);
669 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
671 if (beio->bio_cmd == BIO_READ) {
672 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
675 * UFS pays attention to IO_DIRECT for reads. If the
676 * DIRECTIO option is configured into the kernel, it calls
677 * ffs_rawread(). But that only works for single-segment
678 * uios with user space addresses. In our case, with a
679 * kernel uio, it still reads into the buffer cache, but it
680 * will just try to release the buffer from the cache later
683 * ZFS does not pay attention to IO_DIRECT for reads.
685 * UFS does not pay attention to IO_SYNC for reads.
687 * ZFS pays attention to IO_SYNC (which translates into the
688 * Solaris define FRSYNC for zfs_read()) for reads. It
689 * attempts to sync the file before reading.
691 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
693 VOP_UNLOCK(be_lun->vn);
694 SDT_PROBE0(cbb, , read, file_done);
695 if (error == 0 && xuio.uio_resid > 0) {
697 * If we red less then requested (EOF), then
698 * we should clean the rest of the buffer.
700 s = beio->io_len - xuio.uio_resid;
701 for (i = 0; i < beio->num_segs; i++) {
702 if (s >= beio->sg_segs[i].len) {
703 s -= beio->sg_segs[i].len;
706 bzero((uint8_t *)beio->sg_segs[i].addr + s,
707 beio->sg_segs[i].len - s);
712 struct mount *mountpoint;
715 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
717 if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL)
718 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
719 lock_flags = LK_SHARED;
721 lock_flags = LK_EXCLUSIVE;
722 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
725 * UFS pays attention to IO_DIRECT for writes. The write
726 * is done asynchronously. (Normally the write would just
727 * get put into cache.
729 * UFS pays attention to IO_SYNC for writes. It will
730 * attempt to write the buffer out synchronously if that
733 * ZFS does not pay attention to IO_DIRECT for writes.
735 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
736 * for writes. It will flush the transaction from the
737 * cache before returning.
739 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
740 VOP_UNLOCK(be_lun->vn);
742 vn_finished_write(mountpoint);
743 SDT_PROBE0(cbb, , write, file_done);
746 mtx_lock(&be_lun->io_lock);
747 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
748 beio->ds_tag_type, beio->ds_trans_type,
749 /*now*/ NULL, /*then*/&beio->ds_t0);
750 mtx_unlock(&be_lun->io_lock);
753 * If we got an error, set the sense data to "MEDIUM ERROR" and
754 * return the I/O to the user.
757 if (error == ENOSPC || error == EDQUOT) {
758 ctl_set_space_alloc_fail(&io->scsiio);
759 } else if (error == EROFS || error == EACCES) {
760 ctl_set_hw_write_protected(&io->scsiio);
762 ctl_set_medium_error(&io->scsiio,
763 beio->bio_cmd == BIO_READ);
765 ctl_complete_beio(beio);
770 * If this is a write or a verify, we're all done.
771 * If this is a read, we can now send the data to the user.
773 if ((beio->bio_cmd == BIO_WRITE) ||
774 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
775 ctl_set_success(&io->scsiio);
776 ctl_complete_beio(beio);
778 if ((ARGS(io)->flags & CTL_LLF_READ) &&
779 beio->beio_cont == NULL) {
780 ctl_set_success(&io->scsiio);
784 getbinuptime(&io->io_hdr.dma_start_bt);
791 ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
792 struct ctl_be_block_io *beio)
794 union ctl_io *io = beio->io;
795 struct ctl_lba_len_flags *lbalen = ARGS(io);
796 struct scsi_get_lba_status_data *data;
800 DPRINTF("entered\n");
802 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
803 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
804 error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
805 0, curthread->td_ucred, curthread);
806 if (error == 0 && off > roff)
807 status = 0; /* mapped up to off */
809 error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
810 0, curthread->td_ucred, curthread);
811 if (error == 0 && off > roff)
812 status = 1; /* deallocated up to off */
814 status = 0; /* unknown up to the end */
815 off = be_lun->size_bytes;
818 VOP_UNLOCK(be_lun->vn);
820 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
821 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
822 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
823 lbalen->lba), data->descr[0].length);
824 data->descr[0].status = status;
826 ctl_complete_beio(beio);
830 ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
833 struct statfs statfs;
838 if (be_lun->vn == NULL)
840 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
841 if (strcmp(attrname, "blocksused") == 0) {
842 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
844 val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
846 if (strcmp(attrname, "blocksavail") == 0 &&
847 !VN_IS_DOOMED(be_lun->vn)) {
848 error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
850 val = statfs.f_bavail * statfs.f_bsize /
851 be_lun->cbe_lun.blocksize;
853 VOP_UNLOCK(be_lun->vn);
858 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
859 struct ctl_be_block_io *beio)
865 struct iovec *xiovec;
866 int error, flags, i, ref;
868 DPRINTF("entered\n");
872 if (ARGS(io)->flags & CTL_LLF_DPO)
874 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
877 bzero(&xuio, sizeof(xuio));
878 if (beio->bio_cmd == BIO_READ) {
879 SDT_PROBE0(cbb, , read, file_start);
880 xuio.uio_rw = UIO_READ;
882 SDT_PROBE0(cbb, , write, file_start);
883 xuio.uio_rw = UIO_WRITE;
885 xuio.uio_offset = beio->io_offset;
886 xuio.uio_resid = beio->io_len;
887 xuio.uio_segflg = UIO_SYSSPACE;
888 xuio.uio_iov = beio->xiovecs;
889 xuio.uio_iovcnt = beio->num_segs;
890 xuio.uio_td = curthread;
892 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
893 xiovec->iov_base = beio->sg_segs[i].addr;
894 xiovec->iov_len = beio->sg_segs[i].len;
897 binuptime(&beio->ds_t0);
898 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
900 csw = devvn_refthread(be_lun->vn, &dev, &ref);
902 if (beio->bio_cmd == BIO_READ)
903 error = csw->d_read(dev, &xuio, flags);
905 error = csw->d_write(dev, &xuio, flags);
906 dev_relthread(dev, ref);
910 if (beio->bio_cmd == BIO_READ)
911 SDT_PROBE0(cbb, , read, file_done);
913 SDT_PROBE0(cbb, , write, file_done);
915 mtx_lock(&be_lun->io_lock);
916 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
917 beio->ds_tag_type, beio->ds_trans_type,
918 /*now*/ NULL, /*then*/&beio->ds_t0);
919 mtx_unlock(&be_lun->io_lock);
922 * If we got an error, set the sense data to "MEDIUM ERROR" and
923 * return the I/O to the user.
926 if (error == ENOSPC || error == EDQUOT) {
927 ctl_set_space_alloc_fail(&io->scsiio);
928 } else if (error == EROFS || error == EACCES) {
929 ctl_set_hw_write_protected(&io->scsiio);
931 ctl_set_medium_error(&io->scsiio,
932 beio->bio_cmd == BIO_READ);
934 ctl_complete_beio(beio);
939 * If this is a write or a verify, we're all done.
940 * If this is a read, we can now send the data to the user.
942 if ((beio->bio_cmd == BIO_WRITE) ||
943 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
944 ctl_set_success(&io->scsiio);
945 ctl_complete_beio(beio);
947 if ((ARGS(io)->flags & CTL_LLF_READ) &&
948 beio->beio_cont == NULL) {
949 ctl_set_success(&io->scsiio);
953 getbinuptime(&io->io_hdr.dma_start_bt);
960 ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
961 struct ctl_be_block_io *beio)
963 union ctl_io *io = beio->io;
966 struct ctl_lba_len_flags *lbalen = ARGS(io);
967 struct scsi_get_lba_status_data *data;
969 int error, ref, status;
971 DPRINTF("entered\n");
973 csw = devvn_refthread(be_lun->vn, &dev, &ref);
975 status = 0; /* unknown up to the end */
976 off = be_lun->size_bytes;
979 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
980 error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
982 if (error == 0 && off > roff)
983 status = 0; /* mapped up to off */
985 error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
987 if (error == 0 && off > roff)
988 status = 1; /* deallocated up to off */
990 status = 0; /* unknown up to the end */
991 off = be_lun->size_bytes;
994 dev_relthread(dev, ref);
997 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
998 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
999 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
1000 lbalen->lba), data->descr[0].length);
1001 data->descr[0].status = status;
1003 ctl_complete_beio(beio);
1007 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1008 struct ctl_be_block_io *beio)
1015 DPRINTF("entered\n");
1017 /* This can't fail, it's a blocking allocation. */
1018 bio = g_alloc_bio();
1020 bio->bio_cmd = BIO_FLUSH;
1021 bio->bio_offset = 0;
1023 bio->bio_done = ctl_be_block_biodone;
1024 bio->bio_caller1 = beio;
1025 bio->bio_pblkno = 0;
1028 * We don't need to acquire the LUN lock here, because we are only
1029 * sending one bio, and so there is no other context to synchronize
1032 beio->num_bios_sent = 1;
1033 beio->send_complete = 1;
1035 binuptime(&beio->ds_t0);
1036 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1038 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1041 csw->d_strategy(bio);
1042 dev_relthread(dev, ref);
1044 bio->bio_error = ENXIO;
1045 ctl_be_block_biodone(bio);
1050 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1051 struct ctl_be_block_io *beio,
1052 uint64_t off, uint64_t len, int last)
1060 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1061 maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1063 bio = g_alloc_bio();
1064 bio->bio_cmd = BIO_DELETE;
1066 bio->bio_offset = off;
1067 bio->bio_length = MIN(len, maxlen);
1069 bio->bio_done = ctl_be_block_biodone;
1070 bio->bio_caller1 = beio;
1071 bio->bio_pblkno = off / be_lun->cbe_lun.blocksize;
1073 off += bio->bio_length;
1074 len -= bio->bio_length;
1076 mtx_lock(&be_lun->io_lock);
1077 beio->num_bios_sent++;
1078 if (last && len == 0)
1079 beio->send_complete = 1;
1080 mtx_unlock(&be_lun->io_lock);
1083 csw->d_strategy(bio);
1085 bio->bio_error = ENXIO;
1086 ctl_be_block_biodone(bio);
1090 dev_relthread(dev, ref);
1094 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1095 struct ctl_be_block_io *beio)
1098 struct ctl_ptr_len_flags *ptrlen;
1099 struct scsi_unmap_desc *buf, *end;
1104 DPRINTF("entered\n");
1106 binuptime(&beio->ds_t0);
1107 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1109 if (beio->io_offset == -1) {
1111 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1112 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1113 end = buf + ptrlen->len / sizeof(*buf);
1114 for (; buf < end; buf++) {
1115 len = (uint64_t)scsi_4btoul(buf->length) *
1116 be_lun->cbe_lun.blocksize;
1117 beio->io_len += len;
1118 ctl_be_block_unmap_dev_range(be_lun, beio,
1119 scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
1120 len, (end - buf < 2) ? TRUE : FALSE);
1123 ctl_be_block_unmap_dev_range(be_lun, beio,
1124 beio->io_offset, beio->io_len, TRUE);
1128 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1129 struct ctl_be_block_io *beio)
1131 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1136 int i, max_iosize, ref;
1138 DPRINTF("entered\n");
1139 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1142 * We have to limit our I/O size to the maximum supported by the
1143 * backend device. Hopefully it is MAXPHYS. If the driver doesn't
1144 * set it properly, use DFLTPHYS.
1147 max_iosize = dev->si_iosize_max;
1148 if (max_iosize < PAGE_SIZE)
1149 max_iosize = DFLTPHYS;
1151 max_iosize = DFLTPHYS;
1153 cur_offset = beio->io_offset;
1154 for (i = 0; i < beio->num_segs; i++) {
1158 cur_size = beio->sg_segs[i].len;
1159 cur_ptr = beio->sg_segs[i].addr;
1161 while (cur_size > 0) {
1162 /* This can't fail, it's a blocking allocation. */
1163 bio = g_alloc_bio();
1165 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1167 bio->bio_cmd = beio->bio_cmd;
1169 bio->bio_caller1 = beio;
1170 bio->bio_length = min(cur_size, max_iosize);
1171 bio->bio_offset = cur_offset;
1172 bio->bio_data = cur_ptr;
1173 bio->bio_done = ctl_be_block_biodone;
1174 bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1176 cur_offset += bio->bio_length;
1177 cur_ptr += bio->bio_length;
1178 cur_size -= bio->bio_length;
1180 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1181 beio->num_bios_sent++;
1184 beio->send_complete = 1;
1185 binuptime(&beio->ds_t0);
1186 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1189 * Fire off all allocated requests!
1191 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1192 TAILQ_REMOVE(&queue, bio, bio_queue);
1194 csw->d_strategy(bio);
1196 bio->bio_error = ENXIO;
1197 ctl_be_block_biodone(bio);
1201 dev_relthread(dev, ref);
1205 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1207 struct diocgattr_arg arg;
1212 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1214 return (UINT64_MAX);
1215 strlcpy(arg.name, attrname, sizeof(arg.name));
1216 arg.len = sizeof(arg.value.off);
1218 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
1222 dev_relthread(dev, ref);
1224 return (UINT64_MAX);
1225 return (arg.value.off);
1229 ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1232 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1233 struct ctl_be_block_io *beio;
1234 struct ctl_lba_len_flags *lbalen;
1236 DPRINTF("entered\n");
1237 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1238 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1240 beio->io_len = lbalen->len * cbe_lun->blocksize;
1241 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1242 beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
1243 beio->bio_cmd = BIO_FLUSH;
1244 beio->ds_trans_type = DEVSTAT_NO_DATA;
1246 be_lun->lun_flush(be_lun, beio);
1250 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1255 ctl_free_beio(beio);
1256 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1257 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1258 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1259 ctl_config_write_done(io);
1263 ctl_be_block_config_write(io);
1267 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1270 struct ctl_be_block_softc *softc = be_lun->softc;
1271 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1272 struct ctl_be_block_io *beio;
1273 struct ctl_lba_len_flags *lbalen;
1274 uint64_t len_left, lba;
1275 uint32_t pb, pbo, adj;
1279 DPRINTF("entered\n");
1281 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1282 lbalen = ARGS(beio->io);
1284 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1285 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1286 ctl_free_beio(beio);
1287 ctl_set_invalid_field(&io->scsiio,
1293 ctl_config_write_done(io);
1297 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1298 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1299 beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1300 beio->bio_cmd = BIO_DELETE;
1301 beio->ds_trans_type = DEVSTAT_FREE;
1303 be_lun->unmap(be_lun, beio);
1307 beio->bio_cmd = BIO_WRITE;
1308 beio->ds_trans_type = DEVSTAT_WRITE;
1310 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1311 (uintmax_t)lbalen->lba, lbalen->len);
1313 pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
1314 if (be_lun->cbe_lun.pblockoff > 0)
1315 pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1318 len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1319 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1321 * Setup the S/G entry for this chunk.
1323 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1324 if (pb > cbe_lun->blocksize) {
1325 adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
1330 seglen -= seglen % cbe_lun->blocksize;
1332 seglen -= seglen % cbe_lun->blocksize;
1333 beio->sg_segs[i].len = seglen;
1334 beio->sg_segs[i].addr = uma_zalloc(softc->buf_zone, M_WAITOK);
1336 DPRINTF("segment %d addr %p len %zd\n", i,
1337 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1342 buf = beio->sg_segs[i].addr;
1344 for (; buf < end; buf += cbe_lun->blocksize) {
1345 if (lbalen->flags & SWS_NDOB) {
1346 memset(buf, 0, cbe_lun->blocksize);
1348 memcpy(buf, io->scsiio.kern_data_ptr,
1349 cbe_lun->blocksize);
1351 if (lbalen->flags & SWS_LBDATA)
1352 scsi_ulto4b(lbalen->lba + lba, buf);
1357 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1358 beio->io_len = lba * cbe_lun->blocksize;
1360 /* We can not do all in one run. Correct and schedule rerun. */
1364 beio->beio_cont = ctl_be_block_cw_done_ws;
1367 be_lun->dispatch(be_lun, beio);
1371 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1374 struct ctl_be_block_io *beio;
1375 struct ctl_ptr_len_flags *ptrlen;
1377 DPRINTF("entered\n");
1379 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1380 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1382 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1383 ctl_free_beio(beio);
1384 ctl_set_invalid_field(&io->scsiio,
1390 ctl_config_write_done(io);
1395 beio->io_offset = -1;
1396 beio->bio_cmd = BIO_DELETE;
1397 beio->ds_trans_type = DEVSTAT_FREE;
1399 be_lun->unmap(be_lun, beio);
1403 ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1408 ctl_free_beio(beio);
1409 ctl_config_read_done(io);
1413 ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1416 struct ctl_be_block_io *beio;
1417 struct ctl_be_block_softc *softc;
1419 DPRINTF("entered\n");
1421 softc = be_lun->softc;
1422 beio = ctl_alloc_beio(softc);
1425 beio->beio_cont = ctl_be_block_cr_done;
1426 PRIV(io)->ptr = (void *)beio;
1428 switch (io->scsiio.cdb[0]) {
1429 case SERVICE_ACTION_IN: /* GET LBA STATUS */
1431 beio->ds_trans_type = DEVSTAT_NO_DATA;
1432 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1434 if (be_lun->get_lba_status)
1435 be_lun->get_lba_status(be_lun, beio);
1437 ctl_be_block_cr_done(beio);
1440 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1446 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1451 ctl_free_beio(beio);
1452 ctl_config_write_done(io);
1456 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1459 struct ctl_be_block_io *beio;
1460 struct ctl_be_block_softc *softc;
1462 DPRINTF("entered\n");
1464 softc = be_lun->softc;
1465 beio = ctl_alloc_beio(softc);
1468 beio->beio_cont = ctl_be_block_cw_done;
1469 switch (io->scsiio.tag_type) {
1470 case CTL_TAG_ORDERED:
1471 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1473 case CTL_TAG_HEAD_OF_QUEUE:
1474 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1476 case CTL_TAG_UNTAGGED:
1477 case CTL_TAG_SIMPLE:
1480 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1483 PRIV(io)->ptr = (void *)beio;
1485 switch (io->scsiio.cdb[0]) {
1486 case SYNCHRONIZE_CACHE:
1487 case SYNCHRONIZE_CACHE_16:
1488 ctl_be_block_cw_dispatch_sync(be_lun, io);
1492 ctl_be_block_cw_dispatch_ws(be_lun, io);
1495 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1498 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1503 SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
1504 SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
1505 SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
1506 SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1509 ctl_be_block_next(struct ctl_be_block_io *beio)
1511 struct ctl_be_block_lun *be_lun;
1516 ctl_free_beio(beio);
1517 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1518 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1519 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1520 ctl_data_submit_done(io);
1524 io->io_hdr.status &= ~CTL_STATUS_MASK;
1525 io->io_hdr.status |= CTL_STATUS_NONE;
1527 mtx_lock(&be_lun->queue_lock);
1528 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1529 mtx_unlock(&be_lun->queue_lock);
1530 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1534 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1537 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1538 struct ctl_be_block_io *beio;
1539 struct ctl_be_block_softc *softc;
1540 struct ctl_lba_len_flags *lbalen;
1541 struct ctl_ptr_len_flags *bptrlen;
1542 uint64_t len_left, lbas;
1545 softc = be_lun->softc;
1547 DPRINTF("entered\n");
1550 if (lbalen->flags & CTL_LLF_WRITE) {
1551 SDT_PROBE0(cbb, , write, start);
1553 SDT_PROBE0(cbb, , read, start);
1556 beio = ctl_alloc_beio(softc);
1560 bptrlen->ptr = (void *)beio;
1562 switch (io->scsiio.tag_type) {
1563 case CTL_TAG_ORDERED:
1564 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1566 case CTL_TAG_HEAD_OF_QUEUE:
1567 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1569 case CTL_TAG_UNTAGGED:
1570 case CTL_TAG_SIMPLE:
1573 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1577 if (lbalen->flags & CTL_LLF_WRITE) {
1578 beio->bio_cmd = BIO_WRITE;
1579 beio->ds_trans_type = DEVSTAT_WRITE;
1581 beio->bio_cmd = BIO_READ;
1582 beio->ds_trans_type = DEVSTAT_READ;
1585 DPRINTF("%s at LBA %jx len %u @%ju\n",
1586 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1587 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1588 if (lbalen->flags & CTL_LLF_COMPARE) {
1589 beio->two_sglists = 1;
1590 lbas = CTLBLK_HALF_IO_SIZE;
1592 lbas = CTLBLK_MAX_IO_SIZE;
1594 lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
1595 beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
1596 beio->io_len = lbas * cbe_lun->blocksize;
1597 bptrlen->len += lbas;
1599 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1600 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1601 i, CTLBLK_MAX_SEGS));
1604 * Setup the S/G entry for this chunk.
1606 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1607 beio->sg_segs[i].addr = uma_zalloc(softc->buf_zone, M_WAITOK);
1609 DPRINTF("segment %d addr %p len %zd\n", i,
1610 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1612 /* Set up second segment for compare operation. */
1613 if (beio->two_sglists) {
1614 beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1615 beio->sg_segs[i].len;
1616 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1617 uma_zalloc(softc->buf_zone, M_WAITOK);
1621 len_left -= beio->sg_segs[i].len;
1623 if (bptrlen->len < lbalen->len)
1624 beio->beio_cont = ctl_be_block_next;
1625 io->scsiio.be_move_done = ctl_be_block_move_done;
1626 /* For compare we have separate S/G lists for read and datamove. */
1627 if (beio->two_sglists)
1628 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1630 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1631 io->scsiio.kern_data_len = beio->io_len;
1632 io->scsiio.kern_sg_entries = beio->num_segs;
1633 io->scsiio.kern_data_ref = ctl_refcnt_beio;
1634 io->scsiio.kern_data_arg = beio;
1635 io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1638 * For the read case, we need to read the data into our buffers and
1639 * then we can send it back to the user. For the write case, we
1640 * need to get the data from the user first.
1642 if (beio->bio_cmd == BIO_READ) {
1643 SDT_PROBE0(cbb, , read, alloc_done);
1644 be_lun->dispatch(be_lun, beio);
1646 SDT_PROBE0(cbb, , write, alloc_done);
1648 getbinuptime(&io->io_hdr.dma_start_bt);
1655 ctl_be_block_worker(void *context, int pending)
1657 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1658 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1660 struct ctl_be_block_io *beio;
1662 DPRINTF("entered\n");
1664 * Fetch and process I/Os from all queues. If we detect LUN
1665 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1666 * so make response maximally opaque to not confuse initiator.
1669 mtx_lock(&be_lun->queue_lock);
1670 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1672 DPRINTF("datamove queue\n");
1673 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1675 mtx_unlock(&be_lun->queue_lock);
1676 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1677 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1678 ctl_set_busy(&io->scsiio);
1679 ctl_complete_beio(beio);
1682 be_lun->dispatch(be_lun, beio);
1685 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1687 DPRINTF("config write queue\n");
1688 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1690 mtx_unlock(&be_lun->queue_lock);
1691 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1692 ctl_set_busy(&io->scsiio);
1693 ctl_config_write_done(io);
1696 ctl_be_block_cw_dispatch(be_lun, io);
1699 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1701 DPRINTF("config read queue\n");
1702 STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1704 mtx_unlock(&be_lun->queue_lock);
1705 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1706 ctl_set_busy(&io->scsiio);
1707 ctl_config_read_done(io);
1710 ctl_be_block_cr_dispatch(be_lun, io);
1713 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1715 DPRINTF("input queue\n");
1716 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1718 mtx_unlock(&be_lun->queue_lock);
1719 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1720 ctl_set_busy(&io->scsiio);
1721 ctl_data_submit_done(io);
1724 ctl_be_block_dispatch(be_lun, io);
1729 * If we get here, there is no work left in the queues, so
1730 * just break out and let the task queue go to sleep.
1732 mtx_unlock(&be_lun->queue_lock);
1738 * Entry point from CTL to the backend for I/O. We queue everything to a
1739 * work thread, so this just puts the I/O on a queue and wakes up the
1743 ctl_be_block_submit(union ctl_io *io)
1745 struct ctl_be_block_lun *be_lun;
1747 DPRINTF("entered\n");
1749 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1752 * Make sure we only get SCSI I/O.
1754 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1755 "%#x) encountered", io->io_hdr.io_type));
1759 mtx_lock(&be_lun->queue_lock);
1760 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1761 mtx_unlock(&be_lun->queue_lock);
1762 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1764 return (CTL_RETVAL_COMPLETE);
1768 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1769 int flag, struct thread *td)
1771 struct ctl_be_block_softc *softc = &backend_block_softc;
1777 struct ctl_lun_req *lun_req;
1779 lun_req = (struct ctl_lun_req *)addr;
1781 switch (lun_req->reqtype) {
1782 case CTL_LUNREQ_CREATE:
1783 error = ctl_be_block_create(softc, lun_req);
1786 error = ctl_be_block_rm(softc, lun_req);
1788 case CTL_LUNREQ_MODIFY:
1789 error = ctl_be_block_modify(softc, lun_req);
1792 lun_req->status = CTL_LUN_ERROR;
1793 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1794 "invalid LUN request type %d",
1809 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1811 struct ctl_be_lun *cbe_lun;
1812 struct ctl_be_block_filedata *file_data;
1813 struct ctl_lun_create_params *params;
1816 off_t ps, pss, po, pos, us, uss, uo, uos;
1819 cbe_lun = &be_lun->cbe_lun;
1820 file_data = &be_lun->backend.file;
1821 params = &be_lun->params;
1823 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1824 be_lun->dispatch = ctl_be_block_dispatch_file;
1825 be_lun->lun_flush = ctl_be_block_flush_file;
1826 be_lun->get_lba_status = ctl_be_block_gls_file;
1827 be_lun->getattr = ctl_be_block_getattr_file;
1828 be_lun->unmap = NULL;
1829 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1831 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1833 snprintf(req->error_str, sizeof(req->error_str),
1834 "error calling VOP_GETATTR() for file %s",
1839 file_data->cred = crhold(curthread->td_ucred);
1840 if (params->lun_size_bytes != 0)
1841 be_lun->size_bytes = params->lun_size_bytes;
1843 be_lun->size_bytes = vattr.va_size;
1846 * For files we can use any logical block size. Prefer 512 bytes
1847 * for compatibility reasons. If file's vattr.va_blocksize
1848 * (preferred I/O block size) is bigger and multiple to chosen
1849 * logical block size -- report it as physical block size.
1851 if (params->blocksize_bytes != 0)
1852 cbe_lun->blocksize = params->blocksize_bytes;
1853 else if (cbe_lun->lun_type == T_CDROM)
1854 cbe_lun->blocksize = 2048;
1856 cbe_lun->blocksize = 512;
1857 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1858 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1859 0 : (be_lun->size_blocks - 1);
1861 us = ps = vattr.va_blocksize;
1864 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
1866 ctl_expand_number(value, &ps);
1867 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
1869 ctl_expand_number(value, &po);
1870 pss = ps / cbe_lun->blocksize;
1871 pos = po / cbe_lun->blocksize;
1872 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
1873 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
1874 cbe_lun->pblockexp = fls(pss) - 1;
1875 cbe_lun->pblockoff = (pss - pos) % pss;
1878 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
1880 ctl_expand_number(value, &us);
1881 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
1883 ctl_expand_number(value, &uo);
1884 uss = us / cbe_lun->blocksize;
1885 uos = uo / cbe_lun->blocksize;
1886 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
1887 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
1888 cbe_lun->ublockexp = fls(uss) - 1;
1889 cbe_lun->ublockoff = (uss - uos) % uss;
1893 * Sanity check. The media size has to be at least one
1896 if (be_lun->size_bytes < cbe_lun->blocksize) {
1898 snprintf(req->error_str, sizeof(req->error_str),
1899 "file %s size %ju < block size %u", be_lun->dev_path,
1900 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1903 cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1908 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1910 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1911 struct ctl_lun_create_params *params;
1915 int error, atomic, maxio, ref, unmap, tmp;
1916 off_t ps, pss, po, pos, us, uss, uo, uos, otmp;
1918 params = &be_lun->params;
1920 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1921 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1924 if (strcmp(csw->d_name, "zvol") == 0) {
1925 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1926 be_lun->get_lba_status = ctl_be_block_gls_zvol;
1927 atomic = maxio = CTLBLK_MAX_IO_SIZE;
1929 be_lun->dispatch = ctl_be_block_dispatch_dev;
1930 be_lun->get_lba_status = NULL;
1932 maxio = dev->si_iosize_max;
1935 if (maxio > CTLBLK_MAX_IO_SIZE)
1936 maxio = CTLBLK_MAX_IO_SIZE;
1938 be_lun->lun_flush = ctl_be_block_flush_dev;
1939 be_lun->getattr = ctl_be_block_getattr_dev;
1940 be_lun->unmap = ctl_be_block_unmap_dev;
1942 if (!csw->d_ioctl) {
1943 dev_relthread(dev, ref);
1944 snprintf(req->error_str, sizeof(req->error_str),
1945 "no d_ioctl for device %s!", be_lun->dev_path);
1949 error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1952 dev_relthread(dev, ref);
1953 snprintf(req->error_str, sizeof(req->error_str),
1954 "error %d returned for DIOCGSECTORSIZE ioctl "
1955 "on %s!", error, be_lun->dev_path);
1960 * If the user has asked for a blocksize that is greater than the
1961 * backing device's blocksize, we can do it only if the blocksize
1962 * the user is asking for is an even multiple of the underlying
1963 * device's blocksize.
1965 if ((params->blocksize_bytes != 0) &&
1966 (params->blocksize_bytes >= tmp)) {
1967 if (params->blocksize_bytes % tmp == 0) {
1968 cbe_lun->blocksize = params->blocksize_bytes;
1970 dev_relthread(dev, ref);
1971 snprintf(req->error_str, sizeof(req->error_str),
1972 "requested blocksize %u is not an even "
1973 "multiple of backing device blocksize %u",
1974 params->blocksize_bytes, tmp);
1977 } else if (params->blocksize_bytes != 0) {
1978 dev_relthread(dev, ref);
1979 snprintf(req->error_str, sizeof(req->error_str),
1980 "requested blocksize %u < backing device "
1981 "blocksize %u", params->blocksize_bytes, tmp);
1983 } else if (cbe_lun->lun_type == T_CDROM)
1984 cbe_lun->blocksize = MAX(tmp, 2048);
1986 cbe_lun->blocksize = tmp;
1988 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
1991 dev_relthread(dev, ref);
1992 snprintf(req->error_str, sizeof(req->error_str),
1993 "error %d returned for DIOCGMEDIASIZE "
1994 " ioctl on %s!", error,
1999 if (params->lun_size_bytes != 0) {
2000 if (params->lun_size_bytes > otmp) {
2001 dev_relthread(dev, ref);
2002 snprintf(req->error_str, sizeof(req->error_str),
2003 "requested LUN size %ju > backing device "
2005 (uintmax_t)params->lun_size_bytes,
2010 be_lun->size_bytes = params->lun_size_bytes;
2012 be_lun->size_bytes = otmp;
2013 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2014 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2015 0 : (be_lun->size_blocks - 1);
2017 error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
2022 error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
2030 value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
2032 ctl_expand_number(value, &ps);
2033 value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
2035 ctl_expand_number(value, &po);
2036 pss = ps / cbe_lun->blocksize;
2037 pos = po / cbe_lun->blocksize;
2038 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
2039 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
2040 cbe_lun->pblockexp = fls(pss) - 1;
2041 cbe_lun->pblockoff = (pss - pos) % pss;
2044 value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
2046 ctl_expand_number(value, &us);
2047 value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
2049 ctl_expand_number(value, &uo);
2050 uss = us / cbe_lun->blocksize;
2051 uos = uo / cbe_lun->blocksize;
2052 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
2053 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
2054 cbe_lun->ublockexp = fls(uss) - 1;
2055 cbe_lun->ublockoff = (uss - uos) % uss;
2058 cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
2059 cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2061 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2064 struct diocgattr_arg arg;
2066 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2067 arg.len = sizeof(arg.value.i);
2068 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
2070 unmap = (error == 0) ? arg.value.i : 0;
2072 value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2074 unmap = (strcmp(value, "on") == 0);
2076 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
2078 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2080 dev_relthread(dev, ref);
2085 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2087 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2092 if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
2094 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2097 switch (be_lun->dev_type) {
2098 case CTL_BE_BLOCK_DEV:
2100 case CTL_BE_BLOCK_FILE:
2101 if (be_lun->backend.file.cred != NULL) {
2102 crfree(be_lun->backend.file.cred);
2103 be_lun->backend.file.cred = NULL;
2106 case CTL_BE_BLOCK_NONE:
2109 panic("Unexpected backend type %d", be_lun->dev_type);
2112 be_lun->dev_type = CTL_BE_BLOCK_NONE;
2118 ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2120 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2121 struct nameidata nd;
2126 if (rootvnode == NULL) {
2127 snprintf(req->error_str, sizeof(req->error_str),
2128 "Root filesystem is not mounted");
2133 value = dnvlist_get_string(cbe_lun->options, "file", NULL);
2134 if (value == NULL) {
2135 snprintf(req->error_str, sizeof(req->error_str),
2136 "no file argument specified");
2139 free(be_lun->dev_path, M_CTLBLK);
2140 be_lun->dev_path = strdup(value, M_CTLBLK);
2143 value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
2144 if (value != NULL) {
2145 if (strcmp(value, "on") != 0)
2147 } else if (cbe_lun->lun_type == T_DIRECT)
2151 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2152 error = vn_open(&nd, &flags, 0, NULL);
2153 if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
2159 * This is the only reasonable guess we can make as far as
2160 * path if the user doesn't give us a fully qualified path.
2161 * If they want to specify a file, they need to specify the
2164 if (be_lun->dev_path[0] != '/') {
2167 asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2169 free(be_lun->dev_path, M_CTLBLK);
2170 be_lun->dev_path = dev_name;
2173 snprintf(req->error_str, sizeof(req->error_str),
2174 "error opening %s: %d", be_lun->dev_path, error);
2178 cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
2180 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2182 NDFREE(&nd, NDF_ONLY_PNBUF);
2183 be_lun->vn = nd.ni_vp;
2185 /* We only support disks and files. */
2186 if (vn_isdisk_error(be_lun->vn, &error)) {
2187 error = ctl_be_block_open_dev(be_lun, req);
2188 } else if (be_lun->vn->v_type == VREG) {
2189 error = ctl_be_block_open_file(be_lun, req);
2192 snprintf(req->error_str, sizeof(req->error_str),
2193 "%s is not a disk or plain file", be_lun->dev_path);
2195 VOP_UNLOCK(be_lun->vn);
2198 ctl_be_block_close(be_lun);
2199 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2200 if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2201 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2202 value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
2203 if (value != NULL && strcmp(value, "on") == 0)
2204 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
2205 else if (value != NULL && strcmp(value, "read") == 0)
2206 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2207 else if (value != NULL && strcmp(value, "off") == 0)
2208 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2213 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2215 struct ctl_be_lun *cbe_lun;
2216 struct ctl_be_block_lun *be_lun;
2217 struct ctl_lun_create_params *params;
2218 char num_thread_str[16];
2221 int retval, num_threads;
2222 int tmp_num_threads;
2224 params = &req->reqdata.create;
2226 req->status = CTL_LUN_OK;
2228 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2229 cbe_lun = &be_lun->cbe_lun;
2230 be_lun->params = req->reqdata.create;
2231 be_lun->softc = softc;
2232 STAILQ_INIT(&be_lun->input_queue);
2233 STAILQ_INIT(&be_lun->config_read_queue);
2234 STAILQ_INIT(&be_lun->config_write_queue);
2235 STAILQ_INIT(&be_lun->datamove_queue);
2236 mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
2237 mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
2238 cbe_lun->options = nvlist_clone(req->args_nvl);
2240 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2241 cbe_lun->lun_type = params->device_type;
2243 cbe_lun->lun_type = T_DIRECT;
2246 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2247 if (value != NULL) {
2248 if (strcmp(value, "primary") == 0)
2249 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2250 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2251 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2253 if (cbe_lun->lun_type == T_DIRECT ||
2254 cbe_lun->lun_type == T_CDROM) {
2255 be_lun->size_bytes = params->lun_size_bytes;
2256 if (params->blocksize_bytes != 0)
2257 cbe_lun->blocksize = params->blocksize_bytes;
2258 else if (cbe_lun->lun_type == T_CDROM)
2259 cbe_lun->blocksize = 2048;
2261 cbe_lun->blocksize = 512;
2262 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2263 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2264 0 : (be_lun->size_blocks - 1);
2266 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2267 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2268 retval = ctl_be_block_open(be_lun, req);
2271 req->status = CTL_LUN_WARNING;
2274 num_threads = cbb_num_threads;
2279 value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
2280 if (value != NULL) {
2281 tmp_num_threads = strtol(value, NULL, 0);
2284 * We don't let the user specify less than one
2285 * thread, but hope he's clueful enough not to
2286 * specify 1000 threads.
2288 if (tmp_num_threads < 1) {
2289 snprintf(req->error_str, sizeof(req->error_str),
2290 "invalid number of threads %s",
2294 num_threads = tmp_num_threads;
2297 if (be_lun->vn == NULL)
2298 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2299 /* Tell the user the blocksize we ended up using */
2300 params->lun_size_bytes = be_lun->size_bytes;
2301 params->blocksize_bytes = cbe_lun->blocksize;
2302 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2303 cbe_lun->req_lun_id = params->req_lun_id;
2304 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2306 cbe_lun->req_lun_id = 0;
2308 cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
2309 cbe_lun->be = &ctl_be_block_driver;
2311 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2312 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2314 strncpy((char *)cbe_lun->serial_num, tmpstr,
2315 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2317 /* Tell the user what we used for a serial number */
2318 strncpy((char *)params->serial_num, tmpstr,
2319 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2321 strncpy((char *)cbe_lun->serial_num, params->serial_num,
2322 MIN(sizeof(cbe_lun->serial_num),
2323 sizeof(params->serial_num)));
2325 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2326 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
2327 strncpy((char *)cbe_lun->device_id, tmpstr,
2328 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2330 /* Tell the user what we used for a device ID */
2331 strncpy((char *)params->device_id, tmpstr,
2332 MIN(sizeof(params->device_id), sizeof(tmpstr)));
2334 strncpy((char *)cbe_lun->device_id, params->device_id,
2335 MIN(sizeof(cbe_lun->device_id),
2336 sizeof(params->device_id)));
2339 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2341 be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2342 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2344 if (be_lun->io_taskqueue == NULL) {
2345 snprintf(req->error_str, sizeof(req->error_str),
2346 "unable to create taskqueue");
2351 * Note that we start the same number of threads by default for
2352 * both the file case and the block device case. For the file
2353 * case, we need multiple threads to allow concurrency, because the
2354 * vnode interface is designed to be a blocking interface. For the
2355 * block device case, ZFS zvols at least will block the caller's
2356 * context in many instances, and so we need multiple threads to
2357 * overcome that problem. Other block devices don't need as many
2358 * threads, but they shouldn't cause too many problems.
2360 * If the user wants to just have a single thread for a block
2361 * device, he can specify that when the LUN is created, or change
2362 * the tunable/sysctl to alter the default number of threads.
2364 retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2365 /*num threads*/num_threads,
2367 /*proc*/control_softc->ctl_proc,
2368 /*thread name*/"block");
2373 be_lun->num_threads = num_threads;
2375 retval = ctl_add_lun(&be_lun->cbe_lun);
2377 snprintf(req->error_str, sizeof(req->error_str),
2378 "ctl_add_lun() returned error %d, see dmesg for "
2384 be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
2386 DEVSTAT_ALL_SUPPORTED,
2388 | DEVSTAT_TYPE_IF_OTHER,
2389 DEVSTAT_PRIORITY_OTHER);
2391 mtx_lock(&softc->lock);
2393 SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
2394 mtx_unlock(&softc->lock);
2396 params->req_lun_id = cbe_lun->lun_id;
2401 req->status = CTL_LUN_ERROR;
2403 if (be_lun->io_taskqueue != NULL)
2404 taskqueue_free(be_lun->io_taskqueue);
2405 ctl_be_block_close(be_lun);
2406 if (be_lun->dev_path != NULL)
2407 free(be_lun->dev_path, M_CTLBLK);
2408 nvlist_destroy(cbe_lun->options);
2409 mtx_destroy(&be_lun->queue_lock);
2410 mtx_destroy(&be_lun->io_lock);
2411 free(be_lun, M_CTLBLK);
2417 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2419 struct ctl_lun_rm_params *params;
2420 struct ctl_be_block_lun *be_lun;
2421 struct ctl_be_lun *cbe_lun;
2424 params = &req->reqdata.rm;
2426 sx_xlock(&softc->modify_lock);
2427 mtx_lock(&softc->lock);
2428 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2429 if (be_lun->cbe_lun.lun_id == params->lun_id) {
2430 SLIST_REMOVE(&softc->lun_list, be_lun,
2431 ctl_be_block_lun, links);
2436 mtx_unlock(&softc->lock);
2437 sx_xunlock(&softc->modify_lock);
2438 if (be_lun == NULL) {
2439 snprintf(req->error_str, sizeof(req->error_str),
2440 "LUN %u is not managed by the block backend",
2444 cbe_lun = &be_lun->cbe_lun;
2446 if (be_lun->vn != NULL) {
2447 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2448 ctl_lun_no_media(cbe_lun);
2449 taskqueue_drain_all(be_lun->io_taskqueue);
2450 ctl_be_block_close(be_lun);
2453 mtx_lock(&softc->lock);
2454 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2455 mtx_unlock(&softc->lock);
2457 retval = ctl_remove_lun(cbe_lun);
2459 snprintf(req->error_str, sizeof(req->error_str),
2460 "error %d returned from ctl_remove_lun() for "
2461 "LUN %d", retval, params->lun_id);
2462 mtx_lock(&softc->lock);
2463 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2464 mtx_unlock(&softc->lock);
2468 mtx_lock(&softc->lock);
2469 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2470 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2471 if (retval == EINTR)
2474 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2475 if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2476 mtx_unlock(&softc->lock);
2477 free(be_lun, M_CTLBLK);
2479 mtx_unlock(&softc->lock);
2483 req->status = CTL_LUN_OK;
2487 req->status = CTL_LUN_ERROR;
2492 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2494 struct ctl_lun_modify_params *params;
2495 struct ctl_be_block_lun *be_lun;
2496 struct ctl_be_lun *cbe_lun;
2501 params = &req->reqdata.modify;
2503 sx_xlock(&softc->modify_lock);
2504 mtx_lock(&softc->lock);
2505 SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2506 if (be_lun->cbe_lun.lun_id == params->lun_id)
2509 mtx_unlock(&softc->lock);
2510 if (be_lun == NULL) {
2511 snprintf(req->error_str, sizeof(req->error_str),
2512 "LUN %u is not managed by the block backend",
2516 cbe_lun = &be_lun->cbe_lun;
2518 if (params->lun_size_bytes != 0)
2519 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2521 if (req->args_nvl != NULL) {
2522 nvlist_destroy(cbe_lun->options);
2523 cbe_lun->options = nvlist_clone(req->args_nvl);
2526 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
2527 value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
2528 if (value != NULL) {
2529 if (strcmp(value, "primary") == 0)
2530 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2532 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2533 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2534 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2536 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2537 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
2538 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
2539 ctl_lun_primary(cbe_lun);
2541 ctl_lun_secondary(cbe_lun);
2544 oldsize = be_lun->size_blocks;
2545 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2546 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2547 if (be_lun->vn == NULL)
2548 error = ctl_be_block_open(be_lun, req);
2549 else if (vn_isdisk_error(be_lun->vn, &error))
2550 error = ctl_be_block_open_dev(be_lun, req);
2551 else if (be_lun->vn->v_type == VREG) {
2552 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2553 error = ctl_be_block_open_file(be_lun, req);
2554 VOP_UNLOCK(be_lun->vn);
2557 if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
2558 be_lun->vn != NULL) {
2559 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2560 ctl_lun_has_media(cbe_lun);
2561 } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2562 be_lun->vn == NULL) {
2563 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2564 ctl_lun_no_media(cbe_lun);
2566 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2568 if (be_lun->vn != NULL) {
2569 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2570 ctl_lun_no_media(cbe_lun);
2571 taskqueue_drain_all(be_lun->io_taskqueue);
2572 error = ctl_be_block_close(be_lun);
2576 if (be_lun->size_blocks != oldsize)
2577 ctl_lun_capacity_changed(cbe_lun);
2579 /* Tell the user the exact size we ended up using */
2580 params->lun_size_bytes = be_lun->size_bytes;
2582 sx_xunlock(&softc->modify_lock);
2583 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2587 sx_xunlock(&softc->modify_lock);
2588 req->status = CTL_LUN_ERROR;
2593 ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2595 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
2596 struct ctl_be_block_softc *softc = be_lun->softc;
2598 taskqueue_drain_all(be_lun->io_taskqueue);
2599 taskqueue_free(be_lun->io_taskqueue);
2600 if (be_lun->disk_stats != NULL)
2601 devstat_remove_entry(be_lun->disk_stats);
2602 nvlist_destroy(be_lun->cbe_lun.options);
2603 free(be_lun->dev_path, M_CTLBLK);
2604 mtx_destroy(&be_lun->queue_lock);
2605 mtx_destroy(&be_lun->io_lock);
2607 mtx_lock(&softc->lock);
2608 be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2609 if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2612 free(be_lun, M_CTLBLK);
2613 mtx_unlock(&softc->lock);
2617 ctl_be_block_config_write(union ctl_io *io)
2619 struct ctl_be_block_lun *be_lun;
2620 struct ctl_be_lun *cbe_lun;
2623 DPRINTF("entered\n");
2625 cbe_lun = CTL_BACKEND_LUN(io);
2626 be_lun = (struct ctl_be_block_lun *)cbe_lun;
2629 switch (io->scsiio.cdb[0]) {
2630 case SYNCHRONIZE_CACHE:
2631 case SYNCHRONIZE_CACHE_16:
2636 * The upper level CTL code will filter out any CDBs with
2637 * the immediate bit set and return the proper error.
2639 * We don't really need to worry about what LBA range the
2640 * user asked to be synced out. When they issue a sync
2641 * cache command, we'll sync out the whole thing.
2643 mtx_lock(&be_lun->queue_lock);
2644 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2646 mtx_unlock(&be_lun->queue_lock);
2647 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2649 case START_STOP_UNIT: {
2650 struct scsi_start_stop_unit *cdb;
2651 struct ctl_lun_req req;
2653 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2654 if ((cdb->how & SSS_PC_MASK) != 0) {
2655 ctl_set_success(&io->scsiio);
2656 ctl_config_write_done(io);
2659 if (cdb->how & SSS_START) {
2660 if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2661 retval = ctl_be_block_open(be_lun, &req);
2662 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2664 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2665 ctl_lun_has_media(cbe_lun);
2667 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2668 ctl_lun_no_media(cbe_lun);
2671 ctl_start_lun(cbe_lun);
2673 ctl_stop_lun(cbe_lun);
2674 if (cdb->how & SSS_LOEJ) {
2675 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2676 cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2677 ctl_lun_ejected(cbe_lun);
2678 if (be_lun->vn != NULL)
2679 ctl_be_block_close(be_lun);
2683 ctl_set_success(&io->scsiio);
2684 ctl_config_write_done(io);
2688 ctl_set_success(&io->scsiio);
2689 ctl_config_write_done(io);
2692 ctl_set_invalid_opcode(&io->scsiio);
2693 ctl_config_write_done(io);
2694 retval = CTL_RETVAL_COMPLETE;
2702 ctl_be_block_config_read(union ctl_io *io)
2704 struct ctl_be_block_lun *be_lun;
2707 DPRINTF("entered\n");
2709 be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2711 switch (io->scsiio.cdb[0]) {
2712 case SERVICE_ACTION_IN:
2713 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2714 mtx_lock(&be_lun->queue_lock);
2715 STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2716 &io->io_hdr, links);
2717 mtx_unlock(&be_lun->queue_lock);
2718 taskqueue_enqueue(be_lun->io_taskqueue,
2720 retval = CTL_RETVAL_QUEUED;
2723 ctl_set_invalid_field(&io->scsiio,
2729 ctl_config_read_done(io);
2730 retval = CTL_RETVAL_COMPLETE;
2733 ctl_set_invalid_opcode(&io->scsiio);
2734 ctl_config_read_done(io);
2735 retval = CTL_RETVAL_COMPLETE;
2743 ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2745 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2748 retval = sbuf_printf(sb, "\t<num_threads>");
2751 retval = sbuf_printf(sb, "%d", lun->num_threads);
2754 retval = sbuf_printf(sb, "</num_threads>\n");
2761 ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2763 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2765 if (lun->getattr == NULL)
2766 return (UINT64_MAX);
2767 return (lun->getattr(lun, attrname));
2771 ctl_be_block_init(void)
2773 struct ctl_be_block_softc *softc = &backend_block_softc;
2775 sx_init(&softc->modify_lock, "ctlblock modify");
2776 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2777 softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2778 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2779 softc->buf_zone = uma_zcreate("ctlblock", CTLBLK_MAX_SEG,
2780 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2781 SLIST_INIT(&softc->lun_list);
2786 ctl_be_block_shutdown(void)
2788 struct ctl_be_block_softc *softc = &backend_block_softc;
2789 struct ctl_be_block_lun *lun;
2791 mtx_lock(&softc->lock);
2792 while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
2793 SLIST_REMOVE_HEAD(&softc->lun_list, links);
2796 * Drop our lock here. Since ctl_remove_lun() can call
2797 * back into us, this could potentially lead to a recursive
2798 * lock of the same mutex, which would cause a hang.
2800 mtx_unlock(&softc->lock);
2801 ctl_remove_lun(&lun->cbe_lun);
2802 mtx_lock(&softc->lock);
2804 mtx_unlock(&softc->lock);
2805 uma_zdestroy(softc->buf_zone);
2806 uma_zdestroy(softc->beio_zone);
2807 mtx_destroy(&softc->lock);
2808 sx_destroy(&softc->modify_lock);