xref: /titanic_50/usr/src/uts/sun4v/io/vds.c (revision 15d9d0b528387242011cdcc6190c9e598cfe3a07)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * Virtual disk server
31  */
32 
33 
34 #include <sys/types.h>
35 #include <sys/conf.h>
36 #include <sys/crc32.h>
37 #include <sys/ddi.h>
38 #include <sys/dkio.h>
39 #include <sys/file.h>
40 #include <sys/fs/hsfs_isospec.h>
41 #include <sys/mdeg.h>
42 #include <sys/mhd.h>
43 #include <sys/modhash.h>
44 #include <sys/note.h>
45 #include <sys/pathname.h>
46 #include <sys/sdt.h>
47 #include <sys/sunddi.h>
48 #include <sys/sunldi.h>
49 #include <sys/sysmacros.h>
50 #include <sys/vio_common.h>
51 #include <sys/vio_util.h>
52 #include <sys/vdsk_mailbox.h>
53 #include <sys/vdsk_common.h>
54 #include <sys/vtoc.h>
55 #include <sys/vfs.h>
56 #include <sys/stat.h>
57 #include <sys/scsi/impl/uscsi.h>
58 #include <vm/seg_map.h>
59 
60 /* Virtual disk server initialization flags */
61 #define	VDS_LDI			0x01
62 #define	VDS_MDEG		0x02
63 
64 /* Virtual disk server tunable parameters */
65 #define	VDS_RETRIES		5
66 #define	VDS_LDC_DELAY		1000 /* 1 msecs */
67 #define	VDS_DEV_DELAY		10000000 /* 10 secs */
68 #define	VDS_NCHAINS		32
69 
70 /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
71 #define	VDS_NAME		"virtual-disk-server"
72 
73 #define	VD_NAME			"vd"
74 #define	VD_VOLUME_NAME		"vdisk"
75 #define	VD_ASCIILABEL		"Virtual Disk"
76 
77 #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
78 #define	VD_ID_PROP		"id"
79 #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
80 #define	VD_BLOCK_DEVICE_OPTS	"vds-block-device-opts"
81 #define	VD_REG_PROP		"reg"
82 
83 /* Virtual disk initialization flags */
84 #define	VD_DISK_READY		0x01
85 #define	VD_LOCKING		0x02
86 #define	VD_LDC			0x04
87 #define	VD_DRING		0x08
88 #define	VD_SID			0x10
89 #define	VD_SEQ_NUM		0x20
90 #define	VD_SETUP_ERROR		0x40
91 
92 /* Flags for writing to a vdisk which is a file */
93 #define	VD_FILE_WRITE_FLAGS	SM_ASYNC
94 
95 /* Number of backup labels */
96 #define	VD_FILE_NUM_BACKUP	5
97 
98 /* Timeout for SCSI I/O */
99 #define	VD_SCSI_RDWR_TIMEOUT	30	/* 30 secs */
100 
101 /* Maximum number of logical partitions */
102 #define	VD_MAXPART	(NDKMAP + 1)
103 
104 /*
105  * By Solaris convention, slice/partition 2 represents the entire disk;
106  * unfortunately, this convention does not appear to be codified.
107  */
108 #define	VD_ENTIRE_DISK_SLICE	2
109 
110 /* Return a cpp token as a string */
111 #define	STRINGIZE(token)	#token
112 
113 /*
114  * Print a message prefixed with the current function name to the message log
115  * (and optionally to the console for verbose boots); these macros use cpp's
116  * concatenation of string literals and C99 variable-length-argument-list
117  * macros
118  */
119 #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
120 #define	_PRN(format, ...)					\
121 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
122 
123 /* Return a pointer to the "i"th vdisk dring element */
124 #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
125 	    (vd->dring + (i)*vd->descriptor_size))
126 
127 /* Return the virtual disk client's type as a string (for use in messages) */
128 #define	VD_CLIENT(vd)							\
129 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
130 	    (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" :    \
131 		(((vd)->xfer_mode == 0) ? "null client" :		\
132 		    "unsupported client")))
133 
134 /* Read disk label from a disk on file */
135 #define	VD_FILE_LABEL_READ(vd, labelp) \
136 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
137 	    0, sizeof (struct dk_label))
138 
139 /* Write disk label to a disk on file */
140 #define	VD_FILE_LABEL_WRITE(vd, labelp)	\
141 	vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
142 	    0, sizeof (struct dk_label))
143 
144 /* Message for disk access rights reset failure */
145 #define	VD_RESET_ACCESS_FAILURE_MSG \
146 	"Fail to reset disk access rights for disk %s"
147 
148 /*
149  * Specification of an MD node passed to the MDEG to filter any
150  * 'vport' nodes that do not belong to the specified node. This
151  * template is copied for each vds instance and filled in with
152  * the appropriate 'cfg-handle' value before being passed to the MDEG.
153  */
154 static mdeg_prop_spec_t	vds_prop_template[] = {
155 	{ MDET_PROP_STR,	"name",		VDS_NAME },
156 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
157 	{ MDET_LIST_END,	NULL, 		NULL }
158 };
159 
160 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
161 
162 /*
163  * Matching criteria passed to the MDEG to register interest
164  * in changes to 'virtual-device-port' nodes identified by their
165  * 'id' property.
166  */
167 static md_prop_match_t	vd_prop_match[] = {
168 	{ MDET_PROP_VAL,	VD_ID_PROP },
169 	{ MDET_LIST_END,	NULL }
170 };
171 
172 static mdeg_node_match_t vd_match = {"virtual-device-port",
173 				    vd_prop_match};
174 
175 /*
176  * Options for the VD_BLOCK_DEVICE_OPTS property.
177  */
178 #define	VD_OPT_RDONLY		0x1	/* read-only  */
179 #define	VD_OPT_SLICE		0x2	/* single slice */
180 #define	VD_OPT_EXCLUSIVE	0x4	/* exclusive access */
181 
182 #define	VD_OPTION_NLEN	128
183 
184 typedef struct vd_option {
185 	char vdo_name[VD_OPTION_NLEN];
186 	uint64_t vdo_value;
187 } vd_option_t;
188 
189 vd_option_t vd_bdev_options[] = {
190 	{ "ro",		VD_OPT_RDONLY },
191 	{ "slice", 	VD_OPT_SLICE },
192 	{ "excl",	VD_OPT_EXCLUSIVE }
193 };
194 
195 /* Debugging macros */
196 #ifdef DEBUG
197 
198 static int	vd_msglevel = 0;
199 
200 #define	PR0 if (vd_msglevel > 0)	PRN
201 #define	PR1 if (vd_msglevel > 1)	PRN
202 #define	PR2 if (vd_msglevel > 2)	PRN
203 
204 #define	VD_DUMP_DRING_ELEM(elem)					\
205 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
206 	    elem->hdr.dstate,						\
207 	    elem->payload.operation,					\
208 	    elem->payload.status,					\
209 	    elem->payload.nbytes,					\
210 	    elem->payload.addr,						\
211 	    elem->payload.ncookies);
212 
213 char *
214 vd_decode_state(int state)
215 {
216 	char *str;
217 
218 #define	CASE_STATE(_s)	case _s: str = #_s; break;
219 
220 	switch (state) {
221 	CASE_STATE(VD_STATE_INIT)
222 	CASE_STATE(VD_STATE_VER)
223 	CASE_STATE(VD_STATE_ATTR)
224 	CASE_STATE(VD_STATE_DRING)
225 	CASE_STATE(VD_STATE_RDX)
226 	CASE_STATE(VD_STATE_DATA)
227 	default: str = "unknown"; break;
228 	}
229 
230 #undef CASE_STATE
231 
232 	return (str);
233 }
234 
235 void
236 vd_decode_tag(vio_msg_t *msg)
237 {
238 	char *tstr, *sstr, *estr;
239 
240 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
241 
242 	switch (msg->tag.vio_msgtype) {
243 	CASE_TYPE(VIO_TYPE_CTRL)
244 	CASE_TYPE(VIO_TYPE_DATA)
245 	CASE_TYPE(VIO_TYPE_ERR)
246 	default: tstr = "unknown"; break;
247 	}
248 
249 #undef CASE_TYPE
250 
251 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
252 
253 	switch (msg->tag.vio_subtype) {
254 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
255 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
256 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
257 	default: sstr = "unknown"; break;
258 	}
259 
260 #undef CASE_SUBTYPE
261 
262 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
263 
264 	switch (msg->tag.vio_subtype_env) {
265 	CASE_ENV(VIO_VER_INFO)
266 	CASE_ENV(VIO_ATTR_INFO)
267 	CASE_ENV(VIO_DRING_REG)
268 	CASE_ENV(VIO_DRING_UNREG)
269 	CASE_ENV(VIO_RDX)
270 	CASE_ENV(VIO_PKT_DATA)
271 	CASE_ENV(VIO_DESC_DATA)
272 	CASE_ENV(VIO_DRING_DATA)
273 	default: estr = "unknown"; break;
274 	}
275 
276 #undef CASE_ENV
277 
278 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
279 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
280 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
281 }
282 
283 #else	/* !DEBUG */
284 
285 #define	PR0(...)
286 #define	PR1(...)
287 #define	PR2(...)
288 
289 #define	VD_DUMP_DRING_ELEM(elem)
290 
291 #define	vd_decode_state(_s)	(NULL)
292 #define	vd_decode_tag(_s)	(NULL)
293 
294 #endif	/* DEBUG */
295 
296 
297 /*
298  * Soft state structure for a vds instance
299  */
300 typedef struct vds {
301 	uint_t		initialized;	/* driver inst initialization flags */
302 	dev_info_t	*dip;		/* driver inst devinfo pointer */
303 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
304 	mod_hash_t	*vd_table;	/* table of virtual disks served */
305 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
306 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
307 } vds_t;
308 
309 /*
310  * Types of descriptor-processing tasks
311  */
312 typedef enum vd_task_type {
313 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
314 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
315 } vd_task_type_t;
316 
317 /*
318  * Structure describing the task for processing a descriptor
319  */
320 typedef struct vd_task {
321 	struct vd		*vd;		/* vd instance task is for */
322 	vd_task_type_t		type;		/* type of descriptor task */
323 	int			index;		/* dring elem index for task */
324 	vio_msg_t		*msg;		/* VIO message task is for */
325 	size_t			msglen;		/* length of message content */
326 	vd_dring_payload_t	*request;	/* request task will perform */
327 	struct buf		buf;		/* buf(9s) for I/O request */
328 	ldc_mem_handle_t	mhdl;		/* task memory handle */
329 	int			status;		/* status of processing task */
330 	int	(*completef)(struct vd_task *task); /* completion func ptr */
331 } vd_task_t;
332 
333 /*
334  * Soft state structure for a virtual disk instance
335  */
336 typedef struct vd {
337 	uint_t			initialized;	/* vdisk initialization flags */
338 	uint64_t		operations;	/* bitmask of VD_OPs exported */
339 	vio_ver_t		version;	/* ver negotiated with client */
340 	vds_t			*vds;		/* server for this vdisk */
341 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
342 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
343 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
344 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
345 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
346 	int			open_flags;	/* open flags */
347 	uint_t			nslices;	/* number of slices */
348 	size_t			vdisk_size;	/* number of blocks in vdisk */
349 	size_t			vdisk_block_size; /* size of each vdisk block */
350 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
351 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
352 	vd_media_t		vdisk_media;	/* media type of backing dev. */
353 	boolean_t		is_atapi_dev;	/* Is this an IDE CD-ROM dev? */
354 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
355 	size_t			block_size;	/* blk size of actual device */
356 	boolean_t		pseudo;		/* underlying pseudo dev */
357 	boolean_t		file;		/* is vDisk backed by a file? */
358 	boolean_t		scsi;		/* is vDisk backed by scsi? */
359 	vnode_t			*file_vnode;	/* file vnode */
360 	size_t			file_size;	/* file size */
361 	ddi_devid_t		file_devid;	/* devid for disk image */
362 	efi_gpt_t		efi_gpt;	/* EFI GPT for slice type */
363 	efi_gpe_t		efi_gpe;	/* EFI GPE for slice type */
364 	int			efi_reserved;	/* EFI reserved slice */
365 	struct dk_geom		dk_geom;	/* synthetic for slice type */
366 	struct vtoc		vtoc;		/* synthetic for slice type */
367 	vd_slice_t		slices[VD_MAXPART]; /* logical partitions */
368 	boolean_t		ownership;	/* disk ownership status */
369 	ldc_status_t		ldc_state;	/* LDC connection state */
370 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
371 	size_t			max_msglen;	/* largest LDC message len */
372 	vd_state_t		state;		/* client handshake state */
373 	uint8_t			xfer_mode;	/* transfer mode with client */
374 	uint32_t		sid;		/* client's session ID */
375 	uint64_t		seq_num;	/* message sequence number */
376 	uint64_t		dring_ident;	/* identifier of dring */
377 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
378 	uint32_t		descriptor_size;	/* num bytes in desc */
379 	uint32_t		dring_len;	/* number of dring elements */
380 	caddr_t			dring;		/* address of dring */
381 	caddr_t			vio_msgp;	/* vio msg staging buffer */
382 	vd_task_t		inband_task;	/* task for inband descriptor */
383 	vd_task_t		*dring_task;	/* tasks dring elements */
384 
385 	kmutex_t		lock;		/* protects variables below */
386 	boolean_t		enabled;	/* is vdisk enabled? */
387 	boolean_t		reset_state;	/* reset connection state? */
388 	boolean_t		reset_ldc;	/* reset LDC channel? */
389 } vd_t;
390 
391 typedef struct vds_operation {
392 	char	*namep;
393 	uint8_t	operation;
394 	int	(*start)(vd_task_t *task);
395 	int	(*complete)(vd_task_t *task);
396 } vds_operation_t;
397 
398 typedef struct vd_ioctl {
399 	uint8_t		operation;		/* vdisk operation */
400 	const char	*operation_name;	/* vdisk operation name */
401 	size_t		nbytes;			/* size of operation buffer */
402 	int		cmd;			/* corresponding ioctl cmd */
403 	const char	*cmd_name;		/* ioctl cmd name */
404 	void		*arg;			/* ioctl cmd argument */
405 	/* convert input vd_buf to output ioctl_arg */
406 	int		(*copyin)(void *vd_buf, size_t, void *ioctl_arg);
407 	/* convert input ioctl_arg to output vd_buf */
408 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
409 	/* write is true if the operation writes any data to the backend */
410 	boolean_t	write;
411 } vd_ioctl_t;
412 
413 /* Define trivial copyin/copyout conversion function flag */
414 #define	VD_IDENTITY_IN	((int (*)(void *, size_t, void *))-1)
415 #define	VD_IDENTITY_OUT	((void (*)(void *, void *))-1)
416 
417 
418 static int	vds_ldc_retries = VDS_RETRIES;
419 static int	vds_ldc_delay = VDS_LDC_DELAY;
420 static int	vds_dev_retries = VDS_RETRIES;
421 static int	vds_dev_delay = VDS_DEV_DELAY;
422 static void	*vds_state;
423 
424 static uint_t	vd_file_write_flags = VD_FILE_WRITE_FLAGS;
425 
426 static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
427 static int	vd_scsi_debug = USCSI_SILENT;
428 
429 /*
430  * Tunable to define the behavior of the service domain if the vdisk server
431  * fails to reset disk exclusive access when a LDC channel is reset. When a
432  * LDC channel is reset the vdisk server will try to reset disk exclusive
433  * access by releasing any SCSI-2 reservation or resetting the disk. If these
434  * actions fail then the default behavior (vd_reset_access_failure = 0) is to
435  * print a warning message. This default behavior can be changed by setting
436  * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
437  * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
438  * the service domain to panic. In both cases, the reset of the service domain
439  * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
440  */
441 static int 	vd_reset_access_failure = 0;
442 
443 /*
444  * Tunable for backward compatibility. When this variable is set to B_TRUE,
445  * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
446  * slice disks whether or not they have the "slice" option set. This is
447  * to provide a simple backward compatibility mechanism when upgrading
448  * the vds driver and using a domain configuration created before the
449  * "slice" option was available.
450  */
451 static boolean_t vd_volume_force_slice = B_FALSE;
452 
453 /*
454  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
455  *
456  * Each supported major version should appear only once, paired with (and only
457  * with) its highest supported minor version number (as the protocol requires
458  * supporting all lower minor version numbers as well)
459  */
460 static const vio_ver_t	vds_version[] = {{1, 1}};
461 static const size_t	vds_num_versions =
462     sizeof (vds_version)/sizeof (vds_version[0]);
463 
464 static void vd_free_dring_task(vd_t *vdp);
465 static int vd_setup_vd(vd_t *vd);
466 static int vd_setup_single_slice_disk(vd_t *vd);
467 static int vd_setup_mediainfo(vd_t *vd);
468 static boolean_t vd_enabled(vd_t *vd);
469 static ushort_t vd_lbl2cksum(struct dk_label *label);
470 static int vd_file_validate_geometry(vd_t *vd);
471 static boolean_t vd_file_is_iso_image(vd_t *vd);
472 static void vd_set_exported_operations(vd_t *vd);
473 static void vd_reset_access(vd_t *vd);
474 static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg);
475 static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **);
476 static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *);
477 
478 /*
479  * Function:
480  *	vd_file_rw
481  *
482  * Description:
483  * 	Read or write to a disk on file.
484  *
485  * Parameters:
486  *	vd		- disk on which the operation is performed.
487  *	slice		- slice on which the operation is performed,
488  *			  VD_SLICE_NONE indicates that the operation
489  *			  is done using an absolute disk offset.
490  *	operation	- operation to execute: read (VD_OP_BREAD) or
491  *			  write (VD_OP_BWRITE).
492  *	data		- buffer where data are read to or written from.
493  *	blk		- starting block for the operation.
494  *	len		- number of bytes to read or write.
495  *
496  * Return Code:
497  *	n >= 0		- success, n indicates the number of bytes read
498  *			  or written.
499  *	-1		- error.
500  */
501 static ssize_t
502 vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk,
503     size_t len)
504 {
505 	caddr_t	maddr;
506 	size_t offset, maxlen, moffset, mlen, n;
507 	uint_t smflags;
508 	enum seg_rw srw;
509 
510 	ASSERT(vd->file);
511 	ASSERT(len > 0);
512 
513 	/*
514 	 * If a file is exported as a slice then we don't care about the vtoc.
515 	 * In that case, the vtoc is a fake mainly to make newfs happy and we
516 	 * handle any I/O as a raw disk access so that we can have access to the
517 	 * entire backend.
518 	 */
519 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
520 		/* raw disk access */
521 		offset = blk * DEV_BSIZE;
522 	} else {
523 		ASSERT(slice >= 0 && slice < V_NUMPAR);
524 
525 		/*
526 		 * v1.0 vDisk clients depended on the server not verifying
527 		 * the label of a unformatted disk.  This "feature" is
528 		 * maintained for backward compatibility but all versions
529 		 * from v1.1 onwards must do the right thing.
530 		 */
531 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
532 		    vio_ver_is_supported(vd->version, 1, 1)) {
533 			(void) vd_file_validate_geometry(vd);
534 			if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
535 				PR0("Unknown disk label, can't do I/O "
536 				    "from slice %d", slice);
537 				return (-1);
538 			}
539 		}
540 
541 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
542 			ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE);
543 		} else {
544 			ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI);
545 			ASSERT(vd->vdisk_block_size == DEV_BSIZE);
546 		}
547 
548 		if (blk >= vd->slices[slice].nblocks) {
549 			/* address past the end of the slice */
550 			PR0("req_addr (0x%lx) > psize (0x%lx)",
551 			    blk, vd->slices[slice].nblocks);
552 			return (0);
553 		}
554 
555 		offset = (vd->slices[slice].start + blk) * DEV_BSIZE;
556 
557 		/*
558 		 * If the requested size is greater than the size
559 		 * of the partition, truncate the read/write.
560 		 */
561 		maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE;
562 
563 		if (len > maxlen) {
564 			PR0("I/O size truncated to %lu bytes from %lu bytes",
565 			    maxlen, len);
566 			len = maxlen;
567 		}
568 	}
569 
570 	/*
571 	 * We have to ensure that we are reading/writing into the mmap
572 	 * range. If we have a partial disk image (e.g. an image of
573 	 * s0 instead s2) the system can try to access slices that
574 	 * are not included into the disk image.
575 	 */
576 	if ((offset + len) > vd->file_size) {
577 		PR0("offset + nbytes (0x%lx + 0x%lx) > "
578 		    "file_size (0x%lx)", offset, len, vd->file_size);
579 		return (-1);
580 	}
581 
582 	srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE;
583 	smflags = (operation == VD_OP_BREAD)? 0 :
584 	    (SM_WRITE | vd_file_write_flags);
585 	n = len;
586 
587 	do {
588 		/*
589 		 * segmap_getmapflt() returns a MAXBSIZE chunk which is
590 		 * MAXBSIZE aligned.
591 		 */
592 		moffset = offset & MAXBOFFSET;
593 		mlen = MIN(MAXBSIZE - moffset, n);
594 		maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset,
595 		    mlen, 1, srw);
596 		/*
597 		 * Fault in the pages so we can check for error and ensure
598 		 * that we can safely used the mapped address.
599 		 */
600 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
601 		    F_SOFTLOCK, srw) != 0) {
602 			(void) segmap_release(segkmap, maddr, 0);
603 			return (-1);
604 		}
605 
606 		if (operation == VD_OP_BREAD)
607 			bcopy(maddr + moffset, data, mlen);
608 		else
609 			bcopy(data, maddr + moffset, mlen);
610 
611 		if (segmap_fault(kas.a_hat, segkmap, maddr, mlen,
612 		    F_SOFTUNLOCK, srw) != 0) {
613 			(void) segmap_release(segkmap, maddr, 0);
614 			return (-1);
615 		}
616 		if (segmap_release(segkmap, maddr, smflags) != 0)
617 			return (-1);
618 		n -= mlen;
619 		offset += mlen;
620 		data += mlen;
621 
622 	} while (n > 0);
623 
624 	return (len);
625 }
626 
627 /*
628  * Function:
629  *	vd_file_build_default_label
630  *
631  * Description:
632  *	Return a default label for the given disk. This is used when the disk
633  *	does not have a valid VTOC so that the user can get a valid default
634  *	configuration. The default label has all slice sizes set to 0 (except
635  *	slice 2 which is the entire disk) to force the user to write a valid
636  *	label onto the disk image.
637  *
638  * Parameters:
639  *	vd		- disk on which the operation is performed.
640  *	label		- the returned default label.
641  *
642  * Return Code:
643  *	none.
644  */
645 static void
646 vd_file_build_default_label(vd_t *vd, struct dk_label *label)
647 {
648 	size_t size;
649 	char prefix;
650 
651 	ASSERT(vd->file);
652 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
653 
654 	bzero(label, sizeof (struct dk_label));
655 
656 	/*
657 	 * We must have a resonable number of cylinders and sectors so
658 	 * that newfs can run using default values.
659 	 *
660 	 * if (disk_size < 2MB)
661 	 * 	phys_cylinders = disk_size / 100K
662 	 * else
663 	 * 	phys_cylinders = disk_size / 300K
664 	 *
665 	 * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders
666 	 * alt_cylinders = (phys_cylinders > 2) ? 2 : 0;
667 	 * data_cylinders = phys_cylinders - alt_cylinders
668 	 *
669 	 * sectors = disk_size / (phys_cylinders * blk_size)
670 	 *
671 	 * The file size test is an attempt to not have too few cylinders
672 	 * for a small file, or so many on a big file that you waste space
673 	 * for backup superblocks or cylinder group structures.
674 	 */
675 	if (vd->file_size < (2 * 1024 * 1024))
676 		label->dkl_pcyl = vd->file_size / (100 * 1024);
677 	else
678 		label->dkl_pcyl = vd->file_size / (300 * 1024);
679 
680 	if (label->dkl_pcyl == 0)
681 		label->dkl_pcyl = 1;
682 
683 	label->dkl_acyl = 0;
684 
685 	if (label->dkl_pcyl > 2)
686 		label->dkl_acyl = 2;
687 
688 	label->dkl_nsect = vd->file_size /
689 	    (DEV_BSIZE * label->dkl_pcyl);
690 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
691 	label->dkl_nhead = 1;
692 	label->dkl_write_reinstruct = 0;
693 	label->dkl_read_reinstruct = 0;
694 	label->dkl_rpm = 7200;
695 	label->dkl_apc = 0;
696 	label->dkl_intrlv = 0;
697 
698 	PR0("requested disk size: %ld bytes\n", vd->file_size);
699 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
700 	    label->dkl_nhead, label->dkl_nsect);
701 	PR0("provided disk size: %ld bytes\n", (uint64_t)
702 	    (label->dkl_pcyl * label->dkl_nhead *
703 	    label->dkl_nsect * DEV_BSIZE));
704 
705 	if (vd->file_size < (1ULL << 20)) {
706 		size = vd->file_size >> 10;
707 		prefix = 'K'; /* Kilobyte */
708 	} else if (vd->file_size < (1ULL << 30)) {
709 		size = vd->file_size >> 20;
710 		prefix = 'M'; /* Megabyte */
711 	} else if (vd->file_size < (1ULL << 40)) {
712 		size = vd->file_size >> 30;
713 		prefix = 'G'; /* Gigabyte */
714 	} else {
715 		size = vd->file_size >> 40;
716 		prefix = 'T'; /* Terabyte */
717 	}
718 
719 	/*
720 	 * We must have a correct label name otherwise format(1m) will
721 	 * not recognized the disk as labeled.
722 	 */
723 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
724 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
725 	    size, prefix,
726 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
727 	    label->dkl_nsect);
728 
729 	/* default VTOC */
730 	label->dkl_vtoc.v_version = V_VERSION;
731 	label->dkl_vtoc.v_nparts = V_NUMPAR;
732 	label->dkl_vtoc.v_sanity = VTOC_SANE;
733 	label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
734 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0;
735 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl *
736 	    label->dkl_nhead * label->dkl_nsect;
737 	label->dkl_magic = DKL_MAGIC;
738 	label->dkl_cksum = vd_lbl2cksum(label);
739 }
740 
741 /*
742  * Function:
743  *	vd_file_set_vtoc
744  *
745  * Description:
746  *	Set the vtoc of a disk image by writing the label and backup
747  *	labels into the disk image backend.
748  *
749  * Parameters:
750  *	vd		- disk on which the operation is performed.
751  *	label		- the data to be written.
752  *
753  * Return Code:
754  *	0		- success.
755  *	n > 0		- error, n indicates the errno code.
756  */
757 static int
758 vd_file_set_vtoc(vd_t *vd, struct dk_label *label)
759 {
760 	int blk, sec, cyl, head, cnt;
761 
762 	ASSERT(vd->file);
763 
764 	if (VD_FILE_LABEL_WRITE(vd, label) < 0) {
765 		PR0("fail to write disk label");
766 		return (EIO);
767 	}
768 
769 	/*
770 	 * Backup labels are on the last alternate cylinder's
771 	 * first five odd sectors.
772 	 */
773 	if (label->dkl_acyl == 0) {
774 		PR0("no alternate cylinder, can not store backup labels");
775 		return (0);
776 	}
777 
778 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
779 	head = label->dkl_nhead - 1;
780 
781 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
782 	    (head * label->dkl_nsect);
783 
784 	/*
785 	 * Write the backup labels. Make sure we don't try to write past
786 	 * the last cylinder.
787 	 */
788 	sec = 1;
789 
790 	for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) {
791 
792 		if (sec >= label->dkl_nsect) {
793 			PR0("not enough sector to store all backup labels");
794 			return (0);
795 		}
796 
797 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label,
798 		    blk + sec, sizeof (struct dk_label)) < 0) {
799 			PR0("error writing backup label at block %d\n",
800 			    blk + sec);
801 			return (EIO);
802 		}
803 
804 		PR1("wrote backup label at block %d\n", blk + sec);
805 
806 		sec += 2;
807 	}
808 
809 	return (0);
810 }
811 
812 /*
813  * Function:
814  *	vd_file_get_devid_block
815  *
816  * Description:
817  *	Return the block number where the device id is stored.
818  *
819  * Parameters:
820  *	vd		- disk on which the operation is performed.
821  *	blkp		- pointer to the block number
822  *
823  * Return Code:
824  *	0		- success
825  *	ENOSPC		- disk has no space to store a device id
826  */
827 static int
828 vd_file_get_devid_block(vd_t *vd, size_t *blkp)
829 {
830 	diskaddr_t spc, head, cyl;
831 
832 	ASSERT(vd->file);
833 
834 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
835 		/*
836 		 * If no label is defined we don't know where to find
837 		 * a device id.
838 		 */
839 		return (ENOSPC);
840 	}
841 
842 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
843 		/*
844 		 * For an EFI disk, the devid is at the beginning of
845 		 * the reserved slice
846 		 */
847 		if (vd->efi_reserved == -1) {
848 			PR0("EFI disk has no reserved slice");
849 			return (ENOSPC);
850 		}
851 
852 		*blkp = vd->slices[vd->efi_reserved].start;
853 		return (0);
854 	}
855 
856 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
857 
858 	/* this geometry doesn't allow us to have a devid */
859 	if (vd->dk_geom.dkg_acyl < 2) {
860 		PR0("not enough alternate cylinder available for devid "
861 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
862 		return (ENOSPC);
863 	}
864 
865 	/* the devid is in on the track next to the last cylinder */
866 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
867 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
868 	head = vd->dk_geom.dkg_nhead - 1;
869 
870 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
871 	    (head * vd->dk_geom.dkg_nsect) + 1;
872 
873 	return (0);
874 }
875 
876 /*
877  * Return the checksum of a disk block containing an on-disk devid.
878  */
879 static uint_t
880 vd_dkdevid2cksum(struct dk_devid *dkdevid)
881 {
882 	uint_t chksum, *ip;
883 	int i;
884 
885 	chksum = 0;
886 	ip = (uint_t *)dkdevid;
887 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
888 		chksum ^= ip[i];
889 
890 	return (chksum);
891 }
892 
893 /*
894  * Function:
895  *	vd_file_read_devid
896  *
897  * Description:
898  *	Read the device id stored on a disk image.
899  *
900  * Parameters:
901  *	vd		- disk on which the operation is performed.
902  *	devid		- the return address of the device ID.
903  *
904  * Return Code:
905  *	0		- success
906  *	EIO		- I/O error while trying to access the disk image
907  *	EINVAL		- no valid device id was found
908  *	ENOSPC		- disk has no space to store a device id
909  */
910 static int
911 vd_file_read_devid(vd_t *vd, ddi_devid_t *devid)
912 {
913 	struct dk_devid *dkdevid;
914 	size_t blk;
915 	uint_t chksum;
916 	int status, sz;
917 
918 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
919 		return (status);
920 
921 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
922 
923 	/* get the devid */
924 	if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
925 	    DEV_BSIZE)) < 0) {
926 		PR0("error reading devid block at %lu", blk);
927 		status = EIO;
928 		goto done;
929 	}
930 
931 	/* validate the revision */
932 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
933 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
934 		PR0("invalid devid found at block %lu (bad revision)", blk);
935 		status = EINVAL;
936 		goto done;
937 	}
938 
939 	/* compute checksum */
940 	chksum = vd_dkdevid2cksum(dkdevid);
941 
942 	/* compare the checksums */
943 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
944 		PR0("invalid devid found at block %lu (bad checksum)", blk);
945 		status = EINVAL;
946 		goto done;
947 	}
948 
949 	/* validate the device id */
950 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
951 		PR0("invalid devid found at block %lu", blk);
952 		status = EINVAL;
953 		goto done;
954 	}
955 
956 	PR1("devid read at block %lu", blk);
957 
958 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
959 	*devid = kmem_alloc(sz, KM_SLEEP);
960 	bcopy(&dkdevid->dkd_devid, *devid, sz);
961 
962 done:
963 	kmem_free(dkdevid, DEV_BSIZE);
964 	return (status);
965 
966 }
967 
968 /*
969  * Function:
970  *	vd_file_write_devid
971  *
972  * Description:
973  *	Write a device id into disk image.
974  *
975  * Parameters:
976  *	vd		- disk on which the operation is performed.
977  *	devid		- the device ID to store.
978  *
979  * Return Code:
980  *	0		- success
981  *	EIO		- I/O error while trying to access the disk image
982  *	ENOSPC		- disk has no space to store a device id
983  */
984 static int
985 vd_file_write_devid(vd_t *vd, ddi_devid_t devid)
986 {
987 	struct dk_devid *dkdevid;
988 	uint_t chksum;
989 	size_t blk;
990 	int status;
991 
992 	if (devid == NULL) {
993 		/* nothing to write */
994 		return (0);
995 	}
996 
997 	if ((status = vd_file_get_devid_block(vd, &blk)) != 0)
998 		return (status);
999 
1000 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
1001 
1002 	/* set revision */
1003 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
1004 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
1005 
1006 	/* copy devid */
1007 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
1008 
1009 	/* compute checksum */
1010 	chksum = vd_dkdevid2cksum(dkdevid);
1011 
1012 	/* set checksum */
1013 	DKD_FORMCHKSUM(chksum, dkdevid);
1014 
1015 	/* store the devid */
1016 	if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
1017 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
1018 		PR0("Error writing devid block at %lu", blk);
1019 		status = EIO;
1020 	} else {
1021 		PR1("devid written at block %lu", blk);
1022 		status = 0;
1023 	}
1024 
1025 	kmem_free(dkdevid, DEV_BSIZE);
1026 	return (status);
1027 }
1028 
1029 /*
1030  * Function:
1031  *	vd_do_scsi_rdwr
1032  *
1033  * Description:
1034  * 	Read or write to a SCSI disk using an absolute disk offset.
1035  *
1036  * Parameters:
1037  *	vd		- disk on which the operation is performed.
1038  *	operation	- operation to execute: read (VD_OP_BREAD) or
1039  *			  write (VD_OP_BWRITE).
1040  *	data		- buffer where data are read to or written from.
1041  *	blk		- starting block for the operation.
1042  *	len		- number of bytes to read or write.
1043  *
1044  * Return Code:
1045  *	0		- success
1046  *	n != 0		- error.
1047  */
1048 static int
1049 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
1050 {
1051 	struct uscsi_cmd ucmd;
1052 	union scsi_cdb cdb;
1053 	int nsectors, nblk;
1054 	int max_sectors;
1055 	int status, rval;
1056 
1057 	ASSERT(!vd->file);
1058 	ASSERT(vd->vdisk_block_size > 0);
1059 
1060 	max_sectors = vd->max_xfer_sz;
1061 	nblk = (len / vd->vdisk_block_size);
1062 
1063 	if (len % vd->vdisk_block_size != 0)
1064 		return (EINVAL);
1065 
1066 	/*
1067 	 * Build and execute the uscsi ioctl.  We build a group0, group1
1068 	 * or group4 command as necessary, since some targets
1069 	 * do not support group1 commands.
1070 	 */
1071 	while (nblk) {
1072 
1073 		bzero(&ucmd, sizeof (ucmd));
1074 		bzero(&cdb, sizeof (cdb));
1075 
1076 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
1077 
1078 		/*
1079 		 * Some of the optical drives on sun4v machines are ATAPI
1080 		 * devices which use Group 1 Read/Write commands so we need
1081 		 * to explicitly check a flag which is set when a domain
1082 		 * is bound.
1083 		 */
1084 		if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
1085 			FORMG0ADDR(&cdb, blk);
1086 			FORMG0COUNT(&cdb, nsectors);
1087 			ucmd.uscsi_cdblen = CDB_GROUP0;
1088 		} else if (blk > 0xffffffff) {
1089 			FORMG4LONGADDR(&cdb, blk);
1090 			FORMG4COUNT(&cdb, nsectors);
1091 			ucmd.uscsi_cdblen = CDB_GROUP4;
1092 			cdb.scc_cmd |= SCMD_GROUP4;
1093 		} else {
1094 			FORMG1ADDR(&cdb, blk);
1095 			FORMG1COUNT(&cdb, nsectors);
1096 			ucmd.uscsi_cdblen = CDB_GROUP1;
1097 			cdb.scc_cmd |= SCMD_GROUP1;
1098 		}
1099 		ucmd.uscsi_cdb = (caddr_t)&cdb;
1100 		ucmd.uscsi_bufaddr = data;
1101 		ucmd.uscsi_buflen = nsectors * vd->block_size;
1102 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
1103 		/*
1104 		 * Set flags so that the command is isolated from normal
1105 		 * commands and no error message is printed.
1106 		 */
1107 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
1108 
1109 		if (operation == VD_OP_BREAD) {
1110 			cdb.scc_cmd |= SCMD_READ;
1111 			ucmd.uscsi_flags |= USCSI_READ;
1112 		} else {
1113 			cdb.scc_cmd |= SCMD_WRITE;
1114 		}
1115 
1116 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
1117 		    USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
1118 		    kcred, &rval);
1119 
1120 		if (status == 0)
1121 			status = ucmd.uscsi_status;
1122 
1123 		if (status != 0)
1124 			break;
1125 
1126 		/*
1127 		 * Check if partial DMA breakup is required. If so, reduce
1128 		 * the request size by half and retry the last request.
1129 		 */
1130 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
1131 			max_sectors >>= 1;
1132 			if (max_sectors <= 0) {
1133 				status = EIO;
1134 				break;
1135 			}
1136 			continue;
1137 		}
1138 
1139 		if (ucmd.uscsi_resid != 0) {
1140 			status = EIO;
1141 			break;
1142 		}
1143 
1144 		blk += nsectors;
1145 		nblk -= nsectors;
1146 		data += nsectors * vd->vdisk_block_size; /* SECSIZE */
1147 	}
1148 
1149 	return (status);
1150 }
1151 
1152 /*
1153  * Function:
1154  *	vd_scsi_rdwr
1155  *
1156  * Description:
1157  * 	Wrapper function to read or write to a SCSI disk using an absolute
1158  *	disk offset. It checks the blocksize of the underlying device and,
1159  *	if necessary, adjusts the buffers accordingly before calling
1160  *	vd_do_scsi_rdwr() to do the actual read or write.
1161  *
1162  * Parameters:
1163  *	vd		- disk on which the operation is performed.
1164  *	operation	- operation to execute: read (VD_OP_BREAD) or
1165  *			  write (VD_OP_BWRITE).
1166  *	data		- buffer where data are read to or written from.
1167  *	blk		- starting block for the operation.
1168  *	len		- number of bytes to read or write.
1169  *
1170  * Return Code:
1171  *	0		- success
1172  *	n != 0		- error.
1173  */
1174 static int
1175 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
1176 {
1177 	int	rv;
1178 
1179 	size_t	pblk;	/* physical device block number of data on device */
1180 	size_t	delta;	/* relative offset between pblk and vblk */
1181 	size_t	pnblk;	/* number of physical blocks to be read from device */
1182 	size_t	plen;	/* length of data to be read from physical device */
1183 	char	*buf;	/* buffer area to fit physical device's block size */
1184 
1185 	if (vd->block_size == 0) {
1186 		/*
1187 		 * The block size was not available during the attach,
1188 		 * try to update it now.
1189 		 */
1190 		if (vd_setup_mediainfo(vd) != 0)
1191 			return (EIO);
1192 	}
1193 
1194 	/*
1195 	 * If the vdisk block size and the block size of the underlying device
1196 	 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
1197 	 * to create a buffer large enough to handle the device's block size
1198 	 * and adjust the block to be read from and the amount of data to
1199 	 * read to correspond with the device's block size.
1200 	 */
1201 	if (vd->vdisk_block_size == vd->block_size)
1202 		return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
1203 
1204 	if (vd->vdisk_block_size > vd->block_size)
1205 		return (EINVAL);
1206 
1207 	/*
1208 	 * Writing of physical block sizes larger than the virtual block size
1209 	 * is not supported. This would be added if/when support for guests
1210 	 * writing to DVDs is implemented.
1211 	 */
1212 	if (operation == VD_OP_BWRITE)
1213 		return (ENOTSUP);
1214 
1215 	/* BEGIN CSTYLED */
1216 	/*
1217 	 * Below is a diagram showing the relationship between the physical
1218 	 * and virtual blocks. If the virtual blocks marked by 'X' below are
1219 	 * requested, then the physical blocks denoted by 'Y' are read.
1220 	 *
1221 	 *           vblk
1222 	 *             |      vlen
1223 	 *             |<--------------->|
1224 	 *             v                 v
1225 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   virtual disk:
1226 	 *    |  |  |  |XX|XX|XX|XX|XX|XX|  |  |  |  |  |  } block size is
1227 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-  vd->vdisk_block_size
1228 	 *          :  :                 :  :
1229 	 *         >:==:< delta          :  :
1230 	 *          :  :                 :  :
1231 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   physical disk:
1232 	 *    |     |YY:YY|YYYYY|YYYYY|YY:YY|     |     |   } block size is
1233 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   vd->block_size
1234 	 *          ^                       ^
1235 	 *          |<--------------------->|
1236 	 *          |         plen
1237 	 *         pblk
1238 	 */
1239 	/* END CSTYLED */
1240 	pblk = (vblk * vd->vdisk_block_size) / vd->block_size;
1241 	delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size);
1242 	pnblk = ((delta + vlen - 1) / vd->block_size) + 1;
1243 	plen = pnblk * vd->block_size;
1244 
1245 	PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
1246 
1247 	buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
1248 	rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
1249 	bcopy(buf + delta, data, vlen);
1250 
1251 	kmem_free(buf, sizeof (caddr_t) * plen);
1252 
1253 	return (rv);
1254 }
1255 
1256 /*
1257  * Return Values
1258  *	EINPROGRESS	- operation was successfully started
1259  *	EIO		- encountered LDC (aka. task error)
1260  *	0		- operation completed successfully
1261  *
1262  * Side Effect
1263  *     sets request->status = <disk operation status>
1264  */
1265 static int
1266 vd_start_bio(vd_task_t *task)
1267 {
1268 	int			rv, status = 0;
1269 	vd_t			*vd		= task->vd;
1270 	vd_dring_payload_t	*request	= task->request;
1271 	struct buf		*buf		= &task->buf;
1272 	uint8_t			mtype;
1273 	int 			slice;
1274 	char			*bufaddr = 0;
1275 	size_t			buflen;
1276 
1277 	ASSERT(vd != NULL);
1278 	ASSERT(request != NULL);
1279 
1280 	slice = request->slice;
1281 
1282 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1283 	ASSERT((request->operation == VD_OP_BREAD) ||
1284 	    (request->operation == VD_OP_BWRITE));
1285 
1286 	if (request->nbytes == 0) {
1287 		/* no service for trivial requests */
1288 		request->status = EINVAL;
1289 		return (0);
1290 	}
1291 
1292 	PR1("%s %lu bytes at block %lu",
1293 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1294 	    request->nbytes, request->addr);
1295 
1296 	/*
1297 	 * We have to check the open flags because the functions processing
1298 	 * the read/write request will not do it.
1299 	 */
1300 	if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
1301 		PR0("write fails because backend is opened read-only");
1302 		request->nbytes = 0;
1303 		request->status = EROFS;
1304 		return (0);
1305 	}
1306 
1307 	mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP;
1308 
1309 	/* Map memory exported by client */
1310 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
1311 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
1312 	    &bufaddr, NULL);
1313 	if (status != 0) {
1314 		PR0("ldc_mem_map() returned err %d ", status);
1315 		return (EIO);
1316 	}
1317 
1318 	buflen = request->nbytes;
1319 
1320 	status = ldc_mem_acquire(task->mhdl, 0, buflen);
1321 	if (status != 0) {
1322 		(void) ldc_mem_unmap(task->mhdl);
1323 		PR0("ldc_mem_acquire() returned err %d ", status);
1324 		return (EIO);
1325 	}
1326 
1327 	/* Start the block I/O */
1328 	if (vd->file) {
1329 		rv = vd_file_rw(vd, slice, request->operation, bufaddr,
1330 		    request->addr, request->nbytes);
1331 		if (rv < 0) {
1332 			request->nbytes = 0;
1333 			request->status = EIO;
1334 		} else {
1335 			request->nbytes = rv;
1336 			request->status = 0;
1337 		}
1338 	} else {
1339 		if (slice == VD_SLICE_NONE) {
1340 			/*
1341 			 * This is not a disk image so it is a real disk. We
1342 			 * assume that the underlying device driver supports
1343 			 * USCSICMD ioctls. This is the case of all SCSI devices
1344 			 * (sd, ssd...).
1345 			 *
1346 			 * In the future if we have non-SCSI disks we would need
1347 			 * to invoke the appropriate function to do I/O using an
1348 			 * absolute disk offset (for example using DIOCTL_RWCMD
1349 			 * for IDE disks).
1350 			 */
1351 			rv = vd_scsi_rdwr(vd, request->operation, bufaddr,
1352 			    request->addr, request->nbytes);
1353 			if (rv != 0) {
1354 				request->nbytes = 0;
1355 				request->status = EIO;
1356 			} else {
1357 				request->status = 0;
1358 			}
1359 		} else {
1360 			bioinit(buf);
1361 			buf->b_flags	= B_BUSY;
1362 			buf->b_bcount	= request->nbytes;
1363 			buf->b_lblkno	= request->addr;
1364 			buf->b_edev 	= vd->dev[slice];
1365 			buf->b_un.b_addr = bufaddr;
1366 			buf->b_flags 	|= (request->operation == VD_OP_BREAD)?
1367 			    B_READ : B_WRITE;
1368 
1369 			request->status =
1370 			    ldi_strategy(vd->ldi_handle[slice], buf);
1371 
1372 			/*
1373 			 * This is to indicate to the caller that the request
1374 			 * needs to be finished by vd_complete_bio() by calling
1375 			 * biowait() there and waiting for that to return before
1376 			 * triggering the notification of the vDisk client.
1377 			 *
1378 			 * This is necessary when writing to real disks as
1379 			 * otherwise calls to ldi_strategy() would be serialized
1380 			 * behind the calls to biowait() and performance would
1381 			 * suffer.
1382 			 */
1383 			if (request->status == 0)
1384 				return (EINPROGRESS);
1385 
1386 			biofini(buf);
1387 		}
1388 	}
1389 
1390 	/* Clean up after error */
1391 	rv = ldc_mem_release(task->mhdl, 0, buflen);
1392 	if (rv) {
1393 		PR0("ldc_mem_release() returned err %d ", rv);
1394 		status = EIO;
1395 	}
1396 	rv = ldc_mem_unmap(task->mhdl);
1397 	if (rv) {
1398 		PR0("ldc_mem_unmap() returned err %d ", rv);
1399 		status = EIO;
1400 	}
1401 
1402 	return (status);
1403 }
1404 
1405 /*
1406  * This function should only be called from vd_notify to ensure that requests
1407  * are responded to in the order that they are received.
1408  */
1409 static int
1410 send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
1411 {
1412 	int	status;
1413 	size_t	nbytes;
1414 
1415 	do {
1416 		nbytes = msglen;
1417 		status = ldc_write(ldc_handle, msg, &nbytes);
1418 		if (status != EWOULDBLOCK)
1419 			break;
1420 		drv_usecwait(vds_ldc_delay);
1421 	} while (status == EWOULDBLOCK);
1422 
1423 	if (status != 0) {
1424 		if (status != ECONNRESET)
1425 			PR0("ldc_write() returned errno %d", status);
1426 		return (status);
1427 	} else if (nbytes != msglen) {
1428 		PR0("ldc_write() performed only partial write");
1429 		return (EIO);
1430 	}
1431 
1432 	PR1("SENT %lu bytes", msglen);
1433 	return (0);
1434 }
1435 
1436 static void
1437 vd_need_reset(vd_t *vd, boolean_t reset_ldc)
1438 {
1439 	mutex_enter(&vd->lock);
1440 	vd->reset_state	= B_TRUE;
1441 	vd->reset_ldc	= reset_ldc;
1442 	mutex_exit(&vd->lock);
1443 }
1444 
1445 /*
1446  * Reset the state of the connection with a client, if needed; reset the LDC
1447  * transport as well, if needed.  This function should only be called from the
1448  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
1449  */
1450 static void
1451 vd_reset_if_needed(vd_t *vd)
1452 {
1453 	int	status = 0;
1454 
1455 	mutex_enter(&vd->lock);
1456 	if (!vd->reset_state) {
1457 		ASSERT(!vd->reset_ldc);
1458 		mutex_exit(&vd->lock);
1459 		return;
1460 	}
1461 	mutex_exit(&vd->lock);
1462 
1463 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
1464 
1465 	/*
1466 	 * Let any asynchronous I/O complete before possibly pulling the rug
1467 	 * out from under it; defer checking vd->reset_ldc, as one of the
1468 	 * asynchronous tasks might set it
1469 	 */
1470 	ddi_taskq_wait(vd->completionq);
1471 
1472 	if (vd->file) {
1473 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
1474 		if (status) {
1475 			PR0("VOP_FSYNC returned errno %d", status);
1476 		}
1477 	}
1478 
1479 	if ((vd->initialized & VD_DRING) &&
1480 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
1481 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
1482 
1483 	vd_free_dring_task(vd);
1484 
1485 	/* Free the staging buffer for msgs */
1486 	if (vd->vio_msgp != NULL) {
1487 		kmem_free(vd->vio_msgp, vd->max_msglen);
1488 		vd->vio_msgp = NULL;
1489 	}
1490 
1491 	/* Free the inband message buffer */
1492 	if (vd->inband_task.msg != NULL) {
1493 		kmem_free(vd->inband_task.msg, vd->max_msglen);
1494 		vd->inband_task.msg = NULL;
1495 	}
1496 
1497 	mutex_enter(&vd->lock);
1498 
1499 	if (vd->reset_ldc)
1500 		PR0("taking down LDC channel");
1501 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
1502 		PR0("ldc_down() returned errno %d", status);
1503 
1504 	/* Reset exclusive access rights */
1505 	vd_reset_access(vd);
1506 
1507 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
1508 	vd->state	= VD_STATE_INIT;
1509 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
1510 
1511 	/* Allocate the staging buffer */
1512 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
1513 
1514 	PR0("calling ldc_up\n");
1515 	(void) ldc_up(vd->ldc_handle);
1516 
1517 	vd->reset_state	= B_FALSE;
1518 	vd->reset_ldc	= B_FALSE;
1519 
1520 	mutex_exit(&vd->lock);
1521 }
1522 
1523 static void vd_recv_msg(void *arg);
1524 
1525 static void
1526 vd_mark_in_reset(vd_t *vd)
1527 {
1528 	int status;
1529 
1530 	PR0("vd_mark_in_reset: marking vd in reset\n");
1531 
1532 	vd_need_reset(vd, B_FALSE);
1533 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
1534 	if (status == DDI_FAILURE) {
1535 		PR0("cannot schedule task to recv msg\n");
1536 		vd_need_reset(vd, B_TRUE);
1537 		return;
1538 	}
1539 }
1540 
1541 static int
1542 vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
1543 {
1544 	boolean_t		accepted;
1545 	int			status;
1546 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
1547 
1548 	if (vd->reset_state)
1549 		return (0);
1550 
1551 	/* Acquire the element */
1552 	if (!vd->reset_state &&
1553 	    (status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
1554 		if (status == ECONNRESET) {
1555 			vd_mark_in_reset(vd);
1556 			return (0);
1557 		} else {
1558 			PR0("ldc_mem_dring_acquire() returned errno %d",
1559 			    status);
1560 			return (status);
1561 		}
1562 	}
1563 
1564 	/* Set the element's status and mark it done */
1565 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
1566 	if (accepted) {
1567 		elem->payload.nbytes	= elem_nbytes;
1568 		elem->payload.status	= elem_status;
1569 		elem->hdr.dstate	= VIO_DESC_DONE;
1570 	} else {
1571 		/* Perhaps client timed out waiting for I/O... */
1572 		PR0("element %u no longer \"accepted\"", idx);
1573 		VD_DUMP_DRING_ELEM(elem);
1574 	}
1575 	/* Release the element */
1576 	if (!vd->reset_state &&
1577 	    (status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
1578 		if (status == ECONNRESET) {
1579 			vd_mark_in_reset(vd);
1580 			return (0);
1581 		} else {
1582 			PR0("ldc_mem_dring_release() returned errno %d",
1583 			    status);
1584 			return (status);
1585 		}
1586 	}
1587 
1588 	return (accepted ? 0 : EINVAL);
1589 }
1590 
1591 /*
1592  * Return Values
1593  *	0	- operation completed successfully
1594  *	EIO	- encountered LDC / task error
1595  *
1596  * Side Effect
1597  *	sets request->status = <disk operation status>
1598  */
1599 static int
1600 vd_complete_bio(vd_task_t *task)
1601 {
1602 	int			status		= 0;
1603 	int			rv		= 0;
1604 	vd_t			*vd		= task->vd;
1605 	vd_dring_payload_t	*request	= task->request;
1606 	struct buf		*buf		= &task->buf;
1607 
1608 
1609 	ASSERT(vd != NULL);
1610 	ASSERT(request != NULL);
1611 	ASSERT(task->msg != NULL);
1612 	ASSERT(task->msglen >= sizeof (*task->msg));
1613 	ASSERT(!vd->file);
1614 	ASSERT(request->slice != VD_SLICE_NONE);
1615 
1616 	/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
1617 	request->status = biowait(buf);
1618 
1619 	/* return back the number of bytes read/written */
1620 	request->nbytes = buf->b_bcount - buf->b_resid;
1621 
1622 	/* Release the buffer */
1623 	if (!vd->reset_state)
1624 		status = ldc_mem_release(task->mhdl, 0, buf->b_bcount);
1625 	if (status) {
1626 		PR0("ldc_mem_release() returned errno %d copying to "
1627 		    "client", status);
1628 		if (status == ECONNRESET) {
1629 			vd_mark_in_reset(vd);
1630 		}
1631 		rv = EIO;
1632 	}
1633 
1634 	/* Unmap the memory, even if in reset */
1635 	status = ldc_mem_unmap(task->mhdl);
1636 	if (status) {
1637 		PR0("ldc_mem_unmap() returned errno %d copying to client",
1638 		    status);
1639 		if (status == ECONNRESET) {
1640 			vd_mark_in_reset(vd);
1641 		}
1642 		rv = EIO;
1643 	}
1644 
1645 	biofini(buf);
1646 
1647 	return (rv);
1648 }
1649 
1650 /*
1651  * Description:
1652  *	This function is called by the two functions called by a taskq
1653  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
1654  *	message to the client.
1655  *
1656  * Parameters:
1657  *	arg 	- opaque pointer to structure containing task to be completed
1658  *
1659  * Return Values
1660  *	None
1661  */
1662 static void
1663 vd_notify(vd_task_t *task)
1664 {
1665 	int	status;
1666 
1667 	ASSERT(task != NULL);
1668 	ASSERT(task->vd != NULL);
1669 
1670 	if (task->vd->reset_state)
1671 		return;
1672 
1673 	/*
1674 	 * Send the "ack" or "nack" back to the client; if sending the message
1675 	 * via LDC fails, arrange to reset both the connection state and LDC
1676 	 * itself
1677 	 */
1678 	PR2("Sending %s",
1679 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
1680 
1681 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
1682 	switch (status) {
1683 	case 0:
1684 		break;
1685 	case ECONNRESET:
1686 		vd_mark_in_reset(task->vd);
1687 		break;
1688 	default:
1689 		PR0("initiating full reset");
1690 		vd_need_reset(task->vd, B_TRUE);
1691 		break;
1692 	}
1693 
1694 	DTRACE_PROBE1(task__end, vd_task_t *, task);
1695 }
1696 
1697 /*
1698  * Description:
1699  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
1700  *	the vDisk client
1701  *
1702  * Parameters:
1703  *	task 		- structure containing the request sent from client
1704  *
1705  * Return Values
1706  *	None
1707  */
1708 static void
1709 vd_complete_notify(vd_task_t *task)
1710 {
1711 	int			status		= 0;
1712 	vd_t			*vd		= task->vd;
1713 	vd_dring_payload_t	*request	= task->request;
1714 
1715 	/* Update the dring element for a dring client */
1716 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
1717 		status = vd_mark_elem_done(vd, task->index,
1718 		    request->status, request->nbytes);
1719 		if (status == ECONNRESET)
1720 			vd_mark_in_reset(vd);
1721 	}
1722 
1723 	/*
1724 	 * If a transport error occurred while marking the element done or
1725 	 * previously while executing the task, arrange to "nack" the message
1726 	 * when the final task in the descriptor element range completes
1727 	 */
1728 	if ((status != 0) || (task->status != 0))
1729 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
1730 
1731 	/*
1732 	 * Only the final task for a range of elements will respond to and
1733 	 * free the message
1734 	 */
1735 	if (task->type == VD_NONFINAL_RANGE_TASK) {
1736 		return;
1737 	}
1738 
1739 	vd_notify(task);
1740 }
1741 
1742 /*
1743  * Description:
1744  *	This is the basic completion function called to handle inband data
1745  *	requests and handshake messages. All it needs to do is trigger a
1746  *	message to the client that the request is completed.
1747  *
1748  * Parameters:
1749  *	arg 	- opaque pointer to structure containing task to be completed
1750  *
1751  * Return Values
1752  *	None
1753  */
1754 static void
1755 vd_serial_notify(void *arg)
1756 {
1757 	vd_task_t		*task = (vd_task_t *)arg;
1758 
1759 	ASSERT(task != NULL);
1760 	vd_notify(task);
1761 }
1762 
1763 /* ARGSUSED */
1764 static int
1765 vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
1766 {
1767 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
1768 	return (0);
1769 }
1770 
1771 /* ARGSUSED */
1772 static int
1773 vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
1774 {
1775 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg);
1776 	return (0);
1777 }
1778 
1779 static void
1780 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
1781 {
1782 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
1783 }
1784 
1785 static void
1786 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
1787 {
1788 	VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
1789 }
1790 
1791 static int
1792 vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
1793 {
1794 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
1795 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
1796 	size_t data_len;
1797 
1798 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
1799 	if (vd_efi->length > data_len)
1800 		return (EINVAL);
1801 
1802 	dk_efi->dki_lba = vd_efi->lba;
1803 	dk_efi->dki_length = vd_efi->length;
1804 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
1805 	return (0);
1806 }
1807 
1808 static void
1809 vd_get_efi_out(void *ioctl_arg, void *vd_buf)
1810 {
1811 	int len;
1812 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
1813 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
1814 
1815 	len = vd_efi->length;
1816 	DK_EFI2VD_EFI(dk_efi, vd_efi);
1817 	kmem_free(dk_efi->dki_data, len);
1818 }
1819 
1820 static int
1821 vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
1822 {
1823 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
1824 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
1825 	size_t data_len;
1826 
1827 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
1828 	if (vd_efi->length > data_len)
1829 		return (EINVAL);
1830 
1831 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
1832 	VD_EFI2DK_EFI(vd_efi, dk_efi);
1833 	return (0);
1834 }
1835 
1836 static void
1837 vd_set_efi_out(void *ioctl_arg, void *vd_buf)
1838 {
1839 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
1840 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
1841 
1842 	kmem_free(dk_efi->dki_data, vd_efi->length);
1843 }
1844 
1845 static int
1846 vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
1847 {
1848 	size_t vd_scsi_len;
1849 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
1850 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
1851 
1852 	/* check buffer size */
1853 	vd_scsi_len = VD_SCSI_SIZE;
1854 	vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
1855 	vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
1856 	vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
1857 	vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
1858 
1859 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
1860 
1861 	if (vd_buf_len < vd_scsi_len)
1862 		return (EINVAL);
1863 
1864 	/* set flags */
1865 	uscsi->uscsi_flags = vd_scsi_debug;
1866 
1867 	if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
1868 		uscsi->uscsi_flags |= USCSI_ISOLATE;
1869 		uscsi->uscsi_flags |= USCSI_DIAGNOSE;
1870 	}
1871 
1872 	/* task attribute */
1873 	switch (vd_scsi->task_attribute) {
1874 	case VD_SCSI_TASK_ACA:
1875 		uscsi->uscsi_flags |= USCSI_HEAD;
1876 		break;
1877 	case VD_SCSI_TASK_HQUEUE:
1878 		uscsi->uscsi_flags |= USCSI_HTAG;
1879 		break;
1880 	case VD_SCSI_TASK_ORDERED:
1881 		uscsi->uscsi_flags |= USCSI_OTAG;
1882 		break;
1883 	default:
1884 		uscsi->uscsi_flags |= USCSI_NOTAG;
1885 		break;
1886 	}
1887 
1888 	/* timeout */
1889 	uscsi->uscsi_timeout = vd_scsi->timeout;
1890 
1891 	/* cdb data */
1892 	uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
1893 	uscsi->uscsi_cdblen = vd_scsi->cdb_len;
1894 
1895 	/* sense buffer */
1896 	if (vd_scsi->sense_len != 0) {
1897 		uscsi->uscsi_flags |= USCSI_RQENABLE;
1898 		uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
1899 		uscsi->uscsi_rqlen = vd_scsi->sense_len;
1900 	}
1901 
1902 	if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
1903 		/* uscsi does not support read/write request */
1904 		return (EINVAL);
1905 	}
1906 
1907 	/* request data-in */
1908 	if (vd_scsi->datain_len != 0) {
1909 		uscsi->uscsi_flags |= USCSI_READ;
1910 		uscsi->uscsi_buflen = vd_scsi->datain_len;
1911 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
1912 	}
1913 
1914 	/* request data-out */
1915 	if (vd_scsi->dataout_len != 0) {
1916 		uscsi->uscsi_buflen = vd_scsi->dataout_len;
1917 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
1918 	}
1919 
1920 	return (0);
1921 }
1922 
1923 static void
1924 vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
1925 {
1926 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
1927 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
1928 
1929 	/* output fields */
1930 	vd_scsi->cmd_status = uscsi->uscsi_status;
1931 
1932 	/* sense data */
1933 	if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
1934 	    (uscsi->uscsi_status == STATUS_CHECK ||
1935 	    uscsi->uscsi_status == STATUS_TERMINATED)) {
1936 		vd_scsi->sense_status = uscsi->uscsi_rqstatus;
1937 		if (uscsi->uscsi_rqstatus == STATUS_GOOD)
1938 			vd_scsi->sense_len -= uscsi->uscsi_resid;
1939 		else
1940 			vd_scsi->sense_len = 0;
1941 	} else {
1942 		vd_scsi->sense_len = 0;
1943 	}
1944 
1945 	if (uscsi->uscsi_status != STATUS_GOOD) {
1946 		vd_scsi->dataout_len = 0;
1947 		vd_scsi->datain_len = 0;
1948 		return;
1949 	}
1950 
1951 	if (uscsi->uscsi_flags & USCSI_READ) {
1952 		/* request data (read) */
1953 		vd_scsi->datain_len -= uscsi->uscsi_resid;
1954 		vd_scsi->dataout_len = 0;
1955 	} else {
1956 		/* request data (write) */
1957 		vd_scsi->datain_len = 0;
1958 		vd_scsi->dataout_len -= uscsi->uscsi_resid;
1959 	}
1960 }
1961 
1962 static ushort_t
1963 vd_lbl2cksum(struct dk_label *label)
1964 {
1965 	int	count;
1966 	ushort_t sum, *sp;
1967 
1968 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
1969 	sp = (ushort_t *)label;
1970 	sum = 0;
1971 	while (count--) {
1972 		sum ^= *sp++;
1973 	}
1974 
1975 	return (sum);
1976 }
1977 
1978 /*
1979  * Handle ioctls to a disk slice.
1980  *
1981  * Return Values
1982  *	0	- Indicates that there are no errors in disk operations
1983  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
1984  *	EINVAL	- Not enough room to copy the EFI label
1985  *
1986  */
1987 static int
1988 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
1989 {
1990 	dk_efi_t *dk_ioc;
1991 	int rval;
1992 
1993 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1994 
1995 	if (cmd == DKIOCFLUSHWRITECACHE) {
1996 		if (vd->file) {
1997 			return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
1998 		} else {
1999 			return (ldi_ioctl(vd->ldi_handle[0], cmd,
2000 			    (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL,
2001 			    kcred, &rval));
2002 		}
2003 	}
2004 
2005 	switch (vd->vdisk_label) {
2006 
2007 	/* ioctls for a single slice disk with a VTOC label */
2008 	case VD_DISK_LABEL_VTOC:
2009 
2010 		switch (cmd) {
2011 		case DKIOCGGEOM:
2012 			ASSERT(ioctl_arg != NULL);
2013 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
2014 			return (0);
2015 		case DKIOCGVTOC:
2016 			ASSERT(ioctl_arg != NULL);
2017 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
2018 			return (0);
2019 		default:
2020 			return (ENOTSUP);
2021 		}
2022 
2023 	/* ioctls for a single slice disk with an EFI label */
2024 	case VD_DISK_LABEL_EFI:
2025 
2026 		switch (cmd) {
2027 		case DKIOCGETEFI:
2028 			ASSERT(ioctl_arg != NULL);
2029 			dk_ioc = (dk_efi_t *)ioctl_arg;
2030 
2031 			/*
2032 			 * For a single slice disk with an EFI label, we define
2033 			 * a fake EFI label with the GPT at LBA 1 and one GPE
2034 			 * at LBA 2. So we return the GPT or the GPE depending
2035 			 * on which LBA is requested.
2036 			 */
2037 			if (dk_ioc->dki_lba == 1) {
2038 
2039 				/* return the EFI GPT */
2040 				if (dk_ioc->dki_length < sizeof (efi_gpt_t))
2041 					return (EINVAL);
2042 
2043 				bcopy(&vd->efi_gpt, dk_ioc->dki_data,
2044 				    sizeof (efi_gpt_t));
2045 
2046 				/* also return the GPE if possible */
2047 				if (dk_ioc->dki_length >= sizeof (efi_gpt_t) +
2048 				    sizeof (efi_gpe_t)) {
2049 					bcopy(&vd->efi_gpe, dk_ioc->dki_data +
2050 					    1, sizeof (efi_gpe_t));
2051 				}
2052 
2053 			} else if (dk_ioc->dki_lba == 2) {
2054 
2055 				/* return the EFI GPE */
2056 				if (dk_ioc->dki_length < sizeof (efi_gpe_t))
2057 					return (EINVAL);
2058 
2059 				bcopy(&vd->efi_gpe, dk_ioc->dki_data,
2060 				    sizeof (efi_gpe_t));
2061 
2062 			} else {
2063 				return (EINVAL);
2064 			}
2065 
2066 			return (0);
2067 		default:
2068 			return (ENOTSUP);
2069 		}
2070 
2071 	default:
2072 		/* Unknown disk label type */
2073 		return (ENOTSUP);
2074 	}
2075 }
2076 
2077 static int
2078 vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
2079 {
2080 	vd_efi_dev_t edev;
2081 	int status;
2082 
2083 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2084 
2085 	status = vd_efi_alloc_and_read(&edev, gpt, gpe);
2086 
2087 	return (status);
2088 }
2089 
2090 static void
2091 vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
2092 {
2093 	vd_efi_dev_t edev;
2094 
2095 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
2096 
2097 	vd_efi_free(&edev, gpt, gpe);
2098 }
2099 
2100 static int
2101 vd_file_validate_efi(vd_t *vd)
2102 {
2103 	efi_gpt_t *gpt;
2104 	efi_gpe_t *gpe;
2105 	int i, nparts, status;
2106 	struct uuid efi_reserved = EFI_RESERVED;
2107 
2108 	if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
2109 		return (status);
2110 
2111 	bzero(&vd->vtoc, sizeof (struct vtoc));
2112 	bzero(&vd->dk_geom, sizeof (struct dk_geom));
2113 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2114 
2115 	vd->efi_reserved = -1;
2116 
2117 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
2118 
2119 	for (i = 0; i < nparts && i < VD_MAXPART; i++) {
2120 
2121 		if (gpe[i].efi_gpe_StartingLBA == 0 ||
2122 		    gpe[i].efi_gpe_EndingLBA == 0) {
2123 			continue;
2124 		}
2125 
2126 		vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
2127 		vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
2128 		    gpe[i].efi_gpe_StartingLBA + 1;
2129 
2130 		if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
2131 		    sizeof (struct uuid)) == 0)
2132 			vd->efi_reserved = i;
2133 
2134 	}
2135 
2136 	ASSERT(vd->vdisk_size != 0);
2137 	vd->slices[VD_EFI_WD_SLICE].start = 0;
2138 	vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
2139 
2140 	vds_efi_free(vd, gpt, gpe);
2141 
2142 	return (status);
2143 }
2144 
2145 /*
2146  * Function:
2147  *	vd_file_validate_geometry
2148  *
2149  * Description:
2150  *	Read the label and validate the geometry of a disk image. The driver
2151  *	label, vtoc and geometry information are updated according to the
2152  *	label read from the disk image.
2153  *
2154  *	If no valid label is found, the label is set to unknown and the
2155  *	function returns EINVAL, but a default vtoc and geometry are provided
2156  *	to the driver. If an EFI label is found, ENOTSUP is returned.
2157  *
2158  * Parameters:
2159  *	vd	- disk on which the operation is performed.
2160  *
2161  * Return Code:
2162  *	0	- success.
2163  *	EIO	- error reading the label from the disk image.
2164  *	EINVAL	- unknown disk label.
2165  *	ENOTSUP	- geometry not applicable (EFI label).
2166  */
2167 static int
2168 vd_file_validate_geometry(vd_t *vd)
2169 {
2170 	struct dk_label label;
2171 	struct dk_geom *geom = &vd->dk_geom;
2172 	struct vtoc *vtoc = &vd->vtoc;
2173 	int i;
2174 	int status = 0;
2175 
2176 	ASSERT(vd->file);
2177 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
2178 
2179 	if (VD_FILE_LABEL_READ(vd, &label) < 0)
2180 		return (EIO);
2181 
2182 	if (label.dkl_magic != DKL_MAGIC ||
2183 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
2184 	    label.dkl_vtoc.v_sanity != VTOC_SANE ||
2185 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
2186 
2187 		if (vd_file_validate_efi(vd) == 0) {
2188 			vd->vdisk_label = VD_DISK_LABEL_EFI;
2189 			return (ENOTSUP);
2190 		}
2191 
2192 		vd->vdisk_label = VD_DISK_LABEL_UNK;
2193 		vd_file_build_default_label(vd, &label);
2194 		status = EINVAL;
2195 	} else {
2196 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
2197 	}
2198 
2199 	/* Update the driver geometry */
2200 	bzero(geom, sizeof (struct dk_geom));
2201 
2202 	geom->dkg_ncyl = label.dkl_ncyl;
2203 	geom->dkg_acyl = label.dkl_acyl;
2204 	geom->dkg_nhead = label.dkl_nhead;
2205 	geom->dkg_nsect = label.dkl_nsect;
2206 	geom->dkg_intrlv = label.dkl_intrlv;
2207 	geom->dkg_apc = label.dkl_apc;
2208 	geom->dkg_rpm = label.dkl_rpm;
2209 	geom->dkg_pcyl = label.dkl_pcyl;
2210 	geom->dkg_write_reinstruct = label.dkl_write_reinstruct;
2211 	geom->dkg_read_reinstruct = label.dkl_read_reinstruct;
2212 
2213 	/* Update the driver vtoc */
2214 	bzero(vtoc, sizeof (struct vtoc));
2215 
2216 	vtoc->v_sanity = label.dkl_vtoc.v_sanity;
2217 	vtoc->v_version = label.dkl_vtoc.v_version;
2218 	vtoc->v_sectorsz = DEV_BSIZE;
2219 	vtoc->v_nparts = label.dkl_vtoc.v_nparts;
2220 
2221 	for (i = 0; i < vtoc->v_nparts; i++) {
2222 		vtoc->v_part[i].p_tag =
2223 		    label.dkl_vtoc.v_part[i].p_tag;
2224 		vtoc->v_part[i].p_flag =
2225 		    label.dkl_vtoc.v_part[i].p_flag;
2226 		vtoc->v_part[i].p_start =
2227 		    label.dkl_map[i].dkl_cylno *
2228 		    (label.dkl_nhead * label.dkl_nsect);
2229 		vtoc->v_part[i].p_size = label.dkl_map[i].dkl_nblk;
2230 		vtoc->timestamp[i] =
2231 		    label.dkl_vtoc.v_timestamp[i];
2232 	}
2233 	/*
2234 	 * The bootinfo array can not be copied with bcopy() because
2235 	 * elements are of type long in vtoc (so 64-bit) and of type
2236 	 * int in dk_vtoc (so 32-bit).
2237 	 */
2238 	vtoc->v_bootinfo[0] = label.dkl_vtoc.v_bootinfo[0];
2239 	vtoc->v_bootinfo[1] = label.dkl_vtoc.v_bootinfo[1];
2240 	vtoc->v_bootinfo[2] = label.dkl_vtoc.v_bootinfo[2];
2241 	bcopy(label.dkl_asciilabel, vtoc->v_asciilabel,
2242 	    LEN_DKL_ASCII);
2243 	bcopy(label.dkl_vtoc.v_volume, vtoc->v_volume,
2244 	    LEN_DKL_VVOL);
2245 
2246 	/* Update logical partitions */
2247 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
2248 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
2249 		for (i = 0; i < vtoc->v_nparts; i++) {
2250 			vd->slices[i].start = vtoc->v_part[i].p_start;
2251 			vd->slices[i].nblocks = vtoc->v_part[i].p_size;
2252 		}
2253 	}
2254 
2255 	return (status);
2256 }
2257 
2258 /*
2259  * Handle ioctls to a disk image (file-based).
2260  *
2261  * Return Values
2262  *	0	- Indicates that there are no errors
2263  *	!= 0	- Disk operation returned an error
2264  */
2265 static int
2266 vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
2267 {
2268 	struct dk_label label;
2269 	struct dk_geom *geom;
2270 	struct vtoc *vtoc;
2271 	dk_efi_t *efi;
2272 	int i, rc;
2273 
2274 	ASSERT(vd->file);
2275 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
2276 
2277 	switch (cmd) {
2278 
2279 	case DKIOCGGEOM:
2280 		ASSERT(ioctl_arg != NULL);
2281 		geom = (struct dk_geom *)ioctl_arg;
2282 
2283 		rc = vd_file_validate_geometry(vd);
2284 		if (rc != 0 && rc != EINVAL)
2285 			return (rc);
2286 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
2287 		return (0);
2288 
2289 	case DKIOCGVTOC:
2290 		ASSERT(ioctl_arg != NULL);
2291 		vtoc = (struct vtoc *)ioctl_arg;
2292 
2293 		rc = vd_file_validate_geometry(vd);
2294 		if (rc != 0 && rc != EINVAL)
2295 			return (rc);
2296 		bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc));
2297 		return (0);
2298 
2299 	case DKIOCSGEOM:
2300 		ASSERT(ioctl_arg != NULL);
2301 		geom = (struct dk_geom *)ioctl_arg;
2302 
2303 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
2304 			return (EINVAL);
2305 
2306 		/*
2307 		 * The current device geometry is not updated, just the driver
2308 		 * "notion" of it. The device geometry will be effectively
2309 		 * updated when a label is written to the device during a next
2310 		 * DKIOCSVTOC.
2311 		 */
2312 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
2313 		return (0);
2314 
2315 	case DKIOCSVTOC:
2316 		ASSERT(ioctl_arg != NULL);
2317 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
2318 		    vd->dk_geom.dkg_nsect != 0);
2319 		vtoc = (struct vtoc *)ioctl_arg;
2320 
2321 		if (vtoc->v_sanity != VTOC_SANE ||
2322 		    vtoc->v_sectorsz != DEV_BSIZE ||
2323 		    vtoc->v_nparts != V_NUMPAR)
2324 			return (EINVAL);
2325 
2326 		bzero(&label, sizeof (label));
2327 		label.dkl_ncyl = vd->dk_geom.dkg_ncyl;
2328 		label.dkl_acyl = vd->dk_geom.dkg_acyl;
2329 		label.dkl_pcyl = vd->dk_geom.dkg_pcyl;
2330 		label.dkl_nhead = vd->dk_geom.dkg_nhead;
2331 		label.dkl_nsect = vd->dk_geom.dkg_nsect;
2332 		label.dkl_intrlv = vd->dk_geom.dkg_intrlv;
2333 		label.dkl_apc = vd->dk_geom.dkg_apc;
2334 		label.dkl_rpm = vd->dk_geom.dkg_rpm;
2335 		label.dkl_write_reinstruct = vd->dk_geom.dkg_write_reinstruct;
2336 		label.dkl_read_reinstruct = vd->dk_geom.dkg_read_reinstruct;
2337 
2338 		label.dkl_vtoc.v_nparts = V_NUMPAR;
2339 		label.dkl_vtoc.v_sanity = VTOC_SANE;
2340 		label.dkl_vtoc.v_version = vtoc->v_version;
2341 		for (i = 0; i < V_NUMPAR; i++) {
2342 			label.dkl_vtoc.v_timestamp[i] =
2343 			    vtoc->timestamp[i];
2344 			label.dkl_vtoc.v_part[i].p_tag =
2345 			    vtoc->v_part[i].p_tag;
2346 			label.dkl_vtoc.v_part[i].p_flag =
2347 			    vtoc->v_part[i].p_flag;
2348 			label.dkl_map[i].dkl_cylno =
2349 			    vtoc->v_part[i].p_start /
2350 			    (label.dkl_nhead * label.dkl_nsect);
2351 			label.dkl_map[i].dkl_nblk =
2352 			    vtoc->v_part[i].p_size;
2353 		}
2354 		/*
2355 		 * The bootinfo array can not be copied with bcopy() because
2356 		 * elements are of type long in vtoc (so 64-bit) and of type
2357 		 * int in dk_vtoc (so 32-bit).
2358 		 */
2359 		label.dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
2360 		label.dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
2361 		label.dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2362 		bcopy(vtoc->v_asciilabel, label.dkl_asciilabel,
2363 		    LEN_DKL_ASCII);
2364 		bcopy(vtoc->v_volume, label.dkl_vtoc.v_volume,
2365 		    LEN_DKL_VVOL);
2366 
2367 		/* re-compute checksum */
2368 		label.dkl_magic = DKL_MAGIC;
2369 		label.dkl_cksum = vd_lbl2cksum(&label);
2370 
2371 		/* write label to the disk image */
2372 		if ((rc = vd_file_set_vtoc(vd, &label)) != 0)
2373 			return (rc);
2374 
2375 		break;
2376 
2377 	case DKIOCFLUSHWRITECACHE:
2378 		return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL));
2379 
2380 	case DKIOCGETEFI:
2381 		ASSERT(ioctl_arg != NULL);
2382 		efi = (dk_efi_t *)ioctl_arg;
2383 
2384 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
2385 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
2386 			return (EIO);
2387 
2388 		return (0);
2389 
2390 	case DKIOCSETEFI:
2391 		ASSERT(ioctl_arg != NULL);
2392 		efi = (dk_efi_t *)ioctl_arg;
2393 
2394 		if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
2395 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
2396 			return (EIO);
2397 
2398 		break;
2399 
2400 
2401 	default:
2402 		return (ENOTSUP);
2403 	}
2404 
2405 	ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI);
2406 
2407 	/* label has changed, revalidate the geometry */
2408 	(void) vd_file_validate_geometry(vd);
2409 
2410 	/*
2411 	 * The disk geometry may have changed, so we need to write
2412 	 * the devid (if there is one) so that it is stored at the
2413 	 * right location.
2414 	 */
2415 	if (vd_file_write_devid(vd, vd->file_devid) != 0) {
2416 		PR0("Fail to write devid");
2417 	}
2418 
2419 	return (0);
2420 }
2421 
2422 static int
2423 vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
2424 {
2425 	int rval = 0, status;
2426 
2427 	/*
2428 	 * Call the appropriate function to execute the ioctl depending
2429 	 * on the type of vdisk.
2430 	 */
2431 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
2432 
2433 		/* slice, file or volume exported as a single slice disk */
2434 		status = vd_do_slice_ioctl(vd, cmd, arg);
2435 
2436 	} else if (vd->file) {
2437 
2438 		/* file or volume exported as a full disk */
2439 		status = vd_do_file_ioctl(vd, cmd, arg);
2440 
2441 	} else {
2442 
2443 		/* disk device exported as a full disk */
2444 		status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
2445 		    vd->open_flags | FKIOCTL, kcred, &rval);
2446 	}
2447 
2448 #ifdef DEBUG
2449 	if (rval != 0) {
2450 		PR0("ioctl %x set rval = %d, which is not being returned"
2451 		    " to caller", cmd, rval);
2452 	}
2453 #endif /* DEBUG */
2454 
2455 	return (status);
2456 }
2457 
2458 /*
2459  * Description:
2460  *	This is the function that processes the ioctl requests (farming it
2461  *	out to functions that handle slices, files or whole disks)
2462  *
2463  * Return Values
2464  *     0		- ioctl operation completed successfully
2465  *     != 0		- The LDC error value encountered
2466  *			  (propagated back up the call stack as a task error)
2467  *
2468  * Side Effect
2469  *     sets request->status to the return value of the ioctl function.
2470  */
2471 static int
2472 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
2473 {
2474 	int	status = 0;
2475 	size_t	nbytes = request->nbytes;	/* modifiable copy */
2476 
2477 
2478 	ASSERT(request->slice < vd->nslices);
2479 	PR0("Performing %s", ioctl->operation_name);
2480 
2481 	/* Get data from client and convert, if necessary */
2482 	if (ioctl->copyin != NULL)  {
2483 		ASSERT(nbytes != 0 && buf != NULL);
2484 		PR1("Getting \"arg\" data from client");
2485 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
2486 		    request->cookie, request->ncookies,
2487 		    LDC_COPY_IN)) != 0) {
2488 			PR0("ldc_mem_copy() returned errno %d "
2489 			    "copying from client", status);
2490 			return (status);
2491 		}
2492 
2493 		/* Convert client's data, if necessary */
2494 		if (ioctl->copyin == VD_IDENTITY_IN) {
2495 			/* use client buffer */
2496 			ioctl->arg = buf;
2497 		} else {
2498 			/* convert client vdisk operation data to ioctl data */
2499 			status = (ioctl->copyin)(buf, nbytes,
2500 			    (void *)ioctl->arg);
2501 			if (status != 0) {
2502 				request->status = status;
2503 				return (0);
2504 			}
2505 		}
2506 	}
2507 
2508 	if (ioctl->operation == VD_OP_SCSICMD) {
2509 		struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
2510 
2511 		/* check write permission */
2512 		if (!(vd->open_flags & FWRITE) &&
2513 		    !(uscsi->uscsi_flags & USCSI_READ)) {
2514 			PR0("uscsi fails because backend is opened read-only");
2515 			request->status = EROFS;
2516 			return (0);
2517 		}
2518 	}
2519 
2520 	/*
2521 	 * Send the ioctl to the disk backend.
2522 	 */
2523 	request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
2524 
2525 	if (request->status != 0) {
2526 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
2527 		if (ioctl->operation == VD_OP_SCSICMD &&
2528 		    ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
2529 			/*
2530 			 * USCSICMD has reported an error and the uscsi_status
2531 			 * field is not zero. This means that the SCSI command
2532 			 * has completed but it has an error. So we should
2533 			 * mark the VD operation has succesfully completed
2534 			 * and clients can check the SCSI status field for
2535 			 * SCSI errors.
2536 			 */
2537 			request->status = 0;
2538 		else
2539 			return (0);
2540 	}
2541 
2542 	/* Convert data and send to client, if necessary */
2543 	if (ioctl->copyout != NULL)  {
2544 		ASSERT(nbytes != 0 && buf != NULL);
2545 		PR1("Sending \"arg\" data to client");
2546 
2547 		/* Convert ioctl data to vdisk operation data, if necessary */
2548 		if (ioctl->copyout != VD_IDENTITY_OUT)
2549 			(ioctl->copyout)((void *)ioctl->arg, buf);
2550 
2551 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
2552 		    request->cookie, request->ncookies,
2553 		    LDC_COPY_OUT)) != 0) {
2554 			PR0("ldc_mem_copy() returned errno %d "
2555 			    "copying to client", status);
2556 			return (status);
2557 		}
2558 	}
2559 
2560 	return (status);
2561 }
2562 
2563 #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
2564 
2565 /*
2566  * Description:
2567  *	This generic function is called by the task queue to complete
2568  *	the processing of the tasks. The specific completion function
2569  *	is passed in as a field in the task pointer.
2570  *
2571  * Parameters:
2572  *	arg 	- opaque pointer to structure containing task to be completed
2573  *
2574  * Return Values
2575  *	None
2576  */
2577 static void
2578 vd_complete(void *arg)
2579 {
2580 	vd_task_t	*task = (vd_task_t *)arg;
2581 
2582 	ASSERT(task != NULL);
2583 	ASSERT(task->status == EINPROGRESS);
2584 	ASSERT(task->completef != NULL);
2585 
2586 	task->status = task->completef(task);
2587 	if (task->status)
2588 		PR0("%s: Error %d completing task", __func__, task->status);
2589 
2590 	/* Now notify the vDisk client */
2591 	vd_complete_notify(task);
2592 }
2593 
2594 static int
2595 vd_ioctl(vd_task_t *task)
2596 {
2597 	int			i, status;
2598 	void			*buf = NULL;
2599 	struct dk_geom		dk_geom = {0};
2600 	struct vtoc		vtoc = {0};
2601 	struct dk_efi		dk_efi = {0};
2602 	struct uscsi_cmd	uscsi = {0};
2603 	vd_t			*vd		= task->vd;
2604 	vd_dring_payload_t	*request	= task->request;
2605 	vd_ioctl_t		ioctl[] = {
2606 		/* Command (no-copy) operations */
2607 		{VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
2608 		    DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
2609 		    NULL, NULL, NULL, B_TRUE},
2610 
2611 		/* "Get" (copy-out) operations */
2612 		{VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
2613 		    DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
2614 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE},
2615 		{VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
2616 		    RNDSIZE(vd_geom_t),
2617 		    DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
2618 		    &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
2619 		{VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
2620 		    DKIOCGVTOC, STRINGIZE(DKIOCGVTOC),
2621 		    &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
2622 		{VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
2623 		    DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
2624 		    &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
2625 
2626 		/* "Set" (copy-in) operations */
2627 		{VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
2628 		    DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
2629 		    NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE},
2630 		{VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
2631 		    RNDSIZE(vd_geom_t),
2632 		    DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
2633 		    &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
2634 		{VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
2635 		    DKIOCSVTOC, STRINGIZE(DKIOCSVTOC),
2636 		    &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
2637 		{VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
2638 		    DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
2639 		    &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
2640 
2641 		{VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t),
2642 		    USCSICMD, STRINGIZE(USCSICMD),
2643 		    &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE},
2644 	};
2645 	size_t		nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
2646 
2647 
2648 	ASSERT(vd != NULL);
2649 	ASSERT(request != NULL);
2650 	ASSERT(request->slice < vd->nslices);
2651 
2652 	/*
2653 	 * Determine ioctl corresponding to caller's "operation" and
2654 	 * validate caller's "nbytes"
2655 	 */
2656 	for (i = 0; i < nioctls; i++) {
2657 		if (request->operation == ioctl[i].operation) {
2658 			/* LDC memory operations require 8-byte multiples */
2659 			ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
2660 
2661 			if (request->operation == VD_OP_GET_EFI ||
2662 			    request->operation == VD_OP_SET_EFI ||
2663 			    request->operation == VD_OP_SCSICMD) {
2664 				if (request->nbytes >= ioctl[i].nbytes)
2665 					break;
2666 				PR0("%s:  Expected at least nbytes = %lu, "
2667 				    "got %lu", ioctl[i].operation_name,
2668 				    ioctl[i].nbytes, request->nbytes);
2669 				return (EINVAL);
2670 			}
2671 
2672 			if (request->nbytes != ioctl[i].nbytes) {
2673 				PR0("%s:  Expected nbytes = %lu, got %lu",
2674 				    ioctl[i].operation_name, ioctl[i].nbytes,
2675 				    request->nbytes);
2676 				return (EINVAL);
2677 			}
2678 
2679 			break;
2680 		}
2681 	}
2682 	ASSERT(i < nioctls);	/* because "operation" already validated */
2683 
2684 	if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
2685 		PR0("%s fails because backend is opened read-only",
2686 		    ioctl[i].operation_name);
2687 		request->status = EROFS;
2688 		return (0);
2689 	}
2690 
2691 	if (request->nbytes)
2692 		buf = kmem_zalloc(request->nbytes, KM_SLEEP);
2693 	status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
2694 	if (request->nbytes)
2695 		kmem_free(buf, request->nbytes);
2696 
2697 	return (status);
2698 }
2699 
2700 static int
2701 vd_get_devid(vd_task_t *task)
2702 {
2703 	vd_t *vd = task->vd;
2704 	vd_dring_payload_t *request = task->request;
2705 	vd_devid_t *vd_devid;
2706 	impl_devid_t *devid;
2707 	int status, bufid_len, devid_len, len, sz;
2708 	int bufbytes;
2709 
2710 	PR1("Get Device ID, nbytes=%ld", request->nbytes);
2711 
2712 	if (vd->file) {
2713 		if (vd->file_devid == NULL) {
2714 			PR2("No Device ID");
2715 			request->status = ENOENT;
2716 			return (0);
2717 		} else {
2718 			sz = ddi_devid_sizeof(vd->file_devid);
2719 			devid = kmem_alloc(sz, KM_SLEEP);
2720 			bcopy(vd->file_devid, devid, sz);
2721 		}
2722 	} else {
2723 		if (ddi_lyr_get_devid(vd->dev[request->slice],
2724 		    (ddi_devid_t *)&devid) != DDI_SUCCESS) {
2725 			PR2("No Device ID");
2726 			request->status = ENOENT;
2727 			return (0);
2728 		}
2729 	}
2730 
2731 	bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
2732 	devid_len = DEVID_GETLEN(devid);
2733 
2734 	/*
2735 	 * Save the buffer size here for use in deallocation.
2736 	 * The actual number of bytes copied is returned in
2737 	 * the 'nbytes' field of the request structure.
2738 	 */
2739 	bufbytes = request->nbytes;
2740 
2741 	vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
2742 	vd_devid->length = devid_len;
2743 	vd_devid->type = DEVID_GETTYPE(devid);
2744 
2745 	len = (devid_len > bufid_len)? bufid_len : devid_len;
2746 
2747 	bcopy(devid->did_id, vd_devid->id, len);
2748 
2749 	request->status = 0;
2750 
2751 	/* LDC memory operations require 8-byte multiples */
2752 	ASSERT(request->nbytes % sizeof (uint64_t) == 0);
2753 
2754 	if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
2755 	    &request->nbytes, request->cookie, request->ncookies,
2756 	    LDC_COPY_OUT)) != 0) {
2757 		PR0("ldc_mem_copy() returned errno %d copying to client",
2758 		    status);
2759 	}
2760 	PR1("post mem_copy: nbytes=%ld", request->nbytes);
2761 
2762 	kmem_free(vd_devid, bufbytes);
2763 	ddi_devid_free((ddi_devid_t)devid);
2764 
2765 	return (status);
2766 }
2767 
2768 static int
2769 vd_scsi_reset(vd_t *vd)
2770 {
2771 	int rval, status;
2772 	struct uscsi_cmd uscsi = { 0 };
2773 
2774 	uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET;
2775 	uscsi.uscsi_timeout = vd_scsi_rdwr_timeout;
2776 
2777 	status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi,
2778 	    (vd->open_flags | FKIOCTL), kcred, &rval);
2779 
2780 	return (status);
2781 }
2782 
2783 static int
2784 vd_reset(vd_task_t *task)
2785 {
2786 	vd_t *vd = task->vd;
2787 	vd_dring_payload_t *request = task->request;
2788 
2789 	ASSERT(request->operation == VD_OP_RESET);
2790 	ASSERT(vd->scsi);
2791 
2792 	PR0("Performing VD_OP_RESET");
2793 
2794 	if (request->nbytes != 0) {
2795 		PR0("VD_OP_RESET:  Expected nbytes = 0, got %lu",
2796 		    request->nbytes);
2797 		return (EINVAL);
2798 	}
2799 
2800 	request->status = vd_scsi_reset(vd);
2801 
2802 	return (0);
2803 }
2804 
2805 static int
2806 vd_get_capacity(vd_task_t *task)
2807 {
2808 	int rv;
2809 	size_t nbytes;
2810 	vd_t *vd = task->vd;
2811 	vd_dring_payload_t *request = task->request;
2812 	vd_capacity_t vd_cap = { 0 };
2813 
2814 	ASSERT(request->operation == VD_OP_GET_CAPACITY);
2815 	ASSERT(vd->scsi);
2816 
2817 	PR0("Performing VD_OP_GET_CAPACITY");
2818 
2819 	nbytes = request->nbytes;
2820 
2821 	if (nbytes != RNDSIZE(vd_capacity_t)) {
2822 		PR0("VD_OP_GET_CAPACITY:  Expected nbytes = %lu, got %lu",
2823 		    RNDSIZE(vd_capacity_t), nbytes);
2824 		return (EINVAL);
2825 	}
2826 
2827 	if (vd->vdisk_size == VD_SIZE_UNKNOWN) {
2828 		if (vd_setup_mediainfo(vd) != 0)
2829 			ASSERT(vd->vdisk_size == VD_SIZE_UNKNOWN);
2830 	}
2831 
2832 	ASSERT(vd->vdisk_size != 0);
2833 
2834 	request->status = 0;
2835 
2836 	vd_cap.vdisk_block_size = vd->vdisk_block_size;
2837 	vd_cap.vdisk_size = vd->vdisk_size;
2838 
2839 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes,
2840 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
2841 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
2842 		return (rv);
2843 	}
2844 
2845 	return (0);
2846 }
2847 
2848 static int
2849 vd_get_access(vd_task_t *task)
2850 {
2851 	uint64_t access;
2852 	int rv, rval = 0;
2853 	size_t nbytes;
2854 	vd_t *vd = task->vd;
2855 	vd_dring_payload_t *request = task->request;
2856 
2857 	ASSERT(request->operation == VD_OP_GET_ACCESS);
2858 	ASSERT(vd->scsi);
2859 
2860 	PR0("Performing VD_OP_GET_ACCESS");
2861 
2862 	nbytes = request->nbytes;
2863 
2864 	if (nbytes != sizeof (uint64_t)) {
2865 		PR0("VD_OP_GET_ACCESS:  Expected nbytes = %lu, got %lu",
2866 		    sizeof (uint64_t), nbytes);
2867 		return (EINVAL);
2868 	}
2869 
2870 	request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS,
2871 	    NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
2872 
2873 	if (request->status != 0)
2874 		return (0);
2875 
2876 	access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED;
2877 
2878 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes,
2879 	    request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
2880 		PR0("ldc_mem_copy() returned errno %d copying to client", rv);
2881 		return (rv);
2882 	}
2883 
2884 	return (0);
2885 }
2886 
2887 static int
2888 vd_set_access(vd_task_t *task)
2889 {
2890 	uint64_t flags;
2891 	int rv, rval;
2892 	size_t nbytes;
2893 	vd_t *vd = task->vd;
2894 	vd_dring_payload_t *request = task->request;
2895 
2896 	ASSERT(request->operation == VD_OP_SET_ACCESS);
2897 	ASSERT(vd->scsi);
2898 
2899 	nbytes = request->nbytes;
2900 
2901 	if (nbytes != sizeof (uint64_t)) {
2902 		PR0("VD_OP_SET_ACCESS:  Expected nbytes = %lu, got %lu",
2903 		    sizeof (uint64_t), nbytes);
2904 		return (EINVAL);
2905 	}
2906 
2907 	if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes,
2908 	    request->cookie, request->ncookies, LDC_COPY_IN)) != 0) {
2909 		PR0("ldc_mem_copy() returned errno %d copying from client", rv);
2910 		return (rv);
2911 	}
2912 
2913 	if (flags == VD_ACCESS_SET_CLEAR) {
2914 		PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
2915 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2916 		    MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred,
2917 		    &rval);
2918 		if (request->status == 0)
2919 			vd->ownership = B_FALSE;
2920 		return (0);
2921 	}
2922 
2923 	/*
2924 	 * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
2925 	 * when the EXCLUSIVE flag is set.
2926 	 */
2927 	if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
2928 		PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags);
2929 		request->status = EINVAL;
2930 		return (0);
2931 	}
2932 
2933 	switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) {
2934 
2935 	case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE:
2936 		/*
2937 		 * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
2938 		 * acquire exclusive access rights, preserve them and we
2939 		 * can use preemption. So we can use the MHIOCTKNOWN ioctl.
2940 		 */
2941 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
2942 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2943 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
2944 		break;
2945 
2946 	case VD_ACCESS_SET_PRESERVE:
2947 		/*
2948 		 * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
2949 		 * access rights and preserve them, but not preempt any other
2950 		 * host. So we need to use the MHIOCTKOWN ioctl to enable the
2951 		 * "preserve" feature but we can not called it directly
2952 		 * because it uses preemption. So before that, we use the
2953 		 * MHIOCQRESERVE ioctl to ensure we can get exclusive rights
2954 		 * without preempting anyone.
2955 		 */
2956 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
2957 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2958 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
2959 		    &rval);
2960 		if (request->status != 0)
2961 			break;
2962 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2963 		    MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
2964 		break;
2965 
2966 	case VD_ACCESS_SET_PREEMPT:
2967 		/*
2968 		 * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
2969 		 * access rights and we can use preemption. So we try to do
2970 		 * a SCSI reservation, if it fails we reset the disk to clear
2971 		 * any reservation and we try to reserve again.
2972 		 */
2973 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
2974 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2975 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
2976 		    &rval);
2977 		if (request->status == 0)
2978 			break;
2979 
2980 		/* reset the disk */
2981 		(void) vd_scsi_reset(vd);
2982 
2983 		/* try again even if the reset has failed */
2984 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2985 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
2986 		    &rval);
2987 		break;
2988 
2989 	case 0:
2990 		/* Flag EXCLUSIVE only. Just issue a SCSI reservation */
2991 		PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
2992 		request->status = ldi_ioctl(vd->ldi_handle[request->slice],
2993 		    MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
2994 		    &rval);
2995 		break;
2996 	}
2997 
2998 	if (request->status == 0)
2999 		vd->ownership = B_TRUE;
3000 	else
3001 		PR0("VD_OP_SET_ACCESS: error %d", request->status);
3002 
3003 	return (0);
3004 }
3005 
3006 static void
3007 vd_reset_access(vd_t *vd)
3008 {
3009 	int status, rval;
3010 
3011 	if (vd->file || !vd->ownership)
3012 		return;
3013 
3014 	PR0("Releasing disk ownership");
3015 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
3016 	    (vd->open_flags | FKIOCTL), kcred, &rval);
3017 
3018 	/*
3019 	 * An EACCES failure means that there is a reservation conflict,
3020 	 * so we are not the owner of the disk anymore.
3021 	 */
3022 	if (status == 0 || status == EACCES) {
3023 		vd->ownership = B_FALSE;
3024 		return;
3025 	}
3026 
3027 	PR0("Fail to release ownership, error %d", status);
3028 
3029 	/*
3030 	 * We have failed to release the ownership, try to reset the disk
3031 	 * to release reservations.
3032 	 */
3033 	PR0("Resetting disk");
3034 	status = vd_scsi_reset(vd);
3035 
3036 	if (status != 0)
3037 		PR0("Fail to reset disk, error %d", status);
3038 
3039 	/* whatever the result of the reset is, we try the release again */
3040 	status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
3041 	    (vd->open_flags | FKIOCTL), kcred, &rval);
3042 
3043 	if (status == 0 || status == EACCES) {
3044 		vd->ownership = B_FALSE;
3045 		return;
3046 	}
3047 
3048 	PR0("Fail to release ownership, error %d", status);
3049 
3050 	/*
3051 	 * At this point we have done our best to try to reset the
3052 	 * access rights to the disk and we don't know if we still
3053 	 * own a reservation and if any mechanism to preserve the
3054 	 * ownership is still in place. The ultimate solution would
3055 	 * be to reset the system but this is usually not what we
3056 	 * want to happen.
3057 	 */
3058 
3059 	if (vd_reset_access_failure == A_REBOOT) {
3060 		cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG
3061 		    ", rebooting the system", vd->device_path);
3062 		(void) uadmin(A_SHUTDOWN, AD_BOOT, NULL);
3063 	} else if (vd_reset_access_failure == A_DUMP) {
3064 		panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
3065 	}
3066 
3067 	cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
3068 }
3069 
3070 /*
3071  * Define the supported operations once the functions for performing them have
3072  * been defined
3073  */
3074 static const vds_operation_t	vds_operation[] = {
3075 #define	X(_s)	#_s, _s
3076 	{X(VD_OP_BREAD),	vd_start_bio,	vd_complete_bio},
3077 	{X(VD_OP_BWRITE),	vd_start_bio,	vd_complete_bio},
3078 	{X(VD_OP_FLUSH),	vd_ioctl,	NULL},
3079 	{X(VD_OP_GET_WCE),	vd_ioctl,	NULL},
3080 	{X(VD_OP_SET_WCE),	vd_ioctl,	NULL},
3081 	{X(VD_OP_GET_VTOC),	vd_ioctl,	NULL},
3082 	{X(VD_OP_SET_VTOC),	vd_ioctl,	NULL},
3083 	{X(VD_OP_GET_DISKGEOM),	vd_ioctl,	NULL},
3084 	{X(VD_OP_SET_DISKGEOM),	vd_ioctl,	NULL},
3085 	{X(VD_OP_GET_EFI),	vd_ioctl,	NULL},
3086 	{X(VD_OP_SET_EFI),	vd_ioctl,	NULL},
3087 	{X(VD_OP_GET_DEVID),	vd_get_devid,	NULL},
3088 	{X(VD_OP_SCSICMD),	vd_ioctl,	NULL},
3089 	{X(VD_OP_RESET),	vd_reset,	NULL},
3090 	{X(VD_OP_GET_CAPACITY),	vd_get_capacity, NULL},
3091 	{X(VD_OP_SET_ACCESS),	vd_set_access,	NULL},
3092 	{X(VD_OP_GET_ACCESS),	vd_get_access,	NULL},
3093 #undef	X
3094 };
3095 
3096 static const size_t	vds_noperations =
3097 	(sizeof (vds_operation))/(sizeof (vds_operation[0]));
3098 
3099 /*
3100  * Process a task specifying a client I/O request
3101  *
3102  * Parameters:
3103  *	task 		- structure containing the request sent from client
3104  *
3105  * Return Value
3106  *	0	- success
3107  *	ENOTSUP	- Unknown/Unsupported VD_OP_XXX operation
3108  *	EINVAL	- Invalid disk slice
3109  *	!= 0	- some other non-zero return value from start function
3110  */
3111 static int
3112 vd_do_process_task(vd_task_t *task)
3113 {
3114 	int			i;
3115 	vd_t			*vd		= task->vd;
3116 	vd_dring_payload_t	*request	= task->request;
3117 
3118 	ASSERT(vd != NULL);
3119 	ASSERT(request != NULL);
3120 
3121 	/* Find the requested operation */
3122 	for (i = 0; i < vds_noperations; i++) {
3123 		if (request->operation == vds_operation[i].operation) {
3124 			/* all operations should have a start func */
3125 			ASSERT(vds_operation[i].start != NULL);
3126 
3127 			task->completef = vds_operation[i].complete;
3128 			break;
3129 		}
3130 	}
3131 
3132 	/*
3133 	 * We need to check that the requested operation is permitted
3134 	 * for the particular client that sent it or that the loop above
3135 	 * did not complete without finding the operation type (indicating
3136 	 * that the requested operation is unknown/unimplemented)
3137 	 */
3138 	if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
3139 	    (i == vds_noperations)) {
3140 		PR0("Unsupported operation %u", request->operation);
3141 		request->status = ENOTSUP;
3142 		return (0);
3143 	}
3144 
3145 	/* Range-check slice */
3146 	if (request->slice >= vd->nslices &&
3147 	    (vd->vdisk_type != VD_DISK_TYPE_DISK ||
3148 	    request->slice != VD_SLICE_NONE)) {
3149 		PR0("Invalid \"slice\" %u (max %u) for virtual disk",
3150 		    request->slice, (vd->nslices - 1));
3151 		return (EINVAL);
3152 	}
3153 
3154 	/*
3155 	 * Call the function pointer that starts the operation.
3156 	 */
3157 	return (vds_operation[i].start(task));
3158 }
3159 
3160 /*
3161  * Description:
3162  *	This function is called by both the in-band and descriptor ring
3163  *	message processing functions paths to actually execute the task
3164  *	requested by the vDisk client. It in turn calls its worker
3165  *	function, vd_do_process_task(), to carry our the request.
3166  *
3167  *	Any transport errors (e.g. LDC errors, vDisk protocol errors) are
3168  *	saved in the 'status' field of the task and are propagated back
3169  *	up the call stack to trigger a NACK
3170  *
3171  *	Any request errors (e.g. ENOTTY from an ioctl) are saved in
3172  *	the 'status' field of the request and result in an ACK being sent
3173  *	by the completion handler.
3174  *
3175  * Parameters:
3176  *	task 		- structure containing the request sent from client
3177  *
3178  * Return Value
3179  *	0		- successful synchronous request.
3180  *	!= 0		- transport error (e.g. LDC errors, vDisk protocol)
3181  *	EINPROGRESS	- task will be finished in a completion handler
3182  */
3183 static int
3184 vd_process_task(vd_task_t *task)
3185 {
3186 	vd_t	*vd = task->vd;
3187 	int	status;
3188 
3189 	DTRACE_PROBE1(task__start, vd_task_t *, task);
3190 
3191 	task->status =  vd_do_process_task(task);
3192 
3193 	/*
3194 	 * If the task processing function returned EINPROGRESS indicating
3195 	 * that the task needs completing then schedule a taskq entry to
3196 	 * finish it now.
3197 	 *
3198 	 * Otherwise the task processing function returned either zero
3199 	 * indicating that the task was finished in the start function (and we
3200 	 * don't need to wait in a completion function) or the start function
3201 	 * returned an error - in both cases all that needs to happen is the
3202 	 * notification to the vDisk client higher up the call stack.
3203 	 * If the task was using a Descriptor Ring, we need to mark it as done
3204 	 * at this stage.
3205 	 */
3206 	if (task->status == EINPROGRESS) {
3207 		/* Queue a task to complete the operation */
3208 		(void) ddi_taskq_dispatch(vd->completionq, vd_complete,
3209 		    task, DDI_SLEEP);
3210 
3211 	} else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
3212 		/* Update the dring element if it's a dring client */
3213 		status = vd_mark_elem_done(vd, task->index,
3214 		    task->request->status, task->request->nbytes);
3215 		if (status == ECONNRESET)
3216 			vd_mark_in_reset(vd);
3217 	}
3218 
3219 	return (task->status);
3220 }
3221 
3222 /*
3223  * Return true if the "type", "subtype", and "env" fields of the "tag" first
3224  * argument match the corresponding remaining arguments; otherwise, return false
3225  */
3226 boolean_t
3227 vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
3228 {
3229 	return ((tag->vio_msgtype == type) &&
3230 	    (tag->vio_subtype == subtype) &&
3231 	    (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
3232 }
3233 
3234 /*
3235  * Check whether the major/minor version specified in "ver_msg" is supported
3236  * by this server.
3237  */
3238 static boolean_t
3239 vds_supported_version(vio_ver_msg_t *ver_msg)
3240 {
3241 	for (int i = 0; i < vds_num_versions; i++) {
3242 		ASSERT(vds_version[i].major > 0);
3243 		ASSERT((i == 0) ||
3244 		    (vds_version[i].major < vds_version[i-1].major));
3245 
3246 		/*
3247 		 * If the major versions match, adjust the minor version, if
3248 		 * necessary, down to the highest value supported by this
3249 		 * server and return true so this message will get "ack"ed;
3250 		 * the client should also support all minor versions lower
3251 		 * than the value it sent
3252 		 */
3253 		if (ver_msg->ver_major == vds_version[i].major) {
3254 			if (ver_msg->ver_minor > vds_version[i].minor) {
3255 				PR0("Adjusting minor version from %u to %u",
3256 				    ver_msg->ver_minor, vds_version[i].minor);
3257 				ver_msg->ver_minor = vds_version[i].minor;
3258 			}
3259 			return (B_TRUE);
3260 		}
3261 
3262 		/*
3263 		 * If the message contains a higher major version number, set
3264 		 * the message's major/minor versions to the current values
3265 		 * and return false, so this message will get "nack"ed with
3266 		 * these values, and the client will potentially try again
3267 		 * with the same or a lower version
3268 		 */
3269 		if (ver_msg->ver_major > vds_version[i].major) {
3270 			ver_msg->ver_major = vds_version[i].major;
3271 			ver_msg->ver_minor = vds_version[i].minor;
3272 			return (B_FALSE);
3273 		}
3274 
3275 		/*
3276 		 * Otherwise, the message's major version is less than the
3277 		 * current major version, so continue the loop to the next
3278 		 * (lower) supported version
3279 		 */
3280 	}
3281 
3282 	/*
3283 	 * No common version was found; "ground" the version pair in the
3284 	 * message to terminate negotiation
3285 	 */
3286 	ver_msg->ver_major = 0;
3287 	ver_msg->ver_minor = 0;
3288 	return (B_FALSE);
3289 }
3290 
3291 /*
3292  * Process a version message from a client.  vds expects to receive version
3293  * messages from clients seeking service, but never issues version messages
3294  * itself; therefore, vds can ACK or NACK client version messages, but does
3295  * not expect to receive version-message ACKs or NACKs (and will treat such
3296  * messages as invalid).
3297  */
3298 static int
3299 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
3300 {
3301 	vio_ver_msg_t	*ver_msg = (vio_ver_msg_t *)msg;
3302 
3303 
3304 	ASSERT(msglen >= sizeof (msg->tag));
3305 
3306 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
3307 	    VIO_VER_INFO)) {
3308 		return (ENOMSG);	/* not a version message */
3309 	}
3310 
3311 	if (msglen != sizeof (*ver_msg)) {
3312 		PR0("Expected %lu-byte version message; "
3313 		    "received %lu bytes", sizeof (*ver_msg), msglen);
3314 		return (EBADMSG);
3315 	}
3316 
3317 	if (ver_msg->dev_class != VDEV_DISK) {
3318 		PR0("Expected device class %u (disk); received %u",
3319 		    VDEV_DISK, ver_msg->dev_class);
3320 		return (EBADMSG);
3321 	}
3322 
3323 	/*
3324 	 * We're talking to the expected kind of client; set our device class
3325 	 * for "ack/nack" back to the client
3326 	 */
3327 	ver_msg->dev_class = VDEV_DISK_SERVER;
3328 
3329 	/*
3330 	 * Check whether the (valid) version message specifies a version
3331 	 * supported by this server.  If the version is not supported, return
3332 	 * EBADMSG so the message will get "nack"ed; vds_supported_version()
3333 	 * will have updated the message with a supported version for the
3334 	 * client to consider
3335 	 */
3336 	if (!vds_supported_version(ver_msg))
3337 		return (EBADMSG);
3338 
3339 
3340 	/*
3341 	 * A version has been agreed upon; use the client's SID for
3342 	 * communication on this channel now
3343 	 */
3344 	ASSERT(!(vd->initialized & VD_SID));
3345 	vd->sid = ver_msg->tag.vio_sid;
3346 	vd->initialized |= VD_SID;
3347 
3348 	/*
3349 	 * Store the negotiated major and minor version values in the "vd" data
3350 	 * structure so that we can check if certain operations are supported
3351 	 * by the client.
3352 	 */
3353 	vd->version.major = ver_msg->ver_major;
3354 	vd->version.minor = ver_msg->ver_minor;
3355 
3356 	PR0("Using major version %u, minor version %u",
3357 	    ver_msg->ver_major, ver_msg->ver_minor);
3358 	return (0);
3359 }
3360 
3361 static void
3362 vd_set_exported_operations(vd_t *vd)
3363 {
3364 	vd->operations = 0;	/* clear field */
3365 
3366 	/*
3367 	 * We need to check from the highest version supported to the
3368 	 * lowest because versions with a higher minor number implicitly
3369 	 * support versions with a lower minor number.
3370 	 */
3371 	if (vio_ver_is_supported(vd->version, 1, 1)) {
3372 		ASSERT(vd->open_flags & FREAD);
3373 		vd->operations |= VD_OP_MASK_READ;
3374 
3375 		if (vd->open_flags & FWRITE)
3376 			vd->operations |= VD_OP_MASK_WRITE;
3377 
3378 		if (vd->scsi)
3379 			vd->operations |= VD_OP_MASK_SCSI;
3380 
3381 		if (vd->file && vd_file_is_iso_image(vd)) {
3382 			/*
3383 			 * can't write to ISO images, make sure that write
3384 			 * support is not set in case administrator did not
3385 			 * use "options=ro" when doing an ldm add-vdsdev
3386 			 */
3387 			vd->operations &= ~VD_OP_MASK_WRITE;
3388 		}
3389 	} else if (vio_ver_is_supported(vd->version, 1, 0)) {
3390 		vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
3391 	}
3392 
3393 	/* we should have already agreed on a version */
3394 	ASSERT(vd->operations != 0);
3395 }
3396 
3397 static int
3398 vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
3399 {
3400 	vd_attr_msg_t	*attr_msg = (vd_attr_msg_t *)msg;
3401 	int		status, retry = 0;
3402 
3403 
3404 	ASSERT(msglen >= sizeof (msg->tag));
3405 
3406 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
3407 	    VIO_ATTR_INFO)) {
3408 		PR0("Message is not an attribute message");
3409 		return (ENOMSG);
3410 	}
3411 
3412 	if (msglen != sizeof (*attr_msg)) {
3413 		PR0("Expected %lu-byte attribute message; "
3414 		    "received %lu bytes", sizeof (*attr_msg), msglen);
3415 		return (EBADMSG);
3416 	}
3417 
3418 	if (attr_msg->max_xfer_sz == 0) {
3419 		PR0("Received maximum transfer size of 0 from client");
3420 		return (EBADMSG);
3421 	}
3422 
3423 	if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
3424 	    (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) {
3425 		PR0("Client requested unsupported transfer mode");
3426 		return (EBADMSG);
3427 	}
3428 
3429 	/*
3430 	 * check if the underlying disk is ready, if not try accessing
3431 	 * the device again. Open the vdisk device and extract info
3432 	 * about it, as this is needed to respond to the attr info msg
3433 	 */
3434 	if ((vd->initialized & VD_DISK_READY) == 0) {
3435 		PR0("Retry setting up disk (%s)", vd->device_path);
3436 		do {
3437 			status = vd_setup_vd(vd);
3438 			if (status != EAGAIN || ++retry > vds_dev_retries)
3439 				break;
3440 
3441 			/* incremental delay */
3442 			delay(drv_usectohz(vds_dev_delay));
3443 
3444 			/* if vdisk is no longer enabled - return error */
3445 			if (!vd_enabled(vd))
3446 				return (ENXIO);
3447 
3448 		} while (status == EAGAIN);
3449 
3450 		if (status)
3451 			return (ENXIO);
3452 
3453 		vd->initialized |= VD_DISK_READY;
3454 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
3455 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
3456 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
3457 		    (vd->pseudo ? "yes" : "no"),
3458 		    (vd->file ? "yes" : "no"),
3459 		    vd->nslices);
3460 	}
3461 
3462 	/* Success:  valid message and transfer mode */
3463 	vd->xfer_mode = attr_msg->xfer_mode;
3464 
3465 	if (vd->xfer_mode == VIO_DESC_MODE) {
3466 
3467 		/*
3468 		 * The vd_dring_inband_msg_t contains one cookie; need room
3469 		 * for up to n-1 more cookies, where "n" is the number of full
3470 		 * pages plus possibly one partial page required to cover
3471 		 * "max_xfer_sz".  Add room for one more cookie if
3472 		 * "max_xfer_sz" isn't an integral multiple of the page size.
3473 		 * Must first get the maximum transfer size in bytes.
3474 		 */
3475 		size_t	max_xfer_bytes = attr_msg->vdisk_block_size ?
3476 		    attr_msg->vdisk_block_size*attr_msg->max_xfer_sz :
3477 		    attr_msg->max_xfer_sz;
3478 		size_t	max_inband_msglen =
3479 		    sizeof (vd_dring_inband_msg_t) +
3480 		    ((max_xfer_bytes/PAGESIZE +
3481 		    ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
3482 		    (sizeof (ldc_mem_cookie_t)));
3483 
3484 		/*
3485 		 * Set the maximum expected message length to
3486 		 * accommodate in-band-descriptor messages with all
3487 		 * their cookies
3488 		 */
3489 		vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
3490 
3491 		/*
3492 		 * Initialize the data structure for processing in-band I/O
3493 		 * request descriptors
3494 		 */
3495 		vd->inband_task.vd	= vd;
3496 		vd->inband_task.msg	= kmem_alloc(vd->max_msglen, KM_SLEEP);
3497 		vd->inband_task.index	= 0;
3498 		vd->inband_task.type	= VD_FINAL_RANGE_TASK;	/* range == 1 */
3499 	}
3500 
3501 	/* Return the device's block size and max transfer size to the client */
3502 	attr_msg->vdisk_block_size	= vd->vdisk_block_size;
3503 	attr_msg->max_xfer_sz		= vd->max_xfer_sz;
3504 
3505 	attr_msg->vdisk_size = vd->vdisk_size;
3506 	attr_msg->vdisk_type = vd->vdisk_type;
3507 	attr_msg->vdisk_media = vd->vdisk_media;
3508 
3509 	/* Discover and save the list of supported VD_OP_XXX operations */
3510 	vd_set_exported_operations(vd);
3511 	attr_msg->operations = vd->operations;
3512 
3513 	PR0("%s", VD_CLIENT(vd));
3514 
3515 	ASSERT(vd->dring_task == NULL);
3516 
3517 	return (0);
3518 }
3519 
3520 static int
3521 vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
3522 {
3523 	int			status;
3524 	size_t			expected;
3525 	ldc_mem_info_t		dring_minfo;
3526 	vio_dring_reg_msg_t	*reg_msg = (vio_dring_reg_msg_t *)msg;
3527 
3528 
3529 	ASSERT(msglen >= sizeof (msg->tag));
3530 
3531 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
3532 	    VIO_DRING_REG)) {
3533 		PR0("Message is not a register-dring message");
3534 		return (ENOMSG);
3535 	}
3536 
3537 	if (msglen < sizeof (*reg_msg)) {
3538 		PR0("Expected at least %lu-byte register-dring message; "
3539 		    "received %lu bytes", sizeof (*reg_msg), msglen);
3540 		return (EBADMSG);
3541 	}
3542 
3543 	expected = sizeof (*reg_msg) +
3544 	    (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
3545 	if (msglen != expected) {
3546 		PR0("Expected %lu-byte register-dring message; "
3547 		    "received %lu bytes", expected, msglen);
3548 		return (EBADMSG);
3549 	}
3550 
3551 	if (vd->initialized & VD_DRING) {
3552 		PR0("A dring was previously registered; only support one");
3553 		return (EBADMSG);
3554 	}
3555 
3556 	if (reg_msg->num_descriptors > INT32_MAX) {
3557 		PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
3558 		    reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
3559 		return (EBADMSG);
3560 	}
3561 
3562 	if (reg_msg->ncookies != 1) {
3563 		/*
3564 		 * In addition to fixing the assertion in the success case
3565 		 * below, supporting drings which require more than one
3566 		 * "cookie" requires increasing the value of vd->max_msglen
3567 		 * somewhere in the code path prior to receiving the message
3568 		 * which results in calling this function.  Note that without
3569 		 * making this change, the larger message size required to
3570 		 * accommodate multiple cookies cannot be successfully
3571 		 * received, so this function will not even get called.
3572 		 * Gracefully accommodating more dring cookies might
3573 		 * reasonably demand exchanging an additional attribute or
3574 		 * making a minor protocol adjustment
3575 		 */
3576 		PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
3577 		return (EBADMSG);
3578 	}
3579 
3580 	status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
3581 	    reg_msg->ncookies, reg_msg->num_descriptors,
3582 	    reg_msg->descriptor_size, LDC_DIRECT_MAP, &vd->dring_handle);
3583 	if (status != 0) {
3584 		PR0("ldc_mem_dring_map() returned errno %d", status);
3585 		return (status);
3586 	}
3587 
3588 	/*
3589 	 * To remove the need for this assertion, must call
3590 	 * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
3591 	 * successful call to ldc_mem_dring_map()
3592 	 */
3593 	ASSERT(reg_msg->ncookies == 1);
3594 
3595 	if ((status =
3596 	    ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
3597 		PR0("ldc_mem_dring_info() returned errno %d", status);
3598 		if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
3599 			PR0("ldc_mem_dring_unmap() returned errno %d", status);
3600 		return (status);
3601 	}
3602 
3603 	if (dring_minfo.vaddr == NULL) {
3604 		PR0("Descriptor ring virtual address is NULL");
3605 		return (ENXIO);
3606 	}
3607 
3608 
3609 	/* Initialize for valid message and mapped dring */
3610 	PR1("descriptor size = %u, dring length = %u",
3611 	    vd->descriptor_size, vd->dring_len);
3612 	vd->initialized |= VD_DRING;
3613 	vd->dring_ident = 1;	/* "There Can Be Only One" */
3614 	vd->dring = dring_minfo.vaddr;
3615 	vd->descriptor_size = reg_msg->descriptor_size;
3616 	vd->dring_len = reg_msg->num_descriptors;
3617 	reg_msg->dring_ident = vd->dring_ident;
3618 
3619 	/*
3620 	 * Allocate and initialize a "shadow" array of data structures for
3621 	 * tasks to process I/O requests in dring elements
3622 	 */
3623 	vd->dring_task =
3624 	    kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
3625 	for (int i = 0; i < vd->dring_len; i++) {
3626 		vd->dring_task[i].vd		= vd;
3627 		vd->dring_task[i].index		= i;
3628 		vd->dring_task[i].request	= &VD_DRING_ELEM(i)->payload;
3629 
3630 		status = ldc_mem_alloc_handle(vd->ldc_handle,
3631 		    &(vd->dring_task[i].mhdl));
3632 		if (status) {
3633 			PR0("ldc_mem_alloc_handle() returned err %d ", status);
3634 			return (ENXIO);
3635 		}
3636 
3637 		vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
3638 	}
3639 
3640 	return (0);
3641 }
3642 
3643 static int
3644 vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
3645 {
3646 	vio_dring_unreg_msg_t	*unreg_msg = (vio_dring_unreg_msg_t *)msg;
3647 
3648 
3649 	ASSERT(msglen >= sizeof (msg->tag));
3650 
3651 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
3652 	    VIO_DRING_UNREG)) {
3653 		PR0("Message is not an unregister-dring message");
3654 		return (ENOMSG);
3655 	}
3656 
3657 	if (msglen != sizeof (*unreg_msg)) {
3658 		PR0("Expected %lu-byte unregister-dring message; "
3659 		    "received %lu bytes", sizeof (*unreg_msg), msglen);
3660 		return (EBADMSG);
3661 	}
3662 
3663 	if (unreg_msg->dring_ident != vd->dring_ident) {
3664 		PR0("Expected dring ident %lu; received %lu",
3665 		    vd->dring_ident, unreg_msg->dring_ident);
3666 		return (EBADMSG);
3667 	}
3668 
3669 	return (0);
3670 }
3671 
3672 static int
3673 process_rdx_msg(vio_msg_t *msg, size_t msglen)
3674 {
3675 	ASSERT(msglen >= sizeof (msg->tag));
3676 
3677 	if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
3678 		PR0("Message is not an RDX message");
3679 		return (ENOMSG);
3680 	}
3681 
3682 	if (msglen != sizeof (vio_rdx_msg_t)) {
3683 		PR0("Expected %lu-byte RDX message; received %lu bytes",
3684 		    sizeof (vio_rdx_msg_t), msglen);
3685 		return (EBADMSG);
3686 	}
3687 
3688 	PR0("Valid RDX message");
3689 	return (0);
3690 }
3691 
3692 static int
3693 vd_check_seq_num(vd_t *vd, uint64_t seq_num)
3694 {
3695 	if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
3696 		PR0("Received seq_num %lu; expected %lu",
3697 		    seq_num, (vd->seq_num + 1));
3698 		PR0("initiating soft reset");
3699 		vd_need_reset(vd, B_FALSE);
3700 		return (1);
3701 	}
3702 
3703 	vd->seq_num = seq_num;
3704 	vd->initialized |= VD_SEQ_NUM;	/* superfluous after first time... */
3705 	return (0);
3706 }
3707 
3708 /*
3709  * Return the expected size of an inband-descriptor message with all the
3710  * cookies it claims to include
3711  */
3712 static size_t
3713 expected_inband_size(vd_dring_inband_msg_t *msg)
3714 {
3715 	return ((sizeof (*msg)) +
3716 	    (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
3717 }
3718 
3719 /*
3720  * Process an in-band descriptor message:  used with clients like OBP, with
3721  * which vds exchanges descriptors within VIO message payloads, rather than
3722  * operating on them within a descriptor ring
3723  */
3724 static int
3725 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
3726 {
3727 	size_t			expected;
3728 	vd_dring_inband_msg_t	*desc_msg = (vd_dring_inband_msg_t *)msg;
3729 
3730 
3731 	ASSERT(msglen >= sizeof (msg->tag));
3732 
3733 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
3734 	    VIO_DESC_DATA)) {
3735 		PR1("Message is not an in-band-descriptor message");
3736 		return (ENOMSG);
3737 	}
3738 
3739 	if (msglen < sizeof (*desc_msg)) {
3740 		PR0("Expected at least %lu-byte descriptor message; "
3741 		    "received %lu bytes", sizeof (*desc_msg), msglen);
3742 		return (EBADMSG);
3743 	}
3744 
3745 	if (msglen != (expected = expected_inband_size(desc_msg))) {
3746 		PR0("Expected %lu-byte descriptor message; "
3747 		    "received %lu bytes", expected, msglen);
3748 		return (EBADMSG);
3749 	}
3750 
3751 	if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
3752 		return (EBADMSG);
3753 
3754 	/*
3755 	 * Valid message:  Set up the in-band descriptor task and process the
3756 	 * request.  Arrange to acknowledge the client's message, unless an
3757 	 * error processing the descriptor task results in setting
3758 	 * VIO_SUBTYPE_NACK
3759 	 */
3760 	PR1("Valid in-band-descriptor message");
3761 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
3762 
3763 	ASSERT(vd->inband_task.msg != NULL);
3764 
3765 	bcopy(msg, vd->inband_task.msg, msglen);
3766 	vd->inband_task.msglen	= msglen;
3767 
3768 	/*
3769 	 * The task request is now the payload of the message
3770 	 * that was just copied into the body of the task.
3771 	 */
3772 	desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
3773 	vd->inband_task.request	= &desc_msg->payload;
3774 
3775 	return (vd_process_task(&vd->inband_task));
3776 }
3777 
3778 static int
3779 vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
3780     vio_msg_t *msg, size_t msglen)
3781 {
3782 	int			status;
3783 	boolean_t		ready;
3784 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
3785 
3786 
3787 	/* Accept the updated dring element */
3788 	if ((status = ldc_mem_dring_acquire(vd->dring_handle, idx, idx)) != 0) {
3789 		PR0("ldc_mem_dring_acquire() returned errno %d", status);
3790 		return (status);
3791 	}
3792 	ready = (elem->hdr.dstate == VIO_DESC_READY);
3793 	if (ready) {
3794 		elem->hdr.dstate = VIO_DESC_ACCEPTED;
3795 	} else {
3796 		PR0("descriptor %u not ready", idx);
3797 		VD_DUMP_DRING_ELEM(elem);
3798 	}
3799 	if ((status = ldc_mem_dring_release(vd->dring_handle, idx, idx)) != 0) {
3800 		PR0("ldc_mem_dring_release() returned errno %d", status);
3801 		return (status);
3802 	}
3803 	if (!ready)
3804 		return (EBUSY);
3805 
3806 
3807 	/* Initialize a task and process the accepted element */
3808 	PR1("Processing dring element %u", idx);
3809 	vd->dring_task[idx].type	= type;
3810 
3811 	/* duplicate msg buf for cookies etc. */
3812 	bcopy(msg, vd->dring_task[idx].msg, msglen);
3813 
3814 	vd->dring_task[idx].msglen	= msglen;
3815 	return (vd_process_task(&vd->dring_task[idx]));
3816 }
3817 
3818 static int
3819 vd_process_element_range(vd_t *vd, int start, int end,
3820     vio_msg_t *msg, size_t msglen)
3821 {
3822 	int		i, n, nelem, status = 0;
3823 	boolean_t	inprogress = B_FALSE;
3824 	vd_task_type_t	type;
3825 
3826 
3827 	ASSERT(start >= 0);
3828 	ASSERT(end >= 0);
3829 
3830 	/*
3831 	 * Arrange to acknowledge the client's message, unless an error
3832 	 * processing one of the dring elements results in setting
3833 	 * VIO_SUBTYPE_NACK
3834 	 */
3835 	msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
3836 
3837 	/*
3838 	 * Process the dring elements in the range
3839 	 */
3840 	nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
3841 	for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
3842 		((vio_dring_msg_t *)msg)->end_idx = i;
3843 		type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
3844 		status = vd_process_element(vd, type, i, msg, msglen);
3845 		if (status == EINPROGRESS)
3846 			inprogress = B_TRUE;
3847 		else if (status != 0)
3848 			break;
3849 	}
3850 
3851 	/*
3852 	 * If some, but not all, operations of a multi-element range are in
3853 	 * progress, wait for other operations to complete before returning
3854 	 * (which will result in "ack" or "nack" of the message).  Note that
3855 	 * all outstanding operations will need to complete, not just the ones
3856 	 * corresponding to the current range of dring elements; howevever, as
3857 	 * this situation is an error case, performance is less critical.
3858 	 */
3859 	if ((nelem > 1) && (status != EINPROGRESS) && inprogress)
3860 		ddi_taskq_wait(vd->completionq);
3861 
3862 	return (status);
3863 }
3864 
3865 static int
3866 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
3867 {
3868 	vio_dring_msg_t	*dring_msg = (vio_dring_msg_t *)msg;
3869 
3870 
3871 	ASSERT(msglen >= sizeof (msg->tag));
3872 
3873 	if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
3874 	    VIO_DRING_DATA)) {
3875 		PR1("Message is not a dring-data message");
3876 		return (ENOMSG);
3877 	}
3878 
3879 	if (msglen != sizeof (*dring_msg)) {
3880 		PR0("Expected %lu-byte dring message; received %lu bytes",
3881 		    sizeof (*dring_msg), msglen);
3882 		return (EBADMSG);
3883 	}
3884 
3885 	if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
3886 		return (EBADMSG);
3887 
3888 	if (dring_msg->dring_ident != vd->dring_ident) {
3889 		PR0("Expected dring ident %lu; received ident %lu",
3890 		    vd->dring_ident, dring_msg->dring_ident);
3891 		return (EBADMSG);
3892 	}
3893 
3894 	if (dring_msg->start_idx >= vd->dring_len) {
3895 		PR0("\"start_idx\" = %u; must be less than %u",
3896 		    dring_msg->start_idx, vd->dring_len);
3897 		return (EBADMSG);
3898 	}
3899 
3900 	if ((dring_msg->end_idx < 0) ||
3901 	    (dring_msg->end_idx >= vd->dring_len)) {
3902 		PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
3903 		    dring_msg->end_idx, vd->dring_len);
3904 		return (EBADMSG);
3905 	}
3906 
3907 	/* Valid message; process range of updated dring elements */
3908 	PR1("Processing descriptor range, start = %u, end = %u",
3909 	    dring_msg->start_idx, dring_msg->end_idx);
3910 	return (vd_process_element_range(vd, dring_msg->start_idx,
3911 	    dring_msg->end_idx, msg, msglen));
3912 }
3913 
3914 static int
3915 recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
3916 {
3917 	int	retry, status;
3918 	size_t	size = *nbytes;
3919 
3920 
3921 	for (retry = 0, status = ETIMEDOUT;
3922 	    retry < vds_ldc_retries && status == ETIMEDOUT;
3923 	    retry++) {
3924 		PR1("ldc_read() attempt %d", (retry + 1));
3925 		*nbytes = size;
3926 		status = ldc_read(ldc_handle, msg, nbytes);
3927 	}
3928 
3929 	if (status) {
3930 		PR0("ldc_read() returned errno %d", status);
3931 		if (status != ECONNRESET)
3932 			return (ENOMSG);
3933 		return (status);
3934 	} else if (*nbytes == 0) {
3935 		PR1("ldc_read() returned 0 and no message read");
3936 		return (ENOMSG);
3937 	}
3938 
3939 	PR1("RCVD %lu-byte message", *nbytes);
3940 	return (0);
3941 }
3942 
3943 static int
3944 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
3945 {
3946 	int		status;
3947 
3948 
3949 	PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
3950 	    msg->tag.vio_subtype, msg->tag.vio_subtype_env);
3951 #ifdef	DEBUG
3952 	vd_decode_tag(msg);
3953 #endif
3954 
3955 	/*
3956 	 * Validate session ID up front, since it applies to all messages
3957 	 * once set
3958 	 */
3959 	if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
3960 		PR0("Expected SID %u, received %u", vd->sid,
3961 		    msg->tag.vio_sid);
3962 		return (EBADMSG);
3963 	}
3964 
3965 	PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
3966 
3967 	/*
3968 	 * Process the received message based on connection state
3969 	 */
3970 	switch (vd->state) {
3971 	case VD_STATE_INIT:	/* expect version message */
3972 		if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
3973 			return (status);
3974 
3975 		/* Version negotiated, move to that state */
3976 		vd->state = VD_STATE_VER;
3977 		return (0);
3978 
3979 	case VD_STATE_VER:	/* expect attribute message */
3980 		if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
3981 			return (status);
3982 
3983 		/* Attributes exchanged, move to that state */
3984 		vd->state = VD_STATE_ATTR;
3985 		return (0);
3986 
3987 	case VD_STATE_ATTR:
3988 		switch (vd->xfer_mode) {
3989 		case VIO_DESC_MODE:	/* expect RDX message */
3990 			if ((status = process_rdx_msg(msg, msglen)) != 0)
3991 				return (status);
3992 
3993 			/* Ready to receive in-band descriptors */
3994 			vd->state = VD_STATE_DATA;
3995 			return (0);
3996 
3997 		case VIO_DRING_MODE_V1_0:  /* expect register-dring message */
3998 			if ((status =
3999 			    vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
4000 				return (status);
4001 
4002 			/* One dring negotiated, move to that state */
4003 			vd->state = VD_STATE_DRING;
4004 			return (0);
4005 
4006 		default:
4007 			ASSERT("Unsupported transfer mode");
4008 			PR0("Unsupported transfer mode");
4009 			return (ENOTSUP);
4010 		}
4011 
4012 	case VD_STATE_DRING:	/* expect RDX, register-dring, or unreg-dring */
4013 		if ((status = process_rdx_msg(msg, msglen)) == 0) {
4014 			/* Ready to receive data */
4015 			vd->state = VD_STATE_DATA;
4016 			return (0);
4017 		} else if (status != ENOMSG) {
4018 			return (status);
4019 		}
4020 
4021 
4022 		/*
4023 		 * If another register-dring message is received, stay in
4024 		 * dring state in case the client sends RDX; although the
4025 		 * protocol allows multiple drings, this server does not
4026 		 * support using more than one
4027 		 */
4028 		if ((status =
4029 		    vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
4030 			return (status);
4031 
4032 		/*
4033 		 * Acknowledge an unregister-dring message, but reset the
4034 		 * connection anyway:  Although the protocol allows
4035 		 * unregistering drings, this server cannot serve a vdisk
4036 		 * without its only dring
4037 		 */
4038 		status = vd_process_dring_unreg_msg(vd, msg, msglen);
4039 		return ((status == 0) ? ENOTSUP : status);
4040 
4041 	case VD_STATE_DATA:
4042 		switch (vd->xfer_mode) {
4043 		case VIO_DESC_MODE:	/* expect in-band-descriptor message */
4044 			return (vd_process_desc_msg(vd, msg, msglen));
4045 
4046 		case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
4047 			/*
4048 			 * Typically expect dring-data messages, so handle
4049 			 * them first
4050 			 */
4051 			if ((status = vd_process_dring_msg(vd, msg,
4052 			    msglen)) != ENOMSG)
4053 				return (status);
4054 
4055 			/*
4056 			 * Acknowledge an unregister-dring message, but reset
4057 			 * the connection anyway:  Although the protocol
4058 			 * allows unregistering drings, this server cannot
4059 			 * serve a vdisk without its only dring
4060 			 */
4061 			status = vd_process_dring_unreg_msg(vd, msg, msglen);
4062 			return ((status == 0) ? ENOTSUP : status);
4063 
4064 		default:
4065 			ASSERT("Unsupported transfer mode");
4066 			PR0("Unsupported transfer mode");
4067 			return (ENOTSUP);
4068 		}
4069 
4070 	default:
4071 		ASSERT("Invalid client connection state");
4072 		PR0("Invalid client connection state");
4073 		return (ENOTSUP);
4074 	}
4075 }
4076 
4077 static int
4078 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
4079 {
4080 	int		status;
4081 	boolean_t	reset_ldc = B_FALSE;
4082 	vd_task_t	task;
4083 
4084 	/*
4085 	 * Check that the message is at least big enough for a "tag", so that
4086 	 * message processing can proceed based on tag-specified message type
4087 	 */
4088 	if (msglen < sizeof (vio_msg_tag_t)) {
4089 		PR0("Received short (%lu-byte) message", msglen);
4090 		/* Can't "nack" short message, so drop the big hammer */
4091 		PR0("initiating full reset");
4092 		vd_need_reset(vd, B_TRUE);
4093 		return (EBADMSG);
4094 	}
4095 
4096 	/*
4097 	 * Process the message
4098 	 */
4099 	switch (status = vd_do_process_msg(vd, msg, msglen)) {
4100 	case 0:
4101 		/* "ack" valid, successfully-processed messages */
4102 		msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
4103 		break;
4104 
4105 	case EINPROGRESS:
4106 		/* The completion handler will "ack" or "nack" the message */
4107 		return (EINPROGRESS);
4108 	case ENOMSG:
4109 		PR0("Received unexpected message");
4110 		_NOTE(FALLTHROUGH);
4111 	case EBADMSG:
4112 	case ENOTSUP:
4113 		/* "transport" error will cause NACK of invalid messages */
4114 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
4115 		break;
4116 
4117 	default:
4118 		/* "transport" error will cause NACK of invalid messages */
4119 		msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
4120 		/* An LDC error probably occurred, so try resetting it */
4121 		reset_ldc = B_TRUE;
4122 		break;
4123 	}
4124 
4125 	PR1("\tResulting in state %d (%s)", vd->state,
4126 	    vd_decode_state(vd->state));
4127 
4128 	/* populate the task so we can dispatch it on the taskq */
4129 	task.vd = vd;
4130 	task.msg = msg;
4131 	task.msglen = msglen;
4132 
4133 	/*
4134 	 * Queue a task to send the notification that the operation completed.
4135 	 * We need to ensure that requests are responded to in the correct
4136 	 * order and since the taskq is processed serially this ordering
4137 	 * is maintained.
4138 	 */
4139 	(void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
4140 	    &task, DDI_SLEEP);
4141 
4142 	/*
4143 	 * To ensure handshake negotiations do not happen out of order, such
4144 	 * requests that come through this path should not be done in parallel
4145 	 * so we need to wait here until the response is sent to the client.
4146 	 */
4147 	ddi_taskq_wait(vd->completionq);
4148 
4149 	/* Arrange to reset the connection for nack'ed or failed messages */
4150 	if ((status != 0) || reset_ldc) {
4151 		PR0("initiating %s reset",
4152 		    (reset_ldc) ? "full" : "soft");
4153 		vd_need_reset(vd, reset_ldc);
4154 	}
4155 
4156 	return (status);
4157 }
4158 
4159 static boolean_t
4160 vd_enabled(vd_t *vd)
4161 {
4162 	boolean_t	enabled;
4163 
4164 	mutex_enter(&vd->lock);
4165 	enabled = vd->enabled;
4166 	mutex_exit(&vd->lock);
4167 	return (enabled);
4168 }
4169 
4170 static void
4171 vd_recv_msg(void *arg)
4172 {
4173 	vd_t	*vd = (vd_t *)arg;
4174 	int	rv = 0, status = 0;
4175 
4176 	ASSERT(vd != NULL);
4177 
4178 	PR2("New task to receive incoming message(s)");
4179 
4180 
4181 	while (vd_enabled(vd) && status == 0) {
4182 		size_t		msglen, msgsize;
4183 		ldc_status_t	lstatus;
4184 
4185 		/*
4186 		 * Receive and process a message
4187 		 */
4188 		vd_reset_if_needed(vd);	/* can change vd->max_msglen */
4189 
4190 		/*
4191 		 * check if channel is UP - else break out of loop
4192 		 */
4193 		status = ldc_status(vd->ldc_handle, &lstatus);
4194 		if (lstatus != LDC_UP) {
4195 			PR0("channel not up (status=%d), exiting recv loop\n",
4196 			    lstatus);
4197 			break;
4198 		}
4199 
4200 		ASSERT(vd->max_msglen != 0);
4201 
4202 		msgsize = vd->max_msglen; /* stable copy for alloc/free */
4203 		msglen	= msgsize;	  /* actual len after recv_msg() */
4204 
4205 		status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
4206 		switch (status) {
4207 		case 0:
4208 			rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp,
4209 			    msglen);
4210 			/* check if max_msglen changed */
4211 			if (msgsize != vd->max_msglen) {
4212 				PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
4213 				    msgsize, vd->max_msglen);
4214 				kmem_free(vd->vio_msgp, msgsize);
4215 				vd->vio_msgp =
4216 				    kmem_alloc(vd->max_msglen, KM_SLEEP);
4217 			}
4218 			if (rv == EINPROGRESS)
4219 				continue;
4220 			break;
4221 
4222 		case ENOMSG:
4223 			break;
4224 
4225 		case ECONNRESET:
4226 			PR0("initiating soft reset (ECONNRESET)\n");
4227 			vd_need_reset(vd, B_FALSE);
4228 			status = 0;
4229 			break;
4230 
4231 		default:
4232 			/* Probably an LDC failure; arrange to reset it */
4233 			PR0("initiating full reset (status=0x%x)", status);
4234 			vd_need_reset(vd, B_TRUE);
4235 			break;
4236 		}
4237 	}
4238 
4239 	PR2("Task finished");
4240 }
4241 
4242 static uint_t
4243 vd_handle_ldc_events(uint64_t event, caddr_t arg)
4244 {
4245 	vd_t	*vd = (vd_t *)(void *)arg;
4246 	int	status;
4247 
4248 	ASSERT(vd != NULL);
4249 
4250 	if (!vd_enabled(vd))
4251 		return (LDC_SUCCESS);
4252 
4253 	if (event & LDC_EVT_DOWN) {
4254 		PR0("LDC_EVT_DOWN: LDC channel went down");
4255 
4256 		vd_need_reset(vd, B_TRUE);
4257 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
4258 		    DDI_SLEEP);
4259 		if (status == DDI_FAILURE) {
4260 			PR0("cannot schedule task to recv msg\n");
4261 			vd_need_reset(vd, B_TRUE);
4262 		}
4263 	}
4264 
4265 	if (event & LDC_EVT_RESET) {
4266 		PR0("LDC_EVT_RESET: LDC channel was reset");
4267 
4268 		if (vd->state != VD_STATE_INIT) {
4269 			PR0("scheduling full reset");
4270 			vd_need_reset(vd, B_FALSE);
4271 			status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
4272 			    vd, DDI_SLEEP);
4273 			if (status == DDI_FAILURE) {
4274 				PR0("cannot schedule task to recv msg\n");
4275 				vd_need_reset(vd, B_TRUE);
4276 			}
4277 
4278 		} else {
4279 			PR0("channel already reset, ignoring...\n");
4280 			PR0("doing ldc up...\n");
4281 			(void) ldc_up(vd->ldc_handle);
4282 		}
4283 
4284 		return (LDC_SUCCESS);
4285 	}
4286 
4287 	if (event & LDC_EVT_UP) {
4288 		PR0("EVT_UP: LDC is up\nResetting client connection state");
4289 		PR0("initiating soft reset");
4290 		vd_need_reset(vd, B_FALSE);
4291 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
4292 		    vd, DDI_SLEEP);
4293 		if (status == DDI_FAILURE) {
4294 			PR0("cannot schedule task to recv msg\n");
4295 			vd_need_reset(vd, B_TRUE);
4296 			return (LDC_SUCCESS);
4297 		}
4298 	}
4299 
4300 	if (event & LDC_EVT_READ) {
4301 		int	status;
4302 
4303 		PR1("New data available");
4304 		/* Queue a task to receive the new data */
4305 		status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
4306 		    DDI_SLEEP);
4307 
4308 		if (status == DDI_FAILURE) {
4309 			PR0("cannot schedule task to recv msg\n");
4310 			vd_need_reset(vd, B_TRUE);
4311 		}
4312 	}
4313 
4314 	return (LDC_SUCCESS);
4315 }
4316 
4317 static uint_t
4318 vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
4319 {
4320 	_NOTE(ARGUNUSED(key, val))
4321 	(*((uint_t *)arg))++;
4322 	return (MH_WALK_TERMINATE);
4323 }
4324 
4325 
4326 static int
4327 vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
4328 {
4329 	uint_t	vd_present = 0;
4330 	minor_t	instance;
4331 	vds_t	*vds;
4332 
4333 
4334 	switch (cmd) {
4335 	case DDI_DETACH:
4336 		/* the real work happens below */
4337 		break;
4338 	case DDI_SUSPEND:
4339 		PR0("No action required for DDI_SUSPEND");
4340 		return (DDI_SUCCESS);
4341 	default:
4342 		PR0("Unrecognized \"cmd\"");
4343 		return (DDI_FAILURE);
4344 	}
4345 
4346 	ASSERT(cmd == DDI_DETACH);
4347 	instance = ddi_get_instance(dip);
4348 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
4349 		PR0("Could not get state for instance %u", instance);
4350 		ddi_soft_state_free(vds_state, instance);
4351 		return (DDI_FAILURE);
4352 	}
4353 
4354 	/* Do no detach when serving any vdisks */
4355 	mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
4356 	if (vd_present) {
4357 		PR0("Not detaching because serving vdisks");
4358 		return (DDI_FAILURE);
4359 	}
4360 
4361 	PR0("Detaching");
4362 	if (vds->initialized & VDS_MDEG) {
4363 		(void) mdeg_unregister(vds->mdeg);
4364 		kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
4365 		kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
4366 		vds->ispecp = NULL;
4367 		vds->mdeg = NULL;
4368 	}
4369 
4370 	if (vds->initialized & VDS_LDI)
4371 		(void) ldi_ident_release(vds->ldi_ident);
4372 	mod_hash_destroy_hash(vds->vd_table);
4373 	ddi_soft_state_free(vds_state, instance);
4374 	return (DDI_SUCCESS);
4375 }
4376 
4377 static boolean_t
4378 is_pseudo_device(dev_info_t *dip)
4379 {
4380 	dev_info_t	*parent, *root = ddi_root_node();
4381 
4382 
4383 	for (parent = ddi_get_parent(dip); (parent != NULL) && (parent != root);
4384 	    parent = ddi_get_parent(parent)) {
4385 		if (strcmp(ddi_get_name(parent), DEVI_PSEUDO_NEXNAME) == 0)
4386 			return (B_TRUE);
4387 	}
4388 
4389 	return (B_FALSE);
4390 }
4391 
4392 /*
4393  * Description:
4394  *	This function checks to see if the file being used as a
4395  *	virtual disk is an ISO image. An ISO image is a special
4396  *	case which can be booted/installed from like a CD/DVD
4397  *
4398  * Parameters:
4399  *	vd		- disk on which the operation is performed.
4400  *
4401  * Return Code:
4402  *	B_TRUE		- The file is an ISO 9660 compliant image
4403  *	B_FALSE		- just a regular disk image file
4404  */
4405 static boolean_t
4406 vd_file_is_iso_image(vd_t *vd)
4407 {
4408 	char	iso_buf[ISO_SECTOR_SIZE];
4409 	int	i, rv;
4410 	uint_t	sec;
4411 
4412 	ASSERT(vd->file);
4413 
4414 	/*
4415 	 * If we have already discovered and saved this info we can
4416 	 * short-circuit the check and avoid reading the file.
4417 	 */
4418 	if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
4419 		return (B_TRUE);
4420 
4421 	/*
4422 	 * We wish to read the sector that should contain the 2nd ISO volume
4423 	 * descriptor. The second field in this descriptor is called the
4424 	 * Standard Identifier and is set to CD001 for a CD-ROM compliant
4425 	 * to the ISO 9660 standard.
4426 	 */
4427 	sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size;
4428 	rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
4429 	    sec, ISO_SECTOR_SIZE);
4430 
4431 	if (rv < 0)
4432 		return (B_FALSE);
4433 
4434 	for (i = 0; i < ISO_ID_STRLEN; i++) {
4435 		if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
4436 			return (B_FALSE);
4437 	}
4438 
4439 	return (B_TRUE);
4440 }
4441 
4442 /*
4443  * Description:
4444  *	This function checks to see if the virtual device is an ATAPI
4445  *	device. ATAPI devices use Group 1 Read/Write commands, so
4446  *	any USCSI calls vds makes need to take this into account.
4447  *
4448  * Parameters:
4449  *	vd		- disk on which the operation is performed.
4450  *
4451  * Return Code:
4452  *	B_TRUE		- The virtual disk is backed by an ATAPI device
4453  *	B_FALSE		- not an ATAPI device (presumably SCSI)
4454  */
4455 static boolean_t
4456 vd_is_atapi_device(vd_t *vd)
4457 {
4458 	boolean_t	is_atapi = B_FALSE;
4459 	char		*variantp;
4460 	int		rv;
4461 
4462 	ASSERT(vd->ldi_handle[0] != NULL);
4463 	ASSERT(!vd->file);
4464 
4465 	rv = ldi_prop_lookup_string(vd->ldi_handle[0],
4466 	    (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
4467 	if (rv == DDI_PROP_SUCCESS) {
4468 		PR0("'variant' property exists for %s", vd->device_path);
4469 		if (strcmp(variantp, "atapi") == 0)
4470 			is_atapi = B_TRUE;
4471 		ddi_prop_free(variantp);
4472 	}
4473 
4474 	rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
4475 	if (rv) {
4476 		PR0("'atapi' property exists for %s", vd->device_path);
4477 		is_atapi = B_TRUE;
4478 	}
4479 
4480 	return (is_atapi);
4481 }
4482 
4483 static int
4484 vd_setup_mediainfo(vd_t *vd)
4485 {
4486 	int status, rval;
4487 	struct dk_minfo	dk_minfo;
4488 
4489 	ASSERT(vd->ldi_handle[0] != NULL);
4490 	ASSERT(vd->vdisk_block_size != 0);
4491 
4492 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
4493 	    (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL),
4494 	    kcred, &rval)) != 0)
4495 		return (status);
4496 
4497 	ASSERT(dk_minfo.dki_lbsize % vd->vdisk_block_size == 0);
4498 
4499 	vd->block_size = dk_minfo.dki_lbsize;
4500 	vd->vdisk_size = (dk_minfo.dki_capacity * dk_minfo.dki_lbsize) /
4501 	    vd->vdisk_block_size;
4502 	vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type);
4503 	return (0);
4504 }
4505 
4506 static int
4507 vd_setup_full_disk(vd_t *vd)
4508 {
4509 	int		status;
4510 	major_t		major = getmajor(vd->dev[0]);
4511 	minor_t		minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
4512 
4513 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
4514 
4515 	vd->vdisk_block_size = DEV_BSIZE;
4516 
4517 	/*
4518 	 * At this point, vdisk_size is set to the size of partition 2 but
4519 	 * this does not represent the size of the disk because partition 2
4520 	 * may not cover the entire disk and its size does not include reserved
4521 	 * blocks. So we call vd_get_mediainfo to udpate this information and
4522 	 * set the block size and the media type of the disk.
4523 	 */
4524 	status = vd_setup_mediainfo(vd);
4525 
4526 	if (status != 0) {
4527 		if (!vd->scsi) {
4528 			/* unexpected failure */
4529 			PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
4530 			    status);
4531 			return (status);
4532 		}
4533 
4534 		/*
4535 		 * The function can fail for SCSI disks which are present but
4536 		 * reserved by another system. In that case, we don't know the
4537 		 * size of the disk and the block size.
4538 		 */
4539 		vd->vdisk_size = VD_SIZE_UNKNOWN;
4540 		vd->block_size = 0;
4541 		vd->vdisk_media = VD_MEDIA_FIXED;
4542 	}
4543 
4544 	/* Move dev number and LDI handle to entire-disk-slice array elements */
4545 	vd->dev[VD_ENTIRE_DISK_SLICE]		= vd->dev[0];
4546 	vd->dev[0]				= 0;
4547 	vd->ldi_handle[VD_ENTIRE_DISK_SLICE]	= vd->ldi_handle[0];
4548 	vd->ldi_handle[0]			= NULL;
4549 
4550 	/* Initialize device numbers for remaining slices and open them */
4551 	for (int slice = 0; slice < vd->nslices; slice++) {
4552 		/*
4553 		 * Skip the entire-disk slice, as it's already open and its
4554 		 * device known
4555 		 */
4556 		if (slice == VD_ENTIRE_DISK_SLICE)
4557 			continue;
4558 		ASSERT(vd->dev[slice] == 0);
4559 		ASSERT(vd->ldi_handle[slice] == NULL);
4560 
4561 		/*
4562 		 * Construct the device number for the current slice
4563 		 */
4564 		vd->dev[slice] = makedevice(major, (minor + slice));
4565 
4566 		/*
4567 		 * Open all slices of the disk to serve them to the client.
4568 		 * Slices are opened exclusively to prevent other threads or
4569 		 * processes in the service domain from performing I/O to
4570 		 * slices being accessed by a client.  Failure to open a slice
4571 		 * results in vds not serving this disk, as the client could
4572 		 * attempt (and should be able) to access any slice immediately.
4573 		 * Any slices successfully opened before a failure will get
4574 		 * closed by vds_destroy_vd() as a result of the error returned
4575 		 * by this function.
4576 		 *
4577 		 * We need to do the open with FNDELAY so that opening an empty
4578 		 * slice does not fail.
4579 		 */
4580 		PR0("Opening device major %u, minor %u = slice %u",
4581 		    major, minor, slice);
4582 
4583 		/*
4584 		 * Try to open the device. This can fail for example if we are
4585 		 * opening an empty slice. So in case of a failure, we try the
4586 		 * open again but this time with the FNDELAY flag.
4587 		 */
4588 		status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
4589 		    vd->open_flags, kcred, &vd->ldi_handle[slice],
4590 		    vd->vds->ldi_ident);
4591 
4592 		if (status != 0) {
4593 			status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
4594 			    vd->open_flags | FNDELAY, kcred,
4595 			    &vd->ldi_handle[slice], vd->vds->ldi_ident);
4596 		}
4597 
4598 		if (status != 0) {
4599 			PRN("ldi_open_by_dev() returned errno %d "
4600 			    "for slice %u", status, slice);
4601 			/* vds_destroy_vd() will close any open slices */
4602 			vd->ldi_handle[slice] = NULL;
4603 			return (status);
4604 		}
4605 	}
4606 
4607 	return (0);
4608 }
4609 
4610 /*
4611  * When a slice or a volume is exported as a single-slice disk, we want
4612  * the disk backend (i.e. the slice or volume) to be entirely mapped as
4613  * a slice without the addition of any metadata.
4614  *
4615  * So when exporting the disk as a VTOC disk, we fake a disk with the following
4616  * layout:
4617  *
4618  *                 0 1                         N+1
4619  *                 +-+--------------------------+
4620  *  virtual disk:  |L|           slice 0        |
4621  *                 +-+--------------------------+
4622  *                  ^:                          :
4623  *                  |:                          :
4624  *      VTOC LABEL--+:                          :
4625  *                   +--------------------------+
4626  *  disk backend:    |       slice/volume       |
4627  *                   +--------------------------+
4628  *                   0                          N
4629  *
4630  * N is the number of blocks in the slice/volume.
4631  *
4632  * We simulate a disk with N+1 blocks. The first block (block 0) is faked and
4633  * can not be changed. The remaining blocks (1 to N+1) defines slice 0 and are
4634  * mapped to the exported slice or volume:
4635  *
4636  * - block 0 (L) can return a fake VTOC label if raw read was implemented.
4637  * - block 1 to N+1 is mapped to the exported slice or volume.
4638  *
4639  */
4640 static int
4641 vd_setup_partition_vtoc(vd_t *vd)
4642 {
4643 	int rval, status;
4644 	char *device_path = vd->device_path;
4645 
4646 	status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
4647 	    (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), kcred, &rval);
4648 
4649 	if (status != 0) {
4650 		PRN("ldi_ioctl(DKIOCGEOM) returned errno %d for %s",
4651 		    status, device_path);
4652 		return (status);
4653 	}
4654 
4655 	/* Initialize dk_geom structure for single-slice device */
4656 	if (vd->dk_geom.dkg_nsect == 0) {
4657 		PRN("%s geometry claims 0 sectors per track", device_path);
4658 		return (EIO);
4659 	}
4660 	if (vd->dk_geom.dkg_nhead == 0) {
4661 		PRN("%s geometry claims 0 heads", device_path);
4662 		return (EIO);
4663 	}
4664 	vd->dk_geom.dkg_ncyl = (vd->vdisk_size + 1) / vd->dk_geom.dkg_nsect /
4665 	    vd->dk_geom.dkg_nhead;
4666 	vd->dk_geom.dkg_acyl = 0;
4667 	vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
4668 
4669 
4670 	/* Initialize vtoc structure for single-slice device */
4671 	bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
4672 	    MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
4673 	bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
4674 	vd->vtoc.v_nparts = 1;
4675 	vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
4676 	vd->vtoc.v_part[0].p_flag = 0;
4677 	vd->vtoc.v_part[0].p_start = 1;
4678 	vd->vtoc.v_part[0].p_size = vd->vdisk_size;
4679 	bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
4680 	    MIN(sizeof (VD_ASCIILABEL), sizeof (vd->vtoc.v_asciilabel)));
4681 
4682 	/* adjust the vdisk_size, we emulate the first block */
4683 	vd->vdisk_size += 1;
4684 
4685 	return (0);
4686 }
4687 
4688 /*
4689  * When a slice, volume or file is exported as a single-slice disk, we want
4690  * the disk backend (i.e. the slice, volume or file) to be entirely mapped
4691  * as a slice without the addition of any metadata.
4692  *
4693  * So when exporting the disk as an EFI disk, we fake a disk with the following
4694  * layout:
4695  *
4696  *                 0 1 2 3      34                        34+N
4697  *                 +-+-+-+-------+--------------------------+
4698  *  virtual disk:  |X|T|E|XXXXXXX|           slice 0        |
4699  *                 +-+-+-+-------+--------------------------+
4700  *                    ^ ^        :                          :
4701  *                    | |        :                          :
4702  *                GPT-+ +-GPE    :                          :
4703  *                               +--------------------------+
4704  *  disk backend:                |     slice/volume/file    |
4705  *                               +--------------------------+
4706  *                               0                          N
4707  *
4708  * N is the number of blocks in the slice/volume/file.
4709  *
4710  * We simulate a disk with 34+N blocks. The first 34 blocks (0 to 33) are
4711  * emulated and can not be changed. The remaining blocks (34 to 34+N) defines
4712  * slice 0 and are mapped to the exported slice, volume or file:
4713  *
4714  * - block 0 (X) is unused and can return 0 if raw read was implemented.
4715  * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
4716  * - block 2 (E) returns a fake EFI GPE (via DKIOCGETEFI)
4717  * - block 3 to 33 (X) are unused and return 0 if raw read is implemented.
4718  * - block 34 to 34+N is mapped to the exported slice, volume or file.
4719  *
4720  */
4721 static int
4722 vd_setup_partition_efi(vd_t *vd)
4723 {
4724 	efi_gpt_t *gpt;
4725 	efi_gpe_t *gpe;
4726 	struct uuid uuid = EFI_USR;
4727 	uint32_t crc;
4728 
4729 	gpt = &vd->efi_gpt;
4730 	gpe = &vd->efi_gpe;
4731 
4732 	bzero(gpt, sizeof (efi_gpt_t));
4733 	bzero(gpe, sizeof (efi_gpe_t));
4734 
4735 	/* adjust the vdisk_size, we emulate the first 34 blocks */
4736 	vd->vdisk_size += 34;
4737 
4738 	gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
4739 	gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
4740 	gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
4741 	gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL);
4742 	gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
4743 	gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
4744 	gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
4745 	gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
4746 
4747 	UUID_LE_CONVERT(gpe->efi_gpe_PartitionTypeGUID, uuid);
4748 	gpe->efi_gpe_StartingLBA = gpt->efi_gpt_FirstUsableLBA;
4749 	gpe->efi_gpe_EndingLBA = gpt->efi_gpt_LastUsableLBA;
4750 
4751 	CRC32(crc, gpe, sizeof (efi_gpe_t), -1U, crc32_table);
4752 	gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
4753 
4754 	CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
4755 	gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
4756 
4757 	return (0);
4758 }
4759 
4760 /*
4761  * Setup for a virtual disk whose backend is a file (exported as a single slice
4762  * or as a full disk) or a pseudo device (for example a ZFS, SVM or VxVM volume)
4763  * exported as a full disk. In these cases, the backend is accessed using the
4764  * vnode interface.
4765  */
4766 static int
4767 vd_setup_backend_vnode(vd_t *vd)
4768 {
4769 	int 		rval, status;
4770 	vattr_t		vattr;
4771 	dev_t		dev;
4772 	char		*file_path = vd->device_path;
4773 	char		dev_path[MAXPATHLEN + 1];
4774 	ldi_handle_t	lhandle;
4775 	struct dk_cinfo	dk_cinfo;
4776 
4777 	if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
4778 	    0, &vd->file_vnode, 0, 0)) != 0) {
4779 		PRN("vn_open(%s) = errno %d", file_path, status);
4780 		return (status);
4781 	}
4782 
4783 	/*
4784 	 * We set vd->file now so that vds_destroy_vd will take care of
4785 	 * closing the file and releasing the vnode in case of an error.
4786 	 */
4787 	vd->file = B_TRUE;
4788 
4789 	vattr.va_mask = AT_SIZE;
4790 	if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL))
4791 	    != 0) {
4792 		PRN("VOP_GETATTR(%s) = errno %d", file_path, status);
4793 		return (EIO);
4794 	}
4795 
4796 	vd->file_size = vattr.va_size;
4797 	/* size should be at least sizeof(dk_label) */
4798 	if (vd->file_size < sizeof (struct dk_label)) {
4799 		PRN("Size of file has to be at least %ld bytes",
4800 		    sizeof (struct dk_label));
4801 		return (EIO);
4802 	}
4803 
4804 	if (vd->file_vnode->v_flag & VNOMAP) {
4805 		PRN("File %s cannot be mapped", file_path);
4806 		return (EIO);
4807 	}
4808 
4809 	/* sector size = block size = DEV_BSIZE */
4810 	vd->block_size = DEV_BSIZE;
4811 	vd->vdisk_block_size = DEV_BSIZE;
4812 	vd->vdisk_size = vd->file_size / DEV_BSIZE;
4813 	vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
4814 
4815 	/*
4816 	 * Get max_xfer_sz from the device where the file is or from the device
4817 	 * itself if we have a pseudo device.
4818 	 */
4819 	dev_path[0] = '\0';
4820 
4821 	if (vd->pseudo) {
4822 		status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle,
4823 		    vd->vds->ldi_ident);
4824 	} else {
4825 		dev = vd->file_vnode->v_vfsp->vfs_dev;
4826 		if (ddi_dev_pathname(dev, S_IFBLK, dev_path) == DDI_SUCCESS) {
4827 			PR0("underlying device = %s\n", dev_path);
4828 		}
4829 
4830 		status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
4831 		    vd->vds->ldi_ident);
4832 	}
4833 
4834 	if (status != 0) {
4835 		PR0("ldi_open() returned errno %d for device %s",
4836 		    status, (dev_path[0] == '\0')? file_path : dev_path);
4837 	} else {
4838 		if ((status = ldi_ioctl(lhandle, DKIOCINFO,
4839 		    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
4840 		    &rval)) != 0) {
4841 			PR0("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
4842 			    status, dev_path);
4843 		} else {
4844 			/*
4845 			 * Store the device's max transfer size for
4846 			 * return to the client
4847 			 */
4848 			vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
4849 		}
4850 
4851 		PR0("close the device %s", dev_path);
4852 		(void) ldi_close(lhandle, FREAD, kcred);
4853 	}
4854 
4855 	PR0("using file %s, dev %s, max_xfer = %u blks",
4856 	    file_path, dev_path, vd->max_xfer_sz);
4857 
4858 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
4859 		ASSERT(!vd->pseudo);
4860 		vd->vdisk_label = VD_DISK_LABEL_EFI;
4861 		status = vd_setup_partition_efi(vd);
4862 		return (0);
4863 	}
4864 
4865 	/*
4866 	 * Find and validate the geometry of a disk image.
4867 	 */
4868 	status = vd_file_validate_geometry(vd);
4869 	if (status != 0 && status != EINVAL && status != ENOTSUP) {
4870 		PRN("Failed to read label from %s", file_path);
4871 		return (EIO);
4872 	}
4873 
4874 	if (vd_file_is_iso_image(vd)) {
4875 		/*
4876 		 * Indicate whether to call this a CD or DVD from the size
4877 		 * of the ISO image (images for both drive types are stored
4878 		 * in the ISO-9600 format). CDs can store up to just under 1Gb
4879 		 */
4880 		if ((vd->vdisk_size * vd->vdisk_block_size) >
4881 		    (1024 * 1024 * 1024))
4882 			vd->vdisk_media = VD_MEDIA_DVD;
4883 		else
4884 			vd->vdisk_media = VD_MEDIA_CD;
4885 	} else {
4886 		vd->vdisk_media = VD_MEDIA_FIXED;
4887 	}
4888 
4889 	/* Setup devid for the disk image */
4890 
4891 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
4892 
4893 		status = vd_file_read_devid(vd, &vd->file_devid);
4894 
4895 		if (status == 0) {
4896 			/* a valid devid was found */
4897 			return (0);
4898 		}
4899 
4900 		if (status != EINVAL) {
4901 			/*
4902 			 * There was an error while trying to read the devid.
4903 			 * So this disk image may have a devid but we are
4904 			 * unable to read it.
4905 			 */
4906 			PR0("can not read devid for %s", file_path);
4907 			vd->file_devid = NULL;
4908 			return (0);
4909 		}
4910 	}
4911 
4912 	/*
4913 	 * No valid device id was found so we create one. Note that a failure
4914 	 * to create a device id is not fatal and does not prevent the disk
4915 	 * image from being attached.
4916 	 */
4917 	PR1("creating devid for %s", file_path);
4918 
4919 	if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
4920 	    &vd->file_devid) != DDI_SUCCESS) {
4921 		PR0("fail to create devid for %s", file_path);
4922 		vd->file_devid = NULL;
4923 		return (0);
4924 	}
4925 
4926 	/*
4927 	 * Write devid to the disk image. The devid is stored into the disk
4928 	 * image if we have a valid label; otherwise the devid will be stored
4929 	 * when the user writes a valid label.
4930 	 */
4931 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
4932 		if (vd_file_write_devid(vd, vd->file_devid) != 0) {
4933 			PR0("fail to write devid for %s", file_path);
4934 			ddi_devid_free(vd->file_devid);
4935 			vd->file_devid = NULL;
4936 		}
4937 	}
4938 
4939 	return (0);
4940 }
4941 
4942 
4943 /*
4944  * Description:
4945  *	Open a device using its device path (supplied by ldm(1m))
4946  *
4947  * Parameters:
4948  *	vd 	- pointer to structure containing the vDisk info
4949  *
4950  * Return Value
4951  *	0	- success
4952  *	EIO	- Invalid number of partitions
4953  *	!= 0	- some other non-zero return value from ldi(9F) functions
4954  */
4955 static int
4956 vd_open_using_ldi_by_name(vd_t *vd)
4957 {
4958 	int		rval, status, open_flags;
4959 	struct dk_cinfo	dk_cinfo;
4960 	char		*device_path = vd->device_path;
4961 
4962 	/*
4963 	 * Try to open the device. If the flags indicate that the device should
4964 	 * be opened write-enabled, we first we try to open it "read-only"
4965 	 * to see if we have an optical device such as a CD-ROM which, for
4966 	 * now, we do not permit writes to and thus should not export write
4967 	 * operations to the client.
4968 	 *
4969 	 * Future: if/when we implement support for guest domains writing to
4970 	 * optical devices we will need to do further checking of the media type
4971 	 * to distinguish between read-only and writable discs.
4972 	 */
4973 	if (vd->open_flags & FWRITE) {
4974 		open_flags = vd->open_flags & ~FWRITE;
4975 		status = ldi_open_by_name(device_path, open_flags, kcred,
4976 		    &vd->ldi_handle[0], vd->vds->ldi_ident);
4977 
4978 		if (status == 0) {
4979 			/* Verify backing device supports dk_cinfo */
4980 			status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
4981 			    (intptr_t)&dk_cinfo, (open_flags | FKIOCTL),
4982 			    kcred, &rval);
4983 			if (status != 0) {
4984 				PRN("ldi_ioctl(DKIOCINFO) returned errno %d for"
4985 				    " %s opened as RO", status, device_path);
4986 				return (status);
4987 			}
4988 
4989 			if (dk_cinfo.dki_partition >= V_NUMPAR) {
4990 				PRN("slice %u >= maximum slice %u for %s",
4991 				    dk_cinfo.dki_partition, V_NUMPAR,
4992 				    device_path);
4993 				return (EIO);
4994 			}
4995 
4996 			/*
4997 			 * If this is an optical device then we disable
4998 			 * write access and return, otherwise we close
4999 			 * the device and try again with writes enabled.
5000 			 */
5001 			if (dk_cinfo.dki_ctype == DKC_CDROM) {
5002 				vd->open_flags = open_flags;
5003 				return (0);
5004 			} else {
5005 				(void) ldi_close(vd->ldi_handle[0],
5006 				    open_flags, kcred);
5007 			}
5008 		}
5009 	}
5010 
5011 	/* Attempt to (re)open device */
5012 	status = ldi_open_by_name(device_path, open_flags, kcred,
5013 	    &vd->ldi_handle[0], vd->vds->ldi_ident);
5014 
5015 	/*
5016 	 * The open can fail for example if we are opening an empty slice.
5017 	 * In case of a failure, we try the open again but this time with
5018 	 * the FNDELAY flag.
5019 	 */
5020 	if (status != 0)
5021 		status = ldi_open_by_name(device_path, vd->open_flags | FNDELAY,
5022 		    kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
5023 
5024 	if (status != 0) {
5025 		PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
5026 		vd->ldi_handle[0] = NULL;
5027 		return (status);
5028 	}
5029 
5030 	/* Verify backing device supports dk_cinfo */
5031 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
5032 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
5033 	    &rval)) != 0) {
5034 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
5035 		    status, device_path);
5036 		return (status);
5037 	}
5038 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
5039 		PRN("slice %u >= maximum slice %u for %s",
5040 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
5041 		return (EIO);
5042 	}
5043 
5044 	return (0);
5045 }
5046 
5047 
5048 /*
5049  * Setup for a virtual disk which backend is a device (a physical disk,
5050  * slice or pseudo device) that is directly exported either as a full disk
5051  * for a physical disk or as a slice for a pseudo device or a disk slice.
5052  * In these cases, the backend is accessed using the LDI interface.
5053  */
5054 static int
5055 vd_setup_backend_ldi(vd_t *vd)
5056 {
5057 	int		rval, status;
5058 	struct dk_cinfo	dk_cinfo;
5059 	char		*device_path = vd->device_path;
5060 
5061 	status = vd_open_using_ldi_by_name(vd);
5062 	if (status != 0) {
5063 		PR0("Failed to open (%s) = errno %d", device_path, status);
5064 		return (status);
5065 	}
5066 
5067 	vd->file = B_FALSE;
5068 
5069 	/* Get device number of backing device */
5070 	if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
5071 		PRN("ldi_get_dev() returned errno %d for %s",
5072 		    status, device_path);
5073 		return (status);
5074 	}
5075 
5076 	/* Verify backing device supports dk_cinfo */
5077 	if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
5078 	    (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
5079 	    &rval)) != 0) {
5080 		PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
5081 		    status, device_path);
5082 		return (status);
5083 	}
5084 	if (dk_cinfo.dki_partition >= V_NUMPAR) {
5085 		PRN("slice %u >= maximum slice %u for %s",
5086 		    dk_cinfo.dki_partition, V_NUMPAR, device_path);
5087 		return (EIO);
5088 	}
5089 
5090 	/* Store the device's max transfer size for return to the client */
5091 	vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
5092 
5093 	/*
5094 	 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
5095 	 * that we can use the correct CDB group when sending USCSI commands.
5096 	 */
5097 	vd->is_atapi_dev = vd_is_atapi_device(vd);
5098 
5099 	/*
5100 	 * Export a full disk.
5101 	 *
5102 	 * When we use the LDI interface, we export a device as a full disk
5103 	 * if we have an entire disk slice (slice 2) and if this slice is
5104 	 * exported as a full disk and not as a single slice disk.
5105 	 * Similarly, we want to use LDI if we are accessing a CD or DVD
5106 	 * device (even if it isn't s2)
5107 	 *
5108 	 * Note that pseudo devices are exported as full disks using the vnode
5109 	 * interface, not the LDI interface.
5110 	 */
5111 	if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE &&
5112 	    vd->vdisk_type == VD_DISK_TYPE_DISK) ||
5113 	    dk_cinfo.dki_ctype == DKC_CDROM) {
5114 		ASSERT(!vd->pseudo);
5115 		if (dk_cinfo.dki_ctype == DKC_SCSI_CCS)
5116 			vd->scsi = B_TRUE;
5117 		return (vd_setup_full_disk(vd));
5118 	}
5119 
5120 	/*
5121 	 * Export a single slice disk.
5122 	 *
5123 	 * The exported device can be either a pseudo device or a disk slice. If
5124 	 * it is a disk slice different from slice 2 then it is always exported
5125 	 * as a single slice disk even if the "slice" option is not specified.
5126 	 * If it is disk slice 2 or a pseudo device then it is exported as a
5127 	 * single slice disk only if the "slice" option is specified.
5128 	 */
5129 	return (vd_setup_single_slice_disk(vd));
5130 }
5131 
5132 static int
5133 vd_setup_single_slice_disk(vd_t *vd)
5134 {
5135 	int status, rval;
5136 	char *device_path = vd->device_path;
5137 
5138 	/* Get size of backing device */
5139 	if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) {
5140 		PRN("ldi_get_size() failed for %s", device_path);
5141 		return (EIO);
5142 	}
5143 	vd->vdisk_size = lbtodb(vd->vdisk_size);	/* convert to blocks */
5144 	vd->block_size = DEV_BSIZE;
5145 	vd->vdisk_block_size = DEV_BSIZE;
5146 	vd->vdisk_media = VD_MEDIA_FIXED;
5147 
5148 	if (vd->pseudo) {
5149 		ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
5150 	}
5151 
5152 	/*
5153 	 * We export the slice as a single slice disk even if the "slice"
5154 	 * option was not specified.
5155 	 */
5156 	vd->vdisk_type  = VD_DISK_TYPE_SLICE;
5157 	vd->nslices	= 1;
5158 
5159 	/*
5160 	 * When exporting a slice or a device as a single slice disk, we don't
5161 	 * care about any partitioning exposed by the backend. The goal is just
5162 	 * to export the backend as a flat storage. We provide a fake partition
5163 	 * table (either a VTOC or EFI), which presents only one slice, to
5164 	 * accommodate tools expecting a disk label.
5165 	 *
5166 	 * We check the label of the backend to export the device as a slice
5167 	 * using the same type of label (VTOC or EFI). If there is no label
5168 	 * then we create a fake EFI label.
5169 	 *
5170 	 * Note that the partition table we are creating could also be faked
5171 	 * by the client based on the size of the backend device.
5172 	 */
5173 	status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, (intptr_t)&vd->vtoc,
5174 	    (vd->open_flags | FKIOCTL), kcred, &rval);
5175 
5176 	if (status == 0) {
5177 		/* export with a fake VTOC label */
5178 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
5179 		status = vd_setup_partition_vtoc(vd);
5180 	} else {
5181 		/* export with a fake EFI label */
5182 		vd->vdisk_label = VD_DISK_LABEL_EFI;
5183 		status = vd_setup_partition_efi(vd);
5184 	}
5185 
5186 	return (status);
5187 }
5188 
5189 static int
5190 vd_setup_vd(vd_t *vd)
5191 {
5192 	int		status;
5193 	dev_info_t	*dip;
5194 	vnode_t 	*vnp;
5195 	char		*path = vd->device_path;
5196 
5197 	/* make sure the vdisk backend is valid */
5198 	if ((status = lookupname(path, UIO_SYSSPACE,
5199 	    FOLLOW, NULLVPP, &vnp)) != 0) {
5200 		PR0("Cannot lookup %s errno %d", path, status);
5201 		goto done;
5202 	}
5203 
5204 	switch (vnp->v_type) {
5205 	case VREG:
5206 		/*
5207 		 * Backend is a file so it is exported as a full disk or as a
5208 		 * single slice disk using the vnode interface.
5209 		 */
5210 		VN_RELE(vnp);
5211 		vd->pseudo = B_FALSE;
5212 		status = vd_setup_backend_vnode(vd);
5213 		break;
5214 
5215 	case VBLK:
5216 	case VCHR:
5217 		/*
5218 		 * Backend is a device. The way it is exported depends on the
5219 		 * type of the device.
5220 		 *
5221 		 * - A pseudo device is exported as a full disk using the vnode
5222 		 *   interface or as a single slice disk using the LDI
5223 		 *   interface.
5224 		 *
5225 		 * - A disk (represented by the slice 2 of that disk) is
5226 		 *   exported as a full disk using the LDI interface.
5227 		 *
5228 		 * - A disk slice (different from slice 2) is always exported
5229 		 *   as a single slice disk using the LDI interface.
5230 		 *
5231 		 * - The slice 2 of a disk is exported as a single slice disk
5232 		 *   if the "slice" option is specified, otherwise the entire
5233 		 *   disk will be exported. In any case, the LDI interface is
5234 		 *   used.
5235 		 */
5236 
5237 		/* check if this is a pseudo device */
5238 		if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
5239 		    dev_to_instance(vnp->v_rdev), 0))  == NULL) {
5240 			PRN("%s is no longer accessible", path);
5241 			VN_RELE(vnp);
5242 			status = EIO;
5243 			break;
5244 		}
5245 		vd->pseudo = is_pseudo_device(dip);
5246 		ddi_release_devi(dip);
5247 		VN_RELE(vnp);
5248 
5249 		if (!vd->pseudo) {
5250 			status = vd_setup_backend_ldi(vd);
5251 			break;
5252 		}
5253 
5254 		/*
5255 		 * If this is a pseudo device then its usage depends if the
5256 		 * "slice" option is set or not. If the "slice" option is set
5257 		 * then the pseudo device will be exported as a single slice,
5258 		 * otherwise it will be exported as a full disk.
5259 		 *
5260 		 * For backward compatibility, if vd_volume_force_slice is set
5261 		 * then we always export pseudo devices as slices.
5262 		 */
5263 		if (vd_volume_force_slice) {
5264 			vd->vdisk_type = VD_DISK_TYPE_SLICE;
5265 			vd->nslices = 1;
5266 		}
5267 
5268 		if (vd->vdisk_type == VD_DISK_TYPE_DISK)
5269 			status = vd_setup_backend_vnode(vd);
5270 		else
5271 			status = vd_setup_backend_ldi(vd);
5272 		break;
5273 
5274 	default:
5275 		PRN("Unsupported vdisk backend %s", path);
5276 		VN_RELE(vnp);
5277 		status = EBADF;
5278 	}
5279 
5280 done:
5281 	if (status != 0) {
5282 		/*
5283 		 * If the error is retryable print an error message only
5284 		 * during the first try.
5285 		 */
5286 		if (status == ENXIO || status == ENODEV ||
5287 		    status == ENOENT || status == EROFS) {
5288 			if (!(vd->initialized & VD_SETUP_ERROR)) {
5289 				PRN("%s is currently inaccessible (error %d)",
5290 				    path, status);
5291 			}
5292 			status = EAGAIN;
5293 		} else {
5294 			PRN("%s can not be exported as a virtual disk "
5295 			    "(error %d)", path, status);
5296 		}
5297 		vd->initialized |= VD_SETUP_ERROR;
5298 
5299 	} else if (vd->initialized & VD_SETUP_ERROR) {
5300 		/* print a message only if we previously had an error */
5301 		PRN("%s is now online", path);
5302 		vd->initialized &= ~VD_SETUP_ERROR;
5303 	}
5304 
5305 	return (status);
5306 }
5307 
5308 static int
5309 vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
5310     uint64_t ldc_id, vd_t **vdp)
5311 {
5312 	char			tq_name[TASKQ_NAMELEN];
5313 	int			status;
5314 	ddi_iblock_cookie_t	iblock = NULL;
5315 	ldc_attr_t		ldc_attr;
5316 	vd_t			*vd;
5317 
5318 
5319 	ASSERT(vds != NULL);
5320 	ASSERT(device_path != NULL);
5321 	ASSERT(vdp != NULL);
5322 	PR0("Adding vdisk for %s", device_path);
5323 
5324 	if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
5325 		PRN("No memory for virtual disk");
5326 		return (EAGAIN);
5327 	}
5328 	*vdp = vd;	/* assign here so vds_destroy_vd() can cleanup later */
5329 	vd->vds = vds;
5330 	(void) strncpy(vd->device_path, device_path, MAXPATHLEN);
5331 
5332 	/* Setup open flags */
5333 	vd->open_flags = FREAD;
5334 
5335 	if (!(options & VD_OPT_RDONLY))
5336 		vd->open_flags |= FWRITE;
5337 
5338 	if (options & VD_OPT_EXCLUSIVE)
5339 		vd->open_flags |= FEXCL;
5340 
5341 	/* Setup disk type */
5342 	if (options & VD_OPT_SLICE) {
5343 		vd->vdisk_type = VD_DISK_TYPE_SLICE;
5344 		vd->nslices = 1;
5345 	} else {
5346 		vd->vdisk_type = VD_DISK_TYPE_DISK;
5347 		vd->nslices = V_NUMPAR;
5348 	}
5349 
5350 	/* default disk label */
5351 	vd->vdisk_label = VD_DISK_LABEL_UNK;
5352 
5353 	/* Open vdisk and initialize parameters */
5354 	if ((status = vd_setup_vd(vd)) == 0) {
5355 		vd->initialized |= VD_DISK_READY;
5356 
5357 		ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
5358 		PR0("vdisk_type = %s, pseudo = %s, file = %s, nslices = %u",
5359 		    ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
5360 		    (vd->pseudo ? "yes" : "no"), (vd->file ? "yes" : "no"),
5361 		    vd->nslices);
5362 	} else {
5363 		if (status != EAGAIN)
5364 			return (status);
5365 	}
5366 
5367 	/* Initialize locking */
5368 	if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
5369 	    &iblock) != DDI_SUCCESS) {
5370 		PRN("Could not get iblock cookie.");
5371 		return (EIO);
5372 	}
5373 
5374 	mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
5375 	vd->initialized |= VD_LOCKING;
5376 
5377 
5378 	/* Create start and completion task queues for the vdisk */
5379 	(void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
5380 	PR1("tq_name = %s", tq_name);
5381 	if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
5382 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
5383 		PRN("Could not create task queue");
5384 		return (EIO);
5385 	}
5386 	(void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
5387 	PR1("tq_name = %s", tq_name);
5388 	if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
5389 	    TASKQ_DEFAULTPRI, 0)) == NULL) {
5390 		PRN("Could not create task queue");
5391 		return (EIO);
5392 	}
5393 	vd->enabled = 1;	/* before callback can dispatch to startq */
5394 
5395 
5396 	/* Bring up LDC */
5397 	ldc_attr.devclass	= LDC_DEV_BLK_SVC;
5398 	ldc_attr.instance	= ddi_get_instance(vds->dip);
5399 	ldc_attr.mode		= LDC_MODE_UNRELIABLE;
5400 	ldc_attr.mtu		= VD_LDC_MTU;
5401 	if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
5402 		PRN("Could not initialize LDC channel %lx, "
5403 		    "init failed with error %d", ldc_id, status);
5404 		return (status);
5405 	}
5406 	vd->initialized |= VD_LDC;
5407 
5408 	if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
5409 	    (caddr_t)vd)) != 0) {
5410 		PRN("Could not initialize LDC channel %lu,"
5411 		    "reg_callback failed with error %d", ldc_id, status);
5412 		return (status);
5413 	}
5414 
5415 	if ((status = ldc_open(vd->ldc_handle)) != 0) {
5416 		PRN("Could not initialize LDC channel %lu,"
5417 		    "open failed with error %d", ldc_id, status);
5418 		return (status);
5419 	}
5420 
5421 	if ((status = ldc_up(vd->ldc_handle)) != 0) {
5422 		PR0("ldc_up() returned errno %d", status);
5423 	}
5424 
5425 	/* Allocate the inband task memory handle */
5426 	status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
5427 	if (status) {
5428 		PRN("Could not initialize LDC channel %lu,"
5429 		    "alloc_handle failed with error %d", ldc_id, status);
5430 		return (ENXIO);
5431 	}
5432 
5433 	/* Add the successfully-initialized vdisk to the server's table */
5434 	if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
5435 		PRN("Error adding vdisk ID %lu to table", id);
5436 		return (EIO);
5437 	}
5438 
5439 	/* Allocate the staging buffer */
5440 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
5441 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
5442 
5443 	/* store initial state */
5444 	vd->state = VD_STATE_INIT;
5445 
5446 	return (0);
5447 }
5448 
5449 static void
5450 vd_free_dring_task(vd_t *vdp)
5451 {
5452 	if (vdp->dring_task != NULL) {
5453 		ASSERT(vdp->dring_len != 0);
5454 		/* Free all dring_task memory handles */
5455 		for (int i = 0; i < vdp->dring_len; i++) {
5456 			(void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
5457 			kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
5458 			vdp->dring_task[i].msg = NULL;
5459 		}
5460 		kmem_free(vdp->dring_task,
5461 		    (sizeof (*vdp->dring_task)) * vdp->dring_len);
5462 		vdp->dring_task = NULL;
5463 	}
5464 }
5465 
5466 /*
5467  * Destroy the state associated with a virtual disk
5468  */
5469 static void
5470 vds_destroy_vd(void *arg)
5471 {
5472 	vd_t	*vd = (vd_t *)arg;
5473 	int	retry = 0, rv;
5474 
5475 	if (vd == NULL)
5476 		return;
5477 
5478 	PR0("Destroying vdisk state");
5479 
5480 	/* Disable queuing requests for the vdisk */
5481 	if (vd->initialized & VD_LOCKING) {
5482 		mutex_enter(&vd->lock);
5483 		vd->enabled = 0;
5484 		mutex_exit(&vd->lock);
5485 	}
5486 
5487 	/* Drain and destroy start queue (*before* destroying completionq) */
5488 	if (vd->startq != NULL)
5489 		ddi_taskq_destroy(vd->startq);	/* waits for queued tasks */
5490 
5491 	/* Drain and destroy completion queue (*before* shutting down LDC) */
5492 	if (vd->completionq != NULL)
5493 		ddi_taskq_destroy(vd->completionq);	/* waits for tasks */
5494 
5495 	vd_free_dring_task(vd);
5496 
5497 	/* Free the inband task memory handle */
5498 	(void) ldc_mem_free_handle(vd->inband_task.mhdl);
5499 
5500 	/* Shut down LDC */
5501 	if (vd->initialized & VD_LDC) {
5502 		/* unmap the dring */
5503 		if (vd->initialized & VD_DRING)
5504 			(void) ldc_mem_dring_unmap(vd->dring_handle);
5505 
5506 		/* close LDC channel - retry on EAGAIN */
5507 		while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
5508 			if (++retry > vds_ldc_retries) {
5509 				PR0("Timed out closing channel");
5510 				break;
5511 			}
5512 			drv_usecwait(vds_ldc_delay);
5513 		}
5514 		if (rv == 0) {
5515 			(void) ldc_unreg_callback(vd->ldc_handle);
5516 			(void) ldc_fini(vd->ldc_handle);
5517 		} else {
5518 			/*
5519 			 * Closing the LDC channel has failed. Ideally we should
5520 			 * fail here but there is no Zeus level infrastructure
5521 			 * to handle this. The MD has already been changed and
5522 			 * we have to do the close. So we try to do as much
5523 			 * clean up as we can.
5524 			 */
5525 			(void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
5526 			while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
5527 				drv_usecwait(vds_ldc_delay);
5528 		}
5529 	}
5530 
5531 	/* Free the staging buffer for msgs */
5532 	if (vd->vio_msgp != NULL) {
5533 		kmem_free(vd->vio_msgp, vd->max_msglen);
5534 		vd->vio_msgp = NULL;
5535 	}
5536 
5537 	/* Free the inband message buffer */
5538 	if (vd->inband_task.msg != NULL) {
5539 		kmem_free(vd->inband_task.msg, vd->max_msglen);
5540 		vd->inband_task.msg = NULL;
5541 	}
5542 
5543 	if (vd->file) {
5544 		/* Close file */
5545 		(void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
5546 		    0, kcred, NULL);
5547 		VN_RELE(vd->file_vnode);
5548 		if (vd->file_devid != NULL)
5549 			ddi_devid_free(vd->file_devid);
5550 	} else {
5551 		/* Close any open backing-device slices */
5552 		for (uint_t slice = 0; slice < vd->nslices; slice++) {
5553 			if (vd->ldi_handle[slice] != NULL) {
5554 				PR0("Closing slice %u", slice);
5555 				(void) ldi_close(vd->ldi_handle[slice],
5556 				    vd->open_flags, kcred);
5557 			}
5558 		}
5559 	}
5560 
5561 	/* Free lock */
5562 	if (vd->initialized & VD_LOCKING)
5563 		mutex_destroy(&vd->lock);
5564 
5565 	/* Finally, free the vdisk structure itself */
5566 	kmem_free(vd, sizeof (*vd));
5567 }
5568 
5569 static int
5570 vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
5571     uint64_t ldc_id)
5572 {
5573 	int	status;
5574 	vd_t	*vd = NULL;
5575 
5576 
5577 	if ((status = vds_do_init_vd(vds, id, device_path, options,
5578 	    ldc_id, &vd)) != 0)
5579 		vds_destroy_vd(vd);
5580 
5581 	return (status);
5582 }
5583 
5584 static int
5585 vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
5586     uint64_t *ldc_id)
5587 {
5588 	int	num_channels;
5589 
5590 
5591 	/* Look for channel endpoint child(ren) of the vdisk MD node */
5592 	if ((num_channels = md_scan_dag(md, vd_node,
5593 	    md_find_name(md, VD_CHANNEL_ENDPOINT),
5594 	    md_find_name(md, "fwd"), channel)) <= 0) {
5595 		PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
5596 		return (-1);
5597 	}
5598 
5599 	/* Get the "id" value for the first channel endpoint node */
5600 	if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
5601 		PRN("No \"%s\" property found for \"%s\" of vdisk",
5602 		    VD_ID_PROP, VD_CHANNEL_ENDPOINT);
5603 		return (-1);
5604 	}
5605 
5606 	if (num_channels > 1) {
5607 		PRN("Using ID of first of multiple channels for this vdisk");
5608 	}
5609 
5610 	return (0);
5611 }
5612 
5613 static int
5614 vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
5615 {
5616 	int		num_nodes, status;
5617 	size_t		size;
5618 	mde_cookie_t	*channel;
5619 
5620 
5621 	if ((num_nodes = md_node_count(md)) <= 0) {
5622 		PRN("Invalid node count in Machine Description subtree");
5623 		return (-1);
5624 	}
5625 	size = num_nodes*(sizeof (*channel));
5626 	channel = kmem_zalloc(size, KM_SLEEP);
5627 	status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
5628 	kmem_free(channel, size);
5629 
5630 	return (status);
5631 }
5632 
5633 /*
5634  * Function:
5635  *	vds_get_options
5636  *
5637  * Description:
5638  * 	Parse the options of a vds node. Options are defined as an array
5639  *	of strings in the vds-block-device-opts property of the vds node
5640  *	in the machine description. Options are returned as a bitmask. The
5641  *	mapping between the bitmask options and the options strings from the
5642  *	machine description is defined in the vd_bdev_options[] array.
5643  *
5644  *	The vds-block-device-opts property is optional. If a vds has no such
5645  *	property then no option is defined.
5646  *
5647  * Parameters:
5648  *	md		- machine description.
5649  *	vd_node		- vds node in the machine description for which
5650  *			  options have to be parsed.
5651  *	options		- the returned options.
5652  *
5653  * Return Code:
5654  *	none.
5655  */
5656 static void
5657 vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
5658 {
5659 	char	*optstr, *opt;
5660 	int	len, n, i;
5661 
5662 	*options = 0;
5663 
5664 	if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
5665 	    (uint8_t **)&optstr, &len) != 0) {
5666 		PR0("No options found");
5667 		return;
5668 	}
5669 
5670 	/* parse options */
5671 	opt = optstr;
5672 	n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
5673 
5674 	while (opt < optstr + len) {
5675 		for (i = 0; i < n; i++) {
5676 			if (strncmp(vd_bdev_options[i].vdo_name,
5677 			    opt, VD_OPTION_NLEN) == 0) {
5678 				*options |= vd_bdev_options[i].vdo_value;
5679 				break;
5680 			}
5681 		}
5682 
5683 		if (i < n) {
5684 			PR0("option: %s", opt);
5685 		} else {
5686 			PRN("option %s is unknown or unsupported", opt);
5687 		}
5688 
5689 		opt += strlen(opt) + 1;
5690 	}
5691 }
5692 
5693 static void
5694 vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
5695 {
5696 	char		*device_path = NULL;
5697 	uint64_t	id = 0, ldc_id = 0, options = 0;
5698 
5699 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
5700 		PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
5701 		return;
5702 	}
5703 	PR0("Adding vdisk ID %lu", id);
5704 	if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
5705 	    &device_path) != 0) {
5706 		PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
5707 		return;
5708 	}
5709 
5710 	vds_get_options(md, vd_node, &options);
5711 
5712 	if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
5713 		PRN("Error getting LDC ID for vdisk %lu", id);
5714 		return;
5715 	}
5716 
5717 	if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
5718 		PRN("Failed to add vdisk ID %lu", id);
5719 		if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
5720 			PRN("No vDisk entry found for vdisk ID %lu", id);
5721 		return;
5722 	}
5723 }
5724 
5725 static void
5726 vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
5727 {
5728 	uint64_t	id = 0;
5729 
5730 
5731 	if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
5732 		PRN("Unable to get \"%s\" property from vdisk's MD node",
5733 		    VD_ID_PROP);
5734 		return;
5735 	}
5736 	PR0("Removing vdisk ID %lu", id);
5737 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
5738 		PRN("No vdisk entry found for vdisk ID %lu", id);
5739 }
5740 
5741 static void
5742 vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
5743     md_t *curr_md, mde_cookie_t curr_vd_node)
5744 {
5745 	char		*curr_dev, *prev_dev;
5746 	uint64_t	curr_id = 0, curr_ldc_id = 0, curr_options = 0;
5747 	uint64_t	prev_id = 0, prev_ldc_id = 0, prev_options = 0;
5748 	size_t		len;
5749 
5750 
5751 	/* Validate that vdisk ID has not changed */
5752 	if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
5753 		PRN("Error getting previous vdisk \"%s\" property",
5754 		    VD_ID_PROP);
5755 		return;
5756 	}
5757 	if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
5758 		PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
5759 		return;
5760 	}
5761 	if (curr_id != prev_id) {
5762 		PRN("Not changing vdisk:  ID changed from %lu to %lu",
5763 		    prev_id, curr_id);
5764 		return;
5765 	}
5766 
5767 	/* Validate that LDC ID has not changed */
5768 	if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
5769 		PRN("Error getting LDC ID for vdisk %lu", prev_id);
5770 		return;
5771 	}
5772 
5773 	if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
5774 		PRN("Error getting LDC ID for vdisk %lu", curr_id);
5775 		return;
5776 	}
5777 	if (curr_ldc_id != prev_ldc_id) {
5778 		_NOTE(NOTREACHED);	/* lint is confused */
5779 		PRN("Not changing vdisk:  "
5780 		    "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
5781 		return;
5782 	}
5783 
5784 	/* Determine whether device path has changed */
5785 	if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
5786 	    &prev_dev) != 0) {
5787 		PRN("Error getting previous vdisk \"%s\"",
5788 		    VD_BLOCK_DEVICE_PROP);
5789 		return;
5790 	}
5791 	if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
5792 	    &curr_dev) != 0) {
5793 		PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
5794 		return;
5795 	}
5796 	if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
5797 	    (strncmp(curr_dev, prev_dev, len) == 0))
5798 		return;	/* no relevant (supported) change */
5799 
5800 	/* Validate that options have not changed */
5801 	vds_get_options(prev_md, prev_vd_node, &prev_options);
5802 	vds_get_options(curr_md, curr_vd_node, &curr_options);
5803 	if (prev_options != curr_options) {
5804 		PRN("Not changing vdisk:  options changed from %lx to %lx",
5805 		    prev_options, curr_options);
5806 		return;
5807 	}
5808 
5809 	PR0("Changing vdisk ID %lu", prev_id);
5810 
5811 	/* Remove old state, which will close vdisk and reset */
5812 	if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
5813 		PRN("No entry found for vdisk ID %lu", prev_id);
5814 
5815 	/* Re-initialize vdisk with new state */
5816 	if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
5817 	    curr_ldc_id) != 0) {
5818 		PRN("Failed to change vdisk ID %lu", curr_id);
5819 		return;
5820 	}
5821 }
5822 
5823 static int
5824 vds_process_md(void *arg, mdeg_result_t *md)
5825 {
5826 	int	i;
5827 	vds_t	*vds = arg;
5828 
5829 
5830 	if (md == NULL)
5831 		return (MDEG_FAILURE);
5832 	ASSERT(vds != NULL);
5833 
5834 	for (i = 0; i < md->removed.nelem; i++)
5835 		vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
5836 	for (i = 0; i < md->match_curr.nelem; i++)
5837 		vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
5838 		    md->match_curr.mdp, md->match_curr.mdep[i]);
5839 	for (i = 0; i < md->added.nelem; i++)
5840 		vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
5841 
5842 	return (MDEG_SUCCESS);
5843 }
5844 
5845 
5846 static int
5847 vds_do_attach(dev_info_t *dip)
5848 {
5849 	int			status, sz;
5850 	int			cfg_handle;
5851 	minor_t			instance = ddi_get_instance(dip);
5852 	vds_t			*vds;
5853 	mdeg_prop_spec_t	*pspecp;
5854 	mdeg_node_spec_t	*ispecp;
5855 
5856 	/*
5857 	 * The "cfg-handle" property of a vds node in an MD contains the MD's
5858 	 * notion of "instance", or unique identifier, for that node; OBP
5859 	 * stores the value of the "cfg-handle" MD property as the value of
5860 	 * the "reg" property on the node in the device tree it builds from
5861 	 * the MD and passes to Solaris.  Thus, we look up the devinfo node's
5862 	 * "reg" property value to uniquely identify this device instance when
5863 	 * registering with the MD event-generation framework.  If the "reg"
5864 	 * property cannot be found, the device tree state is presumably so
5865 	 * broken that there is no point in continuing.
5866 	 */
5867 	if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
5868 	    VD_REG_PROP)) {
5869 		PRN("vds \"%s\" property does not exist", VD_REG_PROP);
5870 		return (DDI_FAILURE);
5871 	}
5872 
5873 	/* Get the MD instance for later MDEG registration */
5874 	cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
5875 	    VD_REG_PROP, -1);
5876 
5877 	if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
5878 		PRN("Could not allocate state for instance %u", instance);
5879 		return (DDI_FAILURE);
5880 	}
5881 
5882 	if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
5883 		PRN("Could not get state for instance %u", instance);
5884 		ddi_soft_state_free(vds_state, instance);
5885 		return (DDI_FAILURE);
5886 	}
5887 
5888 	vds->dip	= dip;
5889 	vds->vd_table	= mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
5890 	    vds_destroy_vd, sizeof (void *));
5891 
5892 	ASSERT(vds->vd_table != NULL);
5893 
5894 	if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
5895 		PRN("ldi_ident_from_dip() returned errno %d", status);
5896 		return (DDI_FAILURE);
5897 	}
5898 	vds->initialized |= VDS_LDI;
5899 
5900 	/* Register for MD updates */
5901 	sz = sizeof (vds_prop_template);
5902 	pspecp = kmem_alloc(sz, KM_SLEEP);
5903 	bcopy(vds_prop_template, pspecp, sz);
5904 
5905 	VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
5906 
5907 	/* initialize the complete prop spec structure */
5908 	ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
5909 	ispecp->namep = "virtual-device";
5910 	ispecp->specp = pspecp;
5911 
5912 	if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
5913 	    &vds->mdeg) != MDEG_SUCCESS) {
5914 		PRN("Unable to register for MD updates");
5915 		kmem_free(ispecp, sizeof (mdeg_node_spec_t));
5916 		kmem_free(pspecp, sz);
5917 		return (DDI_FAILURE);
5918 	}
5919 
5920 	vds->ispecp = ispecp;
5921 	vds->initialized |= VDS_MDEG;
5922 
5923 	/* Prevent auto-detaching so driver is available whenever MD changes */
5924 	if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
5925 	    DDI_PROP_SUCCESS) {
5926 		PRN("failed to set \"%s\" property for instance %u",
5927 		    DDI_NO_AUTODETACH, instance);
5928 	}
5929 
5930 	ddi_report_dev(dip);
5931 	return (DDI_SUCCESS);
5932 }
5933 
5934 static int
5935 vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
5936 {
5937 	int	status;
5938 
5939 	switch (cmd) {
5940 	case DDI_ATTACH:
5941 		PR0("Attaching");
5942 		if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
5943 			(void) vds_detach(dip, DDI_DETACH);
5944 		return (status);
5945 	case DDI_RESUME:
5946 		PR0("No action required for DDI_RESUME");
5947 		return (DDI_SUCCESS);
5948 	default:
5949 		return (DDI_FAILURE);
5950 	}
5951 }
5952 
5953 static struct dev_ops vds_ops = {
5954 	DEVO_REV,	/* devo_rev */
5955 	0,		/* devo_refcnt */
5956 	ddi_no_info,	/* devo_getinfo */
5957 	nulldev,	/* devo_identify */
5958 	nulldev,	/* devo_probe */
5959 	vds_attach,	/* devo_attach */
5960 	vds_detach,	/* devo_detach */
5961 	nodev,		/* devo_reset */
5962 	NULL,		/* devo_cb_ops */
5963 	NULL,		/* devo_bus_ops */
5964 	nulldev		/* devo_power */
5965 };
5966 
5967 static struct modldrv modldrv = {
5968 	&mod_driverops,
5969 	"virtual disk server",
5970 	&vds_ops,
5971 };
5972 
5973 static struct modlinkage modlinkage = {
5974 	MODREV_1,
5975 	&modldrv,
5976 	NULL
5977 };
5978 
5979 
5980 int
5981 _init(void)
5982 {
5983 	int		status;
5984 
5985 	if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
5986 		return (status);
5987 
5988 	if ((status = mod_install(&modlinkage)) != 0) {
5989 		ddi_soft_state_fini(&vds_state);
5990 		return (status);
5991 	}
5992 
5993 	return (0);
5994 }
5995 
5996 int
5997 _info(struct modinfo *modinfop)
5998 {
5999 	return (mod_info(&modlinkage, modinfop));
6000 }
6001 
6002 int
6003 _fini(void)
6004 {
6005 	int	status;
6006 
6007 	if ((status = mod_remove(&modlinkage)) != 0)
6008 		return (status);
6009 	ddi_soft_state_fini(&vds_state);
6010 	return (0);
6011 }
6012