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.flags & CTL_FLAG_ABORT) {
386 } else if ((io->io_hdr.port_status == 0) &&
387 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
388 lbalen = ARGS(beio->io);
389 if (lbalen->flags & CTL_LLF_READ) {
390 ctl_set_success(&io->scsiio);
391 } else if (lbalen->flags & CTL_LLF_COMPARE) {
392 /* We have two data blocks ready for comparison. */
393 for (i = 0; i < beio->num_segs; i++) {
394 if (memcmp(beio->sg_segs[i].addr,
395 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
396 beio->sg_segs[i].len) != 0)
399 if (i < beio->num_segs)
400 ctl_set_sense(&io->scsiio,
402 /*sense_key*/ SSD_KEY_MISCOMPARE,
407 ctl_set_success(&io->scsiio);
409 } else if ((io->io_hdr.port_status != 0) &&
410 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
411 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
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);
535 if ((ARGS(io)->flags & CTL_LLF_READ) &&
536 beio->beio_cont == NULL)
537 ctl_set_success(&io->scsiio);
539 getbintime(&io->io_hdr.dma_start_bt);
546 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
547 struct ctl_be_block_io *beio)
549 union ctl_io *io = beio->io;
550 struct mount *mountpoint;
551 int error, lock_flags;
553 DPRINTF("entered\n");
555 binuptime(&beio->ds_t0);
556 mtx_lock(&be_lun->io_lock);
557 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
558 mtx_unlock(&be_lun->io_lock);
560 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
562 if (MNT_SHARED_WRITES(mountpoint)
563 || ((mountpoint == NULL)
564 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
565 lock_flags = LK_SHARED;
567 lock_flags = LK_EXCLUSIVE;
569 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
571 error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
572 VOP_UNLOCK(be_lun->vn, 0);
574 vn_finished_write(mountpoint);
576 mtx_lock(&be_lun->io_lock);
577 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
578 beio->ds_tag_type, beio->ds_trans_type,
579 /*now*/ NULL, /*then*/&beio->ds_t0);
580 mtx_unlock(&be_lun->io_lock);
583 ctl_set_success(&io->scsiio);
585 /* XXX KDM is there is a better error here? */
586 ctl_set_internal_failure(&io->scsiio,
588 /*retry_count*/ 0xbad1);
591 ctl_complete_beio(beio);
594 SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
595 SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
596 SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
597 SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
600 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
601 struct ctl_be_block_io *beio)
603 struct ctl_be_block_filedata *file_data;
606 struct iovec *xiovec;
610 DPRINTF("entered\n");
612 file_data = &be_lun->backend.file;
615 if (ARGS(io)->flags & CTL_LLF_DPO)
617 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
620 bzero(&xuio, sizeof(xuio));
621 if (beio->bio_cmd == BIO_READ) {
622 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
623 xuio.uio_rw = UIO_READ;
625 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
626 xuio.uio_rw = UIO_WRITE;
628 xuio.uio_offset = beio->io_offset;
629 xuio.uio_resid = beio->io_len;
630 xuio.uio_segflg = UIO_SYSSPACE;
631 xuio.uio_iov = beio->xiovecs;
632 xuio.uio_iovcnt = beio->num_segs;
633 xuio.uio_td = curthread;
635 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
636 xiovec->iov_base = beio->sg_segs[i].addr;
637 xiovec->iov_len = beio->sg_segs[i].len;
640 binuptime(&beio->ds_t0);
641 mtx_lock(&be_lun->io_lock);
642 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
643 mtx_unlock(&be_lun->io_lock);
645 if (beio->bio_cmd == BIO_READ) {
646 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
649 * UFS pays attention to IO_DIRECT for reads. If the
650 * DIRECTIO option is configured into the kernel, it calls
651 * ffs_rawread(). But that only works for single-segment
652 * uios with user space addresses. In our case, with a
653 * kernel uio, it still reads into the buffer cache, but it
654 * will just try to release the buffer from the cache later
657 * ZFS does not pay attention to IO_DIRECT for reads.
659 * UFS does not pay attention to IO_SYNC for reads.
661 * ZFS pays attention to IO_SYNC (which translates into the
662 * Solaris define FRSYNC for zfs_read()) for reads. It
663 * attempts to sync the file before reading.
665 * So, to attempt to provide some barrier semantics in the
666 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
668 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
670 VOP_UNLOCK(be_lun->vn, 0);
671 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
673 struct mount *mountpoint;
676 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
678 if (MNT_SHARED_WRITES(mountpoint)
679 || ((mountpoint == NULL)
680 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
681 lock_flags = LK_SHARED;
683 lock_flags = LK_EXCLUSIVE;
685 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
688 * UFS pays attention to IO_DIRECT for writes. The write
689 * is done asynchronously. (Normally the write would just
690 * get put into cache.
692 * UFS pays attention to IO_SYNC for writes. It will
693 * attempt to write the buffer out synchronously if that
696 * ZFS does not pay attention to IO_DIRECT for writes.
698 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
699 * for writes. It will flush the transaction from the
700 * cache before returning.
702 * So if we've got the BIO_ORDERED flag set, we want
703 * IO_SYNC in either the UFS or ZFS case.
705 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
706 VOP_UNLOCK(be_lun->vn, 0);
708 vn_finished_write(mountpoint);
709 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
712 mtx_lock(&be_lun->io_lock);
713 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
714 beio->ds_tag_type, beio->ds_trans_type,
715 /*now*/ NULL, /*then*/&beio->ds_t0);
716 mtx_unlock(&be_lun->io_lock);
719 * If we got an error, set the sense data to "MEDIUM ERROR" and
720 * return the I/O to the user.
725 ctl_scsi_path_string(io, path_str, sizeof(path_str));
726 printf("%s%s command returned errno %d\n", path_str,
727 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
728 if (error == ENOSPC) {
729 ctl_set_space_alloc_fail(&io->scsiio);
731 ctl_set_medium_error(&io->scsiio);
732 ctl_complete_beio(beio);
737 * If this is a write or a verify, we're all done.
738 * If this is a read, we can now send the data to the user.
740 if ((beio->bio_cmd == BIO_WRITE) ||
741 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
742 ctl_set_success(&io->scsiio);
743 ctl_complete_beio(beio);
745 if ((ARGS(io)->flags & CTL_LLF_READ) &&
746 beio->beio_cont == NULL)
747 ctl_set_success(&io->scsiio);
749 getbintime(&io->io_hdr.dma_start_bt);
756 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
757 struct ctl_be_block_io *beio)
759 struct ctl_be_block_devdata *dev_data;
762 struct iovec *xiovec;
766 DPRINTF("entered\n");
768 dev_data = &be_lun->backend.dev;
771 if (ARGS(io)->flags & CTL_LLF_DPO)
773 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
776 bzero(&xuio, sizeof(xuio));
777 if (beio->bio_cmd == BIO_READ) {
778 SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
779 xuio.uio_rw = UIO_READ;
781 SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
782 xuio.uio_rw = UIO_WRITE;
784 xuio.uio_offset = beio->io_offset;
785 xuio.uio_resid = beio->io_len;
786 xuio.uio_segflg = UIO_SYSSPACE;
787 xuio.uio_iov = beio->xiovecs;
788 xuio.uio_iovcnt = beio->num_segs;
789 xuio.uio_td = curthread;
791 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
792 xiovec->iov_base = beio->sg_segs[i].addr;
793 xiovec->iov_len = beio->sg_segs[i].len;
796 binuptime(&beio->ds_t0);
797 mtx_lock(&be_lun->io_lock);
798 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
799 mtx_unlock(&be_lun->io_lock);
801 if (beio->bio_cmd == BIO_READ) {
802 error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
803 SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
805 error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
806 SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
809 mtx_lock(&be_lun->io_lock);
810 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
811 beio->ds_tag_type, beio->ds_trans_type,
812 /*now*/ NULL, /*then*/&beio->ds_t0);
813 mtx_unlock(&be_lun->io_lock);
816 * If we got an error, set the sense data to "MEDIUM ERROR" and
817 * return the I/O to the user.
820 if (error == ENOSPC) {
821 ctl_set_space_alloc_fail(&io->scsiio);
823 ctl_set_medium_error(&io->scsiio);
824 ctl_complete_beio(beio);
829 * If this is a write or a verify, we're all done.
830 * If this is a read, we can now send the data to the user.
832 if ((beio->bio_cmd == BIO_WRITE) ||
833 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
834 ctl_set_success(&io->scsiio);
835 ctl_complete_beio(beio);
837 if ((ARGS(io)->flags & CTL_LLF_READ) &&
838 beio->beio_cont == NULL)
839 ctl_set_success(&io->scsiio);
841 getbintime(&io->io_hdr.dma_start_bt);
848 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
849 struct ctl_be_block_io *beio)
853 struct ctl_be_block_devdata *dev_data;
855 dev_data = &be_lun->backend.dev;
858 DPRINTF("entered\n");
860 /* This can't fail, it's a blocking allocation. */
863 bio->bio_cmd = BIO_FLUSH;
864 bio->bio_flags |= BIO_ORDERED;
865 bio->bio_dev = dev_data->cdev;
868 bio->bio_done = ctl_be_block_biodone;
869 bio->bio_caller1 = beio;
873 * We don't need to acquire the LUN lock here, because we are only
874 * sending one bio, and so there is no other context to synchronize
877 beio->num_bios_sent = 1;
878 beio->send_complete = 1;
880 binuptime(&beio->ds_t0);
881 mtx_lock(&be_lun->io_lock);
882 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
883 mtx_unlock(&be_lun->io_lock);
885 (*dev_data->csw->d_strategy)(bio);
889 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
890 struct ctl_be_block_io *beio,
891 uint64_t off, uint64_t len, int last)
894 struct ctl_be_block_devdata *dev_data;
897 dev_data = &be_lun->backend.dev;
898 maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
901 bio->bio_cmd = BIO_DELETE;
902 bio->bio_dev = dev_data->cdev;
903 bio->bio_offset = off;
904 bio->bio_length = MIN(len, maxlen);
906 bio->bio_done = ctl_be_block_biodone;
907 bio->bio_caller1 = beio;
908 bio->bio_pblkno = off / be_lun->blocksize;
910 off += bio->bio_length;
911 len -= bio->bio_length;
913 mtx_lock(&be_lun->io_lock);
914 beio->num_bios_sent++;
915 if (last && len == 0)
916 beio->send_complete = 1;
917 mtx_unlock(&be_lun->io_lock);
919 (*dev_data->csw->d_strategy)(bio);
924 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
925 struct ctl_be_block_io *beio)
928 struct ctl_be_block_devdata *dev_data;
929 struct ctl_ptr_len_flags *ptrlen;
930 struct scsi_unmap_desc *buf, *end;
933 dev_data = &be_lun->backend.dev;
936 DPRINTF("entered\n");
938 binuptime(&beio->ds_t0);
939 mtx_lock(&be_lun->io_lock);
940 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
941 mtx_unlock(&be_lun->io_lock);
943 if (beio->io_offset == -1) {
945 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
946 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
947 end = buf + ptrlen->len / sizeof(*buf);
948 for (; buf < end; buf++) {
949 len = (uint64_t)scsi_4btoul(buf->length) *
952 ctl_be_block_unmap_dev_range(be_lun, beio,
953 scsi_8btou64(buf->lba) * be_lun->blocksize, len,
954 (end - buf < 2) ? TRUE : FALSE);
957 ctl_be_block_unmap_dev_range(be_lun, beio,
958 beio->io_offset, beio->io_len, TRUE);
962 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
963 struct ctl_be_block_io *beio)
965 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
968 struct ctl_be_block_devdata *dev_data;
972 DPRINTF("entered\n");
974 dev_data = &be_lun->backend.dev;
977 * We have to limit our I/O size to the maximum supported by the
978 * backend device. Hopefully it is MAXPHYS. If the driver doesn't
979 * set it properly, use DFLTPHYS.
981 max_iosize = dev_data->cdev->si_iosize_max;
982 if (max_iosize < PAGE_SIZE)
983 max_iosize = DFLTPHYS;
985 cur_offset = beio->io_offset;
986 for (i = 0; i < beio->num_segs; i++) {
990 cur_size = beio->sg_segs[i].len;
991 cur_ptr = beio->sg_segs[i].addr;
993 while (cur_size > 0) {
994 /* This can't fail, it's a blocking allocation. */
997 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
999 bio->bio_cmd = beio->bio_cmd;
1000 bio->bio_dev = dev_data->cdev;
1001 bio->bio_caller1 = beio;
1002 bio->bio_length = min(cur_size, max_iosize);
1003 bio->bio_offset = cur_offset;
1004 bio->bio_data = cur_ptr;
1005 bio->bio_done = ctl_be_block_biodone;
1006 bio->bio_pblkno = cur_offset / be_lun->blocksize;
1008 cur_offset += bio->bio_length;
1009 cur_ptr += bio->bio_length;
1010 cur_size -= bio->bio_length;
1012 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1013 beio->num_bios_sent++;
1016 binuptime(&beio->ds_t0);
1017 mtx_lock(&be_lun->io_lock);
1018 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1019 beio->send_complete = 1;
1020 mtx_unlock(&be_lun->io_lock);
1023 * Fire off all allocated requests!
1025 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1026 TAILQ_REMOVE(&queue, bio, bio_queue);
1027 (*dev_data->csw->d_strategy)(bio);
1032 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1034 struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev;
1035 struct diocgattr_arg arg;
1038 if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL)
1039 return (UINT64_MAX);
1040 strlcpy(arg.name, attrname, sizeof(arg.name));
1041 arg.len = sizeof(arg.value.off);
1042 error = dev_data->csw->d_ioctl(dev_data->cdev,
1043 DIOCGATTR, (caddr_t)&arg, FREAD, curthread);
1045 return (UINT64_MAX);
1046 return (arg.value.off);
1050 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1055 ctl_free_beio(beio);
1056 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1057 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1058 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1059 ctl_config_write_done(io);
1063 ctl_be_block_config_write(io);
1067 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1070 struct ctl_be_block_io *beio;
1071 struct ctl_be_block_softc *softc;
1072 struct ctl_lba_len_flags *lbalen;
1073 uint64_t len_left, lba;
1077 DPRINTF("entered\n");
1079 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1080 softc = be_lun->softc;
1081 lbalen = ARGS(beio->io);
1083 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1084 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1085 ctl_free_beio(beio);
1086 ctl_set_invalid_field(&io->scsiio,
1092 ctl_config_write_done(io);
1096 switch (io->scsiio.tag_type) {
1097 case CTL_TAG_ORDERED:
1098 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1100 case CTL_TAG_HEAD_OF_QUEUE:
1101 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1103 case CTL_TAG_UNTAGGED:
1104 case CTL_TAG_SIMPLE:
1107 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1111 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1112 beio->io_offset = lbalen->lba * be_lun->blocksize;
1113 beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1114 beio->bio_cmd = BIO_DELETE;
1115 beio->ds_trans_type = DEVSTAT_FREE;
1117 be_lun->unmap(be_lun, beio);
1121 beio->bio_cmd = BIO_WRITE;
1122 beio->ds_trans_type = DEVSTAT_WRITE;
1124 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1125 (uintmax_t)lbalen->lba, lbalen->len);
1127 len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1128 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1131 * Setup the S/G entry for this chunk.
1133 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1134 seglen -= seglen % be_lun->blocksize;
1135 beio->sg_segs[i].len = seglen;
1136 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1138 DPRINTF("segment %d addr %p len %zd\n", i,
1139 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1144 buf = beio->sg_segs[i].addr;
1146 for (; buf < end; buf += be_lun->blocksize) {
1147 memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1148 if (lbalen->flags & SWS_LBDATA)
1149 scsi_ulto4b(lbalen->lba + lba, buf);
1154 beio->io_offset = lbalen->lba * be_lun->blocksize;
1155 beio->io_len = lba * be_lun->blocksize;
1157 /* We can not do all in one run. Correct and schedule rerun. */
1161 beio->beio_cont = ctl_be_block_cw_done_ws;
1164 be_lun->dispatch(be_lun, beio);
1168 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1171 struct ctl_be_block_io *beio;
1172 struct ctl_be_block_softc *softc;
1173 struct ctl_ptr_len_flags *ptrlen;
1175 DPRINTF("entered\n");
1177 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1178 softc = be_lun->softc;
1179 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1181 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1182 ctl_free_beio(beio);
1183 ctl_set_invalid_field(&io->scsiio,
1189 ctl_config_write_done(io);
1193 switch (io->scsiio.tag_type) {
1194 case CTL_TAG_ORDERED:
1195 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1197 case CTL_TAG_HEAD_OF_QUEUE:
1198 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1200 case CTL_TAG_UNTAGGED:
1201 case CTL_TAG_SIMPLE:
1204 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1209 beio->io_offset = -1;
1211 beio->bio_cmd = BIO_DELETE;
1212 beio->ds_trans_type = DEVSTAT_FREE;
1216 be_lun->unmap(be_lun, beio);
1220 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1225 ctl_free_beio(beio);
1226 ctl_config_write_done(io);
1230 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1233 struct ctl_be_block_io *beio;
1234 struct ctl_be_block_softc *softc;
1236 DPRINTF("entered\n");
1238 softc = be_lun->softc;
1239 beio = ctl_alloc_beio(softc);
1242 beio->beio_cont = ctl_be_block_cw_done;
1243 PRIV(io)->ptr = (void *)beio;
1245 switch (io->scsiio.cdb[0]) {
1246 case SYNCHRONIZE_CACHE:
1247 case SYNCHRONIZE_CACHE_16:
1248 beio->bio_cmd = BIO_FLUSH;
1249 beio->ds_trans_type = DEVSTAT_NO_DATA;
1250 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1252 be_lun->lun_flush(be_lun, beio);
1256 ctl_be_block_cw_dispatch_ws(be_lun, io);
1259 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1262 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1267 SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1268 SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1269 SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1270 SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1273 ctl_be_block_next(struct ctl_be_block_io *beio)
1275 struct ctl_be_block_lun *be_lun;
1280 ctl_free_beio(beio);
1281 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1282 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1283 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1284 ctl_data_submit_done(io);
1288 io->io_hdr.status &= ~CTL_STATUS_MASK;
1289 io->io_hdr.status |= CTL_STATUS_NONE;
1291 mtx_lock(&be_lun->queue_lock);
1293 * XXX KDM make sure that links is okay to use at this point.
1294 * Otherwise, we either need to add another field to ctl_io_hdr,
1295 * or deal with resource allocation here.
1297 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1298 mtx_unlock(&be_lun->queue_lock);
1300 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1304 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1307 struct ctl_be_block_io *beio;
1308 struct ctl_be_block_softc *softc;
1309 struct ctl_lba_len_flags *lbalen;
1310 struct ctl_ptr_len_flags *bptrlen;
1311 uint64_t len_left, lbas;
1314 softc = be_lun->softc;
1316 DPRINTF("entered\n");
1319 if (lbalen->flags & CTL_LLF_WRITE) {
1320 SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1322 SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1325 beio = ctl_alloc_beio(softc);
1329 bptrlen->ptr = (void *)beio;
1331 switch (io->scsiio.tag_type) {
1332 case CTL_TAG_ORDERED:
1333 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1335 case CTL_TAG_HEAD_OF_QUEUE:
1336 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1338 case CTL_TAG_UNTAGGED:
1339 case CTL_TAG_SIMPLE:
1342 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1346 if (lbalen->flags & CTL_LLF_WRITE) {
1347 beio->bio_cmd = BIO_WRITE;
1348 beio->ds_trans_type = DEVSTAT_WRITE;
1350 beio->bio_cmd = BIO_READ;
1351 beio->ds_trans_type = DEVSTAT_READ;
1354 DPRINTF("%s at LBA %jx len %u @%ju\n",
1355 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1356 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1357 if (lbalen->flags & CTL_LLF_COMPARE)
1358 lbas = CTLBLK_HALF_IO_SIZE;
1360 lbas = CTLBLK_MAX_IO_SIZE;
1361 lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1362 beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1363 beio->io_len = lbas * be_lun->blocksize;
1364 bptrlen->len += lbas;
1366 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1367 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1368 i, CTLBLK_MAX_SEGS));
1371 * Setup the S/G entry for this chunk.
1373 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1374 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1376 DPRINTF("segment %d addr %p len %zd\n", i,
1377 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1379 /* Set up second segment for compare operation. */
1380 if (lbalen->flags & CTL_LLF_COMPARE) {
1381 beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1382 beio->sg_segs[i].len;
1383 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1384 uma_zalloc(be_lun->lun_zone, M_WAITOK);
1388 len_left -= beio->sg_segs[i].len;
1390 if (bptrlen->len < lbalen->len)
1391 beio->beio_cont = ctl_be_block_next;
1392 io->scsiio.be_move_done = ctl_be_block_move_done;
1393 /* For compare we have separate S/G lists for read and datamove. */
1394 if (lbalen->flags & CTL_LLF_COMPARE)
1395 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1397 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1398 io->scsiio.kern_data_len = beio->io_len;
1399 io->scsiio.kern_data_resid = 0;
1400 io->scsiio.kern_sg_entries = beio->num_segs;
1401 io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1404 * For the read case, we need to read the data into our buffers and
1405 * then we can send it back to the user. For the write case, we
1406 * need to get the data from the user first.
1408 if (beio->bio_cmd == BIO_READ) {
1409 SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1410 be_lun->dispatch(be_lun, beio);
1412 SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1414 getbintime(&io->io_hdr.dma_start_bt);
1421 ctl_be_block_worker(void *context, int pending)
1423 struct ctl_be_block_lun *be_lun;
1424 struct ctl_be_block_softc *softc;
1427 be_lun = (struct ctl_be_block_lun *)context;
1428 softc = be_lun->softc;
1430 DPRINTF("entered\n");
1432 mtx_lock(&be_lun->queue_lock);
1434 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1436 struct ctl_be_block_io *beio;
1438 DPRINTF("datamove queue\n");
1440 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1443 mtx_unlock(&be_lun->queue_lock);
1445 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1447 be_lun->dispatch(be_lun, beio);
1449 mtx_lock(&be_lun->queue_lock);
1452 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1455 DPRINTF("config write queue\n");
1457 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1460 mtx_unlock(&be_lun->queue_lock);
1462 ctl_be_block_cw_dispatch(be_lun, io);
1464 mtx_lock(&be_lun->queue_lock);
1467 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1469 DPRINTF("input queue\n");
1471 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1473 mtx_unlock(&be_lun->queue_lock);
1476 * We must drop the lock, since this routine and
1477 * its children may sleep.
1479 ctl_be_block_dispatch(be_lun, io);
1481 mtx_lock(&be_lun->queue_lock);
1486 * If we get here, there is no work left in the queues, so
1487 * just break out and let the task queue go to sleep.
1491 mtx_unlock(&be_lun->queue_lock);
1495 * Entry point from CTL to the backend for I/O. We queue everything to a
1496 * work thread, so this just puts the I/O on a queue and wakes up the
1500 ctl_be_block_submit(union ctl_io *io)
1502 struct ctl_be_block_lun *be_lun;
1503 struct ctl_be_lun *ctl_be_lun;
1505 DPRINTF("entered\n");
1507 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1508 CTL_PRIV_BACKEND_LUN].ptr;
1509 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1512 * Make sure we only get SCSI I/O.
1514 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1515 "%#x) encountered", io->io_hdr.io_type));
1519 mtx_lock(&be_lun->queue_lock);
1521 * XXX KDM make sure that links is okay to use at this point.
1522 * Otherwise, we either need to add another field to ctl_io_hdr,
1523 * or deal with resource allocation here.
1525 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1526 mtx_unlock(&be_lun->queue_lock);
1527 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1529 return (CTL_RETVAL_COMPLETE);
1533 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1534 int flag, struct thread *td)
1536 struct ctl_be_block_softc *softc;
1539 softc = &backend_block_softc;
1545 struct ctl_lun_req *lun_req;
1547 lun_req = (struct ctl_lun_req *)addr;
1549 switch (lun_req->reqtype) {
1550 case CTL_LUNREQ_CREATE:
1551 error = ctl_be_block_create(softc, lun_req);
1554 error = ctl_be_block_rm(softc, lun_req);
1556 case CTL_LUNREQ_MODIFY:
1557 error = ctl_be_block_modify(softc, lun_req);
1560 lun_req->status = CTL_LUN_ERROR;
1561 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1562 "invalid LUN request type %d",
1577 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1579 struct ctl_be_block_filedata *file_data;
1580 struct ctl_lun_create_params *params;
1586 file_data = &be_lun->backend.file;
1587 params = &be_lun->params;
1589 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1590 be_lun->dispatch = ctl_be_block_dispatch_file;
1591 be_lun->lun_flush = ctl_be_block_flush_file;
1593 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1595 snprintf(req->error_str, sizeof(req->error_str),
1596 "error calling VOP_GETATTR() for file %s",
1602 * Verify that we have the ability to upgrade to exclusive
1603 * access on this file so we can trap errors at open instead
1604 * of reporting them during first access.
1606 if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1607 vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1608 if (be_lun->vn->v_iflag & VI_DOOMED) {
1610 snprintf(req->error_str, sizeof(req->error_str),
1611 "error locking file %s", be_lun->dev_path);
1617 file_data->cred = crhold(curthread->td_ucred);
1618 if (params->lun_size_bytes != 0)
1619 be_lun->size_bytes = params->lun_size_bytes;
1621 be_lun->size_bytes = vattr.va_size;
1623 * We set the multi thread flag for file operations because all
1624 * filesystems (in theory) are capable of allowing multiple readers
1625 * of a file at once. So we want to get the maximum possible
1628 be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1631 * For files we can use any logical block size. Prefer 512 bytes
1632 * for compatibility reasons. If file's vattr.va_blocksize
1633 * (preferred I/O block size) is bigger and multiple to chosen
1634 * logical block size -- report it as physical block size.
1636 if (params->blocksize_bytes != 0)
1637 be_lun->blocksize = params->blocksize_bytes;
1639 be_lun->blocksize = 512;
1640 pss = vattr.va_blocksize / be_lun->blocksize;
1641 if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) &&
1642 ((pss & (pss - 1)) == 0)) {
1643 be_lun->pblockexp = fls(pss) - 1;
1644 be_lun->pblockoff = 0;
1648 * Sanity check. The media size has to be at least one
1651 if (be_lun->size_bytes < be_lun->blocksize) {
1653 snprintf(req->error_str, sizeof(req->error_str),
1654 "file %s size %ju < block size %u", be_lun->dev_path,
1655 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1661 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1663 struct ctl_lun_create_params *params;
1666 struct cdevsw *devsw;
1668 off_t ps, pss, po, pos;
1670 params = &be_lun->params;
1672 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1673 be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1674 be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1675 &be_lun->backend.dev.dev_ref);
1676 if (be_lun->backend.dev.csw == NULL)
1677 panic("Unable to retrieve device switch");
1678 if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0)
1679 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1681 be_lun->dispatch = ctl_be_block_dispatch_dev;
1682 be_lun->lun_flush = ctl_be_block_flush_dev;
1683 be_lun->unmap = ctl_be_block_unmap_dev;
1684 be_lun->getattr = ctl_be_block_getattr_dev;
1686 error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1688 snprintf(req->error_str, sizeof(req->error_str),
1689 "error getting vnode attributes for device %s",
1694 dev = be_lun->vn->v_rdev;
1695 devsw = dev->si_devsw;
1696 if (!devsw->d_ioctl) {
1697 snprintf(req->error_str, sizeof(req->error_str),
1698 "no d_ioctl for device %s!",
1703 error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1704 (caddr_t)&be_lun->blocksize, FREAD,
1707 snprintf(req->error_str, sizeof(req->error_str),
1708 "error %d returned for DIOCGSECTORSIZE ioctl "
1709 "on %s!", error, be_lun->dev_path);
1714 * If the user has asked for a blocksize that is greater than the
1715 * backing device's blocksize, we can do it only if the blocksize
1716 * the user is asking for is an even multiple of the underlying
1717 * device's blocksize.
1719 if ((params->blocksize_bytes != 0)
1720 && (params->blocksize_bytes > be_lun->blocksize)) {
1721 uint32_t bs_multiple, tmp_blocksize;
1723 bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1725 tmp_blocksize = bs_multiple * be_lun->blocksize;
1727 if (tmp_blocksize == params->blocksize_bytes) {
1728 be_lun->blocksize = params->blocksize_bytes;
1730 snprintf(req->error_str, sizeof(req->error_str),
1731 "requested blocksize %u is not an even "
1732 "multiple of backing device blocksize %u",
1733 params->blocksize_bytes,
1738 } else if ((params->blocksize_bytes != 0)
1739 && (params->blocksize_bytes != be_lun->blocksize)) {
1740 snprintf(req->error_str, sizeof(req->error_str),
1741 "requested blocksize %u < backing device "
1742 "blocksize %u", params->blocksize_bytes,
1747 error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1748 (caddr_t)&be_lun->size_bytes, FREAD,
1751 snprintf(req->error_str, sizeof(req->error_str),
1752 "error %d returned for DIOCGMEDIASIZE "
1753 " ioctl on %s!", error,
1758 if (params->lun_size_bytes != 0) {
1759 if (params->lun_size_bytes > be_lun->size_bytes) {
1760 snprintf(req->error_str, sizeof(req->error_str),
1761 "requested LUN size %ju > backing device "
1763 (uintmax_t)params->lun_size_bytes,
1764 (uintmax_t)be_lun->size_bytes);
1768 be_lun->size_bytes = params->lun_size_bytes;
1771 error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1772 (caddr_t)&ps, FREAD, curthread);
1776 error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1777 (caddr_t)&po, FREAD, curthread);
1781 pss = ps / be_lun->blocksize;
1782 pos = po / be_lun->blocksize;
1783 if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1784 ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1785 be_lun->pblockexp = fls(pss) - 1;
1786 be_lun->pblockoff = (pss - pos) % pss;
1793 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
1797 int flags = FREAD | FWRITE;
1799 switch (be_lun->dev_type) {
1800 case CTL_BE_BLOCK_DEV:
1801 if (be_lun->backend.dev.csw) {
1802 dev_relthread(be_lun->backend.dev.cdev,
1803 be_lun->backend.dev.dev_ref);
1804 be_lun->backend.dev.csw = NULL;
1805 be_lun->backend.dev.cdev = NULL;
1808 case CTL_BE_BLOCK_FILE:
1810 case CTL_BE_BLOCK_NONE:
1813 panic("Unexpected backend type.");
1817 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1820 switch (be_lun->dev_type) {
1821 case CTL_BE_BLOCK_DEV:
1823 case CTL_BE_BLOCK_FILE:
1824 if (be_lun->backend.file.cred != NULL) {
1825 crfree(be_lun->backend.file.cred);
1826 be_lun->backend.file.cred = NULL;
1829 case CTL_BE_BLOCK_NONE:
1832 panic("Unexpected backend type.");
1835 be_lun->dev_type = CTL_BE_BLOCK_NONE;
1843 ctl_be_block_open(struct ctl_be_block_softc *softc,
1844 struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1846 struct nameidata nd;
1851 * XXX KDM allow a read-only option?
1853 flags = FREAD | FWRITE;
1856 if (rootvnode == NULL) {
1857 snprintf(req->error_str, sizeof(req->error_str),
1858 "Root filesystem is not mounted");
1862 if (!curthread->td_proc->p_fd->fd_cdir) {
1863 curthread->td_proc->p_fd->fd_cdir = rootvnode;
1866 if (!curthread->td_proc->p_fd->fd_rdir) {
1867 curthread->td_proc->p_fd->fd_rdir = rootvnode;
1870 if (!curthread->td_proc->p_fd->fd_jdir) {
1871 curthread->td_proc->p_fd->fd_jdir = rootvnode;
1876 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1877 error = vn_open(&nd, &flags, 0, NULL);
1880 * This is the only reasonable guess we can make as far as
1881 * path if the user doesn't give us a fully qualified path.
1882 * If they want to specify a file, they need to specify the
1885 if (be_lun->dev_path[0] != '/') {
1886 char *dev_path = "/dev/";
1889 /* Try adding device path at beginning of name */
1890 dev_name = malloc(strlen(be_lun->dev_path)
1891 + strlen(dev_path) + 1,
1892 M_CTLBLK, M_WAITOK);
1894 sprintf(dev_name, "%s%s", dev_path,
1896 free(be_lun->dev_path, M_CTLBLK);
1897 be_lun->dev_path = dev_name;
1901 snprintf(req->error_str, sizeof(req->error_str),
1902 "error opening %s: %d", be_lun->dev_path, error);
1906 NDFREE(&nd, NDF_ONLY_PNBUF);
1908 be_lun->vn = nd.ni_vp;
1910 /* We only support disks and files. */
1911 if (vn_isdisk(be_lun->vn, &error)) {
1912 error = ctl_be_block_open_dev(be_lun, req);
1913 } else if (be_lun->vn->v_type == VREG) {
1914 error = ctl_be_block_open_file(be_lun, req);
1917 snprintf(req->error_str, sizeof(req->error_str),
1918 "%s is not a disk or plain file", be_lun->dev_path);
1920 VOP_UNLOCK(be_lun->vn, 0);
1923 ctl_be_block_close(be_lun);
1927 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1928 be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1934 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1936 struct ctl_be_block_lun *be_lun;
1937 struct ctl_lun_create_params *params;
1938 char num_thread_str[16];
1941 int retval, num_threads, unmap;
1942 int tmp_num_threads;
1944 params = &req->reqdata.create;
1946 req->status = CTL_LUN_OK;
1948 num_threads = cbb_num_threads;
1950 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1952 be_lun->params = req->reqdata.create;
1953 be_lun->softc = softc;
1954 STAILQ_INIT(&be_lun->input_queue);
1955 STAILQ_INIT(&be_lun->config_write_queue);
1956 STAILQ_INIT(&be_lun->datamove_queue);
1957 sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1958 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
1959 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
1960 ctl_init_opts(&be_lun->ctl_be_lun.options,
1961 req->num_be_args, req->kern_be_args);
1963 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1964 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1966 if (be_lun->lun_zone == NULL) {
1967 snprintf(req->error_str, sizeof(req->error_str),
1968 "error allocating UMA zone");
1972 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1973 be_lun->ctl_be_lun.lun_type = params->device_type;
1975 be_lun->ctl_be_lun.lun_type = T_DIRECT;
1977 if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1978 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
1979 if (value == NULL) {
1980 snprintf(req->error_str, sizeof(req->error_str),
1981 "no file argument specified");
1984 be_lun->dev_path = strdup(value, M_CTLBLK);
1985 be_lun->blocksize = 512;
1986 be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1988 retval = ctl_be_block_open(softc, be_lun, req);
1991 req->status = CTL_LUN_WARNING;
1995 * For processor devices, we don't have any size.
1997 be_lun->blocksize = 0;
1998 be_lun->pblockexp = 0;
1999 be_lun->pblockoff = 0;
2000 be_lun->size_blocks = 0;
2001 be_lun->size_bytes = 0;
2002 be_lun->ctl_be_lun.maxlba = 0;
2005 * Default to just 1 thread for processor devices.
2011 * XXX This searching loop might be refactored to be combined with
2014 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
2015 if (value != NULL) {
2016 tmp_num_threads = strtol(value, NULL, 0);
2019 * We don't let the user specify less than one
2020 * thread, but hope he's clueful enough not to
2021 * specify 1000 threads.
2023 if (tmp_num_threads < 1) {
2024 snprintf(req->error_str, sizeof(req->error_str),
2025 "invalid number of threads %s",
2029 num_threads = tmp_num_threads;
2031 unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol);
2032 value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
2034 unmap = (strcmp(value, "on") == 0);
2036 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2037 be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2038 if (be_lun->vn == NULL)
2039 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE;
2041 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2042 be_lun->ctl_be_lun.be_lun = be_lun;
2043 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2044 0 : (be_lun->size_blocks - 1);
2045 be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2046 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2047 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2048 if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2049 be_lun->blocksize != 0)
2050 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2052 /* Tell the user the blocksize we ended up using */
2053 params->lun_size_bytes = be_lun->size_bytes;
2054 params->blocksize_bytes = be_lun->blocksize;
2055 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2056 be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2057 be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2059 be_lun->ctl_be_lun.req_lun_id = 0;
2061 be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2062 be_lun->ctl_be_lun.lun_config_status =
2063 ctl_be_block_lun_config_status;
2064 be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2066 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2067 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2069 strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2070 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2073 /* Tell the user what we used for a serial number */
2074 strncpy((char *)params->serial_num, tmpstr,
2075 ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2077 strncpy((char *)be_lun->ctl_be_lun.serial_num,
2079 ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2080 sizeof(params->serial_num)));
2082 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2083 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2084 strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2085 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2088 /* Tell the user what we used for a device ID */
2089 strncpy((char *)params->device_id, tmpstr,
2090 ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2092 strncpy((char *)be_lun->ctl_be_lun.device_id,
2094 ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2095 sizeof(params->device_id)));
2098 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2100 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2101 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2103 if (be_lun->io_taskqueue == NULL) {
2104 snprintf(req->error_str, sizeof(req->error_str),
2105 "unable to create taskqueue");
2110 * Note that we start the same number of threads by default for
2111 * both the file case and the block device case. For the file
2112 * case, we need multiple threads to allow concurrency, because the
2113 * vnode interface is designed to be a blocking interface. For the
2114 * block device case, ZFS zvols at least will block the caller's
2115 * context in many instances, and so we need multiple threads to
2116 * overcome that problem. Other block devices don't need as many
2117 * threads, but they shouldn't cause too many problems.
2119 * If the user wants to just have a single thread for a block
2120 * device, he can specify that when the LUN is created, or change
2121 * the tunable/sysctl to alter the default number of threads.
2123 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2124 /*num threads*/num_threads,
2127 "%s taskq", be_lun->lunname);
2132 be_lun->num_threads = num_threads;
2134 mtx_lock(&softc->lock);
2136 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2138 mtx_unlock(&softc->lock);
2140 retval = ctl_add_lun(&be_lun->ctl_be_lun);
2142 mtx_lock(&softc->lock);
2143 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2146 mtx_unlock(&softc->lock);
2147 snprintf(req->error_str, sizeof(req->error_str),
2148 "ctl_add_lun() returned error %d, see dmesg for "
2154 mtx_lock(&softc->lock);
2157 * Tell the config_status routine that we're waiting so it won't
2158 * clean up the LUN in the event of an error.
2160 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2162 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2163 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2164 if (retval == EINTR)
2167 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2169 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2170 snprintf(req->error_str, sizeof(req->error_str),
2171 "LUN configuration error, see dmesg for details");
2172 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2175 mtx_unlock(&softc->lock);
2178 params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2181 mtx_unlock(&softc->lock);
2183 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2185 DEVSTAT_ALL_SUPPORTED,
2186 be_lun->ctl_be_lun.lun_type
2187 | DEVSTAT_TYPE_IF_OTHER,
2188 DEVSTAT_PRIORITY_OTHER);
2193 req->status = CTL_LUN_ERROR;
2195 if (be_lun->io_taskqueue != NULL)
2196 taskqueue_free(be_lun->io_taskqueue);
2197 ctl_be_block_close(be_lun);
2198 if (be_lun->dev_path != NULL)
2199 free(be_lun->dev_path, M_CTLBLK);
2200 if (be_lun->lun_zone != NULL)
2201 uma_zdestroy(be_lun->lun_zone);
2202 ctl_free_opts(&be_lun->ctl_be_lun.options);
2203 mtx_destroy(&be_lun->queue_lock);
2204 mtx_destroy(&be_lun->io_lock);
2205 free(be_lun, M_CTLBLK);
2211 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2213 struct ctl_lun_rm_params *params;
2214 struct ctl_be_block_lun *be_lun;
2217 params = &req->reqdata.rm;
2219 mtx_lock(&softc->lock);
2223 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2224 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2227 mtx_unlock(&softc->lock);
2229 if (be_lun == NULL) {
2230 snprintf(req->error_str, sizeof(req->error_str),
2231 "LUN %u is not managed by the block backend",
2236 retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2239 snprintf(req->error_str, sizeof(req->error_str),
2240 "error %d returned from ctl_disable_lun() for "
2241 "LUN %d", retval, params->lun_id);
2246 retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2248 snprintf(req->error_str, sizeof(req->error_str),
2249 "error %d returned from ctl_invalidate_lun() for "
2250 "LUN %d", retval, params->lun_id);
2254 mtx_lock(&softc->lock);
2256 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2258 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2259 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2260 if (retval == EINTR)
2264 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2266 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2267 snprintf(req->error_str, sizeof(req->error_str),
2268 "interrupted waiting for LUN to be freed");
2269 mtx_unlock(&softc->lock);
2273 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2276 mtx_unlock(&softc->lock);
2278 taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2280 taskqueue_free(be_lun->io_taskqueue);
2282 ctl_be_block_close(be_lun);
2284 if (be_lun->disk_stats != NULL)
2285 devstat_remove_entry(be_lun->disk_stats);
2287 uma_zdestroy(be_lun->lun_zone);
2289 ctl_free_opts(&be_lun->ctl_be_lun.options);
2290 free(be_lun->dev_path, M_CTLBLK);
2291 mtx_destroy(&be_lun->queue_lock);
2292 mtx_destroy(&be_lun->io_lock);
2293 free(be_lun, M_CTLBLK);
2295 req->status = CTL_LUN_OK;
2301 req->status = CTL_LUN_ERROR;
2307 ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2308 struct ctl_lun_req *req)
2312 struct ctl_lun_create_params *params = &be_lun->params;
2314 if (params->lun_size_bytes != 0) {
2315 be_lun->size_bytes = params->lun_size_bytes;
2317 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2318 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2319 VOP_UNLOCK(be_lun->vn, 0);
2321 snprintf(req->error_str, sizeof(req->error_str),
2322 "error calling VOP_GETATTR() for file %s",
2327 be_lun->size_bytes = vattr.va_size;
2334 ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2335 struct ctl_lun_req *req)
2337 struct ctl_be_block_devdata *dev_data;
2339 struct ctl_lun_create_params *params = &be_lun->params;
2340 uint64_t size_bytes;
2342 dev_data = &be_lun->backend.dev;
2343 if (!dev_data->csw->d_ioctl) {
2344 snprintf(req->error_str, sizeof(req->error_str),
2345 "no d_ioctl for device %s!", be_lun->dev_path);
2349 error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2350 (caddr_t)&size_bytes, FREAD,
2353 snprintf(req->error_str, sizeof(req->error_str),
2354 "error %d returned for DIOCGMEDIASIZE ioctl "
2355 "on %s!", error, be_lun->dev_path);
2359 if (params->lun_size_bytes != 0) {
2360 if (params->lun_size_bytes > size_bytes) {
2361 snprintf(req->error_str, sizeof(req->error_str),
2362 "requested LUN size %ju > backing device "
2364 (uintmax_t)params->lun_size_bytes,
2365 (uintmax_t)size_bytes);
2369 be_lun->size_bytes = params->lun_size_bytes;
2371 be_lun->size_bytes = size_bytes;
2378 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2380 struct ctl_lun_modify_params *params;
2381 struct ctl_be_block_lun *be_lun;
2385 params = &req->reqdata.modify;
2387 mtx_lock(&softc->lock);
2389 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2390 if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2393 mtx_unlock(&softc->lock);
2395 if (be_lun == NULL) {
2396 snprintf(req->error_str, sizeof(req->error_str),
2397 "LUN %u is not managed by the block backend",
2402 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2404 oldsize = be_lun->size_bytes;
2405 if (be_lun->vn == NULL)
2406 error = ctl_be_block_open(softc, be_lun, req);
2407 else if (be_lun->vn->v_type == VREG)
2408 error = ctl_be_block_modify_file(be_lun, req);
2410 error = ctl_be_block_modify_dev(be_lun, req);
2412 if (error == 0 && be_lun->size_bytes != oldsize) {
2413 be_lun->size_blocks = be_lun->size_bytes >>
2414 be_lun->blocksize_shift;
2417 * The maximum LBA is the size - 1.
2419 * XXX: Note that this field is being updated without locking,
2420 * which might cause problems on 32-bit architectures.
2422 be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2423 0 : (be_lun->size_blocks - 1);
2424 be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2425 be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2426 be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2427 if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2428 be_lun->blocksize != 0)
2429 be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2431 ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2432 if (oldsize == 0 && be_lun->size_blocks != 0)
2433 ctl_lun_online(&be_lun->ctl_be_lun);
2436 /* Tell the user the exact size we ended up using */
2437 params->lun_size_bytes = be_lun->size_bytes;
2439 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2444 req->status = CTL_LUN_ERROR;
2450 ctl_be_block_lun_shutdown(void *be_lun)
2452 struct ctl_be_block_lun *lun;
2453 struct ctl_be_block_softc *softc;
2455 lun = (struct ctl_be_block_lun *)be_lun;
2459 mtx_lock(&softc->lock);
2460 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2461 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2463 mtx_unlock(&softc->lock);
2468 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2470 struct ctl_be_block_lun *lun;
2471 struct ctl_be_block_softc *softc;
2473 lun = (struct ctl_be_block_lun *)be_lun;
2476 if (status == CTL_LUN_CONFIG_OK) {
2477 mtx_lock(&softc->lock);
2478 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2479 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2481 mtx_unlock(&softc->lock);
2484 * We successfully added the LUN, attempt to enable it.
2486 if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2487 printf("%s: ctl_enable_lun() failed!\n", __func__);
2488 if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2489 printf("%s: ctl_invalidate_lun() failed!\n",
2498 mtx_lock(&softc->lock);
2499 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2500 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2502 mtx_unlock(&softc->lock);
2507 ctl_be_block_config_write(union ctl_io *io)
2509 struct ctl_be_block_lun *be_lun;
2510 struct ctl_be_lun *ctl_be_lun;
2515 DPRINTF("entered\n");
2517 ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2518 CTL_PRIV_BACKEND_LUN].ptr;
2519 be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2521 switch (io->scsiio.cdb[0]) {
2522 case SYNCHRONIZE_CACHE:
2523 case SYNCHRONIZE_CACHE_16:
2528 * The upper level CTL code will filter out any CDBs with
2529 * the immediate bit set and return the proper error.
2531 * We don't really need to worry about what LBA range the
2532 * user asked to be synced out. When they issue a sync
2533 * cache command, we'll sync out the whole thing.
2535 mtx_lock(&be_lun->queue_lock);
2536 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2538 mtx_unlock(&be_lun->queue_lock);
2539 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2541 case START_STOP_UNIT: {
2542 struct scsi_start_stop_unit *cdb;
2544 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2546 if (cdb->how & SSS_START)
2547 retval = ctl_start_lun(ctl_be_lun);
2549 retval = ctl_stop_lun(ctl_be_lun);
2551 * XXX KDM Copan-specific offline behavior.
2552 * Figure out a reasonable way to port this?
2556 && (cdb->byte2 & SSS_ONOFFLINE))
2557 retval = ctl_lun_offline(ctl_be_lun);
2562 * In general, the above routines should not fail. They
2563 * just set state for the LUN. So we've got something
2564 * pretty wrong here if we can't start or stop the LUN.
2567 ctl_set_internal_failure(&io->scsiio,
2569 /*retry_count*/ 0xf051);
2570 retval = CTL_RETVAL_COMPLETE;
2572 ctl_set_success(&io->scsiio);
2574 ctl_config_write_done(io);
2578 ctl_set_invalid_opcode(&io->scsiio);
2579 ctl_config_write_done(io);
2580 retval = CTL_RETVAL_COMPLETE;
2589 ctl_be_block_config_read(union ctl_io *io)
2595 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2597 struct ctl_be_block_lun *lun;
2600 lun = (struct ctl_be_block_lun *)be_lun;
2603 retval = sbuf_printf(sb, "\t<num_threads>");
2608 retval = sbuf_printf(sb, "%d", lun->num_threads);
2613 retval = sbuf_printf(sb, "</num_threads>\n");
2621 ctl_be_block_lun_attr(void *be_lun, const char *attrname)
2623 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2625 if (lun->getattr == NULL)
2626 return (UINT64_MAX);
2627 return (lun->getattr(lun, attrname));
2631 ctl_be_block_init(void)
2633 struct ctl_be_block_softc *softc;
2636 softc = &backend_block_softc;
2639 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2640 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2641 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2642 STAILQ_INIT(&softc->disk_list);
2643 STAILQ_INIT(&softc->lun_list);