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$");
44229997Sken
45229997Sken#include <opt_kdtrace.h>
46229997Sken
47229997Sken#include <sys/param.h>
48229997Sken#include <sys/systm.h>
49229997Sken#include <sys/kernel.h>
50229997Sken#include <sys/types.h>
51229997Sken#include <sys/kthread.h>
52229997Sken#include <sys/bio.h>
53229997Sken#include <sys/fcntl.h>
54265634Smav#include <sys/limits.h>
55229997Sken#include <sys/lock.h>
56229997Sken#include <sys/mutex.h>
57229997Sken#include <sys/condvar.h>
58229997Sken#include <sys/malloc.h>
59229997Sken#include <sys/conf.h>
60229997Sken#include <sys/ioccom.h>
61229997Sken#include <sys/queue.h>
62229997Sken#include <sys/sbuf.h>
63229997Sken#include <sys/endian.h>
64229997Sken#include <sys/uio.h>
65229997Sken#include <sys/buf.h>
66229997Sken#include <sys/taskqueue.h>
67229997Sken#include <sys/vnode.h>
68229997Sken#include <sys/namei.h>
69229997Sken#include <sys/mount.h>
70229997Sken#include <sys/disk.h>
71229997Sken#include <sys/fcntl.h>
72229997Sken#include <sys/filedesc.h>
73229997Sken#include <sys/proc.h>
74229997Sken#include <sys/pcpu.h>
75229997Sken#include <sys/module.h>
76229997Sken#include <sys/sdt.h>
77229997Sken#include <sys/devicestat.h>
78229997Sken#include <sys/sysctl.h>
79229997Sken
80229997Sken#include <geom/geom.h>
81229997Sken
82229997Sken#include <cam/cam.h>
83229997Sken#include <cam/scsi/scsi_all.h>
84229997Sken#include <cam/scsi/scsi_da.h>
85229997Sken#include <cam/ctl/ctl_io.h>
86229997Sken#include <cam/ctl/ctl.h>
87229997Sken#include <cam/ctl/ctl_backend.h>
88229997Sken#include <cam/ctl/ctl_frontend_internal.h>
89229997Sken#include <cam/ctl/ctl_ioctl.h>
90229997Sken#include <cam/ctl/ctl_scsi_all.h>
91229997Sken#include <cam/ctl/ctl_error.h>
92229997Sken
93229997Sken/*
94265642Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB
95265642Smav * I/O.  If we get an I/O larger than that, we'll split it.
96229997Sken */
97268151Smav#define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
98268151Smav#define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
99265642Smav#define	CTLBLK_MAX_SEG		MAXPHYS
100268151Smav#define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
101268151Smav#define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
102229997Sken
103229997Sken#ifdef CTLBLK_DEBUG
104229997Sken#define DPRINTF(fmt, args...) \
105229997Sken    printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
106229997Sken#else
107229997Sken#define DPRINTF(fmt, args...) do {} while(0)
108229997Sken#endif
109229997Sken
110268150Smav#define PRIV(io)	\
111268150Smav    ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
112268151Smav#define ARGS(io)	\
113268151Smav    ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
114268150Smav
115229997SkenSDT_PROVIDER_DEFINE(cbb);
116229997Sken
117229997Skentypedef enum {
118229997Sken	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
119229997Sken	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
120229997Sken	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
121229997Sken	CTL_BE_BLOCK_LUN_MULTI_THREAD	= 0x08
122229997Sken} ctl_be_block_lun_flags;
123229997Sken
124229997Skentypedef enum {
125229997Sken	CTL_BE_BLOCK_NONE,
126229997Sken	CTL_BE_BLOCK_DEV,
127229997Sken	CTL_BE_BLOCK_FILE
128229997Sken} ctl_be_block_type;
129229997Sken
130229997Skenstruct ctl_be_block_devdata {
131229997Sken	struct cdev *cdev;
132229997Sken	struct cdevsw *csw;
133229997Sken	int dev_ref;
134229997Sken};
135229997Sken
136229997Skenstruct ctl_be_block_filedata {
137229997Sken	struct ucred *cred;
138229997Sken};
139229997Sken
140229997Skenunion ctl_be_block_bedata {
141229997Sken	struct ctl_be_block_devdata dev;
142229997Sken	struct ctl_be_block_filedata file;
143229997Sken};
144229997Sken
145229997Skenstruct ctl_be_block_io;
146229997Skenstruct ctl_be_block_lun;
147229997Sken
148229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
149229997Sken			       struct ctl_be_block_io *beio);
150229997Sken
151229997Sken/*
152229997Sken * Backend LUN structure.  There is a 1:1 mapping between a block device
153229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN.
154229997Sken */
155229997Skenstruct ctl_be_block_lun {
156229997Sken	struct ctl_block_disk *disk;
157229997Sken	char lunname[32];
158229997Sken	char *dev_path;
159229997Sken	ctl_be_block_type dev_type;
160229997Sken	struct vnode *vn;
161229997Sken	union ctl_be_block_bedata backend;
162229997Sken	cbb_dispatch_t dispatch;
163229997Sken	cbb_dispatch_t lun_flush;
164265634Smav	cbb_dispatch_t unmap;
165229997Sken	uma_zone_t lun_zone;
166229997Sken	uint64_t size_blocks;
167229997Sken	uint64_t size_bytes;
168229997Sken	uint32_t blocksize;
169229997Sken	int blocksize_shift;
170264727Smav	uint16_t pblockexp;
171264727Smav	uint16_t pblockoff;
172229997Sken	struct ctl_be_block_softc *softc;
173229997Sken	struct devstat *disk_stats;
174229997Sken	ctl_be_block_lun_flags flags;
175229997Sken	STAILQ_ENTRY(ctl_be_block_lun) links;
176229997Sken	struct ctl_be_lun ctl_be_lun;
177229997Sken	struct taskqueue *io_taskqueue;
178229997Sken	struct task io_task;
179229997Sken	int num_threads;
180229997Sken	STAILQ_HEAD(, ctl_io_hdr) input_queue;
181229997Sken	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
182229997Sken	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
183268549Smav	struct mtx_padalign io_lock;
184268549Smav	struct mtx_padalign queue_lock;
185229997Sken};
186229997Sken
187229997Sken/*
188229997Sken * Overall softc structure for the block backend module.
189229997Sken */
190229997Skenstruct ctl_be_block_softc {
191229997Sken	struct mtx			 lock;
192229997Sken	int				 num_disks;
193229997Sken	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
194229997Sken	int				 num_luns;
195229997Sken	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
196229997Sken};
197229997Sken
198229997Skenstatic struct ctl_be_block_softc backend_block_softc;
199229997Sken
200229997Sken/*
201229997Sken * Per-I/O information.
202229997Sken */
203229997Skenstruct ctl_be_block_io {
204229997Sken	union ctl_io			*io;
205229997Sken	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
206229997Sken	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
207229997Sken	int				bio_cmd;
208229997Sken	int				bio_flags;
209229997Sken	int				num_segs;
210229997Sken	int				num_bios_sent;
211229997Sken	int				num_bios_done;
212229997Sken	int				send_complete;
213229997Sken	int				num_errors;
214229997Sken	struct bintime			ds_t0;
215229997Sken	devstat_tag_type		ds_tag_type;
216229997Sken	devstat_trans_flags		ds_trans_type;
217229997Sken	uint64_t			io_len;
218229997Sken	uint64_t			io_offset;
219229997Sken	struct ctl_be_block_softc	*softc;
220229997Sken	struct ctl_be_block_lun		*lun;
221265634Smav	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
222229997Sken};
223229997Sken
224229997Skenstatic int cbb_num_threads = 14;
225229997SkenTUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads);
226229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
227229997Sken	    "CAM Target Layer Block Backend");
228229997SkenSYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW,
229229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
230229997Sken
231229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
232229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
233229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
234229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
235229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
236229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
237229997Sken				    struct ctl_be_block_io *beio);
238229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
239229997Sken				       struct ctl_be_block_io *beio);
240229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
241229997Sken				   struct ctl_be_block_io *beio);
242265634Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
243265634Smav				   struct ctl_be_block_io *beio);
244229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
245229997Sken				      struct ctl_be_block_io *beio);
246229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
247229997Sken				    union ctl_io *io);
248229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
249229997Sken				  union ctl_io *io);
250229997Skenstatic void ctl_be_block_worker(void *context, int pending);
251229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
252229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
253229997Sken				   int flag, struct thread *td);
254229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
255229997Sken				  struct ctl_lun_req *req);
256229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
257229997Sken				 struct ctl_lun_req *req);
258229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
259229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
260229997Sken			     struct ctl_be_block_lun *be_lun,
261229997Sken			     struct ctl_lun_req *req);
262229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
263229997Sken			       struct ctl_lun_req *req);
264229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
265229997Sken			   struct ctl_lun_req *req);
266232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
267232604Strasz				  struct ctl_lun_req *req);
268232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
269232604Strasz				 struct ctl_lun_req *req);
270232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
271232604Strasz			   struct ctl_lun_req *req);
272229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
273229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
274229997Sken					   ctl_lun_config_status status);
275229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
276229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
277229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
278229997Skenint ctl_be_block_init(void);
279229997Sken
280229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
281229997Sken{
282230334Sken	.name = "block",
283230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
284230334Sken	.init = ctl_be_block_init,
285230334Sken	.data_submit = ctl_be_block_submit,
286230334Sken	.data_move_done = ctl_be_block_move_done,
287230334Sken	.config_read = ctl_be_block_config_read,
288230334Sken	.config_write = ctl_be_block_config_write,
289230334Sken	.ioctl = ctl_be_block_ioctl,
290230334Sken	.lun_info = ctl_be_block_lun_info
291229997Sken};
292229997Sken
293229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
294229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
295229997Sken
296265494Straszstatic uma_zone_t beio_zone;
297265494Strasz
298229997Skenstatic struct ctl_be_block_io *
299229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
300229997Sken{
301229997Sken	struct ctl_be_block_io *beio;
302229997Sken
303265494Strasz	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
304265494Strasz	beio->softc = softc;
305229997Sken	return (beio);
306229997Sken}
307229997Sken
308229997Skenstatic void
309229997Skenctl_free_beio(struct ctl_be_block_io *beio)
310229997Sken{
311229997Sken	int duplicate_free;
312229997Sken	int i;
313229997Sken
314229997Sken	duplicate_free = 0;
315229997Sken
316229997Sken	for (i = 0; i < beio->num_segs; i++) {
317229997Sken		if (beio->sg_segs[i].addr == NULL)
318229997Sken			duplicate_free++;
319229997Sken
320229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
321229997Sken		beio->sg_segs[i].addr = NULL;
322268151Smav
323268151Smav		/* For compare we had two equal S/G lists. */
324268151Smav		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
325268151Smav			uma_zfree(beio->lun->lun_zone,
326268151Smav			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
327268151Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
328268151Smav		}
329229997Sken	}
330229997Sken
331229997Sken	if (duplicate_free > 0) {
332229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
333229997Sken		       duplicate_free, beio->num_segs);
334229997Sken	}
335229997Sken
336265494Strasz	uma_zfree(beio_zone, beio);
337229997Sken}
338229997Sken
339229997Skenstatic void
340229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
341229997Sken{
342268549Smav	union ctl_io *io = beio->io;
343229997Sken
344265634Smav	if (beio->beio_cont != NULL) {
345265634Smav		beio->beio_cont(beio);
346265634Smav	} else {
347265634Smav		ctl_free_beio(beio);
348268151Smav		ctl_data_submit_done(io);
349265634Smav	}
350229997Sken}
351229997Sken
352229997Skenstatic int
353229997Skenctl_be_block_move_done(union ctl_io *io)
354229997Sken{
355229997Sken	struct ctl_be_block_io *beio;
356229997Sken	struct ctl_be_block_lun *be_lun;
357268151Smav	struct ctl_lba_len_flags *lbalen;
358229997Sken#ifdef CTL_TIME_IO
359229997Sken	struct bintime cur_bt;
360268151Smav#endif
361268151Smav	int i;
362229997Sken
363268150Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
364229997Sken	be_lun = beio->lun;
365229997Sken
366229997Sken	DPRINTF("entered\n");
367229997Sken
368229997Sken#ifdef CTL_TIME_IO
369229997Sken	getbintime(&cur_bt);
370229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
371229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
372229997Sken	io->io_hdr.num_dmas++;
373229997Sken#endif
374268151Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
375229997Sken
376229997Sken	/*
377229997Sken	 * We set status at this point for read commands, and write
378229997Sken	 * commands with errors.
379229997Sken	 */
380268151Smav	if ((io->io_hdr.port_status == 0) &&
381268151Smav	    ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) &&
382268151Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
383268151Smav		lbalen = ARGS(beio->io);
384268151Smav		if (lbalen->flags & CTL_LLF_READ) {
385268151Smav			ctl_set_success(&io->scsiio);
386268151Smav		} else if (lbalen->flags & CTL_LLF_COMPARE) {
387268151Smav			/* We have two data blocks ready for comparison. */
388268151Smav			for (i = 0; i < beio->num_segs; i++) {
389268151Smav				if (memcmp(beio->sg_segs[i].addr,
390268151Smav				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
391268151Smav				    beio->sg_segs[i].len) != 0)
392268151Smav					break;
393268151Smav			}
394268151Smav			if (i < beio->num_segs)
395268151Smav				ctl_set_sense(&io->scsiio,
396268151Smav				    /*current_error*/ 1,
397268151Smav				    /*sense_key*/ SSD_KEY_MISCOMPARE,
398268151Smav				    /*asc*/ 0x1D,
399268151Smav				    /*ascq*/ 0x00,
400268151Smav				    SSD_ELEM_NONE);
401268151Smav			else
402268151Smav				ctl_set_success(&io->scsiio);
403268151Smav		}
404268151Smav	}
405229997Sken	else if ((io->io_hdr.port_status != 0)
406229997Sken	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
407229997Sken	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
408229997Sken		/*
409229997Sken		 * For hardware error sense keys, the sense key
410229997Sken		 * specific value is defined to be a retry count,
411229997Sken		 * but we use it to pass back an internal FETD
412229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
413229997Sken		 * using 16 bits for an error code, since that's
414229997Sken		 * all the space we have in the sks field.
415229997Sken		 */
416229997Sken		ctl_set_internal_failure(&io->scsiio,
417229997Sken					 /*sks_valid*/ 1,
418229997Sken					 /*retry_count*/
419229997Sken					 io->io_hdr.port_status);
420229997Sken	}
421229997Sken
422229997Sken	/*
423229997Sken	 * If this is a read, or a write with errors, it is done.
424229997Sken	 */
425229997Sken	if ((beio->bio_cmd == BIO_READ)
426229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
427229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
428229997Sken		ctl_complete_beio(beio);
429229997Sken		return (0);
430229997Sken	}
431229997Sken
432229997Sken	/*
433229997Sken	 * At this point, we have a write and the DMA completed
434229997Sken	 * successfully.  We now have to queue it to the task queue to
435229997Sken	 * execute the backend I/O.  That is because we do blocking
436229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
437229997Sken	 * This move done routine is generally called in the SIM's
438229997Sken	 * interrupt context, and therefore we cannot block.
439229997Sken	 */
440268549Smav	mtx_lock(&be_lun->queue_lock);
441229997Sken	/*
442229997Sken	 * XXX KDM make sure that links is okay to use at this point.
443229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
444229997Sken	 * or deal with resource allocation here.
445229997Sken	 */
446229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
447268549Smav	mtx_unlock(&be_lun->queue_lock);
448229997Sken
449229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
450229997Sken
451229997Sken	return (0);
452229997Sken}
453229997Sken
454229997Skenstatic void
455229997Skenctl_be_block_biodone(struct bio *bio)
456229997Sken{
457229997Sken	struct ctl_be_block_io *beio;
458229997Sken	struct ctl_be_block_lun *be_lun;
459229997Sken	union ctl_io *io;
460262299Smav	int error;
461229997Sken
462229997Sken	beio = bio->bio_caller1;
463229997Sken	be_lun = beio->lun;
464229997Sken	io = beio->io;
465229997Sken
466229997Sken	DPRINTF("entered\n");
467229997Sken
468262299Smav	error = bio->bio_error;
469268549Smav	mtx_lock(&be_lun->io_lock);
470262299Smav	if (error != 0)
471229997Sken		beio->num_errors++;
472229997Sken
473229997Sken	beio->num_bios_done++;
474229997Sken
475229997Sken	/*
476229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
477229997Sken	 * during the free might cause it to complain.
478229997Sken	 */
479229997Sken	g_destroy_bio(bio);
480229997Sken
481229997Sken	/*
482229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
483229997Sken	 * complete, then we're done.
484229997Sken	 */
485229997Sken	if ((beio->send_complete == 0)
486229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
487268549Smav		mtx_unlock(&be_lun->io_lock);
488229997Sken		return;
489229997Sken	}
490229997Sken
491229997Sken	/*
492229997Sken	 * At this point, we've verified that we are the last I/O to
493229997Sken	 * complete, so it's safe to drop the lock.
494229997Sken	 */
495268549Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
496268549Smav	    beio->ds_tag_type, beio->ds_trans_type,
497268549Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
498268549Smav	mtx_unlock(&be_lun->io_lock);
499229997Sken
500229997Sken	/*
501229997Sken	 * If there are any errors from the backing device, we fail the
502229997Sken	 * entire I/O with a medium error.
503229997Sken	 */
504229997Sken	if (beio->num_errors > 0) {
505262299Smav		if (error == EOPNOTSUPP) {
506262299Smav			ctl_set_invalid_opcode(&io->scsiio);
507262299Smav		} else if (beio->bio_cmd == BIO_FLUSH) {
508229997Sken			/* XXX KDM is there is a better error here? */
509229997Sken			ctl_set_internal_failure(&io->scsiio,
510229997Sken						 /*sks_valid*/ 1,
511229997Sken						 /*retry_count*/ 0xbad2);
512229997Sken		} else
513229997Sken			ctl_set_medium_error(&io->scsiio);
514229997Sken		ctl_complete_beio(beio);
515229997Sken		return;
516229997Sken	}
517229997Sken
518229997Sken	/*
519268151Smav	 * If this is a write, a flush, a delete or verify, we're all done.
520229997Sken	 * If this is a read, we can now send the data to the user.
521229997Sken	 */
522229997Sken	if ((beio->bio_cmd == BIO_WRITE)
523265634Smav	 || (beio->bio_cmd == BIO_FLUSH)
524268151Smav	 || (beio->bio_cmd == BIO_DELETE)
525268151Smav	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
526229997Sken		ctl_set_success(&io->scsiio);
527229997Sken		ctl_complete_beio(beio);
528229997Sken	} else {
529229997Sken#ifdef CTL_TIME_IO
530229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
531229997Sken#endif
532229997Sken		ctl_datamove(io);
533229997Sken	}
534229997Sken}
535229997Sken
536229997Skenstatic void
537229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
538229997Sken			struct ctl_be_block_io *beio)
539229997Sken{
540268549Smav	union ctl_io *io = beio->io;
541229997Sken	struct mount *mountpoint;
542241896Skib	int error, lock_flags;
543229997Sken
544229997Sken	DPRINTF("entered\n");
545229997Sken
546268549Smav	binuptime(&beio->ds_t0);
547268549Smav	mtx_lock(&be_lun->io_lock);
548268549Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
549268549Smav	mtx_unlock(&be_lun->io_lock);
550229997Sken
551268549Smav	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
552229997Sken
553229997Sken	if (MNT_SHARED_WRITES(mountpoint)
554229997Sken	 || ((mountpoint == NULL)
555229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
556229997Sken		lock_flags = LK_SHARED;
557229997Sken	else
558229997Sken		lock_flags = LK_EXCLUSIVE;
559229997Sken
560229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
561229997Sken
562229997Sken	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
563229997Sken	VOP_UNLOCK(be_lun->vn, 0);
564229997Sken
565229997Sken	vn_finished_write(mountpoint);
566229997Sken
567268549Smav	mtx_lock(&be_lun->io_lock);
568268549Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
569268549Smav	    beio->ds_tag_type, beio->ds_trans_type,
570268549Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
571268549Smav	mtx_unlock(&be_lun->io_lock);
572268549Smav
573229997Sken	if (error == 0)
574229997Sken		ctl_set_success(&io->scsiio);
575229997Sken	else {
576229997Sken		/* XXX KDM is there is a better error here? */
577229997Sken		ctl_set_internal_failure(&io->scsiio,
578229997Sken					 /*sks_valid*/ 1,
579229997Sken					 /*retry_count*/ 0xbad1);
580229997Sken	}
581229997Sken
582229997Sken	ctl_complete_beio(beio);
583229997Sken}
584229997Sken
585260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
586260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
587260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
588260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
589229997Sken
590229997Skenstatic void
591229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
592229997Sken			   struct ctl_be_block_io *beio)
593229997Sken{
594229997Sken	struct ctl_be_block_filedata *file_data;
595229997Sken	union ctl_io *io;
596229997Sken	struct uio xuio;
597229997Sken	struct iovec *xiovec;
598241896Skib	int flags;
599229997Sken	int error, i;
600229997Sken
601229997Sken	DPRINTF("entered\n");
602229997Sken
603229997Sken	file_data = &be_lun->backend.file;
604229997Sken	io = beio->io;
605229997Sken	flags = beio->bio_flags;
606229997Sken
607268151Smav	bzero(&xuio, sizeof(xuio));
608229997Sken	if (beio->bio_cmd == BIO_READ) {
609229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
610268151Smav		xuio.uio_rw = UIO_READ;
611229997Sken	} else {
612229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
613268151Smav		xuio.uio_rw = UIO_WRITE;
614229997Sken	}
615229997Sken	xuio.uio_offset = beio->io_offset;
616229997Sken	xuio.uio_resid = beio->io_len;
617229997Sken	xuio.uio_segflg = UIO_SYSSPACE;
618229997Sken	xuio.uio_iov = beio->xiovecs;
619229997Sken	xuio.uio_iovcnt = beio->num_segs;
620229997Sken	xuio.uio_td = curthread;
621229997Sken
622229997Sken	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
623229997Sken		xiovec->iov_base = beio->sg_segs[i].addr;
624229997Sken		xiovec->iov_len = beio->sg_segs[i].len;
625229997Sken	}
626229997Sken
627268549Smav	binuptime(&beio->ds_t0);
628268549Smav	mtx_lock(&be_lun->io_lock);
629268549Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
630268549Smav	mtx_unlock(&be_lun->io_lock);
631268549Smav
632229997Sken	if (beio->bio_cmd == BIO_READ) {
633229997Sken		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
634229997Sken
635229997Sken		/*
636229997Sken		 * UFS pays attention to IO_DIRECT for reads.  If the
637229997Sken		 * DIRECTIO option is configured into the kernel, it calls
638229997Sken		 * ffs_rawread().  But that only works for single-segment
639229997Sken		 * uios with user space addresses.  In our case, with a
640229997Sken		 * kernel uio, it still reads into the buffer cache, but it
641229997Sken		 * will just try to release the buffer from the cache later
642229997Sken		 * on in ffs_read().
643229997Sken		 *
644229997Sken		 * ZFS does not pay attention to IO_DIRECT for reads.
645229997Sken		 *
646229997Sken		 * UFS does not pay attention to IO_SYNC for reads.
647229997Sken		 *
648229997Sken		 * ZFS pays attention to IO_SYNC (which translates into the
649229997Sken		 * Solaris define FRSYNC for zfs_read()) for reads.  It
650229997Sken		 * attempts to sync the file before reading.
651229997Sken		 *
652229997Sken		 * So, to attempt to provide some barrier semantics in the
653229997Sken		 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
654229997Sken		 */
655229997Sken		error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
656229997Sken				 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
657229997Sken
658229997Sken		VOP_UNLOCK(be_lun->vn, 0);
659268151Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
660229997Sken	} else {
661229997Sken		struct mount *mountpoint;
662229997Sken		int lock_flags;
663229997Sken
664229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
665229997Sken
666229997Sken		if (MNT_SHARED_WRITES(mountpoint)
667229997Sken		 || ((mountpoint == NULL)
668229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
669229997Sken			lock_flags = LK_SHARED;
670229997Sken		else
671229997Sken			lock_flags = LK_EXCLUSIVE;
672229997Sken
673229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
674229997Sken
675229997Sken		/*
676229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
677229997Sken		 * is done asynchronously.  (Normally the write would just
678229997Sken		 * get put into cache.
679229997Sken		 *
680229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
681229997Sken		 * attempt to write the buffer out synchronously if that
682229997Sken		 * flag is set.
683229997Sken		 *
684229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
685229997Sken		 *
686229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
687229997Sken		 * for writes.  It will flush the transaction from the
688229997Sken		 * cache before returning.
689229997Sken		 *
690229997Sken		 * So if we've got the BIO_ORDERED flag set, we want
691229997Sken		 * IO_SYNC in either the UFS or ZFS case.
692229997Sken		 */
693229997Sken		error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
694229997Sken				  IO_SYNC : 0, file_data->cred);
695229997Sken		VOP_UNLOCK(be_lun->vn, 0);
696229997Sken
697229997Sken		vn_finished_write(mountpoint);
698268151Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
699229997Sken        }
700229997Sken
701268549Smav	mtx_lock(&be_lun->io_lock);
702268549Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
703268549Smav	    beio->ds_tag_type, beio->ds_trans_type,
704268549Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
705268549Smav	mtx_unlock(&be_lun->io_lock);
706268549Smav
707229997Sken	/*
708229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
709229997Sken	 * return the I/O to the user.
710229997Sken	 */
711229997Sken	if (error != 0) {
712229997Sken		char path_str[32];
713229997Sken
714229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
715229997Sken		/*
716229997Sken		 * XXX KDM ZFS returns ENOSPC when the underlying
717229997Sken		 * filesystem fills up.  What kind of SCSI error should we
718229997Sken		 * return for that?
719229997Sken		 */
720229997Sken		printf("%s%s command returned errno %d\n", path_str,
721229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
722229997Sken		ctl_set_medium_error(&io->scsiio);
723229997Sken		ctl_complete_beio(beio);
724229997Sken		return;
725229997Sken	}
726229997Sken
727229997Sken	/*
728269226Smav	 * If this is a write or a verify, we're all done.
729229997Sken	 * If this is a read, we can now send the data to the user.
730229997Sken	 */
731269226Smav	if ((beio->bio_cmd == BIO_WRITE) ||
732269226Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
733229997Sken		ctl_set_success(&io->scsiio);
734229997Sken		ctl_complete_beio(beio);
735229997Sken	} else {
736229997Sken#ifdef CTL_TIME_IO
737229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
738229997Sken#endif
739229997Sken		ctl_datamove(io);
740229997Sken	}
741229997Sken}
742229997Sken
743229997Skenstatic void
744269429Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
745269429Smav			   struct ctl_be_block_io *beio)
746269429Smav{
747269429Smav	struct ctl_be_block_devdata *dev_data;
748269429Smav	union ctl_io *io;
749269429Smav	struct uio xuio;
750269429Smav	struct iovec *xiovec;
751269429Smav	int flags;
752269429Smav	int error, i;
753269429Smav
754269429Smav	DPRINTF("entered\n");
755269429Smav
756269429Smav	dev_data = &be_lun->backend.dev;
757269429Smav	io = beio->io;
758269429Smav	flags = beio->bio_flags;
759269429Smav
760269429Smav	bzero(&xuio, sizeof(xuio));
761269429Smav	if (beio->bio_cmd == BIO_READ) {
762269429Smav		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
763269429Smav		xuio.uio_rw = UIO_READ;
764269429Smav	} else {
765269429Smav		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
766269429Smav		xuio.uio_rw = UIO_WRITE;
767269429Smav	}
768269429Smav	xuio.uio_offset = beio->io_offset;
769269429Smav	xuio.uio_resid = beio->io_len;
770269429Smav	xuio.uio_segflg = UIO_SYSSPACE;
771269429Smav	xuio.uio_iov = beio->xiovecs;
772269429Smav	xuio.uio_iovcnt = beio->num_segs;
773269429Smav	xuio.uio_td = curthread;
774269429Smav
775269429Smav	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
776269429Smav		xiovec->iov_base = beio->sg_segs[i].addr;
777269429Smav		xiovec->iov_len = beio->sg_segs[i].len;
778269429Smav	}
779269429Smav
780269429Smav	binuptime(&beio->ds_t0);
781269429Smav	mtx_lock(&be_lun->io_lock);
782269429Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
783269429Smav	mtx_unlock(&be_lun->io_lock);
784269429Smav
785269429Smav	if (beio->bio_cmd == BIO_READ) {
786269429Smav		error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, 0);
787269429Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
788269429Smav	} else {
789269429Smav		error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, 0);
790269429Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
791269429Smav	}
792269429Smav
793269429Smav	mtx_lock(&be_lun->io_lock);
794269429Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
795269429Smav	    beio->ds_tag_type, beio->ds_trans_type,
796269429Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
797269429Smav	mtx_unlock(&be_lun->io_lock);
798269429Smav
799269429Smav	/*
800269429Smav	 * If we got an error, set the sense data to "MEDIUM ERROR" and
801269429Smav	 * return the I/O to the user.
802269429Smav	 */
803269429Smav	if (error != 0) {
804269429Smav		ctl_set_medium_error(&io->scsiio);
805269429Smav		ctl_complete_beio(beio);
806269429Smav		return;
807269429Smav	}
808269429Smav
809269429Smav	/*
810269429Smav	 * If this is a write or a verify, we're all done.
811269429Smav	 * If this is a read, we can now send the data to the user.
812269429Smav	 */
813269429Smav	if ((beio->bio_cmd == BIO_WRITE) ||
814269429Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
815269429Smav		ctl_set_success(&io->scsiio);
816269429Smav		ctl_complete_beio(beio);
817269429Smav	} else {
818269429Smav#ifdef CTL_TIME_IO
819269429Smav        	getbintime(&io->io_hdr.dma_start_bt);
820269429Smav#endif
821269429Smav		ctl_datamove(io);
822269429Smav	}
823269429Smav}
824269429Smav
825269429Smavstatic void
826229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
827229997Sken		       struct ctl_be_block_io *beio)
828229997Sken{
829229997Sken	struct bio *bio;
830229997Sken	union ctl_io *io;
831229997Sken	struct ctl_be_block_devdata *dev_data;
832229997Sken
833229997Sken	dev_data = &be_lun->backend.dev;
834229997Sken	io = beio->io;
835229997Sken
836229997Sken	DPRINTF("entered\n");
837229997Sken
838229997Sken	/* This can't fail, it's a blocking allocation. */
839229997Sken	bio = g_alloc_bio();
840229997Sken
841229997Sken	bio->bio_cmd	    = BIO_FLUSH;
842229997Sken	bio->bio_flags	   |= BIO_ORDERED;
843229997Sken	bio->bio_dev	    = dev_data->cdev;
844229997Sken	bio->bio_offset	    = 0;
845229997Sken	bio->bio_data	    = 0;
846229997Sken	bio->bio_done	    = ctl_be_block_biodone;
847229997Sken	bio->bio_caller1    = beio;
848229997Sken	bio->bio_pblkno	    = 0;
849229997Sken
850229997Sken	/*
851229997Sken	 * We don't need to acquire the LUN lock here, because we are only
852229997Sken	 * sending one bio, and so there is no other context to synchronize
853229997Sken	 * with.
854229997Sken	 */
855229997Sken	beio->num_bios_sent = 1;
856229997Sken	beio->send_complete = 1;
857229997Sken
858229997Sken	binuptime(&beio->ds_t0);
859268549Smav	mtx_lock(&be_lun->io_lock);
860229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
861268549Smav	mtx_unlock(&be_lun->io_lock);
862229997Sken
863229997Sken	(*dev_data->csw->d_strategy)(bio);
864229997Sken}
865229997Sken
866229997Skenstatic void
867265634Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
868265634Smav		       struct ctl_be_block_io *beio,
869265634Smav		       uint64_t off, uint64_t len, int last)
870265634Smav{
871265634Smav	struct bio *bio;
872265634Smav	struct ctl_be_block_devdata *dev_data;
873265634Smav	uint64_t maxlen;
874265634Smav
875265634Smav	dev_data = &be_lun->backend.dev;
876265634Smav	maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
877265634Smav	while (len > 0) {
878265634Smav		bio = g_alloc_bio();
879265634Smav		bio->bio_cmd	    = BIO_DELETE;
880265634Smav		bio->bio_flags	   |= beio->bio_flags;
881265634Smav		bio->bio_dev	    = dev_data->cdev;
882265634Smav		bio->bio_offset	    = off;
883265634Smav		bio->bio_length	    = MIN(len, maxlen);
884265634Smav		bio->bio_data	    = 0;
885265634Smav		bio->bio_done	    = ctl_be_block_biodone;
886265634Smav		bio->bio_caller1    = beio;
887265634Smav		bio->bio_pblkno     = off / be_lun->blocksize;
888265634Smav
889265634Smav		off += bio->bio_length;
890265634Smav		len -= bio->bio_length;
891265634Smav
892268549Smav		mtx_lock(&be_lun->io_lock);
893265634Smav		beio->num_bios_sent++;
894265634Smav		if (last && len == 0)
895265634Smav			beio->send_complete = 1;
896268549Smav		mtx_unlock(&be_lun->io_lock);
897265634Smav
898265634Smav		(*dev_data->csw->d_strategy)(bio);
899265634Smav	}
900265634Smav}
901265634Smav
902265634Smavstatic void
903265634Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
904265634Smav		       struct ctl_be_block_io *beio)
905265634Smav{
906265634Smav	union ctl_io *io;
907265634Smav	struct ctl_be_block_devdata *dev_data;
908268149Smav	struct ctl_ptr_len_flags *ptrlen;
909265634Smav	struct scsi_unmap_desc *buf, *end;
910265634Smav	uint64_t len;
911265634Smav
912265634Smav	dev_data = &be_lun->backend.dev;
913265634Smav	io = beio->io;
914265634Smav
915265634Smav	DPRINTF("entered\n");
916265634Smav
917265634Smav	binuptime(&beio->ds_t0);
918268549Smav	mtx_lock(&be_lun->io_lock);
919265634Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
920268549Smav	mtx_unlock(&be_lun->io_lock);
921265634Smav
922265634Smav	if (beio->io_offset == -1) {
923265634Smav		beio->io_len = 0;
924268149Smav		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
925268149Smav		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
926268149Smav		end = buf + ptrlen->len / sizeof(*buf);
927265634Smav		for (; buf < end; buf++) {
928265634Smav			len = (uint64_t)scsi_4btoul(buf->length) *
929265634Smav			    be_lun->blocksize;
930265634Smav			beio->io_len += len;
931265634Smav			ctl_be_block_unmap_dev_range(be_lun, beio,
932265634Smav			    scsi_8btou64(buf->lba) * be_lun->blocksize, len,
933265634Smav			    (end - buf < 2) ? TRUE : FALSE);
934265634Smav		}
935265634Smav	} else
936265634Smav		ctl_be_block_unmap_dev_range(be_lun, beio,
937265634Smav		    beio->io_offset, beio->io_len, TRUE);
938265634Smav}
939265634Smav
940265634Smavstatic void
941229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
942229997Sken			  struct ctl_be_block_io *beio)
943229997Sken{
944268549Smav	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
945229997Sken	int i;
946229997Sken	struct bio *bio;
947229997Sken	struct ctl_be_block_devdata *dev_data;
948229997Sken	off_t cur_offset;
949229997Sken	int max_iosize;
950229997Sken
951229997Sken	DPRINTF("entered\n");
952229997Sken
953229997Sken	dev_data = &be_lun->backend.dev;
954229997Sken
955229997Sken	/*
956229997Sken	 * We have to limit our I/O size to the maximum supported by the
957229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
958229997Sken	 * set it properly, use DFLTPHYS.
959229997Sken	 */
960229997Sken	max_iosize = dev_data->cdev->si_iosize_max;
961229997Sken	if (max_iosize < PAGE_SIZE)
962229997Sken		max_iosize = DFLTPHYS;
963229997Sken
964229997Sken	cur_offset = beio->io_offset;
965229997Sken	for (i = 0; i < beio->num_segs; i++) {
966229997Sken		size_t cur_size;
967229997Sken		uint8_t *cur_ptr;
968229997Sken
969229997Sken		cur_size = beio->sg_segs[i].len;
970229997Sken		cur_ptr = beio->sg_segs[i].addr;
971229997Sken
972229997Sken		while (cur_size > 0) {
973229997Sken			/* This can't fail, it's a blocking allocation. */
974229997Sken			bio = g_alloc_bio();
975229997Sken
976229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
977229997Sken
978229997Sken			bio->bio_cmd = beio->bio_cmd;
979229997Sken			bio->bio_flags |= beio->bio_flags;
980229997Sken			bio->bio_dev = dev_data->cdev;
981229997Sken			bio->bio_caller1 = beio;
982229997Sken			bio->bio_length = min(cur_size, max_iosize);
983229997Sken			bio->bio_offset = cur_offset;
984229997Sken			bio->bio_data = cur_ptr;
985229997Sken			bio->bio_done = ctl_be_block_biodone;
986229997Sken			bio->bio_pblkno = cur_offset / be_lun->blocksize;
987229997Sken
988229997Sken			cur_offset += bio->bio_length;
989229997Sken			cur_ptr += bio->bio_length;
990229997Sken			cur_size -= bio->bio_length;
991229997Sken
992268549Smav			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
993229997Sken			beio->num_bios_sent++;
994229997Sken		}
995229997Sken	}
996268549Smav	binuptime(&beio->ds_t0);
997268549Smav	mtx_lock(&be_lun->io_lock);
998268549Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
999268549Smav	beio->send_complete = 1;
1000268549Smav	mtx_unlock(&be_lun->io_lock);
1001268549Smav
1002268549Smav	/*
1003268549Smav	 * Fire off all allocated requests!
1004268549Smav	 */
1005268549Smav	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1006268549Smav		TAILQ_REMOVE(&queue, bio, bio_queue);
1007268549Smav		(*dev_data->csw->d_strategy)(bio);
1008268549Smav	}
1009229997Sken}
1010229997Sken
1011229997Skenstatic void
1012265634Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1013265634Smav{
1014265634Smav	union ctl_io *io;
1015265634Smav
1016265634Smav	io = beio->io;
1017265634Smav	ctl_free_beio(beio);
1018268261Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1019268261Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1020268261Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1021265634Smav		ctl_config_write_done(io);
1022265634Smav		return;
1023265634Smav	}
1024265634Smav
1025265634Smav	ctl_be_block_config_write(io);
1026265634Smav}
1027265634Smav
1028265634Smavstatic void
1029265634Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1030265634Smav			    union ctl_io *io)
1031265634Smav{
1032265634Smav	struct ctl_be_block_io *beio;
1033265634Smav	struct ctl_be_block_softc *softc;
1034268149Smav	struct ctl_lba_len_flags *lbalen;
1035265634Smav	uint64_t len_left, lba;
1036265634Smav	int i, seglen;
1037265634Smav	uint8_t *buf, *end;
1038265634Smav
1039265634Smav	DPRINTF("entered\n");
1040265634Smav
1041268150Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1042265634Smav	softc = be_lun->softc;
1043268151Smav	lbalen = ARGS(beio->io);
1044265634Smav
1045270108Smav	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR) ||
1046270108Smav	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1047265634Smav		ctl_free_beio(beio);
1048265634Smav		ctl_set_invalid_field(&io->scsiio,
1049265634Smav				      /*sks_valid*/ 1,
1050265634Smav				      /*command*/ 1,
1051265634Smav				      /*field*/ 1,
1052265634Smav				      /*bit_valid*/ 0,
1053265634Smav				      /*bit*/ 0);
1054265634Smav		ctl_config_write_done(io);
1055265634Smav		return;
1056265634Smav	}
1057265634Smav
1058265634Smav	/*
1059265634Smav	 * If the I/O came down with an ordered or head of queue tag, set
1060265634Smav	 * the BIO_ORDERED attribute.  For head of queue tags, that's
1061265634Smav	 * pretty much the best we can do.
1062265634Smav	 */
1063265634Smav	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1064265634Smav	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1065265634Smav		beio->bio_flags = BIO_ORDERED;
1066265634Smav
1067265634Smav	switch (io->scsiio.tag_type) {
1068265634Smav	case CTL_TAG_ORDERED:
1069265634Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1070265634Smav		break;
1071265634Smav	case CTL_TAG_HEAD_OF_QUEUE:
1072265634Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1073265634Smav		break;
1074265634Smav	case CTL_TAG_UNTAGGED:
1075265634Smav	case CTL_TAG_SIMPLE:
1076265634Smav	case CTL_TAG_ACA:
1077265634Smav	default:
1078265634Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1079265634Smav		break;
1080265634Smav	}
1081265634Smav
1082270108Smav	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1083268149Smav		beio->io_offset = lbalen->lba * be_lun->blocksize;
1084268149Smav		beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1085265634Smav		beio->bio_cmd = BIO_DELETE;
1086265634Smav		beio->ds_trans_type = DEVSTAT_FREE;
1087265634Smav
1088265634Smav		be_lun->unmap(be_lun, beio);
1089265634Smav		return;
1090265634Smav	}
1091265634Smav
1092265634Smav	beio->bio_cmd = BIO_WRITE;
1093265634Smav	beio->ds_trans_type = DEVSTAT_WRITE;
1094265634Smav
1095265634Smav	DPRINTF("WRITE SAME at LBA %jx len %u\n",
1096268149Smav	       (uintmax_t)lbalen->lba, lbalen->len);
1097265634Smav
1098268149Smav	len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1099265634Smav	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1100265634Smav
1101265634Smav		/*
1102265634Smav		 * Setup the S/G entry for this chunk.
1103265634Smav		 */
1104265642Smav		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1105265634Smav		seglen -= seglen % be_lun->blocksize;
1106265634Smav		beio->sg_segs[i].len = seglen;
1107265634Smav		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1108265634Smav
1109265634Smav		DPRINTF("segment %d addr %p len %zd\n", i,
1110265634Smav			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1111265634Smav
1112265634Smav		beio->num_segs++;
1113265634Smav		len_left -= seglen;
1114265634Smav
1115265634Smav		buf = beio->sg_segs[i].addr;
1116265634Smav		end = buf + seglen;
1117265634Smav		for (; buf < end; buf += be_lun->blocksize) {
1118265634Smav			memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1119268149Smav			if (lbalen->flags & SWS_LBDATA)
1120268149Smav				scsi_ulto4b(lbalen->lba + lba, buf);
1121265634Smav			lba++;
1122265634Smav		}
1123265634Smav	}
1124265634Smav
1125268149Smav	beio->io_offset = lbalen->lba * be_lun->blocksize;
1126265634Smav	beio->io_len = lba * be_lun->blocksize;
1127265634Smav
1128265634Smav	/* We can not do all in one run. Correct and schedule rerun. */
1129265634Smav	if (len_left > 0) {
1130268149Smav		lbalen->lba += lba;
1131268149Smav		lbalen->len -= lba;
1132265634Smav		beio->beio_cont = ctl_be_block_cw_done_ws;
1133265634Smav	}
1134265634Smav
1135265634Smav	be_lun->dispatch(be_lun, beio);
1136265634Smav}
1137265634Smav
1138265634Smavstatic void
1139265634Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1140265634Smav			    union ctl_io *io)
1141265634Smav{
1142265634Smav	struct ctl_be_block_io *beio;
1143265634Smav	struct ctl_be_block_softc *softc;
1144268149Smav	struct ctl_ptr_len_flags *ptrlen;
1145265634Smav
1146265634Smav	DPRINTF("entered\n");
1147265634Smav
1148268150Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1149265634Smav	softc = be_lun->softc;
1150268149Smav	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1151265634Smav
1152270108Smav	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1153265634Smav		ctl_free_beio(beio);
1154265634Smav		ctl_set_invalid_field(&io->scsiio,
1155265634Smav				      /*sks_valid*/ 0,
1156265634Smav				      /*command*/ 1,
1157265634Smav				      /*field*/ 0,
1158265634Smav				      /*bit_valid*/ 0,
1159265634Smav				      /*bit*/ 0);
1160265634Smav		ctl_config_write_done(io);
1161265634Smav		return;
1162265634Smav	}
1163265634Smav
1164265634Smav	/*
1165265634Smav	 * If the I/O came down with an ordered or head of queue tag, set
1166265634Smav	 * the BIO_ORDERED attribute.  For head of queue tags, that's
1167265634Smav	 * pretty much the best we can do.
1168265634Smav	 */
1169265634Smav	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1170265634Smav	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1171265634Smav		beio->bio_flags = BIO_ORDERED;
1172265634Smav
1173265634Smav	switch (io->scsiio.tag_type) {
1174265634Smav	case CTL_TAG_ORDERED:
1175265634Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1176265634Smav		break;
1177265634Smav	case CTL_TAG_HEAD_OF_QUEUE:
1178265634Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1179265634Smav		break;
1180265634Smav	case CTL_TAG_UNTAGGED:
1181265634Smav	case CTL_TAG_SIMPLE:
1182265634Smav	case CTL_TAG_ACA:
1183265634Smav	default:
1184265634Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1185265634Smav		break;
1186265634Smav	}
1187265634Smav
1188265634Smav	beio->io_len = 0;
1189265634Smav	beio->io_offset = -1;
1190265634Smav
1191265634Smav	beio->bio_cmd = BIO_DELETE;
1192265634Smav	beio->ds_trans_type = DEVSTAT_FREE;
1193265634Smav
1194268149Smav	DPRINTF("UNMAP\n");
1195265634Smav
1196265634Smav	be_lun->unmap(be_lun, beio);
1197265634Smav}
1198265634Smav
1199265634Smavstatic void
1200265634Smavctl_be_block_cw_done(struct ctl_be_block_io *beio)
1201265634Smav{
1202265634Smav	union ctl_io *io;
1203265634Smav
1204265634Smav	io = beio->io;
1205265634Smav	ctl_free_beio(beio);
1206265634Smav	ctl_config_write_done(io);
1207265634Smav}
1208265634Smav
1209265634Smavstatic void
1210229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1211229997Sken			 union ctl_io *io)
1212229997Sken{
1213229997Sken	struct ctl_be_block_io *beio;
1214229997Sken	struct ctl_be_block_softc *softc;
1215229997Sken
1216229997Sken	DPRINTF("entered\n");
1217229997Sken
1218229997Sken	softc = be_lun->softc;
1219229997Sken	beio = ctl_alloc_beio(softc);
1220229997Sken	beio->io = io;
1221229997Sken	beio->lun = be_lun;
1222265634Smav	beio->beio_cont = ctl_be_block_cw_done;
1223268150Smav	PRIV(io)->ptr = (void *)beio;
1224229997Sken
1225229997Sken	switch (io->scsiio.cdb[0]) {
1226229997Sken	case SYNCHRONIZE_CACHE:
1227229997Sken	case SYNCHRONIZE_CACHE_16:
1228249194Strasz		beio->bio_cmd = BIO_FLUSH;
1229229997Sken		beio->ds_trans_type = DEVSTAT_NO_DATA;
1230229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1231229997Sken		beio->io_len = 0;
1232229997Sken		be_lun->lun_flush(be_lun, beio);
1233229997Sken		break;
1234265634Smav	case WRITE_SAME_10:
1235265634Smav	case WRITE_SAME_16:
1236265634Smav		ctl_be_block_cw_dispatch_ws(be_lun, io);
1237265634Smav		break;
1238265634Smav	case UNMAP:
1239265634Smav		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1240265634Smav		break;
1241229997Sken	default:
1242229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1243229997Sken		break;
1244229997Sken	}
1245229997Sken}
1246229997Sken
1247260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1248260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1249260817SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1250260817SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1251229997Sken
1252229997Skenstatic void
1253265642Smavctl_be_block_next(struct ctl_be_block_io *beio)
1254265642Smav{
1255265642Smav	struct ctl_be_block_lun *be_lun;
1256265642Smav	union ctl_io *io;
1257265642Smav
1258265642Smav	io = beio->io;
1259265642Smav	be_lun = beio->lun;
1260265642Smav	ctl_free_beio(beio);
1261268261Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1262268261Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1263268261Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1264268151Smav		ctl_data_submit_done(io);
1265265642Smav		return;
1266265642Smav	}
1267265642Smav
1268265642Smav	io->io_hdr.status &= ~CTL_STATUS_MASK;
1269265642Smav	io->io_hdr.status |= CTL_STATUS_NONE;
1270265642Smav
1271268549Smav	mtx_lock(&be_lun->queue_lock);
1272265642Smav	/*
1273265642Smav	 * XXX KDM make sure that links is okay to use at this point.
1274265642Smav	 * Otherwise, we either need to add another field to ctl_io_hdr,
1275265642Smav	 * or deal with resource allocation here.
1276265642Smav	 */
1277265642Smav	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1278268549Smav	mtx_unlock(&be_lun->queue_lock);
1279265642Smav
1280265642Smav	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1281265642Smav}
1282265642Smav
1283265642Smavstatic void
1284229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1285229997Sken			   union ctl_io *io)
1286229997Sken{
1287229997Sken	struct ctl_be_block_io *beio;
1288229997Sken	struct ctl_be_block_softc *softc;
1289268151Smav	struct ctl_lba_len_flags *lbalen;
1290268150Smav	struct ctl_ptr_len_flags *bptrlen;
1291268150Smav	uint64_t len_left, lbas;
1292229997Sken	int i;
1293229997Sken
1294229997Sken	softc = be_lun->softc;
1295229997Sken
1296229997Sken	DPRINTF("entered\n");
1297229997Sken
1298268151Smav	lbalen = ARGS(io);
1299268151Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1300268151Smav		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1301268151Smav	} else {
1302229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1303229997Sken	}
1304229997Sken
1305229997Sken	beio = ctl_alloc_beio(softc);
1306229997Sken	beio->io = io;
1307229997Sken	beio->lun = be_lun;
1308268150Smav	bptrlen = PRIV(io);
1309268150Smav	bptrlen->ptr = (void *)beio;
1310229997Sken
1311229997Sken	/*
1312229997Sken	 * If the I/O came down with an ordered or head of queue tag, set
1313229997Sken	 * the BIO_ORDERED attribute.  For head of queue tags, that's
1314229997Sken	 * pretty much the best we can do.
1315229997Sken	 *
1316229997Sken	 * XXX KDM we don't have a great way to easily know about the FUA
1317229997Sken	 * bit right now (it is decoded in ctl_read_write(), but we don't
1318229997Sken	 * pass that knowledge to the backend), and in any case we would
1319229997Sken	 * need to determine how to handle it.
1320229997Sken	 */
1321229997Sken	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1322229997Sken	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1323229997Sken		beio->bio_flags = BIO_ORDERED;
1324229997Sken
1325229997Sken	switch (io->scsiio.tag_type) {
1326229997Sken	case CTL_TAG_ORDERED:
1327229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1328229997Sken		break;
1329229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1330229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1331229997Sken		break;
1332229997Sken	case CTL_TAG_UNTAGGED:
1333229997Sken	case CTL_TAG_SIMPLE:
1334229997Sken	case CTL_TAG_ACA:
1335229997Sken	default:
1336229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1337229997Sken		break;
1338229997Sken	}
1339229997Sken
1340268151Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1341268151Smav		beio->bio_cmd = BIO_WRITE;
1342268151Smav		beio->ds_trans_type = DEVSTAT_WRITE;
1343268151Smav	} else {
1344229997Sken		beio->bio_cmd = BIO_READ;
1345229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1346229997Sken	}
1347229997Sken
1348265642Smav	DPRINTF("%s at LBA %jx len %u @%ju\n",
1349229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1350268150Smav	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1351268151Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1352268151Smav		lbas = CTLBLK_HALF_IO_SIZE;
1353268151Smav	else
1354268151Smav		lbas = CTLBLK_MAX_IO_SIZE;
1355268151Smav	lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1356268150Smav	beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1357268150Smav	beio->io_len = lbas * be_lun->blocksize;
1358268150Smav	bptrlen->len += lbas;
1359229997Sken
1360265642Smav	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1361265642Smav		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1362265642Smav		    i, CTLBLK_MAX_SEGS));
1363229997Sken
1364229997Sken		/*
1365229997Sken		 * Setup the S/G entry for this chunk.
1366229997Sken		 */
1367265642Smav		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1368229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1369229997Sken
1370229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1371229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1372229997Sken
1373268151Smav		/* Set up second segment for compare operation. */
1374268151Smav		if (lbalen->flags & CTL_LLF_COMPARE) {
1375268151Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1376268151Smav			    beio->sg_segs[i].len;
1377268151Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1378268151Smav			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
1379268151Smav		}
1380268151Smav
1381229997Sken		beio->num_segs++;
1382229997Sken		len_left -= beio->sg_segs[i].len;
1383229997Sken	}
1384268150Smav	if (bptrlen->len < lbalen->len)
1385265642Smav		beio->beio_cont = ctl_be_block_next;
1386265642Smav	io->scsiio.be_move_done = ctl_be_block_move_done;
1387268151Smav	/* For compare we have separate S/G lists for read and datamove. */
1388268151Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1389268151Smav		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1390268151Smav	else
1391268151Smav		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1392265642Smav	io->scsiio.kern_data_len = beio->io_len;
1393265642Smav	io->scsiio.kern_data_resid = 0;
1394265642Smav	io->scsiio.kern_sg_entries = beio->num_segs;
1395265642Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1396229997Sken
1397229997Sken	/*
1398229997Sken	 * For the read case, we need to read the data into our buffers and
1399229997Sken	 * then we can send it back to the user.  For the write case, we
1400229997Sken	 * need to get the data from the user first.
1401229997Sken	 */
1402229997Sken	if (beio->bio_cmd == BIO_READ) {
1403229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1404229997Sken		be_lun->dispatch(be_lun, beio);
1405229997Sken	} else {
1406229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1407229997Sken#ifdef CTL_TIME_IO
1408229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1409229997Sken#endif
1410229997Sken		ctl_datamove(io);
1411229997Sken	}
1412229997Sken}
1413229997Sken
1414229997Skenstatic void
1415229997Skenctl_be_block_worker(void *context, int pending)
1416229997Sken{
1417229997Sken	struct ctl_be_block_lun *be_lun;
1418229997Sken	struct ctl_be_block_softc *softc;
1419229997Sken	union ctl_io *io;
1420229997Sken
1421229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1422229997Sken	softc = be_lun->softc;
1423229997Sken
1424229997Sken	DPRINTF("entered\n");
1425229997Sken
1426268549Smav	mtx_lock(&be_lun->queue_lock);
1427229997Sken	for (;;) {
1428229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1429229997Sken		if (io != NULL) {
1430229997Sken			struct ctl_be_block_io *beio;
1431229997Sken
1432229997Sken			DPRINTF("datamove queue\n");
1433229997Sken
1434229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1435229997Sken				      ctl_io_hdr, links);
1436229997Sken
1437268549Smav			mtx_unlock(&be_lun->queue_lock);
1438229997Sken
1439268150Smav			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1440229997Sken
1441229997Sken			be_lun->dispatch(be_lun, beio);
1442229997Sken
1443268549Smav			mtx_lock(&be_lun->queue_lock);
1444229997Sken			continue;
1445229997Sken		}
1446229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1447229997Sken		if (io != NULL) {
1448229997Sken
1449229997Sken			DPRINTF("config write queue\n");
1450229997Sken
1451229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1452229997Sken				      ctl_io_hdr, links);
1453229997Sken
1454268549Smav			mtx_unlock(&be_lun->queue_lock);
1455229997Sken
1456229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1457229997Sken
1458268549Smav			mtx_lock(&be_lun->queue_lock);
1459229997Sken			continue;
1460229997Sken		}
1461229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1462229997Sken		if (io != NULL) {
1463229997Sken			DPRINTF("input queue\n");
1464229997Sken
1465229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1466229997Sken				      ctl_io_hdr, links);
1467268549Smav			mtx_unlock(&be_lun->queue_lock);
1468229997Sken
1469229997Sken			/*
1470229997Sken			 * We must drop the lock, since this routine and
1471229997Sken			 * its children may sleep.
1472229997Sken			 */
1473229997Sken			ctl_be_block_dispatch(be_lun, io);
1474229997Sken
1475268549Smav			mtx_lock(&be_lun->queue_lock);
1476229997Sken			continue;
1477229997Sken		}
1478229997Sken
1479229997Sken		/*
1480229997Sken		 * If we get here, there is no work left in the queues, so
1481229997Sken		 * just break out and let the task queue go to sleep.
1482229997Sken		 */
1483229997Sken		break;
1484229997Sken	}
1485268549Smav	mtx_unlock(&be_lun->queue_lock);
1486229997Sken}
1487229997Sken
1488229997Sken/*
1489229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1490229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1491229997Sken * thread.
1492229997Sken */
1493229997Skenstatic int
1494229997Skenctl_be_block_submit(union ctl_io *io)
1495229997Sken{
1496229997Sken	struct ctl_be_block_lun *be_lun;
1497229997Sken	struct ctl_be_lun *ctl_be_lun;
1498229997Sken
1499229997Sken	DPRINTF("entered\n");
1500229997Sken
1501229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1502229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1503229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1504229997Sken
1505229997Sken	/*
1506229997Sken	 * Make sure we only get SCSI I/O.
1507229997Sken	 */
1508229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1509229997Sken		"%#x) encountered", io->io_hdr.io_type));
1510229997Sken
1511268150Smav	PRIV(io)->len = 0;
1512268150Smav
1513268549Smav	mtx_lock(&be_lun->queue_lock);
1514229997Sken	/*
1515229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1516229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1517229997Sken	 * or deal with resource allocation here.
1518229997Sken	 */
1519229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1520268549Smav	mtx_unlock(&be_lun->queue_lock);
1521229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1522229997Sken
1523268148Smav	return (CTL_RETVAL_COMPLETE);
1524229997Sken}
1525229997Sken
1526229997Skenstatic int
1527229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1528229997Sken			int flag, struct thread *td)
1529229997Sken{
1530229997Sken	struct ctl_be_block_softc *softc;
1531229997Sken	int error;
1532229997Sken
1533229997Sken	softc = &backend_block_softc;
1534229997Sken
1535229997Sken	error = 0;
1536229997Sken
1537229997Sken	switch (cmd) {
1538229997Sken	case CTL_LUN_REQ: {
1539229997Sken		struct ctl_lun_req *lun_req;
1540229997Sken
1541229997Sken		lun_req = (struct ctl_lun_req *)addr;
1542229997Sken
1543229997Sken		switch (lun_req->reqtype) {
1544229997Sken		case CTL_LUNREQ_CREATE:
1545229997Sken			error = ctl_be_block_create(softc, lun_req);
1546229997Sken			break;
1547229997Sken		case CTL_LUNREQ_RM:
1548229997Sken			error = ctl_be_block_rm(softc, lun_req);
1549229997Sken			break;
1550232604Strasz		case CTL_LUNREQ_MODIFY:
1551232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1552232604Strasz			break;
1553229997Sken		default:
1554229997Sken			lun_req->status = CTL_LUN_ERROR;
1555229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1556229997Sken				 "%s: invalid LUN request type %d", __func__,
1557229997Sken				 lun_req->reqtype);
1558229997Sken			break;
1559229997Sken		}
1560229997Sken		break;
1561229997Sken	}
1562229997Sken	default:
1563229997Sken		error = ENOTTY;
1564229997Sken		break;
1565229997Sken	}
1566229997Sken
1567229997Sken	return (error);
1568229997Sken}
1569229997Sken
1570229997Skenstatic int
1571229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1572229997Sken{
1573229997Sken	struct ctl_be_block_filedata *file_data;
1574229997Sken	struct ctl_lun_create_params *params;
1575229997Sken	struct vattr		      vattr;
1576229997Sken	int			      error;
1577229997Sken
1578229997Sken	error = 0;
1579229997Sken	file_data = &be_lun->backend.file;
1580229997Sken	params = &req->reqdata.create;
1581229997Sken
1582229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1583229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1584229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1585229997Sken
1586229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1587229997Sken	if (error != 0) {
1588229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1589229997Sken			 "error calling VOP_GETATTR() for file %s",
1590229997Sken			 be_lun->dev_path);
1591229997Sken		return (error);
1592229997Sken	}
1593229997Sken
1594229997Sken	/*
1595229997Sken	 * Verify that we have the ability to upgrade to exclusive
1596229997Sken	 * access on this file so we can trap errors at open instead
1597229997Sken	 * of reporting them during first access.
1598229997Sken	 */
1599229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1600229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1601229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1602229997Sken			error = EBADF;
1603229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1604229997Sken				 "error locking file %s", be_lun->dev_path);
1605229997Sken			return (error);
1606229997Sken		}
1607229997Sken	}
1608229997Sken
1609229997Sken
1610229997Sken	file_data->cred = crhold(curthread->td_ucred);
1611232604Strasz	if (params->lun_size_bytes != 0)
1612232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1613232604Strasz	else
1614232604Strasz		be_lun->size_bytes = vattr.va_size;
1615229997Sken	/*
1616229997Sken	 * We set the multi thread flag for file operations because all
1617229997Sken	 * filesystems (in theory) are capable of allowing multiple readers
1618229997Sken	 * of a file at once.  So we want to get the maximum possible
1619229997Sken	 * concurrency.
1620229997Sken	 */
1621229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1622229997Sken
1623229997Sken	/*
1624229997Sken	 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
1625229997Sken	 * With ZFS, it is 131072 bytes.  Block sizes that large don't work
1626229997Sken	 * with disklabel and UFS on FreeBSD at least.  Large block sizes
1627229997Sken	 * may not work with other OSes as well.  So just export a sector
1628229997Sken	 * size of 512 bytes, which should work with any OS or
1629229997Sken	 * application.  Since our backing is a file, any block size will
1630229997Sken	 * work fine for the backing store.
1631229997Sken	 */
1632229997Sken#if 0
1633229997Sken	be_lun->blocksize= vattr.va_blocksize;
1634229997Sken#endif
1635229997Sken	if (params->blocksize_bytes != 0)
1636229997Sken		be_lun->blocksize = params->blocksize_bytes;
1637229997Sken	else
1638229997Sken		be_lun->blocksize = 512;
1639229997Sken
1640229997Sken	/*
1641229997Sken	 * Sanity check.  The media size has to be at least one
1642229997Sken	 * sector long.
1643229997Sken	 */
1644229997Sken	if (be_lun->size_bytes < be_lun->blocksize) {
1645229997Sken		error = EINVAL;
1646229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1647229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1648229997Sken			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1649229997Sken	}
1650229997Sken	return (error);
1651229997Sken}
1652229997Sken
1653229997Skenstatic int
1654229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1655229997Sken{
1656229997Sken	struct ctl_lun_create_params *params;
1657229997Sken	struct vattr		      vattr;
1658229997Sken	struct cdev		     *dev;
1659229997Sken	struct cdevsw		     *devsw;
1660229997Sken	int			      error;
1661264727Smav	off_t			      ps, pss, po, pos;
1662229997Sken
1663229997Sken	params = &req->reqdata.create;
1664229997Sken
1665229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1666229997Sken	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1667229997Sken	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1668229997Sken					     &be_lun->backend.dev.dev_ref);
1669229997Sken	if (be_lun->backend.dev.csw == NULL)
1670229997Sken		panic("Unable to retrieve device switch");
1671269429Smav	if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0)
1672269429Smav		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1673269429Smav	else
1674269429Smav		be_lun->dispatch = ctl_be_block_dispatch_dev;
1675269429Smav	be_lun->lun_flush = ctl_be_block_flush_dev;
1676269429Smav	be_lun->unmap = ctl_be_block_unmap_dev;
1677229997Sken
1678229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1679229997Sken	if (error) {
1680229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1681229997Sken			 "%s: error getting vnode attributes for device %s",
1682229997Sken			 __func__, be_lun->dev_path);
1683229997Sken		return (error);
1684229997Sken	}
1685229997Sken
1686229997Sken	dev = be_lun->vn->v_rdev;
1687229997Sken	devsw = dev->si_devsw;
1688229997Sken	if (!devsw->d_ioctl) {
1689229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1690229997Sken			 "%s: no d_ioctl for device %s!", __func__,
1691229997Sken			 be_lun->dev_path);
1692229997Sken		return (ENODEV);
1693229997Sken	}
1694229997Sken
1695229997Sken	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1696229997Sken			       (caddr_t)&be_lun->blocksize, FREAD,
1697229997Sken			       curthread);
1698229997Sken	if (error) {
1699229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1700229997Sken			 "%s: error %d returned for DIOCGSECTORSIZE ioctl "
1701229997Sken			 "on %s!", __func__, error, be_lun->dev_path);
1702229997Sken		return (error);
1703229997Sken	}
1704229997Sken
1705229997Sken	/*
1706229997Sken	 * If the user has asked for a blocksize that is greater than the
1707229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1708229997Sken	 * the user is asking for is an even multiple of the underlying
1709229997Sken	 * device's blocksize.
1710229997Sken	 */
1711229997Sken	if ((params->blocksize_bytes != 0)
1712229997Sken	 && (params->blocksize_bytes > be_lun->blocksize)) {
1713229997Sken		uint32_t bs_multiple, tmp_blocksize;
1714229997Sken
1715229997Sken		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1716229997Sken
1717229997Sken		tmp_blocksize = bs_multiple * be_lun->blocksize;
1718229997Sken
1719229997Sken		if (tmp_blocksize == params->blocksize_bytes) {
1720229997Sken			be_lun->blocksize = params->blocksize_bytes;
1721229997Sken		} else {
1722229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1723229997Sken				 "%s: requested blocksize %u is not an even "
1724229997Sken				 "multiple of backing device blocksize %u",
1725229997Sken				 __func__, params->blocksize_bytes,
1726229997Sken				 be_lun->blocksize);
1727229997Sken			return (EINVAL);
1728229997Sken
1729229997Sken		}
1730229997Sken	} else if ((params->blocksize_bytes != 0)
1731229997Sken		&& (params->blocksize_bytes != be_lun->blocksize)) {
1732229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1733229997Sken			 "%s: requested blocksize %u < backing device "
1734229997Sken			 "blocksize %u", __func__, params->blocksize_bytes,
1735229997Sken			 be_lun->blocksize);
1736229997Sken		return (EINVAL);
1737229997Sken	}
1738229997Sken
1739229997Sken	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1740229997Sken			       (caddr_t)&be_lun->size_bytes, FREAD,
1741229997Sken			       curthread);
1742229997Sken	if (error) {
1743229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1744232604Strasz			 "%s: error %d returned for DIOCGMEDIASIZE "
1745232604Strasz			 " ioctl on %s!", __func__, error,
1746232604Strasz			 be_lun->dev_path);
1747229997Sken		return (error);
1748229997Sken	}
1749229997Sken
1750232604Strasz	if (params->lun_size_bytes != 0) {
1751232604Strasz		if (params->lun_size_bytes > be_lun->size_bytes) {
1752232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1753232604Strasz				 "%s: requested LUN size %ju > backing device "
1754232604Strasz				 "size %ju", __func__,
1755232604Strasz				 (uintmax_t)params->lun_size_bytes,
1756232604Strasz				 (uintmax_t)be_lun->size_bytes);
1757232604Strasz			return (EINVAL);
1758232604Strasz		}
1759232604Strasz
1760232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1761232604Strasz	}
1762232604Strasz
1763264727Smav	error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1764264727Smav			       (caddr_t)&ps, FREAD, curthread);
1765264727Smav	if (error)
1766264727Smav		ps = po = 0;
1767264727Smav	else {
1768264727Smav		error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1769264727Smav				       (caddr_t)&po, FREAD, curthread);
1770264727Smav		if (error)
1771264727Smav			po = 0;
1772264727Smav	}
1773264727Smav	pss = ps / be_lun->blocksize;
1774264727Smav	pos = po / be_lun->blocksize;
1775264727Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1776264727Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1777264727Smav		be_lun->pblockexp = fls(pss) - 1;
1778264727Smav		be_lun->pblockoff = (pss - pos) % pss;
1779264727Smav	}
1780264727Smav
1781229997Sken	return (0);
1782229997Sken}
1783229997Sken
1784229997Skenstatic int
1785229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
1786229997Sken{
1787229997Sken	DROP_GIANT();
1788229997Sken	if (be_lun->vn) {
1789229997Sken		int flags = FREAD | FWRITE;
1790229997Sken
1791229997Sken		switch (be_lun->dev_type) {
1792229997Sken		case CTL_BE_BLOCK_DEV:
1793229997Sken			if (be_lun->backend.dev.csw) {
1794229997Sken				dev_relthread(be_lun->backend.dev.cdev,
1795229997Sken					      be_lun->backend.dev.dev_ref);
1796229997Sken				be_lun->backend.dev.csw  = NULL;
1797229997Sken				be_lun->backend.dev.cdev = NULL;
1798229997Sken			}
1799229997Sken			break;
1800229997Sken		case CTL_BE_BLOCK_FILE:
1801229997Sken			break;
1802229997Sken		case CTL_BE_BLOCK_NONE:
1803259304Strasz			break;
1804229997Sken		default:
1805229997Sken			panic("Unexpected backend type.");
1806229997Sken			break;
1807229997Sken		}
1808229997Sken
1809229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1810229997Sken		be_lun->vn = NULL;
1811229997Sken
1812229997Sken		switch (be_lun->dev_type) {
1813229997Sken		case CTL_BE_BLOCK_DEV:
1814229997Sken			break;
1815229997Sken		case CTL_BE_BLOCK_FILE:
1816229997Sken			if (be_lun->backend.file.cred != NULL) {
1817229997Sken				crfree(be_lun->backend.file.cred);
1818229997Sken				be_lun->backend.file.cred = NULL;
1819229997Sken			}
1820229997Sken			break;
1821229997Sken		case CTL_BE_BLOCK_NONE:
1822259304Strasz			break;
1823229997Sken		default:
1824229997Sken			panic("Unexpected backend type.");
1825229997Sken			break;
1826229997Sken		}
1827229997Sken	}
1828229997Sken	PICKUP_GIANT();
1829229997Sken
1830229997Sken	return (0);
1831229997Sken}
1832229997Sken
1833229997Skenstatic int
1834229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
1835229997Sken		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1836229997Sken{
1837229997Sken	struct nameidata nd;
1838229997Sken	int		 flags;
1839229997Sken	int		 error;
1840229997Sken
1841229997Sken	/*
1842229997Sken	 * XXX KDM allow a read-only option?
1843229997Sken	 */
1844229997Sken	flags = FREAD | FWRITE;
1845229997Sken	error = 0;
1846229997Sken
1847229997Sken	if (rootvnode == NULL) {
1848229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1849229997Sken			 "%s: Root filesystem is not mounted", __func__);
1850229997Sken		return (1);
1851229997Sken	}
1852229997Sken
1853229997Sken	if (!curthread->td_proc->p_fd->fd_cdir) {
1854229997Sken		curthread->td_proc->p_fd->fd_cdir = rootvnode;
1855229997Sken		VREF(rootvnode);
1856229997Sken	}
1857229997Sken	if (!curthread->td_proc->p_fd->fd_rdir) {
1858229997Sken		curthread->td_proc->p_fd->fd_rdir = rootvnode;
1859229997Sken		VREF(rootvnode);
1860229997Sken	}
1861229997Sken	if (!curthread->td_proc->p_fd->fd_jdir) {
1862229997Sken		curthread->td_proc->p_fd->fd_jdir = rootvnode;
1863229997Sken		VREF(rootvnode);
1864229997Sken	}
1865229997Sken
1866229997Sken again:
1867229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1868229997Sken	error = vn_open(&nd, &flags, 0, NULL);
1869229997Sken	if (error) {
1870229997Sken		/*
1871229997Sken		 * This is the only reasonable guess we can make as far as
1872229997Sken		 * path if the user doesn't give us a fully qualified path.
1873229997Sken		 * If they want to specify a file, they need to specify the
1874229997Sken		 * full path.
1875229997Sken		 */
1876229997Sken		if (be_lun->dev_path[0] != '/') {
1877229997Sken			char *dev_path = "/dev/";
1878229997Sken			char *dev_name;
1879229997Sken
1880229997Sken			/* Try adding device path at beginning of name */
1881229997Sken			dev_name = malloc(strlen(be_lun->dev_path)
1882229997Sken					+ strlen(dev_path) + 1,
1883229997Sken					  M_CTLBLK, M_WAITOK);
1884229997Sken			if (dev_name) {
1885229997Sken				sprintf(dev_name, "%s%s", dev_path,
1886229997Sken					be_lun->dev_path);
1887229997Sken				free(be_lun->dev_path, M_CTLBLK);
1888229997Sken				be_lun->dev_path = dev_name;
1889229997Sken				goto again;
1890229997Sken			}
1891229997Sken		}
1892229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1893229997Sken			 "%s: error opening %s", __func__, be_lun->dev_path);
1894229997Sken		return (error);
1895229997Sken	}
1896229997Sken
1897229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
1898229997Sken
1899229997Sken	be_lun->vn = nd.ni_vp;
1900229997Sken
1901229997Sken	/* We only support disks and files. */
1902229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
1903229997Sken		error = ctl_be_block_open_dev(be_lun, req);
1904229997Sken	} else if (be_lun->vn->v_type == VREG) {
1905229997Sken		error = ctl_be_block_open_file(be_lun, req);
1906229997Sken	} else {
1907229997Sken		error = EINVAL;
1908229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1909259304Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
1910229997Sken	}
1911229997Sken	VOP_UNLOCK(be_lun->vn, 0);
1912229997Sken
1913229997Sken	if (error != 0) {
1914229997Sken		ctl_be_block_close(be_lun);
1915229997Sken		return (error);
1916229997Sken	}
1917229997Sken
1918229997Sken	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1919229997Sken	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1920229997Sken
1921229997Sken	return (0);
1922229997Sken}
1923229997Sken
1924229997Skenstatic int
1925229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1926229997Sken{
1927229997Sken	struct ctl_be_block_lun *be_lun;
1928229997Sken	struct ctl_lun_create_params *params;
1929268143Smav	char num_thread_str[16];
1930229997Sken	char tmpstr[32];
1931268143Smav	char *value;
1932265634Smav	int retval, num_threads, unmap;
1933268143Smav	int tmp_num_threads;
1934229997Sken
1935229997Sken	params = &req->reqdata.create;
1936229997Sken	retval = 0;
1937229997Sken
1938229997Sken	num_threads = cbb_num_threads;
1939229997Sken
1940229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1941229997Sken
1942229997Sken	be_lun->softc = softc;
1943229997Sken	STAILQ_INIT(&be_lun->input_queue);
1944229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
1945229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
1946229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1947268549Smav	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
1948268549Smav	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
1949268678Smav	ctl_init_opts(&be_lun->ctl_be_lun.options,
1950268678Smav	    req->num_be_args, req->kern_be_args);
1951229997Sken
1952265642Smav	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1953260476Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1954229997Sken
1955229997Sken	if (be_lun->lun_zone == NULL) {
1956229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1957229997Sken			 "%s: error allocating UMA zone", __func__);
1958229997Sken		goto bailout_error;
1959229997Sken	}
1960229997Sken
1961229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1962229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
1963229997Sken	else
1964229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
1965229997Sken
1966229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1967268678Smav		value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
1968268146Smav		if (value == NULL) {
1969229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1970229997Sken				 "%s: no file argument specified", __func__);
1971229997Sken			goto bailout_error;
1972229997Sken		}
1973268146Smav		be_lun->dev_path = strdup(value, M_CTLBLK);
1974229997Sken
1975229997Sken		retval = ctl_be_block_open(softc, be_lun, req);
1976229997Sken		if (retval != 0) {
1977229997Sken			retval = 0;
1978229997Sken			goto bailout_error;
1979229997Sken		}
1980229997Sken
1981229997Sken		/*
1982229997Sken		 * Tell the user the size of the file/device.
1983229997Sken		 */
1984229997Sken		params->lun_size_bytes = be_lun->size_bytes;
1985229997Sken
1986229997Sken		/*
1987229997Sken		 * The maximum LBA is the size - 1.
1988229997Sken		 */
1989229997Sken		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
1990229997Sken	} else {
1991229997Sken		/*
1992229997Sken		 * For processor devices, we don't have any size.
1993229997Sken		 */
1994229997Sken		be_lun->blocksize = 0;
1995264727Smav		be_lun->pblockexp = 0;
1996264727Smav		be_lun->pblockoff = 0;
1997229997Sken		be_lun->size_blocks = 0;
1998229997Sken		be_lun->size_bytes = 0;
1999229997Sken		be_lun->ctl_be_lun.maxlba = 0;
2000229997Sken		params->lun_size_bytes = 0;
2001229997Sken
2002229997Sken		/*
2003229997Sken		 * Default to just 1 thread for processor devices.
2004229997Sken		 */
2005229997Sken		num_threads = 1;
2006229997Sken	}
2007229997Sken
2008229997Sken	/*
2009229997Sken	 * XXX This searching loop might be refactored to be combined with
2010229997Sken	 * the loop above,
2011229997Sken	 */
2012268678Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
2013268143Smav	if (value != NULL) {
2014268143Smav		tmp_num_threads = strtol(value, NULL, 0);
2015229997Sken
2016268143Smav		/*
2017268143Smav		 * We don't let the user specify less than one
2018268143Smav		 * thread, but hope he's clueful enough not to
2019268143Smav		 * specify 1000 threads.
2020268143Smav		 */
2021268143Smav		if (tmp_num_threads < 1) {
2022268143Smav			snprintf(req->error_str, sizeof(req->error_str),
2023268143Smav				 "%s: invalid number of threads %s",
2024268143Smav			         __func__, num_thread_str);
2025268143Smav			goto bailout_error;
2026229997Sken		}
2027268143Smav		num_threads = tmp_num_threads;
2028229997Sken	}
2029268143Smav	unmap = 0;
2030268678Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
2031268143Smav	if (value != NULL && strcmp(value, "on") == 0)
2032268143Smav		unmap = 1;
2033229997Sken
2034229997Sken	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2035229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2036265634Smav	if (unmap)
2037265634Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2038229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
2039229997Sken	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2040264727Smav	be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2041264727Smav	be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2042229997Sken	/* Tell the user the blocksize we ended up using */
2043229997Sken	params->blocksize_bytes = be_lun->blocksize;
2044229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2045229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2046229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2047229997Sken	} else
2048229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
2049229997Sken
2050229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2051229997Sken	be_lun->ctl_be_lun.lun_config_status =
2052229997Sken		ctl_be_block_lun_config_status;
2053229997Sken	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2054229997Sken
2055229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2056229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2057229997Sken			 softc->num_luns);
2058229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2059229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2060229997Sken			sizeof(tmpstr)));
2061229997Sken
2062229997Sken		/* Tell the user what we used for a serial number */
2063229997Sken		strncpy((char *)params->serial_num, tmpstr,
2064229997Sken			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2065229997Sken	} else {
2066229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
2067229997Sken			params->serial_num,
2068229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2069229997Sken			sizeof(params->serial_num)));
2070229997Sken	}
2071229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2072229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2073229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2074229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2075229997Sken			sizeof(tmpstr)));
2076229997Sken
2077229997Sken		/* Tell the user what we used for a device ID */
2078229997Sken		strncpy((char *)params->device_id, tmpstr,
2079229997Sken			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2080229997Sken	} else {
2081229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
2082229997Sken			params->device_id,
2083229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2084229997Sken				sizeof(params->device_id)));
2085229997Sken	}
2086229997Sken
2087229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2088229997Sken
2089229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2090229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2091229997Sken
2092229997Sken	if (be_lun->io_taskqueue == NULL) {
2093229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2094229997Sken			 "%s: Unable to create taskqueue", __func__);
2095229997Sken		goto bailout_error;
2096229997Sken	}
2097229997Sken
2098229997Sken	/*
2099229997Sken	 * Note that we start the same number of threads by default for
2100229997Sken	 * both the file case and the block device case.  For the file
2101229997Sken	 * case, we need multiple threads to allow concurrency, because the
2102229997Sken	 * vnode interface is designed to be a blocking interface.  For the
2103229997Sken	 * block device case, ZFS zvols at least will block the caller's
2104229997Sken	 * context in many instances, and so we need multiple threads to
2105229997Sken	 * overcome that problem.  Other block devices don't need as many
2106229997Sken	 * threads, but they shouldn't cause too many problems.
2107229997Sken	 *
2108229997Sken	 * If the user wants to just have a single thread for a block
2109229997Sken	 * device, he can specify that when the LUN is created, or change
2110229997Sken	 * the tunable/sysctl to alter the default number of threads.
2111229997Sken	 */
2112229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2113229997Sken					 /*num threads*/num_threads,
2114229997Sken					 /*priority*/PWAIT,
2115229997Sken					 /*thread name*/
2116229997Sken					 "%s taskq", be_lun->lunname);
2117229997Sken
2118229997Sken	if (retval != 0)
2119229997Sken		goto bailout_error;
2120229997Sken
2121229997Sken	be_lun->num_threads = num_threads;
2122229997Sken
2123229997Sken	mtx_lock(&softc->lock);
2124229997Sken	softc->num_luns++;
2125229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2126229997Sken
2127229997Sken	mtx_unlock(&softc->lock);
2128229997Sken
2129229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
2130229997Sken	if (retval != 0) {
2131229997Sken		mtx_lock(&softc->lock);
2132229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2133229997Sken			      links);
2134229997Sken		softc->num_luns--;
2135229997Sken		mtx_unlock(&softc->lock);
2136229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2137229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
2138229997Sken			"details", __func__, retval);
2139229997Sken		retval = 0;
2140229997Sken		goto bailout_error;
2141229997Sken	}
2142229997Sken
2143229997Sken	mtx_lock(&softc->lock);
2144229997Sken
2145229997Sken	/*
2146229997Sken	 * Tell the config_status routine that we're waiting so it won't
2147229997Sken	 * clean up the LUN in the event of an error.
2148229997Sken	 */
2149229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2150229997Sken
2151229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2152229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2153229997Sken		if (retval == EINTR)
2154229997Sken			break;
2155229997Sken	}
2156229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2157229997Sken
2158229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2159229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2160229997Sken			 "%s: LUN configuration error, see dmesg for details",
2161229997Sken			 __func__);
2162229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2163229997Sken			      links);
2164229997Sken		softc->num_luns--;
2165229997Sken		mtx_unlock(&softc->lock);
2166229997Sken		goto bailout_error;
2167229997Sken	} else {
2168229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2169229997Sken	}
2170229997Sken
2171229997Sken	mtx_unlock(&softc->lock);
2172229997Sken
2173229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2174229997Sken					       be_lun->blocksize,
2175229997Sken					       DEVSTAT_ALL_SUPPORTED,
2176229997Sken					       be_lun->ctl_be_lun.lun_type
2177229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
2178229997Sken					       DEVSTAT_PRIORITY_OTHER);
2179229997Sken
2180229997Sken
2181229997Sken	req->status = CTL_LUN_OK;
2182229997Sken
2183229997Sken	return (retval);
2184229997Sken
2185229997Skenbailout_error:
2186229997Sken	req->status = CTL_LUN_ERROR;
2187229997Sken
2188267754Smav	if (be_lun->io_taskqueue != NULL)
2189267754Smav		taskqueue_free(be_lun->io_taskqueue);
2190229997Sken	ctl_be_block_close(be_lun);
2191267754Smav	if (be_lun->dev_path != NULL)
2192267754Smav		free(be_lun->dev_path, M_CTLBLK);
2193267754Smav	if (be_lun->lun_zone != NULL)
2194267754Smav		uma_zdestroy(be_lun->lun_zone);
2195268678Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2196268549Smav	mtx_destroy(&be_lun->queue_lock);
2197268549Smav	mtx_destroy(&be_lun->io_lock);
2198229997Sken	free(be_lun, M_CTLBLK);
2199229997Sken
2200229997Sken	return (retval);
2201229997Sken}
2202229997Sken
2203229997Skenstatic int
2204229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2205229997Sken{
2206229997Sken	struct ctl_lun_rm_params *params;
2207229997Sken	struct ctl_be_block_lun *be_lun;
2208229997Sken	int retval;
2209229997Sken
2210229997Sken	params = &req->reqdata.rm;
2211229997Sken
2212229997Sken	mtx_lock(&softc->lock);
2213229997Sken
2214229997Sken	be_lun = NULL;
2215229997Sken
2216229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2217229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2218229997Sken			break;
2219229997Sken	}
2220229997Sken	mtx_unlock(&softc->lock);
2221229997Sken
2222229997Sken	if (be_lun == NULL) {
2223229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2224229997Sken			 "%s: LUN %u is not managed by the block backend",
2225229997Sken			 __func__, params->lun_id);
2226229997Sken		goto bailout_error;
2227229997Sken	}
2228229997Sken
2229229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2230229997Sken
2231229997Sken	if (retval != 0) {
2232229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2233229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
2234229997Sken			 "LUN %d", __func__, retval, params->lun_id);
2235229997Sken		goto bailout_error;
2236229997Sken
2237229997Sken	}
2238229997Sken
2239229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2240229997Sken	if (retval != 0) {
2241229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2242229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
2243229997Sken			 "LUN %d", __func__, retval, params->lun_id);
2244229997Sken		goto bailout_error;
2245229997Sken	}
2246229997Sken
2247229997Sken	mtx_lock(&softc->lock);
2248229997Sken
2249229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2250229997Sken
2251229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2252229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2253229997Sken                if (retval == EINTR)
2254229997Sken                        break;
2255229997Sken        }
2256229997Sken
2257229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2258229997Sken
2259229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2260229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2261229997Sken			 "%s: interrupted waiting for LUN to be freed",
2262229997Sken			 __func__);
2263229997Sken		mtx_unlock(&softc->lock);
2264229997Sken		goto bailout_error;
2265229997Sken	}
2266229997Sken
2267229997Sken	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2268229997Sken
2269229997Sken	softc->num_luns--;
2270229997Sken	mtx_unlock(&softc->lock);
2271229997Sken
2272229997Sken	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2273229997Sken
2274229997Sken	taskqueue_free(be_lun->io_taskqueue);
2275229997Sken
2276229997Sken	ctl_be_block_close(be_lun);
2277229997Sken
2278229997Sken	if (be_lun->disk_stats != NULL)
2279229997Sken		devstat_remove_entry(be_lun->disk_stats);
2280229997Sken
2281229997Sken	uma_zdestroy(be_lun->lun_zone);
2282229997Sken
2283268678Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2284229997Sken	free(be_lun->dev_path, M_CTLBLK);
2285268549Smav	mtx_destroy(&be_lun->queue_lock);
2286268549Smav	mtx_destroy(&be_lun->io_lock);
2287229997Sken	free(be_lun, M_CTLBLK);
2288229997Sken
2289229997Sken	req->status = CTL_LUN_OK;
2290229997Sken
2291229997Sken	return (0);
2292229997Sken
2293229997Skenbailout_error:
2294229997Sken
2295229997Sken	req->status = CTL_LUN_ERROR;
2296229997Sken
2297229997Sken	return (0);
2298229997Sken}
2299229997Sken
2300232604Straszstatic int
2301232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2302232604Strasz			 struct ctl_lun_req *req)
2303232604Strasz{
2304232604Strasz	struct vattr vattr;
2305232604Strasz	int error;
2306232604Strasz	struct ctl_lun_modify_params *params;
2307232604Strasz
2308232604Strasz	params = &req->reqdata.modify;
2309232604Strasz
2310232604Strasz	if (params->lun_size_bytes != 0) {
2311232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2312232604Strasz	} else  {
2313271928Smav		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2314232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2315271928Smav		VOP_UNLOCK(be_lun->vn, 0);
2316232604Strasz		if (error != 0) {
2317232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2318232604Strasz				 "error calling VOP_GETATTR() for file %s",
2319232604Strasz				 be_lun->dev_path);
2320232604Strasz			return (error);
2321232604Strasz		}
2322232604Strasz
2323232604Strasz		be_lun->size_bytes = vattr.va_size;
2324232604Strasz	}
2325232604Strasz
2326232604Strasz	return (0);
2327232604Strasz}
2328232604Strasz
2329232604Straszstatic int
2330232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2331232604Strasz			struct ctl_lun_req *req)
2332232604Strasz{
2333271928Smav	struct ctl_be_block_devdata *dev_data;
2334232604Strasz	int error;
2335232604Strasz	struct ctl_lun_modify_params *params;
2336232604Strasz	uint64_t size_bytes;
2337232604Strasz
2338232604Strasz	params = &req->reqdata.modify;
2339232604Strasz
2340271928Smav	dev_data = &be_lun->backend.dev;
2341271928Smav	if (!dev_data->csw->d_ioctl) {
2342232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2343232604Strasz			 "%s: no d_ioctl for device %s!", __func__,
2344232604Strasz			 be_lun->dev_path);
2345232604Strasz		return (ENODEV);
2346232604Strasz	}
2347232604Strasz
2348271928Smav	error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2349232604Strasz			       (caddr_t)&size_bytes, FREAD,
2350232604Strasz			       curthread);
2351232604Strasz	if (error) {
2352232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2353232604Strasz			 "%s: error %d returned for DIOCGMEDIASIZE ioctl "
2354232604Strasz			 "on %s!", __func__, error, be_lun->dev_path);
2355232604Strasz		return (error);
2356232604Strasz	}
2357232604Strasz
2358232604Strasz	if (params->lun_size_bytes != 0) {
2359232604Strasz		if (params->lun_size_bytes > size_bytes) {
2360232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2361232604Strasz				 "%s: requested LUN size %ju > backing device "
2362232604Strasz				 "size %ju", __func__,
2363232604Strasz				 (uintmax_t)params->lun_size_bytes,
2364232604Strasz				 (uintmax_t)size_bytes);
2365232604Strasz			return (EINVAL);
2366232604Strasz		}
2367232604Strasz
2368232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2369232604Strasz	} else {
2370232604Strasz		be_lun->size_bytes = size_bytes;
2371232604Strasz	}
2372232604Strasz
2373232604Strasz	return (0);
2374232604Strasz}
2375232604Strasz
2376232604Straszstatic int
2377232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2378232604Strasz{
2379232604Strasz	struct ctl_lun_modify_params *params;
2380232604Strasz	struct ctl_be_block_lun *be_lun;
2381271928Smav	uint64_t oldsize;
2382241896Skib	int error;
2383232604Strasz
2384232604Strasz	params = &req->reqdata.modify;
2385232604Strasz
2386232604Strasz	mtx_lock(&softc->lock);
2387232604Strasz
2388232604Strasz	be_lun = NULL;
2389232604Strasz
2390232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2391232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2392232604Strasz			break;
2393232604Strasz	}
2394232604Strasz	mtx_unlock(&softc->lock);
2395232604Strasz
2396232604Strasz	if (be_lun == NULL) {
2397232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2398232604Strasz			 "%s: LUN %u is not managed by the block backend",
2399232604Strasz			 __func__, params->lun_id);
2400232604Strasz		goto bailout_error;
2401232604Strasz	}
2402232604Strasz
2403232604Strasz	if (params->lun_size_bytes != 0) {
2404232604Strasz		if (params->lun_size_bytes < be_lun->blocksize) {
2405232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2406232604Strasz				"%s: LUN size %ju < blocksize %u", __func__,
2407232604Strasz				params->lun_size_bytes, be_lun->blocksize);
2408232604Strasz			goto bailout_error;
2409232604Strasz		}
2410232604Strasz	}
2411232604Strasz
2412271928Smav	oldsize = be_lun->size_bytes;
2413232604Strasz	if (be_lun->vn->v_type == VREG)
2414232604Strasz		error = ctl_be_block_modify_file(be_lun, req);
2415232604Strasz	else
2416232604Strasz		error = ctl_be_block_modify_dev(be_lun, req);
2417232604Strasz	if (error != 0)
2418232604Strasz		goto bailout_error;
2419232604Strasz
2420271928Smav	if (be_lun->size_bytes != oldsize) {
2421271928Smav		be_lun->size_blocks = be_lun->size_bytes >>
2422271928Smav		    be_lun->blocksize_shift;
2423232604Strasz
2424271928Smav		/*
2425271928Smav		 * The maximum LBA is the size - 1.
2426271928Smav		 *
2427271928Smav		 * XXX: Note that this field is being updated without locking,
2428271928Smav		 * 	which might cause problems on 32-bit architectures.
2429271928Smav		 */
2430271928Smav		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
2431271928Smav		ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2432271928Smav	}
2433232604Strasz
2434232604Strasz	/* Tell the user the exact size we ended up using */
2435232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2436232604Strasz
2437232604Strasz	req->status = CTL_LUN_OK;
2438232604Strasz
2439232604Strasz	return (0);
2440232604Strasz
2441232604Straszbailout_error:
2442232604Strasz	req->status = CTL_LUN_ERROR;
2443232604Strasz
2444232604Strasz	return (0);
2445232604Strasz}
2446232604Strasz
2447229997Skenstatic void
2448229997Skenctl_be_block_lun_shutdown(void *be_lun)
2449229997Sken{
2450229997Sken	struct ctl_be_block_lun *lun;
2451229997Sken	struct ctl_be_block_softc *softc;
2452229997Sken
2453229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2454229997Sken
2455229997Sken	softc = lun->softc;
2456229997Sken
2457229997Sken	mtx_lock(&softc->lock);
2458229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2459229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2460229997Sken		wakeup(lun);
2461229997Sken	mtx_unlock(&softc->lock);
2462229997Sken
2463229997Sken}
2464229997Sken
2465229997Skenstatic void
2466229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2467229997Sken{
2468229997Sken	struct ctl_be_block_lun *lun;
2469229997Sken	struct ctl_be_block_softc *softc;
2470229997Sken
2471229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2472229997Sken	softc = lun->softc;
2473229997Sken
2474229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2475229997Sken		mtx_lock(&softc->lock);
2476229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2477229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2478229997Sken			wakeup(lun);
2479229997Sken		mtx_unlock(&softc->lock);
2480229997Sken
2481229997Sken		/*
2482229997Sken		 * We successfully added the LUN, attempt to enable it.
2483229997Sken		 */
2484229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2485229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2486229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2487229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2488229997Sken				       __func__);
2489229997Sken			}
2490229997Sken		}
2491229997Sken
2492229997Sken		return;
2493229997Sken	}
2494229997Sken
2495229997Sken
2496229997Sken	mtx_lock(&softc->lock);
2497229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2498229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2499229997Sken	wakeup(lun);
2500229997Sken	mtx_unlock(&softc->lock);
2501229997Sken}
2502229997Sken
2503229997Sken
2504229997Skenstatic int
2505229997Skenctl_be_block_config_write(union ctl_io *io)
2506229997Sken{
2507229997Sken	struct ctl_be_block_lun *be_lun;
2508229997Sken	struct ctl_be_lun *ctl_be_lun;
2509229997Sken	int retval;
2510229997Sken
2511229997Sken	retval = 0;
2512229997Sken
2513229997Sken	DPRINTF("entered\n");
2514229997Sken
2515229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2516229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2517229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2518229997Sken
2519229997Sken	switch (io->scsiio.cdb[0]) {
2520229997Sken	case SYNCHRONIZE_CACHE:
2521229997Sken	case SYNCHRONIZE_CACHE_16:
2522265634Smav	case WRITE_SAME_10:
2523265634Smav	case WRITE_SAME_16:
2524265634Smav	case UNMAP:
2525229997Sken		/*
2526229997Sken		 * The upper level CTL code will filter out any CDBs with
2527229997Sken		 * the immediate bit set and return the proper error.
2528229997Sken		 *
2529229997Sken		 * We don't really need to worry about what LBA range the
2530229997Sken		 * user asked to be synced out.  When they issue a sync
2531229997Sken		 * cache command, we'll sync out the whole thing.
2532229997Sken		 */
2533268549Smav		mtx_lock(&be_lun->queue_lock);
2534229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2535229997Sken				   links);
2536268549Smav		mtx_unlock(&be_lun->queue_lock);
2537229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2538229997Sken		break;
2539229997Sken	case START_STOP_UNIT: {
2540229997Sken		struct scsi_start_stop_unit *cdb;
2541229997Sken
2542229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2543229997Sken
2544229997Sken		if (cdb->how & SSS_START)
2545229997Sken			retval = ctl_start_lun(ctl_be_lun);
2546229997Sken		else {
2547229997Sken			retval = ctl_stop_lun(ctl_be_lun);
2548229997Sken			/*
2549229997Sken			 * XXX KDM Copan-specific offline behavior.
2550229997Sken			 * Figure out a reasonable way to port this?
2551229997Sken			 */
2552229997Sken#ifdef NEEDTOPORT
2553229997Sken			if ((retval == 0)
2554229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2555229997Sken				retval = ctl_lun_offline(ctl_be_lun);
2556229997Sken#endif
2557229997Sken		}
2558229997Sken
2559229997Sken		/*
2560229997Sken		 * In general, the above routines should not fail.  They
2561229997Sken		 * just set state for the LUN.  So we've got something
2562229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2563229997Sken		 */
2564229997Sken		if (retval != 0) {
2565229997Sken			ctl_set_internal_failure(&io->scsiio,
2566229997Sken						 /*sks_valid*/ 1,
2567229997Sken						 /*retry_count*/ 0xf051);
2568229997Sken			retval = CTL_RETVAL_COMPLETE;
2569229997Sken		} else {
2570229997Sken			ctl_set_success(&io->scsiio);
2571229997Sken		}
2572229997Sken		ctl_config_write_done(io);
2573229997Sken		break;
2574229997Sken	}
2575229997Sken	default:
2576229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2577229997Sken		ctl_config_write_done(io);
2578229997Sken		retval = CTL_RETVAL_COMPLETE;
2579229997Sken		break;
2580229997Sken	}
2581229997Sken
2582229997Sken	return (retval);
2583229997Sken
2584229997Sken}
2585229997Sken
2586229997Skenstatic int
2587229997Skenctl_be_block_config_read(union ctl_io *io)
2588229997Sken{
2589229997Sken	return (0);
2590229997Sken}
2591229997Sken
2592229997Skenstatic int
2593229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2594229997Sken{
2595229997Sken	struct ctl_be_block_lun *lun;
2596229997Sken	int retval;
2597229997Sken
2598229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2599229997Sken	retval = 0;
2600229997Sken
2601268555Smav	retval = sbuf_printf(sb, "\t<num_threads>");
2602229997Sken
2603229997Sken	if (retval != 0)
2604229997Sken		goto bailout;
2605229997Sken
2606229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2607229997Sken
2608229997Sken	if (retval != 0)
2609229997Sken		goto bailout;
2610229997Sken
2611268555Smav	retval = sbuf_printf(sb, "</num_threads>\n");
2612229997Sken
2613229997Skenbailout:
2614229997Sken
2615229997Sken	return (retval);
2616229997Sken}
2617229997Sken
2618229997Skenint
2619229997Skenctl_be_block_init(void)
2620229997Sken{
2621229997Sken	struct ctl_be_block_softc *softc;
2622229997Sken	int retval;
2623229997Sken
2624229997Sken	softc = &backend_block_softc;
2625229997Sken	retval = 0;
2626229997Sken
2627268549Smav	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2628265494Strasz	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2629265494Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2630229997Sken	STAILQ_INIT(&softc->disk_list);
2631229997Sken	STAILQ_INIT(&softc->lun_list);
2632229997Sken
2633229997Sken	return (retval);
2634229997Sken}
2635