2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * Copyright (c) 2009-2011 Spectra Logic Corporation
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
35 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
38 * CAM Target Layer driver backend for block devices.
40 * Author: Ken Merry <ken@FreeBSD.org>
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <opt_kdtrace.h>
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
53 #include <sys/fcntl.h>
54 #include <sys/limits.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
63 #include <sys/endian.h>
66 #include <sys/taskqueue.h>
67 #include <sys/vnode.h>
68 #include <sys/namei.h>
69 #include <sys/mount.h>
71 #include <sys/fcntl.h>
72 #include <sys/filedesc.h>
75 #include <sys/module.h>
77 #include <sys/devicestat.h>
78 #include <sys/sysctl.h>
80 #include <geom/geom.h>
83 #include <cam/scsi/scsi_all.h>
84 #include <cam/scsi/scsi_da.h>
85 #include <cam/ctl/ctl_io.h>
86 #include <cam/ctl/ctl.h>
87 #include <cam/ctl/ctl_backend.h>
88 #include <cam/ctl/ctl_frontend_internal.h>
89 #include <cam/ctl/ctl_ioctl.h>
90 #include <cam/ctl/ctl_scsi_all.h>
91 #include <cam/ctl/ctl_error.h>
94 * The idea here is that we'll allocate enough S/G space to hold a 1MB
95 * I/O. If we get an I/O larger than that, we'll split it.
97 #define CTLBLK_HALF_IO_SIZE (512 * 1024)
98 #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2)
99 #define CTLBLK_MAX_SEG MAXPHYS
100 #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
101 #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2)
104 #define DPRINTF(fmt, args...) \
105 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
107 #define DPRINTF(fmt, args...) do {} while(0)
111 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
113 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
115 SDT_PROVIDER_DEFINE(cbb);
118 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01,
119 CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02,
120 CTL_BE_BLOCK_LUN_WAITING = 0x04,
121 CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08
122 } ctl_be_block_lun_flags;
130 struct ctl_be_block_devdata {
136 struct ctl_be_block_filedata {
140 union ctl_be_block_bedata {
141 struct ctl_be_block_devdata dev;
142 struct ctl_be_block_filedata file;
145 struct ctl_be_block_io;
146 struct ctl_be_block_lun;
148 typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
149 struct ctl_be_block_io *beio);
152 * Backend LUN structure. There is a 1:1 mapping between a block device
153 * and a backend block LUN, and between a backend block LUN and a CTL LUN.
155 struct ctl_be_block_lun {
156 struct ctl_block_disk *disk;
159 ctl_be_block_type dev_type;
161 union ctl_be_block_bedata backend;
162 cbb_dispatch_t dispatch;
163 cbb_dispatch_t lun_flush;
164 cbb_dispatch_t unmap;
166 uint64_t size_blocks;
172 struct ctl_be_block_softc *softc;
173 struct devstat *disk_stats;
174 ctl_be_block_lun_flags flags;
175 STAILQ_ENTRY(ctl_be_block_lun) links;
176 struct ctl_be_lun ctl_be_lun;
177 struct taskqueue *io_taskqueue;
180 STAILQ_HEAD(, ctl_io_hdr) input_queue;
181 STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
182 STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
183 struct mtx_padalign io_lock;
184 struct mtx_padalign queue_lock;
188 * Overall softc structure for the block backend module.
190 struct ctl_be_block_softc {
193 STAILQ_HEAD(, ctl_block_disk) disk_list;
195 STAILQ_HEAD(, ctl_be_block_lun) lun_list;
198 static struct ctl_be_block_softc backend_block_softc;
201 * Per-I/O information.
203 struct ctl_be_block_io {
205 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS];
206 struct iovec xiovecs[CTLBLK_MAX_SEGS];
214 struct bintime ds_t0;
215 devstat_tag_type ds_tag_type;
216 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 static int cbb_num_threads = 14;
225 TUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads);
226 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
227 "CAM Target Layer Block Backend");
228 SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW,
229 &cbb_num_threads, 0, "Number of threads per backing file");
231 static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
232 static void ctl_free_beio(struct ctl_be_block_io *beio);
233 static void ctl_complete_beio(struct ctl_be_block_io *beio);
234 static int ctl_be_block_move_done(union ctl_io *io);
235 static void ctl_be_block_biodone(struct bio *bio);
236 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
237 struct ctl_be_block_io *beio);
238 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
239 struct ctl_be_block_io *beio);
240 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
241 struct ctl_be_block_io *beio);
242 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
243 struct ctl_be_block_io *beio);
244 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
245 struct ctl_be_block_io *beio);
246 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
248 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
250 static void ctl_be_block_worker(void *context, int pending);
251 static int ctl_be_block_submit(union ctl_io *io);
252 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
253 int flag, struct thread *td);
254 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
255 struct ctl_lun_req *req);
256 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
257 struct ctl_lun_req *req);
258 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
259 static int ctl_be_block_open(struct ctl_be_block_softc *softc,
260 struct ctl_be_block_lun *be_lun,
261 struct ctl_lun_req *req);
262 static int ctl_be_block_create(struct ctl_be_block_softc *softc,
263 struct ctl_lun_req *req);
264 static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
265 struct ctl_lun_req *req);
266 static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
267 struct ctl_lun_req *req);
268 static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
269 struct ctl_lun_req *req);
270 static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
271 struct ctl_lun_req *req);
272 static void ctl_be_block_lun_shutdown(void *be_lun);
273 static void ctl_be_block_lun_config_status(void *be_lun,
274 ctl_lun_config_status status);
275 static int ctl_be_block_config_write(union ctl_io *io);
276 static int ctl_be_block_config_read(union ctl_io *io);
277 static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
278 int ctl_be_block_init(void);
280 static struct ctl_backend_driver ctl_be_block_driver =
283 .flags = CTL_BE_FLAG_HAS_CONFIG,
284 .init = ctl_be_block_init,
285 .data_submit = ctl_be_block_submit,
286 .data_move_done = ctl_be_block_move_done,
287 .config_read = ctl_be_block_config_read,
288 .config_write = ctl_be_block_config_write,
289 .ioctl = ctl_be_block_ioctl,
290 .lun_info = ctl_be_block_lun_info
293 MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
294 CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
296 static uma_zone_t beio_zone;
298 static struct ctl_be_block_io *
299 ctl_alloc_beio(struct ctl_be_block_softc *softc)
301 struct ctl_be_block_io *beio;
303 beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
309 ctl_free_beio(struct ctl_be_block_io *beio)
316 for (i = 0; i < beio->num_segs; i++) {
317 if (beio->sg_segs[i].addr == NULL)
320 uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
321 beio->sg_segs[i].addr = NULL;
323 /* For compare we had two equal S/G lists. */
324 if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
325 uma_zfree(beio->lun->lun_zone,
326 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
327 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
331 if (duplicate_free > 0) {
332 printf("%s: %d duplicate frees out of %d segments\n", __func__,
333 duplicate_free, beio->num_segs);
336 uma_zfree(beio_zone, beio);
340 ctl_complete_beio(struct ctl_be_block_io *beio)
342 union ctl_io *io = beio->io;
344 if (beio->beio_cont != NULL) {
345 beio->beio_cont(beio);
348 ctl_data_submit_done(io);
353 ctl_be_block_move_done(union ctl_io *io)
355 struct ctl_be_block_io *beio;
356 struct ctl_be_block_lun *be_lun;
357 struct ctl_lba_len_flags *lbalen;
359 struct bintime cur_bt;
363 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
366 DPRINTF("entered\n");
370 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
371 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
372 io->io_hdr.num_dmas++;
374 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
377 * We set status at this point for read commands, and write
378 * commands with errors.
380 if ((io->io_hdr.port_status == 0) &&
381 ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) &&
382 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
383 lbalen = ARGS(beio->io);
384 if (lbalen->flags & CTL_LLF_READ) {
385 ctl_set_success(&io->scsiio);
386 } else if (lbalen->flags & CTL_LLF_COMPARE) {
387 /* We have two data blocks ready for comparison. */
388 for (i = 0; i < beio->num_segs; i++) {
389 if (memcmp(beio->sg_segs[i].addr,
390 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
391 beio->sg_segs[i].len) != 0)
394 if (i < beio->num_segs)
395 ctl_set_sense(&io->scsiio,
397 /*sense_key*/ SSD_KEY_MISCOMPARE,
402 ctl_set_success(&io->scsiio);
405 else if ((io->io_hdr.port_status != 0)
406 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
407 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
409 * For hardware error sense keys, the sense key
410 * specific value is defined to be a retry count,
411 * but we use it to pass back an internal FETD
412 * error code. XXX KDM Hopefully the FETD is only
413 * using 16 bits for an error code, since that's
414 * all the space we have in the sks field.
416 ctl_set_internal_failure(&io->scsiio,
419 io->io_hdr.port_status);
423 * If this is a read, or a write with errors, it is done.
425 if ((beio->bio_cmd == BIO_READ)
426 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
427 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
428 ctl_complete_beio(beio);
433 * At this point, we have a write and the DMA completed
434 * successfully. We now have to queue it to the task queue to
435 * execute the backend I/O. That is because we do blocking
436 * memory allocations, and in the file backing case, blocking I/O.
437 * This move done routine is generally called in the SIM's
438 * interrupt context, and therefore we cannot block.
440 mtx_lock(&be_lun->queue_lock);
442 * XXX KDM make sure that links is okay to use at this point.
443 * Otherwise, we either need to add another field to ctl_io_hdr,
444 * or deal with resource allocation here.
446 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
447 mtx_unlock(&be_lun->queue_lock);
449 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
455 ctl_be_block_biodone(struct bio *bio)
457 struct ctl_be_block_io *beio;
458 struct ctl_be_block_lun *be_lun;
462 beio = bio->bio_caller1;
466 DPRINTF("entered\n");
468 error = bio->bio_error;
469 mtx_lock(&be_lun->io_lock);
473 beio->num_bios_done++;
476 * XXX KDM will this cause WITNESS to complain? Holding a lock
477 * during the free might cause it to complain.
482 * If the send complete bit isn't set, or we aren't the last I/O to
483 * complete, then we're done.
485 if ((beio->send_complete == 0)
486 || (beio->num_bios_done < beio->num_bios_sent)) {
487 mtx_unlock(&be_lun->io_lock);
492 * At this point, we've verified that we are the last I/O to
493 * complete, so it's safe to drop the lock.
495 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
496 beio->ds_tag_type, beio->ds_trans_type,
497 /*now*/ NULL, /*then*/&beio->ds_t0);
498 mtx_unlock(&be_lun->io_lock);
501 * If there are any errors from the backing device, we fail the
502 * entire I/O with a medium error.
504 if (beio->num_errors > 0) {
505 if (error == EOPNOTSUPP) {
506 ctl_set_invalid_opcode(&io->scsiio);
507 } else if (beio->bio_cmd == BIO_FLUSH) {
508 /* XXX KDM is there is a better error here? */
509 ctl_set_internal_failure(&io->scsiio,
511 /*retry_count*/ 0xbad2);
513 ctl_set_medium_error(&io->scsiio);
514 ctl_complete_beio(beio);
519 * If this is a write, a flush, a delete or verify, we're all done.
520 * If this is a read, we can now send the data to the user.
522 if ((beio->bio_cmd == BIO_WRITE)
523 || (beio->bio_cmd == BIO_FLUSH)
524 || (beio->bio_cmd == BIO_DELETE)
525 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
526 ctl_set_success(&io->scsiio);
527 ctl_complete_beio(beio);
530 getbintime(&io->io_hdr.dma_start_bt);
537 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
538 struct ctl_be_block_io *beio)
540 union ctl_io *io = beio->io;
541 struct mount *mountpoint;
542 int error, lock_flags;
544 DPRINTF("entered\n");
546 binuptime(&beio->ds_t0);
547 mtx_lock(&be_lun->io_lock);
548 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
549 mtx_unlock(&be_lun->io_lock);
551 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
553 if (MNT_SHARED_WRITES(mountpoint)
554 || ((mountpoint == NULL)
555 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
556 lock_flags = LK_SHARED;
558 lock_flags = LK_EXCLUSIVE;
560 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
562 error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
563 VOP_UNLOCK(be_lun->vn, 0);
565 vn_finished_write(mountpoint);
567 mtx_lock(&be_lun->io_lock);
568 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
569 beio->ds_tag_type, beio->ds_trans_type,
570 /*now*/ NULL, /*then*/&beio->ds_t0);
571 mtx_unlock(&be_lun->io_lock);
574 ctl_set_success(&io->scsiio);
576 /* XXX KDM is there is a better error here? */
577 ctl_set_internal_failure(&io->scsiio,
579 /*retry_count*/ 0xbad1);
582 ctl_complete_beio(beio);
585 SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
586 SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
587 SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
588 SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
591 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
592 struct ctl_be_block_io *beio)
594 struct ctl_be_block_filedata *file_data;
597 struct iovec *xiovec;
601 DPRINTF("entered\n");
603 file_data = &be_lun->backend.file;
605 flags = beio->bio_flags;
607 bzero(&xuio, sizeof(xuio));
608 if (beio->bio_cmd == BIO_READ) {
609 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
610 xuio.uio_rw = UIO_READ;
612 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
613 xuio.uio_rw = UIO_WRITE;
615 xuio.uio_offset = beio->io_offset;
616 xuio.uio_resid = beio->io_len;
617 xuio.uio_segflg = UIO_SYSSPACE;
618 xuio.uio_iov = beio->xiovecs;
619 xuio.uio_iovcnt = beio->num_segs;
620 xuio.uio_td = curthread;
622 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
623 xiovec->iov_base = beio->sg_segs[i].addr;
624 xiovec->iov_len = beio->sg_segs[i].len;
627 binuptime(&beio->ds_t0);
628 mtx_lock(&be_lun->io_lock);
629 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
630 mtx_unlock(&be_lun->io_lock);
632 if (beio->bio_cmd == BIO_READ) {
633 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
636 * UFS pays attention to IO_DIRECT for reads. If the
637 * DIRECTIO option is configured into the kernel, it calls
638 * ffs_rawread(). But that only works for single-segment
639 * uios with user space addresses. In our case, with a
640 * kernel uio, it still reads into the buffer cache, but it
641 * will just try to release the buffer from the cache later
644 * ZFS does not pay attention to IO_DIRECT for reads.
646 * UFS does not pay attention to IO_SYNC for reads.
648 * ZFS pays attention to IO_SYNC (which translates into the
649 * Solaris define FRSYNC for zfs_read()) for reads. It
650 * attempts to sync the file before reading.
652 * So, to attempt to provide some barrier semantics in the
653 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
655 error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
656 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
658 VOP_UNLOCK(be_lun->vn, 0);
659 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
661 struct mount *mountpoint;
664 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
666 if (MNT_SHARED_WRITES(mountpoint)
667 || ((mountpoint == NULL)
668 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
669 lock_flags = LK_SHARED;
671 lock_flags = LK_EXCLUSIVE;
673 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
676 * UFS pays attention to IO_DIRECT for writes. The write
677 * is done asynchronously. (Normally the write would just
678 * get put into cache.
680 * UFS pays attention to IO_SYNC for writes. It will
681 * attempt to write the buffer out synchronously if that
684 * ZFS does not pay attention to IO_DIRECT for writes.
686 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
687 * for writes. It will flush the transaction from the
688 * cache before returning.
690 * So if we've got the BIO_ORDERED flag set, we want
691 * IO_SYNC in either the UFS or ZFS case.
693 error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
694 IO_SYNC : 0, file_data->cred);
695 VOP_UNLOCK(be_lun->vn, 0);
697 vn_finished_write(mountpoint);
698 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
701 mtx_lock(&be_lun->io_lock);
702 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
703 beio->ds_tag_type, beio->ds_trans_type,
704 /*now*/ NULL, /*then*/&beio->ds_t0);
705 mtx_unlock(&be_lun->io_lock);
708 * If we got an error, set the sense data to "MEDIUM ERROR" and
709 * return the I/O to the user.
714 ctl_scsi_path_string(io, path_str, sizeof(path_str));
716 * XXX KDM ZFS returns ENOSPC when the underlying
717 * filesystem fills up. What kind of SCSI error should we
720 printf("%s%s command returned errno %d\n", path_str,
721 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
722 ctl_set_medium_error(&io->scsiio);
723 ctl_complete_beio(beio);
728 * If this is a write or a verify, we're all done.
729 * If this is a read, we can now send the data to the user.
731 if ((beio->bio_cmd == BIO_WRITE) ||
732 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
733 ctl_set_success(&io->scsiio);
734 ctl_complete_beio(beio);
737 getbintime(&io->io_hdr.dma_start_bt);
744 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
745 struct ctl_be_block_io *beio)
747 struct ctl_be_block_devdata *dev_data;
750 struct iovec *xiovec;
754 DPRINTF("entered\n");
756 dev_data = &be_lun->backend.dev;
758 flags = beio->bio_flags;
760 bzero(&xuio, sizeof(xuio));
761 if (beio->bio_cmd == BIO_READ) {
762 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
763 xuio.uio_rw = UIO_READ;
765 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
766 xuio.uio_rw = UIO_WRITE;
768 xuio.uio_offset = beio->io_offset;
769 xuio.uio_resid = beio->io_len;
770 xuio.uio_segflg = UIO_SYSSPACE;
771 xuio.uio_iov = beio->xiovecs;
772 xuio.uio_iovcnt = beio->num_segs;
773 xuio.uio_td = curthread;
775 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
776 xiovec->iov_base = beio->sg_segs[i].addr;
777 xiovec->iov_len = beio->sg_segs[i].len;
780 binuptime(&beio->ds_t0);
781 mtx_lock(&be_lun->io_lock);
782 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
783 mtx_unlock(&be_lun->io_lock);
785 if (beio->bio_cmd == BIO_READ) {
786 error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, 0);
787 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
789 error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, 0);
790 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
793 mtx_lock(&be_lun->io_lock);
794 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
795 beio->ds_tag_type, beio->ds_trans_type,
796 /*now*/ NULL, /*then*/&beio->ds_t0);
797 mtx_unlock(&be_lun->io_lock);
800 * If we got an error, set the sense data to "MEDIUM ERROR" and
801 * return the I/O to the user.
804 ctl_set_medium_error(&io->scsiio);
805 ctl_complete_beio(beio);
810 * If this is a write or a verify, we're all done.
811 * If this is a read, we can now send the data to the user.
813 if ((beio->bio_cmd == BIO_WRITE) ||
814 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
815 ctl_set_success(&io->scsiio);
816 ctl_complete_beio(beio);
819 getbintime(&io->io_hdr.dma_start_bt);
826 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
827 struct ctl_be_block_io *beio)
831 struct ctl_be_block_devdata *dev_data;
833 dev_data = &be_lun->backend.dev;
836 DPRINTF("entered\n");
838 /* This can't fail, it's a blocking allocation. */
841 bio->bio_cmd = BIO_FLUSH;
842 bio->bio_flags |= BIO_ORDERED;
843 bio->bio_dev = dev_data->cdev;
846 bio->bio_done = ctl_be_block_biodone;
847 bio->bio_caller1 = beio;
851 * We don't need to acquire the LUN lock here, because we are only
852 * sending one bio, and so there is no other context to synchronize
855 beio->num_bios_sent = 1;
856 beio->send_complete = 1;
858 binuptime(&beio->ds_t0);
859 mtx_lock(&be_lun->io_lock);
860 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
861 mtx_unlock(&be_lun->io_lock);
863 (*dev_data->csw->d_strategy)(bio);
867 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
868 struct ctl_be_block_io *beio,
869 uint64_t off, uint64_t len, int last)
872 struct ctl_be_block_devdata *dev_data;
875 dev_data = &be_lun->backend.dev;
876 maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
879 bio->bio_cmd = BIO_DELETE;
880 bio->bio_flags |= beio->bio_flags;
881 bio->bio_dev = dev_data->cdev;
882 bio->bio_offset = off;
883 bio->bio_length = MIN(len, maxlen);
885 bio->bio_done = ctl_be_block_biodone;
886 bio->bio_caller1 = beio;
887 bio->bio_pblkno = off / be_lun->blocksize;
889 off += bio->bio_length;
890 len -= bio->bio_length;
892 mtx_lock(&be_lun->io_lock);
893 beio->num_bios_sent++;
894 if (last && len == 0)
895 beio->send_complete = 1;
896 mtx_unlock(&be_lun->io_lock);
898 (*dev_data->csw->d_strategy)(bio);
903 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
904 struct ctl_be_block_io *beio)
907 struct ctl_be_block_devdata *dev_data;
908 struct ctl_ptr_len_flags *ptrlen;
909 struct scsi_unmap_desc *buf, *end;
912 dev_data = &be_lun->backend.dev;
915 DPRINTF("entered\n");
917 binuptime(&beio->ds_t0);
918 mtx_lock(&be_lun->io_lock);
919 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
920 mtx_unlock(&be_lun->io_lock);
922 if (beio->io_offset == -1) {
924 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
925 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
926 end = buf + ptrlen->len / sizeof(*buf);
927 for (; buf < end; buf++) {
928 len = (uint64_t)scsi_4btoul(buf->length) *
931 ctl_be_block_unmap_dev_range(be_lun, beio,
932 scsi_8btou64(buf->lba) * be_lun->blocksize, len,
933 (end - buf < 2) ? TRUE : FALSE);
936 ctl_be_block_unmap_dev_range(be_lun, beio,
937 beio->io_offset, beio->io_len, TRUE);
941 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
942 struct ctl_be_block_io *beio)
944 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
947 struct ctl_be_block_devdata *dev_data;
951 DPRINTF("entered\n");
953 dev_data = &be_lun->backend.dev;
956 * We have to limit our I/O size to the maximum supported by the
957 * backend device. Hopefully it is MAXPHYS. If the driver doesn't
958 * set it properly, use DFLTPHYS.
960 max_iosize = dev_data->cdev->si_iosize_max;
961 if (max_iosize < PAGE_SIZE)
962 max_iosize = DFLTPHYS;
964 cur_offset = beio->io_offset;
965 for (i = 0; i < beio->num_segs; i++) {
969 cur_size = beio->sg_segs[i].len;
970 cur_ptr = beio->sg_segs[i].addr;
972 while (cur_size > 0) {
973 /* This can't fail, it's a blocking allocation. */
976 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
978 bio->bio_cmd = beio->bio_cmd;
979 bio->bio_flags |= beio->bio_flags;
980 bio->bio_dev = dev_data->cdev;
981 bio->bio_caller1 = beio;
982 bio->bio_length = min(cur_size, max_iosize);
983 bio->bio_offset = cur_offset;
984 bio->bio_data = cur_ptr;
985 bio->bio_done = ctl_be_block_biodone;
986 bio->bio_pblkno = cur_offset / be_lun->blocksize;
988 cur_offset += bio->bio_length;
989 cur_ptr += bio->bio_length;
990 cur_size -= bio->bio_length;
992 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
993 beio->num_bios_sent++;
996 binuptime(&beio->ds_t0);
997 mtx_lock(&be_lun->io_lock);
998 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
999 beio->send_complete = 1;
1000 mtx_unlock(&be_lun->io_lock);
1003 * Fire off all allocated requests!
1005 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1006 TAILQ_REMOVE(&queue, bio, bio_queue);
1007 (*dev_data->csw->d_strategy)(bio);
1012 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1017 ctl_free_beio(beio);
1018 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1019 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1020 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1021 ctl_config_write_done(io);
1025 ctl_be_block_config_write(io);
1029 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1032 struct ctl_be_block_io *beio;
1033 struct ctl_be_block_softc *softc;
1034 struct ctl_lba_len_flags *lbalen;
1035 uint64_t len_left, lba;
1039 DPRINTF("entered\n");
1041 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1042 softc = be_lun->softc;
1043 lbalen = ARGS(beio->io);
1045 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR) ||
1046 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1047 ctl_free_beio(beio);
1048 ctl_set_invalid_field(&io->scsiio,
1054 ctl_config_write_done(io);
1059 * If the I/O came down with an ordered or head of queue tag, set
1060 * the BIO_ORDERED attribute. For head of queue tags, that's
1061 * pretty much the best we can do.
1063 if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1064 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1065 beio->bio_flags = BIO_ORDERED;
1067 switch (io->scsiio.tag_type) {
1068 case CTL_TAG_ORDERED:
1069 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1071 case CTL_TAG_HEAD_OF_QUEUE:
1072 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1074 case CTL_TAG_UNTAGGED:
1075 case CTL_TAG_SIMPLE:
1078 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1082 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1083 beio->io_offset = lbalen->lba * be_lun->blocksize;
1084 beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1085 beio->bio_cmd = BIO_DELETE;
1086 beio->ds_trans_type = DEVSTAT_FREE;
1088 be_lun->unmap(be_lun, beio);
1092 beio->bio_cmd = BIO_WRITE;
1093 beio->ds_trans_type = DEVSTAT_WRITE;
1095 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1096 (uintmax_t)lbalen->lba, lbalen->len);
1098 len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1099 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1102 * Setup the S/G entry for this chunk.
1104 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1105 seglen -= seglen % be_lun->blocksize;
1106 beio->sg_segs[i].len = seglen;
1107 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1109 DPRINTF("segment %d addr %p len %zd\n", i,
1110 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1115 buf = beio->sg_segs[i].addr;
1117 for (; buf < end; buf += be_lun->blocksize) {
1118 memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1119 if (lbalen->flags & SWS_LBDATA)
1120 scsi_ulto4b(lbalen->lba + lba, buf);
1125 beio->io_offset = lbalen->lba * be_lun->blocksize;
1126 beio->io_len = lba * be_lun->blocksize;
1128 /* We can not do all in one run. Correct and schedule rerun. */
1132 beio->beio_cont = ctl_be_block_cw_done_ws;
1135 be_lun->dispatch(be_lun, beio);
1139 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1142 struct ctl_be_block_io *beio;
1143 struct ctl_be_block_softc *softc;
1144 struct ctl_ptr_len_flags *ptrlen;
1146 DPRINTF("entered\n");
1148 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1149 softc = be_lun->softc;
1150 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1152 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1153 ctl_free_beio(beio);
1154 ctl_set_invalid_field(&io->scsiio,
1160 ctl_config_write_done(io);
1165 * If the I/O came down with an ordered or head of queue tag, set
1166 * the BIO_ORDERED attribute. For head of queue tags, that's
1167 * pretty much the best we can do.
1169 if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1170 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1171 beio->bio_flags = BIO_ORDERED;
1173 switch (io->scsiio.tag_type) {
1174 case CTL_TAG_ORDERED:
1175 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1177 case CTL_TAG_HEAD_OF_QUEUE:
1178 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1180 case CTL_TAG_UNTAGGED:
1181 case CTL_TAG_SIMPLE:
1184 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1189 beio->io_offset = -1;
1191 beio->bio_cmd = BIO_DELETE;
1192 beio->ds_trans_type = DEVSTAT_FREE;
1196 be_lun->unmap(be_lun, beio);
1200 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1205 ctl_free_beio(beio);
1206 ctl_config_write_done(io);
1210 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1213 struct ctl_be_block_io *beio;
1214 struct ctl_be_block_softc *softc;
1216 DPRINTF("entered\n");
1218 softc = be_lun->softc;
1219 beio = ctl_alloc_beio(softc);
1222 beio->beio_cont = ctl_be_block_cw_done;
1223 PRIV(io)->ptr = (void *)beio;
1225 switch (io->scsiio.cdb[0]) {
1226 case SYNCHRONIZE_CACHE:
1227 case SYNCHRONIZE_CACHE_16:
1228 beio->bio_cmd = BIO_FLUSH;
1229 beio->ds_trans_type = DEVSTAT_NO_DATA;
1230 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1232 be_lun->lun_flush(be_lun, beio);
1236 ctl_be_block_cw_dispatch_ws(be_lun, io);
1239 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1242 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1247 SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1248 SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1249 SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1250 SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1253 ctl_be_block_next(struct ctl_be_block_io *beio)
1255 struct ctl_be_block_lun *be_lun;
1260 ctl_free_beio(beio);
1261 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1262 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1263 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1264 ctl_data_submit_done(io);
1268 io->io_hdr.status &= ~CTL_STATUS_MASK;
1269 io->io_hdr.status |= CTL_STATUS_NONE;
1271 mtx_lock(&be_lun->queue_lock);
1273 * XXX KDM make sure that links is okay to use at this point.
1274 * Otherwise, we either need to add another field to ctl_io_hdr,
1275 * or deal with resource allocation here.
1277 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1278 mtx_unlock(&be_lun->queue_lock);
1280 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1284 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1287 struct ctl_be_block_io *beio;
1288 struct ctl_be_block_softc *softc;
1289 struct ctl_lba_len_flags *lbalen;
1290 struct ctl_ptr_len_flags *bptrlen;
1291 uint64_t len_left, lbas;
1294 softc = be_lun->softc;
1296 DPRINTF("entered\n");
1299 if (lbalen->flags & CTL_LLF_WRITE) {
1300 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1302 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1305 beio = ctl_alloc_beio(softc);
1309 bptrlen->ptr = (void *)beio;
1312 * If the I/O came down with an ordered or head of queue tag, set
1313 * the BIO_ORDERED attribute. For head of queue tags, that's
1314 * pretty much the best we can do.
1316 * XXX KDM we don't have a great way to easily know about the FUA
1317 * bit right now (it is decoded in ctl_read_write(), but we don't
1318 * pass that knowledge to the backend), and in any case we would
1319 * need to determine how to handle it.
1321 if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1322 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1323 beio->bio_flags = BIO_ORDERED;
1325 switch (io->scsiio.tag_type) {
1326 case CTL_TAG_ORDERED:
1327 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1329 case CTL_TAG_HEAD_OF_QUEUE:
1330 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1332 case CTL_TAG_UNTAGGED:
1333 case CTL_TAG_SIMPLE:
1336 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1340 if (lbalen->flags & CTL_LLF_WRITE) {
1341 beio->bio_cmd = BIO_WRITE;
1342 beio->ds_trans_type = DEVSTAT_WRITE;
1344 beio->bio_cmd = BIO_READ;
1345 beio->ds_trans_type = DEVSTAT_READ;
1348 DPRINTF("%s at LBA %jx len %u @%ju\n",
1349 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1350 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1351 if (lbalen->flags & CTL_LLF_COMPARE)
1352 lbas = CTLBLK_HALF_IO_SIZE;
1354 lbas = CTLBLK_MAX_IO_SIZE;
1355 lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1356 beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1357 beio->io_len = lbas * be_lun->blocksize;
1358 bptrlen->len += lbas;
1360 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1361 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1362 i, CTLBLK_MAX_SEGS));
1365 * Setup the S/G entry for this chunk.
1367 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1368 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1370 DPRINTF("segment %d addr %p len %zd\n", i,
1371 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1373 /* Set up second segment for compare operation. */
1374 if (lbalen->flags & CTL_LLF_COMPARE) {
1375 beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1376 beio->sg_segs[i].len;
1377 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1378 uma_zalloc(be_lun->lun_zone, M_WAITOK);
1382 len_left -= beio->sg_segs[i].len;
1384 if (bptrlen->len < lbalen->len)
1385 beio->beio_cont = ctl_be_block_next;
1386 io->scsiio.be_move_done = ctl_be_block_move_done;
1387 /* For compare we have separate S/G lists for read and datamove. */
1388 if (lbalen->flags & CTL_LLF_COMPARE)
1389 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1391 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1392 io->scsiio.kern_data_len = beio->io_len;
1393 io->scsiio.kern_data_resid = 0;
1394 io->scsiio.kern_sg_entries = beio->num_segs;
1395 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1398 * For the read case, we need to read the data into our buffers and
1399 * then we can send it back to the user. For the write case, we
1400 * need to get the data from the user first.
1402 if (beio->bio_cmd == BIO_READ) {
1403 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1404 be_lun->dispatch(be_lun, beio);
1406 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1408 getbintime(&io->io_hdr.dma_start_bt);
1415 ctl_be_block_worker(void *context, int pending)
1417 struct ctl_be_block_lun *be_lun;
1418 struct ctl_be_block_softc *softc;
1421 be_lun = (struct ctl_be_block_lun *)context;
1422 softc = be_lun->softc;
1424 DPRINTF("entered\n");
1426 mtx_lock(&be_lun->queue_lock);
1428 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1430 struct ctl_be_block_io *beio;
1432 DPRINTF("datamove queue\n");
1434 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1437 mtx_unlock(&be_lun->queue_lock);
1439 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1441 be_lun->dispatch(be_lun, beio);
1443 mtx_lock(&be_lun->queue_lock);
1446 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1449 DPRINTF("config write queue\n");
1451 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1454 mtx_unlock(&be_lun->queue_lock);
1456 ctl_be_block_cw_dispatch(be_lun, io);
1458 mtx_lock(&be_lun->queue_lock);
1461 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1463 DPRINTF("input queue\n");
1465 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1467 mtx_unlock(&be_lun->queue_lock);
1470 * We must drop the lock, since this routine and
1471 * its children may sleep.
1473 ctl_be_block_dispatch(be_lun, io);
1475 mtx_lock(&be_lun->queue_lock);
1480 * If we get here, there is no work left in the queues, so
1481 * just break out and let the task queue go to sleep.
1485 mtx_unlock(&be_lun->queue_lock);
1489 * Entry point from CTL to the backend for I/O. We queue everything to a
1490 * work thread, so this just puts the I/O on a queue and wakes up the
1494 ctl_be_block_submit(union ctl_io *io)
1496 struct ctl_be_block_lun *be_lun;
1497 struct ctl_be_lun *ctl_be_lun;
1499 DPRINTF("entered\n");
1501 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1502 CTL_PRIV_BACKEND_LUN].ptr;
1503 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1506 * Make sure we only get SCSI I/O.
1508 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1509 "%#x) encountered", io->io_hdr.io_type));
1513 mtx_lock(&be_lun->queue_lock);
1515 * XXX KDM make sure that links is okay to use at this point.
1516 * Otherwise, we either need to add another field to ctl_io_hdr,
1517 * or deal with resource allocation here.
1519 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1520 mtx_unlock(&be_lun->queue_lock);
1521 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1523 return (CTL_RETVAL_COMPLETE);
1527 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1528 int flag, struct thread *td)
1530 struct ctl_be_block_softc *softc;
1533 softc = &backend_block_softc;
1539 struct ctl_lun_req *lun_req;
1541 lun_req = (struct ctl_lun_req *)addr;
1543 switch (lun_req->reqtype) {
1544 case CTL_LUNREQ_CREATE:
1545 error = ctl_be_block_create(softc, lun_req);
1548 error = ctl_be_block_rm(softc, lun_req);
1550 case CTL_LUNREQ_MODIFY:
1551 error = ctl_be_block_modify(softc, lun_req);
1554 lun_req->status = CTL_LUN_ERROR;
1555 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1556 "%s: invalid LUN request type %d", __func__,
1571 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1573 struct ctl_be_block_filedata *file_data;
1574 struct ctl_lun_create_params *params;
1579 file_data = &be_lun->backend.file;
1580 params = &req->reqdata.create;
1582 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1583 be_lun->dispatch = ctl_be_block_dispatch_file;
1584 be_lun->lun_flush = ctl_be_block_flush_file;
1586 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1588 snprintf(req->error_str, sizeof(req->error_str),
1589 "error calling VOP_GETATTR() for file %s",
1595 * Verify that we have the ability to upgrade to exclusive
1596 * access on this file so we can trap errors at open instead
1597 * of reporting them during first access.
1599 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1600 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1601 if (be_lun->vn->v_iflag & VI_DOOMED) {
1603 snprintf(req->error_str, sizeof(req->error_str),
1604 "error locking file %s", be_lun->dev_path);
1610 file_data->cred = crhold(curthread->td_ucred);
1611 if (params->lun_size_bytes != 0)
1612 be_lun->size_bytes = params->lun_size_bytes;
1614 be_lun->size_bytes = vattr.va_size;
1616 * We set the multi thread flag for file operations because all
1617 * filesystems (in theory) are capable of allowing multiple readers
1618 * of a file at once. So we want to get the maximum possible
1621 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1624 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
1625 * With ZFS, it is 131072 bytes. Block sizes that large don't work
1626 * with disklabel and UFS on FreeBSD at least. Large block sizes
1627 * may not work with other OSes as well. So just export a sector
1628 * size of 512 bytes, which should work with any OS or
1629 * application. Since our backing is a file, any block size will
1630 * work fine for the backing store.
1633 be_lun->blocksize= vattr.va_blocksize;
1635 if (params->blocksize_bytes != 0)
1636 be_lun->blocksize = params->blocksize_bytes;
1638 be_lun->blocksize = 512;
1641 * Sanity check. The media size has to be at least one
1644 if (be_lun->size_bytes < be_lun->blocksize) {
1646 snprintf(req->error_str, sizeof(req->error_str),
1647 "file %s size %ju < block size %u", be_lun->dev_path,
1648 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1654 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1656 struct ctl_lun_create_params *params;
1659 struct cdevsw *devsw;
1661 off_t ps, pss, po, pos;
1663 params = &req->reqdata.create;
1665 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1666 be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1667 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1668 &be_lun->backend.dev.dev_ref);
1669 if (be_lun->backend.dev.csw == NULL)
1670 panic("Unable to retrieve device switch");
1671 if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0)
1672 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1674 be_lun->dispatch = ctl_be_block_dispatch_dev;
1675 be_lun->lun_flush = ctl_be_block_flush_dev;
1676 be_lun->unmap = ctl_be_block_unmap_dev;
1678 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1680 snprintf(req->error_str, sizeof(req->error_str),
1681 "%s: error getting vnode attributes for device %s",
1682 __func__, be_lun->dev_path);
1686 dev = be_lun->vn->v_rdev;
1687 devsw = dev->si_devsw;
1688 if (!devsw->d_ioctl) {
1689 snprintf(req->error_str, sizeof(req->error_str),
1690 "%s: no d_ioctl for device %s!", __func__,
1695 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1696 (caddr_t)&be_lun->blocksize, FREAD,
1699 snprintf(req->error_str, sizeof(req->error_str),
1700 "%s: error %d returned for DIOCGSECTORSIZE ioctl "
1701 "on %s!", __func__, error, be_lun->dev_path);
1706 * If the user has asked for a blocksize that is greater than the
1707 * backing device's blocksize, we can do it only if the blocksize
1708 * the user is asking for is an even multiple of the underlying
1709 * device's blocksize.
1711 if ((params->blocksize_bytes != 0)
1712 && (params->blocksize_bytes > be_lun->blocksize)) {
1713 uint32_t bs_multiple, tmp_blocksize;
1715 bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1717 tmp_blocksize = bs_multiple * be_lun->blocksize;
1719 if (tmp_blocksize == params->blocksize_bytes) {
1720 be_lun->blocksize = params->blocksize_bytes;
1722 snprintf(req->error_str, sizeof(req->error_str),
1723 "%s: requested blocksize %u is not an even "
1724 "multiple of backing device blocksize %u",
1725 __func__, params->blocksize_bytes,
1730 } else if ((params->blocksize_bytes != 0)
1731 && (params->blocksize_bytes != be_lun->blocksize)) {
1732 snprintf(req->error_str, sizeof(req->error_str),
1733 "%s: requested blocksize %u < backing device "
1734 "blocksize %u", __func__, params->blocksize_bytes,
1739 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1740 (caddr_t)&be_lun->size_bytes, FREAD,
1743 snprintf(req->error_str, sizeof(req->error_str),
1744 "%s: error %d returned for DIOCGMEDIASIZE "
1745 " ioctl on %s!", __func__, error,
1750 if (params->lun_size_bytes != 0) {
1751 if (params->lun_size_bytes > be_lun->size_bytes) {
1752 snprintf(req->error_str, sizeof(req->error_str),
1753 "%s: requested LUN size %ju > backing device "
1754 "size %ju", __func__,
1755 (uintmax_t)params->lun_size_bytes,
1756 (uintmax_t)be_lun->size_bytes);
1760 be_lun->size_bytes = params->lun_size_bytes;
1763 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1764 (caddr_t)&ps, FREAD, curthread);
1768 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1769 (caddr_t)&po, FREAD, curthread);
1773 pss = ps / be_lun->blocksize;
1774 pos = po / be_lun->blocksize;
1775 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1776 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1777 be_lun->pblockexp = fls(pss) - 1;
1778 be_lun->pblockoff = (pss - pos) % pss;
1785 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
1789 int flags = FREAD | FWRITE;
1791 switch (be_lun->dev_type) {
1792 case CTL_BE_BLOCK_DEV:
1793 if (be_lun->backend.dev.csw) {
1794 dev_relthread(be_lun->backend.dev.cdev,
1795 be_lun->backend.dev.dev_ref);
1796 be_lun->backend.dev.csw = NULL;
1797 be_lun->backend.dev.cdev = NULL;
1800 case CTL_BE_BLOCK_FILE:
1802 case CTL_BE_BLOCK_NONE:
1805 panic("Unexpected backend type.");
1809 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1812 switch (be_lun->dev_type) {
1813 case CTL_BE_BLOCK_DEV:
1815 case CTL_BE_BLOCK_FILE:
1816 if (be_lun->backend.file.cred != NULL) {
1817 crfree(be_lun->backend.file.cred);
1818 be_lun->backend.file.cred = NULL;
1821 case CTL_BE_BLOCK_NONE:
1824 panic("Unexpected backend type.");
1834 ctl_be_block_open(struct ctl_be_block_softc *softc,
1835 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1837 struct nameidata nd;
1842 * XXX KDM allow a read-only option?
1844 flags = FREAD | FWRITE;
1847 if (rootvnode == NULL) {
1848 snprintf(req->error_str, sizeof(req->error_str),
1849 "%s: Root filesystem is not mounted", __func__);
1853 if (!curthread->td_proc->p_fd->fd_cdir) {
1854 curthread->td_proc->p_fd->fd_cdir = rootvnode;
1857 if (!curthread->td_proc->p_fd->fd_rdir) {
1858 curthread->td_proc->p_fd->fd_rdir = rootvnode;
1861 if (!curthread->td_proc->p_fd->fd_jdir) {
1862 curthread->td_proc->p_fd->fd_jdir = rootvnode;
1867 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1868 error = vn_open(&nd, &flags, 0, NULL);
1871 * This is the only reasonable guess we can make as far as
1872 * path if the user doesn't give us a fully qualified path.
1873 * If they want to specify a file, they need to specify the
1876 if (be_lun->dev_path[0] != '/') {
1877 char *dev_path = "/dev/";
1880 /* Try adding device path at beginning of name */
1881 dev_name = malloc(strlen(be_lun->dev_path)
1882 + strlen(dev_path) + 1,
1883 M_CTLBLK, M_WAITOK);
1885 sprintf(dev_name, "%s%s", dev_path,
1887 free(be_lun->dev_path, M_CTLBLK);
1888 be_lun->dev_path = dev_name;
1892 snprintf(req->error_str, sizeof(req->error_str),
1893 "%s: error opening %s", __func__, be_lun->dev_path);
1897 NDFREE(&nd, NDF_ONLY_PNBUF);
1899 be_lun->vn = nd.ni_vp;
1901 /* We only support disks and files. */
1902 if (vn_isdisk(be_lun->vn, &error)) {
1903 error = ctl_be_block_open_dev(be_lun, req);
1904 } else if (be_lun->vn->v_type == VREG) {
1905 error = ctl_be_block_open_file(be_lun, req);
1908 snprintf(req->error_str, sizeof(req->error_str),
1909 "%s is not a disk or plain file", be_lun->dev_path);
1911 VOP_UNLOCK(be_lun->vn, 0);
1914 ctl_be_block_close(be_lun);
1918 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1919 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1925 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1927 struct ctl_be_block_lun *be_lun;
1928 struct ctl_lun_create_params *params;
1929 char num_thread_str[16];
1932 int retval, num_threads, unmap;
1933 int tmp_num_threads;
1935 params = &req->reqdata.create;
1938 num_threads = cbb_num_threads;
1940 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1942 be_lun->softc = softc;
1943 STAILQ_INIT(&be_lun->input_queue);
1944 STAILQ_INIT(&be_lun->config_write_queue);
1945 STAILQ_INIT(&be_lun->datamove_queue);
1946 sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1947 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
1948 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
1949 ctl_init_opts(&be_lun->ctl_be_lun.options,
1950 req->num_be_args, req->kern_be_args);
1952 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1953 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1955 if (be_lun->lun_zone == NULL) {
1956 snprintf(req->error_str, sizeof(req->error_str),
1957 "%s: error allocating UMA zone", __func__);
1961 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1962 be_lun->ctl_be_lun.lun_type = params->device_type;
1964 be_lun->ctl_be_lun.lun_type = T_DIRECT;
1966 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1967 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
1968 if (value == NULL) {
1969 snprintf(req->error_str, sizeof(req->error_str),
1970 "%s: no file argument specified", __func__);
1973 be_lun->dev_path = strdup(value, M_CTLBLK);
1975 retval = ctl_be_block_open(softc, be_lun, req);
1982 * Tell the user the size of the file/device.
1984 params->lun_size_bytes = be_lun->size_bytes;
1987 * The maximum LBA is the size - 1.
1989 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
1992 * For processor devices, we don't have any size.
1994 be_lun->blocksize = 0;
1995 be_lun->pblockexp = 0;
1996 be_lun->pblockoff = 0;
1997 be_lun->size_blocks = 0;
1998 be_lun->size_bytes = 0;
1999 be_lun->ctl_be_lun.maxlba = 0;
2000 params->lun_size_bytes = 0;
2003 * Default to just 1 thread for processor devices.
2009 * XXX This searching loop might be refactored to be combined with
2012 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
2013 if (value != NULL) {
2014 tmp_num_threads = strtol(value, NULL, 0);
2017 * We don't let the user specify less than one
2018 * thread, but hope he's clueful enough not to
2019 * specify 1000 threads.
2021 if (tmp_num_threads < 1) {
2022 snprintf(req->error_str, sizeof(req->error_str),
2023 "%s: invalid number of threads %s",
2024 __func__, num_thread_str);
2027 num_threads = tmp_num_threads;
2030 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
2031 if (value != NULL && strcmp(value, "on") == 0)
2034 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2035 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2037 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2038 be_lun->ctl_be_lun.be_lun = be_lun;
2039 be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2040 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2041 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2042 /* Tell the user the blocksize we ended up using */
2043 params->blocksize_bytes = be_lun->blocksize;
2044 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2045 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2046 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2048 be_lun->ctl_be_lun.req_lun_id = 0;
2050 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2051 be_lun->ctl_be_lun.lun_config_status =
2052 ctl_be_block_lun_config_status;
2053 be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2055 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2056 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2058 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2059 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2062 /* Tell the user what we used for a serial number */
2063 strncpy((char *)params->serial_num, tmpstr,
2064 ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2066 strncpy((char *)be_lun->ctl_be_lun.serial_num,
2068 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2069 sizeof(params->serial_num)));
2071 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2072 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2073 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2074 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2077 /* Tell the user what we used for a device ID */
2078 strncpy((char *)params->device_id, tmpstr,
2079 ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2081 strncpy((char *)be_lun->ctl_be_lun.device_id,
2083 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2084 sizeof(params->device_id)));
2087 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2089 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2090 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2092 if (be_lun->io_taskqueue == NULL) {
2093 snprintf(req->error_str, sizeof(req->error_str),
2094 "%s: Unable to create taskqueue", __func__);
2099 * Note that we start the same number of threads by default for
2100 * both the file case and the block device case. For the file
2101 * case, we need multiple threads to allow concurrency, because the
2102 * vnode interface is designed to be a blocking interface. For the
2103 * block device case, ZFS zvols at least will block the caller's
2104 * context in many instances, and so we need multiple threads to
2105 * overcome that problem. Other block devices don't need as many
2106 * threads, but they shouldn't cause too many problems.
2108 * If the user wants to just have a single thread for a block
2109 * device, he can specify that when the LUN is created, or change
2110 * the tunable/sysctl to alter the default number of threads.
2112 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2113 /*num threads*/num_threads,
2116 "%s taskq", be_lun->lunname);
2121 be_lun->num_threads = num_threads;
2123 mtx_lock(&softc->lock);
2125 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2127 mtx_unlock(&softc->lock);
2129 retval = ctl_add_lun(&be_lun->ctl_be_lun);
2131 mtx_lock(&softc->lock);
2132 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2135 mtx_unlock(&softc->lock);
2136 snprintf(req->error_str, sizeof(req->error_str),
2137 "%s: ctl_add_lun() returned error %d, see dmesg for "
2138 "details", __func__, retval);
2143 mtx_lock(&softc->lock);
2146 * Tell the config_status routine that we're waiting so it won't
2147 * clean up the LUN in the event of an error.
2149 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2151 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2152 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2153 if (retval == EINTR)
2156 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2158 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2159 snprintf(req->error_str, sizeof(req->error_str),
2160 "%s: LUN configuration error, see dmesg for details",
2162 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2165 mtx_unlock(&softc->lock);
2168 params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2171 mtx_unlock(&softc->lock);
2173 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2175 DEVSTAT_ALL_SUPPORTED,
2176 be_lun->ctl_be_lun.lun_type
2177 | DEVSTAT_TYPE_IF_OTHER,
2178 DEVSTAT_PRIORITY_OTHER);
2181 req->status = CTL_LUN_OK;
2186 req->status = CTL_LUN_ERROR;
2188 if (be_lun->io_taskqueue != NULL)
2189 taskqueue_free(be_lun->io_taskqueue);
2190 ctl_be_block_close(be_lun);
2191 if (be_lun->dev_path != NULL)
2192 free(be_lun->dev_path, M_CTLBLK);
2193 if (be_lun->lun_zone != NULL)
2194 uma_zdestroy(be_lun->lun_zone);
2195 ctl_free_opts(&be_lun->ctl_be_lun.options);
2196 mtx_destroy(&be_lun->queue_lock);
2197 mtx_destroy(&be_lun->io_lock);
2198 free(be_lun, M_CTLBLK);
2204 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2206 struct ctl_lun_rm_params *params;
2207 struct ctl_be_block_lun *be_lun;
2210 params = &req->reqdata.rm;
2212 mtx_lock(&softc->lock);
2216 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2217 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2220 mtx_unlock(&softc->lock);
2222 if (be_lun == NULL) {
2223 snprintf(req->error_str, sizeof(req->error_str),
2224 "%s: LUN %u is not managed by the block backend",
2225 __func__, params->lun_id);
2229 retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2232 snprintf(req->error_str, sizeof(req->error_str),
2233 "%s: error %d returned from ctl_disable_lun() for "
2234 "LUN %d", __func__, retval, params->lun_id);
2239 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2241 snprintf(req->error_str, sizeof(req->error_str),
2242 "%s: error %d returned from ctl_invalidate_lun() for "
2243 "LUN %d", __func__, retval, params->lun_id);
2247 mtx_lock(&softc->lock);
2249 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2251 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2252 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2253 if (retval == EINTR)
2257 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2259 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2260 snprintf(req->error_str, sizeof(req->error_str),
2261 "%s: interrupted waiting for LUN to be freed",
2263 mtx_unlock(&softc->lock);
2267 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2270 mtx_unlock(&softc->lock);
2272 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2274 taskqueue_free(be_lun->io_taskqueue);
2276 ctl_be_block_close(be_lun);
2278 if (be_lun->disk_stats != NULL)
2279 devstat_remove_entry(be_lun->disk_stats);
2281 uma_zdestroy(be_lun->lun_zone);
2283 ctl_free_opts(&be_lun->ctl_be_lun.options);
2284 free(be_lun->dev_path, M_CTLBLK);
2285 mtx_destroy(&be_lun->queue_lock);
2286 mtx_destroy(&be_lun->io_lock);
2287 free(be_lun, M_CTLBLK);
2289 req->status = CTL_LUN_OK;
2295 req->status = CTL_LUN_ERROR;
2301 ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2302 struct ctl_lun_req *req)
2306 struct ctl_lun_modify_params *params;
2308 params = &req->reqdata.modify;
2310 if (params->lun_size_bytes != 0) {
2311 be_lun->size_bytes = params->lun_size_bytes;
2313 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2314 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2315 VOP_UNLOCK(be_lun->vn, 0);
2317 snprintf(req->error_str, sizeof(req->error_str),
2318 "error calling VOP_GETATTR() for file %s",
2323 be_lun->size_bytes = vattr.va_size;
2330 ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2331 struct ctl_lun_req *req)
2333 struct ctl_be_block_devdata *dev_data;
2335 struct ctl_lun_modify_params *params;
2336 uint64_t size_bytes;
2338 params = &req->reqdata.modify;
2340 dev_data = &be_lun->backend.dev;
2341 if (!dev_data->csw->d_ioctl) {
2342 snprintf(req->error_str, sizeof(req->error_str),
2343 "%s: no d_ioctl for device %s!", __func__,
2348 error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2349 (caddr_t)&size_bytes, FREAD,
2352 snprintf(req->error_str, sizeof(req->error_str),
2353 "%s: error %d returned for DIOCGMEDIASIZE ioctl "
2354 "on %s!", __func__, error, be_lun->dev_path);
2358 if (params->lun_size_bytes != 0) {
2359 if (params->lun_size_bytes > size_bytes) {
2360 snprintf(req->error_str, sizeof(req->error_str),
2361 "%s: requested LUN size %ju > backing device "
2362 "size %ju", __func__,
2363 (uintmax_t)params->lun_size_bytes,
2364 (uintmax_t)size_bytes);
2368 be_lun->size_bytes = params->lun_size_bytes;
2370 be_lun->size_bytes = size_bytes;
2377 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2379 struct ctl_lun_modify_params *params;
2380 struct ctl_be_block_lun *be_lun;
2384 params = &req->reqdata.modify;
2386 mtx_lock(&softc->lock);
2390 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2391 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2394 mtx_unlock(&softc->lock);
2396 if (be_lun == NULL) {
2397 snprintf(req->error_str, sizeof(req->error_str),
2398 "%s: LUN %u is not managed by the block backend",
2399 __func__, params->lun_id);
2403 if (params->lun_size_bytes != 0) {
2404 if (params->lun_size_bytes < be_lun->blocksize) {
2405 snprintf(req->error_str, sizeof(req->error_str),
2406 "%s: LUN size %ju < blocksize %u", __func__,
2407 params->lun_size_bytes, be_lun->blocksize);
2412 oldsize = be_lun->size_bytes;
2413 if (be_lun->vn->v_type == VREG)
2414 error = ctl_be_block_modify_file(be_lun, req);
2416 error = ctl_be_block_modify_dev(be_lun, req);
2420 if (be_lun->size_bytes != oldsize) {
2421 be_lun->size_blocks = be_lun->size_bytes >>
2422 be_lun->blocksize_shift;
2425 * The maximum LBA is the size - 1.
2427 * XXX: Note that this field is being updated without locking,
2428 * which might cause problems on 32-bit architectures.
2430 be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
2431 ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2434 /* Tell the user the exact size we ended up using */
2435 params->lun_size_bytes = be_lun->size_bytes;
2437 req->status = CTL_LUN_OK;
2442 req->status = CTL_LUN_ERROR;
2448 ctl_be_block_lun_shutdown(void *be_lun)
2450 struct ctl_be_block_lun *lun;
2451 struct ctl_be_block_softc *softc;
2453 lun = (struct ctl_be_block_lun *)be_lun;
2457 mtx_lock(&softc->lock);
2458 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2459 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2461 mtx_unlock(&softc->lock);
2466 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2468 struct ctl_be_block_lun *lun;
2469 struct ctl_be_block_softc *softc;
2471 lun = (struct ctl_be_block_lun *)be_lun;
2474 if (status == CTL_LUN_CONFIG_OK) {
2475 mtx_lock(&softc->lock);
2476 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2477 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2479 mtx_unlock(&softc->lock);
2482 * We successfully added the LUN, attempt to enable it.
2484 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2485 printf("%s: ctl_enable_lun() failed!\n", __func__);
2486 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2487 printf("%s: ctl_invalidate_lun() failed!\n",
2496 mtx_lock(&softc->lock);
2497 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2498 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2500 mtx_unlock(&softc->lock);
2505 ctl_be_block_config_write(union ctl_io *io)
2507 struct ctl_be_block_lun *be_lun;
2508 struct ctl_be_lun *ctl_be_lun;
2513 DPRINTF("entered\n");
2515 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2516 CTL_PRIV_BACKEND_LUN].ptr;
2517 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2519 switch (io->scsiio.cdb[0]) {
2520 case SYNCHRONIZE_CACHE:
2521 case SYNCHRONIZE_CACHE_16:
2526 * The upper level CTL code will filter out any CDBs with
2527 * the immediate bit set and return the proper error.
2529 * We don't really need to worry about what LBA range the
2530 * user asked to be synced out. When they issue a sync
2531 * cache command, we'll sync out the whole thing.
2533 mtx_lock(&be_lun->queue_lock);
2534 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2536 mtx_unlock(&be_lun->queue_lock);
2537 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2539 case START_STOP_UNIT: {
2540 struct scsi_start_stop_unit *cdb;
2542 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2544 if (cdb->how & SSS_START)
2545 retval = ctl_start_lun(ctl_be_lun);
2547 retval = ctl_stop_lun(ctl_be_lun);
2549 * XXX KDM Copan-specific offline behavior.
2550 * Figure out a reasonable way to port this?
2554 && (cdb->byte2 & SSS_ONOFFLINE))
2555 retval = ctl_lun_offline(ctl_be_lun);
2560 * In general, the above routines should not fail. They
2561 * just set state for the LUN. So we've got something
2562 * pretty wrong here if we can't start or stop the LUN.
2565 ctl_set_internal_failure(&io->scsiio,
2567 /*retry_count*/ 0xf051);
2568 retval = CTL_RETVAL_COMPLETE;
2570 ctl_set_success(&io->scsiio);
2572 ctl_config_write_done(io);
2576 ctl_set_invalid_opcode(&io->scsiio);
2577 ctl_config_write_done(io);
2578 retval = CTL_RETVAL_COMPLETE;
2587 ctl_be_block_config_read(union ctl_io *io)
2593 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2595 struct ctl_be_block_lun *lun;
2598 lun = (struct ctl_be_block_lun *)be_lun;
2601 retval = sbuf_printf(sb, "\t<num_threads>");
2606 retval = sbuf_printf(sb, "%d", lun->num_threads);
2611 retval = sbuf_printf(sb, "</num_threads>\n");
2619 ctl_be_block_init(void)
2621 struct ctl_be_block_softc *softc;
2624 softc = &backend_block_softc;
2627 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2628 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2629 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2630 STAILQ_INIT(&softc->disk_list);
2631 STAILQ_INIT(&softc->lun_list);