ctl_backend_block.c revision 271794
1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation
4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation
5229997Sken * All rights reserved.
6229997Sken *
7232604Strasz * Portions of this software were developed by Edward Tomasz Napierala
8232604Strasz * under sponsorship from the FreeBSD Foundation.
9232604Strasz *
10229997Sken * Redistribution and use in source and binary forms, with or without
11229997Sken * modification, are permitted provided that the following conditions
12229997Sken * are met:
13229997Sken * 1. Redistributions of source code must retain the above copyright
14229997Sken *    notice, this list of conditions, and the following disclaimer,
15229997Sken *    without modification.
16229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
18229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
19229997Sken *    including a substantially similar Disclaimer requirement for further
20229997Sken *    binary redistribution.
21229997Sken *
22229997Sken * NO WARRANTY
23229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33229997Sken * POSSIBILITY OF SUCH DAMAGES.
34229997Sken *
35229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
36229997Sken */
37229997Sken/*
38229997Sken * CAM Target Layer driver backend for block devices.
39229997Sken *
40229997Sken * Author: Ken Merry <ken@FreeBSD.org>
41229997Sken */
42229997Sken#include <sys/cdefs.h>
43229997Sken__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 271794 2014-09-18 17:25:20Z mav $");
44229997Sken
45229997Sken#include <sys/param.h>
46229997Sken#include <sys/systm.h>
47229997Sken#include <sys/kernel.h>
48229997Sken#include <sys/types.h>
49229997Sken#include <sys/kthread.h>
50229997Sken#include <sys/bio.h>
51229997Sken#include <sys/fcntl.h>
52264274Smav#include <sys/limits.h>
53229997Sken#include <sys/lock.h>
54229997Sken#include <sys/mutex.h>
55229997Sken#include <sys/condvar.h>
56229997Sken#include <sys/malloc.h>
57229997Sken#include <sys/conf.h>
58229997Sken#include <sys/ioccom.h>
59229997Sken#include <sys/queue.h>
60229997Sken#include <sys/sbuf.h>
61229997Sken#include <sys/endian.h>
62229997Sken#include <sys/uio.h>
63229997Sken#include <sys/buf.h>
64229997Sken#include <sys/taskqueue.h>
65229997Sken#include <sys/vnode.h>
66229997Sken#include <sys/namei.h>
67229997Sken#include <sys/mount.h>
68229997Sken#include <sys/disk.h>
69229997Sken#include <sys/fcntl.h>
70229997Sken#include <sys/filedesc.h>
71229997Sken#include <sys/proc.h>
72229997Sken#include <sys/pcpu.h>
73229997Sken#include <sys/module.h>
74229997Sken#include <sys/sdt.h>
75229997Sken#include <sys/devicestat.h>
76229997Sken#include <sys/sysctl.h>
77229997Sken
78229997Sken#include <geom/geom.h>
79229997Sken
80229997Sken#include <cam/cam.h>
81229997Sken#include <cam/scsi/scsi_all.h>
82229997Sken#include <cam/scsi/scsi_da.h>
83229997Sken#include <cam/ctl/ctl_io.h>
84229997Sken#include <cam/ctl/ctl.h>
85229997Sken#include <cam/ctl/ctl_backend.h>
86229997Sken#include <cam/ctl/ctl_frontend_internal.h>
87229997Sken#include <cam/ctl/ctl_ioctl.h>
88229997Sken#include <cam/ctl/ctl_scsi_all.h>
89229997Sken#include <cam/ctl/ctl_error.h>
90229997Sken
91229997Sken/*
92264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB
93264886Smav * I/O.  If we get an I/O larger than that, we'll split it.
94229997Sken */
95267537Smav#define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
96267537Smav#define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
97264886Smav#define	CTLBLK_MAX_SEG		MAXPHYS
98267537Smav#define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
99267537Smav#define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
100229997Sken
101229997Sken#ifdef CTLBLK_DEBUG
102229997Sken#define DPRINTF(fmt, args...) \
103229997Sken    printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
104229997Sken#else
105229997Sken#define DPRINTF(fmt, args...) do {} while(0)
106229997Sken#endif
107229997Sken
108267519Smav#define PRIV(io)	\
109267519Smav    ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
110267537Smav#define ARGS(io)	\
111267537Smav    ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
112267519Smav
113229997SkenSDT_PROVIDER_DEFINE(cbb);
114229997Sken
115229997Skentypedef enum {
116229997Sken	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
117229997Sken	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
118229997Sken	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
119229997Sken	CTL_BE_BLOCK_LUN_MULTI_THREAD	= 0x08
120229997Sken} ctl_be_block_lun_flags;
121229997Sken
122229997Skentypedef enum {
123229997Sken	CTL_BE_BLOCK_NONE,
124229997Sken	CTL_BE_BLOCK_DEV,
125229997Sken	CTL_BE_BLOCK_FILE
126229997Sken} ctl_be_block_type;
127229997Sken
128229997Skenstruct ctl_be_block_devdata {
129229997Sken	struct cdev *cdev;
130229997Sken	struct cdevsw *csw;
131229997Sken	int dev_ref;
132229997Sken};
133229997Sken
134229997Skenstruct ctl_be_block_filedata {
135229997Sken	struct ucred *cred;
136229997Sken};
137229997Sken
138229997Skenunion ctl_be_block_bedata {
139229997Sken	struct ctl_be_block_devdata dev;
140229997Sken	struct ctl_be_block_filedata file;
141229997Sken};
142229997Sken
143229997Skenstruct ctl_be_block_io;
144229997Skenstruct ctl_be_block_lun;
145229997Sken
146229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
147229997Sken			       struct ctl_be_block_io *beio);
148229997Sken
149229997Sken/*
150229997Sken * Backend LUN structure.  There is a 1:1 mapping between a block device
151229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN.
152229997Sken */
153229997Skenstruct ctl_be_block_lun {
154229997Sken	struct ctl_block_disk *disk;
155229997Sken	char lunname[32];
156229997Sken	char *dev_path;
157229997Sken	ctl_be_block_type dev_type;
158229997Sken	struct vnode *vn;
159229997Sken	union ctl_be_block_bedata backend;
160229997Sken	cbb_dispatch_t dispatch;
161229997Sken	cbb_dispatch_t lun_flush;
162264274Smav	cbb_dispatch_t unmap;
163229997Sken	uma_zone_t lun_zone;
164229997Sken	uint64_t size_blocks;
165229997Sken	uint64_t size_bytes;
166229997Sken	uint32_t blocksize;
167229997Sken	int blocksize_shift;
168264191Smav	uint16_t pblockexp;
169264191Smav	uint16_t pblockoff;
170229997Sken	struct ctl_be_block_softc *softc;
171229997Sken	struct devstat *disk_stats;
172229997Sken	ctl_be_block_lun_flags flags;
173229997Sken	STAILQ_ENTRY(ctl_be_block_lun) links;
174229997Sken	struct ctl_be_lun ctl_be_lun;
175229997Sken	struct taskqueue *io_taskqueue;
176229997Sken	struct task io_task;
177229997Sken	int num_threads;
178229997Sken	STAILQ_HEAD(, ctl_io_hdr) input_queue;
179229997Sken	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
180229997Sken	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
181267877Smav	struct mtx_padalign io_lock;
182267877Smav	struct mtx_padalign queue_lock;
183229997Sken};
184229997Sken
185229997Sken/*
186229997Sken * Overall softc structure for the block backend module.
187229997Sken */
188229997Skenstruct ctl_be_block_softc {
189229997Sken	struct mtx			 lock;
190229997Sken	int				 num_disks;
191229997Sken	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
192229997Sken	int				 num_luns;
193229997Sken	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
194229997Sken};
195229997Sken
196229997Skenstatic struct ctl_be_block_softc backend_block_softc;
197229997Sken
198229997Sken/*
199229997Sken * Per-I/O information.
200229997Sken */
201229997Skenstruct ctl_be_block_io {
202229997Sken	union ctl_io			*io;
203229997Sken	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
204229997Sken	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
205229997Sken	int				bio_cmd;
206229997Sken	int				num_segs;
207229997Sken	int				num_bios_sent;
208229997Sken	int				num_bios_done;
209229997Sken	int				send_complete;
210229997Sken	int				num_errors;
211229997Sken	struct bintime			ds_t0;
212229997Sken	devstat_tag_type		ds_tag_type;
213229997Sken	devstat_trans_flags		ds_trans_type;
214229997Sken	uint64_t			io_len;
215229997Sken	uint64_t			io_offset;
216229997Sken	struct ctl_be_block_softc	*softc;
217229997Sken	struct ctl_be_block_lun		*lun;
218264274Smav	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
219229997Sken};
220229997Sken
221229997Skenstatic int cbb_num_threads = 14;
222229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
223229997Sken	    "CAM Target Layer Block Backend");
224267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
225229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
226229997Sken
227229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
228229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
229229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
230229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
231229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
232229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
233229997Sken				    struct ctl_be_block_io *beio);
234229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
235229997Sken				       struct ctl_be_block_io *beio);
236229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
237229997Sken				   struct ctl_be_block_io *beio);
238264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
239264274Smav				   struct ctl_be_block_io *beio);
240229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
241229997Sken				      struct ctl_be_block_io *beio);
242229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
243229997Sken				    union ctl_io *io);
244229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
245229997Sken				  union ctl_io *io);
246229997Skenstatic void ctl_be_block_worker(void *context, int pending);
247229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
248229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
249229997Sken				   int flag, struct thread *td);
250229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
251229997Sken				  struct ctl_lun_req *req);
252229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
253229997Sken				 struct ctl_lun_req *req);
254229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
255229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
256229997Sken			     struct ctl_be_block_lun *be_lun,
257229997Sken			     struct ctl_lun_req *req);
258229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
259229997Sken			       struct ctl_lun_req *req);
260229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
261229997Sken			   struct ctl_lun_req *req);
262232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
263232604Strasz				  struct ctl_lun_req *req);
264232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
265232604Strasz				 struct ctl_lun_req *req);
266232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
267232604Strasz			   struct ctl_lun_req *req);
268229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
269229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
270229997Sken					   ctl_lun_config_status status);
271229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
272229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
273229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
274229997Skenint ctl_be_block_init(void);
275229997Sken
276229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
277229997Sken{
278230334Sken	.name = "block",
279230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
280230334Sken	.init = ctl_be_block_init,
281230334Sken	.data_submit = ctl_be_block_submit,
282230334Sken	.data_move_done = ctl_be_block_move_done,
283230334Sken	.config_read = ctl_be_block_config_read,
284230334Sken	.config_write = ctl_be_block_config_write,
285230334Sken	.ioctl = ctl_be_block_ioctl,
286230334Sken	.lun_info = ctl_be_block_lun_info
287229997Sken};
288229997Sken
289229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
290229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
291229997Sken
292264020Straszstatic uma_zone_t beio_zone;
293264020Strasz
294229997Skenstatic struct ctl_be_block_io *
295229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
296229997Sken{
297229997Sken	struct ctl_be_block_io *beio;
298229997Sken
299264020Strasz	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
300264020Strasz	beio->softc = softc;
301229997Sken	return (beio);
302229997Sken}
303229997Sken
304229997Skenstatic void
305229997Skenctl_free_beio(struct ctl_be_block_io *beio)
306229997Sken{
307229997Sken	int duplicate_free;
308229997Sken	int i;
309229997Sken
310229997Sken	duplicate_free = 0;
311229997Sken
312229997Sken	for (i = 0; i < beio->num_segs; i++) {
313229997Sken		if (beio->sg_segs[i].addr == NULL)
314229997Sken			duplicate_free++;
315229997Sken
316229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
317229997Sken		beio->sg_segs[i].addr = NULL;
318267537Smav
319267537Smav		/* For compare we had two equal S/G lists. */
320267537Smav		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
321267537Smav			uma_zfree(beio->lun->lun_zone,
322267537Smav			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
323267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
324267537Smav		}
325229997Sken	}
326229997Sken
327229997Sken	if (duplicate_free > 0) {
328229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
329229997Sken		       duplicate_free, beio->num_segs);
330229997Sken	}
331229997Sken
332264020Strasz	uma_zfree(beio_zone, beio);
333229997Sken}
334229997Sken
335229997Skenstatic void
336229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
337229997Sken{
338267877Smav	union ctl_io *io = beio->io;
339229997Sken
340264274Smav	if (beio->beio_cont != NULL) {
341264274Smav		beio->beio_cont(beio);
342264274Smav	} else {
343264274Smav		ctl_free_beio(beio);
344267537Smav		ctl_data_submit_done(io);
345264274Smav	}
346229997Sken}
347229997Sken
348229997Skenstatic int
349229997Skenctl_be_block_move_done(union ctl_io *io)
350229997Sken{
351229997Sken	struct ctl_be_block_io *beio;
352229997Sken	struct ctl_be_block_lun *be_lun;
353267537Smav	struct ctl_lba_len_flags *lbalen;
354229997Sken#ifdef CTL_TIME_IO
355229997Sken	struct bintime cur_bt;
356267537Smav#endif
357267537Smav	int i;
358229997Sken
359267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
360229997Sken	be_lun = beio->lun;
361229997Sken
362229997Sken	DPRINTF("entered\n");
363229997Sken
364229997Sken#ifdef CTL_TIME_IO
365229997Sken	getbintime(&cur_bt);
366229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
367229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
368229997Sken	io->io_hdr.num_dmas++;
369229997Sken#endif
370267537Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
371229997Sken
372229997Sken	/*
373229997Sken	 * We set status at this point for read commands, and write
374229997Sken	 * commands with errors.
375229997Sken	 */
376267537Smav	if ((io->io_hdr.port_status == 0) &&
377267537Smav	    ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) &&
378267537Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
379267537Smav		lbalen = ARGS(beio->io);
380267537Smav		if (lbalen->flags & CTL_LLF_READ) {
381267537Smav			ctl_set_success(&io->scsiio);
382267537Smav		} else if (lbalen->flags & CTL_LLF_COMPARE) {
383267537Smav			/* We have two data blocks ready for comparison. */
384267537Smav			for (i = 0; i < beio->num_segs; i++) {
385267537Smav				if (memcmp(beio->sg_segs[i].addr,
386267537Smav				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
387267537Smav				    beio->sg_segs[i].len) != 0)
388267537Smav					break;
389267537Smav			}
390267537Smav			if (i < beio->num_segs)
391267537Smav				ctl_set_sense(&io->scsiio,
392267537Smav				    /*current_error*/ 1,
393267537Smav				    /*sense_key*/ SSD_KEY_MISCOMPARE,
394267537Smav				    /*asc*/ 0x1D,
395267537Smav				    /*ascq*/ 0x00,
396267537Smav				    SSD_ELEM_NONE);
397267537Smav			else
398267537Smav				ctl_set_success(&io->scsiio);
399267537Smav		}
400267537Smav	}
401229997Sken	else if ((io->io_hdr.port_status != 0)
402229997Sken	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
403229997Sken	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
404229997Sken		/*
405229997Sken		 * For hardware error sense keys, the sense key
406229997Sken		 * specific value is defined to be a retry count,
407229997Sken		 * but we use it to pass back an internal FETD
408229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
409229997Sken		 * using 16 bits for an error code, since that's
410229997Sken		 * all the space we have in the sks field.
411229997Sken		 */
412229997Sken		ctl_set_internal_failure(&io->scsiio,
413229997Sken					 /*sks_valid*/ 1,
414229997Sken					 /*retry_count*/
415229997Sken					 io->io_hdr.port_status);
416229997Sken	}
417229997Sken
418229997Sken	/*
419229997Sken	 * If this is a read, or a write with errors, it is done.
420229997Sken	 */
421229997Sken	if ((beio->bio_cmd == BIO_READ)
422229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
423229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
424229997Sken		ctl_complete_beio(beio);
425229997Sken		return (0);
426229997Sken	}
427229997Sken
428229997Sken	/*
429229997Sken	 * At this point, we have a write and the DMA completed
430229997Sken	 * successfully.  We now have to queue it to the task queue to
431229997Sken	 * execute the backend I/O.  That is because we do blocking
432229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
433229997Sken	 * This move done routine is generally called in the SIM's
434229997Sken	 * interrupt context, and therefore we cannot block.
435229997Sken	 */
436267877Smav	mtx_lock(&be_lun->queue_lock);
437229997Sken	/*
438229997Sken	 * XXX KDM make sure that links is okay to use at this point.
439229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
440229997Sken	 * or deal with resource allocation here.
441229997Sken	 */
442229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
443267877Smav	mtx_unlock(&be_lun->queue_lock);
444229997Sken
445229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
446229997Sken
447229997Sken	return (0);
448229997Sken}
449229997Sken
450229997Skenstatic void
451229997Skenctl_be_block_biodone(struct bio *bio)
452229997Sken{
453229997Sken	struct ctl_be_block_io *beio;
454229997Sken	struct ctl_be_block_lun *be_lun;
455229997Sken	union ctl_io *io;
456261538Smav	int error;
457229997Sken
458229997Sken	beio = bio->bio_caller1;
459229997Sken	be_lun = beio->lun;
460229997Sken	io = beio->io;
461229997Sken
462229997Sken	DPRINTF("entered\n");
463229997Sken
464261538Smav	error = bio->bio_error;
465267877Smav	mtx_lock(&be_lun->io_lock);
466261538Smav	if (error != 0)
467229997Sken		beio->num_errors++;
468229997Sken
469229997Sken	beio->num_bios_done++;
470229997Sken
471229997Sken	/*
472229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
473229997Sken	 * during the free might cause it to complain.
474229997Sken	 */
475229997Sken	g_destroy_bio(bio);
476229997Sken
477229997Sken	/*
478229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
479229997Sken	 * complete, then we're done.
480229997Sken	 */
481229997Sken	if ((beio->send_complete == 0)
482229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
483267877Smav		mtx_unlock(&be_lun->io_lock);
484229997Sken		return;
485229997Sken	}
486229997Sken
487229997Sken	/*
488229997Sken	 * At this point, we've verified that we are the last I/O to
489229997Sken	 * complete, so it's safe to drop the lock.
490229997Sken	 */
491267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
492267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
493267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
494267877Smav	mtx_unlock(&be_lun->io_lock);
495229997Sken
496229997Sken	/*
497229997Sken	 * If there are any errors from the backing device, we fail the
498229997Sken	 * entire I/O with a medium error.
499229997Sken	 */
500229997Sken	if (beio->num_errors > 0) {
501261538Smav		if (error == EOPNOTSUPP) {
502261538Smav			ctl_set_invalid_opcode(&io->scsiio);
503261538Smav		} else if (beio->bio_cmd == BIO_FLUSH) {
504229997Sken			/* XXX KDM is there is a better error here? */
505229997Sken			ctl_set_internal_failure(&io->scsiio,
506229997Sken						 /*sks_valid*/ 1,
507229997Sken						 /*retry_count*/ 0xbad2);
508229997Sken		} else
509229997Sken			ctl_set_medium_error(&io->scsiio);
510229997Sken		ctl_complete_beio(beio);
511229997Sken		return;
512229997Sken	}
513229997Sken
514229997Sken	/*
515267537Smav	 * If this is a write, a flush, a delete or verify, we're all done.
516229997Sken	 * If this is a read, we can now send the data to the user.
517229997Sken	 */
518229997Sken	if ((beio->bio_cmd == BIO_WRITE)
519264274Smav	 || (beio->bio_cmd == BIO_FLUSH)
520267537Smav	 || (beio->bio_cmd == BIO_DELETE)
521267537Smav	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
522229997Sken		ctl_set_success(&io->scsiio);
523229997Sken		ctl_complete_beio(beio);
524229997Sken	} else {
525229997Sken#ifdef CTL_TIME_IO
526229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
527229997Sken#endif
528229997Sken		ctl_datamove(io);
529229997Sken	}
530229997Sken}
531229997Sken
532229997Skenstatic void
533229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
534229997Sken			struct ctl_be_block_io *beio)
535229997Sken{
536267877Smav	union ctl_io *io = beio->io;
537229997Sken	struct mount *mountpoint;
538241896Skib	int error, lock_flags;
539229997Sken
540229997Sken	DPRINTF("entered\n");
541229997Sken
542267877Smav	binuptime(&beio->ds_t0);
543267877Smav	mtx_lock(&be_lun->io_lock);
544267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
545267877Smav	mtx_unlock(&be_lun->io_lock);
546229997Sken
547267877Smav	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
548229997Sken
549229997Sken	if (MNT_SHARED_WRITES(mountpoint)
550229997Sken	 || ((mountpoint == NULL)
551229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
552229997Sken		lock_flags = LK_SHARED;
553229997Sken	else
554229997Sken		lock_flags = LK_EXCLUSIVE;
555229997Sken
556229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
557229997Sken
558229997Sken	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
559229997Sken	VOP_UNLOCK(be_lun->vn, 0);
560229997Sken
561229997Sken	vn_finished_write(mountpoint);
562229997Sken
563267877Smav	mtx_lock(&be_lun->io_lock);
564267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
565267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
566267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
567267877Smav	mtx_unlock(&be_lun->io_lock);
568267877Smav
569229997Sken	if (error == 0)
570229997Sken		ctl_set_success(&io->scsiio);
571229997Sken	else {
572229997Sken		/* XXX KDM is there is a better error here? */
573229997Sken		ctl_set_internal_failure(&io->scsiio,
574229997Sken					 /*sks_valid*/ 1,
575229997Sken					 /*retry_count*/ 0xbad1);
576229997Sken	}
577229997Sken
578229997Sken	ctl_complete_beio(beio);
579229997Sken}
580229997Sken
581258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
582258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
583258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
584258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
585229997Sken
586229997Skenstatic void
587229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
588229997Sken			   struct ctl_be_block_io *beio)
589229997Sken{
590229997Sken	struct ctl_be_block_filedata *file_data;
591229997Sken	union ctl_io *io;
592229997Sken	struct uio xuio;
593229997Sken	struct iovec *xiovec;
594241896Skib	int flags;
595229997Sken	int error, i;
596229997Sken
597229997Sken	DPRINTF("entered\n");
598229997Sken
599229997Sken	file_data = &be_lun->backend.file;
600229997Sken	io = beio->io;
601271309Smav	flags = 0;
602271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
603271309Smav		flags |= IO_DIRECT;
604271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
605271309Smav		flags |= IO_SYNC;
606229997Sken
607267537Smav	bzero(&xuio, sizeof(xuio));
608229997Sken	if (beio->bio_cmd == BIO_READ) {
609229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
610267537Smav		xuio.uio_rw = UIO_READ;
611229997Sken	} else {
612229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
613267537Smav		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
627267877Smav	binuptime(&beio->ds_t0);
628267877Smav	mtx_lock(&be_lun->io_lock);
629267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
630267877Smav	mtx_unlock(&be_lun->io_lock);
631267877Smav
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		 */
655271309Smav		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
656229997Sken
657229997Sken		VOP_UNLOCK(be_lun->vn, 0);
658267537Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
659229997Sken	} else {
660229997Sken		struct mount *mountpoint;
661229997Sken		int lock_flags;
662229997Sken
663229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
664229997Sken
665229997Sken		if (MNT_SHARED_WRITES(mountpoint)
666229997Sken		 || ((mountpoint == NULL)
667229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
668229997Sken			lock_flags = LK_SHARED;
669229997Sken		else
670229997Sken			lock_flags = LK_EXCLUSIVE;
671229997Sken
672229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
673229997Sken
674229997Sken		/*
675229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
676229997Sken		 * is done asynchronously.  (Normally the write would just
677229997Sken		 * get put into cache.
678229997Sken		 *
679229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
680229997Sken		 * attempt to write the buffer out synchronously if that
681229997Sken		 * flag is set.
682229997Sken		 *
683229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
684229997Sken		 *
685229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
686229997Sken		 * for writes.  It will flush the transaction from the
687229997Sken		 * cache before returning.
688229997Sken		 *
689229997Sken		 * So if we've got the BIO_ORDERED flag set, we want
690229997Sken		 * IO_SYNC in either the UFS or ZFS case.
691229997Sken		 */
692271309Smav		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
693229997Sken		VOP_UNLOCK(be_lun->vn, 0);
694229997Sken
695229997Sken		vn_finished_write(mountpoint);
696267537Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
697229997Sken        }
698229997Sken
699267877Smav	mtx_lock(&be_lun->io_lock);
700267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
701267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
702267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
703267877Smav	mtx_unlock(&be_lun->io_lock);
704267877Smav
705229997Sken	/*
706229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
707229997Sken	 * return the I/O to the user.
708229997Sken	 */
709229997Sken	if (error != 0) {
710229997Sken		char path_str[32];
711229997Sken
712229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
713229997Sken		/*
714229997Sken		 * XXX KDM ZFS returns ENOSPC when the underlying
715229997Sken		 * filesystem fills up.  What kind of SCSI error should we
716229997Sken		 * return for that?
717229997Sken		 */
718229997Sken		printf("%s%s command returned errno %d\n", path_str,
719229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
720229997Sken		ctl_set_medium_error(&io->scsiio);
721229997Sken		ctl_complete_beio(beio);
722229997Sken		return;
723229997Sken	}
724229997Sken
725229997Sken	/*
726269122Smav	 * If this is a write or a verify, we're all done.
727229997Sken	 * If this is a read, we can now send the data to the user.
728229997Sken	 */
729269122Smav	if ((beio->bio_cmd == BIO_WRITE) ||
730269122Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
731229997Sken		ctl_set_success(&io->scsiio);
732229997Sken		ctl_complete_beio(beio);
733229997Sken	} else {
734229997Sken#ifdef CTL_TIME_IO
735229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
736229997Sken#endif
737229997Sken		ctl_datamove(io);
738229997Sken	}
739229997Sken}
740229997Sken
741229997Skenstatic void
742269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
743269123Smav			   struct ctl_be_block_io *beio)
744269123Smav{
745269123Smav	struct ctl_be_block_devdata *dev_data;
746269123Smav	union ctl_io *io;
747269123Smav	struct uio xuio;
748269123Smav	struct iovec *xiovec;
749269123Smav	int flags;
750269123Smav	int error, i;
751269123Smav
752269123Smav	DPRINTF("entered\n");
753269123Smav
754269123Smav	dev_data = &be_lun->backend.dev;
755269123Smav	io = beio->io;
756271309Smav	flags = 0;
757271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
758271309Smav		flags |= IO_DIRECT;
759271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
760271309Smav		flags |= IO_SYNC;
761269123Smav
762269123Smav	bzero(&xuio, sizeof(xuio));
763269123Smav	if (beio->bio_cmd == BIO_READ) {
764269123Smav		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
765269123Smav		xuio.uio_rw = UIO_READ;
766269123Smav	} else {
767269123Smav		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
768269123Smav		xuio.uio_rw = UIO_WRITE;
769269123Smav	}
770269123Smav	xuio.uio_offset = beio->io_offset;
771269123Smav	xuio.uio_resid = beio->io_len;
772269123Smav	xuio.uio_segflg = UIO_SYSSPACE;
773269123Smav	xuio.uio_iov = beio->xiovecs;
774269123Smav	xuio.uio_iovcnt = beio->num_segs;
775269123Smav	xuio.uio_td = curthread;
776269123Smav
777269123Smav	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
778269123Smav		xiovec->iov_base = beio->sg_segs[i].addr;
779269123Smav		xiovec->iov_len = beio->sg_segs[i].len;
780269123Smav	}
781269123Smav
782269123Smav	binuptime(&beio->ds_t0);
783269123Smav	mtx_lock(&be_lun->io_lock);
784269123Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
785269123Smav	mtx_unlock(&be_lun->io_lock);
786269123Smav
787269123Smav	if (beio->bio_cmd == BIO_READ) {
788271309Smav		error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
789269123Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
790269123Smav	} else {
791271309Smav		error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
792269123Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
793269123Smav	}
794269123Smav
795269123Smav	mtx_lock(&be_lun->io_lock);
796269123Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
797269123Smav	    beio->ds_tag_type, beio->ds_trans_type,
798269123Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
799269123Smav	mtx_unlock(&be_lun->io_lock);
800269123Smav
801269123Smav	/*
802269123Smav	 * If we got an error, set the sense data to "MEDIUM ERROR" and
803269123Smav	 * return the I/O to the user.
804269123Smav	 */
805269123Smav	if (error != 0) {
806269123Smav		ctl_set_medium_error(&io->scsiio);
807269123Smav		ctl_complete_beio(beio);
808269123Smav		return;
809269123Smav	}
810269123Smav
811269123Smav	/*
812269123Smav	 * If this is a write or a verify, we're all done.
813269123Smav	 * If this is a read, we can now send the data to the user.
814269123Smav	 */
815269123Smav	if ((beio->bio_cmd == BIO_WRITE) ||
816269123Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
817269123Smav		ctl_set_success(&io->scsiio);
818269123Smav		ctl_complete_beio(beio);
819269123Smav	} else {
820269123Smav#ifdef CTL_TIME_IO
821269123Smav        	getbintime(&io->io_hdr.dma_start_bt);
822269123Smav#endif
823269123Smav		ctl_datamove(io);
824269123Smav	}
825269123Smav}
826269123Smav
827269123Smavstatic void
828229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
829229997Sken		       struct ctl_be_block_io *beio)
830229997Sken{
831229997Sken	struct bio *bio;
832229997Sken	union ctl_io *io;
833229997Sken	struct ctl_be_block_devdata *dev_data;
834229997Sken
835229997Sken	dev_data = &be_lun->backend.dev;
836229997Sken	io = beio->io;
837229997Sken
838229997Sken	DPRINTF("entered\n");
839229997Sken
840229997Sken	/* This can't fail, it's a blocking allocation. */
841229997Sken	bio = g_alloc_bio();
842229997Sken
843229997Sken	bio->bio_cmd	    = BIO_FLUSH;
844229997Sken	bio->bio_flags	   |= BIO_ORDERED;
845229997Sken	bio->bio_dev	    = dev_data->cdev;
846229997Sken	bio->bio_offset	    = 0;
847229997Sken	bio->bio_data	    = 0;
848229997Sken	bio->bio_done	    = ctl_be_block_biodone;
849229997Sken	bio->bio_caller1    = beio;
850229997Sken	bio->bio_pblkno	    = 0;
851229997Sken
852229997Sken	/*
853229997Sken	 * We don't need to acquire the LUN lock here, because we are only
854229997Sken	 * sending one bio, and so there is no other context to synchronize
855229997Sken	 * with.
856229997Sken	 */
857229997Sken	beio->num_bios_sent = 1;
858229997Sken	beio->send_complete = 1;
859229997Sken
860229997Sken	binuptime(&beio->ds_t0);
861267877Smav	mtx_lock(&be_lun->io_lock);
862229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
863267877Smav	mtx_unlock(&be_lun->io_lock);
864229997Sken
865229997Sken	(*dev_data->csw->d_strategy)(bio);
866229997Sken}
867229997Sken
868229997Skenstatic void
869264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
870264274Smav		       struct ctl_be_block_io *beio,
871264274Smav		       uint64_t off, uint64_t len, int last)
872264274Smav{
873264274Smav	struct bio *bio;
874264274Smav	struct ctl_be_block_devdata *dev_data;
875264296Smav	uint64_t maxlen;
876264274Smav
877264274Smav	dev_data = &be_lun->backend.dev;
878264296Smav	maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
879264274Smav	while (len > 0) {
880264274Smav		bio = g_alloc_bio();
881264274Smav		bio->bio_cmd	    = BIO_DELETE;
882264274Smav		bio->bio_dev	    = dev_data->cdev;
883264274Smav		bio->bio_offset	    = off;
884264296Smav		bio->bio_length	    = MIN(len, maxlen);
885264274Smav		bio->bio_data	    = 0;
886264274Smav		bio->bio_done	    = ctl_be_block_biodone;
887264274Smav		bio->bio_caller1    = beio;
888264296Smav		bio->bio_pblkno     = off / be_lun->blocksize;
889264274Smav
890264274Smav		off += bio->bio_length;
891264274Smav		len -= bio->bio_length;
892264274Smav
893267877Smav		mtx_lock(&be_lun->io_lock);
894264274Smav		beio->num_bios_sent++;
895264274Smav		if (last && len == 0)
896264274Smav			beio->send_complete = 1;
897267877Smav		mtx_unlock(&be_lun->io_lock);
898264274Smav
899264274Smav		(*dev_data->csw->d_strategy)(bio);
900264274Smav	}
901264274Smav}
902264274Smav
903264274Smavstatic void
904264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
905264274Smav		       struct ctl_be_block_io *beio)
906264274Smav{
907264274Smav	union ctl_io *io;
908264274Smav	struct ctl_be_block_devdata *dev_data;
909267515Smav	struct ctl_ptr_len_flags *ptrlen;
910264274Smav	struct scsi_unmap_desc *buf, *end;
911264274Smav	uint64_t len;
912264274Smav
913264274Smav	dev_data = &be_lun->backend.dev;
914264274Smav	io = beio->io;
915264274Smav
916264274Smav	DPRINTF("entered\n");
917264274Smav
918264274Smav	binuptime(&beio->ds_t0);
919267877Smav	mtx_lock(&be_lun->io_lock);
920264274Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
921267877Smav	mtx_unlock(&be_lun->io_lock);
922264274Smav
923264274Smav	if (beio->io_offset == -1) {
924264274Smav		beio->io_len = 0;
925267515Smav		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
926267515Smav		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
927267515Smav		end = buf + ptrlen->len / sizeof(*buf);
928264274Smav		for (; buf < end; buf++) {
929264274Smav			len = (uint64_t)scsi_4btoul(buf->length) *
930264274Smav			    be_lun->blocksize;
931264274Smav			beio->io_len += len;
932264274Smav			ctl_be_block_unmap_dev_range(be_lun, beio,
933264274Smav			    scsi_8btou64(buf->lba) * be_lun->blocksize, len,
934264283Smav			    (end - buf < 2) ? TRUE : FALSE);
935264274Smav		}
936264274Smav	} else
937264274Smav		ctl_be_block_unmap_dev_range(be_lun, beio,
938264274Smav		    beio->io_offset, beio->io_len, TRUE);
939264274Smav}
940264274Smav
941264274Smavstatic void
942229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
943229997Sken			  struct ctl_be_block_io *beio)
944229997Sken{
945267877Smav	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
946229997Sken	int i;
947229997Sken	struct bio *bio;
948229997Sken	struct ctl_be_block_devdata *dev_data;
949229997Sken	off_t cur_offset;
950229997Sken	int max_iosize;
951229997Sken
952229997Sken	DPRINTF("entered\n");
953229997Sken
954229997Sken	dev_data = &be_lun->backend.dev;
955229997Sken
956229997Sken	/*
957229997Sken	 * We have to limit our I/O size to the maximum supported by the
958229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
959229997Sken	 * set it properly, use DFLTPHYS.
960229997Sken	 */
961229997Sken	max_iosize = dev_data->cdev->si_iosize_max;
962229997Sken	if (max_iosize < PAGE_SIZE)
963229997Sken		max_iosize = DFLTPHYS;
964229997Sken
965229997Sken	cur_offset = beio->io_offset;
966229997Sken	for (i = 0; i < beio->num_segs; i++) {
967229997Sken		size_t cur_size;
968229997Sken		uint8_t *cur_ptr;
969229997Sken
970229997Sken		cur_size = beio->sg_segs[i].len;
971229997Sken		cur_ptr = beio->sg_segs[i].addr;
972229997Sken
973229997Sken		while (cur_size > 0) {
974229997Sken			/* This can't fail, it's a blocking allocation. */
975229997Sken			bio = g_alloc_bio();
976229997Sken
977229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
978229997Sken
979229997Sken			bio->bio_cmd = beio->bio_cmd;
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
992267877Smav			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
993229997Sken			beio->num_bios_sent++;
994229997Sken		}
995229997Sken	}
996267877Smav	binuptime(&beio->ds_t0);
997267877Smav	mtx_lock(&be_lun->io_lock);
998267877Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
999267877Smav	beio->send_complete = 1;
1000267877Smav	mtx_unlock(&be_lun->io_lock);
1001267877Smav
1002267877Smav	/*
1003267877Smav	 * Fire off all allocated requests!
1004267877Smav	 */
1005267877Smav	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1006267877Smav		TAILQ_REMOVE(&queue, bio, bio_queue);
1007267877Smav		(*dev_data->csw->d_strategy)(bio);
1008267877Smav	}
1009229997Sken}
1010229997Sken
1011229997Skenstatic void
1012264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1013264274Smav{
1014264274Smav	union ctl_io *io;
1015264274Smav
1016264274Smav	io = beio->io;
1017264274Smav	ctl_free_beio(beio);
1018267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1019267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1020267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1021264274Smav		ctl_config_write_done(io);
1022264274Smav		return;
1023264274Smav	}
1024264274Smav
1025264274Smav	ctl_be_block_config_write(io);
1026264274Smav}
1027264274Smav
1028264274Smavstatic void
1029264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1030264274Smav			    union ctl_io *io)
1031264274Smav{
1032264274Smav	struct ctl_be_block_io *beio;
1033264274Smav	struct ctl_be_block_softc *softc;
1034267515Smav	struct ctl_lba_len_flags *lbalen;
1035264274Smav	uint64_t len_left, lba;
1036264274Smav	int i, seglen;
1037264274Smav	uint8_t *buf, *end;
1038264274Smav
1039264274Smav	DPRINTF("entered\n");
1040264274Smav
1041267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1042264274Smav	softc = be_lun->softc;
1043267537Smav	lbalen = ARGS(beio->io);
1044264274Smav
1045269622Smav	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR) ||
1046269622Smav	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1047264274Smav		ctl_free_beio(beio);
1048264274Smav		ctl_set_invalid_field(&io->scsiio,
1049264274Smav				      /*sks_valid*/ 1,
1050264274Smav				      /*command*/ 1,
1051264274Smav				      /*field*/ 1,
1052264274Smav				      /*bit_valid*/ 0,
1053264274Smav				      /*bit*/ 0);
1054264274Smav		ctl_config_write_done(io);
1055264274Smav		return;
1056264274Smav	}
1057264274Smav
1058264274Smav	switch (io->scsiio.tag_type) {
1059264274Smav	case CTL_TAG_ORDERED:
1060264274Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1061264274Smav		break;
1062264274Smav	case CTL_TAG_HEAD_OF_QUEUE:
1063264274Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1064264274Smav		break;
1065264274Smav	case CTL_TAG_UNTAGGED:
1066264274Smav	case CTL_TAG_SIMPLE:
1067264274Smav	case CTL_TAG_ACA:
1068264274Smav	default:
1069264274Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1070264274Smav		break;
1071264274Smav	}
1072264274Smav
1073269622Smav	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1074267515Smav		beio->io_offset = lbalen->lba * be_lun->blocksize;
1075267515Smav		beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1076264274Smav		beio->bio_cmd = BIO_DELETE;
1077264274Smav		beio->ds_trans_type = DEVSTAT_FREE;
1078264274Smav
1079264274Smav		be_lun->unmap(be_lun, beio);
1080264274Smav		return;
1081264274Smav	}
1082264274Smav
1083264274Smav	beio->bio_cmd = BIO_WRITE;
1084264274Smav	beio->ds_trans_type = DEVSTAT_WRITE;
1085264274Smav
1086264274Smav	DPRINTF("WRITE SAME at LBA %jx len %u\n",
1087267515Smav	       (uintmax_t)lbalen->lba, lbalen->len);
1088264274Smav
1089267515Smav	len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1090264274Smav	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1091264274Smav
1092264274Smav		/*
1093264274Smav		 * Setup the S/G entry for this chunk.
1094264274Smav		 */
1095264886Smav		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1096264274Smav		seglen -= seglen % be_lun->blocksize;
1097264274Smav		beio->sg_segs[i].len = seglen;
1098264274Smav		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1099264274Smav
1100264274Smav		DPRINTF("segment %d addr %p len %zd\n", i,
1101264274Smav			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1102264274Smav
1103264274Smav		beio->num_segs++;
1104264274Smav		len_left -= seglen;
1105264274Smav
1106264274Smav		buf = beio->sg_segs[i].addr;
1107264274Smav		end = buf + seglen;
1108264274Smav		for (; buf < end; buf += be_lun->blocksize) {
1109264274Smav			memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1110267515Smav			if (lbalen->flags & SWS_LBDATA)
1111267515Smav				scsi_ulto4b(lbalen->lba + lba, buf);
1112264274Smav			lba++;
1113264274Smav		}
1114264274Smav	}
1115264274Smav
1116267515Smav	beio->io_offset = lbalen->lba * be_lun->blocksize;
1117264274Smav	beio->io_len = lba * be_lun->blocksize;
1118264274Smav
1119264274Smav	/* We can not do all in one run. Correct and schedule rerun. */
1120264274Smav	if (len_left > 0) {
1121267515Smav		lbalen->lba += lba;
1122267515Smav		lbalen->len -= lba;
1123264274Smav		beio->beio_cont = ctl_be_block_cw_done_ws;
1124264274Smav	}
1125264274Smav
1126264274Smav	be_lun->dispatch(be_lun, beio);
1127264274Smav}
1128264274Smav
1129264274Smavstatic void
1130264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1131264274Smav			    union ctl_io *io)
1132264274Smav{
1133264274Smav	struct ctl_be_block_io *beio;
1134264274Smav	struct ctl_be_block_softc *softc;
1135267515Smav	struct ctl_ptr_len_flags *ptrlen;
1136264274Smav
1137264274Smav	DPRINTF("entered\n");
1138264274Smav
1139267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1140264274Smav	softc = be_lun->softc;
1141267515Smav	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1142264274Smav
1143269622Smav	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1144264274Smav		ctl_free_beio(beio);
1145264274Smav		ctl_set_invalid_field(&io->scsiio,
1146264274Smav				      /*sks_valid*/ 0,
1147264274Smav				      /*command*/ 1,
1148264274Smav				      /*field*/ 0,
1149264274Smav				      /*bit_valid*/ 0,
1150264274Smav				      /*bit*/ 0);
1151264274Smav		ctl_config_write_done(io);
1152264274Smav		return;
1153264274Smav	}
1154264274Smav
1155264274Smav	switch (io->scsiio.tag_type) {
1156264274Smav	case CTL_TAG_ORDERED:
1157264274Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1158264274Smav		break;
1159264274Smav	case CTL_TAG_HEAD_OF_QUEUE:
1160264274Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1161264274Smav		break;
1162264274Smav	case CTL_TAG_UNTAGGED:
1163264274Smav	case CTL_TAG_SIMPLE:
1164264274Smav	case CTL_TAG_ACA:
1165264274Smav	default:
1166264274Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1167264274Smav		break;
1168264274Smav	}
1169264274Smav
1170264274Smav	beio->io_len = 0;
1171264274Smav	beio->io_offset = -1;
1172264274Smav
1173264274Smav	beio->bio_cmd = BIO_DELETE;
1174264274Smav	beio->ds_trans_type = DEVSTAT_FREE;
1175264274Smav
1176267515Smav	DPRINTF("UNMAP\n");
1177264274Smav
1178264274Smav	be_lun->unmap(be_lun, beio);
1179264274Smav}
1180264274Smav
1181264274Smavstatic void
1182264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio)
1183264274Smav{
1184264274Smav	union ctl_io *io;
1185264274Smav
1186264274Smav	io = beio->io;
1187264274Smav	ctl_free_beio(beio);
1188264274Smav	ctl_config_write_done(io);
1189264274Smav}
1190264274Smav
1191264274Smavstatic void
1192229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1193229997Sken			 union ctl_io *io)
1194229997Sken{
1195229997Sken	struct ctl_be_block_io *beio;
1196229997Sken	struct ctl_be_block_softc *softc;
1197229997Sken
1198229997Sken	DPRINTF("entered\n");
1199229997Sken
1200229997Sken	softc = be_lun->softc;
1201229997Sken	beio = ctl_alloc_beio(softc);
1202229997Sken	beio->io = io;
1203229997Sken	beio->lun = be_lun;
1204264274Smav	beio->beio_cont = ctl_be_block_cw_done;
1205267519Smav	PRIV(io)->ptr = (void *)beio;
1206229997Sken
1207229997Sken	switch (io->scsiio.cdb[0]) {
1208229997Sken	case SYNCHRONIZE_CACHE:
1209229997Sken	case SYNCHRONIZE_CACHE_16:
1210249194Strasz		beio->bio_cmd = BIO_FLUSH;
1211229997Sken		beio->ds_trans_type = DEVSTAT_NO_DATA;
1212229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1213229997Sken		beio->io_len = 0;
1214229997Sken		be_lun->lun_flush(be_lun, beio);
1215229997Sken		break;
1216264274Smav	case WRITE_SAME_10:
1217264274Smav	case WRITE_SAME_16:
1218264274Smav		ctl_be_block_cw_dispatch_ws(be_lun, io);
1219264274Smav		break;
1220264274Smav	case UNMAP:
1221264274Smav		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1222264274Smav		break;
1223229997Sken	default:
1224229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1225229997Sken		break;
1226229997Sken	}
1227229997Sken}
1228229997Sken
1229258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1230258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1231258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1232258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1233229997Sken
1234229997Skenstatic void
1235264886Smavctl_be_block_next(struct ctl_be_block_io *beio)
1236264886Smav{
1237264886Smav	struct ctl_be_block_lun *be_lun;
1238264886Smav	union ctl_io *io;
1239264886Smav
1240264886Smav	io = beio->io;
1241264886Smav	be_lun = beio->lun;
1242264886Smav	ctl_free_beio(beio);
1243267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1244267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1245267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1246267537Smav		ctl_data_submit_done(io);
1247264886Smav		return;
1248264886Smav	}
1249264886Smav
1250264886Smav	io->io_hdr.status &= ~CTL_STATUS_MASK;
1251264886Smav	io->io_hdr.status |= CTL_STATUS_NONE;
1252264886Smav
1253267877Smav	mtx_lock(&be_lun->queue_lock);
1254264886Smav	/*
1255264886Smav	 * XXX KDM make sure that links is okay to use at this point.
1256264886Smav	 * Otherwise, we either need to add another field to ctl_io_hdr,
1257264886Smav	 * or deal with resource allocation here.
1258264886Smav	 */
1259264886Smav	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1260267877Smav	mtx_unlock(&be_lun->queue_lock);
1261264886Smav
1262264886Smav	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1263264886Smav}
1264264886Smav
1265264886Smavstatic void
1266229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1267229997Sken			   union ctl_io *io)
1268229997Sken{
1269229997Sken	struct ctl_be_block_io *beio;
1270229997Sken	struct ctl_be_block_softc *softc;
1271267537Smav	struct ctl_lba_len_flags *lbalen;
1272267519Smav	struct ctl_ptr_len_flags *bptrlen;
1273267519Smav	uint64_t len_left, lbas;
1274229997Sken	int i;
1275229997Sken
1276229997Sken	softc = be_lun->softc;
1277229997Sken
1278229997Sken	DPRINTF("entered\n");
1279229997Sken
1280267537Smav	lbalen = ARGS(io);
1281267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1282267537Smav		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1283267537Smav	} else {
1284229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1285229997Sken	}
1286229997Sken
1287229997Sken	beio = ctl_alloc_beio(softc);
1288229997Sken	beio->io = io;
1289229997Sken	beio->lun = be_lun;
1290267519Smav	bptrlen = PRIV(io);
1291267519Smav	bptrlen->ptr = (void *)beio;
1292229997Sken
1293229997Sken	switch (io->scsiio.tag_type) {
1294229997Sken	case CTL_TAG_ORDERED:
1295229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1296229997Sken		break;
1297229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1298229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1299229997Sken		break;
1300229997Sken	case CTL_TAG_UNTAGGED:
1301229997Sken	case CTL_TAG_SIMPLE:
1302229997Sken	case CTL_TAG_ACA:
1303229997Sken	default:
1304229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1305229997Sken		break;
1306229997Sken	}
1307229997Sken
1308267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1309267537Smav		beio->bio_cmd = BIO_WRITE;
1310267537Smav		beio->ds_trans_type = DEVSTAT_WRITE;
1311267537Smav	} else {
1312229997Sken		beio->bio_cmd = BIO_READ;
1313229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1314229997Sken	}
1315229997Sken
1316264886Smav	DPRINTF("%s at LBA %jx len %u @%ju\n",
1317229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1318267519Smav	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1319267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1320267537Smav		lbas = CTLBLK_HALF_IO_SIZE;
1321267537Smav	else
1322267537Smav		lbas = CTLBLK_MAX_IO_SIZE;
1323267537Smav	lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1324267519Smav	beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1325267519Smav	beio->io_len = lbas * be_lun->blocksize;
1326267519Smav	bptrlen->len += lbas;
1327229997Sken
1328264886Smav	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1329264886Smav		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1330264886Smav		    i, CTLBLK_MAX_SEGS));
1331229997Sken
1332229997Sken		/*
1333229997Sken		 * Setup the S/G entry for this chunk.
1334229997Sken		 */
1335264886Smav		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1336229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1337229997Sken
1338229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1339229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1340229997Sken
1341267537Smav		/* Set up second segment for compare operation. */
1342267537Smav		if (lbalen->flags & CTL_LLF_COMPARE) {
1343267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1344267537Smav			    beio->sg_segs[i].len;
1345267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1346267537Smav			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
1347267537Smav		}
1348267537Smav
1349229997Sken		beio->num_segs++;
1350229997Sken		len_left -= beio->sg_segs[i].len;
1351229997Sken	}
1352267519Smav	if (bptrlen->len < lbalen->len)
1353264886Smav		beio->beio_cont = ctl_be_block_next;
1354264886Smav	io->scsiio.be_move_done = ctl_be_block_move_done;
1355267537Smav	/* For compare we have separate S/G lists for read and datamove. */
1356267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1357267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1358267537Smav	else
1359267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1360264886Smav	io->scsiio.kern_data_len = beio->io_len;
1361264886Smav	io->scsiio.kern_data_resid = 0;
1362264886Smav	io->scsiio.kern_sg_entries = beio->num_segs;
1363264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1364229997Sken
1365229997Sken	/*
1366229997Sken	 * For the read case, we need to read the data into our buffers and
1367229997Sken	 * then we can send it back to the user.  For the write case, we
1368229997Sken	 * need to get the data from the user first.
1369229997Sken	 */
1370229997Sken	if (beio->bio_cmd == BIO_READ) {
1371229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1372229997Sken		be_lun->dispatch(be_lun, beio);
1373229997Sken	} else {
1374229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1375229997Sken#ifdef CTL_TIME_IO
1376229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1377229997Sken#endif
1378229997Sken		ctl_datamove(io);
1379229997Sken	}
1380229997Sken}
1381229997Sken
1382229997Skenstatic void
1383229997Skenctl_be_block_worker(void *context, int pending)
1384229997Sken{
1385229997Sken	struct ctl_be_block_lun *be_lun;
1386229997Sken	struct ctl_be_block_softc *softc;
1387229997Sken	union ctl_io *io;
1388229997Sken
1389229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1390229997Sken	softc = be_lun->softc;
1391229997Sken
1392229997Sken	DPRINTF("entered\n");
1393229997Sken
1394267877Smav	mtx_lock(&be_lun->queue_lock);
1395229997Sken	for (;;) {
1396229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1397229997Sken		if (io != NULL) {
1398229997Sken			struct ctl_be_block_io *beio;
1399229997Sken
1400229997Sken			DPRINTF("datamove queue\n");
1401229997Sken
1402229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1403229997Sken				      ctl_io_hdr, links);
1404229997Sken
1405267877Smav			mtx_unlock(&be_lun->queue_lock);
1406229997Sken
1407267519Smav			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1408229997Sken
1409229997Sken			be_lun->dispatch(be_lun, beio);
1410229997Sken
1411267877Smav			mtx_lock(&be_lun->queue_lock);
1412229997Sken			continue;
1413229997Sken		}
1414229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1415229997Sken		if (io != NULL) {
1416229997Sken
1417229997Sken			DPRINTF("config write queue\n");
1418229997Sken
1419229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1420229997Sken				      ctl_io_hdr, links);
1421229997Sken
1422267877Smav			mtx_unlock(&be_lun->queue_lock);
1423229997Sken
1424229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1425229997Sken
1426267877Smav			mtx_lock(&be_lun->queue_lock);
1427229997Sken			continue;
1428229997Sken		}
1429229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1430229997Sken		if (io != NULL) {
1431229997Sken			DPRINTF("input queue\n");
1432229997Sken
1433229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1434229997Sken				      ctl_io_hdr, links);
1435267877Smav			mtx_unlock(&be_lun->queue_lock);
1436229997Sken
1437229997Sken			/*
1438229997Sken			 * We must drop the lock, since this routine and
1439229997Sken			 * its children may sleep.
1440229997Sken			 */
1441229997Sken			ctl_be_block_dispatch(be_lun, io);
1442229997Sken
1443267877Smav			mtx_lock(&be_lun->queue_lock);
1444229997Sken			continue;
1445229997Sken		}
1446229997Sken
1447229997Sken		/*
1448229997Sken		 * If we get here, there is no work left in the queues, so
1449229997Sken		 * just break out and let the task queue go to sleep.
1450229997Sken		 */
1451229997Sken		break;
1452229997Sken	}
1453267877Smav	mtx_unlock(&be_lun->queue_lock);
1454229997Sken}
1455229997Sken
1456229997Sken/*
1457229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1458229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1459229997Sken * thread.
1460229997Sken */
1461229997Skenstatic int
1462229997Skenctl_be_block_submit(union ctl_io *io)
1463229997Sken{
1464229997Sken	struct ctl_be_block_lun *be_lun;
1465229997Sken	struct ctl_be_lun *ctl_be_lun;
1466229997Sken
1467229997Sken	DPRINTF("entered\n");
1468229997Sken
1469229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1470229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1471229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1472229997Sken
1473229997Sken	/*
1474229997Sken	 * Make sure we only get SCSI I/O.
1475229997Sken	 */
1476229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1477229997Sken		"%#x) encountered", io->io_hdr.io_type));
1478229997Sken
1479267519Smav	PRIV(io)->len = 0;
1480267519Smav
1481267877Smav	mtx_lock(&be_lun->queue_lock);
1482229997Sken	/*
1483229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1484229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1485229997Sken	 * or deal with resource allocation here.
1486229997Sken	 */
1487229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1488267877Smav	mtx_unlock(&be_lun->queue_lock);
1489229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1490229997Sken
1491267514Smav	return (CTL_RETVAL_COMPLETE);
1492229997Sken}
1493229997Sken
1494229997Skenstatic int
1495229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1496229997Sken			int flag, struct thread *td)
1497229997Sken{
1498229997Sken	struct ctl_be_block_softc *softc;
1499229997Sken	int error;
1500229997Sken
1501229997Sken	softc = &backend_block_softc;
1502229997Sken
1503229997Sken	error = 0;
1504229997Sken
1505229997Sken	switch (cmd) {
1506229997Sken	case CTL_LUN_REQ: {
1507229997Sken		struct ctl_lun_req *lun_req;
1508229997Sken
1509229997Sken		lun_req = (struct ctl_lun_req *)addr;
1510229997Sken
1511229997Sken		switch (lun_req->reqtype) {
1512229997Sken		case CTL_LUNREQ_CREATE:
1513229997Sken			error = ctl_be_block_create(softc, lun_req);
1514229997Sken			break;
1515229997Sken		case CTL_LUNREQ_RM:
1516229997Sken			error = ctl_be_block_rm(softc, lun_req);
1517229997Sken			break;
1518232604Strasz		case CTL_LUNREQ_MODIFY:
1519232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1520232604Strasz			break;
1521229997Sken		default:
1522229997Sken			lun_req->status = CTL_LUN_ERROR;
1523229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1524229997Sken				 "%s: invalid LUN request type %d", __func__,
1525229997Sken				 lun_req->reqtype);
1526229997Sken			break;
1527229997Sken		}
1528229997Sken		break;
1529229997Sken	}
1530229997Sken	default:
1531229997Sken		error = ENOTTY;
1532229997Sken		break;
1533229997Sken	}
1534229997Sken
1535229997Sken	return (error);
1536229997Sken}
1537229997Sken
1538229997Skenstatic int
1539229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1540229997Sken{
1541229997Sken	struct ctl_be_block_filedata *file_data;
1542229997Sken	struct ctl_lun_create_params *params;
1543229997Sken	struct vattr		      vattr;
1544229997Sken	int			      error;
1545229997Sken
1546229997Sken	error = 0;
1547229997Sken	file_data = &be_lun->backend.file;
1548229997Sken	params = &req->reqdata.create;
1549229997Sken
1550229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1551229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1552229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1553229997Sken
1554229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1555229997Sken	if (error != 0) {
1556229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1557229997Sken			 "error calling VOP_GETATTR() for file %s",
1558229997Sken			 be_lun->dev_path);
1559229997Sken		return (error);
1560229997Sken	}
1561229997Sken
1562229997Sken	/*
1563229997Sken	 * Verify that we have the ability to upgrade to exclusive
1564229997Sken	 * access on this file so we can trap errors at open instead
1565229997Sken	 * of reporting them during first access.
1566229997Sken	 */
1567229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1568229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1569229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1570229997Sken			error = EBADF;
1571229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1572229997Sken				 "error locking file %s", be_lun->dev_path);
1573229997Sken			return (error);
1574229997Sken		}
1575229997Sken	}
1576229997Sken
1577229997Sken
1578229997Sken	file_data->cred = crhold(curthread->td_ucred);
1579232604Strasz	if (params->lun_size_bytes != 0)
1580232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1581232604Strasz	else
1582232604Strasz		be_lun->size_bytes = vattr.va_size;
1583229997Sken	/*
1584229997Sken	 * We set the multi thread flag for file operations because all
1585229997Sken	 * filesystems (in theory) are capable of allowing multiple readers
1586229997Sken	 * of a file at once.  So we want to get the maximum possible
1587229997Sken	 * concurrency.
1588229997Sken	 */
1589229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1590229997Sken
1591229997Sken	/*
1592229997Sken	 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
1593229997Sken	 * With ZFS, it is 131072 bytes.  Block sizes that large don't work
1594229997Sken	 * with disklabel and UFS on FreeBSD at least.  Large block sizes
1595229997Sken	 * may not work with other OSes as well.  So just export a sector
1596229997Sken	 * size of 512 bytes, which should work with any OS or
1597229997Sken	 * application.  Since our backing is a file, any block size will
1598229997Sken	 * work fine for the backing store.
1599229997Sken	 */
1600229997Sken#if 0
1601229997Sken	be_lun->blocksize= vattr.va_blocksize;
1602229997Sken#endif
1603229997Sken	if (params->blocksize_bytes != 0)
1604229997Sken		be_lun->blocksize = params->blocksize_bytes;
1605229997Sken	else
1606229997Sken		be_lun->blocksize = 512;
1607229997Sken
1608229997Sken	/*
1609229997Sken	 * Sanity check.  The media size has to be at least one
1610229997Sken	 * sector long.
1611229997Sken	 */
1612229997Sken	if (be_lun->size_bytes < be_lun->blocksize) {
1613229997Sken		error = EINVAL;
1614229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1615229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1616229997Sken			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1617229997Sken	}
1618229997Sken	return (error);
1619229997Sken}
1620229997Sken
1621229997Skenstatic int
1622229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1623229997Sken{
1624229997Sken	struct ctl_lun_create_params *params;
1625229997Sken	struct vattr		      vattr;
1626229997Sken	struct cdev		     *dev;
1627229997Sken	struct cdevsw		     *devsw;
1628229997Sken	int			      error;
1629264191Smav	off_t			      ps, pss, po, pos;
1630229997Sken
1631229997Sken	params = &req->reqdata.create;
1632229997Sken
1633229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1634229997Sken	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1635229997Sken	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1636229997Sken					     &be_lun->backend.dev.dev_ref);
1637229997Sken	if (be_lun->backend.dev.csw == NULL)
1638229997Sken		panic("Unable to retrieve device switch");
1639269123Smav	if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0)
1640269123Smav		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1641269123Smav	else
1642269123Smav		be_lun->dispatch = ctl_be_block_dispatch_dev;
1643269123Smav	be_lun->lun_flush = ctl_be_block_flush_dev;
1644269123Smav	be_lun->unmap = ctl_be_block_unmap_dev;
1645229997Sken
1646229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1647229997Sken	if (error) {
1648229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1649229997Sken			 "%s: error getting vnode attributes for device %s",
1650229997Sken			 __func__, be_lun->dev_path);
1651229997Sken		return (error);
1652229997Sken	}
1653229997Sken
1654229997Sken	dev = be_lun->vn->v_rdev;
1655229997Sken	devsw = dev->si_devsw;
1656229997Sken	if (!devsw->d_ioctl) {
1657229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1658229997Sken			 "%s: no d_ioctl for device %s!", __func__,
1659229997Sken			 be_lun->dev_path);
1660229997Sken		return (ENODEV);
1661229997Sken	}
1662229997Sken
1663229997Sken	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1664229997Sken			       (caddr_t)&be_lun->blocksize, FREAD,
1665229997Sken			       curthread);
1666229997Sken	if (error) {
1667229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1668229997Sken			 "%s: error %d returned for DIOCGSECTORSIZE ioctl "
1669229997Sken			 "on %s!", __func__, error, be_lun->dev_path);
1670229997Sken		return (error);
1671229997Sken	}
1672229997Sken
1673229997Sken	/*
1674229997Sken	 * If the user has asked for a blocksize that is greater than the
1675229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1676229997Sken	 * the user is asking for is an even multiple of the underlying
1677229997Sken	 * device's blocksize.
1678229997Sken	 */
1679229997Sken	if ((params->blocksize_bytes != 0)
1680229997Sken	 && (params->blocksize_bytes > be_lun->blocksize)) {
1681229997Sken		uint32_t bs_multiple, tmp_blocksize;
1682229997Sken
1683229997Sken		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1684229997Sken
1685229997Sken		tmp_blocksize = bs_multiple * be_lun->blocksize;
1686229997Sken
1687229997Sken		if (tmp_blocksize == params->blocksize_bytes) {
1688229997Sken			be_lun->blocksize = params->blocksize_bytes;
1689229997Sken		} else {
1690229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1691229997Sken				 "%s: requested blocksize %u is not an even "
1692229997Sken				 "multiple of backing device blocksize %u",
1693229997Sken				 __func__, params->blocksize_bytes,
1694229997Sken				 be_lun->blocksize);
1695229997Sken			return (EINVAL);
1696229997Sken
1697229997Sken		}
1698229997Sken	} else if ((params->blocksize_bytes != 0)
1699229997Sken		&& (params->blocksize_bytes != be_lun->blocksize)) {
1700229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1701229997Sken			 "%s: requested blocksize %u < backing device "
1702229997Sken			 "blocksize %u", __func__, params->blocksize_bytes,
1703229997Sken			 be_lun->blocksize);
1704229997Sken		return (EINVAL);
1705229997Sken	}
1706229997Sken
1707229997Sken	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1708229997Sken			       (caddr_t)&be_lun->size_bytes, FREAD,
1709229997Sken			       curthread);
1710229997Sken	if (error) {
1711229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1712232604Strasz			 "%s: error %d returned for DIOCGMEDIASIZE "
1713232604Strasz			 " ioctl on %s!", __func__, error,
1714232604Strasz			 be_lun->dev_path);
1715229997Sken		return (error);
1716229997Sken	}
1717229997Sken
1718232604Strasz	if (params->lun_size_bytes != 0) {
1719232604Strasz		if (params->lun_size_bytes > be_lun->size_bytes) {
1720232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1721232604Strasz				 "%s: requested LUN size %ju > backing device "
1722232604Strasz				 "size %ju", __func__,
1723232604Strasz				 (uintmax_t)params->lun_size_bytes,
1724232604Strasz				 (uintmax_t)be_lun->size_bytes);
1725232604Strasz			return (EINVAL);
1726232604Strasz		}
1727232604Strasz
1728232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1729232604Strasz	}
1730232604Strasz
1731264191Smav	error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1732264191Smav			       (caddr_t)&ps, FREAD, curthread);
1733264191Smav	if (error)
1734264191Smav		ps = po = 0;
1735264191Smav	else {
1736264191Smav		error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1737264191Smav				       (caddr_t)&po, FREAD, curthread);
1738264191Smav		if (error)
1739264191Smav			po = 0;
1740264191Smav	}
1741264191Smav	pss = ps / be_lun->blocksize;
1742264191Smav	pos = po / be_lun->blocksize;
1743264191Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1744264191Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1745264191Smav		be_lun->pblockexp = fls(pss) - 1;
1746264191Smav		be_lun->pblockoff = (pss - pos) % pss;
1747264191Smav	}
1748264191Smav
1749229997Sken	return (0);
1750229997Sken}
1751229997Sken
1752229997Skenstatic int
1753229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
1754229997Sken{
1755229997Sken	DROP_GIANT();
1756229997Sken	if (be_lun->vn) {
1757229997Sken		int flags = FREAD | FWRITE;
1758229997Sken
1759229997Sken		switch (be_lun->dev_type) {
1760229997Sken		case CTL_BE_BLOCK_DEV:
1761229997Sken			if (be_lun->backend.dev.csw) {
1762229997Sken				dev_relthread(be_lun->backend.dev.cdev,
1763229997Sken					      be_lun->backend.dev.dev_ref);
1764229997Sken				be_lun->backend.dev.csw  = NULL;
1765229997Sken				be_lun->backend.dev.cdev = NULL;
1766229997Sken			}
1767229997Sken			break;
1768229997Sken		case CTL_BE_BLOCK_FILE:
1769229997Sken			break;
1770229997Sken		case CTL_BE_BLOCK_NONE:
1771258871Strasz			break;
1772229997Sken		default:
1773229997Sken			panic("Unexpected backend type.");
1774229997Sken			break;
1775229997Sken		}
1776229997Sken
1777229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1778229997Sken		be_lun->vn = NULL;
1779229997Sken
1780229997Sken		switch (be_lun->dev_type) {
1781229997Sken		case CTL_BE_BLOCK_DEV:
1782229997Sken			break;
1783229997Sken		case CTL_BE_BLOCK_FILE:
1784229997Sken			if (be_lun->backend.file.cred != NULL) {
1785229997Sken				crfree(be_lun->backend.file.cred);
1786229997Sken				be_lun->backend.file.cred = NULL;
1787229997Sken			}
1788229997Sken			break;
1789229997Sken		case CTL_BE_BLOCK_NONE:
1790258871Strasz			break;
1791229997Sken		default:
1792229997Sken			panic("Unexpected backend type.");
1793229997Sken			break;
1794229997Sken		}
1795229997Sken	}
1796229997Sken	PICKUP_GIANT();
1797229997Sken
1798229997Sken	return (0);
1799229997Sken}
1800229997Sken
1801229997Skenstatic int
1802229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
1803229997Sken		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1804229997Sken{
1805229997Sken	struct nameidata nd;
1806229997Sken	int		 flags;
1807229997Sken	int		 error;
1808229997Sken
1809229997Sken	/*
1810229997Sken	 * XXX KDM allow a read-only option?
1811229997Sken	 */
1812229997Sken	flags = FREAD | FWRITE;
1813229997Sken	error = 0;
1814229997Sken
1815229997Sken	if (rootvnode == NULL) {
1816229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1817229997Sken			 "%s: Root filesystem is not mounted", __func__);
1818229997Sken		return (1);
1819229997Sken	}
1820229997Sken
1821229997Sken	if (!curthread->td_proc->p_fd->fd_cdir) {
1822229997Sken		curthread->td_proc->p_fd->fd_cdir = rootvnode;
1823229997Sken		VREF(rootvnode);
1824229997Sken	}
1825229997Sken	if (!curthread->td_proc->p_fd->fd_rdir) {
1826229997Sken		curthread->td_proc->p_fd->fd_rdir = rootvnode;
1827229997Sken		VREF(rootvnode);
1828229997Sken	}
1829229997Sken	if (!curthread->td_proc->p_fd->fd_jdir) {
1830229997Sken		curthread->td_proc->p_fd->fd_jdir = rootvnode;
1831229997Sken		VREF(rootvnode);
1832229997Sken	}
1833229997Sken
1834229997Sken again:
1835229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1836229997Sken	error = vn_open(&nd, &flags, 0, NULL);
1837229997Sken	if (error) {
1838229997Sken		/*
1839229997Sken		 * This is the only reasonable guess we can make as far as
1840229997Sken		 * path if the user doesn't give us a fully qualified path.
1841229997Sken		 * If they want to specify a file, they need to specify the
1842229997Sken		 * full path.
1843229997Sken		 */
1844229997Sken		if (be_lun->dev_path[0] != '/') {
1845229997Sken			char *dev_path = "/dev/";
1846229997Sken			char *dev_name;
1847229997Sken
1848229997Sken			/* Try adding device path at beginning of name */
1849229997Sken			dev_name = malloc(strlen(be_lun->dev_path)
1850229997Sken					+ strlen(dev_path) + 1,
1851229997Sken					  M_CTLBLK, M_WAITOK);
1852229997Sken			if (dev_name) {
1853229997Sken				sprintf(dev_name, "%s%s", dev_path,
1854229997Sken					be_lun->dev_path);
1855229997Sken				free(be_lun->dev_path, M_CTLBLK);
1856229997Sken				be_lun->dev_path = dev_name;
1857229997Sken				goto again;
1858229997Sken			}
1859229997Sken		}
1860229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1861229997Sken			 "%s: error opening %s", __func__, be_lun->dev_path);
1862229997Sken		return (error);
1863229997Sken	}
1864229997Sken
1865229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
1866229997Sken
1867229997Sken	be_lun->vn = nd.ni_vp;
1868229997Sken
1869229997Sken	/* We only support disks and files. */
1870229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
1871229997Sken		error = ctl_be_block_open_dev(be_lun, req);
1872229997Sken	} else if (be_lun->vn->v_type == VREG) {
1873229997Sken		error = ctl_be_block_open_file(be_lun, req);
1874229997Sken	} else {
1875229997Sken		error = EINVAL;
1876229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1877258871Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
1878229997Sken	}
1879229997Sken	VOP_UNLOCK(be_lun->vn, 0);
1880229997Sken
1881229997Sken	if (error != 0) {
1882229997Sken		ctl_be_block_close(be_lun);
1883229997Sken		return (error);
1884229997Sken	}
1885229997Sken
1886229997Sken	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1887229997Sken	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1888229997Sken
1889229997Sken	return (0);
1890229997Sken}
1891229997Sken
1892229997Skenstatic int
1893229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1894229997Sken{
1895229997Sken	struct ctl_be_block_lun *be_lun;
1896229997Sken	struct ctl_lun_create_params *params;
1897267481Smav	char num_thread_str[16];
1898229997Sken	char tmpstr[32];
1899267481Smav	char *value;
1900264274Smav	int retval, num_threads, unmap;
1901267481Smav	int tmp_num_threads;
1902229997Sken
1903229997Sken	params = &req->reqdata.create;
1904229997Sken	retval = 0;
1905229997Sken
1906229997Sken	num_threads = cbb_num_threads;
1907229997Sken
1908229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1909229997Sken
1910229997Sken	be_lun->softc = softc;
1911229997Sken	STAILQ_INIT(&be_lun->input_queue);
1912229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
1913229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
1914229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1915267877Smav	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
1916267877Smav	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
1917268280Smav	ctl_init_opts(&be_lun->ctl_be_lun.options,
1918268280Smav	    req->num_be_args, req->kern_be_args);
1919229997Sken
1920264886Smav	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1921256995Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1922229997Sken
1923229997Sken	if (be_lun->lun_zone == NULL) {
1924229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1925229997Sken			 "%s: error allocating UMA zone", __func__);
1926229997Sken		goto bailout_error;
1927229997Sken	}
1928229997Sken
1929229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1930229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
1931229997Sken	else
1932229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
1933229997Sken
1934229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1935268280Smav		value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
1936267499Smav		if (value == NULL) {
1937229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1938229997Sken				 "%s: no file argument specified", __func__);
1939229997Sken			goto bailout_error;
1940229997Sken		}
1941267499Smav		be_lun->dev_path = strdup(value, M_CTLBLK);
1942229997Sken
1943229997Sken		retval = ctl_be_block_open(softc, be_lun, req);
1944229997Sken		if (retval != 0) {
1945229997Sken			retval = 0;
1946229997Sken			goto bailout_error;
1947229997Sken		}
1948229997Sken
1949229997Sken		/*
1950229997Sken		 * Tell the user the size of the file/device.
1951229997Sken		 */
1952229997Sken		params->lun_size_bytes = be_lun->size_bytes;
1953229997Sken
1954229997Sken		/*
1955229997Sken		 * The maximum LBA is the size - 1.
1956229997Sken		 */
1957229997Sken		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
1958229997Sken	} else {
1959229997Sken		/*
1960229997Sken		 * For processor devices, we don't have any size.
1961229997Sken		 */
1962229997Sken		be_lun->blocksize = 0;
1963264191Smav		be_lun->pblockexp = 0;
1964264191Smav		be_lun->pblockoff = 0;
1965229997Sken		be_lun->size_blocks = 0;
1966229997Sken		be_lun->size_bytes = 0;
1967229997Sken		be_lun->ctl_be_lun.maxlba = 0;
1968229997Sken		params->lun_size_bytes = 0;
1969229997Sken
1970229997Sken		/*
1971229997Sken		 * Default to just 1 thread for processor devices.
1972229997Sken		 */
1973229997Sken		num_threads = 1;
1974229997Sken	}
1975229997Sken
1976229997Sken	/*
1977229997Sken	 * XXX This searching loop might be refactored to be combined with
1978229997Sken	 * the loop above,
1979229997Sken	 */
1980268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
1981267481Smav	if (value != NULL) {
1982267481Smav		tmp_num_threads = strtol(value, NULL, 0);
1983229997Sken
1984267481Smav		/*
1985267481Smav		 * We don't let the user specify less than one
1986267481Smav		 * thread, but hope he's clueful enough not to
1987267481Smav		 * specify 1000 threads.
1988267481Smav		 */
1989267481Smav		if (tmp_num_threads < 1) {
1990267481Smav			snprintf(req->error_str, sizeof(req->error_str),
1991267481Smav				 "%s: invalid number of threads %s",
1992267481Smav			         __func__, num_thread_str);
1993267481Smav			goto bailout_error;
1994229997Sken		}
1995267481Smav		num_threads = tmp_num_threads;
1996229997Sken	}
1997267481Smav	unmap = 0;
1998268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
1999267481Smav	if (value != NULL && strcmp(value, "on") == 0)
2000267481Smav		unmap = 1;
2001229997Sken
2002229997Sken	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2003229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2004264274Smav	if (unmap)
2005264274Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2006229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
2007229997Sken	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2008264191Smav	be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2009264191Smav	be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2010229997Sken	/* Tell the user the blocksize we ended up using */
2011229997Sken	params->blocksize_bytes = be_lun->blocksize;
2012229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2013229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2014229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2015229997Sken	} else
2016229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
2017229997Sken
2018229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2019229997Sken	be_lun->ctl_be_lun.lun_config_status =
2020229997Sken		ctl_be_block_lun_config_status;
2021229997Sken	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2022229997Sken
2023229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2024229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2025229997Sken			 softc->num_luns);
2026229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2027229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2028229997Sken			sizeof(tmpstr)));
2029229997Sken
2030229997Sken		/* Tell the user what we used for a serial number */
2031229997Sken		strncpy((char *)params->serial_num, tmpstr,
2032229997Sken			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2033229997Sken	} else {
2034229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
2035229997Sken			params->serial_num,
2036229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2037229997Sken			sizeof(params->serial_num)));
2038229997Sken	}
2039229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2040229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2041229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2042229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2043229997Sken			sizeof(tmpstr)));
2044229997Sken
2045229997Sken		/* Tell the user what we used for a device ID */
2046229997Sken		strncpy((char *)params->device_id, tmpstr,
2047229997Sken			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2048229997Sken	} else {
2049229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
2050229997Sken			params->device_id,
2051229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2052229997Sken				sizeof(params->device_id)));
2053229997Sken	}
2054229997Sken
2055229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2056229997Sken
2057229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2058229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2059229997Sken
2060229997Sken	if (be_lun->io_taskqueue == NULL) {
2061229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2062229997Sken			 "%s: Unable to create taskqueue", __func__);
2063229997Sken		goto bailout_error;
2064229997Sken	}
2065229997Sken
2066229997Sken	/*
2067229997Sken	 * Note that we start the same number of threads by default for
2068229997Sken	 * both the file case and the block device case.  For the file
2069229997Sken	 * case, we need multiple threads to allow concurrency, because the
2070229997Sken	 * vnode interface is designed to be a blocking interface.  For the
2071229997Sken	 * block device case, ZFS zvols at least will block the caller's
2072229997Sken	 * context in many instances, and so we need multiple threads to
2073229997Sken	 * overcome that problem.  Other block devices don't need as many
2074229997Sken	 * threads, but they shouldn't cause too many problems.
2075229997Sken	 *
2076229997Sken	 * If the user wants to just have a single thread for a block
2077229997Sken	 * device, he can specify that when the LUN is created, or change
2078229997Sken	 * the tunable/sysctl to alter the default number of threads.
2079229997Sken	 */
2080229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2081229997Sken					 /*num threads*/num_threads,
2082229997Sken					 /*priority*/PWAIT,
2083229997Sken					 /*thread name*/
2084229997Sken					 "%s taskq", be_lun->lunname);
2085229997Sken
2086229997Sken	if (retval != 0)
2087229997Sken		goto bailout_error;
2088229997Sken
2089229997Sken	be_lun->num_threads = num_threads;
2090229997Sken
2091229997Sken	mtx_lock(&softc->lock);
2092229997Sken	softc->num_luns++;
2093229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2094229997Sken
2095229997Sken	mtx_unlock(&softc->lock);
2096229997Sken
2097229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
2098229997Sken	if (retval != 0) {
2099229997Sken		mtx_lock(&softc->lock);
2100229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2101229997Sken			      links);
2102229997Sken		softc->num_luns--;
2103229997Sken		mtx_unlock(&softc->lock);
2104229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2105229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
2106229997Sken			"details", __func__, retval);
2107229997Sken		retval = 0;
2108229997Sken		goto bailout_error;
2109229997Sken	}
2110229997Sken
2111229997Sken	mtx_lock(&softc->lock);
2112229997Sken
2113229997Sken	/*
2114229997Sken	 * Tell the config_status routine that we're waiting so it won't
2115229997Sken	 * clean up the LUN in the event of an error.
2116229997Sken	 */
2117229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2118229997Sken
2119229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2120229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2121229997Sken		if (retval == EINTR)
2122229997Sken			break;
2123229997Sken	}
2124229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2125229997Sken
2126229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2127229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2128229997Sken			 "%s: LUN configuration error, see dmesg for details",
2129229997Sken			 __func__);
2130229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2131229997Sken			      links);
2132229997Sken		softc->num_luns--;
2133229997Sken		mtx_unlock(&softc->lock);
2134229997Sken		goto bailout_error;
2135229997Sken	} else {
2136229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2137229997Sken	}
2138229997Sken
2139229997Sken	mtx_unlock(&softc->lock);
2140229997Sken
2141229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2142229997Sken					       be_lun->blocksize,
2143229997Sken					       DEVSTAT_ALL_SUPPORTED,
2144229997Sken					       be_lun->ctl_be_lun.lun_type
2145229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
2146229997Sken					       DEVSTAT_PRIORITY_OTHER);
2147229997Sken
2148229997Sken
2149229997Sken	req->status = CTL_LUN_OK;
2150229997Sken
2151229997Sken	return (retval);
2152229997Sken
2153229997Skenbailout_error:
2154229997Sken	req->status = CTL_LUN_ERROR;
2155229997Sken
2156267429Smav	if (be_lun->io_taskqueue != NULL)
2157267429Smav		taskqueue_free(be_lun->io_taskqueue);
2158229997Sken	ctl_be_block_close(be_lun);
2159267429Smav	if (be_lun->dev_path != NULL)
2160267429Smav		free(be_lun->dev_path, M_CTLBLK);
2161267429Smav	if (be_lun->lun_zone != NULL)
2162267429Smav		uma_zdestroy(be_lun->lun_zone);
2163268280Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2164267877Smav	mtx_destroy(&be_lun->queue_lock);
2165267877Smav	mtx_destroy(&be_lun->io_lock);
2166229997Sken	free(be_lun, M_CTLBLK);
2167229997Sken
2168229997Sken	return (retval);
2169229997Sken}
2170229997Sken
2171229997Skenstatic int
2172229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2173229997Sken{
2174229997Sken	struct ctl_lun_rm_params *params;
2175229997Sken	struct ctl_be_block_lun *be_lun;
2176229997Sken	int retval;
2177229997Sken
2178229997Sken	params = &req->reqdata.rm;
2179229997Sken
2180229997Sken	mtx_lock(&softc->lock);
2181229997Sken
2182229997Sken	be_lun = NULL;
2183229997Sken
2184229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2185229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2186229997Sken			break;
2187229997Sken	}
2188229997Sken	mtx_unlock(&softc->lock);
2189229997Sken
2190229997Sken	if (be_lun == NULL) {
2191229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2192229997Sken			 "%s: LUN %u is not managed by the block backend",
2193229997Sken			 __func__, params->lun_id);
2194229997Sken		goto bailout_error;
2195229997Sken	}
2196229997Sken
2197229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2198229997Sken
2199229997Sken	if (retval != 0) {
2200229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2201229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
2202229997Sken			 "LUN %d", __func__, retval, params->lun_id);
2203229997Sken		goto bailout_error;
2204229997Sken
2205229997Sken	}
2206229997Sken
2207229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2208229997Sken	if (retval != 0) {
2209229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2210229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
2211229997Sken			 "LUN %d", __func__, retval, params->lun_id);
2212229997Sken		goto bailout_error;
2213229997Sken	}
2214229997Sken
2215229997Sken	mtx_lock(&softc->lock);
2216229997Sken
2217229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2218229997Sken
2219229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2220229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2221229997Sken                if (retval == EINTR)
2222229997Sken                        break;
2223229997Sken        }
2224229997Sken
2225229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2226229997Sken
2227229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2228229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2229229997Sken			 "%s: interrupted waiting for LUN to be freed",
2230229997Sken			 __func__);
2231229997Sken		mtx_unlock(&softc->lock);
2232229997Sken		goto bailout_error;
2233229997Sken	}
2234229997Sken
2235229997Sken	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2236229997Sken
2237229997Sken	softc->num_luns--;
2238229997Sken	mtx_unlock(&softc->lock);
2239229997Sken
2240229997Sken	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2241229997Sken
2242229997Sken	taskqueue_free(be_lun->io_taskqueue);
2243229997Sken
2244229997Sken	ctl_be_block_close(be_lun);
2245229997Sken
2246229997Sken	if (be_lun->disk_stats != NULL)
2247229997Sken		devstat_remove_entry(be_lun->disk_stats);
2248229997Sken
2249229997Sken	uma_zdestroy(be_lun->lun_zone);
2250229997Sken
2251268280Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2252229997Sken	free(be_lun->dev_path, M_CTLBLK);
2253267877Smav	mtx_destroy(&be_lun->queue_lock);
2254267877Smav	mtx_destroy(&be_lun->io_lock);
2255229997Sken	free(be_lun, M_CTLBLK);
2256229997Sken
2257229997Sken	req->status = CTL_LUN_OK;
2258229997Sken
2259229997Sken	return (0);
2260229997Sken
2261229997Skenbailout_error:
2262229997Sken
2263229997Sken	req->status = CTL_LUN_ERROR;
2264229997Sken
2265229997Sken	return (0);
2266229997Sken}
2267229997Sken
2268232604Straszstatic int
2269232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2270232604Strasz			 struct ctl_lun_req *req)
2271232604Strasz{
2272232604Strasz	struct vattr vattr;
2273232604Strasz	int error;
2274232604Strasz	struct ctl_lun_modify_params *params;
2275232604Strasz
2276232604Strasz	params = &req->reqdata.modify;
2277232604Strasz
2278232604Strasz	if (params->lun_size_bytes != 0) {
2279232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2280232604Strasz	} else  {
2281271794Smav		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2282232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2283271794Smav		VOP_UNLOCK(be_lun->vn, 0);
2284232604Strasz		if (error != 0) {
2285232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2286232604Strasz				 "error calling VOP_GETATTR() for file %s",
2287232604Strasz				 be_lun->dev_path);
2288232604Strasz			return (error);
2289232604Strasz		}
2290232604Strasz
2291232604Strasz		be_lun->size_bytes = vattr.va_size;
2292232604Strasz	}
2293232604Strasz
2294232604Strasz	return (0);
2295232604Strasz}
2296232604Strasz
2297232604Straszstatic int
2298232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2299232604Strasz			struct ctl_lun_req *req)
2300232604Strasz{
2301271794Smav	struct ctl_be_block_devdata *dev_data;
2302232604Strasz	int error;
2303232604Strasz	struct ctl_lun_modify_params *params;
2304232604Strasz	uint64_t size_bytes;
2305232604Strasz
2306232604Strasz	params = &req->reqdata.modify;
2307232604Strasz
2308271794Smav	dev_data = &be_lun->backend.dev;
2309271794Smav	if (!dev_data->csw->d_ioctl) {
2310232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2311232604Strasz			 "%s: no d_ioctl for device %s!", __func__,
2312232604Strasz			 be_lun->dev_path);
2313232604Strasz		return (ENODEV);
2314232604Strasz	}
2315232604Strasz
2316271794Smav	error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2317232604Strasz			       (caddr_t)&size_bytes, FREAD,
2318232604Strasz			       curthread);
2319232604Strasz	if (error) {
2320232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2321232604Strasz			 "%s: error %d returned for DIOCGMEDIASIZE ioctl "
2322232604Strasz			 "on %s!", __func__, error, be_lun->dev_path);
2323232604Strasz		return (error);
2324232604Strasz	}
2325232604Strasz
2326232604Strasz	if (params->lun_size_bytes != 0) {
2327232604Strasz		if (params->lun_size_bytes > size_bytes) {
2328232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2329232604Strasz				 "%s: requested LUN size %ju > backing device "
2330232604Strasz				 "size %ju", __func__,
2331232604Strasz				 (uintmax_t)params->lun_size_bytes,
2332232604Strasz				 (uintmax_t)size_bytes);
2333232604Strasz			return (EINVAL);
2334232604Strasz		}
2335232604Strasz
2336232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2337232604Strasz	} else {
2338232604Strasz		be_lun->size_bytes = size_bytes;
2339232604Strasz	}
2340232604Strasz
2341232604Strasz	return (0);
2342232604Strasz}
2343232604Strasz
2344232604Straszstatic int
2345232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2346232604Strasz{
2347232604Strasz	struct ctl_lun_modify_params *params;
2348232604Strasz	struct ctl_be_block_lun *be_lun;
2349271794Smav	uint64_t oldsize;
2350241896Skib	int error;
2351232604Strasz
2352232604Strasz	params = &req->reqdata.modify;
2353232604Strasz
2354232604Strasz	mtx_lock(&softc->lock);
2355232604Strasz
2356232604Strasz	be_lun = NULL;
2357232604Strasz
2358232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2359232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2360232604Strasz			break;
2361232604Strasz	}
2362232604Strasz	mtx_unlock(&softc->lock);
2363232604Strasz
2364232604Strasz	if (be_lun == NULL) {
2365232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2366232604Strasz			 "%s: LUN %u is not managed by the block backend",
2367232604Strasz			 __func__, params->lun_id);
2368232604Strasz		goto bailout_error;
2369232604Strasz	}
2370232604Strasz
2371232604Strasz	if (params->lun_size_bytes != 0) {
2372232604Strasz		if (params->lun_size_bytes < be_lun->blocksize) {
2373232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2374232604Strasz				"%s: LUN size %ju < blocksize %u", __func__,
2375232604Strasz				params->lun_size_bytes, be_lun->blocksize);
2376232604Strasz			goto bailout_error;
2377232604Strasz		}
2378232604Strasz	}
2379232604Strasz
2380271794Smav	oldsize = be_lun->size_bytes;
2381232604Strasz	if (be_lun->vn->v_type == VREG)
2382232604Strasz		error = ctl_be_block_modify_file(be_lun, req);
2383232604Strasz	else
2384232604Strasz		error = ctl_be_block_modify_dev(be_lun, req);
2385232604Strasz	if (error != 0)
2386232604Strasz		goto bailout_error;
2387232604Strasz
2388271794Smav	if (be_lun->size_bytes != oldsize) {
2389271794Smav		be_lun->size_blocks = be_lun->size_bytes >>
2390271794Smav		    be_lun->blocksize_shift;
2391232604Strasz
2392271794Smav		/*
2393271794Smav		 * The maximum LBA is the size - 1.
2394271794Smav		 *
2395271794Smav		 * XXX: Note that this field is being updated without locking,
2396271794Smav		 * 	which might cause problems on 32-bit architectures.
2397271794Smav		 */
2398271794Smav		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
2399271794Smav		ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2400271794Smav	}
2401232604Strasz
2402232604Strasz	/* Tell the user the exact size we ended up using */
2403232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2404232604Strasz
2405232604Strasz	req->status = CTL_LUN_OK;
2406232604Strasz
2407232604Strasz	return (0);
2408232604Strasz
2409232604Straszbailout_error:
2410232604Strasz	req->status = CTL_LUN_ERROR;
2411232604Strasz
2412232604Strasz	return (0);
2413232604Strasz}
2414232604Strasz
2415229997Skenstatic void
2416229997Skenctl_be_block_lun_shutdown(void *be_lun)
2417229997Sken{
2418229997Sken	struct ctl_be_block_lun *lun;
2419229997Sken	struct ctl_be_block_softc *softc;
2420229997Sken
2421229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2422229997Sken
2423229997Sken	softc = lun->softc;
2424229997Sken
2425229997Sken	mtx_lock(&softc->lock);
2426229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2427229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2428229997Sken		wakeup(lun);
2429229997Sken	mtx_unlock(&softc->lock);
2430229997Sken
2431229997Sken}
2432229997Sken
2433229997Skenstatic void
2434229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2435229997Sken{
2436229997Sken	struct ctl_be_block_lun *lun;
2437229997Sken	struct ctl_be_block_softc *softc;
2438229997Sken
2439229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2440229997Sken	softc = lun->softc;
2441229997Sken
2442229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2443229997Sken		mtx_lock(&softc->lock);
2444229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2445229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2446229997Sken			wakeup(lun);
2447229997Sken		mtx_unlock(&softc->lock);
2448229997Sken
2449229997Sken		/*
2450229997Sken		 * We successfully added the LUN, attempt to enable it.
2451229997Sken		 */
2452229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2453229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2454229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2455229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2456229997Sken				       __func__);
2457229997Sken			}
2458229997Sken		}
2459229997Sken
2460229997Sken		return;
2461229997Sken	}
2462229997Sken
2463229997Sken
2464229997Sken	mtx_lock(&softc->lock);
2465229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2466229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2467229997Sken	wakeup(lun);
2468229997Sken	mtx_unlock(&softc->lock);
2469229997Sken}
2470229997Sken
2471229997Sken
2472229997Skenstatic int
2473229997Skenctl_be_block_config_write(union ctl_io *io)
2474229997Sken{
2475229997Sken	struct ctl_be_block_lun *be_lun;
2476229997Sken	struct ctl_be_lun *ctl_be_lun;
2477229997Sken	int retval;
2478229997Sken
2479229997Sken	retval = 0;
2480229997Sken
2481229997Sken	DPRINTF("entered\n");
2482229997Sken
2483229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2484229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2485229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2486229997Sken
2487229997Sken	switch (io->scsiio.cdb[0]) {
2488229997Sken	case SYNCHRONIZE_CACHE:
2489229997Sken	case SYNCHRONIZE_CACHE_16:
2490264274Smav	case WRITE_SAME_10:
2491264274Smav	case WRITE_SAME_16:
2492264274Smav	case UNMAP:
2493229997Sken		/*
2494229997Sken		 * The upper level CTL code will filter out any CDBs with
2495229997Sken		 * the immediate bit set and return the proper error.
2496229997Sken		 *
2497229997Sken		 * We don't really need to worry about what LBA range the
2498229997Sken		 * user asked to be synced out.  When they issue a sync
2499229997Sken		 * cache command, we'll sync out the whole thing.
2500229997Sken		 */
2501267877Smav		mtx_lock(&be_lun->queue_lock);
2502229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2503229997Sken				   links);
2504267877Smav		mtx_unlock(&be_lun->queue_lock);
2505229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2506229997Sken		break;
2507229997Sken	case START_STOP_UNIT: {
2508229997Sken		struct scsi_start_stop_unit *cdb;
2509229997Sken
2510229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2511229997Sken
2512229997Sken		if (cdb->how & SSS_START)
2513229997Sken			retval = ctl_start_lun(ctl_be_lun);
2514229997Sken		else {
2515229997Sken			retval = ctl_stop_lun(ctl_be_lun);
2516229997Sken			/*
2517229997Sken			 * XXX KDM Copan-specific offline behavior.
2518229997Sken			 * Figure out a reasonable way to port this?
2519229997Sken			 */
2520229997Sken#ifdef NEEDTOPORT
2521229997Sken			if ((retval == 0)
2522229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2523229997Sken				retval = ctl_lun_offline(ctl_be_lun);
2524229997Sken#endif
2525229997Sken		}
2526229997Sken
2527229997Sken		/*
2528229997Sken		 * In general, the above routines should not fail.  They
2529229997Sken		 * just set state for the LUN.  So we've got something
2530229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2531229997Sken		 */
2532229997Sken		if (retval != 0) {
2533229997Sken			ctl_set_internal_failure(&io->scsiio,
2534229997Sken						 /*sks_valid*/ 1,
2535229997Sken						 /*retry_count*/ 0xf051);
2536229997Sken			retval = CTL_RETVAL_COMPLETE;
2537229997Sken		} else {
2538229997Sken			ctl_set_success(&io->scsiio);
2539229997Sken		}
2540229997Sken		ctl_config_write_done(io);
2541229997Sken		break;
2542229997Sken	}
2543229997Sken	default:
2544229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2545229997Sken		ctl_config_write_done(io);
2546229997Sken		retval = CTL_RETVAL_COMPLETE;
2547229997Sken		break;
2548229997Sken	}
2549229997Sken
2550229997Sken	return (retval);
2551229997Sken
2552229997Sken}
2553229997Sken
2554229997Skenstatic int
2555229997Skenctl_be_block_config_read(union ctl_io *io)
2556229997Sken{
2557229997Sken	return (0);
2558229997Sken}
2559229997Sken
2560229997Skenstatic int
2561229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2562229997Sken{
2563229997Sken	struct ctl_be_block_lun *lun;
2564229997Sken	int retval;
2565229997Sken
2566229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2567229997Sken	retval = 0;
2568229997Sken
2569268283Smav	retval = sbuf_printf(sb, "\t<num_threads>");
2570229997Sken
2571229997Sken	if (retval != 0)
2572229997Sken		goto bailout;
2573229997Sken
2574229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2575229997Sken
2576229997Sken	if (retval != 0)
2577229997Sken		goto bailout;
2578229997Sken
2579268283Smav	retval = sbuf_printf(sb, "</num_threads>\n");
2580229997Sken
2581229997Skenbailout:
2582229997Sken
2583229997Sken	return (retval);
2584229997Sken}
2585229997Sken
2586229997Skenint
2587229997Skenctl_be_block_init(void)
2588229997Sken{
2589229997Sken	struct ctl_be_block_softc *softc;
2590229997Sken	int retval;
2591229997Sken
2592229997Sken	softc = &backend_block_softc;
2593229997Sken	retval = 0;
2594229997Sken
2595267877Smav	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2596264020Strasz	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2597264020Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2598229997Sken	STAILQ_INIT(&softc->disk_list);
2599229997Sken	STAILQ_INIT(&softc->lun_list);
2600229997Sken
2601229997Sken	return (retval);
2602229997Sken}
2603