xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision 767300e87aeaec465970978e9492cc10f2ee2fef)
1130f4520SKenneth D. Merry /*-
2bec9534dSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3bec9534dSPedro F. Giffuni  *
4130f4520SKenneth D. Merry  * Copyright (c) 2003 Silicon Graphics International Corp.
5130f4520SKenneth D. Merry  * Copyright (c) 2009-2011 Spectra Logic Corporation
681177295SEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
7648dfc1aSAlexander Motin  * Copyright (c) 2014-2015 Alexander Motin <mav@FreeBSD.org>
8130f4520SKenneth D. Merry  * All rights reserved.
9130f4520SKenneth D. Merry  *
1081177295SEdward Tomasz Napierala  * Portions of this software were developed by Edward Tomasz Napierala
1181177295SEdward Tomasz Napierala  * under sponsorship from the FreeBSD Foundation.
1281177295SEdward Tomasz Napierala  *
13130f4520SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
14130f4520SKenneth D. Merry  * modification, are permitted provided that the following conditions
15130f4520SKenneth D. Merry  * are met:
16130f4520SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
17130f4520SKenneth D. Merry  *    notice, this list of conditions, and the following disclaimer,
18130f4520SKenneth D. Merry  *    without modification.
19130f4520SKenneth D. Merry  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
20130f4520SKenneth D. Merry  *    substantially similar to the "NO WARRANTY" disclaimer below
21130f4520SKenneth D. Merry  *    ("Disclaimer") and any redistribution must be conditioned upon
22130f4520SKenneth D. Merry  *    including a substantially similar Disclaimer requirement for further
23130f4520SKenneth D. Merry  *    binary redistribution.
24130f4520SKenneth D. Merry  *
25130f4520SKenneth D. Merry  * NO WARRANTY
26130f4520SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27130f4520SKenneth D. Merry  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28130f4520SKenneth D. Merry  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
29130f4520SKenneth D. Merry  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30130f4520SKenneth D. Merry  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31130f4520SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32130f4520SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33130f4520SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34130f4520SKenneth D. Merry  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35130f4520SKenneth D. Merry  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36130f4520SKenneth D. Merry  * POSSIBILITY OF SUCH DAMAGES.
37130f4520SKenneth D. Merry  *
38130f4520SKenneth D. Merry  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
39130f4520SKenneth D. Merry  */
40130f4520SKenneth D. Merry /*
41130f4520SKenneth D. Merry  * CAM Target Layer driver backend for block devices.
42130f4520SKenneth D. Merry  *
43130f4520SKenneth D. Merry  * Author: Ken Merry <ken@FreeBSD.org>
44130f4520SKenneth D. Merry  */
45130f4520SKenneth D. Merry #include <sys/cdefs.h>
46130f4520SKenneth D. Merry __FBSDID("$FreeBSD$");
47130f4520SKenneth D. Merry 
48130f4520SKenneth D. Merry #include <sys/param.h>
49130f4520SKenneth D. Merry #include <sys/systm.h>
50130f4520SKenneth D. Merry #include <sys/kernel.h>
51130f4520SKenneth D. Merry #include <sys/types.h>
52130f4520SKenneth D. Merry #include <sys/kthread.h>
53130f4520SKenneth D. Merry #include <sys/bio.h>
54130f4520SKenneth D. Merry #include <sys/fcntl.h>
55ee7f31c0SAlexander Motin #include <sys/limits.h>
56130f4520SKenneth D. Merry #include <sys/lock.h>
57130f4520SKenneth D. Merry #include <sys/mutex.h>
58130f4520SKenneth D. Merry #include <sys/condvar.h>
59130f4520SKenneth D. Merry #include <sys/malloc.h>
60130f4520SKenneth D. Merry #include <sys/conf.h>
61130f4520SKenneth D. Merry #include <sys/ioccom.h>
62130f4520SKenneth D. Merry #include <sys/queue.h>
63130f4520SKenneth D. Merry #include <sys/sbuf.h>
64130f4520SKenneth D. Merry #include <sys/endian.h>
65130f4520SKenneth D. Merry #include <sys/uio.h>
66130f4520SKenneth D. Merry #include <sys/buf.h>
67130f4520SKenneth D. Merry #include <sys/taskqueue.h>
68130f4520SKenneth D. Merry #include <sys/vnode.h>
69130f4520SKenneth D. Merry #include <sys/namei.h>
70130f4520SKenneth D. Merry #include <sys/mount.h>
71130f4520SKenneth D. Merry #include <sys/disk.h>
72130f4520SKenneth D. Merry #include <sys/fcntl.h>
73130f4520SKenneth D. Merry #include <sys/filedesc.h>
74ef8daf3fSAlexander Motin #include <sys/filio.h>
75130f4520SKenneth D. Merry #include <sys/proc.h>
76130f4520SKenneth D. Merry #include <sys/pcpu.h>
77130f4520SKenneth D. Merry #include <sys/module.h>
78130f4520SKenneth D. Merry #include <sys/sdt.h>
79130f4520SKenneth D. Merry #include <sys/devicestat.h>
80130f4520SKenneth D. Merry #include <sys/sysctl.h>
818951f055SMarcelo Araujo #include <sys/nv.h>
828951f055SMarcelo Araujo #include <sys/dnv.h>
8334144c2cSAlexander Motin #include <sys/sx.h>
84130f4520SKenneth D. Merry 
85130f4520SKenneth D. Merry #include <geom/geom.h>
86130f4520SKenneth D. Merry 
87130f4520SKenneth D. Merry #include <cam/cam.h>
88130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h>
89130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h>
90130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h>
91130f4520SKenneth D. Merry #include <cam/ctl/ctl.h>
92130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h>
93130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h>
947ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h>
95130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h>
967ac58230SAlexander Motin #include <cam/ctl/ctl_private.h>
97130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h>
98130f4520SKenneth D. Merry 
99130f4520SKenneth D. Merry /*
10008a7cce5SAlexander Motin  * The idea here is that we'll allocate enough S/G space to hold a 1MB
10108a7cce5SAlexander Motin  * I/O.  If we get an I/O larger than that, we'll split it.
102130f4520SKenneth D. Merry  */
10311b569f7SAlexander Motin #define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
10411b569f7SAlexander Motin #define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
10508a7cce5SAlexander Motin #define	CTLBLK_MAX_SEG		MAXPHYS
10611b569f7SAlexander Motin #define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
10711b569f7SAlexander Motin #define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
108130f4520SKenneth D. Merry 
109130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG
110130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \
111130f4520SKenneth D. Merry     printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
112130f4520SKenneth D. Merry #else
113130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0)
114130f4520SKenneth D. Merry #endif
115130f4520SKenneth D. Merry 
116e86a4142SAlexander Motin #define PRIV(io)	\
117e86a4142SAlexander Motin     ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
11811b569f7SAlexander Motin #define ARGS(io)	\
11911b569f7SAlexander Motin     ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
120e86a4142SAlexander Motin 
121130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb);
122130f4520SKenneth D. Merry 
123130f4520SKenneth D. Merry typedef enum {
124130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
125130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
126130f4520SKenneth D. Merry } ctl_be_block_lun_flags;
127130f4520SKenneth D. Merry 
128130f4520SKenneth D. Merry typedef enum {
129130f4520SKenneth D. Merry 	CTL_BE_BLOCK_NONE,
130130f4520SKenneth D. Merry 	CTL_BE_BLOCK_DEV,
131130f4520SKenneth D. Merry 	CTL_BE_BLOCK_FILE
132130f4520SKenneth D. Merry } ctl_be_block_type;
133130f4520SKenneth D. Merry 
134130f4520SKenneth D. Merry struct ctl_be_block_filedata {
135130f4520SKenneth D. Merry 	struct ucred *cred;
136130f4520SKenneth D. Merry };
137130f4520SKenneth D. Merry 
138130f4520SKenneth D. Merry union ctl_be_block_bedata {
139130f4520SKenneth D. Merry 	struct ctl_be_block_filedata file;
140130f4520SKenneth D. Merry };
141130f4520SKenneth D. Merry 
142130f4520SKenneth D. Merry struct ctl_be_block_io;
143130f4520SKenneth D. Merry struct ctl_be_block_lun;
144130f4520SKenneth D. Merry 
145130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
146130f4520SKenneth D. Merry 			       struct ctl_be_block_io *beio);
147c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
148c3e7ba3eSAlexander Motin 				  const char *attrname);
149130f4520SKenneth D. Merry 
150130f4520SKenneth D. Merry /*
151130f4520SKenneth D. Merry  * Backend LUN structure.  There is a 1:1 mapping between a block device
152130f4520SKenneth D. Merry  * and a backend block LUN, and between a backend block LUN and a CTL LUN.
153130f4520SKenneth D. Merry  */
154130f4520SKenneth D. Merry struct ctl_be_block_lun {
155*767300e8SAlexander Motin 	struct ctl_be_lun cbe_lun;		/* Must be first element. */
15619720f41SAlexander Motin 	struct ctl_lun_create_params params;
157130f4520SKenneth D. Merry 	char *dev_path;
158130f4520SKenneth D. Merry 	ctl_be_block_type dev_type;
159130f4520SKenneth D. Merry 	struct vnode *vn;
160130f4520SKenneth D. Merry 	union ctl_be_block_bedata backend;
161130f4520SKenneth D. Merry 	cbb_dispatch_t dispatch;
162130f4520SKenneth D. Merry 	cbb_dispatch_t lun_flush;
163ee7f31c0SAlexander Motin 	cbb_dispatch_t unmap;
164ef8daf3fSAlexander Motin 	cbb_dispatch_t get_lba_status;
165c3e7ba3eSAlexander Motin 	cbb_getattr_t getattr;
166130f4520SKenneth D. Merry 	uint64_t size_blocks;
167130f4520SKenneth D. Merry 	uint64_t size_bytes;
168130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
169130f4520SKenneth D. Merry 	struct devstat *disk_stats;
170130f4520SKenneth D. Merry 	ctl_be_block_lun_flags flags;
17134144c2cSAlexander Motin 	SLIST_ENTRY(ctl_be_block_lun) links;
172130f4520SKenneth D. Merry 	struct taskqueue *io_taskqueue;
173130f4520SKenneth D. Merry 	struct task io_task;
174130f4520SKenneth D. Merry 	int num_threads;
175130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) input_queue;
176ef8daf3fSAlexander Motin 	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
177130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
178130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
17975c7a1d3SAlexander Motin 	struct mtx_padalign io_lock;
18075c7a1d3SAlexander Motin 	struct mtx_padalign queue_lock;
181130f4520SKenneth D. Merry };
182130f4520SKenneth D. Merry 
183130f4520SKenneth D. Merry /*
184130f4520SKenneth D. Merry  * Overall softc structure for the block backend module.
185130f4520SKenneth D. Merry  */
186130f4520SKenneth D. Merry struct ctl_be_block_softc {
18734144c2cSAlexander Motin 	struct sx			 modify_lock;
188130f4520SKenneth D. Merry 	struct mtx			 lock;
189130f4520SKenneth D. Merry 	int				 num_luns;
19034144c2cSAlexander Motin 	SLIST_HEAD(, ctl_be_block_lun)	 lun_list;
1910d7fed74SAlexander Motin 	uma_zone_t			 beio_zone;
1920d7fed74SAlexander Motin 	uma_zone_t			 buf_zone;
193130f4520SKenneth D. Merry };
194130f4520SKenneth D. Merry 
195130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc;
196130f4520SKenneth D. Merry 
197130f4520SKenneth D. Merry /*
198130f4520SKenneth D. Merry  * Per-I/O information.
199130f4520SKenneth D. Merry  */
200130f4520SKenneth D. Merry struct ctl_be_block_io {
201130f4520SKenneth D. Merry 	union ctl_io			*io;
202130f4520SKenneth D. Merry 	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
203130f4520SKenneth D. Merry 	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
204130f4520SKenneth D. Merry 	int				bio_cmd;
2050d7fed74SAlexander Motin 	int				two_sglists;
206130f4520SKenneth D. Merry 	int				num_segs;
207130f4520SKenneth D. Merry 	int				num_bios_sent;
208130f4520SKenneth D. Merry 	int				num_bios_done;
209130f4520SKenneth D. Merry 	int				send_complete;
2101f0694a6SAlexander Motin 	int				first_error;
2111f0694a6SAlexander Motin 	uint64_t			first_error_offset;
212130f4520SKenneth D. Merry 	struct bintime			ds_t0;
213130f4520SKenneth D. Merry 	devstat_tag_type		ds_tag_type;
214130f4520SKenneth D. Merry 	devstat_trans_flags		ds_trans_type;
215130f4520SKenneth D. Merry 	uint64_t			io_len;
216130f4520SKenneth D. Merry 	uint64_t			io_offset;
2177d0d4342SAlexander Motin 	int				io_arg;
218130f4520SKenneth D. Merry 	struct ctl_be_block_softc	*softc;
219130f4520SKenneth D. Merry 	struct ctl_be_block_lun		*lun;
220ee7f31c0SAlexander Motin 	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
221130f4520SKenneth D. Merry };
222130f4520SKenneth D. Merry 
2237ac58230SAlexander Motin extern struct ctl_softc *control_softc;
2247ac58230SAlexander Motin 
225130f4520SKenneth D. Merry static int cbb_num_threads = 14;
2267029da5cSPawel Biernacki SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
227130f4520SKenneth D. Merry 	    "CAM Target Layer Block Backend");
228af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
229130f4520SKenneth D. Merry            &cbb_num_threads, 0, "Number of threads per backing file");
230130f4520SKenneth D. Merry 
231130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
232130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio);
233130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio);
234130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io);
235130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio);
236130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
237130f4520SKenneth D. Merry 				    struct ctl_be_block_io *beio);
238130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
239130f4520SKenneth D. Merry 				       struct ctl_be_block_io *beio);
240ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
241ef8daf3fSAlexander Motin 				  struct ctl_be_block_io *beio);
24253c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
24353c146deSAlexander Motin 					 const char *attrname);
244130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
245130f4520SKenneth D. Merry 				   struct ctl_be_block_io *beio);
246ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
247ee7f31c0SAlexander Motin 				   struct ctl_be_block_io *beio);
248130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
249130f4520SKenneth D. Merry 				      struct ctl_be_block_io *beio);
250c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
251c3e7ba3eSAlexander Motin 					 const char *attrname);
252ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
253ef8daf3fSAlexander Motin 				    union ctl_io *io);
254130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
255130f4520SKenneth D. Merry 				    union ctl_io *io);
256130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
257130f4520SKenneth D. Merry 				  union ctl_io *io);
258130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending);
259130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io);
260130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
261130f4520SKenneth D. Merry 				   int flag, struct thread *td);
262130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
263130f4520SKenneth D. Merry 				  struct ctl_lun_req *req);
264130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
265130f4520SKenneth D. Merry 				 struct ctl_lun_req *req);
266130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
267648dfc1aSAlexander Motin static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
268130f4520SKenneth D. Merry 			     struct ctl_lun_req *req);
269130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc,
270130f4520SKenneth D. Merry 			       struct ctl_lun_req *req);
271130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
272130f4520SKenneth D. Merry 			   struct ctl_lun_req *req);
27381177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
27481177295SEdward Tomasz Napierala 			   struct ctl_lun_req *req);
275*767300e8SAlexander Motin static void ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun);
276130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io);
277130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io);
278*767300e8SAlexander Motin static int ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb);
279*767300e8SAlexander Motin static uint64_t ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname);
2800c629e28SAlexander Motin static int ctl_be_block_init(void);
2810c629e28SAlexander Motin static int ctl_be_block_shutdown(void);
282130f4520SKenneth D. Merry 
283130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver =
284130f4520SKenneth D. Merry {
2852a2443d8SKenneth D. Merry 	.name = "block",
2862a2443d8SKenneth D. Merry 	.flags = CTL_BE_FLAG_HAS_CONFIG,
2872a2443d8SKenneth D. Merry 	.init = ctl_be_block_init,
2880c629e28SAlexander Motin 	.shutdown = ctl_be_block_shutdown,
2892a2443d8SKenneth D. Merry 	.data_submit = ctl_be_block_submit,
2902a2443d8SKenneth D. Merry 	.data_move_done = ctl_be_block_move_done,
2912a2443d8SKenneth D. Merry 	.config_read = ctl_be_block_config_read,
2922a2443d8SKenneth D. Merry 	.config_write = ctl_be_block_config_write,
2932a2443d8SKenneth D. Merry 	.ioctl = ctl_be_block_ioctl,
294c3e7ba3eSAlexander Motin 	.lun_info = ctl_be_block_lun_info,
295c3e7ba3eSAlexander Motin 	.lun_attr = ctl_be_block_lun_attr
296130f4520SKenneth D. Merry };
297130f4520SKenneth D. Merry 
29834144c2cSAlexander Motin MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend");
299130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
300130f4520SKenneth D. Merry 
301130f4520SKenneth D. Merry static struct ctl_be_block_io *
302130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc)
303130f4520SKenneth D. Merry {
304130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
305130f4520SKenneth D. Merry 
3060c629e28SAlexander Motin 	beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
307130f4520SKenneth D. Merry 	beio->softc = softc;
308130f4520SKenneth D. Merry 	return (beio);
309130f4520SKenneth D. Merry }
310130f4520SKenneth D. Merry 
311130f4520SKenneth D. Merry static void
312130f4520SKenneth D. Merry ctl_free_beio(struct ctl_be_block_io *beio)
313130f4520SKenneth D. Merry {
3140d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = beio->softc;
315130f4520SKenneth D. Merry 	int i;
316130f4520SKenneth D. Merry 
317130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
3180d7fed74SAlexander Motin 		uma_zfree(softc->buf_zone, beio->sg_segs[i].addr);
31911b569f7SAlexander Motin 
32011b569f7SAlexander Motin 		/* For compare we had two equal S/G lists. */
3210d7fed74SAlexander Motin 		if (beio->two_sglists) {
3220d7fed74SAlexander Motin 			uma_zfree(softc->buf_zone,
32311b569f7SAlexander Motin 			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
32411b569f7SAlexander Motin 		}
325130f4520SKenneth D. Merry 	}
326130f4520SKenneth D. Merry 
3270d7fed74SAlexander Motin 	uma_zfree(softc->beio_zone, beio);
328130f4520SKenneth D. Merry }
329130f4520SKenneth D. Merry 
330130f4520SKenneth D. Merry static void
331130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio)
332130f4520SKenneth D. Merry {
33375c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
334130f4520SKenneth D. Merry 
335ee7f31c0SAlexander Motin 	if (beio->beio_cont != NULL) {
336ee7f31c0SAlexander Motin 		beio->beio_cont(beio);
337ee7f31c0SAlexander Motin 	} else {
338130f4520SKenneth D. Merry 		ctl_free_beio(beio);
33911b569f7SAlexander Motin 		ctl_data_submit_done(io);
340130f4520SKenneth D. Merry 	}
341ee7f31c0SAlexander Motin }
342130f4520SKenneth D. Merry 
343d6043e46SAlexander Motin static size_t
344d6043e46SAlexander Motin cmp(uint8_t *a, uint8_t *b, size_t size)
345d6043e46SAlexander Motin {
346d6043e46SAlexander Motin 	size_t i;
347d6043e46SAlexander Motin 
348d6043e46SAlexander Motin 	for (i = 0; i < size; i++) {
349d6043e46SAlexander Motin 		if (a[i] != b[i])
350d6043e46SAlexander Motin 			break;
351d6043e46SAlexander Motin 	}
352d6043e46SAlexander Motin 	return (i);
353d6043e46SAlexander Motin }
354d6043e46SAlexander Motin 
355d6043e46SAlexander Motin static void
356d6043e46SAlexander Motin ctl_be_block_compare(union ctl_io *io)
357d6043e46SAlexander Motin {
358d6043e46SAlexander Motin 	struct ctl_be_block_io *beio;
359d6043e46SAlexander Motin 	uint64_t off, res;
360d6043e46SAlexander Motin 	int i;
361d6043e46SAlexander Motin 	uint8_t info[8];
362d6043e46SAlexander Motin 
363d6043e46SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
364d6043e46SAlexander Motin 	off = 0;
365d6043e46SAlexander Motin 	for (i = 0; i < beio->num_segs; i++) {
366d6043e46SAlexander Motin 		res = cmp(beio->sg_segs[i].addr,
367d6043e46SAlexander Motin 		    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
368d6043e46SAlexander Motin 		    beio->sg_segs[i].len);
369d6043e46SAlexander Motin 		off += res;
370d6043e46SAlexander Motin 		if (res < beio->sg_segs[i].len)
371d6043e46SAlexander Motin 			break;
372d6043e46SAlexander Motin 	}
373d6043e46SAlexander Motin 	if (i < beio->num_segs) {
374d6043e46SAlexander Motin 		scsi_u64to8b(off, info);
375d6043e46SAlexander Motin 		ctl_set_sense(&io->scsiio, /*current_error*/ 1,
376d6043e46SAlexander Motin 		    /*sense_key*/ SSD_KEY_MISCOMPARE,
377d6043e46SAlexander Motin 		    /*asc*/ 0x1D, /*ascq*/ 0x00,
378d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_INFO,
379d6043e46SAlexander Motin 		    /*size*/ sizeof(info), /*data*/ &info,
380d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_NONE);
381d6043e46SAlexander Motin 	} else
382d6043e46SAlexander Motin 		ctl_set_success(&io->scsiio);
383d6043e46SAlexander Motin }
384d6043e46SAlexander Motin 
385130f4520SKenneth D. Merry static int
386130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io)
387130f4520SKenneth D. Merry {
388130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
389130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
39011b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
391130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
392130f4520SKenneth D. Merry 	struct bintime cur_bt;
393130f4520SKenneth D. Merry #endif
394130f4520SKenneth D. Merry 
395e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
396130f4520SKenneth D. Merry 	be_lun = beio->lun;
397130f4520SKenneth D. Merry 
398130f4520SKenneth D. Merry 	DPRINTF("entered\n");
399130f4520SKenneth D. Merry 
400130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
401e675024aSAlexander Motin 	getbinuptime(&cur_bt);
402130f4520SKenneth D. Merry 	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
403130f4520SKenneth D. Merry 	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
404130f4520SKenneth D. Merry #endif
405e675024aSAlexander Motin 	io->io_hdr.num_dmas++;
40611b569f7SAlexander Motin 	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
407130f4520SKenneth D. Merry 
408130f4520SKenneth D. Merry 	/*
409130f4520SKenneth D. Merry 	 * We set status at this point for read commands, and write
410130f4520SKenneth D. Merry 	 * commands with errors.
411130f4520SKenneth D. Merry 	 */
412f7241cceSAlexander Motin 	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
413f7241cceSAlexander Motin 		;
414eb6ac6f9SAlexander Motin 	} else if ((io->io_hdr.port_status != 0) &&
415eb6ac6f9SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
416eb6ac6f9SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
417eb6ac6f9SAlexander Motin 		ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
418eb6ac6f9SAlexander Motin 		    /*retry_count*/ io->io_hdr.port_status);
419eb6ac6f9SAlexander Motin 	} else if (io->scsiio.kern_data_resid != 0 &&
420eb6ac6f9SAlexander Motin 	    (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
421eb6ac6f9SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
422eb6ac6f9SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
423eb6ac6f9SAlexander Motin 		ctl_set_invalid_field_ciu(&io->scsiio);
424f7241cceSAlexander Motin 	} else if ((io->io_hdr.port_status == 0) &&
42511b569f7SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
42611b569f7SAlexander Motin 		lbalen = ARGS(beio->io);
42711b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_READ) {
428130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
42911b569f7SAlexander Motin 		} else if (lbalen->flags & CTL_LLF_COMPARE) {
43011b569f7SAlexander Motin 			/* We have two data blocks ready for comparison. */
431d6043e46SAlexander Motin 			ctl_be_block_compare(io);
43211b569f7SAlexander Motin 		}
433130f4520SKenneth D. Merry 	}
434130f4520SKenneth D. Merry 
435130f4520SKenneth D. Merry 	/*
436130f4520SKenneth D. Merry 	 * If this is a read, or a write with errors, it is done.
437130f4520SKenneth D. Merry 	 */
438130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_READ)
439130f4520SKenneth D. Merry 	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
440130f4520SKenneth D. Merry 	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
441130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
442130f4520SKenneth D. Merry 		return (0);
443130f4520SKenneth D. Merry 	}
444130f4520SKenneth D. Merry 
445130f4520SKenneth D. Merry 	/*
446130f4520SKenneth D. Merry 	 * At this point, we have a write and the DMA completed
447130f4520SKenneth D. Merry 	 * successfully.  We now have to queue it to the task queue to
448130f4520SKenneth D. Merry 	 * execute the backend I/O.  That is because we do blocking
449130f4520SKenneth D. Merry 	 * memory allocations, and in the file backing case, blocking I/O.
450130f4520SKenneth D. Merry 	 * This move done routine is generally called in the SIM's
451130f4520SKenneth D. Merry 	 * interrupt context, and therefore we cannot block.
452130f4520SKenneth D. Merry 	 */
45375c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
454130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
45575c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
456130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
457130f4520SKenneth D. Merry 
458130f4520SKenneth D. Merry 	return (0);
459130f4520SKenneth D. Merry }
460130f4520SKenneth D. Merry 
461130f4520SKenneth D. Merry static void
462130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio)
463130f4520SKenneth D. Merry {
464130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
465130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
466130f4520SKenneth D. Merry 	union ctl_io *io;
467e0c2f975SAlexander Motin 	int error;
468130f4520SKenneth D. Merry 
469130f4520SKenneth D. Merry 	beio = bio->bio_caller1;
470130f4520SKenneth D. Merry 	be_lun = beio->lun;
471130f4520SKenneth D. Merry 	io = beio->io;
472130f4520SKenneth D. Merry 
473130f4520SKenneth D. Merry 	DPRINTF("entered\n");
474130f4520SKenneth D. Merry 
475e0c2f975SAlexander Motin 	error = bio->bio_error;
47675c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
4771f0694a6SAlexander Motin 	if (error != 0 &&
4781f0694a6SAlexander Motin 	    (beio->first_error == 0 ||
4791f0694a6SAlexander Motin 	     bio->bio_offset < beio->first_error_offset)) {
4801f0694a6SAlexander Motin 		beio->first_error = error;
4811f0694a6SAlexander Motin 		beio->first_error_offset = bio->bio_offset;
4821f0694a6SAlexander Motin 	}
483130f4520SKenneth D. Merry 
484130f4520SKenneth D. Merry 	beio->num_bios_done++;
485130f4520SKenneth D. Merry 
486130f4520SKenneth D. Merry 	/*
487130f4520SKenneth D. Merry 	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
488130f4520SKenneth D. Merry 	 * during the free might cause it to complain.
489130f4520SKenneth D. Merry 	 */
490130f4520SKenneth D. Merry 	g_destroy_bio(bio);
491130f4520SKenneth D. Merry 
492130f4520SKenneth D. Merry 	/*
493130f4520SKenneth D. Merry 	 * If the send complete bit isn't set, or we aren't the last I/O to
494130f4520SKenneth D. Merry 	 * complete, then we're done.
495130f4520SKenneth D. Merry 	 */
496130f4520SKenneth D. Merry 	if ((beio->send_complete == 0)
497130f4520SKenneth D. Merry 	 || (beio->num_bios_done < beio->num_bios_sent)) {
49875c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
499130f4520SKenneth D. Merry 		return;
500130f4520SKenneth D. Merry 	}
501130f4520SKenneth D. Merry 
502130f4520SKenneth D. Merry 	/*
503130f4520SKenneth D. Merry 	 * At this point, we've verified that we are the last I/O to
504130f4520SKenneth D. Merry 	 * complete, so it's safe to drop the lock.
505130f4520SKenneth D. Merry 	 */
50675c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
50775c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
50875c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
50975c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
510130f4520SKenneth D. Merry 
511130f4520SKenneth D. Merry 	/*
512130f4520SKenneth D. Merry 	 * If there are any errors from the backing device, we fail the
513130f4520SKenneth D. Merry 	 * entire I/O with a medium error.
514130f4520SKenneth D. Merry 	 */
5151f0694a6SAlexander Motin 	error = beio->first_error;
5161f0694a6SAlexander Motin 	if (error != 0) {
517e0c2f975SAlexander Motin 		if (error == EOPNOTSUPP) {
518e0c2f975SAlexander Motin 			ctl_set_invalid_opcode(&io->scsiio);
5190631de4aSAlexander Motin 		} else if (error == ENOSPC || error == EDQUOT) {
5204fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
5216187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
5226187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
523e0c2f975SAlexander Motin 		} else if (beio->bio_cmd == BIO_FLUSH) {
524130f4520SKenneth D. Merry 			/* XXX KDM is there is a better error here? */
525130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
526130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
527130f4520SKenneth D. Merry 						 /*retry_count*/ 0xbad2);
5287f7bb97aSAlexander Motin 		} else {
5297f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
5307f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
5317f7bb97aSAlexander Motin 		}
532130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
533130f4520SKenneth D. Merry 		return;
534130f4520SKenneth D. Merry 	}
535130f4520SKenneth D. Merry 
536130f4520SKenneth D. Merry 	/*
53711b569f7SAlexander Motin 	 * If this is a write, a flush, a delete or verify, we're all done.
538130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
539130f4520SKenneth D. Merry 	 */
540130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_WRITE)
541ee7f31c0SAlexander Motin 	 || (beio->bio_cmd == BIO_FLUSH)
54211b569f7SAlexander Motin 	 || (beio->bio_cmd == BIO_DELETE)
54311b569f7SAlexander Motin 	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
544130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
545130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
546130f4520SKenneth D. Merry 	} else {
547f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
54875a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
549f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
55075a3108eSAlexander Motin 			ctl_serseq_done(io);
55175a3108eSAlexander Motin 		}
552130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
553e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
554130f4520SKenneth D. Merry #endif
555130f4520SKenneth D. Merry 		ctl_datamove(io);
556130f4520SKenneth D. Merry 	}
557130f4520SKenneth D. Merry }
558130f4520SKenneth D. Merry 
559130f4520SKenneth D. Merry static void
560130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
561130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio)
562130f4520SKenneth D. Merry {
56375c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
564130f4520SKenneth D. Merry 	struct mount *mountpoint;
5655050aa86SKonstantin Belousov 	int error, lock_flags;
566130f4520SKenneth D. Merry 
567130f4520SKenneth D. Merry 	DPRINTF("entered\n");
568130f4520SKenneth D. Merry 
56975c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
57075c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
571130f4520SKenneth D. Merry 
572130f4520SKenneth D. Merry 	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
573130f4520SKenneth D. Merry 
57467cc546dSAlexander Motin 	if (MNT_SHARED_WRITES(mountpoint) ||
57567cc546dSAlexander Motin 	    ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
576130f4520SKenneth D. Merry 		lock_flags = LK_SHARED;
577130f4520SKenneth D. Merry 	else
578130f4520SKenneth D. Merry 		lock_flags = LK_EXCLUSIVE;
579130f4520SKenneth D. Merry 	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
5807d0d4342SAlexander Motin 	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
5817d0d4342SAlexander Motin 	    curthread);
582b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
583130f4520SKenneth D. Merry 
584130f4520SKenneth D. Merry 	vn_finished_write(mountpoint);
585130f4520SKenneth D. Merry 
58675c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
58775c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
58875c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
58975c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
59075c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
59175c7a1d3SAlexander Motin 
592130f4520SKenneth D. Merry 	if (error == 0)
593130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
594130f4520SKenneth D. Merry 	else {
595130f4520SKenneth D. Merry 		/* XXX KDM is there is a better error here? */
596130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
597130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
598130f4520SKenneth D. Merry 					 /*retry_count*/ 0xbad1);
599130f4520SKenneth D. Merry 	}
600130f4520SKenneth D. Merry 
601130f4520SKenneth D. Merry 	ctl_complete_beio(beio);
602130f4520SKenneth D. Merry }
603130f4520SKenneth D. Merry 
60436160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
60536160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
60636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
60736160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
608130f4520SKenneth D. Merry 
609130f4520SKenneth D. Merry static void
610130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
611130f4520SKenneth D. Merry 			   struct ctl_be_block_io *beio)
612130f4520SKenneth D. Merry {
613130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
614130f4520SKenneth D. Merry 	union ctl_io *io;
615130f4520SKenneth D. Merry 	struct uio xuio;
616130f4520SKenneth D. Merry 	struct iovec *xiovec;
61783981e31SAlexander Motin 	size_t s;
61883981e31SAlexander Motin 	int error, flags, i;
619130f4520SKenneth D. Merry 
620130f4520SKenneth D. Merry 	DPRINTF("entered\n");
621130f4520SKenneth D. Merry 
622130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
623130f4520SKenneth D. Merry 	io = beio->io;
62455551d05SAlexander Motin 	flags = 0;
62555551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
62655551d05SAlexander Motin 		flags |= IO_DIRECT;
62755551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
62855551d05SAlexander Motin 		flags |= IO_SYNC;
629130f4520SKenneth D. Merry 
63011b569f7SAlexander Motin 	bzero(&xuio, sizeof(xuio));
631130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
63236160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
63311b569f7SAlexander Motin 		xuio.uio_rw = UIO_READ;
634130f4520SKenneth D. Merry 	} else {
63536160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
636130f4520SKenneth D. Merry 		xuio.uio_rw = UIO_WRITE;
63711b569f7SAlexander Motin 	}
638130f4520SKenneth D. Merry 	xuio.uio_offset = beio->io_offset;
639130f4520SKenneth D. Merry 	xuio.uio_resid = beio->io_len;
640130f4520SKenneth D. Merry 	xuio.uio_segflg = UIO_SYSSPACE;
641130f4520SKenneth D. Merry 	xuio.uio_iov = beio->xiovecs;
642130f4520SKenneth D. Merry 	xuio.uio_iovcnt = beio->num_segs;
643130f4520SKenneth D. Merry 	xuio.uio_td = curthread;
644130f4520SKenneth D. Merry 
645130f4520SKenneth D. Merry 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
646130f4520SKenneth D. Merry 		xiovec->iov_base = beio->sg_segs[i].addr;
647130f4520SKenneth D. Merry 		xiovec->iov_len = beio->sg_segs[i].len;
648130f4520SKenneth D. Merry 	}
649130f4520SKenneth D. Merry 
65075c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
65175c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
65275c7a1d3SAlexander Motin 
653130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
654130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
655130f4520SKenneth D. Merry 
656130f4520SKenneth D. Merry 		/*
657130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for reads.  If the
658130f4520SKenneth D. Merry 		 * DIRECTIO option is configured into the kernel, it calls
659130f4520SKenneth D. Merry 		 * ffs_rawread().  But that only works for single-segment
660130f4520SKenneth D. Merry 		 * uios with user space addresses.  In our case, with a
661130f4520SKenneth D. Merry 		 * kernel uio, it still reads into the buffer cache, but it
662130f4520SKenneth D. Merry 		 * will just try to release the buffer from the cache later
663130f4520SKenneth D. Merry 		 * on in ffs_read().
664130f4520SKenneth D. Merry 		 *
665130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for reads.
666130f4520SKenneth D. Merry 		 *
667130f4520SKenneth D. Merry 		 * UFS does not pay attention to IO_SYNC for reads.
668130f4520SKenneth D. Merry 		 *
669130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (which translates into the
670130f4520SKenneth D. Merry 		 * Solaris define FRSYNC for zfs_read()) for reads.  It
671130f4520SKenneth D. Merry 		 * attempts to sync the file before reading.
672130f4520SKenneth D. Merry 		 */
67355551d05SAlexander Motin 		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
674130f4520SKenneth D. Merry 
675b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
67636160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
67783981e31SAlexander Motin 		if (error == 0 && xuio.uio_resid > 0) {
67883981e31SAlexander Motin 			/*
67983981e31SAlexander Motin 			 * If we red less then requested (EOF), then
68083981e31SAlexander Motin 			 * we should clean the rest of the buffer.
68183981e31SAlexander Motin 			 */
68283981e31SAlexander Motin 			s = beio->io_len - xuio.uio_resid;
68383981e31SAlexander Motin 			for (i = 0; i < beio->num_segs; i++) {
68483981e31SAlexander Motin 				if (s >= beio->sg_segs[i].len) {
68583981e31SAlexander Motin 					s -= beio->sg_segs[i].len;
68683981e31SAlexander Motin 					continue;
68783981e31SAlexander Motin 				}
68883981e31SAlexander Motin 				bzero((uint8_t *)beio->sg_segs[i].addr + s,
68983981e31SAlexander Motin 				    beio->sg_segs[i].len - s);
69083981e31SAlexander Motin 				s = 0;
69183981e31SAlexander Motin 			}
69283981e31SAlexander Motin 		}
693130f4520SKenneth D. Merry 	} else {
694130f4520SKenneth D. Merry 		struct mount *mountpoint;
695130f4520SKenneth D. Merry 		int lock_flags;
696130f4520SKenneth D. Merry 
697130f4520SKenneth D. Merry 		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
698130f4520SKenneth D. Merry 
69967cc546dSAlexander Motin 		if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL)
700130f4520SKenneth D. Merry 		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
701130f4520SKenneth D. Merry 			lock_flags = LK_SHARED;
702130f4520SKenneth D. Merry 		else
703130f4520SKenneth D. Merry 			lock_flags = LK_EXCLUSIVE;
704130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
705130f4520SKenneth D. Merry 
706130f4520SKenneth D. Merry 		/*
707130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for writes.  The write
708130f4520SKenneth D. Merry 		 * is done asynchronously.  (Normally the write would just
709130f4520SKenneth D. Merry 		 * get put into cache.
710130f4520SKenneth D. Merry 		 *
711130f4520SKenneth D. Merry 		 * UFS pays attention to IO_SYNC for writes.  It will
712130f4520SKenneth D. Merry 		 * attempt to write the buffer out synchronously if that
713130f4520SKenneth D. Merry 		 * flag is set.
714130f4520SKenneth D. Merry 		 *
715130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for writes.
716130f4520SKenneth D. Merry 		 *
717130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
718130f4520SKenneth D. Merry 		 * for writes.  It will flush the transaction from the
719130f4520SKenneth D. Merry 		 * cache before returning.
720130f4520SKenneth D. Merry 		 */
72155551d05SAlexander Motin 		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
722b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
723130f4520SKenneth D. Merry 
724130f4520SKenneth D. Merry 		vn_finished_write(mountpoint);
72536160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
726130f4520SKenneth D. Merry         }
727130f4520SKenneth D. Merry 
72875c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
72975c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
73075c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
73175c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
73275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
73375c7a1d3SAlexander Motin 
734130f4520SKenneth D. Merry 	/*
735130f4520SKenneth D. Merry 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
736130f4520SKenneth D. Merry 	 * return the I/O to the user.
737130f4520SKenneth D. Merry 	 */
738130f4520SKenneth D. Merry 	if (error != 0) {
7390631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
7404fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
7416187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
7426187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
7437f7bb97aSAlexander Motin 		} else {
7447f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
7457f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
7467f7bb97aSAlexander Motin 		}
747130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
748130f4520SKenneth D. Merry 		return;
749130f4520SKenneth D. Merry 	}
750130f4520SKenneth D. Merry 
751130f4520SKenneth D. Merry 	/*
752696297adSAlexander Motin 	 * If this is a write or a verify, we're all done.
753130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
754130f4520SKenneth D. Merry 	 */
755696297adSAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
756696297adSAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
757130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
758130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
759130f4520SKenneth D. Merry 	} else {
760f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
76175a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
762f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
76375a3108eSAlexander Motin 			ctl_serseq_done(io);
76475a3108eSAlexander Motin 		}
765130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
766e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
767130f4520SKenneth D. Merry #endif
768130f4520SKenneth D. Merry 		ctl_datamove(io);
769130f4520SKenneth D. Merry 	}
770130f4520SKenneth D. Merry }
771130f4520SKenneth D. Merry 
772130f4520SKenneth D. Merry static void
773ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
774ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
775ef8daf3fSAlexander Motin {
776ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
777ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
778ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
779ef8daf3fSAlexander Motin 	off_t roff, off;
780ef8daf3fSAlexander Motin 	int error, status;
781ef8daf3fSAlexander Motin 
782ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
783ef8daf3fSAlexander Motin 
7840bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
785ef8daf3fSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
786ef8daf3fSAlexander Motin 	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
787ef8daf3fSAlexander Motin 	    0, curthread->td_ucred, curthread);
788ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
789ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
790ef8daf3fSAlexander Motin 	else {
791ef8daf3fSAlexander Motin 		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
792ef8daf3fSAlexander Motin 		    0, curthread->td_ucred, curthread);
793ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
794ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
795ef8daf3fSAlexander Motin 		else {
796ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
797ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
798ef8daf3fSAlexander Motin 		}
799ef8daf3fSAlexander Motin 	}
800b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
801ef8daf3fSAlexander Motin 
802ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
803ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
8040bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
8050bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
806ef8daf3fSAlexander Motin 	data->descr[0].status = status;
807ef8daf3fSAlexander Motin 
808ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
809ef8daf3fSAlexander Motin }
810ef8daf3fSAlexander Motin 
81153c146deSAlexander Motin static uint64_t
81253c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
81353c146deSAlexander Motin {
81453c146deSAlexander Motin 	struct vattr		vattr;
81553c146deSAlexander Motin 	struct statfs		statfs;
816b9b4269cSAlexander Motin 	uint64_t		val;
81753c146deSAlexander Motin 	int			error;
81853c146deSAlexander Motin 
819b9b4269cSAlexander Motin 	val = UINT64_MAX;
82053c146deSAlexander Motin 	if (be_lun->vn == NULL)
821b9b4269cSAlexander Motin 		return (val);
822b9b4269cSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
82353c146deSAlexander Motin 	if (strcmp(attrname, "blocksused") == 0) {
82453c146deSAlexander Motin 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
825b9b4269cSAlexander Motin 		if (error == 0)
8260bcd4ab6SAlexander Motin 			val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
82753c146deSAlexander Motin 	}
828b9b4269cSAlexander Motin 	if (strcmp(attrname, "blocksavail") == 0 &&
829abd80ddbSMateusz Guzik 	    !VN_IS_DOOMED(be_lun->vn)) {
83053c146deSAlexander Motin 		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
831b9b4269cSAlexander Motin 		if (error == 0)
832a15bbf15SAlexander Motin 			val = statfs.f_bavail * statfs.f_bsize /
8330bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
83453c146deSAlexander Motin 	}
835b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
836b9b4269cSAlexander Motin 	return (val);
83753c146deSAlexander Motin }
83853c146deSAlexander Motin 
839ef8daf3fSAlexander Motin static void
84067f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
84167f586a8SAlexander Motin 			   struct ctl_be_block_io *beio)
84267f586a8SAlexander Motin {
84367f586a8SAlexander Motin 	union ctl_io *io;
8443236151eSAlexander Motin 	struct cdevsw *csw;
8453236151eSAlexander Motin 	struct cdev *dev;
84667f586a8SAlexander Motin 	struct uio xuio;
84767f586a8SAlexander Motin 	struct iovec *xiovec;
8483236151eSAlexander Motin 	int error, flags, i, ref;
84967f586a8SAlexander Motin 
85067f586a8SAlexander Motin 	DPRINTF("entered\n");
85167f586a8SAlexander Motin 
85267f586a8SAlexander Motin 	io = beio->io;
85355551d05SAlexander Motin 	flags = 0;
85455551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
85555551d05SAlexander Motin 		flags |= IO_DIRECT;
85655551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
85755551d05SAlexander Motin 		flags |= IO_SYNC;
85867f586a8SAlexander Motin 
85967f586a8SAlexander Motin 	bzero(&xuio, sizeof(xuio));
86067f586a8SAlexander Motin 	if (beio->bio_cmd == BIO_READ) {
86136160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
86267f586a8SAlexander Motin 		xuio.uio_rw = UIO_READ;
86367f586a8SAlexander Motin 	} else {
86436160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
86567f586a8SAlexander Motin 		xuio.uio_rw = UIO_WRITE;
86667f586a8SAlexander Motin 	}
86767f586a8SAlexander Motin 	xuio.uio_offset = beio->io_offset;
86867f586a8SAlexander Motin 	xuio.uio_resid = beio->io_len;
86967f586a8SAlexander Motin 	xuio.uio_segflg = UIO_SYSSPACE;
87067f586a8SAlexander Motin 	xuio.uio_iov = beio->xiovecs;
87167f586a8SAlexander Motin 	xuio.uio_iovcnt = beio->num_segs;
87267f586a8SAlexander Motin 	xuio.uio_td = curthread;
87367f586a8SAlexander Motin 
87467f586a8SAlexander Motin 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
87567f586a8SAlexander Motin 		xiovec->iov_base = beio->sg_segs[i].addr;
87667f586a8SAlexander Motin 		xiovec->iov_len = beio->sg_segs[i].len;
87767f586a8SAlexander Motin 	}
87867f586a8SAlexander Motin 
87967f586a8SAlexander Motin 	binuptime(&beio->ds_t0);
88067f586a8SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
88167f586a8SAlexander Motin 
8823236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
8833236151eSAlexander Motin 	if (csw) {
8843236151eSAlexander Motin 		if (beio->bio_cmd == BIO_READ)
8853236151eSAlexander Motin 			error = csw->d_read(dev, &xuio, flags);
8863236151eSAlexander Motin 		else
8873236151eSAlexander Motin 			error = csw->d_write(dev, &xuio, flags);
8883236151eSAlexander Motin 		dev_relthread(dev, ref);
8893236151eSAlexander Motin 	} else
8903236151eSAlexander Motin 		error = ENXIO;
8913236151eSAlexander Motin 
8923236151eSAlexander Motin 	if (beio->bio_cmd == BIO_READ)
89336160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
8943236151eSAlexander Motin 	else
89536160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
89667f586a8SAlexander Motin 
89767f586a8SAlexander Motin 	mtx_lock(&be_lun->io_lock);
89867f586a8SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
89967f586a8SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
90067f586a8SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
90167f586a8SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
90267f586a8SAlexander Motin 
90367f586a8SAlexander Motin 	/*
90467f586a8SAlexander Motin 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
90567f586a8SAlexander Motin 	 * return the I/O to the user.
90667f586a8SAlexander Motin 	 */
90767f586a8SAlexander Motin 	if (error != 0) {
9080631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
9094fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
9106187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
9116187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
9127f7bb97aSAlexander Motin 		} else {
9137f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
9147f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
9157f7bb97aSAlexander Motin 		}
91667f586a8SAlexander Motin 		ctl_complete_beio(beio);
91767f586a8SAlexander Motin 		return;
91867f586a8SAlexander Motin 	}
91967f586a8SAlexander Motin 
92067f586a8SAlexander Motin 	/*
92167f586a8SAlexander Motin 	 * If this is a write or a verify, we're all done.
92267f586a8SAlexander Motin 	 * If this is a read, we can now send the data to the user.
92367f586a8SAlexander Motin 	 */
92467f586a8SAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
92567f586a8SAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
92667f586a8SAlexander Motin 		ctl_set_success(&io->scsiio);
92767f586a8SAlexander Motin 		ctl_complete_beio(beio);
92867f586a8SAlexander Motin 	} else {
929f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
93075a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
931f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
93275a3108eSAlexander Motin 			ctl_serseq_done(io);
93375a3108eSAlexander Motin 		}
93467f586a8SAlexander Motin #ifdef CTL_TIME_IO
935e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
93667f586a8SAlexander Motin #endif
93767f586a8SAlexander Motin 		ctl_datamove(io);
93867f586a8SAlexander Motin 	}
93967f586a8SAlexander Motin }
94067f586a8SAlexander Motin 
94167f586a8SAlexander Motin static void
942ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
943ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
944ef8daf3fSAlexander Motin {
945ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
9463236151eSAlexander Motin 	struct cdevsw *csw;
9473236151eSAlexander Motin 	struct cdev *dev;
948ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
949ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
950ef8daf3fSAlexander Motin 	off_t roff, off;
9513236151eSAlexander Motin 	int error, ref, status;
952ef8daf3fSAlexander Motin 
953ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
954ef8daf3fSAlexander Motin 
9553236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
9563236151eSAlexander Motin 	if (csw == NULL) {
9573236151eSAlexander Motin 		status = 0;	/* unknown up to the end */
9583236151eSAlexander Motin 		off = be_lun->size_bytes;
9593236151eSAlexander Motin 		goto done;
9603236151eSAlexander Motin 	}
9610bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
9623236151eSAlexander Motin 	error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
9633236151eSAlexander Motin 	    curthread);
964ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
965ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
966ef8daf3fSAlexander Motin 	else {
9673236151eSAlexander Motin 		error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
9683236151eSAlexander Motin 		    curthread);
969ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
970ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
971ef8daf3fSAlexander Motin 		else {
972ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
973ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
974ef8daf3fSAlexander Motin 		}
975ef8daf3fSAlexander Motin 	}
9763236151eSAlexander Motin 	dev_relthread(dev, ref);
977ef8daf3fSAlexander Motin 
9783236151eSAlexander Motin done:
979ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
980ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
9810bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
9820bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
983ef8daf3fSAlexander Motin 	data->descr[0].status = status;
984ef8daf3fSAlexander Motin 
985ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
986ef8daf3fSAlexander Motin }
987ef8daf3fSAlexander Motin 
988ef8daf3fSAlexander Motin static void
989130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
990130f4520SKenneth D. Merry 		       struct ctl_be_block_io *beio)
991130f4520SKenneth D. Merry {
992130f4520SKenneth D. Merry 	struct bio *bio;
9933236151eSAlexander Motin 	struct cdevsw *csw;
9943236151eSAlexander Motin 	struct cdev *dev;
9953236151eSAlexander Motin 	int ref;
996130f4520SKenneth D. Merry 
997130f4520SKenneth D. Merry 	DPRINTF("entered\n");
998130f4520SKenneth D. Merry 
999130f4520SKenneth D. Merry 	/* This can't fail, it's a blocking allocation. */
1000130f4520SKenneth D. Merry 	bio = g_alloc_bio();
1001130f4520SKenneth D. Merry 
1002130f4520SKenneth D. Merry 	bio->bio_cmd	    = BIO_FLUSH;
1003130f4520SKenneth D. Merry 	bio->bio_offset	    = 0;
1004130f4520SKenneth D. Merry 	bio->bio_data	    = 0;
1005130f4520SKenneth D. Merry 	bio->bio_done	    = ctl_be_block_biodone;
1006130f4520SKenneth D. Merry 	bio->bio_caller1    = beio;
1007130f4520SKenneth D. Merry 	bio->bio_pblkno	    = 0;
1008130f4520SKenneth D. Merry 
1009130f4520SKenneth D. Merry 	/*
1010130f4520SKenneth D. Merry 	 * We don't need to acquire the LUN lock here, because we are only
1011130f4520SKenneth D. Merry 	 * sending one bio, and so there is no other context to synchronize
1012130f4520SKenneth D. Merry 	 * with.
1013130f4520SKenneth D. Merry 	 */
1014130f4520SKenneth D. Merry 	beio->num_bios_sent = 1;
1015130f4520SKenneth D. Merry 	beio->send_complete = 1;
1016130f4520SKenneth D. Merry 
1017130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
1018130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1019130f4520SKenneth D. Merry 
10203236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10213236151eSAlexander Motin 	if (csw) {
10223236151eSAlexander Motin 		bio->bio_dev = dev;
10233236151eSAlexander Motin 		csw->d_strategy(bio);
10243236151eSAlexander Motin 		dev_relthread(dev, ref);
10253236151eSAlexander Motin 	} else {
10263236151eSAlexander Motin 		bio->bio_error = ENXIO;
10273236151eSAlexander Motin 		ctl_be_block_biodone(bio);
10283236151eSAlexander Motin 	}
1029130f4520SKenneth D. Merry }
1030130f4520SKenneth D. Merry 
1031130f4520SKenneth D. Merry static void
1032ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1033ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio,
1034ee7f31c0SAlexander Motin 		       uint64_t off, uint64_t len, int last)
1035ee7f31c0SAlexander Motin {
1036ee7f31c0SAlexander Motin 	struct bio *bio;
10378f5a226aSAlexander Motin 	uint64_t maxlen;
10383236151eSAlexander Motin 	struct cdevsw *csw;
10393236151eSAlexander Motin 	struct cdev *dev;
10403236151eSAlexander Motin 	int ref;
1041ee7f31c0SAlexander Motin 
10423236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10430bcd4ab6SAlexander Motin 	maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1044ee7f31c0SAlexander Motin 	while (len > 0) {
1045ee7f31c0SAlexander Motin 		bio = g_alloc_bio();
1046ee7f31c0SAlexander Motin 		bio->bio_cmd	    = BIO_DELETE;
10473236151eSAlexander Motin 		bio->bio_dev	    = dev;
1048ee7f31c0SAlexander Motin 		bio->bio_offset	    = off;
10498f5a226aSAlexander Motin 		bio->bio_length	    = MIN(len, maxlen);
1050ee7f31c0SAlexander Motin 		bio->bio_data	    = 0;
1051ee7f31c0SAlexander Motin 		bio->bio_done	    = ctl_be_block_biodone;
1052ee7f31c0SAlexander Motin 		bio->bio_caller1    = beio;
10530bcd4ab6SAlexander Motin 		bio->bio_pblkno     = off / be_lun->cbe_lun.blocksize;
1054ee7f31c0SAlexander Motin 
1055ee7f31c0SAlexander Motin 		off += bio->bio_length;
1056ee7f31c0SAlexander Motin 		len -= bio->bio_length;
1057ee7f31c0SAlexander Motin 
105875c7a1d3SAlexander Motin 		mtx_lock(&be_lun->io_lock);
1059ee7f31c0SAlexander Motin 		beio->num_bios_sent++;
1060ee7f31c0SAlexander Motin 		if (last && len == 0)
1061ee7f31c0SAlexander Motin 			beio->send_complete = 1;
106275c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
1063ee7f31c0SAlexander Motin 
10643236151eSAlexander Motin 		if (csw) {
10653236151eSAlexander Motin 			csw->d_strategy(bio);
10663236151eSAlexander Motin 		} else {
10673236151eSAlexander Motin 			bio->bio_error = ENXIO;
10683236151eSAlexander Motin 			ctl_be_block_biodone(bio);
1069ee7f31c0SAlexander Motin 		}
1070ee7f31c0SAlexander Motin 	}
10713236151eSAlexander Motin 	if (csw)
10723236151eSAlexander Motin 		dev_relthread(dev, ref);
10733236151eSAlexander Motin }
1074ee7f31c0SAlexander Motin 
1075ee7f31c0SAlexander Motin static void
1076ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1077ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio)
1078ee7f31c0SAlexander Motin {
1079ee7f31c0SAlexander Motin 	union ctl_io *io;
108066df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1081ee7f31c0SAlexander Motin 	struct scsi_unmap_desc *buf, *end;
1082ee7f31c0SAlexander Motin 	uint64_t len;
1083ee7f31c0SAlexander Motin 
1084ee7f31c0SAlexander Motin 	io = beio->io;
1085ee7f31c0SAlexander Motin 
1086ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1087ee7f31c0SAlexander Motin 
1088ee7f31c0SAlexander Motin 	binuptime(&beio->ds_t0);
1089ee7f31c0SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1090ee7f31c0SAlexander Motin 
1091ee7f31c0SAlexander Motin 	if (beio->io_offset == -1) {
1092ee7f31c0SAlexander Motin 		beio->io_len = 0;
109366df9136SAlexander Motin 		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
109466df9136SAlexander Motin 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
109566df9136SAlexander Motin 		end = buf + ptrlen->len / sizeof(*buf);
1096ee7f31c0SAlexander Motin 		for (; buf < end; buf++) {
1097ee7f31c0SAlexander Motin 			len = (uint64_t)scsi_4btoul(buf->length) *
10980bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
1099ee7f31c0SAlexander Motin 			beio->io_len += len;
1100ee7f31c0SAlexander Motin 			ctl_be_block_unmap_dev_range(be_lun, beio,
11010bcd4ab6SAlexander Motin 			    scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
11020bcd4ab6SAlexander Motin 			    len, (end - buf < 2) ? TRUE : FALSE);
1103ee7f31c0SAlexander Motin 		}
1104ee7f31c0SAlexander Motin 	} else
1105ee7f31c0SAlexander Motin 		ctl_be_block_unmap_dev_range(be_lun, beio,
1106ee7f31c0SAlexander Motin 		    beio->io_offset, beio->io_len, TRUE);
1107ee7f31c0SAlexander Motin }
1108ee7f31c0SAlexander Motin 
1109ee7f31c0SAlexander Motin static void
1110130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1111130f4520SKenneth D. Merry 			  struct ctl_be_block_io *beio)
1112130f4520SKenneth D. Merry {
111375c7a1d3SAlexander Motin 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1114130f4520SKenneth D. Merry 	struct bio *bio;
11153236151eSAlexander Motin 	struct cdevsw *csw;
11163236151eSAlexander Motin 	struct cdev *dev;
1117130f4520SKenneth D. Merry 	off_t cur_offset;
11183236151eSAlexander Motin 	int i, max_iosize, ref;
1119130f4520SKenneth D. Merry 
1120130f4520SKenneth D. Merry 	DPRINTF("entered\n");
11213236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1122130f4520SKenneth D. Merry 
1123130f4520SKenneth D. Merry 	/*
1124130f4520SKenneth D. Merry 	 * We have to limit our I/O size to the maximum supported by the
1125130f4520SKenneth D. Merry 	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
1126130f4520SKenneth D. Merry 	 * set it properly, use DFLTPHYS.
1127130f4520SKenneth D. Merry 	 */
11283236151eSAlexander Motin 	if (csw) {
11293236151eSAlexander Motin 		max_iosize = dev->si_iosize_max;
1130130f4520SKenneth D. Merry 		if (max_iosize < PAGE_SIZE)
1131130f4520SKenneth D. Merry 			max_iosize = DFLTPHYS;
11323236151eSAlexander Motin 	} else
11333236151eSAlexander Motin 		max_iosize = DFLTPHYS;
1134130f4520SKenneth D. Merry 
1135130f4520SKenneth D. Merry 	cur_offset = beio->io_offset;
1136130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
1137130f4520SKenneth D. Merry 		size_t cur_size;
1138130f4520SKenneth D. Merry 		uint8_t *cur_ptr;
1139130f4520SKenneth D. Merry 
1140130f4520SKenneth D. Merry 		cur_size = beio->sg_segs[i].len;
1141130f4520SKenneth D. Merry 		cur_ptr = beio->sg_segs[i].addr;
1142130f4520SKenneth D. Merry 
1143130f4520SKenneth D. Merry 		while (cur_size > 0) {
1144130f4520SKenneth D. Merry 			/* This can't fail, it's a blocking allocation. */
1145130f4520SKenneth D. Merry 			bio = g_alloc_bio();
1146130f4520SKenneth D. Merry 
1147130f4520SKenneth D. Merry 			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1148130f4520SKenneth D. Merry 
1149130f4520SKenneth D. Merry 			bio->bio_cmd = beio->bio_cmd;
11503236151eSAlexander Motin 			bio->bio_dev = dev;
1151130f4520SKenneth D. Merry 			bio->bio_caller1 = beio;
1152130f4520SKenneth D. Merry 			bio->bio_length = min(cur_size, max_iosize);
1153130f4520SKenneth D. Merry 			bio->bio_offset = cur_offset;
1154130f4520SKenneth D. Merry 			bio->bio_data = cur_ptr;
1155130f4520SKenneth D. Merry 			bio->bio_done = ctl_be_block_biodone;
11560bcd4ab6SAlexander Motin 			bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1157130f4520SKenneth D. Merry 
1158130f4520SKenneth D. Merry 			cur_offset += bio->bio_length;
1159130f4520SKenneth D. Merry 			cur_ptr += bio->bio_length;
1160130f4520SKenneth D. Merry 			cur_size -= bio->bio_length;
1161130f4520SKenneth D. Merry 
116275c7a1d3SAlexander Motin 			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1163130f4520SKenneth D. Merry 			beio->num_bios_sent++;
1164130f4520SKenneth D. Merry 		}
1165130f4520SKenneth D. Merry 	}
116675c7a1d3SAlexander Motin 	beio->send_complete = 1;
1167024932aaSAlexander Motin 	binuptime(&beio->ds_t0);
1168024932aaSAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
116975c7a1d3SAlexander Motin 
117075c7a1d3SAlexander Motin 	/*
117175c7a1d3SAlexander Motin 	 * Fire off all allocated requests!
117275c7a1d3SAlexander Motin 	 */
117375c7a1d3SAlexander Motin 	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
117475c7a1d3SAlexander Motin 		TAILQ_REMOVE(&queue, bio, bio_queue);
11753236151eSAlexander Motin 		if (csw)
11763236151eSAlexander Motin 			csw->d_strategy(bio);
11773236151eSAlexander Motin 		else {
11783236151eSAlexander Motin 			bio->bio_error = ENXIO;
11793236151eSAlexander Motin 			ctl_be_block_biodone(bio);
118075c7a1d3SAlexander Motin 		}
1181130f4520SKenneth D. Merry 	}
11823236151eSAlexander Motin 	if (csw)
11833236151eSAlexander Motin 		dev_relthread(dev, ref);
11843236151eSAlexander Motin }
1185130f4520SKenneth D. Merry 
1186c3e7ba3eSAlexander Motin static uint64_t
1187c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1188c3e7ba3eSAlexander Motin {
1189c3e7ba3eSAlexander Motin 	struct diocgattr_arg	arg;
11903236151eSAlexander Motin 	struct cdevsw *csw;
11913236151eSAlexander Motin 	struct cdev *dev;
11923236151eSAlexander Motin 	int error, ref;
1193c3e7ba3eSAlexander Motin 
11943236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
11953236151eSAlexander Motin 	if (csw == NULL)
1196c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1197c3e7ba3eSAlexander Motin 	strlcpy(arg.name, attrname, sizeof(arg.name));
1198c3e7ba3eSAlexander Motin 	arg.len = sizeof(arg.value.off);
11993236151eSAlexander Motin 	if (csw->d_ioctl) {
12003236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
12013236151eSAlexander Motin 		    curthread);
12023236151eSAlexander Motin 	} else
12033236151eSAlexander Motin 		error = ENODEV;
12043236151eSAlexander Motin 	dev_relthread(dev, ref);
1205c3e7ba3eSAlexander Motin 	if (error != 0)
1206c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1207c3e7ba3eSAlexander Motin 	return (arg.value.off);
1208c3e7ba3eSAlexander Motin }
1209c3e7ba3eSAlexander Motin 
1210130f4520SKenneth D. Merry static void
12117d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
12127d0d4342SAlexander Motin 			    union ctl_io *io)
12137d0d4342SAlexander Motin {
12140bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
12157d0d4342SAlexander Motin 	struct ctl_be_block_io *beio;
12167d0d4342SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
12177d0d4342SAlexander Motin 
12187d0d4342SAlexander Motin 	DPRINTF("entered\n");
12197d0d4342SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
12207d0d4342SAlexander Motin 	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
12217d0d4342SAlexander Motin 
12220bcd4ab6SAlexander Motin 	beio->io_len = lbalen->len * cbe_lun->blocksize;
12230bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
12247d0d4342SAlexander Motin 	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
12257d0d4342SAlexander Motin 	beio->bio_cmd = BIO_FLUSH;
12267d0d4342SAlexander Motin 	beio->ds_trans_type = DEVSTAT_NO_DATA;
12277d0d4342SAlexander Motin 	DPRINTF("SYNC\n");
12287d0d4342SAlexander Motin 	be_lun->lun_flush(be_lun, beio);
12297d0d4342SAlexander Motin }
12307d0d4342SAlexander Motin 
12317d0d4342SAlexander Motin static void
1232ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1233ee7f31c0SAlexander Motin {
1234ee7f31c0SAlexander Motin 	union ctl_io *io;
1235ee7f31c0SAlexander Motin 
1236ee7f31c0SAlexander Motin 	io = beio->io;
1237ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1238ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1239ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1240ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1241ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1242ee7f31c0SAlexander Motin 		return;
1243ee7f31c0SAlexander Motin 	}
1244ee7f31c0SAlexander Motin 
1245ee7f31c0SAlexander Motin 	ctl_be_block_config_write(io);
1246ee7f31c0SAlexander Motin }
1247ee7f31c0SAlexander Motin 
1248ee7f31c0SAlexander Motin static void
1249ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1250ee7f31c0SAlexander Motin 			    union ctl_io *io)
1251ee7f31c0SAlexander Motin {
12520d7fed74SAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
12530bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1254ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
125566df9136SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1256fee04ef7SAlexander Motin 	uint64_t len_left, lba;
1257fee04ef7SAlexander Motin 	uint32_t pb, pbo, adj;
1258ee7f31c0SAlexander Motin 	int i, seglen;
1259ee7f31c0SAlexander Motin 	uint8_t *buf, *end;
1260ee7f31c0SAlexander Motin 
1261ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1262ee7f31c0SAlexander Motin 
1263e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
126411b569f7SAlexander Motin 	lbalen = ARGS(beio->io);
1265ee7f31c0SAlexander Motin 
126664c5167cSAlexander Motin 	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
12673406a2a0SAlexander Motin 	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1268ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1269ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1270ee7f31c0SAlexander Motin 				      /*sks_valid*/ 1,
1271ee7f31c0SAlexander Motin 				      /*command*/ 1,
1272ee7f31c0SAlexander Motin 				      /*field*/ 1,
1273ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1274ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1275ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1276ee7f31c0SAlexander Motin 		return;
1277ee7f31c0SAlexander Motin 	}
1278ee7f31c0SAlexander Motin 
12793406a2a0SAlexander Motin 	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
12800bcd4ab6SAlexander Motin 		beio->io_offset = lbalen->lba * cbe_lun->blocksize;
12810bcd4ab6SAlexander Motin 		beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1282ee7f31c0SAlexander Motin 		beio->bio_cmd = BIO_DELETE;
1283ee7f31c0SAlexander Motin 		beio->ds_trans_type = DEVSTAT_FREE;
1284ee7f31c0SAlexander Motin 
1285ee7f31c0SAlexander Motin 		be_lun->unmap(be_lun, beio);
1286ee7f31c0SAlexander Motin 		return;
1287ee7f31c0SAlexander Motin 	}
1288ee7f31c0SAlexander Motin 
1289ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_WRITE;
1290ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_WRITE;
1291ee7f31c0SAlexander Motin 
1292ee7f31c0SAlexander Motin 	DPRINTF("WRITE SAME at LBA %jx len %u\n",
129366df9136SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len);
1294ee7f31c0SAlexander Motin 
12950bcd4ab6SAlexander Motin 	pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
12960bcd4ab6SAlexander Motin 	if (be_lun->cbe_lun.pblockoff > 0)
12970bcd4ab6SAlexander Motin 		pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1298fee04ef7SAlexander Motin 	else
1299fee04ef7SAlexander Motin 		pbo = 0;
13000bcd4ab6SAlexander Motin 	len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1301ee7f31c0SAlexander Motin 	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1302ee7f31c0SAlexander Motin 
1303ee7f31c0SAlexander Motin 		/*
1304ee7f31c0SAlexander Motin 		 * Setup the S/G entry for this chunk.
1305ee7f31c0SAlexander Motin 		 */
130608a7cce5SAlexander Motin 		seglen = MIN(CTLBLK_MAX_SEG, len_left);
13070bcd4ab6SAlexander Motin 		if (pb > cbe_lun->blocksize) {
13080bcd4ab6SAlexander Motin 			adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
130993b8c96cSAlexander Motin 			    seglen - pbo) % pb;
131093b8c96cSAlexander Motin 			if (seglen > adj)
131193b8c96cSAlexander Motin 				seglen -= adj;
131293b8c96cSAlexander Motin 			else
13130bcd4ab6SAlexander Motin 				seglen -= seglen % cbe_lun->blocksize;
131493b8c96cSAlexander Motin 		} else
13150bcd4ab6SAlexander Motin 			seglen -= seglen % cbe_lun->blocksize;
1316ee7f31c0SAlexander Motin 		beio->sg_segs[i].len = seglen;
13170d7fed74SAlexander Motin 		beio->sg_segs[i].addr = uma_zalloc(softc->buf_zone, M_WAITOK);
1318ee7f31c0SAlexander Motin 
1319ee7f31c0SAlexander Motin 		DPRINTF("segment %d addr %p len %zd\n", i,
1320ee7f31c0SAlexander Motin 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1321ee7f31c0SAlexander Motin 
1322ee7f31c0SAlexander Motin 		beio->num_segs++;
1323ee7f31c0SAlexander Motin 		len_left -= seglen;
1324ee7f31c0SAlexander Motin 
1325ee7f31c0SAlexander Motin 		buf = beio->sg_segs[i].addr;
1326ee7f31c0SAlexander Motin 		end = buf + seglen;
13270bcd4ab6SAlexander Motin 		for (; buf < end; buf += cbe_lun->blocksize) {
13286c2acea5SAlexander Motin 			if (lbalen->flags & SWS_NDOB) {
13296c2acea5SAlexander Motin 				memset(buf, 0, cbe_lun->blocksize);
13306c2acea5SAlexander Motin 			} else {
13316c2acea5SAlexander Motin 				memcpy(buf, io->scsiio.kern_data_ptr,
13326c2acea5SAlexander Motin 				    cbe_lun->blocksize);
13336c2acea5SAlexander Motin 			}
133466df9136SAlexander Motin 			if (lbalen->flags & SWS_LBDATA)
133566df9136SAlexander Motin 				scsi_ulto4b(lbalen->lba + lba, buf);
1336ee7f31c0SAlexander Motin 			lba++;
1337ee7f31c0SAlexander Motin 		}
1338ee7f31c0SAlexander Motin 	}
1339ee7f31c0SAlexander Motin 
13400bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13410bcd4ab6SAlexander Motin 	beio->io_len = lba * cbe_lun->blocksize;
1342ee7f31c0SAlexander Motin 
1343ee7f31c0SAlexander Motin 	/* We can not do all in one run. Correct and schedule rerun. */
1344ee7f31c0SAlexander Motin 	if (len_left > 0) {
134566df9136SAlexander Motin 		lbalen->lba += lba;
134666df9136SAlexander Motin 		lbalen->len -= lba;
1347ee7f31c0SAlexander Motin 		beio->beio_cont = ctl_be_block_cw_done_ws;
1348ee7f31c0SAlexander Motin 	}
1349ee7f31c0SAlexander Motin 
1350ee7f31c0SAlexander Motin 	be_lun->dispatch(be_lun, beio);
1351ee7f31c0SAlexander Motin }
1352ee7f31c0SAlexander Motin 
1353ee7f31c0SAlexander Motin static void
1354ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1355ee7f31c0SAlexander Motin 			    union ctl_io *io)
1356ee7f31c0SAlexander Motin {
1357ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
135866df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1359ee7f31c0SAlexander Motin 
1360ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1361ee7f31c0SAlexander Motin 
1362e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
136366df9136SAlexander Motin 	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1364ee7f31c0SAlexander Motin 
13653406a2a0SAlexander Motin 	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1366ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1367ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1368ee7f31c0SAlexander Motin 				      /*sks_valid*/ 0,
1369ee7f31c0SAlexander Motin 				      /*command*/ 1,
1370ee7f31c0SAlexander Motin 				      /*field*/ 0,
1371ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1372ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1373ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1374ee7f31c0SAlexander Motin 		return;
1375ee7f31c0SAlexander Motin 	}
1376ee7f31c0SAlexander Motin 
1377ee7f31c0SAlexander Motin 	beio->io_len = 0;
1378ee7f31c0SAlexander Motin 	beio->io_offset = -1;
1379ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_DELETE;
1380ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_FREE;
138166df9136SAlexander Motin 	DPRINTF("UNMAP\n");
1382ee7f31c0SAlexander Motin 	be_lun->unmap(be_lun, beio);
1383ee7f31c0SAlexander Motin }
1384ee7f31c0SAlexander Motin 
1385ee7f31c0SAlexander Motin static void
1386ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1387ef8daf3fSAlexander Motin {
1388ef8daf3fSAlexander Motin 	union ctl_io *io;
1389ef8daf3fSAlexander Motin 
1390ef8daf3fSAlexander Motin 	io = beio->io;
1391ef8daf3fSAlexander Motin 	ctl_free_beio(beio);
1392ef8daf3fSAlexander Motin 	ctl_config_read_done(io);
1393ef8daf3fSAlexander Motin }
1394ef8daf3fSAlexander Motin 
1395ef8daf3fSAlexander Motin static void
1396ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1397ef8daf3fSAlexander Motin 			 union ctl_io *io)
1398ef8daf3fSAlexander Motin {
1399ef8daf3fSAlexander Motin 	struct ctl_be_block_io *beio;
1400ef8daf3fSAlexander Motin 	struct ctl_be_block_softc *softc;
1401ef8daf3fSAlexander Motin 
1402ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1403ef8daf3fSAlexander Motin 
1404ef8daf3fSAlexander Motin 	softc = be_lun->softc;
1405ef8daf3fSAlexander Motin 	beio = ctl_alloc_beio(softc);
1406ef8daf3fSAlexander Motin 	beio->io = io;
1407ef8daf3fSAlexander Motin 	beio->lun = be_lun;
1408ef8daf3fSAlexander Motin 	beio->beio_cont = ctl_be_block_cr_done;
1409ef8daf3fSAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1410ef8daf3fSAlexander Motin 
1411ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
1412ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1413ef8daf3fSAlexander Motin 		beio->bio_cmd = -1;
1414ef8daf3fSAlexander Motin 		beio->ds_trans_type = DEVSTAT_NO_DATA;
1415ef8daf3fSAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1416ef8daf3fSAlexander Motin 		beio->io_len = 0;
1417ef8daf3fSAlexander Motin 		if (be_lun->get_lba_status)
1418ef8daf3fSAlexander Motin 			be_lun->get_lba_status(be_lun, beio);
1419ef8daf3fSAlexander Motin 		else
1420ef8daf3fSAlexander Motin 			ctl_be_block_cr_done(beio);
1421ef8daf3fSAlexander Motin 		break;
1422ef8daf3fSAlexander Motin 	default:
1423ef8daf3fSAlexander Motin 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1424ef8daf3fSAlexander Motin 		break;
1425ef8daf3fSAlexander Motin 	}
1426ef8daf3fSAlexander Motin }
1427ef8daf3fSAlexander Motin 
1428ef8daf3fSAlexander Motin static void
1429ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1430ee7f31c0SAlexander Motin {
1431ee7f31c0SAlexander Motin 	union ctl_io *io;
1432ee7f31c0SAlexander Motin 
1433ee7f31c0SAlexander Motin 	io = beio->io;
1434ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1435ee7f31c0SAlexander Motin 	ctl_config_write_done(io);
1436ee7f31c0SAlexander Motin }
1437ee7f31c0SAlexander Motin 
1438ee7f31c0SAlexander Motin static void
1439130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1440130f4520SKenneth D. Merry 			 union ctl_io *io)
1441130f4520SKenneth D. Merry {
1442130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1443130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1444130f4520SKenneth D. Merry 
1445130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1446130f4520SKenneth D. Merry 
1447130f4520SKenneth D. Merry 	softc = be_lun->softc;
1448130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1449130f4520SKenneth D. Merry 	beio->io = io;
1450130f4520SKenneth D. Merry 	beio->lun = be_lun;
1451ee7f31c0SAlexander Motin 	beio->beio_cont = ctl_be_block_cw_done;
14527d0d4342SAlexander Motin 	switch (io->scsiio.tag_type) {
14537d0d4342SAlexander Motin 	case CTL_TAG_ORDERED:
14547d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
14557d0d4342SAlexander Motin 		break;
14567d0d4342SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
14577d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
14587d0d4342SAlexander Motin 		break;
14597d0d4342SAlexander Motin 	case CTL_TAG_UNTAGGED:
14607d0d4342SAlexander Motin 	case CTL_TAG_SIMPLE:
14617d0d4342SAlexander Motin 	case CTL_TAG_ACA:
14627d0d4342SAlexander Motin 	default:
14637d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
14647d0d4342SAlexander Motin 		break;
14657d0d4342SAlexander Motin 	}
1466e86a4142SAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1467130f4520SKenneth D. Merry 
1468130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
1469130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
1470130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
14717d0d4342SAlexander Motin 		ctl_be_block_cw_dispatch_sync(be_lun, io);
1472130f4520SKenneth D. Merry 		break;
1473ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
1474ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
1475ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_ws(be_lun, io);
1476ee7f31c0SAlexander Motin 		break;
1477ee7f31c0SAlexander Motin 	case UNMAP:
1478ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1479ee7f31c0SAlexander Motin 		break;
1480130f4520SKenneth D. Merry 	default:
1481130f4520SKenneth D. Merry 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1482130f4520SKenneth D. Merry 		break;
1483130f4520SKenneth D. Merry 	}
1484130f4520SKenneth D. Merry }
1485130f4520SKenneth D. Merry 
148636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
148736160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
148836160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
148936160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1490130f4520SKenneth D. Merry 
1491130f4520SKenneth D. Merry static void
149208a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio)
149308a7cce5SAlexander Motin {
149408a7cce5SAlexander Motin 	struct ctl_be_block_lun *be_lun;
149508a7cce5SAlexander Motin 	union ctl_io *io;
149608a7cce5SAlexander Motin 
149708a7cce5SAlexander Motin 	io = beio->io;
149808a7cce5SAlexander Motin 	be_lun = beio->lun;
149908a7cce5SAlexander Motin 	ctl_free_beio(beio);
1500ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1501ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1502ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
150311b569f7SAlexander Motin 		ctl_data_submit_done(io);
150408a7cce5SAlexander Motin 		return;
150508a7cce5SAlexander Motin 	}
150608a7cce5SAlexander Motin 
150708a7cce5SAlexander Motin 	io->io_hdr.status &= ~CTL_STATUS_MASK;
150808a7cce5SAlexander Motin 	io->io_hdr.status |= CTL_STATUS_NONE;
150908a7cce5SAlexander Motin 
151075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
151108a7cce5SAlexander Motin 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
151275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
151308a7cce5SAlexander Motin 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
151408a7cce5SAlexander Motin }
151508a7cce5SAlexander Motin 
151608a7cce5SAlexander Motin static void
1517130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1518130f4520SKenneth D. Merry 			   union ctl_io *io)
1519130f4520SKenneth D. Merry {
15200bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1521130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1522130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
152311b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1524e86a4142SAlexander Motin 	struct ctl_ptr_len_flags *bptrlen;
1525e86a4142SAlexander Motin 	uint64_t len_left, lbas;
1526130f4520SKenneth D. Merry 	int i;
1527130f4520SKenneth D. Merry 
1528130f4520SKenneth D. Merry 	softc = be_lun->softc;
1529130f4520SKenneth D. Merry 
1530130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1531130f4520SKenneth D. Merry 
153211b569f7SAlexander Motin 	lbalen = ARGS(io);
153311b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
153436160958SMark Johnston 		SDT_PROBE0(cbb, , write, start);
153511b569f7SAlexander Motin 	} else {
153636160958SMark Johnston 		SDT_PROBE0(cbb, , read, start);
1537130f4520SKenneth D. Merry 	}
1538130f4520SKenneth D. Merry 
1539130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1540130f4520SKenneth D. Merry 	beio->io = io;
1541130f4520SKenneth D. Merry 	beio->lun = be_lun;
1542e86a4142SAlexander Motin 	bptrlen = PRIV(io);
1543e86a4142SAlexander Motin 	bptrlen->ptr = (void *)beio;
1544130f4520SKenneth D. Merry 
1545130f4520SKenneth D. Merry 	switch (io->scsiio.tag_type) {
1546130f4520SKenneth D. Merry 	case CTL_TAG_ORDERED:
1547130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1548130f4520SKenneth D. Merry 		break;
1549130f4520SKenneth D. Merry 	case CTL_TAG_HEAD_OF_QUEUE:
1550130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1551130f4520SKenneth D. Merry 		break;
1552130f4520SKenneth D. Merry 	case CTL_TAG_UNTAGGED:
1553130f4520SKenneth D. Merry 	case CTL_TAG_SIMPLE:
1554130f4520SKenneth D. Merry 	case CTL_TAG_ACA:
1555130f4520SKenneth D. Merry 	default:
1556130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1557130f4520SKenneth D. Merry 		break;
1558130f4520SKenneth D. Merry 	}
1559130f4520SKenneth D. Merry 
156011b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1561130f4520SKenneth D. Merry 		beio->bio_cmd = BIO_WRITE;
1562130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_WRITE;
156311b569f7SAlexander Motin 	} else {
156411b569f7SAlexander Motin 		beio->bio_cmd = BIO_READ;
156511b569f7SAlexander Motin 		beio->ds_trans_type = DEVSTAT_READ;
1566130f4520SKenneth D. Merry 	}
1567130f4520SKenneth D. Merry 
156808a7cce5SAlexander Motin 	DPRINTF("%s at LBA %jx len %u @%ju\n",
1569130f4520SKenneth D. Merry 	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1570e86a4142SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
15710d7fed74SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE) {
15720d7fed74SAlexander Motin 		beio->two_sglists = 1;
157311b569f7SAlexander Motin 		lbas = CTLBLK_HALF_IO_SIZE;
15740d7fed74SAlexander Motin 	} else {
157511b569f7SAlexander Motin 		lbas = CTLBLK_MAX_IO_SIZE;
15760d7fed74SAlexander Motin 	}
15770bcd4ab6SAlexander Motin 	lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
15780bcd4ab6SAlexander Motin 	beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
15790bcd4ab6SAlexander Motin 	beio->io_len = lbas * cbe_lun->blocksize;
1580e86a4142SAlexander Motin 	bptrlen->len += lbas;
1581130f4520SKenneth D. Merry 
158208a7cce5SAlexander Motin 	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
158308a7cce5SAlexander Motin 		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
158408a7cce5SAlexander Motin 		    i, CTLBLK_MAX_SEGS));
1585130f4520SKenneth D. Merry 
1586130f4520SKenneth D. Merry 		/*
1587130f4520SKenneth D. Merry 		 * Setup the S/G entry for this chunk.
1588130f4520SKenneth D. Merry 		 */
158908a7cce5SAlexander Motin 		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
15900d7fed74SAlexander Motin 		beio->sg_segs[i].addr = uma_zalloc(softc->buf_zone, M_WAITOK);
1591130f4520SKenneth D. Merry 
1592130f4520SKenneth D. Merry 		DPRINTF("segment %d addr %p len %zd\n", i,
1593130f4520SKenneth D. Merry 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1594130f4520SKenneth D. Merry 
159511b569f7SAlexander Motin 		/* Set up second segment for compare operation. */
15960d7fed74SAlexander Motin 		if (beio->two_sglists) {
159711b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
159811b569f7SAlexander Motin 			    beio->sg_segs[i].len;
159911b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
16000d7fed74SAlexander Motin 			    uma_zalloc(softc->buf_zone, M_WAITOK);
160111b569f7SAlexander Motin 		}
160211b569f7SAlexander Motin 
1603130f4520SKenneth D. Merry 		beio->num_segs++;
1604130f4520SKenneth D. Merry 		len_left -= beio->sg_segs[i].len;
1605130f4520SKenneth D. Merry 	}
1606e86a4142SAlexander Motin 	if (bptrlen->len < lbalen->len)
160708a7cce5SAlexander Motin 		beio->beio_cont = ctl_be_block_next;
160808a7cce5SAlexander Motin 	io->scsiio.be_move_done = ctl_be_block_move_done;
160911b569f7SAlexander Motin 	/* For compare we have separate S/G lists for read and datamove. */
16100d7fed74SAlexander Motin 	if (beio->two_sglists)
161111b569f7SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
161211b569f7SAlexander Motin 	else
161308a7cce5SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
161408a7cce5SAlexander Motin 	io->scsiio.kern_data_len = beio->io_len;
161508a7cce5SAlexander Motin 	io->scsiio.kern_sg_entries = beio->num_segs;
1616b2221369SAlexander Motin 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1617130f4520SKenneth D. Merry 
1618130f4520SKenneth D. Merry 	/*
1619130f4520SKenneth D. Merry 	 * For the read case, we need to read the data into our buffers and
1620130f4520SKenneth D. Merry 	 * then we can send it back to the user.  For the write case, we
1621130f4520SKenneth D. Merry 	 * need to get the data from the user first.
1622130f4520SKenneth D. Merry 	 */
1623130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
162436160958SMark Johnston 		SDT_PROBE0(cbb, , read, alloc_done);
1625130f4520SKenneth D. Merry 		be_lun->dispatch(be_lun, beio);
1626130f4520SKenneth D. Merry 	} else {
162736160958SMark Johnston 		SDT_PROBE0(cbb, , write, alloc_done);
1628130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
1629e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
1630130f4520SKenneth D. Merry #endif
1631130f4520SKenneth D. Merry 		ctl_datamove(io);
1632130f4520SKenneth D. Merry 	}
1633130f4520SKenneth D. Merry }
1634130f4520SKenneth D. Merry 
1635130f4520SKenneth D. Merry static void
1636130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending)
1637130f4520SKenneth D. Merry {
1638ee4ad294SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1639ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1640130f4520SKenneth D. Merry 	union ctl_io *io;
1641130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1642130f4520SKenneth D. Merry 
1643ee4ad294SAlexander Motin 	DPRINTF("entered\n");
1644ee4ad294SAlexander Motin 	/*
1645ee4ad294SAlexander Motin 	 * Fetch and process I/Os from all queues.  If we detect LUN
1646648dfc1aSAlexander Motin 	 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1647ee4ad294SAlexander Motin 	 * so make response maximally opaque to not confuse initiator.
1648ee4ad294SAlexander Motin 	 */
1649ee4ad294SAlexander Motin 	for (;;) {
1650ee4ad294SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
1651ee4ad294SAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1652ee4ad294SAlexander Motin 		if (io != NULL) {
1653130f4520SKenneth D. Merry 			DPRINTF("datamove queue\n");
1654130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1655130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
165675c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1657e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1658648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1659ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1660ee4ad294SAlexander Motin 				ctl_complete_beio(beio);
1661ee4ad294SAlexander Motin 				return;
1662ee4ad294SAlexander Motin 			}
1663130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1664130f4520SKenneth D. Merry 			continue;
1665130f4520SKenneth D. Merry 		}
1666130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1667130f4520SKenneth D. Merry 		if (io != NULL) {
1668130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
1669130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1670130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
167175c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1672648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1673ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1674ee4ad294SAlexander Motin 				ctl_config_write_done(io);
1675ee4ad294SAlexander Motin 				return;
1676ee4ad294SAlexander Motin 			}
1677130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1678ef8daf3fSAlexander Motin 			continue;
1679ef8daf3fSAlexander Motin 		}
1680ef8daf3fSAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1681ef8daf3fSAlexander Motin 		if (io != NULL) {
1682ef8daf3fSAlexander Motin 			DPRINTF("config read queue\n");
1683ef8daf3fSAlexander Motin 			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1684ef8daf3fSAlexander Motin 				      ctl_io_hdr, links);
1685ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1686648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1687ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1688ee4ad294SAlexander Motin 				ctl_config_read_done(io);
1689ee4ad294SAlexander Motin 				return;
1690ee4ad294SAlexander Motin 			}
1691ef8daf3fSAlexander Motin 			ctl_be_block_cr_dispatch(be_lun, io);
1692130f4520SKenneth D. Merry 			continue;
1693130f4520SKenneth D. Merry 		}
1694130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1695130f4520SKenneth D. Merry 		if (io != NULL) {
1696130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
1697130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1698130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
169975c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1700648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1701ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1702ee4ad294SAlexander Motin 				ctl_data_submit_done(io);
1703ee4ad294SAlexander Motin 				return;
1704ee4ad294SAlexander Motin 			}
1705130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1706130f4520SKenneth D. Merry 			continue;
1707130f4520SKenneth D. Merry 		}
1708130f4520SKenneth D. Merry 
1709130f4520SKenneth D. Merry 		/*
1710130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1711130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1712130f4520SKenneth D. Merry 		 */
1713ee4ad294SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
1714130f4520SKenneth D. Merry 		break;
1715130f4520SKenneth D. Merry 	}
1716130f4520SKenneth D. Merry }
1717130f4520SKenneth D. Merry 
1718130f4520SKenneth D. Merry /*
1719130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1720130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1721130f4520SKenneth D. Merry  * thread.
1722130f4520SKenneth D. Merry  */
1723130f4520SKenneth D. Merry static int
1724130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1725130f4520SKenneth D. Merry {
1726130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1727130f4520SKenneth D. Merry 
1728130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1729130f4520SKenneth D. Merry 
1730*767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
1731130f4520SKenneth D. Merry 
1732130f4520SKenneth D. Merry 	/*
1733130f4520SKenneth D. Merry 	 * Make sure we only get SCSI I/O.
1734130f4520SKenneth D. Merry 	 */
1735130f4520SKenneth D. Merry 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1736130f4520SKenneth D. Merry 		"%#x) encountered", io->io_hdr.io_type));
1737130f4520SKenneth D. Merry 
1738e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1739e86a4142SAlexander Motin 
174075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
1741130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
174275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
1743130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1744130f4520SKenneth D. Merry 
17459c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1746130f4520SKenneth D. Merry }
1747130f4520SKenneth D. Merry 
1748130f4520SKenneth D. Merry static int
1749130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1750130f4520SKenneth D. Merry 			int flag, struct thread *td)
1751130f4520SKenneth D. Merry {
175234144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
1753130f4520SKenneth D. Merry 	int error;
1754130f4520SKenneth D. Merry 
1755130f4520SKenneth D. Merry 	error = 0;
1756130f4520SKenneth D. Merry 	switch (cmd) {
1757130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1758130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1759130f4520SKenneth D. Merry 
1760130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1761130f4520SKenneth D. Merry 
1762130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1763130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1764130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1765130f4520SKenneth D. Merry 			break;
1766130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1767130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1768130f4520SKenneth D. Merry 			break;
176981177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
177081177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
177181177295SEdward Tomasz Napierala 			break;
1772130f4520SKenneth D. Merry 		default:
1773130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1774130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
177519720f41SAlexander Motin 				 "invalid LUN request type %d",
1776130f4520SKenneth D. Merry 				 lun_req->reqtype);
1777130f4520SKenneth D. Merry 			break;
1778130f4520SKenneth D. Merry 		}
1779130f4520SKenneth D. Merry 		break;
1780130f4520SKenneth D. Merry 	}
1781130f4520SKenneth D. Merry 	default:
1782130f4520SKenneth D. Merry 		error = ENOTTY;
1783130f4520SKenneth D. Merry 		break;
1784130f4520SKenneth D. Merry 	}
1785130f4520SKenneth D. Merry 
1786130f4520SKenneth D. Merry 	return (error);
1787130f4520SKenneth D. Merry }
1788130f4520SKenneth D. Merry 
1789130f4520SKenneth D. Merry static int
1790130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1791130f4520SKenneth D. Merry {
17920bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1793130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1794130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
17958951f055SMarcelo Araujo 	const char		     *value;
1796130f4520SKenneth D. Merry 	struct vattr		      vattr;
179734961f40SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1798130f4520SKenneth D. Merry 	int			      error;
1799130f4520SKenneth D. Merry 
18000bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
1801130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
180219720f41SAlexander Motin 	params = &be_lun->params;
1803130f4520SKenneth D. Merry 
1804130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1805130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1806130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1807ef8daf3fSAlexander Motin 	be_lun->get_lba_status = ctl_be_block_gls_file;
180853c146deSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_file;
18090bcd4ab6SAlexander Motin 	be_lun->unmap = NULL;
18100bcd4ab6SAlexander Motin 	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1811130f4520SKenneth D. Merry 
1812130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1813130f4520SKenneth D. Merry 	if (error != 0) {
1814130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1815130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1816130f4520SKenneth D. Merry 			 be_lun->dev_path);
1817130f4520SKenneth D. Merry 		return (error);
1818130f4520SKenneth D. Merry 	}
1819130f4520SKenneth D. Merry 
1820130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
182181177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
182281177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
182381177295SEdward Tomasz Napierala 	else
1824130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1825130f4520SKenneth D. Merry 
1826130f4520SKenneth D. Merry 	/*
182720a28d6cSAlexander Motin 	 * For files we can use any logical block size.  Prefer 512 bytes
182820a28d6cSAlexander Motin 	 * for compatibility reasons.  If file's vattr.va_blocksize
182920a28d6cSAlexander Motin 	 * (preferred I/O block size) is bigger and multiple to chosen
183020a28d6cSAlexander Motin 	 * logical block size -- report it as physical block size.
1831130f4520SKenneth D. Merry 	 */
1832130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
18330bcd4ab6SAlexander Motin 		cbe_lun->blocksize = params->blocksize_bytes;
183491be33dcSAlexander Motin 	else if (cbe_lun->lun_type == T_CDROM)
183591be33dcSAlexander Motin 		cbe_lun->blocksize = 2048;
1836130f4520SKenneth D. Merry 	else
18370bcd4ab6SAlexander Motin 		cbe_lun->blocksize = 512;
18380bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
18390bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
18400bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
184134961f40SAlexander Motin 
184234961f40SAlexander Motin 	us = ps = vattr.va_blocksize;
184334961f40SAlexander Motin 	uo = po = 0;
184434961f40SAlexander Motin 
18458951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
184634961f40SAlexander Motin 	if (value != NULL)
184734961f40SAlexander Motin 		ctl_expand_number(value, &ps);
18488951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
184934961f40SAlexander Motin 	if (value != NULL)
185034961f40SAlexander Motin 		ctl_expand_number(value, &po);
18510bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
18520bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
18530bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
18540bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
18550bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
18560bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
185734961f40SAlexander Motin 	}
185834961f40SAlexander Motin 
18598951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
186034961f40SAlexander Motin 	if (value != NULL)
186134961f40SAlexander Motin 		ctl_expand_number(value, &us);
18628951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
186334961f40SAlexander Motin 	if (value != NULL)
186434961f40SAlexander Motin 		ctl_expand_number(value, &uo);
18650bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
18660bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
18670bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
18680bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
18690bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
18700bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
187120a28d6cSAlexander Motin 	}
1872130f4520SKenneth D. Merry 
1873130f4520SKenneth D. Merry 	/*
1874130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1875130f4520SKenneth D. Merry 	 * sector long.
1876130f4520SKenneth D. Merry 	 */
18770bcd4ab6SAlexander Motin 	if (be_lun->size_bytes < cbe_lun->blocksize) {
1878130f4520SKenneth D. Merry 		error = EINVAL;
1879130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1880130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
18810bcd4ab6SAlexander Motin 			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1882130f4520SKenneth D. Merry 	}
1883cb8727e2SAlexander Motin 
18840bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1885130f4520SKenneth D. Merry 	return (error);
1886130f4520SKenneth D. Merry }
1887130f4520SKenneth D. Merry 
1888130f4520SKenneth D. Merry static int
1889130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1890130f4520SKenneth D. Merry {
18910bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1892130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
18933236151eSAlexander Motin 	struct cdevsw		     *csw;
1894130f4520SKenneth D. Merry 	struct cdev		     *dev;
18958951f055SMarcelo Araujo 	const char		     *value;
18963236151eSAlexander Motin 	int			      error, atomic, maxio, ref, unmap, tmp;
1897f6295033SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1898130f4520SKenneth D. Merry 
189919720f41SAlexander Motin 	params = &be_lun->params;
1900130f4520SKenneth D. Merry 
1901130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
19023236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
19033236151eSAlexander Motin 	if (csw == NULL)
19043236151eSAlexander Motin 		return (ENXIO);
19053236151eSAlexander Motin 	if (strcmp(csw->d_name, "zvol") == 0) {
190667f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1907ef8daf3fSAlexander Motin 		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1908cb8727e2SAlexander Motin 		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1909cb8727e2SAlexander Motin 	} else {
191067f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_dev;
19110bcd4ab6SAlexander Motin 		be_lun->get_lba_status = NULL;
1912cb8727e2SAlexander Motin 		atomic = 0;
19133236151eSAlexander Motin 		maxio = dev->si_iosize_max;
1914cb8727e2SAlexander Motin 		if (maxio <= 0)
1915cb8727e2SAlexander Motin 			maxio = DFLTPHYS;
1916cb8727e2SAlexander Motin 		if (maxio > CTLBLK_MAX_IO_SIZE)
1917cb8727e2SAlexander Motin 			maxio = CTLBLK_MAX_IO_SIZE;
1918cb8727e2SAlexander Motin 	}
191967f586a8SAlexander Motin 	be_lun->lun_flush = ctl_be_block_flush_dev;
1920c3e7ba3eSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_dev;
19210bcd4ab6SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
1922130f4520SKenneth D. Merry 
19233236151eSAlexander Motin 	if (!csw->d_ioctl) {
19243236151eSAlexander Motin 		dev_relthread(dev, ref);
1925130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
19263236151eSAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
1927130f4520SKenneth D. Merry 		return (ENODEV);
1928130f4520SKenneth D. Merry 	}
1929130f4520SKenneth D. Merry 
19303236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1931130f4520SKenneth D. Merry 			       curthread);
1932130f4520SKenneth D. Merry 	if (error) {
19333236151eSAlexander Motin 		dev_relthread(dev, ref);
1934130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
193519720f41SAlexander Motin 			 "error %d returned for DIOCGSECTORSIZE ioctl "
193619720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
1937130f4520SKenneth D. Merry 		return (error);
1938130f4520SKenneth D. Merry 	}
1939130f4520SKenneth D. Merry 
1940130f4520SKenneth D. Merry 	/*
1941130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
1942130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
1943130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
1944130f4520SKenneth D. Merry 	 * device's blocksize.
1945130f4520SKenneth D. Merry 	 */
1946a15bbf15SAlexander Motin 	if ((params->blocksize_bytes != 0) &&
1947a15bbf15SAlexander Motin 	    (params->blocksize_bytes >= tmp)) {
1948a15bbf15SAlexander Motin 		if (params->blocksize_bytes % tmp == 0) {
19490bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
1950130f4520SKenneth D. Merry 		} else {
19513236151eSAlexander Motin 			dev_relthread(dev, ref);
1952130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
195319720f41SAlexander Motin 				 "requested blocksize %u is not an even "
1954130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
1955f6295033SAlexander Motin 				 params->blocksize_bytes, tmp);
1956130f4520SKenneth D. Merry 			return (EINVAL);
1957130f4520SKenneth D. Merry 		}
1958a15bbf15SAlexander Motin 	} else if (params->blocksize_bytes != 0) {
19593236151eSAlexander Motin 		dev_relthread(dev, ref);
1960130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
196119720f41SAlexander Motin 			 "requested blocksize %u < backing device "
1962f6295033SAlexander Motin 			 "blocksize %u", params->blocksize_bytes, tmp);
1963130f4520SKenneth D. Merry 		return (EINVAL);
196491be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_CDROM)
196591be33dcSAlexander Motin 		cbe_lun->blocksize = MAX(tmp, 2048);
196691be33dcSAlexander Motin 	else
19670bcd4ab6SAlexander Motin 		cbe_lun->blocksize = tmp;
1968130f4520SKenneth D. Merry 
19693236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
1970130f4520SKenneth D. Merry 			     curthread);
1971130f4520SKenneth D. Merry 	if (error) {
19723236151eSAlexander Motin 		dev_relthread(dev, ref);
1973130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
197419720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE "
197519720f41SAlexander Motin 			 " ioctl on %s!", error,
197681177295SEdward Tomasz Napierala 			 be_lun->dev_path);
1977130f4520SKenneth D. Merry 		return (error);
1978130f4520SKenneth D. Merry 	}
1979130f4520SKenneth D. Merry 
198081177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
1981f6295033SAlexander Motin 		if (params->lun_size_bytes > otmp) {
19823236151eSAlexander Motin 			dev_relthread(dev, ref);
198381177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
198419720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
198519720f41SAlexander Motin 				 "size %ju",
198681177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
1987f6295033SAlexander Motin 				 (uintmax_t)otmp);
198881177295SEdward Tomasz Napierala 			return (EINVAL);
1989130f4520SKenneth D. Merry 		}
1990130f4520SKenneth D. Merry 
199181177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
1992a15bbf15SAlexander Motin 	} else
1993f6295033SAlexander Motin 		be_lun->size_bytes = otmp;
19940bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
19950bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
19960bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
199781177295SEdward Tomasz Napierala 
19983236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
19993236151eSAlexander Motin 	    curthread);
2000f6012722SAlexander Motin 	if (error)
2001f6012722SAlexander Motin 		ps = po = 0;
2002f6012722SAlexander Motin 	else {
20033236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
20043236151eSAlexander Motin 		    FREAD, curthread);
2005f6012722SAlexander Motin 		if (error)
2006f6012722SAlexander Motin 			po = 0;
2007f6012722SAlexander Motin 	}
200834961f40SAlexander Motin 	us = ps;
200934961f40SAlexander Motin 	uo = po;
201034961f40SAlexander Motin 
20118951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
201234961f40SAlexander Motin 	if (value != NULL)
201334961f40SAlexander Motin 		ctl_expand_number(value, &ps);
20148951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
201534961f40SAlexander Motin 	if (value != NULL)
201634961f40SAlexander Motin 		ctl_expand_number(value, &po);
20170bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
20180bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
20190bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
20200bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
20210bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
20220bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
2023f6012722SAlexander Motin 	}
2024f6012722SAlexander Motin 
20258951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
202634961f40SAlexander Motin 	if (value != NULL)
202734961f40SAlexander Motin 		ctl_expand_number(value, &us);
20288951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
202934961f40SAlexander Motin 	if (value != NULL)
203034961f40SAlexander Motin 		ctl_expand_number(value, &uo);
20310bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
20320bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
20330bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
20340bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
20350bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
20360bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
203734961f40SAlexander Motin 	}
203834961f40SAlexander Motin 
20390bcd4ab6SAlexander Motin 	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
20400bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2041fbc8d4ffSAlexander Motin 
2042fbc8d4ffSAlexander Motin 	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2043fbc8d4ffSAlexander Motin 		unmap = 1;
2044fbc8d4ffSAlexander Motin 	} else {
2045fbc8d4ffSAlexander Motin 		struct diocgattr_arg	arg;
2046fbc8d4ffSAlexander Motin 
2047fbc8d4ffSAlexander Motin 		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2048fbc8d4ffSAlexander Motin 		arg.len = sizeof(arg.value.i);
20493236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
20503236151eSAlexander Motin 		    curthread);
2051fbc8d4ffSAlexander Motin 		unmap = (error == 0) ? arg.value.i : 0;
2052fbc8d4ffSAlexander Motin 	}
20538951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2054fbc8d4ffSAlexander Motin 	if (value != NULL)
2055fbc8d4ffSAlexander Motin 		unmap = (strcmp(value, "on") == 0);
2056fbc8d4ffSAlexander Motin 	if (unmap)
20570bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
20580bcd4ab6SAlexander Motin 	else
20590bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2060fbc8d4ffSAlexander Motin 
20613236151eSAlexander Motin 	dev_relthread(dev, ref);
206281177295SEdward Tomasz Napierala 	return (0);
206381177295SEdward Tomasz Napierala }
2064130f4520SKenneth D. Merry 
2065130f4520SKenneth D. Merry static int
2066130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2067130f4520SKenneth D. Merry {
20680bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
20690bcd4ab6SAlexander Motin 	int flags;
2070130f4520SKenneth D. Merry 
20710bcd4ab6SAlexander Motin 	if (be_lun->vn) {
20720bcd4ab6SAlexander Motin 		flags = FREAD;
20730bcd4ab6SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
20740bcd4ab6SAlexander Motin 			flags |= FWRITE;
2075130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2076130f4520SKenneth D. Merry 		be_lun->vn = NULL;
2077130f4520SKenneth D. Merry 
2078130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
2079130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
2080130f4520SKenneth D. Merry 			break;
2081130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
2082130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
2083130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
2084130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
2085130f4520SKenneth D. Merry 			}
2086130f4520SKenneth D. Merry 			break;
2087130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
2088025a2301SEdward Tomasz Napierala 			break;
2089130f4520SKenneth D. Merry 		default:
20905124012aSAlexander Motin 			panic("Unexpected backend type %d", be_lun->dev_type);
2091130f4520SKenneth D. Merry 			break;
2092130f4520SKenneth D. Merry 		}
209319720f41SAlexander Motin 		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2094130f4520SKenneth D. Merry 	}
2095130f4520SKenneth D. Merry 	return (0);
2096130f4520SKenneth D. Merry }
2097130f4520SKenneth D. Merry 
2098130f4520SKenneth D. Merry static int
2099648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2100130f4520SKenneth D. Merry {
21010bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2102130f4520SKenneth D. Merry 	struct nameidata nd;
21038951f055SMarcelo Araujo 	const char	*value;
21040bcd4ab6SAlexander Motin 	int		 error, flags;
2105130f4520SKenneth D. Merry 
2106130f4520SKenneth D. Merry 	error = 0;
2107130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
2108130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
210919720f41SAlexander Motin 			 "Root filesystem is not mounted");
2110130f4520SKenneth D. Merry 		return (1);
2111130f4520SKenneth D. Merry 	}
21128a08cec1SMateusz Guzik 	pwd_ensure_dirs();
2113130f4520SKenneth D. Merry 
21148951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "file", NULL);
21150bcd4ab6SAlexander Motin 	if (value == NULL) {
21160bcd4ab6SAlexander Motin 		snprintf(req->error_str, sizeof(req->error_str),
21170bcd4ab6SAlexander Motin 			 "no file argument specified");
21180bcd4ab6SAlexander Motin 		return (1);
21190bcd4ab6SAlexander Motin 	}
21200bcd4ab6SAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
21210bcd4ab6SAlexander Motin 	be_lun->dev_path = strdup(value, M_CTLBLK);
21220bcd4ab6SAlexander Motin 
21230bcd4ab6SAlexander Motin 	flags = FREAD;
21248951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
212591be33dcSAlexander Motin 	if (value != NULL) {
212691be33dcSAlexander Motin 		if (strcmp(value, "on") != 0)
212791be33dcSAlexander Motin 			flags |= FWRITE;
212891be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_DIRECT)
21290bcd4ab6SAlexander Motin 		flags |= FWRITE;
21300bcd4ab6SAlexander Motin 
2131130f4520SKenneth D. Merry again:
2132130f4520SKenneth D. Merry 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2133130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
21340bcd4ab6SAlexander Motin 	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
21350bcd4ab6SAlexander Motin 		flags &= ~FWRITE;
21360bcd4ab6SAlexander Motin 		goto again;
21370bcd4ab6SAlexander Motin 	}
2138130f4520SKenneth D. Merry 	if (error) {
2139130f4520SKenneth D. Merry 		/*
2140130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
2141130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
2142130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
2143130f4520SKenneth D. Merry 		 * full path.
2144130f4520SKenneth D. Merry 		 */
2145130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
2146130f4520SKenneth D. Merry 			char *dev_name;
2147130f4520SKenneth D. Merry 
21480bcd4ab6SAlexander Motin 			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2149130f4520SKenneth D. Merry 				be_lun->dev_path);
2150130f4520SKenneth D. Merry 			free(be_lun->dev_path, M_CTLBLK);
2151130f4520SKenneth D. Merry 			be_lun->dev_path = dev_name;
2152130f4520SKenneth D. Merry 			goto again;
2153130f4520SKenneth D. Merry 		}
2154130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
215519720f41SAlexander Motin 		    "error opening %s: %d", be_lun->dev_path, error);
2156130f4520SKenneth D. Merry 		return (error);
2157130f4520SKenneth D. Merry 	}
21580bcd4ab6SAlexander Motin 	if (flags & FWRITE)
21590bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
21600bcd4ab6SAlexander Motin 	else
21610bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2162130f4520SKenneth D. Merry 
2163130f4520SKenneth D. Merry 	NDFREE(&nd, NDF_ONLY_PNBUF);
2164130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
2165130f4520SKenneth D. Merry 
2166130f4520SKenneth D. Merry 	/* We only support disks and files. */
2167130f4520SKenneth D. Merry 	if (vn_isdisk(be_lun->vn, &error)) {
2168130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
2169130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
2170130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
2171130f4520SKenneth D. Merry 	} else {
2172130f4520SKenneth D. Merry 		error = EINVAL;
2173130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2174025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
2175130f4520SKenneth D. Merry 	}
2176b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
2177130f4520SKenneth D. Merry 
2178a15bbf15SAlexander Motin 	if (error != 0)
2179130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
21800bcd4ab6SAlexander Motin 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
21810bcd4ab6SAlexander Motin 	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
21820bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
21838951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
21840bcd4ab6SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
21850bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
21860bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "read") == 0)
21870bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
21880bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "off") == 0)
21890bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2190130f4520SKenneth D. Merry 	return (0);
2191130f4520SKenneth D. Merry }
2192130f4520SKenneth D. Merry 
2193130f4520SKenneth D. Merry static int
2194130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2195130f4520SKenneth D. Merry {
21960bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2197130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2198130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
219957a5db13SAlexander Motin 	char num_thread_str[16];
2200130f4520SKenneth D. Merry 	char tmpstr[32];
22018951f055SMarcelo Araujo 	const char *value;
2202fbc8d4ffSAlexander Motin 	int retval, num_threads;
220357a5db13SAlexander Motin 	int tmp_num_threads;
2204130f4520SKenneth D. Merry 
2205130f4520SKenneth D. Merry 	params = &req->reqdata.create;
2206130f4520SKenneth D. Merry 	retval = 0;
220719720f41SAlexander Motin 	req->status = CTL_LUN_OK;
2208130f4520SKenneth D. Merry 
2209130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
22100bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
221119720f41SAlexander Motin 	be_lun->params = req->reqdata.create;
2212130f4520SKenneth D. Merry 	be_lun->softc = softc;
2213130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
2214ef8daf3fSAlexander Motin 	STAILQ_INIT(&be_lun->config_read_queue);
2215130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
2216130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
221734144c2cSAlexander Motin 	mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
221834144c2cSAlexander Motin 	mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
22198951f055SMarcelo Araujo 	cbe_lun->options = nvlist_clone(req->args_nvl);
2220130f4520SKenneth D. Merry 
2221130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
22220bcd4ab6SAlexander Motin 		cbe_lun->lun_type = params->device_type;
2223130f4520SKenneth D. Merry 	else
22240bcd4ab6SAlexander Motin 		cbe_lun->lun_type = T_DIRECT;
222534144c2cSAlexander Motin 	be_lun->flags = 0;
22267ac58230SAlexander Motin 	cbe_lun->flags = 0;
22278951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
22287ac58230SAlexander Motin 	if (value != NULL) {
22297ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
22307ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
22317ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
22327ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2233130f4520SKenneth D. Merry 
223491be33dcSAlexander Motin 	if (cbe_lun->lun_type == T_DIRECT ||
223591be33dcSAlexander Motin 	    cbe_lun->lun_type == T_CDROM) {
2236a15bbf15SAlexander Motin 		be_lun->size_bytes = params->lun_size_bytes;
2237a15bbf15SAlexander Motin 		if (params->blocksize_bytes != 0)
22380bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
223991be33dcSAlexander Motin 		else if (cbe_lun->lun_type == T_CDROM)
224091be33dcSAlexander Motin 			cbe_lun->blocksize = 2048;
2241a15bbf15SAlexander Motin 		else
22420bcd4ab6SAlexander Motin 			cbe_lun->blocksize = 512;
22430bcd4ab6SAlexander Motin 		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
22440bcd4ab6SAlexander Motin 		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
22450bcd4ab6SAlexander Motin 		    0 : (be_lun->size_blocks - 1);
2246130f4520SKenneth D. Merry 
22477ac58230SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
22487ac58230SAlexander Motin 		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2249648dfc1aSAlexander Motin 			retval = ctl_be_block_open(be_lun, req);
2250130f4520SKenneth D. Merry 			if (retval != 0) {
2251130f4520SKenneth D. Merry 				retval = 0;
225219720f41SAlexander Motin 				req->status = CTL_LUN_WARNING;
2253130f4520SKenneth D. Merry 			}
22547ac58230SAlexander Motin 		}
22550bcd4ab6SAlexander Motin 		num_threads = cbb_num_threads;
2256130f4520SKenneth D. Merry 	} else {
2257130f4520SKenneth D. Merry 		num_threads = 1;
2258130f4520SKenneth D. Merry 	}
2259130f4520SKenneth D. Merry 
22608951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
226157a5db13SAlexander Motin 	if (value != NULL) {
226257a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
2263130f4520SKenneth D. Merry 
2264130f4520SKenneth D. Merry 		/*
2265130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
2266130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
2267130f4520SKenneth D. Merry 		 * specify 1000 threads.
2268130f4520SKenneth D. Merry 		 */
2269130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
2270130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
227119720f41SAlexander Motin 				 "invalid number of threads %s",
227219720f41SAlexander Motin 				 num_thread_str);
2273130f4520SKenneth D. Merry 			goto bailout_error;
2274130f4520SKenneth D. Merry 		}
2275130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
227657a5db13SAlexander Motin 	}
2277130f4520SKenneth D. Merry 
227819720f41SAlexander Motin 	if (be_lun->vn == NULL)
2279648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2280130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
228119720f41SAlexander Motin 	params->lun_size_bytes = be_lun->size_bytes;
22820bcd4ab6SAlexander Motin 	params->blocksize_bytes = cbe_lun->blocksize;
2283130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
22840bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = params->req_lun_id;
22850bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2286130f4520SKenneth D. Merry 	} else
22870bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = 0;
2288130f4520SKenneth D. Merry 
22890bcd4ab6SAlexander Motin 	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
22900bcd4ab6SAlexander Motin 	cbe_lun->be = &ctl_be_block_driver;
2291130f4520SKenneth D. Merry 
2292130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
229371cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2294130f4520SKenneth D. Merry 			 softc->num_luns);
22950bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, tmpstr,
22960bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2297130f4520SKenneth D. Merry 
2298130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
2299130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
2300e7038eb7SAlexander Motin 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2301130f4520SKenneth D. Merry 	} else {
23020bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
23030bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num),
2304130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
2305130f4520SKenneth D. Merry 	}
2306130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
230771cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
23080bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, tmpstr,
23090bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2310130f4520SKenneth D. Merry 
2311130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2312130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2313e7038eb7SAlexander Motin 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2314130f4520SKenneth D. Merry 	} else {
23150bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, params->device_id,
23160bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id),
2317130f4520SKenneth D. Merry 			    sizeof(params->device_id)));
2318130f4520SKenneth D. Merry 	}
2319130f4520SKenneth D. Merry 
2320130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2321130f4520SKenneth D. Merry 
232234144c2cSAlexander Motin 	be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2323130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2324130f4520SKenneth D. Merry 
2325130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2326130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
232719720f41SAlexander Motin 			 "unable to create taskqueue");
2328130f4520SKenneth D. Merry 		goto bailout_error;
2329130f4520SKenneth D. Merry 	}
2330130f4520SKenneth D. Merry 
2331130f4520SKenneth D. Merry 	/*
2332130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2333130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2334130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2335130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2336130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2337130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2338130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2339130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2340130f4520SKenneth D. Merry 	 *
2341130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2342130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2343130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2344130f4520SKenneth D. Merry 	 */
234512373e95SAlexander Motin 	retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2346130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2347053db1feSAlexander Motin 					 /*priority*/PUSER,
234812373e95SAlexander Motin 					 /*proc*/control_softc->ctl_proc,
234934144c2cSAlexander Motin 					 /*thread name*/"block");
2350130f4520SKenneth D. Merry 
2351130f4520SKenneth D. Merry 	if (retval != 0)
2352130f4520SKenneth D. Merry 		goto bailout_error;
2353130f4520SKenneth D. Merry 
2354130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2355130f4520SKenneth D. Merry 
23560bcd4ab6SAlexander Motin 	retval = ctl_add_lun(&be_lun->cbe_lun);
2357130f4520SKenneth D. Merry 	if (retval != 0) {
2358130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
235919720f41SAlexander Motin 			 "ctl_add_lun() returned error %d, see dmesg for "
236019720f41SAlexander Motin 			 "details", retval);
2361130f4520SKenneth D. Merry 		retval = 0;
2362130f4520SKenneth D. Merry 		goto bailout_error;
2363130f4520SKenneth D. Merry 	}
2364130f4520SKenneth D. Merry 
236534144c2cSAlexander Motin 	be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
23660bcd4ab6SAlexander Motin 					       cbe_lun->blocksize,
2367130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
23680bcd4ab6SAlexander Motin 					       cbe_lun->lun_type
2369130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2370130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2371130f4520SKenneth D. Merry 
237234144c2cSAlexander Motin 	mtx_lock(&softc->lock);
237334144c2cSAlexander Motin 	softc->num_luns++;
237434144c2cSAlexander Motin 	SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
237534144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
237634144c2cSAlexander Motin 
237734144c2cSAlexander Motin 	params->req_lun_id = cbe_lun->lun_id;
237834144c2cSAlexander Motin 
2379130f4520SKenneth D. Merry 	return (retval);
2380130f4520SKenneth D. Merry 
2381130f4520SKenneth D. Merry bailout_error:
2382130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2383130f4520SKenneth D. Merry 
23849e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
23859e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2386130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
23879e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2388130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
23898951f055SMarcelo Araujo 	nvlist_destroy(cbe_lun->options);
239075c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
239175c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2392130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2393130f4520SKenneth D. Merry 
2394130f4520SKenneth D. Merry 	return (retval);
2395130f4520SKenneth D. Merry }
2396130f4520SKenneth D. Merry 
2397130f4520SKenneth D. Merry static int
2398130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2399130f4520SKenneth D. Merry {
2400130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2401130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2402ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2403130f4520SKenneth D. Merry 	int retval;
2404130f4520SKenneth D. Merry 
2405130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2406130f4520SKenneth D. Merry 
240734144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
2408130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
240934144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
241034144c2cSAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id) {
241134144c2cSAlexander Motin 			SLIST_REMOVE(&softc->lun_list, be_lun,
241234144c2cSAlexander Motin 			    ctl_be_block_lun, links);
241334144c2cSAlexander Motin 			softc->num_luns--;
2414130f4520SKenneth D. Merry 			break;
2415130f4520SKenneth D. Merry 		}
241634144c2cSAlexander Motin 	}
2417130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
241834144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
2419130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2420130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
242119720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
242219720f41SAlexander Motin 			 params->lun_id);
2423130f4520SKenneth D. Merry 		goto bailout_error;
2424130f4520SKenneth D. Merry 	}
2425ee4ad294SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
2426130f4520SKenneth D. Merry 
2427ee4ad294SAlexander Motin 	if (be_lun->vn != NULL) {
2428648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2429648dfc1aSAlexander Motin 		ctl_lun_no_media(cbe_lun);
2430ee4ad294SAlexander Motin 		taskqueue_drain_all(be_lun->io_taskqueue);
2431ee4ad294SAlexander Motin 		ctl_be_block_close(be_lun);
2432ee4ad294SAlexander Motin 	}
2433ee4ad294SAlexander Motin 
243434144c2cSAlexander Motin 	mtx_lock(&softc->lock);
243534144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
243634144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
243734144c2cSAlexander Motin 
243834144c2cSAlexander Motin 	retval = ctl_remove_lun(cbe_lun);
2439130f4520SKenneth D. Merry 	if (retval != 0) {
2440130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
244134144c2cSAlexander Motin 			 "error %d returned from ctl_remove_lun() for "
244219720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
244334144c2cSAlexander Motin 		mtx_lock(&softc->lock);
244434144c2cSAlexander Motin 		be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
244534144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
2446130f4520SKenneth D. Merry 		goto bailout_error;
2447130f4520SKenneth D. Merry 	}
2448130f4520SKenneth D. Merry 
2449130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2450130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
245134144c2cSAlexander Motin 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2452130f4520SKenneth D. Merry 		if (retval == EINTR)
2453130f4520SKenneth D. Merry 			break;
2454130f4520SKenneth D. Merry 	}
2455130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
245634144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2457130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2458130f4520SKenneth D. Merry 		free(be_lun, M_CTLBLK);
245934144c2cSAlexander Motin 	} else {
246034144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
246134144c2cSAlexander Motin 		return (EINTR);
246234144c2cSAlexander Motin 	}
2463130f4520SKenneth D. Merry 
2464130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2465130f4520SKenneth D. Merry 	return (0);
2466130f4520SKenneth D. Merry 
2467130f4520SKenneth D. Merry bailout_error:
2468130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2469130f4520SKenneth D. Merry 	return (0);
2470130f4520SKenneth D. Merry }
2471130f4520SKenneth D. Merry 
247281177295SEdward Tomasz Napierala static int
247381177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
247481177295SEdward Tomasz Napierala {
247581177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
247681177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
2477a3977beaSAlexander Motin 	struct ctl_be_lun *cbe_lun;
24788951f055SMarcelo Araujo 	const char *value;
247971d8e97eSAlexander Motin 	uint64_t oldsize;
24807ac58230SAlexander Motin 	int error, wasprim;
248181177295SEdward Tomasz Napierala 
248281177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
248381177295SEdward Tomasz Napierala 
248434144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
248581177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
248634144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
24870bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
248881177295SEdward Tomasz Napierala 			break;
248981177295SEdward Tomasz Napierala 	}
249081177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
249181177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
249281177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
249319720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
249419720f41SAlexander Motin 			 params->lun_id);
249581177295SEdward Tomasz Napierala 		goto bailout_error;
249681177295SEdward Tomasz Napierala 	}
2497a3977beaSAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
249881177295SEdward Tomasz Napierala 
2499a3977beaSAlexander Motin 	if (params->lun_size_bytes != 0)
250019720f41SAlexander Motin 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
25018951f055SMarcelo Araujo 
2502efeedddcSAlexander Motin 	if (req->args_nvl != NULL) {
25038951f055SMarcelo Araujo 		nvlist_destroy(cbe_lun->options);
25048951f055SMarcelo Araujo 		cbe_lun->options = nvlist_clone(req->args_nvl);
2505efeedddcSAlexander Motin 	}
250681177295SEdward Tomasz Napierala 
25077ac58230SAlexander Motin 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
25088951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
25097ac58230SAlexander Motin 	if (value != NULL) {
25107ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
25117ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25127ac58230SAlexander Motin 		else
25137ac58230SAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25147ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
25157ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25167ac58230SAlexander Motin 	else
25177ac58230SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25187ac58230SAlexander Motin 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
25197ac58230SAlexander Motin 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
25207ac58230SAlexander Motin 			ctl_lun_primary(cbe_lun);
25217ac58230SAlexander Motin 		else
25227ac58230SAlexander Motin 			ctl_lun_secondary(cbe_lun);
25237ac58230SAlexander Motin 	}
25247ac58230SAlexander Motin 
25250bcd4ab6SAlexander Motin 	oldsize = be_lun->size_blocks;
25267ac58230SAlexander Motin 	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
25277ac58230SAlexander Motin 	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
252819720f41SAlexander Motin 		if (be_lun->vn == NULL)
2529648dfc1aSAlexander Motin 			error = ctl_be_block_open(be_lun, req);
2530b9b4269cSAlexander Motin 		else if (vn_isdisk(be_lun->vn, &error))
25314ce7a086SAlexander Motin 			error = ctl_be_block_open_dev(be_lun, req);
25323d5cb709SAlexander Motin 		else if (be_lun->vn->v_type == VREG) {
25333d5cb709SAlexander Motin 			vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
25344ce7a086SAlexander Motin 			error = ctl_be_block_open_file(be_lun, req);
2535b249ce48SMateusz Guzik 			VOP_UNLOCK(be_lun->vn);
25363d5cb709SAlexander Motin 		} else
2537b9b4269cSAlexander Motin 			error = EINVAL;
2538648dfc1aSAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
25390bcd4ab6SAlexander Motin 		    be_lun->vn != NULL) {
2540648dfc1aSAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2541648dfc1aSAlexander Motin 			ctl_lun_has_media(cbe_lun);
2542648dfc1aSAlexander Motin 		} else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2543648dfc1aSAlexander Motin 		    be_lun->vn == NULL) {
2544648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2545648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
254671d8e97eSAlexander Motin 		}
2547648dfc1aSAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
25487ac58230SAlexander Motin 	} else {
25497ac58230SAlexander Motin 		if (be_lun->vn != NULL) {
2550648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2551648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
2552ee4ad294SAlexander Motin 			taskqueue_drain_all(be_lun->io_taskqueue);
25537ac58230SAlexander Motin 			error = ctl_be_block_close(be_lun);
25547ac58230SAlexander Motin 		} else
25557ac58230SAlexander Motin 			error = 0;
25567ac58230SAlexander Motin 	}
25577ac58230SAlexander Motin 	if (be_lun->size_blocks != oldsize)
25587ac58230SAlexander Motin 		ctl_lun_capacity_changed(cbe_lun);
255981177295SEdward Tomasz Napierala 
256081177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
256181177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
256281177295SEdward Tomasz Napierala 
256334144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
256419720f41SAlexander Motin 	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
256581177295SEdward Tomasz Napierala 	return (0);
256681177295SEdward Tomasz Napierala 
256781177295SEdward Tomasz Napierala bailout_error:
256834144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
256981177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
257081177295SEdward Tomasz Napierala 	return (0);
257181177295SEdward Tomasz Napierala }
257281177295SEdward Tomasz Napierala 
2573130f4520SKenneth D. Merry static void
2574*767300e8SAlexander Motin ctl_be_block_lun_shutdown(struct ctl_be_lun *cbe_lun)
2575130f4520SKenneth D. Merry {
2576*767300e8SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)cbe_lun;
257734144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
257834144c2cSAlexander Motin 
257934144c2cSAlexander Motin 	taskqueue_drain_all(be_lun->io_taskqueue);
258034144c2cSAlexander Motin 	taskqueue_free(be_lun->io_taskqueue);
258134144c2cSAlexander Motin 	if (be_lun->disk_stats != NULL)
258234144c2cSAlexander Motin 		devstat_remove_entry(be_lun->disk_stats);
258334144c2cSAlexander Motin 	nvlist_destroy(be_lun->cbe_lun.options);
258434144c2cSAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
258534144c2cSAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
258634144c2cSAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2587130f4520SKenneth D. Merry 
2588130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
258934144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
259034144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
259134144c2cSAlexander Motin 		wakeup(be_lun);
259234144c2cSAlexander Motin 	else
259334144c2cSAlexander Motin 		free(be_lun, M_CTLBLK);
2594130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2595130f4520SKenneth D. Merry }
2596130f4520SKenneth D. Merry 
2597130f4520SKenneth D. Merry static int
2598130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2599130f4520SKenneth D. Merry {
2600130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
26010bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2602130f4520SKenneth D. Merry 	int retval;
2603130f4520SKenneth D. Merry 
2604130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2605130f4520SKenneth D. Merry 
26069cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
2607*767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun;
2608130f4520SKenneth D. Merry 
260967cc546dSAlexander Motin 	retval = 0;
2610130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2611130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2612130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2613ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2614ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2615ee7f31c0SAlexander Motin 	case UNMAP:
2616130f4520SKenneth D. Merry 		/*
2617130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2618130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2619130f4520SKenneth D. Merry 		 *
2620130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2621130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2622130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2623130f4520SKenneth D. Merry 		 */
262475c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
2625130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2626130f4520SKenneth D. Merry 				   links);
262775c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
2628130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2629130f4520SKenneth D. Merry 		break;
2630130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2631130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2632648dfc1aSAlexander Motin 		struct ctl_lun_req req;
2633130f4520SKenneth D. Merry 
2634130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
263566b69676SAlexander Motin 		if ((cdb->how & SSS_PC_MASK) != 0) {
263666b69676SAlexander Motin 			ctl_set_success(&io->scsiio);
263766b69676SAlexander Motin 			ctl_config_write_done(io);
263866b69676SAlexander Motin 			break;
263966b69676SAlexander Motin 		}
2640648dfc1aSAlexander Motin 		if (cdb->how & SSS_START) {
2641648dfc1aSAlexander Motin 			if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2642648dfc1aSAlexander Motin 				retval = ctl_be_block_open(be_lun, &req);
2643648dfc1aSAlexander Motin 				cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2644648dfc1aSAlexander Motin 				if (retval == 0) {
2645648dfc1aSAlexander Motin 					cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2646648dfc1aSAlexander Motin 					ctl_lun_has_media(cbe_lun);
2647130f4520SKenneth D. Merry 				} else {
2648648dfc1aSAlexander Motin 					cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2649648dfc1aSAlexander Motin 					ctl_lun_no_media(cbe_lun);
2650130f4520SKenneth D. Merry 				}
2651648dfc1aSAlexander Motin 			}
2652648dfc1aSAlexander Motin 			ctl_start_lun(cbe_lun);
2653648dfc1aSAlexander Motin 		} else {
2654648dfc1aSAlexander Motin 			ctl_stop_lun(cbe_lun);
2655648dfc1aSAlexander Motin 			if (cdb->how & SSS_LOEJ) {
2656648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2657648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2658648dfc1aSAlexander Motin 				ctl_lun_ejected(cbe_lun);
2659648dfc1aSAlexander Motin 				if (be_lun->vn != NULL)
2660648dfc1aSAlexander Motin 					ctl_be_block_close(be_lun);
2661648dfc1aSAlexander Motin 			}
2662648dfc1aSAlexander Motin 		}
2663648dfc1aSAlexander Motin 
2664648dfc1aSAlexander Motin 		ctl_set_success(&io->scsiio);
2665130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2666130f4520SKenneth D. Merry 		break;
2667130f4520SKenneth D. Merry 	}
266891be33dcSAlexander Motin 	case PREVENT_ALLOW:
266991be33dcSAlexander Motin 		ctl_set_success(&io->scsiio);
267091be33dcSAlexander Motin 		ctl_config_write_done(io);
267191be33dcSAlexander Motin 		break;
2672130f4520SKenneth D. Merry 	default:
2673130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2674130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2675130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2676130f4520SKenneth D. Merry 		break;
2677130f4520SKenneth D. Merry 	}
2678130f4520SKenneth D. Merry 
2679130f4520SKenneth D. Merry 	return (retval);
2680130f4520SKenneth D. Merry }
2681130f4520SKenneth D. Merry 
2682130f4520SKenneth D. Merry static int
2683130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2684130f4520SKenneth D. Merry {
2685ef8daf3fSAlexander Motin 	struct ctl_be_block_lun *be_lun;
2686ef8daf3fSAlexander Motin 	int retval = 0;
2687ef8daf3fSAlexander Motin 
2688ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
2689ef8daf3fSAlexander Motin 
2690*767300e8SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)CTL_BACKEND_LUN(io);
2691ef8daf3fSAlexander Motin 
2692ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
2693ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:
2694ef8daf3fSAlexander Motin 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2695ef8daf3fSAlexander Motin 			mtx_lock(&be_lun->queue_lock);
2696ef8daf3fSAlexander Motin 			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2697ef8daf3fSAlexander Motin 			    &io->io_hdr, links);
2698ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
2699ef8daf3fSAlexander Motin 			taskqueue_enqueue(be_lun->io_taskqueue,
2700ef8daf3fSAlexander Motin 			    &be_lun->io_task);
2701ef8daf3fSAlexander Motin 			retval = CTL_RETVAL_QUEUED;
2702ef8daf3fSAlexander Motin 			break;
2703ef8daf3fSAlexander Motin 		}
2704ef8daf3fSAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
2705ef8daf3fSAlexander Motin 				      /*sks_valid*/ 1,
2706ef8daf3fSAlexander Motin 				      /*command*/ 1,
2707ef8daf3fSAlexander Motin 				      /*field*/ 1,
2708ef8daf3fSAlexander Motin 				      /*bit_valid*/ 1,
2709ef8daf3fSAlexander Motin 				      /*bit*/ 4);
2710ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2711ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2712ef8daf3fSAlexander Motin 		break;
2713ef8daf3fSAlexander Motin 	default:
2714ef8daf3fSAlexander Motin 		ctl_set_invalid_opcode(&io->scsiio);
2715ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2716ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2717ef8daf3fSAlexander Motin 		break;
2718ef8daf3fSAlexander Motin 	}
2719ef8daf3fSAlexander Motin 
2720ef8daf3fSAlexander Motin 	return (retval);
2721130f4520SKenneth D. Merry }
2722130f4520SKenneth D. Merry 
2723130f4520SKenneth D. Merry static int
2724*767300e8SAlexander Motin ctl_be_block_lun_info(struct ctl_be_lun *cbe_lun, struct sbuf *sb)
2725130f4520SKenneth D. Merry {
2726*767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2727130f4520SKenneth D. Merry 	int retval;
2728130f4520SKenneth D. Merry 
27292cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "\t<num_threads>");
2730130f4520SKenneth D. Merry 	if (retval != 0)
2731130f4520SKenneth D. Merry 		goto bailout;
2732130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2733130f4520SKenneth D. Merry 	if (retval != 0)
2734130f4520SKenneth D. Merry 		goto bailout;
27352cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "</num_threads>\n");
2736130f4520SKenneth D. Merry 
2737130f4520SKenneth D. Merry bailout:
2738130f4520SKenneth D. Merry 	return (retval);
2739130f4520SKenneth D. Merry }
2740130f4520SKenneth D. Merry 
2741c3e7ba3eSAlexander Motin static uint64_t
2742*767300e8SAlexander Motin ctl_be_block_lun_attr(struct ctl_be_lun *cbe_lun, const char *attrname)
2743c3e7ba3eSAlexander Motin {
2744*767300e8SAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)cbe_lun;
2745c3e7ba3eSAlexander Motin 
2746c3e7ba3eSAlexander Motin 	if (lun->getattr == NULL)
2747c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
2748c3e7ba3eSAlexander Motin 	return (lun->getattr(lun, attrname));
2749c3e7ba3eSAlexander Motin }
2750c3e7ba3eSAlexander Motin 
27510c629e28SAlexander Motin static int
2752130f4520SKenneth D. Merry ctl_be_block_init(void)
2753130f4520SKenneth D. Merry {
27540c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
2755130f4520SKenneth D. Merry 
275634144c2cSAlexander Motin 	sx_init(&softc->modify_lock, "ctlblock modify");
275775c7a1d3SAlexander Motin 	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
27580c629e28SAlexander Motin 	softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2759a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
27600d7fed74SAlexander Motin 	softc->buf_zone = uma_zcreate("ctlblock", CTLBLK_MAX_SEG,
27610d7fed74SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
276234144c2cSAlexander Motin 	SLIST_INIT(&softc->lun_list);
27630c629e28SAlexander Motin 	return (0);
27640c629e28SAlexander Motin }
2765130f4520SKenneth D. Merry 
27660c629e28SAlexander Motin 
27670c629e28SAlexander Motin static int
27680c629e28SAlexander Motin ctl_be_block_shutdown(void)
27690c629e28SAlexander Motin {
27700c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
277134144c2cSAlexander Motin 	struct ctl_be_block_lun *lun;
27720c629e28SAlexander Motin 
27730c629e28SAlexander Motin 	mtx_lock(&softc->lock);
277434144c2cSAlexander Motin 	while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
277534144c2cSAlexander Motin 		SLIST_REMOVE_HEAD(&softc->lun_list, links);
277634144c2cSAlexander Motin 		softc->num_luns--;
27770c629e28SAlexander Motin 		/*
277834144c2cSAlexander Motin 		 * Drop our lock here.  Since ctl_remove_lun() can call
27790c629e28SAlexander Motin 		 * back into us, this could potentially lead to a recursive
27800c629e28SAlexander Motin 		 * lock of the same mutex, which would cause a hang.
27810c629e28SAlexander Motin 		 */
27820c629e28SAlexander Motin 		mtx_unlock(&softc->lock);
278334144c2cSAlexander Motin 		ctl_remove_lun(&lun->cbe_lun);
27840c629e28SAlexander Motin 		mtx_lock(&softc->lock);
27850c629e28SAlexander Motin 	}
27860c629e28SAlexander Motin 	mtx_unlock(&softc->lock);
27870d7fed74SAlexander Motin 	uma_zdestroy(softc->buf_zone);
27880c629e28SAlexander Motin 	uma_zdestroy(softc->beio_zone);
27890c629e28SAlexander Motin 	mtx_destroy(&softc->lock);
279034144c2cSAlexander Motin 	sx_destroy(&softc->modify_lock);
27910c629e28SAlexander Motin 	return (0);
2792130f4520SKenneth D. Merry }
2793