ctl_backend_block.c revision 273029
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 273029 2014-10-13 11:00:58Z 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 {
154272911Smav	struct ctl_lun_create_params params;
155229997Sken	struct ctl_block_disk *disk;
156229997Sken	char lunname[32];
157229997Sken	char *dev_path;
158229997Sken	ctl_be_block_type dev_type;
159229997Sken	struct vnode *vn;
160229997Sken	union ctl_be_block_bedata backend;
161229997Sken	cbb_dispatch_t dispatch;
162229997Sken	cbb_dispatch_t lun_flush;
163264274Smav	cbb_dispatch_t unmap;
164229997Sken	uma_zone_t lun_zone;
165229997Sken	uint64_t size_blocks;
166229997Sken	uint64_t size_bytes;
167229997Sken	uint32_t blocksize;
168229997Sken	int blocksize_shift;
169264191Smav	uint16_t pblockexp;
170264191Smav	uint16_t pblockoff;
171229997Sken	struct ctl_be_block_softc *softc;
172229997Sken	struct devstat *disk_stats;
173229997Sken	ctl_be_block_lun_flags flags;
174229997Sken	STAILQ_ENTRY(ctl_be_block_lun) links;
175229997Sken	struct ctl_be_lun ctl_be_lun;
176229997Sken	struct taskqueue *io_taskqueue;
177229997Sken	struct task io_task;
178229997Sken	int num_threads;
179229997Sken	STAILQ_HEAD(, ctl_io_hdr) input_queue;
180229997Sken	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
181229997Sken	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
182267877Smav	struct mtx_padalign io_lock;
183267877Smav	struct mtx_padalign queue_lock;
184229997Sken};
185229997Sken
186229997Sken/*
187229997Sken * Overall softc structure for the block backend module.
188229997Sken */
189229997Skenstruct ctl_be_block_softc {
190229997Sken	struct mtx			 lock;
191229997Sken	int				 num_disks;
192229997Sken	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
193229997Sken	int				 num_luns;
194229997Sken	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
195229997Sken};
196229997Sken
197229997Skenstatic struct ctl_be_block_softc backend_block_softc;
198229997Sken
199229997Sken/*
200229997Sken * Per-I/O information.
201229997Sken */
202229997Skenstruct ctl_be_block_io {
203229997Sken	union ctl_io			*io;
204229997Sken	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
205229997Sken	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
206229997Sken	int				bio_cmd;
207229997Sken	int				num_segs;
208229997Sken	int				num_bios_sent;
209229997Sken	int				num_bios_done;
210229997Sken	int				send_complete;
211229997Sken	int				num_errors;
212229997Sken	struct bintime			ds_t0;
213229997Sken	devstat_tag_type		ds_tag_type;
214229997Sken	devstat_trans_flags		ds_trans_type;
215229997Sken	uint64_t			io_len;
216229997Sken	uint64_t			io_offset;
217229997Sken	struct ctl_be_block_softc	*softc;
218229997Sken	struct ctl_be_block_lun		*lun;
219264274Smav	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
220229997Sken};
221229997Sken
222229997Skenstatic int cbb_num_threads = 14;
223229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
224229997Sken	    "CAM Target Layer Block Backend");
225267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
226229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
227229997Sken
228229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
229229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
230229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
231229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
232229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
233229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
234229997Sken				    struct ctl_be_block_io *beio);
235229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
236229997Sken				       struct ctl_be_block_io *beio);
237229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
238229997Sken				   struct ctl_be_block_io *beio);
239264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
240264274Smav				   struct ctl_be_block_io *beio);
241229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
242229997Sken				      struct ctl_be_block_io *beio);
243229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
244229997Sken				    union ctl_io *io);
245229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
246229997Sken				  union ctl_io *io);
247229997Skenstatic void ctl_be_block_worker(void *context, int pending);
248229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
249229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
250229997Sken				   int flag, struct thread *td);
251229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
252229997Sken				  struct ctl_lun_req *req);
253229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
254229997Sken				 struct ctl_lun_req *req);
255229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
256229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
257229997Sken			     struct ctl_be_block_lun *be_lun,
258229997Sken			     struct ctl_lun_req *req);
259229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
260229997Sken			       struct ctl_lun_req *req);
261229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
262229997Sken			   struct ctl_lun_req *req);
263232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
264232604Strasz				  struct ctl_lun_req *req);
265232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
266232604Strasz				 struct ctl_lun_req *req);
267232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
268232604Strasz			   struct ctl_lun_req *req);
269229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
270229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
271229997Sken					   ctl_lun_config_status status);
272229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
273229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
274229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
275229997Skenint ctl_be_block_init(void);
276229997Sken
277229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
278229997Sken{
279230334Sken	.name = "block",
280230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
281230334Sken	.init = ctl_be_block_init,
282230334Sken	.data_submit = ctl_be_block_submit,
283230334Sken	.data_move_done = ctl_be_block_move_done,
284230334Sken	.config_read = ctl_be_block_config_read,
285230334Sken	.config_write = ctl_be_block_config_write,
286230334Sken	.ioctl = ctl_be_block_ioctl,
287230334Sken	.lun_info = ctl_be_block_lun_info
288229997Sken};
289229997Sken
290229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
291229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
292229997Sken
293264020Straszstatic uma_zone_t beio_zone;
294264020Strasz
295229997Skenstatic struct ctl_be_block_io *
296229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
297229997Sken{
298229997Sken	struct ctl_be_block_io *beio;
299229997Sken
300264020Strasz	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
301264020Strasz	beio->softc = softc;
302229997Sken	return (beio);
303229997Sken}
304229997Sken
305229997Skenstatic void
306229997Skenctl_free_beio(struct ctl_be_block_io *beio)
307229997Sken{
308229997Sken	int duplicate_free;
309229997Sken	int i;
310229997Sken
311229997Sken	duplicate_free = 0;
312229997Sken
313229997Sken	for (i = 0; i < beio->num_segs; i++) {
314229997Sken		if (beio->sg_segs[i].addr == NULL)
315229997Sken			duplicate_free++;
316229997Sken
317229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
318229997Sken		beio->sg_segs[i].addr = NULL;
319267537Smav
320267537Smav		/* For compare we had two equal S/G lists. */
321267537Smav		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
322267537Smav			uma_zfree(beio->lun->lun_zone,
323267537Smav			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
324267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
325267537Smav		}
326229997Sken	}
327229997Sken
328229997Sken	if (duplicate_free > 0) {
329229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
330229997Sken		       duplicate_free, beio->num_segs);
331229997Sken	}
332229997Sken
333264020Strasz	uma_zfree(beio_zone, beio);
334229997Sken}
335229997Sken
336229997Skenstatic void
337229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
338229997Sken{
339267877Smav	union ctl_io *io = beio->io;
340229997Sken
341264274Smav	if (beio->beio_cont != NULL) {
342264274Smav		beio->beio_cont(beio);
343264274Smav	} else {
344264274Smav		ctl_free_beio(beio);
345267537Smav		ctl_data_submit_done(io);
346264274Smav	}
347229997Sken}
348229997Sken
349229997Skenstatic int
350229997Skenctl_be_block_move_done(union ctl_io *io)
351229997Sken{
352229997Sken	struct ctl_be_block_io *beio;
353229997Sken	struct ctl_be_block_lun *be_lun;
354267537Smav	struct ctl_lba_len_flags *lbalen;
355229997Sken#ifdef CTL_TIME_IO
356229997Sken	struct bintime cur_bt;
357267537Smav#endif
358267537Smav	int i;
359229997Sken
360267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
361229997Sken	be_lun = beio->lun;
362229997Sken
363229997Sken	DPRINTF("entered\n");
364229997Sken
365229997Sken#ifdef CTL_TIME_IO
366229997Sken	getbintime(&cur_bt);
367229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
368229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
369229997Sken	io->io_hdr.num_dmas++;
370229997Sken#endif
371267537Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
372229997Sken
373229997Sken	/*
374229997Sken	 * We set status at this point for read commands, and write
375229997Sken	 * commands with errors.
376229997Sken	 */
377267537Smav	if ((io->io_hdr.port_status == 0) &&
378267537Smav	    ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) &&
379267537Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
380267537Smav		lbalen = ARGS(beio->io);
381267537Smav		if (lbalen->flags & CTL_LLF_READ) {
382267537Smav			ctl_set_success(&io->scsiio);
383267537Smav		} else if (lbalen->flags & CTL_LLF_COMPARE) {
384267537Smav			/* We have two data blocks ready for comparison. */
385267537Smav			for (i = 0; i < beio->num_segs; i++) {
386267537Smav				if (memcmp(beio->sg_segs[i].addr,
387267537Smav				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
388267537Smav				    beio->sg_segs[i].len) != 0)
389267537Smav					break;
390267537Smav			}
391267537Smav			if (i < beio->num_segs)
392267537Smav				ctl_set_sense(&io->scsiio,
393267537Smav				    /*current_error*/ 1,
394267537Smav				    /*sense_key*/ SSD_KEY_MISCOMPARE,
395267537Smav				    /*asc*/ 0x1D,
396267537Smav				    /*ascq*/ 0x00,
397267537Smav				    SSD_ELEM_NONE);
398267537Smav			else
399267537Smav				ctl_set_success(&io->scsiio);
400267537Smav		}
401267537Smav	}
402229997Sken	else if ((io->io_hdr.port_status != 0)
403229997Sken	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
404229997Sken	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
405229997Sken		/*
406229997Sken		 * For hardware error sense keys, the sense key
407229997Sken		 * specific value is defined to be a retry count,
408229997Sken		 * but we use it to pass back an internal FETD
409229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
410229997Sken		 * using 16 bits for an error code, since that's
411229997Sken		 * all the space we have in the sks field.
412229997Sken		 */
413229997Sken		ctl_set_internal_failure(&io->scsiio,
414229997Sken					 /*sks_valid*/ 1,
415229997Sken					 /*retry_count*/
416229997Sken					 io->io_hdr.port_status);
417229997Sken	}
418229997Sken
419229997Sken	/*
420229997Sken	 * If this is a read, or a write with errors, it is done.
421229997Sken	 */
422229997Sken	if ((beio->bio_cmd == BIO_READ)
423229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
424229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
425229997Sken		ctl_complete_beio(beio);
426229997Sken		return (0);
427229997Sken	}
428229997Sken
429229997Sken	/*
430229997Sken	 * At this point, we have a write and the DMA completed
431229997Sken	 * successfully.  We now have to queue it to the task queue to
432229997Sken	 * execute the backend I/O.  That is because we do blocking
433229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
434229997Sken	 * This move done routine is generally called in the SIM's
435229997Sken	 * interrupt context, and therefore we cannot block.
436229997Sken	 */
437267877Smav	mtx_lock(&be_lun->queue_lock);
438229997Sken	/*
439229997Sken	 * XXX KDM make sure that links is okay to use at this point.
440229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
441229997Sken	 * or deal with resource allocation here.
442229997Sken	 */
443229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
444267877Smav	mtx_unlock(&be_lun->queue_lock);
445229997Sken
446229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
447229997Sken
448229997Sken	return (0);
449229997Sken}
450229997Sken
451229997Skenstatic void
452229997Skenctl_be_block_biodone(struct bio *bio)
453229997Sken{
454229997Sken	struct ctl_be_block_io *beio;
455229997Sken	struct ctl_be_block_lun *be_lun;
456229997Sken	union ctl_io *io;
457261538Smav	int error;
458229997Sken
459229997Sken	beio = bio->bio_caller1;
460229997Sken	be_lun = beio->lun;
461229997Sken	io = beio->io;
462229997Sken
463229997Sken	DPRINTF("entered\n");
464229997Sken
465261538Smav	error = bio->bio_error;
466267877Smav	mtx_lock(&be_lun->io_lock);
467261538Smav	if (error != 0)
468229997Sken		beio->num_errors++;
469229997Sken
470229997Sken	beio->num_bios_done++;
471229997Sken
472229997Sken	/*
473229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
474229997Sken	 * during the free might cause it to complain.
475229997Sken	 */
476229997Sken	g_destroy_bio(bio);
477229997Sken
478229997Sken	/*
479229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
480229997Sken	 * complete, then we're done.
481229997Sken	 */
482229997Sken	if ((beio->send_complete == 0)
483229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
484267877Smav		mtx_unlock(&be_lun->io_lock);
485229997Sken		return;
486229997Sken	}
487229997Sken
488229997Sken	/*
489229997Sken	 * At this point, we've verified that we are the last I/O to
490229997Sken	 * complete, so it's safe to drop the lock.
491229997Sken	 */
492267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
493267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
494267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
495267877Smav	mtx_unlock(&be_lun->io_lock);
496229997Sken
497229997Sken	/*
498229997Sken	 * If there are any errors from the backing device, we fail the
499229997Sken	 * entire I/O with a medium error.
500229997Sken	 */
501229997Sken	if (beio->num_errors > 0) {
502261538Smav		if (error == EOPNOTSUPP) {
503261538Smav			ctl_set_invalid_opcode(&io->scsiio);
504261538Smav		} else if (beio->bio_cmd == BIO_FLUSH) {
505229997Sken			/* XXX KDM is there is a better error here? */
506229997Sken			ctl_set_internal_failure(&io->scsiio,
507229997Sken						 /*sks_valid*/ 1,
508229997Sken						 /*retry_count*/ 0xbad2);
509229997Sken		} else
510229997Sken			ctl_set_medium_error(&io->scsiio);
511229997Sken		ctl_complete_beio(beio);
512229997Sken		return;
513229997Sken	}
514229997Sken
515229997Sken	/*
516267537Smav	 * If this is a write, a flush, a delete or verify, we're all done.
517229997Sken	 * If this is a read, we can now send the data to the user.
518229997Sken	 */
519229997Sken	if ((beio->bio_cmd == BIO_WRITE)
520264274Smav	 || (beio->bio_cmd == BIO_FLUSH)
521267537Smav	 || (beio->bio_cmd == BIO_DELETE)
522267537Smav	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
523229997Sken		ctl_set_success(&io->scsiio);
524229997Sken		ctl_complete_beio(beio);
525229997Sken	} else {
526229997Sken#ifdef CTL_TIME_IO
527229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
528229997Sken#endif
529229997Sken		ctl_datamove(io);
530229997Sken	}
531229997Sken}
532229997Sken
533229997Skenstatic void
534229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
535229997Sken			struct ctl_be_block_io *beio)
536229997Sken{
537267877Smav	union ctl_io *io = beio->io;
538229997Sken	struct mount *mountpoint;
539241896Skib	int error, lock_flags;
540229997Sken
541229997Sken	DPRINTF("entered\n");
542229997Sken
543267877Smav	binuptime(&beio->ds_t0);
544267877Smav	mtx_lock(&be_lun->io_lock);
545267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
546267877Smav	mtx_unlock(&be_lun->io_lock);
547229997Sken
548267877Smav	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
549229997Sken
550229997Sken	if (MNT_SHARED_WRITES(mountpoint)
551229997Sken	 || ((mountpoint == NULL)
552229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
553229997Sken		lock_flags = LK_SHARED;
554229997Sken	else
555229997Sken		lock_flags = LK_EXCLUSIVE;
556229997Sken
557229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
558229997Sken
559229997Sken	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
560229997Sken	VOP_UNLOCK(be_lun->vn, 0);
561229997Sken
562229997Sken	vn_finished_write(mountpoint);
563229997Sken
564267877Smav	mtx_lock(&be_lun->io_lock);
565267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
566267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
567267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
568267877Smav	mtx_unlock(&be_lun->io_lock);
569267877Smav
570229997Sken	if (error == 0)
571229997Sken		ctl_set_success(&io->scsiio);
572229997Sken	else {
573229997Sken		/* XXX KDM is there is a better error here? */
574229997Sken		ctl_set_internal_failure(&io->scsiio,
575229997Sken					 /*sks_valid*/ 1,
576229997Sken					 /*retry_count*/ 0xbad1);
577229997Sken	}
578229997Sken
579229997Sken	ctl_complete_beio(beio);
580229997Sken}
581229997Sken
582258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
583258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
584258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
585258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
586229997Sken
587229997Skenstatic void
588229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
589229997Sken			   struct ctl_be_block_io *beio)
590229997Sken{
591229997Sken	struct ctl_be_block_filedata *file_data;
592229997Sken	union ctl_io *io;
593229997Sken	struct uio xuio;
594229997Sken	struct iovec *xiovec;
595241896Skib	int flags;
596229997Sken	int error, i;
597229997Sken
598229997Sken	DPRINTF("entered\n");
599229997Sken
600229997Sken	file_data = &be_lun->backend.file;
601229997Sken	io = beio->io;
602271309Smav	flags = 0;
603271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
604271309Smav		flags |= IO_DIRECT;
605271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
606271309Smav		flags |= IO_SYNC;
607229997Sken
608267537Smav	bzero(&xuio, sizeof(xuio));
609229997Sken	if (beio->bio_cmd == BIO_READ) {
610229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
611267537Smav		xuio.uio_rw = UIO_READ;
612229997Sken	} else {
613229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
614267537Smav		xuio.uio_rw = UIO_WRITE;
615229997Sken	}
616229997Sken	xuio.uio_offset = beio->io_offset;
617229997Sken	xuio.uio_resid = beio->io_len;
618229997Sken	xuio.uio_segflg = UIO_SYSSPACE;
619229997Sken	xuio.uio_iov = beio->xiovecs;
620229997Sken	xuio.uio_iovcnt = beio->num_segs;
621229997Sken	xuio.uio_td = curthread;
622229997Sken
623229997Sken	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
624229997Sken		xiovec->iov_base = beio->sg_segs[i].addr;
625229997Sken		xiovec->iov_len = beio->sg_segs[i].len;
626229997Sken	}
627229997Sken
628267877Smav	binuptime(&beio->ds_t0);
629267877Smav	mtx_lock(&be_lun->io_lock);
630267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
631267877Smav	mtx_unlock(&be_lun->io_lock);
632267877Smav
633229997Sken	if (beio->bio_cmd == BIO_READ) {
634229997Sken		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
635229997Sken
636229997Sken		/*
637229997Sken		 * UFS pays attention to IO_DIRECT for reads.  If the
638229997Sken		 * DIRECTIO option is configured into the kernel, it calls
639229997Sken		 * ffs_rawread().  But that only works for single-segment
640229997Sken		 * uios with user space addresses.  In our case, with a
641229997Sken		 * kernel uio, it still reads into the buffer cache, but it
642229997Sken		 * will just try to release the buffer from the cache later
643229997Sken		 * on in ffs_read().
644229997Sken		 *
645229997Sken		 * ZFS does not pay attention to IO_DIRECT for reads.
646229997Sken		 *
647229997Sken		 * UFS does not pay attention to IO_SYNC for reads.
648229997Sken		 *
649229997Sken		 * ZFS pays attention to IO_SYNC (which translates into the
650229997Sken		 * Solaris define FRSYNC for zfs_read()) for reads.  It
651229997Sken		 * attempts to sync the file before reading.
652229997Sken		 *
653229997Sken		 * So, to attempt to provide some barrier semantics in the
654229997Sken		 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
655229997Sken		 */
656271309Smav		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
657229997Sken
658229997Sken		VOP_UNLOCK(be_lun->vn, 0);
659267537Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
660229997Sken	} else {
661229997Sken		struct mount *mountpoint;
662229997Sken		int lock_flags;
663229997Sken
664229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
665229997Sken
666229997Sken		if (MNT_SHARED_WRITES(mountpoint)
667229997Sken		 || ((mountpoint == NULL)
668229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
669229997Sken			lock_flags = LK_SHARED;
670229997Sken		else
671229997Sken			lock_flags = LK_EXCLUSIVE;
672229997Sken
673229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
674229997Sken
675229997Sken		/*
676229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
677229997Sken		 * is done asynchronously.  (Normally the write would just
678229997Sken		 * get put into cache.
679229997Sken		 *
680229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
681229997Sken		 * attempt to write the buffer out synchronously if that
682229997Sken		 * flag is set.
683229997Sken		 *
684229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
685229997Sken		 *
686229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
687229997Sken		 * for writes.  It will flush the transaction from the
688229997Sken		 * cache before returning.
689229997Sken		 *
690229997Sken		 * So if we've got the BIO_ORDERED flag set, we want
691229997Sken		 * IO_SYNC in either the UFS or ZFS case.
692229997Sken		 */
693271309Smav		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
694229997Sken		VOP_UNLOCK(be_lun->vn, 0);
695229997Sken
696229997Sken		vn_finished_write(mountpoint);
697267537Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
698229997Sken        }
699229997Sken
700267877Smav	mtx_lock(&be_lun->io_lock);
701267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
702267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
703267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
704267877Smav	mtx_unlock(&be_lun->io_lock);
705267877Smav
706229997Sken	/*
707229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
708229997Sken	 * return the I/O to the user.
709229997Sken	 */
710229997Sken	if (error != 0) {
711229997Sken		char path_str[32];
712229997Sken
713229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
714229997Sken		/*
715229997Sken		 * XXX KDM ZFS returns ENOSPC when the underlying
716229997Sken		 * filesystem fills up.  What kind of SCSI error should we
717229997Sken		 * return for that?
718229997Sken		 */
719229997Sken		printf("%s%s command returned errno %d\n", path_str,
720229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
721229997Sken		ctl_set_medium_error(&io->scsiio);
722229997Sken		ctl_complete_beio(beio);
723229997Sken		return;
724229997Sken	}
725229997Sken
726229997Sken	/*
727269122Smav	 * If this is a write or a verify, we're all done.
728229997Sken	 * If this is a read, we can now send the data to the user.
729229997Sken	 */
730269122Smav	if ((beio->bio_cmd == BIO_WRITE) ||
731269122Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
732229997Sken		ctl_set_success(&io->scsiio);
733229997Sken		ctl_complete_beio(beio);
734229997Sken	} else {
735229997Sken#ifdef CTL_TIME_IO
736229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
737229997Sken#endif
738229997Sken		ctl_datamove(io);
739229997Sken	}
740229997Sken}
741229997Sken
742229997Skenstatic void
743269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
744269123Smav			   struct ctl_be_block_io *beio)
745269123Smav{
746269123Smav	struct ctl_be_block_devdata *dev_data;
747269123Smav	union ctl_io *io;
748269123Smav	struct uio xuio;
749269123Smav	struct iovec *xiovec;
750269123Smav	int flags;
751269123Smav	int error, i;
752269123Smav
753269123Smav	DPRINTF("entered\n");
754269123Smav
755269123Smav	dev_data = &be_lun->backend.dev;
756269123Smav	io = beio->io;
757271309Smav	flags = 0;
758271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
759271309Smav		flags |= IO_DIRECT;
760271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
761271309Smav		flags |= IO_SYNC;
762269123Smav
763269123Smav	bzero(&xuio, sizeof(xuio));
764269123Smav	if (beio->bio_cmd == BIO_READ) {
765269123Smav		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
766269123Smav		xuio.uio_rw = UIO_READ;
767269123Smav	} else {
768269123Smav		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
769269123Smav		xuio.uio_rw = UIO_WRITE;
770269123Smav	}
771269123Smav	xuio.uio_offset = beio->io_offset;
772269123Smav	xuio.uio_resid = beio->io_len;
773269123Smav	xuio.uio_segflg = UIO_SYSSPACE;
774269123Smav	xuio.uio_iov = beio->xiovecs;
775269123Smav	xuio.uio_iovcnt = beio->num_segs;
776269123Smav	xuio.uio_td = curthread;
777269123Smav
778269123Smav	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
779269123Smav		xiovec->iov_base = beio->sg_segs[i].addr;
780269123Smav		xiovec->iov_len = beio->sg_segs[i].len;
781269123Smav	}
782269123Smav
783269123Smav	binuptime(&beio->ds_t0);
784269123Smav	mtx_lock(&be_lun->io_lock);
785269123Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
786269123Smav	mtx_unlock(&be_lun->io_lock);
787269123Smav
788269123Smav	if (beio->bio_cmd == BIO_READ) {
789271309Smav		error = (*dev_data->csw->d_read)(dev_data->cdev, &xuio, flags);
790269123Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
791269123Smav	} else {
792271309Smav		error = (*dev_data->csw->d_write)(dev_data->cdev, &xuio, flags);
793269123Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
794269123Smav	}
795269123Smav
796269123Smav	mtx_lock(&be_lun->io_lock);
797269123Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
798269123Smav	    beio->ds_tag_type, beio->ds_trans_type,
799269123Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
800269123Smav	mtx_unlock(&be_lun->io_lock);
801269123Smav
802269123Smav	/*
803269123Smav	 * If we got an error, set the sense data to "MEDIUM ERROR" and
804269123Smav	 * return the I/O to the user.
805269123Smav	 */
806269123Smav	if (error != 0) {
807269123Smav		ctl_set_medium_error(&io->scsiio);
808269123Smav		ctl_complete_beio(beio);
809269123Smav		return;
810269123Smav	}
811269123Smav
812269123Smav	/*
813269123Smav	 * If this is a write or a verify, we're all done.
814269123Smav	 * If this is a read, we can now send the data to the user.
815269123Smav	 */
816269123Smav	if ((beio->bio_cmd == BIO_WRITE) ||
817269123Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
818269123Smav		ctl_set_success(&io->scsiio);
819269123Smav		ctl_complete_beio(beio);
820269123Smav	} else {
821269123Smav#ifdef CTL_TIME_IO
822269123Smav        	getbintime(&io->io_hdr.dma_start_bt);
823269123Smav#endif
824269123Smav		ctl_datamove(io);
825269123Smav	}
826269123Smav}
827269123Smav
828269123Smavstatic void
829229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
830229997Sken		       struct ctl_be_block_io *beio)
831229997Sken{
832229997Sken	struct bio *bio;
833229997Sken	union ctl_io *io;
834229997Sken	struct ctl_be_block_devdata *dev_data;
835229997Sken
836229997Sken	dev_data = &be_lun->backend.dev;
837229997Sken	io = beio->io;
838229997Sken
839229997Sken	DPRINTF("entered\n");
840229997Sken
841229997Sken	/* This can't fail, it's a blocking allocation. */
842229997Sken	bio = g_alloc_bio();
843229997Sken
844229997Sken	bio->bio_cmd	    = BIO_FLUSH;
845229997Sken	bio->bio_flags	   |= BIO_ORDERED;
846229997Sken	bio->bio_dev	    = dev_data->cdev;
847229997Sken	bio->bio_offset	    = 0;
848229997Sken	bio->bio_data	    = 0;
849229997Sken	bio->bio_done	    = ctl_be_block_biodone;
850229997Sken	bio->bio_caller1    = beio;
851229997Sken	bio->bio_pblkno	    = 0;
852229997Sken
853229997Sken	/*
854229997Sken	 * We don't need to acquire the LUN lock here, because we are only
855229997Sken	 * sending one bio, and so there is no other context to synchronize
856229997Sken	 * with.
857229997Sken	 */
858229997Sken	beio->num_bios_sent = 1;
859229997Sken	beio->send_complete = 1;
860229997Sken
861229997Sken	binuptime(&beio->ds_t0);
862267877Smav	mtx_lock(&be_lun->io_lock);
863229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
864267877Smav	mtx_unlock(&be_lun->io_lock);
865229997Sken
866229997Sken	(*dev_data->csw->d_strategy)(bio);
867229997Sken}
868229997Sken
869229997Skenstatic void
870264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
871264274Smav		       struct ctl_be_block_io *beio,
872264274Smav		       uint64_t off, uint64_t len, int last)
873264274Smav{
874264274Smav	struct bio *bio;
875264274Smav	struct ctl_be_block_devdata *dev_data;
876264296Smav	uint64_t maxlen;
877264274Smav
878264274Smav	dev_data = &be_lun->backend.dev;
879264296Smav	maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
880264274Smav	while (len > 0) {
881264274Smav		bio = g_alloc_bio();
882264274Smav		bio->bio_cmd	    = BIO_DELETE;
883264274Smav		bio->bio_dev	    = dev_data->cdev;
884264274Smav		bio->bio_offset	    = off;
885264296Smav		bio->bio_length	    = MIN(len, maxlen);
886264274Smav		bio->bio_data	    = 0;
887264274Smav		bio->bio_done	    = ctl_be_block_biodone;
888264274Smav		bio->bio_caller1    = beio;
889264296Smav		bio->bio_pblkno     = off / be_lun->blocksize;
890264274Smav
891264274Smav		off += bio->bio_length;
892264274Smav		len -= bio->bio_length;
893264274Smav
894267877Smav		mtx_lock(&be_lun->io_lock);
895264274Smav		beio->num_bios_sent++;
896264274Smav		if (last && len == 0)
897264274Smav			beio->send_complete = 1;
898267877Smav		mtx_unlock(&be_lun->io_lock);
899264274Smav
900264274Smav		(*dev_data->csw->d_strategy)(bio);
901264274Smav	}
902264274Smav}
903264274Smav
904264274Smavstatic void
905264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
906264274Smav		       struct ctl_be_block_io *beio)
907264274Smav{
908264274Smav	union ctl_io *io;
909264274Smav	struct ctl_be_block_devdata *dev_data;
910267515Smav	struct ctl_ptr_len_flags *ptrlen;
911264274Smav	struct scsi_unmap_desc *buf, *end;
912264274Smav	uint64_t len;
913264274Smav
914264274Smav	dev_data = &be_lun->backend.dev;
915264274Smav	io = beio->io;
916264274Smav
917264274Smav	DPRINTF("entered\n");
918264274Smav
919264274Smav	binuptime(&beio->ds_t0);
920267877Smav	mtx_lock(&be_lun->io_lock);
921264274Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
922267877Smav	mtx_unlock(&be_lun->io_lock);
923264274Smav
924264274Smav	if (beio->io_offset == -1) {
925264274Smav		beio->io_len = 0;
926267515Smav		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
927267515Smav		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
928267515Smav		end = buf + ptrlen->len / sizeof(*buf);
929264274Smav		for (; buf < end; buf++) {
930264274Smav			len = (uint64_t)scsi_4btoul(buf->length) *
931264274Smav			    be_lun->blocksize;
932264274Smav			beio->io_len += len;
933264274Smav			ctl_be_block_unmap_dev_range(be_lun, beio,
934264274Smav			    scsi_8btou64(buf->lba) * be_lun->blocksize, len,
935264283Smav			    (end - buf < 2) ? TRUE : FALSE);
936264274Smav		}
937264274Smav	} else
938264274Smav		ctl_be_block_unmap_dev_range(be_lun, beio,
939264274Smav		    beio->io_offset, beio->io_len, TRUE);
940264274Smav}
941264274Smav
942264274Smavstatic void
943229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
944229997Sken			  struct ctl_be_block_io *beio)
945229997Sken{
946267877Smav	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
947229997Sken	int i;
948229997Sken	struct bio *bio;
949229997Sken	struct ctl_be_block_devdata *dev_data;
950229997Sken	off_t cur_offset;
951229997Sken	int max_iosize;
952229997Sken
953229997Sken	DPRINTF("entered\n");
954229997Sken
955229997Sken	dev_data = &be_lun->backend.dev;
956229997Sken
957229997Sken	/*
958229997Sken	 * We have to limit our I/O size to the maximum supported by the
959229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
960229997Sken	 * set it properly, use DFLTPHYS.
961229997Sken	 */
962229997Sken	max_iosize = dev_data->cdev->si_iosize_max;
963229997Sken	if (max_iosize < PAGE_SIZE)
964229997Sken		max_iosize = DFLTPHYS;
965229997Sken
966229997Sken	cur_offset = beio->io_offset;
967229997Sken	for (i = 0; i < beio->num_segs; i++) {
968229997Sken		size_t cur_size;
969229997Sken		uint8_t *cur_ptr;
970229997Sken
971229997Sken		cur_size = beio->sg_segs[i].len;
972229997Sken		cur_ptr = beio->sg_segs[i].addr;
973229997Sken
974229997Sken		while (cur_size > 0) {
975229997Sken			/* This can't fail, it's a blocking allocation. */
976229997Sken			bio = g_alloc_bio();
977229997Sken
978229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
979229997Sken
980229997Sken			bio->bio_cmd = beio->bio_cmd;
981229997Sken			bio->bio_dev = dev_data->cdev;
982229997Sken			bio->bio_caller1 = beio;
983229997Sken			bio->bio_length = min(cur_size, max_iosize);
984229997Sken			bio->bio_offset = cur_offset;
985229997Sken			bio->bio_data = cur_ptr;
986229997Sken			bio->bio_done = ctl_be_block_biodone;
987229997Sken			bio->bio_pblkno = cur_offset / be_lun->blocksize;
988229997Sken
989229997Sken			cur_offset += bio->bio_length;
990229997Sken			cur_ptr += bio->bio_length;
991229997Sken			cur_size -= bio->bio_length;
992229997Sken
993267877Smav			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
994229997Sken			beio->num_bios_sent++;
995229997Sken		}
996229997Sken	}
997267877Smav	binuptime(&beio->ds_t0);
998267877Smav	mtx_lock(&be_lun->io_lock);
999267877Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1000267877Smav	beio->send_complete = 1;
1001267877Smav	mtx_unlock(&be_lun->io_lock);
1002267877Smav
1003267877Smav	/*
1004267877Smav	 * Fire off all allocated requests!
1005267877Smav	 */
1006267877Smav	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1007267877Smav		TAILQ_REMOVE(&queue, bio, bio_queue);
1008267877Smav		(*dev_data->csw->d_strategy)(bio);
1009267877Smav	}
1010229997Sken}
1011229997Sken
1012229997Skenstatic void
1013264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1014264274Smav{
1015264274Smav	union ctl_io *io;
1016264274Smav
1017264274Smav	io = beio->io;
1018264274Smav	ctl_free_beio(beio);
1019267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1020267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1021267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1022264274Smav		ctl_config_write_done(io);
1023264274Smav		return;
1024264274Smav	}
1025264274Smav
1026264274Smav	ctl_be_block_config_write(io);
1027264274Smav}
1028264274Smav
1029264274Smavstatic void
1030264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1031264274Smav			    union ctl_io *io)
1032264274Smav{
1033264274Smav	struct ctl_be_block_io *beio;
1034264274Smav	struct ctl_be_block_softc *softc;
1035267515Smav	struct ctl_lba_len_flags *lbalen;
1036264274Smav	uint64_t len_left, lba;
1037264274Smav	int i, seglen;
1038264274Smav	uint8_t *buf, *end;
1039264274Smav
1040264274Smav	DPRINTF("entered\n");
1041264274Smav
1042267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1043264274Smav	softc = be_lun->softc;
1044267537Smav	lbalen = ARGS(beio->io);
1045264274Smav
1046271839Smav	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1047269622Smav	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1048264274Smav		ctl_free_beio(beio);
1049264274Smav		ctl_set_invalid_field(&io->scsiio,
1050264274Smav				      /*sks_valid*/ 1,
1051264274Smav				      /*command*/ 1,
1052264274Smav				      /*field*/ 1,
1053264274Smav				      /*bit_valid*/ 0,
1054264274Smav				      /*bit*/ 0);
1055264274Smav		ctl_config_write_done(io);
1056264274Smav		return;
1057264274Smav	}
1058264274Smav
1059264274Smav	switch (io->scsiio.tag_type) {
1060264274Smav	case CTL_TAG_ORDERED:
1061264274Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1062264274Smav		break;
1063264274Smav	case CTL_TAG_HEAD_OF_QUEUE:
1064264274Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1065264274Smav		break;
1066264274Smav	case CTL_TAG_UNTAGGED:
1067264274Smav	case CTL_TAG_SIMPLE:
1068264274Smav	case CTL_TAG_ACA:
1069264274Smav	default:
1070264274Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1071264274Smav		break;
1072264274Smav	}
1073264274Smav
1074269622Smav	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1075267515Smav		beio->io_offset = lbalen->lba * be_lun->blocksize;
1076267515Smav		beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1077264274Smav		beio->bio_cmd = BIO_DELETE;
1078264274Smav		beio->ds_trans_type = DEVSTAT_FREE;
1079264274Smav
1080264274Smav		be_lun->unmap(be_lun, beio);
1081264274Smav		return;
1082264274Smav	}
1083264274Smav
1084264274Smav	beio->bio_cmd = BIO_WRITE;
1085264274Smav	beio->ds_trans_type = DEVSTAT_WRITE;
1086264274Smav
1087264274Smav	DPRINTF("WRITE SAME at LBA %jx len %u\n",
1088267515Smav	       (uintmax_t)lbalen->lba, lbalen->len);
1089264274Smav
1090267515Smav	len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1091264274Smav	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1092264274Smav
1093264274Smav		/*
1094264274Smav		 * Setup the S/G entry for this chunk.
1095264274Smav		 */
1096264886Smav		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1097264274Smav		seglen -= seglen % be_lun->blocksize;
1098264274Smav		beio->sg_segs[i].len = seglen;
1099264274Smav		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1100264274Smav
1101264274Smav		DPRINTF("segment %d addr %p len %zd\n", i,
1102264274Smav			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1103264274Smav
1104264274Smav		beio->num_segs++;
1105264274Smav		len_left -= seglen;
1106264274Smav
1107264274Smav		buf = beio->sg_segs[i].addr;
1108264274Smav		end = buf + seglen;
1109264274Smav		for (; buf < end; buf += be_lun->blocksize) {
1110264274Smav			memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
1111267515Smav			if (lbalen->flags & SWS_LBDATA)
1112267515Smav				scsi_ulto4b(lbalen->lba + lba, buf);
1113264274Smav			lba++;
1114264274Smav		}
1115264274Smav	}
1116264274Smav
1117267515Smav	beio->io_offset = lbalen->lba * be_lun->blocksize;
1118264274Smav	beio->io_len = lba * be_lun->blocksize;
1119264274Smav
1120264274Smav	/* We can not do all in one run. Correct and schedule rerun. */
1121264274Smav	if (len_left > 0) {
1122267515Smav		lbalen->lba += lba;
1123267515Smav		lbalen->len -= lba;
1124264274Smav		beio->beio_cont = ctl_be_block_cw_done_ws;
1125264274Smav	}
1126264274Smav
1127264274Smav	be_lun->dispatch(be_lun, beio);
1128264274Smav}
1129264274Smav
1130264274Smavstatic void
1131264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1132264274Smav			    union ctl_io *io)
1133264274Smav{
1134264274Smav	struct ctl_be_block_io *beio;
1135264274Smav	struct ctl_be_block_softc *softc;
1136267515Smav	struct ctl_ptr_len_flags *ptrlen;
1137264274Smav
1138264274Smav	DPRINTF("entered\n");
1139264274Smav
1140267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1141264274Smav	softc = be_lun->softc;
1142267515Smav	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1143264274Smav
1144269622Smav	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1145264274Smav		ctl_free_beio(beio);
1146264274Smav		ctl_set_invalid_field(&io->scsiio,
1147264274Smav				      /*sks_valid*/ 0,
1148264274Smav				      /*command*/ 1,
1149264274Smav				      /*field*/ 0,
1150264274Smav				      /*bit_valid*/ 0,
1151264274Smav				      /*bit*/ 0);
1152264274Smav		ctl_config_write_done(io);
1153264274Smav		return;
1154264274Smav	}
1155264274Smav
1156264274Smav	switch (io->scsiio.tag_type) {
1157264274Smav	case CTL_TAG_ORDERED:
1158264274Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1159264274Smav		break;
1160264274Smav	case CTL_TAG_HEAD_OF_QUEUE:
1161264274Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1162264274Smav		break;
1163264274Smav	case CTL_TAG_UNTAGGED:
1164264274Smav	case CTL_TAG_SIMPLE:
1165264274Smav	case CTL_TAG_ACA:
1166264274Smav	default:
1167264274Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1168264274Smav		break;
1169264274Smav	}
1170264274Smav
1171264274Smav	beio->io_len = 0;
1172264274Smav	beio->io_offset = -1;
1173264274Smav
1174264274Smav	beio->bio_cmd = BIO_DELETE;
1175264274Smav	beio->ds_trans_type = DEVSTAT_FREE;
1176264274Smav
1177267515Smav	DPRINTF("UNMAP\n");
1178264274Smav
1179264274Smav	be_lun->unmap(be_lun, beio);
1180264274Smav}
1181264274Smav
1182264274Smavstatic void
1183264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio)
1184264274Smav{
1185264274Smav	union ctl_io *io;
1186264274Smav
1187264274Smav	io = beio->io;
1188264274Smav	ctl_free_beio(beio);
1189264274Smav	ctl_config_write_done(io);
1190264274Smav}
1191264274Smav
1192264274Smavstatic void
1193229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1194229997Sken			 union ctl_io *io)
1195229997Sken{
1196229997Sken	struct ctl_be_block_io *beio;
1197229997Sken	struct ctl_be_block_softc *softc;
1198229997Sken
1199229997Sken	DPRINTF("entered\n");
1200229997Sken
1201229997Sken	softc = be_lun->softc;
1202229997Sken	beio = ctl_alloc_beio(softc);
1203229997Sken	beio->io = io;
1204229997Sken	beio->lun = be_lun;
1205264274Smav	beio->beio_cont = ctl_be_block_cw_done;
1206267519Smav	PRIV(io)->ptr = (void *)beio;
1207229997Sken
1208229997Sken	switch (io->scsiio.cdb[0]) {
1209229997Sken	case SYNCHRONIZE_CACHE:
1210229997Sken	case SYNCHRONIZE_CACHE_16:
1211249194Strasz		beio->bio_cmd = BIO_FLUSH;
1212229997Sken		beio->ds_trans_type = DEVSTAT_NO_DATA;
1213229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1214229997Sken		beio->io_len = 0;
1215229997Sken		be_lun->lun_flush(be_lun, beio);
1216229997Sken		break;
1217264274Smav	case WRITE_SAME_10:
1218264274Smav	case WRITE_SAME_16:
1219264274Smav		ctl_be_block_cw_dispatch_ws(be_lun, io);
1220264274Smav		break;
1221264274Smav	case UNMAP:
1222264274Smav		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1223264274Smav		break;
1224229997Sken	default:
1225229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1226229997Sken		break;
1227229997Sken	}
1228229997Sken}
1229229997Sken
1230258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1231258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1232258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1233258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1234229997Sken
1235229997Skenstatic void
1236264886Smavctl_be_block_next(struct ctl_be_block_io *beio)
1237264886Smav{
1238264886Smav	struct ctl_be_block_lun *be_lun;
1239264886Smav	union ctl_io *io;
1240264886Smav
1241264886Smav	io = beio->io;
1242264886Smav	be_lun = beio->lun;
1243264886Smav	ctl_free_beio(beio);
1244267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1245267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1246267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1247267537Smav		ctl_data_submit_done(io);
1248264886Smav		return;
1249264886Smav	}
1250264886Smav
1251264886Smav	io->io_hdr.status &= ~CTL_STATUS_MASK;
1252264886Smav	io->io_hdr.status |= CTL_STATUS_NONE;
1253264886Smav
1254267877Smav	mtx_lock(&be_lun->queue_lock);
1255264886Smav	/*
1256264886Smav	 * XXX KDM make sure that links is okay to use at this point.
1257264886Smav	 * Otherwise, we either need to add another field to ctl_io_hdr,
1258264886Smav	 * or deal with resource allocation here.
1259264886Smav	 */
1260264886Smav	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1261267877Smav	mtx_unlock(&be_lun->queue_lock);
1262264886Smav
1263264886Smav	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1264264886Smav}
1265264886Smav
1266264886Smavstatic void
1267229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1268229997Sken			   union ctl_io *io)
1269229997Sken{
1270229997Sken	struct ctl_be_block_io *beio;
1271229997Sken	struct ctl_be_block_softc *softc;
1272267537Smav	struct ctl_lba_len_flags *lbalen;
1273267519Smav	struct ctl_ptr_len_flags *bptrlen;
1274267519Smav	uint64_t len_left, lbas;
1275229997Sken	int i;
1276229997Sken
1277229997Sken	softc = be_lun->softc;
1278229997Sken
1279229997Sken	DPRINTF("entered\n");
1280229997Sken
1281267537Smav	lbalen = ARGS(io);
1282267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1283267537Smav		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1284267537Smav	} else {
1285229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1286229997Sken	}
1287229997Sken
1288229997Sken	beio = ctl_alloc_beio(softc);
1289229997Sken	beio->io = io;
1290229997Sken	beio->lun = be_lun;
1291267519Smav	bptrlen = PRIV(io);
1292267519Smav	bptrlen->ptr = (void *)beio;
1293229997Sken
1294229997Sken	switch (io->scsiio.tag_type) {
1295229997Sken	case CTL_TAG_ORDERED:
1296229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1297229997Sken		break;
1298229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1299229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1300229997Sken		break;
1301229997Sken	case CTL_TAG_UNTAGGED:
1302229997Sken	case CTL_TAG_SIMPLE:
1303229997Sken	case CTL_TAG_ACA:
1304229997Sken	default:
1305229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1306229997Sken		break;
1307229997Sken	}
1308229997Sken
1309267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1310267537Smav		beio->bio_cmd = BIO_WRITE;
1311267537Smav		beio->ds_trans_type = DEVSTAT_WRITE;
1312267537Smav	} else {
1313229997Sken		beio->bio_cmd = BIO_READ;
1314229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1315229997Sken	}
1316229997Sken
1317264886Smav	DPRINTF("%s at LBA %jx len %u @%ju\n",
1318229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1319267519Smav	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1320267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1321267537Smav		lbas = CTLBLK_HALF_IO_SIZE;
1322267537Smav	else
1323267537Smav		lbas = CTLBLK_MAX_IO_SIZE;
1324267537Smav	lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1325267519Smav	beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1326267519Smav	beio->io_len = lbas * be_lun->blocksize;
1327267519Smav	bptrlen->len += lbas;
1328229997Sken
1329264886Smav	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1330264886Smav		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1331264886Smav		    i, CTLBLK_MAX_SEGS));
1332229997Sken
1333229997Sken		/*
1334229997Sken		 * Setup the S/G entry for this chunk.
1335229997Sken		 */
1336264886Smav		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1337229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1338229997Sken
1339229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1340229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1341229997Sken
1342267537Smav		/* Set up second segment for compare operation. */
1343267537Smav		if (lbalen->flags & CTL_LLF_COMPARE) {
1344267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1345267537Smav			    beio->sg_segs[i].len;
1346267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1347267537Smav			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
1348267537Smav		}
1349267537Smav
1350229997Sken		beio->num_segs++;
1351229997Sken		len_left -= beio->sg_segs[i].len;
1352229997Sken	}
1353267519Smav	if (bptrlen->len < lbalen->len)
1354264886Smav		beio->beio_cont = ctl_be_block_next;
1355264886Smav	io->scsiio.be_move_done = ctl_be_block_move_done;
1356267537Smav	/* For compare we have separate S/G lists for read and datamove. */
1357267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1358267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1359267537Smav	else
1360267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1361264886Smav	io->scsiio.kern_data_len = beio->io_len;
1362264886Smav	io->scsiio.kern_data_resid = 0;
1363264886Smav	io->scsiio.kern_sg_entries = beio->num_segs;
1364264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1365229997Sken
1366229997Sken	/*
1367229997Sken	 * For the read case, we need to read the data into our buffers and
1368229997Sken	 * then we can send it back to the user.  For the write case, we
1369229997Sken	 * need to get the data from the user first.
1370229997Sken	 */
1371229997Sken	if (beio->bio_cmd == BIO_READ) {
1372229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1373229997Sken		be_lun->dispatch(be_lun, beio);
1374229997Sken	} else {
1375229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1376229997Sken#ifdef CTL_TIME_IO
1377229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1378229997Sken#endif
1379229997Sken		ctl_datamove(io);
1380229997Sken	}
1381229997Sken}
1382229997Sken
1383229997Skenstatic void
1384229997Skenctl_be_block_worker(void *context, int pending)
1385229997Sken{
1386229997Sken	struct ctl_be_block_lun *be_lun;
1387229997Sken	struct ctl_be_block_softc *softc;
1388229997Sken	union ctl_io *io;
1389229997Sken
1390229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1391229997Sken	softc = be_lun->softc;
1392229997Sken
1393229997Sken	DPRINTF("entered\n");
1394229997Sken
1395267877Smav	mtx_lock(&be_lun->queue_lock);
1396229997Sken	for (;;) {
1397229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1398229997Sken		if (io != NULL) {
1399229997Sken			struct ctl_be_block_io *beio;
1400229997Sken
1401229997Sken			DPRINTF("datamove queue\n");
1402229997Sken
1403229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1404229997Sken				      ctl_io_hdr, links);
1405229997Sken
1406267877Smav			mtx_unlock(&be_lun->queue_lock);
1407229997Sken
1408267519Smav			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1409229997Sken
1410229997Sken			be_lun->dispatch(be_lun, beio);
1411229997Sken
1412267877Smav			mtx_lock(&be_lun->queue_lock);
1413229997Sken			continue;
1414229997Sken		}
1415229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1416229997Sken		if (io != NULL) {
1417229997Sken
1418229997Sken			DPRINTF("config write queue\n");
1419229997Sken
1420229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1421229997Sken				      ctl_io_hdr, links);
1422229997Sken
1423267877Smav			mtx_unlock(&be_lun->queue_lock);
1424229997Sken
1425229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1426229997Sken
1427267877Smav			mtx_lock(&be_lun->queue_lock);
1428229997Sken			continue;
1429229997Sken		}
1430229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1431229997Sken		if (io != NULL) {
1432229997Sken			DPRINTF("input queue\n");
1433229997Sken
1434229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1435229997Sken				      ctl_io_hdr, links);
1436267877Smav			mtx_unlock(&be_lun->queue_lock);
1437229997Sken
1438229997Sken			/*
1439229997Sken			 * We must drop the lock, since this routine and
1440229997Sken			 * its children may sleep.
1441229997Sken			 */
1442229997Sken			ctl_be_block_dispatch(be_lun, io);
1443229997Sken
1444267877Smav			mtx_lock(&be_lun->queue_lock);
1445229997Sken			continue;
1446229997Sken		}
1447229997Sken
1448229997Sken		/*
1449229997Sken		 * If we get here, there is no work left in the queues, so
1450229997Sken		 * just break out and let the task queue go to sleep.
1451229997Sken		 */
1452229997Sken		break;
1453229997Sken	}
1454267877Smav	mtx_unlock(&be_lun->queue_lock);
1455229997Sken}
1456229997Sken
1457229997Sken/*
1458229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1459229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1460229997Sken * thread.
1461229997Sken */
1462229997Skenstatic int
1463229997Skenctl_be_block_submit(union ctl_io *io)
1464229997Sken{
1465229997Sken	struct ctl_be_block_lun *be_lun;
1466229997Sken	struct ctl_be_lun *ctl_be_lun;
1467229997Sken
1468229997Sken	DPRINTF("entered\n");
1469229997Sken
1470229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1471229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1472229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1473229997Sken
1474229997Sken	/*
1475229997Sken	 * Make sure we only get SCSI I/O.
1476229997Sken	 */
1477229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1478229997Sken		"%#x) encountered", io->io_hdr.io_type));
1479229997Sken
1480267519Smav	PRIV(io)->len = 0;
1481267519Smav
1482267877Smav	mtx_lock(&be_lun->queue_lock);
1483229997Sken	/*
1484229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1485229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1486229997Sken	 * or deal with resource allocation here.
1487229997Sken	 */
1488229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1489267877Smav	mtx_unlock(&be_lun->queue_lock);
1490229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1491229997Sken
1492267514Smav	return (CTL_RETVAL_COMPLETE);
1493229997Sken}
1494229997Sken
1495229997Skenstatic int
1496229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1497229997Sken			int flag, struct thread *td)
1498229997Sken{
1499229997Sken	struct ctl_be_block_softc *softc;
1500229997Sken	int error;
1501229997Sken
1502229997Sken	softc = &backend_block_softc;
1503229997Sken
1504229997Sken	error = 0;
1505229997Sken
1506229997Sken	switch (cmd) {
1507229997Sken	case CTL_LUN_REQ: {
1508229997Sken		struct ctl_lun_req *lun_req;
1509229997Sken
1510229997Sken		lun_req = (struct ctl_lun_req *)addr;
1511229997Sken
1512229997Sken		switch (lun_req->reqtype) {
1513229997Sken		case CTL_LUNREQ_CREATE:
1514229997Sken			error = ctl_be_block_create(softc, lun_req);
1515229997Sken			break;
1516229997Sken		case CTL_LUNREQ_RM:
1517229997Sken			error = ctl_be_block_rm(softc, lun_req);
1518229997Sken			break;
1519232604Strasz		case CTL_LUNREQ_MODIFY:
1520232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1521232604Strasz			break;
1522229997Sken		default:
1523229997Sken			lun_req->status = CTL_LUN_ERROR;
1524229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1525272911Smav				 "invalid LUN request type %d",
1526229997Sken				 lun_req->reqtype);
1527229997Sken			break;
1528229997Sken		}
1529229997Sken		break;
1530229997Sken	}
1531229997Sken	default:
1532229997Sken		error = ENOTTY;
1533229997Sken		break;
1534229997Sken	}
1535229997Sken
1536229997Sken	return (error);
1537229997Sken}
1538229997Sken
1539229997Skenstatic int
1540229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1541229997Sken{
1542229997Sken	struct ctl_be_block_filedata *file_data;
1543229997Sken	struct ctl_lun_create_params *params;
1544229997Sken	struct vattr		      vattr;
1545273029Smav	off_t			      pss;
1546229997Sken	int			      error;
1547229997Sken
1548229997Sken	error = 0;
1549229997Sken	file_data = &be_lun->backend.file;
1550272911Smav	params = &be_lun->params;
1551229997Sken
1552229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1553229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1554229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1555229997Sken
1556229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1557229997Sken	if (error != 0) {
1558229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1559229997Sken			 "error calling VOP_GETATTR() for file %s",
1560229997Sken			 be_lun->dev_path);
1561229997Sken		return (error);
1562229997Sken	}
1563229997Sken
1564229997Sken	/*
1565229997Sken	 * Verify that we have the ability to upgrade to exclusive
1566229997Sken	 * access on this file so we can trap errors at open instead
1567229997Sken	 * of reporting them during first access.
1568229997Sken	 */
1569229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1570229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1571229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1572229997Sken			error = EBADF;
1573229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1574229997Sken				 "error locking file %s", be_lun->dev_path);
1575229997Sken			return (error);
1576229997Sken		}
1577229997Sken	}
1578229997Sken
1579229997Sken
1580229997Sken	file_data->cred = crhold(curthread->td_ucred);
1581232604Strasz	if (params->lun_size_bytes != 0)
1582232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1583232604Strasz	else
1584232604Strasz		be_lun->size_bytes = vattr.va_size;
1585229997Sken	/*
1586229997Sken	 * We set the multi thread flag for file operations because all
1587229997Sken	 * filesystems (in theory) are capable of allowing multiple readers
1588229997Sken	 * of a file at once.  So we want to get the maximum possible
1589229997Sken	 * concurrency.
1590229997Sken	 */
1591229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1592229997Sken
1593229997Sken	/*
1594273029Smav	 * For files we can use any logical block size.  Prefer 512 bytes
1595273029Smav	 * for compatibility reasons.  If file's vattr.va_blocksize
1596273029Smav	 * (preferred I/O block size) is bigger and multiple to chosen
1597273029Smav	 * logical block size -- report it as physical block size.
1598229997Sken	 */
1599229997Sken	if (params->blocksize_bytes != 0)
1600229997Sken		be_lun->blocksize = params->blocksize_bytes;
1601229997Sken	else
1602229997Sken		be_lun->blocksize = 512;
1603273029Smav	pss = vattr.va_blocksize / be_lun->blocksize;
1604273029Smav	if ((pss > 0) && (pss * be_lun->blocksize == vattr.va_blocksize) &&
1605273029Smav	    ((pss & (pss - 1)) == 0)) {
1606273029Smav		be_lun->pblockexp = fls(pss) - 1;
1607273029Smav		be_lun->pblockoff = 0;
1608273029Smav	}
1609229997Sken
1610229997Sken	/*
1611229997Sken	 * Sanity check.  The media size has to be at least one
1612229997Sken	 * sector long.
1613229997Sken	 */
1614229997Sken	if (be_lun->size_bytes < be_lun->blocksize) {
1615229997Sken		error = EINVAL;
1616229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1617229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1618229997Sken			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1619229997Sken	}
1620229997Sken	return (error);
1621229997Sken}
1622229997Sken
1623229997Skenstatic int
1624229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1625229997Sken{
1626229997Sken	struct ctl_lun_create_params *params;
1627229997Sken	struct vattr		      vattr;
1628229997Sken	struct cdev		     *dev;
1629229997Sken	struct cdevsw		     *devsw;
1630229997Sken	int			      error;
1631264191Smav	off_t			      ps, pss, po, pos;
1632229997Sken
1633272911Smav	params = &be_lun->params;
1634229997Sken
1635229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1636229997Sken	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1637229997Sken	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1638229997Sken					     &be_lun->backend.dev.dev_ref);
1639229997Sken	if (be_lun->backend.dev.csw == NULL)
1640229997Sken		panic("Unable to retrieve device switch");
1641269123Smav	if (strcmp(be_lun->backend.dev.csw->d_name, "zvol") == 0)
1642269123Smav		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1643269123Smav	else
1644269123Smav		be_lun->dispatch = ctl_be_block_dispatch_dev;
1645269123Smav	be_lun->lun_flush = ctl_be_block_flush_dev;
1646269123Smav	be_lun->unmap = ctl_be_block_unmap_dev;
1647229997Sken
1648229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1649229997Sken	if (error) {
1650229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1651272911Smav			 "error getting vnode attributes for device %s",
1652272911Smav			 be_lun->dev_path);
1653229997Sken		return (error);
1654229997Sken	}
1655229997Sken
1656229997Sken	dev = be_lun->vn->v_rdev;
1657229997Sken	devsw = dev->si_devsw;
1658229997Sken	if (!devsw->d_ioctl) {
1659229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1660272911Smav			 "no d_ioctl for device %s!",
1661229997Sken			 be_lun->dev_path);
1662229997Sken		return (ENODEV);
1663229997Sken	}
1664229997Sken
1665229997Sken	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1666229997Sken			       (caddr_t)&be_lun->blocksize, FREAD,
1667229997Sken			       curthread);
1668229997Sken	if (error) {
1669229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1670272911Smav			 "error %d returned for DIOCGSECTORSIZE ioctl "
1671272911Smav			 "on %s!", error, be_lun->dev_path);
1672229997Sken		return (error);
1673229997Sken	}
1674229997Sken
1675229997Sken	/*
1676229997Sken	 * If the user has asked for a blocksize that is greater than the
1677229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1678229997Sken	 * the user is asking for is an even multiple of the underlying
1679229997Sken	 * device's blocksize.
1680229997Sken	 */
1681229997Sken	if ((params->blocksize_bytes != 0)
1682229997Sken	 && (params->blocksize_bytes > be_lun->blocksize)) {
1683229997Sken		uint32_t bs_multiple, tmp_blocksize;
1684229997Sken
1685229997Sken		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1686229997Sken
1687229997Sken		tmp_blocksize = bs_multiple * be_lun->blocksize;
1688229997Sken
1689229997Sken		if (tmp_blocksize == params->blocksize_bytes) {
1690229997Sken			be_lun->blocksize = params->blocksize_bytes;
1691229997Sken		} else {
1692229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1693272911Smav				 "requested blocksize %u is not an even "
1694229997Sken				 "multiple of backing device blocksize %u",
1695272911Smav				 params->blocksize_bytes,
1696229997Sken				 be_lun->blocksize);
1697229997Sken			return (EINVAL);
1698229997Sken
1699229997Sken		}
1700229997Sken	} else if ((params->blocksize_bytes != 0)
1701229997Sken		&& (params->blocksize_bytes != be_lun->blocksize)) {
1702229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1703272911Smav			 "requested blocksize %u < backing device "
1704272911Smav			 "blocksize %u", params->blocksize_bytes,
1705229997Sken			 be_lun->blocksize);
1706229997Sken		return (EINVAL);
1707229997Sken	}
1708229997Sken
1709229997Sken	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1710229997Sken			       (caddr_t)&be_lun->size_bytes, FREAD,
1711229997Sken			       curthread);
1712229997Sken	if (error) {
1713229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1714272911Smav			 "error %d returned for DIOCGMEDIASIZE "
1715272911Smav			 " ioctl on %s!", error,
1716232604Strasz			 be_lun->dev_path);
1717229997Sken		return (error);
1718229997Sken	}
1719229997Sken
1720232604Strasz	if (params->lun_size_bytes != 0) {
1721232604Strasz		if (params->lun_size_bytes > be_lun->size_bytes) {
1722232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1723272911Smav				 "requested LUN size %ju > backing device "
1724272911Smav				 "size %ju",
1725232604Strasz				 (uintmax_t)params->lun_size_bytes,
1726232604Strasz				 (uintmax_t)be_lun->size_bytes);
1727232604Strasz			return (EINVAL);
1728232604Strasz		}
1729232604Strasz
1730232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1731232604Strasz	}
1732232604Strasz
1733264191Smav	error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1734264191Smav			       (caddr_t)&ps, FREAD, curthread);
1735264191Smav	if (error)
1736264191Smav		ps = po = 0;
1737264191Smav	else {
1738264191Smav		error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1739264191Smav				       (caddr_t)&po, FREAD, curthread);
1740264191Smav		if (error)
1741264191Smav			po = 0;
1742264191Smav	}
1743264191Smav	pss = ps / be_lun->blocksize;
1744264191Smav	pos = po / be_lun->blocksize;
1745264191Smav	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1746264191Smav	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1747264191Smav		be_lun->pblockexp = fls(pss) - 1;
1748264191Smav		be_lun->pblockoff = (pss - pos) % pss;
1749264191Smav	}
1750264191Smav
1751229997Sken	return (0);
1752229997Sken}
1753229997Sken
1754229997Skenstatic int
1755229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
1756229997Sken{
1757229997Sken	DROP_GIANT();
1758229997Sken	if (be_lun->vn) {
1759229997Sken		int flags = FREAD | FWRITE;
1760229997Sken
1761229997Sken		switch (be_lun->dev_type) {
1762229997Sken		case CTL_BE_BLOCK_DEV:
1763229997Sken			if (be_lun->backend.dev.csw) {
1764229997Sken				dev_relthread(be_lun->backend.dev.cdev,
1765229997Sken					      be_lun->backend.dev.dev_ref);
1766229997Sken				be_lun->backend.dev.csw  = NULL;
1767229997Sken				be_lun->backend.dev.cdev = NULL;
1768229997Sken			}
1769229997Sken			break;
1770229997Sken		case CTL_BE_BLOCK_FILE:
1771229997Sken			break;
1772229997Sken		case CTL_BE_BLOCK_NONE:
1773258871Strasz			break;
1774229997Sken		default:
1775229997Sken			panic("Unexpected backend type.");
1776229997Sken			break;
1777229997Sken		}
1778229997Sken
1779229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1780229997Sken		be_lun->vn = NULL;
1781229997Sken
1782229997Sken		switch (be_lun->dev_type) {
1783229997Sken		case CTL_BE_BLOCK_DEV:
1784229997Sken			break;
1785229997Sken		case CTL_BE_BLOCK_FILE:
1786229997Sken			if (be_lun->backend.file.cred != NULL) {
1787229997Sken				crfree(be_lun->backend.file.cred);
1788229997Sken				be_lun->backend.file.cred = NULL;
1789229997Sken			}
1790229997Sken			break;
1791229997Sken		case CTL_BE_BLOCK_NONE:
1792258871Strasz			break;
1793229997Sken		default:
1794229997Sken			panic("Unexpected backend type.");
1795229997Sken			break;
1796229997Sken		}
1797272911Smav		be_lun->dev_type = CTL_BE_BLOCK_NONE;
1798229997Sken	}
1799229997Sken	PICKUP_GIANT();
1800229997Sken
1801229997Sken	return (0);
1802229997Sken}
1803229997Sken
1804229997Skenstatic int
1805229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
1806229997Sken		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1807229997Sken{
1808229997Sken	struct nameidata nd;
1809229997Sken	int		 flags;
1810229997Sken	int		 error;
1811229997Sken
1812229997Sken	/*
1813229997Sken	 * XXX KDM allow a read-only option?
1814229997Sken	 */
1815229997Sken	flags = FREAD | FWRITE;
1816229997Sken	error = 0;
1817229997Sken
1818229997Sken	if (rootvnode == NULL) {
1819229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1820272911Smav			 "Root filesystem is not mounted");
1821229997Sken		return (1);
1822229997Sken	}
1823229997Sken
1824229997Sken	if (!curthread->td_proc->p_fd->fd_cdir) {
1825229997Sken		curthread->td_proc->p_fd->fd_cdir = rootvnode;
1826229997Sken		VREF(rootvnode);
1827229997Sken	}
1828229997Sken	if (!curthread->td_proc->p_fd->fd_rdir) {
1829229997Sken		curthread->td_proc->p_fd->fd_rdir = rootvnode;
1830229997Sken		VREF(rootvnode);
1831229997Sken	}
1832229997Sken	if (!curthread->td_proc->p_fd->fd_jdir) {
1833229997Sken		curthread->td_proc->p_fd->fd_jdir = rootvnode;
1834229997Sken		VREF(rootvnode);
1835229997Sken	}
1836229997Sken
1837229997Sken again:
1838229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1839229997Sken	error = vn_open(&nd, &flags, 0, NULL);
1840229997Sken	if (error) {
1841229997Sken		/*
1842229997Sken		 * This is the only reasonable guess we can make as far as
1843229997Sken		 * path if the user doesn't give us a fully qualified path.
1844229997Sken		 * If they want to specify a file, they need to specify the
1845229997Sken		 * full path.
1846229997Sken		 */
1847229997Sken		if (be_lun->dev_path[0] != '/') {
1848229997Sken			char *dev_path = "/dev/";
1849229997Sken			char *dev_name;
1850229997Sken
1851229997Sken			/* Try adding device path at beginning of name */
1852229997Sken			dev_name = malloc(strlen(be_lun->dev_path)
1853229997Sken					+ strlen(dev_path) + 1,
1854229997Sken					  M_CTLBLK, M_WAITOK);
1855229997Sken			if (dev_name) {
1856229997Sken				sprintf(dev_name, "%s%s", dev_path,
1857229997Sken					be_lun->dev_path);
1858229997Sken				free(be_lun->dev_path, M_CTLBLK);
1859229997Sken				be_lun->dev_path = dev_name;
1860229997Sken				goto again;
1861229997Sken			}
1862229997Sken		}
1863229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1864272911Smav		    "error opening %s: %d", be_lun->dev_path, error);
1865229997Sken		return (error);
1866229997Sken	}
1867229997Sken
1868229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
1869229997Sken
1870229997Sken	be_lun->vn = nd.ni_vp;
1871229997Sken
1872229997Sken	/* We only support disks and files. */
1873229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
1874229997Sken		error = ctl_be_block_open_dev(be_lun, req);
1875229997Sken	} else if (be_lun->vn->v_type == VREG) {
1876229997Sken		error = ctl_be_block_open_file(be_lun, req);
1877229997Sken	} else {
1878229997Sken		error = EINVAL;
1879229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1880258871Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
1881229997Sken	}
1882229997Sken	VOP_UNLOCK(be_lun->vn, 0);
1883229997Sken
1884229997Sken	if (error != 0) {
1885229997Sken		ctl_be_block_close(be_lun);
1886229997Sken		return (error);
1887229997Sken	}
1888229997Sken
1889229997Sken	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1890229997Sken	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1891229997Sken
1892229997Sken	return (0);
1893229997Sken}
1894229997Sken
1895229997Skenstatic int
1896229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1897229997Sken{
1898229997Sken	struct ctl_be_block_lun *be_lun;
1899229997Sken	struct ctl_lun_create_params *params;
1900267481Smav	char num_thread_str[16];
1901229997Sken	char tmpstr[32];
1902267481Smav	char *value;
1903264274Smav	int retval, num_threads, unmap;
1904267481Smav	int tmp_num_threads;
1905229997Sken
1906229997Sken	params = &req->reqdata.create;
1907229997Sken	retval = 0;
1908272911Smav	req->status = CTL_LUN_OK;
1909229997Sken
1910229997Sken	num_threads = cbb_num_threads;
1911229997Sken
1912229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1913229997Sken
1914272911Smav	be_lun->params = req->reqdata.create;
1915229997Sken	be_lun->softc = softc;
1916229997Sken	STAILQ_INIT(&be_lun->input_queue);
1917229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
1918229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
1919229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1920267877Smav	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
1921267877Smav	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
1922268280Smav	ctl_init_opts(&be_lun->ctl_be_lun.options,
1923268280Smav	    req->num_be_args, req->kern_be_args);
1924229997Sken
1925264886Smav	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1926256995Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1927229997Sken
1928229997Sken	if (be_lun->lun_zone == NULL) {
1929229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1930272911Smav			 "error allocating UMA zone");
1931229997Sken		goto bailout_error;
1932229997Sken	}
1933229997Sken
1934229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1935229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
1936229997Sken	else
1937229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
1938229997Sken
1939229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1940268280Smav		value = ctl_get_opt(&be_lun->ctl_be_lun.options, "file");
1941267499Smav		if (value == NULL) {
1942229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1943272911Smav				 "no file argument specified");
1944229997Sken			goto bailout_error;
1945229997Sken		}
1946267499Smav		be_lun->dev_path = strdup(value, M_CTLBLK);
1947272911Smav		be_lun->blocksize = 512;
1948272911Smav		be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1949229997Sken
1950229997Sken		retval = ctl_be_block_open(softc, be_lun, req);
1951229997Sken		if (retval != 0) {
1952229997Sken			retval = 0;
1953272911Smav			req->status = CTL_LUN_WARNING;
1954229997Sken		}
1955229997Sken	} else {
1956229997Sken		/*
1957229997Sken		 * For processor devices, we don't have any size.
1958229997Sken		 */
1959229997Sken		be_lun->blocksize = 0;
1960264191Smav		be_lun->pblockexp = 0;
1961264191Smav		be_lun->pblockoff = 0;
1962229997Sken		be_lun->size_blocks = 0;
1963229997Sken		be_lun->size_bytes = 0;
1964229997Sken		be_lun->ctl_be_lun.maxlba = 0;
1965229997Sken
1966229997Sken		/*
1967229997Sken		 * Default to just 1 thread for processor devices.
1968229997Sken		 */
1969229997Sken		num_threads = 1;
1970229997Sken	}
1971229997Sken
1972229997Sken	/*
1973229997Sken	 * XXX This searching loop might be refactored to be combined with
1974229997Sken	 * the loop above,
1975229997Sken	 */
1976268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "num_threads");
1977267481Smav	if (value != NULL) {
1978267481Smav		tmp_num_threads = strtol(value, NULL, 0);
1979229997Sken
1980267481Smav		/*
1981267481Smav		 * We don't let the user specify less than one
1982267481Smav		 * thread, but hope he's clueful enough not to
1983267481Smav		 * specify 1000 threads.
1984267481Smav		 */
1985267481Smav		if (tmp_num_threads < 1) {
1986267481Smav			snprintf(req->error_str, sizeof(req->error_str),
1987272911Smav				 "invalid number of threads %s",
1988272911Smav				 num_thread_str);
1989267481Smav			goto bailout_error;
1990229997Sken		}
1991267481Smav		num_threads = tmp_num_threads;
1992229997Sken	}
1993267481Smav	unmap = 0;
1994268280Smav	value = ctl_get_opt(&be_lun->ctl_be_lun.options, "unmap");
1995267481Smav	if (value != NULL && strcmp(value, "on") == 0)
1996267481Smav		unmap = 1;
1997229997Sken
1998229997Sken	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
1999229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
2000272911Smav	if (be_lun->vn == NULL)
2001272911Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_OFFLINE;
2002264274Smav	if (unmap)
2003264274Smav		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
2004229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
2005272911Smav	be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2006272911Smav	    0 : (be_lun->size_blocks - 1);
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;
2010272911Smav	if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2011272911Smav	    be_lun->blocksize != 0)
2012272911Smav		be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2013272911Smav		    be_lun->blocksize;
2014229997Sken	/* Tell the user the blocksize we ended up using */
2015272911Smav	params->lun_size_bytes = be_lun->size_bytes;
2016229997Sken	params->blocksize_bytes = be_lun->blocksize;
2017229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2018229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
2019229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
2020229997Sken	} else
2021229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
2022229997Sken
2023229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
2024229997Sken	be_lun->ctl_be_lun.lun_config_status =
2025229997Sken		ctl_be_block_lun_config_status;
2026229997Sken	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
2027229997Sken
2028229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2029229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2030229997Sken			 softc->num_luns);
2031229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
2032229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2033229997Sken			sizeof(tmpstr)));
2034229997Sken
2035229997Sken		/* Tell the user what we used for a serial number */
2036229997Sken		strncpy((char *)params->serial_num, tmpstr,
2037229997Sken			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
2038229997Sken	} else {
2039229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
2040229997Sken			params->serial_num,
2041229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
2042229997Sken			sizeof(params->serial_num)));
2043229997Sken	}
2044229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2045229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2046229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
2047229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2048229997Sken			sizeof(tmpstr)));
2049229997Sken
2050229997Sken		/* Tell the user what we used for a device ID */
2051229997Sken		strncpy((char *)params->device_id, tmpstr,
2052229997Sken			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2053229997Sken	} else {
2054229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
2055229997Sken			params->device_id,
2056229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2057229997Sken				sizeof(params->device_id)));
2058229997Sken	}
2059229997Sken
2060229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2061229997Sken
2062229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2063229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2064229997Sken
2065229997Sken	if (be_lun->io_taskqueue == NULL) {
2066229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2067272911Smav			 "unable to create taskqueue");
2068229997Sken		goto bailout_error;
2069229997Sken	}
2070229997Sken
2071229997Sken	/*
2072229997Sken	 * Note that we start the same number of threads by default for
2073229997Sken	 * both the file case and the block device case.  For the file
2074229997Sken	 * case, we need multiple threads to allow concurrency, because the
2075229997Sken	 * vnode interface is designed to be a blocking interface.  For the
2076229997Sken	 * block device case, ZFS zvols at least will block the caller's
2077229997Sken	 * context in many instances, and so we need multiple threads to
2078229997Sken	 * overcome that problem.  Other block devices don't need as many
2079229997Sken	 * threads, but they shouldn't cause too many problems.
2080229997Sken	 *
2081229997Sken	 * If the user wants to just have a single thread for a block
2082229997Sken	 * device, he can specify that when the LUN is created, or change
2083229997Sken	 * the tunable/sysctl to alter the default number of threads.
2084229997Sken	 */
2085229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2086229997Sken					 /*num threads*/num_threads,
2087229997Sken					 /*priority*/PWAIT,
2088229997Sken					 /*thread name*/
2089229997Sken					 "%s taskq", be_lun->lunname);
2090229997Sken
2091229997Sken	if (retval != 0)
2092229997Sken		goto bailout_error;
2093229997Sken
2094229997Sken	be_lun->num_threads = num_threads;
2095229997Sken
2096229997Sken	mtx_lock(&softc->lock);
2097229997Sken	softc->num_luns++;
2098229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2099229997Sken
2100229997Sken	mtx_unlock(&softc->lock);
2101229997Sken
2102229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
2103229997Sken	if (retval != 0) {
2104229997Sken		mtx_lock(&softc->lock);
2105229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2106229997Sken			      links);
2107229997Sken		softc->num_luns--;
2108229997Sken		mtx_unlock(&softc->lock);
2109229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2110272911Smav			 "ctl_add_lun() returned error %d, see dmesg for "
2111272911Smav			 "details", retval);
2112229997Sken		retval = 0;
2113229997Sken		goto bailout_error;
2114229997Sken	}
2115229997Sken
2116229997Sken	mtx_lock(&softc->lock);
2117229997Sken
2118229997Sken	/*
2119229997Sken	 * Tell the config_status routine that we're waiting so it won't
2120229997Sken	 * clean up the LUN in the event of an error.
2121229997Sken	 */
2122229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2123229997Sken
2124229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2125229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2126229997Sken		if (retval == EINTR)
2127229997Sken			break;
2128229997Sken	}
2129229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2130229997Sken
2131229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2132229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2133272911Smav			 "LUN configuration error, see dmesg for details");
2134229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2135229997Sken			      links);
2136229997Sken		softc->num_luns--;
2137229997Sken		mtx_unlock(&softc->lock);
2138229997Sken		goto bailout_error;
2139229997Sken	} else {
2140229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2141229997Sken	}
2142229997Sken
2143229997Sken	mtx_unlock(&softc->lock);
2144229997Sken
2145229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2146229997Sken					       be_lun->blocksize,
2147229997Sken					       DEVSTAT_ALL_SUPPORTED,
2148229997Sken					       be_lun->ctl_be_lun.lun_type
2149229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
2150229997Sken					       DEVSTAT_PRIORITY_OTHER);
2151229997Sken
2152229997Sken	return (retval);
2153229997Sken
2154229997Skenbailout_error:
2155229997Sken	req->status = CTL_LUN_ERROR;
2156229997Sken
2157267429Smav	if (be_lun->io_taskqueue != NULL)
2158267429Smav		taskqueue_free(be_lun->io_taskqueue);
2159229997Sken	ctl_be_block_close(be_lun);
2160267429Smav	if (be_lun->dev_path != NULL)
2161267429Smav		free(be_lun->dev_path, M_CTLBLK);
2162267429Smav	if (be_lun->lun_zone != NULL)
2163267429Smav		uma_zdestroy(be_lun->lun_zone);
2164268280Smav	ctl_free_opts(&be_lun->ctl_be_lun.options);
2165267877Smav	mtx_destroy(&be_lun->queue_lock);
2166267877Smav	mtx_destroy(&be_lun->io_lock);
2167229997Sken	free(be_lun, M_CTLBLK);
2168229997Sken
2169229997Sken	return (retval);
2170229997Sken}
2171229997Sken
2172229997Skenstatic int
2173229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2174229997Sken{
2175229997Sken	struct ctl_lun_rm_params *params;
2176229997Sken	struct ctl_be_block_lun *be_lun;
2177229997Sken	int retval;
2178229997Sken
2179229997Sken	params = &req->reqdata.rm;
2180229997Sken
2181229997Sken	mtx_lock(&softc->lock);
2182229997Sken
2183229997Sken	be_lun = NULL;
2184229997Sken
2185229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2186229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2187229997Sken			break;
2188229997Sken	}
2189229997Sken	mtx_unlock(&softc->lock);
2190229997Sken
2191229997Sken	if (be_lun == NULL) {
2192229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2193272911Smav			 "LUN %u is not managed by the block backend",
2194272911Smav			 params->lun_id);
2195229997Sken		goto bailout_error;
2196229997Sken	}
2197229997Sken
2198229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2199229997Sken
2200229997Sken	if (retval != 0) {
2201229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2202272911Smav			 "error %d returned from ctl_disable_lun() for "
2203272911Smav			 "LUN %d", retval, params->lun_id);
2204229997Sken		goto bailout_error;
2205229997Sken
2206229997Sken	}
2207229997Sken
2208229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2209229997Sken	if (retval != 0) {
2210229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2211272911Smav			 "error %d returned from ctl_invalidate_lun() for "
2212272911Smav			 "LUN %d", retval, params->lun_id);
2213229997Sken		goto bailout_error;
2214229997Sken	}
2215229997Sken
2216229997Sken	mtx_lock(&softc->lock);
2217229997Sken
2218229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2219229997Sken
2220229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2221229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2222229997Sken                if (retval == EINTR)
2223229997Sken                        break;
2224229997Sken        }
2225229997Sken
2226229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2227229997Sken
2228229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2229229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2230272911Smav			 "interrupted waiting for LUN to be freed");
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;
2274272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2275232604Strasz
2276232604Strasz	if (params->lun_size_bytes != 0) {
2277232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2278232604Strasz	} else  {
2279271794Smav		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2280232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2281271794Smav		VOP_UNLOCK(be_lun->vn, 0);
2282232604Strasz		if (error != 0) {
2283232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2284232604Strasz				 "error calling VOP_GETATTR() for file %s",
2285232604Strasz				 be_lun->dev_path);
2286232604Strasz			return (error);
2287232604Strasz		}
2288232604Strasz
2289232604Strasz		be_lun->size_bytes = vattr.va_size;
2290232604Strasz	}
2291232604Strasz
2292232604Strasz	return (0);
2293232604Strasz}
2294232604Strasz
2295232604Straszstatic int
2296232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2297232604Strasz			struct ctl_lun_req *req)
2298232604Strasz{
2299271794Smav	struct ctl_be_block_devdata *dev_data;
2300232604Strasz	int error;
2301272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2302232604Strasz	uint64_t size_bytes;
2303232604Strasz
2304271794Smav	dev_data = &be_lun->backend.dev;
2305271794Smav	if (!dev_data->csw->d_ioctl) {
2306232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2307272911Smav			 "no d_ioctl for device %s!", be_lun->dev_path);
2308232604Strasz		return (ENODEV);
2309232604Strasz	}
2310232604Strasz
2311271794Smav	error = dev_data->csw->d_ioctl(dev_data->cdev, DIOCGMEDIASIZE,
2312232604Strasz			       (caddr_t)&size_bytes, FREAD,
2313232604Strasz			       curthread);
2314232604Strasz	if (error) {
2315232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2316272911Smav			 "error %d returned for DIOCGMEDIASIZE ioctl "
2317272911Smav			 "on %s!", error, be_lun->dev_path);
2318232604Strasz		return (error);
2319232604Strasz	}
2320232604Strasz
2321232604Strasz	if (params->lun_size_bytes != 0) {
2322232604Strasz		if (params->lun_size_bytes > size_bytes) {
2323232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2324272911Smav				 "requested LUN size %ju > backing device "
2325272911Smav				 "size %ju",
2326232604Strasz				 (uintmax_t)params->lun_size_bytes,
2327232604Strasz				 (uintmax_t)size_bytes);
2328232604Strasz			return (EINVAL);
2329232604Strasz		}
2330232604Strasz
2331232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2332232604Strasz	} else {
2333232604Strasz		be_lun->size_bytes = size_bytes;
2334232604Strasz	}
2335232604Strasz
2336232604Strasz	return (0);
2337232604Strasz}
2338232604Strasz
2339232604Straszstatic int
2340232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2341232604Strasz{
2342232604Strasz	struct ctl_lun_modify_params *params;
2343232604Strasz	struct ctl_be_block_lun *be_lun;
2344271794Smav	uint64_t oldsize;
2345241896Skib	int error;
2346232604Strasz
2347232604Strasz	params = &req->reqdata.modify;
2348232604Strasz
2349232604Strasz	mtx_lock(&softc->lock);
2350232604Strasz	be_lun = NULL;
2351232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2352232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2353232604Strasz			break;
2354232604Strasz	}
2355232604Strasz	mtx_unlock(&softc->lock);
2356232604Strasz
2357232604Strasz	if (be_lun == NULL) {
2358232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2359272911Smav			 "LUN %u is not managed by the block backend",
2360272911Smav			 params->lun_id);
2361232604Strasz		goto bailout_error;
2362232604Strasz	}
2363232604Strasz
2364272911Smav	be_lun->params.lun_size_bytes = params->lun_size_bytes;
2365232604Strasz
2366272911Smav	oldsize = be_lun->size_blocks;
2367272911Smav	if (be_lun->vn == NULL)
2368272911Smav		error = ctl_be_block_open(softc, be_lun, req);
2369272911Smav	else if (be_lun->vn->v_type == VREG)
2370232604Strasz		error = ctl_be_block_modify_file(be_lun, req);
2371232604Strasz	else
2372232604Strasz		error = ctl_be_block_modify_dev(be_lun, req);
2373232604Strasz
2374272911Smav	if (error == 0 && be_lun->size_blocks != oldsize) {
2375271794Smav		be_lun->size_blocks = be_lun->size_bytes >>
2376271794Smav		    be_lun->blocksize_shift;
2377232604Strasz
2378271794Smav		/*
2379271794Smav		 * The maximum LBA is the size - 1.
2380271794Smav		 *
2381271794Smav		 * XXX: Note that this field is being updated without locking,
2382271794Smav		 * 	which might cause problems on 32-bit architectures.
2383271794Smav		 */
2384272911Smav		be_lun->ctl_be_lun.maxlba = (be_lun->size_blocks == 0) ?
2385272911Smav		    0 : (be_lun->size_blocks - 1);
2386272911Smav		be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
2387272911Smav		be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
2388272911Smav		be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
2389272911Smav		if (be_lun->dispatch == ctl_be_block_dispatch_zvol &&
2390272911Smav		    be_lun->blocksize != 0)
2391272911Smav			be_lun->ctl_be_lun.atomicblock = CTLBLK_MAX_IO_SIZE /
2392272911Smav			    be_lun->blocksize;
2393271794Smav		ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2394272911Smav		if (oldsize == 0 && be_lun->size_blocks != 0)
2395272911Smav			ctl_lun_online(&be_lun->ctl_be_lun);
2396271794Smav	}
2397232604Strasz
2398232604Strasz	/* Tell the user the exact size we ended up using */
2399232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2400232604Strasz
2401272911Smav	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2402232604Strasz
2403232604Strasz	return (0);
2404232604Strasz
2405232604Straszbailout_error:
2406232604Strasz	req->status = CTL_LUN_ERROR;
2407232604Strasz
2408232604Strasz	return (0);
2409232604Strasz}
2410232604Strasz
2411229997Skenstatic void
2412229997Skenctl_be_block_lun_shutdown(void *be_lun)
2413229997Sken{
2414229997Sken	struct ctl_be_block_lun *lun;
2415229997Sken	struct ctl_be_block_softc *softc;
2416229997Sken
2417229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2418229997Sken
2419229997Sken	softc = lun->softc;
2420229997Sken
2421229997Sken	mtx_lock(&softc->lock);
2422229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2423229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2424229997Sken		wakeup(lun);
2425229997Sken	mtx_unlock(&softc->lock);
2426229997Sken
2427229997Sken}
2428229997Sken
2429229997Skenstatic void
2430229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2431229997Sken{
2432229997Sken	struct ctl_be_block_lun *lun;
2433229997Sken	struct ctl_be_block_softc *softc;
2434229997Sken
2435229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2436229997Sken	softc = lun->softc;
2437229997Sken
2438229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2439229997Sken		mtx_lock(&softc->lock);
2440229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2441229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2442229997Sken			wakeup(lun);
2443229997Sken		mtx_unlock(&softc->lock);
2444229997Sken
2445229997Sken		/*
2446229997Sken		 * We successfully added the LUN, attempt to enable it.
2447229997Sken		 */
2448229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2449229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2450229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2451229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2452229997Sken				       __func__);
2453229997Sken			}
2454229997Sken		}
2455229997Sken
2456229997Sken		return;
2457229997Sken	}
2458229997Sken
2459229997Sken
2460229997Sken	mtx_lock(&softc->lock);
2461229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2462229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2463229997Sken	wakeup(lun);
2464229997Sken	mtx_unlock(&softc->lock);
2465229997Sken}
2466229997Sken
2467229997Sken
2468229997Skenstatic int
2469229997Skenctl_be_block_config_write(union ctl_io *io)
2470229997Sken{
2471229997Sken	struct ctl_be_block_lun *be_lun;
2472229997Sken	struct ctl_be_lun *ctl_be_lun;
2473229997Sken	int retval;
2474229997Sken
2475229997Sken	retval = 0;
2476229997Sken
2477229997Sken	DPRINTF("entered\n");
2478229997Sken
2479229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2480229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2481229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2482229997Sken
2483229997Sken	switch (io->scsiio.cdb[0]) {
2484229997Sken	case SYNCHRONIZE_CACHE:
2485229997Sken	case SYNCHRONIZE_CACHE_16:
2486264274Smav	case WRITE_SAME_10:
2487264274Smav	case WRITE_SAME_16:
2488264274Smav	case UNMAP:
2489229997Sken		/*
2490229997Sken		 * The upper level CTL code will filter out any CDBs with
2491229997Sken		 * the immediate bit set and return the proper error.
2492229997Sken		 *
2493229997Sken		 * We don't really need to worry about what LBA range the
2494229997Sken		 * user asked to be synced out.  When they issue a sync
2495229997Sken		 * cache command, we'll sync out the whole thing.
2496229997Sken		 */
2497267877Smav		mtx_lock(&be_lun->queue_lock);
2498229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2499229997Sken				   links);
2500267877Smav		mtx_unlock(&be_lun->queue_lock);
2501229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2502229997Sken		break;
2503229997Sken	case START_STOP_UNIT: {
2504229997Sken		struct scsi_start_stop_unit *cdb;
2505229997Sken
2506229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2507229997Sken
2508229997Sken		if (cdb->how & SSS_START)
2509229997Sken			retval = ctl_start_lun(ctl_be_lun);
2510229997Sken		else {
2511229997Sken			retval = ctl_stop_lun(ctl_be_lun);
2512229997Sken			/*
2513229997Sken			 * XXX KDM Copan-specific offline behavior.
2514229997Sken			 * Figure out a reasonable way to port this?
2515229997Sken			 */
2516229997Sken#ifdef NEEDTOPORT
2517229997Sken			if ((retval == 0)
2518229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2519229997Sken				retval = ctl_lun_offline(ctl_be_lun);
2520229997Sken#endif
2521229997Sken		}
2522229997Sken
2523229997Sken		/*
2524229997Sken		 * In general, the above routines should not fail.  They
2525229997Sken		 * just set state for the LUN.  So we've got something
2526229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2527229997Sken		 */
2528229997Sken		if (retval != 0) {
2529229997Sken			ctl_set_internal_failure(&io->scsiio,
2530229997Sken						 /*sks_valid*/ 1,
2531229997Sken						 /*retry_count*/ 0xf051);
2532229997Sken			retval = CTL_RETVAL_COMPLETE;
2533229997Sken		} else {
2534229997Sken			ctl_set_success(&io->scsiio);
2535229997Sken		}
2536229997Sken		ctl_config_write_done(io);
2537229997Sken		break;
2538229997Sken	}
2539229997Sken	default:
2540229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2541229997Sken		ctl_config_write_done(io);
2542229997Sken		retval = CTL_RETVAL_COMPLETE;
2543229997Sken		break;
2544229997Sken	}
2545229997Sken
2546229997Sken	return (retval);
2547229997Sken
2548229997Sken}
2549229997Sken
2550229997Skenstatic int
2551229997Skenctl_be_block_config_read(union ctl_io *io)
2552229997Sken{
2553229997Sken	return (0);
2554229997Sken}
2555229997Sken
2556229997Skenstatic int
2557229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2558229997Sken{
2559229997Sken	struct ctl_be_block_lun *lun;
2560229997Sken	int retval;
2561229997Sken
2562229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2563229997Sken	retval = 0;
2564229997Sken
2565268283Smav	retval = sbuf_printf(sb, "\t<num_threads>");
2566229997Sken
2567229997Sken	if (retval != 0)
2568229997Sken		goto bailout;
2569229997Sken
2570229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2571229997Sken
2572229997Sken	if (retval != 0)
2573229997Sken		goto bailout;
2574229997Sken
2575268283Smav	retval = sbuf_printf(sb, "</num_threads>\n");
2576229997Sken
2577229997Skenbailout:
2578229997Sken
2579229997Sken	return (retval);
2580229997Sken}
2581229997Sken
2582229997Skenint
2583229997Skenctl_be_block_init(void)
2584229997Sken{
2585229997Sken	struct ctl_be_block_softc *softc;
2586229997Sken	int retval;
2587229997Sken
2588229997Sken	softc = &backend_block_softc;
2589229997Sken	retval = 0;
2590229997Sken
2591267877Smav	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2592264020Strasz	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2593264020Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2594229997Sken	STAILQ_INIT(&softc->disk_list);
2595229997Sken	STAILQ_INIT(&softc->lun_list);
2596229997Sken
2597229997Sken	return (retval);
2598229997Sken}
2599