ctl_backend_block.c revision 275895
1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation
4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation
5229997Sken * All rights reserved.
6229997Sken *
7232604Strasz * Portions of this software were developed by Edward Tomasz Napierala
8232604Strasz * under sponsorship from the FreeBSD Foundation.
9232604Strasz *
10229997Sken * Redistribution and use in source and binary forms, with or without
11229997Sken * modification, are permitted provided that the following conditions
12229997Sken * are met:
13229997Sken * 1. Redistributions of source code must retain the above copyright
14229997Sken *    notice, this list of conditions, and the following disclaimer,
15229997Sken *    without modification.
16229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
18229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
19229997Sken *    including a substantially similar Disclaimer requirement for further
20229997Sken *    binary redistribution.
21229997Sken *
22229997Sken * NO WARRANTY
23229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33229997Sken * POSSIBILITY OF SUCH DAMAGES.
34229997Sken *
35229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
36229997Sken */
37229997Sken/*
38229997Sken * CAM Target Layer driver backend for block devices.
39229997Sken *
40229997Sken * Author: Ken Merry <ken@FreeBSD.org>
41229997Sken */
42229997Sken#include <sys/cdefs.h>
43229997Sken__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl_backend_block.c 275895 2014-12-18 08:46:53Z mav $");
44229997Sken
45229997Sken#include <opt_kdtrace.h>
46229997Sken
47229997Sken#include <sys/param.h>
48229997Sken#include <sys/systm.h>
49229997Sken#include <sys/kernel.h>
50229997Sken#include <sys/types.h>
51229997Sken#include <sys/kthread.h>
52229997Sken#include <sys/bio.h>
53229997Sken#include <sys/fcntl.h>
54265634Smav#include <sys/limits.h>
55229997Sken#include <sys/lock.h>
56229997Sken#include <sys/mutex.h>
57229997Sken#include <sys/condvar.h>
58229997Sken#include <sys/malloc.h>
59229997Sken#include <sys/conf.h>
60229997Sken#include <sys/ioccom.h>
61229997Sken#include <sys/queue.h>
62229997Sken#include <sys/sbuf.h>
63229997Sken#include <sys/endian.h>
64229997Sken#include <sys/uio.h>
65229997Sken#include <sys/buf.h>
66229997Sken#include <sys/taskqueue.h>
67229997Sken#include <sys/vnode.h>
68229997Sken#include <sys/namei.h>
69229997Sken#include <sys/mount.h>
70229997Sken#include <sys/disk.h>
71229997Sken#include <sys/fcntl.h>
72229997Sken#include <sys/filedesc.h>
73275892Smav#include <sys/filio.h>
74229997Sken#include <sys/proc.h>
75229997Sken#include <sys/pcpu.h>
76229997Sken#include <sys/module.h>
77229997Sken#include <sys/sdt.h>
78229997Sken#include <sys/devicestat.h>
79229997Sken#include <sys/sysctl.h>
80229997Sken
81229997Sken#include <geom/geom.h>
82229997Sken
83229997Sken#include <cam/cam.h>
84229997Sken#include <cam/scsi/scsi_all.h>
85229997Sken#include <cam/scsi/scsi_da.h>
86229997Sken#include <cam/ctl/ctl_io.h>
87229997Sken#include <cam/ctl/ctl.h>
88229997Sken#include <cam/ctl/ctl_backend.h>
89229997Sken#include <cam/ctl/ctl_frontend_internal.h>
90229997Sken#include <cam/ctl/ctl_ioctl.h>
91229997Sken#include <cam/ctl/ctl_scsi_all.h>
92229997Sken#include <cam/ctl/ctl_error.h>
93229997Sken
94229997Sken/*
95265642Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB
96265642Smav * I/O.  If we get an I/O larger than that, we'll split it.
97229997Sken */
98268151Smav#define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
99268151Smav#define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
100265642Smav#define	CTLBLK_MAX_SEG		MAXPHYS
101268151Smav#define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
102268151Smav#define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
103229997Sken
104229997Sken#ifdef CTLBLK_DEBUG
105229997Sken#define DPRINTF(fmt, args...) \
106229997Sken    printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
107229997Sken#else
108229997Sken#define DPRINTF(fmt, args...) do {} while(0)
109229997Sken#endif
110229997Sken
111268150Smav#define PRIV(io)	\
112268150Smav    ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
113268151Smav#define ARGS(io)	\
114268151Smav    ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
115268150Smav
116229997SkenSDT_PROVIDER_DEFINE(cbb);
117229997Sken
118229997Skentypedef enum {
119229997Sken	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
120229997Sken	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
121229997Sken	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
122229997Sken	CTL_BE_BLOCK_LUN_MULTI_THREAD	= 0x08
123229997Sken} ctl_be_block_lun_flags;
124229997Sken
125229997Skentypedef enum {
126229997Sken	CTL_BE_BLOCK_NONE,
127229997Sken	CTL_BE_BLOCK_DEV,
128229997Sken	CTL_BE_BLOCK_FILE
129229997Sken} ctl_be_block_type;
130229997Sken
131229997Skenstruct ctl_be_block_devdata {
132229997Sken	struct cdev *cdev;
133229997Sken	struct cdevsw *csw;
134229997Sken	int dev_ref;
135229997Sken};
136229997Sken
137229997Skenstruct ctl_be_block_filedata {
138229997Sken	struct ucred *cred;
139229997Sken};
140229997Sken
141229997Skenunion ctl_be_block_bedata {
142229997Sken	struct ctl_be_block_devdata dev;
143229997Sken	struct ctl_be_block_filedata file;
144229997Sken};
145229997Sken
146229997Skenstruct ctl_be_block_io;
147229997Skenstruct ctl_be_block_lun;
148229997Sken
149229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
150229997Sken			       struct ctl_be_block_io *beio);
151274732Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
152274732Smav				  const char *attrname);
153229997Sken
154229997Sken/*
155229997Sken * Backend LUN structure.  There is a 1:1 mapping between a block device
156229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN.
157229997Sken */
158229997Skenstruct ctl_be_block_lun {
159273315Smav	struct ctl_lun_create_params params;
160229997Sken	struct ctl_block_disk *disk;
161229997Sken	char lunname[32];
162229997Sken	char *dev_path;
163229997Sken	ctl_be_block_type dev_type;
164229997Sken	struct vnode *vn;
165229997Sken	union ctl_be_block_bedata backend;
166229997Sken	cbb_dispatch_t dispatch;
167229997Sken	cbb_dispatch_t lun_flush;
168265634Smav	cbb_dispatch_t unmap;
169275892Smav	cbb_dispatch_t get_lba_status;
170274732Smav	cbb_getattr_t getattr;
171229997Sken	uma_zone_t lun_zone;
172229997Sken	uint64_t size_blocks;
173229997Sken	uint64_t size_bytes;
174229997Sken	uint32_t blocksize;
175229997Sken	int blocksize_shift;
176264727Smav	uint16_t pblockexp;
177264727Smav	uint16_t pblockoff;
178229997Sken	struct ctl_be_block_softc *softc;
179229997Sken	struct devstat *disk_stats;
180229997Sken	ctl_be_block_lun_flags flags;
181229997Sken	STAILQ_ENTRY(ctl_be_block_lun) links;
182229997Sken	struct ctl_be_lun ctl_be_lun;
183229997Sken	struct taskqueue *io_taskqueue;
184229997Sken	struct task io_task;
185229997Sken	int num_threads;
186229997Sken	STAILQ_HEAD(, ctl_io_hdr) input_queue;
187275892Smav	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
188229997Sken	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
189229997Sken	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
190268549Smav	struct mtx_padalign io_lock;
191268549Smav	struct mtx_padalign queue_lock;
192229997Sken};
193229997Sken
194229997Sken/*
195229997Sken * Overall softc structure for the block backend module.
196229997Sken */
197229997Skenstruct ctl_be_block_softc {
198229997Sken	struct mtx			 lock;
199229997Sken	int				 num_disks;
200229997Sken	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
201229997Sken	int				 num_luns;
202229997Sken	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
203229997Sken};
204229997Sken
205229997Skenstatic struct ctl_be_block_softc backend_block_softc;
206229997Sken
207229997Sken/*
208229997Sken * Per-I/O information.
209229997Sken */
210229997Skenstruct ctl_be_block_io {
211229997Sken	union ctl_io			*io;
212229997Sken	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
213229997Sken	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
214229997Sken	int				bio_cmd;
215229997Sken	int				num_segs;
216229997Sken	int				num_bios_sent;
217229997Sken	int				num_bios_done;
218229997Sken	int				send_complete;
219229997Sken	int				num_errors;
220229997Sken	struct bintime			ds_t0;
221229997Sken	devstat_tag_type		ds_tag_type;
222229997Sken	devstat_trans_flags		ds_trans_type;
223229997Sken	uint64_t			io_len;
224229997Sken	uint64_t			io_offset;
225229997Sken	struct ctl_be_block_softc	*softc;
226229997Sken	struct ctl_be_block_lun		*lun;
227265634Smav	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
228229997Sken};
229229997Sken
230229997Skenstatic int cbb_num_threads = 14;
231229997SkenTUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads);
232229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
233229997Sken	    "CAM Target Layer Block Backend");
234229997SkenSYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW,
235229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
236229997Sken
237229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
238229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
239229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
240229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
241229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
242229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
243229997Sken				    struct ctl_be_block_io *beio);
244229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
245229997Sken				       struct ctl_be_block_io *beio);
246275892Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
247275892Smav				  struct ctl_be_block_io *beio);
248275893Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
249275893Smav					 const char *attrname);
250229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
251229997Sken				   struct ctl_be_block_io *beio);
252265634Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
253265634Smav				   struct ctl_be_block_io *beio);
254229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
255229997Sken				      struct ctl_be_block_io *beio);
256274732Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
257274732Smav					 const char *attrname);
258275892Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
259275892Smav				    union ctl_io *io);
260229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
261229997Sken				    union ctl_io *io);
262229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
263229997Sken				  union ctl_io *io);
264229997Skenstatic void ctl_be_block_worker(void *context, int pending);
265229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
266229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
267229997Sken				   int flag, struct thread *td);
268229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
269229997Sken				  struct ctl_lun_req *req);
270229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
271229997Sken				 struct ctl_lun_req *req);
272229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
273229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
274229997Sken			     struct ctl_be_block_lun *be_lun,
275229997Sken			     struct ctl_lun_req *req);
276229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
277229997Sken			       struct ctl_lun_req *req);
278229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
279229997Sken			   struct ctl_lun_req *req);
280232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
281232604Strasz				  struct ctl_lun_req *req);
282232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
283232604Strasz				 struct ctl_lun_req *req);
284232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
285232604Strasz			   struct ctl_lun_req *req);
286229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
287229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
288229997Sken					   ctl_lun_config_status status);
289229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
290229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
291229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
292274732Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
293229997Skenint ctl_be_block_init(void);
294229997Sken
295229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
296229997Sken{
297230334Sken	.name = "block",
298230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
299230334Sken	.init = ctl_be_block_init,
300230334Sken	.data_submit = ctl_be_block_submit,
301230334Sken	.data_move_done = ctl_be_block_move_done,
302230334Sken	.config_read = ctl_be_block_config_read,
303230334Sken	.config_write = ctl_be_block_config_write,
304230334Sken	.ioctl = ctl_be_block_ioctl,
305274732Smav	.lun_info = ctl_be_block_lun_info,
306274732Smav	.lun_attr = ctl_be_block_lun_attr
307229997Sken};
308229997Sken
309229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
310229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
311229997Sken
312265494Straszstatic uma_zone_t beio_zone;
313265494Strasz
314229997Skenstatic struct ctl_be_block_io *
315229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
316229997Sken{
317229997Sken	struct ctl_be_block_io *beio;
318229997Sken
319265494Strasz	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
320265494Strasz	beio->softc = softc;
321229997Sken	return (beio);
322229997Sken}
323229997Sken
324229997Skenstatic void
325229997Skenctl_free_beio(struct ctl_be_block_io *beio)
326229997Sken{
327229997Sken	int duplicate_free;
328229997Sken	int i;
329229997Sken
330229997Sken	duplicate_free = 0;
331229997Sken
332229997Sken	for (i = 0; i < beio->num_segs; i++) {
333229997Sken		if (beio->sg_segs[i].addr == NULL)
334229997Sken			duplicate_free++;
335229997Sken
336229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
337229997Sken		beio->sg_segs[i].addr = NULL;
338268151Smav
339268151Smav		/* For compare we had two equal S/G lists. */
340268151Smav		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
341268151Smav			uma_zfree(beio->lun->lun_zone,
342268151Smav			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
343268151Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
344268151Smav		}
345229997Sken	}
346229997Sken
347229997Sken	if (duplicate_free > 0) {
348229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
349229997Sken		       duplicate_free, beio->num_segs);
350229997Sken	}
351229997Sken
352265494Strasz	uma_zfree(beio_zone, beio);
353229997Sken}
354229997Sken
355229997Skenstatic void
356229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
357229997Sken{
358268549Smav	union ctl_io *io = beio->io;
359229997Sken
360265634Smav	if (beio->beio_cont != NULL) {
361265634Smav		beio->beio_cont(beio);
362265634Smav	} else {
363265634Smav		ctl_free_beio(beio);
364268151Smav		ctl_data_submit_done(io);
365265634Smav	}
366229997Sken}
367229997Sken
368229997Skenstatic int
369229997Skenctl_be_block_move_done(union ctl_io *io)
370229997Sken{
371229997Sken	struct ctl_be_block_io *beio;
372229997Sken	struct ctl_be_block_lun *be_lun;
373268151Smav	struct ctl_lba_len_flags *lbalen;
374229997Sken#ifdef CTL_TIME_IO
375229997Sken	struct bintime cur_bt;
376268151Smav#endif
377268151Smav	int i;
378229997Sken
379268150Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
380229997Sken	be_lun = beio->lun;
381229997Sken
382229997Sken	DPRINTF("entered\n");
383229997Sken
384229997Sken#ifdef CTL_TIME_IO
385229997Sken	getbintime(&cur_bt);
386229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
387229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
388229997Sken	io->io_hdr.num_dmas++;
389229997Sken#endif
390268151Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
391229997Sken
392229997Sken	/*
393229997Sken	 * We set status at this point for read commands, and write
394229997Sken	 * commands with errors.
395229997Sken	 */
396275881Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
397275881Smav		;
398275881Smav	} else if ((io->io_hdr.port_status == 0) &&
399268151Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
400268151Smav		lbalen = ARGS(beio->io);
401268151Smav		if (lbalen->flags & CTL_LLF_READ) {
402268151Smav			ctl_set_success(&io->scsiio);
403268151Smav		} else if (lbalen->flags & CTL_LLF_COMPARE) {
404268151Smav			/* We have two data blocks ready for comparison. */
405268151Smav			for (i = 0; i < beio->num_segs; i++) {
406268151Smav				if (memcmp(beio->sg_segs[i].addr,
407268151Smav				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
408268151Smav				    beio->sg_segs[i].len) != 0)
409268151Smav					break;
410268151Smav			}
411268151Smav			if (i < beio->num_segs)
412268151Smav				ctl_set_sense(&io->scsiio,
413268151Smav				    /*current_error*/ 1,
414268151Smav				    /*sense_key*/ SSD_KEY_MISCOMPARE,
415268151Smav				    /*asc*/ 0x1D,
416268151Smav				    /*ascq*/ 0x00,
417268151Smav				    SSD_ELEM_NONE);
418268151Smav			else
419268151Smav				ctl_set_success(&io->scsiio);
420268151Smav		}
421275881Smav	} else if ((io->io_hdr.port_status != 0) &&
422275881Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
423275881Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
424229997Sken		/*
425229997Sken		 * For hardware error sense keys, the sense key
426229997Sken		 * specific value is defined to be a retry count,
427229997Sken		 * but we use it to pass back an internal FETD
428229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
429229997Sken		 * using 16 bits for an error code, since that's
430229997Sken		 * all the space we have in the sks field.
431229997Sken		 */
432229997Sken		ctl_set_internal_failure(&io->scsiio,
433229997Sken					 /*sks_valid*/ 1,
434229997Sken					 /*retry_count*/
435229997Sken					 io->io_hdr.port_status);
436229997Sken	}
437229997Sken
438229997Sken	/*
439229997Sken	 * If this is a read, or a write with errors, it is done.
440229997Sken	 */
441229997Sken	if ((beio->bio_cmd == BIO_READ)
442229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
443229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
444229997Sken		ctl_complete_beio(beio);
445229997Sken		return (0);
446229997Sken	}
447229997Sken
448229997Sken	/*
449229997Sken	 * At this point, we have a write and the DMA completed
450229997Sken	 * successfully.  We now have to queue it to the task queue to
451229997Sken	 * execute the backend I/O.  That is because we do blocking
452229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
453229997Sken	 * This move done routine is generally called in the SIM's
454229997Sken	 * interrupt context, and therefore we cannot block.
455229997Sken	 */
456268549Smav	mtx_lock(&be_lun->queue_lock);
457229997Sken	/*
458229997Sken	 * XXX KDM make sure that links is okay to use at this point.
459229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
460229997Sken	 * or deal with resource allocation here.
461229997Sken	 */
462229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
463268549Smav	mtx_unlock(&be_lun->queue_lock);
464229997Sken
465229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
466229997Sken
467229997Sken	return (0);
468229997Sken}
469229997Sken
470229997Skenstatic void
471229997Skenctl_be_block_biodone(struct bio *bio)
472229997Sken{
473229997Sken	struct ctl_be_block_io *beio;
474229997Sken	struct ctl_be_block_lun *be_lun;
475229997Sken	union ctl_io *io;
476262299Smav	int error;
477229997Sken
478229997Sken	beio = bio->bio_caller1;
479229997Sken	be_lun = beio->lun;
480229997Sken	io = beio->io;
481229997Sken
482229997Sken	DPRINTF("entered\n");
483229997Sken
484262299Smav	error = bio->bio_error;
485268549Smav	mtx_lock(&be_lun->io_lock);
486262299Smav	if (error != 0)
487229997Sken		beio->num_errors++;
488229997Sken
489229997Sken	beio->num_bios_done++;
490229997Sken
491229997Sken	/*
492229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
493229997Sken	 * during the free might cause it to complain.
494229997Sken	 */
495229997Sken	g_destroy_bio(bio);
496229997Sken
497229997Sken	/*
498229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
499229997Sken	 * complete, then we're done.
500229997Sken	 */
501229997Sken	if ((beio->send_complete == 0)
502229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
503268549Smav		mtx_unlock(&be_lun->io_lock);
504229997Sken		return;
505229997Sken	}
506229997Sken
507229997Sken	/*
508229997Sken	 * At this point, we've verified that we are the last I/O to
509229997Sken	 * complete, so it's safe to drop the lock.
510229997Sken	 */
511268549Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
512268549Smav	    beio->ds_tag_type, beio->ds_trans_type,
513268549Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
514268549Smav	mtx_unlock(&be_lun->io_lock);
515229997Sken
516229997Sken	/*
517229997Sken	 * If there are any errors from the backing device, we fail the
518229997Sken	 * entire I/O with a medium error.
519229997Sken	 */
520229997Sken	if (beio->num_errors > 0) {
521262299Smav		if (error == EOPNOTSUPP) {
522262299Smav			ctl_set_invalid_opcode(&io->scsiio);
523274004Smav		} else if (error == ENOSPC) {
524274004Smav			ctl_set_space_alloc_fail(&io->scsiio);
525262299Smav		} else if (beio->bio_cmd == BIO_FLUSH) {
526229997Sken			/* XXX KDM is there is a better error here? */
527229997Sken			ctl_set_internal_failure(&io->scsiio,
528229997Sken						 /*sks_valid*/ 1,
529229997Sken						 /*retry_count*/ 0xbad2);
530229997Sken		} else
531229997Sken			ctl_set_medium_error(&io->scsiio);
532229997Sken		ctl_complete_beio(beio);
533229997Sken		return;
534229997Sken	}
535229997Sken
536229997Sken	/*
537268151Smav	 * If this is a write, a flush, a delete or verify, we're all done.
538229997Sken	 * If this is a read, we can now send the data to the user.
539229997Sken	 */
540229997Sken	if ((beio->bio_cmd == BIO_WRITE)
541265634Smav	 || (beio->bio_cmd == BIO_FLUSH)
542268151Smav	 || (beio->bio_cmd == BIO_DELETE)
543268151Smav	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
544229997Sken		ctl_set_success(&io->scsiio);
545229997Sken		ctl_complete_beio(beio);
546229997Sken	} else {
547275881Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
548275881Smav		    beio->beio_cont == NULL)
549275881Smav			ctl_set_success(&io->scsiio);
550229997Sken#ifdef CTL_TIME_IO
551229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
552229997Sken#endif
553229997Sken		ctl_datamove(io);
554229997Sken	}
555229997Sken}
556229997Sken
557229997Skenstatic void
558229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
559229997Sken			struct ctl_be_block_io *beio)
560229997Sken{
561268549Smav	union ctl_io *io = beio->io;
562229997Sken	struct mount *mountpoint;
563241896Skib	int error, lock_flags;
564229997Sken
565229997Sken	DPRINTF("entered\n");
566229997Sken
567268549Smav	binuptime(&beio->ds_t0);
568268549Smav	mtx_lock(&be_lun->io_lock);
569268549Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
570268549Smav	mtx_unlock(&be_lun->io_lock);
571229997Sken
572268549Smav	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
573229997Sken
574229997Sken	if (MNT_SHARED_WRITES(mountpoint)
575229997Sken	 || ((mountpoint == NULL)
576229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
577229997Sken		lock_flags = LK_SHARED;
578229997Sken	else
579229997Sken		lock_flags = LK_EXCLUSIVE;
580229997Sken
581229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
582229997Sken
583229997Sken	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
584229997Sken	VOP_UNLOCK(be_lun->vn, 0);
585229997Sken
586229997Sken	vn_finished_write(mountpoint);
587229997Sken
588268549Smav	mtx_lock(&be_lun->io_lock);
589268549Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
590268549Smav	    beio->ds_tag_type, beio->ds_trans_type,
591268549Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
592268549Smav	mtx_unlock(&be_lun->io_lock);
593268549Smav
594229997Sken	if (error == 0)
595229997Sken		ctl_set_success(&io->scsiio);
596229997Sken	else {
597229997Sken		/* XXX KDM is there is a better error here? */
598229997Sken		ctl_set_internal_failure(&io->scsiio,
599229997Sken					 /*sks_valid*/ 1,
600229997Sken					 /*retry_count*/ 0xbad1);
601229997Sken	}
602229997Sken
603229997Sken	ctl_complete_beio(beio);
604229997Sken}
605229997Sken
606260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
607260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
608260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
609260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
610229997Sken
611229997Skenstatic void
612229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
613229997Sken			   struct ctl_be_block_io *beio)
614229997Sken{
615229997Sken	struct ctl_be_block_filedata *file_data;
616229997Sken	union ctl_io *io;
617229997Sken	struct uio xuio;
618229997Sken	struct iovec *xiovec;
619241896Skib	int flags;
620229997Sken	int error, i;
621229997Sken
622229997Sken	DPRINTF("entered\n");
623229997Sken
624229997Sken	file_data = &be_lun->backend.file;
625229997Sken	io = beio->io;
626272616Smav	flags = 0;
627272616Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
628272616Smav		flags |= IO_DIRECT;
629272616Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
630272616Smav		flags |= IO_SYNC;
631229997Sken
632268151Smav	bzero(&xuio, sizeof(xuio));
633229997Sken	if (beio->bio_cmd == BIO_READ) {
634229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
635268151Smav		xuio.uio_rw = UIO_READ;
636229997Sken	} else {
637229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
638268151Smav		xuio.uio_rw = UIO_WRITE;
639229997Sken	}
640229997Sken	xuio.uio_offset = beio->io_offset;
641229997Sken	xuio.uio_resid = beio->io_len;
642229997Sken	xuio.uio_segflg = UIO_SYSSPACE;
643229997Sken	xuio.uio_iov = beio->xiovecs;
644229997Sken	xuio.uio_iovcnt = beio->num_segs;
645229997Sken	xuio.uio_td = curthread;
646229997Sken
647229997Sken	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
648229997Sken		xiovec->iov_base = beio->sg_segs[i].addr;
649229997Sken		xiovec->iov_len = beio->sg_segs[i].len;
650229997Sken	}
651229997Sken
652268549Smav	binuptime(&beio->ds_t0);
653268549Smav	mtx_lock(&be_lun->io_lock);
654268549Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
655268549Smav	mtx_unlock(&be_lun->io_lock);
656268549Smav
657229997Sken	if (beio->bio_cmd == BIO_READ) {
658229997Sken		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
659229997Sken
660229997Sken		/*
661229997Sken		 * UFS pays attention to IO_DIRECT for reads.  If the
662229997Sken		 * DIRECTIO option is configured into the kernel, it calls
663229997Sken		 * ffs_rawread().  But that only works for single-segment
664229997Sken		 * uios with user space addresses.  In our case, with a
665229997Sken		 * kernel uio, it still reads into the buffer cache, but it
666229997Sken		 * will just try to release the buffer from the cache later
667229997Sken		 * on in ffs_read().
668229997Sken		 *
669229997Sken		 * ZFS does not pay attention to IO_DIRECT for reads.
670229997Sken		 *
671229997Sken		 * UFS does not pay attention to IO_SYNC for reads.
672229997Sken		 *
673229997Sken		 * ZFS pays attention to IO_SYNC (which translates into the
674229997Sken		 * Solaris define FRSYNC for zfs_read()) for reads.  It
675229997Sken		 * attempts to sync the file before reading.
676229997Sken		 *
677229997Sken		 * So, to attempt to provide some barrier semantics in the
678229997Sken		 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
679229997Sken		 */
680272616Smav		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
681229997Sken
682229997Sken		VOP_UNLOCK(be_lun->vn, 0);
683268151Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
684229997Sken	} else {
685229997Sken		struct mount *mountpoint;
686229997Sken		int lock_flags;
687229997Sken
688229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
689229997Sken
690229997Sken		if (MNT_SHARED_WRITES(mountpoint)
691229997Sken		 || ((mountpoint == NULL)
692229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
693229997Sken			lock_flags = LK_SHARED;
694229997Sken		else
695229997Sken			lock_flags = LK_EXCLUSIVE;
696229997Sken
697229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
698229997Sken
699229997Sken		/*
700229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
701229997Sken		 * is done asynchronously.  (Normally the write would just
702229997Sken		 * get put into cache.
703229997Sken		 *
704229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
705229997Sken		 * attempt to write the buffer out synchronously if that
706229997Sken		 * flag is set.
707229997Sken		 *
708229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
709229997Sken		 *
710229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
711229997Sken		 * for writes.  It will flush the transaction from the
712229997Sken		 * cache before returning.
713229997Sken		 *
714229997Sken		 * So if we've got the BIO_ORDERED flag set, we want
715229997Sken		 * IO_SYNC in either the UFS or ZFS case.
716229997Sken		 */
717272616Smav		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
718229997Sken		VOP_UNLOCK(be_lun->vn, 0);
719229997Sken
720229997Sken		vn_finished_write(mountpoint);
721268151Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
722229997Sken        }
723229997Sken
724268549Smav	mtx_lock(&be_lun->io_lock);
725268549Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
726268549Smav	    beio->ds_tag_type, beio->ds_trans_type,
727268549Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
728268549Smav	mtx_unlock(&be_lun->io_lock);
729268549Smav
730229997Sken	/*
731229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
732229997Sken	 * return the I/O to the user.
733229997Sken	 */
734229997Sken	if (error != 0) {
735229997Sken		char path_str[32];
736229997Sken
737229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
738229997Sken		printf("%s%s command returned errno %d\n", path_str,
739229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
740274004Smav		if (error == ENOSPC) {
741274004Smav			ctl_set_space_alloc_fail(&io->scsiio);
742274004Smav		} else
743274004Smav			ctl_set_medium_error(&io->scsiio);
744229997Sken		ctl_complete_beio(beio);
745229997Sken		return;
746229997Sken	}
747229997Sken
748229997Sken	/*
749269226Smav	 * If this is a write or a verify, we're all done.
750229997Sken	 * If this is a read, we can now send the data to the user.
751229997Sken	 */
752269226Smav	if ((beio->bio_cmd == BIO_WRITE) ||
753269226Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
754229997Sken		ctl_set_success(&io->scsiio);
755229997Sken		ctl_complete_beio(beio);
756229997Sken	} else {
757275881Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
758275881Smav		    beio->beio_cont == NULL)
759275881Smav			ctl_set_success(&io->scsiio);
760229997Sken#ifdef CTL_TIME_IO
761229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
762229997Sken#endif
763229997Sken		ctl_datamove(io);
764229997Sken	}
765229997Sken}
766229997Sken
767229997Skenstatic void
768275892Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
769275892Smav			struct ctl_be_block_io *beio)
770275892Smav{
771275892Smav	union ctl_io *io = beio->io;
772275892Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
773275892Smav	struct scsi_get_lba_status_data *data;
774275892Smav	off_t roff, off;
775275892Smav	int error, status;
776275892Smav
777275892Smav	DPRINTF("entered\n");
778275892Smav
779275892Smav	off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift;
780275892Smav	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
781275892Smav	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
782275892Smav	    0, curthread->td_ucred, curthread);
783275892Smav	if (error == 0 && off > roff)
784275892Smav		status = 0;	/* mapped up to off */
785275892Smav	else {
786275892Smav		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
787275892Smav		    0, curthread->td_ucred, curthread);
788275892Smav		if (error == 0 && off > roff)
789275892Smav			status = 1;	/* deallocated up to off */
790275892Smav		else {
791275892Smav			status = 0;	/* unknown up to the end */
792275892Smav			off = be_lun->size_bytes;
793275892Smav		}
794275892Smav	}
795275892Smav	VOP_UNLOCK(be_lun->vn, 0);
796275892Smav
797275892Smav	off >>= be_lun->blocksize_shift;
798275892Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
799275892Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
800275892Smav	scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba),
801275892Smav	    data->descr[0].length);
802275892Smav	data->descr[0].status = status;
803275892Smav
804275892Smav	ctl_complete_beio(beio);
805275892Smav}
806275892Smav
807275893Smavstatic uint64_t
808275893Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
809275893Smav{
810275893Smav	struct vattr		vattr;
811275893Smav	struct statfs		statfs;
812275893Smav	int			error;
813275893Smav
814275893Smav	if (be_lun->vn == NULL)
815275893Smav		return (UINT64_MAX);
816275893Smav	if (strcmp(attrname, "blocksused") == 0) {
817275893Smav		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
818275893Smav		if (error != 0)
819275893Smav			return (UINT64_MAX);
820275893Smav		return (vattr.va_bytes >> be_lun->blocksize_shift);
821275893Smav	}
822275893Smav	if (strcmp(attrname, "blocksavail") == 0) {
823275893Smav		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
824275893Smav		if (error != 0)
825275893Smav			return (UINT64_MAX);
826275893Smav		return ((statfs.f_bavail * statfs.f_bsize) >>
827275893Smav		    be_lun->blocksize_shift);
828275893Smav	}
829275893Smav	return (UINT64_MAX);
830275893Smav}
831275893Smav
832275892Smavstatic void
833269429Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
834269429Smav			   struct ctl_be_block_io *beio)
835269429Smav{
836269429Smav	struct ctl_be_block_devdata *dev_data;
837269429Smav	union ctl_io *io;
838269429Smav	struct uio xuio;
839269429Smav	struct iovec *xiovec;
840269429Smav	int flags;
841269429Smav	int error, i;
842269429Smav
843269429Smav	DPRINTF("entered\n");
844269429Smav
845269429Smav	dev_data = &be_lun->backend.dev;
846269429Smav	io = beio->io;
847272616Smav	flags = 0;
848272616Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
849272616Smav		flags |= IO_DIRECT;
850272616Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
851272616Smav		flags |= IO_SYNC;
852269429Smav
853269429Smav	bzero(&xuio, sizeof(xuio));
854269429Smav	if (beio->bio_cmd == BIO_READ) {
855269429Smav		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
856269429Smav		xuio.uio_rw = UIO_READ;
857269429Smav	} else {
858269429Smav		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
859269429Smav		xuio.uio_rw = UIO_WRITE;
860269429Smav	}
861269429Smav	xuio.uio_offset = beio->io_offset;
862269429Smav	xuio.uio_resid = beio->io_len;
863269429Smav	xuio.uio_segflg = UIO_SYSSPACE;
864269429Smav	xuio.uio_iov = beio->xiovecs;
865269429Smav	xuio.uio_iovcnt = beio->num_segs;
866269429Smav	xuio.uio_td = curthread;
867269429Smav
868269429Smav	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
869269429Smav		xiovec->iov_base = beio->sg_segs[i].addr;
870269429Smav		xiovec->iov_len = beio->sg_segs[i].len;
871269429Smav	}
872269429Smav
873269429Smav	binuptime(&beio->ds_t0);
874269429Smav	mtx_lock(&be_lun->io_lock);
875269429Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
876269429Smav	mtx_unlock(&be_lun->io_lock);
877269429Smav
878269429Smav	if (beio->bio_cmd == BIO_READ) {
879272616Smav		error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
880269429Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
881269429Smav	} else {
882272616Smav		error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
883269429Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
884269429Smav	}
885269429Smav
886269429Smav	mtx_lock(&be_lun->io_lock);
887269429Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
888269429Smav	    beio->ds_tag_type, beio->ds_trans_type,
889269429Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
890269429Smav	mtx_unlock(&be_lun->io_lock);
891269429Smav
892269429Smav	/*
893269429Smav	 * If we got an error, set the sense data to "MEDIUM ERROR" and
894269429Smav	 * return the I/O to the user.
895269429Smav	 */
896269429Smav	if (error != 0) {
897274004Smav		if (error == ENOSPC) {
898274004Smav			ctl_set_space_alloc_fail(&io->scsiio);
899274004Smav		} else
900274004Smav			ctl_set_medium_error(&io->scsiio);
901269429Smav		ctl_complete_beio(beio);
902269429Smav		return;
903269429Smav	}
904269429Smav
905269429Smav	/*
906269429Smav	 * If this is a write or a verify, we're all done.
907269429Smav	 * If this is a read, we can now send the data to the user.
908269429Smav	 */
909269429Smav	if ((beio->bio_cmd == BIO_WRITE) ||
910269429Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
911269429Smav		ctl_set_success(&io->scsiio);
912269429Smav		ctl_complete_beio(beio);
913269429Smav	} else {
914275881Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
915275881Smav		    beio->beio_cont == NULL)
916275881Smav			ctl_set_success(&io->scsiio);
917269429Smav#ifdef CTL_TIME_IO
918269429Smav        	getbintime(&io->io_hdr.dma_start_bt);
919269429Smav#endif
920269429Smav		ctl_datamove(io);
921269429Smav	}
922269429Smav}
923269429Smav
924269429Smavstatic void
925275892Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
926275892Smav			struct ctl_be_block_io *beio)
927275892Smav{
928275892Smav	struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev;
929275892Smav	union ctl_io *io = beio->io;
930275892Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
931275892Smav	struct scsi_get_lba_status_data *data;
932275892Smav	off_t roff, off;
933275892Smav	int error, status;
934275892Smav
935275892Smav	DPRINTF("entered\n");
936275892Smav
937275892Smav	off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift;
938275892Smav	error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE,
939275892Smav	    (caddr_t)&off, FREAD, curthread);
940275892Smav	if (error == 0 && off > roff)
941275892Smav		status = 0;	/* mapped up to off */
942275892Smav	else {
943275892Smav		error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA,
944275892Smav		    (caddr_t)&off, FREAD, curthread);
945275892Smav		if (error == 0 && off > roff)
946275892Smav			status = 1;	/* deallocated up to off */
947275892Smav		else {
948275892Smav			status = 0;	/* unknown up to the end */
949275892Smav			off = be_lun->size_bytes;
950275892Smav		}
951275892Smav	}
952275892Smav
953275892Smav	off >>= be_lun->blocksize_shift;
954275892Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
955275892Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
956275892Smav	scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba),
957275892Smav	    data->descr[0].length);
958275892Smav	data->descr[0].status = status;
959275892Smav
960275892Smav	ctl_complete_beio(beio);
961275892Smav}
962275892Smav
963275892Smavstatic void
964229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
965229997Sken		       struct ctl_be_block_io *beio)
966229997Sken{
967229997Sken	struct bio *bio;
968229997Sken	union ctl_io *io;
969229997Sken	struct ctl_be_block_devdata *dev_data;
970229997Sken
971229997Sken	dev_data = &be_lun->backend.dev;
972229997Sken	io = beio->io;
973229997Sken
974229997Sken	DPRINTF("entered\n");
975229997Sken
976229997Sken	/* This can't fail, it's a blocking allocation. */
977229997Sken	bio = g_alloc_bio();
978229997Sken
979229997Sken	bio->bio_cmd	    = BIO_FLUSH;
980229997Sken	bio->bio_flags	   |= BIO_ORDERED;
981229997Sken	bio->bio_dev	    = dev_data->cdev;
982229997Sken	bio->bio_offset	    = 0;
983229997Sken	bio->bio_data	    = 0;
984229997Sken	bio->bio_done	    = ctl_be_block_biodone;
985229997Sken	bio->bio_caller1    = beio;
986229997Sken	bio->bio_pblkno	    = 0;
987229997Sken
988229997Sken	/*
989229997Sken	 * We don't need to acquire the LUN lock here, because we are only
990229997Sken	 * sending one bio, and so there is no other context to synchronize
991229997Sken	 * with.
992229997Sken	 */
993229997Sken	beio->num_bios_sent = 1;
994229997Sken	beio->send_complete = 1;
995229997Sken
996229997Sken	binuptime(&beio->ds_t0);
997268549Smav	mtx_lock(&be_lun->io_lock);
998229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
999268549Smav	mtx_unlock(&be_lun->io_lock);
1000229997Sken
1001229997Sken	(*dev_data->csw->d_strategy)(bio);
1002229997Sken}
1003229997Sken
1004229997Skenstatic void
1005265634Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1006265634Smav		       struct ctl_be_block_io *beio,
1007265634Smav		       uint64_t off, uint64_t len, int last)
1008265634Smav{
1009265634Smav	struct bio *bio;
1010265634Smav	struct ctl_be_block_devdata *dev_data;
1011265634Smav	uint64_t maxlen;
1012265634Smav
1013265634Smav	dev_data = &be_lun->backend.dev;
1014265634Smav	maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
1015265634Smav	while (len > 0) {
1016265634Smav		bio = g_alloc_bio();
1017265634Smav		bio->bio_cmd	    = BIO_DELETE;
1018265634Smav		bio->bio_dev	    = dev_data->cdev;
1019265634Smav		bio->bio_offset	    = off;
1020265634Smav		bio->bio_length	    = MIN(len, maxlen);
1021265634Smav		bio->bio_data	    = 0;
1022265634Smav		bio->bio_done	    = ctl_be_block_biodone;
1023265634Smav		bio->bio_caller1    = beio;
1024265634Smav		bio->bio_pblkno     = off / be_lun->blocksize;
1025265634Smav
1026265634Smav		off += bio->bio_length;
1027265634Smav		len -= bio->bio_length;
1028265634Smav
1029268549Smav		mtx_lock(&be_lun->io_lock);
1030265634Smav		beio->num_bios_sent++;
1031265634Smav		if (last && len == 0)
1032265634Smav			beio->send_complete = 1;
1033268549Smav		mtx_unlock(&be_lun->io_lock);
1034265634Smav
1035265634Smav		(*dev_data->csw->d_strategy)(bio);
1036265634Smav	}
1037265634Smav}
1038265634Smav
1039265634Smavstatic void
1040265634Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1041265634Smav		       struct ctl_be_block_io *beio)
1042265634Smav{
1043265634Smav	union ctl_io *io;
1044265634Smav	struct ctl_be_block_devdata *dev_data;
1045268149Smav	struct ctl_ptr_len_flags *ptrlen;
1046265634Smav	struct scsi_unmap_desc *buf, *end;
1047265634Smav	uint64_t len;
1048265634Smav
1049265634Smav	dev_data = &be_lun->backend.dev;
1050265634Smav	io = beio->io;
1051265634Smav
1052265634Smav	DPRINTF("entered\n");
1053265634Smav
1054265634Smav	binuptime(&beio->ds_t0);
1055268549Smav	mtx_lock(&be_lun->io_lock);
1056265634Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1057268549Smav	mtx_unlock(&be_lun->io_lock);
1058265634Smav
1059265634Smav	if (beio->io_offset == -1) {
1060265634Smav		beio->io_len = 0;
1061268149Smav		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1062268149Smav		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1063268149Smav		end = buf + ptrlen->len / sizeof(*buf);
1064265634Smav		for (; buf < end; buf++) {
1065265634Smav			len = (uint64_t)scsi_4btoul(buf->length) *
1066265634Smav			    be_lun->blocksize;
1067265634Smav			beio->io_len += len;
1068265634Smav			ctl_be_block_unmap_dev_range(be_lun, beio,
1069265634Smav			    scsi_8btou64(buf->lba) * be_lun->blocksize, len,
1070265634Smav			    (end - buf < 2) ? TRUE : FALSE);
1071265634Smav		}
1072265634Smav	} else
1073265634Smav		ctl_be_block_unmap_dev_range(be_lun, beio,
1074265634Smav		    beio->io_offset, beio->io_len, TRUE);
1075265634Smav}
1076265634Smav
1077265634Smavstatic void
1078229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1079229997Sken			  struct ctl_be_block_io *beio)
1080229997Sken{
1081268549Smav	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1082229997Sken	int i;
1083229997Sken	struct bio *bio;
1084229997Sken	struct ctl_be_block_devdata *dev_data;
1085229997Sken	off_t cur_offset;
1086229997Sken	int max_iosize;
1087229997Sken
1088229997Sken	DPRINTF("entered\n");
1089229997Sken
1090229997Sken	dev_data = &be_lun->backend.dev;
1091229997Sken
1092229997Sken	/*
1093229997Sken	 * We have to limit our I/O size to the maximum supported by the
1094229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
1095229997Sken	 * set it properly, use DFLTPHYS.
1096229997Sken	 */
1097229997Sken	max_iosize = dev_data->cdev->si_iosize_max;
1098229997Sken	if (max_iosize < PAGE_SIZE)
1099229997Sken		max_iosize = DFLTPHYS;
1100229997Sken
1101229997Sken	cur_offset = beio->io_offset;
1102229997Sken	for (i = 0; i < beio->num_segs; i++) {
1103229997Sken		size_t cur_size;
1104229997Sken		uint8_t *cur_ptr;
1105229997Sken
1106229997Sken		cur_size = beio->sg_segs[i].len;
1107229997Sken		cur_ptr = beio->sg_segs[i].addr;
1108229997Sken
1109229997Sken		while (cur_size > 0) {
1110229997Sken			/* This can't fail, it's a blocking allocation. */
1111229997Sken			bio = g_alloc_bio();
1112229997Sken
1113229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1114229997Sken
1115229997Sken			bio->bio_cmd = beio->bio_cmd;
1116229997Sken			bio->bio_dev = dev_data->cdev;
1117229997Sken			bio->bio_caller1 = beio;
1118229997Sken			bio->bio_length = min(cur_size, max_iosize);
1119229997Sken			bio->bio_offset = cur_offset;
1120229997Sken			bio->bio_data = cur_ptr;
1121229997Sken			bio->bio_done = ctl_be_block_biodone;
1122229997Sken			bio->bio_pblkno = cur_offset / be_lun->blocksize;
1123229997Sken
1124229997Sken			cur_offset += bio->bio_length;
1125229997Sken			cur_ptr += bio->bio_length;
1126229997Sken			cur_size -= bio->bio_length;
1127229997Sken
1128268549Smav			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1129229997Sken			beio->num_bios_sent++;
1130229997Sken		}
1131229997Sken	}
1132268549Smav	binuptime(&beio->ds_t0);
1133268549Smav	mtx_lock(&be_lun->io_lock);
1134268549Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1135268549Smav	beio->send_complete = 1;
1136268549Smav	mtx_unlock(&be_lun->io_lock);
1137268549Smav
1138268549Smav	/*
1139268549Smav	 * Fire off all allocated requests!
1140268549Smav	 */
1141268549Smav	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1142268549Smav		TAILQ_REMOVE(&queue, bio, bio_queue);
1143268549Smav		(*dev_data->csw->d_strategy)(bio);
1144268549Smav	}
1145229997Sken}
1146229997Sken
1147274732Smavstatic uint64_t
1148274732Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1149274732Smav{
1150274732Smav	struct ctl_be_block_devdata	*dev_data = &be_lun->backend.dev;
1151274732Smav	struct diocgattr_arg	arg;
1152274732Smav	int			error;
1153274732Smav
1154274732Smav	if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL)
1155274732Smav		return (UINT64_MAX);
1156274732Smav	strlcpy(arg.name, attrname, sizeof(arg.name));
1157274732Smav	arg.len = sizeof(arg.value.off);
1158274732Smav	error = dev_data->csw->d_ioctl(dev_data->cdev,
1159274732Smav	    DIOCGATTR, (caddr_t)&arg, FREAD, curthread);
1160274732Smav	if (error != 0)
1161274732Smav		return (UINT64_MAX);
1162274732Smav	return (arg.value.off);
1163274732Smav}
1164274732Smav
1165229997Skenstatic void
1166265634Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1167265634Smav{
1168265634Smav	union ctl_io *io;
1169265634Smav
1170265634Smav	io = beio->io;
1171265634Smav	ctl_free_beio(beio);
1172268261Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1173268261Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1174268261Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1175265634Smav		ctl_config_write_done(io);
1176265634Smav		return;
1177265634Smav	}
1178265634Smav
1179265634Smav	ctl_be_block_config_write(io);
1180265634Smav}
1181265634Smav
1182265634Smavstatic void
1183265634Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1184265634Smav			    union ctl_io *io)
1185265634Smav{
1186265634Smav	struct ctl_be_block_io *beio;
1187265634Smav	struct ctl_be_block_softc *softc;
1188268149Smav	struct ctl_lba_len_flags *lbalen;
1189265634Smav	uint64_t len_left, lba;
1190265634Smav	int i, seglen;
1191265634Smav	uint8_t *buf, *end;
1192265634Smav
1193265634Smav	DPRINTF("entered\n");
1194265634Smav
1195268150Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1196265634Smav	softc = be_lun->softc;
1197268151Smav	lbalen = ARGS(beio->io);
1198265634Smav
1199272632Smav	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1200270108Smav	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1201265634Smav		ctl_free_beio(beio);
1202265634Smav		ctl_set_invalid_field(&io->scsiio,
1203265634Smav				      /*sks_valid*/ 1,
1204265634Smav				      /*command*/ 1,
1205265634Smav				      /*field*/ 1,
1206265634Smav				      /*bit_valid*/ 0,
1207265634Smav				      /*bit*/ 0);
1208265634Smav		ctl_config_write_done(io);
1209265634Smav		return;
1210265634Smav	}
1211265634Smav
1212265634Smav	switch (io->scsiio.tag_type) {
1213265634Smav	case CTL_TAG_ORDERED:
1214265634Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1215265634Smav		break;
1216265634Smav	case CTL_TAG_HEAD_OF_QUEUE:
1217265634Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1218265634Smav		break;
1219265634Smav	case CTL_TAG_UNTAGGED:
1220265634Smav	case CTL_TAG_SIMPLE:
1221265634Smav	case CTL_TAG_ACA:
1222265634Smav	default:
1223265634Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1224265634Smav		break;
1225265634Smav	}
1226265634Smav
1227270108Smav	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1228268149Smav		beio->io_offset = lbalen->lba * be_lun->blocksize;
1229268149Smav		beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1230265634Smav		beio->bio_cmd = BIO_DELETE;
1231265634Smav		beio->ds_trans_type = DEVSTAT_FREE;
1232265634Smav
1233265634Smav		be_lun->unmap(be_lun, beio);
1234265634Smav		return;
1235265634Smav	}
1236265634Smav
1237265634Smav	beio->bio_cmd = BIO_WRITE;
1238265634Smav	beio->ds_trans_type = DEVSTAT_WRITE;
1239265634Smav
1240265634Smav	DPRINTF("WRITE SAME at LBA %jx len %u\n",
1241268149Smav	       (uintmax_t)lbalen->lba, lbalen->len);
1242265634Smav
1243268149Smav	len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1244265634Smav	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1245265634Smav
1246265634Smav		/*
1247265634Smav		 * Setup the S/G entry for this chunk.
1248265634Smav		 */
1249265642Smav		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1250265634Smav		seglen -= seglen % be_lun->blocksize;
1251265634Smav		beio->sg_segs[i].len = seglen;
1252265634Smav		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1253265634Smav
1254265634Smav		DPRINTF("segment %d addr %p len %zd\n", i,
1255265634Smav			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1256265634Smav
1257265634Smav		beio->num_segs++;
1258265634Smav		len_left -= seglen;
1259265634Smav
1260265634Smav		buf = beio->sg_segs[i].addr;
1261265634Smav		end = buf + seglen;
1262265634Smav		for (; buf < end; buf += be_lun->blocksize) {
1263265634Smav			memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1264268149Smav			if (lbalen->flags & SWS_LBDATA)
1265268149Smav				scsi_ulto4b(lbalen->lba + lba, buf);
1266265634Smav			lba++;
1267265634Smav		}
1268265634Smav	}
1269265634Smav
1270268149Smav	beio->io_offset = lbalen->lba * be_lun->blocksize;
1271265634Smav	beio->io_len = lba * be_lun->blocksize;
1272265634Smav
1273265634Smav	/* We can not do all in one run. Correct and schedule rerun. */
1274265634Smav	if (len_left > 0) {
1275268149Smav		lbalen->lba += lba;
1276268149Smav		lbalen->len -= lba;
1277265634Smav		beio->beio_cont = ctl_be_block_cw_done_ws;
1278265634Smav	}
1279265634Smav
1280265634Smav	be_lun->dispatch(be_lun, beio);
1281265634Smav}
1282265634Smav
1283265634Smavstatic void
1284265634Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1285265634Smav			    union ctl_io *io)
1286265634Smav{
1287265634Smav	struct ctl_be_block_io *beio;
1288265634Smav	struct ctl_be_block_softc *softc;
1289268149Smav	struct ctl_ptr_len_flags *ptrlen;
1290265634Smav
1291265634Smav	DPRINTF("entered\n");
1292265634Smav
1293268150Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1294265634Smav	softc = be_lun->softc;
1295268149Smav	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1296265634Smav
1297270108Smav	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1298265634Smav		ctl_free_beio(beio);
1299265634Smav		ctl_set_invalid_field(&io->scsiio,
1300265634Smav				      /*sks_valid*/ 0,
1301265634Smav				      /*command*/ 1,
1302265634Smav				      /*field*/ 0,
1303265634Smav				      /*bit_valid*/ 0,
1304265634Smav				      /*bit*/ 0);
1305265634Smav		ctl_config_write_done(io);
1306265634Smav		return;
1307265634Smav	}
1308265634Smav
1309265634Smav	switch (io->scsiio.tag_type) {
1310265634Smav	case CTL_TAG_ORDERED:
1311265634Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1312265634Smav		break;
1313265634Smav	case CTL_TAG_HEAD_OF_QUEUE:
1314265634Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1315265634Smav		break;
1316265634Smav	case CTL_TAG_UNTAGGED:
1317265634Smav	case CTL_TAG_SIMPLE:
1318265634Smav	case CTL_TAG_ACA:
1319265634Smav	default:
1320265634Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1321265634Smav		break;
1322265634Smav	}
1323265634Smav
1324265634Smav	beio->io_len = 0;
1325265634Smav	beio->io_offset = -1;
1326265634Smav
1327265634Smav	beio->bio_cmd = BIO_DELETE;
1328265634Smav	beio->ds_trans_type = DEVSTAT_FREE;
1329265634Smav
1330268149Smav	DPRINTF("UNMAP\n");
1331265634Smav
1332265634Smav	be_lun->unmap(be_lun, beio);
1333265634Smav}
1334265634Smav
1335265634Smavstatic void
1336275892Smavctl_be_block_cr_done(struct ctl_be_block_io *beio)
1337275892Smav{
1338275892Smav	union ctl_io *io;
1339275892Smav
1340275892Smav	io = beio->io;
1341275892Smav	ctl_free_beio(beio);
1342275892Smav	ctl_config_read_done(io);
1343275892Smav}
1344275892Smav
1345275892Smavstatic void
1346275892Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1347275892Smav			 union ctl_io *io)
1348275892Smav{
1349275892Smav	struct ctl_be_block_io *beio;
1350275892Smav	struct ctl_be_block_softc *softc;
1351275892Smav
1352275892Smav	DPRINTF("entered\n");
1353275892Smav
1354275892Smav	softc = be_lun->softc;
1355275892Smav	beio = ctl_alloc_beio(softc);
1356275892Smav	beio->io = io;
1357275892Smav	beio->lun = be_lun;
1358275892Smav	beio->beio_cont = ctl_be_block_cr_done;
1359275892Smav	PRIV(io)->ptr = (void *)beio;
1360275892Smav
1361275892Smav	switch (io->scsiio.cdb[0]) {
1362275892Smav	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1363275892Smav		beio->bio_cmd = -1;
1364275892Smav		beio->ds_trans_type = DEVSTAT_NO_DATA;
1365275892Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1366275892Smav		beio->io_len = 0;
1367275892Smav		if (be_lun->get_lba_status)
1368275892Smav			be_lun->get_lba_status(be_lun, beio);
1369275892Smav		else
1370275892Smav			ctl_be_block_cr_done(beio);
1371275892Smav		break;
1372275892Smav	default:
1373275892Smav		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1374275892Smav		break;
1375275892Smav	}
1376275892Smav}
1377275892Smav
1378275892Smavstatic void
1379265634Smavctl_be_block_cw_done(struct ctl_be_block_io *beio)
1380265634Smav{
1381265634Smav	union ctl_io *io;
1382265634Smav
1383265634Smav	io = beio->io;
1384265634Smav	ctl_free_beio(beio);
1385265634Smav	ctl_config_write_done(io);
1386265634Smav}
1387265634Smav
1388265634Smavstatic void
1389229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1390229997Sken			 union ctl_io *io)
1391229997Sken{
1392229997Sken	struct ctl_be_block_io *beio;
1393229997Sken	struct ctl_be_block_softc *softc;
1394229997Sken
1395229997Sken	DPRINTF("entered\n");
1396229997Sken
1397229997Sken	softc = be_lun->softc;
1398229997Sken	beio = ctl_alloc_beio(softc);
1399229997Sken	beio->io = io;
1400229997Sken	beio->lun = be_lun;
1401265634Smav	beio->beio_cont = ctl_be_block_cw_done;
1402268150Smav	PRIV(io)->ptr = (void *)beio;
1403229997Sken
1404229997Sken	switch (io->scsiio.cdb[0]) {
1405229997Sken	case SYNCHRONIZE_CACHE:
1406229997Sken	case SYNCHRONIZE_CACHE_16:
1407249194Strasz		beio->bio_cmd = BIO_FLUSH;
1408229997Sken		beio->ds_trans_type = DEVSTAT_NO_DATA;
1409229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1410229997Sken		beio->io_len = 0;
1411229997Sken		be_lun->lun_flush(be_lun, beio);
1412229997Sken		break;
1413265634Smav	case WRITE_SAME_10:
1414265634Smav	case WRITE_SAME_16:
1415265634Smav		ctl_be_block_cw_dispatch_ws(be_lun, io);
1416265634Smav		break;
1417265634Smav	case UNMAP:
1418265634Smav		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1419265634Smav		break;
1420229997Sken	default:
1421229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1422229997Sken		break;
1423229997Sken	}
1424229997Sken}
1425229997Sken
1426260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1427260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1428260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1429260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1430229997Sken
1431229997Skenstatic void
1432265642Smavctl_be_block_next(struct ctl_be_block_io *beio)
1433265642Smav{
1434265642Smav	struct ctl_be_block_lun *be_lun;
1435265642Smav	union ctl_io *io;
1436265642Smav
1437265642Smav	io = beio->io;
1438265642Smav	be_lun = beio->lun;
1439265642Smav	ctl_free_beio(beio);
1440268261Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1441268261Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1442268261Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1443268151Smav		ctl_data_submit_done(io);
1444265642Smav		return;
1445265642Smav	}
1446265642Smav
1447265642Smav	io->io_hdr.status &= ~CTL_STATUS_MASK;
1448265642Smav	io->io_hdr.status |= CTL_STATUS_NONE;
1449265642Smav
1450268549Smav	mtx_lock(&be_lun->queue_lock);
1451265642Smav	/*
1452265642Smav	 * XXX KDM make sure that links is okay to use at this point.
1453265642Smav	 * Otherwise, we either need to add another field to ctl_io_hdr,
1454265642Smav	 * or deal with resource allocation here.
1455265642Smav	 */
1456265642Smav	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1457268549Smav	mtx_unlock(&be_lun->queue_lock);
1458265642Smav
1459265642Smav	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1460265642Smav}
1461265642Smav
1462265642Smavstatic void
1463229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1464229997Sken			   union ctl_io *io)
1465229997Sken{
1466229997Sken	struct ctl_be_block_io *beio;
1467229997Sken	struct ctl_be_block_softc *softc;
1468268151Smav	struct ctl_lba_len_flags *lbalen;
1469268150Smav	struct ctl_ptr_len_flags *bptrlen;
1470268150Smav	uint64_t len_left, lbas;
1471229997Sken	int i;
1472229997Sken
1473229997Sken	softc = be_lun->softc;
1474229997Sken
1475229997Sken	DPRINTF("entered\n");
1476229997Sken
1477268151Smav	lbalen = ARGS(io);
1478268151Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1479268151Smav		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1480268151Smav	} else {
1481229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1482229997Sken	}
1483229997Sken
1484229997Sken	beio = ctl_alloc_beio(softc);
1485229997Sken	beio->io = io;
1486229997Sken	beio->lun = be_lun;
1487268150Smav	bptrlen = PRIV(io);
1488268150Smav	bptrlen->ptr = (void *)beio;
1489229997Sken
1490229997Sken	switch (io->scsiio.tag_type) {
1491229997Sken	case CTL_TAG_ORDERED:
1492229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1493229997Sken		break;
1494229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1495229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1496229997Sken		break;
1497229997Sken	case CTL_TAG_UNTAGGED:
1498229997Sken	case CTL_TAG_SIMPLE:
1499229997Sken	case CTL_TAG_ACA:
1500229997Sken	default:
1501229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1502229997Sken		break;
1503229997Sken	}
1504229997Sken
1505268151Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1506268151Smav		beio->bio_cmd = BIO_WRITE;
1507268151Smav		beio->ds_trans_type = DEVSTAT_WRITE;
1508268151Smav	} else {
1509229997Sken		beio->bio_cmd = BIO_READ;
1510229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1511229997Sken	}
1512229997Sken
1513265642Smav	DPRINTF("%s at LBA %jx len %u @%ju\n",
1514229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1515268150Smav	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1516268151Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1517268151Smav		lbas = CTLBLK_HALF_IO_SIZE;
1518268151Smav	else
1519268151Smav		lbas = CTLBLK_MAX_IO_SIZE;
1520268151Smav	lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1521268150Smav	beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1522268150Smav	beio->io_len = lbas * be_lun->blocksize;
1523268150Smav	bptrlen->len += lbas;
1524229997Sken
1525265642Smav	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1526265642Smav		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1527265642Smav		    i, CTLBLK_MAX_SEGS));
1528229997Sken
1529229997Sken		/*
1530229997Sken		 * Setup the S/G entry for this chunk.
1531229997Sken		 */
1532265642Smav		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1533229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1534229997Sken
1535229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1536229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1537229997Sken
1538268151Smav		/* Set up second segment for compare operation. */
1539268151Smav		if (lbalen->flags & CTL_LLF_COMPARE) {
1540268151Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1541268151Smav			    beio->sg_segs[i].len;
1542268151Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1543268151Smav			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
1544268151Smav		}
1545268151Smav
1546229997Sken		beio->num_segs++;
1547229997Sken		len_left -= beio->sg_segs[i].len;
1548229997Sken	}
1549268150Smav	if (bptrlen->len < lbalen->len)
1550265642Smav		beio->beio_cont = ctl_be_block_next;
1551265642Smav	io->scsiio.be_move_done = ctl_be_block_move_done;
1552268151Smav	/* For compare we have separate S/G lists for read and datamove. */
1553268151Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1554268151Smav		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1555268151Smav	else
1556268151Smav		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1557265642Smav	io->scsiio.kern_data_len = beio->io_len;
1558265642Smav	io->scsiio.kern_data_resid = 0;
1559265642Smav	io->scsiio.kern_sg_entries = beio->num_segs;
1560265642Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1561229997Sken
1562229997Sken	/*
1563229997Sken	 * For the read case, we need to read the data into our buffers and
1564229997Sken	 * then we can send it back to the user.  For the write case, we
1565229997Sken	 * need to get the data from the user first.
1566229997Sken	 */
1567229997Sken	if (beio->bio_cmd == BIO_READ) {
1568229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1569229997Sken		be_lun->dispatch(be_lun, beio);
1570229997Sken	} else {
1571229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1572229997Sken#ifdef CTL_TIME_IO
1573229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1574229997Sken#endif
1575229997Sken		ctl_datamove(io);
1576229997Sken	}
1577229997Sken}
1578229997Sken
1579229997Skenstatic void
1580229997Skenctl_be_block_worker(void *context, int pending)
1581229997Sken{
1582229997Sken	struct ctl_be_block_lun *be_lun;
1583229997Sken	struct ctl_be_block_softc *softc;
1584229997Sken	union ctl_io *io;
1585229997Sken
1586229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1587229997Sken	softc = be_lun->softc;
1588229997Sken
1589229997Sken	DPRINTF("entered\n");
1590229997Sken
1591268549Smav	mtx_lock(&be_lun->queue_lock);
1592229997Sken	for (;;) {
1593229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1594229997Sken		if (io != NULL) {
1595229997Sken			struct ctl_be_block_io *beio;
1596229997Sken
1597229997Sken			DPRINTF("datamove queue\n");
1598229997Sken
1599229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1600229997Sken				      ctl_io_hdr, links);
1601229997Sken
1602268549Smav			mtx_unlock(&be_lun->queue_lock);
1603229997Sken
1604268150Smav			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1605229997Sken
1606229997Sken			be_lun->dispatch(be_lun, beio);
1607229997Sken
1608268549Smav			mtx_lock(&be_lun->queue_lock);
1609229997Sken			continue;
1610229997Sken		}
1611229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1612229997Sken		if (io != NULL) {
1613229997Sken			DPRINTF("config write queue\n");
1614229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1615229997Sken				      ctl_io_hdr, links);
1616268549Smav			mtx_unlock(&be_lun->queue_lock);
1617229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1618268549Smav			mtx_lock(&be_lun->queue_lock);
1619229997Sken			continue;
1620229997Sken		}
1621275892Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1622275892Smav		if (io != NULL) {
1623275892Smav			DPRINTF("config read queue\n");
1624275892Smav			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1625275892Smav				      ctl_io_hdr, links);
1626275892Smav			mtx_unlock(&be_lun->queue_lock);
1627275892Smav			ctl_be_block_cr_dispatch(be_lun, io);
1628275892Smav			mtx_lock(&be_lun->queue_lock);
1629275892Smav			continue;
1630275892Smav		}
1631229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1632229997Sken		if (io != NULL) {
1633229997Sken			DPRINTF("input queue\n");
1634229997Sken
1635229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1636229997Sken				      ctl_io_hdr, links);
1637268549Smav			mtx_unlock(&be_lun->queue_lock);
1638229997Sken
1639229997Sken			/*
1640229997Sken			 * We must drop the lock, since this routine and
1641229997Sken			 * its children may sleep.
1642229997Sken			 */
1643229997Sken			ctl_be_block_dispatch(be_lun, io);
1644229997Sken
1645268549Smav			mtx_lock(&be_lun->queue_lock);
1646229997Sken			continue;
1647229997Sken		}
1648229997Sken
1649229997Sken		/*
1650229997Sken		 * If we get here, there is no work left in the queues, so
1651229997Sken		 * just break out and let the task queue go to sleep.
1652229997Sken		 */
1653229997Sken		break;
1654229997Sken	}
1655268549Smav	mtx_unlock(&be_lun->queue_lock);
1656229997Sken}
1657229997Sken
1658229997Sken/*
1659229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1660229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1661229997Sken * thread.
1662229997Sken */
1663229997Skenstatic int
1664229997Skenctl_be_block_submit(union ctl_io *io)
1665229997Sken{
1666229997Sken	struct ctl_be_block_lun *be_lun;
1667229997Sken	struct ctl_be_lun *ctl_be_lun;
1668229997Sken
1669229997Sken	DPRINTF("entered\n");
1670229997Sken
1671229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1672229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1673229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1674229997Sken
1675229997Sken	/*
1676229997Sken	 * Make sure we only get SCSI I/O.
1677229997Sken	 */
1678229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1679229997Sken		"%#x) encountered", io->io_hdr.io_type));
1680229997Sken
1681268150Smav	PRIV(io)->len = 0;
1682268150Smav
1683268549Smav	mtx_lock(&be_lun->queue_lock);
1684229997Sken	/*
1685229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1686229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1687229997Sken	 * or deal with resource allocation here.
1688229997Sken	 */
1689229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1690268549Smav	mtx_unlock(&be_lun->queue_lock);
1691229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1692229997Sken
1693268148Smav	return (CTL_RETVAL_COMPLETE);
1694229997Sken}
1695229997Sken
1696229997Skenstatic int
1697229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1698229997Sken			int flag, struct thread *td)
1699229997Sken{
1700229997Sken	struct ctl_be_block_softc *softc;
1701229997Sken	int error;
1702229997Sken
1703229997Sken	softc = &backend_block_softc;
1704229997Sken
1705229997Sken	error = 0;
1706229997Sken
1707229997Sken	switch (cmd) {
1708229997Sken	case CTL_LUN_REQ: {
1709229997Sken		struct ctl_lun_req *lun_req;
1710229997Sken
1711229997Sken		lun_req = (struct ctl_lun_req *)addr;
1712229997Sken
1713229997Sken		switch (lun_req->reqtype) {
1714229997Sken		case CTL_LUNREQ_CREATE:
1715229997Sken			error = ctl_be_block_create(softc, lun_req);
1716229997Sken			break;
1717229997Sken		case CTL_LUNREQ_RM:
1718229997Sken			error = ctl_be_block_rm(softc, lun_req);
1719229997Sken			break;
1720232604Strasz		case CTL_LUNREQ_MODIFY:
1721232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1722232604Strasz			break;
1723229997Sken		default:
1724229997Sken			lun_req->status = CTL_LUN_ERROR;
1725229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1726273315Smav				 "invalid LUN request type %d",
1727229997Sken				 lun_req->reqtype);
1728229997Sken			break;
1729229997Sken		}
1730229997Sken		break;
1731229997Sken	}
1732229997Sken	default:
1733229997Sken		error = ENOTTY;
1734229997Sken		break;
1735229997Sken	}
1736229997Sken
1737229997Sken	return (error);
1738229997Sken}
1739229997Sken
1740229997Skenstatic int
1741229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1742229997Sken{
1743229997Sken	struct ctl_be_block_filedata *file_data;
1744229997Sken	struct ctl_lun_create_params *params;
1745229997Sken	struct vattr		      vattr;
1746273322Smav	off_t			      pss;
1747229997Sken	int			      error;
1748229997Sken
1749229997Sken	error = 0;
1750229997Sken	file_data = &be_lun->backend.file;
1751273315Smav	params = &be_lun->params;
1752229997Sken
1753229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1754229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1755229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1756275892Smav	be_lun->get_lba_status = ctl_be_block_gls_file;
1757275893Smav	be_lun->getattr = ctl_be_block_getattr_file;
1758229997Sken
1759229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1760229997Sken	if (error != 0) {
1761229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1762229997Sken			 "error calling VOP_GETATTR() for file %s",
1763229997Sken			 be_lun->dev_path);
1764229997Sken		return (error);
1765229997Sken	}
1766229997Sken
1767229997Sken	/*
1768229997Sken	 * Verify that we have the ability to upgrade to exclusive
1769229997Sken	 * access on this file so we can trap errors at open instead
1770229997Sken	 * of reporting them during first access.
1771229997Sken	 */
1772229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1773229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1774229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1775229997Sken			error = EBADF;
1776229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1777229997Sken				 "error locking file %s", be_lun->dev_path);
1778229997Sken			return (error);
1779229997Sken		}
1780229997Sken	}
1781229997Sken
1782229997Sken
1783229997Sken	file_data->cred = crhold(curthread->td_ucred);
1784232604Strasz	if (params->lun_size_bytes != 0)
1785232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1786232604Strasz	else
1787232604Strasz		be_lun->size_bytes = vattr.va_size;
1788229997Sken	/*
1789229997Sken	 * We set the multi thread flag for file operations because all
1790229997Sken	 * filesystems (in theory) are capable of allowing multiple readers
1791229997Sken	 * of a file at once.  So we want to get the maximum possible
1792229997Sken	 * concurrency.
1793229997Sken	 */
1794229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1795229997Sken
1796229997Sken	/*
1797273322Smav	 * For files we can use any logical block size.  Prefer 512 bytes
1798273322Smav	 * for compatibility reasons.  If file's vattr.va_blocksize
1799273322Smav	 * (preferred I/O block size) is bigger and multiple to chosen
1800273322Smav	 * logical block size -- report it as physical block size.
1801229997Sken	 */
1802229997Sken	if (params->blocksize_bytes != 0)
1803229997Sken		be_lun->blocksize = params->blocksize_bytes;
1804229997Sken	else
1805229997Sken		be_lun->blocksize = 512;
1806273322Smav	pss = vattr.va_blocksize / be_lun->blocksize;
1807273322Smav	if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) &&
1808273322Smav	    ((pss & (pss - 1)) == 0)) {
1809273322Smav		be_lun->pblockexp = fls(pss) - 1;
1810273322Smav		be_lun->pblockoff = 0;
1811273322Smav	}
1812229997Sken
1813229997Sken	/*
1814229997Sken	 * Sanity check.  The media size has to be at least one
1815229997Sken	 * sector long.
1816229997Sken	 */
1817229997Sken	if (be_lun->size_bytes < be_lun->blocksize) {
1818229997Sken		error = EINVAL;
1819229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1820229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1821229997Sken			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1822229997Sken	}
1823229997Sken	return (error);
1824229997Sken}
1825229997Sken
1826229997Skenstatic int
1827229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1828229997Sken{
1829229997Sken	struct ctl_lun_create_params *params;
1830229997Sken	struct vattr		      vattr;
1831229997Sken	struct cdev		     *dev;
1832229997Sken	struct cdevsw		     *devsw;
1833229997Sken	int			      error;
1834264727Smav	off_t			      ps, pss, po, pos;
1835229997Sken
1836273315Smav	params = &be_lun->params;
1837229997Sken
1838229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1839229997Sken	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1840229997Sken	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1841229997Sken					     &be_lun->backend.dev.dev_ref);
1842229997Sken	if (be_lun->backend.dev.csw == NULL)
1843229997Sken		panic("Unable to retrieve device switch");
1844275892Smav	if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) {
1845269429Smav		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1846275892Smav		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1847275892Smav	} else
1848269429Smav		be_lun->dispatch = ctl_be_block_dispatch_dev;
1849269429Smav	be_lun->lun_flush = ctl_be_block_flush_dev;
1850269429Smav	be_lun->unmap = ctl_be_block_unmap_dev;
1851274732Smav	be_lun->getattr = ctl_be_block_getattr_dev;
1852229997Sken
1853229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1854229997Sken	if (error) {
1855229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1856273315Smav			 "error getting vnode attributes for device %s",
1857273315Smav			 be_lun->dev_path);
1858229997Sken		return (error);
1859229997Sken	}
1860229997Sken
1861229997Sken	dev = be_lun->vn->v_rdev;
1862229997Sken	devsw = dev->si_devsw;
1863229997Sken	if (!devsw->d_ioctl) {
1864229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1865273315Smav			 "no d_ioctl for device %s!",
1866229997Sken			 be_lun->dev_path);
1867229997Sken		return (ENODEV);
1868229997Sken	}
1869229997Sken
1870229997Sken	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1871229997Sken			       (caddr_t)&be_lun->blocksize, FREAD,
1872229997Sken			       curthread);
1873229997Sken	if (error) {
1874229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1875273315Smav			 "error %d returned for DIOCGSECTORSIZE ioctl "
1876273315Smav			 "on %s!", error, be_lun->dev_path);
1877229997Sken		return (error);
1878229997Sken	}
1879229997Sken
1880229997Sken	/*
1881229997Sken	 * If the user has asked for a blocksize that is greater than the
1882229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1883229997Sken	 * the user is asking for is an even multiple of the underlying
1884229997Sken	 * device's blocksize.
1885229997Sken	 */
1886229997Sken	if ((params->blocksize_bytes != 0)
1887229997Sken	 && (params->blocksize_bytes > be_lun->blocksize)) {
1888229997Sken		uint32_t bs_multiple, tmp_blocksize;
1889229997Sken
1890229997Sken		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1891229997Sken
1892229997Sken		tmp_blocksize = bs_multiple * be_lun->blocksize;
1893229997Sken
1894229997Sken		if (tmp_blocksize == params->blocksize_bytes) {
1895229997Sken			be_lun->blocksize = params->blocksize_bytes;
1896229997Sken		} else {
1897229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1898273315Smav				 "requested blocksize %u is not an even "
1899229997Sken				 "multiple of backing device blocksize %u",
1900273315Smav				 params->blocksize_bytes,
1901229997Sken				 be_lun->blocksize);
1902229997Sken			return (EINVAL);
1903229997Sken
1904229997Sken		}
1905229997Sken	} else if ((params->blocksize_bytes != 0)
1906229997Sken		&& (params->blocksize_bytes != be_lun->blocksize)) {
1907229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1908273315Smav			 "requested blocksize %u < backing device "
1909273315Smav			 "blocksize %u", params->blocksize_bytes,
1910229997Sken			 be_lun->blocksize);
1911229997Sken		return (EINVAL);
1912229997Sken	}
1913229997Sken
1914229997Sken	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1915229997Sken			       (caddr_t)&be_lun->size_bytes, FREAD,
1916229997Sken			       curthread);
1917229997Sken	if (error) {
1918229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1919273315Smav			 "error %d returned for DIOCGMEDIASIZE "
1920273315Smav			 " ioctl on %s!", error,
1921232604Strasz			 be_lun->dev_path);
1922229997Sken		return (error);
1923229997Sken	}
1924229997Sken
1925232604Strasz	if (params->lun_size_bytes != 0) {
1926232604Strasz		if (params->lun_size_bytes > be_lun->size_bytes) {
1927232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1928273315Smav				 "requested LUN size %ju > backing device "
1929273315Smav				 "size %ju",
1930232604Strasz				 (uintmax_t)params->lun_size_bytes,
1931232604Strasz				 (uintmax_t)be_lun->size_bytes);
1932232604Strasz			return (EINVAL);
1933232604Strasz		}
1934232604Strasz
1935232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1936232604Strasz	}
1937232604Strasz
1938264727Smav	error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1939264727Smav			       (caddr_t)&ps, FREAD, curthread);
1940264727Smav	if (error)
1941264727Smav		ps = po = 0;
1942264727Smav	else {
1943264727Smav		error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1944264727Smav				       (caddr_t)&po, FREAD, curthread);
1945264727Smav		if (error)
1946264727Smav			po = 0;
1947264727Smav	}
1948264727Smav	pss = ps / be_lun->blocksize;
1949264727Smav	pos = po / be_lun->blocksize;
1950264727Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1951264727Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1952264727Smav		be_lun->pblockexp = fls(pss) - 1;
1953264727Smav		be_lun->pblockoff = (pss - pos) % pss;
1954264727Smav	}
1955264727Smav
1956229997Sken	return (0);
1957229997Sken}
1958229997Sken
1959229997Skenstatic int
1960229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
1961229997Sken{
1962229997Sken	DROP_GIANT();
1963229997Sken	if (be_lun->vn) {
1964229997Sken		int flags = FREAD | FWRITE;
1965229997Sken
1966229997Sken		switch (be_lun->dev_type) {
1967229997Sken		case CTL_BE_BLOCK_DEV:
1968229997Sken			if (be_lun->backend.dev.csw) {
1969229997Sken				dev_relthread(be_lun->backend.dev.cdev,
1970229997Sken					      be_lun->backend.dev.dev_ref);
1971229997Sken				be_lun->backend.dev.csw  = NULL;
1972229997Sken				be_lun->backend.dev.cdev = NULL;
1973229997Sken			}
1974229997Sken			break;
1975229997Sken		case CTL_BE_BLOCK_FILE:
1976229997Sken			break;
1977229997Sken		case CTL_BE_BLOCK_NONE:
1978259304Strasz			break;
1979229997Sken		default:
1980229997Sken			panic("Unexpected backend type.");
1981229997Sken			break;
1982229997Sken		}
1983229997Sken
1984229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1985229997Sken		be_lun->vn = NULL;
1986229997Sken
1987229997Sken		switch (be_lun->dev_type) {
1988229997Sken		case CTL_BE_BLOCK_DEV:
1989229997Sken			break;
1990229997Sken		case CTL_BE_BLOCK_FILE:
1991229997Sken			if (be_lun->backend.file.cred != NULL) {
1992229997Sken				crfree(be_lun->backend.file.cred);
1993229997Sken				be_lun->backend.file.cred = NULL;
1994229997Sken			}
1995229997Sken			break;
1996229997Sken		case CTL_BE_BLOCK_NONE:
1997259304Strasz			break;
1998229997Sken		default:
1999229997Sken			panic("Unexpected backend type.");
2000229997Sken			break;
2001229997Sken		}
2002273315Smav		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2003229997Sken	}
2004229997Sken	PICKUP_GIANT();
2005229997Sken
2006229997Sken	return (0);
2007229997Sken}
2008229997Sken
2009229997Skenstatic int
2010229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
2011229997Sken		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2012229997Sken{
2013229997Sken	struct nameidata nd;
2014229997Sken	int		 flags;
2015229997Sken	int		 error;
2016229997Sken
2017229997Sken	/*
2018229997Sken	 * XXX KDM allow a read-only option?
2019229997Sken	 */
2020229997Sken	flags = FREAD | FWRITE;
2021229997Sken	error = 0;
2022229997Sken
2023229997Sken	if (rootvnode == NULL) {
2024229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2025273315Smav			 "Root filesystem is not mounted");
2026229997Sken		return (1);
2027229997Sken	}
2028229997Sken
2029229997Sken	if (!curthread->td_proc->p_fd->fd_cdir) {
2030229997Sken		curthread->td_proc->p_fd->fd_cdir = rootvnode;
2031229997Sken		VREF(rootvnode);
2032229997Sken	}
2033229997Sken	if (!curthread->td_proc->p_fd->fd_rdir) {
2034229997Sken		curthread->td_proc->p_fd->fd_rdir = rootvnode;
2035229997Sken		VREF(rootvnode);
2036229997Sken	}
2037229997Sken	if (!curthread->td_proc->p_fd->fd_jdir) {
2038229997Sken		curthread->td_proc->p_fd->fd_jdir = rootvnode;
2039229997Sken		VREF(rootvnode);
2040229997Sken	}
2041229997Sken
2042229997Sken again:
2043229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2044229997Sken	error = vn_open(&nd, &flags, 0, NULL);
2045229997Sken	if (error) {
2046229997Sken		/*
2047229997Sken		 * This is the only reasonable guess we can make as far as
2048229997Sken		 * path if the user doesn't give us a fully qualified path.
2049229997Sken		 * If they want to specify a file, they need to specify the
2050229997Sken		 * full path.
2051229997Sken		 */
2052229997Sken		if (be_lun->dev_path[0] != '/') {
2053229997Sken			char *dev_path = "/dev/";
2054229997Sken			char *dev_name;
2055229997Sken
2056229997Sken			/* Try adding device path at beginning of name */
2057229997Sken			dev_name = malloc(strlen(be_lun->dev_path)
2058229997Sken					+ strlen(dev_path) + 1,
2059229997Sken					  M_CTLBLK, M_WAITOK);
2060229997Sken			if (dev_name) {
2061229997Sken				sprintf(dev_name, "%s%s", dev_path,
2062229997Sken					be_lun->dev_path);
2063229997Sken				free(be_lun->dev_path, M_CTLBLK);
2064229997Sken				be_lun->dev_path = dev_name;
2065229997Sken				goto again;
2066229997Sken			}
2067229997Sken		}
2068229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2069273315Smav		    "error opening %s: %d", be_lun->dev_path, error);
2070229997Sken		return (error);
2071229997Sken	}
2072229997Sken
2073229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
2074229997Sken
2075229997Sken	be_lun->vn = nd.ni_vp;
2076229997Sken
2077229997Sken	/* We only support disks and files. */
2078229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
2079229997Sken		error = ctl_be_block_open_dev(be_lun, req);
2080229997Sken	} else if (be_lun->vn->v_type == VREG) {
2081229997Sken		error = ctl_be_block_open_file(be_lun, req);
2082229997Sken	} else {
2083229997Sken		error = EINVAL;
2084229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2085259304Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
2086229997Sken	}
2087229997Sken	VOP_UNLOCK(be_lun->vn, 0);
2088229997Sken
2089229997Sken	if (error != 0) {
2090229997Sken		ctl_be_block_close(be_lun);
2091229997Sken		return (error);
2092229997Sken	}
2093229997Sken
2094229997Sken	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
2095229997Sken	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
2096229997Sken
2097229997Sken	return (0);
2098229997Sken}
2099229997Sken
2100229997Skenstatic int
2101229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2102229997Sken{
2103229997Sken	struct ctl_be_block_lun *be_lun;
2104229997Sken	struct ctl_lun_create_params *params;
2105268143Smav	char num_thread_str[16];
2106229997Sken	char tmpstr[32];
2107268143Smav	char *value;
2108265634Smav	int retval, num_threads, unmap;
2109268143Smav	int tmp_num_threads;
2110229997Sken
2111229997Sken	params = &req->reqdata.create;
2112229997Sken	retval = 0;
2113273315Smav	req->status = CTL_LUN_OK;
2114229997Sken
2115229997Sken	num_threads = cbb_num_threads;
2116229997Sken
2117229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2118229997Sken
2119273315Smav	be_lun->params = req->reqdata.create;
2120229997Sken	be_lun->softc = softc;
2121229997Sken	STAILQ_INIT(&be_lun->input_queue);
2122275892Smav	STAILQ_INIT(&be_lun->config_read_queue);
2123229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
2124229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
2125229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
2126268549Smav	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
2127268549Smav	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
2128268678Smav	ctl_init_opts(&be_lun->ctl_be_lun.options,
2129268678Smav	    req->num_be_args, req->kern_be_args);
2130229997Sken
2131265642Smav	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
2132260476Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2133229997Sken
2134229997Sken	if (be_lun->lun_zone == NULL) {
2135229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2136273315Smav			 "error allocating UMA zone");
2137229997Sken		goto bailout_error;
2138229997Sken	}
2139229997Sken
2140229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2141229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
2142229997Sken	else
2143229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
2144229997Sken
2145229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
2146268678Smav		value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
2147268146Smav		if (value == NULL) {
2148229997Sken			snprintf(req->error_str, sizeof(req->error_str),
2149273315Smav				 "no file argument specified");
2150229997Sken			goto bailout_error;
2151229997Sken		}
2152268146Smav		be_lun->dev_path = strdup(value, M_CTLBLK);
2153273315Smav		be_lun->blocksize = 512;
2154273315Smav		be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
2155229997Sken
2156229997Sken		retval = ctl_be_block_open(softc, be_lun, req);
2157229997Sken		if (retval != 0) {
2158229997Sken			retval = 0;
2159273315Smav			req->status = CTL_LUN_WARNING;
2160229997Sken		}
2161229997Sken	} else {
2162229997Sken		/*
2163229997Sken		 * For processor devices, we don't have any size.
2164229997Sken		 */
2165229997Sken		be_lun->blocksize = 0;
2166264727Smav		be_lun->pblockexp = 0;
2167264727Smav		be_lun->pblockoff = 0;
2168229997Sken		be_lun->size_blocks = 0;
2169229997Sken		be_lun->size_bytes = 0;
2170229997Sken		be_lun->ctl_be_lun.maxlba = 0;
2171229997Sken
2172229997Sken		/*
2173229997Sken		 * Default to just 1 thread for processor devices.
2174229997Sken		 */
2175229997Sken		num_threads = 1;
2176229997Sken	}
2177229997Sken
2178229997Sken	/*
2179229997Sken	 * XXX This searching loop might be refactored to be combined with
2180229997Sken	 * the loop above,
2181229997Sken	 */
2182268678Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
2183268143Smav	if (value != NULL) {
2184268143Smav		tmp_num_threads = strtol(value, NULL, 0);
2185229997Sken
2186268143Smav		/*
2187268143Smav		 * We don't let the user specify less than one
2188268143Smav		 * thread, but hope he's clueful enough not to
2189268143Smav		 * specify 1000 threads.
2190268143Smav		 */
2191268143Smav		if (tmp_num_threads < 1) {
2192268143Smav			snprintf(req->error_str, sizeof(req->error_str),
2193273315Smav				 "invalid number of threads %s",
2194273315Smav				 num_thread_str);
2195268143Smav			goto bailout_error;
2196229997Sken		}
2197268143Smav		num_threads = tmp_num_threads;
2198229997Sken	}
2199274732Smav	unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol);
2200268678Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
2201274732Smav	if (value != NULL)
2202274732Smav		unmap = (strcmp(value, "on") == 0);
2203229997Sken
2204229997Sken	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2205229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2206273315Smav	if (be_lun->vn == NULL)
2207273315Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE;
2208265634Smav	if (unmap)
2209265634Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2210275895Smav	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2211275895Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_SERSEQ_READ;
2212229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
2213273315Smav	be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2214273315Smav	    0 : (be_lun->size_blocks - 1);
2215229997Sken	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2216264727Smav	be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2217264727Smav	be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2218273315Smav	if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2219273315Smav	    be_lun->blocksize != 0)
2220273315Smav		be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2221273315Smav		    be_lun->blocksize;
2222229997Sken	/* Tell the user the blocksize we ended up using */
2223273315Smav	params->lun_size_bytes = be_lun->size_bytes;
2224229997Sken	params->blocksize_bytes = be_lun->blocksize;
2225229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2226229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2227229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2228229997Sken	} else
2229229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
2230229997Sken
2231229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2232229997Sken	be_lun->ctl_be_lun.lun_config_status =
2233229997Sken		ctl_be_block_lun_config_status;
2234229997Sken	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2235229997Sken
2236229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2237229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2238229997Sken			 softc->num_luns);
2239229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2240229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2241229997Sken			sizeof(tmpstr)));
2242229997Sken
2243229997Sken		/* Tell the user what we used for a serial number */
2244229997Sken		strncpy((char *)params->serial_num, tmpstr,
2245229997Sken			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2246229997Sken	} else {
2247229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
2248229997Sken			params->serial_num,
2249229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2250229997Sken			sizeof(params->serial_num)));
2251229997Sken	}
2252229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2253229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2254229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2255229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2256229997Sken			sizeof(tmpstr)));
2257229997Sken
2258229997Sken		/* Tell the user what we used for a device ID */
2259229997Sken		strncpy((char *)params->device_id, tmpstr,
2260229997Sken			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2261229997Sken	} else {
2262229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
2263229997Sken			params->device_id,
2264229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2265229997Sken				sizeof(params->device_id)));
2266229997Sken	}
2267229997Sken
2268229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2269229997Sken
2270229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2271229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2272229997Sken
2273229997Sken	if (be_lun->io_taskqueue == NULL) {
2274229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2275273315Smav			 "unable to create taskqueue");
2276229997Sken		goto bailout_error;
2277229997Sken	}
2278229997Sken
2279229997Sken	/*
2280229997Sken	 * Note that we start the same number of threads by default for
2281229997Sken	 * both the file case and the block device case.  For the file
2282229997Sken	 * case, we need multiple threads to allow concurrency, because the
2283229997Sken	 * vnode interface is designed to be a blocking interface.  For the
2284229997Sken	 * block device case, ZFS zvols at least will block the caller's
2285229997Sken	 * context in many instances, and so we need multiple threads to
2286229997Sken	 * overcome that problem.  Other block devices don't need as many
2287229997Sken	 * threads, but they shouldn't cause too many problems.
2288229997Sken	 *
2289229997Sken	 * If the user wants to just have a single thread for a block
2290229997Sken	 * device, he can specify that when the LUN is created, or change
2291229997Sken	 * the tunable/sysctl to alter the default number of threads.
2292229997Sken	 */
2293229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2294229997Sken					 /*num threads*/num_threads,
2295229997Sken					 /*priority*/PWAIT,
2296229997Sken					 /*thread name*/
2297229997Sken					 "%s taskq", be_lun->lunname);
2298229997Sken
2299229997Sken	if (retval != 0)
2300229997Sken		goto bailout_error;
2301229997Sken
2302229997Sken	be_lun->num_threads = num_threads;
2303229997Sken
2304229997Sken	mtx_lock(&softc->lock);
2305229997Sken	softc->num_luns++;
2306229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2307229997Sken
2308229997Sken	mtx_unlock(&softc->lock);
2309229997Sken
2310229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
2311229997Sken	if (retval != 0) {
2312229997Sken		mtx_lock(&softc->lock);
2313229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2314229997Sken			      links);
2315229997Sken		softc->num_luns--;
2316229997Sken		mtx_unlock(&softc->lock);
2317229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2318273315Smav			 "ctl_add_lun() returned error %d, see dmesg for "
2319273315Smav			 "details", retval);
2320229997Sken		retval = 0;
2321229997Sken		goto bailout_error;
2322229997Sken	}
2323229997Sken
2324229997Sken	mtx_lock(&softc->lock);
2325229997Sken
2326229997Sken	/*
2327229997Sken	 * Tell the config_status routine that we're waiting so it won't
2328229997Sken	 * clean up the LUN in the event of an error.
2329229997Sken	 */
2330229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2331229997Sken
2332229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2333229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2334229997Sken		if (retval == EINTR)
2335229997Sken			break;
2336229997Sken	}
2337229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2338229997Sken
2339229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2340229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2341273315Smav			 "LUN configuration error, see dmesg for details");
2342229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2343229997Sken			      links);
2344229997Sken		softc->num_luns--;
2345229997Sken		mtx_unlock(&softc->lock);
2346229997Sken		goto bailout_error;
2347229997Sken	} else {
2348229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2349229997Sken	}
2350229997Sken
2351229997Sken	mtx_unlock(&softc->lock);
2352229997Sken
2353229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2354229997Sken					       be_lun->blocksize,
2355229997Sken					       DEVSTAT_ALL_SUPPORTED,
2356229997Sken					       be_lun->ctl_be_lun.lun_type
2357229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
2358229997Sken					       DEVSTAT_PRIORITY_OTHER);
2359229997Sken
2360229997Sken	return (retval);
2361229997Sken
2362229997Skenbailout_error:
2363229997Sken	req->status = CTL_LUN_ERROR;
2364229997Sken
2365267754Smav	if (be_lun->io_taskqueue != NULL)
2366267754Smav		taskqueue_free(be_lun->io_taskqueue);
2367229997Sken	ctl_be_block_close(be_lun);
2368267754Smav	if (be_lun->dev_path != NULL)
2369267754Smav		free(be_lun->dev_path, M_CTLBLK);
2370267754Smav	if (be_lun->lun_zone != NULL)
2371267754Smav		uma_zdestroy(be_lun->lun_zone);
2372268678Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2373268549Smav	mtx_destroy(&be_lun->queue_lock);
2374268549Smav	mtx_destroy(&be_lun->io_lock);
2375229997Sken	free(be_lun, M_CTLBLK);
2376229997Sken
2377229997Sken	return (retval);
2378229997Sken}
2379229997Sken
2380229997Skenstatic int
2381229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2382229997Sken{
2383229997Sken	struct ctl_lun_rm_params *params;
2384229997Sken	struct ctl_be_block_lun *be_lun;
2385229997Sken	int retval;
2386229997Sken
2387229997Sken	params = &req->reqdata.rm;
2388229997Sken
2389229997Sken	mtx_lock(&softc->lock);
2390229997Sken
2391229997Sken	be_lun = NULL;
2392229997Sken
2393229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2394229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2395229997Sken			break;
2396229997Sken	}
2397229997Sken	mtx_unlock(&softc->lock);
2398229997Sken
2399229997Sken	if (be_lun == NULL) {
2400229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2401273315Smav			 "LUN %u is not managed by the block backend",
2402273315Smav			 params->lun_id);
2403229997Sken		goto bailout_error;
2404229997Sken	}
2405229997Sken
2406229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2407229997Sken
2408229997Sken	if (retval != 0) {
2409229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2410273315Smav			 "error %d returned from ctl_disable_lun() for "
2411273315Smav			 "LUN %d", retval, params->lun_id);
2412229997Sken		goto bailout_error;
2413229997Sken
2414229997Sken	}
2415229997Sken
2416229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2417229997Sken	if (retval != 0) {
2418229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2419273315Smav			 "error %d returned from ctl_invalidate_lun() for "
2420273315Smav			 "LUN %d", retval, params->lun_id);
2421229997Sken		goto bailout_error;
2422229997Sken	}
2423229997Sken
2424229997Sken	mtx_lock(&softc->lock);
2425229997Sken
2426229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2427229997Sken
2428229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2429229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2430229997Sken                if (retval == EINTR)
2431229997Sken                        break;
2432229997Sken        }
2433229997Sken
2434229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2435229997Sken
2436229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2437229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2438273315Smav			 "interrupted waiting for LUN to be freed");
2439229997Sken		mtx_unlock(&softc->lock);
2440229997Sken		goto bailout_error;
2441229997Sken	}
2442229997Sken
2443229997Sken	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2444229997Sken
2445229997Sken	softc->num_luns--;
2446229997Sken	mtx_unlock(&softc->lock);
2447229997Sken
2448229997Sken	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2449229997Sken
2450229997Sken	taskqueue_free(be_lun->io_taskqueue);
2451229997Sken
2452229997Sken	ctl_be_block_close(be_lun);
2453229997Sken
2454229997Sken	if (be_lun->disk_stats != NULL)
2455229997Sken		devstat_remove_entry(be_lun->disk_stats);
2456229997Sken
2457229997Sken	uma_zdestroy(be_lun->lun_zone);
2458229997Sken
2459268678Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2460229997Sken	free(be_lun->dev_path, M_CTLBLK);
2461268549Smav	mtx_destroy(&be_lun->queue_lock);
2462268549Smav	mtx_destroy(&be_lun->io_lock);
2463229997Sken	free(be_lun, M_CTLBLK);
2464229997Sken
2465229997Sken	req->status = CTL_LUN_OK;
2466229997Sken
2467229997Sken	return (0);
2468229997Sken
2469229997Skenbailout_error:
2470229997Sken
2471229997Sken	req->status = CTL_LUN_ERROR;
2472229997Sken
2473229997Sken	return (0);
2474229997Sken}
2475229997Sken
2476232604Straszstatic int
2477232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2478232604Strasz			 struct ctl_lun_req *req)
2479232604Strasz{
2480232604Strasz	struct vattr vattr;
2481232604Strasz	int error;
2482273315Smav	struct ctl_lun_create_params *params = &be_lun->params;
2483232604Strasz
2484232604Strasz	if (params->lun_size_bytes != 0) {
2485232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2486232604Strasz	} else  {
2487271928Smav		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2488232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2489271928Smav		VOP_UNLOCK(be_lun->vn, 0);
2490232604Strasz		if (error != 0) {
2491232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2492232604Strasz				 "error calling VOP_GETATTR() for file %s",
2493232604Strasz				 be_lun->dev_path);
2494232604Strasz			return (error);
2495232604Strasz		}
2496232604Strasz
2497232604Strasz		be_lun->size_bytes = vattr.va_size;
2498232604Strasz	}
2499232604Strasz
2500232604Strasz	return (0);
2501232604Strasz}
2502232604Strasz
2503232604Straszstatic int
2504232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2505232604Strasz			struct ctl_lun_req *req)
2506232604Strasz{
2507271928Smav	struct ctl_be_block_devdata *dev_data;
2508232604Strasz	int error;
2509273315Smav	struct ctl_lun_create_params *params = &be_lun->params;
2510232604Strasz	uint64_t size_bytes;
2511232604Strasz
2512271928Smav	dev_data = &be_lun->backend.dev;
2513271928Smav	if (!dev_data->csw->d_ioctl) {
2514232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2515273315Smav			 "no d_ioctl for device %s!", be_lun->dev_path);
2516232604Strasz		return (ENODEV);
2517232604Strasz	}
2518232604Strasz
2519271928Smav	error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2520232604Strasz			       (caddr_t)&size_bytes, FREAD,
2521232604Strasz			       curthread);
2522232604Strasz	if (error) {
2523232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2524273315Smav			 "error %d returned for DIOCGMEDIASIZE ioctl "
2525273315Smav			 "on %s!", error, be_lun->dev_path);
2526232604Strasz		return (error);
2527232604Strasz	}
2528232604Strasz
2529232604Strasz	if (params->lun_size_bytes != 0) {
2530232604Strasz		if (params->lun_size_bytes > size_bytes) {
2531232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2532273315Smav				 "requested LUN size %ju > backing device "
2533273315Smav				 "size %ju",
2534232604Strasz				 (uintmax_t)params->lun_size_bytes,
2535232604Strasz				 (uintmax_t)size_bytes);
2536232604Strasz			return (EINVAL);
2537232604Strasz		}
2538232604Strasz
2539232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2540232604Strasz	} else {
2541232604Strasz		be_lun->size_bytes = size_bytes;
2542232604Strasz	}
2543232604Strasz
2544232604Strasz	return (0);
2545232604Strasz}
2546232604Strasz
2547232604Straszstatic int
2548232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2549232604Strasz{
2550232604Strasz	struct ctl_lun_modify_params *params;
2551232604Strasz	struct ctl_be_block_lun *be_lun;
2552271928Smav	uint64_t oldsize;
2553241896Skib	int error;
2554232604Strasz
2555232604Strasz	params = &req->reqdata.modify;
2556232604Strasz
2557232604Strasz	mtx_lock(&softc->lock);
2558232604Strasz	be_lun = NULL;
2559232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2560232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2561232604Strasz			break;
2562232604Strasz	}
2563232604Strasz	mtx_unlock(&softc->lock);
2564232604Strasz
2565232604Strasz	if (be_lun == NULL) {
2566232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2567273315Smav			 "LUN %u is not managed by the block backend",
2568273315Smav			 params->lun_id);
2569232604Strasz		goto bailout_error;
2570232604Strasz	}
2571232604Strasz
2572273315Smav	be_lun->params.lun_size_bytes = params->lun_size_bytes;
2573232604Strasz
2574274387Smav	oldsize = be_lun->size_bytes;
2575273315Smav	if (be_lun->vn == NULL)
2576273315Smav		error = ctl_be_block_open(softc, be_lun, req);
2577273315Smav	else if (be_lun->vn->v_type == VREG)
2578232604Strasz		error = ctl_be_block_modify_file(be_lun, req);
2579232604Strasz	else
2580232604Strasz		error = ctl_be_block_modify_dev(be_lun, req);
2581232604Strasz
2582274387Smav	if (error == 0 && be_lun->size_bytes != oldsize) {
2583271928Smav		be_lun->size_blocks = be_lun->size_bytes >>
2584271928Smav		    be_lun->blocksize_shift;
2585232604Strasz
2586271928Smav		/*
2587271928Smav		 * The maximum LBA is the size - 1.
2588271928Smav		 *
2589271928Smav		 * XXX: Note that this field is being updated without locking,
2590271928Smav		 * 	which might cause problems on 32-bit architectures.
2591271928Smav		 */
2592273315Smav		be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2593273315Smav		    0 : (be_lun->size_blocks - 1);
2594273315Smav		be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2595273315Smav		be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2596273315Smav		be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2597273315Smav		if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2598273315Smav		    be_lun->blocksize != 0)
2599273315Smav			be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2600273315Smav			    be_lun->blocksize;
2601271928Smav		ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2602273315Smav		if (oldsize == 0 && be_lun->size_blocks != 0)
2603273315Smav			ctl_lun_online(&be_lun->ctl_be_lun);
2604271928Smav	}
2605232604Strasz
2606232604Strasz	/* Tell the user the exact size we ended up using */
2607232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2608232604Strasz
2609273315Smav	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2610232604Strasz
2611232604Strasz	return (0);
2612232604Strasz
2613232604Straszbailout_error:
2614232604Strasz	req->status = CTL_LUN_ERROR;
2615232604Strasz
2616232604Strasz	return (0);
2617232604Strasz}
2618232604Strasz
2619229997Skenstatic void
2620229997Skenctl_be_block_lun_shutdown(void *be_lun)
2621229997Sken{
2622229997Sken	struct ctl_be_block_lun *lun;
2623229997Sken	struct ctl_be_block_softc *softc;
2624229997Sken
2625229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2626229997Sken
2627229997Sken	softc = lun->softc;
2628229997Sken
2629229997Sken	mtx_lock(&softc->lock);
2630229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2631229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2632229997Sken		wakeup(lun);
2633229997Sken	mtx_unlock(&softc->lock);
2634229997Sken
2635229997Sken}
2636229997Sken
2637229997Skenstatic void
2638229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2639229997Sken{
2640229997Sken	struct ctl_be_block_lun *lun;
2641229997Sken	struct ctl_be_block_softc *softc;
2642229997Sken
2643229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2644229997Sken	softc = lun->softc;
2645229997Sken
2646229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2647229997Sken		mtx_lock(&softc->lock);
2648229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2649229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2650229997Sken			wakeup(lun);
2651229997Sken		mtx_unlock(&softc->lock);
2652229997Sken
2653229997Sken		/*
2654229997Sken		 * We successfully added the LUN, attempt to enable it.
2655229997Sken		 */
2656229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2657229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2658229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2659229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2660229997Sken				       __func__);
2661229997Sken			}
2662229997Sken		}
2663229997Sken
2664229997Sken		return;
2665229997Sken	}
2666229997Sken
2667229997Sken
2668229997Sken	mtx_lock(&softc->lock);
2669229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2670229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2671229997Sken	wakeup(lun);
2672229997Sken	mtx_unlock(&softc->lock);
2673229997Sken}
2674229997Sken
2675229997Sken
2676229997Skenstatic int
2677229997Skenctl_be_block_config_write(union ctl_io *io)
2678229997Sken{
2679229997Sken	struct ctl_be_block_lun *be_lun;
2680229997Sken	struct ctl_be_lun *ctl_be_lun;
2681229997Sken	int retval;
2682229997Sken
2683229997Sken	retval = 0;
2684229997Sken
2685229997Sken	DPRINTF("entered\n");
2686229997Sken
2687229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2688229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2689229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2690229997Sken
2691229997Sken	switch (io->scsiio.cdb[0]) {
2692229997Sken	case SYNCHRONIZE_CACHE:
2693229997Sken	case SYNCHRONIZE_CACHE_16:
2694265634Smav	case WRITE_SAME_10:
2695265634Smav	case WRITE_SAME_16:
2696265634Smav	case UNMAP:
2697229997Sken		/*
2698229997Sken		 * The upper level CTL code will filter out any CDBs with
2699229997Sken		 * the immediate bit set and return the proper error.
2700229997Sken		 *
2701229997Sken		 * We don't really need to worry about what LBA range the
2702229997Sken		 * user asked to be synced out.  When they issue a sync
2703229997Sken		 * cache command, we'll sync out the whole thing.
2704229997Sken		 */
2705268549Smav		mtx_lock(&be_lun->queue_lock);
2706229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2707229997Sken				   links);
2708268549Smav		mtx_unlock(&be_lun->queue_lock);
2709229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2710229997Sken		break;
2711229997Sken	case START_STOP_UNIT: {
2712229997Sken		struct scsi_start_stop_unit *cdb;
2713229997Sken
2714229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2715229997Sken
2716229997Sken		if (cdb->how & SSS_START)
2717229997Sken			retval = ctl_start_lun(ctl_be_lun);
2718229997Sken		else {
2719229997Sken			retval = ctl_stop_lun(ctl_be_lun);
2720229997Sken			/*
2721229997Sken			 * XXX KDM Copan-specific offline behavior.
2722229997Sken			 * Figure out a reasonable way to port this?
2723229997Sken			 */
2724229997Sken#ifdef NEEDTOPORT
2725229997Sken			if ((retval == 0)
2726229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2727229997Sken				retval = ctl_lun_offline(ctl_be_lun);
2728229997Sken#endif
2729229997Sken		}
2730229997Sken
2731229997Sken		/*
2732229997Sken		 * In general, the above routines should not fail.  They
2733229997Sken		 * just set state for the LUN.  So we've got something
2734229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2735229997Sken		 */
2736229997Sken		if (retval != 0) {
2737229997Sken			ctl_set_internal_failure(&io->scsiio,
2738229997Sken						 /*sks_valid*/ 1,
2739229997Sken						 /*retry_count*/ 0xf051);
2740229997Sken			retval = CTL_RETVAL_COMPLETE;
2741229997Sken		} else {
2742229997Sken			ctl_set_success(&io->scsiio);
2743229997Sken		}
2744229997Sken		ctl_config_write_done(io);
2745229997Sken		break;
2746229997Sken	}
2747229997Sken	default:
2748229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2749229997Sken		ctl_config_write_done(io);
2750229997Sken		retval = CTL_RETVAL_COMPLETE;
2751229997Sken		break;
2752229997Sken	}
2753229997Sken
2754229997Sken	return (retval);
2755229997Sken}
2756229997Sken
2757229997Skenstatic int
2758229997Skenctl_be_block_config_read(union ctl_io *io)
2759229997Sken{
2760275892Smav	struct ctl_be_block_lun *be_lun;
2761275892Smav	struct ctl_be_lun *ctl_be_lun;
2762275892Smav	int retval = 0;
2763275892Smav
2764275892Smav	DPRINTF("entered\n");
2765275892Smav
2766275892Smav	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2767275892Smav		CTL_PRIV_BACKEND_LUN].ptr;
2768275892Smav	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2769275892Smav
2770275892Smav	switch (io->scsiio.cdb[0]) {
2771275892Smav	case SERVICE_ACTION_IN:
2772275892Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2773275892Smav			mtx_lock(&be_lun->queue_lock);
2774275892Smav			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2775275892Smav			    &io->io_hdr, links);
2776275892Smav			mtx_unlock(&be_lun->queue_lock);
2777275892Smav			taskqueue_enqueue(be_lun->io_taskqueue,
2778275892Smav			    &be_lun->io_task);
2779275892Smav			retval = CTL_RETVAL_QUEUED;
2780275892Smav			break;
2781275892Smav		}
2782275892Smav		ctl_set_invalid_field(&io->scsiio,
2783275892Smav				      /*sks_valid*/ 1,
2784275892Smav				      /*command*/ 1,
2785275892Smav				      /*field*/ 1,
2786275892Smav				      /*bit_valid*/ 1,
2787275892Smav				      /*bit*/ 4);
2788275892Smav		ctl_config_read_done(io);
2789275892Smav		retval = CTL_RETVAL_COMPLETE;
2790275892Smav		break;
2791275892Smav	default:
2792275892Smav		ctl_set_invalid_opcode(&io->scsiio);
2793275892Smav		ctl_config_read_done(io);
2794275892Smav		retval = CTL_RETVAL_COMPLETE;
2795275892Smav		break;
2796275892Smav	}
2797275892Smav
2798275892Smav	return (retval);
2799229997Sken}
2800229997Sken
2801229997Skenstatic int
2802229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2803229997Sken{
2804229997Sken	struct ctl_be_block_lun *lun;
2805229997Sken	int retval;
2806229997Sken
2807229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2808229997Sken	retval = 0;
2809229997Sken
2810268555Smav	retval = sbuf_printf(sb, "\t<num_threads>");
2811229997Sken
2812229997Sken	if (retval != 0)
2813229997Sken		goto bailout;
2814229997Sken
2815229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2816229997Sken
2817229997Sken	if (retval != 0)
2818229997Sken		goto bailout;
2819229997Sken
2820268555Smav	retval = sbuf_printf(sb, "</num_threads>\n");
2821229997Sken
2822229997Skenbailout:
2823229997Sken
2824229997Sken	return (retval);
2825229997Sken}
2826229997Sken
2827274732Smavstatic uint64_t
2828274732Smavctl_be_block_lun_attr(void *be_lun, const char *attrname)
2829274732Smav{
2830274732Smav	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2831274732Smav
2832274732Smav	if (lun->getattr == NULL)
2833274732Smav		return (UINT64_MAX);
2834274732Smav	return (lun->getattr(lun, attrname));
2835274732Smav}
2836274732Smav
2837229997Skenint
2838229997Skenctl_be_block_init(void)
2839229997Sken{
2840229997Sken	struct ctl_be_block_softc *softc;
2841229997Sken	int retval;
2842229997Sken
2843229997Sken	softc = &backend_block_softc;
2844229997Sken	retval = 0;
2845229997Sken
2846268549Smav	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2847265494Strasz	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2848265494Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2849229997Sken	STAILQ_INIT(&softc->disk_list);
2850229997Sken	STAILQ_INIT(&softc->lun_list);
2851229997Sken
2852229997Sken	return (retval);
2853229997Sken}
2854