ctl_backend_block.c revision 286353
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 286353 2015-08-05 22:24:49Z 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;
227286353Smav	int				io_arg;
228229997Sken	struct ctl_be_block_softc	*softc;
229229997Sken	struct ctl_be_block_lun		*lun;
230264274Smav	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
231229997Sken};
232229997Sken
233229997Skenstatic int cbb_num_threads = 14;
234229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
235229997Sken	    "CAM Target Layer Block Backend");
236267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
237229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
238229997Sken
239229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
240229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
241229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
242229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
243229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
244229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
245229997Sken				    struct ctl_be_block_io *beio);
246229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
247229997Sken				       struct ctl_be_block_io *beio);
248275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
249275474Smav				  struct ctl_be_block_io *beio);
250275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
251275481Smav					 const char *attrname);
252229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
253229997Sken				   struct ctl_be_block_io *beio);
254264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
255264274Smav				   struct ctl_be_block_io *beio);
256229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
257229997Sken				      struct ctl_be_block_io *beio);
258274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
259274154Smav					 const char *attrname);
260275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
261275474Smav				    union ctl_io *io);
262229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
263229997Sken				    union ctl_io *io);
264229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
265229997Sken				  union ctl_io *io);
266229997Skenstatic void ctl_be_block_worker(void *context, int pending);
267229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
268229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
269229997Sken				   int flag, struct thread *td);
270229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
271229997Sken				  struct ctl_lun_req *req);
272229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
273229997Sken				 struct ctl_lun_req *req);
274229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
275229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
276229997Sken			     struct ctl_be_block_lun *be_lun,
277229997Sken			     struct ctl_lun_req *req);
278229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
279229997Sken			       struct ctl_lun_req *req);
280229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
281229997Sken			   struct ctl_lun_req *req);
282232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
283232604Strasz				  struct ctl_lun_req *req);
284232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
285232604Strasz				 struct ctl_lun_req *req);
286232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
287232604Strasz			   struct ctl_lun_req *req);
288229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
289229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
290229997Sken					   ctl_lun_config_status status);
291229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
292229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
293229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
294274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
295229997Skenint ctl_be_block_init(void);
296229997Sken
297229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
298229997Sken{
299230334Sken	.name = "block",
300230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
301230334Sken	.init = ctl_be_block_init,
302230334Sken	.data_submit = ctl_be_block_submit,
303230334Sken	.data_move_done = ctl_be_block_move_done,
304230334Sken	.config_read = ctl_be_block_config_read,
305230334Sken	.config_write = ctl_be_block_config_write,
306230334Sken	.ioctl = ctl_be_block_ioctl,
307274154Smav	.lun_info = ctl_be_block_lun_info,
308274154Smav	.lun_attr = ctl_be_block_lun_attr
309229997Sken};
310229997Sken
311229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
312229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
313229997Sken
314264020Straszstatic uma_zone_t beio_zone;
315264020Strasz
316229997Skenstatic struct ctl_be_block_io *
317229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
318229997Sken{
319229997Sken	struct ctl_be_block_io *beio;
320229997Sken
321264020Strasz	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
322264020Strasz	beio->softc = softc;
323229997Sken	return (beio);
324229997Sken}
325229997Sken
326229997Skenstatic void
327229997Skenctl_free_beio(struct ctl_be_block_io *beio)
328229997Sken{
329229997Sken	int duplicate_free;
330229997Sken	int i;
331229997Sken
332229997Sken	duplicate_free = 0;
333229997Sken
334229997Sken	for (i = 0; i < beio->num_segs; i++) {
335229997Sken		if (beio->sg_segs[i].addr == NULL)
336229997Sken			duplicate_free++;
337229997Sken
338229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
339229997Sken		beio->sg_segs[i].addr = NULL;
340267537Smav
341267537Smav		/* For compare we had two equal S/G lists. */
342267537Smav		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
343267537Smav			uma_zfree(beio->lun->lun_zone,
344267537Smav			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
345267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
346267537Smav		}
347229997Sken	}
348229997Sken
349229997Sken	if (duplicate_free > 0) {
350229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
351229997Sken		       duplicate_free, beio->num_segs);
352229997Sken	}
353229997Sken
354264020Strasz	uma_zfree(beio_zone, beio);
355229997Sken}
356229997Sken
357229997Skenstatic void
358229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
359229997Sken{
360267877Smav	union ctl_io *io = beio->io;
361229997Sken
362264274Smav	if (beio->beio_cont != NULL) {
363264274Smav		beio->beio_cont(beio);
364264274Smav	} else {
365264274Smav		ctl_free_beio(beio);
366267537Smav		ctl_data_submit_done(io);
367264274Smav	}
368229997Sken}
369229997Sken
370229997Skenstatic int
371229997Skenctl_be_block_move_done(union ctl_io *io)
372229997Sken{
373229997Sken	struct ctl_be_block_io *beio;
374229997Sken	struct ctl_be_block_lun *be_lun;
375267537Smav	struct ctl_lba_len_flags *lbalen;
376229997Sken#ifdef CTL_TIME_IO
377229997Sken	struct bintime cur_bt;
378267537Smav#endif
379267537Smav	int i;
380229997Sken
381267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
382229997Sken	be_lun = beio->lun;
383229997Sken
384229997Sken	DPRINTF("entered\n");
385229997Sken
386229997Sken#ifdef CTL_TIME_IO
387229997Sken	getbintime(&cur_bt);
388229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
389229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
390229997Sken	io->io_hdr.num_dmas++;
391229997Sken#endif
392267537Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
393229997Sken
394229997Sken	/*
395229997Sken	 * We set status at this point for read commands, and write
396229997Sken	 * commands with errors.
397229997Sken	 */
398275058Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
399275058Smav		;
400275058Smav	} else if ((io->io_hdr.port_status == 0) &&
401267537Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
402267537Smav		lbalen = ARGS(beio->io);
403267537Smav		if (lbalen->flags & CTL_LLF_READ) {
404267537Smav			ctl_set_success(&io->scsiio);
405267537Smav		} else if (lbalen->flags & CTL_LLF_COMPARE) {
406267537Smav			/* We have two data blocks ready for comparison. */
407267537Smav			for (i = 0; i < beio->num_segs; i++) {
408267537Smav				if (memcmp(beio->sg_segs[i].addr,
409267537Smav				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
410267537Smav				    beio->sg_segs[i].len) != 0)
411267537Smav					break;
412267537Smav			}
413267537Smav			if (i < beio->num_segs)
414267537Smav				ctl_set_sense(&io->scsiio,
415267537Smav				    /*current_error*/ 1,
416267537Smav				    /*sense_key*/ SSD_KEY_MISCOMPARE,
417267537Smav				    /*asc*/ 0x1D,
418267537Smav				    /*ascq*/ 0x00,
419267537Smav				    SSD_ELEM_NONE);
420267537Smav			else
421267537Smav				ctl_set_success(&io->scsiio);
422267537Smav		}
423275058Smav	} else if ((io->io_hdr.port_status != 0) &&
424275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
425275058Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
426229997Sken		/*
427229997Sken		 * For hardware error sense keys, the sense key
428229997Sken		 * specific value is defined to be a retry count,
429229997Sken		 * but we use it to pass back an internal FETD
430229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
431229997Sken		 * using 16 bits for an error code, since that's
432229997Sken		 * all the space we have in the sks field.
433229997Sken		 */
434229997Sken		ctl_set_internal_failure(&io->scsiio,
435229997Sken					 /*sks_valid*/ 1,
436229997Sken					 /*retry_count*/
437229997Sken					 io->io_hdr.port_status);
438229997Sken	}
439229997Sken
440229997Sken	/*
441229997Sken	 * If this is a read, or a write with errors, it is done.
442229997Sken	 */
443229997Sken	if ((beio->bio_cmd == BIO_READ)
444229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
445229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
446229997Sken		ctl_complete_beio(beio);
447229997Sken		return (0);
448229997Sken	}
449229997Sken
450229997Sken	/*
451229997Sken	 * At this point, we have a write and the DMA completed
452229997Sken	 * successfully.  We now have to queue it to the task queue to
453229997Sken	 * execute the backend I/O.  That is because we do blocking
454229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
455229997Sken	 * This move done routine is generally called in the SIM's
456229997Sken	 * interrupt context, and therefore we cannot block.
457229997Sken	 */
458267877Smav	mtx_lock(&be_lun->queue_lock);
459229997Sken	/*
460229997Sken	 * XXX KDM make sure that links is okay to use at this point.
461229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
462229997Sken	 * or deal with resource allocation here.
463229997Sken	 */
464229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
465267877Smav	mtx_unlock(&be_lun->queue_lock);
466229997Sken
467229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
468229997Sken
469229997Sken	return (0);
470229997Sken}
471229997Sken
472229997Skenstatic void
473229997Skenctl_be_block_biodone(struct bio *bio)
474229997Sken{
475229997Sken	struct ctl_be_block_io *beio;
476229997Sken	struct ctl_be_block_lun *be_lun;
477229997Sken	union ctl_io *io;
478261538Smav	int error;
479229997Sken
480229997Sken	beio = bio->bio_caller1;
481229997Sken	be_lun = beio->lun;
482229997Sken	io = beio->io;
483229997Sken
484229997Sken	DPRINTF("entered\n");
485229997Sken
486261538Smav	error = bio->bio_error;
487267877Smav	mtx_lock(&be_lun->io_lock);
488261538Smav	if (error != 0)
489229997Sken		beio->num_errors++;
490229997Sken
491229997Sken	beio->num_bios_done++;
492229997Sken
493229997Sken	/*
494229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
495229997Sken	 * during the free might cause it to complain.
496229997Sken	 */
497229997Sken	g_destroy_bio(bio);
498229997Sken
499229997Sken	/*
500229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
501229997Sken	 * complete, then we're done.
502229997Sken	 */
503229997Sken	if ((beio->send_complete == 0)
504229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
505267877Smav		mtx_unlock(&be_lun->io_lock);
506229997Sken		return;
507229997Sken	}
508229997Sken
509229997Sken	/*
510229997Sken	 * At this point, we've verified that we are the last I/O to
511229997Sken	 * complete, so it's safe to drop the lock.
512229997Sken	 */
513267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
514267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
515267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
516267877Smav	mtx_unlock(&be_lun->io_lock);
517229997Sken
518229997Sken	/*
519229997Sken	 * If there are any errors from the backing device, we fail the
520229997Sken	 * entire I/O with a medium error.
521229997Sken	 */
522229997Sken	if (beio->num_errors > 0) {
523261538Smav		if (error == EOPNOTSUPP) {
524261538Smav			ctl_set_invalid_opcode(&io->scsiio);
525282565Smav		} else if (error == ENOSPC || error == EDQUOT) {
526273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
527261538Smav		} else if (beio->bio_cmd == BIO_FLUSH) {
528229997Sken			/* XXX KDM is there is a better error here? */
529229997Sken			ctl_set_internal_failure(&io->scsiio,
530229997Sken						 /*sks_valid*/ 1,
531229997Sken						 /*retry_count*/ 0xbad2);
532229997Sken		} else
533229997Sken			ctl_set_medium_error(&io->scsiio);
534229997Sken		ctl_complete_beio(beio);
535229997Sken		return;
536229997Sken	}
537229997Sken
538229997Sken	/*
539267537Smav	 * If this is a write, a flush, a delete or verify, we're all done.
540229997Sken	 * If this is a read, we can now send the data to the user.
541229997Sken	 */
542229997Sken	if ((beio->bio_cmd == BIO_WRITE)
543264274Smav	 || (beio->bio_cmd == BIO_FLUSH)
544267537Smav	 || (beio->bio_cmd == BIO_DELETE)
545267537Smav	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
546229997Sken		ctl_set_success(&io->scsiio);
547229997Sken		ctl_complete_beio(beio);
548229997Sken	} else {
549275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
550275058Smav		    beio->beio_cont == NULL)
551275058Smav			ctl_set_success(&io->scsiio);
552229997Sken#ifdef CTL_TIME_IO
553229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
554229997Sken#endif
555229997Sken		ctl_datamove(io);
556229997Sken	}
557229997Sken}
558229997Sken
559229997Skenstatic void
560229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
561229997Sken			struct ctl_be_block_io *beio)
562229997Sken{
563267877Smav	union ctl_io *io = beio->io;
564229997Sken	struct mount *mountpoint;
565241896Skib	int error, lock_flags;
566229997Sken
567229997Sken	DPRINTF("entered\n");
568229997Sken
569267877Smav	binuptime(&beio->ds_t0);
570267877Smav	mtx_lock(&be_lun->io_lock);
571267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
572267877Smav	mtx_unlock(&be_lun->io_lock);
573229997Sken
574267877Smav	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
575229997Sken
576229997Sken	if (MNT_SHARED_WRITES(mountpoint)
577229997Sken	 || ((mountpoint == NULL)
578229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
579229997Sken		lock_flags = LK_SHARED;
580229997Sken	else
581229997Sken		lock_flags = LK_EXCLUSIVE;
582229997Sken
583229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
584229997Sken
585286353Smav	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
586286353Smav	    curthread);
587229997Sken	VOP_UNLOCK(be_lun->vn, 0);
588229997Sken
589229997Sken	vn_finished_write(mountpoint);
590229997Sken
591267877Smav	mtx_lock(&be_lun->io_lock);
592267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
593267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
594267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
595267877Smav	mtx_unlock(&be_lun->io_lock);
596267877Smav
597229997Sken	if (error == 0)
598229997Sken		ctl_set_success(&io->scsiio);
599229997Sken	else {
600229997Sken		/* XXX KDM is there is a better error here? */
601229997Sken		ctl_set_internal_failure(&io->scsiio,
602229997Sken					 /*sks_valid*/ 1,
603229997Sken					 /*retry_count*/ 0xbad1);
604229997Sken	}
605229997Sken
606229997Sken	ctl_complete_beio(beio);
607229997Sken}
608229997Sken
609258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
610258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
611258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
612258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
613229997Sken
614229997Skenstatic void
615229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
616229997Sken			   struct ctl_be_block_io *beio)
617229997Sken{
618229997Sken	struct ctl_be_block_filedata *file_data;
619229997Sken	union ctl_io *io;
620229997Sken	struct uio xuio;
621229997Sken	struct iovec *xiovec;
622241896Skib	int flags;
623229997Sken	int error, i;
624229997Sken
625229997Sken	DPRINTF("entered\n");
626229997Sken
627229997Sken	file_data = &be_lun->backend.file;
628229997Sken	io = beio->io;
629271309Smav	flags = 0;
630271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
631271309Smav		flags |= IO_DIRECT;
632271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
633271309Smav		flags |= IO_SYNC;
634229997Sken
635267537Smav	bzero(&xuio, sizeof(xuio));
636229997Sken	if (beio->bio_cmd == BIO_READ) {
637229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
638267537Smav		xuio.uio_rw = UIO_READ;
639229997Sken	} else {
640229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
641267537Smav		xuio.uio_rw = UIO_WRITE;
642229997Sken	}
643229997Sken	xuio.uio_offset = beio->io_offset;
644229997Sken	xuio.uio_resid = beio->io_len;
645229997Sken	xuio.uio_segflg = UIO_SYSSPACE;
646229997Sken	xuio.uio_iov = beio->xiovecs;
647229997Sken	xuio.uio_iovcnt = beio->num_segs;
648229997Sken	xuio.uio_td = curthread;
649229997Sken
650229997Sken	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
651229997Sken		xiovec->iov_base = beio->sg_segs[i].addr;
652229997Sken		xiovec->iov_len = beio->sg_segs[i].len;
653229997Sken	}
654229997Sken
655267877Smav	binuptime(&beio->ds_t0);
656267877Smav	mtx_lock(&be_lun->io_lock);
657267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
658267877Smav	mtx_unlock(&be_lun->io_lock);
659267877Smav
660229997Sken	if (beio->bio_cmd == BIO_READ) {
661229997Sken		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
662229997Sken
663229997Sken		/*
664229997Sken		 * UFS pays attention to IO_DIRECT for reads.  If the
665229997Sken		 * DIRECTIO option is configured into the kernel, it calls
666229997Sken		 * ffs_rawread().  But that only works for single-segment
667229997Sken		 * uios with user space addresses.  In our case, with a
668229997Sken		 * kernel uio, it still reads into the buffer cache, but it
669229997Sken		 * will just try to release the buffer from the cache later
670229997Sken		 * on in ffs_read().
671229997Sken		 *
672229997Sken		 * ZFS does not pay attention to IO_DIRECT for reads.
673229997Sken		 *
674229997Sken		 * UFS does not pay attention to IO_SYNC for reads.
675229997Sken		 *
676229997Sken		 * ZFS pays attention to IO_SYNC (which translates into the
677229997Sken		 * Solaris define FRSYNC for zfs_read()) for reads.  It
678229997Sken		 * attempts to sync the file before reading.
679229997Sken		 */
680271309Smav		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
681229997Sken
682229997Sken		VOP_UNLOCK(be_lun->vn, 0);
683267537Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
684229997Sken	} else {
685229997Sken		struct mount *mountpoint;
686229997Sken		int lock_flags;
687229997Sken
688229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
689229997Sken
690229997Sken		if (MNT_SHARED_WRITES(mountpoint)
691229997Sken		 || ((mountpoint == NULL)
692229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
693229997Sken			lock_flags = LK_SHARED;
694229997Sken		else
695229997Sken			lock_flags = LK_EXCLUSIVE;
696229997Sken
697229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
698229997Sken
699229997Sken		/*
700229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
701229997Sken		 * is done asynchronously.  (Normally the write would just
702229997Sken		 * get put into cache.
703229997Sken		 *
704229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
705229997Sken		 * attempt to write the buffer out synchronously if that
706229997Sken		 * flag is set.
707229997Sken		 *
708229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
709229997Sken		 *
710229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
711229997Sken		 * for writes.  It will flush the transaction from the
712229997Sken		 * cache before returning.
713229997Sken		 */
714271309Smav		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
715229997Sken		VOP_UNLOCK(be_lun->vn, 0);
716229997Sken
717229997Sken		vn_finished_write(mountpoint);
718267537Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
719229997Sken        }
720229997Sken
721267877Smav	mtx_lock(&be_lun->io_lock);
722267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
723267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
724267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
725267877Smav	mtx_unlock(&be_lun->io_lock);
726267877Smav
727229997Sken	/*
728229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
729229997Sken	 * return the I/O to the user.
730229997Sken	 */
731229997Sken	if (error != 0) {
732229997Sken		char path_str[32];
733229997Sken
734229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
735229997Sken		printf("%s%s command returned errno %d\n", path_str,
736229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
737282565Smav		if (error == ENOSPC || error == EDQUOT) {
738273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
739273809Smav		} else
740273809Smav			ctl_set_medium_error(&io->scsiio);
741229997Sken		ctl_complete_beio(beio);
742229997Sken		return;
743229997Sken	}
744229997Sken
745229997Sken	/*
746269122Smav	 * If this is a write or a verify, we're all done.
747229997Sken	 * If this is a read, we can now send the data to the user.
748229997Sken	 */
749269122Smav	if ((beio->bio_cmd == BIO_WRITE) ||
750269122Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
751229997Sken		ctl_set_success(&io->scsiio);
752229997Sken		ctl_complete_beio(beio);
753229997Sken	} else {
754275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
755275058Smav		    beio->beio_cont == NULL)
756275058Smav			ctl_set_success(&io->scsiio);
757229997Sken#ifdef CTL_TIME_IO
758229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
759229997Sken#endif
760229997Sken		ctl_datamove(io);
761229997Sken	}
762229997Sken}
763229997Sken
764229997Skenstatic void
765275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
766275474Smav			struct ctl_be_block_io *beio)
767275474Smav{
768275474Smav	union ctl_io *io = beio->io;
769275474Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
770275474Smav	struct scsi_get_lba_status_data *data;
771275474Smav	off_t roff, off;
772275474Smav	int error, status;
773275474Smav
774275474Smav	DPRINTF("entered\n");
775275474Smav
776275474Smav	off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift;
777275474Smav	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
778275474Smav	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
779275474Smav	    0, curthread->td_ucred, curthread);
780275474Smav	if (error == 0 && off > roff)
781275474Smav		status = 0;	/* mapped up to off */
782275474Smav	else {
783275474Smav		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
784275474Smav		    0, curthread->td_ucred, curthread);
785275474Smav		if (error == 0 && off > roff)
786275474Smav			status = 1;	/* deallocated up to off */
787275474Smav		else {
788275474Smav			status = 0;	/* unknown up to the end */
789275474Smav			off = be_lun->size_bytes;
790275474Smav		}
791275474Smav	}
792275474Smav	VOP_UNLOCK(be_lun->vn, 0);
793275474Smav
794275474Smav	off >>= be_lun->blocksize_shift;
795275474Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
796275474Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
797275474Smav	scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba),
798275474Smav	    data->descr[0].length);
799275474Smav	data->descr[0].status = status;
800275474Smav
801275474Smav	ctl_complete_beio(beio);
802275474Smav}
803275474Smav
804275481Smavstatic uint64_t
805275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
806275481Smav{
807275481Smav	struct vattr		vattr;
808275481Smav	struct statfs		statfs;
809285030Smav	uint64_t		val;
810275481Smav	int			error;
811275481Smav
812285030Smav	val = UINT64_MAX;
813275481Smav	if (be_lun->vn == NULL)
814285030Smav		return (val);
815285030Smav	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
816275481Smav	if (strcmp(attrname, "blocksused") == 0) {
817275481Smav		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
818285030Smav		if (error == 0)
819285030Smav			val = vattr.va_bytes >> be_lun->blocksize_shift;
820275481Smav	}
821285030Smav	if (strcmp(attrname, "blocksavail") == 0 &&
822285030Smav	    (be_lun->vn->v_iflag & VI_DOOMED) == 0) {
823275481Smav		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
824285030Smav		if (error == 0)
825285030Smav			val = (statfs.f_bavail * statfs.f_bsize) >>
826285030Smav			    be_lun->blocksize_shift;
827275481Smav	}
828285030Smav	VOP_UNLOCK(be_lun->vn, 0);
829285030Smav	return (val);
830275481Smav}
831275481Smav
832275474Smavstatic void
833269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
834269123Smav			   struct ctl_be_block_io *beio)
835269123Smav{
836269123Smav	struct ctl_be_block_devdata *dev_data;
837269123Smav	union ctl_io *io;
838269123Smav	struct uio xuio;
839269123Smav	struct iovec *xiovec;
840269123Smav	int flags;
841269123Smav	int error, i;
842269123Smav
843269123Smav	DPRINTF("entered\n");
844269123Smav
845269123Smav	dev_data = &be_lun->backend.dev;
846269123Smav	io = beio->io;
847271309Smav	flags = 0;
848271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
849271309Smav		flags |= IO_DIRECT;
850271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
851271309Smav		flags |= IO_SYNC;
852269123Smav
853269123Smav	bzero(&xuio, sizeof(xuio));
854269123Smav	if (beio->bio_cmd == BIO_READ) {
855269123Smav		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
856269123Smav		xuio.uio_rw = UIO_READ;
857269123Smav	} else {
858269123Smav		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
859269123Smav		xuio.uio_rw = UIO_WRITE;
860269123Smav	}
861269123Smav	xuio.uio_offset = beio->io_offset;
862269123Smav	xuio.uio_resid = beio->io_len;
863269123Smav	xuio.uio_segflg = UIO_SYSSPACE;
864269123Smav	xuio.uio_iov = beio->xiovecs;
865269123Smav	xuio.uio_iovcnt = beio->num_segs;
866269123Smav	xuio.uio_td = curthread;
867269123Smav
868269123Smav	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
869269123Smav		xiovec->iov_base = beio->sg_segs[i].addr;
870269123Smav		xiovec->iov_len = beio->sg_segs[i].len;
871269123Smav	}
872269123Smav
873269123Smav	binuptime(&beio->ds_t0);
874269123Smav	mtx_lock(&be_lun->io_lock);
875269123Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
876269123Smav	mtx_unlock(&be_lun->io_lock);
877269123Smav
878269123Smav	if (beio->bio_cmd == BIO_READ) {
879271309Smav		error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
880269123Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
881269123Smav	} else {
882271309Smav		error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
883269123Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
884269123Smav	}
885269123Smav
886269123Smav	mtx_lock(&be_lun->io_lock);
887269123Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
888269123Smav	    beio->ds_tag_type, beio->ds_trans_type,
889269123Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
890269123Smav	mtx_unlock(&be_lun->io_lock);
891269123Smav
892269123Smav	/*
893269123Smav	 * If we got an error, set the sense data to "MEDIUM ERROR" and
894269123Smav	 * return the I/O to the user.
895269123Smav	 */
896269123Smav	if (error != 0) {
897282565Smav		if (error == ENOSPC || error == EDQUOT) {
898273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
899273809Smav		} else
900273809Smav			ctl_set_medium_error(&io->scsiio);
901269123Smav		ctl_complete_beio(beio);
902269123Smav		return;
903269123Smav	}
904269123Smav
905269123Smav	/*
906269123Smav	 * If this is a write or a verify, we're all done.
907269123Smav	 * If this is a read, we can now send the data to the user.
908269123Smav	 */
909269123Smav	if ((beio->bio_cmd == BIO_WRITE) ||
910269123Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
911269123Smav		ctl_set_success(&io->scsiio);
912269123Smav		ctl_complete_beio(beio);
913269123Smav	} else {
914275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
915275058Smav		    beio->beio_cont == NULL)
916275058Smav			ctl_set_success(&io->scsiio);
917269123Smav#ifdef CTL_TIME_IO
918269123Smav        	getbintime(&io->io_hdr.dma_start_bt);
919269123Smav#endif
920269123Smav		ctl_datamove(io);
921269123Smav	}
922269123Smav}
923269123Smav
924269123Smavstatic void
925275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
926275474Smav			struct ctl_be_block_io *beio)
927275474Smav{
928275474Smav	struct ctl_be_block_devdata *dev_data = &be_lun->backend.dev;
929275474Smav	union ctl_io *io = beio->io;
930275474Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
931275474Smav	struct scsi_get_lba_status_data *data;
932275474Smav	off_t roff, off;
933275474Smav	int error, status;
934275474Smav
935275474Smav	DPRINTF("entered\n");
936275474Smav
937275474Smav	off = roff = ((off_t)lbalen->lba) << be_lun->blocksize_shift;
938275474Smav	error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKHOLE,
939275474Smav	    (caddr_t)&off, FREAD, curthread);
940275474Smav	if (error == 0 && off > roff)
941275474Smav		status = 0;	/* mapped up to off */
942275474Smav	else {
943275474Smav		error = (*dev_data->csw->d_ioctl)(dev_data->cdev, FIOSEEKDATA,
944275474Smav		    (caddr_t)&off, FREAD, curthread);
945275474Smav		if (error == 0 && off > roff)
946275474Smav			status = 1;	/* deallocated up to off */
947275474Smav		else {
948275474Smav			status = 0;	/* unknown up to the end */
949275474Smav			off = be_lun->size_bytes;
950275474Smav		}
951275474Smav	}
952275474Smav
953275474Smav	off >>= be_lun->blocksize_shift;
954275474Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
955275474Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
956275474Smav	scsi_ulto4b(MIN(UINT32_MAX, off - lbalen->lba),
957275474Smav	    data->descr[0].length);
958275474Smav	data->descr[0].status = status;
959275474Smav
960275474Smav	ctl_complete_beio(beio);
961275474Smav}
962275474Smav
963275474Smavstatic void
964229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
965229997Sken		       struct ctl_be_block_io *beio)
966229997Sken{
967229997Sken	struct bio *bio;
968229997Sken	union ctl_io *io;
969229997Sken	struct ctl_be_block_devdata *dev_data;
970229997Sken
971229997Sken	dev_data = &be_lun->backend.dev;
972229997Sken	io = beio->io;
973229997Sken
974229997Sken	DPRINTF("entered\n");
975229997Sken
976229997Sken	/* This can't fail, it's a blocking allocation. */
977229997Sken	bio = g_alloc_bio();
978229997Sken
979229997Sken	bio->bio_cmd	    = BIO_FLUSH;
980229997Sken	bio->bio_dev	    = dev_data->cdev;
981229997Sken	bio->bio_offset	    = 0;
982229997Sken	bio->bio_data	    = 0;
983229997Sken	bio->bio_done	    = ctl_be_block_biodone;
984229997Sken	bio->bio_caller1    = beio;
985229997Sken	bio->bio_pblkno	    = 0;
986229997Sken
987229997Sken	/*
988229997Sken	 * We don't need to acquire the LUN lock here, because we are only
989229997Sken	 * sending one bio, and so there is no other context to synchronize
990229997Sken	 * with.
991229997Sken	 */
992229997Sken	beio->num_bios_sent = 1;
993229997Sken	beio->send_complete = 1;
994229997Sken
995229997Sken	binuptime(&beio->ds_t0);
996267877Smav	mtx_lock(&be_lun->io_lock);
997229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
998267877Smav	mtx_unlock(&be_lun->io_lock);
999229997Sken
1000229997Sken	(*dev_data->csw->d_strategy)(bio);
1001229997Sken}
1002229997Sken
1003229997Skenstatic void
1004264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1005264274Smav		       struct ctl_be_block_io *beio,
1006264274Smav		       uint64_t off, uint64_t len, int last)
1007264274Smav{
1008264274Smav	struct bio *bio;
1009264274Smav	struct ctl_be_block_devdata *dev_data;
1010264296Smav	uint64_t maxlen;
1011264274Smav
1012264274Smav	dev_data = &be_lun->backend.dev;
1013264296Smav	maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
1014264274Smav	while (len > 0) {
1015264274Smav		bio = g_alloc_bio();
1016264274Smav		bio->bio_cmd	    = BIO_DELETE;
1017264274Smav		bio->bio_dev	    = dev_data->cdev;
1018264274Smav		bio->bio_offset	    = off;
1019264296Smav		bio->bio_length	    = MIN(len, maxlen);
1020264274Smav		bio->bio_data	    = 0;
1021264274Smav		bio->bio_done	    = ctl_be_block_biodone;
1022264274Smav		bio->bio_caller1    = beio;
1023264296Smav		bio->bio_pblkno     = off / be_lun->blocksize;
1024264274Smav
1025264274Smav		off += bio->bio_length;
1026264274Smav		len -= bio->bio_length;
1027264274Smav
1028267877Smav		mtx_lock(&be_lun->io_lock);
1029264274Smav		beio->num_bios_sent++;
1030264274Smav		if (last && len == 0)
1031264274Smav			beio->send_complete = 1;
1032267877Smav		mtx_unlock(&be_lun->io_lock);
1033264274Smav
1034264274Smav		(*dev_data->csw->d_strategy)(bio);
1035264274Smav	}
1036264274Smav}
1037264274Smav
1038264274Smavstatic void
1039264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1040264274Smav		       struct ctl_be_block_io *beio)
1041264274Smav{
1042264274Smav	union ctl_io *io;
1043264274Smav	struct ctl_be_block_devdata *dev_data;
1044267515Smav	struct ctl_ptr_len_flags *ptrlen;
1045264274Smav	struct scsi_unmap_desc *buf, *end;
1046264274Smav	uint64_t len;
1047264274Smav
1048264274Smav	dev_data = &be_lun->backend.dev;
1049264274Smav	io = beio->io;
1050264274Smav
1051264274Smav	DPRINTF("entered\n");
1052264274Smav
1053264274Smav	binuptime(&beio->ds_t0);
1054267877Smav	mtx_lock(&be_lun->io_lock);
1055264274Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1056267877Smav	mtx_unlock(&be_lun->io_lock);
1057264274Smav
1058264274Smav	if (beio->io_offset == -1) {
1059264274Smav		beio->io_len = 0;
1060267515Smav		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1061267515Smav		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1062267515Smav		end = buf + ptrlen->len / sizeof(*buf);
1063264274Smav		for (; buf < end; buf++) {
1064264274Smav			len = (uint64_t)scsi_4btoul(buf->length) *
1065264274Smav			    be_lun->blocksize;
1066264274Smav			beio->io_len += len;
1067264274Smav			ctl_be_block_unmap_dev_range(be_lun, beio,
1068264274Smav			    scsi_8btou64(buf->lba) * be_lun->blocksize, len,
1069264283Smav			    (end - buf < 2) ? TRUE : FALSE);
1070264274Smav		}
1071264274Smav	} else
1072264274Smav		ctl_be_block_unmap_dev_range(be_lun, beio,
1073264274Smav		    beio->io_offset, beio->io_len, TRUE);
1074264274Smav}
1075264274Smav
1076264274Smavstatic void
1077229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1078229997Sken			  struct ctl_be_block_io *beio)
1079229997Sken{
1080267877Smav	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1081229997Sken	int i;
1082229997Sken	struct bio *bio;
1083229997Sken	struct ctl_be_block_devdata *dev_data;
1084229997Sken	off_t cur_offset;
1085229997Sken	int max_iosize;
1086229997Sken
1087229997Sken	DPRINTF("entered\n");
1088229997Sken
1089229997Sken	dev_data = &be_lun->backend.dev;
1090229997Sken
1091229997Sken	/*
1092229997Sken	 * We have to limit our I/O size to the maximum supported by the
1093229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
1094229997Sken	 * set it properly, use DFLTPHYS.
1095229997Sken	 */
1096229997Sken	max_iosize = dev_data->cdev->si_iosize_max;
1097229997Sken	if (max_iosize < PAGE_SIZE)
1098229997Sken		max_iosize = DFLTPHYS;
1099229997Sken
1100229997Sken	cur_offset = beio->io_offset;
1101229997Sken	for (i = 0; i < beio->num_segs; i++) {
1102229997Sken		size_t cur_size;
1103229997Sken		uint8_t *cur_ptr;
1104229997Sken
1105229997Sken		cur_size = beio->sg_segs[i].len;
1106229997Sken		cur_ptr = beio->sg_segs[i].addr;
1107229997Sken
1108229997Sken		while (cur_size > 0) {
1109229997Sken			/* This can't fail, it's a blocking allocation. */
1110229997Sken			bio = g_alloc_bio();
1111229997Sken
1112229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1113229997Sken
1114229997Sken			bio->bio_cmd = beio->bio_cmd;
1115229997Sken			bio->bio_dev = dev_data->cdev;
1116229997Sken			bio->bio_caller1 = beio;
1117229997Sken			bio->bio_length = min(cur_size, max_iosize);
1118229997Sken			bio->bio_offset = cur_offset;
1119229997Sken			bio->bio_data = cur_ptr;
1120229997Sken			bio->bio_done = ctl_be_block_biodone;
1121229997Sken			bio->bio_pblkno = cur_offset / be_lun->blocksize;
1122229997Sken
1123229997Sken			cur_offset += bio->bio_length;
1124229997Sken			cur_ptr += bio->bio_length;
1125229997Sken			cur_size -= bio->bio_length;
1126229997Sken
1127267877Smav			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1128229997Sken			beio->num_bios_sent++;
1129229997Sken		}
1130229997Sken	}
1131267877Smav	binuptime(&beio->ds_t0);
1132267877Smav	mtx_lock(&be_lun->io_lock);
1133267877Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1134267877Smav	beio->send_complete = 1;
1135267877Smav	mtx_unlock(&be_lun->io_lock);
1136267877Smav
1137267877Smav	/*
1138267877Smav	 * Fire off all allocated requests!
1139267877Smav	 */
1140267877Smav	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1141267877Smav		TAILQ_REMOVE(&queue, bio, bio_queue);
1142267877Smav		(*dev_data->csw->d_strategy)(bio);
1143267877Smav	}
1144229997Sken}
1145229997Sken
1146274154Smavstatic uint64_t
1147274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1148274154Smav{
1149274154Smav	struct ctl_be_block_devdata	*dev_data = &be_lun->backend.dev;
1150274154Smav	struct diocgattr_arg	arg;
1151274154Smav	int			error;
1152274154Smav
1153274154Smav	if (dev_data->csw == NULL || dev_data->csw->d_ioctl == NULL)
1154274154Smav		return (UINT64_MAX);
1155274154Smav	strlcpy(arg.name, attrname, sizeof(arg.name));
1156274154Smav	arg.len = sizeof(arg.value.off);
1157274154Smav	error = dev_data->csw->d_ioctl(dev_data->cdev,
1158274154Smav	    DIOCGATTR, (caddr_t)&arg, FREAD, curthread);
1159274154Smav	if (error != 0)
1160274154Smav		return (UINT64_MAX);
1161274154Smav	return (arg.value.off);
1162274154Smav}
1163274154Smav
1164229997Skenstatic void
1165286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1166286353Smav			    union ctl_io *io)
1167286353Smav{
1168286353Smav	struct ctl_be_block_io *beio;
1169286353Smav	struct ctl_lba_len_flags *lbalen;
1170286353Smav
1171286353Smav	DPRINTF("entered\n");
1172286353Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1173286353Smav	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1174286353Smav
1175286353Smav	beio->io_len = lbalen->len * be_lun->blocksize;
1176286353Smav	beio->io_offset = lbalen->lba * be_lun->blocksize;
1177286353Smav	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
1178286353Smav	beio->bio_cmd = BIO_FLUSH;
1179286353Smav	beio->ds_trans_type = DEVSTAT_NO_DATA;
1180286353Smav	DPRINTF("SYNC\n");
1181286353Smav	be_lun->lun_flush(be_lun, beio);
1182286353Smav}
1183286353Smav
1184286353Smavstatic void
1185264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1186264274Smav{
1187264274Smav	union ctl_io *io;
1188264274Smav
1189264274Smav	io = beio->io;
1190264274Smav	ctl_free_beio(beio);
1191267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1192267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1193267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1194264274Smav		ctl_config_write_done(io);
1195264274Smav		return;
1196264274Smav	}
1197264274Smav
1198264274Smav	ctl_be_block_config_write(io);
1199264274Smav}
1200264274Smav
1201264274Smavstatic void
1202264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1203264274Smav			    union ctl_io *io)
1204264274Smav{
1205264274Smav	struct ctl_be_block_io *beio;
1206267515Smav	struct ctl_lba_len_flags *lbalen;
1207278625Smav	uint64_t len_left, lba;
1208278625Smav	uint32_t pb, pbo, adj;
1209264274Smav	int i, seglen;
1210264274Smav	uint8_t *buf, *end;
1211264274Smav
1212264274Smav	DPRINTF("entered\n");
1213264274Smav
1214267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1215267537Smav	lbalen = ARGS(beio->io);
1216264274Smav
1217271839Smav	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1218269622Smav	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1219264274Smav		ctl_free_beio(beio);
1220264274Smav		ctl_set_invalid_field(&io->scsiio,
1221264274Smav				      /*sks_valid*/ 1,
1222264274Smav				      /*command*/ 1,
1223264274Smav				      /*field*/ 1,
1224264274Smav				      /*bit_valid*/ 0,
1225264274Smav				      /*bit*/ 0);
1226264274Smav		ctl_config_write_done(io);
1227264274Smav		return;
1228264274Smav	}
1229264274Smav
1230269622Smav	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1231267515Smav		beio->io_offset = lbalen->lba * be_lun->blocksize;
1232267515Smav		beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1233264274Smav		beio->bio_cmd = BIO_DELETE;
1234264274Smav		beio->ds_trans_type = DEVSTAT_FREE;
1235264274Smav
1236264274Smav		be_lun->unmap(be_lun, beio);
1237264274Smav		return;
1238264274Smav	}
1239264274Smav
1240264274Smav	beio->bio_cmd = BIO_WRITE;
1241264274Smav	beio->ds_trans_type = DEVSTAT_WRITE;
1242264274Smav
1243264274Smav	DPRINTF("WRITE SAME at LBA %jx len %u\n",
1244267515Smav	       (uintmax_t)lbalen->lba, lbalen->len);
1245264274Smav
1246278625Smav	pb = be_lun->blocksize << be_lun->pblockexp;
1247278625Smav	if (be_lun->pblockoff > 0)
1248278625Smav		pbo = pb - be_lun->blocksize * be_lun->pblockoff;
1249278625Smav	else
1250278625Smav		pbo = 0;
1251267515Smav	len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1252264274Smav	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1253264274Smav
1254264274Smav		/*
1255264274Smav		 * Setup the S/G entry for this chunk.
1256264274Smav		 */
1257264886Smav		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1258278619Smav		if (pb > be_lun->blocksize) {
1259278619Smav			adj = ((lbalen->lba + lba) * be_lun->blocksize +
1260278619Smav			    seglen - pbo) % pb;
1261278619Smav			if (seglen > adj)
1262278619Smav				seglen -= adj;
1263278619Smav			else
1264278619Smav				seglen -= seglen % be_lun->blocksize;
1265278619Smav		} else
1266278619Smav			seglen -= seglen % be_lun->blocksize;
1267264274Smav		beio->sg_segs[i].len = seglen;
1268264274Smav		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1269264274Smav
1270264274Smav		DPRINTF("segment %d addr %p len %zd\n", i,
1271264274Smav			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1272264274Smav
1273264274Smav		beio->num_segs++;
1274264274Smav		len_left -= seglen;
1275264274Smav
1276264274Smav		buf = beio->sg_segs[i].addr;
1277264274Smav		end = buf + seglen;
1278264274Smav		for (; buf < end; buf += be_lun->blocksize) {
1279264274Smav			memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1280267515Smav			if (lbalen->flags & SWS_LBDATA)
1281267515Smav				scsi_ulto4b(lbalen->lba + lba, buf);
1282264274Smav			lba++;
1283264274Smav		}
1284264274Smav	}
1285264274Smav
1286267515Smav	beio->io_offset = lbalen->lba * be_lun->blocksize;
1287264274Smav	beio->io_len = lba * be_lun->blocksize;
1288264274Smav
1289264274Smav	/* We can not do all in one run. Correct and schedule rerun. */
1290264274Smav	if (len_left > 0) {
1291267515Smav		lbalen->lba += lba;
1292267515Smav		lbalen->len -= lba;
1293264274Smav		beio->beio_cont = ctl_be_block_cw_done_ws;
1294264274Smav	}
1295264274Smav
1296264274Smav	be_lun->dispatch(be_lun, beio);
1297264274Smav}
1298264274Smav
1299264274Smavstatic void
1300264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1301264274Smav			    union ctl_io *io)
1302264274Smav{
1303264274Smav	struct ctl_be_block_io *beio;
1304267515Smav	struct ctl_ptr_len_flags *ptrlen;
1305264274Smav
1306264274Smav	DPRINTF("entered\n");
1307264274Smav
1308267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1309267515Smav	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1310264274Smav
1311269622Smav	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1312264274Smav		ctl_free_beio(beio);
1313264274Smav		ctl_set_invalid_field(&io->scsiio,
1314264274Smav				      /*sks_valid*/ 0,
1315264274Smav				      /*command*/ 1,
1316264274Smav				      /*field*/ 0,
1317264274Smav				      /*bit_valid*/ 0,
1318264274Smav				      /*bit*/ 0);
1319264274Smav		ctl_config_write_done(io);
1320264274Smav		return;
1321264274Smav	}
1322264274Smav
1323264274Smav	beio->io_len = 0;
1324264274Smav	beio->io_offset = -1;
1325264274Smav	beio->bio_cmd = BIO_DELETE;
1326264274Smav	beio->ds_trans_type = DEVSTAT_FREE;
1327267515Smav	DPRINTF("UNMAP\n");
1328264274Smav	be_lun->unmap(be_lun, beio);
1329264274Smav}
1330264274Smav
1331264274Smavstatic void
1332275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio)
1333275474Smav{
1334275474Smav	union ctl_io *io;
1335275474Smav
1336275474Smav	io = beio->io;
1337275474Smav	ctl_free_beio(beio);
1338275474Smav	ctl_config_read_done(io);
1339275474Smav}
1340275474Smav
1341275474Smavstatic void
1342275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1343275474Smav			 union ctl_io *io)
1344275474Smav{
1345275474Smav	struct ctl_be_block_io *beio;
1346275474Smav	struct ctl_be_block_softc *softc;
1347275474Smav
1348275474Smav	DPRINTF("entered\n");
1349275474Smav
1350275474Smav	softc = be_lun->softc;
1351275474Smav	beio = ctl_alloc_beio(softc);
1352275474Smav	beio->io = io;
1353275474Smav	beio->lun = be_lun;
1354275474Smav	beio->beio_cont = ctl_be_block_cr_done;
1355275474Smav	PRIV(io)->ptr = (void *)beio;
1356275474Smav
1357275474Smav	switch (io->scsiio.cdb[0]) {
1358275474Smav	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1359275474Smav		beio->bio_cmd = -1;
1360275474Smav		beio->ds_trans_type = DEVSTAT_NO_DATA;
1361275474Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1362275474Smav		beio->io_len = 0;
1363275474Smav		if (be_lun->get_lba_status)
1364275474Smav			be_lun->get_lba_status(be_lun, beio);
1365275474Smav		else
1366275474Smav			ctl_be_block_cr_done(beio);
1367275474Smav		break;
1368275474Smav	default:
1369275474Smav		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1370275474Smav		break;
1371275474Smav	}
1372275474Smav}
1373275474Smav
1374275474Smavstatic void
1375264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio)
1376264274Smav{
1377264274Smav	union ctl_io *io;
1378264274Smav
1379264274Smav	io = beio->io;
1380264274Smav	ctl_free_beio(beio);
1381264274Smav	ctl_config_write_done(io);
1382264274Smav}
1383264274Smav
1384264274Smavstatic void
1385229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1386229997Sken			 union ctl_io *io)
1387229997Sken{
1388229997Sken	struct ctl_be_block_io *beio;
1389229997Sken	struct ctl_be_block_softc *softc;
1390229997Sken
1391229997Sken	DPRINTF("entered\n");
1392229997Sken
1393229997Sken	softc = be_lun->softc;
1394229997Sken	beio = ctl_alloc_beio(softc);
1395229997Sken	beio->io = io;
1396229997Sken	beio->lun = be_lun;
1397264274Smav	beio->beio_cont = ctl_be_block_cw_done;
1398286353Smav	switch (io->scsiio.tag_type) {
1399286353Smav	case CTL_TAG_ORDERED:
1400286353Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1401286353Smav		break;
1402286353Smav	case CTL_TAG_HEAD_OF_QUEUE:
1403286353Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1404286353Smav		break;
1405286353Smav	case CTL_TAG_UNTAGGED:
1406286353Smav	case CTL_TAG_SIMPLE:
1407286353Smav	case CTL_TAG_ACA:
1408286353Smav	default:
1409286353Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1410286353Smav		break;
1411286353Smav	}
1412267519Smav	PRIV(io)->ptr = (void *)beio;
1413229997Sken
1414229997Sken	switch (io->scsiio.cdb[0]) {
1415229997Sken	case SYNCHRONIZE_CACHE:
1416229997Sken	case SYNCHRONIZE_CACHE_16:
1417286353Smav		ctl_be_block_cw_dispatch_sync(be_lun, io);
1418229997Sken		break;
1419264274Smav	case WRITE_SAME_10:
1420264274Smav	case WRITE_SAME_16:
1421264274Smav		ctl_be_block_cw_dispatch_ws(be_lun, io);
1422264274Smav		break;
1423264274Smav	case UNMAP:
1424264274Smav		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1425264274Smav		break;
1426229997Sken	default:
1427229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1428229997Sken		break;
1429229997Sken	}
1430229997Sken}
1431229997Sken
1432258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1433258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1434258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1435258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1436229997Sken
1437229997Skenstatic void
1438264886Smavctl_be_block_next(struct ctl_be_block_io *beio)
1439264886Smav{
1440264886Smav	struct ctl_be_block_lun *be_lun;
1441264886Smav	union ctl_io *io;
1442264886Smav
1443264886Smav	io = beio->io;
1444264886Smav	be_lun = beio->lun;
1445264886Smav	ctl_free_beio(beio);
1446267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1447267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1448267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1449267537Smav		ctl_data_submit_done(io);
1450264886Smav		return;
1451264886Smav	}
1452264886Smav
1453264886Smav	io->io_hdr.status &= ~CTL_STATUS_MASK;
1454264886Smav	io->io_hdr.status |= CTL_STATUS_NONE;
1455264886Smav
1456267877Smav	mtx_lock(&be_lun->queue_lock);
1457264886Smav	/*
1458264886Smav	 * XXX KDM make sure that links is okay to use at this point.
1459264886Smav	 * Otherwise, we either need to add another field to ctl_io_hdr,
1460264886Smav	 * or deal with resource allocation here.
1461264886Smav	 */
1462264886Smav	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1463267877Smav	mtx_unlock(&be_lun->queue_lock);
1464264886Smav
1465264886Smav	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1466264886Smav}
1467264886Smav
1468264886Smavstatic void
1469229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1470229997Sken			   union ctl_io *io)
1471229997Sken{
1472229997Sken	struct ctl_be_block_io *beio;
1473229997Sken	struct ctl_be_block_softc *softc;
1474267537Smav	struct ctl_lba_len_flags *lbalen;
1475267519Smav	struct ctl_ptr_len_flags *bptrlen;
1476267519Smav	uint64_t len_left, lbas;
1477229997Sken	int i;
1478229997Sken
1479229997Sken	softc = be_lun->softc;
1480229997Sken
1481229997Sken	DPRINTF("entered\n");
1482229997Sken
1483267537Smav	lbalen = ARGS(io);
1484267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1485267537Smav		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1486267537Smav	} else {
1487229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1488229997Sken	}
1489229997Sken
1490229997Sken	beio = ctl_alloc_beio(softc);
1491229997Sken	beio->io = io;
1492229997Sken	beio->lun = be_lun;
1493267519Smav	bptrlen = PRIV(io);
1494267519Smav	bptrlen->ptr = (void *)beio;
1495229997Sken
1496229997Sken	switch (io->scsiio.tag_type) {
1497229997Sken	case CTL_TAG_ORDERED:
1498229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1499229997Sken		break;
1500229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1501229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1502229997Sken		break;
1503229997Sken	case CTL_TAG_UNTAGGED:
1504229997Sken	case CTL_TAG_SIMPLE:
1505229997Sken	case CTL_TAG_ACA:
1506229997Sken	default:
1507229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1508229997Sken		break;
1509229997Sken	}
1510229997Sken
1511267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1512267537Smav		beio->bio_cmd = BIO_WRITE;
1513267537Smav		beio->ds_trans_type = DEVSTAT_WRITE;
1514267537Smav	} else {
1515229997Sken		beio->bio_cmd = BIO_READ;
1516229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1517229997Sken	}
1518229997Sken
1519264886Smav	DPRINTF("%s at LBA %jx len %u @%ju\n",
1520229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1521267519Smav	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1522267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1523267537Smav		lbas = CTLBLK_HALF_IO_SIZE;
1524267537Smav	else
1525267537Smav		lbas = CTLBLK_MAX_IO_SIZE;
1526267537Smav	lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1527267519Smav	beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1528267519Smav	beio->io_len = lbas * be_lun->blocksize;
1529267519Smav	bptrlen->len += lbas;
1530229997Sken
1531264886Smav	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1532264886Smav		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1533264886Smav		    i, CTLBLK_MAX_SEGS));
1534229997Sken
1535229997Sken		/*
1536229997Sken		 * Setup the S/G entry for this chunk.
1537229997Sken		 */
1538264886Smav		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1539229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1540229997Sken
1541229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1542229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1543229997Sken
1544267537Smav		/* Set up second segment for compare operation. */
1545267537Smav		if (lbalen->flags & CTL_LLF_COMPARE) {
1546267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1547267537Smav			    beio->sg_segs[i].len;
1548267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1549267537Smav			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
1550267537Smav		}
1551267537Smav
1552229997Sken		beio->num_segs++;
1553229997Sken		len_left -= beio->sg_segs[i].len;
1554229997Sken	}
1555267519Smav	if (bptrlen->len < lbalen->len)
1556264886Smav		beio->beio_cont = ctl_be_block_next;
1557264886Smav	io->scsiio.be_move_done = ctl_be_block_move_done;
1558267537Smav	/* For compare we have separate S/G lists for read and datamove. */
1559267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1560267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1561267537Smav	else
1562267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1563264886Smav	io->scsiio.kern_data_len = beio->io_len;
1564264886Smav	io->scsiio.kern_data_resid = 0;
1565264886Smav	io->scsiio.kern_sg_entries = beio->num_segs;
1566264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1567229997Sken
1568229997Sken	/*
1569229997Sken	 * For the read case, we need to read the data into our buffers and
1570229997Sken	 * then we can send it back to the user.  For the write case, we
1571229997Sken	 * need to get the data from the user first.
1572229997Sken	 */
1573229997Sken	if (beio->bio_cmd == BIO_READ) {
1574229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1575229997Sken		be_lun->dispatch(be_lun, beio);
1576229997Sken	} else {
1577229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1578229997Sken#ifdef CTL_TIME_IO
1579229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1580229997Sken#endif
1581229997Sken		ctl_datamove(io);
1582229997Sken	}
1583229997Sken}
1584229997Sken
1585229997Skenstatic void
1586229997Skenctl_be_block_worker(void *context, int pending)
1587229997Sken{
1588229997Sken	struct ctl_be_block_lun *be_lun;
1589229997Sken	struct ctl_be_block_softc *softc;
1590229997Sken	union ctl_io *io;
1591229997Sken
1592229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1593229997Sken	softc = be_lun->softc;
1594229997Sken
1595229997Sken	DPRINTF("entered\n");
1596229997Sken
1597267877Smav	mtx_lock(&be_lun->queue_lock);
1598229997Sken	for (;;) {
1599229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1600229997Sken		if (io != NULL) {
1601229997Sken			struct ctl_be_block_io *beio;
1602229997Sken
1603229997Sken			DPRINTF("datamove queue\n");
1604229997Sken
1605229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1606229997Sken				      ctl_io_hdr, links);
1607229997Sken
1608267877Smav			mtx_unlock(&be_lun->queue_lock);
1609229997Sken
1610267519Smav			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1611229997Sken
1612229997Sken			be_lun->dispatch(be_lun, beio);
1613229997Sken
1614267877Smav			mtx_lock(&be_lun->queue_lock);
1615229997Sken			continue;
1616229997Sken		}
1617229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1618229997Sken		if (io != NULL) {
1619229997Sken			DPRINTF("config write queue\n");
1620229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1621229997Sken				      ctl_io_hdr, links);
1622267877Smav			mtx_unlock(&be_lun->queue_lock);
1623229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1624267877Smav			mtx_lock(&be_lun->queue_lock);
1625229997Sken			continue;
1626229997Sken		}
1627275474Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1628275474Smav		if (io != NULL) {
1629275474Smav			DPRINTF("config read queue\n");
1630275474Smav			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1631275474Smav				      ctl_io_hdr, links);
1632275474Smav			mtx_unlock(&be_lun->queue_lock);
1633275474Smav			ctl_be_block_cr_dispatch(be_lun, io);
1634275474Smav			mtx_lock(&be_lun->queue_lock);
1635275474Smav			continue;
1636275474Smav		}
1637229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1638229997Sken		if (io != NULL) {
1639229997Sken			DPRINTF("input queue\n");
1640229997Sken
1641229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1642229997Sken				      ctl_io_hdr, links);
1643267877Smav			mtx_unlock(&be_lun->queue_lock);
1644229997Sken
1645229997Sken			/*
1646229997Sken			 * We must drop the lock, since this routine and
1647229997Sken			 * its children may sleep.
1648229997Sken			 */
1649229997Sken			ctl_be_block_dispatch(be_lun, io);
1650229997Sken
1651267877Smav			mtx_lock(&be_lun->queue_lock);
1652229997Sken			continue;
1653229997Sken		}
1654229997Sken
1655229997Sken		/*
1656229997Sken		 * If we get here, there is no work left in the queues, so
1657229997Sken		 * just break out and let the task queue go to sleep.
1658229997Sken		 */
1659229997Sken		break;
1660229997Sken	}
1661267877Smav	mtx_unlock(&be_lun->queue_lock);
1662229997Sken}
1663229997Sken
1664229997Sken/*
1665229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1666229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1667229997Sken * thread.
1668229997Sken */
1669229997Skenstatic int
1670229997Skenctl_be_block_submit(union ctl_io *io)
1671229997Sken{
1672229997Sken	struct ctl_be_block_lun *be_lun;
1673229997Sken	struct ctl_be_lun *ctl_be_lun;
1674229997Sken
1675229997Sken	DPRINTF("entered\n");
1676229997Sken
1677229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1678229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1679229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1680229997Sken
1681229997Sken	/*
1682229997Sken	 * Make sure we only get SCSI I/O.
1683229997Sken	 */
1684229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1685229997Sken		"%#x) encountered", io->io_hdr.io_type));
1686229997Sken
1687267519Smav	PRIV(io)->len = 0;
1688267519Smav
1689267877Smav	mtx_lock(&be_lun->queue_lock);
1690229997Sken	/*
1691229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1692229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1693229997Sken	 * or deal with resource allocation here.
1694229997Sken	 */
1695229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1696267877Smav	mtx_unlock(&be_lun->queue_lock);
1697229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1698229997Sken
1699267514Smav	return (CTL_RETVAL_COMPLETE);
1700229997Sken}
1701229997Sken
1702229997Skenstatic int
1703229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1704229997Sken			int flag, struct thread *td)
1705229997Sken{
1706229997Sken	struct ctl_be_block_softc *softc;
1707229997Sken	int error;
1708229997Sken
1709229997Sken	softc = &backend_block_softc;
1710229997Sken
1711229997Sken	error = 0;
1712229997Sken
1713229997Sken	switch (cmd) {
1714229997Sken	case CTL_LUN_REQ: {
1715229997Sken		struct ctl_lun_req *lun_req;
1716229997Sken
1717229997Sken		lun_req = (struct ctl_lun_req *)addr;
1718229997Sken
1719229997Sken		switch (lun_req->reqtype) {
1720229997Sken		case CTL_LUNREQ_CREATE:
1721229997Sken			error = ctl_be_block_create(softc, lun_req);
1722229997Sken			break;
1723229997Sken		case CTL_LUNREQ_RM:
1724229997Sken			error = ctl_be_block_rm(softc, lun_req);
1725229997Sken			break;
1726232604Strasz		case CTL_LUNREQ_MODIFY:
1727232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1728232604Strasz			break;
1729229997Sken		default:
1730229997Sken			lun_req->status = CTL_LUN_ERROR;
1731229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1732272911Smav				 "invalid LUN request type %d",
1733229997Sken				 lun_req->reqtype);
1734229997Sken			break;
1735229997Sken		}
1736229997Sken		break;
1737229997Sken	}
1738229997Sken	default:
1739229997Sken		error = ENOTTY;
1740229997Sken		break;
1741229997Sken	}
1742229997Sken
1743229997Sken	return (error);
1744229997Sken}
1745229997Sken
1746229997Skenstatic int
1747229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1748229997Sken{
1749229997Sken	struct ctl_be_block_filedata *file_data;
1750229997Sken	struct ctl_lun_create_params *params;
1751275865Smav	char			     *value;
1752229997Sken	struct vattr		      vattr;
1753275865Smav	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1754229997Sken	int			      error;
1755229997Sken
1756229997Sken	error = 0;
1757229997Sken	file_data = &be_lun->backend.file;
1758272911Smav	params = &be_lun->params;
1759229997Sken
1760229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1761229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1762229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1763275474Smav	be_lun->get_lba_status = ctl_be_block_gls_file;
1764275481Smav	be_lun->getattr = ctl_be_block_getattr_file;
1765229997Sken
1766229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1767229997Sken	if (error != 0) {
1768229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1769229997Sken			 "error calling VOP_GETATTR() for file %s",
1770229997Sken			 be_lun->dev_path);
1771229997Sken		return (error);
1772229997Sken	}
1773229997Sken
1774229997Sken	/*
1775229997Sken	 * Verify that we have the ability to upgrade to exclusive
1776229997Sken	 * access on this file so we can trap errors at open instead
1777229997Sken	 * of reporting them during first access.
1778229997Sken	 */
1779229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1780229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1781229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1782229997Sken			error = EBADF;
1783229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1784229997Sken				 "error locking file %s", be_lun->dev_path);
1785229997Sken			return (error);
1786229997Sken		}
1787229997Sken	}
1788229997Sken
1789229997Sken
1790229997Sken	file_data->cred = crhold(curthread->td_ucred);
1791232604Strasz	if (params->lun_size_bytes != 0)
1792232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1793232604Strasz	else
1794232604Strasz		be_lun->size_bytes = vattr.va_size;
1795229997Sken	/*
1796229997Sken	 * We set the multi thread flag for file operations because all
1797229997Sken	 * filesystems (in theory) are capable of allowing multiple readers
1798229997Sken	 * of a file at once.  So we want to get the maximum possible
1799229997Sken	 * concurrency.
1800229997Sken	 */
1801229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1802229997Sken
1803229997Sken	/*
1804273029Smav	 * For files we can use any logical block size.  Prefer 512 bytes
1805273029Smav	 * for compatibility reasons.  If file's vattr.va_blocksize
1806273029Smav	 * (preferred I/O block size) is bigger and multiple to chosen
1807273029Smav	 * logical block size -- report it as physical block size.
1808229997Sken	 */
1809229997Sken	if (params->blocksize_bytes != 0)
1810229997Sken		be_lun->blocksize = params->blocksize_bytes;
1811229997Sken	else
1812229997Sken		be_lun->blocksize = 512;
1813275865Smav
1814275865Smav	us = ps = vattr.va_blocksize;
1815275865Smav	uo = po = 0;
1816275865Smav
1817275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize");
1818275865Smav	if (value != NULL)
1819275865Smav		ctl_expand_number(value, &ps);
1820275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset");
1821275865Smav	if (value != NULL)
1822275865Smav		ctl_expand_number(value, &po);
1823275865Smav	pss = ps / be_lun->blocksize;
1824275865Smav	pos = po / be_lun->blocksize;
1825275865Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1826275865Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1827273029Smav		be_lun->pblockexp = fls(pss) - 1;
1828275865Smav		be_lun->pblockoff = (pss - pos) % pss;
1829273029Smav	}
1830229997Sken
1831275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize");
1832275865Smav	if (value != NULL)
1833275865Smav		ctl_expand_number(value, &us);
1834275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset");
1835275865Smav	if (value != NULL)
1836275865Smav		ctl_expand_number(value, &uo);
1837275865Smav	uss = us / be_lun->blocksize;
1838275865Smav	uos = uo / be_lun->blocksize;
1839275865Smav	if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) &&
1840275865Smav	    ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) {
1841275865Smav		be_lun->ublockexp = fls(uss) - 1;
1842275865Smav		be_lun->ublockoff = (uss - uos) % uss;
1843275865Smav	}
1844275865Smav
1845229997Sken	/*
1846229997Sken	 * Sanity check.  The media size has to be at least one
1847229997Sken	 * sector long.
1848229997Sken	 */
1849229997Sken	if (be_lun->size_bytes < be_lun->blocksize) {
1850229997Sken		error = EINVAL;
1851229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1852229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1853229997Sken			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1854229997Sken	}
1855275920Smav
1856275920Smav	be_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / be_lun->blocksize;
1857229997Sken	return (error);
1858229997Sken}
1859229997Sken
1860229997Skenstatic int
1861229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1862229997Sken{
1863229997Sken	struct ctl_lun_create_params *params;
1864229997Sken	struct vattr		      vattr;
1865229997Sken	struct cdev		     *dev;
1866229997Sken	struct cdevsw		     *devsw;
1867275865Smav	char			     *value;
1868278672Smav	int			      error, atomic, maxio, unmap;
1869275865Smav	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1870229997Sken
1871272911Smav	params = &be_lun->params;
1872229997Sken
1873229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1874229997Sken	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1875229997Sken	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1876229997Sken					     &be_lun->backend.dev.dev_ref);
1877229997Sken	if (be_lun->backend.dev.csw == NULL)
1878229997Sken		panic("Unable to retrieve device switch");
1879275474Smav	if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0) {
1880269123Smav		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1881275474Smav		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1882275920Smav		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1883275920Smav	} else {
1884269123Smav		be_lun->dispatch = ctl_be_block_dispatch_dev;
1885275920Smav		atomic = 0;
1886275920Smav		maxio = be_lun->backend.dev.cdev->si_iosize_max;
1887275920Smav		if (maxio <= 0)
1888275920Smav			maxio = DFLTPHYS;
1889275920Smav		if (maxio > CTLBLK_MAX_IO_SIZE)
1890275920Smav			maxio = CTLBLK_MAX_IO_SIZE;
1891275920Smav	}
1892269123Smav	be_lun->lun_flush = ctl_be_block_flush_dev;
1893274154Smav	be_lun->getattr = ctl_be_block_getattr_dev;
1894229997Sken
1895229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1896229997Sken	if (error) {
1897229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1898272911Smav			 "error getting vnode attributes for device %s",
1899272911Smav			 be_lun->dev_path);
1900229997Sken		return (error);
1901229997Sken	}
1902229997Sken
1903229997Sken	dev = be_lun->vn->v_rdev;
1904229997Sken	devsw = dev->si_devsw;
1905229997Sken	if (!devsw->d_ioctl) {
1906229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1907272911Smav			 "no d_ioctl for device %s!",
1908229997Sken			 be_lun->dev_path);
1909229997Sken		return (ENODEV);
1910229997Sken	}
1911229997Sken
1912229997Sken	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1913229997Sken			       (caddr_t)&be_lun->blocksize, FREAD,
1914229997Sken			       curthread);
1915229997Sken	if (error) {
1916229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1917272911Smav			 "error %d returned for DIOCGSECTORSIZE ioctl "
1918272911Smav			 "on %s!", error, be_lun->dev_path);
1919229997Sken		return (error);
1920229997Sken	}
1921229997Sken
1922229997Sken	/*
1923229997Sken	 * If the user has asked for a blocksize that is greater than the
1924229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1925229997Sken	 * the user is asking for is an even multiple of the underlying
1926229997Sken	 * device's blocksize.
1927229997Sken	 */
1928229997Sken	if ((params->blocksize_bytes != 0)
1929229997Sken	 && (params->blocksize_bytes > be_lun->blocksize)) {
1930229997Sken		uint32_t bs_multiple, tmp_blocksize;
1931229997Sken
1932229997Sken		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1933229997Sken
1934229997Sken		tmp_blocksize = bs_multiple * be_lun->blocksize;
1935229997Sken
1936229997Sken		if (tmp_blocksize == params->blocksize_bytes) {
1937229997Sken			be_lun->blocksize = params->blocksize_bytes;
1938229997Sken		} else {
1939229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1940272911Smav				 "requested blocksize %u is not an even "
1941229997Sken				 "multiple of backing device blocksize %u",
1942272911Smav				 params->blocksize_bytes,
1943229997Sken				 be_lun->blocksize);
1944229997Sken			return (EINVAL);
1945229997Sken
1946229997Sken		}
1947229997Sken	} else if ((params->blocksize_bytes != 0)
1948229997Sken		&& (params->blocksize_bytes != be_lun->blocksize)) {
1949229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1950272911Smav			 "requested blocksize %u < backing device "
1951272911Smav			 "blocksize %u", params->blocksize_bytes,
1952229997Sken			 be_lun->blocksize);
1953229997Sken		return (EINVAL);
1954229997Sken	}
1955229997Sken
1956229997Sken	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1957229997Sken			       (caddr_t)&be_lun->size_bytes, FREAD,
1958229997Sken			       curthread);
1959229997Sken	if (error) {
1960229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1961272911Smav			 "error %d returned for DIOCGMEDIASIZE "
1962272911Smav			 " ioctl on %s!", error,
1963232604Strasz			 be_lun->dev_path);
1964229997Sken		return (error);
1965229997Sken	}
1966229997Sken
1967232604Strasz	if (params->lun_size_bytes != 0) {
1968232604Strasz		if (params->lun_size_bytes > be_lun->size_bytes) {
1969232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1970272911Smav				 "requested LUN size %ju > backing device "
1971272911Smav				 "size %ju",
1972232604Strasz				 (uintmax_t)params->lun_size_bytes,
1973232604Strasz				 (uintmax_t)be_lun->size_bytes);
1974232604Strasz			return (EINVAL);
1975232604Strasz		}
1976232604Strasz
1977232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1978232604Strasz	}
1979232604Strasz
1980264191Smav	error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1981264191Smav			       (caddr_t)&ps, FREAD, curthread);
1982264191Smav	if (error)
1983264191Smav		ps = po = 0;
1984264191Smav	else {
1985264191Smav		error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1986264191Smav				       (caddr_t)&po, FREAD, curthread);
1987264191Smav		if (error)
1988264191Smav			po = 0;
1989264191Smav	}
1990275865Smav	us = ps;
1991275865Smav	uo = po;
1992275865Smav
1993275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblocksize");
1994275865Smav	if (value != NULL)
1995275865Smav		ctl_expand_number(value, &ps);
1996275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "pblockoffset");
1997275865Smav	if (value != NULL)
1998275865Smav		ctl_expand_number(value, &po);
1999264191Smav	pss = ps / be_lun->blocksize;
2000264191Smav	pos = po / be_lun->blocksize;
2001264191Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
2002264191Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
2003264191Smav		be_lun->pblockexp = fls(pss) - 1;
2004264191Smav		be_lun->pblockoff = (pss - pos) % pss;
2005264191Smav	}
2006264191Smav
2007275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublocksize");
2008275865Smav	if (value != NULL)
2009275865Smav		ctl_expand_number(value, &us);
2010275865Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "ublockoffset");
2011275865Smav	if (value != NULL)
2012275865Smav		ctl_expand_number(value, &uo);
2013275865Smav	uss = us / be_lun->blocksize;
2014275865Smav	uos = uo / be_lun->blocksize;
2015275865Smav	if ((uss > 0) && (uss * be_lun->blocksize == us) && (uss >= uos) &&
2016275865Smav	    ((uss & (uss - 1)) == 0) && (uos * be_lun->blocksize == uo)) {
2017275865Smav		be_lun->ublockexp = fls(uss) - 1;
2018275865Smav		be_lun->ublockoff = (uss - uos) % uss;
2019275865Smav	}
2020275865Smav
2021275920Smav	be_lun->atomicblock = atomic / be_lun->blocksize;
2022275920Smav	be_lun->opttxferlen = maxio / be_lun->blocksize;
2023278672Smav
2024278672Smav	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2025278672Smav		unmap = 1;
2026278672Smav	} else {
2027278672Smav		struct diocgattr_arg	arg;
2028278672Smav
2029278672Smav		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2030278672Smav		arg.len = sizeof(arg.value.i);
2031278672Smav		error = devsw->d_ioctl(dev, DIOCGATTR,
2032278672Smav		    (caddr_t)&arg, FREAD, curthread);
2033278672Smav		unmap = (error == 0) ? arg.value.i : 0;
2034278672Smav	}
2035278672Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
2036278672Smav	if (value != NULL)
2037278672Smav		unmap = (strcmp(value, "on") == 0);
2038278672Smav	if (unmap)
2039278672Smav		be_lun->unmap = ctl_be_block_unmap_dev;
2040278672Smav
2041229997Sken	return (0);
2042229997Sken}
2043229997Sken
2044229997Skenstatic int
2045229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
2046229997Sken{
2047229997Sken	DROP_GIANT();
2048229997Sken	if (be_lun->vn) {
2049229997Sken		int flags = FREAD | FWRITE;
2050229997Sken
2051229997Sken		switch (be_lun->dev_type) {
2052229997Sken		case CTL_BE_BLOCK_DEV:
2053229997Sken			if (be_lun->backend.dev.csw) {
2054229997Sken				dev_relthread(be_lun->backend.dev.cdev,
2055229997Sken					      be_lun->backend.dev.dev_ref);
2056229997Sken				be_lun->backend.dev.csw  = NULL;
2057229997Sken				be_lun->backend.dev.cdev = NULL;
2058229997Sken			}
2059229997Sken			break;
2060229997Sken		case CTL_BE_BLOCK_FILE:
2061229997Sken			break;
2062229997Sken		case CTL_BE_BLOCK_NONE:
2063258871Strasz			break;
2064229997Sken		default:
2065229997Sken			panic("Unexpected backend type.");
2066229997Sken			break;
2067229997Sken		}
2068229997Sken
2069229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2070229997Sken		be_lun->vn = NULL;
2071229997Sken
2072229997Sken		switch (be_lun->dev_type) {
2073229997Sken		case CTL_BE_BLOCK_DEV:
2074229997Sken			break;
2075229997Sken		case CTL_BE_BLOCK_FILE:
2076229997Sken			if (be_lun->backend.file.cred != NULL) {
2077229997Sken				crfree(be_lun->backend.file.cred);
2078229997Sken				be_lun->backend.file.cred = NULL;
2079229997Sken			}
2080229997Sken			break;
2081229997Sken		case CTL_BE_BLOCK_NONE:
2082258871Strasz			break;
2083229997Sken		default:
2084229997Sken			panic("Unexpected backend type.");
2085229997Sken			break;
2086229997Sken		}
2087272911Smav		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2088229997Sken	}
2089229997Sken	PICKUP_GIANT();
2090229997Sken
2091229997Sken	return (0);
2092229997Sken}
2093229997Sken
2094229997Skenstatic int
2095229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
2096229997Sken		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2097229997Sken{
2098229997Sken	struct nameidata nd;
2099229997Sken	int		 flags;
2100229997Sken	int		 error;
2101229997Sken
2102229997Sken	/*
2103229997Sken	 * XXX KDM allow a read-only option?
2104229997Sken	 */
2105229997Sken	flags = FREAD | FWRITE;
2106229997Sken	error = 0;
2107229997Sken
2108229997Sken	if (rootvnode == NULL) {
2109229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2110272911Smav			 "Root filesystem is not mounted");
2111229997Sken		return (1);
2112229997Sken	}
2113229997Sken
2114285391Smjg	pwd_ensure_dirs();
2115229997Sken
2116229997Sken again:
2117229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2118229997Sken	error = vn_open(&nd, &flags, 0, NULL);
2119229997Sken	if (error) {
2120229997Sken		/*
2121229997Sken		 * This is the only reasonable guess we can make as far as
2122229997Sken		 * path if the user doesn't give us a fully qualified path.
2123229997Sken		 * If they want to specify a file, they need to specify the
2124229997Sken		 * full path.
2125229997Sken		 */
2126229997Sken		if (be_lun->dev_path[0] != '/') {
2127229997Sken			char *dev_path = "/dev/";
2128229997Sken			char *dev_name;
2129229997Sken
2130229997Sken			/* Try adding device path at beginning of name */
2131229997Sken			dev_name = malloc(strlen(be_lun->dev_path)
2132229997Sken					+ strlen(dev_path) + 1,
2133229997Sken					  M_CTLBLK, M_WAITOK);
2134229997Sken			if (dev_name) {
2135229997Sken				sprintf(dev_name, "%s%s", dev_path,
2136229997Sken					be_lun->dev_path);
2137229997Sken				free(be_lun->dev_path, M_CTLBLK);
2138229997Sken				be_lun->dev_path = dev_name;
2139229997Sken				goto again;
2140229997Sken			}
2141229997Sken		}
2142229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2143272911Smav		    "error opening %s: %d", be_lun->dev_path, error);
2144229997Sken		return (error);
2145229997Sken	}
2146229997Sken
2147229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
2148229997Sken
2149229997Sken	be_lun->vn = nd.ni_vp;
2150229997Sken
2151229997Sken	/* We only support disks and files. */
2152229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
2153229997Sken		error = ctl_be_block_open_dev(be_lun, req);
2154229997Sken	} else if (be_lun->vn->v_type == VREG) {
2155229997Sken		error = ctl_be_block_open_file(be_lun, req);
2156229997Sken	} else {
2157229997Sken		error = EINVAL;
2158229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2159258871Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
2160229997Sken	}
2161229997Sken	VOP_UNLOCK(be_lun->vn, 0);
2162229997Sken
2163229997Sken	if (error != 0) {
2164229997Sken		ctl_be_block_close(be_lun);
2165229997Sken		return (error);
2166229997Sken	}
2167229997Sken
2168229997Sken	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
2169229997Sken	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
2170229997Sken
2171229997Sken	return (0);
2172229997Sken}
2173229997Sken
2174229997Skenstatic int
2175229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2176229997Sken{
2177229997Sken	struct ctl_be_block_lun *be_lun;
2178229997Sken	struct ctl_lun_create_params *params;
2179267481Smav	char num_thread_str[16];
2180229997Sken	char tmpstr[32];
2181267481Smav	char *value;
2182278672Smav	int retval, num_threads;
2183267481Smav	int tmp_num_threads;
2184229997Sken
2185229997Sken	params = &req->reqdata.create;
2186229997Sken	retval = 0;
2187272911Smav	req->status = CTL_LUN_OK;
2188229997Sken
2189229997Sken	num_threads = cbb_num_threads;
2190229997Sken
2191229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2192229997Sken
2193272911Smav	be_lun->params = req->reqdata.create;
2194229997Sken	be_lun->softc = softc;
2195229997Sken	STAILQ_INIT(&be_lun->input_queue);
2196275474Smav	STAILQ_INIT(&be_lun->config_read_queue);
2197229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
2198229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
2199229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
2200267877Smav	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
2201267877Smav	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
2202268280Smav	ctl_init_opts(&be_lun->ctl_be_lun.options,
2203268280Smav	    req->num_be_args, req->kern_be_args);
2204229997Sken
2205264886Smav	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
2206256995Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2207229997Sken
2208229997Sken	if (be_lun->lun_zone == NULL) {
2209229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2210272911Smav			 "error allocating UMA zone");
2211229997Sken		goto bailout_error;
2212229997Sken	}
2213229997Sken
2214229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2215229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
2216229997Sken	else
2217229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
2218229997Sken
2219229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
2220268280Smav		value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
2221267499Smav		if (value == NULL) {
2222229997Sken			snprintf(req->error_str, sizeof(req->error_str),
2223272911Smav				 "no file argument specified");
2224229997Sken			goto bailout_error;
2225229997Sken		}
2226267499Smav		be_lun->dev_path = strdup(value, M_CTLBLK);
2227272911Smav		be_lun->blocksize = 512;
2228272911Smav		be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
2229229997Sken
2230229997Sken		retval = ctl_be_block_open(softc, be_lun, req);
2231229997Sken		if (retval != 0) {
2232229997Sken			retval = 0;
2233272911Smav			req->status = CTL_LUN_WARNING;
2234229997Sken		}
2235229997Sken	} else {
2236229997Sken		/*
2237229997Sken		 * For processor devices, we don't have any size.
2238229997Sken		 */
2239229997Sken		be_lun->blocksize = 0;
2240264191Smav		be_lun->pblockexp = 0;
2241264191Smav		be_lun->pblockoff = 0;
2242275865Smav		be_lun->ublockexp = 0;
2243275865Smav		be_lun->ublockoff = 0;
2244229997Sken		be_lun->size_blocks = 0;
2245229997Sken		be_lun->size_bytes = 0;
2246229997Sken		be_lun->ctl_be_lun.maxlba = 0;
2247229997Sken
2248229997Sken		/*
2249229997Sken		 * Default to just 1 thread for processor devices.
2250229997Sken		 */
2251229997Sken		num_threads = 1;
2252229997Sken	}
2253229997Sken
2254229997Sken	/*
2255229997Sken	 * XXX This searching loop might be refactored to be combined with
2256229997Sken	 * the loop above,
2257229997Sken	 */
2258268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
2259267481Smav	if (value != NULL) {
2260267481Smav		tmp_num_threads = strtol(value, NULL, 0);
2261229997Sken
2262267481Smav		/*
2263267481Smav		 * We don't let the user specify less than one
2264267481Smav		 * thread, but hope he's clueful enough not to
2265267481Smav		 * specify 1000 threads.
2266267481Smav		 */
2267267481Smav		if (tmp_num_threads < 1) {
2268267481Smav			snprintf(req->error_str, sizeof(req->error_str),
2269272911Smav				 "invalid number of threads %s",
2270272911Smav				 num_thread_str);
2271267481Smav			goto bailout_error;
2272229997Sken		}
2273267481Smav		num_threads = tmp_num_threads;
2274229997Sken	}
2275229997Sken
2276229997Sken	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2277229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2278272911Smav	if (be_lun->vn == NULL)
2279272911Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE;
2280278672Smav	if (be_lun->unmap != NULL)
2281264274Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2282275568Smav	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2283275568Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_SERSEQ_READ;
2284229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
2285272911Smav	be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2286272911Smav	    0 : (be_lun->size_blocks - 1);
2287229997Sken	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2288264191Smav	be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2289264191Smav	be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2290275865Smav	be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp;
2291275865Smav	be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff;
2292275920Smav	be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock;
2293275920Smav	be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen;
2294229997Sken	/* Tell the user the blocksize we ended up using */
2295272911Smav	params->lun_size_bytes = be_lun->size_bytes;
2296229997Sken	params->blocksize_bytes = be_lun->blocksize;
2297229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2298229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2299229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2300229997Sken	} else
2301229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
2302229997Sken
2303229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2304229997Sken	be_lun->ctl_be_lun.lun_config_status =
2305229997Sken		ctl_be_block_lun_config_status;
2306229997Sken	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2307229997Sken
2308229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2309229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2310229997Sken			 softc->num_luns);
2311229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2312275953Smav			MIN(sizeof(be_lun->ctl_be_lun.serial_num),
2313229997Sken			sizeof(tmpstr)));
2314229997Sken
2315229997Sken		/* Tell the user what we used for a serial number */
2316229997Sken		strncpy((char *)params->serial_num, tmpstr,
2317275953Smav			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2318229997Sken	} else {
2319229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
2320229997Sken			params->serial_num,
2321275953Smav			MIN(sizeof(be_lun->ctl_be_lun.serial_num),
2322229997Sken			sizeof(params->serial_num)));
2323229997Sken	}
2324229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2325229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2326229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2327275953Smav			MIN(sizeof(be_lun->ctl_be_lun.device_id),
2328229997Sken			sizeof(tmpstr)));
2329229997Sken
2330229997Sken		/* Tell the user what we used for a device ID */
2331229997Sken		strncpy((char *)params->device_id, tmpstr,
2332275953Smav			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2333229997Sken	} else {
2334229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
2335229997Sken			params->device_id,
2336275953Smav			MIN(sizeof(be_lun->ctl_be_lun.device_id),
2337275953Smav			    sizeof(params->device_id)));
2338229997Sken	}
2339229997Sken
2340229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2341229997Sken
2342229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2343229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2344229997Sken
2345229997Sken	if (be_lun->io_taskqueue == NULL) {
2346229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2347272911Smav			 "unable to create taskqueue");
2348229997Sken		goto bailout_error;
2349229997Sken	}
2350229997Sken
2351229997Sken	/*
2352229997Sken	 * Note that we start the same number of threads by default for
2353229997Sken	 * both the file case and the block device case.  For the file
2354229997Sken	 * case, we need multiple threads to allow concurrency, because the
2355229997Sken	 * vnode interface is designed to be a blocking interface.  For the
2356229997Sken	 * block device case, ZFS zvols at least will block the caller's
2357229997Sken	 * context in many instances, and so we need multiple threads to
2358229997Sken	 * overcome that problem.  Other block devices don't need as many
2359229997Sken	 * threads, but they shouldn't cause too many problems.
2360229997Sken	 *
2361229997Sken	 * If the user wants to just have a single thread for a block
2362229997Sken	 * device, he can specify that when the LUN is created, or change
2363229997Sken	 * the tunable/sysctl to alter the default number of threads.
2364229997Sken	 */
2365229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2366229997Sken					 /*num threads*/num_threads,
2367229997Sken					 /*priority*/PWAIT,
2368229997Sken					 /*thread name*/
2369229997Sken					 "%s taskq", be_lun->lunname);
2370229997Sken
2371229997Sken	if (retval != 0)
2372229997Sken		goto bailout_error;
2373229997Sken
2374229997Sken	be_lun->num_threads = num_threads;
2375229997Sken
2376229997Sken	mtx_lock(&softc->lock);
2377229997Sken	softc->num_luns++;
2378229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2379229997Sken
2380229997Sken	mtx_unlock(&softc->lock);
2381229997Sken
2382229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
2383229997Sken	if (retval != 0) {
2384229997Sken		mtx_lock(&softc->lock);
2385229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2386229997Sken			      links);
2387229997Sken		softc->num_luns--;
2388229997Sken		mtx_unlock(&softc->lock);
2389229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2390272911Smav			 "ctl_add_lun() returned error %d, see dmesg for "
2391272911Smav			 "details", retval);
2392229997Sken		retval = 0;
2393229997Sken		goto bailout_error;
2394229997Sken	}
2395229997Sken
2396229997Sken	mtx_lock(&softc->lock);
2397229997Sken
2398229997Sken	/*
2399229997Sken	 * Tell the config_status routine that we're waiting so it won't
2400229997Sken	 * clean up the LUN in the event of an error.
2401229997Sken	 */
2402229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2403229997Sken
2404229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2405229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2406229997Sken		if (retval == EINTR)
2407229997Sken			break;
2408229997Sken	}
2409229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2410229997Sken
2411229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2412229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2413272911Smav			 "LUN configuration error, see dmesg for details");
2414229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2415229997Sken			      links);
2416229997Sken		softc->num_luns--;
2417229997Sken		mtx_unlock(&softc->lock);
2418229997Sken		goto bailout_error;
2419229997Sken	} else {
2420229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2421229997Sken	}
2422229997Sken
2423229997Sken	mtx_unlock(&softc->lock);
2424229997Sken
2425229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2426229997Sken					       be_lun->blocksize,
2427229997Sken					       DEVSTAT_ALL_SUPPORTED,
2428229997Sken					       be_lun->ctl_be_lun.lun_type
2429229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
2430229997Sken					       DEVSTAT_PRIORITY_OTHER);
2431229997Sken
2432229997Sken	return (retval);
2433229997Sken
2434229997Skenbailout_error:
2435229997Sken	req->status = CTL_LUN_ERROR;
2436229997Sken
2437267429Smav	if (be_lun->io_taskqueue != NULL)
2438267429Smav		taskqueue_free(be_lun->io_taskqueue);
2439229997Sken	ctl_be_block_close(be_lun);
2440267429Smav	if (be_lun->dev_path != NULL)
2441267429Smav		free(be_lun->dev_path, M_CTLBLK);
2442267429Smav	if (be_lun->lun_zone != NULL)
2443267429Smav		uma_zdestroy(be_lun->lun_zone);
2444268280Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2445267877Smav	mtx_destroy(&be_lun->queue_lock);
2446267877Smav	mtx_destroy(&be_lun->io_lock);
2447229997Sken	free(be_lun, M_CTLBLK);
2448229997Sken
2449229997Sken	return (retval);
2450229997Sken}
2451229997Sken
2452229997Skenstatic int
2453229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2454229997Sken{
2455229997Sken	struct ctl_lun_rm_params *params;
2456229997Sken	struct ctl_be_block_lun *be_lun;
2457229997Sken	int retval;
2458229997Sken
2459229997Sken	params = &req->reqdata.rm;
2460229997Sken
2461229997Sken	mtx_lock(&softc->lock);
2462229997Sken
2463229997Sken	be_lun = NULL;
2464229997Sken
2465229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2466229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2467229997Sken			break;
2468229997Sken	}
2469229997Sken	mtx_unlock(&softc->lock);
2470229997Sken
2471229997Sken	if (be_lun == NULL) {
2472229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2473272911Smav			 "LUN %u is not managed by the block backend",
2474272911Smav			 params->lun_id);
2475229997Sken		goto bailout_error;
2476229997Sken	}
2477229997Sken
2478229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2479229997Sken
2480229997Sken	if (retval != 0) {
2481229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2482272911Smav			 "error %d returned from ctl_disable_lun() for "
2483272911Smav			 "LUN %d", retval, params->lun_id);
2484229997Sken		goto bailout_error;
2485229997Sken
2486229997Sken	}
2487229997Sken
2488229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2489229997Sken	if (retval != 0) {
2490229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2491272911Smav			 "error %d returned from ctl_invalidate_lun() for "
2492272911Smav			 "LUN %d", retval, params->lun_id);
2493229997Sken		goto bailout_error;
2494229997Sken	}
2495229997Sken
2496229997Sken	mtx_lock(&softc->lock);
2497229997Sken
2498229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2499229997Sken
2500229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2501229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2502229997Sken                if (retval == EINTR)
2503229997Sken                        break;
2504229997Sken        }
2505229997Sken
2506229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2507229997Sken
2508229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2509229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2510272911Smav			 "interrupted waiting for LUN to be freed");
2511229997Sken		mtx_unlock(&softc->lock);
2512229997Sken		goto bailout_error;
2513229997Sken	}
2514229997Sken
2515229997Sken	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2516229997Sken
2517229997Sken	softc->num_luns--;
2518229997Sken	mtx_unlock(&softc->lock);
2519229997Sken
2520229997Sken	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2521229997Sken
2522229997Sken	taskqueue_free(be_lun->io_taskqueue);
2523229997Sken
2524229997Sken	ctl_be_block_close(be_lun);
2525229997Sken
2526229997Sken	if (be_lun->disk_stats != NULL)
2527229997Sken		devstat_remove_entry(be_lun->disk_stats);
2528229997Sken
2529229997Sken	uma_zdestroy(be_lun->lun_zone);
2530229997Sken
2531268280Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2532229997Sken	free(be_lun->dev_path, M_CTLBLK);
2533267877Smav	mtx_destroy(&be_lun->queue_lock);
2534267877Smav	mtx_destroy(&be_lun->io_lock);
2535229997Sken	free(be_lun, M_CTLBLK);
2536229997Sken
2537229997Sken	req->status = CTL_LUN_OK;
2538229997Sken
2539229997Sken	return (0);
2540229997Sken
2541229997Skenbailout_error:
2542229997Sken
2543229997Sken	req->status = CTL_LUN_ERROR;
2544229997Sken
2545229997Sken	return (0);
2546229997Sken}
2547229997Sken
2548232604Straszstatic int
2549232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2550232604Strasz			 struct ctl_lun_req *req)
2551232604Strasz{
2552232604Strasz	struct vattr vattr;
2553232604Strasz	int error;
2554272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2555232604Strasz
2556232604Strasz	if (params->lun_size_bytes != 0) {
2557232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2558232604Strasz	} else  {
2559271794Smav		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2560232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2561271794Smav		VOP_UNLOCK(be_lun->vn, 0);
2562232604Strasz		if (error != 0) {
2563232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2564232604Strasz				 "error calling VOP_GETATTR() for file %s",
2565232604Strasz				 be_lun->dev_path);
2566232604Strasz			return (error);
2567232604Strasz		}
2568232604Strasz
2569232604Strasz		be_lun->size_bytes = vattr.va_size;
2570232604Strasz	}
2571232604Strasz
2572232604Strasz	return (0);
2573232604Strasz}
2574232604Strasz
2575232604Straszstatic int
2576232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2577232604Strasz			struct ctl_lun_req *req)
2578232604Strasz{
2579271794Smav	struct ctl_be_block_devdata *dev_data;
2580232604Strasz	int error;
2581272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2582232604Strasz	uint64_t size_bytes;
2583232604Strasz
2584271794Smav	dev_data = &be_lun->backend.dev;
2585271794Smav	if (!dev_data->csw->d_ioctl) {
2586232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2587272911Smav			 "no d_ioctl for device %s!", be_lun->dev_path);
2588232604Strasz		return (ENODEV);
2589232604Strasz	}
2590232604Strasz
2591271794Smav	error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2592232604Strasz			       (caddr_t)&size_bytes, FREAD,
2593232604Strasz			       curthread);
2594232604Strasz	if (error) {
2595232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2596272911Smav			 "error %d returned for DIOCGMEDIASIZE ioctl "
2597272911Smav			 "on %s!", error, be_lun->dev_path);
2598232604Strasz		return (error);
2599232604Strasz	}
2600232604Strasz
2601232604Strasz	if (params->lun_size_bytes != 0) {
2602232604Strasz		if (params->lun_size_bytes > size_bytes) {
2603232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2604272911Smav				 "requested LUN size %ju > backing device "
2605272911Smav				 "size %ju",
2606232604Strasz				 (uintmax_t)params->lun_size_bytes,
2607232604Strasz				 (uintmax_t)size_bytes);
2608232604Strasz			return (EINVAL);
2609232604Strasz		}
2610232604Strasz
2611232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2612232604Strasz	} else {
2613232604Strasz		be_lun->size_bytes = size_bytes;
2614232604Strasz	}
2615232604Strasz
2616232604Strasz	return (0);
2617232604Strasz}
2618232604Strasz
2619232604Straszstatic int
2620232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2621232604Strasz{
2622232604Strasz	struct ctl_lun_modify_params *params;
2623232604Strasz	struct ctl_be_block_lun *be_lun;
2624271794Smav	uint64_t oldsize;
2625241896Skib	int error;
2626232604Strasz
2627232604Strasz	params = &req->reqdata.modify;
2628232604Strasz
2629232604Strasz	mtx_lock(&softc->lock);
2630232604Strasz	be_lun = NULL;
2631232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2632232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2633232604Strasz			break;
2634232604Strasz	}
2635232604Strasz	mtx_unlock(&softc->lock);
2636232604Strasz
2637232604Strasz	if (be_lun == NULL) {
2638232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2639272911Smav			 "LUN %u is not managed by the block backend",
2640272911Smav			 params->lun_id);
2641232604Strasz		goto bailout_error;
2642232604Strasz	}
2643232604Strasz
2644272911Smav	be_lun->params.lun_size_bytes = params->lun_size_bytes;
2645232604Strasz
2646274253Smav	oldsize = be_lun->size_bytes;
2647272911Smav	if (be_lun->vn == NULL)
2648272911Smav		error = ctl_be_block_open(softc, be_lun, req);
2649285030Smav	else if (vn_isdisk(be_lun->vn, &error))
2650285030Smav		error = ctl_be_block_modify_dev(be_lun, req);
2651272911Smav	else if (be_lun->vn->v_type == VREG)
2652232604Strasz		error = ctl_be_block_modify_file(be_lun, req);
2653232604Strasz	else
2654285030Smav		error = EINVAL;
2655232604Strasz
2656274253Smav	if (error == 0 && be_lun->size_bytes != oldsize) {
2657271794Smav		be_lun->size_blocks = be_lun->size_bytes >>
2658271794Smav		    be_lun->blocksize_shift;
2659232604Strasz
2660271794Smav		/*
2661271794Smav		 * The maximum LBA is the size - 1.
2662271794Smav		 *
2663271794Smav		 * XXX: Note that this field is being updated without locking,
2664271794Smav		 * 	which might cause problems on 32-bit architectures.
2665271794Smav		 */
2666278672Smav		if (be_lun->unmap != NULL)
2667278672Smav			be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2668272911Smav		be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2669272911Smav		    0 : (be_lun->size_blocks - 1);
2670272911Smav		be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2671272911Smav		be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2672272911Smav		be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2673275865Smav		be_lun->ctl_be_lun.ublockexp = be_lun->ublockexp;
2674275865Smav		be_lun->ctl_be_lun.ublockoff = be_lun->ublockoff;
2675275920Smav		be_lun->ctl_be_lun.atomicblock = be_lun->atomicblock;
2676275920Smav		be_lun->ctl_be_lun.opttxferlen = be_lun->opttxferlen;
2677271794Smav		ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2678272911Smav		if (oldsize == 0 && be_lun->size_blocks != 0)
2679272911Smav			ctl_lun_online(&be_lun->ctl_be_lun);
2680271794Smav	}
2681232604Strasz
2682232604Strasz	/* Tell the user the exact size we ended up using */
2683232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2684232604Strasz
2685272911Smav	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2686232604Strasz
2687232604Strasz	return (0);
2688232604Strasz
2689232604Straszbailout_error:
2690232604Strasz	req->status = CTL_LUN_ERROR;
2691232604Strasz
2692232604Strasz	return (0);
2693232604Strasz}
2694232604Strasz
2695229997Skenstatic void
2696229997Skenctl_be_block_lun_shutdown(void *be_lun)
2697229997Sken{
2698229997Sken	struct ctl_be_block_lun *lun;
2699229997Sken	struct ctl_be_block_softc *softc;
2700229997Sken
2701229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2702229997Sken
2703229997Sken	softc = lun->softc;
2704229997Sken
2705229997Sken	mtx_lock(&softc->lock);
2706229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2707229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2708229997Sken		wakeup(lun);
2709229997Sken	mtx_unlock(&softc->lock);
2710229997Sken
2711229997Sken}
2712229997Sken
2713229997Skenstatic void
2714229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2715229997Sken{
2716229997Sken	struct ctl_be_block_lun *lun;
2717229997Sken	struct ctl_be_block_softc *softc;
2718229997Sken
2719229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2720229997Sken	softc = lun->softc;
2721229997Sken
2722229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2723229997Sken		mtx_lock(&softc->lock);
2724229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2725229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2726229997Sken			wakeup(lun);
2727229997Sken		mtx_unlock(&softc->lock);
2728229997Sken
2729229997Sken		/*
2730229997Sken		 * We successfully added the LUN, attempt to enable it.
2731229997Sken		 */
2732229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2733229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2734229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2735229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2736229997Sken				       __func__);
2737229997Sken			}
2738229997Sken		}
2739229997Sken
2740229997Sken		return;
2741229997Sken	}
2742229997Sken
2743229997Sken
2744229997Sken	mtx_lock(&softc->lock);
2745229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2746229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2747229997Sken	wakeup(lun);
2748229997Sken	mtx_unlock(&softc->lock);
2749229997Sken}
2750229997Sken
2751229997Sken
2752229997Skenstatic int
2753229997Skenctl_be_block_config_write(union ctl_io *io)
2754229997Sken{
2755229997Sken	struct ctl_be_block_lun *be_lun;
2756229997Sken	struct ctl_be_lun *ctl_be_lun;
2757229997Sken	int retval;
2758229997Sken
2759229997Sken	retval = 0;
2760229997Sken
2761229997Sken	DPRINTF("entered\n");
2762229997Sken
2763229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2764229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2765229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2766229997Sken
2767229997Sken	switch (io->scsiio.cdb[0]) {
2768229997Sken	case SYNCHRONIZE_CACHE:
2769229997Sken	case SYNCHRONIZE_CACHE_16:
2770264274Smav	case WRITE_SAME_10:
2771264274Smav	case WRITE_SAME_16:
2772264274Smav	case UNMAP:
2773229997Sken		/*
2774229997Sken		 * The upper level CTL code will filter out any CDBs with
2775229997Sken		 * the immediate bit set and return the proper error.
2776229997Sken		 *
2777229997Sken		 * We don't really need to worry about what LBA range the
2778229997Sken		 * user asked to be synced out.  When they issue a sync
2779229997Sken		 * cache command, we'll sync out the whole thing.
2780229997Sken		 */
2781267877Smav		mtx_lock(&be_lun->queue_lock);
2782229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2783229997Sken				   links);
2784267877Smav		mtx_unlock(&be_lun->queue_lock);
2785229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2786229997Sken		break;
2787229997Sken	case START_STOP_UNIT: {
2788229997Sken		struct scsi_start_stop_unit *cdb;
2789229997Sken
2790229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2791229997Sken
2792229997Sken		if (cdb->how & SSS_START)
2793229997Sken			retval = ctl_start_lun(ctl_be_lun);
2794229997Sken		else {
2795229997Sken			retval = ctl_stop_lun(ctl_be_lun);
2796229997Sken			/*
2797229997Sken			 * XXX KDM Copan-specific offline behavior.
2798229997Sken			 * Figure out a reasonable way to port this?
2799229997Sken			 */
2800229997Sken#ifdef NEEDTOPORT
2801229997Sken			if ((retval == 0)
2802229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2803229997Sken				retval = ctl_lun_offline(ctl_be_lun);
2804229997Sken#endif
2805229997Sken		}
2806229997Sken
2807229997Sken		/*
2808229997Sken		 * In general, the above routines should not fail.  They
2809229997Sken		 * just set state for the LUN.  So we've got something
2810229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2811229997Sken		 */
2812229997Sken		if (retval != 0) {
2813229997Sken			ctl_set_internal_failure(&io->scsiio,
2814229997Sken						 /*sks_valid*/ 1,
2815229997Sken						 /*retry_count*/ 0xf051);
2816229997Sken			retval = CTL_RETVAL_COMPLETE;
2817229997Sken		} else {
2818229997Sken			ctl_set_success(&io->scsiio);
2819229997Sken		}
2820229997Sken		ctl_config_write_done(io);
2821229997Sken		break;
2822229997Sken	}
2823229997Sken	default:
2824229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2825229997Sken		ctl_config_write_done(io);
2826229997Sken		retval = CTL_RETVAL_COMPLETE;
2827229997Sken		break;
2828229997Sken	}
2829229997Sken
2830229997Sken	return (retval);
2831229997Sken}
2832229997Sken
2833229997Skenstatic int
2834229997Skenctl_be_block_config_read(union ctl_io *io)
2835229997Sken{
2836275474Smav	struct ctl_be_block_lun *be_lun;
2837275474Smav	struct ctl_be_lun *ctl_be_lun;
2838275474Smav	int retval = 0;
2839275474Smav
2840275474Smav	DPRINTF("entered\n");
2841275474Smav
2842275474Smav	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2843275474Smav		CTL_PRIV_BACKEND_LUN].ptr;
2844275474Smav	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2845275474Smav
2846275474Smav	switch (io->scsiio.cdb[0]) {
2847275474Smav	case SERVICE_ACTION_IN:
2848275474Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2849275474Smav			mtx_lock(&be_lun->queue_lock);
2850275474Smav			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2851275474Smav			    &io->io_hdr, links);
2852275474Smav			mtx_unlock(&be_lun->queue_lock);
2853275474Smav			taskqueue_enqueue(be_lun->io_taskqueue,
2854275474Smav			    &be_lun->io_task);
2855275474Smav			retval = CTL_RETVAL_QUEUED;
2856275474Smav			break;
2857275474Smav		}
2858275474Smav		ctl_set_invalid_field(&io->scsiio,
2859275474Smav				      /*sks_valid*/ 1,
2860275474Smav				      /*command*/ 1,
2861275474Smav				      /*field*/ 1,
2862275474Smav				      /*bit_valid*/ 1,
2863275474Smav				      /*bit*/ 4);
2864275474Smav		ctl_config_read_done(io);
2865275474Smav		retval = CTL_RETVAL_COMPLETE;
2866275474Smav		break;
2867275474Smav	default:
2868275474Smav		ctl_set_invalid_opcode(&io->scsiio);
2869275474Smav		ctl_config_read_done(io);
2870275474Smav		retval = CTL_RETVAL_COMPLETE;
2871275474Smav		break;
2872275474Smav	}
2873275474Smav
2874275474Smav	return (retval);
2875229997Sken}
2876229997Sken
2877229997Skenstatic int
2878229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2879229997Sken{
2880229997Sken	struct ctl_be_block_lun *lun;
2881229997Sken	int retval;
2882229997Sken
2883229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2884229997Sken	retval = 0;
2885229997Sken
2886268283Smav	retval = sbuf_printf(sb, "\t<num_threads>");
2887229997Sken
2888229997Sken	if (retval != 0)
2889229997Sken		goto bailout;
2890229997Sken
2891229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2892229997Sken
2893229997Sken	if (retval != 0)
2894229997Sken		goto bailout;
2895229997Sken
2896268283Smav	retval = sbuf_printf(sb, "</num_threads>\n");
2897229997Sken
2898229997Skenbailout:
2899229997Sken
2900229997Sken	return (retval);
2901229997Sken}
2902229997Sken
2903274154Smavstatic uint64_t
2904274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname)
2905274154Smav{
2906274154Smav	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2907274154Smav
2908274154Smav	if (lun->getattr == NULL)
2909274154Smav		return (UINT64_MAX);
2910274154Smav	return (lun->getattr(lun, attrname));
2911274154Smav}
2912274154Smav
2913229997Skenint
2914229997Skenctl_be_block_init(void)
2915229997Sken{
2916229997Sken	struct ctl_be_block_softc *softc;
2917229997Sken	int retval;
2918229997Sken
2919229997Sken	softc = &backend_block_softc;
2920229997Sken	retval = 0;
2921229997Sken
2922267877Smav	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2923264020Strasz	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2924264020Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2925229997Sken	STAILQ_INIT(&softc->disk_list);
2926229997Sken	STAILQ_INIT(&softc->lun_list);
2927229997Sken
2928229997Sken	return (retval);
2929229997Sken}
2930