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_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;
167 uint64_t size_blocks;
173 struct ctl_be_block_softc *softc;
174 struct devstat *disk_stats;
175 ctl_be_block_lun_flags flags;
176 STAILQ_ENTRY(ctl_be_block_lun) links;
177 struct ctl_be_lun ctl_be_lun;
178 struct taskqueue *io_taskqueue;
181 STAILQ_HEAD(, ctl_io_hdr) input_queue;
182 STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
183 STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
184 struct mtx_padalign io_lock;
185 struct mtx_padalign queue_lock;
189 * Overall softc structure for the block backend module.
191 struct ctl_be_block_softc {
194 STAILQ_HEAD(, ctl_block_disk) disk_list;
196 STAILQ_HEAD(, ctl_be_block_lun) lun_list;
199 static struct ctl_be_block_softc backend_block_softc;
202 * Per-I/O information.
204 struct ctl_be_block_io {
206 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS];
207 struct iovec xiovecs[CTLBLK_MAX_SEGS];
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;
606 if (ARGS(io)->flags & CTL_LLF_DPO)
608 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
611 bzero(&xuio, sizeof(xuio));
612 if (beio->bio_cmd == BIO_READ) {
613 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
614 xuio.uio_rw = UIO_READ;
616 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
617 xuio.uio_rw = UIO_WRITE;
619 xuio.uio_offset = beio->io_offset;
620 xuio.uio_resid = beio->io_len;
621 xuio.uio_segflg = UIO_SYSSPACE;
622 xuio.uio_iov = beio->xiovecs;
623 xuio.uio_iovcnt = beio->num_segs;
624 xuio.uio_td = curthread;
626 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
627 xiovec->iov_base = beio->sg_segs[i].addr;
628 xiovec->iov_len = beio->sg_segs[i].len;
631 binuptime(&beio->ds_t0);
632 mtx_lock(&be_lun->io_lock);
633 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
634 mtx_unlock(&be_lun->io_lock);
636 if (beio->bio_cmd == BIO_READ) {
637 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
640 * UFS pays attention to IO_DIRECT for reads. If the
641 * DIRECTIO option is configured into the kernel, it calls
642 * ffs_rawread(). But that only works for single-segment
643 * uios with user space addresses. In our case, with a
644 * kernel uio, it still reads into the buffer cache, but it
645 * will just try to release the buffer from the cache later
648 * ZFS does not pay attention to IO_DIRECT for reads.
650 * UFS does not pay attention to IO_SYNC for reads.
652 * ZFS pays attention to IO_SYNC (which translates into the
653 * Solaris define FRSYNC for zfs_read()) for reads. It
654 * attempts to sync the file before reading.
656 * So, to attempt to provide some barrier semantics in the
657 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
659 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
661 VOP_UNLOCK(be_lun->vn, 0);
662 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
664 struct mount *mountpoint;
667 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
669 if (MNT_SHARED_WRITES(mountpoint)
670 || ((mountpoint == NULL)
671 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
672 lock_flags = LK_SHARED;
674 lock_flags = LK_EXCLUSIVE;
676 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
679 * UFS pays attention to IO_DIRECT for writes. The write
680 * is done asynchronously. (Normally the write would just
681 * get put into cache.
683 * UFS pays attention to IO_SYNC for writes. It will
684 * attempt to write the buffer out synchronously if that
687 * ZFS does not pay attention to IO_DIRECT for writes.
689 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
690 * for writes. It will flush the transaction from the
691 * cache before returning.
693 * So if we've got the BIO_ORDERED flag set, we want
694 * IO_SYNC in either the UFS or ZFS case.
696 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
697 VOP_UNLOCK(be_lun->vn, 0);
699 vn_finished_write(mountpoint);
700 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
703 mtx_lock(&be_lun->io_lock);
704 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
705 beio->ds_tag_type, beio->ds_trans_type,
706 /*now*/ NULL, /*then*/&beio->ds_t0);
707 mtx_unlock(&be_lun->io_lock);
710 * If we got an error, set the sense data to "MEDIUM ERROR" and
711 * return the I/O to the user.
716 ctl_scsi_path_string(io, path_str, sizeof(path_str));
718 * XXX KDM ZFS returns ENOSPC when the underlying
719 * filesystem fills up. What kind of SCSI error should we
722 printf("%s%s command returned errno %d\n", path_str,
723 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
724 ctl_set_medium_error(&io->scsiio);
725 ctl_complete_beio(beio);
730 * If this is a write or a verify, we're all done.
731 * If this is a read, we can now send the data to the user.
733 if ((beio->bio_cmd == BIO_WRITE) ||
734 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
735 ctl_set_success(&io->scsiio);
736 ctl_complete_beio(beio);
739 getbintime(&io->io_hdr.dma_start_bt);
746 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
747 struct ctl_be_block_io *beio)
749 struct ctl_be_block_devdata *dev_data;
752 struct iovec *xiovec;
756 DPRINTF("entered\n");
758 dev_data = &be_lun->backend.dev;
761 if (ARGS(io)->flags & CTL_LLF_DPO)
763 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
766 bzero(&xuio, sizeof(xuio));
767 if (beio->bio_cmd == BIO_READ) {
768 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
769 xuio.uio_rw = UIO_READ;
771 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
772 xuio.uio_rw = UIO_WRITE;
774 xuio.uio_offset = beio->io_offset;
775 xuio.uio_resid = beio->io_len;
776 xuio.uio_segflg = UIO_SYSSPACE;
777 xuio.uio_iov = beio->xiovecs;
778 xuio.uio_iovcnt = beio->num_segs;
779 xuio.uio_td = curthread;
781 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
782 xiovec->iov_base = beio->sg_segs[i].addr;
783 xiovec->iov_len = beio->sg_segs[i].len;
786 binuptime(&beio->ds_t0);
787 mtx_lock(&be_lun->io_lock);
788 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
789 mtx_unlock(&be_lun->io_lock);
791 if (beio->bio_cmd == BIO_READ) {
792 error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
793 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
795 error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
796 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
799 mtx_lock(&be_lun->io_lock);
800 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
801 beio->ds_tag_type, beio->ds_trans_type,
802 /*now*/ NULL, /*then*/&beio->ds_t0);
803 mtx_unlock(&be_lun->io_lock);
806 * If we got an error, set the sense data to "MEDIUM ERROR" and
807 * return the I/O to the user.
810 ctl_set_medium_error(&io->scsiio);
811 ctl_complete_beio(beio);
816 * If this is a write or a verify, we're all done.
817 * If this is a read, we can now send the data to the user.
819 if ((beio->bio_cmd == BIO_WRITE) ||
820 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
821 ctl_set_success(&io->scsiio);
822 ctl_complete_beio(beio);
825 getbintime(&io->io_hdr.dma_start_bt);
832 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
833 struct ctl_be_block_io *beio)
837 struct ctl_be_block_devdata *dev_data;
839 dev_data = &be_lun->backend.dev;
842 DPRINTF("entered\n");
844 /* This can't fail, it's a blocking allocation. */
847 bio->bio_cmd = BIO_FLUSH;
848 bio->bio_flags |= BIO_ORDERED;
849 bio->bio_dev = dev_data->cdev;
852 bio->bio_done = ctl_be_block_biodone;
853 bio->bio_caller1 = beio;
857 * We don't need to acquire the LUN lock here, because we are only
858 * sending one bio, and so there is no other context to synchronize
861 beio->num_bios_sent = 1;
862 beio->send_complete = 1;
864 binuptime(&beio->ds_t0);
865 mtx_lock(&be_lun->io_lock);
866 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
867 mtx_unlock(&be_lun->io_lock);
869 (*dev_data->csw->d_strategy)(bio);
873 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
874 struct ctl_be_block_io *beio,
875 uint64_t off, uint64_t len, int last)
878 struct ctl_be_block_devdata *dev_data;
881 dev_data = &be_lun->backend.dev;
882 maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
885 bio->bio_cmd = BIO_DELETE;
886 bio->bio_dev = dev_data->cdev;
887 bio->bio_offset = off;
888 bio->bio_length = MIN(len, maxlen);
890 bio->bio_done = ctl_be_block_biodone;
891 bio->bio_caller1 = beio;
892 bio->bio_pblkno = off / be_lun->blocksize;
894 off += bio->bio_length;
895 len -= bio->bio_length;
897 mtx_lock(&be_lun->io_lock);
898 beio->num_bios_sent++;
899 if (last && len == 0)
900 beio->send_complete = 1;
901 mtx_unlock(&be_lun->io_lock);
903 (*dev_data->csw->d_strategy)(bio);
908 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
909 struct ctl_be_block_io *beio)
912 struct ctl_be_block_devdata *dev_data;
913 struct ctl_ptr_len_flags *ptrlen;
914 struct scsi_unmap_desc *buf, *end;
917 dev_data = &be_lun->backend.dev;
920 DPRINTF("entered\n");
922 binuptime(&beio->ds_t0);
923 mtx_lock(&be_lun->io_lock);
924 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
925 mtx_unlock(&be_lun->io_lock);
927 if (beio->io_offset == -1) {
929 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
930 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
931 end = buf + ptrlen->len / sizeof(*buf);
932 for (; buf < end; buf++) {
933 len = (uint64_t)scsi_4btoul(buf->length) *
936 ctl_be_block_unmap_dev_range(be_lun, beio,
937 scsi_8btou64(buf->lba) * be_lun->blocksize, len,
938 (end - buf < 2) ? TRUE : FALSE);
941 ctl_be_block_unmap_dev_range(be_lun, beio,
942 beio->io_offset, beio->io_len, TRUE);
946 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
947 struct ctl_be_block_io *beio)
949 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
952 struct ctl_be_block_devdata *dev_data;
956 DPRINTF("entered\n");
958 dev_data = &be_lun->backend.dev;
961 * We have to limit our I/O size to the maximum supported by the
962 * backend device. Hopefully it is MAXPHYS. If the driver doesn't
963 * set it properly, use DFLTPHYS.
965 max_iosize = dev_data->cdev->si_iosize_max;
966 if (max_iosize < PAGE_SIZE)
967 max_iosize = DFLTPHYS;
969 cur_offset = beio->io_offset;
970 for (i = 0; i < beio->num_segs; i++) {
974 cur_size = beio->sg_segs[i].len;
975 cur_ptr = beio->sg_segs[i].addr;
977 while (cur_size > 0) {
978 /* This can't fail, it's a blocking allocation. */
981 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
983 bio->bio_cmd = beio->bio_cmd;
984 bio->bio_dev = dev_data->cdev;
985 bio->bio_caller1 = beio;
986 bio->bio_length = min(cur_size, max_iosize);
987 bio->bio_offset = cur_offset;
988 bio->bio_data = cur_ptr;
989 bio->bio_done = ctl_be_block_biodone;
990 bio->bio_pblkno = cur_offset / be_lun->blocksize;
992 cur_offset += bio->bio_length;
993 cur_ptr += bio->bio_length;
994 cur_size -= bio->bio_length;
996 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
997 beio->num_bios_sent++;
1000 binuptime(&beio->ds_t0);
1001 mtx_lock(&be_lun->io_lock);
1002 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1003 beio->send_complete = 1;
1004 mtx_unlock(&be_lun->io_lock);
1007 * Fire off all allocated requests!
1009 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1010 TAILQ_REMOVE(&queue, bio, bio_queue);
1011 (*dev_data->csw->d_strategy)(bio);
1016 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1021 ctl_free_beio(beio);
1022 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1023 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1024 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1025 ctl_config_write_done(io);
1029 ctl_be_block_config_write(io);
1033 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1036 struct ctl_be_block_io *beio;
1037 struct ctl_be_block_softc *softc;
1038 struct ctl_lba_len_flags *lbalen;
1039 uint64_t len_left, lba;
1043 DPRINTF("entered\n");
1045 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1046 softc = be_lun->softc;
1047 lbalen = ARGS(beio->io);
1049 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1050 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1051 ctl_free_beio(beio);
1052 ctl_set_invalid_field(&io->scsiio,
1058 ctl_config_write_done(io);
1062 switch (io->scsiio.tag_type) {
1063 case CTL_TAG_ORDERED:
1064 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1066 case CTL_TAG_HEAD_OF_QUEUE:
1067 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1069 case CTL_TAG_UNTAGGED:
1070 case CTL_TAG_SIMPLE:
1073 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1077 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1078 beio->io_offset = lbalen->lba * be_lun->blocksize;
1079 beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1080 beio->bio_cmd = BIO_DELETE;
1081 beio->ds_trans_type = DEVSTAT_FREE;
1083 be_lun->unmap(be_lun, beio);
1087 beio->bio_cmd = BIO_WRITE;
1088 beio->ds_trans_type = DEVSTAT_WRITE;
1090 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1091 (uintmax_t)lbalen->lba, lbalen->len);
1093 len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1094 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1097 * Setup the S/G entry for this chunk.
1099 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1100 seglen -= seglen % be_lun->blocksize;
1101 beio->sg_segs[i].len = seglen;
1102 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1104 DPRINTF("segment %d addr %p len %zd\n", i,
1105 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1110 buf = beio->sg_segs[i].addr;
1112 for (; buf < end; buf += be_lun->blocksize) {
1113 memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1114 if (lbalen->flags & SWS_LBDATA)
1115 scsi_ulto4b(lbalen->lba + lba, buf);
1120 beio->io_offset = lbalen->lba * be_lun->blocksize;
1121 beio->io_len = lba * be_lun->blocksize;
1123 /* We can not do all in one run. Correct and schedule rerun. */
1127 beio->beio_cont = ctl_be_block_cw_done_ws;
1130 be_lun->dispatch(be_lun, beio);
1134 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1137 struct ctl_be_block_io *beio;
1138 struct ctl_be_block_softc *softc;
1139 struct ctl_ptr_len_flags *ptrlen;
1141 DPRINTF("entered\n");
1143 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1144 softc = be_lun->softc;
1145 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1147 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1148 ctl_free_beio(beio);
1149 ctl_set_invalid_field(&io->scsiio,
1155 ctl_config_write_done(io);
1159 switch (io->scsiio.tag_type) {
1160 case CTL_TAG_ORDERED:
1161 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1163 case CTL_TAG_HEAD_OF_QUEUE:
1164 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1166 case CTL_TAG_UNTAGGED:
1167 case CTL_TAG_SIMPLE:
1170 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1175 beio->io_offset = -1;
1177 beio->bio_cmd = BIO_DELETE;
1178 beio->ds_trans_type = DEVSTAT_FREE;
1182 be_lun->unmap(be_lun, beio);
1186 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1191 ctl_free_beio(beio);
1192 ctl_config_write_done(io);
1196 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1199 struct ctl_be_block_io *beio;
1200 struct ctl_be_block_softc *softc;
1202 DPRINTF("entered\n");
1204 softc = be_lun->softc;
1205 beio = ctl_alloc_beio(softc);
1208 beio->beio_cont = ctl_be_block_cw_done;
1209 PRIV(io)->ptr = (void *)beio;
1211 switch (io->scsiio.cdb[0]) {
1212 case SYNCHRONIZE_CACHE:
1213 case SYNCHRONIZE_CACHE_16:
1214 beio->bio_cmd = BIO_FLUSH;
1215 beio->ds_trans_type = DEVSTAT_NO_DATA;
1216 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1218 be_lun->lun_flush(be_lun, beio);
1222 ctl_be_block_cw_dispatch_ws(be_lun, io);
1225 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1228 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1233 SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1234 SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1235 SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1236 SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1239 ctl_be_block_next(struct ctl_be_block_io *beio)
1241 struct ctl_be_block_lun *be_lun;
1246 ctl_free_beio(beio);
1247 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1248 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1249 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1250 ctl_data_submit_done(io);
1254 io->io_hdr.status &= ~CTL_STATUS_MASK;
1255 io->io_hdr.status |= CTL_STATUS_NONE;
1257 mtx_lock(&be_lun->queue_lock);
1259 * XXX KDM make sure that links is okay to use at this point.
1260 * Otherwise, we either need to add another field to ctl_io_hdr,
1261 * or deal with resource allocation here.
1263 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1264 mtx_unlock(&be_lun->queue_lock);
1266 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1270 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1273 struct ctl_be_block_io *beio;
1274 struct ctl_be_block_softc *softc;
1275 struct ctl_lba_len_flags *lbalen;
1276 struct ctl_ptr_len_flags *bptrlen;
1277 uint64_t len_left, lbas;
1280 softc = be_lun->softc;
1282 DPRINTF("entered\n");
1285 if (lbalen->flags & CTL_LLF_WRITE) {
1286 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1288 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1291 beio = ctl_alloc_beio(softc);
1295 bptrlen->ptr = (void *)beio;
1297 switch (io->scsiio.tag_type) {
1298 case CTL_TAG_ORDERED:
1299 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1301 case CTL_TAG_HEAD_OF_QUEUE:
1302 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1304 case CTL_TAG_UNTAGGED:
1305 case CTL_TAG_SIMPLE:
1308 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1312 if (lbalen->flags & CTL_LLF_WRITE) {
1313 beio->bio_cmd = BIO_WRITE;
1314 beio->ds_trans_type = DEVSTAT_WRITE;
1316 beio->bio_cmd = BIO_READ;
1317 beio->ds_trans_type = DEVSTAT_READ;
1320 DPRINTF("%s at LBA %jx len %u @%ju\n",
1321 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1322 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1323 if (lbalen->flags & CTL_LLF_COMPARE)
1324 lbas = CTLBLK_HALF_IO_SIZE;
1326 lbas = CTLBLK_MAX_IO_SIZE;
1327 lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1328 beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1329 beio->io_len = lbas * be_lun->blocksize;
1330 bptrlen->len += lbas;
1332 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1333 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1334 i, CTLBLK_MAX_SEGS));
1337 * Setup the S/G entry for this chunk.
1339 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1340 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1342 DPRINTF("segment %d addr %p len %zd\n", i,
1343 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1345 /* Set up second segment for compare operation. */
1346 if (lbalen->flags & CTL_LLF_COMPARE) {
1347 beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1348 beio->sg_segs[i].len;
1349 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1350 uma_zalloc(be_lun->lun_zone, M_WAITOK);
1354 len_left -= beio->sg_segs[i].len;
1356 if (bptrlen->len < lbalen->len)
1357 beio->beio_cont = ctl_be_block_next;
1358 io->scsiio.be_move_done = ctl_be_block_move_done;
1359 /* For compare we have separate S/G lists for read and datamove. */
1360 if (lbalen->flags & CTL_LLF_COMPARE)
1361 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1363 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1364 io->scsiio.kern_data_len = beio->io_len;
1365 io->scsiio.kern_data_resid = 0;
1366 io->scsiio.kern_sg_entries = beio->num_segs;
1367 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1370 * For the read case, we need to read the data into our buffers and
1371 * then we can send it back to the user. For the write case, we
1372 * need to get the data from the user first.
1374 if (beio->bio_cmd == BIO_READ) {
1375 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1376 be_lun->dispatch(be_lun, beio);
1378 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1380 getbintime(&io->io_hdr.dma_start_bt);
1387 ctl_be_block_worker(void *context, int pending)
1389 struct ctl_be_block_lun *be_lun;
1390 struct ctl_be_block_softc *softc;
1393 be_lun = (struct ctl_be_block_lun *)context;
1394 softc = be_lun->softc;
1396 DPRINTF("entered\n");
1398 mtx_lock(&be_lun->queue_lock);
1400 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1402 struct ctl_be_block_io *beio;
1404 DPRINTF("datamove queue\n");
1406 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1409 mtx_unlock(&be_lun->queue_lock);
1411 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1413 be_lun->dispatch(be_lun, beio);
1415 mtx_lock(&be_lun->queue_lock);
1418 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1421 DPRINTF("config write queue\n");
1423 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1426 mtx_unlock(&be_lun->queue_lock);
1428 ctl_be_block_cw_dispatch(be_lun, io);
1430 mtx_lock(&be_lun->queue_lock);
1433 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1435 DPRINTF("input queue\n");
1437 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1439 mtx_unlock(&be_lun->queue_lock);
1442 * We must drop the lock, since this routine and
1443 * its children may sleep.
1445 ctl_be_block_dispatch(be_lun, io);
1447 mtx_lock(&be_lun->queue_lock);
1452 * If we get here, there is no work left in the queues, so
1453 * just break out and let the task queue go to sleep.
1457 mtx_unlock(&be_lun->queue_lock);
1461 * Entry point from CTL to the backend for I/O. We queue everything to a
1462 * work thread, so this just puts the I/O on a queue and wakes up the
1466 ctl_be_block_submit(union ctl_io *io)
1468 struct ctl_be_block_lun *be_lun;
1469 struct ctl_be_lun *ctl_be_lun;
1471 DPRINTF("entered\n");
1473 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1474 CTL_PRIV_BACKEND_LUN].ptr;
1475 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1478 * Make sure we only get SCSI I/O.
1480 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1481 "%#x) encountered", io->io_hdr.io_type));
1485 mtx_lock(&be_lun->queue_lock);
1487 * XXX KDM make sure that links is okay to use at this point.
1488 * Otherwise, we either need to add another field to ctl_io_hdr,
1489 * or deal with resource allocation here.
1491 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1492 mtx_unlock(&be_lun->queue_lock);
1493 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1495 return (CTL_RETVAL_COMPLETE);
1499 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1500 int flag, struct thread *td)
1502 struct ctl_be_block_softc *softc;
1505 softc = &backend_block_softc;
1511 struct ctl_lun_req *lun_req;
1513 lun_req = (struct ctl_lun_req *)addr;
1515 switch (lun_req->reqtype) {
1516 case CTL_LUNREQ_CREATE:
1517 error = ctl_be_block_create(softc, lun_req);
1520 error = ctl_be_block_rm(softc, lun_req);
1522 case CTL_LUNREQ_MODIFY:
1523 error = ctl_be_block_modify(softc, lun_req);
1526 lun_req->status = CTL_LUN_ERROR;
1527 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1528 "invalid LUN request type %d",
1543 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1545 struct ctl_be_block_filedata *file_data;
1546 struct ctl_lun_create_params *params;
1551 file_data = &be_lun->backend.file;
1552 params = &be_lun->params;
1554 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1555 be_lun->dispatch = ctl_be_block_dispatch_file;
1556 be_lun->lun_flush = ctl_be_block_flush_file;
1558 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1560 snprintf(req->error_str, sizeof(req->error_str),
1561 "error calling VOP_GETATTR() for file %s",
1567 * Verify that we have the ability to upgrade to exclusive
1568 * access on this file so we can trap errors at open instead
1569 * of reporting them during first access.
1571 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1572 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1573 if (be_lun->vn->v_iflag & VI_DOOMED) {
1575 snprintf(req->error_str, sizeof(req->error_str),
1576 "error locking file %s", be_lun->dev_path);
1582 file_data->cred = crhold(curthread->td_ucred);
1583 if (params->lun_size_bytes != 0)
1584 be_lun->size_bytes = params->lun_size_bytes;
1586 be_lun->size_bytes = vattr.va_size;
1588 * We set the multi thread flag for file operations because all
1589 * filesystems (in theory) are capable of allowing multiple readers
1590 * of a file at once. So we want to get the maximum possible
1593 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1596 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
1597 * With ZFS, it is 131072 bytes. Block sizes that large don't work
1598 * with disklabel and UFS on FreeBSD at least. Large block sizes
1599 * may not work with other OSes as well. So just export a sector
1600 * size of 512 bytes, which should work with any OS or
1601 * application. Since our backing is a file, any block size will
1602 * work fine for the backing store.
1605 be_lun->blocksize= vattr.va_blocksize;
1607 if (params->blocksize_bytes != 0)
1608 be_lun->blocksize = params->blocksize_bytes;
1610 be_lun->blocksize = 512;
1613 * Sanity check. The media size has to be at least one
1616 if (be_lun->size_bytes < be_lun->blocksize) {
1618 snprintf(req->error_str, sizeof(req->error_str),
1619 "file %s size %ju < block size %u", be_lun->dev_path,
1620 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1626 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1628 struct ctl_lun_create_params *params;
1631 struct cdevsw *devsw;
1633 off_t ps, pss, po, pos;
1635 params = &be_lun->params;
1637 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1638 be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1639 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1640 &be_lun->backend.dev.dev_ref);
1641 if (be_lun->backend.dev.csw == NULL)
1642 panic("Unable to retrieve device switch");
1643 if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0)
1644 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1646 be_lun->dispatch = ctl_be_block_dispatch_dev;
1647 be_lun->lun_flush = ctl_be_block_flush_dev;
1648 be_lun->unmap = ctl_be_block_unmap_dev;
1650 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1652 snprintf(req->error_str, sizeof(req->error_str),
1653 "error getting vnode attributes for device %s",
1658 dev = be_lun->vn->v_rdev;
1659 devsw = dev->si_devsw;
1660 if (!devsw->d_ioctl) {
1661 snprintf(req->error_str, sizeof(req->error_str),
1662 "no d_ioctl for device %s!",
1667 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1668 (caddr_t)&be_lun->blocksize, FREAD,
1671 snprintf(req->error_str, sizeof(req->error_str),
1672 "error %d returned for DIOCGSECTORSIZE ioctl "
1673 "on %s!", error, be_lun->dev_path);
1678 * If the user has asked for a blocksize that is greater than the
1679 * backing device's blocksize, we can do it only if the blocksize
1680 * the user is asking for is an even multiple of the underlying
1681 * device's blocksize.
1683 if ((params->blocksize_bytes != 0)
1684 && (params->blocksize_bytes > be_lun->blocksize)) {
1685 uint32_t bs_multiple, tmp_blocksize;
1687 bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1689 tmp_blocksize = bs_multiple * be_lun->blocksize;
1691 if (tmp_blocksize == params->blocksize_bytes) {
1692 be_lun->blocksize = params->blocksize_bytes;
1694 snprintf(req->error_str, sizeof(req->error_str),
1695 "requested blocksize %u is not an even "
1696 "multiple of backing device blocksize %u",
1697 params->blocksize_bytes,
1702 } else if ((params->blocksize_bytes != 0)
1703 && (params->blocksize_bytes != be_lun->blocksize)) {
1704 snprintf(req->error_str, sizeof(req->error_str),
1705 "requested blocksize %u < backing device "
1706 "blocksize %u", params->blocksize_bytes,
1711 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1712 (caddr_t)&be_lun->size_bytes, FREAD,
1715 snprintf(req->error_str, sizeof(req->error_str),
1716 "error %d returned for DIOCGMEDIASIZE "
1717 " ioctl on %s!", error,
1722 if (params->lun_size_bytes != 0) {
1723 if (params->lun_size_bytes > be_lun->size_bytes) {
1724 snprintf(req->error_str, sizeof(req->error_str),
1725 "requested LUN size %ju > backing device "
1727 (uintmax_t)params->lun_size_bytes,
1728 (uintmax_t)be_lun->size_bytes);
1732 be_lun->size_bytes = params->lun_size_bytes;
1735 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1736 (caddr_t)&ps, FREAD, curthread);
1740 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1741 (caddr_t)&po, FREAD, curthread);
1745 pss = ps / be_lun->blocksize;
1746 pos = po / be_lun->blocksize;
1747 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1748 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1749 be_lun->pblockexp = fls(pss) - 1;
1750 be_lun->pblockoff = (pss - pos) % pss;
1757 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
1761 int flags = FREAD | FWRITE;
1763 switch (be_lun->dev_type) {
1764 case CTL_BE_BLOCK_DEV:
1765 if (be_lun->backend.dev.csw) {
1766 dev_relthread(be_lun->backend.dev.cdev,
1767 be_lun->backend.dev.dev_ref);
1768 be_lun->backend.dev.csw = NULL;
1769 be_lun->backend.dev.cdev = NULL;
1772 case CTL_BE_BLOCK_FILE:
1774 case CTL_BE_BLOCK_NONE:
1777 panic("Unexpected backend type.");
1781 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1784 switch (be_lun->dev_type) {
1785 case CTL_BE_BLOCK_DEV:
1787 case CTL_BE_BLOCK_FILE:
1788 if (be_lun->backend.file.cred != NULL) {
1789 crfree(be_lun->backend.file.cred);
1790 be_lun->backend.file.cred = NULL;
1793 case CTL_BE_BLOCK_NONE:
1796 panic("Unexpected backend type.");
1799 be_lun->dev_type = CTL_BE_BLOCK_NONE;
1807 ctl_be_block_open(struct ctl_be_block_softc *softc,
1808 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1810 struct nameidata nd;
1815 * XXX KDM allow a read-only option?
1817 flags = FREAD | FWRITE;
1820 if (rootvnode == NULL) {
1821 snprintf(req->error_str, sizeof(req->error_str),
1822 "Root filesystem is not mounted");
1826 if (!curthread->td_proc->p_fd->fd_cdir) {
1827 curthread->td_proc->p_fd->fd_cdir = rootvnode;
1830 if (!curthread->td_proc->p_fd->fd_rdir) {
1831 curthread->td_proc->p_fd->fd_rdir = rootvnode;
1834 if (!curthread->td_proc->p_fd->fd_jdir) {
1835 curthread->td_proc->p_fd->fd_jdir = rootvnode;
1840 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1841 error = vn_open(&nd, &flags, 0, NULL);
1844 * This is the only reasonable guess we can make as far as
1845 * path if the user doesn't give us a fully qualified path.
1846 * If they want to specify a file, they need to specify the
1849 if (be_lun->dev_path[0] != '/') {
1850 char *dev_path = "/dev/";
1853 /* Try adding device path at beginning of name */
1854 dev_name = malloc(strlen(be_lun->dev_path)
1855 + strlen(dev_path) + 1,
1856 M_CTLBLK, M_WAITOK);
1858 sprintf(dev_name, "%s%s", dev_path,
1860 free(be_lun->dev_path, M_CTLBLK);
1861 be_lun->dev_path = dev_name;
1865 snprintf(req->error_str, sizeof(req->error_str),
1866 "error opening %s: %d", be_lun->dev_path, error);
1870 NDFREE(&nd, NDF_ONLY_PNBUF);
1872 be_lun->vn = nd.ni_vp;
1874 /* We only support disks and files. */
1875 if (vn_isdisk(be_lun->vn, &error)) {
1876 error = ctl_be_block_open_dev(be_lun, req);
1877 } else if (be_lun->vn->v_type == VREG) {
1878 error = ctl_be_block_open_file(be_lun, req);
1881 snprintf(req->error_str, sizeof(req->error_str),
1882 "%s is not a disk or plain file", be_lun->dev_path);
1884 VOP_UNLOCK(be_lun->vn, 0);
1887 ctl_be_block_close(be_lun);
1891 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1892 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1898 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1900 struct ctl_be_block_lun *be_lun;
1901 struct ctl_lun_create_params *params;
1902 char num_thread_str[16];
1905 int retval, num_threads, unmap;
1906 int tmp_num_threads;
1908 params = &req->reqdata.create;
1910 req->status = CTL_LUN_OK;
1912 num_threads = cbb_num_threads;
1914 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1916 be_lun->params = req->reqdata.create;
1917 be_lun->softc = softc;
1918 STAILQ_INIT(&be_lun->input_queue);
1919 STAILQ_INIT(&be_lun->config_write_queue);
1920 STAILQ_INIT(&be_lun->datamove_queue);
1921 sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1922 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
1923 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
1924 ctl_init_opts(&be_lun->ctl_be_lun.options,
1925 req->num_be_args, req->kern_be_args);
1927 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1928 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1930 if (be_lun->lun_zone == NULL) {
1931 snprintf(req->error_str, sizeof(req->error_str),
1932 "error allocating UMA zone");
1936 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1937 be_lun->ctl_be_lun.lun_type = params->device_type;
1939 be_lun->ctl_be_lun.lun_type = T_DIRECT;
1941 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1942 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
1943 if (value == NULL) {
1944 snprintf(req->error_str, sizeof(req->error_str),
1945 "no file argument specified");
1948 be_lun->dev_path = strdup(value, M_CTLBLK);
1949 be_lun->blocksize = 512;
1950 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1952 retval = ctl_be_block_open(softc, be_lun, req);
1955 req->status = CTL_LUN_WARNING;
1959 * For processor devices, we don't have any size.
1961 be_lun->blocksize = 0;
1962 be_lun->pblockexp = 0;
1963 be_lun->pblockoff = 0;
1964 be_lun->size_blocks = 0;
1965 be_lun->size_bytes = 0;
1966 be_lun->ctl_be_lun.maxlba = 0;
1969 * Default to just 1 thread for processor devices.
1975 * XXX This searching loop might be refactored to be combined with
1978 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
1979 if (value != NULL) {
1980 tmp_num_threads = strtol(value, NULL, 0);
1983 * We don't let the user specify less than one
1984 * thread, but hope he's clueful enough not to
1985 * specify 1000 threads.
1987 if (tmp_num_threads < 1) {
1988 snprintf(req->error_str, sizeof(req->error_str),
1989 "invalid number of threads %s",
1993 num_threads = tmp_num_threads;
1996 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
1997 if (value != NULL && strcmp(value, "on") == 0)
2000 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2001 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2002 if (be_lun->vn == NULL)
2003 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE;
2005 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2006 be_lun->ctl_be_lun.be_lun = be_lun;
2007 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2008 0 : (be_lun->size_blocks - 1);
2009 be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2010 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2011 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2012 if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2013 be_lun->blocksize != 0)
2014 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2016 /* Tell the user the blocksize we ended up using */
2017 params->lun_size_bytes = be_lun->size_bytes;
2018 params->blocksize_bytes = be_lun->blocksize;
2019 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2020 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2021 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2023 be_lun->ctl_be_lun.req_lun_id = 0;
2025 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2026 be_lun->ctl_be_lun.lun_config_status =
2027 ctl_be_block_lun_config_status;
2028 be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2030 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2031 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2033 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2034 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2037 /* Tell the user what we used for a serial number */
2038 strncpy((char *)params->serial_num, tmpstr,
2039 ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2041 strncpy((char *)be_lun->ctl_be_lun.serial_num,
2043 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2044 sizeof(params->serial_num)));
2046 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2047 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2048 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2049 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2052 /* Tell the user what we used for a device ID */
2053 strncpy((char *)params->device_id, tmpstr,
2054 ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2056 strncpy((char *)be_lun->ctl_be_lun.device_id,
2058 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2059 sizeof(params->device_id)));
2062 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2064 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2065 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2067 if (be_lun->io_taskqueue == NULL) {
2068 snprintf(req->error_str, sizeof(req->error_str),
2069 "unable to create taskqueue");
2074 * Note that we start the same number of threads by default for
2075 * both the file case and the block device case. For the file
2076 * case, we need multiple threads to allow concurrency, because the
2077 * vnode interface is designed to be a blocking interface. For the
2078 * block device case, ZFS zvols at least will block the caller's
2079 * context in many instances, and so we need multiple threads to
2080 * overcome that problem. Other block devices don't need as many
2081 * threads, but they shouldn't cause too many problems.
2083 * If the user wants to just have a single thread for a block
2084 * device, he can specify that when the LUN is created, or change
2085 * the tunable/sysctl to alter the default number of threads.
2087 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2088 /*num threads*/num_threads,
2091 "%s taskq", be_lun->lunname);
2096 be_lun->num_threads = num_threads;
2098 mtx_lock(&softc->lock);
2100 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2102 mtx_unlock(&softc->lock);
2104 retval = ctl_add_lun(&be_lun->ctl_be_lun);
2106 mtx_lock(&softc->lock);
2107 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2110 mtx_unlock(&softc->lock);
2111 snprintf(req->error_str, sizeof(req->error_str),
2112 "ctl_add_lun() returned error %d, see dmesg for "
2118 mtx_lock(&softc->lock);
2121 * Tell the config_status routine that we're waiting so it won't
2122 * clean up the LUN in the event of an error.
2124 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2126 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2127 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2128 if (retval == EINTR)
2131 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2133 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2134 snprintf(req->error_str, sizeof(req->error_str),
2135 "LUN configuration error, see dmesg for details");
2136 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2139 mtx_unlock(&softc->lock);
2142 params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2145 mtx_unlock(&softc->lock);
2147 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2149 DEVSTAT_ALL_SUPPORTED,
2150 be_lun->ctl_be_lun.lun_type
2151 | DEVSTAT_TYPE_IF_OTHER,
2152 DEVSTAT_PRIORITY_OTHER);
2157 req->status = CTL_LUN_ERROR;
2159 if (be_lun->io_taskqueue != NULL)
2160 taskqueue_free(be_lun->io_taskqueue);
2161 ctl_be_block_close(be_lun);
2162 if (be_lun->dev_path != NULL)
2163 free(be_lun->dev_path, M_CTLBLK);
2164 if (be_lun->lun_zone != NULL)
2165 uma_zdestroy(be_lun->lun_zone);
2166 ctl_free_opts(&be_lun->ctl_be_lun.options);
2167 mtx_destroy(&be_lun->queue_lock);
2168 mtx_destroy(&be_lun->io_lock);
2169 free(be_lun, M_CTLBLK);
2175 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2177 struct ctl_lun_rm_params *params;
2178 struct ctl_be_block_lun *be_lun;
2181 params = &req->reqdata.rm;
2183 mtx_lock(&softc->lock);
2187 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2188 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2191 mtx_unlock(&softc->lock);
2193 if (be_lun == NULL) {
2194 snprintf(req->error_str, sizeof(req->error_str),
2195 "LUN %u is not managed by the block backend",
2200 retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2203 snprintf(req->error_str, sizeof(req->error_str),
2204 "error %d returned from ctl_disable_lun() for "
2205 "LUN %d", retval, params->lun_id);
2210 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2212 snprintf(req->error_str, sizeof(req->error_str),
2213 "error %d returned from ctl_invalidate_lun() for "
2214 "LUN %d", retval, params->lun_id);
2218 mtx_lock(&softc->lock);
2220 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2222 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2223 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2224 if (retval == EINTR)
2228 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2230 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2231 snprintf(req->error_str, sizeof(req->error_str),
2232 "interrupted waiting for LUN to be freed");
2233 mtx_unlock(&softc->lock);
2237 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2240 mtx_unlock(&softc->lock);
2242 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2244 taskqueue_free(be_lun->io_taskqueue);
2246 ctl_be_block_close(be_lun);
2248 if (be_lun->disk_stats != NULL)
2249 devstat_remove_entry(be_lun->disk_stats);
2251 uma_zdestroy(be_lun->lun_zone);
2253 ctl_free_opts(&be_lun->ctl_be_lun.options);
2254 free(be_lun->dev_path, M_CTLBLK);
2255 mtx_destroy(&be_lun->queue_lock);
2256 mtx_destroy(&be_lun->io_lock);
2257 free(be_lun, M_CTLBLK);
2259 req->status = CTL_LUN_OK;
2265 req->status = CTL_LUN_ERROR;
2271 ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2272 struct ctl_lun_req *req)
2276 struct ctl_lun_create_params *params = &be_lun->params;
2278 if (params->lun_size_bytes != 0) {
2279 be_lun->size_bytes = params->lun_size_bytes;
2281 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2282 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2283 VOP_UNLOCK(be_lun->vn, 0);
2285 snprintf(req->error_str, sizeof(req->error_str),
2286 "error calling VOP_GETATTR() for file %s",
2291 be_lun->size_bytes = vattr.va_size;
2298 ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2299 struct ctl_lun_req *req)
2301 struct ctl_be_block_devdata *dev_data;
2303 struct ctl_lun_create_params *params = &be_lun->params;
2304 uint64_t size_bytes;
2306 dev_data = &be_lun->backend.dev;
2307 if (!dev_data->csw->d_ioctl) {
2308 snprintf(req->error_str, sizeof(req->error_str),
2309 "no d_ioctl for device %s!", be_lun->dev_path);
2313 error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2314 (caddr_t)&size_bytes, FREAD,
2317 snprintf(req->error_str, sizeof(req->error_str),
2318 "error %d returned for DIOCGMEDIASIZE ioctl "
2319 "on %s!", error, be_lun->dev_path);
2323 if (params->lun_size_bytes != 0) {
2324 if (params->lun_size_bytes > size_bytes) {
2325 snprintf(req->error_str, sizeof(req->error_str),
2326 "requested LUN size %ju > backing device "
2328 (uintmax_t)params->lun_size_bytes,
2329 (uintmax_t)size_bytes);
2333 be_lun->size_bytes = params->lun_size_bytes;
2335 be_lun->size_bytes = size_bytes;
2342 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2344 struct ctl_lun_modify_params *params;
2345 struct ctl_be_block_lun *be_lun;
2349 params = &req->reqdata.modify;
2351 mtx_lock(&softc->lock);
2353 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2354 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2357 mtx_unlock(&softc->lock);
2359 if (be_lun == NULL) {
2360 snprintf(req->error_str, sizeof(req->error_str),
2361 "LUN %u is not managed by the block backend",
2366 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2368 oldsize = be_lun->size_blocks;
2369 if (be_lun->vn == NULL)
2370 error = ctl_be_block_open(softc, be_lun, req);
2371 else if (be_lun->vn->v_type == VREG)
2372 error = ctl_be_block_modify_file(be_lun, req);
2374 error = ctl_be_block_modify_dev(be_lun, req);
2376 if (error == 0 && be_lun->size_blocks != oldsize) {
2377 be_lun->size_blocks = be_lun->size_bytes >>
2378 be_lun->blocksize_shift;
2381 * The maximum LBA is the size - 1.
2383 * XXX: Note that this field is being updated without locking,
2384 * which might cause problems on 32-bit architectures.
2386 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2387 0 : (be_lun->size_blocks - 1);
2388 be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2389 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2390 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2391 if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2392 be_lun->blocksize != 0)
2393 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2395 ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2396 if (oldsize == 0 && be_lun->size_blocks != 0)
2397 ctl_lun_online(&be_lun->ctl_be_lun);
2400 /* Tell the user the exact size we ended up using */
2401 params->lun_size_bytes = be_lun->size_bytes;
2403 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2408 req->status = CTL_LUN_ERROR;
2414 ctl_be_block_lun_shutdown(void *be_lun)
2416 struct ctl_be_block_lun *lun;
2417 struct ctl_be_block_softc *softc;
2419 lun = (struct ctl_be_block_lun *)be_lun;
2423 mtx_lock(&softc->lock);
2424 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2425 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2427 mtx_unlock(&softc->lock);
2432 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2434 struct ctl_be_block_lun *lun;
2435 struct ctl_be_block_softc *softc;
2437 lun = (struct ctl_be_block_lun *)be_lun;
2440 if (status == CTL_LUN_CONFIG_OK) {
2441 mtx_lock(&softc->lock);
2442 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2443 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2445 mtx_unlock(&softc->lock);
2448 * We successfully added the LUN, attempt to enable it.
2450 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2451 printf("%s: ctl_enable_lun() failed!\n", __func__);
2452 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2453 printf("%s: ctl_invalidate_lun() failed!\n",
2462 mtx_lock(&softc->lock);
2463 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2464 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2466 mtx_unlock(&softc->lock);
2471 ctl_be_block_config_write(union ctl_io *io)
2473 struct ctl_be_block_lun *be_lun;
2474 struct ctl_be_lun *ctl_be_lun;
2479 DPRINTF("entered\n");
2481 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2482 CTL_PRIV_BACKEND_LUN].ptr;
2483 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2485 switch (io->scsiio.cdb[0]) {
2486 case SYNCHRONIZE_CACHE:
2487 case SYNCHRONIZE_CACHE_16:
2492 * The upper level CTL code will filter out any CDBs with
2493 * the immediate bit set and return the proper error.
2495 * We don't really need to worry about what LBA range the
2496 * user asked to be synced out. When they issue a sync
2497 * cache command, we'll sync out the whole thing.
2499 mtx_lock(&be_lun->queue_lock);
2500 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2502 mtx_unlock(&be_lun->queue_lock);
2503 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2505 case START_STOP_UNIT: {
2506 struct scsi_start_stop_unit *cdb;
2508 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2510 if (cdb->how & SSS_START)
2511 retval = ctl_start_lun(ctl_be_lun);
2513 retval = ctl_stop_lun(ctl_be_lun);
2515 * XXX KDM Copan-specific offline behavior.
2516 * Figure out a reasonable way to port this?
2520 && (cdb->byte2 & SSS_ONOFFLINE))
2521 retval = ctl_lun_offline(ctl_be_lun);
2526 * In general, the above routines should not fail. They
2527 * just set state for the LUN. So we've got something
2528 * pretty wrong here if we can't start or stop the LUN.
2531 ctl_set_internal_failure(&io->scsiio,
2533 /*retry_count*/ 0xf051);
2534 retval = CTL_RETVAL_COMPLETE;
2536 ctl_set_success(&io->scsiio);
2538 ctl_config_write_done(io);
2542 ctl_set_invalid_opcode(&io->scsiio);
2543 ctl_config_write_done(io);
2544 retval = CTL_RETVAL_COMPLETE;
2553 ctl_be_block_config_read(union ctl_io *io)
2559 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2561 struct ctl_be_block_lun *lun;
2564 lun = (struct ctl_be_block_lun *)be_lun;
2567 retval = sbuf_printf(sb, "\t<num_threads>");
2572 retval = sbuf_printf(sb, "%d", lun->num_threads);
2577 retval = sbuf_printf(sb, "</num_threads>\n");
2585 ctl_be_block_init(void)
2587 struct ctl_be_block_softc *softc;
2590 softc = &backend_block_softc;
2593 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2594 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2595 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2596 STAILQ_INIT(&softc->disk_list);
2597 STAILQ_INIT(&softc->lun_list);