xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision 34144c2c7153ceb7deb6732434aaf3de5522e7d0)
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>
83*34144c2cSAlexander 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 {
15519720f41SAlexander Motin 	struct ctl_lun_create_params params;
156130f4520SKenneth D. Merry 	char *dev_path;
157130f4520SKenneth D. Merry 	ctl_be_block_type dev_type;
158130f4520SKenneth D. Merry 	struct vnode *vn;
159130f4520SKenneth D. Merry 	union ctl_be_block_bedata backend;
160130f4520SKenneth D. Merry 	cbb_dispatch_t dispatch;
161130f4520SKenneth D. Merry 	cbb_dispatch_t lun_flush;
162ee7f31c0SAlexander Motin 	cbb_dispatch_t unmap;
163ef8daf3fSAlexander Motin 	cbb_dispatch_t get_lba_status;
164c3e7ba3eSAlexander Motin 	cbb_getattr_t getattr;
165130f4520SKenneth D. Merry 	uma_zone_t lun_zone;
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;
171*34144c2cSAlexander Motin 	SLIST_ENTRY(ctl_be_block_lun) links;
1720bcd4ab6SAlexander Motin 	struct ctl_be_lun cbe_lun;
173130f4520SKenneth D. Merry 	struct taskqueue *io_taskqueue;
174130f4520SKenneth D. Merry 	struct task io_task;
175130f4520SKenneth D. Merry 	int num_threads;
176130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) input_queue;
177ef8daf3fSAlexander Motin 	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
178130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
179130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
18075c7a1d3SAlexander Motin 	struct mtx_padalign io_lock;
18175c7a1d3SAlexander Motin 	struct mtx_padalign queue_lock;
182130f4520SKenneth D. Merry };
183130f4520SKenneth D. Merry 
184130f4520SKenneth D. Merry /*
185130f4520SKenneth D. Merry  * Overall softc structure for the block backend module.
186130f4520SKenneth D. Merry  */
187130f4520SKenneth D. Merry struct ctl_be_block_softc {
188*34144c2cSAlexander Motin 	struct sx			 modify_lock;
189130f4520SKenneth D. Merry 	struct mtx			 lock;
1900c629e28SAlexander Motin 	uma_zone_t			 beio_zone;
191130f4520SKenneth D. Merry 	int				 num_luns;
192*34144c2cSAlexander Motin 	SLIST_HEAD(, ctl_be_block_lun)	 lun_list;
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;
205130f4520SKenneth D. Merry 	int				num_segs;
206130f4520SKenneth D. Merry 	int				num_bios_sent;
207130f4520SKenneth D. Merry 	int				num_bios_done;
208130f4520SKenneth D. Merry 	int				send_complete;
2091f0694a6SAlexander Motin 	int				first_error;
2101f0694a6SAlexander Motin 	uint64_t			first_error_offset;
211130f4520SKenneth D. Merry 	struct bintime			ds_t0;
212130f4520SKenneth D. Merry 	devstat_tag_type		ds_tag_type;
213130f4520SKenneth D. Merry 	devstat_trans_flags		ds_trans_type;
214130f4520SKenneth D. Merry 	uint64_t			io_len;
215130f4520SKenneth D. Merry 	uint64_t			io_offset;
2167d0d4342SAlexander Motin 	int				io_arg;
217130f4520SKenneth D. Merry 	struct ctl_be_block_softc	*softc;
218130f4520SKenneth D. Merry 	struct ctl_be_block_lun		*lun;
219ee7f31c0SAlexander Motin 	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
220130f4520SKenneth D. Merry };
221130f4520SKenneth D. Merry 
2227ac58230SAlexander Motin extern struct ctl_softc *control_softc;
2237ac58230SAlexander Motin 
224130f4520SKenneth D. Merry static int cbb_num_threads = 14;
2257029da5cSPawel Biernacki SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
226130f4520SKenneth D. Merry 	    "CAM Target Layer Block Backend");
227af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
228130f4520SKenneth D. Merry            &cbb_num_threads, 0, "Number of threads per backing file");
229130f4520SKenneth D. Merry 
230130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
231130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio);
232130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio);
233130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io);
234130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio);
235130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
236130f4520SKenneth D. Merry 				    struct ctl_be_block_io *beio);
237130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
238130f4520SKenneth D. Merry 				       struct ctl_be_block_io *beio);
239ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
240ef8daf3fSAlexander Motin 				  struct ctl_be_block_io *beio);
24153c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
24253c146deSAlexander Motin 					 const char *attrname);
243130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
244130f4520SKenneth D. Merry 				   struct ctl_be_block_io *beio);
245ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
246ee7f31c0SAlexander Motin 				   struct ctl_be_block_io *beio);
247130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
248130f4520SKenneth D. Merry 				      struct ctl_be_block_io *beio);
249c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
250c3e7ba3eSAlexander Motin 					 const char *attrname);
251ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
252ef8daf3fSAlexander Motin 				    union ctl_io *io);
253130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
254130f4520SKenneth D. Merry 				    union ctl_io *io);
255130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
256130f4520SKenneth D. Merry 				  union ctl_io *io);
257130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending);
258130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io);
259130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
260130f4520SKenneth D. Merry 				   int flag, struct thread *td);
261130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
262130f4520SKenneth D. Merry 				  struct ctl_lun_req *req);
263130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
264130f4520SKenneth D. Merry 				 struct ctl_lun_req *req);
265130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
266648dfc1aSAlexander Motin static int ctl_be_block_open(struct ctl_be_block_lun *be_lun,
267130f4520SKenneth D. Merry 			     struct ctl_lun_req *req);
268130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc,
269130f4520SKenneth D. Merry 			       struct ctl_lun_req *req);
270130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
271130f4520SKenneth D. Merry 			   struct ctl_lun_req *req);
27281177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
27381177295SEdward Tomasz Napierala 			   struct ctl_lun_req *req);
274130f4520SKenneth D. Merry static void ctl_be_block_lun_shutdown(void *be_lun);
275130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io);
276130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io);
277130f4520SKenneth D. Merry static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
278c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_lun_attr(void *be_lun, const char *attrname);
2790c629e28SAlexander Motin static int ctl_be_block_init(void);
2800c629e28SAlexander Motin static int ctl_be_block_shutdown(void);
281130f4520SKenneth D. Merry 
282130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver =
283130f4520SKenneth D. Merry {
2842a2443d8SKenneth D. Merry 	.name = "block",
2852a2443d8SKenneth D. Merry 	.flags = CTL_BE_FLAG_HAS_CONFIG,
2862a2443d8SKenneth D. Merry 	.init = ctl_be_block_init,
2870c629e28SAlexander Motin 	.shutdown = ctl_be_block_shutdown,
2882a2443d8SKenneth D. Merry 	.data_submit = ctl_be_block_submit,
2892a2443d8SKenneth D. Merry 	.data_move_done = ctl_be_block_move_done,
2902a2443d8SKenneth D. Merry 	.config_read = ctl_be_block_config_read,
2912a2443d8SKenneth D. Merry 	.config_write = ctl_be_block_config_write,
2922a2443d8SKenneth D. Merry 	.ioctl = ctl_be_block_ioctl,
293c3e7ba3eSAlexander Motin 	.lun_info = ctl_be_block_lun_info,
294c3e7ba3eSAlexander Motin 	.lun_attr = ctl_be_block_lun_attr
295130f4520SKenneth D. Merry };
296130f4520SKenneth D. Merry 
297*34144c2cSAlexander Motin MALLOC_DEFINE(M_CTLBLK, "ctlblock", "Memory used for CTL block backend");
298130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
299130f4520SKenneth D. Merry 
300130f4520SKenneth D. Merry static struct ctl_be_block_io *
301130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc)
302130f4520SKenneth D. Merry {
303130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
304130f4520SKenneth D. Merry 
3050c629e28SAlexander Motin 	beio = uma_zalloc(softc->beio_zone, M_WAITOK | M_ZERO);
306130f4520SKenneth D. Merry 	beio->softc = softc;
307130f4520SKenneth D. Merry 	return (beio);
308130f4520SKenneth D. Merry }
309130f4520SKenneth D. Merry 
310130f4520SKenneth D. Merry static void
311130f4520SKenneth D. Merry ctl_free_beio(struct ctl_be_block_io *beio)
312130f4520SKenneth D. Merry {
313130f4520SKenneth D. Merry 	int duplicate_free;
314130f4520SKenneth D. Merry 	int i;
315130f4520SKenneth D. Merry 
316130f4520SKenneth D. Merry 	duplicate_free = 0;
317130f4520SKenneth D. Merry 
318130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
319130f4520SKenneth D. Merry 		if (beio->sg_segs[i].addr == NULL)
320130f4520SKenneth D. Merry 			duplicate_free++;
321130f4520SKenneth D. Merry 
322130f4520SKenneth D. Merry 		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
323130f4520SKenneth D. Merry 		beio->sg_segs[i].addr = NULL;
32411b569f7SAlexander Motin 
32511b569f7SAlexander Motin 		/* For compare we had two equal S/G lists. */
32611b569f7SAlexander Motin 		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
32711b569f7SAlexander Motin 			uma_zfree(beio->lun->lun_zone,
32811b569f7SAlexander Motin 			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
32911b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
33011b569f7SAlexander Motin 		}
331130f4520SKenneth D. Merry 	}
332130f4520SKenneth D. Merry 
333130f4520SKenneth D. Merry 	if (duplicate_free > 0) {
334130f4520SKenneth D. Merry 		printf("%s: %d duplicate frees out of %d segments\n", __func__,
335130f4520SKenneth D. Merry 		       duplicate_free, beio->num_segs);
336130f4520SKenneth D. Merry 	}
337a0e36aeeSEdward Tomasz Napierala 
3380c629e28SAlexander Motin 	uma_zfree(beio->softc->beio_zone, beio);
339130f4520SKenneth D. Merry }
340130f4520SKenneth D. Merry 
341130f4520SKenneth D. Merry static void
342130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio)
343130f4520SKenneth D. Merry {
34475c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
345130f4520SKenneth D. Merry 
346ee7f31c0SAlexander Motin 	if (beio->beio_cont != NULL) {
347ee7f31c0SAlexander Motin 		beio->beio_cont(beio);
348ee7f31c0SAlexander Motin 	} else {
349130f4520SKenneth D. Merry 		ctl_free_beio(beio);
35011b569f7SAlexander Motin 		ctl_data_submit_done(io);
351130f4520SKenneth D. Merry 	}
352ee7f31c0SAlexander Motin }
353130f4520SKenneth D. Merry 
354d6043e46SAlexander Motin static size_t
355d6043e46SAlexander Motin cmp(uint8_t *a, uint8_t *b, size_t size)
356d6043e46SAlexander Motin {
357d6043e46SAlexander Motin 	size_t i;
358d6043e46SAlexander Motin 
359d6043e46SAlexander Motin 	for (i = 0; i < size; i++) {
360d6043e46SAlexander Motin 		if (a[i] != b[i])
361d6043e46SAlexander Motin 			break;
362d6043e46SAlexander Motin 	}
363d6043e46SAlexander Motin 	return (i);
364d6043e46SAlexander Motin }
365d6043e46SAlexander Motin 
366d6043e46SAlexander Motin static void
367d6043e46SAlexander Motin ctl_be_block_compare(union ctl_io *io)
368d6043e46SAlexander Motin {
369d6043e46SAlexander Motin 	struct ctl_be_block_io *beio;
370d6043e46SAlexander Motin 	uint64_t off, res;
371d6043e46SAlexander Motin 	int i;
372d6043e46SAlexander Motin 	uint8_t info[8];
373d6043e46SAlexander Motin 
374d6043e46SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
375d6043e46SAlexander Motin 	off = 0;
376d6043e46SAlexander Motin 	for (i = 0; i < beio->num_segs; i++) {
377d6043e46SAlexander Motin 		res = cmp(beio->sg_segs[i].addr,
378d6043e46SAlexander Motin 		    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
379d6043e46SAlexander Motin 		    beio->sg_segs[i].len);
380d6043e46SAlexander Motin 		off += res;
381d6043e46SAlexander Motin 		if (res < beio->sg_segs[i].len)
382d6043e46SAlexander Motin 			break;
383d6043e46SAlexander Motin 	}
384d6043e46SAlexander Motin 	if (i < beio->num_segs) {
385d6043e46SAlexander Motin 		scsi_u64to8b(off, info);
386d6043e46SAlexander Motin 		ctl_set_sense(&io->scsiio, /*current_error*/ 1,
387d6043e46SAlexander Motin 		    /*sense_key*/ SSD_KEY_MISCOMPARE,
388d6043e46SAlexander Motin 		    /*asc*/ 0x1D, /*ascq*/ 0x00,
389d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_INFO,
390d6043e46SAlexander Motin 		    /*size*/ sizeof(info), /*data*/ &info,
391d6043e46SAlexander Motin 		    /*type*/ SSD_ELEM_NONE);
392d6043e46SAlexander Motin 	} else
393d6043e46SAlexander Motin 		ctl_set_success(&io->scsiio);
394d6043e46SAlexander Motin }
395d6043e46SAlexander Motin 
396130f4520SKenneth D. Merry static int
397130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io)
398130f4520SKenneth D. Merry {
399130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
400130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
40111b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
402130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
403130f4520SKenneth D. Merry 	struct bintime cur_bt;
404130f4520SKenneth D. Merry #endif
405130f4520SKenneth D. Merry 
406e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
407130f4520SKenneth D. Merry 	be_lun = beio->lun;
408130f4520SKenneth D. Merry 
409130f4520SKenneth D. Merry 	DPRINTF("entered\n");
410130f4520SKenneth D. Merry 
411130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
412e675024aSAlexander Motin 	getbinuptime(&cur_bt);
413130f4520SKenneth D. Merry 	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
414130f4520SKenneth D. Merry 	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
415130f4520SKenneth D. Merry #endif
416e675024aSAlexander Motin 	io->io_hdr.num_dmas++;
41711b569f7SAlexander Motin 	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
418130f4520SKenneth D. Merry 
419130f4520SKenneth D. Merry 	/*
420130f4520SKenneth D. Merry 	 * We set status at this point for read commands, and write
421130f4520SKenneth D. Merry 	 * commands with errors.
422130f4520SKenneth D. Merry 	 */
423f7241cceSAlexander Motin 	if (io->io_hdr.flags & CTL_FLAG_ABORT) {
424f7241cceSAlexander Motin 		;
425eb6ac6f9SAlexander Motin 	} else if ((io->io_hdr.port_status != 0) &&
426eb6ac6f9SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
427eb6ac6f9SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
428eb6ac6f9SAlexander Motin 		ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
429eb6ac6f9SAlexander Motin 		    /*retry_count*/ io->io_hdr.port_status);
430eb6ac6f9SAlexander Motin 	} else if (io->scsiio.kern_data_resid != 0 &&
431eb6ac6f9SAlexander Motin 	    (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
432eb6ac6f9SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
433eb6ac6f9SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
434eb6ac6f9SAlexander Motin 		ctl_set_invalid_field_ciu(&io->scsiio);
435f7241cceSAlexander Motin 	} else if ((io->io_hdr.port_status == 0) &&
43611b569f7SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
43711b569f7SAlexander Motin 		lbalen = ARGS(beio->io);
43811b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_READ) {
439130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
44011b569f7SAlexander Motin 		} else if (lbalen->flags & CTL_LLF_COMPARE) {
44111b569f7SAlexander Motin 			/* We have two data blocks ready for comparison. */
442d6043e46SAlexander Motin 			ctl_be_block_compare(io);
44311b569f7SAlexander Motin 		}
444130f4520SKenneth D. Merry 	}
445130f4520SKenneth D. Merry 
446130f4520SKenneth D. Merry 	/*
447130f4520SKenneth D. Merry 	 * If this is a read, or a write with errors, it is done.
448130f4520SKenneth D. Merry 	 */
449130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_READ)
450130f4520SKenneth D. Merry 	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
451130f4520SKenneth D. Merry 	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
452130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
453130f4520SKenneth D. Merry 		return (0);
454130f4520SKenneth D. Merry 	}
455130f4520SKenneth D. Merry 
456130f4520SKenneth D. Merry 	/*
457130f4520SKenneth D. Merry 	 * At this point, we have a write and the DMA completed
458130f4520SKenneth D. Merry 	 * successfully.  We now have to queue it to the task queue to
459130f4520SKenneth D. Merry 	 * execute the backend I/O.  That is because we do blocking
460130f4520SKenneth D. Merry 	 * memory allocations, and in the file backing case, blocking I/O.
461130f4520SKenneth D. Merry 	 * This move done routine is generally called in the SIM's
462130f4520SKenneth D. Merry 	 * interrupt context, and therefore we cannot block.
463130f4520SKenneth D. Merry 	 */
46475c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
465130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
46675c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
467130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
468130f4520SKenneth D. Merry 
469130f4520SKenneth D. Merry 	return (0);
470130f4520SKenneth D. Merry }
471130f4520SKenneth D. Merry 
472130f4520SKenneth D. Merry static void
473130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio)
474130f4520SKenneth D. Merry {
475130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
476130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
477130f4520SKenneth D. Merry 	union ctl_io *io;
478e0c2f975SAlexander Motin 	int error;
479130f4520SKenneth D. Merry 
480130f4520SKenneth D. Merry 	beio = bio->bio_caller1;
481130f4520SKenneth D. Merry 	be_lun = beio->lun;
482130f4520SKenneth D. Merry 	io = beio->io;
483130f4520SKenneth D. Merry 
484130f4520SKenneth D. Merry 	DPRINTF("entered\n");
485130f4520SKenneth D. Merry 
486e0c2f975SAlexander Motin 	error = bio->bio_error;
48775c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
4881f0694a6SAlexander Motin 	if (error != 0 &&
4891f0694a6SAlexander Motin 	    (beio->first_error == 0 ||
4901f0694a6SAlexander Motin 	     bio->bio_offset < beio->first_error_offset)) {
4911f0694a6SAlexander Motin 		beio->first_error = error;
4921f0694a6SAlexander Motin 		beio->first_error_offset = bio->bio_offset;
4931f0694a6SAlexander Motin 	}
494130f4520SKenneth D. Merry 
495130f4520SKenneth D. Merry 	beio->num_bios_done++;
496130f4520SKenneth D. Merry 
497130f4520SKenneth D. Merry 	/*
498130f4520SKenneth D. Merry 	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
499130f4520SKenneth D. Merry 	 * during the free might cause it to complain.
500130f4520SKenneth D. Merry 	 */
501130f4520SKenneth D. Merry 	g_destroy_bio(bio);
502130f4520SKenneth D. Merry 
503130f4520SKenneth D. Merry 	/*
504130f4520SKenneth D. Merry 	 * If the send complete bit isn't set, or we aren't the last I/O to
505130f4520SKenneth D. Merry 	 * complete, then we're done.
506130f4520SKenneth D. Merry 	 */
507130f4520SKenneth D. Merry 	if ((beio->send_complete == 0)
508130f4520SKenneth D. Merry 	 || (beio->num_bios_done < beio->num_bios_sent)) {
50975c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
510130f4520SKenneth D. Merry 		return;
511130f4520SKenneth D. Merry 	}
512130f4520SKenneth D. Merry 
513130f4520SKenneth D. Merry 	/*
514130f4520SKenneth D. Merry 	 * At this point, we've verified that we are the last I/O to
515130f4520SKenneth D. Merry 	 * complete, so it's safe to drop the lock.
516130f4520SKenneth D. Merry 	 */
51775c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
51875c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
51975c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
52075c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
521130f4520SKenneth D. Merry 
522130f4520SKenneth D. Merry 	/*
523130f4520SKenneth D. Merry 	 * If there are any errors from the backing device, we fail the
524130f4520SKenneth D. Merry 	 * entire I/O with a medium error.
525130f4520SKenneth D. Merry 	 */
5261f0694a6SAlexander Motin 	error = beio->first_error;
5271f0694a6SAlexander Motin 	if (error != 0) {
528e0c2f975SAlexander Motin 		if (error == EOPNOTSUPP) {
529e0c2f975SAlexander Motin 			ctl_set_invalid_opcode(&io->scsiio);
5300631de4aSAlexander Motin 		} else if (error == ENOSPC || error == EDQUOT) {
5314fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
5326187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
5336187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
534e0c2f975SAlexander Motin 		} else if (beio->bio_cmd == BIO_FLUSH) {
535130f4520SKenneth D. Merry 			/* XXX KDM is there is a better error here? */
536130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
537130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
538130f4520SKenneth D. Merry 						 /*retry_count*/ 0xbad2);
5397f7bb97aSAlexander Motin 		} else {
5407f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
5417f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
5427f7bb97aSAlexander Motin 		}
543130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
544130f4520SKenneth D. Merry 		return;
545130f4520SKenneth D. Merry 	}
546130f4520SKenneth D. Merry 
547130f4520SKenneth D. Merry 	/*
54811b569f7SAlexander Motin 	 * If this is a write, a flush, a delete or verify, we're all done.
549130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
550130f4520SKenneth D. Merry 	 */
551130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_WRITE)
552ee7f31c0SAlexander Motin 	 || (beio->bio_cmd == BIO_FLUSH)
55311b569f7SAlexander Motin 	 || (beio->bio_cmd == BIO_DELETE)
55411b569f7SAlexander Motin 	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
555130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
556130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
557130f4520SKenneth D. Merry 	} else {
558f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
55975a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
560f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
56175a3108eSAlexander Motin 			ctl_serseq_done(io);
56275a3108eSAlexander Motin 		}
563130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
564e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
565130f4520SKenneth D. Merry #endif
566130f4520SKenneth D. Merry 		ctl_datamove(io);
567130f4520SKenneth D. Merry 	}
568130f4520SKenneth D. Merry }
569130f4520SKenneth D. Merry 
570130f4520SKenneth D. Merry static void
571130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
572130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio)
573130f4520SKenneth D. Merry {
57475c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
575130f4520SKenneth D. Merry 	struct mount *mountpoint;
5765050aa86SKonstantin Belousov 	int error, lock_flags;
577130f4520SKenneth D. Merry 
578130f4520SKenneth D. Merry 	DPRINTF("entered\n");
579130f4520SKenneth D. Merry 
58075c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
58175c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
582130f4520SKenneth D. Merry 
583130f4520SKenneth D. Merry 	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
584130f4520SKenneth D. Merry 
58567cc546dSAlexander Motin 	if (MNT_SHARED_WRITES(mountpoint) ||
58667cc546dSAlexander Motin 	    ((mountpoint == NULL) && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
587130f4520SKenneth D. Merry 		lock_flags = LK_SHARED;
588130f4520SKenneth D. Merry 	else
589130f4520SKenneth D. Merry 		lock_flags = LK_EXCLUSIVE;
590130f4520SKenneth D. Merry 	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
5917d0d4342SAlexander Motin 	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
5927d0d4342SAlexander Motin 	    curthread);
593b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
594130f4520SKenneth D. Merry 
595130f4520SKenneth D. Merry 	vn_finished_write(mountpoint);
596130f4520SKenneth D. Merry 
59775c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
59875c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
59975c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
60075c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
60175c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
60275c7a1d3SAlexander Motin 
603130f4520SKenneth D. Merry 	if (error == 0)
604130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
605130f4520SKenneth D. Merry 	else {
606130f4520SKenneth D. Merry 		/* XXX KDM is there is a better error here? */
607130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
608130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
609130f4520SKenneth D. Merry 					 /*retry_count*/ 0xbad1);
610130f4520SKenneth D. Merry 	}
611130f4520SKenneth D. Merry 
612130f4520SKenneth D. Merry 	ctl_complete_beio(beio);
613130f4520SKenneth D. Merry }
614130f4520SKenneth D. Merry 
61536160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_start, "uint64_t");
61636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_start, "uint64_t");
61736160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, file_done,"uint64_t");
61836160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, file_done, "uint64_t");
619130f4520SKenneth D. Merry 
620130f4520SKenneth D. Merry static void
621130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
622130f4520SKenneth D. Merry 			   struct ctl_be_block_io *beio)
623130f4520SKenneth D. Merry {
624130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
625130f4520SKenneth D. Merry 	union ctl_io *io;
626130f4520SKenneth D. Merry 	struct uio xuio;
627130f4520SKenneth D. Merry 	struct iovec *xiovec;
62883981e31SAlexander Motin 	size_t s;
62983981e31SAlexander Motin 	int error, flags, i;
630130f4520SKenneth D. Merry 
631130f4520SKenneth D. Merry 	DPRINTF("entered\n");
632130f4520SKenneth D. Merry 
633130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
634130f4520SKenneth D. Merry 	io = beio->io;
63555551d05SAlexander Motin 	flags = 0;
63655551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
63755551d05SAlexander Motin 		flags |= IO_DIRECT;
63855551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
63955551d05SAlexander Motin 		flags |= IO_SYNC;
640130f4520SKenneth D. Merry 
64111b569f7SAlexander Motin 	bzero(&xuio, sizeof(xuio));
642130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
64336160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
64411b569f7SAlexander Motin 		xuio.uio_rw = UIO_READ;
645130f4520SKenneth D. Merry 	} else {
64636160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
647130f4520SKenneth D. Merry 		xuio.uio_rw = UIO_WRITE;
64811b569f7SAlexander Motin 	}
649130f4520SKenneth D. Merry 	xuio.uio_offset = beio->io_offset;
650130f4520SKenneth D. Merry 	xuio.uio_resid = beio->io_len;
651130f4520SKenneth D. Merry 	xuio.uio_segflg = UIO_SYSSPACE;
652130f4520SKenneth D. Merry 	xuio.uio_iov = beio->xiovecs;
653130f4520SKenneth D. Merry 	xuio.uio_iovcnt = beio->num_segs;
654130f4520SKenneth D. Merry 	xuio.uio_td = curthread;
655130f4520SKenneth D. Merry 
656130f4520SKenneth D. Merry 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
657130f4520SKenneth D. Merry 		xiovec->iov_base = beio->sg_segs[i].addr;
658130f4520SKenneth D. Merry 		xiovec->iov_len = beio->sg_segs[i].len;
659130f4520SKenneth D. Merry 	}
660130f4520SKenneth D. Merry 
66175c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
66275c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
66375c7a1d3SAlexander Motin 
664130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
665130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
666130f4520SKenneth D. Merry 
667130f4520SKenneth D. Merry 		/*
668130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for reads.  If the
669130f4520SKenneth D. Merry 		 * DIRECTIO option is configured into the kernel, it calls
670130f4520SKenneth D. Merry 		 * ffs_rawread().  But that only works for single-segment
671130f4520SKenneth D. Merry 		 * uios with user space addresses.  In our case, with a
672130f4520SKenneth D. Merry 		 * kernel uio, it still reads into the buffer cache, but it
673130f4520SKenneth D. Merry 		 * will just try to release the buffer from the cache later
674130f4520SKenneth D. Merry 		 * on in ffs_read().
675130f4520SKenneth D. Merry 		 *
676130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for reads.
677130f4520SKenneth D. Merry 		 *
678130f4520SKenneth D. Merry 		 * UFS does not pay attention to IO_SYNC for reads.
679130f4520SKenneth D. Merry 		 *
680130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (which translates into the
681130f4520SKenneth D. Merry 		 * Solaris define FRSYNC for zfs_read()) for reads.  It
682130f4520SKenneth D. Merry 		 * attempts to sync the file before reading.
683130f4520SKenneth D. Merry 		 */
68455551d05SAlexander Motin 		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
685130f4520SKenneth D. Merry 
686b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
68736160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
68883981e31SAlexander Motin 		if (error == 0 && xuio.uio_resid > 0) {
68983981e31SAlexander Motin 			/*
69083981e31SAlexander Motin 			 * If we red less then requested (EOF), then
69183981e31SAlexander Motin 			 * we should clean the rest of the buffer.
69283981e31SAlexander Motin 			 */
69383981e31SAlexander Motin 			s = beio->io_len - xuio.uio_resid;
69483981e31SAlexander Motin 			for (i = 0; i < beio->num_segs; i++) {
69583981e31SAlexander Motin 				if (s >= beio->sg_segs[i].len) {
69683981e31SAlexander Motin 					s -= beio->sg_segs[i].len;
69783981e31SAlexander Motin 					continue;
69883981e31SAlexander Motin 				}
69983981e31SAlexander Motin 				bzero((uint8_t *)beio->sg_segs[i].addr + s,
70083981e31SAlexander Motin 				    beio->sg_segs[i].len - s);
70183981e31SAlexander Motin 				s = 0;
70283981e31SAlexander Motin 			}
70383981e31SAlexander Motin 		}
704130f4520SKenneth D. Merry 	} else {
705130f4520SKenneth D. Merry 		struct mount *mountpoint;
706130f4520SKenneth D. Merry 		int lock_flags;
707130f4520SKenneth D. Merry 
708130f4520SKenneth D. Merry 		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
709130f4520SKenneth D. Merry 
71067cc546dSAlexander Motin 		if (MNT_SHARED_WRITES(mountpoint) || ((mountpoint == NULL)
711130f4520SKenneth D. Merry 		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
712130f4520SKenneth D. Merry 			lock_flags = LK_SHARED;
713130f4520SKenneth D. Merry 		else
714130f4520SKenneth D. Merry 			lock_flags = LK_EXCLUSIVE;
715130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
716130f4520SKenneth D. Merry 
717130f4520SKenneth D. Merry 		/*
718130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for writes.  The write
719130f4520SKenneth D. Merry 		 * is done asynchronously.  (Normally the write would just
720130f4520SKenneth D. Merry 		 * get put into cache.
721130f4520SKenneth D. Merry 		 *
722130f4520SKenneth D. Merry 		 * UFS pays attention to IO_SYNC for writes.  It will
723130f4520SKenneth D. Merry 		 * attempt to write the buffer out synchronously if that
724130f4520SKenneth D. Merry 		 * flag is set.
725130f4520SKenneth D. Merry 		 *
726130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for writes.
727130f4520SKenneth D. Merry 		 *
728130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
729130f4520SKenneth D. Merry 		 * for writes.  It will flush the transaction from the
730130f4520SKenneth D. Merry 		 * cache before returning.
731130f4520SKenneth D. Merry 		 */
73255551d05SAlexander Motin 		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
733b249ce48SMateusz Guzik 		VOP_UNLOCK(be_lun->vn);
734130f4520SKenneth D. Merry 
735130f4520SKenneth D. Merry 		vn_finished_write(mountpoint);
73636160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
737130f4520SKenneth D. Merry         }
738130f4520SKenneth D. Merry 
73975c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
74075c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
74175c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
74275c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
74375c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
74475c7a1d3SAlexander Motin 
745130f4520SKenneth D. Merry 	/*
746130f4520SKenneth D. Merry 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
747130f4520SKenneth D. Merry 	 * return the I/O to the user.
748130f4520SKenneth D. Merry 	 */
749130f4520SKenneth D. Merry 	if (error != 0) {
7500631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
7514fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
7526187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
7536187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
7547f7bb97aSAlexander Motin 		} else {
7557f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
7567f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
7577f7bb97aSAlexander Motin 		}
758130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
759130f4520SKenneth D. Merry 		return;
760130f4520SKenneth D. Merry 	}
761130f4520SKenneth D. Merry 
762130f4520SKenneth D. Merry 	/*
763696297adSAlexander Motin 	 * If this is a write or a verify, we're all done.
764130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
765130f4520SKenneth D. Merry 	 */
766696297adSAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
767696297adSAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
768130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
769130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
770130f4520SKenneth D. Merry 	} else {
771f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
77275a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
773f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
77475a3108eSAlexander Motin 			ctl_serseq_done(io);
77575a3108eSAlexander Motin 		}
776130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
777e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
778130f4520SKenneth D. Merry #endif
779130f4520SKenneth D. Merry 		ctl_datamove(io);
780130f4520SKenneth D. Merry 	}
781130f4520SKenneth D. Merry }
782130f4520SKenneth D. Merry 
783130f4520SKenneth D. Merry static void
784ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
785ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
786ef8daf3fSAlexander Motin {
787ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
788ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
789ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
790ef8daf3fSAlexander Motin 	off_t roff, off;
791ef8daf3fSAlexander Motin 	int error, status;
792ef8daf3fSAlexander Motin 
793ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
794ef8daf3fSAlexander Motin 
7950bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
796ef8daf3fSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
797ef8daf3fSAlexander Motin 	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
798ef8daf3fSAlexander Motin 	    0, curthread->td_ucred, curthread);
799ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
800ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
801ef8daf3fSAlexander Motin 	else {
802ef8daf3fSAlexander Motin 		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
803ef8daf3fSAlexander Motin 		    0, curthread->td_ucred, curthread);
804ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
805ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
806ef8daf3fSAlexander Motin 		else {
807ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
808ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
809ef8daf3fSAlexander Motin 		}
810ef8daf3fSAlexander Motin 	}
811b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
812ef8daf3fSAlexander Motin 
813ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
814ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
8150bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
8160bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
817ef8daf3fSAlexander Motin 	data->descr[0].status = status;
818ef8daf3fSAlexander Motin 
819ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
820ef8daf3fSAlexander Motin }
821ef8daf3fSAlexander Motin 
82253c146deSAlexander Motin static uint64_t
82353c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
82453c146deSAlexander Motin {
82553c146deSAlexander Motin 	struct vattr		vattr;
82653c146deSAlexander Motin 	struct statfs		statfs;
827b9b4269cSAlexander Motin 	uint64_t		val;
82853c146deSAlexander Motin 	int			error;
82953c146deSAlexander Motin 
830b9b4269cSAlexander Motin 	val = UINT64_MAX;
83153c146deSAlexander Motin 	if (be_lun->vn == NULL)
832b9b4269cSAlexander Motin 		return (val);
833b9b4269cSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
83453c146deSAlexander Motin 	if (strcmp(attrname, "blocksused") == 0) {
83553c146deSAlexander Motin 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
836b9b4269cSAlexander Motin 		if (error == 0)
8370bcd4ab6SAlexander Motin 			val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
83853c146deSAlexander Motin 	}
839b9b4269cSAlexander Motin 	if (strcmp(attrname, "blocksavail") == 0 &&
840abd80ddbSMateusz Guzik 	    !VN_IS_DOOMED(be_lun->vn)) {
84153c146deSAlexander Motin 		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
842b9b4269cSAlexander Motin 		if (error == 0)
843a15bbf15SAlexander Motin 			val = statfs.f_bavail * statfs.f_bsize /
8440bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
84553c146deSAlexander Motin 	}
846b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
847b9b4269cSAlexander Motin 	return (val);
84853c146deSAlexander Motin }
84953c146deSAlexander Motin 
850ef8daf3fSAlexander Motin static void
85167f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
85267f586a8SAlexander Motin 			   struct ctl_be_block_io *beio)
85367f586a8SAlexander Motin {
85467f586a8SAlexander Motin 	union ctl_io *io;
8553236151eSAlexander Motin 	struct cdevsw *csw;
8563236151eSAlexander Motin 	struct cdev *dev;
85767f586a8SAlexander Motin 	struct uio xuio;
85867f586a8SAlexander Motin 	struct iovec *xiovec;
8593236151eSAlexander Motin 	int error, flags, i, ref;
86067f586a8SAlexander Motin 
86167f586a8SAlexander Motin 	DPRINTF("entered\n");
86267f586a8SAlexander Motin 
86367f586a8SAlexander Motin 	io = beio->io;
86455551d05SAlexander Motin 	flags = 0;
86555551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
86655551d05SAlexander Motin 		flags |= IO_DIRECT;
86755551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
86855551d05SAlexander Motin 		flags |= IO_SYNC;
86967f586a8SAlexander Motin 
87067f586a8SAlexander Motin 	bzero(&xuio, sizeof(xuio));
87167f586a8SAlexander Motin 	if (beio->bio_cmd == BIO_READ) {
87236160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_start);
87367f586a8SAlexander Motin 		xuio.uio_rw = UIO_READ;
87467f586a8SAlexander Motin 	} else {
87536160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_start);
87667f586a8SAlexander Motin 		xuio.uio_rw = UIO_WRITE;
87767f586a8SAlexander Motin 	}
87867f586a8SAlexander Motin 	xuio.uio_offset = beio->io_offset;
87967f586a8SAlexander Motin 	xuio.uio_resid = beio->io_len;
88067f586a8SAlexander Motin 	xuio.uio_segflg = UIO_SYSSPACE;
88167f586a8SAlexander Motin 	xuio.uio_iov = beio->xiovecs;
88267f586a8SAlexander Motin 	xuio.uio_iovcnt = beio->num_segs;
88367f586a8SAlexander Motin 	xuio.uio_td = curthread;
88467f586a8SAlexander Motin 
88567f586a8SAlexander Motin 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
88667f586a8SAlexander Motin 		xiovec->iov_base = beio->sg_segs[i].addr;
88767f586a8SAlexander Motin 		xiovec->iov_len = beio->sg_segs[i].len;
88867f586a8SAlexander Motin 	}
88967f586a8SAlexander Motin 
89067f586a8SAlexander Motin 	binuptime(&beio->ds_t0);
89167f586a8SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
89267f586a8SAlexander Motin 
8933236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
8943236151eSAlexander Motin 	if (csw) {
8953236151eSAlexander Motin 		if (beio->bio_cmd == BIO_READ)
8963236151eSAlexander Motin 			error = csw->d_read(dev, &xuio, flags);
8973236151eSAlexander Motin 		else
8983236151eSAlexander Motin 			error = csw->d_write(dev, &xuio, flags);
8993236151eSAlexander Motin 		dev_relthread(dev, ref);
9003236151eSAlexander Motin 	} else
9013236151eSAlexander Motin 		error = ENXIO;
9023236151eSAlexander Motin 
9033236151eSAlexander Motin 	if (beio->bio_cmd == BIO_READ)
90436160958SMark Johnston 		SDT_PROBE0(cbb, , read, file_done);
9053236151eSAlexander Motin 	else
90636160958SMark Johnston 		SDT_PROBE0(cbb, , write, file_done);
90767f586a8SAlexander Motin 
90867f586a8SAlexander Motin 	mtx_lock(&be_lun->io_lock);
90967f586a8SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
91067f586a8SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
91167f586a8SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
91267f586a8SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
91367f586a8SAlexander Motin 
91467f586a8SAlexander Motin 	/*
91567f586a8SAlexander Motin 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
91667f586a8SAlexander Motin 	 * return the I/O to the user.
91767f586a8SAlexander Motin 	 */
91867f586a8SAlexander Motin 	if (error != 0) {
9190631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
9204fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
9216187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
9226187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
9237f7bb97aSAlexander Motin 		} else {
9247f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
9257f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
9267f7bb97aSAlexander Motin 		}
92767f586a8SAlexander Motin 		ctl_complete_beio(beio);
92867f586a8SAlexander Motin 		return;
92967f586a8SAlexander Motin 	}
93067f586a8SAlexander Motin 
93167f586a8SAlexander Motin 	/*
93267f586a8SAlexander Motin 	 * If this is a write or a verify, we're all done.
93367f586a8SAlexander Motin 	 * If this is a read, we can now send the data to the user.
93467f586a8SAlexander Motin 	 */
93567f586a8SAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
93667f586a8SAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
93767f586a8SAlexander Motin 		ctl_set_success(&io->scsiio);
93867f586a8SAlexander Motin 		ctl_complete_beio(beio);
93967f586a8SAlexander Motin 	} else {
940f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
94175a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
942f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
94375a3108eSAlexander Motin 			ctl_serseq_done(io);
94475a3108eSAlexander Motin 		}
94567f586a8SAlexander Motin #ifdef CTL_TIME_IO
946e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
94767f586a8SAlexander Motin #endif
94867f586a8SAlexander Motin 		ctl_datamove(io);
94967f586a8SAlexander Motin 	}
95067f586a8SAlexander Motin }
95167f586a8SAlexander Motin 
95267f586a8SAlexander Motin static void
953ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
954ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
955ef8daf3fSAlexander Motin {
956ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
9573236151eSAlexander Motin 	struct cdevsw *csw;
9583236151eSAlexander Motin 	struct cdev *dev;
959ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
960ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
961ef8daf3fSAlexander Motin 	off_t roff, off;
9623236151eSAlexander Motin 	int error, ref, status;
963ef8daf3fSAlexander Motin 
964ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
965ef8daf3fSAlexander Motin 
9663236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
9673236151eSAlexander Motin 	if (csw == NULL) {
9683236151eSAlexander Motin 		status = 0;	/* unknown up to the end */
9693236151eSAlexander Motin 		off = be_lun->size_bytes;
9703236151eSAlexander Motin 		goto done;
9713236151eSAlexander Motin 	}
9720bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
9733236151eSAlexander Motin 	error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
9743236151eSAlexander Motin 	    curthread);
975ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
976ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
977ef8daf3fSAlexander Motin 	else {
9783236151eSAlexander Motin 		error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
9793236151eSAlexander Motin 		    curthread);
980ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
981ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
982ef8daf3fSAlexander Motin 		else {
983ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
984ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
985ef8daf3fSAlexander Motin 		}
986ef8daf3fSAlexander Motin 	}
9873236151eSAlexander Motin 	dev_relthread(dev, ref);
988ef8daf3fSAlexander Motin 
9893236151eSAlexander Motin done:
990ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
991ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
9920bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
9930bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
994ef8daf3fSAlexander Motin 	data->descr[0].status = status;
995ef8daf3fSAlexander Motin 
996ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
997ef8daf3fSAlexander Motin }
998ef8daf3fSAlexander Motin 
999ef8daf3fSAlexander Motin static void
1000130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1001130f4520SKenneth D. Merry 		       struct ctl_be_block_io *beio)
1002130f4520SKenneth D. Merry {
1003130f4520SKenneth D. Merry 	struct bio *bio;
10043236151eSAlexander Motin 	struct cdevsw *csw;
10053236151eSAlexander Motin 	struct cdev *dev;
10063236151eSAlexander Motin 	int ref;
1007130f4520SKenneth D. Merry 
1008130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1009130f4520SKenneth D. Merry 
1010130f4520SKenneth D. Merry 	/* This can't fail, it's a blocking allocation. */
1011130f4520SKenneth D. Merry 	bio = g_alloc_bio();
1012130f4520SKenneth D. Merry 
1013130f4520SKenneth D. Merry 	bio->bio_cmd	    = BIO_FLUSH;
1014130f4520SKenneth D. Merry 	bio->bio_offset	    = 0;
1015130f4520SKenneth D. Merry 	bio->bio_data	    = 0;
1016130f4520SKenneth D. Merry 	bio->bio_done	    = ctl_be_block_biodone;
1017130f4520SKenneth D. Merry 	bio->bio_caller1    = beio;
1018130f4520SKenneth D. Merry 	bio->bio_pblkno	    = 0;
1019130f4520SKenneth D. Merry 
1020130f4520SKenneth D. Merry 	/*
1021130f4520SKenneth D. Merry 	 * We don't need to acquire the LUN lock here, because we are only
1022130f4520SKenneth D. Merry 	 * sending one bio, and so there is no other context to synchronize
1023130f4520SKenneth D. Merry 	 * with.
1024130f4520SKenneth D. Merry 	 */
1025130f4520SKenneth D. Merry 	beio->num_bios_sent = 1;
1026130f4520SKenneth D. Merry 	beio->send_complete = 1;
1027130f4520SKenneth D. Merry 
1028130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
1029130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1030130f4520SKenneth D. Merry 
10313236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10323236151eSAlexander Motin 	if (csw) {
10333236151eSAlexander Motin 		bio->bio_dev = dev;
10343236151eSAlexander Motin 		csw->d_strategy(bio);
10353236151eSAlexander Motin 		dev_relthread(dev, ref);
10363236151eSAlexander Motin 	} else {
10373236151eSAlexander Motin 		bio->bio_error = ENXIO;
10383236151eSAlexander Motin 		ctl_be_block_biodone(bio);
10393236151eSAlexander Motin 	}
1040130f4520SKenneth D. Merry }
1041130f4520SKenneth D. Merry 
1042130f4520SKenneth D. Merry static void
1043ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1044ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio,
1045ee7f31c0SAlexander Motin 		       uint64_t off, uint64_t len, int last)
1046ee7f31c0SAlexander Motin {
1047ee7f31c0SAlexander Motin 	struct bio *bio;
10488f5a226aSAlexander Motin 	uint64_t maxlen;
10493236151eSAlexander Motin 	struct cdevsw *csw;
10503236151eSAlexander Motin 	struct cdev *dev;
10513236151eSAlexander Motin 	int ref;
1052ee7f31c0SAlexander Motin 
10533236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10540bcd4ab6SAlexander Motin 	maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1055ee7f31c0SAlexander Motin 	while (len > 0) {
1056ee7f31c0SAlexander Motin 		bio = g_alloc_bio();
1057ee7f31c0SAlexander Motin 		bio->bio_cmd	    = BIO_DELETE;
10583236151eSAlexander Motin 		bio->bio_dev	    = dev;
1059ee7f31c0SAlexander Motin 		bio->bio_offset	    = off;
10608f5a226aSAlexander Motin 		bio->bio_length	    = MIN(len, maxlen);
1061ee7f31c0SAlexander Motin 		bio->bio_data	    = 0;
1062ee7f31c0SAlexander Motin 		bio->bio_done	    = ctl_be_block_biodone;
1063ee7f31c0SAlexander Motin 		bio->bio_caller1    = beio;
10640bcd4ab6SAlexander Motin 		bio->bio_pblkno     = off / be_lun->cbe_lun.blocksize;
1065ee7f31c0SAlexander Motin 
1066ee7f31c0SAlexander Motin 		off += bio->bio_length;
1067ee7f31c0SAlexander Motin 		len -= bio->bio_length;
1068ee7f31c0SAlexander Motin 
106975c7a1d3SAlexander Motin 		mtx_lock(&be_lun->io_lock);
1070ee7f31c0SAlexander Motin 		beio->num_bios_sent++;
1071ee7f31c0SAlexander Motin 		if (last && len == 0)
1072ee7f31c0SAlexander Motin 			beio->send_complete = 1;
107375c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
1074ee7f31c0SAlexander Motin 
10753236151eSAlexander Motin 		if (csw) {
10763236151eSAlexander Motin 			csw->d_strategy(bio);
10773236151eSAlexander Motin 		} else {
10783236151eSAlexander Motin 			bio->bio_error = ENXIO;
10793236151eSAlexander Motin 			ctl_be_block_biodone(bio);
1080ee7f31c0SAlexander Motin 		}
1081ee7f31c0SAlexander Motin 	}
10823236151eSAlexander Motin 	if (csw)
10833236151eSAlexander Motin 		dev_relthread(dev, ref);
10843236151eSAlexander Motin }
1085ee7f31c0SAlexander Motin 
1086ee7f31c0SAlexander Motin static void
1087ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1088ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio)
1089ee7f31c0SAlexander Motin {
1090ee7f31c0SAlexander Motin 	union ctl_io *io;
109166df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1092ee7f31c0SAlexander Motin 	struct scsi_unmap_desc *buf, *end;
1093ee7f31c0SAlexander Motin 	uint64_t len;
1094ee7f31c0SAlexander Motin 
1095ee7f31c0SAlexander Motin 	io = beio->io;
1096ee7f31c0SAlexander Motin 
1097ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1098ee7f31c0SAlexander Motin 
1099ee7f31c0SAlexander Motin 	binuptime(&beio->ds_t0);
1100ee7f31c0SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
1101ee7f31c0SAlexander Motin 
1102ee7f31c0SAlexander Motin 	if (beio->io_offset == -1) {
1103ee7f31c0SAlexander Motin 		beio->io_len = 0;
110466df9136SAlexander Motin 		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
110566df9136SAlexander Motin 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
110666df9136SAlexander Motin 		end = buf + ptrlen->len / sizeof(*buf);
1107ee7f31c0SAlexander Motin 		for (; buf < end; buf++) {
1108ee7f31c0SAlexander Motin 			len = (uint64_t)scsi_4btoul(buf->length) *
11090bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
1110ee7f31c0SAlexander Motin 			beio->io_len += len;
1111ee7f31c0SAlexander Motin 			ctl_be_block_unmap_dev_range(be_lun, beio,
11120bcd4ab6SAlexander Motin 			    scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
11130bcd4ab6SAlexander Motin 			    len, (end - buf < 2) ? TRUE : FALSE);
1114ee7f31c0SAlexander Motin 		}
1115ee7f31c0SAlexander Motin 	} else
1116ee7f31c0SAlexander Motin 		ctl_be_block_unmap_dev_range(be_lun, beio,
1117ee7f31c0SAlexander Motin 		    beio->io_offset, beio->io_len, TRUE);
1118ee7f31c0SAlexander Motin }
1119ee7f31c0SAlexander Motin 
1120ee7f31c0SAlexander Motin static void
1121130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1122130f4520SKenneth D. Merry 			  struct ctl_be_block_io *beio)
1123130f4520SKenneth D. Merry {
112475c7a1d3SAlexander Motin 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1125130f4520SKenneth D. Merry 	struct bio *bio;
11263236151eSAlexander Motin 	struct cdevsw *csw;
11273236151eSAlexander Motin 	struct cdev *dev;
1128130f4520SKenneth D. Merry 	off_t cur_offset;
11293236151eSAlexander Motin 	int i, max_iosize, ref;
1130130f4520SKenneth D. Merry 
1131130f4520SKenneth D. Merry 	DPRINTF("entered\n");
11323236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1133130f4520SKenneth D. Merry 
1134130f4520SKenneth D. Merry 	/*
1135130f4520SKenneth D. Merry 	 * We have to limit our I/O size to the maximum supported by the
1136130f4520SKenneth D. Merry 	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
1137130f4520SKenneth D. Merry 	 * set it properly, use DFLTPHYS.
1138130f4520SKenneth D. Merry 	 */
11393236151eSAlexander Motin 	if (csw) {
11403236151eSAlexander Motin 		max_iosize = dev->si_iosize_max;
1141130f4520SKenneth D. Merry 		if (max_iosize < PAGE_SIZE)
1142130f4520SKenneth D. Merry 			max_iosize = DFLTPHYS;
11433236151eSAlexander Motin 	} else
11443236151eSAlexander Motin 		max_iosize = DFLTPHYS;
1145130f4520SKenneth D. Merry 
1146130f4520SKenneth D. Merry 	cur_offset = beio->io_offset;
1147130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
1148130f4520SKenneth D. Merry 		size_t cur_size;
1149130f4520SKenneth D. Merry 		uint8_t *cur_ptr;
1150130f4520SKenneth D. Merry 
1151130f4520SKenneth D. Merry 		cur_size = beio->sg_segs[i].len;
1152130f4520SKenneth D. Merry 		cur_ptr = beio->sg_segs[i].addr;
1153130f4520SKenneth D. Merry 
1154130f4520SKenneth D. Merry 		while (cur_size > 0) {
1155130f4520SKenneth D. Merry 			/* This can't fail, it's a blocking allocation. */
1156130f4520SKenneth D. Merry 			bio = g_alloc_bio();
1157130f4520SKenneth D. Merry 
1158130f4520SKenneth D. Merry 			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1159130f4520SKenneth D. Merry 
1160130f4520SKenneth D. Merry 			bio->bio_cmd = beio->bio_cmd;
11613236151eSAlexander Motin 			bio->bio_dev = dev;
1162130f4520SKenneth D. Merry 			bio->bio_caller1 = beio;
1163130f4520SKenneth D. Merry 			bio->bio_length = min(cur_size, max_iosize);
1164130f4520SKenneth D. Merry 			bio->bio_offset = cur_offset;
1165130f4520SKenneth D. Merry 			bio->bio_data = cur_ptr;
1166130f4520SKenneth D. Merry 			bio->bio_done = ctl_be_block_biodone;
11670bcd4ab6SAlexander Motin 			bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1168130f4520SKenneth D. Merry 
1169130f4520SKenneth D. Merry 			cur_offset += bio->bio_length;
1170130f4520SKenneth D. Merry 			cur_ptr += bio->bio_length;
1171130f4520SKenneth D. Merry 			cur_size -= bio->bio_length;
1172130f4520SKenneth D. Merry 
117375c7a1d3SAlexander Motin 			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1174130f4520SKenneth D. Merry 			beio->num_bios_sent++;
1175130f4520SKenneth D. Merry 		}
1176130f4520SKenneth D. Merry 	}
117775c7a1d3SAlexander Motin 	beio->send_complete = 1;
1178024932aaSAlexander Motin 	binuptime(&beio->ds_t0);
1179024932aaSAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
118075c7a1d3SAlexander Motin 
118175c7a1d3SAlexander Motin 	/*
118275c7a1d3SAlexander Motin 	 * Fire off all allocated requests!
118375c7a1d3SAlexander Motin 	 */
118475c7a1d3SAlexander Motin 	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
118575c7a1d3SAlexander Motin 		TAILQ_REMOVE(&queue, bio, bio_queue);
11863236151eSAlexander Motin 		if (csw)
11873236151eSAlexander Motin 			csw->d_strategy(bio);
11883236151eSAlexander Motin 		else {
11893236151eSAlexander Motin 			bio->bio_error = ENXIO;
11903236151eSAlexander Motin 			ctl_be_block_biodone(bio);
119175c7a1d3SAlexander Motin 		}
1192130f4520SKenneth D. Merry 	}
11933236151eSAlexander Motin 	if (csw)
11943236151eSAlexander Motin 		dev_relthread(dev, ref);
11953236151eSAlexander Motin }
1196130f4520SKenneth D. Merry 
1197c3e7ba3eSAlexander Motin static uint64_t
1198c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1199c3e7ba3eSAlexander Motin {
1200c3e7ba3eSAlexander Motin 	struct diocgattr_arg	arg;
12013236151eSAlexander Motin 	struct cdevsw *csw;
12023236151eSAlexander Motin 	struct cdev *dev;
12033236151eSAlexander Motin 	int error, ref;
1204c3e7ba3eSAlexander Motin 
12053236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
12063236151eSAlexander Motin 	if (csw == NULL)
1207c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1208c3e7ba3eSAlexander Motin 	strlcpy(arg.name, attrname, sizeof(arg.name));
1209c3e7ba3eSAlexander Motin 	arg.len = sizeof(arg.value.off);
12103236151eSAlexander Motin 	if (csw->d_ioctl) {
12113236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
12123236151eSAlexander Motin 		    curthread);
12133236151eSAlexander Motin 	} else
12143236151eSAlexander Motin 		error = ENODEV;
12153236151eSAlexander Motin 	dev_relthread(dev, ref);
1216c3e7ba3eSAlexander Motin 	if (error != 0)
1217c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1218c3e7ba3eSAlexander Motin 	return (arg.value.off);
1219c3e7ba3eSAlexander Motin }
1220c3e7ba3eSAlexander Motin 
1221130f4520SKenneth D. Merry static void
12227d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
12237d0d4342SAlexander Motin 			    union ctl_io *io)
12247d0d4342SAlexander Motin {
12250bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
12267d0d4342SAlexander Motin 	struct ctl_be_block_io *beio;
12277d0d4342SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
12287d0d4342SAlexander Motin 
12297d0d4342SAlexander Motin 	DPRINTF("entered\n");
12307d0d4342SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
12317d0d4342SAlexander Motin 	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
12327d0d4342SAlexander Motin 
12330bcd4ab6SAlexander Motin 	beio->io_len = lbalen->len * cbe_lun->blocksize;
12340bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
12357d0d4342SAlexander Motin 	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
12367d0d4342SAlexander Motin 	beio->bio_cmd = BIO_FLUSH;
12377d0d4342SAlexander Motin 	beio->ds_trans_type = DEVSTAT_NO_DATA;
12387d0d4342SAlexander Motin 	DPRINTF("SYNC\n");
12397d0d4342SAlexander Motin 	be_lun->lun_flush(be_lun, beio);
12407d0d4342SAlexander Motin }
12417d0d4342SAlexander Motin 
12427d0d4342SAlexander Motin static void
1243ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1244ee7f31c0SAlexander Motin {
1245ee7f31c0SAlexander Motin 	union ctl_io *io;
1246ee7f31c0SAlexander Motin 
1247ee7f31c0SAlexander Motin 	io = beio->io;
1248ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1249ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1250ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1251ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1252ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1253ee7f31c0SAlexander Motin 		return;
1254ee7f31c0SAlexander Motin 	}
1255ee7f31c0SAlexander Motin 
1256ee7f31c0SAlexander Motin 	ctl_be_block_config_write(io);
1257ee7f31c0SAlexander Motin }
1258ee7f31c0SAlexander Motin 
1259ee7f31c0SAlexander Motin static void
1260ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1261ee7f31c0SAlexander Motin 			    union ctl_io *io)
1262ee7f31c0SAlexander Motin {
12630bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1264ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
126566df9136SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1266fee04ef7SAlexander Motin 	uint64_t len_left, lba;
1267fee04ef7SAlexander Motin 	uint32_t pb, pbo, adj;
1268ee7f31c0SAlexander Motin 	int i, seglen;
1269ee7f31c0SAlexander Motin 	uint8_t *buf, *end;
1270ee7f31c0SAlexander Motin 
1271ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1272ee7f31c0SAlexander Motin 
1273e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
127411b569f7SAlexander Motin 	lbalen = ARGS(beio->io);
1275ee7f31c0SAlexander Motin 
127664c5167cSAlexander Motin 	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
12773406a2a0SAlexander Motin 	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1278ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1279ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1280ee7f31c0SAlexander Motin 				      /*sks_valid*/ 1,
1281ee7f31c0SAlexander Motin 				      /*command*/ 1,
1282ee7f31c0SAlexander Motin 				      /*field*/ 1,
1283ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1284ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1285ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1286ee7f31c0SAlexander Motin 		return;
1287ee7f31c0SAlexander Motin 	}
1288ee7f31c0SAlexander Motin 
12893406a2a0SAlexander Motin 	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
12900bcd4ab6SAlexander Motin 		beio->io_offset = lbalen->lba * cbe_lun->blocksize;
12910bcd4ab6SAlexander Motin 		beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1292ee7f31c0SAlexander Motin 		beio->bio_cmd = BIO_DELETE;
1293ee7f31c0SAlexander Motin 		beio->ds_trans_type = DEVSTAT_FREE;
1294ee7f31c0SAlexander Motin 
1295ee7f31c0SAlexander Motin 		be_lun->unmap(be_lun, beio);
1296ee7f31c0SAlexander Motin 		return;
1297ee7f31c0SAlexander Motin 	}
1298ee7f31c0SAlexander Motin 
1299ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_WRITE;
1300ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_WRITE;
1301ee7f31c0SAlexander Motin 
1302ee7f31c0SAlexander Motin 	DPRINTF("WRITE SAME at LBA %jx len %u\n",
130366df9136SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len);
1304ee7f31c0SAlexander Motin 
13050bcd4ab6SAlexander Motin 	pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
13060bcd4ab6SAlexander Motin 	if (be_lun->cbe_lun.pblockoff > 0)
13070bcd4ab6SAlexander Motin 		pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1308fee04ef7SAlexander Motin 	else
1309fee04ef7SAlexander Motin 		pbo = 0;
13100bcd4ab6SAlexander Motin 	len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1311ee7f31c0SAlexander Motin 	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1312ee7f31c0SAlexander Motin 
1313ee7f31c0SAlexander Motin 		/*
1314ee7f31c0SAlexander Motin 		 * Setup the S/G entry for this chunk.
1315ee7f31c0SAlexander Motin 		 */
131608a7cce5SAlexander Motin 		seglen = MIN(CTLBLK_MAX_SEG, len_left);
13170bcd4ab6SAlexander Motin 		if (pb > cbe_lun->blocksize) {
13180bcd4ab6SAlexander Motin 			adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
131993b8c96cSAlexander Motin 			    seglen - pbo) % pb;
132093b8c96cSAlexander Motin 			if (seglen > adj)
132193b8c96cSAlexander Motin 				seglen -= adj;
132293b8c96cSAlexander Motin 			else
13230bcd4ab6SAlexander Motin 				seglen -= seglen % cbe_lun->blocksize;
132493b8c96cSAlexander Motin 		} else
13250bcd4ab6SAlexander Motin 			seglen -= seglen % cbe_lun->blocksize;
1326ee7f31c0SAlexander Motin 		beio->sg_segs[i].len = seglen;
1327ee7f31c0SAlexander Motin 		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1328ee7f31c0SAlexander Motin 
1329ee7f31c0SAlexander Motin 		DPRINTF("segment %d addr %p len %zd\n", i,
1330ee7f31c0SAlexander Motin 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1331ee7f31c0SAlexander Motin 
1332ee7f31c0SAlexander Motin 		beio->num_segs++;
1333ee7f31c0SAlexander Motin 		len_left -= seglen;
1334ee7f31c0SAlexander Motin 
1335ee7f31c0SAlexander Motin 		buf = beio->sg_segs[i].addr;
1336ee7f31c0SAlexander Motin 		end = buf + seglen;
13370bcd4ab6SAlexander Motin 		for (; buf < end; buf += cbe_lun->blocksize) {
13386c2acea5SAlexander Motin 			if (lbalen->flags & SWS_NDOB) {
13396c2acea5SAlexander Motin 				memset(buf, 0, cbe_lun->blocksize);
13406c2acea5SAlexander Motin 			} else {
13416c2acea5SAlexander Motin 				memcpy(buf, io->scsiio.kern_data_ptr,
13426c2acea5SAlexander Motin 				    cbe_lun->blocksize);
13436c2acea5SAlexander Motin 			}
134466df9136SAlexander Motin 			if (lbalen->flags & SWS_LBDATA)
134566df9136SAlexander Motin 				scsi_ulto4b(lbalen->lba + lba, buf);
1346ee7f31c0SAlexander Motin 			lba++;
1347ee7f31c0SAlexander Motin 		}
1348ee7f31c0SAlexander Motin 	}
1349ee7f31c0SAlexander Motin 
13500bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13510bcd4ab6SAlexander Motin 	beio->io_len = lba * cbe_lun->blocksize;
1352ee7f31c0SAlexander Motin 
1353ee7f31c0SAlexander Motin 	/* We can not do all in one run. Correct and schedule rerun. */
1354ee7f31c0SAlexander Motin 	if (len_left > 0) {
135566df9136SAlexander Motin 		lbalen->lba += lba;
135666df9136SAlexander Motin 		lbalen->len -= lba;
1357ee7f31c0SAlexander Motin 		beio->beio_cont = ctl_be_block_cw_done_ws;
1358ee7f31c0SAlexander Motin 	}
1359ee7f31c0SAlexander Motin 
1360ee7f31c0SAlexander Motin 	be_lun->dispatch(be_lun, beio);
1361ee7f31c0SAlexander Motin }
1362ee7f31c0SAlexander Motin 
1363ee7f31c0SAlexander Motin static void
1364ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1365ee7f31c0SAlexander Motin 			    union ctl_io *io)
1366ee7f31c0SAlexander Motin {
1367ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
136866df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1369ee7f31c0SAlexander Motin 
1370ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1371ee7f31c0SAlexander Motin 
1372e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
137366df9136SAlexander Motin 	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1374ee7f31c0SAlexander Motin 
13753406a2a0SAlexander Motin 	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1376ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1377ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1378ee7f31c0SAlexander Motin 				      /*sks_valid*/ 0,
1379ee7f31c0SAlexander Motin 				      /*command*/ 1,
1380ee7f31c0SAlexander Motin 				      /*field*/ 0,
1381ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1382ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1383ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1384ee7f31c0SAlexander Motin 		return;
1385ee7f31c0SAlexander Motin 	}
1386ee7f31c0SAlexander Motin 
1387ee7f31c0SAlexander Motin 	beio->io_len = 0;
1388ee7f31c0SAlexander Motin 	beio->io_offset = -1;
1389ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_DELETE;
1390ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_FREE;
139166df9136SAlexander Motin 	DPRINTF("UNMAP\n");
1392ee7f31c0SAlexander Motin 	be_lun->unmap(be_lun, beio);
1393ee7f31c0SAlexander Motin }
1394ee7f31c0SAlexander Motin 
1395ee7f31c0SAlexander Motin static void
1396ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1397ef8daf3fSAlexander Motin {
1398ef8daf3fSAlexander Motin 	union ctl_io *io;
1399ef8daf3fSAlexander Motin 
1400ef8daf3fSAlexander Motin 	io = beio->io;
1401ef8daf3fSAlexander Motin 	ctl_free_beio(beio);
1402ef8daf3fSAlexander Motin 	ctl_config_read_done(io);
1403ef8daf3fSAlexander Motin }
1404ef8daf3fSAlexander Motin 
1405ef8daf3fSAlexander Motin static void
1406ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1407ef8daf3fSAlexander Motin 			 union ctl_io *io)
1408ef8daf3fSAlexander Motin {
1409ef8daf3fSAlexander Motin 	struct ctl_be_block_io *beio;
1410ef8daf3fSAlexander Motin 	struct ctl_be_block_softc *softc;
1411ef8daf3fSAlexander Motin 
1412ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1413ef8daf3fSAlexander Motin 
1414ef8daf3fSAlexander Motin 	softc = be_lun->softc;
1415ef8daf3fSAlexander Motin 	beio = ctl_alloc_beio(softc);
1416ef8daf3fSAlexander Motin 	beio->io = io;
1417ef8daf3fSAlexander Motin 	beio->lun = be_lun;
1418ef8daf3fSAlexander Motin 	beio->beio_cont = ctl_be_block_cr_done;
1419ef8daf3fSAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1420ef8daf3fSAlexander Motin 
1421ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
1422ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1423ef8daf3fSAlexander Motin 		beio->bio_cmd = -1;
1424ef8daf3fSAlexander Motin 		beio->ds_trans_type = DEVSTAT_NO_DATA;
1425ef8daf3fSAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1426ef8daf3fSAlexander Motin 		beio->io_len = 0;
1427ef8daf3fSAlexander Motin 		if (be_lun->get_lba_status)
1428ef8daf3fSAlexander Motin 			be_lun->get_lba_status(be_lun, beio);
1429ef8daf3fSAlexander Motin 		else
1430ef8daf3fSAlexander Motin 			ctl_be_block_cr_done(beio);
1431ef8daf3fSAlexander Motin 		break;
1432ef8daf3fSAlexander Motin 	default:
1433ef8daf3fSAlexander Motin 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1434ef8daf3fSAlexander Motin 		break;
1435ef8daf3fSAlexander Motin 	}
1436ef8daf3fSAlexander Motin }
1437ef8daf3fSAlexander Motin 
1438ef8daf3fSAlexander Motin static void
1439ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1440ee7f31c0SAlexander Motin {
1441ee7f31c0SAlexander Motin 	union ctl_io *io;
1442ee7f31c0SAlexander Motin 
1443ee7f31c0SAlexander Motin 	io = beio->io;
1444ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1445ee7f31c0SAlexander Motin 	ctl_config_write_done(io);
1446ee7f31c0SAlexander Motin }
1447ee7f31c0SAlexander Motin 
1448ee7f31c0SAlexander Motin static void
1449130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1450130f4520SKenneth D. Merry 			 union ctl_io *io)
1451130f4520SKenneth D. Merry {
1452130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1453130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1454130f4520SKenneth D. Merry 
1455130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1456130f4520SKenneth D. Merry 
1457130f4520SKenneth D. Merry 	softc = be_lun->softc;
1458130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1459130f4520SKenneth D. Merry 	beio->io = io;
1460130f4520SKenneth D. Merry 	beio->lun = be_lun;
1461ee7f31c0SAlexander Motin 	beio->beio_cont = ctl_be_block_cw_done;
14627d0d4342SAlexander Motin 	switch (io->scsiio.tag_type) {
14637d0d4342SAlexander Motin 	case CTL_TAG_ORDERED:
14647d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
14657d0d4342SAlexander Motin 		break;
14667d0d4342SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
14677d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
14687d0d4342SAlexander Motin 		break;
14697d0d4342SAlexander Motin 	case CTL_TAG_UNTAGGED:
14707d0d4342SAlexander Motin 	case CTL_TAG_SIMPLE:
14717d0d4342SAlexander Motin 	case CTL_TAG_ACA:
14727d0d4342SAlexander Motin 	default:
14737d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
14747d0d4342SAlexander Motin 		break;
14757d0d4342SAlexander Motin 	}
1476e86a4142SAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1477130f4520SKenneth D. Merry 
1478130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
1479130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
1480130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
14817d0d4342SAlexander Motin 		ctl_be_block_cw_dispatch_sync(be_lun, io);
1482130f4520SKenneth D. Merry 		break;
1483ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
1484ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
1485ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_ws(be_lun, io);
1486ee7f31c0SAlexander Motin 		break;
1487ee7f31c0SAlexander Motin 	case UNMAP:
1488ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1489ee7f31c0SAlexander Motin 		break;
1490130f4520SKenneth D. Merry 	default:
1491130f4520SKenneth D. Merry 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1492130f4520SKenneth D. Merry 		break;
1493130f4520SKenneth D. Merry 	}
1494130f4520SKenneth D. Merry }
1495130f4520SKenneth D. Merry 
149636160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, start, "uint64_t");
149736160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, start, "uint64_t");
149836160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , read, alloc_done, "uint64_t");
149936160958SMark Johnston SDT_PROBE_DEFINE1(cbb, , write, alloc_done, "uint64_t");
1500130f4520SKenneth D. Merry 
1501130f4520SKenneth D. Merry static void
150208a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio)
150308a7cce5SAlexander Motin {
150408a7cce5SAlexander Motin 	struct ctl_be_block_lun *be_lun;
150508a7cce5SAlexander Motin 	union ctl_io *io;
150608a7cce5SAlexander Motin 
150708a7cce5SAlexander Motin 	io = beio->io;
150808a7cce5SAlexander Motin 	be_lun = beio->lun;
150908a7cce5SAlexander Motin 	ctl_free_beio(beio);
1510ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1511ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1512ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
151311b569f7SAlexander Motin 		ctl_data_submit_done(io);
151408a7cce5SAlexander Motin 		return;
151508a7cce5SAlexander Motin 	}
151608a7cce5SAlexander Motin 
151708a7cce5SAlexander Motin 	io->io_hdr.status &= ~CTL_STATUS_MASK;
151808a7cce5SAlexander Motin 	io->io_hdr.status |= CTL_STATUS_NONE;
151908a7cce5SAlexander Motin 
152075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
152108a7cce5SAlexander Motin 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
152275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
152308a7cce5SAlexander Motin 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
152408a7cce5SAlexander Motin }
152508a7cce5SAlexander Motin 
152608a7cce5SAlexander Motin static void
1527130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1528130f4520SKenneth D. Merry 			   union ctl_io *io)
1529130f4520SKenneth D. Merry {
15300bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1531130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1532130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
153311b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1534e86a4142SAlexander Motin 	struct ctl_ptr_len_flags *bptrlen;
1535e86a4142SAlexander Motin 	uint64_t len_left, lbas;
1536130f4520SKenneth D. Merry 	int i;
1537130f4520SKenneth D. Merry 
1538130f4520SKenneth D. Merry 	softc = be_lun->softc;
1539130f4520SKenneth D. Merry 
1540130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1541130f4520SKenneth D. Merry 
154211b569f7SAlexander Motin 	lbalen = ARGS(io);
154311b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
154436160958SMark Johnston 		SDT_PROBE0(cbb, , write, start);
154511b569f7SAlexander Motin 	} else {
154636160958SMark Johnston 		SDT_PROBE0(cbb, , read, start);
1547130f4520SKenneth D. Merry 	}
1548130f4520SKenneth D. Merry 
1549130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1550130f4520SKenneth D. Merry 	beio->io = io;
1551130f4520SKenneth D. Merry 	beio->lun = be_lun;
1552e86a4142SAlexander Motin 	bptrlen = PRIV(io);
1553e86a4142SAlexander Motin 	bptrlen->ptr = (void *)beio;
1554130f4520SKenneth D. Merry 
1555130f4520SKenneth D. Merry 	switch (io->scsiio.tag_type) {
1556130f4520SKenneth D. Merry 	case CTL_TAG_ORDERED:
1557130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1558130f4520SKenneth D. Merry 		break;
1559130f4520SKenneth D. Merry 	case CTL_TAG_HEAD_OF_QUEUE:
1560130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1561130f4520SKenneth D. Merry 		break;
1562130f4520SKenneth D. Merry 	case CTL_TAG_UNTAGGED:
1563130f4520SKenneth D. Merry 	case CTL_TAG_SIMPLE:
1564130f4520SKenneth D. Merry 	case CTL_TAG_ACA:
1565130f4520SKenneth D. Merry 	default:
1566130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1567130f4520SKenneth D. Merry 		break;
1568130f4520SKenneth D. Merry 	}
1569130f4520SKenneth D. Merry 
157011b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1571130f4520SKenneth D. Merry 		beio->bio_cmd = BIO_WRITE;
1572130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_WRITE;
157311b569f7SAlexander Motin 	} else {
157411b569f7SAlexander Motin 		beio->bio_cmd = BIO_READ;
157511b569f7SAlexander Motin 		beio->ds_trans_type = DEVSTAT_READ;
1576130f4520SKenneth D. Merry 	}
1577130f4520SKenneth D. Merry 
157808a7cce5SAlexander Motin 	DPRINTF("%s at LBA %jx len %u @%ju\n",
1579130f4520SKenneth D. Merry 	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1580e86a4142SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
158111b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE)
158211b569f7SAlexander Motin 		lbas = CTLBLK_HALF_IO_SIZE;
158311b569f7SAlexander Motin 	else
158411b569f7SAlexander Motin 		lbas = CTLBLK_MAX_IO_SIZE;
15850bcd4ab6SAlexander Motin 	lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
15860bcd4ab6SAlexander Motin 	beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
15870bcd4ab6SAlexander Motin 	beio->io_len = lbas * cbe_lun->blocksize;
1588e86a4142SAlexander Motin 	bptrlen->len += lbas;
1589130f4520SKenneth D. Merry 
159008a7cce5SAlexander Motin 	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
159108a7cce5SAlexander Motin 		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
159208a7cce5SAlexander Motin 		    i, CTLBLK_MAX_SEGS));
1593130f4520SKenneth D. Merry 
1594130f4520SKenneth D. Merry 		/*
1595130f4520SKenneth D. Merry 		 * Setup the S/G entry for this chunk.
1596130f4520SKenneth D. Merry 		 */
159708a7cce5SAlexander Motin 		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1598130f4520SKenneth D. Merry 		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1599130f4520SKenneth D. Merry 
1600130f4520SKenneth D. Merry 		DPRINTF("segment %d addr %p len %zd\n", i,
1601130f4520SKenneth D. Merry 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1602130f4520SKenneth D. Merry 
160311b569f7SAlexander Motin 		/* Set up second segment for compare operation. */
160411b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_COMPARE) {
160511b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
160611b569f7SAlexander Motin 			    beio->sg_segs[i].len;
160711b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
160811b569f7SAlexander Motin 			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
160911b569f7SAlexander Motin 		}
161011b569f7SAlexander Motin 
1611130f4520SKenneth D. Merry 		beio->num_segs++;
1612130f4520SKenneth D. Merry 		len_left -= beio->sg_segs[i].len;
1613130f4520SKenneth D. Merry 	}
1614e86a4142SAlexander Motin 	if (bptrlen->len < lbalen->len)
161508a7cce5SAlexander Motin 		beio->beio_cont = ctl_be_block_next;
161608a7cce5SAlexander Motin 	io->scsiio.be_move_done = ctl_be_block_move_done;
161711b569f7SAlexander Motin 	/* For compare we have separate S/G lists for read and datamove. */
161811b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE)
161911b569f7SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
162011b569f7SAlexander Motin 	else
162108a7cce5SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
162208a7cce5SAlexander Motin 	io->scsiio.kern_data_len = beio->io_len;
162308a7cce5SAlexander Motin 	io->scsiio.kern_sg_entries = beio->num_segs;
1624b2221369SAlexander Motin 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED;
1625130f4520SKenneth D. Merry 
1626130f4520SKenneth D. Merry 	/*
1627130f4520SKenneth D. Merry 	 * For the read case, we need to read the data into our buffers and
1628130f4520SKenneth D. Merry 	 * then we can send it back to the user.  For the write case, we
1629130f4520SKenneth D. Merry 	 * need to get the data from the user first.
1630130f4520SKenneth D. Merry 	 */
1631130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
163236160958SMark Johnston 		SDT_PROBE0(cbb, , read, alloc_done);
1633130f4520SKenneth D. Merry 		be_lun->dispatch(be_lun, beio);
1634130f4520SKenneth D. Merry 	} else {
163536160958SMark Johnston 		SDT_PROBE0(cbb, , write, alloc_done);
1636130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
1637e675024aSAlexander Motin 		getbinuptime(&io->io_hdr.dma_start_bt);
1638130f4520SKenneth D. Merry #endif
1639130f4520SKenneth D. Merry 		ctl_datamove(io);
1640130f4520SKenneth D. Merry 	}
1641130f4520SKenneth D. Merry }
1642130f4520SKenneth D. Merry 
1643130f4520SKenneth D. Merry static void
1644130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending)
1645130f4520SKenneth D. Merry {
1646ee4ad294SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1647ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1648130f4520SKenneth D. Merry 	union ctl_io *io;
1649130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1650130f4520SKenneth D. Merry 
1651ee4ad294SAlexander Motin 	DPRINTF("entered\n");
1652ee4ad294SAlexander Motin 	/*
1653ee4ad294SAlexander Motin 	 * Fetch and process I/Os from all queues.  If we detect LUN
1654648dfc1aSAlexander Motin 	 * CTL_LUN_FLAG_NO_MEDIA status here -- it is result of a race,
1655ee4ad294SAlexander Motin 	 * so make response maximally opaque to not confuse initiator.
1656ee4ad294SAlexander Motin 	 */
1657ee4ad294SAlexander Motin 	for (;;) {
1658ee4ad294SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
1659ee4ad294SAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1660ee4ad294SAlexander Motin 		if (io != NULL) {
1661130f4520SKenneth D. Merry 			DPRINTF("datamove queue\n");
1662130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1663130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
166475c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1665e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1666648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1667ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1668ee4ad294SAlexander Motin 				ctl_complete_beio(beio);
1669ee4ad294SAlexander Motin 				return;
1670ee4ad294SAlexander Motin 			}
1671130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1672130f4520SKenneth D. Merry 			continue;
1673130f4520SKenneth D. Merry 		}
1674130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1675130f4520SKenneth D. Merry 		if (io != NULL) {
1676130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
1677130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1678130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
167975c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1680648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1681ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1682ee4ad294SAlexander Motin 				ctl_config_write_done(io);
1683ee4ad294SAlexander Motin 				return;
1684ee4ad294SAlexander Motin 			}
1685130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1686ef8daf3fSAlexander Motin 			continue;
1687ef8daf3fSAlexander Motin 		}
1688ef8daf3fSAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1689ef8daf3fSAlexander Motin 		if (io != NULL) {
1690ef8daf3fSAlexander Motin 			DPRINTF("config read queue\n");
1691ef8daf3fSAlexander Motin 			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1692ef8daf3fSAlexander Motin 				      ctl_io_hdr, links);
1693ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1694648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1695ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1696ee4ad294SAlexander Motin 				ctl_config_read_done(io);
1697ee4ad294SAlexander Motin 				return;
1698ee4ad294SAlexander Motin 			}
1699ef8daf3fSAlexander Motin 			ctl_be_block_cr_dispatch(be_lun, io);
1700130f4520SKenneth D. Merry 			continue;
1701130f4520SKenneth D. Merry 		}
1702130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1703130f4520SKenneth D. Merry 		if (io != NULL) {
1704130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
1705130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1706130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
170775c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1708648dfc1aSAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) {
1709ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1710ee4ad294SAlexander Motin 				ctl_data_submit_done(io);
1711ee4ad294SAlexander Motin 				return;
1712ee4ad294SAlexander Motin 			}
1713130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1714130f4520SKenneth D. Merry 			continue;
1715130f4520SKenneth D. Merry 		}
1716130f4520SKenneth D. Merry 
1717130f4520SKenneth D. Merry 		/*
1718130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1719130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1720130f4520SKenneth D. Merry 		 */
1721ee4ad294SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
1722130f4520SKenneth D. Merry 		break;
1723130f4520SKenneth D. Merry 	}
1724130f4520SKenneth D. Merry }
1725130f4520SKenneth D. Merry 
1726130f4520SKenneth D. Merry /*
1727130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1728130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1729130f4520SKenneth D. Merry  * thread.
1730130f4520SKenneth D. Merry  */
1731130f4520SKenneth D. Merry static int
1732130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1733130f4520SKenneth D. Merry {
1734130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
17350bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1736130f4520SKenneth D. Merry 
1737130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1738130f4520SKenneth D. Merry 
17399cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
17400bcd4ab6SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
1741130f4520SKenneth D. Merry 
1742130f4520SKenneth D. Merry 	/*
1743130f4520SKenneth D. Merry 	 * Make sure we only get SCSI I/O.
1744130f4520SKenneth D. Merry 	 */
1745130f4520SKenneth D. Merry 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1746130f4520SKenneth D. Merry 		"%#x) encountered", io->io_hdr.io_type));
1747130f4520SKenneth D. Merry 
1748e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1749e86a4142SAlexander Motin 
175075c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
1751130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
175275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
1753130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1754130f4520SKenneth D. Merry 
17559c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1756130f4520SKenneth D. Merry }
1757130f4520SKenneth D. Merry 
1758130f4520SKenneth D. Merry static int
1759130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1760130f4520SKenneth D. Merry 			int flag, struct thread *td)
1761130f4520SKenneth D. Merry {
1762*34144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
1763130f4520SKenneth D. Merry 	int error;
1764130f4520SKenneth D. Merry 
1765130f4520SKenneth D. Merry 	error = 0;
1766130f4520SKenneth D. Merry 	switch (cmd) {
1767130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1768130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1769130f4520SKenneth D. Merry 
1770130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1771130f4520SKenneth D. Merry 
1772130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1773130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1774130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1775130f4520SKenneth D. Merry 			break;
1776130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1777130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1778130f4520SKenneth D. Merry 			break;
177981177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
178081177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
178181177295SEdward Tomasz Napierala 			break;
1782130f4520SKenneth D. Merry 		default:
1783130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1784130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
178519720f41SAlexander Motin 				 "invalid LUN request type %d",
1786130f4520SKenneth D. Merry 				 lun_req->reqtype);
1787130f4520SKenneth D. Merry 			break;
1788130f4520SKenneth D. Merry 		}
1789130f4520SKenneth D. Merry 		break;
1790130f4520SKenneth D. Merry 	}
1791130f4520SKenneth D. Merry 	default:
1792130f4520SKenneth D. Merry 		error = ENOTTY;
1793130f4520SKenneth D. Merry 		break;
1794130f4520SKenneth D. Merry 	}
1795130f4520SKenneth D. Merry 
1796130f4520SKenneth D. Merry 	return (error);
1797130f4520SKenneth D. Merry }
1798130f4520SKenneth D. Merry 
1799130f4520SKenneth D. Merry static int
1800130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1801130f4520SKenneth D. Merry {
18020bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1803130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1804130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
18058951f055SMarcelo Araujo 	const char		     *value;
1806130f4520SKenneth D. Merry 	struct vattr		      vattr;
180734961f40SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1808130f4520SKenneth D. Merry 	int			      error;
1809130f4520SKenneth D. Merry 
18100bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
1811130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
181219720f41SAlexander Motin 	params = &be_lun->params;
1813130f4520SKenneth D. Merry 
1814130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1815130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1816130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1817ef8daf3fSAlexander Motin 	be_lun->get_lba_status = ctl_be_block_gls_file;
181853c146deSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_file;
18190bcd4ab6SAlexander Motin 	be_lun->unmap = NULL;
18200bcd4ab6SAlexander Motin 	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1821130f4520SKenneth D. Merry 
1822130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1823130f4520SKenneth D. Merry 	if (error != 0) {
1824130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1825130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1826130f4520SKenneth D. Merry 			 be_lun->dev_path);
1827130f4520SKenneth D. Merry 		return (error);
1828130f4520SKenneth D. Merry 	}
1829130f4520SKenneth D. Merry 
1830130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
183181177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
183281177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
183381177295SEdward Tomasz Napierala 	else
1834130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1835130f4520SKenneth D. Merry 
1836130f4520SKenneth D. Merry 	/*
183720a28d6cSAlexander Motin 	 * For files we can use any logical block size.  Prefer 512 bytes
183820a28d6cSAlexander Motin 	 * for compatibility reasons.  If file's vattr.va_blocksize
183920a28d6cSAlexander Motin 	 * (preferred I/O block size) is bigger and multiple to chosen
184020a28d6cSAlexander Motin 	 * logical block size -- report it as physical block size.
1841130f4520SKenneth D. Merry 	 */
1842130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
18430bcd4ab6SAlexander Motin 		cbe_lun->blocksize = params->blocksize_bytes;
184491be33dcSAlexander Motin 	else if (cbe_lun->lun_type == T_CDROM)
184591be33dcSAlexander Motin 		cbe_lun->blocksize = 2048;
1846130f4520SKenneth D. Merry 	else
18470bcd4ab6SAlexander Motin 		cbe_lun->blocksize = 512;
18480bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
18490bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
18500bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
185134961f40SAlexander Motin 
185234961f40SAlexander Motin 	us = ps = vattr.va_blocksize;
185334961f40SAlexander Motin 	uo = po = 0;
185434961f40SAlexander Motin 
18558951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
185634961f40SAlexander Motin 	if (value != NULL)
185734961f40SAlexander Motin 		ctl_expand_number(value, &ps);
18588951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
185934961f40SAlexander Motin 	if (value != NULL)
186034961f40SAlexander Motin 		ctl_expand_number(value, &po);
18610bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
18620bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
18630bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
18640bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
18650bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
18660bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
186734961f40SAlexander Motin 	}
186834961f40SAlexander Motin 
18698951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
187034961f40SAlexander Motin 	if (value != NULL)
187134961f40SAlexander Motin 		ctl_expand_number(value, &us);
18728951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
187334961f40SAlexander Motin 	if (value != NULL)
187434961f40SAlexander Motin 		ctl_expand_number(value, &uo);
18750bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
18760bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
18770bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
18780bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
18790bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
18800bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
188120a28d6cSAlexander Motin 	}
1882130f4520SKenneth D. Merry 
1883130f4520SKenneth D. Merry 	/*
1884130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1885130f4520SKenneth D. Merry 	 * sector long.
1886130f4520SKenneth D. Merry 	 */
18870bcd4ab6SAlexander Motin 	if (be_lun->size_bytes < cbe_lun->blocksize) {
1888130f4520SKenneth D. Merry 		error = EINVAL;
1889130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1890130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
18910bcd4ab6SAlexander Motin 			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1892130f4520SKenneth D. Merry 	}
1893cb8727e2SAlexander Motin 
18940bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1895130f4520SKenneth D. Merry 	return (error);
1896130f4520SKenneth D. Merry }
1897130f4520SKenneth D. Merry 
1898130f4520SKenneth D. Merry static int
1899130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1900130f4520SKenneth D. Merry {
19010bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1902130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
19033236151eSAlexander Motin 	struct cdevsw		     *csw;
1904130f4520SKenneth D. Merry 	struct cdev		     *dev;
19058951f055SMarcelo Araujo 	const char		     *value;
19063236151eSAlexander Motin 	int			      error, atomic, maxio, ref, unmap, tmp;
1907f6295033SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1908130f4520SKenneth D. Merry 
190919720f41SAlexander Motin 	params = &be_lun->params;
1910130f4520SKenneth D. Merry 
1911130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
19123236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
19133236151eSAlexander Motin 	if (csw == NULL)
19143236151eSAlexander Motin 		return (ENXIO);
19153236151eSAlexander Motin 	if (strcmp(csw->d_name, "zvol") == 0) {
191667f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1917ef8daf3fSAlexander Motin 		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1918cb8727e2SAlexander Motin 		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1919cb8727e2SAlexander Motin 	} else {
192067f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_dev;
19210bcd4ab6SAlexander Motin 		be_lun->get_lba_status = NULL;
1922cb8727e2SAlexander Motin 		atomic = 0;
19233236151eSAlexander Motin 		maxio = dev->si_iosize_max;
1924cb8727e2SAlexander Motin 		if (maxio <= 0)
1925cb8727e2SAlexander Motin 			maxio = DFLTPHYS;
1926cb8727e2SAlexander Motin 		if (maxio > CTLBLK_MAX_IO_SIZE)
1927cb8727e2SAlexander Motin 			maxio = CTLBLK_MAX_IO_SIZE;
1928cb8727e2SAlexander Motin 	}
192967f586a8SAlexander Motin 	be_lun->lun_flush = ctl_be_block_flush_dev;
1930c3e7ba3eSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_dev;
19310bcd4ab6SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
1932130f4520SKenneth D. Merry 
19333236151eSAlexander Motin 	if (!csw->d_ioctl) {
19343236151eSAlexander Motin 		dev_relthread(dev, ref);
1935130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
19363236151eSAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
1937130f4520SKenneth D. Merry 		return (ENODEV);
1938130f4520SKenneth D. Merry 	}
1939130f4520SKenneth D. Merry 
19403236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1941130f4520SKenneth D. Merry 			       curthread);
1942130f4520SKenneth D. Merry 	if (error) {
19433236151eSAlexander Motin 		dev_relthread(dev, ref);
1944130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
194519720f41SAlexander Motin 			 "error %d returned for DIOCGSECTORSIZE ioctl "
194619720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
1947130f4520SKenneth D. Merry 		return (error);
1948130f4520SKenneth D. Merry 	}
1949130f4520SKenneth D. Merry 
1950130f4520SKenneth D. Merry 	/*
1951130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
1952130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
1953130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
1954130f4520SKenneth D. Merry 	 * device's blocksize.
1955130f4520SKenneth D. Merry 	 */
1956a15bbf15SAlexander Motin 	if ((params->blocksize_bytes != 0) &&
1957a15bbf15SAlexander Motin 	    (params->blocksize_bytes >= tmp)) {
1958a15bbf15SAlexander Motin 		if (params->blocksize_bytes % tmp == 0) {
19590bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
1960130f4520SKenneth D. Merry 		} else {
19613236151eSAlexander Motin 			dev_relthread(dev, ref);
1962130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
196319720f41SAlexander Motin 				 "requested blocksize %u is not an even "
1964130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
1965f6295033SAlexander Motin 				 params->blocksize_bytes, tmp);
1966130f4520SKenneth D. Merry 			return (EINVAL);
1967130f4520SKenneth D. Merry 		}
1968a15bbf15SAlexander Motin 	} else if (params->blocksize_bytes != 0) {
19693236151eSAlexander Motin 		dev_relthread(dev, ref);
1970130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
197119720f41SAlexander Motin 			 "requested blocksize %u < backing device "
1972f6295033SAlexander Motin 			 "blocksize %u", params->blocksize_bytes, tmp);
1973130f4520SKenneth D. Merry 		return (EINVAL);
197491be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_CDROM)
197591be33dcSAlexander Motin 		cbe_lun->blocksize = MAX(tmp, 2048);
197691be33dcSAlexander Motin 	else
19770bcd4ab6SAlexander Motin 		cbe_lun->blocksize = tmp;
1978130f4520SKenneth D. Merry 
19793236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
1980130f4520SKenneth D. Merry 			     curthread);
1981130f4520SKenneth D. Merry 	if (error) {
19823236151eSAlexander Motin 		dev_relthread(dev, ref);
1983130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
198419720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE "
198519720f41SAlexander Motin 			 " ioctl on %s!", error,
198681177295SEdward Tomasz Napierala 			 be_lun->dev_path);
1987130f4520SKenneth D. Merry 		return (error);
1988130f4520SKenneth D. Merry 	}
1989130f4520SKenneth D. Merry 
199081177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
1991f6295033SAlexander Motin 		if (params->lun_size_bytes > otmp) {
19923236151eSAlexander Motin 			dev_relthread(dev, ref);
199381177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
199419720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
199519720f41SAlexander Motin 				 "size %ju",
199681177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
1997f6295033SAlexander Motin 				 (uintmax_t)otmp);
199881177295SEdward Tomasz Napierala 			return (EINVAL);
1999130f4520SKenneth D. Merry 		}
2000130f4520SKenneth D. Merry 
200181177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
2002a15bbf15SAlexander Motin 	} else
2003f6295033SAlexander Motin 		be_lun->size_bytes = otmp;
20040bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
20050bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
20060bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
200781177295SEdward Tomasz Napierala 
20083236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
20093236151eSAlexander Motin 	    curthread);
2010f6012722SAlexander Motin 	if (error)
2011f6012722SAlexander Motin 		ps = po = 0;
2012f6012722SAlexander Motin 	else {
20133236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
20143236151eSAlexander Motin 		    FREAD, curthread);
2015f6012722SAlexander Motin 		if (error)
2016f6012722SAlexander Motin 			po = 0;
2017f6012722SAlexander Motin 	}
201834961f40SAlexander Motin 	us = ps;
201934961f40SAlexander Motin 	uo = po;
202034961f40SAlexander Motin 
20218951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblocksize", NULL);
202234961f40SAlexander Motin 	if (value != NULL)
202334961f40SAlexander Motin 		ctl_expand_number(value, &ps);
20248951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "pblockoffset", NULL);
202534961f40SAlexander Motin 	if (value != NULL)
202634961f40SAlexander Motin 		ctl_expand_number(value, &po);
20270bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
20280bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
20290bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
20300bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
20310bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
20320bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
2033f6012722SAlexander Motin 	}
2034f6012722SAlexander Motin 
20358951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublocksize", NULL);
203634961f40SAlexander Motin 	if (value != NULL)
203734961f40SAlexander Motin 		ctl_expand_number(value, &us);
20388951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ublockoffset", NULL);
203934961f40SAlexander Motin 	if (value != NULL)
204034961f40SAlexander Motin 		ctl_expand_number(value, &uo);
20410bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
20420bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
20430bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
20440bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
20450bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
20460bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
204734961f40SAlexander Motin 	}
204834961f40SAlexander Motin 
20490bcd4ab6SAlexander Motin 	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
20500bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2051fbc8d4ffSAlexander Motin 
2052fbc8d4ffSAlexander Motin 	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2053fbc8d4ffSAlexander Motin 		unmap = 1;
2054fbc8d4ffSAlexander Motin 	} else {
2055fbc8d4ffSAlexander Motin 		struct diocgattr_arg	arg;
2056fbc8d4ffSAlexander Motin 
2057fbc8d4ffSAlexander Motin 		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2058fbc8d4ffSAlexander Motin 		arg.len = sizeof(arg.value.i);
20593236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
20603236151eSAlexander Motin 		    curthread);
2061fbc8d4ffSAlexander Motin 		unmap = (error == 0) ? arg.value.i : 0;
2062fbc8d4ffSAlexander Motin 	}
20638951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "unmap", NULL);
2064fbc8d4ffSAlexander Motin 	if (value != NULL)
2065fbc8d4ffSAlexander Motin 		unmap = (strcmp(value, "on") == 0);
2066fbc8d4ffSAlexander Motin 	if (unmap)
20670bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
20680bcd4ab6SAlexander Motin 	else
20690bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2070fbc8d4ffSAlexander Motin 
20713236151eSAlexander Motin 	dev_relthread(dev, ref);
207281177295SEdward Tomasz Napierala 	return (0);
207381177295SEdward Tomasz Napierala }
2074130f4520SKenneth D. Merry 
2075130f4520SKenneth D. Merry static int
2076130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2077130f4520SKenneth D. Merry {
20780bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
20790bcd4ab6SAlexander Motin 	int flags;
2080130f4520SKenneth D. Merry 
20810bcd4ab6SAlexander Motin 	if (be_lun->vn) {
20820bcd4ab6SAlexander Motin 		flags = FREAD;
20830bcd4ab6SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
20840bcd4ab6SAlexander Motin 			flags |= FWRITE;
2085130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2086130f4520SKenneth D. Merry 		be_lun->vn = NULL;
2087130f4520SKenneth D. Merry 
2088130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
2089130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
2090130f4520SKenneth D. Merry 			break;
2091130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
2092130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
2093130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
2094130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
2095130f4520SKenneth D. Merry 			}
2096130f4520SKenneth D. Merry 			break;
2097130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
2098025a2301SEdward Tomasz Napierala 			break;
2099130f4520SKenneth D. Merry 		default:
21005124012aSAlexander Motin 			panic("Unexpected backend type %d", be_lun->dev_type);
2101130f4520SKenneth D. Merry 			break;
2102130f4520SKenneth D. Merry 		}
210319720f41SAlexander Motin 		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2104130f4520SKenneth D. Merry 	}
2105130f4520SKenneth D. Merry 	return (0);
2106130f4520SKenneth D. Merry }
2107130f4520SKenneth D. Merry 
2108130f4520SKenneth D. Merry static int
2109648dfc1aSAlexander Motin ctl_be_block_open(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2110130f4520SKenneth D. Merry {
21110bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2112130f4520SKenneth D. Merry 	struct nameidata nd;
21138951f055SMarcelo Araujo 	const char	*value;
21140bcd4ab6SAlexander Motin 	int		 error, flags;
2115130f4520SKenneth D. Merry 
2116130f4520SKenneth D. Merry 	error = 0;
2117130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
2118130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
211919720f41SAlexander Motin 			 "Root filesystem is not mounted");
2120130f4520SKenneth D. Merry 		return (1);
2121130f4520SKenneth D. Merry 	}
21228a08cec1SMateusz Guzik 	pwd_ensure_dirs();
2123130f4520SKenneth D. Merry 
21248951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "file", NULL);
21250bcd4ab6SAlexander Motin 	if (value == NULL) {
21260bcd4ab6SAlexander Motin 		snprintf(req->error_str, sizeof(req->error_str),
21270bcd4ab6SAlexander Motin 			 "no file argument specified");
21280bcd4ab6SAlexander Motin 		return (1);
21290bcd4ab6SAlexander Motin 	}
21300bcd4ab6SAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
21310bcd4ab6SAlexander Motin 	be_lun->dev_path = strdup(value, M_CTLBLK);
21320bcd4ab6SAlexander Motin 
21330bcd4ab6SAlexander Motin 	flags = FREAD;
21348951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "readonly", NULL);
213591be33dcSAlexander Motin 	if (value != NULL) {
213691be33dcSAlexander Motin 		if (strcmp(value, "on") != 0)
213791be33dcSAlexander Motin 			flags |= FWRITE;
213891be33dcSAlexander Motin 	} else if (cbe_lun->lun_type == T_DIRECT)
21390bcd4ab6SAlexander Motin 		flags |= FWRITE;
21400bcd4ab6SAlexander Motin 
2141130f4520SKenneth D. Merry again:
2142130f4520SKenneth D. Merry 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2143130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
21440bcd4ab6SAlexander Motin 	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
21450bcd4ab6SAlexander Motin 		flags &= ~FWRITE;
21460bcd4ab6SAlexander Motin 		goto again;
21470bcd4ab6SAlexander Motin 	}
2148130f4520SKenneth D. Merry 	if (error) {
2149130f4520SKenneth D. Merry 		/*
2150130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
2151130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
2152130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
2153130f4520SKenneth D. Merry 		 * full path.
2154130f4520SKenneth D. Merry 		 */
2155130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
2156130f4520SKenneth D. Merry 			char *dev_name;
2157130f4520SKenneth D. Merry 
21580bcd4ab6SAlexander Motin 			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2159130f4520SKenneth D. Merry 				be_lun->dev_path);
2160130f4520SKenneth D. Merry 			free(be_lun->dev_path, M_CTLBLK);
2161130f4520SKenneth D. Merry 			be_lun->dev_path = dev_name;
2162130f4520SKenneth D. Merry 			goto again;
2163130f4520SKenneth D. Merry 		}
2164130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
216519720f41SAlexander Motin 		    "error opening %s: %d", be_lun->dev_path, error);
2166130f4520SKenneth D. Merry 		return (error);
2167130f4520SKenneth D. Merry 	}
21680bcd4ab6SAlexander Motin 	if (flags & FWRITE)
21690bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
21700bcd4ab6SAlexander Motin 	else
21710bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2172130f4520SKenneth D. Merry 
2173130f4520SKenneth D. Merry 	NDFREE(&nd, NDF_ONLY_PNBUF);
2174130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
2175130f4520SKenneth D. Merry 
2176130f4520SKenneth D. Merry 	/* We only support disks and files. */
2177130f4520SKenneth D. Merry 	if (vn_isdisk(be_lun->vn, &error)) {
2178130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
2179130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
2180130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
2181130f4520SKenneth D. Merry 	} else {
2182130f4520SKenneth D. Merry 		error = EINVAL;
2183130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2184025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
2185130f4520SKenneth D. Merry 	}
2186b249ce48SMateusz Guzik 	VOP_UNLOCK(be_lun->vn);
2187130f4520SKenneth D. Merry 
2188a15bbf15SAlexander Motin 	if (error != 0)
2189130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
21900bcd4ab6SAlexander Motin 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
21910bcd4ab6SAlexander Motin 	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
21920bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
21938951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "serseq", NULL);
21940bcd4ab6SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
21950bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
21960bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "read") == 0)
21970bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
21980bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "off") == 0)
21990bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2200130f4520SKenneth D. Merry 	return (0);
2201130f4520SKenneth D. Merry }
2202130f4520SKenneth D. Merry 
2203130f4520SKenneth D. Merry static int
2204130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2205130f4520SKenneth D. Merry {
22060bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2207130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2208130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
220957a5db13SAlexander Motin 	char num_thread_str[16];
2210130f4520SKenneth D. Merry 	char tmpstr[32];
22118951f055SMarcelo Araujo 	const char *value;
2212fbc8d4ffSAlexander Motin 	int retval, num_threads;
221357a5db13SAlexander Motin 	int tmp_num_threads;
2214130f4520SKenneth D. Merry 
2215130f4520SKenneth D. Merry 	params = &req->reqdata.create;
2216130f4520SKenneth D. Merry 	retval = 0;
221719720f41SAlexander Motin 	req->status = CTL_LUN_OK;
2218130f4520SKenneth D. Merry 
2219130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
22200bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
22210bcd4ab6SAlexander Motin 	cbe_lun->be_lun = be_lun;
222219720f41SAlexander Motin 	be_lun->params = req->reqdata.create;
2223130f4520SKenneth D. Merry 	be_lun->softc = softc;
2224130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
2225ef8daf3fSAlexander Motin 	STAILQ_INIT(&be_lun->config_read_queue);
2226130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
2227130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
2228*34144c2cSAlexander Motin 	mtx_init(&be_lun->io_lock, "ctlblock io", NULL, MTX_DEF);
2229*34144c2cSAlexander Motin 	mtx_init(&be_lun->queue_lock, "ctlblock queue", NULL, MTX_DEF);
22308951f055SMarcelo Araujo 	cbe_lun->options = nvlist_clone(req->args_nvl);
2231*34144c2cSAlexander Motin 	be_lun->lun_zone = uma_zcreate("ctlblock", CTLBLK_MAX_SEG,
2232eeb94054SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2233130f4520SKenneth D. Merry 	if (be_lun->lun_zone == NULL) {
2234130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
223519720f41SAlexander Motin 			 "error allocating UMA zone");
2236130f4520SKenneth D. Merry 		goto bailout_error;
2237130f4520SKenneth D. Merry 	}
2238130f4520SKenneth D. Merry 
2239130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
22400bcd4ab6SAlexander Motin 		cbe_lun->lun_type = params->device_type;
2241130f4520SKenneth D. Merry 	else
22420bcd4ab6SAlexander Motin 		cbe_lun->lun_type = T_DIRECT;
2243*34144c2cSAlexander Motin 	be_lun->flags = 0;
22447ac58230SAlexander Motin 	cbe_lun->flags = 0;
22458951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
22467ac58230SAlexander Motin 	if (value != NULL) {
22477ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
22487ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
22497ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
22507ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2251130f4520SKenneth D. Merry 
225291be33dcSAlexander Motin 	if (cbe_lun->lun_type == T_DIRECT ||
225391be33dcSAlexander Motin 	    cbe_lun->lun_type == T_CDROM) {
2254a15bbf15SAlexander Motin 		be_lun->size_bytes = params->lun_size_bytes;
2255a15bbf15SAlexander Motin 		if (params->blocksize_bytes != 0)
22560bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
225791be33dcSAlexander Motin 		else if (cbe_lun->lun_type == T_CDROM)
225891be33dcSAlexander Motin 			cbe_lun->blocksize = 2048;
2259a15bbf15SAlexander Motin 		else
22600bcd4ab6SAlexander Motin 			cbe_lun->blocksize = 512;
22610bcd4ab6SAlexander Motin 		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
22620bcd4ab6SAlexander Motin 		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
22630bcd4ab6SAlexander Motin 		    0 : (be_lun->size_blocks - 1);
2264130f4520SKenneth D. Merry 
22657ac58230SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
22667ac58230SAlexander Motin 		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2267648dfc1aSAlexander Motin 			retval = ctl_be_block_open(be_lun, req);
2268130f4520SKenneth D. Merry 			if (retval != 0) {
2269130f4520SKenneth D. Merry 				retval = 0;
227019720f41SAlexander Motin 				req->status = CTL_LUN_WARNING;
2271130f4520SKenneth D. Merry 			}
22727ac58230SAlexander Motin 		}
22730bcd4ab6SAlexander Motin 		num_threads = cbb_num_threads;
2274130f4520SKenneth D. Merry 	} else {
2275130f4520SKenneth D. Merry 		num_threads = 1;
2276130f4520SKenneth D. Merry 	}
2277130f4520SKenneth D. Merry 
22788951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "num_threads", NULL);
227957a5db13SAlexander Motin 	if (value != NULL) {
228057a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
2281130f4520SKenneth D. Merry 
2282130f4520SKenneth D. Merry 		/*
2283130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
2284130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
2285130f4520SKenneth D. Merry 		 * specify 1000 threads.
2286130f4520SKenneth D. Merry 		 */
2287130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
2288130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
228919720f41SAlexander Motin 				 "invalid number of threads %s",
229019720f41SAlexander Motin 				 num_thread_str);
2291130f4520SKenneth D. Merry 			goto bailout_error;
2292130f4520SKenneth D. Merry 		}
2293130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
229457a5db13SAlexander Motin 	}
2295130f4520SKenneth D. Merry 
229619720f41SAlexander Motin 	if (be_lun->vn == NULL)
2297648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2298130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
229919720f41SAlexander Motin 	params->lun_size_bytes = be_lun->size_bytes;
23000bcd4ab6SAlexander Motin 	params->blocksize_bytes = cbe_lun->blocksize;
2301130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
23020bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = params->req_lun_id;
23030bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2304130f4520SKenneth D. Merry 	} else
23050bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = 0;
2306130f4520SKenneth D. Merry 
23070bcd4ab6SAlexander Motin 	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
23080bcd4ab6SAlexander Motin 	cbe_lun->be = &ctl_be_block_driver;
2309130f4520SKenneth D. Merry 
2310130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
231171cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%04d",
2312130f4520SKenneth D. Merry 			 softc->num_luns);
23130bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, tmpstr,
23140bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2315130f4520SKenneth D. Merry 
2316130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
2317130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
2318e7038eb7SAlexander Motin 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2319130f4520SKenneth D. Merry 	} else {
23200bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
23210bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num),
2322130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
2323130f4520SKenneth D. Merry 	}
2324130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
232571cd87c6SAlan Somers 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%04d", softc->num_luns);
23260bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, tmpstr,
23270bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2328130f4520SKenneth D. Merry 
2329130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2330130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2331e7038eb7SAlexander Motin 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2332130f4520SKenneth D. Merry 	} else {
23330bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, params->device_id,
23340bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id),
2335130f4520SKenneth D. Merry 			    sizeof(params->device_id)));
2336130f4520SKenneth D. Merry 	}
2337130f4520SKenneth D. Merry 
2338130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2339130f4520SKenneth D. Merry 
2340*34144c2cSAlexander Motin 	be_lun->io_taskqueue = taskqueue_create("ctlblocktq", M_WAITOK,
2341130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2342130f4520SKenneth D. Merry 
2343130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2344130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
234519720f41SAlexander Motin 			 "unable to create taskqueue");
2346130f4520SKenneth D. Merry 		goto bailout_error;
2347130f4520SKenneth D. Merry 	}
2348130f4520SKenneth D. Merry 
2349130f4520SKenneth D. Merry 	/*
2350130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2351130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2352130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2353130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2354130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2355130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2356130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2357130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2358130f4520SKenneth D. Merry 	 *
2359130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2360130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2361130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2362130f4520SKenneth D. Merry 	 */
236312373e95SAlexander Motin 	retval = taskqueue_start_threads_in_proc(&be_lun->io_taskqueue,
2364130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2365053db1feSAlexander Motin 					 /*priority*/PUSER,
236612373e95SAlexander Motin 					 /*proc*/control_softc->ctl_proc,
2367*34144c2cSAlexander Motin 					 /*thread name*/"block");
2368130f4520SKenneth D. Merry 
2369130f4520SKenneth D. Merry 	if (retval != 0)
2370130f4520SKenneth D. Merry 		goto bailout_error;
2371130f4520SKenneth D. Merry 
2372130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2373130f4520SKenneth D. Merry 
23740bcd4ab6SAlexander Motin 	retval = ctl_add_lun(&be_lun->cbe_lun);
2375130f4520SKenneth D. Merry 	if (retval != 0) {
2376130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
237719720f41SAlexander Motin 			 "ctl_add_lun() returned error %d, see dmesg for "
237819720f41SAlexander Motin 			 "details", retval);
2379130f4520SKenneth D. Merry 		retval = 0;
2380130f4520SKenneth D. Merry 		goto bailout_error;
2381130f4520SKenneth D. Merry 	}
2382130f4520SKenneth D. Merry 
2383*34144c2cSAlexander Motin 	be_lun->disk_stats = devstat_new_entry("cbb", cbe_lun->lun_id,
23840bcd4ab6SAlexander Motin 					       cbe_lun->blocksize,
2385130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
23860bcd4ab6SAlexander Motin 					       cbe_lun->lun_type
2387130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2388130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2389130f4520SKenneth D. Merry 
2390*34144c2cSAlexander Motin 	mtx_lock(&softc->lock);
2391*34144c2cSAlexander Motin 	softc->num_luns++;
2392*34144c2cSAlexander Motin 	SLIST_INSERT_HEAD(&softc->lun_list, be_lun, links);
2393*34144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
2394*34144c2cSAlexander Motin 
2395*34144c2cSAlexander Motin 	params->req_lun_id = cbe_lun->lun_id;
2396*34144c2cSAlexander Motin 
2397130f4520SKenneth D. Merry 	return (retval);
2398130f4520SKenneth D. Merry 
2399130f4520SKenneth D. Merry bailout_error:
2400130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2401130f4520SKenneth D. Merry 
24029e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
24039e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2404130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
24059e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2406130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
24079e005bbcSAlexander Motin 	if (be_lun->lun_zone != NULL)
24089e005bbcSAlexander Motin 		uma_zdestroy(be_lun->lun_zone);
24098951f055SMarcelo Araujo 	nvlist_destroy(cbe_lun->options);
241075c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
241175c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2412130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2413130f4520SKenneth D. Merry 
2414130f4520SKenneth D. Merry 	return (retval);
2415130f4520SKenneth D. Merry }
2416130f4520SKenneth D. Merry 
2417130f4520SKenneth D. Merry static int
2418130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2419130f4520SKenneth D. Merry {
2420130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2421130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2422ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2423130f4520SKenneth D. Merry 	int retval;
2424130f4520SKenneth D. Merry 
2425130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2426130f4520SKenneth D. Merry 
2427*34144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
2428130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2429*34144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
2430*34144c2cSAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id) {
2431*34144c2cSAlexander Motin 			SLIST_REMOVE(&softc->lun_list, be_lun,
2432*34144c2cSAlexander Motin 			    ctl_be_block_lun, links);
2433*34144c2cSAlexander Motin 			softc->num_luns--;
2434130f4520SKenneth D. Merry 			break;
2435130f4520SKenneth D. Merry 		}
2436*34144c2cSAlexander Motin 	}
2437130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2438*34144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
2439130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2440130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
244119720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
244219720f41SAlexander Motin 			 params->lun_id);
2443130f4520SKenneth D. Merry 		goto bailout_error;
2444130f4520SKenneth D. Merry 	}
2445ee4ad294SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
2446130f4520SKenneth D. Merry 
2447ee4ad294SAlexander Motin 	if (be_lun->vn != NULL) {
2448648dfc1aSAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2449648dfc1aSAlexander Motin 		ctl_lun_no_media(cbe_lun);
2450ee4ad294SAlexander Motin 		taskqueue_drain_all(be_lun->io_taskqueue);
2451ee4ad294SAlexander Motin 		ctl_be_block_close(be_lun);
2452ee4ad294SAlexander Motin 	}
2453ee4ad294SAlexander Motin 
2454*34144c2cSAlexander Motin 	mtx_lock(&softc->lock);
2455*34144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2456*34144c2cSAlexander Motin 	mtx_unlock(&softc->lock);
2457*34144c2cSAlexander Motin 
2458*34144c2cSAlexander Motin 	retval = ctl_remove_lun(cbe_lun);
2459130f4520SKenneth D. Merry 	if (retval != 0) {
2460130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2461*34144c2cSAlexander Motin 			 "error %d returned from ctl_remove_lun() for "
246219720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
2463*34144c2cSAlexander Motin 		mtx_lock(&softc->lock);
2464*34144c2cSAlexander Motin 		be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2465*34144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
2466130f4520SKenneth D. Merry 		goto bailout_error;
2467130f4520SKenneth D. Merry 	}
2468130f4520SKenneth D. Merry 
2469130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2470130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2471*34144c2cSAlexander Motin 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblockrm", 0);
2472130f4520SKenneth D. Merry 		if (retval == EINTR)
2473130f4520SKenneth D. Merry 			break;
2474130f4520SKenneth D. Merry 	}
2475130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2476*34144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2477130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2478130f4520SKenneth D. Merry 		free(be_lun, M_CTLBLK);
2479*34144c2cSAlexander Motin 	} else {
2480*34144c2cSAlexander Motin 		mtx_unlock(&softc->lock);
2481*34144c2cSAlexander Motin 		return (EINTR);
2482*34144c2cSAlexander Motin 	}
2483130f4520SKenneth D. Merry 
2484130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2485130f4520SKenneth D. Merry 	return (0);
2486130f4520SKenneth D. Merry 
2487130f4520SKenneth D. Merry bailout_error:
2488130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2489130f4520SKenneth D. Merry 	return (0);
2490130f4520SKenneth D. Merry }
2491130f4520SKenneth D. Merry 
249281177295SEdward Tomasz Napierala static int
249381177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
249481177295SEdward Tomasz Napierala {
249581177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
249681177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
2497a3977beaSAlexander Motin 	struct ctl_be_lun *cbe_lun;
24988951f055SMarcelo Araujo 	const char *value;
249971d8e97eSAlexander Motin 	uint64_t oldsize;
25007ac58230SAlexander Motin 	int error, wasprim;
250181177295SEdward Tomasz Napierala 
250281177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
250381177295SEdward Tomasz Napierala 
2504*34144c2cSAlexander Motin 	sx_xlock(&softc->modify_lock);
250581177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
2506*34144c2cSAlexander Motin 	SLIST_FOREACH(be_lun, &softc->lun_list, links) {
25070bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
250881177295SEdward Tomasz Napierala 			break;
250981177295SEdward Tomasz Napierala 	}
251081177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
251181177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
251281177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
251319720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
251419720f41SAlexander Motin 			 params->lun_id);
251581177295SEdward Tomasz Napierala 		goto bailout_error;
251681177295SEdward Tomasz Napierala 	}
2517a3977beaSAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
251881177295SEdward Tomasz Napierala 
2519a3977beaSAlexander Motin 	if (params->lun_size_bytes != 0)
252019720f41SAlexander Motin 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
25218951f055SMarcelo Araujo 
2522efeedddcSAlexander Motin 	if (req->args_nvl != NULL) {
25238951f055SMarcelo Araujo 		nvlist_destroy(cbe_lun->options);
25248951f055SMarcelo Araujo 		cbe_lun->options = nvlist_clone(req->args_nvl);
2525efeedddcSAlexander Motin 	}
252681177295SEdward Tomasz Napierala 
25277ac58230SAlexander Motin 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
25288951f055SMarcelo Araujo 	value = dnvlist_get_string(cbe_lun->options, "ha_role", NULL);
25297ac58230SAlexander Motin 	if (value != NULL) {
25307ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
25317ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25327ac58230SAlexander Motin 		else
25337ac58230SAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25347ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
25357ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
25367ac58230SAlexander Motin 	else
25377ac58230SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
25387ac58230SAlexander Motin 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
25397ac58230SAlexander Motin 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
25407ac58230SAlexander Motin 			ctl_lun_primary(cbe_lun);
25417ac58230SAlexander Motin 		else
25427ac58230SAlexander Motin 			ctl_lun_secondary(cbe_lun);
25437ac58230SAlexander Motin 	}
25447ac58230SAlexander Motin 
25450bcd4ab6SAlexander Motin 	oldsize = be_lun->size_blocks;
25467ac58230SAlexander Motin 	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
25477ac58230SAlexander Motin 	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
254819720f41SAlexander Motin 		if (be_lun->vn == NULL)
2549648dfc1aSAlexander Motin 			error = ctl_be_block_open(be_lun, req);
2550b9b4269cSAlexander Motin 		else if (vn_isdisk(be_lun->vn, &error))
25514ce7a086SAlexander Motin 			error = ctl_be_block_open_dev(be_lun, req);
25523d5cb709SAlexander Motin 		else if (be_lun->vn->v_type == VREG) {
25533d5cb709SAlexander Motin 			vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
25544ce7a086SAlexander Motin 			error = ctl_be_block_open_file(be_lun, req);
2555b249ce48SMateusz Guzik 			VOP_UNLOCK(be_lun->vn);
25563d5cb709SAlexander Motin 		} else
2557b9b4269cSAlexander Motin 			error = EINVAL;
2558648dfc1aSAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) &&
25590bcd4ab6SAlexander Motin 		    be_lun->vn != NULL) {
2560648dfc1aSAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2561648dfc1aSAlexander Motin 			ctl_lun_has_media(cbe_lun);
2562648dfc1aSAlexander Motin 		} else if ((cbe_lun->flags & CTL_LUN_FLAG_NO_MEDIA) == 0 &&
2563648dfc1aSAlexander Motin 		    be_lun->vn == NULL) {
2564648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2565648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
256671d8e97eSAlexander Motin 		}
2567648dfc1aSAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
25687ac58230SAlexander Motin 	} else {
25697ac58230SAlexander Motin 		if (be_lun->vn != NULL) {
2570648dfc1aSAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2571648dfc1aSAlexander Motin 			ctl_lun_no_media(cbe_lun);
2572ee4ad294SAlexander Motin 			taskqueue_drain_all(be_lun->io_taskqueue);
25737ac58230SAlexander Motin 			error = ctl_be_block_close(be_lun);
25747ac58230SAlexander Motin 		} else
25757ac58230SAlexander Motin 			error = 0;
25767ac58230SAlexander Motin 	}
25777ac58230SAlexander Motin 	if (be_lun->size_blocks != oldsize)
25787ac58230SAlexander Motin 		ctl_lun_capacity_changed(cbe_lun);
257981177295SEdward Tomasz Napierala 
258081177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
258181177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
258281177295SEdward Tomasz Napierala 
2583*34144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
258419720f41SAlexander Motin 	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
258581177295SEdward Tomasz Napierala 	return (0);
258681177295SEdward Tomasz Napierala 
258781177295SEdward Tomasz Napierala bailout_error:
2588*34144c2cSAlexander Motin 	sx_xunlock(&softc->modify_lock);
258981177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
259081177295SEdward Tomasz Napierala 	return (0);
259181177295SEdward Tomasz Napierala }
259281177295SEdward Tomasz Napierala 
2593130f4520SKenneth D. Merry static void
2594*34144c2cSAlexander Motin ctl_be_block_lun_shutdown(void *lun)
2595130f4520SKenneth D. Merry {
2596*34144c2cSAlexander Motin 	struct ctl_be_block_lun *be_lun = lun;
2597*34144c2cSAlexander Motin 	struct ctl_be_block_softc *softc = be_lun->softc;
2598*34144c2cSAlexander Motin 
2599*34144c2cSAlexander Motin 	taskqueue_drain_all(be_lun->io_taskqueue);
2600*34144c2cSAlexander Motin 	taskqueue_free(be_lun->io_taskqueue);
2601*34144c2cSAlexander Motin 	if (be_lun->disk_stats != NULL)
2602*34144c2cSAlexander Motin 		devstat_remove_entry(be_lun->disk_stats);
2603*34144c2cSAlexander Motin 	uma_zdestroy(be_lun->lun_zone);
2604*34144c2cSAlexander Motin 	nvlist_destroy(be_lun->cbe_lun.options);
2605*34144c2cSAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
2606*34144c2cSAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
2607*34144c2cSAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2608130f4520SKenneth D. Merry 
2609130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2610*34144c2cSAlexander Motin 	be_lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2611*34144c2cSAlexander Motin 	if (be_lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2612*34144c2cSAlexander Motin 		wakeup(be_lun);
2613*34144c2cSAlexander Motin 	else
2614*34144c2cSAlexander Motin 		free(be_lun, M_CTLBLK);
2615130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2616130f4520SKenneth D. Merry }
2617130f4520SKenneth D. Merry 
2618130f4520SKenneth D. Merry static int
2619130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2620130f4520SKenneth D. Merry {
2621130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
26220bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2623130f4520SKenneth D. Merry 	int retval;
2624130f4520SKenneth D. Merry 
2625130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2626130f4520SKenneth D. Merry 
26279cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
26280bcd4ab6SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2629130f4520SKenneth D. Merry 
263067cc546dSAlexander Motin 	retval = 0;
2631130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2632130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2633130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2634ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2635ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2636ee7f31c0SAlexander Motin 	case UNMAP:
2637130f4520SKenneth D. Merry 		/*
2638130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2639130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2640130f4520SKenneth D. Merry 		 *
2641130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2642130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2643130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2644130f4520SKenneth D. Merry 		 */
264575c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
2646130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2647130f4520SKenneth D. Merry 				   links);
264875c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
2649130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2650130f4520SKenneth D. Merry 		break;
2651130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2652130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2653648dfc1aSAlexander Motin 		struct ctl_lun_req req;
2654130f4520SKenneth D. Merry 
2655130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
265666b69676SAlexander Motin 		if ((cdb->how & SSS_PC_MASK) != 0) {
265766b69676SAlexander Motin 			ctl_set_success(&io->scsiio);
265866b69676SAlexander Motin 			ctl_config_write_done(io);
265966b69676SAlexander Motin 			break;
266066b69676SAlexander Motin 		}
2661648dfc1aSAlexander Motin 		if (cdb->how & SSS_START) {
2662648dfc1aSAlexander Motin 			if ((cdb->how & SSS_LOEJ) && be_lun->vn == NULL) {
2663648dfc1aSAlexander Motin 				retval = ctl_be_block_open(be_lun, &req);
2664648dfc1aSAlexander Motin 				cbe_lun->flags &= ~CTL_LUN_FLAG_EJECTED;
2665648dfc1aSAlexander Motin 				if (retval == 0) {
2666648dfc1aSAlexander Motin 					cbe_lun->flags &= ~CTL_LUN_FLAG_NO_MEDIA;
2667648dfc1aSAlexander Motin 					ctl_lun_has_media(cbe_lun);
2668130f4520SKenneth D. Merry 				} else {
2669648dfc1aSAlexander Motin 					cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2670648dfc1aSAlexander Motin 					ctl_lun_no_media(cbe_lun);
2671130f4520SKenneth D. Merry 				}
2672648dfc1aSAlexander Motin 			}
2673648dfc1aSAlexander Motin 			ctl_start_lun(cbe_lun);
2674648dfc1aSAlexander Motin 		} else {
2675648dfc1aSAlexander Motin 			ctl_stop_lun(cbe_lun);
2676648dfc1aSAlexander Motin 			if (cdb->how & SSS_LOEJ) {
2677648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_NO_MEDIA;
2678648dfc1aSAlexander Motin 				cbe_lun->flags |= CTL_LUN_FLAG_EJECTED;
2679648dfc1aSAlexander Motin 				ctl_lun_ejected(cbe_lun);
2680648dfc1aSAlexander Motin 				if (be_lun->vn != NULL)
2681648dfc1aSAlexander Motin 					ctl_be_block_close(be_lun);
2682648dfc1aSAlexander Motin 			}
2683648dfc1aSAlexander Motin 		}
2684648dfc1aSAlexander Motin 
2685648dfc1aSAlexander Motin 		ctl_set_success(&io->scsiio);
2686130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2687130f4520SKenneth D. Merry 		break;
2688130f4520SKenneth D. Merry 	}
268991be33dcSAlexander Motin 	case PREVENT_ALLOW:
269091be33dcSAlexander Motin 		ctl_set_success(&io->scsiio);
269191be33dcSAlexander Motin 		ctl_config_write_done(io);
269291be33dcSAlexander Motin 		break;
2693130f4520SKenneth D. Merry 	default:
2694130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2695130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2696130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2697130f4520SKenneth D. Merry 		break;
2698130f4520SKenneth D. Merry 	}
2699130f4520SKenneth D. Merry 
2700130f4520SKenneth D. Merry 	return (retval);
2701130f4520SKenneth D. Merry }
2702130f4520SKenneth D. Merry 
2703130f4520SKenneth D. Merry static int
2704130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2705130f4520SKenneth D. Merry {
2706ef8daf3fSAlexander Motin 	struct ctl_be_block_lun *be_lun;
27070bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2708ef8daf3fSAlexander Motin 	int retval = 0;
2709ef8daf3fSAlexander Motin 
2710ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
2711ef8daf3fSAlexander Motin 
27129cbbfd2fSAlexander Motin 	cbe_lun = CTL_BACKEND_LUN(io);
27130bcd4ab6SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2714ef8daf3fSAlexander Motin 
2715ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
2716ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:
2717ef8daf3fSAlexander Motin 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2718ef8daf3fSAlexander Motin 			mtx_lock(&be_lun->queue_lock);
2719ef8daf3fSAlexander Motin 			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2720ef8daf3fSAlexander Motin 			    &io->io_hdr, links);
2721ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
2722ef8daf3fSAlexander Motin 			taskqueue_enqueue(be_lun->io_taskqueue,
2723ef8daf3fSAlexander Motin 			    &be_lun->io_task);
2724ef8daf3fSAlexander Motin 			retval = CTL_RETVAL_QUEUED;
2725ef8daf3fSAlexander Motin 			break;
2726ef8daf3fSAlexander Motin 		}
2727ef8daf3fSAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
2728ef8daf3fSAlexander Motin 				      /*sks_valid*/ 1,
2729ef8daf3fSAlexander Motin 				      /*command*/ 1,
2730ef8daf3fSAlexander Motin 				      /*field*/ 1,
2731ef8daf3fSAlexander Motin 				      /*bit_valid*/ 1,
2732ef8daf3fSAlexander Motin 				      /*bit*/ 4);
2733ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2734ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2735ef8daf3fSAlexander Motin 		break;
2736ef8daf3fSAlexander Motin 	default:
2737ef8daf3fSAlexander Motin 		ctl_set_invalid_opcode(&io->scsiio);
2738ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2739ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2740ef8daf3fSAlexander Motin 		break;
2741ef8daf3fSAlexander Motin 	}
2742ef8daf3fSAlexander Motin 
2743ef8daf3fSAlexander Motin 	return (retval);
2744130f4520SKenneth D. Merry }
2745130f4520SKenneth D. Merry 
2746130f4520SKenneth D. Merry static int
2747130f4520SKenneth D. Merry ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2748130f4520SKenneth D. Merry {
2749130f4520SKenneth D. Merry 	struct ctl_be_block_lun *lun;
2750130f4520SKenneth D. Merry 	int retval;
2751130f4520SKenneth D. Merry 
2752130f4520SKenneth D. Merry 	lun = (struct ctl_be_block_lun *)be_lun;
2753130f4520SKenneth D. Merry 
27542cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "\t<num_threads>");
2755130f4520SKenneth D. Merry 	if (retval != 0)
2756130f4520SKenneth D. Merry 		goto bailout;
2757130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2758130f4520SKenneth D. Merry 	if (retval != 0)
2759130f4520SKenneth D. Merry 		goto bailout;
27602cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "</num_threads>\n");
2761130f4520SKenneth D. Merry 
2762130f4520SKenneth D. Merry bailout:
2763130f4520SKenneth D. Merry 	return (retval);
2764130f4520SKenneth D. Merry }
2765130f4520SKenneth D. Merry 
2766c3e7ba3eSAlexander Motin static uint64_t
2767c3e7ba3eSAlexander Motin ctl_be_block_lun_attr(void *be_lun, const char *attrname)
2768c3e7ba3eSAlexander Motin {
2769c3e7ba3eSAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2770c3e7ba3eSAlexander Motin 
2771c3e7ba3eSAlexander Motin 	if (lun->getattr == NULL)
2772c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
2773c3e7ba3eSAlexander Motin 	return (lun->getattr(lun, attrname));
2774c3e7ba3eSAlexander Motin }
2775c3e7ba3eSAlexander Motin 
27760c629e28SAlexander Motin static int
2777130f4520SKenneth D. Merry ctl_be_block_init(void)
2778130f4520SKenneth D. Merry {
27790c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
2780130f4520SKenneth D. Merry 
2781*34144c2cSAlexander Motin 	sx_init(&softc->modify_lock, "ctlblock modify");
278275c7a1d3SAlexander Motin 	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
27830c629e28SAlexander Motin 	softc->beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2784a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2785*34144c2cSAlexander Motin 	SLIST_INIT(&softc->lun_list);
27860c629e28SAlexander Motin 	return (0);
27870c629e28SAlexander Motin }
2788130f4520SKenneth D. Merry 
27890c629e28SAlexander Motin 
27900c629e28SAlexander Motin static int
27910c629e28SAlexander Motin ctl_be_block_shutdown(void)
27920c629e28SAlexander Motin {
27930c629e28SAlexander Motin 	struct ctl_be_block_softc *softc = &backend_block_softc;
2794*34144c2cSAlexander Motin 	struct ctl_be_block_lun *lun;
27950c629e28SAlexander Motin 
27960c629e28SAlexander Motin 	mtx_lock(&softc->lock);
2797*34144c2cSAlexander Motin 	while ((lun = SLIST_FIRST(&softc->lun_list)) != NULL) {
2798*34144c2cSAlexander Motin 		SLIST_REMOVE_HEAD(&softc->lun_list, links);
2799*34144c2cSAlexander Motin 		softc->num_luns--;
28000c629e28SAlexander Motin 		/*
2801*34144c2cSAlexander Motin 		 * Drop our lock here.  Since ctl_remove_lun() can call
28020c629e28SAlexander Motin 		 * back into us, this could potentially lead to a recursive
28030c629e28SAlexander Motin 		 * lock of the same mutex, which would cause a hang.
28040c629e28SAlexander Motin 		 */
28050c629e28SAlexander Motin 		mtx_unlock(&softc->lock);
2806*34144c2cSAlexander Motin 		ctl_remove_lun(&lun->cbe_lun);
28070c629e28SAlexander Motin 		mtx_lock(&softc->lock);
28080c629e28SAlexander Motin 	}
28090c629e28SAlexander Motin 	mtx_unlock(&softc->lock);
28100c629e28SAlexander Motin 	uma_zdestroy(softc->beio_zone);
28110c629e28SAlexander Motin 	mtx_destroy(&softc->lock);
2812*34144c2cSAlexander Motin 	sx_destroy(&softc->modify_lock);
28130c629e28SAlexander Motin 	return (0);
2814130f4520SKenneth D. Merry }
2815