xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision bb92cd7bcd16f3f36cdbda18d8193619892715fb)
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
649050613SKa Ho Ng  * Copyright (c) 2012,2021 The FreeBSD Foundation
7b06771aaSAlexander Motin  * Copyright (c) 2014-2021 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  *
1349050613SKa Ho Ng  * Portions of this software were developed by Ka Ho Ng <khng@FreeBSD.org>
1449050613SKa Ho Ng  * under sponsorship from the FreeBSD Foundation.
1549050613SKa Ho Ng  *
16130f4520SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
17130f4520SKenneth D. Merry  * modification, are permitted provided that the following conditions
18130f4520SKenneth D. Merry  * are met:
19130f4520SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
20130f4520SKenneth D. Merry  *    notice, this list of conditions, and the following disclaimer,
21130f4520SKenneth D. Merry  *    without modification.
22130f4520SKenneth D. Merry  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
23130f4520SKenneth D. Merry  *    substantially similar to the "NO WARRANTY" disclaimer below
24130f4520SKenneth D. Merry  *    ("Disclaimer") and any redistribution must be conditioned upon
25130f4520SKenneth D. Merry  *    including a substantially similar Disclaimer requirement for further
26130f4520SKenneth D. Merry  *    binary redistribution.
27130f4520SKenneth D. Merry  *
28130f4520SKenneth D. Merry  * NO WARRANTY
29130f4520SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30130f4520SKenneth D. Merry  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31130f4520SKenneth D. Merry  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
32130f4520SKenneth D. Merry  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33130f4520SKenneth D. Merry  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34130f4520SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35130f4520SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36130f4520SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37130f4520SKenneth D. Merry  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
38130f4520SKenneth D. Merry  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39130f4520SKenneth D. Merry  * POSSIBILITY OF SUCH DAMAGES.
40130f4520SKenneth D. Merry  *
41130f4520SKenneth D. Merry  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
42130f4520SKenneth D. Merry  */
43130f4520SKenneth D. Merry /*
44130f4520SKenneth D. Merry  * CAM Target Layer driver backend for block devices.
45130f4520SKenneth D. Merry  *
46130f4520SKenneth D. Merry  * Author: Ken Merry <ken@FreeBSD.org>
47130f4520SKenneth D. Merry  */
48130f4520SKenneth D. Merry #include <sys/cdefs.h>
49130f4520SKenneth D. Merry __FBSDID("$FreeBSD$");
50130f4520SKenneth D. Merry 
51130f4520SKenneth D. Merry #include <sys/param.h>
52130f4520SKenneth D. Merry #include <sys/systm.h>
53130f4520SKenneth D. Merry #include <sys/kernel.h>
54130f4520SKenneth D. Merry #include <sys/types.h>
55130f4520SKenneth D. Merry #include <sys/kthread.h>
56130f4520SKenneth D. Merry #include <sys/bio.h>
57130f4520SKenneth D. Merry #include <sys/fcntl.h>
58ee7f31c0SAlexander Motin #include <sys/limits.h>
59130f4520SKenneth D. Merry #include <sys/lock.h>
60130f4520SKenneth D. Merry #include <sys/mutex.h>
61130f4520SKenneth D. Merry #include <sys/condvar.h>
62130f4520SKenneth D. Merry #include <sys/malloc.h>
63130f4520SKenneth D. Merry #include <sys/conf.h>
64130f4520SKenneth D. Merry #include <sys/ioccom.h>
65130f4520SKenneth D. Merry #include <sys/queue.h>
66130f4520SKenneth D. Merry #include <sys/sbuf.h>
67130f4520SKenneth D. Merry #include <sys/endian.h>
68130f4520SKenneth D. Merry #include <sys/uio.h>
69130f4520SKenneth D. Merry #include <sys/buf.h>
70130f4520SKenneth D. Merry #include <sys/taskqueue.h>
71130f4520SKenneth D. Merry #include <sys/vnode.h>
72130f4520SKenneth D. Merry #include <sys/namei.h>
73130f4520SKenneth D. Merry #include <sys/mount.h>
74130f4520SKenneth D. Merry #include <sys/disk.h>
75130f4520SKenneth D. Merry #include <sys/fcntl.h>
76130f4520SKenneth D. Merry #include <sys/filedesc.h>
77ef8daf3fSAlexander Motin #include <sys/filio.h>
78130f4520SKenneth D. Merry #include <sys/proc.h>
79130f4520SKenneth D. Merry #include <sys/pcpu.h>
80130f4520SKenneth D. Merry #include <sys/module.h>
81130f4520SKenneth D. Merry #include <sys/sdt.h>
82130f4520SKenneth D. Merry #include <sys/devicestat.h>
83130f4520SKenneth D. Merry #include <sys/sysctl.h>
848951f055SMarcelo Araujo #include <sys/nv.h>
858951f055SMarcelo Araujo #include <sys/dnv.h>
8634144c2cSAlexander Motin #include <sys/sx.h>
8749050613SKa Ho Ng #include <sys/unistd.h>
88130f4520SKenneth D. Merry 
89130f4520SKenneth D. Merry #include <geom/geom.h>
90130f4520SKenneth D. Merry 
91130f4520SKenneth D. Merry #include <cam/cam.h>
92130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h>
93130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h>
94130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h>
95130f4520SKenneth D. Merry #include <cam/ctl/ctl.h>
96130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h>
97130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h>
987ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h>
99130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h>
1007ac58230SAlexander Motin #include <cam/ctl/ctl_private.h>
101130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h>
102130f4520SKenneth D. Merry 
103130f4520SKenneth D. Merry /*
104b06771aaSAlexander Motin  * The idea here is to allocate enough S/G space to handle at least 1MB I/Os.
105b06771aaSAlexander Motin  * On systems with small maxphys it can be 8 128KB segments.  On large systems
106b06771aaSAlexander Motin  * it can be up to 8 1MB segments.  I/Os larger than that we'll split.
107130f4520SKenneth D. Merry  */
108b06771aaSAlexander Motin #define	CTLBLK_MAX_SEGS		8
109b06771aaSAlexander Motin #define	CTLBLK_HALF_SEGS	(CTLBLK_MAX_SEGS / 2)
110cd853791SKonstantin Belousov #define	CTLBLK_MIN_SEG		(128 * 1024)
111b06771aaSAlexander Motin #define	CTLBLK_MAX_SEG		MIN(1024 * 1024, MAX(CTLBLK_MIN_SEG, maxphys))
112b06771aaSAlexander Motin #define	CTLBLK_MAX_IO_SIZE	(CTLBLK_MAX_SEG * CTLBLK_MAX_SEGS)
113130f4520SKenneth D. Merry 
114130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG
115130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \
116130f4520SKenneth D. Merry     printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
117130f4520SKenneth D. Merry #else
118130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0)
119130f4520SKenneth D. Merry #endif
120130f4520SKenneth D. Merry 
121e86a4142SAlexander Motin #define PRIV(io)	\
122e86a4142SAlexander Motin     ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
12311b569f7SAlexander Motin #define ARGS(io)	\
12411b569f7SAlexander Motin     ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
125e86a4142SAlexander Motin 
126130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb);
127130f4520SKenneth D. Merry 
128130f4520SKenneth D. Merry typedef enum {
129130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
130130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
131130f4520SKenneth D. Merry } ctl_be_block_lun_flags;
132130f4520SKenneth D. Merry 
133130f4520SKenneth D. Merry typedef enum {
134130f4520SKenneth D. Merry 	CTL_BE_BLOCK_NONE,
135130f4520SKenneth D. Merry 	CTL_BE_BLOCK_DEV,
136130f4520SKenneth D. Merry 	CTL_BE_BLOCK_FILE
137130f4520SKenneth D. Merry } ctl_be_block_type;
138130f4520SKenneth D. Merry 
139130f4520SKenneth D. Merry struct ctl_be_block_filedata {
140130f4520SKenneth D. Merry 	struct ucred *cred;
141130f4520SKenneth D. Merry };
142130f4520SKenneth D. Merry 
143130f4520SKenneth D. Merry union ctl_be_block_bedata {
144130f4520SKenneth D. Merry 	struct ctl_be_block_filedata file;
145130f4520SKenneth D. Merry };
146130f4520SKenneth D. Merry 
147130f4520SKenneth D. Merry struct ctl_be_block_io;
148130f4520SKenneth D. Merry struct ctl_be_block_lun;
149130f4520SKenneth D. Merry 
150130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
151130f4520SKenneth D. Merry 			       struct ctl_be_block_io *beio);
152c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
153c3e7ba3eSAlexander Motin 				  const char *attrname);
154130f4520SKenneth D. Merry 
155130f4520SKenneth D. Merry /*
156130f4520SKenneth D. Merry  * Backend LUN structure.  There is a 1:1 mapping between a block device
157130f4520SKenneth D. Merry  * and a backend block LUN, and between a backend block LUN and a CTL LUN.
158130f4520SKenneth D. Merry  */
159130f4520SKenneth D. Merry struct ctl_be_block_lun {
160767300e8SAlexander Motin 	struct ctl_be_lun cbe_lun;		/* Must be first element. */
16119720f41SAlexander Motin 	struct ctl_lun_create_params params;
162130f4520SKenneth D. Merry 	char *dev_path;
163130f4520SKenneth D. Merry 	ctl_be_block_type dev_type;
164130f4520SKenneth D. Merry 	struct vnode *vn;
165130f4520SKenneth D. Merry 	union ctl_be_block_bedata backend;
166130f4520SKenneth D. Merry 	cbb_dispatch_t dispatch;
167130f4520SKenneth D. Merry 	cbb_dispatch_t lun_flush;
168ee7f31c0SAlexander Motin 	cbb_dispatch_t unmap;
169ef8daf3fSAlexander Motin 	cbb_dispatch_t get_lba_status;
170c3e7ba3eSAlexander Motin 	cbb_getattr_t getattr;
171130f4520SKenneth D. Merry 	uint64_t size_blocks;
172130f4520SKenneth D. Merry 	uint64_t size_bytes;
173130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
174130f4520SKenneth D. Merry 	struct devstat *disk_stats;
175130f4520SKenneth D. Merry 	ctl_be_block_lun_flags flags;
17634144c2cSAlexander Motin 	SLIST_ENTRY(ctl_be_block_lun) links;
177130f4520SKenneth D. Merry 	struct taskqueue *io_taskqueue;
178130f4520SKenneth D. Merry 	struct task io_task;
179130f4520SKenneth D. Merry 	int num_threads;
180130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) input_queue;
181ef8daf3fSAlexander Motin 	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
182130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
183130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
18475c7a1d3SAlexander Motin 	struct mtx_padalign io_lock;
18575c7a1d3SAlexander Motin 	struct mtx_padalign queue_lock;
186130f4520SKenneth D. Merry };
187130f4520SKenneth D. Merry 
188130f4520SKenneth D. Merry /*
189130f4520SKenneth D. Merry  * Overall softc structure for the block backend module.
190130f4520SKenneth D. Merry  */
191130f4520SKenneth D. Merry struct ctl_be_block_softc {
19234144c2cSAlexander Motin 	struct sx			 modify_lock;
193130f4520SKenneth D. Merry 	struct mtx			 lock;
194130f4520SKenneth D. Merry 	int				 num_luns;
19534144c2cSAlexander Motin 	SLIST_HEAD(, ctl_be_block_lun)	 lun_list;
1960d7fed74SAlexander Motin 	uma_zone_t			 beio_zone;
197cd853791SKonstantin Belousov 	uma_zone_t			 bufmin_zone;
198cd853791SKonstantin Belousov 	uma_zone_t			 bufmax_zone;
199130f4520SKenneth D. Merry };
200130f4520SKenneth D. Merry 
201130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc;
202130f4520SKenneth D. Merry 
203130f4520SKenneth D. Merry /*
204130f4520SKenneth D. Merry  * Per-I/O information.
205130f4520SKenneth D. Merry  */
206130f4520SKenneth D. Merry struct ctl_be_block_io {
207130f4520SKenneth D. Merry 	union ctl_io			*io;
208130f4520SKenneth D. Merry 	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
209130f4520SKenneth D. Merry 	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
2109a4510acSAlexander Motin 	int				refcnt;
211130f4520SKenneth D. Merry 	int				bio_cmd;
2120d7fed74SAlexander Motin 	int				two_sglists;
213130f4520SKenneth D. Merry 	int				num_segs;
214130f4520SKenneth D. Merry 	int				num_bios_sent;
215130f4520SKenneth D. Merry 	int				num_bios_done;
216130f4520SKenneth D. Merry 	int				send_complete;
2171f0694a6SAlexander Motin 	int				first_error;
2181f0694a6SAlexander Motin 	uint64_t			first_error_offset;
219130f4520SKenneth D. Merry 	struct bintime			ds_t0;
220130f4520SKenneth D. Merry 	devstat_tag_type		ds_tag_type;
221130f4520SKenneth D. Merry 	devstat_trans_flags		ds_trans_type;
222130f4520SKenneth D. Merry 	uint64_t			io_len;
223130f4520SKenneth D. Merry 	uint64_t			io_offset;
2247d0d4342SAlexander Motin 	int				io_arg;
225130f4520SKenneth D. Merry 	struct ctl_be_block_softc	*softc;
226130f4520SKenneth D. Merry 	struct ctl_be_block_lun		*lun;
227ee7f31c0SAlexander Motin 	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
228130f4520SKenneth D. Merry };
229130f4520SKenneth D. Merry 
2307ac58230SAlexander Motin extern struct ctl_softc *control_softc;
2317ac58230SAlexander Motin 
2327d4c4443SAlexander Motin static int cbb_num_threads = 32;
2337029da5cSPawel Biernacki SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
234130f4520SKenneth D. Merry 	    "CAM Target Layer Block Backend");
235af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
236130f4520SKenneth D. Merry            &cbb_num_threads, 0, "Number of threads per backing file");
237130f4520SKenneth D. Merry 
238130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
239130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio);
240130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio);
2412c7dc6baSAlexander Motin static int ctl_be_block_move_done(union ctl_io *io, bool samethr);
242130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio);
243130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
244130f4520SKenneth D. Merry 				    struct ctl_be_block_io *beio);
245130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
246130f4520SKenneth D. Merry 				       struct ctl_be_block_io *beio);
247ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
248ef8daf3fSAlexander Motin 				  struct ctl_be_block_io *beio);
24953c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
25053c146deSAlexander Motin 					 const char *attrname);
25149050613SKa Ho Ng static void ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
25249050613SKa Ho Ng 				    struct ctl_be_block_io *beio);
253130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
254130f4520SKenneth D. Merry 				   struct ctl_be_block_io *beio);
255ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
256ee7f31c0SAlexander Motin 				   struct ctl_be_block_io *beio);
257130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
258130f4520SKenneth D. Merry 				      struct ctl_be_block_io *beio);
259c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
260c3e7ba3eSAlexander Motin 					 const char *attrname);
261ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
262ef8daf3fSAlexander Motin 				    union ctl_io *io);
263130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
264130f4520SKenneth D. Merry 				    union ctl_io *io);
265130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
266130f4520SKenneth D. Merry 				  union ctl_io *io);
267130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending);
268130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io);
269130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
270130f4520SKenneth D. Merry 				   int flag, struct thread *td);
271130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
272130f4520SKenneth D. Merry 				  struct ctl_lun_req *req);
273130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
274130f4520SKenneth D. Merry 				 struct ctl_lun_req *req);
275130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
276648dfc1aSAlexander Motin static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
277130f4520SKenneth D. Merry 			     struct ctl_lun_req *req);
278130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc,
279130f4520SKenneth D. Merry 			       struct ctl_lun_req *req);
280130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
281130f4520SKenneth D. Merry 			   struct ctl_lun_req *req);
28281177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
28381177295SEdward Tomasz Napierala 			   struct ctl_lun_req *req);
284767300e8SAlexander Motin static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun);
285130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io);
286130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io);
287767300e8SAlexander Motin static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb);
288767300e8SAlexander Motin static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname);
2890c629e28SAlexander Motin static int ctl_be_block_init(void);
2900c629e28SAlexander Motin static int ctl_be_block_shutdown(void);
291130f4520SKenneth D. Merry 
292130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver =
293130f4520SKenneth D. Merry {
2942a2443d8SKenneth D. Merry 	.name = "block",
2952a2443d8SKenneth D. Merry 	.flags = CTL_BE_FLAG_HAS_CONFIG,
2962a2443d8SKenneth D. Merry 	.init = ctl_be_block_init,
2970c629e28SAlexander Motin 	.shutdown = ctl_be_block_shutdown,
2982a2443d8SKenneth D. Merry 	.data_submit = ctl_be_block_submit,
2992a2443d8SKenneth D. Merry 	.config_read = ctl_be_block_config_read,
3002a2443d8SKenneth D. Merry 	.config_write = ctl_be_block_config_write,
3012a2443d8SKenneth D. Merry 	.ioctl = ctl_be_block_ioctl,
302c3e7ba3eSAlexander Motin 	.lun_info = ctl_be_block_lun_info,
303c3e7ba3eSAlexander Motin 	.lun_attr = ctl_be_block_lun_attr
304130f4520SKenneth D. Merry };
305130f4520SKenneth D. Merry 
30634144c2cSAlexander Motin MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend");
307130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
308130f4520SKenneth D. Merry 
3098054320eSAlexander Motin static void
3108054320eSAlexander Motin ctl_alloc_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg,
3118054320eSAlexander Motin     size_t len)
3128054320eSAlexander Motin {
3138054320eSAlexander Motin 
314cd853791SKonstantin Belousov 	if (len <= CTLBLK_MIN_SEG) {
315cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK);
316cd853791SKonstantin Belousov 	} else {
317cd853791SKonstantin Belousov 		KASSERT(len <= CTLBLK_MAX_SEG,
318cd853791SKonstantin Belousov 		    ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG));
319cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK);
320cd853791SKonstantin Belousov 	}
3218054320eSAlexander Motin 	sg->len = len;
3228054320eSAlexander Motin }
3238054320eSAlexander Motin 
3248054320eSAlexander Motin static void
3258054320eSAlexander Motin ctl_free_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg)
3268054320eSAlexander Motin {
3278054320eSAlexander Motin 
328cd853791SKonstantin Belousov 	if (sg->len <= CTLBLK_MIN_SEG) {
329cd853791SKonstantin Belousov 		uma_zfree(softc->bufmin_zone, sg->addr);
330cd853791SKonstantin Belousov 	} else {
331cd853791SKonstantin Belousov 		KASSERT(sg->len <= CTLBLK_MAX_SEG,
332cd853791SKonstantin Belousov 		    ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG));
333cd853791SKonstantin Belousov 		uma_zfree(softc->bufmax_zone, sg->addr);
334cd853791SKonstantin Belousov 	}
3358054320eSAlexander Motin }
3368054320eSAlexander Motin 
337130f4520SKenneth D. Merry static struct ctl_be_block_io *
338130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc)
339130f4520SKenneth D. Merry {
340130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
341130f4520SKenneth D. Merry 
3420c629e28SAlexander Motin 	beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
343130f4520SKenneth D. Merry 	beio->softc = softc;
3449a4510acSAlexander Motin 	beio->refcnt = 1;
345130f4520SKenneth D. Merry 	return (beio);
346130f4520SKenneth D. Merry }
347130f4520SKenneth D. Merry 
348130f4520SKenneth D. Merry static void
3499a4510acSAlexander Motin ctl_real_free_beio(struct ctl_be_block_io *beio)
350130f4520SKenneth D. Merry {
3510d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = beio->softc;
352130f4520SKenneth D. Merry 	int i;
353130f4520SKenneth D. Merry 
354130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
3558054320eSAlexander Motin 		ctl_free_seg(softc, &beio->sg_segs[i]);
35611b569f7SAlexander Motin 
35711b569f7SAlexander Motin 		/* For compare we had two equal S/G lists. */
3580d7fed74SAlexander Motin 		if (beio->two_sglists) {
3598054320eSAlexander Motin 			ctl_free_seg(softc,
3608054320eSAlexander Motin 			    &beio->sg_segs[i + CTLBLK_HALF_SEGS]);
36111b569f7SAlexander Motin 		}
362130f4520SKenneth D. Merry 	}
363130f4520SKenneth D. Merry 
3640d7fed74SAlexander Motin 	uma_zfree(softc->beio_zone, beio);
365130f4520SKenneth D. Merry }
366130f4520SKenneth D. Merry 
367130f4520SKenneth D. Merry static void
3689a4510acSAlexander Motin ctl_refcnt_beio(void *arg, int diff)
3699a4510acSAlexander Motin {
3709a4510acSAlexander Motin 	struct ctl_be_block_io *beio = arg;
3719a4510acSAlexander Motin 
3729a4510acSAlexander Motin 	if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0)
3739a4510acSAlexander Motin 		ctl_real_free_beio(beio);
3749a4510acSAlexander Motin }
3759a4510acSAlexander Motin 
3769a4510acSAlexander Motin static void
3779a4510acSAlexander Motin ctl_free_beio(struct ctl_be_block_io *beio)
3789a4510acSAlexander Motin {
3799a4510acSAlexander Motin 
3809a4510acSAlexander Motin 	ctl_refcnt_beio(beio, -1);
3819a4510acSAlexander Motin }
3829a4510acSAlexander Motin 
3839a4510acSAlexander Motin static void
384130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio)
385130f4520SKenneth D. Merry {
38675c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
387130f4520SKenneth D. Merry 
388ee7f31c0SAlexander Motin 	if (beio->beio_cont != NULL) {
389ee7f31c0SAlexander Motin 		beio->beio_cont(beio);
390ee7f31c0SAlexander Motin 	} else {
391130f4520SKenneth D. Merry 		ctl_free_beio(beio);
39211b569f7SAlexander Motin 		ctl_data_submit_done(io);
393130f4520SKenneth D. Merry 	}
394ee7f31c0SAlexander Motin }
395130f4520SKenneth D. Merry 
396d6043e46SAlexander Motin static size_t
397d6043e46SAlexander Motin cmp(uint8_t *a, uint8_t *b, size_t size)
398d6043e46SAlexander Motin {
399d6043e46SAlexander Motin 	size_t i;
400d6043e46SAlexander Motin 
401d6043e46SAlexander Motin 	for (i = 0; i < size; i++) {
402d6043e46SAlexander Motin 		if (a[i] != b[i])
403d6043e46SAlexander Motin 			break;
404d6043e46SAlexander Motin 	}
405d6043e46SAlexander Motin 	return (i);
406d6043e46SAlexander Motin }
407d6043e46SAlexander Motin 
408d6043e46SAlexander Motin static void
409d6043e46SAlexander Motin ctl_be_block_compare(union ctl_io *io)
410d6043e46SAlexander Motin {
411d6043e46SAlexander Motin 	struct ctl_be_block_io *beio;
412d6043e46SAlexander Motin 	uint64_t off, res;
413d6043e46SAlexander Motin 	int i;
414d6043e46SAlexander Motin 	uint8_t info[8];
415d6043e46SAlexander Motin 
416d6043e46SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
417d6043e46SAlexander Motin 	off = 0;
418d6043e46SAlexander Motin 	for (i = 0; i < beio->num_segs; i++) {
419d6043e46SAlexander Motin 		res = cmp(beio->sg_segs[i].addr,
420d6043e46SAlexander Motin 		    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
421d6043e46SAlexander Motin 		    beio->sg_segs[i].len);
422d6043e46SAlexander Motin 		off += res;
423d6043e46SAlexander Motin 		if (res < beio->sg_segs[i].len)
424d6043e46SAlexander Motin 			break;
425d6043e46SAlexander Motin 	}
426d6043e46SAlexander Motin 	if (i < beio->num_segs) {
427d6043e46SAlexander Motin 		scsi_u64to8b(off, info);
428d6043e46SAlexander Motin 		ctl_set_sense(&io->scsiio, /*current_error*/ 1,
429d6043e46SAlexander Motin 		    /*sense_key*/ SSD_KEY_MISCOMPARE,
430d6043e46SAlexander Motin 		    /*asc*/ 0x1D, /*ascq*/ 0x00,
431d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_INFO,
432d6043e46SAlexander Motin 		    /*size*/ sizeof(info), /*data*/ &info,
433d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_NONE);
434d6043e46SAlexander Motin 	} else
435d6043e46SAlexander Motin 		ctl_set_success(&io->scsiio);
436d6043e46SAlexander Motin }
437d6043e46SAlexander Motin 
438130f4520SKenneth D. Merry static int
4392c7dc6baSAlexander Motin ctl_be_block_move_done(union ctl_io *io, bool samethr)
440130f4520SKenneth D. Merry {
441130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
442130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
44311b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
444130f4520SKenneth D. Merry 
445e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
446130f4520SKenneth D. Merry 
447130f4520SKenneth D. Merry 	DPRINTF("entered\n");
44811b569f7SAlexander Motin 	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
449130f4520SKenneth D. Merry 
450130f4520SKenneth D. Merry 	/*
4512c7dc6baSAlexander Motin 	 * We set status at this point for read and compare commands.
452130f4520SKenneth D. Merry 	 */
4532c7dc6baSAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4542c7dc6baSAlexander Motin 	    (io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) {
455a59e2982SAlexander Motin 		lbalen = ARGS(io);
45611b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_READ) {
457130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
45811b569f7SAlexander Motin 		} else if (lbalen->flags & CTL_LLF_COMPARE) {
45911b569f7SAlexander Motin 			/* We have two data blocks ready for comparison. */
460d6043e46SAlexander Motin 			ctl_be_block_compare(io);
46111b569f7SAlexander Motin 		}
462130f4520SKenneth D. Merry 	}
463130f4520SKenneth D. Merry 
464130f4520SKenneth D. Merry 	/*
465130f4520SKenneth D. Merry 	 * If this is a read, or a write with errors, it is done.
466130f4520SKenneth D. Merry 	 */
467130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_READ)
468130f4520SKenneth D. Merry 	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
469130f4520SKenneth D. Merry 	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
470130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
471130f4520SKenneth D. Merry 		return (0);
472130f4520SKenneth D. Merry 	}
473130f4520SKenneth D. Merry 
474130f4520SKenneth D. Merry 	/*
4752c7dc6baSAlexander Motin 	 * At this point, we have a write and the DMA completed successfully.
4762c7dc6baSAlexander Motin 	 * If we were called synchronously in the original thread then just
4772c7dc6baSAlexander Motin 	 * dispatch, otherwise we now have to queue it to the task queue to
478130f4520SKenneth D. Merry 	 * execute the backend I/O.  That is because we do blocking
479130f4520SKenneth D. Merry 	 * memory allocations, and in the file backing case, blocking I/O.
480130f4520SKenneth D. Merry 	 * This move done routine is generally called in the SIM's
481130f4520SKenneth D. Merry 	 * interrupt context, and therefore we cannot block.
482130f4520SKenneth D. Merry 	 */
483a59e2982SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
4842c7dc6baSAlexander Motin 	if (samethr) {
4852c7dc6baSAlexander Motin 		be_lun->dispatch(be_lun, beio);
4862c7dc6baSAlexander Motin 	} else {
48775c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
488130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
48975c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
490130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
4912c7dc6baSAlexander Motin 	}
492130f4520SKenneth D. Merry 	return (0);
493130f4520SKenneth D. Merry }
494130f4520SKenneth D. Merry 
495130f4520SKenneth D. Merry static void
496130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio)
497130f4520SKenneth D. Merry {
498ac503c19SAlexander Motin 	struct ctl_be_block_io *beio = bio->bio_caller1;
499ac503c19SAlexander Motin 	struct ctl_be_block_lun *be_lun = beio->lun;
500ac503c19SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
501130f4520SKenneth D. Merry 	union ctl_io *io;
502e0c2f975SAlexander Motin 	int error;
503130f4520SKenneth D. Merry 
504130f4520SKenneth D. Merry 	io = beio->io;
505130f4520SKenneth D. Merry 
506130f4520SKenneth D. Merry 	DPRINTF("entered\n");
507130f4520SKenneth D. Merry 
508e0c2f975SAlexander Motin 	error = bio->bio_error;
50975c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
5101f0694a6SAlexander Motin 	if (error != 0 &&
5111f0694a6SAlexander Motin 	    (beio->first_error == 0 ||
5121f0694a6SAlexander Motin 	     bio->bio_offset < beio->first_error_offset)) {
5131f0694a6SAlexander Motin 		beio->first_error = error;
5141f0694a6SAlexander Motin 		beio->first_error_offset = bio->bio_offset;
5151f0694a6SAlexander Motin 	}
516130f4520SKenneth D. Merry 
517130f4520SKenneth D. Merry 	beio->num_bios_done++;
518130f4520SKenneth D. Merry 
519130f4520SKenneth D. Merry 	/*
520130f4520SKenneth D. Merry 	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
521130f4520SKenneth D. Merry 	 * during the free might cause it to complain.
522130f4520SKenneth D. Merry 	 */
523130f4520SKenneth D. Merry 	g_destroy_bio(bio);
524130f4520SKenneth D. Merry 
525130f4520SKenneth D. Merry 	/*
526130f4520SKenneth D. Merry 	 * If the send complete bit isn't set, or we aren't the last I/O to
527130f4520SKenneth D. Merry 	 * complete, then we're done.
528130f4520SKenneth D. Merry 	 */
529130f4520SKenneth D. Merry 	if ((beio->send_complete == 0)
530130f4520SKenneth D. Merry 	 || (beio->num_bios_done < beio->num_bios_sent)) {
53175c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
532130f4520SKenneth D. Merry 		return;
533130f4520SKenneth D. Merry 	}
534130f4520SKenneth D. Merry 
535130f4520SKenneth D. Merry 	/*
536130f4520SKenneth D. Merry 	 * At this point, we've verified that we are the last I/O to
537130f4520SKenneth D. Merry 	 * complete, so it's safe to drop the lock.
538130f4520SKenneth D. Merry 	 */
53975c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
54075c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
54175c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
54275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
543130f4520SKenneth D. Merry 
544130f4520SKenneth D. Merry 	/*
545130f4520SKenneth D. Merry 	 * If there are any errors from the backing device, we fail the
546130f4520SKenneth D. Merry 	 * entire I/O with a medium error.
547130f4520SKenneth D. Merry 	 */
5481f0694a6SAlexander Motin 	error = beio->first_error;
5491f0694a6SAlexander Motin 	if (error != 0) {
550e0c2f975SAlexander Motin 		if (error == EOPNOTSUPP) {
551e0c2f975SAlexander Motin 			ctl_set_invalid_opcode(&io->scsiio);
5520631de4aSAlexander Motin 		} else if (error == ENOSPC || error == EDQUOT) {
5534fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
5546187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
5556187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
556e0c2f975SAlexander Motin 		} else if (beio->bio_cmd == BIO_FLUSH) {
557130f4520SKenneth D. Merry 			/* XXX KDM is there is a better error here? */
558130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
559130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
560130f4520SKenneth D. Merry 						 /*retry_count*/ 0xbad2);
5617f7bb97aSAlexander Motin 		} else {
5627f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
5637f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
5647f7bb97aSAlexander Motin 		}
565130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
566130f4520SKenneth D. Merry 		return;
567130f4520SKenneth D. Merry 	}
568130f4520SKenneth D. Merry 
569130f4520SKenneth D. Merry 	/*
57011b569f7SAlexander Motin 	 * If this is a write, a flush, a delete or verify, we're all done.
571130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
572130f4520SKenneth D. Merry 	 */
573130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_WRITE)
574ee7f31c0SAlexander Motin 	 || (beio->bio_cmd == BIO_FLUSH)
57511b569f7SAlexander Motin 	 || (beio->bio_cmd == BIO_DELETE)
57611b569f7SAlexander Motin 	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
577130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
578130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
579130f4520SKenneth D. Merry 	} else {
580f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
58175a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
582f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
583ac503c19SAlexander Motin 			if (cbe_lun->serseq >= CTL_LUN_SERSEQ_SOFT)
58475a3108eSAlexander Motin 				ctl_serseq_done(io);
58575a3108eSAlexander Motin 		}
586130f4520SKenneth D. Merry 		ctl_datamove(io);
587130f4520SKenneth D. Merry 	}
588130f4520SKenneth D. Merry }
589130f4520SKenneth D. Merry 
590130f4520SKenneth D. Merry static void
591130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
592130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio)
593130f4520SKenneth D. Merry {
59475c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
595130f4520SKenneth D. Merry 	struct mount *mountpoint;
5960ef5eee9SKonstantin Belousov 	int error;
597130f4520SKenneth D. Merry 
598130f4520SKenneth D. Merry 	DPRINTF("entered\n");
599130f4520SKenneth D. Merry 
60075c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
60175c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
602130f4520SKenneth D. Merry 
603130f4520SKenneth D. Merry 	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
604130f4520SKenneth D. Merry 
6050ef5eee9SKonstantin Belousov 	vn_lock(be_lun->vn, vn_lktype_write(mountpoint, be_lun->vn) |
6060ef5eee9SKonstantin Belousov 	    LK_RETRY);
6077d0d4342SAlexander Motin 	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
6087d0d4342SAlexander Motin 	    curthread);
609b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
610130f4520SKenneth D. Merry 
611130f4520SKenneth D. Merry 	vn_finished_write(mountpoint);
612130f4520SKenneth D. Merry 
61375c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
61475c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
61575c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
61675c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
61775c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
61875c7a1d3SAlexander Motin 
619130f4520SKenneth D. Merry 	if (error == 0)
620130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
621130f4520SKenneth D. Merry 	else {
622130f4520SKenneth D. Merry 		/* XXX KDM is there is a better error here? */
623130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
624130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
625130f4520SKenneth D. Merry 					 /*retry_count*/ 0xbad1);
626130f4520SKenneth D. Merry 	}
627130f4520SKenneth D. Merry 
628130f4520SKenneth D. Merry 	ctl_complete_beio(beio);
629130f4520SKenneth D. Merry }
630130f4520SKenneth D. Merry 
63136160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
63236160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
63336160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
63436160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
635130f4520SKenneth D. Merry 
636130f4520SKenneth D. Merry static void
637130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
638130f4520SKenneth D. Merry 			   struct ctl_be_block_io *beio)
639130f4520SKenneth D. Merry {
640ac503c19SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
641130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
642130f4520SKenneth D. Merry 	union ctl_io *io;
643130f4520SKenneth D. Merry 	struct uio xuio;
644130f4520SKenneth D. Merry 	struct iovec *xiovec;
64583981e31SAlexander Motin 	size_t s;
64683981e31SAlexander Motin 	int error, flags, i;
647130f4520SKenneth D. Merry 
648130f4520SKenneth D. Merry 	DPRINTF("entered\n");
649130f4520SKenneth D. Merry 
650130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
651130f4520SKenneth D. Merry 	io = beio->io;
65255551d05SAlexander Motin 	flags = 0;
65355551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
65455551d05SAlexander Motin 		flags |= IO_DIRECT;
65555551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
65655551d05SAlexander Motin 		flags |= IO_SYNC;
657130f4520SKenneth D. Merry 
65811b569f7SAlexander Motin 	bzero(&xuio, sizeof(xuio));
659130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
66036160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
66111b569f7SAlexander Motin 		xuio.uio_rw = UIO_READ;
662130f4520SKenneth D. Merry 	} else {
66336160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
664130f4520SKenneth D. Merry 		xuio.uio_rw = UIO_WRITE;
66511b569f7SAlexander Motin 	}
666130f4520SKenneth D. Merry 	xuio.uio_offset = beio->io_offset;
667130f4520SKenneth D. Merry 	xuio.uio_resid = beio->io_len;
668130f4520SKenneth D. Merry 	xuio.uio_segflg = UIO_SYSSPACE;
669130f4520SKenneth D. Merry 	xuio.uio_iov = beio->xiovecs;
670130f4520SKenneth D. Merry 	xuio.uio_iovcnt = beio->num_segs;
671130f4520SKenneth D. Merry 	xuio.uio_td = curthread;
672130f4520SKenneth D. Merry 
673130f4520SKenneth D. Merry 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
674130f4520SKenneth D. Merry 		xiovec->iov_base = beio->sg_segs[i].addr;
675130f4520SKenneth D. Merry 		xiovec->iov_len = beio->sg_segs[i].len;
676130f4520SKenneth D. Merry 	}
677130f4520SKenneth D. Merry 
67875c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
67975c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
68075c7a1d3SAlexander Motin 
681130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
682130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
683130f4520SKenneth D. Merry 
684ac503c19SAlexander Motin 		if (beio->beio_cont == NULL &&
685ac503c19SAlexander Motin 		    cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
686ac503c19SAlexander Motin 			ctl_serseq_done(io);
687130f4520SKenneth D. Merry 		/*
688130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for reads.  If the
689130f4520SKenneth D. Merry 		 * DIRECTIO option is configured into the kernel, it calls
690130f4520SKenneth D. Merry 		 * ffs_rawread().  But that only works for single-segment
691130f4520SKenneth D. Merry 		 * uios with user space addresses.  In our case, with a
692130f4520SKenneth D. Merry 		 * kernel uio, it still reads into the buffer cache, but it
693130f4520SKenneth D. Merry 		 * will just try to release the buffer from the cache later
694130f4520SKenneth D. Merry 		 * on in ffs_read().
695130f4520SKenneth D. Merry 		 *
696130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for reads.
697130f4520SKenneth D. Merry 		 *
698130f4520SKenneth D. Merry 		 * UFS does not pay attention to IO_SYNC for reads.
699130f4520SKenneth D. Merry 		 *
700130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (which translates into the
701130f4520SKenneth D. Merry 		 * Solaris define FRSYNC for zfs_read()) for reads.  It
702130f4520SKenneth D. Merry 		 * attempts to sync the file before reading.
703130f4520SKenneth D. Merry 		 */
70455551d05SAlexander Motin 		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
705130f4520SKenneth D. Merry 
706b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
70736160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
70883981e31SAlexander Motin 		if (error == 0 && xuio.uio_resid > 0) {
70983981e31SAlexander Motin 			/*
71083981e31SAlexander Motin 			 * If we red less then requested (EOF), then
71183981e31SAlexander Motin 			 * we should clean the rest of the buffer.
71283981e31SAlexander Motin 			 */
71383981e31SAlexander Motin 			s = beio->io_len - xuio.uio_resid;
71483981e31SAlexander Motin 			for (i = 0; i < beio->num_segs; i++) {
71583981e31SAlexander Motin 				if (s >= beio->sg_segs[i].len) {
71683981e31SAlexander Motin 					s -= beio->sg_segs[i].len;
71783981e31SAlexander Motin 					continue;
71883981e31SAlexander Motin 				}
71983981e31SAlexander Motin 				bzero((uint8_t *)beio->sg_segs[i].addr + s,
72083981e31SAlexander Motin 				    beio->sg_segs[i].len - s);
72183981e31SAlexander Motin 				s = 0;
72283981e31SAlexander Motin 			}
72383981e31SAlexander Motin 		}
724130f4520SKenneth D. Merry 	} else {
725130f4520SKenneth D. Merry 		struct mount *mountpoint;
726130f4520SKenneth D. Merry 
727130f4520SKenneth D. Merry 		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
7280ef5eee9SKonstantin Belousov 		vn_lock(be_lun->vn, vn_lktype_write(mountpoint,
7290ef5eee9SKonstantin Belousov 		    be_lun->vn) | LK_RETRY);
730130f4520SKenneth D. Merry 
731130f4520SKenneth D. Merry 		/*
732130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for writes.  The write
733130f4520SKenneth D. Merry 		 * is done asynchronously.  (Normally the write would just
734130f4520SKenneth D. Merry 		 * get put into cache.
735130f4520SKenneth D. Merry 		 *
736130f4520SKenneth D. Merry 		 * UFS pays attention to IO_SYNC for writes.  It will
737130f4520SKenneth D. Merry 		 * attempt to write the buffer out synchronously if that
738130f4520SKenneth D. Merry 		 * flag is set.
739130f4520SKenneth D. Merry 		 *
740130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for writes.
741130f4520SKenneth D. Merry 		 *
742130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
743130f4520SKenneth D. Merry 		 * for writes.  It will flush the transaction from the
744130f4520SKenneth D. Merry 		 * cache before returning.
745130f4520SKenneth D. Merry 		 */
74655551d05SAlexander Motin 		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
747b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
748130f4520SKenneth D. Merry 
749130f4520SKenneth D. Merry 		vn_finished_write(mountpoint);
75036160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
751130f4520SKenneth D. Merry         }
752130f4520SKenneth D. Merry 
75375c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
75475c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
75575c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
75675c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
75775c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
75875c7a1d3SAlexander Motin 
759130f4520SKenneth D. Merry 	/*
760130f4520SKenneth D. Merry 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
761130f4520SKenneth D. Merry 	 * return the I/O to the user.
762130f4520SKenneth D. Merry 	 */
763130f4520SKenneth D. Merry 	if (error != 0) {
7640631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
7654fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
7666187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
7676187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
7687f7bb97aSAlexander Motin 		} else {
7697f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
7707f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
7717f7bb97aSAlexander Motin 		}
772130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
773130f4520SKenneth D. Merry 		return;
774130f4520SKenneth D. Merry 	}
775130f4520SKenneth D. Merry 
776130f4520SKenneth D. Merry 	/*
777696297adSAlexander Motin 	 * If this is a write or a verify, we're all done.
778130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
779130f4520SKenneth D. Merry 	 */
780696297adSAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
781696297adSAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
782130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
783130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
784130f4520SKenneth D. Merry 	} else {
785f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
78675a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
787f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
788ac503c19SAlexander Motin 			if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
78975a3108eSAlexander Motin 				ctl_serseq_done(io);
79075a3108eSAlexander Motin 		}
791130f4520SKenneth D. Merry 		ctl_datamove(io);
792130f4520SKenneth D. Merry 	}
793130f4520SKenneth D. Merry }
794130f4520SKenneth D. Merry 
795130f4520SKenneth D. Merry static void
796ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
797ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
798ef8daf3fSAlexander Motin {
799ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
800ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
801ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
802ef8daf3fSAlexander Motin 	off_t roff, off;
803ef8daf3fSAlexander Motin 	int error, status;
804ef8daf3fSAlexander Motin 
805ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
806ef8daf3fSAlexander Motin 
8070bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
808ef8daf3fSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
809ef8daf3fSAlexander Motin 	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
810ef8daf3fSAlexander Motin 	    0, curthread->td_ucred, curthread);
811ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
812ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
813ef8daf3fSAlexander Motin 	else {
814ef8daf3fSAlexander Motin 		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
815ef8daf3fSAlexander Motin 		    0, curthread->td_ucred, curthread);
816ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
817ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
818ef8daf3fSAlexander Motin 		else {
819ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
820ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
821ef8daf3fSAlexander Motin 		}
822ef8daf3fSAlexander Motin 	}
823b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
824ef8daf3fSAlexander Motin 
825ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
826ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
8270bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
8280bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
829ef8daf3fSAlexander Motin 	data->descr[0].status = status;
830ef8daf3fSAlexander Motin 
831ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
832ef8daf3fSAlexander Motin }
833ef8daf3fSAlexander Motin 
83453c146deSAlexander Motin static uint64_t
83553c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
83653c146deSAlexander Motin {
83753c146deSAlexander Motin 	struct vattr		vattr;
83853c146deSAlexander Motin 	struct statfs		statfs;
839b9b4269cSAlexander Motin 	uint64_t		val;
84053c146deSAlexander Motin 	int			error;
84153c146deSAlexander Motin 
842b9b4269cSAlexander Motin 	val = UINT64_MAX;
84353c146deSAlexander Motin 	if (be_lun->vn == NULL)
844b9b4269cSAlexander Motin 		return (val);
845b9b4269cSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
84653c146deSAlexander Motin 	if (strcmp(attrname, "blocksused") == 0) {
84753c146deSAlexander Motin 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
848b9b4269cSAlexander Motin 		if (error == 0)
8490bcd4ab6SAlexander Motin 			val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
85053c146deSAlexander Motin 	}
851b9b4269cSAlexander Motin 	if (strcmp(attrname, "blocksavail") == 0 &&
852abd80ddbSMateusz Guzik 	    !VN_IS_DOOMED(be_lun->vn)) {
85353c146deSAlexander Motin 		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
854b9b4269cSAlexander Motin 		if (error == 0)
855a15bbf15SAlexander Motin 			val = statfs.f_bavail * statfs.f_bsize /
8560bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
85753c146deSAlexander Motin 	}
858b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
859b9b4269cSAlexander Motin 	return (val);
86053c146deSAlexander Motin }
86153c146deSAlexander Motin 
862ef8daf3fSAlexander Motin static void
86349050613SKa Ho Ng ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
86449050613SKa Ho Ng 		        struct ctl_be_block_io *beio)
86549050613SKa Ho Ng {
86649050613SKa Ho Ng 	struct ctl_be_block_filedata *file_data;
86749050613SKa Ho Ng 	union ctl_io *io;
86849050613SKa Ho Ng 	struct ctl_ptr_len_flags *ptrlen;
86949050613SKa Ho Ng 	struct scsi_unmap_desc *buf, *end;
87049050613SKa Ho Ng 	struct mount *mp;
87149050613SKa Ho Ng 	off_t off, len;
87249050613SKa Ho Ng 	int error;
87349050613SKa Ho Ng 
87449050613SKa Ho Ng 	io = beio->io;
87549050613SKa Ho Ng 	file_data = &be_lun->backend.file;
87649050613SKa Ho Ng 	mp = NULL;
87749050613SKa Ho Ng 	error = 0;
87849050613SKa Ho Ng 
87949050613SKa Ho Ng 	binuptime(&beio->ds_t0);
88049050613SKa Ho Ng 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
88149050613SKa Ho Ng 
88249050613SKa Ho Ng 	(void)vn_start_write(be_lun->vn, &mp, V_WAIT);
88349050613SKa Ho Ng 	vn_lock(be_lun->vn, vn_lktype_write(mp, be_lun->vn) | LK_RETRY);
88449050613SKa Ho Ng 	if (beio->io_offset == -1) {
88549050613SKa Ho Ng 		beio->io_len = 0;
88649050613SKa Ho Ng 		ptrlen = (struct ctl_ptr_len_flags *)
88749050613SKa Ho Ng 		    &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
88849050613SKa Ho Ng 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
88949050613SKa Ho Ng 		end = buf + ptrlen->len / sizeof(*buf);
89049050613SKa Ho Ng 		for (; buf < end; buf++) {
89149050613SKa Ho Ng 			off = (off_t)scsi_8btou64(buf->lba) *
89249050613SKa Ho Ng 			    be_lun->cbe_lun.blocksize;
89349050613SKa Ho Ng 			len = (off_t)scsi_4btoul(buf->length) *
89449050613SKa Ho Ng 			    be_lun->cbe_lun.blocksize;
89549050613SKa Ho Ng 			beio->io_len += len;
89649050613SKa Ho Ng 			error = vn_deallocate(be_lun->vn, &off, &len,
89749050613SKa Ho Ng 			    0, IO_NOMACCHECK | IO_NODELOCKED, file_data->cred,
89849050613SKa Ho Ng 			    NOCRED);
89949050613SKa Ho Ng 			if (error != 0)
90049050613SKa Ho Ng 				break;
90149050613SKa Ho Ng 		}
90249050613SKa Ho Ng 	} else {
90349050613SKa Ho Ng 		/* WRITE_SAME */
90449050613SKa Ho Ng 		off = beio->io_offset;
90549050613SKa Ho Ng 		len = beio->io_len;
90649050613SKa Ho Ng 		error = vn_deallocate(be_lun->vn, &off, &len, 0,
90749050613SKa Ho Ng 		    IO_NOMACCHECK | IO_NODELOCKED, file_data->cred, NOCRED);
90849050613SKa Ho Ng 	}
90949050613SKa Ho Ng 	VOP_UNLOCK(be_lun->vn);
91049050613SKa Ho Ng 	vn_finished_write(mp);
91149050613SKa Ho Ng 
91249050613SKa Ho Ng 	mtx_lock(&be_lun->io_lock);
91349050613SKa Ho Ng 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
91449050613SKa Ho Ng 	    beio->ds_tag_type, beio->ds_trans_type,
91549050613SKa Ho Ng 	    /*now*/ NULL, /*then*/&beio->ds_t0);
91649050613SKa Ho Ng 	mtx_unlock(&be_lun->io_lock);
91749050613SKa Ho Ng 
91849050613SKa Ho Ng 	/*
91949050613SKa Ho Ng 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
92049050613SKa Ho Ng 	 * return the I/O to the user.
92149050613SKa Ho Ng 	 */
92249050613SKa Ho Ng 	switch (error) {
92349050613SKa Ho Ng 	case 0:
92449050613SKa Ho Ng 		ctl_set_success(&io->scsiio);
92549050613SKa Ho Ng 		break;
92649050613SKa Ho Ng 	case ENOSPC:
92749050613SKa Ho Ng 	case EDQUOT:
92849050613SKa Ho Ng 		ctl_set_space_alloc_fail(&io->scsiio);
92949050613SKa Ho Ng 		break;
93049050613SKa Ho Ng 	case EROFS:
93149050613SKa Ho Ng 	case EACCES:
93249050613SKa Ho Ng 		ctl_set_hw_write_protected(&io->scsiio);
93349050613SKa Ho Ng 		break;
93449050613SKa Ho Ng 	default:
93549050613SKa Ho Ng 		ctl_set_medium_error(&io->scsiio, false);
93649050613SKa Ho Ng 	}
93749050613SKa Ho Ng 	ctl_complete_beio(beio);
93849050613SKa Ho Ng }
93949050613SKa Ho Ng 
94049050613SKa Ho Ng static void
94167f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
94267f586a8SAlexander Motin 			   struct ctl_be_block_io *beio)
94367f586a8SAlexander Motin {
944ac503c19SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
94567f586a8SAlexander Motin 	union ctl_io *io;
9463236151eSAlexander Motin 	struct cdevsw *csw;
9473236151eSAlexander Motin 	struct cdev *dev;
94867f586a8SAlexander Motin 	struct uio xuio;
94967f586a8SAlexander Motin 	struct iovec *xiovec;
9503236151eSAlexander Motin 	int error, flags, i, ref;
95167f586a8SAlexander Motin 
95267f586a8SAlexander Motin 	DPRINTF("entered\n");
95367f586a8SAlexander Motin 
95467f586a8SAlexander Motin 	io = beio->io;
95555551d05SAlexander Motin 	flags = 0;
95655551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
95755551d05SAlexander Motin 		flags |= IO_DIRECT;
95855551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
95955551d05SAlexander Motin 		flags |= IO_SYNC;
96067f586a8SAlexander Motin 
96167f586a8SAlexander Motin 	bzero(&xuio, sizeof(xuio));
96267f586a8SAlexander Motin 	if (beio->bio_cmd == BIO_READ) {
96336160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
96467f586a8SAlexander Motin 		xuio.uio_rw = UIO_READ;
96567f586a8SAlexander Motin 	} else {
96636160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
96767f586a8SAlexander Motin 		xuio.uio_rw = UIO_WRITE;
96867f586a8SAlexander Motin 	}
96967f586a8SAlexander Motin 	xuio.uio_offset = beio->io_offset;
97067f586a8SAlexander Motin 	xuio.uio_resid = beio->io_len;
97167f586a8SAlexander Motin 	xuio.uio_segflg = UIO_SYSSPACE;
97267f586a8SAlexander Motin 	xuio.uio_iov = beio->xiovecs;
97367f586a8SAlexander Motin 	xuio.uio_iovcnt = beio->num_segs;
97467f586a8SAlexander Motin 	xuio.uio_td = curthread;
97567f586a8SAlexander Motin 
97667f586a8SAlexander Motin 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
97767f586a8SAlexander Motin 		xiovec->iov_base = beio->sg_segs[i].addr;
97867f586a8SAlexander Motin 		xiovec->iov_len = beio->sg_segs[i].len;
97967f586a8SAlexander Motin 	}
98067f586a8SAlexander Motin 
98167f586a8SAlexander Motin 	binuptime(&beio->ds_t0);
98267f586a8SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
98367f586a8SAlexander Motin 
9843236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
9853236151eSAlexander Motin 	if (csw) {
986ac503c19SAlexander Motin 		if (beio->bio_cmd == BIO_READ) {
987ac503c19SAlexander Motin 			if (beio->beio_cont == NULL &&
988ac503c19SAlexander Motin 			    cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
989ac503c19SAlexander Motin 				ctl_serseq_done(io);
9903236151eSAlexander Motin 			error = csw->d_read(dev, &xuio, flags);
991ac503c19SAlexander Motin 		} else
9923236151eSAlexander Motin 			error = csw->d_write(dev, &xuio, flags);
9933236151eSAlexander Motin 		dev_relthread(dev, ref);
9943236151eSAlexander Motin 	} else
9953236151eSAlexander Motin 		error = ENXIO;
9963236151eSAlexander Motin 
9973236151eSAlexander Motin 	if (beio->bio_cmd == BIO_READ)
99836160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
9993236151eSAlexander Motin 	else
100036160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
100167f586a8SAlexander Motin 
100267f586a8SAlexander Motin 	mtx_lock(&be_lun->io_lock);
100367f586a8SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
100467f586a8SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
100567f586a8SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
100667f586a8SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
100767f586a8SAlexander Motin 
100867f586a8SAlexander Motin 	/*
100967f586a8SAlexander Motin 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
101067f586a8SAlexander Motin 	 * return the I/O to the user.
101167f586a8SAlexander Motin 	 */
101267f586a8SAlexander Motin 	if (error != 0) {
10130631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
10144fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
10156187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
10166187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
10177f7bb97aSAlexander Motin 		} else {
10187f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
10197f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
10207f7bb97aSAlexander Motin 		}
102167f586a8SAlexander Motin 		ctl_complete_beio(beio);
102267f586a8SAlexander Motin 		return;
102367f586a8SAlexander Motin 	}
102467f586a8SAlexander Motin 
102567f586a8SAlexander Motin 	/*
102667f586a8SAlexander Motin 	 * If this is a write or a verify, we're all done.
102767f586a8SAlexander Motin 	 * If this is a read, we can now send the data to the user.
102867f586a8SAlexander Motin 	 */
102967f586a8SAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
103067f586a8SAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
103167f586a8SAlexander Motin 		ctl_set_success(&io->scsiio);
103267f586a8SAlexander Motin 		ctl_complete_beio(beio);
103367f586a8SAlexander Motin 	} else {
1034f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
103575a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
1036f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
1037ac503c19SAlexander Motin 			if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
103875a3108eSAlexander Motin 				ctl_serseq_done(io);
103975a3108eSAlexander Motin 		}
104067f586a8SAlexander Motin 		ctl_datamove(io);
104167f586a8SAlexander Motin 	}
104267f586a8SAlexander Motin }
104367f586a8SAlexander Motin 
104467f586a8SAlexander Motin static void
1045ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
1046ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
1047ef8daf3fSAlexander Motin {
1048ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
10493236151eSAlexander Motin 	struct cdevsw *csw;
10503236151eSAlexander Motin 	struct cdev *dev;
1051ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
1052ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
1053ef8daf3fSAlexander Motin 	off_t roff, off;
10543236151eSAlexander Motin 	int error, ref, status;
1055ef8daf3fSAlexander Motin 
1056ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1057ef8daf3fSAlexander Motin 
10583236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10593236151eSAlexander Motin 	if (csw == NULL) {
10603236151eSAlexander Motin 		status = 0;	/* unknown up to the end */
10613236151eSAlexander Motin 		off = be_lun->size_bytes;
10623236151eSAlexander Motin 		goto done;
10633236151eSAlexander Motin 	}
10640bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
10653236151eSAlexander Motin 	error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
10663236151eSAlexander Motin 	    curthread);
1067ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
1068ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
1069ef8daf3fSAlexander Motin 	else {
10703236151eSAlexander Motin 		error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
10713236151eSAlexander Motin 		    curthread);
1072ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
1073ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
1074ef8daf3fSAlexander Motin 		else {
1075ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
1076ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
1077ef8daf3fSAlexander Motin 		}
1078ef8daf3fSAlexander Motin 	}
10793236151eSAlexander Motin 	dev_relthread(dev, ref);
1080ef8daf3fSAlexander Motin 
10813236151eSAlexander Motin done:
1082ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
1083ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
10840bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
10850bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
1086ef8daf3fSAlexander Motin 	data->descr[0].status = status;
1087ef8daf3fSAlexander Motin 
1088ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
1089ef8daf3fSAlexander Motin }
1090ef8daf3fSAlexander Motin 
1091ef8daf3fSAlexander Motin static void
1092130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1093130f4520SKenneth D. Merry 		       struct ctl_be_block_io *beio)
1094130f4520SKenneth D. Merry {
1095130f4520SKenneth D. Merry 	struct bio *bio;
10963236151eSAlexander Motin 	struct cdevsw *csw;
10973236151eSAlexander Motin 	struct cdev *dev;
10983236151eSAlexander Motin 	int ref;
1099130f4520SKenneth D. Merry 
1100130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1101130f4520SKenneth D. Merry 
1102130f4520SKenneth D. Merry 	/* This can't fail, it's a blocking allocation. */
1103130f4520SKenneth D. Merry 	bio = g_alloc_bio();
1104130f4520SKenneth D. Merry 
1105130f4520SKenneth D. Merry 	bio->bio_cmd	    = BIO_FLUSH;
1106130f4520SKenneth D. Merry 	bio->bio_offset	    = 0;
1107130f4520SKenneth D. Merry 	bio->bio_data	    = 0;
1108130f4520SKenneth D. Merry 	bio->bio_done	    = ctl_be_block_biodone;
1109130f4520SKenneth D. Merry 	bio->bio_caller1    = beio;
1110130f4520SKenneth D. Merry 	bio->bio_pblkno	    = 0;
1111130f4520SKenneth D. Merry 
1112130f4520SKenneth D. Merry 	/*
1113130f4520SKenneth D. Merry 	 * We don't need to acquire the LUN lock here, because we are only
1114130f4520SKenneth D. Merry 	 * sending one bio, and so there is no other context to synchronize
1115130f4520SKenneth D. Merry 	 * with.
1116130f4520SKenneth D. Merry 	 */
1117130f4520SKenneth D. Merry 	beio->num_bios_sent = 1;
1118130f4520SKenneth D. Merry 	beio->send_complete = 1;
1119130f4520SKenneth D. Merry 
1120130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
1121130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1122130f4520SKenneth D. Merry 
11233236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
11243236151eSAlexander Motin 	if (csw) {
11253236151eSAlexander Motin 		bio->bio_dev = dev;
11263236151eSAlexander Motin 		csw->d_strategy(bio);
11273236151eSAlexander Motin 		dev_relthread(dev, ref);
11283236151eSAlexander Motin 	} else {
11293236151eSAlexander Motin 		bio->bio_error = ENXIO;
11303236151eSAlexander Motin 		ctl_be_block_biodone(bio);
11313236151eSAlexander Motin 	}
1132130f4520SKenneth D. Merry }
1133130f4520SKenneth D. Merry 
1134130f4520SKenneth D. Merry static void
1135ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1136ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio,
1137ee7f31c0SAlexander Motin 		       uint64_t off, uint64_t len, int last)
1138ee7f31c0SAlexander Motin {
1139ee7f31c0SAlexander Motin 	struct bio *bio;
11408f5a226aSAlexander Motin 	uint64_t maxlen;
11413236151eSAlexander Motin 	struct cdevsw *csw;
11423236151eSAlexander Motin 	struct cdev *dev;
11433236151eSAlexander Motin 	int ref;
1144ee7f31c0SAlexander Motin 
11453236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
11460bcd4ab6SAlexander Motin 	maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1147ee7f31c0SAlexander Motin 	while (len > 0) {
1148ee7f31c0SAlexander Motin 		bio = g_alloc_bio();
1149ee7f31c0SAlexander Motin 		bio->bio_cmd	    = BIO_DELETE;
11503236151eSAlexander Motin 		bio->bio_dev	    = dev;
1151ee7f31c0SAlexander Motin 		bio->bio_offset	    = off;
11528f5a226aSAlexander Motin 		bio->bio_length	    = MIN(len, maxlen);
1153ee7f31c0SAlexander Motin 		bio->bio_data	    = 0;
1154ee7f31c0SAlexander Motin 		bio->bio_done	    = ctl_be_block_biodone;
1155ee7f31c0SAlexander Motin 		bio->bio_caller1    = beio;
11560bcd4ab6SAlexander Motin 		bio->bio_pblkno     = off / be_lun->cbe_lun.blocksize;
1157ee7f31c0SAlexander Motin 
1158ee7f31c0SAlexander Motin 		off += bio->bio_length;
1159ee7f31c0SAlexander Motin 		len -= bio->bio_length;
1160ee7f31c0SAlexander Motin 
116175c7a1d3SAlexander Motin 		mtx_lock(&be_lun->io_lock);
1162ee7f31c0SAlexander Motin 		beio->num_bios_sent++;
1163ee7f31c0SAlexander Motin 		if (last && len == 0)
1164ee7f31c0SAlexander Motin 			beio->send_complete = 1;
116575c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
1166ee7f31c0SAlexander Motin 
11673236151eSAlexander Motin 		if (csw) {
11683236151eSAlexander Motin 			csw->d_strategy(bio);
11693236151eSAlexander Motin 		} else {
11703236151eSAlexander Motin 			bio->bio_error = ENXIO;
11713236151eSAlexander Motin 			ctl_be_block_biodone(bio);
1172ee7f31c0SAlexander Motin 		}
1173ee7f31c0SAlexander Motin 	}
11743236151eSAlexander Motin 	if (csw)
11753236151eSAlexander Motin 		dev_relthread(dev, ref);
11763236151eSAlexander Motin }
1177ee7f31c0SAlexander Motin 
1178ee7f31c0SAlexander Motin static void
1179ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1180ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio)
1181ee7f31c0SAlexander Motin {
1182ee7f31c0SAlexander Motin 	union ctl_io *io;
118366df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1184ee7f31c0SAlexander Motin 	struct scsi_unmap_desc *buf, *end;
1185ee7f31c0SAlexander Motin 	uint64_t len;
1186ee7f31c0SAlexander Motin 
1187ee7f31c0SAlexander Motin 	io = beio->io;
1188ee7f31c0SAlexander Motin 
1189ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1190ee7f31c0SAlexander Motin 
1191ee7f31c0SAlexander Motin 	binuptime(&beio->ds_t0);
1192ee7f31c0SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1193ee7f31c0SAlexander Motin 
1194ee7f31c0SAlexander Motin 	if (beio->io_offset == -1) {
1195ee7f31c0SAlexander Motin 		beio->io_len = 0;
119666df9136SAlexander Motin 		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
119766df9136SAlexander Motin 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
119866df9136SAlexander Motin 		end = buf + ptrlen->len / sizeof(*buf);
1199ee7f31c0SAlexander Motin 		for (; buf < end; buf++) {
1200ee7f31c0SAlexander Motin 			len = (uint64_t)scsi_4btoul(buf->length) *
12010bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
1202ee7f31c0SAlexander Motin 			beio->io_len += len;
1203ee7f31c0SAlexander Motin 			ctl_be_block_unmap_dev_range(be_lun, beio,
12040bcd4ab6SAlexander Motin 			    scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
12050bcd4ab6SAlexander Motin 			    len, (end - buf < 2) ? TRUE : FALSE);
1206ee7f31c0SAlexander Motin 		}
1207ee7f31c0SAlexander Motin 	} else
1208ee7f31c0SAlexander Motin 		ctl_be_block_unmap_dev_range(be_lun, beio,
1209ee7f31c0SAlexander Motin 		    beio->io_offset, beio->io_len, TRUE);
1210ee7f31c0SAlexander Motin }
1211ee7f31c0SAlexander Motin 
1212ee7f31c0SAlexander Motin static void
1213130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1214130f4520SKenneth D. Merry 			  struct ctl_be_block_io *beio)
1215130f4520SKenneth D. Merry {
121675c7a1d3SAlexander Motin 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1217130f4520SKenneth D. Merry 	struct bio *bio;
12183236151eSAlexander Motin 	struct cdevsw *csw;
12193236151eSAlexander Motin 	struct cdev *dev;
1220130f4520SKenneth D. Merry 	off_t cur_offset;
12213236151eSAlexander Motin 	int i, max_iosize, ref;
1222130f4520SKenneth D. Merry 
1223130f4520SKenneth D. Merry 	DPRINTF("entered\n");
12243236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1225130f4520SKenneth D. Merry 
1226130f4520SKenneth D. Merry 	/*
1227130f4520SKenneth D. Merry 	 * We have to limit our I/O size to the maximum supported by the
12288054320eSAlexander Motin 	 * backend device.
1229130f4520SKenneth D. Merry 	 */
12303236151eSAlexander Motin 	if (csw) {
12313236151eSAlexander Motin 		max_iosize = dev->si_iosize_max;
1232b06771aaSAlexander Motin 		if (max_iosize <= 0)
1233130f4520SKenneth D. Merry 			max_iosize = DFLTPHYS;
12343236151eSAlexander Motin 	} else
1235b06771aaSAlexander Motin 		max_iosize = maxphys;
1236130f4520SKenneth D. Merry 
1237130f4520SKenneth D. Merry 	cur_offset = beio->io_offset;
1238130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
1239130f4520SKenneth D. Merry 		size_t cur_size;
1240130f4520SKenneth D. Merry 		uint8_t *cur_ptr;
1241130f4520SKenneth D. Merry 
1242130f4520SKenneth D. Merry 		cur_size = beio->sg_segs[i].len;
1243130f4520SKenneth D. Merry 		cur_ptr = beio->sg_segs[i].addr;
1244130f4520SKenneth D. Merry 
1245130f4520SKenneth D. Merry 		while (cur_size > 0) {
1246130f4520SKenneth D. Merry 			/* This can't fail, it's a blocking allocation. */
1247130f4520SKenneth D. Merry 			bio = g_alloc_bio();
1248130f4520SKenneth D. Merry 
1249130f4520SKenneth D. Merry 			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1250130f4520SKenneth D. Merry 
1251130f4520SKenneth D. Merry 			bio->bio_cmd = beio->bio_cmd;
12523236151eSAlexander Motin 			bio->bio_dev = dev;
1253130f4520SKenneth D. Merry 			bio->bio_caller1 = beio;
1254130f4520SKenneth D. Merry 			bio->bio_length = min(cur_size, max_iosize);
1255130f4520SKenneth D. Merry 			bio->bio_offset = cur_offset;
1256130f4520SKenneth D. Merry 			bio->bio_data = cur_ptr;
1257130f4520SKenneth D. Merry 			bio->bio_done = ctl_be_block_biodone;
12580bcd4ab6SAlexander Motin 			bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1259130f4520SKenneth D. Merry 
1260130f4520SKenneth D. Merry 			cur_offset += bio->bio_length;
1261130f4520SKenneth D. Merry 			cur_ptr += bio->bio_length;
1262130f4520SKenneth D. Merry 			cur_size -= bio->bio_length;
1263130f4520SKenneth D. Merry 
126475c7a1d3SAlexander Motin 			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1265130f4520SKenneth D. Merry 			beio->num_bios_sent++;
1266130f4520SKenneth D. Merry 		}
1267130f4520SKenneth D. Merry 	}
126875c7a1d3SAlexander Motin 	beio->send_complete = 1;
1269024932aaSAlexander Motin 	binuptime(&beio->ds_t0);
1270024932aaSAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
127175c7a1d3SAlexander Motin 
127275c7a1d3SAlexander Motin 	/*
127375c7a1d3SAlexander Motin 	 * Fire off all allocated requests!
127475c7a1d3SAlexander Motin 	 */
127575c7a1d3SAlexander Motin 	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
127675c7a1d3SAlexander Motin 		TAILQ_REMOVE(&queue, bio, bio_queue);
12773236151eSAlexander Motin 		if (csw)
12783236151eSAlexander Motin 			csw->d_strategy(bio);
12793236151eSAlexander Motin 		else {
12803236151eSAlexander Motin 			bio->bio_error = ENXIO;
12813236151eSAlexander Motin 			ctl_be_block_biodone(bio);
128275c7a1d3SAlexander Motin 		}
1283130f4520SKenneth D. Merry 	}
12843236151eSAlexander Motin 	if (csw)
12853236151eSAlexander Motin 		dev_relthread(dev, ref);
12863236151eSAlexander Motin }
1287130f4520SKenneth D. Merry 
1288c3e7ba3eSAlexander Motin static uint64_t
1289c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1290c3e7ba3eSAlexander Motin {
1291c3e7ba3eSAlexander Motin 	struct diocgattr_arg	arg;
12923236151eSAlexander Motin 	struct cdevsw *csw;
12933236151eSAlexander Motin 	struct cdev *dev;
12943236151eSAlexander Motin 	int error, ref;
1295c3e7ba3eSAlexander Motin 
12963236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
12973236151eSAlexander Motin 	if (csw == NULL)
1298c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1299c3e7ba3eSAlexander Motin 	strlcpy(arg.name, attrname, sizeof(arg.name));
1300c3e7ba3eSAlexander Motin 	arg.len = sizeof(arg.value.off);
13013236151eSAlexander Motin 	if (csw->d_ioctl) {
13023236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
13033236151eSAlexander Motin 		    curthread);
13043236151eSAlexander Motin 	} else
13053236151eSAlexander Motin 		error = ENODEV;
13063236151eSAlexander Motin 	dev_relthread(dev, ref);
1307c3e7ba3eSAlexander Motin 	if (error != 0)
1308c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1309c3e7ba3eSAlexander Motin 	return (arg.value.off);
1310c3e7ba3eSAlexander Motin }
1311c3e7ba3eSAlexander Motin 
1312130f4520SKenneth D. Merry static void
13137d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
13147d0d4342SAlexander Motin 			    union ctl_io *io)
13157d0d4342SAlexander Motin {
13160bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
13177d0d4342SAlexander Motin 	struct ctl_be_block_io *beio;
13187d0d4342SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
13197d0d4342SAlexander Motin 
13207d0d4342SAlexander Motin 	DPRINTF("entered\n");
13217d0d4342SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
13227d0d4342SAlexander Motin 	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
13237d0d4342SAlexander Motin 
13240bcd4ab6SAlexander Motin 	beio->io_len = lbalen->len * cbe_lun->blocksize;
13250bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13267d0d4342SAlexander Motin 	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
13277d0d4342SAlexander Motin 	beio->bio_cmd = BIO_FLUSH;
13287d0d4342SAlexander Motin 	beio->ds_trans_type = DEVSTAT_NO_DATA;
13297d0d4342SAlexander Motin 	DPRINTF("SYNC\n");
13307d0d4342SAlexander Motin 	be_lun->lun_flush(be_lun, beio);
13317d0d4342SAlexander Motin }
13327d0d4342SAlexander Motin 
13337d0d4342SAlexander Motin static void
1334ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1335ee7f31c0SAlexander Motin {
1336ee7f31c0SAlexander Motin 	union ctl_io *io;
1337ee7f31c0SAlexander Motin 
1338ee7f31c0SAlexander Motin 	io = beio->io;
1339ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1340ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1341ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1342ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1343ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1344ee7f31c0SAlexander Motin 		return;
1345ee7f31c0SAlexander Motin 	}
1346ee7f31c0SAlexander Motin 
1347ee7f31c0SAlexander Motin 	ctl_be_block_config_write(io);
1348ee7f31c0SAlexander Motin }
1349ee7f31c0SAlexander Motin 
1350ee7f31c0SAlexander Motin static void
1351ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1352ee7f31c0SAlexander Motin 			    union ctl_io *io)
1353ee7f31c0SAlexander Motin {
13540d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
13550bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1356ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
135766df9136SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1358fee04ef7SAlexander Motin 	uint64_t len_left, lba;
1359fee04ef7SAlexander Motin 	uint32_t pb, pbo, adj;
1360ee7f31c0SAlexander Motin 	int i, seglen;
1361ee7f31c0SAlexander Motin 	uint8_t *buf, *end;
1362ee7f31c0SAlexander Motin 
1363ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1364ee7f31c0SAlexander Motin 
1365e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1366a59e2982SAlexander Motin 	lbalen = ARGS(io);
1367ee7f31c0SAlexander Motin 
136864c5167cSAlexander Motin 	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
13693406a2a0SAlexander Motin 	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1370ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1371ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1372ee7f31c0SAlexander Motin 				      /*sks_valid*/ 1,
1373ee7f31c0SAlexander Motin 				      /*command*/ 1,
1374ee7f31c0SAlexander Motin 				      /*field*/ 1,
1375ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1376ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1377ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1378ee7f31c0SAlexander Motin 		return;
1379ee7f31c0SAlexander Motin 	}
1380ee7f31c0SAlexander Motin 
13813406a2a0SAlexander Motin 	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
13820bcd4ab6SAlexander Motin 		beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13830bcd4ab6SAlexander Motin 		beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1384ee7f31c0SAlexander Motin 		beio->bio_cmd = BIO_DELETE;
1385ee7f31c0SAlexander Motin 		beio->ds_trans_type = DEVSTAT_FREE;
1386ee7f31c0SAlexander Motin 
1387ee7f31c0SAlexander Motin 		be_lun->unmap(be_lun, beio);
1388ee7f31c0SAlexander Motin 		return;
1389ee7f31c0SAlexander Motin 	}
1390ee7f31c0SAlexander Motin 
1391ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_WRITE;
1392ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_WRITE;
1393ee7f31c0SAlexander Motin 
1394ee7f31c0SAlexander Motin 	DPRINTF("WRITE SAME at LBA %jx len %u\n",
139566df9136SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len);
1396ee7f31c0SAlexander Motin 
13970bcd4ab6SAlexander Motin 	pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
13980bcd4ab6SAlexander Motin 	if (be_lun->cbe_lun.pblockoff > 0)
13990bcd4ab6SAlexander Motin 		pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1400fee04ef7SAlexander Motin 	else
1401fee04ef7SAlexander Motin 		pbo = 0;
14020bcd4ab6SAlexander Motin 	len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1403b06771aaSAlexander Motin 	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1404ee7f31c0SAlexander Motin 		/*
1405ee7f31c0SAlexander Motin 		 * Setup the S/G entry for this chunk.
1406ee7f31c0SAlexander Motin 		 */
140708a7cce5SAlexander Motin 		seglen = MIN(CTLBLK_MAX_SEG, len_left);
14080bcd4ab6SAlexander Motin 		if (pb > cbe_lun->blocksize) {
14090bcd4ab6SAlexander Motin 			adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
141093b8c96cSAlexander Motin 			    seglen - pbo) % pb;
141193b8c96cSAlexander Motin 			if (seglen > adj)
141293b8c96cSAlexander Motin 				seglen -= adj;
141393b8c96cSAlexander Motin 			else
14140bcd4ab6SAlexander Motin 				seglen -= seglen % cbe_lun->blocksize;
141593b8c96cSAlexander Motin 		} else
14160bcd4ab6SAlexander Motin 			seglen -= seglen % cbe_lun->blocksize;
14178054320eSAlexander Motin 		ctl_alloc_seg(softc, &beio->sg_segs[i], seglen);
1418ee7f31c0SAlexander Motin 
1419ee7f31c0SAlexander Motin 		DPRINTF("segment %d addr %p len %zd\n", i,
1420ee7f31c0SAlexander Motin 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1421ee7f31c0SAlexander Motin 
1422ee7f31c0SAlexander Motin 		beio->num_segs++;
1423ee7f31c0SAlexander Motin 		len_left -= seglen;
1424ee7f31c0SAlexander Motin 
1425ee7f31c0SAlexander Motin 		buf = beio->sg_segs[i].addr;
1426ee7f31c0SAlexander Motin 		end = buf + seglen;
14270bcd4ab6SAlexander Motin 		for (; buf < end; buf += cbe_lun->blocksize) {
14286c2acea5SAlexander Motin 			if (lbalen->flags & SWS_NDOB) {
14296c2acea5SAlexander Motin 				memset(buf, 0, cbe_lun->blocksize);
14306c2acea5SAlexander Motin 			} else {
14316c2acea5SAlexander Motin 				memcpy(buf, io->scsiio.kern_data_ptr,
14326c2acea5SAlexander Motin 				    cbe_lun->blocksize);
14336c2acea5SAlexander Motin 			}
143466df9136SAlexander Motin 			if (lbalen->flags & SWS_LBDATA)
143566df9136SAlexander Motin 				scsi_ulto4b(lbalen->lba + lba, buf);
1436ee7f31c0SAlexander Motin 			lba++;
1437ee7f31c0SAlexander Motin 		}
1438ee7f31c0SAlexander Motin 	}
1439ee7f31c0SAlexander Motin 
14400bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
14410bcd4ab6SAlexander Motin 	beio->io_len = lba * cbe_lun->blocksize;
1442ee7f31c0SAlexander Motin 
1443ee7f31c0SAlexander Motin 	/* We can not do all in one run. Correct and schedule rerun. */
1444ee7f31c0SAlexander Motin 	if (len_left > 0) {
144566df9136SAlexander Motin 		lbalen->lba += lba;
144666df9136SAlexander Motin 		lbalen->len -= lba;
1447ee7f31c0SAlexander Motin 		beio->beio_cont = ctl_be_block_cw_done_ws;
1448ee7f31c0SAlexander Motin 	}
1449ee7f31c0SAlexander Motin 
1450ee7f31c0SAlexander Motin 	be_lun->dispatch(be_lun, beio);
1451ee7f31c0SAlexander Motin }
1452ee7f31c0SAlexander Motin 
1453ee7f31c0SAlexander Motin static void
1454ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1455ee7f31c0SAlexander Motin 			    union ctl_io *io)
1456ee7f31c0SAlexander Motin {
1457ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
145866df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1459ee7f31c0SAlexander Motin 
1460ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1461ee7f31c0SAlexander Motin 
1462e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
146366df9136SAlexander Motin 	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1464ee7f31c0SAlexander Motin 
14653406a2a0SAlexander Motin 	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1466ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1467ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1468ee7f31c0SAlexander Motin 				      /*sks_valid*/ 0,
1469ee7f31c0SAlexander Motin 				      /*command*/ 1,
1470ee7f31c0SAlexander Motin 				      /*field*/ 0,
1471ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1472ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1473ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1474ee7f31c0SAlexander Motin 		return;
1475ee7f31c0SAlexander Motin 	}
1476ee7f31c0SAlexander Motin 
1477ee7f31c0SAlexander Motin 	beio->io_len = 0;
1478ee7f31c0SAlexander Motin 	beio->io_offset = -1;
1479ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_DELETE;
1480ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_FREE;
148166df9136SAlexander Motin 	DPRINTF("UNMAP\n");
1482ee7f31c0SAlexander Motin 	be_lun->unmap(be_lun, beio);
1483ee7f31c0SAlexander Motin }
1484ee7f31c0SAlexander Motin 
1485ee7f31c0SAlexander Motin static void
1486ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1487ef8daf3fSAlexander Motin {
1488ef8daf3fSAlexander Motin 	union ctl_io *io;
1489ef8daf3fSAlexander Motin 
1490ef8daf3fSAlexander Motin 	io = beio->io;
1491ef8daf3fSAlexander Motin 	ctl_free_beio(beio);
1492ef8daf3fSAlexander Motin 	ctl_config_read_done(io);
1493ef8daf3fSAlexander Motin }
1494ef8daf3fSAlexander Motin 
1495ef8daf3fSAlexander Motin static void
1496ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1497ef8daf3fSAlexander Motin 			 union ctl_io *io)
1498ef8daf3fSAlexander Motin {
1499ef8daf3fSAlexander Motin 	struct ctl_be_block_io *beio;
1500ef8daf3fSAlexander Motin 	struct ctl_be_block_softc *softc;
1501ef8daf3fSAlexander Motin 
1502ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1503ef8daf3fSAlexander Motin 
1504ef8daf3fSAlexander Motin 	softc = be_lun->softc;
1505ef8daf3fSAlexander Motin 	beio = ctl_alloc_beio(softc);
1506ef8daf3fSAlexander Motin 	beio->io = io;
1507ef8daf3fSAlexander Motin 	beio->lun = be_lun;
1508ef8daf3fSAlexander Motin 	beio->beio_cont = ctl_be_block_cr_done;
1509ef8daf3fSAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1510ef8daf3fSAlexander Motin 
1511ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
1512ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1513ef8daf3fSAlexander Motin 		beio->bio_cmd = -1;
1514ef8daf3fSAlexander Motin 		beio->ds_trans_type = DEVSTAT_NO_DATA;
1515ef8daf3fSAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1516ef8daf3fSAlexander Motin 		beio->io_len = 0;
1517ef8daf3fSAlexander Motin 		if (be_lun->get_lba_status)
1518ef8daf3fSAlexander Motin 			be_lun->get_lba_status(be_lun, beio);
1519ef8daf3fSAlexander Motin 		else
1520ef8daf3fSAlexander Motin 			ctl_be_block_cr_done(beio);
1521ef8daf3fSAlexander Motin 		break;
1522ef8daf3fSAlexander Motin 	default:
1523ef8daf3fSAlexander Motin 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1524ef8daf3fSAlexander Motin 		break;
1525ef8daf3fSAlexander Motin 	}
1526ef8daf3fSAlexander Motin }
1527ef8daf3fSAlexander Motin 
1528ef8daf3fSAlexander Motin static void
1529ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1530ee7f31c0SAlexander Motin {
1531ee7f31c0SAlexander Motin 	union ctl_io *io;
1532ee7f31c0SAlexander Motin 
1533ee7f31c0SAlexander Motin 	io = beio->io;
1534ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1535ee7f31c0SAlexander Motin 	ctl_config_write_done(io);
1536ee7f31c0SAlexander Motin }
1537ee7f31c0SAlexander Motin 
1538ee7f31c0SAlexander Motin static void
1539130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1540130f4520SKenneth D. Merry 			 union ctl_io *io)
1541130f4520SKenneth D. Merry {
1542130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1543130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1544130f4520SKenneth D. Merry 
1545130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1546130f4520SKenneth D. Merry 
1547130f4520SKenneth D. Merry 	softc = be_lun->softc;
1548130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1549130f4520SKenneth D. Merry 	beio->io = io;
1550130f4520SKenneth D. Merry 	beio->lun = be_lun;
1551ee7f31c0SAlexander Motin 	beio->beio_cont = ctl_be_block_cw_done;
15527d0d4342SAlexander Motin 	switch (io->scsiio.tag_type) {
15537d0d4342SAlexander Motin 	case CTL_TAG_ORDERED:
15547d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
15557d0d4342SAlexander Motin 		break;
15567d0d4342SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
15577d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
15587d0d4342SAlexander Motin 		break;
15597d0d4342SAlexander Motin 	case CTL_TAG_UNTAGGED:
15607d0d4342SAlexander Motin 	case CTL_TAG_SIMPLE:
15617d0d4342SAlexander Motin 	case CTL_TAG_ACA:
15627d0d4342SAlexander Motin 	default:
15637d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
15647d0d4342SAlexander Motin 		break;
15657d0d4342SAlexander Motin 	}
1566e86a4142SAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1567130f4520SKenneth D. Merry 
1568130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
1569130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
1570130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
15717d0d4342SAlexander Motin 		ctl_be_block_cw_dispatch_sync(be_lun, io);
1572130f4520SKenneth D. Merry 		break;
1573ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
1574ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
1575ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_ws(be_lun, io);
1576ee7f31c0SAlexander Motin 		break;
1577ee7f31c0SAlexander Motin 	case UNMAP:
1578ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1579ee7f31c0SAlexander Motin 		break;
1580130f4520SKenneth D. Merry 	default:
1581130f4520SKenneth D. Merry 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1582130f4520SKenneth D. Merry 		break;
1583130f4520SKenneth D. Merry 	}
1584130f4520SKenneth D. Merry }
1585130f4520SKenneth D. Merry 
158636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
158736160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
158836160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
158936160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1590130f4520SKenneth D. Merry 
1591130f4520SKenneth D. Merry static void
159208a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio)
159308a7cce5SAlexander Motin {
159408a7cce5SAlexander Motin 	struct ctl_be_block_lun *be_lun;
159508a7cce5SAlexander Motin 	union ctl_io *io;
159608a7cce5SAlexander Motin 
159708a7cce5SAlexander Motin 	io = beio->io;
159808a7cce5SAlexander Motin 	be_lun = beio->lun;
159908a7cce5SAlexander Motin 	ctl_free_beio(beio);
1600ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1601ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1602ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
160311b569f7SAlexander Motin 		ctl_data_submit_done(io);
160408a7cce5SAlexander Motin 		return;
160508a7cce5SAlexander Motin 	}
160608a7cce5SAlexander Motin 
160708a7cce5SAlexander Motin 	io->io_hdr.status &= ~CTL_STATUS_MASK;
160808a7cce5SAlexander Motin 	io->io_hdr.status |= CTL_STATUS_NONE;
160908a7cce5SAlexander Motin 
161075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
161108a7cce5SAlexander Motin 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
161275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
161308a7cce5SAlexander Motin 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
161408a7cce5SAlexander Motin }
161508a7cce5SAlexander Motin 
161608a7cce5SAlexander Motin static void
1617130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1618130f4520SKenneth D. Merry 			   union ctl_io *io)
1619130f4520SKenneth D. Merry {
16200bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1621130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1622130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
162311b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1624e86a4142SAlexander Motin 	struct ctl_ptr_len_flags *bptrlen;
1625e86a4142SAlexander Motin 	uint64_t len_left, lbas;
1626130f4520SKenneth D. Merry 	int i;
1627130f4520SKenneth D. Merry 
1628130f4520SKenneth D. Merry 	softc = be_lun->softc;
1629130f4520SKenneth D. Merry 
1630130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1631130f4520SKenneth D. Merry 
163211b569f7SAlexander Motin 	lbalen = ARGS(io);
163311b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
163436160958SMark Johnston 		SDT_PROBE0(cbb, , write, start);
163511b569f7SAlexander Motin 	} else {
163636160958SMark Johnston 		SDT_PROBE0(cbb, , read, start);
1637130f4520SKenneth D. Merry 	}
1638130f4520SKenneth D. Merry 
1639130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1640130f4520SKenneth D. Merry 	beio->io = io;
1641130f4520SKenneth D. Merry 	beio->lun = be_lun;
1642e86a4142SAlexander Motin 	bptrlen = PRIV(io);
1643e86a4142SAlexander Motin 	bptrlen->ptr = (void *)beio;
1644130f4520SKenneth D. Merry 
1645130f4520SKenneth D. Merry 	switch (io->scsiio.tag_type) {
1646130f4520SKenneth D. Merry 	case CTL_TAG_ORDERED:
1647130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1648130f4520SKenneth D. Merry 		break;
1649130f4520SKenneth D. Merry 	case CTL_TAG_HEAD_OF_QUEUE:
1650130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1651130f4520SKenneth D. Merry 		break;
1652130f4520SKenneth D. Merry 	case CTL_TAG_UNTAGGED:
1653130f4520SKenneth D. Merry 	case CTL_TAG_SIMPLE:
1654130f4520SKenneth D. Merry 	case CTL_TAG_ACA:
1655130f4520SKenneth D. Merry 	default:
1656130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1657130f4520SKenneth D. Merry 		break;
1658130f4520SKenneth D. Merry 	}
1659130f4520SKenneth D. Merry 
166011b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1661130f4520SKenneth D. Merry 		beio->bio_cmd = BIO_WRITE;
1662130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_WRITE;
166311b569f7SAlexander Motin 	} else {
166411b569f7SAlexander Motin 		beio->bio_cmd = BIO_READ;
166511b569f7SAlexander Motin 		beio->ds_trans_type = DEVSTAT_READ;
1666130f4520SKenneth D. Merry 	}
1667130f4520SKenneth D. Merry 
166808a7cce5SAlexander Motin 	DPRINTF("%s at LBA %jx len %u @%ju\n",
1669130f4520SKenneth D. Merry 	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1670e86a4142SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1671b06771aaSAlexander Motin 	lbas = CTLBLK_MAX_IO_SIZE;
16720d7fed74SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE) {
16730d7fed74SAlexander Motin 		beio->two_sglists = 1;
1674b06771aaSAlexander Motin 		lbas /= 2;
16750d7fed74SAlexander Motin 	}
16760bcd4ab6SAlexander Motin 	lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
16770bcd4ab6SAlexander Motin 	beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
16780bcd4ab6SAlexander Motin 	beio->io_len = lbas * cbe_lun->blocksize;
1679e86a4142SAlexander Motin 	bptrlen->len += lbas;
1680130f4520SKenneth D. Merry 
168108a7cce5SAlexander Motin 	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
168208a7cce5SAlexander Motin 		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
168308a7cce5SAlexander Motin 		    i, CTLBLK_MAX_SEGS));
1684130f4520SKenneth D. Merry 
1685130f4520SKenneth D. Merry 		/*
1686130f4520SKenneth D. Merry 		 * Setup the S/G entry for this chunk.
1687130f4520SKenneth D. Merry 		 */
16888054320eSAlexander Motin 		ctl_alloc_seg(softc, &beio->sg_segs[i],
1689cd853791SKonstantin Belousov 		    MIN(CTLBLK_MAX_SEG, len_left));
1690130f4520SKenneth D. Merry 
1691130f4520SKenneth D. Merry 		DPRINTF("segment %d addr %p len %zd\n", i,
1692130f4520SKenneth D. Merry 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1693130f4520SKenneth D. Merry 
169411b569f7SAlexander Motin 		/* Set up second segment for compare operation. */
16950d7fed74SAlexander Motin 		if (beio->two_sglists) {
16968054320eSAlexander Motin 			ctl_alloc_seg(softc,
16978054320eSAlexander Motin 			    &beio->sg_segs[i + CTLBLK_HALF_SEGS],
16988054320eSAlexander Motin 			    beio->sg_segs[i].len);
169911b569f7SAlexander Motin 		}
170011b569f7SAlexander Motin 
1701130f4520SKenneth D. Merry 		beio->num_segs++;
1702130f4520SKenneth D. Merry 		len_left -= beio->sg_segs[i].len;
1703130f4520SKenneth D. Merry 	}
1704e86a4142SAlexander Motin 	if (bptrlen->len < lbalen->len)
170508a7cce5SAlexander Motin 		beio->beio_cont = ctl_be_block_next;
170608a7cce5SAlexander Motin 	io->scsiio.be_move_done = ctl_be_block_move_done;
170711b569f7SAlexander Motin 	/* For compare we have separate S/G lists for read and datamove. */
17080d7fed74SAlexander Motin 	if (beio->two_sglists)
170911b569f7SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
171011b569f7SAlexander Motin 	else
171108a7cce5SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
171208a7cce5SAlexander Motin 	io->scsiio.kern_data_len = beio->io_len;
171308a7cce5SAlexander Motin 	io->scsiio.kern_sg_entries = beio->num_segs;
17149a4510acSAlexander Motin 	io->scsiio.kern_data_ref = ctl_refcnt_beio;
17159a4510acSAlexander Motin 	io->scsiio.kern_data_arg = beio;
1716b2221369SAlexander Motin 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1717130f4520SKenneth D. Merry 
1718130f4520SKenneth D. Merry 	/*
1719130f4520SKenneth D. Merry 	 * For the read case, we need to read the data into our buffers and
1720130f4520SKenneth D. Merry 	 * then we can send it back to the user.  For the write case, we
1721130f4520SKenneth D. Merry 	 * need to get the data from the user first.
1722130f4520SKenneth D. Merry 	 */
1723130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
172436160958SMark Johnston 		SDT_PROBE0(cbb, , read, alloc_done);
1725130f4520SKenneth D. Merry 		be_lun->dispatch(be_lun, beio);
1726130f4520SKenneth D. Merry 	} else {
172736160958SMark Johnston 		SDT_PROBE0(cbb, , write, alloc_done);
1728130f4520SKenneth D. Merry 		ctl_datamove(io);
1729130f4520SKenneth D. Merry 	}
1730130f4520SKenneth D. Merry }
1731130f4520SKenneth D. Merry 
1732130f4520SKenneth D. Merry static void
1733130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending)
1734130f4520SKenneth D. Merry {
1735ee4ad294SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1736ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1737130f4520SKenneth D. Merry 	union ctl_io *io;
1738130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1739130f4520SKenneth D. Merry 
1740ee4ad294SAlexander Motin 	DPRINTF("entered\n");
1741ee4ad294SAlexander Motin 	/*
1742ee4ad294SAlexander Motin 	 * Fetch and process I/Os from all queues.  If we detect LUN
1743648dfc1aSAlexander Motin 	 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1744ee4ad294SAlexander Motin 	 * so make response maximally opaque to not confuse initiator.
1745ee4ad294SAlexander Motin 	 */
1746ee4ad294SAlexander Motin 	for (;;) {
1747ee4ad294SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
1748ee4ad294SAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1749ee4ad294SAlexander Motin 		if (io != NULL) {
1750130f4520SKenneth D. Merry 			DPRINTF("datamove queue\n");
175105d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links);
175275c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1753e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1754648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1755ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1756ee4ad294SAlexander Motin 				ctl_complete_beio(beio);
17576ed39db2SAlexander Motin 				continue;
1758ee4ad294SAlexander Motin 			}
1759130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1760130f4520SKenneth D. Merry 			continue;
1761130f4520SKenneth D. Merry 		}
1762130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1763130f4520SKenneth D. Merry 		if (io != NULL) {
1764130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
176505d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links);
176675c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1767648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1768ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1769ee4ad294SAlexander Motin 				ctl_config_write_done(io);
17706ed39db2SAlexander Motin 				continue;
1771ee4ad294SAlexander Motin 			}
1772130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1773ef8daf3fSAlexander Motin 			continue;
1774ef8daf3fSAlexander Motin 		}
1775ef8daf3fSAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1776ef8daf3fSAlexander Motin 		if (io != NULL) {
1777ef8daf3fSAlexander Motin 			DPRINTF("config read queue\n");
177805d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links);
1779ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1780648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1781ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1782ee4ad294SAlexander Motin 				ctl_config_read_done(io);
17836ed39db2SAlexander Motin 				continue;
1784ee4ad294SAlexander Motin 			}
1785ef8daf3fSAlexander Motin 			ctl_be_block_cr_dispatch(be_lun, io);
1786130f4520SKenneth D. Merry 			continue;
1787130f4520SKenneth D. Merry 		}
1788130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1789130f4520SKenneth D. Merry 		if (io != NULL) {
1790130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
179105d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->input_queue, links);
179275c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1793648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1794ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1795ee4ad294SAlexander Motin 				ctl_data_submit_done(io);
17966ed39db2SAlexander Motin 				continue;
1797ee4ad294SAlexander Motin 			}
1798130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1799130f4520SKenneth D. Merry 			continue;
1800130f4520SKenneth D. Merry 		}
1801130f4520SKenneth D. Merry 
1802130f4520SKenneth D. Merry 		/*
1803130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1804130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1805130f4520SKenneth D. Merry 		 */
1806ee4ad294SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
1807130f4520SKenneth D. Merry 		break;
1808130f4520SKenneth D. Merry 	}
1809130f4520SKenneth D. Merry }
1810130f4520SKenneth D. Merry 
1811130f4520SKenneth D. Merry /*
1812130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1813130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1814130f4520SKenneth D. Merry  * thread.
1815130f4520SKenneth D. Merry  */
1816130f4520SKenneth D. Merry static int
1817130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1818130f4520SKenneth D. Merry {
1819130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1820130f4520SKenneth D. Merry 
1821130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1822130f4520SKenneth D. Merry 
1823767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1824130f4520SKenneth D. Merry 
182505d882b7SAlexander Motin 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
182605d882b7SAlexander Motin 	    ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
1827130f4520SKenneth D. Merry 
1828e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1829e86a4142SAlexander Motin 
183075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
1831130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
183275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
1833130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1834130f4520SKenneth D. Merry 
18359c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1836130f4520SKenneth D. Merry }
1837130f4520SKenneth D. Merry 
1838130f4520SKenneth D. Merry static int
1839130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1840130f4520SKenneth D. Merry 			int flag, struct thread *td)
1841130f4520SKenneth D. Merry {
184234144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
1843130f4520SKenneth D. Merry 	int error;
1844130f4520SKenneth D. Merry 
1845130f4520SKenneth D. Merry 	error = 0;
1846130f4520SKenneth D. Merry 	switch (cmd) {
1847130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1848130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1849130f4520SKenneth D. Merry 
1850130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1851130f4520SKenneth D. Merry 
1852130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1853130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1854130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1855130f4520SKenneth D. Merry 			break;
1856130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1857130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1858130f4520SKenneth D. Merry 			break;
185981177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
186081177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
186181177295SEdward Tomasz Napierala 			break;
1862130f4520SKenneth D. Merry 		default:
1863130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1864130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
186519720f41SAlexander Motin 				 "invalid LUN request type %d",
1866130f4520SKenneth D. Merry 				 lun_req->reqtype);
1867130f4520SKenneth D. Merry 			break;
1868130f4520SKenneth D. Merry 		}
1869130f4520SKenneth D. Merry 		break;
1870130f4520SKenneth D. Merry 	}
1871130f4520SKenneth D. Merry 	default:
1872130f4520SKenneth D. Merry 		error = ENOTTY;
1873130f4520SKenneth D. Merry 		break;
1874130f4520SKenneth D. Merry 	}
1875130f4520SKenneth D. Merry 
1876130f4520SKenneth D. Merry 	return (error);
1877130f4520SKenneth D. Merry }
1878130f4520SKenneth D. Merry 
1879130f4520SKenneth D. Merry static int
1880130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1881130f4520SKenneth D. Merry {
18820bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1883130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1884130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
18858951f055SMarcelo Araujo 	const char		     *value;
1886130f4520SKenneth D. Merry 	struct vattr		      vattr;
188734961f40SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1888130f4520SKenneth D. Merry 	int			      error;
188949050613SKa Ho Ng 	long			      pconf;
1890130f4520SKenneth D. Merry 
18910bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
1892130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
189319720f41SAlexander Motin 	params = &be_lun->params;
1894130f4520SKenneth D. Merry 
1895130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1896130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1897130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1898ef8daf3fSAlexander Motin 	be_lun->get_lba_status = ctl_be_block_gls_file;
189953c146deSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_file;
190049050613SKa Ho Ng 	be_lun->unmap = ctl_be_block_unmap_file;
19010bcd4ab6SAlexander Motin 	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1902130f4520SKenneth D. Merry 
1903130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1904130f4520SKenneth D. Merry 	if (error != 0) {
1905130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1906130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1907130f4520SKenneth D. Merry 			 be_lun->dev_path);
1908130f4520SKenneth D. Merry 		return (error);
1909130f4520SKenneth D. Merry 	}
1910130f4520SKenneth D. Merry 
191149050613SKa Ho Ng 	error = VOP_PATHCONF(be_lun->vn, _PC_DEALLOC_PRESENT, &pconf);
191249050613SKa Ho Ng 	if (error != 0) {
191349050613SKa Ho Ng 		snprintf(req->error_str, sizeof(req->error_str),
191449050613SKa Ho Ng 		    "error calling VOP_PATHCONF() for file %s",
191549050613SKa Ho Ng 		    be_lun->dev_path);
191649050613SKa Ho Ng 		return (error);
191749050613SKa Ho Ng 	}
191849050613SKa Ho Ng 	if (pconf == 1)
191949050613SKa Ho Ng 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
192049050613SKa Ho Ng 
1921130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
192281177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
192381177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
192481177295SEdward Tomasz Napierala 	else
1925130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1926130f4520SKenneth D. Merry 
1927130f4520SKenneth D. Merry 	/*
192820a28d6cSAlexander Motin 	 * For files we can use any logical block size.  Prefer 512 bytes
192920a28d6cSAlexander Motin 	 * for compatibility reasons.  If file's vattr.va_blocksize
193020a28d6cSAlexander Motin 	 * (preferred I/O block size) is bigger and multiple to chosen
193120a28d6cSAlexander Motin 	 * logical block size -- report it as physical block size.
1932130f4520SKenneth D. Merry 	 */
1933130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
19340bcd4ab6SAlexander Motin 		cbe_lun->blocksize = params->blocksize_bytes;
193591be33dcSAlexander Motin 	else if (cbe_lun->lun_type == T_CDROM)
193691be33dcSAlexander Motin 		cbe_lun->blocksize = 2048;
1937130f4520SKenneth D. Merry 	else
19380bcd4ab6SAlexander Motin 		cbe_lun->blocksize = 512;
19390bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
19400bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
19410bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
194234961f40SAlexander Motin 
194334961f40SAlexander Motin 	us = ps = vattr.va_blocksize;
194434961f40SAlexander Motin 	uo = po = 0;
194534961f40SAlexander Motin 
19468951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
194734961f40SAlexander Motin 	if (value != NULL)
194834961f40SAlexander Motin 		ctl_expand_number(value, &ps);
19498951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
195034961f40SAlexander Motin 	if (value != NULL)
195134961f40SAlexander Motin 		ctl_expand_number(value, &po);
19520bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
19530bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
19540bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
19550bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
19560bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
19570bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
195834961f40SAlexander Motin 	}
195934961f40SAlexander Motin 
19608951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
196134961f40SAlexander Motin 	if (value != NULL)
196234961f40SAlexander Motin 		ctl_expand_number(value, &us);
19638951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
196434961f40SAlexander Motin 	if (value != NULL)
196534961f40SAlexander Motin 		ctl_expand_number(value, &uo);
19660bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
19670bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
19680bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
19690bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
19700bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
19710bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
197220a28d6cSAlexander Motin 	}
1973130f4520SKenneth D. Merry 
1974130f4520SKenneth D. Merry 	/*
1975130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1976130f4520SKenneth D. Merry 	 * sector long.
1977130f4520SKenneth D. Merry 	 */
19780bcd4ab6SAlexander Motin 	if (be_lun->size_bytes < cbe_lun->blocksize) {
1979130f4520SKenneth D. Merry 		error = EINVAL;
1980130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1981130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
19820bcd4ab6SAlexander Motin 			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1983130f4520SKenneth D. Merry 	}
1984cb8727e2SAlexander Motin 
19850bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1986130f4520SKenneth D. Merry 	return (error);
1987130f4520SKenneth D. Merry }
1988130f4520SKenneth D. Merry 
1989130f4520SKenneth D. Merry static int
1990130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1991130f4520SKenneth D. Merry {
19920bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1993130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
19943236151eSAlexander Motin 	struct cdevsw		     *csw;
1995130f4520SKenneth D. Merry 	struct cdev		     *dev;
19968951f055SMarcelo Araujo 	const char		     *value;
19973236151eSAlexander Motin 	int			      error, atomic, maxio, ref, unmap, tmp;
1998f6295033SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1999130f4520SKenneth D. Merry 
200019720f41SAlexander Motin 	params = &be_lun->params;
2001130f4520SKenneth D. Merry 
2002130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
20033236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
20043236151eSAlexander Motin 	if (csw == NULL)
20053236151eSAlexander Motin 		return (ENXIO);
20063236151eSAlexander Motin 	if (strcmp(csw->d_name, "zvol") == 0) {
200767f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_zvol;
2008ef8daf3fSAlexander Motin 		be_lun->get_lba_status = ctl_be_block_gls_zvol;
2009cb8727e2SAlexander Motin 		atomic = maxio = CTLBLK_MAX_IO_SIZE;
2010cb8727e2SAlexander Motin 	} else {
201167f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_dev;
20120bcd4ab6SAlexander Motin 		be_lun->get_lba_status = NULL;
2013cb8727e2SAlexander Motin 		atomic = 0;
20143236151eSAlexander Motin 		maxio = dev->si_iosize_max;
2015cb8727e2SAlexander Motin 		if (maxio <= 0)
2016cb8727e2SAlexander Motin 			maxio = DFLTPHYS;
20178054320eSAlexander Motin 		if (maxio > CTLBLK_MAX_SEG)
20188054320eSAlexander Motin 			maxio = CTLBLK_MAX_SEG;
2019cb8727e2SAlexander Motin 	}
202067f586a8SAlexander Motin 	be_lun->lun_flush = ctl_be_block_flush_dev;
2021c3e7ba3eSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_dev;
20220bcd4ab6SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
2023130f4520SKenneth D. Merry 
20243236151eSAlexander Motin 	if (!csw->d_ioctl) {
20253236151eSAlexander Motin 		dev_relthread(dev, ref);
2026130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
20273236151eSAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
2028130f4520SKenneth D. Merry 		return (ENODEV);
2029130f4520SKenneth D. Merry 	}
2030130f4520SKenneth D. Merry 
20313236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
2032130f4520SKenneth D. Merry 			       curthread);
2033130f4520SKenneth D. Merry 	if (error) {
20343236151eSAlexander Motin 		dev_relthread(dev, ref);
2035130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
203619720f41SAlexander Motin 			 "error %d returned for DIOCGSECTORSIZE ioctl "
203719720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
2038130f4520SKenneth D. Merry 		return (error);
2039130f4520SKenneth D. Merry 	}
2040130f4520SKenneth D. Merry 
2041130f4520SKenneth D. Merry 	/*
2042130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
2043130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
2044130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
2045130f4520SKenneth D. Merry 	 * device's blocksize.
2046130f4520SKenneth D. Merry 	 */
2047a15bbf15SAlexander Motin 	if ((params->blocksize_bytes != 0) &&
2048a15bbf15SAlexander Motin 	    (params->blocksize_bytes >= tmp)) {
2049a15bbf15SAlexander Motin 		if (params->blocksize_bytes % tmp == 0) {
20500bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
2051130f4520SKenneth D. Merry 		} else {
20523236151eSAlexander Motin 			dev_relthread(dev, ref);
2053130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
205419720f41SAlexander Motin 				 "requested blocksize %u is not an even "
2055130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
2056f6295033SAlexander Motin 				 params->blocksize_bytes, tmp);
2057130f4520SKenneth D. Merry 			return (EINVAL);
2058130f4520SKenneth D. Merry 		}
2059a15bbf15SAlexander Motin 	} else if (params->blocksize_bytes != 0) {
20603236151eSAlexander Motin 		dev_relthread(dev, ref);
2061130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
206219720f41SAlexander Motin 			 "requested blocksize %u < backing device "
2063f6295033SAlexander Motin 			 "blocksize %u", params->blocksize_bytes, tmp);
2064130f4520SKenneth D. Merry 		return (EINVAL);
206591be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_CDROM)
206691be33dcSAlexander Motin 		cbe_lun->blocksize = MAX(tmp, 2048);
206791be33dcSAlexander Motin 	else
20680bcd4ab6SAlexander Motin 		cbe_lun->blocksize = tmp;
2069130f4520SKenneth D. Merry 
20703236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
2071130f4520SKenneth D. Merry 			     curthread);
2072130f4520SKenneth D. Merry 	if (error) {
20733236151eSAlexander Motin 		dev_relthread(dev, ref);
2074130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
207519720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE "
207619720f41SAlexander Motin 			 " ioctl on %s!", error,
207781177295SEdward Tomasz Napierala 			 be_lun->dev_path);
2078130f4520SKenneth D. Merry 		return (error);
2079130f4520SKenneth D. Merry 	}
2080130f4520SKenneth D. Merry 
208181177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
2082f6295033SAlexander Motin 		if (params->lun_size_bytes > otmp) {
20833236151eSAlexander Motin 			dev_relthread(dev, ref);
208481177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
208519720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
208619720f41SAlexander Motin 				 "size %ju",
208781177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
2088f6295033SAlexander Motin 				 (uintmax_t)otmp);
208981177295SEdward Tomasz Napierala 			return (EINVAL);
2090130f4520SKenneth D. Merry 		}
2091130f4520SKenneth D. Merry 
209281177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
2093a15bbf15SAlexander Motin 	} else
2094f6295033SAlexander Motin 		be_lun->size_bytes = otmp;
20950bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
20960bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
20970bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
209881177295SEdward Tomasz Napierala 
20993236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
21003236151eSAlexander Motin 	    curthread);
2101f6012722SAlexander Motin 	if (error)
2102f6012722SAlexander Motin 		ps = po = 0;
2103f6012722SAlexander Motin 	else {
21043236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
21053236151eSAlexander Motin 		    FREAD, curthread);
2106f6012722SAlexander Motin 		if (error)
2107f6012722SAlexander Motin 			po = 0;
2108f6012722SAlexander Motin 	}
210934961f40SAlexander Motin 	us = ps;
211034961f40SAlexander Motin 	uo = po;
211134961f40SAlexander Motin 
21128951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
211334961f40SAlexander Motin 	if (value != NULL)
211434961f40SAlexander Motin 		ctl_expand_number(value, &ps);
21158951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
211634961f40SAlexander Motin 	if (value != NULL)
211734961f40SAlexander Motin 		ctl_expand_number(value, &po);
21180bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
21190bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
21200bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
21210bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
21220bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
21230bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
2124f6012722SAlexander Motin 	}
2125f6012722SAlexander Motin 
21268951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
212734961f40SAlexander Motin 	if (value != NULL)
212834961f40SAlexander Motin 		ctl_expand_number(value, &us);
21298951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
213034961f40SAlexander Motin 	if (value != NULL)
213134961f40SAlexander Motin 		ctl_expand_number(value, &uo);
21320bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
21330bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
21340bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
21350bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
21360bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
21370bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
213834961f40SAlexander Motin 	}
213934961f40SAlexander Motin 
21400bcd4ab6SAlexander Motin 	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
21410bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2142fbc8d4ffSAlexander Motin 
2143fbc8d4ffSAlexander Motin 	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2144fbc8d4ffSAlexander Motin 		unmap = 1;
2145fbc8d4ffSAlexander Motin 	} else {
2146fbc8d4ffSAlexander Motin 		struct diocgattr_arg	arg;
2147fbc8d4ffSAlexander Motin 
2148fbc8d4ffSAlexander Motin 		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2149fbc8d4ffSAlexander Motin 		arg.len = sizeof(arg.value.i);
21503236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
21513236151eSAlexander Motin 		    curthread);
2152fbc8d4ffSAlexander Motin 		unmap = (error == 0) ? arg.value.i : 0;
2153fbc8d4ffSAlexander Motin 	}
21548951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2155fbc8d4ffSAlexander Motin 	if (value != NULL)
2156fbc8d4ffSAlexander Motin 		unmap = (strcmp(value, "on") == 0);
2157fbc8d4ffSAlexander Motin 	if (unmap)
21580bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
21590bcd4ab6SAlexander Motin 	else
21600bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2161fbc8d4ffSAlexander Motin 
21623236151eSAlexander Motin 	dev_relthread(dev, ref);
216381177295SEdward Tomasz Napierala 	return (0);
216481177295SEdward Tomasz Napierala }
2165130f4520SKenneth D. Merry 
2166130f4520SKenneth D. Merry static int
2167130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2168130f4520SKenneth D. Merry {
21690bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
21700bcd4ab6SAlexander Motin 	int flags;
2171130f4520SKenneth D. Merry 
21720bcd4ab6SAlexander Motin 	if (be_lun->vn) {
21730bcd4ab6SAlexander Motin 		flags = FREAD;
21740bcd4ab6SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
21750bcd4ab6SAlexander Motin 			flags |= FWRITE;
2176130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2177130f4520SKenneth D. Merry 		be_lun->vn = NULL;
2178130f4520SKenneth D. Merry 
2179130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
2180130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
2181130f4520SKenneth D. Merry 			break;
2182130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
2183130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
2184130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
2185130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
2186130f4520SKenneth D. Merry 			}
2187130f4520SKenneth D. Merry 			break;
2188130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
2189025a2301SEdward Tomasz Napierala 			break;
2190130f4520SKenneth D. Merry 		default:
21915124012aSAlexander Motin 			panic("Unexpected backend type %d", be_lun->dev_type);
2192130f4520SKenneth D. Merry 			break;
2193130f4520SKenneth D. Merry 		}
219419720f41SAlexander Motin 		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2195130f4520SKenneth D. Merry 	}
2196130f4520SKenneth D. Merry 	return (0);
2197130f4520SKenneth D. Merry }
2198130f4520SKenneth D. Merry 
2199130f4520SKenneth D. Merry static int
2200648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2201130f4520SKenneth D. Merry {
22020bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2203130f4520SKenneth D. Merry 	struct nameidata nd;
22048951f055SMarcelo Araujo 	const char	*value;
22050bcd4ab6SAlexander Motin 	int		 error, flags;
2206130f4520SKenneth D. Merry 
2207130f4520SKenneth D. Merry 	error = 0;
2208130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
2209130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
221019720f41SAlexander Motin 			 "Root filesystem is not mounted");
2211130f4520SKenneth D. Merry 		return (1);
2212130f4520SKenneth D. Merry 	}
22138a08cec1SMateusz Guzik 	pwd_ensure_dirs();
2214130f4520SKenneth D. Merry 
22158951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "file", NULL);
22160bcd4ab6SAlexander Motin 	if (value == NULL) {
22170bcd4ab6SAlexander Motin 		snprintf(req->error_str, sizeof(req->error_str),
22180bcd4ab6SAlexander Motin 			 "no file argument specified");
22190bcd4ab6SAlexander Motin 		return (1);
22200bcd4ab6SAlexander Motin 	}
22210bcd4ab6SAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
22220bcd4ab6SAlexander Motin 	be_lun->dev_path = strdup(value, M_CTLBLK);
22230bcd4ab6SAlexander Motin 
22240bcd4ab6SAlexander Motin 	flags = FREAD;
22258951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
222691be33dcSAlexander Motin 	if (value != NULL) {
222791be33dcSAlexander Motin 		if (strcmp(value, "on") != 0)
222891be33dcSAlexander Motin 			flags |= FWRITE;
222991be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_DIRECT)
22300bcd4ab6SAlexander Motin 		flags |= FWRITE;
22310bcd4ab6SAlexander Motin 
2232130f4520SKenneth D. Merry again:
22337e1d3eefSMateusz Guzik 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path);
2234130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
22350bcd4ab6SAlexander Motin 	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
22360bcd4ab6SAlexander Motin 		flags &= ~FWRITE;
22370bcd4ab6SAlexander Motin 		goto again;
22380bcd4ab6SAlexander Motin 	}
2239130f4520SKenneth D. Merry 	if (error) {
2240130f4520SKenneth D. Merry 		/*
2241130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
2242130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
2243130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
2244130f4520SKenneth D. Merry 		 * full path.
2245130f4520SKenneth D. Merry 		 */
2246130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
2247130f4520SKenneth D. Merry 			char *dev_name;
2248130f4520SKenneth D. Merry 
22490bcd4ab6SAlexander Motin 			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2250130f4520SKenneth D. Merry 				be_lun->dev_path);
2251130f4520SKenneth D. Merry 			free(be_lun->dev_path, M_CTLBLK);
2252130f4520SKenneth D. Merry 			be_lun->dev_path = dev_name;
2253130f4520SKenneth D. Merry 			goto again;
2254130f4520SKenneth D. Merry 		}
2255130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
225619720f41SAlexander Motin 		    "error opening %s: %d", be_lun->dev_path, error);
2257130f4520SKenneth D. Merry 		return (error);
2258130f4520SKenneth D. Merry 	}
22590bcd4ab6SAlexander Motin 	if (flags & FWRITE)
22600bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
22610bcd4ab6SAlexander Motin 	else
22620bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2263130f4520SKenneth D. Merry 
2264*bb92cd7bSMateusz Guzik 	NDFREE_PNBUF(&nd);
2265130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
2266130f4520SKenneth D. Merry 
2267130f4520SKenneth D. Merry 	/* We only support disks and files. */
22687ad2a82dSMateusz Guzik 	if (vn_isdisk_error(be_lun->vn, &error)) {
2269130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
2270130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
2271130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
2272130f4520SKenneth D. Merry 	} else {
2273130f4520SKenneth D. Merry 		error = EINVAL;
2274130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2275025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
2276130f4520SKenneth D. Merry 	}
2277b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
2278130f4520SKenneth D. Merry 
2279a15bbf15SAlexander Motin 	if (error != 0)
2280130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
22810bcd4ab6SAlexander Motin 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
22820bcd4ab6SAlexander Motin 	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2283ac503c19SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
22848951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
22850bcd4ab6SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
22860bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
22870bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "read") == 0)
22880bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2289ac503c19SAlexander Motin 	else if (value != NULL && strcmp(value, "soft") == 0)
2290ac503c19SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
22910bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "off") == 0)
22920bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2293130f4520SKenneth D. Merry 	return (0);
2294130f4520SKenneth D. Merry }
2295130f4520SKenneth D. Merry 
2296130f4520SKenneth D. Merry static int
2297130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2298130f4520SKenneth D. Merry {
22990bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2300130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2301130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
230257a5db13SAlexander Motin 	char num_thread_str[16];
2303130f4520SKenneth D. Merry 	char tmpstr[32];
23048951f055SMarcelo Araujo 	const char *value;
2305fbc8d4ffSAlexander Motin 	int retval, num_threads;
230657a5db13SAlexander Motin 	int tmp_num_threads;
2307130f4520SKenneth D. Merry 
2308130f4520SKenneth D. Merry 	params = &req->reqdata.create;
2309130f4520SKenneth D. Merry 	retval = 0;
231019720f41SAlexander Motin 	req->status = CTL_LUN_OK;
2311130f4520SKenneth D. Merry 
2312130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
23130bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
231419720f41SAlexander Motin 	be_lun->params = req->reqdata.create;
2315130f4520SKenneth D. Merry 	be_lun->softc = softc;
2316130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
2317ef8daf3fSAlexander Motin 	STAILQ_INIT(&be_lun->config_read_queue);
2318130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
2319130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
232034144c2cSAlexander Motin 	mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
232134144c2cSAlexander Motin 	mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
23228951f055SMarcelo Araujo 	cbe_lun->options = nvlist_clone(req->args_nvl);
2323130f4520SKenneth D. Merry 
2324130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
23250bcd4ab6SAlexander Motin 		cbe_lun->lun_type = params->device_type;
2326130f4520SKenneth D. Merry 	else
23270bcd4ab6SAlexander Motin 		cbe_lun->lun_type = T_DIRECT;
232834144c2cSAlexander Motin 	be_lun->flags = 0;
23297ac58230SAlexander Motin 	cbe_lun->flags = 0;
23308951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
23317ac58230SAlexander Motin 	if (value != NULL) {
23327ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
23337ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
23347ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
23357ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2336130f4520SKenneth D. Merry 
233791be33dcSAlexander Motin 	if (cbe_lun->lun_type == T_DIRECT ||
233891be33dcSAlexander Motin 	    cbe_lun->lun_type == T_CDROM) {
2339a15bbf15SAlexander Motin 		be_lun->size_bytes = params->lun_size_bytes;
2340a15bbf15SAlexander Motin 		if (params->blocksize_bytes != 0)
23410bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
234291be33dcSAlexander Motin 		else if (cbe_lun->lun_type == T_CDROM)
234391be33dcSAlexander Motin 			cbe_lun->blocksize = 2048;
2344a15bbf15SAlexander Motin 		else
23450bcd4ab6SAlexander Motin 			cbe_lun->blocksize = 512;
23460bcd4ab6SAlexander Motin 		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
23470bcd4ab6SAlexander Motin 		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
23480bcd4ab6SAlexander Motin 		    0 : (be_lun->size_blocks - 1);
2349130f4520SKenneth D. Merry 
23507ac58230SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
23517ac58230SAlexander Motin 		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2352648dfc1aSAlexander Motin 			retval = ctl_be_block_open(be_lun, req);
2353130f4520SKenneth D. Merry 			if (retval != 0) {
2354130f4520SKenneth D. Merry 				retval = 0;
235519720f41SAlexander Motin 				req->status = CTL_LUN_WARNING;
2356130f4520SKenneth D. Merry 			}
23577ac58230SAlexander Motin 		}
23580bcd4ab6SAlexander Motin 		num_threads = cbb_num_threads;
2359130f4520SKenneth D. Merry 	} else {
2360130f4520SKenneth D. Merry 		num_threads = 1;
2361130f4520SKenneth D. Merry 	}
2362130f4520SKenneth D. Merry 
23638951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
236457a5db13SAlexander Motin 	if (value != NULL) {
236557a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
2366130f4520SKenneth D. Merry 
2367130f4520SKenneth D. Merry 		/*
2368130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
2369130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
2370130f4520SKenneth D. Merry 		 * specify 1000 threads.
2371130f4520SKenneth D. Merry 		 */
2372130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
2373130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
237419720f41SAlexander Motin 				 "invalid number of threads %s",
237519720f41SAlexander Motin 				 num_thread_str);
2376130f4520SKenneth D. Merry 			goto bailout_error;
2377130f4520SKenneth D. Merry 		}
2378130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
237957a5db13SAlexander Motin 	}
2380130f4520SKenneth D. Merry 
238119720f41SAlexander Motin 	if (be_lun->vn == NULL)
2382648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2383130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
238419720f41SAlexander Motin 	params->lun_size_bytes = be_lun->size_bytes;
23850bcd4ab6SAlexander Motin 	params->blocksize_bytes = cbe_lun->blocksize;
2386130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
23870bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = params->req_lun_id;
23880bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2389130f4520SKenneth D. Merry 	} else
23900bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = 0;
2391130f4520SKenneth D. Merry 
23920bcd4ab6SAlexander Motin 	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
23930bcd4ab6SAlexander Motin 	cbe_lun->be = &ctl_be_block_driver;
2394130f4520SKenneth D. Merry 
2395130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
239671cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2397130f4520SKenneth D. Merry 			 softc->num_luns);
23980bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, tmpstr,
23990bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2400130f4520SKenneth D. Merry 
2401130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
2402130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
2403e7038eb7SAlexander Motin 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2404130f4520SKenneth D. Merry 	} else {
24050bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
24060bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num),
2407130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
2408130f4520SKenneth D. Merry 	}
2409130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
241071cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
24110bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, tmpstr,
24120bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2413130f4520SKenneth D. Merry 
2414130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2415130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2416e7038eb7SAlexander Motin 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2417130f4520SKenneth D. Merry 	} else {
24180bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, params->device_id,
24190bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id),
2420130f4520SKenneth D. Merry 			    sizeof(params->device_id)));
2421130f4520SKenneth D. Merry 	}
2422130f4520SKenneth D. Merry 
2423130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2424130f4520SKenneth D. Merry 
242534144c2cSAlexander Motin 	be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2426130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2427130f4520SKenneth D. Merry 
2428130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2429130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
243019720f41SAlexander Motin 			 "unable to create taskqueue");
2431130f4520SKenneth D. Merry 		goto bailout_error;
2432130f4520SKenneth D. Merry 	}
2433130f4520SKenneth D. Merry 
2434130f4520SKenneth D. Merry 	/*
2435130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2436130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2437130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2438130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2439130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2440130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2441130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2442130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2443130f4520SKenneth D. Merry 	 *
2444130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2445130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2446130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2447130f4520SKenneth D. Merry 	 */
244812373e95SAlexander Motin 	retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2449130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2450053db1feSAlexander Motin 					 /*priority*/PUSER,
245112373e95SAlexander Motin 					 /*proc*/control_softc->ctl_proc,
245234144c2cSAlexander Motin 					 /*thread name*/"block");
2453130f4520SKenneth D. Merry 
2454130f4520SKenneth D. Merry 	if (retval != 0)
2455130f4520SKenneth D. Merry 		goto bailout_error;
2456130f4520SKenneth D. Merry 
2457130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2458130f4520SKenneth D. Merry 
24590bcd4ab6SAlexander Motin 	retval = ctl_add_lun(&be_lun->cbe_lun);
2460130f4520SKenneth D. Merry 	if (retval != 0) {
2461130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
246219720f41SAlexander Motin 			 "ctl_add_lun() returned error %d, see dmesg for "
246319720f41SAlexander Motin 			 "details", retval);
2464130f4520SKenneth D. Merry 		retval = 0;
2465130f4520SKenneth D. Merry 		goto bailout_error;
2466130f4520SKenneth D. Merry 	}
2467130f4520SKenneth D. Merry 
246834144c2cSAlexander Motin 	be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
24690bcd4ab6SAlexander Motin 					       cbe_lun->blocksize,
2470130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
24710bcd4ab6SAlexander Motin 					       cbe_lun->lun_type
2472130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2473130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2474130f4520SKenneth D. Merry 
247534144c2cSAlexander Motin 	mtx_lock(&softc->lock);
247634144c2cSAlexander Motin 	softc->num_luns++;
247734144c2cSAlexander Motin 	SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
247834144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
247934144c2cSAlexander Motin 
248034144c2cSAlexander Motin 	params->req_lun_id = cbe_lun->lun_id;
248134144c2cSAlexander Motin 
2482130f4520SKenneth D. Merry 	return (retval);
2483130f4520SKenneth D. Merry 
2484130f4520SKenneth D. Merry bailout_error:
2485130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2486130f4520SKenneth D. Merry 
24879e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
24889e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2489130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
24909e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2491130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
24928951f055SMarcelo Araujo 	nvlist_destroy(cbe_lun->options);
249375c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
249475c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2495130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2496130f4520SKenneth D. Merry 
2497130f4520SKenneth D. Merry 	return (retval);
2498130f4520SKenneth D. Merry }
2499130f4520SKenneth D. Merry 
2500130f4520SKenneth D. Merry static int
2501130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2502130f4520SKenneth D. Merry {
2503130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2504130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2505ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2506130f4520SKenneth D. Merry 	int retval;
2507130f4520SKenneth D. Merry 
2508130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2509130f4520SKenneth D. Merry 
251034144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
2511130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
251234144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
251334144c2cSAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id) {
251434144c2cSAlexander Motin 			SLIST_REMOVE(&softc->lun_list, be_lun,
251534144c2cSAlexander Motin 			    ctl_be_block_lun, links);
251634144c2cSAlexander Motin 			softc->num_luns--;
2517130f4520SKenneth D. Merry 			break;
2518130f4520SKenneth D. Merry 		}
251934144c2cSAlexander Motin 	}
2520130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
252134144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
2522130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2523130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
252419720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
252519720f41SAlexander Motin 			 params->lun_id);
2526130f4520SKenneth D. Merry 		goto bailout_error;
2527130f4520SKenneth D. Merry 	}
2528ee4ad294SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
2529130f4520SKenneth D. Merry 
2530ee4ad294SAlexander Motin 	if (be_lun->vn != NULL) {
2531648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2532648dfc1aSAlexander Motin 		ctl_lun_no_media(cbe_lun);
2533ee4ad294SAlexander Motin 		taskqueue_drain_all(be_lun->io_taskqueue);
2534ee4ad294SAlexander Motin 		ctl_be_block_close(be_lun);
2535ee4ad294SAlexander Motin 	}
2536ee4ad294SAlexander Motin 
253734144c2cSAlexander Motin 	mtx_lock(&softc->lock);
253834144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
253934144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
254034144c2cSAlexander Motin 
254134144c2cSAlexander Motin 	retval = ctl_remove_lun(cbe_lun);
2542130f4520SKenneth D. Merry 	if (retval != 0) {
2543130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
254434144c2cSAlexander Motin 			 "error %d returned from ctl_remove_lun() for "
254519720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
254634144c2cSAlexander Motin 		mtx_lock(&softc->lock);
254734144c2cSAlexander Motin 		be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
254834144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
2549130f4520SKenneth D. Merry 		goto bailout_error;
2550130f4520SKenneth D. Merry 	}
2551130f4520SKenneth D. Merry 
2552130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2553130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
255434144c2cSAlexander Motin 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2555130f4520SKenneth D. Merry 		if (retval == EINTR)
2556130f4520SKenneth D. Merry 			break;
2557130f4520SKenneth D. Merry 	}
2558130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
255934144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2560130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2561130f4520SKenneth D. Merry 		free(be_lun, M_CTLBLK);
256234144c2cSAlexander Motin 	} else {
256334144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
256434144c2cSAlexander Motin 		return (EINTR);
256534144c2cSAlexander Motin 	}
2566130f4520SKenneth D. Merry 
2567130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2568130f4520SKenneth D. Merry 	return (0);
2569130f4520SKenneth D. Merry 
2570130f4520SKenneth D. Merry bailout_error:
2571130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2572130f4520SKenneth D. Merry 	return (0);
2573130f4520SKenneth D. Merry }
2574130f4520SKenneth D. Merry 
257581177295SEdward Tomasz Napierala static int
257681177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
257781177295SEdward Tomasz Napierala {
257881177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
257981177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
2580a3977beaSAlexander Motin 	struct ctl_be_lun *cbe_lun;
25818951f055SMarcelo Araujo 	const char *value;
258271d8e97eSAlexander Motin 	uint64_t oldsize;
25837ac58230SAlexander Motin 	int error, wasprim;
258481177295SEdward Tomasz Napierala 
258581177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
258681177295SEdward Tomasz Napierala 
258734144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
258881177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
258934144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
25900bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
259181177295SEdward Tomasz Napierala 			break;
259281177295SEdward Tomasz Napierala 	}
259381177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
259481177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
259581177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
259619720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
259719720f41SAlexander Motin 			 params->lun_id);
259881177295SEdward Tomasz Napierala 		goto bailout_error;
259981177295SEdward Tomasz Napierala 	}
2600a3977beaSAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
260181177295SEdward Tomasz Napierala 
2602a3977beaSAlexander Motin 	if (params->lun_size_bytes != 0)
260319720f41SAlexander Motin 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
26048951f055SMarcelo Araujo 
2605efeedddcSAlexander Motin 	if (req->args_nvl != NULL) {
26068951f055SMarcelo Araujo 		nvlist_destroy(cbe_lun->options);
26078951f055SMarcelo Araujo 		cbe_lun->options = nvlist_clone(req->args_nvl);
2608efeedddcSAlexander Motin 	}
260981177295SEdward Tomasz Napierala 
26107ac58230SAlexander Motin 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
26118951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
26127ac58230SAlexander Motin 	if (value != NULL) {
26137ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
26147ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
26157ac58230SAlexander Motin 		else
26167ac58230SAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
26177ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
26187ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
26197ac58230SAlexander Motin 	else
26207ac58230SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
26217ac58230SAlexander Motin 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
26227ac58230SAlexander Motin 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
26237ac58230SAlexander Motin 			ctl_lun_primary(cbe_lun);
26247ac58230SAlexander Motin 		else
26257ac58230SAlexander Motin 			ctl_lun_secondary(cbe_lun);
26267ac58230SAlexander Motin 	}
26277ac58230SAlexander Motin 
26280bcd4ab6SAlexander Motin 	oldsize = be_lun->size_blocks;
26297ac58230SAlexander Motin 	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
26307ac58230SAlexander Motin 	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
263119720f41SAlexander Motin 		if (be_lun->vn == NULL)
2632648dfc1aSAlexander Motin 			error = ctl_be_block_open(be_lun, req);
26337ad2a82dSMateusz Guzik 		else if (vn_isdisk_error(be_lun->vn, &error))
26344ce7a086SAlexander Motin 			error = ctl_be_block_open_dev(be_lun, req);
26353d5cb709SAlexander Motin 		else if (be_lun->vn->v_type == VREG) {
26363d5cb709SAlexander Motin 			vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
26374ce7a086SAlexander Motin 			error = ctl_be_block_open_file(be_lun, req);
2638b249ce48SMateusz Guzik 			VOP_UNLOCK(be_lun->vn);
26393d5cb709SAlexander Motin 		} else
2640b9b4269cSAlexander Motin 			error = EINVAL;
2641648dfc1aSAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
26420bcd4ab6SAlexander Motin 		    be_lun->vn != NULL) {
2643648dfc1aSAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2644648dfc1aSAlexander Motin 			ctl_lun_has_media(cbe_lun);
2645648dfc1aSAlexander Motin 		} else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2646648dfc1aSAlexander Motin 		    be_lun->vn == NULL) {
2647648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2648648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
264971d8e97eSAlexander Motin 		}
2650648dfc1aSAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
26517ac58230SAlexander Motin 	} else {
26527ac58230SAlexander Motin 		if (be_lun->vn != NULL) {
2653648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2654648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
2655ee4ad294SAlexander Motin 			taskqueue_drain_all(be_lun->io_taskqueue);
26567ac58230SAlexander Motin 			error = ctl_be_block_close(be_lun);
26577ac58230SAlexander Motin 		} else
26587ac58230SAlexander Motin 			error = 0;
26597ac58230SAlexander Motin 	}
26607ac58230SAlexander Motin 	if (be_lun->size_blocks != oldsize)
26617ac58230SAlexander Motin 		ctl_lun_capacity_changed(cbe_lun);
266281177295SEdward Tomasz Napierala 
266381177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
266481177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
266581177295SEdward Tomasz Napierala 
266634144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
266719720f41SAlexander Motin 	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
266881177295SEdward Tomasz Napierala 	return (0);
266981177295SEdward Tomasz Napierala 
267081177295SEdward Tomasz Napierala bailout_error:
267134144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
267281177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
267381177295SEdward Tomasz Napierala 	return (0);
267481177295SEdward Tomasz Napierala }
267581177295SEdward Tomasz Napierala 
2676130f4520SKenneth D. Merry static void
2677767300e8SAlexander Motin ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2678130f4520SKenneth D. Merry {
2679767300e8SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
268034144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
268134144c2cSAlexander Motin 
268234144c2cSAlexander Motin 	taskqueue_drain_all(be_lun->io_taskqueue);
268334144c2cSAlexander Motin 	taskqueue_free(be_lun->io_taskqueue);
268434144c2cSAlexander Motin 	if (be_lun->disk_stats != NULL)
268534144c2cSAlexander Motin 		devstat_remove_entry(be_lun->disk_stats);
268634144c2cSAlexander Motin 	nvlist_destroy(be_lun->cbe_lun.options);
268734144c2cSAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
268834144c2cSAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
268934144c2cSAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2690130f4520SKenneth D. Merry 
2691130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
269234144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
269334144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
269434144c2cSAlexander Motin 		wakeup(be_lun);
269534144c2cSAlexander Motin 	else
269634144c2cSAlexander Motin 		free(be_lun, M_CTLBLK);
2697130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2698130f4520SKenneth D. Merry }
2699130f4520SKenneth D. Merry 
2700130f4520SKenneth D. Merry static int
2701130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2702130f4520SKenneth D. Merry {
2703130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
27040bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2705130f4520SKenneth D. Merry 	int retval;
2706130f4520SKenneth D. Merry 
2707130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2708130f4520SKenneth D. Merry 
27099cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
2710767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun;
2711130f4520SKenneth D. Merry 
271267cc546dSAlexander Motin 	retval = 0;
2713130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2714130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2715130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2716ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2717ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2718ee7f31c0SAlexander Motin 	case UNMAP:
2719130f4520SKenneth D. Merry 		/*
2720130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2721130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2722130f4520SKenneth D. Merry 		 *
2723130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2724130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2725130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2726130f4520SKenneth D. Merry 		 */
272775c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
2728130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2729130f4520SKenneth D. Merry 				   links);
273075c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
2731130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2732130f4520SKenneth D. Merry 		break;
2733130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2734130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2735648dfc1aSAlexander Motin 		struct ctl_lun_req req;
2736130f4520SKenneth D. Merry 
2737130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
273866b69676SAlexander Motin 		if ((cdb->how & SSS_PC_MASK) != 0) {
273966b69676SAlexander Motin 			ctl_set_success(&io->scsiio);
274066b69676SAlexander Motin 			ctl_config_write_done(io);
274166b69676SAlexander Motin 			break;
274266b69676SAlexander Motin 		}
2743648dfc1aSAlexander Motin 		if (cdb->how & SSS_START) {
2744648dfc1aSAlexander Motin 			if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2745648dfc1aSAlexander Motin 				retval = ctl_be_block_open(be_lun, &req);
2746648dfc1aSAlexander Motin 				cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2747648dfc1aSAlexander Motin 				if (retval == 0) {
2748648dfc1aSAlexander Motin 					cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2749648dfc1aSAlexander Motin 					ctl_lun_has_media(cbe_lun);
2750130f4520SKenneth D. Merry 				} else {
2751648dfc1aSAlexander Motin 					cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2752648dfc1aSAlexander Motin 					ctl_lun_no_media(cbe_lun);
2753130f4520SKenneth D. Merry 				}
2754648dfc1aSAlexander Motin 			}
2755648dfc1aSAlexander Motin 			ctl_start_lun(cbe_lun);
2756648dfc1aSAlexander Motin 		} else {
2757648dfc1aSAlexander Motin 			ctl_stop_lun(cbe_lun);
2758648dfc1aSAlexander Motin 			if (cdb->how & SSS_LOEJ) {
2759648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2760648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2761648dfc1aSAlexander Motin 				ctl_lun_ejected(cbe_lun);
2762648dfc1aSAlexander Motin 				if (be_lun->vn != NULL)
2763648dfc1aSAlexander Motin 					ctl_be_block_close(be_lun);
2764648dfc1aSAlexander Motin 			}
2765648dfc1aSAlexander Motin 		}
2766648dfc1aSAlexander Motin 
2767648dfc1aSAlexander Motin 		ctl_set_success(&io->scsiio);
2768130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2769130f4520SKenneth D. Merry 		break;
2770130f4520SKenneth D. Merry 	}
277191be33dcSAlexander Motin 	case PREVENT_ALLOW:
277291be33dcSAlexander Motin 		ctl_set_success(&io->scsiio);
277391be33dcSAlexander Motin 		ctl_config_write_done(io);
277491be33dcSAlexander Motin 		break;
2775130f4520SKenneth D. Merry 	default:
2776130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2777130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2778130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2779130f4520SKenneth D. Merry 		break;
2780130f4520SKenneth D. Merry 	}
2781130f4520SKenneth D. Merry 
2782130f4520SKenneth D. Merry 	return (retval);
2783130f4520SKenneth D. Merry }
2784130f4520SKenneth D. Merry 
2785130f4520SKenneth D. Merry static int
2786130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2787130f4520SKenneth D. Merry {
2788ef8daf3fSAlexander Motin 	struct ctl_be_block_lun *be_lun;
2789ef8daf3fSAlexander Motin 	int retval = 0;
2790ef8daf3fSAlexander Motin 
2791ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
2792ef8daf3fSAlexander Motin 
2793767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2794ef8daf3fSAlexander Motin 
2795ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
2796ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:
2797ef8daf3fSAlexander Motin 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2798ef8daf3fSAlexander Motin 			mtx_lock(&be_lun->queue_lock);
2799ef8daf3fSAlexander Motin 			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2800ef8daf3fSAlexander Motin 			    &io->io_hdr, links);
2801ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
2802ef8daf3fSAlexander Motin 			taskqueue_enqueue(be_lun->io_taskqueue,
2803ef8daf3fSAlexander Motin 			    &be_lun->io_task);
2804ef8daf3fSAlexander Motin 			retval = CTL_RETVAL_QUEUED;
2805ef8daf3fSAlexander Motin 			break;
2806ef8daf3fSAlexander Motin 		}
2807ef8daf3fSAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
2808ef8daf3fSAlexander Motin 				      /*sks_valid*/ 1,
2809ef8daf3fSAlexander Motin 				      /*command*/ 1,
2810ef8daf3fSAlexander Motin 				      /*field*/ 1,
2811ef8daf3fSAlexander Motin 				      /*bit_valid*/ 1,
2812ef8daf3fSAlexander Motin 				      /*bit*/ 4);
2813ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2814ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2815ef8daf3fSAlexander Motin 		break;
2816ef8daf3fSAlexander Motin 	default:
2817ef8daf3fSAlexander Motin 		ctl_set_invalid_opcode(&io->scsiio);
2818ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2819ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2820ef8daf3fSAlexander Motin 		break;
2821ef8daf3fSAlexander Motin 	}
2822ef8daf3fSAlexander Motin 
2823ef8daf3fSAlexander Motin 	return (retval);
2824130f4520SKenneth D. Merry }
2825130f4520SKenneth D. Merry 
2826130f4520SKenneth D. Merry static int
2827767300e8SAlexander Motin ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2828130f4520SKenneth D. Merry {
2829767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2830130f4520SKenneth D. Merry 	int retval;
2831130f4520SKenneth D. Merry 
28322cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "\t<num_threads>");
2833130f4520SKenneth D. Merry 	if (retval != 0)
2834130f4520SKenneth D. Merry 		goto bailout;
2835130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2836130f4520SKenneth D. Merry 	if (retval != 0)
2837130f4520SKenneth D. Merry 		goto bailout;
28382cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "</num_threads>\n");
2839130f4520SKenneth D. Merry 
2840130f4520SKenneth D. Merry bailout:
2841130f4520SKenneth D. Merry 	return (retval);
2842130f4520SKenneth D. Merry }
2843130f4520SKenneth D. Merry 
2844c3e7ba3eSAlexander Motin static uint64_t
2845767300e8SAlexander Motin ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2846c3e7ba3eSAlexander Motin {
2847767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2848c3e7ba3eSAlexander Motin 
2849c3e7ba3eSAlexander Motin 	if (lun->getattr == NULL)
2850c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
2851c3e7ba3eSAlexander Motin 	return (lun->getattr(lun, attrname));
2852c3e7ba3eSAlexander Motin }
2853c3e7ba3eSAlexander Motin 
28540c629e28SAlexander Motin static int
2855130f4520SKenneth D. Merry ctl_be_block_init(void)
2856130f4520SKenneth D. Merry {
28570c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
2858130f4520SKenneth D. Merry 
285934144c2cSAlexander Motin 	sx_init(&softc->modify_lock, "ctlblock modify");
286075c7a1d3SAlexander Motin 	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
28610c629e28SAlexander Motin 	softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2862a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2863cd853791SKonstantin Belousov 	softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG,
28640d7fed74SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2865cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2866cd853791SKonstantin Belousov 		softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG,
28678054320eSAlexander Motin 		    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
286834144c2cSAlexander Motin 	SLIST_INIT(&softc->lun_list);
28690c629e28SAlexander Motin 	return (0);
28700c629e28SAlexander Motin }
2871130f4520SKenneth D. Merry 
28720c629e28SAlexander Motin static int
28730c629e28SAlexander Motin ctl_be_block_shutdown(void)
28740c629e28SAlexander Motin {
28750c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
287634144c2cSAlexander Motin 	struct ctl_be_block_lun *lun;
28770c629e28SAlexander Motin 
28780c629e28SAlexander Motin 	mtx_lock(&softc->lock);
287934144c2cSAlexander Motin 	while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
288034144c2cSAlexander Motin 		SLIST_REMOVE_HEAD(&softc->lun_list, links);
288134144c2cSAlexander Motin 		softc->num_luns--;
28820c629e28SAlexander Motin 		/*
288334144c2cSAlexander Motin 		 * Drop our lock here.  Since ctl_remove_lun() can call
28840c629e28SAlexander Motin 		 * back into us, this could potentially lead to a recursive
28850c629e28SAlexander Motin 		 * lock of the same mutex, which would cause a hang.
28860c629e28SAlexander Motin 		 */
28870c629e28SAlexander Motin 		mtx_unlock(&softc->lock);
288834144c2cSAlexander Motin 		ctl_remove_lun(&lun->cbe_lun);
28890c629e28SAlexander Motin 		mtx_lock(&softc->lock);
28900c629e28SAlexander Motin 	}
28910c629e28SAlexander Motin 	mtx_unlock(&softc->lock);
2892cd853791SKonstantin Belousov 	uma_zdestroy(softc->bufmin_zone);
2893cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2894cd853791SKonstantin Belousov 		uma_zdestroy(softc->bufmax_zone);
28950c629e28SAlexander Motin 	uma_zdestroy(softc->beio_zone);
28960c629e28SAlexander Motin 	mtx_destroy(&softc->lock);
289734144c2cSAlexander Motin 	sx_destroy(&softc->modify_lock);
28980c629e28SAlexander Motin 	return (0);
2899130f4520SKenneth D. Merry }
2900