xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision 05d882b780f5be2da6f3d3bfef9160aacc4888d6)
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)
105cd853791SKonstantin Belousov #define	CTLBLK_MIN_SEG		(128 * 1024)
106cd853791SKonstantin Belousov #define	CTLBLK_MAX_SEG		MIN(CTLBLK_HALF_IO_SIZE, maxphys)
107cd853791SKonstantin Belousov #define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MIN_SEG, 1)
10811b569f7SAlexander Motin #define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
109cd853791SKonstantin 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;
194cd853791SKonstantin Belousov 	uma_zone_t			 bufmin_zone;
195cd853791SKonstantin 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 
310cd853791SKonstantin Belousov 	if (len <= CTLBLK_MIN_SEG) {
311cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK);
312cd853791SKonstantin Belousov 	} else {
313cd853791SKonstantin Belousov 		KASSERT(len <= CTLBLK_MAX_SEG,
314cd853791SKonstantin Belousov 		    ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG));
315cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK);
316cd853791SKonstantin 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 
324cd853791SKonstantin Belousov 	if (sg->len <= CTLBLK_MIN_SEG) {
325cd853791SKonstantin Belousov 		uma_zfree(softc->bufmin_zone, sg->addr);
326cd853791SKonstantin Belousov 	} else {
327cd853791SKonstantin Belousov 		KASSERT(sg->len <= CTLBLK_MAX_SEG,
328cd853791SKonstantin Belousov 		    ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG));
329cd853791SKonstantin Belousov 		uma_zfree(softc->bufmax_zone, sg->addr);
330cd853791SKonstantin 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;
1349cd853791SKonstantin 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],
1636cd853791SKonstantin 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");
1701*05d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links);
170275c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1703e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1704648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1705ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1706ee4ad294SAlexander Motin 				ctl_complete_beio(beio);
1707ee4ad294SAlexander Motin 				return;
1708ee4ad294SAlexander Motin 			}
1709130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1710130f4520SKenneth D. Merry 			continue;
1711130f4520SKenneth D. Merry 		}
1712130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1713130f4520SKenneth D. Merry 		if (io != NULL) {
1714130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
1715*05d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links);
171675c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1717648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1718ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1719ee4ad294SAlexander Motin 				ctl_config_write_done(io);
1720ee4ad294SAlexander Motin 				return;
1721ee4ad294SAlexander Motin 			}
1722130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1723ef8daf3fSAlexander Motin 			continue;
1724ef8daf3fSAlexander Motin 		}
1725ef8daf3fSAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1726ef8daf3fSAlexander Motin 		if (io != NULL) {
1727ef8daf3fSAlexander Motin 			DPRINTF("config read queue\n");
1728*05d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links);
1729ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1730648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1731ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1732ee4ad294SAlexander Motin 				ctl_config_read_done(io);
1733ee4ad294SAlexander Motin 				return;
1734ee4ad294SAlexander Motin 			}
1735ef8daf3fSAlexander Motin 			ctl_be_block_cr_dispatch(be_lun, io);
1736130f4520SKenneth D. Merry 			continue;
1737130f4520SKenneth D. Merry 		}
1738130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1739130f4520SKenneth D. Merry 		if (io != NULL) {
1740130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
1741*05d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->input_queue, links);
174275c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1743648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1744ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1745ee4ad294SAlexander Motin 				ctl_data_submit_done(io);
1746ee4ad294SAlexander Motin 				return;
1747ee4ad294SAlexander Motin 			}
1748130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1749130f4520SKenneth D. Merry 			continue;
1750130f4520SKenneth D. Merry 		}
1751130f4520SKenneth D. Merry 
1752130f4520SKenneth D. Merry 		/*
1753130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1754130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1755130f4520SKenneth D. Merry 		 */
1756ee4ad294SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
1757130f4520SKenneth D. Merry 		break;
1758130f4520SKenneth D. Merry 	}
1759130f4520SKenneth D. Merry }
1760130f4520SKenneth D. Merry 
1761130f4520SKenneth D. Merry /*
1762130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1763130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1764130f4520SKenneth D. Merry  * thread.
1765130f4520SKenneth D. Merry  */
1766130f4520SKenneth D. Merry static int
1767130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1768130f4520SKenneth D. Merry {
1769130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1770130f4520SKenneth D. Merry 
1771130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1772130f4520SKenneth D. Merry 
1773767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1774130f4520SKenneth D. Merry 
1775*05d882b7SAlexander Motin 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
1776*05d882b7SAlexander Motin 	    ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
1777130f4520SKenneth D. Merry 
1778e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1779e86a4142SAlexander Motin 
178075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
1781130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
178275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
1783130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1784130f4520SKenneth D. Merry 
17859c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1786130f4520SKenneth D. Merry }
1787130f4520SKenneth D. Merry 
1788130f4520SKenneth D. Merry static int
1789130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1790130f4520SKenneth D. Merry 			int flag, struct thread *td)
1791130f4520SKenneth D. Merry {
179234144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
1793130f4520SKenneth D. Merry 	int error;
1794130f4520SKenneth D. Merry 
1795130f4520SKenneth D. Merry 	error = 0;
1796130f4520SKenneth D. Merry 	switch (cmd) {
1797130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1798130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1799130f4520SKenneth D. Merry 
1800130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1801130f4520SKenneth D. Merry 
1802130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1803130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1804130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1805130f4520SKenneth D. Merry 			break;
1806130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1807130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1808130f4520SKenneth D. Merry 			break;
180981177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
181081177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
181181177295SEdward Tomasz Napierala 			break;
1812130f4520SKenneth D. Merry 		default:
1813130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1814130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
181519720f41SAlexander Motin 				 "invalid LUN request type %d",
1816130f4520SKenneth D. Merry 				 lun_req->reqtype);
1817130f4520SKenneth D. Merry 			break;
1818130f4520SKenneth D. Merry 		}
1819130f4520SKenneth D. Merry 		break;
1820130f4520SKenneth D. Merry 	}
1821130f4520SKenneth D. Merry 	default:
1822130f4520SKenneth D. Merry 		error = ENOTTY;
1823130f4520SKenneth D. Merry 		break;
1824130f4520SKenneth D. Merry 	}
1825130f4520SKenneth D. Merry 
1826130f4520SKenneth D. Merry 	return (error);
1827130f4520SKenneth D. Merry }
1828130f4520SKenneth D. Merry 
1829130f4520SKenneth D. Merry static int
1830130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1831130f4520SKenneth D. Merry {
18320bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1833130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1834130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
18358951f055SMarcelo Araujo 	const char		     *value;
1836130f4520SKenneth D. Merry 	struct vattr		      vattr;
183734961f40SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1838130f4520SKenneth D. Merry 	int			      error;
1839130f4520SKenneth D. Merry 
18400bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
1841130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
184219720f41SAlexander Motin 	params = &be_lun->params;
1843130f4520SKenneth D. Merry 
1844130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1845130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1846130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1847ef8daf3fSAlexander Motin 	be_lun->get_lba_status = ctl_be_block_gls_file;
184853c146deSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_file;
18490bcd4ab6SAlexander Motin 	be_lun->unmap = NULL;
18500bcd4ab6SAlexander Motin 	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1851130f4520SKenneth D. Merry 
1852130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1853130f4520SKenneth D. Merry 	if (error != 0) {
1854130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1855130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1856130f4520SKenneth D. Merry 			 be_lun->dev_path);
1857130f4520SKenneth D. Merry 		return (error);
1858130f4520SKenneth D. Merry 	}
1859130f4520SKenneth D. Merry 
1860130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
186181177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
186281177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
186381177295SEdward Tomasz Napierala 	else
1864130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1865130f4520SKenneth D. Merry 
1866130f4520SKenneth D. Merry 	/*
186720a28d6cSAlexander Motin 	 * For files we can use any logical block size.  Prefer 512 bytes
186820a28d6cSAlexander Motin 	 * for compatibility reasons.  If file's vattr.va_blocksize
186920a28d6cSAlexander Motin 	 * (preferred I/O block size) is bigger and multiple to chosen
187020a28d6cSAlexander Motin 	 * logical block size -- report it as physical block size.
1871130f4520SKenneth D. Merry 	 */
1872130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
18730bcd4ab6SAlexander Motin 		cbe_lun->blocksize = params->blocksize_bytes;
187491be33dcSAlexander Motin 	else if (cbe_lun->lun_type == T_CDROM)
187591be33dcSAlexander Motin 		cbe_lun->blocksize = 2048;
1876130f4520SKenneth D. Merry 	else
18770bcd4ab6SAlexander Motin 		cbe_lun->blocksize = 512;
18780bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
18790bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
18800bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
188134961f40SAlexander Motin 
188234961f40SAlexander Motin 	us = ps = vattr.va_blocksize;
188334961f40SAlexander Motin 	uo = po = 0;
188434961f40SAlexander Motin 
18858951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
188634961f40SAlexander Motin 	if (value != NULL)
188734961f40SAlexander Motin 		ctl_expand_number(value, &ps);
18888951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
188934961f40SAlexander Motin 	if (value != NULL)
189034961f40SAlexander Motin 		ctl_expand_number(value, &po);
18910bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
18920bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
18930bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
18940bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
18950bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
18960bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
189734961f40SAlexander Motin 	}
189834961f40SAlexander Motin 
18998951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
190034961f40SAlexander Motin 	if (value != NULL)
190134961f40SAlexander Motin 		ctl_expand_number(value, &us);
19028951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
190334961f40SAlexander Motin 	if (value != NULL)
190434961f40SAlexander Motin 		ctl_expand_number(value, &uo);
19050bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
19060bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
19070bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
19080bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
19090bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
19100bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
191120a28d6cSAlexander Motin 	}
1912130f4520SKenneth D. Merry 
1913130f4520SKenneth D. Merry 	/*
1914130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1915130f4520SKenneth D. Merry 	 * sector long.
1916130f4520SKenneth D. Merry 	 */
19170bcd4ab6SAlexander Motin 	if (be_lun->size_bytes < cbe_lun->blocksize) {
1918130f4520SKenneth D. Merry 		error = EINVAL;
1919130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1920130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
19210bcd4ab6SAlexander Motin 			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1922130f4520SKenneth D. Merry 	}
1923cb8727e2SAlexander Motin 
19240bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1925130f4520SKenneth D. Merry 	return (error);
1926130f4520SKenneth D. Merry }
1927130f4520SKenneth D. Merry 
1928130f4520SKenneth D. Merry static int
1929130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1930130f4520SKenneth D. Merry {
19310bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1932130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
19333236151eSAlexander Motin 	struct cdevsw		     *csw;
1934130f4520SKenneth D. Merry 	struct cdev		     *dev;
19358951f055SMarcelo Araujo 	const char		     *value;
19363236151eSAlexander Motin 	int			      error, atomic, maxio, ref, unmap, tmp;
1937f6295033SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1938130f4520SKenneth D. Merry 
193919720f41SAlexander Motin 	params = &be_lun->params;
1940130f4520SKenneth D. Merry 
1941130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
19423236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
19433236151eSAlexander Motin 	if (csw == NULL)
19443236151eSAlexander Motin 		return (ENXIO);
19453236151eSAlexander Motin 	if (strcmp(csw->d_name, "zvol") == 0) {
194667f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1947ef8daf3fSAlexander Motin 		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1948cb8727e2SAlexander Motin 		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1949cb8727e2SAlexander Motin 	} else {
195067f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_dev;
19510bcd4ab6SAlexander Motin 		be_lun->get_lba_status = NULL;
1952cb8727e2SAlexander Motin 		atomic = 0;
19533236151eSAlexander Motin 		maxio = dev->si_iosize_max;
1954cb8727e2SAlexander Motin 		if (maxio <= 0)
1955cb8727e2SAlexander Motin 			maxio = DFLTPHYS;
19568054320eSAlexander Motin 		if (maxio > CTLBLK_MAX_SEG)
19578054320eSAlexander Motin 			maxio = CTLBLK_MAX_SEG;
1958cb8727e2SAlexander Motin 	}
195967f586a8SAlexander Motin 	be_lun->lun_flush = ctl_be_block_flush_dev;
1960c3e7ba3eSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_dev;
19610bcd4ab6SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
1962130f4520SKenneth D. Merry 
19633236151eSAlexander Motin 	if (!csw->d_ioctl) {
19643236151eSAlexander Motin 		dev_relthread(dev, ref);
1965130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
19663236151eSAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
1967130f4520SKenneth D. Merry 		return (ENODEV);
1968130f4520SKenneth D. Merry 	}
1969130f4520SKenneth D. Merry 
19703236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1971130f4520SKenneth D. Merry 			       curthread);
1972130f4520SKenneth D. Merry 	if (error) {
19733236151eSAlexander Motin 		dev_relthread(dev, ref);
1974130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
197519720f41SAlexander Motin 			 "error %d returned for DIOCGSECTORSIZE ioctl "
197619720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
1977130f4520SKenneth D. Merry 		return (error);
1978130f4520SKenneth D. Merry 	}
1979130f4520SKenneth D. Merry 
1980130f4520SKenneth D. Merry 	/*
1981130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
1982130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
1983130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
1984130f4520SKenneth D. Merry 	 * device's blocksize.
1985130f4520SKenneth D. Merry 	 */
1986a15bbf15SAlexander Motin 	if ((params->blocksize_bytes != 0) &&
1987a15bbf15SAlexander Motin 	    (params->blocksize_bytes >= tmp)) {
1988a15bbf15SAlexander Motin 		if (params->blocksize_bytes % tmp == 0) {
19890bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
1990130f4520SKenneth D. Merry 		} else {
19913236151eSAlexander Motin 			dev_relthread(dev, ref);
1992130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
199319720f41SAlexander Motin 				 "requested blocksize %u is not an even "
1994130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
1995f6295033SAlexander Motin 				 params->blocksize_bytes, tmp);
1996130f4520SKenneth D. Merry 			return (EINVAL);
1997130f4520SKenneth D. Merry 		}
1998a15bbf15SAlexander Motin 	} else if (params->blocksize_bytes != 0) {
19993236151eSAlexander Motin 		dev_relthread(dev, ref);
2000130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
200119720f41SAlexander Motin 			 "requested blocksize %u < backing device "
2002f6295033SAlexander Motin 			 "blocksize %u", params->blocksize_bytes, tmp);
2003130f4520SKenneth D. Merry 		return (EINVAL);
200491be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_CDROM)
200591be33dcSAlexander Motin 		cbe_lun->blocksize = MAX(tmp, 2048);
200691be33dcSAlexander Motin 	else
20070bcd4ab6SAlexander Motin 		cbe_lun->blocksize = tmp;
2008130f4520SKenneth D. Merry 
20093236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
2010130f4520SKenneth D. Merry 			     curthread);
2011130f4520SKenneth D. Merry 	if (error) {
20123236151eSAlexander Motin 		dev_relthread(dev, ref);
2013130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
201419720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE "
201519720f41SAlexander Motin 			 " ioctl on %s!", error,
201681177295SEdward Tomasz Napierala 			 be_lun->dev_path);
2017130f4520SKenneth D. Merry 		return (error);
2018130f4520SKenneth D. Merry 	}
2019130f4520SKenneth D. Merry 
202081177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
2021f6295033SAlexander Motin 		if (params->lun_size_bytes > otmp) {
20223236151eSAlexander Motin 			dev_relthread(dev, ref);
202381177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
202419720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
202519720f41SAlexander Motin 				 "size %ju",
202681177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
2027f6295033SAlexander Motin 				 (uintmax_t)otmp);
202881177295SEdward Tomasz Napierala 			return (EINVAL);
2029130f4520SKenneth D. Merry 		}
2030130f4520SKenneth D. Merry 
203181177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
2032a15bbf15SAlexander Motin 	} else
2033f6295033SAlexander Motin 		be_lun->size_bytes = otmp;
20340bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
20350bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
20360bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
203781177295SEdward Tomasz Napierala 
20383236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
20393236151eSAlexander Motin 	    curthread);
2040f6012722SAlexander Motin 	if (error)
2041f6012722SAlexander Motin 		ps = po = 0;
2042f6012722SAlexander Motin 	else {
20433236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
20443236151eSAlexander Motin 		    FREAD, curthread);
2045f6012722SAlexander Motin 		if (error)
2046f6012722SAlexander Motin 			po = 0;
2047f6012722SAlexander Motin 	}
204834961f40SAlexander Motin 	us = ps;
204934961f40SAlexander Motin 	uo = po;
205034961f40SAlexander Motin 
20518951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
205234961f40SAlexander Motin 	if (value != NULL)
205334961f40SAlexander Motin 		ctl_expand_number(value, &ps);
20548951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
205534961f40SAlexander Motin 	if (value != NULL)
205634961f40SAlexander Motin 		ctl_expand_number(value, &po);
20570bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
20580bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
20590bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
20600bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
20610bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
20620bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
2063f6012722SAlexander Motin 	}
2064f6012722SAlexander Motin 
20658951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
206634961f40SAlexander Motin 	if (value != NULL)
206734961f40SAlexander Motin 		ctl_expand_number(value, &us);
20688951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
206934961f40SAlexander Motin 	if (value != NULL)
207034961f40SAlexander Motin 		ctl_expand_number(value, &uo);
20710bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
20720bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
20730bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
20740bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
20750bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
20760bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
207734961f40SAlexander Motin 	}
207834961f40SAlexander Motin 
20790bcd4ab6SAlexander Motin 	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
20800bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2081fbc8d4ffSAlexander Motin 
2082fbc8d4ffSAlexander Motin 	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2083fbc8d4ffSAlexander Motin 		unmap = 1;
2084fbc8d4ffSAlexander Motin 	} else {
2085fbc8d4ffSAlexander Motin 		struct diocgattr_arg	arg;
2086fbc8d4ffSAlexander Motin 
2087fbc8d4ffSAlexander Motin 		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2088fbc8d4ffSAlexander Motin 		arg.len = sizeof(arg.value.i);
20893236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
20903236151eSAlexander Motin 		    curthread);
2091fbc8d4ffSAlexander Motin 		unmap = (error == 0) ? arg.value.i : 0;
2092fbc8d4ffSAlexander Motin 	}
20938951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2094fbc8d4ffSAlexander Motin 	if (value != NULL)
2095fbc8d4ffSAlexander Motin 		unmap = (strcmp(value, "on") == 0);
2096fbc8d4ffSAlexander Motin 	if (unmap)
20970bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
20980bcd4ab6SAlexander Motin 	else
20990bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2100fbc8d4ffSAlexander Motin 
21013236151eSAlexander Motin 	dev_relthread(dev, ref);
210281177295SEdward Tomasz Napierala 	return (0);
210381177295SEdward Tomasz Napierala }
2104130f4520SKenneth D. Merry 
2105130f4520SKenneth D. Merry static int
2106130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2107130f4520SKenneth D. Merry {
21080bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
21090bcd4ab6SAlexander Motin 	int flags;
2110130f4520SKenneth D. Merry 
21110bcd4ab6SAlexander Motin 	if (be_lun->vn) {
21120bcd4ab6SAlexander Motin 		flags = FREAD;
21130bcd4ab6SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
21140bcd4ab6SAlexander Motin 			flags |= FWRITE;
2115130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2116130f4520SKenneth D. Merry 		be_lun->vn = NULL;
2117130f4520SKenneth D. Merry 
2118130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
2119130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
2120130f4520SKenneth D. Merry 			break;
2121130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
2122130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
2123130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
2124130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
2125130f4520SKenneth D. Merry 			}
2126130f4520SKenneth D. Merry 			break;
2127130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
2128025a2301SEdward Tomasz Napierala 			break;
2129130f4520SKenneth D. Merry 		default:
21305124012aSAlexander Motin 			panic("Unexpected backend type %d", be_lun->dev_type);
2131130f4520SKenneth D. Merry 			break;
2132130f4520SKenneth D. Merry 		}
213319720f41SAlexander Motin 		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2134130f4520SKenneth D. Merry 	}
2135130f4520SKenneth D. Merry 	return (0);
2136130f4520SKenneth D. Merry }
2137130f4520SKenneth D. Merry 
2138130f4520SKenneth D. Merry static int
2139648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2140130f4520SKenneth D. Merry {
21410bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2142130f4520SKenneth D. Merry 	struct nameidata nd;
21438951f055SMarcelo Araujo 	const char	*value;
21440bcd4ab6SAlexander Motin 	int		 error, flags;
2145130f4520SKenneth D. Merry 
2146130f4520SKenneth D. Merry 	error = 0;
2147130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
2148130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
214919720f41SAlexander Motin 			 "Root filesystem is not mounted");
2150130f4520SKenneth D. Merry 		return (1);
2151130f4520SKenneth D. Merry 	}
21528a08cec1SMateusz Guzik 	pwd_ensure_dirs();
2153130f4520SKenneth D. Merry 
21548951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "file", NULL);
21550bcd4ab6SAlexander Motin 	if (value == NULL) {
21560bcd4ab6SAlexander Motin 		snprintf(req->error_str, sizeof(req->error_str),
21570bcd4ab6SAlexander Motin 			 "no file argument specified");
21580bcd4ab6SAlexander Motin 		return (1);
21590bcd4ab6SAlexander Motin 	}
21600bcd4ab6SAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
21610bcd4ab6SAlexander Motin 	be_lun->dev_path = strdup(value, M_CTLBLK);
21620bcd4ab6SAlexander Motin 
21630bcd4ab6SAlexander Motin 	flags = FREAD;
21648951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
216591be33dcSAlexander Motin 	if (value != NULL) {
216691be33dcSAlexander Motin 		if (strcmp(value, "on") != 0)
216791be33dcSAlexander Motin 			flags |= FWRITE;
216891be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_DIRECT)
21690bcd4ab6SAlexander Motin 		flags |= FWRITE;
21700bcd4ab6SAlexander Motin 
2171130f4520SKenneth D. Merry again:
2172130f4520SKenneth D. Merry 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2173130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
21740bcd4ab6SAlexander Motin 	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
21750bcd4ab6SAlexander Motin 		flags &= ~FWRITE;
21760bcd4ab6SAlexander Motin 		goto again;
21770bcd4ab6SAlexander Motin 	}
2178130f4520SKenneth D. Merry 	if (error) {
2179130f4520SKenneth D. Merry 		/*
2180130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
2181130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
2182130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
2183130f4520SKenneth D. Merry 		 * full path.
2184130f4520SKenneth D. Merry 		 */
2185130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
2186130f4520SKenneth D. Merry 			char *dev_name;
2187130f4520SKenneth D. Merry 
21880bcd4ab6SAlexander Motin 			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2189130f4520SKenneth D. Merry 				be_lun->dev_path);
2190130f4520SKenneth D. Merry 			free(be_lun->dev_path, M_CTLBLK);
2191130f4520SKenneth D. Merry 			be_lun->dev_path = dev_name;
2192130f4520SKenneth D. Merry 			goto again;
2193130f4520SKenneth D. Merry 		}
2194130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
219519720f41SAlexander Motin 		    "error opening %s: %d", be_lun->dev_path, error);
2196130f4520SKenneth D. Merry 		return (error);
2197130f4520SKenneth D. Merry 	}
21980bcd4ab6SAlexander Motin 	if (flags & FWRITE)
21990bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
22000bcd4ab6SAlexander Motin 	else
22010bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2202130f4520SKenneth D. Merry 
2203130f4520SKenneth D. Merry 	NDFREE(&nd, NDF_ONLY_PNBUF);
2204130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
2205130f4520SKenneth D. Merry 
2206130f4520SKenneth D. Merry 	/* We only support disks and files. */
22077ad2a82dSMateusz Guzik 	if (vn_isdisk_error(be_lun->vn, &error)) {
2208130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
2209130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
2210130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
2211130f4520SKenneth D. Merry 	} else {
2212130f4520SKenneth D. Merry 		error = EINVAL;
2213130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2214025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
2215130f4520SKenneth D. Merry 	}
2216b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
2217130f4520SKenneth D. Merry 
2218a15bbf15SAlexander Motin 	if (error != 0)
2219130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
22200bcd4ab6SAlexander Motin 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
22210bcd4ab6SAlexander Motin 	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
22220bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
22238951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
22240bcd4ab6SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
22250bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
22260bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "read") == 0)
22270bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
22280bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "off") == 0)
22290bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2230130f4520SKenneth D. Merry 	return (0);
2231130f4520SKenneth D. Merry }
2232130f4520SKenneth D. Merry 
2233130f4520SKenneth D. Merry static int
2234130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2235130f4520SKenneth D. Merry {
22360bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2237130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2238130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
223957a5db13SAlexander Motin 	char num_thread_str[16];
2240130f4520SKenneth D. Merry 	char tmpstr[32];
22418951f055SMarcelo Araujo 	const char *value;
2242fbc8d4ffSAlexander Motin 	int retval, num_threads;
224357a5db13SAlexander Motin 	int tmp_num_threads;
2244130f4520SKenneth D. Merry 
2245130f4520SKenneth D. Merry 	params = &req->reqdata.create;
2246130f4520SKenneth D. Merry 	retval = 0;
224719720f41SAlexander Motin 	req->status = CTL_LUN_OK;
2248130f4520SKenneth D. Merry 
2249130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
22500bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
225119720f41SAlexander Motin 	be_lun->params = req->reqdata.create;
2252130f4520SKenneth D. Merry 	be_lun->softc = softc;
2253130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
2254ef8daf3fSAlexander Motin 	STAILQ_INIT(&be_lun->config_read_queue);
2255130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
2256130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
225734144c2cSAlexander Motin 	mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
225834144c2cSAlexander Motin 	mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
22598951f055SMarcelo Araujo 	cbe_lun->options = nvlist_clone(req->args_nvl);
2260130f4520SKenneth D. Merry 
2261130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
22620bcd4ab6SAlexander Motin 		cbe_lun->lun_type = params->device_type;
2263130f4520SKenneth D. Merry 	else
22640bcd4ab6SAlexander Motin 		cbe_lun->lun_type = T_DIRECT;
226534144c2cSAlexander Motin 	be_lun->flags = 0;
22667ac58230SAlexander Motin 	cbe_lun->flags = 0;
22678951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
22687ac58230SAlexander Motin 	if (value != NULL) {
22697ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
22707ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
22717ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
22727ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2273130f4520SKenneth D. Merry 
227491be33dcSAlexander Motin 	if (cbe_lun->lun_type == T_DIRECT ||
227591be33dcSAlexander Motin 	    cbe_lun->lun_type == T_CDROM) {
2276a15bbf15SAlexander Motin 		be_lun->size_bytes = params->lun_size_bytes;
2277a15bbf15SAlexander Motin 		if (params->blocksize_bytes != 0)
22780bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
227991be33dcSAlexander Motin 		else if (cbe_lun->lun_type == T_CDROM)
228091be33dcSAlexander Motin 			cbe_lun->blocksize = 2048;
2281a15bbf15SAlexander Motin 		else
22820bcd4ab6SAlexander Motin 			cbe_lun->blocksize = 512;
22830bcd4ab6SAlexander Motin 		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
22840bcd4ab6SAlexander Motin 		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
22850bcd4ab6SAlexander Motin 		    0 : (be_lun->size_blocks - 1);
2286130f4520SKenneth D. Merry 
22877ac58230SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
22887ac58230SAlexander Motin 		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2289648dfc1aSAlexander Motin 			retval = ctl_be_block_open(be_lun, req);
2290130f4520SKenneth D. Merry 			if (retval != 0) {
2291130f4520SKenneth D. Merry 				retval = 0;
229219720f41SAlexander Motin 				req->status = CTL_LUN_WARNING;
2293130f4520SKenneth D. Merry 			}
22947ac58230SAlexander Motin 		}
22950bcd4ab6SAlexander Motin 		num_threads = cbb_num_threads;
2296130f4520SKenneth D. Merry 	} else {
2297130f4520SKenneth D. Merry 		num_threads = 1;
2298130f4520SKenneth D. Merry 	}
2299130f4520SKenneth D. Merry 
23008951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
230157a5db13SAlexander Motin 	if (value != NULL) {
230257a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
2303130f4520SKenneth D. Merry 
2304130f4520SKenneth D. Merry 		/*
2305130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
2306130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
2307130f4520SKenneth D. Merry 		 * specify 1000 threads.
2308130f4520SKenneth D. Merry 		 */
2309130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
2310130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
231119720f41SAlexander Motin 				 "invalid number of threads %s",
231219720f41SAlexander Motin 				 num_thread_str);
2313130f4520SKenneth D. Merry 			goto bailout_error;
2314130f4520SKenneth D. Merry 		}
2315130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
231657a5db13SAlexander Motin 	}
2317130f4520SKenneth D. Merry 
231819720f41SAlexander Motin 	if (be_lun->vn == NULL)
2319648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2320130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
232119720f41SAlexander Motin 	params->lun_size_bytes = be_lun->size_bytes;
23220bcd4ab6SAlexander Motin 	params->blocksize_bytes = cbe_lun->blocksize;
2323130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
23240bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = params->req_lun_id;
23250bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2326130f4520SKenneth D. Merry 	} else
23270bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = 0;
2328130f4520SKenneth D. Merry 
23290bcd4ab6SAlexander Motin 	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
23300bcd4ab6SAlexander Motin 	cbe_lun->be = &ctl_be_block_driver;
2331130f4520SKenneth D. Merry 
2332130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
233371cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2334130f4520SKenneth D. Merry 			 softc->num_luns);
23350bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, tmpstr,
23360bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2337130f4520SKenneth D. Merry 
2338130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
2339130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
2340e7038eb7SAlexander Motin 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2341130f4520SKenneth D. Merry 	} else {
23420bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
23430bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num),
2344130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
2345130f4520SKenneth D. Merry 	}
2346130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
234771cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
23480bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, tmpstr,
23490bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2350130f4520SKenneth D. Merry 
2351130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2352130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2353e7038eb7SAlexander Motin 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2354130f4520SKenneth D. Merry 	} else {
23550bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, params->device_id,
23560bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id),
2357130f4520SKenneth D. Merry 			    sizeof(params->device_id)));
2358130f4520SKenneth D. Merry 	}
2359130f4520SKenneth D. Merry 
2360130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2361130f4520SKenneth D. Merry 
236234144c2cSAlexander Motin 	be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2363130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2364130f4520SKenneth D. Merry 
2365130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2366130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
236719720f41SAlexander Motin 			 "unable to create taskqueue");
2368130f4520SKenneth D. Merry 		goto bailout_error;
2369130f4520SKenneth D. Merry 	}
2370130f4520SKenneth D. Merry 
2371130f4520SKenneth D. Merry 	/*
2372130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2373130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2374130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2375130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2376130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2377130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2378130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2379130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2380130f4520SKenneth D. Merry 	 *
2381130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2382130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2383130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2384130f4520SKenneth D. Merry 	 */
238512373e95SAlexander Motin 	retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2386130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2387053db1feSAlexander Motin 					 /*priority*/PUSER,
238812373e95SAlexander Motin 					 /*proc*/control_softc->ctl_proc,
238934144c2cSAlexander Motin 					 /*thread name*/"block");
2390130f4520SKenneth D. Merry 
2391130f4520SKenneth D. Merry 	if (retval != 0)
2392130f4520SKenneth D. Merry 		goto bailout_error;
2393130f4520SKenneth D. Merry 
2394130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2395130f4520SKenneth D. Merry 
23960bcd4ab6SAlexander Motin 	retval = ctl_add_lun(&be_lun->cbe_lun);
2397130f4520SKenneth D. Merry 	if (retval != 0) {
2398130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
239919720f41SAlexander Motin 			 "ctl_add_lun() returned error %d, see dmesg for "
240019720f41SAlexander Motin 			 "details", retval);
2401130f4520SKenneth D. Merry 		retval = 0;
2402130f4520SKenneth D. Merry 		goto bailout_error;
2403130f4520SKenneth D. Merry 	}
2404130f4520SKenneth D. Merry 
240534144c2cSAlexander Motin 	be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
24060bcd4ab6SAlexander Motin 					       cbe_lun->blocksize,
2407130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
24080bcd4ab6SAlexander Motin 					       cbe_lun->lun_type
2409130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2410130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2411130f4520SKenneth D. Merry 
241234144c2cSAlexander Motin 	mtx_lock(&softc->lock);
241334144c2cSAlexander Motin 	softc->num_luns++;
241434144c2cSAlexander Motin 	SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
241534144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
241634144c2cSAlexander Motin 
241734144c2cSAlexander Motin 	params->req_lun_id = cbe_lun->lun_id;
241834144c2cSAlexander Motin 
2419130f4520SKenneth D. Merry 	return (retval);
2420130f4520SKenneth D. Merry 
2421130f4520SKenneth D. Merry bailout_error:
2422130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2423130f4520SKenneth D. Merry 
24249e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
24259e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2426130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
24279e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2428130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
24298951f055SMarcelo Araujo 	nvlist_destroy(cbe_lun->options);
243075c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
243175c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2432130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2433130f4520SKenneth D. Merry 
2434130f4520SKenneth D. Merry 	return (retval);
2435130f4520SKenneth D. Merry }
2436130f4520SKenneth D. Merry 
2437130f4520SKenneth D. Merry static int
2438130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2439130f4520SKenneth D. Merry {
2440130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2441130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2442ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2443130f4520SKenneth D. Merry 	int retval;
2444130f4520SKenneth D. Merry 
2445130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2446130f4520SKenneth D. Merry 
244734144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
2448130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
244934144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
245034144c2cSAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id) {
245134144c2cSAlexander Motin 			SLIST_REMOVE(&softc->lun_list, be_lun,
245234144c2cSAlexander Motin 			    ctl_be_block_lun, links);
245334144c2cSAlexander Motin 			softc->num_luns--;
2454130f4520SKenneth D. Merry 			break;
2455130f4520SKenneth D. Merry 		}
245634144c2cSAlexander Motin 	}
2457130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
245834144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
2459130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2460130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
246119720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
246219720f41SAlexander Motin 			 params->lun_id);
2463130f4520SKenneth D. Merry 		goto bailout_error;
2464130f4520SKenneth D. Merry 	}
2465ee4ad294SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
2466130f4520SKenneth D. Merry 
2467ee4ad294SAlexander Motin 	if (be_lun->vn != NULL) {
2468648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2469648dfc1aSAlexander Motin 		ctl_lun_no_media(cbe_lun);
2470ee4ad294SAlexander Motin 		taskqueue_drain_all(be_lun->io_taskqueue);
2471ee4ad294SAlexander Motin 		ctl_be_block_close(be_lun);
2472ee4ad294SAlexander Motin 	}
2473ee4ad294SAlexander Motin 
247434144c2cSAlexander Motin 	mtx_lock(&softc->lock);
247534144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
247634144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
247734144c2cSAlexander Motin 
247834144c2cSAlexander Motin 	retval = ctl_remove_lun(cbe_lun);
2479130f4520SKenneth D. Merry 	if (retval != 0) {
2480130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
248134144c2cSAlexander Motin 			 "error %d returned from ctl_remove_lun() for "
248219720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
248334144c2cSAlexander Motin 		mtx_lock(&softc->lock);
248434144c2cSAlexander Motin 		be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
248534144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
2486130f4520SKenneth D. Merry 		goto bailout_error;
2487130f4520SKenneth D. Merry 	}
2488130f4520SKenneth D. Merry 
2489130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2490130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
249134144c2cSAlexander Motin 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2492130f4520SKenneth D. Merry 		if (retval == EINTR)
2493130f4520SKenneth D. Merry 			break;
2494130f4520SKenneth D. Merry 	}
2495130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
249634144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2497130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2498130f4520SKenneth D. Merry 		free(be_lun, M_CTLBLK);
249934144c2cSAlexander Motin 	} else {
250034144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
250134144c2cSAlexander Motin 		return (EINTR);
250234144c2cSAlexander Motin 	}
2503130f4520SKenneth D. Merry 
2504130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2505130f4520SKenneth D. Merry 	return (0);
2506130f4520SKenneth D. Merry 
2507130f4520SKenneth D. Merry bailout_error:
2508130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2509130f4520SKenneth D. Merry 	return (0);
2510130f4520SKenneth D. Merry }
2511130f4520SKenneth D. Merry 
251281177295SEdward Tomasz Napierala static int
251381177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
251481177295SEdward Tomasz Napierala {
251581177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
251681177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
2517a3977beaSAlexander Motin 	struct ctl_be_lun *cbe_lun;
25188951f055SMarcelo Araujo 	const char *value;
251971d8e97eSAlexander Motin 	uint64_t oldsize;
25207ac58230SAlexander Motin 	int error, wasprim;
252181177295SEdward Tomasz Napierala 
252281177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
252381177295SEdward Tomasz Napierala 
252434144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
252581177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
252634144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
25270bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
252881177295SEdward Tomasz Napierala 			break;
252981177295SEdward Tomasz Napierala 	}
253081177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
253181177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
253281177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
253319720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
253419720f41SAlexander Motin 			 params->lun_id);
253581177295SEdward Tomasz Napierala 		goto bailout_error;
253681177295SEdward Tomasz Napierala 	}
2537a3977beaSAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
253881177295SEdward Tomasz Napierala 
2539a3977beaSAlexander Motin 	if (params->lun_size_bytes != 0)
254019720f41SAlexander Motin 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
25418951f055SMarcelo Araujo 
2542efeedddcSAlexander Motin 	if (req->args_nvl != NULL) {
25438951f055SMarcelo Araujo 		nvlist_destroy(cbe_lun->options);
25448951f055SMarcelo Araujo 		cbe_lun->options = nvlist_clone(req->args_nvl);
2545efeedddcSAlexander Motin 	}
254681177295SEdward Tomasz Napierala 
25477ac58230SAlexander Motin 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
25488951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
25497ac58230SAlexander Motin 	if (value != NULL) {
25507ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
25517ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25527ac58230SAlexander Motin 		else
25537ac58230SAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25547ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
25557ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25567ac58230SAlexander Motin 	else
25577ac58230SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25587ac58230SAlexander Motin 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
25597ac58230SAlexander Motin 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
25607ac58230SAlexander Motin 			ctl_lun_primary(cbe_lun);
25617ac58230SAlexander Motin 		else
25627ac58230SAlexander Motin 			ctl_lun_secondary(cbe_lun);
25637ac58230SAlexander Motin 	}
25647ac58230SAlexander Motin 
25650bcd4ab6SAlexander Motin 	oldsize = be_lun->size_blocks;
25667ac58230SAlexander Motin 	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
25677ac58230SAlexander Motin 	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
256819720f41SAlexander Motin 		if (be_lun->vn == NULL)
2569648dfc1aSAlexander Motin 			error = ctl_be_block_open(be_lun, req);
25707ad2a82dSMateusz Guzik 		else if (vn_isdisk_error(be_lun->vn, &error))
25714ce7a086SAlexander Motin 			error = ctl_be_block_open_dev(be_lun, req);
25723d5cb709SAlexander Motin 		else if (be_lun->vn->v_type == VREG) {
25733d5cb709SAlexander Motin 			vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
25744ce7a086SAlexander Motin 			error = ctl_be_block_open_file(be_lun, req);
2575b249ce48SMateusz Guzik 			VOP_UNLOCK(be_lun->vn);
25763d5cb709SAlexander Motin 		} else
2577b9b4269cSAlexander Motin 			error = EINVAL;
2578648dfc1aSAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
25790bcd4ab6SAlexander Motin 		    be_lun->vn != NULL) {
2580648dfc1aSAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2581648dfc1aSAlexander Motin 			ctl_lun_has_media(cbe_lun);
2582648dfc1aSAlexander Motin 		} else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2583648dfc1aSAlexander Motin 		    be_lun->vn == NULL) {
2584648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2585648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
258671d8e97eSAlexander Motin 		}
2587648dfc1aSAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
25887ac58230SAlexander Motin 	} else {
25897ac58230SAlexander Motin 		if (be_lun->vn != NULL) {
2590648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2591648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
2592ee4ad294SAlexander Motin 			taskqueue_drain_all(be_lun->io_taskqueue);
25937ac58230SAlexander Motin 			error = ctl_be_block_close(be_lun);
25947ac58230SAlexander Motin 		} else
25957ac58230SAlexander Motin 			error = 0;
25967ac58230SAlexander Motin 	}
25977ac58230SAlexander Motin 	if (be_lun->size_blocks != oldsize)
25987ac58230SAlexander Motin 		ctl_lun_capacity_changed(cbe_lun);
259981177295SEdward Tomasz Napierala 
260081177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
260181177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
260281177295SEdward Tomasz Napierala 
260334144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
260419720f41SAlexander Motin 	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
260581177295SEdward Tomasz Napierala 	return (0);
260681177295SEdward Tomasz Napierala 
260781177295SEdward Tomasz Napierala bailout_error:
260834144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
260981177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
261081177295SEdward Tomasz Napierala 	return (0);
261181177295SEdward Tomasz Napierala }
261281177295SEdward Tomasz Napierala 
2613130f4520SKenneth D. Merry static void
2614767300e8SAlexander Motin ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2615130f4520SKenneth D. Merry {
2616767300e8SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
261734144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
261834144c2cSAlexander Motin 
261934144c2cSAlexander Motin 	taskqueue_drain_all(be_lun->io_taskqueue);
262034144c2cSAlexander Motin 	taskqueue_free(be_lun->io_taskqueue);
262134144c2cSAlexander Motin 	if (be_lun->disk_stats != NULL)
262234144c2cSAlexander Motin 		devstat_remove_entry(be_lun->disk_stats);
262334144c2cSAlexander Motin 	nvlist_destroy(be_lun->cbe_lun.options);
262434144c2cSAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
262534144c2cSAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
262634144c2cSAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2627130f4520SKenneth D. Merry 
2628130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
262934144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
263034144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
263134144c2cSAlexander Motin 		wakeup(be_lun);
263234144c2cSAlexander Motin 	else
263334144c2cSAlexander Motin 		free(be_lun, M_CTLBLK);
2634130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2635130f4520SKenneth D. Merry }
2636130f4520SKenneth D. Merry 
2637130f4520SKenneth D. Merry static int
2638130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2639130f4520SKenneth D. Merry {
2640130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
26410bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2642130f4520SKenneth D. Merry 	int retval;
2643130f4520SKenneth D. Merry 
2644130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2645130f4520SKenneth D. Merry 
26469cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
2647767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun;
2648130f4520SKenneth D. Merry 
264967cc546dSAlexander Motin 	retval = 0;
2650130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2651130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2652130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2653ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2654ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2655ee7f31c0SAlexander Motin 	case UNMAP:
2656130f4520SKenneth D. Merry 		/*
2657130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2658130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2659130f4520SKenneth D. Merry 		 *
2660130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2661130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2662130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2663130f4520SKenneth D. Merry 		 */
266475c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
2665130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2666130f4520SKenneth D. Merry 				   links);
266775c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
2668130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2669130f4520SKenneth D. Merry 		break;
2670130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2671130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2672648dfc1aSAlexander Motin 		struct ctl_lun_req req;
2673130f4520SKenneth D. Merry 
2674130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
267566b69676SAlexander Motin 		if ((cdb->how & SSS_PC_MASK) != 0) {
267666b69676SAlexander Motin 			ctl_set_success(&io->scsiio);
267766b69676SAlexander Motin 			ctl_config_write_done(io);
267866b69676SAlexander Motin 			break;
267966b69676SAlexander Motin 		}
2680648dfc1aSAlexander Motin 		if (cdb->how & SSS_START) {
2681648dfc1aSAlexander Motin 			if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2682648dfc1aSAlexander Motin 				retval = ctl_be_block_open(be_lun, &req);
2683648dfc1aSAlexander Motin 				cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2684648dfc1aSAlexander Motin 				if (retval == 0) {
2685648dfc1aSAlexander Motin 					cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2686648dfc1aSAlexander Motin 					ctl_lun_has_media(cbe_lun);
2687130f4520SKenneth D. Merry 				} else {
2688648dfc1aSAlexander Motin 					cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2689648dfc1aSAlexander Motin 					ctl_lun_no_media(cbe_lun);
2690130f4520SKenneth D. Merry 				}
2691648dfc1aSAlexander Motin 			}
2692648dfc1aSAlexander Motin 			ctl_start_lun(cbe_lun);
2693648dfc1aSAlexander Motin 		} else {
2694648dfc1aSAlexander Motin 			ctl_stop_lun(cbe_lun);
2695648dfc1aSAlexander Motin 			if (cdb->how & SSS_LOEJ) {
2696648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2697648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2698648dfc1aSAlexander Motin 				ctl_lun_ejected(cbe_lun);
2699648dfc1aSAlexander Motin 				if (be_lun->vn != NULL)
2700648dfc1aSAlexander Motin 					ctl_be_block_close(be_lun);
2701648dfc1aSAlexander Motin 			}
2702648dfc1aSAlexander Motin 		}
2703648dfc1aSAlexander Motin 
2704648dfc1aSAlexander Motin 		ctl_set_success(&io->scsiio);
2705130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2706130f4520SKenneth D. Merry 		break;
2707130f4520SKenneth D. Merry 	}
270891be33dcSAlexander Motin 	case PREVENT_ALLOW:
270991be33dcSAlexander Motin 		ctl_set_success(&io->scsiio);
271091be33dcSAlexander Motin 		ctl_config_write_done(io);
271191be33dcSAlexander Motin 		break;
2712130f4520SKenneth D. Merry 	default:
2713130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2714130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2715130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2716130f4520SKenneth D. Merry 		break;
2717130f4520SKenneth D. Merry 	}
2718130f4520SKenneth D. Merry 
2719130f4520SKenneth D. Merry 	return (retval);
2720130f4520SKenneth D. Merry }
2721130f4520SKenneth D. Merry 
2722130f4520SKenneth D. Merry static int
2723130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2724130f4520SKenneth D. Merry {
2725ef8daf3fSAlexander Motin 	struct ctl_be_block_lun *be_lun;
2726ef8daf3fSAlexander Motin 	int retval = 0;
2727ef8daf3fSAlexander Motin 
2728ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
2729ef8daf3fSAlexander Motin 
2730767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2731ef8daf3fSAlexander Motin 
2732ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
2733ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:
2734ef8daf3fSAlexander Motin 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2735ef8daf3fSAlexander Motin 			mtx_lock(&be_lun->queue_lock);
2736ef8daf3fSAlexander Motin 			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2737ef8daf3fSAlexander Motin 			    &io->io_hdr, links);
2738ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
2739ef8daf3fSAlexander Motin 			taskqueue_enqueue(be_lun->io_taskqueue,
2740ef8daf3fSAlexander Motin 			    &be_lun->io_task);
2741ef8daf3fSAlexander Motin 			retval = CTL_RETVAL_QUEUED;
2742ef8daf3fSAlexander Motin 			break;
2743ef8daf3fSAlexander Motin 		}
2744ef8daf3fSAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
2745ef8daf3fSAlexander Motin 				      /*sks_valid*/ 1,
2746ef8daf3fSAlexander Motin 				      /*command*/ 1,
2747ef8daf3fSAlexander Motin 				      /*field*/ 1,
2748ef8daf3fSAlexander Motin 				      /*bit_valid*/ 1,
2749ef8daf3fSAlexander Motin 				      /*bit*/ 4);
2750ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2751ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2752ef8daf3fSAlexander Motin 		break;
2753ef8daf3fSAlexander Motin 	default:
2754ef8daf3fSAlexander Motin 		ctl_set_invalid_opcode(&io->scsiio);
2755ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2756ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2757ef8daf3fSAlexander Motin 		break;
2758ef8daf3fSAlexander Motin 	}
2759ef8daf3fSAlexander Motin 
2760ef8daf3fSAlexander Motin 	return (retval);
2761130f4520SKenneth D. Merry }
2762130f4520SKenneth D. Merry 
2763130f4520SKenneth D. Merry static int
2764767300e8SAlexander Motin ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2765130f4520SKenneth D. Merry {
2766767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2767130f4520SKenneth D. Merry 	int retval;
2768130f4520SKenneth D. Merry 
27692cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "\t<num_threads>");
2770130f4520SKenneth D. Merry 	if (retval != 0)
2771130f4520SKenneth D. Merry 		goto bailout;
2772130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2773130f4520SKenneth D. Merry 	if (retval != 0)
2774130f4520SKenneth D. Merry 		goto bailout;
27752cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "</num_threads>\n");
2776130f4520SKenneth D. Merry 
2777130f4520SKenneth D. Merry bailout:
2778130f4520SKenneth D. Merry 	return (retval);
2779130f4520SKenneth D. Merry }
2780130f4520SKenneth D. Merry 
2781c3e7ba3eSAlexander Motin static uint64_t
2782767300e8SAlexander Motin ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2783c3e7ba3eSAlexander Motin {
2784767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2785c3e7ba3eSAlexander Motin 
2786c3e7ba3eSAlexander Motin 	if (lun->getattr == NULL)
2787c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
2788c3e7ba3eSAlexander Motin 	return (lun->getattr(lun, attrname));
2789c3e7ba3eSAlexander Motin }
2790c3e7ba3eSAlexander Motin 
27910c629e28SAlexander Motin static int
2792130f4520SKenneth D. Merry ctl_be_block_init(void)
2793130f4520SKenneth D. Merry {
27940c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
2795130f4520SKenneth D. Merry 
279634144c2cSAlexander Motin 	sx_init(&softc->modify_lock, "ctlblock modify");
279775c7a1d3SAlexander Motin 	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
27980c629e28SAlexander Motin 	softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2799a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2800cd853791SKonstantin Belousov 	softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG,
28010d7fed74SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2802cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2803cd853791SKonstantin Belousov 		softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG,
28048054320eSAlexander Motin 		    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
280534144c2cSAlexander Motin 	SLIST_INIT(&softc->lun_list);
28060c629e28SAlexander Motin 	return (0);
28070c629e28SAlexander Motin }
2808130f4520SKenneth D. Merry 
28090c629e28SAlexander Motin static int
28100c629e28SAlexander Motin ctl_be_block_shutdown(void)
28110c629e28SAlexander Motin {
28120c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
281334144c2cSAlexander Motin 	struct ctl_be_block_lun *lun;
28140c629e28SAlexander Motin 
28150c629e28SAlexander Motin 	mtx_lock(&softc->lock);
281634144c2cSAlexander Motin 	while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
281734144c2cSAlexander Motin 		SLIST_REMOVE_HEAD(&softc->lun_list, links);
281834144c2cSAlexander Motin 		softc->num_luns--;
28190c629e28SAlexander Motin 		/*
282034144c2cSAlexander Motin 		 * Drop our lock here.  Since ctl_remove_lun() can call
28210c629e28SAlexander Motin 		 * back into us, this could potentially lead to a recursive
28220c629e28SAlexander Motin 		 * lock of the same mutex, which would cause a hang.
28230c629e28SAlexander Motin 		 */
28240c629e28SAlexander Motin 		mtx_unlock(&softc->lock);
282534144c2cSAlexander Motin 		ctl_remove_lun(&lun->cbe_lun);
28260c629e28SAlexander Motin 		mtx_lock(&softc->lock);
28270c629e28SAlexander Motin 	}
28280c629e28SAlexander Motin 	mtx_unlock(&softc->lock);
2829cd853791SKonstantin Belousov 	uma_zdestroy(softc->bufmin_zone);
2830cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2831cd853791SKonstantin Belousov 		uma_zdestroy(softc->bufmax_zone);
28320c629e28SAlexander Motin 	uma_zdestroy(softc->beio_zone);
28330c629e28SAlexander Motin 	mtx_destroy(&softc->lock);
283434144c2cSAlexander Motin 	sx_destroy(&softc->modify_lock);
28350c629e28SAlexander Motin 	return (0);
2836130f4520SKenneth D. Merry }
2837