xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision 519b24f02968da4930d859288c2f5263c1ad17e1)
1130f4520SKenneth D. Merry /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
49130f4520SKenneth D. Merry #include <sys/systm.h>
50130f4520SKenneth D. Merry #include <sys/kernel.h>
51130f4520SKenneth D. Merry #include <sys/types.h>
52130f4520SKenneth D. Merry #include <sys/kthread.h>
53130f4520SKenneth D. Merry #include <sys/bio.h>
54130f4520SKenneth D. Merry #include <sys/fcntl.h>
55ee7f31c0SAlexander Motin #include <sys/limits.h>
56130f4520SKenneth D. Merry #include <sys/lock.h>
57130f4520SKenneth D. Merry #include <sys/mutex.h>
58130f4520SKenneth D. Merry #include <sys/condvar.h>
59130f4520SKenneth D. Merry #include <sys/malloc.h>
60130f4520SKenneth D. Merry #include <sys/conf.h>
61130f4520SKenneth D. Merry #include <sys/ioccom.h>
62130f4520SKenneth D. Merry #include <sys/queue.h>
63130f4520SKenneth D. Merry #include <sys/sbuf.h>
64130f4520SKenneth D. Merry #include <sys/endian.h>
65130f4520SKenneth D. Merry #include <sys/uio.h>
66130f4520SKenneth D. Merry #include <sys/buf.h>
67130f4520SKenneth D. Merry #include <sys/taskqueue.h>
68130f4520SKenneth D. Merry #include <sys/vnode.h>
69130f4520SKenneth D. Merry #include <sys/namei.h>
70130f4520SKenneth D. Merry #include <sys/mount.h>
71130f4520SKenneth D. Merry #include <sys/disk.h>
72130f4520SKenneth D. Merry #include <sys/fcntl.h>
73130f4520SKenneth D. Merry #include <sys/filedesc.h>
74ef8daf3fSAlexander Motin #include <sys/filio.h>
75130f4520SKenneth D. Merry #include <sys/proc.h>
76130f4520SKenneth D. Merry #include <sys/pcpu.h>
77130f4520SKenneth D. Merry #include <sys/module.h>
78130f4520SKenneth D. Merry #include <sys/sdt.h>
79130f4520SKenneth D. Merry #include <sys/devicestat.h>
80130f4520SKenneth D. Merry #include <sys/sysctl.h>
818951f055SMarcelo Araujo #include <sys/nv.h>
828951f055SMarcelo Araujo #include <sys/dnv.h>
8334144c2cSAlexander Motin #include <sys/sx.h>
8449050613SKa Ho Ng #include <sys/unistd.h>
85130f4520SKenneth D. Merry 
86130f4520SKenneth D. Merry #include <geom/geom.h>
87130f4520SKenneth D. Merry 
88130f4520SKenneth D. Merry #include <cam/cam.h>
89130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h>
90130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h>
91130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h>
92130f4520SKenneth D. Merry #include <cam/ctl/ctl.h>
93130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h>
94130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h>
957ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h>
96130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h>
977ac58230SAlexander Motin #include <cam/ctl/ctl_private.h>
98130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h>
99130f4520SKenneth D. Merry 
100130f4520SKenneth D. Merry /*
101b06771aaSAlexander Motin  * The idea here is to allocate enough S/G space to handle at least 1MB I/Os.
102b06771aaSAlexander Motin  * On systems with small maxphys it can be 8 128KB segments.  On large systems
103b06771aaSAlexander Motin  * it can be up to 8 1MB segments.  I/Os larger than that we'll split.
104130f4520SKenneth D. Merry  */
105b06771aaSAlexander Motin #define	CTLBLK_MAX_SEGS		8
106b06771aaSAlexander Motin #define	CTLBLK_HALF_SEGS	(CTLBLK_MAX_SEGS / 2)
107cd853791SKonstantin Belousov #define	CTLBLK_MIN_SEG		(128 * 1024)
108b06771aaSAlexander Motin #define	CTLBLK_MAX_SEG		MIN(1024 * 1024, MAX(CTLBLK_MIN_SEG, maxphys))
109b06771aaSAlexander Motin #define	CTLBLK_MAX_IO_SIZE	(CTLBLK_MAX_SEG * CTLBLK_MAX_SEGS)
110130f4520SKenneth D. Merry 
111130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG
112130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \
113130f4520SKenneth D. Merry     printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
114130f4520SKenneth D. Merry #else
115130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0)
116130f4520SKenneth D. Merry #endif
117130f4520SKenneth D. Merry 
118e86a4142SAlexander Motin #define PRIV(io)	\
119e86a4142SAlexander Motin     ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
12011b569f7SAlexander Motin #define ARGS(io)	\
12111b569f7SAlexander Motin     ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
122e86a4142SAlexander Motin 
123130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb);
124130f4520SKenneth D. Merry 
125130f4520SKenneth D. Merry typedef enum {
126130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
127130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
128130f4520SKenneth D. Merry } ctl_be_block_lun_flags;
129130f4520SKenneth D. Merry 
130130f4520SKenneth D. Merry typedef enum {
131130f4520SKenneth D. Merry 	CTL_BE_BLOCK_NONE,
132130f4520SKenneth D. Merry 	CTL_BE_BLOCK_DEV,
133130f4520SKenneth D. Merry 	CTL_BE_BLOCK_FILE
134130f4520SKenneth D. Merry } ctl_be_block_type;
135130f4520SKenneth D. Merry 
136130f4520SKenneth D. Merry struct ctl_be_block_filedata {
137130f4520SKenneth D. Merry 	struct ucred *cred;
138130f4520SKenneth D. Merry };
139130f4520SKenneth D. Merry 
140130f4520SKenneth D. Merry union ctl_be_block_bedata {
141130f4520SKenneth D. Merry 	struct ctl_be_block_filedata file;
142130f4520SKenneth D. Merry };
143130f4520SKenneth D. Merry 
144130f4520SKenneth D. Merry struct ctl_be_block_io;
145130f4520SKenneth D. Merry struct ctl_be_block_lun;
146130f4520SKenneth D. Merry 
147130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
148130f4520SKenneth D. Merry 			       struct ctl_be_block_io *beio);
149c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
150c3e7ba3eSAlexander Motin 				  const char *attrname);
151130f4520SKenneth D. Merry 
152130f4520SKenneth D. Merry /*
153130f4520SKenneth D. Merry  * Backend LUN structure.  There is a 1:1 mapping between a block device
154130f4520SKenneth D. Merry  * and a backend block LUN, and between a backend block LUN and a CTL LUN.
155130f4520SKenneth D. Merry  */
156130f4520SKenneth D. Merry struct ctl_be_block_lun {
157767300e8SAlexander Motin 	struct ctl_be_lun cbe_lun;		/* Must be first element. */
15819720f41SAlexander Motin 	struct ctl_lun_create_params params;
159130f4520SKenneth D. Merry 	char *dev_path;
160130f4520SKenneth D. Merry 	ctl_be_block_type dev_type;
161130f4520SKenneth D. Merry 	struct vnode *vn;
162130f4520SKenneth D. Merry 	union ctl_be_block_bedata backend;
163130f4520SKenneth D. Merry 	cbb_dispatch_t dispatch;
164130f4520SKenneth D. Merry 	cbb_dispatch_t lun_flush;
165ee7f31c0SAlexander Motin 	cbb_dispatch_t unmap;
166ef8daf3fSAlexander Motin 	cbb_dispatch_t get_lba_status;
167c3e7ba3eSAlexander Motin 	cbb_getattr_t getattr;
168130f4520SKenneth D. Merry 	uint64_t size_blocks;
169130f4520SKenneth D. Merry 	uint64_t size_bytes;
170130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
171130f4520SKenneth D. Merry 	struct devstat *disk_stats;
172130f4520SKenneth D. Merry 	ctl_be_block_lun_flags flags;
17334144c2cSAlexander Motin 	SLIST_ENTRY(ctl_be_block_lun) links;
174130f4520SKenneth D. Merry 	struct taskqueue *io_taskqueue;
175130f4520SKenneth D. Merry 	struct task io_task;
176130f4520SKenneth D. Merry 	int num_threads;
177130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) input_queue;
178ef8daf3fSAlexander Motin 	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
179130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
180130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
18175c7a1d3SAlexander Motin 	struct mtx_padalign io_lock;
18275c7a1d3SAlexander Motin 	struct mtx_padalign queue_lock;
183130f4520SKenneth D. Merry };
184130f4520SKenneth D. Merry 
185130f4520SKenneth D. Merry /*
186130f4520SKenneth D. Merry  * Overall softc structure for the block backend module.
187130f4520SKenneth D. Merry  */
188130f4520SKenneth D. Merry struct ctl_be_block_softc {
18934144c2cSAlexander Motin 	struct sx			 modify_lock;
190130f4520SKenneth D. Merry 	struct mtx			 lock;
191130f4520SKenneth D. Merry 	int				 num_luns;
19234144c2cSAlexander Motin 	SLIST_HEAD(, ctl_be_block_lun)	 lun_list;
1930d7fed74SAlexander Motin 	uma_zone_t			 beio_zone;
194cd853791SKonstantin Belousov 	uma_zone_t			 bufmin_zone;
195cd853791SKonstantin Belousov 	uma_zone_t			 bufmax_zone;
196130f4520SKenneth D. Merry };
197130f4520SKenneth D. Merry 
198130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc;
199130f4520SKenneth D. Merry 
200130f4520SKenneth D. Merry /*
201130f4520SKenneth D. Merry  * Per-I/O information.
202130f4520SKenneth D. Merry  */
203130f4520SKenneth D. Merry struct ctl_be_block_io {
204130f4520SKenneth D. Merry 	union ctl_io			*io;
205130f4520SKenneth D. Merry 	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
206130f4520SKenneth D. Merry 	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
2079a4510acSAlexander Motin 	int				refcnt;
208130f4520SKenneth D. Merry 	int				bio_cmd;
2090d7fed74SAlexander Motin 	int				two_sglists;
210130f4520SKenneth D. Merry 	int				num_segs;
211130f4520SKenneth D. Merry 	int				num_bios_sent;
212130f4520SKenneth D. Merry 	int				num_bios_done;
213130f4520SKenneth D. Merry 	int				send_complete;
2141f0694a6SAlexander Motin 	int				first_error;
2151f0694a6SAlexander Motin 	uint64_t			first_error_offset;
216130f4520SKenneth D. Merry 	struct bintime			ds_t0;
217130f4520SKenneth D. Merry 	devstat_tag_type		ds_tag_type;
218130f4520SKenneth D. Merry 	devstat_trans_flags		ds_trans_type;
219130f4520SKenneth D. Merry 	uint64_t			io_len;
220130f4520SKenneth D. Merry 	uint64_t			io_offset;
2217d0d4342SAlexander Motin 	int				io_arg;
222130f4520SKenneth D. Merry 	struct ctl_be_block_softc	*softc;
223130f4520SKenneth D. Merry 	struct ctl_be_block_lun		*lun;
224ee7f31c0SAlexander Motin 	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
225130f4520SKenneth D. Merry };
226130f4520SKenneth D. Merry 
2277ac58230SAlexander Motin extern struct ctl_softc *control_softc;
2287ac58230SAlexander Motin 
2297d4c4443SAlexander Motin static int cbb_num_threads = 32;
2307029da5cSPawel Biernacki SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
231130f4520SKenneth D. Merry 	    "CAM Target Layer Block Backend");
232af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
233130f4520SKenneth D. Merry            &cbb_num_threads, 0, "Number of threads per backing file");
234130f4520SKenneth D. Merry 
235130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
236130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio);
237130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio);
2382c7dc6baSAlexander Motin static int ctl_be_block_move_done(union ctl_io *io, bool samethr);
239130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio);
240130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
241130f4520SKenneth D. Merry 				    struct ctl_be_block_io *beio);
242130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
243130f4520SKenneth D. Merry 				       struct ctl_be_block_io *beio);
244ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
245ef8daf3fSAlexander Motin 				  struct ctl_be_block_io *beio);
24653c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
24753c146deSAlexander Motin 					 const char *attrname);
24849050613SKa Ho Ng static void ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
24949050613SKa Ho Ng 				    struct ctl_be_block_io *beio);
250130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
251130f4520SKenneth D. Merry 				   struct ctl_be_block_io *beio);
252ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
253ee7f31c0SAlexander Motin 				   struct ctl_be_block_io *beio);
254130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
255130f4520SKenneth D. Merry 				      struct ctl_be_block_io *beio);
256c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
257c3e7ba3eSAlexander Motin 					 const char *attrname);
258ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
259ef8daf3fSAlexander Motin 				    union ctl_io *io);
260130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
261130f4520SKenneth D. Merry 				    union ctl_io *io);
262130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
263130f4520SKenneth D. Merry 				  union ctl_io *io);
264130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending);
265130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io);
266130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
267130f4520SKenneth D. Merry 				   int flag, struct thread *td);
268130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
269130f4520SKenneth D. Merry 				  struct ctl_lun_req *req);
270130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
271130f4520SKenneth D. Merry 				 struct ctl_lun_req *req);
272130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
273648dfc1aSAlexander Motin static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
274130f4520SKenneth D. Merry 			     struct ctl_lun_req *req);
275130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc,
276130f4520SKenneth D. Merry 			       struct ctl_lun_req *req);
277130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
278130f4520SKenneth D. Merry 			   struct ctl_lun_req *req);
27981177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
28081177295SEdward Tomasz Napierala 			   struct ctl_lun_req *req);
281767300e8SAlexander Motin static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun);
282130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io);
283130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io);
284767300e8SAlexander Motin static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb);
285767300e8SAlexander Motin static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname);
2860c629e28SAlexander Motin static int ctl_be_block_init(void);
2870c629e28SAlexander Motin static int ctl_be_block_shutdown(void);
288130f4520SKenneth D. Merry 
289130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver =
290130f4520SKenneth D. Merry {
2912a2443d8SKenneth D. Merry 	.name = "block",
2922a2443d8SKenneth D. Merry 	.flags = CTL_BE_FLAG_HAS_CONFIG,
2932a2443d8SKenneth D. Merry 	.init = ctl_be_block_init,
2940c629e28SAlexander Motin 	.shutdown = ctl_be_block_shutdown,
2952a2443d8SKenneth D. Merry 	.data_submit = ctl_be_block_submit,
2962a2443d8SKenneth D. Merry 	.config_read = ctl_be_block_config_read,
2972a2443d8SKenneth D. Merry 	.config_write = ctl_be_block_config_write,
2982a2443d8SKenneth D. Merry 	.ioctl = ctl_be_block_ioctl,
299c3e7ba3eSAlexander Motin 	.lun_info = ctl_be_block_lun_info,
300c3e7ba3eSAlexander Motin 	.lun_attr = ctl_be_block_lun_attr
301130f4520SKenneth D. Merry };
302130f4520SKenneth D. Merry 
30334144c2cSAlexander Motin MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend");
304130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
305130f4520SKenneth D. Merry 
3068054320eSAlexander Motin static void
3078054320eSAlexander Motin ctl_alloc_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg,
3088054320eSAlexander Motin     size_t len)
3098054320eSAlexander Motin {
3108054320eSAlexander Motin 
311cd853791SKonstantin Belousov 	if (len <= CTLBLK_MIN_SEG) {
312cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmin_zone, M_WAITOK);
313cd853791SKonstantin Belousov 	} else {
314cd853791SKonstantin Belousov 		KASSERT(len <= CTLBLK_MAX_SEG,
315cd853791SKonstantin Belousov 		    ("Too large alloc %zu > %lu", len, CTLBLK_MAX_SEG));
316cd853791SKonstantin Belousov 		sg->addr = uma_zalloc(softc->bufmax_zone, M_WAITOK);
317cd853791SKonstantin Belousov 	}
3188054320eSAlexander Motin 	sg->len = len;
3198054320eSAlexander Motin }
3208054320eSAlexander Motin 
3218054320eSAlexander Motin static void
3228054320eSAlexander Motin ctl_free_seg(struct ctl_be_block_softc *softc, struct ctl_sg_entry *sg)
3238054320eSAlexander Motin {
3248054320eSAlexander Motin 
325cd853791SKonstantin Belousov 	if (sg->len <= CTLBLK_MIN_SEG) {
326cd853791SKonstantin Belousov 		uma_zfree(softc->bufmin_zone, sg->addr);
327cd853791SKonstantin Belousov 	} else {
328cd853791SKonstantin Belousov 		KASSERT(sg->len <= CTLBLK_MAX_SEG,
329cd853791SKonstantin Belousov 		    ("Too large free %zu > %lu", sg->len, CTLBLK_MAX_SEG));
330cd853791SKonstantin Belousov 		uma_zfree(softc->bufmax_zone, sg->addr);
331cd853791SKonstantin Belousov 	}
3328054320eSAlexander Motin }
3338054320eSAlexander Motin 
334130f4520SKenneth D. Merry static struct ctl_be_block_io *
335130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc)
336130f4520SKenneth D. Merry {
337130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
338130f4520SKenneth D. Merry 
3390c629e28SAlexander Motin 	beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
340130f4520SKenneth D. Merry 	beio->softc = softc;
3419a4510acSAlexander Motin 	beio->refcnt = 1;
342130f4520SKenneth D. Merry 	return (beio);
343130f4520SKenneth D. Merry }
344130f4520SKenneth D. Merry 
345130f4520SKenneth D. Merry static void
3469a4510acSAlexander Motin ctl_real_free_beio(struct ctl_be_block_io *beio)
347130f4520SKenneth D. Merry {
3480d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = beio->softc;
349130f4520SKenneth D. Merry 	int i;
350130f4520SKenneth D. Merry 
351130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
3528054320eSAlexander Motin 		ctl_free_seg(softc, &beio->sg_segs[i]);
35311b569f7SAlexander Motin 
35411b569f7SAlexander Motin 		/* For compare we had two equal S/G lists. */
3550d7fed74SAlexander Motin 		if (beio->two_sglists) {
3568054320eSAlexander Motin 			ctl_free_seg(softc,
3578054320eSAlexander Motin 			    &beio->sg_segs[i + CTLBLK_HALF_SEGS]);
35811b569f7SAlexander Motin 		}
359130f4520SKenneth D. Merry 	}
360130f4520SKenneth D. Merry 
3610d7fed74SAlexander Motin 	uma_zfree(softc->beio_zone, beio);
362130f4520SKenneth D. Merry }
363130f4520SKenneth D. Merry 
364130f4520SKenneth D. Merry static void
3659a4510acSAlexander Motin ctl_refcnt_beio(void *arg, int diff)
3669a4510acSAlexander Motin {
3679a4510acSAlexander Motin 	struct ctl_be_block_io *beio = arg;
3689a4510acSAlexander Motin 
3699a4510acSAlexander Motin 	if (atomic_fetchadd_int(&beio->refcnt, diff) + diff == 0)
3709a4510acSAlexander Motin 		ctl_real_free_beio(beio);
3719a4510acSAlexander Motin }
3729a4510acSAlexander Motin 
3739a4510acSAlexander Motin static void
3749a4510acSAlexander Motin ctl_free_beio(struct ctl_be_block_io *beio)
3759a4510acSAlexander Motin {
3769a4510acSAlexander Motin 
3779a4510acSAlexander Motin 	ctl_refcnt_beio(beio, -1);
3789a4510acSAlexander Motin }
3799a4510acSAlexander Motin 
3809a4510acSAlexander Motin static void
381130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio)
382130f4520SKenneth D. Merry {
38375c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
384130f4520SKenneth D. Merry 
385ee7f31c0SAlexander Motin 	if (beio->beio_cont != NULL) {
386ee7f31c0SAlexander Motin 		beio->beio_cont(beio);
387ee7f31c0SAlexander Motin 	} else {
388130f4520SKenneth D. Merry 		ctl_free_beio(beio);
38911b569f7SAlexander Motin 		ctl_data_submit_done(io);
390130f4520SKenneth D. Merry 	}
391ee7f31c0SAlexander Motin }
392130f4520SKenneth D. Merry 
393d6043e46SAlexander Motin static size_t
394d6043e46SAlexander Motin cmp(uint8_t *a, uint8_t *b, size_t size)
395d6043e46SAlexander Motin {
396d6043e46SAlexander Motin 	size_t i;
397d6043e46SAlexander Motin 
398d6043e46SAlexander Motin 	for (i = 0; i < size; i++) {
399d6043e46SAlexander Motin 		if (a[i] != b[i])
400d6043e46SAlexander Motin 			break;
401d6043e46SAlexander Motin 	}
402d6043e46SAlexander Motin 	return (i);
403d6043e46SAlexander Motin }
404d6043e46SAlexander Motin 
405d6043e46SAlexander Motin static void
406d6043e46SAlexander Motin ctl_be_block_compare(union ctl_io *io)
407d6043e46SAlexander Motin {
408d6043e46SAlexander Motin 	struct ctl_be_block_io *beio;
409d6043e46SAlexander Motin 	uint64_t off, res;
410d6043e46SAlexander Motin 	int i;
411d6043e46SAlexander Motin 	uint8_t info[8];
412d6043e46SAlexander Motin 
413d6043e46SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
414d6043e46SAlexander Motin 	off = 0;
415d6043e46SAlexander Motin 	for (i = 0; i < beio->num_segs; i++) {
416d6043e46SAlexander Motin 		res = cmp(beio->sg_segs[i].addr,
417d6043e46SAlexander Motin 		    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
418d6043e46SAlexander Motin 		    beio->sg_segs[i].len);
419d6043e46SAlexander Motin 		off += res;
420d6043e46SAlexander Motin 		if (res < beio->sg_segs[i].len)
421d6043e46SAlexander Motin 			break;
422d6043e46SAlexander Motin 	}
423d6043e46SAlexander Motin 	if (i < beio->num_segs) {
424d6043e46SAlexander Motin 		scsi_u64to8b(off, info);
425d6043e46SAlexander Motin 		ctl_set_sense(&io->scsiio, /*current_error*/ 1,
426d6043e46SAlexander Motin 		    /*sense_key*/ SSD_KEY_MISCOMPARE,
427d6043e46SAlexander Motin 		    /*asc*/ 0x1D, /*ascq*/ 0x00,
428d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_INFO,
429d6043e46SAlexander Motin 		    /*size*/ sizeof(info), /*data*/ &info,
430d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_NONE);
431d6043e46SAlexander Motin 	} else
432d6043e46SAlexander Motin 		ctl_set_success(&io->scsiio);
433d6043e46SAlexander Motin }
434d6043e46SAlexander Motin 
435130f4520SKenneth D. Merry static int
4362c7dc6baSAlexander Motin ctl_be_block_move_done(union ctl_io *io, bool samethr)
437130f4520SKenneth D. Merry {
438130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
439130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
44011b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
441130f4520SKenneth D. Merry 
442e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
443130f4520SKenneth D. Merry 
444130f4520SKenneth D. Merry 	DPRINTF("entered\n");
44511b569f7SAlexander Motin 	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
446130f4520SKenneth D. Merry 
447130f4520SKenneth D. Merry 	/*
4482c7dc6baSAlexander Motin 	 * We set status at this point for read and compare commands.
449130f4520SKenneth D. Merry 	 */
4502c7dc6baSAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4512c7dc6baSAlexander Motin 	    (io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) {
452a59e2982SAlexander Motin 		lbalen = ARGS(io);
45311b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_READ) {
454130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
45511b569f7SAlexander Motin 		} else if (lbalen->flags & CTL_LLF_COMPARE) {
45611b569f7SAlexander Motin 			/* We have two data blocks ready for comparison. */
457d6043e46SAlexander Motin 			ctl_be_block_compare(io);
45811b569f7SAlexander Motin 		}
459130f4520SKenneth D. Merry 	}
460130f4520SKenneth D. Merry 
461130f4520SKenneth D. Merry 	/*
462130f4520SKenneth D. Merry 	 * If this is a read, or a write with errors, it is done.
463130f4520SKenneth D. Merry 	 */
464130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_READ)
465130f4520SKenneth D. Merry 	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
466130f4520SKenneth D. Merry 	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
467130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
468130f4520SKenneth D. Merry 		return (0);
469130f4520SKenneth D. Merry 	}
470130f4520SKenneth D. Merry 
471130f4520SKenneth D. Merry 	/*
4722c7dc6baSAlexander Motin 	 * At this point, we have a write and the DMA completed successfully.
4732c7dc6baSAlexander Motin 	 * If we were called synchronously in the original thread then just
4742c7dc6baSAlexander Motin 	 * dispatch, otherwise we now have to queue it to the task queue to
475130f4520SKenneth D. Merry 	 * execute the backend I/O.  That is because we do blocking
476130f4520SKenneth D. Merry 	 * memory allocations, and in the file backing case, blocking I/O.
477130f4520SKenneth D. Merry 	 * This move done routine is generally called in the SIM's
478130f4520SKenneth D. Merry 	 * interrupt context, and therefore we cannot block.
479130f4520SKenneth D. Merry 	 */
480a59e2982SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
4812c7dc6baSAlexander Motin 	if (samethr) {
4822c7dc6baSAlexander Motin 		be_lun->dispatch(be_lun, beio);
4832c7dc6baSAlexander Motin 	} else {
48475c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
485130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
48675c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
487130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
4882c7dc6baSAlexander Motin 	}
489130f4520SKenneth D. Merry 	return (0);
490130f4520SKenneth D. Merry }
491130f4520SKenneth D. Merry 
492130f4520SKenneth D. Merry static void
493130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio)
494130f4520SKenneth D. Merry {
495ac503c19SAlexander Motin 	struct ctl_be_block_io *beio = bio->bio_caller1;
496ac503c19SAlexander Motin 	struct ctl_be_block_lun *be_lun = beio->lun;
497ac503c19SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
498130f4520SKenneth D. Merry 	union ctl_io *io;
499e0c2f975SAlexander Motin 	int error;
500130f4520SKenneth D. Merry 
501130f4520SKenneth D. Merry 	io = beio->io;
502130f4520SKenneth D. Merry 
503130f4520SKenneth D. Merry 	DPRINTF("entered\n");
504130f4520SKenneth D. Merry 
505e0c2f975SAlexander Motin 	error = bio->bio_error;
50675c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
5071f0694a6SAlexander Motin 	if (error != 0 &&
5081f0694a6SAlexander Motin 	    (beio->first_error == 0 ||
5091f0694a6SAlexander Motin 	     bio->bio_offset < beio->first_error_offset)) {
5101f0694a6SAlexander Motin 		beio->first_error = error;
5111f0694a6SAlexander Motin 		beio->first_error_offset = bio->bio_offset;
5121f0694a6SAlexander Motin 	}
513130f4520SKenneth D. Merry 
514130f4520SKenneth D. Merry 	beio->num_bios_done++;
515130f4520SKenneth D. Merry 
516130f4520SKenneth D. Merry 	/*
517130f4520SKenneth D. Merry 	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
518130f4520SKenneth D. Merry 	 * during the free might cause it to complain.
519130f4520SKenneth D. Merry 	 */
520130f4520SKenneth D. Merry 	g_destroy_bio(bio);
521130f4520SKenneth D. Merry 
522130f4520SKenneth D. Merry 	/*
523130f4520SKenneth D. Merry 	 * If the send complete bit isn't set, or we aren't the last I/O to
524130f4520SKenneth D. Merry 	 * complete, then we're done.
525130f4520SKenneth D. Merry 	 */
526130f4520SKenneth D. Merry 	if ((beio->send_complete == 0)
527130f4520SKenneth D. Merry 	 || (beio->num_bios_done < beio->num_bios_sent)) {
52875c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
529130f4520SKenneth D. Merry 		return;
530130f4520SKenneth D. Merry 	}
531130f4520SKenneth D. Merry 
532130f4520SKenneth D. Merry 	/*
533130f4520SKenneth D. Merry 	 * At this point, we've verified that we are the last I/O to
534130f4520SKenneth D. Merry 	 * complete, so it's safe to drop the lock.
535130f4520SKenneth D. Merry 	 */
53675c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
53775c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
53875c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
53975c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
540130f4520SKenneth D. Merry 
541130f4520SKenneth D. Merry 	/*
542130f4520SKenneth D. Merry 	 * If there are any errors from the backing device, we fail the
543130f4520SKenneth D. Merry 	 * entire I/O with a medium error.
544130f4520SKenneth D. Merry 	 */
5451f0694a6SAlexander Motin 	error = beio->first_error;
5461f0694a6SAlexander Motin 	if (error != 0) {
547e0c2f975SAlexander Motin 		if (error == EOPNOTSUPP) {
548e0c2f975SAlexander Motin 			ctl_set_invalid_opcode(&io->scsiio);
5490631de4aSAlexander Motin 		} else if (error == ENOSPC || error == EDQUOT) {
5504fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
5516187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
5526187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
553e0c2f975SAlexander Motin 		} else if (beio->bio_cmd == BIO_FLUSH) {
554130f4520SKenneth D. Merry 			/* XXX KDM is there is a better error here? */
555130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
556130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
557130f4520SKenneth D. Merry 						 /*retry_count*/ 0xbad2);
5587f7bb97aSAlexander Motin 		} else {
5597f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
5607f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
5617f7bb97aSAlexander Motin 		}
562130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
563130f4520SKenneth D. Merry 		return;
564130f4520SKenneth D. Merry 	}
565130f4520SKenneth D. Merry 
566130f4520SKenneth D. Merry 	/*
56711b569f7SAlexander Motin 	 * If this is a write, a flush, a delete or verify, we're all done.
568130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
569130f4520SKenneth D. Merry 	 */
570130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_WRITE)
571ee7f31c0SAlexander Motin 	 || (beio->bio_cmd == BIO_FLUSH)
57211b569f7SAlexander Motin 	 || (beio->bio_cmd == BIO_DELETE)
57311b569f7SAlexander Motin 	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
574130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
575130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
576130f4520SKenneth D. Merry 	} else {
577f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
57875a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
579f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
580ac503c19SAlexander Motin 			if (cbe_lun->serseq >= CTL_LUN_SERSEQ_SOFT)
58175a3108eSAlexander Motin 				ctl_serseq_done(io);
58275a3108eSAlexander Motin 		}
583130f4520SKenneth D. Merry 		ctl_datamove(io);
584130f4520SKenneth D. Merry 	}
585130f4520SKenneth D. Merry }
586130f4520SKenneth D. Merry 
587130f4520SKenneth D. Merry static void
588130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
589130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio)
590130f4520SKenneth D. Merry {
59175c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
592130f4520SKenneth D. Merry 	struct mount *mountpoint;
5930ef5eee9SKonstantin Belousov 	int error;
594130f4520SKenneth D. Merry 
595130f4520SKenneth D. Merry 	DPRINTF("entered\n");
596130f4520SKenneth D. Merry 
59775c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
59875c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
599130f4520SKenneth D. Merry 
600130f4520SKenneth D. Merry 	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
601130f4520SKenneth D. Merry 
6020ef5eee9SKonstantin Belousov 	vn_lock(be_lun->vn, vn_lktype_write(mountpoint, be_lun->vn) |
6030ef5eee9SKonstantin Belousov 	    LK_RETRY);
6047d0d4342SAlexander Motin 	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
6057d0d4342SAlexander Motin 	    curthread);
606b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
607130f4520SKenneth D. Merry 
608130f4520SKenneth D. Merry 	vn_finished_write(mountpoint);
609130f4520SKenneth D. Merry 
61075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
61175c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
61275c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
61375c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
61475c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
61575c7a1d3SAlexander Motin 
616130f4520SKenneth D. Merry 	if (error == 0)
617130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
618130f4520SKenneth D. Merry 	else {
619130f4520SKenneth D. Merry 		/* XXX KDM is there is a better error here? */
620130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
621130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
622130f4520SKenneth D. Merry 					 /*retry_count*/ 0xbad1);
623130f4520SKenneth D. Merry 	}
624130f4520SKenneth D. Merry 
625130f4520SKenneth D. Merry 	ctl_complete_beio(beio);
626130f4520SKenneth D. Merry }
627130f4520SKenneth D. Merry 
62836160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
62936160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
63036160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
63136160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
632130f4520SKenneth D. Merry 
633130f4520SKenneth D. Merry static void
634130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
635130f4520SKenneth D. Merry 			   struct ctl_be_block_io *beio)
636130f4520SKenneth D. Merry {
637ac503c19SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
638130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
639130f4520SKenneth D. Merry 	union ctl_io *io;
640130f4520SKenneth D. Merry 	struct uio xuio;
641130f4520SKenneth D. Merry 	struct iovec *xiovec;
64283981e31SAlexander Motin 	size_t s;
64383981e31SAlexander Motin 	int error, flags, i;
644130f4520SKenneth D. Merry 
645130f4520SKenneth D. Merry 	DPRINTF("entered\n");
646130f4520SKenneth D. Merry 
647130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
648130f4520SKenneth D. Merry 	io = beio->io;
64955551d05SAlexander Motin 	flags = 0;
65055551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
65155551d05SAlexander Motin 		flags |= IO_DIRECT;
65255551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
65355551d05SAlexander Motin 		flags |= IO_SYNC;
654130f4520SKenneth D. Merry 
65511b569f7SAlexander Motin 	bzero(&xuio, sizeof(xuio));
656130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
65736160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
65811b569f7SAlexander Motin 		xuio.uio_rw = UIO_READ;
659130f4520SKenneth D. Merry 	} else {
66036160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
661130f4520SKenneth D. Merry 		xuio.uio_rw = UIO_WRITE;
66211b569f7SAlexander Motin 	}
663130f4520SKenneth D. Merry 	xuio.uio_offset = beio->io_offset;
664130f4520SKenneth D. Merry 	xuio.uio_resid = beio->io_len;
665130f4520SKenneth D. Merry 	xuio.uio_segflg = UIO_SYSSPACE;
666130f4520SKenneth D. Merry 	xuio.uio_iov = beio->xiovecs;
667130f4520SKenneth D. Merry 	xuio.uio_iovcnt = beio->num_segs;
668130f4520SKenneth D. Merry 	xuio.uio_td = curthread;
669130f4520SKenneth D. Merry 
670130f4520SKenneth D. Merry 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
671130f4520SKenneth D. Merry 		xiovec->iov_base = beio->sg_segs[i].addr;
672130f4520SKenneth D. Merry 		xiovec->iov_len = beio->sg_segs[i].len;
673130f4520SKenneth D. Merry 	}
674130f4520SKenneth D. Merry 
67575c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
67675c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
67775c7a1d3SAlexander Motin 
678130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
679130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
680130f4520SKenneth D. Merry 
681ac503c19SAlexander Motin 		if (beio->beio_cont == NULL &&
682ac503c19SAlexander Motin 		    cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
683ac503c19SAlexander Motin 			ctl_serseq_done(io);
684130f4520SKenneth D. Merry 		/*
685130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for reads.  If the
686130f4520SKenneth D. Merry 		 * DIRECTIO option is configured into the kernel, it calls
687130f4520SKenneth D. Merry 		 * ffs_rawread().  But that only works for single-segment
688130f4520SKenneth D. Merry 		 * uios with user space addresses.  In our case, with a
689130f4520SKenneth D. Merry 		 * kernel uio, it still reads into the buffer cache, but it
690130f4520SKenneth D. Merry 		 * will just try to release the buffer from the cache later
691130f4520SKenneth D. Merry 		 * on in ffs_read().
692130f4520SKenneth D. Merry 		 *
693130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for reads.
694130f4520SKenneth D. Merry 		 *
695130f4520SKenneth D. Merry 		 * UFS does not pay attention to IO_SYNC for reads.
696130f4520SKenneth D. Merry 		 *
697130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (which translates into the
698130f4520SKenneth D. Merry 		 * Solaris define FRSYNC for zfs_read()) for reads.  It
699130f4520SKenneth D. Merry 		 * attempts to sync the file before reading.
700130f4520SKenneth D. Merry 		 */
70155551d05SAlexander Motin 		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
702130f4520SKenneth D. Merry 
703b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
70436160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
70583981e31SAlexander Motin 		if (error == 0 && xuio.uio_resid > 0) {
70683981e31SAlexander Motin 			/*
70783981e31SAlexander Motin 			 * If we red less then requested (EOF), then
70883981e31SAlexander Motin 			 * we should clean the rest of the buffer.
70983981e31SAlexander Motin 			 */
71083981e31SAlexander Motin 			s = beio->io_len - xuio.uio_resid;
71183981e31SAlexander Motin 			for (i = 0; i < beio->num_segs; i++) {
71283981e31SAlexander Motin 				if (s >= beio->sg_segs[i].len) {
71383981e31SAlexander Motin 					s -= beio->sg_segs[i].len;
71483981e31SAlexander Motin 					continue;
71583981e31SAlexander Motin 				}
71683981e31SAlexander Motin 				bzero((uint8_t *)beio->sg_segs[i].addr + s,
71783981e31SAlexander Motin 				    beio->sg_segs[i].len - s);
71883981e31SAlexander Motin 				s = 0;
71983981e31SAlexander Motin 			}
72083981e31SAlexander Motin 		}
721130f4520SKenneth D. Merry 	} else {
722130f4520SKenneth D. Merry 		struct mount *mountpoint;
723130f4520SKenneth D. Merry 
724130f4520SKenneth D. Merry 		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
7250ef5eee9SKonstantin Belousov 		vn_lock(be_lun->vn, vn_lktype_write(mountpoint,
7260ef5eee9SKonstantin Belousov 		    be_lun->vn) | LK_RETRY);
727130f4520SKenneth D. Merry 
728130f4520SKenneth D. Merry 		/*
729130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for writes.  The write
730130f4520SKenneth D. Merry 		 * is done asynchronously.  (Normally the write would just
731130f4520SKenneth D. Merry 		 * get put into cache.
732130f4520SKenneth D. Merry 		 *
733130f4520SKenneth D. Merry 		 * UFS pays attention to IO_SYNC for writes.  It will
734130f4520SKenneth D. Merry 		 * attempt to write the buffer out synchronously if that
735130f4520SKenneth D. Merry 		 * flag is set.
736130f4520SKenneth D. Merry 		 *
737130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for writes.
738130f4520SKenneth D. Merry 		 *
739130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
740130f4520SKenneth D. Merry 		 * for writes.  It will flush the transaction from the
741130f4520SKenneth D. Merry 		 * cache before returning.
742130f4520SKenneth D. Merry 		 */
74355551d05SAlexander Motin 		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
744b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
745130f4520SKenneth D. Merry 
746130f4520SKenneth D. Merry 		vn_finished_write(mountpoint);
74736160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
748130f4520SKenneth D. Merry         }
749130f4520SKenneth D. Merry 
75075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
75175c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
75275c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
75375c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
75475c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
75575c7a1d3SAlexander Motin 
756130f4520SKenneth D. Merry 	/*
757130f4520SKenneth D. Merry 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
758130f4520SKenneth D. Merry 	 * return the I/O to the user.
759130f4520SKenneth D. Merry 	 */
760130f4520SKenneth D. Merry 	if (error != 0) {
7610631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
7624fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
7636187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
7646187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
7657f7bb97aSAlexander Motin 		} else {
7667f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
7677f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
7687f7bb97aSAlexander Motin 		}
769130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
770130f4520SKenneth D. Merry 		return;
771130f4520SKenneth D. Merry 	}
772130f4520SKenneth D. Merry 
773130f4520SKenneth D. Merry 	/*
774696297adSAlexander Motin 	 * If this is a write or a verify, we're all done.
775130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
776130f4520SKenneth D. Merry 	 */
777696297adSAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
778696297adSAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
779130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
780130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
781130f4520SKenneth D. Merry 	} else {
782f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
78375a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
784f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
785ac503c19SAlexander Motin 			if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
78675a3108eSAlexander Motin 				ctl_serseq_done(io);
78775a3108eSAlexander Motin 		}
788130f4520SKenneth D. Merry 		ctl_datamove(io);
789130f4520SKenneth D. Merry 	}
790130f4520SKenneth D. Merry }
791130f4520SKenneth D. Merry 
792130f4520SKenneth D. Merry static void
793ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
794ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
795ef8daf3fSAlexander Motin {
796ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
797ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
798ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
799ef8daf3fSAlexander Motin 	off_t roff, off;
800ef8daf3fSAlexander Motin 	int error, status;
801ef8daf3fSAlexander Motin 
802ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
803ef8daf3fSAlexander Motin 
8040bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
805ef8daf3fSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
806ef8daf3fSAlexander Motin 	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
807ef8daf3fSAlexander Motin 	    0, curthread->td_ucred, curthread);
808ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
809ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
810ef8daf3fSAlexander Motin 	else {
811ef8daf3fSAlexander Motin 		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
812ef8daf3fSAlexander Motin 		    0, curthread->td_ucred, curthread);
813ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
814ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
815ef8daf3fSAlexander Motin 		else {
816ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
817ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
818ef8daf3fSAlexander Motin 		}
819ef8daf3fSAlexander Motin 	}
820b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
821ef8daf3fSAlexander Motin 
822ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
823ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
8240bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
8250bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
826ef8daf3fSAlexander Motin 	data->descr[0].status = status;
827ef8daf3fSAlexander Motin 
828ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
829ef8daf3fSAlexander Motin }
830ef8daf3fSAlexander Motin 
83153c146deSAlexander Motin static uint64_t
83253c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
83353c146deSAlexander Motin {
83453c146deSAlexander Motin 	struct vattr		vattr;
83553c146deSAlexander Motin 	struct statfs		statfs;
836b9b4269cSAlexander Motin 	uint64_t		val;
83753c146deSAlexander Motin 	int			error;
83853c146deSAlexander Motin 
839b9b4269cSAlexander Motin 	val = UINT64_MAX;
84053c146deSAlexander Motin 	if (be_lun->vn == NULL)
841b9b4269cSAlexander Motin 		return (val);
842b9b4269cSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
84353c146deSAlexander Motin 	if (strcmp(attrname, "blocksused") == 0) {
84453c146deSAlexander Motin 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
845b9b4269cSAlexander Motin 		if (error == 0)
8460bcd4ab6SAlexander Motin 			val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
84753c146deSAlexander Motin 	}
848b9b4269cSAlexander Motin 	if (strcmp(attrname, "blocksavail") == 0 &&
849abd80ddbSMateusz Guzik 	    !VN_IS_DOOMED(be_lun->vn)) {
85053c146deSAlexander Motin 		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
851b9b4269cSAlexander Motin 		if (error == 0)
852a15bbf15SAlexander Motin 			val = statfs.f_bavail * statfs.f_bsize /
8530bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
85453c146deSAlexander Motin 	}
855b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
856b9b4269cSAlexander Motin 	return (val);
85753c146deSAlexander Motin }
85853c146deSAlexander Motin 
859ef8daf3fSAlexander Motin static void
86049050613SKa Ho Ng ctl_be_block_unmap_file(struct ctl_be_block_lun *be_lun,
86149050613SKa Ho Ng 		        struct ctl_be_block_io *beio)
86249050613SKa Ho Ng {
86349050613SKa Ho Ng 	struct ctl_be_block_filedata *file_data;
86449050613SKa Ho Ng 	union ctl_io *io;
86549050613SKa Ho Ng 	struct ctl_ptr_len_flags *ptrlen;
86649050613SKa Ho Ng 	struct scsi_unmap_desc *buf, *end;
86749050613SKa Ho Ng 	struct mount *mp;
86849050613SKa Ho Ng 	off_t off, len;
86949050613SKa Ho Ng 	int error;
87049050613SKa Ho Ng 
87149050613SKa Ho Ng 	io = beio->io;
87249050613SKa Ho Ng 	file_data = &be_lun->backend.file;
87349050613SKa Ho Ng 	mp = NULL;
87449050613SKa Ho Ng 	error = 0;
87549050613SKa Ho Ng 
87649050613SKa Ho Ng 	binuptime(&beio->ds_t0);
87749050613SKa Ho Ng 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
87849050613SKa Ho Ng 
87949050613SKa Ho Ng 	(void)vn_start_write(be_lun->vn, &mp, V_WAIT);
88049050613SKa Ho Ng 	vn_lock(be_lun->vn, vn_lktype_write(mp, be_lun->vn) | LK_RETRY);
88149050613SKa Ho Ng 	if (beio->io_offset == -1) {
88249050613SKa Ho Ng 		beio->io_len = 0;
88349050613SKa Ho Ng 		ptrlen = (struct ctl_ptr_len_flags *)
88449050613SKa Ho Ng 		    &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
88549050613SKa Ho Ng 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
88649050613SKa Ho Ng 		end = buf + ptrlen->len / sizeof(*buf);
88749050613SKa Ho Ng 		for (; buf < end; buf++) {
88849050613SKa Ho Ng 			off = (off_t)scsi_8btou64(buf->lba) *
88949050613SKa Ho Ng 			    be_lun->cbe_lun.blocksize;
89049050613SKa Ho Ng 			len = (off_t)scsi_4btoul(buf->length) *
89149050613SKa Ho Ng 			    be_lun->cbe_lun.blocksize;
89249050613SKa Ho Ng 			beio->io_len += len;
89349050613SKa Ho Ng 			error = vn_deallocate(be_lun->vn, &off, &len,
89449050613SKa Ho Ng 			    0, IO_NOMACCHECK | IO_NODELOCKED, file_data->cred,
89549050613SKa Ho Ng 			    NOCRED);
89649050613SKa Ho Ng 			if (error != 0)
89749050613SKa Ho Ng 				break;
89849050613SKa Ho Ng 		}
89949050613SKa Ho Ng 	} else {
90049050613SKa Ho Ng 		/* WRITE_SAME */
90149050613SKa Ho Ng 		off = beio->io_offset;
90249050613SKa Ho Ng 		len = beio->io_len;
90349050613SKa Ho Ng 		error = vn_deallocate(be_lun->vn, &off, &len, 0,
90449050613SKa Ho Ng 		    IO_NOMACCHECK | IO_NODELOCKED, file_data->cred, NOCRED);
90549050613SKa Ho Ng 	}
90649050613SKa Ho Ng 	VOP_UNLOCK(be_lun->vn);
90749050613SKa Ho Ng 	vn_finished_write(mp);
90849050613SKa Ho Ng 
90949050613SKa Ho Ng 	mtx_lock(&be_lun->io_lock);
91049050613SKa Ho Ng 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
91149050613SKa Ho Ng 	    beio->ds_tag_type, beio->ds_trans_type,
91249050613SKa Ho Ng 	    /*now*/ NULL, /*then*/&beio->ds_t0);
91349050613SKa Ho Ng 	mtx_unlock(&be_lun->io_lock);
91449050613SKa Ho Ng 
91549050613SKa Ho Ng 	/*
91649050613SKa Ho Ng 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
91749050613SKa Ho Ng 	 * return the I/O to the user.
91849050613SKa Ho Ng 	 */
91949050613SKa Ho Ng 	switch (error) {
92049050613SKa Ho Ng 	case 0:
92149050613SKa Ho Ng 		ctl_set_success(&io->scsiio);
92249050613SKa Ho Ng 		break;
92349050613SKa Ho Ng 	case ENOSPC:
92449050613SKa Ho Ng 	case EDQUOT:
92549050613SKa Ho Ng 		ctl_set_space_alloc_fail(&io->scsiio);
92649050613SKa Ho Ng 		break;
92749050613SKa Ho Ng 	case EROFS:
92849050613SKa Ho Ng 	case EACCES:
92949050613SKa Ho Ng 		ctl_set_hw_write_protected(&io->scsiio);
93049050613SKa Ho Ng 		break;
93149050613SKa Ho Ng 	default:
93249050613SKa Ho Ng 		ctl_set_medium_error(&io->scsiio, false);
93349050613SKa Ho Ng 	}
93449050613SKa Ho Ng 	ctl_complete_beio(beio);
93549050613SKa Ho Ng }
93649050613SKa Ho Ng 
93749050613SKa Ho Ng static void
93867f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
93967f586a8SAlexander Motin 			   struct ctl_be_block_io *beio)
94067f586a8SAlexander Motin {
941ac503c19SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
94267f586a8SAlexander Motin 	union ctl_io *io;
9433236151eSAlexander Motin 	struct cdevsw *csw;
9443236151eSAlexander Motin 	struct cdev *dev;
94567f586a8SAlexander Motin 	struct uio xuio;
94667f586a8SAlexander Motin 	struct iovec *xiovec;
9473236151eSAlexander Motin 	int error, flags, i, ref;
94867f586a8SAlexander Motin 
94967f586a8SAlexander Motin 	DPRINTF("entered\n");
95067f586a8SAlexander Motin 
95167f586a8SAlexander Motin 	io = beio->io;
95255551d05SAlexander Motin 	flags = 0;
95355551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
95455551d05SAlexander Motin 		flags |= IO_DIRECT;
95555551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
95655551d05SAlexander Motin 		flags |= IO_SYNC;
95767f586a8SAlexander Motin 
95867f586a8SAlexander Motin 	bzero(&xuio, sizeof(xuio));
95967f586a8SAlexander Motin 	if (beio->bio_cmd == BIO_READ) {
96036160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
96167f586a8SAlexander Motin 		xuio.uio_rw = UIO_READ;
96267f586a8SAlexander Motin 	} else {
96336160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
96467f586a8SAlexander Motin 		xuio.uio_rw = UIO_WRITE;
96567f586a8SAlexander Motin 	}
96667f586a8SAlexander Motin 	xuio.uio_offset = beio->io_offset;
96767f586a8SAlexander Motin 	xuio.uio_resid = beio->io_len;
96867f586a8SAlexander Motin 	xuio.uio_segflg = UIO_SYSSPACE;
96967f586a8SAlexander Motin 	xuio.uio_iov = beio->xiovecs;
97067f586a8SAlexander Motin 	xuio.uio_iovcnt = beio->num_segs;
97167f586a8SAlexander Motin 	xuio.uio_td = curthread;
97267f586a8SAlexander Motin 
97367f586a8SAlexander Motin 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
97467f586a8SAlexander Motin 		xiovec->iov_base = beio->sg_segs[i].addr;
97567f586a8SAlexander Motin 		xiovec->iov_len = beio->sg_segs[i].len;
97667f586a8SAlexander Motin 	}
97767f586a8SAlexander Motin 
97867f586a8SAlexander Motin 	binuptime(&beio->ds_t0);
97967f586a8SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
98067f586a8SAlexander Motin 
9813236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
9823236151eSAlexander Motin 	if (csw) {
983ac503c19SAlexander Motin 		if (beio->bio_cmd == BIO_READ) {
984ac503c19SAlexander Motin 			if (beio->beio_cont == NULL &&
985ac503c19SAlexander Motin 			    cbe_lun->serseq == CTL_LUN_SERSEQ_SOFT)
986ac503c19SAlexander Motin 				ctl_serseq_done(io);
9873236151eSAlexander Motin 			error = csw->d_read(dev, &xuio, flags);
988ac503c19SAlexander Motin 		} else
9893236151eSAlexander Motin 			error = csw->d_write(dev, &xuio, flags);
9903236151eSAlexander Motin 		dev_relthread(dev, ref);
9913236151eSAlexander Motin 	} else
9923236151eSAlexander Motin 		error = ENXIO;
9933236151eSAlexander Motin 
9943236151eSAlexander Motin 	if (beio->bio_cmd == BIO_READ)
99536160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
9963236151eSAlexander Motin 	else
99736160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
99867f586a8SAlexander Motin 
99967f586a8SAlexander Motin 	mtx_lock(&be_lun->io_lock);
100067f586a8SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
100167f586a8SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
100267f586a8SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
100367f586a8SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
100467f586a8SAlexander Motin 
100567f586a8SAlexander Motin 	/*
100667f586a8SAlexander Motin 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
100767f586a8SAlexander Motin 	 * return the I/O to the user.
100867f586a8SAlexander Motin 	 */
100967f586a8SAlexander Motin 	if (error != 0) {
10100631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
10114fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
10126187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
10136187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
10147f7bb97aSAlexander Motin 		} else {
10157f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
10167f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
10177f7bb97aSAlexander Motin 		}
101867f586a8SAlexander Motin 		ctl_complete_beio(beio);
101967f586a8SAlexander Motin 		return;
102067f586a8SAlexander Motin 	}
102167f586a8SAlexander Motin 
102267f586a8SAlexander Motin 	/*
102367f586a8SAlexander Motin 	 * If this is a write or a verify, we're all done.
102467f586a8SAlexander Motin 	 * If this is a read, we can now send the data to the user.
102567f586a8SAlexander Motin 	 */
102667f586a8SAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
102767f586a8SAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
102867f586a8SAlexander Motin 		ctl_set_success(&io->scsiio);
102967f586a8SAlexander Motin 		ctl_complete_beio(beio);
103067f586a8SAlexander Motin 	} else {
1031f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
103275a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
1033f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
1034ac503c19SAlexander Motin 			if (cbe_lun->serseq > CTL_LUN_SERSEQ_SOFT)
103575a3108eSAlexander Motin 				ctl_serseq_done(io);
103675a3108eSAlexander Motin 		}
103767f586a8SAlexander Motin 		ctl_datamove(io);
103867f586a8SAlexander Motin 	}
103967f586a8SAlexander Motin }
104067f586a8SAlexander Motin 
104167f586a8SAlexander Motin static void
1042ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
1043ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
1044ef8daf3fSAlexander Motin {
1045ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
10463236151eSAlexander Motin 	struct cdevsw *csw;
10473236151eSAlexander Motin 	struct cdev *dev;
1048ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
1049ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
1050ef8daf3fSAlexander Motin 	off_t roff, off;
10513236151eSAlexander Motin 	int error, ref, status;
1052ef8daf3fSAlexander Motin 
1053ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1054ef8daf3fSAlexander Motin 
10553236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10563236151eSAlexander Motin 	if (csw == NULL) {
10573236151eSAlexander Motin 		status = 0;	/* unknown up to the end */
10583236151eSAlexander Motin 		off = be_lun->size_bytes;
10593236151eSAlexander Motin 		goto done;
10603236151eSAlexander Motin 	}
10610bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
10623236151eSAlexander Motin 	error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
10633236151eSAlexander Motin 	    curthread);
1064ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
1065ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
1066ef8daf3fSAlexander Motin 	else {
10673236151eSAlexander Motin 		error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
10683236151eSAlexander Motin 		    curthread);
1069ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
1070ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
1071ef8daf3fSAlexander Motin 		else {
1072ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
1073ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
1074ef8daf3fSAlexander Motin 		}
1075ef8daf3fSAlexander Motin 	}
10763236151eSAlexander Motin 	dev_relthread(dev, ref);
1077ef8daf3fSAlexander Motin 
10783236151eSAlexander Motin done:
1079ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
1080ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
10810bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
10820bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
1083ef8daf3fSAlexander Motin 	data->descr[0].status = status;
1084ef8daf3fSAlexander Motin 
1085ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
1086ef8daf3fSAlexander Motin }
1087ef8daf3fSAlexander Motin 
1088ef8daf3fSAlexander Motin static void
1089130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1090130f4520SKenneth D. Merry 		       struct ctl_be_block_io *beio)
1091130f4520SKenneth D. Merry {
1092130f4520SKenneth D. Merry 	struct bio *bio;
10933236151eSAlexander Motin 	struct cdevsw *csw;
10943236151eSAlexander Motin 	struct cdev *dev;
10953236151eSAlexander Motin 	int ref;
1096130f4520SKenneth D. Merry 
1097130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1098130f4520SKenneth D. Merry 
1099130f4520SKenneth D. Merry 	/* This can't fail, it's a blocking allocation. */
1100130f4520SKenneth D. Merry 	bio = g_alloc_bio();
1101130f4520SKenneth D. Merry 
1102130f4520SKenneth D. Merry 	bio->bio_cmd	    = BIO_FLUSH;
1103130f4520SKenneth D. Merry 	bio->bio_offset	    = 0;
1104130f4520SKenneth D. Merry 	bio->bio_data	    = 0;
1105130f4520SKenneth D. Merry 	bio->bio_done	    = ctl_be_block_biodone;
1106130f4520SKenneth D. Merry 	bio->bio_caller1    = beio;
1107130f4520SKenneth D. Merry 	bio->bio_pblkno	    = 0;
1108130f4520SKenneth D. Merry 
1109130f4520SKenneth D. Merry 	/*
1110130f4520SKenneth D. Merry 	 * We don't need to acquire the LUN lock here, because we are only
1111130f4520SKenneth D. Merry 	 * sending one bio, and so there is no other context to synchronize
1112130f4520SKenneth D. Merry 	 * with.
1113130f4520SKenneth D. Merry 	 */
1114130f4520SKenneth D. Merry 	beio->num_bios_sent = 1;
1115130f4520SKenneth D. Merry 	beio->send_complete = 1;
1116130f4520SKenneth D. Merry 
1117130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
1118130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1119130f4520SKenneth D. Merry 
11203236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
11213236151eSAlexander Motin 	if (csw) {
11223236151eSAlexander Motin 		bio->bio_dev = dev;
11233236151eSAlexander Motin 		csw->d_strategy(bio);
11243236151eSAlexander Motin 		dev_relthread(dev, ref);
11253236151eSAlexander Motin 	} else {
11263236151eSAlexander Motin 		bio->bio_error = ENXIO;
11273236151eSAlexander Motin 		ctl_be_block_biodone(bio);
11283236151eSAlexander Motin 	}
1129130f4520SKenneth D. Merry }
1130130f4520SKenneth D. Merry 
1131130f4520SKenneth D. Merry static void
1132ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1133ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio,
1134ee7f31c0SAlexander Motin 		       uint64_t off, uint64_t len, int last)
1135ee7f31c0SAlexander Motin {
1136ee7f31c0SAlexander Motin 	struct bio *bio;
11378f5a226aSAlexander Motin 	uint64_t maxlen;
11383236151eSAlexander Motin 	struct cdevsw *csw;
11393236151eSAlexander Motin 	struct cdev *dev;
11403236151eSAlexander Motin 	int ref;
1141ee7f31c0SAlexander Motin 
11423236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
11430bcd4ab6SAlexander Motin 	maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1144ee7f31c0SAlexander Motin 	while (len > 0) {
1145ee7f31c0SAlexander Motin 		bio = g_alloc_bio();
1146ee7f31c0SAlexander Motin 		bio->bio_cmd	    = BIO_DELETE;
11473236151eSAlexander Motin 		bio->bio_dev	    = dev;
1148ee7f31c0SAlexander Motin 		bio->bio_offset	    = off;
11498f5a226aSAlexander Motin 		bio->bio_length	    = MIN(len, maxlen);
1150ee7f31c0SAlexander Motin 		bio->bio_data	    = 0;
1151ee7f31c0SAlexander Motin 		bio->bio_done	    = ctl_be_block_biodone;
1152ee7f31c0SAlexander Motin 		bio->bio_caller1    = beio;
11530bcd4ab6SAlexander Motin 		bio->bio_pblkno     = off / be_lun->cbe_lun.blocksize;
1154ee7f31c0SAlexander Motin 
1155ee7f31c0SAlexander Motin 		off += bio->bio_length;
1156ee7f31c0SAlexander Motin 		len -= bio->bio_length;
1157ee7f31c0SAlexander Motin 
115875c7a1d3SAlexander Motin 		mtx_lock(&be_lun->io_lock);
1159ee7f31c0SAlexander Motin 		beio->num_bios_sent++;
1160ee7f31c0SAlexander Motin 		if (last && len == 0)
1161ee7f31c0SAlexander Motin 			beio->send_complete = 1;
116275c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
1163ee7f31c0SAlexander Motin 
11643236151eSAlexander Motin 		if (csw) {
11653236151eSAlexander Motin 			csw->d_strategy(bio);
11663236151eSAlexander Motin 		} else {
11673236151eSAlexander Motin 			bio->bio_error = ENXIO;
11683236151eSAlexander Motin 			ctl_be_block_biodone(bio);
1169ee7f31c0SAlexander Motin 		}
1170ee7f31c0SAlexander Motin 	}
11713236151eSAlexander Motin 	if (csw)
11723236151eSAlexander Motin 		dev_relthread(dev, ref);
11733236151eSAlexander Motin }
1174ee7f31c0SAlexander Motin 
1175ee7f31c0SAlexander Motin static void
1176ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1177ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio)
1178ee7f31c0SAlexander Motin {
1179ee7f31c0SAlexander Motin 	union ctl_io *io;
118066df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1181ee7f31c0SAlexander Motin 	struct scsi_unmap_desc *buf, *end;
1182ee7f31c0SAlexander Motin 	uint64_t len;
1183ee7f31c0SAlexander Motin 
1184ee7f31c0SAlexander Motin 	io = beio->io;
1185ee7f31c0SAlexander Motin 
1186ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1187ee7f31c0SAlexander Motin 
1188ee7f31c0SAlexander Motin 	binuptime(&beio->ds_t0);
1189ee7f31c0SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1190ee7f31c0SAlexander Motin 
1191ee7f31c0SAlexander Motin 	if (beio->io_offset == -1) {
1192ee7f31c0SAlexander Motin 		beio->io_len = 0;
119366df9136SAlexander Motin 		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
119466df9136SAlexander Motin 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
119566df9136SAlexander Motin 		end = buf + ptrlen->len / sizeof(*buf);
1196ee7f31c0SAlexander Motin 		for (; buf < end; buf++) {
1197ee7f31c0SAlexander Motin 			len = (uint64_t)scsi_4btoul(buf->length) *
11980bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
1199ee7f31c0SAlexander Motin 			beio->io_len += len;
1200ee7f31c0SAlexander Motin 			ctl_be_block_unmap_dev_range(be_lun, beio,
12010bcd4ab6SAlexander Motin 			    scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
12020bcd4ab6SAlexander Motin 			    len, (end - buf < 2) ? TRUE : FALSE);
1203ee7f31c0SAlexander Motin 		}
1204ee7f31c0SAlexander Motin 	} else
1205ee7f31c0SAlexander Motin 		ctl_be_block_unmap_dev_range(be_lun, beio,
1206ee7f31c0SAlexander Motin 		    beio->io_offset, beio->io_len, TRUE);
1207ee7f31c0SAlexander Motin }
1208ee7f31c0SAlexander Motin 
1209ee7f31c0SAlexander Motin static void
1210130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1211130f4520SKenneth D. Merry 			  struct ctl_be_block_io *beio)
1212130f4520SKenneth D. Merry {
121375c7a1d3SAlexander Motin 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1214130f4520SKenneth D. Merry 	struct bio *bio;
12153236151eSAlexander Motin 	struct cdevsw *csw;
12163236151eSAlexander Motin 	struct cdev *dev;
1217130f4520SKenneth D. Merry 	off_t cur_offset;
12183236151eSAlexander Motin 	int i, max_iosize, ref;
1219130f4520SKenneth D. Merry 
1220130f4520SKenneth D. Merry 	DPRINTF("entered\n");
12213236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1222130f4520SKenneth D. Merry 
1223130f4520SKenneth D. Merry 	/*
1224130f4520SKenneth D. Merry 	 * We have to limit our I/O size to the maximum supported by the
12258054320eSAlexander Motin 	 * backend device.
1226130f4520SKenneth D. Merry 	 */
12273236151eSAlexander Motin 	if (csw) {
12283236151eSAlexander Motin 		max_iosize = dev->si_iosize_max;
1229b06771aaSAlexander Motin 		if (max_iosize <= 0)
1230130f4520SKenneth D. Merry 			max_iosize = DFLTPHYS;
12313236151eSAlexander Motin 	} else
1232b06771aaSAlexander Motin 		max_iosize = maxphys;
1233130f4520SKenneth D. Merry 
1234130f4520SKenneth D. Merry 	cur_offset = beio->io_offset;
1235130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
1236130f4520SKenneth D. Merry 		size_t cur_size;
1237130f4520SKenneth D. Merry 		uint8_t *cur_ptr;
1238130f4520SKenneth D. Merry 
1239130f4520SKenneth D. Merry 		cur_size = beio->sg_segs[i].len;
1240130f4520SKenneth D. Merry 		cur_ptr = beio->sg_segs[i].addr;
1241130f4520SKenneth D. Merry 
1242130f4520SKenneth D. Merry 		while (cur_size > 0) {
1243130f4520SKenneth D. Merry 			/* This can't fail, it's a blocking allocation. */
1244130f4520SKenneth D. Merry 			bio = g_alloc_bio();
1245130f4520SKenneth D. Merry 
1246130f4520SKenneth D. Merry 			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1247130f4520SKenneth D. Merry 
1248130f4520SKenneth D. Merry 			bio->bio_cmd = beio->bio_cmd;
12493236151eSAlexander Motin 			bio->bio_dev = dev;
1250130f4520SKenneth D. Merry 			bio->bio_caller1 = beio;
1251130f4520SKenneth D. Merry 			bio->bio_length = min(cur_size, max_iosize);
1252130f4520SKenneth D. Merry 			bio->bio_offset = cur_offset;
1253130f4520SKenneth D. Merry 			bio->bio_data = cur_ptr;
1254130f4520SKenneth D. Merry 			bio->bio_done = ctl_be_block_biodone;
12550bcd4ab6SAlexander Motin 			bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1256130f4520SKenneth D. Merry 
1257130f4520SKenneth D. Merry 			cur_offset += bio->bio_length;
1258130f4520SKenneth D. Merry 			cur_ptr += bio->bio_length;
1259130f4520SKenneth D. Merry 			cur_size -= bio->bio_length;
1260130f4520SKenneth D. Merry 
126175c7a1d3SAlexander Motin 			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1262130f4520SKenneth D. Merry 			beio->num_bios_sent++;
1263130f4520SKenneth D. Merry 		}
1264130f4520SKenneth D. Merry 	}
126575c7a1d3SAlexander Motin 	beio->send_complete = 1;
1266024932aaSAlexander Motin 	binuptime(&beio->ds_t0);
1267024932aaSAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
126875c7a1d3SAlexander Motin 
126975c7a1d3SAlexander Motin 	/*
127075c7a1d3SAlexander Motin 	 * Fire off all allocated requests!
127175c7a1d3SAlexander Motin 	 */
127275c7a1d3SAlexander Motin 	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
127375c7a1d3SAlexander Motin 		TAILQ_REMOVE(&queue, bio, bio_queue);
12743236151eSAlexander Motin 		if (csw)
12753236151eSAlexander Motin 			csw->d_strategy(bio);
12763236151eSAlexander Motin 		else {
12773236151eSAlexander Motin 			bio->bio_error = ENXIO;
12783236151eSAlexander Motin 			ctl_be_block_biodone(bio);
127975c7a1d3SAlexander Motin 		}
1280130f4520SKenneth D. Merry 	}
12813236151eSAlexander Motin 	if (csw)
12823236151eSAlexander Motin 		dev_relthread(dev, ref);
12833236151eSAlexander Motin }
1284130f4520SKenneth D. Merry 
1285c3e7ba3eSAlexander Motin static uint64_t
1286c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1287c3e7ba3eSAlexander Motin {
1288c3e7ba3eSAlexander Motin 	struct diocgattr_arg	arg;
12893236151eSAlexander Motin 	struct cdevsw *csw;
12903236151eSAlexander Motin 	struct cdev *dev;
12913236151eSAlexander Motin 	int error, ref;
1292c3e7ba3eSAlexander Motin 
12933236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
12943236151eSAlexander Motin 	if (csw == NULL)
1295c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1296c3e7ba3eSAlexander Motin 	strlcpy(arg.name, attrname, sizeof(arg.name));
1297c3e7ba3eSAlexander Motin 	arg.len = sizeof(arg.value.off);
12983236151eSAlexander Motin 	if (csw->d_ioctl) {
12993236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
13003236151eSAlexander Motin 		    curthread);
13013236151eSAlexander Motin 	} else
13023236151eSAlexander Motin 		error = ENODEV;
13033236151eSAlexander Motin 	dev_relthread(dev, ref);
1304c3e7ba3eSAlexander Motin 	if (error != 0)
1305c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1306c3e7ba3eSAlexander Motin 	return (arg.value.off);
1307c3e7ba3eSAlexander Motin }
1308c3e7ba3eSAlexander Motin 
1309130f4520SKenneth D. Merry static void
13107d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
13117d0d4342SAlexander Motin 			    union ctl_io *io)
13127d0d4342SAlexander Motin {
13130bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
13147d0d4342SAlexander Motin 	struct ctl_be_block_io *beio;
13157d0d4342SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
13167d0d4342SAlexander Motin 
13177d0d4342SAlexander Motin 	DPRINTF("entered\n");
13187d0d4342SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
13197d0d4342SAlexander Motin 	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
13207d0d4342SAlexander Motin 
13210bcd4ab6SAlexander Motin 	beio->io_len = lbalen->len * cbe_lun->blocksize;
13220bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13237d0d4342SAlexander Motin 	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
13247d0d4342SAlexander Motin 	beio->bio_cmd = BIO_FLUSH;
13257d0d4342SAlexander Motin 	beio->ds_trans_type = DEVSTAT_NO_DATA;
13267d0d4342SAlexander Motin 	DPRINTF("SYNC\n");
13277d0d4342SAlexander Motin 	be_lun->lun_flush(be_lun, beio);
13287d0d4342SAlexander Motin }
13297d0d4342SAlexander Motin 
13307d0d4342SAlexander Motin static void
1331ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1332ee7f31c0SAlexander Motin {
1333ee7f31c0SAlexander Motin 	union ctl_io *io;
1334ee7f31c0SAlexander Motin 
1335ee7f31c0SAlexander Motin 	io = beio->io;
1336ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1337ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1338ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1339ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1340ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1341ee7f31c0SAlexander Motin 		return;
1342ee7f31c0SAlexander Motin 	}
1343ee7f31c0SAlexander Motin 
1344ee7f31c0SAlexander Motin 	ctl_be_block_config_write(io);
1345ee7f31c0SAlexander Motin }
1346ee7f31c0SAlexander Motin 
1347ee7f31c0SAlexander Motin static void
1348ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1349ee7f31c0SAlexander Motin 			    union ctl_io *io)
1350ee7f31c0SAlexander Motin {
13510d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
13520bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1353ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
135466df9136SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1355fee04ef7SAlexander Motin 	uint64_t len_left, lba;
1356fee04ef7SAlexander Motin 	uint32_t pb, pbo, adj;
1357ee7f31c0SAlexander Motin 	int i, seglen;
1358ee7f31c0SAlexander Motin 	uint8_t *buf, *end;
1359ee7f31c0SAlexander Motin 
1360ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1361ee7f31c0SAlexander Motin 
1362e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1363a59e2982SAlexander Motin 	lbalen = ARGS(io);
1364ee7f31c0SAlexander Motin 
136564c5167cSAlexander Motin 	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
13663406a2a0SAlexander Motin 	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1367ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1368ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1369ee7f31c0SAlexander Motin 				      /*sks_valid*/ 1,
1370ee7f31c0SAlexander Motin 				      /*command*/ 1,
1371ee7f31c0SAlexander Motin 				      /*field*/ 1,
1372ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1373ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1374ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1375ee7f31c0SAlexander Motin 		return;
1376ee7f31c0SAlexander Motin 	}
1377ee7f31c0SAlexander Motin 
13783406a2a0SAlexander Motin 	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
13790bcd4ab6SAlexander Motin 		beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13800bcd4ab6SAlexander Motin 		beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1381ee7f31c0SAlexander Motin 		beio->bio_cmd = BIO_DELETE;
1382ee7f31c0SAlexander Motin 		beio->ds_trans_type = DEVSTAT_FREE;
1383ee7f31c0SAlexander Motin 
1384ee7f31c0SAlexander Motin 		be_lun->unmap(be_lun, beio);
1385ee7f31c0SAlexander Motin 		return;
1386ee7f31c0SAlexander Motin 	}
1387ee7f31c0SAlexander Motin 
1388ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_WRITE;
1389ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_WRITE;
1390ee7f31c0SAlexander Motin 
1391ee7f31c0SAlexander Motin 	DPRINTF("WRITE SAME at LBA %jx len %u\n",
139266df9136SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len);
1393ee7f31c0SAlexander Motin 
13940bcd4ab6SAlexander Motin 	pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
13950bcd4ab6SAlexander Motin 	if (be_lun->cbe_lun.pblockoff > 0)
13960bcd4ab6SAlexander Motin 		pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1397fee04ef7SAlexander Motin 	else
1398fee04ef7SAlexander Motin 		pbo = 0;
13990bcd4ab6SAlexander Motin 	len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1400b06771aaSAlexander Motin 	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1401ee7f31c0SAlexander Motin 		/*
1402ee7f31c0SAlexander Motin 		 * Setup the S/G entry for this chunk.
1403ee7f31c0SAlexander Motin 		 */
140408a7cce5SAlexander Motin 		seglen = MIN(CTLBLK_MAX_SEG, len_left);
14050bcd4ab6SAlexander Motin 		if (pb > cbe_lun->blocksize) {
14060bcd4ab6SAlexander Motin 			adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
140793b8c96cSAlexander Motin 			    seglen - pbo) % pb;
140893b8c96cSAlexander Motin 			if (seglen > adj)
140993b8c96cSAlexander Motin 				seglen -= adj;
141093b8c96cSAlexander Motin 			else
14110bcd4ab6SAlexander Motin 				seglen -= seglen % cbe_lun->blocksize;
141293b8c96cSAlexander Motin 		} else
14130bcd4ab6SAlexander Motin 			seglen -= seglen % cbe_lun->blocksize;
14148054320eSAlexander Motin 		ctl_alloc_seg(softc, &beio->sg_segs[i], seglen);
1415ee7f31c0SAlexander Motin 
1416ee7f31c0SAlexander Motin 		DPRINTF("segment %d addr %p len %zd\n", i,
1417ee7f31c0SAlexander Motin 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1418ee7f31c0SAlexander Motin 
1419ee7f31c0SAlexander Motin 		beio->num_segs++;
1420ee7f31c0SAlexander Motin 		len_left -= seglen;
1421ee7f31c0SAlexander Motin 
1422ee7f31c0SAlexander Motin 		buf = beio->sg_segs[i].addr;
1423ee7f31c0SAlexander Motin 		end = buf + seglen;
14240bcd4ab6SAlexander Motin 		for (; buf < end; buf += cbe_lun->blocksize) {
14256c2acea5SAlexander Motin 			if (lbalen->flags & SWS_NDOB) {
14266c2acea5SAlexander Motin 				memset(buf, 0, cbe_lun->blocksize);
14276c2acea5SAlexander Motin 			} else {
14286c2acea5SAlexander Motin 				memcpy(buf, io->scsiio.kern_data_ptr,
14296c2acea5SAlexander Motin 				    cbe_lun->blocksize);
14306c2acea5SAlexander Motin 			}
143166df9136SAlexander Motin 			if (lbalen->flags & SWS_LBDATA)
143266df9136SAlexander Motin 				scsi_ulto4b(lbalen->lba + lba, buf);
1433ee7f31c0SAlexander Motin 			lba++;
1434ee7f31c0SAlexander Motin 		}
1435ee7f31c0SAlexander Motin 	}
1436ee7f31c0SAlexander Motin 
14370bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
14380bcd4ab6SAlexander Motin 	beio->io_len = lba * cbe_lun->blocksize;
1439ee7f31c0SAlexander Motin 
1440ee7f31c0SAlexander Motin 	/* We can not do all in one run. Correct and schedule rerun. */
1441ee7f31c0SAlexander Motin 	if (len_left > 0) {
144266df9136SAlexander Motin 		lbalen->lba += lba;
144366df9136SAlexander Motin 		lbalen->len -= lba;
1444ee7f31c0SAlexander Motin 		beio->beio_cont = ctl_be_block_cw_done_ws;
1445ee7f31c0SAlexander Motin 	}
1446ee7f31c0SAlexander Motin 
1447ee7f31c0SAlexander Motin 	be_lun->dispatch(be_lun, beio);
1448ee7f31c0SAlexander Motin }
1449ee7f31c0SAlexander Motin 
1450ee7f31c0SAlexander Motin static void
1451ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1452ee7f31c0SAlexander Motin 			    union ctl_io *io)
1453ee7f31c0SAlexander Motin {
1454ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
145566df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1456ee7f31c0SAlexander Motin 
1457ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1458ee7f31c0SAlexander Motin 
1459e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
146066df9136SAlexander Motin 	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1461ee7f31c0SAlexander Motin 
14623406a2a0SAlexander Motin 	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1463ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1464ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1465ee7f31c0SAlexander Motin 				      /*sks_valid*/ 0,
1466ee7f31c0SAlexander Motin 				      /*command*/ 1,
1467ee7f31c0SAlexander Motin 				      /*field*/ 0,
1468ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1469ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1470ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1471ee7f31c0SAlexander Motin 		return;
1472ee7f31c0SAlexander Motin 	}
1473ee7f31c0SAlexander Motin 
1474ee7f31c0SAlexander Motin 	beio->io_len = 0;
1475ee7f31c0SAlexander Motin 	beio->io_offset = -1;
1476ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_DELETE;
1477ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_FREE;
147866df9136SAlexander Motin 	DPRINTF("UNMAP\n");
1479ee7f31c0SAlexander Motin 	be_lun->unmap(be_lun, beio);
1480ee7f31c0SAlexander Motin }
1481ee7f31c0SAlexander Motin 
1482ee7f31c0SAlexander Motin static void
1483ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1484ef8daf3fSAlexander Motin {
1485ef8daf3fSAlexander Motin 	union ctl_io *io;
1486ef8daf3fSAlexander Motin 
1487ef8daf3fSAlexander Motin 	io = beio->io;
1488ef8daf3fSAlexander Motin 	ctl_free_beio(beio);
1489ef8daf3fSAlexander Motin 	ctl_config_read_done(io);
1490ef8daf3fSAlexander Motin }
1491ef8daf3fSAlexander Motin 
1492ef8daf3fSAlexander Motin static void
1493ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1494ef8daf3fSAlexander Motin 			 union ctl_io *io)
1495ef8daf3fSAlexander Motin {
1496ef8daf3fSAlexander Motin 	struct ctl_be_block_io *beio;
1497ef8daf3fSAlexander Motin 	struct ctl_be_block_softc *softc;
1498ef8daf3fSAlexander Motin 
1499ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1500ef8daf3fSAlexander Motin 
1501ef8daf3fSAlexander Motin 	softc = be_lun->softc;
1502ef8daf3fSAlexander Motin 	beio = ctl_alloc_beio(softc);
1503ef8daf3fSAlexander Motin 	beio->io = io;
1504ef8daf3fSAlexander Motin 	beio->lun = be_lun;
1505ef8daf3fSAlexander Motin 	beio->beio_cont = ctl_be_block_cr_done;
1506ef8daf3fSAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1507ef8daf3fSAlexander Motin 
1508ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
1509ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1510ef8daf3fSAlexander Motin 		beio->bio_cmd = -1;
1511ef8daf3fSAlexander Motin 		beio->ds_trans_type = DEVSTAT_NO_DATA;
1512ef8daf3fSAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1513ef8daf3fSAlexander Motin 		beio->io_len = 0;
1514ef8daf3fSAlexander Motin 		if (be_lun->get_lba_status)
1515ef8daf3fSAlexander Motin 			be_lun->get_lba_status(be_lun, beio);
1516ef8daf3fSAlexander Motin 		else
1517ef8daf3fSAlexander Motin 			ctl_be_block_cr_done(beio);
1518ef8daf3fSAlexander Motin 		break;
1519ef8daf3fSAlexander Motin 	default:
1520ef8daf3fSAlexander Motin 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1521ef8daf3fSAlexander Motin 		break;
1522ef8daf3fSAlexander Motin 	}
1523ef8daf3fSAlexander Motin }
1524ef8daf3fSAlexander Motin 
1525ef8daf3fSAlexander Motin static void
1526ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1527ee7f31c0SAlexander Motin {
1528ee7f31c0SAlexander Motin 	union ctl_io *io;
1529ee7f31c0SAlexander Motin 
1530ee7f31c0SAlexander Motin 	io = beio->io;
1531ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1532ee7f31c0SAlexander Motin 	ctl_config_write_done(io);
1533ee7f31c0SAlexander Motin }
1534ee7f31c0SAlexander Motin 
1535ee7f31c0SAlexander Motin static void
1536130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1537130f4520SKenneth D. Merry 			 union ctl_io *io)
1538130f4520SKenneth D. Merry {
1539130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1540130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1541130f4520SKenneth D. Merry 
1542130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1543130f4520SKenneth D. Merry 
1544130f4520SKenneth D. Merry 	softc = be_lun->softc;
1545130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1546130f4520SKenneth D. Merry 	beio->io = io;
1547130f4520SKenneth D. Merry 	beio->lun = be_lun;
1548ee7f31c0SAlexander Motin 	beio->beio_cont = ctl_be_block_cw_done;
15497d0d4342SAlexander Motin 	switch (io->scsiio.tag_type) {
15507d0d4342SAlexander Motin 	case CTL_TAG_ORDERED:
15517d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
15527d0d4342SAlexander Motin 		break;
15537d0d4342SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
15547d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
15557d0d4342SAlexander Motin 		break;
15567d0d4342SAlexander Motin 	case CTL_TAG_UNTAGGED:
15577d0d4342SAlexander Motin 	case CTL_TAG_SIMPLE:
15587d0d4342SAlexander Motin 	case CTL_TAG_ACA:
15597d0d4342SAlexander Motin 	default:
15607d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
15617d0d4342SAlexander Motin 		break;
15627d0d4342SAlexander Motin 	}
1563e86a4142SAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1564130f4520SKenneth D. Merry 
1565130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
1566130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
1567130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
15687d0d4342SAlexander Motin 		ctl_be_block_cw_dispatch_sync(be_lun, io);
1569130f4520SKenneth D. Merry 		break;
1570ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
1571ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
1572ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_ws(be_lun, io);
1573ee7f31c0SAlexander Motin 		break;
1574ee7f31c0SAlexander Motin 	case UNMAP:
1575ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1576ee7f31c0SAlexander Motin 		break;
1577130f4520SKenneth D. Merry 	default:
1578130f4520SKenneth D. Merry 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1579130f4520SKenneth D. Merry 		break;
1580130f4520SKenneth D. Merry 	}
1581130f4520SKenneth D. Merry }
1582130f4520SKenneth D. Merry 
158336160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
158436160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
158536160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
158636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1587130f4520SKenneth D. Merry 
1588130f4520SKenneth D. Merry static void
158908a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio)
159008a7cce5SAlexander Motin {
159108a7cce5SAlexander Motin 	struct ctl_be_block_lun *be_lun;
159208a7cce5SAlexander Motin 	union ctl_io *io;
159308a7cce5SAlexander Motin 
159408a7cce5SAlexander Motin 	io = beio->io;
159508a7cce5SAlexander Motin 	be_lun = beio->lun;
159608a7cce5SAlexander Motin 	ctl_free_beio(beio);
1597ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1598ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1599ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
160011b569f7SAlexander Motin 		ctl_data_submit_done(io);
160108a7cce5SAlexander Motin 		return;
160208a7cce5SAlexander Motin 	}
160308a7cce5SAlexander Motin 
160408a7cce5SAlexander Motin 	io->io_hdr.status &= ~CTL_STATUS_MASK;
160508a7cce5SAlexander Motin 	io->io_hdr.status |= CTL_STATUS_NONE;
160608a7cce5SAlexander Motin 
160775c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
160808a7cce5SAlexander Motin 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
160975c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
161008a7cce5SAlexander Motin 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
161108a7cce5SAlexander Motin }
161208a7cce5SAlexander Motin 
161308a7cce5SAlexander Motin static void
1614130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1615130f4520SKenneth D. Merry 			   union ctl_io *io)
1616130f4520SKenneth D. Merry {
16170bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1618130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1619130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
162011b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1621e86a4142SAlexander Motin 	struct ctl_ptr_len_flags *bptrlen;
1622e86a4142SAlexander Motin 	uint64_t len_left, lbas;
1623130f4520SKenneth D. Merry 	int i;
1624130f4520SKenneth D. Merry 
1625130f4520SKenneth D. Merry 	softc = be_lun->softc;
1626130f4520SKenneth D. Merry 
1627130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1628130f4520SKenneth D. Merry 
162911b569f7SAlexander Motin 	lbalen = ARGS(io);
163011b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
163136160958SMark Johnston 		SDT_PROBE0(cbb, , write, start);
163211b569f7SAlexander Motin 	} else {
163336160958SMark Johnston 		SDT_PROBE0(cbb, , read, start);
1634130f4520SKenneth D. Merry 	}
1635130f4520SKenneth D. Merry 
1636130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1637130f4520SKenneth D. Merry 	beio->io = io;
1638130f4520SKenneth D. Merry 	beio->lun = be_lun;
1639e86a4142SAlexander Motin 	bptrlen = PRIV(io);
1640e86a4142SAlexander Motin 	bptrlen->ptr = (void *)beio;
1641130f4520SKenneth D. Merry 
1642130f4520SKenneth D. Merry 	switch (io->scsiio.tag_type) {
1643130f4520SKenneth D. Merry 	case CTL_TAG_ORDERED:
1644130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1645130f4520SKenneth D. Merry 		break;
1646130f4520SKenneth D. Merry 	case CTL_TAG_HEAD_OF_QUEUE:
1647130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1648130f4520SKenneth D. Merry 		break;
1649130f4520SKenneth D. Merry 	case CTL_TAG_UNTAGGED:
1650130f4520SKenneth D. Merry 	case CTL_TAG_SIMPLE:
1651130f4520SKenneth D. Merry 	case CTL_TAG_ACA:
1652130f4520SKenneth D. Merry 	default:
1653130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1654130f4520SKenneth D. Merry 		break;
1655130f4520SKenneth D. Merry 	}
1656130f4520SKenneth D. Merry 
165711b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1658130f4520SKenneth D. Merry 		beio->bio_cmd = BIO_WRITE;
1659130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_WRITE;
166011b569f7SAlexander Motin 	} else {
166111b569f7SAlexander Motin 		beio->bio_cmd = BIO_READ;
166211b569f7SAlexander Motin 		beio->ds_trans_type = DEVSTAT_READ;
1663130f4520SKenneth D. Merry 	}
1664130f4520SKenneth D. Merry 
166508a7cce5SAlexander Motin 	DPRINTF("%s at LBA %jx len %u @%ju\n",
1666130f4520SKenneth D. Merry 	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1667e86a4142SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
1668b06771aaSAlexander Motin 	lbas = CTLBLK_MAX_IO_SIZE;
16690d7fed74SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE) {
16700d7fed74SAlexander Motin 		beio->two_sglists = 1;
1671b06771aaSAlexander Motin 		lbas /= 2;
16720d7fed74SAlexander Motin 	}
16730bcd4ab6SAlexander Motin 	lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
16740bcd4ab6SAlexander Motin 	beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
16750bcd4ab6SAlexander Motin 	beio->io_len = lbas * cbe_lun->blocksize;
1676e86a4142SAlexander Motin 	bptrlen->len += lbas;
1677130f4520SKenneth D. Merry 
167808a7cce5SAlexander Motin 	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
167908a7cce5SAlexander Motin 		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
168008a7cce5SAlexander Motin 		    i, CTLBLK_MAX_SEGS));
1681130f4520SKenneth D. Merry 
1682130f4520SKenneth D. Merry 		/*
1683130f4520SKenneth D. Merry 		 * Setup the S/G entry for this chunk.
1684130f4520SKenneth D. Merry 		 */
16858054320eSAlexander Motin 		ctl_alloc_seg(softc, &beio->sg_segs[i],
1686cd853791SKonstantin Belousov 		    MIN(CTLBLK_MAX_SEG, len_left));
1687130f4520SKenneth D. Merry 
1688130f4520SKenneth D. Merry 		DPRINTF("segment %d addr %p len %zd\n", i,
1689130f4520SKenneth D. Merry 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1690130f4520SKenneth D. Merry 
169111b569f7SAlexander Motin 		/* Set up second segment for compare operation. */
16920d7fed74SAlexander Motin 		if (beio->two_sglists) {
16938054320eSAlexander Motin 			ctl_alloc_seg(softc,
16948054320eSAlexander Motin 			    &beio->sg_segs[i + CTLBLK_HALF_SEGS],
16958054320eSAlexander Motin 			    beio->sg_segs[i].len);
169611b569f7SAlexander Motin 		}
169711b569f7SAlexander Motin 
1698130f4520SKenneth D. Merry 		beio->num_segs++;
1699130f4520SKenneth D. Merry 		len_left -= beio->sg_segs[i].len;
1700130f4520SKenneth D. Merry 	}
1701e86a4142SAlexander Motin 	if (bptrlen->len < lbalen->len)
170208a7cce5SAlexander Motin 		beio->beio_cont = ctl_be_block_next;
170308a7cce5SAlexander Motin 	io->scsiio.be_move_done = ctl_be_block_move_done;
170411b569f7SAlexander Motin 	/* For compare we have separate S/G lists for read and datamove. */
17050d7fed74SAlexander Motin 	if (beio->two_sglists)
170611b569f7SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
170711b569f7SAlexander Motin 	else
170808a7cce5SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
170908a7cce5SAlexander Motin 	io->scsiio.kern_data_len = beio->io_len;
171008a7cce5SAlexander Motin 	io->scsiio.kern_sg_entries = beio->num_segs;
17119a4510acSAlexander Motin 	io->scsiio.kern_data_ref = ctl_refcnt_beio;
17129a4510acSAlexander Motin 	io->scsiio.kern_data_arg = beio;
1713b2221369SAlexander Motin 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1714130f4520SKenneth D. Merry 
1715130f4520SKenneth D. Merry 	/*
1716130f4520SKenneth D. Merry 	 * For the read case, we need to read the data into our buffers and
1717130f4520SKenneth D. Merry 	 * then we can send it back to the user.  For the write case, we
1718130f4520SKenneth D. Merry 	 * need to get the data from the user first.
1719130f4520SKenneth D. Merry 	 */
1720130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
172136160958SMark Johnston 		SDT_PROBE0(cbb, , read, alloc_done);
1722130f4520SKenneth D. Merry 		be_lun->dispatch(be_lun, beio);
1723130f4520SKenneth D. Merry 	} else {
172436160958SMark Johnston 		SDT_PROBE0(cbb, , write, alloc_done);
1725130f4520SKenneth D. Merry 		ctl_datamove(io);
1726130f4520SKenneth D. Merry 	}
1727130f4520SKenneth D. Merry }
1728130f4520SKenneth D. Merry 
1729130f4520SKenneth D. Merry static void
1730130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending)
1731130f4520SKenneth D. Merry {
1732ee4ad294SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1733ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1734130f4520SKenneth D. Merry 	union ctl_io *io;
1735130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1736130f4520SKenneth D. Merry 
1737ee4ad294SAlexander Motin 	DPRINTF("entered\n");
1738ee4ad294SAlexander Motin 	/*
1739ee4ad294SAlexander Motin 	 * Fetch and process I/Os from all queues.  If we detect LUN
1740648dfc1aSAlexander Motin 	 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1741ee4ad294SAlexander Motin 	 * so make response maximally opaque to not confuse initiator.
1742ee4ad294SAlexander Motin 	 */
1743ee4ad294SAlexander Motin 	for (;;) {
1744ee4ad294SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
1745ee4ad294SAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1746ee4ad294SAlexander Motin 		if (io != NULL) {
1747130f4520SKenneth D. Merry 			DPRINTF("datamove queue\n");
174805d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->datamove_queue, links);
174975c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1750e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1751648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1752ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1753ee4ad294SAlexander Motin 				ctl_complete_beio(beio);
17546ed39db2SAlexander Motin 				continue;
1755ee4ad294SAlexander Motin 			}
1756130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1757130f4520SKenneth D. Merry 			continue;
1758130f4520SKenneth D. Merry 		}
1759130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1760130f4520SKenneth D. Merry 		if (io != NULL) {
1761130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
176205d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->config_write_queue, links);
176375c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1764648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1765ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1766ee4ad294SAlexander Motin 				ctl_config_write_done(io);
17676ed39db2SAlexander Motin 				continue;
1768ee4ad294SAlexander Motin 			}
1769130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1770ef8daf3fSAlexander Motin 			continue;
1771ef8daf3fSAlexander Motin 		}
1772ef8daf3fSAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1773ef8daf3fSAlexander Motin 		if (io != NULL) {
1774ef8daf3fSAlexander Motin 			DPRINTF("config read queue\n");
177505d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->config_read_queue, links);
1776ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1777648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1778ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1779ee4ad294SAlexander Motin 				ctl_config_read_done(io);
17806ed39db2SAlexander Motin 				continue;
1781ee4ad294SAlexander Motin 			}
1782ef8daf3fSAlexander Motin 			ctl_be_block_cr_dispatch(be_lun, io);
1783130f4520SKenneth D. Merry 			continue;
1784130f4520SKenneth D. Merry 		}
1785130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1786130f4520SKenneth D. Merry 		if (io != NULL) {
1787130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
178805d882b7SAlexander Motin 			STAILQ_REMOVE_HEAD(&be_lun->input_queue, links);
178975c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1790648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1791ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1792ee4ad294SAlexander Motin 				ctl_data_submit_done(io);
17936ed39db2SAlexander Motin 				continue;
1794ee4ad294SAlexander Motin 			}
1795130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1796130f4520SKenneth D. Merry 			continue;
1797130f4520SKenneth D. Merry 		}
1798130f4520SKenneth D. Merry 
1799130f4520SKenneth D. Merry 		/*
1800130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1801130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1802130f4520SKenneth D. Merry 		 */
1803ee4ad294SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
1804130f4520SKenneth D. Merry 		break;
1805130f4520SKenneth D. Merry 	}
1806130f4520SKenneth D. Merry }
1807130f4520SKenneth D. Merry 
1808130f4520SKenneth D. Merry /*
1809130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1810130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1811130f4520SKenneth D. Merry  * thread.
1812130f4520SKenneth D. Merry  */
1813130f4520SKenneth D. Merry static int
1814130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1815130f4520SKenneth D. Merry {
1816130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1817130f4520SKenneth D. Merry 
1818130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1819130f4520SKenneth D. Merry 
1820767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1821130f4520SKenneth D. Merry 
182205d882b7SAlexander Motin 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
182305d882b7SAlexander Motin 	    ("%s: unexpected I/O type %x", __func__, io->io_hdr.io_type));
1824130f4520SKenneth D. Merry 
1825e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1826e86a4142SAlexander Motin 
182775c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
1828130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
182975c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
1830130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1831130f4520SKenneth D. Merry 
18329c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1833130f4520SKenneth D. Merry }
1834130f4520SKenneth D. Merry 
1835130f4520SKenneth D. Merry static int
1836130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1837130f4520SKenneth D. Merry 			int flag, struct thread *td)
1838130f4520SKenneth D. Merry {
183934144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
1840130f4520SKenneth D. Merry 	int error;
1841130f4520SKenneth D. Merry 
1842130f4520SKenneth D. Merry 	error = 0;
1843130f4520SKenneth D. Merry 	switch (cmd) {
1844130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1845130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1846130f4520SKenneth D. Merry 
1847130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1848130f4520SKenneth D. Merry 
1849130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1850130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1851130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1852130f4520SKenneth D. Merry 			break;
1853130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1854130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1855130f4520SKenneth D. Merry 			break;
185681177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
185781177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
185881177295SEdward Tomasz Napierala 			break;
1859130f4520SKenneth D. Merry 		default:
1860130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1861130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
186219720f41SAlexander Motin 				 "invalid LUN request type %d",
1863130f4520SKenneth D. Merry 				 lun_req->reqtype);
1864130f4520SKenneth D. Merry 			break;
1865130f4520SKenneth D. Merry 		}
1866130f4520SKenneth D. Merry 		break;
1867130f4520SKenneth D. Merry 	}
1868130f4520SKenneth D. Merry 	default:
1869130f4520SKenneth D. Merry 		error = ENOTTY;
1870130f4520SKenneth D. Merry 		break;
1871130f4520SKenneth D. Merry 	}
1872130f4520SKenneth D. Merry 
1873130f4520SKenneth D. Merry 	return (error);
1874130f4520SKenneth D. Merry }
1875130f4520SKenneth D. Merry 
1876130f4520SKenneth D. Merry static int
1877130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1878130f4520SKenneth D. Merry {
18790bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1880130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1881130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
18828951f055SMarcelo Araujo 	const char		     *value;
1883130f4520SKenneth D. Merry 	struct vattr		      vattr;
188434961f40SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1885130f4520SKenneth D. Merry 	int			      error;
188649050613SKa Ho Ng 	long			      pconf;
1887130f4520SKenneth D. Merry 
18880bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
1889130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
189019720f41SAlexander Motin 	params = &be_lun->params;
1891130f4520SKenneth D. Merry 
1892130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1893130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1894130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1895ef8daf3fSAlexander Motin 	be_lun->get_lba_status = ctl_be_block_gls_file;
189653c146deSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_file;
189749050613SKa Ho Ng 	be_lun->unmap = ctl_be_block_unmap_file;
18980bcd4ab6SAlexander Motin 	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1899130f4520SKenneth D. Merry 
1900130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1901130f4520SKenneth D. Merry 	if (error != 0) {
1902130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1903130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1904130f4520SKenneth D. Merry 			 be_lun->dev_path);
1905130f4520SKenneth D. Merry 		return (error);
1906130f4520SKenneth D. Merry 	}
1907130f4520SKenneth D. Merry 
190849050613SKa Ho Ng 	error = VOP_PATHCONF(be_lun->vn, _PC_DEALLOC_PRESENT, &pconf);
190949050613SKa Ho Ng 	if (error != 0) {
191049050613SKa Ho Ng 		snprintf(req->error_str, sizeof(req->error_str),
191149050613SKa Ho Ng 		    "error calling VOP_PATHCONF() for file %s",
191249050613SKa Ho Ng 		    be_lun->dev_path);
191349050613SKa Ho Ng 		return (error);
191449050613SKa Ho Ng 	}
191549050613SKa Ho Ng 	if (pconf == 1)
191649050613SKa Ho Ng 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
191749050613SKa Ho Ng 
1918130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
191981177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
192081177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
192181177295SEdward Tomasz Napierala 	else
1922130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1923130f4520SKenneth D. Merry 
1924130f4520SKenneth D. Merry 	/*
192520a28d6cSAlexander Motin 	 * For files we can use any logical block size.  Prefer 512 bytes
192620a28d6cSAlexander Motin 	 * for compatibility reasons.  If file's vattr.va_blocksize
192720a28d6cSAlexander Motin 	 * (preferred I/O block size) is bigger and multiple to chosen
192820a28d6cSAlexander Motin 	 * logical block size -- report it as physical block size.
1929130f4520SKenneth D. Merry 	 */
1930130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
19310bcd4ab6SAlexander Motin 		cbe_lun->blocksize = params->blocksize_bytes;
193291be33dcSAlexander Motin 	else if (cbe_lun->lun_type == T_CDROM)
193391be33dcSAlexander Motin 		cbe_lun->blocksize = 2048;
1934130f4520SKenneth D. Merry 	else
19350bcd4ab6SAlexander Motin 		cbe_lun->blocksize = 512;
19360bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
19370bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
19380bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
193934961f40SAlexander Motin 
194034961f40SAlexander Motin 	us = ps = vattr.va_blocksize;
194134961f40SAlexander Motin 	uo = po = 0;
194234961f40SAlexander Motin 
19438951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
194434961f40SAlexander Motin 	if (value != NULL)
194534961f40SAlexander Motin 		ctl_expand_number(value, &ps);
19468951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
194734961f40SAlexander Motin 	if (value != NULL)
194834961f40SAlexander Motin 		ctl_expand_number(value, &po);
19490bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
19500bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
19510bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
19520bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
19530bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
19540bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
195534961f40SAlexander Motin 	}
195634961f40SAlexander Motin 
19578951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
195834961f40SAlexander Motin 	if (value != NULL)
195934961f40SAlexander Motin 		ctl_expand_number(value, &us);
19608951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
196134961f40SAlexander Motin 	if (value != NULL)
196234961f40SAlexander Motin 		ctl_expand_number(value, &uo);
19630bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
19640bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
19650bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
19660bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
19670bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
19680bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
196920a28d6cSAlexander Motin 	}
1970130f4520SKenneth D. Merry 
1971130f4520SKenneth D. Merry 	/*
1972130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1973130f4520SKenneth D. Merry 	 * sector long.
1974130f4520SKenneth D. Merry 	 */
19750bcd4ab6SAlexander Motin 	if (be_lun->size_bytes < cbe_lun->blocksize) {
1976130f4520SKenneth D. Merry 		error = EINVAL;
1977130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1978130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
19790bcd4ab6SAlexander Motin 			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1980130f4520SKenneth D. Merry 	}
1981cb8727e2SAlexander Motin 
19820bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1983130f4520SKenneth D. Merry 	return (error);
1984130f4520SKenneth D. Merry }
1985130f4520SKenneth D. Merry 
1986130f4520SKenneth D. Merry static int
1987130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1988130f4520SKenneth D. Merry {
19890bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1990130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
19913236151eSAlexander Motin 	struct cdevsw		     *csw;
1992130f4520SKenneth D. Merry 	struct cdev		     *dev;
19938951f055SMarcelo Araujo 	const char		     *value;
19943236151eSAlexander Motin 	int			      error, atomic, maxio, ref, unmap, tmp;
1995f6295033SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1996130f4520SKenneth D. Merry 
199719720f41SAlexander Motin 	params = &be_lun->params;
1998130f4520SKenneth D. Merry 
1999130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
20003236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
20013236151eSAlexander Motin 	if (csw == NULL)
20023236151eSAlexander Motin 		return (ENXIO);
20033236151eSAlexander Motin 	if (strcmp(csw->d_name, "zvol") == 0) {
200467f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_zvol;
2005ef8daf3fSAlexander Motin 		be_lun->get_lba_status = ctl_be_block_gls_zvol;
2006cb8727e2SAlexander Motin 		atomic = maxio = CTLBLK_MAX_IO_SIZE;
2007cb8727e2SAlexander Motin 	} else {
200867f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_dev;
20090bcd4ab6SAlexander Motin 		be_lun->get_lba_status = NULL;
2010cb8727e2SAlexander Motin 		atomic = 0;
20113236151eSAlexander Motin 		maxio = dev->si_iosize_max;
2012cb8727e2SAlexander Motin 		if (maxio <= 0)
2013cb8727e2SAlexander Motin 			maxio = DFLTPHYS;
20148054320eSAlexander Motin 		if (maxio > CTLBLK_MAX_SEG)
20158054320eSAlexander Motin 			maxio = CTLBLK_MAX_SEG;
2016cb8727e2SAlexander Motin 	}
201767f586a8SAlexander Motin 	be_lun->lun_flush = ctl_be_block_flush_dev;
2018c3e7ba3eSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_dev;
20190bcd4ab6SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
2020130f4520SKenneth D. Merry 
20213236151eSAlexander Motin 	if (!csw->d_ioctl) {
20223236151eSAlexander Motin 		dev_relthread(dev, ref);
2023130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
20243236151eSAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
2025130f4520SKenneth D. Merry 		return (ENODEV);
2026130f4520SKenneth D. Merry 	}
2027130f4520SKenneth D. Merry 
20283236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
2029130f4520SKenneth D. Merry 			       curthread);
2030130f4520SKenneth D. Merry 	if (error) {
20313236151eSAlexander Motin 		dev_relthread(dev, ref);
2032130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
203319720f41SAlexander Motin 			 "error %d returned for DIOCGSECTORSIZE ioctl "
203419720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
2035130f4520SKenneth D. Merry 		return (error);
2036130f4520SKenneth D. Merry 	}
2037130f4520SKenneth D. Merry 
2038130f4520SKenneth D. Merry 	/*
2039130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
2040130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
2041130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
2042130f4520SKenneth D. Merry 	 * device's blocksize.
2043130f4520SKenneth D. Merry 	 */
2044a15bbf15SAlexander Motin 	if ((params->blocksize_bytes != 0) &&
2045a15bbf15SAlexander Motin 	    (params->blocksize_bytes >= tmp)) {
2046a15bbf15SAlexander Motin 		if (params->blocksize_bytes % tmp == 0) {
20470bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
2048130f4520SKenneth D. Merry 		} else {
20493236151eSAlexander Motin 			dev_relthread(dev, ref);
2050130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
205119720f41SAlexander Motin 				 "requested blocksize %u is not an even "
2052130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
2053f6295033SAlexander Motin 				 params->blocksize_bytes, tmp);
2054130f4520SKenneth D. Merry 			return (EINVAL);
2055130f4520SKenneth D. Merry 		}
2056a15bbf15SAlexander Motin 	} else if (params->blocksize_bytes != 0) {
20573236151eSAlexander Motin 		dev_relthread(dev, ref);
2058130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
205919720f41SAlexander Motin 			 "requested blocksize %u < backing device "
2060f6295033SAlexander Motin 			 "blocksize %u", params->blocksize_bytes, tmp);
2061130f4520SKenneth D. Merry 		return (EINVAL);
206291be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_CDROM)
206391be33dcSAlexander Motin 		cbe_lun->blocksize = MAX(tmp, 2048);
206491be33dcSAlexander Motin 	else
20650bcd4ab6SAlexander Motin 		cbe_lun->blocksize = tmp;
2066130f4520SKenneth D. Merry 
20673236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
2068130f4520SKenneth D. Merry 			     curthread);
2069130f4520SKenneth D. Merry 	if (error) {
20703236151eSAlexander Motin 		dev_relthread(dev, ref);
2071130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
207219720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE "
207319720f41SAlexander Motin 			 " ioctl on %s!", error,
207481177295SEdward Tomasz Napierala 			 be_lun->dev_path);
2075130f4520SKenneth D. Merry 		return (error);
2076130f4520SKenneth D. Merry 	}
2077130f4520SKenneth D. Merry 
207881177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
2079f6295033SAlexander Motin 		if (params->lun_size_bytes > otmp) {
20803236151eSAlexander Motin 			dev_relthread(dev, ref);
208181177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
208219720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
208319720f41SAlexander Motin 				 "size %ju",
208481177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
2085f6295033SAlexander Motin 				 (uintmax_t)otmp);
208681177295SEdward Tomasz Napierala 			return (EINVAL);
2087130f4520SKenneth D. Merry 		}
2088130f4520SKenneth D. Merry 
208981177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
2090a15bbf15SAlexander Motin 	} else
2091f6295033SAlexander Motin 		be_lun->size_bytes = otmp;
20920bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
20930bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
20940bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
209581177295SEdward Tomasz Napierala 
20963236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
20973236151eSAlexander Motin 	    curthread);
2098f6012722SAlexander Motin 	if (error)
2099f6012722SAlexander Motin 		ps = po = 0;
2100f6012722SAlexander Motin 	else {
21013236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
21023236151eSAlexander Motin 		    FREAD, curthread);
2103f6012722SAlexander Motin 		if (error)
2104f6012722SAlexander Motin 			po = 0;
2105f6012722SAlexander Motin 	}
210634961f40SAlexander Motin 	us = ps;
210734961f40SAlexander Motin 	uo = po;
210834961f40SAlexander Motin 
21098951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
211034961f40SAlexander Motin 	if (value != NULL)
211134961f40SAlexander Motin 		ctl_expand_number(value, &ps);
21128951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
211334961f40SAlexander Motin 	if (value != NULL)
211434961f40SAlexander Motin 		ctl_expand_number(value, &po);
21150bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
21160bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
21170bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
21180bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
21190bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
21200bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
2121f6012722SAlexander Motin 	}
2122f6012722SAlexander Motin 
21238951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
212434961f40SAlexander Motin 	if (value != NULL)
212534961f40SAlexander Motin 		ctl_expand_number(value, &us);
21268951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
212734961f40SAlexander Motin 	if (value != NULL)
212834961f40SAlexander Motin 		ctl_expand_number(value, &uo);
21290bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
21300bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
21310bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
21320bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
21330bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
21340bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
213534961f40SAlexander Motin 	}
213634961f40SAlexander Motin 
21370bcd4ab6SAlexander Motin 	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
21380bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2139fbc8d4ffSAlexander Motin 
2140fbc8d4ffSAlexander Motin 	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2141fbc8d4ffSAlexander Motin 		unmap = 1;
2142fbc8d4ffSAlexander Motin 	} else {
2143fbc8d4ffSAlexander Motin 		struct diocgattr_arg	arg;
2144fbc8d4ffSAlexander Motin 
2145fbc8d4ffSAlexander Motin 		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2146fbc8d4ffSAlexander Motin 		arg.len = sizeof(arg.value.i);
21473236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
21483236151eSAlexander Motin 		    curthread);
2149fbc8d4ffSAlexander Motin 		unmap = (error == 0) ? arg.value.i : 0;
2150fbc8d4ffSAlexander Motin 	}
21518951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2152fbc8d4ffSAlexander Motin 	if (value != NULL)
2153fbc8d4ffSAlexander Motin 		unmap = (strcmp(value, "on") == 0);
2154fbc8d4ffSAlexander Motin 	if (unmap)
21550bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
21560bcd4ab6SAlexander Motin 	else
21570bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2158fbc8d4ffSAlexander Motin 
21593236151eSAlexander Motin 	dev_relthread(dev, ref);
216081177295SEdward Tomasz Napierala 	return (0);
216181177295SEdward Tomasz Napierala }
2162130f4520SKenneth D. Merry 
2163130f4520SKenneth D. Merry static int
2164130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2165130f4520SKenneth D. Merry {
21660bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
21670bcd4ab6SAlexander Motin 	int flags;
2168130f4520SKenneth D. Merry 
21690bcd4ab6SAlexander Motin 	if (be_lun->vn) {
21700bcd4ab6SAlexander Motin 		flags = FREAD;
21710bcd4ab6SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
21720bcd4ab6SAlexander Motin 			flags |= FWRITE;
2173130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2174130f4520SKenneth D. Merry 		be_lun->vn = NULL;
2175130f4520SKenneth D. Merry 
2176130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
2177130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
2178130f4520SKenneth D. Merry 			break;
2179130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
2180130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
2181130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
2182130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
2183130f4520SKenneth D. Merry 			}
2184130f4520SKenneth D. Merry 			break;
2185130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
2186025a2301SEdward Tomasz Napierala 			break;
2187130f4520SKenneth D. Merry 		default:
21885124012aSAlexander Motin 			panic("Unexpected backend type %d", be_lun->dev_type);
2189130f4520SKenneth D. Merry 			break;
2190130f4520SKenneth D. Merry 		}
219119720f41SAlexander Motin 		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2192130f4520SKenneth D. Merry 	}
2193130f4520SKenneth D. Merry 	return (0);
2194130f4520SKenneth D. Merry }
2195130f4520SKenneth D. Merry 
2196130f4520SKenneth D. Merry static int
2197648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2198130f4520SKenneth D. Merry {
21990bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2200130f4520SKenneth D. Merry 	struct nameidata nd;
22018951f055SMarcelo Araujo 	const char	*value;
22020bcd4ab6SAlexander Motin 	int		 error, flags;
2203130f4520SKenneth D. Merry 
2204130f4520SKenneth D. Merry 	error = 0;
2205130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
2206130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
220719720f41SAlexander Motin 			 "Root filesystem is not mounted");
2208130f4520SKenneth D. Merry 		return (1);
2209130f4520SKenneth D. Merry 	}
22108a08cec1SMateusz Guzik 	pwd_ensure_dirs();
2211130f4520SKenneth D. Merry 
22128951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "file", NULL);
22130bcd4ab6SAlexander Motin 	if (value == NULL) {
22140bcd4ab6SAlexander Motin 		snprintf(req->error_str, sizeof(req->error_str),
22150bcd4ab6SAlexander Motin 			 "no file argument specified");
22160bcd4ab6SAlexander Motin 		return (1);
22170bcd4ab6SAlexander Motin 	}
22180bcd4ab6SAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
22190bcd4ab6SAlexander Motin 	be_lun->dev_path = strdup(value, M_CTLBLK);
22200bcd4ab6SAlexander Motin 
22210bcd4ab6SAlexander Motin 	flags = FREAD;
22228951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
222391be33dcSAlexander Motin 	if (value != NULL) {
222491be33dcSAlexander Motin 		if (strcmp(value, "on") != 0)
222591be33dcSAlexander Motin 			flags |= FWRITE;
222691be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_DIRECT)
22270bcd4ab6SAlexander Motin 		flags |= FWRITE;
22280bcd4ab6SAlexander Motin 
2229130f4520SKenneth D. Merry again:
22307e1d3eefSMateusz Guzik 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path);
2231130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
22320bcd4ab6SAlexander Motin 	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
22330bcd4ab6SAlexander Motin 		flags &= ~FWRITE;
22340bcd4ab6SAlexander Motin 		goto again;
22350bcd4ab6SAlexander Motin 	}
2236130f4520SKenneth D. Merry 	if (error) {
2237130f4520SKenneth D. Merry 		/*
2238130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
2239130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
2240130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
2241130f4520SKenneth D. Merry 		 * full path.
2242130f4520SKenneth D. Merry 		 */
2243130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
2244130f4520SKenneth D. Merry 			char *dev_name;
2245130f4520SKenneth D. Merry 
22460bcd4ab6SAlexander Motin 			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2247130f4520SKenneth D. Merry 				be_lun->dev_path);
2248130f4520SKenneth D. Merry 			free(be_lun->dev_path, M_CTLBLK);
2249130f4520SKenneth D. Merry 			be_lun->dev_path = dev_name;
2250130f4520SKenneth D. Merry 			goto again;
2251130f4520SKenneth D. Merry 		}
2252130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
225319720f41SAlexander Motin 		    "error opening %s: %d", be_lun->dev_path, error);
2254130f4520SKenneth D. Merry 		return (error);
2255130f4520SKenneth D. Merry 	}
22560bcd4ab6SAlexander Motin 	if (flags & FWRITE)
22570bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
22580bcd4ab6SAlexander Motin 	else
22590bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2260130f4520SKenneth D. Merry 
2261bb92cd7bSMateusz Guzik 	NDFREE_PNBUF(&nd);
2262130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
2263130f4520SKenneth D. Merry 
2264130f4520SKenneth D. Merry 	/* We only support disks and files. */
22657ad2a82dSMateusz Guzik 	if (vn_isdisk_error(be_lun->vn, &error)) {
2266130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
2267130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
2268130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
2269130f4520SKenneth D. Merry 	} else {
2270130f4520SKenneth D. Merry 		error = EINVAL;
2271130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2272025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
2273130f4520SKenneth D. Merry 	}
2274b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
2275130f4520SKenneth D. Merry 
2276a15bbf15SAlexander Motin 	if (error != 0)
2277130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
22780bcd4ab6SAlexander Motin 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
22790bcd4ab6SAlexander Motin 	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
2280ac503c19SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
22818951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
22820bcd4ab6SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
22830bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
22840bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "read") == 0)
22850bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
2286ac503c19SAlexander Motin 	else if (value != NULL && strcmp(value, "soft") == 0)
2287ac503c19SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_SOFT;
22880bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "off") == 0)
22890bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2290130f4520SKenneth D. Merry 	return (0);
2291130f4520SKenneth D. Merry }
2292130f4520SKenneth D. Merry 
2293130f4520SKenneth D. Merry static int
2294130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2295130f4520SKenneth D. Merry {
22960bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2297130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2298130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
229957a5db13SAlexander Motin 	char num_thread_str[16];
2300130f4520SKenneth D. Merry 	char tmpstr[32];
23018951f055SMarcelo Araujo 	const char *value;
2302fbc8d4ffSAlexander Motin 	int retval, num_threads;
230357a5db13SAlexander Motin 	int tmp_num_threads;
2304130f4520SKenneth D. Merry 
2305130f4520SKenneth D. Merry 	params = &req->reqdata.create;
2306130f4520SKenneth D. Merry 	retval = 0;
230719720f41SAlexander Motin 	req->status = CTL_LUN_OK;
2308130f4520SKenneth D. Merry 
2309130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
23100bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
231119720f41SAlexander Motin 	be_lun->params = req->reqdata.create;
2312130f4520SKenneth D. Merry 	be_lun->softc = softc;
2313130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
2314ef8daf3fSAlexander Motin 	STAILQ_INIT(&be_lun->config_read_queue);
2315130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
2316130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
231734144c2cSAlexander Motin 	mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
231834144c2cSAlexander Motin 	mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
23198951f055SMarcelo Araujo 	cbe_lun->options = nvlist_clone(req->args_nvl);
2320130f4520SKenneth D. Merry 
2321130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
23220bcd4ab6SAlexander Motin 		cbe_lun->lun_type = params->device_type;
2323130f4520SKenneth D. Merry 	else
23240bcd4ab6SAlexander Motin 		cbe_lun->lun_type = T_DIRECT;
232534144c2cSAlexander Motin 	be_lun->flags = 0;
23267ac58230SAlexander Motin 	cbe_lun->flags = 0;
23278951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
23287ac58230SAlexander Motin 	if (value != NULL) {
23297ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
23307ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
23317ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
23327ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2333130f4520SKenneth D. Merry 
233491be33dcSAlexander Motin 	if (cbe_lun->lun_type == T_DIRECT ||
233591be33dcSAlexander Motin 	    cbe_lun->lun_type == T_CDROM) {
2336a15bbf15SAlexander Motin 		be_lun->size_bytes = params->lun_size_bytes;
2337a15bbf15SAlexander Motin 		if (params->blocksize_bytes != 0)
23380bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
233991be33dcSAlexander Motin 		else if (cbe_lun->lun_type == T_CDROM)
234091be33dcSAlexander Motin 			cbe_lun->blocksize = 2048;
2341a15bbf15SAlexander Motin 		else
23420bcd4ab6SAlexander Motin 			cbe_lun->blocksize = 512;
23430bcd4ab6SAlexander Motin 		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
23440bcd4ab6SAlexander Motin 		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
23450bcd4ab6SAlexander Motin 		    0 : (be_lun->size_blocks - 1);
2346130f4520SKenneth D. Merry 
23477ac58230SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
23487ac58230SAlexander Motin 		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2349648dfc1aSAlexander Motin 			retval = ctl_be_block_open(be_lun, req);
2350130f4520SKenneth D. Merry 			if (retval != 0) {
2351130f4520SKenneth D. Merry 				retval = 0;
235219720f41SAlexander Motin 				req->status = CTL_LUN_WARNING;
2353130f4520SKenneth D. Merry 			}
23547ac58230SAlexander Motin 		}
23550bcd4ab6SAlexander Motin 		num_threads = cbb_num_threads;
2356130f4520SKenneth D. Merry 	} else {
2357130f4520SKenneth D. Merry 		num_threads = 1;
2358130f4520SKenneth D. Merry 	}
2359130f4520SKenneth D. Merry 
23608951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
236157a5db13SAlexander Motin 	if (value != NULL) {
236257a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
2363130f4520SKenneth D. Merry 
2364130f4520SKenneth D. Merry 		/*
2365130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
2366130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
2367130f4520SKenneth D. Merry 		 * specify 1000 threads.
2368130f4520SKenneth D. Merry 		 */
2369130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
2370130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
237119720f41SAlexander Motin 				 "invalid number of threads %s",
237219720f41SAlexander Motin 				 num_thread_str);
2373130f4520SKenneth D. Merry 			goto bailout_error;
2374130f4520SKenneth D. Merry 		}
2375130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
237657a5db13SAlexander Motin 	}
2377130f4520SKenneth D. Merry 
237819720f41SAlexander Motin 	if (be_lun->vn == NULL)
2379648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2380130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
238119720f41SAlexander Motin 	params->lun_size_bytes = be_lun->size_bytes;
23820bcd4ab6SAlexander Motin 	params->blocksize_bytes = cbe_lun->blocksize;
2383130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
23840bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = params->req_lun_id;
23850bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2386130f4520SKenneth D. Merry 	} else
23870bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = 0;
2388130f4520SKenneth D. Merry 
23890bcd4ab6SAlexander Motin 	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
23900bcd4ab6SAlexander Motin 	cbe_lun->be = &ctl_be_block_driver;
2391130f4520SKenneth D. Merry 
2392130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
239371cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2394130f4520SKenneth D. Merry 			 softc->num_luns);
23950bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, tmpstr,
23960bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2397130f4520SKenneth D. Merry 
2398130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
2399130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
2400e7038eb7SAlexander Motin 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2401130f4520SKenneth D. Merry 	} else {
24020bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
24030bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num),
2404130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
2405130f4520SKenneth D. Merry 	}
2406130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
240771cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
24080bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, tmpstr,
24090bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2410130f4520SKenneth D. Merry 
2411130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2412130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2413e7038eb7SAlexander Motin 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2414130f4520SKenneth D. Merry 	} else {
24150bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, params->device_id,
24160bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id),
2417130f4520SKenneth D. Merry 			    sizeof(params->device_id)));
2418130f4520SKenneth D. Merry 	}
2419130f4520SKenneth D. Merry 
2420130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2421130f4520SKenneth D. Merry 
242234144c2cSAlexander Motin 	be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2423130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2424130f4520SKenneth D. Merry 
2425130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2426130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
242719720f41SAlexander Motin 			 "unable to create taskqueue");
2428130f4520SKenneth D. Merry 		goto bailout_error;
2429130f4520SKenneth D. Merry 	}
2430130f4520SKenneth D. Merry 
2431130f4520SKenneth D. Merry 	/*
2432130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2433130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2434130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2435130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2436130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2437130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2438130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2439130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2440130f4520SKenneth D. Merry 	 *
2441130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2442130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2443130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2444130f4520SKenneth D. Merry 	 */
244512373e95SAlexander Motin 	retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2446130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2447053db1feSAlexander Motin 					 /*priority*/PUSER,
244812373e95SAlexander Motin 					 /*proc*/control_softc->ctl_proc,
244934144c2cSAlexander Motin 					 /*thread name*/"block");
2450130f4520SKenneth D. Merry 
2451130f4520SKenneth D. Merry 	if (retval != 0)
2452130f4520SKenneth D. Merry 		goto bailout_error;
2453130f4520SKenneth D. Merry 
2454130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2455130f4520SKenneth D. Merry 
24560bcd4ab6SAlexander Motin 	retval = ctl_add_lun(&be_lun->cbe_lun);
2457130f4520SKenneth D. Merry 	if (retval != 0) {
2458130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
245919720f41SAlexander Motin 			 "ctl_add_lun() returned error %d, see dmesg for "
246019720f41SAlexander Motin 			 "details", retval);
2461130f4520SKenneth D. Merry 		retval = 0;
2462130f4520SKenneth D. Merry 		goto bailout_error;
2463130f4520SKenneth D. Merry 	}
2464130f4520SKenneth D. Merry 
246534144c2cSAlexander Motin 	be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
24660bcd4ab6SAlexander Motin 					       cbe_lun->blocksize,
2467130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
24680bcd4ab6SAlexander Motin 					       cbe_lun->lun_type
2469130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2470130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2471130f4520SKenneth D. Merry 
247234144c2cSAlexander Motin 	mtx_lock(&softc->lock);
247334144c2cSAlexander Motin 	softc->num_luns++;
247434144c2cSAlexander Motin 	SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
247534144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
247634144c2cSAlexander Motin 
247734144c2cSAlexander Motin 	params->req_lun_id = cbe_lun->lun_id;
247834144c2cSAlexander Motin 
2479130f4520SKenneth D. Merry 	return (retval);
2480130f4520SKenneth D. Merry 
2481130f4520SKenneth D. Merry bailout_error:
2482130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2483130f4520SKenneth D. Merry 
24849e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
24859e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2486130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
24879e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2488130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
24898951f055SMarcelo Araujo 	nvlist_destroy(cbe_lun->options);
249075c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
249175c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2492130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2493130f4520SKenneth D. Merry 
2494130f4520SKenneth D. Merry 	return (retval);
2495130f4520SKenneth D. Merry }
2496130f4520SKenneth D. Merry 
2497130f4520SKenneth D. Merry static int
2498130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2499130f4520SKenneth D. Merry {
2500130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2501130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2502ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2503130f4520SKenneth D. Merry 	int retval;
2504130f4520SKenneth D. Merry 
2505130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2506130f4520SKenneth D. Merry 
250734144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
2508130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
250934144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
251034144c2cSAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id) {
251134144c2cSAlexander Motin 			SLIST_REMOVE(&softc->lun_list, be_lun,
251234144c2cSAlexander Motin 			    ctl_be_block_lun, links);
251334144c2cSAlexander Motin 			softc->num_luns--;
2514130f4520SKenneth D. Merry 			break;
2515130f4520SKenneth D. Merry 		}
251634144c2cSAlexander Motin 	}
2517130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
251834144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
2519130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2520130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
252119720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
252219720f41SAlexander Motin 			 params->lun_id);
2523130f4520SKenneth D. Merry 		goto bailout_error;
2524130f4520SKenneth D. Merry 	}
2525ee4ad294SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
2526130f4520SKenneth D. Merry 
2527ee4ad294SAlexander Motin 	if (be_lun->vn != NULL) {
2528648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2529648dfc1aSAlexander Motin 		ctl_lun_no_media(cbe_lun);
2530ee4ad294SAlexander Motin 		taskqueue_drain_all(be_lun->io_taskqueue);
2531ee4ad294SAlexander Motin 		ctl_be_block_close(be_lun);
2532ee4ad294SAlexander Motin 	}
2533ee4ad294SAlexander Motin 
253434144c2cSAlexander Motin 	mtx_lock(&softc->lock);
253534144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
253634144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
253734144c2cSAlexander Motin 
253834144c2cSAlexander Motin 	retval = ctl_remove_lun(cbe_lun);
2539130f4520SKenneth D. Merry 	if (retval != 0) {
2540130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
254134144c2cSAlexander Motin 			 "error %d returned from ctl_remove_lun() for "
254219720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
254334144c2cSAlexander Motin 		mtx_lock(&softc->lock);
254434144c2cSAlexander Motin 		be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
254534144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
2546130f4520SKenneth D. Merry 		goto bailout_error;
2547130f4520SKenneth D. Merry 	}
2548130f4520SKenneth D. Merry 
2549130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2550130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
255134144c2cSAlexander Motin 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2552130f4520SKenneth D. Merry 		if (retval == EINTR)
2553130f4520SKenneth D. Merry 			break;
2554130f4520SKenneth D. Merry 	}
2555130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
255634144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2557130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2558130f4520SKenneth D. Merry 		free(be_lun, M_CTLBLK);
255934144c2cSAlexander Motin 	} else {
256034144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
256134144c2cSAlexander Motin 		return (EINTR);
256234144c2cSAlexander Motin 	}
2563130f4520SKenneth D. Merry 
2564130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2565130f4520SKenneth D. Merry 	return (0);
2566130f4520SKenneth D. Merry 
2567130f4520SKenneth D. Merry bailout_error:
2568130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2569130f4520SKenneth D. Merry 	return (0);
2570130f4520SKenneth D. Merry }
2571130f4520SKenneth D. Merry 
257281177295SEdward Tomasz Napierala static int
257381177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
257481177295SEdward Tomasz Napierala {
257581177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
257681177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
2577a3977beaSAlexander Motin 	struct ctl_be_lun *cbe_lun;
25788951f055SMarcelo Araujo 	const char *value;
257971d8e97eSAlexander Motin 	uint64_t oldsize;
25807ac58230SAlexander Motin 	int error, wasprim;
258181177295SEdward Tomasz Napierala 
258281177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
258381177295SEdward Tomasz Napierala 
258434144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
258581177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
258634144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
25870bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
258881177295SEdward Tomasz Napierala 			break;
258981177295SEdward Tomasz Napierala 	}
259081177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
259181177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
259281177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
259319720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
259419720f41SAlexander Motin 			 params->lun_id);
259581177295SEdward Tomasz Napierala 		goto bailout_error;
259681177295SEdward Tomasz Napierala 	}
2597a3977beaSAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
259881177295SEdward Tomasz Napierala 
2599a3977beaSAlexander Motin 	if (params->lun_size_bytes != 0)
260019720f41SAlexander Motin 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
26018951f055SMarcelo Araujo 
2602efeedddcSAlexander Motin 	if (req->args_nvl != NULL) {
26038951f055SMarcelo Araujo 		nvlist_destroy(cbe_lun->options);
26048951f055SMarcelo Araujo 		cbe_lun->options = nvlist_clone(req->args_nvl);
2605efeedddcSAlexander Motin 	}
260681177295SEdward Tomasz Napierala 
26077ac58230SAlexander Motin 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
26088951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
26097ac58230SAlexander Motin 	if (value != NULL) {
26107ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
26117ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
26127ac58230SAlexander Motin 		else
26137ac58230SAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
26147ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
26157ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
26167ac58230SAlexander Motin 	else
26177ac58230SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
26187ac58230SAlexander Motin 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
26197ac58230SAlexander Motin 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
26207ac58230SAlexander Motin 			ctl_lun_primary(cbe_lun);
26217ac58230SAlexander Motin 		else
26227ac58230SAlexander Motin 			ctl_lun_secondary(cbe_lun);
26237ac58230SAlexander Motin 	}
26247ac58230SAlexander Motin 
26250bcd4ab6SAlexander Motin 	oldsize = be_lun->size_blocks;
26267ac58230SAlexander Motin 	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
26277ac58230SAlexander Motin 	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
262819720f41SAlexander Motin 		if (be_lun->vn == NULL)
2629648dfc1aSAlexander Motin 			error = ctl_be_block_open(be_lun, req);
26307ad2a82dSMateusz Guzik 		else if (vn_isdisk_error(be_lun->vn, &error))
26314ce7a086SAlexander Motin 			error = ctl_be_block_open_dev(be_lun, req);
26323d5cb709SAlexander Motin 		else if (be_lun->vn->v_type == VREG) {
26333d5cb709SAlexander Motin 			vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
26344ce7a086SAlexander Motin 			error = ctl_be_block_open_file(be_lun, req);
2635b249ce48SMateusz Guzik 			VOP_UNLOCK(be_lun->vn);
26363d5cb709SAlexander Motin 		} else
2637b9b4269cSAlexander Motin 			error = EINVAL;
2638648dfc1aSAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
26390bcd4ab6SAlexander Motin 		    be_lun->vn != NULL) {
2640648dfc1aSAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2641648dfc1aSAlexander Motin 			ctl_lun_has_media(cbe_lun);
2642648dfc1aSAlexander Motin 		} else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2643648dfc1aSAlexander Motin 		    be_lun->vn == NULL) {
2644648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2645648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
264671d8e97eSAlexander Motin 		}
2647648dfc1aSAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
26487ac58230SAlexander Motin 	} else {
26497ac58230SAlexander Motin 		if (be_lun->vn != NULL) {
2650648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2651648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
2652ee4ad294SAlexander Motin 			taskqueue_drain_all(be_lun->io_taskqueue);
26537ac58230SAlexander Motin 			error = ctl_be_block_close(be_lun);
26547ac58230SAlexander Motin 		} else
26557ac58230SAlexander Motin 			error = 0;
26567ac58230SAlexander Motin 	}
26577ac58230SAlexander Motin 	if (be_lun->size_blocks != oldsize)
26587ac58230SAlexander Motin 		ctl_lun_capacity_changed(cbe_lun);
265981177295SEdward Tomasz Napierala 
266081177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
266181177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
266281177295SEdward Tomasz Napierala 
266334144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
266419720f41SAlexander Motin 	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
266581177295SEdward Tomasz Napierala 	return (0);
266681177295SEdward Tomasz Napierala 
266781177295SEdward Tomasz Napierala bailout_error:
266834144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
266981177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
267081177295SEdward Tomasz Napierala 	return (0);
267181177295SEdward Tomasz Napierala }
267281177295SEdward Tomasz Napierala 
2673130f4520SKenneth D. Merry static void
2674767300e8SAlexander Motin ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2675130f4520SKenneth D. Merry {
2676767300e8SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
267734144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
267834144c2cSAlexander Motin 
267934144c2cSAlexander Motin 	taskqueue_drain_all(be_lun->io_taskqueue);
268034144c2cSAlexander Motin 	taskqueue_free(be_lun->io_taskqueue);
268134144c2cSAlexander Motin 	if (be_lun->disk_stats != NULL)
268234144c2cSAlexander Motin 		devstat_remove_entry(be_lun->disk_stats);
268334144c2cSAlexander Motin 	nvlist_destroy(be_lun->cbe_lun.options);
268434144c2cSAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
268534144c2cSAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
268634144c2cSAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2687130f4520SKenneth D. Merry 
2688130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
268934144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
269034144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
269134144c2cSAlexander Motin 		wakeup(be_lun);
269234144c2cSAlexander Motin 	else
269334144c2cSAlexander Motin 		free(be_lun, M_CTLBLK);
2694130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2695130f4520SKenneth D. Merry }
2696130f4520SKenneth D. Merry 
2697130f4520SKenneth D. Merry static int
2698130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2699130f4520SKenneth D. Merry {
2700130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
27010bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2702130f4520SKenneth D. Merry 	int retval;
2703130f4520SKenneth D. Merry 
2704130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2705130f4520SKenneth D. Merry 
27069cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
2707767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun;
2708130f4520SKenneth D. Merry 
270967cc546dSAlexander Motin 	retval = 0;
2710130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2711130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2712130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2713ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2714ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2715ee7f31c0SAlexander Motin 	case UNMAP:
2716130f4520SKenneth D. Merry 		/*
2717130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2718130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2719130f4520SKenneth D. Merry 		 *
2720130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2721130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2722130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2723130f4520SKenneth D. Merry 		 */
272475c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
2725130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2726130f4520SKenneth D. Merry 				   links);
272775c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
2728130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2729130f4520SKenneth D. Merry 		break;
2730130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2731130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2732648dfc1aSAlexander Motin 		struct ctl_lun_req req;
2733130f4520SKenneth D. Merry 
2734130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
273566b69676SAlexander Motin 		if ((cdb->how & SSS_PC_MASK) != 0) {
273666b69676SAlexander Motin 			ctl_set_success(&io->scsiio);
273766b69676SAlexander Motin 			ctl_config_write_done(io);
273866b69676SAlexander Motin 			break;
273966b69676SAlexander Motin 		}
2740648dfc1aSAlexander Motin 		if (cdb->how & SSS_START) {
2741648dfc1aSAlexander Motin 			if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2742648dfc1aSAlexander Motin 				retval = ctl_be_block_open(be_lun, &req);
2743648dfc1aSAlexander Motin 				cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2744648dfc1aSAlexander Motin 				if (retval == 0) {
2745648dfc1aSAlexander Motin 					cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2746648dfc1aSAlexander Motin 					ctl_lun_has_media(cbe_lun);
2747130f4520SKenneth D. Merry 				} else {
2748648dfc1aSAlexander Motin 					cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2749648dfc1aSAlexander Motin 					ctl_lun_no_media(cbe_lun);
2750130f4520SKenneth D. Merry 				}
2751648dfc1aSAlexander Motin 			}
2752648dfc1aSAlexander Motin 			ctl_start_lun(cbe_lun);
2753648dfc1aSAlexander Motin 		} else {
2754648dfc1aSAlexander Motin 			ctl_stop_lun(cbe_lun);
2755648dfc1aSAlexander Motin 			if (cdb->how & SSS_LOEJ) {
2756648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2757648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2758648dfc1aSAlexander Motin 				ctl_lun_ejected(cbe_lun);
2759648dfc1aSAlexander Motin 				if (be_lun->vn != NULL)
2760648dfc1aSAlexander Motin 					ctl_be_block_close(be_lun);
2761648dfc1aSAlexander Motin 			}
2762648dfc1aSAlexander Motin 		}
2763648dfc1aSAlexander Motin 
2764648dfc1aSAlexander Motin 		ctl_set_success(&io->scsiio);
2765130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2766130f4520SKenneth D. Merry 		break;
2767130f4520SKenneth D. Merry 	}
276891be33dcSAlexander Motin 	case PREVENT_ALLOW:
276991be33dcSAlexander Motin 		ctl_set_success(&io->scsiio);
277091be33dcSAlexander Motin 		ctl_config_write_done(io);
277191be33dcSAlexander Motin 		break;
2772130f4520SKenneth D. Merry 	default:
2773130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2774130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2775130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2776130f4520SKenneth D. Merry 		break;
2777130f4520SKenneth D. Merry 	}
2778130f4520SKenneth D. Merry 
2779130f4520SKenneth D. Merry 	return (retval);
2780130f4520SKenneth D. Merry }
2781130f4520SKenneth D. Merry 
2782130f4520SKenneth D. Merry static int
2783130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2784130f4520SKenneth D. Merry {
2785ef8daf3fSAlexander Motin 	struct ctl_be_block_lun *be_lun;
2786ef8daf3fSAlexander Motin 	int retval = 0;
2787ef8daf3fSAlexander Motin 
2788ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
2789ef8daf3fSAlexander Motin 
2790767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2791ef8daf3fSAlexander Motin 
2792ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
2793ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:
2794ef8daf3fSAlexander Motin 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2795ef8daf3fSAlexander Motin 			mtx_lock(&be_lun->queue_lock);
2796ef8daf3fSAlexander Motin 			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2797ef8daf3fSAlexander Motin 			    &io->io_hdr, links);
2798ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
2799ef8daf3fSAlexander Motin 			taskqueue_enqueue(be_lun->io_taskqueue,
2800ef8daf3fSAlexander Motin 			    &be_lun->io_task);
2801ef8daf3fSAlexander Motin 			retval = CTL_RETVAL_QUEUED;
2802ef8daf3fSAlexander Motin 			break;
2803ef8daf3fSAlexander Motin 		}
2804ef8daf3fSAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
2805ef8daf3fSAlexander Motin 				      /*sks_valid*/ 1,
2806ef8daf3fSAlexander Motin 				      /*command*/ 1,
2807ef8daf3fSAlexander Motin 				      /*field*/ 1,
2808ef8daf3fSAlexander Motin 				      /*bit_valid*/ 1,
2809ef8daf3fSAlexander Motin 				      /*bit*/ 4);
2810ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2811ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2812ef8daf3fSAlexander Motin 		break;
2813ef8daf3fSAlexander Motin 	default:
2814ef8daf3fSAlexander Motin 		ctl_set_invalid_opcode(&io->scsiio);
2815ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2816ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2817ef8daf3fSAlexander Motin 		break;
2818ef8daf3fSAlexander Motin 	}
2819ef8daf3fSAlexander Motin 
2820ef8daf3fSAlexander Motin 	return (retval);
2821130f4520SKenneth D. Merry }
2822130f4520SKenneth D. Merry 
2823130f4520SKenneth D. Merry static int
2824767300e8SAlexander Motin ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2825130f4520SKenneth D. Merry {
2826767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2827130f4520SKenneth D. Merry 	int retval;
2828130f4520SKenneth D. Merry 
2829*519b24f0SAlexander Motin 	retval = sbuf_cat(sb, "\t<num_threads>");
2830130f4520SKenneth D. Merry 	if (retval != 0)
2831130f4520SKenneth D. Merry 		goto bailout;
2832130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2833130f4520SKenneth D. Merry 	if (retval != 0)
2834130f4520SKenneth D. Merry 		goto bailout;
2835*519b24f0SAlexander Motin 	retval = sbuf_cat(sb, "</num_threads>\n");
2836130f4520SKenneth D. Merry 
2837130f4520SKenneth D. Merry bailout:
2838130f4520SKenneth D. Merry 	return (retval);
2839130f4520SKenneth D. Merry }
2840130f4520SKenneth D. Merry 
2841c3e7ba3eSAlexander Motin static uint64_t
2842767300e8SAlexander Motin ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2843c3e7ba3eSAlexander Motin {
2844767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2845c3e7ba3eSAlexander Motin 
2846c3e7ba3eSAlexander Motin 	if (lun->getattr == NULL)
2847c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
2848c3e7ba3eSAlexander Motin 	return (lun->getattr(lun, attrname));
2849c3e7ba3eSAlexander Motin }
2850c3e7ba3eSAlexander Motin 
28510c629e28SAlexander Motin static int
2852130f4520SKenneth D. Merry ctl_be_block_init(void)
2853130f4520SKenneth D. Merry {
28540c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
2855130f4520SKenneth D. Merry 
285634144c2cSAlexander Motin 	sx_init(&softc->modify_lock, "ctlblock modify");
285775c7a1d3SAlexander Motin 	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
28580c629e28SAlexander Motin 	softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2859a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2860cd853791SKonstantin Belousov 	softc->bufmin_zone = uma_zcreate("ctlblockmin", CTLBLK_MIN_SEG,
28610d7fed74SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2862cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2863cd853791SKonstantin Belousov 		softc->bufmax_zone = uma_zcreate("ctlblockmax", CTLBLK_MAX_SEG,
28648054320eSAlexander Motin 		    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
286534144c2cSAlexander Motin 	SLIST_INIT(&softc->lun_list);
28660c629e28SAlexander Motin 	return (0);
28670c629e28SAlexander Motin }
2868130f4520SKenneth D. Merry 
28690c629e28SAlexander Motin static int
28700c629e28SAlexander Motin ctl_be_block_shutdown(void)
28710c629e28SAlexander Motin {
28720c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
287334144c2cSAlexander Motin 	struct ctl_be_block_lun *lun;
28740c629e28SAlexander Motin 
28750c629e28SAlexander Motin 	mtx_lock(&softc->lock);
287634144c2cSAlexander Motin 	while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
287734144c2cSAlexander Motin 		SLIST_REMOVE_HEAD(&softc->lun_list, links);
287834144c2cSAlexander Motin 		softc->num_luns--;
28790c629e28SAlexander Motin 		/*
288034144c2cSAlexander Motin 		 * Drop our lock here.  Since ctl_remove_lun() can call
28810c629e28SAlexander Motin 		 * back into us, this could potentially lead to a recursive
28820c629e28SAlexander Motin 		 * lock of the same mutex, which would cause a hang.
28830c629e28SAlexander Motin 		 */
28840c629e28SAlexander Motin 		mtx_unlock(&softc->lock);
288534144c2cSAlexander Motin 		ctl_remove_lun(&lun->cbe_lun);
28860c629e28SAlexander Motin 		mtx_lock(&softc->lock);
28870c629e28SAlexander Motin 	}
28880c629e28SAlexander Motin 	mtx_unlock(&softc->lock);
2889cd853791SKonstantin Belousov 	uma_zdestroy(softc->bufmin_zone);
2890cd853791SKonstantin Belousov 	if (CTLBLK_MIN_SEG < CTLBLK_MAX_SEG)
2891cd853791SKonstantin Belousov 		uma_zdestroy(softc->bufmax_zone);
28920c629e28SAlexander Motin 	uma_zdestroy(softc->beio_zone);
28930c629e28SAlexander Motin 	mtx_destroy(&softc->lock);
289434144c2cSAlexander Motin 	sx_destroy(&softc->modify_lock);
28950c629e28SAlexander Motin 	return (0);
2896130f4520SKenneth D. Merry }
2897