ctl_backend_block.c revision 287664
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 287664 2015-09-11 12:50:52Z mav $");
44229997Sken
45229997Sken#include <sys/param.h>
46229997Sken#include <sys/systm.h>
47229997Sken#include <sys/kernel.h>
48229997Sken#include <sys/types.h>
49229997Sken#include <sys/kthread.h>
50229997Sken#include <sys/bio.h>
51229997Sken#include <sys/fcntl.h>
52264274Smav#include <sys/limits.h>
53229997Sken#include <sys/lock.h>
54229997Sken#include <sys/mutex.h>
55229997Sken#include <sys/condvar.h>
56229997Sken#include <sys/malloc.h>
57229997Sken#include <sys/conf.h>
58229997Sken#include <sys/ioccom.h>
59229997Sken#include <sys/queue.h>
60229997Sken#include <sys/sbuf.h>
61229997Sken#include <sys/endian.h>
62229997Sken#include <sys/uio.h>
63229997Sken#include <sys/buf.h>
64229997Sken#include <sys/taskqueue.h>
65229997Sken#include <sys/vnode.h>
66229997Sken#include <sys/namei.h>
67229997Sken#include <sys/mount.h>
68229997Sken#include <sys/disk.h>
69229997Sken#include <sys/fcntl.h>
70229997Sken#include <sys/filedesc.h>
71275474Smav#include <sys/filio.h>
72229997Sken#include <sys/proc.h>
73229997Sken#include <sys/pcpu.h>
74229997Sken#include <sys/module.h>
75229997Sken#include <sys/sdt.h>
76229997Sken#include <sys/devicestat.h>
77229997Sken#include <sys/sysctl.h>
78229997Sken
79229997Sken#include <geom/geom.h>
80229997Sken
81229997Sken#include <cam/cam.h>
82229997Sken#include <cam/scsi/scsi_all.h>
83229997Sken#include <cam/scsi/scsi_da.h>
84229997Sken#include <cam/ctl/ctl_io.h>
85229997Sken#include <cam/ctl/ctl.h>
86229997Sken#include <cam/ctl/ctl_backend.h>
87229997Sken#include <cam/ctl/ctl_ioctl.h>
88287621Smav#include <cam/ctl/ctl_ha.h>
89229997Sken#include <cam/ctl/ctl_scsi_all.h>
90287621Smav#include <cam/ctl/ctl_private.h>
91229997Sken#include <cam/ctl/ctl_error.h>
92229997Sken
93229997Sken/*
94264886Smav * The idea here is that we'll allocate enough S/G space to hold a 1MB
95264886Smav * I/O.  If we get an I/O larger than that, we'll split it.
96229997Sken */
97267537Smav#define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
98267537Smav#define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
99264886Smav#define	CTLBLK_MAX_SEG		MAXPHYS
100267537Smav#define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
101267537Smav#define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
102229997Sken
103229997Sken#ifdef CTLBLK_DEBUG
104229997Sken#define DPRINTF(fmt, args...) \
105229997Sken    printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
106229997Sken#else
107229997Sken#define DPRINTF(fmt, args...) do {} while(0)
108229997Sken#endif
109229997Sken
110267519Smav#define PRIV(io)	\
111267519Smav    ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
112267537Smav#define ARGS(io)	\
113267537Smav    ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
114267519Smav
115229997SkenSDT_PROVIDER_DEFINE(cbb);
116229997Sken
117229997Skentypedef enum {
118229997Sken	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
119229997Sken	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
120229997Sken	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
121229997Sken} ctl_be_block_lun_flags;
122229997Sken
123229997Skentypedef enum {
124229997Sken	CTL_BE_BLOCK_NONE,
125229997Sken	CTL_BE_BLOCK_DEV,
126229997Sken	CTL_BE_BLOCK_FILE
127229997Sken} ctl_be_block_type;
128229997Sken
129229997Skenstruct ctl_be_block_filedata {
130229997Sken	struct ucred *cred;
131229997Sken};
132229997Sken
133229997Skenunion ctl_be_block_bedata {
134229997Sken	struct ctl_be_block_filedata file;
135229997Sken};
136229997Sken
137229997Skenstruct ctl_be_block_io;
138229997Skenstruct ctl_be_block_lun;
139229997Sken
140229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
141229997Sken			       struct ctl_be_block_io *beio);
142274154Smavtypedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
143274154Smav				  const char *attrname);
144229997Sken
145229997Sken/*
146229997Sken * Backend LUN structure.  There is a 1:1 mapping between a block device
147229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN.
148229997Sken */
149229997Skenstruct ctl_be_block_lun {
150272911Smav	struct ctl_lun_create_params params;
151229997Sken	char lunname[32];
152229997Sken	char *dev_path;
153229997Sken	ctl_be_block_type dev_type;
154229997Sken	struct vnode *vn;
155229997Sken	union ctl_be_block_bedata backend;
156229997Sken	cbb_dispatch_t dispatch;
157229997Sken	cbb_dispatch_t lun_flush;
158264274Smav	cbb_dispatch_t unmap;
159275474Smav	cbb_dispatch_t get_lba_status;
160274154Smav	cbb_getattr_t getattr;
161229997Sken	uma_zone_t lun_zone;
162229997Sken	uint64_t size_blocks;
163229997Sken	uint64_t size_bytes;
164229997Sken	struct ctl_be_block_softc *softc;
165229997Sken	struct devstat *disk_stats;
166229997Sken	ctl_be_block_lun_flags flags;
167229997Sken	STAILQ_ENTRY(ctl_be_block_lun) links;
168287499Smav	struct ctl_be_lun cbe_lun;
169229997Sken	struct taskqueue *io_taskqueue;
170229997Sken	struct task io_task;
171229997Sken	int num_threads;
172229997Sken	STAILQ_HEAD(, ctl_io_hdr) input_queue;
173275474Smav	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
174229997Sken	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
175229997Sken	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
176267877Smav	struct mtx_padalign io_lock;
177267877Smav	struct mtx_padalign queue_lock;
178229997Sken};
179229997Sken
180229997Sken/*
181229997Sken * Overall softc structure for the block backend module.
182229997Sken */
183229997Skenstruct ctl_be_block_softc {
184229997Sken	struct mtx			 lock;
185229997Sken	int				 num_luns;
186229997Sken	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
187229997Sken};
188229997Sken
189229997Skenstatic struct ctl_be_block_softc backend_block_softc;
190229997Sken
191229997Sken/*
192229997Sken * Per-I/O information.
193229997Sken */
194229997Skenstruct ctl_be_block_io {
195229997Sken	union ctl_io			*io;
196229997Sken	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
197229997Sken	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
198229997Sken	int				bio_cmd;
199229997Sken	int				num_segs;
200229997Sken	int				num_bios_sent;
201229997Sken	int				num_bios_done;
202229997Sken	int				send_complete;
203229997Sken	int				num_errors;
204229997Sken	struct bintime			ds_t0;
205229997Sken	devstat_tag_type		ds_tag_type;
206229997Sken	devstat_trans_flags		ds_trans_type;
207229997Sken	uint64_t			io_len;
208229997Sken	uint64_t			io_offset;
209286353Smav	int				io_arg;
210229997Sken	struct ctl_be_block_softc	*softc;
211229997Sken	struct ctl_be_block_lun		*lun;
212264274Smav	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
213229997Sken};
214229997Sken
215287621Smavextern struct ctl_softc *control_softc;
216287621Smav
217229997Skenstatic int cbb_num_threads = 14;
218229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
219229997Sken	    "CAM Target Layer Block Backend");
220267992ShselaskySYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
221229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
222229997Sken
223229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
224229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
225229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
226229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
227229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
228229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
229229997Sken				    struct ctl_be_block_io *beio);
230229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
231229997Sken				       struct ctl_be_block_io *beio);
232275474Smavstatic void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
233275474Smav				  struct ctl_be_block_io *beio);
234275481Smavstatic uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
235275481Smav					 const char *attrname);
236229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
237229997Sken				   struct ctl_be_block_io *beio);
238264274Smavstatic void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
239264274Smav				   struct ctl_be_block_io *beio);
240229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
241229997Sken				      struct ctl_be_block_io *beio);
242274154Smavstatic uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
243274154Smav					 const char *attrname);
244275474Smavstatic void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
245275474Smav				    union ctl_io *io);
246229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
247229997Sken				    union ctl_io *io);
248229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
249229997Sken				  union ctl_io *io);
250229997Skenstatic void ctl_be_block_worker(void *context, int pending);
251229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
252229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
253229997Sken				   int flag, struct thread *td);
254229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
255229997Sken				  struct ctl_lun_req *req);
256229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
257229997Sken				 struct ctl_lun_req *req);
258229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
259229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
260229997Sken			     struct ctl_be_block_lun *be_lun,
261229997Sken			     struct ctl_lun_req *req);
262229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
263229997Sken			       struct ctl_lun_req *req);
264229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
265229997Sken			   struct ctl_lun_req *req);
266232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
267232604Strasz				  struct ctl_lun_req *req);
268232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
269232604Strasz				 struct ctl_lun_req *req);
270232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
271232604Strasz			   struct ctl_lun_req *req);
272229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
273229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
274229997Sken					   ctl_lun_config_status status);
275229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
276229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
277229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
278274154Smavstatic uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
279229997Skenint ctl_be_block_init(void);
280229997Sken
281229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
282229997Sken{
283230334Sken	.name = "block",
284230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
285230334Sken	.init = ctl_be_block_init,
286230334Sken	.data_submit = ctl_be_block_submit,
287230334Sken	.data_move_done = ctl_be_block_move_done,
288230334Sken	.config_read = ctl_be_block_config_read,
289230334Sken	.config_write = ctl_be_block_config_write,
290230334Sken	.ioctl = ctl_be_block_ioctl,
291274154Smav	.lun_info = ctl_be_block_lun_info,
292274154Smav	.lun_attr = ctl_be_block_lun_attr
293229997Sken};
294229997Sken
295229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
296229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
297229997Sken
298264020Straszstatic uma_zone_t beio_zone;
299264020Strasz
300229997Skenstatic struct ctl_be_block_io *
301229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
302229997Sken{
303229997Sken	struct ctl_be_block_io *beio;
304229997Sken
305264020Strasz	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
306264020Strasz	beio->softc = softc;
307229997Sken	return (beio);
308229997Sken}
309229997Sken
310229997Skenstatic void
311229997Skenctl_free_beio(struct ctl_be_block_io *beio)
312229997Sken{
313229997Sken	int duplicate_free;
314229997Sken	int i;
315229997Sken
316229997Sken	duplicate_free = 0;
317229997Sken
318229997Sken	for (i = 0; i < beio->num_segs; i++) {
319229997Sken		if (beio->sg_segs[i].addr == NULL)
320229997Sken			duplicate_free++;
321229997Sken
322229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
323229997Sken		beio->sg_segs[i].addr = NULL;
324267537Smav
325267537Smav		/* For compare we had two equal S/G lists. */
326267537Smav		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
327267537Smav			uma_zfree(beio->lun->lun_zone,
328267537Smav			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
329267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
330267537Smav		}
331229997Sken	}
332229997Sken
333229997Sken	if (duplicate_free > 0) {
334229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
335229997Sken		       duplicate_free, beio->num_segs);
336229997Sken	}
337229997Sken
338264020Strasz	uma_zfree(beio_zone, beio);
339229997Sken}
340229997Sken
341229997Skenstatic void
342229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
343229997Sken{
344267877Smav	union ctl_io *io = beio->io;
345229997Sken
346264274Smav	if (beio->beio_cont != NULL) {
347264274Smav		beio->beio_cont(beio);
348264274Smav	} else {
349264274Smav		ctl_free_beio(beio);
350267537Smav		ctl_data_submit_done(io);
351264274Smav	}
352229997Sken}
353229997Sken
354229997Skenstatic int
355229997Skenctl_be_block_move_done(union ctl_io *io)
356229997Sken{
357229997Sken	struct ctl_be_block_io *beio;
358229997Sken	struct ctl_be_block_lun *be_lun;
359267537Smav	struct ctl_lba_len_flags *lbalen;
360229997Sken#ifdef CTL_TIME_IO
361229997Sken	struct bintime cur_bt;
362267537Smav#endif
363267537Smav	int i;
364229997Sken
365267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
366229997Sken	be_lun = beio->lun;
367229997Sken
368229997Sken	DPRINTF("entered\n");
369229997Sken
370229997Sken#ifdef CTL_TIME_IO
371229997Sken	getbintime(&cur_bt);
372229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
373229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
374229997Sken	io->io_hdr.num_dmas++;
375229997Sken#endif
376267537Smav	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
377229997Sken
378229997Sken	/*
379229997Sken	 * We set status at this point for read commands, and write
380229997Sken	 * commands with errors.
381229997Sken	 */
382275058Smav	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
383275058Smav		;
384275058Smav	} else if ((io->io_hdr.port_status == 0) &&
385267537Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
386267537Smav		lbalen = ARGS(beio->io);
387267537Smav		if (lbalen->flags & CTL_LLF_READ) {
388267537Smav			ctl_set_success(&io->scsiio);
389267537Smav		} else if (lbalen->flags & CTL_LLF_COMPARE) {
390267537Smav			/* We have two data blocks ready for comparison. */
391267537Smav			for (i = 0; i < beio->num_segs; i++) {
392267537Smav				if (memcmp(beio->sg_segs[i].addr,
393267537Smav				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
394267537Smav				    beio->sg_segs[i].len) != 0)
395267537Smav					break;
396267537Smav			}
397267537Smav			if (i < beio->num_segs)
398267537Smav				ctl_set_sense(&io->scsiio,
399267537Smav				    /*current_error*/ 1,
400267537Smav				    /*sense_key*/ SSD_KEY_MISCOMPARE,
401267537Smav				    /*asc*/ 0x1D,
402267537Smav				    /*ascq*/ 0x00,
403267537Smav				    SSD_ELEM_NONE);
404267537Smav			else
405267537Smav				ctl_set_success(&io->scsiio);
406267537Smav		}
407275058Smav	} else if ((io->io_hdr.port_status != 0) &&
408275058Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
409275058Smav	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
410229997Sken		/*
411229997Sken		 * For hardware error sense keys, the sense key
412229997Sken		 * specific value is defined to be a retry count,
413229997Sken		 * but we use it to pass back an internal FETD
414229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
415229997Sken		 * using 16 bits for an error code, since that's
416229997Sken		 * all the space we have in the sks field.
417229997Sken		 */
418229997Sken		ctl_set_internal_failure(&io->scsiio,
419229997Sken					 /*sks_valid*/ 1,
420229997Sken					 /*retry_count*/
421229997Sken					 io->io_hdr.port_status);
422229997Sken	}
423229997Sken
424229997Sken	/*
425229997Sken	 * If this is a read, or a write with errors, it is done.
426229997Sken	 */
427229997Sken	if ((beio->bio_cmd == BIO_READ)
428229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
429229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
430229997Sken		ctl_complete_beio(beio);
431229997Sken		return (0);
432229997Sken	}
433229997Sken
434229997Sken	/*
435229997Sken	 * At this point, we have a write and the DMA completed
436229997Sken	 * successfully.  We now have to queue it to the task queue to
437229997Sken	 * execute the backend I/O.  That is because we do blocking
438229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
439229997Sken	 * This move done routine is generally called in the SIM's
440229997Sken	 * interrupt context, and therefore we cannot block.
441229997Sken	 */
442267877Smav	mtx_lock(&be_lun->queue_lock);
443229997Sken	/*
444229997Sken	 * XXX KDM make sure that links is okay to use at this point.
445229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
446229997Sken	 * or deal with resource allocation here.
447229997Sken	 */
448229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
449267877Smav	mtx_unlock(&be_lun->queue_lock);
450229997Sken
451229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
452229997Sken
453229997Sken	return (0);
454229997Sken}
455229997Sken
456229997Skenstatic void
457229997Skenctl_be_block_biodone(struct bio *bio)
458229997Sken{
459229997Sken	struct ctl_be_block_io *beio;
460229997Sken	struct ctl_be_block_lun *be_lun;
461229997Sken	union ctl_io *io;
462261538Smav	int error;
463229997Sken
464229997Sken	beio = bio->bio_caller1;
465229997Sken	be_lun = beio->lun;
466229997Sken	io = beio->io;
467229997Sken
468229997Sken	DPRINTF("entered\n");
469229997Sken
470261538Smav	error = bio->bio_error;
471267877Smav	mtx_lock(&be_lun->io_lock);
472261538Smav	if (error != 0)
473229997Sken		beio->num_errors++;
474229997Sken
475229997Sken	beio->num_bios_done++;
476229997Sken
477229997Sken	/*
478229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
479229997Sken	 * during the free might cause it to complain.
480229997Sken	 */
481229997Sken	g_destroy_bio(bio);
482229997Sken
483229997Sken	/*
484229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
485229997Sken	 * complete, then we're done.
486229997Sken	 */
487229997Sken	if ((beio->send_complete == 0)
488229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
489267877Smav		mtx_unlock(&be_lun->io_lock);
490229997Sken		return;
491229997Sken	}
492229997Sken
493229997Sken	/*
494229997Sken	 * At this point, we've verified that we are the last I/O to
495229997Sken	 * complete, so it's safe to drop the lock.
496229997Sken	 */
497267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
498267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
499267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
500267877Smav	mtx_unlock(&be_lun->io_lock);
501229997Sken
502229997Sken	/*
503229997Sken	 * If there are any errors from the backing device, we fail the
504229997Sken	 * entire I/O with a medium error.
505229997Sken	 */
506229997Sken	if (beio->num_errors > 0) {
507261538Smav		if (error == EOPNOTSUPP) {
508261538Smav			ctl_set_invalid_opcode(&io->scsiio);
509282565Smav		} else if (error == ENOSPC || error == EDQUOT) {
510273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
511261538Smav		} else if (beio->bio_cmd == BIO_FLUSH) {
512229997Sken			/* XXX KDM is there is a better error here? */
513229997Sken			ctl_set_internal_failure(&io->scsiio,
514229997Sken						 /*sks_valid*/ 1,
515229997Sken						 /*retry_count*/ 0xbad2);
516229997Sken		} else
517229997Sken			ctl_set_medium_error(&io->scsiio);
518229997Sken		ctl_complete_beio(beio);
519229997Sken		return;
520229997Sken	}
521229997Sken
522229997Sken	/*
523267537Smav	 * If this is a write, a flush, a delete or verify, we're all done.
524229997Sken	 * If this is a read, we can now send the data to the user.
525229997Sken	 */
526229997Sken	if ((beio->bio_cmd == BIO_WRITE)
527264274Smav	 || (beio->bio_cmd == BIO_FLUSH)
528267537Smav	 || (beio->bio_cmd == BIO_DELETE)
529267537Smav	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
530229997Sken		ctl_set_success(&io->scsiio);
531229997Sken		ctl_complete_beio(beio);
532229997Sken	} else {
533275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
534275058Smav		    beio->beio_cont == NULL)
535275058Smav			ctl_set_success(&io->scsiio);
536229997Sken#ifdef CTL_TIME_IO
537229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
538229997Sken#endif
539229997Sken		ctl_datamove(io);
540229997Sken	}
541229997Sken}
542229997Sken
543229997Skenstatic void
544229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
545229997Sken			struct ctl_be_block_io *beio)
546229997Sken{
547267877Smav	union ctl_io *io = beio->io;
548229997Sken	struct mount *mountpoint;
549241896Skib	int error, lock_flags;
550229997Sken
551229997Sken	DPRINTF("entered\n");
552229997Sken
553267877Smav	binuptime(&beio->ds_t0);
554267877Smav	mtx_lock(&be_lun->io_lock);
555267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
556267877Smav	mtx_unlock(&be_lun->io_lock);
557229997Sken
558267877Smav	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
559229997Sken
560229997Sken	if (MNT_SHARED_WRITES(mountpoint)
561229997Sken	 || ((mountpoint == NULL)
562229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
563229997Sken		lock_flags = LK_SHARED;
564229997Sken	else
565229997Sken		lock_flags = LK_EXCLUSIVE;
566229997Sken
567229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
568229997Sken
569286353Smav	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
570286353Smav	    curthread);
571229997Sken	VOP_UNLOCK(be_lun->vn, 0);
572229997Sken
573229997Sken	vn_finished_write(mountpoint);
574229997Sken
575267877Smav	mtx_lock(&be_lun->io_lock);
576267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
577267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
578267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
579267877Smav	mtx_unlock(&be_lun->io_lock);
580267877Smav
581229997Sken	if (error == 0)
582229997Sken		ctl_set_success(&io->scsiio);
583229997Sken	else {
584229997Sken		/* XXX KDM is there is a better error here? */
585229997Sken		ctl_set_internal_failure(&io->scsiio,
586229997Sken					 /*sks_valid*/ 1,
587229997Sken					 /*retry_count*/ 0xbad1);
588229997Sken	}
589229997Sken
590229997Sken	ctl_complete_beio(beio);
591229997Sken}
592229997Sken
593258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
594258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
595258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
596258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
597229997Sken
598229997Skenstatic void
599229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
600229997Sken			   struct ctl_be_block_io *beio)
601229997Sken{
602229997Sken	struct ctl_be_block_filedata *file_data;
603229997Sken	union ctl_io *io;
604229997Sken	struct uio xuio;
605229997Sken	struct iovec *xiovec;
606241896Skib	int flags;
607229997Sken	int error, i;
608229997Sken
609229997Sken	DPRINTF("entered\n");
610229997Sken
611229997Sken	file_data = &be_lun->backend.file;
612229997Sken	io = beio->io;
613271309Smav	flags = 0;
614271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
615271309Smav		flags |= IO_DIRECT;
616271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
617271309Smav		flags |= IO_SYNC;
618229997Sken
619267537Smav	bzero(&xuio, sizeof(xuio));
620229997Sken	if (beio->bio_cmd == BIO_READ) {
621229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
622267537Smav		xuio.uio_rw = UIO_READ;
623229997Sken	} else {
624229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
625267537Smav		xuio.uio_rw = UIO_WRITE;
626229997Sken	}
627229997Sken	xuio.uio_offset = beio->io_offset;
628229997Sken	xuio.uio_resid = beio->io_len;
629229997Sken	xuio.uio_segflg = UIO_SYSSPACE;
630229997Sken	xuio.uio_iov = beio->xiovecs;
631229997Sken	xuio.uio_iovcnt = beio->num_segs;
632229997Sken	xuio.uio_td = curthread;
633229997Sken
634229997Sken	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
635229997Sken		xiovec->iov_base = beio->sg_segs[i].addr;
636229997Sken		xiovec->iov_len = beio->sg_segs[i].len;
637229997Sken	}
638229997Sken
639267877Smav	binuptime(&beio->ds_t0);
640267877Smav	mtx_lock(&be_lun->io_lock);
641267877Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
642267877Smav	mtx_unlock(&be_lun->io_lock);
643267877Smav
644229997Sken	if (beio->bio_cmd == BIO_READ) {
645229997Sken		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
646229997Sken
647229997Sken		/*
648229997Sken		 * UFS pays attention to IO_DIRECT for reads.  If the
649229997Sken		 * DIRECTIO option is configured into the kernel, it calls
650229997Sken		 * ffs_rawread().  But that only works for single-segment
651229997Sken		 * uios with user space addresses.  In our case, with a
652229997Sken		 * kernel uio, it still reads into the buffer cache, but it
653229997Sken		 * will just try to release the buffer from the cache later
654229997Sken		 * on in ffs_read().
655229997Sken		 *
656229997Sken		 * ZFS does not pay attention to IO_DIRECT for reads.
657229997Sken		 *
658229997Sken		 * UFS does not pay attention to IO_SYNC for reads.
659229997Sken		 *
660229997Sken		 * ZFS pays attention to IO_SYNC (which translates into the
661229997Sken		 * Solaris define FRSYNC for zfs_read()) for reads.  It
662229997Sken		 * attempts to sync the file before reading.
663229997Sken		 */
664271309Smav		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
665229997Sken
666229997Sken		VOP_UNLOCK(be_lun->vn, 0);
667267537Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
668229997Sken	} else {
669229997Sken		struct mount *mountpoint;
670229997Sken		int lock_flags;
671229997Sken
672229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
673229997Sken
674229997Sken		if (MNT_SHARED_WRITES(mountpoint)
675229997Sken		 || ((mountpoint == NULL)
676229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
677229997Sken			lock_flags = LK_SHARED;
678229997Sken		else
679229997Sken			lock_flags = LK_EXCLUSIVE;
680229997Sken
681229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
682229997Sken
683229997Sken		/*
684229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
685229997Sken		 * is done asynchronously.  (Normally the write would just
686229997Sken		 * get put into cache.
687229997Sken		 *
688229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
689229997Sken		 * attempt to write the buffer out synchronously if that
690229997Sken		 * flag is set.
691229997Sken		 *
692229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
693229997Sken		 *
694229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
695229997Sken		 * for writes.  It will flush the transaction from the
696229997Sken		 * cache before returning.
697229997Sken		 */
698271309Smav		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
699229997Sken		VOP_UNLOCK(be_lun->vn, 0);
700229997Sken
701229997Sken		vn_finished_write(mountpoint);
702267537Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
703229997Sken        }
704229997Sken
705267877Smav	mtx_lock(&be_lun->io_lock);
706267877Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
707267877Smav	    beio->ds_tag_type, beio->ds_trans_type,
708267877Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
709267877Smav	mtx_unlock(&be_lun->io_lock);
710267877Smav
711229997Sken	/*
712229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
713229997Sken	 * return the I/O to the user.
714229997Sken	 */
715229997Sken	if (error != 0) {
716229997Sken		char path_str[32];
717229997Sken
718229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
719229997Sken		printf("%s%s command returned errno %d\n", path_str,
720229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
721282565Smav		if (error == ENOSPC || error == EDQUOT) {
722273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
723273809Smav		} else
724273809Smav			ctl_set_medium_error(&io->scsiio);
725229997Sken		ctl_complete_beio(beio);
726229997Sken		return;
727229997Sken	}
728229997Sken
729229997Sken	/*
730269122Smav	 * If this is a write or a verify, we're all done.
731229997Sken	 * If this is a read, we can now send the data to the user.
732229997Sken	 */
733269122Smav	if ((beio->bio_cmd == BIO_WRITE) ||
734269122Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
735229997Sken		ctl_set_success(&io->scsiio);
736229997Sken		ctl_complete_beio(beio);
737229997Sken	} else {
738275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
739275058Smav		    beio->beio_cont == NULL)
740275058Smav			ctl_set_success(&io->scsiio);
741229997Sken#ifdef CTL_TIME_IO
742229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
743229997Sken#endif
744229997Sken		ctl_datamove(io);
745229997Sken	}
746229997Sken}
747229997Sken
748229997Skenstatic void
749275474Smavctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
750275474Smav			struct ctl_be_block_io *beio)
751275474Smav{
752275474Smav	union ctl_io *io = beio->io;
753275474Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
754275474Smav	struct scsi_get_lba_status_data *data;
755275474Smav	off_t roff, off;
756275474Smav	int error, status;
757275474Smav
758275474Smav	DPRINTF("entered\n");
759275474Smav
760287499Smav	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
761275474Smav	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
762275474Smav	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
763275474Smav	    0, curthread->td_ucred, curthread);
764275474Smav	if (error == 0 && off > roff)
765275474Smav		status = 0;	/* mapped up to off */
766275474Smav	else {
767275474Smav		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
768275474Smav		    0, curthread->td_ucred, curthread);
769275474Smav		if (error == 0 && off > roff)
770275474Smav			status = 1;	/* deallocated up to off */
771275474Smav		else {
772275474Smav			status = 0;	/* unknown up to the end */
773275474Smav			off = be_lun->size_bytes;
774275474Smav		}
775275474Smav	}
776275474Smav	VOP_UNLOCK(be_lun->vn, 0);
777275474Smav
778275474Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
779275474Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
780287499Smav	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
781287499Smav	    lbalen->lba), data->descr[0].length);
782275474Smav	data->descr[0].status = status;
783275474Smav
784275474Smav	ctl_complete_beio(beio);
785275474Smav}
786275474Smav
787275481Smavstatic uint64_t
788275481Smavctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
789275481Smav{
790275481Smav	struct vattr		vattr;
791275481Smav	struct statfs		statfs;
792285030Smav	uint64_t		val;
793275481Smav	int			error;
794275481Smav
795285030Smav	val = UINT64_MAX;
796275481Smav	if (be_lun->vn == NULL)
797285030Smav		return (val);
798285030Smav	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
799275481Smav	if (strcmp(attrname, "blocksused") == 0) {
800275481Smav		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
801285030Smav		if (error == 0)
802287499Smav			val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
803275481Smav	}
804285030Smav	if (strcmp(attrname, "blocksavail") == 0 &&
805285030Smav	    (be_lun->vn->v_iflag & VI_DOOMED) == 0) {
806275481Smav		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
807285030Smav		if (error == 0)
808286811Smav			val = statfs.f_bavail * statfs.f_bsize /
809287499Smav			    be_lun->cbe_lun.blocksize;
810275481Smav	}
811285030Smav	VOP_UNLOCK(be_lun->vn, 0);
812285030Smav	return (val);
813275481Smav}
814275481Smav
815275474Smavstatic void
816269123Smavctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
817269123Smav			   struct ctl_be_block_io *beio)
818269123Smav{
819269123Smav	union ctl_io *io;
820287664Smav	struct cdevsw *csw;
821287664Smav	struct cdev *dev;
822269123Smav	struct uio xuio;
823269123Smav	struct iovec *xiovec;
824287664Smav	int error, flags, i, ref;
825269123Smav
826269123Smav	DPRINTF("entered\n");
827269123Smav
828269123Smav	io = beio->io;
829271309Smav	flags = 0;
830271309Smav	if (ARGS(io)->flags & CTL_LLF_DPO)
831271309Smav		flags |= IO_DIRECT;
832271309Smav	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
833271309Smav		flags |= IO_SYNC;
834269123Smav
835269123Smav	bzero(&xuio, sizeof(xuio));
836269123Smav	if (beio->bio_cmd == BIO_READ) {
837269123Smav		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
838269123Smav		xuio.uio_rw = UIO_READ;
839269123Smav	} else {
840269123Smav		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
841269123Smav		xuio.uio_rw = UIO_WRITE;
842269123Smav	}
843269123Smav	xuio.uio_offset = beio->io_offset;
844269123Smav	xuio.uio_resid = beio->io_len;
845269123Smav	xuio.uio_segflg = UIO_SYSSPACE;
846269123Smav	xuio.uio_iov = beio->xiovecs;
847269123Smav	xuio.uio_iovcnt = beio->num_segs;
848269123Smav	xuio.uio_td = curthread;
849269123Smav
850269123Smav	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
851269123Smav		xiovec->iov_base = beio->sg_segs[i].addr;
852269123Smav		xiovec->iov_len = beio->sg_segs[i].len;
853269123Smav	}
854269123Smav
855269123Smav	binuptime(&beio->ds_t0);
856269123Smav	mtx_lock(&be_lun->io_lock);
857269123Smav	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
858269123Smav	mtx_unlock(&be_lun->io_lock);
859269123Smav
860287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
861287664Smav	if (csw) {
862287664Smav		if (beio->bio_cmd == BIO_READ)
863287664Smav			error = csw->d_read(dev, &xuio, flags);
864287664Smav		else
865287664Smav			error = csw->d_write(dev, &xuio, flags);
866287664Smav		dev_relthread(dev, ref);
867287664Smav	} else
868287664Smav		error = ENXIO;
869287664Smav
870287664Smav	if (beio->bio_cmd == BIO_READ)
871269123Smav		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
872287664Smav	else
873269123Smav		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
874269123Smav
875269123Smav	mtx_lock(&be_lun->io_lock);
876269123Smav	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
877269123Smav	    beio->ds_tag_type, beio->ds_trans_type,
878269123Smav	    /*now*/ NULL, /*then*/&beio->ds_t0);
879269123Smav	mtx_unlock(&be_lun->io_lock);
880269123Smav
881269123Smav	/*
882269123Smav	 * If we got an error, set the sense data to "MEDIUM ERROR" and
883269123Smav	 * return the I/O to the user.
884269123Smav	 */
885269123Smav	if (error != 0) {
886282565Smav		if (error == ENOSPC || error == EDQUOT) {
887273809Smav			ctl_set_space_alloc_fail(&io->scsiio);
888273809Smav		} else
889273809Smav			ctl_set_medium_error(&io->scsiio);
890269123Smav		ctl_complete_beio(beio);
891269123Smav		return;
892269123Smav	}
893269123Smav
894269123Smav	/*
895269123Smav	 * If this is a write or a verify, we're all done.
896269123Smav	 * If this is a read, we can now send the data to the user.
897269123Smav	 */
898269123Smav	if ((beio->bio_cmd == BIO_WRITE) ||
899269123Smav	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
900269123Smav		ctl_set_success(&io->scsiio);
901269123Smav		ctl_complete_beio(beio);
902269123Smav	} else {
903275058Smav		if ((ARGS(io)->flags & CTL_LLF_READ) &&
904275058Smav		    beio->beio_cont == NULL)
905275058Smav			ctl_set_success(&io->scsiio);
906269123Smav#ifdef CTL_TIME_IO
907269123Smav        	getbintime(&io->io_hdr.dma_start_bt);
908269123Smav#endif
909269123Smav		ctl_datamove(io);
910269123Smav	}
911269123Smav}
912269123Smav
913269123Smavstatic void
914275474Smavctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
915275474Smav			struct ctl_be_block_io *beio)
916275474Smav{
917275474Smav	union ctl_io *io = beio->io;
918287664Smav	struct cdevsw *csw;
919287664Smav	struct cdev *dev;
920275474Smav	struct ctl_lba_len_flags *lbalen = ARGS(io);
921275474Smav	struct scsi_get_lba_status_data *data;
922275474Smav	off_t roff, off;
923287664Smav	int error, ref, status;
924275474Smav
925275474Smav	DPRINTF("entered\n");
926275474Smav
927287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
928287664Smav	if (csw == NULL) {
929287664Smav		status = 0;	/* unknown up to the end */
930287664Smav		off = be_lun->size_bytes;
931287664Smav		goto done;
932287664Smav	}
933287499Smav	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
934287664Smav	error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
935287664Smav	    curthread);
936275474Smav	if (error == 0 && off > roff)
937275474Smav		status = 0;	/* mapped up to off */
938275474Smav	else {
939287664Smav		error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
940287664Smav		    curthread);
941275474Smav		if (error == 0 && off > roff)
942275474Smav			status = 1;	/* deallocated up to off */
943275474Smav		else {
944275474Smav			status = 0;	/* unknown up to the end */
945275474Smav			off = be_lun->size_bytes;
946275474Smav		}
947275474Smav	}
948287664Smav	dev_relthread(dev, ref);
949275474Smav
950287664Smavdone:
951275474Smav	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
952275474Smav	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
953287499Smav	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
954287499Smav	    lbalen->lba), data->descr[0].length);
955275474Smav	data->descr[0].status = status;
956275474Smav
957275474Smav	ctl_complete_beio(beio);
958275474Smav}
959275474Smav
960275474Smavstatic void
961229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
962229997Sken		       struct ctl_be_block_io *beio)
963229997Sken{
964229997Sken	struct bio *bio;
965229997Sken	union ctl_io *io;
966287664Smav	struct cdevsw *csw;
967287664Smav	struct cdev *dev;
968287664Smav	int ref;
969229997Sken
970229997Sken	io = beio->io;
971229997Sken
972229997Sken	DPRINTF("entered\n");
973229997Sken
974229997Sken	/* This can't fail, it's a blocking allocation. */
975229997Sken	bio = g_alloc_bio();
976229997Sken
977229997Sken	bio->bio_cmd	    = BIO_FLUSH;
978229997Sken	bio->bio_offset	    = 0;
979229997Sken	bio->bio_data	    = 0;
980229997Sken	bio->bio_done	    = ctl_be_block_biodone;
981229997Sken	bio->bio_caller1    = beio;
982229997Sken	bio->bio_pblkno	    = 0;
983229997Sken
984229997Sken	/*
985229997Sken	 * We don't need to acquire the LUN lock here, because we are only
986229997Sken	 * sending one bio, and so there is no other context to synchronize
987229997Sken	 * with.
988229997Sken	 */
989229997Sken	beio->num_bios_sent = 1;
990229997Sken	beio->send_complete = 1;
991229997Sken
992229997Sken	binuptime(&beio->ds_t0);
993267877Smav	mtx_lock(&be_lun->io_lock);
994229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
995267877Smav	mtx_unlock(&be_lun->io_lock);
996229997Sken
997287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
998287664Smav	if (csw) {
999287664Smav		bio->bio_dev = dev;
1000287664Smav		csw->d_strategy(bio);
1001287664Smav		dev_relthread(dev, ref);
1002287664Smav	} else {
1003287664Smav		bio->bio_error = ENXIO;
1004287664Smav		ctl_be_block_biodone(bio);
1005287664Smav	}
1006229997Sken}
1007229997Sken
1008229997Skenstatic void
1009264274Smavctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1010264274Smav		       struct ctl_be_block_io *beio,
1011264274Smav		       uint64_t off, uint64_t len, int last)
1012264274Smav{
1013264274Smav	struct bio *bio;
1014264296Smav	uint64_t maxlen;
1015287664Smav	struct cdevsw *csw;
1016287664Smav	struct cdev *dev;
1017287664Smav	int ref;
1018264274Smav
1019287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1020287499Smav	maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1021264274Smav	while (len > 0) {
1022264274Smav		bio = g_alloc_bio();
1023264274Smav		bio->bio_cmd	    = BIO_DELETE;
1024287664Smav		bio->bio_dev	    = dev;
1025264274Smav		bio->bio_offset	    = off;
1026264296Smav		bio->bio_length	    = MIN(len, maxlen);
1027264274Smav		bio->bio_data	    = 0;
1028264274Smav		bio->bio_done	    = ctl_be_block_biodone;
1029264274Smav		bio->bio_caller1    = beio;
1030287499Smav		bio->bio_pblkno     = off / be_lun->cbe_lun.blocksize;
1031264274Smav
1032264274Smav		off += bio->bio_length;
1033264274Smav		len -= bio->bio_length;
1034264274Smav
1035267877Smav		mtx_lock(&be_lun->io_lock);
1036264274Smav		beio->num_bios_sent++;
1037264274Smav		if (last && len == 0)
1038264274Smav			beio->send_complete = 1;
1039267877Smav		mtx_unlock(&be_lun->io_lock);
1040264274Smav
1041287664Smav		if (csw) {
1042287664Smav			csw->d_strategy(bio);
1043287664Smav		} else {
1044287664Smav			bio->bio_error = ENXIO;
1045287664Smav			ctl_be_block_biodone(bio);
1046287664Smav		}
1047264274Smav	}
1048287664Smav	if (csw)
1049287664Smav		dev_relthread(dev, ref);
1050264274Smav}
1051264274Smav
1052264274Smavstatic void
1053264274Smavctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1054264274Smav		       struct ctl_be_block_io *beio)
1055264274Smav{
1056264274Smav	union ctl_io *io;
1057267515Smav	struct ctl_ptr_len_flags *ptrlen;
1058264274Smav	struct scsi_unmap_desc *buf, *end;
1059264274Smav	uint64_t len;
1060264274Smav
1061264274Smav	io = beio->io;
1062264274Smav
1063264274Smav	DPRINTF("entered\n");
1064264274Smav
1065264274Smav	binuptime(&beio->ds_t0);
1066267877Smav	mtx_lock(&be_lun->io_lock);
1067264274Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1068267877Smav	mtx_unlock(&be_lun->io_lock);
1069264274Smav
1070264274Smav	if (beio->io_offset == -1) {
1071264274Smav		beio->io_len = 0;
1072267515Smav		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1073267515Smav		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
1074267515Smav		end = buf + ptrlen->len / sizeof(*buf);
1075264274Smav		for (; buf < end; buf++) {
1076264274Smav			len = (uint64_t)scsi_4btoul(buf->length) *
1077287499Smav			    be_lun->cbe_lun.blocksize;
1078264274Smav			beio->io_len += len;
1079264274Smav			ctl_be_block_unmap_dev_range(be_lun, beio,
1080287499Smav			    scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
1081287499Smav			    len, (end - buf < 2) ? TRUE : FALSE);
1082264274Smav		}
1083264274Smav	} else
1084264274Smav		ctl_be_block_unmap_dev_range(be_lun, beio,
1085264274Smav		    beio->io_offset, beio->io_len, TRUE);
1086264274Smav}
1087264274Smav
1088264274Smavstatic void
1089229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1090229997Sken			  struct ctl_be_block_io *beio)
1091229997Sken{
1092267877Smav	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1093229997Sken	struct bio *bio;
1094287664Smav	struct cdevsw *csw;
1095287664Smav	struct cdev *dev;
1096229997Sken	off_t cur_offset;
1097287664Smav	int i, max_iosize, ref;
1098229997Sken
1099229997Sken	DPRINTF("entered\n");
1100287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1101229997Sken
1102229997Sken	/*
1103229997Sken	 * We have to limit our I/O size to the maximum supported by the
1104229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
1105229997Sken	 * set it properly, use DFLTPHYS.
1106229997Sken	 */
1107287664Smav	if (csw) {
1108287664Smav		max_iosize = dev->si_iosize_max;
1109287664Smav		if (max_iosize < PAGE_SIZE)
1110287664Smav			max_iosize = DFLTPHYS;
1111287664Smav	} else
1112229997Sken		max_iosize = DFLTPHYS;
1113229997Sken
1114229997Sken	cur_offset = beio->io_offset;
1115229997Sken	for (i = 0; i < beio->num_segs; i++) {
1116229997Sken		size_t cur_size;
1117229997Sken		uint8_t *cur_ptr;
1118229997Sken
1119229997Sken		cur_size = beio->sg_segs[i].len;
1120229997Sken		cur_ptr = beio->sg_segs[i].addr;
1121229997Sken
1122229997Sken		while (cur_size > 0) {
1123229997Sken			/* This can't fail, it's a blocking allocation. */
1124229997Sken			bio = g_alloc_bio();
1125229997Sken
1126229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1127229997Sken
1128229997Sken			bio->bio_cmd = beio->bio_cmd;
1129287664Smav			bio->bio_dev = dev;
1130229997Sken			bio->bio_caller1 = beio;
1131229997Sken			bio->bio_length = min(cur_size, max_iosize);
1132229997Sken			bio->bio_offset = cur_offset;
1133229997Sken			bio->bio_data = cur_ptr;
1134229997Sken			bio->bio_done = ctl_be_block_biodone;
1135287499Smav			bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1136229997Sken
1137229997Sken			cur_offset += bio->bio_length;
1138229997Sken			cur_ptr += bio->bio_length;
1139229997Sken			cur_size -= bio->bio_length;
1140229997Sken
1141267877Smav			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1142229997Sken			beio->num_bios_sent++;
1143229997Sken		}
1144229997Sken	}
1145267877Smav	binuptime(&beio->ds_t0);
1146267877Smav	mtx_lock(&be_lun->io_lock);
1147267877Smav	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1148267877Smav	beio->send_complete = 1;
1149267877Smav	mtx_unlock(&be_lun->io_lock);
1150267877Smav
1151267877Smav	/*
1152267877Smav	 * Fire off all allocated requests!
1153267877Smav	 */
1154267877Smav	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
1155267877Smav		TAILQ_REMOVE(&queue, bio, bio_queue);
1156287664Smav		if (csw)
1157287664Smav			csw->d_strategy(bio);
1158287664Smav		else {
1159287664Smav			bio->bio_error = ENXIO;
1160287664Smav			ctl_be_block_biodone(bio);
1161287664Smav		}
1162267877Smav	}
1163287664Smav	if (csw)
1164287664Smav		dev_relthread(dev, ref);
1165229997Sken}
1166229997Sken
1167274154Smavstatic uint64_t
1168274154Smavctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1169274154Smav{
1170274154Smav	struct diocgattr_arg	arg;
1171287664Smav	struct cdevsw *csw;
1172287664Smav	struct cdev *dev;
1173287664Smav	int error, ref;
1174274154Smav
1175287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1176287664Smav	if (csw == NULL)
1177274154Smav		return (UINT64_MAX);
1178274154Smav	strlcpy(arg.name, attrname, sizeof(arg.name));
1179274154Smav	arg.len = sizeof(arg.value.off);
1180287664Smav	if (csw->d_ioctl) {
1181287664Smav		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
1182287664Smav		    curthread);
1183287664Smav	} else
1184287664Smav		error = ENODEV;
1185287664Smav	dev_relthread(dev, ref);
1186274154Smav	if (error != 0)
1187274154Smav		return (UINT64_MAX);
1188274154Smav	return (arg.value.off);
1189274154Smav}
1190274154Smav
1191229997Skenstatic void
1192286353Smavctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
1193286353Smav			    union ctl_io *io)
1194286353Smav{
1195287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1196286353Smav	struct ctl_be_block_io *beio;
1197286353Smav	struct ctl_lba_len_flags *lbalen;
1198286353Smav
1199286353Smav	DPRINTF("entered\n");
1200286353Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1201286353Smav	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1202286353Smav
1203287499Smav	beio->io_len = lbalen->len * cbe_lun->blocksize;
1204287499Smav	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1205286353Smav	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
1206286353Smav	beio->bio_cmd = BIO_FLUSH;
1207286353Smav	beio->ds_trans_type = DEVSTAT_NO_DATA;
1208286353Smav	DPRINTF("SYNC\n");
1209286353Smav	be_lun->lun_flush(be_lun, beio);
1210286353Smav}
1211286353Smav
1212286353Smavstatic void
1213264274Smavctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1214264274Smav{
1215264274Smav	union ctl_io *io;
1216264274Smav
1217264274Smav	io = beio->io;
1218264274Smav	ctl_free_beio(beio);
1219267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1220267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1221267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1222264274Smav		ctl_config_write_done(io);
1223264274Smav		return;
1224264274Smav	}
1225264274Smav
1226264274Smav	ctl_be_block_config_write(io);
1227264274Smav}
1228264274Smav
1229264274Smavstatic void
1230264274Smavctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1231264274Smav			    union ctl_io *io)
1232264274Smav{
1233287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1234264274Smav	struct ctl_be_block_io *beio;
1235267515Smav	struct ctl_lba_len_flags *lbalen;
1236278625Smav	uint64_t len_left, lba;
1237278625Smav	uint32_t pb, pbo, adj;
1238264274Smav	int i, seglen;
1239264274Smav	uint8_t *buf, *end;
1240264274Smav
1241264274Smav	DPRINTF("entered\n");
1242264274Smav
1243267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1244267537Smav	lbalen = ARGS(beio->io);
1245264274Smav
1246271839Smav	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
1247269622Smav	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1248264274Smav		ctl_free_beio(beio);
1249264274Smav		ctl_set_invalid_field(&io->scsiio,
1250264274Smav				      /*sks_valid*/ 1,
1251264274Smav				      /*command*/ 1,
1252264274Smav				      /*field*/ 1,
1253264274Smav				      /*bit_valid*/ 0,
1254264274Smav				      /*bit*/ 0);
1255264274Smav		ctl_config_write_done(io);
1256264274Smav		return;
1257264274Smav	}
1258264274Smav
1259269622Smav	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
1260287499Smav		beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1261287499Smav		beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1262264274Smav		beio->bio_cmd = BIO_DELETE;
1263264274Smav		beio->ds_trans_type = DEVSTAT_FREE;
1264264274Smav
1265264274Smav		be_lun->unmap(be_lun, beio);
1266264274Smav		return;
1267264274Smav	}
1268264274Smav
1269264274Smav	beio->bio_cmd = BIO_WRITE;
1270264274Smav	beio->ds_trans_type = DEVSTAT_WRITE;
1271264274Smav
1272264274Smav	DPRINTF("WRITE SAME at LBA %jx len %u\n",
1273267515Smav	       (uintmax_t)lbalen->lba, lbalen->len);
1274264274Smav
1275287499Smav	pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
1276287499Smav	if (be_lun->cbe_lun.pblockoff > 0)
1277287499Smav		pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1278278625Smav	else
1279278625Smav		pbo = 0;
1280287499Smav	len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1281264274Smav	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1282264274Smav
1283264274Smav		/*
1284264274Smav		 * Setup the S/G entry for this chunk.
1285264274Smav		 */
1286264886Smav		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1287287499Smav		if (pb > cbe_lun->blocksize) {
1288287499Smav			adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
1289278619Smav			    seglen - pbo) % pb;
1290278619Smav			if (seglen > adj)
1291278619Smav				seglen -= adj;
1292278619Smav			else
1293287499Smav				seglen -= seglen % cbe_lun->blocksize;
1294278619Smav		} else
1295287499Smav			seglen -= seglen % cbe_lun->blocksize;
1296264274Smav		beio->sg_segs[i].len = seglen;
1297264274Smav		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1298264274Smav
1299264274Smav		DPRINTF("segment %d addr %p len %zd\n", i,
1300264274Smav			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1301264274Smav
1302264274Smav		beio->num_segs++;
1303264274Smav		len_left -= seglen;
1304264274Smav
1305264274Smav		buf = beio->sg_segs[i].addr;
1306264274Smav		end = buf + seglen;
1307287499Smav		for (; buf < end; buf += cbe_lun->blocksize) {
1308287499Smav			memcpy(buf, io->scsiio.kern_data_ptr, cbe_lun->blocksize);
1309267515Smav			if (lbalen->flags & SWS_LBDATA)
1310267515Smav				scsi_ulto4b(lbalen->lba + lba, buf);
1311264274Smav			lba++;
1312264274Smav		}
1313264274Smav	}
1314264274Smav
1315287499Smav	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
1316287499Smav	beio->io_len = lba * cbe_lun->blocksize;
1317264274Smav
1318264274Smav	/* We can not do all in one run. Correct and schedule rerun. */
1319264274Smav	if (len_left > 0) {
1320267515Smav		lbalen->lba += lba;
1321267515Smav		lbalen->len -= lba;
1322264274Smav		beio->beio_cont = ctl_be_block_cw_done_ws;
1323264274Smav	}
1324264274Smav
1325264274Smav	be_lun->dispatch(be_lun, beio);
1326264274Smav}
1327264274Smav
1328264274Smavstatic void
1329264274Smavctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1330264274Smav			    union ctl_io *io)
1331264274Smav{
1332264274Smav	struct ctl_be_block_io *beio;
1333267515Smav	struct ctl_ptr_len_flags *ptrlen;
1334264274Smav
1335264274Smav	DPRINTF("entered\n");
1336264274Smav
1337267519Smav	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1338267515Smav	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1339264274Smav
1340269622Smav	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1341264274Smav		ctl_free_beio(beio);
1342264274Smav		ctl_set_invalid_field(&io->scsiio,
1343264274Smav				      /*sks_valid*/ 0,
1344264274Smav				      /*command*/ 1,
1345264274Smav				      /*field*/ 0,
1346264274Smav				      /*bit_valid*/ 0,
1347264274Smav				      /*bit*/ 0);
1348264274Smav		ctl_config_write_done(io);
1349264274Smav		return;
1350264274Smav	}
1351264274Smav
1352264274Smav	beio->io_len = 0;
1353264274Smav	beio->io_offset = -1;
1354264274Smav	beio->bio_cmd = BIO_DELETE;
1355264274Smav	beio->ds_trans_type = DEVSTAT_FREE;
1356267515Smav	DPRINTF("UNMAP\n");
1357264274Smav	be_lun->unmap(be_lun, beio);
1358264274Smav}
1359264274Smav
1360264274Smavstatic void
1361275474Smavctl_be_block_cr_done(struct ctl_be_block_io *beio)
1362275474Smav{
1363275474Smav	union ctl_io *io;
1364275474Smav
1365275474Smav	io = beio->io;
1366275474Smav	ctl_free_beio(beio);
1367275474Smav	ctl_config_read_done(io);
1368275474Smav}
1369275474Smav
1370275474Smavstatic void
1371275474Smavctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1372275474Smav			 union ctl_io *io)
1373275474Smav{
1374275474Smav	struct ctl_be_block_io *beio;
1375275474Smav	struct ctl_be_block_softc *softc;
1376275474Smav
1377275474Smav	DPRINTF("entered\n");
1378275474Smav
1379275474Smav	softc = be_lun->softc;
1380275474Smav	beio = ctl_alloc_beio(softc);
1381275474Smav	beio->io = io;
1382275474Smav	beio->lun = be_lun;
1383275474Smav	beio->beio_cont = ctl_be_block_cr_done;
1384275474Smav	PRIV(io)->ptr = (void *)beio;
1385275474Smav
1386275474Smav	switch (io->scsiio.cdb[0]) {
1387275474Smav	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1388275474Smav		beio->bio_cmd = -1;
1389275474Smav		beio->ds_trans_type = DEVSTAT_NO_DATA;
1390275474Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1391275474Smav		beio->io_len = 0;
1392275474Smav		if (be_lun->get_lba_status)
1393275474Smav			be_lun->get_lba_status(be_lun, beio);
1394275474Smav		else
1395275474Smav			ctl_be_block_cr_done(beio);
1396275474Smav		break;
1397275474Smav	default:
1398275474Smav		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1399275474Smav		break;
1400275474Smav	}
1401275474Smav}
1402275474Smav
1403275474Smavstatic void
1404264274Smavctl_be_block_cw_done(struct ctl_be_block_io *beio)
1405264274Smav{
1406264274Smav	union ctl_io *io;
1407264274Smav
1408264274Smav	io = beio->io;
1409264274Smav	ctl_free_beio(beio);
1410264274Smav	ctl_config_write_done(io);
1411264274Smav}
1412264274Smav
1413264274Smavstatic void
1414229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1415229997Sken			 union ctl_io *io)
1416229997Sken{
1417229997Sken	struct ctl_be_block_io *beio;
1418229997Sken	struct ctl_be_block_softc *softc;
1419229997Sken
1420229997Sken	DPRINTF("entered\n");
1421229997Sken
1422229997Sken	softc = be_lun->softc;
1423229997Sken	beio = ctl_alloc_beio(softc);
1424229997Sken	beio->io = io;
1425229997Sken	beio->lun = be_lun;
1426264274Smav	beio->beio_cont = ctl_be_block_cw_done;
1427286353Smav	switch (io->scsiio.tag_type) {
1428286353Smav	case CTL_TAG_ORDERED:
1429286353Smav		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1430286353Smav		break;
1431286353Smav	case CTL_TAG_HEAD_OF_QUEUE:
1432286353Smav		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1433286353Smav		break;
1434286353Smav	case CTL_TAG_UNTAGGED:
1435286353Smav	case CTL_TAG_SIMPLE:
1436286353Smav	case CTL_TAG_ACA:
1437286353Smav	default:
1438286353Smav		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1439286353Smav		break;
1440286353Smav	}
1441267519Smav	PRIV(io)->ptr = (void *)beio;
1442229997Sken
1443229997Sken	switch (io->scsiio.cdb[0]) {
1444229997Sken	case SYNCHRONIZE_CACHE:
1445229997Sken	case SYNCHRONIZE_CACHE_16:
1446286353Smav		ctl_be_block_cw_dispatch_sync(be_lun, io);
1447229997Sken		break;
1448264274Smav	case WRITE_SAME_10:
1449264274Smav	case WRITE_SAME_16:
1450264274Smav		ctl_be_block_cw_dispatch_ws(be_lun, io);
1451264274Smav		break;
1452264274Smav	case UNMAP:
1453264274Smav		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1454264274Smav		break;
1455229997Sken	default:
1456229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1457229997Sken		break;
1458229997Sken	}
1459229997Sken}
1460229997Sken
1461258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1462258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1463258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1464258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1465229997Sken
1466229997Skenstatic void
1467264886Smavctl_be_block_next(struct ctl_be_block_io *beio)
1468264886Smav{
1469264886Smav	struct ctl_be_block_lun *be_lun;
1470264886Smav	union ctl_io *io;
1471264886Smav
1472264886Smav	io = beio->io;
1473264886Smav	be_lun = beio->lun;
1474264886Smav	ctl_free_beio(beio);
1475267641Smav	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1476267641Smav	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1477267641Smav	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1478267537Smav		ctl_data_submit_done(io);
1479264886Smav		return;
1480264886Smav	}
1481264886Smav
1482264886Smav	io->io_hdr.status &= ~CTL_STATUS_MASK;
1483264886Smav	io->io_hdr.status |= CTL_STATUS_NONE;
1484264886Smav
1485267877Smav	mtx_lock(&be_lun->queue_lock);
1486264886Smav	/*
1487264886Smav	 * XXX KDM make sure that links is okay to use at this point.
1488264886Smav	 * Otherwise, we either need to add another field to ctl_io_hdr,
1489264886Smav	 * or deal with resource allocation here.
1490264886Smav	 */
1491264886Smav	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1492267877Smav	mtx_unlock(&be_lun->queue_lock);
1493264886Smav
1494264886Smav	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1495264886Smav}
1496264886Smav
1497264886Smavstatic void
1498229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1499229997Sken			   union ctl_io *io)
1500229997Sken{
1501287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1502229997Sken	struct ctl_be_block_io *beio;
1503229997Sken	struct ctl_be_block_softc *softc;
1504267537Smav	struct ctl_lba_len_flags *lbalen;
1505267519Smav	struct ctl_ptr_len_flags *bptrlen;
1506267519Smav	uint64_t len_left, lbas;
1507229997Sken	int i;
1508229997Sken
1509229997Sken	softc = be_lun->softc;
1510229997Sken
1511229997Sken	DPRINTF("entered\n");
1512229997Sken
1513267537Smav	lbalen = ARGS(io);
1514267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1515267537Smav		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
1516267537Smav	} else {
1517229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1518229997Sken	}
1519229997Sken
1520229997Sken	beio = ctl_alloc_beio(softc);
1521229997Sken	beio->io = io;
1522229997Sken	beio->lun = be_lun;
1523267519Smav	bptrlen = PRIV(io);
1524267519Smav	bptrlen->ptr = (void *)beio;
1525229997Sken
1526229997Sken	switch (io->scsiio.tag_type) {
1527229997Sken	case CTL_TAG_ORDERED:
1528229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1529229997Sken		break;
1530229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1531229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1532229997Sken		break;
1533229997Sken	case CTL_TAG_UNTAGGED:
1534229997Sken	case CTL_TAG_SIMPLE:
1535229997Sken	case CTL_TAG_ACA:
1536229997Sken	default:
1537229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1538229997Sken		break;
1539229997Sken	}
1540229997Sken
1541267537Smav	if (lbalen->flags & CTL_LLF_WRITE) {
1542267537Smav		beio->bio_cmd = BIO_WRITE;
1543267537Smav		beio->ds_trans_type = DEVSTAT_WRITE;
1544267537Smav	} else {
1545229997Sken		beio->bio_cmd = BIO_READ;
1546229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1547229997Sken	}
1548229997Sken
1549264886Smav	DPRINTF("%s at LBA %jx len %u @%ju\n",
1550229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1551267519Smav	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1552267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1553267537Smav		lbas = CTLBLK_HALF_IO_SIZE;
1554267537Smav	else
1555267537Smav		lbas = CTLBLK_MAX_IO_SIZE;
1556287499Smav	lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
1557287499Smav	beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
1558287499Smav	beio->io_len = lbas * cbe_lun->blocksize;
1559267519Smav	bptrlen->len += lbas;
1560229997Sken
1561264886Smav	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
1562264886Smav		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
1563264886Smav		    i, CTLBLK_MAX_SEGS));
1564229997Sken
1565229997Sken		/*
1566229997Sken		 * Setup the S/G entry for this chunk.
1567229997Sken		 */
1568264886Smav		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1569229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1570229997Sken
1571229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1572229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1573229997Sken
1574267537Smav		/* Set up second segment for compare operation. */
1575267537Smav		if (lbalen->flags & CTL_LLF_COMPARE) {
1576267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
1577267537Smav			    beio->sg_segs[i].len;
1578267537Smav			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
1579267537Smav			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
1580267537Smav		}
1581267537Smav
1582229997Sken		beio->num_segs++;
1583229997Sken		len_left -= beio->sg_segs[i].len;
1584229997Sken	}
1585267519Smav	if (bptrlen->len < lbalen->len)
1586264886Smav		beio->beio_cont = ctl_be_block_next;
1587264886Smav	io->scsiio.be_move_done = ctl_be_block_move_done;
1588267537Smav	/* For compare we have separate S/G lists for read and datamove. */
1589267537Smav	if (lbalen->flags & CTL_LLF_COMPARE)
1590267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
1591267537Smav	else
1592267537Smav		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1593264886Smav	io->scsiio.kern_data_len = beio->io_len;
1594264886Smav	io->scsiio.kern_data_resid = 0;
1595264886Smav	io->scsiio.kern_sg_entries = beio->num_segs;
1596264886Smav	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1597229997Sken
1598229997Sken	/*
1599229997Sken	 * For the read case, we need to read the data into our buffers and
1600229997Sken	 * then we can send it back to the user.  For the write case, we
1601229997Sken	 * need to get the data from the user first.
1602229997Sken	 */
1603229997Sken	if (beio->bio_cmd == BIO_READ) {
1604229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1605229997Sken		be_lun->dispatch(be_lun, beio);
1606229997Sken	} else {
1607229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1608229997Sken#ifdef CTL_TIME_IO
1609229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1610229997Sken#endif
1611229997Sken		ctl_datamove(io);
1612229997Sken	}
1613229997Sken}
1614229997Sken
1615229997Skenstatic void
1616229997Skenctl_be_block_worker(void *context, int pending)
1617229997Sken{
1618229997Sken	struct ctl_be_block_lun *be_lun;
1619229997Sken	struct ctl_be_block_softc *softc;
1620229997Sken	union ctl_io *io;
1621229997Sken
1622229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1623229997Sken	softc = be_lun->softc;
1624229997Sken
1625229997Sken	DPRINTF("entered\n");
1626229997Sken
1627267877Smav	mtx_lock(&be_lun->queue_lock);
1628229997Sken	for (;;) {
1629229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1630229997Sken		if (io != NULL) {
1631229997Sken			struct ctl_be_block_io *beio;
1632229997Sken
1633229997Sken			DPRINTF("datamove queue\n");
1634229997Sken
1635229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1636229997Sken				      ctl_io_hdr, links);
1637229997Sken
1638267877Smav			mtx_unlock(&be_lun->queue_lock);
1639229997Sken
1640267519Smav			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1641229997Sken
1642229997Sken			be_lun->dispatch(be_lun, beio);
1643229997Sken
1644267877Smav			mtx_lock(&be_lun->queue_lock);
1645229997Sken			continue;
1646229997Sken		}
1647229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1648229997Sken		if (io != NULL) {
1649229997Sken			DPRINTF("config write queue\n");
1650229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1651229997Sken				      ctl_io_hdr, links);
1652267877Smav			mtx_unlock(&be_lun->queue_lock);
1653229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1654267877Smav			mtx_lock(&be_lun->queue_lock);
1655229997Sken			continue;
1656229997Sken		}
1657275474Smav		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1658275474Smav		if (io != NULL) {
1659275474Smav			DPRINTF("config read queue\n");
1660275474Smav			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1661275474Smav				      ctl_io_hdr, links);
1662275474Smav			mtx_unlock(&be_lun->queue_lock);
1663275474Smav			ctl_be_block_cr_dispatch(be_lun, io);
1664275474Smav			mtx_lock(&be_lun->queue_lock);
1665275474Smav			continue;
1666275474Smav		}
1667229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1668229997Sken		if (io != NULL) {
1669229997Sken			DPRINTF("input queue\n");
1670229997Sken
1671229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1672229997Sken				      ctl_io_hdr, links);
1673267877Smav			mtx_unlock(&be_lun->queue_lock);
1674229997Sken
1675229997Sken			/*
1676229997Sken			 * We must drop the lock, since this routine and
1677229997Sken			 * its children may sleep.
1678229997Sken			 */
1679229997Sken			ctl_be_block_dispatch(be_lun, io);
1680229997Sken
1681267877Smav			mtx_lock(&be_lun->queue_lock);
1682229997Sken			continue;
1683229997Sken		}
1684229997Sken
1685229997Sken		/*
1686229997Sken		 * If we get here, there is no work left in the queues, so
1687229997Sken		 * just break out and let the task queue go to sleep.
1688229997Sken		 */
1689229997Sken		break;
1690229997Sken	}
1691267877Smav	mtx_unlock(&be_lun->queue_lock);
1692229997Sken}
1693229997Sken
1694229997Sken/*
1695229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1696229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1697229997Sken * thread.
1698229997Sken */
1699229997Skenstatic int
1700229997Skenctl_be_block_submit(union ctl_io *io)
1701229997Sken{
1702229997Sken	struct ctl_be_block_lun *be_lun;
1703287499Smav	struct ctl_be_lun *cbe_lun;
1704229997Sken
1705229997Sken	DPRINTF("entered\n");
1706229997Sken
1707287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1708229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1709287499Smav	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
1710229997Sken
1711229997Sken	/*
1712229997Sken	 * Make sure we only get SCSI I/O.
1713229997Sken	 */
1714229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1715229997Sken		"%#x) encountered", io->io_hdr.io_type));
1716229997Sken
1717267519Smav	PRIV(io)->len = 0;
1718267519Smav
1719267877Smav	mtx_lock(&be_lun->queue_lock);
1720229997Sken	/*
1721229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1722229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1723229997Sken	 * or deal with resource allocation here.
1724229997Sken	 */
1725229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1726267877Smav	mtx_unlock(&be_lun->queue_lock);
1727229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1728229997Sken
1729267514Smav	return (CTL_RETVAL_COMPLETE);
1730229997Sken}
1731229997Sken
1732229997Skenstatic int
1733229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1734229997Sken			int flag, struct thread *td)
1735229997Sken{
1736229997Sken	struct ctl_be_block_softc *softc;
1737229997Sken	int error;
1738229997Sken
1739229997Sken	softc = &backend_block_softc;
1740229997Sken
1741229997Sken	error = 0;
1742229997Sken
1743229997Sken	switch (cmd) {
1744229997Sken	case CTL_LUN_REQ: {
1745229997Sken		struct ctl_lun_req *lun_req;
1746229997Sken
1747229997Sken		lun_req = (struct ctl_lun_req *)addr;
1748229997Sken
1749229997Sken		switch (lun_req->reqtype) {
1750229997Sken		case CTL_LUNREQ_CREATE:
1751229997Sken			error = ctl_be_block_create(softc, lun_req);
1752229997Sken			break;
1753229997Sken		case CTL_LUNREQ_RM:
1754229997Sken			error = ctl_be_block_rm(softc, lun_req);
1755229997Sken			break;
1756232604Strasz		case CTL_LUNREQ_MODIFY:
1757232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1758232604Strasz			break;
1759229997Sken		default:
1760229997Sken			lun_req->status = CTL_LUN_ERROR;
1761229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1762272911Smav				 "invalid LUN request type %d",
1763229997Sken				 lun_req->reqtype);
1764229997Sken			break;
1765229997Sken		}
1766229997Sken		break;
1767229997Sken	}
1768229997Sken	default:
1769229997Sken		error = ENOTTY;
1770229997Sken		break;
1771229997Sken	}
1772229997Sken
1773229997Sken	return (error);
1774229997Sken}
1775229997Sken
1776229997Skenstatic int
1777229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1778229997Sken{
1779287499Smav	struct ctl_be_lun *cbe_lun;
1780229997Sken	struct ctl_be_block_filedata *file_data;
1781229997Sken	struct ctl_lun_create_params *params;
1782275865Smav	char			     *value;
1783229997Sken	struct vattr		      vattr;
1784275865Smav	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1785229997Sken	int			      error;
1786229997Sken
1787229997Sken	error = 0;
1788287499Smav	cbe_lun = &be_lun->cbe_lun;
1789229997Sken	file_data = &be_lun->backend.file;
1790272911Smav	params = &be_lun->params;
1791229997Sken
1792229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1793229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1794229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1795275474Smav	be_lun->get_lba_status = ctl_be_block_gls_file;
1796275481Smav	be_lun->getattr = ctl_be_block_getattr_file;
1797287499Smav	be_lun->unmap = NULL;
1798287499Smav	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1799229997Sken
1800229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1801229997Sken	if (error != 0) {
1802229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1803229997Sken			 "error calling VOP_GETATTR() for file %s",
1804229997Sken			 be_lun->dev_path);
1805229997Sken		return (error);
1806229997Sken	}
1807229997Sken
1808229997Sken	/*
1809229997Sken	 * Verify that we have the ability to upgrade to exclusive
1810229997Sken	 * access on this file so we can trap errors at open instead
1811229997Sken	 * of reporting them during first access.
1812229997Sken	 */
1813229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1814229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1815229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1816229997Sken			error = EBADF;
1817229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1818229997Sken				 "error locking file %s", be_lun->dev_path);
1819229997Sken			return (error);
1820229997Sken		}
1821229997Sken	}
1822229997Sken
1823229997Sken	file_data->cred = crhold(curthread->td_ucred);
1824232604Strasz	if (params->lun_size_bytes != 0)
1825232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1826232604Strasz	else
1827232604Strasz		be_lun->size_bytes = vattr.va_size;
1828229997Sken
1829229997Sken	/*
1830273029Smav	 * For files we can use any logical block size.  Prefer 512 bytes
1831273029Smav	 * for compatibility reasons.  If file's vattr.va_blocksize
1832273029Smav	 * (preferred I/O block size) is bigger and multiple to chosen
1833273029Smav	 * logical block size -- report it as physical block size.
1834229997Sken	 */
1835229997Sken	if (params->blocksize_bytes != 0)
1836287499Smav		cbe_lun->blocksize = params->blocksize_bytes;
1837229997Sken	else
1838287499Smav		cbe_lun->blocksize = 512;
1839287499Smav	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1840287499Smav	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1841287499Smav	    0 : (be_lun->size_blocks - 1);
1842275865Smav
1843275865Smav	us = ps = vattr.va_blocksize;
1844275865Smav	uo = po = 0;
1845275865Smav
1846287499Smav	value = ctl_get_opt(&cbe_lun->options, "pblocksize");
1847275865Smav	if (value != NULL)
1848275865Smav		ctl_expand_number(value, &ps);
1849287499Smav	value = ctl_get_opt(&cbe_lun->options, "pblockoffset");
1850275865Smav	if (value != NULL)
1851275865Smav		ctl_expand_number(value, &po);
1852287499Smav	pss = ps / cbe_lun->blocksize;
1853287499Smav	pos = po / cbe_lun->blocksize;
1854287499Smav	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
1855287499Smav	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
1856287499Smav		cbe_lun->pblockexp = fls(pss) - 1;
1857287499Smav		cbe_lun->pblockoff = (pss - pos) % pss;
1858273029Smav	}
1859229997Sken
1860287499Smav	value = ctl_get_opt(&cbe_lun->options, "ublocksize");
1861275865Smav	if (value != NULL)
1862275865Smav		ctl_expand_number(value, &us);
1863287499Smav	value = ctl_get_opt(&cbe_lun->options, "ublockoffset");
1864275865Smav	if (value != NULL)
1865275865Smav		ctl_expand_number(value, &uo);
1866287499Smav	uss = us / cbe_lun->blocksize;
1867287499Smav	uos = uo / cbe_lun->blocksize;
1868287499Smav	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
1869287499Smav	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
1870287499Smav		cbe_lun->ublockexp = fls(uss) - 1;
1871287499Smav		cbe_lun->ublockoff = (uss - uos) % uss;
1872275865Smav	}
1873275865Smav
1874229997Sken	/*
1875229997Sken	 * Sanity check.  The media size has to be at least one
1876229997Sken	 * sector long.
1877229997Sken	 */
1878287499Smav	if (be_lun->size_bytes < cbe_lun->blocksize) {
1879229997Sken		error = EINVAL;
1880229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1881229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1882287499Smav			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1883229997Sken	}
1884275920Smav
1885287499Smav	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1886229997Sken	return (error);
1887229997Sken}
1888229997Sken
1889229997Skenstatic int
1890229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1891229997Sken{
1892287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1893229997Sken	struct ctl_lun_create_params *params;
1894287664Smav	struct cdevsw		     *csw;
1895229997Sken	struct cdev		     *dev;
1896275865Smav	char			     *value;
1897287664Smav	int			      error, atomic, maxio, ref, unmap, tmp;
1898287221Smav	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1899229997Sken
1900272911Smav	params = &be_lun->params;
1901229997Sken
1902229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1903287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1904287664Smav	if (csw == NULL)
1905287664Smav		return (ENXIO);
1906287664Smav	if (strcmp(csw->d_name, "zvol") == 0) {
1907269123Smav		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1908275474Smav		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1909275920Smav		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1910275920Smav	} else {
1911269123Smav		be_lun->dispatch = ctl_be_block_dispatch_dev;
1912287499Smav		be_lun->get_lba_status = NULL;
1913275920Smav		atomic = 0;
1914287664Smav		maxio = dev->si_iosize_max;
1915275920Smav		if (maxio <= 0)
1916275920Smav			maxio = DFLTPHYS;
1917275920Smav		if (maxio > CTLBLK_MAX_IO_SIZE)
1918275920Smav			maxio = CTLBLK_MAX_IO_SIZE;
1919275920Smav	}
1920269123Smav	be_lun->lun_flush = ctl_be_block_flush_dev;
1921274154Smav	be_lun->getattr = ctl_be_block_getattr_dev;
1922287499Smav	be_lun->unmap = ctl_be_block_unmap_dev;
1923229997Sken
1924287664Smav	if (!csw->d_ioctl) {
1925287664Smav		dev_relthread(dev, ref);
1926229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1927287664Smav			 "no d_ioctl for device %s!", be_lun->dev_path);
1928229997Sken		return (ENODEV);
1929229997Sken	}
1930229997Sken
1931287664Smav	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1932229997Sken			       curthread);
1933229997Sken	if (error) {
1934287664Smav		dev_relthread(dev, ref);
1935229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1936272911Smav			 "error %d returned for DIOCGSECTORSIZE ioctl "
1937272911Smav			 "on %s!", error, be_lun->dev_path);
1938229997Sken		return (error);
1939229997Sken	}
1940229997Sken
1941229997Sken	/*
1942229997Sken	 * If the user has asked for a blocksize that is greater than the
1943229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1944229997Sken	 * the user is asking for is an even multiple of the underlying
1945229997Sken	 * device's blocksize.
1946229997Sken	 */
1947286811Smav	if ((params->blocksize_bytes != 0) &&
1948286811Smav	    (params->blocksize_bytes >= tmp)) {
1949286811Smav		if (params->blocksize_bytes % tmp == 0) {
1950287499Smav			cbe_lun->blocksize = params->blocksize_bytes;
1951229997Sken		} else {
1952287664Smav			dev_relthread(dev, ref);
1953229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1954272911Smav				 "requested blocksize %u is not an even "
1955229997Sken				 "multiple of backing device blocksize %u",
1956287221Smav				 params->blocksize_bytes, tmp);
1957229997Sken			return (EINVAL);
1958229997Sken		}
1959286811Smav	} else if (params->blocksize_bytes != 0) {
1960287664Smav		dev_relthread(dev, ref);
1961229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1962272911Smav			 "requested blocksize %u < backing device "
1963287221Smav			 "blocksize %u", params->blocksize_bytes, tmp);
1964229997Sken		return (EINVAL);
1965286811Smav	} else
1966287499Smav		cbe_lun->blocksize = tmp;
1967229997Sken
1968287664Smav	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
1969287664Smav			     curthread);
1970229997Sken	if (error) {
1971287664Smav		dev_relthread(dev, ref);
1972229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1973272911Smav			 "error %d returned for DIOCGMEDIASIZE "
1974272911Smav			 " ioctl on %s!", error,
1975232604Strasz			 be_lun->dev_path);
1976229997Sken		return (error);
1977229997Sken	}
1978229997Sken
1979232604Strasz	if (params->lun_size_bytes != 0) {
1980287221Smav		if (params->lun_size_bytes > otmp) {
1981287664Smav			dev_relthread(dev, ref);
1982232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1983272911Smav				 "requested LUN size %ju > backing device "
1984272911Smav				 "size %ju",
1985232604Strasz				 (uintmax_t)params->lun_size_bytes,
1986287221Smav				 (uintmax_t)otmp);
1987232604Strasz			return (EINVAL);
1988232604Strasz		}
1989232604Strasz
1990232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1991286811Smav	} else
1992287221Smav		be_lun->size_bytes = otmp;
1993287499Smav	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
1994287499Smav	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
1995287499Smav	    0 : (be_lun->size_blocks - 1);
1996232604Strasz
1997287664Smav	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
1998287664Smav	    curthread);
1999264191Smav	if (error)
2000264191Smav		ps = po = 0;
2001264191Smav	else {
2002287664Smav		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
2003287664Smav		    FREAD, curthread);
2004264191Smav		if (error)
2005264191Smav			po = 0;
2006264191Smav	}
2007275865Smav	us = ps;
2008275865Smav	uo = po;
2009275865Smav
2010287499Smav	value = ctl_get_opt(&cbe_lun->options, "pblocksize");
2011275865Smav	if (value != NULL)
2012275865Smav		ctl_expand_number(value, &ps);
2013287499Smav	value = ctl_get_opt(&cbe_lun->options, "pblockoffset");
2014275865Smav	if (value != NULL)
2015275865Smav		ctl_expand_number(value, &po);
2016287499Smav	pss = ps / cbe_lun->blocksize;
2017287499Smav	pos = po / cbe_lun->blocksize;
2018287499Smav	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
2019287499Smav	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
2020287499Smav		cbe_lun->pblockexp = fls(pss) - 1;
2021287499Smav		cbe_lun->pblockoff = (pss - pos) % pss;
2022264191Smav	}
2023264191Smav
2024287499Smav	value = ctl_get_opt(&cbe_lun->options, "ublocksize");
2025275865Smav	if (value != NULL)
2026275865Smav		ctl_expand_number(value, &us);
2027287499Smav	value = ctl_get_opt(&cbe_lun->options, "ublockoffset");
2028275865Smav	if (value != NULL)
2029275865Smav		ctl_expand_number(value, &uo);
2030287499Smav	uss = us / cbe_lun->blocksize;
2031287499Smav	uos = uo / cbe_lun->blocksize;
2032287499Smav	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
2033287499Smav	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
2034287499Smav		cbe_lun->ublockexp = fls(uss) - 1;
2035287499Smav		cbe_lun->ublockoff = (uss - uos) % uss;
2036275865Smav	}
2037275865Smav
2038287499Smav	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
2039287499Smav	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2040278672Smav
2041278672Smav	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2042278672Smav		unmap = 1;
2043278672Smav	} else {
2044278672Smav		struct diocgattr_arg	arg;
2045278672Smav
2046278672Smav		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2047278672Smav		arg.len = sizeof(arg.value.i);
2048287664Smav		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
2049287664Smav		    curthread);
2050278672Smav		unmap = (error == 0) ? arg.value.i : 0;
2051278672Smav	}
2052287499Smav	value = ctl_get_opt(&cbe_lun->options, "unmap");
2053278672Smav	if (value != NULL)
2054278672Smav		unmap = (strcmp(value, "on") == 0);
2055278672Smav	if (unmap)
2056287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
2057287499Smav	else
2058287499Smav		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2059278672Smav
2060287664Smav	dev_relthread(dev, ref);
2061229997Sken	return (0);
2062229997Sken}
2063229997Sken
2064229997Skenstatic int
2065229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
2066229997Sken{
2067287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2068287499Smav	int flags;
2069287499Smav
2070229997Sken	if (be_lun->vn) {
2071287499Smav		flags = FREAD;
2072287499Smav		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
2073287499Smav			flags |= FWRITE;
2074229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2075229997Sken		be_lun->vn = NULL;
2076229997Sken
2077229997Sken		switch (be_lun->dev_type) {
2078229997Sken		case CTL_BE_BLOCK_DEV:
2079229997Sken			break;
2080229997Sken		case CTL_BE_BLOCK_FILE:
2081229997Sken			if (be_lun->backend.file.cred != NULL) {
2082229997Sken				crfree(be_lun->backend.file.cred);
2083229997Sken				be_lun->backend.file.cred = NULL;
2084229997Sken			}
2085229997Sken			break;
2086229997Sken		case CTL_BE_BLOCK_NONE:
2087258871Strasz			break;
2088229997Sken		default:
2089229997Sken			panic("Unexpected backend type.");
2090229997Sken			break;
2091229997Sken		}
2092272911Smav		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2093229997Sken	}
2094229997Sken	return (0);
2095229997Sken}
2096229997Sken
2097229997Skenstatic int
2098229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
2099287499Smav		  struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2100229997Sken{
2101287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2102229997Sken	struct nameidata nd;
2103287499Smav	char		*value;
2104287499Smav	int		 error, flags;
2105229997Sken
2106229997Sken	error = 0;
2107229997Sken	if (rootvnode == NULL) {
2108229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2109272911Smav			 "Root filesystem is not mounted");
2110229997Sken		return (1);
2111229997Sken	}
2112285391Smjg	pwd_ensure_dirs();
2113229997Sken
2114287499Smav	value = ctl_get_opt(&cbe_lun->options, "file");
2115287499Smav	if (value == NULL) {
2116287499Smav		snprintf(req->error_str, sizeof(req->error_str),
2117287499Smav			 "no file argument specified");
2118287499Smav		return (1);
2119287499Smav	}
2120287499Smav	free(be_lun->dev_path, M_CTLBLK);
2121287499Smav	be_lun->dev_path = strdup(value, M_CTLBLK);
2122287499Smav
2123287499Smav	flags = FREAD;
2124287499Smav	value = ctl_get_opt(&cbe_lun->options, "readonly");
2125287499Smav	if (value == NULL || strcmp(value, "on") != 0)
2126287499Smav		flags |= FWRITE;
2127287499Smav
2128287499Smavagain:
2129229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2130229997Sken	error = vn_open(&nd, &flags, 0, NULL);
2131287499Smav	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
2132287499Smav		flags &= ~FWRITE;
2133287499Smav		goto again;
2134287499Smav	}
2135229997Sken	if (error) {
2136229997Sken		/*
2137229997Sken		 * This is the only reasonable guess we can make as far as
2138229997Sken		 * path if the user doesn't give us a fully qualified path.
2139229997Sken		 * If they want to specify a file, they need to specify the
2140229997Sken		 * full path.
2141229997Sken		 */
2142229997Sken		if (be_lun->dev_path[0] != '/') {
2143229997Sken			char *dev_name;
2144229997Sken
2145287499Smav			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2146287499Smav				be_lun->dev_path);
2147287499Smav			free(be_lun->dev_path, M_CTLBLK);
2148287499Smav			be_lun->dev_path = dev_name;
2149287499Smav			goto again;
2150229997Sken		}
2151229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2152272911Smav		    "error opening %s: %d", be_lun->dev_path, error);
2153229997Sken		return (error);
2154229997Sken	}
2155287499Smav	if (flags & FWRITE)
2156287499Smav		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
2157287499Smav	else
2158287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2159229997Sken
2160229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
2161229997Sken	be_lun->vn = nd.ni_vp;
2162229997Sken
2163229997Sken	/* We only support disks and files. */
2164229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
2165229997Sken		error = ctl_be_block_open_dev(be_lun, req);
2166229997Sken	} else if (be_lun->vn->v_type == VREG) {
2167229997Sken		error = ctl_be_block_open_file(be_lun, req);
2168229997Sken	} else {
2169229997Sken		error = EINVAL;
2170229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2171258871Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
2172229997Sken	}
2173229997Sken	VOP_UNLOCK(be_lun->vn, 0);
2174229997Sken
2175286811Smav	if (error != 0)
2176229997Sken		ctl_be_block_close(be_lun);
2177287499Smav	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2178287499Smav	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2179287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2180287499Smav	value = ctl_get_opt(&cbe_lun->options, "serseq");
2181287499Smav	if (value != NULL && strcmp(value, "on") == 0)
2182287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
2183287499Smav	else if (value != NULL && strcmp(value, "read") == 0)
2184287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2185287499Smav	else if (value != NULL && strcmp(value, "off") == 0)
2186287499Smav		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2187229997Sken	return (0);
2188229997Sken}
2189229997Sken
2190229997Skenstatic int
2191229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2192229997Sken{
2193287499Smav	struct ctl_be_lun *cbe_lun;
2194229997Sken	struct ctl_be_block_lun *be_lun;
2195229997Sken	struct ctl_lun_create_params *params;
2196267481Smav	char num_thread_str[16];
2197229997Sken	char tmpstr[32];
2198267481Smav	char *value;
2199278672Smav	int retval, num_threads;
2200267481Smav	int tmp_num_threads;
2201229997Sken
2202229997Sken	params = &req->reqdata.create;
2203229997Sken	retval = 0;
2204272911Smav	req->status = CTL_LUN_OK;
2205229997Sken
2206229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
2207287499Smav	cbe_lun = &be_lun->cbe_lun;
2208287499Smav	cbe_lun->be_lun = be_lun;
2209272911Smav	be_lun->params = req->reqdata.create;
2210229997Sken	be_lun->softc = softc;
2211229997Sken	STAILQ_INIT(&be_lun->input_queue);
2212275474Smav	STAILQ_INIT(&be_lun->config_read_queue);
2213229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
2214229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
2215229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
2216267877Smav	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
2217267877Smav	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
2218287499Smav	ctl_init_opts(&cbe_lun->options,
2219268280Smav	    req->num_be_args, req->kern_be_args);
2220264886Smav	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
2221256995Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2222229997Sken	if (be_lun->lun_zone == NULL) {
2223229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2224272911Smav			 "error allocating UMA zone");
2225229997Sken		goto bailout_error;
2226229997Sken	}
2227229997Sken
2228229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
2229287499Smav		cbe_lun->lun_type = params->device_type;
2230229997Sken	else
2231287499Smav		cbe_lun->lun_type = T_DIRECT;
2232287499Smav	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
2233287621Smav	cbe_lun->flags = 0;
2234287621Smav	value = ctl_get_opt(&cbe_lun->options, "ha_role");
2235287621Smav	if (value != NULL) {
2236287621Smav		if (strcmp(value, "primary") == 0)
2237287621Smav			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2238287621Smav	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2239287621Smav		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2240229997Sken
2241287499Smav	if (cbe_lun->lun_type == T_DIRECT) {
2242286811Smav		be_lun->size_bytes = params->lun_size_bytes;
2243286811Smav		if (params->blocksize_bytes != 0)
2244287499Smav			cbe_lun->blocksize = params->blocksize_bytes;
2245286811Smav		else
2246287499Smav			cbe_lun->blocksize = 512;
2247287499Smav		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2248287499Smav		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2249287499Smav		    0 : (be_lun->size_blocks - 1);
2250229997Sken
2251287621Smav		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2252287621Smav		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2253287621Smav			retval = ctl_be_block_open(softc, be_lun, req);
2254287621Smav			if (retval != 0) {
2255287621Smav				retval = 0;
2256287621Smav				req->status = CTL_LUN_WARNING;
2257287621Smav			}
2258229997Sken		}
2259287499Smav		num_threads = cbb_num_threads;
2260229997Sken	} else {
2261229997Sken		num_threads = 1;
2262229997Sken	}
2263229997Sken
2264229997Sken	/*
2265229997Sken	 * XXX This searching loop might be refactored to be combined with
2266229997Sken	 * the loop above,
2267229997Sken	 */
2268287499Smav	value = ctl_get_opt(&cbe_lun->options, "num_threads");
2269267481Smav	if (value != NULL) {
2270267481Smav		tmp_num_threads = strtol(value, NULL, 0);
2271229997Sken
2272267481Smav		/*
2273267481Smav		 * We don't let the user specify less than one
2274267481Smav		 * thread, but hope he's clueful enough not to
2275267481Smav		 * specify 1000 threads.
2276267481Smav		 */
2277267481Smav		if (tmp_num_threads < 1) {
2278267481Smav			snprintf(req->error_str, sizeof(req->error_str),
2279272911Smav				 "invalid number of threads %s",
2280272911Smav				 num_thread_str);
2281267481Smav			goto bailout_error;
2282229997Sken		}
2283267481Smav		num_threads = tmp_num_threads;
2284229997Sken	}
2285229997Sken
2286272911Smav	if (be_lun->vn == NULL)
2287287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE;
2288229997Sken	/* Tell the user the blocksize we ended up using */
2289272911Smav	params->lun_size_bytes = be_lun->size_bytes;
2290287499Smav	params->blocksize_bytes = cbe_lun->blocksize;
2291229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
2292287499Smav		cbe_lun->req_lun_id = params->req_lun_id;
2293287499Smav		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2294229997Sken	} else
2295287499Smav		cbe_lun->req_lun_id = 0;
2296229997Sken
2297287499Smav	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
2298287499Smav	cbe_lun->lun_config_status = ctl_be_block_lun_config_status;
2299287499Smav	cbe_lun->be = &ctl_be_block_driver;
2300229997Sken
2301229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2302229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2303229997Sken			 softc->num_luns);
2304287499Smav		strncpy((char *)cbe_lun->serial_num, tmpstr,
2305287499Smav			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2306229997Sken
2307229997Sken		/* Tell the user what we used for a serial number */
2308229997Sken		strncpy((char *)params->serial_num, tmpstr,
2309275953Smav			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2310229997Sken	} else {
2311287499Smav		strncpy((char *)cbe_lun->serial_num, params->serial_num,
2312287499Smav			MIN(sizeof(cbe_lun->serial_num),
2313229997Sken			sizeof(params->serial_num)));
2314229997Sken	}
2315229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2316229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
2317287499Smav		strncpy((char *)cbe_lun->device_id, tmpstr,
2318287499Smav			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2319229997Sken
2320229997Sken		/* Tell the user what we used for a device ID */
2321229997Sken		strncpy((char *)params->device_id, tmpstr,
2322275953Smav			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2323229997Sken	} else {
2324287499Smav		strncpy((char *)cbe_lun->device_id, params->device_id,
2325287499Smav			MIN(sizeof(cbe_lun->device_id),
2326275953Smav			    sizeof(params->device_id)));
2327229997Sken	}
2328229997Sken
2329229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2330229997Sken
2331229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2332229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2333229997Sken
2334229997Sken	if (be_lun->io_taskqueue == NULL) {
2335229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2336272911Smav			 "unable to create taskqueue");
2337229997Sken		goto bailout_error;
2338229997Sken	}
2339229997Sken
2340229997Sken	/*
2341229997Sken	 * Note that we start the same number of threads by default for
2342229997Sken	 * both the file case and the block device case.  For the file
2343229997Sken	 * case, we need multiple threads to allow concurrency, because the
2344229997Sken	 * vnode interface is designed to be a blocking interface.  For the
2345229997Sken	 * block device case, ZFS zvols at least will block the caller's
2346229997Sken	 * context in many instances, and so we need multiple threads to
2347229997Sken	 * overcome that problem.  Other block devices don't need as many
2348229997Sken	 * threads, but they shouldn't cause too many problems.
2349229997Sken	 *
2350229997Sken	 * If the user wants to just have a single thread for a block
2351229997Sken	 * device, he can specify that when the LUN is created, or change
2352229997Sken	 * the tunable/sysctl to alter the default number of threads.
2353229997Sken	 */
2354229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2355229997Sken					 /*num threads*/num_threads,
2356229997Sken					 /*priority*/PWAIT,
2357229997Sken					 /*thread name*/
2358229997Sken					 "%s taskq", be_lun->lunname);
2359229997Sken
2360229997Sken	if (retval != 0)
2361229997Sken		goto bailout_error;
2362229997Sken
2363229997Sken	be_lun->num_threads = num_threads;
2364229997Sken
2365229997Sken	mtx_lock(&softc->lock);
2366229997Sken	softc->num_luns++;
2367229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2368229997Sken
2369229997Sken	mtx_unlock(&softc->lock);
2370229997Sken
2371287499Smav	retval = ctl_add_lun(&be_lun->cbe_lun);
2372229997Sken	if (retval != 0) {
2373229997Sken		mtx_lock(&softc->lock);
2374229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2375229997Sken			      links);
2376229997Sken		softc->num_luns--;
2377229997Sken		mtx_unlock(&softc->lock);
2378229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2379272911Smav			 "ctl_add_lun() returned error %d, see dmesg for "
2380272911Smav			 "details", retval);
2381229997Sken		retval = 0;
2382229997Sken		goto bailout_error;
2383229997Sken	}
2384229997Sken
2385229997Sken	mtx_lock(&softc->lock);
2386229997Sken
2387229997Sken	/*
2388229997Sken	 * Tell the config_status routine that we're waiting so it won't
2389229997Sken	 * clean up the LUN in the event of an error.
2390229997Sken	 */
2391229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2392229997Sken
2393229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2394229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2395229997Sken		if (retval == EINTR)
2396229997Sken			break;
2397229997Sken	}
2398229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2399229997Sken
2400229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2401229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2402272911Smav			 "LUN configuration error, see dmesg for details");
2403229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2404229997Sken			      links);
2405229997Sken		softc->num_luns--;
2406229997Sken		mtx_unlock(&softc->lock);
2407229997Sken		goto bailout_error;
2408229997Sken	} else {
2409287499Smav		params->req_lun_id = cbe_lun->lun_id;
2410229997Sken	}
2411229997Sken
2412229997Sken	mtx_unlock(&softc->lock);
2413229997Sken
2414229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2415287499Smav					       cbe_lun->blocksize,
2416229997Sken					       DEVSTAT_ALL_SUPPORTED,
2417287499Smav					       cbe_lun->lun_type
2418229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
2419229997Sken					       DEVSTAT_PRIORITY_OTHER);
2420229997Sken
2421229997Sken	return (retval);
2422229997Sken
2423229997Skenbailout_error:
2424229997Sken	req->status = CTL_LUN_ERROR;
2425229997Sken
2426267429Smav	if (be_lun->io_taskqueue != NULL)
2427267429Smav		taskqueue_free(be_lun->io_taskqueue);
2428229997Sken	ctl_be_block_close(be_lun);
2429267429Smav	if (be_lun->dev_path != NULL)
2430267429Smav		free(be_lun->dev_path, M_CTLBLK);
2431267429Smav	if (be_lun->lun_zone != NULL)
2432267429Smav		uma_zdestroy(be_lun->lun_zone);
2433287499Smav	ctl_free_opts(&cbe_lun->options);
2434267877Smav	mtx_destroy(&be_lun->queue_lock);
2435267877Smav	mtx_destroy(&be_lun->io_lock);
2436229997Sken	free(be_lun, M_CTLBLK);
2437229997Sken
2438229997Sken	return (retval);
2439229997Sken}
2440229997Sken
2441229997Skenstatic int
2442229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2443229997Sken{
2444229997Sken	struct ctl_lun_rm_params *params;
2445229997Sken	struct ctl_be_block_lun *be_lun;
2446229997Sken	int retval;
2447229997Sken
2448229997Sken	params = &req->reqdata.rm;
2449229997Sken
2450229997Sken	mtx_lock(&softc->lock);
2451229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2452287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
2453229997Sken			break;
2454229997Sken	}
2455229997Sken	mtx_unlock(&softc->lock);
2456229997Sken
2457229997Sken	if (be_lun == NULL) {
2458229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2459272911Smav			 "LUN %u is not managed by the block backend",
2460272911Smav			 params->lun_id);
2461229997Sken		goto bailout_error;
2462229997Sken	}
2463229997Sken
2464287499Smav	retval = ctl_disable_lun(&be_lun->cbe_lun);
2465229997Sken
2466229997Sken	if (retval != 0) {
2467229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2468272911Smav			 "error %d returned from ctl_disable_lun() for "
2469272911Smav			 "LUN %d", retval, params->lun_id);
2470229997Sken		goto bailout_error;
2471229997Sken
2472229997Sken	}
2473229997Sken
2474287499Smav	retval = ctl_invalidate_lun(&be_lun->cbe_lun);
2475229997Sken	if (retval != 0) {
2476229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2477272911Smav			 "error %d returned from ctl_invalidate_lun() for "
2478272911Smav			 "LUN %d", retval, params->lun_id);
2479229997Sken		goto bailout_error;
2480229997Sken	}
2481229997Sken
2482229997Sken	mtx_lock(&softc->lock);
2483229997Sken
2484229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2485229997Sken
2486229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2487229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2488229997Sken                if (retval == EINTR)
2489229997Sken                        break;
2490229997Sken        }
2491229997Sken
2492229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2493229997Sken
2494229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2495229997Sken		snprintf(req->error_str, sizeof(req->error_str),
2496272911Smav			 "interrupted waiting for LUN to be freed");
2497229997Sken		mtx_unlock(&softc->lock);
2498229997Sken		goto bailout_error;
2499229997Sken	}
2500229997Sken
2501229997Sken	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2502229997Sken
2503229997Sken	softc->num_luns--;
2504229997Sken	mtx_unlock(&softc->lock);
2505229997Sken
2506229997Sken	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2507229997Sken
2508229997Sken	taskqueue_free(be_lun->io_taskqueue);
2509229997Sken
2510229997Sken	ctl_be_block_close(be_lun);
2511229997Sken
2512229997Sken	if (be_lun->disk_stats != NULL)
2513229997Sken		devstat_remove_entry(be_lun->disk_stats);
2514229997Sken
2515229997Sken	uma_zdestroy(be_lun->lun_zone);
2516229997Sken
2517287499Smav	ctl_free_opts(&be_lun->cbe_lun.options);
2518229997Sken	free(be_lun->dev_path, M_CTLBLK);
2519267877Smav	mtx_destroy(&be_lun->queue_lock);
2520267877Smav	mtx_destroy(&be_lun->io_lock);
2521229997Sken	free(be_lun, M_CTLBLK);
2522229997Sken
2523229997Sken	req->status = CTL_LUN_OK;
2524229997Sken
2525229997Sken	return (0);
2526229997Sken
2527229997Skenbailout_error:
2528229997Sken
2529229997Sken	req->status = CTL_LUN_ERROR;
2530229997Sken
2531229997Sken	return (0);
2532229997Sken}
2533229997Sken
2534232604Straszstatic int
2535232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
2536232604Strasz			 struct ctl_lun_req *req)
2537232604Strasz{
2538287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2539232604Strasz	struct vattr vattr;
2540232604Strasz	int error;
2541272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2542232604Strasz
2543232604Strasz	if (params->lun_size_bytes != 0) {
2544232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2545232604Strasz	} else  {
2546271794Smav		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2547232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2548271794Smav		VOP_UNLOCK(be_lun->vn, 0);
2549232604Strasz		if (error != 0) {
2550232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2551232604Strasz				 "error calling VOP_GETATTR() for file %s",
2552232604Strasz				 be_lun->dev_path);
2553232604Strasz			return (error);
2554232604Strasz		}
2555232604Strasz		be_lun->size_bytes = vattr.va_size;
2556232604Strasz	}
2557287499Smav	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2558287499Smav	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2559287499Smav	    0 : (be_lun->size_blocks - 1);
2560232604Strasz	return (0);
2561232604Strasz}
2562232604Strasz
2563232604Straszstatic int
2564232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2565232604Strasz			struct ctl_lun_req *req)
2566232604Strasz{
2567287499Smav	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2568272911Smav	struct ctl_lun_create_params *params = &be_lun->params;
2569287664Smav	struct cdevsw *csw;
2570287664Smav	struct cdev *dev;
2571232604Strasz	uint64_t size_bytes;
2572287664Smav	int error, ref;
2573232604Strasz
2574287664Smav	csw = devvn_refthread(be_lun->vn, &dev, &ref);
2575287664Smav	if (csw == NULL)
2576287664Smav		return (ENXIO);
2577287664Smav	if (csw->d_ioctl == NULL) {
2578287664Smav		dev_relthread(dev, ref);
2579232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2580272911Smav			 "no d_ioctl for device %s!", be_lun->dev_path);
2581232604Strasz		return (ENODEV);
2582232604Strasz	}
2583232604Strasz
2584287664Smav	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&size_bytes, FREAD,
2585287664Smav	    curthread);
2586287664Smav	dev_relthread(dev, ref);
2587232604Strasz	if (error) {
2588232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2589272911Smav			 "error %d returned for DIOCGMEDIASIZE ioctl "
2590272911Smav			 "on %s!", error, be_lun->dev_path);
2591232604Strasz		return (error);
2592232604Strasz	}
2593232604Strasz
2594232604Strasz	if (params->lun_size_bytes != 0) {
2595232604Strasz		if (params->lun_size_bytes > size_bytes) {
2596232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2597272911Smav				 "requested LUN size %ju > backing device "
2598272911Smav				 "size %ju",
2599232604Strasz				 (uintmax_t)params->lun_size_bytes,
2600232604Strasz				 (uintmax_t)size_bytes);
2601232604Strasz			return (EINVAL);
2602232604Strasz		}
2603232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2604232604Strasz	} else {
2605232604Strasz		be_lun->size_bytes = size_bytes;
2606232604Strasz	}
2607287499Smav	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
2608287499Smav	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
2609287499Smav	    0 : (be_lun->size_blocks - 1);
2610232604Strasz	return (0);
2611232604Strasz}
2612232604Strasz
2613232604Straszstatic int
2614232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2615232604Strasz{
2616232604Strasz	struct ctl_lun_modify_params *params;
2617232604Strasz	struct ctl_be_block_lun *be_lun;
2618287500Smav	struct ctl_be_lun *cbe_lun;
2619287621Smav	char *value;
2620271794Smav	uint64_t oldsize;
2621287621Smav	int error, wasprim;
2622232604Strasz
2623232604Strasz	params = &req->reqdata.modify;
2624232604Strasz
2625232604Strasz	mtx_lock(&softc->lock);
2626232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2627287499Smav		if (be_lun->cbe_lun.lun_id == params->lun_id)
2628232604Strasz			break;
2629232604Strasz	}
2630232604Strasz	mtx_unlock(&softc->lock);
2631232604Strasz
2632232604Strasz	if (be_lun == NULL) {
2633232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2634272911Smav			 "LUN %u is not managed by the block backend",
2635272911Smav			 params->lun_id);
2636232604Strasz		goto bailout_error;
2637232604Strasz	}
2638287500Smav	cbe_lun = &be_lun->cbe_lun;
2639232604Strasz
2640287500Smav	if (params->lun_size_bytes != 0)
2641287500Smav		be_lun->params.lun_size_bytes = params->lun_size_bytes;
2642287500Smav	ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
2643232604Strasz
2644287621Smav	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
2645287621Smav	value = ctl_get_opt(&cbe_lun->options, "ha_role");
2646287621Smav	if (value != NULL) {
2647287621Smav		if (strcmp(value, "primary") == 0)
2648287621Smav			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2649287621Smav		else
2650287621Smav			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2651287621Smav	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
2652287621Smav		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2653232604Strasz	else
2654287621Smav		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
2655287621Smav	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
2656287621Smav		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
2657287621Smav			ctl_lun_primary(cbe_lun);
2658287621Smav		else
2659287621Smav			ctl_lun_secondary(cbe_lun);
2660287621Smav	}
2661232604Strasz
2662287621Smav	oldsize = be_lun->size_blocks;
2663287621Smav	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
2664287621Smav	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2665287621Smav		if (be_lun->vn == NULL)
2666287621Smav			error = ctl_be_block_open(softc, be_lun, req);
2667287621Smav		else if (vn_isdisk(be_lun->vn, &error))
2668287621Smav			error = ctl_be_block_modify_dev(be_lun, req);
2669287621Smav		else if (be_lun->vn->v_type == VREG)
2670287621Smav			error = ctl_be_block_modify_file(be_lun, req);
2671287621Smav		else
2672287621Smav			error = EINVAL;
2673287621Smav		if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) &&
2674287621Smav		    be_lun->vn != NULL) {
2675287621Smav			cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE;
2676287621Smav			ctl_lun_online(cbe_lun);
2677287621Smav		}
2678287621Smav	} else {
2679287621Smav		if (be_lun->vn != NULL) {
2680287621Smav			cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE;
2681287621Smav			ctl_lun_offline(cbe_lun);
2682287621Smav			pause("CTL LUN offline", hz / 8);	// XXX
2683287621Smav			error = ctl_be_block_close(be_lun);
2684287621Smav		} else
2685287621Smav			error = 0;
2686287621Smav	}
2687287499Smav	if (be_lun->size_blocks != oldsize)
2688287500Smav		ctl_lun_capacity_changed(cbe_lun);
2689232604Strasz
2690232604Strasz	/* Tell the user the exact size we ended up using */
2691232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2692232604Strasz
2693272911Smav	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
2694232604Strasz	return (0);
2695232604Strasz
2696232604Straszbailout_error:
2697232604Strasz	req->status = CTL_LUN_ERROR;
2698232604Strasz	return (0);
2699232604Strasz}
2700232604Strasz
2701229997Skenstatic void
2702229997Skenctl_be_block_lun_shutdown(void *be_lun)
2703229997Sken{
2704229997Sken	struct ctl_be_block_lun *lun;
2705229997Sken	struct ctl_be_block_softc *softc;
2706229997Sken
2707229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2708229997Sken
2709229997Sken	softc = lun->softc;
2710229997Sken
2711229997Sken	mtx_lock(&softc->lock);
2712229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2713229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2714229997Sken		wakeup(lun);
2715229997Sken	mtx_unlock(&softc->lock);
2716229997Sken
2717229997Sken}
2718229997Sken
2719229997Skenstatic void
2720229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2721229997Sken{
2722229997Sken	struct ctl_be_block_lun *lun;
2723229997Sken	struct ctl_be_block_softc *softc;
2724229997Sken
2725229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2726229997Sken	softc = lun->softc;
2727229997Sken
2728229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2729229997Sken		mtx_lock(&softc->lock);
2730229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2731229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2732229997Sken			wakeup(lun);
2733229997Sken		mtx_unlock(&softc->lock);
2734229997Sken
2735229997Sken		/*
2736229997Sken		 * We successfully added the LUN, attempt to enable it.
2737229997Sken		 */
2738287499Smav		if (ctl_enable_lun(&lun->cbe_lun) != 0) {
2739229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2740287499Smav			if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
2741229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2742229997Sken				       __func__);
2743229997Sken			}
2744229997Sken		}
2745229997Sken
2746229997Sken		return;
2747229997Sken	}
2748229997Sken
2749229997Sken
2750229997Sken	mtx_lock(&softc->lock);
2751229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2752229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2753229997Sken	wakeup(lun);
2754229997Sken	mtx_unlock(&softc->lock);
2755229997Sken}
2756229997Sken
2757229997Sken
2758229997Skenstatic int
2759229997Skenctl_be_block_config_write(union ctl_io *io)
2760229997Sken{
2761229997Sken	struct ctl_be_block_lun *be_lun;
2762287499Smav	struct ctl_be_lun *cbe_lun;
2763229997Sken	int retval;
2764229997Sken
2765229997Sken	retval = 0;
2766229997Sken
2767229997Sken	DPRINTF("entered\n");
2768229997Sken
2769287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2770229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2771287499Smav	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2772229997Sken
2773229997Sken	switch (io->scsiio.cdb[0]) {
2774229997Sken	case SYNCHRONIZE_CACHE:
2775229997Sken	case SYNCHRONIZE_CACHE_16:
2776264274Smav	case WRITE_SAME_10:
2777264274Smav	case WRITE_SAME_16:
2778264274Smav	case UNMAP:
2779229997Sken		/*
2780229997Sken		 * The upper level CTL code will filter out any CDBs with
2781229997Sken		 * the immediate bit set and return the proper error.
2782229997Sken		 *
2783229997Sken		 * We don't really need to worry about what LBA range the
2784229997Sken		 * user asked to be synced out.  When they issue a sync
2785229997Sken		 * cache command, we'll sync out the whole thing.
2786229997Sken		 */
2787267877Smav		mtx_lock(&be_lun->queue_lock);
2788229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2789229997Sken				   links);
2790267877Smav		mtx_unlock(&be_lun->queue_lock);
2791229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2792229997Sken		break;
2793229997Sken	case START_STOP_UNIT: {
2794229997Sken		struct scsi_start_stop_unit *cdb;
2795229997Sken
2796229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2797229997Sken
2798229997Sken		if (cdb->how & SSS_START)
2799287499Smav			retval = ctl_start_lun(cbe_lun);
2800229997Sken		else {
2801287499Smav			retval = ctl_stop_lun(cbe_lun);
2802229997Sken			/*
2803229997Sken			 * XXX KDM Copan-specific offline behavior.
2804229997Sken			 * Figure out a reasonable way to port this?
2805229997Sken			 */
2806229997Sken#ifdef NEEDTOPORT
2807229997Sken			if ((retval == 0)
2808229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2809287499Smav				retval = ctl_lun_offline(cbe_lun);
2810229997Sken#endif
2811229997Sken		}
2812229997Sken
2813229997Sken		/*
2814229997Sken		 * In general, the above routines should not fail.  They
2815229997Sken		 * just set state for the LUN.  So we've got something
2816229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2817229997Sken		 */
2818229997Sken		if (retval != 0) {
2819229997Sken			ctl_set_internal_failure(&io->scsiio,
2820229997Sken						 /*sks_valid*/ 1,
2821229997Sken						 /*retry_count*/ 0xf051);
2822229997Sken			retval = CTL_RETVAL_COMPLETE;
2823229997Sken		} else {
2824229997Sken			ctl_set_success(&io->scsiio);
2825229997Sken		}
2826229997Sken		ctl_config_write_done(io);
2827229997Sken		break;
2828229997Sken	}
2829229997Sken	default:
2830229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2831229997Sken		ctl_config_write_done(io);
2832229997Sken		retval = CTL_RETVAL_COMPLETE;
2833229997Sken		break;
2834229997Sken	}
2835229997Sken
2836229997Sken	return (retval);
2837229997Sken}
2838229997Sken
2839229997Skenstatic int
2840229997Skenctl_be_block_config_read(union ctl_io *io)
2841229997Sken{
2842275474Smav	struct ctl_be_block_lun *be_lun;
2843287499Smav	struct ctl_be_lun *cbe_lun;
2844275474Smav	int retval = 0;
2845275474Smav
2846275474Smav	DPRINTF("entered\n");
2847275474Smav
2848287499Smav	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2849275474Smav		CTL_PRIV_BACKEND_LUN].ptr;
2850287499Smav	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2851275474Smav
2852275474Smav	switch (io->scsiio.cdb[0]) {
2853275474Smav	case SERVICE_ACTION_IN:
2854275474Smav		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2855275474Smav			mtx_lock(&be_lun->queue_lock);
2856275474Smav			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2857275474Smav			    &io->io_hdr, links);
2858275474Smav			mtx_unlock(&be_lun->queue_lock);
2859275474Smav			taskqueue_enqueue(be_lun->io_taskqueue,
2860275474Smav			    &be_lun->io_task);
2861275474Smav			retval = CTL_RETVAL_QUEUED;
2862275474Smav			break;
2863275474Smav		}
2864275474Smav		ctl_set_invalid_field(&io->scsiio,
2865275474Smav				      /*sks_valid*/ 1,
2866275474Smav				      /*command*/ 1,
2867275474Smav				      /*field*/ 1,
2868275474Smav				      /*bit_valid*/ 1,
2869275474Smav				      /*bit*/ 4);
2870275474Smav		ctl_config_read_done(io);
2871275474Smav		retval = CTL_RETVAL_COMPLETE;
2872275474Smav		break;
2873275474Smav	default:
2874275474Smav		ctl_set_invalid_opcode(&io->scsiio);
2875275474Smav		ctl_config_read_done(io);
2876275474Smav		retval = CTL_RETVAL_COMPLETE;
2877275474Smav		break;
2878275474Smav	}
2879275474Smav
2880275474Smav	return (retval);
2881229997Sken}
2882229997Sken
2883229997Skenstatic int
2884229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2885229997Sken{
2886229997Sken	struct ctl_be_block_lun *lun;
2887229997Sken	int retval;
2888229997Sken
2889229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2890229997Sken	retval = 0;
2891229997Sken
2892268283Smav	retval = sbuf_printf(sb, "\t<num_threads>");
2893229997Sken
2894229997Sken	if (retval != 0)
2895229997Sken		goto bailout;
2896229997Sken
2897229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2898229997Sken
2899229997Sken	if (retval != 0)
2900229997Sken		goto bailout;
2901229997Sken
2902268283Smav	retval = sbuf_printf(sb, "</num_threads>\n");
2903229997Sken
2904229997Skenbailout:
2905229997Sken
2906229997Sken	return (retval);
2907229997Sken}
2908229997Sken
2909274154Smavstatic uint64_t
2910274154Smavctl_be_block_lun_attr(void *be_lun, const char *attrname)
2911274154Smav{
2912274154Smav	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2913274154Smav
2914274154Smav	if (lun->getattr == NULL)
2915274154Smav		return (UINT64_MAX);
2916274154Smav	return (lun->getattr(lun, attrname));
2917274154Smav}
2918274154Smav
2919229997Skenint
2920229997Skenctl_be_block_init(void)
2921229997Sken{
2922229997Sken	struct ctl_be_block_softc *softc;
2923229997Sken	int retval;
2924229997Sken
2925229997Sken	softc = &backend_block_softc;
2926229997Sken	retval = 0;
2927229997Sken
2928267877Smav	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2929264020Strasz	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2930264020Strasz	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2931229997Sken	STAILQ_INIT(&softc->lun_list);
2932229997Sken
2933229997Sken	return (retval);
2934229997Sken}
2935