ctl_backend_block.c revision 278619
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: head/sys/cam/ctl/ctl_backend_block.c 278619 2015-02-12 10:28:45Z mav $");
44229997Sken
45229997Sken#include <sys/param.h>
46229997Sken#include <sys/systm.h>
47229997Sken#include <sys/kernel.h>
48229997Sken#include <sys/types.h>
49229997Sken#include <sys/kthread.h>
50229997Sken#include <sys/bio.h>
51229997Sken#include <sys/fcntl.h>
52264274Smav#include <sys/limits.h>
53229997Sken#include <sys/lock.h>
54229997Sken#include <sys/mutex.h>
55229997Sken#include <sys/condvar.h>
56229997Sken#include <sys/malloc.h>
57229997Sken#include <sys/conf.h>
58229997Sken#include <sys/ioccom.h>
59229997Sken#include <sys/queue.h>
60229997Sken#include <sys/sbuf.h>
61229997Sken#include <sys/endian.h>
62229997Sken#include <sys/uio.h>
63229997Sken#include <sys/buf.h>
64229997Sken#include <sys/taskqueue.h>
65229997Sken#include <sys/vnode.h>
66229997Sken#include <sys/namei.h>
67229997Sken#include <sys/mount.h>
68229997Sken#include <sys/disk.h>
69229997Sken#include <sys/fcntl.h>
70229997Sken#include <sys/filedesc.h>
71275474Smav#include <sys/filio.h>
72229997Sken#include <sys/proc.h>
73229997Sken#include <sys/pcpu.h>
74229997Sken#include <sys/module.h>
75229997Sken#include <sys/sdt.h>
76229997Sken#include <sys/devicestat.h>
77229997Sken#include <sys/sysctl.h>
78229997Sken
79229997Sken#include <geom/geom.h>
80229997Sken
81229997Sken#include <cam/cam.h>
82229997Sken#include <cam/scsi/scsi_all.h>
83229997Sken#include <cam/scsi/scsi_da.h>
84229997Sken#include <cam/ctl/ctl_io.h>
85229997Sken#include <cam/ctl/ctl.h>
86229997Sken#include <cam/ctl/ctl_backend.h>
87229997Sken#include <cam/ctl/ctl_frontend_internal.h>
88229997Sken#include <cam/ctl/ctl_ioctl.h>
89229997Sken#include <cam/ctl/ctl_scsi_all.h>
90229997Sken#include <cam/ctl/ctl_error.h>
91229997Sken
92229997Sken/*
93264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB
94264886Smav * I/O.  If we get an I/O larger than that, we'll split it.
95229997Sken */
96267537Smav#define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
97267537Smav#define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
98264886Smav#define	CTLBLK_MAX_SEG		MAXPHYS
99267537Smav#define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
100267537Smav#define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
101229997Sken
102229997Sken#ifdef CTLBLK_DEBUG
103229997Sken#define DPRINTF(fmt, args...) \
104229997Sken    printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
105229997Sken#else
106229997Sken#define DPRINTF(fmt, args...) do {} while(0)
107229997Sken#endif
108229997Sken
109267519Smav#define PRIV(io)	\
110267519Smav    ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
111267537Smav#define ARGS(io)	\
112267537Smav    ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
113267519Smav
114229997SkenSDT_PROVIDER_DEFINE(cbb);
115229997Sken
116229997Skentypedef enum {
117229997Sken	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
118229997Sken	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
119229997Sken	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
120229997Sken	CTL_BE_BLOCK_LUN_MULTI_THREAD	= 0x08
121229997Sken} ctl_be_block_lun_flags;
122229997Sken
123229997Skentypedef enum {
124229997Sken	CTL_BE_BLOCK_NONE,
125229997Sken	CTL_BE_BLOCK_DEV,
126229997Sken	CTL_BE_BLOCK_FILE
127229997Sken} ctl_be_block_type;
128229997Sken
129229997Skenstruct ctl_be_block_devdata {
130229997Sken	struct cdev *cdev;
131229997Sken	struct cdevsw *csw;
132229997Sken	int dev_ref;
133229997Sken};
134229997Sken
135229997Skenstruct ctl_be_block_filedata {
136229997Sken	struct ucred *cred;
137229997Sken};
138229997Sken
139229997Skenunion ctl_be_block_bedata {
140229997Sken	struct ctl_be_block_devdata dev;
141229997Sken	struct ctl_be_block_filedata file;
142229997Sken};
143229997Sken
144229997Skenstruct ctl_be_block_io;
145229997Skenstruct ctl_be_block_lun;
146229997Sken
147229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
148229997Sken			       struct ctl_be_block_io *beio);
149274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
150274154Smav				  const char *attrname);
151229997Sken
152229997Sken/*
153229997Sken * Backend LUN structure.  There is a 1:1 mapping between a block device
154229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN.
155229997Sken */
156229997Skenstruct ctl_be_block_lun {
157272911Smav	struct ctl_lun_create_params params;
158229997Sken	struct ctl_block_disk *disk;
159229997Sken	char lunname[32];
160229997Sken	char *dev_path;
161229997Sken	ctl_be_block_type dev_type;
162229997Sken	struct vnode *vn;
163229997Sken	union ctl_be_block_bedata backend;
164229997Sken	cbb_dispatch_t dispatch;
165229997Sken	cbb_dispatch_t lun_flush;
166264274Smav	cbb_dispatch_t unmap;
167275474Smav	cbb_dispatch_t get_lba_status;
168274154Smav	cbb_getattr_t getattr;
169229997Sken	uma_zone_t lun_zone;
170229997Sken	uint64_t size_blocks;
171229997Sken	uint64_t size_bytes;
172229997Sken	uint32_t blocksize;
173229997Sken	int blocksize_shift;
174264191Smav	uint16_t pblockexp;
175264191Smav	uint16_t pblockoff;
176275865Smav	uint16_t ublockexp;
177275865Smav	uint16_t ublockoff;
178275920Smav	uint32_t atomicblock;
179275920Smav	uint32_t opttxferlen;
180229997Sken	struct ctl_be_block_softc *softc;
181229997Sken	struct devstat *disk_stats;
182229997Sken	ctl_be_block_lun_flags flags;
183229997Sken	STAILQ_ENTRY(ctl_be_block_lun) links;
184229997Sken	struct ctl_be_lun ctl_be_lun;
185229997Sken	struct taskqueue *io_taskqueue;
186229997Sken	struct task io_task;
187229997Sken	int num_threads;
188229997Sken	STAILQ_HEAD(, ctl_io_hdr) input_queue;
189275474Smav	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
190229997Sken	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
191229997Sken	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
192267877Smav	struct mtx_padalign io_lock;
193267877Smav	struct mtx_padalign queue_lock;
194229997Sken};
195229997Sken
196229997Sken/*
197229997Sken * Overall softc structure for the block backend module.
198229997Sken */
199229997Skenstruct ctl_be_block_softc {
200229997Sken	struct mtx			 lock;
201229997Sken	int				 num_disks;
202229997Sken	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
203229997Sken	int				 num_luns;
204229997Sken	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
205229997Sken};
206229997Sken
207229997Skenstatic struct ctl_be_block_softc backend_block_softc;
208229997Sken
209229997Sken/*
210229997Sken * Per-I/O information.
211229997Sken */
212229997Skenstruct ctl_be_block_io {
213229997Sken	union ctl_io			*io;
214229997Sken	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
215229997Sken	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
216229997Sken	int				bio_cmd;
217229997Sken	int				num_segs;
218229997Sken	int				num_bios_sent;
219229997Sken	int				num_bios_done;
220229997Sken	int				send_complete;
221229997Sken	int				num_errors;
222229997Sken	struct bintime			ds_t0;
223229997Sken	devstat_tag_type		ds_tag_type;
224229997Sken	devstat_trans_flags		ds_trans_type;
225229997Sken	uint64_t			io_len;
226229997Sken	uint64_t			io_offset;
227229997Sken	struct ctl_be_block_softc	*softc;
228229997Sken	struct ctl_be_block_lun		*lun;
229264274Smav	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
230229997Sken};
231229997Sken
232229997Skenstatic int cbb_num_threads = 14;
233229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
234229997Sken	    "CAM Target Layer Block Backend");
235267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
236229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
237229997Sken
238229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
239229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
240229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
241229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
242229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
243229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
244229997Sken				    struct ctl_be_block_io *beio);
245229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
246229997Sken				       struct ctl_be_block_io *beio);
247275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
248275474Smav				  struct ctl_be_block_io *beio);
249275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
250275481Smav					 const char *attrname);
251229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
252229997Sken				   struct ctl_be_block_io *beio);
253264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
254264274Smav				   struct ctl_be_block_io *beio);
255229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
256229997Sken				      struct ctl_be_block_io *beio);
257274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
258274154Smav					 const char *attrname);
259275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
260275474Smav				    union ctl_io *io);
261229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
262229997Sken				    union ctl_io *io);
263229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
264229997Sken				  union ctl_io *io);
265229997Skenstatic void ctl_be_block_worker(void *context, int pending);
266229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
267229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
268229997Sken				   int flag, struct thread *td);
269229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
270229997Sken				  struct ctl_lun_req *req);
271229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
272229997Sken				 struct ctl_lun_req *req);
273229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
274229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
275229997Sken			     struct ctl_be_block_lun *be_lun,
276229997Sken			     struct ctl_lun_req *req);
277229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
278229997Sken			       struct ctl_lun_req *req);
279229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
280229997Sken			   struct ctl_lun_req *req);
281232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
282232604Strasz				  struct ctl_lun_req *req);
283232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
284232604Strasz				 struct ctl_lun_req *req);
285232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
286232604Strasz			   struct ctl_lun_req *req);
287229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
288229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
289229997Sken					   ctl_lun_config_status status);
290229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
291229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
292229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
293274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
294229997Skenint ctl_be_block_init(void);
295229997Sken
296229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
297229997Sken{
298230334Sken	.name = "block",
299230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
300230334Sken	.init = ctl_be_block_init,
301230334Sken	.data_submit = ctl_be_block_submit,
302230334Sken	.data_move_done = ctl_be_block_move_done,
303230334Sken	.config_read = ctl_be_block_config_read,
304230334Sken	.config_write = ctl_be_block_config_write,
305230334Sken	.ioctl = ctl_be_block_ioctl,
306274154Smav	.lun_info = ctl_be_block_lun_info,
307274154Smav	.lun_attr = ctl_be_block_lun_attr
308229997Sken};
309229997Sken
310229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
311229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
312229997Sken
313264020Straszstatic uma_zone_t beio_zone;
314264020Strasz
315229997Skenstatic struct ctl_be_block_io *
316229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
317229997Sken{
318229997Sken	struct ctl_be_block_io *beio;
319229997Sken
320264020Strasz	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
321264020Strasz	beio->softc = softc;
322229997Sken	return (beio);
323229997Sken}
324229997Sken
325229997Skenstatic void
326229997Skenctl_free_beio(struct ctl_be_block_io *beio)
327229997Sken{
328229997Sken	int duplicate_free;
329229997Sken	int i;
330229997Sken
331229997Sken	duplicate_free = 0;
332229997Sken
333229997Sken	for (i = 0; i < beio->num_segs; i++) {
334229997Sken		if (beio->sg_segs[i].addr == NULL)
335229997Sken			duplicate_free++;
336229997Sken
337229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
338229997Sken		beio->sg_segs[i].addr = NULL;
339267537Smav
340267537Smav		/* For compare we had two equal S/G lists. */
341267537Smav		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
342267537Smav			uma_zfree(beio->lun->lun_zone,
343267537Smav			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
344267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
345267537Smav		}
346229997Sken	}
347229997Sken
348229997Sken	if (duplicate_free > 0) {
349229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
350229997Sken		       duplicate_free, beio->num_segs);
351229997Sken	}
352229997Sken
353264020Strasz	uma_zfree(beio_zone, beio);
354229997Sken}
355229997Sken
356229997Skenstatic void
357229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
358229997Sken{
359267877Smav	union ctl_io *io = beio->io;
360229997Sken
361264274Smav	if (beio->beio_cont != NULL) {
362264274Smav		beio->beio_cont(beio);
363264274Smav	} else {
364264274Smav		ctl_free_beio(beio);
365267537Smav		ctl_data_submit_done(io);
366264274Smav	}
367229997Sken}
368229997Sken
369229997Skenstatic int
370229997Skenctl_be_block_move_done(union ctl_io *io)
371229997Sken{
372229997Sken	struct ctl_be_block_io *beio;
373229997Sken	struct ctl_be_block_lun *be_lun;
374267537Smav	struct ctl_lba_len_flags *lbalen;
375229997Sken#ifdef CTL_TIME_IO
376229997Sken	struct bintime cur_bt;
377267537Smav#endif
378267537Smav	int i;
379229997Sken
380267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
381229997Sken	be_lun = beio->lun;
382229997Sken
383229997Sken	DPRINTF("entered\n");
384229997Sken
385229997Sken#ifdef CTL_TIME_IO
386229997Sken	getbintime(&cur_bt);
387229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
388229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
389229997Sken	io->io_hdr.num_dmas++;
390229997Sken#endif
391267537Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
392229997Sken
393229997Sken	/*
394229997Sken	 * We set status at this point for read commands, and write
395229997Sken	 * commands with errors.
396229997Sken	 */
397275058Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
398275058Smav		;
399275058Smav	} else if ((io->io_hdr.port_status == 0) &&
400267537Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
401267537Smav		lbalen = ARGS(beio->io);
402267537Smav		if (lbalen->flags & CTL_LLF_READ) {
403267537Smav			ctl_set_success(&io->scsiio);
404267537Smav		} else if (lbalen->flags & CTL_LLF_COMPARE) {
405267537Smav			/* We have two data blocks ready for comparison. */
406267537Smav			for (i = 0; i < beio->num_segs; i++) {
407267537Smav				if (memcmp(beio->sg_segs[i].addr,
408267537Smav				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
409267537Smav				    beio->sg_segs[i].len) != 0)
410267537Smav					break;
411267537Smav			}
412267537Smav			if (i < beio->num_segs)
413267537Smav				ctl_set_sense(&io->scsiio,
414267537Smav				    /*current_error*/ 1,
415267537Smav				    /*sense_key*/ SSD_KEY_MISCOMPARE,
416267537Smav				    /*asc*/ 0x1D,
417267537Smav				    /*ascq*/ 0x00,
418267537Smav				    SSD_ELEM_NONE);
419267537Smav			else
420267537Smav				ctl_set_success(&io->scsiio);
421267537Smav		}
422275058Smav	} else if ((io->io_hdr.port_status != 0) &&
423275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
424275058Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
425229997Sken		/*
426229997Sken		 * For hardware error sense keys, the sense key
427229997Sken		 * specific value is defined to be a retry count,
428229997Sken		 * but we use it to pass back an internal FETD
429229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
430229997Sken		 * using 16 bits for an error code, since that's
431229997Sken		 * all the space we have in the sks field.
432229997Sken		 */
433229997Sken		ctl_set_internal_failure(&io->scsiio,
434229997Sken					 /*sks_valid*/ 1,
435229997Sken					 /*retry_count*/
436229997Sken					 io->io_hdr.port_status);
437229997Sken	}
438229997Sken
439229997Sken	/*
440229997Sken	 * If this is a read, or a write with errors, it is done.
441229997Sken	 */
442229997Sken	if ((beio->bio_cmd == BIO_READ)
443229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
444229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
445229997Sken		ctl_complete_beio(beio);
446229997Sken		return (0);
447229997Sken	}
448229997Sken
449229997Sken	/*
450229997Sken	 * At this point, we have a write and the DMA completed
451229997Sken	 * successfully.  We now have to queue it to the task queue to
452229997Sken	 * execute the backend I/O.  That is because we do blocking
453229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
454229997Sken	 * This move done routine is generally called in the SIM's
455229997Sken	 * interrupt context, and therefore we cannot block.
456229997Sken	 */
457267877Smav	mtx_lock(&be_lun->queue_lock);
458229997Sken	/*
459229997Sken	 * XXX KDM make sure that links is okay to use at this point.
460229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
461229997Sken	 * or deal with resource allocation here.
462229997Sken	 */
463229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
464267877Smav	mtx_unlock(&be_lun->queue_lock);
465229997Sken
466229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
467229997Sken
468229997Sken	return (0);
469229997Sken}
470229997Sken
471229997Skenstatic void
472229997Skenctl_be_block_biodone(struct bio *bio)
473229997Sken{
474229997Sken	struct ctl_be_block_io *beio;
475229997Sken	struct ctl_be_block_lun *be_lun;
476229997Sken	union ctl_io *io;
477261538Smav	int error;
478229997Sken
479229997Sken	beio = bio->bio_caller1;
480229997Sken	be_lun = beio->lun;
481229997Sken	io = beio->io;
482229997Sken
483229997Sken	DPRINTF("entered\n");
484229997Sken
485261538Smav	error = bio->bio_error;
486267877Smav	mtx_lock(&be_lun->io_lock);
487261538Smav	if (error != 0)
488229997Sken		beio->num_errors++;
489229997Sken
490229997Sken	beio->num_bios_done++;
491229997Sken
492229997Sken	/*
493229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
494229997Sken	 * during the free might cause it to complain.
495229997Sken	 */
496229997Sken	g_destroy_bio(bio);
497229997Sken
498229997Sken	/*
499229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
500229997Sken	 * complete, then we're done.
501229997Sken	 */
502229997Sken	if ((beio->send_complete == 0)
503229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
504267877Smav		mtx_unlock(&be_lun->io_lock);
505229997Sken		return;
506229997Sken	}
507229997Sken
508229997Sken	/*
509229997Sken	 * At this point, we've verified that we are the last I/O to
510229997Sken	 * complete, so it's safe to drop the lock.
511229997Sken	 */
512267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
513267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
514267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
515267877Smav	mtx_unlock(&be_lun->io_lock);
516229997Sken
517229997Sken	/*
518229997Sken	 * If there are any errors from the backing device, we fail the
519229997Sken	 * entire I/O with a medium error.
520229997Sken	 */
521229997Sken	if (beio->num_errors > 0) {
522261538Smav		if (error == EOPNOTSUPP) {
523261538Smav			ctl_set_invalid_opcode(&io->scsiio);
524273809Smav		} else if (error == ENOSPC) {
525273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
526261538Smav		} else if (beio->bio_cmd == BIO_FLUSH) {
527229997Sken			/* XXX KDM is there is a better error here? */
528229997Sken			ctl_set_internal_failure(&io->scsiio,
529229997Sken						 /*sks_valid*/ 1,
530229997Sken						 /*retry_count*/ 0xbad2);
531229997Sken		} else
532229997Sken			ctl_set_medium_error(&io->scsiio);
533229997Sken		ctl_complete_beio(beio);
534229997Sken		return;
535229997Sken	}
536229997Sken
537229997Sken	/*
538267537Smav	 * If this is a write, a flush, a delete or verify, we're all done.
539229997Sken	 * If this is a read, we can now send the data to the user.
540229997Sken	 */
541229997Sken	if ((beio->bio_cmd == BIO_WRITE)
542264274Smav	 || (beio->bio_cmd == BIO_FLUSH)
543267537Smav	 || (beio->bio_cmd == BIO_DELETE)
544267537Smav	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
545229997Sken		ctl_set_success(&io->scsiio);
546229997Sken		ctl_complete_beio(beio);
547229997Sken	} else {
548275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
549275058Smav		    beio->beio_cont == NULL)
550275058Smav			ctl_set_success(&io->scsiio);
551229997Sken#ifdef CTL_TIME_IO
552229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
553229997Sken#endif
554229997Sken		ctl_datamove(io);
555229997Sken	}
556229997Sken}
557229997Sken
558229997Skenstatic void
559229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
560229997Sken			struct ctl_be_block_io *beio)
561229997Sken{
562267877Smav	union ctl_io *io = beio->io;
563229997Sken	struct mount *mountpoint;
564241896Skib	int error, lock_flags;
565229997Sken
566229997Sken	DPRINTF("entered\n");
567229997Sken
568267877Smav	binuptime(&beio->ds_t0);
569267877Smav	mtx_lock(&be_lun->io_lock);
570267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
571267877Smav	mtx_unlock(&be_lun->io_lock);
572229997Sken
573267877Smav	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
574229997Sken
575229997Sken	if (MNT_SHARED_WRITES(mountpoint)
576229997Sken	 || ((mountpoint == NULL)
577229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
578229997Sken		lock_flags = LK_SHARED;
579229997Sken	else
580229997Sken		lock_flags = LK_EXCLUSIVE;
581229997Sken
582229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
583229997Sken
584229997Sken	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
585229997Sken	VOP_UNLOCK(be_lun->vn, 0);
586229997Sken
587229997Sken	vn_finished_write(mountpoint);
588229997Sken
589267877Smav	mtx_lock(&be_lun->io_lock);
590267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
591267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
592267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
593267877Smav	mtx_unlock(&be_lun->io_lock);
594267877Smav
595229997Sken	if (error == 0)
596229997Sken		ctl_set_success(&io->scsiio);
597229997Sken	else {
598229997Sken		/* XXX KDM is there is a better error here? */
599229997Sken		ctl_set_internal_failure(&io->scsiio,
600229997Sken					 /*sks_valid*/ 1,
601229997Sken					 /*retry_count*/ 0xbad1);
602229997Sken	}
603229997Sken
604229997Sken	ctl_complete_beio(beio);
605229997Sken}
606229997Sken
607258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
608258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
609258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
610258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
611229997Sken
612229997Skenstatic void
613229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
614229997Sken			   struct ctl_be_block_io *beio)
615229997Sken{
616229997Sken	struct ctl_be_block_filedata *file_data;
617229997Sken	union ctl_io *io;
618229997Sken	struct uio xuio;
619229997Sken	struct iovec *xiovec;
620241896Skib	int flags;
621229997Sken	int error, i;
622229997Sken
623229997Sken	DPRINTF("entered\n");
624229997Sken
625229997Sken	file_data = &be_lun->backend.file;
626229997Sken	io = beio->io;
627271309Smav	flags = 0;
628271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
629271309Smav		flags |= IO_DIRECT;
630271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
631271309Smav		flags |= IO_SYNC;
632229997Sken
633267537Smav	bzero(&xuio, sizeof(xuio));
634229997Sken	if (beio->bio_cmd == BIO_READ) {
635229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
636267537Smav		xuio.uio_rw = UIO_READ;
637229997Sken	} else {
638229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
639267537Smav		xuio.uio_rw = UIO_WRITE;
640229997Sken	}
641229997Sken	xuio.uio_offset = beio->io_offset;
642229997Sken	xuio.uio_resid = beio->io_len;
643229997Sken	xuio.uio_segflg = UIO_SYSSPACE;
644229997Sken	xuio.uio_iov = beio->xiovecs;
645229997Sken	xuio.uio_iovcnt = beio->num_segs;
646229997Sken	xuio.uio_td = curthread;
647229997Sken
648229997Sken	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
649229997Sken		xiovec->iov_base = beio->sg_segs[i].addr;
650229997Sken		xiovec->iov_len = beio->sg_segs[i].len;
651229997Sken	}
652229997Sken
653267877Smav	binuptime(&beio->ds_t0);
654267877Smav	mtx_lock(&be_lun->io_lock);
655267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
656267877Smav	mtx_unlock(&be_lun->io_lock);
657267877Smav
658229997Sken	if (beio->bio_cmd == BIO_READ) {
659229997Sken		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
660229997Sken
661229997Sken		/*
662229997Sken		 * UFS pays attention to IO_DIRECT for reads.  If the
663229997Sken		 * DIRECTIO option is configured into the kernel, it calls
664229997Sken		 * ffs_rawread().  But that only works for single-segment
665229997Sken		 * uios with user space addresses.  In our case, with a
666229997Sken		 * kernel uio, it still reads into the buffer cache, but it
667229997Sken		 * will just try to release the buffer from the cache later
668229997Sken		 * on in ffs_read().
669229997Sken		 *
670229997Sken		 * ZFS does not pay attention to IO_DIRECT for reads.
671229997Sken		 *
672229997Sken		 * UFS does not pay attention to IO_SYNC for reads.
673229997Sken		 *
674229997Sken		 * ZFS pays attention to IO_SYNC (which translates into the
675229997Sken		 * Solaris define FRSYNC for zfs_read()) for reads.  It
676229997Sken		 * attempts to sync the file before reading.
677229997Sken		 *
678229997Sken		 * So, to attempt to provide some barrier semantics in the
679229997Sken		 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
680229997Sken		 */
681271309Smav		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
682229997Sken
683229997Sken		VOP_UNLOCK(be_lun->vn, 0);
684267537Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
685229997Sken	} else {
686229997Sken		struct mount *mountpoint;
687229997Sken		int lock_flags;
688229997Sken
689229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
690229997Sken
691229997Sken		if (MNT_SHARED_WRITES(mountpoint)
692229997Sken		 || ((mountpoint == NULL)
693229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
694229997Sken			lock_flags = LK_SHARED;
695229997Sken		else
696229997Sken			lock_flags = LK_EXCLUSIVE;
697229997Sken
698229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
699229997Sken
700229997Sken		/*
701229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
702229997Sken		 * is done asynchronously.  (Normally the write would just
703229997Sken		 * get put into cache.
704229997Sken		 *
705229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
706229997Sken		 * attempt to write the buffer out synchronously if that
707229997Sken		 * flag is set.
708229997Sken		 *
709229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
710229997Sken		 *
711229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
712229997Sken		 * for writes.  It will flush the transaction from the
713229997Sken		 * cache before returning.
714229997Sken		 *
715229997Sken		 * So if we've got the BIO_ORDERED flag set, we want
716229997Sken		 * IO_SYNC in either the UFS or ZFS case.
717229997Sken		 */
718271309Smav		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
719229997Sken		VOP_UNLOCK(be_lun->vn, 0);
720229997Sken
721229997Sken		vn_finished_write(mountpoint);
722267537Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
723229997Sken        }
724229997Sken
725267877Smav	mtx_lock(&be_lun->io_lock);
726267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
727267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
728267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
729267877Smav	mtx_unlock(&be_lun->io_lock);
730267877Smav
731229997Sken	/*
732229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
733229997Sken	 * return the I/O to the user.
734229997Sken	 */
735229997Sken	if (error != 0) {
736229997Sken		char path_str[32];
737229997Sken
738229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
739229997Sken		printf("%s%s command returned errno %d\n", path_str,
740229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
741273809Smav		if (error == ENOSPC) {
742273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
743273809Smav		} else
744273809Smav			ctl_set_medium_error(&io->scsiio);
745229997Sken		ctl_complete_beio(beio);
746229997Sken		return;
747229997Sken	}
748229997Sken
749229997Sken	/*
750269122Smav	 * If this is a write or a verify, we're all done.
751229997Sken	 * If this is a read, we can now send the data to the user.
752229997Sken	 */
753269122Smav	if ((beio->bio_cmd == BIO_WRITE) ||
754269122Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
755229997Sken		ctl_set_success(&io->scsiio);
756229997Sken		ctl_complete_beio(beio);
757229997Sken	} else {
758275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
759275058Smav		    beio->beio_cont == NULL)
760275058Smav			ctl_set_success(&io->scsiio);
761229997Sken#ifdef CTL_TIME_IO
762229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
763229997Sken#endif
764229997Sken		ctl_datamove(io);
765229997Sken	}
766229997Sken}
767229997Sken
768229997Skenstatic void
769275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
770275474Smav			struct ctl_be_block_io *beio)
771275474Smav{
772275474Smav	union ctl_io *io = beio->io;
773275474Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
774275474Smav	struct scsi_get_lba_status_data *data;
775275474Smav	off_t roff, off;
776275474Smav	int error, status;
777275474Smav
778275474Smav	DPRINTF("entered\n");
779275474Smav
780275474Smav	off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift;
781275474Smav	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
782275474Smav	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
783275474Smav	    0, curthread->td_ucred, curthread);
784275474Smav	if (error == 0 && off > roff)
785275474Smav		status = 0;	/* mapped up to off */
786275474Smav	else {
787275474Smav		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
788275474Smav		    0, curthread->td_ucred, curthread);
789275474Smav		if (error == 0 && off > roff)
790275474Smav			status = 1;	/* deallocated up to off */
791275474Smav		else {
792275474Smav			status = 0;	/* unknown up to the end */
793275474Smav			off = be_lun->size_bytes;
794275474Smav		}
795275474Smav	}
796275474Smav	VOP_UNLOCK(be_lun->vn, 0);
797275474Smav
798275474Smav	off >>= be_lun->blocksize_shift;
799275474Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
800275474Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
801275474Smav	scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba),
802275474Smav	    data->descr[0].length);
803275474Smav	data->descr[0].status = status;
804275474Smav
805275474Smav	ctl_complete_beio(beio);
806275474Smav}
807275474Smav
808275481Smavstatic uint64_t
809275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
810275481Smav{
811275481Smav	struct vattr		vattr;
812275481Smav	struct statfs		statfs;
813275481Smav	int			error;
814275481Smav
815275481Smav	if (be_lun->vn == NULL)
816275481Smav		return (UINT64_MAX);
817275481Smav	if (strcmp(attrname, "blocksused") == 0) {
818275481Smav		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
819275481Smav		if (error != 0)
820275481Smav			return (UINT64_MAX);
821275481Smav		return (vattr.va_bytes >> be_lun->blocksize_shift);
822275481Smav	}
823275481Smav	if (strcmp(attrname, "blocksavail") == 0) {
824275481Smav		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
825275481Smav		if (error != 0)
826275481Smav			return (UINT64_MAX);
827275481Smav		return ((statfs.f_bavail * statfs.f_bsize) >>
828275481Smav		    be_lun->blocksize_shift);
829275481Smav	}
830275481Smav	return (UINT64_MAX);
831275481Smav}
832275481Smav
833275474Smavstatic void
834269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
835269123Smav			   struct ctl_be_block_io *beio)
836269123Smav{
837269123Smav	struct ctl_be_block_devdata *dev_data;
838269123Smav	union ctl_io *io;
839269123Smav	struct uio xuio;
840269123Smav	struct iovec *xiovec;
841269123Smav	int flags;
842269123Smav	int error, i;
843269123Smav
844269123Smav	DPRINTF("entered\n");
845269123Smav
846269123Smav	dev_data = &be_lun->backend.dev;
847269123Smav	io = beio->io;
848271309Smav	flags = 0;
849271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
850271309Smav		flags |= IO_DIRECT;
851271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
852271309Smav		flags |= IO_SYNC;
853269123Smav
854269123Smav	bzero(&xuio, sizeof(xuio));
855269123Smav	if (beio->bio_cmd == BIO_READ) {
856269123Smav		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
857269123Smav		xuio.uio_rw = UIO_READ;
858269123Smav	} else {
859269123Smav		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
860269123Smav		xuio.uio_rw = UIO_WRITE;
861269123Smav	}
862269123Smav	xuio.uio_offset = beio->io_offset;
863269123Smav	xuio.uio_resid = beio->io_len;
864269123Smav	xuio.uio_segflg = UIO_SYSSPACE;
865269123Smav	xuio.uio_iov = beio->xiovecs;
866269123Smav	xuio.uio_iovcnt = beio->num_segs;
867269123Smav	xuio.uio_td = curthread;
868269123Smav
869269123Smav	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
870269123Smav		xiovec->iov_base = beio->sg_segs[i].addr;
871269123Smav		xiovec->iov_len = beio->sg_segs[i].len;
872269123Smav	}
873269123Smav
874269123Smav	binuptime(&beio->ds_t0);
875269123Smav	mtx_lock(&be_lun->io_lock);
876269123Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
877269123Smav	mtx_unlock(&be_lun->io_lock);
878269123Smav
879269123Smav	if (beio->bio_cmd == BIO_READ) {
880271309Smav		error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
881269123Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
882269123Smav	} else {
883271309Smav		error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
884269123Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
885269123Smav	}
886269123Smav
887269123Smav	mtx_lock(&be_lun->io_lock);
888269123Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
889269123Smav	    beio->ds_tag_type, beio->ds_trans_type,
890269123Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
891269123Smav	mtx_unlock(&be_lun->io_lock);
892269123Smav
893269123Smav	/*
894269123Smav	 * If we got an error, set the sense data to "MEDIUM ERROR" and
895269123Smav	 * return the I/O to the user.
896269123Smav	 */
897269123Smav	if (error != 0) {
898273809Smav		if (error == ENOSPC) {
899273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
900273809Smav		} else
901273809Smav			ctl_set_medium_error(&io->scsiio);
902269123Smav		ctl_complete_beio(beio);
903269123Smav		return;
904269123Smav	}
905269123Smav
906269123Smav	/*
907269123Smav	 * If this is a write or a verify, we're all done.
908269123Smav	 * If this is a read, we can now send the data to the user.
909269123Smav	 */
910269123Smav	if ((beio->bio_cmd == BIO_WRITE) ||
911269123Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
912269123Smav		ctl_set_success(&io->scsiio);
913269123Smav		ctl_complete_beio(beio);
914269123Smav	} else {
915275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
916275058Smav		    beio->beio_cont == NULL)
917275058Smav			ctl_set_success(&io->scsiio);
918269123Smav#ifdef CTL_TIME_IO
919269123Smav        	getbintime(&io->io_hdr.dma_start_bt);
920269123Smav#endif
921269123Smav		ctl_datamove(io);
922269123Smav	}
923269123Smav}
924269123Smav
925269123Smavstatic void
926275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
927275474Smav			struct ctl_be_block_io *beio)
928275474Smav{
929275474Smav	struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev;
930275474Smav	union ctl_io *io = beio->io;
931275474Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
932275474Smav	struct scsi_get_lba_status_data *data;
933275474Smav	off_t roff, off;
934275474Smav	int error, status;
935275474Smav
936275474Smav	DPRINTF("entered\n");
937275474Smav
938275474Smav	off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift;
939275474Smav	error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE,
940275474Smav	    (caddr_t)&off, FREAD, curthread);
941275474Smav	if (error == 0 && off > roff)
942275474Smav		status = 0;	/* mapped up to off */
943275474Smav	else {
944275474Smav		error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA,
945275474Smav		    (caddr_t)&off, FREAD, curthread);
946275474Smav		if (error == 0 && off > roff)
947275474Smav			status = 1;	/* deallocated up to off */
948275474Smav		else {
949275474Smav			status = 0;	/* unknown up to the end */
950275474Smav			off = be_lun->size_bytes;
951275474Smav		}
952275474Smav	}
953275474Smav
954275474Smav	off >>= be_lun->blocksize_shift;
955275474Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
956275474Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
957275474Smav	scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba),
958275474Smav	    data->descr[0].length);
959275474Smav	data->descr[0].status = status;
960275474Smav
961275474Smav	ctl_complete_beio(beio);
962275474Smav}
963275474Smav
964275474Smavstatic void
965229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
966229997Sken		       struct ctl_be_block_io *beio)
967229997Sken{
968229997Sken	struct bio *bio;
969229997Sken	union ctl_io *io;
970229997Sken	struct ctl_be_block_devdata *dev_data;
971229997Sken
972229997Sken	dev_data = &be_lun->backend.dev;
973229997Sken	io = beio->io;
974229997Sken
975229997Sken	DPRINTF("entered\n");
976229997Sken
977229997Sken	/* This can't fail, it's a blocking allocation. */
978229997Sken	bio = g_alloc_bio();
979229997Sken
980229997Sken	bio->bio_cmd	    = BIO_FLUSH;
981229997Sken	bio->bio_flags	   |= BIO_ORDERED;
982229997Sken	bio->bio_dev	    = dev_data->cdev;
983229997Sken	bio->bio_offset	    = 0;
984229997Sken	bio->bio_data	    = 0;
985229997Sken	bio->bio_done	    = ctl_be_block_biodone;
986229997Sken	bio->bio_caller1    = beio;
987229997Sken	bio->bio_pblkno	    = 0;
988229997Sken
989229997Sken	/*
990229997Sken	 * We don't need to acquire the LUN lock here, because we are only
991229997Sken	 * sending one bio, and so there is no other context to synchronize
992229997Sken	 * with.
993229997Sken	 */
994229997Sken	beio->num_bios_sent = 1;
995229997Sken	beio->send_complete = 1;
996229997Sken
997229997Sken	binuptime(&beio->ds_t0);
998267877Smav	mtx_lock(&be_lun->io_lock);
999229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1000267877Smav	mtx_unlock(&be_lun->io_lock);
1001229997Sken
1002229997Sken	(*dev_data->csw->d_strategy)(bio);
1003229997Sken}
1004229997Sken
1005229997Skenstatic void
1006264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1007264274Smav		       struct ctl_be_block_io *beio,
1008264274Smav		       uint64_t off, uint64_t len, int last)
1009264274Smav{
1010264274Smav	struct bio *bio;
1011264274Smav	struct ctl_be_block_devdata *dev_data;
1012264296Smav	uint64_t maxlen;
1013264274Smav
1014264274Smav	dev_data = &be_lun->backend.dev;
1015264296Smav	maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
1016264274Smav	while (len > 0) {
1017264274Smav		bio = g_alloc_bio();
1018264274Smav		bio->bio_cmd	    = BIO_DELETE;
1019264274Smav		bio->bio_dev	    = dev_data->cdev;
1020264274Smav		bio->bio_offset	    = off;
1021264296Smav		bio->bio_length	    = MIN(len, maxlen);
1022264274Smav		bio->bio_data	    = 0;
1023264274Smav		bio->bio_done	    = ctl_be_block_biodone;
1024264274Smav		bio->bio_caller1    = beio;
1025264296Smav		bio->bio_pblkno     = off / be_lun->blocksize;
1026264274Smav
1027264274Smav		off += bio->bio_length;
1028264274Smav		len -= bio->bio_length;
1029264274Smav
1030267877Smav		mtx_lock(&be_lun->io_lock);
1031264274Smav		beio->num_bios_sent++;
1032264274Smav		if (last && len == 0)
1033264274Smav			beio->send_complete = 1;
1034267877Smav		mtx_unlock(&be_lun->io_lock);
1035264274Smav
1036264274Smav		(*dev_data->csw->d_strategy)(bio);
1037264274Smav	}
1038264274Smav}
1039264274Smav
1040264274Smavstatic void
1041264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1042264274Smav		       struct ctl_be_block_io *beio)
1043264274Smav{
1044264274Smav	union ctl_io *io;
1045264274Smav	struct ctl_be_block_devdata *dev_data;
1046267515Smav	struct ctl_ptr_len_flags *ptrlen;
1047264274Smav	struct scsi_unmap_desc *buf, *end;
1048264274Smav	uint64_t len;
1049264274Smav
1050264274Smav	dev_data = &be_lun->backend.dev;
1051264274Smav	io = beio->io;
1052264274Smav
1053264274Smav	DPRINTF("entered\n");
1054264274Smav
1055264274Smav	binuptime(&beio->ds_t0);
1056267877Smav	mtx_lock(&be_lun->io_lock);
1057264274Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1058267877Smav	mtx_unlock(&be_lun->io_lock);
1059264274Smav
1060264274Smav	if (beio->io_offset == -1) {
1061264274Smav		beio->io_len = 0;
1062267515Smav		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1063267515Smav		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1064267515Smav		end = buf + ptrlen->len / sizeof(*buf);
1065264274Smav		for (; buf < end; buf++) {
1066264274Smav			len = (uint64_t)scsi_4btoul(buf->length) *
1067264274Smav			    be_lun->blocksize;
1068264274Smav			beio->io_len += len;
1069264274Smav			ctl_be_block_unmap_dev_range(be_lun, beio,
1070264274Smav			    scsi_8btou64(buf->lba) * be_lun->blocksize, len,
1071264283Smav			    (end - buf < 2) ? TRUE : FALSE);
1072264274Smav		}
1073264274Smav	} else
1074264274Smav		ctl_be_block_unmap_dev_range(be_lun, beio,
1075264274Smav		    beio->io_offset, beio->io_len, TRUE);
1076264274Smav}
1077264274Smav
1078264274Smavstatic void
1079229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1080229997Sken			  struct ctl_be_block_io *beio)
1081229997Sken{
1082267877Smav	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1083229997Sken	int i;
1084229997Sken	struct bio *bio;
1085229997Sken	struct ctl_be_block_devdata *dev_data;
1086229997Sken	off_t cur_offset;
1087229997Sken	int max_iosize;
1088229997Sken
1089229997Sken	DPRINTF("entered\n");
1090229997Sken
1091229997Sken	dev_data = &be_lun->backend.dev;
1092229997Sken
1093229997Sken	/*
1094229997Sken	 * We have to limit our I/O size to the maximum supported by the
1095229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
1096229997Sken	 * set it properly, use DFLTPHYS.
1097229997Sken	 */
1098229997Sken	max_iosize = dev_data->cdev->si_iosize_max;
1099229997Sken	if (max_iosize < PAGE_SIZE)
1100229997Sken		max_iosize = DFLTPHYS;
1101229997Sken
1102229997Sken	cur_offset = beio->io_offset;
1103229997Sken	for (i = 0; i < beio->num_segs; i++) {
1104229997Sken		size_t cur_size;
1105229997Sken		uint8_t *cur_ptr;
1106229997Sken
1107229997Sken		cur_size = beio->sg_segs[i].len;
1108229997Sken		cur_ptr = beio->sg_segs[i].addr;
1109229997Sken
1110229997Sken		while (cur_size > 0) {
1111229997Sken			/* This can't fail, it's a blocking allocation. */
1112229997Sken			bio = g_alloc_bio();
1113229997Sken
1114229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1115229997Sken
1116229997Sken			bio->bio_cmd = beio->bio_cmd;
1117229997Sken			bio->bio_dev = dev_data->cdev;
1118229997Sken			bio->bio_caller1 = beio;
1119229997Sken			bio->bio_length = min(cur_size, max_iosize);
1120229997Sken			bio->bio_offset = cur_offset;
1121229997Sken			bio->bio_data = cur_ptr;
1122229997Sken			bio->bio_done = ctl_be_block_biodone;
1123229997Sken			bio->bio_pblkno = cur_offset / be_lun->blocksize;
1124229997Sken
1125229997Sken			cur_offset += bio->bio_length;
1126229997Sken			cur_ptr += bio->bio_length;
1127229997Sken			cur_size -= bio->bio_length;
1128229997Sken
1129267877Smav			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1130229997Sken			beio->num_bios_sent++;
1131229997Sken		}
1132229997Sken	}
1133267877Smav	binuptime(&beio->ds_t0);
1134267877Smav	mtx_lock(&be_lun->io_lock);
1135267877Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1136267877Smav	beio->send_complete = 1;
1137267877Smav	mtx_unlock(&be_lun->io_lock);
1138267877Smav
1139267877Smav	/*
1140267877Smav	 * Fire off all allocated requests!
1141267877Smav	 */
1142267877Smav	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1143267877Smav		TAILQ_REMOVE(&queue, bio, bio_queue);
1144267877Smav		(*dev_data->csw->d_strategy)(bio);
1145267877Smav	}
1146229997Sken}
1147229997Sken
1148274154Smavstatic uint64_t
1149274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1150274154Smav{
1151274154Smav	struct ctl_be_block_devdata	*dev_data = &be_lun->backend.dev;
1152274154Smav	struct diocgattr_arg	arg;
1153274154Smav	int			error;
1154274154Smav
1155274154Smav	if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL)
1156274154Smav		return (UINT64_MAX);
1157274154Smav	strlcpy(arg.name, attrname, sizeof(arg.name));
1158274154Smav	arg.len = sizeof(arg.value.off);
1159274154Smav	error = dev_data->csw->d_ioctl(dev_data->cdev,
1160274154Smav	    DIOCGATTR, (caddr_t)&arg, FREAD, curthread);
1161274154Smav	if (error != 0)
1162274154Smav		return (UINT64_MAX);
1163274154Smav	return (arg.value.off);
1164274154Smav}
1165274154Smav
1166229997Skenstatic void
1167264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1168264274Smav{
1169264274Smav	union ctl_io *io;
1170264274Smav
1171264274Smav	io = beio->io;
1172264274Smav	ctl_free_beio(beio);
1173267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1174267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1175267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1176264274Smav		ctl_config_write_done(io);
1177264274Smav		return;
1178264274Smav	}
1179264274Smav
1180264274Smav	ctl_be_block_config_write(io);
1181264274Smav}
1182264274Smav
1183264274Smavstatic void
1184264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1185264274Smav			    union ctl_io *io)
1186264274Smav{
1187264274Smav	struct ctl_be_block_io *beio;
1188264274Smav	struct ctl_be_block_softc *softc;
1189267515Smav	struct ctl_lba_len_flags *lbalen;
1190278619Smav	uint64_t len_left, lba, pb, pbo, adj;
1191264274Smav	int i, seglen;
1192264274Smav	uint8_t *buf, *end;
1193264274Smav
1194264274Smav	DPRINTF("entered\n");
1195264274Smav
1196267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1197264274Smav	softc = be_lun->softc;
1198267537Smav	lbalen = ARGS(beio->io);
1199264274Smav
1200271839Smav	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1201269622Smav	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1202264274Smav		ctl_free_beio(beio);
1203264274Smav		ctl_set_invalid_field(&io->scsiio,
1204264274Smav				      /*sks_valid*/ 1,
1205264274Smav				      /*command*/ 1,
1206264274Smav				      /*field*/ 1,
1207264274Smav				      /*bit_valid*/ 0,
1208264274Smav				      /*bit*/ 0);
1209264274Smav		ctl_config_write_done(io);
1210264274Smav		return;
1211264274Smav	}
1212264274Smav
1213264274Smav	switch (io->scsiio.tag_type) {
1214264274Smav	case CTL_TAG_ORDERED:
1215264274Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1216264274Smav		break;
1217264274Smav	case CTL_TAG_HEAD_OF_QUEUE:
1218264274Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1219264274Smav		break;
1220264274Smav	case CTL_TAG_UNTAGGED:
1221264274Smav	case CTL_TAG_SIMPLE:
1222264274Smav	case CTL_TAG_ACA:
1223264274Smav	default:
1224264274Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1225264274Smav		break;
1226264274Smav	}
1227264274Smav
1228269622Smav	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1229267515Smav		beio->io_offset = lbalen->lba * be_lun->blocksize;
1230267515Smav		beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1231264274Smav		beio->bio_cmd = BIO_DELETE;
1232264274Smav		beio->ds_trans_type = DEVSTAT_FREE;
1233264274Smav
1234264274Smav		be_lun->unmap(be_lun, beio);
1235264274Smav		return;
1236264274Smav	}
1237264274Smav
1238264274Smav	beio->bio_cmd = BIO_WRITE;
1239264274Smav	beio->ds_trans_type = DEVSTAT_WRITE;
1240264274Smav
1241264274Smav	DPRINTF("WRITE SAME at LBA %jx len %u\n",
1242267515Smav	       (uintmax_t)lbalen->lba, lbalen->len);
1243264274Smav
1244278619Smav	pb = (uint64_t)be_lun->blocksize << be_lun->pblockexp;
1245278619Smav	pbo = pb - (uint64_t)be_lun->blocksize * be_lun->pblockoff;
1246267515Smav	len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1247264274Smav	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1248264274Smav
1249264274Smav		/*
1250264274Smav		 * Setup the S/G entry for this chunk.
1251264274Smav		 */
1252264886Smav		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1253278619Smav		if (pb > be_lun->blocksize) {
1254278619Smav			adj = ((lbalen->lba + lba) * be_lun->blocksize +
1255278619Smav			    seglen - pbo) % pb;
1256278619Smav			if (seglen > adj)
1257278619Smav				seglen -= adj;
1258278619Smav			else
1259278619Smav				seglen -= seglen % be_lun->blocksize;
1260278619Smav		} else
1261278619Smav			seglen -= seglen % be_lun->blocksize;
1262264274Smav		beio->sg_segs[i].len = seglen;
1263264274Smav		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1264264274Smav
1265264274Smav		DPRINTF("segment %d addr %p len %zd\n", i,
1266264274Smav			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1267264274Smav
1268264274Smav		beio->num_segs++;
1269264274Smav		len_left -= seglen;
1270264274Smav
1271264274Smav		buf = beio->sg_segs[i].addr;
1272264274Smav		end = buf + seglen;
1273264274Smav		for (; buf < end; buf += be_lun->blocksize) {
1274264274Smav			memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1275267515Smav			if (lbalen->flags & SWS_LBDATA)
1276267515Smav				scsi_ulto4b(lbalen->lba + lba, buf);
1277264274Smav			lba++;
1278264274Smav		}
1279264274Smav	}
1280264274Smav
1281267515Smav	beio->io_offset = lbalen->lba * be_lun->blocksize;
1282264274Smav	beio->io_len = lba * be_lun->blocksize;
1283264274Smav
1284264274Smav	/* We can not do all in one run. Correct and schedule rerun. */
1285264274Smav	if (len_left > 0) {
1286267515Smav		lbalen->lba += lba;
1287267515Smav		lbalen->len -= lba;
1288264274Smav		beio->beio_cont = ctl_be_block_cw_done_ws;
1289264274Smav	}
1290264274Smav
1291264274Smav	be_lun->dispatch(be_lun, beio);
1292264274Smav}
1293264274Smav
1294264274Smavstatic void
1295264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1296264274Smav			    union ctl_io *io)
1297264274Smav{
1298264274Smav	struct ctl_be_block_io *beio;
1299264274Smav	struct ctl_be_block_softc *softc;
1300267515Smav	struct ctl_ptr_len_flags *ptrlen;
1301264274Smav
1302264274Smav	DPRINTF("entered\n");
1303264274Smav
1304267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1305264274Smav	softc = be_lun->softc;
1306267515Smav	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1307264274Smav
1308269622Smav	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1309264274Smav		ctl_free_beio(beio);
1310264274Smav		ctl_set_invalid_field(&io->scsiio,
1311264274Smav				      /*sks_valid*/ 0,
1312264274Smav				      /*command*/ 1,
1313264274Smav				      /*field*/ 0,
1314264274Smav				      /*bit_valid*/ 0,
1315264274Smav				      /*bit*/ 0);
1316264274Smav		ctl_config_write_done(io);
1317264274Smav		return;
1318264274Smav	}
1319264274Smav
1320264274Smav	switch (io->scsiio.tag_type) {
1321264274Smav	case CTL_TAG_ORDERED:
1322264274Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1323264274Smav		break;
1324264274Smav	case CTL_TAG_HEAD_OF_QUEUE:
1325264274Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1326264274Smav		break;
1327264274Smav	case CTL_TAG_UNTAGGED:
1328264274Smav	case CTL_TAG_SIMPLE:
1329264274Smav	case CTL_TAG_ACA:
1330264274Smav	default:
1331264274Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1332264274Smav		break;
1333264274Smav	}
1334264274Smav
1335264274Smav	beio->io_len = 0;
1336264274Smav	beio->io_offset = -1;
1337264274Smav
1338264274Smav	beio->bio_cmd = BIO_DELETE;
1339264274Smav	beio->ds_trans_type = DEVSTAT_FREE;
1340264274Smav
1341267515Smav	DPRINTF("UNMAP\n");
1342264274Smav
1343264274Smav	be_lun->unmap(be_lun, beio);
1344264274Smav}
1345264274Smav
1346264274Smavstatic void
1347275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio)
1348275474Smav{
1349275474Smav	union ctl_io *io;
1350275474Smav
1351275474Smav	io = beio->io;
1352275474Smav	ctl_free_beio(beio);
1353275474Smav	ctl_config_read_done(io);
1354275474Smav}
1355275474Smav
1356275474Smavstatic void
1357275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1358275474Smav			 union ctl_io *io)
1359275474Smav{
1360275474Smav	struct ctl_be_block_io *beio;
1361275474Smav	struct ctl_be_block_softc *softc;
1362275474Smav
1363275474Smav	DPRINTF("entered\n");
1364275474Smav
1365275474Smav	softc = be_lun->softc;
1366275474Smav	beio = ctl_alloc_beio(softc);
1367275474Smav	beio->io = io;
1368275474Smav	beio->lun = be_lun;
1369275474Smav	beio->beio_cont = ctl_be_block_cr_done;
1370275474Smav	PRIV(io)->ptr = (void *)beio;
1371275474Smav
1372275474Smav	switch (io->scsiio.cdb[0]) {
1373275474Smav	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1374275474Smav		beio->bio_cmd = -1;
1375275474Smav		beio->ds_trans_type = DEVSTAT_NO_DATA;
1376275474Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1377275474Smav		beio->io_len = 0;
1378275474Smav		if (be_lun->get_lba_status)
1379275474Smav			be_lun->get_lba_status(be_lun, beio);
1380275474Smav		else
1381275474Smav			ctl_be_block_cr_done(beio);
1382275474Smav		break;
1383275474Smav	default:
1384275474Smav		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1385275474Smav		break;
1386275474Smav	}
1387275474Smav}
1388275474Smav
1389275474Smavstatic void
1390264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio)
1391264274Smav{
1392264274Smav	union ctl_io *io;
1393264274Smav
1394264274Smav	io = beio->io;
1395264274Smav	ctl_free_beio(beio);
1396264274Smav	ctl_config_write_done(io);
1397264274Smav}
1398264274Smav
1399264274Smavstatic void
1400229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1401229997Sken			 union ctl_io *io)
1402229997Sken{
1403229997Sken	struct ctl_be_block_io *beio;
1404229997Sken	struct ctl_be_block_softc *softc;
1405229997Sken
1406229997Sken	DPRINTF("entered\n");
1407229997Sken
1408229997Sken	softc = be_lun->softc;
1409229997Sken	beio = ctl_alloc_beio(softc);
1410229997Sken	beio->io = io;
1411229997Sken	beio->lun = be_lun;
1412264274Smav	beio->beio_cont = ctl_be_block_cw_done;
1413267519Smav	PRIV(io)->ptr = (void *)beio;
1414229997Sken
1415229997Sken	switch (io->scsiio.cdb[0]) {
1416229997Sken	case SYNCHRONIZE_CACHE:
1417229997Sken	case SYNCHRONIZE_CACHE_16:
1418249194Strasz		beio->bio_cmd = BIO_FLUSH;
1419229997Sken		beio->ds_trans_type = DEVSTAT_NO_DATA;
1420229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1421229997Sken		beio->io_len = 0;
1422229997Sken		be_lun->lun_flush(be_lun, beio);
1423229997Sken		break;
1424264274Smav	case WRITE_SAME_10:
1425264274Smav	case WRITE_SAME_16:
1426264274Smav		ctl_be_block_cw_dispatch_ws(be_lun, io);
1427264274Smav		break;
1428264274Smav	case UNMAP:
1429264274Smav		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1430264274Smav		break;
1431229997Sken	default:
1432229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1433229997Sken		break;
1434229997Sken	}
1435229997Sken}
1436229997Sken
1437258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1438258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1439258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1440258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1441229997Sken
1442229997Skenstatic void
1443264886Smavctl_be_block_next(struct ctl_be_block_io *beio)
1444264886Smav{
1445264886Smav	struct ctl_be_block_lun *be_lun;
1446264886Smav	union ctl_io *io;
1447264886Smav
1448264886Smav	io = beio->io;
1449264886Smav	be_lun = beio->lun;
1450264886Smav	ctl_free_beio(beio);
1451267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1452267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1453267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1454267537Smav		ctl_data_submit_done(io);
1455264886Smav		return;
1456264886Smav	}
1457264886Smav
1458264886Smav	io->io_hdr.status &= ~CTL_STATUS_MASK;
1459264886Smav	io->io_hdr.status |= CTL_STATUS_NONE;
1460264886Smav
1461267877Smav	mtx_lock(&be_lun->queue_lock);
1462264886Smav	/*
1463264886Smav	 * XXX KDM make sure that links is okay to use at this point.
1464264886Smav	 * Otherwise, we either need to add another field to ctl_io_hdr,
1465264886Smav	 * or deal with resource allocation here.
1466264886Smav	 */
1467264886Smav	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1468267877Smav	mtx_unlock(&be_lun->queue_lock);
1469264886Smav
1470264886Smav	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1471264886Smav}
1472264886Smav
1473264886Smavstatic void
1474229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1475229997Sken			   union ctl_io *io)
1476229997Sken{
1477229997Sken	struct ctl_be_block_io *beio;
1478229997Sken	struct ctl_be_block_softc *softc;
1479267537Smav	struct ctl_lba_len_flags *lbalen;
1480267519Smav	struct ctl_ptr_len_flags *bptrlen;
1481267519Smav	uint64_t len_left, lbas;
1482229997Sken	int i;
1483229997Sken
1484229997Sken	softc = be_lun->softc;
1485229997Sken
1486229997Sken	DPRINTF("entered\n");
1487229997Sken
1488267537Smav	lbalen = ARGS(io);
1489267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1490267537Smav		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1491267537Smav	} else {
1492229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1493229997Sken	}
1494229997Sken
1495229997Sken	beio = ctl_alloc_beio(softc);
1496229997Sken	beio->io = io;
1497229997Sken	beio->lun = be_lun;
1498267519Smav	bptrlen = PRIV(io);
1499267519Smav	bptrlen->ptr = (void *)beio;
1500229997Sken
1501229997Sken	switch (io->scsiio.tag_type) {
1502229997Sken	case CTL_TAG_ORDERED:
1503229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1504229997Sken		break;
1505229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1506229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1507229997Sken		break;
1508229997Sken	case CTL_TAG_UNTAGGED:
1509229997Sken	case CTL_TAG_SIMPLE:
1510229997Sken	case CTL_TAG_ACA:
1511229997Sken	default:
1512229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1513229997Sken		break;
1514229997Sken	}
1515229997Sken
1516267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1517267537Smav		beio->bio_cmd = BIO_WRITE;
1518267537Smav		beio->ds_trans_type = DEVSTAT_WRITE;
1519267537Smav	} else {
1520229997Sken		beio->bio_cmd = BIO_READ;
1521229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1522229997Sken	}
1523229997Sken
1524264886Smav	DPRINTF("%s at LBA %jx len %u @%ju\n",
1525229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1526267519Smav	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1527267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1528267537Smav		lbas = CTLBLK_HALF_IO_SIZE;
1529267537Smav	else
1530267537Smav		lbas = CTLBLK_MAX_IO_SIZE;
1531267537Smav	lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1532267519Smav	beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1533267519Smav	beio->io_len = lbas * be_lun->blocksize;
1534267519Smav	bptrlen->len += lbas;
1535229997Sken
1536264886Smav	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1537264886Smav		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1538264886Smav		    i, CTLBLK_MAX_SEGS));
1539229997Sken
1540229997Sken		/*
1541229997Sken		 * Setup the S/G entry for this chunk.
1542229997Sken		 */
1543264886Smav		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1544229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1545229997Sken
1546229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1547229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1548229997Sken
1549267537Smav		/* Set up second segment for compare operation. */
1550267537Smav		if (lbalen->flags & CTL_LLF_COMPARE) {
1551267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1552267537Smav			    beio->sg_segs[i].len;
1553267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1554267537Smav			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
1555267537Smav		}
1556267537Smav
1557229997Sken		beio->num_segs++;
1558229997Sken		len_left -= beio->sg_segs[i].len;
1559229997Sken	}
1560267519Smav	if (bptrlen->len < lbalen->len)
1561264886Smav		beio->beio_cont = ctl_be_block_next;
1562264886Smav	io->scsiio.be_move_done = ctl_be_block_move_done;
1563267537Smav	/* For compare we have separate S/G lists for read and datamove. */
1564267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1565267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1566267537Smav	else
1567267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1568264886Smav	io->scsiio.kern_data_len = beio->io_len;
1569264886Smav	io->scsiio.kern_data_resid = 0;
1570264886Smav	io->scsiio.kern_sg_entries = beio->num_segs;
1571264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1572229997Sken
1573229997Sken	/*
1574229997Sken	 * For the read case, we need to read the data into our buffers and
1575229997Sken	 * then we can send it back to the user.  For the write case, we
1576229997Sken	 * need to get the data from the user first.
1577229997Sken	 */
1578229997Sken	if (beio->bio_cmd == BIO_READ) {
1579229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1580229997Sken		be_lun->dispatch(be_lun, beio);
1581229997Sken	} else {
1582229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1583229997Sken#ifdef CTL_TIME_IO
1584229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1585229997Sken#endif
1586229997Sken		ctl_datamove(io);
1587229997Sken	}
1588229997Sken}
1589229997Sken
1590229997Skenstatic void
1591229997Skenctl_be_block_worker(void *context, int pending)
1592229997Sken{
1593229997Sken	struct ctl_be_block_lun *be_lun;
1594229997Sken	struct ctl_be_block_softc *softc;
1595229997Sken	union ctl_io *io;
1596229997Sken
1597229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1598229997Sken	softc = be_lun->softc;
1599229997Sken
1600229997Sken	DPRINTF("entered\n");
1601229997Sken
1602267877Smav	mtx_lock(&be_lun->queue_lock);
1603229997Sken	for (;;) {
1604229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1605229997Sken		if (io != NULL) {
1606229997Sken			struct ctl_be_block_io *beio;
1607229997Sken
1608229997Sken			DPRINTF("datamove queue\n");
1609229997Sken
1610229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1611229997Sken				      ctl_io_hdr, links);
1612229997Sken
1613267877Smav			mtx_unlock(&be_lun->queue_lock);
1614229997Sken
1615267519Smav			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1616229997Sken
1617229997Sken			be_lun->dispatch(be_lun, beio);
1618229997Sken
1619267877Smav			mtx_lock(&be_lun->queue_lock);
1620229997Sken			continue;
1621229997Sken		}
1622229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1623229997Sken		if (io != NULL) {
1624229997Sken			DPRINTF("config write queue\n");
1625229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1626229997Sken				      ctl_io_hdr, links);
1627267877Smav			mtx_unlock(&be_lun->queue_lock);
1628229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1629267877Smav			mtx_lock(&be_lun->queue_lock);
1630229997Sken			continue;
1631229997Sken		}
1632275474Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1633275474Smav		if (io != NULL) {
1634275474Smav			DPRINTF("config read queue\n");
1635275474Smav			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1636275474Smav				      ctl_io_hdr, links);
1637275474Smav			mtx_unlock(&be_lun->queue_lock);
1638275474Smav			ctl_be_block_cr_dispatch(be_lun, io);
1639275474Smav			mtx_lock(&be_lun->queue_lock);
1640275474Smav			continue;
1641275474Smav		}
1642229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1643229997Sken		if (io != NULL) {
1644229997Sken			DPRINTF("input queue\n");
1645229997Sken
1646229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1647229997Sken				      ctl_io_hdr, links);
1648267877Smav			mtx_unlock(&be_lun->queue_lock);
1649229997Sken
1650229997Sken			/*
1651229997Sken			 * We must drop the lock, since this routine and
1652229997Sken			 * its children may sleep.
1653229997Sken			 */
1654229997Sken			ctl_be_block_dispatch(be_lun, io);
1655229997Sken
1656267877Smav			mtx_lock(&be_lun->queue_lock);
1657229997Sken			continue;
1658229997Sken		}
1659229997Sken
1660229997Sken		/*
1661229997Sken		 * If we get here, there is no work left in the queues, so
1662229997Sken		 * just break out and let the task queue go to sleep.
1663229997Sken		 */
1664229997Sken		break;
1665229997Sken	}
1666267877Smav	mtx_unlock(&be_lun->queue_lock);
1667229997Sken}
1668229997Sken
1669229997Sken/*
1670229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1671229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1672229997Sken * thread.
1673229997Sken */
1674229997Skenstatic int
1675229997Skenctl_be_block_submit(union ctl_io *io)
1676229997Sken{
1677229997Sken	struct ctl_be_block_lun *be_lun;
1678229997Sken	struct ctl_be_lun *ctl_be_lun;
1679229997Sken
1680229997Sken	DPRINTF("entered\n");
1681229997Sken
1682229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1683229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1684229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1685229997Sken
1686229997Sken	/*
1687229997Sken	 * Make sure we only get SCSI I/O.
1688229997Sken	 */
1689229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1690229997Sken		"%#x) encountered", io->io_hdr.io_type));
1691229997Sken
1692267519Smav	PRIV(io)->len = 0;
1693267519Smav
1694267877Smav	mtx_lock(&be_lun->queue_lock);
1695229997Sken	/*
1696229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1697229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1698229997Sken	 * or deal with resource allocation here.
1699229997Sken	 */
1700229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1701267877Smav	mtx_unlock(&be_lun->queue_lock);
1702229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1703229997Sken
1704267514Smav	return (CTL_RETVAL_COMPLETE);
1705229997Sken}
1706229997Sken
1707229997Skenstatic int
1708229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1709229997Sken			int flag, struct thread *td)
1710229997Sken{
1711229997Sken	struct ctl_be_block_softc *softc;
1712229997Sken	int error;
1713229997Sken
1714229997Sken	softc = &backend_block_softc;
1715229997Sken
1716229997Sken	error = 0;
1717229997Sken
1718229997Sken	switch (cmd) {
1719229997Sken	case CTL_LUN_REQ: {
1720229997Sken		struct ctl_lun_req *lun_req;
1721229997Sken
1722229997Sken		lun_req = (struct ctl_lun_req *)addr;
1723229997Sken
1724229997Sken		switch (lun_req->reqtype) {
1725229997Sken		case CTL_LUNREQ_CREATE:
1726229997Sken			error = ctl_be_block_create(softc, lun_req);
1727229997Sken			break;
1728229997Sken		case CTL_LUNREQ_RM:
1729229997Sken			error = ctl_be_block_rm(softc, lun_req);
1730229997Sken			break;
1731232604Strasz		case CTL_LUNREQ_MODIFY:
1732232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1733232604Strasz			break;
1734229997Sken		default:
1735229997Sken			lun_req->status = CTL_LUN_ERROR;
1736229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1737272911Smav				 "invalid LUN request type %d",
1738229997Sken				 lun_req->reqtype);
1739229997Sken			break;
1740229997Sken		}
1741229997Sken		break;
1742229997Sken	}
1743229997Sken	default:
1744229997Sken		error = ENOTTY;
1745229997Sken		break;
1746229997Sken	}
1747229997Sken
1748229997Sken	return (error);
1749229997Sken}
1750229997Sken
1751229997Skenstatic int
1752229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1753229997Sken{
1754229997Sken	struct ctl_be_block_filedata *file_data;
1755229997Sken	struct ctl_lun_create_params *params;
1756275865Smav	char			     *value;
1757229997Sken	struct vattr		      vattr;
1758275865Smav	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1759229997Sken	int			      error;
1760229997Sken
1761229997Sken	error = 0;
1762229997Sken	file_data = &be_lun->backend.file;
1763272911Smav	params = &be_lun->params;
1764229997Sken
1765229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1766229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1767229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1768275474Smav	be_lun->get_lba_status = ctl_be_block_gls_file;
1769275481Smav	be_lun->getattr = ctl_be_block_getattr_file;
1770229997Sken
1771229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1772229997Sken	if (error != 0) {
1773229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1774229997Sken			 "error calling VOP_GETATTR() for file %s",
1775229997Sken			 be_lun->dev_path);
1776229997Sken		return (error);
1777229997Sken	}
1778229997Sken
1779229997Sken	/*
1780229997Sken	 * Verify that we have the ability to upgrade to exclusive
1781229997Sken	 * access on this file so we can trap errors at open instead
1782229997Sken	 * of reporting them during first access.
1783229997Sken	 */
1784229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1785229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1786229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1787229997Sken			error = EBADF;
1788229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1789229997Sken				 "error locking file %s", be_lun->dev_path);
1790229997Sken			return (error);
1791229997Sken		}
1792229997Sken	}
1793229997Sken
1794229997Sken
1795229997Sken	file_data->cred = crhold(curthread->td_ucred);
1796232604Strasz	if (params->lun_size_bytes != 0)
1797232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1798232604Strasz	else
1799232604Strasz		be_lun->size_bytes = vattr.va_size;
1800229997Sken	/*
1801229997Sken	 * We set the multi thread flag for file operations because all
1802229997Sken	 * filesystems (in theory) are capable of allowing multiple readers
1803229997Sken	 * of a file at once.  So we want to get the maximum possible
1804229997Sken	 * concurrency.
1805229997Sken	 */
1806229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1807229997Sken
1808229997Sken	/*
1809273029Smav	 * For files we can use any logical block size.  Prefer 512 bytes
1810273029Smav	 * for compatibility reasons.  If file's vattr.va_blocksize
1811273029Smav	 * (preferred I/O block size) is bigger and multiple to chosen
1812273029Smav	 * logical block size -- report it as physical block size.
1813229997Sken	 */
1814229997Sken	if (params->blocksize_bytes != 0)
1815229997Sken		be_lun->blocksize = params->blocksize_bytes;
1816229997Sken	else
1817229997Sken		be_lun->blocksize = 512;
1818275865Smav
1819275865Smav	us = ps = vattr.va_blocksize;
1820275865Smav	uo = po = 0;
1821275865Smav
1822275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize");
1823275865Smav	if (value != NULL)
1824275865Smav		ctl_expand_number(value, &ps);
1825275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset");
1826275865Smav	if (value != NULL)
1827275865Smav		ctl_expand_number(value, &po);
1828275865Smav	pss = ps / be_lun->blocksize;
1829275865Smav	pos = po / be_lun->blocksize;
1830275865Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1831275865Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1832273029Smav		be_lun->pblockexp = fls(pss) - 1;
1833275865Smav		be_lun->pblockoff = (pss - pos) % pss;
1834273029Smav	}
1835229997Sken
1836275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize");
1837275865Smav	if (value != NULL)
1838275865Smav		ctl_expand_number(value, &us);
1839275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset");
1840275865Smav	if (value != NULL)
1841275865Smav		ctl_expand_number(value, &uo);
1842275865Smav	uss = us / be_lun->blocksize;
1843275865Smav	uos = uo / be_lun->blocksize;
1844275865Smav	if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) &&
1845275865Smav	    ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) {
1846275865Smav		be_lun->ublockexp = fls(uss) - 1;
1847275865Smav		be_lun->ublockoff = (uss - uos) % uss;
1848275865Smav	}
1849275865Smav
1850229997Sken	/*
1851229997Sken	 * Sanity check.  The media size has to be at least one
1852229997Sken	 * sector long.
1853229997Sken	 */
1854229997Sken	if (be_lun->size_bytes < be_lun->blocksize) {
1855229997Sken		error = EINVAL;
1856229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1857229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1858229997Sken			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1859229997Sken	}
1860275920Smav
1861275920Smav	be_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / be_lun->blocksize;
1862229997Sken	return (error);
1863229997Sken}
1864229997Sken
1865229997Skenstatic int
1866229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1867229997Sken{
1868229997Sken	struct ctl_lun_create_params *params;
1869229997Sken	struct vattr		      vattr;
1870229997Sken	struct cdev		     *dev;
1871229997Sken	struct cdevsw		     *devsw;
1872275865Smav	char			     *value;
1873275920Smav	int			      error, atomic, maxio;
1874275865Smav	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1875229997Sken
1876272911Smav	params = &be_lun->params;
1877229997Sken
1878229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1879229997Sken	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1880229997Sken	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1881229997Sken					     &be_lun->backend.dev.dev_ref);
1882229997Sken	if (be_lun->backend.dev.csw == NULL)
1883229997Sken		panic("Unable to retrieve device switch");
1884275474Smav	if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) {
1885269123Smav		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1886275474Smav		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1887275920Smav		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1888275920Smav	} else {
1889269123Smav		be_lun->dispatch = ctl_be_block_dispatch_dev;
1890275920Smav		atomic = 0;
1891275920Smav		maxio = be_lun->backend.dev.cdev->si_iosize_max;
1892275920Smav		if (maxio <= 0)
1893275920Smav			maxio = DFLTPHYS;
1894275920Smav		if (maxio > CTLBLK_MAX_IO_SIZE)
1895275920Smav			maxio = CTLBLK_MAX_IO_SIZE;
1896275920Smav	}
1897269123Smav	be_lun->lun_flush = ctl_be_block_flush_dev;
1898269123Smav	be_lun->unmap = ctl_be_block_unmap_dev;
1899274154Smav	be_lun->getattr = ctl_be_block_getattr_dev;
1900229997Sken
1901229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1902229997Sken	if (error) {
1903229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1904272911Smav			 "error getting vnode attributes for device %s",
1905272911Smav			 be_lun->dev_path);
1906229997Sken		return (error);
1907229997Sken	}
1908229997Sken
1909229997Sken	dev = be_lun->vn->v_rdev;
1910229997Sken	devsw = dev->si_devsw;
1911229997Sken	if (!devsw->d_ioctl) {
1912229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1913272911Smav			 "no d_ioctl for device %s!",
1914229997Sken			 be_lun->dev_path);
1915229997Sken		return (ENODEV);
1916229997Sken	}
1917229997Sken
1918229997Sken	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1919229997Sken			       (caddr_t)&be_lun->blocksize, FREAD,
1920229997Sken			       curthread);
1921229997Sken	if (error) {
1922229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1923272911Smav			 "error %d returned for DIOCGSECTORSIZE ioctl "
1924272911Smav			 "on %s!", error, be_lun->dev_path);
1925229997Sken		return (error);
1926229997Sken	}
1927229997Sken
1928229997Sken	/*
1929229997Sken	 * If the user has asked for a blocksize that is greater than the
1930229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1931229997Sken	 * the user is asking for is an even multiple of the underlying
1932229997Sken	 * device's blocksize.
1933229997Sken	 */
1934229997Sken	if ((params->blocksize_bytes != 0)
1935229997Sken	 && (params->blocksize_bytes > be_lun->blocksize)) {
1936229997Sken		uint32_t bs_multiple, tmp_blocksize;
1937229997Sken
1938229997Sken		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1939229997Sken
1940229997Sken		tmp_blocksize = bs_multiple * be_lun->blocksize;
1941229997Sken
1942229997Sken		if (tmp_blocksize == params->blocksize_bytes) {
1943229997Sken			be_lun->blocksize = params->blocksize_bytes;
1944229997Sken		} else {
1945229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1946272911Smav				 "requested blocksize %u is not an even "
1947229997Sken				 "multiple of backing device blocksize %u",
1948272911Smav				 params->blocksize_bytes,
1949229997Sken				 be_lun->blocksize);
1950229997Sken			return (EINVAL);
1951229997Sken
1952229997Sken		}
1953229997Sken	} else if ((params->blocksize_bytes != 0)
1954229997Sken		&& (params->blocksize_bytes != be_lun->blocksize)) {
1955229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1956272911Smav			 "requested blocksize %u < backing device "
1957272911Smav			 "blocksize %u", params->blocksize_bytes,
1958229997Sken			 be_lun->blocksize);
1959229997Sken		return (EINVAL);
1960229997Sken	}
1961229997Sken
1962229997Sken	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1963229997Sken			       (caddr_t)&be_lun->size_bytes, FREAD,
1964229997Sken			       curthread);
1965229997Sken	if (error) {
1966229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1967272911Smav			 "error %d returned for DIOCGMEDIASIZE "
1968272911Smav			 " ioctl on %s!", error,
1969232604Strasz			 be_lun->dev_path);
1970229997Sken		return (error);
1971229997Sken	}
1972229997Sken
1973232604Strasz	if (params->lun_size_bytes != 0) {
1974232604Strasz		if (params->lun_size_bytes > be_lun->size_bytes) {
1975232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1976272911Smav				 "requested LUN size %ju > backing device "
1977272911Smav				 "size %ju",
1978232604Strasz				 (uintmax_t)params->lun_size_bytes,
1979232604Strasz				 (uintmax_t)be_lun->size_bytes);
1980232604Strasz			return (EINVAL);
1981232604Strasz		}
1982232604Strasz
1983232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1984232604Strasz	}
1985232604Strasz
1986264191Smav	error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1987264191Smav			       (caddr_t)&ps, FREAD, curthread);
1988264191Smav	if (error)
1989264191Smav		ps = po = 0;
1990264191Smav	else {
1991264191Smav		error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1992264191Smav				       (caddr_t)&po, FREAD, curthread);
1993264191Smav		if (error)
1994264191Smav			po = 0;
1995264191Smav	}
1996275865Smav	us = ps;
1997275865Smav	uo = po;
1998275865Smav
1999275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize");
2000275865Smav	if (value != NULL)
2001275865Smav		ctl_expand_number(value, &ps);
2002275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset");
2003275865Smav	if (value != NULL)
2004275865Smav		ctl_expand_number(value, &po);
2005264191Smav	pss = ps / be_lun->blocksize;
2006264191Smav	pos = po / be_lun->blocksize;
2007264191Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
2008264191Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
2009264191Smav		be_lun->pblockexp = fls(pss) - 1;
2010264191Smav		be_lun->pblockoff = (pss - pos) % pss;
2011264191Smav	}
2012264191Smav
2013275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize");
2014275865Smav	if (value != NULL)
2015275865Smav		ctl_expand_number(value, &us);
2016275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset");
2017275865Smav	if (value != NULL)
2018275865Smav		ctl_expand_number(value, &uo);
2019275865Smav	uss = us / be_lun->blocksize;
2020275865Smav	uos = uo / be_lun->blocksize;
2021275865Smav	if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) &&
2022275865Smav	    ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) {
2023275865Smav		be_lun->ublockexp = fls(uss) - 1;
2024275865Smav		be_lun->ublockoff = (uss - uos) % uss;
2025275865Smav	}
2026275865Smav
2027275920Smav	be_lun->atomicblock = atomic / be_lun->blocksize;
2028275920Smav	be_lun->opttxferlen = maxio / be_lun->blocksize;
2029229997Sken	return (0);
2030229997Sken}
2031229997Sken
2032229997Skenstatic int
2033229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
2034229997Sken{
2035229997Sken	DROP_GIANT();
2036229997Sken	if (be_lun->vn) {
2037229997Sken		int flags = FREAD | FWRITE;
2038229997Sken
2039229997Sken		switch (be_lun->dev_type) {
2040229997Sken		case CTL_BE_BLOCK_DEV:
2041229997Sken			if (be_lun->backend.dev.csw) {
2042229997Sken				dev_relthread(be_lun->backend.dev.cdev,
2043229997Sken					      be_lun->backend.dev.dev_ref);
2044229997Sken				be_lun->backend.dev.csw  = NULL;
2045229997Sken				be_lun->backend.dev.cdev = NULL;
2046229997Sken			}
2047229997Sken			break;
2048229997Sken		case CTL_BE_BLOCK_FILE:
2049229997Sken			break;
2050229997Sken		case CTL_BE_BLOCK_NONE:
2051258871Strasz			break;
2052229997Sken		default:
2053229997Sken			panic("Unexpected backend type.");
2054229997Sken			break;
2055229997Sken		}
2056229997Sken
2057229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2058229997Sken		be_lun->vn = NULL;
2059229997Sken
2060229997Sken		switch (be_lun->dev_type) {
2061229997Sken		case CTL_BE_BLOCK_DEV:
2062229997Sken			break;
2063229997Sken		case CTL_BE_BLOCK_FILE:
2064229997Sken			if (be_lun->backend.file.cred != NULL) {
2065229997Sken				crfree(be_lun->backend.file.cred);
2066229997Sken				be_lun->backend.file.cred = NULL;
2067229997Sken			}
2068229997Sken			break;
2069229997Sken		case CTL_BE_BLOCK_NONE:
2070258871Strasz			break;
2071229997Sken		default:
2072229997Sken			panic("Unexpected backend type.");
2073229997Sken			break;
2074229997Sken		}
2075272911Smav		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2076229997Sken	}
2077229997Sken	PICKUP_GIANT();
2078229997Sken
2079229997Sken	return (0);
2080229997Sken}
2081229997Sken
2082229997Skenstatic int
2083229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
2084229997Sken		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2085229997Sken{
2086229997Sken	struct nameidata nd;
2087229997Sken	int		 flags;
2088229997Sken	int		 error;
2089229997Sken
2090229997Sken	/*
2091229997Sken	 * XXX KDM allow a read-only option?
2092229997Sken	 */
2093229997Sken	flags = FREAD | FWRITE;
2094229997Sken	error = 0;
2095229997Sken
2096229997Sken	if (rootvnode == NULL) {
2097229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2098272911Smav			 "Root filesystem is not mounted");
2099229997Sken		return (1);
2100229997Sken	}
2101229997Sken
2102229997Sken	if (!curthread->td_proc->p_fd->fd_cdir) {
2103229997Sken		curthread->td_proc->p_fd->fd_cdir = rootvnode;
2104229997Sken		VREF(rootvnode);
2105229997Sken	}
2106229997Sken	if (!curthread->td_proc->p_fd->fd_rdir) {
2107229997Sken		curthread->td_proc->p_fd->fd_rdir = rootvnode;
2108229997Sken		VREF(rootvnode);
2109229997Sken	}
2110229997Sken	if (!curthread->td_proc->p_fd->fd_jdir) {
2111229997Sken		curthread->td_proc->p_fd->fd_jdir = rootvnode;
2112229997Sken		VREF(rootvnode);
2113229997Sken	}
2114229997Sken
2115229997Sken again:
2116229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2117229997Sken	error = vn_open(&nd, &flags, 0, NULL);
2118229997Sken	if (error) {
2119229997Sken		/*
2120229997Sken		 * This is the only reasonable guess we can make as far as
2121229997Sken		 * path if the user doesn't give us a fully qualified path.
2122229997Sken		 * If they want to specify a file, they need to specify the
2123229997Sken		 * full path.
2124229997Sken		 */
2125229997Sken		if (be_lun->dev_path[0] != '/') {
2126229997Sken			char *dev_path = "/dev/";
2127229997Sken			char *dev_name;
2128229997Sken
2129229997Sken			/* Try adding device path at beginning of name */
2130229997Sken			dev_name = malloc(strlen(be_lun->dev_path)
2131229997Sken					+ strlen(dev_path) + 1,
2132229997Sken					  M_CTLBLK, M_WAITOK);
2133229997Sken			if (dev_name) {
2134229997Sken				sprintf(dev_name, "%s%s", dev_path,
2135229997Sken					be_lun->dev_path);
2136229997Sken				free(be_lun->dev_path, M_CTLBLK);
2137229997Sken				be_lun->dev_path = dev_name;
2138229997Sken				goto again;
2139229997Sken			}
2140229997Sken		}
2141229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2142272911Smav		    "error opening %s: %d", be_lun->dev_path, error);
2143229997Sken		return (error);
2144229997Sken	}
2145229997Sken
2146229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
2147229997Sken
2148229997Sken	be_lun->vn = nd.ni_vp;
2149229997Sken
2150229997Sken	/* We only support disks and files. */
2151229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
2152229997Sken		error = ctl_be_block_open_dev(be_lun, req);
2153229997Sken	} else if (be_lun->vn->v_type == VREG) {
2154229997Sken		error = ctl_be_block_open_file(be_lun, req);
2155229997Sken	} else {
2156229997Sken		error = EINVAL;
2157229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2158258871Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
2159229997Sken	}
2160229997Sken	VOP_UNLOCK(be_lun->vn, 0);
2161229997Sken
2162229997Sken	if (error != 0) {
2163229997Sken		ctl_be_block_close(be_lun);
2164229997Sken		return (error);
2165229997Sken	}
2166229997Sken
2167229997Sken	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
2168229997Sken	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
2169229997Sken
2170229997Sken	return (0);
2171229997Sken}
2172229997Sken
2173229997Skenstatic int
2174229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2175229997Sken{
2176229997Sken	struct ctl_be_block_lun *be_lun;
2177229997Sken	struct ctl_lun_create_params *params;
2178267481Smav	char num_thread_str[16];
2179229997Sken	char tmpstr[32];
2180267481Smav	char *value;
2181264274Smav	int retval, num_threads, unmap;
2182267481Smav	int tmp_num_threads;
2183229997Sken
2184229997Sken	params = &req->reqdata.create;
2185229997Sken	retval = 0;
2186272911Smav	req->status = CTL_LUN_OK;
2187229997Sken
2188229997Sken	num_threads = cbb_num_threads;
2189229997Sken
2190229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2191229997Sken
2192272911Smav	be_lun->params = req->reqdata.create;
2193229997Sken	be_lun->softc = softc;
2194229997Sken	STAILQ_INIT(&be_lun->input_queue);
2195275474Smav	STAILQ_INIT(&be_lun->config_read_queue);
2196229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
2197229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
2198229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
2199267877Smav	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
2200267877Smav	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
2201268280Smav	ctl_init_opts(&be_lun->ctl_be_lun.options,
2202268280Smav	    req->num_be_args, req->kern_be_args);
2203229997Sken
2204264886Smav	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
2205256995Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2206229997Sken
2207229997Sken	if (be_lun->lun_zone == NULL) {
2208229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2209272911Smav			 "error allocating UMA zone");
2210229997Sken		goto bailout_error;
2211229997Sken	}
2212229997Sken
2213229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2214229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
2215229997Sken	else
2216229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
2217229997Sken
2218229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
2219268280Smav		value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
2220267499Smav		if (value == NULL) {
2221229997Sken			snprintf(req->error_str, sizeof(req->error_str),
2222272911Smav				 "no file argument specified");
2223229997Sken			goto bailout_error;
2224229997Sken		}
2225267499Smav		be_lun->dev_path = strdup(value, M_CTLBLK);
2226272911Smav		be_lun->blocksize = 512;
2227272911Smav		be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
2228229997Sken
2229229997Sken		retval = ctl_be_block_open(softc, be_lun, req);
2230229997Sken		if (retval != 0) {
2231229997Sken			retval = 0;
2232272911Smav			req->status = CTL_LUN_WARNING;
2233229997Sken		}
2234229997Sken	} else {
2235229997Sken		/*
2236229997Sken		 * For processor devices, we don't have any size.
2237229997Sken		 */
2238229997Sken		be_lun->blocksize = 0;
2239264191Smav		be_lun->pblockexp = 0;
2240264191Smav		be_lun->pblockoff = 0;
2241275865Smav		be_lun->ublockexp = 0;
2242275865Smav		be_lun->ublockoff = 0;
2243229997Sken		be_lun->size_blocks = 0;
2244229997Sken		be_lun->size_bytes = 0;
2245229997Sken		be_lun->ctl_be_lun.maxlba = 0;
2246229997Sken
2247229997Sken		/*
2248229997Sken		 * Default to just 1 thread for processor devices.
2249229997Sken		 */
2250229997Sken		num_threads = 1;
2251229997Sken	}
2252229997Sken
2253229997Sken	/*
2254229997Sken	 * XXX This searching loop might be refactored to be combined with
2255229997Sken	 * the loop above,
2256229997Sken	 */
2257268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
2258267481Smav	if (value != NULL) {
2259267481Smav		tmp_num_threads = strtol(value, NULL, 0);
2260229997Sken
2261267481Smav		/*
2262267481Smav		 * We don't let the user specify less than one
2263267481Smav		 * thread, but hope he's clueful enough not to
2264267481Smav		 * specify 1000 threads.
2265267481Smav		 */
2266267481Smav		if (tmp_num_threads < 1) {
2267267481Smav			snprintf(req->error_str, sizeof(req->error_str),
2268272911Smav				 "invalid number of threads %s",
2269272911Smav				 num_thread_str);
2270267481Smav			goto bailout_error;
2271229997Sken		}
2272267481Smav		num_threads = tmp_num_threads;
2273229997Sken	}
2274274154Smav	unmap = (be_lun->dispatch == ctl_be_block_dispatch_zvol);
2275268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
2276274154Smav	if (value != NULL)
2277274154Smav		unmap = (strcmp(value, "on") == 0);
2278229997Sken
2279229997Sken	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2280229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2281272911Smav	if (be_lun->vn == NULL)
2282272911Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE;
2283264274Smav	if (unmap)
2284264274Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2285275568Smav	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2286275568Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_SERSEQ_READ;
2287229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
2288272911Smav	be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2289272911Smav	    0 : (be_lun->size_blocks - 1);
2290229997Sken	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2291264191Smav	be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2292264191Smav	be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2293275865Smav	be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp;
2294275865Smav	be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff;
2295275920Smav	be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock;
2296275920Smav	be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen;
2297229997Sken	/* Tell the user the blocksize we ended up using */
2298272911Smav	params->lun_size_bytes = be_lun->size_bytes;
2299229997Sken	params->blocksize_bytes = be_lun->blocksize;
2300229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2301229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2302229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2303229997Sken	} else
2304229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
2305229997Sken
2306229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2307229997Sken	be_lun->ctl_be_lun.lun_config_status =
2308229997Sken		ctl_be_block_lun_config_status;
2309229997Sken	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2310229997Sken
2311229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2312229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2313229997Sken			 softc->num_luns);
2314229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2315275953Smav			MIN(sizeof(be_lun->ctl_be_lun.serial_num),
2316229997Sken			sizeof(tmpstr)));
2317229997Sken
2318229997Sken		/* Tell the user what we used for a serial number */
2319229997Sken		strncpy((char *)params->serial_num, tmpstr,
2320275953Smav			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2321229997Sken	} else {
2322229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
2323229997Sken			params->serial_num,
2324275953Smav			MIN(sizeof(be_lun->ctl_be_lun.serial_num),
2325229997Sken			sizeof(params->serial_num)));
2326229997Sken	}
2327229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2328229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2329229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2330275953Smav			MIN(sizeof(be_lun->ctl_be_lun.device_id),
2331229997Sken			sizeof(tmpstr)));
2332229997Sken
2333229997Sken		/* Tell the user what we used for a device ID */
2334229997Sken		strncpy((char *)params->device_id, tmpstr,
2335275953Smav			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2336229997Sken	} else {
2337229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
2338229997Sken			params->device_id,
2339275953Smav			MIN(sizeof(be_lun->ctl_be_lun.device_id),
2340275953Smav			    sizeof(params->device_id)));
2341229997Sken	}
2342229997Sken
2343229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2344229997Sken
2345229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2346229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2347229997Sken
2348229997Sken	if (be_lun->io_taskqueue == NULL) {
2349229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2350272911Smav			 "unable to create taskqueue");
2351229997Sken		goto bailout_error;
2352229997Sken	}
2353229997Sken
2354229997Sken	/*
2355229997Sken	 * Note that we start the same number of threads by default for
2356229997Sken	 * both the file case and the block device case.  For the file
2357229997Sken	 * case, we need multiple threads to allow concurrency, because the
2358229997Sken	 * vnode interface is designed to be a blocking interface.  For the
2359229997Sken	 * block device case, ZFS zvols at least will block the caller's
2360229997Sken	 * context in many instances, and so we need multiple threads to
2361229997Sken	 * overcome that problem.  Other block devices don't need as many
2362229997Sken	 * threads, but they shouldn't cause too many problems.
2363229997Sken	 *
2364229997Sken	 * If the user wants to just have a single thread for a block
2365229997Sken	 * device, he can specify that when the LUN is created, or change
2366229997Sken	 * the tunable/sysctl to alter the default number of threads.
2367229997Sken	 */
2368229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2369229997Sken					 /*num threads*/num_threads,
2370229997Sken					 /*priority*/PWAIT,
2371229997Sken					 /*thread name*/
2372229997Sken					 "%s taskq", be_lun->lunname);
2373229997Sken
2374229997Sken	if (retval != 0)
2375229997Sken		goto bailout_error;
2376229997Sken
2377229997Sken	be_lun->num_threads = num_threads;
2378229997Sken
2379229997Sken	mtx_lock(&softc->lock);
2380229997Sken	softc->num_luns++;
2381229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2382229997Sken
2383229997Sken	mtx_unlock(&softc->lock);
2384229997Sken
2385229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
2386229997Sken	if (retval != 0) {
2387229997Sken		mtx_lock(&softc->lock);
2388229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2389229997Sken			      links);
2390229997Sken		softc->num_luns--;
2391229997Sken		mtx_unlock(&softc->lock);
2392229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2393272911Smav			 "ctl_add_lun() returned error %d, see dmesg for "
2394272911Smav			 "details", retval);
2395229997Sken		retval = 0;
2396229997Sken		goto bailout_error;
2397229997Sken	}
2398229997Sken
2399229997Sken	mtx_lock(&softc->lock);
2400229997Sken
2401229997Sken	/*
2402229997Sken	 * Tell the config_status routine that we're waiting so it won't
2403229997Sken	 * clean up the LUN in the event of an error.
2404229997Sken	 */
2405229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2406229997Sken
2407229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2408229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2409229997Sken		if (retval == EINTR)
2410229997Sken			break;
2411229997Sken	}
2412229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2413229997Sken
2414229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2415229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2416272911Smav			 "LUN configuration error, see dmesg for details");
2417229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2418229997Sken			      links);
2419229997Sken		softc->num_luns--;
2420229997Sken		mtx_unlock(&softc->lock);
2421229997Sken		goto bailout_error;
2422229997Sken	} else {
2423229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2424229997Sken	}
2425229997Sken
2426229997Sken	mtx_unlock(&softc->lock);
2427229997Sken
2428229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2429229997Sken					       be_lun->blocksize,
2430229997Sken					       DEVSTAT_ALL_SUPPORTED,
2431229997Sken					       be_lun->ctl_be_lun.lun_type
2432229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
2433229997Sken					       DEVSTAT_PRIORITY_OTHER);
2434229997Sken
2435229997Sken	return (retval);
2436229997Sken
2437229997Skenbailout_error:
2438229997Sken	req->status = CTL_LUN_ERROR;
2439229997Sken
2440267429Smav	if (be_lun->io_taskqueue != NULL)
2441267429Smav		taskqueue_free(be_lun->io_taskqueue);
2442229997Sken	ctl_be_block_close(be_lun);
2443267429Smav	if (be_lun->dev_path != NULL)
2444267429Smav		free(be_lun->dev_path, M_CTLBLK);
2445267429Smav	if (be_lun->lun_zone != NULL)
2446267429Smav		uma_zdestroy(be_lun->lun_zone);
2447268280Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2448267877Smav	mtx_destroy(&be_lun->queue_lock);
2449267877Smav	mtx_destroy(&be_lun->io_lock);
2450229997Sken	free(be_lun, M_CTLBLK);
2451229997Sken
2452229997Sken	return (retval);
2453229997Sken}
2454229997Sken
2455229997Skenstatic int
2456229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2457229997Sken{
2458229997Sken	struct ctl_lun_rm_params *params;
2459229997Sken	struct ctl_be_block_lun *be_lun;
2460229997Sken	int retval;
2461229997Sken
2462229997Sken	params = &req->reqdata.rm;
2463229997Sken
2464229997Sken	mtx_lock(&softc->lock);
2465229997Sken
2466229997Sken	be_lun = NULL;
2467229997Sken
2468229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2469229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2470229997Sken			break;
2471229997Sken	}
2472229997Sken	mtx_unlock(&softc->lock);
2473229997Sken
2474229997Sken	if (be_lun == NULL) {
2475229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2476272911Smav			 "LUN %u is not managed by the block backend",
2477272911Smav			 params->lun_id);
2478229997Sken		goto bailout_error;
2479229997Sken	}
2480229997Sken
2481229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2482229997Sken
2483229997Sken	if (retval != 0) {
2484229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2485272911Smav			 "error %d returned from ctl_disable_lun() for "
2486272911Smav			 "LUN %d", retval, params->lun_id);
2487229997Sken		goto bailout_error;
2488229997Sken
2489229997Sken	}
2490229997Sken
2491229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2492229997Sken	if (retval != 0) {
2493229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2494272911Smav			 "error %d returned from ctl_invalidate_lun() for "
2495272911Smav			 "LUN %d", retval, params->lun_id);
2496229997Sken		goto bailout_error;
2497229997Sken	}
2498229997Sken
2499229997Sken	mtx_lock(&softc->lock);
2500229997Sken
2501229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2502229997Sken
2503229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2504229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2505229997Sken                if (retval == EINTR)
2506229997Sken                        break;
2507229997Sken        }
2508229997Sken
2509229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2510229997Sken
2511229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2512229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2513272911Smav			 "interrupted waiting for LUN to be freed");
2514229997Sken		mtx_unlock(&softc->lock);
2515229997Sken		goto bailout_error;
2516229997Sken	}
2517229997Sken
2518229997Sken	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2519229997Sken
2520229997Sken	softc->num_luns--;
2521229997Sken	mtx_unlock(&softc->lock);
2522229997Sken
2523229997Sken	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2524229997Sken
2525229997Sken	taskqueue_free(be_lun->io_taskqueue);
2526229997Sken
2527229997Sken	ctl_be_block_close(be_lun);
2528229997Sken
2529229997Sken	if (be_lun->disk_stats != NULL)
2530229997Sken		devstat_remove_entry(be_lun->disk_stats);
2531229997Sken
2532229997Sken	uma_zdestroy(be_lun->lun_zone);
2533229997Sken
2534268280Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2535229997Sken	free(be_lun->dev_path, M_CTLBLK);
2536267877Smav	mtx_destroy(&be_lun->queue_lock);
2537267877Smav	mtx_destroy(&be_lun->io_lock);
2538229997Sken	free(be_lun, M_CTLBLK);
2539229997Sken
2540229997Sken	req->status = CTL_LUN_OK;
2541229997Sken
2542229997Sken	return (0);
2543229997Sken
2544229997Skenbailout_error:
2545229997Sken
2546229997Sken	req->status = CTL_LUN_ERROR;
2547229997Sken
2548229997Sken	return (0);
2549229997Sken}
2550229997Sken
2551232604Straszstatic int
2552232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2553232604Strasz			 struct ctl_lun_req *req)
2554232604Strasz{
2555232604Strasz	struct vattr vattr;
2556232604Strasz	int error;
2557272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2558232604Strasz
2559232604Strasz	if (params->lun_size_bytes != 0) {
2560232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2561232604Strasz	} else  {
2562271794Smav		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2563232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2564271794Smav		VOP_UNLOCK(be_lun->vn, 0);
2565232604Strasz		if (error != 0) {
2566232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2567232604Strasz				 "error calling VOP_GETATTR() for file %s",
2568232604Strasz				 be_lun->dev_path);
2569232604Strasz			return (error);
2570232604Strasz		}
2571232604Strasz
2572232604Strasz		be_lun->size_bytes = vattr.va_size;
2573232604Strasz	}
2574232604Strasz
2575232604Strasz	return (0);
2576232604Strasz}
2577232604Strasz
2578232604Straszstatic int
2579232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2580232604Strasz			struct ctl_lun_req *req)
2581232604Strasz{
2582271794Smav	struct ctl_be_block_devdata *dev_data;
2583232604Strasz	int error;
2584272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2585232604Strasz	uint64_t size_bytes;
2586232604Strasz
2587271794Smav	dev_data = &be_lun->backend.dev;
2588271794Smav	if (!dev_data->csw->d_ioctl) {
2589232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2590272911Smav			 "no d_ioctl for device %s!", be_lun->dev_path);
2591232604Strasz		return (ENODEV);
2592232604Strasz	}
2593232604Strasz
2594271794Smav	error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2595232604Strasz			       (caddr_t)&size_bytes, FREAD,
2596232604Strasz			       curthread);
2597232604Strasz	if (error) {
2598232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2599272911Smav			 "error %d returned for DIOCGMEDIASIZE ioctl "
2600272911Smav			 "on %s!", error, be_lun->dev_path);
2601232604Strasz		return (error);
2602232604Strasz	}
2603232604Strasz
2604232604Strasz	if (params->lun_size_bytes != 0) {
2605232604Strasz		if (params->lun_size_bytes > size_bytes) {
2606232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2607272911Smav				 "requested LUN size %ju > backing device "
2608272911Smav				 "size %ju",
2609232604Strasz				 (uintmax_t)params->lun_size_bytes,
2610232604Strasz				 (uintmax_t)size_bytes);
2611232604Strasz			return (EINVAL);
2612232604Strasz		}
2613232604Strasz
2614232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2615232604Strasz	} else {
2616232604Strasz		be_lun->size_bytes = size_bytes;
2617232604Strasz	}
2618232604Strasz
2619232604Strasz	return (0);
2620232604Strasz}
2621232604Strasz
2622232604Straszstatic int
2623232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2624232604Strasz{
2625232604Strasz	struct ctl_lun_modify_params *params;
2626232604Strasz	struct ctl_be_block_lun *be_lun;
2627271794Smav	uint64_t oldsize;
2628241896Skib	int error;
2629232604Strasz
2630232604Strasz	params = &req->reqdata.modify;
2631232604Strasz
2632232604Strasz	mtx_lock(&softc->lock);
2633232604Strasz	be_lun = NULL;
2634232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2635232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2636232604Strasz			break;
2637232604Strasz	}
2638232604Strasz	mtx_unlock(&softc->lock);
2639232604Strasz
2640232604Strasz	if (be_lun == NULL) {
2641232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2642272911Smav			 "LUN %u is not managed by the block backend",
2643272911Smav			 params->lun_id);
2644232604Strasz		goto bailout_error;
2645232604Strasz	}
2646232604Strasz
2647272911Smav	be_lun->params.lun_size_bytes = params->lun_size_bytes;
2648232604Strasz
2649274253Smav	oldsize = be_lun->size_bytes;
2650272911Smav	if (be_lun->vn == NULL)
2651272911Smav		error = ctl_be_block_open(softc, be_lun, req);
2652272911Smav	else if (be_lun->vn->v_type == VREG)
2653232604Strasz		error = ctl_be_block_modify_file(be_lun, req);
2654232604Strasz	else
2655232604Strasz		error = ctl_be_block_modify_dev(be_lun, req);
2656232604Strasz
2657274253Smav	if (error == 0 && be_lun->size_bytes != oldsize) {
2658271794Smav		be_lun->size_blocks = be_lun->size_bytes >>
2659271794Smav		    be_lun->blocksize_shift;
2660232604Strasz
2661271794Smav		/*
2662271794Smav		 * The maximum LBA is the size - 1.
2663271794Smav		 *
2664271794Smav		 * XXX: Note that this field is being updated without locking,
2665271794Smav		 * 	which might cause problems on 32-bit architectures.
2666271794Smav		 */
2667272911Smav		be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2668272911Smav		    0 : (be_lun->size_blocks - 1);
2669272911Smav		be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2670272911Smav		be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2671272911Smav		be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2672275865Smav		be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp;
2673275865Smav		be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff;
2674275920Smav		be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock;
2675275920Smav		be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen;
2676271794Smav		ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2677272911Smav		if (oldsize == 0 && be_lun->size_blocks != 0)
2678272911Smav			ctl_lun_online(&be_lun->ctl_be_lun);
2679271794Smav	}
2680232604Strasz
2681232604Strasz	/* Tell the user the exact size we ended up using */
2682232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2683232604Strasz
2684272911Smav	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2685232604Strasz
2686232604Strasz	return (0);
2687232604Strasz
2688232604Straszbailout_error:
2689232604Strasz	req->status = CTL_LUN_ERROR;
2690232604Strasz
2691232604Strasz	return (0);
2692232604Strasz}
2693232604Strasz
2694229997Skenstatic void
2695229997Skenctl_be_block_lun_shutdown(void *be_lun)
2696229997Sken{
2697229997Sken	struct ctl_be_block_lun *lun;
2698229997Sken	struct ctl_be_block_softc *softc;
2699229997Sken
2700229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2701229997Sken
2702229997Sken	softc = lun->softc;
2703229997Sken
2704229997Sken	mtx_lock(&softc->lock);
2705229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2706229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2707229997Sken		wakeup(lun);
2708229997Sken	mtx_unlock(&softc->lock);
2709229997Sken
2710229997Sken}
2711229997Sken
2712229997Skenstatic void
2713229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2714229997Sken{
2715229997Sken	struct ctl_be_block_lun *lun;
2716229997Sken	struct ctl_be_block_softc *softc;
2717229997Sken
2718229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2719229997Sken	softc = lun->softc;
2720229997Sken
2721229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2722229997Sken		mtx_lock(&softc->lock);
2723229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2724229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2725229997Sken			wakeup(lun);
2726229997Sken		mtx_unlock(&softc->lock);
2727229997Sken
2728229997Sken		/*
2729229997Sken		 * We successfully added the LUN, attempt to enable it.
2730229997Sken		 */
2731229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2732229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2733229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2734229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2735229997Sken				       __func__);
2736229997Sken			}
2737229997Sken		}
2738229997Sken
2739229997Sken		return;
2740229997Sken	}
2741229997Sken
2742229997Sken
2743229997Sken	mtx_lock(&softc->lock);
2744229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2745229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2746229997Sken	wakeup(lun);
2747229997Sken	mtx_unlock(&softc->lock);
2748229997Sken}
2749229997Sken
2750229997Sken
2751229997Skenstatic int
2752229997Skenctl_be_block_config_write(union ctl_io *io)
2753229997Sken{
2754229997Sken	struct ctl_be_block_lun *be_lun;
2755229997Sken	struct ctl_be_lun *ctl_be_lun;
2756229997Sken	int retval;
2757229997Sken
2758229997Sken	retval = 0;
2759229997Sken
2760229997Sken	DPRINTF("entered\n");
2761229997Sken
2762229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2763229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2764229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2765229997Sken
2766229997Sken	switch (io->scsiio.cdb[0]) {
2767229997Sken	case SYNCHRONIZE_CACHE:
2768229997Sken	case SYNCHRONIZE_CACHE_16:
2769264274Smav	case WRITE_SAME_10:
2770264274Smav	case WRITE_SAME_16:
2771264274Smav	case UNMAP:
2772229997Sken		/*
2773229997Sken		 * The upper level CTL code will filter out any CDBs with
2774229997Sken		 * the immediate bit set and return the proper error.
2775229997Sken		 *
2776229997Sken		 * We don't really need to worry about what LBA range the
2777229997Sken		 * user asked to be synced out.  When they issue a sync
2778229997Sken		 * cache command, we'll sync out the whole thing.
2779229997Sken		 */
2780267877Smav		mtx_lock(&be_lun->queue_lock);
2781229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2782229997Sken				   links);
2783267877Smav		mtx_unlock(&be_lun->queue_lock);
2784229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2785229997Sken		break;
2786229997Sken	case START_STOP_UNIT: {
2787229997Sken		struct scsi_start_stop_unit *cdb;
2788229997Sken
2789229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2790229997Sken
2791229997Sken		if (cdb->how & SSS_START)
2792229997Sken			retval = ctl_start_lun(ctl_be_lun);
2793229997Sken		else {
2794229997Sken			retval = ctl_stop_lun(ctl_be_lun);
2795229997Sken			/*
2796229997Sken			 * XXX KDM Copan-specific offline behavior.
2797229997Sken			 * Figure out a reasonable way to port this?
2798229997Sken			 */
2799229997Sken#ifdef NEEDTOPORT
2800229997Sken			if ((retval == 0)
2801229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2802229997Sken				retval = ctl_lun_offline(ctl_be_lun);
2803229997Sken#endif
2804229997Sken		}
2805229997Sken
2806229997Sken		/*
2807229997Sken		 * In general, the above routines should not fail.  They
2808229997Sken		 * just set state for the LUN.  So we've got something
2809229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2810229997Sken		 */
2811229997Sken		if (retval != 0) {
2812229997Sken			ctl_set_internal_failure(&io->scsiio,
2813229997Sken						 /*sks_valid*/ 1,
2814229997Sken						 /*retry_count*/ 0xf051);
2815229997Sken			retval = CTL_RETVAL_COMPLETE;
2816229997Sken		} else {
2817229997Sken			ctl_set_success(&io->scsiio);
2818229997Sken		}
2819229997Sken		ctl_config_write_done(io);
2820229997Sken		break;
2821229997Sken	}
2822229997Sken	default:
2823229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2824229997Sken		ctl_config_write_done(io);
2825229997Sken		retval = CTL_RETVAL_COMPLETE;
2826229997Sken		break;
2827229997Sken	}
2828229997Sken
2829229997Sken	return (retval);
2830229997Sken}
2831229997Sken
2832229997Skenstatic int
2833229997Skenctl_be_block_config_read(union ctl_io *io)
2834229997Sken{
2835275474Smav	struct ctl_be_block_lun *be_lun;
2836275474Smav	struct ctl_be_lun *ctl_be_lun;
2837275474Smav	int retval = 0;
2838275474Smav
2839275474Smav	DPRINTF("entered\n");
2840275474Smav
2841275474Smav	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2842275474Smav		CTL_PRIV_BACKEND_LUN].ptr;
2843275474Smav	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2844275474Smav
2845275474Smav	switch (io->scsiio.cdb[0]) {
2846275474Smav	case SERVICE_ACTION_IN:
2847275474Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2848275474Smav			mtx_lock(&be_lun->queue_lock);
2849275474Smav			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2850275474Smav			    &io->io_hdr, links);
2851275474Smav			mtx_unlock(&be_lun->queue_lock);
2852275474Smav			taskqueue_enqueue(be_lun->io_taskqueue,
2853275474Smav			    &be_lun->io_task);
2854275474Smav			retval = CTL_RETVAL_QUEUED;
2855275474Smav			break;
2856275474Smav		}
2857275474Smav		ctl_set_invalid_field(&io->scsiio,
2858275474Smav				      /*sks_valid*/ 1,
2859275474Smav				      /*command*/ 1,
2860275474Smav				      /*field*/ 1,
2861275474Smav				      /*bit_valid*/ 1,
2862275474Smav				      /*bit*/ 4);
2863275474Smav		ctl_config_read_done(io);
2864275474Smav		retval = CTL_RETVAL_COMPLETE;
2865275474Smav		break;
2866275474Smav	default:
2867275474Smav		ctl_set_invalid_opcode(&io->scsiio);
2868275474Smav		ctl_config_read_done(io);
2869275474Smav		retval = CTL_RETVAL_COMPLETE;
2870275474Smav		break;
2871275474Smav	}
2872275474Smav
2873275474Smav	return (retval);
2874229997Sken}
2875229997Sken
2876229997Skenstatic int
2877229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2878229997Sken{
2879229997Sken	struct ctl_be_block_lun *lun;
2880229997Sken	int retval;
2881229997Sken
2882229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2883229997Sken	retval = 0;
2884229997Sken
2885268283Smav	retval = sbuf_printf(sb, "\t<num_threads>");
2886229997Sken
2887229997Sken	if (retval != 0)
2888229997Sken		goto bailout;
2889229997Sken
2890229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2891229997Sken
2892229997Sken	if (retval != 0)
2893229997Sken		goto bailout;
2894229997Sken
2895268283Smav	retval = sbuf_printf(sb, "</num_threads>\n");
2896229997Sken
2897229997Skenbailout:
2898229997Sken
2899229997Sken	return (retval);
2900229997Sken}
2901229997Sken
2902274154Smavstatic uint64_t
2903274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname)
2904274154Smav{
2905274154Smav	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2906274154Smav
2907274154Smav	if (lun->getattr == NULL)
2908274154Smav		return (UINT64_MAX);
2909274154Smav	return (lun->getattr(lun, attrname));
2910274154Smav}
2911274154Smav
2912229997Skenint
2913229997Skenctl_be_block_init(void)
2914229997Sken{
2915229997Sken	struct ctl_be_block_softc *softc;
2916229997Sken	int retval;
2917229997Sken
2918229997Sken	softc = &backend_block_softc;
2919229997Sken	retval = 0;
2920229997Sken
2921267877Smav	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2922264020Strasz	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2923264020Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2924229997Sken	STAILQ_INIT(&softc->disk_list);
2925229997Sken	STAILQ_INIT(&softc->lun_list);
2926229997Sken
2927229997Sken	return (retval);
2928229997Sken}
2929