11991Sheppo /*
21991Sheppo * CDDL HEADER START
31991Sheppo *
41991Sheppo * The contents of this file are subject to the terms of the
51991Sheppo * Common Development and Distribution License (the "License").
61991Sheppo * You may not use this file except in compliance with the License.
71991Sheppo *
81991Sheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91991Sheppo * or http://www.opensolaris.org/os/licensing.
101991Sheppo * See the License for the specific language governing permissions
111991Sheppo * and limitations under the License.
121991Sheppo *
131991Sheppo * When distributing Covered Code, include this CDDL HEADER in each
141991Sheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151991Sheppo * If applicable, add the following below this CDDL HEADER, with the
161991Sheppo * fields enclosed by brackets "[]" replaced with your own identifying
171991Sheppo * information: Portions Copyright [yyyy] [name of copyright owner]
181991Sheppo *
191991Sheppo * CDDL HEADER END
201991Sheppo */
211991Sheppo
221991Sheppo /*
23*12847SZachary.Kissel@Sun.COM * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
241991Sheppo */
251991Sheppo
261991Sheppo /*
271991Sheppo * Virtual disk server
281991Sheppo */
291991Sheppo
301991Sheppo
311991Sheppo #include <sys/types.h>
321991Sheppo #include <sys/conf.h>
332531Snarayan #include <sys/crc32.h>
341991Sheppo #include <sys/ddi.h>
351991Sheppo #include <sys/dkio.h>
361991Sheppo #include <sys/file.h>
375365Slm66018 #include <sys/fs/hsfs_isospec.h>
381991Sheppo #include <sys/mdeg.h>
395658Sachartre #include <sys/mhd.h>
401991Sheppo #include <sys/modhash.h>
411991Sheppo #include <sys/note.h>
421991Sheppo #include <sys/pathname.h>
434838Slm66018 #include <sys/sdt.h>
441991Sheppo #include <sys/sunddi.h>
451991Sheppo #include <sys/sunldi.h>
461991Sheppo #include <sys/sysmacros.h>
471991Sheppo #include <sys/vio_common.h>
485365Slm66018 #include <sys/vio_util.h>
491991Sheppo #include <sys/vdsk_mailbox.h>
501991Sheppo #include <sys/vdsk_common.h>
511991Sheppo #include <sys/vtoc.h>
523401Snarayan #include <sys/vfs.h>
533401Snarayan #include <sys/stat.h>
544696Sachartre #include <sys/scsi/impl/uscsi.h>
556845Sha137994 #include <sys/ontrap.h>
563782Sachartre #include <vm/seg_map.h>
571991Sheppo
587563SPrasad.Singamsetty@Sun.COM #define ONE_MEGABYTE (1ULL << 20)
597563SPrasad.Singamsetty@Sun.COM #define ONE_GIGABYTE (1ULL << 30)
606836Sachartre #define ONE_TERABYTE (1ULL << 40)
616836Sachartre
621991Sheppo /* Virtual disk server initialization flags */
632336Snarayan #define VDS_LDI 0x01
642336Snarayan #define VDS_MDEG 0x02
651991Sheppo
661991Sheppo /* Virtual disk server tunable parameters */
673401Snarayan #define VDS_RETRIES 5
683401Snarayan #define VDS_LDC_DELAY 1000 /* 1 msecs */
693401Snarayan #define VDS_DEV_DELAY 10000000 /* 10 secs */
701991Sheppo #define VDS_NCHAINS 32
711991Sheppo
721991Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */
731991Sheppo #define VDS_NAME "virtual-disk-server"
741991Sheppo
751991Sheppo #define VD_NAME "vd"
761991Sheppo #define VD_VOLUME_NAME "vdisk"
771991Sheppo #define VD_ASCIILABEL "Virtual Disk"
781991Sheppo
791991Sheppo #define VD_CHANNEL_ENDPOINT "channel-endpoint"
801991Sheppo #define VD_ID_PROP "id"
811991Sheppo #define VD_BLOCK_DEVICE_PROP "vds-block-device"
825081Sachartre #define VD_BLOCK_DEVICE_OPTS "vds-block-device-opts"
833297Ssb155480 #define VD_REG_PROP "reg"
841991Sheppo
851991Sheppo /* Virtual disk initialization flags */
863401Snarayan #define VD_DISK_READY 0x01
873401Snarayan #define VD_LOCKING 0x02
883401Snarayan #define VD_LDC 0x04
893401Snarayan #define VD_DRING 0x08
903401Snarayan #define VD_SID 0x10
913401Snarayan #define VD_SEQ_NUM 0x20
925081Sachartre #define VD_SETUP_ERROR 0x40
931991Sheppo
944696Sachartre /* Number of backup labels */
957597SAlexandre.Chartre@Sun.COM #define VD_DSKIMG_NUM_BACKUP 5
964696Sachartre
974696Sachartre /* Timeout for SCSI I/O */
984696Sachartre #define VD_SCSI_RDWR_TIMEOUT 30 /* 30 secs */
994696Sachartre
1007791SAlexandre.Chartre@Sun.COM /*
1017791SAlexandre.Chartre@Sun.COM * Default number of threads for the I/O queue. In many cases, we will not
1027791SAlexandre.Chartre@Sun.COM * receive more than 8 I/O requests at the same time. However there are
1037791SAlexandre.Chartre@Sun.COM * cases (for example during the OS installation) where we can have a lot
1047791SAlexandre.Chartre@Sun.COM * more (up to the limit of the DRing size).
1057791SAlexandre.Chartre@Sun.COM */
1067791SAlexandre.Chartre@Sun.COM #define VD_IOQ_NTHREADS 8
1077791SAlexandre.Chartre@Sun.COM
1085874Sachartre /* Maximum number of logical partitions */
1095874Sachartre #define VD_MAXPART (NDKMAP + 1)
1105874Sachartre
1111991Sheppo /*
1121991Sheppo * By Solaris convention, slice/partition 2 represents the entire disk;
1131991Sheppo * unfortunately, this convention does not appear to be codified.
1141991Sheppo */
1151991Sheppo #define VD_ENTIRE_DISK_SLICE 2
1161991Sheppo
1176836Sachartre /* Logical block address for EFI */
1186836Sachartre #define VD_EFI_LBA_GPT 1 /* LBA of the GPT */
1196836Sachartre #define VD_EFI_LBA_GPE 2 /* LBA of the GPE */
1206836Sachartre
1219889SLarry.Liu@Sun.COM #define VD_EFI_DEV_SET(dev, vdsk, ioctl) \
1229889SLarry.Liu@Sun.COM VDSK_EFI_DEV_SET(dev, vdsk, ioctl, \
1239889SLarry.Liu@Sun.COM (vdsk)->vdisk_bsize, (vdsk)->vdisk_size)
1249889SLarry.Liu@Sun.COM
1257791SAlexandre.Chartre@Sun.COM /*
1267791SAlexandre.Chartre@Sun.COM * Flags defining the behavior for flushing asynchronous writes used to
1277791SAlexandre.Chartre@Sun.COM * performed some write I/O requests.
1287791SAlexandre.Chartre@Sun.COM *
1297791SAlexandre.Chartre@Sun.COM * The VD_AWFLUSH_IMMEDIATE enables immediate flushing of asynchronous
1307791SAlexandre.Chartre@Sun.COM * writes. This ensures that data are committed to the backend when the I/O
1317791SAlexandre.Chartre@Sun.COM * request reply is sent to the guest domain so this prevents any data to
1327791SAlexandre.Chartre@Sun.COM * be lost in case a service domain unexpectedly crashes.
1337791SAlexandre.Chartre@Sun.COM *
1347791SAlexandre.Chartre@Sun.COM * The flag VD_AWFLUSH_DEFER indicates that flushing is deferred to another
1357791SAlexandre.Chartre@Sun.COM * thread while the request is immediatly marked as completed. In that case,
1367791SAlexandre.Chartre@Sun.COM * a guest domain can a receive a reply that its write request is completed
1377791SAlexandre.Chartre@Sun.COM * while data haven't been flushed to disk yet.
1387791SAlexandre.Chartre@Sun.COM *
1397791SAlexandre.Chartre@Sun.COM * Flags VD_AWFLUSH_IMMEDIATE and VD_AWFLUSH_DEFER are mutually exclusive.
1407791SAlexandre.Chartre@Sun.COM */
1417791SAlexandre.Chartre@Sun.COM #define VD_AWFLUSH_IMMEDIATE 0x01 /* immediate flushing */
1427791SAlexandre.Chartre@Sun.COM #define VD_AWFLUSH_DEFER 0x02 /* defer flushing */
1437791SAlexandre.Chartre@Sun.COM #define VD_AWFLUSH_GROUP 0x04 /* group requests before flushing */
1447791SAlexandre.Chartre@Sun.COM
1456108Sachartre /* Driver types */
1466108Sachartre typedef enum vd_driver {
1476108Sachartre VD_DRIVER_UNKNOWN = 0, /* driver type unknown */
1486108Sachartre VD_DRIVER_DISK, /* disk driver */
1496108Sachartre VD_DRIVER_VOLUME /* volume driver */
1506108Sachartre } vd_driver_t;
1516108Sachartre
1526108Sachartre #define VD_DRIVER_NAME_LEN 64
1536108Sachartre
1546108Sachartre #define VDS_NUM_DRIVERS (sizeof (vds_driver_types) / sizeof (vd_driver_type_t))
1556108Sachartre
1566108Sachartre typedef struct vd_driver_type {
1576108Sachartre char name[VD_DRIVER_NAME_LEN]; /* driver name */
1586108Sachartre vd_driver_t type; /* driver type (disk or volume) */
1596108Sachartre } vd_driver_type_t;
1606108Sachartre
1616108Sachartre /*
1626108Sachartre * There is no reliable way to determine if a device is representing a disk
1636108Sachartre * or a volume, especially with pseudo devices. So we maintain a list of well
1646108Sachartre * known drivers and the type of device they represent (either a disk or a
1656108Sachartre * volume).
1666108Sachartre *
1676108Sachartre * The list can be extended by adding a "driver-type-list" entry in vds.conf
1686108Sachartre * with the following syntax:
1696108Sachartre *
1706108Sachartre * driver-type-list="<driver>:<type>", ... ,"<driver>:<type>";
1716108Sachartre *
1726108Sachartre * Where:
1736108Sachartre * <driver> is the name of a driver (limited to 64 characters)
1746108Sachartre * <type> is either the string "disk" or "volume"
1756108Sachartre *
1766108Sachartre * Invalid entries in "driver-type-list" will be ignored.
1776108Sachartre *
1786108Sachartre * For example, the following line in vds.conf:
1796108Sachartre *
1806108Sachartre * driver-type-list="foo:disk","bar:volume";
1816108Sachartre *
1826108Sachartre * defines that "foo" is a disk driver, and driver "bar" is a volume driver.
1836108Sachartre *
1846108Sachartre * When a list is defined in vds.conf, it is checked before the built-in list
1856108Sachartre * (vds_driver_types[]) so that any definition from this list can be overriden
1866108Sachartre * using vds.conf.
1876108Sachartre */
1886108Sachartre vd_driver_type_t vds_driver_types[] = {
1896108Sachartre { "dad", VD_DRIVER_DISK }, /* Solaris */
1906108Sachartre { "did", VD_DRIVER_DISK }, /* Sun Cluster */
1917929SAlexandre.Chartre@Sun.COM { "dlmfdrv", VD_DRIVER_DISK }, /* Hitachi HDLM */
1926623Sachartre { "emcp", VD_DRIVER_DISK }, /* EMC Powerpath */
1936108Sachartre { "lofi", VD_DRIVER_VOLUME }, /* Solaris */
1946108Sachartre { "md", VD_DRIVER_VOLUME }, /* Solaris - SVM */
1956108Sachartre { "sd", VD_DRIVER_DISK }, /* Solaris */
1966108Sachartre { "ssd", VD_DRIVER_DISK }, /* Solaris */
1976108Sachartre { "vdc", VD_DRIVER_DISK }, /* Solaris */
1986108Sachartre { "vxdmp", VD_DRIVER_DISK }, /* Veritas */
1996108Sachartre { "vxio", VD_DRIVER_VOLUME }, /* Veritas - VxVM */
2006108Sachartre { "zfs", VD_DRIVER_VOLUME } /* Solaris */
2016108Sachartre };
2026108Sachartre
2031991Sheppo /* Return a cpp token as a string */
2041991Sheppo #define STRINGIZE(token) #token
2051991Sheppo
2061991Sheppo /*
2071991Sheppo * Print a message prefixed with the current function name to the message log
2081991Sheppo * (and optionally to the console for verbose boots); these macros use cpp's
2091991Sheppo * concatenation of string literals and C99 variable-length-argument-list
2101991Sheppo * macros
2111991Sheppo */
2121991Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "")
2131991Sheppo #define _PRN(format, ...) \
2141991Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__)
2151991Sheppo
2161991Sheppo /* Return a pointer to the "i"th vdisk dring element */
2171991Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \
2181991Sheppo (vd->dring + (i)*vd->descriptor_size))
2191991Sheppo
2201991Sheppo /* Return the virtual disk client's type as a string (for use in messages) */
2211991Sheppo #define VD_CLIENT(vd) \
2221991Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \
2235935Ssb155480 (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" : \
2241991Sheppo (((vd)->xfer_mode == 0) ? "null client" : \
2251991Sheppo "unsupported client")))
2261991Sheppo
2277597SAlexandre.Chartre@Sun.COM /* Read disk label from a disk image */
2287597SAlexandre.Chartre@Sun.COM #define VD_DSKIMG_LABEL_READ(vd, labelp) \
2297597SAlexandre.Chartre@Sun.COM vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \
2303782Sachartre 0, sizeof (struct dk_label))
2313782Sachartre
2327597SAlexandre.Chartre@Sun.COM /* Write disk label to a disk image */
2337597SAlexandre.Chartre@Sun.COM #define VD_DSKIMG_LABEL_WRITE(vd, labelp) \
2347597SAlexandre.Chartre@Sun.COM vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \
2353782Sachartre 0, sizeof (struct dk_label))
2363782Sachartre
2377597SAlexandre.Chartre@Sun.COM /* Identify if a backend is a disk image */
2387597SAlexandre.Chartre@Sun.COM #define VD_DSKIMG(vd) ((vd)->vdisk_type == VD_DISK_TYPE_DISK && \
2397597SAlexandre.Chartre@Sun.COM ((vd)->file || (vd)->volume))
2407597SAlexandre.Chartre@Sun.COM
2417791SAlexandre.Chartre@Sun.COM /* Next index in a write queue */
2427791SAlexandre.Chartre@Sun.COM #define VD_WRITE_INDEX_NEXT(vd, id) \
2437791SAlexandre.Chartre@Sun.COM ((((id) + 1) >= vd->dring_len)? 0 : (id) + 1)
2447791SAlexandre.Chartre@Sun.COM
2455658Sachartre /* Message for disk access rights reset failure */
2465658Sachartre #define VD_RESET_ACCESS_FAILURE_MSG \
2475658Sachartre "Fail to reset disk access rights for disk %s"
2485658Sachartre
2493297Ssb155480 /*
2503297Ssb155480 * Specification of an MD node passed to the MDEG to filter any
2513297Ssb155480 * 'vport' nodes that do not belong to the specified node. This
2523297Ssb155480 * template is copied for each vds instance and filled in with
2533297Ssb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG.
2543297Ssb155480 */
2553297Ssb155480 static mdeg_prop_spec_t vds_prop_template[] = {
2563297Ssb155480 { MDET_PROP_STR, "name", VDS_NAME },
2573297Ssb155480 { MDET_PROP_VAL, "cfg-handle", NULL },
2583297Ssb155480 { MDET_LIST_END, NULL, NULL }
2593297Ssb155480 };
2603297Ssb155480
2613297Ssb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val);
2623297Ssb155480
2633297Ssb155480 /*
2643297Ssb155480 * Matching criteria passed to the MDEG to register interest
2653297Ssb155480 * in changes to 'virtual-device-port' nodes identified by their
2663297Ssb155480 * 'id' property.
2673297Ssb155480 */
2683297Ssb155480 static md_prop_match_t vd_prop_match[] = {
2693297Ssb155480 { MDET_PROP_VAL, VD_ID_PROP },
2703297Ssb155480 { MDET_LIST_END, NULL }
2713297Ssb155480 };
2723297Ssb155480
2733297Ssb155480 static mdeg_node_match_t vd_match = {"virtual-device-port",
2743297Ssb155480 vd_prop_match};
2753297Ssb155480
2765081Sachartre /*
2775081Sachartre * Options for the VD_BLOCK_DEVICE_OPTS property.
2785081Sachartre */
2795081Sachartre #define VD_OPT_RDONLY 0x1 /* read-only */
2805081Sachartre #define VD_OPT_SLICE 0x2 /* single slice */
2815081Sachartre #define VD_OPT_EXCLUSIVE 0x4 /* exclusive access */
2825081Sachartre
2835081Sachartre #define VD_OPTION_NLEN 128
2845081Sachartre
2855081Sachartre typedef struct vd_option {
2865081Sachartre char vdo_name[VD_OPTION_NLEN];
2875081Sachartre uint64_t vdo_value;
2885081Sachartre } vd_option_t;
2895081Sachartre
2905081Sachartre vd_option_t vd_bdev_options[] = {
2915081Sachartre { "ro", VD_OPT_RDONLY },
2925081Sachartre { "slice", VD_OPT_SLICE },
2935081Sachartre { "excl", VD_OPT_EXCLUSIVE }
2945081Sachartre };
2955081Sachartre
2961991Sheppo /* Debugging macros */
2971991Sheppo #ifdef DEBUG
2982793Slm66018
2992793Slm66018 static int vd_msglevel = 0;
3002793Slm66018
3011991Sheppo #define PR0 if (vd_msglevel > 0) PRN
3021991Sheppo #define PR1 if (vd_msglevel > 1) PRN
3031991Sheppo #define PR2 if (vd_msglevel > 2) PRN
3041991Sheppo
3051991Sheppo #define VD_DUMP_DRING_ELEM(elem) \
3063401Snarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \
3071991Sheppo elem->hdr.dstate, \
3081991Sheppo elem->payload.operation, \
3091991Sheppo elem->payload.status, \
3101991Sheppo elem->payload.nbytes, \
3111991Sheppo elem->payload.addr, \
3121991Sheppo elem->payload.ncookies);
3131991Sheppo
3142793Slm66018 char *
vd_decode_state(int state)3152793Slm66018 vd_decode_state(int state)
3162793Slm66018 {
3172793Slm66018 char *str;
3182793Slm66018
3192793Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break;
3202793Slm66018
3212793Slm66018 switch (state) {
3222793Slm66018 CASE_STATE(VD_STATE_INIT)
3232793Slm66018 CASE_STATE(VD_STATE_VER)
3242793Slm66018 CASE_STATE(VD_STATE_ATTR)
3252793Slm66018 CASE_STATE(VD_STATE_DRING)
3262793Slm66018 CASE_STATE(VD_STATE_RDX)
3272793Slm66018 CASE_STATE(VD_STATE_DATA)
3282793Slm66018 default: str = "unknown"; break;
3292793Slm66018 }
3302793Slm66018
3312793Slm66018 #undef CASE_STATE
3322793Slm66018
3332793Slm66018 return (str);
3342793Slm66018 }
3352793Slm66018
3362793Slm66018 void
vd_decode_tag(vio_msg_t * msg)3372793Slm66018 vd_decode_tag(vio_msg_t *msg)
3382793Slm66018 {
3392793Slm66018 char *tstr, *sstr, *estr;
3402793Slm66018
3412793Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break;
3422793Slm66018
3432793Slm66018 switch (msg->tag.vio_msgtype) {
3442793Slm66018 CASE_TYPE(VIO_TYPE_CTRL)
3452793Slm66018 CASE_TYPE(VIO_TYPE_DATA)
3462793Slm66018 CASE_TYPE(VIO_TYPE_ERR)
3472793Slm66018 default: tstr = "unknown"; break;
3482793Slm66018 }
3492793Slm66018
3502793Slm66018 #undef CASE_TYPE
3512793Slm66018
3522793Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break;
3532793Slm66018
3542793Slm66018 switch (msg->tag.vio_subtype) {
3552793Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO)
3562793Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK)
3572793Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK)
3582793Slm66018 default: sstr = "unknown"; break;
3592793Slm66018 }
3602793Slm66018
3612793Slm66018 #undef CASE_SUBTYPE
3622793Slm66018
3632793Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break;
3642793Slm66018
3652793Slm66018 switch (msg->tag.vio_subtype_env) {
3662793Slm66018 CASE_ENV(VIO_VER_INFO)
3672793Slm66018 CASE_ENV(VIO_ATTR_INFO)
3682793Slm66018 CASE_ENV(VIO_DRING_REG)
3692793Slm66018 CASE_ENV(VIO_DRING_UNREG)
3702793Slm66018 CASE_ENV(VIO_RDX)
3712793Slm66018 CASE_ENV(VIO_PKT_DATA)
3722793Slm66018 CASE_ENV(VIO_DESC_DATA)
3732793Slm66018 CASE_ENV(VIO_DRING_DATA)
3742793Slm66018 default: estr = "unknown"; break;
3752793Slm66018 }
3762793Slm66018
3772793Slm66018 #undef CASE_ENV
3782793Slm66018
3792793Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)",
3802793Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype,
3812793Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr);
3822793Slm66018 }
3832793Slm66018
3841991Sheppo #else /* !DEBUG */
3852793Slm66018
3861991Sheppo #define PR0(...)
3871991Sheppo #define PR1(...)
3881991Sheppo #define PR2(...)
3891991Sheppo
3901991Sheppo #define VD_DUMP_DRING_ELEM(elem)
3911991Sheppo
3922793Slm66018 #define vd_decode_state(_s) (NULL)
3932793Slm66018 #define vd_decode_tag(_s) (NULL)
3942793Slm66018
3951991Sheppo #endif /* DEBUG */
3961991Sheppo
3971991Sheppo
3982336Snarayan /*
3992336Snarayan * Soft state structure for a vds instance
4002336Snarayan */
4011991Sheppo typedef struct vds {
4021991Sheppo uint_t initialized; /* driver inst initialization flags */
4031991Sheppo dev_info_t *dip; /* driver inst devinfo pointer */
4041991Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */
4051991Sheppo mod_hash_t *vd_table; /* table of virtual disks served */
4063297Ssb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */
4071991Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */
4086108Sachartre vd_driver_type_t *driver_types; /* extra driver types (from vds.conf) */
4096108Sachartre int num_drivers; /* num of extra driver types */
4101991Sheppo } vds_t;
4111991Sheppo
4122336Snarayan /*
4132336Snarayan * Types of descriptor-processing tasks
4142336Snarayan */
4152336Snarayan typedef enum vd_task_type {
4162336Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */
4172336Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */
4182336Snarayan } vd_task_type_t;
4192336Snarayan
4202336Snarayan /*
4212336Snarayan * Structure describing the task for processing a descriptor
4222336Snarayan */
4232336Snarayan typedef struct vd_task {
4242336Snarayan struct vd *vd; /* vd instance task is for */
4252336Snarayan vd_task_type_t type; /* type of descriptor task */
4262336Snarayan int index; /* dring elem index for task */
4272336Snarayan vio_msg_t *msg; /* VIO message task is for */
4282336Snarayan size_t msglen; /* length of message content */
4292336Snarayan vd_dring_payload_t *request; /* request task will perform */
4302336Snarayan struct buf buf; /* buf(9s) for I/O request */
4312531Snarayan ldc_mem_handle_t mhdl; /* task memory handle */
4324838Slm66018 int status; /* status of processing task */
4334838Slm66018 int (*completef)(struct vd_task *task); /* completion func ptr */
4347791SAlexandre.Chartre@Sun.COM uint32_t write_index; /* index in the write_queue */
4352336Snarayan } vd_task_t;
4362336Snarayan
4372336Snarayan /*
4382336Snarayan * Soft state structure for a virtual disk instance
4392336Snarayan */
4401991Sheppo typedef struct vd {
4417791SAlexandre.Chartre@Sun.COM uint64_t id; /* vdisk id */
4421991Sheppo uint_t initialized; /* vdisk initialization flags */
4435365Slm66018 uint64_t operations; /* bitmask of VD_OPs exported */
4445365Slm66018 vio_ver_t version; /* ver negotiated with client */
4451991Sheppo vds_t *vds; /* server for this vdisk */
4462336Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */
4472336Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */
4487791SAlexandre.Chartre@Sun.COM ddi_taskq_t *ioq; /* queue for I/O */
4497791SAlexandre.Chartre@Sun.COM uint32_t write_index; /* next write index */
4507791SAlexandre.Chartre@Sun.COM buf_t **write_queue; /* queue for async writes */
4511991Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */
4523401Snarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */
4531991Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */
4545081Sachartre int open_flags; /* open flags */
4556836Sachartre uint_t nslices; /* number of slices we export */
4561991Sheppo size_t vdisk_size; /* number of blocks in vdisk */
4579889SLarry.Liu@Sun.COM size_t vdisk_bsize; /* blk size of the vdisk */
4581991Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */
4592531Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */
4605365Slm66018 vd_media_t vdisk_media; /* media type of backing dev. */
4615365Slm66018 boolean_t is_atapi_dev; /* Is this an IDE CD-ROM dev? */
4622410Slm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */
4639889SLarry.Liu@Sun.COM size_t backend_bsize; /* blk size of backend device */
4649889SLarry.Liu@Sun.COM int vio_bshift; /* shift for blk convertion */
4656108Sachartre boolean_t volume; /* is vDisk backed by volume */
4667597SAlexandre.Chartre@Sun.COM boolean_t zvol; /* is vDisk backed by a zvol */
4675365Slm66018 boolean_t file; /* is vDisk backed by a file? */
4685658Sachartre boolean_t scsi; /* is vDisk backed by scsi? */
4693401Snarayan vnode_t *file_vnode; /* file vnode */
4707597SAlexandre.Chartre@Sun.COM size_t dskimg_size; /* size of disk image */
4717597SAlexandre.Chartre@Sun.COM ddi_devid_t dskimg_devid; /* devid for disk image */
4725874Sachartre int efi_reserved; /* EFI reserved slice */
4736836Sachartre caddr_t flabel; /* fake label for slice type */
4746836Sachartre uint_t flabel_size; /* fake label size */
4756836Sachartre uint_t flabel_limit; /* limit of the fake label */
4761991Sheppo struct dk_geom dk_geom; /* synthetic for slice type */
4777563SPrasad.Singamsetty@Sun.COM struct extvtoc vtoc; /* synthetic for slice type */
4785874Sachartre vd_slice_t slices[VD_MAXPART]; /* logical partitions */
4795658Sachartre boolean_t ownership; /* disk ownership status */
4801991Sheppo ldc_status_t ldc_state; /* LDC connection state */
4811991Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */
4821991Sheppo size_t max_msglen; /* largest LDC message len */
4831991Sheppo vd_state_t state; /* client handshake state */
4841991Sheppo uint8_t xfer_mode; /* transfer mode with client */
4851991Sheppo uint32_t sid; /* client's session ID */
4861991Sheppo uint64_t seq_num; /* message sequence number */
4871991Sheppo uint64_t dring_ident; /* identifier of dring */
4881991Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */
4891991Sheppo uint32_t descriptor_size; /* num bytes in desc */
4901991Sheppo uint32_t dring_len; /* number of dring elements */
4916845Sha137994 uint8_t dring_mtype; /* dring mem map type */
4921991Sheppo caddr_t dring; /* address of dring */
4932793Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */
4942336Snarayan vd_task_t inband_task; /* task for inband descriptor */
4952336Snarayan vd_task_t *dring_task; /* tasks dring elements */
4962336Snarayan
4972336Snarayan kmutex_t lock; /* protects variables below */
4982336Snarayan boolean_t enabled; /* is vdisk enabled? */
4992336Snarayan boolean_t reset_state; /* reset connection state? */
5002336Snarayan boolean_t reset_ldc; /* reset LDC channel? */
5011991Sheppo } vd_t;
5021991Sheppo
5036836Sachartre /*
5046836Sachartre * Macros to manipulate the fake label (flabel) for single slice disks.
5056836Sachartre *
5066836Sachartre * If we fake a VTOC label then the fake label consists of only one block
5076836Sachartre * containing the VTOC label (struct dk_label).
5086836Sachartre *
5096836Sachartre * If we fake an EFI label then the fake label consists of a blank block
5106836Sachartre * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t).
5116836Sachartre *
5126836Sachartre */
5139889SLarry.Liu@Sun.COM #define VD_LABEL_VTOC_SIZE(lba) \
5149889SLarry.Liu@Sun.COM P2ROUNDUP(sizeof (struct dk_label), (lba))
5159889SLarry.Liu@Sun.COM
5169889SLarry.Liu@Sun.COM #define VD_LABEL_EFI_SIZE(lba) \
5179889SLarry.Liu@Sun.COM P2ROUNDUP(2 * (lba) + sizeof (efi_gpe_t) * VD_MAXPART, \
5189889SLarry.Liu@Sun.COM (lba))
5196836Sachartre
5206836Sachartre #define VD_LABEL_VTOC(vd) \
5217563SPrasad.Singamsetty@Sun.COM ((struct dk_label *)(void *)((vd)->flabel))
5226836Sachartre
5239889SLarry.Liu@Sun.COM #define VD_LABEL_EFI_GPT(vd, lba) \
5249889SLarry.Liu@Sun.COM ((efi_gpt_t *)(void *)((vd)->flabel + (lba)))
5259889SLarry.Liu@Sun.COM #define VD_LABEL_EFI_GPE(vd, lba) \
5269889SLarry.Liu@Sun.COM ((efi_gpe_t *)(void *)((vd)->flabel + 2 * (lba)))
5276836Sachartre
5286836Sachartre
5291991Sheppo typedef struct vds_operation {
5302793Slm66018 char *namep;
5311991Sheppo uint8_t operation;
5322336Snarayan int (*start)(vd_task_t *task);
5334838Slm66018 int (*complete)(vd_task_t *task);
5341991Sheppo } vds_operation_t;
5351991Sheppo
5362032Slm66018 typedef struct vd_ioctl {
5372032Slm66018 uint8_t operation; /* vdisk operation */
5382032Slm66018 const char *operation_name; /* vdisk operation name */
5392032Slm66018 size_t nbytes; /* size of operation buffer */
5402032Slm66018 int cmd; /* corresponding ioctl cmd */
5412032Slm66018 const char *cmd_name; /* ioctl cmd name */
5422032Slm66018 void *arg; /* ioctl cmd argument */
5432032Slm66018 /* convert input vd_buf to output ioctl_arg */
5445658Sachartre int (*copyin)(void *vd_buf, size_t, void *ioctl_arg);
5452032Slm66018 /* convert input ioctl_arg to output vd_buf */
5462032Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf);
5475081Sachartre /* write is true if the operation writes any data to the backend */
5485081Sachartre boolean_t write;
5492032Slm66018 } vd_ioctl_t;
5502032Slm66018
5512032Slm66018 /* Define trivial copyin/copyout conversion function flag */
5525658Sachartre #define VD_IDENTITY_IN ((int (*)(void *, size_t, void *))-1)
5535658Sachartre #define VD_IDENTITY_OUT ((void (*)(void *, void *))-1)
5541991Sheppo
5551991Sheppo
5563401Snarayan static int vds_ldc_retries = VDS_RETRIES;
5572793Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY;
5583401Snarayan static int vds_dev_retries = VDS_RETRIES;
5593401Snarayan static int vds_dev_delay = VDS_DEV_DELAY;
5601991Sheppo static void *vds_state;
5611991Sheppo
5624696Sachartre static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT;
5635658Sachartre static int vd_scsi_debug = USCSI_SILENT;
5645658Sachartre
5655658Sachartre /*
5667791SAlexandre.Chartre@Sun.COM * Number of threads in the taskq handling vdisk I/O. This can be set up to
5677791SAlexandre.Chartre@Sun.COM * the size of the DRing which is the maximum number of I/O we can receive
5687791SAlexandre.Chartre@Sun.COM * in parallel. Note that using a high number of threads can improve performance
5697791SAlexandre.Chartre@Sun.COM * but this is going to consume a lot of resources if there are many vdisks.
5707791SAlexandre.Chartre@Sun.COM */
5717791SAlexandre.Chartre@Sun.COM static int vd_ioq_nthreads = VD_IOQ_NTHREADS;
5727791SAlexandre.Chartre@Sun.COM
5737791SAlexandre.Chartre@Sun.COM /*
5747791SAlexandre.Chartre@Sun.COM * Tunable to define the behavior for flushing asynchronous writes used to
5757791SAlexandre.Chartre@Sun.COM * performed some write I/O requests. The default behavior is to group as
5767791SAlexandre.Chartre@Sun.COM * much asynchronous writes as possible and to flush them immediatly.
5777791SAlexandre.Chartre@Sun.COM *
5787791SAlexandre.Chartre@Sun.COM * If the tunable is set to 0 then explicit flushing is disabled. In that
5797791SAlexandre.Chartre@Sun.COM * case, data will be flushed by traditional mechanism (like fsflush) but
5807791SAlexandre.Chartre@Sun.COM * this might not happen immediatly.
5817791SAlexandre.Chartre@Sun.COM *
5827791SAlexandre.Chartre@Sun.COM */
5837791SAlexandre.Chartre@Sun.COM static int vd_awflush = VD_AWFLUSH_IMMEDIATE | VD_AWFLUSH_GROUP;
5847791SAlexandre.Chartre@Sun.COM
5857791SAlexandre.Chartre@Sun.COM /*
5865658Sachartre * Tunable to define the behavior of the service domain if the vdisk server
5875658Sachartre * fails to reset disk exclusive access when a LDC channel is reset. When a
5885658Sachartre * LDC channel is reset the vdisk server will try to reset disk exclusive
5895658Sachartre * access by releasing any SCSI-2 reservation or resetting the disk. If these
5905658Sachartre * actions fail then the default behavior (vd_reset_access_failure = 0) is to
5915658Sachartre * print a warning message. This default behavior can be changed by setting
5925658Sachartre * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will
5935658Sachartre * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause
5945658Sachartre * the service domain to panic. In both cases, the reset of the service domain
5955658Sachartre * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation.
5965658Sachartre */
5975658Sachartre static int vd_reset_access_failure = 0;
5985658Sachartre
5995658Sachartre /*
6005658Sachartre * Tunable for backward compatibility. When this variable is set to B_TRUE,
6015658Sachartre * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single
6025658Sachartre * slice disks whether or not they have the "slice" option set. This is
6035658Sachartre * to provide a simple backward compatibility mechanism when upgrading
6045658Sachartre * the vds driver and using a domain configuration created before the
6055658Sachartre * "slice" option was available.
6065658Sachartre */
6075658Sachartre static boolean_t vd_volume_force_slice = B_FALSE;
6084696Sachartre
6092032Slm66018 /*
6106092Sachartre * The label of disk images created with some earlier versions of the virtual
6116092Sachartre * disk software is not entirely correct and have an incorrect v_sanity field
6126092Sachartre * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with
6136092Sachartre * these images because we are now validating that the disk label (and the
6146092Sachartre * sanity) is correct when a disk image is opened.
6156092Sachartre *
6166092Sachartre * This tunable is set to false to not validate the sanity field and ensure
6176092Sachartre * compatibility. If the tunable is set to true, we will do a strict checking
6186092Sachartre * of the sanity but this can create compatibility problems with old disk
6196092Sachartre * images.
6206092Sachartre */
6217597SAlexandre.Chartre@Sun.COM static boolean_t vd_dskimg_validate_sanity = B_FALSE;
6226092Sachartre
6236092Sachartre /*
6246845Sha137994 * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings.
6256845Sha137994 */
6266845Sha137994 static boolean_t vd_direct_mapped_drings = B_TRUE;
6276845Sha137994
6286845Sha137994 /*
6296836Sachartre * When a backend is exported as a single-slice disk then we entirely fake
6306836Sachartre * its disk label. So it can be exported either with a VTOC label or with
6316836Sachartre * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all
6326836Sachartre * single-slice disks will be exported with a VTOC label; and if it is set
6336836Sachartre * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with
6346836Sachartre * an EFI label.
6356836Sachartre *
6366836Sachartre * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk
6376836Sachartre * or volume device then it will be exported with the same type of label as
6386836Sachartre * defined on the device. Otherwise if the backend is a file then it will
6396836Sachartre * exported with the disk label type set in the vd_file_slice_label variable.
6406836Sachartre *
6416836Sachartre * Note that if the backend size is greater than 1TB then it will always be
6426836Sachartre * exported with an EFI label no matter what the setting is.
6436836Sachartre */
6446836Sachartre static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK;
6456836Sachartre
6466836Sachartre static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC;
6476836Sachartre
6486836Sachartre /*
6496836Sachartre * Tunable for backward compatibility. If this variable is set to B_TRUE then
6506836Sachartre * single-slice disks are exported as disks with only one slice instead of
6516836Sachartre * faking a complete disk partitioning.
6526836Sachartre */
6536836Sachartre static boolean_t vd_slice_single_slice = B_FALSE;
6546836Sachartre
6556836Sachartre /*
6562032Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest)
6572032Slm66018 *
6582032Slm66018 * Each supported major version should appear only once, paired with (and only
6592032Slm66018 * with) its highest supported minor version number (as the protocol requires
6602032Slm66018 * supporting all lower minor version numbers as well)
6612032Slm66018 */
6625365Slm66018 static const vio_ver_t vds_version[] = {{1, 1}};
6632032Slm66018 static const size_t vds_num_versions =
6642032Slm66018 sizeof (vds_version)/sizeof (vds_version[0]);
6652032Slm66018
6662793Slm66018 static void vd_free_dring_task(vd_t *vdp);
6673401Snarayan static int vd_setup_vd(vd_t *vd);
6685081Sachartre static int vd_setup_single_slice_disk(vd_t *vd);
6697597SAlexandre.Chartre@Sun.COM static int vd_setup_slice_image(vd_t *vd);
6707597SAlexandre.Chartre@Sun.COM static int vd_setup_disk_image(vd_t *vd);
6717540SRamesh.Chitrothu@Sun.COM static int vd_backend_check_size(vd_t *vd);
6723401Snarayan static boolean_t vd_enabled(vd_t *vd);
6734963Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label);
6747597SAlexandre.Chartre@Sun.COM static int vd_dskimg_validate_geometry(vd_t *vd);
6757597SAlexandre.Chartre@Sun.COM static boolean_t vd_dskimg_is_iso_image(vd_t *vd);
6765365Slm66018 static void vd_set_exported_operations(vd_t *vd);
6775658Sachartre static void vd_reset_access(vd_t *vd);
6785874Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg);
6795874Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **);
6805874Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *);
6816108Sachartre static void vds_driver_types_free(vds_t *vds);
6827563SPrasad.Singamsetty@Sun.COM static void vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom,
6836836Sachartre struct dk_label *label);
6847563SPrasad.Singamsetty@Sun.COM static void vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc,
6856836Sachartre struct dk_geom *geom);
6866836Sachartre static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom);
6877563SPrasad.Singamsetty@Sun.COM static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc);
6886836Sachartre
6896836Sachartre extern int is_pseudo_device(dev_info_t *);
6906836Sachartre
6916836Sachartre /*
6926836Sachartre * Function:
6936836Sachartre * vd_get_readable_size
6946836Sachartre *
6956836Sachartre * Description:
6966836Sachartre * Convert a given size in bytes to a human readable format in
6976836Sachartre * kilobytes, megabytes, gigabytes or terabytes.
6986836Sachartre *
6996836Sachartre * Parameters:
7006836Sachartre * full_size - the size to convert in bytes.
7016836Sachartre * size - the converted size.
7026836Sachartre * unit - the unit of the converted size: 'K' (kilobyte),
7036836Sachartre * 'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte).
7046836Sachartre *
7056836Sachartre * Return Code:
7066836Sachartre * none
7076836Sachartre */
7087597SAlexandre.Chartre@Sun.COM static void
vd_get_readable_size(size_t full_size,size_t * size,char * unit)7096836Sachartre vd_get_readable_size(size_t full_size, size_t *size, char *unit)
7106836Sachartre {
7116836Sachartre if (full_size < (1ULL << 20)) {
7126836Sachartre *size = full_size >> 10;
7136836Sachartre *unit = 'K'; /* Kilobyte */
7146836Sachartre } else if (full_size < (1ULL << 30)) {
7156836Sachartre *size = full_size >> 20;
7166836Sachartre *unit = 'M'; /* Megabyte */
7176836Sachartre } else if (full_size < (1ULL << 40)) {
7186836Sachartre *size = full_size >> 30;
7196836Sachartre *unit = 'G'; /* Gigabyte */
7206836Sachartre } else {
7216836Sachartre *size = full_size >> 40;
7226836Sachartre *unit = 'T'; /* Terabyte */
7236836Sachartre }
7246836Sachartre }
7255081Sachartre
7263782Sachartre /*
7273782Sachartre * Function:
7287597SAlexandre.Chartre@Sun.COM * vd_dskimg_io_params
7293782Sachartre *
7303782Sachartre * Description:
7317597SAlexandre.Chartre@Sun.COM * Convert virtual disk I/O parameters (slice, block, length) to
7327597SAlexandre.Chartre@Sun.COM * (offset, length) relative to the disk image and according to
7337597SAlexandre.Chartre@Sun.COM * the virtual disk partitioning.
7343782Sachartre *
7353782Sachartre * Parameters:
7363782Sachartre * vd - disk on which the operation is performed.
7377597SAlexandre.Chartre@Sun.COM * slice - slice to which is the I/O parameters apply.
7387597SAlexandre.Chartre@Sun.COM * VD_SLICE_NONE indicates that parameters are
7397597SAlexandre.Chartre@Sun.COM * are relative to the entire virtual disk.
7407597SAlexandre.Chartre@Sun.COM * blkp - pointer to the starting block relative to the
7417597SAlexandre.Chartre@Sun.COM * slice; return the starting block relative to
7427597SAlexandre.Chartre@Sun.COM * the disk image.
7437597SAlexandre.Chartre@Sun.COM * lenp - pointer to the number of bytes requested; return
7447597SAlexandre.Chartre@Sun.COM * the number of bytes that can effectively be used.
7453782Sachartre *
7463782Sachartre * Return Code:
7477597SAlexandre.Chartre@Sun.COM * 0 - I/O parameters have been successfully converted;
7487597SAlexandre.Chartre@Sun.COM * blkp and lenp point to the converted values.
7497597SAlexandre.Chartre@Sun.COM * ENODATA - no data are available for the given I/O parameters;
7507597SAlexandre.Chartre@Sun.COM * This occurs if the starting block is past the limit
7517597SAlexandre.Chartre@Sun.COM * of the slice.
7527597SAlexandre.Chartre@Sun.COM * EINVAL - I/O parameters are invalid.
7533782Sachartre */
7547597SAlexandre.Chartre@Sun.COM static int
vd_dskimg_io_params(vd_t * vd,int slice,size_t * blkp,size_t * lenp)7557597SAlexandre.Chartre@Sun.COM vd_dskimg_io_params(vd_t *vd, int slice, size_t *blkp, size_t *lenp)
7563782Sachartre {
7577597SAlexandre.Chartre@Sun.COM size_t blk = *blkp;
7587597SAlexandre.Chartre@Sun.COM size_t len = *lenp;
7597597SAlexandre.Chartre@Sun.COM size_t offset, maxlen;
7607597SAlexandre.Chartre@Sun.COM
7617597SAlexandre.Chartre@Sun.COM ASSERT(vd->file || VD_DSKIMG(vd));
7623782Sachartre ASSERT(len > 0);
7639889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_bsize == DEV_BSIZE);
7643782Sachartre
7655081Sachartre /*
7665081Sachartre * If a file is exported as a slice then we don't care about the vtoc.
7675081Sachartre * In that case, the vtoc is a fake mainly to make newfs happy and we
7685081Sachartre * handle any I/O as a raw disk access so that we can have access to the
7695081Sachartre * entire backend.
7705081Sachartre */
7715081Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) {
7723782Sachartre /* raw disk access */
7733782Sachartre offset = blk * DEV_BSIZE;
7747597SAlexandre.Chartre@Sun.COM if (offset >= vd->dskimg_size) {
7756836Sachartre /* offset past the end of the disk */
7767597SAlexandre.Chartre@Sun.COM PR0("offset (0x%lx) >= size (0x%lx)",
7777597SAlexandre.Chartre@Sun.COM offset, vd->dskimg_size);
7787597SAlexandre.Chartre@Sun.COM return (ENODATA);
7796836Sachartre }
7807597SAlexandre.Chartre@Sun.COM maxlen = vd->dskimg_size - offset;
7813782Sachartre } else {
7823782Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR);
7834963Sachartre
7845365Slm66018 /*
7855365Slm66018 * v1.0 vDisk clients depended on the server not verifying
7865365Slm66018 * the label of a unformatted disk. This "feature" is
7875365Slm66018 * maintained for backward compatibility but all versions
7885365Slm66018 * from v1.1 onwards must do the right thing.
7895365Slm66018 */
7904963Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK &&
7915874Sachartre vio_ver_is_supported(vd->version, 1, 1)) {
7927597SAlexandre.Chartre@Sun.COM (void) vd_dskimg_validate_geometry(vd);
7935874Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
7945874Sachartre PR0("Unknown disk label, can't do I/O "
7955874Sachartre "from slice %d", slice);
7967597SAlexandre.Chartre@Sun.COM return (EINVAL);
7975874Sachartre }
7984963Sachartre }
7994963Sachartre
8005874Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
8015874Sachartre ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE);
8025874Sachartre } else {
8035874Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI);
8045874Sachartre }
8055874Sachartre
8065874Sachartre if (blk >= vd->slices[slice].nblocks) {
8073782Sachartre /* address past the end of the slice */
8086836Sachartre PR0("req_addr (0x%lx) >= psize (0x%lx)",
8095874Sachartre blk, vd->slices[slice].nblocks);
8107597SAlexandre.Chartre@Sun.COM return (ENODATA);
8113782Sachartre }
8123782Sachartre
8135874Sachartre offset = (vd->slices[slice].start + blk) * DEV_BSIZE;
8145874Sachartre maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE;
8156836Sachartre }
8166836Sachartre
8176836Sachartre /*
8186836Sachartre * If the requested size is greater than the size
8196836Sachartre * of the partition, truncate the read/write.
8206836Sachartre */
8216836Sachartre if (len > maxlen) {
8226836Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes",
8236836Sachartre maxlen, len);
8246836Sachartre len = maxlen;
8253782Sachartre }
8263782Sachartre
8273782Sachartre /*
8283782Sachartre * We have to ensure that we are reading/writing into the mmap
8293782Sachartre * range. If we have a partial disk image (e.g. an image of
8303782Sachartre * s0 instead s2) the system can try to access slices that
8313782Sachartre * are not included into the disk image.
8323782Sachartre */
8337597SAlexandre.Chartre@Sun.COM if ((offset + len) > vd->dskimg_size) {
8345874Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) > "
8357597SAlexandre.Chartre@Sun.COM "dskimg_size (0x%lx)", offset, len, vd->dskimg_size);
8367597SAlexandre.Chartre@Sun.COM return (EINVAL);
8377597SAlexandre.Chartre@Sun.COM }
8387597SAlexandre.Chartre@Sun.COM
8397597SAlexandre.Chartre@Sun.COM *blkp = offset / DEV_BSIZE;
8407597SAlexandre.Chartre@Sun.COM *lenp = len;
8417597SAlexandre.Chartre@Sun.COM
8427597SAlexandre.Chartre@Sun.COM return (0);
8437597SAlexandre.Chartre@Sun.COM }
8447597SAlexandre.Chartre@Sun.COM
8457597SAlexandre.Chartre@Sun.COM /*
8467597SAlexandre.Chartre@Sun.COM * Function:
8477597SAlexandre.Chartre@Sun.COM * vd_dskimg_rw
8487597SAlexandre.Chartre@Sun.COM *
8497597SAlexandre.Chartre@Sun.COM * Description:
8507597SAlexandre.Chartre@Sun.COM * Read or write to a disk image. It handles the case where the disk
8517597SAlexandre.Chartre@Sun.COM * image is a file or a volume exported as a full disk or a file
8527597SAlexandre.Chartre@Sun.COM * exported as single-slice disk. Read or write to volumes exported as
8537597SAlexandre.Chartre@Sun.COM * single slice disks are done by directly using the ldi interface.
8547597SAlexandre.Chartre@Sun.COM *
8557597SAlexandre.Chartre@Sun.COM * Parameters:
8567597SAlexandre.Chartre@Sun.COM * vd - disk on which the operation is performed.
8577597SAlexandre.Chartre@Sun.COM * slice - slice on which the operation is performed,
8587597SAlexandre.Chartre@Sun.COM * VD_SLICE_NONE indicates that the operation
8597597SAlexandre.Chartre@Sun.COM * is done using an absolute disk offset.
8607597SAlexandre.Chartre@Sun.COM * operation - operation to execute: read (VD_OP_BREAD) or
8617597SAlexandre.Chartre@Sun.COM * write (VD_OP_BWRITE).
8627597SAlexandre.Chartre@Sun.COM * data - buffer where data are read to or written from.
8637597SAlexandre.Chartre@Sun.COM * blk - starting block for the operation.
8647597SAlexandre.Chartre@Sun.COM * len - number of bytes to read or write.
8657597SAlexandre.Chartre@Sun.COM *
8667597SAlexandre.Chartre@Sun.COM * Return Code:
8677597SAlexandre.Chartre@Sun.COM * n >= 0 - success, n indicates the number of bytes read
8687597SAlexandre.Chartre@Sun.COM * or written.
8697597SAlexandre.Chartre@Sun.COM * -1 - error.
8707597SAlexandre.Chartre@Sun.COM */
8717597SAlexandre.Chartre@Sun.COM static ssize_t
vd_dskimg_rw(vd_t * vd,int slice,int operation,caddr_t data,size_t offset,size_t len)8727597SAlexandre.Chartre@Sun.COM vd_dskimg_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t offset,
8737597SAlexandre.Chartre@Sun.COM size_t len)
8747597SAlexandre.Chartre@Sun.COM {
8757597SAlexandre.Chartre@Sun.COM ssize_t resid;
8767597SAlexandre.Chartre@Sun.COM struct buf buf;
8777597SAlexandre.Chartre@Sun.COM int status;
8787597SAlexandre.Chartre@Sun.COM
8797597SAlexandre.Chartre@Sun.COM ASSERT(vd->file || VD_DSKIMG(vd));
8807597SAlexandre.Chartre@Sun.COM ASSERT(len > 0);
8819889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_bsize == DEV_BSIZE);
8827597SAlexandre.Chartre@Sun.COM
8837597SAlexandre.Chartre@Sun.COM if ((status = vd_dskimg_io_params(vd, slice, &offset, &len)) != 0)
8847597SAlexandre.Chartre@Sun.COM return ((status == ENODATA)? 0: -1);
8857597SAlexandre.Chartre@Sun.COM
8867597SAlexandre.Chartre@Sun.COM if (vd->volume) {
8877597SAlexandre.Chartre@Sun.COM
8887597SAlexandre.Chartre@Sun.COM bioinit(&buf);
8897597SAlexandre.Chartre@Sun.COM buf.b_flags = B_BUSY |
8907597SAlexandre.Chartre@Sun.COM ((operation == VD_OP_BREAD)? B_READ : B_WRITE);
8917597SAlexandre.Chartre@Sun.COM buf.b_bcount = len;
8927791SAlexandre.Chartre@Sun.COM buf.b_lblkno = offset;
8937597SAlexandre.Chartre@Sun.COM buf.b_edev = vd->dev[0];
8947597SAlexandre.Chartre@Sun.COM buf.b_un.b_addr = data;
8957597SAlexandre.Chartre@Sun.COM
8967597SAlexandre.Chartre@Sun.COM /*
8977597SAlexandre.Chartre@Sun.COM * We use ldi_strategy() and not ldi_read()/ldi_write() because
8987597SAlexandre.Chartre@Sun.COM * the read/write functions of the underlying driver may try to
8997597SAlexandre.Chartre@Sun.COM * lock pages of the data buffer, and this requires the data
9007597SAlexandre.Chartre@Sun.COM * buffer to be kmem_alloc'ed (and not allocated on the stack).
9017597SAlexandre.Chartre@Sun.COM *
9027597SAlexandre.Chartre@Sun.COM * Also using ldi_strategy() ensures that writes are immediatly
9037597SAlexandre.Chartre@Sun.COM * commited and not cached as this may be the case with
9047597SAlexandre.Chartre@Sun.COM * ldi_write() (for example with a ZFS volume).
9057597SAlexandre.Chartre@Sun.COM */
9067597SAlexandre.Chartre@Sun.COM if (ldi_strategy(vd->ldi_handle[0], &buf) != 0) {
9077597SAlexandre.Chartre@Sun.COM biofini(&buf);
9087597SAlexandre.Chartre@Sun.COM return (-1);
9097597SAlexandre.Chartre@Sun.COM }
9107597SAlexandre.Chartre@Sun.COM
9117597SAlexandre.Chartre@Sun.COM if (biowait(&buf) != 0) {
9127597SAlexandre.Chartre@Sun.COM biofini(&buf);
9137597SAlexandre.Chartre@Sun.COM return (-1);
9147597SAlexandre.Chartre@Sun.COM }
9157597SAlexandre.Chartre@Sun.COM
9167597SAlexandre.Chartre@Sun.COM resid = buf.b_resid;
9177597SAlexandre.Chartre@Sun.COM biofini(&buf);
9187597SAlexandre.Chartre@Sun.COM
9197597SAlexandre.Chartre@Sun.COM ASSERT(resid <= len);
9207597SAlexandre.Chartre@Sun.COM return (len - resid);
9217597SAlexandre.Chartre@Sun.COM }
9227597SAlexandre.Chartre@Sun.COM
9237597SAlexandre.Chartre@Sun.COM ASSERT(vd->file);
9243782Sachartre
9257791SAlexandre.Chartre@Sun.COM status = vn_rdwr((operation == VD_OP_BREAD)? UIO_READ : UIO_WRITE,
9267791SAlexandre.Chartre@Sun.COM vd->file_vnode, data, len, offset * DEV_BSIZE, UIO_SYSSPACE, FSYNC,
9277791SAlexandre.Chartre@Sun.COM RLIM64_INFINITY, kcred, &resid);
9287791SAlexandre.Chartre@Sun.COM
9297791SAlexandre.Chartre@Sun.COM if (status != 0)
9307791SAlexandre.Chartre@Sun.COM return (-1);
9313782Sachartre
9323782Sachartre return (len);
9333782Sachartre }
9343782Sachartre
9354696Sachartre /*
9364696Sachartre * Function:
9376836Sachartre * vd_build_default_label
9384963Sachartre *
9394963Sachartre * Description:
9406836Sachartre * Return a default label for a given disk size. This is used when the disk
9414963Sachartre * does not have a valid VTOC so that the user can get a valid default
9425365Slm66018 * configuration. The default label has all slice sizes set to 0 (except
9434963Sachartre * slice 2 which is the entire disk) to force the user to write a valid
9444963Sachartre * label onto the disk image.
9454963Sachartre *
9464963Sachartre * Parameters:
9476836Sachartre * disk_size - the disk size in bytes
9489889SLarry.Liu@Sun.COM * bsize - the disk block size in bytes
9494963Sachartre * label - the returned default label.
9504963Sachartre *
9514963Sachartre * Return Code:
9524963Sachartre * none.
9534963Sachartre */
9544963Sachartre static void
vd_build_default_label(size_t disk_size,size_t bsize,struct dk_label * label)9559889SLarry.Liu@Sun.COM vd_build_default_label(size_t disk_size, size_t bsize, struct dk_label *label)
9564963Sachartre {
9574963Sachartre size_t size;
9586836Sachartre char unit;
9595874Sachartre
96010063SAlexandre.Chartre@Sun.COM ASSERT(bsize > 0);
96110063SAlexandre.Chartre@Sun.COM
9625874Sachartre bzero(label, sizeof (struct dk_label));
9634963Sachartre
9644963Sachartre /*
9657563SPrasad.Singamsetty@Sun.COM * Ideally we would like the cylinder size (nsect * nhead) to be the
9667563SPrasad.Singamsetty@Sun.COM * same whatever the disk size is. That way the VTOC label could be
9677563SPrasad.Singamsetty@Sun.COM * easily updated in case the disk size is increased (keeping the
9687563SPrasad.Singamsetty@Sun.COM * same cylinder size allows to preserve the existing partitioning
9697563SPrasad.Singamsetty@Sun.COM * when updating the VTOC label). But it is not possible to have
9707563SPrasad.Singamsetty@Sun.COM * a fixed cylinder size and to cover all disk size.
9717563SPrasad.Singamsetty@Sun.COM *
9727563SPrasad.Singamsetty@Sun.COM * So we define different cylinder sizes depending on the disk size.
9737563SPrasad.Singamsetty@Sun.COM * The cylinder size is chosen so that we don't have too few cylinders
9747563SPrasad.Singamsetty@Sun.COM * for a small disk image, or so many on a big disk image that you
9757563SPrasad.Singamsetty@Sun.COM * waste space for backup superblocks or cylinder group structures.
9767563SPrasad.Singamsetty@Sun.COM * Also we must have a resonable number of cylinders and sectors so
9774963Sachartre * that newfs can run using default values.
9784963Sachartre *
9797563SPrasad.Singamsetty@Sun.COM * +-----------+--------+---------+--------+
9807563SPrasad.Singamsetty@Sun.COM * | disk_size | < 2MB | 2MB-4GB | >= 8GB |
9817563SPrasad.Singamsetty@Sun.COM * +-----------+--------+---------+--------+
9827563SPrasad.Singamsetty@Sun.COM * | nhead | 1 | 1 | 96 |
9837563SPrasad.Singamsetty@Sun.COM * | nsect | 200 | 600 | 768 |
9847563SPrasad.Singamsetty@Sun.COM * +-----------+--------+---------+--------+
9854963Sachartre *
9867563SPrasad.Singamsetty@Sun.COM * Other parameters are computed from these values:
9877563SPrasad.Singamsetty@Sun.COM *
9887563SPrasad.Singamsetty@Sun.COM * pcyl = disk_size / (nhead * nsect * 512)
9897563SPrasad.Singamsetty@Sun.COM * acyl = (pcyl > 2)? 2 : 0
9907563SPrasad.Singamsetty@Sun.COM * ncyl = pcyl - acyl
9914963Sachartre *
9927563SPrasad.Singamsetty@Sun.COM * The maximum number of cylinder is 65535 so this allows to define a
9937563SPrasad.Singamsetty@Sun.COM * geometry for a disk size up to 65535 * 96 * 768 * 512 = 2.24 TB
9947563SPrasad.Singamsetty@Sun.COM * which is more than enough to cover the maximum size allowed by the
9957563SPrasad.Singamsetty@Sun.COM * extended VTOC format (2TB).
9964963Sachartre */
9977563SPrasad.Singamsetty@Sun.COM
9987563SPrasad.Singamsetty@Sun.COM if (disk_size >= 8 * ONE_GIGABYTE) {
9997563SPrasad.Singamsetty@Sun.COM
10007563SPrasad.Singamsetty@Sun.COM label->dkl_nhead = 96;
10017563SPrasad.Singamsetty@Sun.COM label->dkl_nsect = 768;
10027563SPrasad.Singamsetty@Sun.COM
10037563SPrasad.Singamsetty@Sun.COM } else if (disk_size >= 2 * ONE_MEGABYTE) {
10047563SPrasad.Singamsetty@Sun.COM
10057563SPrasad.Singamsetty@Sun.COM label->dkl_nhead = 1;
10067563SPrasad.Singamsetty@Sun.COM label->dkl_nsect = 600;
10077563SPrasad.Singamsetty@Sun.COM
10087563SPrasad.Singamsetty@Sun.COM } else {
10097563SPrasad.Singamsetty@Sun.COM
10107563SPrasad.Singamsetty@Sun.COM label->dkl_nhead = 1;
10117563SPrasad.Singamsetty@Sun.COM label->dkl_nsect = 200;
10127563SPrasad.Singamsetty@Sun.COM }
10137563SPrasad.Singamsetty@Sun.COM
10147563SPrasad.Singamsetty@Sun.COM label->dkl_pcyl = disk_size /
10159889SLarry.Liu@Sun.COM (label->dkl_nsect * label->dkl_nhead * bsize);
10164963Sachartre
10174963Sachartre if (label->dkl_pcyl == 0)
10184963Sachartre label->dkl_pcyl = 1;
10194963Sachartre
10205081Sachartre label->dkl_acyl = 0;
10215081Sachartre
10225874Sachartre if (label->dkl_pcyl > 2)
10235874Sachartre label->dkl_acyl = 2;
10244963Sachartre
10254963Sachartre label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl;
10264963Sachartre label->dkl_write_reinstruct = 0;
10274963Sachartre label->dkl_read_reinstruct = 0;
10284963Sachartre label->dkl_rpm = 7200;
10294963Sachartre label->dkl_apc = 0;
10304963Sachartre label->dkl_intrlv = 0;
10314963Sachartre
10326836Sachartre PR0("requested disk size: %ld bytes\n", disk_size);
10334963Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl,
10344963Sachartre label->dkl_nhead, label->dkl_nsect);
10354963Sachartre PR0("provided disk size: %ld bytes\n", (uint64_t)
10364963Sachartre (label->dkl_pcyl * label->dkl_nhead *
10379889SLarry.Liu@Sun.COM label->dkl_nsect * bsize));
10384963Sachartre
10396836Sachartre vd_get_readable_size(disk_size, &size, &unit);
10404963Sachartre
10414963Sachartre /*
10424963Sachartre * We must have a correct label name otherwise format(1m) will
10434963Sachartre * not recognized the disk as labeled.
10444963Sachartre */
10454963Sachartre (void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII,
10464963Sachartre "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d",
10476836Sachartre size, unit,
10484963Sachartre label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead,
10494963Sachartre label->dkl_nsect);
10504963Sachartre
10514963Sachartre /* default VTOC */
10527563SPrasad.Singamsetty@Sun.COM label->dkl_vtoc.v_version = V_EXTVERSION;
10535874Sachartre label->dkl_vtoc.v_nparts = V_NUMPAR;
10544963Sachartre label->dkl_vtoc.v_sanity = VTOC_SANE;
10555874Sachartre label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
10565874Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0;
10575874Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl *
10584963Sachartre label->dkl_nhead * label->dkl_nsect;
10595874Sachartre label->dkl_magic = DKL_MAGIC;
10604963Sachartre label->dkl_cksum = vd_lbl2cksum(label);
10614963Sachartre }
10624963Sachartre
10634963Sachartre /*
10644963Sachartre * Function:
10657597SAlexandre.Chartre@Sun.COM * vd_dskimg_set_vtoc
10664696Sachartre *
10674696Sachartre * Description:
10684696Sachartre * Set the vtoc of a disk image by writing the label and backup
10694696Sachartre * labels into the disk image backend.
10704696Sachartre *
10714696Sachartre * Parameters:
10724696Sachartre * vd - disk on which the operation is performed.
10734696Sachartre * label - the data to be written.
10744696Sachartre *
10754696Sachartre * Return Code:
10764696Sachartre * 0 - success.
10774696Sachartre * n > 0 - error, n indicates the errno code.
10784696Sachartre */
10794696Sachartre static int
vd_dskimg_set_vtoc(vd_t * vd,struct dk_label * label)10807597SAlexandre.Chartre@Sun.COM vd_dskimg_set_vtoc(vd_t *vd, struct dk_label *label)
10814696Sachartre {
10827563SPrasad.Singamsetty@Sun.COM size_t blk, sec, cyl, head, cnt;
10834696Sachartre
10847597SAlexandre.Chartre@Sun.COM ASSERT(VD_DSKIMG(vd));
10857597SAlexandre.Chartre@Sun.COM
10867597SAlexandre.Chartre@Sun.COM if (VD_DSKIMG_LABEL_WRITE(vd, label) < 0) {
10874696Sachartre PR0("fail to write disk label");
10884696Sachartre return (EIO);
10894696Sachartre }
10904696Sachartre
10914696Sachartre /*
10924696Sachartre * Backup labels are on the last alternate cylinder's
10934696Sachartre * first five odd sectors.
10944696Sachartre */
10954696Sachartre if (label->dkl_acyl == 0) {
10964696Sachartre PR0("no alternate cylinder, can not store backup labels");
10974696Sachartre return (0);
10984696Sachartre }
10994696Sachartre
11004696Sachartre cyl = label->dkl_ncyl + label->dkl_acyl - 1;
11014696Sachartre head = label->dkl_nhead - 1;
11024696Sachartre
11034696Sachartre blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) +
11044696Sachartre (head * label->dkl_nsect);
11054696Sachartre
11064696Sachartre /*
11074696Sachartre * Write the backup labels. Make sure we don't try to write past
11084696Sachartre * the last cylinder.
11094696Sachartre */
11104696Sachartre sec = 1;
11114696Sachartre
11127597SAlexandre.Chartre@Sun.COM for (cnt = 0; cnt < VD_DSKIMG_NUM_BACKUP; cnt++) {
11134696Sachartre
11144696Sachartre if (sec >= label->dkl_nsect) {
11154696Sachartre PR0("not enough sector to store all backup labels");
11164696Sachartre return (0);
11174696Sachartre }
11184696Sachartre
11197597SAlexandre.Chartre@Sun.COM if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
11207597SAlexandre.Chartre@Sun.COM (caddr_t)label, blk + sec, sizeof (struct dk_label)) < 0) {
11217563SPrasad.Singamsetty@Sun.COM PR0("error writing backup label at block %lu\n",
11224696Sachartre blk + sec);
11234696Sachartre return (EIO);
11244696Sachartre }
11254696Sachartre
11267563SPrasad.Singamsetty@Sun.COM PR1("wrote backup label at block %lu\n", blk + sec);
11274696Sachartre
11284696Sachartre sec += 2;
11294696Sachartre }
11304696Sachartre
11314696Sachartre return (0);
11324696Sachartre }
11334696Sachartre
11344696Sachartre /*
11354696Sachartre * Function:
11367597SAlexandre.Chartre@Sun.COM * vd_dskimg_get_devid_block
11374696Sachartre *
11384696Sachartre * Description:
11394696Sachartre * Return the block number where the device id is stored.
11404696Sachartre *
11414696Sachartre * Parameters:
11424696Sachartre * vd - disk on which the operation is performed.
11434696Sachartre * blkp - pointer to the block number
11444696Sachartre *
11454696Sachartre * Return Code:
11464696Sachartre * 0 - success
11474696Sachartre * ENOSPC - disk has no space to store a device id
11484696Sachartre */
11494696Sachartre static int
vd_dskimg_get_devid_block(vd_t * vd,size_t * blkp)11507597SAlexandre.Chartre@Sun.COM vd_dskimg_get_devid_block(vd_t *vd, size_t *blkp)
11514696Sachartre {
11524696Sachartre diskaddr_t spc, head, cyl;
11534696Sachartre
11547597SAlexandre.Chartre@Sun.COM ASSERT(VD_DSKIMG(vd));
11555874Sachartre
11565874Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) {
11575874Sachartre /*
11585874Sachartre * If no label is defined we don't know where to find
11595874Sachartre * a device id.
11605874Sachartre */
11615874Sachartre return (ENOSPC);
11625874Sachartre }
11635874Sachartre
11645874Sachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
11655874Sachartre /*
11665874Sachartre * For an EFI disk, the devid is at the beginning of
11675874Sachartre * the reserved slice
11685874Sachartre */
11695874Sachartre if (vd->efi_reserved == -1) {
11705874Sachartre PR0("EFI disk has no reserved slice");
11715874Sachartre return (ENOSPC);
11725874Sachartre }
11735874Sachartre
11745874Sachartre *blkp = vd->slices[vd->efi_reserved].start;
11755874Sachartre return (0);
11765874Sachartre }
11775874Sachartre
11784696Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
11794696Sachartre
11804696Sachartre /* this geometry doesn't allow us to have a devid */
11814696Sachartre if (vd->dk_geom.dkg_acyl < 2) {
11824696Sachartre PR0("not enough alternate cylinder available for devid "
11834696Sachartre "(acyl=%u)", vd->dk_geom.dkg_acyl);
11844696Sachartre return (ENOSPC);
11854696Sachartre }
11864696Sachartre
11874696Sachartre /* the devid is in on the track next to the last cylinder */
11884696Sachartre cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2;
11894696Sachartre spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
11904696Sachartre head = vd->dk_geom.dkg_nhead - 1;
11914696Sachartre
11924696Sachartre *blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) +
11934696Sachartre (head * vd->dk_geom.dkg_nsect) + 1;
11944696Sachartre
11954696Sachartre return (0);
11964696Sachartre }
11974696Sachartre
11984696Sachartre /*
11994696Sachartre * Return the checksum of a disk block containing an on-disk devid.
12004696Sachartre */
12014696Sachartre static uint_t
vd_dkdevid2cksum(struct dk_devid * dkdevid)12024696Sachartre vd_dkdevid2cksum(struct dk_devid *dkdevid)
12034696Sachartre {
12044696Sachartre uint_t chksum, *ip;
12054696Sachartre int i;
12064696Sachartre
12074696Sachartre chksum = 0;
12087563SPrasad.Singamsetty@Sun.COM ip = (void *)dkdevid;
12094696Sachartre for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++)
12104696Sachartre chksum ^= ip[i];
12114696Sachartre
12124696Sachartre return (chksum);
12134696Sachartre }
12144696Sachartre
12154696Sachartre /*
12164696Sachartre * Function:
12177597SAlexandre.Chartre@Sun.COM * vd_dskimg_read_devid
12184696Sachartre *
12194696Sachartre * Description:
12204696Sachartre * Read the device id stored on a disk image.
12214696Sachartre *
12224696Sachartre * Parameters:
12234696Sachartre * vd - disk on which the operation is performed.
12244696Sachartre * devid - the return address of the device ID.
12254696Sachartre *
12264696Sachartre * Return Code:
12274696Sachartre * 0 - success
12284696Sachartre * EIO - I/O error while trying to access the disk image
12294696Sachartre * EINVAL - no valid device id was found
12304696Sachartre * ENOSPC - disk has no space to store a device id
12314696Sachartre */
12324696Sachartre static int
vd_dskimg_read_devid(vd_t * vd,ddi_devid_t * devid)12337597SAlexandre.Chartre@Sun.COM vd_dskimg_read_devid(vd_t *vd, ddi_devid_t *devid)
12344696Sachartre {
12354696Sachartre struct dk_devid *dkdevid;
12364696Sachartre size_t blk;
12374696Sachartre uint_t chksum;
12384696Sachartre int status, sz;
12394696Sachartre
12409889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_bsize == DEV_BSIZE);
12419889SLarry.Liu@Sun.COM
12427597SAlexandre.Chartre@Sun.COM if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0)
12434696Sachartre return (status);
12444696Sachartre
12454696Sachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
12464696Sachartre
12474696Sachartre /* get the devid */
12487597SAlexandre.Chartre@Sun.COM if ((vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk,
12494696Sachartre DEV_BSIZE)) < 0) {
12504696Sachartre PR0("error reading devid block at %lu", blk);
12514696Sachartre status = EIO;
12524696Sachartre goto done;
12534696Sachartre }
12544696Sachartre
12554696Sachartre /* validate the revision */
12564696Sachartre if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) ||
12574696Sachartre (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) {
12584696Sachartre PR0("invalid devid found at block %lu (bad revision)", blk);
12594696Sachartre status = EINVAL;
12604696Sachartre goto done;
12614696Sachartre }
12624696Sachartre
12634696Sachartre /* compute checksum */
12644696Sachartre chksum = vd_dkdevid2cksum(dkdevid);
12654696Sachartre
12664696Sachartre /* compare the checksums */
12674696Sachartre if (DKD_GETCHKSUM(dkdevid) != chksum) {
12684696Sachartre PR0("invalid devid found at block %lu (bad checksum)", blk);
12694696Sachartre status = EINVAL;
12704696Sachartre goto done;
12714696Sachartre }
12724696Sachartre
12734696Sachartre /* validate the device id */
12744696Sachartre if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) {
12754696Sachartre PR0("invalid devid found at block %lu", blk);
12764696Sachartre status = EINVAL;
12774696Sachartre goto done;
12784696Sachartre }
12794696Sachartre
12804696Sachartre PR1("devid read at block %lu", blk);
12814696Sachartre
12824696Sachartre sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid);
12834696Sachartre *devid = kmem_alloc(sz, KM_SLEEP);
12844696Sachartre bcopy(&dkdevid->dkd_devid, *devid, sz);
12854696Sachartre
12864696Sachartre done:
12874696Sachartre kmem_free(dkdevid, DEV_BSIZE);
12884696Sachartre return (status);
12894696Sachartre
12904696Sachartre }
12914696Sachartre
12924696Sachartre /*
12934696Sachartre * Function:
12947597SAlexandre.Chartre@Sun.COM * vd_dskimg_write_devid
12954696Sachartre *
12964696Sachartre * Description:
12974696Sachartre * Write a device id into disk image.
12984696Sachartre *
12994696Sachartre * Parameters:
13004696Sachartre * vd - disk on which the operation is performed.
13014696Sachartre * devid - the device ID to store.
13024696Sachartre *
13034696Sachartre * Return Code:
13044696Sachartre * 0 - success
13054696Sachartre * EIO - I/O error while trying to access the disk image
13064696Sachartre * ENOSPC - disk has no space to store a device id
13074696Sachartre */
13084696Sachartre static int
vd_dskimg_write_devid(vd_t * vd,ddi_devid_t devid)13097597SAlexandre.Chartre@Sun.COM vd_dskimg_write_devid(vd_t *vd, ddi_devid_t devid)
13104696Sachartre {
13114696Sachartre struct dk_devid *dkdevid;
13124696Sachartre uint_t chksum;
13134696Sachartre size_t blk;
13144696Sachartre int status;
13154696Sachartre
13169889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_bsize == DEV_BSIZE);
13179889SLarry.Liu@Sun.COM
13185874Sachartre if (devid == NULL) {
13195874Sachartre /* nothing to write */
13205874Sachartre return (0);
13215874Sachartre }
13225874Sachartre
13237597SAlexandre.Chartre@Sun.COM if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0)
13244696Sachartre return (status);
13254696Sachartre
13264696Sachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
13274696Sachartre
13284696Sachartre /* set revision */
13294696Sachartre dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB;
13304696Sachartre dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB;
13314696Sachartre
13324696Sachartre /* copy devid */
13334696Sachartre bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid));
13344696Sachartre
13354696Sachartre /* compute checksum */
13364696Sachartre chksum = vd_dkdevid2cksum(dkdevid);
13374696Sachartre
13384696Sachartre /* set checksum */
13394696Sachartre DKD_FORMCHKSUM(chksum, dkdevid);
13404696Sachartre
13414696Sachartre /* store the devid */
13427597SAlexandre.Chartre@Sun.COM if ((status = vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
13434696Sachartre (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) {
13444696Sachartre PR0("Error writing devid block at %lu", blk);
13454696Sachartre status = EIO;
13464696Sachartre } else {
13474696Sachartre PR1("devid written at block %lu", blk);
13484696Sachartre status = 0;
13494696Sachartre }
13504696Sachartre
13514696Sachartre kmem_free(dkdevid, DEV_BSIZE);
13524696Sachartre return (status);
13534696Sachartre }
13544696Sachartre
13554696Sachartre /*
13564696Sachartre * Function:
13575365Slm66018 * vd_do_scsi_rdwr
13584696Sachartre *
13594696Sachartre * Description:
13604696Sachartre * Read or write to a SCSI disk using an absolute disk offset.
13614696Sachartre *
13624696Sachartre * Parameters:
13634696Sachartre * vd - disk on which the operation is performed.
13644696Sachartre * operation - operation to execute: read (VD_OP_BREAD) or
13654696Sachartre * write (VD_OP_BWRITE).
13664696Sachartre * data - buffer where data are read to or written from.
13674696Sachartre * blk - starting block for the operation.
13684696Sachartre * len - number of bytes to read or write.
13694696Sachartre *
13704696Sachartre * Return Code:
13714696Sachartre * 0 - success
13724696Sachartre * n != 0 - error.
13734696Sachartre */
13744696Sachartre static int
vd_do_scsi_rdwr(vd_t * vd,int operation,caddr_t data,size_t blk,size_t len)13755365Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len)
13764696Sachartre {
13774696Sachartre struct uscsi_cmd ucmd;
13784696Sachartre union scsi_cdb cdb;
13794696Sachartre int nsectors, nblk;
13804696Sachartre int max_sectors;
13814696Sachartre int status, rval;
13824696Sachartre
13834696Sachartre ASSERT(!vd->file);
13847597SAlexandre.Chartre@Sun.COM ASSERT(!vd->volume);
13859889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_bsize > 0);
13864696Sachartre
13874696Sachartre max_sectors = vd->max_xfer_sz;
13889889SLarry.Liu@Sun.COM nblk = (len / vd->vdisk_bsize);
13899889SLarry.Liu@Sun.COM
13909889SLarry.Liu@Sun.COM if (len % vd->vdisk_bsize != 0)
13914696Sachartre return (EINVAL);
13924696Sachartre
13934696Sachartre /*
13944696Sachartre * Build and execute the uscsi ioctl. We build a group0, group1
13954696Sachartre * or group4 command as necessary, since some targets
13964696Sachartre * do not support group1 commands.
13974696Sachartre */
13984696Sachartre while (nblk) {
13994696Sachartre
14004696Sachartre bzero(&ucmd, sizeof (ucmd));
14014696Sachartre bzero(&cdb, sizeof (cdb));
14024696Sachartre
14034696Sachartre nsectors = (max_sectors < nblk) ? max_sectors : nblk;
14044696Sachartre
14055365Slm66018 /*
14065365Slm66018 * Some of the optical drives on sun4v machines are ATAPI
14075365Slm66018 * devices which use Group 1 Read/Write commands so we need
14085365Slm66018 * to explicitly check a flag which is set when a domain
14095365Slm66018 * is bound.
14105365Slm66018 */
14115365Slm66018 if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) {
14124696Sachartre FORMG0ADDR(&cdb, blk);
14137563SPrasad.Singamsetty@Sun.COM FORMG0COUNT(&cdb, (uchar_t)nsectors);
14144696Sachartre ucmd.uscsi_cdblen = CDB_GROUP0;
14154696Sachartre } else if (blk > 0xffffffff) {
14164696Sachartre FORMG4LONGADDR(&cdb, blk);
14174696Sachartre FORMG4COUNT(&cdb, nsectors);
14184696Sachartre ucmd.uscsi_cdblen = CDB_GROUP4;
14194696Sachartre cdb.scc_cmd |= SCMD_GROUP4;
14204696Sachartre } else {
14214696Sachartre FORMG1ADDR(&cdb, blk);
14224696Sachartre FORMG1COUNT(&cdb, nsectors);
14234696Sachartre ucmd.uscsi_cdblen = CDB_GROUP1;
14244696Sachartre cdb.scc_cmd |= SCMD_GROUP1;
14254696Sachartre }
14264696Sachartre ucmd.uscsi_cdb = (caddr_t)&cdb;
14274696Sachartre ucmd.uscsi_bufaddr = data;
14289889SLarry.Liu@Sun.COM ucmd.uscsi_buflen = nsectors * vd->backend_bsize;
14294696Sachartre ucmd.uscsi_timeout = vd_scsi_rdwr_timeout;
14304696Sachartre /*
14314696Sachartre * Set flags so that the command is isolated from normal
14324696Sachartre * commands and no error message is printed.
14334696Sachartre */
14344696Sachartre ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT;
14354696Sachartre
14364696Sachartre if (operation == VD_OP_BREAD) {
14374696Sachartre cdb.scc_cmd |= SCMD_READ;
14384696Sachartre ucmd.uscsi_flags |= USCSI_READ;
14394696Sachartre } else {
14404696Sachartre cdb.scc_cmd |= SCMD_WRITE;
14414696Sachartre }
14424696Sachartre
14434696Sachartre status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE],
14445081Sachartre USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL),
14454696Sachartre kcred, &rval);
14464696Sachartre
14474696Sachartre if (status == 0)
14484696Sachartre status = ucmd.uscsi_status;
14494696Sachartre
14504696Sachartre if (status != 0)
14514696Sachartre break;
14524696Sachartre
14534696Sachartre /*
14544696Sachartre * Check if partial DMA breakup is required. If so, reduce
14554696Sachartre * the request size by half and retry the last request.
14564696Sachartre */
14574696Sachartre if (ucmd.uscsi_resid == ucmd.uscsi_buflen) {
14584696Sachartre max_sectors >>= 1;
14594696Sachartre if (max_sectors <= 0) {
14604696Sachartre status = EIO;
14614696Sachartre break;
14624696Sachartre }
14634696Sachartre continue;
14644696Sachartre }
14654696Sachartre
14664696Sachartre if (ucmd.uscsi_resid != 0) {
14674696Sachartre status = EIO;
14684696Sachartre break;
14694696Sachartre }
14704696Sachartre
14714696Sachartre blk += nsectors;
14724696Sachartre nblk -= nsectors;
14739889SLarry.Liu@Sun.COM data += nsectors * vd->vdisk_bsize;
14744696Sachartre }
14754696Sachartre
14764696Sachartre return (status);
14774696Sachartre }
14784696Sachartre
14794838Slm66018 /*
14805365Slm66018 * Function:
14815365Slm66018 * vd_scsi_rdwr
14825365Slm66018 *
14835365Slm66018 * Description:
14845365Slm66018 * Wrapper function to read or write to a SCSI disk using an absolute
14855365Slm66018 * disk offset. It checks the blocksize of the underlying device and,
14865365Slm66018 * if necessary, adjusts the buffers accordingly before calling
14875365Slm66018 * vd_do_scsi_rdwr() to do the actual read or write.
14885365Slm66018 *
14895365Slm66018 * Parameters:
14905365Slm66018 * vd - disk on which the operation is performed.
14915365Slm66018 * operation - operation to execute: read (VD_OP_BREAD) or
14925365Slm66018 * write (VD_OP_BWRITE).
14935365Slm66018 * data - buffer where data are read to or written from.
14945365Slm66018 * blk - starting block for the operation.
14955365Slm66018 * len - number of bytes to read or write.
14965365Slm66018 *
14975365Slm66018 * Return Code:
14985365Slm66018 * 0 - success
14995365Slm66018 * n != 0 - error.
15005365Slm66018 */
15015365Slm66018 static int
vd_scsi_rdwr(vd_t * vd,int operation,caddr_t data,size_t vblk,size_t vlen)15025365Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen)
15035365Slm66018 {
15045365Slm66018 int rv;
15055365Slm66018
15065365Slm66018 size_t pblk; /* physical device block number of data on device */
15075365Slm66018 size_t delta; /* relative offset between pblk and vblk */
15085365Slm66018 size_t pnblk; /* number of physical blocks to be read from device */
15095365Slm66018 size_t plen; /* length of data to be read from physical device */
15105365Slm66018 char *buf; /* buffer area to fit physical device's block size */
15115365Slm66018
15129889SLarry.Liu@Sun.COM if (vd->backend_bsize == 0) {
15135658Sachartre /*
15145658Sachartre * The block size was not available during the attach,
15155658Sachartre * try to update it now.
15165658Sachartre */
15177540SRamesh.Chitrothu@Sun.COM if (vd_backend_check_size(vd) != 0)
15185658Sachartre return (EIO);
15195658Sachartre }
15205658Sachartre
15215365Slm66018 /*
15225365Slm66018 * If the vdisk block size and the block size of the underlying device
15235365Slm66018 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need
15245365Slm66018 * to create a buffer large enough to handle the device's block size
15255365Slm66018 * and adjust the block to be read from and the amount of data to
15265365Slm66018 * read to correspond with the device's block size.
15275365Slm66018 */
15289889SLarry.Liu@Sun.COM if (vd->vdisk_bsize == vd->backend_bsize)
15295365Slm66018 return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen));
15305365Slm66018
15319889SLarry.Liu@Sun.COM if (vd->vdisk_bsize > vd->backend_bsize)
15325365Slm66018 return (EINVAL);
15335365Slm66018
15345365Slm66018 /*
15355365Slm66018 * Writing of physical block sizes larger than the virtual block size
15365365Slm66018 * is not supported. This would be added if/when support for guests
15375365Slm66018 * writing to DVDs is implemented.
15385365Slm66018 */
15395365Slm66018 if (operation == VD_OP_BWRITE)
15405365Slm66018 return (ENOTSUP);
15415365Slm66018
15425365Slm66018 /* BEGIN CSTYLED */
15435365Slm66018 /*
15445365Slm66018 * Below is a diagram showing the relationship between the physical
15455365Slm66018 * and virtual blocks. If the virtual blocks marked by 'X' below are
15465365Slm66018 * requested, then the physical blocks denoted by 'Y' are read.
15475365Slm66018 *
15485365Slm66018 * vblk
15495365Slm66018 * | vlen
15505365Slm66018 * |<--------------->|
15515365Slm66018 * v v
15525365Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- virtual disk:
15535365Slm66018 * | | | |XX|XX|XX|XX|XX|XX| | | | | | } block size is
15549889SLarry.Liu@Sun.COM * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- vd->vdisk_bsize
15555365Slm66018 * : : : :
15565365Slm66018 * >:==:< delta : :
15575365Slm66018 * : : : :
15585365Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- physical disk:
15595365Slm66018 * | |YY:YY|YYYYY|YYYYY|YY:YY| | | } block size is
15609889SLarry.Liu@Sun.COM * --+-----+-----+-----+-----+-----+-----+-----+-- vd->backend_bsize
15615365Slm66018 * ^ ^
15625365Slm66018 * |<--------------------->|
15635365Slm66018 * | plen
15645365Slm66018 * pblk
15655365Slm66018 */
15665365Slm66018 /* END CSTYLED */
15679889SLarry.Liu@Sun.COM pblk = (vblk * vd->vdisk_bsize) / vd->backend_bsize;
15689889SLarry.Liu@Sun.COM delta = (vblk * vd->vdisk_bsize) - (pblk * vd->backend_bsize);
15699889SLarry.Liu@Sun.COM pnblk = ((delta + vlen - 1) / vd->backend_bsize) + 1;
15709889SLarry.Liu@Sun.COM plen = pnblk * vd->backend_bsize;
15715365Slm66018
15725365Slm66018 PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen);
15735365Slm66018
15745365Slm66018 buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP);
15755365Slm66018 rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen);
15765365Slm66018 bcopy(buf + delta, data, vlen);
15775365Slm66018
15785365Slm66018 kmem_free(buf, sizeof (caddr_t) * plen);
15795365Slm66018
15805365Slm66018 return (rv);
15815365Slm66018 }
15825365Slm66018
15835365Slm66018 /*
15846836Sachartre * Function:
15856836Sachartre * vd_slice_flabel_read
15866836Sachartre *
15876836Sachartre * Description:
15886836Sachartre * This function simulates a read operation from the fake label of
15896836Sachartre * a single-slice disk.
15906836Sachartre *
15916836Sachartre * Parameters:
15926836Sachartre * vd - single-slice disk to read from
15936836Sachartre * data - buffer where data should be read to
15946836Sachartre * offset - offset in byte where the read should start
15956836Sachartre * length - number of bytes to read
15966836Sachartre *
15976836Sachartre * Return Code:
15986836Sachartre * n >= 0 - success, n indicates the number of bytes read
15996836Sachartre * -1 - error
16006836Sachartre */
16016836Sachartre static ssize_t
vd_slice_flabel_read(vd_t * vd,caddr_t data,size_t offset,size_t length)16026836Sachartre vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length)
16036836Sachartre {
16046836Sachartre size_t n = 0;
16059889SLarry.Liu@Sun.COM uint_t limit = vd->flabel_limit * vd->vdisk_bsize;
16066836Sachartre
16076836Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
16086836Sachartre ASSERT(vd->flabel != NULL);
16096836Sachartre
16106836Sachartre /* if offset is past the fake label limit there's nothing to read */
16116836Sachartre if (offset >= limit)
16126836Sachartre return (0);
16136836Sachartre
16146836Sachartre /* data with offset 0 to flabel_size are read from flabel */
16156836Sachartre if (offset < vd->flabel_size) {
16166836Sachartre
16176836Sachartre if (offset + length <= vd->flabel_size) {
16186836Sachartre bcopy(vd->flabel + offset, data, length);
16196836Sachartre return (length);
16206836Sachartre }
16216836Sachartre
16226836Sachartre n = vd->flabel_size - offset;
16236836Sachartre bcopy(vd->flabel + offset, data, n);
16246836Sachartre data += n;
16256836Sachartre }
16266836Sachartre
16276836Sachartre /* data with offset from flabel_size to flabel_limit are all zeros */
16286836Sachartre if (offset + length <= limit) {
16296836Sachartre bzero(data, length - n);
16306836Sachartre return (length);
16316836Sachartre }
16326836Sachartre
16336836Sachartre bzero(data, limit - offset - n);
16346836Sachartre return (limit - offset);
16356836Sachartre }
16366836Sachartre
16376836Sachartre /*
16386836Sachartre * Function:
16396836Sachartre * vd_slice_flabel_write
16406836Sachartre *
16416836Sachartre * Description:
16426836Sachartre * This function simulates a write operation to the fake label of
16436836Sachartre * a single-slice disk. Write operations are actually faked and return
16446836Sachartre * success although the label is never changed. This is mostly to
16456836Sachartre * simulate a successful label update.
16466836Sachartre *
16476836Sachartre * Parameters:
16486836Sachartre * vd - single-slice disk to write to
16496836Sachartre * data - buffer where data should be written from
16506836Sachartre * offset - offset in byte where the write should start
16516836Sachartre * length - number of bytes to written
16526836Sachartre *
16536836Sachartre * Return Code:
16546836Sachartre * n >= 0 - success, n indicates the number of bytes written
16556836Sachartre * -1 - error
16566836Sachartre */
16576836Sachartre static ssize_t
vd_slice_flabel_write(vd_t * vd,caddr_t data,size_t offset,size_t length)16586836Sachartre vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length)
16596836Sachartre {
16609889SLarry.Liu@Sun.COM uint_t limit = vd->flabel_limit * vd->vdisk_bsize;
16616836Sachartre struct dk_label *label;
16626836Sachartre struct dk_geom geom;
16637563SPrasad.Singamsetty@Sun.COM struct extvtoc vtoc;
16646836Sachartre
16656836Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
16666836Sachartre ASSERT(vd->flabel != NULL);
16676836Sachartre
16686836Sachartre if (offset >= limit)
16696836Sachartre return (0);
16706836Sachartre
16716836Sachartre /*
16726836Sachartre * If this is a request to overwrite the VTOC disk label, check that
16736836Sachartre * the new label is similar to the previous one and return that the
16746836Sachartre * write was successful, but note that nothing is actually overwritten.
16756836Sachartre */
16766836Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
16779889SLarry.Liu@Sun.COM offset == 0 && length == vd->vdisk_bsize) {
16787563SPrasad.Singamsetty@Sun.COM label = (void *)data;
16796836Sachartre
16806836Sachartre /* check that this is a valid label */
16816836Sachartre if (label->dkl_magic != DKL_MAGIC ||
16826836Sachartre label->dkl_cksum != vd_lbl2cksum(label))
16836836Sachartre return (-1);
16846836Sachartre
16856836Sachartre /* check the vtoc and geometry */
16866836Sachartre vd_label_to_vtocgeom(label, &vtoc, &geom);
16876836Sachartre if (vd_slice_geom_isvalid(vd, &geom) &&
16886836Sachartre vd_slice_vtoc_isvalid(vd, &vtoc))
16896836Sachartre return (length);
16906836Sachartre }
16916836Sachartre
16926836Sachartre /* fail any other write */
16936836Sachartre return (-1);
16946836Sachartre }
16956836Sachartre
16966836Sachartre /*
16976836Sachartre * Function:
16986836Sachartre * vd_slice_fake_rdwr
16996836Sachartre *
17006836Sachartre * Description:
17016836Sachartre * This function simulates a raw read or write operation to a single-slice
17026836Sachartre * disk. It only handles the faked part of the operation i.e. I/Os to
17036836Sachartre * blocks which have no mapping with the vdisk backend (I/Os to the
17046836Sachartre * beginning and to the end of the vdisk).
17056836Sachartre *
17066836Sachartre * The function returns 0 is the operation is completed and it has been
17076836Sachartre * entirely handled as a fake read or write. In that case, lengthp points
17086836Sachartre * to the number of bytes not read or written. Values returned by datap
17096836Sachartre * and blkp are undefined.
17106836Sachartre *
17116836Sachartre * If the fake operation has succeeded but the read or write is not
17126836Sachartre * complete (i.e. the read/write operation extends beyond the blocks
17136836Sachartre * we fake) then the function returns EAGAIN and datap, blkp and lengthp
17146836Sachartre * pointers points to the parameters for completing the operation.
17156836Sachartre *
17166836Sachartre * In case of an error, for example if the slice is empty or parameters
17176836Sachartre * are invalid, then the function returns a non-zero value different
17186836Sachartre * from EAGAIN. In that case, the returned values of datap, blkp and
17196836Sachartre * lengthp are undefined.
17206836Sachartre *
17216836Sachartre * Parameters:
17226836Sachartre * vd - single-slice disk on which the operation is performed
17236836Sachartre * slice - slice on which the operation is performed,
17246836Sachartre * VD_SLICE_NONE indicates that the operation
17256836Sachartre * is done using an absolute disk offset.
17266836Sachartre * operation - operation to execute: read (VD_OP_BREAD) or
17276836Sachartre * write (VD_OP_BWRITE).
17286836Sachartre * datap - pointer to the buffer where data are read to
17296836Sachartre * or written from. Return the pointer where remaining
17306836Sachartre * data have to be read to or written from.
17316836Sachartre * blkp - pointer to the starting block for the operation.
17326836Sachartre * Return the starting block relative to the vdisk
17336836Sachartre * backend for the remaining operation.
17346836Sachartre * lengthp - pointer to the number of bytes to read or write.
17359889SLarry.Liu@Sun.COM * This should be a multiple of vdisk_bsize. Return the
17366836Sachartre * remaining number of bytes to read or write.
17376836Sachartre *
17386836Sachartre * Return Code:
17396836Sachartre * 0 - read/write operation is completed
17406836Sachartre * EAGAIN - read/write operation is not completed
17416836Sachartre * other values - error
17426836Sachartre */
17436836Sachartre static int
vd_slice_fake_rdwr(vd_t * vd,int slice,int operation,caddr_t * datap,size_t * blkp,size_t * lengthp)17446836Sachartre vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap,
17456836Sachartre size_t *blkp, size_t *lengthp)
17466836Sachartre {
17476836Sachartre struct dk_label *label;
17486836Sachartre caddr_t data;
17496836Sachartre size_t blk, length, csize;
17506836Sachartre size_t ablk, asize, aoff, alen;
17516836Sachartre ssize_t n;
17526836Sachartre int sec, status;
17539889SLarry.Liu@Sun.COM size_t bsize = vd->vdisk_bsize;
17546836Sachartre
17556836Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
17566836Sachartre ASSERT(slice != 0);
17576836Sachartre
17586836Sachartre data = *datap;
17596836Sachartre blk = *blkp;
17606836Sachartre length = *lengthp;
17616836Sachartre
17626836Sachartre /*
17636836Sachartre * If this is not a raw I/O or an I/O from a full disk slice then
17646836Sachartre * this is an I/O to/from an empty slice.
17656836Sachartre */
17666836Sachartre if (slice != VD_SLICE_NONE &&
17676836Sachartre (slice != VD_ENTIRE_DISK_SLICE ||
17686836Sachartre vd->vdisk_label != VD_DISK_LABEL_VTOC) &&
17696836Sachartre (slice != VD_EFI_WD_SLICE ||
17706836Sachartre vd->vdisk_label != VD_DISK_LABEL_EFI)) {
17716836Sachartre return (EIO);
17726836Sachartre }
17736836Sachartre
17749889SLarry.Liu@Sun.COM if (length % bsize != 0)
17756836Sachartre return (EINVAL);
17766836Sachartre
17776836Sachartre /* handle any I/O with the fake label */
17786836Sachartre if (operation == VD_OP_BWRITE)
17799889SLarry.Liu@Sun.COM n = vd_slice_flabel_write(vd, data, blk * bsize, length);
17806836Sachartre else
17819889SLarry.Liu@Sun.COM n = vd_slice_flabel_read(vd, data, blk * bsize, length);
17826836Sachartre
17836836Sachartre if (n == -1)
17846836Sachartre return (EINVAL);
17856836Sachartre
17869889SLarry.Liu@Sun.COM ASSERT(n % bsize == 0);
17876836Sachartre
17886836Sachartre /* adjust I/O arguments */
17896836Sachartre data += n;
17909889SLarry.Liu@Sun.COM blk += n / bsize;
17916836Sachartre length -= n;
17926836Sachartre
17936836Sachartre /* check if there's something else to process */
17946836Sachartre if (length == 0) {
17956836Sachartre status = 0;
17966836Sachartre goto done;
17976836Sachartre }
17986836Sachartre
17996836Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC &&
18006836Sachartre slice == VD_ENTIRE_DISK_SLICE) {
18016836Sachartre status = EAGAIN;
18026836Sachartre goto done;
18036836Sachartre }
18046836Sachartre
18056836Sachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) {
18069889SLarry.Liu@Sun.COM asize = EFI_MIN_RESV_SIZE + (EFI_MIN_ARRAY_SIZE / bsize) + 1;
18076836Sachartre ablk = vd->vdisk_size - asize;
18086836Sachartre } else {
18096836Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
18106836Sachartre ASSERT(vd->dk_geom.dkg_apc == 0);
18116836Sachartre
18126836Sachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
18136836Sachartre ablk = vd->dk_geom.dkg_ncyl * csize;
18146836Sachartre asize = vd->dk_geom.dkg_acyl * csize;
18156836Sachartre }
18166836Sachartre
18179889SLarry.Liu@Sun.COM alen = length / bsize;
18186836Sachartre aoff = blk;
18196836Sachartre
18206836Sachartre /* if we have reached the last block then the I/O is completed */
18216836Sachartre if (aoff == ablk + asize) {
18226836Sachartre status = 0;
18236836Sachartre goto done;
18246836Sachartre }
18256836Sachartre
18266836Sachartre /* if we are past the last block then return an error */
18276836Sachartre if (aoff > ablk + asize)
18286836Sachartre return (EIO);
18296836Sachartre
18306836Sachartre /* check if there is any I/O to end of the disk */
18316836Sachartre if (aoff + alen < ablk) {
18326836Sachartre status = EAGAIN;
18336836Sachartre goto done;
18346836Sachartre }
18356836Sachartre
18366836Sachartre /* we don't allow any write to the end of the disk */
18376836Sachartre if (operation == VD_OP_BWRITE)
18386836Sachartre return (EIO);
18396836Sachartre
18406836Sachartre if (aoff < ablk) {
18416836Sachartre alen -= (ablk - aoff);
18426836Sachartre aoff = ablk;
18436836Sachartre }
18446836Sachartre
18456836Sachartre if (aoff + alen > ablk + asize) {
18466836Sachartre alen = ablk + asize - aoff;
18476836Sachartre }
18486836Sachartre
18499889SLarry.Liu@Sun.COM alen *= bsize;
18506836Sachartre
18516836Sachartre if (operation == VD_OP_BREAD) {
18529889SLarry.Liu@Sun.COM bzero(data + (aoff - blk) * bsize, alen);
18536836Sachartre
18546836Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
18556836Sachartre /* check if we read backup labels */
18566836Sachartre label = VD_LABEL_VTOC(vd);
18576836Sachartre ablk += (label->dkl_acyl - 1) * csize +
18586836Sachartre (label->dkl_nhead - 1) * label->dkl_nsect;
18596836Sachartre
18606836Sachartre for (sec = 1; (sec < 5 * 2 + 1); sec += 2) {
18616836Sachartre
18626836Sachartre if (ablk + sec >= blk &&
18639889SLarry.Liu@Sun.COM ablk + sec < blk + (length / bsize)) {
18646836Sachartre bcopy(label, data +
18659889SLarry.Liu@Sun.COM (ablk + sec - blk) * bsize,
18666836Sachartre sizeof (struct dk_label));
18676836Sachartre }
18686836Sachartre }
18696836Sachartre }
18706836Sachartre }
18716836Sachartre
18726836Sachartre length -= alen;
18736836Sachartre
18746836Sachartre status = (length == 0)? 0: EAGAIN;
18756836Sachartre
18766836Sachartre done:
18776836Sachartre ASSERT(length == 0 || blk >= vd->flabel_limit);
18786836Sachartre
18796836Sachartre /*
18806836Sachartre * Return the parameters for the remaining I/O. The starting block is
18816836Sachartre * adjusted so that it is relative to the vdisk backend.
18826836Sachartre */
18836836Sachartre *datap = data;
18846836Sachartre *blkp = blk - vd->flabel_limit;
18856836Sachartre *lengthp = length;
18866836Sachartre
18876836Sachartre return (status);
18886836Sachartre }
18896836Sachartre
18907791SAlexandre.Chartre@Sun.COM static int
vd_flush_write(vd_t * vd)18917791SAlexandre.Chartre@Sun.COM vd_flush_write(vd_t *vd)
18927791SAlexandre.Chartre@Sun.COM {
18937791SAlexandre.Chartre@Sun.COM int status, rval;
18947791SAlexandre.Chartre@Sun.COM
18957791SAlexandre.Chartre@Sun.COM if (vd->file) {
18967791SAlexandre.Chartre@Sun.COM status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL);
18977791SAlexandre.Chartre@Sun.COM } else {
18987791SAlexandre.Chartre@Sun.COM status = ldi_ioctl(vd->ldi_handle[0], DKIOCFLUSHWRITECACHE,
18997791SAlexandre.Chartre@Sun.COM NULL, vd->open_flags | FKIOCTL, kcred, &rval);
19007791SAlexandre.Chartre@Sun.COM }
19017791SAlexandre.Chartre@Sun.COM
19027791SAlexandre.Chartre@Sun.COM return (status);
19037791SAlexandre.Chartre@Sun.COM }
19047791SAlexandre.Chartre@Sun.COM
19057791SAlexandre.Chartre@Sun.COM static void
vd_bio_task(void * arg)19067791SAlexandre.Chartre@Sun.COM vd_bio_task(void *arg)
19077791SAlexandre.Chartre@Sun.COM {
19087791SAlexandre.Chartre@Sun.COM struct buf *buf = (struct buf *)arg;
19097791SAlexandre.Chartre@Sun.COM vd_task_t *task = (vd_task_t *)buf->b_private;
19107791SAlexandre.Chartre@Sun.COM vd_t *vd = task->vd;
19117791SAlexandre.Chartre@Sun.COM ssize_t resid;
19127791SAlexandre.Chartre@Sun.COM int status;
19137791SAlexandre.Chartre@Sun.COM
19149889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_bsize == DEV_BSIZE);
19159889SLarry.Liu@Sun.COM
19167791SAlexandre.Chartre@Sun.COM if (vd->zvol) {
19177791SAlexandre.Chartre@Sun.COM
19187791SAlexandre.Chartre@Sun.COM status = ldi_strategy(vd->ldi_handle[0], buf);
19197791SAlexandre.Chartre@Sun.COM
19207791SAlexandre.Chartre@Sun.COM } else {
19217791SAlexandre.Chartre@Sun.COM
19227791SAlexandre.Chartre@Sun.COM ASSERT(vd->file);
19237791SAlexandre.Chartre@Sun.COM
19247791SAlexandre.Chartre@Sun.COM status = vn_rdwr((buf->b_flags & B_READ)? UIO_READ : UIO_WRITE,
19257791SAlexandre.Chartre@Sun.COM vd->file_vnode, buf->b_un.b_addr, buf->b_bcount,
19267791SAlexandre.Chartre@Sun.COM buf->b_lblkno * DEV_BSIZE, UIO_SYSSPACE, 0,
19277791SAlexandre.Chartre@Sun.COM RLIM64_INFINITY, kcred, &resid);
19287791SAlexandre.Chartre@Sun.COM
19297791SAlexandre.Chartre@Sun.COM if (status == 0) {
19307791SAlexandre.Chartre@Sun.COM buf->b_resid = resid;
19317791SAlexandre.Chartre@Sun.COM biodone(buf);
19327791SAlexandre.Chartre@Sun.COM return;
19337791SAlexandre.Chartre@Sun.COM }
19347791SAlexandre.Chartre@Sun.COM }
19357791SAlexandre.Chartre@Sun.COM
19367791SAlexandre.Chartre@Sun.COM if (status != 0) {
19377791SAlexandre.Chartre@Sun.COM bioerror(buf, status);
19387791SAlexandre.Chartre@Sun.COM biodone(buf);
19397791SAlexandre.Chartre@Sun.COM }
19407791SAlexandre.Chartre@Sun.COM }
19417791SAlexandre.Chartre@Sun.COM
19426836Sachartre /*
19437597SAlexandre.Chartre@Sun.COM * We define our own biodone function so that buffers used for
19447597SAlexandre.Chartre@Sun.COM * asynchronous writes are not released when biodone() is called.
19457597SAlexandre.Chartre@Sun.COM */
19467597SAlexandre.Chartre@Sun.COM static int
vd_biodone(struct buf * bp)19477597SAlexandre.Chartre@Sun.COM vd_biodone(struct buf *bp)
19487597SAlexandre.Chartre@Sun.COM {
19497597SAlexandre.Chartre@Sun.COM ASSERT((bp->b_flags & B_DONE) == 0);
19507597SAlexandre.Chartre@Sun.COM ASSERT(SEMA_HELD(&bp->b_sem));
19517597SAlexandre.Chartre@Sun.COM
19527597SAlexandre.Chartre@Sun.COM bp->b_flags |= B_DONE;
19537791SAlexandre.Chartre@Sun.COM sema_v(&bp->b_io);
19547597SAlexandre.Chartre@Sun.COM
19557597SAlexandre.Chartre@Sun.COM return (0);
19567597SAlexandre.Chartre@Sun.COM }
19577597SAlexandre.Chartre@Sun.COM
19587597SAlexandre.Chartre@Sun.COM /*
19594838Slm66018 * Return Values
19604838Slm66018 * EINPROGRESS - operation was successfully started
19614838Slm66018 * EIO - encountered LDC (aka. task error)
19624838Slm66018 * 0 - operation completed successfully
19634838Slm66018 *
19644838Slm66018 * Side Effect
19654838Slm66018 * sets request->status = <disk operation status>
19664838Slm66018 */
19671991Sheppo static int
vd_start_bio(vd_task_t * task)19682336Snarayan vd_start_bio(vd_task_t *task)
19691991Sheppo {
19702531Snarayan int rv, status = 0;
19712336Snarayan vd_t *vd = task->vd;
19722336Snarayan vd_dring_payload_t *request = task->request;
19732336Snarayan struct buf *buf = &task->buf;
19742531Snarayan uint8_t mtype;
19753401Snarayan int slice;
19765081Sachartre char *bufaddr = 0;
19775081Sachartre size_t buflen;
19786836Sachartre size_t offset, length, nbytes;
19792336Snarayan
19802336Snarayan ASSERT(vd != NULL);
19812336Snarayan ASSERT(request != NULL);
19823401Snarayan
19833401Snarayan slice = request->slice;
19843401Snarayan
19854696Sachartre ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices);
19862336Snarayan ASSERT((request->operation == VD_OP_BREAD) ||
19872336Snarayan (request->operation == VD_OP_BWRITE));
19882336Snarayan
19894838Slm66018 if (request->nbytes == 0) {
19904838Slm66018 /* no service for trivial requests */
19914838Slm66018 request->status = EINVAL;
19924838Slm66018 return (0);
19934838Slm66018 }
19942336Snarayan
19952336Snarayan PR1("%s %lu bytes at block %lu",
19962336Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write",
19972336Snarayan request->nbytes, request->addr);
19982336Snarayan
19995081Sachartre /*
20005081Sachartre * We have to check the open flags because the functions processing
20015081Sachartre * the read/write request will not do it.
20025081Sachartre */
20035081Sachartre if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) {
20045081Sachartre PR0("write fails because backend is opened read-only");
20055081Sachartre request->nbytes = 0;
20065081Sachartre request->status = EROFS;
20075081Sachartre return (0);
20085081Sachartre }
20092336Snarayan
201012011SSriharsha.Basavapatna@Sun.COM mtype = LDC_SHADOW_MAP;
20112531Snarayan
20122531Snarayan /* Map memory exported by client */
20132531Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies,
20142531Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R,
20155081Sachartre &bufaddr, NULL);
20162531Snarayan if (status != 0) {
20172793Slm66018 PR0("ldc_mem_map() returned err %d ", status);
20184838Slm66018 return (EIO);
20192336Snarayan }
20202336Snarayan
20216836Sachartre /*
20226836Sachartre * The buffer size has to be 8-byte aligned, so the client should have
20236836Sachartre * sent a buffer which size is roundup to the next 8-byte aligned value.
20246836Sachartre */
20256836Sachartre buflen = P2ROUNDUP(request->nbytes, 8);
20265081Sachartre
20275081Sachartre status = ldc_mem_acquire(task->mhdl, 0, buflen);
20282531Snarayan if (status != 0) {
20292531Snarayan (void) ldc_mem_unmap(task->mhdl);
20302793Slm66018 PR0("ldc_mem_acquire() returned err %d ", status);
20314838Slm66018 return (EIO);
20322531Snarayan }
20332531Snarayan
20346836Sachartre offset = request->addr;
20356836Sachartre nbytes = request->nbytes;
20366836Sachartre length = nbytes;
20376836Sachartre
20386836Sachartre /* default number of byte returned by the I/O */
20396836Sachartre request->nbytes = 0;
20406836Sachartre
20416836Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
20426836Sachartre
20436836Sachartre if (slice != 0) {
20446836Sachartre /* handle any fake I/O */
20456836Sachartre rv = vd_slice_fake_rdwr(vd, slice, request->operation,
20466836Sachartre &bufaddr, &offset, &length);
20476836Sachartre
20486836Sachartre /* record the number of bytes from the fake I/O */
20496836Sachartre request->nbytes = nbytes - length;
20506836Sachartre
20516836Sachartre if (rv == 0) {
20526836Sachartre request->status = 0;
20536836Sachartre goto io_done;
20546836Sachartre }
20556836Sachartre
20566836Sachartre if (rv != EAGAIN) {
20576836Sachartre request->nbytes = 0;
20586836Sachartre request->status = EIO;
20596836Sachartre goto io_done;
20606836Sachartre }
20616836Sachartre
20626836Sachartre /*
20636836Sachartre * If we return with EAGAIN then this means that there
20646836Sachartre * are still data to read or write.
20656836Sachartre */
20666836Sachartre ASSERT(length != 0);
20676836Sachartre
20686836Sachartre /*
20696836Sachartre * We need to continue the I/O from the slice backend to
20706836Sachartre * complete the request. The variables bufaddr, offset
20716836Sachartre * and length have been adjusted to have the right
20726836Sachartre * information to do the remaining I/O from the backend.
20736836Sachartre * The backend is entirely mapped to slice 0 so we just
20746836Sachartre * have to complete the I/O from that slice.
20756836Sachartre */
20766836Sachartre slice = 0;
20776836Sachartre }
20786836Sachartre
20797791SAlexandre.Chartre@Sun.COM } else if (vd->volume || vd->file) {
20807597SAlexandre.Chartre@Sun.COM
20817597SAlexandre.Chartre@Sun.COM rv = vd_dskimg_io_params(vd, slice, &offset, &length);
20827597SAlexandre.Chartre@Sun.COM if (rv != 0) {
20837597SAlexandre.Chartre@Sun.COM request->status = (rv == ENODATA)? 0: EIO;
20847597SAlexandre.Chartre@Sun.COM goto io_done;
20857597SAlexandre.Chartre@Sun.COM }
20867597SAlexandre.Chartre@Sun.COM slice = 0;
20877597SAlexandre.Chartre@Sun.COM
20887791SAlexandre.Chartre@Sun.COM } else if (slice == VD_SLICE_NONE) {
20896836Sachartre
20906836Sachartre /*
20916836Sachartre * This is not a disk image so it is a real disk. We
20926836Sachartre * assume that the underlying device driver supports
20936836Sachartre * USCSICMD ioctls. This is the case of all SCSI devices
20946836Sachartre * (sd, ssd...).
20956836Sachartre *
20966836Sachartre * In the future if we have non-SCSI disks we would need
20976836Sachartre * to invoke the appropriate function to do I/O using an
20986836Sachartre * absolute disk offset (for example using DIOCTL_RWCMD
20996836Sachartre * for IDE disks).
21006836Sachartre */
21016836Sachartre rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset,
21026836Sachartre length);
21036836Sachartre if (rv != 0) {
21046836Sachartre request->status = EIO;
21056836Sachartre } else {
21066836Sachartre request->nbytes = length;
21076836Sachartre request->status = 0;
21086836Sachartre }
21096836Sachartre goto io_done;
21106836Sachartre }
21116836Sachartre
21122336Snarayan /* Start the block I/O */
21137791SAlexandre.Chartre@Sun.COM bioinit(buf);
21147791SAlexandre.Chartre@Sun.COM buf->b_flags = B_BUSY;
21157791SAlexandre.Chartre@Sun.COM buf->b_bcount = length;
21167791SAlexandre.Chartre@Sun.COM buf->b_lblkno = offset;
21177791SAlexandre.Chartre@Sun.COM buf->b_bufsize = buflen;
21187791SAlexandre.Chartre@Sun.COM buf->b_edev = vd->dev[slice];
21197791SAlexandre.Chartre@Sun.COM buf->b_un.b_addr = bufaddr;
21207791SAlexandre.Chartre@Sun.COM buf->b_iodone = vd_biodone;
21217791SAlexandre.Chartre@Sun.COM
21227791SAlexandre.Chartre@Sun.COM if (vd->file || vd->zvol) {
21237791SAlexandre.Chartre@Sun.COM /*
21247791SAlexandre.Chartre@Sun.COM * I/O to a file are dispatched to an I/O queue, so that several
21257791SAlexandre.Chartre@Sun.COM * I/Os can be processed in parallel. We also do that for ZFS
21267791SAlexandre.Chartre@Sun.COM * volumes because the ZFS volume strategy() function will only
21277791SAlexandre.Chartre@Sun.COM * return after the I/O is completed (instead of just starting
21287791SAlexandre.Chartre@Sun.COM * the I/O).
21297791SAlexandre.Chartre@Sun.COM */
21307597SAlexandre.Chartre@Sun.COM
21317597SAlexandre.Chartre@Sun.COM if (request->operation == VD_OP_BREAD) {
21327597SAlexandre.Chartre@Sun.COM buf->b_flags |= B_READ;
21337597SAlexandre.Chartre@Sun.COM } else {
21347597SAlexandre.Chartre@Sun.COM /*
21357791SAlexandre.Chartre@Sun.COM * For ZFS volumes and files, we do an asynchronous
21367791SAlexandre.Chartre@Sun.COM * write and we will wait for the completion of the
21377791SAlexandre.Chartre@Sun.COM * write in vd_complete_bio() by flushing the volume
21387791SAlexandre.Chartre@Sun.COM * or file.
21397791SAlexandre.Chartre@Sun.COM *
21407791SAlexandre.Chartre@Sun.COM * This done for performance reasons, so that we can
21417791SAlexandre.Chartre@Sun.COM * group together several write requests into a single
21427791SAlexandre.Chartre@Sun.COM * flush operation.
21437597SAlexandre.Chartre@Sun.COM */
21447791SAlexandre.Chartre@Sun.COM buf->b_flags |= B_WRITE | B_ASYNC;
21457791SAlexandre.Chartre@Sun.COM
21467791SAlexandre.Chartre@Sun.COM /*
21477791SAlexandre.Chartre@Sun.COM * We keep track of the write so that we can group
21487791SAlexandre.Chartre@Sun.COM * requests when flushing. The write queue has the
21497791SAlexandre.Chartre@Sun.COM * same number of slots as the dring so this prevents
21507791SAlexandre.Chartre@Sun.COM * the write queue from wrapping and overwriting
21517791SAlexandre.Chartre@Sun.COM * existing entries: if the write queue gets full
21527791SAlexandre.Chartre@Sun.COM * then that means that the dring is full so we stop
21537791SAlexandre.Chartre@Sun.COM * receiving new requests until an existing request
21547791SAlexandre.Chartre@Sun.COM * is processed, removed from the write queue and
21557791SAlexandre.Chartre@Sun.COM * then from the dring.
21567791SAlexandre.Chartre@Sun.COM */
21577791SAlexandre.Chartre@Sun.COM task->write_index = vd->write_index;
21587791SAlexandre.Chartre@Sun.COM vd->write_queue[task->write_index] = buf;
21597791SAlexandre.Chartre@Sun.COM vd->write_index =
21607791SAlexandre.Chartre@Sun.COM VD_WRITE_INDEX_NEXT(vd, vd->write_index);
21617791SAlexandre.Chartre@Sun.COM }
21627791SAlexandre.Chartre@Sun.COM
21637791SAlexandre.Chartre@Sun.COM buf->b_private = task;
21647791SAlexandre.Chartre@Sun.COM
21657791SAlexandre.Chartre@Sun.COM ASSERT(vd->ioq != NULL);
21667791SAlexandre.Chartre@Sun.COM
21677791SAlexandre.Chartre@Sun.COM request->status = 0;
21687791SAlexandre.Chartre@Sun.COM (void) ddi_taskq_dispatch(task->vd->ioq, vd_bio_task, buf,
21697791SAlexandre.Chartre@Sun.COM DDI_SLEEP);
21707791SAlexandre.Chartre@Sun.COM
21717791SAlexandre.Chartre@Sun.COM } else {
21727791SAlexandre.Chartre@Sun.COM
21737791SAlexandre.Chartre@Sun.COM if (request->operation == VD_OP_BREAD) {
21747791SAlexandre.Chartre@Sun.COM buf->b_flags |= B_READ;
21757791SAlexandre.Chartre@Sun.COM } else {
21767791SAlexandre.Chartre@Sun.COM buf->b_flags |= B_WRITE;
21777597SAlexandre.Chartre@Sun.COM }
21786836Sachartre
21799889SLarry.Liu@Sun.COM /* convert VIO block number to buf block number */
21809889SLarry.Liu@Sun.COM buf->b_lblkno = offset << vd->vio_bshift;
21819889SLarry.Liu@Sun.COM
21826836Sachartre request->status = ldi_strategy(vd->ldi_handle[slice], buf);
21837791SAlexandre.Chartre@Sun.COM }
21847791SAlexandre.Chartre@Sun.COM
21857791SAlexandre.Chartre@Sun.COM /*
21867791SAlexandre.Chartre@Sun.COM * This is to indicate to the caller that the request
21877791SAlexandre.Chartre@Sun.COM * needs to be finished by vd_complete_bio() by calling
21887791SAlexandre.Chartre@Sun.COM * biowait() there and waiting for that to return before
21897791SAlexandre.Chartre@Sun.COM * triggering the notification of the vDisk client.
21907791SAlexandre.Chartre@Sun.COM *
21917791SAlexandre.Chartre@Sun.COM * This is necessary when writing to real disks as
21927791SAlexandre.Chartre@Sun.COM * otherwise calls to ldi_strategy() would be serialized
21937791SAlexandre.Chartre@Sun.COM * behind the calls to biowait() and performance would
21947791SAlexandre.Chartre@Sun.COM * suffer.
21957791SAlexandre.Chartre@Sun.COM */
21967791SAlexandre.Chartre@Sun.COM if (request->status == 0)
21977791SAlexandre.Chartre@Sun.COM return (EINPROGRESS);
21987791SAlexandre.Chartre@Sun.COM
21997791SAlexandre.Chartre@Sun.COM biofini(buf);
22006836Sachartre
22016836Sachartre io_done:
22026836Sachartre /* Clean up after error or completion */
22035081Sachartre rv = ldc_mem_release(task->mhdl, 0, buflen);
22042531Snarayan if (rv) {
22052793Slm66018 PR0("ldc_mem_release() returned err %d ", rv);
22064838Slm66018 status = EIO;
22072531Snarayan }
22082531Snarayan rv = ldc_mem_unmap(task->mhdl);
22092531Snarayan if (rv) {
22104838Slm66018 PR0("ldc_mem_unmap() returned err %d ", rv);
22114838Slm66018 status = EIO;
22122531Snarayan }
22132531Snarayan
22142336Snarayan return (status);
22152336Snarayan }
22162336Snarayan
22174838Slm66018 /*
22184838Slm66018 * This function should only be called from vd_notify to ensure that requests
22194838Slm66018 * are responded to in the order that they are received.
22204838Slm66018 */
22212336Snarayan static int
send_msg(ldc_handle_t ldc_handle,void * msg,size_t msglen)22222336Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen)
22232336Snarayan {
22242793Slm66018 int status;
22252336Snarayan size_t nbytes;
22262336Snarayan
22272793Slm66018 do {
22282336Snarayan nbytes = msglen;
22292336Snarayan status = ldc_write(ldc_handle, msg, &nbytes);
22302793Slm66018 if (status != EWOULDBLOCK)
22312793Slm66018 break;
22322793Slm66018 drv_usecwait(vds_ldc_delay);
22332793Slm66018 } while (status == EWOULDBLOCK);
22342336Snarayan
22352336Snarayan if (status != 0) {
22362793Slm66018 if (status != ECONNRESET)
22372793Slm66018 PR0("ldc_write() returned errno %d", status);
22382336Snarayan return (status);
22392336Snarayan } else if (nbytes != msglen) {
22402793Slm66018 PR0("ldc_write() performed only partial write");
22412336Snarayan return (EIO);
22422336Snarayan }
22432336Snarayan
22442336Snarayan PR1("SENT %lu bytes", msglen);
22452336Snarayan return (0);
22462336Snarayan }
22472336Snarayan
22482336Snarayan static void
vd_need_reset(vd_t * vd,boolean_t reset_ldc)22492336Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc)
22502336Snarayan {
22512336Snarayan mutex_enter(&vd->lock);
22522336Snarayan vd->reset_state = B_TRUE;
22532336Snarayan vd->reset_ldc = reset_ldc;
22542336Snarayan mutex_exit(&vd->lock);
22552336Snarayan }
22562336Snarayan
22572336Snarayan /*
22582336Snarayan * Reset the state of the connection with a client, if needed; reset the LDC
22592336Snarayan * transport as well, if needed. This function should only be called from the
22602793Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur.
22612336Snarayan */
22622336Snarayan static void
vd_reset_if_needed(vd_t * vd)22632336Snarayan vd_reset_if_needed(vd_t *vd)
22642336Snarayan {
22652793Slm66018 int status = 0;
22662336Snarayan
22672336Snarayan mutex_enter(&vd->lock);
22682336Snarayan if (!vd->reset_state) {
22692336Snarayan ASSERT(!vd->reset_ldc);
22702336Snarayan mutex_exit(&vd->lock);
22712336Snarayan return;
22722336Snarayan }
22732336Snarayan mutex_exit(&vd->lock);
22742336Snarayan
22752336Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd));
22762336Snarayan
22772336Snarayan /*
22782336Snarayan * Let any asynchronous I/O complete before possibly pulling the rug
22792336Snarayan * out from under it; defer checking vd->reset_ldc, as one of the
22802336Snarayan * asynchronous tasks might set it
22812336Snarayan */
22827791SAlexandre.Chartre@Sun.COM if (vd->ioq != NULL)
22837791SAlexandre.Chartre@Sun.COM ddi_taskq_wait(vd->ioq);
22842336Snarayan ddi_taskq_wait(vd->completionq);
22852336Snarayan
22867791SAlexandre.Chartre@Sun.COM status = vd_flush_write(vd);
22877791SAlexandre.Chartre@Sun.COM if (status) {
22887791SAlexandre.Chartre@Sun.COM PR0("flushwrite returned error %d", status);
22893401Snarayan }
22903401Snarayan
22912336Snarayan if ((vd->initialized & VD_DRING) &&
22922336Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0))
22932793Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status);
22942793Slm66018
22952793Slm66018 vd_free_dring_task(vd);
22962793Slm66018
22972793Slm66018 /* Free the staging buffer for msgs */
22982793Slm66018 if (vd->vio_msgp != NULL) {
22992793Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen);
23002793Slm66018 vd->vio_msgp = NULL;
23012336Snarayan }
23022336Snarayan
23032793Slm66018 /* Free the inband message buffer */
23042793Slm66018 if (vd->inband_task.msg != NULL) {
23052793Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen);
23062793Slm66018 vd->inband_task.msg = NULL;
23072793Slm66018 }
23082336Snarayan
23092336Snarayan mutex_enter(&vd->lock);
23102793Slm66018
23112793Slm66018 if (vd->reset_ldc)
23122793Slm66018 PR0("taking down LDC channel");
23132410Slm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0))
23142793Slm66018 PR0("ldc_down() returned errno %d", status);
23152336Snarayan
23165658Sachartre /* Reset exclusive access rights */
23175658Sachartre vd_reset_access(vd);
23185658Sachartre
23192336Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING);
23202336Snarayan vd->state = VD_STATE_INIT;
23212336Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */
23222336Snarayan
23232793Slm66018 /* Allocate the staging buffer */
23242793Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
23252793Slm66018
23263010Slm66018 PR0("calling ldc_up\n");
23273010Slm66018 (void) ldc_up(vd->ldc_handle);
23282793Slm66018
23292336Snarayan vd->reset_state = B_FALSE;
23302336Snarayan vd->reset_ldc = B_FALSE;
23312793Slm66018
23322336Snarayan mutex_exit(&vd->lock);
23332336Snarayan }
23342336Snarayan
23352793Slm66018 static void vd_recv_msg(void *arg);
23362793Slm66018
23372793Slm66018 static void
vd_mark_in_reset(vd_t * vd)23382793Slm66018 vd_mark_in_reset(vd_t *vd)
23392793Slm66018 {
23402793Slm66018 int status;
23412793Slm66018
23422793Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n");
23432793Slm66018
23442793Slm66018 vd_need_reset(vd, B_FALSE);
23452793Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP);
23462793Slm66018 if (status == DDI_FAILURE) {
23472793Slm66018 PR0("cannot schedule task to recv msg\n");
23482793Slm66018 vd_need_reset(vd, B_TRUE);
23492793Slm66018 return;
23502793Slm66018 }
23512793Slm66018 }
23522793Slm66018
23532336Snarayan static int
vd_mark_elem_done(vd_t * vd,int idx,int elem_status,int elem_nbytes)23543401Snarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes)
23552336Snarayan {
23562336Snarayan boolean_t accepted;
23572336Snarayan int status;
23586845Sha137994 on_trap_data_t otd;
23592336Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx);
23602336Snarayan
23612793Slm66018 if (vd->reset_state)
23622793Slm66018 return (0);
23632336Snarayan
23642336Snarayan /* Acquire the element */
23656845Sha137994 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
23666845Sha137994 vd->dring_handle, idx, idx)) != 0) {
23672793Slm66018 if (status == ECONNRESET) {
23682793Slm66018 vd_mark_in_reset(vd);
23692793Slm66018 return (0);
23702793Slm66018 } else {
23712793Slm66018 return (status);
23722793Slm66018 }
23732336Snarayan }
23742336Snarayan
23752336Snarayan /* Set the element's status and mark it done */
23762336Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED);
23772336Snarayan if (accepted) {
23783401Snarayan elem->payload.nbytes = elem_nbytes;
23792336Snarayan elem->payload.status = elem_status;
23802336Snarayan elem->hdr.dstate = VIO_DESC_DONE;
23812336Snarayan } else {
23822336Snarayan /* Perhaps client timed out waiting for I/O... */
23832793Slm66018 PR0("element %u no longer \"accepted\"", idx);
23842336Snarayan VD_DUMP_DRING_ELEM(elem);
23852336Snarayan }
23862336Snarayan /* Release the element */
23876845Sha137994 if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
23886845Sha137994 vd->dring_handle, idx, idx)) != 0) {
23892793Slm66018 if (status == ECONNRESET) {
23902793Slm66018 vd_mark_in_reset(vd);
23912793Slm66018 return (0);
23922793Slm66018 } else {
23936845Sha137994 PR0("VIO_DRING_RELEASE() returned errno %d",
23942793Slm66018 status);
23952793Slm66018 return (status);
23962793Slm66018 }
23972336Snarayan }
23982336Snarayan
23992336Snarayan return (accepted ? 0 : EINVAL);
24002336Snarayan }
24012336Snarayan
24024838Slm66018 /*
24034838Slm66018 * Return Values
24044838Slm66018 * 0 - operation completed successfully
24054838Slm66018 * EIO - encountered LDC / task error
24064838Slm66018 *
24074838Slm66018 * Side Effect
24084838Slm66018 * sets request->status = <disk operation status>
24094838Slm66018 */
24104838Slm66018 static int
vd_complete_bio(vd_task_t * task)24114838Slm66018 vd_complete_bio(vd_task_t *task)
24122336Snarayan {
24132336Snarayan int status = 0;
24144838Slm66018 int rv = 0;
24152336Snarayan vd_t *vd = task->vd;
24162336Snarayan vd_dring_payload_t *request = task->request;
24172336Snarayan struct buf *buf = &task->buf;
24187791SAlexandre.Chartre@Sun.COM int wid, nwrites;
24192336Snarayan
24202336Snarayan
24212336Snarayan ASSERT(vd != NULL);
24222336Snarayan ASSERT(request != NULL);
24232336Snarayan ASSERT(task->msg != NULL);
24242336Snarayan ASSERT(task->msglen >= sizeof (*task->msg));
24257791SAlexandre.Chartre@Sun.COM
24267791SAlexandre.Chartre@Sun.COM if (buf->b_flags & B_DONE) {
24277791SAlexandre.Chartre@Sun.COM /*
24287791SAlexandre.Chartre@Sun.COM * If the I/O is already done then we don't call biowait()
24297791SAlexandre.Chartre@Sun.COM * because biowait() might already have been called when
24307791SAlexandre.Chartre@Sun.COM * flushing a previous asynchronous write. So we just
24317791SAlexandre.Chartre@Sun.COM * retrieve the status of the request.
24327791SAlexandre.Chartre@Sun.COM */
24337791SAlexandre.Chartre@Sun.COM request->status = geterror(buf);
24347791SAlexandre.Chartre@Sun.COM } else {
24357791SAlexandre.Chartre@Sun.COM /*
24367791SAlexandre.Chartre@Sun.COM * Wait for the I/O. For synchronous I/O, biowait() will return
24377791SAlexandre.Chartre@Sun.COM * when the I/O has completed. For asynchronous write, it will
24387791SAlexandre.Chartre@Sun.COM * return the write has been submitted to the backend, but it
24397791SAlexandre.Chartre@Sun.COM * may not have been committed.
24407791SAlexandre.Chartre@Sun.COM */
24417791SAlexandre.Chartre@Sun.COM request->status = biowait(buf);
24427791SAlexandre.Chartre@Sun.COM }
24437791SAlexandre.Chartre@Sun.COM
24447791SAlexandre.Chartre@Sun.COM if (buf->b_flags & B_ASYNC) {
24457597SAlexandre.Chartre@Sun.COM /*
24467791SAlexandre.Chartre@Sun.COM * Asynchronous writes are used when writing to a file or a
24477791SAlexandre.Chartre@Sun.COM * ZFS volume. In that case the bio notification indicates
24487791SAlexandre.Chartre@Sun.COM * that the write has started. We have to flush the backend
24497791SAlexandre.Chartre@Sun.COM * to ensure that the write has been committed before marking
24507791SAlexandre.Chartre@Sun.COM * the request as completed.
24517597SAlexandre.Chartre@Sun.COM */
24527791SAlexandre.Chartre@Sun.COM ASSERT(task->request->operation == VD_OP_BWRITE);
24537791SAlexandre.Chartre@Sun.COM
24547791SAlexandre.Chartre@Sun.COM wid = task->write_index;
24557791SAlexandre.Chartre@Sun.COM
24567791SAlexandre.Chartre@Sun.COM /* check if write has been already flushed */
24577791SAlexandre.Chartre@Sun.COM if (vd->write_queue[wid] != NULL) {
24587791SAlexandre.Chartre@Sun.COM
24597791SAlexandre.Chartre@Sun.COM vd->write_queue[wid] = NULL;
24607791SAlexandre.Chartre@Sun.COM wid = VD_WRITE_INDEX_NEXT(vd, wid);
24617791SAlexandre.Chartre@Sun.COM
24627791SAlexandre.Chartre@Sun.COM /*
24637791SAlexandre.Chartre@Sun.COM * Because flushing is time consuming, it is worth
24647791SAlexandre.Chartre@Sun.COM * waiting for any other writes so that they can be
24657791SAlexandre.Chartre@Sun.COM * included in this single flush request.
24667791SAlexandre.Chartre@Sun.COM */
24677791SAlexandre.Chartre@Sun.COM if (vd_awflush & VD_AWFLUSH_GROUP) {
24687791SAlexandre.Chartre@Sun.COM nwrites = 1;
24697791SAlexandre.Chartre@Sun.COM while (vd->write_queue[wid] != NULL) {
24707791SAlexandre.Chartre@Sun.COM (void) biowait(vd->write_queue[wid]);
24717791SAlexandre.Chartre@Sun.COM vd->write_queue[wid] = NULL;
24727791SAlexandre.Chartre@Sun.COM wid = VD_WRITE_INDEX_NEXT(vd, wid);
24737791SAlexandre.Chartre@Sun.COM nwrites++;
24747791SAlexandre.Chartre@Sun.COM }
24757791SAlexandre.Chartre@Sun.COM DTRACE_PROBE2(flushgrp, vd_task_t *, task,
24767791SAlexandre.Chartre@Sun.COM int, nwrites);
24777791SAlexandre.Chartre@Sun.COM }
24787791SAlexandre.Chartre@Sun.COM
24797791SAlexandre.Chartre@Sun.COM if (vd_awflush & VD_AWFLUSH_IMMEDIATE) {
24807791SAlexandre.Chartre@Sun.COM request->status = vd_flush_write(vd);
24817791SAlexandre.Chartre@Sun.COM } else if (vd_awflush & VD_AWFLUSH_DEFER) {
24827791SAlexandre.Chartre@Sun.COM (void) taskq_dispatch(system_taskq,
24837791SAlexandre.Chartre@Sun.COM (void (*)(void *))vd_flush_write, vd,
24847791SAlexandre.Chartre@Sun.COM DDI_SLEEP);
24857791SAlexandre.Chartre@Sun.COM request->status = 0;
24867791SAlexandre.Chartre@Sun.COM }
24877791SAlexandre.Chartre@Sun.COM }
24887597SAlexandre.Chartre@Sun.COM }
24892336Snarayan
24906836Sachartre /* Update the number of bytes read/written */
24916836Sachartre request->nbytes += buf->b_bcount - buf->b_resid;
24923401Snarayan
24932531Snarayan /* Release the buffer */
24942793Slm66018 if (!vd->reset_state)
24956836Sachartre status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize);
24962531Snarayan if (status) {
24972793Slm66018 PR0("ldc_mem_release() returned errno %d copying to "
24982793Slm66018 "client", status);
24992793Slm66018 if (status == ECONNRESET) {
25002793Slm66018 vd_mark_in_reset(vd);
25012793Slm66018 }
25024838Slm66018 rv = EIO;
25031991Sheppo }
25042336Snarayan
25052793Slm66018 /* Unmap the memory, even if in reset */
25062531Snarayan status = ldc_mem_unmap(task->mhdl);
25072531Snarayan if (status) {
25082793Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client",
25092531Snarayan status);
25102793Slm66018 if (status == ECONNRESET) {
25112793Slm66018 vd_mark_in_reset(vd);
25122793Slm66018 }
25134838Slm66018 rv = EIO;
25142531Snarayan }
25152531Snarayan
25162336Snarayan biofini(buf);
25172336Snarayan
25184838Slm66018 return (rv);
25194838Slm66018 }
25204838Slm66018
25214838Slm66018 /*
25224838Slm66018 * Description:
25234838Slm66018 * This function is called by the two functions called by a taskq
25244838Slm66018 * [ vd_complete_notify() and vd_serial_notify()) ] to send the
25254838Slm66018 * message to the client.
25264838Slm66018 *
25274838Slm66018 * Parameters:
25284838Slm66018 * arg - opaque pointer to structure containing task to be completed
25294838Slm66018 *
25304838Slm66018 * Return Values
25314838Slm66018 * None
25324838Slm66018 */
25334838Slm66018 static void
vd_notify(vd_task_t * task)25344838Slm66018 vd_notify(vd_task_t *task)
25354838Slm66018 {
25364838Slm66018 int status;
25374838Slm66018
25384838Slm66018 ASSERT(task != NULL);
25394838Slm66018 ASSERT(task->vd != NULL);
25404838Slm66018
25414838Slm66018 /*
25424838Slm66018 * Send the "ack" or "nack" back to the client; if sending the message
25434838Slm66018 * via LDC fails, arrange to reset both the connection state and LDC
25444838Slm66018 * itself
25454838Slm66018 */
25464838Slm66018 PR2("Sending %s",
25474838Slm66018 (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK");
25484838Slm66018
25494838Slm66018 status = send_msg(task->vd->ldc_handle, task->msg, task->msglen);
25504838Slm66018 switch (status) {
25514838Slm66018 case 0:
25524838Slm66018 break;
25534838Slm66018 case ECONNRESET:
25544838Slm66018 vd_mark_in_reset(task->vd);
25554838Slm66018 break;
25564838Slm66018 default:
25574838Slm66018 PR0("initiating full reset");
25584838Slm66018 vd_need_reset(task->vd, B_TRUE);
25594838Slm66018 break;
25604838Slm66018 }
25614838Slm66018
25624838Slm66018 DTRACE_PROBE1(task__end, vd_task_t *, task);
25634838Slm66018 }
25644838Slm66018
25654838Slm66018 /*
25664838Slm66018 * Description:
25674838Slm66018 * Mark the Dring entry as Done and (if necessary) send an ACK/NACK to
25684838Slm66018 * the vDisk client
25694838Slm66018 *
25704838Slm66018 * Parameters:
25714838Slm66018 * task - structure containing the request sent from client
25724838Slm66018 *
25734838Slm66018 * Return Values
25744838Slm66018 * None
25754838Slm66018 */
25764838Slm66018 static void
vd_complete_notify(vd_task_t * task)25774838Slm66018 vd_complete_notify(vd_task_t *task)
25784838Slm66018 {
25794838Slm66018 int status = 0;
25804838Slm66018 vd_t *vd = task->vd;
25814838Slm66018 vd_dring_payload_t *request = task->request;
25824838Slm66018
25832336Snarayan /* Update the dring element for a dring client */
25845935Ssb155480 if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
25853401Snarayan status = vd_mark_elem_done(vd, task->index,
25863401Snarayan request->status, request->nbytes);
25872793Slm66018 if (status == ECONNRESET)
25882793Slm66018 vd_mark_in_reset(vd);
25896845Sha137994 else if (status == EACCES)
25906845Sha137994 vd_need_reset(vd, B_TRUE);
25912793Slm66018 }
25922336Snarayan
25932336Snarayan /*
25944838Slm66018 * If a transport error occurred while marking the element done or
25954838Slm66018 * previously while executing the task, arrange to "nack" the message
25964838Slm66018 * when the final task in the descriptor element range completes
25972336Snarayan */
25984838Slm66018 if ((status != 0) || (task->status != 0))
25992336Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
26002336Snarayan
26012336Snarayan /*
26022336Snarayan * Only the final task for a range of elements will respond to and
26032336Snarayan * free the message
26042336Snarayan */
26052793Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) {
26062336Snarayan return;
26072793Slm66018 }
26082336Snarayan
26096903Szk194757 /*
26106903Szk194757 * We should only send an ACK/NACK here if we are not currently in
26116903Szk194757 * reset as, depending on how we reset, the dring may have been
26126903Szk194757 * blown away and we don't want to ACK/NACK a message that isn't
26136903Szk194757 * there.
26146903Szk194757 */
26156903Szk194757 if (!vd->reset_state)
26166903Szk194757 vd_notify(task);
26174838Slm66018 }
26184838Slm66018
26194838Slm66018 /*
26204838Slm66018 * Description:
26214838Slm66018 * This is the basic completion function called to handle inband data
26224838Slm66018 * requests and handshake messages. All it needs to do is trigger a
26234838Slm66018 * message to the client that the request is completed.
26244838Slm66018 *
26254838Slm66018 * Parameters:
26264838Slm66018 * arg - opaque pointer to structure containing task to be completed
26274838Slm66018 *
26284838Slm66018 * Return Values
26294838Slm66018 * None
26304838Slm66018 */
26314838Slm66018 static void
vd_serial_notify(void * arg)26324838Slm66018 vd_serial_notify(void *arg)
26334838Slm66018 {
26344838Slm66018 vd_task_t *task = (vd_task_t *)arg;
26354838Slm66018
26364838Slm66018 ASSERT(task != NULL);
26374838Slm66018 vd_notify(task);
26381991Sheppo }
26391991Sheppo
26405658Sachartre /* ARGSUSED */
26415658Sachartre static int
vd_geom2dk_geom(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)26425658Sachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
26432032Slm66018 {
26442032Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg);
26455658Sachartre return (0);
26462032Slm66018 }
26472032Slm66018
26485658Sachartre /* ARGSUSED */
26495658Sachartre static int
vd_vtoc2vtoc(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)26505658Sachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
26512032Slm66018 {
26527563SPrasad.Singamsetty@Sun.COM VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct extvtoc *)ioctl_arg);
26535658Sachartre return (0);
26542032Slm66018 }
26552032Slm66018
26562032Slm66018 static void
dk_geom2vd_geom(void * ioctl_arg,void * vd_buf)26572032Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf)
26582032Slm66018 {
26592032Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf);
26602032Slm66018 }
26612032Slm66018
26622032Slm66018 static void
vtoc2vd_vtoc(void * ioctl_arg,void * vd_buf)26632032Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf)
26642032Slm66018 {
26657563SPrasad.Singamsetty@Sun.COM VTOC2VD_VTOC((struct extvtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf);
26662032Slm66018 }
26672032Slm66018
26685658Sachartre static int
vd_get_efi_in(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)26695658Sachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
26702531Snarayan {
26712531Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
26722531Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
26735658Sachartre size_t data_len;
26745658Sachartre
26755658Sachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
26765658Sachartre if (vd_efi->length > data_len)
26775658Sachartre return (EINVAL);
26782531Snarayan
26792531Snarayan dk_efi->dki_lba = vd_efi->lba;
26802531Snarayan dk_efi->dki_length = vd_efi->length;
26812531Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP);
26825658Sachartre return (0);
26832531Snarayan }
26842531Snarayan
26852531Snarayan static void
vd_get_efi_out(void * ioctl_arg,void * vd_buf)26862531Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf)
26872531Snarayan {
26882531Snarayan int len;
26892531Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
26902531Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
26912531Snarayan
26922531Snarayan len = vd_efi->length;
26932531Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi);
26942531Snarayan kmem_free(dk_efi->dki_data, len);
26952531Snarayan }
26962531Snarayan
26975658Sachartre static int
vd_set_efi_in(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)26985658Sachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
26992531Snarayan {
27002531Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
27012531Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
27025658Sachartre size_t data_len;
27035658Sachartre
27045658Sachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t));
27055658Sachartre if (vd_efi->length > data_len)
27065658Sachartre return (EINVAL);
27072531Snarayan
27082531Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP);
27092531Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi);
27105658Sachartre return (0);
27112531Snarayan }
27122531Snarayan
27132531Snarayan static void
vd_set_efi_out(void * ioctl_arg,void * vd_buf)27142531Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf)
27152531Snarayan {
27162531Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf;
27172531Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg;
27182531Snarayan
27192531Snarayan kmem_free(dk_efi->dki_data, vd_efi->length);
27202531Snarayan }
27212531Snarayan
27225658Sachartre static int
vd_scsicmd_in(void * vd_buf,size_t vd_buf_len,void * ioctl_arg)27235658Sachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg)
27245658Sachartre {
27255658Sachartre size_t vd_scsi_len;
27265658Sachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
27275658Sachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
27285658Sachartre
27295658Sachartre /* check buffer size */
27305658Sachartre vd_scsi_len = VD_SCSI_SIZE;
27315658Sachartre vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t));
27325658Sachartre vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t));
27335658Sachartre vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t));
27345658Sachartre vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t));
27355658Sachartre
27365658Sachartre ASSERT(vd_scsi_len % sizeof (uint64_t) == 0);
27375658Sachartre
27385658Sachartre if (vd_buf_len < vd_scsi_len)
27395658Sachartre return (EINVAL);
27405658Sachartre
27415658Sachartre /* set flags */
27425658Sachartre uscsi->uscsi_flags = vd_scsi_debug;
27435658Sachartre
27445658Sachartre if (vd_scsi->options & VD_SCSI_OPT_NORETRY) {
27455658Sachartre uscsi->uscsi_flags |= USCSI_ISOLATE;
27465658Sachartre uscsi->uscsi_flags |= USCSI_DIAGNOSE;
27475658Sachartre }
27485658Sachartre
27495658Sachartre /* task attribute */
27505658Sachartre switch (vd_scsi->task_attribute) {
27515658Sachartre case VD_SCSI_TASK_ACA:
27525658Sachartre uscsi->uscsi_flags |= USCSI_HEAD;
27535658Sachartre break;
27545658Sachartre case VD_SCSI_TASK_HQUEUE:
27555658Sachartre uscsi->uscsi_flags |= USCSI_HTAG;
27565658Sachartre break;
27575658Sachartre case VD_SCSI_TASK_ORDERED:
27585658Sachartre uscsi->uscsi_flags |= USCSI_OTAG;
27595658Sachartre break;
27605658Sachartre default:
27615658Sachartre uscsi->uscsi_flags |= USCSI_NOTAG;
27625658Sachartre break;
27635658Sachartre }
27645658Sachartre
27655658Sachartre /* timeout */
27665658Sachartre uscsi->uscsi_timeout = vd_scsi->timeout;
27675658Sachartre
27685658Sachartre /* cdb data */
27695658Sachartre uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi);
27705658Sachartre uscsi->uscsi_cdblen = vd_scsi->cdb_len;
27715658Sachartre
27725658Sachartre /* sense buffer */
27735658Sachartre if (vd_scsi->sense_len != 0) {
27745658Sachartre uscsi->uscsi_flags |= USCSI_RQENABLE;
27755658Sachartre uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi);
27765658Sachartre uscsi->uscsi_rqlen = vd_scsi->sense_len;
27775658Sachartre }
27785658Sachartre
27795658Sachartre if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) {
27805658Sachartre /* uscsi does not support read/write request */
27815658Sachartre return (EINVAL);
27825658Sachartre }
27835658Sachartre
27845658Sachartre /* request data-in */
27855658Sachartre if (vd_scsi->datain_len != 0) {
27865658Sachartre uscsi->uscsi_flags |= USCSI_READ;
27875658Sachartre uscsi->uscsi_buflen = vd_scsi->datain_len;
27885658Sachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi);
27895658Sachartre }
27905658Sachartre
27915658Sachartre /* request data-out */
27925658Sachartre if (vd_scsi->dataout_len != 0) {
27935658Sachartre uscsi->uscsi_buflen = vd_scsi->dataout_len;
27945658Sachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi);
27955658Sachartre }
27965658Sachartre
27975658Sachartre return (0);
27985658Sachartre }
27995658Sachartre
28005658Sachartre static void
vd_scsicmd_out(void * ioctl_arg,void * vd_buf)28015658Sachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf)
28025658Sachartre {
28035658Sachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf;
28045658Sachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg;
28055658Sachartre
28065658Sachartre /* output fields */
28075658Sachartre vd_scsi->cmd_status = uscsi->uscsi_status;
28085658Sachartre
28095658Sachartre /* sense data */
28105658Sachartre if ((uscsi->uscsi_flags & USCSI_RQENABLE) &&
28115658Sachartre (uscsi->uscsi_status == STATUS_CHECK ||
28125658Sachartre uscsi->uscsi_status == STATUS_TERMINATED)) {
28135658Sachartre vd_scsi->sense_status = uscsi->uscsi_rqstatus;
28145658Sachartre if (uscsi->uscsi_rqstatus == STATUS_GOOD)
28156853Szk194757 vd_scsi->sense_len -= uscsi->uscsi_rqresid;
28165658Sachartre else
28175658Sachartre vd_scsi->sense_len = 0;
28185658Sachartre } else {
28195658Sachartre vd_scsi->sense_len = 0;
28205658Sachartre }
28215658Sachartre
28225658Sachartre if (uscsi->uscsi_status != STATUS_GOOD) {
28235658Sachartre vd_scsi->dataout_len = 0;
28245658Sachartre vd_scsi->datain_len = 0;
28255658Sachartre return;
28265658Sachartre }
28275658Sachartre
28285658Sachartre if (uscsi->uscsi_flags & USCSI_READ) {
28295658Sachartre /* request data (read) */
28305658Sachartre vd_scsi->datain_len -= uscsi->uscsi_resid;
28315658Sachartre vd_scsi->dataout_len = 0;
28325658Sachartre } else {
28335658Sachartre /* request data (write) */
28345658Sachartre vd_scsi->datain_len = 0;
28355658Sachartre vd_scsi->dataout_len -= uscsi->uscsi_resid;
28365658Sachartre }
28375658Sachartre }
28385658Sachartre
28393782Sachartre static ushort_t
vd_lbl2cksum(struct dk_label * label)28403401Snarayan vd_lbl2cksum(struct dk_label *label)
28413401Snarayan {
28423401Snarayan int count;
28433782Sachartre ushort_t sum, *sp;
28443401Snarayan
28453401Snarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1;
28463782Sachartre sp = (ushort_t *)label;
28473401Snarayan sum = 0;
28483401Snarayan while (count--) {
28493401Snarayan sum ^= *sp++;
28503401Snarayan }
28513401Snarayan
28523401Snarayan return (sum);
28533401Snarayan }
28543401Snarayan
28554696Sachartre /*
28566836Sachartre * Copy information from a vtoc and dk_geom structures to a dk_label structure.
28576836Sachartre */
28586836Sachartre static void
vd_vtocgeom_to_label(struct extvtoc * vtoc,struct dk_geom * geom,struct dk_label * label)28597563SPrasad.Singamsetty@Sun.COM vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom,
28606836Sachartre struct dk_label *label)
28616836Sachartre {
28626836Sachartre int i;
28636836Sachartre
28646836Sachartre ASSERT(vtoc->v_nparts == V_NUMPAR);
28656836Sachartre ASSERT(vtoc->v_sanity == VTOC_SANE);
28666836Sachartre
28676836Sachartre bzero(label, sizeof (struct dk_label));
28686836Sachartre
28696836Sachartre label->dkl_ncyl = geom->dkg_ncyl;
28706836Sachartre label->dkl_acyl = geom->dkg_acyl;
28716836Sachartre label->dkl_pcyl = geom->dkg_pcyl;
28726836Sachartre label->dkl_nhead = geom->dkg_nhead;
28736836Sachartre label->dkl_nsect = geom->dkg_nsect;
28746836Sachartre label->dkl_intrlv = geom->dkg_intrlv;
28756836Sachartre label->dkl_apc = geom->dkg_apc;
28766836Sachartre label->dkl_rpm = geom->dkg_rpm;
28776836Sachartre label->dkl_write_reinstruct = geom->dkg_write_reinstruct;
28786836Sachartre label->dkl_read_reinstruct = geom->dkg_read_reinstruct;
28796836Sachartre
28806836Sachartre label->dkl_vtoc.v_nparts = V_NUMPAR;
28816836Sachartre label->dkl_vtoc.v_sanity = VTOC_SANE;
28826836Sachartre label->dkl_vtoc.v_version = vtoc->v_version;
28836836Sachartre for (i = 0; i < V_NUMPAR; i++) {
28846836Sachartre label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i];
28856836Sachartre label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag;
28866836Sachartre label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag;
28876836Sachartre label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start /
28886836Sachartre (label->dkl_nhead * label->dkl_nsect);
28896836Sachartre label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size;
28906836Sachartre }
28916836Sachartre
28926836Sachartre /*
28936836Sachartre * The bootinfo array can not be copied with bcopy() because
28946836Sachartre * elements are of type long in vtoc (so 64-bit) and of type
28956836Sachartre * int in dk_vtoc (so 32-bit).
28966836Sachartre */
28976836Sachartre label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0];
28986836Sachartre label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1];
28996836Sachartre label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2];
29006836Sachartre bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII);
29016836Sachartre bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL);
29026836Sachartre
29036836Sachartre /* re-compute checksum */
29046836Sachartre label->dkl_magic = DKL_MAGIC;
29056836Sachartre label->dkl_cksum = vd_lbl2cksum(label);
29066836Sachartre }
29076836Sachartre
29086836Sachartre /*
29096836Sachartre * Copy information from a dk_label structure to a vtoc and dk_geom structures.
29106836Sachartre */
29116836Sachartre static void
vd_label_to_vtocgeom(struct dk_label * label,struct extvtoc * vtoc,struct dk_geom * geom)29127563SPrasad.Singamsetty@Sun.COM vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc,
29136836Sachartre struct dk_geom *geom)
29146836Sachartre {
29156836Sachartre int i;
29166836Sachartre
291710063SAlexandre.Chartre@Sun.COM bzero(vtoc, sizeof (struct extvtoc));
29186836Sachartre bzero(geom, sizeof (struct dk_geom));
29196836Sachartre
29206836Sachartre geom->dkg_ncyl = label->dkl_ncyl;
29216836Sachartre geom->dkg_acyl = label->dkl_acyl;
29226836Sachartre geom->dkg_nhead = label->dkl_nhead;
29236836Sachartre geom->dkg_nsect = label->dkl_nsect;
29246836Sachartre geom->dkg_intrlv = label->dkl_intrlv;
29256836Sachartre geom->dkg_apc = label->dkl_apc;
29266836Sachartre geom->dkg_rpm = label->dkl_rpm;
29276836Sachartre geom->dkg_pcyl = label->dkl_pcyl;
29286836Sachartre geom->dkg_write_reinstruct = label->dkl_write_reinstruct;
29296836Sachartre geom->dkg_read_reinstruct = label->dkl_read_reinstruct;
29306836Sachartre
29316836Sachartre vtoc->v_sanity = label->dkl_vtoc.v_sanity;
29326836Sachartre vtoc->v_version = label->dkl_vtoc.v_version;
29336836Sachartre vtoc->v_sectorsz = DEV_BSIZE;
29346836Sachartre vtoc->v_nparts = label->dkl_vtoc.v_nparts;
29356836Sachartre
29366836Sachartre for (i = 0; i < vtoc->v_nparts; i++) {
29376836Sachartre vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag;
29386836Sachartre vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag;
29396836Sachartre vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno *
29406836Sachartre (label->dkl_nhead * label->dkl_nsect);
29416836Sachartre vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk;
29426836Sachartre vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i];
29436836Sachartre }
29446836Sachartre
29456836Sachartre /*
29466836Sachartre * The bootinfo array can not be copied with bcopy() because
29476836Sachartre * elements are of type long in vtoc (so 64-bit) and of type
29486836Sachartre * int in dk_vtoc (so 32-bit).
29496836Sachartre */
29506836Sachartre vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0];
29516836Sachartre vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1];
29526836Sachartre vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2];
29536836Sachartre bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII);
29546836Sachartre bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL);
29556836Sachartre }
29566836Sachartre
29576836Sachartre /*
29586836Sachartre * Check if a geometry is valid for a single-slice disk. A geometry is
29596836Sachartre * considered valid if the main attributes of the geometry match with the
29606836Sachartre * attributes of the fake geometry we have created.
29616836Sachartre */
29626836Sachartre static boolean_t
vd_slice_geom_isvalid(vd_t * vd,struct dk_geom * geom)29636836Sachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom)
29646836Sachartre {
29656836Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
29666836Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
29676836Sachartre
29686836Sachartre if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl ||
29696836Sachartre geom->dkg_acyl != vd->dk_geom.dkg_acyl ||
29706836Sachartre geom->dkg_nsect != vd->dk_geom.dkg_nsect ||
29716836Sachartre geom->dkg_pcyl != vd->dk_geom.dkg_pcyl)
29726836Sachartre return (B_FALSE);
29736836Sachartre
29746836Sachartre return (B_TRUE);
29756836Sachartre }
29766836Sachartre
29776836Sachartre /*
29786836Sachartre * Check if a vtoc is valid for a single-slice disk. A vtoc is considered
29796836Sachartre * valid if the main attributes of the vtoc match with the attributes of the
29806836Sachartre * fake vtoc we have created.
29816836Sachartre */
29826836Sachartre static boolean_t
vd_slice_vtoc_isvalid(vd_t * vd,struct extvtoc * vtoc)29837563SPrasad.Singamsetty@Sun.COM vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc)
29846836Sachartre {
29856836Sachartre size_t csize;
29866836Sachartre int i;
29876836Sachartre
29886836Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
29896836Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC);
29906836Sachartre
29916836Sachartre if (vtoc->v_sanity != vd->vtoc.v_sanity ||
29926836Sachartre vtoc->v_version != vd->vtoc.v_version ||
29936836Sachartre vtoc->v_nparts != vd->vtoc.v_nparts ||
29946836Sachartre strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 ||
29956836Sachartre strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0)
29966836Sachartre return (B_FALSE);
29976836Sachartre
29986836Sachartre /* slice 2 should be unchanged */
29996836Sachartre if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start !=
30006836Sachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start ||
30016836Sachartre vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size !=
30026836Sachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size)
30036836Sachartre return (B_FALSE);
30046836Sachartre
30056836Sachartre /*
30066836Sachartre * Slice 0 should be mostly unchanged and cover most of the disk.
30076836Sachartre * However we allow some flexibility wrt to the start and the size
30086836Sachartre * of this slice mainly because we can't exactly know how it will
30096836Sachartre * be defined by the OS installer.
30106836Sachartre *
30116836Sachartre * We allow slice 0 to be defined as starting on any of the first
30126836Sachartre * 4 cylinders.
30136836Sachartre */
30146836Sachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
30156836Sachartre
30166836Sachartre if (vtoc->v_part[0].p_start > 4 * csize ||
30176836Sachartre vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size)
30186836Sachartre return (B_FALSE);
30196836Sachartre
30206836Sachartre if (vd->vtoc.v_part[0].p_size >= 4 * csize &&
30216836Sachartre vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize)
30226836Sachartre return (B_FALSE);
30236836Sachartre
30246836Sachartre /* any other slice should have a size of 0 */
30256836Sachartre for (i = 1; i < vtoc->v_nparts; i++) {
30266836Sachartre if (i != VD_ENTIRE_DISK_SLICE &&
30276836Sachartre vtoc->v_part[i].p_size != 0)
30286836Sachartre return (B_FALSE);
30296836Sachartre }
30306836Sachartre
30316836Sachartre return (B_TRUE);
30326836Sachartre }
30336836Sachartre
30346836Sachartre /*
30354696Sachartre * Handle ioctls to a disk slice.
30364838Slm66018 *
30374838Slm66018 * Return Values
30384838Slm66018 * 0 - Indicates that there are no errors in disk operations
30394838Slm66018 * ENOTSUP - Unknown disk label type or unsupported DKIO ioctl
30404838Slm66018 * EINVAL - Not enough room to copy the EFI label
30414838Slm66018 *
30424696Sachartre */
30431991Sheppo static int
vd_do_slice_ioctl(vd_t * vd,int cmd,void * ioctl_arg)30442032Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
30451991Sheppo {
30462531Snarayan dk_efi_t *dk_ioc;
30477563SPrasad.Singamsetty@Sun.COM struct extvtoc *vtoc;
30486836Sachartre struct dk_geom *geom;
30496836Sachartre size_t len, lba;
30505874Sachartre
30515874Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
30525874Sachartre
30537791SAlexandre.Chartre@Sun.COM if (cmd == DKIOCFLUSHWRITECACHE)
30547791SAlexandre.Chartre@Sun.COM return (vd_flush_write(vd));
30552531Snarayan
30562531Snarayan switch (vd->vdisk_label) {
30572531Snarayan
30585874Sachartre /* ioctls for a single slice disk with a VTOC label */
30592531Snarayan case VD_DISK_LABEL_VTOC:
30602531Snarayan
30612531Snarayan switch (cmd) {
30626836Sachartre
30632531Snarayan case DKIOCGGEOM:
30642531Snarayan ASSERT(ioctl_arg != NULL);
30652531Snarayan bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom));
30662531Snarayan return (0);
30676836Sachartre
30687563SPrasad.Singamsetty@Sun.COM case DKIOCGEXTVTOC:
30692531Snarayan ASSERT(ioctl_arg != NULL);
30702531Snarayan bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc));
30712531Snarayan return (0);
30726836Sachartre
30736836Sachartre case DKIOCSGEOM:
30746836Sachartre ASSERT(ioctl_arg != NULL);
30756836Sachartre if (vd_slice_single_slice)
30766836Sachartre return (ENOTSUP);
30776836Sachartre
30786836Sachartre /* fake success only if new geometry is valid */
30796836Sachartre geom = (struct dk_geom *)ioctl_arg;
30806836Sachartre if (!vd_slice_geom_isvalid(vd, geom))
30816836Sachartre return (EINVAL);
30826836Sachartre
30836836Sachartre return (0);
30846836Sachartre
30857563SPrasad.Singamsetty@Sun.COM case DKIOCSEXTVTOC:
30866836Sachartre ASSERT(ioctl_arg != NULL);
30876836Sachartre if (vd_slice_single_slice)
30886836Sachartre return (ENOTSUP);
30896836Sachartre
30906836Sachartre /* fake sucess only if the new vtoc is valid */
30917563SPrasad.Singamsetty@Sun.COM vtoc = (struct extvtoc *)ioctl_arg;
30926836Sachartre if (!vd_slice_vtoc_isvalid(vd, vtoc))
30936836Sachartre return (EINVAL);
30946836Sachartre
30956836Sachartre return (0);
30966836Sachartre
30972531Snarayan default:
30982531Snarayan return (ENOTSUP);
30992531Snarayan }
31002531Snarayan
31015874Sachartre /* ioctls for a single slice disk with an EFI label */
31022531Snarayan case VD_DISK_LABEL_EFI:
31032531Snarayan
31046836Sachartre if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI)
31056836Sachartre return (ENOTSUP);
31066836Sachartre
31076836Sachartre ASSERT(ioctl_arg != NULL);
31086836Sachartre dk_ioc = (dk_efi_t *)ioctl_arg;
31096836Sachartre
31106836Sachartre len = dk_ioc->dki_length;
31116836Sachartre lba = dk_ioc->dki_lba;
31126836Sachartre
31136836Sachartre if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) ||
31146836Sachartre (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) ||
31156836Sachartre (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t)))
31166836Sachartre return (EINVAL);
31176836Sachartre
31182531Snarayan switch (cmd) {
31192531Snarayan case DKIOCGETEFI:
31206836Sachartre len = vd_slice_flabel_read(vd,
31219889SLarry.Liu@Sun.COM (caddr_t)dk_ioc->dki_data,
31229889SLarry.Liu@Sun.COM lba * vd->vdisk_bsize, len);
31236836Sachartre
31246836Sachartre ASSERT(len > 0);
31255874Sachartre
31262531Snarayan return (0);
31276836Sachartre
31286836Sachartre case DKIOCSETEFI:
31296836Sachartre if (vd_slice_single_slice)
31306836Sachartre return (ENOTSUP);
31316836Sachartre
31326836Sachartre /* we currently don't support writing EFI */
31336836Sachartre return (EIO);
31342531Snarayan }
31352531Snarayan
31361991Sheppo default:
31374838Slm66018 /* Unknown disk label type */
31381991Sheppo return (ENOTSUP);
31391991Sheppo }
31401991Sheppo }
31411991Sheppo
31425874Sachartre static int
vds_efi_alloc_and_read(vd_t * vd,efi_gpt_t ** gpt,efi_gpe_t ** gpe)31435874Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe)
31445874Sachartre {
31455874Sachartre vd_efi_dev_t edev;
31465874Sachartre int status;
31475874Sachartre
31485874Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
31495874Sachartre
31505874Sachartre status = vd_efi_alloc_and_read(&edev, gpt, gpe);
31515874Sachartre
31525874Sachartre return (status);
31535874Sachartre }
31545874Sachartre
31555874Sachartre static void
vds_efi_free(vd_t * vd,efi_gpt_t * gpt,efi_gpe_t * gpe)31565874Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe)
31575874Sachartre {
31585874Sachartre vd_efi_dev_t edev;
31595874Sachartre
31605874Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl);
31615874Sachartre
31625874Sachartre vd_efi_free(&edev, gpt, gpe);
31635874Sachartre }
31645874Sachartre
31655874Sachartre static int
vd_dskimg_validate_efi(vd_t * vd)31667597SAlexandre.Chartre@Sun.COM vd_dskimg_validate_efi(vd_t *vd)
31675874Sachartre {
31685874Sachartre efi_gpt_t *gpt;
31695874Sachartre efi_gpe_t *gpe;
31705874Sachartre int i, nparts, status;
31715874Sachartre struct uuid efi_reserved = EFI_RESERVED;
31725874Sachartre
31735874Sachartre if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0)
31745874Sachartre return (status);
31755874Sachartre
31767563SPrasad.Singamsetty@Sun.COM bzero(&vd->vtoc, sizeof (struct extvtoc));
31775874Sachartre bzero(&vd->dk_geom, sizeof (struct dk_geom));
31785874Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
31795874Sachartre
31805874Sachartre vd->efi_reserved = -1;
31815874Sachartre
31825874Sachartre nparts = gpt->efi_gpt_NumberOfPartitionEntries;
31835874Sachartre
31845874Sachartre for (i = 0; i < nparts && i < VD_MAXPART; i++) {
31855874Sachartre
31868528SAlexandre.Chartre@Sun.COM if (gpe[i].efi_gpe_StartingLBA == 0 &&
31875874Sachartre gpe[i].efi_gpe_EndingLBA == 0) {
31885874Sachartre continue;
31895874Sachartre }
31905874Sachartre
31915874Sachartre vd->slices[i].start = gpe[i].efi_gpe_StartingLBA;
31925874Sachartre vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA -
31935874Sachartre gpe[i].efi_gpe_StartingLBA + 1;
31945874Sachartre
31955874Sachartre if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved,
31965874Sachartre sizeof (struct uuid)) == 0)
31975874Sachartre vd->efi_reserved = i;
31985874Sachartre
31995874Sachartre }
32005874Sachartre
32015874Sachartre ASSERT(vd->vdisk_size != 0);
32025874Sachartre vd->slices[VD_EFI_WD_SLICE].start = 0;
32035874Sachartre vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size;
32045874Sachartre
32055874Sachartre vds_efi_free(vd, gpt, gpe);
32065874Sachartre
32075874Sachartre return (status);
32085874Sachartre }
32095874Sachartre
32104696Sachartre /*
32114963Sachartre * Function:
32127597SAlexandre.Chartre@Sun.COM * vd_dskimg_validate_geometry
32134963Sachartre *
32144963Sachartre * Description:
32154963Sachartre * Read the label and validate the geometry of a disk image. The driver
32164963Sachartre * label, vtoc and geometry information are updated according to the
32174963Sachartre * label read from the disk image.
32184963Sachartre *
32194963Sachartre * If no valid label is found, the label is set to unknown and the
32204963Sachartre * function returns EINVAL, but a default vtoc and geometry are provided
32215874Sachartre * to the driver. If an EFI label is found, ENOTSUP is returned.
32224963Sachartre *
32234963Sachartre * Parameters:
32244963Sachartre * vd - disk on which the operation is performed.
32254963Sachartre *
32264963Sachartre * Return Code:
32274963Sachartre * 0 - success.
32284963Sachartre * EIO - error reading the label from the disk image.
32294963Sachartre * EINVAL - unknown disk label.
32305874Sachartre * ENOTSUP - geometry not applicable (EFI label).
32314963Sachartre */
32324963Sachartre static int
vd_dskimg_validate_geometry(vd_t * vd)32337597SAlexandre.Chartre@Sun.COM vd_dskimg_validate_geometry(vd_t *vd)
32344963Sachartre {
32354963Sachartre struct dk_label label;
32364963Sachartre struct dk_geom *geom = &vd->dk_geom;
32377563SPrasad.Singamsetty@Sun.COM struct extvtoc *vtoc = &vd->vtoc;
32384963Sachartre int i;
32394963Sachartre int status = 0;
32404963Sachartre
32417597SAlexandre.Chartre@Sun.COM ASSERT(VD_DSKIMG(vd));
32427597SAlexandre.Chartre@Sun.COM
32437597SAlexandre.Chartre@Sun.COM if (VD_DSKIMG_LABEL_READ(vd, &label) < 0)
32445874Sachartre return (EIO);
32455874Sachartre
32465874Sachartre if (label.dkl_magic != DKL_MAGIC ||
32475874Sachartre label.dkl_cksum != vd_lbl2cksum(&label) ||
32487597SAlexandre.Chartre@Sun.COM (vd_dskimg_validate_sanity &&
32497597SAlexandre.Chartre@Sun.COM label.dkl_vtoc.v_sanity != VTOC_SANE) ||
32505874Sachartre label.dkl_vtoc.v_nparts != V_NUMPAR) {
32515874Sachartre
32527597SAlexandre.Chartre@Sun.COM if (vd_dskimg_validate_efi(vd) == 0) {
32535874Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI;
32545874Sachartre return (ENOTSUP);
32555874Sachartre }
32565874Sachartre
32575874Sachartre vd->vdisk_label = VD_DISK_LABEL_UNK;
32589889SLarry.Liu@Sun.COM vd_build_default_label(vd->dskimg_size, vd->vdisk_bsize,
32599889SLarry.Liu@Sun.COM &label);
32605874Sachartre status = EINVAL;
32615874Sachartre } else {
32625081Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC;
32634963Sachartre }
32644963Sachartre
32656836Sachartre /* Update the driver geometry and vtoc */
32666836Sachartre vd_label_to_vtocgeom(&label, vtoc, geom);
32674963Sachartre
32685874Sachartre /* Update logical partitions */
32695874Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART);
32705874Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
32715874Sachartre for (i = 0; i < vtoc->v_nparts; i++) {
32725874Sachartre vd->slices[i].start = vtoc->v_part[i].p_start;
32735874Sachartre vd->slices[i].nblocks = vtoc->v_part[i].p_size;
32745874Sachartre }
32755874Sachartre }
32765874Sachartre
32774963Sachartre return (status);
32784963Sachartre }
32794963Sachartre
32804963Sachartre /*
32817597SAlexandre.Chartre@Sun.COM * Handle ioctls to a disk image.
32824838Slm66018 *
32834838Slm66018 * Return Values
32844838Slm66018 * 0 - Indicates that there are no errors
32854838Slm66018 * != 0 - Disk operation returned an error
32864696Sachartre */
32874696Sachartre static int
vd_do_dskimg_ioctl(vd_t * vd,int cmd,void * ioctl_arg)32887597SAlexandre.Chartre@Sun.COM vd_do_dskimg_ioctl(vd_t *vd, int cmd, void *ioctl_arg)
32894696Sachartre {
32904696Sachartre struct dk_label label;
32914696Sachartre struct dk_geom *geom;
32927563SPrasad.Singamsetty@Sun.COM struct extvtoc *vtoc;
32935874Sachartre dk_efi_t *efi;
32947791SAlexandre.Chartre@Sun.COM int rc;
32957597SAlexandre.Chartre@Sun.COM
32967597SAlexandre.Chartre@Sun.COM ASSERT(VD_DSKIMG(vd));
32974696Sachartre
32984696Sachartre switch (cmd) {
32994696Sachartre
33004696Sachartre case DKIOCGGEOM:
33014696Sachartre ASSERT(ioctl_arg != NULL);
33024696Sachartre geom = (struct dk_geom *)ioctl_arg;
33034696Sachartre
33047597SAlexandre.Chartre@Sun.COM rc = vd_dskimg_validate_geometry(vd);
33055874Sachartre if (rc != 0 && rc != EINVAL)
33064963Sachartre return (rc);
33074963Sachartre bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom));
33084696Sachartre return (0);
33094696Sachartre
33107563SPrasad.Singamsetty@Sun.COM case DKIOCGEXTVTOC:
33114696Sachartre ASSERT(ioctl_arg != NULL);
33127563SPrasad.Singamsetty@Sun.COM vtoc = (struct extvtoc *)ioctl_arg;
33134696Sachartre
33147597SAlexandre.Chartre@Sun.COM rc = vd_dskimg_validate_geometry(vd);
33155874Sachartre if (rc != 0 && rc != EINVAL)
33164963Sachartre return (rc);
33177563SPrasad.Singamsetty@Sun.COM bcopy(&vd->vtoc, vtoc, sizeof (struct extvtoc));
33184696Sachartre return (0);
33194696Sachartre
33204696Sachartre case DKIOCSGEOM:
33214696Sachartre ASSERT(ioctl_arg != NULL);
33224696Sachartre geom = (struct dk_geom *)ioctl_arg;
33234696Sachartre
33244696Sachartre if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0)
33254696Sachartre return (EINVAL);
33264696Sachartre
33274696Sachartre /*
33284696Sachartre * The current device geometry is not updated, just the driver
33294696Sachartre * "notion" of it. The device geometry will be effectively
33304696Sachartre * updated when a label is written to the device during a next
33317563SPrasad.Singamsetty@Sun.COM * DKIOCSEXTVTOC.
33324696Sachartre */
33334696Sachartre bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom));
33344696Sachartre return (0);
33354696Sachartre
33367563SPrasad.Singamsetty@Sun.COM case DKIOCSEXTVTOC:
33374696Sachartre ASSERT(ioctl_arg != NULL);
33384696Sachartre ASSERT(vd->dk_geom.dkg_nhead != 0 &&
33394696Sachartre vd->dk_geom.dkg_nsect != 0);
33407563SPrasad.Singamsetty@Sun.COM vtoc = (struct extvtoc *)ioctl_arg;
33414696Sachartre
33424696Sachartre if (vtoc->v_sanity != VTOC_SANE ||
33434696Sachartre vtoc->v_sectorsz != DEV_BSIZE ||
33444696Sachartre vtoc->v_nparts != V_NUMPAR)
33454696Sachartre return (EINVAL);
33464696Sachartre
33476836Sachartre vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label);
33484696Sachartre
33494696Sachartre /* write label to the disk image */
33507597SAlexandre.Chartre@Sun.COM if ((rc = vd_dskimg_set_vtoc(vd, &label)) != 0)
33514696Sachartre return (rc);
33524696Sachartre
33535874Sachartre break;
33544696Sachartre
33555188Szk194757 case DKIOCFLUSHWRITECACHE:
33567791SAlexandre.Chartre@Sun.COM return (vd_flush_write(vd));
33575188Szk194757
33585874Sachartre case DKIOCGETEFI:
33595874Sachartre ASSERT(ioctl_arg != NULL);
33605874Sachartre efi = (dk_efi_t *)ioctl_arg;
33615874Sachartre
33627597SAlexandre.Chartre@Sun.COM if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD,
33635874Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
33645874Sachartre return (EIO);
33655874Sachartre
33665874Sachartre return (0);
33675874Sachartre
33685874Sachartre case DKIOCSETEFI:
33695874Sachartre ASSERT(ioctl_arg != NULL);
33705874Sachartre efi = (dk_efi_t *)ioctl_arg;
33715874Sachartre
33727597SAlexandre.Chartre@Sun.COM if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE,
33735874Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0)
33745874Sachartre return (EIO);
33755874Sachartre
33765874Sachartre break;
33775874Sachartre
33785874Sachartre
33794696Sachartre default:
33804696Sachartre return (ENOTSUP);
33814696Sachartre }
33825874Sachartre
33837563SPrasad.Singamsetty@Sun.COM ASSERT(cmd == DKIOCSEXTVTOC || cmd == DKIOCSETEFI);
33845874Sachartre
33855874Sachartre /* label has changed, revalidate the geometry */
33867597SAlexandre.Chartre@Sun.COM (void) vd_dskimg_validate_geometry(vd);
33875874Sachartre
33885874Sachartre /*
33895874Sachartre * The disk geometry may have changed, so we need to write
33905874Sachartre * the devid (if there is one) so that it is stored at the
33915874Sachartre * right location.
33925874Sachartre */
33937597SAlexandre.Chartre@Sun.COM if (vd_dskimg_write_devid(vd, vd->dskimg_devid) != 0) {
33945874Sachartre PR0("Fail to write devid");
33955874Sachartre }
33965874Sachartre
33975874Sachartre return (0);
33985874Sachartre }
33995874Sachartre
34005874Sachartre static int
vd_backend_ioctl(vd_t * vd,int cmd,caddr_t arg)34015874Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg)
34025874Sachartre {
34035874Sachartre int rval = 0, status;
34047563SPrasad.Singamsetty@Sun.COM struct vtoc vtoc;
34055874Sachartre
34065874Sachartre /*
34075874Sachartre * Call the appropriate function to execute the ioctl depending
34085874Sachartre * on the type of vdisk.
34095874Sachartre */
34105874Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
34115874Sachartre
34125874Sachartre /* slice, file or volume exported as a single slice disk */
34135874Sachartre status = vd_do_slice_ioctl(vd, cmd, arg);
34145874Sachartre
34157597SAlexandre.Chartre@Sun.COM } else if (VD_DSKIMG(vd)) {
34165874Sachartre
34175874Sachartre /* file or volume exported as a full disk */
34187597SAlexandre.Chartre@Sun.COM status = vd_do_dskimg_ioctl(vd, cmd, arg);
34195874Sachartre
34205874Sachartre } else {
34215874Sachartre
34225874Sachartre /* disk device exported as a full disk */
34235874Sachartre status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg,
34245874Sachartre vd->open_flags | FKIOCTL, kcred, &rval);
34257563SPrasad.Singamsetty@Sun.COM
34267563SPrasad.Singamsetty@Sun.COM /*
34277563SPrasad.Singamsetty@Sun.COM * By default VTOC ioctls are done using ioctls for the
34287563SPrasad.Singamsetty@Sun.COM * extended VTOC. Some drivers (in particular non-Sun drivers)
34297563SPrasad.Singamsetty@Sun.COM * may not support these ioctls. In that case, we fallback to
34307563SPrasad.Singamsetty@Sun.COM * the regular VTOC ioctls.
34317563SPrasad.Singamsetty@Sun.COM */
34327563SPrasad.Singamsetty@Sun.COM if (status == ENOTTY) {
34337563SPrasad.Singamsetty@Sun.COM switch (cmd) {
34347563SPrasad.Singamsetty@Sun.COM
34357563SPrasad.Singamsetty@Sun.COM case DKIOCGEXTVTOC:
34367563SPrasad.Singamsetty@Sun.COM cmd = DKIOCGVTOC;
34377563SPrasad.Singamsetty@Sun.COM status = ldi_ioctl(vd->ldi_handle[0], cmd,
34387563SPrasad.Singamsetty@Sun.COM (intptr_t)&vtoc, vd->open_flags | FKIOCTL,
34397563SPrasad.Singamsetty@Sun.COM kcred, &rval);
34407563SPrasad.Singamsetty@Sun.COM vtoctoextvtoc(vtoc,
34417563SPrasad.Singamsetty@Sun.COM (*(struct extvtoc *)(void *)arg));
34427563SPrasad.Singamsetty@Sun.COM break;
34437563SPrasad.Singamsetty@Sun.COM
34447563SPrasad.Singamsetty@Sun.COM case DKIOCSEXTVTOC:
34457563SPrasad.Singamsetty@Sun.COM cmd = DKIOCSVTOC;
34467563SPrasad.Singamsetty@Sun.COM extvtoctovtoc((*(struct extvtoc *)(void *)arg),
34477563SPrasad.Singamsetty@Sun.COM vtoc);
34487563SPrasad.Singamsetty@Sun.COM status = ldi_ioctl(vd->ldi_handle[0], cmd,
34497563SPrasad.Singamsetty@Sun.COM (intptr_t)&vtoc, vd->open_flags | FKIOCTL,
34507563SPrasad.Singamsetty@Sun.COM kcred, &rval);
34517563SPrasad.Singamsetty@Sun.COM break;
34527563SPrasad.Singamsetty@Sun.COM }
34537563SPrasad.Singamsetty@Sun.COM }
34545874Sachartre }
34555874Sachartre
34565874Sachartre #ifdef DEBUG
34575874Sachartre if (rval != 0) {
34585874Sachartre PR0("ioctl %x set rval = %d, which is not being returned"
34595874Sachartre " to caller", cmd, rval);
34605874Sachartre }
34615874Sachartre #endif /* DEBUG */
34625874Sachartre
34635874Sachartre return (status);
34644696Sachartre }
34654696Sachartre
34664838Slm66018 /*
34674838Slm66018 * Description:
34684838Slm66018 * This is the function that processes the ioctl requests (farming it
34694838Slm66018 * out to functions that handle slices, files or whole disks)
34704838Slm66018 *
34714838Slm66018 * Return Values
34724838Slm66018 * 0 - ioctl operation completed successfully
34734838Slm66018 * != 0 - The LDC error value encountered
34744838Slm66018 * (propagated back up the call stack as a task error)
34754838Slm66018 *
34764838Slm66018 * Side Effect
34774838Slm66018 * sets request->status to the return value of the ioctl function.
34784838Slm66018 */
34791991Sheppo static int
vd_do_ioctl(vd_t * vd,vd_dring_payload_t * request,void * buf,vd_ioctl_t * ioctl)34802032Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl)
34811991Sheppo {
34825874Sachartre int status = 0;
34831991Sheppo size_t nbytes = request->nbytes; /* modifiable copy */
34841991Sheppo
34851991Sheppo
34861991Sheppo ASSERT(request->slice < vd->nslices);
34871991Sheppo PR0("Performing %s", ioctl->operation_name);
34881991Sheppo
34892032Slm66018 /* Get data from client and convert, if necessary */
34902032Slm66018 if (ioctl->copyin != NULL) {
34911991Sheppo ASSERT(nbytes != 0 && buf != NULL);
34921991Sheppo PR1("Getting \"arg\" data from client");
34931991Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
34944696Sachartre request->cookie, request->ncookies,
34954696Sachartre LDC_COPY_IN)) != 0) {
34962793Slm66018 PR0("ldc_mem_copy() returned errno %d "
34971991Sheppo "copying from client", status);
34981991Sheppo return (status);
34991991Sheppo }
35002032Slm66018
35012032Slm66018 /* Convert client's data, if necessary */
35025658Sachartre if (ioctl->copyin == VD_IDENTITY_IN) {
35035658Sachartre /* use client buffer */
35042032Slm66018 ioctl->arg = buf;
35055658Sachartre } else {
35065658Sachartre /* convert client vdisk operation data to ioctl data */
35075658Sachartre status = (ioctl->copyin)(buf, nbytes,
35085658Sachartre (void *)ioctl->arg);
35095658Sachartre if (status != 0) {
35105658Sachartre request->status = status;
35115658Sachartre return (0);
35125658Sachartre }
35135658Sachartre }
35145658Sachartre }
35155658Sachartre
35165658Sachartre if (ioctl->operation == VD_OP_SCSICMD) {
35175658Sachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg;
35185658Sachartre
35195658Sachartre /* check write permission */
35205658Sachartre if (!(vd->open_flags & FWRITE) &&
35215658Sachartre !(uscsi->uscsi_flags & USCSI_READ)) {
35225658Sachartre PR0("uscsi fails because backend is opened read-only");
35235658Sachartre request->status = EROFS;
35245658Sachartre return (0);
35255658Sachartre }
35261991Sheppo }
35271991Sheppo
35281991Sheppo /*
35295874Sachartre * Send the ioctl to the disk backend.
35301991Sheppo */
35315874Sachartre request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg);
35324838Slm66018
35334838Slm66018 if (request->status != 0) {
35344838Slm66018 PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status);
35355658Sachartre if (ioctl->operation == VD_OP_SCSICMD &&
35365658Sachartre ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0)
35375658Sachartre /*
35385658Sachartre * USCSICMD has reported an error and the uscsi_status
35395658Sachartre * field is not zero. This means that the SCSI command
35405658Sachartre * has completed but it has an error. So we should
35415658Sachartre * mark the VD operation has succesfully completed
35425658Sachartre * and clients can check the SCSI status field for
35435658Sachartre * SCSI errors.
35445658Sachartre */
35455658Sachartre request->status = 0;
35465658Sachartre else
35475658Sachartre return (0);
35481991Sheppo }
35491991Sheppo
35502032Slm66018 /* Convert data and send to client, if necessary */
35512032Slm66018 if (ioctl->copyout != NULL) {
35521991Sheppo ASSERT(nbytes != 0 && buf != NULL);
35531991Sheppo PR1("Sending \"arg\" data to client");
35542032Slm66018
35552032Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */
35565658Sachartre if (ioctl->copyout != VD_IDENTITY_OUT)
35572032Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf);
35582032Slm66018
35591991Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes,
35604696Sachartre request->cookie, request->ncookies,
35614696Sachartre LDC_COPY_OUT)) != 0) {
35622793Slm66018 PR0("ldc_mem_copy() returned errno %d "
35631991Sheppo "copying to client", status);
35641991Sheppo return (status);
35651991Sheppo }
35661991Sheppo }
35671991Sheppo
35681991Sheppo return (status);
35691991Sheppo }
35701991Sheppo
35711991Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t))
35724838Slm66018
35734838Slm66018 /*
35744838Slm66018 * Description:
35754838Slm66018 * This generic function is called by the task queue to complete
35764838Slm66018 * the processing of the tasks. The specific completion function
35774838Slm66018 * is passed in as a field in the task pointer.
35784838Slm66018 *
35794838Slm66018 * Parameters:
35804838Slm66018 * arg - opaque pointer to structure containing task to be completed
35814838Slm66018 *
35824838Slm66018 * Return Values
35834838Slm66018 * None
35844838Slm66018 */
35854838Slm66018 static void
vd_complete(void * arg)35864838Slm66018 vd_complete(void *arg)
35874838Slm66018 {
35884838Slm66018 vd_task_t *task = (vd_task_t *)arg;
35894838Slm66018
35904838Slm66018 ASSERT(task != NULL);
35914838Slm66018 ASSERT(task->status == EINPROGRESS);
35924838Slm66018 ASSERT(task->completef != NULL);
35934838Slm66018
35944838Slm66018 task->status = task->completef(task);
35954838Slm66018 if (task->status)
35964838Slm66018 PR0("%s: Error %d completing task", __func__, task->status);
35974838Slm66018
35984838Slm66018 /* Now notify the vDisk client */
35994838Slm66018 vd_complete_notify(task);
36004838Slm66018 }
36014838Slm66018
36021991Sheppo static int
vd_ioctl(vd_task_t * task)36032336Snarayan vd_ioctl(vd_task_t *task)
36041991Sheppo {
36054696Sachartre int i, status;
36062336Snarayan void *buf = NULL;
36072336Snarayan struct dk_geom dk_geom = {0};
36087563SPrasad.Singamsetty@Sun.COM struct extvtoc vtoc = {0};
36092531Snarayan struct dk_efi dk_efi = {0};
36105658Sachartre struct uscsi_cmd uscsi = {0};
36112336Snarayan vd_t *vd = task->vd;
36122336Snarayan vd_dring_payload_t *request = task->request;
36132336Snarayan vd_ioctl_t ioctl[] = {
36141991Sheppo /* Command (no-copy) operations */
36152032Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0,
36162032Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE),
36175081Sachartre NULL, NULL, NULL, B_TRUE},
36181991Sheppo
36191991Sheppo /* "Get" (copy-out) operations */
36202032Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int),
36212032Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE),
36225658Sachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE},
36232032Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM),
36242032Slm66018 RNDSIZE(vd_geom_t),
36252032Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM),
36265081Sachartre &dk_geom, NULL, dk_geom2vd_geom, B_FALSE},
36272032Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t),
36287563SPrasad.Singamsetty@Sun.COM DKIOCGEXTVTOC, STRINGIZE(DKIOCGEXTVTOC),
36295081Sachartre &vtoc, NULL, vtoc2vd_vtoc, B_FALSE},
36302531Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t),
36312531Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI),
36325081Sachartre &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE},
36331991Sheppo
36341991Sheppo /* "Set" (copy-in) operations */
36352032Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int),
36362032Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE),
36375658Sachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE},
36382032Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM),
36392032Slm66018 RNDSIZE(vd_geom_t),
36402032Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM),
36415081Sachartre &dk_geom, vd_geom2dk_geom, NULL, B_TRUE},
36422032Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t),
36437563SPrasad.Singamsetty@Sun.COM DKIOCSEXTVTOC, STRINGIZE(DKIOCSEXTVTOC),
36445081Sachartre &vtoc, vd_vtoc2vtoc, NULL, B_TRUE},
36452531Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t),
36462531Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI),
36475081Sachartre &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE},
36485658Sachartre
36495658Sachartre {VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t),
36505658Sachartre USCSICMD, STRINGIZE(USCSICMD),
36515658Sachartre &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE},
36521991Sheppo };
36531991Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0]));
36541991Sheppo
36551991Sheppo
36562336Snarayan ASSERT(vd != NULL);
36572336Snarayan ASSERT(request != NULL);
36581991Sheppo ASSERT(request->slice < vd->nslices);
36591991Sheppo
36601991Sheppo /*
36611991Sheppo * Determine ioctl corresponding to caller's "operation" and
36621991Sheppo * validate caller's "nbytes"
36631991Sheppo */
36641991Sheppo for (i = 0; i < nioctls; i++) {
36651991Sheppo if (request->operation == ioctl[i].operation) {
36662032Slm66018 /* LDC memory operations require 8-byte multiples */
36672032Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0);
36682032Slm66018
36692531Snarayan if (request->operation == VD_OP_GET_EFI ||
36705658Sachartre request->operation == VD_OP_SET_EFI ||
36715658Sachartre request->operation == VD_OP_SCSICMD) {
36722531Snarayan if (request->nbytes >= ioctl[i].nbytes)
36732531Snarayan break;
36742793Slm66018 PR0("%s: Expected at least nbytes = %lu, "
36752531Snarayan "got %lu", ioctl[i].operation_name,
36762531Snarayan ioctl[i].nbytes, request->nbytes);
36772531Snarayan return (EINVAL);
36782531Snarayan }
36792531Snarayan
36802032Slm66018 if (request->nbytes != ioctl[i].nbytes) {
36812793Slm66018 PR0("%s: Expected nbytes = %lu, got %lu",
36822032Slm66018 ioctl[i].operation_name, ioctl[i].nbytes,
36832032Slm66018 request->nbytes);
36841991Sheppo return (EINVAL);
36851991Sheppo }
36861991Sheppo
36871991Sheppo break;
36881991Sheppo }
36891991Sheppo }
36901991Sheppo ASSERT(i < nioctls); /* because "operation" already validated */
36911991Sheppo
36925081Sachartre if (!(vd->open_flags & FWRITE) && ioctl[i].write) {
36935081Sachartre PR0("%s fails because backend is opened read-only",
36945081Sachartre ioctl[i].operation_name);
36955081Sachartre request->status = EROFS;
36965081Sachartre return (0);
36975081Sachartre }
36985081Sachartre
36991991Sheppo if (request->nbytes)
37001991Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP);
37011991Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]);
37021991Sheppo if (request->nbytes)
37031991Sheppo kmem_free(buf, request->nbytes);
37044696Sachartre
37051991Sheppo return (status);
37061991Sheppo }
37071991Sheppo
37082531Snarayan static int
vd_get_devid(vd_task_t * task)37092531Snarayan vd_get_devid(vd_task_t *task)
37102531Snarayan {
37112531Snarayan vd_t *vd = task->vd;
37122531Snarayan vd_dring_payload_t *request = task->request;
37132531Snarayan vd_devid_t *vd_devid;
37142531Snarayan impl_devid_t *devid;
37154696Sachartre int status, bufid_len, devid_len, len, sz;
37162793Slm66018 int bufbytes;
37172793Slm66018
37182793Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes);
37192531Snarayan
37206836Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
37216836Sachartre /*
37226836Sachartre * We don't support devid for single-slice disks because we
37236836Sachartre * have no space to store a fabricated devid and for physical
37246836Sachartre * disk slices, we can't use the devid of the disk otherwise
37256836Sachartre * exporting multiple slices from the same disk will produce
37266836Sachartre * the same devids.
37276836Sachartre */
37286836Sachartre PR2("No Device ID for slices");
37296836Sachartre request->status = ENOTSUP;
37306836Sachartre return (0);
37316836Sachartre }
37326836Sachartre
37337597SAlexandre.Chartre@Sun.COM if (VD_DSKIMG(vd)) {
37347597SAlexandre.Chartre@Sun.COM if (vd->dskimg_devid == NULL) {
37354696Sachartre PR2("No Device ID");
37364838Slm66018 request->status = ENOENT;
37374838Slm66018 return (0);
37384696Sachartre } else {
37397597SAlexandre.Chartre@Sun.COM sz = ddi_devid_sizeof(vd->dskimg_devid);
37404696Sachartre devid = kmem_alloc(sz, KM_SLEEP);
37417597SAlexandre.Chartre@Sun.COM bcopy(vd->dskimg_devid, devid, sz);
37424696Sachartre }
37434696Sachartre } else {
37444696Sachartre if (ddi_lyr_get_devid(vd->dev[request->slice],
37454696Sachartre (ddi_devid_t *)&devid) != DDI_SUCCESS) {
37464696Sachartre PR2("No Device ID");
37474838Slm66018 request->status = ENOENT;
37484838Slm66018 return (0);
37494696Sachartre }
37502531Snarayan }
37512531Snarayan
37522531Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1;
37532531Snarayan devid_len = DEVID_GETLEN(devid);
37542531Snarayan
37552793Slm66018 /*
37562793Slm66018 * Save the buffer size here for use in deallocation.
37572793Slm66018 * The actual number of bytes copied is returned in
37582793Slm66018 * the 'nbytes' field of the request structure.
37592793Slm66018 */
37602793Slm66018 bufbytes = request->nbytes;
37612793Slm66018
37622793Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP);
37632531Snarayan vd_devid->length = devid_len;
37642531Snarayan vd_devid->type = DEVID_GETTYPE(devid);
37652531Snarayan
37662531Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len;
37672531Snarayan
37682531Snarayan bcopy(devid->did_id, vd_devid->id, len);
37692531Snarayan
37704963Sachartre request->status = 0;
37714963Sachartre
37722531Snarayan /* LDC memory operations require 8-byte multiples */
37732531Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0);
37742531Snarayan
37752531Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0,
37762531Snarayan &request->nbytes, request->cookie, request->ncookies,
37772531Snarayan LDC_COPY_OUT)) != 0) {
37782793Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client",
37792531Snarayan status);
37802531Snarayan }
37812793Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes);
37822793Slm66018
37832793Slm66018 kmem_free(vd_devid, bufbytes);
37842531Snarayan ddi_devid_free((ddi_devid_t)devid);
37852531Snarayan
37862531Snarayan return (status);
37872531Snarayan }
37882531Snarayan
37895658Sachartre static int
vd_scsi_reset(vd_t * vd)37905658Sachartre vd_scsi_reset(vd_t *vd)
37915658Sachartre {
37925658Sachartre int rval, status;
37935658Sachartre struct uscsi_cmd uscsi = { 0 };
37945658Sachartre
37955658Sachartre uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET;
37965658Sachartre uscsi.uscsi_timeout = vd_scsi_rdwr_timeout;
37975658Sachartre
37985658Sachartre status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi,
37995658Sachartre (vd->open_flags | FKIOCTL), kcred, &rval);
38005658Sachartre
38015658Sachartre return (status);
38025658Sachartre }
38035658Sachartre
38045658Sachartre static int
vd_reset(vd_task_t * task)38055658Sachartre vd_reset(vd_task_t *task)
38065658Sachartre {
38075658Sachartre vd_t *vd = task->vd;
38085658Sachartre vd_dring_payload_t *request = task->request;
38095658Sachartre
38105658Sachartre ASSERT(request->operation == VD_OP_RESET);
38115658Sachartre ASSERT(vd->scsi);
38125658Sachartre
38135658Sachartre PR0("Performing VD_OP_RESET");
38145658Sachartre
38155658Sachartre if (request->nbytes != 0) {
38165658Sachartre PR0("VD_OP_RESET: Expected nbytes = 0, got %lu",
38175658Sachartre request->nbytes);
38185658Sachartre return (EINVAL);
38195658Sachartre }
38205658Sachartre
38215658Sachartre request->status = vd_scsi_reset(vd);
38225658Sachartre
38235658Sachartre return (0);
38245658Sachartre }
38255658Sachartre
38265658Sachartre static int
vd_get_capacity(vd_task_t * task)38275658Sachartre vd_get_capacity(vd_task_t *task)
38285658Sachartre {
38295658Sachartre int rv;
38305658Sachartre size_t nbytes;
38315658Sachartre vd_t *vd = task->vd;
38325658Sachartre vd_dring_payload_t *request = task->request;
38335658Sachartre vd_capacity_t vd_cap = { 0 };
38345658Sachartre
38355658Sachartre ASSERT(request->operation == VD_OP_GET_CAPACITY);
38365658Sachartre
38375658Sachartre PR0("Performing VD_OP_GET_CAPACITY");
38385658Sachartre
38395658Sachartre nbytes = request->nbytes;
38405658Sachartre
38415658Sachartre if (nbytes != RNDSIZE(vd_capacity_t)) {
38425658Sachartre PR0("VD_OP_GET_CAPACITY: Expected nbytes = %lu, got %lu",
38435658Sachartre RNDSIZE(vd_capacity_t), nbytes);
38445658Sachartre return (EINVAL);
38455658Sachartre }
38465658Sachartre
38477540SRamesh.Chitrothu@Sun.COM /*
38487540SRamesh.Chitrothu@Sun.COM * Check the backend size in case it has changed. If the check fails
38497540SRamesh.Chitrothu@Sun.COM * then we will return the last known size.
38507540SRamesh.Chitrothu@Sun.COM */
38517540SRamesh.Chitrothu@Sun.COM
38527540SRamesh.Chitrothu@Sun.COM (void) vd_backend_check_size(vd);
38535658Sachartre ASSERT(vd->vdisk_size != 0);
38545658Sachartre
38555658Sachartre request->status = 0;
38565658Sachartre
38579889SLarry.Liu@Sun.COM vd_cap.vdisk_block_size = vd->vdisk_bsize;
38585658Sachartre vd_cap.vdisk_size = vd->vdisk_size;
38595658Sachartre
38605658Sachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes,
38615658Sachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
38625658Sachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv);
38635658Sachartre return (rv);
38645658Sachartre }
38655658Sachartre
38665658Sachartre return (0);
38675658Sachartre }
38685658Sachartre
38695658Sachartre static int
vd_get_access(vd_task_t * task)38705658Sachartre vd_get_access(vd_task_t *task)
38715658Sachartre {
38725658Sachartre uint64_t access;
38735658Sachartre int rv, rval = 0;
38745658Sachartre size_t nbytes;
38755658Sachartre vd_t *vd = task->vd;
38765658Sachartre vd_dring_payload_t *request = task->request;
38775658Sachartre
38785658Sachartre ASSERT(request->operation == VD_OP_GET_ACCESS);
38795658Sachartre ASSERT(vd->scsi);
38805658Sachartre
38815658Sachartre PR0("Performing VD_OP_GET_ACCESS");
38825658Sachartre
38835658Sachartre nbytes = request->nbytes;
38845658Sachartre
38855658Sachartre if (nbytes != sizeof (uint64_t)) {
38865658Sachartre PR0("VD_OP_GET_ACCESS: Expected nbytes = %lu, got %lu",
38875658Sachartre sizeof (uint64_t), nbytes);
38885658Sachartre return (EINVAL);
38895658Sachartre }
38905658Sachartre
38915658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS,
38925658Sachartre NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
38935658Sachartre
38945658Sachartre if (request->status != 0)
38955658Sachartre return (0);
38965658Sachartre
38975658Sachartre access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED;
38985658Sachartre
38995658Sachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes,
39005658Sachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) {
39015658Sachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv);
39025658Sachartre return (rv);
39035658Sachartre }
39045658Sachartre
39055658Sachartre return (0);
39065658Sachartre }
39075658Sachartre
39085658Sachartre static int
vd_set_access(vd_task_t * task)39095658Sachartre vd_set_access(vd_task_t *task)
39105658Sachartre {
39115658Sachartre uint64_t flags;
39125658Sachartre int rv, rval;
39135658Sachartre size_t nbytes;
39145658Sachartre vd_t *vd = task->vd;
39155658Sachartre vd_dring_payload_t *request = task->request;
39165658Sachartre
39175658Sachartre ASSERT(request->operation == VD_OP_SET_ACCESS);
39185658Sachartre ASSERT(vd->scsi);
39195658Sachartre
39205658Sachartre nbytes = request->nbytes;
39215658Sachartre
39225658Sachartre if (nbytes != sizeof (uint64_t)) {
39235658Sachartre PR0("VD_OP_SET_ACCESS: Expected nbytes = %lu, got %lu",
39245658Sachartre sizeof (uint64_t), nbytes);
39255658Sachartre return (EINVAL);
39265658Sachartre }
39275658Sachartre
39285658Sachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes,
39295658Sachartre request->cookie, request->ncookies, LDC_COPY_IN)) != 0) {
39305658Sachartre PR0("ldc_mem_copy() returned errno %d copying from client", rv);
39315658Sachartre return (rv);
39325658Sachartre }
39335658Sachartre
39345658Sachartre if (flags == VD_ACCESS_SET_CLEAR) {
39355658Sachartre PR0("Performing VD_OP_SET_ACCESS (CLEAR)");
39365658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice],
39375658Sachartre MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred,
39385658Sachartre &rval);
39395658Sachartre if (request->status == 0)
39405658Sachartre vd->ownership = B_FALSE;
39415658Sachartre return (0);
39425658Sachartre }
39435658Sachartre
39445658Sachartre /*
39455658Sachartre * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid
39465658Sachartre * when the EXCLUSIVE flag is set.
39475658Sachartre */
39485658Sachartre if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) {
39495658Sachartre PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags);
39505658Sachartre request->status = EINVAL;
39515658Sachartre return (0);
39525658Sachartre }
39535658Sachartre
39545658Sachartre switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) {
39555658Sachartre
39565658Sachartre case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE:
39575658Sachartre /*
39585658Sachartre * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to
39595658Sachartre * acquire exclusive access rights, preserve them and we
39605658Sachartre * can use preemption. So we can use the MHIOCTKNOWN ioctl.
39615658Sachartre */
39625658Sachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)");
39635658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice],
39645658Sachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
39655658Sachartre break;
39665658Sachartre
39675658Sachartre case VD_ACCESS_SET_PRESERVE:
39685658Sachartre /*
39695658Sachartre * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive
39705658Sachartre * access rights and preserve them, but not preempt any other
39715658Sachartre * host. So we need to use the MHIOCTKOWN ioctl to enable the
39725658Sachartre * "preserve" feature but we can not called it directly
39735658Sachartre * because it uses preemption. So before that, we use the
39745658Sachartre * MHIOCQRESERVE ioctl to ensure we can get exclusive rights
39755658Sachartre * without preempting anyone.
39765658Sachartre */
39775658Sachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)");
39785658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice],
39795658Sachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
39805658Sachartre &rval);
39815658Sachartre if (request->status != 0)
39825658Sachartre break;
39835658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice],
39845658Sachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval);
39855658Sachartre break;
39865658Sachartre
39875658Sachartre case VD_ACCESS_SET_PREEMPT:
39885658Sachartre /*
39895658Sachartre * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive
39905658Sachartre * access rights and we can use preemption. So we try to do
39915658Sachartre * a SCSI reservation, if it fails we reset the disk to clear
39925658Sachartre * any reservation and we try to reserve again.
39935658Sachartre */
39945658Sachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)");
39955658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice],
39965658Sachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
39975658Sachartre &rval);
39985658Sachartre if (request->status == 0)
39995658Sachartre break;
40005658Sachartre
40015658Sachartre /* reset the disk */
40025658Sachartre (void) vd_scsi_reset(vd);
40035658Sachartre
40045658Sachartre /* try again even if the reset has failed */
40055658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice],
40065658Sachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
40075658Sachartre &rval);
40085658Sachartre break;
40095658Sachartre
40105658Sachartre case 0:
40115658Sachartre /* Flag EXCLUSIVE only. Just issue a SCSI reservation */
40125658Sachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)");
40135658Sachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice],
40145658Sachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred,
40155658Sachartre &rval);
40165658Sachartre break;
40175658Sachartre }
40185658Sachartre
40195658Sachartre if (request->status == 0)
40205658Sachartre vd->ownership = B_TRUE;
40215658Sachartre else
40225658Sachartre PR0("VD_OP_SET_ACCESS: error %d", request->status);
40235658Sachartre
40245658Sachartre return (0);
40255658Sachartre }
40265658Sachartre
40275658Sachartre static void
vd_reset_access(vd_t * vd)40285658Sachartre vd_reset_access(vd_t *vd)
40295658Sachartre {
40305658Sachartre int status, rval;
40315658Sachartre
40327597SAlexandre.Chartre@Sun.COM if (vd->file || vd->volume || !vd->ownership)
40335658Sachartre return;
40345658Sachartre
40355658Sachartre PR0("Releasing disk ownership");
40365658Sachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
40375658Sachartre (vd->open_flags | FKIOCTL), kcred, &rval);
40385658Sachartre
40395658Sachartre /*
40405658Sachartre * An EACCES failure means that there is a reservation conflict,
40415658Sachartre * so we are not the owner of the disk anymore.
40425658Sachartre */
40435658Sachartre if (status == 0 || status == EACCES) {
40445658Sachartre vd->ownership = B_FALSE;
40455658Sachartre return;
40465658Sachartre }
40475658Sachartre
40485658Sachartre PR0("Fail to release ownership, error %d", status);
40495658Sachartre
40505658Sachartre /*
40515658Sachartre * We have failed to release the ownership, try to reset the disk
40525658Sachartre * to release reservations.
40535658Sachartre */
40545658Sachartre PR0("Resetting disk");
40555658Sachartre status = vd_scsi_reset(vd);
40565658Sachartre
40575658Sachartre if (status != 0)
40585658Sachartre PR0("Fail to reset disk, error %d", status);
40595658Sachartre
40605658Sachartre /* whatever the result of the reset is, we try the release again */
40615658Sachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL,
40625658Sachartre (vd->open_flags | FKIOCTL), kcred, &rval);
40635658Sachartre
40645658Sachartre if (status == 0 || status == EACCES) {
40655658Sachartre vd->ownership = B_FALSE;
40665658Sachartre return;
40675658Sachartre }
40685658Sachartre
40695658Sachartre PR0("Fail to release ownership, error %d", status);
40705658Sachartre
40715658Sachartre /*
40725658Sachartre * At this point we have done our best to try to reset the
40735658Sachartre * access rights to the disk and we don't know if we still
40745658Sachartre * own a reservation and if any mechanism to preserve the
40755658Sachartre * ownership is still in place. The ultimate solution would
40765658Sachartre * be to reset the system but this is usually not what we
40775658Sachartre * want to happen.
40785658Sachartre */
40795658Sachartre
40805658Sachartre if (vd_reset_access_failure == A_REBOOT) {
40815658Sachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG
40825658Sachartre ", rebooting the system", vd->device_path);
40835658Sachartre (void) uadmin(A_SHUTDOWN, AD_BOOT, NULL);
40845658Sachartre } else if (vd_reset_access_failure == A_DUMP) {
40855658Sachartre panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
40865658Sachartre }
40875658Sachartre
40885658Sachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path);
40895658Sachartre }
40905658Sachartre
40911991Sheppo /*
40921991Sheppo * Define the supported operations once the functions for performing them have
40931991Sheppo * been defined
40941991Sheppo */
40951991Sheppo static const vds_operation_t vds_operation[] = {
40962793Slm66018 #define X(_s) #_s, _s
40972793Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio},
40982793Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio},
40992793Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL},
41002793Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL},
41012793Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL},
41022793Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL},
41032793Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL},
41042793Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL},
41052793Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL},
41062793Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL},
41072793Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL},
41082793Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL},
41095658Sachartre {X(VD_OP_SCSICMD), vd_ioctl, NULL},
41105658Sachartre {X(VD_OP_RESET), vd_reset, NULL},
41115658Sachartre {X(VD_OP_GET_CAPACITY), vd_get_capacity, NULL},
41125658Sachartre {X(VD_OP_SET_ACCESS), vd_set_access, NULL},
41135658Sachartre {X(VD_OP_GET_ACCESS), vd_get_access, NULL},
41142793Slm66018 #undef X
41151991Sheppo };
41161991Sheppo
41171991Sheppo static const size_t vds_noperations =
41181991Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0]));
41191991Sheppo
41201991Sheppo /*
41212336Snarayan * Process a task specifying a client I/O request
41224838Slm66018 *
41234838Slm66018 * Parameters:
41244838Slm66018 * task - structure containing the request sent from client
41254838Slm66018 *
41264838Slm66018 * Return Value
41274838Slm66018 * 0 - success
41284838Slm66018 * ENOTSUP - Unknown/Unsupported VD_OP_XXX operation
41294838Slm66018 * EINVAL - Invalid disk slice
41304838Slm66018 * != 0 - some other non-zero return value from start function
41311991Sheppo */
41321991Sheppo static int
vd_do_process_task(vd_task_t * task)41334838Slm66018 vd_do_process_task(vd_task_t *task)
41341991Sheppo {
41354838Slm66018 int i;
41362336Snarayan vd_t *vd = task->vd;
41372336Snarayan vd_dring_payload_t *request = task->request;
41382336Snarayan
41392336Snarayan ASSERT(vd != NULL);
41402336Snarayan ASSERT(request != NULL);
41411991Sheppo
41422336Snarayan /* Find the requested operation */
41434838Slm66018 for (i = 0; i < vds_noperations; i++) {
41444838Slm66018 if (request->operation == vds_operation[i].operation) {
41454838Slm66018 /* all operations should have a start func */
41464838Slm66018 ASSERT(vds_operation[i].start != NULL);
41474838Slm66018
41484838Slm66018 task->completef = vds_operation[i].complete;
41492336Snarayan break;
41504838Slm66018 }
41514838Slm66018 }
41525365Slm66018
41535365Slm66018 /*
41545365Slm66018 * We need to check that the requested operation is permitted
41555365Slm66018 * for the particular client that sent it or that the loop above
41565365Slm66018 * did not complete without finding the operation type (indicating
41575365Slm66018 * that the requested operation is unknown/unimplemented)
41585365Slm66018 */
41595365Slm66018 if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) ||
41605365Slm66018 (i == vds_noperations)) {
41612793Slm66018 PR0("Unsupported operation %u", request->operation);
41625365Slm66018 request->status = ENOTSUP;
41635365Slm66018 return (0);
41642336Snarayan }
41652336Snarayan
41662748Slm66018 /* Range-check slice */
41674696Sachartre if (request->slice >= vd->nslices &&
41686836Sachartre ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) ||
41694696Sachartre request->slice != VD_SLICE_NONE)) {
41702793Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk",
41712748Slm66018 request->slice, (vd->nslices - 1));
41726836Sachartre request->status = EINVAL;
41736836Sachartre return (0);
41742748Slm66018 }
41752748Slm66018
41764838Slm66018 /*
41774838Slm66018 * Call the function pointer that starts the operation.
41784838Slm66018 */
41794838Slm66018 return (vds_operation[i].start(task));
41804838Slm66018 }
41814838Slm66018
41824838Slm66018 /*
41834838Slm66018 * Description:
41844838Slm66018 * This function is called by both the in-band and descriptor ring
41854838Slm66018 * message processing functions paths to actually execute the task
41864838Slm66018 * requested by the vDisk client. It in turn calls its worker
41874838Slm66018 * function, vd_do_process_task(), to carry our the request.
41884838Slm66018 *
41894838Slm66018 * Any transport errors (e.g. LDC errors, vDisk protocol errors) are
41904838Slm66018 * saved in the 'status' field of the task and are propagated back
41914838Slm66018 * up the call stack to trigger a NACK
41924838Slm66018 *
41934838Slm66018 * Any request errors (e.g. ENOTTY from an ioctl) are saved in
41944838Slm66018 * the 'status' field of the request and result in an ACK being sent
41954838Slm66018 * by the completion handler.
41964838Slm66018 *
41974838Slm66018 * Parameters:
41984838Slm66018 * task - structure containing the request sent from client
41994838Slm66018 *
42004838Slm66018 * Return Value
42014838Slm66018 * 0 - successful synchronous request.
42024838Slm66018 * != 0 - transport error (e.g. LDC errors, vDisk protocol)
42034838Slm66018 * EINPROGRESS - task will be finished in a completion handler
42044838Slm66018 */
42054838Slm66018 static int
vd_process_task(vd_task_t * task)42064838Slm66018 vd_process_task(vd_task_t *task)
42074838Slm66018 {
42084838Slm66018 vd_t *vd = task->vd;
42094838Slm66018 int status;
42104838Slm66018
42114838Slm66018 DTRACE_PROBE1(task__start, vd_task_t *, task);
42124838Slm66018
42134838Slm66018 task->status = vd_do_process_task(task);
42144838Slm66018
42154838Slm66018 /*
42164838Slm66018 * If the task processing function returned EINPROGRESS indicating
42174838Slm66018 * that the task needs completing then schedule a taskq entry to
42184838Slm66018 * finish it now.
42194838Slm66018 *
42204838Slm66018 * Otherwise the task processing function returned either zero
42214838Slm66018 * indicating that the task was finished in the start function (and we
42224838Slm66018 * don't need to wait in a completion function) or the start function
42234838Slm66018 * returned an error - in both cases all that needs to happen is the
42244838Slm66018 * notification to the vDisk client higher up the call stack.
42254838Slm66018 * If the task was using a Descriptor Ring, we need to mark it as done
42264838Slm66018 * at this stage.
42274838Slm66018 */
42284838Slm66018 if (task->status == EINPROGRESS) {
42294838Slm66018 /* Queue a task to complete the operation */
42304838Slm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_complete,
42314838Slm66018 task, DDI_SLEEP);
42327791SAlexandre.Chartre@Sun.COM return (EINPROGRESS);
42337791SAlexandre.Chartre@Sun.COM }
42347791SAlexandre.Chartre@Sun.COM
42357791SAlexandre.Chartre@Sun.COM if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) {
42364838Slm66018 /* Update the dring element if it's a dring client */
42374838Slm66018 status = vd_mark_elem_done(vd, task->index,
42384838Slm66018 task->request->status, task->request->nbytes);
42394838Slm66018 if (status == ECONNRESET)
42404838Slm66018 vd_mark_in_reset(vd);
42416845Sha137994 else if (status == EACCES)
42426845Sha137994 vd_need_reset(vd, B_TRUE);
42431991Sheppo }
42441991Sheppo
42454838Slm66018 return (task->status);
42461991Sheppo }
42471991Sheppo
42481991Sheppo /*
42492032Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first
42502032Slm66018 * argument match the corresponding remaining arguments; otherwise, return false
42511991Sheppo */
42522032Slm66018 boolean_t
vd_msgtype(vio_msg_tag_t * tag,int type,int subtype,int env)42531991Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env)
42541991Sheppo {
42551991Sheppo return ((tag->vio_msgtype == type) &&
42564696Sachartre (tag->vio_subtype == subtype) &&
42574696Sachartre (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE;
42581991Sheppo }
42591991Sheppo
42602032Slm66018 /*
42612032Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported
42622032Slm66018 * by this server.
42632032Slm66018 */
42642032Slm66018 static boolean_t
vds_supported_version(vio_ver_msg_t * ver_msg)42652032Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg)
42662032Slm66018 {
42672032Slm66018 for (int i = 0; i < vds_num_versions; i++) {
42682032Slm66018 ASSERT(vds_version[i].major > 0);
42692032Slm66018 ASSERT((i == 0) ||
42702032Slm66018 (vds_version[i].major < vds_version[i-1].major));
42712032Slm66018
42722032Slm66018 /*
42732032Slm66018 * If the major versions match, adjust the minor version, if
42742032Slm66018 * necessary, down to the highest value supported by this
42752032Slm66018 * server and return true so this message will get "ack"ed;
42762032Slm66018 * the client should also support all minor versions lower
42772032Slm66018 * than the value it sent
42782032Slm66018 */
42792032Slm66018 if (ver_msg->ver_major == vds_version[i].major) {
42802032Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) {
42812032Slm66018 PR0("Adjusting minor version from %u to %u",
42822032Slm66018 ver_msg->ver_minor, vds_version[i].minor);
42832032Slm66018 ver_msg->ver_minor = vds_version[i].minor;
42842032Slm66018 }
42852032Slm66018 return (B_TRUE);
42862032Slm66018 }
42872032Slm66018
42882032Slm66018 /*
42892032Slm66018 * If the message contains a higher major version number, set
42902032Slm66018 * the message's major/minor versions to the current values
42912032Slm66018 * and return false, so this message will get "nack"ed with
42922032Slm66018 * these values, and the client will potentially try again
42932032Slm66018 * with the same or a lower version
42942032Slm66018 */
42952032Slm66018 if (ver_msg->ver_major > vds_version[i].major) {
42962032Slm66018 ver_msg->ver_major = vds_version[i].major;
42972032Slm66018 ver_msg->ver_minor = vds_version[i].minor;
42982032Slm66018 return (B_FALSE);
42992032Slm66018 }
43002032Slm66018
43012032Slm66018 /*
43022032Slm66018 * Otherwise, the message's major version is less than the
43032032Slm66018 * current major version, so continue the loop to the next
43042032Slm66018 * (lower) supported version
43052032Slm66018 */
43062032Slm66018 }
43072032Slm66018
43082032Slm66018 /*
43092032Slm66018 * No common version was found; "ground" the version pair in the
43102032Slm66018 * message to terminate negotiation
43112032Slm66018 */
43122032Slm66018 ver_msg->ver_major = 0;
43132032Slm66018 ver_msg->ver_minor = 0;
43142032Slm66018 return (B_FALSE);
43152032Slm66018 }
43162032Slm66018
43172032Slm66018 /*
43182032Slm66018 * Process a version message from a client. vds expects to receive version
43192032Slm66018 * messages from clients seeking service, but never issues version messages
43202032Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does
43212032Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such
43222032Slm66018 * messages as invalid).
43232032Slm66018 */
43241991Sheppo static int
vd_process_ver_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)43252032Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
43261991Sheppo {
43271991Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg;
43281991Sheppo
43291991Sheppo
43301991Sheppo ASSERT(msglen >= sizeof (msg->tag));
43311991Sheppo
43321991Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
43334696Sachartre VIO_VER_INFO)) {
43341991Sheppo return (ENOMSG); /* not a version message */
43351991Sheppo }
43361991Sheppo
43371991Sheppo if (msglen != sizeof (*ver_msg)) {
43382793Slm66018 PR0("Expected %lu-byte version message; "
43391991Sheppo "received %lu bytes", sizeof (*ver_msg), msglen);
43401991Sheppo return (EBADMSG);
43411991Sheppo }
43421991Sheppo
43431991Sheppo if (ver_msg->dev_class != VDEV_DISK) {
43442793Slm66018 PR0("Expected device class %u (disk); received %u",
43451991Sheppo VDEV_DISK, ver_msg->dev_class);
43461991Sheppo return (EBADMSG);
43471991Sheppo }
43481991Sheppo
43492032Slm66018 /*
43502032Slm66018 * We're talking to the expected kind of client; set our device class
43512032Slm66018 * for "ack/nack" back to the client
43522032Slm66018 */
43532032Slm66018 ver_msg->dev_class = VDEV_DISK_SERVER;
43542032Slm66018
43552032Slm66018 /*
43562032Slm66018 * Check whether the (valid) version message specifies a version
43572032Slm66018 * supported by this server. If the version is not supported, return
43582032Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version()
43592032Slm66018 * will have updated the message with a supported version for the
43602032Slm66018 * client to consider
43612032Slm66018 */
43622032Slm66018 if (!vds_supported_version(ver_msg))
43631991Sheppo return (EBADMSG);
43642032Slm66018
43652032Slm66018
43662032Slm66018 /*
43672032Slm66018 * A version has been agreed upon; use the client's SID for
43682032Slm66018 * communication on this channel now
43692032Slm66018 */
43702032Slm66018 ASSERT(!(vd->initialized & VD_SID));
43712032Slm66018 vd->sid = ver_msg->tag.vio_sid;
43722032Slm66018 vd->initialized |= VD_SID;
43731991Sheppo
43742032Slm66018 /*
43755365Slm66018 * Store the negotiated major and minor version values in the "vd" data
43765365Slm66018 * structure so that we can check if certain operations are supported
43775365Slm66018 * by the client.
43782032Slm66018 */
43795365Slm66018 vd->version.major = ver_msg->ver_major;
43805365Slm66018 vd->version.minor = ver_msg->ver_minor;
43812032Slm66018
43822032Slm66018 PR0("Using major version %u, minor version %u",
43832032Slm66018 ver_msg->ver_major, ver_msg->ver_minor);
43841991Sheppo return (0);
43851991Sheppo }
43861991Sheppo
43875365Slm66018 static void
vd_set_exported_operations(vd_t * vd)43885365Slm66018 vd_set_exported_operations(vd_t *vd)
43895365Slm66018 {
43905365Slm66018 vd->operations = 0; /* clear field */
43915365Slm66018
43925365Slm66018 /*
43935365Slm66018 * We need to check from the highest version supported to the
43945365Slm66018 * lowest because versions with a higher minor number implicitly
43955365Slm66018 * support versions with a lower minor number.
43965365Slm66018 */
43975365Slm66018 if (vio_ver_is_supported(vd->version, 1, 1)) {
43985365Slm66018 ASSERT(vd->open_flags & FREAD);
43997540SRamesh.Chitrothu@Sun.COM vd->operations |= VD_OP_MASK_READ | (1 << VD_OP_GET_CAPACITY);
44005365Slm66018
44015365Slm66018 if (vd->open_flags & FWRITE)
44025365Slm66018 vd->operations |= VD_OP_MASK_WRITE;
44035365Slm66018
44045658Sachartre if (vd->scsi)
44055658Sachartre vd->operations |= VD_OP_MASK_SCSI;
44065658Sachartre
44077597SAlexandre.Chartre@Sun.COM if (VD_DSKIMG(vd) && vd_dskimg_is_iso_image(vd)) {
44085365Slm66018 /*
44095365Slm66018 * can't write to ISO images, make sure that write
44105365Slm66018 * support is not set in case administrator did not
44115365Slm66018 * use "options=ro" when doing an ldm add-vdsdev
44125365Slm66018 */
44135365Slm66018 vd->operations &= ~VD_OP_MASK_WRITE;
44145365Slm66018 }
44155365Slm66018 } else if (vio_ver_is_supported(vd->version, 1, 0)) {
44165365Slm66018 vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE;
44175365Slm66018 }
44185365Slm66018
44195365Slm66018 /* we should have already agreed on a version */
44205365Slm66018 ASSERT(vd->operations != 0);
44215365Slm66018 }
44225365Slm66018
44231991Sheppo static int
vd_process_attr_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)44241991Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
44251991Sheppo {
44261991Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg;
44273401Snarayan int status, retry = 0;
44281991Sheppo
44291991Sheppo
44301991Sheppo ASSERT(msglen >= sizeof (msg->tag));
44311991Sheppo
44321991Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
44334696Sachartre VIO_ATTR_INFO)) {
44342336Snarayan PR0("Message is not an attribute message");
44352336Snarayan return (ENOMSG);
44361991Sheppo }
44371991Sheppo
44381991Sheppo if (msglen != sizeof (*attr_msg)) {
44392793Slm66018 PR0("Expected %lu-byte attribute message; "
44401991Sheppo "received %lu bytes", sizeof (*attr_msg), msglen);
44411991Sheppo return (EBADMSG);
44421991Sheppo }
44431991Sheppo
44441991Sheppo if (attr_msg->max_xfer_sz == 0) {
44452793Slm66018 PR0("Received maximum transfer size of 0 from client");
44461991Sheppo return (EBADMSG);
44471991Sheppo }
44481991Sheppo
44491991Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) &&
44505935Ssb155480 (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) {
44512793Slm66018 PR0("Client requested unsupported transfer mode");
44521991Sheppo return (EBADMSG);
44531991Sheppo }
44541991Sheppo
44553401Snarayan /*
44563401Snarayan * check if the underlying disk is ready, if not try accessing
44573401Snarayan * the device again. Open the vdisk device and extract info
44583401Snarayan * about it, as this is needed to respond to the attr info msg
44593401Snarayan */
44603401Snarayan if ((vd->initialized & VD_DISK_READY) == 0) {
44613401Snarayan PR0("Retry setting up disk (%s)", vd->device_path);
44623401Snarayan do {
44633401Snarayan status = vd_setup_vd(vd);
44643401Snarayan if (status != EAGAIN || ++retry > vds_dev_retries)
44653401Snarayan break;
44663401Snarayan
44673401Snarayan /* incremental delay */
44683401Snarayan delay(drv_usectohz(vds_dev_delay));
44693401Snarayan
44703401Snarayan /* if vdisk is no longer enabled - return error */
44713401Snarayan if (!vd_enabled(vd))
44723401Snarayan return (ENXIO);
44733401Snarayan
44743401Snarayan } while (status == EAGAIN);
44753401Snarayan
44763401Snarayan if (status)
44773401Snarayan return (ENXIO);
44783401Snarayan
44793401Snarayan vd->initialized |= VD_DISK_READY;
44803401Snarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
44816108Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
44823401Snarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
44836108Sachartre (vd->volume ? "yes" : "no"),
44843401Snarayan (vd->file ? "yes" : "no"),
44853401Snarayan vd->nslices);
44863401Snarayan }
44873401Snarayan
44881991Sheppo /* Success: valid message and transfer mode */
44891991Sheppo vd->xfer_mode = attr_msg->xfer_mode;
44902793Slm66018
44911991Sheppo if (vd->xfer_mode == VIO_DESC_MODE) {
44922793Slm66018
44931991Sheppo /*
44941991Sheppo * The vd_dring_inband_msg_t contains one cookie; need room
44951991Sheppo * for up to n-1 more cookies, where "n" is the number of full
44961991Sheppo * pages plus possibly one partial page required to cover
44971991Sheppo * "max_xfer_sz". Add room for one more cookie if
44981991Sheppo * "max_xfer_sz" isn't an integral multiple of the page size.
44991991Sheppo * Must first get the maximum transfer size in bytes.
45001991Sheppo */
45011991Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ?
45029889SLarry.Liu@Sun.COM attr_msg->vdisk_block_size * attr_msg->max_xfer_sz :
45031991Sheppo attr_msg->max_xfer_sz;
45041991Sheppo size_t max_inband_msglen =
45051991Sheppo sizeof (vd_dring_inband_msg_t) +
45061991Sheppo ((max_xfer_bytes/PAGESIZE +
45074696Sachartre ((max_xfer_bytes % PAGESIZE) ? 1 : 0))*
45084696Sachartre (sizeof (ldc_mem_cookie_t)));
45091991Sheppo
45101991Sheppo /*
45111991Sheppo * Set the maximum expected message length to
45121991Sheppo * accommodate in-band-descriptor messages with all
45131991Sheppo * their cookies
45141991Sheppo */
45151991Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen);
45162336Snarayan
45172336Snarayan /*
45182336Snarayan * Initialize the data structure for processing in-band I/O
45192336Snarayan * request descriptors
45202336Snarayan */
45212336Snarayan vd->inband_task.vd = vd;
45222793Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
45232336Snarayan vd->inband_task.index = 0;
45242336Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */
45251991Sheppo }
45261991Sheppo
45272410Slm66018 /* Return the device's block size and max transfer size to the client */
45289889SLarry.Liu@Sun.COM attr_msg->vdisk_block_size = vd->vdisk_bsize;
45292410Slm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz;
45302410Slm66018
45311991Sheppo attr_msg->vdisk_size = vd->vdisk_size;
45326836Sachartre attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type :
45336836Sachartre VD_DISK_TYPE_DISK;
45345365Slm66018 attr_msg->vdisk_media = vd->vdisk_media;
45355365Slm66018
45365365Slm66018 /* Discover and save the list of supported VD_OP_XXX operations */
45375365Slm66018 vd_set_exported_operations(vd);
45385365Slm66018 attr_msg->operations = vd->operations;
45395365Slm66018
45401991Sheppo PR0("%s", VD_CLIENT(vd));
45412793Slm66018
45422793Slm66018 ASSERT(vd->dring_task == NULL);
45432793Slm66018
45441991Sheppo return (0);
45451991Sheppo }
45461991Sheppo
45471991Sheppo static int
vd_process_dring_reg_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)45481991Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
45491991Sheppo {
45501991Sheppo int status;
45511991Sheppo size_t expected;
45521991Sheppo ldc_mem_info_t dring_minfo;
45536845Sha137994 uint8_t mtype;
45541991Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg;
45551991Sheppo
45561991Sheppo
45571991Sheppo ASSERT(msglen >= sizeof (msg->tag));
45581991Sheppo
45591991Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
45604696Sachartre VIO_DRING_REG)) {
45612336Snarayan PR0("Message is not a register-dring message");
45622336Snarayan return (ENOMSG);
45631991Sheppo }
45641991Sheppo
45651991Sheppo if (msglen < sizeof (*reg_msg)) {
45662793Slm66018 PR0("Expected at least %lu-byte register-dring message; "
45671991Sheppo "received %lu bytes", sizeof (*reg_msg), msglen);
45681991Sheppo return (EBADMSG);
45691991Sheppo }
45701991Sheppo
45711991Sheppo expected = sizeof (*reg_msg) +
45721991Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0]));
45731991Sheppo if (msglen != expected) {
45742793Slm66018 PR0("Expected %lu-byte register-dring message; "
45751991Sheppo "received %lu bytes", expected, msglen);
45761991Sheppo return (EBADMSG);
45771991Sheppo }
45781991Sheppo
45791991Sheppo if (vd->initialized & VD_DRING) {
45802793Slm66018 PR0("A dring was previously registered; only support one");
45811991Sheppo return (EBADMSG);
45821991Sheppo }
45831991Sheppo
45842336Snarayan if (reg_msg->num_descriptors > INT32_MAX) {
45852793Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)",
45862336Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX));
45872336Snarayan return (EBADMSG);
45882336Snarayan }
45892336Snarayan
45901991Sheppo if (reg_msg->ncookies != 1) {
45911991Sheppo /*
45921991Sheppo * In addition to fixing the assertion in the success case
45931991Sheppo * below, supporting drings which require more than one
45941991Sheppo * "cookie" requires increasing the value of vd->max_msglen
45951991Sheppo * somewhere in the code path prior to receiving the message
45961991Sheppo * which results in calling this function. Note that without
45971991Sheppo * making this change, the larger message size required to
45981991Sheppo * accommodate multiple cookies cannot be successfully
45991991Sheppo * received, so this function will not even get called.
46001991Sheppo * Gracefully accommodating more dring cookies might
46011991Sheppo * reasonably demand exchanging an additional attribute or
46021991Sheppo * making a minor protocol adjustment
46031991Sheppo */
46042793Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies);
46051991Sheppo return (EBADMSG);
46061991Sheppo }
46071991Sheppo
46086845Sha137994 if (vd_direct_mapped_drings)
46096845Sha137994 mtype = LDC_DIRECT_MAP;
46106845Sha137994 else
46116845Sha137994 mtype = LDC_SHADOW_MAP;
46126845Sha137994
46131991Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie,
46141991Sheppo reg_msg->ncookies, reg_msg->num_descriptors,
46156845Sha137994 reg_msg->descriptor_size, mtype, &vd->dring_handle);
46161991Sheppo if (status != 0) {
46172793Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status);
46181991Sheppo return (status);
46191991Sheppo }
46201991Sheppo
46211991Sheppo /*
46221991Sheppo * To remove the need for this assertion, must call
46231991Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a
46241991Sheppo * successful call to ldc_mem_dring_map()
46251991Sheppo */
46261991Sheppo ASSERT(reg_msg->ncookies == 1);
46271991Sheppo
46281991Sheppo if ((status =
46294696Sachartre ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) {
46302793Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status);
46311991Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)
46322793Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status);
46331991Sheppo return (status);
46341991Sheppo }
46351991Sheppo
46361991Sheppo if (dring_minfo.vaddr == NULL) {
46372793Slm66018 PR0("Descriptor ring virtual address is NULL");
46382032Slm66018 return (ENXIO);
46391991Sheppo }
46401991Sheppo
46411991Sheppo
46422336Snarayan /* Initialize for valid message and mapped dring */
46431991Sheppo vd->initialized |= VD_DRING;
46441991Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */
46451991Sheppo vd->dring = dring_minfo.vaddr;
46461991Sheppo vd->descriptor_size = reg_msg->descriptor_size;
46471991Sheppo vd->dring_len = reg_msg->num_descriptors;
46486845Sha137994 vd->dring_mtype = dring_minfo.mtype;
46491991Sheppo reg_msg->dring_ident = vd->dring_ident;
46507226Sha137994 PR1("descriptor size = %u, dring length = %u",
46517226Sha137994 vd->descriptor_size, vd->dring_len);
46522336Snarayan
46532336Snarayan /*
46542336Snarayan * Allocate and initialize a "shadow" array of data structures for
46552336Snarayan * tasks to process I/O requests in dring elements
46562336Snarayan */
46572336Snarayan vd->dring_task =
46582336Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP);
46592336Snarayan for (int i = 0; i < vd->dring_len; i++) {
46602336Snarayan vd->dring_task[i].vd = vd;
46612336Snarayan vd->dring_task[i].index = i;
46622531Snarayan
46632531Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle,
46642531Snarayan &(vd->dring_task[i].mhdl));
46652531Snarayan if (status) {
46662793Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status);
46672531Snarayan return (ENXIO);
46682531Snarayan }
46692793Slm66018
46707226Sha137994 /*
46717226Sha137994 * The descriptor payload varies in length. Calculate its
46727226Sha137994 * size by subtracting the header size from the total
46737226Sha137994 * descriptor size.
46747226Sha137994 */
46757226Sha137994 vd->dring_task[i].request = kmem_zalloc((vd->descriptor_size -
46767226Sha137994 sizeof (vio_dring_entry_hdr_t)), KM_SLEEP);
46772793Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP);
46782336Snarayan }
46792336Snarayan
46807791SAlexandre.Chartre@Sun.COM if (vd->file || vd->zvol) {
46817791SAlexandre.Chartre@Sun.COM vd->write_queue =
46827791SAlexandre.Chartre@Sun.COM kmem_zalloc(sizeof (buf_t *) * vd->dring_len, KM_SLEEP);
46837791SAlexandre.Chartre@Sun.COM }
46847791SAlexandre.Chartre@Sun.COM
46851991Sheppo return (0);
46861991Sheppo }
46871991Sheppo
46881991Sheppo static int
vd_process_dring_unreg_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)46891991Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
46901991Sheppo {
46911991Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg;
46921991Sheppo
46931991Sheppo
46941991Sheppo ASSERT(msglen >= sizeof (msg->tag));
46951991Sheppo
46961991Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO,
46974696Sachartre VIO_DRING_UNREG)) {
46982336Snarayan PR0("Message is not an unregister-dring message");
46992336Snarayan return (ENOMSG);
47001991Sheppo }
47011991Sheppo
47021991Sheppo if (msglen != sizeof (*unreg_msg)) {
47032793Slm66018 PR0("Expected %lu-byte unregister-dring message; "
47041991Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen);
47051991Sheppo return (EBADMSG);
47061991Sheppo }
47071991Sheppo
47081991Sheppo if (unreg_msg->dring_ident != vd->dring_ident) {
47092793Slm66018 PR0("Expected dring ident %lu; received %lu",
47101991Sheppo vd->dring_ident, unreg_msg->dring_ident);
47111991Sheppo return (EBADMSG);
47121991Sheppo }
47131991Sheppo
47141991Sheppo return (0);
47151991Sheppo }
47161991Sheppo
47171991Sheppo static int
process_rdx_msg(vio_msg_t * msg,size_t msglen)47181991Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen)
47191991Sheppo {
47201991Sheppo ASSERT(msglen >= sizeof (msg->tag));
47211991Sheppo
47222336Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) {
47232336Snarayan PR0("Message is not an RDX message");
47242336Snarayan return (ENOMSG);
47252336Snarayan }
47261991Sheppo
47271991Sheppo if (msglen != sizeof (vio_rdx_msg_t)) {
47282793Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes",
47291991Sheppo sizeof (vio_rdx_msg_t), msglen);
47301991Sheppo return (EBADMSG);
47311991Sheppo }
47321991Sheppo
47332336Snarayan PR0("Valid RDX message");
47341991Sheppo return (0);
47351991Sheppo }
47361991Sheppo
47371991Sheppo static int
vd_check_seq_num(vd_t * vd,uint64_t seq_num)47381991Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num)
47391991Sheppo {
47401991Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) {
47412793Slm66018 PR0("Received seq_num %lu; expected %lu",
47421991Sheppo seq_num, (vd->seq_num + 1));
47432793Slm66018 PR0("initiating soft reset");
47442336Snarayan vd_need_reset(vd, B_FALSE);
47451991Sheppo return (1);
47461991Sheppo }
47471991Sheppo
47481991Sheppo vd->seq_num = seq_num;
47491991Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */
47501991Sheppo return (0);
47511991Sheppo }
47521991Sheppo
47531991Sheppo /*
47541991Sheppo * Return the expected size of an inband-descriptor message with all the
47551991Sheppo * cookies it claims to include
47561991Sheppo */
47571991Sheppo static size_t
expected_inband_size(vd_dring_inband_msg_t * msg)47581991Sheppo expected_inband_size(vd_dring_inband_msg_t *msg)
47591991Sheppo {
47601991Sheppo return ((sizeof (*msg)) +
47611991Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0])));
47621991Sheppo }
47631991Sheppo
47641991Sheppo /*
47651991Sheppo * Process an in-band descriptor message: used with clients like OBP, with
47661991Sheppo * which vds exchanges descriptors within VIO message payloads, rather than
47671991Sheppo * operating on them within a descriptor ring
47681991Sheppo */
47691991Sheppo static int
vd_process_desc_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)47702793Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
47711991Sheppo {
47721991Sheppo size_t expected;
47731991Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg;
47741991Sheppo
47751991Sheppo
47761991Sheppo ASSERT(msglen >= sizeof (msg->tag));
47771991Sheppo
47781991Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
47794696Sachartre VIO_DESC_DATA)) {
47802336Snarayan PR1("Message is not an in-band-descriptor message");
47812336Snarayan return (ENOMSG);
47822336Snarayan }
47831991Sheppo
47841991Sheppo if (msglen < sizeof (*desc_msg)) {
47852793Slm66018 PR0("Expected at least %lu-byte descriptor message; "
47861991Sheppo "received %lu bytes", sizeof (*desc_msg), msglen);
47871991Sheppo return (EBADMSG);
47881991Sheppo }
47891991Sheppo
47901991Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) {
47912793Slm66018 PR0("Expected %lu-byte descriptor message; "
47921991Sheppo "received %lu bytes", expected, msglen);
47931991Sheppo return (EBADMSG);
47941991Sheppo }
47951991Sheppo
47962336Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0)
47971991Sheppo return (EBADMSG);
47982336Snarayan
47992336Snarayan /*
48002336Snarayan * Valid message: Set up the in-band descriptor task and process the
48012336Snarayan * request. Arrange to acknowledge the client's message, unless an
48022336Snarayan * error processing the descriptor task results in setting
48032336Snarayan * VIO_SUBTYPE_NACK
48042336Snarayan */
48052336Snarayan PR1("Valid in-band-descriptor message");
48062336Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
48072793Slm66018
48082793Slm66018 ASSERT(vd->inband_task.msg != NULL);
48092793Slm66018
48102793Slm66018 bcopy(msg, vd->inband_task.msg, msglen);
48112336Snarayan vd->inband_task.msglen = msglen;
48122793Slm66018
48132793Slm66018 /*
48142793Slm66018 * The task request is now the payload of the message
48152793Slm66018 * that was just copied into the body of the task.
48162793Slm66018 */
48172793Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg;
48182336Snarayan vd->inband_task.request = &desc_msg->payload;
48192793Slm66018
48202336Snarayan return (vd_process_task(&vd->inband_task));
48211991Sheppo }
48221991Sheppo
48231991Sheppo static int
vd_process_element(vd_t * vd,vd_task_type_t type,uint32_t idx,vio_msg_t * msg,size_t msglen)48242336Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx,
48252793Slm66018 vio_msg_t *msg, size_t msglen)
48261991Sheppo {
48272336Snarayan int status;
48282336Snarayan boolean_t ready;
48296845Sha137994 on_trap_data_t otd;
48302336Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx);
48312336Snarayan
48322336Snarayan /* Accept the updated dring element */
48336845Sha137994 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype,
48346845Sha137994 vd->dring_handle, idx, idx)) != 0) {
48351991Sheppo return (status);
48361991Sheppo }
48372336Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY);
48382336Snarayan if (ready) {
48392336Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED;
48407226Sha137994 bcopy(&elem->payload, vd->dring_task[idx].request,
48417226Sha137994 (vd->descriptor_size - sizeof (vio_dring_entry_hdr_t)));
48422336Snarayan } else {
48432793Slm66018 PR0("descriptor %u not ready", idx);
48442336Snarayan VD_DUMP_DRING_ELEM(elem);
48452336Snarayan }
48466845Sha137994 if ((status = VIO_DRING_RELEASE(vd->dring_mtype,
48476845Sha137994 vd->dring_handle, idx, idx)) != 0) {
48486845Sha137994 PR0("VIO_DRING_RELEASE() returned errno %d", status);
48491991Sheppo return (status);
48501991Sheppo }
48512336Snarayan if (!ready)
48522336Snarayan return (EBUSY);
48532336Snarayan
48542336Snarayan
48552336Snarayan /* Initialize a task and process the accepted element */
48562336Snarayan PR1("Processing dring element %u", idx);
48572336Snarayan vd->dring_task[idx].type = type;
48582793Slm66018
48592793Slm66018 /* duplicate msg buf for cookies etc. */
48602793Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen);
48612793Slm66018
48622336Snarayan vd->dring_task[idx].msglen = msglen;
48634838Slm66018 return (vd_process_task(&vd->dring_task[idx]));
48641991Sheppo }
48651991Sheppo
48661991Sheppo static int
vd_process_element_range(vd_t * vd,int start,int end,vio_msg_t * msg,size_t msglen)48672336Snarayan vd_process_element_range(vd_t *vd, int start, int end,
48682793Slm66018 vio_msg_t *msg, size_t msglen)
48692336Snarayan {
48702336Snarayan int i, n, nelem, status = 0;
48712336Snarayan boolean_t inprogress = B_FALSE;
48722336Snarayan vd_task_type_t type;
48732336Snarayan
48742336Snarayan
48752336Snarayan ASSERT(start >= 0);
48762336Snarayan ASSERT(end >= 0);
48772336Snarayan
48782336Snarayan /*
48792336Snarayan * Arrange to acknowledge the client's message, unless an error
48802336Snarayan * processing one of the dring elements results in setting
48812336Snarayan * VIO_SUBTYPE_NACK
48822336Snarayan */
48832336Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
48842336Snarayan
48852336Snarayan /*
48862336Snarayan * Process the dring elements in the range
48872336Snarayan */
48882336Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1;
48892336Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) {
48902336Snarayan ((vio_dring_msg_t *)msg)->end_idx = i;
48912336Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK;
48922793Slm66018 status = vd_process_element(vd, type, i, msg, msglen);
48932336Snarayan if (status == EINPROGRESS)
48942336Snarayan inprogress = B_TRUE;
48952336Snarayan else if (status != 0)
48962336Snarayan break;
48972336Snarayan }
48982336Snarayan
48992336Snarayan /*
49002336Snarayan * If some, but not all, operations of a multi-element range are in
49012336Snarayan * progress, wait for other operations to complete before returning
49022336Snarayan * (which will result in "ack" or "nack" of the message). Note that
49032336Snarayan * all outstanding operations will need to complete, not just the ones
49042336Snarayan * corresponding to the current range of dring elements; howevever, as
49052336Snarayan * this situation is an error case, performance is less critical.
49062336Snarayan */
49077791SAlexandre.Chartre@Sun.COM if ((nelem > 1) && (status != EINPROGRESS) && inprogress) {
49087791SAlexandre.Chartre@Sun.COM if (vd->ioq != NULL)
49097791SAlexandre.Chartre@Sun.COM ddi_taskq_wait(vd->ioq);
49102336Snarayan ddi_taskq_wait(vd->completionq);
49117791SAlexandre.Chartre@Sun.COM }
49122336Snarayan
49132336Snarayan return (status);
49142336Snarayan }
49152336Snarayan
49162336Snarayan static int
vd_process_dring_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)49172793Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
49181991Sheppo {
49191991Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg;
49201991Sheppo
49211991Sheppo
49221991Sheppo ASSERT(msglen >= sizeof (msg->tag));
49231991Sheppo
49241991Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO,
49254696Sachartre VIO_DRING_DATA)) {
49262336Snarayan PR1("Message is not a dring-data message");
49272336Snarayan return (ENOMSG);
49281991Sheppo }
49291991Sheppo
49301991Sheppo if (msglen != sizeof (*dring_msg)) {
49312793Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes",
49321991Sheppo sizeof (*dring_msg), msglen);
49331991Sheppo return (EBADMSG);
49341991Sheppo }
49351991Sheppo
49362336Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0)
49371991Sheppo return (EBADMSG);
49381991Sheppo
49391991Sheppo if (dring_msg->dring_ident != vd->dring_ident) {
49402793Slm66018 PR0("Expected dring ident %lu; received ident %lu",
49411991Sheppo vd->dring_ident, dring_msg->dring_ident);
49421991Sheppo return (EBADMSG);
49431991Sheppo }
49441991Sheppo
49452336Snarayan if (dring_msg->start_idx >= vd->dring_len) {
49462793Slm66018 PR0("\"start_idx\" = %u; must be less than %u",
49472336Snarayan dring_msg->start_idx, vd->dring_len);
49482336Snarayan return (EBADMSG);
49492336Snarayan }
49502336Snarayan
49512336Snarayan if ((dring_msg->end_idx < 0) ||
49522336Snarayan (dring_msg->end_idx >= vd->dring_len)) {
49532793Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u",
49542336Snarayan dring_msg->end_idx, vd->dring_len);
49552336Snarayan return (EBADMSG);
49562336Snarayan }
49572336Snarayan
49582336Snarayan /* Valid message; process range of updated dring elements */
49592336Snarayan PR1("Processing descriptor range, start = %u, end = %u",
49602336Snarayan dring_msg->start_idx, dring_msg->end_idx);
49612336Snarayan return (vd_process_element_range(vd, dring_msg->start_idx,
49624696Sachartre dring_msg->end_idx, msg, msglen));
49631991Sheppo }
49641991Sheppo
49651991Sheppo static int
recv_msg(ldc_handle_t ldc_handle,void * msg,size_t * nbytes)49661991Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes)
49671991Sheppo {
49681991Sheppo int retry, status;
49691991Sheppo size_t size = *nbytes;
49701991Sheppo
49711991Sheppo
49721991Sheppo for (retry = 0, status = ETIMEDOUT;
49731991Sheppo retry < vds_ldc_retries && status == ETIMEDOUT;
49741991Sheppo retry++) {
49751991Sheppo PR1("ldc_read() attempt %d", (retry + 1));
49761991Sheppo *nbytes = size;
49771991Sheppo status = ldc_read(ldc_handle, msg, nbytes);
49781991Sheppo }
49791991Sheppo
49802793Slm66018 if (status) {
49812793Slm66018 PR0("ldc_read() returned errno %d", status);
49822793Slm66018 if (status != ECONNRESET)
49832793Slm66018 return (ENOMSG);
49841991Sheppo return (status);
49851991Sheppo } else if (*nbytes == 0) {
49861991Sheppo PR1("ldc_read() returned 0 and no message read");
49871991Sheppo return (ENOMSG);
49881991Sheppo }
49891991Sheppo
49901991Sheppo PR1("RCVD %lu-byte message", *nbytes);
49911991Sheppo return (0);
49921991Sheppo }
49931991Sheppo
49941991Sheppo static int
vd_do_process_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)49952793Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
49961991Sheppo {
49971991Sheppo int status;
49981991Sheppo
49991991Sheppo
50001991Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype,
50011991Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env);
50022793Slm66018 #ifdef DEBUG
50032793Slm66018 vd_decode_tag(msg);
50042793Slm66018 #endif
50051991Sheppo
50061991Sheppo /*
50071991Sheppo * Validate session ID up front, since it applies to all messages
50081991Sheppo * once set
50091991Sheppo */
50101991Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) {
50112793Slm66018 PR0("Expected SID %u, received %u", vd->sid,
50121991Sheppo msg->tag.vio_sid);
50131991Sheppo return (EBADMSG);
50141991Sheppo }
50151991Sheppo
50162793Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state));
50171991Sheppo
50181991Sheppo /*
50191991Sheppo * Process the received message based on connection state
50201991Sheppo */
50211991Sheppo switch (vd->state) {
50221991Sheppo case VD_STATE_INIT: /* expect version message */
50232032Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0)
50241991Sheppo return (status);
50251991Sheppo
50261991Sheppo /* Version negotiated, move to that state */
50271991Sheppo vd->state = VD_STATE_VER;
50281991Sheppo return (0);
50291991Sheppo
50301991Sheppo case VD_STATE_VER: /* expect attribute message */
50311991Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0)
50321991Sheppo return (status);
50331991Sheppo
50341991Sheppo /* Attributes exchanged, move to that state */
50351991Sheppo vd->state = VD_STATE_ATTR;
50361991Sheppo return (0);
50371991Sheppo
50381991Sheppo case VD_STATE_ATTR:
50391991Sheppo switch (vd->xfer_mode) {
50401991Sheppo case VIO_DESC_MODE: /* expect RDX message */
50411991Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0)
50421991Sheppo return (status);
50431991Sheppo
50441991Sheppo /* Ready to receive in-band descriptors */
50451991Sheppo vd->state = VD_STATE_DATA;
50461991Sheppo return (0);
50471991Sheppo
50485935Ssb155480 case VIO_DRING_MODE_V1_0: /* expect register-dring message */
50491991Sheppo if ((status =
50504696Sachartre vd_process_dring_reg_msg(vd, msg, msglen)) != 0)
50511991Sheppo return (status);
50521991Sheppo
50531991Sheppo /* One dring negotiated, move to that state */
50541991Sheppo vd->state = VD_STATE_DRING;
50551991Sheppo return (0);
50561991Sheppo
50571991Sheppo default:
50581991Sheppo ASSERT("Unsupported transfer mode");
50592793Slm66018 PR0("Unsupported transfer mode");
50601991Sheppo return (ENOTSUP);
50611991Sheppo }
50621991Sheppo
50631991Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */
50641991Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) {
50651991Sheppo /* Ready to receive data */
50661991Sheppo vd->state = VD_STATE_DATA;
50671991Sheppo return (0);
50681991Sheppo } else if (status != ENOMSG) {
50691991Sheppo return (status);
50701991Sheppo }
50711991Sheppo
50721991Sheppo
50731991Sheppo /*
50741991Sheppo * If another register-dring message is received, stay in
50751991Sheppo * dring state in case the client sends RDX; although the
50761991Sheppo * protocol allows multiple drings, this server does not
50771991Sheppo * support using more than one
50781991Sheppo */
50791991Sheppo if ((status =
50804696Sachartre vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG)
50811991Sheppo return (status);
50821991Sheppo
50831991Sheppo /*
50841991Sheppo * Acknowledge an unregister-dring message, but reset the
50851991Sheppo * connection anyway: Although the protocol allows
50861991Sheppo * unregistering drings, this server cannot serve a vdisk
50871991Sheppo * without its only dring
50881991Sheppo */
50891991Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen);
50901991Sheppo return ((status == 0) ? ENOTSUP : status);
50911991Sheppo
50921991Sheppo case VD_STATE_DATA:
50931991Sheppo switch (vd->xfer_mode) {
50941991Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */
50952793Slm66018 return (vd_process_desc_msg(vd, msg, msglen));
50961991Sheppo
50975935Ssb155480 case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */
50981991Sheppo /*
50991991Sheppo * Typically expect dring-data messages, so handle
51001991Sheppo * them first
51011991Sheppo */
51021991Sheppo if ((status = vd_process_dring_msg(vd, msg,
51034696Sachartre msglen)) != ENOMSG)
51041991Sheppo return (status);
51051991Sheppo
51061991Sheppo /*
51071991Sheppo * Acknowledge an unregister-dring message, but reset
51081991Sheppo * the connection anyway: Although the protocol
51091991Sheppo * allows unregistering drings, this server cannot
51101991Sheppo * serve a vdisk without its only dring
51111991Sheppo */
51121991Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen);
51131991Sheppo return ((status == 0) ? ENOTSUP : status);
51141991Sheppo
51151991Sheppo default:
51161991Sheppo ASSERT("Unsupported transfer mode");
51172793Slm66018 PR0("Unsupported transfer mode");
51181991Sheppo return (ENOTSUP);
51191991Sheppo }
51201991Sheppo
51211991Sheppo default:
51221991Sheppo ASSERT("Invalid client connection state");
51232793Slm66018 PR0("Invalid client connection state");
51241991Sheppo return (ENOTSUP);
51251991Sheppo }
51261991Sheppo }
51271991Sheppo
51282336Snarayan static int
vd_process_msg(vd_t * vd,vio_msg_t * msg,size_t msglen)51292793Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen)
51301991Sheppo {
51311991Sheppo int status;
51321991Sheppo boolean_t reset_ldc = B_FALSE;
51334838Slm66018 vd_task_t task;
51341991Sheppo
51351991Sheppo /*
51361991Sheppo * Check that the message is at least big enough for a "tag", so that
51371991Sheppo * message processing can proceed based on tag-specified message type
51381991Sheppo */
51391991Sheppo if (msglen < sizeof (vio_msg_tag_t)) {
51402793Slm66018 PR0("Received short (%lu-byte) message", msglen);
51411991Sheppo /* Can't "nack" short message, so drop the big hammer */
51422793Slm66018 PR0("initiating full reset");
51432336Snarayan vd_need_reset(vd, B_TRUE);
51442336Snarayan return (EBADMSG);
51451991Sheppo }
51461991Sheppo
51471991Sheppo /*
51481991Sheppo * Process the message
51491991Sheppo */
51502793Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) {
51511991Sheppo case 0:
51521991Sheppo /* "ack" valid, successfully-processed messages */
51531991Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK;
51541991Sheppo break;
51551991Sheppo
51562336Snarayan case EINPROGRESS:
51572336Snarayan /* The completion handler will "ack" or "nack" the message */
51582336Snarayan return (EINPROGRESS);
51591991Sheppo case ENOMSG:
51602793Slm66018 PR0("Received unexpected message");
51611991Sheppo _NOTE(FALLTHROUGH);
51621991Sheppo case EBADMSG:
51631991Sheppo case ENOTSUP:
51644838Slm66018 /* "transport" error will cause NACK of invalid messages */
51651991Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
51661991Sheppo break;
51671991Sheppo
51681991Sheppo default:
51694838Slm66018 /* "transport" error will cause NACK of invalid messages */
51701991Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK;
51711991Sheppo /* An LDC error probably occurred, so try resetting it */
51721991Sheppo reset_ldc = B_TRUE;
51731991Sheppo break;
51741991Sheppo }
51751991Sheppo
51762793Slm66018 PR1("\tResulting in state %d (%s)", vd->state,
51774696Sachartre vd_decode_state(vd->state));
51782793Slm66018
51794838Slm66018 /* populate the task so we can dispatch it on the taskq */
51804838Slm66018 task.vd = vd;
51814838Slm66018 task.msg = msg;
51824838Slm66018 task.msglen = msglen;
51834838Slm66018
51844838Slm66018 /*
51854838Slm66018 * Queue a task to send the notification that the operation completed.
51864838Slm66018 * We need to ensure that requests are responded to in the correct
51874838Slm66018 * order and since the taskq is processed serially this ordering
51884838Slm66018 * is maintained.
51894838Slm66018 */
51904838Slm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify,
51914838Slm66018 &task, DDI_SLEEP);
51924838Slm66018
51934838Slm66018 /*
51944838Slm66018 * To ensure handshake negotiations do not happen out of order, such
51954838Slm66018 * requests that come through this path should not be done in parallel
51964838Slm66018 * so we need to wait here until the response is sent to the client.
51974838Slm66018 */
51984838Slm66018 ddi_taskq_wait(vd->completionq);
51991991Sheppo
52002336Snarayan /* Arrange to reset the connection for nack'ed or failed messages */
52012793Slm66018 if ((status != 0) || reset_ldc) {
52022793Slm66018 PR0("initiating %s reset",
52032793Slm66018 (reset_ldc) ? "full" : "soft");
52042336Snarayan vd_need_reset(vd, reset_ldc);
52052793Slm66018 }
52062336Snarayan
52072336Snarayan return (status);
52082336Snarayan }
52092336Snarayan
52102336Snarayan static boolean_t
vd_enabled(vd_t * vd)52112336Snarayan vd_enabled(vd_t *vd)
52122336Snarayan {
52132336Snarayan boolean_t enabled;
52142336Snarayan
52152336Snarayan mutex_enter(&vd->lock);
52162336Snarayan enabled = vd->enabled;
52172336Snarayan mutex_exit(&vd->lock);
52182336Snarayan return (enabled);
52191991Sheppo }
52201991Sheppo
52211991Sheppo static void
vd_recv_msg(void * arg)52222032Slm66018 vd_recv_msg(void *arg)
52231991Sheppo {
52242032Slm66018 vd_t *vd = (vd_t *)arg;
52252793Slm66018 int rv = 0, status = 0;
52261991Sheppo
52271991Sheppo ASSERT(vd != NULL);
52282793Slm66018
52292336Snarayan PR2("New task to receive incoming message(s)");
52302793Slm66018
52312793Slm66018
52322336Snarayan while (vd_enabled(vd) && status == 0) {
52332336Snarayan size_t msglen, msgsize;
52342793Slm66018 ldc_status_t lstatus;
52352336Snarayan
52362336Snarayan /*
52372336Snarayan * Receive and process a message
52382336Snarayan */
52392336Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */
52402793Slm66018
52412793Slm66018 /*
52422793Slm66018 * check if channel is UP - else break out of loop
52432793Slm66018 */
52442793Slm66018 status = ldc_status(vd->ldc_handle, &lstatus);
52452793Slm66018 if (lstatus != LDC_UP) {
52462793Slm66018 PR0("channel not up (status=%d), exiting recv loop\n",
52472793Slm66018 lstatus);
52482793Slm66018 break;
52492793Slm66018 }
52502793Slm66018
52512793Slm66018 ASSERT(vd->max_msglen != 0);
52522793Slm66018
52532793Slm66018 msgsize = vd->max_msglen; /* stable copy for alloc/free */
52542793Slm66018 msglen = msgsize; /* actual len after recv_msg() */
52552793Slm66018
52562793Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen);
52572793Slm66018 switch (status) {
52582793Slm66018 case 0:
52597563SPrasad.Singamsetty@Sun.COM rv = vd_process_msg(vd, (void *)vd->vio_msgp, msglen);
52602793Slm66018 /* check if max_msglen changed */
52612793Slm66018 if (msgsize != vd->max_msglen) {
52622793Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n",
52632793Slm66018 msgsize, vd->max_msglen);
52642793Slm66018 kmem_free(vd->vio_msgp, msgsize);
52652793Slm66018 vd->vio_msgp =
52664696Sachartre kmem_alloc(vd->max_msglen, KM_SLEEP);
52672793Slm66018 }
52682793Slm66018 if (rv == EINPROGRESS)
52692793Slm66018 continue;
52702793Slm66018 break;
52712793Slm66018
52722793Slm66018 case ENOMSG:
52732793Slm66018 break;
52742793Slm66018
52752793Slm66018 case ECONNRESET:
52762793Slm66018 PR0("initiating soft reset (ECONNRESET)\n");
52772793Slm66018 vd_need_reset(vd, B_FALSE);
52782793Slm66018 status = 0;
52792793Slm66018 break;
52802793Slm66018
52812793Slm66018 default:
52822336Snarayan /* Probably an LDC failure; arrange to reset it */
52832793Slm66018 PR0("initiating full reset (status=0x%x)", status);
52842336Snarayan vd_need_reset(vd, B_TRUE);
52852793Slm66018 break;
52862336Snarayan }
52872032Slm66018 }
52882793Slm66018
52892336Snarayan PR2("Task finished");
52902032Slm66018 }
52912032Slm66018
52922032Slm66018 static uint_t
vd_handle_ldc_events(uint64_t event,caddr_t arg)52931991Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg)
52941991Sheppo {
52951991Sheppo vd_t *vd = (vd_t *)(void *)arg;
52962793Slm66018 int status;
52971991Sheppo
52981991Sheppo ASSERT(vd != NULL);
52992336Snarayan
53002336Snarayan if (!vd_enabled(vd))
53012336Snarayan return (LDC_SUCCESS);
53022336Snarayan
53032793Slm66018 if (event & LDC_EVT_DOWN) {
53043166Ssg70180 PR0("LDC_EVT_DOWN: LDC channel went down");
53052793Slm66018
53062793Slm66018 vd_need_reset(vd, B_TRUE);
53072793Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
53082793Slm66018 DDI_SLEEP);
53092793Slm66018 if (status == DDI_FAILURE) {
53102793Slm66018 PR0("cannot schedule task to recv msg\n");
53112793Slm66018 vd_need_reset(vd, B_TRUE);
53122793Slm66018 }
53132793Slm66018 }
53142793Slm66018
53152336Snarayan if (event & LDC_EVT_RESET) {
53162793Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset");
53172793Slm66018
53182793Slm66018 if (vd->state != VD_STATE_INIT) {
53192793Slm66018 PR0("scheduling full reset");
53202793Slm66018 vd_need_reset(vd, B_FALSE);
53212793Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
53222793Slm66018 vd, DDI_SLEEP);
53232793Slm66018 if (status == DDI_FAILURE) {
53242793Slm66018 PR0("cannot schedule task to recv msg\n");
53252793Slm66018 vd_need_reset(vd, B_TRUE);
53262793Slm66018 }
53272793Slm66018
53282793Slm66018 } else {
53292793Slm66018 PR0("channel already reset, ignoring...\n");
53302793Slm66018 PR0("doing ldc up...\n");
53312793Slm66018 (void) ldc_up(vd->ldc_handle);
53322793Slm66018 }
53332793Slm66018
53342336Snarayan return (LDC_SUCCESS);
53352336Snarayan }
53362336Snarayan
53372336Snarayan if (event & LDC_EVT_UP) {
53382793Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state");
53392793Slm66018 PR0("initiating soft reset");
53402336Snarayan vd_need_reset(vd, B_FALSE);
53412793Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg,
53422793Slm66018 vd, DDI_SLEEP);
53432793Slm66018 if (status == DDI_FAILURE) {
53442793Slm66018 PR0("cannot schedule task to recv msg\n");
53452793Slm66018 vd_need_reset(vd, B_TRUE);
53462793Slm66018 return (LDC_SUCCESS);
53472793Slm66018 }
53482336Snarayan }
53492336Snarayan
53502336Snarayan if (event & LDC_EVT_READ) {
53512336Snarayan int status;
53522336Snarayan
53532336Snarayan PR1("New data available");
53542336Snarayan /* Queue a task to receive the new data */
53552336Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd,
53562336Snarayan DDI_SLEEP);
53572793Slm66018
53582793Slm66018 if (status == DDI_FAILURE) {
53592793Slm66018 PR0("cannot schedule task to recv msg\n");
53602793Slm66018 vd_need_reset(vd, B_TRUE);
53612793Slm66018 }
53622336Snarayan }
53632336Snarayan
53642336Snarayan return (LDC_SUCCESS);
53651991Sheppo }
53661991Sheppo
53671991Sheppo static uint_t
vds_check_for_vd(mod_hash_key_t key,mod_hash_val_t * val,void * arg)53681991Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg)
53691991Sheppo {
53701991Sheppo _NOTE(ARGUNUSED(key, val))
53711991Sheppo (*((uint_t *)arg))++;
53721991Sheppo return (MH_WALK_TERMINATE);
53731991Sheppo }
53741991Sheppo
53751991Sheppo
53761991Sheppo static int
vds_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)53771991Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
53781991Sheppo {
53791991Sheppo uint_t vd_present = 0;
53801991Sheppo minor_t instance;
53811991Sheppo vds_t *vds;
53821991Sheppo
53831991Sheppo
53841991Sheppo switch (cmd) {
53851991Sheppo case DDI_DETACH:
53861991Sheppo /* the real work happens below */
53871991Sheppo break;
53881991Sheppo case DDI_SUSPEND:
53892336Snarayan PR0("No action required for DDI_SUSPEND");
53901991Sheppo return (DDI_SUCCESS);
53911991Sheppo default:
53922793Slm66018 PR0("Unrecognized \"cmd\"");
53931991Sheppo return (DDI_FAILURE);
53941991Sheppo }
53951991Sheppo
53961991Sheppo ASSERT(cmd == DDI_DETACH);
53971991Sheppo instance = ddi_get_instance(dip);
53981991Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
53992793Slm66018 PR0("Could not get state for instance %u", instance);
54001991Sheppo ddi_soft_state_free(vds_state, instance);
54011991Sheppo return (DDI_FAILURE);
54021991Sheppo }
54031991Sheppo
54041991Sheppo /* Do no detach when serving any vdisks */
54051991Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present);
54061991Sheppo if (vd_present) {
54071991Sheppo PR0("Not detaching because serving vdisks");
54081991Sheppo return (DDI_FAILURE);
54091991Sheppo }
54101991Sheppo
54111991Sheppo PR0("Detaching");
54123297Ssb155480 if (vds->initialized & VDS_MDEG) {
54131991Sheppo (void) mdeg_unregister(vds->mdeg);
54143297Ssb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template));
54153297Ssb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t));
54163297Ssb155480 vds->ispecp = NULL;
54173297Ssb155480 vds->mdeg = NULL;
54183297Ssb155480 }
54193297Ssb155480
54206108Sachartre vds_driver_types_free(vds);
54216108Sachartre
54221991Sheppo if (vds->initialized & VDS_LDI)
54231991Sheppo (void) ldi_ident_release(vds->ldi_ident);
54241991Sheppo mod_hash_destroy_hash(vds->vd_table);
54251991Sheppo ddi_soft_state_free(vds_state, instance);
54261991Sheppo return (DDI_SUCCESS);
54271991Sheppo }
54281991Sheppo
54295365Slm66018 /*
54305365Slm66018 * Description:
54317597SAlexandre.Chartre@Sun.COM * This function checks to see if the disk image being used as a
54327597SAlexandre.Chartre@Sun.COM * virtual disk is an ISO image. An ISO image is a special case
54337597SAlexandre.Chartre@Sun.COM * which can be booted/installed from like a CD/DVD.
54345365Slm66018 *
54355365Slm66018 * Parameters:
54365365Slm66018 * vd - disk on which the operation is performed.
54375365Slm66018 *
54385365Slm66018 * Return Code:
54397597SAlexandre.Chartre@Sun.COM * B_TRUE - The disk image is an ISO 9660 compliant image
54407597SAlexandre.Chartre@Sun.COM * B_FALSE - just a regular disk image
54415365Slm66018 */
54425365Slm66018 static boolean_t
vd_dskimg_is_iso_image(vd_t * vd)54437597SAlexandre.Chartre@Sun.COM vd_dskimg_is_iso_image(vd_t *vd)
54445365Slm66018 {
54455365Slm66018 char iso_buf[ISO_SECTOR_SIZE];
54465365Slm66018 int i, rv;
54475365Slm66018 uint_t sec;
54485365Slm66018
54497597SAlexandre.Chartre@Sun.COM ASSERT(VD_DSKIMG(vd));
54505365Slm66018
54515365Slm66018 /*
54525365Slm66018 * If we have already discovered and saved this info we can
54537597SAlexandre.Chartre@Sun.COM * short-circuit the check and avoid reading the disk image.
54545365Slm66018 */
54555365Slm66018 if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD)
54565365Slm66018 return (B_TRUE);
54575365Slm66018
54585365Slm66018 /*
54595365Slm66018 * We wish to read the sector that should contain the 2nd ISO volume
54605365Slm66018 * descriptor. The second field in this descriptor is called the
54615365Slm66018 * Standard Identifier and is set to CD001 for a CD-ROM compliant
54625365Slm66018 * to the ISO 9660 standard.
54635365Slm66018 */
54649889SLarry.Liu@Sun.COM sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_bsize;
54657597SAlexandre.Chartre@Sun.COM rv = vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf,
54665365Slm66018 sec, ISO_SECTOR_SIZE);
54675365Slm66018
54685365Slm66018 if (rv < 0)
54695365Slm66018 return (B_FALSE);
54705365Slm66018
54715365Slm66018 for (i = 0; i < ISO_ID_STRLEN; i++) {
54725365Slm66018 if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i])
54735365Slm66018 return (B_FALSE);
54745365Slm66018 }
54755365Slm66018
54765365Slm66018 return (B_TRUE);
54775365Slm66018 }
54785365Slm66018
54795365Slm66018 /*
54805365Slm66018 * Description:
54815365Slm66018 * This function checks to see if the virtual device is an ATAPI
54825365Slm66018 * device. ATAPI devices use Group 1 Read/Write commands, so
54835365Slm66018 * any USCSI calls vds makes need to take this into account.
54845365Slm66018 *
54855365Slm66018 * Parameters:
54865365Slm66018 * vd - disk on which the operation is performed.
54875365Slm66018 *
54885365Slm66018 * Return Code:
54895365Slm66018 * B_TRUE - The virtual disk is backed by an ATAPI device
54905365Slm66018 * B_FALSE - not an ATAPI device (presumably SCSI)
54915365Slm66018 */
54925365Slm66018 static boolean_t
vd_is_atapi_device(vd_t * vd)54935365Slm66018 vd_is_atapi_device(vd_t *vd)
54945365Slm66018 {
54955365Slm66018 boolean_t is_atapi = B_FALSE;
54965365Slm66018 char *variantp;
54975365Slm66018 int rv;
54985365Slm66018
54995365Slm66018 ASSERT(vd->ldi_handle[0] != NULL);
55005365Slm66018 ASSERT(!vd->file);
55015365Slm66018
55025365Slm66018 rv = ldi_prop_lookup_string(vd->ldi_handle[0],
55035365Slm66018 (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp);
55045365Slm66018 if (rv == DDI_PROP_SUCCESS) {
55055365Slm66018 PR0("'variant' property exists for %s", vd->device_path);
55065365Slm66018 if (strcmp(variantp, "atapi") == 0)
55075365Slm66018 is_atapi = B_TRUE;
55085365Slm66018 ddi_prop_free(variantp);
55095365Slm66018 }
55105365Slm66018
55115365Slm66018 rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi");
55125365Slm66018 if (rv) {
55135365Slm66018 PR0("'atapi' property exists for %s", vd->device_path);
55145365Slm66018 is_atapi = B_TRUE;
55155365Slm66018 }
55165365Slm66018
55175365Slm66018 return (is_atapi);
55185365Slm66018 }
55195365Slm66018
55201991Sheppo static int
vd_setup_full_disk(vd_t * vd)55212032Slm66018 vd_setup_full_disk(vd_t *vd)
55222032Slm66018 {
55235658Sachartre int status;
55242032Slm66018 major_t major = getmajor(vd->dev[0]);
55252032Slm66018 minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE;
55262531Snarayan
55275081Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
55285081Sachartre
55297540SRamesh.Chitrothu@Sun.COM /* set the disk size, block size and the media type of the disk */
55307540SRamesh.Chitrothu@Sun.COM status = vd_backend_check_size(vd);
55315658Sachartre
55325658Sachartre if (status != 0) {
55335658Sachartre if (!vd->scsi) {
55345658Sachartre /* unexpected failure */
553510063SAlexandre.Chartre@Sun.COM PRN("Check size failed for %s (errno %d)",
553610063SAlexandre.Chartre@Sun.COM vd->device_path, status);
553710063SAlexandre.Chartre@Sun.COM return (EIO);
55385658Sachartre }
55395658Sachartre
55405658Sachartre /*
55415658Sachartre * The function can fail for SCSI disks which are present but
55425658Sachartre * reserved by another system. In that case, we don't know the
55435658Sachartre * size of the disk and the block size.
55445658Sachartre */
55455658Sachartre vd->vdisk_size = VD_SIZE_UNKNOWN;
55469889SLarry.Liu@Sun.COM vd->vdisk_bsize = 0;
55479889SLarry.Liu@Sun.COM vd->backend_bsize = 0;
55485658Sachartre vd->vdisk_media = VD_MEDIA_FIXED;
55495658Sachartre }
55502032Slm66018
55512032Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */
55522032Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0];
55532032Slm66018 vd->dev[0] = 0;
55542032Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0];
55552032Slm66018 vd->ldi_handle[0] = NULL;
55562032Slm66018
55572032Slm66018 /* Initialize device numbers for remaining slices and open them */
55582032Slm66018 for (int slice = 0; slice < vd->nslices; slice++) {
55592032Slm66018 /*
55602032Slm66018 * Skip the entire-disk slice, as it's already open and its
55612032Slm66018 * device known
55622032Slm66018 */
55632032Slm66018 if (slice == VD_ENTIRE_DISK_SLICE)
55642032Slm66018 continue;
55652032Slm66018 ASSERT(vd->dev[slice] == 0);
55662032Slm66018 ASSERT(vd->ldi_handle[slice] == NULL);
55672032Slm66018
55682032Slm66018 /*
55692032Slm66018 * Construct the device number for the current slice
55702032Slm66018 */
55712032Slm66018 vd->dev[slice] = makedevice(major, (minor + slice));
55722032Slm66018
55732032Slm66018 /*
55743166Ssg70180 * Open all slices of the disk to serve them to the client.
55753166Ssg70180 * Slices are opened exclusively to prevent other threads or
55763166Ssg70180 * processes in the service domain from performing I/O to
55773166Ssg70180 * slices being accessed by a client. Failure to open a slice
55783166Ssg70180 * results in vds not serving this disk, as the client could
55793166Ssg70180 * attempt (and should be able) to access any slice immediately.
55803166Ssg70180 * Any slices successfully opened before a failure will get
55813166Ssg70180 * closed by vds_destroy_vd() as a result of the error returned
55823166Ssg70180 * by this function.
55833166Ssg70180 *
55843166Ssg70180 * We need to do the open with FNDELAY so that opening an empty
55853166Ssg70180 * slice does not fail.
55862032Slm66018 */
55872032Slm66018 PR0("Opening device major %u, minor %u = slice %u",
55882032Slm66018 major, minor, slice);
55895081Sachartre
55905081Sachartre /*
55915081Sachartre * Try to open the device. This can fail for example if we are
55925081Sachartre * opening an empty slice. So in case of a failure, we try the
55935081Sachartre * open again but this time with the FNDELAY flag.
55945081Sachartre */
55955081Sachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
55965081Sachartre vd->open_flags, kcred, &vd->ldi_handle[slice],
55975081Sachartre vd->vds->ldi_ident);
55985081Sachartre
55995081Sachartre if (status != 0) {
56005081Sachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK,
56015081Sachartre vd->open_flags | FNDELAY, kcred,
56025081Sachartre &vd->ldi_handle[slice], vd->vds->ldi_ident);
56035081Sachartre }
56045081Sachartre
56055081Sachartre if (status != 0) {
56063782Sachartre PRN("ldi_open_by_dev() returned errno %d "
56072032Slm66018 "for slice %u", status, slice);
56082032Slm66018 /* vds_destroy_vd() will close any open slices */
56093782Sachartre vd->ldi_handle[slice] = NULL;
56102032Slm66018 return (status);
56112032Slm66018 }
56122032Slm66018 }
56132032Slm66018
56142032Slm66018 return (0);
56152032Slm66018 }
56162032Slm66018
56175874Sachartre /*
56185874Sachartre * When a slice or a volume is exported as a single-slice disk, we want
56195874Sachartre * the disk backend (i.e. the slice or volume) to be entirely mapped as
56205874Sachartre * a slice without the addition of any metadata.
56215874Sachartre *
56225874Sachartre * So when exporting the disk as a VTOC disk, we fake a disk with the following
56235874Sachartre * layout:
56246836Sachartre * flabel +--- flabel_limit
56256836Sachartre * <-> V
56266836Sachartre * 0 1 C D E
56276836Sachartre * +-+---+--------------------------+--+
56286836Sachartre * virtual disk: |L|XXX| slice 0 |AA|
56296836Sachartre * +-+---+--------------------------+--+
56306836Sachartre * ^ : :
56316836Sachartre * | : :
56326836Sachartre * VTOC LABEL--+ : :
56336836Sachartre * +--------------------------+
56346836Sachartre * disk backend: | slice/volume/file |
56356836Sachartre * +--------------------------+
56366836Sachartre * 0 N
56375874Sachartre *
56386836Sachartre * N is the number of blocks in the slice/volume/file.
56396836Sachartre *
56406836Sachartre * We simulate a disk with N+M blocks, where M is the number of blocks
56416836Sachartre * simluated at the beginning and at the end of the disk (blocks 0-C
56426836Sachartre * and D-E).
56435874Sachartre *
56446836Sachartre * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C
56456836Sachartre * to D defines slice 0 and are mapped to the backend. Finally we emulate 2
56466836Sachartre * alternate cylinders at the end of the disk (blocks D-E). In summary we have:
56475874Sachartre *
56486836Sachartre * - block 0 (L) returns a fake VTOC label
56496836Sachartre * - blocks 1 to C-1 (X) are unused and return 0
56506836Sachartre * - blocks C to D-1 are mapped to the exported slice or volume
56516836Sachartre * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders)
56525874Sachartre *
56536836Sachartre * Note: because we define a fake disk geometry, it is possible that the length
56546836Sachartre * of the backend is not a multiple of the size of cylinder, in that case the
56556836Sachartre * very end of the backend will not map to any block of the virtual disk.
56565874Sachartre */
56572032Slm66018 static int
vd_setup_partition_vtoc(vd_t * vd)56584963Sachartre vd_setup_partition_vtoc(vd_t *vd)
56594963Sachartre {
56604963Sachartre char *device_path = vd->device_path;
56616836Sachartre char unit;
56626836Sachartre size_t size, csize;
56634963Sachartre
56644963Sachartre /* Initialize dk_geom structure for single-slice device */
56654963Sachartre if (vd->dk_geom.dkg_nsect == 0) {
56664963Sachartre PRN("%s geometry claims 0 sectors per track", device_path);
56674963Sachartre return (EIO);
56684963Sachartre }
56694963Sachartre if (vd->dk_geom.dkg_nhead == 0) {
56704963Sachartre PRN("%s geometry claims 0 heads", device_path);
56714963Sachartre return (EIO);
56724963Sachartre }
56736836Sachartre
56746836Sachartre /* size of a cylinder in block */
56756836Sachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect;
56766836Sachartre
56776836Sachartre /*
56786836Sachartre * Add extra cylinders: we emulate the first cylinder (which contains
56796836Sachartre * the disk label).
56806836Sachartre */
56816836Sachartre vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1;
56826836Sachartre
56836836Sachartre /* we emulate 2 alternate cylinders */
56846836Sachartre vd->dk_geom.dkg_acyl = 2;
56854963Sachartre vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl;
56864963Sachartre
56874963Sachartre
56884963Sachartre /* Initialize vtoc structure for single-slice device */
56894963Sachartre bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part));
56904963Sachartre vd->vtoc.v_part[0].p_tag = V_UNASSIGNED;
56914963Sachartre vd->vtoc.v_part[0].p_flag = 0;
56926836Sachartre /*
56936836Sachartre * Partition 0 starts on cylinder 1 and its size has to be
56946836Sachartre * a multiple of a number of cylinder.
56956836Sachartre */
56966836Sachartre vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */
56976836Sachartre vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize;
56986836Sachartre
56996836Sachartre if (vd_slice_single_slice) {
57006836Sachartre vd->vtoc.v_nparts = 1;
57016836Sachartre bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel,
57026836Sachartre MIN(sizeof (VD_ASCIILABEL),
57036836Sachartre sizeof (vd->vtoc.v_asciilabel)));
57046836Sachartre bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume,
57056836Sachartre MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume)));
57066836Sachartre } else {
57076836Sachartre /* adjust the number of slices */
57086836Sachartre vd->nslices = V_NUMPAR;
57096836Sachartre vd->vtoc.v_nparts = V_NUMPAR;
57106836Sachartre
57116836Sachartre /* define slice 2 representing the entire disk */
57126836Sachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP;
57136836Sachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0;
57146836Sachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0;
57156836Sachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size =
57166836Sachartre vd->dk_geom.dkg_ncyl * csize;
57176836Sachartre
57189889SLarry.Liu@Sun.COM vd_get_readable_size(vd->vdisk_size * vd->vdisk_bsize,
57196836Sachartre &size, &unit);
57206836Sachartre
57216836Sachartre /*
57226836Sachartre * Set some attributes of the geometry to what format(1m) uses
57236836Sachartre * so that writing a default label using format(1m) does not
57246836Sachartre * produce any error.
57256836Sachartre */
57266836Sachartre vd->dk_geom.dkg_bcyl = 0;
57276836Sachartre vd->dk_geom.dkg_intrlv = 1;
57286836Sachartre vd->dk_geom.dkg_write_reinstruct = 0;
57296836Sachartre vd->dk_geom.dkg_read_reinstruct = 0;
57306836Sachartre
57316836Sachartre /*
57326836Sachartre * We must have a correct label name otherwise format(1m) will
57336836Sachartre * not recognized the disk as labeled.
57346836Sachartre */
57356836Sachartre (void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII,
57366836Sachartre "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d",
57376836Sachartre size, unit,
57386836Sachartre vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl,
57396836Sachartre vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect);
57406836Sachartre bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume));
57416836Sachartre
57426836Sachartre /* create a fake label from the vtoc and geometry */
57437563SPrasad.Singamsetty@Sun.COM vd->flabel_limit = (uint_t)csize;
57449889SLarry.Liu@Sun.COM vd->flabel_size = VD_LABEL_VTOC_SIZE(vd->vdisk_bsize);
57456836Sachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
57466836Sachartre vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom,
57476836Sachartre VD_LABEL_VTOC(vd));
57486836Sachartre }
57496836Sachartre
57506836Sachartre /* adjust the vdisk_size, we emulate 3 cylinders */
57516836Sachartre vd->vdisk_size += csize * 3;
57525874Sachartre
57534963Sachartre return (0);
57544963Sachartre }
57554963Sachartre
57565874Sachartre /*
57575874Sachartre * When a slice, volume or file is exported as a single-slice disk, we want
57585874Sachartre * the disk backend (i.e. the slice, volume or file) to be entirely mapped
57595874Sachartre * as a slice without the addition of any metadata.
57605874Sachartre *
57615874Sachartre * So when exporting the disk as an EFI disk, we fake a disk with the following
57629889SLarry.Liu@Sun.COM * layout: (assuming the block size is 512 bytes)
57635874Sachartre *
57646836Sachartre * flabel +--- flabel_limit
57656836Sachartre * <------> v
57666836Sachartre * 0 1 2 L 34 34+N P
57676836Sachartre * +-+-+--+-------+--------------------------+-------+
57686836Sachartre * virtual disk: |X|T|EE|XXXXXXX| slice 0 |RRRRRRR|
57696836Sachartre * +-+-+--+-------+--------------------------+-------+
57706836Sachartre * ^ ^ : :
57716836Sachartre * | | : :
57726836Sachartre * GPT-+ +-GPE : :
57736836Sachartre * +--------------------------+
57746836Sachartre * disk backend: | slice/volume/file |
57756836Sachartre * +--------------------------+
57766836Sachartre * 0 N
57775874Sachartre *
57785874Sachartre * N is the number of blocks in the slice/volume/file.
57795874Sachartre *
57806836Sachartre * We simulate a disk with N+M blocks, where M is the number of blocks
57816836Sachartre * simluated at the beginning and at the end of the disk (blocks 0-34
57826836Sachartre * and 34+N-P).
57836836Sachartre *
57846836Sachartre * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34
57856836Sachartre * to 34+N defines slice 0 and are mapped to the exported backend, and we
57866836Sachartre * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI
57876836Sachartre * reserved partition.
57885874Sachartre *
57896836Sachartre * - block 0 (X) is unused and return 0
57905874Sachartre * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI)
57916836Sachartre * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI)
57926836Sachartre * - blocks L to 33 (X) are unused and return 0
57936836Sachartre * - blocks 34 to 34+N are mapped to the exported slice, volume or file
57946836Sachartre * - blocks 34+N+1 to P define a fake reserved partition and backup label, it
57956836Sachartre * returns 0
57965874Sachartre *
57979889SLarry.Liu@Sun.COM * Note: if the backend size is not a multiple of the vdisk block size then
57989889SLarry.Liu@Sun.COM * the very end of the backend will not map to any block of the virtual disk.
57995874Sachartre */
58004963Sachartre static int
vd_setup_partition_efi(vd_t * vd)58012531Snarayan vd_setup_partition_efi(vd_t *vd)
58022531Snarayan {
58032531Snarayan efi_gpt_t *gpt;
58042531Snarayan efi_gpe_t *gpe;
58055874Sachartre struct uuid uuid = EFI_USR;
58066836Sachartre struct uuid efi_reserved = EFI_RESERVED;
58072531Snarayan uint32_t crc;
58089889SLarry.Liu@Sun.COM uint64_t s0_start, s0_end, first_u_lba;
58099889SLarry.Liu@Sun.COM size_t bsize;
58109889SLarry.Liu@Sun.COM
58119889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_bsize > 0);
58129889SLarry.Liu@Sun.COM
58139889SLarry.Liu@Sun.COM bsize = vd->vdisk_bsize;
58149889SLarry.Liu@Sun.COM /*
58159889SLarry.Liu@Sun.COM * The minimum size for the label is 16K (EFI_MIN_ARRAY_SIZE)
58169889SLarry.Liu@Sun.COM * for GPEs plus one block for the GPT and one for PMBR.
58179889SLarry.Liu@Sun.COM */
58189889SLarry.Liu@Sun.COM first_u_lba = (EFI_MIN_ARRAY_SIZE / bsize) + 2;
58199889SLarry.Liu@Sun.COM vd->flabel_limit = (uint_t)first_u_lba;
58209889SLarry.Liu@Sun.COM vd->flabel_size = VD_LABEL_EFI_SIZE(bsize);
58216836Sachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP);
58229889SLarry.Liu@Sun.COM gpt = VD_LABEL_EFI_GPT(vd, bsize);
58239889SLarry.Liu@Sun.COM gpe = VD_LABEL_EFI_GPE(vd, bsize);
58249889SLarry.Liu@Sun.COM
58259889SLarry.Liu@Sun.COM /*
58269889SLarry.Liu@Sun.COM * Adjust the vdisk_size, we emulate the first few blocks
58279889SLarry.Liu@Sun.COM * for the disk label.
58289889SLarry.Liu@Sun.COM */
58299889SLarry.Liu@Sun.COM vd->vdisk_size += first_u_lba;
58309889SLarry.Liu@Sun.COM s0_start = first_u_lba;
58316836Sachartre s0_end = vd->vdisk_size - 1;
58322531Snarayan
58332531Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE);
58342531Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT);
58352531Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t));
58369889SLarry.Liu@Sun.COM gpt->efi_gpt_FirstUsableLBA = LE_64(first_u_lba);
58375874Sachartre gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL);
58382531Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t));
58392531Snarayan
58406836Sachartre UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid);
58416836Sachartre gpe[0].efi_gpe_StartingLBA = LE_64(s0_start);
58426836Sachartre gpe[0].efi_gpe_EndingLBA = LE_64(s0_end);
58436836Sachartre
58446836Sachartre if (vd_slice_single_slice) {
58456836Sachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1);
58466836Sachartre } else {
58476836Sachartre /* adjust the number of slices */
58486836Sachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART);
58496836Sachartre vd->nslices = V_NUMPAR;
58506836Sachartre
58516836Sachartre /* define a fake reserved partition */
58526836Sachartre UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID,
58536836Sachartre efi_reserved);
58546836Sachartre gpe[VD_MAXPART - 1].efi_gpe_StartingLBA =
58556836Sachartre LE_64(s0_end + 1);
58566836Sachartre gpe[VD_MAXPART - 1].efi_gpe_EndingLBA =
58576836Sachartre LE_64(s0_end + EFI_MIN_RESV_SIZE);
58586836Sachartre
58596836Sachartre /* adjust the vdisk_size to include the reserved slice */
58606836Sachartre vd->vdisk_size += EFI_MIN_RESV_SIZE;
58616836Sachartre }
58626836Sachartre
58636836Sachartre gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1);
58646836Sachartre
58656836Sachartre /* adjust the vdisk size for the backup GPT and GPE */
58669889SLarry.Liu@Sun.COM vd->vdisk_size += (EFI_MIN_ARRAY_SIZE / bsize) + 1;
58679889SLarry.Liu@Sun.COM gpt->efi_gpt_AlternateLBA = LE_64(vd->vdisk_size - 1);
58686836Sachartre
58696836Sachartre CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table);
58702531Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc);
58712531Snarayan
58722531Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table);
58732531Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc);
58742531Snarayan
58752531Snarayan return (0);
58762531Snarayan }
58772531Snarayan
58785081Sachartre /*
58795081Sachartre * Setup for a virtual disk whose backend is a file (exported as a single slice
58807597SAlexandre.Chartre@Sun.COM * or as a full disk). In that case, the backend is accessed using the vnode
58817597SAlexandre.Chartre@Sun.COM * interface.
58825081Sachartre */
58832531Snarayan static int
vd_setup_backend_vnode(vd_t * vd)58845081Sachartre vd_setup_backend_vnode(vd_t *vd)
58853401Snarayan {
58864963Sachartre int rval, status;
58873401Snarayan dev_t dev;
58883401Snarayan char *file_path = vd->device_path;
58893401Snarayan ldi_handle_t lhandle;
58903401Snarayan struct dk_cinfo dk_cinfo;
58917597SAlexandre.Chartre@Sun.COM
58927597SAlexandre.Chartre@Sun.COM ASSERT(!vd->volume);
58933401Snarayan
58945081Sachartre if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX,
58953401Snarayan 0, &vd->file_vnode, 0, 0)) != 0) {
5896*12847SZachary.Kissel@Sun.COM if ((status == ENXIO || status == ENODEV || status == ENOENT ||
5897*12847SZachary.Kissel@Sun.COM status == EROFS) && (!(vd->initialized & VD_SETUP_ERROR) &&
5898*12847SZachary.Kissel@Sun.COM !(DEVI_IS_ATTACHING(vd->vds->dip)))) {
5899*12847SZachary.Kissel@Sun.COM PRN("vn_open(%s) = errno %d", file_path, status);
5900*12847SZachary.Kissel@Sun.COM }
59013401Snarayan return (status);
59023401Snarayan }
59033401Snarayan
59043782Sachartre /*
59053782Sachartre * We set vd->file now so that vds_destroy_vd will take care of
59063782Sachartre * closing the file and releasing the vnode in case of an error.
59073782Sachartre */
59083782Sachartre vd->file = B_TRUE;
59093782Sachartre
59103401Snarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */
59113401Snarayan
59125081Sachartre /*
59137597SAlexandre.Chartre@Sun.COM * Get max_xfer_sz from the device where the file is.
59145081Sachartre */
59157597SAlexandre.Chartre@Sun.COM dev = vd->file_vnode->v_vfsp->vfs_dev;
59167597SAlexandre.Chartre@Sun.COM PR0("underlying device of %s = (%d, %d)\n", file_path,
59177597SAlexandre.Chartre@Sun.COM getmajor(dev), getminor(dev));
59187597SAlexandre.Chartre@Sun.COM
59197597SAlexandre.Chartre@Sun.COM status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle,
59207597SAlexandre.Chartre@Sun.COM vd->vds->ldi_ident);
59213401Snarayan
59225081Sachartre if (status != 0) {
59236904Sachartre PR0("ldi_open() returned errno %d for underlying device",
59246904Sachartre status);
59253401Snarayan } else {
59263401Snarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO,
59275081Sachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
59283401Snarayan &rval)) != 0) {
59296904Sachartre PR0("ldi_ioctl(DKIOCINFO) returned errno %d for "
59306904Sachartre "underlying device", status);
59313401Snarayan } else {
59323401Snarayan /*
59333401Snarayan * Store the device's max transfer size for
59343401Snarayan * return to the client
59353401Snarayan */
59363401Snarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
59373401Snarayan }
59383401Snarayan
59396904Sachartre PR0("close the underlying device");
59403401Snarayan (void) ldi_close(lhandle, FREAD, kcred);
59413401Snarayan }
59423401Snarayan
59437597SAlexandre.Chartre@Sun.COM PR0("using file %s on device (%d, %d), max_xfer = %u blks",
59447597SAlexandre.Chartre@Sun.COM file_path, getmajor(dev), getminor(dev), vd->max_xfer_sz);
59457597SAlexandre.Chartre@Sun.COM
59467597SAlexandre.Chartre@Sun.COM if (vd->vdisk_type == VD_DISK_TYPE_SLICE)
59477597SAlexandre.Chartre@Sun.COM status = vd_setup_slice_image(vd);
59487597SAlexandre.Chartre@Sun.COM else
59497597SAlexandre.Chartre@Sun.COM status = vd_setup_disk_image(vd);
59507597SAlexandre.Chartre@Sun.COM
59517597SAlexandre.Chartre@Sun.COM return (status);
59527597SAlexandre.Chartre@Sun.COM }
59537597SAlexandre.Chartre@Sun.COM
59547597SAlexandre.Chartre@Sun.COM static int
vd_setup_slice_image(vd_t * vd)59557597SAlexandre.Chartre@Sun.COM vd_setup_slice_image(vd_t *vd)
59567597SAlexandre.Chartre@Sun.COM {
59577597SAlexandre.Chartre@Sun.COM struct dk_label label;
59587597SAlexandre.Chartre@Sun.COM int status;
59597597SAlexandre.Chartre@Sun.COM
596010063SAlexandre.Chartre@Sun.COM if ((status = vd_backend_check_size(vd)) != 0) {
596110063SAlexandre.Chartre@Sun.COM PRN("Check size failed for %s (errno %d)",
596210063SAlexandre.Chartre@Sun.COM vd->device_path, status);
596310063SAlexandre.Chartre@Sun.COM return (EIO);
596410063SAlexandre.Chartre@Sun.COM }
596510063SAlexandre.Chartre@Sun.COM
59667597SAlexandre.Chartre@Sun.COM vd->vdisk_media = VD_MEDIA_FIXED;
59677597SAlexandre.Chartre@Sun.COM vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)?
59687597SAlexandre.Chartre@Sun.COM vd_file_slice_label : vd_slice_label;
59697597SAlexandre.Chartre@Sun.COM
59707597SAlexandre.Chartre@Sun.COM if (vd->vdisk_label == VD_DISK_LABEL_EFI ||
59717597SAlexandre.Chartre@Sun.COM vd->dskimg_size >= 2 * ONE_TERABYTE) {
59727597SAlexandre.Chartre@Sun.COM status = vd_setup_partition_efi(vd);
59736904Sachartre } else {
59747597SAlexandre.Chartre@Sun.COM /*
59757597SAlexandre.Chartre@Sun.COM * We build a default label to get a geometry for
59767597SAlexandre.Chartre@Sun.COM * the vdisk. Then the partition setup function will
59777597SAlexandre.Chartre@Sun.COM * adjust the vtoc so that it defines a single-slice
59787597SAlexandre.Chartre@Sun.COM * disk.
59797597SAlexandre.Chartre@Sun.COM */
59809889SLarry.Liu@Sun.COM vd_build_default_label(vd->dskimg_size, vd->vdisk_bsize,
59819889SLarry.Liu@Sun.COM &label);
59827597SAlexandre.Chartre@Sun.COM vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
59837597SAlexandre.Chartre@Sun.COM status = vd_setup_partition_vtoc(vd);
59847597SAlexandre.Chartre@Sun.COM }
59857597SAlexandre.Chartre@Sun.COM
59867597SAlexandre.Chartre@Sun.COM return (status);
59877597SAlexandre.Chartre@Sun.COM }
59887597SAlexandre.Chartre@Sun.COM
59897597SAlexandre.Chartre@Sun.COM static int
vd_setup_disk_image(vd_t * vd)59907597SAlexandre.Chartre@Sun.COM vd_setup_disk_image(vd_t *vd)
59917597SAlexandre.Chartre@Sun.COM {
59927597SAlexandre.Chartre@Sun.COM int status;
59937597SAlexandre.Chartre@Sun.COM char *backend_path = vd->device_path;
59947597SAlexandre.Chartre@Sun.COM
59959889SLarry.Liu@Sun.COM if ((status = vd_backend_check_size(vd)) != 0) {
599610063SAlexandre.Chartre@Sun.COM PRN("Check size failed for %s (errno %d)",
59979889SLarry.Liu@Sun.COM backend_path, status);
59989889SLarry.Liu@Sun.COM return (EIO);
59999889SLarry.Liu@Sun.COM }
60009889SLarry.Liu@Sun.COM
60017597SAlexandre.Chartre@Sun.COM /* size should be at least sizeof(dk_label) */
60027597SAlexandre.Chartre@Sun.COM if (vd->dskimg_size < sizeof (struct dk_label)) {
60037597SAlexandre.Chartre@Sun.COM PRN("Size of file has to be at least %ld bytes",
60047597SAlexandre.Chartre@Sun.COM sizeof (struct dk_label));
60057597SAlexandre.Chartre@Sun.COM return (EIO);
60067597SAlexandre.Chartre@Sun.COM }
60077597SAlexandre.Chartre@Sun.COM
60085874Sachartre /*
60095874Sachartre * Find and validate the geometry of a disk image.
60105874Sachartre */
60117597SAlexandre.Chartre@Sun.COM status = vd_dskimg_validate_geometry(vd);
60125874Sachartre if (status != 0 && status != EINVAL && status != ENOTSUP) {
60137597SAlexandre.Chartre@Sun.COM PRN("Failed to read label from %s", backend_path);
60145874Sachartre return (EIO);
60155874Sachartre }
60165874Sachartre
60177597SAlexandre.Chartre@Sun.COM if (vd_dskimg_is_iso_image(vd)) {
60185874Sachartre /*
60195874Sachartre * Indicate whether to call this a CD or DVD from the size
60205874Sachartre * of the ISO image (images for both drive types are stored
60215874Sachartre * in the ISO-9600 format). CDs can store up to just under 1Gb
60225874Sachartre */
60239889SLarry.Liu@Sun.COM if ((vd->vdisk_size * vd->vdisk_bsize) > ONE_GIGABYTE)
60245874Sachartre vd->vdisk_media = VD_MEDIA_DVD;
60255874Sachartre else
60265874Sachartre vd->vdisk_media = VD_MEDIA_CD;
60275874Sachartre } else {
60285874Sachartre vd->vdisk_media = VD_MEDIA_FIXED;
60295874Sachartre }
60305874Sachartre
60314696Sachartre /* Setup devid for the disk image */
60324696Sachartre
60334963Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
60344963Sachartre
60357597SAlexandre.Chartre@Sun.COM status = vd_dskimg_read_devid(vd, &vd->dskimg_devid);
60364963Sachartre
60374963Sachartre if (status == 0) {
60384963Sachartre /* a valid devid was found */
60394963Sachartre return (0);
60404963Sachartre }
60414963Sachartre
60424963Sachartre if (status != EINVAL) {
60434963Sachartre /*
60444963Sachartre * There was an error while trying to read the devid.
60454963Sachartre * So this disk image may have a devid but we are
60464963Sachartre * unable to read it.
60474963Sachartre */
60487597SAlexandre.Chartre@Sun.COM PR0("can not read devid for %s", backend_path);
60497597SAlexandre.Chartre@Sun.COM vd->dskimg_devid = NULL;
60504963Sachartre return (0);
60514963Sachartre }
60524696Sachartre }
60534696Sachartre
60544696Sachartre /*
60554696Sachartre * No valid device id was found so we create one. Note that a failure
60564696Sachartre * to create a device id is not fatal and does not prevent the disk
60574696Sachartre * image from being attached.
60584696Sachartre */
60597597SAlexandre.Chartre@Sun.COM PR1("creating devid for %s", backend_path);
60604696Sachartre
60614696Sachartre if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0,
60627597SAlexandre.Chartre@Sun.COM &vd->dskimg_devid) != DDI_SUCCESS) {
60637597SAlexandre.Chartre@Sun.COM PR0("fail to create devid for %s", backend_path);
60647597SAlexandre.Chartre@Sun.COM vd->dskimg_devid = NULL;
60654696Sachartre return (0);
60664696Sachartre }
60674696Sachartre
60684963Sachartre /*
60694963Sachartre * Write devid to the disk image. The devid is stored into the disk
60704963Sachartre * image if we have a valid label; otherwise the devid will be stored
60714963Sachartre * when the user writes a valid label.
60724963Sachartre */
60734963Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) {
60747597SAlexandre.Chartre@Sun.COM if (vd_dskimg_write_devid(vd, vd->dskimg_devid) != 0) {
60757597SAlexandre.Chartre@Sun.COM PR0("fail to write devid for %s", backend_path);
60767597SAlexandre.Chartre@Sun.COM ddi_devid_free(vd->dskimg_devid);
60777597SAlexandre.Chartre@Sun.COM vd->dskimg_devid = NULL;
60784963Sachartre }
60794696Sachartre }
60804696Sachartre
60813401Snarayan return (0);
60823401Snarayan }
60833401Snarayan
60845365Slm66018
60855365Slm66018 /*
60865365Slm66018 * Description:
60875365Slm66018 * Open a device using its device path (supplied by ldm(1m))
60885365Slm66018 *
60895365Slm66018 * Parameters:
60905365Slm66018 * vd - pointer to structure containing the vDisk info
60916108Sachartre * flags - open flags
60925365Slm66018 *
60935365Slm66018 * Return Value
60945365Slm66018 * 0 - success
60955365Slm66018 * != 0 - some other non-zero return value from ldi(9F) functions
60965365Slm66018 */
60975365Slm66018 static int
vd_open_using_ldi_by_name(vd_t * vd,int flags)60986108Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags)
60995365Slm66018 {
61006108Sachartre int status;
61015365Slm66018 char *device_path = vd->device_path;
61025365Slm66018
61036108Sachartre /* Attempt to open device */
61046108Sachartre status = ldi_open_by_name(device_path, flags, kcred,
61055365Slm66018 &vd->ldi_handle[0], vd->vds->ldi_ident);
61065365Slm66018
61075365Slm66018 /*
61085365Slm66018 * The open can fail for example if we are opening an empty slice.
61095365Slm66018 * In case of a failure, we try the open again but this time with
61105365Slm66018 * the FNDELAY flag.
61115365Slm66018 */
61125365Slm66018 if (status != 0)
61136108Sachartre status = ldi_open_by_name(device_path, flags | FNDELAY,
61145365Slm66018 kcred, &vd->ldi_handle[0], vd->vds->ldi_ident);
61155365Slm66018
61165365Slm66018 if (status != 0) {
61175365Slm66018 PR0("ldi_open_by_name(%s) = errno %d", device_path, status);
61185365Slm66018 vd->ldi_handle[0] = NULL;
61195365Slm66018 return (status);
61205365Slm66018 }
61215365Slm66018
61226108Sachartre return (0);
61236108Sachartre }
61246108Sachartre
61256108Sachartre /*
61266108Sachartre * Setup for a virtual disk which backend is a device (a physical disk,
61277597SAlexandre.Chartre@Sun.COM * slice or volume device) exported as a full disk or as a slice. In these
61287597SAlexandre.Chartre@Sun.COM * cases, the backend is accessed using the LDI interface.
61296108Sachartre */
61306108Sachartre static int
vd_setup_backend_ldi(vd_t * vd)61316108Sachartre vd_setup_backend_ldi(vd_t *vd)
61326108Sachartre {
61336108Sachartre int rval, status;
61346108Sachartre struct dk_cinfo dk_cinfo;
61356108Sachartre char *device_path = vd->device_path;
61366108Sachartre
61376108Sachartre /* device has been opened by vd_identify_dev() */
61386108Sachartre ASSERT(vd->ldi_handle[0] != NULL);
61396108Sachartre ASSERT(vd->dev[0] != NULL);
61406108Sachartre
61416108Sachartre vd->file = B_FALSE;
61426108Sachartre
61435365Slm66018 /* Verify backing device supports dk_cinfo */
61445365Slm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO,
61455365Slm66018 (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred,
61465365Slm66018 &rval)) != 0) {
61475365Slm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s",
61485365Slm66018 status, device_path);
61495365Slm66018 return (status);
61505365Slm66018 }
61515365Slm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) {
61525365Slm66018 PRN("slice %u >= maximum slice %u for %s",
61535365Slm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path);
61545365Slm66018 return (EIO);
61555365Slm66018 }
61565365Slm66018
61576108Sachartre /*
61586108Sachartre * The device has been opened read-only by vd_identify_dev(), re-open
61596108Sachartre * it read-write if the write flag is set and we don't have an optical
61606108Sachartre * device such as a CD-ROM, which, for now, we do not permit writes to
61616108Sachartre * and thus should not export write operations to the client.
61626108Sachartre *
61636108Sachartre * Future: if/when we implement support for guest domains writing to
61646108Sachartre * optical devices we will need to do further checking of the media type
61656108Sachartre * to distinguish between read-only and writable discs.
61666108Sachartre */
61676108Sachartre if (dk_cinfo.dki_ctype == DKC_CDROM) {
61686108Sachartre
61696108Sachartre vd->open_flags &= ~FWRITE;
61706108Sachartre
61716108Sachartre } else if (vd->open_flags & FWRITE) {
61726108Sachartre
61736108Sachartre (void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE,
61746108Sachartre kcred);
61756108Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags);
61766108Sachartre if (status != 0) {
61776108Sachartre PR0("Failed to open (%s) = errno %d",
61786108Sachartre device_path, status);
61796108Sachartre return (status);
61806108Sachartre }
61811991Sheppo }
61822531Snarayan
61832410Slm66018 /* Store the device's max transfer size for return to the client */
61842410Slm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer;
61852410Slm66018
61865081Sachartre /*
61875365Slm66018 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so
61885365Slm66018 * that we can use the correct CDB group when sending USCSI commands.
61895365Slm66018 */
61905365Slm66018 vd->is_atapi_dev = vd_is_atapi_device(vd);
61915365Slm66018
61925365Slm66018 /*
61935081Sachartre * Export a full disk.
61945081Sachartre *
61957597SAlexandre.Chartre@Sun.COM * The exported device can be either a volume, a disk or a CD/DVD
61967597SAlexandre.Chartre@Sun.COM * device. We export a device as a full disk if we have an entire
61977597SAlexandre.Chartre@Sun.COM * disk slice (slice 2) and if this slice is exported as a full disk
61987597SAlexandre.Chartre@Sun.COM * and not as a single slice disk. A CD or DVD device is exported
61997597SAlexandre.Chartre@Sun.COM * as a full disk (even if it isn't s2). A volume is exported as a
62007597SAlexandre.Chartre@Sun.COM * full disk as long as the "slice" option is not specified.
62015081Sachartre */
62027597SAlexandre.Chartre@Sun.COM if (vd->vdisk_type == VD_DISK_TYPE_DISK) {
62037597SAlexandre.Chartre@Sun.COM
62047597SAlexandre.Chartre@Sun.COM if (vd->volume) {
62057597SAlexandre.Chartre@Sun.COM /* setup disk image */
62067597SAlexandre.Chartre@Sun.COM return (vd_setup_disk_image(vd));
62077597SAlexandre.Chartre@Sun.COM }
62087597SAlexandre.Chartre@Sun.COM
62097597SAlexandre.Chartre@Sun.COM if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE ||
62107597SAlexandre.Chartre@Sun.COM dk_cinfo.dki_ctype == DKC_CDROM) {
62117597SAlexandre.Chartre@Sun.COM ASSERT(!vd->volume);
62127597SAlexandre.Chartre@Sun.COM if (dk_cinfo.dki_ctype == DKC_SCSI_CCS)
62137597SAlexandre.Chartre@Sun.COM vd->scsi = B_TRUE;
62147597SAlexandre.Chartre@Sun.COM return (vd_setup_full_disk(vd));
62157597SAlexandre.Chartre@Sun.COM }
62165081Sachartre }
62175081Sachartre
62185081Sachartre /*
62195081Sachartre * Export a single slice disk.
62205081Sachartre *
62216108Sachartre * The exported device can be either a volume device or a disk slice. If
62225081Sachartre * it is a disk slice different from slice 2 then it is always exported
62235081Sachartre * as a single slice disk even if the "slice" option is not specified.
62246108Sachartre * If it is disk slice 2 or a volume device then it is exported as a
62255081Sachartre * single slice disk only if the "slice" option is specified.
62265081Sachartre */
62275081Sachartre return (vd_setup_single_slice_disk(vd));
62285081Sachartre }
62295081Sachartre
62305081Sachartre static int
vd_setup_single_slice_disk(vd_t * vd)62315081Sachartre vd_setup_single_slice_disk(vd_t *vd)
62325081Sachartre {
62335874Sachartre int status, rval;
62346836Sachartre struct dk_label label;
62355081Sachartre char *device_path = vd->device_path;
62367563SPrasad.Singamsetty@Sun.COM struct vtoc vtoc;
62375081Sachartre
62385365Slm66018 vd->vdisk_media = VD_MEDIA_FIXED;
62395081Sachartre
62406108Sachartre if (vd->volume) {
62415081Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE);
62424963Sachartre }
62432032Slm66018
62445081Sachartre /*
62455081Sachartre * We export the slice as a single slice disk even if the "slice"
62465081Sachartre * option was not specified.
62475081Sachartre */
62485874Sachartre vd->vdisk_type = VD_DISK_TYPE_SLICE;
62491991Sheppo vd->nslices = 1;
62501991Sheppo
62519889SLarry.Liu@Sun.COM /* Get size of backing device */
62529889SLarry.Liu@Sun.COM if ((status = vd_backend_check_size(vd)) != 0) {
625310063SAlexandre.Chartre@Sun.COM PRN("Check size failed for %s (errno %d)", device_path, status);
62549889SLarry.Liu@Sun.COM return (EIO);
62559889SLarry.Liu@Sun.COM }
62569889SLarry.Liu@Sun.COM
62575874Sachartre /*
62585874Sachartre * When exporting a slice or a device as a single slice disk, we don't
62595874Sachartre * care about any partitioning exposed by the backend. The goal is just
62605874Sachartre * to export the backend as a flat storage. We provide a fake partition
62615874Sachartre * table (either a VTOC or EFI), which presents only one slice, to
62626836Sachartre * accommodate tools expecting a disk label. The selection of the label
62636836Sachartre * type (VTOC or EFI) depends on the value of the vd_slice_label
62646836Sachartre * variable.
62655874Sachartre */
62666836Sachartre if (vd_slice_label == VD_DISK_LABEL_EFI ||
62679889SLarry.Liu@Sun.COM vd->vdisk_size >= ONE_TERABYTE / vd->vdisk_bsize) {
62686836Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI;
62696836Sachartre } else {
62707563SPrasad.Singamsetty@Sun.COM status = ldi_ioctl(vd->ldi_handle[0], DKIOCGEXTVTOC,
62716836Sachartre (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL),
62726836Sachartre kcred, &rval);
62736836Sachartre
62747563SPrasad.Singamsetty@Sun.COM if (status == ENOTTY) {
62757563SPrasad.Singamsetty@Sun.COM /* try with the non-extended vtoc ioctl */
62767563SPrasad.Singamsetty@Sun.COM status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC,
62777563SPrasad.Singamsetty@Sun.COM (intptr_t)&vtoc, (vd->open_flags | FKIOCTL),
62787563SPrasad.Singamsetty@Sun.COM kcred, &rval);
62797563SPrasad.Singamsetty@Sun.COM vtoctoextvtoc(vtoc, vd->vtoc);
62807563SPrasad.Singamsetty@Sun.COM }
62817563SPrasad.Singamsetty@Sun.COM
62826836Sachartre if (status == 0) {
62836836Sachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM,
62846836Sachartre (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL),
62856836Sachartre kcred, &rval);
62866836Sachartre
62876836Sachartre if (status != 0) {
62886836Sachartre PRN("ldi_ioctl(DKIOCGEOM) returned errno %d "
62896836Sachartre "for %s", status, device_path);
62906836Sachartre return (status);
62916836Sachartre }
62926836Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC;
62936836Sachartre
62946836Sachartre } else if (vd_slice_label == VD_DISK_LABEL_VTOC) {
62956836Sachartre
62966836Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC;
62979889SLarry.Liu@Sun.COM vd_build_default_label(vd->vdisk_size * vd->vdisk_bsize,
62989889SLarry.Liu@Sun.COM vd->vdisk_bsize, &label);
62996836Sachartre vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom);
63006836Sachartre
63016836Sachartre } else {
63026836Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI;
63036836Sachartre }
63046836Sachartre }
63056836Sachartre
63066836Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
63075874Sachartre /* export with a fake VTOC label */
63085874Sachartre status = vd_setup_partition_vtoc(vd);
63096836Sachartre
63104963Sachartre } else {
63115874Sachartre /* export with a fake EFI label */
63125874Sachartre status = vd_setup_partition_efi(vd);
63131991Sheppo }
63144963Sachartre
63154963Sachartre return (status);
63161991Sheppo }
63171991Sheppo
63189889SLarry.Liu@Sun.COM /*
63199889SLarry.Liu@Sun.COM * This function is invoked when setting up the vdisk backend and to process
63209889SLarry.Liu@Sun.COM * the VD_OP_GET_CAPACITY operation. It checks the backend size and set the
63219889SLarry.Liu@Sun.COM * following attributes of the vd structure:
63229889SLarry.Liu@Sun.COM *
63239889SLarry.Liu@Sun.COM * - vdisk_bsize: block size for the virtual disk used by the VIO protocol. Its
63249889SLarry.Liu@Sun.COM * value is 512 bytes (DEV_BSIZE) when the backend is a file, a volume or a
63259889SLarry.Liu@Sun.COM * CD/DVD. When the backend is a disk or a disk slice then it has the value
63269889SLarry.Liu@Sun.COM * of the logical block size of that disk (as returned by the DKIOCGMEDIAINFO
63279889SLarry.Liu@Sun.COM * ioctl). This block size is expected to be a power of 2 and a multiple of
63289889SLarry.Liu@Sun.COM * 512.
63299889SLarry.Liu@Sun.COM *
63309889SLarry.Liu@Sun.COM * - vdisk_size: size of the virtual disk expressed as a number of vdisk_bsize
63319889SLarry.Liu@Sun.COM * blocks.
63329889SLarry.Liu@Sun.COM *
63339889SLarry.Liu@Sun.COM * vdisk_size and vdisk_bsize are sent to the vdisk client during the connection
63349889SLarry.Liu@Sun.COM * handshake and in the result of a VD_OP_GET_CAPACITY operation.
63359889SLarry.Liu@Sun.COM *
63369889SLarry.Liu@Sun.COM * - backend_bsize: block size of the backend device. backend_bsize has the same
63379889SLarry.Liu@Sun.COM * value as vdisk_bsize except when the backend is a CD/DVD. In that case,
63389889SLarry.Liu@Sun.COM * vdisk_bsize is set to 512 (DEV_BSIZE) while backend_bsize is set to the
63399889SLarry.Liu@Sun.COM * effective logical block size of the CD/DVD (usually 2048).
63409889SLarry.Liu@Sun.COM *
63419889SLarry.Liu@Sun.COM * - dskimg_size: size of the backend when the backend is a disk image. This
63429889SLarry.Liu@Sun.COM * attribute is set only when the backend is a file or a volume, otherwise it
63439889SLarry.Liu@Sun.COM * is unused.
63449889SLarry.Liu@Sun.COM *
63459889SLarry.Liu@Sun.COM * - vio_bshift: number of bit to shift to convert a VIO block number (which
63469889SLarry.Liu@Sun.COM * uses a block size of vdisk_bsize) to a buf(9s) block number (which uses a
63479889SLarry.Liu@Sun.COM * block size of 512 bytes) i.e. we have vdisk_bsize = 512 x 2 ^ vio_bshift
63489889SLarry.Liu@Sun.COM *
63499889SLarry.Liu@Sun.COM * - vdisk_media: media of the virtual disk. This function only sets this
63509889SLarry.Liu@Sun.COM * attribute for physical disk and CD/DVD. For other backend types, this
63519889SLarry.Liu@Sun.COM * attribute is set in the setup function of the backend.
63529889SLarry.Liu@Sun.COM */
63537540SRamesh.Chitrothu@Sun.COM static int
vd_backend_check_size(vd_t * vd)63547540SRamesh.Chitrothu@Sun.COM vd_backend_check_size(vd_t *vd)
63557540SRamesh.Chitrothu@Sun.COM {
63569889SLarry.Liu@Sun.COM size_t backend_size, backend_bsize, vdisk_bsize;
63579889SLarry.Liu@Sun.COM size_t old_size, new_size;
63587540SRamesh.Chitrothu@Sun.COM struct dk_minfo minfo;
63597540SRamesh.Chitrothu@Sun.COM vattr_t vattr;
63609889SLarry.Liu@Sun.COM int rval, rv, media, nshift = 0;
63619889SLarry.Liu@Sun.COM uint32_t n;
63627540SRamesh.Chitrothu@Sun.COM
63637540SRamesh.Chitrothu@Sun.COM if (vd->file) {
63647540SRamesh.Chitrothu@Sun.COM
63657540SRamesh.Chitrothu@Sun.COM /* file (slice or full disk) */
63667540SRamesh.Chitrothu@Sun.COM vattr.va_mask = AT_SIZE;
63677540SRamesh.Chitrothu@Sun.COM rv = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL);
63687540SRamesh.Chitrothu@Sun.COM if (rv != 0) {
63697540SRamesh.Chitrothu@Sun.COM PR0("VOP_GETATTR(%s) = errno %d", vd->device_path, rv);
63707540SRamesh.Chitrothu@Sun.COM return (rv);
63717540SRamesh.Chitrothu@Sun.COM }
63727540SRamesh.Chitrothu@Sun.COM backend_size = vattr.va_size;
63739889SLarry.Liu@Sun.COM backend_bsize = DEV_BSIZE;
63749889SLarry.Liu@Sun.COM vdisk_bsize = DEV_BSIZE;
63759889SLarry.Liu@Sun.COM
63769889SLarry.Liu@Sun.COM } else if (vd->volume) {
63779889SLarry.Liu@Sun.COM
63789889SLarry.Liu@Sun.COM /* volume (slice or full disk) */
63797540SRamesh.Chitrothu@Sun.COM rv = ldi_get_size(vd->ldi_handle[0], &backend_size);
63807540SRamesh.Chitrothu@Sun.COM if (rv != DDI_SUCCESS) {
63817540SRamesh.Chitrothu@Sun.COM PR0("ldi_get_size() failed for %s", vd->device_path);
63827540SRamesh.Chitrothu@Sun.COM return (EIO);
63837540SRamesh.Chitrothu@Sun.COM }
63849889SLarry.Liu@Sun.COM backend_bsize = DEV_BSIZE;
63859889SLarry.Liu@Sun.COM vdisk_bsize = DEV_BSIZE;
63867540SRamesh.Chitrothu@Sun.COM
63877540SRamesh.Chitrothu@Sun.COM } else {
63887540SRamesh.Chitrothu@Sun.COM
63899889SLarry.Liu@Sun.COM /* physical disk or slice */
63907540SRamesh.Chitrothu@Sun.COM rv = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO,
63917540SRamesh.Chitrothu@Sun.COM (intptr_t)&minfo, (vd->open_flags | FKIOCTL),
63927540SRamesh.Chitrothu@Sun.COM kcred, &rval);
63937540SRamesh.Chitrothu@Sun.COM if (rv != 0) {
63947540SRamesh.Chitrothu@Sun.COM PR0("DKIOCGMEDIAINFO failed for %s (err=%d)",
63957540SRamesh.Chitrothu@Sun.COM vd->device_path, rv);
63967540SRamesh.Chitrothu@Sun.COM return (rv);
63977540SRamesh.Chitrothu@Sun.COM }
63989889SLarry.Liu@Sun.COM
63999889SLarry.Liu@Sun.COM if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
64009889SLarry.Liu@Sun.COM rv = ldi_get_size(vd->ldi_handle[0], &backend_size);
64019889SLarry.Liu@Sun.COM if (rv != DDI_SUCCESS) {
64029889SLarry.Liu@Sun.COM PR0("ldi_get_size() failed for %s",
64039889SLarry.Liu@Sun.COM vd->device_path);
64049889SLarry.Liu@Sun.COM return (EIO);
64059889SLarry.Liu@Sun.COM }
64069889SLarry.Liu@Sun.COM } else {
64079889SLarry.Liu@Sun.COM ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
64089889SLarry.Liu@Sun.COM backend_size = minfo.dki_capacity * minfo.dki_lbsize;
64099889SLarry.Liu@Sun.COM }
64109889SLarry.Liu@Sun.COM
64119889SLarry.Liu@Sun.COM backend_bsize = minfo.dki_lbsize;
64129889SLarry.Liu@Sun.COM media = DK_MEDIATYPE2VD_MEDIATYPE(minfo.dki_media_type);
64139889SLarry.Liu@Sun.COM
64149889SLarry.Liu@Sun.COM /*
64159889SLarry.Liu@Sun.COM * If the device is a CD or a DVD then we force the vdisk block
64169889SLarry.Liu@Sun.COM * size to 512 bytes (DEV_BSIZE). In that case, vdisk_bsize can
64179889SLarry.Liu@Sun.COM * be different from backend_size.
64189889SLarry.Liu@Sun.COM */
64199889SLarry.Liu@Sun.COM if (media == VD_MEDIA_CD || media == VD_MEDIA_DVD)
64209889SLarry.Liu@Sun.COM vdisk_bsize = DEV_BSIZE;
64219889SLarry.Liu@Sun.COM else
64229889SLarry.Liu@Sun.COM vdisk_bsize = backend_bsize;
64239889SLarry.Liu@Sun.COM }
64249889SLarry.Liu@Sun.COM
64259889SLarry.Liu@Sun.COM /* check vdisk block size */
64269889SLarry.Liu@Sun.COM if (vdisk_bsize == 0 || vdisk_bsize % DEV_BSIZE != 0)
64279889SLarry.Liu@Sun.COM return (EINVAL);
64287540SRamesh.Chitrothu@Sun.COM
64297540SRamesh.Chitrothu@Sun.COM old_size = vd->vdisk_size;
64309889SLarry.Liu@Sun.COM new_size = backend_size / vdisk_bsize;
64317540SRamesh.Chitrothu@Sun.COM
64327540SRamesh.Chitrothu@Sun.COM /* check if size has changed */
64339889SLarry.Liu@Sun.COM if (old_size != VD_SIZE_UNKNOWN && old_size == new_size &&
64349889SLarry.Liu@Sun.COM vd->vdisk_bsize == vdisk_bsize)
64357540SRamesh.Chitrothu@Sun.COM return (0);
64367540SRamesh.Chitrothu@Sun.COM
64379889SLarry.Liu@Sun.COM /* cache info for blk conversion */
64389889SLarry.Liu@Sun.COM for (n = vdisk_bsize / DEV_BSIZE; n > 1; n >>= 1) {
64399889SLarry.Liu@Sun.COM if ((n & 0x1) != 0) {
64409889SLarry.Liu@Sun.COM /* blk_size is not a power of 2 */
64419889SLarry.Liu@Sun.COM return (EINVAL);
64429889SLarry.Liu@Sun.COM }
64439889SLarry.Liu@Sun.COM nshift++;
64449889SLarry.Liu@Sun.COM }
64459889SLarry.Liu@Sun.COM
64469889SLarry.Liu@Sun.COM vd->vio_bshift = nshift;
64477540SRamesh.Chitrothu@Sun.COM vd->vdisk_size = new_size;
64489889SLarry.Liu@Sun.COM vd->vdisk_bsize = vdisk_bsize;
64499889SLarry.Liu@Sun.COM vd->backend_bsize = backend_bsize;
64507540SRamesh.Chitrothu@Sun.COM
64517597SAlexandre.Chartre@Sun.COM if (vd->file || vd->volume)
64527597SAlexandre.Chartre@Sun.COM vd->dskimg_size = backend_size;
64537540SRamesh.Chitrothu@Sun.COM
64547540SRamesh.Chitrothu@Sun.COM /*
64557540SRamesh.Chitrothu@Sun.COM * If we are exporting a single-slice disk and the size of the backend
64567540SRamesh.Chitrothu@Sun.COM * has changed then we regenerate the partition setup so that the
64577540SRamesh.Chitrothu@Sun.COM * partitioning matches with the new disk backend size.
64587540SRamesh.Chitrothu@Sun.COM */
64597540SRamesh.Chitrothu@Sun.COM
64607540SRamesh.Chitrothu@Sun.COM if (vd->vdisk_type == VD_DISK_TYPE_SLICE) {
64617540SRamesh.Chitrothu@Sun.COM /* slice or file or device exported as a slice */
64627540SRamesh.Chitrothu@Sun.COM if (vd->vdisk_label == VD_DISK_LABEL_VTOC) {
64637540SRamesh.Chitrothu@Sun.COM rv = vd_setup_partition_vtoc(vd);
64647540SRamesh.Chitrothu@Sun.COM if (rv != 0) {
64657540SRamesh.Chitrothu@Sun.COM PR0("vd_setup_partition_vtoc() failed for %s "
64667540SRamesh.Chitrothu@Sun.COM "(err = %d)", vd->device_path, rv);
64677540SRamesh.Chitrothu@Sun.COM return (rv);
64687540SRamesh.Chitrothu@Sun.COM }
64697540SRamesh.Chitrothu@Sun.COM } else {
64707540SRamesh.Chitrothu@Sun.COM rv = vd_setup_partition_efi(vd);
64717540SRamesh.Chitrothu@Sun.COM if (rv != 0) {
64727540SRamesh.Chitrothu@Sun.COM PR0("vd_setup_partition_efi() failed for %s "
64737540SRamesh.Chitrothu@Sun.COM "(err = %d)", vd->device_path, rv);
64747540SRamesh.Chitrothu@Sun.COM return (rv);
64757540SRamesh.Chitrothu@Sun.COM }
64767540SRamesh.Chitrothu@Sun.COM }
64777540SRamesh.Chitrothu@Sun.COM
64787597SAlexandre.Chartre@Sun.COM } else if (!vd->file && !vd->volume) {
64797597SAlexandre.Chartre@Sun.COM /* physical disk */
64807540SRamesh.Chitrothu@Sun.COM ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK);
64819889SLarry.Liu@Sun.COM vd->vdisk_media = media;
64827540SRamesh.Chitrothu@Sun.COM }
64837540SRamesh.Chitrothu@Sun.COM
64847540SRamesh.Chitrothu@Sun.COM return (0);
64857540SRamesh.Chitrothu@Sun.COM }
64867540SRamesh.Chitrothu@Sun.COM
64876108Sachartre /*
64886108Sachartre * Description:
64896108Sachartre * Open a device using its device path and identify if this is
64906108Sachartre * a disk device or a volume device.
64916108Sachartre *
64926108Sachartre * Parameters:
64936108Sachartre * vd - pointer to structure containing the vDisk info
64946108Sachartre * dtype - return the driver type of the device
64956108Sachartre *
64966108Sachartre * Return Value
64976108Sachartre * 0 - success
64986108Sachartre * != 0 - some other non-zero return value from ldi(9F) functions
64996108Sachartre */
65006108Sachartre static int
vd_identify_dev(vd_t * vd,int * dtype)65016108Sachartre vd_identify_dev(vd_t *vd, int *dtype)
65026108Sachartre {
65036108Sachartre int status, i;
65046108Sachartre char *device_path = vd->device_path;
65056108Sachartre char *drv_name;
65066108Sachartre int drv_type;
65076108Sachartre vds_t *vds = vd->vds;
65086108Sachartre
65096108Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE);
65106108Sachartre if (status != 0) {
65116108Sachartre PR0("Failed to open (%s) = errno %d", device_path, status);
65126108Sachartre return (status);
65136108Sachartre }
65146108Sachartre
65156108Sachartre /* Get device number of backing device */
65166108Sachartre if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) {
65176108Sachartre PRN("ldi_get_dev() returned errno %d for %s",
65186108Sachartre status, device_path);
65196108Sachartre return (status);
65206108Sachartre }
65216108Sachartre
65226108Sachartre /*
65236108Sachartre * We start by looking if the driver is in the list from vds.conf
65246108Sachartre * so that we can override the built-in list using vds.conf.
65256108Sachartre */
65266108Sachartre drv_name = ddi_major_to_name(getmajor(vd->dev[0]));
65276108Sachartre drv_type = VD_DRIVER_UNKNOWN;
65286108Sachartre
65296108Sachartre /* check vds.conf list */
65306108Sachartre for (i = 0; i < vds->num_drivers; i++) {
65316108Sachartre if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) {
65326108Sachartre /* ignore invalid entries */
65336108Sachartre continue;
65346108Sachartre }
65356108Sachartre if (strcmp(drv_name, vds->driver_types[i].name) == 0) {
65366108Sachartre drv_type = vds->driver_types[i].type;
65376108Sachartre goto done;
65386108Sachartre }
65396108Sachartre }
65406108Sachartre
65416108Sachartre /* check built-in list */
65426108Sachartre for (i = 0; i < VDS_NUM_DRIVERS; i++) {
65436108Sachartre if (strcmp(drv_name, vds_driver_types[i].name) == 0) {
65446108Sachartre drv_type = vds_driver_types[i].type;
65456108Sachartre goto done;
65466108Sachartre }
65476108Sachartre }
65486108Sachartre
65496108Sachartre done:
65506108Sachartre PR0("driver %s identified as %s", drv_name,
65516108Sachartre (drv_type == VD_DRIVER_DISK)? "DISK" :
65526108Sachartre (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN");
65536108Sachartre
65547597SAlexandre.Chartre@Sun.COM if (strcmp(drv_name, "zfs") == 0)
65557597SAlexandre.Chartre@Sun.COM vd->zvol = B_TRUE;
65567597SAlexandre.Chartre@Sun.COM
65576108Sachartre *dtype = drv_type;
65586108Sachartre
65596108Sachartre return (0);
65606108Sachartre }
65616108Sachartre
65621991Sheppo static int
vd_setup_vd(vd_t * vd)65635081Sachartre vd_setup_vd(vd_t *vd)
65645081Sachartre {
65656108Sachartre int status, drv_type, pseudo;
65665081Sachartre dev_info_t *dip;
65675081Sachartre vnode_t *vnp;
65685081Sachartre char *path = vd->device_path;
65697791SAlexandre.Chartre@Sun.COM char tq_name[TASKQ_NAMELEN];
65705081Sachartre
65715081Sachartre /* make sure the vdisk backend is valid */
65725081Sachartre if ((status = lookupname(path, UIO_SYSSPACE,
65735081Sachartre FOLLOW, NULLVPP, &vnp)) != 0) {
65745081Sachartre PR0("Cannot lookup %s errno %d", path, status);
65755081Sachartre goto done;
65765081Sachartre }
65775081Sachartre
65785081Sachartre switch (vnp->v_type) {
65795081Sachartre case VREG:
65805081Sachartre /*
65815081Sachartre * Backend is a file so it is exported as a full disk or as a
65825081Sachartre * single slice disk using the vnode interface.
65835081Sachartre */
65845081Sachartre VN_RELE(vnp);
65856108Sachartre vd->volume = B_FALSE;
65865081Sachartre status = vd_setup_backend_vnode(vd);
65875081Sachartre break;
65885081Sachartre
65895081Sachartre case VBLK:
65905081Sachartre case VCHR:
65915081Sachartre /*
65927597SAlexandre.Chartre@Sun.COM * Backend is a device. In that case, it is exported using the
65937597SAlexandre.Chartre@Sun.COM * LDI interface, and it is exported either as a single-slice
65947597SAlexandre.Chartre@Sun.COM * disk or as a full disk depending on the "slice" option and
65957597SAlexandre.Chartre@Sun.COM * on the type of device.
65965081Sachartre *
65977597SAlexandre.Chartre@Sun.COM * - A volume device is exported as a single-slice disk if the
65987597SAlexandre.Chartre@Sun.COM * "slice" is specified, otherwise it is exported as a full
65997597SAlexandre.Chartre@Sun.COM * disk.
66005081Sachartre *
66015081Sachartre * - A disk slice (different from slice 2) is always exported
66025081Sachartre * as a single slice disk using the LDI interface.
66035081Sachartre *
66045081Sachartre * - The slice 2 of a disk is exported as a single slice disk
66055081Sachartre * if the "slice" option is specified, otherwise the entire
66067597SAlexandre.Chartre@Sun.COM * disk will be exported.
66077597SAlexandre.Chartre@Sun.COM *
66087597SAlexandre.Chartre@Sun.COM * - The slice of a CD or DVD is exported as single slice disk
66097597SAlexandre.Chartre@Sun.COM * if the "slice" option is specified, otherwise the entire
66107597SAlexandre.Chartre@Sun.COM * disk will be exported.
66115081Sachartre */
66125081Sachartre
66135081Sachartre /* check if this is a pseudo device */
66145081Sachartre if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev),
66155081Sachartre dev_to_instance(vnp->v_rdev), 0)) == NULL) {
66165081Sachartre PRN("%s is no longer accessible", path);
66175081Sachartre VN_RELE(vnp);
66185081Sachartre status = EIO;
66195081Sachartre break;
66205081Sachartre }
66216108Sachartre pseudo = is_pseudo_device(dip);
66225081Sachartre ddi_release_devi(dip);
66235081Sachartre VN_RELE(vnp);
66245081Sachartre
66258792SZachary.Kissel@Sun.COM if ((status = vd_identify_dev(vd, &drv_type)) != 0) {
66268792SZachary.Kissel@Sun.COM if (status != ENODEV && status != ENXIO &&
66278792SZachary.Kissel@Sun.COM status != ENOENT && status != EROFS) {
66288792SZachary.Kissel@Sun.COM PRN("%s identification failed with status %d",
66298792SZachary.Kissel@Sun.COM path, status);
66308792SZachary.Kissel@Sun.COM status = EIO;
66318792SZachary.Kissel@Sun.COM }
66326108Sachartre break;
66336108Sachartre }
66346108Sachartre
66356108Sachartre /*
66366108Sachartre * If the driver hasn't been identified then we consider that
66376108Sachartre * pseudo devices are volumes and other devices are disks.
66386108Sachartre */
66396108Sachartre if (drv_type == VD_DRIVER_VOLUME ||
66406108Sachartre (drv_type == VD_DRIVER_UNKNOWN && pseudo)) {
66416108Sachartre vd->volume = B_TRUE;
66425658Sachartre }
66435658Sachartre
66445081Sachartre /*
66456108Sachartre * If this is a volume device then its usage depends if the
66465081Sachartre * "slice" option is set or not. If the "slice" option is set
66476108Sachartre * then the volume device will be exported as a single slice,
66485081Sachartre * otherwise it will be exported as a full disk.
66495658Sachartre *
66505658Sachartre * For backward compatibility, if vd_volume_force_slice is set
66516108Sachartre * then we always export volume devices as slices.
66525081Sachartre */
66537597SAlexandre.Chartre@Sun.COM if (vd->volume && vd_volume_force_slice) {
66545658Sachartre vd->vdisk_type = VD_DISK_TYPE_SLICE;
66555658Sachartre vd->nslices = 1;
66565658Sachartre }
66575658Sachartre
66587597SAlexandre.Chartre@Sun.COM status = vd_setup_backend_ldi(vd);
66595081Sachartre break;
66605081Sachartre
66615081Sachartre default:
66625081Sachartre PRN("Unsupported vdisk backend %s", path);
66635081Sachartre VN_RELE(vnp);
66645081Sachartre status = EBADF;
66655081Sachartre }
66665081Sachartre
66675081Sachartre done:
66685081Sachartre if (status != 0) {
66695081Sachartre /*
66705081Sachartre * If the error is retryable print an error message only
66715081Sachartre * during the first try.
66725081Sachartre */
66735081Sachartre if (status == ENXIO || status == ENODEV ||
66745081Sachartre status == ENOENT || status == EROFS) {
6675*12847SZachary.Kissel@Sun.COM if (!(vd->initialized & VD_SETUP_ERROR) &&
6676*12847SZachary.Kissel@Sun.COM !(DEVI_IS_ATTACHING(vd->vds->dip))) {
66775081Sachartre PRN("%s is currently inaccessible (error %d)",
66785081Sachartre path, status);
66795081Sachartre }
66805081Sachartre status = EAGAIN;
66815081Sachartre } else {
66825081Sachartre PRN("%s can not be exported as a virtual disk "
66835081Sachartre "(error %d)", path, status);
66845081Sachartre }
66855081Sachartre vd->initialized |= VD_SETUP_ERROR;
66865081Sachartre
66875081Sachartre } else if (vd->initialized & VD_SETUP_ERROR) {
66885081Sachartre /* print a message only if we previously had an error */
66895081Sachartre PRN("%s is now online", path);
66905081Sachartre vd->initialized &= ~VD_SETUP_ERROR;
66915081Sachartre }
66925081Sachartre
66937791SAlexandre.Chartre@Sun.COM /*
66947791SAlexandre.Chartre@Sun.COM * For file or ZFS volume we also need an I/O queue.
66957791SAlexandre.Chartre@Sun.COM *
66967791SAlexandre.Chartre@Sun.COM * The I/O task queue is initialized here and not in vds_do_init_vd()
66977791SAlexandre.Chartre@Sun.COM * (as the start and completion queues) because vd_setup_vd() will be
66987791SAlexandre.Chartre@Sun.COM * call again if the backend is not available, and we need to know if
66997791SAlexandre.Chartre@Sun.COM * the backend is a ZFS volume or a file.
67007791SAlexandre.Chartre@Sun.COM */
67017791SAlexandre.Chartre@Sun.COM if ((vd->file || vd->zvol) && vd->ioq == NULL) {
67027791SAlexandre.Chartre@Sun.COM (void) snprintf(tq_name, sizeof (tq_name), "vd_ioq%lu", vd->id);
67037791SAlexandre.Chartre@Sun.COM
67047791SAlexandre.Chartre@Sun.COM if ((vd->ioq = ddi_taskq_create(vd->vds->dip, tq_name,
67057791SAlexandre.Chartre@Sun.COM vd_ioq_nthreads, TASKQ_DEFAULTPRI, 0)) == NULL) {
67067791SAlexandre.Chartre@Sun.COM PRN("Could not create io task queue");
67077791SAlexandre.Chartre@Sun.COM return (EIO);
67087791SAlexandre.Chartre@Sun.COM }
67097791SAlexandre.Chartre@Sun.COM }
67107791SAlexandre.Chartre@Sun.COM
67115081Sachartre return (status);
67125081Sachartre }
67135081Sachartre
67145081Sachartre static int
vds_do_init_vd(vds_t * vds,uint64_t id,char * device_path,uint64_t options,uint64_t ldc_id,vd_t ** vdp)67155081Sachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
67165081Sachartre uint64_t ldc_id, vd_t **vdp)
67171991Sheppo {
67181991Sheppo char tq_name[TASKQ_NAMELEN];
67192032Slm66018 int status;
67201991Sheppo ddi_iblock_cookie_t iblock = NULL;
67211991Sheppo ldc_attr_t ldc_attr;
67221991Sheppo vd_t *vd;
67231991Sheppo
67241991Sheppo
67251991Sheppo ASSERT(vds != NULL);
67262410Slm66018 ASSERT(device_path != NULL);
67271991Sheppo ASSERT(vdp != NULL);
67282410Slm66018 PR0("Adding vdisk for %s", device_path);
67291991Sheppo
67301991Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) {
67311991Sheppo PRN("No memory for virtual disk");
67321991Sheppo return (EAGAIN);
67331991Sheppo }
67341991Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */
67357791SAlexandre.Chartre@Sun.COM vd->id = id;
67361991Sheppo vd->vds = vds;
67373401Snarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN);
67381991Sheppo
67395081Sachartre /* Setup open flags */
67405081Sachartre vd->open_flags = FREAD;
67415081Sachartre
67425081Sachartre if (!(options & VD_OPT_RDONLY))
67435081Sachartre vd->open_flags |= FWRITE;
67445081Sachartre
67455081Sachartre if (options & VD_OPT_EXCLUSIVE)
67465081Sachartre vd->open_flags |= FEXCL;
67475081Sachartre
67485081Sachartre /* Setup disk type */
67495081Sachartre if (options & VD_OPT_SLICE) {
67505081Sachartre vd->vdisk_type = VD_DISK_TYPE_SLICE;
67515081Sachartre vd->nslices = 1;
67525081Sachartre } else {
67535081Sachartre vd->vdisk_type = VD_DISK_TYPE_DISK;
67545081Sachartre vd->nslices = V_NUMPAR;
67555081Sachartre }
67565081Sachartre
67575081Sachartre /* default disk label */
67585081Sachartre vd->vdisk_label = VD_DISK_LABEL_UNK;
67595081Sachartre
67602032Slm66018 /* Open vdisk and initialize parameters */
67613401Snarayan if ((status = vd_setup_vd(vd)) == 0) {
67623401Snarayan vd->initialized |= VD_DISK_READY;
67633401Snarayan
67643401Snarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR);
67656108Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u",
67663401Snarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"),
67676108Sachartre (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"),
67683401Snarayan vd->nslices);
67693401Snarayan } else {
67703401Snarayan if (status != EAGAIN)
67713401Snarayan return (status);
67723401Snarayan }
67731991Sheppo
67741991Sheppo /* Initialize locking */
67751991Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED,
67764696Sachartre &iblock) != DDI_SUCCESS) {
67771991Sheppo PRN("Could not get iblock cookie.");
67781991Sheppo return (EIO);
67791991Sheppo }
67801991Sheppo
67811991Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock);
67821991Sheppo vd->initialized |= VD_LOCKING;
67831991Sheppo
67841991Sheppo
67852336Snarayan /* Create start and completion task queues for the vdisk */
67862336Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id);
67871991Sheppo PR1("tq_name = %s", tq_name);
67882336Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1,
67894696Sachartre TASKQ_DEFAULTPRI, 0)) == NULL) {
67901991Sheppo PRN("Could not create task queue");
67911991Sheppo return (EIO);
67921991Sheppo }
67932336Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id);
67942336Snarayan PR1("tq_name = %s", tq_name);
67952336Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1,
67964696Sachartre TASKQ_DEFAULTPRI, 0)) == NULL) {
67972336Snarayan PRN("Could not create task queue");
67982336Snarayan return (EIO);
67992336Snarayan }
68006623Sachartre
68016623Sachartre /* Allocate the staging buffer */
68026623Sachartre vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */
68036623Sachartre vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP);
68046623Sachartre
68052336Snarayan vd->enabled = 1; /* before callback can dispatch to startq */
68061991Sheppo
68071991Sheppo
68081991Sheppo /* Bring up LDC */
68091991Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC;
68101991Sheppo ldc_attr.instance = ddi_get_instance(vds->dip);
68111991Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE;
68122410Slm66018 ldc_attr.mtu = VD_LDC_MTU;
68131991Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) {
68145365Slm66018 PRN("Could not initialize LDC channel %lx, "
68153782Sachartre "init failed with error %d", ldc_id, status);
68161991Sheppo return (status);
68171991Sheppo }
68181991Sheppo vd->initialized |= VD_LDC;
68191991Sheppo
68201991Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events,
68214696Sachartre (caddr_t)vd)) != 0) {
68223782Sachartre PRN("Could not initialize LDC channel %lu,"
68233782Sachartre "reg_callback failed with error %d", ldc_id, status);
68241991Sheppo return (status);
68251991Sheppo }
68261991Sheppo
68271991Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) {
68283782Sachartre PRN("Could not initialize LDC channel %lu,"
68293782Sachartre "open failed with error %d", ldc_id, status);
68301991Sheppo return (status);
68311991Sheppo }
68321991Sheppo
68332793Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) {
68343166Ssg70180 PR0("ldc_up() returned errno %d", status);
68352793Slm66018 }
68362793Slm66018
68372531Snarayan /* Allocate the inband task memory handle */
68382531Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl));
68392531Snarayan if (status) {
68403782Sachartre PRN("Could not initialize LDC channel %lu,"
68413782Sachartre "alloc_handle failed with error %d", ldc_id, status);
68422531Snarayan return (ENXIO);
68432531Snarayan }
68441991Sheppo
68451991Sheppo /* Add the successfully-initialized vdisk to the server's table */
68461991Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) {
68471991Sheppo PRN("Error adding vdisk ID %lu to table", id);
68481991Sheppo return (EIO);
68491991Sheppo }
68501991Sheppo
68512793Slm66018 /* store initial state */
68522793Slm66018 vd->state = VD_STATE_INIT;
68532793Slm66018
68541991Sheppo return (0);
68551991Sheppo }
68561991Sheppo
68572793Slm66018 static void
vd_free_dring_task(vd_t * vdp)68582793Slm66018 vd_free_dring_task(vd_t *vdp)
68592793Slm66018 {
68602793Slm66018 if (vdp->dring_task != NULL) {
68612793Slm66018 ASSERT(vdp->dring_len != 0);
68622793Slm66018 /* Free all dring_task memory handles */
68632793Slm66018 for (int i = 0; i < vdp->dring_len; i++) {
68642793Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl);
68657226Sha137994 kmem_free(vdp->dring_task[i].request,
68667226Sha137994 (vdp->descriptor_size -
68677226Sha137994 sizeof (vio_dring_entry_hdr_t)));
68687226Sha137994 vdp->dring_task[i].request = NULL;
68692793Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen);
68702793Slm66018 vdp->dring_task[i].msg = NULL;
68712793Slm66018 }
68722793Slm66018 kmem_free(vdp->dring_task,
68732793Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len);
68742793Slm66018 vdp->dring_task = NULL;
68752793Slm66018 }
68767791SAlexandre.Chartre@Sun.COM
68777791SAlexandre.Chartre@Sun.COM if (vdp->write_queue != NULL) {
68787791SAlexandre.Chartre@Sun.COM kmem_free(vdp->write_queue, sizeof (buf_t *) * vdp->dring_len);
68797791SAlexandre.Chartre@Sun.COM vdp->write_queue = NULL;
68807791SAlexandre.Chartre@Sun.COM }
68812793Slm66018 }
68822793Slm66018
68831991Sheppo /*
68841991Sheppo * Destroy the state associated with a virtual disk
68851991Sheppo */
68861991Sheppo static void
vds_destroy_vd(void * arg)68871991Sheppo vds_destroy_vd(void *arg)
68881991Sheppo {
68891991Sheppo vd_t *vd = (vd_t *)arg;
68903166Ssg70180 int retry = 0, rv;
68911991Sheppo
68921991Sheppo if (vd == NULL)
68931991Sheppo return;
68941991Sheppo
68952336Snarayan PR0("Destroying vdisk state");
68962336Snarayan
68971991Sheppo /* Disable queuing requests for the vdisk */
68981991Sheppo if (vd->initialized & VD_LOCKING) {
68991991Sheppo mutex_enter(&vd->lock);
69001991Sheppo vd->enabled = 0;
69011991Sheppo mutex_exit(&vd->lock);
69021991Sheppo }
69031991Sheppo
69047791SAlexandre.Chartre@Sun.COM /* Drain and destroy start queue (*before* destroying ioq) */
69052336Snarayan if (vd->startq != NULL)
69062336Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */
69072336Snarayan
69087791SAlexandre.Chartre@Sun.COM /* Drain and destroy the I/O queue (*before* destroying completionq) */
69097791SAlexandre.Chartre@Sun.COM if (vd->ioq != NULL)
69107791SAlexandre.Chartre@Sun.COM ddi_taskq_destroy(vd->ioq);
69117791SAlexandre.Chartre@Sun.COM
69122336Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */
69132336Snarayan if (vd->completionq != NULL)
69142336Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */
69152336Snarayan
69162793Slm66018 vd_free_dring_task(vd);
69172793Slm66018
69183166Ssg70180 /* Free the inband task memory handle */
69193166Ssg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl);
69203166Ssg70180
69213166Ssg70180 /* Shut down LDC */
69223166Ssg70180 if (vd->initialized & VD_LDC) {
69233166Ssg70180 /* unmap the dring */
69243166Ssg70180 if (vd->initialized & VD_DRING)
69253166Ssg70180 (void) ldc_mem_dring_unmap(vd->dring_handle);
69263166Ssg70180
69273166Ssg70180 /* close LDC channel - retry on EAGAIN */
69283166Ssg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) {
69293166Ssg70180 if (++retry > vds_ldc_retries) {
69303166Ssg70180 PR0("Timed out closing channel");
69313166Ssg70180 break;
69323166Ssg70180 }
69333166Ssg70180 drv_usecwait(vds_ldc_delay);
69343166Ssg70180 }
69353166Ssg70180 if (rv == 0) {
69363166Ssg70180 (void) ldc_unreg_callback(vd->ldc_handle);
69373166Ssg70180 (void) ldc_fini(vd->ldc_handle);
69383166Ssg70180 } else {
69393166Ssg70180 /*
69403166Ssg70180 * Closing the LDC channel has failed. Ideally we should
69413166Ssg70180 * fail here but there is no Zeus level infrastructure
69423166Ssg70180 * to handle this. The MD has already been changed and
69433166Ssg70180 * we have to do the close. So we try to do as much
69443166Ssg70180 * clean up as we can.
69453166Ssg70180 */
69463166Ssg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE);
69473166Ssg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN)
69483166Ssg70180 drv_usecwait(vds_ldc_delay);
69493166Ssg70180 }
69503166Ssg70180 }
69513166Ssg70180
69522793Slm66018 /* Free the staging buffer for msgs */
69532793Slm66018 if (vd->vio_msgp != NULL) {
69542793Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen);
69552793Slm66018 vd->vio_msgp = NULL;
69562793Slm66018 }
69572793Slm66018
69582793Slm66018 /* Free the inband message buffer */
69592793Slm66018 if (vd->inband_task.msg != NULL) {
69602793Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen);
69612793Slm66018 vd->inband_task.msg = NULL;
69622336Snarayan }
69635331Samw
69643782Sachartre if (vd->file) {
69653782Sachartre /* Close file */
69665081Sachartre (void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1,
69675331Samw 0, kcred, NULL);
69683782Sachartre VN_RELE(vd->file_vnode);
69693782Sachartre } else {
69703782Sachartre /* Close any open backing-device slices */
69716836Sachartre for (uint_t slice = 0; slice < V_NUMPAR; slice++) {
69723782Sachartre if (vd->ldi_handle[slice] != NULL) {
69733782Sachartre PR0("Closing slice %u", slice);
69743782Sachartre (void) ldi_close(vd->ldi_handle[slice],
69755081Sachartre vd->open_flags, kcred);
69763401Snarayan }
69771991Sheppo }
69781991Sheppo }
69791991Sheppo
69807597SAlexandre.Chartre@Sun.COM /* Free disk image devid */
69817597SAlexandre.Chartre@Sun.COM if (vd->dskimg_devid != NULL)
69827597SAlexandre.Chartre@Sun.COM ddi_devid_free(vd->dskimg_devid);
69837597SAlexandre.Chartre@Sun.COM
69846836Sachartre /* Free any fake label */
69856836Sachartre if (vd->flabel) {
69866836Sachartre kmem_free(vd->flabel, vd->flabel_size);
69876836Sachartre vd->flabel = NULL;
69886836Sachartre vd->flabel_size = 0;
69896836Sachartre }
69906836Sachartre
69911991Sheppo /* Free lock */
69921991Sheppo if (vd->initialized & VD_LOCKING)
69931991Sheppo mutex_destroy(&vd->lock);
69941991Sheppo
69951991Sheppo /* Finally, free the vdisk structure itself */
69961991Sheppo kmem_free(vd, sizeof (*vd));
69971991Sheppo }
69981991Sheppo
69991991Sheppo static int
vds_init_vd(vds_t * vds,uint64_t id,char * device_path,uint64_t options,uint64_t ldc_id)70005081Sachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options,
70015081Sachartre uint64_t ldc_id)
70021991Sheppo {
70031991Sheppo int status;
70041991Sheppo vd_t *vd = NULL;
70051991Sheppo
70061991Sheppo
70075081Sachartre if ((status = vds_do_init_vd(vds, id, device_path, options,
70085081Sachartre ldc_id, &vd)) != 0)
70091991Sheppo vds_destroy_vd(vd);
70101991Sheppo
70111991Sheppo return (status);
70121991Sheppo }
70131991Sheppo
70141991Sheppo static int
vds_do_get_ldc_id(md_t * md,mde_cookie_t vd_node,mde_cookie_t * channel,uint64_t * ldc_id)70151991Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel,
70161991Sheppo uint64_t *ldc_id)
70171991Sheppo {
70181991Sheppo int num_channels;
70191991Sheppo
70201991Sheppo
70211991Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */
70221991Sheppo if ((num_channels = md_scan_dag(md, vd_node,
70234696Sachartre md_find_name(md, VD_CHANNEL_ENDPOINT),
70244696Sachartre md_find_name(md, "fwd"), channel)) <= 0) {
70251991Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT);
70261991Sheppo return (-1);
70271991Sheppo }
70281991Sheppo
70291991Sheppo /* Get the "id" value for the first channel endpoint node */
70301991Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) {
70311991Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk",
70321991Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT);
70331991Sheppo return (-1);
70341991Sheppo }
70351991Sheppo
70361991Sheppo if (num_channels > 1) {
70371991Sheppo PRN("Using ID of first of multiple channels for this vdisk");
70381991Sheppo }
70391991Sheppo
70401991Sheppo return (0);
70411991Sheppo }
70421991Sheppo
70431991Sheppo static int
vds_get_ldc_id(md_t * md,mde_cookie_t vd_node,uint64_t * ldc_id)70441991Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id)
70451991Sheppo {
70461991Sheppo int num_nodes, status;
70471991Sheppo size_t size;
70481991Sheppo mde_cookie_t *channel;
70491991Sheppo
70501991Sheppo
70511991Sheppo if ((num_nodes = md_node_count(md)) <= 0) {
70521991Sheppo PRN("Invalid node count in Machine Description subtree");
70531991Sheppo return (-1);
70541991Sheppo }
70551991Sheppo size = num_nodes*(sizeof (*channel));
70561991Sheppo channel = kmem_zalloc(size, KM_SLEEP);
70571991Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id);
70581991Sheppo kmem_free(channel, size);
70591991Sheppo
70601991Sheppo return (status);
70611991Sheppo }
70621991Sheppo
70635081Sachartre /*
70645081Sachartre * Function:
70655081Sachartre * vds_get_options
70665081Sachartre *
70675081Sachartre * Description:
70685081Sachartre * Parse the options of a vds node. Options are defined as an array
70695081Sachartre * of strings in the vds-block-device-opts property of the vds node
70705081Sachartre * in the machine description. Options are returned as a bitmask. The
70715081Sachartre * mapping between the bitmask options and the options strings from the
70725081Sachartre * machine description is defined in the vd_bdev_options[] array.
70735081Sachartre *
70745081Sachartre * The vds-block-device-opts property is optional. If a vds has no such
70755081Sachartre * property then no option is defined.
70765081Sachartre *
70775081Sachartre * Parameters:
70785081Sachartre * md - machine description.
70795081Sachartre * vd_node - vds node in the machine description for which
70805081Sachartre * options have to be parsed.
70815081Sachartre * options - the returned options.
70825081Sachartre *
70835081Sachartre * Return Code:
70845081Sachartre * none.
70855081Sachartre */
70865081Sachartre static void
vds_get_options(md_t * md,mde_cookie_t vd_node,uint64_t * options)70875081Sachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options)
70885081Sachartre {
70895081Sachartre char *optstr, *opt;
70905081Sachartre int len, n, i;
70915081Sachartre
70925081Sachartre *options = 0;
70935081Sachartre
70945081Sachartre if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS,
70955081Sachartre (uint8_t **)&optstr, &len) != 0) {
70965081Sachartre PR0("No options found");
70975081Sachartre return;
70985081Sachartre }
70995081Sachartre
71005081Sachartre /* parse options */
71015081Sachartre opt = optstr;
71025081Sachartre n = sizeof (vd_bdev_options) / sizeof (vd_option_t);
71035081Sachartre
71045081Sachartre while (opt < optstr + len) {
71055081Sachartre for (i = 0; i < n; i++) {
71065081Sachartre if (strncmp(vd_bdev_options[i].vdo_name,
71075081Sachartre opt, VD_OPTION_NLEN) == 0) {
71085081Sachartre *options |= vd_bdev_options[i].vdo_value;
71095081Sachartre break;
71105081Sachartre }
71115081Sachartre }
71125081Sachartre
71135081Sachartre if (i < n) {
71145081Sachartre PR0("option: %s", opt);
71155081Sachartre } else {
71165081Sachartre PRN("option %s is unknown or unsupported", opt);
71175081Sachartre }
71185081Sachartre
71195081Sachartre opt += strlen(opt) + 1;
71205081Sachartre }
71215081Sachartre }
71225081Sachartre
71231991Sheppo static void
vds_driver_types_free(vds_t * vds)71246108Sachartre vds_driver_types_free(vds_t *vds)
71256108Sachartre {
71266108Sachartre if (vds->driver_types != NULL) {
71276108Sachartre kmem_free(vds->driver_types, sizeof (vd_driver_type_t) *
71286108Sachartre vds->num_drivers);
71296108Sachartre vds->driver_types = NULL;
71306108Sachartre vds->num_drivers = 0;
71316108Sachartre }
71326108Sachartre }
71336108Sachartre
71346108Sachartre /*
71356108Sachartre * Update the driver type list with information from vds.conf.
71366108Sachartre */
71376108Sachartre static void
vds_driver_types_update(vds_t * vds)71386108Sachartre vds_driver_types_update(vds_t *vds)
71396108Sachartre {
71406108Sachartre char **list, *s;
71416108Sachartre uint_t i, num, count = 0, len;
71426108Sachartre
71436108Sachartre if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip,
71446108Sachartre DDI_PROP_DONTPASS, "driver-type-list", &list, &num) !=
71456108Sachartre DDI_PROP_SUCCESS)
71466108Sachartre return;
71476108Sachartre
71486108Sachartre /*
71496108Sachartre * We create a driver_types list with as many as entries as there
71506108Sachartre * is in the driver-type-list from vds.conf. However only valid
71516108Sachartre * entries will be populated (i.e. entries from driver-type-list
71526108Sachartre * with a valid syntax). Invalid entries will be left blank so
71536108Sachartre * they will have no driver name and the driver type will be
71546108Sachartre * VD_DRIVER_UNKNOWN (= 0).
71556108Sachartre */
71566108Sachartre vds->num_drivers = num;
71576108Sachartre vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num,
71586108Sachartre KM_SLEEP);
71596108Sachartre
71606108Sachartre for (i = 0; i < num; i++) {
71616108Sachartre
71626108Sachartre s = strchr(list[i], ':');
71636108Sachartre
71646108Sachartre if (s == NULL) {
71656108Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): "
71666108Sachartre "a colon is expected in the entry",
71676108Sachartre i, list[i]);
71686108Sachartre continue;
71696108Sachartre }
71706108Sachartre
71716108Sachartre len = (uintptr_t)s - (uintptr_t)list[i];
71726108Sachartre
71736108Sachartre if (len == 0) {
71746108Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): "
71756108Sachartre "the driver name is empty",
71766108Sachartre i, list[i]);
71776108Sachartre continue;
71786108Sachartre }
71796108Sachartre
71806108Sachartre if (len >= VD_DRIVER_NAME_LEN) {
71816108Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): "
71826108Sachartre "the driver name is too long",
71836108Sachartre i, list[i]);
71846108Sachartre continue;
71856108Sachartre }
71866108Sachartre
71876108Sachartre if (strcmp(s + 1, "disk") == 0) {
71886108Sachartre
71896108Sachartre vds->driver_types[i].type = VD_DRIVER_DISK;
71906108Sachartre
71916108Sachartre } else if (strcmp(s + 1, "volume") == 0) {
71926108Sachartre
71936108Sachartre vds->driver_types[i].type = VD_DRIVER_VOLUME;
71946108Sachartre
71956108Sachartre } else {
71966108Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): "
71976108Sachartre "the driver type is invalid",
71986108Sachartre i, list[i]);
71996108Sachartre continue;
72006108Sachartre }
72016108Sachartre
72026108Sachartre (void) strncpy(vds->driver_types[i].name, list[i], len);
72036108Sachartre
72046108Sachartre PR0("driver-type-list, entry %d (%s) added",
72056108Sachartre i, list[i]);
72066108Sachartre
72076108Sachartre count++;
72086108Sachartre }
72096108Sachartre
72106108Sachartre ddi_prop_free(list);
72116108Sachartre
72126108Sachartre if (count == 0) {
72136108Sachartre /* nothing was added, clean up */
72146108Sachartre vds_driver_types_free(vds);
72156108Sachartre }
72166108Sachartre }
72176108Sachartre
72186108Sachartre static void
vds_add_vd(vds_t * vds,md_t * md,mde_cookie_t vd_node)72191991Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
72201991Sheppo {
72212410Slm66018 char *device_path = NULL;
72225081Sachartre uint64_t id = 0, ldc_id = 0, options = 0;
72231991Sheppo
72241991Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
72251991Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP);
72261991Sheppo return;
72271991Sheppo }
72281991Sheppo PR0("Adding vdisk ID %lu", id);
72291991Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP,
72304696Sachartre &device_path) != 0) {
72311991Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
72321991Sheppo return;
72331991Sheppo }
72341991Sheppo
72355081Sachartre vds_get_options(md, vd_node, &options);
72365081Sachartre
72371991Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) {
72381991Sheppo PRN("Error getting LDC ID for vdisk %lu", id);
72391991Sheppo return;
72401991Sheppo }
72411991Sheppo
72425081Sachartre if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) {
72431991Sheppo PRN("Failed to add vdisk ID %lu", id);
72445365Slm66018 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
72455365Slm66018 PRN("No vDisk entry found for vdisk ID %lu", id);
72461991Sheppo return;
72471991Sheppo }
72481991Sheppo }
72491991Sheppo
72501991Sheppo static void
vds_remove_vd(vds_t * vds,md_t * md,mde_cookie_t vd_node)72511991Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node)
72521991Sheppo {
72531991Sheppo uint64_t id = 0;
72541991Sheppo
72551991Sheppo
72561991Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) {
72571991Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node",
72581991Sheppo VD_ID_PROP);
72591991Sheppo return;
72601991Sheppo }
72611991Sheppo PR0("Removing vdisk ID %lu", id);
72621991Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0)
72631991Sheppo PRN("No vdisk entry found for vdisk ID %lu", id);
72641991Sheppo }
72651991Sheppo
72661991Sheppo static void
vds_change_vd(vds_t * vds,md_t * prev_md,mde_cookie_t prev_vd_node,md_t * curr_md,mde_cookie_t curr_vd_node)72671991Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node,
72681991Sheppo md_t *curr_md, mde_cookie_t curr_vd_node)
72691991Sheppo {
72701991Sheppo char *curr_dev, *prev_dev;
72715081Sachartre uint64_t curr_id = 0, curr_ldc_id = 0, curr_options = 0;
72725081Sachartre uint64_t prev_id = 0, prev_ldc_id = 0, prev_options = 0;
72731991Sheppo size_t len;
72741991Sheppo
72751991Sheppo
72761991Sheppo /* Validate that vdisk ID has not changed */
72771991Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) {
72781991Sheppo PRN("Error getting previous vdisk \"%s\" property",
72791991Sheppo VD_ID_PROP);
72801991Sheppo return;
72811991Sheppo }
72821991Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) {
72831991Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP);
72841991Sheppo return;
72851991Sheppo }
72861991Sheppo if (curr_id != prev_id) {
72871991Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu",
72881991Sheppo prev_id, curr_id);
72891991Sheppo return;
72901991Sheppo }
72911991Sheppo
72921991Sheppo /* Validate that LDC ID has not changed */
72931991Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) {
72941991Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id);
72951991Sheppo return;
72961991Sheppo }
72971991Sheppo
72981991Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) {
72991991Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id);
73001991Sheppo return;
73011991Sheppo }
73021991Sheppo if (curr_ldc_id != prev_ldc_id) {
73032032Slm66018 _NOTE(NOTREACHED); /* lint is confused */
73041991Sheppo PRN("Not changing vdisk: "
73051991Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id);
73061991Sheppo return;
73071991Sheppo }
73081991Sheppo
73091991Sheppo /* Determine whether device path has changed */
73101991Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP,
73114696Sachartre &prev_dev) != 0) {
73121991Sheppo PRN("Error getting previous vdisk \"%s\"",
73131991Sheppo VD_BLOCK_DEVICE_PROP);
73141991Sheppo return;
73151991Sheppo }
73161991Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP,
73174696Sachartre &curr_dev) != 0) {
73181991Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP);
73191991Sheppo return;
73201991Sheppo }
73211991Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) &&
73221991Sheppo (strncmp(curr_dev, prev_dev, len) == 0))
73231991Sheppo return; /* no relevant (supported) change */
73241991Sheppo
73255081Sachartre /* Validate that options have not changed */
73265081Sachartre vds_get_options(prev_md, prev_vd_node, &prev_options);
73275081Sachartre vds_get_options(curr_md, curr_vd_node, &curr_options);
73285081Sachartre if (prev_options != curr_options) {
73295081Sachartre PRN("Not changing vdisk: options changed from %lx to %lx",
73305081Sachartre prev_options, curr_options);
73315081Sachartre return;
73325081Sachartre }
73335081Sachartre
73341991Sheppo PR0("Changing vdisk ID %lu", prev_id);
73352793Slm66018
73361991Sheppo /* Remove old state, which will close vdisk and reset */
73371991Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0)
73381991Sheppo PRN("No entry found for vdisk ID %lu", prev_id);
73392793Slm66018
73401991Sheppo /* Re-initialize vdisk with new state */
73415081Sachartre if (vds_init_vd(vds, curr_id, curr_dev, curr_options,
73425081Sachartre curr_ldc_id) != 0) {
73431991Sheppo PRN("Failed to change vdisk ID %lu", curr_id);
73441991Sheppo return;
73451991Sheppo }
73461991Sheppo }
73471991Sheppo
73481991Sheppo static int
vds_process_md(void * arg,mdeg_result_t * md)73491991Sheppo vds_process_md(void *arg, mdeg_result_t *md)
73501991Sheppo {
73511991Sheppo int i;
73521991Sheppo vds_t *vds = arg;
73531991Sheppo
73541991Sheppo
73551991Sheppo if (md == NULL)
73561991Sheppo return (MDEG_FAILURE);
73571991Sheppo ASSERT(vds != NULL);
73581991Sheppo
73591991Sheppo for (i = 0; i < md->removed.nelem; i++)
73601991Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]);
73611991Sheppo for (i = 0; i < md->match_curr.nelem; i++)
73621991Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i],
73631991Sheppo md->match_curr.mdp, md->match_curr.mdep[i]);
73641991Sheppo for (i = 0; i < md->added.nelem; i++)
73651991Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]);
73661991Sheppo
73671991Sheppo return (MDEG_SUCCESS);
73681991Sheppo }
73691991Sheppo
73703401Snarayan
73711991Sheppo static int
vds_do_attach(dev_info_t * dip)73721991Sheppo vds_do_attach(dev_info_t *dip)
73731991Sheppo {
73743297Ssb155480 int status, sz;
73753297Ssb155480 int cfg_handle;
73761991Sheppo minor_t instance = ddi_get_instance(dip);
73771991Sheppo vds_t *vds;
73783297Ssb155480 mdeg_prop_spec_t *pspecp;
73793297Ssb155480 mdeg_node_spec_t *ispecp;
73801991Sheppo
73811991Sheppo /*
73821991Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's
73831991Sheppo * notion of "instance", or unique identifier, for that node; OBP
73841991Sheppo * stores the value of the "cfg-handle" MD property as the value of
73851991Sheppo * the "reg" property on the node in the device tree it builds from
73861991Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's
73871991Sheppo * "reg" property value to uniquely identify this device instance when
73881991Sheppo * registering with the MD event-generation framework. If the "reg"
73891991Sheppo * property cannot be found, the device tree state is presumably so
73901991Sheppo * broken that there is no point in continuing.
73911991Sheppo */
73923297Ssb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
73934696Sachartre VD_REG_PROP)) {
73943297Ssb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP);
73951991Sheppo return (DDI_FAILURE);
73961991Sheppo }
73971991Sheppo
73981991Sheppo /* Get the MD instance for later MDEG registration */
73991991Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS,
74003297Ssb155480 VD_REG_PROP, -1);
74011991Sheppo
74021991Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) {
74031991Sheppo PRN("Could not allocate state for instance %u", instance);
74041991Sheppo return (DDI_FAILURE);
74051991Sheppo }
74061991Sheppo
74071991Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) {
74081991Sheppo PRN("Could not get state for instance %u", instance);
74091991Sheppo ddi_soft_state_free(vds_state, instance);
74101991Sheppo return (DDI_FAILURE);
74111991Sheppo }
74121991Sheppo
74131991Sheppo vds->dip = dip;
74141991Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS,
74154696Sachartre vds_destroy_vd, sizeof (void *));
74164696Sachartre
74171991Sheppo ASSERT(vds->vd_table != NULL);
74181991Sheppo
74191991Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) {
74201991Sheppo PRN("ldi_ident_from_dip() returned errno %d", status);
74211991Sheppo return (DDI_FAILURE);
74221991Sheppo }
74231991Sheppo vds->initialized |= VDS_LDI;
74241991Sheppo
74251991Sheppo /* Register for MD updates */
74263297Ssb155480 sz = sizeof (vds_prop_template);
74273297Ssb155480 pspecp = kmem_alloc(sz, KM_SLEEP);
74283297Ssb155480 bcopy(vds_prop_template, pspecp, sz);
74293297Ssb155480
74303297Ssb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle);
74313297Ssb155480
74323297Ssb155480 /* initialize the complete prop spec structure */
74333297Ssb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP);
74343297Ssb155480 ispecp->namep = "virtual-device";
74353297Ssb155480 ispecp->specp = pspecp;
74363297Ssb155480
74373297Ssb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds,
74384696Sachartre &vds->mdeg) != MDEG_SUCCESS) {
74391991Sheppo PRN("Unable to register for MD updates");
74403297Ssb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t));
74413297Ssb155480 kmem_free(pspecp, sz);
74421991Sheppo return (DDI_FAILURE);
74431991Sheppo }
74443297Ssb155480
74453297Ssb155480 vds->ispecp = ispecp;
74461991Sheppo vds->initialized |= VDS_MDEG;
74471991Sheppo
74482032Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */
74492032Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) !=
74502032Slm66018 DDI_PROP_SUCCESS) {
74512032Slm66018 PRN("failed to set \"%s\" property for instance %u",
74522032Slm66018 DDI_NO_AUTODETACH, instance);
74532032Slm66018 }
74542032Slm66018
74556108Sachartre /* read any user defined driver types from conf file and update list */
74566108Sachartre vds_driver_types_update(vds);
74576108Sachartre
74581991Sheppo ddi_report_dev(dip);
74591991Sheppo return (DDI_SUCCESS);
74601991Sheppo }
74611991Sheppo
74621991Sheppo static int
vds_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)74631991Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
74641991Sheppo {
74651991Sheppo int status;
74661991Sheppo
74671991Sheppo switch (cmd) {
74681991Sheppo case DDI_ATTACH:
74692336Snarayan PR0("Attaching");
74701991Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS)
74711991Sheppo (void) vds_detach(dip, DDI_DETACH);
74721991Sheppo return (status);
74731991Sheppo case DDI_RESUME:
74742336Snarayan PR0("No action required for DDI_RESUME");
74751991Sheppo return (DDI_SUCCESS);
74761991Sheppo default:
74771991Sheppo return (DDI_FAILURE);
74781991Sheppo }
74791991Sheppo }
74801991Sheppo
74811991Sheppo static struct dev_ops vds_ops = {
74821991Sheppo DEVO_REV, /* devo_rev */
74831991Sheppo 0, /* devo_refcnt */
74841991Sheppo ddi_no_info, /* devo_getinfo */
74851991Sheppo nulldev, /* devo_identify */
74861991Sheppo nulldev, /* devo_probe */
74871991Sheppo vds_attach, /* devo_attach */
74881991Sheppo vds_detach, /* devo_detach */
74891991Sheppo nodev, /* devo_reset */
74901991Sheppo NULL, /* devo_cb_ops */
74911991Sheppo NULL, /* devo_bus_ops */
74927656SSherry.Moore@Sun.COM nulldev, /* devo_power */
74937656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* devo_quiesce */
74941991Sheppo };
74951991Sheppo
74961991Sheppo static struct modldrv modldrv = {
74971991Sheppo &mod_driverops,
74984838Slm66018 "virtual disk server",
74991991Sheppo &vds_ops,
75001991Sheppo };
75011991Sheppo
75021991Sheppo static struct modlinkage modlinkage = {
75031991Sheppo MODREV_1,
75041991Sheppo &modldrv,
75051991Sheppo NULL
75061991Sheppo };
75071991Sheppo
75081991Sheppo
75091991Sheppo int
_init(void)75101991Sheppo _init(void)
75111991Sheppo {
75125365Slm66018 int status;
75132336Snarayan
75141991Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0)
75151991Sheppo return (status);
75165365Slm66018
75171991Sheppo if ((status = mod_install(&modlinkage)) != 0) {
75181991Sheppo ddi_soft_state_fini(&vds_state);
75191991Sheppo return (status);
75201991Sheppo }
75211991Sheppo
75221991Sheppo return (0);
75231991Sheppo }
75241991Sheppo
75251991Sheppo int
_info(struct modinfo * modinfop)75261991Sheppo _info(struct modinfo *modinfop)
75271991Sheppo {
75281991Sheppo return (mod_info(&modlinkage, modinfop));
75291991Sheppo }
75301991Sheppo
75311991Sheppo int
_fini(void)75321991Sheppo _fini(void)
75331991Sheppo {
75341991Sheppo int status;
75351991Sheppo
75361991Sheppo if ((status = mod_remove(&modlinkage)) != 0)
75371991Sheppo return (status);
75381991Sheppo ddi_soft_state_fini(&vds_state);
75391991Sheppo return (0);
75401991Sheppo }
7541