2 * Copyright (c) 2003 Silicon Graphics International Corp.
3 * Copyright (c) 2009-2011 Spectra Logic Corporation
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
8 * Portions of this software were developed by Edward Tomasz Napierala
9 * under sponsorship from the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGES.
36 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
39 * CAM Target Layer driver backend for block devices.
41 * Author: Ken Merry <ken@FreeBSD.org>
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/types.h>
50 #include <sys/kthread.h>
52 #include <sys/fcntl.h>
53 #include <sys/limits.h>
55 #include <sys/mutex.h>
56 #include <sys/condvar.h>
57 #include <sys/malloc.h>
59 #include <sys/ioccom.h>
60 #include <sys/queue.h>
62 #include <sys/endian.h>
65 #include <sys/taskqueue.h>
66 #include <sys/vnode.h>
67 #include <sys/namei.h>
68 #include <sys/mount.h>
70 #include <sys/fcntl.h>
71 #include <sys/filedesc.h>
72 #include <sys/filio.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_ioctl.h>
89 #include <cam/ctl/ctl_ha.h>
90 #include <cam/ctl/ctl_scsi_all.h>
91 #include <cam/ctl/ctl_private.h>
92 #include <cam/ctl/ctl_error.h>
95 * The idea here is that we'll allocate enough S/G space to hold a 1MB
96 * I/O. If we get an I/O larger than that, we'll split it.
98 #define CTLBLK_HALF_IO_SIZE (512 * 1024)
99 #define CTLBLK_MAX_IO_SIZE (CTLBLK_HALF_IO_SIZE * 2)
100 #define CTLBLK_MAX_SEG MAXPHYS
101 #define CTLBLK_HALF_SEGS MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
102 #define CTLBLK_MAX_SEGS (CTLBLK_HALF_SEGS * 2)
105 #define DPRINTF(fmt, args...) \
106 printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
108 #define DPRINTF(fmt, args...) do {} while(0)
112 ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
114 ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
116 SDT_PROVIDER_DEFINE(cbb);
119 CTL_BE_BLOCK_LUN_UNCONFIGURED = 0x01,
120 CTL_BE_BLOCK_LUN_CONFIG_ERR = 0x02,
121 CTL_BE_BLOCK_LUN_WAITING = 0x04,
122 } ctl_be_block_lun_flags;
130 struct ctl_be_block_filedata {
134 union ctl_be_block_bedata {
135 struct ctl_be_block_filedata file;
138 struct ctl_be_block_io;
139 struct ctl_be_block_lun;
141 typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
142 struct ctl_be_block_io *beio);
143 typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
144 const char *attrname);
147 * Backend LUN structure. There is a 1:1 mapping between a block device
148 * and a backend block LUN, and between a backend block LUN and a CTL LUN.
150 struct ctl_be_block_lun {
151 struct ctl_lun_create_params params;
154 ctl_be_block_type dev_type;
156 union ctl_be_block_bedata backend;
157 cbb_dispatch_t dispatch;
158 cbb_dispatch_t lun_flush;
159 cbb_dispatch_t unmap;
160 cbb_dispatch_t get_lba_status;
161 cbb_getattr_t getattr;
163 uint64_t size_blocks;
165 struct ctl_be_block_softc *softc;
166 struct devstat *disk_stats;
167 ctl_be_block_lun_flags flags;
168 STAILQ_ENTRY(ctl_be_block_lun) links;
169 struct ctl_be_lun cbe_lun;
170 struct taskqueue *io_taskqueue;
173 STAILQ_HEAD(, ctl_io_hdr) input_queue;
174 STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
175 STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
176 STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
177 struct mtx_padalign io_lock;
178 struct mtx_padalign queue_lock;
182 * Overall softc structure for the block backend module.
184 struct ctl_be_block_softc {
187 STAILQ_HEAD(, ctl_be_block_lun) lun_list;
190 static struct ctl_be_block_softc backend_block_softc;
193 * Per-I/O information.
195 struct ctl_be_block_io {
197 struct ctl_sg_entry sg_segs[CTLBLK_MAX_SEGS];
198 struct iovec xiovecs[CTLBLK_MAX_SEGS];
205 struct bintime ds_t0;
206 devstat_tag_type ds_tag_type;
207 devstat_trans_flags ds_trans_type;
211 struct ctl_be_block_softc *softc;
212 struct ctl_be_block_lun *lun;
213 void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
216 extern struct ctl_softc *control_softc;
218 static int cbb_num_threads = 14;
219 SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
220 "CAM Target Layer Block Backend");
221 SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
222 &cbb_num_threads, 0, "Number of threads per backing file");
224 static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
225 static void ctl_free_beio(struct ctl_be_block_io *beio);
226 static void ctl_complete_beio(struct ctl_be_block_io *beio);
227 static int ctl_be_block_move_done(union ctl_io *io);
228 static void ctl_be_block_biodone(struct bio *bio);
229 static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
230 struct ctl_be_block_io *beio);
231 static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
232 struct ctl_be_block_io *beio);
233 static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
234 struct ctl_be_block_io *beio);
235 static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
236 const char *attrname);
237 static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
238 struct ctl_be_block_io *beio);
239 static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
240 struct ctl_be_block_io *beio);
241 static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
242 struct ctl_be_block_io *beio);
243 static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
244 const char *attrname);
245 static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
247 static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
249 static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
251 static void ctl_be_block_worker(void *context, int pending);
252 static int ctl_be_block_submit(union ctl_io *io);
253 static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
254 int flag, struct thread *td);
255 static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
256 struct ctl_lun_req *req);
257 static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
258 struct ctl_lun_req *req);
259 static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
260 static int ctl_be_block_open(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(struct ctl_be_block_softc *softc,
267 struct ctl_lun_req *req);
268 static void ctl_be_block_lun_shutdown(void *be_lun);
269 static void ctl_be_block_lun_config_status(void *be_lun,
270 ctl_lun_config_status status);
271 static int ctl_be_block_config_write(union ctl_io *io);
272 static int ctl_be_block_config_read(union ctl_io *io);
273 static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
274 static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
275 int ctl_be_block_init(void);
277 static struct ctl_backend_driver ctl_be_block_driver =
280 .flags = CTL_BE_FLAG_HAS_CONFIG,
281 .init = ctl_be_block_init,
282 .data_submit = ctl_be_block_submit,
283 .data_move_done = ctl_be_block_move_done,
284 .config_read = ctl_be_block_config_read,
285 .config_write = ctl_be_block_config_write,
286 .ioctl = ctl_be_block_ioctl,
287 .lun_info = ctl_be_block_lun_info,
288 .lun_attr = ctl_be_block_lun_attr
291 MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
292 CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
294 static uma_zone_t beio_zone;
296 static struct ctl_be_block_io *
297 ctl_alloc_beio(struct ctl_be_block_softc *softc)
299 struct ctl_be_block_io *beio;
301 beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
307 ctl_free_beio(struct ctl_be_block_io *beio)
314 for (i = 0; i < beio->num_segs; i++) {
315 if (beio->sg_segs[i].addr == NULL)
318 uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
319 beio->sg_segs[i].addr = NULL;
321 /* For compare we had two equal S/G lists. */
322 if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
323 uma_zfree(beio->lun->lun_zone,
324 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
325 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
329 if (duplicate_free > 0) {
330 printf("%s: %d duplicate frees out of %d segments\n", __func__,
331 duplicate_free, beio->num_segs);
334 uma_zfree(beio_zone, beio);
338 ctl_complete_beio(struct ctl_be_block_io *beio)
340 union ctl_io *io = beio->io;
342 if (beio->beio_cont != NULL) {
343 beio->beio_cont(beio);
346 ctl_data_submit_done(io);
351 cmp(uint8_t *a, uint8_t *b, size_t size)
355 for (i = 0; i < size; i++) {
363 ctl_be_block_compare(union ctl_io *io)
365 struct ctl_be_block_io *beio;
370 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
372 for (i = 0; i < beio->num_segs; i++) {
373 res = cmp(beio->sg_segs[i].addr,
374 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
375 beio->sg_segs[i].len);
377 if (res < beio->sg_segs[i].len)
380 if (i < beio->num_segs) {
381 scsi_u64to8b(off, info);
382 ctl_set_sense(&io->scsiio, /*current_error*/ 1,
383 /*sense_key*/ SSD_KEY_MISCOMPARE,
384 /*asc*/ 0x1D, /*ascq*/ 0x00,
385 /*type*/ SSD_ELEM_INFO,
386 /*size*/ sizeof(info), /*data*/ &info,
387 /*type*/ SSD_ELEM_NONE);
389 ctl_set_success(&io->scsiio);
393 ctl_be_block_move_done(union ctl_io *io)
395 struct ctl_be_block_io *beio;
396 struct ctl_be_block_lun *be_lun;
397 struct ctl_lba_len_flags *lbalen;
399 struct bintime cur_bt;
402 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
405 DPRINTF("entered\n");
408 getbinuptime(&cur_bt);
409 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
410 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
412 io->io_hdr.num_dmas++;
413 io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
416 * We set status at this point for read commands, and write
417 * commands with errors.
419 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
421 } else if ((io->io_hdr.port_status == 0) &&
422 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
423 lbalen = ARGS(beio->io);
424 if (lbalen->flags & CTL_LLF_READ) {
425 ctl_set_success(&io->scsiio);
426 } else if (lbalen->flags & CTL_LLF_COMPARE) {
427 /* We have two data blocks ready for comparison. */
428 ctl_be_block_compare(io);
430 } else if ((io->io_hdr.port_status != 0) &&
431 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
432 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
434 * For hardware error sense keys, the sense key
435 * specific value is defined to be a retry count,
436 * but we use it to pass back an internal FETD
437 * error code. XXX KDM Hopefully the FETD is only
438 * using 16 bits for an error code, since that's
439 * all the space we have in the sks field.
441 ctl_set_internal_failure(&io->scsiio,
444 io->io_hdr.port_status);
448 * If this is a read, or a write with errors, it is done.
450 if ((beio->bio_cmd == BIO_READ)
451 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
452 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
453 ctl_complete_beio(beio);
458 * At this point, we have a write and the DMA completed
459 * successfully. We now have to queue it to the task queue to
460 * execute the backend I/O. That is because we do blocking
461 * memory allocations, and in the file backing case, blocking I/O.
462 * This move done routine is generally called in the SIM's
463 * interrupt context, and therefore we cannot block.
465 mtx_lock(&be_lun->queue_lock);
466 STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
467 mtx_unlock(&be_lun->queue_lock);
468 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
474 ctl_be_block_biodone(struct bio *bio)
476 struct ctl_be_block_io *beio;
477 struct ctl_be_block_lun *be_lun;
481 beio = bio->bio_caller1;
485 DPRINTF("entered\n");
487 error = bio->bio_error;
488 mtx_lock(&be_lun->io_lock);
492 beio->num_bios_done++;
495 * XXX KDM will this cause WITNESS to complain? Holding a lock
496 * during the free might cause it to complain.
501 * If the send complete bit isn't set, or we aren't the last I/O to
502 * complete, then we're done.
504 if ((beio->send_complete == 0)
505 || (beio->num_bios_done < beio->num_bios_sent)) {
506 mtx_unlock(&be_lun->io_lock);
511 * At this point, we've verified that we are the last I/O to
512 * complete, so it's safe to drop the lock.
514 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
515 beio->ds_tag_type, beio->ds_trans_type,
516 /*now*/ NULL, /*then*/&beio->ds_t0);
517 mtx_unlock(&be_lun->io_lock);
520 * If there are any errors from the backing device, we fail the
521 * entire I/O with a medium error.
523 if (beio->num_errors > 0) {
524 if (error == EOPNOTSUPP) {
525 ctl_set_invalid_opcode(&io->scsiio);
526 } else if (error == ENOSPC || error == EDQUOT) {
527 ctl_set_space_alloc_fail(&io->scsiio);
528 } else if (error == EROFS || error == EACCES) {
529 ctl_set_hw_write_protected(&io->scsiio);
530 } else if (beio->bio_cmd == BIO_FLUSH) {
531 /* XXX KDM is there is a better error here? */
532 ctl_set_internal_failure(&io->scsiio,
534 /*retry_count*/ 0xbad2);
536 ctl_set_medium_error(&io->scsiio,
537 beio->bio_cmd == BIO_READ);
539 ctl_complete_beio(beio);
544 * If this is a write, a flush, a delete or verify, we're all done.
545 * If this is a read, we can now send the data to the user.
547 if ((beio->bio_cmd == BIO_WRITE)
548 || (beio->bio_cmd == BIO_FLUSH)
549 || (beio->bio_cmd == BIO_DELETE)
550 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
551 ctl_set_success(&io->scsiio);
552 ctl_complete_beio(beio);
554 if ((ARGS(io)->flags & CTL_LLF_READ) &&
555 beio->beio_cont == NULL) {
556 ctl_set_success(&io->scsiio);
560 getbinuptime(&io->io_hdr.dma_start_bt);
567 ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
568 struct ctl_be_block_io *beio)
570 union ctl_io *io = beio->io;
571 struct mount *mountpoint;
572 int error, lock_flags;
574 DPRINTF("entered\n");
576 binuptime(&beio->ds_t0);
577 mtx_lock(&be_lun->io_lock);
578 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
579 mtx_unlock(&be_lun->io_lock);
581 (void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
583 if (MNT_SHARED_WRITES(mountpoint) ||
584 ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
585 lock_flags = LK_SHARED;
587 lock_flags = LK_EXCLUSIVE;
588 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
589 error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
591 VOP_UNLOCK(be_lun->vn, 0);
593 vn_finished_write(mountpoint);
595 mtx_lock(&be_lun->io_lock);
596 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
597 beio->ds_tag_type, beio->ds_trans_type,
598 /*now*/ NULL, /*then*/&beio->ds_t0);
599 mtx_unlock(&be_lun->io_lock);
602 ctl_set_success(&io->scsiio);
604 /* XXX KDM is there is a better error here? */
605 ctl_set_internal_failure(&io->scsiio,
607 /*retry_count*/ 0xbad1);
610 ctl_complete_beio(beio);
613 SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
614 SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
615 SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
616 SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
619 ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
620 struct ctl_be_block_io *beio)
622 struct ctl_be_block_filedata *file_data;
625 struct iovec *xiovec;
629 DPRINTF("entered\n");
631 file_data = &be_lun->backend.file;
634 if (ARGS(io)->flags & CTL_LLF_DPO)
636 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
639 bzero(&xuio, sizeof(xuio));
640 if (beio->bio_cmd == BIO_READ) {
641 SDT_PROBE0(cbb, , read, file_start);
642 xuio.uio_rw = UIO_READ;
644 SDT_PROBE0(cbb, , write, file_start);
645 xuio.uio_rw = UIO_WRITE;
647 xuio.uio_offset = beio->io_offset;
648 xuio.uio_resid = beio->io_len;
649 xuio.uio_segflg = UIO_SYSSPACE;
650 xuio.uio_iov = beio->xiovecs;
651 xuio.uio_iovcnt = beio->num_segs;
652 xuio.uio_td = curthread;
654 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
655 xiovec->iov_base = beio->sg_segs[i].addr;
656 xiovec->iov_len = beio->sg_segs[i].len;
659 binuptime(&beio->ds_t0);
660 mtx_lock(&be_lun->io_lock);
661 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
662 mtx_unlock(&be_lun->io_lock);
664 if (beio->bio_cmd == BIO_READ) {
665 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
668 * UFS pays attention to IO_DIRECT for reads. If the
669 * DIRECTIO option is configured into the kernel, it calls
670 * ffs_rawread(). But that only works for single-segment
671 * uios with user space addresses. In our case, with a
672 * kernel uio, it still reads into the buffer cache, but it
673 * will just try to release the buffer from the cache later
676 * ZFS does not pay attention to IO_DIRECT for reads.
678 * UFS does not pay attention to IO_SYNC for reads.
680 * ZFS pays attention to IO_SYNC (which translates into the
681 * Solaris define FRSYNC for zfs_read()) for reads. It
682 * attempts to sync the file before reading.
684 error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
686 VOP_UNLOCK(be_lun->vn, 0);
687 SDT_PROBE0(cbb, , read, file_done);
688 if (error == 0 && xuio.uio_resid > 0) {
690 * If we red less then requested (EOF), then
691 * we should clean the rest of the buffer.
693 s = beio->io_len - xuio.uio_resid;
694 for (i = 0; i < beio->num_segs; i++) {
695 if (s >= beio->sg_segs[i].len) {
696 s -= beio->sg_segs[i].len;
699 bzero((uint8_t *)beio->sg_segs[i].addr + s,
700 beio->sg_segs[i].len - s);
705 struct mount *mountpoint;
708 (void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
710 if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL)
711 && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
712 lock_flags = LK_SHARED;
714 lock_flags = LK_EXCLUSIVE;
715 vn_lock(be_lun->vn, lock_flags | LK_RETRY);
718 * UFS pays attention to IO_DIRECT for writes. The write
719 * is done asynchronously. (Normally the write would just
720 * get put into cache.
722 * UFS pays attention to IO_SYNC for writes. It will
723 * attempt to write the buffer out synchronously if that
726 * ZFS does not pay attention to IO_DIRECT for writes.
728 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
729 * for writes. It will flush the transaction from the
730 * cache before returning.
732 error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
733 VOP_UNLOCK(be_lun->vn, 0);
735 vn_finished_write(mountpoint);
736 SDT_PROBE0(cbb, , write, file_done);
739 mtx_lock(&be_lun->io_lock);
740 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
741 beio->ds_tag_type, beio->ds_trans_type,
742 /*now*/ NULL, /*then*/&beio->ds_t0);
743 mtx_unlock(&be_lun->io_lock);
746 * If we got an error, set the sense data to "MEDIUM ERROR" and
747 * return the I/O to the user.
750 if (error == ENOSPC || error == EDQUOT) {
751 ctl_set_space_alloc_fail(&io->scsiio);
752 } else if (error == EROFS || error == EACCES) {
753 ctl_set_hw_write_protected(&io->scsiio);
755 ctl_set_medium_error(&io->scsiio,
756 beio->bio_cmd == BIO_READ);
758 ctl_complete_beio(beio);
763 * If this is a write or a verify, we're all done.
764 * If this is a read, we can now send the data to the user.
766 if ((beio->bio_cmd == BIO_WRITE) ||
767 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
768 ctl_set_success(&io->scsiio);
769 ctl_complete_beio(beio);
771 if ((ARGS(io)->flags & CTL_LLF_READ) &&
772 beio->beio_cont == NULL) {
773 ctl_set_success(&io->scsiio);
777 getbinuptime(&io->io_hdr.dma_start_bt);
784 ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
785 struct ctl_be_block_io *beio)
787 union ctl_io *io = beio->io;
788 struct ctl_lba_len_flags *lbalen = ARGS(io);
789 struct scsi_get_lba_status_data *data;
793 DPRINTF("entered\n");
795 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
796 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
797 error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
798 0, curthread->td_ucred, curthread);
799 if (error == 0 && off > roff)
800 status = 0; /* mapped up to off */
802 error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
803 0, curthread->td_ucred, curthread);
804 if (error == 0 && off > roff)
805 status = 1; /* deallocated up to off */
807 status = 0; /* unknown up to the end */
808 off = be_lun->size_bytes;
811 VOP_UNLOCK(be_lun->vn, 0);
813 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
814 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
815 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
816 lbalen->lba), data->descr[0].length);
817 data->descr[0].status = status;
819 ctl_complete_beio(beio);
823 ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
826 struct statfs statfs;
831 if (be_lun->vn == NULL)
833 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
834 if (strcmp(attrname, "blocksused") == 0) {
835 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
837 val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
839 if (strcmp(attrname, "blocksavail") == 0 &&
840 (be_lun->vn->v_iflag & VI_DOOMED) == 0) {
841 error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
843 val = statfs.f_bavail * statfs.f_bsize /
844 be_lun->cbe_lun.blocksize;
846 VOP_UNLOCK(be_lun->vn, 0);
851 ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
852 struct ctl_be_block_io *beio)
858 struct iovec *xiovec;
859 int error, flags, i, ref;
861 DPRINTF("entered\n");
865 if (ARGS(io)->flags & CTL_LLF_DPO)
867 if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
870 bzero(&xuio, sizeof(xuio));
871 if (beio->bio_cmd == BIO_READ) {
872 SDT_PROBE0(cbb, , read, file_start);
873 xuio.uio_rw = UIO_READ;
875 SDT_PROBE0(cbb, , write, file_start);
876 xuio.uio_rw = UIO_WRITE;
878 xuio.uio_offset = beio->io_offset;
879 xuio.uio_resid = beio->io_len;
880 xuio.uio_segflg = UIO_SYSSPACE;
881 xuio.uio_iov = beio->xiovecs;
882 xuio.uio_iovcnt = beio->num_segs;
883 xuio.uio_td = curthread;
885 for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
886 xiovec->iov_base = beio->sg_segs[i].addr;
887 xiovec->iov_len = beio->sg_segs[i].len;
890 binuptime(&beio->ds_t0);
891 mtx_lock(&be_lun->io_lock);
892 devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
893 mtx_unlock(&be_lun->io_lock);
895 csw = devvn_refthread(be_lun->vn, &dev, &ref);
897 if (beio->bio_cmd == BIO_READ)
898 error = csw->d_read(dev, &xuio, flags);
900 error = csw->d_write(dev, &xuio, flags);
901 dev_relthread(dev, ref);
905 if (beio->bio_cmd == BIO_READ)
906 SDT_PROBE0(cbb, , read, file_done);
908 SDT_PROBE0(cbb, , write, file_done);
910 mtx_lock(&be_lun->io_lock);
911 devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
912 beio->ds_tag_type, beio->ds_trans_type,
913 /*now*/ NULL, /*then*/&beio->ds_t0);
914 mtx_unlock(&be_lun->io_lock);
917 * If we got an error, set the sense data to "MEDIUM ERROR" and
918 * return the I/O to the user.
921 if (error == ENOSPC || error == EDQUOT) {
922 ctl_set_space_alloc_fail(&io->scsiio);
923 } else if (error == EROFS || error == EACCES) {
924 ctl_set_hw_write_protected(&io->scsiio);
926 ctl_set_medium_error(&io->scsiio,
927 beio->bio_cmd == BIO_READ);
929 ctl_complete_beio(beio);
934 * If this is a write or a verify, we're all done.
935 * If this is a read, we can now send the data to the user.
937 if ((beio->bio_cmd == BIO_WRITE) ||
938 (ARGS(io)->flags & CTL_LLF_VERIFY)) {
939 ctl_set_success(&io->scsiio);
940 ctl_complete_beio(beio);
942 if ((ARGS(io)->flags & CTL_LLF_READ) &&
943 beio->beio_cont == NULL) {
944 ctl_set_success(&io->scsiio);
948 getbinuptime(&io->io_hdr.dma_start_bt);
955 ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
956 struct ctl_be_block_io *beio)
958 union ctl_io *io = beio->io;
961 struct ctl_lba_len_flags *lbalen = ARGS(io);
962 struct scsi_get_lba_status_data *data;
964 int error, ref, status;
966 DPRINTF("entered\n");
968 csw = devvn_refthread(be_lun->vn, &dev, &ref);
970 status = 0; /* unknown up to the end */
971 off = be_lun->size_bytes;
974 off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
975 error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
977 if (error == 0 && off > roff)
978 status = 0; /* mapped up to off */
980 error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
982 if (error == 0 && off > roff)
983 status = 1; /* deallocated up to off */
985 status = 0; /* unknown up to the end */
986 off = be_lun->size_bytes;
989 dev_relthread(dev, ref);
992 data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
993 scsi_u64to8b(lbalen->lba, data->descr[0].addr);
994 scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
995 lbalen->lba), data->descr[0].length);
996 data->descr[0].status = status;
998 ctl_complete_beio(beio);
1002 ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1003 struct ctl_be_block_io *beio)
1010 DPRINTF("entered\n");
1012 /* This can't fail, it's a blocking allocation. */
1013 bio = g_alloc_bio();
1015 bio->bio_cmd = BIO_FLUSH;
1016 bio->bio_offset = 0;
1018 bio->bio_done = ctl_be_block_biodone;
1019 bio->bio_caller1 = beio;
1020 bio->bio_pblkno = 0;
1023 * We don't need to acquire the LUN lock here, because we are only
1024 * sending one bio, and so there is no other context to synchronize
1027 beio->num_bios_sent = 1;
1028 beio->send_complete = 1;
1030 binuptime(&beio->ds_t0);
1031 mtx_lock(&be_lun->io_lock);
1032 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1033 mtx_unlock(&be_lun->io_lock);
1035 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1038 csw->d_strategy(bio);
1039 dev_relthread(dev, ref);
1041 bio->bio_error = ENXIO;
1042 ctl_be_block_biodone(bio);
1047 ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1048 struct ctl_be_block_io *beio,
1049 uint64_t off, uint64_t len, int last)
1057 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1058 maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1060 bio = g_alloc_bio();
1061 bio->bio_cmd = BIO_DELETE;
1063 bio->bio_offset = off;
1064 bio->bio_length = MIN(len, maxlen);
1066 bio->bio_done = ctl_be_block_biodone;
1067 bio->bio_caller1 = beio;
1068 bio->bio_pblkno = off / be_lun->cbe_lun.blocksize;
1070 off += bio->bio_length;
1071 len -= bio->bio_length;
1073 mtx_lock(&be_lun->io_lock);
1074 beio->num_bios_sent++;
1075 if (last && len == 0)
1076 beio->send_complete = 1;
1077 mtx_unlock(&be_lun->io_lock);
1080 csw->d_strategy(bio);
1082 bio->bio_error = ENXIO;
1083 ctl_be_block_biodone(bio);
1087 dev_relthread(dev, ref);
1091 ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1092 struct ctl_be_block_io *beio)
1095 struct ctl_ptr_len_flags *ptrlen;
1096 struct scsi_unmap_desc *buf, *end;
1101 DPRINTF("entered\n");
1103 binuptime(&beio->ds_t0);
1104 mtx_lock(&be_lun->io_lock);
1105 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1106 mtx_unlock(&be_lun->io_lock);
1108 if (beio->io_offset == -1) {
1110 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1111 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1112 end = buf + ptrlen->len / sizeof(*buf);
1113 for (; buf < end; buf++) {
1114 len = (uint64_t)scsi_4btoul(buf->length) *
1115 be_lun->cbe_lun.blocksize;
1116 beio->io_len += len;
1117 ctl_be_block_unmap_dev_range(be_lun, beio,
1118 scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
1119 len, (end - buf < 2) ? TRUE : FALSE);
1122 ctl_be_block_unmap_dev_range(be_lun, beio,
1123 beio->io_offset, beio->io_len, TRUE);
1127 ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1128 struct ctl_be_block_io *beio)
1130 TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1135 int i, max_iosize, ref;
1137 DPRINTF("entered\n");
1138 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1141 * We have to limit our I/O size to the maximum supported by the
1142 * backend device. Hopefully it is MAXPHYS. If the driver doesn't
1143 * set it properly, use DFLTPHYS.
1146 max_iosize = dev->si_iosize_max;
1147 if (max_iosize < PAGE_SIZE)
1148 max_iosize = DFLTPHYS;
1150 max_iosize = DFLTPHYS;
1152 cur_offset = beio->io_offset;
1153 for (i = 0; i < beio->num_segs; i++) {
1157 cur_size = beio->sg_segs[i].len;
1158 cur_ptr = beio->sg_segs[i].addr;
1160 while (cur_size > 0) {
1161 /* This can't fail, it's a blocking allocation. */
1162 bio = g_alloc_bio();
1164 KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1166 bio->bio_cmd = beio->bio_cmd;
1168 bio->bio_caller1 = beio;
1169 bio->bio_length = min(cur_size, max_iosize);
1170 bio->bio_offset = cur_offset;
1171 bio->bio_data = cur_ptr;
1172 bio->bio_done = ctl_be_block_biodone;
1173 bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1175 cur_offset += bio->bio_length;
1176 cur_ptr += bio->bio_length;
1177 cur_size -= bio->bio_length;
1179 TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1180 beio->num_bios_sent++;
1183 binuptime(&beio->ds_t0);
1184 mtx_lock(&be_lun->io_lock);
1185 devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1186 beio->send_complete = 1;
1187 mtx_unlock(&be_lun->io_lock);
1190 * Fire off all allocated requests!
1192 while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1193 TAILQ_REMOVE(&queue, bio, bio_queue);
1195 csw->d_strategy(bio);
1197 bio->bio_error = ENXIO;
1198 ctl_be_block_biodone(bio);
1202 dev_relthread(dev, ref);
1206 ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1208 struct diocgattr_arg arg;
1213 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1215 return (UINT64_MAX);
1216 strlcpy(arg.name, attrname, sizeof(arg.name));
1217 arg.len = sizeof(arg.value.off);
1219 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
1223 dev_relthread(dev, ref);
1225 return (UINT64_MAX);
1226 return (arg.value.off);
1230 ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1233 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1234 struct ctl_be_block_io *beio;
1235 struct ctl_lba_len_flags *lbalen;
1237 DPRINTF("entered\n");
1238 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1239 lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1241 beio->io_len = lbalen->len * cbe_lun->blocksize;
1242 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1243 beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
1244 beio->bio_cmd = BIO_FLUSH;
1245 beio->ds_trans_type = DEVSTAT_NO_DATA;
1247 be_lun->lun_flush(be_lun, beio);
1251 ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1256 ctl_free_beio(beio);
1257 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1258 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1259 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1260 ctl_config_write_done(io);
1264 ctl_be_block_config_write(io);
1268 ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1271 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1272 struct ctl_be_block_io *beio;
1273 struct ctl_lba_len_flags *lbalen;
1274 uint64_t len_left, lba;
1275 uint32_t pb, pbo, adj;
1279 DPRINTF("entered\n");
1281 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1282 lbalen = ARGS(beio->io);
1284 if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1285 (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1286 ctl_free_beio(beio);
1287 ctl_set_invalid_field(&io->scsiio,
1293 ctl_config_write_done(io);
1297 if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1298 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1299 beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1300 beio->bio_cmd = BIO_DELETE;
1301 beio->ds_trans_type = DEVSTAT_FREE;
1303 be_lun->unmap(be_lun, beio);
1307 beio->bio_cmd = BIO_WRITE;
1308 beio->ds_trans_type = DEVSTAT_WRITE;
1310 DPRINTF("WRITE SAME at LBA %jx len %u\n",
1311 (uintmax_t)lbalen->lba, lbalen->len);
1313 pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
1314 if (be_lun->cbe_lun.pblockoff > 0)
1315 pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1318 len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1319 for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1322 * Setup the S/G entry for this chunk.
1324 seglen = MIN(CTLBLK_MAX_SEG, len_left);
1325 if (pb > cbe_lun->blocksize) {
1326 adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
1331 seglen -= seglen % cbe_lun->blocksize;
1333 seglen -= seglen % cbe_lun->blocksize;
1334 beio->sg_segs[i].len = seglen;
1335 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1337 DPRINTF("segment %d addr %p len %zd\n", i,
1338 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1343 buf = beio->sg_segs[i].addr;
1345 for (; buf < end; buf += cbe_lun->blocksize) {
1346 if (lbalen->flags & SWS_NDOB) {
1347 memset(buf, 0, cbe_lun->blocksize);
1349 memcpy(buf, io->scsiio.kern_data_ptr,
1350 cbe_lun->blocksize);
1352 if (lbalen->flags & SWS_LBDATA)
1353 scsi_ulto4b(lbalen->lba + lba, buf);
1358 beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1359 beio->io_len = lba * cbe_lun->blocksize;
1361 /* We can not do all in one run. Correct and schedule rerun. */
1365 beio->beio_cont = ctl_be_block_cw_done_ws;
1368 be_lun->dispatch(be_lun, beio);
1372 ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1375 struct ctl_be_block_io *beio;
1376 struct ctl_ptr_len_flags *ptrlen;
1378 DPRINTF("entered\n");
1380 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1381 ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1383 if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1384 ctl_free_beio(beio);
1385 ctl_set_invalid_field(&io->scsiio,
1391 ctl_config_write_done(io);
1396 beio->io_offset = -1;
1397 beio->bio_cmd = BIO_DELETE;
1398 beio->ds_trans_type = DEVSTAT_FREE;
1400 be_lun->unmap(be_lun, beio);
1404 ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1409 ctl_free_beio(beio);
1410 ctl_config_read_done(io);
1414 ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1417 struct ctl_be_block_io *beio;
1418 struct ctl_be_block_softc *softc;
1420 DPRINTF("entered\n");
1422 softc = be_lun->softc;
1423 beio = ctl_alloc_beio(softc);
1426 beio->beio_cont = ctl_be_block_cr_done;
1427 PRIV(io)->ptr = (void *)beio;
1429 switch (io->scsiio.cdb[0]) {
1430 case SERVICE_ACTION_IN: /* GET LBA STATUS */
1432 beio->ds_trans_type = DEVSTAT_NO_DATA;
1433 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1435 if (be_lun->get_lba_status)
1436 be_lun->get_lba_status(be_lun, beio);
1438 ctl_be_block_cr_done(beio);
1441 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1447 ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1452 ctl_free_beio(beio);
1453 ctl_config_write_done(io);
1457 ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1460 struct ctl_be_block_io *beio;
1461 struct ctl_be_block_softc *softc;
1463 DPRINTF("entered\n");
1465 softc = be_lun->softc;
1466 beio = ctl_alloc_beio(softc);
1469 beio->beio_cont = ctl_be_block_cw_done;
1470 switch (io->scsiio.tag_type) {
1471 case CTL_TAG_ORDERED:
1472 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1474 case CTL_TAG_HEAD_OF_QUEUE:
1475 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1477 case CTL_TAG_UNTAGGED:
1478 case CTL_TAG_SIMPLE:
1481 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1484 PRIV(io)->ptr = (void *)beio;
1486 switch (io->scsiio.cdb[0]) {
1487 case SYNCHRONIZE_CACHE:
1488 case SYNCHRONIZE_CACHE_16:
1489 ctl_be_block_cw_dispatch_sync(be_lun, io);
1493 ctl_be_block_cw_dispatch_ws(be_lun, io);
1496 ctl_be_block_cw_dispatch_unmap(be_lun, io);
1499 panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1504 SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
1505 SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
1506 SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
1507 SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1510 ctl_be_block_next(struct ctl_be_block_io *beio)
1512 struct ctl_be_block_lun *be_lun;
1517 ctl_free_beio(beio);
1518 if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1519 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1520 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1521 ctl_data_submit_done(io);
1525 io->io_hdr.status &= ~CTL_STATUS_MASK;
1526 io->io_hdr.status |= CTL_STATUS_NONE;
1528 mtx_lock(&be_lun->queue_lock);
1529 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1530 mtx_unlock(&be_lun->queue_lock);
1531 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1535 ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1538 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1539 struct ctl_be_block_io *beio;
1540 struct ctl_be_block_softc *softc;
1541 struct ctl_lba_len_flags *lbalen;
1542 struct ctl_ptr_len_flags *bptrlen;
1543 uint64_t len_left, lbas;
1546 softc = be_lun->softc;
1548 DPRINTF("entered\n");
1551 if (lbalen->flags & CTL_LLF_WRITE) {
1552 SDT_PROBE0(cbb, , write, start);
1554 SDT_PROBE0(cbb, , read, start);
1557 beio = ctl_alloc_beio(softc);
1561 bptrlen->ptr = (void *)beio;
1563 switch (io->scsiio.tag_type) {
1564 case CTL_TAG_ORDERED:
1565 beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1567 case CTL_TAG_HEAD_OF_QUEUE:
1568 beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1570 case CTL_TAG_UNTAGGED:
1571 case CTL_TAG_SIMPLE:
1574 beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1578 if (lbalen->flags & CTL_LLF_WRITE) {
1579 beio->bio_cmd = BIO_WRITE;
1580 beio->ds_trans_type = DEVSTAT_WRITE;
1582 beio->bio_cmd = BIO_READ;
1583 beio->ds_trans_type = DEVSTAT_READ;
1586 DPRINTF("%s at LBA %jx len %u @%ju\n",
1587 (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1588 (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1589 if (lbalen->flags & CTL_LLF_COMPARE)
1590 lbas = CTLBLK_HALF_IO_SIZE;
1592 lbas = CTLBLK_MAX_IO_SIZE;
1593 lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
1594 beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
1595 beio->io_len = lbas * cbe_lun->blocksize;
1596 bptrlen->len += lbas;
1598 for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1599 KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1600 i, CTLBLK_MAX_SEGS));
1603 * Setup the S/G entry for this chunk.
1605 beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1606 beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1608 DPRINTF("segment %d addr %p len %zd\n", i,
1609 beio->sg_segs[i].addr, beio->sg_segs[i].len);
1611 /* Set up second segment for compare operation. */
1612 if (lbalen->flags & CTL_LLF_COMPARE) {
1613 beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1614 beio->sg_segs[i].len;
1615 beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1616 uma_zalloc(be_lun->lun_zone, M_WAITOK);
1620 len_left -= beio->sg_segs[i].len;
1622 if (bptrlen->len < lbalen->len)
1623 beio->beio_cont = ctl_be_block_next;
1624 io->scsiio.be_move_done = ctl_be_block_move_done;
1625 /* For compare we have separate S/G lists for read and datamove. */
1626 if (lbalen->flags & CTL_LLF_COMPARE)
1627 io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1629 io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1630 io->scsiio.kern_data_len = beio->io_len;
1631 io->scsiio.kern_data_resid = 0;
1632 io->scsiio.kern_sg_entries = beio->num_segs;
1633 io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1636 * For the read case, we need to read the data into our buffers and
1637 * then we can send it back to the user. For the write case, we
1638 * need to get the data from the user first.
1640 if (beio->bio_cmd == BIO_READ) {
1641 SDT_PROBE0(cbb, , read, alloc_done);
1642 be_lun->dispatch(be_lun, beio);
1644 SDT_PROBE0(cbb, , write, alloc_done);
1646 getbinuptime(&io->io_hdr.dma_start_bt);
1653 ctl_be_block_worker(void *context, int pending)
1655 struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1656 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1658 struct ctl_be_block_io *beio;
1660 DPRINTF("entered\n");
1662 * Fetch and process I/Os from all queues. If we detect LUN
1663 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1664 * so make response maximally opaque to not confuse initiator.
1667 mtx_lock(&be_lun->queue_lock);
1668 io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1670 DPRINTF("datamove queue\n");
1671 STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1673 mtx_unlock(&be_lun->queue_lock);
1674 beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1675 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1676 ctl_set_busy(&io->scsiio);
1677 ctl_complete_beio(beio);
1680 be_lun->dispatch(be_lun, beio);
1683 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1685 DPRINTF("config write queue\n");
1686 STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1688 mtx_unlock(&be_lun->queue_lock);
1689 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1690 ctl_set_busy(&io->scsiio);
1691 ctl_config_write_done(io);
1694 ctl_be_block_cw_dispatch(be_lun, io);
1697 io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1699 DPRINTF("config read queue\n");
1700 STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1702 mtx_unlock(&be_lun->queue_lock);
1703 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1704 ctl_set_busy(&io->scsiio);
1705 ctl_config_read_done(io);
1708 ctl_be_block_cr_dispatch(be_lun, io);
1711 io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1713 DPRINTF("input queue\n");
1714 STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1716 mtx_unlock(&be_lun->queue_lock);
1717 if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1718 ctl_set_busy(&io->scsiio);
1719 ctl_data_submit_done(io);
1722 ctl_be_block_dispatch(be_lun, io);
1727 * If we get here, there is no work left in the queues, so
1728 * just break out and let the task queue go to sleep.
1730 mtx_unlock(&be_lun->queue_lock);
1736 * Entry point from CTL to the backend for I/O. We queue everything to a
1737 * work thread, so this just puts the I/O on a queue and wakes up the
1741 ctl_be_block_submit(union ctl_io *io)
1743 struct ctl_be_block_lun *be_lun;
1744 struct ctl_be_lun *cbe_lun;
1746 DPRINTF("entered\n");
1748 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1749 CTL_PRIV_BACKEND_LUN].ptr;
1750 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
1753 * Make sure we only get SCSI I/O.
1755 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1756 "%#x) encountered", io->io_hdr.io_type));
1760 mtx_lock(&be_lun->queue_lock);
1761 STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1762 mtx_unlock(&be_lun->queue_lock);
1763 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1765 return (CTL_RETVAL_COMPLETE);
1769 ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1770 int flag, struct thread *td)
1772 struct ctl_be_block_softc *softc;
1775 softc = &backend_block_softc;
1781 struct ctl_lun_req *lun_req;
1783 lun_req = (struct ctl_lun_req *)addr;
1785 switch (lun_req->reqtype) {
1786 case CTL_LUNREQ_CREATE:
1787 error = ctl_be_block_create(softc, lun_req);
1790 error = ctl_be_block_rm(softc, lun_req);
1792 case CTL_LUNREQ_MODIFY:
1793 error = ctl_be_block_modify(softc, lun_req);
1796 lun_req->status = CTL_LUN_ERROR;
1797 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1798 "invalid LUN request type %d",
1813 ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1815 struct ctl_be_lun *cbe_lun;
1816 struct ctl_be_block_filedata *file_data;
1817 struct ctl_lun_create_params *params;
1820 off_t ps, pss, po, pos, us, uss, uo, uos;
1823 cbe_lun = &be_lun->cbe_lun;
1824 file_data = &be_lun->backend.file;
1825 params = &be_lun->params;
1827 be_lun->dev_type = CTL_BE_BLOCK_FILE;
1828 be_lun->dispatch = ctl_be_block_dispatch_file;
1829 be_lun->lun_flush = ctl_be_block_flush_file;
1830 be_lun->get_lba_status = ctl_be_block_gls_file;
1831 be_lun->getattr = ctl_be_block_getattr_file;
1832 be_lun->unmap = NULL;
1833 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1835 error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1837 snprintf(req->error_str, sizeof(req->error_str),
1838 "error calling VOP_GETATTR() for file %s",
1843 file_data->cred = crhold(curthread->td_ucred);
1844 if (params->lun_size_bytes != 0)
1845 be_lun->size_bytes = params->lun_size_bytes;
1847 be_lun->size_bytes = vattr.va_size;
1850 * For files we can use any logical block size. Prefer 512 bytes
1851 * for compatibility reasons. If file's vattr.va_blocksize
1852 * (preferred I/O block size) is bigger and multiple to chosen
1853 * logical block size -- report it as physical block size.
1855 if (params->blocksize_bytes != 0)
1856 cbe_lun->blocksize = params->blocksize_bytes;
1857 else if (cbe_lun->lun_type == T_CDROM)
1858 cbe_lun->blocksize = 2048;
1860 cbe_lun->blocksize = 512;
1861 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1862 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1863 0 : (be_lun->size_blocks - 1);
1865 us = ps = vattr.va_blocksize;
1868 value = ctl_get_opt(&cbe_lun->options, "pblocksize");
1870 ctl_expand_number(value, &ps);
1871 value = ctl_get_opt(&cbe_lun->options, "pblockoffset");
1873 ctl_expand_number(value, &po);
1874 pss = ps / cbe_lun->blocksize;
1875 pos = po / cbe_lun->blocksize;
1876 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
1877 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
1878 cbe_lun->pblockexp = fls(pss) - 1;
1879 cbe_lun->pblockoff = (pss - pos) % pss;
1882 value = ctl_get_opt(&cbe_lun->options, "ublocksize");
1884 ctl_expand_number(value, &us);
1885 value = ctl_get_opt(&cbe_lun->options, "ublockoffset");
1887 ctl_expand_number(value, &uo);
1888 uss = us / cbe_lun->blocksize;
1889 uos = uo / cbe_lun->blocksize;
1890 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
1891 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
1892 cbe_lun->ublockexp = fls(uss) - 1;
1893 cbe_lun->ublockoff = (uss - uos) % uss;
1897 * Sanity check. The media size has to be at least one
1900 if (be_lun->size_bytes < cbe_lun->blocksize) {
1902 snprintf(req->error_str, sizeof(req->error_str),
1903 "file %s size %ju < block size %u", be_lun->dev_path,
1904 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1907 cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1912 ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1914 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1915 struct ctl_lun_create_params *params;
1919 int error, atomic, maxio, ref, unmap, tmp;
1920 off_t ps, pss, po, pos, us, uss, uo, uos, otmp;
1922 params = &be_lun->params;
1924 be_lun->dev_type = CTL_BE_BLOCK_DEV;
1925 csw = devvn_refthread(be_lun->vn, &dev, &ref);
1928 if (strcmp(csw->d_name, "zvol") == 0) {
1929 be_lun->dispatch = ctl_be_block_dispatch_zvol;
1930 be_lun->get_lba_status = ctl_be_block_gls_zvol;
1931 atomic = maxio = CTLBLK_MAX_IO_SIZE;
1933 be_lun->dispatch = ctl_be_block_dispatch_dev;
1934 be_lun->get_lba_status = NULL;
1936 maxio = dev->si_iosize_max;
1939 if (maxio > CTLBLK_MAX_IO_SIZE)
1940 maxio = CTLBLK_MAX_IO_SIZE;
1942 be_lun->lun_flush = ctl_be_block_flush_dev;
1943 be_lun->getattr = ctl_be_block_getattr_dev;
1944 be_lun->unmap = ctl_be_block_unmap_dev;
1946 if (!csw->d_ioctl) {
1947 dev_relthread(dev, ref);
1948 snprintf(req->error_str, sizeof(req->error_str),
1949 "no d_ioctl for device %s!", be_lun->dev_path);
1953 error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1956 dev_relthread(dev, ref);
1957 snprintf(req->error_str, sizeof(req->error_str),
1958 "error %d returned for DIOCGSECTORSIZE ioctl "
1959 "on %s!", error, be_lun->dev_path);
1964 * If the user has asked for a blocksize that is greater than the
1965 * backing device's blocksize, we can do it only if the blocksize
1966 * the user is asking for is an even multiple of the underlying
1967 * device's blocksize.
1969 if ((params->blocksize_bytes != 0) &&
1970 (params->blocksize_bytes >= tmp)) {
1971 if (params->blocksize_bytes % tmp == 0) {
1972 cbe_lun->blocksize = params->blocksize_bytes;
1974 dev_relthread(dev, ref);
1975 snprintf(req->error_str, sizeof(req->error_str),
1976 "requested blocksize %u is not an even "
1977 "multiple of backing device blocksize %u",
1978 params->blocksize_bytes, tmp);
1981 } else if (params->blocksize_bytes != 0) {
1982 dev_relthread(dev, ref);
1983 snprintf(req->error_str, sizeof(req->error_str),
1984 "requested blocksize %u < backing device "
1985 "blocksize %u", params->blocksize_bytes, tmp);
1987 } else if (cbe_lun->lun_type == T_CDROM)
1988 cbe_lun->blocksize = MAX(tmp, 2048);
1990 cbe_lun->blocksize = tmp;
1992 error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
1995 dev_relthread(dev, ref);
1996 snprintf(req->error_str, sizeof(req->error_str),
1997 "error %d returned for DIOCGMEDIASIZE "
1998 " ioctl on %s!", error,
2003 if (params->lun_size_bytes != 0) {
2004 if (params->lun_size_bytes > otmp) {
2005 dev_relthread(dev, ref);
2006 snprintf(req->error_str, sizeof(req->error_str),
2007 "requested LUN size %ju > backing device "
2009 (uintmax_t)params->lun_size_bytes,
2014 be_lun->size_bytes = params->lun_size_bytes;
2016 be_lun->size_bytes = otmp;
2017 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2018 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2019 0 : (be_lun->size_blocks - 1);
2021 error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
2026 error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
2034 value = ctl_get_opt(&cbe_lun->options, "pblocksize");
2036 ctl_expand_number(value, &ps);
2037 value = ctl_get_opt(&cbe_lun->options, "pblockoffset");
2039 ctl_expand_number(value, &po);
2040 pss = ps / cbe_lun->blocksize;
2041 pos = po / cbe_lun->blocksize;
2042 if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
2043 ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
2044 cbe_lun->pblockexp = fls(pss) - 1;
2045 cbe_lun->pblockoff = (pss - pos) % pss;
2048 value = ctl_get_opt(&cbe_lun->options, "ublocksize");
2050 ctl_expand_number(value, &us);
2051 value = ctl_get_opt(&cbe_lun->options, "ublockoffset");
2053 ctl_expand_number(value, &uo);
2054 uss = us / cbe_lun->blocksize;
2055 uos = uo / cbe_lun->blocksize;
2056 if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
2057 ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
2058 cbe_lun->ublockexp = fls(uss) - 1;
2059 cbe_lun->ublockoff = (uss - uos) % uss;
2062 cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
2063 cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2065 if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2068 struct diocgattr_arg arg;
2070 strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2071 arg.len = sizeof(arg.value.i);
2072 error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
2074 unmap = (error == 0) ? arg.value.i : 0;
2076 value = ctl_get_opt(&cbe_lun->options, "unmap");
2078 unmap = (strcmp(value, "on") == 0);
2080 cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
2082 cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2084 dev_relthread(dev, ref);
2089 ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2091 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2096 if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
2098 (void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2101 switch (be_lun->dev_type) {
2102 case CTL_BE_BLOCK_DEV:
2104 case CTL_BE_BLOCK_FILE:
2105 if (be_lun->backend.file.cred != NULL) {
2106 crfree(be_lun->backend.file.cred);
2107 be_lun->backend.file.cred = NULL;
2110 case CTL_BE_BLOCK_NONE:
2113 panic("Unexpected backend type %d", be_lun->dev_type);
2116 be_lun->dev_type = CTL_BE_BLOCK_NONE;
2122 ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2124 struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2125 struct nameidata nd;
2130 if (rootvnode == NULL) {
2131 snprintf(req->error_str, sizeof(req->error_str),
2132 "Root filesystem is not mounted");
2137 value = ctl_get_opt(&cbe_lun->options, "file");
2138 if (value == NULL) {
2139 snprintf(req->error_str, sizeof(req->error_str),
2140 "no file argument specified");
2143 free(be_lun->dev_path, M_CTLBLK);
2144 be_lun->dev_path = strdup(value, M_CTLBLK);
2147 value = ctl_get_opt(&cbe_lun->options, "readonly");
2148 if (value != NULL) {
2149 if (strcmp(value, "on") != 0)
2151 } else if (cbe_lun->lun_type == T_DIRECT)
2155 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2156 error = vn_open(&nd, &flags, 0, NULL);
2157 if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
2163 * This is the only reasonable guess we can make as far as
2164 * path if the user doesn't give us a fully qualified path.
2165 * If they want to specify a file, they need to specify the
2168 if (be_lun->dev_path[0] != '/') {
2171 asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2173 free(be_lun->dev_path, M_CTLBLK);
2174 be_lun->dev_path = dev_name;
2177 snprintf(req->error_str, sizeof(req->error_str),
2178 "error opening %s: %d", be_lun->dev_path, error);
2182 cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
2184 cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2186 NDFREE(&nd, NDF_ONLY_PNBUF);
2187 be_lun->vn = nd.ni_vp;
2189 /* We only support disks and files. */
2190 if (vn_isdisk(be_lun->vn, &error)) {
2191 error = ctl_be_block_open_dev(be_lun, req);
2192 } else if (be_lun->vn->v_type == VREG) {
2193 error = ctl_be_block_open_file(be_lun, req);
2196 snprintf(req->error_str, sizeof(req->error_str),
2197 "%s is not a disk or plain file", be_lun->dev_path);
2199 VOP_UNLOCK(be_lun->vn, 0);
2202 ctl_be_block_close(be_lun);
2203 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2204 if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2205 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2206 value = ctl_get_opt(&cbe_lun->options, "serseq");
2207 if (value != NULL && strcmp(value, "on") == 0)
2208 cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
2209 else if (value != NULL && strcmp(value, "read") == 0)
2210 cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2211 else if (value != NULL && strcmp(value, "off") == 0)
2212 cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2217 ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2219 struct ctl_be_lun *cbe_lun;
2220 struct ctl_be_block_lun *be_lun;
2221 struct ctl_lun_create_params *params;
2222 char num_thread_str[16];
2225 int retval, num_threads;
2226 int tmp_num_threads;
2228 params = &req->reqdata.create;
2230 req->status = CTL_LUN_OK;
2232 be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2233 cbe_lun = &be_lun->cbe_lun;
2234 cbe_lun->be_lun = be_lun;
2235 be_lun->params = req->reqdata.create;
2236 be_lun->softc = softc;
2237 STAILQ_INIT(&be_lun->input_queue);
2238 STAILQ_INIT(&be_lun->config_read_queue);
2239 STAILQ_INIT(&be_lun->config_write_queue);
2240 STAILQ_INIT(&be_lun->datamove_queue);
2241 sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
2242 mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
2243 mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
2244 ctl_init_opts(&cbe_lun->options,
2245 req->num_be_args, req->kern_be_args);
2246 be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
2247 NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2248 if (be_lun->lun_zone == NULL) {
2249 snprintf(req->error_str, sizeof(req->error_str),
2250 "error allocating UMA zone");
2254 if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2255 cbe_lun->lun_type = params->device_type;
2257 cbe_lun->lun_type = T_DIRECT;
2258 be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2260 value = ctl_get_opt(&cbe_lun->options, "ha_role");
2261 if (value != NULL) {
2262 if (strcmp(value, "primary") == 0)
2263 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2264 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2265 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2267 if (cbe_lun->lun_type == T_DIRECT ||
2268 cbe_lun->lun_type == T_CDROM) {
2269 be_lun->size_bytes = params->lun_size_bytes;
2270 if (params->blocksize_bytes != 0)
2271 cbe_lun->blocksize = params->blocksize_bytes;
2272 else if (cbe_lun->lun_type == T_CDROM)
2273 cbe_lun->blocksize = 2048;
2275 cbe_lun->blocksize = 512;
2276 be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2277 cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2278 0 : (be_lun->size_blocks - 1);
2280 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2281 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2282 retval = ctl_be_block_open(be_lun, req);
2285 req->status = CTL_LUN_WARNING;
2288 num_threads = cbb_num_threads;
2293 value = ctl_get_opt(&cbe_lun->options, "num_threads");
2294 if (value != NULL) {
2295 tmp_num_threads = strtol(value, NULL, 0);
2298 * We don't let the user specify less than one
2299 * thread, but hope he's clueful enough not to
2300 * specify 1000 threads.
2302 if (tmp_num_threads < 1) {
2303 snprintf(req->error_str, sizeof(req->error_str),
2304 "invalid number of threads %s",
2308 num_threads = tmp_num_threads;
2311 if (be_lun->vn == NULL)
2312 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2313 /* Tell the user the blocksize we ended up using */
2314 params->lun_size_bytes = be_lun->size_bytes;
2315 params->blocksize_bytes = cbe_lun->blocksize;
2316 if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2317 cbe_lun->req_lun_id = params->req_lun_id;
2318 cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2320 cbe_lun->req_lun_id = 0;
2322 cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
2323 cbe_lun->lun_config_status = ctl_be_block_lun_config_status;
2324 cbe_lun->be = &ctl_be_block_driver;
2326 if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2327 snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2329 strncpy((char *)cbe_lun->serial_num, tmpstr,
2330 MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2332 /* Tell the user what we used for a serial number */
2333 strncpy((char *)params->serial_num, tmpstr,
2334 MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2336 strncpy((char *)cbe_lun->serial_num, params->serial_num,
2337 MIN(sizeof(cbe_lun->serial_num),
2338 sizeof(params->serial_num)));
2340 if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2341 snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2342 strncpy((char *)cbe_lun->device_id, tmpstr,
2343 MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2345 /* Tell the user what we used for a device ID */
2346 strncpy((char *)params->device_id, tmpstr,
2347 MIN(sizeof(params->device_id), sizeof(tmpstr)));
2349 strncpy((char *)cbe_lun->device_id, params->device_id,
2350 MIN(sizeof(cbe_lun->device_id),
2351 sizeof(params->device_id)));
2354 TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2356 be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2357 taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2359 if (be_lun->io_taskqueue == NULL) {
2360 snprintf(req->error_str, sizeof(req->error_str),
2361 "unable to create taskqueue");
2366 * Note that we start the same number of threads by default for
2367 * both the file case and the block device case. For the file
2368 * case, we need multiple threads to allow concurrency, because the
2369 * vnode interface is designed to be a blocking interface. For the
2370 * block device case, ZFS zvols at least will block the caller's
2371 * context in many instances, and so we need multiple threads to
2372 * overcome that problem. Other block devices don't need as many
2373 * threads, but they shouldn't cause too many problems.
2375 * If the user wants to just have a single thread for a block
2376 * device, he can specify that when the LUN is created, or change
2377 * the tunable/sysctl to alter the default number of threads.
2379 retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2380 /*num threads*/num_threads,
2383 "%s taskq", be_lun->lunname);
2388 be_lun->num_threads = num_threads;
2390 mtx_lock(&softc->lock);
2392 STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2394 mtx_unlock(&softc->lock);
2396 retval = ctl_add_lun(&be_lun->cbe_lun);
2398 mtx_lock(&softc->lock);
2399 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2402 mtx_unlock(&softc->lock);
2403 snprintf(req->error_str, sizeof(req->error_str),
2404 "ctl_add_lun() returned error %d, see dmesg for "
2410 mtx_lock(&softc->lock);
2413 * Tell the config_status routine that we're waiting so it won't
2414 * clean up the LUN in the event of an error.
2416 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2418 while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2419 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2420 if (retval == EINTR)
2423 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2425 if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2426 snprintf(req->error_str, sizeof(req->error_str),
2427 "LUN configuration error, see dmesg for details");
2428 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2431 mtx_unlock(&softc->lock);
2434 params->req_lun_id = cbe_lun->lun_id;
2437 mtx_unlock(&softc->lock);
2439 be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2441 DEVSTAT_ALL_SUPPORTED,
2443 | DEVSTAT_TYPE_IF_OTHER,
2444 DEVSTAT_PRIORITY_OTHER);
2449 req->status = CTL_LUN_ERROR;
2451 if (be_lun->io_taskqueue != NULL)
2452 taskqueue_free(be_lun->io_taskqueue);
2453 ctl_be_block_close(be_lun);
2454 if (be_lun->dev_path != NULL)
2455 free(be_lun->dev_path, M_CTLBLK);
2456 if (be_lun->lun_zone != NULL)
2457 uma_zdestroy(be_lun->lun_zone);
2458 ctl_free_opts(&cbe_lun->options);
2459 mtx_destroy(&be_lun->queue_lock);
2460 mtx_destroy(&be_lun->io_lock);
2461 free(be_lun, M_CTLBLK);
2467 ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2469 struct ctl_lun_rm_params *params;
2470 struct ctl_be_block_lun *be_lun;
2471 struct ctl_be_lun *cbe_lun;
2474 params = &req->reqdata.rm;
2476 mtx_lock(&softc->lock);
2477 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2478 if (be_lun->cbe_lun.lun_id == params->lun_id)
2481 mtx_unlock(&softc->lock);
2482 if (be_lun == NULL) {
2483 snprintf(req->error_str, sizeof(req->error_str),
2484 "LUN %u is not managed by the block backend",
2488 cbe_lun = &be_lun->cbe_lun;
2490 retval = ctl_disable_lun(cbe_lun);
2492 snprintf(req->error_str, sizeof(req->error_str),
2493 "error %d returned from ctl_disable_lun() for "
2494 "LUN %d", retval, params->lun_id);
2498 if (be_lun->vn != NULL) {
2499 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2500 ctl_lun_no_media(cbe_lun);
2501 taskqueue_drain_all(be_lun->io_taskqueue);
2502 ctl_be_block_close(be_lun);
2505 retval = ctl_invalidate_lun(cbe_lun);
2507 snprintf(req->error_str, sizeof(req->error_str),
2508 "error %d returned from ctl_invalidate_lun() for "
2509 "LUN %d", retval, params->lun_id);
2513 mtx_lock(&softc->lock);
2514 be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2515 while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2516 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2517 if (retval == EINTR)
2520 be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2522 if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2523 snprintf(req->error_str, sizeof(req->error_str),
2524 "interrupted waiting for LUN to be freed");
2525 mtx_unlock(&softc->lock);
2529 STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2532 mtx_unlock(&softc->lock);
2534 taskqueue_drain_all(be_lun->io_taskqueue);
2535 taskqueue_free(be_lun->io_taskqueue);
2537 if (be_lun->disk_stats != NULL)
2538 devstat_remove_entry(be_lun->disk_stats);
2540 uma_zdestroy(be_lun->lun_zone);
2542 ctl_free_opts(&cbe_lun->options);
2543 free(be_lun->dev_path, M_CTLBLK);
2544 mtx_destroy(&be_lun->queue_lock);
2545 mtx_destroy(&be_lun->io_lock);
2546 free(be_lun, M_CTLBLK);
2548 req->status = CTL_LUN_OK;
2552 req->status = CTL_LUN_ERROR;
2557 ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2559 struct ctl_lun_modify_params *params;
2560 struct ctl_be_block_lun *be_lun;
2561 struct ctl_be_lun *cbe_lun;
2566 params = &req->reqdata.modify;
2568 mtx_lock(&softc->lock);
2569 STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2570 if (be_lun->cbe_lun.lun_id == params->lun_id)
2573 mtx_unlock(&softc->lock);
2574 if (be_lun == NULL) {
2575 snprintf(req->error_str, sizeof(req->error_str),
2576 "LUN %u is not managed by the block backend",
2580 cbe_lun = &be_lun->cbe_lun;
2582 if (params->lun_size_bytes != 0)
2583 be_lun->params.lun_size_bytes = params->lun_size_bytes;
2584 ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
2586 wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
2587 value = ctl_get_opt(&cbe_lun->options, "ha_role");
2588 if (value != NULL) {
2589 if (strcmp(value, "primary") == 0)
2590 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2592 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2593 } else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2594 cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2596 cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2597 if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
2598 if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
2599 ctl_lun_primary(cbe_lun);
2601 ctl_lun_secondary(cbe_lun);
2604 oldsize = be_lun->size_blocks;
2605 if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2606 control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2607 if (be_lun->vn == NULL)
2608 error = ctl_be_block_open(be_lun, req);
2609 else if (vn_isdisk(be_lun->vn, &error))
2610 error = ctl_be_block_open_dev(be_lun, req);
2611 else if (be_lun->vn->v_type == VREG) {
2612 vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2613 error = ctl_be_block_open_file(be_lun, req);
2614 VOP_UNLOCK(be_lun->vn, 0);
2617 if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
2618 be_lun->vn != NULL) {
2619 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2620 ctl_lun_has_media(cbe_lun);
2621 } else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2622 be_lun->vn == NULL) {
2623 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2624 ctl_lun_no_media(cbe_lun);
2626 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2628 if (be_lun->vn != NULL) {
2629 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2630 ctl_lun_no_media(cbe_lun);
2631 taskqueue_drain_all(be_lun->io_taskqueue);
2632 error = ctl_be_block_close(be_lun);
2636 if (be_lun->size_blocks != oldsize)
2637 ctl_lun_capacity_changed(cbe_lun);
2639 /* Tell the user the exact size we ended up using */
2640 params->lun_size_bytes = be_lun->size_bytes;
2642 req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2646 req->status = CTL_LUN_ERROR;
2651 ctl_be_block_lun_shutdown(void *be_lun)
2653 struct ctl_be_block_lun *lun;
2654 struct ctl_be_block_softc *softc;
2656 lun = (struct ctl_be_block_lun *)be_lun;
2659 mtx_lock(&softc->lock);
2660 lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2661 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2663 mtx_unlock(&softc->lock);
2667 ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2669 struct ctl_be_block_lun *lun;
2670 struct ctl_be_block_softc *softc;
2672 lun = (struct ctl_be_block_lun *)be_lun;
2675 if (status == CTL_LUN_CONFIG_OK) {
2676 mtx_lock(&softc->lock);
2677 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2678 if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2680 mtx_unlock(&softc->lock);
2683 * We successfully added the LUN, attempt to enable it.
2685 if (ctl_enable_lun(&lun->cbe_lun) != 0) {
2686 printf("%s: ctl_enable_lun() failed!\n", __func__);
2687 if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
2688 printf("%s: ctl_invalidate_lun() failed!\n",
2697 mtx_lock(&softc->lock);
2698 lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2699 lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2701 mtx_unlock(&softc->lock);
2706 ctl_be_block_config_write(union ctl_io *io)
2708 struct ctl_be_block_lun *be_lun;
2709 struct ctl_be_lun *cbe_lun;
2712 DPRINTF("entered\n");
2714 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2715 CTL_PRIV_BACKEND_LUN].ptr;
2716 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2719 switch (io->scsiio.cdb[0]) {
2720 case SYNCHRONIZE_CACHE:
2721 case SYNCHRONIZE_CACHE_16:
2726 * The upper level CTL code will filter out any CDBs with
2727 * the immediate bit set and return the proper error.
2729 * We don't really need to worry about what LBA range the
2730 * user asked to be synced out. When they issue a sync
2731 * cache command, we'll sync out the whole thing.
2733 mtx_lock(&be_lun->queue_lock);
2734 STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2736 mtx_unlock(&be_lun->queue_lock);
2737 taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2739 case START_STOP_UNIT: {
2740 struct scsi_start_stop_unit *cdb;
2741 struct ctl_lun_req req;
2743 cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2744 if ((cdb->how & SSS_PC_MASK) != 0) {
2745 ctl_set_success(&io->scsiio);
2746 ctl_config_write_done(io);
2749 if (cdb->how & SSS_START) {
2750 if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2751 retval = ctl_be_block_open(be_lun, &req);
2752 cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2754 cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2755 ctl_lun_has_media(cbe_lun);
2757 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2758 ctl_lun_no_media(cbe_lun);
2761 ctl_start_lun(cbe_lun);
2763 ctl_stop_lun(cbe_lun);
2764 if (cdb->how & SSS_LOEJ) {
2765 cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2766 cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2767 ctl_lun_ejected(cbe_lun);
2768 if (be_lun->vn != NULL)
2769 ctl_be_block_close(be_lun);
2773 ctl_set_success(&io->scsiio);
2774 ctl_config_write_done(io);
2778 ctl_set_success(&io->scsiio);
2779 ctl_config_write_done(io);
2782 ctl_set_invalid_opcode(&io->scsiio);
2783 ctl_config_write_done(io);
2784 retval = CTL_RETVAL_COMPLETE;
2792 ctl_be_block_config_read(union ctl_io *io)
2794 struct ctl_be_block_lun *be_lun;
2795 struct ctl_be_lun *cbe_lun;
2798 DPRINTF("entered\n");
2800 cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2801 CTL_PRIV_BACKEND_LUN].ptr;
2802 be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2804 switch (io->scsiio.cdb[0]) {
2805 case SERVICE_ACTION_IN:
2806 if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2807 mtx_lock(&be_lun->queue_lock);
2808 STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2809 &io->io_hdr, links);
2810 mtx_unlock(&be_lun->queue_lock);
2811 taskqueue_enqueue(be_lun->io_taskqueue,
2813 retval = CTL_RETVAL_QUEUED;
2816 ctl_set_invalid_field(&io->scsiio,
2822 ctl_config_read_done(io);
2823 retval = CTL_RETVAL_COMPLETE;
2826 ctl_set_invalid_opcode(&io->scsiio);
2827 ctl_config_read_done(io);
2828 retval = CTL_RETVAL_COMPLETE;
2836 ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2838 struct ctl_be_block_lun *lun;
2841 lun = (struct ctl_be_block_lun *)be_lun;
2843 retval = sbuf_printf(sb, "\t<num_threads>");
2846 retval = sbuf_printf(sb, "%d", lun->num_threads);
2849 retval = sbuf_printf(sb, "</num_threads>\n");
2856 ctl_be_block_lun_attr(void *be_lun, const char *attrname)
2858 struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2860 if (lun->getattr == NULL)
2861 return (UINT64_MAX);
2862 return (lun->getattr(lun, attrname));
2866 ctl_be_block_init(void)
2868 struct ctl_be_block_softc *softc;
2871 softc = &backend_block_softc;
2874 mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2875 beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2876 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2877 STAILQ_INIT(&softc->lun_list);