xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision cd8537910406e68d4719136a5b0cf6d23bb1b23b)
1130f4520SKenneth D. Merry /*-
2bec9534dSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3bec9534dSPedro F. Giffuni  *
4130f4520SKenneth D. Merry  * Copyright (c) 2003 Silicon Graphics International Corp.
5130f4520SKenneth D. Merry  * Copyright (c) 2009-2011 Spectra Logic Corporation
681177295SEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
7648dfc1aSAlexander Motin  * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
8130f4520SKenneth D. Merry  * All rights reserved.
9130f4520SKenneth D. Merry  *
1081177295SEdward Tomasz Napierala  * Portions of this software were developed by Edward Tomasz Napierala
1181177295SEdward Tomasz Napierala  * under sponsorship from the FreeBSD Foundation.
1281177295SEdward Tomasz Napierala  *
13130f4520SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
14130f4520SKenneth D. Merry  * modification, are permitted provided that the following conditions
15130f4520SKenneth D. Merry  * are met:
16130f4520SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
17130f4520SKenneth D. Merry  *    notice, this list of conditions, and the following disclaimer,
18130f4520SKenneth D. Merry  *    without modification.
19130f4520SKenneth D. Merry  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20130f4520SKenneth D. Merry  *    substantially similar to the "NO WARRANTY" disclaimer below
21130f4520SKenneth D. Merry  *    ("Disclaimer") and any redistribution must be conditioned upon
22130f4520SKenneth D. Merry  *    including a substantially similar Disclaimer requirement for further
23130f4520SKenneth D. Merry  *    binary redistribution.
24130f4520SKenneth D. Merry  *
25130f4520SKenneth D. Merry  * NO WARRANTY
26130f4520SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27130f4520SKenneth D. Merry  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28130f4520SKenneth D. Merry  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
29130f4520SKenneth D. Merry  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30130f4520SKenneth D. Merry  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31130f4520SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32130f4520SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33130f4520SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34130f4520SKenneth D. Merry  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35130f4520SKenneth D. Merry  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36130f4520SKenneth D. Merry  * POSSIBILITY OF SUCH DAMAGES.
37130f4520SKenneth D. Merry  *
38130f4520SKenneth D. Merry  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
39130f4520SKenneth D. Merry  */
40130f4520SKenneth D. Merry /*
41130f4520SKenneth D. Merry  * CAM Target Layer driver backend for block devices.
42130f4520SKenneth D. Merry  *
43130f4520SKenneth D. Merry  * Author: Ken Merry <ken@FreeBSD.org>
44130f4520SKenneth D. Merry  */
45130f4520SKenneth D. Merry #include <sys/cdefs.h>
46130f4520SKenneth D. Merry __FBSDID("$FreeBSD$");
47130f4520SKenneth D. Merry 
48130f4520SKenneth D. Merry #include <sys/param.h>
49130f4520SKenneth D. Merry #include <sys/systm.h>
50130f4520SKenneth D. Merry #include <sys/kernel.h>
51130f4520SKenneth D. Merry #include <sys/types.h>
52130f4520SKenneth D. Merry #include <sys/kthread.h>
53130f4520SKenneth D. Merry #include <sys/bio.h>
54130f4520SKenneth D. Merry #include <sys/fcntl.h>
55ee7f31c0SAlexander Motin #include <sys/limits.h>
56130f4520SKenneth D. Merry #include <sys/lock.h>
57130f4520SKenneth D. Merry #include <sys/mutex.h>
58130f4520SKenneth D. Merry #include <sys/condvar.h>
59130f4520SKenneth D. Merry #include <sys/malloc.h>
60130f4520SKenneth D. Merry #include <sys/conf.h>
61130f4520SKenneth D. Merry #include <sys/ioccom.h>
62130f4520SKenneth D. Merry #include <sys/queue.h>
63130f4520SKenneth D. Merry #include <sys/sbuf.h>
64130f4520SKenneth D. Merry #include <sys/endian.h>
65130f4520SKenneth D. Merry #include <sys/uio.h>
66130f4520SKenneth D. Merry #include <sys/buf.h>
67130f4520SKenneth D. Merry #include <sys/taskqueue.h>
68130f4520SKenneth D. Merry #include <sys/vnode.h>
69130f4520SKenneth D. Merry #include <sys/namei.h>
70130f4520SKenneth D. Merry #include <sys/mount.h>
71130f4520SKenneth D. Merry #include <sys/disk.h>
72130f4520SKenneth D. Merry #include <sys/fcntl.h>
73130f4520SKenneth D. Merry #include <sys/filedesc.h>
74ef8daf3fSAlexander Motin #include <sys/filio.h>
75130f4520SKenneth D. Merry #include <sys/proc.h>
76130f4520SKenneth D. Merry #include <sys/pcpu.h>
77130f4520SKenneth D. Merry #include <sys/module.h>
78130f4520SKenneth D. Merry #include <sys/sdt.h>
79130f4520SKenneth D. Merry #include <sys/devicestat.h>
80130f4520SKenneth D. Merry #include <sys/sysctl.h>
818951f055SMarcelo Araujo #include <sys/nv.h>
828951f055SMarcelo Araujo #include <sys/dnv.h>
8334144c2cSAlexander Motin #include <sys/sx.h>
84130f4520SKenneth D. Merry 
85130f4520SKenneth D. Merry #include <geom/geom.h>
86130f4520SKenneth D. Merry 
87130f4520SKenneth D. Merry #include <cam/cam.h>
88130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h>
89130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h>
90130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h>
91130f4520SKenneth D. Merry #include <cam/ctl/ctl.h>
92130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h>
93130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h>
947ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h>
95130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h>
967ac58230SAlexander Motin #include <cam/ctl/ctl_private.h>
97130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h>
98130f4520SKenneth D. Merry 
99130f4520SKenneth D. Merry /*
10008a7cce5SAlexander Motin  * The idea here is that we'll allocate enough S/G space to hold a 1MB
10108a7cce5SAlexander Motin  * I/O.  If we get an I/O larger than that, we'll split it.
102130f4520SKenneth D. Merry  */
10311b569f7SAlexander Motin #define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
10411b569f7SAlexander Motin #define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
105*cd853791SKonstantin Belousov #define	CTLBLK_MIN_SEG		(128 * 1024)
106*cd853791SKonstantin Belousov #define	CTLBLK_MAX_SEG		MIN(CTLBLK_HALF_IO_SIZE, maxphys)
107*cd853791SKonstantin Belousov #define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MIN_SEG, 1)
10811b569f7SAlexander Motin #define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
109*cd853791SKonstantin Belousov #define	CTLBLK_NUM_SEGS		(CTLBLK_MAX_IO_SIZE / CTLBLK_MAX_SEG)
110130f4520SKenneth D. Merry 
111130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG
112130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \
113130f4520SKenneth D. Merry     printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
114130f4520SKenneth D. Merry #else
115130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0)
116130f4520SKenneth D. Merry #endif
117130f4520SKenneth D. Merry 
118e86a4142SAlexander Motin #define PRIV(io)	\
119e86a4142SAlexander Motin     ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
12011b569f7SAlexander Motin #define ARGS(io)	\
12111b569f7SAlexander Motin     ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
122e86a4142SAlexander Motin 
123130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb);
124130f4520SKenneth D. Merry 
125130f4520SKenneth D. Merry typedef enum {
126130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
127130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
128130f4520SKenneth D. Merry } ctl_be_block_lun_flags;
129130f4520SKenneth D. Merry 
130130f4520SKenneth D. Merry typedef enum {
131130f4520SKenneth D. Merry 	CTL_BE_BLOCK_NONE,
132130f4520SKenneth D. Merry 	CTL_BE_BLOCK_DEV,
133130f4520SKenneth D. Merry 	CTL_BE_BLOCK_FILE
134130f4520SKenneth D. Merry } ctl_be_block_type;
135130f4520SKenneth D. Merry 
136130f4520SKenneth D. Merry struct ctl_be_block_filedata {
137130f4520SKenneth D. Merry 	struct ucred *cred;
138130f4520SKenneth D. Merry };
139130f4520SKenneth D. Merry 
140130f4520SKenneth D. Merry union ctl_be_block_bedata {
141130f4520SKenneth D. Merry 	struct ctl_be_block_filedata file;
142130f4520SKenneth D. Merry };
143130f4520SKenneth D. Merry 
144130f4520SKenneth D. Merry struct ctl_be_block_io;
145130f4520SKenneth D. Merry struct ctl_be_block_lun;
146130f4520SKenneth D. Merry 
147130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
148130f4520SKenneth D. Merry 			       struct ctl_be_block_io *beio);
149c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
150c3e7ba3eSAlexander Motin 				  const char *attrname);
151130f4520SKenneth D. Merry 
152130f4520SKenneth D. Merry /*
153130f4520SKenneth D. Merry  * Backend LUN structure.  There is a 1:1 mapping between a block device
154130f4520SKenneth D. Merry  * and a backend block LUN, and between a backend block LUN and a CTL LUN.
155130f4520SKenneth D. Merry  */
156130f4520SKenneth D. Merry struct ctl_be_block_lun {
157767300e8SAlexander Motin 	struct ctl_be_lun cbe_lun;		/* Must be first element. */
15819720f41SAlexander Motin 	struct ctl_lun_create_params params;
159130f4520SKenneth D. Merry 	char *dev_path;
160130f4520SKenneth D. Merry 	ctl_be_block_type dev_type;
161130f4520SKenneth D. Merry 	struct vnode *vn;
162130f4520SKenneth D. Merry 	union ctl_be_block_bedata backend;
163130f4520SKenneth D. Merry 	cbb_dispatch_t dispatch;
164130f4520SKenneth D. Merry 	cbb_dispatch_t lun_flush;
165ee7f31c0SAlexander Motin 	cbb_dispatch_t unmap;
166ef8daf3fSAlexander Motin 	cbb_dispatch_t get_lba_status;
167c3e7ba3eSAlexander Motin 	cbb_getattr_t getattr;
168130f4520SKenneth D. Merry 	uint64_t size_blocks;
169130f4520SKenneth D. Merry 	uint64_t size_bytes;
170130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
171130f4520SKenneth D. Merry 	struct devstat *disk_stats;
172130f4520SKenneth D. Merry 	ctl_be_block_lun_flags flags;
17334144c2cSAlexander Motin 	SLIST_ENTRY(ctl_be_block_lun) links;
174130f4520SKenneth D. Merry 	struct taskqueue *io_taskqueue;
175130f4520SKenneth D. Merry 	struct task io_task;
176130f4520SKenneth D. Merry 	int num_threads;
177130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) input_queue;
178ef8daf3fSAlexander Motin 	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
179130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
180130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
18175c7a1d3SAlexander Motin 	struct mtx_padalign io_lock;
18275c7a1d3SAlexander Motin 	struct mtx_padalign queue_lock;
183130f4520SKenneth D. Merry };
184130f4520SKenneth D. Merry 
185130f4520SKenneth D. Merry /*
186130f4520SKenneth D. Merry  * Overall softc structure for the block backend module.
187130f4520SKenneth D. Merry  */
188130f4520SKenneth D. Merry struct ctl_be_block_softc {
18934144c2cSAlexander Motin 	struct sx			 modify_lock;
190130f4520SKenneth D. Merry 	struct mtx			 lock;
191130f4520SKenneth D. Merry 	int				 num_luns;
19234144c2cSAlexander Motin 	SLIST_HEAD(, ctl_be_block_lun)	 lun_list;
1930d7fed74SAlexander Motin 	uma_zone_t			 beio_zone;
194*cd853791SKonstantin Belousov 	uma_zone_t			 bufmin_zone;
195*cd853791SKonstantin Belousov 	uma_zone_t			 bufmax_zone;
196130f4520SKenneth D. Merry };
197130f4520SKenneth D. Merry 
198130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc;
199130f4520SKenneth D. Merry 
200130f4520SKenneth D. Merry /*
201130f4520SKenneth D. Merry  * Per-I/O information.
202130f4520SKenneth D. Merry  */
203130f4520SKenneth D. Merry struct ctl_be_block_io {
204130f4520SKenneth D. Merry 	union ctl_io			*io;
205130f4520SKenneth D. Merry 	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
206130f4520SKenneth D. Merry 	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
2079a4510acSAlexander Motin 	int				refcnt;
208130f4520SKenneth D. Merry 	int				bio_cmd;
2090d7fed74SAlexander Motin 	int				two_sglists;
210130f4520SKenneth D. Merry 	int				num_segs;
211130f4520SKenneth D. Merry 	int				num_bios_sent;
212130f4520SKenneth D. Merry 	int				num_bios_done;
213130f4520SKenneth D. Merry 	int				send_complete;
2141f0694a6SAlexander Motin 	int				first_error;
2151f0694a6SAlexander Motin 	uint64_t			first_error_offset;
216130f4520SKenneth D. Merry 	struct bintime			ds_t0;
217130f4520SKenneth D. Merry 	devstat_tag_type		ds_tag_type;
218130f4520SKenneth D. Merry 	devstat_trans_flags		ds_trans_type;
219130f4520SKenneth D. Merry 	uint64_t			io_len;
220130f4520SKenneth D. Merry 	uint64_t			io_offset;
2217d0d4342SAlexander Motin 	int				io_arg;
222130f4520SKenneth D. Merry 	struct ctl_be_block_softc	*softc;
223130f4520SKenneth D. Merry 	struct ctl_be_block_lun		*lun;
224ee7f31c0SAlexander Motin 	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
225130f4520SKenneth D. Merry };
226130f4520SKenneth D. Merry 
2277ac58230SAlexander Motin extern struct ctl_softc *control_softc;
2287ac58230SAlexander Motin 
229130f4520SKenneth D. Merry static int cbb_num_threads = 14;
2307029da5cSPawel Biernacki SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
231130f4520SKenneth D. Merry 	    "CAM Target Layer Block Backend");
232af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
233130f4520SKenneth D. Merry            &cbb_num_threads, 0, "Number of threads per backing file");
234130f4520SKenneth D. Merry 
235130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
236130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio);
237130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio);
238130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io);
239130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio);
240130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
241130f4520SKenneth D. Merry 				    struct ctl_be_block_io *beio);
242130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
243130f4520SKenneth D. Merry 				       struct ctl_be_block_io *beio);
244ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
245ef8daf3fSAlexander Motin 				  struct ctl_be_block_io *beio);
24653c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
24753c146deSAlexander Motin 					 const char *attrname);
248130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
249130f4520SKenneth D. Merry 				   struct ctl_be_block_io *beio);
250ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
251ee7f31c0SAlexander Motin 				   struct ctl_be_block_io *beio);
252130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
253130f4520SKenneth D. Merry 				      struct ctl_be_block_io *beio);
254c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
255c3e7ba3eSAlexander Motin 					 const char *attrname);
256ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
257ef8daf3fSAlexander Motin 				    union ctl_io *io);
258130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
259130f4520SKenneth D. Merry 				    union ctl_io *io);
260130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
261130f4520SKenneth D. Merry 				  union ctl_io *io);
262130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending);
263130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io);
264130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
265130f4520SKenneth D. Merry 				   int flag, struct thread *td);
266130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
267130f4520SKenneth D. Merry 				  struct ctl_lun_req *req);
268130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
269130f4520SKenneth D. Merry 				 struct ctl_lun_req *req);
270130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
271648dfc1aSAlexander Motin static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
272130f4520SKenneth D. Merry 			     struct ctl_lun_req *req);
273130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc,
274130f4520SKenneth D. Merry 			       struct ctl_lun_req *req);
275130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
276130f4520SKenneth D. Merry 			   struct ctl_lun_req *req);
27781177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
27881177295SEdward Tomasz Napierala 			   struct ctl_lun_req *req);
279767300e8SAlexander Motin static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun);
280130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io);
281130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io);
282767300e8SAlexander Motin static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb);
283767300e8SAlexander Motin static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname);
2840c629e28SAlexander Motin static int ctl_be_block_init(void);
2850c629e28SAlexander Motin static int ctl_be_block_shutdown(void);
286130f4520SKenneth D. Merry 
287130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver =
288130f4520SKenneth D. Merry {
2892a2443d8SKenneth D. Merry 	.name = "block",
2902a2443d8SKenneth D. Merry 	.flags = CTL_BE_FLAG_HAS_CONFIG,
2912a2443d8SKenneth D. Merry 	.init = ctl_be_block_init,
2920c629e28SAlexander Motin 	.shutdown = ctl_be_block_shutdown,
2932a2443d8SKenneth D. Merry 	.data_submit = ctl_be_block_submit,
2942a2443d8SKenneth D. Merry 	.data_move_done = ctl_be_block_move_done,
2952a2443d8SKenneth D. Merry 	.config_read = ctl_be_block_config_read,
2962a2443d8SKenneth D. Merry 	.config_write = ctl_be_block_config_write,
2972a2443d8SKenneth D. Merry 	.ioctl = ctl_be_block_ioctl,
298c3e7ba3eSAlexander Motin 	.lun_info = ctl_be_block_lun_info,
299c3e7ba3eSAlexander Motin 	.lun_attr = ctl_be_block_lun_attr
300130f4520SKenneth D. Merry };
301130f4520SKenneth D. Merry 
30234144c2cSAlexander Motin MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend");
303130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
304130f4520SKenneth D. Merry 
3058054320eSAlexander Motin static void
3068054320eSAlexander Motin ctl_alloc_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg,
3078054320eSAlexander Motin     size_t len)
3088054320eSAlexander Motin {
3098054320eSAlexander Motin 
310*cd853791SKonstantin Belousov 	if (len <= CTLBLK_MIN_SEG) {
311*cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK);
312*cd853791SKonstantin Belousov 	} else {
313*cd853791SKonstantin Belousov 		KASSERT(len <= CTLBLK_MAX_SEG,
314*cd853791SKonstantin Belousov 		    ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG));
315*cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK);
316*cd853791SKonstantin Belousov 	}
3178054320eSAlexander Motin 	sg->len = len;
3188054320eSAlexander Motin }
3198054320eSAlexander Motin 
3208054320eSAlexander Motin static void
3218054320eSAlexander Motin ctl_free_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg)
3228054320eSAlexander Motin {
3238054320eSAlexander Motin 
324*cd853791SKonstantin Belousov 	if (sg->len <= CTLBLK_MIN_SEG) {
325*cd853791SKonstantin Belousov 		uma_zfree(softc->bufmin_zone, sg->addr);
326*cd853791SKonstantin Belousov 	} else {
327*cd853791SKonstantin Belousov 		KASSERT(sg->len <= CTLBLK_MAX_SEG,
328*cd853791SKonstantin Belousov 		    ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG));
329*cd853791SKonstantin Belousov 		uma_zfree(softc->bufmax_zone, sg->addr);
330*cd853791SKonstantin Belousov 	}
3318054320eSAlexander Motin }
3328054320eSAlexander Motin 
333130f4520SKenneth D. Merry static struct ctl_be_block_io *
334130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc)
335130f4520SKenneth D. Merry {
336130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
337130f4520SKenneth D. Merry 
3380c629e28SAlexander Motin 	beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
339130f4520SKenneth D. Merry 	beio->softc = softc;
3409a4510acSAlexander Motin 	beio->refcnt = 1;
341130f4520SKenneth D. Merry 	return (beio);
342130f4520SKenneth D. Merry }
343130f4520SKenneth D. Merry 
344130f4520SKenneth D. Merry static void
3459a4510acSAlexander Motin ctl_real_free_beio(struct ctl_be_block_io *beio)
346130f4520SKenneth D. Merry {
3470d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = beio->softc;
348130f4520SKenneth D. Merry 	int i;
349130f4520SKenneth D. Merry 
350130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
3518054320eSAlexander Motin 		ctl_free_seg(softc, &beio->sg_segs[i]);
35211b569f7SAlexander Motin 
35311b569f7SAlexander Motin 		/* For compare we had two equal S/G lists. */
3540d7fed74SAlexander Motin 		if (beio->two_sglists) {
3558054320eSAlexander Motin 			ctl_free_seg(softc,
3568054320eSAlexander Motin 			    &beio->sg_segs[i + CTLBLK_HALF_SEGS]);
35711b569f7SAlexander Motin 		}
358130f4520SKenneth D. Merry 	}
359130f4520SKenneth D. Merry 
3600d7fed74SAlexander Motin 	uma_zfree(softc->beio_zone, beio);
361130f4520SKenneth D. Merry }
362130f4520SKenneth D. Merry 
363130f4520SKenneth D. Merry static void
3649a4510acSAlexander Motin ctl_refcnt_beio(void *arg, int diff)
3659a4510acSAlexander Motin {
3669a4510acSAlexander Motin 	struct ctl_be_block_io *beio = arg;
3679a4510acSAlexander Motin 
3689a4510acSAlexander Motin 	if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0)
3699a4510acSAlexander Motin 		ctl_real_free_beio(beio);
3709a4510acSAlexander Motin }
3719a4510acSAlexander Motin 
3729a4510acSAlexander Motin static void
3739a4510acSAlexander Motin ctl_free_beio(struct ctl_be_block_io *beio)
3749a4510acSAlexander Motin {
3759a4510acSAlexander Motin 
3769a4510acSAlexander Motin 	ctl_refcnt_beio(beio, -1);
3779a4510acSAlexander Motin }
3789a4510acSAlexander Motin 
3799a4510acSAlexander Motin static void
380130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio)
381130f4520SKenneth D. Merry {
38275c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
383130f4520SKenneth D. Merry 
384ee7f31c0SAlexander Motin 	if (beio->beio_cont != NULL) {
385ee7f31c0SAlexander Motin 		beio->beio_cont(beio);
386ee7f31c0SAlexander Motin 	} else {
387130f4520SKenneth D. Merry 		ctl_free_beio(beio);
38811b569f7SAlexander Motin 		ctl_data_submit_done(io);
389130f4520SKenneth D. Merry 	}
390ee7f31c0SAlexander Motin }
391130f4520SKenneth D. Merry 
392d6043e46SAlexander Motin static size_t
393d6043e46SAlexander Motin cmp(uint8_t *a, uint8_t *b, size_t size)
394d6043e46SAlexander Motin {
395d6043e46SAlexander Motin 	size_t i;
396d6043e46SAlexander Motin 
397d6043e46SAlexander Motin 	for (i = 0; i < size; i++) {
398d6043e46SAlexander Motin 		if (a[i] != b[i])
399d6043e46SAlexander Motin 			break;
400d6043e46SAlexander Motin 	}
401d6043e46SAlexander Motin 	return (i);
402d6043e46SAlexander Motin }
403d6043e46SAlexander Motin 
404d6043e46SAlexander Motin static void
405d6043e46SAlexander Motin ctl_be_block_compare(union ctl_io *io)
406d6043e46SAlexander Motin {
407d6043e46SAlexander Motin 	struct ctl_be_block_io *beio;
408d6043e46SAlexander Motin 	uint64_t off, res;
409d6043e46SAlexander Motin 	int i;
410d6043e46SAlexander Motin 	uint8_t info[8];
411d6043e46SAlexander Motin 
412d6043e46SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
413d6043e46SAlexander Motin 	off = 0;
414d6043e46SAlexander Motin 	for (i = 0; i < beio->num_segs; i++) {
415d6043e46SAlexander Motin 		res = cmp(beio->sg_segs[i].addr,
416d6043e46SAlexander Motin 		    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
417d6043e46SAlexander Motin 		    beio->sg_segs[i].len);
418d6043e46SAlexander Motin 		off += res;
419d6043e46SAlexander Motin 		if (res < beio->sg_segs[i].len)
420d6043e46SAlexander Motin 			break;
421d6043e46SAlexander Motin 	}
422d6043e46SAlexander Motin 	if (i < beio->num_segs) {
423d6043e46SAlexander Motin 		scsi_u64to8b(off, info);
424d6043e46SAlexander Motin 		ctl_set_sense(&io->scsiio, /*current_error*/ 1,
425d6043e46SAlexander Motin 		    /*sense_key*/ SSD_KEY_MISCOMPARE,
426d6043e46SAlexander Motin 		    /*asc*/ 0x1D, /*ascq*/ 0x00,
427d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_INFO,
428d6043e46SAlexander Motin 		    /*size*/ sizeof(info), /*data*/ &info,
429d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_NONE);
430d6043e46SAlexander Motin 	} else
431d6043e46SAlexander Motin 		ctl_set_success(&io->scsiio);
432d6043e46SAlexander Motin }
433d6043e46SAlexander Motin 
434130f4520SKenneth D. Merry static int
435130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io)
436130f4520SKenneth D. Merry {
437130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
438130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
43911b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
440130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
441130f4520SKenneth D. Merry 	struct bintime cur_bt;
442130f4520SKenneth D. Merry #endif
443130f4520SKenneth D. Merry 
444e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
445130f4520SKenneth D. Merry 	be_lun = beio->lun;
446130f4520SKenneth D. Merry 
447130f4520SKenneth D. Merry 	DPRINTF("entered\n");
448130f4520SKenneth D. Merry 
449130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
450e675024aSAlexander Motin 	getbinuptime(&cur_bt);
451130f4520SKenneth D. Merry 	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
452130f4520SKenneth D. Merry 	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
453130f4520SKenneth D. Merry #endif
454e675024aSAlexander Motin 	io->io_hdr.num_dmas++;
45511b569f7SAlexander Motin 	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
456130f4520SKenneth D. Merry 
457130f4520SKenneth D. Merry 	/*
458130f4520SKenneth D. Merry 	 * We set status at this point for read commands, and write
459130f4520SKenneth D. Merry 	 * commands with errors.
460130f4520SKenneth D. Merry 	 */
461f7241cceSAlexander Motin 	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
462f7241cceSAlexander Motin 		;
463eb6ac6f9SAlexander Motin 	} else if ((io->io_hdr.port_status != 0) &&
464eb6ac6f9SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
465eb6ac6f9SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
466eb6ac6f9SAlexander Motin 		ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
467eb6ac6f9SAlexander Motin 		    /*retry_count*/ io->io_hdr.port_status);
468eb6ac6f9SAlexander Motin 	} else if (io->scsiio.kern_data_resid != 0 &&
469eb6ac6f9SAlexander Motin 	    (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
470eb6ac6f9SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
471eb6ac6f9SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
472eb6ac6f9SAlexander Motin 		ctl_set_invalid_field_ciu(&io->scsiio);
473f7241cceSAlexander Motin 	} else if ((io->io_hdr.port_status == 0) &&
47411b569f7SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
47511b569f7SAlexander Motin 		lbalen = ARGS(beio->io);
47611b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_READ) {
477130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
47811b569f7SAlexander Motin 		} else if (lbalen->flags & CTL_LLF_COMPARE) {
47911b569f7SAlexander Motin 			/* We have two data blocks ready for comparison. */
480d6043e46SAlexander Motin 			ctl_be_block_compare(io);
48111b569f7SAlexander Motin 		}
482130f4520SKenneth D. Merry 	}
483130f4520SKenneth D. Merry 
484130f4520SKenneth D. Merry 	/*
485130f4520SKenneth D. Merry 	 * If this is a read, or a write with errors, it is done.
486130f4520SKenneth D. Merry 	 */
487130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_READ)
488130f4520SKenneth D. Merry 	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
489130f4520SKenneth D. Merry 	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
490130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
491130f4520SKenneth D. Merry 		return (0);
492130f4520SKenneth D. Merry 	}
493130f4520SKenneth D. Merry 
494130f4520SKenneth D. Merry 	/*
495130f4520SKenneth D. Merry 	 * At this point, we have a write and the DMA completed
496130f4520SKenneth D. Merry 	 * successfully.  We now have to queue it to the task queue to
497130f4520SKenneth D. Merry 	 * execute the backend I/O.  That is because we do blocking
498130f4520SKenneth D. Merry 	 * memory allocations, and in the file backing case, blocking I/O.
499130f4520SKenneth D. Merry 	 * This move done routine is generally called in the SIM's
500130f4520SKenneth D. Merry 	 * interrupt context, and therefore we cannot block.
501130f4520SKenneth D. Merry 	 */
50275c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
503130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
50475c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
505130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
506130f4520SKenneth D. Merry 
507130f4520SKenneth D. Merry 	return (0);
508130f4520SKenneth D. Merry }
509130f4520SKenneth D. Merry 
510130f4520SKenneth D. Merry static void
511130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio)
512130f4520SKenneth D. Merry {
513130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
514130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
515130f4520SKenneth D. Merry 	union ctl_io *io;
516e0c2f975SAlexander Motin 	int error;
517130f4520SKenneth D. Merry 
518130f4520SKenneth D. Merry 	beio = bio->bio_caller1;
519130f4520SKenneth D. Merry 	be_lun = beio->lun;
520130f4520SKenneth D. Merry 	io = beio->io;
521130f4520SKenneth D. Merry 
522130f4520SKenneth D. Merry 	DPRINTF("entered\n");
523130f4520SKenneth D. Merry 
524e0c2f975SAlexander Motin 	error = bio->bio_error;
52575c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
5261f0694a6SAlexander Motin 	if (error != 0 &&
5271f0694a6SAlexander Motin 	    (beio->first_error == 0 ||
5281f0694a6SAlexander Motin 	     bio->bio_offset < beio->first_error_offset)) {
5291f0694a6SAlexander Motin 		beio->first_error = error;
5301f0694a6SAlexander Motin 		beio->first_error_offset = bio->bio_offset;
5311f0694a6SAlexander Motin 	}
532130f4520SKenneth D. Merry 
533130f4520SKenneth D. Merry 	beio->num_bios_done++;
534130f4520SKenneth D. Merry 
535130f4520SKenneth D. Merry 	/*
536130f4520SKenneth D. Merry 	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
537130f4520SKenneth D. Merry 	 * during the free might cause it to complain.
538130f4520SKenneth D. Merry 	 */
539130f4520SKenneth D. Merry 	g_destroy_bio(bio);
540130f4520SKenneth D. Merry 
541130f4520SKenneth D. Merry 	/*
542130f4520SKenneth D. Merry 	 * If the send complete bit isn't set, or we aren't the last I/O to
543130f4520SKenneth D. Merry 	 * complete, then we're done.
544130f4520SKenneth D. Merry 	 */
545130f4520SKenneth D. Merry 	if ((beio->send_complete == 0)
546130f4520SKenneth D. Merry 	 || (beio->num_bios_done < beio->num_bios_sent)) {
54775c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
548130f4520SKenneth D. Merry 		return;
549130f4520SKenneth D. Merry 	}
550130f4520SKenneth D. Merry 
551130f4520SKenneth D. Merry 	/*
552130f4520SKenneth D. Merry 	 * At this point, we've verified that we are the last I/O to
553130f4520SKenneth D. Merry 	 * complete, so it's safe to drop the lock.
554130f4520SKenneth D. Merry 	 */
55575c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
55675c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
55775c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
55875c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
559130f4520SKenneth D. Merry 
560130f4520SKenneth D. Merry 	/*
561130f4520SKenneth D. Merry 	 * If there are any errors from the backing device, we fail the
562130f4520SKenneth D. Merry 	 * entire I/O with a medium error.
563130f4520SKenneth D. Merry 	 */
5641f0694a6SAlexander Motin 	error = beio->first_error;
5651f0694a6SAlexander Motin 	if (error != 0) {
566e0c2f975SAlexander Motin 		if (error == EOPNOTSUPP) {
567e0c2f975SAlexander Motin 			ctl_set_invalid_opcode(&io->scsiio);
5680631de4aSAlexander Motin 		} else if (error == ENOSPC || error == EDQUOT) {
5694fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
5706187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
5716187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
572e0c2f975SAlexander Motin 		} else if (beio->bio_cmd == BIO_FLUSH) {
573130f4520SKenneth D. Merry 			/* XXX KDM is there is a better error here? */
574130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
575130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
576130f4520SKenneth D. Merry 						 /*retry_count*/ 0xbad2);
5777f7bb97aSAlexander Motin 		} else {
5787f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
5797f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
5807f7bb97aSAlexander Motin 		}
581130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
582130f4520SKenneth D. Merry 		return;
583130f4520SKenneth D. Merry 	}
584130f4520SKenneth D. Merry 
585130f4520SKenneth D. Merry 	/*
58611b569f7SAlexander Motin 	 * If this is a write, a flush, a delete or verify, we're all done.
587130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
588130f4520SKenneth D. Merry 	 */
589130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_WRITE)
590ee7f31c0SAlexander Motin 	 || (beio->bio_cmd == BIO_FLUSH)
59111b569f7SAlexander Motin 	 || (beio->bio_cmd == BIO_DELETE)
59211b569f7SAlexander Motin 	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
593130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
594130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
595130f4520SKenneth D. Merry 	} else {
596f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
59775a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
598f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
59975a3108eSAlexander Motin 			ctl_serseq_done(io);
60075a3108eSAlexander Motin 		}
601130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
602e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
603130f4520SKenneth D. Merry #endif
604130f4520SKenneth D. Merry 		ctl_datamove(io);
605130f4520SKenneth D. Merry 	}
606130f4520SKenneth D. Merry }
607130f4520SKenneth D. Merry 
608130f4520SKenneth D. Merry static void
609130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
610130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio)
611130f4520SKenneth D. Merry {
61275c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
613130f4520SKenneth D. Merry 	struct mount *mountpoint;
6145050aa86SKonstantin Belousov 	int error, lock_flags;
615130f4520SKenneth D. Merry 
616130f4520SKenneth D. Merry 	DPRINTF("entered\n");
617130f4520SKenneth D. Merry 
61875c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
61975c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
620130f4520SKenneth D. Merry 
621130f4520SKenneth D. Merry 	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
622130f4520SKenneth D. Merry 
62367cc546dSAlexander Motin 	if (MNT_SHARED_WRITES(mountpoint) ||
62467cc546dSAlexander Motin 	    ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
625130f4520SKenneth D. Merry 		lock_flags = LK_SHARED;
626130f4520SKenneth D. Merry 	else
627130f4520SKenneth D. Merry 		lock_flags = LK_EXCLUSIVE;
628130f4520SKenneth D. Merry 	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
6297d0d4342SAlexander Motin 	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
6307d0d4342SAlexander Motin 	    curthread);
631b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
632130f4520SKenneth D. Merry 
633130f4520SKenneth D. Merry 	vn_finished_write(mountpoint);
634130f4520SKenneth D. Merry 
63575c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
63675c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
63775c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
63875c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
63975c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
64075c7a1d3SAlexander Motin 
641130f4520SKenneth D. Merry 	if (error == 0)
642130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
643130f4520SKenneth D. Merry 	else {
644130f4520SKenneth D. Merry 		/* XXX KDM is there is a better error here? */
645130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
646130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
647130f4520SKenneth D. Merry 					 /*retry_count*/ 0xbad1);
648130f4520SKenneth D. Merry 	}
649130f4520SKenneth D. Merry 
650130f4520SKenneth D. Merry 	ctl_complete_beio(beio);
651130f4520SKenneth D. Merry }
652130f4520SKenneth D. Merry 
65336160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
65436160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
65536160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
65636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
657130f4520SKenneth D. Merry 
658130f4520SKenneth D. Merry static void
659130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
660130f4520SKenneth D. Merry 			   struct ctl_be_block_io *beio)
661130f4520SKenneth D. Merry {
662130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
663130f4520SKenneth D. Merry 	union ctl_io *io;
664130f4520SKenneth D. Merry 	struct uio xuio;
665130f4520SKenneth D. Merry 	struct iovec *xiovec;
66683981e31SAlexander Motin 	size_t s;
66783981e31SAlexander Motin 	int error, flags, i;
668130f4520SKenneth D. Merry 
669130f4520SKenneth D. Merry 	DPRINTF("entered\n");
670130f4520SKenneth D. Merry 
671130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
672130f4520SKenneth D. Merry 	io = beio->io;
67355551d05SAlexander Motin 	flags = 0;
67455551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
67555551d05SAlexander Motin 		flags |= IO_DIRECT;
67655551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
67755551d05SAlexander Motin 		flags |= IO_SYNC;
678130f4520SKenneth D. Merry 
67911b569f7SAlexander Motin 	bzero(&xuio, sizeof(xuio));
680130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
68136160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
68211b569f7SAlexander Motin 		xuio.uio_rw = UIO_READ;
683130f4520SKenneth D. Merry 	} else {
68436160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
685130f4520SKenneth D. Merry 		xuio.uio_rw = UIO_WRITE;
68611b569f7SAlexander Motin 	}
687130f4520SKenneth D. Merry 	xuio.uio_offset = beio->io_offset;
688130f4520SKenneth D. Merry 	xuio.uio_resid = beio->io_len;
689130f4520SKenneth D. Merry 	xuio.uio_segflg = UIO_SYSSPACE;
690130f4520SKenneth D. Merry 	xuio.uio_iov = beio->xiovecs;
691130f4520SKenneth D. Merry 	xuio.uio_iovcnt = beio->num_segs;
692130f4520SKenneth D. Merry 	xuio.uio_td = curthread;
693130f4520SKenneth D. Merry 
694130f4520SKenneth D. Merry 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
695130f4520SKenneth D. Merry 		xiovec->iov_base = beio->sg_segs[i].addr;
696130f4520SKenneth D. Merry 		xiovec->iov_len = beio->sg_segs[i].len;
697130f4520SKenneth D. Merry 	}
698130f4520SKenneth D. Merry 
69975c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
70075c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
70175c7a1d3SAlexander Motin 
702130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
703130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
704130f4520SKenneth D. Merry 
705130f4520SKenneth D. Merry 		/*
706130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for reads.  If the
707130f4520SKenneth D. Merry 		 * DIRECTIO option is configured into the kernel, it calls
708130f4520SKenneth D. Merry 		 * ffs_rawread().  But that only works for single-segment
709130f4520SKenneth D. Merry 		 * uios with user space addresses.  In our case, with a
710130f4520SKenneth D. Merry 		 * kernel uio, it still reads into the buffer cache, but it
711130f4520SKenneth D. Merry 		 * will just try to release the buffer from the cache later
712130f4520SKenneth D. Merry 		 * on in ffs_read().
713130f4520SKenneth D. Merry 		 *
714130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for reads.
715130f4520SKenneth D. Merry 		 *
716130f4520SKenneth D. Merry 		 * UFS does not pay attention to IO_SYNC for reads.
717130f4520SKenneth D. Merry 		 *
718130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (which translates into the
719130f4520SKenneth D. Merry 		 * Solaris define FRSYNC for zfs_read()) for reads.  It
720130f4520SKenneth D. Merry 		 * attempts to sync the file before reading.
721130f4520SKenneth D. Merry 		 */
72255551d05SAlexander Motin 		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
723130f4520SKenneth D. Merry 
724b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
72536160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
72683981e31SAlexander Motin 		if (error == 0 && xuio.uio_resid > 0) {
72783981e31SAlexander Motin 			/*
72883981e31SAlexander Motin 			 * If we red less then requested (EOF), then
72983981e31SAlexander Motin 			 * we should clean the rest of the buffer.
73083981e31SAlexander Motin 			 */
73183981e31SAlexander Motin 			s = beio->io_len - xuio.uio_resid;
73283981e31SAlexander Motin 			for (i = 0; i < beio->num_segs; i++) {
73383981e31SAlexander Motin 				if (s >= beio->sg_segs[i].len) {
73483981e31SAlexander Motin 					s -= beio->sg_segs[i].len;
73583981e31SAlexander Motin 					continue;
73683981e31SAlexander Motin 				}
73783981e31SAlexander Motin 				bzero((uint8_t *)beio->sg_segs[i].addr + s,
73883981e31SAlexander Motin 				    beio->sg_segs[i].len - s);
73983981e31SAlexander Motin 				s = 0;
74083981e31SAlexander Motin 			}
74183981e31SAlexander Motin 		}
742130f4520SKenneth D. Merry 	} else {
743130f4520SKenneth D. Merry 		struct mount *mountpoint;
744130f4520SKenneth D. Merry 		int lock_flags;
745130f4520SKenneth D. Merry 
746130f4520SKenneth D. Merry 		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
747130f4520SKenneth D. Merry 
74867cc546dSAlexander Motin 		if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL)
749130f4520SKenneth D. Merry 		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
750130f4520SKenneth D. Merry 			lock_flags = LK_SHARED;
751130f4520SKenneth D. Merry 		else
752130f4520SKenneth D. Merry 			lock_flags = LK_EXCLUSIVE;
753130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
754130f4520SKenneth D. Merry 
755130f4520SKenneth D. Merry 		/*
756130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for writes.  The write
757130f4520SKenneth D. Merry 		 * is done asynchronously.  (Normally the write would just
758130f4520SKenneth D. Merry 		 * get put into cache.
759130f4520SKenneth D. Merry 		 *
760130f4520SKenneth D. Merry 		 * UFS pays attention to IO_SYNC for writes.  It will
761130f4520SKenneth D. Merry 		 * attempt to write the buffer out synchronously if that
762130f4520SKenneth D. Merry 		 * flag is set.
763130f4520SKenneth D. Merry 		 *
764130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for writes.
765130f4520SKenneth D. Merry 		 *
766130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
767130f4520SKenneth D. Merry 		 * for writes.  It will flush the transaction from the
768130f4520SKenneth D. Merry 		 * cache before returning.
769130f4520SKenneth D. Merry 		 */
77055551d05SAlexander Motin 		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
771b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
772130f4520SKenneth D. Merry 
773130f4520SKenneth D. Merry 		vn_finished_write(mountpoint);
77436160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
775130f4520SKenneth D. Merry         }
776130f4520SKenneth D. Merry 
77775c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
77875c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
77975c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
78075c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
78175c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
78275c7a1d3SAlexander Motin 
783130f4520SKenneth D. Merry 	/*
784130f4520SKenneth D. Merry 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
785130f4520SKenneth D. Merry 	 * return the I/O to the user.
786130f4520SKenneth D. Merry 	 */
787130f4520SKenneth D. Merry 	if (error != 0) {
7880631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
7894fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
7906187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
7916187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
7927f7bb97aSAlexander Motin 		} else {
7937f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
7947f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
7957f7bb97aSAlexander Motin 		}
796130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
797130f4520SKenneth D. Merry 		return;
798130f4520SKenneth D. Merry 	}
799130f4520SKenneth D. Merry 
800130f4520SKenneth D. Merry 	/*
801696297adSAlexander Motin 	 * If this is a write or a verify, we're all done.
802130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
803130f4520SKenneth D. Merry 	 */
804696297adSAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
805696297adSAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
806130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
807130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
808130f4520SKenneth D. Merry 	} else {
809f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
81075a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
811f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
81275a3108eSAlexander Motin 			ctl_serseq_done(io);
81375a3108eSAlexander Motin 		}
814130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
815e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
816130f4520SKenneth D. Merry #endif
817130f4520SKenneth D. Merry 		ctl_datamove(io);
818130f4520SKenneth D. Merry 	}
819130f4520SKenneth D. Merry }
820130f4520SKenneth D. Merry 
821130f4520SKenneth D. Merry static void
822ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
823ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
824ef8daf3fSAlexander Motin {
825ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
826ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
827ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
828ef8daf3fSAlexander Motin 	off_t roff, off;
829ef8daf3fSAlexander Motin 	int error, status;
830ef8daf3fSAlexander Motin 
831ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
832ef8daf3fSAlexander Motin 
8330bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
834ef8daf3fSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
835ef8daf3fSAlexander Motin 	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
836ef8daf3fSAlexander Motin 	    0, curthread->td_ucred, curthread);
837ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
838ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
839ef8daf3fSAlexander Motin 	else {
840ef8daf3fSAlexander Motin 		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
841ef8daf3fSAlexander Motin 		    0, curthread->td_ucred, curthread);
842ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
843ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
844ef8daf3fSAlexander Motin 		else {
845ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
846ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
847ef8daf3fSAlexander Motin 		}
848ef8daf3fSAlexander Motin 	}
849b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
850ef8daf3fSAlexander Motin 
851ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
852ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
8530bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
8540bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
855ef8daf3fSAlexander Motin 	data->descr[0].status = status;
856ef8daf3fSAlexander Motin 
857ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
858ef8daf3fSAlexander Motin }
859ef8daf3fSAlexander Motin 
86053c146deSAlexander Motin static uint64_t
86153c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
86253c146deSAlexander Motin {
86353c146deSAlexander Motin 	struct vattr		vattr;
86453c146deSAlexander Motin 	struct statfs		statfs;
865b9b4269cSAlexander Motin 	uint64_t		val;
86653c146deSAlexander Motin 	int			error;
86753c146deSAlexander Motin 
868b9b4269cSAlexander Motin 	val = UINT64_MAX;
86953c146deSAlexander Motin 	if (be_lun->vn == NULL)
870b9b4269cSAlexander Motin 		return (val);
871b9b4269cSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
87253c146deSAlexander Motin 	if (strcmp(attrname, "blocksused") == 0) {
87353c146deSAlexander Motin 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
874b9b4269cSAlexander Motin 		if (error == 0)
8750bcd4ab6SAlexander Motin 			val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
87653c146deSAlexander Motin 	}
877b9b4269cSAlexander Motin 	if (strcmp(attrname, "blocksavail") == 0 &&
878abd80ddbSMateusz Guzik 	    !VN_IS_DOOMED(be_lun->vn)) {
87953c146deSAlexander Motin 		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
880b9b4269cSAlexander Motin 		if (error == 0)
881a15bbf15SAlexander Motin 			val = statfs.f_bavail * statfs.f_bsize /
8820bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
88353c146deSAlexander Motin 	}
884b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
885b9b4269cSAlexander Motin 	return (val);
88653c146deSAlexander Motin }
88753c146deSAlexander Motin 
888ef8daf3fSAlexander Motin static void
88967f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
89067f586a8SAlexander Motin 			   struct ctl_be_block_io *beio)
89167f586a8SAlexander Motin {
89267f586a8SAlexander Motin 	union ctl_io *io;
8933236151eSAlexander Motin 	struct cdevsw *csw;
8943236151eSAlexander Motin 	struct cdev *dev;
89567f586a8SAlexander Motin 	struct uio xuio;
89667f586a8SAlexander Motin 	struct iovec *xiovec;
8973236151eSAlexander Motin 	int error, flags, i, ref;
89867f586a8SAlexander Motin 
89967f586a8SAlexander Motin 	DPRINTF("entered\n");
90067f586a8SAlexander Motin 
90167f586a8SAlexander Motin 	io = beio->io;
90255551d05SAlexander Motin 	flags = 0;
90355551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
90455551d05SAlexander Motin 		flags |= IO_DIRECT;
90555551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
90655551d05SAlexander Motin 		flags |= IO_SYNC;
90767f586a8SAlexander Motin 
90867f586a8SAlexander Motin 	bzero(&xuio, sizeof(xuio));
90967f586a8SAlexander Motin 	if (beio->bio_cmd == BIO_READ) {
91036160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
91167f586a8SAlexander Motin 		xuio.uio_rw = UIO_READ;
91267f586a8SAlexander Motin 	} else {
91336160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
91467f586a8SAlexander Motin 		xuio.uio_rw = UIO_WRITE;
91567f586a8SAlexander Motin 	}
91667f586a8SAlexander Motin 	xuio.uio_offset = beio->io_offset;
91767f586a8SAlexander Motin 	xuio.uio_resid = beio->io_len;
91867f586a8SAlexander Motin 	xuio.uio_segflg = UIO_SYSSPACE;
91967f586a8SAlexander Motin 	xuio.uio_iov = beio->xiovecs;
92067f586a8SAlexander Motin 	xuio.uio_iovcnt = beio->num_segs;
92167f586a8SAlexander Motin 	xuio.uio_td = curthread;
92267f586a8SAlexander Motin 
92367f586a8SAlexander Motin 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
92467f586a8SAlexander Motin 		xiovec->iov_base = beio->sg_segs[i].addr;
92567f586a8SAlexander Motin 		xiovec->iov_len = beio->sg_segs[i].len;
92667f586a8SAlexander Motin 	}
92767f586a8SAlexander Motin 
92867f586a8SAlexander Motin 	binuptime(&beio->ds_t0);
92967f586a8SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
93067f586a8SAlexander Motin 
9313236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
9323236151eSAlexander Motin 	if (csw) {
9333236151eSAlexander Motin 		if (beio->bio_cmd == BIO_READ)
9343236151eSAlexander Motin 			error = csw->d_read(dev, &xuio, flags);
9353236151eSAlexander Motin 		else
9363236151eSAlexander Motin 			error = csw->d_write(dev, &xuio, flags);
9373236151eSAlexander Motin 		dev_relthread(dev, ref);
9383236151eSAlexander Motin 	} else
9393236151eSAlexander Motin 		error = ENXIO;
9403236151eSAlexander Motin 
9413236151eSAlexander Motin 	if (beio->bio_cmd == BIO_READ)
94236160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
9433236151eSAlexander Motin 	else
94436160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
94567f586a8SAlexander Motin 
94667f586a8SAlexander Motin 	mtx_lock(&be_lun->io_lock);
94767f586a8SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
94867f586a8SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
94967f586a8SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
95067f586a8SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
95167f586a8SAlexander Motin 
95267f586a8SAlexander Motin 	/*
95367f586a8SAlexander Motin 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
95467f586a8SAlexander Motin 	 * return the I/O to the user.
95567f586a8SAlexander Motin 	 */
95667f586a8SAlexander Motin 	if (error != 0) {
9570631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
9584fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
9596187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
9606187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
9617f7bb97aSAlexander Motin 		} else {
9627f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
9637f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
9647f7bb97aSAlexander Motin 		}
96567f586a8SAlexander Motin 		ctl_complete_beio(beio);
96667f586a8SAlexander Motin 		return;
96767f586a8SAlexander Motin 	}
96867f586a8SAlexander Motin 
96967f586a8SAlexander Motin 	/*
97067f586a8SAlexander Motin 	 * If this is a write or a verify, we're all done.
97167f586a8SAlexander Motin 	 * If this is a read, we can now send the data to the user.
97267f586a8SAlexander Motin 	 */
97367f586a8SAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
97467f586a8SAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
97567f586a8SAlexander Motin 		ctl_set_success(&io->scsiio);
97667f586a8SAlexander Motin 		ctl_complete_beio(beio);
97767f586a8SAlexander Motin 	} else {
978f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
97975a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
980f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
98175a3108eSAlexander Motin 			ctl_serseq_done(io);
98275a3108eSAlexander Motin 		}
98367f586a8SAlexander Motin #ifdef CTL_TIME_IO
984e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
98567f586a8SAlexander Motin #endif
98667f586a8SAlexander Motin 		ctl_datamove(io);
98767f586a8SAlexander Motin 	}
98867f586a8SAlexander Motin }
98967f586a8SAlexander Motin 
99067f586a8SAlexander Motin static void
991ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
992ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
993ef8daf3fSAlexander Motin {
994ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
9953236151eSAlexander Motin 	struct cdevsw *csw;
9963236151eSAlexander Motin 	struct cdev *dev;
997ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
998ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
999ef8daf3fSAlexander Motin 	off_t roff, off;
10003236151eSAlexander Motin 	int error, ref, status;
1001ef8daf3fSAlexander Motin 
1002ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1003ef8daf3fSAlexander Motin 
10043236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10053236151eSAlexander Motin 	if (csw == NULL) {
10063236151eSAlexander Motin 		status = 0;	/* unknown up to the end */
10073236151eSAlexander Motin 		off = be_lun->size_bytes;
10083236151eSAlexander Motin 		goto done;
10093236151eSAlexander Motin 	}
10100bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
10113236151eSAlexander Motin 	error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
10123236151eSAlexander Motin 	    curthread);
1013ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
1014ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
1015ef8daf3fSAlexander Motin 	else {
10163236151eSAlexander Motin 		error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
10173236151eSAlexander Motin 		    curthread);
1018ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
1019ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
1020ef8daf3fSAlexander Motin 		else {
1021ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
1022ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
1023ef8daf3fSAlexander Motin 		}
1024ef8daf3fSAlexander Motin 	}
10253236151eSAlexander Motin 	dev_relthread(dev, ref);
1026ef8daf3fSAlexander Motin 
10273236151eSAlexander Motin done:
1028ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
1029ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
10300bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
10310bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
1032ef8daf3fSAlexander Motin 	data->descr[0].status = status;
1033ef8daf3fSAlexander Motin 
1034ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
1035ef8daf3fSAlexander Motin }
1036ef8daf3fSAlexander Motin 
1037ef8daf3fSAlexander Motin static void
1038130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1039130f4520SKenneth D. Merry 		       struct ctl_be_block_io *beio)
1040130f4520SKenneth D. Merry {
1041130f4520SKenneth D. Merry 	struct bio *bio;
10423236151eSAlexander Motin 	struct cdevsw *csw;
10433236151eSAlexander Motin 	struct cdev *dev;
10443236151eSAlexander Motin 	int ref;
1045130f4520SKenneth D. Merry 
1046130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1047130f4520SKenneth D. Merry 
1048130f4520SKenneth D. Merry 	/* This can't fail, it's a blocking allocation. */
1049130f4520SKenneth D. Merry 	bio = g_alloc_bio();
1050130f4520SKenneth D. Merry 
1051130f4520SKenneth D. Merry 	bio->bio_cmd	    = BIO_FLUSH;
1052130f4520SKenneth D. Merry 	bio->bio_offset	    = 0;
1053130f4520SKenneth D. Merry 	bio->bio_data	    = 0;
1054130f4520SKenneth D. Merry 	bio->bio_done	    = ctl_be_block_biodone;
1055130f4520SKenneth D. Merry 	bio->bio_caller1    = beio;
1056130f4520SKenneth D. Merry 	bio->bio_pblkno	    = 0;
1057130f4520SKenneth D. Merry 
1058130f4520SKenneth D. Merry 	/*
1059130f4520SKenneth D. Merry 	 * We don't need to acquire the LUN lock here, because we are only
1060130f4520SKenneth D. Merry 	 * sending one bio, and so there is no other context to synchronize
1061130f4520SKenneth D. Merry 	 * with.
1062130f4520SKenneth D. Merry 	 */
1063130f4520SKenneth D. Merry 	beio->num_bios_sent = 1;
1064130f4520SKenneth D. Merry 	beio->send_complete = 1;
1065130f4520SKenneth D. Merry 
1066130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
1067130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1068130f4520SKenneth D. Merry 
10693236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10703236151eSAlexander Motin 	if (csw) {
10713236151eSAlexander Motin 		bio->bio_dev = dev;
10723236151eSAlexander Motin 		csw->d_strategy(bio);
10733236151eSAlexander Motin 		dev_relthread(dev, ref);
10743236151eSAlexander Motin 	} else {
10753236151eSAlexander Motin 		bio->bio_error = ENXIO;
10763236151eSAlexander Motin 		ctl_be_block_biodone(bio);
10773236151eSAlexander Motin 	}
1078130f4520SKenneth D. Merry }
1079130f4520SKenneth D. Merry 
1080130f4520SKenneth D. Merry static void
1081ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1082ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio,
1083ee7f31c0SAlexander Motin 		       uint64_t off, uint64_t len, int last)
1084ee7f31c0SAlexander Motin {
1085ee7f31c0SAlexander Motin 	struct bio *bio;
10868f5a226aSAlexander Motin 	uint64_t maxlen;
10873236151eSAlexander Motin 	struct cdevsw *csw;
10883236151eSAlexander Motin 	struct cdev *dev;
10893236151eSAlexander Motin 	int ref;
1090ee7f31c0SAlexander Motin 
10913236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10920bcd4ab6SAlexander Motin 	maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1093ee7f31c0SAlexander Motin 	while (len > 0) {
1094ee7f31c0SAlexander Motin 		bio = g_alloc_bio();
1095ee7f31c0SAlexander Motin 		bio->bio_cmd	    = BIO_DELETE;
10963236151eSAlexander Motin 		bio->bio_dev	    = dev;
1097ee7f31c0SAlexander Motin 		bio->bio_offset	    = off;
10988f5a226aSAlexander Motin 		bio->bio_length	    = MIN(len, maxlen);
1099ee7f31c0SAlexander Motin 		bio->bio_data	    = 0;
1100ee7f31c0SAlexander Motin 		bio->bio_done	    = ctl_be_block_biodone;
1101ee7f31c0SAlexander Motin 		bio->bio_caller1    = beio;
11020bcd4ab6SAlexander Motin 		bio->bio_pblkno     = off / be_lun->cbe_lun.blocksize;
1103ee7f31c0SAlexander Motin 
1104ee7f31c0SAlexander Motin 		off += bio->bio_length;
1105ee7f31c0SAlexander Motin 		len -= bio->bio_length;
1106ee7f31c0SAlexander Motin 
110775c7a1d3SAlexander Motin 		mtx_lock(&be_lun->io_lock);
1108ee7f31c0SAlexander Motin 		beio->num_bios_sent++;
1109ee7f31c0SAlexander Motin 		if (last && len == 0)
1110ee7f31c0SAlexander Motin 			beio->send_complete = 1;
111175c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
1112ee7f31c0SAlexander Motin 
11133236151eSAlexander Motin 		if (csw) {
11143236151eSAlexander Motin 			csw->d_strategy(bio);
11153236151eSAlexander Motin 		} else {
11163236151eSAlexander Motin 			bio->bio_error = ENXIO;
11173236151eSAlexander Motin 			ctl_be_block_biodone(bio);
1118ee7f31c0SAlexander Motin 		}
1119ee7f31c0SAlexander Motin 	}
11203236151eSAlexander Motin 	if (csw)
11213236151eSAlexander Motin 		dev_relthread(dev, ref);
11223236151eSAlexander Motin }
1123ee7f31c0SAlexander Motin 
1124ee7f31c0SAlexander Motin static void
1125ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1126ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio)
1127ee7f31c0SAlexander Motin {
1128ee7f31c0SAlexander Motin 	union ctl_io *io;
112966df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1130ee7f31c0SAlexander Motin 	struct scsi_unmap_desc *buf, *end;
1131ee7f31c0SAlexander Motin 	uint64_t len;
1132ee7f31c0SAlexander Motin 
1133ee7f31c0SAlexander Motin 	io = beio->io;
1134ee7f31c0SAlexander Motin 
1135ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1136ee7f31c0SAlexander Motin 
1137ee7f31c0SAlexander Motin 	binuptime(&beio->ds_t0);
1138ee7f31c0SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1139ee7f31c0SAlexander Motin 
1140ee7f31c0SAlexander Motin 	if (beio->io_offset == -1) {
1141ee7f31c0SAlexander Motin 		beio->io_len = 0;
114266df9136SAlexander Motin 		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
114366df9136SAlexander Motin 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
114466df9136SAlexander Motin 		end = buf + ptrlen->len / sizeof(*buf);
1145ee7f31c0SAlexander Motin 		for (; buf < end; buf++) {
1146ee7f31c0SAlexander Motin 			len = (uint64_t)scsi_4btoul(buf->length) *
11470bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
1148ee7f31c0SAlexander Motin 			beio->io_len += len;
1149ee7f31c0SAlexander Motin 			ctl_be_block_unmap_dev_range(be_lun, beio,
11500bcd4ab6SAlexander Motin 			    scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
11510bcd4ab6SAlexander Motin 			    len, (end - buf < 2) ? TRUE : FALSE);
1152ee7f31c0SAlexander Motin 		}
1153ee7f31c0SAlexander Motin 	} else
1154ee7f31c0SAlexander Motin 		ctl_be_block_unmap_dev_range(be_lun, beio,
1155ee7f31c0SAlexander Motin 		    beio->io_offset, beio->io_len, TRUE);
1156ee7f31c0SAlexander Motin }
1157ee7f31c0SAlexander Motin 
1158ee7f31c0SAlexander Motin static void
1159130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1160130f4520SKenneth D. Merry 			  struct ctl_be_block_io *beio)
1161130f4520SKenneth D. Merry {
116275c7a1d3SAlexander Motin 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1163130f4520SKenneth D. Merry 	struct bio *bio;
11643236151eSAlexander Motin 	struct cdevsw *csw;
11653236151eSAlexander Motin 	struct cdev *dev;
1166130f4520SKenneth D. Merry 	off_t cur_offset;
11673236151eSAlexander Motin 	int i, max_iosize, ref;
1168130f4520SKenneth D. Merry 
1169130f4520SKenneth D. Merry 	DPRINTF("entered\n");
11703236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1171130f4520SKenneth D. Merry 
1172130f4520SKenneth D. Merry 	/*
1173130f4520SKenneth D. Merry 	 * We have to limit our I/O size to the maximum supported by the
11748054320eSAlexander Motin 	 * backend device.
1175130f4520SKenneth D. Merry 	 */
11763236151eSAlexander Motin 	if (csw) {
11773236151eSAlexander Motin 		max_iosize = dev->si_iosize_max;
1178130f4520SKenneth D. Merry 		if (max_iosize < PAGE_SIZE)
1179130f4520SKenneth D. Merry 			max_iosize = DFLTPHYS;
11803236151eSAlexander Motin 	} else
11813236151eSAlexander Motin 		max_iosize = DFLTPHYS;
1182130f4520SKenneth D. Merry 
1183130f4520SKenneth D. Merry 	cur_offset = beio->io_offset;
1184130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
1185130f4520SKenneth D. Merry 		size_t cur_size;
1186130f4520SKenneth D. Merry 		uint8_t *cur_ptr;
1187130f4520SKenneth D. Merry 
1188130f4520SKenneth D. Merry 		cur_size = beio->sg_segs[i].len;
1189130f4520SKenneth D. Merry 		cur_ptr = beio->sg_segs[i].addr;
1190130f4520SKenneth D. Merry 
1191130f4520SKenneth D. Merry 		while (cur_size > 0) {
1192130f4520SKenneth D. Merry 			/* This can't fail, it's a blocking allocation. */
1193130f4520SKenneth D. Merry 			bio = g_alloc_bio();
1194130f4520SKenneth D. Merry 
1195130f4520SKenneth D. Merry 			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1196130f4520SKenneth D. Merry 
1197130f4520SKenneth D. Merry 			bio->bio_cmd = beio->bio_cmd;
11983236151eSAlexander Motin 			bio->bio_dev = dev;
1199130f4520SKenneth D. Merry 			bio->bio_caller1 = beio;
1200130f4520SKenneth D. Merry 			bio->bio_length = min(cur_size, max_iosize);
1201130f4520SKenneth D. Merry 			bio->bio_offset = cur_offset;
1202130f4520SKenneth D. Merry 			bio->bio_data = cur_ptr;
1203130f4520SKenneth D. Merry 			bio->bio_done = ctl_be_block_biodone;
12040bcd4ab6SAlexander Motin 			bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1205130f4520SKenneth D. Merry 
1206130f4520SKenneth D. Merry 			cur_offset += bio->bio_length;
1207130f4520SKenneth D. Merry 			cur_ptr += bio->bio_length;
1208130f4520SKenneth D. Merry 			cur_size -= bio->bio_length;
1209130f4520SKenneth D. Merry 
121075c7a1d3SAlexander Motin 			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1211130f4520SKenneth D. Merry 			beio->num_bios_sent++;
1212130f4520SKenneth D. Merry 		}
1213130f4520SKenneth D. Merry 	}
121475c7a1d3SAlexander Motin 	beio->send_complete = 1;
1215024932aaSAlexander Motin 	binuptime(&beio->ds_t0);
1216024932aaSAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
121775c7a1d3SAlexander Motin 
121875c7a1d3SAlexander Motin 	/*
121975c7a1d3SAlexander Motin 	 * Fire off all allocated requests!
122075c7a1d3SAlexander Motin 	 */
122175c7a1d3SAlexander Motin 	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
122275c7a1d3SAlexander Motin 		TAILQ_REMOVE(&queue, bio, bio_queue);
12233236151eSAlexander Motin 		if (csw)
12243236151eSAlexander Motin 			csw->d_strategy(bio);
12253236151eSAlexander Motin 		else {
12263236151eSAlexander Motin 			bio->bio_error = ENXIO;
12273236151eSAlexander Motin 			ctl_be_block_biodone(bio);
122875c7a1d3SAlexander Motin 		}
1229130f4520SKenneth D. Merry 	}
12303236151eSAlexander Motin 	if (csw)
12313236151eSAlexander Motin 		dev_relthread(dev, ref);
12323236151eSAlexander Motin }
1233130f4520SKenneth D. Merry 
1234c3e7ba3eSAlexander Motin static uint64_t
1235c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1236c3e7ba3eSAlexander Motin {
1237c3e7ba3eSAlexander Motin 	struct diocgattr_arg	arg;
12383236151eSAlexander Motin 	struct cdevsw *csw;
12393236151eSAlexander Motin 	struct cdev *dev;
12403236151eSAlexander Motin 	int error, ref;
1241c3e7ba3eSAlexander Motin 
12423236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
12433236151eSAlexander Motin 	if (csw == NULL)
1244c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1245c3e7ba3eSAlexander Motin 	strlcpy(arg.name, attrname, sizeof(arg.name));
1246c3e7ba3eSAlexander Motin 	arg.len = sizeof(arg.value.off);
12473236151eSAlexander Motin 	if (csw->d_ioctl) {
12483236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
12493236151eSAlexander Motin 		    curthread);
12503236151eSAlexander Motin 	} else
12513236151eSAlexander Motin 		error = ENODEV;
12523236151eSAlexander Motin 	dev_relthread(dev, ref);
1253c3e7ba3eSAlexander Motin 	if (error != 0)
1254c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1255c3e7ba3eSAlexander Motin 	return (arg.value.off);
1256c3e7ba3eSAlexander Motin }
1257c3e7ba3eSAlexander Motin 
1258130f4520SKenneth D. Merry static void
12597d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
12607d0d4342SAlexander Motin 			    union ctl_io *io)
12617d0d4342SAlexander Motin {
12620bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
12637d0d4342SAlexander Motin 	struct ctl_be_block_io *beio;
12647d0d4342SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
12657d0d4342SAlexander Motin 
12667d0d4342SAlexander Motin 	DPRINTF("entered\n");
12677d0d4342SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
12687d0d4342SAlexander Motin 	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
12697d0d4342SAlexander Motin 
12700bcd4ab6SAlexander Motin 	beio->io_len = lbalen->len * cbe_lun->blocksize;
12710bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
12727d0d4342SAlexander Motin 	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
12737d0d4342SAlexander Motin 	beio->bio_cmd = BIO_FLUSH;
12747d0d4342SAlexander Motin 	beio->ds_trans_type = DEVSTAT_NO_DATA;
12757d0d4342SAlexander Motin 	DPRINTF("SYNC\n");
12767d0d4342SAlexander Motin 	be_lun->lun_flush(be_lun, beio);
12777d0d4342SAlexander Motin }
12787d0d4342SAlexander Motin 
12797d0d4342SAlexander Motin static void
1280ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1281ee7f31c0SAlexander Motin {
1282ee7f31c0SAlexander Motin 	union ctl_io *io;
1283ee7f31c0SAlexander Motin 
1284ee7f31c0SAlexander Motin 	io = beio->io;
1285ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1286ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1287ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1288ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1289ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1290ee7f31c0SAlexander Motin 		return;
1291ee7f31c0SAlexander Motin 	}
1292ee7f31c0SAlexander Motin 
1293ee7f31c0SAlexander Motin 	ctl_be_block_config_write(io);
1294ee7f31c0SAlexander Motin }
1295ee7f31c0SAlexander Motin 
1296ee7f31c0SAlexander Motin static void
1297ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1298ee7f31c0SAlexander Motin 			    union ctl_io *io)
1299ee7f31c0SAlexander Motin {
13000d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
13010bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1302ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
130366df9136SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1304fee04ef7SAlexander Motin 	uint64_t len_left, lba;
1305fee04ef7SAlexander Motin 	uint32_t pb, pbo, adj;
1306ee7f31c0SAlexander Motin 	int i, seglen;
1307ee7f31c0SAlexander Motin 	uint8_t *buf, *end;
1308ee7f31c0SAlexander Motin 
1309ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1310ee7f31c0SAlexander Motin 
1311e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
131211b569f7SAlexander Motin 	lbalen = ARGS(beio->io);
1313ee7f31c0SAlexander Motin 
131464c5167cSAlexander Motin 	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
13153406a2a0SAlexander Motin 	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1316ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1317ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1318ee7f31c0SAlexander Motin 				      /*sks_valid*/ 1,
1319ee7f31c0SAlexander Motin 				      /*command*/ 1,
1320ee7f31c0SAlexander Motin 				      /*field*/ 1,
1321ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1322ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1323ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1324ee7f31c0SAlexander Motin 		return;
1325ee7f31c0SAlexander Motin 	}
1326ee7f31c0SAlexander Motin 
13273406a2a0SAlexander Motin 	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
13280bcd4ab6SAlexander Motin 		beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13290bcd4ab6SAlexander Motin 		beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1330ee7f31c0SAlexander Motin 		beio->bio_cmd = BIO_DELETE;
1331ee7f31c0SAlexander Motin 		beio->ds_trans_type = DEVSTAT_FREE;
1332ee7f31c0SAlexander Motin 
1333ee7f31c0SAlexander Motin 		be_lun->unmap(be_lun, beio);
1334ee7f31c0SAlexander Motin 		return;
1335ee7f31c0SAlexander Motin 	}
1336ee7f31c0SAlexander Motin 
1337ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_WRITE;
1338ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_WRITE;
1339ee7f31c0SAlexander Motin 
1340ee7f31c0SAlexander Motin 	DPRINTF("WRITE SAME at LBA %jx len %u\n",
134166df9136SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len);
1342ee7f31c0SAlexander Motin 
13430bcd4ab6SAlexander Motin 	pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
13440bcd4ab6SAlexander Motin 	if (be_lun->cbe_lun.pblockoff > 0)
13450bcd4ab6SAlexander Motin 		pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1346fee04ef7SAlexander Motin 	else
1347fee04ef7SAlexander Motin 		pbo = 0;
13480bcd4ab6SAlexander Motin 	len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1349*cd853791SKonstantin Belousov 	for (i = 0, lba = 0; i < CTLBLK_NUM_SEGS && len_left > 0; i++) {
1350ee7f31c0SAlexander Motin 		/*
1351ee7f31c0SAlexander Motin 		 * Setup the S/G entry for this chunk.
1352ee7f31c0SAlexander Motin 		 */
135308a7cce5SAlexander Motin 		seglen = MIN(CTLBLK_MAX_SEG, len_left);
13540bcd4ab6SAlexander Motin 		if (pb > cbe_lun->blocksize) {
13550bcd4ab6SAlexander Motin 			adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
135693b8c96cSAlexander Motin 			    seglen - pbo) % pb;
135793b8c96cSAlexander Motin 			if (seglen > adj)
135893b8c96cSAlexander Motin 				seglen -= adj;
135993b8c96cSAlexander Motin 			else
13600bcd4ab6SAlexander Motin 				seglen -= seglen % cbe_lun->blocksize;
136193b8c96cSAlexander Motin 		} else
13620bcd4ab6SAlexander Motin 			seglen -= seglen % cbe_lun->blocksize;
13638054320eSAlexander Motin 		ctl_alloc_seg(softc, &beio->sg_segs[i], seglen);
1364ee7f31c0SAlexander Motin 
1365ee7f31c0SAlexander Motin 		DPRINTF("segment %d addr %p len %zd\n", i,
1366ee7f31c0SAlexander Motin 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1367ee7f31c0SAlexander Motin 
1368ee7f31c0SAlexander Motin 		beio->num_segs++;
1369ee7f31c0SAlexander Motin 		len_left -= seglen;
1370ee7f31c0SAlexander Motin 
1371ee7f31c0SAlexander Motin 		buf = beio->sg_segs[i].addr;
1372ee7f31c0SAlexander Motin 		end = buf + seglen;
13730bcd4ab6SAlexander Motin 		for (; buf < end; buf += cbe_lun->blocksize) {
13746c2acea5SAlexander Motin 			if (lbalen->flags & SWS_NDOB) {
13756c2acea5SAlexander Motin 				memset(buf, 0, cbe_lun->blocksize);
13766c2acea5SAlexander Motin 			} else {
13776c2acea5SAlexander Motin 				memcpy(buf, io->scsiio.kern_data_ptr,
13786c2acea5SAlexander Motin 				    cbe_lun->blocksize);
13796c2acea5SAlexander Motin 			}
138066df9136SAlexander Motin 			if (lbalen->flags & SWS_LBDATA)
138166df9136SAlexander Motin 				scsi_ulto4b(lbalen->lba + lba, buf);
1382ee7f31c0SAlexander Motin 			lba++;
1383ee7f31c0SAlexander Motin 		}
1384ee7f31c0SAlexander Motin 	}
1385ee7f31c0SAlexander Motin 
13860bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13870bcd4ab6SAlexander Motin 	beio->io_len = lba * cbe_lun->blocksize;
1388ee7f31c0SAlexander Motin 
1389ee7f31c0SAlexander Motin 	/* We can not do all in one run. Correct and schedule rerun. */
1390ee7f31c0SAlexander Motin 	if (len_left > 0) {
139166df9136SAlexander Motin 		lbalen->lba += lba;
139266df9136SAlexander Motin 		lbalen->len -= lba;
1393ee7f31c0SAlexander Motin 		beio->beio_cont = ctl_be_block_cw_done_ws;
1394ee7f31c0SAlexander Motin 	}
1395ee7f31c0SAlexander Motin 
1396ee7f31c0SAlexander Motin 	be_lun->dispatch(be_lun, beio);
1397ee7f31c0SAlexander Motin }
1398ee7f31c0SAlexander Motin 
1399ee7f31c0SAlexander Motin static void
1400ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1401ee7f31c0SAlexander Motin 			    union ctl_io *io)
1402ee7f31c0SAlexander Motin {
1403ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
140466df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1405ee7f31c0SAlexander Motin 
1406ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1407ee7f31c0SAlexander Motin 
1408e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
140966df9136SAlexander Motin 	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1410ee7f31c0SAlexander Motin 
14113406a2a0SAlexander Motin 	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1412ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1413ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1414ee7f31c0SAlexander Motin 				      /*sks_valid*/ 0,
1415ee7f31c0SAlexander Motin 				      /*command*/ 1,
1416ee7f31c0SAlexander Motin 				      /*field*/ 0,
1417ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1418ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1419ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1420ee7f31c0SAlexander Motin 		return;
1421ee7f31c0SAlexander Motin 	}
1422ee7f31c0SAlexander Motin 
1423ee7f31c0SAlexander Motin 	beio->io_len = 0;
1424ee7f31c0SAlexander Motin 	beio->io_offset = -1;
1425ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_DELETE;
1426ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_FREE;
142766df9136SAlexander Motin 	DPRINTF("UNMAP\n");
1428ee7f31c0SAlexander Motin 	be_lun->unmap(be_lun, beio);
1429ee7f31c0SAlexander Motin }
1430ee7f31c0SAlexander Motin 
1431ee7f31c0SAlexander Motin static void
1432ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1433ef8daf3fSAlexander Motin {
1434ef8daf3fSAlexander Motin 	union ctl_io *io;
1435ef8daf3fSAlexander Motin 
1436ef8daf3fSAlexander Motin 	io = beio->io;
1437ef8daf3fSAlexander Motin 	ctl_free_beio(beio);
1438ef8daf3fSAlexander Motin 	ctl_config_read_done(io);
1439ef8daf3fSAlexander Motin }
1440ef8daf3fSAlexander Motin 
1441ef8daf3fSAlexander Motin static void
1442ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1443ef8daf3fSAlexander Motin 			 union ctl_io *io)
1444ef8daf3fSAlexander Motin {
1445ef8daf3fSAlexander Motin 	struct ctl_be_block_io *beio;
1446ef8daf3fSAlexander Motin 	struct ctl_be_block_softc *softc;
1447ef8daf3fSAlexander Motin 
1448ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1449ef8daf3fSAlexander Motin 
1450ef8daf3fSAlexander Motin 	softc = be_lun->softc;
1451ef8daf3fSAlexander Motin 	beio = ctl_alloc_beio(softc);
1452ef8daf3fSAlexander Motin 	beio->io = io;
1453ef8daf3fSAlexander Motin 	beio->lun = be_lun;
1454ef8daf3fSAlexander Motin 	beio->beio_cont = ctl_be_block_cr_done;
1455ef8daf3fSAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1456ef8daf3fSAlexander Motin 
1457ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
1458ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1459ef8daf3fSAlexander Motin 		beio->bio_cmd = -1;
1460ef8daf3fSAlexander Motin 		beio->ds_trans_type = DEVSTAT_NO_DATA;
1461ef8daf3fSAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1462ef8daf3fSAlexander Motin 		beio->io_len = 0;
1463ef8daf3fSAlexander Motin 		if (be_lun->get_lba_status)
1464ef8daf3fSAlexander Motin 			be_lun->get_lba_status(be_lun, beio);
1465ef8daf3fSAlexander Motin 		else
1466ef8daf3fSAlexander Motin 			ctl_be_block_cr_done(beio);
1467ef8daf3fSAlexander Motin 		break;
1468ef8daf3fSAlexander Motin 	default:
1469ef8daf3fSAlexander Motin 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1470ef8daf3fSAlexander Motin 		break;
1471ef8daf3fSAlexander Motin 	}
1472ef8daf3fSAlexander Motin }
1473ef8daf3fSAlexander Motin 
1474ef8daf3fSAlexander Motin static void
1475ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1476ee7f31c0SAlexander Motin {
1477ee7f31c0SAlexander Motin 	union ctl_io *io;
1478ee7f31c0SAlexander Motin 
1479ee7f31c0SAlexander Motin 	io = beio->io;
1480ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1481ee7f31c0SAlexander Motin 	ctl_config_write_done(io);
1482ee7f31c0SAlexander Motin }
1483ee7f31c0SAlexander Motin 
1484ee7f31c0SAlexander Motin static void
1485130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1486130f4520SKenneth D. Merry 			 union ctl_io *io)
1487130f4520SKenneth D. Merry {
1488130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1489130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1490130f4520SKenneth D. Merry 
1491130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1492130f4520SKenneth D. Merry 
1493130f4520SKenneth D. Merry 	softc = be_lun->softc;
1494130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1495130f4520SKenneth D. Merry 	beio->io = io;
1496130f4520SKenneth D. Merry 	beio->lun = be_lun;
1497ee7f31c0SAlexander Motin 	beio->beio_cont = ctl_be_block_cw_done;
14987d0d4342SAlexander Motin 	switch (io->scsiio.tag_type) {
14997d0d4342SAlexander Motin 	case CTL_TAG_ORDERED:
15007d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
15017d0d4342SAlexander Motin 		break;
15027d0d4342SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
15037d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
15047d0d4342SAlexander Motin 		break;
15057d0d4342SAlexander Motin 	case CTL_TAG_UNTAGGED:
15067d0d4342SAlexander Motin 	case CTL_TAG_SIMPLE:
15077d0d4342SAlexander Motin 	case CTL_TAG_ACA:
15087d0d4342SAlexander Motin 	default:
15097d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
15107d0d4342SAlexander Motin 		break;
15117d0d4342SAlexander Motin 	}
1512e86a4142SAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1513130f4520SKenneth D. Merry 
1514130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
1515130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
1516130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
15177d0d4342SAlexander Motin 		ctl_be_block_cw_dispatch_sync(be_lun, io);
1518130f4520SKenneth D. Merry 		break;
1519ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
1520ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
1521ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_ws(be_lun, io);
1522ee7f31c0SAlexander Motin 		break;
1523ee7f31c0SAlexander Motin 	case UNMAP:
1524ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1525ee7f31c0SAlexander Motin 		break;
1526130f4520SKenneth D. Merry 	default:
1527130f4520SKenneth D. Merry 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1528130f4520SKenneth D. Merry 		break;
1529130f4520SKenneth D. Merry 	}
1530130f4520SKenneth D. Merry }
1531130f4520SKenneth D. Merry 
153236160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
153336160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
153436160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
153536160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1536130f4520SKenneth D. Merry 
1537130f4520SKenneth D. Merry static void
153808a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio)
153908a7cce5SAlexander Motin {
154008a7cce5SAlexander Motin 	struct ctl_be_block_lun *be_lun;
154108a7cce5SAlexander Motin 	union ctl_io *io;
154208a7cce5SAlexander Motin 
154308a7cce5SAlexander Motin 	io = beio->io;
154408a7cce5SAlexander Motin 	be_lun = beio->lun;
154508a7cce5SAlexander Motin 	ctl_free_beio(beio);
1546ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1547ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1548ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
154911b569f7SAlexander Motin 		ctl_data_submit_done(io);
155008a7cce5SAlexander Motin 		return;
155108a7cce5SAlexander Motin 	}
155208a7cce5SAlexander Motin 
155308a7cce5SAlexander Motin 	io->io_hdr.status &= ~CTL_STATUS_MASK;
155408a7cce5SAlexander Motin 	io->io_hdr.status |= CTL_STATUS_NONE;
155508a7cce5SAlexander Motin 
155675c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
155708a7cce5SAlexander Motin 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
155875c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
155908a7cce5SAlexander Motin 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
156008a7cce5SAlexander Motin }
156108a7cce5SAlexander Motin 
156208a7cce5SAlexander Motin static void
1563130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1564130f4520SKenneth D. Merry 			   union ctl_io *io)
1565130f4520SKenneth D. Merry {
15660bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1567130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1568130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
156911b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1570e86a4142SAlexander Motin 	struct ctl_ptr_len_flags *bptrlen;
1571e86a4142SAlexander Motin 	uint64_t len_left, lbas;
1572130f4520SKenneth D. Merry 	int i;
1573130f4520SKenneth D. Merry 
1574130f4520SKenneth D. Merry 	softc = be_lun->softc;
1575130f4520SKenneth D. Merry 
1576130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1577130f4520SKenneth D. Merry 
157811b569f7SAlexander Motin 	lbalen = ARGS(io);
157911b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
158036160958SMark Johnston 		SDT_PROBE0(cbb, , write, start);
158111b569f7SAlexander Motin 	} else {
158236160958SMark Johnston 		SDT_PROBE0(cbb, , read, start);
1583130f4520SKenneth D. Merry 	}
1584130f4520SKenneth D. Merry 
1585130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1586130f4520SKenneth D. Merry 	beio->io = io;
1587130f4520SKenneth D. Merry 	beio->lun = be_lun;
1588e86a4142SAlexander Motin 	bptrlen = PRIV(io);
1589e86a4142SAlexander Motin 	bptrlen->ptr = (void *)beio;
1590130f4520SKenneth D. Merry 
1591130f4520SKenneth D. Merry 	switch (io->scsiio.tag_type) {
1592130f4520SKenneth D. Merry 	case CTL_TAG_ORDERED:
1593130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1594130f4520SKenneth D. Merry 		break;
1595130f4520SKenneth D. Merry 	case CTL_TAG_HEAD_OF_QUEUE:
1596130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1597130f4520SKenneth D. Merry 		break;
1598130f4520SKenneth D. Merry 	case CTL_TAG_UNTAGGED:
1599130f4520SKenneth D. Merry 	case CTL_TAG_SIMPLE:
1600130f4520SKenneth D. Merry 	case CTL_TAG_ACA:
1601130f4520SKenneth D. Merry 	default:
1602130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1603130f4520SKenneth D. Merry 		break;
1604130f4520SKenneth D. Merry 	}
1605130f4520SKenneth D. Merry 
160611b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1607130f4520SKenneth D. Merry 		beio->bio_cmd = BIO_WRITE;
1608130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_WRITE;
160911b569f7SAlexander Motin 	} else {
161011b569f7SAlexander Motin 		beio->bio_cmd = BIO_READ;
161111b569f7SAlexander Motin 		beio->ds_trans_type = DEVSTAT_READ;
1612130f4520SKenneth D. Merry 	}
1613130f4520SKenneth D. Merry 
161408a7cce5SAlexander Motin 	DPRINTF("%s at LBA %jx len %u @%ju\n",
1615130f4520SKenneth D. Merry 	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1616e86a4142SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
16170d7fed74SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE) {
16180d7fed74SAlexander Motin 		beio->two_sglists = 1;
161911b569f7SAlexander Motin 		lbas = CTLBLK_HALF_IO_SIZE;
16200d7fed74SAlexander Motin 	} else {
162111b569f7SAlexander Motin 		lbas = CTLBLK_MAX_IO_SIZE;
16220d7fed74SAlexander Motin 	}
16230bcd4ab6SAlexander Motin 	lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
16240bcd4ab6SAlexander Motin 	beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
16250bcd4ab6SAlexander Motin 	beio->io_len = lbas * cbe_lun->blocksize;
1626e86a4142SAlexander Motin 	bptrlen->len += lbas;
1627130f4520SKenneth D. Merry 
162808a7cce5SAlexander Motin 	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
162908a7cce5SAlexander Motin 		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
163008a7cce5SAlexander Motin 		    i, CTLBLK_MAX_SEGS));
1631130f4520SKenneth D. Merry 
1632130f4520SKenneth D. Merry 		/*
1633130f4520SKenneth D. Merry 		 * Setup the S/G entry for this chunk.
1634130f4520SKenneth D. Merry 		 */
16358054320eSAlexander Motin 		ctl_alloc_seg(softc, &beio->sg_segs[i],
1636*cd853791SKonstantin Belousov 		    MIN(CTLBLK_MAX_SEG, len_left));
1637130f4520SKenneth D. Merry 
1638130f4520SKenneth D. Merry 		DPRINTF("segment %d addr %p len %zd\n", i,
1639130f4520SKenneth D. Merry 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1640130f4520SKenneth D. Merry 
164111b569f7SAlexander Motin 		/* Set up second segment for compare operation. */
16420d7fed74SAlexander Motin 		if (beio->two_sglists) {
16438054320eSAlexander Motin 			ctl_alloc_seg(softc,
16448054320eSAlexander Motin 			    &beio->sg_segs[i + CTLBLK_HALF_SEGS],
16458054320eSAlexander Motin 			    beio->sg_segs[i].len);
164611b569f7SAlexander Motin 		}
164711b569f7SAlexander Motin 
1648130f4520SKenneth D. Merry 		beio->num_segs++;
1649130f4520SKenneth D. Merry 		len_left -= beio->sg_segs[i].len;
1650130f4520SKenneth D. Merry 	}
1651e86a4142SAlexander Motin 	if (bptrlen->len < lbalen->len)
165208a7cce5SAlexander Motin 		beio->beio_cont = ctl_be_block_next;
165308a7cce5SAlexander Motin 	io->scsiio.be_move_done = ctl_be_block_move_done;
165411b569f7SAlexander Motin 	/* For compare we have separate S/G lists for read and datamove. */
16550d7fed74SAlexander Motin 	if (beio->two_sglists)
165611b569f7SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
165711b569f7SAlexander Motin 	else
165808a7cce5SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
165908a7cce5SAlexander Motin 	io->scsiio.kern_data_len = beio->io_len;
166008a7cce5SAlexander Motin 	io->scsiio.kern_sg_entries = beio->num_segs;
16619a4510acSAlexander Motin 	io->scsiio.kern_data_ref = ctl_refcnt_beio;
16629a4510acSAlexander Motin 	io->scsiio.kern_data_arg = beio;
1663b2221369SAlexander Motin 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1664130f4520SKenneth D. Merry 
1665130f4520SKenneth D. Merry 	/*
1666130f4520SKenneth D. Merry 	 * For the read case, we need to read the data into our buffers and
1667130f4520SKenneth D. Merry 	 * then we can send it back to the user.  For the write case, we
1668130f4520SKenneth D. Merry 	 * need to get the data from the user first.
1669130f4520SKenneth D. Merry 	 */
1670130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
167136160958SMark Johnston 		SDT_PROBE0(cbb, , read, alloc_done);
1672130f4520SKenneth D. Merry 		be_lun->dispatch(be_lun, beio);
1673130f4520SKenneth D. Merry 	} else {
167436160958SMark Johnston 		SDT_PROBE0(cbb, , write, alloc_done);
1675130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
1676e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
1677130f4520SKenneth D. Merry #endif
1678130f4520SKenneth D. Merry 		ctl_datamove(io);
1679130f4520SKenneth D. Merry 	}
1680130f4520SKenneth D. Merry }
1681130f4520SKenneth D. Merry 
1682130f4520SKenneth D. Merry static void
1683130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending)
1684130f4520SKenneth D. Merry {
1685ee4ad294SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1686ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1687130f4520SKenneth D. Merry 	union ctl_io *io;
1688130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1689130f4520SKenneth D. Merry 
1690ee4ad294SAlexander Motin 	DPRINTF("entered\n");
1691ee4ad294SAlexander Motin 	/*
1692ee4ad294SAlexander Motin 	 * Fetch and process I/Os from all queues.  If we detect LUN
1693648dfc1aSAlexander Motin 	 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1694ee4ad294SAlexander Motin 	 * so make response maximally opaque to not confuse initiator.
1695ee4ad294SAlexander Motin 	 */
1696ee4ad294SAlexander Motin 	for (;;) {
1697ee4ad294SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
1698ee4ad294SAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1699ee4ad294SAlexander Motin 		if (io != NULL) {
1700130f4520SKenneth D. Merry 			DPRINTF("datamove queue\n");
1701130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1702130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
170375c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1704e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1705648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1706ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1707ee4ad294SAlexander Motin 				ctl_complete_beio(beio);
1708ee4ad294SAlexander Motin 				return;
1709ee4ad294SAlexander Motin 			}
1710130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1711130f4520SKenneth D. Merry 			continue;
1712130f4520SKenneth D. Merry 		}
1713130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1714130f4520SKenneth D. Merry 		if (io != NULL) {
1715130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
1716130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1717130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
171875c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1719648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1720ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1721ee4ad294SAlexander Motin 				ctl_config_write_done(io);
1722ee4ad294SAlexander Motin 				return;
1723ee4ad294SAlexander Motin 			}
1724130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1725ef8daf3fSAlexander Motin 			continue;
1726ef8daf3fSAlexander Motin 		}
1727ef8daf3fSAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1728ef8daf3fSAlexander Motin 		if (io != NULL) {
1729ef8daf3fSAlexander Motin 			DPRINTF("config read queue\n");
1730ef8daf3fSAlexander Motin 			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1731ef8daf3fSAlexander Motin 				      ctl_io_hdr, links);
1732ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1733648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1734ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1735ee4ad294SAlexander Motin 				ctl_config_read_done(io);
1736ee4ad294SAlexander Motin 				return;
1737ee4ad294SAlexander Motin 			}
1738ef8daf3fSAlexander Motin 			ctl_be_block_cr_dispatch(be_lun, io);
1739130f4520SKenneth D. Merry 			continue;
1740130f4520SKenneth D. Merry 		}
1741130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1742130f4520SKenneth D. Merry 		if (io != NULL) {
1743130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
1744130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1745130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
174675c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1747648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1748ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1749ee4ad294SAlexander Motin 				ctl_data_submit_done(io);
1750ee4ad294SAlexander Motin 				return;
1751ee4ad294SAlexander Motin 			}
1752130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1753130f4520SKenneth D. Merry 			continue;
1754130f4520SKenneth D. Merry 		}
1755130f4520SKenneth D. Merry 
1756130f4520SKenneth D. Merry 		/*
1757130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1758130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1759130f4520SKenneth D. Merry 		 */
1760ee4ad294SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
1761130f4520SKenneth D. Merry 		break;
1762130f4520SKenneth D. Merry 	}
1763130f4520SKenneth D. Merry }
1764130f4520SKenneth D. Merry 
1765130f4520SKenneth D. Merry /*
1766130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1767130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1768130f4520SKenneth D. Merry  * thread.
1769130f4520SKenneth D. Merry  */
1770130f4520SKenneth D. Merry static int
1771130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1772130f4520SKenneth D. Merry {
1773130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1774130f4520SKenneth D. Merry 
1775130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1776130f4520SKenneth D. Merry 
1777767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1778130f4520SKenneth D. Merry 
1779130f4520SKenneth D. Merry 	/*
1780130f4520SKenneth D. Merry 	 * Make sure we only get SCSI I/O.
1781130f4520SKenneth D. Merry 	 */
1782130f4520SKenneth D. Merry 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1783130f4520SKenneth D. Merry 		"%#x) encountered", io->io_hdr.io_type));
1784130f4520SKenneth D. Merry 
1785e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1786e86a4142SAlexander Motin 
178775c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
1788130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
178975c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
1790130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1791130f4520SKenneth D. Merry 
17929c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1793130f4520SKenneth D. Merry }
1794130f4520SKenneth D. Merry 
1795130f4520SKenneth D. Merry static int
1796130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1797130f4520SKenneth D. Merry 			int flag, struct thread *td)
1798130f4520SKenneth D. Merry {
179934144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
1800130f4520SKenneth D. Merry 	int error;
1801130f4520SKenneth D. Merry 
1802130f4520SKenneth D. Merry 	error = 0;
1803130f4520SKenneth D. Merry 	switch (cmd) {
1804130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1805130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1806130f4520SKenneth D. Merry 
1807130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1808130f4520SKenneth D. Merry 
1809130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1810130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1811130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1812130f4520SKenneth D. Merry 			break;
1813130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1814130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1815130f4520SKenneth D. Merry 			break;
181681177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
181781177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
181881177295SEdward Tomasz Napierala 			break;
1819130f4520SKenneth D. Merry 		default:
1820130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1821130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
182219720f41SAlexander Motin 				 "invalid LUN request type %d",
1823130f4520SKenneth D. Merry 				 lun_req->reqtype);
1824130f4520SKenneth D. Merry 			break;
1825130f4520SKenneth D. Merry 		}
1826130f4520SKenneth D. Merry 		break;
1827130f4520SKenneth D. Merry 	}
1828130f4520SKenneth D. Merry 	default:
1829130f4520SKenneth D. Merry 		error = ENOTTY;
1830130f4520SKenneth D. Merry 		break;
1831130f4520SKenneth D. Merry 	}
1832130f4520SKenneth D. Merry 
1833130f4520SKenneth D. Merry 	return (error);
1834130f4520SKenneth D. Merry }
1835130f4520SKenneth D. Merry 
1836130f4520SKenneth D. Merry static int
1837130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1838130f4520SKenneth D. Merry {
18390bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1840130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1841130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
18428951f055SMarcelo Araujo 	const char		     *value;
1843130f4520SKenneth D. Merry 	struct vattr		      vattr;
184434961f40SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1845130f4520SKenneth D. Merry 	int			      error;
1846130f4520SKenneth D. Merry 
18470bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
1848130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
184919720f41SAlexander Motin 	params = &be_lun->params;
1850130f4520SKenneth D. Merry 
1851130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1852130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1853130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1854ef8daf3fSAlexander Motin 	be_lun->get_lba_status = ctl_be_block_gls_file;
185553c146deSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_file;
18560bcd4ab6SAlexander Motin 	be_lun->unmap = NULL;
18570bcd4ab6SAlexander Motin 	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1858130f4520SKenneth D. Merry 
1859130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1860130f4520SKenneth D. Merry 	if (error != 0) {
1861130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1862130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1863130f4520SKenneth D. Merry 			 be_lun->dev_path);
1864130f4520SKenneth D. Merry 		return (error);
1865130f4520SKenneth D. Merry 	}
1866130f4520SKenneth D. Merry 
1867130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
186881177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
186981177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
187081177295SEdward Tomasz Napierala 	else
1871130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1872130f4520SKenneth D. Merry 
1873130f4520SKenneth D. Merry 	/*
187420a28d6cSAlexander Motin 	 * For files we can use any logical block size.  Prefer 512 bytes
187520a28d6cSAlexander Motin 	 * for compatibility reasons.  If file's vattr.va_blocksize
187620a28d6cSAlexander Motin 	 * (preferred I/O block size) is bigger and multiple to chosen
187720a28d6cSAlexander Motin 	 * logical block size -- report it as physical block size.
1878130f4520SKenneth D. Merry 	 */
1879130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
18800bcd4ab6SAlexander Motin 		cbe_lun->blocksize = params->blocksize_bytes;
188191be33dcSAlexander Motin 	else if (cbe_lun->lun_type == T_CDROM)
188291be33dcSAlexander Motin 		cbe_lun->blocksize = 2048;
1883130f4520SKenneth D. Merry 	else
18840bcd4ab6SAlexander Motin 		cbe_lun->blocksize = 512;
18850bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
18860bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
18870bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
188834961f40SAlexander Motin 
188934961f40SAlexander Motin 	us = ps = vattr.va_blocksize;
189034961f40SAlexander Motin 	uo = po = 0;
189134961f40SAlexander Motin 
18928951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
189334961f40SAlexander Motin 	if (value != NULL)
189434961f40SAlexander Motin 		ctl_expand_number(value, &ps);
18958951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
189634961f40SAlexander Motin 	if (value != NULL)
189734961f40SAlexander Motin 		ctl_expand_number(value, &po);
18980bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
18990bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
19000bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
19010bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
19020bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
19030bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
190434961f40SAlexander Motin 	}
190534961f40SAlexander Motin 
19068951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
190734961f40SAlexander Motin 	if (value != NULL)
190834961f40SAlexander Motin 		ctl_expand_number(value, &us);
19098951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
191034961f40SAlexander Motin 	if (value != NULL)
191134961f40SAlexander Motin 		ctl_expand_number(value, &uo);
19120bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
19130bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
19140bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
19150bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
19160bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
19170bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
191820a28d6cSAlexander Motin 	}
1919130f4520SKenneth D. Merry 
1920130f4520SKenneth D. Merry 	/*
1921130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1922130f4520SKenneth D. Merry 	 * sector long.
1923130f4520SKenneth D. Merry 	 */
19240bcd4ab6SAlexander Motin 	if (be_lun->size_bytes < cbe_lun->blocksize) {
1925130f4520SKenneth D. Merry 		error = EINVAL;
1926130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1927130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
19280bcd4ab6SAlexander Motin 			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1929130f4520SKenneth D. Merry 	}
1930cb8727e2SAlexander Motin 
19310bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1932130f4520SKenneth D. Merry 	return (error);
1933130f4520SKenneth D. Merry }
1934130f4520SKenneth D. Merry 
1935130f4520SKenneth D. Merry static int
1936130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1937130f4520SKenneth D. Merry {
19380bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1939130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
19403236151eSAlexander Motin 	struct cdevsw		     *csw;
1941130f4520SKenneth D. Merry 	struct cdev		     *dev;
19428951f055SMarcelo Araujo 	const char		     *value;
19433236151eSAlexander Motin 	int			      error, atomic, maxio, ref, unmap, tmp;
1944f6295033SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1945130f4520SKenneth D. Merry 
194619720f41SAlexander Motin 	params = &be_lun->params;
1947130f4520SKenneth D. Merry 
1948130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
19493236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
19503236151eSAlexander Motin 	if (csw == NULL)
19513236151eSAlexander Motin 		return (ENXIO);
19523236151eSAlexander Motin 	if (strcmp(csw->d_name, "zvol") == 0) {
195367f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1954ef8daf3fSAlexander Motin 		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1955cb8727e2SAlexander Motin 		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1956cb8727e2SAlexander Motin 	} else {
195767f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_dev;
19580bcd4ab6SAlexander Motin 		be_lun->get_lba_status = NULL;
1959cb8727e2SAlexander Motin 		atomic = 0;
19603236151eSAlexander Motin 		maxio = dev->si_iosize_max;
1961cb8727e2SAlexander Motin 		if (maxio <= 0)
1962cb8727e2SAlexander Motin 			maxio = DFLTPHYS;
19638054320eSAlexander Motin 		if (maxio > CTLBLK_MAX_SEG)
19648054320eSAlexander Motin 			maxio = CTLBLK_MAX_SEG;
1965cb8727e2SAlexander Motin 	}
196667f586a8SAlexander Motin 	be_lun->lun_flush = ctl_be_block_flush_dev;
1967c3e7ba3eSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_dev;
19680bcd4ab6SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
1969130f4520SKenneth D. Merry 
19703236151eSAlexander Motin 	if (!csw->d_ioctl) {
19713236151eSAlexander Motin 		dev_relthread(dev, ref);
1972130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
19733236151eSAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
1974130f4520SKenneth D. Merry 		return (ENODEV);
1975130f4520SKenneth D. Merry 	}
1976130f4520SKenneth D. Merry 
19773236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1978130f4520SKenneth D. Merry 			       curthread);
1979130f4520SKenneth D. Merry 	if (error) {
19803236151eSAlexander Motin 		dev_relthread(dev, ref);
1981130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
198219720f41SAlexander Motin 			 "error %d returned for DIOCGSECTORSIZE ioctl "
198319720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
1984130f4520SKenneth D. Merry 		return (error);
1985130f4520SKenneth D. Merry 	}
1986130f4520SKenneth D. Merry 
1987130f4520SKenneth D. Merry 	/*
1988130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
1989130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
1990130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
1991130f4520SKenneth D. Merry 	 * device's blocksize.
1992130f4520SKenneth D. Merry 	 */
1993a15bbf15SAlexander Motin 	if ((params->blocksize_bytes != 0) &&
1994a15bbf15SAlexander Motin 	    (params->blocksize_bytes >= tmp)) {
1995a15bbf15SAlexander Motin 		if (params->blocksize_bytes % tmp == 0) {
19960bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
1997130f4520SKenneth D. Merry 		} else {
19983236151eSAlexander Motin 			dev_relthread(dev, ref);
1999130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
200019720f41SAlexander Motin 				 "requested blocksize %u is not an even "
2001130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
2002f6295033SAlexander Motin 				 params->blocksize_bytes, tmp);
2003130f4520SKenneth D. Merry 			return (EINVAL);
2004130f4520SKenneth D. Merry 		}
2005a15bbf15SAlexander Motin 	} else if (params->blocksize_bytes != 0) {
20063236151eSAlexander Motin 		dev_relthread(dev, ref);
2007130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
200819720f41SAlexander Motin 			 "requested blocksize %u < backing device "
2009f6295033SAlexander Motin 			 "blocksize %u", params->blocksize_bytes, tmp);
2010130f4520SKenneth D. Merry 		return (EINVAL);
201191be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_CDROM)
201291be33dcSAlexander Motin 		cbe_lun->blocksize = MAX(tmp, 2048);
201391be33dcSAlexander Motin 	else
20140bcd4ab6SAlexander Motin 		cbe_lun->blocksize = tmp;
2015130f4520SKenneth D. Merry 
20163236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
2017130f4520SKenneth D. Merry 			     curthread);
2018130f4520SKenneth D. Merry 	if (error) {
20193236151eSAlexander Motin 		dev_relthread(dev, ref);
2020130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
202119720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE "
202219720f41SAlexander Motin 			 " ioctl on %s!", error,
202381177295SEdward Tomasz Napierala 			 be_lun->dev_path);
2024130f4520SKenneth D. Merry 		return (error);
2025130f4520SKenneth D. Merry 	}
2026130f4520SKenneth D. Merry 
202781177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
2028f6295033SAlexander Motin 		if (params->lun_size_bytes > otmp) {
20293236151eSAlexander Motin 			dev_relthread(dev, ref);
203081177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
203119720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
203219720f41SAlexander Motin 				 "size %ju",
203381177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
2034f6295033SAlexander Motin 				 (uintmax_t)otmp);
203581177295SEdward Tomasz Napierala 			return (EINVAL);
2036130f4520SKenneth D. Merry 		}
2037130f4520SKenneth D. Merry 
203881177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
2039a15bbf15SAlexander Motin 	} else
2040f6295033SAlexander Motin 		be_lun->size_bytes = otmp;
20410bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
20420bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
20430bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
204481177295SEdward Tomasz Napierala 
20453236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
20463236151eSAlexander Motin 	    curthread);
2047f6012722SAlexander Motin 	if (error)
2048f6012722SAlexander Motin 		ps = po = 0;
2049f6012722SAlexander Motin 	else {
20503236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
20513236151eSAlexander Motin 		    FREAD, curthread);
2052f6012722SAlexander Motin 		if (error)
2053f6012722SAlexander Motin 			po = 0;
2054f6012722SAlexander Motin 	}
205534961f40SAlexander Motin 	us = ps;
205634961f40SAlexander Motin 	uo = po;
205734961f40SAlexander Motin 
20588951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
205934961f40SAlexander Motin 	if (value != NULL)
206034961f40SAlexander Motin 		ctl_expand_number(value, &ps);
20618951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
206234961f40SAlexander Motin 	if (value != NULL)
206334961f40SAlexander Motin 		ctl_expand_number(value, &po);
20640bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
20650bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
20660bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
20670bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
20680bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
20690bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
2070f6012722SAlexander Motin 	}
2071f6012722SAlexander Motin 
20728951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
207334961f40SAlexander Motin 	if (value != NULL)
207434961f40SAlexander Motin 		ctl_expand_number(value, &us);
20758951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
207634961f40SAlexander Motin 	if (value != NULL)
207734961f40SAlexander Motin 		ctl_expand_number(value, &uo);
20780bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
20790bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
20800bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
20810bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
20820bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
20830bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
208434961f40SAlexander Motin 	}
208534961f40SAlexander Motin 
20860bcd4ab6SAlexander Motin 	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
20870bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2088fbc8d4ffSAlexander Motin 
2089fbc8d4ffSAlexander Motin 	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2090fbc8d4ffSAlexander Motin 		unmap = 1;
2091fbc8d4ffSAlexander Motin 	} else {
2092fbc8d4ffSAlexander Motin 		struct diocgattr_arg	arg;
2093fbc8d4ffSAlexander Motin 
2094fbc8d4ffSAlexander Motin 		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2095fbc8d4ffSAlexander Motin 		arg.len = sizeof(arg.value.i);
20963236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
20973236151eSAlexander Motin 		    curthread);
2098fbc8d4ffSAlexander Motin 		unmap = (error == 0) ? arg.value.i : 0;
2099fbc8d4ffSAlexander Motin 	}
21008951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2101fbc8d4ffSAlexander Motin 	if (value != NULL)
2102fbc8d4ffSAlexander Motin 		unmap = (strcmp(value, "on") == 0);
2103fbc8d4ffSAlexander Motin 	if (unmap)
21040bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
21050bcd4ab6SAlexander Motin 	else
21060bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2107fbc8d4ffSAlexander Motin 
21083236151eSAlexander Motin 	dev_relthread(dev, ref);
210981177295SEdward Tomasz Napierala 	return (0);
211081177295SEdward Tomasz Napierala }
2111130f4520SKenneth D. Merry 
2112130f4520SKenneth D. Merry static int
2113130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2114130f4520SKenneth D. Merry {
21150bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
21160bcd4ab6SAlexander Motin 	int flags;
2117130f4520SKenneth D. Merry 
21180bcd4ab6SAlexander Motin 	if (be_lun->vn) {
21190bcd4ab6SAlexander Motin 		flags = FREAD;
21200bcd4ab6SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
21210bcd4ab6SAlexander Motin 			flags |= FWRITE;
2122130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2123130f4520SKenneth D. Merry 		be_lun->vn = NULL;
2124130f4520SKenneth D. Merry 
2125130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
2126130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
2127130f4520SKenneth D. Merry 			break;
2128130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
2129130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
2130130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
2131130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
2132130f4520SKenneth D. Merry 			}
2133130f4520SKenneth D. Merry 			break;
2134130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
2135025a2301SEdward Tomasz Napierala 			break;
2136130f4520SKenneth D. Merry 		default:
21375124012aSAlexander Motin 			panic("Unexpected backend type %d", be_lun->dev_type);
2138130f4520SKenneth D. Merry 			break;
2139130f4520SKenneth D. Merry 		}
214019720f41SAlexander Motin 		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2141130f4520SKenneth D. Merry 	}
2142130f4520SKenneth D. Merry 	return (0);
2143130f4520SKenneth D. Merry }
2144130f4520SKenneth D. Merry 
2145130f4520SKenneth D. Merry static int
2146648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2147130f4520SKenneth D. Merry {
21480bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2149130f4520SKenneth D. Merry 	struct nameidata nd;
21508951f055SMarcelo Araujo 	const char	*value;
21510bcd4ab6SAlexander Motin 	int		 error, flags;
2152130f4520SKenneth D. Merry 
2153130f4520SKenneth D. Merry 	error = 0;
2154130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
2155130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
215619720f41SAlexander Motin 			 "Root filesystem is not mounted");
2157130f4520SKenneth D. Merry 		return (1);
2158130f4520SKenneth D. Merry 	}
21598a08cec1SMateusz Guzik 	pwd_ensure_dirs();
2160130f4520SKenneth D. Merry 
21618951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "file", NULL);
21620bcd4ab6SAlexander Motin 	if (value == NULL) {
21630bcd4ab6SAlexander Motin 		snprintf(req->error_str, sizeof(req->error_str),
21640bcd4ab6SAlexander Motin 			 "no file argument specified");
21650bcd4ab6SAlexander Motin 		return (1);
21660bcd4ab6SAlexander Motin 	}
21670bcd4ab6SAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
21680bcd4ab6SAlexander Motin 	be_lun->dev_path = strdup(value, M_CTLBLK);
21690bcd4ab6SAlexander Motin 
21700bcd4ab6SAlexander Motin 	flags = FREAD;
21718951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
217291be33dcSAlexander Motin 	if (value != NULL) {
217391be33dcSAlexander Motin 		if (strcmp(value, "on") != 0)
217491be33dcSAlexander Motin 			flags |= FWRITE;
217591be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_DIRECT)
21760bcd4ab6SAlexander Motin 		flags |= FWRITE;
21770bcd4ab6SAlexander Motin 
2178130f4520SKenneth D. Merry again:
2179130f4520SKenneth D. Merry 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2180130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
21810bcd4ab6SAlexander Motin 	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
21820bcd4ab6SAlexander Motin 		flags &= ~FWRITE;
21830bcd4ab6SAlexander Motin 		goto again;
21840bcd4ab6SAlexander Motin 	}
2185130f4520SKenneth D. Merry 	if (error) {
2186130f4520SKenneth D. Merry 		/*
2187130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
2188130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
2189130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
2190130f4520SKenneth D. Merry 		 * full path.
2191130f4520SKenneth D. Merry 		 */
2192130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
2193130f4520SKenneth D. Merry 			char *dev_name;
2194130f4520SKenneth D. Merry 
21950bcd4ab6SAlexander Motin 			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2196130f4520SKenneth D. Merry 				be_lun->dev_path);
2197130f4520SKenneth D. Merry 			free(be_lun->dev_path, M_CTLBLK);
2198130f4520SKenneth D. Merry 			be_lun->dev_path = dev_name;
2199130f4520SKenneth D. Merry 			goto again;
2200130f4520SKenneth D. Merry 		}
2201130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
220219720f41SAlexander Motin 		    "error opening %s: %d", be_lun->dev_path, error);
2203130f4520SKenneth D. Merry 		return (error);
2204130f4520SKenneth D. Merry 	}
22050bcd4ab6SAlexander Motin 	if (flags & FWRITE)
22060bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
22070bcd4ab6SAlexander Motin 	else
22080bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2209130f4520SKenneth D. Merry 
2210130f4520SKenneth D. Merry 	NDFREE(&nd, NDF_ONLY_PNBUF);
2211130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
2212130f4520SKenneth D. Merry 
2213130f4520SKenneth D. Merry 	/* We only support disks and files. */
22147ad2a82dSMateusz Guzik 	if (vn_isdisk_error(be_lun->vn, &error)) {
2215130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
2216130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
2217130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
2218130f4520SKenneth D. Merry 	} else {
2219130f4520SKenneth D. Merry 		error = EINVAL;
2220130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2221025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
2222130f4520SKenneth D. Merry 	}
2223b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
2224130f4520SKenneth D. Merry 
2225a15bbf15SAlexander Motin 	if (error != 0)
2226130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
22270bcd4ab6SAlexander Motin 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
22280bcd4ab6SAlexander Motin 	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
22290bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
22308951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
22310bcd4ab6SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
22320bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
22330bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "read") == 0)
22340bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
22350bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "off") == 0)
22360bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2237130f4520SKenneth D. Merry 	return (0);
2238130f4520SKenneth D. Merry }
2239130f4520SKenneth D. Merry 
2240130f4520SKenneth D. Merry static int
2241130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2242130f4520SKenneth D. Merry {
22430bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2244130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2245130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
224657a5db13SAlexander Motin 	char num_thread_str[16];
2247130f4520SKenneth D. Merry 	char tmpstr[32];
22488951f055SMarcelo Araujo 	const char *value;
2249fbc8d4ffSAlexander Motin 	int retval, num_threads;
225057a5db13SAlexander Motin 	int tmp_num_threads;
2251130f4520SKenneth D. Merry 
2252130f4520SKenneth D. Merry 	params = &req->reqdata.create;
2253130f4520SKenneth D. Merry 	retval = 0;
225419720f41SAlexander Motin 	req->status = CTL_LUN_OK;
2255130f4520SKenneth D. Merry 
2256130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
22570bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
225819720f41SAlexander Motin 	be_lun->params = req->reqdata.create;
2259130f4520SKenneth D. Merry 	be_lun->softc = softc;
2260130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
2261ef8daf3fSAlexander Motin 	STAILQ_INIT(&be_lun->config_read_queue);
2262130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
2263130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
226434144c2cSAlexander Motin 	mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
226534144c2cSAlexander Motin 	mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
22668951f055SMarcelo Araujo 	cbe_lun->options = nvlist_clone(req->args_nvl);
2267130f4520SKenneth D. Merry 
2268130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
22690bcd4ab6SAlexander Motin 		cbe_lun->lun_type = params->device_type;
2270130f4520SKenneth D. Merry 	else
22710bcd4ab6SAlexander Motin 		cbe_lun->lun_type = T_DIRECT;
227234144c2cSAlexander Motin 	be_lun->flags = 0;
22737ac58230SAlexander Motin 	cbe_lun->flags = 0;
22748951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
22757ac58230SAlexander Motin 	if (value != NULL) {
22767ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
22777ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
22787ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
22797ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2280130f4520SKenneth D. Merry 
228191be33dcSAlexander Motin 	if (cbe_lun->lun_type == T_DIRECT ||
228291be33dcSAlexander Motin 	    cbe_lun->lun_type == T_CDROM) {
2283a15bbf15SAlexander Motin 		be_lun->size_bytes = params->lun_size_bytes;
2284a15bbf15SAlexander Motin 		if (params->blocksize_bytes != 0)
22850bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
228691be33dcSAlexander Motin 		else if (cbe_lun->lun_type == T_CDROM)
228791be33dcSAlexander Motin 			cbe_lun->blocksize = 2048;
2288a15bbf15SAlexander Motin 		else
22890bcd4ab6SAlexander Motin 			cbe_lun->blocksize = 512;
22900bcd4ab6SAlexander Motin 		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
22910bcd4ab6SAlexander Motin 		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
22920bcd4ab6SAlexander Motin 		    0 : (be_lun->size_blocks - 1);
2293130f4520SKenneth D. Merry 
22947ac58230SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
22957ac58230SAlexander Motin 		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2296648dfc1aSAlexander Motin 			retval = ctl_be_block_open(be_lun, req);
2297130f4520SKenneth D. Merry 			if (retval != 0) {
2298130f4520SKenneth D. Merry 				retval = 0;
229919720f41SAlexander Motin 				req->status = CTL_LUN_WARNING;
2300130f4520SKenneth D. Merry 			}
23017ac58230SAlexander Motin 		}
23020bcd4ab6SAlexander Motin 		num_threads = cbb_num_threads;
2303130f4520SKenneth D. Merry 	} else {
2304130f4520SKenneth D. Merry 		num_threads = 1;
2305130f4520SKenneth D. Merry 	}
2306130f4520SKenneth D. Merry 
23078951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
230857a5db13SAlexander Motin 	if (value != NULL) {
230957a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
2310130f4520SKenneth D. Merry 
2311130f4520SKenneth D. Merry 		/*
2312130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
2313130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
2314130f4520SKenneth D. Merry 		 * specify 1000 threads.
2315130f4520SKenneth D. Merry 		 */
2316130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
2317130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
231819720f41SAlexander Motin 				 "invalid number of threads %s",
231919720f41SAlexander Motin 				 num_thread_str);
2320130f4520SKenneth D. Merry 			goto bailout_error;
2321130f4520SKenneth D. Merry 		}
2322130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
232357a5db13SAlexander Motin 	}
2324130f4520SKenneth D. Merry 
232519720f41SAlexander Motin 	if (be_lun->vn == NULL)
2326648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2327130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
232819720f41SAlexander Motin 	params->lun_size_bytes = be_lun->size_bytes;
23290bcd4ab6SAlexander Motin 	params->blocksize_bytes = cbe_lun->blocksize;
2330130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
23310bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = params->req_lun_id;
23320bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2333130f4520SKenneth D. Merry 	} else
23340bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = 0;
2335130f4520SKenneth D. Merry 
23360bcd4ab6SAlexander Motin 	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
23370bcd4ab6SAlexander Motin 	cbe_lun->be = &ctl_be_block_driver;
2338130f4520SKenneth D. Merry 
2339130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
234071cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2341130f4520SKenneth D. Merry 			 softc->num_luns);
23420bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, tmpstr,
23430bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2344130f4520SKenneth D. Merry 
2345130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
2346130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
2347e7038eb7SAlexander Motin 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2348130f4520SKenneth D. Merry 	} else {
23490bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
23500bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num),
2351130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
2352130f4520SKenneth D. Merry 	}
2353130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
235471cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
23550bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, tmpstr,
23560bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2357130f4520SKenneth D. Merry 
2358130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2359130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2360e7038eb7SAlexander Motin 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2361130f4520SKenneth D. Merry 	} else {
23620bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, params->device_id,
23630bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id),
2364130f4520SKenneth D. Merry 			    sizeof(params->device_id)));
2365130f4520SKenneth D. Merry 	}
2366130f4520SKenneth D. Merry 
2367130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2368130f4520SKenneth D. Merry 
236934144c2cSAlexander Motin 	be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2370130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2371130f4520SKenneth D. Merry 
2372130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2373130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
237419720f41SAlexander Motin 			 "unable to create taskqueue");
2375130f4520SKenneth D. Merry 		goto bailout_error;
2376130f4520SKenneth D. Merry 	}
2377130f4520SKenneth D. Merry 
2378130f4520SKenneth D. Merry 	/*
2379130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2380130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2381130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2382130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2383130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2384130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2385130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2386130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2387130f4520SKenneth D. Merry 	 *
2388130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2389130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2390130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2391130f4520SKenneth D. Merry 	 */
239212373e95SAlexander Motin 	retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2393130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2394053db1feSAlexander Motin 					 /*priority*/PUSER,
239512373e95SAlexander Motin 					 /*proc*/control_softc->ctl_proc,
239634144c2cSAlexander Motin 					 /*thread name*/"block");
2397130f4520SKenneth D. Merry 
2398130f4520SKenneth D. Merry 	if (retval != 0)
2399130f4520SKenneth D. Merry 		goto bailout_error;
2400130f4520SKenneth D. Merry 
2401130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2402130f4520SKenneth D. Merry 
24030bcd4ab6SAlexander Motin 	retval = ctl_add_lun(&be_lun->cbe_lun);
2404130f4520SKenneth D. Merry 	if (retval != 0) {
2405130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
240619720f41SAlexander Motin 			 "ctl_add_lun() returned error %d, see dmesg for "
240719720f41SAlexander Motin 			 "details", retval);
2408130f4520SKenneth D. Merry 		retval = 0;
2409130f4520SKenneth D. Merry 		goto bailout_error;
2410130f4520SKenneth D. Merry 	}
2411130f4520SKenneth D. Merry 
241234144c2cSAlexander Motin 	be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
24130bcd4ab6SAlexander Motin 					       cbe_lun->blocksize,
2414130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
24150bcd4ab6SAlexander Motin 					       cbe_lun->lun_type
2416130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2417130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2418130f4520SKenneth D. Merry 
241934144c2cSAlexander Motin 	mtx_lock(&softc->lock);
242034144c2cSAlexander Motin 	softc->num_luns++;
242134144c2cSAlexander Motin 	SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
242234144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
242334144c2cSAlexander Motin 
242434144c2cSAlexander Motin 	params->req_lun_id = cbe_lun->lun_id;
242534144c2cSAlexander Motin 
2426130f4520SKenneth D. Merry 	return (retval);
2427130f4520SKenneth D. Merry 
2428130f4520SKenneth D. Merry bailout_error:
2429130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2430130f4520SKenneth D. Merry 
24319e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
24329e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2433130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
24349e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2435130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
24368951f055SMarcelo Araujo 	nvlist_destroy(cbe_lun->options);
243775c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
243875c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2439130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2440130f4520SKenneth D. Merry 
2441130f4520SKenneth D. Merry 	return (retval);
2442130f4520SKenneth D. Merry }
2443130f4520SKenneth D. Merry 
2444130f4520SKenneth D. Merry static int
2445130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2446130f4520SKenneth D. Merry {
2447130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2448130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2449ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2450130f4520SKenneth D. Merry 	int retval;
2451130f4520SKenneth D. Merry 
2452130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2453130f4520SKenneth D. Merry 
245434144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
2455130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
245634144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
245734144c2cSAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id) {
245834144c2cSAlexander Motin 			SLIST_REMOVE(&softc->lun_list, be_lun,
245934144c2cSAlexander Motin 			    ctl_be_block_lun, links);
246034144c2cSAlexander Motin 			softc->num_luns--;
2461130f4520SKenneth D. Merry 			break;
2462130f4520SKenneth D. Merry 		}
246334144c2cSAlexander Motin 	}
2464130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
246534144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
2466130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2467130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
246819720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
246919720f41SAlexander Motin 			 params->lun_id);
2470130f4520SKenneth D. Merry 		goto bailout_error;
2471130f4520SKenneth D. Merry 	}
2472ee4ad294SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
2473130f4520SKenneth D. Merry 
2474ee4ad294SAlexander Motin 	if (be_lun->vn != NULL) {
2475648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2476648dfc1aSAlexander Motin 		ctl_lun_no_media(cbe_lun);
2477ee4ad294SAlexander Motin 		taskqueue_drain_all(be_lun->io_taskqueue);
2478ee4ad294SAlexander Motin 		ctl_be_block_close(be_lun);
2479ee4ad294SAlexander Motin 	}
2480ee4ad294SAlexander Motin 
248134144c2cSAlexander Motin 	mtx_lock(&softc->lock);
248234144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
248334144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
248434144c2cSAlexander Motin 
248534144c2cSAlexander Motin 	retval = ctl_remove_lun(cbe_lun);
2486130f4520SKenneth D. Merry 	if (retval != 0) {
2487130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
248834144c2cSAlexander Motin 			 "error %d returned from ctl_remove_lun() for "
248919720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
249034144c2cSAlexander Motin 		mtx_lock(&softc->lock);
249134144c2cSAlexander Motin 		be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
249234144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
2493130f4520SKenneth D. Merry 		goto bailout_error;
2494130f4520SKenneth D. Merry 	}
2495130f4520SKenneth D. Merry 
2496130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2497130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
249834144c2cSAlexander Motin 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2499130f4520SKenneth D. Merry 		if (retval == EINTR)
2500130f4520SKenneth D. Merry 			break;
2501130f4520SKenneth D. Merry 	}
2502130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
250334144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2504130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2505130f4520SKenneth D. Merry 		free(be_lun, M_CTLBLK);
250634144c2cSAlexander Motin 	} else {
250734144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
250834144c2cSAlexander Motin 		return (EINTR);
250934144c2cSAlexander Motin 	}
2510130f4520SKenneth D. Merry 
2511130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2512130f4520SKenneth D. Merry 	return (0);
2513130f4520SKenneth D. Merry 
2514130f4520SKenneth D. Merry bailout_error:
2515130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2516130f4520SKenneth D. Merry 	return (0);
2517130f4520SKenneth D. Merry }
2518130f4520SKenneth D. Merry 
251981177295SEdward Tomasz Napierala static int
252081177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
252181177295SEdward Tomasz Napierala {
252281177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
252381177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
2524a3977beaSAlexander Motin 	struct ctl_be_lun *cbe_lun;
25258951f055SMarcelo Araujo 	const char *value;
252671d8e97eSAlexander Motin 	uint64_t oldsize;
25277ac58230SAlexander Motin 	int error, wasprim;
252881177295SEdward Tomasz Napierala 
252981177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
253081177295SEdward Tomasz Napierala 
253134144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
253281177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
253334144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
25340bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
253581177295SEdward Tomasz Napierala 			break;
253681177295SEdward Tomasz Napierala 	}
253781177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
253881177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
253981177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
254019720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
254119720f41SAlexander Motin 			 params->lun_id);
254281177295SEdward Tomasz Napierala 		goto bailout_error;
254381177295SEdward Tomasz Napierala 	}
2544a3977beaSAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
254581177295SEdward Tomasz Napierala 
2546a3977beaSAlexander Motin 	if (params->lun_size_bytes != 0)
254719720f41SAlexander Motin 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
25488951f055SMarcelo Araujo 
2549efeedddcSAlexander Motin 	if (req->args_nvl != NULL) {
25508951f055SMarcelo Araujo 		nvlist_destroy(cbe_lun->options);
25518951f055SMarcelo Araujo 		cbe_lun->options = nvlist_clone(req->args_nvl);
2552efeedddcSAlexander Motin 	}
255381177295SEdward Tomasz Napierala 
25547ac58230SAlexander Motin 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
25558951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
25567ac58230SAlexander Motin 	if (value != NULL) {
25577ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
25587ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25597ac58230SAlexander Motin 		else
25607ac58230SAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25617ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
25627ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25637ac58230SAlexander Motin 	else
25647ac58230SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25657ac58230SAlexander Motin 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
25667ac58230SAlexander Motin 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
25677ac58230SAlexander Motin 			ctl_lun_primary(cbe_lun);
25687ac58230SAlexander Motin 		else
25697ac58230SAlexander Motin 			ctl_lun_secondary(cbe_lun);
25707ac58230SAlexander Motin 	}
25717ac58230SAlexander Motin 
25720bcd4ab6SAlexander Motin 	oldsize = be_lun->size_blocks;
25737ac58230SAlexander Motin 	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
25747ac58230SAlexander Motin 	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
257519720f41SAlexander Motin 		if (be_lun->vn == NULL)
2576648dfc1aSAlexander Motin 			error = ctl_be_block_open(be_lun, req);
25777ad2a82dSMateusz Guzik 		else if (vn_isdisk_error(be_lun->vn, &error))
25784ce7a086SAlexander Motin 			error = ctl_be_block_open_dev(be_lun, req);
25793d5cb709SAlexander Motin 		else if (be_lun->vn->v_type == VREG) {
25803d5cb709SAlexander Motin 			vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
25814ce7a086SAlexander Motin 			error = ctl_be_block_open_file(be_lun, req);
2582b249ce48SMateusz Guzik 			VOP_UNLOCK(be_lun->vn);
25833d5cb709SAlexander Motin 		} else
2584b9b4269cSAlexander Motin 			error = EINVAL;
2585648dfc1aSAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
25860bcd4ab6SAlexander Motin 		    be_lun->vn != NULL) {
2587648dfc1aSAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2588648dfc1aSAlexander Motin 			ctl_lun_has_media(cbe_lun);
2589648dfc1aSAlexander Motin 		} else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2590648dfc1aSAlexander Motin 		    be_lun->vn == NULL) {
2591648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2592648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
259371d8e97eSAlexander Motin 		}
2594648dfc1aSAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
25957ac58230SAlexander Motin 	} else {
25967ac58230SAlexander Motin 		if (be_lun->vn != NULL) {
2597648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2598648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
2599ee4ad294SAlexander Motin 			taskqueue_drain_all(be_lun->io_taskqueue);
26007ac58230SAlexander Motin 			error = ctl_be_block_close(be_lun);
26017ac58230SAlexander Motin 		} else
26027ac58230SAlexander Motin 			error = 0;
26037ac58230SAlexander Motin 	}
26047ac58230SAlexander Motin 	if (be_lun->size_blocks != oldsize)
26057ac58230SAlexander Motin 		ctl_lun_capacity_changed(cbe_lun);
260681177295SEdward Tomasz Napierala 
260781177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
260881177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
260981177295SEdward Tomasz Napierala 
261034144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
261119720f41SAlexander Motin 	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
261281177295SEdward Tomasz Napierala 	return (0);
261381177295SEdward Tomasz Napierala 
261481177295SEdward Tomasz Napierala bailout_error:
261534144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
261681177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
261781177295SEdward Tomasz Napierala 	return (0);
261881177295SEdward Tomasz Napierala }
261981177295SEdward Tomasz Napierala 
2620130f4520SKenneth D. Merry static void
2621767300e8SAlexander Motin ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2622130f4520SKenneth D. Merry {
2623767300e8SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
262434144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
262534144c2cSAlexander Motin 
262634144c2cSAlexander Motin 	taskqueue_drain_all(be_lun->io_taskqueue);
262734144c2cSAlexander Motin 	taskqueue_free(be_lun->io_taskqueue);
262834144c2cSAlexander Motin 	if (be_lun->disk_stats != NULL)
262934144c2cSAlexander Motin 		devstat_remove_entry(be_lun->disk_stats);
263034144c2cSAlexander Motin 	nvlist_destroy(be_lun->cbe_lun.options);
263134144c2cSAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
263234144c2cSAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
263334144c2cSAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2634130f4520SKenneth D. Merry 
2635130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
263634144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
263734144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
263834144c2cSAlexander Motin 		wakeup(be_lun);
263934144c2cSAlexander Motin 	else
264034144c2cSAlexander Motin 		free(be_lun, M_CTLBLK);
2641130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2642130f4520SKenneth D. Merry }
2643130f4520SKenneth D. Merry 
2644130f4520SKenneth D. Merry static int
2645130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2646130f4520SKenneth D. Merry {
2647130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
26480bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2649130f4520SKenneth D. Merry 	int retval;
2650130f4520SKenneth D. Merry 
2651130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2652130f4520SKenneth D. Merry 
26539cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
2654767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun;
2655130f4520SKenneth D. Merry 
265667cc546dSAlexander Motin 	retval = 0;
2657130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2658130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2659130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2660ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2661ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2662ee7f31c0SAlexander Motin 	case UNMAP:
2663130f4520SKenneth D. Merry 		/*
2664130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2665130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2666130f4520SKenneth D. Merry 		 *
2667130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2668130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2669130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2670130f4520SKenneth D. Merry 		 */
267175c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
2672130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2673130f4520SKenneth D. Merry 				   links);
267475c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
2675130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2676130f4520SKenneth D. Merry 		break;
2677130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2678130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2679648dfc1aSAlexander Motin 		struct ctl_lun_req req;
2680130f4520SKenneth D. Merry 
2681130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
268266b69676SAlexander Motin 		if ((cdb->how & SSS_PC_MASK) != 0) {
268366b69676SAlexander Motin 			ctl_set_success(&io->scsiio);
268466b69676SAlexander Motin 			ctl_config_write_done(io);
268566b69676SAlexander Motin 			break;
268666b69676SAlexander Motin 		}
2687648dfc1aSAlexander Motin 		if (cdb->how & SSS_START) {
2688648dfc1aSAlexander Motin 			if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2689648dfc1aSAlexander Motin 				retval = ctl_be_block_open(be_lun, &req);
2690648dfc1aSAlexander Motin 				cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2691648dfc1aSAlexander Motin 				if (retval == 0) {
2692648dfc1aSAlexander Motin 					cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2693648dfc1aSAlexander Motin 					ctl_lun_has_media(cbe_lun);
2694130f4520SKenneth D. Merry 				} else {
2695648dfc1aSAlexander Motin 					cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2696648dfc1aSAlexander Motin 					ctl_lun_no_media(cbe_lun);
2697130f4520SKenneth D. Merry 				}
2698648dfc1aSAlexander Motin 			}
2699648dfc1aSAlexander Motin 			ctl_start_lun(cbe_lun);
2700648dfc1aSAlexander Motin 		} else {
2701648dfc1aSAlexander Motin 			ctl_stop_lun(cbe_lun);
2702648dfc1aSAlexander Motin 			if (cdb->how & SSS_LOEJ) {
2703648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2704648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2705648dfc1aSAlexander Motin 				ctl_lun_ejected(cbe_lun);
2706648dfc1aSAlexander Motin 				if (be_lun->vn != NULL)
2707648dfc1aSAlexander Motin 					ctl_be_block_close(be_lun);
2708648dfc1aSAlexander Motin 			}
2709648dfc1aSAlexander Motin 		}
2710648dfc1aSAlexander Motin 
2711648dfc1aSAlexander Motin 		ctl_set_success(&io->scsiio);
2712130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2713130f4520SKenneth D. Merry 		break;
2714130f4520SKenneth D. Merry 	}
271591be33dcSAlexander Motin 	case PREVENT_ALLOW:
271691be33dcSAlexander Motin 		ctl_set_success(&io->scsiio);
271791be33dcSAlexander Motin 		ctl_config_write_done(io);
271891be33dcSAlexander Motin 		break;
2719130f4520SKenneth D. Merry 	default:
2720130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2721130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2722130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2723130f4520SKenneth D. Merry 		break;
2724130f4520SKenneth D. Merry 	}
2725130f4520SKenneth D. Merry 
2726130f4520SKenneth D. Merry 	return (retval);
2727130f4520SKenneth D. Merry }
2728130f4520SKenneth D. Merry 
2729130f4520SKenneth D. Merry static int
2730130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2731130f4520SKenneth D. Merry {
2732ef8daf3fSAlexander Motin 	struct ctl_be_block_lun *be_lun;
2733ef8daf3fSAlexander Motin 	int retval = 0;
2734ef8daf3fSAlexander Motin 
2735ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
2736ef8daf3fSAlexander Motin 
2737767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2738ef8daf3fSAlexander Motin 
2739ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
2740ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:
2741ef8daf3fSAlexander Motin 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2742ef8daf3fSAlexander Motin 			mtx_lock(&be_lun->queue_lock);
2743ef8daf3fSAlexander Motin 			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2744ef8daf3fSAlexander Motin 			    &io->io_hdr, links);
2745ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
2746ef8daf3fSAlexander Motin 			taskqueue_enqueue(be_lun->io_taskqueue,
2747ef8daf3fSAlexander Motin 			    &be_lun->io_task);
2748ef8daf3fSAlexander Motin 			retval = CTL_RETVAL_QUEUED;
2749ef8daf3fSAlexander Motin 			break;
2750ef8daf3fSAlexander Motin 		}
2751ef8daf3fSAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
2752ef8daf3fSAlexander Motin 				      /*sks_valid*/ 1,
2753ef8daf3fSAlexander Motin 				      /*command*/ 1,
2754ef8daf3fSAlexander Motin 				      /*field*/ 1,
2755ef8daf3fSAlexander Motin 				      /*bit_valid*/ 1,
2756ef8daf3fSAlexander Motin 				      /*bit*/ 4);
2757ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2758ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2759ef8daf3fSAlexander Motin 		break;
2760ef8daf3fSAlexander Motin 	default:
2761ef8daf3fSAlexander Motin 		ctl_set_invalid_opcode(&io->scsiio);
2762ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2763ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2764ef8daf3fSAlexander Motin 		break;
2765ef8daf3fSAlexander Motin 	}
2766ef8daf3fSAlexander Motin 
2767ef8daf3fSAlexander Motin 	return (retval);
2768130f4520SKenneth D. Merry }
2769130f4520SKenneth D. Merry 
2770130f4520SKenneth D. Merry static int
2771767300e8SAlexander Motin ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2772130f4520SKenneth D. Merry {
2773767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2774130f4520SKenneth D. Merry 	int retval;
2775130f4520SKenneth D. Merry 
27762cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "\t<num_threads>");
2777130f4520SKenneth D. Merry 	if (retval != 0)
2778130f4520SKenneth D. Merry 		goto bailout;
2779130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2780130f4520SKenneth D. Merry 	if (retval != 0)
2781130f4520SKenneth D. Merry 		goto bailout;
27822cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "</num_threads>\n");
2783130f4520SKenneth D. Merry 
2784130f4520SKenneth D. Merry bailout:
2785130f4520SKenneth D. Merry 	return (retval);
2786130f4520SKenneth D. Merry }
2787130f4520SKenneth D. Merry 
2788c3e7ba3eSAlexander Motin static uint64_t
2789767300e8SAlexander Motin ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2790c3e7ba3eSAlexander Motin {
2791767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2792c3e7ba3eSAlexander Motin 
2793c3e7ba3eSAlexander Motin 	if (lun->getattr == NULL)
2794c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
2795c3e7ba3eSAlexander Motin 	return (lun->getattr(lun, attrname));
2796c3e7ba3eSAlexander Motin }
2797c3e7ba3eSAlexander Motin 
27980c629e28SAlexander Motin static int
2799130f4520SKenneth D. Merry ctl_be_block_init(void)
2800130f4520SKenneth D. Merry {
28010c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
2802130f4520SKenneth D. Merry 
280334144c2cSAlexander Motin 	sx_init(&softc->modify_lock, "ctlblock modify");
280475c7a1d3SAlexander Motin 	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
28050c629e28SAlexander Motin 	softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2806a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2807*cd853791SKonstantin Belousov 	softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG,
28080d7fed74SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2809*cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2810*cd853791SKonstantin Belousov 		softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG,
28118054320eSAlexander Motin 		    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
281234144c2cSAlexander Motin 	SLIST_INIT(&softc->lun_list);
28130c629e28SAlexander Motin 	return (0);
28140c629e28SAlexander Motin }
2815130f4520SKenneth D. Merry 
28160c629e28SAlexander Motin static int
28170c629e28SAlexander Motin ctl_be_block_shutdown(void)
28180c629e28SAlexander Motin {
28190c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
282034144c2cSAlexander Motin 	struct ctl_be_block_lun *lun;
28210c629e28SAlexander Motin 
28220c629e28SAlexander Motin 	mtx_lock(&softc->lock);
282334144c2cSAlexander Motin 	while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
282434144c2cSAlexander Motin 		SLIST_REMOVE_HEAD(&softc->lun_list, links);
282534144c2cSAlexander Motin 		softc->num_luns--;
28260c629e28SAlexander Motin 		/*
282734144c2cSAlexander Motin 		 * Drop our lock here.  Since ctl_remove_lun() can call
28280c629e28SAlexander Motin 		 * back into us, this could potentially lead to a recursive
28290c629e28SAlexander Motin 		 * lock of the same mutex, which would cause a hang.
28300c629e28SAlexander Motin 		 */
28310c629e28SAlexander Motin 		mtx_unlock(&softc->lock);
283234144c2cSAlexander Motin 		ctl_remove_lun(&lun->cbe_lun);
28330c629e28SAlexander Motin 		mtx_lock(&softc->lock);
28340c629e28SAlexander Motin 	}
28350c629e28SAlexander Motin 	mtx_unlock(&softc->lock);
2836*cd853791SKonstantin Belousov 	uma_zdestroy(softc->bufmin_zone);
2837*cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2838*cd853791SKonstantin Belousov 		uma_zdestroy(softc->bufmax_zone);
28390c629e28SAlexander Motin 	uma_zdestroy(softc->beio_zone);
28400c629e28SAlexander Motin 	mtx_destroy(&softc->lock);
284134144c2cSAlexander Motin 	sx_destroy(&softc->modify_lock);
28420c629e28SAlexander Motin 	return (0);
2843130f4520SKenneth D. Merry }
2844