xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision 75a3108e139056c8fbe7f83d6b54ef3aab9b23e7)
1130f4520SKenneth D. Merry /*-
2130f4520SKenneth D. Merry  * Copyright (c) 2003 Silicon Graphics International Corp.
3130f4520SKenneth D. Merry  * Copyright (c) 2009-2011 Spectra Logic Corporation
481177295SEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
5130f4520SKenneth D. Merry  * All rights reserved.
6130f4520SKenneth D. Merry  *
781177295SEdward Tomasz Napierala  * Portions of this software were developed by Edward Tomasz Napierala
881177295SEdward Tomasz Napierala  * under sponsorship from the FreeBSD Foundation.
981177295SEdward Tomasz Napierala  *
10130f4520SKenneth D. Merry  * Redistribution and use in source and binary forms, with or without
11130f4520SKenneth D. Merry  * modification, are permitted provided that the following conditions
12130f4520SKenneth D. Merry  * are met:
13130f4520SKenneth D. Merry  * 1. Redistributions of source code must retain the above copyright
14130f4520SKenneth D. Merry  *    notice, this list of conditions, and the following disclaimer,
15130f4520SKenneth D. Merry  *    without modification.
16130f4520SKenneth D. Merry  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17130f4520SKenneth D. Merry  *    substantially similar to the "NO WARRANTY" disclaimer below
18130f4520SKenneth D. Merry  *    ("Disclaimer") and any redistribution must be conditioned upon
19130f4520SKenneth D. Merry  *    including a substantially similar Disclaimer requirement for further
20130f4520SKenneth D. Merry  *    binary redistribution.
21130f4520SKenneth D. Merry  *
22130f4520SKenneth D. Merry  * NO WARRANTY
23130f4520SKenneth D. Merry  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24130f4520SKenneth D. Merry  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25130f4520SKenneth D. Merry  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26130f4520SKenneth D. Merry  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27130f4520SKenneth D. Merry  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28130f4520SKenneth D. Merry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29130f4520SKenneth D. Merry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30130f4520SKenneth D. Merry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31130f4520SKenneth D. Merry  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32130f4520SKenneth D. Merry  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33130f4520SKenneth D. Merry  * POSSIBILITY OF SUCH DAMAGES.
34130f4520SKenneth D. Merry  *
35130f4520SKenneth D. Merry  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
36130f4520SKenneth D. Merry  */
37130f4520SKenneth D. Merry /*
38130f4520SKenneth D. Merry  * CAM Target Layer driver backend for block devices.
39130f4520SKenneth D. Merry  *
40130f4520SKenneth D. Merry  * Author: Ken Merry <ken@FreeBSD.org>
41130f4520SKenneth D. Merry  */
42130f4520SKenneth D. Merry #include <sys/cdefs.h>
43130f4520SKenneth D. Merry __FBSDID("$FreeBSD$");
44130f4520SKenneth D. Merry 
45130f4520SKenneth D. Merry #include <sys/param.h>
46130f4520SKenneth D. Merry #include <sys/systm.h>
47130f4520SKenneth D. Merry #include <sys/kernel.h>
48130f4520SKenneth D. Merry #include <sys/types.h>
49130f4520SKenneth D. Merry #include <sys/kthread.h>
50130f4520SKenneth D. Merry #include <sys/bio.h>
51130f4520SKenneth D. Merry #include <sys/fcntl.h>
52ee7f31c0SAlexander Motin #include <sys/limits.h>
53130f4520SKenneth D. Merry #include <sys/lock.h>
54130f4520SKenneth D. Merry #include <sys/mutex.h>
55130f4520SKenneth D. Merry #include <sys/condvar.h>
56130f4520SKenneth D. Merry #include <sys/malloc.h>
57130f4520SKenneth D. Merry #include <sys/conf.h>
58130f4520SKenneth D. Merry #include <sys/ioccom.h>
59130f4520SKenneth D. Merry #include <sys/queue.h>
60130f4520SKenneth D. Merry #include <sys/sbuf.h>
61130f4520SKenneth D. Merry #include <sys/endian.h>
62130f4520SKenneth D. Merry #include <sys/uio.h>
63130f4520SKenneth D. Merry #include <sys/buf.h>
64130f4520SKenneth D. Merry #include <sys/taskqueue.h>
65130f4520SKenneth D. Merry #include <sys/vnode.h>
66130f4520SKenneth D. Merry #include <sys/namei.h>
67130f4520SKenneth D. Merry #include <sys/mount.h>
68130f4520SKenneth D. Merry #include <sys/disk.h>
69130f4520SKenneth D. Merry #include <sys/fcntl.h>
70130f4520SKenneth D. Merry #include <sys/filedesc.h>
71ef8daf3fSAlexander Motin #include <sys/filio.h>
72130f4520SKenneth D. Merry #include <sys/proc.h>
73130f4520SKenneth D. Merry #include <sys/pcpu.h>
74130f4520SKenneth D. Merry #include <sys/module.h>
75130f4520SKenneth D. Merry #include <sys/sdt.h>
76130f4520SKenneth D. Merry #include <sys/devicestat.h>
77130f4520SKenneth D. Merry #include <sys/sysctl.h>
78130f4520SKenneth D. Merry 
79130f4520SKenneth D. Merry #include <geom/geom.h>
80130f4520SKenneth D. Merry 
81130f4520SKenneth D. Merry #include <cam/cam.h>
82130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h>
83130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h>
84130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h>
85130f4520SKenneth D. Merry #include <cam/ctl/ctl.h>
86130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h>
87130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h>
887ac58230SAlexander Motin #include <cam/ctl/ctl_ha.h>
89130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h>
907ac58230SAlexander Motin #include <cam/ctl/ctl_private.h>
91130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h>
92130f4520SKenneth D. Merry 
93130f4520SKenneth D. Merry /*
9408a7cce5SAlexander Motin  * The idea here is that we'll allocate enough S/G space to hold a 1MB
9508a7cce5SAlexander Motin  * I/O.  If we get an I/O larger than that, we'll split it.
96130f4520SKenneth D. Merry  */
9711b569f7SAlexander Motin #define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
9811b569f7SAlexander Motin #define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
9908a7cce5SAlexander Motin #define	CTLBLK_MAX_SEG		MAXPHYS
10011b569f7SAlexander Motin #define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
10111b569f7SAlexander Motin #define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
102130f4520SKenneth D. Merry 
103130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG
104130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \
105130f4520SKenneth D. Merry     printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
106130f4520SKenneth D. Merry #else
107130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0)
108130f4520SKenneth D. Merry #endif
109130f4520SKenneth D. Merry 
110e86a4142SAlexander Motin #define PRIV(io)	\
111e86a4142SAlexander Motin     ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
11211b569f7SAlexander Motin #define ARGS(io)	\
11311b569f7SAlexander Motin     ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
114e86a4142SAlexander Motin 
115130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb);
116130f4520SKenneth D. Merry 
117130f4520SKenneth D. Merry typedef enum {
118130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
119130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
120130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
121130f4520SKenneth D. Merry } ctl_be_block_lun_flags;
122130f4520SKenneth D. Merry 
123130f4520SKenneth D. Merry typedef enum {
124130f4520SKenneth D. Merry 	CTL_BE_BLOCK_NONE,
125130f4520SKenneth D. Merry 	CTL_BE_BLOCK_DEV,
126130f4520SKenneth D. Merry 	CTL_BE_BLOCK_FILE
127130f4520SKenneth D. Merry } ctl_be_block_type;
128130f4520SKenneth D. Merry 
129130f4520SKenneth D. Merry struct ctl_be_block_filedata {
130130f4520SKenneth D. Merry 	struct ucred *cred;
131130f4520SKenneth D. Merry };
132130f4520SKenneth D. Merry 
133130f4520SKenneth D. Merry union ctl_be_block_bedata {
134130f4520SKenneth D. Merry 	struct ctl_be_block_filedata file;
135130f4520SKenneth D. Merry };
136130f4520SKenneth D. Merry 
137130f4520SKenneth D. Merry struct ctl_be_block_io;
138130f4520SKenneth D. Merry struct ctl_be_block_lun;
139130f4520SKenneth D. Merry 
140130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
141130f4520SKenneth D. Merry 			       struct ctl_be_block_io *beio);
142c3e7ba3eSAlexander Motin typedef uint64_t (*cbb_getattr_t)(struct ctl_be_block_lun *be_lun,
143c3e7ba3eSAlexander Motin 				  const char *attrname);
144130f4520SKenneth D. Merry 
145130f4520SKenneth D. Merry /*
146130f4520SKenneth D. Merry  * Backend LUN structure.  There is a 1:1 mapping between a block device
147130f4520SKenneth D. Merry  * and a backend block LUN, and between a backend block LUN and a CTL LUN.
148130f4520SKenneth D. Merry  */
149130f4520SKenneth D. Merry struct ctl_be_block_lun {
15019720f41SAlexander Motin 	struct ctl_lun_create_params params;
151130f4520SKenneth D. Merry 	char lunname[32];
152130f4520SKenneth D. Merry 	char *dev_path;
153130f4520SKenneth D. Merry 	ctl_be_block_type dev_type;
154130f4520SKenneth D. Merry 	struct vnode *vn;
155130f4520SKenneth D. Merry 	union ctl_be_block_bedata backend;
156130f4520SKenneth D. Merry 	cbb_dispatch_t dispatch;
157130f4520SKenneth D. Merry 	cbb_dispatch_t lun_flush;
158ee7f31c0SAlexander Motin 	cbb_dispatch_t unmap;
159ef8daf3fSAlexander Motin 	cbb_dispatch_t get_lba_status;
160c3e7ba3eSAlexander Motin 	cbb_getattr_t getattr;
161130f4520SKenneth D. Merry 	uma_zone_t lun_zone;
162130f4520SKenneth D. Merry 	uint64_t size_blocks;
163130f4520SKenneth D. Merry 	uint64_t size_bytes;
164130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
165130f4520SKenneth D. Merry 	struct devstat *disk_stats;
166130f4520SKenneth D. Merry 	ctl_be_block_lun_flags flags;
167130f4520SKenneth D. Merry 	STAILQ_ENTRY(ctl_be_block_lun) links;
1680bcd4ab6SAlexander Motin 	struct ctl_be_lun cbe_lun;
169130f4520SKenneth D. Merry 	struct taskqueue *io_taskqueue;
170130f4520SKenneth D. Merry 	struct task io_task;
171130f4520SKenneth D. Merry 	int num_threads;
172130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) input_queue;
173ef8daf3fSAlexander Motin 	STAILQ_HEAD(, ctl_io_hdr) config_read_queue;
174130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
175130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
17675c7a1d3SAlexander Motin 	struct mtx_padalign io_lock;
17775c7a1d3SAlexander Motin 	struct mtx_padalign queue_lock;
178130f4520SKenneth D. Merry };
179130f4520SKenneth D. Merry 
180130f4520SKenneth D. Merry /*
181130f4520SKenneth D. Merry  * Overall softc structure for the block backend module.
182130f4520SKenneth D. Merry  */
183130f4520SKenneth D. Merry struct ctl_be_block_softc {
184130f4520SKenneth D. Merry 	struct mtx			 lock;
185130f4520SKenneth D. Merry 	int				 num_luns;
186130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
187130f4520SKenneth D. Merry };
188130f4520SKenneth D. Merry 
189130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc;
190130f4520SKenneth D. Merry 
191130f4520SKenneth D. Merry /*
192130f4520SKenneth D. Merry  * Per-I/O information.
193130f4520SKenneth D. Merry  */
194130f4520SKenneth D. Merry struct ctl_be_block_io {
195130f4520SKenneth D. Merry 	union ctl_io			*io;
196130f4520SKenneth D. Merry 	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
197130f4520SKenneth D. Merry 	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
198130f4520SKenneth D. Merry 	int				bio_cmd;
199130f4520SKenneth D. Merry 	int				num_segs;
200130f4520SKenneth D. Merry 	int				num_bios_sent;
201130f4520SKenneth D. Merry 	int				num_bios_done;
202130f4520SKenneth D. Merry 	int				send_complete;
203130f4520SKenneth D. Merry 	int				num_errors;
204130f4520SKenneth D. Merry 	struct bintime			ds_t0;
205130f4520SKenneth D. Merry 	devstat_tag_type		ds_tag_type;
206130f4520SKenneth D. Merry 	devstat_trans_flags		ds_trans_type;
207130f4520SKenneth D. Merry 	uint64_t			io_len;
208130f4520SKenneth D. Merry 	uint64_t			io_offset;
2097d0d4342SAlexander Motin 	int				io_arg;
210130f4520SKenneth D. Merry 	struct ctl_be_block_softc	*softc;
211130f4520SKenneth D. Merry 	struct ctl_be_block_lun		*lun;
212ee7f31c0SAlexander Motin 	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
213130f4520SKenneth D. Merry };
214130f4520SKenneth D. Merry 
2157ac58230SAlexander Motin extern struct ctl_softc *control_softc;
2167ac58230SAlexander Motin 
217130f4520SKenneth D. Merry static int cbb_num_threads = 14;
218130f4520SKenneth D. Merry SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
219130f4520SKenneth D. Merry 	    "CAM Target Layer Block Backend");
220af3b2549SHans Petter Selasky SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RWTUN,
221130f4520SKenneth D. Merry            &cbb_num_threads, 0, "Number of threads per backing file");
222130f4520SKenneth D. Merry 
223130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
224130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio);
225130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio);
226130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io);
227130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio);
228130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
229130f4520SKenneth D. Merry 				    struct ctl_be_block_io *beio);
230130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
231130f4520SKenneth D. Merry 				       struct ctl_be_block_io *beio);
232ef8daf3fSAlexander Motin static void ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
233ef8daf3fSAlexander Motin 				  struct ctl_be_block_io *beio);
23453c146deSAlexander Motin static uint64_t ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun,
23553c146deSAlexander Motin 					 const char *attrname);
236130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
237130f4520SKenneth D. Merry 				   struct ctl_be_block_io *beio);
238ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
239ee7f31c0SAlexander Motin 				   struct ctl_be_block_io *beio);
240130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
241130f4520SKenneth D. Merry 				      struct ctl_be_block_io *beio);
242c3e7ba3eSAlexander Motin static uint64_t ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun,
243c3e7ba3eSAlexander Motin 					 const char *attrname);
244ef8daf3fSAlexander Motin static void ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
245ef8daf3fSAlexander Motin 				    union ctl_io *io);
246130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
247130f4520SKenneth D. Merry 				    union ctl_io *io);
248130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
249130f4520SKenneth D. Merry 				  union ctl_io *io);
250130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending);
251130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io);
252130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
253130f4520SKenneth D. Merry 				   int flag, struct thread *td);
254130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
255130f4520SKenneth D. Merry 				  struct ctl_lun_req *req);
256130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
257130f4520SKenneth D. Merry 				 struct ctl_lun_req *req);
258130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
259130f4520SKenneth D. Merry static int ctl_be_block_open(struct ctl_be_block_softc *softc,
260130f4520SKenneth D. Merry 			     struct ctl_be_block_lun *be_lun,
261130f4520SKenneth D. Merry 			     struct ctl_lun_req *req);
262130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc,
263130f4520SKenneth D. Merry 			       struct ctl_lun_req *req);
264130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
265130f4520SKenneth D. Merry 			   struct ctl_lun_req *req);
26681177295SEdward Tomasz Napierala static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
26781177295SEdward Tomasz Napierala 				  struct ctl_lun_req *req);
26881177295SEdward Tomasz Napierala static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
26981177295SEdward Tomasz Napierala 				 struct ctl_lun_req *req);
27081177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
27181177295SEdward Tomasz Napierala 			   struct ctl_lun_req *req);
272130f4520SKenneth D. Merry static void ctl_be_block_lun_shutdown(void *be_lun);
273130f4520SKenneth D. Merry static void ctl_be_block_lun_config_status(void *be_lun,
274130f4520SKenneth D. Merry 					   ctl_lun_config_status status);
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);
279130f4520SKenneth D. Merry int ctl_be_block_init(void);
280130f4520SKenneth D. Merry 
281130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver =
282130f4520SKenneth D. Merry {
2832a2443d8SKenneth D. Merry 	.name = "block",
2842a2443d8SKenneth D. Merry 	.flags = CTL_BE_FLAG_HAS_CONFIG,
2852a2443d8SKenneth D. Merry 	.init = ctl_be_block_init,
2862a2443d8SKenneth D. Merry 	.data_submit = ctl_be_block_submit,
2872a2443d8SKenneth D. Merry 	.data_move_done = ctl_be_block_move_done,
2882a2443d8SKenneth D. Merry 	.config_read = ctl_be_block_config_read,
2892a2443d8SKenneth D. Merry 	.config_write = ctl_be_block_config_write,
2902a2443d8SKenneth D. Merry 	.ioctl = ctl_be_block_ioctl,
291c3e7ba3eSAlexander Motin 	.lun_info = ctl_be_block_lun_info,
292c3e7ba3eSAlexander Motin 	.lun_attr = ctl_be_block_lun_attr
293130f4520SKenneth D. Merry };
294130f4520SKenneth D. Merry 
295130f4520SKenneth D. Merry MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
296130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
297130f4520SKenneth D. Merry 
298a0e36aeeSEdward Tomasz Napierala static uma_zone_t beio_zone;
299a0e36aeeSEdward Tomasz Napierala 
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 
305a0e36aeeSEdward Tomasz Napierala 	beio = uma_zalloc(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 
338a0e36aeeSEdward Tomasz Napierala 	uma_zfree(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
412130f4520SKenneth D. Merry 	getbintime(&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 	io->io_hdr.num_dmas++;
416130f4520SKenneth D. Merry #endif
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 		;
425f7241cceSAlexander Motin 	} else if ((io->io_hdr.port_status == 0) &&
42611b569f7SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
42711b569f7SAlexander Motin 		lbalen = ARGS(beio->io);
42811b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_READ) {
429130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
43011b569f7SAlexander Motin 		} else if (lbalen->flags & CTL_LLF_COMPARE) {
43111b569f7SAlexander Motin 			/* We have two data blocks ready for comparison. */
432d6043e46SAlexander Motin 			ctl_be_block_compare(io);
43311b569f7SAlexander Motin 		}
434f7241cceSAlexander Motin 	} else if ((io->io_hdr.port_status != 0) &&
435f7241cceSAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
436f7241cceSAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
437130f4520SKenneth D. Merry 		/*
438130f4520SKenneth D. Merry 		 * For hardware error sense keys, the sense key
439130f4520SKenneth D. Merry 		 * specific value is defined to be a retry count,
440130f4520SKenneth D. Merry 		 * but we use it to pass back an internal FETD
441130f4520SKenneth D. Merry 		 * error code.  XXX KDM  Hopefully the FETD is only
442130f4520SKenneth D. Merry 		 * using 16 bits for an error code, since that's
443130f4520SKenneth D. Merry 		 * all the space we have in the sks field.
444130f4520SKenneth D. Merry 		 */
445130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
446130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
447130f4520SKenneth D. Merry 					 /*retry_count*/
448130f4520SKenneth D. Merry 					 io->io_hdr.port_status);
449130f4520SKenneth D. Merry 	}
450130f4520SKenneth D. Merry 
451130f4520SKenneth D. Merry 	/*
452130f4520SKenneth D. Merry 	 * If this is a read, or a write with errors, it is done.
453130f4520SKenneth D. Merry 	 */
454130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_READ)
455130f4520SKenneth D. Merry 	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
456130f4520SKenneth D. Merry 	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
457130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
458130f4520SKenneth D. Merry 		return (0);
459130f4520SKenneth D. Merry 	}
460130f4520SKenneth D. Merry 
461130f4520SKenneth D. Merry 	/*
462130f4520SKenneth D. Merry 	 * At this point, we have a write and the DMA completed
463130f4520SKenneth D. Merry 	 * successfully.  We now have to queue it to the task queue to
464130f4520SKenneth D. Merry 	 * execute the backend I/O.  That is because we do blocking
465130f4520SKenneth D. Merry 	 * memory allocations, and in the file backing case, blocking I/O.
466130f4520SKenneth D. Merry 	 * This move done routine is generally called in the SIM's
467130f4520SKenneth D. Merry 	 * interrupt context, and therefore we cannot block.
468130f4520SKenneth D. Merry 	 */
46975c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
470130f4520SKenneth D. Merry 	/*
471130f4520SKenneth D. Merry 	 * XXX KDM make sure that links is okay to use at this point.
472130f4520SKenneth D. Merry 	 * Otherwise, we either need to add another field to ctl_io_hdr,
473130f4520SKenneth D. Merry 	 * or deal with resource allocation here.
474130f4520SKenneth D. Merry 	 */
475130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
47675c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
477130f4520SKenneth D. Merry 
478130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
479130f4520SKenneth D. Merry 
480130f4520SKenneth D. Merry 	return (0);
481130f4520SKenneth D. Merry }
482130f4520SKenneth D. Merry 
483130f4520SKenneth D. Merry static void
484130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio)
485130f4520SKenneth D. Merry {
486130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
487130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
488130f4520SKenneth D. Merry 	union ctl_io *io;
489e0c2f975SAlexander Motin 	int error;
490130f4520SKenneth D. Merry 
491130f4520SKenneth D. Merry 	beio = bio->bio_caller1;
492130f4520SKenneth D. Merry 	be_lun = beio->lun;
493130f4520SKenneth D. Merry 	io = beio->io;
494130f4520SKenneth D. Merry 
495130f4520SKenneth D. Merry 	DPRINTF("entered\n");
496130f4520SKenneth D. Merry 
497e0c2f975SAlexander Motin 	error = bio->bio_error;
49875c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
499e0c2f975SAlexander Motin 	if (error != 0)
500130f4520SKenneth D. Merry 		beio->num_errors++;
501130f4520SKenneth D. Merry 
502130f4520SKenneth D. Merry 	beio->num_bios_done++;
503130f4520SKenneth D. Merry 
504130f4520SKenneth D. Merry 	/*
505130f4520SKenneth D. Merry 	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
506130f4520SKenneth D. Merry 	 * during the free might cause it to complain.
507130f4520SKenneth D. Merry 	 */
508130f4520SKenneth D. Merry 	g_destroy_bio(bio);
509130f4520SKenneth D. Merry 
510130f4520SKenneth D. Merry 	/*
511130f4520SKenneth D. Merry 	 * If the send complete bit isn't set, or we aren't the last I/O to
512130f4520SKenneth D. Merry 	 * complete, then we're done.
513130f4520SKenneth D. Merry 	 */
514130f4520SKenneth D. Merry 	if ((beio->send_complete == 0)
515130f4520SKenneth D. Merry 	 || (beio->num_bios_done < beio->num_bios_sent)) {
51675c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
517130f4520SKenneth D. Merry 		return;
518130f4520SKenneth D. Merry 	}
519130f4520SKenneth D. Merry 
520130f4520SKenneth D. Merry 	/*
521130f4520SKenneth D. Merry 	 * At this point, we've verified that we are the last I/O to
522130f4520SKenneth D. Merry 	 * complete, so it's safe to drop the lock.
523130f4520SKenneth D. Merry 	 */
52475c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
52575c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
52675c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
52775c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
528130f4520SKenneth D. Merry 
529130f4520SKenneth D. Merry 	/*
530130f4520SKenneth D. Merry 	 * If there are any errors from the backing device, we fail the
531130f4520SKenneth D. Merry 	 * entire I/O with a medium error.
532130f4520SKenneth D. Merry 	 */
533130f4520SKenneth D. Merry 	if (beio->num_errors > 0) {
534e0c2f975SAlexander Motin 		if (error == EOPNOTSUPP) {
535e0c2f975SAlexander Motin 			ctl_set_invalid_opcode(&io->scsiio);
5360631de4aSAlexander Motin 		} else if (error == ENOSPC || error == EDQUOT) {
5374fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
5386187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
5396187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
540e0c2f975SAlexander Motin 		} else if (beio->bio_cmd == BIO_FLUSH) {
541130f4520SKenneth D. Merry 			/* XXX KDM is there is a better error here? */
542130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
543130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
544130f4520SKenneth D. Merry 						 /*retry_count*/ 0xbad2);
5457f7bb97aSAlexander Motin 		} else {
5467f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
5477f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
5487f7bb97aSAlexander Motin 		}
549130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
550130f4520SKenneth D. Merry 		return;
551130f4520SKenneth D. Merry 	}
552130f4520SKenneth D. Merry 
553130f4520SKenneth D. Merry 	/*
55411b569f7SAlexander Motin 	 * If this is a write, a flush, a delete or verify, we're all done.
555130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
556130f4520SKenneth D. Merry 	 */
557130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_WRITE)
558ee7f31c0SAlexander Motin 	 || (beio->bio_cmd == BIO_FLUSH)
55911b569f7SAlexander Motin 	 || (beio->bio_cmd == BIO_DELETE)
56011b569f7SAlexander Motin 	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
561130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
562130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
563130f4520SKenneth D. Merry 	} else {
564f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
565*75a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
566f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
567*75a3108eSAlexander Motin 			ctl_serseq_done(io);
568*75a3108eSAlexander Motin 		}
569130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
570130f4520SKenneth D. Merry         	getbintime(&io->io_hdr.dma_start_bt);
571130f4520SKenneth D. Merry #endif
572130f4520SKenneth D. Merry 		ctl_datamove(io);
573130f4520SKenneth D. Merry 	}
574130f4520SKenneth D. Merry }
575130f4520SKenneth D. Merry 
576130f4520SKenneth D. Merry static void
577130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
578130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio)
579130f4520SKenneth D. Merry {
58075c7a1d3SAlexander Motin 	union ctl_io *io = beio->io;
581130f4520SKenneth D. Merry 	struct mount *mountpoint;
5825050aa86SKonstantin Belousov 	int error, lock_flags;
583130f4520SKenneth D. Merry 
584130f4520SKenneth D. Merry 	DPRINTF("entered\n");
585130f4520SKenneth D. Merry 
58675c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
58775c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
58875c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
58975c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
590130f4520SKenneth D. Merry 
591130f4520SKenneth D. Merry 	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
592130f4520SKenneth D. Merry 
593130f4520SKenneth D. Merry 	if (MNT_SHARED_WRITES(mountpoint)
594130f4520SKenneth D. Merry 	 || ((mountpoint == NULL)
595130f4520SKenneth D. Merry 	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
596130f4520SKenneth D. Merry 		lock_flags = LK_SHARED;
597130f4520SKenneth D. Merry 	else
598130f4520SKenneth D. Merry 		lock_flags = LK_EXCLUSIVE;
599130f4520SKenneth D. Merry 
600130f4520SKenneth D. Merry 	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
601130f4520SKenneth D. Merry 
6027d0d4342SAlexander Motin 	error = VOP_FSYNC(be_lun->vn, beio->io_arg ? MNT_NOWAIT : MNT_WAIT,
6037d0d4342SAlexander Motin 	    curthread);
604130f4520SKenneth D. Merry 	VOP_UNLOCK(be_lun->vn, 0);
605130f4520SKenneth D. Merry 
606130f4520SKenneth D. Merry 	vn_finished_write(mountpoint);
607130f4520SKenneth D. Merry 
60875c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
60975c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
61075c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
61175c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
61275c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
61375c7a1d3SAlexander Motin 
614130f4520SKenneth D. Merry 	if (error == 0)
615130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
616130f4520SKenneth D. Merry 	else {
617130f4520SKenneth D. Merry 		/* XXX KDM is there is a better error here? */
618130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
619130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
620130f4520SKenneth D. Merry 					 /*retry_count*/ 0xbad1);
621130f4520SKenneth D. Merry 	}
622130f4520SKenneth D. Merry 
623130f4520SKenneth D. Merry 	ctl_complete_beio(beio);
624130f4520SKenneth D. Merry }
625130f4520SKenneth D. Merry 
626d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
627d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
628d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
629d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
630130f4520SKenneth D. Merry 
631130f4520SKenneth D. Merry static void
632130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
633130f4520SKenneth D. Merry 			   struct ctl_be_block_io *beio)
634130f4520SKenneth D. Merry {
635130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
636130f4520SKenneth D. Merry 	union ctl_io *io;
637130f4520SKenneth D. Merry 	struct uio xuio;
638130f4520SKenneth D. Merry 	struct iovec *xiovec;
63983981e31SAlexander Motin 	size_t s;
64083981e31SAlexander Motin 	int error, flags, i;
641130f4520SKenneth D. Merry 
642130f4520SKenneth D. Merry 	DPRINTF("entered\n");
643130f4520SKenneth D. Merry 
644130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
645130f4520SKenneth D. Merry 	io = beio->io;
64655551d05SAlexander Motin 	flags = 0;
64755551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
64855551d05SAlexander Motin 		flags |= IO_DIRECT;
64955551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
65055551d05SAlexander Motin 		flags |= IO_SYNC;
651130f4520SKenneth D. Merry 
65211b569f7SAlexander Motin 	bzero(&xuio, sizeof(xuio));
653130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
654130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
65511b569f7SAlexander Motin 		xuio.uio_rw = UIO_READ;
656130f4520SKenneth D. Merry 	} else {
657130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
658130f4520SKenneth D. Merry 		xuio.uio_rw = UIO_WRITE;
65911b569f7SAlexander Motin 	}
660130f4520SKenneth D. Merry 	xuio.uio_offset = beio->io_offset;
661130f4520SKenneth D. Merry 	xuio.uio_resid = beio->io_len;
662130f4520SKenneth D. Merry 	xuio.uio_segflg = UIO_SYSSPACE;
663130f4520SKenneth D. Merry 	xuio.uio_iov = beio->xiovecs;
664130f4520SKenneth D. Merry 	xuio.uio_iovcnt = beio->num_segs;
665130f4520SKenneth D. Merry 	xuio.uio_td = curthread;
666130f4520SKenneth D. Merry 
667130f4520SKenneth D. Merry 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
668130f4520SKenneth D. Merry 		xiovec->iov_base = beio->sg_segs[i].addr;
669130f4520SKenneth D. Merry 		xiovec->iov_len = beio->sg_segs[i].len;
670130f4520SKenneth D. Merry 	}
671130f4520SKenneth D. Merry 
67275c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
67375c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
67475c7a1d3SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
67575c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
67675c7a1d3SAlexander Motin 
677130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
678130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
679130f4520SKenneth D. Merry 
680130f4520SKenneth D. Merry 		/*
681130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for reads.  If the
682130f4520SKenneth D. Merry 		 * DIRECTIO option is configured into the kernel, it calls
683130f4520SKenneth D. Merry 		 * ffs_rawread().  But that only works for single-segment
684130f4520SKenneth D. Merry 		 * uios with user space addresses.  In our case, with a
685130f4520SKenneth D. Merry 		 * kernel uio, it still reads into the buffer cache, but it
686130f4520SKenneth D. Merry 		 * will just try to release the buffer from the cache later
687130f4520SKenneth D. Merry 		 * on in ffs_read().
688130f4520SKenneth D. Merry 		 *
689130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for reads.
690130f4520SKenneth D. Merry 		 *
691130f4520SKenneth D. Merry 		 * UFS does not pay attention to IO_SYNC for reads.
692130f4520SKenneth D. Merry 		 *
693130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (which translates into the
694130f4520SKenneth D. Merry 		 * Solaris define FRSYNC for zfs_read()) for reads.  It
695130f4520SKenneth D. Merry 		 * attempts to sync the file before reading.
696130f4520SKenneth D. Merry 		 */
69755551d05SAlexander Motin 		error = VOP_READ(be_lun->vn, &xuio, flags, file_data->cred);
698130f4520SKenneth D. Merry 
699130f4520SKenneth D. Merry 		VOP_UNLOCK(be_lun->vn, 0);
70011b569f7SAlexander Motin 		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
70183981e31SAlexander Motin 		if (error == 0 && xuio.uio_resid > 0) {
70283981e31SAlexander Motin 			/*
70383981e31SAlexander Motin 			 * If we red less then requested (EOF), then
70483981e31SAlexander Motin 			 * we should clean the rest of the buffer.
70583981e31SAlexander Motin 			 */
70683981e31SAlexander Motin 			s = beio->io_len - xuio.uio_resid;
70783981e31SAlexander Motin 			for (i = 0; i < beio->num_segs; i++) {
70883981e31SAlexander Motin 				if (s >= beio->sg_segs[i].len) {
70983981e31SAlexander Motin 					s -= beio->sg_segs[i].len;
71083981e31SAlexander Motin 					continue;
71183981e31SAlexander Motin 				}
71283981e31SAlexander Motin 				bzero((uint8_t *)beio->sg_segs[i].addr + s,
71383981e31SAlexander Motin 				    beio->sg_segs[i].len - s);
71483981e31SAlexander Motin 				s = 0;
71583981e31SAlexander Motin 			}
71683981e31SAlexander Motin 		}
717130f4520SKenneth D. Merry 	} else {
718130f4520SKenneth D. Merry 		struct mount *mountpoint;
719130f4520SKenneth D. Merry 		int lock_flags;
720130f4520SKenneth D. Merry 
721130f4520SKenneth D. Merry 		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
722130f4520SKenneth D. Merry 
723130f4520SKenneth D. Merry 		if (MNT_SHARED_WRITES(mountpoint)
724130f4520SKenneth D. Merry 		 || ((mountpoint == NULL)
725130f4520SKenneth D. Merry 		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
726130f4520SKenneth D. Merry 			lock_flags = LK_SHARED;
727130f4520SKenneth D. Merry 		else
728130f4520SKenneth D. Merry 			lock_flags = LK_EXCLUSIVE;
729130f4520SKenneth D. Merry 
730130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
731130f4520SKenneth D. Merry 
732130f4520SKenneth D. Merry 		/*
733130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for writes.  The write
734130f4520SKenneth D. Merry 		 * is done asynchronously.  (Normally the write would just
735130f4520SKenneth D. Merry 		 * get put into cache.
736130f4520SKenneth D. Merry 		 *
737130f4520SKenneth D. Merry 		 * UFS pays attention to IO_SYNC for writes.  It will
738130f4520SKenneth D. Merry 		 * attempt to write the buffer out synchronously if that
739130f4520SKenneth D. Merry 		 * flag is set.
740130f4520SKenneth D. Merry 		 *
741130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for writes.
742130f4520SKenneth D. Merry 		 *
743130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
744130f4520SKenneth D. Merry 		 * for writes.  It will flush the transaction from the
745130f4520SKenneth D. Merry 		 * cache before returning.
746130f4520SKenneth D. Merry 		 */
74755551d05SAlexander Motin 		error = VOP_WRITE(be_lun->vn, &xuio, flags, file_data->cred);
748130f4520SKenneth D. Merry 		VOP_UNLOCK(be_lun->vn, 0);
749130f4520SKenneth D. Merry 
750130f4520SKenneth D. Merry 		vn_finished_write(mountpoint);
75111b569f7SAlexander Motin 		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
752130f4520SKenneth D. Merry         }
753130f4520SKenneth D. Merry 
75475c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
75575c7a1d3SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
75675c7a1d3SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
75775c7a1d3SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
75875c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
75975c7a1d3SAlexander Motin 
760130f4520SKenneth D. Merry 	/*
761130f4520SKenneth D. Merry 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
762130f4520SKenneth D. Merry 	 * return the I/O to the user.
763130f4520SKenneth D. Merry 	 */
764130f4520SKenneth D. Merry 	if (error != 0) {
7650631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
7664fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
7676187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
7686187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
7697f7bb97aSAlexander Motin 		} else {
7707f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
7717f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
7727f7bb97aSAlexander Motin 		}
773130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
774130f4520SKenneth D. Merry 		return;
775130f4520SKenneth D. Merry 	}
776130f4520SKenneth D. Merry 
777130f4520SKenneth D. Merry 	/*
778696297adSAlexander Motin 	 * If this is a write or a verify, we're all done.
779130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
780130f4520SKenneth D. Merry 	 */
781696297adSAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
782696297adSAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
783130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
784130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
785130f4520SKenneth D. Merry 	} else {
786f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
787*75a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
788f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
789*75a3108eSAlexander Motin 			ctl_serseq_done(io);
790*75a3108eSAlexander Motin 		}
791130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
792130f4520SKenneth D. Merry         	getbintime(&io->io_hdr.dma_start_bt);
793130f4520SKenneth D. Merry #endif
794130f4520SKenneth D. Merry 		ctl_datamove(io);
795130f4520SKenneth D. Merry 	}
796130f4520SKenneth D. Merry }
797130f4520SKenneth D. Merry 
798130f4520SKenneth D. Merry static void
799ef8daf3fSAlexander Motin ctl_be_block_gls_file(struct ctl_be_block_lun *be_lun,
800ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
801ef8daf3fSAlexander Motin {
802ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
803ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
804ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
805ef8daf3fSAlexander Motin 	off_t roff, off;
806ef8daf3fSAlexander Motin 	int error, status;
807ef8daf3fSAlexander Motin 
808ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
809ef8daf3fSAlexander Motin 
8100bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
811ef8daf3fSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
812ef8daf3fSAlexander Motin 	error = VOP_IOCTL(be_lun->vn, FIOSEEKHOLE, &off,
813ef8daf3fSAlexander Motin 	    0, curthread->td_ucred, curthread);
814ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
815ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
816ef8daf3fSAlexander Motin 	else {
817ef8daf3fSAlexander Motin 		error = VOP_IOCTL(be_lun->vn, FIOSEEKDATA, &off,
818ef8daf3fSAlexander Motin 		    0, curthread->td_ucred, curthread);
819ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
820ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
821ef8daf3fSAlexander Motin 		else {
822ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
823ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
824ef8daf3fSAlexander Motin 		}
825ef8daf3fSAlexander Motin 	}
826ef8daf3fSAlexander Motin 	VOP_UNLOCK(be_lun->vn, 0);
827ef8daf3fSAlexander Motin 
828ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
829ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
8300bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
8310bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
832ef8daf3fSAlexander Motin 	data->descr[0].status = status;
833ef8daf3fSAlexander Motin 
834ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
835ef8daf3fSAlexander Motin }
836ef8daf3fSAlexander Motin 
83753c146deSAlexander Motin static uint64_t
83853c146deSAlexander Motin ctl_be_block_getattr_file(struct ctl_be_block_lun *be_lun, const char *attrname)
83953c146deSAlexander Motin {
84053c146deSAlexander Motin 	struct vattr		vattr;
84153c146deSAlexander Motin 	struct statfs		statfs;
842b9b4269cSAlexander Motin 	uint64_t		val;
84353c146deSAlexander Motin 	int			error;
84453c146deSAlexander Motin 
845b9b4269cSAlexander Motin 	val = UINT64_MAX;
84653c146deSAlexander Motin 	if (be_lun->vn == NULL)
847b9b4269cSAlexander Motin 		return (val);
848b9b4269cSAlexander Motin 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
84953c146deSAlexander Motin 	if (strcmp(attrname, "blocksused") == 0) {
85053c146deSAlexander Motin 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
851b9b4269cSAlexander Motin 		if (error == 0)
8520bcd4ab6SAlexander Motin 			val = vattr.va_bytes / be_lun->cbe_lun.blocksize;
85353c146deSAlexander Motin 	}
854b9b4269cSAlexander Motin 	if (strcmp(attrname, "blocksavail") == 0 &&
855b9b4269cSAlexander Motin 	    (be_lun->vn->v_iflag & VI_DOOMED) == 0) {
85653c146deSAlexander Motin 		error = VFS_STATFS(be_lun->vn->v_mount, &statfs);
857b9b4269cSAlexander Motin 		if (error == 0)
858a15bbf15SAlexander Motin 			val = statfs.f_bavail * statfs.f_bsize /
8590bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
86053c146deSAlexander Motin 	}
861b9b4269cSAlexander Motin 	VOP_UNLOCK(be_lun->vn, 0);
862b9b4269cSAlexander Motin 	return (val);
86353c146deSAlexander Motin }
86453c146deSAlexander Motin 
865ef8daf3fSAlexander Motin static void
86667f586a8SAlexander Motin ctl_be_block_dispatch_zvol(struct ctl_be_block_lun *be_lun,
86767f586a8SAlexander Motin 			   struct ctl_be_block_io *beio)
86867f586a8SAlexander Motin {
86967f586a8SAlexander Motin 	union ctl_io *io;
8703236151eSAlexander Motin 	struct cdevsw *csw;
8713236151eSAlexander Motin 	struct cdev *dev;
87267f586a8SAlexander Motin 	struct uio xuio;
87367f586a8SAlexander Motin 	struct iovec *xiovec;
8743236151eSAlexander Motin 	int error, flags, i, ref;
87567f586a8SAlexander Motin 
87667f586a8SAlexander Motin 	DPRINTF("entered\n");
87767f586a8SAlexander Motin 
87867f586a8SAlexander Motin 	io = beio->io;
87955551d05SAlexander Motin 	flags = 0;
88055551d05SAlexander Motin 	if (ARGS(io)->flags & CTL_LLF_DPO)
88155551d05SAlexander Motin 		flags |= IO_DIRECT;
88255551d05SAlexander Motin 	if (beio->bio_cmd == BIO_WRITE && ARGS(io)->flags & CTL_LLF_FUA)
88355551d05SAlexander Motin 		flags |= IO_SYNC;
88467f586a8SAlexander Motin 
88567f586a8SAlexander Motin 	bzero(&xuio, sizeof(xuio));
88667f586a8SAlexander Motin 	if (beio->bio_cmd == BIO_READ) {
88767f586a8SAlexander Motin 		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
88867f586a8SAlexander Motin 		xuio.uio_rw = UIO_READ;
88967f586a8SAlexander Motin 	} else {
89067f586a8SAlexander Motin 		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
89167f586a8SAlexander Motin 		xuio.uio_rw = UIO_WRITE;
89267f586a8SAlexander Motin 	}
89367f586a8SAlexander Motin 	xuio.uio_offset = beio->io_offset;
89467f586a8SAlexander Motin 	xuio.uio_resid = beio->io_len;
89567f586a8SAlexander Motin 	xuio.uio_segflg = UIO_SYSSPACE;
89667f586a8SAlexander Motin 	xuio.uio_iov = beio->xiovecs;
89767f586a8SAlexander Motin 	xuio.uio_iovcnt = beio->num_segs;
89867f586a8SAlexander Motin 	xuio.uio_td = curthread;
89967f586a8SAlexander Motin 
90067f586a8SAlexander Motin 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
90167f586a8SAlexander Motin 		xiovec->iov_base = beio->sg_segs[i].addr;
90267f586a8SAlexander Motin 		xiovec->iov_len = beio->sg_segs[i].len;
90367f586a8SAlexander Motin 	}
90467f586a8SAlexander Motin 
90567f586a8SAlexander Motin 	binuptime(&beio->ds_t0);
90667f586a8SAlexander Motin 	mtx_lock(&be_lun->io_lock);
90767f586a8SAlexander Motin 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
90867f586a8SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
90967f586a8SAlexander Motin 
9103236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
9113236151eSAlexander Motin 	if (csw) {
9123236151eSAlexander Motin 		if (beio->bio_cmd == BIO_READ)
9133236151eSAlexander Motin 			error = csw->d_read(dev, &xuio, flags);
9143236151eSAlexander Motin 		else
9153236151eSAlexander Motin 			error = csw->d_write(dev, &xuio, flags);
9163236151eSAlexander Motin 		dev_relthread(dev, ref);
9173236151eSAlexander Motin 	} else
9183236151eSAlexander Motin 		error = ENXIO;
9193236151eSAlexander Motin 
9203236151eSAlexander Motin 	if (beio->bio_cmd == BIO_READ)
92167f586a8SAlexander Motin 		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
9223236151eSAlexander Motin 	else
92367f586a8SAlexander Motin 		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
92467f586a8SAlexander Motin 
92567f586a8SAlexander Motin 	mtx_lock(&be_lun->io_lock);
92667f586a8SAlexander Motin 	devstat_end_transaction(beio->lun->disk_stats, beio->io_len,
92767f586a8SAlexander Motin 	    beio->ds_tag_type, beio->ds_trans_type,
92867f586a8SAlexander Motin 	    /*now*/ NULL, /*then*/&beio->ds_t0);
92967f586a8SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
93067f586a8SAlexander Motin 
93167f586a8SAlexander Motin 	/*
93267f586a8SAlexander Motin 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
93367f586a8SAlexander Motin 	 * return the I/O to the user.
93467f586a8SAlexander Motin 	 */
93567f586a8SAlexander Motin 	if (error != 0) {
9360631de4aSAlexander Motin 		if (error == ENOSPC || error == EDQUOT) {
9374fc18ff9SAlexander Motin 			ctl_set_space_alloc_fail(&io->scsiio);
9386187d472SAlexander Motin 		} else if (error == EROFS || error == EACCES) {
9396187d472SAlexander Motin 			ctl_set_hw_write_protected(&io->scsiio);
9407f7bb97aSAlexander Motin 		} else {
9417f7bb97aSAlexander Motin 			ctl_set_medium_error(&io->scsiio,
9427f7bb97aSAlexander Motin 			    beio->bio_cmd == BIO_READ);
9437f7bb97aSAlexander Motin 		}
94467f586a8SAlexander Motin 		ctl_complete_beio(beio);
94567f586a8SAlexander Motin 		return;
94667f586a8SAlexander Motin 	}
94767f586a8SAlexander Motin 
94867f586a8SAlexander Motin 	/*
94967f586a8SAlexander Motin 	 * If this is a write or a verify, we're all done.
95067f586a8SAlexander Motin 	 * If this is a read, we can now send the data to the user.
95167f586a8SAlexander Motin 	 */
95267f586a8SAlexander Motin 	if ((beio->bio_cmd == BIO_WRITE) ||
95367f586a8SAlexander Motin 	    (ARGS(io)->flags & CTL_LLF_VERIFY)) {
95467f586a8SAlexander Motin 		ctl_set_success(&io->scsiio);
95567f586a8SAlexander Motin 		ctl_complete_beio(beio);
95667f586a8SAlexander Motin 	} else {
957f7241cceSAlexander Motin 		if ((ARGS(io)->flags & CTL_LLF_READ) &&
958*75a3108eSAlexander Motin 		    beio->beio_cont == NULL) {
959f7241cceSAlexander Motin 			ctl_set_success(&io->scsiio);
960*75a3108eSAlexander Motin 			ctl_serseq_done(io);
961*75a3108eSAlexander Motin 		}
96267f586a8SAlexander Motin #ifdef CTL_TIME_IO
96367f586a8SAlexander Motin         	getbintime(&io->io_hdr.dma_start_bt);
96467f586a8SAlexander Motin #endif
96567f586a8SAlexander Motin 		ctl_datamove(io);
96667f586a8SAlexander Motin 	}
96767f586a8SAlexander Motin }
96867f586a8SAlexander Motin 
96967f586a8SAlexander Motin static void
970ef8daf3fSAlexander Motin ctl_be_block_gls_zvol(struct ctl_be_block_lun *be_lun,
971ef8daf3fSAlexander Motin 			struct ctl_be_block_io *beio)
972ef8daf3fSAlexander Motin {
973ef8daf3fSAlexander Motin 	union ctl_io *io = beio->io;
9743236151eSAlexander Motin 	struct cdevsw *csw;
9753236151eSAlexander Motin 	struct cdev *dev;
976ef8daf3fSAlexander Motin 	struct ctl_lba_len_flags *lbalen = ARGS(io);
977ef8daf3fSAlexander Motin 	struct scsi_get_lba_status_data *data;
978ef8daf3fSAlexander Motin 	off_t roff, off;
9793236151eSAlexander Motin 	int error, ref, status;
980ef8daf3fSAlexander Motin 
981ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
982ef8daf3fSAlexander Motin 
9833236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
9843236151eSAlexander Motin 	if (csw == NULL) {
9853236151eSAlexander Motin 		status = 0;	/* unknown up to the end */
9863236151eSAlexander Motin 		off = be_lun->size_bytes;
9873236151eSAlexander Motin 		goto done;
9883236151eSAlexander Motin 	}
9890bcd4ab6SAlexander Motin 	off = roff = ((off_t)lbalen->lba) * be_lun->cbe_lun.blocksize;
9903236151eSAlexander Motin 	error = csw->d_ioctl(dev, FIOSEEKHOLE, (caddr_t)&off, FREAD,
9913236151eSAlexander Motin 	    curthread);
992ef8daf3fSAlexander Motin 	if (error == 0 && off > roff)
993ef8daf3fSAlexander Motin 		status = 0;	/* mapped up to off */
994ef8daf3fSAlexander Motin 	else {
9953236151eSAlexander Motin 		error = csw->d_ioctl(dev, FIOSEEKDATA, (caddr_t)&off, FREAD,
9963236151eSAlexander Motin 		    curthread);
997ef8daf3fSAlexander Motin 		if (error == 0 && off > roff)
998ef8daf3fSAlexander Motin 			status = 1;	/* deallocated up to off */
999ef8daf3fSAlexander Motin 		else {
1000ef8daf3fSAlexander Motin 			status = 0;	/* unknown up to the end */
1001ef8daf3fSAlexander Motin 			off = be_lun->size_bytes;
1002ef8daf3fSAlexander Motin 		}
1003ef8daf3fSAlexander Motin 	}
10043236151eSAlexander Motin 	dev_relthread(dev, ref);
1005ef8daf3fSAlexander Motin 
10063236151eSAlexander Motin done:
1007ef8daf3fSAlexander Motin 	data = (struct scsi_get_lba_status_data *)io->scsiio.kern_data_ptr;
1008ef8daf3fSAlexander Motin 	scsi_u64to8b(lbalen->lba, data->descr[0].addr);
10090bcd4ab6SAlexander Motin 	scsi_ulto4b(MIN(UINT32_MAX, off / be_lun->cbe_lun.blocksize -
10100bcd4ab6SAlexander Motin 	    lbalen->lba), data->descr[0].length);
1011ef8daf3fSAlexander Motin 	data->descr[0].status = status;
1012ef8daf3fSAlexander Motin 
1013ef8daf3fSAlexander Motin 	ctl_complete_beio(beio);
1014ef8daf3fSAlexander Motin }
1015ef8daf3fSAlexander Motin 
1016ef8daf3fSAlexander Motin static void
1017130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
1018130f4520SKenneth D. Merry 		       struct ctl_be_block_io *beio)
1019130f4520SKenneth D. Merry {
1020130f4520SKenneth D. Merry 	struct bio *bio;
1021130f4520SKenneth D. Merry 	union ctl_io *io;
10223236151eSAlexander Motin 	struct cdevsw *csw;
10233236151eSAlexander Motin 	struct cdev *dev;
10243236151eSAlexander Motin 	int ref;
1025130f4520SKenneth D. Merry 
1026130f4520SKenneth D. Merry 	io = beio->io;
1027130f4520SKenneth D. Merry 
1028130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1029130f4520SKenneth D. Merry 
1030130f4520SKenneth D. Merry 	/* This can't fail, it's a blocking allocation. */
1031130f4520SKenneth D. Merry 	bio = g_alloc_bio();
1032130f4520SKenneth D. Merry 
1033130f4520SKenneth D. Merry 	bio->bio_cmd	    = BIO_FLUSH;
1034130f4520SKenneth D. Merry 	bio->bio_offset	    = 0;
1035130f4520SKenneth D. Merry 	bio->bio_data	    = 0;
1036130f4520SKenneth D. Merry 	bio->bio_done	    = ctl_be_block_biodone;
1037130f4520SKenneth D. Merry 	bio->bio_caller1    = beio;
1038130f4520SKenneth D. Merry 	bio->bio_pblkno	    = 0;
1039130f4520SKenneth D. Merry 
1040130f4520SKenneth D. Merry 	/*
1041130f4520SKenneth D. Merry 	 * We don't need to acquire the LUN lock here, because we are only
1042130f4520SKenneth D. Merry 	 * sending one bio, and so there is no other context to synchronize
1043130f4520SKenneth D. Merry 	 * with.
1044130f4520SKenneth D. Merry 	 */
1045130f4520SKenneth D. Merry 	beio->num_bios_sent = 1;
1046130f4520SKenneth D. Merry 	beio->send_complete = 1;
1047130f4520SKenneth D. Merry 
1048130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
104975c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
1050130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
105175c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
1052130f4520SKenneth D. Merry 
10533236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10543236151eSAlexander Motin 	if (csw) {
10553236151eSAlexander Motin 		bio->bio_dev = dev;
10563236151eSAlexander Motin 		csw->d_strategy(bio);
10573236151eSAlexander Motin 		dev_relthread(dev, ref);
10583236151eSAlexander Motin 	} else {
10593236151eSAlexander Motin 		bio->bio_error = ENXIO;
10603236151eSAlexander Motin 		ctl_be_block_biodone(bio);
10613236151eSAlexander Motin 	}
1062130f4520SKenneth D. Merry }
1063130f4520SKenneth D. Merry 
1064130f4520SKenneth D. Merry static void
1065ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
1066ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio,
1067ee7f31c0SAlexander Motin 		       uint64_t off, uint64_t len, int last)
1068ee7f31c0SAlexander Motin {
1069ee7f31c0SAlexander Motin 	struct bio *bio;
10708f5a226aSAlexander Motin 	uint64_t maxlen;
10713236151eSAlexander Motin 	struct cdevsw *csw;
10723236151eSAlexander Motin 	struct cdev *dev;
10733236151eSAlexander Motin 	int ref;
1074ee7f31c0SAlexander Motin 
10753236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
10760bcd4ab6SAlexander Motin 	maxlen = LONG_MAX - (LONG_MAX % be_lun->cbe_lun.blocksize);
1077ee7f31c0SAlexander Motin 	while (len > 0) {
1078ee7f31c0SAlexander Motin 		bio = g_alloc_bio();
1079ee7f31c0SAlexander Motin 		bio->bio_cmd	    = BIO_DELETE;
10803236151eSAlexander Motin 		bio->bio_dev	    = dev;
1081ee7f31c0SAlexander Motin 		bio->bio_offset	    = off;
10828f5a226aSAlexander Motin 		bio->bio_length	    = MIN(len, maxlen);
1083ee7f31c0SAlexander Motin 		bio->bio_data	    = 0;
1084ee7f31c0SAlexander Motin 		bio->bio_done	    = ctl_be_block_biodone;
1085ee7f31c0SAlexander Motin 		bio->bio_caller1    = beio;
10860bcd4ab6SAlexander Motin 		bio->bio_pblkno     = off / be_lun->cbe_lun.blocksize;
1087ee7f31c0SAlexander Motin 
1088ee7f31c0SAlexander Motin 		off += bio->bio_length;
1089ee7f31c0SAlexander Motin 		len -= bio->bio_length;
1090ee7f31c0SAlexander Motin 
109175c7a1d3SAlexander Motin 		mtx_lock(&be_lun->io_lock);
1092ee7f31c0SAlexander Motin 		beio->num_bios_sent++;
1093ee7f31c0SAlexander Motin 		if (last && len == 0)
1094ee7f31c0SAlexander Motin 			beio->send_complete = 1;
109575c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->io_lock);
1096ee7f31c0SAlexander Motin 
10973236151eSAlexander Motin 		if (csw) {
10983236151eSAlexander Motin 			csw->d_strategy(bio);
10993236151eSAlexander Motin 		} else {
11003236151eSAlexander Motin 			bio->bio_error = ENXIO;
11013236151eSAlexander Motin 			ctl_be_block_biodone(bio);
1102ee7f31c0SAlexander Motin 		}
1103ee7f31c0SAlexander Motin 	}
11043236151eSAlexander Motin 	if (csw)
11053236151eSAlexander Motin 		dev_relthread(dev, ref);
11063236151eSAlexander Motin }
1107ee7f31c0SAlexander Motin 
1108ee7f31c0SAlexander Motin static void
1109ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
1110ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio)
1111ee7f31c0SAlexander Motin {
1112ee7f31c0SAlexander Motin 	union ctl_io *io;
111366df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1114ee7f31c0SAlexander Motin 	struct scsi_unmap_desc *buf, *end;
1115ee7f31c0SAlexander Motin 	uint64_t len;
1116ee7f31c0SAlexander Motin 
1117ee7f31c0SAlexander Motin 	io = beio->io;
1118ee7f31c0SAlexander Motin 
1119ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1120ee7f31c0SAlexander Motin 
1121ee7f31c0SAlexander Motin 	binuptime(&beio->ds_t0);
112275c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
1123ee7f31c0SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
112475c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
1125ee7f31c0SAlexander Motin 
1126ee7f31c0SAlexander Motin 	if (beio->io_offset == -1) {
1127ee7f31c0SAlexander Motin 		beio->io_len = 0;
112866df9136SAlexander Motin 		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
112966df9136SAlexander Motin 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
113066df9136SAlexander Motin 		end = buf + ptrlen->len / sizeof(*buf);
1131ee7f31c0SAlexander Motin 		for (; buf < end; buf++) {
1132ee7f31c0SAlexander Motin 			len = (uint64_t)scsi_4btoul(buf->length) *
11330bcd4ab6SAlexander Motin 			    be_lun->cbe_lun.blocksize;
1134ee7f31c0SAlexander Motin 			beio->io_len += len;
1135ee7f31c0SAlexander Motin 			ctl_be_block_unmap_dev_range(be_lun, beio,
11360bcd4ab6SAlexander Motin 			    scsi_8btou64(buf->lba) * be_lun->cbe_lun.blocksize,
11370bcd4ab6SAlexander Motin 			    len, (end - buf < 2) ? TRUE : FALSE);
1138ee7f31c0SAlexander Motin 		}
1139ee7f31c0SAlexander Motin 	} else
1140ee7f31c0SAlexander Motin 		ctl_be_block_unmap_dev_range(be_lun, beio,
1141ee7f31c0SAlexander Motin 		    beio->io_offset, beio->io_len, TRUE);
1142ee7f31c0SAlexander Motin }
1143ee7f31c0SAlexander Motin 
1144ee7f31c0SAlexander Motin static void
1145130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
1146130f4520SKenneth D. Merry 			  struct ctl_be_block_io *beio)
1147130f4520SKenneth D. Merry {
114875c7a1d3SAlexander Motin 	TAILQ_HEAD(, bio) queue = TAILQ_HEAD_INITIALIZER(queue);
1149130f4520SKenneth D. Merry 	struct bio *bio;
11503236151eSAlexander Motin 	struct cdevsw *csw;
11513236151eSAlexander Motin 	struct cdev *dev;
1152130f4520SKenneth D. Merry 	off_t cur_offset;
11533236151eSAlexander Motin 	int i, max_iosize, ref;
1154130f4520SKenneth D. Merry 
1155130f4520SKenneth D. Merry 	DPRINTF("entered\n");
11563236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
1157130f4520SKenneth D. Merry 
1158130f4520SKenneth D. Merry 	/*
1159130f4520SKenneth D. Merry 	 * We have to limit our I/O size to the maximum supported by the
1160130f4520SKenneth D. Merry 	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
1161130f4520SKenneth D. Merry 	 * set it properly, use DFLTPHYS.
1162130f4520SKenneth D. Merry 	 */
11633236151eSAlexander Motin 	if (csw) {
11643236151eSAlexander Motin 		max_iosize = dev->si_iosize_max;
1165130f4520SKenneth D. Merry 		if (max_iosize < PAGE_SIZE)
1166130f4520SKenneth D. Merry 			max_iosize = DFLTPHYS;
11673236151eSAlexander Motin 	} else
11683236151eSAlexander Motin 		max_iosize = DFLTPHYS;
1169130f4520SKenneth D. Merry 
1170130f4520SKenneth D. Merry 	cur_offset = beio->io_offset;
1171130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
1172130f4520SKenneth D. Merry 		size_t cur_size;
1173130f4520SKenneth D. Merry 		uint8_t *cur_ptr;
1174130f4520SKenneth D. Merry 
1175130f4520SKenneth D. Merry 		cur_size = beio->sg_segs[i].len;
1176130f4520SKenneth D. Merry 		cur_ptr = beio->sg_segs[i].addr;
1177130f4520SKenneth D. Merry 
1178130f4520SKenneth D. Merry 		while (cur_size > 0) {
1179130f4520SKenneth D. Merry 			/* This can't fail, it's a blocking allocation. */
1180130f4520SKenneth D. Merry 			bio = g_alloc_bio();
1181130f4520SKenneth D. Merry 
1182130f4520SKenneth D. Merry 			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
1183130f4520SKenneth D. Merry 
1184130f4520SKenneth D. Merry 			bio->bio_cmd = beio->bio_cmd;
11853236151eSAlexander Motin 			bio->bio_dev = dev;
1186130f4520SKenneth D. Merry 			bio->bio_caller1 = beio;
1187130f4520SKenneth D. Merry 			bio->bio_length = min(cur_size, max_iosize);
1188130f4520SKenneth D. Merry 			bio->bio_offset = cur_offset;
1189130f4520SKenneth D. Merry 			bio->bio_data = cur_ptr;
1190130f4520SKenneth D. Merry 			bio->bio_done = ctl_be_block_biodone;
11910bcd4ab6SAlexander Motin 			bio->bio_pblkno = cur_offset / be_lun->cbe_lun.blocksize;
1192130f4520SKenneth D. Merry 
1193130f4520SKenneth D. Merry 			cur_offset += bio->bio_length;
1194130f4520SKenneth D. Merry 			cur_ptr += bio->bio_length;
1195130f4520SKenneth D. Merry 			cur_size -= bio->bio_length;
1196130f4520SKenneth D. Merry 
119775c7a1d3SAlexander Motin 			TAILQ_INSERT_TAIL(&queue, bio, bio_queue);
1198130f4520SKenneth D. Merry 			beio->num_bios_sent++;
1199130f4520SKenneth D. Merry 		}
1200130f4520SKenneth D. Merry 	}
120175c7a1d3SAlexander Motin 	binuptime(&beio->ds_t0);
120275c7a1d3SAlexander Motin 	mtx_lock(&be_lun->io_lock);
120375c7a1d3SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
120475c7a1d3SAlexander Motin 	beio->send_complete = 1;
120575c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->io_lock);
120675c7a1d3SAlexander Motin 
120775c7a1d3SAlexander Motin 	/*
120875c7a1d3SAlexander Motin 	 * Fire off all allocated requests!
120975c7a1d3SAlexander Motin 	 */
121075c7a1d3SAlexander Motin 	while ((bio = TAILQ_FIRST(&queue)) != NULL) {
121175c7a1d3SAlexander Motin 		TAILQ_REMOVE(&queue, bio, bio_queue);
12123236151eSAlexander Motin 		if (csw)
12133236151eSAlexander Motin 			csw->d_strategy(bio);
12143236151eSAlexander Motin 		else {
12153236151eSAlexander Motin 			bio->bio_error = ENXIO;
12163236151eSAlexander Motin 			ctl_be_block_biodone(bio);
121775c7a1d3SAlexander Motin 		}
1218130f4520SKenneth D. Merry 	}
12193236151eSAlexander Motin 	if (csw)
12203236151eSAlexander Motin 		dev_relthread(dev, ref);
12213236151eSAlexander Motin }
1222130f4520SKenneth D. Merry 
1223c3e7ba3eSAlexander Motin static uint64_t
1224c3e7ba3eSAlexander Motin ctl_be_block_getattr_dev(struct ctl_be_block_lun *be_lun, const char *attrname)
1225c3e7ba3eSAlexander Motin {
1226c3e7ba3eSAlexander Motin 	struct diocgattr_arg	arg;
12273236151eSAlexander Motin 	struct cdevsw *csw;
12283236151eSAlexander Motin 	struct cdev *dev;
12293236151eSAlexander Motin 	int error, ref;
1230c3e7ba3eSAlexander Motin 
12313236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
12323236151eSAlexander Motin 	if (csw == NULL)
1233c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1234c3e7ba3eSAlexander Motin 	strlcpy(arg.name, attrname, sizeof(arg.name));
1235c3e7ba3eSAlexander Motin 	arg.len = sizeof(arg.value.off);
12363236151eSAlexander Motin 	if (csw->d_ioctl) {
12373236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
12383236151eSAlexander Motin 		    curthread);
12393236151eSAlexander Motin 	} else
12403236151eSAlexander Motin 		error = ENODEV;
12413236151eSAlexander Motin 	dev_relthread(dev, ref);
1242c3e7ba3eSAlexander Motin 	if (error != 0)
1243c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
1244c3e7ba3eSAlexander Motin 	return (arg.value.off);
1245c3e7ba3eSAlexander Motin }
1246c3e7ba3eSAlexander Motin 
1247130f4520SKenneth D. Merry static void
12487d0d4342SAlexander Motin ctl_be_block_cw_dispatch_sync(struct ctl_be_block_lun *be_lun,
12497d0d4342SAlexander Motin 			    union ctl_io *io)
12507d0d4342SAlexander Motin {
12510bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
12527d0d4342SAlexander Motin 	struct ctl_be_block_io *beio;
12537d0d4342SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
12547d0d4342SAlexander Motin 
12557d0d4342SAlexander Motin 	DPRINTF("entered\n");
12567d0d4342SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
12577d0d4342SAlexander Motin 	lbalen = (struct ctl_lba_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
12587d0d4342SAlexander Motin 
12590bcd4ab6SAlexander Motin 	beio->io_len = lbalen->len * cbe_lun->blocksize;
12600bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
12617d0d4342SAlexander Motin 	beio->io_arg = (lbalen->flags & SSC_IMMED) != 0;
12627d0d4342SAlexander Motin 	beio->bio_cmd = BIO_FLUSH;
12637d0d4342SAlexander Motin 	beio->ds_trans_type = DEVSTAT_NO_DATA;
12647d0d4342SAlexander Motin 	DPRINTF("SYNC\n");
12657d0d4342SAlexander Motin 	be_lun->lun_flush(be_lun, beio);
12667d0d4342SAlexander Motin }
12677d0d4342SAlexander Motin 
12687d0d4342SAlexander Motin static void
1269ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
1270ee7f31c0SAlexander Motin {
1271ee7f31c0SAlexander Motin 	union ctl_io *io;
1272ee7f31c0SAlexander Motin 
1273ee7f31c0SAlexander Motin 	io = beio->io;
1274ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1275ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1276ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1277ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
1278ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1279ee7f31c0SAlexander Motin 		return;
1280ee7f31c0SAlexander Motin 	}
1281ee7f31c0SAlexander Motin 
1282ee7f31c0SAlexander Motin 	ctl_be_block_config_write(io);
1283ee7f31c0SAlexander Motin }
1284ee7f31c0SAlexander Motin 
1285ee7f31c0SAlexander Motin static void
1286ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
1287ee7f31c0SAlexander Motin 			    union ctl_io *io)
1288ee7f31c0SAlexander Motin {
12890bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1290ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
129166df9136SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1292fee04ef7SAlexander Motin 	uint64_t len_left, lba;
1293fee04ef7SAlexander Motin 	uint32_t pb, pbo, adj;
1294ee7f31c0SAlexander Motin 	int i, seglen;
1295ee7f31c0SAlexander Motin 	uint8_t *buf, *end;
1296ee7f31c0SAlexander Motin 
1297ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1298ee7f31c0SAlexander Motin 
1299e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
130011b569f7SAlexander Motin 	lbalen = ARGS(beio->io);
1301ee7f31c0SAlexander Motin 
130264c5167cSAlexander Motin 	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP | SWS_ANCHOR | SWS_NDOB) ||
13033406a2a0SAlexander Motin 	    (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR) && be_lun->unmap == NULL)) {
1304ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1305ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1306ee7f31c0SAlexander Motin 				      /*sks_valid*/ 1,
1307ee7f31c0SAlexander Motin 				      /*command*/ 1,
1308ee7f31c0SAlexander Motin 				      /*field*/ 1,
1309ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1310ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1311ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1312ee7f31c0SAlexander Motin 		return;
1313ee7f31c0SAlexander Motin 	}
1314ee7f31c0SAlexander Motin 
13153406a2a0SAlexander Motin 	if (lbalen->flags & (SWS_UNMAP | SWS_ANCHOR)) {
13160bcd4ab6SAlexander Motin 		beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13170bcd4ab6SAlexander Motin 		beio->io_len = (uint64_t)lbalen->len * cbe_lun->blocksize;
1318ee7f31c0SAlexander Motin 		beio->bio_cmd = BIO_DELETE;
1319ee7f31c0SAlexander Motin 		beio->ds_trans_type = DEVSTAT_FREE;
1320ee7f31c0SAlexander Motin 
1321ee7f31c0SAlexander Motin 		be_lun->unmap(be_lun, beio);
1322ee7f31c0SAlexander Motin 		return;
1323ee7f31c0SAlexander Motin 	}
1324ee7f31c0SAlexander Motin 
1325ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_WRITE;
1326ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_WRITE;
1327ee7f31c0SAlexander Motin 
1328ee7f31c0SAlexander Motin 	DPRINTF("WRITE SAME at LBA %jx len %u\n",
132966df9136SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len);
1330ee7f31c0SAlexander Motin 
13310bcd4ab6SAlexander Motin 	pb = cbe_lun->blocksize << be_lun->cbe_lun.pblockexp;
13320bcd4ab6SAlexander Motin 	if (be_lun->cbe_lun.pblockoff > 0)
13330bcd4ab6SAlexander Motin 		pbo = pb - cbe_lun->blocksize * be_lun->cbe_lun.pblockoff;
1334fee04ef7SAlexander Motin 	else
1335fee04ef7SAlexander Motin 		pbo = 0;
13360bcd4ab6SAlexander Motin 	len_left = (uint64_t)lbalen->len * cbe_lun->blocksize;
1337ee7f31c0SAlexander Motin 	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1338ee7f31c0SAlexander Motin 
1339ee7f31c0SAlexander Motin 		/*
1340ee7f31c0SAlexander Motin 		 * Setup the S/G entry for this chunk.
1341ee7f31c0SAlexander Motin 		 */
134208a7cce5SAlexander Motin 		seglen = MIN(CTLBLK_MAX_SEG, len_left);
13430bcd4ab6SAlexander Motin 		if (pb > cbe_lun->blocksize) {
13440bcd4ab6SAlexander Motin 			adj = ((lbalen->lba + lba) * cbe_lun->blocksize +
134593b8c96cSAlexander Motin 			    seglen - pbo) % pb;
134693b8c96cSAlexander Motin 			if (seglen > adj)
134793b8c96cSAlexander Motin 				seglen -= adj;
134893b8c96cSAlexander Motin 			else
13490bcd4ab6SAlexander Motin 				seglen -= seglen % cbe_lun->blocksize;
135093b8c96cSAlexander Motin 		} else
13510bcd4ab6SAlexander Motin 			seglen -= seglen % cbe_lun->blocksize;
1352ee7f31c0SAlexander Motin 		beio->sg_segs[i].len = seglen;
1353ee7f31c0SAlexander Motin 		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1354ee7f31c0SAlexander Motin 
1355ee7f31c0SAlexander Motin 		DPRINTF("segment %d addr %p len %zd\n", i,
1356ee7f31c0SAlexander Motin 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1357ee7f31c0SAlexander Motin 
1358ee7f31c0SAlexander Motin 		beio->num_segs++;
1359ee7f31c0SAlexander Motin 		len_left -= seglen;
1360ee7f31c0SAlexander Motin 
1361ee7f31c0SAlexander Motin 		buf = beio->sg_segs[i].addr;
1362ee7f31c0SAlexander Motin 		end = buf + seglen;
13630bcd4ab6SAlexander Motin 		for (; buf < end; buf += cbe_lun->blocksize) {
13640bcd4ab6SAlexander Motin 			memcpy(buf, io->scsiio.kern_data_ptr, cbe_lun->blocksize);
136566df9136SAlexander Motin 			if (lbalen->flags & SWS_LBDATA)
136666df9136SAlexander Motin 				scsi_ulto4b(lbalen->lba + lba, buf);
1367ee7f31c0SAlexander Motin 			lba++;
1368ee7f31c0SAlexander Motin 		}
1369ee7f31c0SAlexander Motin 	}
1370ee7f31c0SAlexander Motin 
13710bcd4ab6SAlexander Motin 	beio->io_offset = lbalen->lba * cbe_lun->blocksize;
13720bcd4ab6SAlexander Motin 	beio->io_len = lba * cbe_lun->blocksize;
1373ee7f31c0SAlexander Motin 
1374ee7f31c0SAlexander Motin 	/* We can not do all in one run. Correct and schedule rerun. */
1375ee7f31c0SAlexander Motin 	if (len_left > 0) {
137666df9136SAlexander Motin 		lbalen->lba += lba;
137766df9136SAlexander Motin 		lbalen->len -= lba;
1378ee7f31c0SAlexander Motin 		beio->beio_cont = ctl_be_block_cw_done_ws;
1379ee7f31c0SAlexander Motin 	}
1380ee7f31c0SAlexander Motin 
1381ee7f31c0SAlexander Motin 	be_lun->dispatch(be_lun, beio);
1382ee7f31c0SAlexander Motin }
1383ee7f31c0SAlexander Motin 
1384ee7f31c0SAlexander Motin static void
1385ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1386ee7f31c0SAlexander Motin 			    union ctl_io *io)
1387ee7f31c0SAlexander Motin {
1388ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
138966df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1390ee7f31c0SAlexander Motin 
1391ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1392ee7f31c0SAlexander Motin 
1393e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
139466df9136SAlexander Motin 	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1395ee7f31c0SAlexander Motin 
13963406a2a0SAlexander Motin 	if ((ptrlen->flags & ~SU_ANCHOR) != 0 || be_lun->unmap == NULL) {
1397ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1398ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1399ee7f31c0SAlexander Motin 				      /*sks_valid*/ 0,
1400ee7f31c0SAlexander Motin 				      /*command*/ 1,
1401ee7f31c0SAlexander Motin 				      /*field*/ 0,
1402ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1403ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1404ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1405ee7f31c0SAlexander Motin 		return;
1406ee7f31c0SAlexander Motin 	}
1407ee7f31c0SAlexander Motin 
1408ee7f31c0SAlexander Motin 	beio->io_len = 0;
1409ee7f31c0SAlexander Motin 	beio->io_offset = -1;
1410ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_DELETE;
1411ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_FREE;
141266df9136SAlexander Motin 	DPRINTF("UNMAP\n");
1413ee7f31c0SAlexander Motin 	be_lun->unmap(be_lun, beio);
1414ee7f31c0SAlexander Motin }
1415ee7f31c0SAlexander Motin 
1416ee7f31c0SAlexander Motin static void
1417ef8daf3fSAlexander Motin ctl_be_block_cr_done(struct ctl_be_block_io *beio)
1418ef8daf3fSAlexander Motin {
1419ef8daf3fSAlexander Motin 	union ctl_io *io;
1420ef8daf3fSAlexander Motin 
1421ef8daf3fSAlexander Motin 	io = beio->io;
1422ef8daf3fSAlexander Motin 	ctl_free_beio(beio);
1423ef8daf3fSAlexander Motin 	ctl_config_read_done(io);
1424ef8daf3fSAlexander Motin }
1425ef8daf3fSAlexander Motin 
1426ef8daf3fSAlexander Motin static void
1427ef8daf3fSAlexander Motin ctl_be_block_cr_dispatch(struct ctl_be_block_lun *be_lun,
1428ef8daf3fSAlexander Motin 			 union ctl_io *io)
1429ef8daf3fSAlexander Motin {
1430ef8daf3fSAlexander Motin 	struct ctl_be_block_io *beio;
1431ef8daf3fSAlexander Motin 	struct ctl_be_block_softc *softc;
1432ef8daf3fSAlexander Motin 
1433ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
1434ef8daf3fSAlexander Motin 
1435ef8daf3fSAlexander Motin 	softc = be_lun->softc;
1436ef8daf3fSAlexander Motin 	beio = ctl_alloc_beio(softc);
1437ef8daf3fSAlexander Motin 	beio->io = io;
1438ef8daf3fSAlexander Motin 	beio->lun = be_lun;
1439ef8daf3fSAlexander Motin 	beio->beio_cont = ctl_be_block_cr_done;
1440ef8daf3fSAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1441ef8daf3fSAlexander Motin 
1442ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
1443ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:		/* GET LBA STATUS */
1444ef8daf3fSAlexander Motin 		beio->bio_cmd = -1;
1445ef8daf3fSAlexander Motin 		beio->ds_trans_type = DEVSTAT_NO_DATA;
1446ef8daf3fSAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1447ef8daf3fSAlexander Motin 		beio->io_len = 0;
1448ef8daf3fSAlexander Motin 		if (be_lun->get_lba_status)
1449ef8daf3fSAlexander Motin 			be_lun->get_lba_status(be_lun, beio);
1450ef8daf3fSAlexander Motin 		else
1451ef8daf3fSAlexander Motin 			ctl_be_block_cr_done(beio);
1452ef8daf3fSAlexander Motin 		break;
1453ef8daf3fSAlexander Motin 	default:
1454ef8daf3fSAlexander Motin 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1455ef8daf3fSAlexander Motin 		break;
1456ef8daf3fSAlexander Motin 	}
1457ef8daf3fSAlexander Motin }
1458ef8daf3fSAlexander Motin 
1459ef8daf3fSAlexander Motin static void
1460ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1461ee7f31c0SAlexander Motin {
1462ee7f31c0SAlexander Motin 	union ctl_io *io;
1463ee7f31c0SAlexander Motin 
1464ee7f31c0SAlexander Motin 	io = beio->io;
1465ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1466ee7f31c0SAlexander Motin 	ctl_config_write_done(io);
1467ee7f31c0SAlexander Motin }
1468ee7f31c0SAlexander Motin 
1469ee7f31c0SAlexander Motin static void
1470130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1471130f4520SKenneth D. Merry 			 union ctl_io *io)
1472130f4520SKenneth D. Merry {
1473130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1474130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1475130f4520SKenneth D. Merry 
1476130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1477130f4520SKenneth D. Merry 
1478130f4520SKenneth D. Merry 	softc = be_lun->softc;
1479130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1480130f4520SKenneth D. Merry 	beio->io = io;
1481130f4520SKenneth D. Merry 	beio->lun = be_lun;
1482ee7f31c0SAlexander Motin 	beio->beio_cont = ctl_be_block_cw_done;
14837d0d4342SAlexander Motin 	switch (io->scsiio.tag_type) {
14847d0d4342SAlexander Motin 	case CTL_TAG_ORDERED:
14857d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
14867d0d4342SAlexander Motin 		break;
14877d0d4342SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
14887d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
14897d0d4342SAlexander Motin 		break;
14907d0d4342SAlexander Motin 	case CTL_TAG_UNTAGGED:
14917d0d4342SAlexander Motin 	case CTL_TAG_SIMPLE:
14927d0d4342SAlexander Motin 	case CTL_TAG_ACA:
14937d0d4342SAlexander Motin 	default:
14947d0d4342SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
14957d0d4342SAlexander Motin 		break;
14967d0d4342SAlexander Motin 	}
1497e86a4142SAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1498130f4520SKenneth D. Merry 
1499130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
1500130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
1501130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
15027d0d4342SAlexander Motin 		ctl_be_block_cw_dispatch_sync(be_lun, io);
1503130f4520SKenneth D. Merry 		break;
1504ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
1505ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
1506ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_ws(be_lun, io);
1507ee7f31c0SAlexander Motin 		break;
1508ee7f31c0SAlexander Motin 	case UNMAP:
1509ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1510ee7f31c0SAlexander Motin 		break;
1511130f4520SKenneth D. Merry 	default:
1512130f4520SKenneth D. Merry 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1513130f4520SKenneth D. Merry 		break;
1514130f4520SKenneth D. Merry 	}
1515130f4520SKenneth D. Merry }
1516130f4520SKenneth D. Merry 
1517d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1518d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1519d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1520d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1521130f4520SKenneth D. Merry 
1522130f4520SKenneth D. Merry static void
152308a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio)
152408a7cce5SAlexander Motin {
152508a7cce5SAlexander Motin 	struct ctl_be_block_lun *be_lun;
152608a7cce5SAlexander Motin 	union ctl_io *io;
152708a7cce5SAlexander Motin 
152808a7cce5SAlexander Motin 	io = beio->io;
152908a7cce5SAlexander Motin 	be_lun = beio->lun;
153008a7cce5SAlexander Motin 	ctl_free_beio(beio);
1531ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1532ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1533ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
153411b569f7SAlexander Motin 		ctl_data_submit_done(io);
153508a7cce5SAlexander Motin 		return;
153608a7cce5SAlexander Motin 	}
153708a7cce5SAlexander Motin 
153808a7cce5SAlexander Motin 	io->io_hdr.status &= ~CTL_STATUS_MASK;
153908a7cce5SAlexander Motin 	io->io_hdr.status |= CTL_STATUS_NONE;
154008a7cce5SAlexander Motin 
154175c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
154208a7cce5SAlexander Motin 	/*
154308a7cce5SAlexander Motin 	 * XXX KDM make sure that links is okay to use at this point.
154408a7cce5SAlexander Motin 	 * Otherwise, we either need to add another field to ctl_io_hdr,
154508a7cce5SAlexander Motin 	 * or deal with resource allocation here.
154608a7cce5SAlexander Motin 	 */
154708a7cce5SAlexander Motin 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
154875c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
154908a7cce5SAlexander Motin 
155008a7cce5SAlexander Motin 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
155108a7cce5SAlexander Motin }
155208a7cce5SAlexander Motin 
155308a7cce5SAlexander Motin static void
1554130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1555130f4520SKenneth D. Merry 			   union ctl_io *io)
1556130f4520SKenneth D. Merry {
15570bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1558130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1559130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
156011b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1561e86a4142SAlexander Motin 	struct ctl_ptr_len_flags *bptrlen;
1562e86a4142SAlexander Motin 	uint64_t len_left, lbas;
1563130f4520SKenneth D. Merry 	int i;
1564130f4520SKenneth D. Merry 
1565130f4520SKenneth D. Merry 	softc = be_lun->softc;
1566130f4520SKenneth D. Merry 
1567130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1568130f4520SKenneth D. Merry 
156911b569f7SAlexander Motin 	lbalen = ARGS(io);
157011b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1571130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
157211b569f7SAlexander Motin 	} else {
157311b569f7SAlexander Motin 		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1574130f4520SKenneth D. Merry 	}
1575130f4520SKenneth D. Merry 
1576130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1577130f4520SKenneth D. Merry 	beio->io = io;
1578130f4520SKenneth D. Merry 	beio->lun = be_lun;
1579e86a4142SAlexander Motin 	bptrlen = PRIV(io);
1580e86a4142SAlexander Motin 	bptrlen->ptr = (void *)beio;
1581130f4520SKenneth D. Merry 
1582130f4520SKenneth D. Merry 	switch (io->scsiio.tag_type) {
1583130f4520SKenneth D. Merry 	case CTL_TAG_ORDERED:
1584130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1585130f4520SKenneth D. Merry 		break;
1586130f4520SKenneth D. Merry 	case CTL_TAG_HEAD_OF_QUEUE:
1587130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1588130f4520SKenneth D. Merry 		break;
1589130f4520SKenneth D. Merry 	case CTL_TAG_UNTAGGED:
1590130f4520SKenneth D. Merry 	case CTL_TAG_SIMPLE:
1591130f4520SKenneth D. Merry 	case CTL_TAG_ACA:
1592130f4520SKenneth D. Merry 	default:
1593130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1594130f4520SKenneth D. Merry 		break;
1595130f4520SKenneth D. Merry 	}
1596130f4520SKenneth D. Merry 
159711b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1598130f4520SKenneth D. Merry 		beio->bio_cmd = BIO_WRITE;
1599130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_WRITE;
160011b569f7SAlexander Motin 	} else {
160111b569f7SAlexander Motin 		beio->bio_cmd = BIO_READ;
160211b569f7SAlexander Motin 		beio->ds_trans_type = DEVSTAT_READ;
1603130f4520SKenneth D. Merry 	}
1604130f4520SKenneth D. Merry 
160508a7cce5SAlexander Motin 	DPRINTF("%s at LBA %jx len %u @%ju\n",
1606130f4520SKenneth D. Merry 	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1607e86a4142SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
160811b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE)
160911b569f7SAlexander Motin 		lbas = CTLBLK_HALF_IO_SIZE;
161011b569f7SAlexander Motin 	else
161111b569f7SAlexander Motin 		lbas = CTLBLK_MAX_IO_SIZE;
16120bcd4ab6SAlexander Motin 	lbas = MIN(lbalen->len - bptrlen->len, lbas / cbe_lun->blocksize);
16130bcd4ab6SAlexander Motin 	beio->io_offset = (lbalen->lba + bptrlen->len) * cbe_lun->blocksize;
16140bcd4ab6SAlexander Motin 	beio->io_len = lbas * cbe_lun->blocksize;
1615e86a4142SAlexander Motin 	bptrlen->len += lbas;
1616130f4520SKenneth D. Merry 
161708a7cce5SAlexander Motin 	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
161808a7cce5SAlexander Motin 		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
161908a7cce5SAlexander Motin 		    i, CTLBLK_MAX_SEGS));
1620130f4520SKenneth D. Merry 
1621130f4520SKenneth D. Merry 		/*
1622130f4520SKenneth D. Merry 		 * Setup the S/G entry for this chunk.
1623130f4520SKenneth D. Merry 		 */
162408a7cce5SAlexander Motin 		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1625130f4520SKenneth D. Merry 		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1626130f4520SKenneth D. Merry 
1627130f4520SKenneth D. Merry 		DPRINTF("segment %d addr %p len %zd\n", i,
1628130f4520SKenneth D. Merry 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1629130f4520SKenneth D. Merry 
163011b569f7SAlexander Motin 		/* Set up second segment for compare operation. */
163111b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_COMPARE) {
163211b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
163311b569f7SAlexander Motin 			    beio->sg_segs[i].len;
163411b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
163511b569f7SAlexander Motin 			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
163611b569f7SAlexander Motin 		}
163711b569f7SAlexander Motin 
1638130f4520SKenneth D. Merry 		beio->num_segs++;
1639130f4520SKenneth D. Merry 		len_left -= beio->sg_segs[i].len;
1640130f4520SKenneth D. Merry 	}
1641e86a4142SAlexander Motin 	if (bptrlen->len < lbalen->len)
164208a7cce5SAlexander Motin 		beio->beio_cont = ctl_be_block_next;
164308a7cce5SAlexander Motin 	io->scsiio.be_move_done = ctl_be_block_move_done;
164411b569f7SAlexander Motin 	/* For compare we have separate S/G lists for read and datamove. */
164511b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE)
164611b569f7SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
164711b569f7SAlexander Motin 	else
164808a7cce5SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
164908a7cce5SAlexander Motin 	io->scsiio.kern_data_len = beio->io_len;
165008a7cce5SAlexander Motin 	io->scsiio.kern_data_resid = 0;
165108a7cce5SAlexander Motin 	io->scsiio.kern_sg_entries = beio->num_segs;
165208a7cce5SAlexander Motin 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1653130f4520SKenneth D. Merry 
1654130f4520SKenneth D. Merry 	/*
1655130f4520SKenneth D. Merry 	 * For the read case, we need to read the data into our buffers and
1656130f4520SKenneth D. Merry 	 * then we can send it back to the user.  For the write case, we
1657130f4520SKenneth D. Merry 	 * need to get the data from the user first.
1658130f4520SKenneth D. Merry 	 */
1659130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
1660130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1661130f4520SKenneth D. Merry 		be_lun->dispatch(be_lun, beio);
1662130f4520SKenneth D. Merry 	} else {
1663130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1664130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
1665130f4520SKenneth D. Merry         	getbintime(&io->io_hdr.dma_start_bt);
1666130f4520SKenneth D. Merry #endif
1667130f4520SKenneth D. Merry 		ctl_datamove(io);
1668130f4520SKenneth D. Merry 	}
1669130f4520SKenneth D. Merry }
1670130f4520SKenneth D. Merry 
1671130f4520SKenneth D. Merry static void
1672130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending)
1673130f4520SKenneth D. Merry {
1674ee4ad294SAlexander Motin 	struct ctl_be_block_lun *be_lun = (struct ctl_be_block_lun *)context;
1675ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1676130f4520SKenneth D. Merry 	union ctl_io *io;
1677130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1678130f4520SKenneth D. Merry 
1679ee4ad294SAlexander Motin 	DPRINTF("entered\n");
1680ee4ad294SAlexander Motin 	/*
1681ee4ad294SAlexander Motin 	 * Fetch and process I/Os from all queues.  If we detect LUN
1682ee4ad294SAlexander Motin 	 * CTL_LUN_FLAG_OFFLINE status here -- it is result of a race,
1683ee4ad294SAlexander Motin 	 * so make response maximally opaque to not confuse initiator.
1684ee4ad294SAlexander Motin 	 */
1685ee4ad294SAlexander Motin 	for (;;) {
1686ee4ad294SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
1687ee4ad294SAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1688ee4ad294SAlexander Motin 		if (io != NULL) {
1689130f4520SKenneth D. Merry 			DPRINTF("datamove queue\n");
1690130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1691130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
169275c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1693e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1694ee4ad294SAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) {
1695ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1696ee4ad294SAlexander Motin 				ctl_complete_beio(beio);
1697ee4ad294SAlexander Motin 				return;
1698ee4ad294SAlexander Motin 			}
1699130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1700130f4520SKenneth D. Merry 			continue;
1701130f4520SKenneth D. Merry 		}
1702130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1703130f4520SKenneth D. Merry 		if (io != NULL) {
1704130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
1705130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1706130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
170775c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1708ee4ad294SAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) {
1709ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1710ee4ad294SAlexander Motin 				ctl_config_write_done(io);
1711ee4ad294SAlexander Motin 				return;
1712ee4ad294SAlexander Motin 			}
1713130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1714ef8daf3fSAlexander Motin 			continue;
1715ef8daf3fSAlexander Motin 		}
1716ef8daf3fSAlexander Motin 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_read_queue);
1717ef8daf3fSAlexander Motin 		if (io != NULL) {
1718ef8daf3fSAlexander Motin 			DPRINTF("config read queue\n");
1719ef8daf3fSAlexander Motin 			STAILQ_REMOVE(&be_lun->config_read_queue, &io->io_hdr,
1720ef8daf3fSAlexander Motin 				      ctl_io_hdr, links);
1721ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1722ee4ad294SAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) {
1723ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1724ee4ad294SAlexander Motin 				ctl_config_read_done(io);
1725ee4ad294SAlexander Motin 				return;
1726ee4ad294SAlexander Motin 			}
1727ef8daf3fSAlexander Motin 			ctl_be_block_cr_dispatch(be_lun, io);
1728130f4520SKenneth D. Merry 			continue;
1729130f4520SKenneth D. Merry 		}
1730130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1731130f4520SKenneth D. Merry 		if (io != NULL) {
1732130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
1733130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1734130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
173575c7a1d3SAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
1736ee4ad294SAlexander Motin 			if (cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) {
1737ee4ad294SAlexander Motin 				ctl_set_busy(&io->scsiio);
1738ee4ad294SAlexander Motin 				ctl_data_submit_done(io);
1739ee4ad294SAlexander Motin 				return;
1740ee4ad294SAlexander Motin 			}
1741130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1742130f4520SKenneth D. Merry 			continue;
1743130f4520SKenneth D. Merry 		}
1744130f4520SKenneth D. Merry 
1745130f4520SKenneth D. Merry 		/*
1746130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1747130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1748130f4520SKenneth D. Merry 		 */
1749ee4ad294SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
1750130f4520SKenneth D. Merry 		break;
1751130f4520SKenneth D. Merry 	}
1752130f4520SKenneth D. Merry }
1753130f4520SKenneth D. Merry 
1754130f4520SKenneth D. Merry /*
1755130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1756130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1757130f4520SKenneth D. Merry  * thread.
1758130f4520SKenneth D. Merry  */
1759130f4520SKenneth D. Merry static int
1760130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1761130f4520SKenneth D. Merry {
1762130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
17630bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1764130f4520SKenneth D. Merry 
1765130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1766130f4520SKenneth D. Merry 
17670bcd4ab6SAlexander Motin 	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1768130f4520SKenneth D. Merry 		CTL_PRIV_BACKEND_LUN].ptr;
17690bcd4ab6SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
1770130f4520SKenneth D. Merry 
1771130f4520SKenneth D. Merry 	/*
1772130f4520SKenneth D. Merry 	 * Make sure we only get SCSI I/O.
1773130f4520SKenneth D. Merry 	 */
1774130f4520SKenneth D. Merry 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1775130f4520SKenneth D. Merry 		"%#x) encountered", io->io_hdr.io_type));
1776130f4520SKenneth D. Merry 
1777e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1778e86a4142SAlexander Motin 
177975c7a1d3SAlexander Motin 	mtx_lock(&be_lun->queue_lock);
1780130f4520SKenneth D. Merry 	/*
1781130f4520SKenneth D. Merry 	 * XXX KDM make sure that links is okay to use at this point.
1782130f4520SKenneth D. Merry 	 * Otherwise, we either need to add another field to ctl_io_hdr,
1783130f4520SKenneth D. Merry 	 * or deal with resource allocation here.
1784130f4520SKenneth D. Merry 	 */
1785130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
178675c7a1d3SAlexander Motin 	mtx_unlock(&be_lun->queue_lock);
1787130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1788130f4520SKenneth D. Merry 
17899c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1790130f4520SKenneth D. Merry }
1791130f4520SKenneth D. Merry 
1792130f4520SKenneth D. Merry static int
1793130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1794130f4520SKenneth D. Merry 			int flag, struct thread *td)
1795130f4520SKenneth D. Merry {
1796130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1797130f4520SKenneth D. Merry 	int error;
1798130f4520SKenneth D. Merry 
1799130f4520SKenneth D. Merry 	softc = &backend_block_softc;
1800130f4520SKenneth D. Merry 
1801130f4520SKenneth D. Merry 	error = 0;
1802130f4520SKenneth D. Merry 
1803130f4520SKenneth D. Merry 	switch (cmd) {
1804130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1805130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1806130f4520SKenneth D. Merry 
1807130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1808130f4520SKenneth D. Merry 
1809130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1810130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1811130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1812130f4520SKenneth D. Merry 			break;
1813130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1814130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1815130f4520SKenneth D. Merry 			break;
181681177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
181781177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
181881177295SEdward Tomasz Napierala 			break;
1819130f4520SKenneth D. Merry 		default:
1820130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1821130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
182219720f41SAlexander Motin 				 "invalid LUN request type %d",
1823130f4520SKenneth D. Merry 				 lun_req->reqtype);
1824130f4520SKenneth D. Merry 			break;
1825130f4520SKenneth D. Merry 		}
1826130f4520SKenneth D. Merry 		break;
1827130f4520SKenneth D. Merry 	}
1828130f4520SKenneth D. Merry 	default:
1829130f4520SKenneth D. Merry 		error = ENOTTY;
1830130f4520SKenneth D. Merry 		break;
1831130f4520SKenneth D. Merry 	}
1832130f4520SKenneth D. Merry 
1833130f4520SKenneth D. Merry 	return (error);
1834130f4520SKenneth D. Merry }
1835130f4520SKenneth D. Merry 
1836130f4520SKenneth D. Merry static int
1837130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1838130f4520SKenneth D. Merry {
18390bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
1840130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1841130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
184234961f40SAlexander Motin 	char			     *value;
1843130f4520SKenneth D. Merry 	struct vattr		      vattr;
184434961f40SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos;
1845130f4520SKenneth D. Merry 	int			      error;
1846130f4520SKenneth D. Merry 
1847130f4520SKenneth D. Merry 	error = 0;
18480bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
1849130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
185019720f41SAlexander Motin 	params = &be_lun->params;
1851130f4520SKenneth D. Merry 
1852130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1853130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1854130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1855ef8daf3fSAlexander Motin 	be_lun->get_lba_status = ctl_be_block_gls_file;
185653c146deSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_file;
18570bcd4ab6SAlexander Motin 	be_lun->unmap = NULL;
18580bcd4ab6SAlexander Motin 	cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
1859130f4520SKenneth D. Merry 
1860130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1861130f4520SKenneth D. Merry 	if (error != 0) {
1862130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1863130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1864130f4520SKenneth D. Merry 			 be_lun->dev_path);
1865130f4520SKenneth D. Merry 		return (error);
1866130f4520SKenneth D. Merry 	}
1867130f4520SKenneth D. Merry 
1868130f4520SKenneth D. Merry 	/*
1869130f4520SKenneth D. Merry 	 * Verify that we have the ability to upgrade to exclusive
1870130f4520SKenneth D. Merry 	 * access on this file so we can trap errors at open instead
1871130f4520SKenneth D. Merry 	 * of reporting them during first access.
1872130f4520SKenneth D. Merry 	 */
1873130f4520SKenneth D. Merry 	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1874130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1875130f4520SKenneth D. Merry 		if (be_lun->vn->v_iflag & VI_DOOMED) {
1876130f4520SKenneth D. Merry 			error = EBADF;
1877130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
1878130f4520SKenneth D. Merry 				 "error locking file %s", be_lun->dev_path);
1879130f4520SKenneth D. Merry 			return (error);
1880130f4520SKenneth D. Merry 		}
1881130f4520SKenneth D. Merry 	}
1882130f4520SKenneth D. Merry 
1883130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
188481177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
188581177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
188681177295SEdward Tomasz Napierala 	else
1887130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1888130f4520SKenneth D. Merry 
1889130f4520SKenneth D. Merry 	/*
189020a28d6cSAlexander Motin 	 * For files we can use any logical block size.  Prefer 512 bytes
189120a28d6cSAlexander Motin 	 * for compatibility reasons.  If file's vattr.va_blocksize
189220a28d6cSAlexander Motin 	 * (preferred I/O block size) is bigger and multiple to chosen
189320a28d6cSAlexander Motin 	 * logical block size -- report it as physical block size.
1894130f4520SKenneth D. Merry 	 */
1895130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
18960bcd4ab6SAlexander Motin 		cbe_lun->blocksize = params->blocksize_bytes;
1897130f4520SKenneth D. Merry 	else
18980bcd4ab6SAlexander Motin 		cbe_lun->blocksize = 512;
18990bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
19000bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
19010bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
190234961f40SAlexander Motin 
190334961f40SAlexander Motin 	us = ps = vattr.va_blocksize;
190434961f40SAlexander Motin 	uo = po = 0;
190534961f40SAlexander Motin 
19060bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "pblocksize");
190734961f40SAlexander Motin 	if (value != NULL)
190834961f40SAlexander Motin 		ctl_expand_number(value, &ps);
19090bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "pblockoffset");
191034961f40SAlexander Motin 	if (value != NULL)
191134961f40SAlexander Motin 		ctl_expand_number(value, &po);
19120bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
19130bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
19140bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
19150bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
19160bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
19170bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
191834961f40SAlexander Motin 	}
191934961f40SAlexander Motin 
19200bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "ublocksize");
192134961f40SAlexander Motin 	if (value != NULL)
192234961f40SAlexander Motin 		ctl_expand_number(value, &us);
19230bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "ublockoffset");
192434961f40SAlexander Motin 	if (value != NULL)
192534961f40SAlexander Motin 		ctl_expand_number(value, &uo);
19260bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
19270bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
19280bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
19290bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
19300bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
19310bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
193220a28d6cSAlexander Motin 	}
1933130f4520SKenneth D. Merry 
1934130f4520SKenneth D. Merry 	/*
1935130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1936130f4520SKenneth D. Merry 	 * sector long.
1937130f4520SKenneth D. Merry 	 */
19380bcd4ab6SAlexander Motin 	if (be_lun->size_bytes < cbe_lun->blocksize) {
1939130f4520SKenneth D. Merry 		error = EINVAL;
1940130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1941130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
19420bcd4ab6SAlexander Motin 			 (uintmax_t)be_lun->size_bytes, cbe_lun->blocksize);
1943130f4520SKenneth D. Merry 	}
1944cb8727e2SAlexander Motin 
19450bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = CTLBLK_MAX_IO_SIZE / cbe_lun->blocksize;
1946130f4520SKenneth D. Merry 	return (error);
1947130f4520SKenneth D. Merry }
1948130f4520SKenneth D. Merry 
1949130f4520SKenneth D. Merry static int
1950130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1951130f4520SKenneth D. Merry {
19520bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
1953130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
19543236151eSAlexander Motin 	struct cdevsw		     *csw;
1955130f4520SKenneth D. Merry 	struct cdev		     *dev;
195634961f40SAlexander Motin 	char			     *value;
19573236151eSAlexander Motin 	int			      error, atomic, maxio, ref, unmap, tmp;
1958f6295033SAlexander Motin 	off_t			      ps, pss, po, pos, us, uss, uo, uos, otmp;
1959130f4520SKenneth D. Merry 
196019720f41SAlexander Motin 	params = &be_lun->params;
1961130f4520SKenneth D. Merry 
1962130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
19633236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
19643236151eSAlexander Motin 	if (csw == NULL)
19653236151eSAlexander Motin 		return (ENXIO);
19663236151eSAlexander Motin 	if (strcmp(csw->d_name, "zvol") == 0) {
196767f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_zvol;
1968ef8daf3fSAlexander Motin 		be_lun->get_lba_status = ctl_be_block_gls_zvol;
1969cb8727e2SAlexander Motin 		atomic = maxio = CTLBLK_MAX_IO_SIZE;
1970cb8727e2SAlexander Motin 	} else {
197167f586a8SAlexander Motin 		be_lun->dispatch = ctl_be_block_dispatch_dev;
19720bcd4ab6SAlexander Motin 		be_lun->get_lba_status = NULL;
1973cb8727e2SAlexander Motin 		atomic = 0;
19743236151eSAlexander Motin 		maxio = dev->si_iosize_max;
1975cb8727e2SAlexander Motin 		if (maxio <= 0)
1976cb8727e2SAlexander Motin 			maxio = DFLTPHYS;
1977cb8727e2SAlexander Motin 		if (maxio > CTLBLK_MAX_IO_SIZE)
1978cb8727e2SAlexander Motin 			maxio = CTLBLK_MAX_IO_SIZE;
1979cb8727e2SAlexander Motin 	}
198067f586a8SAlexander Motin 	be_lun->lun_flush = ctl_be_block_flush_dev;
1981c3e7ba3eSAlexander Motin 	be_lun->getattr = ctl_be_block_getattr_dev;
19820bcd4ab6SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
1983130f4520SKenneth D. Merry 
19843236151eSAlexander Motin 	if (!csw->d_ioctl) {
19853236151eSAlexander Motin 		dev_relthread(dev, ref);
1986130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
19873236151eSAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
1988130f4520SKenneth D. Merry 		return (ENODEV);
1989130f4520SKenneth D. Merry 	}
1990130f4520SKenneth D. Merry 
19913236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSECTORSIZE, (caddr_t)&tmp, FREAD,
1992130f4520SKenneth D. Merry 			       curthread);
1993130f4520SKenneth D. Merry 	if (error) {
19943236151eSAlexander Motin 		dev_relthread(dev, ref);
1995130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
199619720f41SAlexander Motin 			 "error %d returned for DIOCGSECTORSIZE ioctl "
199719720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
1998130f4520SKenneth D. Merry 		return (error);
1999130f4520SKenneth D. Merry 	}
2000130f4520SKenneth D. Merry 
2001130f4520SKenneth D. Merry 	/*
2002130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
2003130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
2004130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
2005130f4520SKenneth D. Merry 	 * device's blocksize.
2006130f4520SKenneth D. Merry 	 */
2007a15bbf15SAlexander Motin 	if ((params->blocksize_bytes != 0) &&
2008a15bbf15SAlexander Motin 	    (params->blocksize_bytes >= tmp)) {
2009a15bbf15SAlexander Motin 		if (params->blocksize_bytes % tmp == 0) {
20100bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
2011130f4520SKenneth D. Merry 		} else {
20123236151eSAlexander Motin 			dev_relthread(dev, ref);
2013130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
201419720f41SAlexander Motin 				 "requested blocksize %u is not an even "
2015130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
2016f6295033SAlexander Motin 				 params->blocksize_bytes, tmp);
2017130f4520SKenneth D. Merry 			return (EINVAL);
2018130f4520SKenneth D. Merry 		}
2019a15bbf15SAlexander Motin 	} else if (params->blocksize_bytes != 0) {
20203236151eSAlexander Motin 		dev_relthread(dev, ref);
2021130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
202219720f41SAlexander Motin 			 "requested blocksize %u < backing device "
2023f6295033SAlexander Motin 			 "blocksize %u", params->blocksize_bytes, tmp);
2024130f4520SKenneth D. Merry 		return (EINVAL);
2025a15bbf15SAlexander Motin 	} else
20260bcd4ab6SAlexander Motin 		cbe_lun->blocksize = tmp;
2027130f4520SKenneth D. Merry 
20283236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&otmp, FREAD,
2029130f4520SKenneth D. Merry 			     curthread);
2030130f4520SKenneth D. Merry 	if (error) {
20313236151eSAlexander Motin 		dev_relthread(dev, ref);
2032130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
203319720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE "
203419720f41SAlexander Motin 			 " ioctl on %s!", error,
203581177295SEdward Tomasz Napierala 			 be_lun->dev_path);
2036130f4520SKenneth D. Merry 		return (error);
2037130f4520SKenneth D. Merry 	}
2038130f4520SKenneth D. Merry 
203981177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
2040f6295033SAlexander Motin 		if (params->lun_size_bytes > otmp) {
20413236151eSAlexander Motin 			dev_relthread(dev, ref);
204281177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
204319720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
204419720f41SAlexander Motin 				 "size %ju",
204581177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
2046f6295033SAlexander Motin 				 (uintmax_t)otmp);
204781177295SEdward Tomasz Napierala 			return (EINVAL);
2048130f4520SKenneth D. Merry 		}
2049130f4520SKenneth D. Merry 
205081177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
2051a15bbf15SAlexander Motin 	} else
2052f6295033SAlexander Motin 		be_lun->size_bytes = otmp;
20530bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
20540bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
20550bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
205681177295SEdward Tomasz Napierala 
20573236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGSTRIPESIZE, (caddr_t)&ps, FREAD,
20583236151eSAlexander Motin 	    curthread);
2059f6012722SAlexander Motin 	if (error)
2060f6012722SAlexander Motin 		ps = po = 0;
2061f6012722SAlexander Motin 	else {
20623236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGSTRIPEOFFSET, (caddr_t)&po,
20633236151eSAlexander Motin 		    FREAD, curthread);
2064f6012722SAlexander Motin 		if (error)
2065f6012722SAlexander Motin 			po = 0;
2066f6012722SAlexander Motin 	}
206734961f40SAlexander Motin 	us = ps;
206834961f40SAlexander Motin 	uo = po;
206934961f40SAlexander Motin 
20700bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "pblocksize");
207134961f40SAlexander Motin 	if (value != NULL)
207234961f40SAlexander Motin 		ctl_expand_number(value, &ps);
20730bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "pblockoffset");
207434961f40SAlexander Motin 	if (value != NULL)
207534961f40SAlexander Motin 		ctl_expand_number(value, &po);
20760bcd4ab6SAlexander Motin 	pss = ps / cbe_lun->blocksize;
20770bcd4ab6SAlexander Motin 	pos = po / cbe_lun->blocksize;
20780bcd4ab6SAlexander Motin 	if ((pss > 0) && (pss * cbe_lun->blocksize == ps) && (pss >= pos) &&
20790bcd4ab6SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * cbe_lun->blocksize == po)) {
20800bcd4ab6SAlexander Motin 		cbe_lun->pblockexp = fls(pss) - 1;
20810bcd4ab6SAlexander Motin 		cbe_lun->pblockoff = (pss - pos) % pss;
2082f6012722SAlexander Motin 	}
2083f6012722SAlexander Motin 
20840bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "ublocksize");
208534961f40SAlexander Motin 	if (value != NULL)
208634961f40SAlexander Motin 		ctl_expand_number(value, &us);
20870bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "ublockoffset");
208834961f40SAlexander Motin 	if (value != NULL)
208934961f40SAlexander Motin 		ctl_expand_number(value, &uo);
20900bcd4ab6SAlexander Motin 	uss = us / cbe_lun->blocksize;
20910bcd4ab6SAlexander Motin 	uos = uo / cbe_lun->blocksize;
20920bcd4ab6SAlexander Motin 	if ((uss > 0) && (uss * cbe_lun->blocksize == us) && (uss >= uos) &&
20930bcd4ab6SAlexander Motin 	    ((uss & (uss - 1)) == 0) && (uos * cbe_lun->blocksize == uo)) {
20940bcd4ab6SAlexander Motin 		cbe_lun->ublockexp = fls(uss) - 1;
20950bcd4ab6SAlexander Motin 		cbe_lun->ublockoff = (uss - uos) % uss;
209634961f40SAlexander Motin 	}
209734961f40SAlexander Motin 
20980bcd4ab6SAlexander Motin 	cbe_lun->atomicblock = atomic / cbe_lun->blocksize;
20990bcd4ab6SAlexander Motin 	cbe_lun->opttxferlen = maxio / cbe_lun->blocksize;
2100fbc8d4ffSAlexander Motin 
2101fbc8d4ffSAlexander Motin 	if (be_lun->dispatch == ctl_be_block_dispatch_zvol) {
2102fbc8d4ffSAlexander Motin 		unmap = 1;
2103fbc8d4ffSAlexander Motin 	} else {
2104fbc8d4ffSAlexander Motin 		struct diocgattr_arg	arg;
2105fbc8d4ffSAlexander Motin 
2106fbc8d4ffSAlexander Motin 		strlcpy(arg.name, "GEOM::candelete", sizeof(arg.name));
2107fbc8d4ffSAlexander Motin 		arg.len = sizeof(arg.value.i);
21083236151eSAlexander Motin 		error = csw->d_ioctl(dev, DIOCGATTR, (caddr_t)&arg, FREAD,
21093236151eSAlexander Motin 		    curthread);
2110fbc8d4ffSAlexander Motin 		unmap = (error == 0) ? arg.value.i : 0;
2111fbc8d4ffSAlexander Motin 	}
21120bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "unmap");
2113fbc8d4ffSAlexander Motin 	if (value != NULL)
2114fbc8d4ffSAlexander Motin 		unmap = (strcmp(value, "on") == 0);
2115fbc8d4ffSAlexander Motin 	if (unmap)
21160bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_UNMAP;
21170bcd4ab6SAlexander Motin 	else
21180bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_UNMAP;
2119fbc8d4ffSAlexander Motin 
21203236151eSAlexander Motin 	dev_relthread(dev, ref);
212181177295SEdward Tomasz Napierala 	return (0);
212281177295SEdward Tomasz Napierala }
2123130f4520SKenneth D. Merry 
2124130f4520SKenneth D. Merry static int
2125130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
2126130f4520SKenneth D. Merry {
21270bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
21280bcd4ab6SAlexander Motin 	int flags;
2129130f4520SKenneth D. Merry 
21300bcd4ab6SAlexander Motin 	if (be_lun->vn) {
21310bcd4ab6SAlexander Motin 		flags = FREAD;
21320bcd4ab6SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_READONLY) == 0)
21330bcd4ab6SAlexander Motin 			flags |= FWRITE;
2134130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
2135130f4520SKenneth D. Merry 		be_lun->vn = NULL;
2136130f4520SKenneth D. Merry 
2137130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
2138130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
2139130f4520SKenneth D. Merry 			break;
2140130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
2141130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
2142130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
2143130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
2144130f4520SKenneth D. Merry 			}
2145130f4520SKenneth D. Merry 			break;
2146130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
2147025a2301SEdward Tomasz Napierala 			break;
2148130f4520SKenneth D. Merry 		default:
2149130f4520SKenneth D. Merry 			panic("Unexpected backend type.");
2150130f4520SKenneth D. Merry 			break;
2151130f4520SKenneth D. Merry 		}
215219720f41SAlexander Motin 		be_lun->dev_type = CTL_BE_BLOCK_NONE;
2153130f4520SKenneth D. Merry 	}
2154130f4520SKenneth D. Merry 	return (0);
2155130f4520SKenneth D. Merry }
2156130f4520SKenneth D. Merry 
2157130f4520SKenneth D. Merry static int
2158130f4520SKenneth D. Merry ctl_be_block_open(struct ctl_be_block_softc *softc,
2159130f4520SKenneth D. Merry 		  struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
2160130f4520SKenneth D. Merry {
21610bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
2162130f4520SKenneth D. Merry 	struct nameidata nd;
21630bcd4ab6SAlexander Motin 	char		*value;
21640bcd4ab6SAlexander Motin 	int		 error, flags;
2165130f4520SKenneth D. Merry 
2166130f4520SKenneth D. Merry 	error = 0;
2167130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
2168130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
216919720f41SAlexander Motin 			 "Root filesystem is not mounted");
2170130f4520SKenneth D. Merry 		return (1);
2171130f4520SKenneth D. Merry 	}
21728a08cec1SMateusz Guzik 	pwd_ensure_dirs();
2173130f4520SKenneth D. Merry 
21740bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "file");
21750bcd4ab6SAlexander Motin 	if (value == NULL) {
21760bcd4ab6SAlexander Motin 		snprintf(req->error_str, sizeof(req->error_str),
21770bcd4ab6SAlexander Motin 			 "no file argument specified");
21780bcd4ab6SAlexander Motin 		return (1);
21790bcd4ab6SAlexander Motin 	}
21800bcd4ab6SAlexander Motin 	free(be_lun->dev_path, M_CTLBLK);
21810bcd4ab6SAlexander Motin 	be_lun->dev_path = strdup(value, M_CTLBLK);
21820bcd4ab6SAlexander Motin 
21830bcd4ab6SAlexander Motin 	flags = FREAD;
21840bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "readonly");
21850bcd4ab6SAlexander Motin 	if (value == NULL || strcmp(value, "on") != 0)
21860bcd4ab6SAlexander Motin 		flags |= FWRITE;
21870bcd4ab6SAlexander Motin 
2188130f4520SKenneth D. Merry again:
2189130f4520SKenneth D. Merry 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
2190130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
21910bcd4ab6SAlexander Motin 	if ((error == EROFS || error == EACCES) && (flags & FWRITE)) {
21920bcd4ab6SAlexander Motin 		flags &= ~FWRITE;
21930bcd4ab6SAlexander Motin 		goto again;
21940bcd4ab6SAlexander Motin 	}
2195130f4520SKenneth D. Merry 	if (error) {
2196130f4520SKenneth D. Merry 		/*
2197130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
2198130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
2199130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
2200130f4520SKenneth D. Merry 		 * full path.
2201130f4520SKenneth D. Merry 		 */
2202130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
2203130f4520SKenneth D. Merry 			char *dev_name;
2204130f4520SKenneth D. Merry 
22050bcd4ab6SAlexander Motin 			asprintf(&dev_name, M_CTLBLK, "/dev/%s",
2206130f4520SKenneth D. Merry 				be_lun->dev_path);
2207130f4520SKenneth D. Merry 			free(be_lun->dev_path, M_CTLBLK);
2208130f4520SKenneth D. Merry 			be_lun->dev_path = dev_name;
2209130f4520SKenneth D. Merry 			goto again;
2210130f4520SKenneth D. Merry 		}
2211130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
221219720f41SAlexander Motin 		    "error opening %s: %d", be_lun->dev_path, error);
2213130f4520SKenneth D. Merry 		return (error);
2214130f4520SKenneth D. Merry 	}
22150bcd4ab6SAlexander Motin 	if (flags & FWRITE)
22160bcd4ab6SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_READONLY;
22170bcd4ab6SAlexander Motin 	else
22180bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_READONLY;
2219130f4520SKenneth D. Merry 
2220130f4520SKenneth D. Merry 	NDFREE(&nd, NDF_ONLY_PNBUF);
2221130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
2222130f4520SKenneth D. Merry 
2223130f4520SKenneth D. Merry 	/* We only support disks and files. */
2224130f4520SKenneth D. Merry 	if (vn_isdisk(be_lun->vn, &error)) {
2225130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
2226130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
2227130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
2228130f4520SKenneth D. Merry 	} else {
2229130f4520SKenneth D. Merry 		error = EINVAL;
2230130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2231025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
2232130f4520SKenneth D. Merry 	}
2233130f4520SKenneth D. Merry 	VOP_UNLOCK(be_lun->vn, 0);
2234130f4520SKenneth D. Merry 
2235a15bbf15SAlexander Motin 	if (error != 0)
2236130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
22370bcd4ab6SAlexander Motin 	cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
22380bcd4ab6SAlexander Motin 	if (be_lun->dispatch != ctl_be_block_dispatch_dev)
22390bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
22400bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "serseq");
22410bcd4ab6SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
22420bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_ON;
22430bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "read") == 0)
22440bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_READ;
22450bcd4ab6SAlexander Motin 	else if (value != NULL && strcmp(value, "off") == 0)
22460bcd4ab6SAlexander Motin 		cbe_lun->serseq = CTL_LUN_SERSEQ_OFF;
2247130f4520SKenneth D. Merry 	return (0);
2248130f4520SKenneth D. Merry }
2249130f4520SKenneth D. Merry 
2250130f4520SKenneth D. Merry static int
2251130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2252130f4520SKenneth D. Merry {
22530bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2254130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2255130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
225657a5db13SAlexander Motin 	char num_thread_str[16];
2257130f4520SKenneth D. Merry 	char tmpstr[32];
225857a5db13SAlexander Motin 	char *value;
2259fbc8d4ffSAlexander Motin 	int retval, num_threads;
226057a5db13SAlexander Motin 	int tmp_num_threads;
2261130f4520SKenneth D. Merry 
2262130f4520SKenneth D. Merry 	params = &req->reqdata.create;
2263130f4520SKenneth D. Merry 	retval = 0;
226419720f41SAlexander Motin 	req->status = CTL_LUN_OK;
2265130f4520SKenneth D. Merry 
2266130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
22670bcd4ab6SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
22680bcd4ab6SAlexander Motin 	cbe_lun->be_lun = be_lun;
226919720f41SAlexander Motin 	be_lun->params = req->reqdata.create;
2270130f4520SKenneth D. Merry 	be_lun->softc = softc;
2271130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
2272ef8daf3fSAlexander Motin 	STAILQ_INIT(&be_lun->config_read_queue);
2273130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
2274130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
2275130f4520SKenneth D. Merry 	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
227675c7a1d3SAlexander Motin 	mtx_init(&be_lun->io_lock, "cblk io lock", NULL, MTX_DEF);
227775c7a1d3SAlexander Motin 	mtx_init(&be_lun->queue_lock, "cblk queue lock", NULL, MTX_DEF);
22780bcd4ab6SAlexander Motin 	ctl_init_opts(&cbe_lun->options,
227943fb3a65SAlexander Motin 	    req->num_be_args, req->kern_be_args);
228008a7cce5SAlexander Motin 	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
2281eeb94054SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
2282130f4520SKenneth D. Merry 	if (be_lun->lun_zone == NULL) {
2283130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
228419720f41SAlexander Motin 			 "error allocating UMA zone");
2285130f4520SKenneth D. Merry 		goto bailout_error;
2286130f4520SKenneth D. Merry 	}
2287130f4520SKenneth D. Merry 
2288130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
22890bcd4ab6SAlexander Motin 		cbe_lun->lun_type = params->device_type;
2290130f4520SKenneth D. Merry 	else
22910bcd4ab6SAlexander Motin 		cbe_lun->lun_type = T_DIRECT;
22920bcd4ab6SAlexander Motin 	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
22937ac58230SAlexander Motin 	cbe_lun->flags = 0;
22947ac58230SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "ha_role");
22957ac58230SAlexander Motin 	if (value != NULL) {
22967ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
22977ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
22987ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
22997ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
2300130f4520SKenneth D. Merry 
23010bcd4ab6SAlexander Motin 	if (cbe_lun->lun_type == T_DIRECT) {
2302a15bbf15SAlexander Motin 		be_lun->size_bytes = params->lun_size_bytes;
2303a15bbf15SAlexander Motin 		if (params->blocksize_bytes != 0)
23040bcd4ab6SAlexander Motin 			cbe_lun->blocksize = params->blocksize_bytes;
2305a15bbf15SAlexander Motin 		else
23060bcd4ab6SAlexander Motin 			cbe_lun->blocksize = 512;
23070bcd4ab6SAlexander Motin 		be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
23080bcd4ab6SAlexander Motin 		cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
23090bcd4ab6SAlexander Motin 		    0 : (be_lun->size_blocks - 1);
2310130f4520SKenneth D. Merry 
23117ac58230SAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
23127ac58230SAlexander Motin 		    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
2313130f4520SKenneth D. Merry 			retval = ctl_be_block_open(softc, be_lun, req);
2314130f4520SKenneth D. Merry 			if (retval != 0) {
2315130f4520SKenneth D. Merry 				retval = 0;
231619720f41SAlexander Motin 				req->status = CTL_LUN_WARNING;
2317130f4520SKenneth D. Merry 			}
23187ac58230SAlexander Motin 		}
23190bcd4ab6SAlexander Motin 		num_threads = cbb_num_threads;
2320130f4520SKenneth D. Merry 	} else {
2321130f4520SKenneth D. Merry 		num_threads = 1;
2322130f4520SKenneth D. Merry 	}
2323130f4520SKenneth D. Merry 
2324130f4520SKenneth D. Merry 	/*
2325130f4520SKenneth D. Merry 	 * XXX This searching loop might be refactored to be combined with
2326130f4520SKenneth D. Merry 	 * the loop above,
2327130f4520SKenneth D. Merry 	 */
23280bcd4ab6SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "num_threads");
232957a5db13SAlexander Motin 	if (value != NULL) {
233057a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
2331130f4520SKenneth D. Merry 
2332130f4520SKenneth D. Merry 		/*
2333130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
2334130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
2335130f4520SKenneth D. Merry 		 * specify 1000 threads.
2336130f4520SKenneth D. Merry 		 */
2337130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
2338130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
233919720f41SAlexander Motin 				 "invalid number of threads %s",
234019720f41SAlexander Motin 				 num_thread_str);
2341130f4520SKenneth D. Merry 			goto bailout_error;
2342130f4520SKenneth D. Merry 		}
2343130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
234457a5db13SAlexander Motin 	}
2345130f4520SKenneth D. Merry 
234619720f41SAlexander Motin 	if (be_lun->vn == NULL)
23470bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE;
2348130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
234919720f41SAlexander Motin 	params->lun_size_bytes = be_lun->size_bytes;
23500bcd4ab6SAlexander Motin 	params->blocksize_bytes = cbe_lun->blocksize;
2351130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
23520bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = params->req_lun_id;
23530bcd4ab6SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_ID_REQ;
2354130f4520SKenneth D. Merry 	} else
23550bcd4ab6SAlexander Motin 		cbe_lun->req_lun_id = 0;
2356130f4520SKenneth D. Merry 
23570bcd4ab6SAlexander Motin 	cbe_lun->lun_shutdown = ctl_be_block_lun_shutdown;
23580bcd4ab6SAlexander Motin 	cbe_lun->lun_config_status = ctl_be_block_lun_config_status;
23590bcd4ab6SAlexander Motin 	cbe_lun->be = &ctl_be_block_driver;
2360130f4520SKenneth D. Merry 
2361130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
2362130f4520SKenneth D. Merry 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
2363130f4520SKenneth D. Merry 			 softc->num_luns);
23640bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, tmpstr,
23650bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num), sizeof(tmpstr)));
2366130f4520SKenneth D. Merry 
2367130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
2368130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
2369e7038eb7SAlexander Motin 			MIN(sizeof(params->serial_num), sizeof(tmpstr)));
2370130f4520SKenneth D. Merry 	} else {
23710bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->serial_num, params->serial_num,
23720bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->serial_num),
2373130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
2374130f4520SKenneth D. Merry 	}
2375130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
2376130f4520SKenneth D. Merry 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
23770bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, tmpstr,
23780bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id), sizeof(tmpstr)));
2379130f4520SKenneth D. Merry 
2380130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2381130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2382e7038eb7SAlexander Motin 			MIN(sizeof(params->device_id), sizeof(tmpstr)));
2383130f4520SKenneth D. Merry 	} else {
23840bcd4ab6SAlexander Motin 		strncpy((char *)cbe_lun->device_id, params->device_id,
23850bcd4ab6SAlexander Motin 			MIN(sizeof(cbe_lun->device_id),
2386130f4520SKenneth D. Merry 			    sizeof(params->device_id)));
2387130f4520SKenneth D. Merry 	}
2388130f4520SKenneth D. Merry 
2389130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2390130f4520SKenneth D. Merry 
2391130f4520SKenneth D. Merry 	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2392130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2393130f4520SKenneth D. Merry 
2394130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2395130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
239619720f41SAlexander Motin 			 "unable to create taskqueue");
2397130f4520SKenneth D. Merry 		goto bailout_error;
2398130f4520SKenneth D. Merry 	}
2399130f4520SKenneth D. Merry 
2400130f4520SKenneth D. Merry 	/*
2401130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2402130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2403130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2404130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2405130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2406130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2407130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2408130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2409130f4520SKenneth D. Merry 	 *
2410130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2411130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2412130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2413130f4520SKenneth D. Merry 	 */
2414130f4520SKenneth D. Merry 	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2415130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2416130f4520SKenneth D. Merry 					 /*priority*/PWAIT,
2417130f4520SKenneth D. Merry 					 /*thread name*/
2418130f4520SKenneth D. Merry 					 "%s taskq", be_lun->lunname);
2419130f4520SKenneth D. Merry 
2420130f4520SKenneth D. Merry 	if (retval != 0)
2421130f4520SKenneth D. Merry 		goto bailout_error;
2422130f4520SKenneth D. Merry 
2423130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2424130f4520SKenneth D. Merry 
2425130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2426130f4520SKenneth D. Merry 	softc->num_luns++;
2427130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2428130f4520SKenneth D. Merry 
2429130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2430130f4520SKenneth D. Merry 
24310bcd4ab6SAlexander Motin 	retval = ctl_add_lun(&be_lun->cbe_lun);
2432130f4520SKenneth D. Merry 	if (retval != 0) {
2433130f4520SKenneth D. Merry 		mtx_lock(&softc->lock);
2434130f4520SKenneth D. Merry 		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2435130f4520SKenneth D. Merry 			      links);
2436130f4520SKenneth D. Merry 		softc->num_luns--;
2437130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2438130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
243919720f41SAlexander Motin 			 "ctl_add_lun() returned error %d, see dmesg for "
244019720f41SAlexander Motin 			 "details", retval);
2441130f4520SKenneth D. Merry 		retval = 0;
2442130f4520SKenneth D. Merry 		goto bailout_error;
2443130f4520SKenneth D. Merry 	}
2444130f4520SKenneth D. Merry 
2445130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2446130f4520SKenneth D. Merry 
2447130f4520SKenneth D. Merry 	/*
2448130f4520SKenneth D. Merry 	 * Tell the config_status routine that we're waiting so it won't
2449130f4520SKenneth D. Merry 	 * clean up the LUN in the event of an error.
2450130f4520SKenneth D. Merry 	 */
2451130f4520SKenneth D. Merry 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2452130f4520SKenneth D. Merry 
2453130f4520SKenneth D. Merry 	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2454130f4520SKenneth D. Merry 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2455130f4520SKenneth D. Merry 		if (retval == EINTR)
2456130f4520SKenneth D. Merry 			break;
2457130f4520SKenneth D. Merry 	}
2458130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2459130f4520SKenneth D. Merry 
2460130f4520SKenneth D. Merry 	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2461130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
246219720f41SAlexander Motin 			 "LUN configuration error, see dmesg for details");
2463130f4520SKenneth D. Merry 		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2464130f4520SKenneth D. Merry 			      links);
2465130f4520SKenneth D. Merry 		softc->num_luns--;
2466130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2467130f4520SKenneth D. Merry 		goto bailout_error;
2468130f4520SKenneth D. Merry 	} else {
24690bcd4ab6SAlexander Motin 		params->req_lun_id = cbe_lun->lun_id;
2470130f4520SKenneth D. Merry 	}
2471130f4520SKenneth D. Merry 
2472130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2473130f4520SKenneth D. Merry 
2474130f4520SKenneth D. Merry 	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
24750bcd4ab6SAlexander Motin 					       cbe_lun->blocksize,
2476130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
24770bcd4ab6SAlexander Motin 					       cbe_lun->lun_type
2478130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2479130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2480130f4520SKenneth D. Merry 
2481130f4520SKenneth D. Merry 	return (retval);
2482130f4520SKenneth D. Merry 
2483130f4520SKenneth D. Merry bailout_error:
2484130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2485130f4520SKenneth D. Merry 
24869e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
24879e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2488130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
24899e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2490130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
24919e005bbcSAlexander Motin 	if (be_lun->lun_zone != NULL)
24929e005bbcSAlexander Motin 		uma_zdestroy(be_lun->lun_zone);
24930bcd4ab6SAlexander Motin 	ctl_free_opts(&cbe_lun->options);
249475c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
249575c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2496130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2497130f4520SKenneth D. Merry 
2498130f4520SKenneth D. Merry 	return (retval);
2499130f4520SKenneth D. Merry }
2500130f4520SKenneth D. Merry 
2501130f4520SKenneth D. Merry static int
2502130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2503130f4520SKenneth D. Merry {
2504130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2505130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2506ee4ad294SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2507130f4520SKenneth D. Merry 	int retval;
2508130f4520SKenneth D. Merry 
2509130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2510130f4520SKenneth D. Merry 
2511130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2512130f4520SKenneth D. Merry 	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
25130bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
2514130f4520SKenneth D. Merry 			break;
2515130f4520SKenneth D. Merry 	}
2516130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2517130f4520SKenneth D. Merry 
2518130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2519130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
252019720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
252119720f41SAlexander Motin 			 params->lun_id);
2522130f4520SKenneth D. Merry 		goto bailout_error;
2523130f4520SKenneth D. Merry 	}
2524ee4ad294SAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
2525130f4520SKenneth D. Merry 
2526ee4ad294SAlexander Motin 	retval = ctl_disable_lun(cbe_lun);
2527130f4520SKenneth D. Merry 	if (retval != 0) {
2528130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
252919720f41SAlexander Motin 			 "error %d returned from ctl_disable_lun() for "
253019720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
2531130f4520SKenneth D. Merry 		goto bailout_error;
2532130f4520SKenneth D. Merry 	}
2533130f4520SKenneth D. Merry 
2534ee4ad294SAlexander Motin 	if (be_lun->vn != NULL) {
2535ee4ad294SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE;
2536ee4ad294SAlexander Motin 		ctl_lun_offline(cbe_lun);
2537ee4ad294SAlexander Motin 		taskqueue_drain_all(be_lun->io_taskqueue);
2538ee4ad294SAlexander Motin 		ctl_be_block_close(be_lun);
2539ee4ad294SAlexander Motin 	}
2540ee4ad294SAlexander Motin 
2541ee4ad294SAlexander Motin 	retval = ctl_invalidate_lun(cbe_lun);
2542130f4520SKenneth D. Merry 	if (retval != 0) {
2543130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
254419720f41SAlexander Motin 			 "error %d returned from ctl_invalidate_lun() for "
254519720f41SAlexander Motin 			 "LUN %d", retval, params->lun_id);
2546130f4520SKenneth D. Merry 		goto bailout_error;
2547130f4520SKenneth D. Merry 	}
2548130f4520SKenneth D. Merry 
2549130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2550130f4520SKenneth D. Merry 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2551130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2552130f4520SKenneth D. Merry                 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2553130f4520SKenneth D. Merry                 if (retval == EINTR)
2554130f4520SKenneth D. Merry                         break;
2555130f4520SKenneth D. Merry         }
2556130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2557130f4520SKenneth D. Merry 
2558130f4520SKenneth D. Merry 	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2559130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
256019720f41SAlexander Motin 			 "interrupted waiting for LUN to be freed");
2561130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2562130f4520SKenneth D. Merry 		goto bailout_error;
2563130f4520SKenneth D. Merry 	}
2564130f4520SKenneth D. Merry 
2565130f4520SKenneth D. Merry 	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2566130f4520SKenneth D. Merry 
2567130f4520SKenneth D. Merry 	softc->num_luns--;
2568130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2569130f4520SKenneth D. Merry 
2570ee4ad294SAlexander Motin 	taskqueue_drain_all(be_lun->io_taskqueue);
2571130f4520SKenneth D. Merry 	taskqueue_free(be_lun->io_taskqueue);
2572130f4520SKenneth D. Merry 
2573130f4520SKenneth D. Merry 	if (be_lun->disk_stats != NULL)
2574130f4520SKenneth D. Merry 		devstat_remove_entry(be_lun->disk_stats);
2575130f4520SKenneth D. Merry 
2576130f4520SKenneth D. Merry 	uma_zdestroy(be_lun->lun_zone);
2577130f4520SKenneth D. Merry 
2578ee4ad294SAlexander Motin 	ctl_free_opts(&cbe_lun->options);
2579130f4520SKenneth D. Merry 	free(be_lun->dev_path, M_CTLBLK);
258075c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->queue_lock);
258175c7a1d3SAlexander Motin 	mtx_destroy(&be_lun->io_lock);
2582130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2583130f4520SKenneth D. Merry 
2584130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2585130f4520SKenneth D. Merry 
2586130f4520SKenneth D. Merry 	return (0);
2587130f4520SKenneth D. Merry 
2588130f4520SKenneth D. Merry bailout_error:
2589130f4520SKenneth D. Merry 
2590130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2591130f4520SKenneth D. Merry 
2592130f4520SKenneth D. Merry 	return (0);
2593130f4520SKenneth D. Merry }
2594130f4520SKenneth D. Merry 
259581177295SEdward Tomasz Napierala static int
259681177295SEdward Tomasz Napierala ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
259781177295SEdward Tomasz Napierala 			 struct ctl_lun_req *req)
259881177295SEdward Tomasz Napierala {
25990bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
260081177295SEdward Tomasz Napierala 	struct vattr vattr;
260181177295SEdward Tomasz Napierala 	int error;
260219720f41SAlexander Motin 	struct ctl_lun_create_params *params = &be_lun->params;
260381177295SEdward Tomasz Napierala 
260481177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
260581177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
260681177295SEdward Tomasz Napierala 	} else  {
260771d8e97eSAlexander Motin 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
260881177295SEdward Tomasz Napierala 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
260971d8e97eSAlexander Motin 		VOP_UNLOCK(be_lun->vn, 0);
261081177295SEdward Tomasz Napierala 		if (error != 0) {
261181177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
261281177295SEdward Tomasz Napierala 				 "error calling VOP_GETATTR() for file %s",
261381177295SEdward Tomasz Napierala 				 be_lun->dev_path);
261481177295SEdward Tomasz Napierala 			return (error);
261581177295SEdward Tomasz Napierala 		}
261681177295SEdward Tomasz Napierala 		be_lun->size_bytes = vattr.va_size;
261781177295SEdward Tomasz Napierala 	}
26180bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
26190bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
26200bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
262181177295SEdward Tomasz Napierala 	return (0);
262281177295SEdward Tomasz Napierala }
262381177295SEdward Tomasz Napierala 
262481177295SEdward Tomasz Napierala static int
262581177295SEdward Tomasz Napierala ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
262681177295SEdward Tomasz Napierala 			struct ctl_lun_req *req)
262781177295SEdward Tomasz Napierala {
26280bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun = &be_lun->cbe_lun;
262919720f41SAlexander Motin 	struct ctl_lun_create_params *params = &be_lun->params;
26303236151eSAlexander Motin 	struct cdevsw *csw;
26313236151eSAlexander Motin 	struct cdev *dev;
263281177295SEdward Tomasz Napierala 	uint64_t size_bytes;
26333236151eSAlexander Motin 	int error, ref;
263481177295SEdward Tomasz Napierala 
26353236151eSAlexander Motin 	csw = devvn_refthread(be_lun->vn, &dev, &ref);
26363236151eSAlexander Motin 	if (csw == NULL)
26373236151eSAlexander Motin 		return (ENXIO);
26383236151eSAlexander Motin 	if (csw->d_ioctl == NULL) {
26393236151eSAlexander Motin 		dev_relthread(dev, ref);
264081177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
264119720f41SAlexander Motin 			 "no d_ioctl for device %s!", be_lun->dev_path);
264281177295SEdward Tomasz Napierala 		return (ENODEV);
264381177295SEdward Tomasz Napierala 	}
264481177295SEdward Tomasz Napierala 
26453236151eSAlexander Motin 	error = csw->d_ioctl(dev, DIOCGMEDIASIZE, (caddr_t)&size_bytes, FREAD,
264681177295SEdward Tomasz Napierala 	    curthread);
26473236151eSAlexander Motin 	dev_relthread(dev, ref);
264881177295SEdward Tomasz Napierala 	if (error) {
264981177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
265019720f41SAlexander Motin 			 "error %d returned for DIOCGMEDIASIZE ioctl "
265119720f41SAlexander Motin 			 "on %s!", error, be_lun->dev_path);
265281177295SEdward Tomasz Napierala 		return (error);
265381177295SEdward Tomasz Napierala 	}
265481177295SEdward Tomasz Napierala 
265581177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
265681177295SEdward Tomasz Napierala 		if (params->lun_size_bytes > size_bytes) {
265781177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
265819720f41SAlexander Motin 				 "requested LUN size %ju > backing device "
265919720f41SAlexander Motin 				 "size %ju",
266081177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
266181177295SEdward Tomasz Napierala 				 (uintmax_t)size_bytes);
266281177295SEdward Tomasz Napierala 			return (EINVAL);
266381177295SEdward Tomasz Napierala 		}
266481177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
266581177295SEdward Tomasz Napierala 	} else {
266681177295SEdward Tomasz Napierala 		be_lun->size_bytes = size_bytes;
266781177295SEdward Tomasz Napierala 	}
26680bcd4ab6SAlexander Motin 	be_lun->size_blocks = be_lun->size_bytes / cbe_lun->blocksize;
26690bcd4ab6SAlexander Motin 	cbe_lun->maxlba = (be_lun->size_blocks == 0) ?
26700bcd4ab6SAlexander Motin 	    0 : (be_lun->size_blocks - 1);
267181177295SEdward Tomasz Napierala 	return (0);
267281177295SEdward Tomasz Napierala }
267381177295SEdward Tomasz Napierala 
267481177295SEdward Tomasz Napierala static int
267581177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
267681177295SEdward Tomasz Napierala {
267781177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
267881177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
2679a3977beaSAlexander Motin 	struct ctl_be_lun *cbe_lun;
26807ac58230SAlexander Motin 	char *value;
268171d8e97eSAlexander Motin 	uint64_t oldsize;
26827ac58230SAlexander Motin 	int error, wasprim;
268381177295SEdward Tomasz Napierala 
268481177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
268581177295SEdward Tomasz Napierala 
268681177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
268781177295SEdward Tomasz Napierala 	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
26880bcd4ab6SAlexander Motin 		if (be_lun->cbe_lun.lun_id == params->lun_id)
268981177295SEdward Tomasz Napierala 			break;
269081177295SEdward Tomasz Napierala 	}
269181177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
269281177295SEdward Tomasz Napierala 
269381177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
269481177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
269519720f41SAlexander Motin 			 "LUN %u is not managed by the block backend",
269619720f41SAlexander Motin 			 params->lun_id);
269781177295SEdward Tomasz Napierala 		goto bailout_error;
269881177295SEdward Tomasz Napierala 	}
2699a3977beaSAlexander Motin 	cbe_lun = &be_lun->cbe_lun;
270081177295SEdward Tomasz Napierala 
2701a3977beaSAlexander Motin 	if (params->lun_size_bytes != 0)
270219720f41SAlexander Motin 		be_lun->params.lun_size_bytes = params->lun_size_bytes;
2703a3977beaSAlexander Motin 	ctl_update_opts(&cbe_lun->options, req->num_be_args, req->kern_be_args);
270481177295SEdward Tomasz Napierala 
27057ac58230SAlexander Motin 	wasprim = (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY);
27067ac58230SAlexander Motin 	value = ctl_get_opt(&cbe_lun->options, "ha_role");
27077ac58230SAlexander Motin 	if (value != NULL) {
27087ac58230SAlexander Motin 		if (strcmp(value, "primary") == 0)
27097ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
27107ac58230SAlexander Motin 		else
27117ac58230SAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
27127ac58230SAlexander Motin 	} else if (control_softc->flags & CTL_FLAG_ACTIVE_SHELF)
27137ac58230SAlexander Motin 		cbe_lun->flags |= CTL_LUN_FLAG_PRIMARY;
27147ac58230SAlexander Motin 	else
27157ac58230SAlexander Motin 		cbe_lun->flags &= ~CTL_LUN_FLAG_PRIMARY;
27167ac58230SAlexander Motin 	if (wasprim != (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)) {
27177ac58230SAlexander Motin 		if (cbe_lun->flags & CTL_LUN_FLAG_PRIMARY)
27187ac58230SAlexander Motin 			ctl_lun_primary(cbe_lun);
27197ac58230SAlexander Motin 		else
27207ac58230SAlexander Motin 			ctl_lun_secondary(cbe_lun);
27217ac58230SAlexander Motin 	}
27227ac58230SAlexander Motin 
27230bcd4ab6SAlexander Motin 	oldsize = be_lun->size_blocks;
27247ac58230SAlexander Motin 	if ((cbe_lun->flags & CTL_LUN_FLAG_PRIMARY) ||
27257ac58230SAlexander Motin 	    control_softc->ha_mode == CTL_HA_MODE_SER_ONLY) {
272619720f41SAlexander Motin 		if (be_lun->vn == NULL)
272719720f41SAlexander Motin 			error = ctl_be_block_open(softc, be_lun, req);
2728b9b4269cSAlexander Motin 		else if (vn_isdisk(be_lun->vn, &error))
2729b9b4269cSAlexander Motin 			error = ctl_be_block_modify_dev(be_lun, req);
273019720f41SAlexander Motin 		else if (be_lun->vn->v_type == VREG)
273181177295SEdward Tomasz Napierala 			error = ctl_be_block_modify_file(be_lun, req);
273281177295SEdward Tomasz Napierala 		else
2733b9b4269cSAlexander Motin 			error = EINVAL;
2734a3977beaSAlexander Motin 		if ((cbe_lun->flags & CTL_LUN_FLAG_OFFLINE) &&
27350bcd4ab6SAlexander Motin 		    be_lun->vn != NULL) {
2736a3977beaSAlexander Motin 			cbe_lun->flags &= ~CTL_LUN_FLAG_OFFLINE;
2737a3977beaSAlexander Motin 			ctl_lun_online(cbe_lun);
273871d8e97eSAlexander Motin 		}
27397ac58230SAlexander Motin 	} else {
27407ac58230SAlexander Motin 		if (be_lun->vn != NULL) {
27417ac58230SAlexander Motin 			cbe_lun->flags |= CTL_LUN_FLAG_OFFLINE;
27427ac58230SAlexander Motin 			ctl_lun_offline(cbe_lun);
2743ee4ad294SAlexander Motin 			taskqueue_drain_all(be_lun->io_taskqueue);
27447ac58230SAlexander Motin 			error = ctl_be_block_close(be_lun);
27457ac58230SAlexander Motin 		} else
27467ac58230SAlexander Motin 			error = 0;
27477ac58230SAlexander Motin 	}
27487ac58230SAlexander Motin 	if (be_lun->size_blocks != oldsize)
27497ac58230SAlexander Motin 		ctl_lun_capacity_changed(cbe_lun);
275081177295SEdward Tomasz Napierala 
275181177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
275281177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
275381177295SEdward Tomasz Napierala 
275419720f41SAlexander Motin 	req->status = error ? CTL_LUN_WARNING : CTL_LUN_OK;
275581177295SEdward Tomasz Napierala 	return (0);
275681177295SEdward Tomasz Napierala 
275781177295SEdward Tomasz Napierala bailout_error:
275881177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
275981177295SEdward Tomasz Napierala 	return (0);
276081177295SEdward Tomasz Napierala }
276181177295SEdward Tomasz Napierala 
2762130f4520SKenneth D. Merry static void
2763130f4520SKenneth D. Merry ctl_be_block_lun_shutdown(void *be_lun)
2764130f4520SKenneth D. Merry {
2765130f4520SKenneth D. Merry 	struct ctl_be_block_lun *lun;
2766130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
2767130f4520SKenneth D. Merry 
2768130f4520SKenneth D. Merry 	lun = (struct ctl_be_block_lun *)be_lun;
2769130f4520SKenneth D. Merry 
2770130f4520SKenneth D. Merry 	softc = lun->softc;
2771130f4520SKenneth D. Merry 
2772130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2773130f4520SKenneth D. Merry 	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2774130f4520SKenneth D. Merry 	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2775130f4520SKenneth D. Merry 		wakeup(lun);
2776130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2777130f4520SKenneth D. Merry 
2778130f4520SKenneth D. Merry }
2779130f4520SKenneth D. Merry 
2780130f4520SKenneth D. Merry static void
2781130f4520SKenneth D. Merry ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2782130f4520SKenneth D. Merry {
2783130f4520SKenneth D. Merry 	struct ctl_be_block_lun *lun;
2784130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
2785130f4520SKenneth D. Merry 
2786130f4520SKenneth D. Merry 	lun = (struct ctl_be_block_lun *)be_lun;
2787130f4520SKenneth D. Merry 	softc = lun->softc;
2788130f4520SKenneth D. Merry 
2789130f4520SKenneth D. Merry 	if (status == CTL_LUN_CONFIG_OK) {
2790130f4520SKenneth D. Merry 		mtx_lock(&softc->lock);
2791130f4520SKenneth D. Merry 		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2792130f4520SKenneth D. Merry 		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2793130f4520SKenneth D. Merry 			wakeup(lun);
2794130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2795130f4520SKenneth D. Merry 
2796130f4520SKenneth D. Merry 		/*
2797130f4520SKenneth D. Merry 		 * We successfully added the LUN, attempt to enable it.
2798130f4520SKenneth D. Merry 		 */
27990bcd4ab6SAlexander Motin 		if (ctl_enable_lun(&lun->cbe_lun) != 0) {
2800130f4520SKenneth D. Merry 			printf("%s: ctl_enable_lun() failed!\n", __func__);
28010bcd4ab6SAlexander Motin 			if (ctl_invalidate_lun(&lun->cbe_lun) != 0) {
2802130f4520SKenneth D. Merry 				printf("%s: ctl_invalidate_lun() failed!\n",
2803130f4520SKenneth D. Merry 				       __func__);
2804130f4520SKenneth D. Merry 			}
2805130f4520SKenneth D. Merry 		}
2806130f4520SKenneth D. Merry 
2807130f4520SKenneth D. Merry 		return;
2808130f4520SKenneth D. Merry 	}
2809130f4520SKenneth D. Merry 
2810130f4520SKenneth D. Merry 
2811130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2812130f4520SKenneth D. Merry 	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2813130f4520SKenneth D. Merry 	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2814130f4520SKenneth D. Merry 	wakeup(lun);
2815130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2816130f4520SKenneth D. Merry }
2817130f4520SKenneth D. Merry 
2818130f4520SKenneth D. Merry 
2819130f4520SKenneth D. Merry static int
2820130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2821130f4520SKenneth D. Merry {
2822130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
28230bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2824130f4520SKenneth D. Merry 	int retval;
2825130f4520SKenneth D. Merry 
2826130f4520SKenneth D. Merry 	retval = 0;
2827130f4520SKenneth D. Merry 
2828130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2829130f4520SKenneth D. Merry 
28300bcd4ab6SAlexander Motin 	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2831130f4520SKenneth D. Merry 		CTL_PRIV_BACKEND_LUN].ptr;
28320bcd4ab6SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2833130f4520SKenneth D. Merry 
2834130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2835130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2836130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2837ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2838ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2839ee7f31c0SAlexander Motin 	case UNMAP:
2840130f4520SKenneth D. Merry 		/*
2841130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2842130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2843130f4520SKenneth D. Merry 		 *
2844130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2845130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2846130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2847130f4520SKenneth D. Merry 		 */
284875c7a1d3SAlexander Motin 		mtx_lock(&be_lun->queue_lock);
2849130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2850130f4520SKenneth D. Merry 				   links);
285175c7a1d3SAlexander Motin 		mtx_unlock(&be_lun->queue_lock);
2852130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2853130f4520SKenneth D. Merry 		break;
2854130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2855130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2856130f4520SKenneth D. Merry 
2857130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2858130f4520SKenneth D. Merry 
2859130f4520SKenneth D. Merry 		if (cdb->how & SSS_START)
28600bcd4ab6SAlexander Motin 			retval = ctl_start_lun(cbe_lun);
2861130f4520SKenneth D. Merry 		else {
28620bcd4ab6SAlexander Motin 			retval = ctl_stop_lun(cbe_lun);
2863130f4520SKenneth D. Merry 			/*
2864130f4520SKenneth D. Merry 			 * XXX KDM Copan-specific offline behavior.
2865130f4520SKenneth D. Merry 			 * Figure out a reasonable way to port this?
2866130f4520SKenneth D. Merry 			 */
2867130f4520SKenneth D. Merry #ifdef NEEDTOPORT
2868130f4520SKenneth D. Merry 			if ((retval == 0)
2869130f4520SKenneth D. Merry 			 && (cdb->byte2 & SSS_ONOFFLINE))
28700bcd4ab6SAlexander Motin 				retval = ctl_lun_offline(cbe_lun);
2871130f4520SKenneth D. Merry #endif
2872130f4520SKenneth D. Merry 		}
2873130f4520SKenneth D. Merry 
2874130f4520SKenneth D. Merry 		/*
2875130f4520SKenneth D. Merry 		 * In general, the above routines should not fail.  They
2876130f4520SKenneth D. Merry 		 * just set state for the LUN.  So we've got something
2877130f4520SKenneth D. Merry 		 * pretty wrong here if we can't start or stop the LUN.
2878130f4520SKenneth D. Merry 		 */
2879130f4520SKenneth D. Merry 		if (retval != 0) {
2880130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
2881130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
2882130f4520SKenneth D. Merry 						 /*retry_count*/ 0xf051);
2883130f4520SKenneth D. Merry 			retval = CTL_RETVAL_COMPLETE;
2884130f4520SKenneth D. Merry 		} else {
2885130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
2886130f4520SKenneth D. Merry 		}
2887130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2888130f4520SKenneth D. Merry 		break;
2889130f4520SKenneth D. Merry 	}
2890130f4520SKenneth D. Merry 	default:
2891130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2892130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2893130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2894130f4520SKenneth D. Merry 		break;
2895130f4520SKenneth D. Merry 	}
2896130f4520SKenneth D. Merry 
2897130f4520SKenneth D. Merry 	return (retval);
2898130f4520SKenneth D. Merry }
2899130f4520SKenneth D. Merry 
2900130f4520SKenneth D. Merry static int
2901130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2902130f4520SKenneth D. Merry {
2903ef8daf3fSAlexander Motin 	struct ctl_be_block_lun *be_lun;
29040bcd4ab6SAlexander Motin 	struct ctl_be_lun *cbe_lun;
2905ef8daf3fSAlexander Motin 	int retval = 0;
2906ef8daf3fSAlexander Motin 
2907ef8daf3fSAlexander Motin 	DPRINTF("entered\n");
2908ef8daf3fSAlexander Motin 
29090bcd4ab6SAlexander Motin 	cbe_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2910ef8daf3fSAlexander Motin 		CTL_PRIV_BACKEND_LUN].ptr;
29110bcd4ab6SAlexander Motin 	be_lun = (struct ctl_be_block_lun *)cbe_lun->be_lun;
2912ef8daf3fSAlexander Motin 
2913ef8daf3fSAlexander Motin 	switch (io->scsiio.cdb[0]) {
2914ef8daf3fSAlexander Motin 	case SERVICE_ACTION_IN:
2915ef8daf3fSAlexander Motin 		if (io->scsiio.cdb[1] == SGLS_SERVICE_ACTION) {
2916ef8daf3fSAlexander Motin 			mtx_lock(&be_lun->queue_lock);
2917ef8daf3fSAlexander Motin 			STAILQ_INSERT_TAIL(&be_lun->config_read_queue,
2918ef8daf3fSAlexander Motin 			    &io->io_hdr, links);
2919ef8daf3fSAlexander Motin 			mtx_unlock(&be_lun->queue_lock);
2920ef8daf3fSAlexander Motin 			taskqueue_enqueue(be_lun->io_taskqueue,
2921ef8daf3fSAlexander Motin 			    &be_lun->io_task);
2922ef8daf3fSAlexander Motin 			retval = CTL_RETVAL_QUEUED;
2923ef8daf3fSAlexander Motin 			break;
2924ef8daf3fSAlexander Motin 		}
2925ef8daf3fSAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
2926ef8daf3fSAlexander Motin 				      /*sks_valid*/ 1,
2927ef8daf3fSAlexander Motin 				      /*command*/ 1,
2928ef8daf3fSAlexander Motin 				      /*field*/ 1,
2929ef8daf3fSAlexander Motin 				      /*bit_valid*/ 1,
2930ef8daf3fSAlexander Motin 				      /*bit*/ 4);
2931ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2932ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2933ef8daf3fSAlexander Motin 		break;
2934ef8daf3fSAlexander Motin 	default:
2935ef8daf3fSAlexander Motin 		ctl_set_invalid_opcode(&io->scsiio);
2936ef8daf3fSAlexander Motin 		ctl_config_read_done(io);
2937ef8daf3fSAlexander Motin 		retval = CTL_RETVAL_COMPLETE;
2938ef8daf3fSAlexander Motin 		break;
2939ef8daf3fSAlexander Motin 	}
2940ef8daf3fSAlexander Motin 
2941ef8daf3fSAlexander Motin 	return (retval);
2942130f4520SKenneth D. Merry }
2943130f4520SKenneth D. Merry 
2944130f4520SKenneth D. Merry static int
2945130f4520SKenneth D. Merry ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2946130f4520SKenneth D. Merry {
2947130f4520SKenneth D. Merry 	struct ctl_be_block_lun *lun;
2948130f4520SKenneth D. Merry 	int retval;
2949130f4520SKenneth D. Merry 
2950130f4520SKenneth D. Merry 	lun = (struct ctl_be_block_lun *)be_lun;
2951130f4520SKenneth D. Merry 	retval = 0;
2952130f4520SKenneth D. Merry 
29532cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "\t<num_threads>");
2954130f4520SKenneth D. Merry 
2955130f4520SKenneth D. Merry 	if (retval != 0)
2956130f4520SKenneth D. Merry 		goto bailout;
2957130f4520SKenneth D. Merry 
2958130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2959130f4520SKenneth D. Merry 
2960130f4520SKenneth D. Merry 	if (retval != 0)
2961130f4520SKenneth D. Merry 		goto bailout;
2962130f4520SKenneth D. Merry 
29632cfbcb9bSAlexander Motin 	retval = sbuf_printf(sb, "</num_threads>\n");
2964130f4520SKenneth D. Merry 
2965130f4520SKenneth D. Merry bailout:
2966130f4520SKenneth D. Merry 
2967130f4520SKenneth D. Merry 	return (retval);
2968130f4520SKenneth D. Merry }
2969130f4520SKenneth D. Merry 
2970c3e7ba3eSAlexander Motin static uint64_t
2971c3e7ba3eSAlexander Motin ctl_be_block_lun_attr(void *be_lun, const char *attrname)
2972c3e7ba3eSAlexander Motin {
2973c3e7ba3eSAlexander Motin 	struct ctl_be_block_lun *lun = (struct ctl_be_block_lun *)be_lun;
2974c3e7ba3eSAlexander Motin 
2975c3e7ba3eSAlexander Motin 	if (lun->getattr == NULL)
2976c3e7ba3eSAlexander Motin 		return (UINT64_MAX);
2977c3e7ba3eSAlexander Motin 	return (lun->getattr(lun, attrname));
2978c3e7ba3eSAlexander Motin }
2979c3e7ba3eSAlexander Motin 
2980130f4520SKenneth D. Merry int
2981130f4520SKenneth D. Merry ctl_be_block_init(void)
2982130f4520SKenneth D. Merry {
2983130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
2984130f4520SKenneth D. Merry 	int retval;
2985130f4520SKenneth D. Merry 
2986130f4520SKenneth D. Merry 	softc = &backend_block_softc;
2987130f4520SKenneth D. Merry 	retval = 0;
2988130f4520SKenneth D. Merry 
298975c7a1d3SAlexander Motin 	mtx_init(&softc->lock, "ctlblock", NULL, MTX_DEF);
2990a0e36aeeSEdward Tomasz Napierala 	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2991a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2992130f4520SKenneth D. Merry 	STAILQ_INIT(&softc->lun_list);
2993130f4520SKenneth D. Merry 
2994130f4520SKenneth D. Merry 	return (retval);
2995130f4520SKenneth D. Merry }
2996