xref: /illumos-gate/usr/src/uts/sun4v/io/vds.c (revision 969dd6c4)
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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2019, Joyent, Inc.
25  */
26 
27 /*
28  * Virtual disk server
29  */
30 
31 
32 #include <sys/types.h>
33 #include <sys/conf.h>
34 #include <sys/crc32.h>
35 #include <sys/ddi.h>
36 #include <sys/dkio.h>
37 #include <sys/file.h>
38 #include <sys/fs/hsfs_isospec.h>
39 #include <sys/mdeg.h>
40 #include <sys/mhd.h>
41 #include <sys/modhash.h>
42 #include <sys/note.h>
43 #include <sys/pathname.h>
44 #include <sys/sdt.h>
45 #include <sys/sunddi.h>
46 #include <sys/sunldi.h>
47 #include <sys/sysmacros.h>
48 #include <sys/vio_common.h>
49 #include <sys/vio_util.h>
50 #include <sys/vdsk_mailbox.h>
51 #include <sys/vdsk_common.h>
52 #include <sys/vtoc.h>
53 #include <sys/vfs.h>
54 #include <sys/stat.h>
55 #include <sys/scsi/impl/uscsi.h>
56 #include <sys/ontrap.h>
57 #include <vm/seg_map.h>
58 
59 #define	ONE_MEGABYTE	(1ULL << 20)
60 #define	ONE_GIGABYTE	(1ULL << 30)
61 #define	ONE_TERABYTE	(1ULL << 40)
62 
63 /* Virtual disk server initialization flags */
64 #define	VDS_LDI			0x01
65 #define	VDS_MDEG		0x02
66 
67 /* Virtual disk server tunable parameters */
68 #define	VDS_RETRIES		5
69 #define	VDS_LDC_DELAY		1000 /* 1 msecs */
70 #define	VDS_DEV_DELAY		10000000 /* 10 secs */
71 #define	VDS_NCHAINS		32
72 
73 /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
74 #define	VDS_NAME		"virtual-disk-server"
75 
76 #define	VD_NAME			"vd"
77 #define	VD_VOLUME_NAME		"vdisk"
78 #define	VD_ASCIILABEL		"Virtual Disk"
79 
80 #define	VD_CHANNEL_ENDPOINT	"channel-endpoint"
81 #define	VD_ID_PROP		"id"
82 #define	VD_BLOCK_DEVICE_PROP	"vds-block-device"
83 #define	VD_BLOCK_DEVICE_OPTS	"vds-block-device-opts"
84 #define	VD_REG_PROP		"reg"
85 
86 /* Virtual disk initialization flags */
87 #define	VD_DISK_READY		0x01
88 #define	VD_LOCKING		0x02
89 #define	VD_LDC			0x04
90 #define	VD_DRING		0x08
91 #define	VD_SID			0x10
92 #define	VD_SEQ_NUM		0x20
93 #define	VD_SETUP_ERROR		0x40
94 
95 /* Number of backup labels */
96 #define	VD_DSKIMG_NUM_BACKUP	5
97 
98 /* Timeout for SCSI I/O */
99 #define	VD_SCSI_RDWR_TIMEOUT	30	/* 30 secs */
100 
101 /*
102  * Default number of threads for the I/O queue. In many cases, we will not
103  * receive more than 8 I/O requests at the same time. However there are
104  * cases (for example during the OS installation) where we can have a lot
105  * more (up to the limit of the DRing size).
106  */
107 #define	VD_IOQ_NTHREADS		8
108 
109 /* Maximum number of logical partitions */
110 #define	VD_MAXPART	(NDKMAP + 1)
111 
112 /*
113  * By Solaris convention, slice/partition 2 represents the entire disk;
114  * unfortunately, this convention does not appear to be codified.
115  */
116 #define	VD_ENTIRE_DISK_SLICE	2
117 
118 /* Logical block address for EFI */
119 #define	VD_EFI_LBA_GPT		1	/* LBA of the GPT */
120 #define	VD_EFI_LBA_GPE		2	/* LBA of the GPE */
121 
122 #define	VD_EFI_DEV_SET(dev, vdsk, ioctl)	\
123 	VDSK_EFI_DEV_SET(dev, vdsk, ioctl,	\
124 	    (vdsk)->vdisk_bsize, (vdsk)->vdisk_size)
125 
126 /*
127  * Flags defining the behavior for flushing asynchronous writes used to
128  * performed some write I/O requests.
129  *
130  * The VD_AWFLUSH_IMMEDIATE enables immediate flushing of asynchronous
131  * writes. This ensures that data are committed to the backend when the I/O
132  * request reply is sent to the guest domain so this prevents any data to
133  * be lost in case a service domain unexpectedly crashes.
134  *
135  * The flag VD_AWFLUSH_DEFER indicates that flushing is deferred to another
136  * thread while the request is immediatly marked as completed. In that case,
137  * a guest domain can a receive a reply that its write request is completed
138  * while data haven't been flushed to disk yet.
139  *
140  * Flags VD_AWFLUSH_IMMEDIATE and VD_AWFLUSH_DEFER are mutually exclusive.
141  */
142 #define	VD_AWFLUSH_IMMEDIATE	0x01	/* immediate flushing */
143 #define	VD_AWFLUSH_DEFER	0x02	/* defer flushing */
144 #define	VD_AWFLUSH_GROUP	0x04	/* group requests before flushing */
145 
146 /* Driver types */
147 typedef enum vd_driver {
148 	VD_DRIVER_UNKNOWN = 0,	/* driver type unknown  */
149 	VD_DRIVER_DISK,		/* disk driver */
150 	VD_DRIVER_VOLUME	/* volume driver */
151 } vd_driver_t;
152 
153 #define	VD_DRIVER_NAME_LEN	64
154 
155 #define	VDS_NUM_DRIVERS	(sizeof (vds_driver_types) / sizeof (vd_driver_type_t))
156 
157 typedef struct vd_driver_type {
158 	char name[VD_DRIVER_NAME_LEN];	/* driver name */
159 	vd_driver_t type;		/* driver type (disk or volume) */
160 } vd_driver_type_t;
161 
162 /*
163  * There is no reliable way to determine if a device is representing a disk
164  * or a volume, especially with pseudo devices. So we maintain a list of well
165  * known drivers and the type of device they represent (either a disk or a
166  * volume).
167  *
168  * The list can be extended by adding a "driver-type-list" entry in vds.conf
169  * with the following syntax:
170  *
171  *	driver-type-list="<driver>:<type>", ... ,"<driver>:<type>";
172  *
173  * Where:
174  *	<driver> is the name of a driver (limited to 64 characters)
175  *	<type> is either the string "disk" or "volume"
176  *
177  * Invalid entries in "driver-type-list" will be ignored.
178  *
179  * For example, the following line in vds.conf:
180  *
181  *	driver-type-list="foo:disk","bar:volume";
182  *
183  * defines that "foo" is a disk driver, and driver "bar" is a volume driver.
184  *
185  * When a list is defined in vds.conf, it is checked before the built-in list
186  * (vds_driver_types[]) so that any definition from this list can be overriden
187  * using vds.conf.
188  */
189 vd_driver_type_t vds_driver_types[] = {
190 	{ "dad",	VD_DRIVER_DISK },	/* Solaris */
191 	{ "did",	VD_DRIVER_DISK },	/* Sun Cluster */
192 	{ "dlmfdrv",	VD_DRIVER_DISK },	/* Hitachi HDLM */
193 	{ "emcp",	VD_DRIVER_DISK },	/* EMC Powerpath */
194 	{ "lofi",	VD_DRIVER_VOLUME },	/* Solaris */
195 	{ "md",		VD_DRIVER_VOLUME },	/* Solaris - SVM */
196 	{ "sd",		VD_DRIVER_DISK },	/* Solaris */
197 	{ "ssd",	VD_DRIVER_DISK },	/* Solaris */
198 	{ "vdc",	VD_DRIVER_DISK },	/* Solaris */
199 	{ "vxdmp",	VD_DRIVER_DISK },	/* Veritas */
200 	{ "vxio",	VD_DRIVER_VOLUME },	/* Veritas - VxVM */
201 	{ "zfs",	VD_DRIVER_VOLUME }	/* Solaris */
202 };
203 
204 /* Return a cpp token as a string */
205 #define	STRINGIZE(token)	#token
206 
207 /*
208  * Print a message prefixed with the current function name to the message log
209  * (and optionally to the console for verbose boots); these macros use cpp's
210  * concatenation of string literals and C99 variable-length-argument-list
211  * macros
212  */
213 #define	PRN(...)	_PRN("?%s():  "__VA_ARGS__, "")
214 #define	_PRN(format, ...)					\
215 	cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
216 
217 /* Return a pointer to the "i"th vdisk dring element */
218 #define	VD_DRING_ELEM(i)	((vd_dring_entry_t *)(void *)	\
219 	    (vd->dring + (i)*vd->descriptor_size))
220 
221 /* Return the virtual disk client's type as a string (for use in messages) */
222 #define	VD_CLIENT(vd)							\
223 	(((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" :	\
224 	    (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" :    \
225 		(((vd)->xfer_mode == 0) ? "null client" :		\
226 		    "unsupported client")))
227 
228 /* Read disk label from a disk image */
229 #define	VD_DSKIMG_LABEL_READ(vd, labelp) \
230 	vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
231 	    0, sizeof (struct dk_label))
232 
233 /* Write disk label to a disk image */
234 #define	VD_DSKIMG_LABEL_WRITE(vd, labelp)	\
235 	vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
236 	    0, sizeof (struct dk_label))
237 
238 /* Identify if a backend is a disk image */
239 #define	VD_DSKIMG(vd)	((vd)->vdisk_type == VD_DISK_TYPE_DISK &&	\
240 	((vd)->file || (vd)->volume))
241 
242 /* Next index in a write queue */
243 #define	VD_WRITE_INDEX_NEXT(vd, id)		\
244 	((((id) + 1) >= vd->dring_len)? 0 : (id) + 1)
245 
246 /* Message for disk access rights reset failure */
247 #define	VD_RESET_ACCESS_FAILURE_MSG \
248 	"Fail to reset disk access rights for disk %s"
249 
250 /*
251  * Specification of an MD node passed to the MDEG to filter any
252  * 'vport' nodes that do not belong to the specified node. This
253  * template is copied for each vds instance and filled in with
254  * the appropriate 'cfg-handle' value before being passed to the MDEG.
255  */
256 static mdeg_prop_spec_t	vds_prop_template[] = {
257 	{ MDET_PROP_STR,	"name",		VDS_NAME },
258 	{ MDET_PROP_VAL,	"cfg-handle",	NULL },
259 	{ MDET_LIST_END,	NULL,		NULL }
260 };
261 
262 #define	VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
263 
264 /*
265  * Matching criteria passed to the MDEG to register interest
266  * in changes to 'virtual-device-port' nodes identified by their
267  * 'id' property.
268  */
269 static md_prop_match_t	vd_prop_match[] = {
270 	{ MDET_PROP_VAL,	VD_ID_PROP },
271 	{ MDET_LIST_END,	NULL }
272 };
273 
274 static mdeg_node_match_t vd_match = {"virtual-device-port",
275 				    vd_prop_match};
276 
277 /*
278  * Options for the VD_BLOCK_DEVICE_OPTS property.
279  */
280 #define	VD_OPT_RDONLY		0x1	/* read-only  */
281 #define	VD_OPT_SLICE		0x2	/* single slice */
282 #define	VD_OPT_EXCLUSIVE	0x4	/* exclusive access */
283 
284 #define	VD_OPTION_NLEN	128
285 
286 typedef struct vd_option {
287 	char vdo_name[VD_OPTION_NLEN];
288 	uint64_t vdo_value;
289 } vd_option_t;
290 
291 vd_option_t vd_bdev_options[] = {
292 	{ "ro",		VD_OPT_RDONLY },
293 	{ "slice",	VD_OPT_SLICE },
294 	{ "excl",	VD_OPT_EXCLUSIVE }
295 };
296 
297 /* Debugging macros */
298 #ifdef DEBUG
299 
300 static int	vd_msglevel = 0;
301 
302 #define	PR0 if (vd_msglevel > 0)	PRN
303 #define	PR1 if (vd_msglevel > 1)	PRN
304 #define	PR2 if (vd_msglevel > 2)	PRN
305 
306 #define	VD_DUMP_DRING_ELEM(elem)					\
307 	PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n",		\
308 	    elem->hdr.dstate,						\
309 	    elem->payload.operation,					\
310 	    elem->payload.status,					\
311 	    elem->payload.nbytes,					\
312 	    elem->payload.addr,						\
313 	    elem->payload.ncookies);
314 
315 char *
vd_decode_state(int state)316 vd_decode_state(int state)
317 {
318 	char *str;
319 
320 #define	CASE_STATE(_s)	case _s: str = #_s; break;
321 
322 	switch (state) {
323 	CASE_STATE(VD_STATE_INIT)
324 	CASE_STATE(VD_STATE_VER)
325 	CASE_STATE(VD_STATE_ATTR)
326 	CASE_STATE(VD_STATE_DRING)
327 	CASE_STATE(VD_STATE_RDX)
328 	CASE_STATE(VD_STATE_DATA)
329 	default: str = "unknown"; break;
330 	}
331 
332 #undef CASE_STATE
333 
334 	return (str);
335 }
336 
337 void
vd_decode_tag(vio_msg_t * msg)338 vd_decode_tag(vio_msg_t *msg)
339 {
340 	char *tstr, *sstr, *estr;
341 
342 #define	CASE_TYPE(_s)	case _s: tstr = #_s; break;
343 
344 	switch (msg->tag.vio_msgtype) {
345 	CASE_TYPE(VIO_TYPE_CTRL)
346 	CASE_TYPE(VIO_TYPE_DATA)
347 	CASE_TYPE(VIO_TYPE_ERR)
348 	default: tstr = "unknown"; break;
349 	}
350 
351 #undef CASE_TYPE
352 
353 #define	CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
354 
355 	switch (msg->tag.vio_subtype) {
356 	CASE_SUBTYPE(VIO_SUBTYPE_INFO)
357 	CASE_SUBTYPE(VIO_SUBTYPE_ACK)
358 	CASE_SUBTYPE(VIO_SUBTYPE_NACK)
359 	default: sstr = "unknown"; break;
360 	}
361 
362 #undef CASE_SUBTYPE
363 
364 #define	CASE_ENV(_s)	case _s: estr = #_s; break;
365 
366 	switch (msg->tag.vio_subtype_env) {
367 	CASE_ENV(VIO_VER_INFO)
368 	CASE_ENV(VIO_ATTR_INFO)
369 	CASE_ENV(VIO_DRING_REG)
370 	CASE_ENV(VIO_DRING_UNREG)
371 	CASE_ENV(VIO_RDX)
372 	CASE_ENV(VIO_PKT_DATA)
373 	CASE_ENV(VIO_DESC_DATA)
374 	CASE_ENV(VIO_DRING_DATA)
375 	default: estr = "unknown"; break;
376 	}
377 
378 #undef CASE_ENV
379 
380 	PR1("(%x/%x/%x) message : (%s/%s/%s)",
381 	    msg->tag.vio_msgtype, msg->tag.vio_subtype,
382 	    msg->tag.vio_subtype_env, tstr, sstr, estr);
383 }
384 
385 #else	/* !DEBUG */
386 
387 #define	PR0(...)
388 #define	PR1(...)
389 #define	PR2(...)
390 
391 #define	VD_DUMP_DRING_ELEM(elem)
392 
393 #define	vd_decode_state(_s)	(NULL)
394 #define	vd_decode_tag(_s)	(NULL)
395 
396 #endif	/* DEBUG */
397 
398 
399 /*
400  * Soft state structure for a vds instance
401  */
402 typedef struct vds {
403 	uint_t		initialized;	/* driver inst initialization flags */
404 	dev_info_t	*dip;		/* driver inst devinfo pointer */
405 	ldi_ident_t	ldi_ident;	/* driver's identifier for LDI */
406 	mod_hash_t	*vd_table;	/* table of virtual disks served */
407 	mdeg_node_spec_t *ispecp;	/* mdeg node specification */
408 	mdeg_handle_t	mdeg;		/* handle for MDEG operations  */
409 	vd_driver_type_t *driver_types;	/* extra driver types (from vds.conf) */
410 	int		num_drivers;	/* num of extra driver types */
411 } vds_t;
412 
413 /*
414  * Types of descriptor-processing tasks
415  */
416 typedef enum vd_task_type {
417 	VD_NONFINAL_RANGE_TASK,	/* task for intermediate descriptor in range */
418 	VD_FINAL_RANGE_TASK,	/* task for last in a range of descriptors */
419 } vd_task_type_t;
420 
421 /*
422  * Structure describing the task for processing a descriptor
423  */
424 typedef struct vd_task {
425 	struct vd		*vd;		/* vd instance task is for */
426 	vd_task_type_t		type;		/* type of descriptor task */
427 	int			index;		/* dring elem index for task */
428 	vio_msg_t		*msg;		/* VIO message task is for */
429 	size_t			msglen;		/* length of message content */
430 	vd_dring_payload_t	*request;	/* request task will perform */
431 	struct buf		buf;		/* buf(9s) for I/O request */
432 	ldc_mem_handle_t	mhdl;		/* task memory handle */
433 	int			status;		/* status of processing task */
434 	int	(*completef)(struct vd_task *task); /* completion func ptr */
435 	uint32_t		write_index;	/* index in the write_queue */
436 } vd_task_t;
437 
438 /*
439  * Soft state structure for a virtual disk instance
440  */
441 typedef struct vd {
442 	uint64_t		id;		/* vdisk id */
443 	uint_t			initialized;	/* vdisk initialization flags */
444 	uint64_t		operations;	/* bitmask of VD_OPs exported */
445 	vio_ver_t		version;	/* ver negotiated with client */
446 	vds_t			*vds;		/* server for this vdisk */
447 	ddi_taskq_t		*startq;	/* queue for I/O start tasks */
448 	ddi_taskq_t		*completionq;	/* queue for completion tasks */
449 	ddi_taskq_t		*ioq;		/* queue for I/O */
450 	uint32_t		write_index;	/* next write index */
451 	buf_t			**write_queue;	/* queue for async writes */
452 	ldi_handle_t		ldi_handle[V_NUMPAR];	/* LDI slice handles */
453 	char			device_path[MAXPATHLEN + 1]; /* vdisk device */
454 	dev_t			dev[V_NUMPAR];	/* dev numbers for slices */
455 	int			open_flags;	/* open flags */
456 	uint_t			nslices;	/* number of slices we export */
457 	size_t			vdisk_size;	/* number of blocks in vdisk */
458 	size_t			vdisk_bsize;	/* blk size of the vdisk */
459 	vd_disk_type_t		vdisk_type;	/* slice or entire disk */
460 	vd_disk_label_t		vdisk_label;	/* EFI or VTOC label */
461 	vd_media_t		vdisk_media;	/* media type of backing dev. */
462 	boolean_t		is_atapi_dev;	/* Is this an IDE CD-ROM dev? */
463 	ushort_t		max_xfer_sz;	/* max xfer size in DEV_BSIZE */
464 	size_t			backend_bsize;	/* blk size of backend device */
465 	int			vio_bshift;	/* shift for blk convertion */
466 	boolean_t		volume;		/* is vDisk backed by volume */
467 	boolean_t		zvol;		/* is vDisk backed by a zvol */
468 	boolean_t		file;		/* is vDisk backed by a file? */
469 	boolean_t		scsi;		/* is vDisk backed by scsi? */
470 	vnode_t			*file_vnode;	/* file vnode */
471 	size_t			dskimg_size;	/* size of disk image */
472 	ddi_devid_t		dskimg_devid;	/* devid for disk image */
473 	int			efi_reserved;	/* EFI reserved slice */
474 	caddr_t			flabel;		/* fake label for slice type */
475 	uint_t			flabel_size;	/* fake label size */
476 	uint_t			flabel_limit;	/* limit of the fake label */
477 	struct dk_geom		dk_geom;	/* synthetic for slice type */
478 	struct extvtoc		vtoc;		/* synthetic for slice type */
479 	vd_slice_t		slices[VD_MAXPART]; /* logical partitions */
480 	boolean_t		ownership;	/* disk ownership status */
481 	ldc_status_t		ldc_state;	/* LDC connection state */
482 	ldc_handle_t		ldc_handle;	/* handle for LDC comm */
483 	size_t			max_msglen;	/* largest LDC message len */
484 	vd_state_t		state;		/* client handshake state */
485 	uint8_t			xfer_mode;	/* transfer mode with client */
486 	uint32_t		sid;		/* client's session ID */
487 	uint64_t		seq_num;	/* message sequence number */
488 	uint64_t		dring_ident;	/* identifier of dring */
489 	ldc_dring_handle_t	dring_handle;	/* handle for dring ops */
490 	uint32_t		descriptor_size;	/* num bytes in desc */
491 	uint32_t		dring_len;	/* number of dring elements */
492 	uint8_t			dring_mtype;	/* dring mem map type */
493 	caddr_t			dring;		/* address of dring */
494 	caddr_t			vio_msgp;	/* vio msg staging buffer */
495 	vd_task_t		inband_task;	/* task for inband descriptor */
496 	vd_task_t		*dring_task;	/* tasks dring elements */
497 
498 	kmutex_t		lock;		/* protects variables below */
499 	boolean_t		enabled;	/* is vdisk enabled? */
500 	boolean_t		reset_state;	/* reset connection state? */
501 	boolean_t		reset_ldc;	/* reset LDC channel? */
502 } vd_t;
503 
504 /*
505  * Macros to manipulate the fake label (flabel) for single slice disks.
506  *
507  * If we fake a VTOC label then the fake label consists of only one block
508  * containing the VTOC label (struct dk_label).
509  *
510  * If we fake an EFI label then the fake label consists of a blank block
511  * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t).
512  *
513  */
514 #define	VD_LABEL_VTOC_SIZE(lba)					\
515 	P2ROUNDUP(sizeof (struct dk_label), (lba))
516 
517 #define	VD_LABEL_EFI_SIZE(lba)					\
518 	P2ROUNDUP(2 * (lba) + sizeof (efi_gpe_t) * VD_MAXPART,	\
519 	    (lba))
520 
521 #define	VD_LABEL_VTOC(vd)	\
522 		((struct dk_label *)(void *)((vd)->flabel))
523 
524 #define	VD_LABEL_EFI_GPT(vd, lba)	\
525 		((efi_gpt_t *)(void *)((vd)->flabel + (lba)))
526 #define	VD_LABEL_EFI_GPE(vd, lba)	\
527 		((efi_gpe_t *)(void *)((vd)->flabel + 2 * (lba)))
528 
529 
530 typedef struct vds_operation {
531 	char	*namep;
532 	uint8_t	operation;
533 	int	(*start)(vd_task_t *task);
534 	int	(*complete)(vd_task_t *task);
535 } vds_operation_t;
536 
537 typedef struct vd_ioctl {
538 	uint8_t		operation;		/* vdisk operation */
539 	const char	*operation_name;	/* vdisk operation name */
540 	size_t		nbytes;			/* size of operation buffer */
541 	int		cmd;			/* corresponding ioctl cmd */
542 	const char	*cmd_name;		/* ioctl cmd name */
543 	void		*arg;			/* ioctl cmd argument */
544 	/* convert input vd_buf to output ioctl_arg */
545 	int		(*copyin)(void *vd_buf, size_t, void *ioctl_arg);
546 	/* convert input ioctl_arg to output vd_buf */
547 	void		(*copyout)(void *ioctl_arg, void *vd_buf);
548 	/* write is true if the operation writes any data to the backend */
549 	boolean_t	write;
550 } vd_ioctl_t;
551 
552 /* Define trivial copyin/copyout conversion function flag */
553 #define	VD_IDENTITY_IN	((int (*)(void *, size_t, void *))-1)
554 #define	VD_IDENTITY_OUT	((void (*)(void *, void *))-1)
555 
556 
557 static int	vds_ldc_retries = VDS_RETRIES;
558 static int	vds_ldc_delay = VDS_LDC_DELAY;
559 static int	vds_dev_retries = VDS_RETRIES;
560 static int	vds_dev_delay = VDS_DEV_DELAY;
561 static void	*vds_state;
562 
563 static short	vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
564 static int	vd_scsi_debug = USCSI_SILENT;
565 
566 /*
567  * Number of threads in the taskq handling vdisk I/O. This can be set up to
568  * the size of the DRing which is the maximum number of I/O we can receive
569  * in parallel. Note that using a high number of threads can improve performance
570  * but this is going to consume a lot of resources if there are many vdisks.
571  */
572 static int	vd_ioq_nthreads = VD_IOQ_NTHREADS;
573 
574 /*
575  * Tunable to define the behavior for flushing asynchronous writes used to
576  * performed some write I/O requests. The default behavior is to group as
577  * much asynchronous writes as possible and to flush them immediatly.
578  *
579  * If the tunable is set to 0 then explicit flushing is disabled. In that
580  * case, data will be flushed by traditional mechanism (like fsflush) but
581  * this might not happen immediatly.
582  *
583  */
584 static int	vd_awflush = VD_AWFLUSH_IMMEDIATE | VD_AWFLUSH_GROUP;
585 
586 /*
587  * Tunable to define the behavior of the service domain if the vdisk server
588  * fails to reset disk exclusive access when a LDC channel is reset. When a
589  * LDC channel is reset the vdisk server will try to reset disk exclusive
590  * access by releasing any SCSI-2 reservation or resetting the disk. If these
591  * actions fail then the default behavior (vd_reset_access_failure = 0) is to
592  * print a warning message. This default behavior can be changed by setting
593  * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
594  * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
595  * the service domain to panic. In both cases, the reset of the service domain
596  * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
597  */
598 static int	vd_reset_access_failure = 0;
599 
600 /*
601  * Tunable for backward compatibility. When this variable is set to B_TRUE,
602  * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
603  * slice disks whether or not they have the "slice" option set. This is
604  * to provide a simple backward compatibility mechanism when upgrading
605  * the vds driver and using a domain configuration created before the
606  * "slice" option was available.
607  */
608 static boolean_t vd_volume_force_slice = B_FALSE;
609 
610 /*
611  * The label of disk images created with some earlier versions of the virtual
612  * disk software is not entirely correct and have an incorrect v_sanity field
613  * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with
614  * these images because we are now validating that the disk label (and the
615  * sanity) is correct when a disk image is opened.
616  *
617  * This tunable is set to false to not validate the sanity field and ensure
618  * compatibility. If the tunable is set to true, we will do a strict checking
619  * of the sanity but this can create compatibility problems with old disk
620  * images.
621  */
622 static boolean_t vd_dskimg_validate_sanity = B_FALSE;
623 
624 /*
625  * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings.
626  */
627 static boolean_t vd_direct_mapped_drings = B_TRUE;
628 
629 /*
630  * When a backend is exported as a single-slice disk then we entirely fake
631  * its disk label. So it can be exported either with a VTOC label or with
632  * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all
633  * single-slice disks will be exported with a VTOC label; and if it is set
634  * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with
635  * an EFI label.
636  *
637  * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk
638  * or volume device then it will be exported with the same type of label as
639  * defined on the device. Otherwise if the backend is a file then it will
640  * exported with the disk label type set in the vd_file_slice_label variable.
641  *
642  * Note that if the backend size is greater than 1TB then it will always be
643  * exported with an EFI label no matter what the setting is.
644  */
645 static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK;
646 
647 static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC;
648 
649 /*
650  * Tunable for backward compatibility. If this variable is set to B_TRUE then
651  * single-slice disks are exported as disks with only one slice instead of
652  * faking a complete disk partitioning.
653  */
654 static boolean_t vd_slice_single_slice = B_FALSE;
655 
656 /*
657  * Supported protocol version pairs, from highest (newest) to lowest (oldest)
658  *
659  * Each supported major version should appear only once, paired with (and only
660  * with) its highest supported minor version number (as the protocol requires
661  * supporting all lower minor version numbers as well)
662  */
663 static const vio_ver_t	vds_version[] = {{1, 1}};
664 static const size_t	vds_num_versions =
665     sizeof (vds_version)/sizeof (vds_version[0]);
666 
667 static void vd_free_dring_task(vd_t *vdp);
668 static int vd_setup_vd(vd_t *vd);
669 static int vd_setup_single_slice_disk(vd_t *vd);
670 static int vd_setup_slice_image(vd_t *vd);
671 static int vd_setup_disk_image(vd_t *vd);
672 static int vd_backend_check_size(vd_t *vd);
673 static boolean_t vd_enabled(vd_t *vd);
674 static ushort_t vd_lbl2cksum(struct dk_label *label);
675 static int vd_dskimg_validate_geometry(vd_t *vd);
676 static boolean_t vd_dskimg_is_iso_image(vd_t *vd);
677 static void vd_set_exported_operations(vd_t *vd);
678 static void vd_reset_access(vd_t *vd);
679 static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg);
680 static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **);
681 static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *);
682 static void vds_driver_types_free(vds_t *vds);
683 static void vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom,
684     struct dk_label *label);
685 static void vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc,
686     struct dk_geom *geom);
687 static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom);
688 static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc);
689 
690 extern int is_pseudo_device(dev_info_t *);
691 
692 /*
693  * Function:
694  *	vd_get_readable_size
695  *
696  * Description:
697  *	Convert a given size in bytes to a human readable format in
698  *	kilobytes, megabytes, gigabytes or terabytes.
699  *
700  * Parameters:
701  *	full_size	- the size to convert in bytes.
702  *	size		- the converted size.
703  *	unit		- the unit of the converted size: 'K' (kilobyte),
704  *			  'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte).
705  *
706  * Return Code:
707  *	none
708  */
709 static void
vd_get_readable_size(size_t full_size,size_t * size,char * unit)710 vd_get_readable_size(size_t full_size, size_t *size, char *unit)
711 {
712 	if (full_size < (1ULL << 20)) {
713 		*size = full_size >> 10;
714 		*unit = 'K'; /* Kilobyte */
715 	} else if (full_size < (1ULL << 30)) {
716 		*size = full_size >> 20;
717 		*unit = 'M'; /* Megabyte */
718 	} else if (full_size < (1ULL << 40)) {
719 		*size = full_size >> 30;
720 		*unit = 'G'; /* Gigabyte */
721 	} else {
722 		*size = full_size >> 40;
723 		*unit = 'T'; /* Terabyte */
724 	}
725 }
726 
727 /*
728  * Function:
729  *	vd_dskimg_io_params
730  *
731  * Description:
732  *	Convert virtual disk I/O parameters (slice, block, length) to
733  *	(offset, length) relative to the disk image and according to
734  *	the virtual disk partitioning.
735  *
736  * Parameters:
737  *	vd		- disk on which the operation is performed.
738  *	slice		- slice to which is the I/O parameters apply.
739  *			  VD_SLICE_NONE indicates that parameters are
740  *			  are relative to the entire virtual disk.
741  *	blkp		- pointer to the starting block relative to the
742  *			  slice; return the starting block relative to
743  *			  the disk image.
744  *	lenp		- pointer to the number of bytes requested; return
745  *			  the number of bytes that can effectively be used.
746  *
747  * Return Code:
748  *	0		- I/O parameters have been successfully converted;
749  *			  blkp and lenp point to the converted values.
750  *	ENODATA		- no data are available for the given I/O parameters;
751  *			  This occurs if the starting block is past the limit
752  *			  of the slice.
753  *	EINVAL		- I/O parameters are invalid.
754  */
755 static int
vd_dskimg_io_params(vd_t * vd,int slice,size_t * blkp,size_t * lenp)756 vd_dskimg_io_params(vd_t *vd, int slice, size_t *blkp, size_t *lenp)
757 {
758 	size_t blk = *blkp;
759 	size_t len = *lenp;
760 	size_t offset, maxlen;
761 
762 	ASSERT(vd->file || VD_DSKIMG(vd));
763 	ASSERT(len > 0);
764 	ASSERT(vd->vdisk_bsize == DEV_BSIZE);
765 
766 	/*
767 	 * If a file is exported as a slice then we don't care about the vtoc.
768 	 * In that case, the vtoc is a fake mainly to make newfs happy and we
769 	 * handle any I/O as a raw disk access so that we can have access to the
770 	 * entire backend.
771 	 */
772 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
773 		/* raw disk access */
774 		offset = blk * DEV_BSIZE;
775 		if (offset >= vd->dskimg_size) {
776 			/* offset past the end of the disk */
777 			PR0("offset (0x%lx) >= size (0x%lx)",
778 			    offset, vd->dskimg_size);
779 			return (ENODATA);
780 		}
781 		maxlen = vd->dskimg_size - offset;
782 	} else {
783 		ASSERT(slice >= 0 && slice < V_NUMPAR);
784 
785 		/*
786 		 * v1.0 vDisk clients depended on the server not verifying
787 		 * the label of a unformatted disk.  This "feature" is
788 		 * maintained for backward compatibility but all versions
789 		 * from v1.1 onwards must do the right thing.
790 		 */
791 		if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
792 		    vio_ver_is_supported(vd->version, 1, 1)) {
793 			(void) vd_dskimg_validate_geometry(vd);
794 			if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
795 				PR0("Unknown disk label, can't do I/O "
796 				    "from slice %d", slice);
797 				return (EINVAL);
798 			}
799 		}
800 
801 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
802 			ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE);
803 		} else {
804 			ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI);
805 		}
806 
807 		if (blk >= vd->slices[slice].nblocks) {
808 			/* address past the end of the slice */
809 			PR0("req_addr (0x%lx) >= psize (0x%lx)",
810 			    blk, vd->slices[slice].nblocks);
811 			return (ENODATA);
812 		}
813 
814 		offset = (vd->slices[slice].start + blk) * DEV_BSIZE;
815 		maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE;
816 	}
817 
818 	/*
819 	 * If the requested size is greater than the size
820 	 * of the partition, truncate the read/write.
821 	 */
822 	if (len > maxlen) {
823 		PR0("I/O size truncated to %lu bytes from %lu bytes",
824 		    maxlen, len);
825 		len = maxlen;
826 	}
827 
828 	/*
829 	 * We have to ensure that we are reading/writing into the mmap
830 	 * range. If we have a partial disk image (e.g. an image of
831 	 * s0 instead s2) the system can try to access slices that
832 	 * are not included into the disk image.
833 	 */
834 	if ((offset + len) > vd->dskimg_size) {
835 		PR0("offset + nbytes (0x%lx + 0x%lx) > "
836 		    "dskimg_size (0x%lx)", offset, len, vd->dskimg_size);
837 		return (EINVAL);
838 	}
839 
840 	*blkp = offset / DEV_BSIZE;
841 	*lenp = len;
842 
843 	return (0);
844 }
845 
846 /*
847  * Function:
848  *	vd_dskimg_rw
849  *
850  * Description:
851  *	Read or write to a disk image. It handles the case where the disk
852  *	image is a file or a volume exported as a full disk or a file
853  *	exported as single-slice disk. Read or write to volumes exported as
854  *	single slice disks are done by directly using the ldi interface.
855  *
856  * Parameters:
857  *	vd		- disk on which the operation is performed.
858  *	slice		- slice on which the operation is performed,
859  *			  VD_SLICE_NONE indicates that the operation
860  *			  is done using an absolute disk offset.
861  *	operation	- operation to execute: read (VD_OP_BREAD) or
862  *			  write (VD_OP_BWRITE).
863  *	data		- buffer where data are read to or written from.
864  *	blk		- starting block for the operation.
865  *	len		- number of bytes to read or write.
866  *
867  * Return Code:
868  *	n >= 0		- success, n indicates the number of bytes read
869  *			  or written.
870  *	-1		- error.
871  */
872 static ssize_t
vd_dskimg_rw(vd_t * vd,int slice,int operation,caddr_t data,size_t offset,size_t len)873 vd_dskimg_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t offset,
874     size_t len)
875 {
876 	ssize_t resid;
877 	struct buf buf;
878 	int status;
879 
880 	ASSERT(vd->file || VD_DSKIMG(vd));
881 	ASSERT(len > 0);
882 	ASSERT(vd->vdisk_bsize == DEV_BSIZE);
883 
884 	if ((status = vd_dskimg_io_params(vd, slice, &offset, &len)) != 0)
885 		return ((status == ENODATA)? 0: -1);
886 
887 	if (vd->volume) {
888 
889 		bioinit(&buf);
890 		buf.b_flags	= B_BUSY |
891 		    ((operation == VD_OP_BREAD)? B_READ : B_WRITE);
892 		buf.b_bcount	= len;
893 		buf.b_lblkno	= offset;
894 		buf.b_edev	= vd->dev[0];
895 		buf.b_un.b_addr = data;
896 
897 		/*
898 		 * We use ldi_strategy() and not ldi_read()/ldi_write() because
899 		 * the read/write functions of the underlying driver may try to
900 		 * lock pages of the data buffer, and this requires the data
901 		 * buffer to be kmem_alloc'ed (and not allocated on the stack).
902 		 *
903 		 * Also using ldi_strategy() ensures that writes are immediatly
904 		 * commited and not cached as this may be the case with
905 		 * ldi_write() (for example with a ZFS volume).
906 		 */
907 		if (ldi_strategy(vd->ldi_handle[0], &buf) != 0) {
908 			biofini(&buf);
909 			return (-1);
910 		}
911 
912 		if (biowait(&buf) != 0) {
913 			biofini(&buf);
914 			return (-1);
915 		}
916 
917 		resid = buf.b_resid;
918 		biofini(&buf);
919 
920 		ASSERT(resid <= len);
921 		return (len - resid);
922 	}
923 
924 	ASSERT(vd->file);
925 
926 	status = vn_rdwr((operation == VD_OP_BREAD)? UIO_READ : UIO_WRITE,
927 	    vd->file_vnode, data, len, offset * DEV_BSIZE, UIO_SYSSPACE, FSYNC,
928 	    RLIM64_INFINITY, kcred, &resid);
929 
930 	if (status != 0)
931 		return (-1);
932 
933 	return (len);
934 }
935 
936 /*
937  * Function:
938  *	vd_build_default_label
939  *
940  * Description:
941  *	Return a default label for a given disk size. This is used when the disk
942  *	does not have a valid VTOC so that the user can get a valid default
943  *	configuration. The default label has all slice sizes set to 0 (except
944  *	slice 2 which is the entire disk) to force the user to write a valid
945  *	label onto the disk image.
946  *
947  * Parameters:
948  *	disk_size	- the disk size in bytes
949  *	bsize		- the disk block size in bytes
950  *	label		- the returned default label.
951  *
952  * Return Code:
953  *	none.
954  */
955 static void
vd_build_default_label(size_t disk_size,size_t bsize,struct dk_label * label)956 vd_build_default_label(size_t disk_size, size_t bsize, struct dk_label *label)
957 {
958 	size_t size;
959 	char unit;
960 
961 	ASSERT(bsize > 0);
962 
963 	bzero(label, sizeof (struct dk_label));
964 
965 	/*
966 	 * Ideally we would like the cylinder size (nsect * nhead) to be the
967 	 * same whatever the disk size is. That way the VTOC label could be
968 	 * easily updated in case the disk size is increased (keeping the
969 	 * same cylinder size allows to preserve the existing partitioning
970 	 * when updating the VTOC label). But it is not possible to have
971 	 * a fixed cylinder size and to cover all disk size.
972 	 *
973 	 * So we define different cylinder sizes depending on the disk size.
974 	 * The cylinder size is chosen so that we don't have too few cylinders
975 	 * for a small disk image, or so many on a big disk image that you
976 	 * waste space for backup superblocks or cylinder group structures.
977 	 * Also we must have a resonable number of cylinders and sectors so
978 	 * that newfs can run using default values.
979 	 *
980 	 *	+-----------+--------+---------+--------+
981 	 *	| disk_size |  < 2MB | 2MB-4GB | >= 8GB |
982 	 *	+-----------+--------+---------+--------+
983 	 *	| nhead	    |	 1   |	   1   |    96  |
984 	 *	| nsect	    |  200   |   600   |   768  |
985 	 *	+-----------+--------+---------+--------+
986 	 *
987 	 * Other parameters are computed from these values:
988 	 *
989 	 *	pcyl = disk_size / (nhead * nsect * 512)
990 	 *	acyl = (pcyl > 2)? 2 : 0
991 	 *	ncyl = pcyl - acyl
992 	 *
993 	 * The maximum number of cylinder is 65535 so this allows to define a
994 	 * geometry for a disk size up to 65535 * 96 * 768 * 512 = 2.24 TB
995 	 * which is more than enough to cover the maximum size allowed by the
996 	 * extended VTOC format (2TB).
997 	 */
998 
999 	if (disk_size >= 8 * ONE_GIGABYTE) {
1000 
1001 		label->dkl_nhead = 96;
1002 		label->dkl_nsect = 768;
1003 
1004 	} else if (disk_size >= 2 * ONE_MEGABYTE) {
1005 
1006 		label->dkl_nhead = 1;
1007 		label->dkl_nsect = 600;
1008 
1009 	} else {
1010 
1011 		label->dkl_nhead = 1;
1012 		label->dkl_nsect = 200;
1013 	}
1014 
1015 	label->dkl_pcyl = disk_size /
1016 	    (label->dkl_nsect * label->dkl_nhead * bsize);
1017 
1018 	if (label->dkl_pcyl == 0)
1019 		label->dkl_pcyl = 1;
1020 
1021 	label->dkl_acyl = 0;
1022 
1023 	if (label->dkl_pcyl > 2)
1024 		label->dkl_acyl = 2;
1025 
1026 	label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
1027 	label->dkl_write_reinstruct = 0;
1028 	label->dkl_read_reinstruct = 0;
1029 	label->dkl_rpm = 7200;
1030 	label->dkl_apc = 0;
1031 	label->dkl_intrlv = 0;
1032 
1033 	PR0("requested disk size: %ld bytes\n", disk_size);
1034 	PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
1035 	    label->dkl_nhead, label->dkl_nsect);
1036 	PR0("provided disk size: %ld bytes\n", (uint64_t)
1037 	    (label->dkl_pcyl * label->dkl_nhead *
1038 	    label->dkl_nsect * bsize));
1039 
1040 	vd_get_readable_size(disk_size, &size, &unit);
1041 
1042 	/*
1043 	 * We must have a correct label name otherwise format(1m) will
1044 	 * not recognized the disk as labeled.
1045 	 */
1046 	(void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
1047 	    "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
1048 	    size, unit,
1049 	    label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
1050 	    label->dkl_nsect);
1051 
1052 	/* default VTOC */
1053 	label->dkl_vtoc.v_version = V_EXTVERSION;
1054 	label->dkl_vtoc.v_nparts = V_NUMPAR;
1055 	label->dkl_vtoc.v_sanity = VTOC_SANE;
1056 	label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
1057 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0;
1058 	label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl *
1059 	    label->dkl_nhead * label->dkl_nsect;
1060 	label->dkl_magic = DKL_MAGIC;
1061 	label->dkl_cksum = vd_lbl2cksum(label);
1062 }
1063 
1064 /*
1065  * Function:
1066  *	vd_dskimg_set_vtoc
1067  *
1068  * Description:
1069  *	Set the vtoc of a disk image by writing the label and backup
1070  *	labels into the disk image backend.
1071  *
1072  * Parameters:
1073  *	vd		- disk on which the operation is performed.
1074  *	label		- the data to be written.
1075  *
1076  * Return Code:
1077  *	0		- success.
1078  *	n > 0		- error, n indicates the errno code.
1079  */
1080 static int
vd_dskimg_set_vtoc(vd_t * vd,struct dk_label * label)1081 vd_dskimg_set_vtoc(vd_t *vd, struct dk_label *label)
1082 {
1083 	size_t blk, sec, cyl, head, cnt;
1084 
1085 	ASSERT(VD_DSKIMG(vd));
1086 
1087 	if (VD_DSKIMG_LABEL_WRITE(vd, label) < 0) {
1088 		PR0("fail to write disk label");
1089 		return (EIO);
1090 	}
1091 
1092 	/*
1093 	 * Backup labels are on the last alternate cylinder's
1094 	 * first five odd sectors.
1095 	 */
1096 	if (label->dkl_acyl == 0) {
1097 		PR0("no alternate cylinder, can not store backup labels");
1098 		return (0);
1099 	}
1100 
1101 	cyl = label->dkl_ncyl  + label->dkl_acyl - 1;
1102 	head = label->dkl_nhead - 1;
1103 
1104 	blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
1105 	    (head * label->dkl_nsect);
1106 
1107 	/*
1108 	 * Write the backup labels. Make sure we don't try to write past
1109 	 * the last cylinder.
1110 	 */
1111 	sec = 1;
1112 
1113 	for (cnt = 0; cnt < VD_DSKIMG_NUM_BACKUP; cnt++) {
1114 
1115 		if (sec >= label->dkl_nsect) {
1116 			PR0("not enough sector to store all backup labels");
1117 			return (0);
1118 		}
1119 
1120 		if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
1121 		    (caddr_t)label, blk + sec, sizeof (struct dk_label)) < 0) {
1122 			PR0("error writing backup label at block %lu\n",
1123 			    blk + sec);
1124 			return (EIO);
1125 		}
1126 
1127 		PR1("wrote backup label at block %lu\n", blk + sec);
1128 
1129 		sec += 2;
1130 	}
1131 
1132 	return (0);
1133 }
1134 
1135 /*
1136  * Function:
1137  *	vd_dskimg_get_devid_block
1138  *
1139  * Description:
1140  *	Return the block number where the device id is stored.
1141  *
1142  * Parameters:
1143  *	vd		- disk on which the operation is performed.
1144  *	blkp		- pointer to the block number
1145  *
1146  * Return Code:
1147  *	0		- success
1148  *	ENOSPC		- disk has no space to store a device id
1149  */
1150 static int
vd_dskimg_get_devid_block(vd_t * vd,size_t * blkp)1151 vd_dskimg_get_devid_block(vd_t *vd, size_t *blkp)
1152 {
1153 	diskaddr_t spc, head, cyl;
1154 
1155 	ASSERT(VD_DSKIMG(vd));
1156 
1157 	if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
1158 		/*
1159 		 * If no label is defined we don't know where to find
1160 		 * a device id.
1161 		 */
1162 		return (ENOSPC);
1163 	}
1164 
1165 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1166 		/*
1167 		 * For an EFI disk, the devid is at the beginning of
1168 		 * the reserved slice
1169 		 */
1170 		if (vd->efi_reserved == -1) {
1171 			PR0("EFI disk has no reserved slice");
1172 			return (ENOSPC);
1173 		}
1174 
1175 		*blkp = vd->slices[vd->efi_reserved].start;
1176 		return (0);
1177 	}
1178 
1179 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
1180 
1181 	/* this geometry doesn't allow us to have a devid */
1182 	if (vd->dk_geom.dkg_acyl < 2) {
1183 		PR0("not enough alternate cylinder available for devid "
1184 		    "(acyl=%u)", vd->dk_geom.dkg_acyl);
1185 		return (ENOSPC);
1186 	}
1187 
1188 	/* the devid is in on the track next to the last cylinder */
1189 	cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
1190 	spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
1191 	head = vd->dk_geom.dkg_nhead - 1;
1192 
1193 	*blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
1194 	    (head * vd->dk_geom.dkg_nsect) + 1;
1195 
1196 	return (0);
1197 }
1198 
1199 /*
1200  * Return the checksum of a disk block containing an on-disk devid.
1201  */
1202 static uint_t
vd_dkdevid2cksum(struct dk_devid * dkdevid)1203 vd_dkdevid2cksum(struct dk_devid *dkdevid)
1204 {
1205 	uint_t chksum, *ip;
1206 	int i;
1207 
1208 	chksum = 0;
1209 	ip = (void *)dkdevid;
1210 	for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
1211 		chksum ^= ip[i];
1212 
1213 	return (chksum);
1214 }
1215 
1216 /*
1217  * Function:
1218  *	vd_dskimg_read_devid
1219  *
1220  * Description:
1221  *	Read the device id stored on a disk image.
1222  *
1223  * Parameters:
1224  *	vd		- disk on which the operation is performed.
1225  *	devid		- the return address of the device ID.
1226  *
1227  * Return Code:
1228  *	0		- success
1229  *	EIO		- I/O error while trying to access the disk image
1230  *	EINVAL		- no valid device id was found
1231  *	ENOSPC		- disk has no space to store a device id
1232  */
1233 static int
vd_dskimg_read_devid(vd_t * vd,ddi_devid_t * devid)1234 vd_dskimg_read_devid(vd_t *vd, ddi_devid_t *devid)
1235 {
1236 	struct dk_devid *dkdevid;
1237 	size_t blk;
1238 	uint_t chksum;
1239 	int status, sz;
1240 
1241 	ASSERT(vd->vdisk_bsize == DEV_BSIZE);
1242 
1243 	if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0)
1244 		return (status);
1245 
1246 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
1247 
1248 	/* get the devid */
1249 	if ((vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
1250 	    DEV_BSIZE)) < 0) {
1251 		PR0("error reading devid block at %lu", blk);
1252 		status = EIO;
1253 		goto done;
1254 	}
1255 
1256 	/* validate the revision */
1257 	if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
1258 	    (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
1259 		PR0("invalid devid found at block %lu (bad revision)", blk);
1260 		status = EINVAL;
1261 		goto done;
1262 	}
1263 
1264 	/* compute checksum */
1265 	chksum = vd_dkdevid2cksum(dkdevid);
1266 
1267 	/* compare the checksums */
1268 	if (DKD_GETCHKSUM(dkdevid) != chksum) {
1269 		PR0("invalid devid found at block %lu (bad checksum)", blk);
1270 		status = EINVAL;
1271 		goto done;
1272 	}
1273 
1274 	/* validate the device id */
1275 	if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
1276 		PR0("invalid devid found at block %lu", blk);
1277 		status = EINVAL;
1278 		goto done;
1279 	}
1280 
1281 	PR1("devid read at block %lu", blk);
1282 
1283 	sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
1284 	*devid = kmem_alloc(sz, KM_SLEEP);
1285 	bcopy(&dkdevid->dkd_devid, *devid, sz);
1286 
1287 done:
1288 	kmem_free(dkdevid, DEV_BSIZE);
1289 	return (status);
1290 
1291 }
1292 
1293 /*
1294  * Function:
1295  *	vd_dskimg_write_devid
1296  *
1297  * Description:
1298  *	Write a device id into disk image.
1299  *
1300  * Parameters:
1301  *	vd		- disk on which the operation is performed.
1302  *	devid		- the device ID to store.
1303  *
1304  * Return Code:
1305  *	0		- success
1306  *	EIO		- I/O error while trying to access the disk image
1307  *	ENOSPC		- disk has no space to store a device id
1308  */
1309 static int
vd_dskimg_write_devid(vd_t * vd,ddi_devid_t devid)1310 vd_dskimg_write_devid(vd_t *vd, ddi_devid_t devid)
1311 {
1312 	struct dk_devid *dkdevid;
1313 	uint_t chksum;
1314 	size_t blk;
1315 	int status;
1316 
1317 	ASSERT(vd->vdisk_bsize == DEV_BSIZE);
1318 
1319 	if (devid == NULL) {
1320 		/* nothing to write */
1321 		return (0);
1322 	}
1323 
1324 	if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0)
1325 		return (status);
1326 
1327 	dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
1328 
1329 	/* set revision */
1330 	dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
1331 	dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
1332 
1333 	/* copy devid */
1334 	bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
1335 
1336 	/* compute checksum */
1337 	chksum = vd_dkdevid2cksum(dkdevid);
1338 
1339 	/* set checksum */
1340 	DKD_FORMCHKSUM(chksum, dkdevid);
1341 
1342 	/* store the devid */
1343 	if ((status = vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
1344 	    (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
1345 		PR0("Error writing devid block at %lu", blk);
1346 		status = EIO;
1347 	} else {
1348 		PR1("devid written at block %lu", blk);
1349 		status = 0;
1350 	}
1351 
1352 	kmem_free(dkdevid, DEV_BSIZE);
1353 	return (status);
1354 }
1355 
1356 /*
1357  * Function:
1358  *	vd_do_scsi_rdwr
1359  *
1360  * Description:
1361  *	Read or write to a SCSI disk using an absolute disk offset.
1362  *
1363  * Parameters:
1364  *	vd		- disk on which the operation is performed.
1365  *	operation	- operation to execute: read (VD_OP_BREAD) or
1366  *			  write (VD_OP_BWRITE).
1367  *	data		- buffer where data are read to or written from.
1368  *	blk		- starting block for the operation.
1369  *	len		- number of bytes to read or write.
1370  *
1371  * Return Code:
1372  *	0		- success
1373  *	n != 0		- error.
1374  */
1375 static int
vd_do_scsi_rdwr(vd_t * vd,int operation,caddr_t data,size_t blk,size_t len)1376 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
1377 {
1378 	struct uscsi_cmd ucmd;
1379 	union scsi_cdb cdb;
1380 	int nsectors, nblk;
1381 	int max_sectors;
1382 	int status, rval;
1383 
1384 	ASSERT(!vd->file);
1385 	ASSERT(!vd->volume);
1386 	ASSERT(vd->vdisk_bsize > 0);
1387 
1388 	max_sectors = vd->max_xfer_sz;
1389 	nblk = (len / vd->vdisk_bsize);
1390 
1391 	if (len % vd->vdisk_bsize != 0)
1392 		return (EINVAL);
1393 
1394 	/*
1395 	 * Build and execute the uscsi ioctl.  We build a group0, group1
1396 	 * or group4 command as necessary, since some targets
1397 	 * do not support group1 commands.
1398 	 */
1399 	while (nblk) {
1400 
1401 		bzero(&ucmd, sizeof (ucmd));
1402 		bzero(&cdb, sizeof (cdb));
1403 
1404 		nsectors = (max_sectors < nblk) ? max_sectors : nblk;
1405 
1406 		/*
1407 		 * Some of the optical drives on sun4v machines are ATAPI
1408 		 * devices which use Group 1 Read/Write commands so we need
1409 		 * to explicitly check a flag which is set when a domain
1410 		 * is bound.
1411 		 */
1412 		if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
1413 			FORMG0ADDR(&cdb, blk);
1414 			FORMG0COUNT(&cdb, (uchar_t)nsectors);
1415 			ucmd.uscsi_cdblen = CDB_GROUP0;
1416 		} else if (blk > 0xffffffff) {
1417 			FORMG4LONGADDR(&cdb, blk);
1418 			FORMG4COUNT(&cdb, nsectors);
1419 			ucmd.uscsi_cdblen = CDB_GROUP4;
1420 			cdb.scc_cmd |= SCMD_GROUP4;
1421 		} else {
1422 			FORMG1ADDR(&cdb, blk);
1423 			FORMG1COUNT(&cdb, nsectors);
1424 			ucmd.uscsi_cdblen = CDB_GROUP1;
1425 			cdb.scc_cmd |= SCMD_GROUP1;
1426 		}
1427 		ucmd.uscsi_cdb = (caddr_t)&cdb;
1428 		ucmd.uscsi_bufaddr = data;
1429 		ucmd.uscsi_buflen = nsectors * vd->backend_bsize;
1430 		ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
1431 		/*
1432 		 * Set flags so that the command is isolated from normal
1433 		 * commands and no error message is printed.
1434 		 */
1435 		ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
1436 
1437 		if (operation == VD_OP_BREAD) {
1438 			cdb.scc_cmd |= SCMD_READ;
1439 			ucmd.uscsi_flags |= USCSI_READ;
1440 		} else {
1441 			cdb.scc_cmd |= SCMD_WRITE;
1442 		}
1443 
1444 		status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
1445 		    USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
1446 		    kcred, &rval);
1447 
1448 		if (status == 0)
1449 			status = ucmd.uscsi_status;
1450 
1451 		if (status != 0)
1452 			break;
1453 
1454 		/*
1455 		 * Check if partial DMA breakup is required. If so, reduce
1456 		 * the request size by half and retry the last request.
1457 		 */
1458 		if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
1459 			max_sectors >>= 1;
1460 			if (max_sectors <= 0) {
1461 				status = EIO;
1462 				break;
1463 			}
1464 			continue;
1465 		}
1466 
1467 		if (ucmd.uscsi_resid != 0) {
1468 			status = EIO;
1469 			break;
1470 		}
1471 
1472 		blk += nsectors;
1473 		nblk -= nsectors;
1474 		data += nsectors * vd->vdisk_bsize;
1475 	}
1476 
1477 	return (status);
1478 }
1479 
1480 /*
1481  * Function:
1482  *	vd_scsi_rdwr
1483  *
1484  * Description:
1485  *	Wrapper function to read or write to a SCSI disk using an absolute
1486  *	disk offset. It checks the blocksize of the underlying device and,
1487  *	if necessary, adjusts the buffers accordingly before calling
1488  *	vd_do_scsi_rdwr() to do the actual read or write.
1489  *
1490  * Parameters:
1491  *	vd		- disk on which the operation is performed.
1492  *	operation	- operation to execute: read (VD_OP_BREAD) or
1493  *			  write (VD_OP_BWRITE).
1494  *	data		- buffer where data are read to or written from.
1495  *	blk		- starting block for the operation.
1496  *	len		- number of bytes to read or write.
1497  *
1498  * Return Code:
1499  *	0		- success
1500  *	n != 0		- error.
1501  */
1502 static int
vd_scsi_rdwr(vd_t * vd,int operation,caddr_t data,size_t vblk,size_t vlen)1503 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
1504 {
1505 	int	rv;
1506 
1507 	size_t	pblk;	/* physical device block number of data on device */
1508 	size_t	delta;	/* relative offset between pblk and vblk */
1509 	size_t	pnblk;	/* number of physical blocks to be read from device */
1510 	size_t	plen;	/* length of data to be read from physical device */
1511 	char	*buf;	/* buffer area to fit physical device's block size */
1512 
1513 	if (vd->backend_bsize == 0) {
1514 		/*
1515 		 * The block size was not available during the attach,
1516 		 * try to update it now.
1517 		 */
1518 		if (vd_backend_check_size(vd) != 0)
1519 			return (EIO);
1520 	}
1521 
1522 	/*
1523 	 * If the vdisk block size and the block size of the underlying device
1524 	 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
1525 	 * to create a buffer large enough to handle the device's block size
1526 	 * and adjust the block to be read from and the amount of data to
1527 	 * read to correspond with the device's block size.
1528 	 */
1529 	if (vd->vdisk_bsize == vd->backend_bsize)
1530 		return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
1531 
1532 	if (vd->vdisk_bsize > vd->backend_bsize)
1533 		return (EINVAL);
1534 
1535 	/*
1536 	 * Writing of physical block sizes larger than the virtual block size
1537 	 * is not supported. This would be added if/when support for guests
1538 	 * writing to DVDs is implemented.
1539 	 */
1540 	if (operation == VD_OP_BWRITE)
1541 		return (ENOTSUP);
1542 
1543 	/* BEGIN CSTYLED */
1544 	/*
1545 	 * Below is a diagram showing the relationship between the physical
1546 	 * and virtual blocks. If the virtual blocks marked by 'X' below are
1547 	 * requested, then the physical blocks denoted by 'Y' are read.
1548 	 *
1549 	 *           vblk
1550 	 *             |      vlen
1551 	 *             |<--------------->|
1552 	 *             v                 v
1553 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   virtual disk:
1554 	 *    |  |  |  |XX|XX|XX|XX|XX|XX|  |  |  |  |  |  } block size is
1555 	 *  --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+-   vd->vdisk_bsize
1556 	 *          :  :                 :  :
1557 	 *         >:==:< delta          :  :
1558 	 *          :  :                 :  :
1559 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   physical disk:
1560 	 *    |     |YY:YY|YYYYY|YYYYY|YY:YY|     |     |   } block size is
1561 	 *  --+-----+-----+-----+-----+-----+-----+-----+--   vd->backend_bsize
1562 	 *          ^                       ^
1563 	 *          |<--------------------->|
1564 	 *          |         plen
1565 	 *	   pblk
1566 	 */
1567 	/* END CSTYLED */
1568 	pblk = (vblk * vd->vdisk_bsize) / vd->backend_bsize;
1569 	delta = (vblk * vd->vdisk_bsize) - (pblk * vd->backend_bsize);
1570 	pnblk = ((delta + vlen - 1) / vd->backend_bsize) + 1;
1571 	plen = pnblk * vd->backend_bsize;
1572 
1573 	PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
1574 
1575 	buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
1576 	rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
1577 	bcopy(buf + delta, data, vlen);
1578 
1579 	kmem_free(buf, sizeof (caddr_t) * plen);
1580 
1581 	return (rv);
1582 }
1583 
1584 /*
1585  * Function:
1586  *	vd_slice_flabel_read
1587  *
1588  * Description:
1589  *	This function simulates a read operation from the fake label of
1590  *	a single-slice disk.
1591  *
1592  * Parameters:
1593  *	vd		- single-slice disk to read from
1594  *	data		- buffer where data should be read to
1595  *	offset		- offset in byte where the read should start
1596  *	length		- number of bytes to read
1597  *
1598  * Return Code:
1599  *	n >= 0		- success, n indicates the number of bytes read
1600  *	-1		- error
1601  */
1602 static ssize_t
vd_slice_flabel_read(vd_t * vd,caddr_t data,size_t offset,size_t length)1603 vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length)
1604 {
1605 	size_t n = 0;
1606 	uint_t limit = vd->flabel_limit * vd->vdisk_bsize;
1607 
1608 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1609 	ASSERT(vd->flabel != NULL);
1610 
1611 	/* if offset is past the fake label limit there's nothing to read */
1612 	if (offset >= limit)
1613 		return (0);
1614 
1615 	/* data with offset 0 to flabel_size are read from flabel */
1616 	if (offset < vd->flabel_size) {
1617 
1618 		if (offset + length <= vd->flabel_size) {
1619 			bcopy(vd->flabel + offset, data, length);
1620 			return (length);
1621 		}
1622 
1623 		n = vd->flabel_size - offset;
1624 		bcopy(vd->flabel + offset, data, n);
1625 		data += n;
1626 	}
1627 
1628 	/* data with offset from flabel_size to flabel_limit are all zeros */
1629 	if (offset + length <= limit) {
1630 		bzero(data, length - n);
1631 		return (length);
1632 	}
1633 
1634 	bzero(data, limit - offset - n);
1635 	return (limit - offset);
1636 }
1637 
1638 /*
1639  * Function:
1640  *	vd_slice_flabel_write
1641  *
1642  * Description:
1643  *	This function simulates a write operation to the fake label of
1644  *	a single-slice disk. Write operations are actually faked and return
1645  *	success although the label is never changed. This is mostly to
1646  *	simulate a successful label update.
1647  *
1648  * Parameters:
1649  *	vd		- single-slice disk to write to
1650  *	data		- buffer where data should be written from
1651  *	offset		- offset in byte where the write should start
1652  *	length		- number of bytes to written
1653  *
1654  * Return Code:
1655  *	n >= 0		- success, n indicates the number of bytes written
1656  *	-1		- error
1657  */
1658 static ssize_t
vd_slice_flabel_write(vd_t * vd,caddr_t data,size_t offset,size_t length)1659 vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length)
1660 {
1661 	uint_t limit = vd->flabel_limit * vd->vdisk_bsize;
1662 	struct dk_label *label;
1663 	struct dk_geom geom;
1664 	struct extvtoc vtoc;
1665 
1666 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1667 	ASSERT(vd->flabel != NULL);
1668 
1669 	if (offset >= limit)
1670 		return (0);
1671 
1672 	/*
1673 	 * If this is a request to overwrite the VTOC disk label, check that
1674 	 * the new label is similar to the previous one and return that the
1675 	 * write was successful, but note that nothing is actually overwritten.
1676 	 */
1677 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1678 	    offset == 0 && length == vd->vdisk_bsize) {
1679 		label = (void *)data;
1680 
1681 		/* check that this is a valid label */
1682 		if (label->dkl_magic != DKL_MAGIC ||
1683 		    label->dkl_cksum != vd_lbl2cksum(label))
1684 			return (-1);
1685 
1686 		/* check the vtoc and geometry */
1687 		vd_label_to_vtocgeom(label, &vtoc, &geom);
1688 		if (vd_slice_geom_isvalid(vd, &geom) &&
1689 		    vd_slice_vtoc_isvalid(vd, &vtoc))
1690 			return (length);
1691 	}
1692 
1693 	/* fail any other write */
1694 	return (-1);
1695 }
1696 
1697 /*
1698  * Function:
1699  *	vd_slice_fake_rdwr
1700  *
1701  * Description:
1702  *	This function simulates a raw read or write operation to a single-slice
1703  *	disk. It only handles the faked part of the operation i.e. I/Os to
1704  *	blocks which have no mapping with the vdisk backend (I/Os to the
1705  *	beginning and to the end of the vdisk).
1706  *
1707  *	The function returns 0 is the operation	is completed and it has been
1708  *	entirely handled as a fake read or write. In that case, lengthp points
1709  *	to the number of bytes not read or written. Values returned by datap
1710  *	and blkp are undefined.
1711  *
1712  *	If the fake operation has succeeded but the read or write is not
1713  *	complete (i.e. the read/write operation extends beyond the blocks
1714  *	we fake) then the function returns EAGAIN and datap, blkp and lengthp
1715  *	pointers points to the parameters for completing the operation.
1716  *
1717  *	In case of an error, for example if the slice is empty or parameters
1718  *	are invalid, then the function returns a non-zero value different
1719  *	from EAGAIN. In that case, the returned values of datap, blkp and
1720  *	lengthp are undefined.
1721  *
1722  * Parameters:
1723  *	vd		- single-slice disk on which the operation is performed
1724  *	slice		- slice on which the operation is performed,
1725  *			  VD_SLICE_NONE indicates that the operation
1726  *			  is done using an absolute disk offset.
1727  *	operation	- operation to execute: read (VD_OP_BREAD) or
1728  *			  write (VD_OP_BWRITE).
1729  *	datap		- pointer to the buffer where data are read to
1730  *			  or written from. Return the pointer where remaining
1731  *			  data have to be read to or written from.
1732  *	blkp		- pointer to the starting block for the operation.
1733  *			  Return the starting block relative to the vdisk
1734  *			  backend for the remaining operation.
1735  *	lengthp		- pointer to the number of bytes to read or write.
1736  *			  This should be a multiple of vdisk_bsize. Return the
1737  *			  remaining number of bytes to read or write.
1738  *
1739  * Return Code:
1740  *	0		- read/write operation is completed
1741  *	EAGAIN		- read/write operation is not completed
1742  *	other values	- error
1743  */
1744 static int
vd_slice_fake_rdwr(vd_t * vd,int slice,int operation,caddr_t * datap,size_t * blkp,size_t * lengthp)1745 vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap,
1746     size_t *blkp, size_t *lengthp)
1747 {
1748 	struct dk_label *label;
1749 	caddr_t data;
1750 	size_t blk, length, csize;
1751 	size_t ablk, asize, aoff, alen;
1752 	ssize_t n;
1753 	int sec, status;
1754 	size_t bsize = vd->vdisk_bsize;
1755 
1756 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
1757 	ASSERT(slice != 0);
1758 
1759 	data = *datap;
1760 	blk = *blkp;
1761 	length = *lengthp;
1762 
1763 	/*
1764 	 * If this is not a raw I/O or an I/O from a full disk slice then
1765 	 * this is an I/O to/from an empty slice.
1766 	 */
1767 	if (slice != VD_SLICE_NONE &&
1768 	    (slice != VD_ENTIRE_DISK_SLICE ||
1769 	    vd->vdisk_label != VD_DISK_LABEL_VTOC) &&
1770 	    (slice != VD_EFI_WD_SLICE ||
1771 	    vd->vdisk_label != VD_DISK_LABEL_EFI)) {
1772 		return (EIO);
1773 	}
1774 
1775 	if (length % bsize != 0)
1776 		return (EINVAL);
1777 
1778 	/* handle any I/O with the fake label */
1779 	if (operation == VD_OP_BWRITE)
1780 		n = vd_slice_flabel_write(vd, data, blk * bsize, length);
1781 	else
1782 		n = vd_slice_flabel_read(vd, data, blk * bsize, length);
1783 
1784 	if (n == -1)
1785 		return (EINVAL);
1786 
1787 	ASSERT(n % bsize == 0);
1788 
1789 	/* adjust I/O arguments */
1790 	data += n;
1791 	blk += n / bsize;
1792 	length -= n;
1793 
1794 	/* check if there's something else to process */
1795 	if (length == 0) {
1796 		status = 0;
1797 		goto done;
1798 	}
1799 
1800 	if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
1801 	    slice == VD_ENTIRE_DISK_SLICE) {
1802 		status = EAGAIN;
1803 		goto done;
1804 	}
1805 
1806 	if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
1807 		asize = EFI_MIN_RESV_SIZE + (EFI_MIN_ARRAY_SIZE / bsize) + 1;
1808 		ablk = vd->vdisk_size - asize;
1809 	} else {
1810 		ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
1811 		ASSERT(vd->dk_geom.dkg_apc == 0);
1812 
1813 		csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
1814 		ablk = vd->dk_geom.dkg_ncyl * csize;
1815 		asize = vd->dk_geom.dkg_acyl * csize;
1816 	}
1817 
1818 	alen = length / bsize;
1819 	aoff = blk;
1820 
1821 	/* if we have reached the last block then the I/O is completed */
1822 	if (aoff == ablk + asize) {
1823 		status = 0;
1824 		goto done;
1825 	}
1826 
1827 	/* if we are past the last block then return an error */
1828 	if (aoff > ablk + asize)
1829 		return (EIO);
1830 
1831 	/* check if there is any I/O to end of the disk */
1832 	if (aoff + alen < ablk) {
1833 		status = EAGAIN;
1834 		goto done;
1835 	}
1836 
1837 	/* we don't allow any write to the end of the disk */
1838 	if (operation == VD_OP_BWRITE)
1839 		return (EIO);
1840 
1841 	if (aoff < ablk) {
1842 		alen -= (ablk - aoff);
1843 		aoff = ablk;
1844 	}
1845 
1846 	if (aoff + alen > ablk + asize) {
1847 		alen = ablk + asize - aoff;
1848 	}
1849 
1850 	alen *= bsize;
1851 
1852 	if (operation == VD_OP_BREAD) {
1853 		bzero(data + (aoff - blk) * bsize, alen);
1854 
1855 		if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
1856 			/* check if we read backup labels */
1857 			label = VD_LABEL_VTOC(vd);
1858 			ablk += (label->dkl_acyl - 1) * csize +
1859 			    (label->dkl_nhead - 1) * label->dkl_nsect;
1860 
1861 			for (sec = 1; (sec < 5 * 2 + 1); sec += 2) {
1862 
1863 				if (ablk + sec >= blk &&
1864 				    ablk + sec < blk + (length / bsize)) {
1865 					bcopy(label, data +
1866 					    (ablk + sec - blk) * bsize,
1867 					    sizeof (struct dk_label));
1868 				}
1869 			}
1870 		}
1871 	}
1872 
1873 	length -= alen;
1874 
1875 	status = (length == 0)? 0: EAGAIN;
1876 
1877 done:
1878 	ASSERT(length == 0 || blk >= vd->flabel_limit);
1879 
1880 	/*
1881 	 * Return the parameters for the remaining I/O. The starting block is
1882 	 * adjusted so that it is relative to the vdisk backend.
1883 	 */
1884 	*datap = data;
1885 	*blkp = blk - vd->flabel_limit;
1886 	*lengthp = length;
1887 
1888 	return (status);
1889 }
1890 
1891 static int
vd_flush_write(vd_t * vd)1892 vd_flush_write(vd_t *vd)
1893 {
1894 	int status, rval;
1895 
1896 	if (vd->file) {
1897 		status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
1898 	} else {
1899 		status = ldi_ioctl(vd->ldi_handle[0], DKIOCFLUSHWRITECACHE,
1900 		    (intptr_t)NULL, vd->open_flags | FKIOCTL, kcred, &rval);
1901 	}
1902 
1903 	return (status);
1904 }
1905 
1906 static void
vd_bio_task(void * arg)1907 vd_bio_task(void *arg)
1908 {
1909 	struct buf *buf = (struct buf *)arg;
1910 	vd_task_t *task = (vd_task_t *)buf->b_private;
1911 	vd_t *vd = task->vd;
1912 	ssize_t resid;
1913 	int status;
1914 
1915 	ASSERT(vd->vdisk_bsize == DEV_BSIZE);
1916 
1917 	if (vd->zvol) {
1918 
1919 		status = ldi_strategy(vd->ldi_handle[0], buf);
1920 
1921 	} else {
1922 
1923 		ASSERT(vd->file);
1924 
1925 		status = vn_rdwr((buf->b_flags & B_READ)? UIO_READ : UIO_WRITE,
1926 		    vd->file_vnode, buf->b_un.b_addr, buf->b_bcount,
1927 		    buf->b_lblkno * DEV_BSIZE, UIO_SYSSPACE, 0,
1928 		    RLIM64_INFINITY, kcred, &resid);
1929 
1930 		if (status == 0) {
1931 			buf->b_resid = resid;
1932 			biodone(buf);
1933 			return;
1934 		}
1935 	}
1936 
1937 	if (status != 0) {
1938 		bioerror(buf, status);
1939 		biodone(buf);
1940 	}
1941 }
1942 
1943 /*
1944  * We define our own biodone function so that buffers used for
1945  * asynchronous writes are not released when biodone() is called.
1946  */
1947 static int
vd_biodone(struct buf * bp)1948 vd_biodone(struct buf *bp)
1949 {
1950 	ASSERT((bp->b_flags & B_DONE) == 0);
1951 	ASSERT(SEMA_HELD(&bp->b_sem));
1952 
1953 	bp->b_flags |= B_DONE;
1954 	sema_v(&bp->b_io);
1955 
1956 	return (0);
1957 }
1958 
1959 /*
1960  * Return Values
1961  *	EINPROGRESS	- operation was successfully started
1962  *	EIO		- encountered LDC (aka. task error)
1963  *	0		- operation completed successfully
1964  *
1965  * Side Effect
1966  *     sets request->status = <disk operation status>
1967  */
1968 static int
vd_start_bio(vd_task_t * task)1969 vd_start_bio(vd_task_t *task)
1970 {
1971 	int			rv, status = 0;
1972 	vd_t			*vd		= task->vd;
1973 	vd_dring_payload_t	*request	= task->request;
1974 	struct buf		*buf		= &task->buf;
1975 	uint8_t			mtype;
1976 	int			slice;
1977 	char			*bufaddr = 0;
1978 	size_t			buflen;
1979 	size_t			offset, length, nbytes;
1980 
1981 	ASSERT(vd != NULL);
1982 	ASSERT(request != NULL);
1983 
1984 	slice = request->slice;
1985 
1986 	ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
1987 	ASSERT((request->operation == VD_OP_BREAD) ||
1988 	    (request->operation == VD_OP_BWRITE));
1989 
1990 	if (request->nbytes == 0) {
1991 		/* no service for trivial requests */
1992 		request->status = EINVAL;
1993 		return (0);
1994 	}
1995 
1996 	PR1("%s %lu bytes at block %lu",
1997 	    (request->operation == VD_OP_BREAD) ? "Read" : "Write",
1998 	    request->nbytes, request->addr);
1999 
2000 	/*
2001 	 * We have to check the open flags because the functions processing
2002 	 * the read/write request will not do it.
2003 	 */
2004 	if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
2005 		PR0("write fails because backend is opened read-only");
2006 		request->nbytes = 0;
2007 		request->status = EROFS;
2008 		return (0);
2009 	}
2010 
2011 	mtype = LDC_SHADOW_MAP;
2012 
2013 	/* Map memory exported by client */
2014 	status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
2015 	    mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
2016 	    &bufaddr, NULL);
2017 	if (status != 0) {
2018 		PR0("ldc_mem_map() returned err %d ", status);
2019 		return (EIO);
2020 	}
2021 
2022 	/*
2023 	 * The buffer size has to be 8-byte aligned, so the client should have
2024 	 * sent a buffer which size is roundup to the next 8-byte aligned value.
2025 	 */
2026 	buflen = P2ROUNDUP(request->nbytes, 8);
2027 
2028 	status = ldc_mem_acquire(task->mhdl, 0, buflen);
2029 	if (status != 0) {
2030 		(void) ldc_mem_unmap(task->mhdl);
2031 		PR0("ldc_mem_acquire() returned err %d ", status);
2032 		return (EIO);
2033 	}
2034 
2035 	offset = request->addr;
2036 	nbytes = request->nbytes;
2037 	length = nbytes;
2038 
2039 	/* default number of byte returned by the I/O */
2040 	request->nbytes = 0;
2041 
2042 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
2043 
2044 		if (slice != 0) {
2045 			/* handle any fake I/O */
2046 			rv = vd_slice_fake_rdwr(vd, slice, request->operation,
2047 			    &bufaddr, &offset, &length);
2048 
2049 			/* record the number of bytes from the fake I/O */
2050 			request->nbytes = nbytes - length;
2051 
2052 			if (rv == 0) {
2053 				request->status = 0;
2054 				goto io_done;
2055 			}
2056 
2057 			if (rv != EAGAIN) {
2058 				request->nbytes = 0;
2059 				request->status = EIO;
2060 				goto io_done;
2061 			}
2062 
2063 			/*
2064 			 * If we return with EAGAIN then this means that there
2065 			 * are still data to read or write.
2066 			 */
2067 			ASSERT(length != 0);
2068 
2069 			/*
2070 			 * We need to continue the I/O from the slice backend to
2071 			 * complete the request. The variables bufaddr, offset
2072 			 * and length have been adjusted to have the right
2073 			 * information to do the remaining I/O from the backend.
2074 			 * The backend is entirely mapped to slice 0 so we just
2075 			 * have to complete the I/O from that slice.
2076 			 */
2077 			slice = 0;
2078 		}
2079 
2080 	} else if (vd->volume || vd->file) {
2081 
2082 		rv = vd_dskimg_io_params(vd, slice, &offset, &length);
2083 		if (rv != 0) {
2084 			request->status = (rv == ENODATA)? 0: EIO;
2085 			goto io_done;
2086 		}
2087 		slice = 0;
2088 
2089 	} else if (slice == VD_SLICE_NONE) {
2090 
2091 		/*
2092 		 * This is not a disk image so it is a real disk. We
2093 		 * assume that the underlying device driver supports
2094 		 * USCSICMD ioctls. This is the case of all SCSI devices
2095 		 * (sd, ssd...).
2096 		 *
2097 		 * In the future if we have non-SCSI disks we would need
2098 		 * to invoke the appropriate function to do I/O using an
2099 		 * absolute disk offset (for example using DIOCTL_RWCMD
2100 		 * for IDE disks).
2101 		 */
2102 		rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset,
2103 		    length);
2104 		if (rv != 0) {
2105 			request->status = EIO;
2106 		} else {
2107 			request->nbytes = length;
2108 			request->status = 0;
2109 		}
2110 		goto io_done;
2111 	}
2112 
2113 	/* Start the block I/O */
2114 	bioinit(buf);
2115 	buf->b_flags	= B_BUSY;
2116 	buf->b_bcount	= length;
2117 	buf->b_lblkno	= offset;
2118 	buf->b_bufsize	= buflen;
2119 	buf->b_edev	= vd->dev[slice];
2120 	buf->b_un.b_addr = bufaddr;
2121 	buf->b_iodone	= vd_biodone;
2122 
2123 	if (vd->file || vd->zvol) {
2124 		/*
2125 		 * I/O to a file are dispatched to an I/O queue, so that several
2126 		 * I/Os can be processed in parallel. We also do that for ZFS
2127 		 * volumes because the ZFS volume strategy() function will only
2128 		 * return after the I/O is completed (instead of just starting
2129 		 * the I/O).
2130 		 */
2131 
2132 		if (request->operation == VD_OP_BREAD) {
2133 			buf->b_flags |= B_READ;
2134 		} else {
2135 			/*
2136 			 * For ZFS volumes and files, we do an asynchronous
2137 			 * write and we will wait for the completion of the
2138 			 * write in vd_complete_bio() by flushing the volume
2139 			 * or file.
2140 			 *
2141 			 * This done for performance reasons, so that we can
2142 			 * group together several write requests into a single
2143 			 * flush operation.
2144 			 */
2145 			buf->b_flags |= B_WRITE | B_ASYNC;
2146 
2147 			/*
2148 			 * We keep track of the write so that we can group
2149 			 * requests when flushing. The write queue has the
2150 			 * same number of slots as the dring so this prevents
2151 			 * the write queue from wrapping and overwriting
2152 			 * existing entries: if the write queue gets full
2153 			 * then that means that the dring is full so we stop
2154 			 * receiving new requests until an existing request
2155 			 * is processed, removed from the write queue and
2156 			 * then from the dring.
2157 			 */
2158 			task->write_index = vd->write_index;
2159 			vd->write_queue[task->write_index] = buf;
2160 			vd->write_index =
2161 			    VD_WRITE_INDEX_NEXT(vd, vd->write_index);
2162 		}
2163 
2164 		buf->b_private = task;
2165 
2166 		ASSERT(vd->ioq != NULL);
2167 
2168 		request->status = 0;
2169 		(void) ddi_taskq_dispatch(task->vd->ioq, vd_bio_task, buf,
2170 		    DDI_SLEEP);
2171 
2172 	} else {
2173 
2174 		if (request->operation == VD_OP_BREAD) {
2175 			buf->b_flags |= B_READ;
2176 		} else {
2177 			buf->b_flags |= B_WRITE;
2178 		}
2179 
2180 		/* convert VIO block number to buf block number */
2181 		buf->b_lblkno = offset << vd->vio_bshift;
2182 
2183 		request->status = ldi_strategy(vd->ldi_handle[slice], buf);
2184 	}
2185 
2186 	/*
2187 	 * This is to indicate to the caller that the request
2188 	 * needs to be finished by vd_complete_bio() by calling
2189 	 * biowait() there and waiting for that to return before
2190 	 * triggering the notification of the vDisk client.
2191 	 *
2192 	 * This is necessary when writing to real disks as
2193 	 * otherwise calls to ldi_strategy() would be serialized
2194 	 * behind the calls to biowait() and performance would
2195 	 * suffer.
2196 	 */
2197 	if (request->status == 0)
2198 		return (EINPROGRESS);
2199 
2200 	biofini(buf);
2201 
2202 io_done:
2203 	/* Clean up after error or completion */
2204 	rv = ldc_mem_release(task->mhdl, 0, buflen);
2205 	if (rv) {
2206 		PR0("ldc_mem_release() returned err %d ", rv);
2207 		status = EIO;
2208 	}
2209 	rv = ldc_mem_unmap(task->mhdl);
2210 	if (rv) {
2211 		PR0("ldc_mem_unmap() returned err %d ", rv);
2212 		status = EIO;
2213 	}
2214 
2215 	return (status);
2216 }
2217 
2218 /*
2219  * This function should only be called from vd_notify to ensure that requests
2220  * are responded to in the order that they are received.
2221  */
2222 static int
send_msg(ldc_handle_t ldc_handle,void * msg,size_t msglen)2223 send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
2224 {
2225 	int	status;
2226 	size_t	nbytes;
2227 
2228 	do {
2229 		nbytes = msglen;
2230 		status = ldc_write(ldc_handle, msg, &nbytes);
2231 		if (status != EWOULDBLOCK)
2232 			break;
2233 		drv_usecwait(vds_ldc_delay);
2234 	} while (status == EWOULDBLOCK);
2235 
2236 	if (status != 0) {
2237 		if (status != ECONNRESET)
2238 			PR0("ldc_write() returned errno %d", status);
2239 		return (status);
2240 	} else if (nbytes != msglen) {
2241 		PR0("ldc_write() performed only partial write");
2242 		return (EIO);
2243 	}
2244 
2245 	PR1("SENT %lu bytes", msglen);
2246 	return (0);
2247 }
2248 
2249 static void
vd_need_reset(vd_t * vd,boolean_t reset_ldc)2250 vd_need_reset(vd_t *vd, boolean_t reset_ldc)
2251 {
2252 	mutex_enter(&vd->lock);
2253 	vd->reset_state	= B_TRUE;
2254 	vd->reset_ldc	= reset_ldc;
2255 	mutex_exit(&vd->lock);
2256 }
2257 
2258 /*
2259  * Reset the state of the connection with a client, if needed; reset the LDC
2260  * transport as well, if needed.  This function should only be called from the
2261  * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
2262  */
2263 static void
vd_reset_if_needed(vd_t * vd)2264 vd_reset_if_needed(vd_t *vd)
2265 {
2266 	int	status = 0;
2267 
2268 	mutex_enter(&vd->lock);
2269 	if (!vd->reset_state) {
2270 		ASSERT(!vd->reset_ldc);
2271 		mutex_exit(&vd->lock);
2272 		return;
2273 	}
2274 	mutex_exit(&vd->lock);
2275 
2276 	PR0("Resetting connection state with %s", VD_CLIENT(vd));
2277 
2278 	/*
2279 	 * Let any asynchronous I/O complete before possibly pulling the rug
2280 	 * out from under it; defer checking vd->reset_ldc, as one of the
2281 	 * asynchronous tasks might set it
2282 	 */
2283 	if (vd->ioq != NULL)
2284 		ddi_taskq_wait(vd->ioq);
2285 	ddi_taskq_wait(vd->completionq);
2286 
2287 	status = vd_flush_write(vd);
2288 	if (status) {
2289 		PR0("flushwrite returned error %d", status);
2290 	}
2291 
2292 	if ((vd->initialized & VD_DRING) &&
2293 	    ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
2294 		PR0("ldc_mem_dring_unmap() returned errno %d", status);
2295 
2296 	vd_free_dring_task(vd);
2297 
2298 	/* Free the staging buffer for msgs */
2299 	if (vd->vio_msgp != NULL) {
2300 		kmem_free(vd->vio_msgp, vd->max_msglen);
2301 		vd->vio_msgp = NULL;
2302 	}
2303 
2304 	/* Free the inband message buffer */
2305 	if (vd->inband_task.msg != NULL) {
2306 		kmem_free(vd->inband_task.msg, vd->max_msglen);
2307 		vd->inband_task.msg = NULL;
2308 	}
2309 
2310 	mutex_enter(&vd->lock);
2311 
2312 	if (vd->reset_ldc)
2313 		PR0("taking down LDC channel");
2314 	if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
2315 		PR0("ldc_down() returned errno %d", status);
2316 
2317 	/* Reset exclusive access rights */
2318 	vd_reset_access(vd);
2319 
2320 	vd->initialized	&= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
2321 	vd->state	= VD_STATE_INIT;
2322 	vd->max_msglen	= sizeof (vio_msg_t);	/* baseline vio message size */
2323 
2324 	/* Allocate the staging buffer */
2325 	vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
2326 
2327 	PR0("calling ldc_up\n");
2328 	(void) ldc_up(vd->ldc_handle);
2329 
2330 	vd->reset_state	= B_FALSE;
2331 	vd->reset_ldc	= B_FALSE;
2332 
2333 	mutex_exit(&vd->lock);
2334 }
2335 
2336 static void vd_recv_msg(void *arg);
2337 
2338 static void
vd_mark_in_reset(vd_t * vd)2339 vd_mark_in_reset(vd_t *vd)
2340 {
2341 	int status;
2342 
2343 	PR0("vd_mark_in_reset: marking vd in reset\n");
2344 
2345 	vd_need_reset(vd, B_FALSE);
2346 	status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
2347 	if (status == DDI_FAILURE) {
2348 		PR0("cannot schedule task to recv msg\n");
2349 		vd_need_reset(vd, B_TRUE);
2350 		return;
2351 	}
2352 }
2353 
2354 static int
vd_mark_elem_done(vd_t * vd,int idx,int elem_status,int elem_nbytes)2355 vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
2356 {
2357 	boolean_t		accepted;
2358 	int			status;
2359 	on_trap_data_t		otd;
2360 	vd_dring_entry_t	*elem = VD_DRING_ELEM(idx);
2361 
2362 	if (vd->reset_state)
2363 		return (0);
2364 
2365 	/* Acquire the element */
2366 	if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
2367 	    vd->dring_handle, idx, idx)) != 0) {
2368 		if (status == ECONNRESET) {
2369 			vd_mark_in_reset(vd);
2370 			return (0);
2371 		} else {
2372 			return (status);
2373 		}
2374 	}
2375 
2376 	/* Set the element's status and mark it done */
2377 	accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
2378 	if (accepted) {
2379 		elem->payload.nbytes	= elem_nbytes;
2380 		elem->payload.status	= elem_status;
2381 		elem->hdr.dstate	= VIO_DESC_DONE;
2382 	} else {
2383 		/* Perhaps client timed out waiting for I/O... */
2384 		PR0("element %u no longer \"accepted\"", idx);
2385 		VD_DUMP_DRING_ELEM(elem);
2386 	}
2387 	/* Release the element */
2388 	if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
2389 	    vd->dring_handle, idx, idx)) != 0) {
2390 		if (status == ECONNRESET) {
2391 			vd_mark_in_reset(vd);
2392 			return (0);
2393 		} else {
2394 			PR0("VIO_DRING_RELEASE() returned errno %d",
2395 			    status);
2396 			return (status);
2397 		}
2398 	}
2399 
2400 	return (accepted ? 0 : EINVAL);
2401 }
2402 
2403 /*
2404  * Return Values
2405  *	0	- operation completed successfully
2406  *	EIO	- encountered LDC / task error
2407  *
2408  * Side Effect
2409  *	sets request->status = <disk operation status>
2410  */
2411 static int
vd_complete_bio(vd_task_t * task)2412 vd_complete_bio(vd_task_t *task)
2413 {
2414 	int			status		= 0;
2415 	int			rv		= 0;
2416 	vd_t			*vd		= task->vd;
2417 	vd_dring_payload_t	*request	= task->request;
2418 	struct buf		*buf		= &task->buf;
2419 	int			wid, nwrites;
2420 
2421 
2422 	ASSERT(vd != NULL);
2423 	ASSERT(request != NULL);
2424 	ASSERT(task->msg != NULL);
2425 	ASSERT(task->msglen >= sizeof (*task->msg));
2426 
2427 	if (buf->b_flags & B_DONE) {
2428 		/*
2429 		 * If the I/O is already done then we don't call biowait()
2430 		 * because biowait() might already have been called when
2431 		 * flushing a previous asynchronous write. So we just
2432 		 * retrieve the status of the request.
2433 		 */
2434 		request->status = geterror(buf);
2435 	} else {
2436 		/*
2437 		 * Wait for the I/O. For synchronous I/O, biowait() will return
2438 		 * when the I/O has completed. For asynchronous write, it will
2439 		 * return the write has been submitted to the backend, but it
2440 		 * may not have been committed.
2441 		 */
2442 		request->status = biowait(buf);
2443 	}
2444 
2445 	if (buf->b_flags & B_ASYNC) {
2446 		/*
2447 		 * Asynchronous writes are used when writing to a file or a
2448 		 * ZFS volume. In that case the bio notification indicates
2449 		 * that the write has started. We have to flush the backend
2450 		 * to ensure that the write has been committed before marking
2451 		 * the request as completed.
2452 		 */
2453 		ASSERT(task->request->operation == VD_OP_BWRITE);
2454 
2455 		wid = task->write_index;
2456 
2457 		/* check if write has been already flushed */
2458 		if (vd->write_queue[wid] != NULL) {
2459 
2460 			vd->write_queue[wid] = NULL;
2461 			wid = VD_WRITE_INDEX_NEXT(vd, wid);
2462 
2463 			/*
2464 			 * Because flushing is time consuming, it is worth
2465 			 * waiting for any other writes so that they can be
2466 			 * included in this single flush request.
2467 			 */
2468 			if (vd_awflush & VD_AWFLUSH_GROUP) {
2469 				nwrites = 1;
2470 				while (vd->write_queue[wid] != NULL) {
2471 					(void) biowait(vd->write_queue[wid]);
2472 					vd->write_queue[wid] = NULL;
2473 					wid = VD_WRITE_INDEX_NEXT(vd, wid);
2474 					nwrites++;
2475 				}
2476 				DTRACE_PROBE2(flushgrp, vd_task_t *, task,
2477 				    int, nwrites);
2478 			}
2479 
2480 			if (vd_awflush & VD_AWFLUSH_IMMEDIATE) {
2481 				request->status = vd_flush_write(vd);
2482 			} else if (vd_awflush & VD_AWFLUSH_DEFER) {
2483 				(void) taskq_dispatch(system_taskq,
2484 				    (void (*)(void *))vd_flush_write, vd,
2485 				    DDI_SLEEP);
2486 				request->status = 0;
2487 			}
2488 		}
2489 	}
2490 
2491 	/* Update the number of bytes read/written */
2492 	request->nbytes += buf->b_bcount - buf->b_resid;
2493 
2494 	/* Release the buffer */
2495 	if (!vd->reset_state)
2496 		status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize);
2497 	if (status) {
2498 		PR0("ldc_mem_release() returned errno %d copying to "
2499 		    "client", status);
2500 		if (status == ECONNRESET) {
2501 			vd_mark_in_reset(vd);
2502 		}
2503 		rv = EIO;
2504 	}
2505 
2506 	/* Unmap the memory, even if in reset */
2507 	status = ldc_mem_unmap(task->mhdl);
2508 	if (status) {
2509 		PR0("ldc_mem_unmap() returned errno %d copying to client",
2510 		    status);
2511 		if (status == ECONNRESET) {
2512 			vd_mark_in_reset(vd);
2513 		}
2514 		rv = EIO;
2515 	}
2516 
2517 	biofini(buf);
2518 
2519 	return (rv);
2520 }
2521 
2522 /*
2523  * Description:
2524  *	This function is called by the two functions called by a taskq
2525  *	[ vd_complete_notify() and vd_serial_notify()) ] to send the
2526  *	message to the client.
2527  *
2528  * Parameters:
2529  *	arg	- opaque pointer to structure containing task to be completed
2530  *
2531  * Return Values
2532  *	None
2533  */
2534 static void
vd_notify(vd_task_t * task)2535 vd_notify(vd_task_t *task)
2536 {
2537 	int	status;
2538 
2539 	ASSERT(task != NULL);
2540 	ASSERT(task->vd != NULL);
2541 
2542 	/*
2543 	 * Send the "ack" or "nack" back to the client; if sending the message
2544 	 * via LDC fails, arrange to reset both the connection state and LDC
2545 	 * itself
2546 	 */
2547 	PR2("Sending %s",
2548 	    (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
2549 
2550 	status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
2551 	switch (status) {
2552 	case 0:
2553 		break;
2554 	case ECONNRESET:
2555 		vd_mark_in_reset(task->vd);
2556 		break;
2557 	default:
2558 		PR0("initiating full reset");
2559 		vd_need_reset(task->vd, B_TRUE);
2560 		break;
2561 	}
2562 
2563 	DTRACE_PROBE1(task__end, vd_task_t *, task);
2564 }
2565 
2566 /*
2567  * Description:
2568  *	Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
2569  *	the vDisk client
2570  *
2571  * Parameters:
2572  *	task		- structure containing the request sent from client
2573  *
2574  * Return Values
2575  *	None
2576  */
2577 static void
vd_complete_notify(vd_task_t * task)2578 vd_complete_notify(vd_task_t *task)
2579 {
2580 	int			status		= 0;
2581 	vd_t			*vd		= task->vd;
2582 	vd_dring_payload_t	*request	= task->request;
2583 
2584 	/* Update the dring element for a dring client */
2585 	if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
2586 		status = vd_mark_elem_done(vd, task->index,
2587 		    request->status, request->nbytes);
2588 		if (status == ECONNRESET)
2589 			vd_mark_in_reset(vd);
2590 		else if (status == EACCES)
2591 			vd_need_reset(vd, B_TRUE);
2592 	}
2593 
2594 	/*
2595 	 * If a transport error occurred while marking the element done or
2596 	 * previously while executing the task, arrange to "nack" the message
2597 	 * when the final task in the descriptor element range completes
2598 	 */
2599 	if ((status != 0) || (task->status != 0))
2600 		task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
2601 
2602 	/*
2603 	 * Only the final task for a range of elements will respond to and
2604 	 * free the message
2605 	 */
2606 	if (task->type == VD_NONFINAL_RANGE_TASK) {
2607 		return;
2608 	}
2609 
2610 	/*
2611 	 * We should only send an ACK/NACK here if we are not currently in
2612 	 * reset as, depending on how we reset, the dring may have been
2613 	 * blown away and we don't want to ACK/NACK a message that isn't
2614 	 * there.
2615 	 */
2616 	if (!vd->reset_state)
2617 		vd_notify(task);
2618 }
2619 
2620 /*
2621  * Description:
2622  *	This is the basic completion function called to handle inband data
2623  *	requests and handshake messages. All it needs to do is trigger a
2624  *	message to the client that the request is completed.
2625  *
2626  * Parameters:
2627  *	arg	- opaque pointer to structure containing task to be completed
2628  *
2629  * Return Values
2630  *	None
2631  */
2632 static void
vd_serial_notify(void * arg)2633 vd_serial_notify(void *arg)
2634 {
2635 	vd_task_t		*task = (vd_task_t *)arg;
2636 
2637 	ASSERT(task != NULL);
2638 	vd_notify(task);
2639 }
2640 
2641 /* ARGSUSED */
2642 static int
vd_geom2dk_geom(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)2643 vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
2644 {
2645 	VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
2646 	return (0);
2647 }
2648 
2649 /* ARGSUSED */
2650 static int
vd_vtoc2vtoc(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)2651 vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
2652 {
2653 	VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct extvtoc *)ioctl_arg);
2654 	return (0);
2655 }
2656 
2657 static void
dk_geom2vd_geom(void * ioctl_arg,void * vd_buf)2658 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
2659 {
2660 	DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
2661 }
2662 
2663 static void
vtoc2vd_vtoc(void * ioctl_arg,void * vd_buf)2664 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
2665 {
2666 	VTOC2VD_VTOC((struct extvtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
2667 }
2668 
2669 static int
vd_get_efi_in(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)2670 vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
2671 {
2672 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
2673 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
2674 	size_t data_len;
2675 
2676 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
2677 	if (vd_efi->length > data_len)
2678 		return (EINVAL);
2679 
2680 	dk_efi->dki_lba = vd_efi->lba;
2681 	dk_efi->dki_length = vd_efi->length;
2682 	dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
2683 	return (0);
2684 }
2685 
2686 static void
vd_get_efi_out(void * ioctl_arg,void * vd_buf)2687 vd_get_efi_out(void *ioctl_arg, void *vd_buf)
2688 {
2689 	int len;
2690 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
2691 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
2692 
2693 	len = vd_efi->length;
2694 	DK_EFI2VD_EFI(dk_efi, vd_efi);
2695 	kmem_free(dk_efi->dki_data, len);
2696 }
2697 
2698 static int
vd_set_efi_in(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)2699 vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
2700 {
2701 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
2702 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
2703 	size_t data_len;
2704 
2705 	data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
2706 	if (vd_efi->length > data_len)
2707 		return (EINVAL);
2708 
2709 	dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
2710 	VD_EFI2DK_EFI(vd_efi, dk_efi);
2711 	return (0);
2712 }
2713 
2714 static void
vd_set_efi_out(void * ioctl_arg,void * vd_buf)2715 vd_set_efi_out(void *ioctl_arg, void *vd_buf)
2716 {
2717 	vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
2718 	dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
2719 
2720 	kmem_free(dk_efi->dki_data, vd_efi->length);
2721 }
2722 
2723 static int
vd_scsicmd_in(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)2724 vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
2725 {
2726 	size_t vd_scsi_len;
2727 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
2728 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
2729 
2730 	/* check buffer size */
2731 	vd_scsi_len = VD_SCSI_SIZE;
2732 	vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
2733 	vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
2734 	vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
2735 	vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
2736 
2737 	ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
2738 
2739 	if (vd_buf_len < vd_scsi_len)
2740 		return (EINVAL);
2741 
2742 	/* set flags */
2743 	uscsi->uscsi_flags = vd_scsi_debug;
2744 
2745 	if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
2746 		uscsi->uscsi_flags |= USCSI_ISOLATE;
2747 		uscsi->uscsi_flags |= USCSI_DIAGNOSE;
2748 	}
2749 
2750 	/* task attribute */
2751 	switch (vd_scsi->task_attribute) {
2752 	case VD_SCSI_TASK_ACA:
2753 		uscsi->uscsi_flags |= USCSI_HEAD;
2754 		break;
2755 	case VD_SCSI_TASK_HQUEUE:
2756 		uscsi->uscsi_flags |= USCSI_HTAG;
2757 		break;
2758 	case VD_SCSI_TASK_ORDERED:
2759 		uscsi->uscsi_flags |= USCSI_OTAG;
2760 		break;
2761 	default:
2762 		uscsi->uscsi_flags |= USCSI_NOTAG;
2763 		break;
2764 	}
2765 
2766 	/* timeout */
2767 	uscsi->uscsi_timeout = vd_scsi->timeout;
2768 
2769 	/* cdb data */
2770 	uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
2771 	uscsi->uscsi_cdblen = vd_scsi->cdb_len;
2772 
2773 	/* sense buffer */
2774 	if (vd_scsi->sense_len != 0) {
2775 		uscsi->uscsi_flags |= USCSI_RQENABLE;
2776 		uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
2777 		uscsi->uscsi_rqlen = vd_scsi->sense_len;
2778 	}
2779 
2780 	if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
2781 		/* uscsi does not support read/write request */
2782 		return (EINVAL);
2783 	}
2784 
2785 	/* request data-in */
2786 	if (vd_scsi->datain_len != 0) {
2787 		uscsi->uscsi_flags |= USCSI_READ;
2788 		uscsi->uscsi_buflen = vd_scsi->datain_len;
2789 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
2790 	}
2791 
2792 	/* request data-out */
2793 	if (vd_scsi->dataout_len != 0) {
2794 		uscsi->uscsi_buflen = vd_scsi->dataout_len;
2795 		uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
2796 	}
2797 
2798 	return (0);
2799 }
2800 
2801 static void
vd_scsicmd_out(void * ioctl_arg,void * vd_buf)2802 vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
2803 {
2804 	vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
2805 	struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
2806 
2807 	/* output fields */
2808 	vd_scsi->cmd_status = uscsi->uscsi_status;
2809 
2810 	/* sense data */
2811 	if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
2812 	    (uscsi->uscsi_status == STATUS_CHECK ||
2813 	    uscsi->uscsi_status == STATUS_TERMINATED)) {
2814 		vd_scsi->sense_status = uscsi->uscsi_rqstatus;
2815 		if (uscsi->uscsi_rqstatus == STATUS_GOOD)
2816 			vd_scsi->sense_len -= uscsi->uscsi_rqresid;
2817 		else
2818 			vd_scsi->sense_len = 0;
2819 	} else {
2820 		vd_scsi->sense_len = 0;
2821 	}
2822 
2823 	if (uscsi->uscsi_status != STATUS_GOOD) {
2824 		vd_scsi->dataout_len = 0;
2825 		vd_scsi->datain_len = 0;
2826 		return;
2827 	}
2828 
2829 	if (uscsi->uscsi_flags & USCSI_READ) {
2830 		/* request data (read) */
2831 		vd_scsi->datain_len -= uscsi->uscsi_resid;
2832 		vd_scsi->dataout_len = 0;
2833 	} else {
2834 		/* request data (write) */
2835 		vd_scsi->datain_len = 0;
2836 		vd_scsi->dataout_len -= uscsi->uscsi_resid;
2837 	}
2838 }
2839 
2840 static ushort_t
vd_lbl2cksum(struct dk_label * label)2841 vd_lbl2cksum(struct dk_label *label)
2842 {
2843 	int	count;
2844 	ushort_t sum, *sp;
2845 
2846 	count =	(sizeof (struct dk_label)) / (sizeof (short)) - 1;
2847 	sp = (ushort_t *)label;
2848 	sum = 0;
2849 	while (count--) {
2850 		sum ^= *sp++;
2851 	}
2852 
2853 	return (sum);
2854 }
2855 
2856 /*
2857  * Copy information from a vtoc and dk_geom structures to a dk_label structure.
2858  */
2859 static void
vd_vtocgeom_to_label(struct extvtoc * vtoc,struct dk_geom * geom,struct dk_label * label)2860 vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom,
2861     struct dk_label *label)
2862 {
2863 	int i;
2864 
2865 	ASSERT(vtoc->v_nparts == V_NUMPAR);
2866 	ASSERT(vtoc->v_sanity == VTOC_SANE);
2867 
2868 	bzero(label, sizeof (struct dk_label));
2869 
2870 	label->dkl_ncyl = geom->dkg_ncyl;
2871 	label->dkl_acyl = geom->dkg_acyl;
2872 	label->dkl_pcyl = geom->dkg_pcyl;
2873 	label->dkl_nhead = geom->dkg_nhead;
2874 	label->dkl_nsect = geom->dkg_nsect;
2875 	label->dkl_intrlv = geom->dkg_intrlv;
2876 	label->dkl_apc = geom->dkg_apc;
2877 	label->dkl_rpm = geom->dkg_rpm;
2878 	label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
2879 	label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
2880 
2881 	label->dkl_vtoc.v_nparts = V_NUMPAR;
2882 	label->dkl_vtoc.v_sanity = VTOC_SANE;
2883 	label->dkl_vtoc.v_version = vtoc->v_version;
2884 	for (i = 0; i < V_NUMPAR; i++) {
2885 		label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
2886 		label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
2887 		label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
2888 		label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start /
2889 		    (label->dkl_nhead * label->dkl_nsect);
2890 		label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size;
2891 	}
2892 
2893 	/*
2894 	 * The bootinfo array can not be copied with bcopy() because
2895 	 * elements are of type long in vtoc (so 64-bit) and of type
2896 	 * int in dk_vtoc (so 32-bit).
2897 	 */
2898 	label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
2899 	label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
2900 	label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
2901 	bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII);
2902 	bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL);
2903 
2904 	/* re-compute checksum */
2905 	label->dkl_magic = DKL_MAGIC;
2906 	label->dkl_cksum = vd_lbl2cksum(label);
2907 }
2908 
2909 /*
2910  * Copy information from a dk_label structure to a vtoc and dk_geom structures.
2911  */
2912 static void
vd_label_to_vtocgeom(struct dk_label * label,struct extvtoc * vtoc,struct dk_geom * geom)2913 vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc,
2914     struct dk_geom *geom)
2915 {
2916 	int i;
2917 
2918 	bzero(vtoc, sizeof (struct extvtoc));
2919 	bzero(geom, sizeof (struct dk_geom));
2920 
2921 	geom->dkg_ncyl = label->dkl_ncyl;
2922 	geom->dkg_acyl = label->dkl_acyl;
2923 	geom->dkg_nhead = label->dkl_nhead;
2924 	geom->dkg_nsect = label->dkl_nsect;
2925 	geom->dkg_intrlv = label->dkl_intrlv;
2926 	geom->dkg_apc = label->dkl_apc;
2927 	geom->dkg_rpm = label->dkl_rpm;
2928 	geom->dkg_pcyl = label->dkl_pcyl;
2929 	geom->dkg_write_reinstruct = label->dkl_write_reinstruct;
2930 	geom->dkg_read_reinstruct = label->dkl_read_reinstruct;
2931 
2932 	vtoc->v_sanity = label->dkl_vtoc.v_sanity;
2933 	vtoc->v_version = label->dkl_vtoc.v_version;
2934 	vtoc->v_sectorsz = DEV_BSIZE;
2935 	vtoc->v_nparts = label->dkl_vtoc.v_nparts;
2936 
2937 	for (i = 0; i < vtoc->v_nparts; i++) {
2938 		vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag;
2939 		vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag;
2940 		vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno *
2941 		    (label->dkl_nhead * label->dkl_nsect);
2942 		vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk;
2943 		vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i];
2944 	}
2945 
2946 	/*
2947 	 * The bootinfo array can not be copied with bcopy() because
2948 	 * elements are of type long in vtoc (so 64-bit) and of type
2949 	 * int in dk_vtoc (so 32-bit).
2950 	 */
2951 	vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0];
2952 	vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1];
2953 	vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2];
2954 	bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII);
2955 	bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL);
2956 }
2957 
2958 /*
2959  * Check if a geometry is valid for a single-slice disk. A geometry is
2960  * considered valid if the main attributes of the geometry match with the
2961  * attributes of the fake geometry we have created.
2962  */
2963 static boolean_t
vd_slice_geom_isvalid(vd_t * vd,struct dk_geom * geom)2964 vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom)
2965 {
2966 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2967 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2968 
2969 	if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl ||
2970 	    geom->dkg_acyl != vd->dk_geom.dkg_acyl ||
2971 	    geom->dkg_nsect != vd->dk_geom.dkg_nsect ||
2972 	    geom->dkg_pcyl != vd->dk_geom.dkg_pcyl)
2973 		return (B_FALSE);
2974 
2975 	return (B_TRUE);
2976 }
2977 
2978 /*
2979  * Check if a vtoc is valid for a single-slice disk. A vtoc is considered
2980  * valid if the main attributes of the vtoc match with the attributes of the
2981  * fake vtoc we have created.
2982  */
2983 static boolean_t
vd_slice_vtoc_isvalid(vd_t * vd,struct extvtoc * vtoc)2984 vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc)
2985 {
2986 	size_t csize;
2987 	int i;
2988 
2989 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
2990 	ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
2991 
2992 	if (vtoc->v_sanity != vd->vtoc.v_sanity ||
2993 	    vtoc->v_version != vd->vtoc.v_version ||
2994 	    vtoc->v_nparts != vd->vtoc.v_nparts ||
2995 	    strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 ||
2996 	    strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0)
2997 		return (B_FALSE);
2998 
2999 	/* slice 2 should be unchanged */
3000 	if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start !=
3001 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start ||
3002 	    vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size !=
3003 	    vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size)
3004 		return (B_FALSE);
3005 
3006 	/*
3007 	 * Slice 0 should be mostly unchanged and cover most of the disk.
3008 	 * However we allow some flexibility wrt to the start and the size
3009 	 * of this slice mainly because we can't exactly know how it will
3010 	 * be defined by the OS installer.
3011 	 *
3012 	 * We allow slice 0 to be defined as starting on any of the first
3013 	 * 4 cylinders.
3014 	 */
3015 	csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
3016 
3017 	if (vtoc->v_part[0].p_start > 4 * csize ||
3018 	    vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size)
3019 			return (B_FALSE);
3020 
3021 	if (vd->vtoc.v_part[0].p_size >= 4 * csize &&
3022 	    vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize)
3023 			return (B_FALSE);
3024 
3025 	/* any other slice should have a size of 0 */
3026 	for (i = 1; i < vtoc->v_nparts; i++) {
3027 		if (i != VD_ENTIRE_DISK_SLICE &&
3028 		    vtoc->v_part[i].p_size != 0)
3029 			return (B_FALSE);
3030 	}
3031 
3032 	return (B_TRUE);
3033 }
3034 
3035 /*
3036  * Handle ioctls to a disk slice.
3037  *
3038  * Return Values
3039  *	0	- Indicates that there are no errors in disk operations
3040  *	ENOTSUP	- Unknown disk label type or unsupported DKIO ioctl
3041  *	EINVAL	- Not enough room to copy the EFI label
3042  *
3043  */
3044 static int
vd_do_slice_ioctl(vd_t * vd,int cmd,void * ioctl_arg)3045 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
3046 {
3047 	dk_efi_t *dk_ioc;
3048 	struct extvtoc *vtoc;
3049 	struct dk_geom *geom;
3050 	size_t len, lba;
3051 
3052 	ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
3053 
3054 	if (cmd == DKIOCFLUSHWRITECACHE)
3055 		return (vd_flush_write(vd));
3056 
3057 	switch (vd->vdisk_label) {
3058 
3059 	/* ioctls for a single slice disk with a VTOC label */
3060 	case VD_DISK_LABEL_VTOC:
3061 
3062 		switch (cmd) {
3063 
3064 		case DKIOCGGEOM:
3065 			ASSERT(ioctl_arg != NULL);
3066 			bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
3067 			return (0);
3068 
3069 		case DKIOCGEXTVTOC:
3070 			ASSERT(ioctl_arg != NULL);
3071 			bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
3072 			return (0);
3073 
3074 		case DKIOCSGEOM:
3075 			ASSERT(ioctl_arg != NULL);
3076 			if (vd_slice_single_slice)
3077 				return (ENOTSUP);
3078 
3079 			/* fake success only if new geometry is valid */
3080 			geom = (struct dk_geom *)ioctl_arg;
3081 			if (!vd_slice_geom_isvalid(vd, geom))
3082 				return (EINVAL);
3083 
3084 			return (0);
3085 
3086 		case DKIOCSEXTVTOC:
3087 			ASSERT(ioctl_arg != NULL);
3088 			if (vd_slice_single_slice)
3089 				return (ENOTSUP);
3090 
3091 			/* fake sucess only if the new vtoc is valid */
3092 			vtoc = (struct extvtoc *)ioctl_arg;
3093 			if (!vd_slice_vtoc_isvalid(vd, vtoc))
3094 				return (EINVAL);
3095 
3096 			return (0);
3097 
3098 		default:
3099 			return (ENOTSUP);
3100 		}
3101 
3102 	/* ioctls for a single slice disk with an EFI label */
3103 	case VD_DISK_LABEL_EFI:
3104 
3105 		if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI)
3106 			return (ENOTSUP);
3107 
3108 		ASSERT(ioctl_arg != NULL);
3109 		dk_ioc = (dk_efi_t *)ioctl_arg;
3110 
3111 		len = dk_ioc->dki_length;
3112 		lba = dk_ioc->dki_lba;
3113 
3114 		if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) ||
3115 		    (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) ||
3116 		    (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t)))
3117 			return (EINVAL);
3118 
3119 		switch (cmd) {
3120 		case DKIOCGETEFI:
3121 			len = vd_slice_flabel_read(vd,
3122 			    (caddr_t)dk_ioc->dki_data,
3123 			    lba * vd->vdisk_bsize, len);
3124 
3125 			ASSERT(len > 0);
3126 
3127 			return (0);
3128 
3129 		case DKIOCSETEFI:
3130 			if (vd_slice_single_slice)
3131 				return (ENOTSUP);
3132 
3133 			/* we currently don't support writing EFI */
3134 			return (EIO);
3135 		}
3136 
3137 		/* FALLTHROUGH */
3138 	default:
3139 		/* Unknown disk label type */
3140 		return (ENOTSUP);
3141 	}
3142 }
3143 
3144 static int
vds_efi_alloc_and_read(vd_t * vd,efi_gpt_t ** gpt,efi_gpe_t ** gpe)3145 vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
3146 {
3147 	vd_efi_dev_t edev;
3148 	int status;
3149 
3150 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
3151 
3152 	status = vd_efi_alloc_and_read(&edev, gpt, gpe);
3153 
3154 	return (status);
3155 }
3156 
3157 static void
vds_efi_free(vd_t * vd,efi_gpt_t * gpt,efi_gpe_t * gpe)3158 vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
3159 {
3160 	vd_efi_dev_t edev;
3161 
3162 	VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
3163 
3164 	vd_efi_free(&edev, gpt, gpe);
3165 }
3166 
3167 static int
vd_dskimg_validate_efi(vd_t * vd)3168 vd_dskimg_validate_efi(vd_t *vd)
3169 {
3170 	efi_gpt_t *gpt;
3171 	efi_gpe_t *gpe;
3172 	int i, nparts, status;
3173 	struct uuid efi_reserved = EFI_RESERVED;
3174 
3175 	if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
3176 		return (status);
3177 
3178 	bzero(&vd->vtoc, sizeof (struct extvtoc));
3179 	bzero(&vd->dk_geom, sizeof (struct dk_geom));
3180 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
3181 
3182 	vd->efi_reserved = -1;
3183 
3184 	nparts = gpt->efi_gpt_NumberOfPartitionEntries;
3185 
3186 	for (i = 0; i < nparts && i < VD_MAXPART; i++) {
3187 
3188 		if (gpe[i].efi_gpe_StartingLBA == 0 &&
3189 		    gpe[i].efi_gpe_EndingLBA == 0) {
3190 			continue;
3191 		}
3192 
3193 		vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
3194 		vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
3195 		    gpe[i].efi_gpe_StartingLBA + 1;
3196 
3197 		if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
3198 		    sizeof (struct uuid)) == 0)
3199 			vd->efi_reserved = i;
3200 
3201 	}
3202 
3203 	ASSERT(vd->vdisk_size != 0);
3204 	vd->slices[VD_EFI_WD_SLICE].start = 0;
3205 	vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
3206 
3207 	vds_efi_free(vd, gpt, gpe);
3208 
3209 	return (status);
3210 }
3211 
3212 /*
3213  * Function:
3214  *	vd_dskimg_validate_geometry
3215  *
3216  * Description:
3217  *	Read the label and validate the geometry of a disk image. The driver
3218  *	label, vtoc and geometry information are updated according to the
3219  *	label read from the disk image.
3220  *
3221  *	If no valid label is found, the label is set to unknown and the
3222  *	function returns EINVAL, but a default vtoc and geometry are provided
3223  *	to the driver. If an EFI label is found, ENOTSUP is returned.
3224  *
3225  * Parameters:
3226  *	vd	- disk on which the operation is performed.
3227  *
3228  * Return Code:
3229  *	0	- success.
3230  *	EIO	- error reading the label from the disk image.
3231  *	EINVAL	- unknown disk label.
3232  *	ENOTSUP	- geometry not applicable (EFI label).
3233  */
3234 static int
vd_dskimg_validate_geometry(vd_t * vd)3235 vd_dskimg_validate_geometry(vd_t *vd)
3236 {
3237 	struct dk_label label;
3238 	struct dk_geom *geom = &vd->dk_geom;
3239 	struct extvtoc *vtoc = &vd->vtoc;
3240 	int i;
3241 	int status = 0;
3242 
3243 	ASSERT(VD_DSKIMG(vd));
3244 
3245 	if (VD_DSKIMG_LABEL_READ(vd, &label) < 0)
3246 		return (EIO);
3247 
3248 	if (label.dkl_magic != DKL_MAGIC ||
3249 	    label.dkl_cksum != vd_lbl2cksum(&label) ||
3250 	    (vd_dskimg_validate_sanity &&
3251 	    label.dkl_vtoc.v_sanity != VTOC_SANE) ||
3252 	    label.dkl_vtoc.v_nparts != V_NUMPAR) {
3253 
3254 		if (vd_dskimg_validate_efi(vd) == 0) {
3255 			vd->vdisk_label = VD_DISK_LABEL_EFI;
3256 			return (ENOTSUP);
3257 		}
3258 
3259 		vd->vdisk_label = VD_DISK_LABEL_UNK;
3260 		vd_build_default_label(vd->dskimg_size, vd->vdisk_bsize,
3261 		    &label);
3262 		status = EINVAL;
3263 	} else {
3264 		vd->vdisk_label = VD_DISK_LABEL_VTOC;
3265 	}
3266 
3267 	/* Update the driver geometry and vtoc */
3268 	vd_label_to_vtocgeom(&label, vtoc, geom);
3269 
3270 	/* Update logical partitions */
3271 	bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
3272 	if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
3273 		for (i = 0; i < vtoc->v_nparts; i++) {
3274 			vd->slices[i].start = vtoc->v_part[i].p_start;
3275 			vd->slices[i].nblocks = vtoc->v_part[i].p_size;
3276 		}
3277 	}
3278 
3279 	return (status);
3280 }
3281 
3282 /*
3283  * Handle ioctls to a disk image.
3284  *
3285  * Return Values
3286  *	0	- Indicates that there are no errors
3287  *	!= 0	- Disk operation returned an error
3288  */
3289 static int
vd_do_dskimg_ioctl(vd_t * vd,int cmd,void * ioctl_arg)3290 vd_do_dskimg_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
3291 {
3292 	struct dk_label label;
3293 	struct dk_geom *geom;
3294 	struct extvtoc *vtoc;
3295 	dk_efi_t *efi;
3296 	int rc;
3297 
3298 	ASSERT(VD_DSKIMG(vd));
3299 
3300 	switch (cmd) {
3301 
3302 	case DKIOCGGEOM:
3303 		ASSERT(ioctl_arg != NULL);
3304 		geom = (struct dk_geom *)ioctl_arg;
3305 
3306 		rc = vd_dskimg_validate_geometry(vd);
3307 		if (rc != 0 && rc != EINVAL)
3308 			return (rc);
3309 		bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
3310 		return (0);
3311 
3312 	case DKIOCGEXTVTOC:
3313 		ASSERT(ioctl_arg != NULL);
3314 		vtoc = (struct extvtoc *)ioctl_arg;
3315 
3316 		rc = vd_dskimg_validate_geometry(vd);
3317 		if (rc != 0 && rc != EINVAL)
3318 			return (rc);
3319 		bcopy(&vd->vtoc, vtoc, sizeof (struct extvtoc));
3320 		return (0);
3321 
3322 	case DKIOCSGEOM:
3323 		ASSERT(ioctl_arg != NULL);
3324 		geom = (struct dk_geom *)ioctl_arg;
3325 
3326 		if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
3327 			return (EINVAL);
3328 
3329 		/*
3330 		 * The current device geometry is not updated, just the driver
3331 		 * "notion" of it. The device geometry will be effectively
3332 		 * updated when a label is written to the device during a next
3333 		 * DKIOCSEXTVTOC.
3334 		 */
3335 		bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
3336 		return (0);
3337 
3338 	case DKIOCSEXTVTOC:
3339 		ASSERT(ioctl_arg != NULL);
3340 		ASSERT(vd->dk_geom.dkg_nhead != 0 &&
3341 		    vd->dk_geom.dkg_nsect != 0);
3342 		vtoc = (struct extvtoc *)ioctl_arg;
3343 
3344 		if (vtoc->v_sanity != VTOC_SANE ||
3345 		    vtoc->v_sectorsz != DEV_BSIZE ||
3346 		    vtoc->v_nparts != V_NUMPAR)
3347 			return (EINVAL);
3348 
3349 		vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label);
3350 
3351 		/* write label to the disk image */
3352 		if ((rc = vd_dskimg_set_vtoc(vd, &label)) != 0)
3353 			return (rc);
3354 
3355 		break;
3356 
3357 	case DKIOCFLUSHWRITECACHE:
3358 		return (vd_flush_write(vd));
3359 
3360 	case DKIOCGETEFI:
3361 		ASSERT(ioctl_arg != NULL);
3362 		efi = (dk_efi_t *)ioctl_arg;
3363 
3364 		if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
3365 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3366 			return (EIO);
3367 
3368 		return (0);
3369 
3370 	case DKIOCSETEFI:
3371 		ASSERT(ioctl_arg != NULL);
3372 		efi = (dk_efi_t *)ioctl_arg;
3373 
3374 		if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
3375 		    (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
3376 			return (EIO);
3377 
3378 		break;
3379 
3380 
3381 	default:
3382 		return (ENOTSUP);
3383 	}
3384 
3385 	ASSERT(cmd == DKIOCSEXTVTOC || cmd == DKIOCSETEFI);
3386 
3387 	/* label has changed, revalidate the geometry */
3388 	(void) vd_dskimg_validate_geometry(vd);
3389 
3390 	/*
3391 	 * The disk geometry may have changed, so we need to write
3392 	 * the devid (if there is one) so that it is stored at the
3393 	 * right location.
3394 	 */
3395 	if (vd_dskimg_write_devid(vd, vd->dskimg_devid) != 0) {
3396 		PR0("Fail to write devid");
3397 	}
3398 
3399 	return (0);
3400 }
3401 
3402 static int
vd_backend_ioctl(vd_t * vd,int cmd,caddr_t arg)3403 vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
3404 {
3405 	int rval = 0, status;
3406 	struct vtoc vtoc;
3407 
3408 	/*
3409 	 * Call the appropriate function to execute the ioctl depending
3410 	 * on the type of vdisk.
3411 	 */
3412 	if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
3413 
3414 		/* slice, file or volume exported as a single slice disk */
3415 		status = vd_do_slice_ioctl(vd, cmd, arg);
3416 
3417 	} else if (VD_DSKIMG(vd)) {
3418 
3419 		/* file or volume exported as a full disk */
3420 		status = vd_do_dskimg_ioctl(vd, cmd, arg);
3421 
3422 	} else {
3423 
3424 		/* disk device exported as a full disk */
3425 		status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
3426 		    vd->open_flags | FKIOCTL, kcred, &rval);
3427 
3428 		/*
3429 		 * By default VTOC ioctls are done using ioctls for the
3430 		 * extended VTOC. Some drivers (in particular non-Sun drivers)
3431 		 * may not support these ioctls. In that case, we fallback to
3432 		 * the regular VTOC ioctls.
3433 		 */
3434 		if (status == ENOTTY) {
3435 			switch (cmd) {
3436 
3437 			case DKIOCGEXTVTOC:
3438 				cmd = DKIOCGVTOC;
3439 				status = ldi_ioctl(vd->ldi_handle[0], cmd,
3440 				    (intptr_t)&vtoc, vd->open_flags | FKIOCTL,
3441 				    kcred, &rval);
3442 				vtoctoextvtoc(vtoc,
3443 				    (*(struct extvtoc *)(void *)arg));
3444 				break;
3445 
3446 			case DKIOCSEXTVTOC:
3447 				cmd = DKIOCSVTOC;
3448 				extvtoctovtoc((*(struct extvtoc *)(void *)arg),
3449 				    vtoc);
3450 				status = ldi_ioctl(vd->ldi_handle[0], cmd,
3451 				    (intptr_t)&vtoc, vd->open_flags | FKIOCTL,
3452 				    kcred, &rval);
3453 				break;
3454 			}
3455 		}
3456 	}
3457 
3458 #ifdef DEBUG
3459 	if (rval != 0) {
3460 		PR0("ioctl %x set rval = %d, which is not being returned"
3461 		    " to caller", cmd, rval);
3462 	}
3463 #endif /* DEBUG */
3464 
3465 	return (status);
3466 }
3467 
3468 /*
3469  * Description:
3470  *	This is the function that processes the ioctl requests (farming it
3471  *	out to functions that handle slices, files or whole disks)
3472  *
3473  * Return Values
3474  *     0		- ioctl operation completed successfully
3475  *     != 0		- The LDC error value encountered
3476  *			  (propagated back up the call stack as a task error)
3477  *
3478  * Side Effect
3479  *     sets request->status to the return value of the ioctl function.
3480  */
3481 static int
vd_do_ioctl(vd_t * vd,vd_dring_payload_t * request,void * buf,vd_ioctl_t * ioctl)3482 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
3483 {
3484 	int	status = 0;
3485 	size_t	nbytes = request->nbytes;	/* modifiable copy */
3486 
3487 
3488 	ASSERT(request->slice < vd->nslices);
3489 	PR0("Performing %s", ioctl->operation_name);
3490 
3491 	/* Get data from client and convert, if necessary */
3492 	if (ioctl->copyin != NULL)  {
3493 		ASSERT(nbytes != 0 && buf != NULL);
3494 		PR1("Getting \"arg\" data from client");
3495 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
3496 		    request->cookie, request->ncookies,
3497 		    LDC_COPY_IN)) != 0) {
3498 			PR0("ldc_mem_copy() returned errno %d "
3499 			    "copying from client", status);
3500 			return (status);
3501 		}
3502 
3503 		/* Convert client's data, if necessary */
3504 		if (ioctl->copyin == VD_IDENTITY_IN) {
3505 			/* use client buffer */
3506 			ioctl->arg = buf;
3507 		} else {
3508 			/* convert client vdisk operation data to ioctl data */
3509 			status = (ioctl->copyin)(buf, nbytes,
3510 			    (void *)ioctl->arg);
3511 			if (status != 0) {
3512 				request->status = status;
3513 				return (0);
3514 			}
3515 		}
3516 	}
3517 
3518 	if (ioctl->operation == VD_OP_SCSICMD) {
3519 		struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
3520 
3521 		/* check write permission */
3522 		if (!(vd->open_flags & FWRITE) &&
3523 		    !(uscsi->uscsi_flags & USCSI_READ)) {
3524 			PR0("uscsi fails because backend is opened read-only");
3525 			request->status = EROFS;
3526 			return (0);
3527 		}
3528 	}
3529 
3530 	/*
3531 	 * Send the ioctl to the disk backend.
3532 	 */
3533 	request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
3534 
3535 	if (request->status != 0) {
3536 		PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
3537 		if (ioctl->operation == VD_OP_SCSICMD &&
3538 		    ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
3539 			/*
3540 			 * USCSICMD has reported an error and the uscsi_status
3541 			 * field is not zero. This means that the SCSI command
3542 			 * has completed but it has an error. So we should
3543 			 * mark the VD operation has succesfully completed
3544 			 * and clients can check the SCSI status field for
3545 			 * SCSI errors.
3546 			 */
3547 			request->status = 0;
3548 		else
3549 			return (0);
3550 	}
3551 
3552 	/* Convert data and send to client, if necessary */
3553 	if (ioctl->copyout != NULL)  {
3554 		ASSERT(nbytes != 0 && buf != NULL);
3555 		PR1("Sending \"arg\" data to client");
3556 
3557 		/* Convert ioctl data to vdisk operation data, if necessary */
3558 		if (ioctl->copyout != VD_IDENTITY_OUT)
3559 			(ioctl->copyout)((void *)ioctl->arg, buf);
3560 
3561 		if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
3562 		    request->cookie, request->ncookies,
3563 		    LDC_COPY_OUT)) != 0) {
3564 			PR0("ldc_mem_copy() returned errno %d "
3565 			    "copying to client", status);
3566 			return (status);
3567 		}
3568 	}
3569 
3570 	return (status);
3571 }
3572 
3573 #define	RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
3574 
3575 /*
3576  * Description:
3577  *	This generic function is called by the task queue to complete
3578  *	the processing of the tasks. The specific completion function
3579  *	is passed in as a field in the task pointer.
3580  *
3581  * Parameters:
3582  *	arg	- opaque pointer to structure containing task to be completed
3583  *
3584  * Return Values
3585  *	None
3586  */
3587 static void
vd_complete(void * arg)3588 vd_complete(void *arg)
3589 {
3590 	vd_task_t	*task = (vd_task_t *)arg;
3591 
3592 	ASSERT(task != NULL);
3593 	ASSERT(task->status == EINPROGRESS);
3594 	ASSERT(task->completef != NULL);
3595 
3596 	task->status = task->completef(task);
3597 	if (task->status)
3598 		PR0("%s: Error %d completing task", __func__, task->status);
3599 
3600 	/* Now notify the vDisk client */
3601 	vd_complete_notify(task);
3602 }
3603 
3604 static int
vd_ioctl(vd_task_t * task)3605 vd_ioctl(vd_task_t *task)
3606 {
3607 	int			i, status;
3608 	void			*