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 <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/types.h>
49 #include <sys/kthread.h>
51 #include <sys/fcntl.h>
52 #include <sys/limits.h>
54 #include <sys/mutex.h>
55 #include <sys/condvar.h>
56 #include <sys/malloc.h>
58 #include <sys/ioccom.h>
59 #include <sys/queue.h>
61 #include <sys/endian.h>
64 #include <sys/taskqueue.h>
65 #include <sys/vnode.h>
66 #include <sys/namei.h>
67 #include <sys/mount.h>
69 #include <sys/fcntl.h>
70 #include <sys/filedesc.h>
73 #include <sys/module.h>
75 #include <sys/devicestat.h>
76 #include <sys/sysctl.h>
78 #include <geom/geom.h>
81 #include <cam/scsi/scsi_all.h>
82 #include <cam/scsi/scsi_da.h>
83 #include <cam/ctl/ctl_io.h>
84 #include <cam/ctl/ctl.h>
85 #include <cam/ctl/ctl_backend.h>
86 #include <cam/ctl/ctl_frontend_internal.h>
87 #include <cam/ctl/ctl_ioctl.h>
88 #include <cam/ctl/ctl_scsi_all.h>
89 #include <cam/ctl/ctl_error.h>
92 * The idea here is that we'll allocate enough S/G space to hold a 1MB
93 * I/O. If we get an I/O larger than that, we'll split it.
95 #define CTLBLK_HALF_IO_SIZE (512 * 1024)
96 #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2)
97 #define CTLBLK_MAX_SEG MAXPHYS
98 #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
99 #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2)
102 #define DPRINTF(fmt, args...) \
103 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
105 #define DPRINTF(fmt, args...) do {} while(0)
109 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
111 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
113 SDT_PROVIDER_DEFINE(cbb);
116 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01,
117 CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02,
118 CTL_BE_BLOCK_LUN_WAITING = 0x04,
119 CTL_BE_BLOCK_LUN_MULTI_THREAD = 0x08
120 } ctl_be_block_lun_flags;
128 struct ctl_be_block_devdata {
134 struct ctl_be_block_filedata {
138 union ctl_be_block_bedata {
139 struct ctl_be_block_devdata dev;
140 struct ctl_be_block_filedata file;
143 struct ctl_be_block_io;
144 struct ctl_be_block_lun;
146 typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
147 struct ctl_be_block_io *beio);
148 typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
149 const char *attrname);
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_lun_create_params params;
157 struct ctl_block_disk *disk;
160 ctl_be_block_type dev_type;
162 union ctl_be_block_bedata backend;
163 cbb_dispatch_t dispatch;
164 cbb_dispatch_t lun_flush;
165 cbb_dispatch_t unmap;
166 cbb_getattr_t getattr;
168 uint64_t size_blocks;
174 struct ctl_be_block_softc *softc;
175 struct devstat *disk_stats;
176 ctl_be_block_lun_flags flags;
177 STAILQ_ENTRY(ctl_be_block_lun) links;
178 struct ctl_be_lun ctl_be_lun;
179 struct taskqueue *io_taskqueue;
182 STAILQ_HEAD(, ctl_io_hdr) input_queue;
183 STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
184 STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
185 struct mtx_padalign io_lock;
186 struct mtx_padalign queue_lock;
190 * Overall softc structure for the block backend module.
192 struct ctl_be_block_softc {
195 STAILQ_HEAD(, ctl_block_disk) disk_list;
197 STAILQ_HEAD(, ctl_be_block_lun) lun_list;
200 static struct ctl_be_block_softc backend_block_softc;
203 * Per-I/O information.
205 struct ctl_be_block_io {
207 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS];
208 struct iovec xiovecs[CTLBLK_MAX_SEGS];
215 struct bintime ds_t0;
216 devstat_tag_type ds_tag_type;
217 devstat_trans_flags ds_trans_type;
220 struct ctl_be_block_softc *softc;
221 struct ctl_be_block_lun *lun;
222 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
225 static int cbb_num_threads = 14;
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_RWTUN,
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 uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
247 const char *attrname);
248 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
250 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
252 static void ctl_be_block_worker(void *context, int pending);
253 static int ctl_be_block_submit(union ctl_io *io);
254 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
255 int flag, struct thread *td);
256 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
257 struct ctl_lun_req *req);
258 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
259 struct ctl_lun_req *req);
260 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
261 static int ctl_be_block_open(struct ctl_be_block_softc *softc,
262 struct ctl_be_block_lun *be_lun,
263 struct ctl_lun_req *req);
264 static int ctl_be_block_create(struct ctl_be_block_softc *softc,
265 struct ctl_lun_req *req);
266 static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
267 struct ctl_lun_req *req);
268 static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
269 struct ctl_lun_req *req);
270 static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
271 struct ctl_lun_req *req);
272 static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
273 struct ctl_lun_req *req);
274 static void ctl_be_block_lun_shutdown(void *be_lun);
275 static void ctl_be_block_lun_config_status(void *be_lun,
276 ctl_lun_config_status status);
277 static int ctl_be_block_config_write(union ctl_io *io);
278 static int ctl_be_block_config_read(union ctl_io *io);
279 static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
280 static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
281 int ctl_be_block_init(void);
283 static struct ctl_backend_driver ctl_be_block_driver =
286 .flags = CTL_BE_FLAG_HAS_CONFIG,
287 .init = ctl_be_block_init,
288 .data_submit = ctl_be_block_submit,
289 .data_move_done = ctl_be_block_move_done,
290 .config_read = ctl_be_block_config_read,
291 .config_write = ctl_be_block_config_write,
292 .ioctl = ctl_be_block_ioctl,
293 .lun_info = ctl_be_block_lun_info,
294 .lun_attr = ctl_be_block_lun_attr
297 MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
298 CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
300 static uma_zone_t beio_zone;
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(beio_zone, M_WAITOK | M_ZERO);
313 ctl_free_beio(struct ctl_be_block_io *beio)
320 for (i = 0; i < beio->num_segs; i++) {
321 if (beio->sg_segs[i].addr == NULL)
324 uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
325 beio->sg_segs[i].addr = NULL;
327 /* For compare we had two equal S/G lists. */
328 if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
329 uma_zfree(beio->lun->lun_zone,
330 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
331 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
335 if (duplicate_free > 0) {
336 printf("%s: %d duplicate frees out of %d segments\n", __func__,
337 duplicate_free, beio->num_segs);
340 uma_zfree(beio_zone, beio);
344 ctl_complete_beio(struct ctl_be_block_io *beio)
346 union ctl_io *io = beio->io;
348 if (beio->beio_cont != NULL) {
349 beio->beio_cont(beio);
352 ctl_data_submit_done(io);
357 ctl_be_block_move_done(union ctl_io *io)
359 struct ctl_be_block_io *beio;
360 struct ctl_be_block_lun *be_lun;
361 struct ctl_lba_len_flags *lbalen;
363 struct bintime cur_bt;
367 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
370 DPRINTF("entered\n");
374 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
375 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
376 io->io_hdr.num_dmas++;
378 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
381 * We set status at this point for read commands, and write
382 * commands with errors.
384 if ((io->io_hdr.port_status == 0) &&
385 ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) &&
386 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
387 lbalen = ARGS(beio->io);
388 if (lbalen->flags & CTL_LLF_READ) {
389 ctl_set_success(&io->scsiio);
390 } else if (lbalen->flags & CTL_LLF_COMPARE) {
391 /* We have two data blocks ready for comparison. */
392 for (i = 0; i < beio->num_segs; i++) {
393 if (memcmp(beio->sg_segs[i].addr,
394 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
395 beio->sg_segs[i].len) != 0)
398 if (i < beio->num_segs)
399 ctl_set_sense(&io->scsiio,
401 /*sense_key*/ SSD_KEY_MISCOMPARE,
406 ctl_set_success(&io->scsiio);
409 else if ((io->io_hdr.port_status != 0)
410 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
411 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
413 * For hardware error sense keys, the sense key
414 * specific value is defined to be a retry count,
415 * but we use it to pass back an internal FETD
416 * error code. XXX KDM Hopefully the FETD is only
417 * using 16 bits for an error code, since that's
418 * all the space we have in the sks field.
420 ctl_set_internal_failure(&io->scsiio,
423 io->io_hdr.port_status);
427 * If this is a read, or a write with errors, it is done.
429 if ((beio->bio_cmd == BIO_READ)
430 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
431 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
432 ctl_complete_beio(beio);
437 * At this point, we have a write and the DMA completed
438 * successfully. We now have to queue it to the task queue to
439 * execute the backend I/O. That is because we do blocking
440 * memory allocations, and in the file backing case, blocking I/O.
441 * This move done routine is generally called in the SIM's
442 * interrupt context, and therefore we cannot block.
444 mtx_lock(&be_lun->queue_lock);
446 * XXX KDM make sure that links is okay to use at this point.
447 * Otherwise, we either need to add another field to ctl_io_hdr,
448 * or deal with resource allocation here.
450 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
451 mtx_unlock(&be_lun->queue_lock);
453 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
459 ctl_be_block_biodone(struct bio *bio)
461 struct ctl_be_block_io *beio;
462 struct ctl_be_block_lun *be_lun;
466 beio = bio->bio_caller1;
470 DPRINTF("entered\n");
472 error = bio->bio_error;
473 mtx_lock(&be_lun->io_lock);
477 beio->num_bios_done++;
480 * XXX KDM will this cause WITNESS to complain? Holding a lock
481 * during the free might cause it to complain.
486 * If the send complete bit isn't set, or we aren't the last I/O to
487 * complete, then we're done.
489 if ((beio->send_complete == 0)
490 || (beio->num_bios_done < beio->num_bios_sent)) {
491 mtx_unlock(&be_lun->io_lock);
496 * At this point, we've verified that we are the last I/O to
497 * complete, so it's safe to drop the lock.
499 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
500 beio->ds_tag_type, beio->ds_trans_type,
501 /*now*/ NULL, /*then*/&beio->ds_t0);
502 mtx_unlock(&be_lun->io_lock);
505 * If there are any errors from the backing device, we fail the
506 * entire I/O with a medium error.
508 if (beio->num_errors > 0) {
509 if (error == EOPNOTSUPP) {
510 ctl_set_invalid_opcode(&io->scsiio);
511 } else if (error == ENOSPC) {
512 ctl_set_space_alloc_fail(&io->scsiio);
513 } else if (beio->bio_cmd == BIO_FLUSH) {
514 /* XXX KDM is there is a better error here? */
515 ctl_set_internal_failure(&io->scsiio,
517 /*retry_count*/ 0xbad2);
519 ctl_set_medium_error(&io->scsiio);
520 ctl_complete_beio(beio);
525 * If this is a write, a flush, a delete or verify, we're all done.
526 * If this is a read, we can now send the data to the user.
528 if ((beio->bio_cmd == BIO_WRITE)
529 || (beio->bio_cmd == BIO_FLUSH)
530 || (beio->bio_cmd == BIO_DELETE)
531 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
532 ctl_set_success(&io->scsiio);
533 ctl_complete_beio(beio);
536 getbintime(&io->io_hdr.dma_start_bt);
543 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
544 struct ctl_be_block_io *beio)
546 union ctl_io *io = beio->io;
547 struct mount *mountpoint;
548 int error, lock_flags;
550 DPRINTF("entered\n");
552 binuptime(&beio->ds_t0);
553 mtx_lock(&be_lun->io_lock);
554 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
555 mtx_unlock(&be_lun->io_lock);
557 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
559 if (MNT_SHARED_WRITES(mountpoint)
560 || ((mountpoint == NULL)
561 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
562 lock_flags = LK_SHARED;
564 lock_flags = LK_EXCLUSIVE;
566 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
568 error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
569 VOP_UNLOCK(be_lun->vn, 0);
571 vn_finished_write(mountpoint);
573 mtx_lock(&be_lun->io_lock);
574 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
575 beio->ds_tag_type, beio->ds_trans_type,
576 /*now*/ NULL, /*then*/&beio->ds_t0);
577 mtx_unlock(&be_lun->io_lock);
580 ctl_set_success(&io->scsiio);
582 /* XXX KDM is there is a better error here? */
583 ctl_set_internal_failure(&io->scsiio,
585 /*retry_count*/ 0xbad1);
588 ctl_complete_beio(beio);
591 SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
592 SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
593 SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
594 SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
597 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
598 struct ctl_be_block_io *beio)
600 struct ctl_be_block_filedata *file_data;
603 struct iovec *xiovec;
607 DPRINTF("entered\n");
609 file_data = &be_lun->backend.file;
612 if (ARGS(io)->flags & CTL_LLF_DPO)
614 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
617 bzero(&xuio, sizeof(xuio));
618 if (beio->bio_cmd == BIO_READ) {
619 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
620 xuio.uio_rw = UIO_READ;
622 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
623 xuio.uio_rw = UIO_WRITE;
625 xuio.uio_offset = beio->io_offset;
626 xuio.uio_resid = beio->io_len;
627 xuio.uio_segflg = UIO_SYSSPACE;
628 xuio.uio_iov = beio->xiovecs;
629 xuio.uio_iovcnt = beio->num_segs;
630 xuio.uio_td = curthread;
632 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
633 xiovec->iov_base = beio->sg_segs[i].addr;
634 xiovec->iov_len = beio->sg_segs[i].len;
637 binuptime(&beio->ds_t0);
638 mtx_lock(&be_lun->io_lock);
639 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
640 mtx_unlock(&be_lun->io_lock);
642 if (beio->bio_cmd == BIO_READ) {
643 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
646 * UFS pays attention to IO_DIRECT for reads. If the
647 * DIRECTIO option is configured into the kernel, it calls
648 * ffs_rawread(). But that only works for single-segment
649 * uios with user space addresses. In our case, with a
650 * kernel uio, it still reads into the buffer cache, but it
651 * will just try to release the buffer from the cache later
654 * ZFS does not pay attention to IO_DIRECT for reads.
656 * UFS does not pay attention to IO_SYNC for reads.
658 * ZFS pays attention to IO_SYNC (which translates into the
659 * Solaris define FRSYNC for zfs_read()) for reads. It
660 * attempts to sync the file before reading.
662 * So, to attempt to provide some barrier semantics in the
663 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
665 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
667 VOP_UNLOCK(be_lun->vn, 0);
668 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
670 struct mount *mountpoint;
673 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
675 if (MNT_SHARED_WRITES(mountpoint)
676 || ((mountpoint == NULL)
677 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
678 lock_flags = LK_SHARED;
680 lock_flags = LK_EXCLUSIVE;
682 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
685 * UFS pays attention to IO_DIRECT for writes. The write
686 * is done asynchronously. (Normally the write would just
687 * get put into cache.
689 * UFS pays attention to IO_SYNC for writes. It will
690 * attempt to write the buffer out synchronously if that
693 * ZFS does not pay attention to IO_DIRECT for writes.
695 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
696 * for writes. It will flush the transaction from the
697 * cache before returning.
699 * So if we've got the BIO_ORDERED flag set, we want
700 * IO_SYNC in either the UFS or ZFS case.
702 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
703 VOP_UNLOCK(be_lun->vn, 0);
705 vn_finished_write(mountpoint);
706 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
709 mtx_lock(&be_lun->io_lock);
710 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
711 beio->ds_tag_type, beio->ds_trans_type,
712 /*now*/ NULL, /*then*/&beio->ds_t0);
713 mtx_unlock(&be_lun->io_lock);
716 * If we got an error, set the sense data to "MEDIUM ERROR" and
717 * return the I/O to the user.
722 ctl_scsi_path_string(io, path_str, sizeof(path_str));
723 printf("%s%s command returned errno %d\n", path_str,
724 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
725 if (error == ENOSPC) {
726 ctl_set_space_alloc_fail(&io->scsiio);
728 ctl_set_medium_error(&io->scsiio);
729 ctl_complete_beio(beio);
734 * If this is a write or a verify, we're all done.
735 * If this is a read, we can now send the data to the user.
737 if ((beio->bio_cmd == BIO_WRITE) ||
738 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
739 ctl_set_success(&io->scsiio);
740 ctl_complete_beio(beio);
743 getbintime(&io->io_hdr.dma_start_bt);
750 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
751 struct ctl_be_block_io *beio)
753 struct ctl_be_block_devdata *dev_data;
756 struct iovec *xiovec;
760 DPRINTF("entered\n");
762 dev_data = &be_lun->backend.dev;
765 if (ARGS(io)->flags & CTL_LLF_DPO)
767 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
770 bzero(&xuio, sizeof(xuio));
771 if (beio->bio_cmd == BIO_READ) {
772 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
773 xuio.uio_rw = UIO_READ;
775 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
776 xuio.uio_rw = UIO_WRITE;
778 xuio.uio_offset = beio->io_offset;
779 xuio.uio_resid = beio->io_len;
780 xuio.uio_segflg = UIO_SYSSPACE;
781 xuio.uio_iov = beio->xiovecs;
782 xuio.uio_iovcnt = beio->num_segs;
783 xuio.uio_td = curthread;
785 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
786 xiovec->iov_base = beio->sg_segs[i].addr;
787 xiovec->iov_len = beio->sg_segs[i].len;
790 binuptime(&beio->ds_t0);
791 mtx_lock(&be_lun->io_lock);
792 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
793 mtx_unlock(&be_lun->io_lock);
795 if (beio->bio_cmd == BIO_READ) {
796 error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
797 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
799 error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
800 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
803 mtx_lock(&be_lun->io_lock);
804 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
805 beio->ds_tag_type, beio->ds_trans_type,
806 /*now*/ NULL, /*then*/&beio->ds_t0);
807 mtx_unlock(&be_lun->io_lock);
810 * If we got an error, set the sense data to "MEDIUM ERROR" and
811 * return the I/O to the user.
814 if (error == ENOSPC) {
815 ctl_set_space_alloc_fail(&io->scsiio);
817 ctl_set_medium_error(&io->scsiio);
818 ctl_complete_beio(beio);
823 * If this is a write or a verify, we're all done.
824 * If this is a read, we can now send the data to the user.
826 if ((beio->bio_cmd == BIO_WRITE) ||
827 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
828 ctl_set_success(&io->scsiio);
829 ctl_complete_beio(beio);
832 getbintime(&io->io_hdr.dma_start_bt);
839 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
840 struct ctl_be_block_io *beio)
844 struct ctl_be_block_devdata *dev_data;
846 dev_data = &be_lun->backend.dev;
849 DPRINTF("entered\n");
851 /* This can't fail, it's a blocking allocation. */
854 bio->bio_cmd = BIO_FLUSH;
855 bio->bio_flags |= BIO_ORDERED;
856 bio->bio_dev = dev_data->cdev;
859 bio->bio_done = ctl_be_block_biodone;
860 bio->bio_caller1 = beio;
864 * We don't need to acquire the LUN lock here, because we are only
865 * sending one bio, and so there is no other context to synchronize
868 beio->num_bios_sent = 1;
869 beio->send_complete = 1;
871 binuptime(&beio->ds_t0);
872 mtx_lock(&be_lun->io_lock);
873 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
874 mtx_unlock(&be_lun->io_lock);
876 (*dev_data->csw->d_strategy)(bio);
880 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
881 struct ctl_be_block_io *beio,
882 uint64_t off, uint64_t len, int last)
885 struct ctl_be_block_devdata *dev_data;
888 dev_data = &be_lun->backend.dev;
889 maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
892 bio->bio_cmd = BIO_DELETE;
893 bio->bio_dev = dev_data->cdev;
894 bio->bio_offset = off;
895 bio->bio_length = MIN(len, maxlen);
897 bio->bio_done = ctl_be_block_biodone;
898 bio->bio_caller1 = beio;
899 bio->bio_pblkno = off / be_lun->blocksize;
901 off += bio->bio_length;
902 len -= bio->bio_length;
904 mtx_lock(&be_lun->io_lock);
905 beio->num_bios_sent++;
906 if (last && len == 0)
907 beio->send_complete = 1;
908 mtx_unlock(&be_lun->io_lock);
910 (*dev_data->csw->d_strategy)(bio);
915 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
916 struct ctl_be_block_io *beio)
919 struct ctl_be_block_devdata *dev_data;
920 struct ctl_ptr_len_flags *ptrlen;
921 struct scsi_unmap_desc *buf, *end;
924 dev_data = &be_lun->backend.dev;
927 DPRINTF("entered\n");
929 binuptime(&beio->ds_t0);
930 mtx_lock(&be_lun->io_lock);
931 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
932 mtx_unlock(&be_lun->io_lock);
934 if (beio->io_offset == -1) {
936 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
937 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
938 end = buf + ptrlen->len / sizeof(*buf);
939 for (; buf < end; buf++) {
940 len = (uint64_t)scsi_4btoul(buf->length) *
943 ctl_be_block_unmap_dev_range(be_lun, beio,
944 scsi_8btou64(buf->lba) * be_lun->blocksize, len,
945 (end - buf < 2) ? TRUE : FALSE);
948 ctl_be_block_unmap_dev_range(be_lun, beio,
949 beio->io_offset, beio->io_len, TRUE);
953 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
954 struct ctl_be_block_io *beio)
956 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
959 struct ctl_be_block_devdata *dev_data;
963 DPRINTF("entered\n");
965 dev_data = &be_lun->backend.dev;
968 * We have to limit our I/O size to the maximum supported by the
969 * backend device. Hopefully it is MAXPHYS. If the driver doesn't
970 * set it properly, use DFLTPHYS.
972 max_iosize = dev_data->cdev->si_iosize_max;
973 if (max_iosize < PAGE_SIZE)
974 max_iosize = DFLTPHYS;
976 cur_offset = beio->io_offset;
977 for (i = 0; i < beio->num_segs; i++) {
981 cur_size = beio->sg_segs[i].len;
982 cur_ptr = beio->sg_segs[i].addr;
984 while (cur_size > 0) {
985 /* This can't fail, it's a blocking allocation. */
988 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
990 bio->bio_cmd = beio->bio_cmd;
991 bio->bio_dev = dev_data->cdev;
992 bio->bio_caller1 = beio;
993 bio->bio_length = min(cur_size, max_iosize);
994 bio->bio_offset = cur_offset;
995 bio->bio_data = cur_ptr;
996 bio->bio_done = ctl_be_block_biodone;
997 bio->bio_pblkno = cur_offset / be_lun->blocksize;
999 cur_offset += bio->bio_length;
1000 cur_ptr += bio->bio_length;
1001 cur_size -= bio->bio_length;
1003 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1004 beio->num_bios_sent++;
1007 binuptime(&beio->ds_t0);
1008 mtx_lock(&be_lun->io_lock);
1009 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1010 beio->send_complete = 1;
1011 mtx_unlock(&be_lun->io_lock);
1014 * Fire off all allocated requests!
1016 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1017 TAILQ_REMOVE(&queue, bio, bio_queue);
1018 (*dev_data->csw->d_strategy)(bio);
1023 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1025 struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev;
1026 struct diocgattr_arg arg;
1029 if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL)
1030 return (UINT64_MAX);
1031 strlcpy(arg.name, attrname, sizeof(arg.name));
1032 arg.len = sizeof(arg.value.off);
1033 error = dev_data->csw->d_ioctl(dev_data->cdev,
1034 DIOCGATTR, (caddr_t)&arg, FREAD, curthread);
1036 return (UINT64_MAX);
1037 return (arg.value.off);
1041 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1046 ctl_free_beio(beio);
1047 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1048 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1049 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1050 ctl_config_write_done(io);
1054 ctl_be_block_config_write(io);
1058 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1061 struct ctl_be_block_io *beio;
1062 struct ctl_be_block_softc *softc;
1063 struct ctl_lba_len_flags *lbalen;
1064 uint64_t len_left, lba;
1068 DPRINTF("entered\n");
1070 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1071 softc = be_lun->softc;
1072 lbalen = ARGS(beio->io);
1074 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1075 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1076 ctl_free_beio(beio);
1077 ctl_set_invalid_field(&io->scsiio,
1083 ctl_config_write_done(io);
1087 switch (io->scsiio.tag_type) {
1088 case CTL_TAG_ORDERED:
1089 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1091 case CTL_TAG_HEAD_OF_QUEUE:
1092 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1094 case CTL_TAG_UNTAGGED:
1095 case CTL_TAG_SIMPLE:
1098 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1102 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1103 beio->io_offset = lbalen->lba * be_lun->blocksize;
1104 beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1105 beio->bio_cmd = BIO_DELETE;
1106 beio->ds_trans_type = DEVSTAT_FREE;
1108 be_lun->unmap(be_lun, beio);
1112 beio->bio_cmd = BIO_WRITE;
1113 beio->ds_trans_type = DEVSTAT_WRITE;
1115 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1116 (uintmax_t)lbalen->lba, lbalen->len);
1118 len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1119 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1122 * Setup the S/G entry for this chunk.
1124 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1125 seglen -= seglen % be_lun->blocksize;
1126 beio->sg_segs[i].len = seglen;
1127 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1129 DPRINTF("segment %d addr %p len %zd\n", i,
1130 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1135 buf = beio->sg_segs[i].addr;
1137 for (; buf < end; buf += be_lun->blocksize) {
1138 memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1139 if (lbalen->flags & SWS_LBDATA)
1140 scsi_ulto4b(lbalen->lba + lba, buf);
1145 beio->io_offset = lbalen->lba * be_lun->blocksize;
1146 beio->io_len = lba * be_lun->blocksize;
1148 /* We can not do all in one run. Correct and schedule rerun. */
1152 beio->beio_cont = ctl_be_block_cw_done_ws;
1155 be_lun->dispatch(be_lun, beio);
1159 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1162 struct ctl_be_block_io *beio;
1163 struct ctl_be_block_softc *softc;
1164 struct ctl_ptr_len_flags *ptrlen;
1166 DPRINTF("entered\n");
1168 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1169 softc = be_lun->softc;
1170 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1172 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1173 ctl_free_beio(beio);
1174 ctl_set_invalid_field(&io->scsiio,
1180 ctl_config_write_done(io);
1184 switch (io->scsiio.tag_type) {
1185 case CTL_TAG_ORDERED:
1186 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1188 case CTL_TAG_HEAD_OF_QUEUE:
1189 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1191 case CTL_TAG_UNTAGGED:
1192 case CTL_TAG_SIMPLE:
1195 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1200 beio->io_offset = -1;
1202 beio->bio_cmd = BIO_DELETE;
1203 beio->ds_trans_type = DEVSTAT_FREE;
1207 be_lun->unmap(be_lun, beio);
1211 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1216 ctl_free_beio(beio);
1217 ctl_config_write_done(io);
1221 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1224 struct ctl_be_block_io *beio;
1225 struct ctl_be_block_softc *softc;
1227 DPRINTF("entered\n");
1229 softc = be_lun->softc;
1230 beio = ctl_alloc_beio(softc);
1233 beio->beio_cont = ctl_be_block_cw_done;
1234 PRIV(io)->ptr = (void *)beio;
1236 switch (io->scsiio.cdb[0]) {
1237 case SYNCHRONIZE_CACHE:
1238 case SYNCHRONIZE_CACHE_16:
1239 beio->bio_cmd = BIO_FLUSH;
1240 beio->ds_trans_type = DEVSTAT_NO_DATA;
1241 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1243 be_lun->lun_flush(be_lun, beio);
1247 ctl_be_block_cw_dispatch_ws(be_lun, io);
1250 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1253 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1258 SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1259 SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1260 SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1261 SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1264 ctl_be_block_next(struct ctl_be_block_io *beio)
1266 struct ctl_be_block_lun *be_lun;
1271 ctl_free_beio(beio);
1272 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1273 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1274 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1275 ctl_data_submit_done(io);
1279 io->io_hdr.status &= ~CTL_STATUS_MASK;
1280 io->io_hdr.status |= CTL_STATUS_NONE;
1282 mtx_lock(&be_lun->queue_lock);
1284 * XXX KDM make sure that links is okay to use at this point.
1285 * Otherwise, we either need to add another field to ctl_io_hdr,
1286 * or deal with resource allocation here.
1288 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1289 mtx_unlock(&be_lun->queue_lock);
1291 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1295 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1298 struct ctl_be_block_io *beio;
1299 struct ctl_be_block_softc *softc;
1300 struct ctl_lba_len_flags *lbalen;
1301 struct ctl_ptr_len_flags *bptrlen;
1302 uint64_t len_left, lbas;
1305 softc = be_lun->softc;
1307 DPRINTF("entered\n");
1310 if (lbalen->flags & CTL_LLF_WRITE) {
1311 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1313 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1316 beio = ctl_alloc_beio(softc);
1320 bptrlen->ptr = (void *)beio;
1322 switch (io->scsiio.tag_type) {
1323 case CTL_TAG_ORDERED:
1324 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1326 case CTL_TAG_HEAD_OF_QUEUE:
1327 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1329 case CTL_TAG_UNTAGGED:
1330 case CTL_TAG_SIMPLE:
1333 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1337 if (lbalen->flags & CTL_LLF_WRITE) {
1338 beio->bio_cmd = BIO_WRITE;
1339 beio->ds_trans_type = DEVSTAT_WRITE;
1341 beio->bio_cmd = BIO_READ;
1342 beio->ds_trans_type = DEVSTAT_READ;
1345 DPRINTF("%s at LBA %jx len %u @%ju\n",
1346 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1347 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1348 if (lbalen->flags & CTL_LLF_COMPARE)
1349 lbas = CTLBLK_HALF_IO_SIZE;
1351 lbas = CTLBLK_MAX_IO_SIZE;
1352 lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1353 beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1354 beio->io_len = lbas * be_lun->blocksize;
1355 bptrlen->len += lbas;
1357 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1358 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1359 i, CTLBLK_MAX_SEGS));
1362 * Setup the S/G entry for this chunk.
1364 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1365 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1367 DPRINTF("segment %d addr %p len %zd\n", i,
1368 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1370 /* Set up second segment for compare operation. */
1371 if (lbalen->flags & CTL_LLF_COMPARE) {
1372 beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1373 beio->sg_segs[i].len;
1374 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1375 uma_zalloc(be_lun->lun_zone, M_WAITOK);
1379 len_left -= beio->sg_segs[i].len;
1381 if (bptrlen->len < lbalen->len)
1382 beio->beio_cont = ctl_be_block_next;
1383 io->scsiio.be_move_done = ctl_be_block_move_done;
1384 /* For compare we have separate S/G lists for read and datamove. */
1385 if (lbalen->flags & CTL_LLF_COMPARE)
1386 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1388 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1389 io->scsiio.kern_data_len = beio->io_len;
1390 io->scsiio.kern_data_resid = 0;
1391 io->scsiio.kern_sg_entries = beio->num_segs;
1392 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1395 * For the read case, we need to read the data into our buffers and
1396 * then we can send it back to the user. For the write case, we
1397 * need to get the data from the user first.
1399 if (beio->bio_cmd == BIO_READ) {
1400 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1401 be_lun->dispatch(be_lun, beio);
1403 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1405 getbintime(&io->io_hdr.dma_start_bt);
1412 ctl_be_block_worker(void *context, int pending)
1414 struct ctl_be_block_lun *be_lun;
1415 struct ctl_be_block_softc *softc;
1418 be_lun = (struct ctl_be_block_lun *)context;
1419 softc = be_lun->softc;
1421 DPRINTF("entered\n");
1423 mtx_lock(&be_lun->queue_lock);
1425 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1427 struct ctl_be_block_io *beio;
1429 DPRINTF("datamove queue\n");
1431 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1434 mtx_unlock(&be_lun->queue_lock);
1436 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1438 be_lun->dispatch(be_lun, beio);
1440 mtx_lock(&be_lun->queue_lock);
1443 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1446 DPRINTF("config write queue\n");
1448 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1451 mtx_unlock(&be_lun->queue_lock);
1453 ctl_be_block_cw_dispatch(be_lun, io);
1455 mtx_lock(&be_lun->queue_lock);
1458 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1460 DPRINTF("input queue\n");
1462 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1464 mtx_unlock(&be_lun->queue_lock);
1467 * We must drop the lock, since this routine and
1468 * its children may sleep.
1470 ctl_be_block_dispatch(be_lun, io);
1472 mtx_lock(&be_lun->queue_lock);
1477 * If we get here, there is no work left in the queues, so
1478 * just break out and let the task queue go to sleep.
1482 mtx_unlock(&be_lun->queue_lock);
1486 * Entry point from CTL to the backend for I/O. We queue everything to a
1487 * work thread, so this just puts the I/O on a queue and wakes up the
1491 ctl_be_block_submit(union ctl_io *io)
1493 struct ctl_be_block_lun *be_lun;
1494 struct ctl_be_lun *ctl_be_lun;
1496 DPRINTF("entered\n");
1498 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1499 CTL_PRIV_BACKEND_LUN].ptr;
1500 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1503 * Make sure we only get SCSI I/O.
1505 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1506 "%#x) encountered", io->io_hdr.io_type));
1510 mtx_lock(&be_lun->queue_lock);
1512 * XXX KDM make sure that links is okay to use at this point.
1513 * Otherwise, we either need to add another field to ctl_io_hdr,
1514 * or deal with resource allocation here.
1516 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1517 mtx_unlock(&be_lun->queue_lock);
1518 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1520 return (CTL_RETVAL_COMPLETE);
1524 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1525 int flag, struct thread *td)
1527 struct ctl_be_block_softc *softc;
1530 softc = &backend_block_softc;
1536 struct ctl_lun_req *lun_req;
1538 lun_req = (struct ctl_lun_req *)addr;
1540 switch (lun_req->reqtype) {
1541 case CTL_LUNREQ_CREATE:
1542 error = ctl_be_block_create(softc, lun_req);
1545 error = ctl_be_block_rm(softc, lun_req);
1547 case CTL_LUNREQ_MODIFY:
1548 error = ctl_be_block_modify(softc, lun_req);
1551 lun_req->status = CTL_LUN_ERROR;
1552 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1553 "invalid LUN request type %d",
1568 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1570 struct ctl_be_block_filedata *file_data;
1571 struct ctl_lun_create_params *params;
1577 file_data = &be_lun->backend.file;
1578 params = &be_lun->params;
1580 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1581 be_lun->dispatch = ctl_be_block_dispatch_file;
1582 be_lun->lun_flush = ctl_be_block_flush_file;
1584 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1586 snprintf(req->error_str, sizeof(req->error_str),
1587 "error calling VOP_GETATTR() for file %s",
1593 * Verify that we have the ability to upgrade to exclusive
1594 * access on this file so we can trap errors at open instead
1595 * of reporting them during first access.
1597 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1598 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1599 if (be_lun->vn->v_iflag & VI_DOOMED) {
1601 snprintf(req->error_str, sizeof(req->error_str),
1602 "error locking file %s", be_lun->dev_path);
1608 file_data->cred = crhold(curthread->td_ucred);
1609 if (params->lun_size_bytes != 0)
1610 be_lun->size_bytes = params->lun_size_bytes;
1612 be_lun->size_bytes = vattr.va_size;
1614 * We set the multi thread flag for file operations because all
1615 * filesystems (in theory) are capable of allowing multiple readers
1616 * of a file at once. So we want to get the maximum possible
1619 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1622 * For files we can use any logical block size. Prefer 512 bytes
1623 * for compatibility reasons. If file's vattr.va_blocksize
1624 * (preferred I/O block size) is bigger and multiple to chosen
1625 * logical block size -- report it as physical block size.
1627 if (params->blocksize_bytes != 0)
1628 be_lun->blocksize = params->blocksize_bytes;
1630 be_lun->blocksize = 512;
1631 pss = vattr.va_blocksize / be_lun->blocksize;
1632 if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) &&
1633 ((pss & (pss - 1)) == 0)) {
1634 be_lun->pblockexp = fls(pss) - 1;
1635 be_lun->pblockoff = 0;
1639 * Sanity check. The media size has to be at least one
1642 if (be_lun->size_bytes < be_lun->blocksize) {
1644 snprintf(req->error_str, sizeof(req->error_str),
1645 "file %s size %ju < block size %u", be_lun->dev_path,
1646 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1652 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1654 struct ctl_lun_create_params *params;
1657 struct cdevsw *devsw;
1659 off_t ps, pss, po, pos;
1661 params = &be_lun->params;
1663 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1664 be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1665 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1666 &be_lun->backend.dev.dev_ref);
1667 if (be_lun->backend.dev.csw == NULL)
1668 panic("Unable to retrieve device switch");
1669 if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0)
1670 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1672 be_lun->dispatch = ctl_be_block_dispatch_dev;
1673 be_lun->lun_flush = ctl_be_block_flush_dev;
1674 be_lun->unmap = ctl_be_block_unmap_dev;
1675 be_lun->getattr = ctl_be_block_getattr_dev;
1677 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1679 snprintf(req->error_str, sizeof(req->error_str),
1680 "error getting vnode attributes for device %s",
1685 dev = be_lun->vn->v_rdev;
1686 devsw = dev->si_devsw;
1687 if (!devsw->d_ioctl) {
1688 snprintf(req->error_str, sizeof(req->error_str),
1689 "no d_ioctl for device %s!",
1694 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1695 (caddr_t)&be_lun->blocksize, FREAD,
1698 snprintf(req->error_str, sizeof(req->error_str),
1699 "error %d returned for DIOCGSECTORSIZE ioctl "
1700 "on %s!", error, be_lun->dev_path);
1705 * If the user has asked for a blocksize that is greater than the
1706 * backing device's blocksize, we can do it only if the blocksize
1707 * the user is asking for is an even multiple of the underlying
1708 * device's blocksize.
1710 if ((params->blocksize_bytes != 0)
1711 && (params->blocksize_bytes > be_lun->blocksize)) {
1712 uint32_t bs_multiple, tmp_blocksize;
1714 bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1716 tmp_blocksize = bs_multiple * be_lun->blocksize;
1718 if (tmp_blocksize == params->blocksize_bytes) {
1719 be_lun->blocksize = params->blocksize_bytes;
1721 snprintf(req->error_str, sizeof(req->error_str),
1722 "requested blocksize %u is not an even "
1723 "multiple of backing device blocksize %u",
1724 params->blocksize_bytes,
1729 } else if ((params->blocksize_bytes != 0)
1730 && (params->blocksize_bytes != be_lun->blocksize)) {
1731 snprintf(req->error_str, sizeof(req->error_str),
1732 "requested blocksize %u < backing device "
1733 "blocksize %u", params->blocksize_bytes,
1738 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1739 (caddr_t)&be_lun->size_bytes, FREAD,
1742 snprintf(req->error_str, sizeof(req->error_str),
1743 "error %d returned for DIOCGMEDIASIZE "
1744 " ioctl on %s!", error,
1749 if (params->lun_size_bytes != 0) {
1750 if (params->lun_size_bytes > be_lun->size_bytes) {
1751 snprintf(req->error_str, sizeof(req->error_str),
1752 "requested LUN size %ju > backing device "
1754 (uintmax_t)params->lun_size_bytes,
1755 (uintmax_t)be_lun->size_bytes);
1759 be_lun->size_bytes = params->lun_size_bytes;
1762 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1763 (caddr_t)&ps, FREAD, curthread);
1767 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1768 (caddr_t)&po, FREAD, curthread);
1772 pss = ps / be_lun->blocksize;
1773 pos = po / be_lun->blocksize;
1774 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1775 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1776 be_lun->pblockexp = fls(pss) - 1;
1777 be_lun->pblockoff = (pss - pos) % pss;
1784 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
1788 int flags = FREAD | FWRITE;
1790 switch (be_lun->dev_type) {
1791 case CTL_BE_BLOCK_DEV:
1792 if (be_lun->backend.dev.csw) {
1793 dev_relthread(be_lun->backend.dev.cdev,
1794 be_lun->backend.dev.dev_ref);
1795 be_lun->backend.dev.csw = NULL;
1796 be_lun->backend.dev.cdev = NULL;
1799 case CTL_BE_BLOCK_FILE:
1801 case CTL_BE_BLOCK_NONE:
1804 panic("Unexpected backend type.");
1808 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1811 switch (be_lun->dev_type) {
1812 case CTL_BE_BLOCK_DEV:
1814 case CTL_BE_BLOCK_FILE:
1815 if (be_lun->backend.file.cred != NULL) {
1816 crfree(be_lun->backend.file.cred);
1817 be_lun->backend.file.cred = NULL;
1820 case CTL_BE_BLOCK_NONE:
1823 panic("Unexpected backend type.");
1826 be_lun->dev_type = CTL_BE_BLOCK_NONE;
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 "Root filesystem is not mounted");
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 "error opening %s: %d", be_lun->dev_path, error);
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;
1937 req->status = CTL_LUN_OK;
1939 num_threads = cbb_num_threads;
1941 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1943 be_lun->params = req->reqdata.create;
1944 be_lun->softc = softc;
1945 STAILQ_INIT(&be_lun->input_queue);
1946 STAILQ_INIT(&be_lun->config_write_queue);
1947 STAILQ_INIT(&be_lun->datamove_queue);
1948 sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1949 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
1950 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
1951 ctl_init_opts(&be_lun->ctl_be_lun.options,
1952 req->num_be_args, req->kern_be_args);
1954 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1955 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1957 if (be_lun->lun_zone == NULL) {
1958 snprintf(req->error_str, sizeof(req->error_str),
1959 "error allocating UMA zone");
1963 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1964 be_lun->ctl_be_lun.lun_type = params->device_type;
1966 be_lun->ctl_be_lun.lun_type = T_DIRECT;
1968 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1969 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
1970 if (value == NULL) {
1971 snprintf(req->error_str, sizeof(req->error_str),
1972 "no file argument specified");
1975 be_lun->dev_path = strdup(value, M_CTLBLK);
1976 be_lun->blocksize = 512;
1977 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1979 retval = ctl_be_block_open(softc, be_lun, req);
1982 req->status = CTL_LUN_WARNING;
1986 * For processor devices, we don't have any size.
1988 be_lun->blocksize = 0;
1989 be_lun->pblockexp = 0;
1990 be_lun->pblockoff = 0;
1991 be_lun->size_blocks = 0;
1992 be_lun->size_bytes = 0;
1993 be_lun->ctl_be_lun.maxlba = 0;
1996 * Default to just 1 thread for processor devices.
2002 * XXX This searching loop might be refactored to be combined with
2005 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
2006 if (value != NULL) {
2007 tmp_num_threads = strtol(value, NULL, 0);
2010 * We don't let the user specify less than one
2011 * thread, but hope he's clueful enough not to
2012 * specify 1000 threads.
2014 if (tmp_num_threads < 1) {
2015 snprintf(req->error_str, sizeof(req->error_str),
2016 "invalid number of threads %s",
2020 num_threads = tmp_num_threads;
2022 unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol);
2023 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
2025 unmap = (strcmp(value, "on") == 0);
2027 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2028 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2029 if (be_lun->vn == NULL)
2030 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE;
2032 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2033 be_lun->ctl_be_lun.be_lun = be_lun;
2034 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2035 0 : (be_lun->size_blocks - 1);
2036 be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2037 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2038 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2039 if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2040 be_lun->blocksize != 0)
2041 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2043 /* Tell the user the blocksize we ended up using */
2044 params->lun_size_bytes = be_lun->size_bytes;
2045 params->blocksize_bytes = be_lun->blocksize;
2046 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2047 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2048 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2050 be_lun->ctl_be_lun.req_lun_id = 0;
2052 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2053 be_lun->ctl_be_lun.lun_config_status =
2054 ctl_be_block_lun_config_status;
2055 be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2057 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2058 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2060 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2061 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2064 /* Tell the user what we used for a serial number */
2065 strncpy((char *)params->serial_num, tmpstr,
2066 ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2068 strncpy((char *)be_lun->ctl_be_lun.serial_num,
2070 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2071 sizeof(params->serial_num)));
2073 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2074 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2075 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2076 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2079 /* Tell the user what we used for a device ID */
2080 strncpy((char *)params->device_id, tmpstr,
2081 ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2083 strncpy((char *)be_lun->ctl_be_lun.device_id,
2085 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2086 sizeof(params->device_id)));
2089 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2091 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2092 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2094 if (be_lun->io_taskqueue == NULL) {
2095 snprintf(req->error_str, sizeof(req->error_str),
2096 "unable to create taskqueue");
2101 * Note that we start the same number of threads by default for
2102 * both the file case and the block device case. For the file
2103 * case, we need multiple threads to allow concurrency, because the
2104 * vnode interface is designed to be a blocking interface. For the
2105 * block device case, ZFS zvols at least will block the caller's
2106 * context in many instances, and so we need multiple threads to
2107 * overcome that problem. Other block devices don't need as many
2108 * threads, but they shouldn't cause too many problems.
2110 * If the user wants to just have a single thread for a block
2111 * device, he can specify that when the LUN is created, or change
2112 * the tunable/sysctl to alter the default number of threads.
2114 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2115 /*num threads*/num_threads,
2118 "%s taskq", be_lun->lunname);
2123 be_lun->num_threads = num_threads;
2125 mtx_lock(&softc->lock);
2127 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2129 mtx_unlock(&softc->lock);
2131 retval = ctl_add_lun(&be_lun->ctl_be_lun);
2133 mtx_lock(&softc->lock);
2134 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2137 mtx_unlock(&softc->lock);
2138 snprintf(req->error_str, sizeof(req->error_str),
2139 "ctl_add_lun() returned error %d, see dmesg for "
2145 mtx_lock(&softc->lock);
2148 * Tell the config_status routine that we're waiting so it won't
2149 * clean up the LUN in the event of an error.
2151 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2153 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2154 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2155 if (retval == EINTR)
2158 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2160 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2161 snprintf(req->error_str, sizeof(req->error_str),
2162 "LUN configuration error, see dmesg for details");
2163 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2166 mtx_unlock(&softc->lock);
2169 params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2172 mtx_unlock(&softc->lock);
2174 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2176 DEVSTAT_ALL_SUPPORTED,
2177 be_lun->ctl_be_lun.lun_type
2178 | DEVSTAT_TYPE_IF_OTHER,
2179 DEVSTAT_PRIORITY_OTHER);
2184 req->status = CTL_LUN_ERROR;
2186 if (be_lun->io_taskqueue != NULL)
2187 taskqueue_free(be_lun->io_taskqueue);
2188 ctl_be_block_close(be_lun);
2189 if (be_lun->dev_path != NULL)
2190 free(be_lun->dev_path, M_CTLBLK);
2191 if (be_lun->lun_zone != NULL)
2192 uma_zdestroy(be_lun->lun_zone);
2193 ctl_free_opts(&be_lun->ctl_be_lun.options);
2194 mtx_destroy(&be_lun->queue_lock);
2195 mtx_destroy(&be_lun->io_lock);
2196 free(be_lun, M_CTLBLK);
2202 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2204 struct ctl_lun_rm_params *params;
2205 struct ctl_be_block_lun *be_lun;
2208 params = &req->reqdata.rm;
2210 mtx_lock(&softc->lock);
2214 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2215 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2218 mtx_unlock(&softc->lock);
2220 if (be_lun == NULL) {
2221 snprintf(req->error_str, sizeof(req->error_str),
2222 "LUN %u is not managed by the block backend",
2227 retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2230 snprintf(req->error_str, sizeof(req->error_str),
2231 "error %d returned from ctl_disable_lun() for "
2232 "LUN %d", retval, params->lun_id);
2237 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2239 snprintf(req->error_str, sizeof(req->error_str),
2240 "error %d returned from ctl_invalidate_lun() for "
2241 "LUN %d", retval, params->lun_id);
2245 mtx_lock(&softc->lock);
2247 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2249 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2250 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2251 if (retval == EINTR)
2255 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2257 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2258 snprintf(req->error_str, sizeof(req->error_str),
2259 "interrupted waiting for LUN to be freed");
2260 mtx_unlock(&softc->lock);
2264 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2267 mtx_unlock(&softc->lock);
2269 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2271 taskqueue_free(be_lun->io_taskqueue);
2273 ctl_be_block_close(be_lun);
2275 if (be_lun->disk_stats != NULL)
2276 devstat_remove_entry(be_lun->disk_stats);
2278 uma_zdestroy(be_lun->lun_zone);
2280 ctl_free_opts(&be_lun->ctl_be_lun.options);
2281 free(be_lun->dev_path, M_CTLBLK);
2282 mtx_destroy(&be_lun->queue_lock);
2283 mtx_destroy(&be_lun->io_lock);
2284 free(be_lun, M_CTLBLK);
2286 req->status = CTL_LUN_OK;
2292 req->status = CTL_LUN_ERROR;
2298 ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2299 struct ctl_lun_req *req)
2303 struct ctl_lun_create_params *params = &be_lun->params;
2305 if (params->lun_size_bytes != 0) {
2306 be_lun->size_bytes = params->lun_size_bytes;
2308 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2309 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2310 VOP_UNLOCK(be_lun->vn, 0);
2312 snprintf(req->error_str, sizeof(req->error_str),
2313 "error calling VOP_GETATTR() for file %s",
2318 be_lun->size_bytes = vattr.va_size;
2325 ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2326 struct ctl_lun_req *req)
2328 struct ctl_be_block_devdata *dev_data;
2330 struct ctl_lun_create_params *params = &be_lun->params;
2331 uint64_t size_bytes;
2333 dev_data = &be_lun->backend.dev;
2334 if (!dev_data->csw->d_ioctl) {
2335 snprintf(req->error_str, sizeof(req->error_str),
2336 "no d_ioctl for device %s!", be_lun->dev_path);
2340 error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2341 (caddr_t)&size_bytes, FREAD,
2344 snprintf(req->error_str, sizeof(req->error_str),
2345 "error %d returned for DIOCGMEDIASIZE ioctl "
2346 "on %s!", error, be_lun->dev_path);
2350 if (params->lun_size_bytes != 0) {
2351 if (params->lun_size_bytes > size_bytes) {
2352 snprintf(req->error_str, sizeof(req->error_str),
2353 "requested LUN size %ju > backing device "
2355 (uintmax_t)params->lun_size_bytes,
2356 (uintmax_t)size_bytes);
2360 be_lun->size_bytes = params->lun_size_bytes;
2362 be_lun->size_bytes = size_bytes;
2369 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2371 struct ctl_lun_modify_params *params;
2372 struct ctl_be_block_lun *be_lun;
2376 params = &req->reqdata.modify;
2378 mtx_lock(&softc->lock);
2380 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2381 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2384 mtx_unlock(&softc->lock);
2386 if (be_lun == NULL) {
2387 snprintf(req->error_str, sizeof(req->error_str),
2388 "LUN %u is not managed by the block backend",
2393 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2395 oldsize = be_lun->size_bytes;
2396 if (be_lun->vn == NULL)
2397 error = ctl_be_block_open(softc, be_lun, req);
2398 else if (be_lun->vn->v_type == VREG)
2399 error = ctl_be_block_modify_file(be_lun, req);
2401 error = ctl_be_block_modify_dev(be_lun, req);
2403 if (error == 0 && be_lun->size_bytes != oldsize) {
2404 be_lun->size_blocks = be_lun->size_bytes >>
2405 be_lun->blocksize_shift;
2408 * The maximum LBA is the size - 1.
2410 * XXX: Note that this field is being updated without locking,
2411 * which might cause problems on 32-bit architectures.
2413 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2414 0 : (be_lun->size_blocks - 1);
2415 be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2416 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2417 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2418 if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2419 be_lun->blocksize != 0)
2420 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2422 ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2423 if (oldsize == 0 && be_lun->size_blocks != 0)
2424 ctl_lun_online(&be_lun->ctl_be_lun);
2427 /* Tell the user the exact size we ended up using */
2428 params->lun_size_bytes = be_lun->size_bytes;
2430 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2435 req->status = CTL_LUN_ERROR;
2441 ctl_be_block_lun_shutdown(void *be_lun)
2443 struct ctl_be_block_lun *lun;
2444 struct ctl_be_block_softc *softc;
2446 lun = (struct ctl_be_block_lun *)be_lun;
2450 mtx_lock(&softc->lock);
2451 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2452 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2454 mtx_unlock(&softc->lock);
2459 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2461 struct ctl_be_block_lun *lun;
2462 struct ctl_be_block_softc *softc;
2464 lun = (struct ctl_be_block_lun *)be_lun;
2467 if (status == CTL_LUN_CONFIG_OK) {
2468 mtx_lock(&softc->lock);
2469 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2470 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2472 mtx_unlock(&softc->lock);
2475 * We successfully added the LUN, attempt to enable it.
2477 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2478 printf("%s: ctl_enable_lun() failed!\n", __func__);
2479 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2480 printf("%s: ctl_invalidate_lun() failed!\n",
2489 mtx_lock(&softc->lock);
2490 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2491 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2493 mtx_unlock(&softc->lock);
2498 ctl_be_block_config_write(union ctl_io *io)
2500 struct ctl_be_block_lun *be_lun;
2501 struct ctl_be_lun *ctl_be_lun;
2506 DPRINTF("entered\n");
2508 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2509 CTL_PRIV_BACKEND_LUN].ptr;
2510 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2512 switch (io->scsiio.cdb[0]) {
2513 case SYNCHRONIZE_CACHE:
2514 case SYNCHRONIZE_CACHE_16:
2519 * The upper level CTL code will filter out any CDBs with
2520 * the immediate bit set and return the proper error.
2522 * We don't really need to worry about what LBA range the
2523 * user asked to be synced out. When they issue a sync
2524 * cache command, we'll sync out the whole thing.
2526 mtx_lock(&be_lun->queue_lock);
2527 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2529 mtx_unlock(&be_lun->queue_lock);
2530 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2532 case START_STOP_UNIT: {
2533 struct scsi_start_stop_unit *cdb;
2535 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2537 if (cdb->how & SSS_START)
2538 retval = ctl_start_lun(ctl_be_lun);
2540 retval = ctl_stop_lun(ctl_be_lun);
2542 * XXX KDM Copan-specific offline behavior.
2543 * Figure out a reasonable way to port this?
2547 && (cdb->byte2 & SSS_ONOFFLINE))
2548 retval = ctl_lun_offline(ctl_be_lun);
2553 * In general, the above routines should not fail. They
2554 * just set state for the LUN. So we've got something
2555 * pretty wrong here if we can't start or stop the LUN.
2558 ctl_set_internal_failure(&io->scsiio,
2560 /*retry_count*/ 0xf051);
2561 retval = CTL_RETVAL_COMPLETE;
2563 ctl_set_success(&io->scsiio);
2565 ctl_config_write_done(io);
2569 ctl_set_invalid_opcode(&io->scsiio);
2570 ctl_config_write_done(io);
2571 retval = CTL_RETVAL_COMPLETE;
2580 ctl_be_block_config_read(union ctl_io *io)
2586 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2588 struct ctl_be_block_lun *lun;
2591 lun = (struct ctl_be_block_lun *)be_lun;
2594 retval = sbuf_printf(sb, "\t<num_threads>");
2599 retval = sbuf_printf(sb, "%d", lun->num_threads);
2604 retval = sbuf_printf(sb, "</num_threads>\n");
2612 ctl_be_block_lun_attr(void *be_lun, const char *attrname)
2614 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2616 if (lun->getattr == NULL)
2617 return (UINT64_MAX);
2618 return (lun->getattr(lun, attrname));
2622 ctl_be_block_init(void)
2624 struct ctl_be_block_softc *softc;
2627 softc = &backend_block_softc;
2630 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2631 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2632 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2633 STAILQ_INIT(&softc->disk_list);
2634 STAILQ_INIT(&softc->lun_list);