vds.c revision 193974072f41a843678abf5f61979c748687e66b
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Virtual disk server
*/
#include <sys/pathname.h>
#include <sys/sysmacros.h>
#include <sys/vio_common.h>
#include <sys/vio_util.h>
#include <sys/vdsk_mailbox.h>
#include <sys/vdsk_common.h>
/* Virtual disk server initialization flags */
#define VDS_LDI 0x01
#define VDS_MDEG 0x02
/* Virtual disk server tunable parameters */
#define VDS_RETRIES 5
#define VDS_NCHAINS 32
/* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
#define VDS_NAME "virtual-disk-server"
#define VD_NAME "vd"
#define VD_VOLUME_NAME "vdisk"
#define VD_ASCIILABEL "Virtual Disk"
#define VD_CHANNEL_ENDPOINT "channel-endpoint"
#define VD_ID_PROP "id"
#define VD_BLOCK_DEVICE_PROP "vds-block-device"
#define VD_BLOCK_DEVICE_OPTS "vds-block-device-opts"
#define VD_REG_PROP "reg"
/* Virtual disk initialization flags */
#define VD_DISK_READY 0x01
#define VD_LOCKING 0x02
#define VD_LDC 0x04
#define VD_DRING 0x08
#define VD_SID 0x10
#define VD_SEQ_NUM 0x20
#define VD_SETUP_ERROR 0x40
/* Flags for writing to a vdisk which is a file */
#define VD_FILE_WRITE_FLAGS SM_ASYNC
/* Number of backup labels */
#define VD_DSKIMG_NUM_BACKUP 5
/* Timeout for SCSI I/O */
/* Maximum number of logical partitions */
/*
* unfortunately, this convention does not appear to be codified.
*/
#define VD_ENTIRE_DISK_SLICE 2
/* Logical block address for EFI */
/* Driver types */
typedef enum vd_driver {
VD_DRIVER_UNKNOWN = 0, /* driver type unknown */
VD_DRIVER_DISK, /* disk driver */
VD_DRIVER_VOLUME /* volume driver */
} vd_driver_t;
#define VD_DRIVER_NAME_LEN 64
typedef struct vd_driver_type {
/*
* There is no reliable way to determine if a device is representing a disk
* or a volume, especially with pseudo devices. So we maintain a list of well
* known drivers and the type of device they represent (either a disk or a
* volume).
*
* The list can be extended by adding a "driver-type-list" entry in vds.conf
* with the following syntax:
*
* driver-type-list="<driver>:<type>", ... ,"<driver>:<type>";
*
* Where:
* <driver> is the name of a driver (limited to 64 characters)
* <type> is either the string "disk" or "volume"
*
* Invalid entries in "driver-type-list" will be ignored.
*
* For example, the following line in vds.conf:
*
* driver-type-list="foo:disk","bar:volume";
*
* defines that "foo" is a disk driver, and driver "bar" is a volume driver.
*
* When a list is defined in vds.conf, it is checked before the built-in list
* (vds_driver_types[]) so that any definition from this list can be overriden
* using vds.conf.
*/
};
/* Return a cpp token as a string */
/*
* Print a message prefixed with the current function name to the message log
* (and optionally to the console for verbose boots); these macros use cpp's
* concatenation of string literals and C99 variable-length-argument-list
* macros
*/
/* Return a pointer to the "i"th vdisk dring element */
#define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \
/* Return the virtual disk client's type as a string (for use in messages) */
"unsupported client")))
/* Read disk label from a disk image */
0, sizeof (struct dk_label))
/* Write disk label to a disk image */
0, sizeof (struct dk_label))
/* Identify if a backend is a disk image */
/* Message for disk access rights reset failure */
#define VD_RESET_ACCESS_FAILURE_MSG \
"Fail to reset disk access rights for disk %s"
/*
* Specification of an MD node passed to the MDEG to filter any
* 'vport' nodes that do not belong to the specified node. This
* template is copied for each vds instance and filled in with
* the appropriate 'cfg-handle' value before being passed to the MDEG.
*/
static mdeg_prop_spec_t vds_prop_template[] = {
};
/*
* Matching criteria passed to the MDEG to register interest
* in changes to 'virtual-device-port' nodes identified by their
* 'id' property.
*/
static md_prop_match_t vd_prop_match[] = {
{ MDET_PROP_VAL, VD_ID_PROP },
{ MDET_LIST_END, NULL }
};
/*
* Options for the VD_BLOCK_DEVICE_OPTS property.
*/
#define VD_OPTION_NLEN 128
typedef struct vd_option {
char vdo_name[VD_OPTION_NLEN];
} vd_option_t;
vd_option_t vd_bdev_options[] = {
{ "ro", VD_OPT_RDONLY },
{ "slice", VD_OPT_SLICE },
{ "excl", VD_OPT_EXCLUSIVE }
};
/* Debugging macros */
#ifdef DEBUG
static int vd_msglevel = 0;
#define VD_DUMP_DRING_ELEM(elem) \
PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \
char *
vd_decode_state(int state)
{
char *str;
switch (state) {
default: str = "unknown"; break;
}
return (str);
}
void
{
default: tstr = "unknown"; break;
}
default: sstr = "unknown"; break;
}
default: estr = "unknown"; break;
}
PR1("(%x/%x/%x) message : (%s/%s/%s)",
}
#else /* !DEBUG */
#define PR0(...)
#define PR1(...)
#define PR2(...)
#define VD_DUMP_DRING_ELEM(elem)
#endif /* DEBUG */
/*
* Soft state structure for a vds instance
*/
typedef struct vds {
int num_drivers; /* num of extra driver types */
} vds_t;
/*
* Types of descriptor-processing tasks
*/
typedef enum vd_task_type {
VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */
VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */
/*
* Structure describing the task for processing a descriptor
*/
typedef struct vd_task {
int index; /* dring elem index for task */
int status; /* status of processing task */
} vd_task_t;
/*
* Soft state structure for a virtual disk instance
*/
typedef struct vd {
int open_flags; /* open flags */
int efi_reserved; /* EFI reserved slice */
} vd_t;
/*
* Macros to manipulate the fake label (flabel) for single slice disks.
*
* If we fake a VTOC label then the fake label consists of only one block
* containing the VTOC label (struct dk_label).
*
* If we fake an EFI label then the fake label consists of a blank block
* followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t).
*
*/
#define VD_LABEL_VTOC_SIZE \
#define VD_LABEL_EFI_SIZE \
#define VD_LABEL_VTOC(vd) \
#define VD_LABEL_EFI_GPT(vd) \
#define VD_LABEL_EFI_GPE(vd) \
sizeof (efi_gpt_t)))
typedef struct vds_operation {
char *namep;
typedef struct vd_ioctl {
const char *operation_name; /* vdisk operation name */
int cmd; /* corresponding ioctl cmd */
const char *cmd_name; /* ioctl cmd name */
void *arg; /* ioctl cmd argument */
/* convert input vd_buf to output ioctl_arg */
/* convert input ioctl_arg to output vd_buf */
/* write is true if the operation writes any data to the backend */
} vd_ioctl_t;
#define VD_IDENTITY_OUT ((void (*)(void *, void *))-1)
static int vds_ldc_retries = VDS_RETRIES;
static int vds_ldc_delay = VDS_LDC_DELAY;
static int vds_dev_retries = VDS_RETRIES;
static int vds_dev_delay = VDS_DEV_DELAY;
static void *vds_state;
static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
static int vd_scsi_debug = USCSI_SILENT;
/*
* Tunable to define the behavior of the service domain if the vdisk server
* fails to reset disk exclusive access when a LDC channel is reset. When a
* LDC channel is reset the vdisk server will try to reset disk exclusive
* access by releasing any SCSI-2 reservation or resetting the disk. If these
* actions fail then the default behavior (vd_reset_access_failure = 0) is to
* print a warning message. This default behavior can be changed by setting
* the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
* cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
* the service domain to panic. In both cases, the reset of the service domain
* should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
*/
static int vd_reset_access_failure = 0;
/*
* Tunable for backward compatibility. When this variable is set to B_TRUE,
* all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
* slice disks whether or not they have the "slice" option set. This is
* to provide a simple backward compatibility mechanism when upgrading
* the vds driver and using a domain configuration created before the
* "slice" option was available.
*/
/*
* The label of disk images created with some earlier versions of the virtual
* disk software is not entirely correct and have an incorrect v_sanity field
* (usually 0) instead of VTOC_SANE. This creates a compatibility problem with
* these images because we are now validating that the disk label (and the
* sanity) is correct when a disk image is opened.
*
* This tunable is set to false to not validate the sanity field and ensure
* compatibility. If the tunable is set to true, we will do a strict checking
* of the sanity but this can create compatibility problems with old disk
* images.
*/
/*
* Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings.
*/
/*
* When a backend is exported as a single-slice disk then we entirely fake
* its disk label. So it can be exported either with a VTOC label or with
* an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all
* single-slice disks will be exported with a VTOC label; and if it is set
* to VD_DISK_LABEL_EFI then all single-slice disks will be exported with
* an EFI label.
*
* If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk
* or volume device then it will be exported with the same type of label as
* defined on the device. Otherwise if the backend is a file then it will
* exported with the disk label type set in the vd_file_slice_label variable.
*
* Note that if the backend size is greater than 1TB then it will always be
* exported with an EFI label no matter what the setting is.
*/
/*
* Tunable for backward compatibility. If this variable is set to B_TRUE then
* single-slice disks are exported as disks with only one slice instead of
* faking a complete disk partitioning.
*/
/*
* Supported protocol version pairs, from highest (newest) to lowest (oldest)
*
* Each supported major version should appear only once, paired with (and only
* with) its highest supported minor version number (as the protocol requires
* supporting all lower minor version numbers as well)
*/
static const size_t vds_num_versions =
sizeof (vds_version)/sizeof (vds_version[0]);
extern int is_pseudo_device(dev_info_t *);
/*
* Function:
* vd_get_readable_size
*
* Description:
* Convert a given size in bytes to a human readable format in
* kilobytes, megabytes, gigabytes or terabytes.
*
* Parameters:
* full_size - the size to convert in bytes.
* size - the converted size.
* unit - the unit of the converted size: 'K' (kilobyte),
* 'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte).
*
* Return Code:
* none
*/
static void
{
} else {
}
}
/*
* Function:
* vd_dskimg_io_params
*
* Description:
* Convert virtual disk I/O parameters (slice, block, length) to
* (offset, length) relative to the disk image and according to
* the virtual disk partitioning.
*
* Parameters:
* vd - disk on which the operation is performed.
* slice - slice to which is the I/O parameters apply.
* VD_SLICE_NONE indicates that parameters are
* are relative to the entire virtual disk.
* blkp - pointer to the starting block relative to the
* slice; return the starting block relative to
* the disk image.
* lenp - pointer to the number of bytes requested; return
* the number of bytes that can effectively be used.
*
* Return Code:
* 0 - I/O parameters have been successfully converted;
* blkp and lenp point to the converted values.
* ENODATA - no data are available for the given I/O parameters;
* This occurs if the starting block is past the limit
* of the slice.
* EINVAL - I/O parameters are invalid.
*/
static int
{
/*
* If a file is exported as a slice then we don't care about the vtoc.
* In that case, the vtoc is a fake mainly to make newfs happy and we
* handle any I/O as a raw disk access so that we can have access to the
* entire backend.
*/
/* raw disk access */
/* offset past the end of the disk */
PR0("offset (0x%lx) >= size (0x%lx)",
return (ENODATA);
}
} else {
/*
* v1.0 vDisk clients depended on the server not verifying
* the label of a unformatted disk. This "feature" is
* maintained for backward compatibility but all versions
* from v1.1 onwards must do the right thing.
*/
(void) vd_dskimg_validate_geometry(vd);
PR0("Unknown disk label, can't do I/O "
"from slice %d", slice);
return (EINVAL);
}
}
} else {
}
/* address past the end of the slice */
PR0("req_addr (0x%lx) >= psize (0x%lx)",
return (ENODATA);
}
}
/*
* If the requested size is greater than the size
*/
PR0("I/O size truncated to %lu bytes from %lu bytes",
}
/*
* range. If we have a partial disk image (e.g. an image of
* s0 instead s2) the system can try to access slices that
* are not included into the disk image.
*/
PR0("offset + nbytes (0x%lx + 0x%lx) > "
return (EINVAL);
}
return (0);
}
/*
* Function:
* vd_dskimg_rw
*
* Description:
* Read or write to a disk image. It handles the case where the disk
* image is a file or a volume exported as a full disk or a file
* exported as single-slice disk. Read or write to volumes exported as
* single slice disks are done by directly using the ldi interface.
*
* Parameters:
* vd - disk on which the operation is performed.
* slice - slice on which the operation is performed,
* VD_SLICE_NONE indicates that the operation
* is done using an absolute disk offset.
* operation - operation to execute: read (VD_OP_BREAD) or
* write (VD_OP_BWRITE).
* data - buffer where data are read to or written from.
* blk - starting block for the operation.
* len - number of bytes to read or write.
*
* Return Code:
* n >= 0 - success, n indicates the number of bytes read
* or written.
* -1 - error.
*/
static ssize_t
{
int status;
/*
* We use ldi_strategy() and not ldi_read()/ldi_write() because
* lock pages of the data buffer, and this requires the data
* buffer to be kmem_alloc'ed (and not allocated on the stack).
*
* Also using ldi_strategy() ensures that writes are immediatly
* commited and not cached as this may be the case with
* ldi_write() (for example with a ZFS volume).
*/
return (-1);
}
return (-1);
}
}
n = len;
do {
/*
* segmap_getmapflt() returns a MAXBSIZE chunk which is
* MAXBSIZE aligned.
*/
/*
* Fault in the pages so we can check for error and ensure
* that we can safely used the mapped address.
*/
F_SOFTLOCK, srw) != 0) {
return (-1);
}
if (operation == VD_OP_BREAD)
else
F_SOFTUNLOCK, srw) != 0) {
return (-1);
}
return (-1);
n -= mlen;
} while (n > 0);
return (len);
}
/*
* Function:
* vd_build_default_label
*
* Description:
* Return a default label for a given disk size. This is used when the disk
* does not have a valid VTOC so that the user can get a valid default
* configuration. The default label has all slice sizes set to 0 (except
* slice 2 which is the entire disk) to force the user to write a valid
* label onto the disk image.
*
* Parameters:
* disk_size - the disk size in bytes
* label - the returned default label.
*
* Return Code:
* none.
*/
static void
{
char unit;
/*
* Ideally we would like the cylinder size (nsect * nhead) to be the
* same whatever the disk size is. That way the VTOC label could be
* easily updated in case the disk size is increased (keeping the
* same cylinder size allows to preserve the existing partitioning
* when updating the VTOC label). But it is not possible to have
* a fixed cylinder size and to cover all disk size.
*
* So we define different cylinder sizes depending on the disk size.
* The cylinder size is chosen so that we don't have too few cylinders
* for a small disk image, or so many on a big disk image that you
* waste space for backup superblocks or cylinder group structures.
* Also we must have a resonable number of cylinders and sectors so
* that newfs can run using default values.
*
* +-----------+--------+---------+--------+
* | disk_size | < 2MB | 2MB-4GB | >= 8GB |
* +-----------+--------+---------+--------+
* | nhead | 1 | 1 | 96 |
* | nsect | 200 | 600 | 768 |
* +-----------+--------+---------+--------+
*
* Other parameters are computed from these values:
*
* pcyl = disk_size / (nhead * nsect * 512)
* acyl = (pcyl > 2)? 2 : 0
* ncyl = pcyl - acyl
*
* The maximum number of cylinder is 65535 so this allows to define a
* geometry for a disk size up to 65535 * 96 * 768 * 512 = 2.24 TB
* which is more than enough to cover the maximum size allowed by the
* extended VTOC format (2TB).
*/
} else {
}
label->dkl_write_reinstruct = 0;
label->dkl_read_reinstruct = 0;
label->dkl_intrlv = 0;
/*
* We must have a correct label name otherwise format(1m) will
* not recognized the disk as labeled.
*/
"SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
/* default VTOC */
}
/*
* Function:
* vd_dskimg_set_vtoc
*
* Description:
* Set the vtoc of a disk image by writing the label and backup
* labels into the disk image backend.
*
* Parameters:
* vd - disk on which the operation is performed.
* label - the data to be written.
*
* Return Code:
* 0 - success.
* n > 0 - error, n indicates the errno code.
*/
static int
{
PR0("fail to write disk label");
return (EIO);
}
/*
* Backup labels are on the last alternate cylinder's
* first five odd sectors.
*/
PR0("no alternate cylinder, can not store backup labels");
return (0);
}
/*
* Write the backup labels. Make sure we don't try to write past
* the last cylinder.
*/
sec = 1;
PR0("not enough sector to store all backup labels");
return (0);
}
PR0("error writing backup label at block %lu\n",
return (EIO);
}
sec += 2;
}
return (0);
}
/*
* Function:
* vd_dskimg_get_devid_block
*
* Description:
* Return the block number where the device id is stored.
*
* Parameters:
* vd - disk on which the operation is performed.
* blkp - pointer to the block number
*
* Return Code:
* 0 - success
* ENOSPC - disk has no space to store a device id
*/
static int
{
/*
* If no label is defined we don't know where to find
* a device id.
*/
return (ENOSPC);
}
/*
* For an EFI disk, the devid is at the beginning of
* the reserved slice
*/
PR0("EFI disk has no reserved slice");
return (ENOSPC);
}
return (0);
}
/* this geometry doesn't allow us to have a devid */
PR0("not enough alternate cylinder available for devid "
return (ENOSPC);
}
/* the devid is in on the track next to the last cylinder */
return (0);
}
/*
* Return the checksum of a disk block containing an on-disk devid.
*/
static uint_t
{
int i;
chksum = 0;
for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
return (chksum);
}
/*
* Function:
* vd_dskimg_read_devid
*
* Description:
* Read the device id stored on a disk image.
*
* Parameters:
* vd - disk on which the operation is performed.
* devid - the return address of the device ID.
*
* Return Code:
* 0 - success
* EIO - I/O error while trying to access the disk image
* EINVAL - no valid device id was found
* ENOSPC - disk has no space to store a device id
*/
static int
{
return (status);
/* get the devid */
DEV_BSIZE)) < 0) {
goto done;
}
/* validate the revision */
goto done;
}
/* compute checksum */
/* compare the checksums */
goto done;
}
/* validate the device id */
goto done;
}
done:
return (status);
}
/*
* Function:
* vd_dskimg_write_devid
*
* Description:
* Write a device id into disk image.
*
* Parameters:
* vd - disk on which the operation is performed.
* devid - the device ID to store.
*
* Return Code:
* 0 - success
* EIO - I/O error while trying to access the disk image
* ENOSPC - disk has no space to store a device id
*/
static int
{
int status;
/* nothing to write */
return (0);
}
return (status);
/* set revision */
/* copy devid */
/* compute checksum */
/* set checksum */
/* store the devid */
} else {
status = 0;
}
return (status);
}
/*
* Function:
* vd_do_scsi_rdwr
*
* Description:
* Read or write to a SCSI disk using an absolute disk offset.
*
* Parameters:
* vd - disk on which the operation is performed.
* operation - operation to execute: read (VD_OP_BREAD) or
* write (VD_OP_BWRITE).
* data - buffer where data are read to or written from.
* blk - starting block for the operation.
* len - number of bytes to read or write.
*
* Return Code:
* 0 - success
* n != 0 - error.
*/
static int
{
int max_sectors;
return (EINVAL);
/*
* Build and execute the uscsi ioctl. We build a group0, group1
* or group4 command as necessary, since some targets
* do not support group1 commands.
*/
while (nblk) {
/*
* Some of the optical drives on sun4v machines are ATAPI
* to explicitly check a flag which is set when a domain
* is bound.
*/
} else if (blk > 0xffffffff) {
} else {
}
/*
* Set flags so that the command is isolated from normal
* commands and no error message is printed.
*/
if (operation == VD_OP_BREAD) {
} else {
}
if (status == 0)
if (status != 0)
break;
/*
* Check if partial DMA breakup is required. If so, reduce
* the request size by half and retry the last request.
*/
max_sectors >>= 1;
if (max_sectors <= 0) {
break;
}
continue;
}
if (ucmd.uscsi_resid != 0) {
break;
}
}
return (status);
}
/*
* Function:
* vd_scsi_rdwr
*
* Description:
* Wrapper function to read or write to a SCSI disk using an absolute
* disk offset. It checks the blocksize of the underlying device and,
* if necessary, adjusts the buffers accordingly before calling
* vd_do_scsi_rdwr() to do the actual read or write.
*
* Parameters:
* vd - disk on which the operation is performed.
* operation - operation to execute: read (VD_OP_BREAD) or
* write (VD_OP_BWRITE).
* data - buffer where data are read to or written from.
* blk - starting block for the operation.
* len - number of bytes to read or write.
*
* Return Code:
* 0 - success
* n != 0 - error.
*/
static int
{
int rv;
char *buf; /* buffer area to fit physical device's block size */
if (vd->block_size == 0) {
/*
* The block size was not available during the attach,
* try to update it now.
*/
if (vd_backend_check_size(vd) != 0)
return (EIO);
}
/*
* If the vdisk block size and the block size of the underlying device
* match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
* to create a buffer large enough to handle the device's block size
* and adjust the block to be read from and the amount of data to
* read to correspond with the device's block size.
*/
return (EINVAL);
/*
* Writing of physical block sizes larger than the virtual block size
* writing to DVDs is implemented.
*/
if (operation == VD_OP_BWRITE)
return (ENOTSUP);
/* BEGIN CSTYLED */
/*
* Below is a diagram showing the relationship between the physical
* and virtual blocks. If the virtual blocks marked by 'X' below are
* requested, then the physical blocks denoted by 'Y' are read.
*
* vblk
* | vlen
* |<--------------->|
* v v
* --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- virtual disk:
* | | | |XX|XX|XX|XX|XX|XX| | | | | | } block size is
* --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- vd->vdisk_block_size
* : : : :
* >:==:< delta : :
* : : : :
* --+-----+-----+-----+-----+-----+-----+-----+-- physical disk:
* | |YY:YY|YYYYY|YYYYY|YY:YY| | | } block size is
* --+-----+-----+-----+-----+-----+-----+-----+-- vd->block_size
* ^ ^
* |<--------------------->|
* | plen
* pblk
*/
/* END CSTYLED */
return (rv);
}
/*
* Function:
* vd_slice_flabel_read
*
* Description:
* This function simulates a read operation from the fake label of
* a single-slice disk.
*
* Parameters:
* vd - single-slice disk to read from
* data - buffer where data should be read to
* offset - offset in byte where the read should start
* length - number of bytes to read
*
* Return Code:
* n >= 0 - success, n indicates the number of bytes read
* -1 - error
*/
static ssize_t
{
size_t n = 0;
/* if offset is past the fake label limit there's nothing to read */
return (0);
/* data with offset 0 to flabel_size are read from flabel */
return (length);
}
data += n;
}
/* data with offset from flabel_size to flabel_limit are all zeros */
return (length);
}
}
/*
* Function:
* vd_slice_flabel_write
*
* Description:
* This function simulates a write operation to the fake label of
* a single-slice disk. Write operations are actually faked and return
* success although the label is never changed. This is mostly to
* simulate a successful label update.
*
* Parameters:
* vd - single-slice disk to write to
* data - buffer where data should be written from
* offset - offset in byte where the write should start
* length - number of bytes to written
*
* Return Code:
* n >= 0 - success, n indicates the number of bytes written
* -1 - error
*/
static ssize_t
{
return (0);
/*
* If this is a request to overwrite the VTOC disk label, check that
* the new label is similar to the previous one and return that the
* write was successful, but note that nothing is actually overwritten.
*/
/* check that this is a valid label */
return (-1);
/* check the vtoc and geometry */
return (length);
}
/* fail any other write */
return (-1);
}
/*
* Function:
* vd_slice_fake_rdwr
*
* Description:
* This function simulates a raw read or write operation to a single-slice
* beginning and to the end of the vdisk).
*
* The function returns 0 is the operation is completed and it has been
* entirely handled as a fake read or write. In that case, lengthp points
* to the number of bytes not read or written. Values returned by datap
* and blkp are undefined.
*
* If the fake operation has succeeded but the read or write is not
* we fake) then the function returns EAGAIN and datap, blkp and lengthp
* pointers points to the parameters for completing the operation.
*
* In case of an error, for example if the slice is empty or parameters
* are invalid, then the function returns a non-zero value different
* from EAGAIN. In that case, the returned values of datap, blkp and
* lengthp are undefined.
*
* Parameters:
* vd - single-slice disk on which the operation is performed
* slice - slice on which the operation is performed,
* VD_SLICE_NONE indicates that the operation
* is done using an absolute disk offset.
* operation - operation to execute: read (VD_OP_BREAD) or
* write (VD_OP_BWRITE).
* datap - pointer to the buffer where data are read to
* or written from. Return the pointer where remaining
* data have to be read to or written from.
* blkp - pointer to the starting block for the operation.
* Return the starting block relative to the vdisk
* backend for the remaining operation.
* lengthp - pointer to the number of bytes to read or write.
* This should be a multiple of DEV_BSIZE. Return the
* remaining number of bytes to read or write.
*
* Return Code:
* other values - error
*/
static int
{
ssize_t n;
/*
* If this is not a raw I/O or an I/O from a full disk slice then
*/
if (slice != VD_SLICE_NONE &&
(slice != VD_ENTIRE_DISK_SLICE ||
(slice != VD_EFI_WD_SLICE ||
return (EIO);
}
return (EINVAL);
/* handle any I/O with the fake label */
if (operation == VD_OP_BWRITE)
else
if (n == -1)
return (EINVAL);
/* adjust I/O arguments */
data += n;
length -= n;
/* check if there's something else to process */
if (length == 0) {
status = 0;
goto done;
}
slice == VD_ENTIRE_DISK_SLICE) {
goto done;
}
} else {
}
/* if we have reached the last block then the I/O is completed */
status = 0;
goto done;
}
/* if we are past the last block then return an error */
return (EIO);
/* check if there is any I/O to end of the disk */
goto done;
}
/* we don't allow any write to the end of the disk */
if (operation == VD_OP_BWRITE)
return (EIO);
}
}
if (operation == VD_OP_BREAD) {
/* check if we read backup labels */
sizeof (struct dk_label));
}
}
}
}
done:
/*
* Return the parameters for the remaining I/O. The starting block is
* adjusted so that it is relative to the vdisk backend.
*/
return (status);
}
/*
* We define our own biodone function so that buffers used for
* asynchronous writes are not released when biodone() is called.
*/
static int
{
return (0);
}
/*
* Return Values
* EINPROGRESS - operation was successfully started
* EIO - encountered LDC (aka. task error)
* 0 - operation completed successfully
*
* Side Effect
* sets request->status = <disk operation status>
*/
static int
{
int slice;
char *bufaddr = 0;
/* no service for trivial requests */
return (0);
}
PR1("%s %lu bytes at block %lu",
/*
* We have to check the open flags because the functions processing
*/
PR0("write fails because backend is opened read-only");
return (0);
}
/* Map memory exported by client */
if (status != 0) {
return (EIO);
}
/*
* The buffer size has to be 8-byte aligned, so the client should have
* sent a buffer which size is roundup to the next 8-byte aligned value.
*/
if (status != 0) {
return (EIO);
}
/* default number of byte returned by the I/O */
if (slice != 0) {
/* handle any fake I/O */
/* record the number of bytes from the fake I/O */
if (rv == 0) {
goto io_done;
}
goto io_done;
}
/*
* If we return with EAGAIN then this means that there
* are still data to read or write.
*/
/*
* We need to continue the I/O from the slice backend to
* complete the request. The variables bufaddr, offset
* and length have been adjusted to have the right
* information to do the remaining I/O from the backend.
* The backend is entirely mapped to slice 0 so we just
* have to complete the I/O from that slice.
*/
slice = 0;
}
if (rv != 0) {
goto io_done;
}
slice = 0;
/*
* This is not a disk image so it is a real disk. We
* assume that the underlying device driver supports
* USCSICMD ioctls. This is the case of all SCSI devices
* (sd, ssd...).
*
* In the future if we have non-SCSI disks we would need
* to invoke the appropriate function to do I/O using an
* absolute disk offset (for example using DIOCTL_RWCMD
* for IDE disks).
*/
length);
if (rv != 0) {
} else {
}
goto io_done;
}
/* Start the block I/O */
if (rv < 0) {
} else {
}
} else {
} else {
/*
* If we have a ZFS volume then we do an
* asynchronous write and we will wait for the
* completion of the write in vd_complete_bio()
* using the DKIOCFLUSHWRITECACHE ioctl. We
* do so for performance reason because, for a
* synchronous write, the ZFS volume strategy()
* function would only return after the write is
* commited and this prevents starting multiple
* writes in parallel.
*/
else
}
/*
* This is to indicate to the caller that the request
* needs to be finished by vd_complete_bio() by calling
* biowait() there and waiting for that to return before
* triggering the notification of the vDisk client.
*
* This is necessary when writing to real disks as
* otherwise calls to ldi_strategy() would be serialized
* behind the calls to biowait() and performance would
* suffer.
*/
return (EINPROGRESS);
}
/* Clean up after error or completion */
if (rv) {
}
if (rv) {
}
return (status);
}
/*
* This function should only be called from vd_notify to ensure that requests
* are responded to in the order that they are received.
*/
static int
{
int status;
do {
if (status != EWOULDBLOCK)
break;
} while (status == EWOULDBLOCK);
if (status != 0) {
if (status != ECONNRESET)
return (status);
PR0("ldc_write() performed only partial write");
return (EIO);
}
return (0);
}
static void
{
}
/*
* Reset the state of the connection with a client, if needed; reset the LDC
* transport as well, if needed. This function should only be called from the
* "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
*/
static void
{
int status = 0;
if (!vd->reset_state) {
return;
}
/*
* Let any asynchronous I/O complete before possibly pulling the rug
* out from under it; defer checking vd->reset_ldc, as one of the
* asynchronous tasks might set it
*/
if (status) {
}
}
/* Free the staging buffer for msgs */
}
/* Free the inband message buffer */
}
PR0("taking down LDC channel");
/* Reset exclusive access rights */
/* Allocate the staging buffer */
PR0("calling ldc_up\n");
}
static void vd_recv_msg(void *arg);
static void
{
int status;
PR0("vd_mark_in_reset: marking vd in reset\n");
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
return;
}
}
static int
{
int status;
if (vd->reset_state)
return (0);
/* Acquire the element */
if (status == ECONNRESET) {
return (0);
} else {
return (status);
}
}
/* Set the element's status and mark it done */
if (accepted) {
} else {
/* Perhaps client timed out waiting for I/O... */
}
/* Release the element */
if (status == ECONNRESET) {
return (0);
} else {
PR0("VIO_DRING_RELEASE() returned errno %d",
status);
return (status);
}
}
}
/*
* Return Values
* 0 - operation completed successfully
* EIO - encountered LDC / task error
*
* Side Effect
* sets request->status = <disk operation status>
*/
static int
{
int status = 0;
int rv = 0;
int rval;
/*
* For a ZFS volume, we use asynchronous writes so we have to
* ensure that writes have been commited before marking the
* I/O as completed.
*/
} else {
/* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */
}
/* Release the buffer */
if (!vd->reset_state)
if (status) {
PR0("ldc_mem_release() returned errno %d copying to "
"client", status);
if (status == ECONNRESET) {
}
}
/* Unmap the memory, even if in reset */
if (status) {
PR0("ldc_mem_unmap() returned errno %d copying to client",
status);
if (status == ECONNRESET) {
}
}
return (rv);
}
/*
* Description:
* This function is called by the two functions called by a taskq
* [ vd_complete_notify() and vd_serial_notify()) ] to send the
* message to the client.
*
* Parameters:
* arg - opaque pointer to structure containing task to be completed
*
* Return Values
* None
*/
static void
{
int status;
/*
* Send the "ack" or "nack" back to the client; if sending the message
* via LDC fails, arrange to reset both the connection state and LDC
* itself
*/
PR2("Sending %s",
switch (status) {
case 0:
break;
case ECONNRESET:
break;
default:
PR0("initiating full reset");
break;
}
}
/*
* Description:
* the vDisk client
*
* Parameters:
* task - structure containing the request sent from client
*
* Return Values
* None
*/
static void
{
int status = 0;
/* Update the dring element for a dring client */
if (status == ECONNRESET)
}
/*
* If a transport error occurred while marking the element done or
* previously while executing the task, arrange to "nack" the message
* when the final task in the descriptor element range completes
*/
/*
* Only the final task for a range of elements will respond to and
* free the message
*/
return;
}
/*
* reset as, depending on how we reset, the dring may have been
* there.
*/
if (!vd->reset_state)
}
/*
* Description:
* This is the basic completion function called to handle inband data
* requests and handshake messages. All it needs to do is trigger a
* message to the client that the request is completed.
*
* Parameters:
* arg - opaque pointer to structure containing task to be completed
*
* Return Values
* None
*/
static void
vd_serial_notify(void *arg)
{
}
/* ARGSUSED */
static int
{
return (0);
}
/* ARGSUSED */
static int
{
return (0);
}
static void
{
}
static void
{
}
static int
{
return (EINVAL);
return (0);
}
static void
{
int len;
}
static int
{
return (EINVAL);
return (0);
}
static void
{
}
static int
{
/* check buffer size */
if (vd_buf_len < vd_scsi_len)
return (EINVAL);
/* set flags */
}
/* task attribute */
switch (vd_scsi->task_attribute) {
case VD_SCSI_TASK_ACA:
break;
case VD_SCSI_TASK_HQUEUE:
break;
case VD_SCSI_TASK_ORDERED:
break;
default:
break;
}
/* timeout */
/* cdb data */
/* sense buffer */
}
return (EINVAL);
}
/* request data-in */
if (vd_scsi->datain_len != 0) {
}
/* request data-out */
if (vd_scsi->dataout_len != 0) {
}
return (0);
}
static void
{
/* output fields */
/* sense data */
else
} else {
}
vd_scsi->dataout_len = 0;
vd_scsi->datain_len = 0;
return;
}
/* request data (read) */
vd_scsi->dataout_len = 0;
} else {
/* request data (write) */
vd_scsi->datain_len = 0;
}
}
static ushort_t
{
int count;
sum = 0;
while (count--) {
}
return (sum);
}
/*
* Copy information from a vtoc and dk_geom structures to a dk_label structure.
*/
static void
{
int i;
for (i = 0; i < V_NUMPAR; i++) {
}
/*
* The bootinfo array can not be copied with bcopy() because
* elements are of type long in vtoc (so 64-bit) and of type
* int in dk_vtoc (so 32-bit).
*/
/* re-compute checksum */
}
/*
* Copy information from a dk_label structure to a vtoc and dk_geom structures.
*/
static void
{
int i;
}
/*
* The bootinfo array can not be copied with bcopy() because
* elements are of type long in vtoc (so 64-bit) and of type
* int in dk_vtoc (so 32-bit).
*/
}
/*
* Check if a geometry is valid for a single-slice disk. A geometry is
* considered valid if the main attributes of the geometry match with the
* attributes of the fake geometry we have created.
*/
static boolean_t
{
return (B_FALSE);
return (B_TRUE);
}
/*
* Check if a vtoc is valid for a single-slice disk. A vtoc is considered
* valid if the main attributes of the vtoc match with the attributes of the
* fake vtoc we have created.
*/
static boolean_t
{
int i;
return (B_FALSE);
/* slice 2 should be unchanged */
return (B_FALSE);
/*
* Slice 0 should be mostly unchanged and cover most of the disk.
* However we allow some flexibility wrt to the start and the size
* of this slice mainly because we can't exactly know how it will
* be defined by the OS installer.
*
* We allow slice 0 to be defined as starting on any of the first
* 4 cylinders.
*/
return (B_FALSE);
return (B_FALSE);
/* any other slice should have a size of 0 */
if (i != VD_ENTIRE_DISK_SLICE &&
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Handle ioctls to a disk slice.
*
* Return Values
* 0 - Indicates that there are no errors in disk operations
* ENOTSUP - Unknown disk label type or unsupported DKIO ioctl
* EINVAL - Not enough room to copy the EFI label
*
*/
static int
{
int rval;
if (cmd == DKIOCFLUSHWRITECACHE) {
} else {
}
}
switch (vd->vdisk_label) {
/* ioctls for a single slice disk with a VTOC label */
case VD_DISK_LABEL_VTOC:
switch (cmd) {
case DKIOCGGEOM:
return (0);
case DKIOCGEXTVTOC:
return (0);
case DKIOCSGEOM:
return (ENOTSUP);
/* fake success only if new geometry is valid */
return (EINVAL);
return (0);
case DKIOCSEXTVTOC:
return (ENOTSUP);
/* fake sucess only if the new vtoc is valid */
return (EINVAL);
return (0);
default:
return (ENOTSUP);
}
/* ioctls for a single slice disk with an EFI label */
case VD_DISK_LABEL_EFI:
return (ENOTSUP);
return (EINVAL);
switch (cmd) {
case DKIOCGETEFI:
return (0);
case DKIOCSETEFI:
return (ENOTSUP);
/* we currently don't support writing EFI */
return (EIO);
}
default:
/* Unknown disk label type */
return (ENOTSUP);
}
}
static int
{
int status;
return (status);
}
static void
{
}
static int
{
return (status);
for (i = 0; i < nparts && i < VD_MAXPART; i++) {
if (gpe[i].efi_gpe_StartingLBA == 0 ||
gpe[i].efi_gpe_EndingLBA == 0) {
continue;
}
sizeof (struct uuid)) == 0)
vd->efi_reserved = i;
}
return (status);
}
/*
* Function:
* vd_dskimg_validate_geometry
*
* Description:
* Read the label and validate the geometry of a disk image. The driver
* label, vtoc and geometry information are updated according to the
* label read from the disk image.
*
* If no valid label is found, the label is set to unknown and the
* function returns EINVAL, but a default vtoc and geometry are provided
* to the driver. If an EFI label is found, ENOTSUP is returned.
*
* Parameters:
* vd - disk on which the operation is performed.
*
* Return Code:
* 0 - success.
* EIO - error reading the label from the disk image.
* EINVAL - unknown disk label.
* ENOTSUP - geometry not applicable (EFI label).
*/
static int
{
int i;
int status = 0;
return (EIO);
if (vd_dskimg_validate_efi(vd) == 0) {
return (ENOTSUP);
}
} else {
}
/* Update the driver geometry and vtoc */
/* Update logical partitions */
}
}
return (status);
}
/*
* Handle ioctls to a disk image.
*
* Return Values
* 0 - Indicates that there are no errors
* != 0 - Disk operation returned an error
*/
static int
{
switch (cmd) {
case DKIOCGGEOM:
return (rc);
return (0);
case DKIOCGEXTVTOC:
return (rc);
return (0);
case DKIOCSGEOM:
return (EINVAL);
/*
* The current device geometry is not updated, just the driver
* "notion" of it. The device geometry will be effectively
* updated when a label is written to the device during a next
* DKIOCSEXTVTOC.
*/
return (0);
case DKIOCSEXTVTOC:
return (EINVAL);
/* write label to the disk image */
return (rc);
break;
case DKIOCFLUSHWRITECACHE:
else
case DKIOCGETEFI:
return (EIO);
return (0);
case DKIOCSETEFI:
return (EIO);
break;
default:
return (ENOTSUP);
}
/* label has changed, revalidate the geometry */
(void) vd_dskimg_validate_geometry(vd);
/*
* The disk geometry may have changed, so we need to write
* the devid (if there is one) so that it is stored at the
* right location.
*/
PR0("Fail to write devid");
}
return (0);
}
static int
{
/*
* Call the appropriate function to execute the ioctl depending
* on the type of vdisk.
*/
/* slice, file or volume exported as a single slice disk */
/* file or volume exported as a full disk */
} else {
/* disk device exported as a full disk */
/*
* By default VTOC ioctls are done using ioctls for the
* extended VTOC. Some drivers (in particular non-Sun drivers)
* may not support these ioctls. In that case, we fallback to
* the regular VTOC ioctls.
*/
switch (cmd) {
case DKIOCGEXTVTOC:
cmd = DKIOCGVTOC;
break;
case DKIOCSEXTVTOC:
cmd = DKIOCSVTOC;
vtoc);
break;
}
}
}
#ifdef DEBUG
if (rval != 0) {
PR0("ioctl %x set rval = %d, which is not being returned"
}
#endif /* DEBUG */
return (status);
}
/*
* Description:
* This is the function that processes the ioctl requests (farming it
* out to functions that handle slices, files or whole disks)
*
* Return Values
* 0 - ioctl operation completed successfully
* != 0 - The LDC error value encountered
* (propagated back up the call stack as a task error)
*
* Side Effect
* sets request->status to the return value of the ioctl function.
*/
static int
{
int status = 0;
/* Get data from client and convert, if necessary */
PR1("Getting \"arg\" data from client");
LDC_COPY_IN)) != 0) {
PR0("ldc_mem_copy() returned errno %d "
"copying from client", status);
return (status);
}
/* Convert client's data, if necessary */
/* use client buffer */
} else {
/* convert client vdisk operation data to ioctl data */
if (status != 0) {
return (0);
}
}
}
/* check write permission */
PR0("uscsi fails because backend is opened read-only");
return (0);
}
}
/*
* Send the ioctl to the disk backend.
*/
/*
* USCSICMD has reported an error and the uscsi_status
* field is not zero. This means that the SCSI command
* has completed but it has an error. So we should
* mark the VD operation has succesfully completed
* and clients can check the SCSI status field for
* SCSI errors.
*/
else
return (0);
}
/* Convert data and send to client, if necessary */
PR1("Sending \"arg\" data to client");
/* Convert ioctl data to vdisk operation data, if necessary */
LDC_COPY_OUT)) != 0) {
PR0("ldc_mem_copy() returned errno %d "
"copying to client", status);
return (status);
}
}
return (status);
}
/*
* Description:
* This generic function is called by the task queue to complete
* the processing of the tasks. The specific completion function
* is passed in as a field in the task pointer.
*
* Parameters:
* arg - opaque pointer to structure containing task to be completed
*
* Return Values
* None
*/
static void
vd_complete(void *arg)
{
/* Now notify the vDisk client */
}
static int
{
int i, status;
vd_ioctl_t ioctl[] = {
/* Command (no-copy) operations */
/* "Get" (copy-out) operations */
/* "Set" (copy-in) operations */
};
/*
* Determine ioctl corresponding to caller's "operation" and
* validate caller's "nbytes"
*/
for (i = 0; i < nioctls; i++) {
/* LDC memory operations require 8-byte multiples */
break;
PR0("%s: Expected at least nbytes = %lu, "
return (EINVAL);
}
PR0("%s: Expected nbytes = %lu, got %lu",
return (EINVAL);
}
break;
}
}
PR0("%s fails because backend is opened read-only",
ioctl[i].operation_name);
return (0);
}
return (status);
}
static int
{
int bufbytes;
/*
* We don't support devid for single-slice disks because we
* have no space to store a fabricated devid and for physical
* disk slices, we can't use the devid of the disk otherwise
* exporting multiple slices from the same disk will produce
* the same devids.
*/
PR2("No Device ID for slices");
return (0);
}
PR2("No Device ID");
return (0);
} else {
}
} else {
PR2("No Device ID");
return (0);
}
}
/*
* Save the buffer size here for use in deallocation.
* The actual number of bytes copied is returned in
* the 'nbytes' field of the request structure.
*/
/* LDC memory operations require 8-byte multiples */
LDC_COPY_OUT)) != 0) {
PR0("ldc_mem_copy() returned errno %d copying to client",
status);
}
return (status);
}
static int
{
return (status);
}
static int
{
PR0("Performing VD_OP_RESET");
PR0("VD_OP_RESET: Expected nbytes = 0, got %lu",
return (EINVAL);
}
return (0);
}
static int
{
int rv;
vd_capacity_t vd_cap = { 0 };
PR0("Performing VD_OP_GET_CAPACITY");
PR0("VD_OP_GET_CAPACITY: Expected nbytes = %lu, got %lu",
return (EINVAL);
}
/*
* Check the backend size in case it has changed. If the check fails
* then we will return the last known size.
*/
(void) vd_backend_check_size(vd);
return (rv);
}
return (0);
}
static int
{
PR0("Performing VD_OP_GET_ACCESS");
PR0("VD_OP_GET_ACCESS: Expected nbytes = %lu, got %lu",
return (EINVAL);
}
return (0);
return (rv);
}
return (0);
}
static int
{
PR0("VD_OP_SET_ACCESS: Expected nbytes = %lu, got %lu",
return (EINVAL);
}
return (rv);
}
if (flags == VD_ACCESS_SET_CLEAR) {
PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
&rval);
return (0);
}
/*
* As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
* when the EXCLUSIVE flag is set.
*/
if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
return (0);
}
/*
* Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
* acquire exclusive access rights, preserve them and we
* can use preemption. So we can use the MHIOCTKNOWN ioctl.
*/
PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
break;
case VD_ACCESS_SET_PRESERVE:
/*
* Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
* access rights and preserve them, but not preempt any other
* host. So we need to use the MHIOCTKOWN ioctl to enable the
* "preserve" feature but we can not called it directly
* because it uses preemption. So before that, we use the
* MHIOCQRESERVE ioctl to ensure we can get exclusive rights
* without preempting anyone.
*/
PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
&rval);
break;
break;
case VD_ACCESS_SET_PREEMPT:
/*
* Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
* access rights and we can use preemption. So we try to do
* a SCSI reservation, if it fails we reset the disk to clear
* any reservation and we try to reserve again.
*/
PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
&rval);
break;
/* reset the disk */
(void) vd_scsi_reset(vd);
/* try again even if the reset has failed */
&rval);
break;
case 0:
/* Flag EXCLUSIVE only. Just issue a SCSI reservation */
PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
&rval);
break;
}
else
return (0);
}
static void
{
return;
PR0("Releasing disk ownership");
/*
* An EACCES failure means that there is a reservation conflict,
* so we are not the owner of the disk anymore.
*/
return;
}
/*
* We have failed to release the ownership, try to reset the disk
* to release reservations.
*/
PR0("Resetting disk");
if (status != 0)
/* whatever the result of the reset is, we try the release again */
return;
}
/*
* At this point we have done our best to try to reset the
* access rights to the disk and we don't know if we still
* own a reservation and if any mechanism to preserve the
* ownership is still in place. The ultimate solution would
* be to reset the system but this is usually not what we
* want to happen.
*/
if (vd_reset_access_failure == A_REBOOT) {
} else if (vd_reset_access_failure == A_DUMP) {
}
}
/*
* Define the supported operations once the functions for performing them have
* been defined
*/
static const vds_operation_t vds_operation[] = {
#undef X
};
static const size_t vds_noperations =
(sizeof (vds_operation))/(sizeof (vds_operation[0]));
/*
* Process a task specifying a client I/O request
*
* Parameters:
* task - structure containing the request sent from client
*
* Return Value
* 0 - success
* ENOTSUP - Unknown/Unsupported VD_OP_XXX operation
* EINVAL - Invalid disk slice
* != 0 - some other non-zero return value from start function
*/
static int
{
int i;
/* Find the requested operation */
for (i = 0; i < vds_noperations; i++) {
/* all operations should have a start func */
break;
}
}
/*
* We need to check that the requested operation is permitted
* for the particular client that sent it or that the loop above
* did not complete without finding the operation type (indicating
* that the requested operation is unknown/unimplemented)
*/
(i == vds_noperations)) {
return (0);
}
/* Range-check slice */
PR0("Invalid \"slice\" %u (max %u) for virtual disk",
return (0);
}
/*
* Call the function pointer that starts the operation.
*/
}
/*
* Description:
* This function is called by both the in-band and descriptor ring
* message processing functions paths to actually execute the task
* requested by the vDisk client. It in turn calls its worker
* function, vd_do_process_task(), to carry our the request.
*
* Any transport errors (e.g. LDC errors, vDisk protocol errors) are
* saved in the 'status' field of the task and are propagated back
* up the call stack to trigger a NACK
*
* Any request errors (e.g. ENOTTY from an ioctl) are saved in
* the 'status' field of the request and result in an ACK being sent
* by the completion handler.
*
* Parameters:
* task - structure containing the request sent from client
*
* Return Value
* 0 - successful synchronous request.
* != 0 - transport error (e.g. LDC errors, vDisk protocol)
* EINPROGRESS - task will be finished in a completion handler
*/
static int
{
int status;
/*
* If the task processing function returned EINPROGRESS indicating
* that the task needs completing then schedule a taskq entry to
* finish it now.
*
* Otherwise the task processing function returned either zero
* indicating that the task was finished in the start function (and we
* don't need to wait in a completion function) or the start function
* returned an error - in both cases all that needs to happen is the
* notification to the vDisk client higher up the call stack.
* If the task was using a Descriptor Ring, we need to mark it as done
* at this stage.
*/
/* Queue a task to complete the operation */
/* Update the dring element if it's a dring client */
if (status == ECONNRESET)
}
}
/*
* Return true if the "type", "subtype", and "env" fields of the "tag" first
* argument match the corresponding remaining arguments; otherwise, return false
*/
{
}
/*
* by this server.
*/
static boolean_t
{
for (int i = 0; i < vds_num_versions; i++) {
ASSERT((i == 0) ||
/*
* If the major versions match, adjust the minor version, if
* necessary, down to the highest value supported by this
* server and return true so this message will get "ack"ed;
* the client should also support all minor versions lower
* than the value it sent
*/
PR0("Adjusting minor version from %u to %u",
}
return (B_TRUE);
}
/*
* If the message contains a higher major version number, set
* and return false, so this message will get "nack"ed with
* these values, and the client will potentially try again
* with the same or a lower version
*/
return (B_FALSE);
}
/*
* Otherwise, the message's major version is less than the
* current major version, so continue the loop to the next
* (lower) supported version
*/
}
/*
* No common version was found; "ground" the version pair in the
* message to terminate negotiation
*/
return (B_FALSE);
}
/*
* Process a version message from a client. vds expects to receive version
* messages from clients seeking service, but never issues version messages
* itself; therefore, vds can ACK or NACK client version messages, but does
* not expect to receive version-message ACKs or NACKs (and will treat such
* messages as invalid).
*/
static int
{
VIO_VER_INFO)) {
return (ENOMSG); /* not a version message */
}
PR0("Expected %lu-byte version message; "
return (EBADMSG);
}
PR0("Expected device class %u (disk); received %u",
return (EBADMSG);
}
/*
* We're talking to the expected kind of client; set our device class
*/
/*
* Check whether the (valid) version message specifies a version
* supported by this server. If the version is not supported, return
* EBADMSG so the message will get "nack"ed; vds_supported_version()
* will have updated the message with a supported version for the
* client to consider
*/
if (!vds_supported_version(ver_msg))
return (EBADMSG);
/*
* A version has been agreed upon; use the client's SID for
* communication on this channel now
*/
/*
* Store the negotiated major and minor version values in the "vd" data
* structure so that we can check if certain operations are supported
* by the client.
*/
PR0("Using major version %u, minor version %u",
return (0);
}
static void
{
/*
* We need to check from the highest version supported to the
* lowest because versions with a higher minor number implicitly
* support versions with a lower minor number.
*/
/*
* can't write to ISO images, make sure that write
* support is not set in case administrator did not
* use "options=ro" when doing an ldm add-vdsdev
*/
}
}
/* we should have already agreed on a version */
}
static int
{
VIO_ATTR_INFO)) {
PR0("Message is not an attribute message");
return (ENOMSG);
}
PR0("Expected %lu-byte attribute message; "
return (EBADMSG);
}
if (attr_msg->max_xfer_sz == 0) {
PR0("Received maximum transfer size of 0 from client");
return (EBADMSG);
}
PR0("Client requested unsupported transfer mode");
return (EBADMSG);
}
/*
* check if the underlying disk is ready, if not try accessing
* the device again. Open the vdisk device and extract info
* about it, as this is needed to respond to the attr info msg
*/
do {
break;
/* incremental delay */
/* if vdisk is no longer enabled - return error */
if (!vd_enabled(vd))
return (ENXIO);
if (status)
return (ENXIO);
PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
}
/* Success: valid message and transfer mode */
/*
* The vd_dring_inband_msg_t contains one cookie; need room
* for up to n-1 more cookies, where "n" is the number of full
* pages plus possibly one partial page required to cover
* "max_xfer_sz". Add room for one more cookie if
* "max_xfer_sz" isn't an integral multiple of the page size.
* Must first get the maximum transfer size in bytes.
*/
sizeof (vd_dring_inband_msg_t) +
(sizeof (ldc_mem_cookie_t)));
/*
* Set the maximum expected message length to
* accommodate in-band-descriptor messages with all
* their cookies
*/
/*
* Initialize the data structure for processing in-band I/O
* request descriptors
*/
}
/* Return the device's block size and max transfer size to the client */
/* Discover and save the list of supported VD_OP_XXX operations */
return (0);
}
static int
{
int status;
VIO_DRING_REG)) {
PR0("Message is not a register-dring message");
return (ENOMSG);
}
PR0("Expected at least %lu-byte register-dring message; "
return (EBADMSG);
}
PR0("Expected %lu-byte register-dring message; "
return (EBADMSG);
}
PR0("A dring was previously registered; only support one");
return (EBADMSG);
}
PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
return (EBADMSG);
}
/*
* In addition to fixing the assertion in the success case
* below, supporting drings which require more than one
* "cookie" requires increasing the value of vd->max_msglen
* somewhere in the code path prior to receiving the message
* which results in calling this function. Note that without
* making this change, the larger message size required to
* accommodate multiple cookies cannot be successfully
* received, so this function will not even get called.
* Gracefully accommodating more dring cookies might
* reasonably demand exchanging an additional attribute or
* making a minor protocol adjustment
*/
return (EBADMSG);
}
else
if (status != 0) {
return (status);
}
/*
* To remove the need for this assertion, must call
* ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
* successful call to ldc_mem_dring_map()
*/
if ((status =
return (status);
}
PR0("Descriptor ring virtual address is NULL");
return (ENXIO);
}
/* Initialize for valid message and mapped dring */
PR1("descriptor size = %u, dring length = %u",
/*
* Allocate and initialize a "shadow" array of data structures for
* tasks to process I/O requests in dring elements
*/
vd->dring_task =
if (status) {
return (ENXIO);
}
/*
* The descriptor payload varies in length. Calculate its
* size by subtracting the header size from the total
* descriptor size.
*/
sizeof (vio_dring_entry_hdr_t)), KM_SLEEP);
}
return (0);
}
static int
{
VIO_DRING_UNREG)) {
PR0("Message is not an unregister-dring message");
return (ENOMSG);
}
PR0("Expected %lu-byte unregister-dring message; "
return (EBADMSG);
}
PR0("Expected dring ident %lu; received %lu",
return (EBADMSG);
}
return (0);
}
static int
{
PR0("Message is not an RDX message");
return (ENOMSG);
}
if (msglen != sizeof (vio_rdx_msg_t)) {
PR0("Expected %lu-byte RDX message; received %lu bytes",
sizeof (vio_rdx_msg_t), msglen);
return (EBADMSG);
}
PR0("Valid RDX message");
return (0);
}
static int
{
PR0("Received seq_num %lu; expected %lu",
PR0("initiating soft reset");
return (1);
}
return (0);
}
/*
* Return the expected size of an inband-descriptor message with all the
* cookies it claims to include
*/
static size_t
{
return ((sizeof (*msg)) +
}
/*
* Process an in-band descriptor message: used with clients like OBP, with
* which vds exchanges descriptors within VIO message payloads, rather than
* operating on them within a descriptor ring
*/
static int
{
VIO_DESC_DATA)) {
PR1("Message is not an in-band-descriptor message");
return (ENOMSG);
}
PR0("Expected at least %lu-byte descriptor message; "
return (EBADMSG);
}
PR0("Expected %lu-byte descriptor message; "
return (EBADMSG);
}
return (EBADMSG);
/*
* Valid message: Set up the in-band descriptor task and process the
* request. Arrange to acknowledge the client's message, unless an
* error processing the descriptor task results in setting
* VIO_SUBTYPE_NACK
*/
PR1("Valid in-band-descriptor message");
/*
* The task request is now the payload of the message
* that was just copied into the body of the task.
*/
}
static int
{
int status;
/* Accept the updated dring element */
return (status);
}
if (ready) {
} else {
}
return (status);
}
if (!ready)
return (EBUSY);
/* Initialize a task and process the accepted element */
/* duplicate msg buf for cookies etc. */
}
static int
{
/*
* Arrange to acknowledge the client's message, unless an error
* processing one of the dring elements results in setting
* VIO_SUBTYPE_NACK
*/
/*
* Process the dring elements in the range
*/
if (status == EINPROGRESS)
inprogress = B_TRUE;
else if (status != 0)
break;
}
/*
* If some, but not all, operations of a multi-element range are in
* progress, wait for other operations to complete before returning
* (which will result in "ack" or "nack" of the message). Note that
* all outstanding operations will need to complete, not just the ones
* corresponding to the current range of dring elements; howevever, as
* this situation is an error case, performance is less critical.
*/
return (status);
}
static int
{
VIO_DRING_DATA)) {
PR1("Message is not a dring-data message");
return (ENOMSG);
}
PR0("Expected %lu-byte dring message; received %lu bytes",
return (EBADMSG);
}
return (EBADMSG);
PR0("Expected dring ident %lu; received ident %lu",
return (EBADMSG);
}
PR0("\"start_idx\" = %u; must be less than %u",
return (EBADMSG);
}
PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
return (EBADMSG);
}
/* Valid message; process range of updated dring elements */
PR1("Processing descriptor range, start = %u, end = %u",
}
static int
{
retry++) {
}
if (status) {
if (status != ECONNRESET)
return (ENOMSG);
return (status);
} else if (*nbytes == 0) {
PR1("ldc_read() returned 0 and no message read");
return (ENOMSG);
}
return (0);
}
static int
{
int status;
#ifdef DEBUG
#endif
/*
* Validate session ID up front, since it applies to all messages
* once set
*/
return (EBADMSG);
}
/*
* Process the received message based on connection state
*/
case VD_STATE_INIT: /* expect version message */
return (status);
/* Version negotiated, move to that state */
return (0);
case VD_STATE_VER: /* expect attribute message */
return (status);
/* Attributes exchanged, move to that state */
return (0);
case VD_STATE_ATTR:
case VIO_DESC_MODE: /* expect RDX message */
return (status);
/* Ready to receive in-band descriptors */
return (0);
case VIO_DRING_MODE_V1_0: /* expect register-dring message */
if ((status =
return (status);
/* One dring negotiated, move to that state */
return (0);
default:
ASSERT("Unsupported transfer mode");
PR0("Unsupported transfer mode");
return (ENOTSUP);
}
case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */
/* Ready to receive data */
return (0);
return (status);
}
/*
* If another register-dring message is received, stay in
* dring state in case the client sends RDX; although the
* protocol allows multiple drings, this server does not
* support using more than one
*/
if ((status =
return (status);
/*
* Acknowledge an unregister-dring message, but reset the
* connection anyway: Although the protocol allows
* unregistering drings, this server cannot serve a vdisk
* without its only dring
*/
case VD_STATE_DATA:
case VIO_DESC_MODE: /* expect in-band-descriptor message */
case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
/*
* Typically expect dring-data messages, so handle
* them first
*/
return (status);
/*
* Acknowledge an unregister-dring message, but reset
* the connection anyway: Although the protocol
* allows unregistering drings, this server cannot
* serve a vdisk without its only dring
*/
default:
ASSERT("Unsupported transfer mode");
PR0("Unsupported transfer mode");
return (ENOTSUP);
}
default:
ASSERT("Invalid client connection state");
PR0("Invalid client connection state");
return (ENOTSUP);
}
}
static int
{
int status;
/*
* Check that the message is at least big enough for a "tag", so that
* message processing can proceed based on tag-specified message type
*/
if (msglen < sizeof (vio_msg_tag_t)) {
/* Can't "nack" short message, so drop the big hammer */
PR0("initiating full reset");
return (EBADMSG);
}
/*
* Process the message
*/
case 0:
/* "ack" valid, successfully-processed messages */
break;
case EINPROGRESS:
/* The completion handler will "ack" or "nack" the message */
return (EINPROGRESS);
case ENOMSG:
PR0("Received unexpected message");
case EBADMSG:
case ENOTSUP:
/* "transport" error will cause NACK of invalid messages */
break;
default:
/* "transport" error will cause NACK of invalid messages */
/* An LDC error probably occurred, so try resetting it */
break;
}
/* populate the task so we can dispatch it on the taskq */
/*
* Queue a task to send the notification that the operation completed.
* We need to ensure that requests are responded to in the correct
* order and since the taskq is processed serially this ordering
* is maintained.
*/
/*
* To ensure handshake negotiations do not happen out of order, such
* requests that come through this path should not be done in parallel
* so we need to wait here until the response is sent to the client.
*/
/* Arrange to reset the connection for nack'ed or failed messages */
PR0("initiating %s reset",
}
return (status);
}
static boolean_t
{
return (enabled);
}
static void
vd_recv_msg(void *arg)
{
PR2("New task to receive incoming message(s)");
/*
* Receive and process a message
*/
/*
* check if channel is UP - else break out of loop
*/
PR0("channel not up (status=%d), exiting recv loop\n",
lstatus);
break;
}
switch (status) {
case 0:
/* check if max_msglen changed */
PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
}
if (rv == EINPROGRESS)
continue;
break;
case ENOMSG:
break;
case ECONNRESET:
PR0("initiating soft reset (ECONNRESET)\n");
status = 0;
break;
default:
/* Probably an LDC failure; arrange to reset it */
break;
}
}
PR2("Task finished");
}
static uint_t
{
int status;
if (!vd_enabled(vd))
return (LDC_SUCCESS);
if (event & LDC_EVT_DOWN) {
PR0("LDC_EVT_DOWN: LDC channel went down");
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
}
}
if (event & LDC_EVT_RESET) {
PR0("LDC_EVT_RESET: LDC channel was reset");
PR0("scheduling full reset");
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
}
} else {
PR0("channel already reset, ignoring...\n");
PR0("doing ldc up...\n");
}
return (LDC_SUCCESS);
}
if (event & LDC_EVT_UP) {
PR0("EVT_UP: LDC is up\nResetting client connection state");
PR0("initiating soft reset");
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
return (LDC_SUCCESS);
}
}
if (event & LDC_EVT_READ) {
int status;
PR1("New data available");
/* Queue a task to receive the new data */
if (status == DDI_FAILURE) {
PR0("cannot schedule task to recv msg\n");
}
}
return (LDC_SUCCESS);
}
static uint_t
{
return (MH_WALK_TERMINATE);
}
static int
{
uint_t vd_present = 0;
switch (cmd) {
case DDI_DETACH:
/* the real work happens below */
break;
case DDI_SUSPEND:
PR0("No action required for DDI_SUSPEND");
return (DDI_SUCCESS);
default:
PR0("Unrecognized \"cmd\"");
return (DDI_FAILURE);
}
return (DDI_FAILURE);
}
/* Do no detach when serving any vdisks */
if (vd_present) {
PR0("Not detaching because serving vdisks");
return (DDI_FAILURE);
}
PR0("Detaching");
}
return (DDI_SUCCESS);
}
/*
* Description:
* This function checks to see if the disk image being used as a
* virtual disk is an ISO image. An ISO image is a special case
*
* Parameters:
* vd - disk on which the operation is performed.
*
* Return Code:
* B_TRUE - The disk image is an ISO 9660 compliant image
* B_FALSE - just a regular disk image
*/
static boolean_t
{
char iso_buf[ISO_SECTOR_SIZE];
int i, rv;
/*
* If we have already discovered and saved this info we can
* short-circuit the check and avoid reading the disk image.
*/
return (B_TRUE);
/*
* We wish to read the sector that should contain the 2nd ISO volume
* descriptor. The second field in this descriptor is called the
* Standard Identifier and is set to CD001 for a CD-ROM compliant
* to the ISO 9660 standard.
*/
if (rv < 0)
return (B_FALSE);
for (i = 0; i < ISO_ID_STRLEN; i++) {
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Description:
* This function checks to see if the virtual device is an ATAPI
* any USCSI calls vds makes need to take this into account.
*
* Parameters:
* vd - disk on which the operation is performed.
*
* Return Code:
* B_TRUE - The virtual disk is backed by an ATAPI device
* B_FALSE - not an ATAPI device (presumably SCSI)
*/
static boolean_t
{
char *variantp;
int rv;
if (rv == DDI_PROP_SUCCESS) {
}
if (rv) {
}
return (is_atapi);
}
static int
{
int status;
/* set the disk size, block size and the media type of the disk */
if (status != 0) {
/* unexpected failure */
PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d",
status);
return (status);
}
/*
* The function can fail for SCSI disks which are present but
* reserved by another system. In that case, we don't know the
* size of the disk and the block size.
*/
vd->block_size = 0;
}
/* Move dev number and LDI handle to entire-disk-slice array elements */
/* Initialize device numbers for remaining slices and open them */
/*
* Skip the entire-disk slice, as it's already open and its
* device known
*/
if (slice == VD_ENTIRE_DISK_SLICE)
continue;
/*
* Construct the device number for the current slice
*/
/*
* Open all slices of the disk to serve them to the client.
* Slices are opened exclusively to prevent other threads or
* processes in the service domain from performing I/O to
* slices being accessed by a client. Failure to open a slice
* results in vds not serving this disk, as the client could
* attempt (and should be able) to access any slice immediately.
* Any slices successfully opened before a failure will get
* closed by vds_destroy_vd() as a result of the error returned
* by this function.
*
* We need to do the open with FNDELAY so that opening an empty
* slice does not fail.
*/
PR0("Opening device major %u, minor %u = slice %u",
/*
* Try to open the device. This can fail for example if we are
* opening an empty slice. So in case of a failure, we try the
* open again but this time with the FNDELAY flag.
*/
if (status != 0) {
}
if (status != 0) {
PRN("ldi_open_by_dev() returned errno %d "
/* vds_destroy_vd() will close any open slices */
return (status);
}
}
return (0);
}
/*
* When a slice or a volume is exported as a single-slice disk, we want
* the disk backend (i.e. the slice or volume) to be entirely mapped as
* a slice without the addition of any metadata.
*
* So when exporting the disk as a VTOC disk, we fake a disk with the following
* layout:
* flabel +--- flabel_limit
* <-> V
* 0 1 C D E
* +-+---+--------------------------+--+
* virtual disk: |L|XXX| slice 0 |AA|
* +-+---+--------------------------+--+
* ^ : :
* | : :
* VTOC LABEL--+ : :
* +--------------------------+
* +--------------------------+
* 0 N
*
*
* We simulate a disk with N+M blocks, where M is the number of blocks
* simluated at the beginning and at the end of the disk (blocks 0-C
* and D-E).
*
* The first blocks (0 to C-1) are emulated and can not be changed. Blocks C
* to D defines slice 0 and are mapped to the backend. Finally we emulate 2
* alternate cylinders at the end of the disk (blocks D-E). In summary we have:
*
* - block 0 (L) returns a fake VTOC label
* - blocks 1 to C-1 (X) are unused and return 0
* - blocks C to D-1 are mapped to the exported slice or volume
* - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders)
*
* Note: because we define a fake disk geometry, it is possible that the length
* of the backend is not a multiple of the size of cylinder, in that case the
* very end of the backend will not map to any block of the virtual disk.
*/
static int
{
char unit;
/* Initialize dk_geom structure for single-slice device */
return (EIO);
}
return (EIO);
}
/* size of a cylinder in block */
/*
* Add extra cylinders: we emulate the first cylinder (which contains
* the disk label).
*/
/* we emulate 2 alternate cylinders */
/* Initialize vtoc structure for single-slice device */
/*
* Partition 0 starts on cylinder 1 and its size has to be
* a multiple of a number of cylinder.
*/
if (vd_slice_single_slice) {
MIN(sizeof (VD_ASCIILABEL),
} else {
/* adjust the number of slices */
/* define slice 2 representing the entire disk */
/*
* Set some attributes of the geometry to what format(1m) uses
* so that writing a default label using format(1m) does not
* produce any error.
*/
/*
* We must have a correct label name otherwise format(1m) will
* not recognized the disk as labeled.
*/
"SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d",
/* create a fake label from the vtoc and geometry */
VD_LABEL_VTOC(vd));
}
/* adjust the vdisk_size, we emulate 3 cylinders */
return (0);
}
/*
* When a slice, volume or file is exported as a single-slice disk, we want
* the disk backend (i.e. the slice, volume or file) to be entirely mapped
* as a slice without the addition of any metadata.
*
* So when exporting the disk as an EFI disk, we fake a disk with the following
* layout:
*
* flabel +--- flabel_limit
* <------> v
* 0 1 2 L 34 34+N P
* +-+-+--+-------+--------------------------+-------+
* virtual disk: |X|T|EE|XXXXXXX| slice 0 |RRRRRRR|
* +-+-+--+-------+--------------------------+-------+
* ^ ^ : :
* | | : :
* GPT-+ +-GPE : :
* +--------------------------+
* +--------------------------+
* 0 N
*
*
* We simulate a disk with N+M blocks, where M is the number of blocks
* simluated at the beginning and at the end of the disk (blocks 0-34
* and 34+N-P).
*
* The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34
* to 34+N defines slice 0 and are mapped to the exported backend, and we
* emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI
* reserved partition.
*
* - block 0 (X) is unused and return 0
* - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
* - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI)
* - blocks L to 33 (X) are unused and return 0
* - blocks 34 to 34+N are mapped to the exported slice, volume or file
* - blocks 34+N+1 to P define a fake reserved partition and backup label, it
* returns 0
*
* Note: if the backend size is not a multiple of the vdisk block size
* (DEV_BSIZE = 512 byte) then the very end of the backend will not map to
* any block of the virtual disk.
*/
static int
{
/* adjust the vdisk_size, we emulate the first 34 blocks */
s0_start = 34;
if (vd_slice_single_slice) {
} else {
/* adjust the number of slices */
/* define a fake reserved partition */
/* adjust the vdisk_size to include the reserved slice */
}
/* adjust the vdisk size for the backup GPT and GPE */
return (0);
}
/*
* Setup for a virtual disk whose backend is a file (exported as a single slice
* or as a full disk). In that case, the backend is accessed using the vnode
* interface.
*/
static int
{
0, &vd->file_vnode, 0, 0)) != 0) {
return (status);
}
/*
* We set vd->file now so that vds_destroy_vd will take care of
* closing the file and releasing the vnode in case of an error.
*/
!= 0) {
return (EIO);
}
return (EIO);
}
/*
* Get max_xfer_sz from the device where the file is.
*/
if (status != 0) {
PR0("ldi_open() returned errno %d for underlying device",
status);
} else {
&rval)) != 0) {
PR0("ldi_ioctl(DKIOCINFO) returned errno %d for "
"underlying device", status);
} else {
/*
* Store the device's max transfer size for
* return to the client
*/
}
PR0("close the underlying device");
}
PR0("using file %s on device (%d, %d), max_xfer = %u blks",
else
return (status);
}
static int
{
int status;
/* sector size = block size = DEV_BSIZE */
} else {
/*
* We build a default label to get a geometry for
* the vdisk. Then the partition setup function will
* adjust the vtoc so that it defines a single-slice
* disk.
*/
}
return (status);
}
static int
{
int status;
/* size should be at least sizeof(dk_label) */
PRN("Size of file has to be at least %ld bytes",
sizeof (struct dk_label));
return (EIO);
}
/* sector size = block size = DEV_BSIZE */
/*
* Find and validate the geometry of a disk image.
*/
return (EIO);
}
if (vd_dskimg_is_iso_image(vd)) {
/*
* Indicate whether to call this a CD or DVD from the size
* of the ISO image (images for both drive types are stored
* in the ISO-9600 format). CDs can store up to just under 1Gb
*/
else
} else {
}
/* Setup devid for the disk image */
if (status == 0) {
/* a valid devid was found */
return (0);
}
/*
* There was an error while trying to read the devid.
* So this disk image may have a devid but we are
* unable to read it.
*/
return (0);
}
}
/*
* No valid device id was found so we create one. Note that a failure
* to create a device id is not fatal and does not prevent the disk
* image from being attached.
*/
return (0);
}
/*
* Write devid to the disk image. The devid is stored into the disk
* image if we have a valid label; otherwise the devid will be stored
* when the user writes a valid label.
*/
}
}
return (0);
}
/*
* Description:
* Open a device using its device path (supplied by ldm(1m))
*
* Parameters:
* vd - pointer to structure containing the vDisk info
* flags - open flags
*
* Return Value
* 0 - success
* != 0 - some other non-zero return value from ldi(9F) functions
*/
static int
{
int status;
/* Attempt to open device */
/*
* The open can fail for example if we are opening an empty slice.
* In case of a failure, we try the open again but this time with
* the FNDELAY flag.
*/
if (status != 0)
if (status != 0) {
return (status);
}
return (0);
}
/*
* Setup for a virtual disk which backend is a device (a physical disk,
* slice or volume device) exported as a full disk or as a slice. In these
* cases, the backend is accessed using the LDI interface.
*/
static int
{
/* device has been opened by vd_identify_dev() */
/* Verify backing device supports dk_cinfo */
&rval)) != 0) {
PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
return (status);
}
PRN("slice %u >= maximum slice %u for %s",
return (EIO);
}
/*
* The device has been opened read-only by vd_identify_dev(), re-open
* it read-write if the write flag is set and we don't have an optical
* device such as a CD-ROM, which, for now, we do not permit writes to
* and thus should not export write operations to the client.
*
* optical devices we will need to do further checking of the media type
* to distinguish between read-only and writable discs.
*/
kcred);
if (status != 0) {
PR0("Failed to open (%s) = errno %d",
return (status);
}
}
/* Store the device's max transfer size for return to the client */
/*
* We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
* that we can use the correct CDB group when sending USCSI commands.
*/
/*
* Export a full disk.
*
* device. We export a device as a full disk if we have an entire
* disk slice (slice 2) and if this slice is exported as a full disk
* and not as a single slice disk. A CD or DVD device is exported
* as a full disk (even if it isn't s2). A volume is exported as a
* full disk as long as the "slice" option is not specified.
*/
/* get size of backing device */
DDI_SUCCESS) {
PRN("ldi_get_size() failed for %s",
return (EIO);
}
/* setup disk image */
return (vd_setup_disk_image(vd));
}
return (vd_setup_full_disk(vd));
}
}
/*
* Export a single slice disk.
*
* The exported device can be either a volume device or a disk slice. If
* it is a disk slice different from slice 2 then it is always exported
* as a single slice disk even if the "slice" option is not specified.
* If it is disk slice 2 or a volume device then it is exported as a
* single slice disk only if the "slice" option is specified.
*/
return (vd_setup_single_slice_disk(vd));
}
static int
{
/* Get size of backing device */
return (EIO);
}
}
/*
* We export the slice as a single slice disk even if the "slice"
* option was not specified.
*/
/*
* When exporting a slice or a device as a single slice disk, we don't
* care about any partitioning exposed by the backend. The goal is just
* to export the backend as a flat storage. We provide a fake partition
* table (either a VTOC or EFI), which presents only one slice, to
* accommodate tools expecting a disk label. The selection of the label
* type (VTOC or EFI) depends on the value of the vd_slice_label
* variable.
*/
if (vd_slice_label == VD_DISK_LABEL_EFI ||
} else {
/* try with the non-extended vtoc ioctl */
}
if (status == 0) {
if (status != 0) {
PRN("ldi_ioctl(DKIOCGEOM) returned errno %d "
return (status);
}
} else if (vd_slice_label == VD_DISK_LABEL_VTOC) {
&label);
} else {
}
}
/* export with a fake VTOC label */
} else {
/* export with a fake EFI label */
}
return (status);
}
static int
{
/* file (slice or full disk) */
if (rv != 0) {
return (rv);
}
/* physical slice or volume (slice or full disk) */
if (rv != DDI_SUCCESS) {
return (EIO);
}
} else {
/* physical disk */
if (rv != 0) {
PR0("DKIOCGMEDIAINFO failed for %s (err=%d)",
return (rv);
}
}
/* check if size has changed */
return (0);
/*
* If we are exporting a single-slice disk and the size of the backend
* has changed then we regenerate the partition setup so that the
* partitioning matches with the new disk backend size.
*/
/* slice or file or device exported as a slice */
if (rv != 0) {
PR0("vd_setup_partition_vtoc() failed for %s "
return (rv);
}
} else {
if (rv != 0) {
PR0("vd_setup_partition_efi() failed for %s "
return (rv);
}
}
/* physical disk */
vd->vdisk_media =
}
return (0);
}
/*
* Description:
* Open a device using its device path and identify if this is
* a disk device or a volume device.
*
* Parameters:
* vd - pointer to structure containing the vDisk info
* dtype - return the driver type of the device
*
* Return Value
* 0 - success
* != 0 - some other non-zero return value from ldi(9F) functions
*/
static int
{
int status, i;
char *drv_name;
int drv_type;
if (status != 0) {
return (status);
}
/* Get device number of backing device */
PRN("ldi_get_dev() returned errno %d for %s",
return (status);
}
/*
* We start by looking if the driver is in the list from vds.conf
* so that we can override the built-in list using vds.conf.
*/
/* check vds.conf list */
for (i = 0; i < vds->num_drivers; i++) {
/* ignore invalid entries */
continue;
}
goto done;
}
}
/* check built-in list */
for (i = 0; i < VDS_NUM_DRIVERS; i++) {
goto done;
}
}
done:
return (0);
}
static int
{
/* make sure the vdisk backend is valid */
goto done;
}
case VREG:
/*
* Backend is a file so it is exported as a full disk or as a
* single slice disk using the vnode interface.
*/
break;
case VBLK:
case VCHR:
/*
* Backend is a device. In that case, it is exported using the
* LDI interface, and it is exported either as a single-slice
* disk or as a full disk depending on the "slice" option and
* on the type of device.
*
* - A volume device is exported as a single-slice disk if the
* "slice" is specified, otherwise it is exported as a full
* disk.
*
* - A disk slice (different from slice 2) is always exported
* as a single slice disk using the LDI interface.
*
* - The slice 2 of a disk is exported as a single slice disk
* if the "slice" option is specified, otherwise the entire
* disk will be exported.
*
* - The slice of a CD or DVD is exported as single slice disk
* if the "slice" option is specified, otherwise the entire
* disk will be exported.
*/
/* check if this is a pseudo device */
break;
}
break;
}
/*
* If the driver hasn't been identified then we consider that
* pseudo devices are volumes and other devices are disks.
*/
if (drv_type == VD_DRIVER_VOLUME ||
}
/*
* If this is a volume device then its usage depends if the
* "slice" option is set or not. If the "slice" option is set
* then the volume device will be exported as a single slice,
* otherwise it will be exported as a full disk.
*
* For backward compatibility, if vd_volume_force_slice is set
* then we always export volume devices as slices.
*/
}
break;
default:
}
done:
if (status != 0) {
/*
* If the error is retryable print an error message only
* during the first try.
*/
PRN("%s is currently inaccessible (error %d)",
}
} else {
PRN("%s can not be exported as a virtual disk "
}
/* print a message only if we previously had an error */
}
return (status);
}
static int
{
char tq_name[TASKQ_NAMELEN];
int status;
PRN("No memory for virtual disk");
return (EAGAIN);
}
/* Setup open flags */
if (!(options & VD_OPT_RDONLY))
if (options & VD_OPT_EXCLUSIVE)
/* Setup disk type */
if (options & VD_OPT_SLICE) {
} else {
}
/* default disk label */
/* Open vdisk and initialize parameters */
PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
} else {
return (status);
}
/* Initialize locking */
&iblock) != DDI_SUCCESS) {
PRN("Could not get iblock cookie.");
return (EIO);
}
/* Create start and completion task queues for the vdisk */
TASKQ_DEFAULTPRI, 0)) == NULL) {
PRN("Could not create task queue");
return (EIO);
}
TASKQ_DEFAULTPRI, 0)) == NULL) {
PRN("Could not create task queue");
return (EIO);
}
/* Allocate the staging buffer */
/* Bring up LDC */
PRN("Could not initialize LDC channel %lx, "
return (status);
}
PRN("Could not initialize LDC channel %lu,"
return (status);
}
PRN("Could not initialize LDC channel %lu,"
return (status);
}
}
/* Allocate the inband task memory handle */
if (status) {
PRN("Could not initialize LDC channel %lu,"
return (ENXIO);
}
/* Add the successfully-initialized vdisk to the server's table */
return (EIO);
}
/* store initial state */
return (0);
}
static void
{
/* Free all dring_task memory handles */
(vdp->descriptor_size -
sizeof (vio_dring_entry_hdr_t)));
}
}
}
/*
* Destroy the state associated with a virtual disk
*/
static void
vds_destroy_vd(void *arg)
{
return;
PR0("Destroying vdisk state");
/* Disable queuing requests for the vdisk */
}
/* Drain and destroy start queue (*before* destroying completionq) */
/* Drain and destroy completion queue (*before* shutting down LDC) */
/* Free the inband task memory handle */
/* Shut down LDC */
/* unmap the dring */
/* close LDC channel - retry on EAGAIN */
if (++retry > vds_ldc_retries) {
PR0("Timed out closing channel");
break;
}
}
if (rv == 0) {
} else {
/*
* Closing the LDC channel has failed. Ideally we should
* fail here but there is no Zeus level infrastructure
* to handle this. The MD has already been changed and
* we have to do the close. So we try to do as much
* clean up as we can.
*/
}
}
/* Free the staging buffer for msgs */
}
/* Free the inband message buffer */
}
/* Close file */
} else {
/* Close any open backing-device slices */
}
}
}
/* Free disk image devid */
/* Free any fake label */
vd->flabel_size = 0;
}
/* Free lock */
/* Finally, free the vdisk structure itself */
}
static int
{
int status;
return (status);
}
static int
{
int num_channels;
/* Look for channel endpoint child(ren) of the vdisk MD node */
return (-1);
}
/* Get the "id" value for the first channel endpoint node */
PRN("No \"%s\" property found for \"%s\" of vdisk",
return (-1);
}
if (num_channels > 1) {
PRN("Using ID of first of multiple channels for this vdisk");
}
return (0);
}
static int
{
PRN("Invalid node count in Machine Description subtree");
return (-1);
}
return (status);
}
/*
* Function:
* vds_get_options
*
* Description:
* Parse the options of a vds node. Options are defined as an array
* of strings in the vds-block-device-opts property of the vds node
* in the machine description. Options are returned as a bitmask. The
* mapping between the bitmask options and the options strings from the
* machine description is defined in the vd_bdev_options[] array.
*
* The vds-block-device-opts property is optional. If a vds has no such
* property then no option is defined.
*
* Parameters:
* md - machine description.
* vd_node - vds node in the machine description for which
* options have to be parsed.
* options - the returned options.
*
* Return Code:
* none.
*/
static void
{
int len, n, i;
*options = 0;
PR0("No options found");
return;
}
/* parse options */
n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
for (i = 0; i < n; i++) {
opt, VD_OPTION_NLEN) == 0) {
break;
}
}
if (i < n) {
} else {
}
}
}
static void
{
vds->num_drivers);
vds->num_drivers = 0;
}
}
/*
* Update the driver type list with information from vds.conf.
*/
static void
{
char **list, *s;
return;
/*
* We create a driver_types list with as many as entries as there
* is in the driver-type-list from vds.conf. However only valid
* entries will be populated (i.e. entries from driver-type-list
* with a valid syntax). Invalid entries will be left blank so
* they will have no driver name and the driver type will be
* VD_DRIVER_UNKNOWN (= 0).
*/
KM_SLEEP);
for (i = 0; i < num; i++) {
if (s == NULL) {
"a colon is expected in the entry",
i, list[i]);
continue;
}
if (len == 0) {
"the driver name is empty",
i, list[i]);
continue;
}
if (len >= VD_DRIVER_NAME_LEN) {
"the driver name is too long",
i, list[i]);
continue;
}
} else {
"the driver type is invalid",
i, list[i]);
continue;
}
PR0("driver-type-list, entry %d (%s) added",
i, list[i]);
count++;
}
if (count == 0) {
/* nothing was added, clean up */
}
}
static void
{
char *device_path = NULL;
return;
}
&device_path) != 0) {
return;
}
return;
}
return;
}
}
static void
{
PRN("Unable to get \"%s\" property from vdisk's MD node",
return;
}
}
static void
{
/* Validate that vdisk ID has not changed */
PRN("Error getting previous vdisk \"%s\" property",
return;
}
return;
}
PRN("Not changing vdisk: ID changed from %lu to %lu",
return;
}
/* Validate that LDC ID has not changed */
return;
}
return;
}
if (curr_ldc_id != prev_ldc_id) {
PRN("Not changing vdisk: "
return;
}
/* Determine whether device path has changed */
&prev_dev) != 0) {
PRN("Error getting previous vdisk \"%s\"",
return;
}
&curr_dev) != 0) {
return;
}
return; /* no relevant (supported) change */
/* Validate that options have not changed */
if (prev_options != curr_options) {
PRN("Not changing vdisk: options changed from %lx to %lx",
return;
}
/* Remove old state, which will close vdisk and reset */
/* Re-initialize vdisk with new state */
curr_ldc_id) != 0) {
return;
}
}
static int
{
int i;
return (MDEG_FAILURE);
return (MDEG_SUCCESS);
}
static int
{
int cfg_handle;
/*
* The "cfg-handle" property of a vds node in an MD contains the MD's
* notion of "instance", or unique identifier, for that node; OBP
* stores the value of the "cfg-handle" MD property as the value of
* the "reg" property on the node in the device tree it builds from
* the MD and passes to Solaris. Thus, we look up the devinfo node's
* "reg" property value to uniquely identify this device instance when
* registering with the MD event-generation framework. If the "reg"
* property cannot be found, the device tree state is presumably so
* broken that there is no point in continuing.
*/
VD_REG_PROP)) {
return (DDI_FAILURE);
}
/* Get the MD instance for later MDEG registration */
VD_REG_PROP, -1);
return (DDI_FAILURE);
}
return (DDI_FAILURE);
}
vds_destroy_vd, sizeof (void *));
return (DDI_FAILURE);
}
/* Register for MD updates */
sz = sizeof (vds_prop_template);
/* initialize the complete prop spec structure */
PRN("Unable to register for MD updates");
return (DDI_FAILURE);
}
/* Prevent auto-detaching so driver is available whenever MD changes */
PRN("failed to set \"%s\" property for instance %u",
}
/* read any user defined driver types from conf file and update list */
return (DDI_SUCCESS);
}
static int
{
int status;
switch (cmd) {
case DDI_ATTACH:
PR0("Attaching");
return (status);
case DDI_RESUME:
PR0("No action required for DDI_RESUME");
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
DEVO_REV, /* devo_rev */
0, /* devo_refcnt */
ddi_no_info, /* devo_getinfo */
nulldev, /* devo_identify */
nulldev, /* devo_probe */
vds_attach, /* devo_attach */
vds_detach, /* devo_detach */
nodev, /* devo_reset */
NULL, /* devo_cb_ops */
NULL, /* devo_bus_ops */
nulldev, /* devo_power */
ddi_quiesce_not_needed, /* devo_quiesce */
};
"virtual disk server",
&vds_ops,
};
static struct modlinkage modlinkage = {
&modldrv,
};
int
_init(void)
{
int status;
return (status);
return (status);
}
return (0);
}
int
{
}
int
_fini(void)
{
int status;
return (status);
return (0);
}