ctl_backend_block.c revision 258871
1229997Sken/*-
2229997Sken * Copyright (c) 2003 Silicon Graphics International Corp.
3229997Sken * Copyright (c) 2009-2011 Spectra Logic Corporation
4232604Strasz * Copyright (c) 2012 The FreeBSD Foundation
5229997Sken * All rights reserved.
6229997Sken *
7232604Strasz * Portions of this software were developed by Edward Tomasz Napierala
8232604Strasz * under sponsorship from the FreeBSD Foundation.
9232604Strasz *
10229997Sken * Redistribution and use in source and binary forms, with or without
11229997Sken * modification, are permitted provided that the following conditions
12229997Sken * are met:
13229997Sken * 1. Redistributions of source code must retain the above copyright
14229997Sken *    notice, this list of conditions, and the following disclaimer,
15229997Sken *    without modification.
16229997Sken * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17229997Sken *    substantially similar to the "NO WARRANTY" disclaimer below
18229997Sken *    ("Disclaimer") and any redistribution must be conditioned upon
19229997Sken *    including a substantially similar Disclaimer requirement for further
20229997Sken *    binary redistribution.
21229997Sken *
22229997Sken * NO WARRANTY
23229997Sken * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24229997Sken * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25229997Sken * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26229997Sken * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27229997Sken * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31229997Sken * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32229997Sken * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33229997Sken * POSSIBILITY OF SUCH DAMAGES.
34229997Sken *
35229997Sken * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_backend_block.c#5 $
36229997Sken */
37229997Sken/*
38229997Sken * CAM Target Layer driver backend for block devices.
39229997Sken *
40229997Sken * Author: Ken Merry <ken@FreeBSD.org>
41229997Sken */
42229997Sken#include <sys/cdefs.h>
43229997Sken__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl_backend_block.c 258871 2013-12-03 18:04:14Z trasz $");
44229997Sken
45229997Sken#include <sys/param.h>
46229997Sken#include <sys/systm.h>
47229997Sken#include <sys/kernel.h>
48229997Sken#include <sys/types.h>
49229997Sken#include <sys/kthread.h>
50229997Sken#include <sys/bio.h>
51229997Sken#include <sys/fcntl.h>
52229997Sken#include <sys/lock.h>
53229997Sken#include <sys/mutex.h>
54229997Sken#include <sys/condvar.h>
55229997Sken#include <sys/malloc.h>
56229997Sken#include <sys/conf.h>
57229997Sken#include <sys/ioccom.h>
58229997Sken#include <sys/queue.h>
59229997Sken#include <sys/sbuf.h>
60229997Sken#include <sys/endian.h>
61229997Sken#include <sys/uio.h>
62229997Sken#include <sys/buf.h>
63229997Sken#include <sys/taskqueue.h>
64229997Sken#include <sys/vnode.h>
65229997Sken#include <sys/namei.h>
66229997Sken#include <sys/mount.h>
67229997Sken#include <sys/disk.h>
68229997Sken#include <sys/fcntl.h>
69229997Sken#include <sys/filedesc.h>
70229997Sken#include <sys/proc.h>
71229997Sken#include <sys/pcpu.h>
72229997Sken#include <sys/module.h>
73229997Sken#include <sys/sdt.h>
74229997Sken#include <sys/devicestat.h>
75229997Sken#include <sys/sysctl.h>
76229997Sken
77229997Sken#include <geom/geom.h>
78229997Sken
79229997Sken#include <cam/cam.h>
80229997Sken#include <cam/scsi/scsi_all.h>
81229997Sken#include <cam/scsi/scsi_da.h>
82229997Sken#include <cam/ctl/ctl_io.h>
83229997Sken#include <cam/ctl/ctl.h>
84229997Sken#include <cam/ctl/ctl_backend.h>
85229997Sken#include <cam/ctl/ctl_frontend_internal.h>
86229997Sken#include <cam/ctl/ctl_ioctl.h>
87229997Sken#include <cam/ctl/ctl_scsi_all.h>
88229997Sken#include <cam/ctl/ctl_error.h>
89229997Sken
90229997Sken/*
91229997Sken * The idea here is that we'll allocate enough S/G space to hold a 16MB
92229997Sken * I/O.  If we get an I/O larger than that, we'll reject it.
93229997Sken */
94229997Sken#define	CTLBLK_MAX_IO_SIZE	(16 * 1024 * 1024)
95229997Sken#define	CTLBLK_MAX_SEGS		(CTLBLK_MAX_IO_SIZE / MAXPHYS) + 1
96229997Sken
97229997Sken#ifdef CTLBLK_DEBUG
98229997Sken#define DPRINTF(fmt, args...) \
99229997Sken    printf("cbb(%s:%d): " fmt, __FUNCTION__, __LINE__, ##args)
100229997Sken#else
101229997Sken#define DPRINTF(fmt, args...) do {} while(0)
102229997Sken#endif
103229997Sken
104229997SkenSDT_PROVIDER_DEFINE(cbb);
105229997Sken
106229997Skentypedef enum {
107229997Sken	CTL_BE_BLOCK_LUN_UNCONFIGURED	= 0x01,
108229997Sken	CTL_BE_BLOCK_LUN_CONFIG_ERR	= 0x02,
109229997Sken	CTL_BE_BLOCK_LUN_WAITING	= 0x04,
110229997Sken	CTL_BE_BLOCK_LUN_MULTI_THREAD	= 0x08
111229997Sken} ctl_be_block_lun_flags;
112229997Sken
113229997Skentypedef enum {
114229997Sken	CTL_BE_BLOCK_NONE,
115229997Sken	CTL_BE_BLOCK_DEV,
116229997Sken	CTL_BE_BLOCK_FILE
117229997Sken} ctl_be_block_type;
118229997Sken
119229997Skenstruct ctl_be_block_devdata {
120229997Sken	struct cdev *cdev;
121229997Sken	struct cdevsw *csw;
122229997Sken	int dev_ref;
123229997Sken};
124229997Sken
125229997Skenstruct ctl_be_block_filedata {
126229997Sken	struct ucred *cred;
127229997Sken};
128229997Sken
129229997Skenunion ctl_be_block_bedata {
130229997Sken	struct ctl_be_block_devdata dev;
131229997Sken	struct ctl_be_block_filedata file;
132229997Sken};
133229997Sken
134229997Skenstruct ctl_be_block_io;
135229997Skenstruct ctl_be_block_lun;
136229997Sken
137229997Skentypedef void (*cbb_dispatch_t)(struct ctl_be_block_lun *be_lun,
138229997Sken			       struct ctl_be_block_io *beio);
139229997Sken
140229997Sken/*
141229997Sken * Backend LUN structure.  There is a 1:1 mapping between a block device
142229997Sken * and a backend block LUN, and between a backend block LUN and a CTL LUN.
143229997Sken */
144229997Skenstruct ctl_be_block_lun {
145229997Sken	struct ctl_block_disk *disk;
146229997Sken	char lunname[32];
147229997Sken	char *dev_path;
148229997Sken	ctl_be_block_type dev_type;
149229997Sken	struct vnode *vn;
150229997Sken	union ctl_be_block_bedata backend;
151229997Sken	cbb_dispatch_t dispatch;
152229997Sken	cbb_dispatch_t lun_flush;
153229997Sken	struct mtx lock;
154229997Sken	uma_zone_t lun_zone;
155229997Sken	uint64_t size_blocks;
156229997Sken	uint64_t size_bytes;
157229997Sken	uint32_t blocksize;
158229997Sken	int blocksize_shift;
159229997Sken	struct ctl_be_block_softc *softc;
160229997Sken	struct devstat *disk_stats;
161229997Sken	ctl_be_block_lun_flags flags;
162229997Sken	STAILQ_ENTRY(ctl_be_block_lun) links;
163229997Sken	struct ctl_be_lun ctl_be_lun;
164229997Sken	struct taskqueue *io_taskqueue;
165229997Sken	struct task io_task;
166229997Sken	int num_threads;
167229997Sken	STAILQ_HEAD(, ctl_io_hdr) input_queue;
168229997Sken	STAILQ_HEAD(, ctl_io_hdr) config_write_queue;
169229997Sken	STAILQ_HEAD(, ctl_io_hdr) datamove_queue;
170229997Sken};
171229997Sken
172229997Sken/*
173229997Sken * Overall softc structure for the block backend module.
174229997Sken */
175229997Skenstruct ctl_be_block_softc {
176229997Sken	STAILQ_HEAD(, ctl_be_block_io)   beio_free_queue;
177229997Sken	struct mtx			 lock;
178229997Sken	int				 prealloc_beio;
179229997Sken	int				 num_disks;
180229997Sken	STAILQ_HEAD(, ctl_block_disk)	 disk_list;
181229997Sken	int				 num_luns;
182229997Sken	STAILQ_HEAD(, ctl_be_block_lun)	 lun_list;
183229997Sken};
184229997Sken
185229997Skenstatic struct ctl_be_block_softc backend_block_softc;
186229997Sken
187229997Sken/*
188229997Sken * Per-I/O information.
189229997Sken */
190229997Skenstruct ctl_be_block_io {
191229997Sken	union ctl_io			*io;
192229997Sken	struct ctl_sg_entry		sg_segs[CTLBLK_MAX_SEGS];
193229997Sken	struct iovec			xiovecs[CTLBLK_MAX_SEGS];
194229997Sken	int				bio_cmd;
195229997Sken	int				bio_flags;
196229997Sken	int				num_segs;
197229997Sken	int				num_bios_sent;
198229997Sken	int				num_bios_done;
199229997Sken	int				send_complete;
200229997Sken	int				num_errors;
201229997Sken	struct bintime			ds_t0;
202229997Sken	devstat_tag_type		ds_tag_type;
203229997Sken	devstat_trans_flags		ds_trans_type;
204229997Sken	uint64_t			io_len;
205229997Sken	uint64_t			io_offset;
206229997Sken	struct ctl_be_block_softc	*softc;
207229997Sken	struct ctl_be_block_lun		*lun;
208229997Sken	STAILQ_ENTRY(ctl_be_block_io)	links;
209229997Sken};
210229997Sken
211229997Skenstatic int cbb_num_threads = 14;
212229997SkenTUNABLE_INT("kern.cam.ctl.block.num_threads", &cbb_num_threads);
213229997SkenSYSCTL_NODE(_kern_cam_ctl, OID_AUTO, block, CTLFLAG_RD, 0,
214229997Sken	    "CAM Target Layer Block Backend");
215229997SkenSYSCTL_INT(_kern_cam_ctl_block, OID_AUTO, num_threads, CTLFLAG_RW,
216229997Sken           &cbb_num_threads, 0, "Number of threads per backing file");
217229997Sken
218229997Skenstatic struct ctl_be_block_io *ctl_alloc_beio(struct ctl_be_block_softc *softc);
219229997Skenstatic void ctl_free_beio(struct ctl_be_block_io *beio);
220229997Skenstatic int ctl_grow_beio(struct ctl_be_block_softc *softc, int count);
221229997Sken#if 0
222229997Skenstatic void ctl_shrink_beio(struct ctl_be_block_softc *softc);
223229997Sken#endif
224229997Skenstatic void ctl_complete_beio(struct ctl_be_block_io *beio);
225229997Skenstatic int ctl_be_block_move_done(union ctl_io *io);
226229997Skenstatic void ctl_be_block_biodone(struct bio *bio);
227229997Skenstatic void ctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
228229997Sken				    struct ctl_be_block_io *beio);
229229997Skenstatic void ctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
230229997Sken				       struct ctl_be_block_io *beio);
231229997Skenstatic void ctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
232229997Sken				   struct ctl_be_block_io *beio);
233229997Skenstatic void ctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
234229997Sken				      struct ctl_be_block_io *beio);
235229997Skenstatic void ctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
236229997Sken				    union ctl_io *io);
237229997Skenstatic void ctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
238229997Sken				  union ctl_io *io);
239229997Skenstatic void ctl_be_block_worker(void *context, int pending);
240229997Skenstatic int ctl_be_block_submit(union ctl_io *io);
241229997Skenstatic int ctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
242229997Sken				   int flag, struct thread *td);
243229997Skenstatic int ctl_be_block_open_file(struct ctl_be_block_lun *be_lun,
244229997Sken				  struct ctl_lun_req *req);
245229997Skenstatic int ctl_be_block_open_dev(struct ctl_be_block_lun *be_lun,
246229997Sken				 struct ctl_lun_req *req);
247229997Skenstatic int ctl_be_block_close(struct ctl_be_block_lun *be_lun);
248229997Skenstatic int ctl_be_block_open(struct ctl_be_block_softc *softc,
249229997Sken			     struct ctl_be_block_lun *be_lun,
250229997Sken			     struct ctl_lun_req *req);
251229997Skenstatic int ctl_be_block_create(struct ctl_be_block_softc *softc,
252229997Sken			       struct ctl_lun_req *req);
253229997Skenstatic int ctl_be_block_rm(struct ctl_be_block_softc *softc,
254229997Sken			   struct ctl_lun_req *req);
255232604Straszstatic int ctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
256232604Strasz				  struct ctl_lun_req *req);
257232604Straszstatic int ctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
258232604Strasz				 struct ctl_lun_req *req);
259232604Straszstatic int ctl_be_block_modify(struct ctl_be_block_softc *softc,
260232604Strasz			   struct ctl_lun_req *req);
261229997Skenstatic void ctl_be_block_lun_shutdown(void *be_lun);
262229997Skenstatic void ctl_be_block_lun_config_status(void *be_lun,
263229997Sken					   ctl_lun_config_status status);
264229997Skenstatic int ctl_be_block_config_write(union ctl_io *io);
265229997Skenstatic int ctl_be_block_config_read(union ctl_io *io);
266229997Skenstatic int ctl_be_block_lun_info(void *be_lun, struct sbuf *sb);
267229997Skenint ctl_be_block_init(void);
268229997Sken
269229997Skenstatic struct ctl_backend_driver ctl_be_block_driver =
270229997Sken{
271230334Sken	.name = "block",
272230334Sken	.flags = CTL_BE_FLAG_HAS_CONFIG,
273230334Sken	.init = ctl_be_block_init,
274230334Sken	.data_submit = ctl_be_block_submit,
275230334Sken	.data_move_done = ctl_be_block_move_done,
276230334Sken	.config_read = ctl_be_block_config_read,
277230334Sken	.config_write = ctl_be_block_config_write,
278230334Sken	.ioctl = ctl_be_block_ioctl,
279230334Sken	.lun_info = ctl_be_block_lun_info
280229997Sken};
281229997Sken
282229997SkenMALLOC_DEFINE(M_CTLBLK, "ctlblk", "Memory used for CTL block backend");
283229997SkenCTL_BACKEND_DECLARE(cbb, ctl_be_block_driver);
284229997Sken
285229997Skenstatic struct ctl_be_block_io *
286229997Skenctl_alloc_beio(struct ctl_be_block_softc *softc)
287229997Sken{
288229997Sken	struct ctl_be_block_io *beio;
289229997Sken	int count;
290229997Sken
291229997Sken	mtx_lock(&softc->lock);
292229997Sken
293229997Sken	beio = STAILQ_FIRST(&softc->beio_free_queue);
294229997Sken	if (beio != NULL) {
295229997Sken		STAILQ_REMOVE(&softc->beio_free_queue, beio,
296229997Sken			      ctl_be_block_io, links);
297229997Sken	}
298229997Sken	mtx_unlock(&softc->lock);
299229997Sken
300229997Sken	if (beio != NULL) {
301229997Sken		bzero(beio, sizeof(*beio));
302229997Sken		beio->softc = softc;
303229997Sken		return (beio);
304229997Sken	}
305229997Sken
306229997Sken	for (;;) {
307229997Sken
308229997Sken		count = ctl_grow_beio(softc, /*count*/ 10);
309229997Sken
310229997Sken		/*
311229997Sken		 * This shouldn't be possible, since ctl_grow_beio() uses a
312229997Sken		 * blocking malloc.
313229997Sken		 */
314229997Sken		if (count == 0)
315229997Sken			return (NULL);
316229997Sken
317229997Sken		/*
318229997Sken		 * Since we have to drop the lock when we're allocating beio
319229997Sken		 * structures, it's possible someone else can come along and
320229997Sken		 * allocate the beio's we've just allocated.
321229997Sken		 */
322229997Sken		mtx_lock(&softc->lock);
323229997Sken		beio = STAILQ_FIRST(&softc->beio_free_queue);
324229997Sken		if (beio != NULL) {
325229997Sken			STAILQ_REMOVE(&softc->beio_free_queue, beio,
326229997Sken				      ctl_be_block_io, links);
327229997Sken		}
328229997Sken		mtx_unlock(&softc->lock);
329229997Sken
330229997Sken		if (beio != NULL) {
331229997Sken			bzero(beio, sizeof(*beio));
332229997Sken			beio->softc = softc;
333229997Sken			break;
334229997Sken		}
335229997Sken	}
336229997Sken	return (beio);
337229997Sken}
338229997Sken
339229997Skenstatic void
340229997Skenctl_free_beio(struct ctl_be_block_io *beio)
341229997Sken{
342229997Sken	struct ctl_be_block_softc *softc;
343229997Sken	int duplicate_free;
344229997Sken	int i;
345229997Sken
346229997Sken	softc = beio->softc;
347229997Sken	duplicate_free = 0;
348229997Sken
349229997Sken	for (i = 0; i < beio->num_segs; i++) {
350229997Sken		if (beio->sg_segs[i].addr == NULL)
351229997Sken			duplicate_free++;
352229997Sken
353229997Sken		uma_zfree(beio->lun->lun_zone, beio->sg_segs[i].addr);
354229997Sken		beio->sg_segs[i].addr = NULL;
355229997Sken	}
356229997Sken
357229997Sken	if (duplicate_free > 0) {
358229997Sken		printf("%s: %d duplicate frees out of %d segments\n", __func__,
359229997Sken		       duplicate_free, beio->num_segs);
360229997Sken	}
361229997Sken	mtx_lock(&softc->lock);
362229997Sken	STAILQ_INSERT_TAIL(&softc->beio_free_queue, beio, links);
363229997Sken	mtx_unlock(&softc->lock);
364229997Sken}
365229997Sken
366229997Skenstatic int
367229997Skenctl_grow_beio(struct ctl_be_block_softc *softc, int count)
368229997Sken{
369229997Sken	int i;
370229997Sken
371229997Sken	for (i = 0; i < count; i++) {
372229997Sken		struct ctl_be_block_io *beio;
373229997Sken
374229997Sken		beio = (struct ctl_be_block_io *)malloc(sizeof(*beio),
375229997Sken							   M_CTLBLK,
376229997Sken							   M_WAITOK | M_ZERO);
377229997Sken		beio->softc = softc;
378229997Sken		mtx_lock(&softc->lock);
379229997Sken		STAILQ_INSERT_TAIL(&softc->beio_free_queue, beio, links);
380229997Sken		mtx_unlock(&softc->lock);
381229997Sken	}
382229997Sken
383229997Sken	return (i);
384229997Sken}
385229997Sken
386229997Sken#if 0
387229997Skenstatic void
388229997Skenctl_shrink_beio(struct ctl_be_block_softc *softc)
389229997Sken{
390229997Sken	struct ctl_be_block_io *beio, *beio_tmp;
391229997Sken
392229997Sken	mtx_lock(&softc->lock);
393229997Sken	STAILQ_FOREACH_SAFE(beio, &softc->beio_free_queue, links, beio_tmp) {
394229997Sken		STAILQ_REMOVE(&softc->beio_free_queue, beio,
395229997Sken			      ctl_be_block_io, links);
396229997Sken		free(beio, M_CTLBLK);
397229997Sken	}
398229997Sken	mtx_unlock(&softc->lock);
399229997Sken}
400229997Sken#endif
401229997Sken
402229997Skenstatic void
403229997Skenctl_complete_beio(struct ctl_be_block_io *beio)
404229997Sken{
405229997Sken	union ctl_io *io;
406229997Sken	int io_len;
407229997Sken
408229997Sken	io = beio->io;
409229997Sken
410229997Sken	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
411229997Sken		io_len = beio->io_len;
412229997Sken	else
413229997Sken		io_len = 0;
414229997Sken
415229997Sken	devstat_end_transaction(beio->lun->disk_stats,
416229997Sken				/*bytes*/ io_len,
417229997Sken				beio->ds_tag_type,
418229997Sken				beio->ds_trans_type,
419229997Sken				/*now*/ NULL,
420229997Sken				/*then*/&beio->ds_t0);
421229997Sken
422229997Sken	ctl_free_beio(beio);
423229997Sken	ctl_done(io);
424229997Sken}
425229997Sken
426229997Skenstatic int
427229997Skenctl_be_block_move_done(union ctl_io *io)
428229997Sken{
429229997Sken	struct ctl_be_block_io *beio;
430229997Sken	struct ctl_be_block_lun *be_lun;
431229997Sken#ifdef CTL_TIME_IO
432229997Sken	struct bintime cur_bt;
433229997Sken#endif
434229997Sken
435229997Sken	beio = (struct ctl_be_block_io *)
436229997Sken		io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr;
437229997Sken
438229997Sken	be_lun = beio->lun;
439229997Sken
440229997Sken	DPRINTF("entered\n");
441229997Sken
442229997Sken#ifdef CTL_TIME_IO
443229997Sken	getbintime(&cur_bt);
444229997Sken	bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
445229997Sken	bintime_add(&io->io_hdr.dma_bt, &cur_bt);
446229997Sken	io->io_hdr.num_dmas++;
447229997Sken#endif
448229997Sken
449229997Sken	/*
450229997Sken	 * We set status at this point for read commands, and write
451229997Sken	 * commands with errors.
452229997Sken	 */
453229997Sken	if ((beio->bio_cmd == BIO_READ)
454229997Sken	 && (io->io_hdr.port_status == 0)
455229997Sken	 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
456229997Sken	 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
457229997Sken		ctl_set_success(&io->scsiio);
458229997Sken	else if ((io->io_hdr.port_status != 0)
459229997Sken	      && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
460229997Sken	      && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) {
461229997Sken		/*
462229997Sken		 * For hardware error sense keys, the sense key
463229997Sken		 * specific value is defined to be a retry count,
464229997Sken		 * but we use it to pass back an internal FETD
465229997Sken		 * error code.  XXX KDM  Hopefully the FETD is only
466229997Sken		 * using 16 bits for an error code, since that's
467229997Sken		 * all the space we have in the sks field.
468229997Sken		 */
469229997Sken		ctl_set_internal_failure(&io->scsiio,
470229997Sken					 /*sks_valid*/ 1,
471229997Sken					 /*retry_count*/
472229997Sken					 io->io_hdr.port_status);
473229997Sken	}
474229997Sken
475229997Sken	/*
476229997Sken	 * If this is a read, or a write with errors, it is done.
477229997Sken	 */
478229997Sken	if ((beio->bio_cmd == BIO_READ)
479229997Sken	 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)
480229997Sken	 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) {
481229997Sken		ctl_complete_beio(beio);
482229997Sken		return (0);
483229997Sken	}
484229997Sken
485229997Sken	/*
486229997Sken	 * At this point, we have a write and the DMA completed
487229997Sken	 * successfully.  We now have to queue it to the task queue to
488229997Sken	 * execute the backend I/O.  That is because we do blocking
489229997Sken	 * memory allocations, and in the file backing case, blocking I/O.
490229997Sken	 * This move done routine is generally called in the SIM's
491229997Sken	 * interrupt context, and therefore we cannot block.
492229997Sken	 */
493229997Sken	mtx_lock(&be_lun->lock);
494229997Sken	/*
495229997Sken	 * XXX KDM make sure that links is okay to use at this point.
496229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
497229997Sken	 * or deal with resource allocation here.
498229997Sken	 */
499229997Sken	STAILQ_INSERT_TAIL(&be_lun->datamove_queue, &io->io_hdr, links);
500229997Sken	mtx_unlock(&be_lun->lock);
501229997Sken
502229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
503229997Sken
504229997Sken	return (0);
505229997Sken}
506229997Sken
507229997Skenstatic void
508229997Skenctl_be_block_biodone(struct bio *bio)
509229997Sken{
510229997Sken	struct ctl_be_block_io *beio;
511229997Sken	struct ctl_be_block_lun *be_lun;
512229997Sken	union ctl_io *io;
513229997Sken
514229997Sken	beio = bio->bio_caller1;
515229997Sken	be_lun = beio->lun;
516229997Sken	io = beio->io;
517229997Sken
518229997Sken	DPRINTF("entered\n");
519229997Sken
520229997Sken	mtx_lock(&be_lun->lock);
521229997Sken	if (bio->bio_error != 0)
522229997Sken		beio->num_errors++;
523229997Sken
524229997Sken	beio->num_bios_done++;
525229997Sken
526229997Sken	/*
527229997Sken	 * XXX KDM will this cause WITNESS to complain?  Holding a lock
528229997Sken	 * during the free might cause it to complain.
529229997Sken	 */
530229997Sken	g_destroy_bio(bio);
531229997Sken
532229997Sken	/*
533229997Sken	 * If the send complete bit isn't set, or we aren't the last I/O to
534229997Sken	 * complete, then we're done.
535229997Sken	 */
536229997Sken	if ((beio->send_complete == 0)
537229997Sken	 || (beio->num_bios_done < beio->num_bios_sent)) {
538229997Sken		mtx_unlock(&be_lun->lock);
539229997Sken		return;
540229997Sken	}
541229997Sken
542229997Sken	/*
543229997Sken	 * At this point, we've verified that we are the last I/O to
544229997Sken	 * complete, so it's safe to drop the lock.
545229997Sken	 */
546229997Sken	mtx_unlock(&be_lun->lock);
547229997Sken
548229997Sken	/*
549229997Sken	 * If there are any errors from the backing device, we fail the
550229997Sken	 * entire I/O with a medium error.
551229997Sken	 */
552229997Sken	if (beio->num_errors > 0) {
553229997Sken		if (beio->bio_cmd == BIO_FLUSH) {
554229997Sken			/* XXX KDM is there is a better error here? */
555229997Sken			ctl_set_internal_failure(&io->scsiio,
556229997Sken						 /*sks_valid*/ 1,
557229997Sken						 /*retry_count*/ 0xbad2);
558229997Sken		} else
559229997Sken			ctl_set_medium_error(&io->scsiio);
560229997Sken		ctl_complete_beio(beio);
561229997Sken		return;
562229997Sken	}
563229997Sken
564229997Sken	/*
565229997Sken	 * If this is a write or a flush, we're all done.
566229997Sken	 * If this is a read, we can now send the data to the user.
567229997Sken	 */
568229997Sken	if ((beio->bio_cmd == BIO_WRITE)
569229997Sken	 || (beio->bio_cmd == BIO_FLUSH)) {
570229997Sken		ctl_set_success(&io->scsiio);
571229997Sken		ctl_complete_beio(beio);
572229997Sken	} else {
573229997Sken		io->scsiio.be_move_done = ctl_be_block_move_done;
574229997Sken		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
575229997Sken		io->scsiio.kern_data_len = beio->io_len;
576229997Sken		io->scsiio.kern_total_len = beio->io_len;
577229997Sken		io->scsiio.kern_rel_offset = 0;
578229997Sken		io->scsiio.kern_data_resid = 0;
579229997Sken		io->scsiio.kern_sg_entries = beio->num_segs;
580229997Sken		io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
581229997Sken#ifdef CTL_TIME_IO
582229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
583229997Sken#endif
584229997Sken		ctl_datamove(io);
585229997Sken	}
586229997Sken}
587229997Sken
588229997Skenstatic void
589229997Skenctl_be_block_flush_file(struct ctl_be_block_lun *be_lun,
590229997Sken			struct ctl_be_block_io *beio)
591229997Sken{
592229997Sken	union ctl_io *io;
593229997Sken	struct mount *mountpoint;
594241896Skib	int error, lock_flags;
595229997Sken
596229997Sken	DPRINTF("entered\n");
597229997Sken
598229997Sken	io = beio->io;
599229997Sken
600229997Sken       	(void) vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
601229997Sken
602229997Sken	if (MNT_SHARED_WRITES(mountpoint)
603229997Sken	 || ((mountpoint == NULL)
604229997Sken	  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
605229997Sken		lock_flags = LK_SHARED;
606229997Sken	else
607229997Sken		lock_flags = LK_EXCLUSIVE;
608229997Sken
609229997Sken	vn_lock(be_lun->vn, lock_flags | LK_RETRY);
610229997Sken
611229997Sken	binuptime(&beio->ds_t0);
612229997Sken	devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
613229997Sken
614229997Sken	error = VOP_FSYNC(be_lun->vn, MNT_WAIT, curthread);
615229997Sken	VOP_UNLOCK(be_lun->vn, 0);
616229997Sken
617229997Sken	vn_finished_write(mountpoint);
618229997Sken
619229997Sken	if (error == 0)
620229997Sken		ctl_set_success(&io->scsiio);
621229997Sken	else {
622229997Sken		/* XXX KDM is there is a better error here? */
623229997Sken		ctl_set_internal_failure(&io->scsiio,
624229997Sken					 /*sks_valid*/ 1,
625229997Sken					 /*retry_count*/ 0xbad1);
626229997Sken	}
627229997Sken
628229997Sken	ctl_complete_beio(beio);
629229997Sken}
630229997Sken
631258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_start, "uint64_t");
632258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_start, "uint64_t");
633258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, file_done,"uint64_t");
634258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, file_done, "uint64_t");
635229997Sken
636229997Skenstatic void
637229997Skenctl_be_block_dispatch_file(struct ctl_be_block_lun *be_lun,
638229997Sken			   struct ctl_be_block_io *beio)
639229997Sken{
640229997Sken	struct ctl_be_block_filedata *file_data;
641229997Sken	union ctl_io *io;
642229997Sken	struct uio xuio;
643229997Sken	struct iovec *xiovec;
644241896Skib	int flags;
645229997Sken	int error, i;
646229997Sken
647229997Sken	DPRINTF("entered\n");
648229997Sken
649229997Sken	file_data = &be_lun->backend.file;
650229997Sken	io = beio->io;
651229997Sken	flags = beio->bio_flags;
652229997Sken
653229997Sken	if (beio->bio_cmd == BIO_READ) {
654229997Sken		SDT_PROBE(cbb, kernel, read, file_start, 0, 0, 0, 0, 0);
655229997Sken	} else {
656229997Sken		SDT_PROBE(cbb, kernel, write, file_start, 0, 0, 0, 0, 0);
657229997Sken	}
658229997Sken
659229997Sken	bzero(&xuio, sizeof(xuio));
660229997Sken	if (beio->bio_cmd == BIO_READ)
661229997Sken		xuio.uio_rw = UIO_READ;
662229997Sken	else
663229997Sken		xuio.uio_rw = UIO_WRITE;
664229997Sken
665229997Sken	xuio.uio_offset = beio->io_offset;
666229997Sken	xuio.uio_resid = beio->io_len;
667229997Sken	xuio.uio_segflg = UIO_SYSSPACE;
668229997Sken	xuio.uio_iov = beio->xiovecs;
669229997Sken	xuio.uio_iovcnt = beio->num_segs;
670229997Sken	xuio.uio_td = curthread;
671229997Sken
672229997Sken	for (i = 0, xiovec = xuio.uio_iov; i < xuio.uio_iovcnt; i++, xiovec++) {
673229997Sken		xiovec->iov_base = beio->sg_segs[i].addr;
674229997Sken		xiovec->iov_len = beio->sg_segs[i].len;
675229997Sken	}
676229997Sken
677229997Sken	if (beio->bio_cmd == BIO_READ) {
678229997Sken		vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
679229997Sken
680229997Sken		binuptime(&beio->ds_t0);
681229997Sken		devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
682229997Sken
683229997Sken		/*
684229997Sken		 * UFS pays attention to IO_DIRECT for reads.  If the
685229997Sken		 * DIRECTIO option is configured into the kernel, it calls
686229997Sken		 * ffs_rawread().  But that only works for single-segment
687229997Sken		 * uios with user space addresses.  In our case, with a
688229997Sken		 * kernel uio, it still reads into the buffer cache, but it
689229997Sken		 * will just try to release the buffer from the cache later
690229997Sken		 * on in ffs_read().
691229997Sken		 *
692229997Sken		 * ZFS does not pay attention to IO_DIRECT for reads.
693229997Sken		 *
694229997Sken		 * UFS does not pay attention to IO_SYNC for reads.
695229997Sken		 *
696229997Sken		 * ZFS pays attention to IO_SYNC (which translates into the
697229997Sken		 * Solaris define FRSYNC for zfs_read()) for reads.  It
698229997Sken		 * attempts to sync the file before reading.
699229997Sken		 *
700229997Sken		 * So, to attempt to provide some barrier semantics in the
701229997Sken		 * BIO_ORDERED case, set both IO_DIRECT and IO_SYNC.
702229997Sken		 */
703229997Sken		error = VOP_READ(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
704229997Sken				 (IO_DIRECT|IO_SYNC) : 0, file_data->cred);
705229997Sken
706229997Sken		VOP_UNLOCK(be_lun->vn, 0);
707229997Sken	} else {
708229997Sken		struct mount *mountpoint;
709229997Sken		int lock_flags;
710229997Sken
711229997Sken		(void)vn_start_write(be_lun->vn, &mountpoint, V_WAIT);
712229997Sken
713229997Sken		if (MNT_SHARED_WRITES(mountpoint)
714229997Sken		 || ((mountpoint == NULL)
715229997Sken		  && MNT_SHARED_WRITES(be_lun->vn->v_mount)))
716229997Sken			lock_flags = LK_SHARED;
717229997Sken		else
718229997Sken			lock_flags = LK_EXCLUSIVE;
719229997Sken
720229997Sken		vn_lock(be_lun->vn, lock_flags | LK_RETRY);
721229997Sken
722229997Sken		binuptime(&beio->ds_t0);
723229997Sken		devstat_start_transaction(beio->lun->disk_stats, &beio->ds_t0);
724229997Sken
725229997Sken		/*
726229997Sken		 * UFS pays attention to IO_DIRECT for writes.  The write
727229997Sken		 * is done asynchronously.  (Normally the write would just
728229997Sken		 * get put into cache.
729229997Sken		 *
730229997Sken		 * UFS pays attention to IO_SYNC for writes.  It will
731229997Sken		 * attempt to write the buffer out synchronously if that
732229997Sken		 * flag is set.
733229997Sken		 *
734229997Sken		 * ZFS does not pay attention to IO_DIRECT for writes.
735229997Sken		 *
736229997Sken		 * ZFS pays attention to IO_SYNC (a.k.a. FSYNC or FRSYNC)
737229997Sken		 * for writes.  It will flush the transaction from the
738229997Sken		 * cache before returning.
739229997Sken		 *
740229997Sken		 * So if we've got the BIO_ORDERED flag set, we want
741229997Sken		 * IO_SYNC in either the UFS or ZFS case.
742229997Sken		 */
743229997Sken		error = VOP_WRITE(be_lun->vn, &xuio, (flags & BIO_ORDERED) ?
744229997Sken				  IO_SYNC : 0, file_data->cred);
745229997Sken		VOP_UNLOCK(be_lun->vn, 0);
746229997Sken
747229997Sken		vn_finished_write(mountpoint);
748229997Sken        }
749229997Sken
750229997Sken	/*
751229997Sken	 * If we got an error, set the sense data to "MEDIUM ERROR" and
752229997Sken	 * return the I/O to the user.
753229997Sken	 */
754229997Sken	if (error != 0) {
755229997Sken		char path_str[32];
756229997Sken
757229997Sken		ctl_scsi_path_string(io, path_str, sizeof(path_str));
758229997Sken		/*
759229997Sken		 * XXX KDM ZFS returns ENOSPC when the underlying
760229997Sken		 * filesystem fills up.  What kind of SCSI error should we
761229997Sken		 * return for that?
762229997Sken		 */
763229997Sken		printf("%s%s command returned errno %d\n", path_str,
764229997Sken		       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE", error);
765229997Sken		ctl_set_medium_error(&io->scsiio);
766229997Sken		ctl_complete_beio(beio);
767229997Sken		return;
768229997Sken	}
769229997Sken
770229997Sken	/*
771229997Sken	 * If this is a write, we're all done.
772229997Sken	 * If this is a read, we can now send the data to the user.
773229997Sken	 */
774229997Sken	if (beio->bio_cmd == BIO_WRITE) {
775229997Sken		ctl_set_success(&io->scsiio);
776229997Sken		SDT_PROBE(cbb, kernel, write, file_done, 0, 0, 0, 0, 0);
777229997Sken		ctl_complete_beio(beio);
778229997Sken	} else {
779229997Sken		SDT_PROBE(cbb, kernel, read, file_done, 0, 0, 0, 0, 0);
780229997Sken		io->scsiio.be_move_done = ctl_be_block_move_done;
781229997Sken		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
782229997Sken		io->scsiio.kern_data_len = beio->io_len;
783229997Sken		io->scsiio.kern_total_len = beio->io_len;
784229997Sken		io->scsiio.kern_rel_offset = 0;
785229997Sken		io->scsiio.kern_data_resid = 0;
786229997Sken		io->scsiio.kern_sg_entries = beio->num_segs;
787229997Sken		io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
788229997Sken#ifdef CTL_TIME_IO
789229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
790229997Sken#endif
791229997Sken		ctl_datamove(io);
792229997Sken	}
793229997Sken}
794229997Sken
795229997Skenstatic void
796229997Skenctl_be_block_flush_dev(struct ctl_be_block_lun *be_lun,
797229997Sken		       struct ctl_be_block_io *beio)
798229997Sken{
799229997Sken	struct bio *bio;
800229997Sken	union ctl_io *io;
801229997Sken	struct ctl_be_block_devdata *dev_data;
802229997Sken
803229997Sken	dev_data = &be_lun->backend.dev;
804229997Sken	io = beio->io;
805229997Sken
806229997Sken	DPRINTF("entered\n");
807229997Sken
808229997Sken	/* This can't fail, it's a blocking allocation. */
809229997Sken	bio = g_alloc_bio();
810229997Sken
811229997Sken	bio->bio_cmd	    = BIO_FLUSH;
812229997Sken	bio->bio_flags	   |= BIO_ORDERED;
813229997Sken	bio->bio_dev	    = dev_data->cdev;
814229997Sken	bio->bio_offset	    = 0;
815229997Sken	bio->bio_data	    = 0;
816229997Sken	bio->bio_done	    = ctl_be_block_biodone;
817229997Sken	bio->bio_caller1    = beio;
818229997Sken	bio->bio_pblkno	    = 0;
819229997Sken
820229997Sken	/*
821229997Sken	 * We don't need to acquire the LUN lock here, because we are only
822229997Sken	 * sending one bio, and so there is no other context to synchronize
823229997Sken	 * with.
824229997Sken	 */
825229997Sken	beio->num_bios_sent = 1;
826229997Sken	beio->send_complete = 1;
827229997Sken
828229997Sken	binuptime(&beio->ds_t0);
829229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
830229997Sken
831229997Sken	(*dev_data->csw->d_strategy)(bio);
832229997Sken}
833229997Sken
834229997Skenstatic void
835229997Skenctl_be_block_dispatch_dev(struct ctl_be_block_lun *be_lun,
836229997Sken			  struct ctl_be_block_io *beio)
837229997Sken{
838229997Sken	int i;
839229997Sken	struct bio *bio;
840229997Sken	struct ctl_be_block_devdata *dev_data;
841229997Sken	off_t cur_offset;
842229997Sken	int max_iosize;
843229997Sken
844229997Sken	DPRINTF("entered\n");
845229997Sken
846229997Sken	dev_data = &be_lun->backend.dev;
847229997Sken
848229997Sken	/*
849229997Sken	 * We have to limit our I/O size to the maximum supported by the
850229997Sken	 * backend device.  Hopefully it is MAXPHYS.  If the driver doesn't
851229997Sken	 * set it properly, use DFLTPHYS.
852229997Sken	 */
853229997Sken	max_iosize = dev_data->cdev->si_iosize_max;
854229997Sken	if (max_iosize < PAGE_SIZE)
855229997Sken		max_iosize = DFLTPHYS;
856229997Sken
857229997Sken	cur_offset = beio->io_offset;
858229997Sken
859229997Sken	/*
860229997Sken	 * XXX KDM need to accurately reflect the number of I/Os outstanding
861229997Sken	 * to a device.
862229997Sken	 */
863229997Sken	binuptime(&beio->ds_t0);
864229997Sken	devstat_start_transaction(be_lun->disk_stats, &beio->ds_t0);
865229997Sken
866229997Sken	for (i = 0; i < beio->num_segs; i++) {
867229997Sken		size_t cur_size;
868229997Sken		uint8_t *cur_ptr;
869229997Sken
870229997Sken		cur_size = beio->sg_segs[i].len;
871229997Sken		cur_ptr = beio->sg_segs[i].addr;
872229997Sken
873229997Sken		while (cur_size > 0) {
874229997Sken			/* This can't fail, it's a blocking allocation. */
875229997Sken			bio = g_alloc_bio();
876229997Sken
877229997Sken			KASSERT(bio != NULL, ("g_alloc_bio() failed!\n"));
878229997Sken
879229997Sken			bio->bio_cmd = beio->bio_cmd;
880229997Sken			bio->bio_flags |= beio->bio_flags;
881229997Sken			bio->bio_dev = dev_data->cdev;
882229997Sken			bio->bio_caller1 = beio;
883229997Sken			bio->bio_length = min(cur_size, max_iosize);
884229997Sken			bio->bio_offset = cur_offset;
885229997Sken			bio->bio_data = cur_ptr;
886229997Sken			bio->bio_done = ctl_be_block_biodone;
887229997Sken			bio->bio_pblkno = cur_offset / be_lun->blocksize;
888229997Sken
889229997Sken			cur_offset += bio->bio_length;
890229997Sken			cur_ptr += bio->bio_length;
891229997Sken			cur_size -= bio->bio_length;
892229997Sken
893229997Sken			/*
894229997Sken			 * Make sure we set the complete bit just before we
895229997Sken			 * issue the last bio so we don't wind up with a
896229997Sken			 * race.
897229997Sken			 *
898229997Sken			 * Use the LUN mutex here instead of a combination
899229997Sken			 * of atomic variables for simplicity.
900229997Sken			 *
901229997Sken			 * XXX KDM we could have a per-IO lock, but that
902229997Sken			 * would cause additional per-IO setup and teardown
903229997Sken			 * overhead.  Hopefully there won't be too much
904229997Sken			 * contention on the LUN lock.
905229997Sken			 */
906229997Sken			mtx_lock(&be_lun->lock);
907229997Sken
908229997Sken			beio->num_bios_sent++;
909229997Sken
910229997Sken			if ((i == beio->num_segs - 1)
911229997Sken			 && (cur_size == 0))
912229997Sken				beio->send_complete = 1;
913229997Sken
914229997Sken			mtx_unlock(&be_lun->lock);
915229997Sken
916229997Sken			(*dev_data->csw->d_strategy)(bio);
917229997Sken		}
918229997Sken	}
919229997Sken}
920229997Sken
921229997Skenstatic void
922229997Skenctl_be_block_cw_dispatch(struct ctl_be_block_lun *be_lun,
923229997Sken			 union ctl_io *io)
924229997Sken{
925229997Sken	struct ctl_be_block_io *beio;
926229997Sken	struct ctl_be_block_softc *softc;
927229997Sken
928229997Sken	DPRINTF("entered\n");
929229997Sken
930229997Sken	softc = be_lun->softc;
931229997Sken	beio = ctl_alloc_beio(softc);
932229997Sken	if (beio == NULL) {
933229997Sken		/*
934229997Sken		 * This should not happen.  ctl_alloc_beio() will call
935229997Sken		 * ctl_grow_beio() with a blocking malloc as needed.
936229997Sken		 * A malloc with M_WAITOK should not fail.
937229997Sken		 */
938229997Sken		ctl_set_busy(&io->scsiio);
939229997Sken		ctl_done(io);
940229997Sken		return;
941229997Sken	}
942229997Sken
943229997Sken	beio->io = io;
944229997Sken	beio->softc = softc;
945229997Sken	beio->lun = be_lun;
946229997Sken	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr = beio;
947229997Sken
948229997Sken	switch (io->scsiio.cdb[0]) {
949229997Sken	case SYNCHRONIZE_CACHE:
950229997Sken	case SYNCHRONIZE_CACHE_16:
951249194Strasz		beio->bio_cmd = BIO_FLUSH;
952229997Sken		beio->ds_trans_type = DEVSTAT_NO_DATA;
953229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
954229997Sken		beio->io_len = 0;
955229997Sken		be_lun->lun_flush(be_lun, beio);
956229997Sken		break;
957229997Sken	default:
958229997Sken		panic("Unhandled CDB type %#x", io->scsiio.cdb[0]);
959229997Sken		break;
960229997Sken	}
961229997Sken}
962229997Sken
963258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, start, "uint64_t");
964258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, start, "uint64_t");
965258622SavgSDT_PROBE_DEFINE1(cbb, kernel, read, alloc_done, "uint64_t");
966258622SavgSDT_PROBE_DEFINE1(cbb, kernel, write, alloc_done, "uint64_t");
967229997Sken
968229997Skenstatic void
969229997Skenctl_be_block_dispatch(struct ctl_be_block_lun *be_lun,
970229997Sken			   union ctl_io *io)
971229997Sken{
972229997Sken	struct ctl_be_block_io *beio;
973229997Sken	struct ctl_be_block_softc *softc;
974229997Sken	struct ctl_lba_len lbalen;
975229997Sken	uint64_t len_left, io_size_bytes;
976229997Sken	int i;
977229997Sken
978229997Sken	softc = be_lun->softc;
979229997Sken
980229997Sken	DPRINTF("entered\n");
981229997Sken
982229997Sken	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
983229997Sken		SDT_PROBE(cbb, kernel, read, start, 0, 0, 0, 0, 0);
984229997Sken	} else {
985229997Sken		SDT_PROBE(cbb, kernel, write, start, 0, 0, 0, 0, 0);
986229997Sken	}
987229997Sken
988229997Sken	memcpy(&lbalen, io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
989229997Sken	       sizeof(lbalen));
990229997Sken
991229997Sken	io_size_bytes = lbalen.len * be_lun->blocksize;
992229997Sken
993229997Sken	/*
994229997Sken	 * XXX KDM this is temporary, until we implement chaining of beio
995229997Sken	 * structures and multiple datamove calls to move all the data in
996229997Sken	 * or out.
997229997Sken	 */
998229997Sken	if (io_size_bytes > CTLBLK_MAX_IO_SIZE) {
999229997Sken		printf("%s: IO length %ju > max io size %u\n", __func__,
1000229997Sken		       io_size_bytes, CTLBLK_MAX_IO_SIZE);
1001229997Sken		ctl_set_invalid_field(&io->scsiio,
1002229997Sken				      /*sks_valid*/ 0,
1003229997Sken				      /*command*/ 1,
1004229997Sken				      /*field*/ 0,
1005229997Sken				      /*bit_valid*/ 0,
1006229997Sken				      /*bit*/ 0);
1007229997Sken		ctl_done(io);
1008229997Sken		return;
1009229997Sken	}
1010229997Sken
1011229997Sken	beio = ctl_alloc_beio(softc);
1012229997Sken	if (beio == NULL) {
1013229997Sken		/*
1014229997Sken		 * This should not happen.  ctl_alloc_beio() will call
1015229997Sken		 * ctl_grow_beio() with a blocking malloc as needed.
1016229997Sken		 * A malloc with M_WAITOK should not fail.
1017229997Sken		 */
1018229997Sken		ctl_set_busy(&io->scsiio);
1019229997Sken		ctl_done(io);
1020229997Sken		return;
1021229997Sken	}
1022229997Sken
1023229997Sken	beio->io = io;
1024229997Sken	beio->softc = softc;
1025229997Sken	beio->lun = be_lun;
1026229997Sken	io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr = beio;
1027229997Sken
1028229997Sken	/*
1029229997Sken	 * If the I/O came down with an ordered or head of queue tag, set
1030229997Sken	 * the BIO_ORDERED attribute.  For head of queue tags, that's
1031229997Sken	 * pretty much the best we can do.
1032229997Sken	 *
1033229997Sken	 * XXX KDM we don't have a great way to easily know about the FUA
1034229997Sken	 * bit right now (it is decoded in ctl_read_write(), but we don't
1035229997Sken	 * pass that knowledge to the backend), and in any case we would
1036229997Sken	 * need to determine how to handle it.
1037229997Sken	 */
1038229997Sken	if ((io->scsiio.tag_type == CTL_TAG_ORDERED)
1039229997Sken	 || (io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE))
1040229997Sken		beio->bio_flags = BIO_ORDERED;
1041229997Sken
1042229997Sken	switch (io->scsiio.tag_type) {
1043229997Sken	case CTL_TAG_ORDERED:
1044229997Sken		beio->ds_tag_type = DEVSTAT_TAG_ORDERED;
1045229997Sken		break;
1046229997Sken	case CTL_TAG_HEAD_OF_QUEUE:
1047229997Sken		beio->ds_tag_type = DEVSTAT_TAG_HEAD;
1048229997Sken		break;
1049229997Sken	case CTL_TAG_UNTAGGED:
1050229997Sken	case CTL_TAG_SIMPLE:
1051229997Sken	case CTL_TAG_ACA:
1052229997Sken	default:
1053229997Sken		beio->ds_tag_type = DEVSTAT_TAG_SIMPLE;
1054229997Sken		break;
1055229997Sken	}
1056229997Sken
1057229997Sken	/*
1058229997Sken	 * This path handles read and write only.  The config write path
1059229997Sken	 * handles flush operations.
1060229997Sken	 */
1061229997Sken	if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) {
1062229997Sken		beio->bio_cmd = BIO_READ;
1063229997Sken		beio->ds_trans_type = DEVSTAT_READ;
1064229997Sken	} else {
1065229997Sken		beio->bio_cmd = BIO_WRITE;
1066229997Sken		beio->ds_trans_type = DEVSTAT_WRITE;
1067229997Sken	}
1068229997Sken
1069229997Sken	beio->io_len = lbalen.len * be_lun->blocksize;
1070229997Sken	beio->io_offset = lbalen.lba * be_lun->blocksize;
1071229997Sken
1072229997Sken	DPRINTF("%s at LBA %jx len %u\n",
1073229997Sken	       (beio->bio_cmd == BIO_READ) ? "READ" : "WRITE",
1074229997Sken	       (uintmax_t)lbalen.lba, lbalen.len);
1075229997Sken
1076229997Sken	for (i = 0, len_left = io_size_bytes; i < CTLBLK_MAX_SEGS &&
1077229997Sken	     len_left > 0; i++) {
1078229997Sken
1079229997Sken		/*
1080229997Sken		 * Setup the S/G entry for this chunk.
1081229997Sken		 */
1082229997Sken		beio->sg_segs[i].len = min(MAXPHYS, len_left);
1083229997Sken		beio->sg_segs[i].addr = uma_zalloc(be_lun->lun_zone, M_WAITOK);
1084229997Sken
1085229997Sken		DPRINTF("segment %d addr %p len %zd\n", i,
1086229997Sken			beio->sg_segs[i].addr, beio->sg_segs[i].len);
1087229997Sken
1088229997Sken		beio->num_segs++;
1089229997Sken		len_left -= beio->sg_segs[i].len;
1090229997Sken	}
1091229997Sken
1092229997Sken	/*
1093229997Sken	 * For the read case, we need to read the data into our buffers and
1094229997Sken	 * then we can send it back to the user.  For the write case, we
1095229997Sken	 * need to get the data from the user first.
1096229997Sken	 */
1097229997Sken	if (beio->bio_cmd == BIO_READ) {
1098229997Sken		SDT_PROBE(cbb, kernel, read, alloc_done, 0, 0, 0, 0, 0);
1099229997Sken		be_lun->dispatch(be_lun, beio);
1100229997Sken	} else {
1101229997Sken		SDT_PROBE(cbb, kernel, write, alloc_done, 0, 0, 0, 0, 0);
1102229997Sken		io->scsiio.be_move_done = ctl_be_block_move_done;
1103229997Sken		io->scsiio.kern_data_ptr = (uint8_t *)beio->sg_segs;
1104229997Sken		io->scsiio.kern_data_len = beio->io_len;
1105229997Sken		io->scsiio.kern_total_len = beio->io_len;
1106229997Sken		io->scsiio.kern_rel_offset = 0;
1107229997Sken		io->scsiio.kern_data_resid = 0;
1108229997Sken		io->scsiio.kern_sg_entries = beio->num_segs;
1109229997Sken		io->io_hdr.flags |= CTL_FLAG_ALLOCATED | CTL_FLAG_KDPTR_SGLIST;
1110229997Sken#ifdef CTL_TIME_IO
1111229997Sken        	getbintime(&io->io_hdr.dma_start_bt);
1112229997Sken#endif
1113229997Sken		ctl_datamove(io);
1114229997Sken	}
1115229997Sken}
1116229997Sken
1117229997Skenstatic void
1118229997Skenctl_be_block_worker(void *context, int pending)
1119229997Sken{
1120229997Sken	struct ctl_be_block_lun *be_lun;
1121229997Sken	struct ctl_be_block_softc *softc;
1122229997Sken	union ctl_io *io;
1123229997Sken
1124229997Sken	be_lun = (struct ctl_be_block_lun *)context;
1125229997Sken	softc = be_lun->softc;
1126229997Sken
1127229997Sken	DPRINTF("entered\n");
1128229997Sken
1129229997Sken	mtx_lock(&be_lun->lock);
1130229997Sken	for (;;) {
1131229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->datamove_queue);
1132229997Sken		if (io != NULL) {
1133229997Sken			struct ctl_be_block_io *beio;
1134229997Sken
1135229997Sken			DPRINTF("datamove queue\n");
1136229997Sken
1137229997Sken			STAILQ_REMOVE(&be_lun->datamove_queue, &io->io_hdr,
1138229997Sken				      ctl_io_hdr, links);
1139229997Sken
1140229997Sken			mtx_unlock(&be_lun->lock);
1141229997Sken
1142229997Sken			beio = (struct ctl_be_block_io *)
1143229997Sken			    io->io_hdr.ctl_private[CTL_PRIV_BACKEND].ptr;
1144229997Sken
1145229997Sken			be_lun->dispatch(be_lun, beio);
1146229997Sken
1147229997Sken			mtx_lock(&be_lun->lock);
1148229997Sken			continue;
1149229997Sken		}
1150229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->config_write_queue);
1151229997Sken		if (io != NULL) {
1152229997Sken
1153229997Sken			DPRINTF("config write queue\n");
1154229997Sken
1155229997Sken			STAILQ_REMOVE(&be_lun->config_write_queue, &io->io_hdr,
1156229997Sken				      ctl_io_hdr, links);
1157229997Sken
1158229997Sken			mtx_unlock(&be_lun->lock);
1159229997Sken
1160229997Sken			ctl_be_block_cw_dispatch(be_lun, io);
1161229997Sken
1162229997Sken			mtx_lock(&be_lun->lock);
1163229997Sken			continue;
1164229997Sken		}
1165229997Sken		io = (union ctl_io *)STAILQ_FIRST(&be_lun->input_queue);
1166229997Sken		if (io != NULL) {
1167229997Sken			DPRINTF("input queue\n");
1168229997Sken
1169229997Sken			STAILQ_REMOVE(&be_lun->input_queue, &io->io_hdr,
1170229997Sken				      ctl_io_hdr, links);
1171229997Sken			mtx_unlock(&be_lun->lock);
1172229997Sken
1173229997Sken			/*
1174229997Sken			 * We must drop the lock, since this routine and
1175229997Sken			 * its children may sleep.
1176229997Sken			 */
1177229997Sken			ctl_be_block_dispatch(be_lun, io);
1178229997Sken
1179229997Sken			mtx_lock(&be_lun->lock);
1180229997Sken			continue;
1181229997Sken		}
1182229997Sken
1183229997Sken		/*
1184229997Sken		 * If we get here, there is no work left in the queues, so
1185229997Sken		 * just break out and let the task queue go to sleep.
1186229997Sken		 */
1187229997Sken		break;
1188229997Sken	}
1189229997Sken	mtx_unlock(&be_lun->lock);
1190229997Sken}
1191229997Sken
1192229997Sken/*
1193229997Sken * Entry point from CTL to the backend for I/O.  We queue everything to a
1194229997Sken * work thread, so this just puts the I/O on a queue and wakes up the
1195229997Sken * thread.
1196229997Sken */
1197229997Skenstatic int
1198229997Skenctl_be_block_submit(union ctl_io *io)
1199229997Sken{
1200229997Sken	struct ctl_be_block_lun *be_lun;
1201229997Sken	struct ctl_be_lun *ctl_be_lun;
1202229997Sken	int retval;
1203229997Sken
1204229997Sken	DPRINTF("entered\n");
1205229997Sken
1206229997Sken	retval = CTL_RETVAL_COMPLETE;
1207229997Sken
1208229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
1209229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
1210229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
1211229997Sken
1212229997Sken	/*
1213229997Sken	 * Make sure we only get SCSI I/O.
1214229997Sken	 */
1215229997Sken	KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, ("Non-SCSI I/O (type "
1216229997Sken		"%#x) encountered", io->io_hdr.io_type));
1217229997Sken
1218229997Sken	mtx_lock(&be_lun->lock);
1219229997Sken	/*
1220229997Sken	 * XXX KDM make sure that links is okay to use at this point.
1221229997Sken	 * Otherwise, we either need to add another field to ctl_io_hdr,
1222229997Sken	 * or deal with resource allocation here.
1223229997Sken	 */
1224229997Sken	STAILQ_INSERT_TAIL(&be_lun->input_queue, &io->io_hdr, links);
1225229997Sken	mtx_unlock(&be_lun->lock);
1226229997Sken
1227229997Sken	taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
1228229997Sken
1229229997Sken	return (retval);
1230229997Sken}
1231229997Sken
1232229997Skenstatic int
1233229997Skenctl_be_block_ioctl(struct cdev *dev, u_long cmd, caddr_t addr,
1234229997Sken			int flag, struct thread *td)
1235229997Sken{
1236229997Sken	struct ctl_be_block_softc *softc;
1237229997Sken	int error;
1238229997Sken
1239229997Sken	softc = &backend_block_softc;
1240229997Sken
1241229997Sken	error = 0;
1242229997Sken
1243229997Sken	switch (cmd) {
1244229997Sken	case CTL_LUN_REQ: {
1245229997Sken		struct ctl_lun_req *lun_req;
1246229997Sken
1247229997Sken		lun_req = (struct ctl_lun_req *)addr;
1248229997Sken
1249229997Sken		switch (lun_req->reqtype) {
1250229997Sken		case CTL_LUNREQ_CREATE:
1251229997Sken			error = ctl_be_block_create(softc, lun_req);
1252229997Sken			break;
1253229997Sken		case CTL_LUNREQ_RM:
1254229997Sken			error = ctl_be_block_rm(softc, lun_req);
1255229997Sken			break;
1256232604Strasz		case CTL_LUNREQ_MODIFY:
1257232604Strasz			error = ctl_be_block_modify(softc, lun_req);
1258232604Strasz			break;
1259229997Sken		default:
1260229997Sken			lun_req->status = CTL_LUN_ERROR;
1261229997Sken			snprintf(lun_req->error_str, sizeof(lun_req->error_str),
1262229997Sken				 "%s: invalid LUN request type %d", __func__,
1263229997Sken				 lun_req->reqtype);
1264229997Sken			break;
1265229997Sken		}
1266229997Sken		break;
1267229997Sken	}
1268229997Sken	default:
1269229997Sken		error = ENOTTY;
1270229997Sken		break;
1271229997Sken	}
1272229997Sken
1273229997Sken	return (error);
1274229997Sken}
1275229997Sken
1276229997Skenstatic int
1277229997Skenctl_be_block_open_file(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1278229997Sken{
1279229997Sken	struct ctl_be_block_filedata *file_data;
1280229997Sken	struct ctl_lun_create_params *params;
1281229997Sken	struct vattr		      vattr;
1282229997Sken	int			      error;
1283229997Sken
1284229997Sken	error = 0;
1285229997Sken	file_data = &be_lun->backend.file;
1286229997Sken	params = &req->reqdata.create;
1287229997Sken
1288229997Sken	be_lun->dev_type = CTL_BE_BLOCK_FILE;
1289229997Sken	be_lun->dispatch = ctl_be_block_dispatch_file;
1290229997Sken	be_lun->lun_flush = ctl_be_block_flush_file;
1291229997Sken
1292229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
1293229997Sken	if (error != 0) {
1294229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1295229997Sken			 "error calling VOP_GETATTR() for file %s",
1296229997Sken			 be_lun->dev_path);
1297229997Sken		return (error);
1298229997Sken	}
1299229997Sken
1300229997Sken	/*
1301229997Sken	 * Verify that we have the ability to upgrade to exclusive
1302229997Sken	 * access on this file so we can trap errors at open instead
1303229997Sken	 * of reporting them during first access.
1304229997Sken	 */
1305229997Sken	if (VOP_ISLOCKED(be_lun->vn) != LK_EXCLUSIVE) {
1306229997Sken		vn_lock(be_lun->vn, LK_UPGRADE | LK_RETRY);
1307229997Sken		if (be_lun->vn->v_iflag & VI_DOOMED) {
1308229997Sken			error = EBADF;
1309229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1310229997Sken				 "error locking file %s", be_lun->dev_path);
1311229997Sken			return (error);
1312229997Sken		}
1313229997Sken	}
1314229997Sken
1315229997Sken
1316229997Sken	file_data->cred = crhold(curthread->td_ucred);
1317232604Strasz	if (params->lun_size_bytes != 0)
1318232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1319232604Strasz	else
1320232604Strasz		be_lun->size_bytes = vattr.va_size;
1321229997Sken	/*
1322229997Sken	 * We set the multi thread flag for file operations because all
1323229997Sken	 * filesystems (in theory) are capable of allowing multiple readers
1324229997Sken	 * of a file at once.  So we want to get the maximum possible
1325229997Sken	 * concurrency.
1326229997Sken	 */
1327229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_MULTI_THREAD;
1328229997Sken
1329229997Sken	/*
1330229997Sken	 * XXX KDM vattr.va_blocksize may be larger than 512 bytes here.
1331229997Sken	 * With ZFS, it is 131072 bytes.  Block sizes that large don't work
1332229997Sken	 * with disklabel and UFS on FreeBSD at least.  Large block sizes
1333229997Sken	 * may not work with other OSes as well.  So just export a sector
1334229997Sken	 * size of 512 bytes, which should work with any OS or
1335229997Sken	 * application.  Since our backing is a file, any block size will
1336229997Sken	 * work fine for the backing store.
1337229997Sken	 */
1338229997Sken#if 0
1339229997Sken	be_lun->blocksize= vattr.va_blocksize;
1340229997Sken#endif
1341229997Sken	if (params->blocksize_bytes != 0)
1342229997Sken		be_lun->blocksize = params->blocksize_bytes;
1343229997Sken	else
1344229997Sken		be_lun->blocksize = 512;
1345229997Sken
1346229997Sken	/*
1347229997Sken	 * Sanity check.  The media size has to be at least one
1348229997Sken	 * sector long.
1349229997Sken	 */
1350229997Sken	if (be_lun->size_bytes < be_lun->blocksize) {
1351229997Sken		error = EINVAL;
1352229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1353229997Sken			 "file %s size %ju < block size %u", be_lun->dev_path,
1354229997Sken			 (uintmax_t)be_lun->size_bytes, be_lun->blocksize);
1355229997Sken	}
1356229997Sken	return (error);
1357229997Sken}
1358229997Sken
1359229997Skenstatic int
1360229997Skenctl_be_block_open_dev(struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1361229997Sken{
1362229997Sken	struct ctl_lun_create_params *params;
1363229997Sken	struct vattr		      vattr;
1364229997Sken	struct cdev		     *dev;
1365229997Sken	struct cdevsw		     *devsw;
1366229997Sken	int			      error;
1367229997Sken
1368229997Sken	params = &req->reqdata.create;
1369229997Sken
1370229997Sken	be_lun->dev_type = CTL_BE_BLOCK_DEV;
1371229997Sken	be_lun->dispatch = ctl_be_block_dispatch_dev;
1372229997Sken	be_lun->lun_flush = ctl_be_block_flush_dev;
1373229997Sken	be_lun->backend.dev.cdev = be_lun->vn->v_rdev;
1374229997Sken	be_lun->backend.dev.csw = dev_refthread(be_lun->backend.dev.cdev,
1375229997Sken					     &be_lun->backend.dev.dev_ref);
1376229997Sken	if (be_lun->backend.dev.csw == NULL)
1377229997Sken		panic("Unable to retrieve device switch");
1378229997Sken
1379229997Sken	error = VOP_GETATTR(be_lun->vn, &vattr, NOCRED);
1380229997Sken	if (error) {
1381229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1382229997Sken			 "%s: error getting vnode attributes for device %s",
1383229997Sken			 __func__, be_lun->dev_path);
1384229997Sken		return (error);
1385229997Sken	}
1386229997Sken
1387229997Sken	dev = be_lun->vn->v_rdev;
1388229997Sken	devsw = dev->si_devsw;
1389229997Sken	if (!devsw->d_ioctl) {
1390229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1391229997Sken			 "%s: no d_ioctl for device %s!", __func__,
1392229997Sken			 be_lun->dev_path);
1393229997Sken		return (ENODEV);
1394229997Sken	}
1395229997Sken
1396229997Sken	error = devsw->d_ioctl(dev, DIOCGSECTORSIZE,
1397229997Sken			       (caddr_t)&be_lun->blocksize, FREAD,
1398229997Sken			       curthread);
1399229997Sken	if (error) {
1400229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1401229997Sken			 "%s: error %d returned for DIOCGSECTORSIZE ioctl "
1402229997Sken			 "on %s!", __func__, error, be_lun->dev_path);
1403229997Sken		return (error);
1404229997Sken	}
1405229997Sken
1406229997Sken	/*
1407229997Sken	 * If the user has asked for a blocksize that is greater than the
1408229997Sken	 * backing device's blocksize, we can do it only if the blocksize
1409229997Sken	 * the user is asking for is an even multiple of the underlying
1410229997Sken	 * device's blocksize.
1411229997Sken	 */
1412229997Sken	if ((params->blocksize_bytes != 0)
1413229997Sken	 && (params->blocksize_bytes > be_lun->blocksize)) {
1414229997Sken		uint32_t bs_multiple, tmp_blocksize;
1415229997Sken
1416229997Sken		bs_multiple = params->blocksize_bytes / be_lun->blocksize;
1417229997Sken
1418229997Sken		tmp_blocksize = bs_multiple * be_lun->blocksize;
1419229997Sken
1420229997Sken		if (tmp_blocksize == params->blocksize_bytes) {
1421229997Sken			be_lun->blocksize = params->blocksize_bytes;
1422229997Sken		} else {
1423229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1424229997Sken				 "%s: requested blocksize %u is not an even "
1425229997Sken				 "multiple of backing device blocksize %u",
1426229997Sken				 __func__, params->blocksize_bytes,
1427229997Sken				 be_lun->blocksize);
1428229997Sken			return (EINVAL);
1429229997Sken
1430229997Sken		}
1431229997Sken	} else if ((params->blocksize_bytes != 0)
1432229997Sken		&& (params->blocksize_bytes != be_lun->blocksize)) {
1433229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1434229997Sken			 "%s: requested blocksize %u < backing device "
1435229997Sken			 "blocksize %u", __func__, params->blocksize_bytes,
1436229997Sken			 be_lun->blocksize);
1437229997Sken		return (EINVAL);
1438229997Sken	}
1439229997Sken
1440229997Sken	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
1441229997Sken			       (caddr_t)&be_lun->size_bytes, FREAD,
1442229997Sken			       curthread);
1443229997Sken	if (error) {
1444229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1445232604Strasz			 "%s: error %d returned for DIOCGMEDIASIZE "
1446232604Strasz			 " ioctl on %s!", __func__, error,
1447232604Strasz			 be_lun->dev_path);
1448229997Sken		return (error);
1449229997Sken	}
1450229997Sken
1451232604Strasz	if (params->lun_size_bytes != 0) {
1452232604Strasz		if (params->lun_size_bytes > be_lun->size_bytes) {
1453232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
1454232604Strasz				 "%s: requested LUN size %ju > backing device "
1455232604Strasz				 "size %ju", __func__,
1456232604Strasz				 (uintmax_t)params->lun_size_bytes,
1457232604Strasz				 (uintmax_t)be_lun->size_bytes);
1458232604Strasz			return (EINVAL);
1459232604Strasz		}
1460232604Strasz
1461232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
1462232604Strasz	}
1463232604Strasz
1464229997Sken	return (0);
1465229997Sken}
1466229997Sken
1467229997Skenstatic int
1468229997Skenctl_be_block_close(struct ctl_be_block_lun *be_lun)
1469229997Sken{
1470229997Sken	DROP_GIANT();
1471229997Sken	if (be_lun->vn) {
1472229997Sken		int flags = FREAD | FWRITE;
1473229997Sken
1474229997Sken		switch (be_lun->dev_type) {
1475229997Sken		case CTL_BE_BLOCK_DEV:
1476229997Sken			if (be_lun->backend.dev.csw) {
1477229997Sken				dev_relthread(be_lun->backend.dev.cdev,
1478229997Sken					      be_lun->backend.dev.dev_ref);
1479229997Sken				be_lun->backend.dev.csw  = NULL;
1480229997Sken				be_lun->backend.dev.cdev = NULL;
1481229997Sken			}
1482229997Sken			break;
1483229997Sken		case CTL_BE_BLOCK_FILE:
1484229997Sken			break;
1485229997Sken		case CTL_BE_BLOCK_NONE:
1486258871Strasz			break;
1487229997Sken		default:
1488229997Sken			panic("Unexpected backend type.");
1489229997Sken			break;
1490229997Sken		}
1491229997Sken
1492229997Sken		(void)vn_close(be_lun->vn, flags, NOCRED, curthread);
1493229997Sken		be_lun->vn = NULL;
1494229997Sken
1495229997Sken		switch (be_lun->dev_type) {
1496229997Sken		case CTL_BE_BLOCK_DEV:
1497229997Sken			break;
1498229997Sken		case CTL_BE_BLOCK_FILE:
1499229997Sken			if (be_lun->backend.file.cred != NULL) {
1500229997Sken				crfree(be_lun->backend.file.cred);
1501229997Sken				be_lun->backend.file.cred = NULL;
1502229997Sken			}
1503229997Sken			break;
1504229997Sken		case CTL_BE_BLOCK_NONE:
1505258871Strasz			break;
1506229997Sken		default:
1507229997Sken			panic("Unexpected backend type.");
1508229997Sken			break;
1509229997Sken		}
1510229997Sken	}
1511229997Sken	PICKUP_GIANT();
1512229997Sken
1513229997Sken	return (0);
1514229997Sken}
1515229997Sken
1516229997Skenstatic int
1517229997Skenctl_be_block_open(struct ctl_be_block_softc *softc,
1518229997Sken		       struct ctl_be_block_lun *be_lun, struct ctl_lun_req *req)
1519229997Sken{
1520229997Sken	struct nameidata nd;
1521229997Sken	int		 flags;
1522229997Sken	int		 error;
1523229997Sken
1524229997Sken	/*
1525229997Sken	 * XXX KDM allow a read-only option?
1526229997Sken	 */
1527229997Sken	flags = FREAD | FWRITE;
1528229997Sken	error = 0;
1529229997Sken
1530229997Sken	if (rootvnode == NULL) {
1531229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1532229997Sken			 "%s: Root filesystem is not mounted", __func__);
1533229997Sken		return (1);
1534229997Sken	}
1535229997Sken
1536229997Sken	if (!curthread->td_proc->p_fd->fd_cdir) {
1537229997Sken		curthread->td_proc->p_fd->fd_cdir = rootvnode;
1538229997Sken		VREF(rootvnode);
1539229997Sken	}
1540229997Sken	if (!curthread->td_proc->p_fd->fd_rdir) {
1541229997Sken		curthread->td_proc->p_fd->fd_rdir = rootvnode;
1542229997Sken		VREF(rootvnode);
1543229997Sken	}
1544229997Sken	if (!curthread->td_proc->p_fd->fd_jdir) {
1545229997Sken		curthread->td_proc->p_fd->fd_jdir = rootvnode;
1546229997Sken		VREF(rootvnode);
1547229997Sken	}
1548229997Sken
1549229997Sken again:
1550229997Sken	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, be_lun->dev_path, curthread);
1551229997Sken	error = vn_open(&nd, &flags, 0, NULL);
1552229997Sken	if (error) {
1553229997Sken		/*
1554229997Sken		 * This is the only reasonable guess we can make as far as
1555229997Sken		 * path if the user doesn't give us a fully qualified path.
1556229997Sken		 * If they want to specify a file, they need to specify the
1557229997Sken		 * full path.
1558229997Sken		 */
1559229997Sken		if (be_lun->dev_path[0] != '/') {
1560229997Sken			char *dev_path = "/dev/";
1561229997Sken			char *dev_name;
1562229997Sken
1563229997Sken			/* Try adding device path at beginning of name */
1564229997Sken			dev_name = malloc(strlen(be_lun->dev_path)
1565229997Sken					+ strlen(dev_path) + 1,
1566229997Sken					  M_CTLBLK, M_WAITOK);
1567229997Sken			if (dev_name) {
1568229997Sken				sprintf(dev_name, "%s%s", dev_path,
1569229997Sken					be_lun->dev_path);
1570229997Sken				free(be_lun->dev_path, M_CTLBLK);
1571229997Sken				be_lun->dev_path = dev_name;
1572229997Sken				goto again;
1573229997Sken			}
1574229997Sken		}
1575229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1576229997Sken			 "%s: error opening %s", __func__, be_lun->dev_path);
1577229997Sken		return (error);
1578229997Sken	}
1579229997Sken
1580229997Sken	NDFREE(&nd, NDF_ONLY_PNBUF);
1581229997Sken
1582229997Sken	be_lun->vn = nd.ni_vp;
1583229997Sken
1584229997Sken	/* We only support disks and files. */
1585229997Sken	if (vn_isdisk(be_lun->vn, &error)) {
1586229997Sken		error = ctl_be_block_open_dev(be_lun, req);
1587229997Sken	} else if (be_lun->vn->v_type == VREG) {
1588229997Sken		error = ctl_be_block_open_file(be_lun, req);
1589229997Sken	} else {
1590229997Sken		error = EINVAL;
1591229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1592258871Strasz			 "%s is not a disk or plain file", be_lun->dev_path);
1593229997Sken	}
1594229997Sken	VOP_UNLOCK(be_lun->vn, 0);
1595229997Sken
1596229997Sken	if (error != 0) {
1597229997Sken		ctl_be_block_close(be_lun);
1598229997Sken		return (error);
1599229997Sken	}
1600229997Sken
1601229997Sken	be_lun->blocksize_shift = fls(be_lun->blocksize) - 1;
1602229997Sken	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
1603229997Sken
1604229997Sken	return (0);
1605229997Sken}
1606229997Sken
1607229997Skenstatic int
1608229997Skenctl_be_block_create(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1609229997Sken{
1610229997Sken	struct ctl_be_block_lun *be_lun;
1611229997Sken	struct ctl_lun_create_params *params;
1612229997Sken	struct ctl_be_arg *file_arg;
1613229997Sken	char tmpstr[32];
1614229997Sken	int retval, num_threads;
1615229997Sken	int i;
1616229997Sken
1617229997Sken	params = &req->reqdata.create;
1618229997Sken	retval = 0;
1619229997Sken
1620229997Sken	num_threads = cbb_num_threads;
1621229997Sken
1622229997Sken	file_arg = NULL;
1623229997Sken
1624229997Sken	be_lun = malloc(sizeof(*be_lun), M_CTLBLK, M_ZERO | M_WAITOK);
1625229997Sken
1626229997Sken	be_lun->softc = softc;
1627229997Sken	STAILQ_INIT(&be_lun->input_queue);
1628229997Sken	STAILQ_INIT(&be_lun->config_write_queue);
1629229997Sken	STAILQ_INIT(&be_lun->datamove_queue);
1630254759Strasz	STAILQ_INIT(&be_lun->ctl_be_lun.options);
1631229997Sken	sprintf(be_lun->lunname, "cblk%d", softc->num_luns);
1632229997Sken	mtx_init(&be_lun->lock, be_lun->lunname, NULL, MTX_DEF);
1633229997Sken
1634229997Sken	be_lun->lun_zone = uma_zcreate(be_lun->lunname, MAXPHYS,
1635256995Smav	    NULL, NULL, NULL, NULL, /*align*/ 0, /*flags*/0);
1636229997Sken
1637229997Sken	if (be_lun->lun_zone == NULL) {
1638229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1639229997Sken			 "%s: error allocating UMA zone", __func__);
1640229997Sken		goto bailout_error;
1641229997Sken	}
1642229997Sken
1643229997Sken	if (params->flags & CTL_LUN_FLAG_DEV_TYPE)
1644229997Sken		be_lun->ctl_be_lun.lun_type = params->device_type;
1645229997Sken	else
1646229997Sken		be_lun->ctl_be_lun.lun_type = T_DIRECT;
1647229997Sken
1648229997Sken	if (be_lun->ctl_be_lun.lun_type == T_DIRECT) {
1649229997Sken		for (i = 0; i < req->num_be_args; i++) {
1650249026Strasz			if (strcmp(req->kern_be_args[i].kname, "file") == 0) {
1651229997Sken				file_arg = &req->kern_be_args[i];
1652229997Sken				break;
1653229997Sken			}
1654229997Sken		}
1655229997Sken
1656229997Sken		if (file_arg == NULL) {
1657229997Sken			snprintf(req->error_str, sizeof(req->error_str),
1658229997Sken				 "%s: no file argument specified", __func__);
1659229997Sken			goto bailout_error;
1660229997Sken		}
1661229997Sken
1662229997Sken		be_lun->dev_path = malloc(file_arg->vallen, M_CTLBLK,
1663229997Sken					  M_WAITOK | M_ZERO);
1664229997Sken
1665249026Strasz		strlcpy(be_lun->dev_path, (char *)file_arg->kvalue,
1666229997Sken			file_arg->vallen);
1667229997Sken
1668229997Sken		retval = ctl_be_block_open(softc, be_lun, req);
1669229997Sken		if (retval != 0) {
1670229997Sken			retval = 0;
1671229997Sken			goto bailout_error;
1672229997Sken		}
1673229997Sken
1674229997Sken		/*
1675229997Sken		 * Tell the user the size of the file/device.
1676229997Sken		 */
1677229997Sken		params->lun_size_bytes = be_lun->size_bytes;
1678229997Sken
1679229997Sken		/*
1680229997Sken		 * The maximum LBA is the size - 1.
1681229997Sken		 */
1682229997Sken		be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
1683229997Sken	} else {
1684229997Sken		/*
1685229997Sken		 * For processor devices, we don't have any size.
1686229997Sken		 */
1687229997Sken		be_lun->blocksize = 0;
1688229997Sken		be_lun->size_blocks = 0;
1689229997Sken		be_lun->size_bytes = 0;
1690229997Sken		be_lun->ctl_be_lun.maxlba = 0;
1691229997Sken		params->lun_size_bytes = 0;
1692229997Sken
1693229997Sken		/*
1694229997Sken		 * Default to just 1 thread for processor devices.
1695229997Sken		 */
1696229997Sken		num_threads = 1;
1697229997Sken	}
1698229997Sken
1699229997Sken	/*
1700229997Sken	 * XXX This searching loop might be refactored to be combined with
1701229997Sken	 * the loop above,
1702229997Sken	 */
1703229997Sken	for (i = 0; i < req->num_be_args; i++) {
1704249026Strasz		if (strcmp(req->kern_be_args[i].kname, "num_threads") == 0) {
1705229997Sken			struct ctl_be_arg *thread_arg;
1706229997Sken			char num_thread_str[16];
1707229997Sken			int tmp_num_threads;
1708229997Sken
1709229997Sken
1710229997Sken			thread_arg = &req->kern_be_args[i];
1711229997Sken
1712249026Strasz			strlcpy(num_thread_str, (char *)thread_arg->kvalue,
1713229997Sken				min(thread_arg->vallen,
1714229997Sken				sizeof(num_thread_str)));
1715229997Sken
1716229997Sken			tmp_num_threads = strtol(num_thread_str, NULL, 0);
1717229997Sken
1718229997Sken			/*
1719229997Sken			 * We don't let the user specify less than one
1720229997Sken			 * thread, but hope he's clueful enough not to
1721229997Sken			 * specify 1000 threads.
1722229997Sken			 */
1723229997Sken			if (tmp_num_threads < 1) {
1724229997Sken				snprintf(req->error_str, sizeof(req->error_str),
1725229997Sken					 "%s: invalid number of threads %s",
1726229997Sken				         __func__, num_thread_str);
1727229997Sken				goto bailout_error;
1728229997Sken			}
1729229997Sken
1730229997Sken			num_threads = tmp_num_threads;
1731254759Strasz		} else if (strcmp(req->kern_be_args[i].kname, "file") != 0 &&
1732254759Strasz		    strcmp(req->kern_be_args[i].kname, "dev") != 0) {
1733254759Strasz			struct ctl_be_lun_option *opt;
1734254759Strasz
1735254759Strasz			opt = malloc(sizeof(*opt), M_CTLBLK, M_WAITOK);
1736254759Strasz			opt->name = malloc(strlen(req->kern_be_args[i].kname) + 1, M_CTLBLK, M_WAITOK);
1737254759Strasz			strcpy(opt->name, req->kern_be_args[i].kname);
1738254759Strasz			opt->value = malloc(strlen(req->kern_be_args[i].kvalue) + 1, M_CTLBLK, M_WAITOK);
1739254759Strasz			strcpy(opt->value, req->kern_be_args[i].kvalue);
1740254759Strasz			STAILQ_INSERT_TAIL(&be_lun->ctl_be_lun.options, opt, links);
1741229997Sken		}
1742229997Sken	}
1743229997Sken
1744229997Sken	be_lun->flags = CTL_BE_BLOCK_LUN_UNCONFIGURED;
1745229997Sken	be_lun->ctl_be_lun.flags = CTL_LUN_FLAG_PRIMARY;
1746229997Sken	be_lun->ctl_be_lun.be_lun = be_lun;
1747229997Sken	be_lun->ctl_be_lun.blocksize = be_lun->blocksize;
1748229997Sken	/* Tell the user the blocksize we ended up using */
1749229997Sken	params->blocksize_bytes = be_lun->blocksize;
1750229997Sken	if (params->flags & CTL_LUN_FLAG_ID_REQ) {
1751229997Sken		be_lun->ctl_be_lun.req_lun_id = params->req_lun_id;
1752229997Sken		be_lun->ctl_be_lun.flags |= CTL_LUN_FLAG_ID_REQ;
1753229997Sken	} else
1754229997Sken		be_lun->ctl_be_lun.req_lun_id = 0;
1755229997Sken
1756229997Sken	be_lun->ctl_be_lun.lun_shutdown = ctl_be_block_lun_shutdown;
1757229997Sken	be_lun->ctl_be_lun.lun_config_status =
1758229997Sken		ctl_be_block_lun_config_status;
1759229997Sken	be_lun->ctl_be_lun.be = &ctl_be_block_driver;
1760229997Sken
1761229997Sken	if ((params->flags & CTL_LUN_FLAG_SERIAL_NUM) == 0) {
1762229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYSERIAL%4d",
1763229997Sken			 softc->num_luns);
1764229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num, tmpstr,
1765229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
1766229997Sken			sizeof(tmpstr)));
1767229997Sken
1768229997Sken		/* Tell the user what we used for a serial number */
1769229997Sken		strncpy((char *)params->serial_num, tmpstr,
1770229997Sken			ctl_min(sizeof(params->serial_num), sizeof(tmpstr)));
1771229997Sken	} else {
1772229997Sken		strncpy((char *)be_lun->ctl_be_lun.serial_num,
1773229997Sken			params->serial_num,
1774229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.serial_num),
1775229997Sken			sizeof(params->serial_num)));
1776229997Sken	}
1777229997Sken	if ((params->flags & CTL_LUN_FLAG_DEVID) == 0) {
1778229997Sken		snprintf(tmpstr, sizeof(tmpstr), "MYDEVID%4d", softc->num_luns);
1779229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id, tmpstr,
1780229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
1781229997Sken			sizeof(tmpstr)));
1782229997Sken
1783229997Sken		/* Tell the user what we used for a device ID */
1784229997Sken		strncpy((char *)params->device_id, tmpstr,
1785229997Sken			ctl_min(sizeof(params->device_id), sizeof(tmpstr)));
1786229997Sken	} else {
1787229997Sken		strncpy((char *)be_lun->ctl_be_lun.device_id,
1788229997Sken			params->device_id,
1789229997Sken			ctl_min(sizeof(be_lun->ctl_be_lun.device_id),
1790229997Sken				sizeof(params->device_id)));
1791229997Sken	}
1792229997Sken
1793229997Sken	TASK_INIT(&be_lun->io_task, /*priority*/0, ctl_be_block_worker, be_lun);
1794229997Sken
1795229997Sken	be_lun->io_taskqueue = taskqueue_create(be_lun->lunname, M_WAITOK,
1796229997Sken	    taskqueue_thread_enqueue, /*context*/&be_lun->io_taskqueue);
1797229997Sken
1798229997Sken	if (be_lun->io_taskqueue == NULL) {
1799229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1800229997Sken			 "%s: Unable to create taskqueue", __func__);
1801229997Sken		goto bailout_error;
1802229997Sken	}
1803229997Sken
1804229997Sken	/*
1805229997Sken	 * Note that we start the same number of threads by default for
1806229997Sken	 * both the file case and the block device case.  For the file
1807229997Sken	 * case, we need multiple threads to allow concurrency, because the
1808229997Sken	 * vnode interface is designed to be a blocking interface.  For the
1809229997Sken	 * block device case, ZFS zvols at least will block the caller's
1810229997Sken	 * context in many instances, and so we need multiple threads to
1811229997Sken	 * overcome that problem.  Other block devices don't need as many
1812229997Sken	 * threads, but they shouldn't cause too many problems.
1813229997Sken	 *
1814229997Sken	 * If the user wants to just have a single thread for a block
1815229997Sken	 * device, he can specify that when the LUN is created, or change
1816229997Sken	 * the tunable/sysctl to alter the default number of threads.
1817229997Sken	 */
1818229997Sken	retval = taskqueue_start_threads(&be_lun->io_taskqueue,
1819229997Sken					 /*num threads*/num_threads,
1820229997Sken					 /*priority*/PWAIT,
1821229997Sken					 /*thread name*/
1822229997Sken					 "%s taskq", be_lun->lunname);
1823229997Sken
1824229997Sken	if (retval != 0)
1825229997Sken		goto bailout_error;
1826229997Sken
1827229997Sken	be_lun->num_threads = num_threads;
1828229997Sken
1829229997Sken	mtx_lock(&softc->lock);
1830229997Sken	softc->num_luns++;
1831229997Sken	STAILQ_INSERT_TAIL(&softc->lun_list, be_lun, links);
1832229997Sken
1833229997Sken	mtx_unlock(&softc->lock);
1834229997Sken
1835229997Sken	retval = ctl_add_lun(&be_lun->ctl_be_lun);
1836229997Sken	if (retval != 0) {
1837229997Sken		mtx_lock(&softc->lock);
1838229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
1839229997Sken			      links);
1840229997Sken		softc->num_luns--;
1841229997Sken		mtx_unlock(&softc->lock);
1842229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1843229997Sken			 "%s: ctl_add_lun() returned error %d, see dmesg for "
1844229997Sken			"details", __func__, retval);
1845229997Sken		retval = 0;
1846229997Sken		goto bailout_error;
1847229997Sken	}
1848229997Sken
1849229997Sken	mtx_lock(&softc->lock);
1850229997Sken
1851229997Sken	/*
1852229997Sken	 * Tell the config_status routine that we're waiting so it won't
1853229997Sken	 * clean up the LUN in the event of an error.
1854229997Sken	 */
1855229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
1856229997Sken
1857229997Sken	while (be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) {
1858229997Sken		retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
1859229997Sken		if (retval == EINTR)
1860229997Sken			break;
1861229997Sken	}
1862229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
1863229997Sken
1864229997Sken	if (be_lun->flags & CTL_BE_BLOCK_LUN_CONFIG_ERR) {
1865229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1866229997Sken			 "%s: LUN configuration error, see dmesg for details",
1867229997Sken			 __func__);
1868229997Sken		STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun,
1869229997Sken			      links);
1870229997Sken		softc->num_luns--;
1871229997Sken		mtx_unlock(&softc->lock);
1872229997Sken		goto bailout_error;
1873229997Sken	} else {
1874229997Sken		params->req_lun_id = be_lun->ctl_be_lun.lun_id;
1875229997Sken	}
1876229997Sken
1877229997Sken	mtx_unlock(&softc->lock);
1878229997Sken
1879229997Sken	be_lun->disk_stats = devstat_new_entry("cbb", params->req_lun_id,
1880229997Sken					       be_lun->blocksize,
1881229997Sken					       DEVSTAT_ALL_SUPPORTED,
1882229997Sken					       be_lun->ctl_be_lun.lun_type
1883229997Sken					       | DEVSTAT_TYPE_IF_OTHER,
1884229997Sken					       DEVSTAT_PRIORITY_OTHER);
1885229997Sken
1886229997Sken
1887229997Sken	req->status = CTL_LUN_OK;
1888229997Sken
1889229997Sken	return (retval);
1890229997Sken
1891229997Skenbailout_error:
1892229997Sken	req->status = CTL_LUN_ERROR;
1893229997Sken
1894229997Sken	ctl_be_block_close(be_lun);
1895229997Sken
1896229997Sken	free(be_lun->dev_path, M_CTLBLK);
1897229997Sken	free(be_lun, M_CTLBLK);
1898229997Sken
1899229997Sken	return (retval);
1900229997Sken}
1901229997Sken
1902229997Skenstatic int
1903229997Skenctl_be_block_rm(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
1904229997Sken{
1905229997Sken	struct ctl_lun_rm_params *params;
1906229997Sken	struct ctl_be_block_lun *be_lun;
1907229997Sken	int retval;
1908229997Sken
1909229997Sken	params = &req->reqdata.rm;
1910229997Sken
1911229997Sken	mtx_lock(&softc->lock);
1912229997Sken
1913229997Sken	be_lun = NULL;
1914229997Sken
1915229997Sken	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
1916229997Sken		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
1917229997Sken			break;
1918229997Sken	}
1919229997Sken	mtx_unlock(&softc->lock);
1920229997Sken
1921229997Sken	if (be_lun == NULL) {
1922229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1923229997Sken			 "%s: LUN %u is not managed by the block backend",
1924229997Sken			 __func__, params->lun_id);
1925229997Sken		goto bailout_error;
1926229997Sken	}
1927229997Sken
1928229997Sken	retval = ctl_disable_lun(&be_lun->ctl_be_lun);
1929229997Sken
1930229997Sken	if (retval != 0) {
1931229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1932229997Sken			 "%s: error %d returned from ctl_disable_lun() for "
1933229997Sken			 "LUN %d", __func__, retval, params->lun_id);
1934229997Sken		goto bailout_error;
1935229997Sken
1936229997Sken	}
1937229997Sken
1938229997Sken	retval = ctl_invalidate_lun(&be_lun->ctl_be_lun);
1939229997Sken	if (retval != 0) {
1940229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1941229997Sken			 "%s: error %d returned from ctl_invalidate_lun() for "
1942229997Sken			 "LUN %d", __func__, retval, params->lun_id);
1943229997Sken		goto bailout_error;
1944229997Sken	}
1945229997Sken
1946229997Sken	mtx_lock(&softc->lock);
1947229997Sken
1948229997Sken	be_lun->flags |= CTL_BE_BLOCK_LUN_WAITING;
1949229997Sken
1950229997Sken	while ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
1951229997Sken                retval = msleep(be_lun, &softc->lock, PCATCH, "ctlblk", 0);
1952229997Sken                if (retval == EINTR)
1953229997Sken                        break;
1954229997Sken        }
1955229997Sken
1956229997Sken	be_lun->flags &= ~CTL_BE_BLOCK_LUN_WAITING;
1957229997Sken
1958229997Sken	if ((be_lun->flags & CTL_BE_BLOCK_LUN_UNCONFIGURED) == 0) {
1959229997Sken		snprintf(req->error_str, sizeof(req->error_str),
1960229997Sken			 "%s: interrupted waiting for LUN to be freed",
1961229997Sken			 __func__);
1962229997Sken		mtx_unlock(&softc->lock);
1963229997Sken		goto bailout_error;
1964229997Sken	}
1965229997Sken
1966229997Sken	STAILQ_REMOVE(&softc->lun_list, be_lun, ctl_be_block_lun, links);
1967229997Sken
1968229997Sken	softc->num_luns--;
1969229997Sken	mtx_unlock(&softc->lock);
1970229997Sken
1971229997Sken	taskqueue_drain(be_lun->io_taskqueue, &be_lun->io_task);
1972229997Sken
1973229997Sken	taskqueue_free(be_lun->io_taskqueue);
1974229997Sken
1975229997Sken	ctl_be_block_close(be_lun);
1976229997Sken
1977229997Sken	if (be_lun->disk_stats != NULL)
1978229997Sken		devstat_remove_entry(be_lun->disk_stats);
1979229997Sken
1980229997Sken	uma_zdestroy(be_lun->lun_zone);
1981229997Sken
1982229997Sken	free(be_lun->dev_path, M_CTLBLK);
1983229997Sken
1984229997Sken	free(be_lun, M_CTLBLK);
1985229997Sken
1986229997Sken	req->status = CTL_LUN_OK;
1987229997Sken
1988229997Sken	return (0);
1989229997Sken
1990229997Skenbailout_error:
1991229997Sken
1992229997Sken	req->status = CTL_LUN_ERROR;
1993229997Sken
1994229997Sken	return (0);
1995229997Sken}
1996229997Sken
1997232604Straszstatic int
1998232604Straszctl_be_block_modify_file(struct ctl_be_block_lun *be_lun,
1999232604Strasz			 struct ctl_lun_req *req)
2000232604Strasz{
2001232604Strasz	struct vattr vattr;
2002232604Strasz	int error;
2003232604Strasz	struct ctl_lun_modify_params *params;
2004232604Strasz
2005232604Strasz	params = &req->reqdata.modify;
2006232604Strasz
2007232604Strasz	if (params->lun_size_bytes != 0) {
2008232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2009232604Strasz	} else  {
2010232604Strasz		error = VOP_GETATTR(be_lun->vn, &vattr, curthread->td_ucred);
2011232604Strasz		if (error != 0) {
2012232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2013232604Strasz				 "error calling VOP_GETATTR() for file %s",
2014232604Strasz				 be_lun->dev_path);
2015232604Strasz			return (error);
2016232604Strasz		}
2017232604Strasz
2018232604Strasz		be_lun->size_bytes = vattr.va_size;
2019232604Strasz	}
2020232604Strasz
2021232604Strasz	return (0);
2022232604Strasz}
2023232604Strasz
2024232604Straszstatic int
2025232604Straszctl_be_block_modify_dev(struct ctl_be_block_lun *be_lun,
2026232604Strasz			struct ctl_lun_req *req)
2027232604Strasz{
2028232604Strasz	struct cdev *dev;
2029232604Strasz	struct cdevsw *devsw;
2030232604Strasz	int error;
2031232604Strasz	struct ctl_lun_modify_params *params;
2032232604Strasz	uint64_t size_bytes;
2033232604Strasz
2034232604Strasz	params = &req->reqdata.modify;
2035232604Strasz
2036232604Strasz	dev = be_lun->vn->v_rdev;
2037232604Strasz	devsw = dev->si_devsw;
2038232604Strasz	if (!devsw->d_ioctl) {
2039232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2040232604Strasz			 "%s: no d_ioctl for device %s!", __func__,
2041232604Strasz			 be_lun->dev_path);
2042232604Strasz		return (ENODEV);
2043232604Strasz	}
2044232604Strasz
2045232604Strasz	error = devsw->d_ioctl(dev, DIOCGMEDIASIZE,
2046232604Strasz			       (caddr_t)&size_bytes, FREAD,
2047232604Strasz			       curthread);
2048232604Strasz	if (error) {
2049232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2050232604Strasz			 "%s: error %d returned for DIOCGMEDIASIZE ioctl "
2051232604Strasz			 "on %s!", __func__, error, be_lun->dev_path);
2052232604Strasz		return (error);
2053232604Strasz	}
2054232604Strasz
2055232604Strasz	if (params->lun_size_bytes != 0) {
2056232604Strasz		if (params->lun_size_bytes > size_bytes) {
2057232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2058232604Strasz				 "%s: requested LUN size %ju > backing device "
2059232604Strasz				 "size %ju", __func__,
2060232604Strasz				 (uintmax_t)params->lun_size_bytes,
2061232604Strasz				 (uintmax_t)size_bytes);
2062232604Strasz			return (EINVAL);
2063232604Strasz		}
2064232604Strasz
2065232604Strasz		be_lun->size_bytes = params->lun_size_bytes;
2066232604Strasz	} else {
2067232604Strasz		be_lun->size_bytes = size_bytes;
2068232604Strasz	}
2069232604Strasz
2070232604Strasz	return (0);
2071232604Strasz}
2072232604Strasz
2073232604Straszstatic int
2074232604Straszctl_be_block_modify(struct ctl_be_block_softc *softc, struct ctl_lun_req *req)
2075232604Strasz{
2076232604Strasz	struct ctl_lun_modify_params *params;
2077232604Strasz	struct ctl_be_block_lun *be_lun;
2078241896Skib	int error;
2079232604Strasz
2080232604Strasz	params = &req->reqdata.modify;
2081232604Strasz
2082232604Strasz	mtx_lock(&softc->lock);
2083232604Strasz
2084232604Strasz	be_lun = NULL;
2085232604Strasz
2086232604Strasz	STAILQ_FOREACH(be_lun, &softc->lun_list, links) {
2087232604Strasz		if (be_lun->ctl_be_lun.lun_id == params->lun_id)
2088232604Strasz			break;
2089232604Strasz	}
2090232604Strasz	mtx_unlock(&softc->lock);
2091232604Strasz
2092232604Strasz	if (be_lun == NULL) {
2093232604Strasz		snprintf(req->error_str, sizeof(req->error_str),
2094232604Strasz			 "%s: LUN %u is not managed by the block backend",
2095232604Strasz			 __func__, params->lun_id);
2096232604Strasz		goto bailout_error;
2097232604Strasz	}
2098232604Strasz
2099232604Strasz	if (params->lun_size_bytes != 0) {
2100232604Strasz		if (params->lun_size_bytes < be_lun->blocksize) {
2101232604Strasz			snprintf(req->error_str, sizeof(req->error_str),
2102232604Strasz				"%s: LUN size %ju < blocksize %u", __func__,
2103232604Strasz				params->lun_size_bytes, be_lun->blocksize);
2104232604Strasz			goto bailout_error;
2105232604Strasz		}
2106232604Strasz	}
2107232604Strasz
2108232604Strasz	vn_lock(be_lun->vn, LK_SHARED | LK_RETRY);
2109232604Strasz
2110232604Strasz	if (be_lun->vn->v_type == VREG)
2111232604Strasz		error = ctl_be_block_modify_file(be_lun, req);
2112232604Strasz	else
2113232604Strasz		error = ctl_be_block_modify_dev(be_lun, req);
2114232604Strasz
2115232604Strasz	VOP_UNLOCK(be_lun->vn, 0);
2116232604Strasz
2117232604Strasz	if (error != 0)
2118232604Strasz		goto bailout_error;
2119232604Strasz
2120232604Strasz	be_lun->size_blocks = be_lun->size_bytes >> be_lun->blocksize_shift;
2121232604Strasz
2122232604Strasz	/*
2123232604Strasz	 * The maximum LBA is the size - 1.
2124232604Strasz	 *
2125232604Strasz	 * XXX: Note that this field is being updated without locking,
2126232604Strasz	 * 	which might cause problems on 32-bit architectures.
2127232604Strasz	 */
2128232604Strasz	be_lun->ctl_be_lun.maxlba = be_lun->size_blocks - 1;
2129232604Strasz	ctl_lun_capacity_changed(&be_lun->ctl_be_lun);
2130232604Strasz
2131232604Strasz	/* Tell the user the exact size we ended up using */
2132232604Strasz	params->lun_size_bytes = be_lun->size_bytes;
2133232604Strasz
2134232604Strasz	req->status = CTL_LUN_OK;
2135232604Strasz
2136232604Strasz	return (0);
2137232604Strasz
2138232604Straszbailout_error:
2139232604Strasz	req->status = CTL_LUN_ERROR;
2140232604Strasz
2141232604Strasz	return (0);
2142232604Strasz}
2143232604Strasz
2144229997Skenstatic void
2145229997Skenctl_be_block_lun_shutdown(void *be_lun)
2146229997Sken{
2147229997Sken	struct ctl_be_block_lun *lun;
2148229997Sken	struct ctl_be_block_softc *softc;
2149229997Sken
2150229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2151229997Sken
2152229997Sken	softc = lun->softc;
2153229997Sken
2154229997Sken	mtx_lock(&softc->lock);
2155229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_UNCONFIGURED;
2156229997Sken	if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2157229997Sken		wakeup(lun);
2158229997Sken	mtx_unlock(&softc->lock);
2159229997Sken
2160229997Sken}
2161229997Sken
2162229997Skenstatic void
2163229997Skenctl_be_block_lun_config_status(void *be_lun, ctl_lun_config_status status)
2164229997Sken{
2165229997Sken	struct ctl_be_block_lun *lun;
2166229997Sken	struct ctl_be_block_softc *softc;
2167229997Sken
2168229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2169229997Sken	softc = lun->softc;
2170229997Sken
2171229997Sken	if (status == CTL_LUN_CONFIG_OK) {
2172229997Sken		mtx_lock(&softc->lock);
2173229997Sken		lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2174229997Sken		if (lun->flags & CTL_BE_BLOCK_LUN_WAITING)
2175229997Sken			wakeup(lun);
2176229997Sken		mtx_unlock(&softc->lock);
2177229997Sken
2178229997Sken		/*
2179229997Sken		 * We successfully added the LUN, attempt to enable it.
2180229997Sken		 */
2181229997Sken		if (ctl_enable_lun(&lun->ctl_be_lun) != 0) {
2182229997Sken			printf("%s: ctl_enable_lun() failed!\n", __func__);
2183229997Sken			if (ctl_invalidate_lun(&lun->ctl_be_lun) != 0) {
2184229997Sken				printf("%s: ctl_invalidate_lun() failed!\n",
2185229997Sken				       __func__);
2186229997Sken			}
2187229997Sken		}
2188229997Sken
2189229997Sken		return;
2190229997Sken	}
2191229997Sken
2192229997Sken
2193229997Sken	mtx_lock(&softc->lock);
2194229997Sken	lun->flags &= ~CTL_BE_BLOCK_LUN_UNCONFIGURED;
2195229997Sken	lun->flags |= CTL_BE_BLOCK_LUN_CONFIG_ERR;
2196229997Sken	wakeup(lun);
2197229997Sken	mtx_unlock(&softc->lock);
2198229997Sken}
2199229997Sken
2200229997Sken
2201229997Skenstatic int
2202229997Skenctl_be_block_config_write(union ctl_io *io)
2203229997Sken{
2204229997Sken	struct ctl_be_block_lun *be_lun;
2205229997Sken	struct ctl_be_lun *ctl_be_lun;
2206229997Sken	int retval;
2207229997Sken
2208229997Sken	retval = 0;
2209229997Sken
2210229997Sken	DPRINTF("entered\n");
2211229997Sken
2212229997Sken	ctl_be_lun = (struct ctl_be_lun *)io->io_hdr.ctl_private[
2213229997Sken		CTL_PRIV_BACKEND_LUN].ptr;
2214229997Sken	be_lun = (struct ctl_be_block_lun *)ctl_be_lun->be_lun;
2215229997Sken
2216229997Sken	switch (io->scsiio.cdb[0]) {
2217229997Sken	case SYNCHRONIZE_CACHE:
2218229997Sken	case SYNCHRONIZE_CACHE_16:
2219229997Sken		/*
2220229997Sken		 * The upper level CTL code will filter out any CDBs with
2221229997Sken		 * the immediate bit set and return the proper error.
2222229997Sken		 *
2223229997Sken		 * We don't really need to worry about what LBA range the
2224229997Sken		 * user asked to be synced out.  When they issue a sync
2225229997Sken		 * cache command, we'll sync out the whole thing.
2226229997Sken		 */
2227229997Sken		mtx_lock(&be_lun->lock);
2228229997Sken		STAILQ_INSERT_TAIL(&be_lun->config_write_queue, &io->io_hdr,
2229229997Sken				   links);
2230229997Sken		mtx_unlock(&be_lun->lock);
2231229997Sken		taskqueue_enqueue(be_lun->io_taskqueue, &be_lun->io_task);
2232229997Sken		break;
2233229997Sken	case START_STOP_UNIT: {
2234229997Sken		struct scsi_start_stop_unit *cdb;
2235229997Sken
2236229997Sken		cdb = (struct scsi_start_stop_unit *)io->scsiio.cdb;
2237229997Sken
2238229997Sken		if (cdb->how & SSS_START)
2239229997Sken			retval = ctl_start_lun(ctl_be_lun);
2240229997Sken		else {
2241229997Sken			retval = ctl_stop_lun(ctl_be_lun);
2242229997Sken			/*
2243229997Sken			 * XXX KDM Copan-specific offline behavior.
2244229997Sken			 * Figure out a reasonable way to port this?
2245229997Sken			 */
2246229997Sken#ifdef NEEDTOPORT
2247229997Sken			if ((retval == 0)
2248229997Sken			 && (cdb->byte2 & SSS_ONOFFLINE))
2249229997Sken				retval = ctl_lun_offline(ctl_be_lun);
2250229997Sken#endif
2251229997Sken		}
2252229997Sken
2253229997Sken		/*
2254229997Sken		 * In general, the above routines should not fail.  They
2255229997Sken		 * just set state for the LUN.  So we've got something
2256229997Sken		 * pretty wrong here if we can't start or stop the LUN.
2257229997Sken		 */
2258229997Sken		if (retval != 0) {
2259229997Sken			ctl_set_internal_failure(&io->scsiio,
2260229997Sken						 /*sks_valid*/ 1,
2261229997Sken						 /*retry_count*/ 0xf051);
2262229997Sken			retval = CTL_RETVAL_COMPLETE;
2263229997Sken		} else {
2264229997Sken			ctl_set_success(&io->scsiio);
2265229997Sken		}
2266229997Sken		ctl_config_write_done(io);
2267229997Sken		break;
2268229997Sken	}
2269229997Sken	default:
2270229997Sken		ctl_set_invalid_opcode(&io->scsiio);
2271229997Sken		ctl_config_write_done(io);
2272229997Sken		retval = CTL_RETVAL_COMPLETE;
2273229997Sken		break;
2274229997Sken	}
2275229997Sken
2276229997Sken	return (retval);
2277229997Sken
2278229997Sken}
2279229997Sken
2280229997Skenstatic int
2281229997Skenctl_be_block_config_read(union ctl_io *io)
2282229997Sken{
2283229997Sken	return (0);
2284229997Sken}
2285229997Sken
2286229997Skenstatic int
2287229997Skenctl_be_block_lun_info(void *be_lun, struct sbuf *sb)
2288229997Sken{
2289229997Sken	struct ctl_be_block_lun *lun;
2290229997Sken	int retval;
2291229997Sken
2292229997Sken	lun = (struct ctl_be_block_lun *)be_lun;
2293229997Sken	retval = 0;
2294229997Sken
2295229997Sken	retval = sbuf_printf(sb, "<num_threads>");
2296229997Sken
2297229997Sken	if (retval != 0)
2298229997Sken		goto bailout;
2299229997Sken
2300229997Sken	retval = sbuf_printf(sb, "%d", lun->num_threads);
2301229997Sken
2302229997Sken	if (retval != 0)
2303229997Sken		goto bailout;
2304229997Sken
2305229997Sken	retval = sbuf_printf(sb, "</num_threads>");
2306229997Sken
2307229997Sken	/*
2308229997Sken	 * For processor devices, we don't have a path variable.
2309229997Sken	 */
2310229997Sken	if ((retval != 0)
2311229997Sken	 || (lun->dev_path == NULL))
2312229997Sken		goto bailout;
2313229997Sken
2314229997Sken	retval = sbuf_printf(sb, "<file>");
2315229997Sken
2316229997Sken	if (retval != 0)
2317229997Sken		goto bailout;
2318229997Sken
2319229997Sken	retval = ctl_sbuf_printf_esc(sb, lun->dev_path);
2320229997Sken
2321229997Sken	if (retval != 0)
2322229997Sken		goto bailout;
2323229997Sken
2324229997Sken	retval = sbuf_printf(sb, "</file>\n");
2325229997Sken
2326229997Skenbailout:
2327229997Sken
2328229997Sken	return (retval);
2329229997Sken}
2330229997Sken
2331229997Skenint
2332229997Skenctl_be_block_init(void)
2333229997Sken{
2334229997Sken	struct ctl_be_block_softc *softc;
2335229997Sken	int retval;
2336229997Sken
2337229997Sken	softc = &backend_block_softc;
2338229997Sken	retval = 0;
2339229997Sken
2340229997Sken	mtx_init(&softc->lock, "ctlblk", NULL, MTX_DEF);
2341229997Sken	STAILQ_INIT(&softc->beio_free_queue);
2342229997Sken	STAILQ_INIT(&softc->disk_list);
2343229997Sken	STAILQ_INIT(&softc->lun_list);
2344229997Sken	ctl_grow_beio(softc, 200);
2345229997Sken
2346229997Sken	return (retval);
2347229997Sken}
2348