xref: /freebsd/sys/cam/ctl/ctl_backend_block.c (revision ead2f1172473f33446318fe0a90e0551fa09e177)
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>
71130f4520SKenneth D. Merry #include <sys/proc.h>
72130f4520SKenneth D. Merry #include <sys/pcpu.h>
73130f4520SKenneth D. Merry #include <sys/module.h>
74130f4520SKenneth D. Merry #include <sys/sdt.h>
75130f4520SKenneth D. Merry #include <sys/devicestat.h>
76130f4520SKenneth D. Merry #include <sys/sysctl.h>
77130f4520SKenneth D. Merry 
78130f4520SKenneth D. Merry #include <geom/geom.h>
79130f4520SKenneth D. Merry 
80130f4520SKenneth D. Merry #include <cam/cam.h>
81130f4520SKenneth D. Merry #include <cam/scsi/scsi_all.h>
82130f4520SKenneth D. Merry #include <cam/scsi/scsi_da.h>
83130f4520SKenneth D. Merry #include <cam/ctl/ctl_io.h>
84130f4520SKenneth D. Merry #include <cam/ctl/ctl.h>
85130f4520SKenneth D. Merry #include <cam/ctl/ctl_backend.h>
86130f4520SKenneth D. Merry #include <cam/ctl/ctl_frontend_internal.h>
87130f4520SKenneth D. Merry #include <cam/ctl/ctl_ioctl.h>
88130f4520SKenneth D. Merry #include <cam/ctl/ctl_scsi_all.h>
89130f4520SKenneth D. Merry #include <cam/ctl/ctl_error.h>
90130f4520SKenneth D. Merry 
91130f4520SKenneth D. Merry /*
9208a7cce5SAlexander Motin  * The idea here is that we'll allocate enough S/G space to hold a 1MB
9308a7cce5SAlexander Motin  * I/O.  If we get an I/O larger than that, we'll split it.
94130f4520SKenneth D. Merry  */
9511b569f7SAlexander Motin #define	CTLBLK_HALF_IO_SIZE	(512 * 1024)
9611b569f7SAlexander Motin #define	CTLBLK_MAX_IO_SIZE	(CTLBLK_HALF_IO_SIZE * 2)
9708a7cce5SAlexander Motin #define	CTLBLK_MAX_SEG		MAXPHYS
9811b569f7SAlexander Motin #define	CTLBLK_HALF_SEGS	MAX(CTLBLK_HALF_IO_SIZE / CTLBLK_MAX_SEG, 1)
9911b569f7SAlexander Motin #define	CTLBLK_MAX_SEGS		(CTLBLK_HALF_SEGS * 2)
100130f4520SKenneth D. Merry 
101130f4520SKenneth D. Merry #ifdef CTLBLK_DEBUG
102130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) \
103130f4520SKenneth D. Merry     printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
104130f4520SKenneth D. Merry #else
105130f4520SKenneth D. Merry #define DPRINTF(fmt, args...) do {} while(0)
106130f4520SKenneth D. Merry #endif
107130f4520SKenneth D. Merry 
108e86a4142SAlexander Motin #define PRIV(io)	\
109e86a4142SAlexander Motin     ((struct ctl_ptr_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_BACKEND])
11011b569f7SAlexander Motin #define ARGS(io)	\
11111b569f7SAlexander Motin     ((struct ctl_lba_len_flags *)&(io)->io_hdr.ctl_private[CTL_PRIV_LBA_LEN])
112e86a4142SAlexander Motin 
113130f4520SKenneth D. Merry SDT_PROVIDER_DEFINE(cbb);
114130f4520SKenneth D. Merry 
115130f4520SKenneth D. Merry typedef enum {
116130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
117130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
118130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
119130f4520SKenneth D. Merry 	CTL_BE_BLOCK_LUN_MULTI_THREAD	= 0x08
120130f4520SKenneth D. Merry } ctl_be_block_lun_flags;
121130f4520SKenneth D. Merry 
122130f4520SKenneth D. Merry typedef enum {
123130f4520SKenneth D. Merry 	CTL_BE_BLOCK_NONE,
124130f4520SKenneth D. Merry 	CTL_BE_BLOCK_DEV,
125130f4520SKenneth D. Merry 	CTL_BE_BLOCK_FILE
126130f4520SKenneth D. Merry } ctl_be_block_type;
127130f4520SKenneth D. Merry 
128130f4520SKenneth D. Merry struct ctl_be_block_devdata {
129130f4520SKenneth D. Merry 	struct cdev *cdev;
130130f4520SKenneth D. Merry 	struct cdevsw *csw;
131130f4520SKenneth D. Merry 	int dev_ref;
132130f4520SKenneth D. Merry };
133130f4520SKenneth D. Merry 
134130f4520SKenneth D. Merry struct ctl_be_block_filedata {
135130f4520SKenneth D. Merry 	struct ucred *cred;
136130f4520SKenneth D. Merry };
137130f4520SKenneth D. Merry 
138130f4520SKenneth D. Merry union ctl_be_block_bedata {
139130f4520SKenneth D. Merry 	struct ctl_be_block_devdata dev;
140130f4520SKenneth D. Merry 	struct ctl_be_block_filedata file;
141130f4520SKenneth D. Merry };
142130f4520SKenneth D. Merry 
143130f4520SKenneth D. Merry struct ctl_be_block_io;
144130f4520SKenneth D. Merry struct ctl_be_block_lun;
145130f4520SKenneth D. Merry 
146130f4520SKenneth D. Merry typedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
147130f4520SKenneth D. Merry 			       struct ctl_be_block_io *beio);
148130f4520SKenneth D. Merry 
149130f4520SKenneth D. Merry /*
150130f4520SKenneth D. Merry  * Backend LUN structure.  There is a 1:1 mapping between a block device
151130f4520SKenneth D. Merry  * and a backend block LUN, and between a backend block LUN and a CTL LUN.
152130f4520SKenneth D. Merry  */
153130f4520SKenneth D. Merry struct ctl_be_block_lun {
154130f4520SKenneth D. Merry 	struct ctl_block_disk *disk;
155130f4520SKenneth D. Merry 	char lunname[32];
156130f4520SKenneth D. Merry 	char *dev_path;
157130f4520SKenneth D. Merry 	ctl_be_block_type dev_type;
158130f4520SKenneth D. Merry 	struct vnode *vn;
159130f4520SKenneth D. Merry 	union ctl_be_block_bedata backend;
160130f4520SKenneth D. Merry 	cbb_dispatch_t dispatch;
161130f4520SKenneth D. Merry 	cbb_dispatch_t lun_flush;
162ee7f31c0SAlexander Motin 	cbb_dispatch_t unmap;
163130f4520SKenneth D. Merry 	struct mtx lock;
164130f4520SKenneth D. Merry 	uma_zone_t lun_zone;
165130f4520SKenneth D. Merry 	uint64_t size_blocks;
166130f4520SKenneth D. Merry 	uint64_t size_bytes;
167130f4520SKenneth D. Merry 	uint32_t blocksize;
168130f4520SKenneth D. Merry 	int blocksize_shift;
169f6012722SAlexander Motin 	uint16_t pblockexp;
170f6012722SAlexander Motin 	uint16_t pblockoff;
171130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
172130f4520SKenneth D. Merry 	struct devstat *disk_stats;
173130f4520SKenneth D. Merry 	ctl_be_block_lun_flags flags;
174130f4520SKenneth D. Merry 	STAILQ_ENTRY(ctl_be_block_lun) links;
175130f4520SKenneth D. Merry 	struct ctl_be_lun ctl_be_lun;
176130f4520SKenneth D. Merry 	struct taskqueue *io_taskqueue;
177130f4520SKenneth D. Merry 	struct task io_task;
178130f4520SKenneth D. Merry 	int num_threads;
179130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) input_queue;
180130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
181130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
182130f4520SKenneth D. Merry };
183130f4520SKenneth D. Merry 
184130f4520SKenneth D. Merry /*
185130f4520SKenneth D. Merry  * Overall softc structure for the block backend module.
186130f4520SKenneth D. Merry  */
187130f4520SKenneth D. Merry struct ctl_be_block_softc {
188130f4520SKenneth D. Merry 	struct mtx			 lock;
189130f4520SKenneth D. Merry 	int				 num_disks;
190130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
191130f4520SKenneth D. Merry 	int				 num_luns;
192130f4520SKenneth D. Merry 	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
193130f4520SKenneth D. Merry };
194130f4520SKenneth D. Merry 
195130f4520SKenneth D. Merry static struct ctl_be_block_softc backend_block_softc;
196130f4520SKenneth D. Merry 
197130f4520SKenneth D. Merry /*
198130f4520SKenneth D. Merry  * Per-I/O information.
199130f4520SKenneth D. Merry  */
200130f4520SKenneth D. Merry struct ctl_be_block_io {
201130f4520SKenneth D. Merry 	union ctl_io			*io;
202130f4520SKenneth D. Merry 	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
203130f4520SKenneth D. Merry 	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
204130f4520SKenneth D. Merry 	int				bio_cmd;
205130f4520SKenneth D. Merry 	int				bio_flags;
206130f4520SKenneth D. Merry 	int				num_segs;
207130f4520SKenneth D. Merry 	int				num_bios_sent;
208130f4520SKenneth D. Merry 	int				num_bios_done;
209130f4520SKenneth D. Merry 	int				send_complete;
210130f4520SKenneth D. Merry 	int				num_errors;
211130f4520SKenneth D. Merry 	struct bintime			ds_t0;
212130f4520SKenneth D. Merry 	devstat_tag_type		ds_tag_type;
213130f4520SKenneth D. Merry 	devstat_trans_flags		ds_trans_type;
214130f4520SKenneth D. Merry 	uint64_t			io_len;
215130f4520SKenneth D. Merry 	uint64_t			io_offset;
216130f4520SKenneth D. Merry 	struct ctl_be_block_softc	*softc;
217130f4520SKenneth D. Merry 	struct ctl_be_block_lun		*lun;
218ee7f31c0SAlexander Motin 	void (*beio_cont)(struct ctl_be_block_io *beio); /* to continue processing */
219130f4520SKenneth D. Merry };
220130f4520SKenneth D. Merry 
221130f4520SKenneth D. Merry static int cbb_num_threads = 14;
222130f4520SKenneth D. Merry TUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads);
223130f4520SKenneth D. Merry SYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
224130f4520SKenneth D. Merry 	    "CAM Target Layer Block Backend");
225130f4520SKenneth D. Merry SYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW,
226130f4520SKenneth D. Merry            &cbb_num_threads, 0, "Number of threads per backing file");
227130f4520SKenneth D. Merry 
228130f4520SKenneth D. Merry static struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
229130f4520SKenneth D. Merry static void ctl_free_beio(struct ctl_be_block_io *beio);
230130f4520SKenneth D. Merry static void ctl_complete_beio(struct ctl_be_block_io *beio);
231130f4520SKenneth D. Merry static int ctl_be_block_move_done(union ctl_io *io);
232130f4520SKenneth D. Merry static void ctl_be_block_biodone(struct bio *bio);
233130f4520SKenneth D. Merry static void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
234130f4520SKenneth D. Merry 				    struct ctl_be_block_io *beio);
235130f4520SKenneth D. Merry static void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
236130f4520SKenneth D. Merry 				       struct ctl_be_block_io *beio);
237130f4520SKenneth D. Merry static void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
238130f4520SKenneth D. Merry 				   struct ctl_be_block_io *beio);
239ee7f31c0SAlexander Motin static void ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
240ee7f31c0SAlexander Motin 				   struct ctl_be_block_io *beio);
241130f4520SKenneth D. Merry static void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
242130f4520SKenneth D. Merry 				      struct ctl_be_block_io *beio);
243130f4520SKenneth D. Merry static void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
244130f4520SKenneth D. Merry 				    union ctl_io *io);
245130f4520SKenneth D. Merry static void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
246130f4520SKenneth D. Merry 				  union ctl_io *io);
247130f4520SKenneth D. Merry static void ctl_be_block_worker(void *context, int pending);
248130f4520SKenneth D. Merry static int ctl_be_block_submit(union ctl_io *io);
249130f4520SKenneth D. Merry static int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
250130f4520SKenneth D. Merry 				   int flag, struct thread *td);
251130f4520SKenneth D. Merry static int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
252130f4520SKenneth D. Merry 				  struct ctl_lun_req *req);
253130f4520SKenneth D. Merry static int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
254130f4520SKenneth D. Merry 				 struct ctl_lun_req *req);
255130f4520SKenneth D. Merry static int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
256130f4520SKenneth D. Merry static int ctl_be_block_open(struct ctl_be_block_softc *softc,
257130f4520SKenneth D. Merry 			     struct ctl_be_block_lun *be_lun,
258130f4520SKenneth D. Merry 			     struct ctl_lun_req *req);
259130f4520SKenneth D. Merry static int ctl_be_block_create(struct ctl_be_block_softc *softc,
260130f4520SKenneth D. Merry 			       struct ctl_lun_req *req);
261130f4520SKenneth D. Merry static int ctl_be_block_rm(struct ctl_be_block_softc *softc,
262130f4520SKenneth D. Merry 			   struct ctl_lun_req *req);
26381177295SEdward Tomasz Napierala static int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
26481177295SEdward Tomasz Napierala 				  struct ctl_lun_req *req);
26581177295SEdward Tomasz Napierala static int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
26681177295SEdward Tomasz Napierala 				 struct ctl_lun_req *req);
26781177295SEdward Tomasz Napierala static int ctl_be_block_modify(struct ctl_be_block_softc *softc,
26881177295SEdward Tomasz Napierala 			   struct ctl_lun_req *req);
269130f4520SKenneth D. Merry static void ctl_be_block_lun_shutdown(void *be_lun);
270130f4520SKenneth D. Merry static void ctl_be_block_lun_config_status(void *be_lun,
271130f4520SKenneth D. Merry 					   ctl_lun_config_status status);
272130f4520SKenneth D. Merry static int ctl_be_block_config_write(union ctl_io *io);
273130f4520SKenneth D. Merry static int ctl_be_block_config_read(union ctl_io *io);
274130f4520SKenneth D. Merry static int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
275130f4520SKenneth D. Merry int ctl_be_block_init(void);
276130f4520SKenneth D. Merry 
277130f4520SKenneth D. Merry static struct ctl_backend_driver ctl_be_block_driver =
278130f4520SKenneth D. Merry {
2792a2443d8SKenneth D. Merry 	.name = "block",
2802a2443d8SKenneth D. Merry 	.flags = CTL_BE_FLAG_HAS_CONFIG,
2812a2443d8SKenneth D. Merry 	.init = ctl_be_block_init,
2822a2443d8SKenneth D. Merry 	.data_submit = ctl_be_block_submit,
2832a2443d8SKenneth D. Merry 	.data_move_done = ctl_be_block_move_done,
2842a2443d8SKenneth D. Merry 	.config_read = ctl_be_block_config_read,
2852a2443d8SKenneth D. Merry 	.config_write = ctl_be_block_config_write,
2862a2443d8SKenneth D. Merry 	.ioctl = ctl_be_block_ioctl,
2872a2443d8SKenneth D. Merry 	.lun_info = ctl_be_block_lun_info
288130f4520SKenneth D. Merry };
289130f4520SKenneth D. Merry 
290130f4520SKenneth D. Merry MALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
291130f4520SKenneth D. Merry CTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
292130f4520SKenneth D. Merry 
293a0e36aeeSEdward Tomasz Napierala static uma_zone_t beio_zone;
294a0e36aeeSEdward Tomasz Napierala 
295130f4520SKenneth D. Merry static struct ctl_be_block_io *
296130f4520SKenneth D. Merry ctl_alloc_beio(struct ctl_be_block_softc *softc)
297130f4520SKenneth D. Merry {
298130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
299130f4520SKenneth D. Merry 
300a0e36aeeSEdward Tomasz Napierala 	beio = uma_zalloc(beio_zone, M_WAITOK | M_ZERO);
301130f4520SKenneth D. Merry 	beio->softc = softc;
302130f4520SKenneth D. Merry 	return (beio);
303130f4520SKenneth D. Merry }
304130f4520SKenneth D. Merry 
305130f4520SKenneth D. Merry static void
306130f4520SKenneth D. Merry ctl_free_beio(struct ctl_be_block_io *beio)
307130f4520SKenneth D. Merry {
308130f4520SKenneth D. Merry 	int duplicate_free;
309130f4520SKenneth D. Merry 	int i;
310130f4520SKenneth D. Merry 
311130f4520SKenneth D. Merry 	duplicate_free = 0;
312130f4520SKenneth D. Merry 
313130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
314130f4520SKenneth D. Merry 		if (beio->sg_segs[i].addr == NULL)
315130f4520SKenneth D. Merry 			duplicate_free++;
316130f4520SKenneth D. Merry 
317130f4520SKenneth D. Merry 		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
318130f4520SKenneth D. Merry 		beio->sg_segs[i].addr = NULL;
31911b569f7SAlexander Motin 
32011b569f7SAlexander Motin 		/* For compare we had two equal S/G lists. */
32111b569f7SAlexander Motin 		if (ARGS(beio->io)->flags & CTL_LLF_COMPARE) {
32211b569f7SAlexander Motin 			uma_zfree(beio->lun->lun_zone,
32311b569f7SAlexander Motin 			    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr);
32411b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr = NULL;
32511b569f7SAlexander Motin 		}
326130f4520SKenneth D. Merry 	}
327130f4520SKenneth D. Merry 
328130f4520SKenneth D. Merry 	if (duplicate_free > 0) {
329130f4520SKenneth D. Merry 		printf("%s: %d duplicate frees out of %d segments\n", __func__,
330130f4520SKenneth D. Merry 		       duplicate_free, beio->num_segs);
331130f4520SKenneth D. Merry 	}
332a0e36aeeSEdward Tomasz Napierala 
333a0e36aeeSEdward Tomasz Napierala 	uma_zfree(beio_zone, beio);
334130f4520SKenneth D. Merry }
335130f4520SKenneth D. Merry 
336130f4520SKenneth D. Merry static void
337130f4520SKenneth D. Merry ctl_complete_beio(struct ctl_be_block_io *beio)
338130f4520SKenneth D. Merry {
339130f4520SKenneth D. Merry 	union ctl_io *io;
340130f4520SKenneth D. Merry 	int io_len;
341130f4520SKenneth D. Merry 
342130f4520SKenneth D. Merry 	io = beio->io;
343130f4520SKenneth D. Merry 
344130f4520SKenneth D. Merry 	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
345130f4520SKenneth D. Merry 		io_len = beio->io_len;
346130f4520SKenneth D. Merry 	else
347130f4520SKenneth D. Merry 		io_len = 0;
348130f4520SKenneth D. Merry 
349130f4520SKenneth D. Merry 	devstat_end_transaction(beio->lun->disk_stats,
350130f4520SKenneth D. Merry 				/*bytes*/ io_len,
351130f4520SKenneth D. Merry 				beio->ds_tag_type,
352130f4520SKenneth D. Merry 				beio->ds_trans_type,
353130f4520SKenneth D. Merry 				/*now*/ NULL,
354130f4520SKenneth D. Merry 				/*then*/&beio->ds_t0);
355130f4520SKenneth D. Merry 
356ee7f31c0SAlexander Motin 	if (beio->beio_cont != NULL) {
357ee7f31c0SAlexander Motin 		beio->beio_cont(beio);
358ee7f31c0SAlexander Motin 	} else {
359130f4520SKenneth D. Merry 		ctl_free_beio(beio);
36011b569f7SAlexander Motin 		ctl_data_submit_done(io);
361130f4520SKenneth D. Merry 	}
362ee7f31c0SAlexander Motin }
363130f4520SKenneth D. Merry 
364130f4520SKenneth D. Merry static int
365130f4520SKenneth D. Merry ctl_be_block_move_done(union ctl_io *io)
366130f4520SKenneth D. Merry {
367130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
368130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
36911b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
370130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
371130f4520SKenneth D. Merry 	struct bintime cur_bt;
372130f4520SKenneth D. Merry #endif
37311b569f7SAlexander Motin 	int i;
374130f4520SKenneth D. Merry 
375e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
376130f4520SKenneth D. Merry 	be_lun = beio->lun;
377130f4520SKenneth D. Merry 
378130f4520SKenneth D. Merry 	DPRINTF("entered\n");
379130f4520SKenneth D. Merry 
380130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
381130f4520SKenneth D. Merry 	getbintime(&cur_bt);
382130f4520SKenneth D. Merry 	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
383130f4520SKenneth D. Merry 	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
384130f4520SKenneth D. Merry 	io->io_hdr.num_dmas++;
385130f4520SKenneth D. Merry #endif
38611b569f7SAlexander Motin 	io->scsiio.kern_rel_offset += io->scsiio.kern_data_len;
387130f4520SKenneth D. Merry 
388130f4520SKenneth D. Merry 	/*
389130f4520SKenneth D. Merry 	 * We set status at this point for read commands, and write
390130f4520SKenneth D. Merry 	 * commands with errors.
391130f4520SKenneth D. Merry 	 */
39211b569f7SAlexander Motin 	if ((io->io_hdr.port_status == 0) &&
39311b569f7SAlexander Motin 	    ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) &&
39411b569f7SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
39511b569f7SAlexander Motin 		lbalen = ARGS(beio->io);
39611b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_READ) {
397130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
39811b569f7SAlexander Motin 		} else if (lbalen->flags & CTL_LLF_COMPARE) {
39911b569f7SAlexander Motin 			/* We have two data blocks ready for comparison. */
40011b569f7SAlexander Motin 			for (i = 0; i < beio->num_segs; i++) {
40111b569f7SAlexander Motin 				if (memcmp(beio->sg_segs[i].addr,
40211b569f7SAlexander Motin 				    beio->sg_segs[i + CTLBLK_HALF_SEGS].addr,
40311b569f7SAlexander Motin 				    beio->sg_segs[i].len) != 0)
40411b569f7SAlexander Motin 					break;
40511b569f7SAlexander Motin 			}
40611b569f7SAlexander Motin 			if (i < beio->num_segs)
40711b569f7SAlexander Motin 				ctl_set_sense(&io->scsiio,
40811b569f7SAlexander Motin 				    /*current_error*/ 1,
40911b569f7SAlexander Motin 				    /*sense_key*/ SSD_KEY_MISCOMPARE,
41011b569f7SAlexander Motin 				    /*asc*/ 0x1D,
41111b569f7SAlexander Motin 				    /*ascq*/ 0x00,
41211b569f7SAlexander Motin 				    SSD_ELEM_NONE);
41311b569f7SAlexander Motin 			else
41411b569f7SAlexander Motin 				ctl_set_success(&io->scsiio);
41511b569f7SAlexander Motin 		}
41611b569f7SAlexander Motin 	}
417130f4520SKenneth D. Merry 	else if ((io->io_hdr.port_status != 0)
418130f4520SKenneth D. Merry 	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
419130f4520SKenneth D. Merry 	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
420130f4520SKenneth D. Merry 		/*
421130f4520SKenneth D. Merry 		 * For hardware error sense keys, the sense key
422130f4520SKenneth D. Merry 		 * specific value is defined to be a retry count,
423130f4520SKenneth D. Merry 		 * but we use it to pass back an internal FETD
424130f4520SKenneth D. Merry 		 * error code.  XXX KDM  Hopefully the FETD is only
425130f4520SKenneth D. Merry 		 * using 16 bits for an error code, since that's
426130f4520SKenneth D. Merry 		 * all the space we have in the sks field.
427130f4520SKenneth D. Merry 		 */
428130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
429130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
430130f4520SKenneth D. Merry 					 /*retry_count*/
431130f4520SKenneth D. Merry 					 io->io_hdr.port_status);
432130f4520SKenneth D. Merry 	}
433130f4520SKenneth D. Merry 
434130f4520SKenneth D. Merry 	/*
435130f4520SKenneth D. Merry 	 * If this is a read, or a write with errors, it is done.
436130f4520SKenneth D. Merry 	 */
437130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_READ)
438130f4520SKenneth D. Merry 	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
439130f4520SKenneth D. Merry 	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
440130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
441130f4520SKenneth D. Merry 		return (0);
442130f4520SKenneth D. Merry 	}
443130f4520SKenneth D. Merry 
444130f4520SKenneth D. Merry 	/*
445130f4520SKenneth D. Merry 	 * At this point, we have a write and the DMA completed
446130f4520SKenneth D. Merry 	 * successfully.  We now have to queue it to the task queue to
447130f4520SKenneth D. Merry 	 * execute the backend I/O.  That is because we do blocking
448130f4520SKenneth D. Merry 	 * memory allocations, and in the file backing case, blocking I/O.
449130f4520SKenneth D. Merry 	 * This move done routine is generally called in the SIM's
450130f4520SKenneth D. Merry 	 * interrupt context, and therefore we cannot block.
451130f4520SKenneth D. Merry 	 */
452130f4520SKenneth D. Merry 	mtx_lock(&be_lun->lock);
453130f4520SKenneth D. Merry 	/*
454130f4520SKenneth D. Merry 	 * XXX KDM make sure that links is okay to use at this point.
455130f4520SKenneth D. Merry 	 * Otherwise, we either need to add another field to ctl_io_hdr,
456130f4520SKenneth D. Merry 	 * or deal with resource allocation here.
457130f4520SKenneth D. Merry 	 */
458130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
459130f4520SKenneth D. Merry 	mtx_unlock(&be_lun->lock);
460130f4520SKenneth D. Merry 
461130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
462130f4520SKenneth D. Merry 
463130f4520SKenneth D. Merry 	return (0);
464130f4520SKenneth D. Merry }
465130f4520SKenneth D. Merry 
466130f4520SKenneth D. Merry static void
467130f4520SKenneth D. Merry ctl_be_block_biodone(struct bio *bio)
468130f4520SKenneth D. Merry {
469130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
470130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
471130f4520SKenneth D. Merry 	union ctl_io *io;
472e0c2f975SAlexander Motin 	int error;
473130f4520SKenneth D. Merry 
474130f4520SKenneth D. Merry 	beio = bio->bio_caller1;
475130f4520SKenneth D. Merry 	be_lun = beio->lun;
476130f4520SKenneth D. Merry 	io = beio->io;
477130f4520SKenneth D. Merry 
478130f4520SKenneth D. Merry 	DPRINTF("entered\n");
479130f4520SKenneth D. Merry 
480e0c2f975SAlexander Motin 	error = bio->bio_error;
481130f4520SKenneth D. Merry 	mtx_lock(&be_lun->lock);
482e0c2f975SAlexander Motin 	if (error != 0)
483130f4520SKenneth D. Merry 		beio->num_errors++;
484130f4520SKenneth D. Merry 
485130f4520SKenneth D. Merry 	beio->num_bios_done++;
486130f4520SKenneth D. Merry 
487130f4520SKenneth D. Merry 	/*
488130f4520SKenneth D. Merry 	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
489130f4520SKenneth D. Merry 	 * during the free might cause it to complain.
490130f4520SKenneth D. Merry 	 */
491130f4520SKenneth D. Merry 	g_destroy_bio(bio);
492130f4520SKenneth D. Merry 
493130f4520SKenneth D. Merry 	/*
494130f4520SKenneth D. Merry 	 * If the send complete bit isn't set, or we aren't the last I/O to
495130f4520SKenneth D. Merry 	 * complete, then we're done.
496130f4520SKenneth D. Merry 	 */
497130f4520SKenneth D. Merry 	if ((beio->send_complete == 0)
498130f4520SKenneth D. Merry 	 || (beio->num_bios_done < beio->num_bios_sent)) {
499130f4520SKenneth D. Merry 		mtx_unlock(&be_lun->lock);
500130f4520SKenneth D. Merry 		return;
501130f4520SKenneth D. Merry 	}
502130f4520SKenneth D. Merry 
503130f4520SKenneth D. Merry 	/*
504130f4520SKenneth D. Merry 	 * At this point, we've verified that we are the last I/O to
505130f4520SKenneth D. Merry 	 * complete, so it's safe to drop the lock.
506130f4520SKenneth D. Merry 	 */
507130f4520SKenneth D. Merry 	mtx_unlock(&be_lun->lock);
508130f4520SKenneth D. Merry 
509130f4520SKenneth D. Merry 	/*
510130f4520SKenneth D. Merry 	 * If there are any errors from the backing device, we fail the
511130f4520SKenneth D. Merry 	 * entire I/O with a medium error.
512130f4520SKenneth D. Merry 	 */
513130f4520SKenneth D. Merry 	if (beio->num_errors > 0) {
514e0c2f975SAlexander Motin 		if (error == EOPNOTSUPP) {
515e0c2f975SAlexander Motin 			ctl_set_invalid_opcode(&io->scsiio);
516e0c2f975SAlexander Motin 		} else if (beio->bio_cmd == BIO_FLUSH) {
517130f4520SKenneth D. Merry 			/* XXX KDM is there is a better error here? */
518130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
519130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
520130f4520SKenneth D. Merry 						 /*retry_count*/ 0xbad2);
521130f4520SKenneth D. Merry 		} else
522130f4520SKenneth D. Merry 			ctl_set_medium_error(&io->scsiio);
523130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
524130f4520SKenneth D. Merry 		return;
525130f4520SKenneth D. Merry 	}
526130f4520SKenneth D. Merry 
527130f4520SKenneth D. Merry 	/*
52811b569f7SAlexander Motin 	 * If this is a write, a flush, a delete or verify, we're all done.
529130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
530130f4520SKenneth D. Merry 	 */
531130f4520SKenneth D. Merry 	if ((beio->bio_cmd == BIO_WRITE)
532ee7f31c0SAlexander Motin 	 || (beio->bio_cmd == BIO_FLUSH)
53311b569f7SAlexander Motin 	 || (beio->bio_cmd == BIO_DELETE)
53411b569f7SAlexander Motin 	 || (ARGS(io)->flags & CTL_LLF_VERIFY)) {
535130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
536130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
537130f4520SKenneth D. Merry 	} else {
538130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
539130f4520SKenneth D. Merry         	getbintime(&io->io_hdr.dma_start_bt);
540130f4520SKenneth D. Merry #endif
541130f4520SKenneth D. Merry 		ctl_datamove(io);
542130f4520SKenneth D. Merry 	}
543130f4520SKenneth D. Merry }
544130f4520SKenneth D. Merry 
545130f4520SKenneth D. Merry static void
546130f4520SKenneth D. Merry ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
547130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio)
548130f4520SKenneth D. Merry {
549130f4520SKenneth D. Merry 	union ctl_io *io;
550130f4520SKenneth D. Merry 	struct mount *mountpoint;
5515050aa86SKonstantin Belousov 	int error, lock_flags;
552130f4520SKenneth D. Merry 
553130f4520SKenneth D. Merry 	DPRINTF("entered\n");
554130f4520SKenneth D. Merry 
555130f4520SKenneth D. Merry 	io = beio->io;
556130f4520SKenneth D. Merry 
557130f4520SKenneth D. Merry        	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
558130f4520SKenneth D. Merry 
559130f4520SKenneth D. Merry 	if (MNT_SHARED_WRITES(mountpoint)
560130f4520SKenneth D. Merry 	 || ((mountpoint == NULL)
561130f4520SKenneth D. Merry 	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
562130f4520SKenneth D. Merry 		lock_flags = LK_SHARED;
563130f4520SKenneth D. Merry 	else
564130f4520SKenneth D. Merry 		lock_flags = LK_EXCLUSIVE;
565130f4520SKenneth D. Merry 
566130f4520SKenneth D. Merry 	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
567130f4520SKenneth D. Merry 
568130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
569130f4520SKenneth D. Merry 	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
570130f4520SKenneth D. Merry 
571130f4520SKenneth D. Merry 	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
572130f4520SKenneth D. Merry 	VOP_UNLOCK(be_lun->vn, 0);
573130f4520SKenneth D. Merry 
574130f4520SKenneth D. Merry 	vn_finished_write(mountpoint);
575130f4520SKenneth D. Merry 
576130f4520SKenneth D. Merry 	if (error == 0)
577130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
578130f4520SKenneth D. Merry 	else {
579130f4520SKenneth D. Merry 		/* XXX KDM is there is a better error here? */
580130f4520SKenneth D. Merry 		ctl_set_internal_failure(&io->scsiio,
581130f4520SKenneth D. Merry 					 /*sks_valid*/ 1,
582130f4520SKenneth D. Merry 					 /*retry_count*/ 0xbad1);
583130f4520SKenneth D. Merry 	}
584130f4520SKenneth D. Merry 
585130f4520SKenneth D. Merry 	ctl_complete_beio(beio);
586130f4520SKenneth D. Merry }
587130f4520SKenneth D. Merry 
588d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
589d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
590d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
591d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
592130f4520SKenneth D. Merry 
593130f4520SKenneth D. Merry static void
594130f4520SKenneth D. Merry ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
595130f4520SKenneth D. Merry 			   struct ctl_be_block_io *beio)
596130f4520SKenneth D. Merry {
597130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
598130f4520SKenneth D. Merry 	union ctl_io *io;
599130f4520SKenneth D. Merry 	struct uio xuio;
600130f4520SKenneth D. Merry 	struct iovec *xiovec;
6015050aa86SKonstantin Belousov 	int flags;
602130f4520SKenneth D. Merry 	int error, i;
603130f4520SKenneth D. Merry 
604130f4520SKenneth D. Merry 	DPRINTF("entered\n");
605130f4520SKenneth D. Merry 
606130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
607130f4520SKenneth D. Merry 	io = beio->io;
608130f4520SKenneth D. Merry 	flags = beio->bio_flags;
609130f4520SKenneth D. Merry 
61011b569f7SAlexander Motin 	bzero(&xuio, sizeof(xuio));
611130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
612130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
61311b569f7SAlexander Motin 		xuio.uio_rw = UIO_READ;
614130f4520SKenneth D. Merry 	} else {
615130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
616130f4520SKenneth D. Merry 		xuio.uio_rw = UIO_WRITE;
61711b569f7SAlexander Motin 	}
618130f4520SKenneth D. Merry 	xuio.uio_offset = beio->io_offset;
619130f4520SKenneth D. Merry 	xuio.uio_resid = beio->io_len;
620130f4520SKenneth D. Merry 	xuio.uio_segflg = UIO_SYSSPACE;
621130f4520SKenneth D. Merry 	xuio.uio_iov = beio->xiovecs;
622130f4520SKenneth D. Merry 	xuio.uio_iovcnt = beio->num_segs;
623130f4520SKenneth D. Merry 	xuio.uio_td = curthread;
624130f4520SKenneth D. Merry 
625130f4520SKenneth D. Merry 	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
626130f4520SKenneth D. Merry 		xiovec->iov_base = beio->sg_segs[i].addr;
627130f4520SKenneth D. Merry 		xiovec->iov_len = beio->sg_segs[i].len;
628130f4520SKenneth D. Merry 	}
629130f4520SKenneth D. Merry 
630130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
631130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
632130f4520SKenneth D. Merry 
633130f4520SKenneth D. Merry 		binuptime(&beio->ds_t0);
634130f4520SKenneth D. Merry 		devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
635130f4520SKenneth D. Merry 
636130f4520SKenneth D. Merry 		/*
637130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for reads.  If the
638130f4520SKenneth D. Merry 		 * DIRECTIO option is configured into the kernel, it calls
639130f4520SKenneth D. Merry 		 * ffs_rawread().  But that only works for single-segment
640130f4520SKenneth D. Merry 		 * uios with user space addresses.  In our case, with a
641130f4520SKenneth D. Merry 		 * kernel uio, it still reads into the buffer cache, but it
642130f4520SKenneth D. Merry 		 * will just try to release the buffer from the cache later
643130f4520SKenneth D. Merry 		 * on in ffs_read().
644130f4520SKenneth D. Merry 		 *
645130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for reads.
646130f4520SKenneth D. Merry 		 *
647130f4520SKenneth D. Merry 		 * UFS does not pay attention to IO_SYNC for reads.
648130f4520SKenneth D. Merry 		 *
649130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (which translates into the
650130f4520SKenneth D. Merry 		 * Solaris define FRSYNC for zfs_read()) for reads.  It
651130f4520SKenneth D. Merry 		 * attempts to sync the file before reading.
652130f4520SKenneth D. Merry 		 *
653130f4520SKenneth D. Merry 		 * So, to attempt to provide some barrier semantics in the
654130f4520SKenneth D. Merry 		 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
655130f4520SKenneth D. Merry 		 */
656130f4520SKenneth D. Merry 		error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
657130f4520SKenneth D. Merry 				 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
658130f4520SKenneth D. Merry 
659130f4520SKenneth D. Merry 		VOP_UNLOCK(be_lun->vn, 0);
66011b569f7SAlexander Motin 		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
661130f4520SKenneth D. Merry 	} else {
662130f4520SKenneth D. Merry 		struct mount *mountpoint;
663130f4520SKenneth D. Merry 		int lock_flags;
664130f4520SKenneth D. Merry 
665130f4520SKenneth D. Merry 		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
666130f4520SKenneth D. Merry 
667130f4520SKenneth D. Merry 		if (MNT_SHARED_WRITES(mountpoint)
668130f4520SKenneth D. Merry 		 || ((mountpoint == NULL)
669130f4520SKenneth D. Merry 		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
670130f4520SKenneth D. Merry 			lock_flags = LK_SHARED;
671130f4520SKenneth D. Merry 		else
672130f4520SKenneth D. Merry 			lock_flags = LK_EXCLUSIVE;
673130f4520SKenneth D. Merry 
674130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
675130f4520SKenneth D. Merry 
676130f4520SKenneth D. Merry 		binuptime(&beio->ds_t0);
677130f4520SKenneth D. Merry 		devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
678130f4520SKenneth D. Merry 
679130f4520SKenneth D. Merry 		/*
680130f4520SKenneth D. Merry 		 * UFS pays attention to IO_DIRECT for writes.  The write
681130f4520SKenneth D. Merry 		 * is done asynchronously.  (Normally the write would just
682130f4520SKenneth D. Merry 		 * get put into cache.
683130f4520SKenneth D. Merry 		 *
684130f4520SKenneth D. Merry 		 * UFS pays attention to IO_SYNC for writes.  It will
685130f4520SKenneth D. Merry 		 * attempt to write the buffer out synchronously if that
686130f4520SKenneth D. Merry 		 * flag is set.
687130f4520SKenneth D. Merry 		 *
688130f4520SKenneth D. Merry 		 * ZFS does not pay attention to IO_DIRECT for writes.
689130f4520SKenneth D. Merry 		 *
690130f4520SKenneth D. Merry 		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
691130f4520SKenneth D. Merry 		 * for writes.  It will flush the transaction from the
692130f4520SKenneth D. Merry 		 * cache before returning.
693130f4520SKenneth D. Merry 		 *
694130f4520SKenneth D. Merry 		 * So if we've got the BIO_ORDERED flag set, we want
695130f4520SKenneth D. Merry 		 * IO_SYNC in either the UFS or ZFS case.
696130f4520SKenneth D. Merry 		 */
697130f4520SKenneth D. Merry 		error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
698130f4520SKenneth D. Merry 				  IO_SYNC : 0, file_data->cred);
699130f4520SKenneth D. Merry 		VOP_UNLOCK(be_lun->vn, 0);
700130f4520SKenneth D. Merry 
701130f4520SKenneth D. Merry 		vn_finished_write(mountpoint);
70211b569f7SAlexander Motin 		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
703130f4520SKenneth D. Merry         }
704130f4520SKenneth D. Merry 
705130f4520SKenneth D. Merry 	/*
706130f4520SKenneth D. Merry 	 * If we got an error, set the sense data to "MEDIUM ERROR" and
707130f4520SKenneth D. Merry 	 * return the I/O to the user.
708130f4520SKenneth D. Merry 	 */
709130f4520SKenneth D. Merry 	if (error != 0) {
710130f4520SKenneth D. Merry 		char path_str[32];
711130f4520SKenneth D. Merry 
712130f4520SKenneth D. Merry 		ctl_scsi_path_string(io, path_str, sizeof(path_str));
713130f4520SKenneth D. Merry 		/*
714130f4520SKenneth D. Merry 		 * XXX KDM ZFS returns ENOSPC when the underlying
715130f4520SKenneth D. Merry 		 * filesystem fills up.  What kind of SCSI error should we
716130f4520SKenneth D. Merry 		 * return for that?
717130f4520SKenneth D. Merry 		 */
718130f4520SKenneth D. Merry 		printf("%s%s command returned errno %d\n", path_str,
719130f4520SKenneth D. Merry 		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
720130f4520SKenneth D. Merry 		ctl_set_medium_error(&io->scsiio);
721130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
722130f4520SKenneth D. Merry 		return;
723130f4520SKenneth D. Merry 	}
724130f4520SKenneth D. Merry 
725130f4520SKenneth D. Merry 	/*
726130f4520SKenneth D. Merry 	 * If this is a write, we're all done.
727130f4520SKenneth D. Merry 	 * If this is a read, we can now send the data to the user.
728130f4520SKenneth D. Merry 	 */
72911b569f7SAlexander Motin 	if (ARGS(io)->flags & (CTL_LLF_WRITE | CTL_LLF_VERIFY)) {
730130f4520SKenneth D. Merry 		ctl_set_success(&io->scsiio);
731130f4520SKenneth D. Merry 		ctl_complete_beio(beio);
732130f4520SKenneth D. Merry 	} else {
733130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
734130f4520SKenneth D. Merry         	getbintime(&io->io_hdr.dma_start_bt);
735130f4520SKenneth D. Merry #endif
736130f4520SKenneth D. Merry 		ctl_datamove(io);
737130f4520SKenneth D. Merry 	}
738130f4520SKenneth D. Merry }
739130f4520SKenneth D. Merry 
740130f4520SKenneth D. Merry static void
741130f4520SKenneth D. Merry ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
742130f4520SKenneth D. Merry 		       struct ctl_be_block_io *beio)
743130f4520SKenneth D. Merry {
744130f4520SKenneth D. Merry 	struct bio *bio;
745130f4520SKenneth D. Merry 	union ctl_io *io;
746130f4520SKenneth D. Merry 	struct ctl_be_block_devdata *dev_data;
747130f4520SKenneth D. Merry 
748130f4520SKenneth D. Merry 	dev_data = &be_lun->backend.dev;
749130f4520SKenneth D. Merry 	io = beio->io;
750130f4520SKenneth D. Merry 
751130f4520SKenneth D. Merry 	DPRINTF("entered\n");
752130f4520SKenneth D. Merry 
753130f4520SKenneth D. Merry 	/* This can't fail, it's a blocking allocation. */
754130f4520SKenneth D. Merry 	bio = g_alloc_bio();
755130f4520SKenneth D. Merry 
756130f4520SKenneth D. Merry 	bio->bio_cmd	    = BIO_FLUSH;
757130f4520SKenneth D. Merry 	bio->bio_flags	   |= BIO_ORDERED;
758130f4520SKenneth D. Merry 	bio->bio_dev	    = dev_data->cdev;
759130f4520SKenneth D. Merry 	bio->bio_offset	    = 0;
760130f4520SKenneth D. Merry 	bio->bio_data	    = 0;
761130f4520SKenneth D. Merry 	bio->bio_done	    = ctl_be_block_biodone;
762130f4520SKenneth D. Merry 	bio->bio_caller1    = beio;
763130f4520SKenneth D. Merry 	bio->bio_pblkno	    = 0;
764130f4520SKenneth D. Merry 
765130f4520SKenneth D. Merry 	/*
766130f4520SKenneth D. Merry 	 * We don't need to acquire the LUN lock here, because we are only
767130f4520SKenneth D. Merry 	 * sending one bio, and so there is no other context to synchronize
768130f4520SKenneth D. Merry 	 * with.
769130f4520SKenneth D. Merry 	 */
770130f4520SKenneth D. Merry 	beio->num_bios_sent = 1;
771130f4520SKenneth D. Merry 	beio->send_complete = 1;
772130f4520SKenneth D. Merry 
773130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
774130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
775130f4520SKenneth D. Merry 
776130f4520SKenneth D. Merry 	(*dev_data->csw->d_strategy)(bio);
777130f4520SKenneth D. Merry }
778130f4520SKenneth D. Merry 
779130f4520SKenneth D. Merry static void
780ee7f31c0SAlexander Motin ctl_be_block_unmap_dev_range(struct ctl_be_block_lun *be_lun,
781ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio,
782ee7f31c0SAlexander Motin 		       uint64_t off, uint64_t len, int last)
783ee7f31c0SAlexander Motin {
784ee7f31c0SAlexander Motin 	struct bio *bio;
785ee7f31c0SAlexander Motin 	struct ctl_be_block_devdata *dev_data;
7868f5a226aSAlexander Motin 	uint64_t maxlen;
787ee7f31c0SAlexander Motin 
788ee7f31c0SAlexander Motin 	dev_data = &be_lun->backend.dev;
7898f5a226aSAlexander Motin 	maxlen = LONG_MAX - (LONG_MAX % be_lun->blocksize);
790ee7f31c0SAlexander Motin 	while (len > 0) {
791ee7f31c0SAlexander Motin 		bio = g_alloc_bio();
792ee7f31c0SAlexander Motin 		bio->bio_cmd	    = BIO_DELETE;
793ee7f31c0SAlexander Motin 		bio->bio_flags	   |= beio->bio_flags;
794ee7f31c0SAlexander Motin 		bio->bio_dev	    = dev_data->cdev;
795ee7f31c0SAlexander Motin 		bio->bio_offset	    = off;
7968f5a226aSAlexander Motin 		bio->bio_length	    = MIN(len, maxlen);
797ee7f31c0SAlexander Motin 		bio->bio_data	    = 0;
798ee7f31c0SAlexander Motin 		bio->bio_done	    = ctl_be_block_biodone;
799ee7f31c0SAlexander Motin 		bio->bio_caller1    = beio;
8008f5a226aSAlexander Motin 		bio->bio_pblkno     = off / be_lun->blocksize;
801ee7f31c0SAlexander Motin 
802ee7f31c0SAlexander Motin 		off += bio->bio_length;
803ee7f31c0SAlexander Motin 		len -= bio->bio_length;
804ee7f31c0SAlexander Motin 
805ee7f31c0SAlexander Motin 		mtx_lock(&be_lun->lock);
806ee7f31c0SAlexander Motin 		beio->num_bios_sent++;
807ee7f31c0SAlexander Motin 		if (last && len == 0)
808ee7f31c0SAlexander Motin 			beio->send_complete = 1;
809ee7f31c0SAlexander Motin 		mtx_unlock(&be_lun->lock);
810ee7f31c0SAlexander Motin 
811ee7f31c0SAlexander Motin 		(*dev_data->csw->d_strategy)(bio);
812ee7f31c0SAlexander Motin 	}
813ee7f31c0SAlexander Motin }
814ee7f31c0SAlexander Motin 
815ee7f31c0SAlexander Motin static void
816ee7f31c0SAlexander Motin ctl_be_block_unmap_dev(struct ctl_be_block_lun *be_lun,
817ee7f31c0SAlexander Motin 		       struct ctl_be_block_io *beio)
818ee7f31c0SAlexander Motin {
819ee7f31c0SAlexander Motin 	union ctl_io *io;
820ee7f31c0SAlexander Motin 	struct ctl_be_block_devdata *dev_data;
82166df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
822ee7f31c0SAlexander Motin 	struct scsi_unmap_desc *buf, *end;
823ee7f31c0SAlexander Motin 	uint64_t len;
824ee7f31c0SAlexander Motin 
825ee7f31c0SAlexander Motin 	dev_data = &be_lun->backend.dev;
826ee7f31c0SAlexander Motin 	io = beio->io;
827ee7f31c0SAlexander Motin 
828ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
829ee7f31c0SAlexander Motin 
830ee7f31c0SAlexander Motin 	binuptime(&beio->ds_t0);
831ee7f31c0SAlexander Motin 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
832ee7f31c0SAlexander Motin 
833ee7f31c0SAlexander Motin 	if (beio->io_offset == -1) {
834ee7f31c0SAlexander Motin 		beio->io_len = 0;
83566df9136SAlexander Motin 		ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
83666df9136SAlexander Motin 		buf = (struct scsi_unmap_desc *)ptrlen->ptr;
83766df9136SAlexander Motin 		end = buf + ptrlen->len / sizeof(*buf);
838ee7f31c0SAlexander Motin 		for (; buf < end; buf++) {
839ee7f31c0SAlexander Motin 			len = (uint64_t)scsi_4btoul(buf->length) *
840ee7f31c0SAlexander Motin 			    be_lun->blocksize;
841ee7f31c0SAlexander Motin 			beio->io_len += len;
842ee7f31c0SAlexander Motin 			ctl_be_block_unmap_dev_range(be_lun, beio,
843ee7f31c0SAlexander Motin 			    scsi_8btou64(buf->lba) * be_lun->blocksize, len,
8447e0be022SAlexander Motin 			    (end - buf < 2) ? TRUE : FALSE);
845ee7f31c0SAlexander Motin 		}
846ee7f31c0SAlexander Motin 	} else
847ee7f31c0SAlexander Motin 		ctl_be_block_unmap_dev_range(be_lun, beio,
848ee7f31c0SAlexander Motin 		    beio->io_offset, beio->io_len, TRUE);
849ee7f31c0SAlexander Motin }
850ee7f31c0SAlexander Motin 
851ee7f31c0SAlexander Motin static void
852130f4520SKenneth D. Merry ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
853130f4520SKenneth D. Merry 			  struct ctl_be_block_io *beio)
854130f4520SKenneth D. Merry {
855130f4520SKenneth D. Merry 	int i;
856130f4520SKenneth D. Merry 	struct bio *bio;
857130f4520SKenneth D. Merry 	struct ctl_be_block_devdata *dev_data;
858130f4520SKenneth D. Merry 	off_t cur_offset;
859130f4520SKenneth D. Merry 	int max_iosize;
860130f4520SKenneth D. Merry 
861130f4520SKenneth D. Merry 	DPRINTF("entered\n");
862130f4520SKenneth D. Merry 
863130f4520SKenneth D. Merry 	dev_data = &be_lun->backend.dev;
864130f4520SKenneth D. Merry 
865130f4520SKenneth D. Merry 	/*
866130f4520SKenneth D. Merry 	 * We have to limit our I/O size to the maximum supported by the
867130f4520SKenneth D. Merry 	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
868130f4520SKenneth D. Merry 	 * set it properly, use DFLTPHYS.
869130f4520SKenneth D. Merry 	 */
870130f4520SKenneth D. Merry 	max_iosize = dev_data->cdev->si_iosize_max;
871130f4520SKenneth D. Merry 	if (max_iosize < PAGE_SIZE)
872130f4520SKenneth D. Merry 		max_iosize = DFLTPHYS;
873130f4520SKenneth D. Merry 
874130f4520SKenneth D. Merry 	cur_offset = beio->io_offset;
875130f4520SKenneth D. Merry 
876130f4520SKenneth D. Merry 	/*
877130f4520SKenneth D. Merry 	 * XXX KDM need to accurately reflect the number of I/Os outstanding
878130f4520SKenneth D. Merry 	 * to a device.
879130f4520SKenneth D. Merry 	 */
880130f4520SKenneth D. Merry 	binuptime(&beio->ds_t0);
881130f4520SKenneth D. Merry 	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
882130f4520SKenneth D. Merry 
883130f4520SKenneth D. Merry 	for (i = 0; i < beio->num_segs; i++) {
884130f4520SKenneth D. Merry 		size_t cur_size;
885130f4520SKenneth D. Merry 		uint8_t *cur_ptr;
886130f4520SKenneth D. Merry 
887130f4520SKenneth D. Merry 		cur_size = beio->sg_segs[i].len;
888130f4520SKenneth D. Merry 		cur_ptr = beio->sg_segs[i].addr;
889130f4520SKenneth D. Merry 
890130f4520SKenneth D. Merry 		while (cur_size > 0) {
891130f4520SKenneth D. Merry 			/* This can't fail, it's a blocking allocation. */
892130f4520SKenneth D. Merry 			bio = g_alloc_bio();
893130f4520SKenneth D. Merry 
894130f4520SKenneth D. Merry 			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
895130f4520SKenneth D. Merry 
896130f4520SKenneth D. Merry 			bio->bio_cmd = beio->bio_cmd;
897130f4520SKenneth D. Merry 			bio->bio_flags |= beio->bio_flags;
898130f4520SKenneth D. Merry 			bio->bio_dev = dev_data->cdev;
899130f4520SKenneth D. Merry 			bio->bio_caller1 = beio;
900130f4520SKenneth D. Merry 			bio->bio_length = min(cur_size, max_iosize);
901130f4520SKenneth D. Merry 			bio->bio_offset = cur_offset;
902130f4520SKenneth D. Merry 			bio->bio_data = cur_ptr;
903130f4520SKenneth D. Merry 			bio->bio_done = ctl_be_block_biodone;
904130f4520SKenneth D. Merry 			bio->bio_pblkno = cur_offset / be_lun->blocksize;
905130f4520SKenneth D. Merry 
906130f4520SKenneth D. Merry 			cur_offset += bio->bio_length;
907130f4520SKenneth D. Merry 			cur_ptr += bio->bio_length;
908130f4520SKenneth D. Merry 			cur_size -= bio->bio_length;
909130f4520SKenneth D. Merry 
910130f4520SKenneth D. Merry 			/*
911130f4520SKenneth D. Merry 			 * Make sure we set the complete bit just before we
912130f4520SKenneth D. Merry 			 * issue the last bio so we don't wind up with a
913130f4520SKenneth D. Merry 			 * race.
914130f4520SKenneth D. Merry 			 *
915130f4520SKenneth D. Merry 			 * Use the LUN mutex here instead of a combination
916130f4520SKenneth D. Merry 			 * of atomic variables for simplicity.
917130f4520SKenneth D. Merry 			 *
918130f4520SKenneth D. Merry 			 * XXX KDM we could have a per-IO lock, but that
919130f4520SKenneth D. Merry 			 * would cause additional per-IO setup and teardown
920130f4520SKenneth D. Merry 			 * overhead.  Hopefully there won't be too much
921130f4520SKenneth D. Merry 			 * contention on the LUN lock.
922130f4520SKenneth D. Merry 			 */
923130f4520SKenneth D. Merry 			mtx_lock(&be_lun->lock);
924130f4520SKenneth D. Merry 
925130f4520SKenneth D. Merry 			beio->num_bios_sent++;
926130f4520SKenneth D. Merry 
927130f4520SKenneth D. Merry 			if ((i == beio->num_segs - 1)
928130f4520SKenneth D. Merry 			 && (cur_size == 0))
929130f4520SKenneth D. Merry 				beio->send_complete = 1;
930130f4520SKenneth D. Merry 
931130f4520SKenneth D. Merry 			mtx_unlock(&be_lun->lock);
932130f4520SKenneth D. Merry 
933130f4520SKenneth D. Merry 			(*dev_data->csw->d_strategy)(bio);
934130f4520SKenneth D. Merry 		}
935130f4520SKenneth D. Merry 	}
936130f4520SKenneth D. Merry }
937130f4520SKenneth D. Merry 
938130f4520SKenneth D. Merry static void
939ee7f31c0SAlexander Motin ctl_be_block_cw_done_ws(struct ctl_be_block_io *beio)
940ee7f31c0SAlexander Motin {
941ee7f31c0SAlexander Motin 	union ctl_io *io;
942ee7f31c0SAlexander Motin 
943ee7f31c0SAlexander Motin 	io = beio->io;
944ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
945*ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
946*ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
947*ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
948ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
949ee7f31c0SAlexander Motin 		return;
950ee7f31c0SAlexander Motin 	}
951ee7f31c0SAlexander Motin 
952ee7f31c0SAlexander Motin 	ctl_be_block_config_write(io);
953ee7f31c0SAlexander Motin }
954ee7f31c0SAlexander Motin 
955ee7f31c0SAlexander Motin static void
956ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_ws(struct ctl_be_block_lun *be_lun,
957ee7f31c0SAlexander Motin 			    union ctl_io *io)
958ee7f31c0SAlexander Motin {
959ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
960ee7f31c0SAlexander Motin 	struct ctl_be_block_softc *softc;
96166df9136SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
962ee7f31c0SAlexander Motin 	uint64_t len_left, lba;
963ee7f31c0SAlexander Motin 	int i, seglen;
964ee7f31c0SAlexander Motin 	uint8_t *buf, *end;
965ee7f31c0SAlexander Motin 
966ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
967ee7f31c0SAlexander Motin 
968e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
969ee7f31c0SAlexander Motin 	softc = be_lun->softc;
97011b569f7SAlexander Motin 	lbalen = ARGS(beio->io);
971ee7f31c0SAlexander Motin 
97266df9136SAlexander Motin 	if (lbalen->flags & ~(SWS_LBDATA | SWS_UNMAP) ||
97366df9136SAlexander Motin 	    (lbalen->flags & SWS_UNMAP && be_lun->unmap == NULL)) {
974ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
975ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
976ee7f31c0SAlexander Motin 				      /*sks_valid*/ 1,
977ee7f31c0SAlexander Motin 				      /*command*/ 1,
978ee7f31c0SAlexander Motin 				      /*field*/ 1,
979ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
980ee7f31c0SAlexander Motin 				      /*bit*/ 0);
981ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
982ee7f31c0SAlexander Motin 		return;
983ee7f31c0SAlexander Motin 	}
984ee7f31c0SAlexander Motin 
985ee7f31c0SAlexander Motin 	/*
986ee7f31c0SAlexander Motin 	 * If the I/O came down with an ordered or head of queue tag, set
987ee7f31c0SAlexander Motin 	 * the BIO_ORDERED attribute.  For head of queue tags, that's
988ee7f31c0SAlexander Motin 	 * pretty much the best we can do.
989ee7f31c0SAlexander Motin 	 */
990ee7f31c0SAlexander Motin 	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
991ee7f31c0SAlexander Motin 	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
992ee7f31c0SAlexander Motin 		beio->bio_flags = BIO_ORDERED;
993ee7f31c0SAlexander Motin 
994ee7f31c0SAlexander Motin 	switch (io->scsiio.tag_type) {
995ee7f31c0SAlexander Motin 	case CTL_TAG_ORDERED:
996ee7f31c0SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
997ee7f31c0SAlexander Motin 		break;
998ee7f31c0SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
999ee7f31c0SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1000ee7f31c0SAlexander Motin 		break;
1001ee7f31c0SAlexander Motin 	case CTL_TAG_UNTAGGED:
1002ee7f31c0SAlexander Motin 	case CTL_TAG_SIMPLE:
1003ee7f31c0SAlexander Motin 	case CTL_TAG_ACA:
1004ee7f31c0SAlexander Motin 	default:
1005ee7f31c0SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1006ee7f31c0SAlexander Motin 		break;
1007ee7f31c0SAlexander Motin 	}
1008ee7f31c0SAlexander Motin 
100966df9136SAlexander Motin 	if (lbalen->flags & SWS_UNMAP) {
101066df9136SAlexander Motin 		beio->io_offset = lbalen->lba * be_lun->blocksize;
101166df9136SAlexander Motin 		beio->io_len = (uint64_t)lbalen->len * be_lun->blocksize;
1012ee7f31c0SAlexander Motin 		beio->bio_cmd = BIO_DELETE;
1013ee7f31c0SAlexander Motin 		beio->ds_trans_type = DEVSTAT_FREE;
1014ee7f31c0SAlexander Motin 
1015ee7f31c0SAlexander Motin 		be_lun->unmap(be_lun, beio);
1016ee7f31c0SAlexander Motin 		return;
1017ee7f31c0SAlexander Motin 	}
1018ee7f31c0SAlexander Motin 
1019ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_WRITE;
1020ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_WRITE;
1021ee7f31c0SAlexander Motin 
1022ee7f31c0SAlexander Motin 	DPRINTF("WRITE SAME at LBA %jx len %u\n",
102366df9136SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len);
1024ee7f31c0SAlexander Motin 
102566df9136SAlexander Motin 	len_left = (uint64_t)lbalen->len * be_lun->blocksize;
1026ee7f31c0SAlexander Motin 	for (i = 0, lba = 0; i < CTLBLK_MAX_SEGS && len_left > 0; i++) {
1027ee7f31c0SAlexander Motin 
1028ee7f31c0SAlexander Motin 		/*
1029ee7f31c0SAlexander Motin 		 * Setup the S/G entry for this chunk.
1030ee7f31c0SAlexander Motin 		 */
103108a7cce5SAlexander Motin 		seglen = MIN(CTLBLK_MAX_SEG, len_left);
1032ee7f31c0SAlexander Motin 		seglen -= seglen % be_lun->blocksize;
1033ee7f31c0SAlexander Motin 		beio->sg_segs[i].len = seglen;
1034ee7f31c0SAlexander Motin 		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1035ee7f31c0SAlexander Motin 
1036ee7f31c0SAlexander Motin 		DPRINTF("segment %d addr %p len %zd\n", i,
1037ee7f31c0SAlexander Motin 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1038ee7f31c0SAlexander Motin 
1039ee7f31c0SAlexander Motin 		beio->num_segs++;
1040ee7f31c0SAlexander Motin 		len_left -= seglen;
1041ee7f31c0SAlexander Motin 
1042ee7f31c0SAlexander Motin 		buf = beio->sg_segs[i].addr;
1043ee7f31c0SAlexander Motin 		end = buf + seglen;
1044ee7f31c0SAlexander Motin 		for (; buf < end; buf += be_lun->blocksize) {
1045ee7f31c0SAlexander Motin 			memcpy(buf, io->scsiio.kern_data_ptr, be_lun->blocksize);
104666df9136SAlexander Motin 			if (lbalen->flags & SWS_LBDATA)
104766df9136SAlexander Motin 				scsi_ulto4b(lbalen->lba + lba, buf);
1048ee7f31c0SAlexander Motin 			lba++;
1049ee7f31c0SAlexander Motin 		}
1050ee7f31c0SAlexander Motin 	}
1051ee7f31c0SAlexander Motin 
105266df9136SAlexander Motin 	beio->io_offset = lbalen->lba * be_lun->blocksize;
1053ee7f31c0SAlexander Motin 	beio->io_len = lba * be_lun->blocksize;
1054ee7f31c0SAlexander Motin 
1055ee7f31c0SAlexander Motin 	/* We can not do all in one run. Correct and schedule rerun. */
1056ee7f31c0SAlexander Motin 	if (len_left > 0) {
105766df9136SAlexander Motin 		lbalen->lba += lba;
105866df9136SAlexander Motin 		lbalen->len -= lba;
1059ee7f31c0SAlexander Motin 		beio->beio_cont = ctl_be_block_cw_done_ws;
1060ee7f31c0SAlexander Motin 	}
1061ee7f31c0SAlexander Motin 
1062ee7f31c0SAlexander Motin 	be_lun->dispatch(be_lun, beio);
1063ee7f31c0SAlexander Motin }
1064ee7f31c0SAlexander Motin 
1065ee7f31c0SAlexander Motin static void
1066ee7f31c0SAlexander Motin ctl_be_block_cw_dispatch_unmap(struct ctl_be_block_lun *be_lun,
1067ee7f31c0SAlexander Motin 			    union ctl_io *io)
1068ee7f31c0SAlexander Motin {
1069ee7f31c0SAlexander Motin 	struct ctl_be_block_io *beio;
1070ee7f31c0SAlexander Motin 	struct ctl_be_block_softc *softc;
107166df9136SAlexander Motin 	struct ctl_ptr_len_flags *ptrlen;
1072ee7f31c0SAlexander Motin 
1073ee7f31c0SAlexander Motin 	DPRINTF("entered\n");
1074ee7f31c0SAlexander Motin 
1075e86a4142SAlexander Motin 	beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1076ee7f31c0SAlexander Motin 	softc = be_lun->softc;
107766df9136SAlexander Motin 	ptrlen = (struct ctl_ptr_len_flags *)&io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
1078ee7f31c0SAlexander Motin 
107966df9136SAlexander Motin 	if (ptrlen->flags != 0 || be_lun->unmap == NULL) {
1080ee7f31c0SAlexander Motin 		ctl_free_beio(beio);
1081ee7f31c0SAlexander Motin 		ctl_set_invalid_field(&io->scsiio,
1082ee7f31c0SAlexander Motin 				      /*sks_valid*/ 0,
1083ee7f31c0SAlexander Motin 				      /*command*/ 1,
1084ee7f31c0SAlexander Motin 				      /*field*/ 0,
1085ee7f31c0SAlexander Motin 				      /*bit_valid*/ 0,
1086ee7f31c0SAlexander Motin 				      /*bit*/ 0);
1087ee7f31c0SAlexander Motin 		ctl_config_write_done(io);
1088ee7f31c0SAlexander Motin 		return;
1089ee7f31c0SAlexander Motin 	}
1090ee7f31c0SAlexander Motin 
1091ee7f31c0SAlexander Motin 	/*
1092ee7f31c0SAlexander Motin 	 * If the I/O came down with an ordered or head of queue tag, set
1093ee7f31c0SAlexander Motin 	 * the BIO_ORDERED attribute.  For head of queue tags, that's
1094ee7f31c0SAlexander Motin 	 * pretty much the best we can do.
1095ee7f31c0SAlexander Motin 	 */
1096ee7f31c0SAlexander Motin 	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1097ee7f31c0SAlexander Motin 	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1098ee7f31c0SAlexander Motin 		beio->bio_flags = BIO_ORDERED;
1099ee7f31c0SAlexander Motin 
1100ee7f31c0SAlexander Motin 	switch (io->scsiio.tag_type) {
1101ee7f31c0SAlexander Motin 	case CTL_TAG_ORDERED:
1102ee7f31c0SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1103ee7f31c0SAlexander Motin 		break;
1104ee7f31c0SAlexander Motin 	case CTL_TAG_HEAD_OF_QUEUE:
1105ee7f31c0SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1106ee7f31c0SAlexander Motin 		break;
1107ee7f31c0SAlexander Motin 	case CTL_TAG_UNTAGGED:
1108ee7f31c0SAlexander Motin 	case CTL_TAG_SIMPLE:
1109ee7f31c0SAlexander Motin 	case CTL_TAG_ACA:
1110ee7f31c0SAlexander Motin 	default:
1111ee7f31c0SAlexander Motin 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1112ee7f31c0SAlexander Motin 		break;
1113ee7f31c0SAlexander Motin 	}
1114ee7f31c0SAlexander Motin 
1115ee7f31c0SAlexander Motin 	beio->io_len = 0;
1116ee7f31c0SAlexander Motin 	beio->io_offset = -1;
1117ee7f31c0SAlexander Motin 
1118ee7f31c0SAlexander Motin 	beio->bio_cmd = BIO_DELETE;
1119ee7f31c0SAlexander Motin 	beio->ds_trans_type = DEVSTAT_FREE;
1120ee7f31c0SAlexander Motin 
112166df9136SAlexander Motin 	DPRINTF("UNMAP\n");
1122ee7f31c0SAlexander Motin 
1123ee7f31c0SAlexander Motin 	be_lun->unmap(be_lun, beio);
1124ee7f31c0SAlexander Motin }
1125ee7f31c0SAlexander Motin 
1126ee7f31c0SAlexander Motin static void
1127ee7f31c0SAlexander Motin ctl_be_block_cw_done(struct ctl_be_block_io *beio)
1128ee7f31c0SAlexander Motin {
1129ee7f31c0SAlexander Motin 	union ctl_io *io;
1130ee7f31c0SAlexander Motin 
1131ee7f31c0SAlexander Motin 	io = beio->io;
1132ee7f31c0SAlexander Motin 	ctl_free_beio(beio);
1133ee7f31c0SAlexander Motin 	ctl_config_write_done(io);
1134ee7f31c0SAlexander Motin }
1135ee7f31c0SAlexander Motin 
1136ee7f31c0SAlexander Motin static void
1137130f4520SKenneth D. Merry ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
1138130f4520SKenneth D. Merry 			 union ctl_io *io)
1139130f4520SKenneth D. Merry {
1140130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1141130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1142130f4520SKenneth D. Merry 
1143130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1144130f4520SKenneth D. Merry 
1145130f4520SKenneth D. Merry 	softc = be_lun->softc;
1146130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1147130f4520SKenneth D. Merry 	beio->io = io;
1148130f4520SKenneth D. Merry 	beio->lun = be_lun;
1149ee7f31c0SAlexander Motin 	beio->beio_cont = ctl_be_block_cw_done;
1150e86a4142SAlexander Motin 	PRIV(io)->ptr = (void *)beio;
1151130f4520SKenneth D. Merry 
1152130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
1153130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
1154130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
1155d2a0972dSEdward Tomasz Napierala 		beio->bio_cmd = BIO_FLUSH;
1156130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_NO_DATA;
1157130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1158130f4520SKenneth D. Merry 		beio->io_len = 0;
1159130f4520SKenneth D. Merry 		be_lun->lun_flush(be_lun, beio);
1160130f4520SKenneth D. Merry 		break;
1161ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
1162ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
1163ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_ws(be_lun, io);
1164ee7f31c0SAlexander Motin 		break;
1165ee7f31c0SAlexander Motin 	case UNMAP:
1166ee7f31c0SAlexander Motin 		ctl_be_block_cw_dispatch_unmap(be_lun, io);
1167ee7f31c0SAlexander Motin 		break;
1168130f4520SKenneth D. Merry 	default:
1169130f4520SKenneth D. Merry 		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
1170130f4520SKenneth D. Merry 		break;
1171130f4520SKenneth D. Merry 	}
1172130f4520SKenneth D. Merry }
1173130f4520SKenneth D. Merry 
1174d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
1175d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
1176d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
1177d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
1178130f4520SKenneth D. Merry 
1179130f4520SKenneth D. Merry static void
118008a7cce5SAlexander Motin ctl_be_block_next(struct ctl_be_block_io *beio)
118108a7cce5SAlexander Motin {
118208a7cce5SAlexander Motin 	struct ctl_be_block_lun *be_lun;
118308a7cce5SAlexander Motin 	union ctl_io *io;
118408a7cce5SAlexander Motin 
118508a7cce5SAlexander Motin 	io = beio->io;
118608a7cce5SAlexander Motin 	be_lun = beio->lun;
118708a7cce5SAlexander Motin 	ctl_free_beio(beio);
1188*ead2f117SAlexander Motin 	if ((io->io_hdr.flags & CTL_FLAG_ABORT) ||
1189*ead2f117SAlexander Motin 	    ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
1190*ead2f117SAlexander Motin 	     (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
119111b569f7SAlexander Motin 		ctl_data_submit_done(io);
119208a7cce5SAlexander Motin 		return;
119308a7cce5SAlexander Motin 	}
119408a7cce5SAlexander Motin 
119508a7cce5SAlexander Motin 	io->io_hdr.status &= ~CTL_STATUS_MASK;
119608a7cce5SAlexander Motin 	io->io_hdr.status |= CTL_STATUS_NONE;
119708a7cce5SAlexander Motin 
119808a7cce5SAlexander Motin 	mtx_lock(&be_lun->lock);
119908a7cce5SAlexander Motin 	/*
120008a7cce5SAlexander Motin 	 * XXX KDM make sure that links is okay to use at this point.
120108a7cce5SAlexander Motin 	 * Otherwise, we either need to add another field to ctl_io_hdr,
120208a7cce5SAlexander Motin 	 * or deal with resource allocation here.
120308a7cce5SAlexander Motin 	 */
120408a7cce5SAlexander Motin 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
120508a7cce5SAlexander Motin 	mtx_unlock(&be_lun->lock);
120608a7cce5SAlexander Motin 
120708a7cce5SAlexander Motin 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
120808a7cce5SAlexander Motin }
120908a7cce5SAlexander Motin 
121008a7cce5SAlexander Motin static void
1211130f4520SKenneth D. Merry ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
1212130f4520SKenneth D. Merry 			   union ctl_io *io)
1213130f4520SKenneth D. Merry {
1214130f4520SKenneth D. Merry 	struct ctl_be_block_io *beio;
1215130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
121611b569f7SAlexander Motin 	struct ctl_lba_len_flags *lbalen;
1217e86a4142SAlexander Motin 	struct ctl_ptr_len_flags *bptrlen;
1218e86a4142SAlexander Motin 	uint64_t len_left, lbas;
1219130f4520SKenneth D. Merry 	int i;
1220130f4520SKenneth D. Merry 
1221130f4520SKenneth D. Merry 	softc = be_lun->softc;
1222130f4520SKenneth D. Merry 
1223130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1224130f4520SKenneth D. Merry 
122511b569f7SAlexander Motin 	lbalen = ARGS(io);
122611b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1227130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
122811b569f7SAlexander Motin 	} else {
122911b569f7SAlexander Motin 		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
1230130f4520SKenneth D. Merry 	}
1231130f4520SKenneth D. Merry 
1232130f4520SKenneth D. Merry 	beio = ctl_alloc_beio(softc);
1233130f4520SKenneth D. Merry 	beio->io = io;
1234130f4520SKenneth D. Merry 	beio->lun = be_lun;
1235e86a4142SAlexander Motin 	bptrlen = PRIV(io);
1236e86a4142SAlexander Motin 	bptrlen->ptr = (void *)beio;
1237130f4520SKenneth D. Merry 
1238130f4520SKenneth D. Merry 	/*
1239130f4520SKenneth D. Merry 	 * If the I/O came down with an ordered or head of queue tag, set
1240130f4520SKenneth D. Merry 	 * the BIO_ORDERED attribute.  For head of queue tags, that's
1241130f4520SKenneth D. Merry 	 * pretty much the best we can do.
1242130f4520SKenneth D. Merry 	 *
1243130f4520SKenneth D. Merry 	 * XXX KDM we don't have a great way to easily know about the FUA
1244130f4520SKenneth D. Merry 	 * bit right now (it is decoded in ctl_read_write(), but we don't
1245130f4520SKenneth D. Merry 	 * pass that knowledge to the backend), and in any case we would
1246130f4520SKenneth D. Merry 	 * need to determine how to handle it.
1247130f4520SKenneth D. Merry 	 */
1248130f4520SKenneth D. Merry 	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1249130f4520SKenneth D. Merry 	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1250130f4520SKenneth D. Merry 		beio->bio_flags = BIO_ORDERED;
1251130f4520SKenneth D. Merry 
1252130f4520SKenneth D. Merry 	switch (io->scsiio.tag_type) {
1253130f4520SKenneth D. Merry 	case CTL_TAG_ORDERED:
1254130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1255130f4520SKenneth D. Merry 		break;
1256130f4520SKenneth D. Merry 	case CTL_TAG_HEAD_OF_QUEUE:
1257130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1258130f4520SKenneth D. Merry 		break;
1259130f4520SKenneth D. Merry 	case CTL_TAG_UNTAGGED:
1260130f4520SKenneth D. Merry 	case CTL_TAG_SIMPLE:
1261130f4520SKenneth D. Merry 	case CTL_TAG_ACA:
1262130f4520SKenneth D. Merry 	default:
1263130f4520SKenneth D. Merry 		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1264130f4520SKenneth D. Merry 		break;
1265130f4520SKenneth D. Merry 	}
1266130f4520SKenneth D. Merry 
126711b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_WRITE) {
1268130f4520SKenneth D. Merry 		beio->bio_cmd = BIO_WRITE;
1269130f4520SKenneth D. Merry 		beio->ds_trans_type = DEVSTAT_WRITE;
127011b569f7SAlexander Motin 	} else {
127111b569f7SAlexander Motin 		beio->bio_cmd = BIO_READ;
127211b569f7SAlexander Motin 		beio->ds_trans_type = DEVSTAT_READ;
1273130f4520SKenneth D. Merry 	}
1274130f4520SKenneth D. Merry 
127508a7cce5SAlexander Motin 	DPRINTF("%s at LBA %jx len %u @%ju\n",
1276130f4520SKenneth D. Merry 	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1277e86a4142SAlexander Motin 	       (uintmax_t)lbalen->lba, lbalen->len, bptrlen->len);
127811b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE)
127911b569f7SAlexander Motin 		lbas = CTLBLK_HALF_IO_SIZE;
128011b569f7SAlexander Motin 	else
128111b569f7SAlexander Motin 		lbas = CTLBLK_MAX_IO_SIZE;
128211b569f7SAlexander Motin 	lbas = MIN(lbalen->len - bptrlen->len, lbas / be_lun->blocksize);
1283e86a4142SAlexander Motin 	beio->io_offset = (lbalen->lba + bptrlen->len) * be_lun->blocksize;
1284e86a4142SAlexander Motin 	beio->io_len = lbas * be_lun->blocksize;
1285e86a4142SAlexander Motin 	bptrlen->len += lbas;
1286130f4520SKenneth D. Merry 
128708a7cce5SAlexander Motin 	for (i = 0, len_left = beio->io_len; len_left > 0; i++) {
128808a7cce5SAlexander Motin 		KASSERT(i < CTLBLK_MAX_SEGS, ("Too many segs (%d >= %d)",
128908a7cce5SAlexander Motin 		    i, CTLBLK_MAX_SEGS));
1290130f4520SKenneth D. Merry 
1291130f4520SKenneth D. Merry 		/*
1292130f4520SKenneth D. Merry 		 * Setup the S/G entry for this chunk.
1293130f4520SKenneth D. Merry 		 */
129408a7cce5SAlexander Motin 		beio->sg_segs[i].len = min(CTLBLK_MAX_SEG, len_left);
1295130f4520SKenneth D. Merry 		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1296130f4520SKenneth D. Merry 
1297130f4520SKenneth D. Merry 		DPRINTF("segment %d addr %p len %zd\n", i,
1298130f4520SKenneth D. Merry 			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1299130f4520SKenneth D. Merry 
130011b569f7SAlexander Motin 		/* Set up second segment for compare operation. */
130111b569f7SAlexander Motin 		if (lbalen->flags & CTL_LLF_COMPARE) {
130211b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].len =
130311b569f7SAlexander Motin 			    beio->sg_segs[i].len;
130411b569f7SAlexander Motin 			beio->sg_segs[i + CTLBLK_HALF_SEGS].addr =
130511b569f7SAlexander Motin 			    uma_zalloc(be_lun->lun_zone, M_WAITOK);
130611b569f7SAlexander Motin 		}
130711b569f7SAlexander Motin 
1308130f4520SKenneth D. Merry 		beio->num_segs++;
1309130f4520SKenneth D. Merry 		len_left -= beio->sg_segs[i].len;
1310130f4520SKenneth D. Merry 	}
1311e86a4142SAlexander Motin 	if (bptrlen->len < lbalen->len)
131208a7cce5SAlexander Motin 		beio->beio_cont = ctl_be_block_next;
131308a7cce5SAlexander Motin 	io->scsiio.be_move_done = ctl_be_block_move_done;
131411b569f7SAlexander Motin 	/* For compare we have separate S/G lists for read and datamove. */
131511b569f7SAlexander Motin 	if (lbalen->flags & CTL_LLF_COMPARE)
131611b569f7SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)&beio->sg_segs[CTLBLK_HALF_SEGS];
131711b569f7SAlexander Motin 	else
131808a7cce5SAlexander Motin 		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
131908a7cce5SAlexander Motin 	io->scsiio.kern_data_len = beio->io_len;
132008a7cce5SAlexander Motin 	io->scsiio.kern_data_resid = 0;
132108a7cce5SAlexander Motin 	io->scsiio.kern_sg_entries = beio->num_segs;
132208a7cce5SAlexander Motin 	io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1323130f4520SKenneth D. Merry 
1324130f4520SKenneth D. Merry 	/*
1325130f4520SKenneth D. Merry 	 * For the read case, we need to read the data into our buffers and
1326130f4520SKenneth D. Merry 	 * then we can send it back to the user.  For the write case, we
1327130f4520SKenneth D. Merry 	 * need to get the data from the user first.
1328130f4520SKenneth D. Merry 	 */
1329130f4520SKenneth D. Merry 	if (beio->bio_cmd == BIO_READ) {
1330130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1331130f4520SKenneth D. Merry 		be_lun->dispatch(be_lun, beio);
1332130f4520SKenneth D. Merry 	} else {
1333130f4520SKenneth D. Merry 		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1334130f4520SKenneth D. Merry #ifdef CTL_TIME_IO
1335130f4520SKenneth D. Merry         	getbintime(&io->io_hdr.dma_start_bt);
1336130f4520SKenneth D. Merry #endif
1337130f4520SKenneth D. Merry 		ctl_datamove(io);
1338130f4520SKenneth D. Merry 	}
1339130f4520SKenneth D. Merry }
1340130f4520SKenneth D. Merry 
1341130f4520SKenneth D. Merry static void
1342130f4520SKenneth D. Merry ctl_be_block_worker(void *context, int pending)
1343130f4520SKenneth D. Merry {
1344130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1345130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1346130f4520SKenneth D. Merry 	union ctl_io *io;
1347130f4520SKenneth D. Merry 
1348130f4520SKenneth D. Merry 	be_lun = (struct ctl_be_block_lun *)context;
1349130f4520SKenneth D. Merry 	softc = be_lun->softc;
1350130f4520SKenneth D. Merry 
1351130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1352130f4520SKenneth D. Merry 
1353130f4520SKenneth D. Merry 	mtx_lock(&be_lun->lock);
1354130f4520SKenneth D. Merry 	for (;;) {
1355130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1356130f4520SKenneth D. Merry 		if (io != NULL) {
1357130f4520SKenneth D. Merry 			struct ctl_be_block_io *beio;
1358130f4520SKenneth D. Merry 
1359130f4520SKenneth D. Merry 			DPRINTF("datamove queue\n");
1360130f4520SKenneth D. Merry 
1361130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1362130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
1363130f4520SKenneth D. Merry 
1364130f4520SKenneth D. Merry 			mtx_unlock(&be_lun->lock);
1365130f4520SKenneth D. Merry 
1366e86a4142SAlexander Motin 			beio = (struct ctl_be_block_io *)PRIV(io)->ptr;
1367130f4520SKenneth D. Merry 
1368130f4520SKenneth D. Merry 			be_lun->dispatch(be_lun, beio);
1369130f4520SKenneth D. Merry 
1370130f4520SKenneth D. Merry 			mtx_lock(&be_lun->lock);
1371130f4520SKenneth D. Merry 			continue;
1372130f4520SKenneth D. Merry 		}
1373130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1374130f4520SKenneth D. Merry 		if (io != NULL) {
1375130f4520SKenneth D. Merry 
1376130f4520SKenneth D. Merry 			DPRINTF("config write queue\n");
1377130f4520SKenneth D. Merry 
1378130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1379130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
1380130f4520SKenneth D. Merry 
1381130f4520SKenneth D. Merry 			mtx_unlock(&be_lun->lock);
1382130f4520SKenneth D. Merry 
1383130f4520SKenneth D. Merry 			ctl_be_block_cw_dispatch(be_lun, io);
1384130f4520SKenneth D. Merry 
1385130f4520SKenneth D. Merry 			mtx_lock(&be_lun->lock);
1386130f4520SKenneth D. Merry 			continue;
1387130f4520SKenneth D. Merry 		}
1388130f4520SKenneth D. Merry 		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1389130f4520SKenneth D. Merry 		if (io != NULL) {
1390130f4520SKenneth D. Merry 			DPRINTF("input queue\n");
1391130f4520SKenneth D. Merry 
1392130f4520SKenneth D. Merry 			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1393130f4520SKenneth D. Merry 				      ctl_io_hdr, links);
1394130f4520SKenneth D. Merry 			mtx_unlock(&be_lun->lock);
1395130f4520SKenneth D. Merry 
1396130f4520SKenneth D. Merry 			/*
1397130f4520SKenneth D. Merry 			 * We must drop the lock, since this routine and
1398130f4520SKenneth D. Merry 			 * its children may sleep.
1399130f4520SKenneth D. Merry 			 */
1400130f4520SKenneth D. Merry 			ctl_be_block_dispatch(be_lun, io);
1401130f4520SKenneth D. Merry 
1402130f4520SKenneth D. Merry 			mtx_lock(&be_lun->lock);
1403130f4520SKenneth D. Merry 			continue;
1404130f4520SKenneth D. Merry 		}
1405130f4520SKenneth D. Merry 
1406130f4520SKenneth D. Merry 		/*
1407130f4520SKenneth D. Merry 		 * If we get here, there is no work left in the queues, so
1408130f4520SKenneth D. Merry 		 * just break out and let the task queue go to sleep.
1409130f4520SKenneth D. Merry 		 */
1410130f4520SKenneth D. Merry 		break;
1411130f4520SKenneth D. Merry 	}
1412130f4520SKenneth D. Merry 	mtx_unlock(&be_lun->lock);
1413130f4520SKenneth D. Merry }
1414130f4520SKenneth D. Merry 
1415130f4520SKenneth D. Merry /*
1416130f4520SKenneth D. Merry  * Entry point from CTL to the backend for I/O.  We queue everything to a
1417130f4520SKenneth D. Merry  * work thread, so this just puts the I/O on a queue and wakes up the
1418130f4520SKenneth D. Merry  * thread.
1419130f4520SKenneth D. Merry  */
1420130f4520SKenneth D. Merry static int
1421130f4520SKenneth D. Merry ctl_be_block_submit(union ctl_io *io)
1422130f4520SKenneth D. Merry {
1423130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1424130f4520SKenneth D. Merry 	struct ctl_be_lun *ctl_be_lun;
1425130f4520SKenneth D. Merry 
1426130f4520SKenneth D. Merry 	DPRINTF("entered\n");
1427130f4520SKenneth D. Merry 
1428130f4520SKenneth D. Merry 	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1429130f4520SKenneth D. Merry 		CTL_PRIV_BACKEND_LUN].ptr;
1430130f4520SKenneth D. Merry 	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1431130f4520SKenneth D. Merry 
1432130f4520SKenneth D. Merry 	/*
1433130f4520SKenneth D. Merry 	 * Make sure we only get SCSI I/O.
1434130f4520SKenneth D. Merry 	 */
1435130f4520SKenneth D. Merry 	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1436130f4520SKenneth D. Merry 		"%#x) encountered", io->io_hdr.io_type));
1437130f4520SKenneth D. Merry 
1438e86a4142SAlexander Motin 	PRIV(io)->len = 0;
1439e86a4142SAlexander Motin 
1440130f4520SKenneth D. Merry 	mtx_lock(&be_lun->lock);
1441130f4520SKenneth D. Merry 	/*
1442130f4520SKenneth D. Merry 	 * XXX KDM make sure that links is okay to use at this point.
1443130f4520SKenneth D. Merry 	 * Otherwise, we either need to add another field to ctl_io_hdr,
1444130f4520SKenneth D. Merry 	 * or deal with resource allocation here.
1445130f4520SKenneth D. Merry 	 */
1446130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1447130f4520SKenneth D. Merry 	mtx_unlock(&be_lun->lock);
1448130f4520SKenneth D. Merry 	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1449130f4520SKenneth D. Merry 
14509c71cd5aSAlexander Motin 	return (CTL_RETVAL_COMPLETE);
1451130f4520SKenneth D. Merry }
1452130f4520SKenneth D. Merry 
1453130f4520SKenneth D. Merry static int
1454130f4520SKenneth D. Merry ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1455130f4520SKenneth D. Merry 			int flag, struct thread *td)
1456130f4520SKenneth D. Merry {
1457130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
1458130f4520SKenneth D. Merry 	int error;
1459130f4520SKenneth D. Merry 
1460130f4520SKenneth D. Merry 	softc = &backend_block_softc;
1461130f4520SKenneth D. Merry 
1462130f4520SKenneth D. Merry 	error = 0;
1463130f4520SKenneth D. Merry 
1464130f4520SKenneth D. Merry 	switch (cmd) {
1465130f4520SKenneth D. Merry 	case CTL_LUN_REQ: {
1466130f4520SKenneth D. Merry 		struct ctl_lun_req *lun_req;
1467130f4520SKenneth D. Merry 
1468130f4520SKenneth D. Merry 		lun_req = (struct ctl_lun_req *)addr;
1469130f4520SKenneth D. Merry 
1470130f4520SKenneth D. Merry 		switch (lun_req->reqtype) {
1471130f4520SKenneth D. Merry 		case CTL_LUNREQ_CREATE:
1472130f4520SKenneth D. Merry 			error = ctl_be_block_create(softc, lun_req);
1473130f4520SKenneth D. Merry 			break;
1474130f4520SKenneth D. Merry 		case CTL_LUNREQ_RM:
1475130f4520SKenneth D. Merry 			error = ctl_be_block_rm(softc, lun_req);
1476130f4520SKenneth D. Merry 			break;
147781177295SEdward Tomasz Napierala 		case CTL_LUNREQ_MODIFY:
147881177295SEdward Tomasz Napierala 			error = ctl_be_block_modify(softc, lun_req);
147981177295SEdward Tomasz Napierala 			break;
1480130f4520SKenneth D. Merry 		default:
1481130f4520SKenneth D. Merry 			lun_req->status = CTL_LUN_ERROR;
1482130f4520SKenneth D. Merry 			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1483130f4520SKenneth D. Merry 				 "%s: invalid LUN request type %d", __func__,
1484130f4520SKenneth D. Merry 				 lun_req->reqtype);
1485130f4520SKenneth D. Merry 			break;
1486130f4520SKenneth D. Merry 		}
1487130f4520SKenneth D. Merry 		break;
1488130f4520SKenneth D. Merry 	}
1489130f4520SKenneth D. Merry 	default:
1490130f4520SKenneth D. Merry 		error = ENOTTY;
1491130f4520SKenneth D. Merry 		break;
1492130f4520SKenneth D. Merry 	}
1493130f4520SKenneth D. Merry 
1494130f4520SKenneth D. Merry 	return (error);
1495130f4520SKenneth D. Merry }
1496130f4520SKenneth D. Merry 
1497130f4520SKenneth D. Merry static int
1498130f4520SKenneth D. Merry ctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1499130f4520SKenneth D. Merry {
1500130f4520SKenneth D. Merry 	struct ctl_be_block_filedata *file_data;
1501130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
1502130f4520SKenneth D. Merry 	struct vattr		      vattr;
1503130f4520SKenneth D. Merry 	int			      error;
1504130f4520SKenneth D. Merry 
1505130f4520SKenneth D. Merry 	error = 0;
1506130f4520SKenneth D. Merry 	file_data = &be_lun->backend.file;
1507130f4520SKenneth D. Merry 	params = &req->reqdata.create;
1508130f4520SKenneth D. Merry 
1509130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1510130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_file;
1511130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_file;
1512130f4520SKenneth D. Merry 
1513130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1514130f4520SKenneth D. Merry 	if (error != 0) {
1515130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1516130f4520SKenneth D. Merry 			 "error calling VOP_GETATTR() for file %s",
1517130f4520SKenneth D. Merry 			 be_lun->dev_path);
1518130f4520SKenneth D. Merry 		return (error);
1519130f4520SKenneth D. Merry 	}
1520130f4520SKenneth D. Merry 
1521130f4520SKenneth D. Merry 	/*
1522130f4520SKenneth D. Merry 	 * Verify that we have the ability to upgrade to exclusive
1523130f4520SKenneth D. Merry 	 * access on this file so we can trap errors at open instead
1524130f4520SKenneth D. Merry 	 * of reporting them during first access.
1525130f4520SKenneth D. Merry 	 */
1526130f4520SKenneth D. Merry 	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1527130f4520SKenneth D. Merry 		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1528130f4520SKenneth D. Merry 		if (be_lun->vn->v_iflag & VI_DOOMED) {
1529130f4520SKenneth D. Merry 			error = EBADF;
1530130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
1531130f4520SKenneth D. Merry 				 "error locking file %s", be_lun->dev_path);
1532130f4520SKenneth D. Merry 			return (error);
1533130f4520SKenneth D. Merry 		}
1534130f4520SKenneth D. Merry 	}
1535130f4520SKenneth D. Merry 
1536130f4520SKenneth D. Merry 
1537130f4520SKenneth D. Merry 	file_data->cred = crhold(curthread->td_ucred);
153881177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0)
153981177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
154081177295SEdward Tomasz Napierala 	else
1541130f4520SKenneth D. Merry 		be_lun->size_bytes = vattr.va_size;
1542130f4520SKenneth D. Merry 	/*
1543130f4520SKenneth D. Merry 	 * We set the multi thread flag for file operations because all
1544130f4520SKenneth D. Merry 	 * filesystems (in theory) are capable of allowing multiple readers
1545130f4520SKenneth D. Merry 	 * of a file at once.  So we want to get the maximum possible
1546130f4520SKenneth D. Merry 	 * concurrency.
1547130f4520SKenneth D. Merry 	 */
1548130f4520SKenneth D. Merry 	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1549130f4520SKenneth D. Merry 
1550130f4520SKenneth D. Merry 	/*
1551130f4520SKenneth D. Merry 	 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
1552130f4520SKenneth D. Merry 	 * With ZFS, it is 131072 bytes.  Block sizes that large don't work
1553130f4520SKenneth D. Merry 	 * with disklabel and UFS on FreeBSD at least.  Large block sizes
1554130f4520SKenneth D. Merry 	 * may not work with other OSes as well.  So just export a sector
1555130f4520SKenneth D. Merry 	 * size of 512 bytes, which should work with any OS or
1556130f4520SKenneth D. Merry 	 * application.  Since our backing is a file, any block size will
1557130f4520SKenneth D. Merry 	 * work fine for the backing store.
1558130f4520SKenneth D. Merry 	 */
1559130f4520SKenneth D. Merry #if 0
1560130f4520SKenneth D. Merry 	be_lun->blocksize= vattr.va_blocksize;
1561130f4520SKenneth D. Merry #endif
1562130f4520SKenneth D. Merry 	if (params->blocksize_bytes != 0)
1563130f4520SKenneth D. Merry 		be_lun->blocksize = params->blocksize_bytes;
1564130f4520SKenneth D. Merry 	else
1565130f4520SKenneth D. Merry 		be_lun->blocksize = 512;
1566130f4520SKenneth D. Merry 
1567130f4520SKenneth D. Merry 	/*
1568130f4520SKenneth D. Merry 	 * Sanity check.  The media size has to be at least one
1569130f4520SKenneth D. Merry 	 * sector long.
1570130f4520SKenneth D. Merry 	 */
1571130f4520SKenneth D. Merry 	if (be_lun->size_bytes < be_lun->blocksize) {
1572130f4520SKenneth D. Merry 		error = EINVAL;
1573130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1574130f4520SKenneth D. Merry 			 "file %s size %ju < block size %u", be_lun->dev_path,
1575130f4520SKenneth D. Merry 			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1576130f4520SKenneth D. Merry 	}
1577130f4520SKenneth D. Merry 	return (error);
1578130f4520SKenneth D. Merry }
1579130f4520SKenneth D. Merry 
1580130f4520SKenneth D. Merry static int
1581130f4520SKenneth D. Merry ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1582130f4520SKenneth D. Merry {
1583130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
1584130f4520SKenneth D. Merry 	struct vattr		      vattr;
1585130f4520SKenneth D. Merry 	struct cdev		     *dev;
1586130f4520SKenneth D. Merry 	struct cdevsw		     *devsw;
1587130f4520SKenneth D. Merry 	int			      error;
1588f6012722SAlexander Motin 	off_t			      ps, pss, po, pos;
1589130f4520SKenneth D. Merry 
1590130f4520SKenneth D. Merry 	params = &req->reqdata.create;
1591130f4520SKenneth D. Merry 
1592130f4520SKenneth D. Merry 	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1593130f4520SKenneth D. Merry 	be_lun->dispatch = ctl_be_block_dispatch_dev;
1594130f4520SKenneth D. Merry 	be_lun->lun_flush = ctl_be_block_flush_dev;
1595ee7f31c0SAlexander Motin 	be_lun->unmap = ctl_be_block_unmap_dev;
1596130f4520SKenneth D. Merry 	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1597130f4520SKenneth D. Merry 	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1598130f4520SKenneth D. Merry 					     &be_lun->backend.dev.dev_ref);
1599130f4520SKenneth D. Merry 	if (be_lun->backend.dev.csw == NULL)
1600130f4520SKenneth D. Merry 		panic("Unable to retrieve device switch");
1601130f4520SKenneth D. Merry 
1602130f4520SKenneth D. Merry 	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1603130f4520SKenneth D. Merry 	if (error) {
1604130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1605130f4520SKenneth D. Merry 			 "%s: error getting vnode attributes for device %s",
1606130f4520SKenneth D. Merry 			 __func__, be_lun->dev_path);
1607130f4520SKenneth D. Merry 		return (error);
1608130f4520SKenneth D. Merry 	}
1609130f4520SKenneth D. Merry 
1610130f4520SKenneth D. Merry 	dev = be_lun->vn->v_rdev;
1611130f4520SKenneth D. Merry 	devsw = dev->si_devsw;
1612130f4520SKenneth D. Merry 	if (!devsw->d_ioctl) {
1613130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1614130f4520SKenneth D. Merry 			 "%s: no d_ioctl for device %s!", __func__,
1615130f4520SKenneth D. Merry 			 be_lun->dev_path);
1616130f4520SKenneth D. Merry 		return (ENODEV);
1617130f4520SKenneth D. Merry 	}
1618130f4520SKenneth D. Merry 
1619130f4520SKenneth D. Merry 	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1620130f4520SKenneth D. Merry 			       (caddr_t)&be_lun->blocksize, FREAD,
1621130f4520SKenneth D. Merry 			       curthread);
1622130f4520SKenneth D. Merry 	if (error) {
1623130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1624130f4520SKenneth D. Merry 			 "%s: error %d returned for DIOCGSECTORSIZE ioctl "
1625130f4520SKenneth D. Merry 			 "on %s!", __func__, error, be_lun->dev_path);
1626130f4520SKenneth D. Merry 		return (error);
1627130f4520SKenneth D. Merry 	}
1628130f4520SKenneth D. Merry 
1629130f4520SKenneth D. Merry 	/*
1630130f4520SKenneth D. Merry 	 * If the user has asked for a blocksize that is greater than the
1631130f4520SKenneth D. Merry 	 * backing device's blocksize, we can do it only if the blocksize
1632130f4520SKenneth D. Merry 	 * the user is asking for is an even multiple of the underlying
1633130f4520SKenneth D. Merry 	 * device's blocksize.
1634130f4520SKenneth D. Merry 	 */
1635130f4520SKenneth D. Merry 	if ((params->blocksize_bytes != 0)
1636130f4520SKenneth D. Merry 	 && (params->blocksize_bytes > be_lun->blocksize)) {
1637130f4520SKenneth D. Merry 		uint32_t bs_multiple, tmp_blocksize;
1638130f4520SKenneth D. Merry 
1639130f4520SKenneth D. Merry 		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1640130f4520SKenneth D. Merry 
1641130f4520SKenneth D. Merry 		tmp_blocksize = bs_multiple * be_lun->blocksize;
1642130f4520SKenneth D. Merry 
1643130f4520SKenneth D. Merry 		if (tmp_blocksize == params->blocksize_bytes) {
1644130f4520SKenneth D. Merry 			be_lun->blocksize = params->blocksize_bytes;
1645130f4520SKenneth D. Merry 		} else {
1646130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
1647130f4520SKenneth D. Merry 				 "%s: requested blocksize %u is not an even "
1648130f4520SKenneth D. Merry 				 "multiple of backing device blocksize %u",
1649130f4520SKenneth D. Merry 				 __func__, params->blocksize_bytes,
1650130f4520SKenneth D. Merry 				 be_lun->blocksize);
1651130f4520SKenneth D. Merry 			return (EINVAL);
1652130f4520SKenneth D. Merry 
1653130f4520SKenneth D. Merry 		}
1654130f4520SKenneth D. Merry 	} else if ((params->blocksize_bytes != 0)
1655130f4520SKenneth D. Merry 		&& (params->blocksize_bytes != be_lun->blocksize)) {
1656130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1657130f4520SKenneth D. Merry 			 "%s: requested blocksize %u < backing device "
1658130f4520SKenneth D. Merry 			 "blocksize %u", __func__, params->blocksize_bytes,
1659130f4520SKenneth D. Merry 			 be_lun->blocksize);
1660130f4520SKenneth D. Merry 		return (EINVAL);
1661130f4520SKenneth D. Merry 	}
1662130f4520SKenneth D. Merry 
1663130f4520SKenneth D. Merry 	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1664130f4520SKenneth D. Merry 			       (caddr_t)&be_lun->size_bytes, FREAD,
1665130f4520SKenneth D. Merry 			       curthread);
1666130f4520SKenneth D. Merry 	if (error) {
1667130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
166881177295SEdward Tomasz Napierala 			 "%s: error %d returned for DIOCGMEDIASIZE "
166981177295SEdward Tomasz Napierala 			 " ioctl on %s!", __func__, error,
167081177295SEdward Tomasz Napierala 			 be_lun->dev_path);
1671130f4520SKenneth D. Merry 		return (error);
1672130f4520SKenneth D. Merry 	}
1673130f4520SKenneth D. Merry 
167481177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
167581177295SEdward Tomasz Napierala 		if (params->lun_size_bytes > be_lun->size_bytes) {
167681177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
167781177295SEdward Tomasz Napierala 				 "%s: requested LUN size %ju > backing device "
167881177295SEdward Tomasz Napierala 				 "size %ju", __func__,
167981177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
168081177295SEdward Tomasz Napierala 				 (uintmax_t)be_lun->size_bytes);
168181177295SEdward Tomasz Napierala 			return (EINVAL);
1682130f4520SKenneth D. Merry 		}
1683130f4520SKenneth D. Merry 
168481177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
168581177295SEdward Tomasz Napierala 	}
168681177295SEdward Tomasz Napierala 
1687f6012722SAlexander Motin 	error = devsw->d_ioctl(dev, DIOCGSTRIPESIZE,
1688f6012722SAlexander Motin 			       (caddr_t)&ps, FREAD, curthread);
1689f6012722SAlexander Motin 	if (error)
1690f6012722SAlexander Motin 		ps = po = 0;
1691f6012722SAlexander Motin 	else {
1692f6012722SAlexander Motin 		error = devsw->d_ioctl(dev, DIOCGSTRIPEOFFSET,
1693f6012722SAlexander Motin 				       (caddr_t)&po, FREAD, curthread);
1694f6012722SAlexander Motin 		if (error)
1695f6012722SAlexander Motin 			po = 0;
1696f6012722SAlexander Motin 	}
1697f6012722SAlexander Motin 	pss = ps / be_lun->blocksize;
1698f6012722SAlexander Motin 	pos = po / be_lun->blocksize;
1699f6012722SAlexander Motin 	if ((pss > 0) && (pss * be_lun->blocksize == ps) && (pss >= pos) &&
1700f6012722SAlexander Motin 	    ((pss & (pss - 1)) == 0) && (pos * be_lun->blocksize == po)) {
1701f6012722SAlexander Motin 		be_lun->pblockexp = fls(pss) - 1;
1702f6012722SAlexander Motin 		be_lun->pblockoff = (pss - pos) % pss;
1703f6012722SAlexander Motin 	}
1704f6012722SAlexander Motin 
170581177295SEdward Tomasz Napierala 	return (0);
170681177295SEdward Tomasz Napierala }
1707130f4520SKenneth D. Merry 
1708130f4520SKenneth D. Merry static int
1709130f4520SKenneth D. Merry ctl_be_block_close(struct ctl_be_block_lun *be_lun)
1710130f4520SKenneth D. Merry {
1711130f4520SKenneth D. Merry 	DROP_GIANT();
1712130f4520SKenneth D. Merry 	if (be_lun->vn) {
1713130f4520SKenneth D. Merry 		int flags = FREAD | FWRITE;
1714130f4520SKenneth D. Merry 
1715130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
1716130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
1717130f4520SKenneth D. Merry 			if (be_lun->backend.dev.csw) {
1718130f4520SKenneth D. Merry 				dev_relthread(be_lun->backend.dev.cdev,
1719130f4520SKenneth D. Merry 					      be_lun->backend.dev.dev_ref);
1720130f4520SKenneth D. Merry 				be_lun->backend.dev.csw  = NULL;
1721130f4520SKenneth D. Merry 				be_lun->backend.dev.cdev = NULL;
1722130f4520SKenneth D. Merry 			}
1723130f4520SKenneth D. Merry 			break;
1724130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
1725130f4520SKenneth D. Merry 			break;
1726130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
1727025a2301SEdward Tomasz Napierala 			break;
1728130f4520SKenneth D. Merry 		default:
1729130f4520SKenneth D. Merry 			panic("Unexpected backend type.");
1730130f4520SKenneth D. Merry 			break;
1731130f4520SKenneth D. Merry 		}
1732130f4520SKenneth D. Merry 
1733130f4520SKenneth D. Merry 		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1734130f4520SKenneth D. Merry 		be_lun->vn = NULL;
1735130f4520SKenneth D. Merry 
1736130f4520SKenneth D. Merry 		switch (be_lun->dev_type) {
1737130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_DEV:
1738130f4520SKenneth D. Merry 			break;
1739130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_FILE:
1740130f4520SKenneth D. Merry 			if (be_lun->backend.file.cred != NULL) {
1741130f4520SKenneth D. Merry 				crfree(be_lun->backend.file.cred);
1742130f4520SKenneth D. Merry 				be_lun->backend.file.cred = NULL;
1743130f4520SKenneth D. Merry 			}
1744130f4520SKenneth D. Merry 			break;
1745130f4520SKenneth D. Merry 		case CTL_BE_BLOCK_NONE:
1746025a2301SEdward Tomasz Napierala 			break;
1747130f4520SKenneth D. Merry 		default:
1748130f4520SKenneth D. Merry 			panic("Unexpected backend type.");
1749130f4520SKenneth D. Merry 			break;
1750130f4520SKenneth D. Merry 		}
1751130f4520SKenneth D. Merry 	}
1752130f4520SKenneth D. Merry 	PICKUP_GIANT();
1753130f4520SKenneth D. Merry 
1754130f4520SKenneth D. Merry 	return (0);
1755130f4520SKenneth D. Merry }
1756130f4520SKenneth D. Merry 
1757130f4520SKenneth D. Merry static int
1758130f4520SKenneth D. Merry ctl_be_block_open(struct ctl_be_block_softc *softc,
1759130f4520SKenneth D. Merry 		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1760130f4520SKenneth D. Merry {
1761130f4520SKenneth D. Merry 	struct nameidata nd;
1762130f4520SKenneth D. Merry 	int		 flags;
1763130f4520SKenneth D. Merry 	int		 error;
1764130f4520SKenneth D. Merry 
1765130f4520SKenneth D. Merry 	/*
1766130f4520SKenneth D. Merry 	 * XXX KDM allow a read-only option?
1767130f4520SKenneth D. Merry 	 */
1768130f4520SKenneth D. Merry 	flags = FREAD | FWRITE;
1769130f4520SKenneth D. Merry 	error = 0;
1770130f4520SKenneth D. Merry 
1771130f4520SKenneth D. Merry 	if (rootvnode == NULL) {
1772130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1773130f4520SKenneth D. Merry 			 "%s: Root filesystem is not mounted", __func__);
1774130f4520SKenneth D. Merry 		return (1);
1775130f4520SKenneth D. Merry 	}
1776130f4520SKenneth D. Merry 
1777130f4520SKenneth D. Merry 	if (!curthread->td_proc->p_fd->fd_cdir) {
1778130f4520SKenneth D. Merry 		curthread->td_proc->p_fd->fd_cdir = rootvnode;
1779130f4520SKenneth D. Merry 		VREF(rootvnode);
1780130f4520SKenneth D. Merry 	}
1781130f4520SKenneth D. Merry 	if (!curthread->td_proc->p_fd->fd_rdir) {
1782130f4520SKenneth D. Merry 		curthread->td_proc->p_fd->fd_rdir = rootvnode;
1783130f4520SKenneth D. Merry 		VREF(rootvnode);
1784130f4520SKenneth D. Merry 	}
1785130f4520SKenneth D. Merry 	if (!curthread->td_proc->p_fd->fd_jdir) {
1786130f4520SKenneth D. Merry 		curthread->td_proc->p_fd->fd_jdir = rootvnode;
1787130f4520SKenneth D. Merry 		VREF(rootvnode);
1788130f4520SKenneth D. Merry 	}
1789130f4520SKenneth D. Merry 
1790130f4520SKenneth D. Merry  again:
1791130f4520SKenneth D. Merry 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1792130f4520SKenneth D. Merry 	error = vn_open(&nd, &flags, 0, NULL);
1793130f4520SKenneth D. Merry 	if (error) {
1794130f4520SKenneth D. Merry 		/*
1795130f4520SKenneth D. Merry 		 * This is the only reasonable guess we can make as far as
1796130f4520SKenneth D. Merry 		 * path if the user doesn't give us a fully qualified path.
1797130f4520SKenneth D. Merry 		 * If they want to specify a file, they need to specify the
1798130f4520SKenneth D. Merry 		 * full path.
1799130f4520SKenneth D. Merry 		 */
1800130f4520SKenneth D. Merry 		if (be_lun->dev_path[0] != '/') {
1801130f4520SKenneth D. Merry 			char *dev_path = "/dev/";
1802130f4520SKenneth D. Merry 			char *dev_name;
1803130f4520SKenneth D. Merry 
1804130f4520SKenneth D. Merry 			/* Try adding device path at beginning of name */
1805130f4520SKenneth D. Merry 			dev_name = malloc(strlen(be_lun->dev_path)
1806130f4520SKenneth D. Merry 					+ strlen(dev_path) + 1,
1807130f4520SKenneth D. Merry 					  M_CTLBLK, M_WAITOK);
1808130f4520SKenneth D. Merry 			if (dev_name) {
1809130f4520SKenneth D. Merry 				sprintf(dev_name, "%s%s", dev_path,
1810130f4520SKenneth D. Merry 					be_lun->dev_path);
1811130f4520SKenneth D. Merry 				free(be_lun->dev_path, M_CTLBLK);
1812130f4520SKenneth D. Merry 				be_lun->dev_path = dev_name;
1813130f4520SKenneth D. Merry 				goto again;
1814130f4520SKenneth D. Merry 			}
1815130f4520SKenneth D. Merry 		}
1816130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1817130f4520SKenneth D. Merry 			 "%s: error opening %s", __func__, be_lun->dev_path);
1818130f4520SKenneth D. Merry 		return (error);
1819130f4520SKenneth D. Merry 	}
1820130f4520SKenneth D. Merry 
1821130f4520SKenneth D. Merry 	NDFREE(&nd, NDF_ONLY_PNBUF);
1822130f4520SKenneth D. Merry 
1823130f4520SKenneth D. Merry 	be_lun->vn = nd.ni_vp;
1824130f4520SKenneth D. Merry 
1825130f4520SKenneth D. Merry 	/* We only support disks and files. */
1826130f4520SKenneth D. Merry 	if (vn_isdisk(be_lun->vn, &error)) {
1827130f4520SKenneth D. Merry 		error = ctl_be_block_open_dev(be_lun, req);
1828130f4520SKenneth D. Merry 	} else if (be_lun->vn->v_type == VREG) {
1829130f4520SKenneth D. Merry 		error = ctl_be_block_open_file(be_lun, req);
1830130f4520SKenneth D. Merry 	} else {
1831130f4520SKenneth D. Merry 		error = EINVAL;
1832130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1833025a2301SEdward Tomasz Napierala 			 "%s is not a disk or plain file", be_lun->dev_path);
1834130f4520SKenneth D. Merry 	}
1835130f4520SKenneth D. Merry 	VOP_UNLOCK(be_lun->vn, 0);
1836130f4520SKenneth D. Merry 
1837130f4520SKenneth D. Merry 	if (error != 0) {
1838130f4520SKenneth D. Merry 		ctl_be_block_close(be_lun);
1839130f4520SKenneth D. Merry 		return (error);
1840130f4520SKenneth D. Merry 	}
1841130f4520SKenneth D. Merry 
1842130f4520SKenneth D. Merry 	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1843130f4520SKenneth D. Merry 	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1844130f4520SKenneth D. Merry 
1845130f4520SKenneth D. Merry 	return (0);
1846130f4520SKenneth D. Merry }
1847130f4520SKenneth D. Merry 
1848130f4520SKenneth D. Merry static int
1849130f4520SKenneth D. Merry ctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1850130f4520SKenneth D. Merry {
1851130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
1852130f4520SKenneth D. Merry 	struct ctl_lun_create_params *params;
185357a5db13SAlexander Motin 	char num_thread_str[16];
1854130f4520SKenneth D. Merry 	char tmpstr[32];
185557a5db13SAlexander Motin 	char *value;
1856ee7f31c0SAlexander Motin 	int retval, num_threads, unmap;
185757a5db13SAlexander Motin 	int tmp_num_threads;
1858130f4520SKenneth D. Merry 
1859130f4520SKenneth D. Merry 	params = &req->reqdata.create;
1860130f4520SKenneth D. Merry 	retval = 0;
1861130f4520SKenneth D. Merry 
1862130f4520SKenneth D. Merry 	num_threads = cbb_num_threads;
1863130f4520SKenneth D. Merry 
1864130f4520SKenneth D. Merry 	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1865130f4520SKenneth D. Merry 
1866130f4520SKenneth D. Merry 	be_lun->softc = softc;
1867130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->input_queue);
1868130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->config_write_queue);
1869130f4520SKenneth D. Merry 	STAILQ_INIT(&be_lun->datamove_queue);
1870130f4520SKenneth D. Merry 	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1871130f4520SKenneth D. Merry 	mtx_init(&be_lun->lock, be_lun->lunname, NULL, MTX_DEF);
187257a5db13SAlexander Motin 	ctl_init_opts(&be_lun->ctl_be_lun, req);
1873130f4520SKenneth D. Merry 
187408a7cce5SAlexander Motin 	be_lun->lun_zone = uma_zcreate(be_lun->lunname, CTLBLK_MAX_SEG,
1875eeb94054SAlexander Motin 	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1876130f4520SKenneth D. Merry 
1877130f4520SKenneth D. Merry 	if (be_lun->lun_zone == NULL) {
1878130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
1879130f4520SKenneth D. Merry 			 "%s: error allocating UMA zone", __func__);
1880130f4520SKenneth D. Merry 		goto bailout_error;
1881130f4520SKenneth D. Merry 	}
1882130f4520SKenneth D. Merry 
1883130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1884130f4520SKenneth D. Merry 		be_lun->ctl_be_lun.lun_type = params->device_type;
1885130f4520SKenneth D. Merry 	else
1886130f4520SKenneth D. Merry 		be_lun->ctl_be_lun.lun_type = T_DIRECT;
1887130f4520SKenneth D. Merry 
1888130f4520SKenneth D. Merry 	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1889eb3687a6SAlexander Motin 		value = ctl_get_opt(&be_lun->ctl_be_lun, "file");
1890eb3687a6SAlexander Motin 		if (value == NULL) {
1891130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
1892130f4520SKenneth D. Merry 				 "%s: no file argument specified", __func__);
1893130f4520SKenneth D. Merry 			goto bailout_error;
1894130f4520SKenneth D. Merry 		}
1895eb3687a6SAlexander Motin 		be_lun->dev_path = strdup(value, M_CTLBLK);
1896130f4520SKenneth D. Merry 
1897130f4520SKenneth D. Merry 		retval = ctl_be_block_open(softc, be_lun, req);
1898130f4520SKenneth D. Merry 		if (retval != 0) {
1899130f4520SKenneth D. Merry 			retval = 0;
1900130f4520SKenneth D. Merry 			goto bailout_error;
1901130f4520SKenneth D. Merry 		}
1902130f4520SKenneth D. Merry 
1903130f4520SKenneth D. Merry 		/*
1904130f4520SKenneth D. Merry 		 * Tell the user the size of the file/device.
1905130f4520SKenneth D. Merry 		 */
1906130f4520SKenneth D. Merry 		params->lun_size_bytes = be_lun->size_bytes;
1907130f4520SKenneth D. Merry 
1908130f4520SKenneth D. Merry 		/*
1909130f4520SKenneth D. Merry 		 * The maximum LBA is the size - 1.
1910130f4520SKenneth D. Merry 		 */
1911130f4520SKenneth D. Merry 		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
1912130f4520SKenneth D. Merry 	} else {
1913130f4520SKenneth D. Merry 		/*
1914130f4520SKenneth D. Merry 		 * For processor devices, we don't have any size.
1915130f4520SKenneth D. Merry 		 */
1916130f4520SKenneth D. Merry 		be_lun->blocksize = 0;
1917f6012722SAlexander Motin 		be_lun->pblockexp = 0;
1918f6012722SAlexander Motin 		be_lun->pblockoff = 0;
1919130f4520SKenneth D. Merry 		be_lun->size_blocks = 0;
1920130f4520SKenneth D. Merry 		be_lun->size_bytes = 0;
1921130f4520SKenneth D. Merry 		be_lun->ctl_be_lun.maxlba = 0;
1922130f4520SKenneth D. Merry 		params->lun_size_bytes = 0;
1923130f4520SKenneth D. Merry 
1924130f4520SKenneth D. Merry 		/*
1925130f4520SKenneth D. Merry 		 * Default to just 1 thread for processor devices.
1926130f4520SKenneth D. Merry 		 */
1927130f4520SKenneth D. Merry 		num_threads = 1;
1928130f4520SKenneth D. Merry 	}
1929130f4520SKenneth D. Merry 
1930130f4520SKenneth D. Merry 	/*
1931130f4520SKenneth D. Merry 	 * XXX This searching loop might be refactored to be combined with
1932130f4520SKenneth D. Merry 	 * the loop above,
1933130f4520SKenneth D. Merry 	 */
193457a5db13SAlexander Motin 	value = ctl_get_opt(&be_lun->ctl_be_lun, "num_threads");
193557a5db13SAlexander Motin 	if (value != NULL) {
193657a5db13SAlexander Motin 		tmp_num_threads = strtol(value, NULL, 0);
1937130f4520SKenneth D. Merry 
1938130f4520SKenneth D. Merry 		/*
1939130f4520SKenneth D. Merry 		 * We don't let the user specify less than one
1940130f4520SKenneth D. Merry 		 * thread, but hope he's clueful enough not to
1941130f4520SKenneth D. Merry 		 * specify 1000 threads.
1942130f4520SKenneth D. Merry 		 */
1943130f4520SKenneth D. Merry 		if (tmp_num_threads < 1) {
1944130f4520SKenneth D. Merry 			snprintf(req->error_str, sizeof(req->error_str),
1945130f4520SKenneth D. Merry 				 "%s: invalid number of threads %s",
1946130f4520SKenneth D. Merry 			         __func__, num_thread_str);
1947130f4520SKenneth D. Merry 			goto bailout_error;
1948130f4520SKenneth D. Merry 		}
1949130f4520SKenneth D. Merry 		num_threads = tmp_num_threads;
195057a5db13SAlexander Motin 	}
195157a5db13SAlexander Motin 	unmap = 0;
195257a5db13SAlexander Motin 	value = ctl_get_opt(&be_lun->ctl_be_lun, "unmap");
195357a5db13SAlexander Motin 	if (value != NULL && strcmp(value, "on") == 0)
1954ee7f31c0SAlexander Motin 		unmap = 1;
1955130f4520SKenneth D. Merry 
1956130f4520SKenneth D. Merry 	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
1957130f4520SKenneth D. Merry 	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
1958ee7f31c0SAlexander Motin 	if (unmap)
1959ee7f31c0SAlexander Motin 		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_UNMAP;
1960130f4520SKenneth D. Merry 	be_lun->ctl_be_lun.be_lun = be_lun;
1961130f4520SKenneth D. Merry 	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
1962f6012722SAlexander Motin 	be_lun->ctl_be_lun.pblockexp = be_lun->pblockexp;
1963f6012722SAlexander Motin 	be_lun->ctl_be_lun.pblockoff = be_lun->pblockoff;
1964130f4520SKenneth D. Merry 	/* Tell the user the blocksize we ended up using */
1965130f4520SKenneth D. Merry 	params->blocksize_bytes = be_lun->blocksize;
1966130f4520SKenneth D. Merry 	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
1967130f4520SKenneth D. Merry 		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
1968130f4520SKenneth D. Merry 		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
1969130f4520SKenneth D. Merry 	} else
1970130f4520SKenneth D. Merry 		be_lun->ctl_be_lun.req_lun_id = 0;
1971130f4520SKenneth D. Merry 
1972130f4520SKenneth D. Merry 	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
1973130f4520SKenneth D. Merry 	be_lun->ctl_be_lun.lun_config_status =
1974130f4520SKenneth D. Merry 		ctl_be_block_lun_config_status;
1975130f4520SKenneth D. Merry 	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
1976130f4520SKenneth D. Merry 
1977130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
1978130f4520SKenneth D. Merry 		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
1979130f4520SKenneth D. Merry 			 softc->num_luns);
1980130f4520SKenneth D. Merry 		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
1981130f4520SKenneth D. Merry 			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
1982130f4520SKenneth D. Merry 			sizeof(tmpstr)));
1983130f4520SKenneth D. Merry 
1984130f4520SKenneth D. Merry 		/* Tell the user what we used for a serial number */
1985130f4520SKenneth D. Merry 		strncpy((char *)params->serial_num, tmpstr,
1986130f4520SKenneth D. Merry 			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
1987130f4520SKenneth D. Merry 	} else {
1988130f4520SKenneth D. Merry 		strncpy((char *)be_lun->ctl_be_lun.serial_num,
1989130f4520SKenneth D. Merry 			params->serial_num,
1990130f4520SKenneth D. Merry 			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
1991130f4520SKenneth D. Merry 			sizeof(params->serial_num)));
1992130f4520SKenneth D. Merry 	}
1993130f4520SKenneth D. Merry 	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
1994130f4520SKenneth D. Merry 		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
1995130f4520SKenneth D. Merry 		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
1996130f4520SKenneth D. Merry 			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
1997130f4520SKenneth D. Merry 			sizeof(tmpstr)));
1998130f4520SKenneth D. Merry 
1999130f4520SKenneth D. Merry 		/* Tell the user what we used for a device ID */
2000130f4520SKenneth D. Merry 		strncpy((char *)params->device_id, tmpstr,
2001130f4520SKenneth D. Merry 			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
2002130f4520SKenneth D. Merry 	} else {
2003130f4520SKenneth D. Merry 		strncpy((char *)be_lun->ctl_be_lun.device_id,
2004130f4520SKenneth D. Merry 			params->device_id,
2005130f4520SKenneth D. Merry 			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
2006130f4520SKenneth D. Merry 				sizeof(params->device_id)));
2007130f4520SKenneth D. Merry 	}
2008130f4520SKenneth D. Merry 
2009130f4520SKenneth D. Merry 	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
2010130f4520SKenneth D. Merry 
2011130f4520SKenneth D. Merry 	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
2012130f4520SKenneth D. Merry 	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
2013130f4520SKenneth D. Merry 
2014130f4520SKenneth D. Merry 	if (be_lun->io_taskqueue == NULL) {
2015130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2016130f4520SKenneth D. Merry 			 "%s: Unable to create taskqueue", __func__);
2017130f4520SKenneth D. Merry 		goto bailout_error;
2018130f4520SKenneth D. Merry 	}
2019130f4520SKenneth D. Merry 
2020130f4520SKenneth D. Merry 	/*
2021130f4520SKenneth D. Merry 	 * Note that we start the same number of threads by default for
2022130f4520SKenneth D. Merry 	 * both the file case and the block device case.  For the file
2023130f4520SKenneth D. Merry 	 * case, we need multiple threads to allow concurrency, because the
2024130f4520SKenneth D. Merry 	 * vnode interface is designed to be a blocking interface.  For the
2025130f4520SKenneth D. Merry 	 * block device case, ZFS zvols at least will block the caller's
2026130f4520SKenneth D. Merry 	 * context in many instances, and so we need multiple threads to
2027130f4520SKenneth D. Merry 	 * overcome that problem.  Other block devices don't need as many
2028130f4520SKenneth D. Merry 	 * threads, but they shouldn't cause too many problems.
2029130f4520SKenneth D. Merry 	 *
2030130f4520SKenneth D. Merry 	 * If the user wants to just have a single thread for a block
2031130f4520SKenneth D. Merry 	 * device, he can specify that when the LUN is created, or change
2032130f4520SKenneth D. Merry 	 * the tunable/sysctl to alter the default number of threads.
2033130f4520SKenneth D. Merry 	 */
2034130f4520SKenneth D. Merry 	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
2035130f4520SKenneth D. Merry 					 /*num threads*/num_threads,
2036130f4520SKenneth D. Merry 					 /*priority*/PWAIT,
2037130f4520SKenneth D. Merry 					 /*thread name*/
2038130f4520SKenneth D. Merry 					 "%s taskq", be_lun->lunname);
2039130f4520SKenneth D. Merry 
2040130f4520SKenneth D. Merry 	if (retval != 0)
2041130f4520SKenneth D. Merry 		goto bailout_error;
2042130f4520SKenneth D. Merry 
2043130f4520SKenneth D. Merry 	be_lun->num_threads = num_threads;
2044130f4520SKenneth D. Merry 
2045130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2046130f4520SKenneth D. Merry 	softc->num_luns++;
2047130f4520SKenneth D. Merry 	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
2048130f4520SKenneth D. Merry 
2049130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2050130f4520SKenneth D. Merry 
2051130f4520SKenneth D. Merry 	retval = ctl_add_lun(&be_lun->ctl_be_lun);
2052130f4520SKenneth D. Merry 	if (retval != 0) {
2053130f4520SKenneth D. Merry 		mtx_lock(&softc->lock);
2054130f4520SKenneth D. Merry 		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2055130f4520SKenneth D. Merry 			      links);
2056130f4520SKenneth D. Merry 		softc->num_luns--;
2057130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2058130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2059130f4520SKenneth D. Merry 			 "%s: ctl_add_lun() returned error %d, see dmesg for "
2060130f4520SKenneth D. Merry 			"details", __func__, retval);
2061130f4520SKenneth D. Merry 		retval = 0;
2062130f4520SKenneth D. Merry 		goto bailout_error;
2063130f4520SKenneth D. Merry 	}
2064130f4520SKenneth D. Merry 
2065130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2066130f4520SKenneth D. Merry 
2067130f4520SKenneth D. Merry 	/*
2068130f4520SKenneth D. Merry 	 * Tell the config_status routine that we're waiting so it won't
2069130f4520SKenneth D. Merry 	 * clean up the LUN in the event of an error.
2070130f4520SKenneth D. Merry 	 */
2071130f4520SKenneth D. Merry 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2072130f4520SKenneth D. Merry 
2073130f4520SKenneth D. Merry 	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
2074130f4520SKenneth D. Merry 		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2075130f4520SKenneth D. Merry 		if (retval == EINTR)
2076130f4520SKenneth D. Merry 			break;
2077130f4520SKenneth D. Merry 	}
2078130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2079130f4520SKenneth D. Merry 
2080130f4520SKenneth D. Merry 	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
2081130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2082130f4520SKenneth D. Merry 			 "%s: LUN configuration error, see dmesg for details",
2083130f4520SKenneth D. Merry 			 __func__);
2084130f4520SKenneth D. Merry 		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
2085130f4520SKenneth D. Merry 			      links);
2086130f4520SKenneth D. Merry 		softc->num_luns--;
2087130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2088130f4520SKenneth D. Merry 		goto bailout_error;
2089130f4520SKenneth D. Merry 	} else {
2090130f4520SKenneth D. Merry 		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
2091130f4520SKenneth D. Merry 	}
2092130f4520SKenneth D. Merry 
2093130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2094130f4520SKenneth D. Merry 
2095130f4520SKenneth D. Merry 	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
2096130f4520SKenneth D. Merry 					       be_lun->blocksize,
2097130f4520SKenneth D. Merry 					       DEVSTAT_ALL_SUPPORTED,
2098130f4520SKenneth D. Merry 					       be_lun->ctl_be_lun.lun_type
2099130f4520SKenneth D. Merry 					       | DEVSTAT_TYPE_IF_OTHER,
2100130f4520SKenneth D. Merry 					       DEVSTAT_PRIORITY_OTHER);
2101130f4520SKenneth D. Merry 
2102130f4520SKenneth D. Merry 
2103130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2104130f4520SKenneth D. Merry 
2105130f4520SKenneth D. Merry 	return (retval);
2106130f4520SKenneth D. Merry 
2107130f4520SKenneth D. Merry bailout_error:
2108130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2109130f4520SKenneth D. Merry 
21109e005bbcSAlexander Motin 	if (be_lun->io_taskqueue != NULL)
21119e005bbcSAlexander Motin 		taskqueue_free(be_lun->io_taskqueue);
2112130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
21139e005bbcSAlexander Motin 	if (be_lun->dev_path != NULL)
2114130f4520SKenneth D. Merry 		free(be_lun->dev_path, M_CTLBLK);
21159e005bbcSAlexander Motin 	if (be_lun->lun_zone != NULL)
21169e005bbcSAlexander Motin 		uma_zdestroy(be_lun->lun_zone);
211757a5db13SAlexander Motin 	ctl_free_opts(&be_lun->ctl_be_lun);
21189e005bbcSAlexander Motin 	mtx_destroy(&be_lun->lock);
2119130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2120130f4520SKenneth D. Merry 
2121130f4520SKenneth D. Merry 	return (retval);
2122130f4520SKenneth D. Merry }
2123130f4520SKenneth D. Merry 
2124130f4520SKenneth D. Merry static int
2125130f4520SKenneth D. Merry ctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2126130f4520SKenneth D. Merry {
2127130f4520SKenneth D. Merry 	struct ctl_lun_rm_params *params;
2128130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2129130f4520SKenneth D. Merry 	int retval;
2130130f4520SKenneth D. Merry 
2131130f4520SKenneth D. Merry 	params = &req->reqdata.rm;
2132130f4520SKenneth D. Merry 
2133130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2134130f4520SKenneth D. Merry 
2135130f4520SKenneth D. Merry 	be_lun = NULL;
2136130f4520SKenneth D. Merry 
2137130f4520SKenneth D. Merry 	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2138130f4520SKenneth D. Merry 		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2139130f4520SKenneth D. Merry 			break;
2140130f4520SKenneth D. Merry 	}
2141130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2142130f4520SKenneth D. Merry 
2143130f4520SKenneth D. Merry 	if (be_lun == NULL) {
2144130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2145130f4520SKenneth D. Merry 			 "%s: LUN %u is not managed by the block backend",
2146130f4520SKenneth D. Merry 			 __func__, params->lun_id);
2147130f4520SKenneth D. Merry 		goto bailout_error;
2148130f4520SKenneth D. Merry 	}
2149130f4520SKenneth D. Merry 
2150130f4520SKenneth D. Merry 	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
2151130f4520SKenneth D. Merry 
2152130f4520SKenneth D. Merry 	if (retval != 0) {
2153130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2154130f4520SKenneth D. Merry 			 "%s: error %d returned from ctl_disable_lun() for "
2155130f4520SKenneth D. Merry 			 "LUN %d", __func__, retval, params->lun_id);
2156130f4520SKenneth D. Merry 		goto bailout_error;
2157130f4520SKenneth D. Merry 
2158130f4520SKenneth D. Merry 	}
2159130f4520SKenneth D. Merry 
2160130f4520SKenneth D. Merry 	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
2161130f4520SKenneth D. Merry 	if (retval != 0) {
2162130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2163130f4520SKenneth D. Merry 			 "%s: error %d returned from ctl_invalidate_lun() for "
2164130f4520SKenneth D. Merry 			 "LUN %d", __func__, retval, params->lun_id);
2165130f4520SKenneth D. Merry 		goto bailout_error;
2166130f4520SKenneth D. Merry 	}
2167130f4520SKenneth D. Merry 
2168130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2169130f4520SKenneth D. Merry 
2170130f4520SKenneth D. Merry 	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
2171130f4520SKenneth D. Merry 
2172130f4520SKenneth D. Merry 	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2173130f4520SKenneth D. Merry                 retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
2174130f4520SKenneth D. Merry                 if (retval == EINTR)
2175130f4520SKenneth D. Merry                         break;
2176130f4520SKenneth D. Merry         }
2177130f4520SKenneth D. Merry 
2178130f4520SKenneth D. Merry 	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
2179130f4520SKenneth D. Merry 
2180130f4520SKenneth D. Merry 	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
2181130f4520SKenneth D. Merry 		snprintf(req->error_str, sizeof(req->error_str),
2182130f4520SKenneth D. Merry 			 "%s: interrupted waiting for LUN to be freed",
2183130f4520SKenneth D. Merry 			 __func__);
2184130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2185130f4520SKenneth D. Merry 		goto bailout_error;
2186130f4520SKenneth D. Merry 	}
2187130f4520SKenneth D. Merry 
2188130f4520SKenneth D. Merry 	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
2189130f4520SKenneth D. Merry 
2190130f4520SKenneth D. Merry 	softc->num_luns--;
2191130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2192130f4520SKenneth D. Merry 
2193130f4520SKenneth D. Merry 	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
2194130f4520SKenneth D. Merry 
2195130f4520SKenneth D. Merry 	taskqueue_free(be_lun->io_taskqueue);
2196130f4520SKenneth D. Merry 
2197130f4520SKenneth D. Merry 	ctl_be_block_close(be_lun);
2198130f4520SKenneth D. Merry 
2199130f4520SKenneth D. Merry 	if (be_lun->disk_stats != NULL)
2200130f4520SKenneth D. Merry 		devstat_remove_entry(be_lun->disk_stats);
2201130f4520SKenneth D. Merry 
2202130f4520SKenneth D. Merry 	uma_zdestroy(be_lun->lun_zone);
2203130f4520SKenneth D. Merry 
220457a5db13SAlexander Motin 	ctl_free_opts(&be_lun->ctl_be_lun);
2205130f4520SKenneth D. Merry 	free(be_lun->dev_path, M_CTLBLK);
2206130f4520SKenneth D. Merry 
2207130f4520SKenneth D. Merry 	free(be_lun, M_CTLBLK);
2208130f4520SKenneth D. Merry 
2209130f4520SKenneth D. Merry 	req->status = CTL_LUN_OK;
2210130f4520SKenneth D. Merry 
2211130f4520SKenneth D. Merry 	return (0);
2212130f4520SKenneth D. Merry 
2213130f4520SKenneth D. Merry bailout_error:
2214130f4520SKenneth D. Merry 
2215130f4520SKenneth D. Merry 	req->status = CTL_LUN_ERROR;
2216130f4520SKenneth D. Merry 
2217130f4520SKenneth D. Merry 	return (0);
2218130f4520SKenneth D. Merry }
2219130f4520SKenneth D. Merry 
222081177295SEdward Tomasz Napierala static int
222181177295SEdward Tomasz Napierala ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
222281177295SEdward Tomasz Napierala 			 struct ctl_lun_req *req)
222381177295SEdward Tomasz Napierala {
222481177295SEdward Tomasz Napierala 	struct vattr vattr;
222581177295SEdward Tomasz Napierala 	int error;
222681177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
222781177295SEdward Tomasz Napierala 
222881177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
222981177295SEdward Tomasz Napierala 
223081177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
223181177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
223281177295SEdward Tomasz Napierala 	} else  {
223381177295SEdward Tomasz Napierala 		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
223481177295SEdward Tomasz Napierala 		if (error != 0) {
223581177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
223681177295SEdward Tomasz Napierala 				 "error calling VOP_GETATTR() for file %s",
223781177295SEdward Tomasz Napierala 				 be_lun->dev_path);
223881177295SEdward Tomasz Napierala 			return (error);
223981177295SEdward Tomasz Napierala 		}
224081177295SEdward Tomasz Napierala 
224181177295SEdward Tomasz Napierala 		be_lun->size_bytes = vattr.va_size;
224281177295SEdward Tomasz Napierala 	}
224381177295SEdward Tomasz Napierala 
224481177295SEdward Tomasz Napierala 	return (0);
224581177295SEdward Tomasz Napierala }
224681177295SEdward Tomasz Napierala 
224781177295SEdward Tomasz Napierala static int
224881177295SEdward Tomasz Napierala ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
224981177295SEdward Tomasz Napierala 			struct ctl_lun_req *req)
225081177295SEdward Tomasz Napierala {
225181177295SEdward Tomasz Napierala 	struct cdev *dev;
225281177295SEdward Tomasz Napierala 	struct cdevsw *devsw;
225381177295SEdward Tomasz Napierala 	int error;
225481177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
225581177295SEdward Tomasz Napierala 	uint64_t size_bytes;
225681177295SEdward Tomasz Napierala 
225781177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
225881177295SEdward Tomasz Napierala 
225981177295SEdward Tomasz Napierala 	dev = be_lun->vn->v_rdev;
226081177295SEdward Tomasz Napierala 	devsw = dev->si_devsw;
226181177295SEdward Tomasz Napierala 	if (!devsw->d_ioctl) {
226281177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
226381177295SEdward Tomasz Napierala 			 "%s: no d_ioctl for device %s!", __func__,
226481177295SEdward Tomasz Napierala 			 be_lun->dev_path);
226581177295SEdward Tomasz Napierala 		return (ENODEV);
226681177295SEdward Tomasz Napierala 	}
226781177295SEdward Tomasz Napierala 
226881177295SEdward Tomasz Napierala 	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
226981177295SEdward Tomasz Napierala 			       (caddr_t)&size_bytes, FREAD,
227081177295SEdward Tomasz Napierala 			       curthread);
227181177295SEdward Tomasz Napierala 	if (error) {
227281177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
227381177295SEdward Tomasz Napierala 			 "%s: error %d returned for DIOCGMEDIASIZE ioctl "
227481177295SEdward Tomasz Napierala 			 "on %s!", __func__, error, be_lun->dev_path);
227581177295SEdward Tomasz Napierala 		return (error);
227681177295SEdward Tomasz Napierala 	}
227781177295SEdward Tomasz Napierala 
227881177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
227981177295SEdward Tomasz Napierala 		if (params->lun_size_bytes > size_bytes) {
228081177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
228181177295SEdward Tomasz Napierala 				 "%s: requested LUN size %ju > backing device "
228281177295SEdward Tomasz Napierala 				 "size %ju", __func__,
228381177295SEdward Tomasz Napierala 				 (uintmax_t)params->lun_size_bytes,
228481177295SEdward Tomasz Napierala 				 (uintmax_t)size_bytes);
228581177295SEdward Tomasz Napierala 			return (EINVAL);
228681177295SEdward Tomasz Napierala 		}
228781177295SEdward Tomasz Napierala 
228881177295SEdward Tomasz Napierala 		be_lun->size_bytes = params->lun_size_bytes;
228981177295SEdward Tomasz Napierala 	} else {
229081177295SEdward Tomasz Napierala 		be_lun->size_bytes = size_bytes;
229181177295SEdward Tomasz Napierala 	}
229281177295SEdward Tomasz Napierala 
229381177295SEdward Tomasz Napierala 	return (0);
229481177295SEdward Tomasz Napierala }
229581177295SEdward Tomasz Napierala 
229681177295SEdward Tomasz Napierala static int
229781177295SEdward Tomasz Napierala ctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
229881177295SEdward Tomasz Napierala {
229981177295SEdward Tomasz Napierala 	struct ctl_lun_modify_params *params;
230081177295SEdward Tomasz Napierala 	struct ctl_be_block_lun *be_lun;
23015050aa86SKonstantin Belousov 	int error;
230281177295SEdward Tomasz Napierala 
230381177295SEdward Tomasz Napierala 	params = &req->reqdata.modify;
230481177295SEdward Tomasz Napierala 
230581177295SEdward Tomasz Napierala 	mtx_lock(&softc->lock);
230681177295SEdward Tomasz Napierala 
230781177295SEdward Tomasz Napierala 	be_lun = NULL;
230881177295SEdward Tomasz Napierala 
230981177295SEdward Tomasz Napierala 	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
231081177295SEdward Tomasz Napierala 		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
231181177295SEdward Tomasz Napierala 			break;
231281177295SEdward Tomasz Napierala 	}
231381177295SEdward Tomasz Napierala 	mtx_unlock(&softc->lock);
231481177295SEdward Tomasz Napierala 
231581177295SEdward Tomasz Napierala 	if (be_lun == NULL) {
231681177295SEdward Tomasz Napierala 		snprintf(req->error_str, sizeof(req->error_str),
231781177295SEdward Tomasz Napierala 			 "%s: LUN %u is not managed by the block backend",
231881177295SEdward Tomasz Napierala 			 __func__, params->lun_id);
231981177295SEdward Tomasz Napierala 		goto bailout_error;
232081177295SEdward Tomasz Napierala 	}
232181177295SEdward Tomasz Napierala 
232281177295SEdward Tomasz Napierala 	if (params->lun_size_bytes != 0) {
232381177295SEdward Tomasz Napierala 		if (params->lun_size_bytes < be_lun->blocksize) {
232481177295SEdward Tomasz Napierala 			snprintf(req->error_str, sizeof(req->error_str),
232581177295SEdward Tomasz Napierala 				"%s: LUN size %ju < blocksize %u", __func__,
232681177295SEdward Tomasz Napierala 				params->lun_size_bytes, be_lun->blocksize);
232781177295SEdward Tomasz Napierala 			goto bailout_error;
232881177295SEdward Tomasz Napierala 		}
232981177295SEdward Tomasz Napierala 	}
233081177295SEdward Tomasz Napierala 
233181177295SEdward Tomasz Napierala 	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
233281177295SEdward Tomasz Napierala 
233381177295SEdward Tomasz Napierala 	if (be_lun->vn->v_type == VREG)
233481177295SEdward Tomasz Napierala 		error = ctl_be_block_modify_file(be_lun, req);
233581177295SEdward Tomasz Napierala 	else
233681177295SEdward Tomasz Napierala 		error = ctl_be_block_modify_dev(be_lun, req);
233781177295SEdward Tomasz Napierala 
233881177295SEdward Tomasz Napierala 	VOP_UNLOCK(be_lun->vn, 0);
233981177295SEdward Tomasz Napierala 
234081177295SEdward Tomasz Napierala 	if (error != 0)
234181177295SEdward Tomasz Napierala 		goto bailout_error;
234281177295SEdward Tomasz Napierala 
234381177295SEdward Tomasz Napierala 	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
234481177295SEdward Tomasz Napierala 
234581177295SEdward Tomasz Napierala 	/*
234681177295SEdward Tomasz Napierala 	 * The maximum LBA is the size - 1.
234781177295SEdward Tomasz Napierala 	 *
234881177295SEdward Tomasz Napierala 	 * XXX: Note that this field is being updated without locking,
234981177295SEdward Tomasz Napierala 	 * 	which might cause problems on 32-bit architectures.
235081177295SEdward Tomasz Napierala 	 */
235181177295SEdward Tomasz Napierala 	be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
235281177295SEdward Tomasz Napierala 	ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
235381177295SEdward Tomasz Napierala 
235481177295SEdward Tomasz Napierala 	/* Tell the user the exact size we ended up using */
235581177295SEdward Tomasz Napierala 	params->lun_size_bytes = be_lun->size_bytes;
235681177295SEdward Tomasz Napierala 
235781177295SEdward Tomasz Napierala 	req->status = CTL_LUN_OK;
235881177295SEdward Tomasz Napierala 
235981177295SEdward Tomasz Napierala 	return (0);
236081177295SEdward Tomasz Napierala 
236181177295SEdward Tomasz Napierala bailout_error:
236281177295SEdward Tomasz Napierala 	req->status = CTL_LUN_ERROR;
236381177295SEdward Tomasz Napierala 
236481177295SEdward Tomasz Napierala 	return (0);
236581177295SEdward Tomasz Napierala }
236681177295SEdward Tomasz Napierala 
2367130f4520SKenneth D. Merry static void
2368130f4520SKenneth D. Merry ctl_be_block_lun_shutdown(void *be_lun)
2369130f4520SKenneth D. Merry {
2370130f4520SKenneth D. Merry 	struct ctl_be_block_lun *lun;
2371130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
2372130f4520SKenneth D. Merry 
2373130f4520SKenneth D. Merry 	lun = (struct ctl_be_block_lun *)be_lun;
2374130f4520SKenneth D. Merry 
2375130f4520SKenneth D. Merry 	softc = lun->softc;
2376130f4520SKenneth D. Merry 
2377130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2378130f4520SKenneth D. Merry 	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2379130f4520SKenneth D. Merry 	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2380130f4520SKenneth D. Merry 		wakeup(lun);
2381130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2382130f4520SKenneth D. Merry 
2383130f4520SKenneth D. Merry }
2384130f4520SKenneth D. Merry 
2385130f4520SKenneth D. Merry static void
2386130f4520SKenneth D. Merry ctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2387130f4520SKenneth D. Merry {
2388130f4520SKenneth D. Merry 	struct ctl_be_block_lun *lun;
2389130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
2390130f4520SKenneth D. Merry 
2391130f4520SKenneth D. Merry 	lun = (struct ctl_be_block_lun *)be_lun;
2392130f4520SKenneth D. Merry 	softc = lun->softc;
2393130f4520SKenneth D. Merry 
2394130f4520SKenneth D. Merry 	if (status == CTL_LUN_CONFIG_OK) {
2395130f4520SKenneth D. Merry 		mtx_lock(&softc->lock);
2396130f4520SKenneth D. Merry 		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2397130f4520SKenneth D. Merry 		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2398130f4520SKenneth D. Merry 			wakeup(lun);
2399130f4520SKenneth D. Merry 		mtx_unlock(&softc->lock);
2400130f4520SKenneth D. Merry 
2401130f4520SKenneth D. Merry 		/*
2402130f4520SKenneth D. Merry 		 * We successfully added the LUN, attempt to enable it.
2403130f4520SKenneth D. Merry 		 */
2404130f4520SKenneth D. Merry 		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2405130f4520SKenneth D. Merry 			printf("%s: ctl_enable_lun() failed!\n", __func__);
2406130f4520SKenneth D. Merry 			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2407130f4520SKenneth D. Merry 				printf("%s: ctl_invalidate_lun() failed!\n",
2408130f4520SKenneth D. Merry 				       __func__);
2409130f4520SKenneth D. Merry 			}
2410130f4520SKenneth D. Merry 		}
2411130f4520SKenneth D. Merry 
2412130f4520SKenneth D. Merry 		return;
2413130f4520SKenneth D. Merry 	}
2414130f4520SKenneth D. Merry 
2415130f4520SKenneth D. Merry 
2416130f4520SKenneth D. Merry 	mtx_lock(&softc->lock);
2417130f4520SKenneth D. Merry 	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2418130f4520SKenneth D. Merry 	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2419130f4520SKenneth D. Merry 	wakeup(lun);
2420130f4520SKenneth D. Merry 	mtx_unlock(&softc->lock);
2421130f4520SKenneth D. Merry }
2422130f4520SKenneth D. Merry 
2423130f4520SKenneth D. Merry 
2424130f4520SKenneth D. Merry static int
2425130f4520SKenneth D. Merry ctl_be_block_config_write(union ctl_io *io)
2426130f4520SKenneth D. Merry {
2427130f4520SKenneth D. Merry 	struct ctl_be_block_lun *be_lun;
2428130f4520SKenneth D. Merry 	struct ctl_be_lun *ctl_be_lun;
2429130f4520SKenneth D. Merry 	int retval;
2430130f4520SKenneth D. Merry 
2431130f4520SKenneth D. Merry 	retval = 0;
2432130f4520SKenneth D. Merry 
2433130f4520SKenneth D. Merry 	DPRINTF("entered\n");
2434130f4520SKenneth D. Merry 
2435130f4520SKenneth D. Merry 	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2436130f4520SKenneth D. Merry 		CTL_PRIV_BACKEND_LUN].ptr;
2437130f4520SKenneth D. Merry 	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2438130f4520SKenneth D. Merry 
2439130f4520SKenneth D. Merry 	switch (io->scsiio.cdb[0]) {
2440130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE:
2441130f4520SKenneth D. Merry 	case SYNCHRONIZE_CACHE_16:
2442ee7f31c0SAlexander Motin 	case WRITE_SAME_10:
2443ee7f31c0SAlexander Motin 	case WRITE_SAME_16:
2444ee7f31c0SAlexander Motin 	case UNMAP:
2445130f4520SKenneth D. Merry 		/*
2446130f4520SKenneth D. Merry 		 * The upper level CTL code will filter out any CDBs with
2447130f4520SKenneth D. Merry 		 * the immediate bit set and return the proper error.
2448130f4520SKenneth D. Merry 		 *
2449130f4520SKenneth D. Merry 		 * We don't really need to worry about what LBA range the
2450130f4520SKenneth D. Merry 		 * user asked to be synced out.  When they issue a sync
2451130f4520SKenneth D. Merry 		 * cache command, we'll sync out the whole thing.
2452130f4520SKenneth D. Merry 		 */
2453130f4520SKenneth D. Merry 		mtx_lock(&be_lun->lock);
2454130f4520SKenneth D. Merry 		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2455130f4520SKenneth D. Merry 				   links);
2456130f4520SKenneth D. Merry 		mtx_unlock(&be_lun->lock);
2457130f4520SKenneth D. Merry 		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2458130f4520SKenneth D. Merry 		break;
2459130f4520SKenneth D. Merry 	case START_STOP_UNIT: {
2460130f4520SKenneth D. Merry 		struct scsi_start_stop_unit *cdb;
2461130f4520SKenneth D. Merry 
2462130f4520SKenneth D. Merry 		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2463130f4520SKenneth D. Merry 
2464130f4520SKenneth D. Merry 		if (cdb->how & SSS_START)
2465130f4520SKenneth D. Merry 			retval = ctl_start_lun(ctl_be_lun);
2466130f4520SKenneth D. Merry 		else {
2467130f4520SKenneth D. Merry 			retval = ctl_stop_lun(ctl_be_lun);
2468130f4520SKenneth D. Merry 			/*
2469130f4520SKenneth D. Merry 			 * XXX KDM Copan-specific offline behavior.
2470130f4520SKenneth D. Merry 			 * Figure out a reasonable way to port this?
2471130f4520SKenneth D. Merry 			 */
2472130f4520SKenneth D. Merry #ifdef NEEDTOPORT
2473130f4520SKenneth D. Merry 			if ((retval == 0)
2474130f4520SKenneth D. Merry 			 && (cdb->byte2 & SSS_ONOFFLINE))
2475130f4520SKenneth D. Merry 				retval = ctl_lun_offline(ctl_be_lun);
2476130f4520SKenneth D. Merry #endif
2477130f4520SKenneth D. Merry 		}
2478130f4520SKenneth D. Merry 
2479130f4520SKenneth D. Merry 		/*
2480130f4520SKenneth D. Merry 		 * In general, the above routines should not fail.  They
2481130f4520SKenneth D. Merry 		 * just set state for the LUN.  So we've got something
2482130f4520SKenneth D. Merry 		 * pretty wrong here if we can't start or stop the LUN.
2483130f4520SKenneth D. Merry 		 */
2484130f4520SKenneth D. Merry 		if (retval != 0) {
2485130f4520SKenneth D. Merry 			ctl_set_internal_failure(&io->scsiio,
2486130f4520SKenneth D. Merry 						 /*sks_valid*/ 1,
2487130f4520SKenneth D. Merry 						 /*retry_count*/ 0xf051);
2488130f4520SKenneth D. Merry 			retval = CTL_RETVAL_COMPLETE;
2489130f4520SKenneth D. Merry 		} else {
2490130f4520SKenneth D. Merry 			ctl_set_success(&io->scsiio);
2491130f4520SKenneth D. Merry 		}
2492130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2493130f4520SKenneth D. Merry 		break;
2494130f4520SKenneth D. Merry 	}
2495130f4520SKenneth D. Merry 	default:
2496130f4520SKenneth D. Merry 		ctl_set_invalid_opcode(&io->scsiio);
2497130f4520SKenneth D. Merry 		ctl_config_write_done(io);
2498130f4520SKenneth D. Merry 		retval = CTL_RETVAL_COMPLETE;
2499130f4520SKenneth D. Merry 		break;
2500130f4520SKenneth D. Merry 	}
2501130f4520SKenneth D. Merry 
2502130f4520SKenneth D. Merry 	return (retval);
2503130f4520SKenneth D. Merry 
2504130f4520SKenneth D. Merry }
2505130f4520SKenneth D. Merry 
2506130f4520SKenneth D. Merry static int
2507130f4520SKenneth D. Merry ctl_be_block_config_read(union ctl_io *io)
2508130f4520SKenneth D. Merry {
2509130f4520SKenneth D. Merry 	return (0);
2510130f4520SKenneth D. Merry }
2511130f4520SKenneth D. Merry 
2512130f4520SKenneth D. Merry static int
2513130f4520SKenneth D. Merry ctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2514130f4520SKenneth D. Merry {
2515130f4520SKenneth D. Merry 	struct ctl_be_block_lun *lun;
2516130f4520SKenneth D. Merry 	int retval;
2517130f4520SKenneth D. Merry 
2518130f4520SKenneth D. Merry 	lun = (struct ctl_be_block_lun *)be_lun;
2519130f4520SKenneth D. Merry 	retval = 0;
2520130f4520SKenneth D. Merry 
2521130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "<num_threads>");
2522130f4520SKenneth D. Merry 
2523130f4520SKenneth D. Merry 	if (retval != 0)
2524130f4520SKenneth D. Merry 		goto bailout;
2525130f4520SKenneth D. Merry 
2526130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "%d", lun->num_threads);
2527130f4520SKenneth D. Merry 
2528130f4520SKenneth D. Merry 	if (retval != 0)
2529130f4520SKenneth D. Merry 		goto bailout;
2530130f4520SKenneth D. Merry 
2531130f4520SKenneth D. Merry 	retval = sbuf_printf(sb, "</num_threads>");
2532130f4520SKenneth D. Merry 
2533130f4520SKenneth D. Merry bailout:
2534130f4520SKenneth D. Merry 
2535130f4520SKenneth D. Merry 	return (retval);
2536130f4520SKenneth D. Merry }
2537130f4520SKenneth D. Merry 
2538130f4520SKenneth D. Merry int
2539130f4520SKenneth D. Merry ctl_be_block_init(void)
2540130f4520SKenneth D. Merry {
2541130f4520SKenneth D. Merry 	struct ctl_be_block_softc *softc;
2542130f4520SKenneth D. Merry 	int retval;
2543130f4520SKenneth D. Merry 
2544130f4520SKenneth D. Merry 	softc = &backend_block_softc;
2545130f4520SKenneth D. Merry 	retval = 0;
2546130f4520SKenneth D. Merry 
2547130f4520SKenneth D. Merry 	mtx_init(&softc->lock, "ctlblk", NULL, MTX_DEF);
2548a0e36aeeSEdward Tomasz Napierala 	beio_zone = uma_zcreate("beio", sizeof(struct ctl_be_block_io),
2549a0e36aeeSEdward Tomasz Napierala 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2550130f4520SKenneth D. Merry 	STAILQ_INIT(&softc->disk_list);
2551130f4520SKenneth D. Merry 	STAILQ_INIT(&softc->lun_list);
2552130f4520SKenneth D. Merry 
2553130f4520SKenneth D. Merry 	return (retval);
2554130f4520SKenneth D. Merry }
2555