11ae08745Sheppo /* 21ae08745Sheppo * CDDL HEADER START 31ae08745Sheppo * 41ae08745Sheppo * The contents of this file are subject to the terms of the 51ae08745Sheppo * Common Development and Distribution License (the "License"). 61ae08745Sheppo * You may not use this file except in compliance with the License. 71ae08745Sheppo * 81ae08745Sheppo * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 91ae08745Sheppo * or http://www.opensolaris.org/os/licensing. 101ae08745Sheppo * See the License for the specific language governing permissions 111ae08745Sheppo * and limitations under the License. 121ae08745Sheppo * 131ae08745Sheppo * When distributing Covered Code, include this CDDL HEADER in each 141ae08745Sheppo * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 151ae08745Sheppo * If applicable, add the following below this CDDL HEADER, with the 161ae08745Sheppo * fields enclosed by brackets "[]" replaced with your own identifying 171ae08745Sheppo * information: Portions Copyright [yyyy] [name of copyright owner] 181ae08745Sheppo * 191ae08745Sheppo * CDDL HEADER END 201ae08745Sheppo */ 211ae08745Sheppo 221ae08745Sheppo /* 23d84f0041SAlexandre Chartre * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 241ae08745Sheppo * Use is subject to license terms. 251ae08745Sheppo */ 261ae08745Sheppo 271ae08745Sheppo /* 281ae08745Sheppo * Virtual disk server 291ae08745Sheppo */ 301ae08745Sheppo 311ae08745Sheppo 321ae08745Sheppo #include <sys/types.h> 331ae08745Sheppo #include <sys/conf.h> 344bac2208Snarayan #include <sys/crc32.h> 351ae08745Sheppo #include <sys/ddi.h> 361ae08745Sheppo #include <sys/dkio.h> 371ae08745Sheppo #include <sys/file.h> 3817cadca8Slm66018 #include <sys/fs/hsfs_isospec.h> 391ae08745Sheppo #include <sys/mdeg.h> 402f5224aeSachartre #include <sys/mhd.h> 411ae08745Sheppo #include <sys/modhash.h> 421ae08745Sheppo #include <sys/note.h> 431ae08745Sheppo #include <sys/pathname.h> 44205eeb1aSlm66018 #include <sys/sdt.h> 451ae08745Sheppo #include <sys/sunddi.h> 461ae08745Sheppo #include <sys/sunldi.h> 471ae08745Sheppo #include <sys/sysmacros.h> 481ae08745Sheppo #include <sys/vio_common.h> 4917cadca8Slm66018 #include <sys/vio_util.h> 501ae08745Sheppo #include <sys/vdsk_mailbox.h> 511ae08745Sheppo #include <sys/vdsk_common.h> 521ae08745Sheppo #include <sys/vtoc.h> 533c96341aSnarayan #include <sys/vfs.h> 543c96341aSnarayan #include <sys/stat.h> 5587a7269eSachartre #include <sys/scsi/impl/uscsi.h> 56bbfa0259Sha137994 #include <sys/ontrap.h> 57690555a1Sachartre #include <vm/seg_map.h> 581ae08745Sheppo 59342440ecSPrasad Singamsetty #define ONE_MEGABYTE (1ULL << 20) 60342440ecSPrasad Singamsetty #define ONE_GIGABYTE (1ULL << 30) 61bae9e67eSachartre #define ONE_TERABYTE (1ULL << 40) 62bae9e67eSachartre 631ae08745Sheppo /* Virtual disk server initialization flags */ 64d10e4ef2Snarayan #define VDS_LDI 0x01 65d10e4ef2Snarayan #define VDS_MDEG 0x02 661ae08745Sheppo 671ae08745Sheppo /* Virtual disk server tunable parameters */ 683c96341aSnarayan #define VDS_RETRIES 5 693c96341aSnarayan #define VDS_LDC_DELAY 1000 /* 1 msecs */ 703c96341aSnarayan #define VDS_DEV_DELAY 10000000 /* 10 secs */ 711ae08745Sheppo #define VDS_NCHAINS 32 721ae08745Sheppo 731ae08745Sheppo /* Identification parameters for MD, synthetic dkio(7i) structures, etc. */ 741ae08745Sheppo #define VDS_NAME "virtual-disk-server" 751ae08745Sheppo 761ae08745Sheppo #define VD_NAME "vd" 771ae08745Sheppo #define VD_VOLUME_NAME "vdisk" 781ae08745Sheppo #define VD_ASCIILABEL "Virtual Disk" 791ae08745Sheppo 801ae08745Sheppo #define VD_CHANNEL_ENDPOINT "channel-endpoint" 811ae08745Sheppo #define VD_ID_PROP "id" 821ae08745Sheppo #define VD_BLOCK_DEVICE_PROP "vds-block-device" 83047ba61eSachartre #define VD_BLOCK_DEVICE_OPTS "vds-block-device-opts" 84445b4c2eSsb155480 #define VD_REG_PROP "reg" 851ae08745Sheppo 861ae08745Sheppo /* Virtual disk initialization flags */ 873c96341aSnarayan #define VD_DISK_READY 0x01 883c96341aSnarayan #define VD_LOCKING 0x02 893c96341aSnarayan #define VD_LDC 0x04 903c96341aSnarayan #define VD_DRING 0x08 913c96341aSnarayan #define VD_SID 0x10 923c96341aSnarayan #define VD_SEQ_NUM 0x20 93047ba61eSachartre #define VD_SETUP_ERROR 0x40 941ae08745Sheppo 9587a7269eSachartre /* Number of backup labels */ 961aff8f07SAlexandre Chartre #define VD_DSKIMG_NUM_BACKUP 5 9787a7269eSachartre 9887a7269eSachartre /* Timeout for SCSI I/O */ 9987a7269eSachartre #define VD_SCSI_RDWR_TIMEOUT 30 /* 30 secs */ 10087a7269eSachartre 10183990c4aSAlexandre Chartre /* 10283990c4aSAlexandre Chartre * Default number of threads for the I/O queue. In many cases, we will not 10383990c4aSAlexandre Chartre * receive more than 8 I/O requests at the same time. However there are 10483990c4aSAlexandre Chartre * cases (for example during the OS installation) where we can have a lot 10583990c4aSAlexandre Chartre * more (up to the limit of the DRing size). 10683990c4aSAlexandre Chartre */ 10783990c4aSAlexandre Chartre #define VD_IOQ_NTHREADS 8 10883990c4aSAlexandre Chartre 109edcc0754Sachartre /* Maximum number of logical partitions */ 110edcc0754Sachartre #define VD_MAXPART (NDKMAP + 1) 111edcc0754Sachartre 1121ae08745Sheppo /* 1131ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 1141ae08745Sheppo * unfortunately, this convention does not appear to be codified. 1151ae08745Sheppo */ 1161ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 1171ae08745Sheppo 118bae9e67eSachartre /* Logical block address for EFI */ 119bae9e67eSachartre #define VD_EFI_LBA_GPT 1 /* LBA of the GPT */ 120bae9e67eSachartre #define VD_EFI_LBA_GPE 2 /* LBA of the GPE */ 121bae9e67eSachartre 12283990c4aSAlexandre Chartre /* 12383990c4aSAlexandre Chartre * Flags defining the behavior for flushing asynchronous writes used to 12483990c4aSAlexandre Chartre * performed some write I/O requests. 12583990c4aSAlexandre Chartre * 12683990c4aSAlexandre Chartre * The VD_AWFLUSH_IMMEDIATE enables immediate flushing of asynchronous 12783990c4aSAlexandre Chartre * writes. This ensures that data are committed to the backend when the I/O 12883990c4aSAlexandre Chartre * request reply is sent to the guest domain so this prevents any data to 12983990c4aSAlexandre Chartre * be lost in case a service domain unexpectedly crashes. 13083990c4aSAlexandre Chartre * 13183990c4aSAlexandre Chartre * The flag VD_AWFLUSH_DEFER indicates that flushing is deferred to another 13283990c4aSAlexandre Chartre * thread while the request is immediatly marked as completed. In that case, 13383990c4aSAlexandre Chartre * a guest domain can a receive a reply that its write request is completed 13483990c4aSAlexandre Chartre * while data haven't been flushed to disk yet. 13583990c4aSAlexandre Chartre * 13683990c4aSAlexandre Chartre * Flags VD_AWFLUSH_IMMEDIATE and VD_AWFLUSH_DEFER are mutually exclusive. 13783990c4aSAlexandre Chartre */ 13883990c4aSAlexandre Chartre #define VD_AWFLUSH_IMMEDIATE 0x01 /* immediate flushing */ 13983990c4aSAlexandre Chartre #define VD_AWFLUSH_DEFER 0x02 /* defer flushing */ 14083990c4aSAlexandre Chartre #define VD_AWFLUSH_GROUP 0x04 /* group requests before flushing */ 14183990c4aSAlexandre Chartre 1428fce2fd6Sachartre /* Driver types */ 1438fce2fd6Sachartre typedef enum vd_driver { 1448fce2fd6Sachartre VD_DRIVER_UNKNOWN = 0, /* driver type unknown */ 1458fce2fd6Sachartre VD_DRIVER_DISK, /* disk driver */ 1468fce2fd6Sachartre VD_DRIVER_VOLUME /* volume driver */ 1478fce2fd6Sachartre } vd_driver_t; 1488fce2fd6Sachartre 1498fce2fd6Sachartre #define VD_DRIVER_NAME_LEN 64 1508fce2fd6Sachartre 1518fce2fd6Sachartre #define VDS_NUM_DRIVERS (sizeof (vds_driver_types) / sizeof (vd_driver_type_t)) 1528fce2fd6Sachartre 1538fce2fd6Sachartre typedef struct vd_driver_type { 1548fce2fd6Sachartre char name[VD_DRIVER_NAME_LEN]; /* driver name */ 1558fce2fd6Sachartre vd_driver_t type; /* driver type (disk or volume) */ 1568fce2fd6Sachartre } vd_driver_type_t; 1578fce2fd6Sachartre 1588fce2fd6Sachartre /* 1598fce2fd6Sachartre * There is no reliable way to determine if a device is representing a disk 1608fce2fd6Sachartre * or a volume, especially with pseudo devices. So we maintain a list of well 1618fce2fd6Sachartre * known drivers and the type of device they represent (either a disk or a 1628fce2fd6Sachartre * volume). 1638fce2fd6Sachartre * 1648fce2fd6Sachartre * The list can be extended by adding a "driver-type-list" entry in vds.conf 1658fce2fd6Sachartre * with the following syntax: 1668fce2fd6Sachartre * 1678fce2fd6Sachartre * driver-type-list="<driver>:<type>", ... ,"<driver>:<type>"; 1688fce2fd6Sachartre * 1698fce2fd6Sachartre * Where: 1708fce2fd6Sachartre * <driver> is the name of a driver (limited to 64 characters) 1718fce2fd6Sachartre * <type> is either the string "disk" or "volume" 1728fce2fd6Sachartre * 1738fce2fd6Sachartre * Invalid entries in "driver-type-list" will be ignored. 1748fce2fd6Sachartre * 1758fce2fd6Sachartre * For example, the following line in vds.conf: 1768fce2fd6Sachartre * 1778fce2fd6Sachartre * driver-type-list="foo:disk","bar:volume"; 1788fce2fd6Sachartre * 1798fce2fd6Sachartre * defines that "foo" is a disk driver, and driver "bar" is a volume driver. 1808fce2fd6Sachartre * 1818fce2fd6Sachartre * When a list is defined in vds.conf, it is checked before the built-in list 1828fce2fd6Sachartre * (vds_driver_types[]) so that any definition from this list can be overriden 1838fce2fd6Sachartre * using vds.conf. 1848fce2fd6Sachartre */ 1858fce2fd6Sachartre vd_driver_type_t vds_driver_types[] = { 1868fce2fd6Sachartre { "dad", VD_DRIVER_DISK }, /* Solaris */ 1878fce2fd6Sachartre { "did", VD_DRIVER_DISK }, /* Sun Cluster */ 18811f54b6eSAlexandre Chartre { "dlmfdrv", VD_DRIVER_DISK }, /* Hitachi HDLM */ 1895b98b509Sachartre { "emcp", VD_DRIVER_DISK }, /* EMC Powerpath */ 1908fce2fd6Sachartre { "lofi", VD_DRIVER_VOLUME }, /* Solaris */ 1918fce2fd6Sachartre { "md", VD_DRIVER_VOLUME }, /* Solaris - SVM */ 1928fce2fd6Sachartre { "sd", VD_DRIVER_DISK }, /* Solaris */ 1938fce2fd6Sachartre { "ssd", VD_DRIVER_DISK }, /* Solaris */ 1948fce2fd6Sachartre { "vdc", VD_DRIVER_DISK }, /* Solaris */ 1958fce2fd6Sachartre { "vxdmp", VD_DRIVER_DISK }, /* Veritas */ 1968fce2fd6Sachartre { "vxio", VD_DRIVER_VOLUME }, /* Veritas - VxVM */ 1978fce2fd6Sachartre { "zfs", VD_DRIVER_VOLUME } /* Solaris */ 1988fce2fd6Sachartre }; 1998fce2fd6Sachartre 2001ae08745Sheppo /* Return a cpp token as a string */ 2011ae08745Sheppo #define STRINGIZE(token) #token 2021ae08745Sheppo 2031ae08745Sheppo /* 2041ae08745Sheppo * Print a message prefixed with the current function name to the message log 2051ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 2061ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 2071ae08745Sheppo * macros 2081ae08745Sheppo */ 2091ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 2101ae08745Sheppo #define _PRN(format, ...) \ 2111ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 2121ae08745Sheppo 2131ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 2141ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 2151ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 2161ae08745Sheppo 2171ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 2181ae08745Sheppo #define VD_CLIENT(vd) \ 2191ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 220f0ca1d9aSsb155480 (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" : \ 2211ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 2221ae08745Sheppo "unsupported client"))) 2231ae08745Sheppo 2241aff8f07SAlexandre Chartre /* Read disk label from a disk image */ 2251aff8f07SAlexandre Chartre #define VD_DSKIMG_LABEL_READ(vd, labelp) \ 2261aff8f07SAlexandre Chartre vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 227690555a1Sachartre 0, sizeof (struct dk_label)) 228690555a1Sachartre 2291aff8f07SAlexandre Chartre /* Write disk label to a disk image */ 2301aff8f07SAlexandre Chartre #define VD_DSKIMG_LABEL_WRITE(vd, labelp) \ 2311aff8f07SAlexandre Chartre vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 232690555a1Sachartre 0, sizeof (struct dk_label)) 233690555a1Sachartre 2341aff8f07SAlexandre Chartre /* Identify if a backend is a disk image */ 2351aff8f07SAlexandre Chartre #define VD_DSKIMG(vd) ((vd)->vdisk_type == VD_DISK_TYPE_DISK && \ 2361aff8f07SAlexandre Chartre ((vd)->file || (vd)->volume)) 2371aff8f07SAlexandre Chartre 23883990c4aSAlexandre Chartre /* Next index in a write queue */ 23983990c4aSAlexandre Chartre #define VD_WRITE_INDEX_NEXT(vd, id) \ 24083990c4aSAlexandre Chartre ((((id) + 1) >= vd->dring_len)? 0 : (id) + 1) 24183990c4aSAlexandre Chartre 2422f5224aeSachartre /* Message for disk access rights reset failure */ 2432f5224aeSachartre #define VD_RESET_ACCESS_FAILURE_MSG \ 2442f5224aeSachartre "Fail to reset disk access rights for disk %s" 2452f5224aeSachartre 246445b4c2eSsb155480 /* 247445b4c2eSsb155480 * Specification of an MD node passed to the MDEG to filter any 248445b4c2eSsb155480 * 'vport' nodes that do not belong to the specified node. This 249445b4c2eSsb155480 * template is copied for each vds instance and filled in with 250445b4c2eSsb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG. 251445b4c2eSsb155480 */ 252445b4c2eSsb155480 static mdeg_prop_spec_t vds_prop_template[] = { 253445b4c2eSsb155480 { MDET_PROP_STR, "name", VDS_NAME }, 254445b4c2eSsb155480 { MDET_PROP_VAL, "cfg-handle", NULL }, 255445b4c2eSsb155480 { MDET_LIST_END, NULL, NULL } 256445b4c2eSsb155480 }; 257445b4c2eSsb155480 258445b4c2eSsb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 259445b4c2eSsb155480 260445b4c2eSsb155480 /* 261445b4c2eSsb155480 * Matching criteria passed to the MDEG to register interest 262445b4c2eSsb155480 * in changes to 'virtual-device-port' nodes identified by their 263445b4c2eSsb155480 * 'id' property. 264445b4c2eSsb155480 */ 265445b4c2eSsb155480 static md_prop_match_t vd_prop_match[] = { 266445b4c2eSsb155480 { MDET_PROP_VAL, VD_ID_PROP }, 267445b4c2eSsb155480 { MDET_LIST_END, NULL } 268445b4c2eSsb155480 }; 269445b4c2eSsb155480 270445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port", 271445b4c2eSsb155480 vd_prop_match}; 272445b4c2eSsb155480 273047ba61eSachartre /* 274047ba61eSachartre * Options for the VD_BLOCK_DEVICE_OPTS property. 275047ba61eSachartre */ 276047ba61eSachartre #define VD_OPT_RDONLY 0x1 /* read-only */ 277047ba61eSachartre #define VD_OPT_SLICE 0x2 /* single slice */ 278047ba61eSachartre #define VD_OPT_EXCLUSIVE 0x4 /* exclusive access */ 279047ba61eSachartre 280047ba61eSachartre #define VD_OPTION_NLEN 128 281047ba61eSachartre 282047ba61eSachartre typedef struct vd_option { 283047ba61eSachartre char vdo_name[VD_OPTION_NLEN]; 284047ba61eSachartre uint64_t vdo_value; 285047ba61eSachartre } vd_option_t; 286047ba61eSachartre 287047ba61eSachartre vd_option_t vd_bdev_options[] = { 288047ba61eSachartre { "ro", VD_OPT_RDONLY }, 289047ba61eSachartre { "slice", VD_OPT_SLICE }, 290047ba61eSachartre { "excl", VD_OPT_EXCLUSIVE } 291047ba61eSachartre }; 292047ba61eSachartre 2931ae08745Sheppo /* Debugging macros */ 2941ae08745Sheppo #ifdef DEBUG 2953af08d82Slm66018 2963af08d82Slm66018 static int vd_msglevel = 0; 2973af08d82Slm66018 2981ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 2991ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 3001ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 3011ae08745Sheppo 3021ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 3033c96341aSnarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 3041ae08745Sheppo elem->hdr.dstate, \ 3051ae08745Sheppo elem->payload.operation, \ 3061ae08745Sheppo elem->payload.status, \ 3071ae08745Sheppo elem->payload.nbytes, \ 3081ae08745Sheppo elem->payload.addr, \ 3091ae08745Sheppo elem->payload.ncookies); 3101ae08745Sheppo 3113af08d82Slm66018 char * 3123af08d82Slm66018 vd_decode_state(int state) 3133af08d82Slm66018 { 3143af08d82Slm66018 char *str; 3153af08d82Slm66018 3163af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 3173af08d82Slm66018 3183af08d82Slm66018 switch (state) { 3193af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 3203af08d82Slm66018 CASE_STATE(VD_STATE_VER) 3213af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 3223af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 3233af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 3243af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 3253af08d82Slm66018 default: str = "unknown"; break; 3263af08d82Slm66018 } 3273af08d82Slm66018 3283af08d82Slm66018 #undef CASE_STATE 3293af08d82Slm66018 3303af08d82Slm66018 return (str); 3313af08d82Slm66018 } 3323af08d82Slm66018 3333af08d82Slm66018 void 3343af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 3353af08d82Slm66018 { 3363af08d82Slm66018 char *tstr, *sstr, *estr; 3373af08d82Slm66018 3383af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 3393af08d82Slm66018 3403af08d82Slm66018 switch (msg->tag.vio_msgtype) { 3413af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 3423af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 3433af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 3443af08d82Slm66018 default: tstr = "unknown"; break; 3453af08d82Slm66018 } 3463af08d82Slm66018 3473af08d82Slm66018 #undef CASE_TYPE 3483af08d82Slm66018 3493af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 3503af08d82Slm66018 3513af08d82Slm66018 switch (msg->tag.vio_subtype) { 3523af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 3533af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 3543af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 3553af08d82Slm66018 default: sstr = "unknown"; break; 3563af08d82Slm66018 } 3573af08d82Slm66018 3583af08d82Slm66018 #undef CASE_SUBTYPE 3593af08d82Slm66018 3603af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 3613af08d82Slm66018 3623af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 3633af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 3643af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 3653af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 3663af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 3673af08d82Slm66018 CASE_ENV(VIO_RDX) 3683af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 3693af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 3703af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 3713af08d82Slm66018 default: estr = "unknown"; break; 3723af08d82Slm66018 } 3733af08d82Slm66018 3743af08d82Slm66018 #undef CASE_ENV 3753af08d82Slm66018 3763af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 3773af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 3783af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 3793af08d82Slm66018 } 3803af08d82Slm66018 3811ae08745Sheppo #else /* !DEBUG */ 3823af08d82Slm66018 3831ae08745Sheppo #define PR0(...) 3841ae08745Sheppo #define PR1(...) 3851ae08745Sheppo #define PR2(...) 3861ae08745Sheppo 3871ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 3881ae08745Sheppo 3893af08d82Slm66018 #define vd_decode_state(_s) (NULL) 3903af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 3913af08d82Slm66018 3921ae08745Sheppo #endif /* DEBUG */ 3931ae08745Sheppo 3941ae08745Sheppo 395d10e4ef2Snarayan /* 396d10e4ef2Snarayan * Soft state structure for a vds instance 397d10e4ef2Snarayan */ 3981ae08745Sheppo typedef struct vds { 3991ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 4001ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 4011ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 4021ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 403445b4c2eSsb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 4041ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 4058fce2fd6Sachartre vd_driver_type_t *driver_types; /* extra driver types (from vds.conf) */ 4068fce2fd6Sachartre int num_drivers; /* num of extra driver types */ 4071ae08745Sheppo } vds_t; 4081ae08745Sheppo 409d10e4ef2Snarayan /* 410d10e4ef2Snarayan * Types of descriptor-processing tasks 411d10e4ef2Snarayan */ 412d10e4ef2Snarayan typedef enum vd_task_type { 413d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 414d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 415d10e4ef2Snarayan } vd_task_type_t; 416d10e4ef2Snarayan 417d10e4ef2Snarayan /* 418d10e4ef2Snarayan * Structure describing the task for processing a descriptor 419d10e4ef2Snarayan */ 420d10e4ef2Snarayan typedef struct vd_task { 421d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 422d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 423d10e4ef2Snarayan int index; /* dring elem index for task */ 424d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 425d10e4ef2Snarayan size_t msglen; /* length of message content */ 426d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 427d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 4284bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 429205eeb1aSlm66018 int status; /* status of processing task */ 430205eeb1aSlm66018 int (*completef)(struct vd_task *task); /* completion func ptr */ 43183990c4aSAlexandre Chartre uint32_t write_index; /* index in the write_queue */ 432d10e4ef2Snarayan } vd_task_t; 433d10e4ef2Snarayan 434d10e4ef2Snarayan /* 435d10e4ef2Snarayan * Soft state structure for a virtual disk instance 436d10e4ef2Snarayan */ 4371ae08745Sheppo typedef struct vd { 43883990c4aSAlexandre Chartre uint64_t id; /* vdisk id */ 4391ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 44017cadca8Slm66018 uint64_t operations; /* bitmask of VD_OPs exported */ 44117cadca8Slm66018 vio_ver_t version; /* ver negotiated with client */ 4421ae08745Sheppo vds_t *vds; /* server for this vdisk */ 443d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 444d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 44583990c4aSAlexandre Chartre ddi_taskq_t *ioq; /* queue for I/O */ 44683990c4aSAlexandre Chartre uint32_t write_index; /* next write index */ 44783990c4aSAlexandre Chartre buf_t **write_queue; /* queue for async writes */ 4481ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 4493c96341aSnarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */ 4501ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 451047ba61eSachartre int open_flags; /* open flags */ 452bae9e67eSachartre uint_t nslices; /* number of slices we export */ 4531ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 45417cadca8Slm66018 size_t vdisk_block_size; /* size of each vdisk block */ 4551ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 4564bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 45717cadca8Slm66018 vd_media_t vdisk_media; /* media type of backing dev. */ 45817cadca8Slm66018 boolean_t is_atapi_dev; /* Is this an IDE CD-ROM dev? */ 459e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 46017cadca8Slm66018 size_t block_size; /* blk size of actual device */ 4618fce2fd6Sachartre boolean_t volume; /* is vDisk backed by volume */ 4621aff8f07SAlexandre Chartre boolean_t zvol; /* is vDisk backed by a zvol */ 46317cadca8Slm66018 boolean_t file; /* is vDisk backed by a file? */ 4642f5224aeSachartre boolean_t scsi; /* is vDisk backed by scsi? */ 4653c96341aSnarayan vnode_t *file_vnode; /* file vnode */ 4661aff8f07SAlexandre Chartre size_t dskimg_size; /* size of disk image */ 4671aff8f07SAlexandre Chartre ddi_devid_t dskimg_devid; /* devid for disk image */ 468edcc0754Sachartre int efi_reserved; /* EFI reserved slice */ 469bae9e67eSachartre caddr_t flabel; /* fake label for slice type */ 470bae9e67eSachartre uint_t flabel_size; /* fake label size */ 471bae9e67eSachartre uint_t flabel_limit; /* limit of the fake label */ 4721ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 473342440ecSPrasad Singamsetty struct extvtoc vtoc; /* synthetic for slice type */ 474edcc0754Sachartre vd_slice_t slices[VD_MAXPART]; /* logical partitions */ 4752f5224aeSachartre boolean_t ownership; /* disk ownership status */ 4761ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 4771ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 4781ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 4791ae08745Sheppo vd_state_t state; /* client handshake state */ 4801ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 4811ae08745Sheppo uint32_t sid; /* client's session ID */ 4821ae08745Sheppo uint64_t seq_num; /* message sequence number */ 4831ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 4841ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 4851ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 4861ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 487bbfa0259Sha137994 uint8_t dring_mtype; /* dring mem map type */ 4881ae08745Sheppo caddr_t dring; /* address of dring */ 4893af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 490d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 491d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 492d10e4ef2Snarayan 493d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 494d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 495d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 496d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 4971ae08745Sheppo } vd_t; 4981ae08745Sheppo 499bae9e67eSachartre /* 500bae9e67eSachartre * Macros to manipulate the fake label (flabel) for single slice disks. 501bae9e67eSachartre * 502bae9e67eSachartre * If we fake a VTOC label then the fake label consists of only one block 503bae9e67eSachartre * containing the VTOC label (struct dk_label). 504bae9e67eSachartre * 505bae9e67eSachartre * If we fake an EFI label then the fake label consists of a blank block 506bae9e67eSachartre * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t). 507bae9e67eSachartre * 508bae9e67eSachartre */ 509bae9e67eSachartre #define VD_LABEL_VTOC_SIZE \ 510bae9e67eSachartre P2ROUNDUP(sizeof (struct dk_label), DEV_BSIZE) 511bae9e67eSachartre 512bae9e67eSachartre #define VD_LABEL_EFI_SIZE \ 513bae9e67eSachartre P2ROUNDUP(DEV_BSIZE + sizeof (efi_gpt_t) + \ 514bae9e67eSachartre sizeof (efi_gpe_t) * VD_MAXPART, DEV_BSIZE) 515bae9e67eSachartre 516bae9e67eSachartre #define VD_LABEL_VTOC(vd) \ 517342440ecSPrasad Singamsetty ((struct dk_label *)(void *)((vd)->flabel)) 518bae9e67eSachartre 519bae9e67eSachartre #define VD_LABEL_EFI_GPT(vd) \ 520342440ecSPrasad Singamsetty ((efi_gpt_t *)(void *)((vd)->flabel + DEV_BSIZE)) 521bae9e67eSachartre #define VD_LABEL_EFI_GPE(vd) \ 522342440ecSPrasad Singamsetty ((efi_gpe_t *)(void *)((vd)->flabel + DEV_BSIZE + \ 523342440ecSPrasad Singamsetty sizeof (efi_gpt_t))) 524bae9e67eSachartre 525bae9e67eSachartre 5261ae08745Sheppo typedef struct vds_operation { 5273af08d82Slm66018 char *namep; 5281ae08745Sheppo uint8_t operation; 529d10e4ef2Snarayan int (*start)(vd_task_t *task); 530205eeb1aSlm66018 int (*complete)(vd_task_t *task); 5311ae08745Sheppo } vds_operation_t; 5321ae08745Sheppo 5330a55fbb7Slm66018 typedef struct vd_ioctl { 5340a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 5350a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 5360a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 5370a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 5380a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 5390a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 5400a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 5412f5224aeSachartre int (*copyin)(void *vd_buf, size_t, void *ioctl_arg); 5420a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 5430a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 544047ba61eSachartre /* write is true if the operation writes any data to the backend */ 545047ba61eSachartre boolean_t write; 5460a55fbb7Slm66018 } vd_ioctl_t; 5470a55fbb7Slm66018 5480a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 5492f5224aeSachartre #define VD_IDENTITY_IN ((int (*)(void *, size_t, void *))-1) 5502f5224aeSachartre #define VD_IDENTITY_OUT ((void (*)(void *, void *))-1) 5511ae08745Sheppo 5521ae08745Sheppo 5533c96341aSnarayan static int vds_ldc_retries = VDS_RETRIES; 5543af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 5553c96341aSnarayan static int vds_dev_retries = VDS_RETRIES; 5563c96341aSnarayan static int vds_dev_delay = VDS_DEV_DELAY; 5571ae08745Sheppo static void *vds_state; 5581ae08745Sheppo 55987a7269eSachartre static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT; 5602f5224aeSachartre static int vd_scsi_debug = USCSI_SILENT; 5612f5224aeSachartre 5622f5224aeSachartre /* 56383990c4aSAlexandre Chartre * Number of threads in the taskq handling vdisk I/O. This can be set up to 56483990c4aSAlexandre Chartre * the size of the DRing which is the maximum number of I/O we can receive 56583990c4aSAlexandre Chartre * in parallel. Note that using a high number of threads can improve performance 56683990c4aSAlexandre Chartre * but this is going to consume a lot of resources if there are many vdisks. 56783990c4aSAlexandre Chartre */ 56883990c4aSAlexandre Chartre static int vd_ioq_nthreads = VD_IOQ_NTHREADS; 56983990c4aSAlexandre Chartre 57083990c4aSAlexandre Chartre /* 57183990c4aSAlexandre Chartre * Tunable to define the behavior for flushing asynchronous writes used to 57283990c4aSAlexandre Chartre * performed some write I/O requests. The default behavior is to group as 57383990c4aSAlexandre Chartre * much asynchronous writes as possible and to flush them immediatly. 57483990c4aSAlexandre Chartre * 57583990c4aSAlexandre Chartre * If the tunable is set to 0 then explicit flushing is disabled. In that 57683990c4aSAlexandre Chartre * case, data will be flushed by traditional mechanism (like fsflush) but 57783990c4aSAlexandre Chartre * this might not happen immediatly. 57883990c4aSAlexandre Chartre * 57983990c4aSAlexandre Chartre */ 58083990c4aSAlexandre Chartre static int vd_awflush = VD_AWFLUSH_IMMEDIATE | VD_AWFLUSH_GROUP; 58183990c4aSAlexandre Chartre 58283990c4aSAlexandre Chartre /* 5832f5224aeSachartre * Tunable to define the behavior of the service domain if the vdisk server 5842f5224aeSachartre * fails to reset disk exclusive access when a LDC channel is reset. When a 5852f5224aeSachartre * LDC channel is reset the vdisk server will try to reset disk exclusive 5862f5224aeSachartre * access by releasing any SCSI-2 reservation or resetting the disk. If these 5872f5224aeSachartre * actions fail then the default behavior (vd_reset_access_failure = 0) is to 5882f5224aeSachartre * print a warning message. This default behavior can be changed by setting 5892f5224aeSachartre * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will 5902f5224aeSachartre * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause 5912f5224aeSachartre * the service domain to panic. In both cases, the reset of the service domain 5922f5224aeSachartre * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation. 5932f5224aeSachartre */ 5942f5224aeSachartre static int vd_reset_access_failure = 0; 5952f5224aeSachartre 5962f5224aeSachartre /* 5972f5224aeSachartre * Tunable for backward compatibility. When this variable is set to B_TRUE, 5982f5224aeSachartre * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single 5992f5224aeSachartre * slice disks whether or not they have the "slice" option set. This is 6002f5224aeSachartre * to provide a simple backward compatibility mechanism when upgrading 6012f5224aeSachartre * the vds driver and using a domain configuration created before the 6022f5224aeSachartre * "slice" option was available. 6032f5224aeSachartre */ 6042f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE; 60587a7269eSachartre 6060a55fbb7Slm66018 /* 60766cfcfbeSachartre * The label of disk images created with some earlier versions of the virtual 60866cfcfbeSachartre * disk software is not entirely correct and have an incorrect v_sanity field 60966cfcfbeSachartre * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with 61066cfcfbeSachartre * these images because we are now validating that the disk label (and the 61166cfcfbeSachartre * sanity) is correct when a disk image is opened. 61266cfcfbeSachartre * 61366cfcfbeSachartre * This tunable is set to false to not validate the sanity field and ensure 61466cfcfbeSachartre * compatibility. If the tunable is set to true, we will do a strict checking 61566cfcfbeSachartre * of the sanity but this can create compatibility problems with old disk 61666cfcfbeSachartre * images. 61766cfcfbeSachartre */ 6181aff8f07SAlexandre Chartre static boolean_t vd_dskimg_validate_sanity = B_FALSE; 61966cfcfbeSachartre 62066cfcfbeSachartre /* 621bbfa0259Sha137994 * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings. 622bbfa0259Sha137994 */ 623bbfa0259Sha137994 static boolean_t vd_direct_mapped_drings = B_TRUE; 624bbfa0259Sha137994 625bbfa0259Sha137994 /* 626bae9e67eSachartre * When a backend is exported as a single-slice disk then we entirely fake 627bae9e67eSachartre * its disk label. So it can be exported either with a VTOC label or with 628bae9e67eSachartre * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all 629bae9e67eSachartre * single-slice disks will be exported with a VTOC label; and if it is set 630bae9e67eSachartre * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with 631bae9e67eSachartre * an EFI label. 632bae9e67eSachartre * 633bae9e67eSachartre * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk 634bae9e67eSachartre * or volume device then it will be exported with the same type of label as 635bae9e67eSachartre * defined on the device. Otherwise if the backend is a file then it will 636bae9e67eSachartre * exported with the disk label type set in the vd_file_slice_label variable. 637bae9e67eSachartre * 638bae9e67eSachartre * Note that if the backend size is greater than 1TB then it will always be 639bae9e67eSachartre * exported with an EFI label no matter what the setting is. 640bae9e67eSachartre */ 641bae9e67eSachartre static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK; 642bae9e67eSachartre 643bae9e67eSachartre static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC; 644bae9e67eSachartre 645bae9e67eSachartre /* 646bae9e67eSachartre * Tunable for backward compatibility. If this variable is set to B_TRUE then 647bae9e67eSachartre * single-slice disks are exported as disks with only one slice instead of 648bae9e67eSachartre * faking a complete disk partitioning. 649bae9e67eSachartre */ 650bae9e67eSachartre static boolean_t vd_slice_single_slice = B_FALSE; 651bae9e67eSachartre 652bae9e67eSachartre /* 6530a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 6540a55fbb7Slm66018 * 6550a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 6560a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 6570a55fbb7Slm66018 * supporting all lower minor version numbers as well) 6580a55fbb7Slm66018 */ 65917cadca8Slm66018 static const vio_ver_t vds_version[] = {{1, 1}}; 6600a55fbb7Slm66018 static const size_t vds_num_versions = 6610a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 6620a55fbb7Slm66018 6633af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 6643c96341aSnarayan static int vd_setup_vd(vd_t *vd); 665047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd); 6661aff8f07SAlexandre Chartre static int vd_setup_slice_image(vd_t *vd); 6671aff8f07SAlexandre Chartre static int vd_setup_disk_image(vd_t *vd); 668de3a5331SRamesh Chitrothu static int vd_backend_check_size(vd_t *vd); 6693c96341aSnarayan static boolean_t vd_enabled(vd_t *vd); 67078fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label); 6711aff8f07SAlexandre Chartre static int vd_dskimg_validate_geometry(vd_t *vd); 6721aff8f07SAlexandre Chartre static boolean_t vd_dskimg_is_iso_image(vd_t *vd); 67317cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd); 6742f5224aeSachartre static void vd_reset_access(vd_t *vd); 675edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg); 676edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **); 677edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *); 6788fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds); 679342440ecSPrasad Singamsetty static void vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom, 680bae9e67eSachartre struct dk_label *label); 681342440ecSPrasad Singamsetty static void vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc, 682bae9e67eSachartre struct dk_geom *geom); 683bae9e67eSachartre static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom); 684342440ecSPrasad Singamsetty static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc); 685bae9e67eSachartre 686bae9e67eSachartre extern int is_pseudo_device(dev_info_t *); 687bae9e67eSachartre 688bae9e67eSachartre /* 689bae9e67eSachartre * Function: 690bae9e67eSachartre * vd_get_readable_size 691bae9e67eSachartre * 692bae9e67eSachartre * Description: 693bae9e67eSachartre * Convert a given size in bytes to a human readable format in 694bae9e67eSachartre * kilobytes, megabytes, gigabytes or terabytes. 695bae9e67eSachartre * 696bae9e67eSachartre * Parameters: 697bae9e67eSachartre * full_size - the size to convert in bytes. 698bae9e67eSachartre * size - the converted size. 699bae9e67eSachartre * unit - the unit of the converted size: 'K' (kilobyte), 700bae9e67eSachartre * 'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte). 701bae9e67eSachartre * 702bae9e67eSachartre * Return Code: 703bae9e67eSachartre * none 704bae9e67eSachartre */ 7051aff8f07SAlexandre Chartre static void 706bae9e67eSachartre vd_get_readable_size(size_t full_size, size_t *size, char *unit) 707bae9e67eSachartre { 708bae9e67eSachartre if (full_size < (1ULL << 20)) { 709bae9e67eSachartre *size = full_size >> 10; 710bae9e67eSachartre *unit = 'K'; /* Kilobyte */ 711bae9e67eSachartre } else if (full_size < (1ULL << 30)) { 712bae9e67eSachartre *size = full_size >> 20; 713bae9e67eSachartre *unit = 'M'; /* Megabyte */ 714bae9e67eSachartre } else if (full_size < (1ULL << 40)) { 715bae9e67eSachartre *size = full_size >> 30; 716bae9e67eSachartre *unit = 'G'; /* Gigabyte */ 717bae9e67eSachartre } else { 718bae9e67eSachartre *size = full_size >> 40; 719bae9e67eSachartre *unit = 'T'; /* Terabyte */ 720bae9e67eSachartre } 721bae9e67eSachartre } 722047ba61eSachartre 723690555a1Sachartre /* 724690555a1Sachartre * Function: 7251aff8f07SAlexandre Chartre * vd_dskimg_io_params 726690555a1Sachartre * 727690555a1Sachartre * Description: 7281aff8f07SAlexandre Chartre * Convert virtual disk I/O parameters (slice, block, length) to 7291aff8f07SAlexandre Chartre * (offset, length) relative to the disk image and according to 7301aff8f07SAlexandre Chartre * the virtual disk partitioning. 731690555a1Sachartre * 732690555a1Sachartre * Parameters: 733690555a1Sachartre * vd - disk on which the operation is performed. 7341aff8f07SAlexandre Chartre * slice - slice to which is the I/O parameters apply. 7351aff8f07SAlexandre Chartre * VD_SLICE_NONE indicates that parameters are 7361aff8f07SAlexandre Chartre * are relative to the entire virtual disk. 7371aff8f07SAlexandre Chartre * blkp - pointer to the starting block relative to the 7381aff8f07SAlexandre Chartre * slice; return the starting block relative to 7391aff8f07SAlexandre Chartre * the disk image. 7401aff8f07SAlexandre Chartre * lenp - pointer to the number of bytes requested; return 7411aff8f07SAlexandre Chartre * the number of bytes that can effectively be used. 742690555a1Sachartre * 743690555a1Sachartre * Return Code: 7441aff8f07SAlexandre Chartre * 0 - I/O parameters have been successfully converted; 7451aff8f07SAlexandre Chartre * blkp and lenp point to the converted values. 7461aff8f07SAlexandre Chartre * ENODATA - no data are available for the given I/O parameters; 7471aff8f07SAlexandre Chartre * This occurs if the starting block is past the limit 7481aff8f07SAlexandre Chartre * of the slice. 7491aff8f07SAlexandre Chartre * EINVAL - I/O parameters are invalid. 750690555a1Sachartre */ 7511aff8f07SAlexandre Chartre static int 7521aff8f07SAlexandre Chartre vd_dskimg_io_params(vd_t *vd, int slice, size_t *blkp, size_t *lenp) 753690555a1Sachartre { 7541aff8f07SAlexandre Chartre size_t blk = *blkp; 7551aff8f07SAlexandre Chartre size_t len = *lenp; 7561aff8f07SAlexandre Chartre size_t offset, maxlen; 757690555a1Sachartre 7581aff8f07SAlexandre Chartre ASSERT(vd->file || VD_DSKIMG(vd)); 759690555a1Sachartre ASSERT(len > 0); 760690555a1Sachartre 761047ba61eSachartre /* 762047ba61eSachartre * If a file is exported as a slice then we don't care about the vtoc. 763047ba61eSachartre * In that case, the vtoc is a fake mainly to make newfs happy and we 764047ba61eSachartre * handle any I/O as a raw disk access so that we can have access to the 765047ba61eSachartre * entire backend. 766047ba61eSachartre */ 767047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) { 768690555a1Sachartre /* raw disk access */ 769690555a1Sachartre offset = blk * DEV_BSIZE; 7701aff8f07SAlexandre Chartre if (offset >= vd->dskimg_size) { 771bae9e67eSachartre /* offset past the end of the disk */ 7721aff8f07SAlexandre Chartre PR0("offset (0x%lx) >= size (0x%lx)", 7731aff8f07SAlexandre Chartre offset, vd->dskimg_size); 7741aff8f07SAlexandre Chartre return (ENODATA); 775bae9e67eSachartre } 7761aff8f07SAlexandre Chartre maxlen = vd->dskimg_size - offset; 777690555a1Sachartre } else { 778690555a1Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR); 77978fcd0a1Sachartre 78017cadca8Slm66018 /* 78117cadca8Slm66018 * v1.0 vDisk clients depended on the server not verifying 78217cadca8Slm66018 * the label of a unformatted disk. This "feature" is 78317cadca8Slm66018 * maintained for backward compatibility but all versions 78417cadca8Slm66018 * from v1.1 onwards must do the right thing. 78517cadca8Slm66018 */ 78678fcd0a1Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK && 787edcc0754Sachartre vio_ver_is_supported(vd->version, 1, 1)) { 7881aff8f07SAlexandre Chartre (void) vd_dskimg_validate_geometry(vd); 789edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 790edcc0754Sachartre PR0("Unknown disk label, can't do I/O " 791edcc0754Sachartre "from slice %d", slice); 7921aff8f07SAlexandre Chartre return (EINVAL); 79378fcd0a1Sachartre } 794edcc0754Sachartre } 79578fcd0a1Sachartre 796edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 797edcc0754Sachartre ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE); 798edcc0754Sachartre } else { 799edcc0754Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI); 800edcc0754Sachartre ASSERT(vd->vdisk_block_size == DEV_BSIZE); 801edcc0754Sachartre } 802edcc0754Sachartre 803edcc0754Sachartre if (blk >= vd->slices[slice].nblocks) { 804690555a1Sachartre /* address past the end of the slice */ 805bae9e67eSachartre PR0("req_addr (0x%lx) >= psize (0x%lx)", 806edcc0754Sachartre blk, vd->slices[slice].nblocks); 8071aff8f07SAlexandre Chartre return (ENODATA); 808690555a1Sachartre } 809690555a1Sachartre 810edcc0754Sachartre offset = (vd->slices[slice].start + blk) * DEV_BSIZE; 811bae9e67eSachartre maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE; 812bae9e67eSachartre } 813690555a1Sachartre 814690555a1Sachartre /* 815690555a1Sachartre * If the requested size is greater than the size 816690555a1Sachartre * of the partition, truncate the read/write. 817690555a1Sachartre */ 818690555a1Sachartre if (len > maxlen) { 819690555a1Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes", 820690555a1Sachartre maxlen, len); 821690555a1Sachartre len = maxlen; 822690555a1Sachartre } 823690555a1Sachartre 824690555a1Sachartre /* 825690555a1Sachartre * We have to ensure that we are reading/writing into the mmap 826690555a1Sachartre * range. If we have a partial disk image (e.g. an image of 827690555a1Sachartre * s0 instead s2) the system can try to access slices that 828690555a1Sachartre * are not included into the disk image. 829690555a1Sachartre */ 8301aff8f07SAlexandre Chartre if ((offset + len) > vd->dskimg_size) { 831edcc0754Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) > " 8321aff8f07SAlexandre Chartre "dskimg_size (0x%lx)", offset, len, vd->dskimg_size); 8331aff8f07SAlexandre Chartre return (EINVAL); 8341aff8f07SAlexandre Chartre } 8351aff8f07SAlexandre Chartre 8361aff8f07SAlexandre Chartre *blkp = offset / DEV_BSIZE; 8371aff8f07SAlexandre Chartre *lenp = len; 8381aff8f07SAlexandre Chartre 8391aff8f07SAlexandre Chartre return (0); 8401aff8f07SAlexandre Chartre } 8411aff8f07SAlexandre Chartre 8421aff8f07SAlexandre Chartre /* 8431aff8f07SAlexandre Chartre * Function: 8441aff8f07SAlexandre Chartre * vd_dskimg_rw 8451aff8f07SAlexandre Chartre * 8461aff8f07SAlexandre Chartre * Description: 8471aff8f07SAlexandre Chartre * Read or write to a disk image. It handles the case where the disk 8481aff8f07SAlexandre Chartre * image is a file or a volume exported as a full disk or a file 8491aff8f07SAlexandre Chartre * exported as single-slice disk. Read or write to volumes exported as 8501aff8f07SAlexandre Chartre * single slice disks are done by directly using the ldi interface. 8511aff8f07SAlexandre Chartre * 8521aff8f07SAlexandre Chartre * Parameters: 8531aff8f07SAlexandre Chartre * vd - disk on which the operation is performed. 8541aff8f07SAlexandre Chartre * slice - slice on which the operation is performed, 8551aff8f07SAlexandre Chartre * VD_SLICE_NONE indicates that the operation 8561aff8f07SAlexandre Chartre * is done using an absolute disk offset. 8571aff8f07SAlexandre Chartre * operation - operation to execute: read (VD_OP_BREAD) or 8581aff8f07SAlexandre Chartre * write (VD_OP_BWRITE). 8591aff8f07SAlexandre Chartre * data - buffer where data are read to or written from. 8601aff8f07SAlexandre Chartre * blk - starting block for the operation. 8611aff8f07SAlexandre Chartre * len - number of bytes to read or write. 8621aff8f07SAlexandre Chartre * 8631aff8f07SAlexandre Chartre * Return Code: 8641aff8f07SAlexandre Chartre * n >= 0 - success, n indicates the number of bytes read 8651aff8f07SAlexandre Chartre * or written. 8661aff8f07SAlexandre Chartre * -1 - error. 8671aff8f07SAlexandre Chartre */ 8681aff8f07SAlexandre Chartre static ssize_t 8691aff8f07SAlexandre Chartre vd_dskimg_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t offset, 8701aff8f07SAlexandre Chartre size_t len) 8711aff8f07SAlexandre Chartre { 8721aff8f07SAlexandre Chartre ssize_t resid; 8731aff8f07SAlexandre Chartre struct buf buf; 8741aff8f07SAlexandre Chartre int status; 8751aff8f07SAlexandre Chartre 8761aff8f07SAlexandre Chartre ASSERT(vd->file || VD_DSKIMG(vd)); 8771aff8f07SAlexandre Chartre ASSERT(len > 0); 8781aff8f07SAlexandre Chartre 8791aff8f07SAlexandre Chartre if ((status = vd_dskimg_io_params(vd, slice, &offset, &len)) != 0) 8801aff8f07SAlexandre Chartre return ((status == ENODATA)? 0: -1); 8811aff8f07SAlexandre Chartre 8821aff8f07SAlexandre Chartre if (vd->volume) { 8831aff8f07SAlexandre Chartre 8841aff8f07SAlexandre Chartre bioinit(&buf); 8851aff8f07SAlexandre Chartre buf.b_flags = B_BUSY | 8861aff8f07SAlexandre Chartre ((operation == VD_OP_BREAD)? B_READ : B_WRITE); 8871aff8f07SAlexandre Chartre buf.b_bcount = len; 88883990c4aSAlexandre Chartre buf.b_lblkno = offset; 8891aff8f07SAlexandre Chartre buf.b_edev = vd->dev[0]; 8901aff8f07SAlexandre Chartre buf.b_un.b_addr = data; 8911aff8f07SAlexandre Chartre 8921aff8f07SAlexandre Chartre /* 8931aff8f07SAlexandre Chartre * We use ldi_strategy() and not ldi_read()/ldi_write() because 8941aff8f07SAlexandre Chartre * the read/write functions of the underlying driver may try to 8951aff8f07SAlexandre Chartre * lock pages of the data buffer, and this requires the data 8961aff8f07SAlexandre Chartre * buffer to be kmem_alloc'ed (and not allocated on the stack). 8971aff8f07SAlexandre Chartre * 8981aff8f07SAlexandre Chartre * Also using ldi_strategy() ensures that writes are immediatly 8991aff8f07SAlexandre Chartre * commited and not cached as this may be the case with 9001aff8f07SAlexandre Chartre * ldi_write() (for example with a ZFS volume). 9011aff8f07SAlexandre Chartre */ 9021aff8f07SAlexandre Chartre if (ldi_strategy(vd->ldi_handle[0], &buf) != 0) { 9031aff8f07SAlexandre Chartre biofini(&buf); 904690555a1Sachartre return (-1); 905690555a1Sachartre } 906690555a1Sachartre 9071aff8f07SAlexandre Chartre if (biowait(&buf) != 0) { 9081aff8f07SAlexandre Chartre biofini(&buf); 9091aff8f07SAlexandre Chartre return (-1); 9101aff8f07SAlexandre Chartre } 9111aff8f07SAlexandre Chartre 9121aff8f07SAlexandre Chartre resid = buf.b_resid; 9131aff8f07SAlexandre Chartre biofini(&buf); 9141aff8f07SAlexandre Chartre 9151aff8f07SAlexandre Chartre ASSERT(resid <= len); 9161aff8f07SAlexandre Chartre return (len - resid); 9171aff8f07SAlexandre Chartre } 9181aff8f07SAlexandre Chartre 9191aff8f07SAlexandre Chartre ASSERT(vd->file); 9201aff8f07SAlexandre Chartre 92183990c4aSAlexandre Chartre status = vn_rdwr((operation == VD_OP_BREAD)? UIO_READ : UIO_WRITE, 92283990c4aSAlexandre Chartre vd->file_vnode, data, len, offset * DEV_BSIZE, UIO_SYSSPACE, FSYNC, 92383990c4aSAlexandre Chartre RLIM64_INFINITY, kcred, &resid); 924690555a1Sachartre 92583990c4aSAlexandre Chartre if (status != 0) 926690555a1Sachartre return (-1); 927690555a1Sachartre 928690555a1Sachartre return (len); 929690555a1Sachartre } 930690555a1Sachartre 93187a7269eSachartre /* 93287a7269eSachartre * Function: 933bae9e67eSachartre * vd_build_default_label 93478fcd0a1Sachartre * 93578fcd0a1Sachartre * Description: 936bae9e67eSachartre * Return a default label for a given disk size. This is used when the disk 93778fcd0a1Sachartre * does not have a valid VTOC so that the user can get a valid default 93817cadca8Slm66018 * configuration. The default label has all slice sizes set to 0 (except 93978fcd0a1Sachartre * slice 2 which is the entire disk) to force the user to write a valid 94078fcd0a1Sachartre * label onto the disk image. 94178fcd0a1Sachartre * 94278fcd0a1Sachartre * Parameters: 943bae9e67eSachartre * disk_size - the disk size in bytes 94478fcd0a1Sachartre * label - the returned default label. 94578fcd0a1Sachartre * 94678fcd0a1Sachartre * Return Code: 94778fcd0a1Sachartre * none. 94878fcd0a1Sachartre */ 94978fcd0a1Sachartre static void 950bae9e67eSachartre vd_build_default_label(size_t disk_size, struct dk_label *label) 95178fcd0a1Sachartre { 95278fcd0a1Sachartre size_t size; 953bae9e67eSachartre char unit; 954edcc0754Sachartre 955edcc0754Sachartre bzero(label, sizeof (struct dk_label)); 95678fcd0a1Sachartre 95778fcd0a1Sachartre /* 958342440ecSPrasad Singamsetty * Ideally we would like the cylinder size (nsect * nhead) to be the 959342440ecSPrasad Singamsetty * same whatever the disk size is. That way the VTOC label could be 960342440ecSPrasad Singamsetty * easily updated in case the disk size is increased (keeping the 961342440ecSPrasad Singamsetty * same cylinder size allows to preserve the existing partitioning 962342440ecSPrasad Singamsetty * when updating the VTOC label). But it is not possible to have 963342440ecSPrasad Singamsetty * a fixed cylinder size and to cover all disk size. 96478fcd0a1Sachartre * 965342440ecSPrasad Singamsetty * So we define different cylinder sizes depending on the disk size. 966342440ecSPrasad Singamsetty * The cylinder size is chosen so that we don't have too few cylinders 967f745d6a3Sachartre * for a small disk image, or so many on a big disk image that you 968f745d6a3Sachartre * waste space for backup superblocks or cylinder group structures. 969342440ecSPrasad Singamsetty * Also we must have a resonable number of cylinders and sectors so 970342440ecSPrasad Singamsetty * that newfs can run using default values. 971342440ecSPrasad Singamsetty * 972342440ecSPrasad Singamsetty * +-----------+--------+---------+--------+ 973342440ecSPrasad Singamsetty * | disk_size | < 2MB | 2MB-4GB | >= 8GB | 974342440ecSPrasad Singamsetty * +-----------+--------+---------+--------+ 975342440ecSPrasad Singamsetty * | nhead | 1 | 1 | 96 | 976342440ecSPrasad Singamsetty * | nsect | 200 | 600 | 768 | 977342440ecSPrasad Singamsetty * +-----------+--------+---------+--------+ 978342440ecSPrasad Singamsetty * 979342440ecSPrasad Singamsetty * Other parameters are computed from these values: 980342440ecSPrasad Singamsetty * 981342440ecSPrasad Singamsetty * pcyl = disk_size / (nhead * nsect * 512) 982342440ecSPrasad Singamsetty * acyl = (pcyl > 2)? 2 : 0 983342440ecSPrasad Singamsetty * ncyl = pcyl - acyl 984342440ecSPrasad Singamsetty * 985342440ecSPrasad Singamsetty * The maximum number of cylinder is 65535 so this allows to define a 986342440ecSPrasad Singamsetty * geometry for a disk size up to 65535 * 96 * 768 * 512 = 2.24 TB 987342440ecSPrasad Singamsetty * which is more than enough to cover the maximum size allowed by the 988342440ecSPrasad Singamsetty * extended VTOC format (2TB). 98978fcd0a1Sachartre */ 990342440ecSPrasad Singamsetty 991342440ecSPrasad Singamsetty if (disk_size >= 8 * ONE_GIGABYTE) { 992342440ecSPrasad Singamsetty 993342440ecSPrasad Singamsetty label->dkl_nhead = 96; 994342440ecSPrasad Singamsetty label->dkl_nsect = 768; 995342440ecSPrasad Singamsetty 996342440ecSPrasad Singamsetty } else if (disk_size >= 2 * ONE_MEGABYTE) { 997342440ecSPrasad Singamsetty 998342440ecSPrasad Singamsetty label->dkl_nhead = 1; 999342440ecSPrasad Singamsetty label->dkl_nsect = 600; 1000342440ecSPrasad Singamsetty 1001342440ecSPrasad Singamsetty } else { 1002342440ecSPrasad Singamsetty 1003342440ecSPrasad Singamsetty label->dkl_nhead = 1; 1004342440ecSPrasad Singamsetty label->dkl_nsect = 200; 1005342440ecSPrasad Singamsetty } 1006342440ecSPrasad Singamsetty 1007342440ecSPrasad Singamsetty label->dkl_pcyl = disk_size / 1008342440ecSPrasad Singamsetty (label->dkl_nsect * label->dkl_nhead * DEV_BSIZE); 100978fcd0a1Sachartre 101078fcd0a1Sachartre if (label->dkl_pcyl == 0) 101178fcd0a1Sachartre label->dkl_pcyl = 1; 101278fcd0a1Sachartre 1013047ba61eSachartre label->dkl_acyl = 0; 1014047ba61eSachartre 101578fcd0a1Sachartre if (label->dkl_pcyl > 2) 101678fcd0a1Sachartre label->dkl_acyl = 2; 101778fcd0a1Sachartre 101878fcd0a1Sachartre label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl; 101978fcd0a1Sachartre label->dkl_write_reinstruct = 0; 102078fcd0a1Sachartre label->dkl_read_reinstruct = 0; 102178fcd0a1Sachartre label->dkl_rpm = 7200; 102278fcd0a1Sachartre label->dkl_apc = 0; 102378fcd0a1Sachartre label->dkl_intrlv = 0; 102478fcd0a1Sachartre 1025bae9e67eSachartre PR0("requested disk size: %ld bytes\n", disk_size); 102678fcd0a1Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl, 102778fcd0a1Sachartre label->dkl_nhead, label->dkl_nsect); 102878fcd0a1Sachartre PR0("provided disk size: %ld bytes\n", (uint64_t) 102978fcd0a1Sachartre (label->dkl_pcyl * label->dkl_nhead * 103078fcd0a1Sachartre label->dkl_nsect * DEV_BSIZE)); 103178fcd0a1Sachartre 1032bae9e67eSachartre vd_get_readable_size(disk_size, &size, &unit); 103378fcd0a1Sachartre 103478fcd0a1Sachartre /* 103578fcd0a1Sachartre * We must have a correct label name otherwise format(1m) will 103678fcd0a1Sachartre * not recognized the disk as labeled. 103778fcd0a1Sachartre */ 103878fcd0a1Sachartre (void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII, 103978fcd0a1Sachartre "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d", 1040bae9e67eSachartre size, unit, 104178fcd0a1Sachartre label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead, 104278fcd0a1Sachartre label->dkl_nsect); 104378fcd0a1Sachartre 104478fcd0a1Sachartre /* default VTOC */ 1045342440ecSPrasad Singamsetty label->dkl_vtoc.v_version = V_EXTVERSION; 1046edcc0754Sachartre label->dkl_vtoc.v_nparts = V_NUMPAR; 104778fcd0a1Sachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 1048edcc0754Sachartre label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 1049edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0; 1050edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl * 105178fcd0a1Sachartre label->dkl_nhead * label->dkl_nsect; 1052edcc0754Sachartre label->dkl_magic = DKL_MAGIC; 105378fcd0a1Sachartre label->dkl_cksum = vd_lbl2cksum(label); 105478fcd0a1Sachartre } 105578fcd0a1Sachartre 105678fcd0a1Sachartre /* 105778fcd0a1Sachartre * Function: 10581aff8f07SAlexandre Chartre * vd_dskimg_set_vtoc 105987a7269eSachartre * 106087a7269eSachartre * Description: 106187a7269eSachartre * Set the vtoc of a disk image by writing the label and backup 106287a7269eSachartre * labels into the disk image backend. 106387a7269eSachartre * 106487a7269eSachartre * Parameters: 106587a7269eSachartre * vd - disk on which the operation is performed. 106687a7269eSachartre * label - the data to be written. 106787a7269eSachartre * 106887a7269eSachartre * Return Code: 106987a7269eSachartre * 0 - success. 107087a7269eSachartre * n > 0 - error, n indicates the errno code. 107187a7269eSachartre */ 107287a7269eSachartre static int 10731aff8f07SAlexandre Chartre vd_dskimg_set_vtoc(vd_t *vd, struct dk_label *label) 107487a7269eSachartre { 1075342440ecSPrasad Singamsetty size_t blk, sec, cyl, head, cnt; 107687a7269eSachartre 10771aff8f07SAlexandre Chartre ASSERT(VD_DSKIMG(vd)); 107887a7269eSachartre 10791aff8f07SAlexandre Chartre if (VD_DSKIMG_LABEL_WRITE(vd, label) < 0) { 108087a7269eSachartre PR0("fail to write disk label"); 108187a7269eSachartre return (EIO); 108287a7269eSachartre } 108387a7269eSachartre 108487a7269eSachartre /* 108587a7269eSachartre * Backup labels are on the last alternate cylinder's 108687a7269eSachartre * first five odd sectors. 108787a7269eSachartre */ 108887a7269eSachartre if (label->dkl_acyl == 0) { 108987a7269eSachartre PR0("no alternate cylinder, can not store backup labels"); 109087a7269eSachartre return (0); 109187a7269eSachartre } 109287a7269eSachartre 109387a7269eSachartre cyl = label->dkl_ncyl + label->dkl_acyl - 1; 109487a7269eSachartre head = label->dkl_nhead - 1; 109587a7269eSachartre 109687a7269eSachartre blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) + 109787a7269eSachartre (head * label->dkl_nsect); 109887a7269eSachartre 109987a7269eSachartre /* 110087a7269eSachartre * Write the backup labels. Make sure we don't try to write past 110187a7269eSachartre * the last cylinder. 110287a7269eSachartre */ 110387a7269eSachartre sec = 1; 110487a7269eSachartre 11051aff8f07SAlexandre Chartre for (cnt = 0; cnt < VD_DSKIMG_NUM_BACKUP; cnt++) { 110687a7269eSachartre 110787a7269eSachartre if (sec >= label->dkl_nsect) { 110887a7269eSachartre PR0("not enough sector to store all backup labels"); 110987a7269eSachartre return (0); 111087a7269eSachartre } 111187a7269eSachartre 11121aff8f07SAlexandre Chartre if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 11131aff8f07SAlexandre Chartre (caddr_t)label, blk + sec, sizeof (struct dk_label)) < 0) { 1114342440ecSPrasad Singamsetty PR0("error writing backup label at block %lu\n", 111587a7269eSachartre blk + sec); 111687a7269eSachartre return (EIO); 111787a7269eSachartre } 111887a7269eSachartre 1119342440ecSPrasad Singamsetty PR1("wrote backup label at block %lu\n", blk + sec); 112087a7269eSachartre 112187a7269eSachartre sec += 2; 112287a7269eSachartre } 112387a7269eSachartre 112487a7269eSachartre return (0); 112587a7269eSachartre } 112687a7269eSachartre 112787a7269eSachartre /* 112887a7269eSachartre * Function: 11291aff8f07SAlexandre Chartre * vd_dskimg_get_devid_block 113087a7269eSachartre * 113187a7269eSachartre * Description: 113287a7269eSachartre * Return the block number where the device id is stored. 113387a7269eSachartre * 113487a7269eSachartre * Parameters: 113587a7269eSachartre * vd - disk on which the operation is performed. 113687a7269eSachartre * blkp - pointer to the block number 113787a7269eSachartre * 113887a7269eSachartre * Return Code: 113987a7269eSachartre * 0 - success 114087a7269eSachartre * ENOSPC - disk has no space to store a device id 114187a7269eSachartre */ 114287a7269eSachartre static int 11431aff8f07SAlexandre Chartre vd_dskimg_get_devid_block(vd_t *vd, size_t *blkp) 114487a7269eSachartre { 114587a7269eSachartre diskaddr_t spc, head, cyl; 114687a7269eSachartre 11471aff8f07SAlexandre Chartre ASSERT(VD_DSKIMG(vd)); 1148edcc0754Sachartre 1149edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 1150edcc0754Sachartre /* 1151edcc0754Sachartre * If no label is defined we don't know where to find 1152edcc0754Sachartre * a device id. 1153edcc0754Sachartre */ 1154edcc0754Sachartre return (ENOSPC); 1155edcc0754Sachartre } 1156edcc0754Sachartre 1157edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1158edcc0754Sachartre /* 1159edcc0754Sachartre * For an EFI disk, the devid is at the beginning of 1160edcc0754Sachartre * the reserved slice 1161edcc0754Sachartre */ 1162edcc0754Sachartre if (vd->efi_reserved == -1) { 1163edcc0754Sachartre PR0("EFI disk has no reserved slice"); 1164edcc0754Sachartre return (ENOSPC); 1165edcc0754Sachartre } 1166edcc0754Sachartre 1167edcc0754Sachartre *blkp = vd->slices[vd->efi_reserved].start; 1168edcc0754Sachartre return (0); 1169edcc0754Sachartre } 1170edcc0754Sachartre 117187a7269eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 117287a7269eSachartre 117387a7269eSachartre /* this geometry doesn't allow us to have a devid */ 117487a7269eSachartre if (vd->dk_geom.dkg_acyl < 2) { 117587a7269eSachartre PR0("not enough alternate cylinder available for devid " 117687a7269eSachartre "(acyl=%u)", vd->dk_geom.dkg_acyl); 117787a7269eSachartre return (ENOSPC); 117887a7269eSachartre } 117987a7269eSachartre 118087a7269eSachartre /* the devid is in on the track next to the last cylinder */ 118187a7269eSachartre cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2; 118287a7269eSachartre spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 118387a7269eSachartre head = vd->dk_geom.dkg_nhead - 1; 118487a7269eSachartre 118587a7269eSachartre *blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) + 118687a7269eSachartre (head * vd->dk_geom.dkg_nsect) + 1; 118787a7269eSachartre 118887a7269eSachartre return (0); 118987a7269eSachartre } 119087a7269eSachartre 119187a7269eSachartre /* 119287a7269eSachartre * Return the checksum of a disk block containing an on-disk devid. 119387a7269eSachartre */ 119487a7269eSachartre static uint_t 119587a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid) 119687a7269eSachartre { 119787a7269eSachartre uint_t chksum, *ip; 119887a7269eSachartre int i; 119987a7269eSachartre 120087a7269eSachartre chksum = 0; 1201342440ecSPrasad Singamsetty ip = (void *)dkdevid; 120287a7269eSachartre for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++) 120387a7269eSachartre chksum ^= ip[i]; 120487a7269eSachartre 120587a7269eSachartre return (chksum); 120687a7269eSachartre } 120787a7269eSachartre 120887a7269eSachartre /* 120987a7269eSachartre * Function: 12101aff8f07SAlexandre Chartre * vd_dskimg_read_devid 121187a7269eSachartre * 121287a7269eSachartre * Description: 121387a7269eSachartre * Read the device id stored on a disk image. 121487a7269eSachartre * 121587a7269eSachartre * Parameters: 121687a7269eSachartre * vd - disk on which the operation is performed. 121787a7269eSachartre * devid - the return address of the device ID. 121887a7269eSachartre * 121987a7269eSachartre * Return Code: 122087a7269eSachartre * 0 - success 122187a7269eSachartre * EIO - I/O error while trying to access the disk image 122287a7269eSachartre * EINVAL - no valid device id was found 122387a7269eSachartre * ENOSPC - disk has no space to store a device id 122487a7269eSachartre */ 122587a7269eSachartre static int 12261aff8f07SAlexandre Chartre vd_dskimg_read_devid(vd_t *vd, ddi_devid_t *devid) 122787a7269eSachartre { 122887a7269eSachartre struct dk_devid *dkdevid; 122987a7269eSachartre size_t blk; 123087a7269eSachartre uint_t chksum; 123187a7269eSachartre int status, sz; 123287a7269eSachartre 12331aff8f07SAlexandre Chartre if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0) 123487a7269eSachartre return (status); 123587a7269eSachartre 123687a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 123787a7269eSachartre 123887a7269eSachartre /* get the devid */ 12391aff8f07SAlexandre Chartre if ((vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk, 124087a7269eSachartre DEV_BSIZE)) < 0) { 124187a7269eSachartre PR0("error reading devid block at %lu", blk); 124287a7269eSachartre status = EIO; 124387a7269eSachartre goto done; 124487a7269eSachartre } 124587a7269eSachartre 124687a7269eSachartre /* validate the revision */ 124787a7269eSachartre if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 124887a7269eSachartre (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 124987a7269eSachartre PR0("invalid devid found at block %lu (bad revision)", blk); 125087a7269eSachartre status = EINVAL; 125187a7269eSachartre goto done; 125287a7269eSachartre } 125387a7269eSachartre 125487a7269eSachartre /* compute checksum */ 125587a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 125687a7269eSachartre 125787a7269eSachartre /* compare the checksums */ 125887a7269eSachartre if (DKD_GETCHKSUM(dkdevid) != chksum) { 125987a7269eSachartre PR0("invalid devid found at block %lu (bad checksum)", blk); 126087a7269eSachartre status = EINVAL; 126187a7269eSachartre goto done; 126287a7269eSachartre } 126387a7269eSachartre 126487a7269eSachartre /* validate the device id */ 126587a7269eSachartre if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 126687a7269eSachartre PR0("invalid devid found at block %lu", blk); 126787a7269eSachartre status = EINVAL; 126887a7269eSachartre goto done; 126987a7269eSachartre } 127087a7269eSachartre 127187a7269eSachartre PR1("devid read at block %lu", blk); 127287a7269eSachartre 127387a7269eSachartre sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 127487a7269eSachartre *devid = kmem_alloc(sz, KM_SLEEP); 127587a7269eSachartre bcopy(&dkdevid->dkd_devid, *devid, sz); 127687a7269eSachartre 127787a7269eSachartre done: 127887a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 127987a7269eSachartre return (status); 128087a7269eSachartre 128187a7269eSachartre } 128287a7269eSachartre 128387a7269eSachartre /* 128487a7269eSachartre * Function: 12851aff8f07SAlexandre Chartre * vd_dskimg_write_devid 128687a7269eSachartre * 128787a7269eSachartre * Description: 128887a7269eSachartre * Write a device id into disk image. 128987a7269eSachartre * 129087a7269eSachartre * Parameters: 129187a7269eSachartre * vd - disk on which the operation is performed. 129287a7269eSachartre * devid - the device ID to store. 129387a7269eSachartre * 129487a7269eSachartre * Return Code: 129587a7269eSachartre * 0 - success 129687a7269eSachartre * EIO - I/O error while trying to access the disk image 129787a7269eSachartre * ENOSPC - disk has no space to store a device id 129887a7269eSachartre */ 129987a7269eSachartre static int 13001aff8f07SAlexandre Chartre vd_dskimg_write_devid(vd_t *vd, ddi_devid_t devid) 130187a7269eSachartre { 130287a7269eSachartre struct dk_devid *dkdevid; 130387a7269eSachartre uint_t chksum; 130487a7269eSachartre size_t blk; 130587a7269eSachartre int status; 130687a7269eSachartre 1307edcc0754Sachartre if (devid == NULL) { 1308edcc0754Sachartre /* nothing to write */ 1309edcc0754Sachartre return (0); 1310edcc0754Sachartre } 1311edcc0754Sachartre 13121aff8f07SAlexandre Chartre if ((status = vd_dskimg_get_devid_block(vd, &blk)) != 0) 131387a7269eSachartre return (status); 131487a7269eSachartre 131587a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 131687a7269eSachartre 131787a7269eSachartre /* set revision */ 131887a7269eSachartre dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 131987a7269eSachartre dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 132087a7269eSachartre 132187a7269eSachartre /* copy devid */ 132287a7269eSachartre bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid)); 132387a7269eSachartre 132487a7269eSachartre /* compute checksum */ 132587a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 132687a7269eSachartre 132787a7269eSachartre /* set checksum */ 132887a7269eSachartre DKD_FORMCHKSUM(chksum, dkdevid); 132987a7269eSachartre 133087a7269eSachartre /* store the devid */ 13311aff8f07SAlexandre Chartre if ((status = vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 133287a7269eSachartre (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) { 133387a7269eSachartre PR0("Error writing devid block at %lu", blk); 133487a7269eSachartre status = EIO; 133587a7269eSachartre } else { 133687a7269eSachartre PR1("devid written at block %lu", blk); 133787a7269eSachartre status = 0; 133887a7269eSachartre } 133987a7269eSachartre 134087a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 134187a7269eSachartre return (status); 134287a7269eSachartre } 134387a7269eSachartre 134487a7269eSachartre /* 134587a7269eSachartre * Function: 134617cadca8Slm66018 * vd_do_scsi_rdwr 134787a7269eSachartre * 134887a7269eSachartre * Description: 134987a7269eSachartre * Read or write to a SCSI disk using an absolute disk offset. 135087a7269eSachartre * 135187a7269eSachartre * Parameters: 135287a7269eSachartre * vd - disk on which the operation is performed. 135387a7269eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 135487a7269eSachartre * write (VD_OP_BWRITE). 135587a7269eSachartre * data - buffer where data are read to or written from. 135687a7269eSachartre * blk - starting block for the operation. 135787a7269eSachartre * len - number of bytes to read or write. 135887a7269eSachartre * 135987a7269eSachartre * Return Code: 136087a7269eSachartre * 0 - success 136187a7269eSachartre * n != 0 - error. 136287a7269eSachartre */ 136387a7269eSachartre static int 136417cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len) 136587a7269eSachartre { 136687a7269eSachartre struct uscsi_cmd ucmd; 136787a7269eSachartre union scsi_cdb cdb; 136887a7269eSachartre int nsectors, nblk; 136987a7269eSachartre int max_sectors; 137087a7269eSachartre int status, rval; 137187a7269eSachartre 137287a7269eSachartre ASSERT(!vd->file); 13731aff8f07SAlexandre Chartre ASSERT(!vd->volume); 137417cadca8Slm66018 ASSERT(vd->vdisk_block_size > 0); 137587a7269eSachartre 137687a7269eSachartre max_sectors = vd->max_xfer_sz; 137717cadca8Slm66018 nblk = (len / vd->vdisk_block_size); 137887a7269eSachartre 137917cadca8Slm66018 if (len % vd->vdisk_block_size != 0) 138087a7269eSachartre return (EINVAL); 138187a7269eSachartre 138287a7269eSachartre /* 138387a7269eSachartre * Build and execute the uscsi ioctl. We build a group0, group1 138487a7269eSachartre * or group4 command as necessary, since some targets 138587a7269eSachartre * do not support group1 commands. 138687a7269eSachartre */ 138787a7269eSachartre while (nblk) { 138887a7269eSachartre 138987a7269eSachartre bzero(&ucmd, sizeof (ucmd)); 139087a7269eSachartre bzero(&cdb, sizeof (cdb)); 139187a7269eSachartre 139287a7269eSachartre nsectors = (max_sectors < nblk) ? max_sectors : nblk; 139387a7269eSachartre 139417cadca8Slm66018 /* 139517cadca8Slm66018 * Some of the optical drives on sun4v machines are ATAPI 139617cadca8Slm66018 * devices which use Group 1 Read/Write commands so we need 139717cadca8Slm66018 * to explicitly check a flag which is set when a domain 139817cadca8Slm66018 * is bound. 139917cadca8Slm66018 */ 140017cadca8Slm66018 if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) { 140187a7269eSachartre FORMG0ADDR(&cdb, blk); 1402342440ecSPrasad Singamsetty FORMG0COUNT(&cdb, (uchar_t)nsectors); 140387a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP0; 140487a7269eSachartre } else if (blk > 0xffffffff) { 140587a7269eSachartre FORMG4LONGADDR(&cdb, blk); 140687a7269eSachartre FORMG4COUNT(&cdb, nsectors); 140787a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP4; 140887a7269eSachartre cdb.scc_cmd |= SCMD_GROUP4; 140987a7269eSachartre } else { 141087a7269eSachartre FORMG1ADDR(&cdb, blk); 141187a7269eSachartre FORMG1COUNT(&cdb, nsectors); 141287a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP1; 141387a7269eSachartre cdb.scc_cmd |= SCMD_GROUP1; 141487a7269eSachartre } 141587a7269eSachartre ucmd.uscsi_cdb = (caddr_t)&cdb; 141687a7269eSachartre ucmd.uscsi_bufaddr = data; 141717cadca8Slm66018 ucmd.uscsi_buflen = nsectors * vd->block_size; 141887a7269eSachartre ucmd.uscsi_timeout = vd_scsi_rdwr_timeout; 141987a7269eSachartre /* 142087a7269eSachartre * Set flags so that the command is isolated from normal 142187a7269eSachartre * commands and no error message is printed. 142287a7269eSachartre */ 142387a7269eSachartre ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT; 142487a7269eSachartre 142587a7269eSachartre if (operation == VD_OP_BREAD) { 142687a7269eSachartre cdb.scc_cmd |= SCMD_READ; 142787a7269eSachartre ucmd.uscsi_flags |= USCSI_READ; 142887a7269eSachartre } else { 142987a7269eSachartre cdb.scc_cmd |= SCMD_WRITE; 143087a7269eSachartre } 143187a7269eSachartre 143287a7269eSachartre status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE], 1433047ba61eSachartre USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL), 143487a7269eSachartre kcred, &rval); 143587a7269eSachartre 143687a7269eSachartre if (status == 0) 143787a7269eSachartre status = ucmd.uscsi_status; 143887a7269eSachartre 143987a7269eSachartre if (status != 0) 144087a7269eSachartre break; 144187a7269eSachartre 144287a7269eSachartre /* 144387a7269eSachartre * Check if partial DMA breakup is required. If so, reduce 144487a7269eSachartre * the request size by half and retry the last request. 144587a7269eSachartre */ 144687a7269eSachartre if (ucmd.uscsi_resid == ucmd.uscsi_buflen) { 144787a7269eSachartre max_sectors >>= 1; 144887a7269eSachartre if (max_sectors <= 0) { 144987a7269eSachartre status = EIO; 145087a7269eSachartre break; 145187a7269eSachartre } 145287a7269eSachartre continue; 145387a7269eSachartre } 145487a7269eSachartre 145587a7269eSachartre if (ucmd.uscsi_resid != 0) { 145687a7269eSachartre status = EIO; 145787a7269eSachartre break; 145887a7269eSachartre } 145987a7269eSachartre 146087a7269eSachartre blk += nsectors; 146187a7269eSachartre nblk -= nsectors; 146217cadca8Slm66018 data += nsectors * vd->vdisk_block_size; /* SECSIZE */ 146387a7269eSachartre } 146487a7269eSachartre 146587a7269eSachartre return (status); 146687a7269eSachartre } 146787a7269eSachartre 1468205eeb1aSlm66018 /* 146917cadca8Slm66018 * Function: 147017cadca8Slm66018 * vd_scsi_rdwr 147117cadca8Slm66018 * 147217cadca8Slm66018 * Description: 147317cadca8Slm66018 * Wrapper function to read or write to a SCSI disk using an absolute 147417cadca8Slm66018 * disk offset. It checks the blocksize of the underlying device and, 147517cadca8Slm66018 * if necessary, adjusts the buffers accordingly before calling 147617cadca8Slm66018 * vd_do_scsi_rdwr() to do the actual read or write. 147717cadca8Slm66018 * 147817cadca8Slm66018 * Parameters: 147917cadca8Slm66018 * vd - disk on which the operation is performed. 148017cadca8Slm66018 * operation - operation to execute: read (VD_OP_BREAD) or 148117cadca8Slm66018 * write (VD_OP_BWRITE). 148217cadca8Slm66018 * data - buffer where data are read to or written from. 148317cadca8Slm66018 * blk - starting block for the operation. 148417cadca8Slm66018 * len - number of bytes to read or write. 148517cadca8Slm66018 * 148617cadca8Slm66018 * Return Code: 148717cadca8Slm66018 * 0 - success 148817cadca8Slm66018 * n != 0 - error. 148917cadca8Slm66018 */ 149017cadca8Slm66018 static int 149117cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen) 149217cadca8Slm66018 { 149317cadca8Slm66018 int rv; 149417cadca8Slm66018 149517cadca8Slm66018 size_t pblk; /* physical device block number of data on device */ 149617cadca8Slm66018 size_t delta; /* relative offset between pblk and vblk */ 149717cadca8Slm66018 size_t pnblk; /* number of physical blocks to be read from device */ 149817cadca8Slm66018 size_t plen; /* length of data to be read from physical device */ 149917cadca8Slm66018 char *buf; /* buffer area to fit physical device's block size */ 150017cadca8Slm66018 15012f5224aeSachartre if (vd->block_size == 0) { 15022f5224aeSachartre /* 15032f5224aeSachartre * The block size was not available during the attach, 15042f5224aeSachartre * try to update it now. 15052f5224aeSachartre */ 1506de3a5331SRamesh Chitrothu if (vd_backend_check_size(vd) != 0) 15072f5224aeSachartre return (EIO); 15082f5224aeSachartre } 15092f5224aeSachartre 151017cadca8Slm66018 /* 151117cadca8Slm66018 * If the vdisk block size and the block size of the underlying device 151217cadca8Slm66018 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need 151317cadca8Slm66018 * to create a buffer large enough to handle the device's block size 151417cadca8Slm66018 * and adjust the block to be read from and the amount of data to 151517cadca8Slm66018 * read to correspond with the device's block size. 151617cadca8Slm66018 */ 151717cadca8Slm66018 if (vd->vdisk_block_size == vd->block_size) 151817cadca8Slm66018 return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen)); 151917cadca8Slm66018 152017cadca8Slm66018 if (vd->vdisk_block_size > vd->block_size) 152117cadca8Slm66018 return (EINVAL); 152217cadca8Slm66018 152317cadca8Slm66018 /* 152417cadca8Slm66018 * Writing of physical block sizes larger than the virtual block size 152517cadca8Slm66018 * is not supported. This would be added if/when support for guests 152617cadca8Slm66018 * writing to DVDs is implemented. 152717cadca8Slm66018 */ 152817cadca8Slm66018 if (operation == VD_OP_BWRITE) 152917cadca8Slm66018 return (ENOTSUP); 153017cadca8Slm66018 153117cadca8Slm66018 /* BEGIN CSTYLED */ 153217cadca8Slm66018 /* 153317cadca8Slm66018 * Below is a diagram showing the relationship between the physical 153417cadca8Slm66018 * and virtual blocks. If the virtual blocks marked by 'X' below are 153517cadca8Slm66018 * requested, then the physical blocks denoted by 'Y' are read. 153617cadca8Slm66018 * 153717cadca8Slm66018 * vblk 153817cadca8Slm66018 * | vlen 153917cadca8Slm66018 * |<--------------->| 154017cadca8Slm66018 * v v 154117cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- virtual disk: 154217cadca8Slm66018 * | | | |XX|XX|XX|XX|XX|XX| | | | | | } block size is 154317cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- vd->vdisk_block_size 154417cadca8Slm66018 * : : : : 154517cadca8Slm66018 * >:==:< delta : : 154617cadca8Slm66018 * : : : : 154717cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- physical disk: 154817cadca8Slm66018 * | |YY:YY|YYYYY|YYYYY|YY:YY| | | } block size is 154917cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- vd->block_size 155017cadca8Slm66018 * ^ ^ 155117cadca8Slm66018 * |<--------------------->| 155217cadca8Slm66018 * | plen 155317cadca8Slm66018 * pblk 155417cadca8Slm66018 */ 155517cadca8Slm66018 /* END CSTYLED */ 155617cadca8Slm66018 pblk = (vblk * vd->vdisk_block_size) / vd->block_size; 155717cadca8Slm66018 delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size); 155817cadca8Slm66018 pnblk = ((delta + vlen - 1) / vd->block_size) + 1; 155917cadca8Slm66018 plen = pnblk * vd->block_size; 156017cadca8Slm66018 156117cadca8Slm66018 PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen); 156217cadca8Slm66018 156317cadca8Slm66018 buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP); 156417cadca8Slm66018 rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen); 156517cadca8Slm66018 bcopy(buf + delta, data, vlen); 156617cadca8Slm66018 156717cadca8Slm66018 kmem_free(buf, sizeof (caddr_t) * plen); 156817cadca8Slm66018 156917cadca8Slm66018 return (rv); 157017cadca8Slm66018 } 157117cadca8Slm66018 157217cadca8Slm66018 /* 1573bae9e67eSachartre * Function: 1574bae9e67eSachartre * vd_slice_flabel_read 1575bae9e67eSachartre * 1576bae9e67eSachartre * Description: 1577bae9e67eSachartre * This function simulates a read operation from the fake label of 1578bae9e67eSachartre * a single-slice disk. 1579bae9e67eSachartre * 1580bae9e67eSachartre * Parameters: 1581bae9e67eSachartre * vd - single-slice disk to read from 1582bae9e67eSachartre * data - buffer where data should be read to 1583bae9e67eSachartre * offset - offset in byte where the read should start 1584bae9e67eSachartre * length - number of bytes to read 1585bae9e67eSachartre * 1586bae9e67eSachartre * Return Code: 1587bae9e67eSachartre * n >= 0 - success, n indicates the number of bytes read 1588bae9e67eSachartre * -1 - error 1589bae9e67eSachartre */ 1590bae9e67eSachartre static ssize_t 1591bae9e67eSachartre vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length) 1592bae9e67eSachartre { 1593bae9e67eSachartre size_t n = 0; 1594bae9e67eSachartre uint_t limit = vd->flabel_limit * DEV_BSIZE; 1595bae9e67eSachartre 1596bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1597bae9e67eSachartre ASSERT(vd->flabel != NULL); 1598bae9e67eSachartre 1599bae9e67eSachartre /* if offset is past the fake label limit there's nothing to read */ 1600bae9e67eSachartre if (offset >= limit) 1601bae9e67eSachartre return (0); 1602bae9e67eSachartre 1603bae9e67eSachartre /* data with offset 0 to flabel_size are read from flabel */ 1604bae9e67eSachartre if (offset < vd->flabel_size) { 1605bae9e67eSachartre 1606bae9e67eSachartre if (offset + length <= vd->flabel_size) { 1607bae9e67eSachartre bcopy(vd->flabel + offset, data, length); 1608bae9e67eSachartre return (length); 1609bae9e67eSachartre } 1610bae9e67eSachartre 1611bae9e67eSachartre n = vd->flabel_size - offset; 1612bae9e67eSachartre bcopy(vd->flabel + offset, data, n); 1613bae9e67eSachartre data += n; 1614bae9e67eSachartre } 1615bae9e67eSachartre 1616bae9e67eSachartre /* data with offset from flabel_size to flabel_limit are all zeros */ 1617bae9e67eSachartre if (offset + length <= limit) { 1618bae9e67eSachartre bzero(data, length - n); 1619bae9e67eSachartre return (length); 1620bae9e67eSachartre } 1621bae9e67eSachartre 1622bae9e67eSachartre bzero(data, limit - offset - n); 1623bae9e67eSachartre return (limit - offset); 1624bae9e67eSachartre } 1625bae9e67eSachartre 1626bae9e67eSachartre /* 1627bae9e67eSachartre * Function: 1628bae9e67eSachartre * vd_slice_flabel_write 1629bae9e67eSachartre * 1630bae9e67eSachartre * Description: 1631bae9e67eSachartre * This function simulates a write operation to the fake label of 1632bae9e67eSachartre * a single-slice disk. Write operations are actually faked and return 1633bae9e67eSachartre * success although the label is never changed. This is mostly to 1634bae9e67eSachartre * simulate a successful label update. 1635bae9e67eSachartre * 1636bae9e67eSachartre * Parameters: 1637bae9e67eSachartre * vd - single-slice disk to write to 1638bae9e67eSachartre * data - buffer where data should be written from 1639bae9e67eSachartre * offset - offset in byte where the write should start 1640bae9e67eSachartre * length - number of bytes to written 1641bae9e67eSachartre * 1642bae9e67eSachartre * Return Code: 1643bae9e67eSachartre * n >= 0 - success, n indicates the number of bytes written 1644bae9e67eSachartre * -1 - error 1645bae9e67eSachartre */ 1646bae9e67eSachartre static ssize_t 1647bae9e67eSachartre vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length) 1648bae9e67eSachartre { 1649bae9e67eSachartre uint_t limit = vd->flabel_limit * DEV_BSIZE; 1650bae9e67eSachartre struct dk_label *label; 1651bae9e67eSachartre struct dk_geom geom; 1652342440ecSPrasad Singamsetty struct extvtoc vtoc; 1653bae9e67eSachartre 1654bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1655bae9e67eSachartre ASSERT(vd->flabel != NULL); 1656bae9e67eSachartre 1657bae9e67eSachartre if (offset >= limit) 1658bae9e67eSachartre return (0); 1659bae9e67eSachartre 1660bae9e67eSachartre /* 1661bae9e67eSachartre * If this is a request to overwrite the VTOC disk label, check that 1662bae9e67eSachartre * the new label is similar to the previous one and return that the 1663bae9e67eSachartre * write was successful, but note that nothing is actually overwritten. 1664bae9e67eSachartre */ 1665bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1666bae9e67eSachartre offset == 0 && length == DEV_BSIZE) { 1667342440ecSPrasad Singamsetty label = (void *)data; 1668bae9e67eSachartre 1669bae9e67eSachartre /* check that this is a valid label */ 1670bae9e67eSachartre if (label->dkl_magic != DKL_MAGIC || 1671bae9e67eSachartre label->dkl_cksum != vd_lbl2cksum(label)) 1672bae9e67eSachartre return (-1); 1673bae9e67eSachartre 1674bae9e67eSachartre /* check the vtoc and geometry */ 1675bae9e67eSachartre vd_label_to_vtocgeom(label, &vtoc, &geom); 1676bae9e67eSachartre if (vd_slice_geom_isvalid(vd, &geom) && 1677bae9e67eSachartre vd_slice_vtoc_isvalid(vd, &vtoc)) 1678bae9e67eSachartre return (length); 1679bae9e67eSachartre } 1680bae9e67eSachartre 1681bae9e67eSachartre /* fail any other write */ 1682bae9e67eSachartre return (-1); 1683bae9e67eSachartre } 1684bae9e67eSachartre 1685bae9e67eSachartre /* 1686bae9e67eSachartre * Function: 1687bae9e67eSachartre * vd_slice_fake_rdwr 1688bae9e67eSachartre * 1689bae9e67eSachartre * Description: 1690bae9e67eSachartre * This function simulates a raw read or write operation to a single-slice 1691bae9e67eSachartre * disk. It only handles the faked part of the operation i.e. I/Os to 1692bae9e67eSachartre * blocks which have no mapping with the vdisk backend (I/Os to the 1693bae9e67eSachartre * beginning and to the end of the vdisk). 1694bae9e67eSachartre * 1695bae9e67eSachartre * The function returns 0 is the operation is completed and it has been 1696bae9e67eSachartre * entirely handled as a fake read or write. In that case, lengthp points 1697bae9e67eSachartre * to the number of bytes not read or written. Values returned by datap 1698bae9e67eSachartre * and blkp are undefined. 1699bae9e67eSachartre * 1700bae9e67eSachartre * If the fake operation has succeeded but the read or write is not 1701bae9e67eSachartre * complete (i.e. the read/write operation extends beyond the blocks 1702bae9e67eSachartre * we fake) then the function returns EAGAIN and datap, blkp and lengthp 1703bae9e67eSachartre * pointers points to the parameters for completing the operation. 1704bae9e67eSachartre * 1705bae9e67eSachartre * In case of an error, for example if the slice is empty or parameters 1706bae9e67eSachartre * are invalid, then the function returns a non-zero value different 1707bae9e67eSachartre * from EAGAIN. In that case, the returned values of datap, blkp and 1708bae9e67eSachartre * lengthp are undefined. 1709bae9e67eSachartre * 1710bae9e67eSachartre * Parameters: 1711bae9e67eSachartre * vd - single-slice disk on which the operation is performed 1712bae9e67eSachartre * slice - slice on which the operation is performed, 1713bae9e67eSachartre * VD_SLICE_NONE indicates that the operation 1714bae9e67eSachartre * is done using an absolute disk offset. 1715bae9e67eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 1716bae9e67eSachartre * write (VD_OP_BWRITE). 1717bae9e67eSachartre * datap - pointer to the buffer where data are read to 1718bae9e67eSachartre * or written from. Return the pointer where remaining 1719bae9e67eSachartre * data have to be read to or written from. 1720bae9e67eSachartre * blkp - pointer to the starting block for the operation. 1721bae9e67eSachartre * Return the starting block relative to the vdisk 1722bae9e67eSachartre * backend for the remaining operation. 1723bae9e67eSachartre * lengthp - pointer to the number of bytes to read or write. 1724bae9e67eSachartre * This should be a multiple of DEV_BSIZE. Return the 1725bae9e67eSachartre * remaining number of bytes to read or write. 1726bae9e67eSachartre * 1727bae9e67eSachartre * Return Code: 1728bae9e67eSachartre * 0 - read/write operation is completed 1729bae9e67eSachartre * EAGAIN - read/write operation is not completed 1730bae9e67eSachartre * other values - error 1731bae9e67eSachartre */ 1732bae9e67eSachartre static int 1733bae9e67eSachartre vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap, 1734bae9e67eSachartre size_t *blkp, size_t *lengthp) 1735bae9e67eSachartre { 1736bae9e67eSachartre struct dk_label *label; 1737bae9e67eSachartre caddr_t data; 1738bae9e67eSachartre size_t blk, length, csize; 1739bae9e67eSachartre size_t ablk, asize, aoff, alen; 1740bae9e67eSachartre ssize_t n; 1741bae9e67eSachartre int sec, status; 1742bae9e67eSachartre 1743bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1744bae9e67eSachartre ASSERT(slice != 0); 1745bae9e67eSachartre 1746bae9e67eSachartre data = *datap; 1747bae9e67eSachartre blk = *blkp; 1748bae9e67eSachartre length = *lengthp; 1749bae9e67eSachartre 1750bae9e67eSachartre /* 1751bae9e67eSachartre * If this is not a raw I/O or an I/O from a full disk slice then 1752bae9e67eSachartre * this is an I/O to/from an empty slice. 1753bae9e67eSachartre */ 1754bae9e67eSachartre if (slice != VD_SLICE_NONE && 1755bae9e67eSachartre (slice != VD_ENTIRE_DISK_SLICE || 1756bae9e67eSachartre vd->vdisk_label != VD_DISK_LABEL_VTOC) && 1757bae9e67eSachartre (slice != VD_EFI_WD_SLICE || 1758bae9e67eSachartre vd->vdisk_label != VD_DISK_LABEL_EFI)) { 1759bae9e67eSachartre return (EIO); 1760bae9e67eSachartre } 1761bae9e67eSachartre 1762bae9e67eSachartre if (length % DEV_BSIZE != 0) 1763bae9e67eSachartre return (EINVAL); 1764bae9e67eSachartre 1765bae9e67eSachartre /* handle any I/O with the fake label */ 1766bae9e67eSachartre if (operation == VD_OP_BWRITE) 1767bae9e67eSachartre n = vd_slice_flabel_write(vd, data, blk * DEV_BSIZE, length); 1768bae9e67eSachartre else 1769bae9e67eSachartre n = vd_slice_flabel_read(vd, data, blk * DEV_BSIZE, length); 1770bae9e67eSachartre 1771bae9e67eSachartre if (n == -1) 1772bae9e67eSachartre return (EINVAL); 1773bae9e67eSachartre 1774bae9e67eSachartre ASSERT(n % DEV_BSIZE == 0); 1775bae9e67eSachartre 1776bae9e67eSachartre /* adjust I/O arguments */ 1777bae9e67eSachartre data += n; 1778bae9e67eSachartre blk += n / DEV_BSIZE; 1779bae9e67eSachartre length -= n; 1780bae9e67eSachartre 1781bae9e67eSachartre /* check if there's something else to process */ 1782bae9e67eSachartre if (length == 0) { 1783bae9e67eSachartre status = 0; 1784bae9e67eSachartre goto done; 1785bae9e67eSachartre } 1786bae9e67eSachartre 1787bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1788bae9e67eSachartre slice == VD_ENTIRE_DISK_SLICE) { 1789bae9e67eSachartre status = EAGAIN; 1790bae9e67eSachartre goto done; 1791bae9e67eSachartre } 1792bae9e67eSachartre 1793bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1794bae9e67eSachartre asize = EFI_MIN_RESV_SIZE + 33; 1795bae9e67eSachartre ablk = vd->vdisk_size - asize; 1796bae9e67eSachartre } else { 1797bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 1798bae9e67eSachartre ASSERT(vd->dk_geom.dkg_apc == 0); 1799bae9e67eSachartre 1800bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 1801bae9e67eSachartre ablk = vd->dk_geom.dkg_ncyl * csize; 1802bae9e67eSachartre asize = vd->dk_geom.dkg_acyl * csize; 1803bae9e67eSachartre } 1804bae9e67eSachartre 1805bae9e67eSachartre alen = length / DEV_BSIZE; 1806bae9e67eSachartre aoff = blk; 1807bae9e67eSachartre 1808bae9e67eSachartre /* if we have reached the last block then the I/O is completed */ 1809bae9e67eSachartre if (aoff == ablk + asize) { 1810bae9e67eSachartre status = 0; 1811bae9e67eSachartre goto done; 1812bae9e67eSachartre } 1813bae9e67eSachartre 1814bae9e67eSachartre /* if we are past the last block then return an error */ 1815bae9e67eSachartre if (aoff > ablk + asize) 1816bae9e67eSachartre return (EIO); 1817bae9e67eSachartre 1818bae9e67eSachartre /* check if there is any I/O to end of the disk */ 1819bae9e67eSachartre if (aoff + alen < ablk) { 1820bae9e67eSachartre status = EAGAIN; 1821bae9e67eSachartre goto done; 1822bae9e67eSachartre } 1823bae9e67eSachartre 1824bae9e67eSachartre /* we don't allow any write to the end of the disk */ 1825bae9e67eSachartre if (operation == VD_OP_BWRITE) 1826bae9e67eSachartre return (EIO); 1827bae9e67eSachartre 1828bae9e67eSachartre if (aoff < ablk) { 1829bae9e67eSachartre alen -= (ablk - aoff); 1830bae9e67eSachartre aoff = ablk; 1831bae9e67eSachartre } 1832bae9e67eSachartre 1833bae9e67eSachartre if (aoff + alen > ablk + asize) { 1834bae9e67eSachartre alen = ablk + asize - aoff; 1835bae9e67eSachartre } 1836bae9e67eSachartre 1837bae9e67eSachartre alen *= DEV_BSIZE; 1838bae9e67eSachartre 1839bae9e67eSachartre if (operation == VD_OP_BREAD) { 1840bae9e67eSachartre bzero(data + (aoff - blk) * DEV_BSIZE, alen); 1841bae9e67eSachartre 1842bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 1843bae9e67eSachartre /* check if we read backup labels */ 1844bae9e67eSachartre label = VD_LABEL_VTOC(vd); 1845bae9e67eSachartre ablk += (label->dkl_acyl - 1) * csize + 1846bae9e67eSachartre (label->dkl_nhead - 1) * label->dkl_nsect; 1847bae9e67eSachartre 1848bae9e67eSachartre for (sec = 1; (sec < 5 * 2 + 1); sec += 2) { 1849bae9e67eSachartre 1850bae9e67eSachartre if (ablk + sec >= blk && 1851bae9e67eSachartre ablk + sec < blk + (length / DEV_BSIZE)) { 1852bae9e67eSachartre bcopy(label, data + 1853bae9e67eSachartre (ablk + sec - blk) * DEV_BSIZE, 1854bae9e67eSachartre sizeof (struct dk_label)); 1855bae9e67eSachartre } 1856bae9e67eSachartre } 1857bae9e67eSachartre } 1858bae9e67eSachartre } 1859bae9e67eSachartre 1860bae9e67eSachartre length -= alen; 1861bae9e67eSachartre 1862bae9e67eSachartre status = (length == 0)? 0: EAGAIN; 1863bae9e67eSachartre 1864bae9e67eSachartre done: 1865bae9e67eSachartre ASSERT(length == 0 || blk >= vd->flabel_limit); 1866bae9e67eSachartre 1867bae9e67eSachartre /* 1868bae9e67eSachartre * Return the parameters for the remaining I/O. The starting block is 1869bae9e67eSachartre * adjusted so that it is relative to the vdisk backend. 1870bae9e67eSachartre */ 1871bae9e67eSachartre *datap = data; 1872bae9e67eSachartre *blkp = blk - vd->flabel_limit; 1873bae9e67eSachartre *lengthp = length; 1874bae9e67eSachartre 1875bae9e67eSachartre return (status); 1876bae9e67eSachartre } 1877bae9e67eSachartre 187883990c4aSAlexandre Chartre static int 187983990c4aSAlexandre Chartre vd_flush_write(vd_t *vd) 188083990c4aSAlexandre Chartre { 188183990c4aSAlexandre Chartre int status, rval; 188283990c4aSAlexandre Chartre 188383990c4aSAlexandre Chartre if (vd->file) { 188483990c4aSAlexandre Chartre status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL); 188583990c4aSAlexandre Chartre } else { 188683990c4aSAlexandre Chartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCFLUSHWRITECACHE, 188783990c4aSAlexandre Chartre NULL, vd->open_flags | FKIOCTL, kcred, &rval); 188883990c4aSAlexandre Chartre } 188983990c4aSAlexandre Chartre 189083990c4aSAlexandre Chartre return (status); 189183990c4aSAlexandre Chartre } 189283990c4aSAlexandre Chartre 189383990c4aSAlexandre Chartre static void 189483990c4aSAlexandre Chartre vd_bio_task(void *arg) 189583990c4aSAlexandre Chartre { 189683990c4aSAlexandre Chartre struct buf *buf = (struct buf *)arg; 189783990c4aSAlexandre Chartre vd_task_t *task = (vd_task_t *)buf->b_private; 189883990c4aSAlexandre Chartre vd_t *vd = task->vd; 189983990c4aSAlexandre Chartre ssize_t resid; 190083990c4aSAlexandre Chartre int status; 190183990c4aSAlexandre Chartre 190283990c4aSAlexandre Chartre if (vd->zvol) { 190383990c4aSAlexandre Chartre 190483990c4aSAlexandre Chartre status = ldi_strategy(vd->ldi_handle[0], buf); 190583990c4aSAlexandre Chartre 190683990c4aSAlexandre Chartre } else { 190783990c4aSAlexandre Chartre 190883990c4aSAlexandre Chartre ASSERT(vd->file); 190983990c4aSAlexandre Chartre 191083990c4aSAlexandre Chartre status = vn_rdwr((buf->b_flags & B_READ)? UIO_READ : UIO_WRITE, 191183990c4aSAlexandre Chartre vd->file_vnode, buf->b_un.b_addr, buf->b_bcount, 191283990c4aSAlexandre Chartre buf->b_lblkno * DEV_BSIZE, UIO_SYSSPACE, 0, 191383990c4aSAlexandre Chartre RLIM64_INFINITY, kcred, &resid); 191483990c4aSAlexandre Chartre 191583990c4aSAlexandre Chartre if (status == 0) { 191683990c4aSAlexandre Chartre buf->b_resid = resid; 191783990c4aSAlexandre Chartre biodone(buf); 191883990c4aSAlexandre Chartre return; 191983990c4aSAlexandre Chartre } 192083990c4aSAlexandre Chartre } 192183990c4aSAlexandre Chartre 192283990c4aSAlexandre Chartre if (status != 0) { 192383990c4aSAlexandre Chartre bioerror(buf, status); 192483990c4aSAlexandre Chartre biodone(buf); 192583990c4aSAlexandre Chartre } 192683990c4aSAlexandre Chartre } 192783990c4aSAlexandre Chartre 1928bae9e67eSachartre /* 19291aff8f07SAlexandre Chartre * We define our own biodone function so that buffers used for 19301aff8f07SAlexandre Chartre * asynchronous writes are not released when biodone() is called. 19311aff8f07SAlexandre Chartre */ 19321aff8f07SAlexandre Chartre static int 19331aff8f07SAlexandre Chartre vd_biodone(struct buf *bp) 19341aff8f07SAlexandre Chartre { 19351aff8f07SAlexandre Chartre ASSERT((bp->b_flags & B_DONE) == 0); 19361aff8f07SAlexandre Chartre ASSERT(SEMA_HELD(&bp->b_sem)); 19371aff8f07SAlexandre Chartre 19381aff8f07SAlexandre Chartre bp->b_flags |= B_DONE; 19391aff8f07SAlexandre Chartre sema_v(&bp->b_io); 19401aff8f07SAlexandre Chartre 19411aff8f07SAlexandre Chartre return (0); 19421aff8f07SAlexandre Chartre } 19431aff8f07SAlexandre Chartre 19441aff8f07SAlexandre Chartre /* 1945205eeb1aSlm66018 * Return Values 1946205eeb1aSlm66018 * EINPROGRESS - operation was successfully started 1947205eeb1aSlm66018 * EIO - encountered LDC (aka. task error) 1948205eeb1aSlm66018 * 0 - operation completed successfully 1949205eeb1aSlm66018 * 1950205eeb1aSlm66018 * Side Effect 1951205eeb1aSlm66018 * sets request->status = <disk operation status> 1952205eeb1aSlm66018 */ 19531ae08745Sheppo static int 1954d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 19551ae08745Sheppo { 19564bac2208Snarayan int rv, status = 0; 1957d10e4ef2Snarayan vd_t *vd = task->vd; 1958d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 1959d10e4ef2Snarayan struct buf *buf = &task->buf; 19604bac2208Snarayan uint8_t mtype; 19613c96341aSnarayan int slice; 1962047ba61eSachartre char *bufaddr = 0; 1963047ba61eSachartre size_t buflen; 1964bae9e67eSachartre size_t offset, length, nbytes; 1965d10e4ef2Snarayan 1966d10e4ef2Snarayan ASSERT(vd != NULL); 1967d10e4ef2Snarayan ASSERT(request != NULL); 19683c96341aSnarayan 19693c96341aSnarayan slice = request->slice; 19703c96341aSnarayan 197187a7269eSachartre ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices); 1972d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 1973d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 1974d10e4ef2Snarayan 1975205eeb1aSlm66018 if (request->nbytes == 0) { 1976205eeb1aSlm66018 /* no service for trivial requests */ 1977205eeb1aSlm66018 request->status = EINVAL; 1978205eeb1aSlm66018 return (0); 1979205eeb1aSlm66018 } 19801ae08745Sheppo 1981d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 1982d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 1983d10e4ef2Snarayan request->nbytes, request->addr); 19841ae08745Sheppo 1985047ba61eSachartre /* 1986047ba61eSachartre * We have to check the open flags because the functions processing 1987047ba61eSachartre * the read/write request will not do it. 1988047ba61eSachartre */ 1989047ba61eSachartre if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) { 1990047ba61eSachartre PR0("write fails because backend is opened read-only"); 1991047ba61eSachartre request->nbytes = 0; 1992047ba61eSachartre request->status = EROFS; 1993047ba61eSachartre return (0); 1994047ba61eSachartre } 1995d10e4ef2Snarayan 19964bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 19974bac2208Snarayan 19984bac2208Snarayan /* Map memory exported by client */ 19994bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 20004bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 2001047ba61eSachartre &bufaddr, NULL); 20024bac2208Snarayan if (status != 0) { 20033af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 2004205eeb1aSlm66018 return (EIO); 2005d10e4ef2Snarayan } 2006d10e4ef2Snarayan 2007bae9e67eSachartre /* 2008bae9e67eSachartre * The buffer size has to be 8-byte aligned, so the client should have 2009bae9e67eSachartre * sent a buffer which size is roundup to the next 8-byte aligned value. 2010bae9e67eSachartre */ 2011bae9e67eSachartre buflen = P2ROUNDUP(request->nbytes, 8); 2012047ba61eSachartre 2013047ba61eSachartre status = ldc_mem_acquire(task->mhdl, 0, buflen); 20144bac2208Snarayan if (status != 0) { 20154bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 20163af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 2017205eeb1aSlm66018 return (EIO); 20184bac2208Snarayan } 20194bac2208Snarayan 2020bae9e67eSachartre offset = request->addr; 2021bae9e67eSachartre nbytes = request->nbytes; 2022bae9e67eSachartre length = nbytes; 2023bae9e67eSachartre 2024bae9e67eSachartre /* default number of byte returned by the I/O */ 2025bae9e67eSachartre request->nbytes = 0; 2026bae9e67eSachartre 2027bae9e67eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 2028bae9e67eSachartre 2029bae9e67eSachartre if (slice != 0) { 2030bae9e67eSachartre /* handle any fake I/O */ 2031bae9e67eSachartre rv = vd_slice_fake_rdwr(vd, slice, request->operation, 2032bae9e67eSachartre &bufaddr, &offset, &length); 2033bae9e67eSachartre 2034bae9e67eSachartre /* record the number of bytes from the fake I/O */ 2035bae9e67eSachartre request->nbytes = nbytes - length; 2036bae9e67eSachartre 2037bae9e67eSachartre if (rv == 0) { 2038bae9e67eSachartre request->status = 0; 2039bae9e67eSachartre goto io_done; 2040bae9e67eSachartre } 2041bae9e67eSachartre 2042bae9e67eSachartre if (rv != EAGAIN) { 20433c96341aSnarayan request->nbytes = 0; 2044205eeb1aSlm66018 request->status = EIO; 2045bae9e67eSachartre goto io_done; 20463c96341aSnarayan } 2047bae9e67eSachartre 2048bae9e67eSachartre /* 2049bae9e67eSachartre * If we return with EAGAIN then this means that there 2050bae9e67eSachartre * are still data to read or write. 2051bae9e67eSachartre */ 2052bae9e67eSachartre ASSERT(length != 0); 2053bae9e67eSachartre 2054bae9e67eSachartre /* 2055bae9e67eSachartre * We need to continue the I/O from the slice backend to 2056bae9e67eSachartre * complete the request. The variables bufaddr, offset 2057bae9e67eSachartre * and length have been adjusted to have the right 2058bae9e67eSachartre * information to do the remaining I/O from the backend. 2059bae9e67eSachartre * The backend is entirely mapped to slice 0 so we just 2060bae9e67eSachartre * have to complete the I/O from that slice. 2061bae9e67eSachartre */ 2062bae9e67eSachartre slice = 0; 2063bae9e67eSachartre } 2064bae9e67eSachartre 206583990c4aSAlexandre Chartre } else if (vd->volume || vd->file) { 20661aff8f07SAlexandre Chartre 20671aff8f07SAlexandre Chartre rv = vd_dskimg_io_params(vd, slice, &offset, &length); 20681aff8f07SAlexandre Chartre if (rv != 0) { 20691aff8f07SAlexandre Chartre request->status = (rv == ENODATA)? 0: EIO; 20701aff8f07SAlexandre Chartre goto io_done; 20711aff8f07SAlexandre Chartre } 20721aff8f07SAlexandre Chartre slice = 0; 20731aff8f07SAlexandre Chartre 207483990c4aSAlexandre Chartre } else if (slice == VD_SLICE_NONE) { 2075bae9e67eSachartre 207687a7269eSachartre /* 207787a7269eSachartre * This is not a disk image so it is a real disk. We 207887a7269eSachartre * assume that the underlying device driver supports 207987a7269eSachartre * USCSICMD ioctls. This is the case of all SCSI devices 208087a7269eSachartre * (sd, ssd...). 208187a7269eSachartre * 208287a7269eSachartre * In the future if we have non-SCSI disks we would need 208387a7269eSachartre * to invoke the appropriate function to do I/O using an 208417cadca8Slm66018 * absolute disk offset (for example using DIOCTL_RWCMD 208587a7269eSachartre * for IDE disks). 208687a7269eSachartre */ 2087bae9e67eSachartre rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset, 2088bae9e67eSachartre length); 208987a7269eSachartre if (rv != 0) { 2090bae9e67eSachartre request->status = EIO; 2091bae9e67eSachartre } else { 2092bae9e67eSachartre request->nbytes = length; 2093bae9e67eSachartre request->status = 0; 2094bae9e67eSachartre } 2095bae9e67eSachartre goto io_done; 2096bae9e67eSachartre } 2097bae9e67eSachartre 2098bae9e67eSachartre /* Start the block I/O */ 2099047ba61eSachartre bioinit(buf); 2100047ba61eSachartre buf->b_flags = B_BUSY; 2101bae9e67eSachartre buf->b_bcount = length; 2102bae9e67eSachartre buf->b_lblkno = offset; 2103bae9e67eSachartre buf->b_bufsize = buflen; 2104047ba61eSachartre buf->b_edev = vd->dev[slice]; 2105047ba61eSachartre buf->b_un.b_addr = bufaddr; 21061aff8f07SAlexandre Chartre buf->b_iodone = vd_biodone; 21071aff8f07SAlexandre Chartre 210883990c4aSAlexandre Chartre if (vd->file || vd->zvol) { 210983990c4aSAlexandre Chartre /* 211083990c4aSAlexandre Chartre * I/O to a file are dispatched to an I/O queue, so that several 211183990c4aSAlexandre Chartre * I/Os can be processed in parallel. We also do that for ZFS 211283990c4aSAlexandre Chartre * volumes because the ZFS volume strategy() function will only 211383990c4aSAlexandre Chartre * return after the I/O is completed (instead of just starting 211483990c4aSAlexandre Chartre * the I/O). 211583990c4aSAlexandre Chartre */ 211683990c4aSAlexandre Chartre 21171aff8f07SAlexandre Chartre if (request->operation == VD_OP_BREAD) { 21181aff8f07SAlexandre Chartre buf->b_flags |= B_READ; 21191aff8f07SAlexandre Chartre } else { 21201aff8f07SAlexandre Chartre /* 212183990c4aSAlexandre Chartre * For ZFS volumes and files, we do an asynchronous 212283990c4aSAlexandre Chartre * write and we will wait for the completion of the 212383990c4aSAlexandre Chartre * write in vd_complete_bio() by flushing the volume 212483990c4aSAlexandre Chartre * or file. 212583990c4aSAlexandre Chartre * 212683990c4aSAlexandre Chartre * This done for performance reasons, so that we can 212783990c4aSAlexandre Chartre * group together several write requests into a single 212883990c4aSAlexandre Chartre * flush operation. 21291aff8f07SAlexandre Chartre */ 21301aff8f07SAlexandre Chartre buf->b_flags |= B_WRITE | B_ASYNC; 213183990c4aSAlexandre Chartre 213283990c4aSAlexandre Chartre /* 213383990c4aSAlexandre Chartre * We keep track of the write so that we can group 213483990c4aSAlexandre Chartre * requests when flushing. The write queue has the 213583990c4aSAlexandre Chartre * same number of slots as the dring so this prevents 213683990c4aSAlexandre Chartre * the write queue from wrapping and overwriting 213783990c4aSAlexandre Chartre * existing entries: if the write queue gets full 213883990c4aSAlexandre Chartre * then that means that the dring is full so we stop 213983990c4aSAlexandre Chartre * receiving new requests until an existing request 214083990c4aSAlexandre Chartre * is processed, removed from the write queue and 214183990c4aSAlexandre Chartre * then from the dring. 214283990c4aSAlexandre Chartre */ 214383990c4aSAlexandre Chartre task->write_index = vd->write_index; 214483990c4aSAlexandre Chartre vd->write_queue[task->write_index] = buf; 214583990c4aSAlexandre Chartre vd->write_index = 214683990c4aSAlexandre Chartre VD_WRITE_INDEX_NEXT(vd, vd->write_index); 214783990c4aSAlexandre Chartre } 214883990c4aSAlexandre Chartre 214983990c4aSAlexandre Chartre buf->b_private = task; 215083990c4aSAlexandre Chartre 215183990c4aSAlexandre Chartre ASSERT(vd->ioq != NULL); 215283990c4aSAlexandre Chartre 215383990c4aSAlexandre Chartre request->status = 0; 215483990c4aSAlexandre Chartre (void) ddi_taskq_dispatch(task->vd->ioq, vd_bio_task, buf, 215583990c4aSAlexandre Chartre DDI_SLEEP); 215683990c4aSAlexandre Chartre 215783990c4aSAlexandre Chartre } else { 215883990c4aSAlexandre Chartre 215983990c4aSAlexandre Chartre if (request->operation == VD_OP_BREAD) { 216083990c4aSAlexandre Chartre buf->b_flags |= B_READ; 216183990c4aSAlexandre Chartre } else { 21621aff8f07SAlexandre Chartre buf->b_flags |= B_WRITE; 21631aff8f07SAlexandre Chartre } 2164047ba61eSachartre 2165bae9e67eSachartre request->status = ldi_strategy(vd->ldi_handle[slice], buf); 216683990c4aSAlexandre Chartre } 2167205eeb1aSlm66018 2168205eeb1aSlm66018 /* 2169205eeb1aSlm66018 * This is to indicate to the caller that the request 2170205eeb1aSlm66018 * needs to be finished by vd_complete_bio() by calling 2171205eeb1aSlm66018 * biowait() there and waiting for that to return before 2172205eeb1aSlm66018 * triggering the notification of the vDisk client. 2173205eeb1aSlm66018 * 2174205eeb1aSlm66018 * This is necessary when writing to real disks as 2175205eeb1aSlm66018 * otherwise calls to ldi_strategy() would be serialized 2176205eeb1aSlm66018 * behind the calls to biowait() and performance would 2177205eeb1aSlm66018 * suffer. 2178205eeb1aSlm66018 */ 2179205eeb1aSlm66018 if (request->status == 0) 218087a7269eSachartre return (EINPROGRESS); 2181047ba61eSachartre 2182047ba61eSachartre biofini(buf); 21833c96341aSnarayan 2184bae9e67eSachartre io_done: 2185bae9e67eSachartre /* Clean up after error or completion */ 2186047ba61eSachartre rv = ldc_mem_release(task->mhdl, 0, buflen); 21874bac2208Snarayan if (rv) { 21883af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 2189205eeb1aSlm66018 status = EIO; 21904bac2208Snarayan } 21914bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 21924bac2208Snarayan if (rv) { 2193205eeb1aSlm66018 PR0("ldc_mem_unmap() returned err %d ", rv); 2194205eeb1aSlm66018 status = EIO; 21954bac2208Snarayan } 21964bac2208Snarayan 2197d10e4ef2Snarayan return (status); 2198d10e4ef2Snarayan } 2199d10e4ef2Snarayan 2200205eeb1aSlm66018 /* 2201205eeb1aSlm66018 * This function should only be called from vd_notify to ensure that requests 2202205eeb1aSlm66018 * are responded to in the order that they are received. 2203205eeb1aSlm66018 */ 2204d10e4ef2Snarayan static int 2205d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 2206d10e4ef2Snarayan { 22073af08d82Slm66018 int status; 2208d10e4ef2Snarayan size_t nbytes; 2209d10e4ef2Snarayan 22103af08d82Slm66018 do { 2211d10e4ef2Snarayan nbytes = msglen; 2212d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 22133af08d82Slm66018 if (status != EWOULDBLOCK) 22143af08d82Slm66018 break; 22153af08d82Slm66018 drv_usecwait(vds_ldc_delay); 22163af08d82Slm66018 } while (status == EWOULDBLOCK); 2217d10e4ef2Snarayan 2218d10e4ef2Snarayan if (status != 0) { 22193af08d82Slm66018 if (status != ECONNRESET) 22203af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 2221d10e4ef2Snarayan return (status); 2222d10e4ef2Snarayan } else if (nbytes != msglen) { 22233af08d82Slm66018 PR0("ldc_write() performed only partial write"); 2224d10e4ef2Snarayan return (EIO); 2225d10e4ef2Snarayan } 2226d10e4ef2Snarayan 2227d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 2228d10e4ef2Snarayan return (0); 2229d10e4ef2Snarayan } 2230d10e4ef2Snarayan 2231d10e4ef2Snarayan static void 2232d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 2233d10e4ef2Snarayan { 2234d10e4ef2Snarayan mutex_enter(&vd->lock); 2235d10e4ef2Snarayan vd->reset_state = B_TRUE; 2236d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 2237d10e4ef2Snarayan mutex_exit(&vd->lock); 2238d10e4ef2Snarayan } 2239d10e4ef2Snarayan 2240d10e4ef2Snarayan /* 2241d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 2242d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 22433af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 2244d10e4ef2Snarayan */ 2245d10e4ef2Snarayan static void 2246d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 2247d10e4ef2Snarayan { 2248d10e4ef2Snarayan int status = 0; 2249d10e4ef2Snarayan 2250d10e4ef2Snarayan mutex_enter(&vd->lock); 2251d10e4ef2Snarayan if (!vd->reset_state) { 2252d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 2253d10e4ef2Snarayan mutex_exit(&vd->lock); 2254d10e4ef2Snarayan return; 2255d10e4ef2Snarayan } 2256d10e4ef2Snarayan mutex_exit(&vd->lock); 2257d10e4ef2Snarayan 2258d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 2259d10e4ef2Snarayan 2260d10e4ef2Snarayan /* 2261d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 2262d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 2263d10e4ef2Snarayan * asynchronous tasks might set it 2264d10e4ef2Snarayan */ 226583990c4aSAlexandre Chartre if (vd->ioq != NULL) 226683990c4aSAlexandre Chartre ddi_taskq_wait(vd->ioq); 2267d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 2268d10e4ef2Snarayan 226983990c4aSAlexandre Chartre status = vd_flush_write(vd); 22703c96341aSnarayan if (status) { 227183990c4aSAlexandre Chartre PR0("flushwrite returned error %d", status); 22723c96341aSnarayan } 22733c96341aSnarayan 2274d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 2275d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 22763af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 2277d10e4ef2Snarayan 22783af08d82Slm66018 vd_free_dring_task(vd); 22793af08d82Slm66018 22803af08d82Slm66018 /* Free the staging buffer for msgs */ 22813af08d82Slm66018 if (vd->vio_msgp != NULL) { 22823af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 22833af08d82Slm66018 vd->vio_msgp = NULL; 2284d10e4ef2Snarayan } 2285d10e4ef2Snarayan 22863af08d82Slm66018 /* Free the inband message buffer */ 22873af08d82Slm66018 if (vd->inband_task.msg != NULL) { 22883af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 22893af08d82Slm66018 vd->inband_task.msg = NULL; 22903af08d82Slm66018 } 2291d10e4ef2Snarayan 2292d10e4ef2Snarayan mutex_enter(&vd->lock); 22933af08d82Slm66018 22943af08d82Slm66018 if (vd->reset_ldc) 22953af08d82Slm66018 PR0("taking down LDC channel"); 2296e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 22973af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 2298d10e4ef2Snarayan 22992f5224aeSachartre /* Reset exclusive access rights */ 23002f5224aeSachartre vd_reset_access(vd); 23012f5224aeSachartre 2302d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 2303d10e4ef2Snarayan vd->state = VD_STATE_INIT; 2304d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 2305d10e4ef2Snarayan 23063af08d82Slm66018 /* Allocate the staging buffer */ 23073af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 23083af08d82Slm66018 23093af08d82Slm66018 PR0("calling ldc_up\n"); 23103af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 23113af08d82Slm66018 2312d10e4ef2Snarayan vd->reset_state = B_FALSE; 2313d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 23143af08d82Slm66018 2315d10e4ef2Snarayan mutex_exit(&vd->lock); 2316d10e4ef2Snarayan } 2317d10e4ef2Snarayan 23183af08d82Slm66018 static void vd_recv_msg(void *arg); 23193af08d82Slm66018 23203af08d82Slm66018 static void 23213af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 23223af08d82Slm66018 { 23233af08d82Slm66018 int status; 23243af08d82Slm66018 23253af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 23263af08d82Slm66018 23273af08d82Slm66018 vd_need_reset(vd, B_FALSE); 23283af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 23293af08d82Slm66018 if (status == DDI_FAILURE) { 23303af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 23313af08d82Slm66018 vd_need_reset(vd, B_TRUE); 23323af08d82Slm66018 return; 23333af08d82Slm66018 } 23343af08d82Slm66018 } 23353af08d82Slm66018 2336d10e4ef2Snarayan static int 23373c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 2338d10e4ef2Snarayan { 2339d10e4ef2Snarayan boolean_t accepted; 2340d10e4ef2Snarayan int status; 2341bbfa0259Sha137994 on_trap_data_t otd; 2342d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 2343d10e4ef2Snarayan 23443af08d82Slm66018 if (vd->reset_state) 23453af08d82Slm66018 return (0); 2346d10e4ef2Snarayan 2347d10e4ef2Snarayan /* Acquire the element */ 2348bbfa0259Sha137994 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype, 2349bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 23503af08d82Slm66018 if (status == ECONNRESET) { 23513af08d82Slm66018 vd_mark_in_reset(vd); 23523af08d82Slm66018 return (0); 23533af08d82Slm66018 } else { 2354d10e4ef2Snarayan return (status); 2355d10e4ef2Snarayan } 23563af08d82Slm66018 } 2357d10e4ef2Snarayan 2358d10e4ef2Snarayan /* Set the element's status and mark it done */ 2359d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 2360d10e4ef2Snarayan if (accepted) { 23613c96341aSnarayan elem->payload.nbytes = elem_nbytes; 2362d10e4ef2Snarayan elem->payload.status = elem_status; 2363d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 2364d10e4ef2Snarayan } else { 2365d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 23663af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 2367d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 2368d10e4ef2Snarayan } 2369d10e4ef2Snarayan /* Release the element */ 2370bbfa0259Sha137994 if ((status = VIO_DRING_RELEASE(vd->dring_mtype, 2371bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 23723af08d82Slm66018 if (status == ECONNRESET) { 23733af08d82Slm66018 vd_mark_in_reset(vd); 23743af08d82Slm66018 return (0); 23753af08d82Slm66018 } else { 2376bbfa0259Sha137994 PR0("VIO_DRING_RELEASE() returned errno %d", 23773af08d82Slm66018 status); 2378d10e4ef2Snarayan return (status); 2379d10e4ef2Snarayan } 23803af08d82Slm66018 } 2381d10e4ef2Snarayan 2382d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 2383d10e4ef2Snarayan } 2384d10e4ef2Snarayan 2385205eeb1aSlm66018 /* 2386205eeb1aSlm66018 * Return Values 2387205eeb1aSlm66018 * 0 - operation completed successfully 2388205eeb1aSlm66018 * EIO - encountered LDC / task error 2389205eeb1aSlm66018 * 2390205eeb1aSlm66018 * Side Effect 2391205eeb1aSlm66018 * sets request->status = <disk operation status> 2392205eeb1aSlm66018 */ 2393205eeb1aSlm66018 static int 2394205eeb1aSlm66018 vd_complete_bio(vd_task_t *task) 2395d10e4ef2Snarayan { 2396d10e4ef2Snarayan int status = 0; 2397205eeb1aSlm66018 int rv = 0; 2398d10e4ef2Snarayan vd_t *vd = task->vd; 2399d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 2400d10e4ef2Snarayan struct buf *buf = &task->buf; 240183990c4aSAlexandre Chartre int wid, nwrites; 2402d10e4ef2Snarayan 2403d10e4ef2Snarayan 2404d10e4ef2Snarayan ASSERT(vd != NULL); 2405d10e4ef2Snarayan ASSERT(request != NULL); 2406d10e4ef2Snarayan ASSERT(task->msg != NULL); 2407d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 2408d10e4ef2Snarayan 240983990c4aSAlexandre Chartre if (buf->b_flags & B_DONE) { 24101aff8f07SAlexandre Chartre /* 241183990c4aSAlexandre Chartre * If the I/O is already done then we don't call biowait() 241283990c4aSAlexandre Chartre * because biowait() might already have been called when 241383990c4aSAlexandre Chartre * flushing a previous asynchronous write. So we just 241483990c4aSAlexandre Chartre * retrieve the status of the request. 24151aff8f07SAlexandre Chartre */ 241683990c4aSAlexandre Chartre request->status = geterror(buf); 24171aff8f07SAlexandre Chartre } else { 241883990c4aSAlexandre Chartre /* 241983990c4aSAlexandre Chartre * Wait for the I/O. For synchronous I/O, biowait() will return 242083990c4aSAlexandre Chartre * when the I/O has completed. For asynchronous write, it will 242183990c4aSAlexandre Chartre * return the write has been submitted to the backend, but it 242283990c4aSAlexandre Chartre * may not have been committed. 242383990c4aSAlexandre Chartre */ 2424d10e4ef2Snarayan request->status = biowait(buf); 24251aff8f07SAlexandre Chartre } 2426d10e4ef2Snarayan 242783990c4aSAlexandre Chartre if (buf->b_flags & B_ASYNC) { 242883990c4aSAlexandre Chartre /* 242983990c4aSAlexandre Chartre * Asynchronous writes are used when writing to a file or a 243083990c4aSAlexandre Chartre * ZFS volume. In that case the bio notification indicates 243183990c4aSAlexandre Chartre * that the write has started. We have to flush the backend 243283990c4aSAlexandre Chartre * to ensure that the write has been committed before marking 243383990c4aSAlexandre Chartre * the request as completed. 243483990c4aSAlexandre Chartre */ 243583990c4aSAlexandre Chartre ASSERT(task->request->operation == VD_OP_BWRITE); 243683990c4aSAlexandre Chartre 243783990c4aSAlexandre Chartre wid = task->write_index; 243883990c4aSAlexandre Chartre 243983990c4aSAlexandre Chartre /* check if write has been already flushed */ 244083990c4aSAlexandre Chartre if (vd->write_queue[wid] != NULL) { 244183990c4aSAlexandre Chartre 244283990c4aSAlexandre Chartre vd->write_queue[wid] = NULL; 244383990c4aSAlexandre Chartre wid = VD_WRITE_INDEX_NEXT(vd, wid); 244483990c4aSAlexandre Chartre 244583990c4aSAlexandre Chartre /* 244683990c4aSAlexandre Chartre * Because flushing is time consuming, it is worth 244783990c4aSAlexandre Chartre * waiting for any other writes so that they can be 244883990c4aSAlexandre Chartre * included in this single flush request. 244983990c4aSAlexandre Chartre */ 245083990c4aSAlexandre Chartre if (vd_awflush & VD_AWFLUSH_GROUP) { 245183990c4aSAlexandre Chartre nwrites = 1; 245283990c4aSAlexandre Chartre while (vd->write_queue[wid] != NULL) { 245383990c4aSAlexandre Chartre (void) biowait(vd->write_queue[wid]); 245483990c4aSAlexandre Chartre vd->write_queue[wid] = NULL; 245583990c4aSAlexandre Chartre wid = VD_WRITE_INDEX_NEXT(vd, wid); 245683990c4aSAlexandre Chartre nwrites++; 245783990c4aSAlexandre Chartre } 245883990c4aSAlexandre Chartre DTRACE_PROBE2(flushgrp, vd_task_t *, task, 245983990c4aSAlexandre Chartre int, nwrites); 246083990c4aSAlexandre Chartre } 246183990c4aSAlexandre Chartre 246283990c4aSAlexandre Chartre if (vd_awflush & VD_AWFLUSH_IMMEDIATE) { 246383990c4aSAlexandre Chartre request->status = vd_flush_write(vd); 246483990c4aSAlexandre Chartre } else if (vd_awflush & VD_AWFLUSH_DEFER) { 246583990c4aSAlexandre Chartre (void) taskq_dispatch(system_taskq, 246683990c4aSAlexandre Chartre (void (*)(void *))vd_flush_write, vd, 246783990c4aSAlexandre Chartre DDI_SLEEP); 246883990c4aSAlexandre Chartre request->status = 0; 246983990c4aSAlexandre Chartre } 247083990c4aSAlexandre Chartre } 247183990c4aSAlexandre Chartre } 247283990c4aSAlexandre Chartre 2473bae9e67eSachartre /* Update the number of bytes read/written */ 2474bae9e67eSachartre request->nbytes += buf->b_bcount - buf->b_resid; 24753c96341aSnarayan 24764bac2208Snarayan /* Release the buffer */ 24773af08d82Slm66018 if (!vd->reset_state) 2478bae9e67eSachartre status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize); 24794bac2208Snarayan if (status) { 24803af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 24813af08d82Slm66018 "client", status); 24823af08d82Slm66018 if (status == ECONNRESET) { 24833af08d82Slm66018 vd_mark_in_reset(vd); 24843af08d82Slm66018 } 2485205eeb1aSlm66018 rv = EIO; 24861ae08745Sheppo } 24871ae08745Sheppo 24883af08d82Slm66018 /* Unmap the memory, even if in reset */ 24894bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 24904bac2208Snarayan if (status) { 24913af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 24924bac2208Snarayan status); 24933af08d82Slm66018 if (status == ECONNRESET) { 24943af08d82Slm66018 vd_mark_in_reset(vd); 24953af08d82Slm66018 } 2496205eeb1aSlm66018 rv = EIO; 24974bac2208Snarayan } 24984bac2208Snarayan 2499d10e4ef2Snarayan biofini(buf); 25001ae08745Sheppo 2501205eeb1aSlm66018 return (rv); 2502205eeb1aSlm66018 } 2503205eeb1aSlm66018 2504205eeb1aSlm66018 /* 2505205eeb1aSlm66018 * Description: 2506205eeb1aSlm66018 * This function is called by the two functions called by a taskq 2507205eeb1aSlm66018 * [ vd_complete_notify() and vd_serial_notify()) ] to send the 2508205eeb1aSlm66018 * message to the client. 2509205eeb1aSlm66018 * 2510205eeb1aSlm66018 * Parameters: 2511205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2512205eeb1aSlm66018 * 2513205eeb1aSlm66018 * Return Values 2514205eeb1aSlm66018 * None 2515205eeb1aSlm66018 */ 2516205eeb1aSlm66018 static void 2517205eeb1aSlm66018 vd_notify(vd_task_t *task) 2518205eeb1aSlm66018 { 2519205eeb1aSlm66018 int status; 2520205eeb1aSlm66018 2521205eeb1aSlm66018 ASSERT(task != NULL); 2522205eeb1aSlm66018 ASSERT(task->vd != NULL); 2523205eeb1aSlm66018 2524205eeb1aSlm66018 /* 2525205eeb1aSlm66018 * Send the "ack" or "nack" back to the client; if sending the message 2526205eeb1aSlm66018 * via LDC fails, arrange to reset both the connection state and LDC 2527205eeb1aSlm66018 * itself 2528205eeb1aSlm66018 */ 2529205eeb1aSlm66018 PR2("Sending %s", 2530205eeb1aSlm66018 (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 2531205eeb1aSlm66018 2532205eeb1aSlm66018 status = send_msg(task->vd->ldc_handle, task->msg, task->msglen); 2533205eeb1aSlm66018 switch (status) { 2534205eeb1aSlm66018 case 0: 2535205eeb1aSlm66018 break; 2536205eeb1aSlm66018 case ECONNRESET: 2537205eeb1aSlm66018 vd_mark_in_reset(task->vd); 2538205eeb1aSlm66018 break; 2539205eeb1aSlm66018 default: 2540205eeb1aSlm66018 PR0("initiating full reset"); 2541205eeb1aSlm66018 vd_need_reset(task->vd, B_TRUE); 2542205eeb1aSlm66018 break; 2543205eeb1aSlm66018 } 2544205eeb1aSlm66018 2545205eeb1aSlm66018 DTRACE_PROBE1(task__end, vd_task_t *, task); 2546205eeb1aSlm66018 } 2547205eeb1aSlm66018 2548205eeb1aSlm66018 /* 2549205eeb1aSlm66018 * Description: 2550205eeb1aSlm66018 * Mark the Dring entry as Done and (if necessary) send an ACK/NACK to 2551205eeb1aSlm66018 * the vDisk client 2552205eeb1aSlm66018 * 2553205eeb1aSlm66018 * Parameters: 2554205eeb1aSlm66018 * task - structure containing the request sent from client 2555205eeb1aSlm66018 * 2556205eeb1aSlm66018 * Return Values 2557205eeb1aSlm66018 * None 2558205eeb1aSlm66018 */ 2559205eeb1aSlm66018 static void 2560205eeb1aSlm66018 vd_complete_notify(vd_task_t *task) 2561205eeb1aSlm66018 { 2562205eeb1aSlm66018 int status = 0; 2563205eeb1aSlm66018 vd_t *vd = task->vd; 2564205eeb1aSlm66018 vd_dring_payload_t *request = task->request; 2565205eeb1aSlm66018 2566d10e4ef2Snarayan /* Update the dring element for a dring client */ 2567f0ca1d9aSsb155480 if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 25683c96341aSnarayan status = vd_mark_elem_done(vd, task->index, 25693c96341aSnarayan request->status, request->nbytes); 25703af08d82Slm66018 if (status == ECONNRESET) 25713af08d82Slm66018 vd_mark_in_reset(vd); 2572bbfa0259Sha137994 else if (status == EACCES) 2573bbfa0259Sha137994 vd_need_reset(vd, B_TRUE); 25743af08d82Slm66018 } 25751ae08745Sheppo 2576d10e4ef2Snarayan /* 2577205eeb1aSlm66018 * If a transport error occurred while marking the element done or 2578205eeb1aSlm66018 * previously while executing the task, arrange to "nack" the message 2579205eeb1aSlm66018 * when the final task in the descriptor element range completes 2580d10e4ef2Snarayan */ 2581205eeb1aSlm66018 if ((status != 0) || (task->status != 0)) 2582d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 25831ae08745Sheppo 2584d10e4ef2Snarayan /* 2585d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 2586d10e4ef2Snarayan * free the message 2587d10e4ef2Snarayan */ 25883af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 2589d10e4ef2Snarayan return; 25903af08d82Slm66018 } 25911ae08745Sheppo 259227ac699dSzk194757 /* 259327ac699dSzk194757 * We should only send an ACK/NACK here if we are not currently in 259427ac699dSzk194757 * reset as, depending on how we reset, the dring may have been 259527ac699dSzk194757 * blown away and we don't want to ACK/NACK a message that isn't 259627ac699dSzk194757 * there. 259727ac699dSzk194757 */ 259827ac699dSzk194757 if (!vd->reset_state) 2599205eeb1aSlm66018 vd_notify(task); 2600205eeb1aSlm66018 } 2601205eeb1aSlm66018 2602d10e4ef2Snarayan /* 2603205eeb1aSlm66018 * Description: 2604205eeb1aSlm66018 * This is the basic completion function called to handle inband data 2605205eeb1aSlm66018 * requests and handshake messages. All it needs to do is trigger a 2606205eeb1aSlm66018 * message to the client that the request is completed. 2607205eeb1aSlm66018 * 2608205eeb1aSlm66018 * Parameters: 2609205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2610205eeb1aSlm66018 * 2611205eeb1aSlm66018 * Return Values 2612205eeb1aSlm66018 * None 2613d10e4ef2Snarayan */ 2614205eeb1aSlm66018 static void 2615205eeb1aSlm66018 vd_serial_notify(void *arg) 2616205eeb1aSlm66018 { 2617205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 2618205eeb1aSlm66018 2619205eeb1aSlm66018 ASSERT(task != NULL); 2620205eeb1aSlm66018 vd_notify(task); 26211ae08745Sheppo } 26221ae08745Sheppo 26232f5224aeSachartre /* ARGSUSED */ 26242f5224aeSachartre static int 26252f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 26260a55fbb7Slm66018 { 26270a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 26282f5224aeSachartre return (0); 26290a55fbb7Slm66018 } 26300a55fbb7Slm66018 26312f5224aeSachartre /* ARGSUSED */ 26322f5224aeSachartre static int 26332f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 26340a55fbb7Slm66018 { 2635342440ecSPrasad Singamsetty VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct extvtoc *)ioctl_arg); 26362f5224aeSachartre return (0); 26370a55fbb7Slm66018 } 26380a55fbb7Slm66018 26390a55fbb7Slm66018 static void 26400a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 26410a55fbb7Slm66018 { 26420a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 26430a55fbb7Slm66018 } 26440a55fbb7Slm66018 26450a55fbb7Slm66018 static void 26460a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 26470a55fbb7Slm66018 { 2648342440ecSPrasad Singamsetty VTOC2VD_VTOC((struct extvtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 26490a55fbb7Slm66018 } 26500a55fbb7Slm66018 26512f5224aeSachartre static int 26522f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 26534bac2208Snarayan { 26544bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 26554bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 26562f5224aeSachartre size_t data_len; 26572f5224aeSachartre 26582f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 26592f5224aeSachartre if (vd_efi->length > data_len) 26602f5224aeSachartre return (EINVAL); 26614bac2208Snarayan 26624bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 26634bac2208Snarayan dk_efi->dki_length = vd_efi->length; 26644bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 26652f5224aeSachartre return (0); 26664bac2208Snarayan } 26674bac2208Snarayan 26684bac2208Snarayan static void 26694bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 26704bac2208Snarayan { 26714bac2208Snarayan int len; 26724bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 26734bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 26744bac2208Snarayan 26754bac2208Snarayan len = vd_efi->length; 26764bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 26774bac2208Snarayan kmem_free(dk_efi->dki_data, len); 26784bac2208Snarayan } 26794bac2208Snarayan 26802f5224aeSachartre static int 26812f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 26824bac2208Snarayan { 26834bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 26844bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 26852f5224aeSachartre size_t data_len; 26862f5224aeSachartre 26872f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 26882f5224aeSachartre if (vd_efi->length > data_len) 26892f5224aeSachartre return (EINVAL); 26904bac2208Snarayan 26914bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 26924bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 26932f5224aeSachartre return (0); 26944bac2208Snarayan } 26954bac2208Snarayan 26964bac2208Snarayan static void 26974bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 26984bac2208Snarayan { 26994bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 27004bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 27014bac2208Snarayan 27024bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 27034bac2208Snarayan } 27044bac2208Snarayan 27052f5224aeSachartre static int 27062f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 27072f5224aeSachartre { 27082f5224aeSachartre size_t vd_scsi_len; 27092f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 27102f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 27112f5224aeSachartre 27122f5224aeSachartre /* check buffer size */ 27132f5224aeSachartre vd_scsi_len = VD_SCSI_SIZE; 27142f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t)); 27152f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t)); 27162f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t)); 27172f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t)); 27182f5224aeSachartre 27192f5224aeSachartre ASSERT(vd_scsi_len % sizeof (uint64_t) == 0); 27202f5224aeSachartre 27212f5224aeSachartre if (vd_buf_len < vd_scsi_len) 27222f5224aeSachartre return (EINVAL); 27232f5224aeSachartre 27242f5224aeSachartre /* set flags */ 27252f5224aeSachartre uscsi->uscsi_flags = vd_scsi_debug; 27262f5224aeSachartre 27272f5224aeSachartre if (vd_scsi->options & VD_SCSI_OPT_NORETRY) { 27282f5224aeSachartre uscsi->uscsi_flags |= USCSI_ISOLATE; 27292f5224aeSachartre uscsi->uscsi_flags |= USCSI_DIAGNOSE; 27302f5224aeSachartre } 27312f5224aeSachartre 27322f5224aeSachartre /* task attribute */ 27332f5224aeSachartre switch (vd_scsi->task_attribute) { 27342f5224aeSachartre case VD_SCSI_TASK_ACA: 27352f5224aeSachartre uscsi->uscsi_flags |= USCSI_HEAD; 27362f5224aeSachartre break; 27372f5224aeSachartre case VD_SCSI_TASK_HQUEUE: 27382f5224aeSachartre uscsi->uscsi_flags |= USCSI_HTAG; 27392f5224aeSachartre break; 27402f5224aeSachartre case VD_SCSI_TASK_ORDERED: 27412f5224aeSachartre uscsi->uscsi_flags |= USCSI_OTAG; 27422f5224aeSachartre break; 27432f5224aeSachartre default: 27442f5224aeSachartre uscsi->uscsi_flags |= USCSI_NOTAG; 27452f5224aeSachartre break; 27462f5224aeSachartre } 27472f5224aeSachartre 27482f5224aeSachartre /* timeout */ 27492f5224aeSachartre uscsi->uscsi_timeout = vd_scsi->timeout; 27502f5224aeSachartre 27512f5224aeSachartre /* cdb data */ 27522f5224aeSachartre uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi); 27532f5224aeSachartre uscsi->uscsi_cdblen = vd_scsi->cdb_len; 27542f5224aeSachartre 27552f5224aeSachartre /* sense buffer */ 27562f5224aeSachartre if (vd_scsi->sense_len != 0) { 27572f5224aeSachartre uscsi->uscsi_flags |= USCSI_RQENABLE; 27582f5224aeSachartre uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi); 27592f5224aeSachartre uscsi->uscsi_rqlen = vd_scsi->sense_len; 27602f5224aeSachartre } 27612f5224aeSachartre 27622f5224aeSachartre if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) { 27632f5224aeSachartre /* uscsi does not support read/write request */ 27642f5224aeSachartre return (EINVAL); 27652f5224aeSachartre } 27662f5224aeSachartre 27672f5224aeSachartre /* request data-in */ 27682f5224aeSachartre if (vd_scsi->datain_len != 0) { 27692f5224aeSachartre uscsi->uscsi_flags |= USCSI_READ; 27702f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->datain_len; 27712f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi); 27722f5224aeSachartre } 27732f5224aeSachartre 27742f5224aeSachartre /* request data-out */ 27752f5224aeSachartre if (vd_scsi->dataout_len != 0) { 27762f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->dataout_len; 27772f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi); 27782f5224aeSachartre } 27792f5224aeSachartre 27802f5224aeSachartre return (0); 27812f5224aeSachartre } 27822f5224aeSachartre 27832f5224aeSachartre static void 27842f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf) 27852f5224aeSachartre { 27862f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 27872f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 27882f5224aeSachartre 27892f5224aeSachartre /* output fields */ 27902f5224aeSachartre vd_scsi->cmd_status = uscsi->uscsi_status; 27912f5224aeSachartre 27922f5224aeSachartre /* sense data */ 27932f5224aeSachartre if ((uscsi->uscsi_flags & USCSI_RQENABLE) && 27942f5224aeSachartre (uscsi->uscsi_status == STATUS_CHECK || 27952f5224aeSachartre uscsi->uscsi_status == STATUS_TERMINATED)) { 27962f5224aeSachartre vd_scsi->sense_status = uscsi->uscsi_rqstatus; 27972f5224aeSachartre if (uscsi->uscsi_rqstatus == STATUS_GOOD) 279814466a20Szk194757 vd_scsi->sense_len -= uscsi->uscsi_rqresid; 27992f5224aeSachartre else 28002f5224aeSachartre vd_scsi->sense_len = 0; 28012f5224aeSachartre } else { 28022f5224aeSachartre vd_scsi->sense_len = 0; 28032f5224aeSachartre } 28042f5224aeSachartre 28052f5224aeSachartre if (uscsi->uscsi_status != STATUS_GOOD) { 28062f5224aeSachartre vd_scsi->dataout_len = 0; 28072f5224aeSachartre vd_scsi->datain_len = 0; 28082f5224aeSachartre return; 28092f5224aeSachartre } 28102f5224aeSachartre 28112f5224aeSachartre if (uscsi->uscsi_flags & USCSI_READ) { 28122f5224aeSachartre /* request data (read) */ 28132f5224aeSachartre vd_scsi->datain_len -= uscsi->uscsi_resid; 28142f5224aeSachartre vd_scsi->dataout_len = 0; 28152f5224aeSachartre } else { 28162f5224aeSachartre /* request data (write) */ 28172f5224aeSachartre vd_scsi->datain_len = 0; 28182f5224aeSachartre vd_scsi->dataout_len -= uscsi->uscsi_resid; 28192f5224aeSachartre } 28202f5224aeSachartre } 28212f5224aeSachartre 2822690555a1Sachartre static ushort_t 28233c96341aSnarayan vd_lbl2cksum(struct dk_label *label) 28243c96341aSnarayan { 28253c96341aSnarayan int count; 2826690555a1Sachartre ushort_t sum, *sp; 28273c96341aSnarayan 28283c96341aSnarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 2829690555a1Sachartre sp = (ushort_t *)label; 28303c96341aSnarayan sum = 0; 28313c96341aSnarayan while (count--) { 28323c96341aSnarayan sum ^= *sp++; 28333c96341aSnarayan } 28343c96341aSnarayan 28353c96341aSnarayan return (sum); 28363c96341aSnarayan } 28373c96341aSnarayan 283887a7269eSachartre /* 2839bae9e67eSachartre * Copy information from a vtoc and dk_geom structures to a dk_label structure. 2840bae9e67eSachartre */ 2841bae9e67eSachartre static void 2842342440ecSPrasad Singamsetty vd_vtocgeom_to_label(struct extvtoc *vtoc, struct dk_geom *geom, 2843bae9e67eSachartre struct dk_label *label) 2844bae9e67eSachartre { 2845bae9e67eSachartre int i; 2846bae9e67eSachartre 2847bae9e67eSachartre ASSERT(vtoc->v_nparts == V_NUMPAR); 2848bae9e67eSachartre ASSERT(vtoc->v_sanity == VTOC_SANE); 2849bae9e67eSachartre 2850bae9e67eSachartre bzero(label, sizeof (struct dk_label)); 2851bae9e67eSachartre 2852bae9e67eSachartre label->dkl_ncyl = geom->dkg_ncyl; 2853bae9e67eSachartre label->dkl_acyl = geom->dkg_acyl; 2854bae9e67eSachartre label->dkl_pcyl = geom->dkg_pcyl; 2855bae9e67eSachartre label->dkl_nhead = geom->dkg_nhead; 2856bae9e67eSachartre label->dkl_nsect = geom->dkg_nsect; 2857bae9e67eSachartre label->dkl_intrlv = geom->dkg_intrlv; 2858bae9e67eSachartre label->dkl_apc = geom->dkg_apc; 2859bae9e67eSachartre label->dkl_rpm = geom->dkg_rpm; 2860bae9e67eSachartre label->dkl_write_reinstruct = geom->dkg_write_reinstruct; 2861bae9e67eSachartre label->dkl_read_reinstruct = geom->dkg_read_reinstruct; 2862bae9e67eSachartre 2863bae9e67eSachartre label->dkl_vtoc.v_nparts = V_NUMPAR; 2864bae9e67eSachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 2865bae9e67eSachartre label->dkl_vtoc.v_version = vtoc->v_version; 2866bae9e67eSachartre for (i = 0; i < V_NUMPAR; i++) { 2867bae9e67eSachartre label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i]; 2868bae9e67eSachartre label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag; 2869bae9e67eSachartre label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag; 2870bae9e67eSachartre label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start / 2871bae9e67eSachartre (label->dkl_nhead * label->dkl_nsect); 2872bae9e67eSachartre label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size; 2873bae9e67eSachartre } 2874bae9e67eSachartre 2875bae9e67eSachartre /* 2876bae9e67eSachartre * The bootinfo array can not be copied with bcopy() because 2877bae9e67eSachartre * elements are of type long in vtoc (so 64-bit) and of type 2878bae9e67eSachartre * int in dk_vtoc (so 32-bit). 2879bae9e67eSachartre */ 2880bae9e67eSachartre label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0]; 2881bae9e67eSachartre label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1]; 2882bae9e67eSachartre label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2]; 2883bae9e67eSachartre bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII); 2884bae9e67eSachartre bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL); 2885bae9e67eSachartre 2886bae9e67eSachartre /* re-compute checksum */ 2887bae9e67eSachartre label->dkl_magic = DKL_MAGIC; 2888bae9e67eSachartre label->dkl_cksum = vd_lbl2cksum(label); 2889bae9e67eSachartre } 2890bae9e67eSachartre 2891bae9e67eSachartre /* 2892bae9e67eSachartre * Copy information from a dk_label structure to a vtoc and dk_geom structures. 2893bae9e67eSachartre */ 2894bae9e67eSachartre static void 2895342440ecSPrasad Singamsetty vd_label_to_vtocgeom(struct dk_label *label, struct extvtoc *vtoc, 2896bae9e67eSachartre struct dk_geom *geom) 2897bae9e67eSachartre { 2898bae9e67eSachartre int i; 2899bae9e67eSachartre 2900bae9e67eSachartre bzero(vtoc, sizeof (struct vtoc)); 2901bae9e67eSachartre bzero(geom, sizeof (struct dk_geom)); 2902bae9e67eSachartre 2903bae9e67eSachartre geom->dkg_ncyl = label->dkl_ncyl; 2904bae9e67eSachartre geom->dkg_acyl = label->dkl_acyl; 2905bae9e67eSachartre geom->dkg_nhead = label->dkl_nhead; 2906bae9e67eSachartre geom->dkg_nsect = label->dkl_nsect; 2907bae9e67eSachartre geom->dkg_intrlv = label->dkl_intrlv; 2908bae9e67eSachartre geom->dkg_apc = label->dkl_apc; 2909bae9e67eSachartre geom->dkg_rpm = label->dkl_rpm; 2910bae9e67eSachartre geom->dkg_pcyl = label->dkl_pcyl; 2911bae9e67eSachartre geom->dkg_write_reinstruct = label->dkl_write_reinstruct; 2912bae9e67eSachartre geom->dkg_read_reinstruct = label->dkl_read_reinstruct; 2913bae9e67eSachartre 2914bae9e67eSachartre vtoc->v_sanity = label->dkl_vtoc.v_sanity; 2915bae9e67eSachartre vtoc->v_version = label->dkl_vtoc.v_version; 2916bae9e67eSachartre vtoc->v_sectorsz = DEV_BSIZE; 2917bae9e67eSachartre vtoc->v_nparts = label->dkl_vtoc.v_nparts; 2918bae9e67eSachartre 2919bae9e67eSachartre for (i = 0; i < vtoc->v_nparts; i++) { 2920bae9e67eSachartre vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag; 2921bae9e67eSachartre vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag; 2922bae9e67eSachartre vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno * 2923bae9e67eSachartre (label->dkl_nhead * label->dkl_nsect); 2924bae9e67eSachartre vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk; 2925bae9e67eSachartre vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i]; 2926bae9e67eSachartre } 2927bae9e67eSachartre 2928bae9e67eSachartre /* 2929bae9e67eSachartre * The bootinfo array can not be copied with bcopy() because 2930bae9e67eSachartre * elements are of type long in vtoc (so 64-bit) and of type 2931bae9e67eSachartre * int in dk_vtoc (so 32-bit). 2932bae9e67eSachartre */ 2933bae9e67eSachartre vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0]; 2934bae9e67eSachartre vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1]; 2935bae9e67eSachartre vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2]; 2936bae9e67eSachartre bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII); 2937bae9e67eSachartre bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL); 2938bae9e67eSachartre } 2939bae9e67eSachartre 2940bae9e67eSachartre /* 2941bae9e67eSachartre * Check if a geometry is valid for a single-slice disk. A geometry is 2942bae9e67eSachartre * considered valid if the main attributes of the geometry match with the 2943bae9e67eSachartre * attributes of the fake geometry we have created. 2944bae9e67eSachartre */ 2945bae9e67eSachartre static boolean_t 2946bae9e67eSachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom) 2947bae9e67eSachartre { 2948bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2949bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2950bae9e67eSachartre 2951bae9e67eSachartre if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl || 2952bae9e67eSachartre geom->dkg_acyl != vd->dk_geom.dkg_acyl || 2953bae9e67eSachartre geom->dkg_nsect != vd->dk_geom.dkg_nsect || 2954bae9e67eSachartre geom->dkg_pcyl != vd->dk_geom.dkg_pcyl) 2955bae9e67eSachartre return (B_FALSE); 2956bae9e67eSachartre 2957bae9e67eSachartre return (B_TRUE); 2958bae9e67eSachartre } 2959bae9e67eSachartre 2960bae9e67eSachartre /* 2961bae9e67eSachartre * Check if a vtoc is valid for a single-slice disk. A vtoc is considered 2962bae9e67eSachartre * valid if the main attributes of the vtoc match with the attributes of the 2963bae9e67eSachartre * fake vtoc we have created. 2964bae9e67eSachartre */ 2965bae9e67eSachartre static boolean_t 2966342440ecSPrasad Singamsetty vd_slice_vtoc_isvalid(vd_t *vd, struct extvtoc *vtoc) 2967bae9e67eSachartre { 2968bae9e67eSachartre size_t csize; 2969bae9e67eSachartre int i; 2970bae9e67eSachartre 2971bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2972bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2973bae9e67eSachartre 2974bae9e67eSachartre if (vtoc->v_sanity != vd->vtoc.v_sanity || 2975bae9e67eSachartre vtoc->v_version != vd->vtoc.v_version || 2976bae9e67eSachartre vtoc->v_nparts != vd->vtoc.v_nparts || 2977bae9e67eSachartre strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 || 2978bae9e67eSachartre strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0) 2979bae9e67eSachartre return (B_FALSE); 2980bae9e67eSachartre 2981bae9e67eSachartre /* slice 2 should be unchanged */ 2982bae9e67eSachartre if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start != 2983bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start || 2984bae9e67eSachartre vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size != 2985bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size) 2986bae9e67eSachartre return (B_FALSE); 2987bae9e67eSachartre 2988bae9e67eSachartre /* 2989bae9e67eSachartre * Slice 0 should be mostly unchanged and cover most of the disk. 2990bae9e67eSachartre * However we allow some flexibility wrt to the start and the size 2991bae9e67eSachartre * of this slice mainly because we can't exactly know how it will 2992bae9e67eSachartre * be defined by the OS installer. 2993bae9e67eSachartre * 2994bae9e67eSachartre * We allow slice 0 to be defined as starting on any of the first 2995bae9e67eSachartre * 4 cylinders. 2996bae9e67eSachartre */ 2997bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 2998bae9e67eSachartre 2999bae9e67eSachartre if (vtoc->v_part[0].p_start > 4 * csize || 3000bae9e67eSachartre vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size) 3001bae9e67eSachartre return (B_FALSE); 3002bae9e67eSachartre 3003bae9e67eSachartre if (vd->vtoc.v_part[0].p_size >= 4 * csize && 3004bae9e67eSachartre vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize) 3005bae9e67eSachartre return (B_FALSE); 3006bae9e67eSachartre 3007bae9e67eSachartre /* any other slice should have a size of 0 */ 3008bae9e67eSachartre for (i = 1; i < vtoc->v_nparts; i++) { 3009bae9e67eSachartre if (i != VD_ENTIRE_DISK_SLICE && 3010bae9e67eSachartre vtoc->v_part[i].p_size != 0) 3011bae9e67eSachartre return (B_FALSE); 3012bae9e67eSachartre } 3013bae9e67eSachartre 3014bae9e67eSachartre return (B_TRUE); 3015bae9e67eSachartre } 3016bae9e67eSachartre 3017bae9e67eSachartre /* 301887a7269eSachartre * Handle ioctls to a disk slice. 3019205eeb1aSlm66018 * 3020205eeb1aSlm66018 * Return Values 3021205eeb1aSlm66018 * 0 - Indicates that there are no errors in disk operations 3022205eeb1aSlm66018 * ENOTSUP - Unknown disk label type or unsupported DKIO ioctl 3023205eeb1aSlm66018 * EINVAL - Not enough room to copy the EFI label 3024205eeb1aSlm66018 * 302587a7269eSachartre */ 30261ae08745Sheppo static int 30270a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 30281ae08745Sheppo { 30294bac2208Snarayan dk_efi_t *dk_ioc; 3030342440ecSPrasad Singamsetty struct extvtoc *vtoc; 3031bae9e67eSachartre struct dk_geom *geom; 3032bae9e67eSachartre size_t len, lba; 3033edcc0754Sachartre 3034edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 3035edcc0754Sachartre 303683990c4aSAlexandre Chartre if (cmd == DKIOCFLUSHWRITECACHE) 303783990c4aSAlexandre Chartre return (vd_flush_write(vd)); 30384bac2208Snarayan 30394bac2208Snarayan switch (vd->vdisk_label) { 30404bac2208Snarayan 3041edcc0754Sachartre /* ioctls for a single slice disk with a VTOC label */ 30424bac2208Snarayan case VD_DISK_LABEL_VTOC: 30434bac2208Snarayan 30441ae08745Sheppo switch (cmd) { 3045bae9e67eSachartre 30461ae08745Sheppo case DKIOCGGEOM: 30470a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 30480a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 30491ae08745Sheppo return (0); 3050bae9e67eSachartre 3051342440ecSPrasad Singamsetty case DKIOCGEXTVTOC: 30520a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 30530a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 30541ae08745Sheppo return (0); 3055bae9e67eSachartre 3056bae9e67eSachartre case DKIOCSGEOM: 3057bae9e67eSachartre ASSERT(ioctl_arg != NULL); 3058bae9e67eSachartre if (vd_slice_single_slice) 3059bae9e67eSachartre return (ENOTSUP); 3060bae9e67eSachartre 3061bae9e67eSachartre /* fake success only if new geometry is valid */ 3062bae9e67eSachartre geom = (struct dk_geom *)ioctl_arg; 3063bae9e67eSachartre if (!vd_slice_geom_isvalid(vd, geom)) 3064bae9e67eSachartre return (EINVAL); 3065bae9e67eSachartre 3066bae9e67eSachartre return (0); 3067bae9e67eSachartre 3068342440ecSPrasad Singamsetty case DKIOCSEXTVTOC: 3069bae9e67eSachartre ASSERT(ioctl_arg != NULL); 3070bae9e67eSachartre if (vd_slice_single_slice) 3071bae9e67eSachartre return (ENOTSUP); 3072bae9e67eSachartre 3073bae9e67eSachartre /* fake sucess only if the new vtoc is valid */ 3074342440ecSPrasad Singamsetty vtoc = (struct extvtoc *)ioctl_arg; 3075bae9e67eSachartre if (!vd_slice_vtoc_isvalid(vd, vtoc)) 3076bae9e67eSachartre return (EINVAL); 3077bae9e67eSachartre 3078bae9e67eSachartre return (0); 3079bae9e67eSachartre 308087a7269eSachartre default: 30813c96341aSnarayan return (ENOTSUP); 308287a7269eSachartre } 308387a7269eSachartre 3084edcc0754Sachartre /* ioctls for a single slice disk with an EFI label */ 308587a7269eSachartre case VD_DISK_LABEL_EFI: 308687a7269eSachartre 3087bae9e67eSachartre if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI) 3088bae9e67eSachartre return (ENOTSUP); 3089bae9e67eSachartre 30903c96341aSnarayan ASSERT(ioctl_arg != NULL); 309187a7269eSachartre dk_ioc = (dk_efi_t *)ioctl_arg; 3092edcc0754Sachartre 3093bae9e67eSachartre len = dk_ioc->dki_length; 3094bae9e67eSachartre lba = dk_ioc->dki_lba; 3095edcc0754Sachartre 3096bae9e67eSachartre if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) || 3097bae9e67eSachartre (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) || 3098bae9e67eSachartre (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t))) 309987a7269eSachartre return (EINVAL); 3100edcc0754Sachartre 3101bae9e67eSachartre switch (cmd) { 3102bae9e67eSachartre case DKIOCGETEFI: 3103bae9e67eSachartre len = vd_slice_flabel_read(vd, 3104bae9e67eSachartre (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len); 3105edcc0754Sachartre 3106bae9e67eSachartre ASSERT(len > 0); 3107edcc0754Sachartre 310887a7269eSachartre return (0); 3109bae9e67eSachartre 3110bae9e67eSachartre case DKIOCSETEFI: 3111bae9e67eSachartre if (vd_slice_single_slice) 311287a7269eSachartre return (ENOTSUP); 3113bae9e67eSachartre 3114bae9e67eSachartre /* we currently don't support writing EFI */ 3115bae9e67eSachartre return (EIO); 311687a7269eSachartre } 311787a7269eSachartre 311887a7269eSachartre default: 3119205eeb1aSlm66018 /* Unknown disk label type */ 312087a7269eSachartre return (ENOTSUP); 312187a7269eSachartre } 312287a7269eSachartre } 312387a7269eSachartre 3124edcc0754Sachartre static int 3125edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe) 3126edcc0754Sachartre { 3127edcc0754Sachartre vd_efi_dev_t edev; 3128edcc0754Sachartre int status; 3129edcc0754Sachartre 3130edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 3131edcc0754Sachartre 3132edcc0754Sachartre status = vd_efi_alloc_and_read(&edev, gpt, gpe); 3133edcc0754Sachartre 3134edcc0754Sachartre return (status); 3135edcc0754Sachartre } 3136edcc0754Sachartre 3137edcc0754Sachartre static void 3138edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe) 3139edcc0754Sachartre { 3140edcc0754Sachartre vd_efi_dev_t edev; 3141edcc0754Sachartre 3142edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 3143edcc0754Sachartre 3144edcc0754Sachartre vd_efi_free(&edev, gpt, gpe); 3145edcc0754Sachartre } 3146edcc0754Sachartre 3147edcc0754Sachartre static int 31481aff8f07SAlexandre Chartre vd_dskimg_validate_efi(vd_t *vd) 3149edcc0754Sachartre { 3150edcc0754Sachartre efi_gpt_t *gpt; 3151edcc0754Sachartre efi_gpe_t *gpe; 3152edcc0754Sachartre int i, nparts, status; 3153edcc0754Sachartre struct uuid efi_reserved = EFI_RESERVED; 3154edcc0754Sachartre 3155edcc0754Sachartre if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0) 3156edcc0754Sachartre return (status); 3157edcc0754Sachartre 3158342440ecSPrasad Singamsetty bzero(&vd->vtoc, sizeof (struct extvtoc)); 3159edcc0754Sachartre bzero(&vd->dk_geom, sizeof (struct dk_geom)); 3160edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 3161edcc0754Sachartre 3162edcc0754Sachartre vd->efi_reserved = -1; 3163edcc0754Sachartre 3164edcc0754Sachartre nparts = gpt->efi_gpt_NumberOfPartitionEntries; 3165edcc0754Sachartre 3166edcc0754Sachartre for (i = 0; i < nparts && i < VD_MAXPART; i++) { 3167edcc0754Sachartre 3168d84f0041SAlexandre Chartre if (gpe[i].efi_gpe_StartingLBA == 0 && 3169edcc0754Sachartre gpe[i].efi_gpe_EndingLBA == 0) { 3170edcc0754Sachartre continue; 3171edcc0754Sachartre } 3172edcc0754Sachartre 3173edcc0754Sachartre vd->slices[i].start = gpe[i].efi_gpe_StartingLBA; 3174edcc0754Sachartre vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA - 3175edcc0754Sachartre gpe[i].efi_gpe_StartingLBA + 1; 3176edcc0754Sachartre 3177edcc0754Sachartre if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved, 3178edcc0754Sachartre sizeof (struct uuid)) == 0) 3179edcc0754Sachartre vd->efi_reserved = i; 3180edcc0754Sachartre 3181edcc0754Sachartre } 3182edcc0754Sachartre 3183edcc0754Sachartre ASSERT(vd->vdisk_size != 0); 3184edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].start = 0; 3185edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size; 3186edcc0754Sachartre 3187edcc0754Sachartre vds_efi_free(vd, gpt, gpe); 3188edcc0754Sachartre 3189edcc0754Sachartre return (status); 3190edcc0754Sachartre } 3191edcc0754Sachartre 319287a7269eSachartre /* 319378fcd0a1Sachartre * Function: 31941aff8f07SAlexandre Chartre * vd_dskimg_validate_geometry 3195205eeb1aSlm66018 * 319678fcd0a1Sachartre * Description: 319778fcd0a1Sachartre * Read the label and validate the geometry of a disk image. The driver 319878fcd0a1Sachartre * label, vtoc and geometry information are updated according to the 319978fcd0a1Sachartre * label read from the disk image. 320078fcd0a1Sachartre * 320178fcd0a1Sachartre * If no valid label is found, the label is set to unknown and the 320278fcd0a1Sachartre * function returns EINVAL, but a default vtoc and geometry are provided 3203edcc0754Sachartre * to the driver. If an EFI label is found, ENOTSUP is returned. 320478fcd0a1Sachartre * 320578fcd0a1Sachartre * Parameters: 320678fcd0a1Sachartre * vd - disk on which the operation is performed. 320778fcd0a1Sachartre * 320878fcd0a1Sachartre * Return Code: 320978fcd0a1Sachartre * 0 - success. 321078fcd0a1Sachartre * EIO - error reading the label from the disk image. 321178fcd0a1Sachartre * EINVAL - unknown disk label. 3212edcc0754Sachartre * ENOTSUP - geometry not applicable (EFI label). 321387a7269eSachartre */ 321487a7269eSachartre static int 32151aff8f07SAlexandre Chartre vd_dskimg_validate_geometry(vd_t *vd) 321687a7269eSachartre { 321787a7269eSachartre struct dk_label label; 321878fcd0a1Sachartre struct dk_geom *geom = &vd->dk_geom; 3219342440ecSPrasad Singamsetty struct extvtoc *vtoc = &vd->vtoc; 322078fcd0a1Sachartre int i; 322178fcd0a1Sachartre int status = 0; 322287a7269eSachartre 32231aff8f07SAlexandre Chartre ASSERT(VD_DSKIMG(vd)); 322487a7269eSachartre 32251aff8f07SAlexandre Chartre if (VD_DSKIMG_LABEL_READ(vd, &label) < 0) 322687a7269eSachartre return (EIO); 322787a7269eSachartre 322887a7269eSachartre if (label.dkl_magic != DKL_MAGIC || 322978fcd0a1Sachartre label.dkl_cksum != vd_lbl2cksum(&label) || 32301aff8f07SAlexandre Chartre (vd_dskimg_validate_sanity && 32311aff8f07SAlexandre Chartre label.dkl_vtoc.v_sanity != VTOC_SANE) || 323278fcd0a1Sachartre label.dkl_vtoc.v_nparts != V_NUMPAR) { 3233edcc0754Sachartre 32341aff8f07SAlexandre Chartre if (vd_dskimg_validate_efi(vd) == 0) { 3235edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 3236edcc0754Sachartre return (ENOTSUP); 3237edcc0754Sachartre } 3238edcc0754Sachartre 323978fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 32401aff8f07SAlexandre Chartre vd_build_default_label(vd->dskimg_size, &label); 324178fcd0a1Sachartre status = EINVAL; 324278fcd0a1Sachartre } else { 324378fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 324478fcd0a1Sachartre } 324587a7269eSachartre 3246bae9e67eSachartre /* Update the driver geometry and vtoc */ 3247bae9e67eSachartre vd_label_to_vtocgeom(&label, vtoc, geom); 324887a7269eSachartre 3249edcc0754Sachartre /* Update logical partitions */ 3250edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 3251edcc0754Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 3252edcc0754Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 3253edcc0754Sachartre vd->slices[i].start = vtoc->v_part[i].p_start; 3254edcc0754Sachartre vd->slices[i].nblocks = vtoc->v_part[i].p_size; 3255edcc0754Sachartre } 3256edcc0754Sachartre } 3257edcc0754Sachartre 325878fcd0a1Sachartre return (status); 325978fcd0a1Sachartre } 326078fcd0a1Sachartre 326178fcd0a1Sachartre /* 32621aff8f07SAlexandre Chartre * Handle ioctls to a disk image. 326378fcd0a1Sachartre * 326478fcd0a1Sachartre * Return Values 326578fcd0a1Sachartre * 0 - Indicates that there are no errors 326678fcd0a1Sachartre * != 0 - Disk operation returned an error 326778fcd0a1Sachartre */ 326878fcd0a1Sachartre static int 32691aff8f07SAlexandre Chartre vd_do_dskimg_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 327078fcd0a1Sachartre { 327178fcd0a1Sachartre struct dk_label label; 327278fcd0a1Sachartre struct dk_geom *geom; 3273342440ecSPrasad Singamsetty struct extvtoc *vtoc; 3274edcc0754Sachartre dk_efi_t *efi; 327583990c4aSAlexandre Chartre int rc; 327678fcd0a1Sachartre 32771aff8f07SAlexandre Chartre ASSERT(VD_DSKIMG(vd)); 327878fcd0a1Sachartre 327978fcd0a1Sachartre switch (cmd) { 328078fcd0a1Sachartre 328178fcd0a1Sachartre case DKIOCGGEOM: 328278fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 328378fcd0a1Sachartre geom = (struct dk_geom *)ioctl_arg; 328478fcd0a1Sachartre 32851aff8f07SAlexandre Chartre rc = vd_dskimg_validate_geometry(vd); 3286edcc0754Sachartre if (rc != 0 && rc != EINVAL) 328778fcd0a1Sachartre return (rc); 328878fcd0a1Sachartre bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom)); 328978fcd0a1Sachartre return (0); 329078fcd0a1Sachartre 3291342440ecSPrasad Singamsetty case DKIOCGEXTVTOC: 329278fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 3293342440ecSPrasad Singamsetty vtoc = (struct extvtoc *)ioctl_arg; 329478fcd0a1Sachartre 32951aff8f07SAlexandre Chartre rc = vd_dskimg_validate_geometry(vd); 3296edcc0754Sachartre if (rc != 0 && rc != EINVAL) 329778fcd0a1Sachartre return (rc); 3298342440ecSPrasad Singamsetty bcopy(&vd->vtoc, vtoc, sizeof (struct extvtoc)); 329987a7269eSachartre return (0); 330087a7269eSachartre 330187a7269eSachartre case DKIOCSGEOM: 330287a7269eSachartre ASSERT(ioctl_arg != NULL); 330387a7269eSachartre geom = (struct dk_geom *)ioctl_arg; 330487a7269eSachartre 330587a7269eSachartre if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0) 330687a7269eSachartre return (EINVAL); 330787a7269eSachartre 330887a7269eSachartre /* 330987a7269eSachartre * The current device geometry is not updated, just the driver 331087a7269eSachartre * "notion" of it. The device geometry will be effectively 331187a7269eSachartre * updated when a label is written to the device during a next 3312342440ecSPrasad Singamsetty * DKIOCSEXTVTOC. 331387a7269eSachartre */ 331487a7269eSachartre bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom)); 331587a7269eSachartre return (0); 331687a7269eSachartre 3317342440ecSPrasad Singamsetty case DKIOCSEXTVTOC: 331887a7269eSachartre ASSERT(ioctl_arg != NULL); 331987a7269eSachartre ASSERT(vd->dk_geom.dkg_nhead != 0 && 332087a7269eSachartre vd->dk_geom.dkg_nsect != 0); 3321342440ecSPrasad Singamsetty vtoc = (struct extvtoc *)ioctl_arg; 3322690555a1Sachartre 3323690555a1Sachartre if (vtoc->v_sanity != VTOC_SANE || 3324690555a1Sachartre vtoc->v_sectorsz != DEV_BSIZE || 3325690555a1Sachartre vtoc->v_nparts != V_NUMPAR) 3326690555a1Sachartre return (EINVAL); 3327690555a1Sachartre 3328bae9e67eSachartre vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label); 3329690555a1Sachartre 333087a7269eSachartre /* write label to the disk image */ 33311aff8f07SAlexandre Chartre if ((rc = vd_dskimg_set_vtoc(vd, &label)) != 0) 333287a7269eSachartre return (rc); 3333690555a1Sachartre 3334edcc0754Sachartre break; 3335edcc0754Sachartre 3336edcc0754Sachartre case DKIOCFLUSHWRITECACHE: 333783990c4aSAlexandre Chartre return (vd_flush_write(vd)); 3338edcc0754Sachartre 3339edcc0754Sachartre case DKIOCGETEFI: 3340edcc0754Sachartre ASSERT(ioctl_arg != NULL); 3341edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 3342edcc0754Sachartre 33431aff8f07SAlexandre Chartre if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, 3344edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3345edcc0754Sachartre return (EIO); 3346edcc0754Sachartre 3347edcc0754Sachartre return (0); 3348edcc0754Sachartre 3349edcc0754Sachartre case DKIOCSETEFI: 3350edcc0754Sachartre ASSERT(ioctl_arg != NULL); 3351edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 3352edcc0754Sachartre 33531aff8f07SAlexandre Chartre if (vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 3354edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3355edcc0754Sachartre return (EIO); 3356edcc0754Sachartre 3357edcc0754Sachartre break; 3358edcc0754Sachartre 3359edcc0754Sachartre 3360edcc0754Sachartre default: 3361edcc0754Sachartre return (ENOTSUP); 3362edcc0754Sachartre } 3363edcc0754Sachartre 3364342440ecSPrasad Singamsetty ASSERT(cmd == DKIOCSEXTVTOC || cmd == DKIOCSETEFI); 3365edcc0754Sachartre 3366edcc0754Sachartre /* label has changed, revalidate the geometry */ 33671aff8f07SAlexandre Chartre (void) vd_dskimg_validate_geometry(vd); 33683c96341aSnarayan 336987a7269eSachartre /* 337087a7269eSachartre * The disk geometry may have changed, so we need to write 337187a7269eSachartre * the devid (if there is one) so that it is stored at the 337287a7269eSachartre * right location. 337387a7269eSachartre */ 33741aff8f07SAlexandre Chartre if (vd_dskimg_write_devid(vd, vd->dskimg_devid) != 0) { 337587a7269eSachartre PR0("Fail to write devid"); 33761ae08745Sheppo } 33774bac2208Snarayan 33784bac2208Snarayan return (0); 33794bac2208Snarayan } 3380edcc0754Sachartre 3381edcc0754Sachartre static int 3382edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg) 3383edcc0754Sachartre { 3384edcc0754Sachartre int rval = 0, status; 3385342440ecSPrasad Singamsetty struct vtoc vtoc; 3386edcc0754Sachartre 3387edcc0754Sachartre /* 3388edcc0754Sachartre * Call the appropriate function to execute the ioctl depending 3389edcc0754Sachartre * on the type of vdisk. 3390edcc0754Sachartre */ 3391edcc0754Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3392edcc0754Sachartre 3393edcc0754Sachartre /* slice, file or volume exported as a single slice disk */ 3394edcc0754Sachartre status = vd_do_slice_ioctl(vd, cmd, arg); 3395edcc0754Sachartre 33961aff8f07SAlexandre Chartre } else if (VD_DSKIMG(vd)) { 3397edcc0754Sachartre 3398edcc0754Sachartre /* file or volume exported as a full disk */ 33991aff8f07SAlexandre Chartre status = vd_do_dskimg_ioctl(vd, cmd, arg); 3400edcc0754Sachartre 3401edcc0754Sachartre } else { 3402edcc0754Sachartre 3403edcc0754Sachartre /* disk device exported as a full disk */ 3404edcc0754Sachartre status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg, 3405edcc0754Sachartre vd->open_flags | FKIOCTL, kcred, &rval); 3406342440ecSPrasad Singamsetty 3407342440ecSPrasad Singamsetty /* 3408342440ecSPrasad Singamsetty * By default VTOC ioctls are done using ioctls for the 3409342440ecSPrasad Singamsetty * extended VTOC. Some drivers (in particular non-Sun drivers) 3410342440ecSPrasad Singamsetty * may not support these ioctls. In that case, we fallback to 3411342440ecSPrasad Singamsetty * the regular VTOC ioctls. 3412342440ecSPrasad Singamsetty */ 3413342440ecSPrasad Singamsetty if (status == ENOTTY) { 3414342440ecSPrasad Singamsetty switch (cmd) { 3415342440ecSPrasad Singamsetty 3416342440ecSPrasad Singamsetty case DKIOCGEXTVTOC: 3417342440ecSPrasad Singamsetty cmd = DKIOCGVTOC; 3418342440ecSPrasad Singamsetty status = ldi_ioctl(vd->ldi_handle[0], cmd, 3419342440ecSPrasad Singamsetty (intptr_t)&vtoc, vd->open_flags | FKIOCTL, 3420342440ecSPrasad Singamsetty kcred, &rval); 3421342440ecSPrasad Singamsetty vtoctoextvtoc(vtoc, 3422342440ecSPrasad Singamsetty (*(struct extvtoc *)(void *)arg)); 3423342440ecSPrasad Singamsetty break; 3424342440ecSPrasad Singamsetty 3425342440ecSPrasad Singamsetty case DKIOCSEXTVTOC: 3426342440ecSPrasad Singamsetty cmd = DKIOCSVTOC; 3427342440ecSPrasad Singamsetty extvtoctovtoc((*(struct extvtoc *)(void *)arg), 3428342440ecSPrasad Singamsetty vtoc); 3429342440ecSPrasad Singamsetty status = ldi_ioctl(vd->ldi_handle[0], cmd, 3430342440ecSPrasad Singamsetty (intptr_t)&vtoc, vd->open_flags | FKIOCTL, 3431342440ecSPrasad Singamsetty kcred, &rval); 3432342440ecSPrasad Singamsetty break; 3433342440ecSPrasad Singamsetty } 3434342440ecSPrasad Singamsetty } 3435edcc0754Sachartre } 3436edcc0754Sachartre 3437edcc0754Sachartre #ifdef DEBUG 3438edcc0754Sachartre if (rval != 0) { 3439edcc0754Sachartre PR0("ioctl %x set rval = %d, which is not being returned" 3440edcc0754Sachartre " to caller", cmd, rval); 3441edcc0754Sachartre } 3442edcc0754Sachartre #endif /* DEBUG */ 3443edcc0754Sachartre 3444edcc0754Sachartre return (status); 34451ae08745Sheppo } 34461ae08745Sheppo 3447205eeb1aSlm66018 /* 3448205eeb1aSlm66018 * Description: 3449205eeb1aSlm66018 * This is the function that processes the ioctl requests (farming it 3450205eeb1aSlm66018 * out to functions that handle slices, files or whole disks) 3451205eeb1aSlm66018 * 3452205eeb1aSlm66018 * Return Values 3453205eeb1aSlm66018 * 0 - ioctl operation completed successfully 3454205eeb1aSlm66018 * != 0 - The LDC error value encountered 3455205eeb1aSlm66018 * (propagated back up the call stack as a task error) 3456205eeb1aSlm66018 * 3457205eeb1aSlm66018 * Side Effect 3458205eeb1aSlm66018 * sets request->status to the return value of the ioctl function. 3459205eeb1aSlm66018 */ 34601ae08745Sheppo static int 34610a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 34621ae08745Sheppo { 3463edcc0754Sachartre int status = 0; 34641ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 34651ae08745Sheppo 34661ae08745Sheppo 34671ae08745Sheppo ASSERT(request->slice < vd->nslices); 34681ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 34691ae08745Sheppo 34700a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 34710a55fbb7Slm66018 if (ioctl->copyin != NULL) { 34721ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 34731ae08745Sheppo PR1("Getting \"arg\" data from client"); 34741ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 34751ae08745Sheppo request->cookie, request->ncookies, 34761ae08745Sheppo LDC_COPY_IN)) != 0) { 34773af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 34781ae08745Sheppo "copying from client", status); 34791ae08745Sheppo return (status); 34801ae08745Sheppo } 34810a55fbb7Slm66018 34820a55fbb7Slm66018 /* Convert client's data, if necessary */ 34832f5224aeSachartre if (ioctl->copyin == VD_IDENTITY_IN) { 34842f5224aeSachartre /* use client buffer */ 34850a55fbb7Slm66018 ioctl->arg = buf; 34862f5224aeSachartre } else { 34872f5224aeSachartre /* convert client vdisk operation data to ioctl data */ 34882f5224aeSachartre status = (ioctl->copyin)(buf, nbytes, 34892f5224aeSachartre (void *)ioctl->arg); 34902f5224aeSachartre if (status != 0) { 34912f5224aeSachartre request->status = status; 34922f5224aeSachartre return (0); 34932f5224aeSachartre } 34942f5224aeSachartre } 34952f5224aeSachartre } 34962f5224aeSachartre 34972f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD) { 34982f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg; 34992f5224aeSachartre 35002f5224aeSachartre /* check write permission */ 35012f5224aeSachartre if (!(vd->open_flags & FWRITE) && 35022f5224aeSachartre !(uscsi->uscsi_flags & USCSI_READ)) { 35032f5224aeSachartre PR0("uscsi fails because backend is opened read-only"); 35042f5224aeSachartre request->status = EROFS; 35052f5224aeSachartre return (0); 35062f5224aeSachartre } 35071ae08745Sheppo } 35081ae08745Sheppo 35091ae08745Sheppo /* 3510edcc0754Sachartre * Send the ioctl to the disk backend. 35111ae08745Sheppo */ 3512edcc0754Sachartre request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg); 3513205eeb1aSlm66018 3514205eeb1aSlm66018 if (request->status != 0) { 3515205eeb1aSlm66018 PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status); 35162f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD && 35172f5224aeSachartre ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0) 35182f5224aeSachartre /* 35192f5224aeSachartre * USCSICMD has reported an error and the uscsi_status 35202f5224aeSachartre * field is not zero. This means that the SCSI command 35212f5224aeSachartre * has completed but it has an error. So we should 35222f5224aeSachartre * mark the VD operation has succesfully completed 35232f5224aeSachartre * and clients can check the SCSI status field for 35242f5224aeSachartre * SCSI errors. 35252f5224aeSachartre */ 35262f5224aeSachartre request->status = 0; 35272f5224aeSachartre else 3528205eeb1aSlm66018 return (0); 3529205eeb1aSlm66018 } 35301ae08745Sheppo 35310a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 35320a55fbb7Slm66018 if (ioctl->copyout != NULL) { 35331ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 35341ae08745Sheppo PR1("Sending \"arg\" data to client"); 35350a55fbb7Slm66018 35360a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 35372f5224aeSachartre if (ioctl->copyout != VD_IDENTITY_OUT) 35380a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 35390a55fbb7Slm66018 35401ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 35411ae08745Sheppo request->cookie, request->ncookies, 35421ae08745Sheppo LDC_COPY_OUT)) != 0) { 35433af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 35441ae08745Sheppo "copying to client", status); 35451ae08745Sheppo return (status); 35461ae08745Sheppo } 35471ae08745Sheppo } 35481ae08745Sheppo 35491ae08745Sheppo return (status); 35501ae08745Sheppo } 35511ae08745Sheppo 35521ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 3553205eeb1aSlm66018 3554205eeb1aSlm66018 /* 3555205eeb1aSlm66018 * Description: 3556205eeb1aSlm66018 * This generic function is called by the task queue to complete 3557205eeb1aSlm66018 * the processing of the tasks. The specific completion function 3558205eeb1aSlm66018 * is passed in as a field in the task pointer. 3559205eeb1aSlm66018 * 3560205eeb1aSlm66018 * Parameters: 3561205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 3562205eeb1aSlm66018 * 3563205eeb1aSlm66018 * Return Values 3564205eeb1aSlm66018 * None 3565205eeb1aSlm66018 */ 3566205eeb1aSlm66018 static void 3567205eeb1aSlm66018 vd_complete(void *arg) 3568205eeb1aSlm66018 { 3569205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 3570205eeb1aSlm66018 3571205eeb1aSlm66018 ASSERT(task != NULL); 3572205eeb1aSlm66018 ASSERT(task->status == EINPROGRESS); 3573205eeb1aSlm66018 ASSERT(task->completef != NULL); 3574205eeb1aSlm66018 3575205eeb1aSlm66018 task->status = task->completef(task); 3576205eeb1aSlm66018 if (task->status) 3577205eeb1aSlm66018 PR0("%s: Error %d completing task", __func__, task->status); 3578205eeb1aSlm66018 3579205eeb1aSlm66018 /* Now notify the vDisk client */ 3580205eeb1aSlm66018 vd_complete_notify(task); 3581205eeb1aSlm66018 } 3582205eeb1aSlm66018 35831ae08745Sheppo static int 3584d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 35851ae08745Sheppo { 358687a7269eSachartre int i, status; 35871ae08745Sheppo void *buf = NULL; 35880a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 3589342440ecSPrasad Singamsetty struct extvtoc vtoc = {0}; 35904bac2208Snarayan struct dk_efi dk_efi = {0}; 35912f5224aeSachartre struct uscsi_cmd uscsi = {0}; 3592d10e4ef2Snarayan vd_t *vd = task->vd; 3593d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 35940a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 35950a55fbb7Slm66018 /* Command (no-copy) operations */ 35960a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 35970a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 3598047ba61eSachartre NULL, NULL, NULL, B_TRUE}, 35990a55fbb7Slm66018 36000a55fbb7Slm66018 /* "Get" (copy-out) operations */ 36010a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 36020a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 36032f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE}, 36040a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 36050a55fbb7Slm66018 RNDSIZE(vd_geom_t), 36060a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 3607047ba61eSachartre &dk_geom, NULL, dk_geom2vd_geom, B_FALSE}, 36080a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 3609342440ecSPrasad Singamsetty DKIOCGEXTVTOC, STRINGIZE(DKIOCGEXTVTOC), 3610047ba61eSachartre &vtoc, NULL, vtoc2vd_vtoc, B_FALSE}, 36114bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 36124bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 3613047ba61eSachartre &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE}, 36140a55fbb7Slm66018 36150a55fbb7Slm66018 /* "Set" (copy-in) operations */ 36160a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 36170a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 36182f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE}, 36190a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 36200a55fbb7Slm66018 RNDSIZE(vd_geom_t), 36210a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 3622047ba61eSachartre &dk_geom, vd_geom2dk_geom, NULL, B_TRUE}, 36230a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 3624342440ecSPrasad Singamsetty DKIOCSEXTVTOC, STRINGIZE(DKIOCSEXTVTOC), 3625047ba61eSachartre &vtoc, vd_vtoc2vtoc, NULL, B_TRUE}, 36264bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 36274bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 3628047ba61eSachartre &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE}, 36292f5224aeSachartre 36302f5224aeSachartre {VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t), 36312f5224aeSachartre USCSICMD, STRINGIZE(USCSICMD), 36322f5224aeSachartre &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE}, 36330a55fbb7Slm66018 }; 36341ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 36351ae08745Sheppo 36361ae08745Sheppo 3637d10e4ef2Snarayan ASSERT(vd != NULL); 3638d10e4ef2Snarayan ASSERT(request != NULL); 36391ae08745Sheppo ASSERT(request->slice < vd->nslices); 36401ae08745Sheppo 36411ae08745Sheppo /* 36421ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 36431ae08745Sheppo * validate caller's "nbytes" 36441ae08745Sheppo */ 36451ae08745Sheppo for (i = 0; i < nioctls; i++) { 36461ae08745Sheppo if (request->operation == ioctl[i].operation) { 36470a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 36480a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 36490a55fbb7Slm66018 36504bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 36512f5224aeSachartre request->operation == VD_OP_SET_EFI || 36522f5224aeSachartre request->operation == VD_OP_SCSICMD) { 36534bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 36544bac2208Snarayan break; 36553af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 36564bac2208Snarayan "got %lu", ioctl[i].operation_name, 36574bac2208Snarayan ioctl[i].nbytes, request->nbytes); 36584bac2208Snarayan return (EINVAL); 36594bac2208Snarayan } 36604bac2208Snarayan 36610a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 36623af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 36630a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 36640a55fbb7Slm66018 request->nbytes); 36651ae08745Sheppo return (EINVAL); 36661ae08745Sheppo } 36671ae08745Sheppo 36681ae08745Sheppo break; 36691ae08745Sheppo } 36701ae08745Sheppo } 36711ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 36721ae08745Sheppo 3673047ba61eSachartre if (!(vd->open_flags & FWRITE) && ioctl[i].write) { 3674047ba61eSachartre PR0("%s fails because backend is opened read-only", 3675047ba61eSachartre ioctl[i].operation_name); 3676047ba61eSachartre request->status = EROFS; 3677047ba61eSachartre return (0); 3678047ba61eSachartre } 3679047ba61eSachartre 36801ae08745Sheppo if (request->nbytes) 36811ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 36821ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 36831ae08745Sheppo if (request->nbytes) 36841ae08745Sheppo kmem_free(buf, request->nbytes); 368587a7269eSachartre 36861ae08745Sheppo return (status); 36871ae08745Sheppo } 36881ae08745Sheppo 36894bac2208Snarayan static int 36904bac2208Snarayan vd_get_devid(vd_task_t *task) 36914bac2208Snarayan { 36924bac2208Snarayan vd_t *vd = task->vd; 36934bac2208Snarayan vd_dring_payload_t *request = task->request; 36944bac2208Snarayan vd_devid_t *vd_devid; 36954bac2208Snarayan impl_devid_t *devid; 369687a7269eSachartre int status, bufid_len, devid_len, len, sz; 36973af08d82Slm66018 int bufbytes; 36984bac2208Snarayan 36993af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 37004bac2208Snarayan 3701bae9e67eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3702bae9e67eSachartre /* 3703bae9e67eSachartre * We don't support devid for single-slice disks because we 3704bae9e67eSachartre * have no space to store a fabricated devid and for physical 3705bae9e67eSachartre * disk slices, we can't use the devid of the disk otherwise 3706bae9e67eSachartre * exporting multiple slices from the same disk will produce 3707bae9e67eSachartre * the same devids. 3708bae9e67eSachartre */ 3709bae9e67eSachartre PR2("No Device ID for slices"); 3710bae9e67eSachartre request->status = ENOTSUP; 3711bae9e67eSachartre return (0); 3712bae9e67eSachartre } 3713bae9e67eSachartre 37141aff8f07SAlexandre Chartre if (VD_DSKIMG(vd)) { 37151aff8f07SAlexandre Chartre if (vd->dskimg_devid == NULL) { 37163af08d82Slm66018 PR2("No Device ID"); 3717205eeb1aSlm66018 request->status = ENOENT; 3718205eeb1aSlm66018 return (0); 371987a7269eSachartre } else { 37201aff8f07SAlexandre Chartre sz = ddi_devid_sizeof(vd->dskimg_devid); 372187a7269eSachartre devid = kmem_alloc(sz, KM_SLEEP); 37221aff8f07SAlexandre Chartre bcopy(vd->dskimg_devid, devid, sz); 372387a7269eSachartre } 372487a7269eSachartre } else { 372587a7269eSachartre if (ddi_lyr_get_devid(vd->dev[request->slice], 372687a7269eSachartre (ddi_devid_t *)&devid) != DDI_SUCCESS) { 372787a7269eSachartre PR2("No Device ID"); 3728205eeb1aSlm66018 request->status = ENOENT; 3729205eeb1aSlm66018 return (0); 373087a7269eSachartre } 37314bac2208Snarayan } 37324bac2208Snarayan 37334bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 37344bac2208Snarayan devid_len = DEVID_GETLEN(devid); 37354bac2208Snarayan 37363af08d82Slm66018 /* 37373af08d82Slm66018 * Save the buffer size here for use in deallocation. 37383af08d82Slm66018 * The actual number of bytes copied is returned in 37393af08d82Slm66018 * the 'nbytes' field of the request structure. 37403af08d82Slm66018 */ 37413af08d82Slm66018 bufbytes = request->nbytes; 37423af08d82Slm66018 37433af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 37444bac2208Snarayan vd_devid->length = devid_len; 37454bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 37464bac2208Snarayan 37474bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 37484bac2208Snarayan 37494bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 37504bac2208Snarayan 375178fcd0a1Sachartre request->status = 0; 375278fcd0a1Sachartre 37534bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 37544bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 37554bac2208Snarayan 37564bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 37574bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 37584bac2208Snarayan LDC_COPY_OUT)) != 0) { 37593af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 37604bac2208Snarayan status); 37614bac2208Snarayan } 37623af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 37634bac2208Snarayan 37643af08d82Slm66018 kmem_free(vd_devid, bufbytes); 37654bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 37664bac2208Snarayan 37674bac2208Snarayan return (status); 37684bac2208Snarayan } 37694bac2208Snarayan 37702f5224aeSachartre static int 37712f5224aeSachartre vd_scsi_reset(vd_t *vd) 37722f5224aeSachartre { 37732f5224aeSachartre int rval, status; 37742f5224aeSachartre struct uscsi_cmd uscsi = { 0 }; 37752f5224aeSachartre 37762f5224aeSachartre uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET; 37772f5224aeSachartre uscsi.uscsi_timeout = vd_scsi_rdwr_timeout; 37782f5224aeSachartre 37792f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi, 37802f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 37812f5224aeSachartre 37822f5224aeSachartre return (status); 37832f5224aeSachartre } 37842f5224aeSachartre 37852f5224aeSachartre static int 37862f5224aeSachartre vd_reset(vd_task_t *task) 37872f5224aeSachartre { 37882f5224aeSachartre vd_t *vd = task->vd; 37892f5224aeSachartre vd_dring_payload_t *request = task->request; 37902f5224aeSachartre 37912f5224aeSachartre ASSERT(request->operation == VD_OP_RESET); 37922f5224aeSachartre ASSERT(vd->scsi); 37932f5224aeSachartre 37942f5224aeSachartre PR0("Performing VD_OP_RESET"); 37952f5224aeSachartre 37962f5224aeSachartre if (request->nbytes != 0) { 37972f5224aeSachartre PR0("VD_OP_RESET: Expected nbytes = 0, got %lu", 37982f5224aeSachartre request->nbytes); 37992f5224aeSachartre return (EINVAL); 38002f5224aeSachartre } 38012f5224aeSachartre 38022f5224aeSachartre request->status = vd_scsi_reset(vd); 38032f5224aeSachartre 38042f5224aeSachartre return (0); 38052f5224aeSachartre } 38062f5224aeSachartre 38072f5224aeSachartre static int 38082f5224aeSachartre vd_get_capacity(vd_task_t *task) 38092f5224aeSachartre { 38102f5224aeSachartre int rv; 38112f5224aeSachartre size_t nbytes; 38122f5224aeSachartre vd_t *vd = task->vd; 38132f5224aeSachartre vd_dring_payload_t *request = task->request; 38142f5224aeSachartre vd_capacity_t vd_cap = { 0 }; 38152f5224aeSachartre 38162f5224aeSachartre ASSERT(request->operation == VD_OP_GET_CAPACITY); 38172f5224aeSachartre 38182f5224aeSachartre PR0("Performing VD_OP_GET_CAPACITY"); 38192f5224aeSachartre 38202f5224aeSachartre nbytes = request->nbytes; 38212f5224aeSachartre 38222f5224aeSachartre if (nbytes != RNDSIZE(vd_capacity_t)) { 38232f5224aeSachartre PR0("VD_OP_GET_CAPACITY: Expected nbytes = %lu, got %lu", 38242f5224aeSachartre RNDSIZE(vd_capacity_t), nbytes); 38252f5224aeSachartre return (EINVAL); 38262f5224aeSachartre } 38272f5224aeSachartre 3828de3a5331SRamesh Chitrothu /* 3829de3a5331SRamesh Chitrothu * Check the backend size in case it has changed. If the check fails 3830de3a5331SRamesh Chitrothu * then we will return the last known size. 3831de3a5331SRamesh Chitrothu */ 38322f5224aeSachartre 3833de3a5331SRamesh Chitrothu (void) vd_backend_check_size(vd); 38342f5224aeSachartre ASSERT(vd->vdisk_size != 0); 38352f5224aeSachartre 38362f5224aeSachartre request->status = 0; 38372f5224aeSachartre 38382f5224aeSachartre vd_cap.vdisk_block_size = vd->vdisk_block_size; 38392f5224aeSachartre vd_cap.vdisk_size = vd->vdisk_size; 38402f5224aeSachartre 38412f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes, 38422f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 38432f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 38442f5224aeSachartre return (rv); 38452f5224aeSachartre } 38462f5224aeSachartre 38472f5224aeSachartre return (0); 38482f5224aeSachartre } 38492f5224aeSachartre 38502f5224aeSachartre static int 38512f5224aeSachartre vd_get_access(vd_task_t *task) 38522f5224aeSachartre { 38532f5224aeSachartre uint64_t access; 38542f5224aeSachartre int rv, rval = 0; 38552f5224aeSachartre size_t nbytes; 38562f5224aeSachartre vd_t *vd = task->vd; 38572f5224aeSachartre vd_dring_payload_t *request = task->request; 38582f5224aeSachartre 38592f5224aeSachartre ASSERT(request->operation == VD_OP_GET_ACCESS); 38602f5224aeSachartre ASSERT(vd->scsi); 38612f5224aeSachartre 38622f5224aeSachartre PR0("Performing VD_OP_GET_ACCESS"); 38632f5224aeSachartre 38642f5224aeSachartre nbytes = request->nbytes; 38652f5224aeSachartre 38662f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 38672f5224aeSachartre PR0("VD_OP_GET_ACCESS: Expected nbytes = %lu, got %lu", 38682f5224aeSachartre sizeof (uint64_t), nbytes); 38692f5224aeSachartre return (EINVAL); 38702f5224aeSachartre } 38712f5224aeSachartre 38722f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS, 38732f5224aeSachartre NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 38742f5224aeSachartre 38752f5224aeSachartre if (request->status != 0) 38762f5224aeSachartre return (0); 38772f5224aeSachartre 38782f5224aeSachartre access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED; 38792f5224aeSachartre 38802f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes, 38812f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 38822f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 38832f5224aeSachartre return (rv); 38842f5224aeSachartre } 38852f5224aeSachartre 38862f5224aeSachartre return (0); 38872f5224aeSachartre } 38882f5224aeSachartre 38892f5224aeSachartre static int 38902f5224aeSachartre vd_set_access(vd_task_t *task) 38912f5224aeSachartre { 38922f5224aeSachartre uint64_t flags; 38932f5224aeSachartre int rv, rval; 38942f5224aeSachartre size_t nbytes; 38952f5224aeSachartre vd_t *vd = task->vd; 38962f5224aeSachartre vd_dring_payload_t *request = task->request; 38972f5224aeSachartre 38982f5224aeSachartre ASSERT(request->operation == VD_OP_SET_ACCESS); 38992f5224aeSachartre ASSERT(vd->scsi); 39002f5224aeSachartre 39012f5224aeSachartre nbytes = request->nbytes; 39022f5224aeSachartre 39032f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 39042f5224aeSachartre PR0("VD_OP_SET_ACCESS: Expected nbytes = %lu, got %lu", 39052f5224aeSachartre sizeof (uint64_t), nbytes); 39062f5224aeSachartre return (EINVAL); 39072f5224aeSachartre } 39082f5224aeSachartre 39092f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes, 39102f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_IN)) != 0) { 39112f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying from client", rv); 39122f5224aeSachartre return (rv); 39132f5224aeSachartre } 39142f5224aeSachartre 39152f5224aeSachartre if (flags == VD_ACCESS_SET_CLEAR) { 39162f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (CLEAR)"); 39172f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 39182f5224aeSachartre MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred, 39192f5224aeSachartre &rval); 39202f5224aeSachartre if (request->status == 0) 39212f5224aeSachartre vd->ownership = B_FALSE; 39222f5224aeSachartre return (0); 39232f5224aeSachartre } 39242f5224aeSachartre 39252f5224aeSachartre /* 39262f5224aeSachartre * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid 39272f5224aeSachartre * when the EXCLUSIVE flag is set. 39282f5224aeSachartre */ 39292f5224aeSachartre if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) { 39302f5224aeSachartre PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags); 39312f5224aeSachartre request->status = EINVAL; 39322f5224aeSachartre return (0); 39332f5224aeSachartre } 39342f5224aeSachartre 39352f5224aeSachartre switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) { 39362f5224aeSachartre 39372f5224aeSachartre case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE: 39382f5224aeSachartre /* 39392f5224aeSachartre * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to 39402f5224aeSachartre * acquire exclusive access rights, preserve them and we 39412f5224aeSachartre * can use preemption. So we can use the MHIOCTKNOWN ioctl. 39422f5224aeSachartre */ 39432f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)"); 39442f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 39452f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 39462f5224aeSachartre break; 39472f5224aeSachartre 39482f5224aeSachartre case VD_ACCESS_SET_PRESERVE: 39492f5224aeSachartre /* 39502f5224aeSachartre * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive 39512f5224aeSachartre * access rights and preserve them, but not preempt any other 39522f5224aeSachartre * host. So we need to use the MHIOCTKOWN ioctl to enable the 39532f5224aeSachartre * "preserve" feature but we can not called it directly 39542f5224aeSachartre * because it uses preemption. So before that, we use the 39552f5224aeSachartre * MHIOCQRESERVE ioctl to ensure we can get exclusive rights 39562f5224aeSachartre * without preempting anyone. 39572f5224aeSachartre */ 39582f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)"); 39592f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 39602f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 39612f5224aeSachartre &rval); 39622f5224aeSachartre if (request->status != 0) 39632f5224aeSachartre break; 39642f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 39652f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 39662f5224aeSachartre break; 39672f5224aeSachartre 39682f5224aeSachartre case VD_ACCESS_SET_PREEMPT: 39692f5224aeSachartre /* 39702f5224aeSachartre * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive 39712f5224aeSachartre * access rights and we can use preemption. So we try to do 39722f5224aeSachartre * a SCSI reservation, if it fails we reset the disk to clear 39732f5224aeSachartre * any reservation and we try to reserve again. 39742f5224aeSachartre */ 39752f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)"); 39762f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 39772f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 39782f5224aeSachartre &rval); 39792f5224aeSachartre if (request->status == 0) 39802f5224aeSachartre break; 39812f5224aeSachartre 39822f5224aeSachartre /* reset the disk */ 39832f5224aeSachartre (void) vd_scsi_reset(vd); 39842f5224aeSachartre 39852f5224aeSachartre /* try again even if the reset has failed */ 39862f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 39872f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 39882f5224aeSachartre &rval); 39892f5224aeSachartre break; 39902f5224aeSachartre 39912f5224aeSachartre case 0: 39922f5224aeSachartre /* Flag EXCLUSIVE only. Just issue a SCSI reservation */ 39932f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)"); 39942f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 39952f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 39962f5224aeSachartre &rval); 39972f5224aeSachartre break; 39982f5224aeSachartre } 39992f5224aeSachartre 40002f5224aeSachartre if (request->status == 0) 40012f5224aeSachartre vd->ownership = B_TRUE; 40022f5224aeSachartre else 40032f5224aeSachartre PR0("VD_OP_SET_ACCESS: error %d", request->status); 40042f5224aeSachartre 40052f5224aeSachartre return (0); 40062f5224aeSachartre } 40072f5224aeSachartre 40082f5224aeSachartre static void 40092f5224aeSachartre vd_reset_access(vd_t *vd) 40102f5224aeSachartre { 40112f5224aeSachartre int status, rval; 40122f5224aeSachartre 40131aff8f07SAlexandre Chartre if (vd->file || vd->volume || !vd->ownership) 40142f5224aeSachartre return; 40152f5224aeSachartre 40162f5224aeSachartre PR0("Releasing disk ownership"); 40172f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 40182f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 40192f5224aeSachartre 40202f5224aeSachartre /* 40212f5224aeSachartre * An EACCES failure means that there is a reservation conflict, 40222f5224aeSachartre * so we are not the owner of the disk anymore. 40232f5224aeSachartre */ 40242f5224aeSachartre if (status == 0 || status == EACCES) { 40252f5224aeSachartre vd->ownership = B_FALSE; 40262f5224aeSachartre return; 40272f5224aeSachartre } 40282f5224aeSachartre 40292f5224aeSachartre PR0("Fail to release ownership, error %d", status); 40302f5224aeSachartre 40312f5224aeSachartre /* 40322f5224aeSachartre * We have failed to release the ownership, try to reset the disk 40332f5224aeSachartre * to release reservations. 40342f5224aeSachartre */ 40352f5224aeSachartre PR0("Resetting disk"); 40362f5224aeSachartre status = vd_scsi_reset(vd); 40372f5224aeSachartre 40382f5224aeSachartre if (status != 0) 40392f5224aeSachartre PR0("Fail to reset disk, error %d", status); 40402f5224aeSachartre 40412f5224aeSachartre /* whatever the result of the reset is, we try the release again */ 40422f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 40432f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 40442f5224aeSachartre 40452f5224aeSachartre if (status == 0 || status == EACCES) { 40462f5224aeSachartre vd->ownership = B_FALSE; 40472f5224aeSachartre return; 40482f5224aeSachartre } 40492f5224aeSachartre 40502f5224aeSachartre PR0("Fail to release ownership, error %d", status); 40512f5224aeSachartre 40522f5224aeSachartre /* 40532f5224aeSachartre * At this point we have done our best to try to reset the 40542f5224aeSachartre * access rights to the disk and we don't know if we still 40552f5224aeSachartre * own a reservation and if any mechanism to preserve the 40562f5224aeSachartre * ownership is still in place. The ultimate solution would 40572f5224aeSachartre * be to reset the system but this is usually not what we 40582f5224aeSachartre * want to happen. 40592f5224aeSachartre */ 40602f5224aeSachartre 40612f5224aeSachartre if (vd_reset_access_failure == A_REBOOT) { 40622f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG 40632f5224aeSachartre ", rebooting the system", vd->device_path); 40642f5224aeSachartre (void) uadmin(A_SHUTDOWN, AD_BOOT, NULL); 40652f5224aeSachartre } else if (vd_reset_access_failure == A_DUMP) { 40662f5224aeSachartre panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 40672f5224aeSachartre } 40682f5224aeSachartre 40692f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 40702f5224aeSachartre } 40712f5224aeSachartre 40721ae08745Sheppo /* 40731ae08745Sheppo * Define the supported operations once the functions for performing them have 40741ae08745Sheppo * been defined 40751ae08745Sheppo */ 40761ae08745Sheppo static const vds_operation_t vds_operation[] = { 40773af08d82Slm66018 #define X(_s) #_s, _s 40783af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 40793af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 40803af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 40813af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 40823af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 40833af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 40843af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 40853af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 40863af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 40873af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 40883af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 40893af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 40902f5224aeSachartre {X(VD_OP_SCSICMD), vd_ioctl, NULL}, 40912f5224aeSachartre {X(VD_OP_RESET), vd_reset, NULL}, 40922f5224aeSachartre {X(VD_OP_GET_CAPACITY), vd_get_capacity, NULL}, 40932f5224aeSachartre {X(VD_OP_SET_ACCESS), vd_set_access, NULL}, 40942f5224aeSachartre {X(VD_OP_GET_ACCESS), vd_get_access, NULL}, 40953af08d82Slm66018 #undef X 40961ae08745Sheppo }; 40971ae08745Sheppo 40981ae08745Sheppo static const size_t vds_noperations = 40991ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 41001ae08745Sheppo 41011ae08745Sheppo /* 4102d10e4ef2Snarayan * Process a task specifying a client I/O request 4103205eeb1aSlm66018 * 4104205eeb1aSlm66018 * Parameters: 4105205eeb1aSlm66018 * task - structure containing the request sent from client 4106205eeb1aSlm66018 * 4107205eeb1aSlm66018 * Return Value 4108205eeb1aSlm66018 * 0 - success 4109205eeb1aSlm66018 * ENOTSUP - Unknown/Unsupported VD_OP_XXX operation 4110205eeb1aSlm66018 * EINVAL - Invalid disk slice 4111205eeb1aSlm66018 * != 0 - some other non-zero return value from start function 41121ae08745Sheppo */ 41131ae08745Sheppo static int 4114205eeb1aSlm66018 vd_do_process_task(vd_task_t *task) 41151ae08745Sheppo { 4116205eeb1aSlm66018 int i; 4117d10e4ef2Snarayan vd_t *vd = task->vd; 4118d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 41191ae08745Sheppo 4120d10e4ef2Snarayan ASSERT(vd != NULL); 4121d10e4ef2Snarayan ASSERT(request != NULL); 41221ae08745Sheppo 4123d10e4ef2Snarayan /* Find the requested operation */ 4124205eeb1aSlm66018 for (i = 0; i < vds_noperations; i++) { 4125205eeb1aSlm66018 if (request->operation == vds_operation[i].operation) { 4126205eeb1aSlm66018 /* all operations should have a start func */ 4127205eeb1aSlm66018 ASSERT(vds_operation[i].start != NULL); 4128205eeb1aSlm66018 4129205eeb1aSlm66018 task->completef = vds_operation[i].complete; 4130d10e4ef2Snarayan break; 4131205eeb1aSlm66018 } 4132205eeb1aSlm66018 } 413317cadca8Slm66018 413417cadca8Slm66018 /* 413517cadca8Slm66018 * We need to check that the requested operation is permitted 413617cadca8Slm66018 * for the particular client that sent it or that the loop above 413717cadca8Slm66018 * did not complete without finding the operation type (indicating 413817cadca8Slm66018 * that the requested operation is unknown/unimplemented) 413917cadca8Slm66018 */ 414017cadca8Slm66018 if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) || 414117cadca8Slm66018 (i == vds_noperations)) { 41423af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 414317cadca8Slm66018 request->status = ENOTSUP; 414417cadca8Slm66018 return (0); 41451ae08745Sheppo } 41461ae08745Sheppo 41477636cb21Slm66018 /* Range-check slice */ 414887a7269eSachartre if (request->slice >= vd->nslices && 4149bae9e67eSachartre ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) || 415087a7269eSachartre request->slice != VD_SLICE_NONE)) { 41513af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 41527636cb21Slm66018 request->slice, (vd->nslices - 1)); 4153bae9e67eSachartre request->status = EINVAL; 4154bae9e67eSachartre return (0); 41557636cb21Slm66018 } 41567636cb21Slm66018 4157205eeb1aSlm66018 /* 4158205eeb1aSlm66018 * Call the function pointer that starts the operation. 4159205eeb1aSlm66018 */ 4160205eeb1aSlm66018 return (vds_operation[i].start(task)); 41611ae08745Sheppo } 41621ae08745Sheppo 4163205eeb1aSlm66018 /* 4164205eeb1aSlm66018 * Description: 4165205eeb1aSlm66018 * This function is called by both the in-band and descriptor ring 4166205eeb1aSlm66018 * message processing functions paths to actually execute the task 4167205eeb1aSlm66018 * requested by the vDisk client. It in turn calls its worker 4168205eeb1aSlm66018 * function, vd_do_process_task(), to carry our the request. 4169205eeb1aSlm66018 * 4170205eeb1aSlm66018 * Any transport errors (e.g. LDC errors, vDisk protocol errors) are 4171205eeb1aSlm66018 * saved in the 'status' field of the task and are propagated back 4172205eeb1aSlm66018 * up the call stack to trigger a NACK 4173205eeb1aSlm66018 * 4174205eeb1aSlm66018 * Any request errors (e.g. ENOTTY from an ioctl) are saved in 4175205eeb1aSlm66018 * the 'status' field of the request and result in an ACK being sent 4176205eeb1aSlm66018 * by the completion handler. 4177205eeb1aSlm66018 * 4178205eeb1aSlm66018 * Parameters: 4179205eeb1aSlm66018 * task - structure containing the request sent from client 4180205eeb1aSlm66018 * 4181205eeb1aSlm66018 * Return Value 4182205eeb1aSlm66018 * 0 - successful synchronous request. 4183205eeb1aSlm66018 * != 0 - transport error (e.g. LDC errors, vDisk protocol) 4184205eeb1aSlm66018 * EINPROGRESS - task will be finished in a completion handler 4185205eeb1aSlm66018 */ 4186205eeb1aSlm66018 static int 4187205eeb1aSlm66018 vd_process_task(vd_task_t *task) 4188205eeb1aSlm66018 { 4189205eeb1aSlm66018 vd_t *vd = task->vd; 4190205eeb1aSlm66018 int status; 41911ae08745Sheppo 4192205eeb1aSlm66018 DTRACE_PROBE1(task__start, vd_task_t *, task); 41933af08d82Slm66018 4194205eeb1aSlm66018 task->status = vd_do_process_task(task); 4195205eeb1aSlm66018 4196205eeb1aSlm66018 /* 4197205eeb1aSlm66018 * If the task processing function returned EINPROGRESS indicating 4198205eeb1aSlm66018 * that the task needs completing then schedule a taskq entry to 4199205eeb1aSlm66018 * finish it now. 4200205eeb1aSlm66018 * 4201205eeb1aSlm66018 * Otherwise the task processing function returned either zero 4202205eeb1aSlm66018 * indicating that the task was finished in the start function (and we 4203205eeb1aSlm66018 * don't need to wait in a completion function) or the start function 4204205eeb1aSlm66018 * returned an error - in both cases all that needs to happen is the 4205205eeb1aSlm66018 * notification to the vDisk client higher up the call stack. 4206205eeb1aSlm66018 * If the task was using a Descriptor Ring, we need to mark it as done 4207205eeb1aSlm66018 * at this stage. 4208205eeb1aSlm66018 */ 4209205eeb1aSlm66018 if (task->status == EINPROGRESS) { 4210d10e4ef2Snarayan /* Queue a task to complete the operation */ 4211205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_complete, 4212d10e4ef2Snarayan task, DDI_SLEEP); 421383990c4aSAlexandre Chartre return (EINPROGRESS); 421483990c4aSAlexandre Chartre } 4215d10e4ef2Snarayan 421683990c4aSAlexandre Chartre if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 4217205eeb1aSlm66018 /* Update the dring element if it's a dring client */ 4218205eeb1aSlm66018 status = vd_mark_elem_done(vd, task->index, 4219205eeb1aSlm66018 task->request->status, task->request->nbytes); 4220205eeb1aSlm66018 if (status == ECONNRESET) 4221205eeb1aSlm66018 vd_mark_in_reset(vd); 4222bbfa0259Sha137994 else if (status == EACCES) 4223bbfa0259Sha137994 vd_need_reset(vd, B_TRUE); 4224205eeb1aSlm66018 } 4225205eeb1aSlm66018 4226205eeb1aSlm66018 return (task->status); 42271ae08745Sheppo } 42281ae08745Sheppo 42291ae08745Sheppo /* 42300a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 42310a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 42321ae08745Sheppo */ 42330a55fbb7Slm66018 boolean_t 42341ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 42351ae08745Sheppo { 42361ae08745Sheppo return ((tag->vio_msgtype == type) && 42371ae08745Sheppo (tag->vio_subtype == subtype) && 42380a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 42391ae08745Sheppo } 42401ae08745Sheppo 42410a55fbb7Slm66018 /* 42420a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 42430a55fbb7Slm66018 * by this server. 42440a55fbb7Slm66018 */ 42450a55fbb7Slm66018 static boolean_t 42460a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 42470a55fbb7Slm66018 { 42480a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 42490a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 42500a55fbb7Slm66018 ASSERT((i == 0) || 42510a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 42520a55fbb7Slm66018 42530a55fbb7Slm66018 /* 42540a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 42550a55fbb7Slm66018 * necessary, down to the highest value supported by this 42560a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 42570a55fbb7Slm66018 * the client should also support all minor versions lower 42580a55fbb7Slm66018 * than the value it sent 42590a55fbb7Slm66018 */ 42600a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 42610a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 42620a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 42630a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 42640a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 42650a55fbb7Slm66018 } 42660a55fbb7Slm66018 return (B_TRUE); 42670a55fbb7Slm66018 } 42680a55fbb7Slm66018 42690a55fbb7Slm66018 /* 42700a55fbb7Slm66018 * If the message contains a higher major version number, set 42710a55fbb7Slm66018 * the message's major/minor versions to the current values 42720a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 42730a55fbb7Slm66018 * these values, and the client will potentially try again 42740a55fbb7Slm66018 * with the same or a lower version 42750a55fbb7Slm66018 */ 42760a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 42770a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 42780a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 42790a55fbb7Slm66018 return (B_FALSE); 42800a55fbb7Slm66018 } 42810a55fbb7Slm66018 42820a55fbb7Slm66018 /* 42830a55fbb7Slm66018 * Otherwise, the message's major version is less than the 42840a55fbb7Slm66018 * current major version, so continue the loop to the next 42850a55fbb7Slm66018 * (lower) supported version 42860a55fbb7Slm66018 */ 42870a55fbb7Slm66018 } 42880a55fbb7Slm66018 42890a55fbb7Slm66018 /* 42900a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 42910a55fbb7Slm66018 * message to terminate negotiation 42920a55fbb7Slm66018 */ 42930a55fbb7Slm66018 ver_msg->ver_major = 0; 42940a55fbb7Slm66018 ver_msg->ver_minor = 0; 42950a55fbb7Slm66018 return (B_FALSE); 42960a55fbb7Slm66018 } 42970a55fbb7Slm66018 42980a55fbb7Slm66018 /* 42990a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 43000a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 43010a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 43020a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 43030a55fbb7Slm66018 * messages as invalid). 43040a55fbb7Slm66018 */ 43051ae08745Sheppo static int 43060a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 43071ae08745Sheppo { 43081ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 43091ae08745Sheppo 43101ae08745Sheppo 43111ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 43121ae08745Sheppo 43131ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 43141ae08745Sheppo VIO_VER_INFO)) { 43151ae08745Sheppo return (ENOMSG); /* not a version message */ 43161ae08745Sheppo } 43171ae08745Sheppo 43181ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 43193af08d82Slm66018 PR0("Expected %lu-byte version message; " 43201ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 43211ae08745Sheppo return (EBADMSG); 43221ae08745Sheppo } 43231ae08745Sheppo 43241ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 43253af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 43261ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 43271ae08745Sheppo return (EBADMSG); 43281ae08745Sheppo } 43291ae08745Sheppo 43300a55fbb7Slm66018 /* 43310a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 43320a55fbb7Slm66018 * for "ack/nack" back to the client 43330a55fbb7Slm66018 */ 43341ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 43350a55fbb7Slm66018 43360a55fbb7Slm66018 /* 43370a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 43380a55fbb7Slm66018 * supported by this server. If the version is not supported, return 43390a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 43400a55fbb7Slm66018 * will have updated the message with a supported version for the 43410a55fbb7Slm66018 * client to consider 43420a55fbb7Slm66018 */ 43430a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 43440a55fbb7Slm66018 return (EBADMSG); 43450a55fbb7Slm66018 43460a55fbb7Slm66018 43470a55fbb7Slm66018 /* 43480a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 43490a55fbb7Slm66018 * communication on this channel now 43500a55fbb7Slm66018 */ 43510a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 43520a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 43530a55fbb7Slm66018 vd->initialized |= VD_SID; 43540a55fbb7Slm66018 43550a55fbb7Slm66018 /* 435617cadca8Slm66018 * Store the negotiated major and minor version values in the "vd" data 435717cadca8Slm66018 * structure so that we can check if certain operations are supported 435817cadca8Slm66018 * by the client. 43590a55fbb7Slm66018 */ 436017cadca8Slm66018 vd->version.major = ver_msg->ver_major; 436117cadca8Slm66018 vd->version.minor = ver_msg->ver_minor; 43620a55fbb7Slm66018 43630a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 43640a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 43651ae08745Sheppo return (0); 43661ae08745Sheppo } 43671ae08745Sheppo 436817cadca8Slm66018 static void 436917cadca8Slm66018 vd_set_exported_operations(vd_t *vd) 437017cadca8Slm66018 { 437117cadca8Slm66018 vd->operations = 0; /* clear field */ 437217cadca8Slm66018 437317cadca8Slm66018 /* 437417cadca8Slm66018 * We need to check from the highest version supported to the 437517cadca8Slm66018 * lowest because versions with a higher minor number implicitly 437617cadca8Slm66018 * support versions with a lower minor number. 437717cadca8Slm66018 */ 437817cadca8Slm66018 if (vio_ver_is_supported(vd->version, 1, 1)) { 437917cadca8Slm66018 ASSERT(vd->open_flags & FREAD); 4380de3a5331SRamesh Chitrothu vd->operations |= VD_OP_MASK_READ | (1 << VD_OP_GET_CAPACITY); 438117cadca8Slm66018 438217cadca8Slm66018 if (vd->open_flags & FWRITE) 438317cadca8Slm66018 vd->operations |= VD_OP_MASK_WRITE; 438417cadca8Slm66018 43852f5224aeSachartre if (vd->scsi) 43862f5224aeSachartre vd->operations |= VD_OP_MASK_SCSI; 43872f5224aeSachartre 43881aff8f07SAlexandre Chartre if (VD_DSKIMG(vd) && vd_dskimg_is_iso_image(vd)) { 438917cadca8Slm66018 /* 439017cadca8Slm66018 * can't write to ISO images, make sure that write 439117cadca8Slm66018 * support is not set in case administrator did not 439217cadca8Slm66018 * use "options=ro" when doing an ldm add-vdsdev 439317cadca8Slm66018 */ 439417cadca8Slm66018 vd->operations &= ~VD_OP_MASK_WRITE; 439517cadca8Slm66018 } 439617cadca8Slm66018 } else if (vio_ver_is_supported(vd->version, 1, 0)) { 439717cadca8Slm66018 vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE; 439817cadca8Slm66018 } 439917cadca8Slm66018 440017cadca8Slm66018 /* we should have already agreed on a version */ 440117cadca8Slm66018 ASSERT(vd->operations != 0); 440217cadca8Slm66018 } 440317cadca8Slm66018 44041ae08745Sheppo static int 44051ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 44061ae08745Sheppo { 44071ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 44083c96341aSnarayan int status, retry = 0; 44091ae08745Sheppo 44101ae08745Sheppo 44111ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 44121ae08745Sheppo 44131ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 44141ae08745Sheppo VIO_ATTR_INFO)) { 4415d10e4ef2Snarayan PR0("Message is not an attribute message"); 4416d10e4ef2Snarayan return (ENOMSG); 44171ae08745Sheppo } 44181ae08745Sheppo 44191ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 44203af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 44211ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 44221ae08745Sheppo return (EBADMSG); 44231ae08745Sheppo } 44241ae08745Sheppo 44251ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 44263af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 44271ae08745Sheppo return (EBADMSG); 44281ae08745Sheppo } 44291ae08745Sheppo 44301ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 4431f0ca1d9aSsb155480 (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) { 44323af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 44331ae08745Sheppo return (EBADMSG); 44341ae08745Sheppo } 44351ae08745Sheppo 44363c96341aSnarayan /* 44373c96341aSnarayan * check if the underlying disk is ready, if not try accessing 44383c96341aSnarayan * the device again. Open the vdisk device and extract info 44393c96341aSnarayan * about it, as this is needed to respond to the attr info msg 44403c96341aSnarayan */ 44413c96341aSnarayan if ((vd->initialized & VD_DISK_READY) == 0) { 44423c96341aSnarayan PR0("Retry setting up disk (%s)", vd->device_path); 44433c96341aSnarayan do { 44443c96341aSnarayan status = vd_setup_vd(vd); 44453c96341aSnarayan if (status != EAGAIN || ++retry > vds_dev_retries) 44463c96341aSnarayan break; 44473c96341aSnarayan 44483c96341aSnarayan /* incremental delay */ 44493c96341aSnarayan delay(drv_usectohz(vds_dev_delay)); 44503c96341aSnarayan 44513c96341aSnarayan /* if vdisk is no longer enabled - return error */ 44523c96341aSnarayan if (!vd_enabled(vd)) 44533c96341aSnarayan return (ENXIO); 44543c96341aSnarayan 44553c96341aSnarayan } while (status == EAGAIN); 44563c96341aSnarayan 44573c96341aSnarayan if (status) 44583c96341aSnarayan return (ENXIO); 44593c96341aSnarayan 44603c96341aSnarayan vd->initialized |= VD_DISK_READY; 44613c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 44628fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 44633c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 44648fce2fd6Sachartre (vd->volume ? "yes" : "no"), 44653c96341aSnarayan (vd->file ? "yes" : "no"), 44663c96341aSnarayan vd->nslices); 44673c96341aSnarayan } 44683c96341aSnarayan 44691ae08745Sheppo /* Success: valid message and transfer mode */ 44701ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 44713af08d82Slm66018 44721ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 44733af08d82Slm66018 44741ae08745Sheppo /* 44751ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 44761ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 44771ae08745Sheppo * pages plus possibly one partial page required to cover 44781ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 44791ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 44801ae08745Sheppo * Must first get the maximum transfer size in bytes. 44811ae08745Sheppo */ 44821ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 44831ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 44841ae08745Sheppo attr_msg->max_xfer_sz; 44851ae08745Sheppo size_t max_inband_msglen = 44861ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 44871ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 44881ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 44891ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 44901ae08745Sheppo 44911ae08745Sheppo /* 44921ae08745Sheppo * Set the maximum expected message length to 44931ae08745Sheppo * accommodate in-band-descriptor messages with all 44941ae08745Sheppo * their cookies 44951ae08745Sheppo */ 44961ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 4497d10e4ef2Snarayan 4498d10e4ef2Snarayan /* 4499d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 4500d10e4ef2Snarayan * request descriptors 4501d10e4ef2Snarayan */ 4502d10e4ef2Snarayan vd->inband_task.vd = vd; 45033af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4504d10e4ef2Snarayan vd->inband_task.index = 0; 4505d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 45061ae08745Sheppo } 45071ae08745Sheppo 4508e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 45092f5224aeSachartre attr_msg->vdisk_block_size = vd->vdisk_block_size; 4510e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 4511e1ebb9ecSlm66018 45121ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 4513bae9e67eSachartre attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type : 4514bae9e67eSachartre VD_DISK_TYPE_DISK; 451517cadca8Slm66018 attr_msg->vdisk_media = vd->vdisk_media; 451617cadca8Slm66018 451717cadca8Slm66018 /* Discover and save the list of supported VD_OP_XXX operations */ 451817cadca8Slm66018 vd_set_exported_operations(vd); 451917cadca8Slm66018 attr_msg->operations = vd->operations; 452017cadca8Slm66018 45211ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 45223af08d82Slm66018 45233af08d82Slm66018 ASSERT(vd->dring_task == NULL); 45243af08d82Slm66018 45251ae08745Sheppo return (0); 45261ae08745Sheppo } 45271ae08745Sheppo 45281ae08745Sheppo static int 45291ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 45301ae08745Sheppo { 45311ae08745Sheppo int status; 45321ae08745Sheppo size_t expected; 45331ae08745Sheppo ldc_mem_info_t dring_minfo; 4534bbfa0259Sha137994 uint8_t mtype; 45351ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 45361ae08745Sheppo 45371ae08745Sheppo 45381ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 45391ae08745Sheppo 45401ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 45411ae08745Sheppo VIO_DRING_REG)) { 4542d10e4ef2Snarayan PR0("Message is not a register-dring message"); 4543d10e4ef2Snarayan return (ENOMSG); 45441ae08745Sheppo } 45451ae08745Sheppo 45461ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 45473af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 45481ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 45491ae08745Sheppo return (EBADMSG); 45501ae08745Sheppo } 45511ae08745Sheppo 45521ae08745Sheppo expected = sizeof (*reg_msg) + 45531ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 45541ae08745Sheppo if (msglen != expected) { 45553af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 45561ae08745Sheppo "received %lu bytes", expected, msglen); 45571ae08745Sheppo return (EBADMSG); 45581ae08745Sheppo } 45591ae08745Sheppo 45601ae08745Sheppo if (vd->initialized & VD_DRING) { 45613af08d82Slm66018 PR0("A dring was previously registered; only support one"); 45621ae08745Sheppo return (EBADMSG); 45631ae08745Sheppo } 45641ae08745Sheppo 4565d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 45663af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 4567d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 4568d10e4ef2Snarayan return (EBADMSG); 4569d10e4ef2Snarayan } 4570d10e4ef2Snarayan 45711ae08745Sheppo if (reg_msg->ncookies != 1) { 45721ae08745Sheppo /* 45731ae08745Sheppo * In addition to fixing the assertion in the success case 45741ae08745Sheppo * below, supporting drings which require more than one 45751ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 45761ae08745Sheppo * somewhere in the code path prior to receiving the message 45771ae08745Sheppo * which results in calling this function. Note that without 45781ae08745Sheppo * making this change, the larger message size required to 45791ae08745Sheppo * accommodate multiple cookies cannot be successfully 45801ae08745Sheppo * received, so this function will not even get called. 45811ae08745Sheppo * Gracefully accommodating more dring cookies might 45821ae08745Sheppo * reasonably demand exchanging an additional attribute or 45831ae08745Sheppo * making a minor protocol adjustment 45841ae08745Sheppo */ 45853af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 45861ae08745Sheppo return (EBADMSG); 45871ae08745Sheppo } 45881ae08745Sheppo 4589bbfa0259Sha137994 if (vd_direct_mapped_drings) 4590bbfa0259Sha137994 mtype = LDC_DIRECT_MAP; 4591bbfa0259Sha137994 else 4592bbfa0259Sha137994 mtype = LDC_SHADOW_MAP; 4593bbfa0259Sha137994 45941ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 45951ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 4596bbfa0259Sha137994 reg_msg->descriptor_size, mtype, &vd->dring_handle); 45971ae08745Sheppo if (status != 0) { 45983af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 45991ae08745Sheppo return (status); 46001ae08745Sheppo } 46011ae08745Sheppo 46021ae08745Sheppo /* 46031ae08745Sheppo * To remove the need for this assertion, must call 46041ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 46051ae08745Sheppo * successful call to ldc_mem_dring_map() 46061ae08745Sheppo */ 46071ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 46081ae08745Sheppo 46091ae08745Sheppo if ((status = 46101ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 46113af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 46121ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 46133af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 46141ae08745Sheppo return (status); 46151ae08745Sheppo } 46161ae08745Sheppo 46171ae08745Sheppo if (dring_minfo.vaddr == NULL) { 46183af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 46190a55fbb7Slm66018 return (ENXIO); 46201ae08745Sheppo } 46211ae08745Sheppo 46221ae08745Sheppo 4623d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 46241ae08745Sheppo vd->initialized |= VD_DRING; 46251ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 46261ae08745Sheppo vd->dring = dring_minfo.vaddr; 46271ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 46281ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 4629bbfa0259Sha137994 vd->dring_mtype = dring_minfo.mtype; 46301ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 46315b7cb889Sha137994 PR1("descriptor size = %u, dring length = %u", 46325b7cb889Sha137994 vd->descriptor_size, vd->dring_len); 4633d10e4ef2Snarayan 4634d10e4ef2Snarayan /* 4635d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 4636d10e4ef2Snarayan * tasks to process I/O requests in dring elements 4637d10e4ef2Snarayan */ 4638d10e4ef2Snarayan vd->dring_task = 4639d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 4640d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 4641d10e4ef2Snarayan vd->dring_task[i].vd = vd; 4642d10e4ef2Snarayan vd->dring_task[i].index = i; 46434bac2208Snarayan 46444bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 46454bac2208Snarayan &(vd->dring_task[i].mhdl)); 46464bac2208Snarayan if (status) { 46473af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 46484bac2208Snarayan return (ENXIO); 46494bac2208Snarayan } 46503af08d82Slm66018 46515b7cb889Sha137994 /* 46525b7cb889Sha137994 * The descriptor payload varies in length. Calculate its 46535b7cb889Sha137994 * size by subtracting the header size from the total 46545b7cb889Sha137994 * descriptor size. 46555b7cb889Sha137994 */ 46565b7cb889Sha137994 vd->dring_task[i].request = kmem_zalloc((vd->descriptor_size - 46575b7cb889Sha137994 sizeof (vio_dring_entry_hdr_t)), KM_SLEEP); 46583af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4659d10e4ef2Snarayan } 4660d10e4ef2Snarayan 466183990c4aSAlexandre Chartre if (vd->file || vd->zvol) { 466283990c4aSAlexandre Chartre vd->write_queue = 466383990c4aSAlexandre Chartre kmem_zalloc(sizeof (buf_t *) * vd->dring_len, KM_SLEEP); 466483990c4aSAlexandre Chartre } 466583990c4aSAlexandre Chartre 46661ae08745Sheppo return (0); 46671ae08745Sheppo } 46681ae08745Sheppo 46691ae08745Sheppo static int 46701ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 46711ae08745Sheppo { 46721ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 46731ae08745Sheppo 46741ae08745Sheppo 46751ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 46761ae08745Sheppo 46771ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 46781ae08745Sheppo VIO_DRING_UNREG)) { 4679d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 4680d10e4ef2Snarayan return (ENOMSG); 46811ae08745Sheppo } 46821ae08745Sheppo 46831ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 46843af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 46851ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 46861ae08745Sheppo return (EBADMSG); 46871ae08745Sheppo } 46881ae08745Sheppo 46891ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 46903af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 46911ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 46921ae08745Sheppo return (EBADMSG); 46931ae08745Sheppo } 46941ae08745Sheppo 46951ae08745Sheppo return (0); 46961ae08745Sheppo } 46971ae08745Sheppo 46981ae08745Sheppo static int 46991ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 47001ae08745Sheppo { 47011ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 47021ae08745Sheppo 4703d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 4704d10e4ef2Snarayan PR0("Message is not an RDX message"); 4705d10e4ef2Snarayan return (ENOMSG); 4706d10e4ef2Snarayan } 47071ae08745Sheppo 47081ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 47093af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 47101ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 47111ae08745Sheppo return (EBADMSG); 47121ae08745Sheppo } 47131ae08745Sheppo 4714d10e4ef2Snarayan PR0("Valid RDX message"); 47151ae08745Sheppo return (0); 47161ae08745Sheppo } 47171ae08745Sheppo 47181ae08745Sheppo static int 47191ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 47201ae08745Sheppo { 47211ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 47223af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 47231ae08745Sheppo seq_num, (vd->seq_num + 1)); 47243af08d82Slm66018 PR0("initiating soft reset"); 4725d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 47261ae08745Sheppo return (1); 47271ae08745Sheppo } 47281ae08745Sheppo 47291ae08745Sheppo vd->seq_num = seq_num; 47301ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 47311ae08745Sheppo return (0); 47321ae08745Sheppo } 47331ae08745Sheppo 47341ae08745Sheppo /* 47351ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 47361ae08745Sheppo * cookies it claims to include 47371ae08745Sheppo */ 47381ae08745Sheppo static size_t 47391ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 47401ae08745Sheppo { 47411ae08745Sheppo return ((sizeof (*msg)) + 47421ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 47431ae08745Sheppo } 47441ae08745Sheppo 47451ae08745Sheppo /* 47461ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 47471ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 47481ae08745Sheppo * operating on them within a descriptor ring 47491ae08745Sheppo */ 47501ae08745Sheppo static int 47513af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 47521ae08745Sheppo { 47531ae08745Sheppo size_t expected; 47541ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 47551ae08745Sheppo 47561ae08745Sheppo 47571ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 47581ae08745Sheppo 47591ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 4760d10e4ef2Snarayan VIO_DESC_DATA)) { 4761d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 4762d10e4ef2Snarayan return (ENOMSG); 4763d10e4ef2Snarayan } 47641ae08745Sheppo 47651ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 47663af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 47671ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 47681ae08745Sheppo return (EBADMSG); 47691ae08745Sheppo } 47701ae08745Sheppo 47711ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 47723af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 47731ae08745Sheppo "received %lu bytes", expected, msglen); 47741ae08745Sheppo return (EBADMSG); 47751ae08745Sheppo } 47761ae08745Sheppo 4777d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 47781ae08745Sheppo return (EBADMSG); 47791ae08745Sheppo 4780d10e4ef2Snarayan /* 4781d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 4782d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 4783d10e4ef2Snarayan * error processing the descriptor task results in setting 4784d10e4ef2Snarayan * VIO_SUBTYPE_NACK 4785d10e4ef2Snarayan */ 4786d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 4787d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 47883af08d82Slm66018 47893af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 47903af08d82Slm66018 47913af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 4792d10e4ef2Snarayan vd->inband_task.msglen = msglen; 47933af08d82Slm66018 47943af08d82Slm66018 /* 47953af08d82Slm66018 * The task request is now the payload of the message 47963af08d82Slm66018 * that was just copied into the body of the task. 47973af08d82Slm66018 */ 47983af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 4799d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 48003af08d82Slm66018 4801d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 48021ae08745Sheppo } 48031ae08745Sheppo 48041ae08745Sheppo static int 4805d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 48063af08d82Slm66018 vio_msg_t *msg, size_t msglen) 48071ae08745Sheppo { 48081ae08745Sheppo int status; 4809d10e4ef2Snarayan boolean_t ready; 4810bbfa0259Sha137994 on_trap_data_t otd; 4811d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 48121ae08745Sheppo 4813d10e4ef2Snarayan /* Accept the updated dring element */ 4814bbfa0259Sha137994 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype, 4815bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 48161ae08745Sheppo return (status); 48171ae08745Sheppo } 4818d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 4819d10e4ef2Snarayan if (ready) { 4820d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 48215b7cb889Sha137994 bcopy(&elem->payload, vd->dring_task[idx].request, 48225b7cb889Sha137994 (vd->descriptor_size - sizeof (vio_dring_entry_hdr_t))); 4823d10e4ef2Snarayan } else { 48243af08d82Slm66018 PR0("descriptor %u not ready", idx); 4825d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 4826d10e4ef2Snarayan } 4827bbfa0259Sha137994 if ((status = VIO_DRING_RELEASE(vd->dring_mtype, 4828bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 4829bbfa0259Sha137994 PR0("VIO_DRING_RELEASE() returned errno %d", status); 48301ae08745Sheppo return (status); 48311ae08745Sheppo } 4832d10e4ef2Snarayan if (!ready) 4833d10e4ef2Snarayan return (EBUSY); 48341ae08745Sheppo 48351ae08745Sheppo 4836d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 4837d10e4ef2Snarayan PR1("Processing dring element %u", idx); 4838d10e4ef2Snarayan vd->dring_task[idx].type = type; 48393af08d82Slm66018 48403af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 48413af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 48423af08d82Slm66018 4843d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 4844205eeb1aSlm66018 return (vd_process_task(&vd->dring_task[idx])); 48451ae08745Sheppo } 48461ae08745Sheppo 48471ae08745Sheppo static int 4848d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 48493af08d82Slm66018 vio_msg_t *msg, size_t msglen) 4850d10e4ef2Snarayan { 4851d10e4ef2Snarayan int i, n, nelem, status = 0; 4852d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 4853d10e4ef2Snarayan vd_task_type_t type; 4854d10e4ef2Snarayan 4855d10e4ef2Snarayan 4856d10e4ef2Snarayan ASSERT(start >= 0); 4857d10e4ef2Snarayan ASSERT(end >= 0); 4858d10e4ef2Snarayan 4859d10e4ef2Snarayan /* 4860d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 4861d10e4ef2Snarayan * processing one of the dring elements results in setting 4862d10e4ef2Snarayan * VIO_SUBTYPE_NACK 4863d10e4ef2Snarayan */ 4864d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 4865d10e4ef2Snarayan 4866d10e4ef2Snarayan /* 4867d10e4ef2Snarayan * Process the dring elements in the range 4868d10e4ef2Snarayan */ 4869d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 4870d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 4871d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 4872d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 48733af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 4874d10e4ef2Snarayan if (status == EINPROGRESS) 4875d10e4ef2Snarayan inprogress = B_TRUE; 4876d10e4ef2Snarayan else if (status != 0) 4877d10e4ef2Snarayan break; 4878d10e4ef2Snarayan } 4879d10e4ef2Snarayan 4880d10e4ef2Snarayan /* 4881d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 4882d10e4ef2Snarayan * progress, wait for other operations to complete before returning 4883d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 4884d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 4885d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 4886d10e4ef2Snarayan * this situation is an error case, performance is less critical. 4887d10e4ef2Snarayan */ 488883990c4aSAlexandre Chartre if ((nelem > 1) && (status != EINPROGRESS) && inprogress) { 488983990c4aSAlexandre Chartre if (vd->ioq != NULL) 489083990c4aSAlexandre Chartre ddi_taskq_wait(vd->ioq); 4891d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 489283990c4aSAlexandre Chartre } 4893d10e4ef2Snarayan 4894d10e4ef2Snarayan return (status); 4895d10e4ef2Snarayan } 4896d10e4ef2Snarayan 4897d10e4ef2Snarayan static int 48983af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 48991ae08745Sheppo { 49001ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 49011ae08745Sheppo 49021ae08745Sheppo 49031ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 49041ae08745Sheppo 49051ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 49061ae08745Sheppo VIO_DRING_DATA)) { 4907d10e4ef2Snarayan PR1("Message is not a dring-data message"); 4908d10e4ef2Snarayan return (ENOMSG); 49091ae08745Sheppo } 49101ae08745Sheppo 49111ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 49123af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 49131ae08745Sheppo sizeof (*dring_msg), msglen); 49141ae08745Sheppo return (EBADMSG); 49151ae08745Sheppo } 49161ae08745Sheppo 4917d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 49181ae08745Sheppo return (EBADMSG); 49191ae08745Sheppo 49201ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 49213af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 49221ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 49231ae08745Sheppo return (EBADMSG); 49241ae08745Sheppo } 49251ae08745Sheppo 4926d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 49273af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 4928d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 4929d10e4ef2Snarayan return (EBADMSG); 4930d10e4ef2Snarayan } 49311ae08745Sheppo 4932d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 4933d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 49343af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 4935d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 4936d10e4ef2Snarayan return (EBADMSG); 4937d10e4ef2Snarayan } 4938d10e4ef2Snarayan 4939d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 4940d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 4941d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 4942d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 49433af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 49441ae08745Sheppo } 49451ae08745Sheppo 49461ae08745Sheppo static int 49471ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 49481ae08745Sheppo { 49491ae08745Sheppo int retry, status; 49501ae08745Sheppo size_t size = *nbytes; 49511ae08745Sheppo 49521ae08745Sheppo 49531ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 49541ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 49551ae08745Sheppo retry++) { 49561ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 49571ae08745Sheppo *nbytes = size; 49581ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 49591ae08745Sheppo } 49601ae08745Sheppo 49613af08d82Slm66018 if (status) { 49623af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 49633af08d82Slm66018 if (status != ECONNRESET) 49643af08d82Slm66018 return (ENOMSG); 49651ae08745Sheppo return (status); 49661ae08745Sheppo } else if (*nbytes == 0) { 49671ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 49681ae08745Sheppo return (ENOMSG); 49691ae08745Sheppo } 49701ae08745Sheppo 49711ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 49721ae08745Sheppo return (0); 49731ae08745Sheppo } 49741ae08745Sheppo 49751ae08745Sheppo static int 49763af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 49771ae08745Sheppo { 49781ae08745Sheppo int status; 49791ae08745Sheppo 49801ae08745Sheppo 49811ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 49821ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 49833af08d82Slm66018 #ifdef DEBUG 49843af08d82Slm66018 vd_decode_tag(msg); 49853af08d82Slm66018 #endif 49861ae08745Sheppo 49871ae08745Sheppo /* 49881ae08745Sheppo * Validate session ID up front, since it applies to all messages 49891ae08745Sheppo * once set 49901ae08745Sheppo */ 49911ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 49923af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 49931ae08745Sheppo msg->tag.vio_sid); 49941ae08745Sheppo return (EBADMSG); 49951ae08745Sheppo } 49961ae08745Sheppo 49973af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 49981ae08745Sheppo 49991ae08745Sheppo /* 50001ae08745Sheppo * Process the received message based on connection state 50011ae08745Sheppo */ 50021ae08745Sheppo switch (vd->state) { 50031ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 50040a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 50051ae08745Sheppo return (status); 50061ae08745Sheppo 50071ae08745Sheppo /* Version negotiated, move to that state */ 50081ae08745Sheppo vd->state = VD_STATE_VER; 50091ae08745Sheppo return (0); 50101ae08745Sheppo 50111ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 50121ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 50131ae08745Sheppo return (status); 50141ae08745Sheppo 50151ae08745Sheppo /* Attributes exchanged, move to that state */ 50161ae08745Sheppo vd->state = VD_STATE_ATTR; 50171ae08745Sheppo return (0); 50181ae08745Sheppo 50191ae08745Sheppo case VD_STATE_ATTR: 50201ae08745Sheppo switch (vd->xfer_mode) { 50211ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 50221ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 50231ae08745Sheppo return (status); 50241ae08745Sheppo 50251ae08745Sheppo /* Ready to receive in-band descriptors */ 50261ae08745Sheppo vd->state = VD_STATE_DATA; 50271ae08745Sheppo return (0); 50281ae08745Sheppo 5029f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect register-dring message */ 50301ae08745Sheppo if ((status = 50311ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 50321ae08745Sheppo return (status); 50331ae08745Sheppo 50341ae08745Sheppo /* One dring negotiated, move to that state */ 50351ae08745Sheppo vd->state = VD_STATE_DRING; 50361ae08745Sheppo return (0); 50371ae08745Sheppo 50381ae08745Sheppo default: 50391ae08745Sheppo ASSERT("Unsupported transfer mode"); 50403af08d82Slm66018 PR0("Unsupported transfer mode"); 50411ae08745Sheppo return (ENOTSUP); 50421ae08745Sheppo } 50431ae08745Sheppo 50441ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 50451ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 50461ae08745Sheppo /* Ready to receive data */ 50471ae08745Sheppo vd->state = VD_STATE_DATA; 50481ae08745Sheppo return (0); 50491ae08745Sheppo } else if (status != ENOMSG) { 50501ae08745Sheppo return (status); 50511ae08745Sheppo } 50521ae08745Sheppo 50531ae08745Sheppo 50541ae08745Sheppo /* 50551ae08745Sheppo * If another register-dring message is received, stay in 50561ae08745Sheppo * dring state in case the client sends RDX; although the 50571ae08745Sheppo * protocol allows multiple drings, this server does not 50581ae08745Sheppo * support using more than one 50591ae08745Sheppo */ 50601ae08745Sheppo if ((status = 50611ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 50621ae08745Sheppo return (status); 50631ae08745Sheppo 50641ae08745Sheppo /* 50651ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 50661ae08745Sheppo * connection anyway: Although the protocol allows 50671ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 50681ae08745Sheppo * without its only dring 50691ae08745Sheppo */ 50701ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 50711ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 50721ae08745Sheppo 50731ae08745Sheppo case VD_STATE_DATA: 50741ae08745Sheppo switch (vd->xfer_mode) { 50751ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 50763af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 50771ae08745Sheppo 5078f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */ 50791ae08745Sheppo /* 50801ae08745Sheppo * Typically expect dring-data messages, so handle 50811ae08745Sheppo * them first 50821ae08745Sheppo */ 50831ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 50843af08d82Slm66018 msglen)) != ENOMSG) 50851ae08745Sheppo return (status); 50861ae08745Sheppo 50871ae08745Sheppo /* 50881ae08745Sheppo * Acknowledge an unregister-dring message, but reset 50891ae08745Sheppo * the connection anyway: Although the protocol 50901ae08745Sheppo * allows unregistering drings, this server cannot 50911ae08745Sheppo * serve a vdisk without its only dring 50921ae08745Sheppo */ 50931ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 50941ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 50951ae08745Sheppo 50961ae08745Sheppo default: 50971ae08745Sheppo ASSERT("Unsupported transfer mode"); 50983af08d82Slm66018 PR0("Unsupported transfer mode"); 50991ae08745Sheppo return (ENOTSUP); 51001ae08745Sheppo } 51011ae08745Sheppo 51021ae08745Sheppo default: 51031ae08745Sheppo ASSERT("Invalid client connection state"); 51043af08d82Slm66018 PR0("Invalid client connection state"); 51051ae08745Sheppo return (ENOTSUP); 51061ae08745Sheppo } 51071ae08745Sheppo } 51081ae08745Sheppo 5109d10e4ef2Snarayan static int 51103af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 51111ae08745Sheppo { 51121ae08745Sheppo int status; 51131ae08745Sheppo boolean_t reset_ldc = B_FALSE; 5114205eeb1aSlm66018 vd_task_t task; 51151ae08745Sheppo 51161ae08745Sheppo /* 51171ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 51181ae08745Sheppo * message processing can proceed based on tag-specified message type 51191ae08745Sheppo */ 51201ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 51213af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 51221ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 51233af08d82Slm66018 PR0("initiating full reset"); 5124d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 5125d10e4ef2Snarayan return (EBADMSG); 51261ae08745Sheppo } 51271ae08745Sheppo 51281ae08745Sheppo /* 51291ae08745Sheppo * Process the message 51301ae08745Sheppo */ 51313af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 51321ae08745Sheppo case 0: 51331ae08745Sheppo /* "ack" valid, successfully-processed messages */ 51341ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 51351ae08745Sheppo break; 51361ae08745Sheppo 5137d10e4ef2Snarayan case EINPROGRESS: 5138d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 5139d10e4ef2Snarayan return (EINPROGRESS); 51401ae08745Sheppo case ENOMSG: 51413af08d82Slm66018 PR0("Received unexpected message"); 51421ae08745Sheppo _NOTE(FALLTHROUGH); 51431ae08745Sheppo case EBADMSG: 51441ae08745Sheppo case ENOTSUP: 5145205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 51461ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 51471ae08745Sheppo break; 51481ae08745Sheppo 51491ae08745Sheppo default: 5150205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 51511ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 51521ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 51531ae08745Sheppo reset_ldc = B_TRUE; 51541ae08745Sheppo break; 51551ae08745Sheppo } 51561ae08745Sheppo 51573af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 51583af08d82Slm66018 vd_decode_state(vd->state)); 51593af08d82Slm66018 5160205eeb1aSlm66018 /* populate the task so we can dispatch it on the taskq */ 5161205eeb1aSlm66018 task.vd = vd; 5162205eeb1aSlm66018 task.msg = msg; 5163205eeb1aSlm66018 task.msglen = msglen; 5164205eeb1aSlm66018 5165205eeb1aSlm66018 /* 5166205eeb1aSlm66018 * Queue a task to send the notification that the operation completed. 5167205eeb1aSlm66018 * We need to ensure that requests are responded to in the correct 5168205eeb1aSlm66018 * order and since the taskq is processed serially this ordering 5169205eeb1aSlm66018 * is maintained. 5170205eeb1aSlm66018 */ 5171205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify, 5172205eeb1aSlm66018 &task, DDI_SLEEP); 5173205eeb1aSlm66018 5174205eeb1aSlm66018 /* 5175205eeb1aSlm66018 * To ensure handshake negotiations do not happen out of order, such 5176205eeb1aSlm66018 * requests that come through this path should not be done in parallel 5177205eeb1aSlm66018 * so we need to wait here until the response is sent to the client. 5178205eeb1aSlm66018 */ 5179205eeb1aSlm66018 ddi_taskq_wait(vd->completionq); 51801ae08745Sheppo 5181d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 51823af08d82Slm66018 if ((status != 0) || reset_ldc) { 51833af08d82Slm66018 PR0("initiating %s reset", 51843af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 5185d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 51863af08d82Slm66018 } 5187d10e4ef2Snarayan 5188d10e4ef2Snarayan return (status); 5189d10e4ef2Snarayan } 5190d10e4ef2Snarayan 5191d10e4ef2Snarayan static boolean_t 5192d10e4ef2Snarayan vd_enabled(vd_t *vd) 5193d10e4ef2Snarayan { 5194d10e4ef2Snarayan boolean_t enabled; 5195d10e4ef2Snarayan 5196d10e4ef2Snarayan mutex_enter(&vd->lock); 5197d10e4ef2Snarayan enabled = vd->enabled; 5198d10e4ef2Snarayan mutex_exit(&vd->lock); 5199d10e4ef2Snarayan return (enabled); 52001ae08745Sheppo } 52011ae08745Sheppo 52021ae08745Sheppo static void 52030a55fbb7Slm66018 vd_recv_msg(void *arg) 52041ae08745Sheppo { 52051ae08745Sheppo vd_t *vd = (vd_t *)arg; 52063af08d82Slm66018 int rv = 0, status = 0; 52071ae08745Sheppo 52081ae08745Sheppo ASSERT(vd != NULL); 52093af08d82Slm66018 5210d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 52113af08d82Slm66018 52123af08d82Slm66018 5213d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 5214d10e4ef2Snarayan size_t msglen, msgsize; 52153af08d82Slm66018 ldc_status_t lstatus; 5216d10e4ef2Snarayan 52170a55fbb7Slm66018 /* 5218d10e4ef2Snarayan * Receive and process a message 52190a55fbb7Slm66018 */ 5220d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 52213af08d82Slm66018 52223af08d82Slm66018 /* 52233af08d82Slm66018 * check if channel is UP - else break out of loop 52243af08d82Slm66018 */ 52253af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 52263af08d82Slm66018 if (lstatus != LDC_UP) { 52273af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 52283af08d82Slm66018 lstatus); 52293af08d82Slm66018 break; 52303af08d82Slm66018 } 52313af08d82Slm66018 52323af08d82Slm66018 ASSERT(vd->max_msglen != 0); 52333af08d82Slm66018 5234d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 52353af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 52363af08d82Slm66018 52373af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 52383af08d82Slm66018 switch (status) { 52393af08d82Slm66018 case 0: 5240342440ecSPrasad Singamsetty rv = vd_process_msg(vd, (void *)vd->vio_msgp, msglen); 52413af08d82Slm66018 /* check if max_msglen changed */ 52423af08d82Slm66018 if (msgsize != vd->max_msglen) { 52433af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 52443af08d82Slm66018 msgsize, vd->max_msglen); 52453af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 52463af08d82Slm66018 vd->vio_msgp = 52473af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 52483af08d82Slm66018 } 52493af08d82Slm66018 if (rv == EINPROGRESS) 52503af08d82Slm66018 continue; 52513af08d82Slm66018 break; 52523af08d82Slm66018 52533af08d82Slm66018 case ENOMSG: 52543af08d82Slm66018 break; 52553af08d82Slm66018 52563af08d82Slm66018 case ECONNRESET: 52573af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 52583af08d82Slm66018 vd_need_reset(vd, B_FALSE); 52593af08d82Slm66018 status = 0; 52603af08d82Slm66018 break; 52613af08d82Slm66018 52623af08d82Slm66018 default: 5263d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 52643af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 5265d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 52663af08d82Slm66018 break; 52670a55fbb7Slm66018 } 52681ae08745Sheppo } 52693af08d82Slm66018 5270d10e4ef2Snarayan PR2("Task finished"); 52710a55fbb7Slm66018 } 52720a55fbb7Slm66018 52730a55fbb7Slm66018 static uint_t 52741ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 52751ae08745Sheppo { 52761ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 52773af08d82Slm66018 int status; 52781ae08745Sheppo 52791ae08745Sheppo ASSERT(vd != NULL); 5280d10e4ef2Snarayan 5281d10e4ef2Snarayan if (!vd_enabled(vd)) 5282d10e4ef2Snarayan return (LDC_SUCCESS); 5283d10e4ef2Snarayan 52843af08d82Slm66018 if (event & LDC_EVT_DOWN) { 528534683adeSsg70180 PR0("LDC_EVT_DOWN: LDC channel went down"); 52863af08d82Slm66018 52873af08d82Slm66018 vd_need_reset(vd, B_TRUE); 52883af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 52893af08d82Slm66018 DDI_SLEEP); 52903af08d82Slm66018 if (status == DDI_FAILURE) { 52913af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 52923af08d82Slm66018 vd_need_reset(vd, B_TRUE); 52933af08d82Slm66018 } 52943af08d82Slm66018 } 52953af08d82Slm66018 5296d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 52973af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 52983af08d82Slm66018 52993af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 53003af08d82Slm66018 PR0("scheduling full reset"); 53013af08d82Slm66018 vd_need_reset(vd, B_FALSE); 53023af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 53033af08d82Slm66018 vd, DDI_SLEEP); 53043af08d82Slm66018 if (status == DDI_FAILURE) { 53053af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 53063af08d82Slm66018 vd_need_reset(vd, B_TRUE); 53073af08d82Slm66018 } 53083af08d82Slm66018 53093af08d82Slm66018 } else { 53103af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 53113af08d82Slm66018 PR0("doing ldc up...\n"); 53123af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 53133af08d82Slm66018 } 53143af08d82Slm66018 5315d10e4ef2Snarayan return (LDC_SUCCESS); 5316d10e4ef2Snarayan } 5317d10e4ef2Snarayan 5318d10e4ef2Snarayan if (event & LDC_EVT_UP) { 53193af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 53203af08d82Slm66018 PR0("initiating soft reset"); 5321d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 53223af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 53233af08d82Slm66018 vd, DDI_SLEEP); 53243af08d82Slm66018 if (status == DDI_FAILURE) { 53253af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 53263af08d82Slm66018 vd_need_reset(vd, B_TRUE); 53273af08d82Slm66018 return (LDC_SUCCESS); 53283af08d82Slm66018 } 5329d10e4ef2Snarayan } 5330d10e4ef2Snarayan 5331d10e4ef2Snarayan if (event & LDC_EVT_READ) { 5332d10e4ef2Snarayan int status; 5333d10e4ef2Snarayan 5334d10e4ef2Snarayan PR1("New data available"); 5335d10e4ef2Snarayan /* Queue a task to receive the new data */ 5336d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 5337d10e4ef2Snarayan DDI_SLEEP); 53383af08d82Slm66018 53393af08d82Slm66018 if (status == DDI_FAILURE) { 53403af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 53413af08d82Slm66018 vd_need_reset(vd, B_TRUE); 53423af08d82Slm66018 } 5343d10e4ef2Snarayan } 5344d10e4ef2Snarayan 5345d10e4ef2Snarayan return (LDC_SUCCESS); 53461ae08745Sheppo } 53471ae08745Sheppo 53481ae08745Sheppo static uint_t 53491ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 53501ae08745Sheppo { 53511ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 53521ae08745Sheppo (*((uint_t *)arg))++; 53531ae08745Sheppo return (MH_WALK_TERMINATE); 53541ae08745Sheppo } 53551ae08745Sheppo 53561ae08745Sheppo 53571ae08745Sheppo static int 53581ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 53591ae08745Sheppo { 53601ae08745Sheppo uint_t vd_present = 0; 53611ae08745Sheppo minor_t instance; 53621ae08745Sheppo vds_t *vds; 53631ae08745Sheppo 53641ae08745Sheppo 53651ae08745Sheppo switch (cmd) { 53661ae08745Sheppo case DDI_DETACH: 53671ae08745Sheppo /* the real work happens below */ 53681ae08745Sheppo break; 53691ae08745Sheppo case DDI_SUSPEND: 5370d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 53711ae08745Sheppo return (DDI_SUCCESS); 53721ae08745Sheppo default: 53733af08d82Slm66018 PR0("Unrecognized \"cmd\""); 53741ae08745Sheppo return (DDI_FAILURE); 53751ae08745Sheppo } 53761ae08745Sheppo 53771ae08745Sheppo ASSERT(cmd == DDI_DETACH); 53781ae08745Sheppo instance = ddi_get_instance(dip); 53791ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 53803af08d82Slm66018 PR0("Could not get state for instance %u", instance); 53811ae08745Sheppo ddi_soft_state_free(vds_state, instance); 53821ae08745Sheppo return (DDI_FAILURE); 53831ae08745Sheppo } 53841ae08745Sheppo 53851ae08745Sheppo /* Do no detach when serving any vdisks */ 53861ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 53871ae08745Sheppo if (vd_present) { 53881ae08745Sheppo PR0("Not detaching because serving vdisks"); 53891ae08745Sheppo return (DDI_FAILURE); 53901ae08745Sheppo } 53911ae08745Sheppo 53921ae08745Sheppo PR0("Detaching"); 5393445b4c2eSsb155480 if (vds->initialized & VDS_MDEG) { 53941ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 5395445b4c2eSsb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 5396445b4c2eSsb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 5397445b4c2eSsb155480 vds->ispecp = NULL; 5398445b4c2eSsb155480 vds->mdeg = NULL; 5399445b4c2eSsb155480 } 5400445b4c2eSsb155480 54018fce2fd6Sachartre vds_driver_types_free(vds); 54028fce2fd6Sachartre 54031ae08745Sheppo if (vds->initialized & VDS_LDI) 54041ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 54051ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 54061ae08745Sheppo ddi_soft_state_free(vds_state, instance); 54071ae08745Sheppo return (DDI_SUCCESS); 54081ae08745Sheppo } 54091ae08745Sheppo 541017cadca8Slm66018 /* 541117cadca8Slm66018 * Description: 54121aff8f07SAlexandre Chartre * This function checks to see if the disk image being used as a 54131aff8f07SAlexandre Chartre * virtual disk is an ISO image. An ISO image is a special case 54141aff8f07SAlexandre Chartre * which can be booted/installed from like a CD/DVD. 541517cadca8Slm66018 * 541617cadca8Slm66018 * Parameters: 541717cadca8Slm66018 * vd - disk on which the operation is performed. 541817cadca8Slm66018 * 541917cadca8Slm66018 * Return Code: 54201aff8f07SAlexandre Chartre * B_TRUE - The disk image is an ISO 9660 compliant image 54211aff8f07SAlexandre Chartre * B_FALSE - just a regular disk image 542217cadca8Slm66018 */ 542317cadca8Slm66018 static boolean_t 54241aff8f07SAlexandre Chartre vd_dskimg_is_iso_image(vd_t *vd) 542517cadca8Slm66018 { 542617cadca8Slm66018 char iso_buf[ISO_SECTOR_SIZE]; 542717cadca8Slm66018 int i, rv; 542817cadca8Slm66018 uint_t sec; 542917cadca8Slm66018 54301aff8f07SAlexandre Chartre ASSERT(VD_DSKIMG(vd)); 543117cadca8Slm66018 543217cadca8Slm66018 /* 543317cadca8Slm66018 * If we have already discovered and saved this info we can 54341aff8f07SAlexandre Chartre * short-circuit the check and avoid reading the disk image. 543517cadca8Slm66018 */ 543617cadca8Slm66018 if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD) 543717cadca8Slm66018 return (B_TRUE); 543817cadca8Slm66018 543917cadca8Slm66018 /* 544017cadca8Slm66018 * We wish to read the sector that should contain the 2nd ISO volume 544117cadca8Slm66018 * descriptor. The second field in this descriptor is called the 544217cadca8Slm66018 * Standard Identifier and is set to CD001 for a CD-ROM compliant 544317cadca8Slm66018 * to the ISO 9660 standard. 544417cadca8Slm66018 */ 544517cadca8Slm66018 sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size; 54461aff8f07SAlexandre Chartre rv = vd_dskimg_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf, 544717cadca8Slm66018 sec, ISO_SECTOR_SIZE); 544817cadca8Slm66018 544917cadca8Slm66018 if (rv < 0) 545017cadca8Slm66018 return (B_FALSE); 545117cadca8Slm66018 545217cadca8Slm66018 for (i = 0; i < ISO_ID_STRLEN; i++) { 545317cadca8Slm66018 if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i]) 545417cadca8Slm66018 return (B_FALSE); 545517cadca8Slm66018 } 545617cadca8Slm66018 545717cadca8Slm66018 return (B_TRUE); 545817cadca8Slm66018 } 545917cadca8Slm66018 546017cadca8Slm66018 /* 546117cadca8Slm66018 * Description: 546217cadca8Slm66018 * This function checks to see if the virtual device is an ATAPI 546317cadca8Slm66018 * device. ATAPI devices use Group 1 Read/Write commands, so 546417cadca8Slm66018 * any USCSI calls vds makes need to take this into account. 546517cadca8Slm66018 * 546617cadca8Slm66018 * Parameters: 546717cadca8Slm66018 * vd - disk on which the operation is performed. 546817cadca8Slm66018 * 546917cadca8Slm66018 * Return Code: 547017cadca8Slm66018 * B_TRUE - The virtual disk is backed by an ATAPI device 547117cadca8Slm66018 * B_FALSE - not an ATAPI device (presumably SCSI) 547217cadca8Slm66018 */ 547317cadca8Slm66018 static boolean_t 547417cadca8Slm66018 vd_is_atapi_device(vd_t *vd) 547517cadca8Slm66018 { 547617cadca8Slm66018 boolean_t is_atapi = B_FALSE; 547717cadca8Slm66018 char *variantp; 547817cadca8Slm66018 int rv; 547917cadca8Slm66018 548017cadca8Slm66018 ASSERT(vd->ldi_handle[0] != NULL); 548117cadca8Slm66018 ASSERT(!vd->file); 548217cadca8Slm66018 548317cadca8Slm66018 rv = ldi_prop_lookup_string(vd->ldi_handle[0], 548417cadca8Slm66018 (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp); 548517cadca8Slm66018 if (rv == DDI_PROP_SUCCESS) { 548617cadca8Slm66018 PR0("'variant' property exists for %s", vd->device_path); 548717cadca8Slm66018 if (strcmp(variantp, "atapi") == 0) 548817cadca8Slm66018 is_atapi = B_TRUE; 548917cadca8Slm66018 ddi_prop_free(variantp); 549017cadca8Slm66018 } 549117cadca8Slm66018 549217cadca8Slm66018 rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi"); 549317cadca8Slm66018 if (rv) { 549417cadca8Slm66018 PR0("'atapi' property exists for %s", vd->device_path); 549517cadca8Slm66018 is_atapi = B_TRUE; 549617cadca8Slm66018 } 549717cadca8Slm66018 549817cadca8Slm66018 return (is_atapi); 549917cadca8Slm66018 } 550017cadca8Slm66018 55011ae08745Sheppo static int 55022f5224aeSachartre vd_setup_full_disk(vd_t *vd) 55032f5224aeSachartre { 55042f5224aeSachartre int status; 55052f5224aeSachartre major_t major = getmajor(vd->dev[0]); 55062f5224aeSachartre minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 55072f5224aeSachartre 5508047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 5509047ba61eSachartre 55102f5224aeSachartre vd->vdisk_block_size = DEV_BSIZE; 55112f5224aeSachartre 5512de3a5331SRamesh Chitrothu /* set the disk size, block size and the media type of the disk */ 5513de3a5331SRamesh Chitrothu status = vd_backend_check_size(vd); 55142f5224aeSachartre 55152f5224aeSachartre if (status != 0) { 55162f5224aeSachartre if (!vd->scsi) { 55172f5224aeSachartre /* unexpected failure */ 5518690555a1Sachartre PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 55194bac2208Snarayan status); 55200a55fbb7Slm66018 return (status); 55210a55fbb7Slm66018 } 55222f5224aeSachartre 55232f5224aeSachartre /* 55242f5224aeSachartre * The function can fail for SCSI disks which are present but 55252f5224aeSachartre * reserved by another system. In that case, we don't know the 55262f5224aeSachartre * size of the disk and the block size. 55272f5224aeSachartre */ 55282f5224aeSachartre vd->vdisk_size = VD_SIZE_UNKNOWN; 55292f5224aeSachartre vd->block_size = 0; 55302f5224aeSachartre vd->vdisk_media = VD_MEDIA_FIXED; 55312f5224aeSachartre } 55320a55fbb7Slm66018 55330a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 55340a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 55350a55fbb7Slm66018 vd->dev[0] = 0; 55360a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 55370a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 55380a55fbb7Slm66018 55390a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 55400a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 55410a55fbb7Slm66018 /* 55420a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 55430a55fbb7Slm66018 * device known 55440a55fbb7Slm66018 */ 55450a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 55460a55fbb7Slm66018 continue; 55470a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 55480a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 55490a55fbb7Slm66018 55500a55fbb7Slm66018 /* 55510a55fbb7Slm66018 * Construct the device number for the current slice 55520a55fbb7Slm66018 */ 55530a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 55540a55fbb7Slm66018 55550a55fbb7Slm66018 /* 555634683adeSsg70180 * Open all slices of the disk to serve them to the client. 555734683adeSsg70180 * Slices are opened exclusively to prevent other threads or 555834683adeSsg70180 * processes in the service domain from performing I/O to 555934683adeSsg70180 * slices being accessed by a client. Failure to open a slice 556034683adeSsg70180 * results in vds not serving this disk, as the client could 556134683adeSsg70180 * attempt (and should be able) to access any slice immediately. 556234683adeSsg70180 * Any slices successfully opened before a failure will get 556334683adeSsg70180 * closed by vds_destroy_vd() as a result of the error returned 556434683adeSsg70180 * by this function. 556534683adeSsg70180 * 556634683adeSsg70180 * We need to do the open with FNDELAY so that opening an empty 556734683adeSsg70180 * slice does not fail. 55680a55fbb7Slm66018 */ 55690a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 55700a55fbb7Slm66018 major, minor, slice); 5571047ba61eSachartre 5572047ba61eSachartre /* 5573047ba61eSachartre * Try to open the device. This can fail for example if we are 5574047ba61eSachartre * opening an empty slice. So in case of a failure, we try the 5575047ba61eSachartre * open again but this time with the FNDELAY flag. 5576047ba61eSachartre */ 5577047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5578047ba61eSachartre vd->open_flags, kcred, &vd->ldi_handle[slice], 5579047ba61eSachartre vd->vds->ldi_ident); 5580047ba61eSachartre 5581047ba61eSachartre if (status != 0) { 5582047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5583047ba61eSachartre vd->open_flags | FNDELAY, kcred, 5584047ba61eSachartre &vd->ldi_handle[slice], vd->vds->ldi_ident); 5585047ba61eSachartre } 5586047ba61eSachartre 5587047ba61eSachartre if (status != 0) { 5588690555a1Sachartre PRN("ldi_open_by_dev() returned errno %d " 55890a55fbb7Slm66018 "for slice %u", status, slice); 55900a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 5591690555a1Sachartre vd->ldi_handle[slice] = NULL; 55920a55fbb7Slm66018 return (status); 55930a55fbb7Slm66018 } 55940a55fbb7Slm66018 } 55950a55fbb7Slm66018 55960a55fbb7Slm66018 return (0); 55970a55fbb7Slm66018 } 55980a55fbb7Slm66018 5599edcc0754Sachartre /* 5600edcc0754Sachartre * When a slice or a volume is exported as a single-slice disk, we want 5601edcc0754Sachartre * the disk backend (i.e. the slice or volume) to be entirely mapped as 5602edcc0754Sachartre * a slice without the addition of any metadata. 5603edcc0754Sachartre * 5604edcc0754Sachartre * So when exporting the disk as a VTOC disk, we fake a disk with the following 5605edcc0754Sachartre * layout: 5606bae9e67eSachartre * flabel +--- flabel_limit 5607bae9e67eSachartre * <-> V 5608bae9e67eSachartre * 0 1 C D E 5609bae9e67eSachartre * +-+---+--------------------------+--+ 5610bae9e67eSachartre * virtual disk: |L|XXX| slice 0 |AA| 5611bae9e67eSachartre * +-+---+--------------------------+--+ 5612edcc0754Sachartre * ^ : : 5613edcc0754Sachartre * | : : 5614edcc0754Sachartre * VTOC LABEL--+ : : 5615edcc0754Sachartre * +--------------------------+ 5616bae9e67eSachartre * disk backend: | slice/volume/file | 5617edcc0754Sachartre * +--------------------------+ 5618edcc0754Sachartre * 0 N 5619edcc0754Sachartre * 5620bae9e67eSachartre * N is the number of blocks in the slice/volume/file. 5621edcc0754Sachartre * 5622bae9e67eSachartre * We simulate a disk with N+M blocks, where M is the number of blocks 5623bae9e67eSachartre * simluated at the beginning and at the end of the disk (blocks 0-C 5624bae9e67eSachartre * and D-E). 5625edcc0754Sachartre * 5626bae9e67eSachartre * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C 5627bae9e67eSachartre * to D defines slice 0 and are mapped to the backend. Finally we emulate 2 5628bae9e67eSachartre * alternate cylinders at the end of the disk (blocks D-E). In summary we have: 5629edcc0754Sachartre * 5630bae9e67eSachartre * - block 0 (L) returns a fake VTOC label 5631bae9e67eSachartre * - blocks 1 to C-1 (X) are unused and return 0 5632bae9e67eSachartre * - blocks C to D-1 are mapped to the exported slice or volume 5633bae9e67eSachartre * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders) 5634bae9e67eSachartre * 5635bae9e67eSachartre * Note: because we define a fake disk geometry, it is possible that the length 5636bae9e67eSachartre * of the backend is not a multiple of the size of cylinder, in that case the 5637bae9e67eSachartre * very end of the backend will not map to any block of the virtual disk. 5638edcc0754Sachartre */ 56390a55fbb7Slm66018 static int 564078fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd) 564178fcd0a1Sachartre { 564278fcd0a1Sachartre char *device_path = vd->device_path; 5643bae9e67eSachartre char unit; 5644bae9e67eSachartre size_t size, csize; 564578fcd0a1Sachartre 564678fcd0a1Sachartre /* Initialize dk_geom structure for single-slice device */ 564778fcd0a1Sachartre if (vd->dk_geom.dkg_nsect == 0) { 564878fcd0a1Sachartre PRN("%s geometry claims 0 sectors per track", device_path); 564978fcd0a1Sachartre return (EIO); 565078fcd0a1Sachartre } 565178fcd0a1Sachartre if (vd->dk_geom.dkg_nhead == 0) { 565278fcd0a1Sachartre PRN("%s geometry claims 0 heads", device_path); 565378fcd0a1Sachartre return (EIO); 565478fcd0a1Sachartre } 5655bae9e67eSachartre 5656bae9e67eSachartre /* size of a cylinder in block */ 5657bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 5658bae9e67eSachartre 5659bae9e67eSachartre /* 5660bae9e67eSachartre * Add extra cylinders: we emulate the first cylinder (which contains 5661bae9e67eSachartre * the disk label). 5662bae9e67eSachartre */ 5663bae9e67eSachartre vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1; 5664bae9e67eSachartre 5665bae9e67eSachartre /* we emulate 2 alternate cylinders */ 5666bae9e67eSachartre vd->dk_geom.dkg_acyl = 2; 566778fcd0a1Sachartre vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 566878fcd0a1Sachartre 566978fcd0a1Sachartre 567078fcd0a1Sachartre /* Initialize vtoc structure for single-slice device */ 567178fcd0a1Sachartre bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 567278fcd0a1Sachartre vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 567378fcd0a1Sachartre vd->vtoc.v_part[0].p_flag = 0; 5674bae9e67eSachartre /* 5675bae9e67eSachartre * Partition 0 starts on cylinder 1 and its size has to be 5676bae9e67eSachartre * a multiple of a number of cylinder. 5677bae9e67eSachartre */ 5678bae9e67eSachartre vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */ 5679bae9e67eSachartre vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize; 568078fcd0a1Sachartre 5681bae9e67eSachartre if (vd_slice_single_slice) { 5682bae9e67eSachartre vd->vtoc.v_nparts = 1; 5683bae9e67eSachartre bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 5684bae9e67eSachartre MIN(sizeof (VD_ASCIILABEL), 5685bae9e67eSachartre sizeof (vd->vtoc.v_asciilabel))); 5686bae9e67eSachartre bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 5687bae9e67eSachartre MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 5688bae9e67eSachartre } else { 5689bae9e67eSachartre /* adjust the number of slices */ 5690bae9e67eSachartre vd->nslices = V_NUMPAR; 5691bae9e67eSachartre vd->vtoc.v_nparts = V_NUMPAR; 5692bae9e67eSachartre 5693bae9e67eSachartre /* define slice 2 representing the entire disk */ 5694bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 5695bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0; 5696bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0; 5697bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size = 5698bae9e67eSachartre vd->dk_geom.dkg_ncyl * csize; 5699bae9e67eSachartre 5700bae9e67eSachartre vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size, 5701bae9e67eSachartre &size, &unit); 5702bae9e67eSachartre 5703bae9e67eSachartre /* 5704bae9e67eSachartre * Set some attributes of the geometry to what format(1m) uses 5705bae9e67eSachartre * so that writing a default label using format(1m) does not 5706bae9e67eSachartre * produce any error. 5707bae9e67eSachartre */ 5708bae9e67eSachartre vd->dk_geom.dkg_bcyl = 0; 5709bae9e67eSachartre vd->dk_geom.dkg_intrlv = 1; 5710bae9e67eSachartre vd->dk_geom.dkg_write_reinstruct = 0; 5711bae9e67eSachartre vd->dk_geom.dkg_read_reinstruct = 0; 5712bae9e67eSachartre 5713bae9e67eSachartre /* 5714bae9e67eSachartre * We must have a correct label name otherwise format(1m) will 5715bae9e67eSachartre * not recognized the disk as labeled. 5716bae9e67eSachartre */ 5717bae9e67eSachartre (void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII, 5718bae9e67eSachartre "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d", 5719bae9e67eSachartre size, unit, 5720bae9e67eSachartre vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl, 5721bae9e67eSachartre vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect); 5722bae9e67eSachartre bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume)); 5723bae9e67eSachartre 5724bae9e67eSachartre /* create a fake label from the vtoc and geometry */ 5725342440ecSPrasad Singamsetty vd->flabel_limit = (uint_t)csize; 5726bae9e67eSachartre vd->flabel_size = VD_LABEL_VTOC_SIZE; 5727bae9e67eSachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5728bae9e67eSachartre vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom, 5729bae9e67eSachartre VD_LABEL_VTOC(vd)); 5730bae9e67eSachartre } 5731bae9e67eSachartre 5732bae9e67eSachartre /* adjust the vdisk_size, we emulate 3 cylinders */ 5733bae9e67eSachartre vd->vdisk_size += csize * 3; 5734edcc0754Sachartre 573578fcd0a1Sachartre return (0); 573678fcd0a1Sachartre } 573778fcd0a1Sachartre 5738edcc0754Sachartre /* 5739edcc0754Sachartre * When a slice, volume or file is exported as a single-slice disk, we want 5740edcc0754Sachartre * the disk backend (i.e. the slice, volume or file) to be entirely mapped 5741edcc0754Sachartre * as a slice without the addition of any metadata. 5742edcc0754Sachartre * 5743edcc0754Sachartre * So when exporting the disk as an EFI disk, we fake a disk with the following 5744edcc0754Sachartre * layout: 5745edcc0754Sachartre * 5746bae9e67eSachartre * flabel +--- flabel_limit 5747bae9e67eSachartre * <------> v 5748bae9e67eSachartre * 0 1 2 L 34 34+N P 5749bae9e67eSachartre * +-+-+--+-------+--------------------------+-------+ 5750bae9e67eSachartre * virtual disk: |X|T|EE|XXXXXXX| slice 0 |RRRRRRR| 5751bae9e67eSachartre * +-+-+--+-------+--------------------------+-------+ 5752edcc0754Sachartre * ^ ^ : : 5753edcc0754Sachartre * | | : : 5754edcc0754Sachartre * GPT-+ +-GPE : : 5755edcc0754Sachartre * +--------------------------+ 5756edcc0754Sachartre * disk backend: | slice/volume/file | 5757edcc0754Sachartre * +--------------------------+ 5758edcc0754Sachartre * 0 N 5759edcc0754Sachartre * 5760edcc0754Sachartre * N is the number of blocks in the slice/volume/file. 5761edcc0754Sachartre * 5762bae9e67eSachartre * We simulate a disk with N+M blocks, where M is the number of blocks 5763bae9e67eSachartre * simluated at the beginning and at the end of the disk (blocks 0-34 5764bae9e67eSachartre * and 34+N-P). 5765edcc0754Sachartre * 5766bae9e67eSachartre * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34 5767bae9e67eSachartre * to 34+N defines slice 0 and are mapped to the exported backend, and we 5768bae9e67eSachartre * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI 5769bae9e67eSachartre * reserved partition. 5770bae9e67eSachartre * 5771bae9e67eSachartre * - block 0 (X) is unused and return 0 5772edcc0754Sachartre * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI) 5773bae9e67eSachartre * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI) 5774bae9e67eSachartre * - blocks L to 33 (X) are unused and return 0 5775bae9e67eSachartre * - blocks 34 to 34+N are mapped to the exported slice, volume or file 5776bae9e67eSachartre * - blocks 34+N+1 to P define a fake reserved partition and backup label, it 5777bae9e67eSachartre * returns 0 5778edcc0754Sachartre * 5779bae9e67eSachartre * Note: if the backend size is not a multiple of the vdisk block size 5780bae9e67eSachartre * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to 5781bae9e67eSachartre * any block of the virtual disk. 5782edcc0754Sachartre */ 578378fcd0a1Sachartre static int 57844bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 57854bac2208Snarayan { 57864bac2208Snarayan efi_gpt_t *gpt; 57874bac2208Snarayan efi_gpe_t *gpe; 5788edcc0754Sachartre struct uuid uuid = EFI_USR; 5789bae9e67eSachartre struct uuid efi_reserved = EFI_RESERVED; 57904bac2208Snarayan uint32_t crc; 5791bae9e67eSachartre uint64_t s0_start, s0_end; 57924bac2208Snarayan 5793bae9e67eSachartre vd->flabel_limit = 34; 5794bae9e67eSachartre vd->flabel_size = VD_LABEL_EFI_SIZE; 5795bae9e67eSachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5796bae9e67eSachartre gpt = VD_LABEL_EFI_GPT(vd); 5797bae9e67eSachartre gpe = VD_LABEL_EFI_GPE(vd); 5798edcc0754Sachartre 5799edcc0754Sachartre /* adjust the vdisk_size, we emulate the first 34 blocks */ 5800edcc0754Sachartre vd->vdisk_size += 34; 5801bae9e67eSachartre s0_start = 34; 5802bae9e67eSachartre s0_end = vd->vdisk_size - 1; 58034bac2208Snarayan 58044bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 58054bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 58064bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 5807edcc0754Sachartre gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL); 5808edcc0754Sachartre gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL); 58094bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 58104bac2208Snarayan 5811bae9e67eSachartre UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid); 5812bae9e67eSachartre gpe[0].efi_gpe_StartingLBA = LE_64(s0_start); 5813bae9e67eSachartre gpe[0].efi_gpe_EndingLBA = LE_64(s0_end); 58144bac2208Snarayan 5815bae9e67eSachartre if (vd_slice_single_slice) { 5816bae9e67eSachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 5817bae9e67eSachartre } else { 5818bae9e67eSachartre /* adjust the number of slices */ 5819bae9e67eSachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART); 5820bae9e67eSachartre vd->nslices = V_NUMPAR; 5821bae9e67eSachartre 5822bae9e67eSachartre /* define a fake reserved partition */ 5823bae9e67eSachartre UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID, 5824bae9e67eSachartre efi_reserved); 5825bae9e67eSachartre gpe[VD_MAXPART - 1].efi_gpe_StartingLBA = 5826bae9e67eSachartre LE_64(s0_end + 1); 5827bae9e67eSachartre gpe[VD_MAXPART - 1].efi_gpe_EndingLBA = 5828bae9e67eSachartre LE_64(s0_end + EFI_MIN_RESV_SIZE); 5829bae9e67eSachartre 5830bae9e67eSachartre /* adjust the vdisk_size to include the reserved slice */ 5831bae9e67eSachartre vd->vdisk_size += EFI_MIN_RESV_SIZE; 5832bae9e67eSachartre } 5833bae9e67eSachartre 5834bae9e67eSachartre gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 5835bae9e67eSachartre 5836bae9e67eSachartre /* adjust the vdisk size for the backup GPT and GPE */ 5837bae9e67eSachartre vd->vdisk_size += 33; 5838bae9e67eSachartre 5839bae9e67eSachartre CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table); 58404bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 58414bac2208Snarayan 58424bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 58434bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 58444bac2208Snarayan 58454bac2208Snarayan return (0); 58464bac2208Snarayan } 58474bac2208Snarayan 5848047ba61eSachartre /* 5849047ba61eSachartre * Setup for a virtual disk whose backend is a file (exported as a single slice 58501aff8f07SAlexandre Chartre * or as a full disk). In that case, the backend is accessed using the vnode 58511aff8f07SAlexandre Chartre * interface. 5852047ba61eSachartre */ 58534bac2208Snarayan static int 5854047ba61eSachartre vd_setup_backend_vnode(vd_t *vd) 58553c96341aSnarayan { 585678fcd0a1Sachartre int rval, status; 58573c96341aSnarayan vattr_t vattr; 58583c96341aSnarayan dev_t dev; 58593c96341aSnarayan char *file_path = vd->device_path; 58603c96341aSnarayan ldi_handle_t lhandle; 58613c96341aSnarayan struct dk_cinfo dk_cinfo; 58621aff8f07SAlexandre Chartre 58631aff8f07SAlexandre Chartre ASSERT(!vd->volume); 58643c96341aSnarayan 5865047ba61eSachartre if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX, 58663c96341aSnarayan 0, &vd->file_vnode, 0, 0)) != 0) { 5867690555a1Sachartre PRN("vn_open(%s) = errno %d", file_path, status); 58683c96341aSnarayan return (status); 58693c96341aSnarayan } 58703c96341aSnarayan 5871690555a1Sachartre /* 5872690555a1Sachartre * We set vd->file now so that vds_destroy_vd will take care of 5873690555a1Sachartre * closing the file and releasing the vnode in case of an error. 5874690555a1Sachartre */ 5875690555a1Sachartre vd->file = B_TRUE; 5876690555a1Sachartre 58773c96341aSnarayan vattr.va_mask = AT_SIZE; 5878da6c28aaSamw if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL)) 5879da6c28aaSamw != 0) { 5880690555a1Sachartre PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 58813c96341aSnarayan return (EIO); 58823c96341aSnarayan } 58833c96341aSnarayan 58841aff8f07SAlexandre Chartre vd->dskimg_size = vattr.va_size; 58853c96341aSnarayan 5886690555a1Sachartre if (vd->file_vnode->v_flag & VNOMAP) { 5887690555a1Sachartre PRN("File %s cannot be mapped", file_path); 58883c96341aSnarayan return (EIO); 58893c96341aSnarayan } 58903c96341aSnarayan 58913c96341aSnarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 58923c96341aSnarayan 5893047ba61eSachartre /* 58941aff8f07SAlexandre Chartre * Get max_xfer_sz from the device where the file is. 5895047ba61eSachartre */ 58963c96341aSnarayan dev = vd->file_vnode->v_vfsp->vfs_dev; 5897f745d6a3Sachartre PR0("underlying device of %s = (%d, %d)\n", file_path, 5898f745d6a3Sachartre getmajor(dev), getminor(dev)); 58993c96341aSnarayan 5900047ba61eSachartre status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle, 5901047ba61eSachartre vd->vds->ldi_ident); 5902047ba61eSachartre 5903047ba61eSachartre if (status != 0) { 5904f745d6a3Sachartre PR0("ldi_open() returned errno %d for underlying device", 5905f745d6a3Sachartre status); 59063c96341aSnarayan } else { 59073c96341aSnarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO, 5908047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 59093c96341aSnarayan &rval)) != 0) { 5910f745d6a3Sachartre PR0("ldi_ioctl(DKIOCINFO) returned errno %d for " 5911f745d6a3Sachartre "underlying device", status); 59123c96341aSnarayan } else { 59133c96341aSnarayan /* 59143c96341aSnarayan * Store the device's max transfer size for 59153c96341aSnarayan * return to the client 59163c96341aSnarayan */ 59173c96341aSnarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 59183c96341aSnarayan } 59193c96341aSnarayan 5920f745d6a3Sachartre PR0("close the underlying device"); 59213c96341aSnarayan (void) ldi_close(lhandle, FREAD, kcred); 59223c96341aSnarayan } 59233c96341aSnarayan 5924f745d6a3Sachartre PR0("using file %s on device (%d, %d), max_xfer = %u blks", 5925f745d6a3Sachartre file_path, getmajor(dev), getminor(dev), vd->max_xfer_sz); 59261aff8f07SAlexandre Chartre 59271aff8f07SAlexandre Chartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) 59281aff8f07SAlexandre Chartre status = vd_setup_slice_image(vd); 59291aff8f07SAlexandre Chartre else 59301aff8f07SAlexandre Chartre status = vd_setup_disk_image(vd); 59311aff8f07SAlexandre Chartre 59321aff8f07SAlexandre Chartre return (status); 5933f745d6a3Sachartre } 59343c96341aSnarayan 59351aff8f07SAlexandre Chartre static int 59361aff8f07SAlexandre Chartre vd_setup_slice_image(vd_t *vd) 59371aff8f07SAlexandre Chartre { 59381aff8f07SAlexandre Chartre struct dk_label label; 59391aff8f07SAlexandre Chartre int status; 59401aff8f07SAlexandre Chartre 59411aff8f07SAlexandre Chartre /* sector size = block size = DEV_BSIZE */ 59421aff8f07SAlexandre Chartre vd->block_size = DEV_BSIZE; 59431aff8f07SAlexandre Chartre vd->vdisk_block_size = DEV_BSIZE; 59441aff8f07SAlexandre Chartre vd->vdisk_size = vd->dskimg_size / DEV_BSIZE; 5945bae9e67eSachartre vd->vdisk_media = VD_MEDIA_FIXED; 5946bae9e67eSachartre vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)? 5947bae9e67eSachartre vd_file_slice_label : vd_slice_label; 59481aff8f07SAlexandre Chartre 5949bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI || 59501aff8f07SAlexandre Chartre vd->dskimg_size >= 2 * ONE_TERABYTE) { 5951edcc0754Sachartre status = vd_setup_partition_efi(vd); 5952bae9e67eSachartre } else { 5953bae9e67eSachartre /* 5954bae9e67eSachartre * We build a default label to get a geometry for 5955bae9e67eSachartre * the vdisk. Then the partition setup function will 5956bae9e67eSachartre * adjust the vtoc so that it defines a single-slice 5957bae9e67eSachartre * disk. 5958bae9e67eSachartre */ 59591aff8f07SAlexandre Chartre vd_build_default_label(vd->dskimg_size, &label); 5960bae9e67eSachartre vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 5961bae9e67eSachartre status = vd_setup_partition_vtoc(vd); 5962bae9e67eSachartre } 59631aff8f07SAlexandre Chartre 5964bae9e67eSachartre return (status); 5965edcc0754Sachartre } 5966edcc0754Sachartre 59671aff8f07SAlexandre Chartre static int 59681aff8f07SAlexandre Chartre vd_setup_disk_image(vd_t *vd) 59691aff8f07SAlexandre Chartre { 59701aff8f07SAlexandre Chartre int status; 59711aff8f07SAlexandre Chartre char *backend_path = vd->device_path; 59721aff8f07SAlexandre Chartre 59731aff8f07SAlexandre Chartre /* size should be at least sizeof(dk_label) */ 59741aff8f07SAlexandre Chartre if (vd->dskimg_size < sizeof (struct dk_label)) { 59751aff8f07SAlexandre Chartre PRN("Size of file has to be at least %ld bytes", 59761aff8f07SAlexandre Chartre sizeof (struct dk_label)); 59771aff8f07SAlexandre Chartre return (EIO); 59781aff8f07SAlexandre Chartre } 59791aff8f07SAlexandre Chartre 59801aff8f07SAlexandre Chartre /* sector size = block size = DEV_BSIZE */ 59811aff8f07SAlexandre Chartre vd->block_size = DEV_BSIZE; 59821aff8f07SAlexandre Chartre vd->vdisk_block_size = DEV_BSIZE; 59831aff8f07SAlexandre Chartre vd->vdisk_size = vd->dskimg_size / DEV_BSIZE; 59841aff8f07SAlexandre Chartre 5985edcc0754Sachartre /* 5986edcc0754Sachartre * Find and validate the geometry of a disk image. 5987edcc0754Sachartre */ 59881aff8f07SAlexandre Chartre status = vd_dskimg_validate_geometry(vd); 5989edcc0754Sachartre if (status != 0 && status != EINVAL && status != ENOTSUP) { 59901aff8f07SAlexandre Chartre PRN("Failed to read label from %s", backend_path); 5991edcc0754Sachartre return (EIO); 5992edcc0754Sachartre } 5993edcc0754Sachartre 59941aff8f07SAlexandre Chartre if (vd_dskimg_is_iso_image(vd)) { 5995edcc0754Sachartre /* 5996edcc0754Sachartre * Indicate whether to call this a CD or DVD from the size 5997edcc0754Sachartre * of the ISO image (images for both drive types are stored 5998edcc0754Sachartre * in the ISO-9600 format). CDs can store up to just under 1Gb 5999edcc0754Sachartre */ 6000342440ecSPrasad Singamsetty if ((vd->vdisk_size * vd->vdisk_block_size) > ONE_GIGABYTE) 6001edcc0754Sachartre vd->vdisk_media = VD_MEDIA_DVD; 6002edcc0754Sachartre else 6003edcc0754Sachartre vd->vdisk_media = VD_MEDIA_CD; 6004edcc0754Sachartre } else { 6005edcc0754Sachartre vd->vdisk_media = VD_MEDIA_FIXED; 6006edcc0754Sachartre } 6007edcc0754Sachartre 6008edcc0754Sachartre /* Setup devid for the disk image */ 6009047ba61eSachartre 601078fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 601178fcd0a1Sachartre 60121aff8f07SAlexandre Chartre status = vd_dskimg_read_devid(vd, &vd->dskimg_devid); 601387a7269eSachartre 601487a7269eSachartre if (status == 0) { 601587a7269eSachartre /* a valid devid was found */ 601687a7269eSachartre return (0); 601787a7269eSachartre } 601887a7269eSachartre 601987a7269eSachartre if (status != EINVAL) { 602087a7269eSachartre /* 602178fcd0a1Sachartre * There was an error while trying to read the devid. 602278fcd0a1Sachartre * So this disk image may have a devid but we are 602378fcd0a1Sachartre * unable to read it. 602487a7269eSachartre */ 60251aff8f07SAlexandre Chartre PR0("can not read devid for %s", backend_path); 60261aff8f07SAlexandre Chartre vd->dskimg_devid = NULL; 602787a7269eSachartre return (0); 602887a7269eSachartre } 602978fcd0a1Sachartre } 603087a7269eSachartre 603187a7269eSachartre /* 603287a7269eSachartre * No valid device id was found so we create one. Note that a failure 603387a7269eSachartre * to create a device id is not fatal and does not prevent the disk 603487a7269eSachartre * image from being attached. 603587a7269eSachartre */ 60361aff8f07SAlexandre Chartre PR1("creating devid for %s", backend_path); 603787a7269eSachartre 603887a7269eSachartre if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0, 60391aff8f07SAlexandre Chartre &vd->dskimg_devid) != DDI_SUCCESS) { 60401aff8f07SAlexandre Chartre PR0("fail to create devid for %s", backend_path); 60411aff8f07SAlexandre Chartre vd->dskimg_devid = NULL; 604287a7269eSachartre return (0); 604387a7269eSachartre } 604487a7269eSachartre 604578fcd0a1Sachartre /* 604678fcd0a1Sachartre * Write devid to the disk image. The devid is stored into the disk 604778fcd0a1Sachartre * image if we have a valid label; otherwise the devid will be stored 604878fcd0a1Sachartre * when the user writes a valid label. 604978fcd0a1Sachartre */ 605078fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 60511aff8f07SAlexandre Chartre if (vd_dskimg_write_devid(vd, vd->dskimg_devid) != 0) { 60521aff8f07SAlexandre Chartre PR0("fail to write devid for %s", backend_path); 60531aff8f07SAlexandre Chartre ddi_devid_free(vd->dskimg_devid); 60541aff8f07SAlexandre Chartre vd->dskimg_devid = NULL; 605587a7269eSachartre } 605678fcd0a1Sachartre } 605787a7269eSachartre 60583c96341aSnarayan return (0); 60593c96341aSnarayan } 60603c96341aSnarayan 606117cadca8Slm66018 606217cadca8Slm66018 /* 606317cadca8Slm66018 * Description: 606417cadca8Slm66018 * Open a device using its device path (supplied by ldm(1m)) 606517cadca8Slm66018 * 606617cadca8Slm66018 * Parameters: 606717cadca8Slm66018 * vd - pointer to structure containing the vDisk info 60688fce2fd6Sachartre * flags - open flags 606917cadca8Slm66018 * 607017cadca8Slm66018 * Return Value 607117cadca8Slm66018 * 0 - success 607217cadca8Slm66018 * != 0 - some other non-zero return value from ldi(9F) functions 607317cadca8Slm66018 */ 607417cadca8Slm66018 static int 60758fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags) 607617cadca8Slm66018 { 60778fce2fd6Sachartre int status; 607817cadca8Slm66018 char *device_path = vd->device_path; 607917cadca8Slm66018 60808fce2fd6Sachartre /* Attempt to open device */ 60818fce2fd6Sachartre status = ldi_open_by_name(device_path, flags, kcred, 608217cadca8Slm66018 &vd->ldi_handle[0], vd->vds->ldi_ident); 608317cadca8Slm66018 608417cadca8Slm66018 /* 608517cadca8Slm66018 * The open can fail for example if we are opening an empty slice. 608617cadca8Slm66018 * In case of a failure, we try the open again but this time with 608717cadca8Slm66018 * the FNDELAY flag. 608817cadca8Slm66018 */ 608917cadca8Slm66018 if (status != 0) 60908fce2fd6Sachartre status = ldi_open_by_name(device_path, flags | FNDELAY, 609117cadca8Slm66018 kcred, &vd->ldi_handle[0], vd->vds->ldi_ident); 609217cadca8Slm66018 609317cadca8Slm66018 if (status != 0) { 609417cadca8Slm66018 PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 609517cadca8Slm66018 vd->ldi_handle[0] = NULL; 609617cadca8Slm66018 return (status); 609717cadca8Slm66018 } 609817cadca8Slm66018 609917cadca8Slm66018 return (0); 610017cadca8Slm66018 } 610117cadca8Slm66018 6102047ba61eSachartre /* 6103047ba61eSachartre * Setup for a virtual disk which backend is a device (a physical disk, 61041aff8f07SAlexandre Chartre * slice or volume device) exported as a full disk or as a slice. In these 61051aff8f07SAlexandre Chartre * cases, the backend is accessed using the LDI interface. 6106047ba61eSachartre */ 61073c96341aSnarayan static int 6108047ba61eSachartre vd_setup_backend_ldi(vd_t *vd) 61091ae08745Sheppo { 6110e1ebb9ecSlm66018 int rval, status; 61111ae08745Sheppo struct dk_cinfo dk_cinfo; 61123c96341aSnarayan char *device_path = vd->device_path; 61131ae08745Sheppo 61148fce2fd6Sachartre /* device has been opened by vd_identify_dev() */ 61158fce2fd6Sachartre ASSERT(vd->ldi_handle[0] != NULL); 61168fce2fd6Sachartre ASSERT(vd->dev[0] != NULL); 61170a55fbb7Slm66018 61183c96341aSnarayan vd->file = B_FALSE; 61194bac2208Snarayan 612078fcd0a1Sachartre /* Verify backing device supports dk_cinfo */ 6121e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 6122047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 6123e1ebb9ecSlm66018 &rval)) != 0) { 6124e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 6125e1ebb9ecSlm66018 status, device_path); 6126e1ebb9ecSlm66018 return (status); 6127e1ebb9ecSlm66018 } 6128e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 6129e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 6130e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 6131e1ebb9ecSlm66018 return (EIO); 6132e1ebb9ecSlm66018 } 61334bac2208Snarayan 61348fce2fd6Sachartre /* 61358fce2fd6Sachartre * The device has been opened read-only by vd_identify_dev(), re-open 61368fce2fd6Sachartre * it read-write if the write flag is set and we don't have an optical 61378fce2fd6Sachartre * device such as a CD-ROM, which, for now, we do not permit writes to 61388fce2fd6Sachartre * and thus should not export write operations to the client. 61398fce2fd6Sachartre * 61408fce2fd6Sachartre * Future: if/when we implement support for guest domains writing to 61418fce2fd6Sachartre * optical devices we will need to do further checking of the media type 61428fce2fd6Sachartre * to distinguish between read-only and writable discs. 61438fce2fd6Sachartre */ 61448fce2fd6Sachartre if (dk_cinfo.dki_ctype == DKC_CDROM) { 61458fce2fd6Sachartre 61468fce2fd6Sachartre vd->open_flags &= ~FWRITE; 61478fce2fd6Sachartre 61488fce2fd6Sachartre } else if (vd->open_flags & FWRITE) { 61498fce2fd6Sachartre 61508fce2fd6Sachartre (void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE, 61518fce2fd6Sachartre kcred); 61528fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags); 61538fce2fd6Sachartre if (status != 0) { 61548fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", 61558fce2fd6Sachartre device_path, status); 61568fce2fd6Sachartre return (status); 61578fce2fd6Sachartre } 61588fce2fd6Sachartre } 61598fce2fd6Sachartre 6160e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 6161e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 6162e1ebb9ecSlm66018 6163047ba61eSachartre /* 616417cadca8Slm66018 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so 616517cadca8Slm66018 * that we can use the correct CDB group when sending USCSI commands. 616617cadca8Slm66018 */ 616717cadca8Slm66018 vd->is_atapi_dev = vd_is_atapi_device(vd); 616817cadca8Slm66018 616917cadca8Slm66018 /* 6170047ba61eSachartre * Export a full disk. 6171047ba61eSachartre * 61721aff8f07SAlexandre Chartre * The exported device can be either a volume, a disk or a CD/DVD 61731aff8f07SAlexandre Chartre * device. We export a device as a full disk if we have an entire 61741aff8f07SAlexandre Chartre * disk slice (slice 2) and if this slice is exported as a full disk 61751aff8f07SAlexandre Chartre * and not as a single slice disk. A CD or DVD device is exported 61761aff8f07SAlexandre Chartre * as a full disk (even if it isn't s2). A volume is exported as a 61771aff8f07SAlexandre Chartre * full disk as long as the "slice" option is not specified. 6178047ba61eSachartre */ 61791aff8f07SAlexandre Chartre if (vd->vdisk_type == VD_DISK_TYPE_DISK) { 61801aff8f07SAlexandre Chartre 61811aff8f07SAlexandre Chartre if (vd->volume) { 61821aff8f07SAlexandre Chartre /* get size of backing device */ 61831aff8f07SAlexandre Chartre if (ldi_get_size(vd->ldi_handle[0], &vd->dskimg_size) != 61841aff8f07SAlexandre Chartre DDI_SUCCESS) { 61851aff8f07SAlexandre Chartre PRN("ldi_get_size() failed for %s", 61861aff8f07SAlexandre Chartre device_path); 61871aff8f07SAlexandre Chartre return (EIO); 61881aff8f07SAlexandre Chartre } 61891aff8f07SAlexandre Chartre 61901aff8f07SAlexandre Chartre /* setup disk image */ 61911aff8f07SAlexandre Chartre return (vd_setup_disk_image(vd)); 61921aff8f07SAlexandre Chartre } 61931aff8f07SAlexandre Chartre 61941aff8f07SAlexandre Chartre if (dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE || 619517cadca8Slm66018 dk_cinfo.dki_ctype == DKC_CDROM) { 61968fce2fd6Sachartre ASSERT(!vd->volume); 61972f5224aeSachartre if (dk_cinfo.dki_ctype == DKC_SCSI_CCS) 61982f5224aeSachartre vd->scsi = B_TRUE; 6199047ba61eSachartre return (vd_setup_full_disk(vd)); 6200047ba61eSachartre } 62011aff8f07SAlexandre Chartre } 6202047ba61eSachartre 6203047ba61eSachartre /* 6204047ba61eSachartre * Export a single slice disk. 6205047ba61eSachartre * 62068fce2fd6Sachartre * The exported device can be either a volume device or a disk slice. If 6207047ba61eSachartre * it is a disk slice different from slice 2 then it is always exported 6208047ba61eSachartre * as a single slice disk even if the "slice" option is not specified. 62098fce2fd6Sachartre * If it is disk slice 2 or a volume device then it is exported as a 6210047ba61eSachartre * single slice disk only if the "slice" option is specified. 6211047ba61eSachartre */ 6212047ba61eSachartre return (vd_setup_single_slice_disk(vd)); 6213047ba61eSachartre } 6214047ba61eSachartre 6215047ba61eSachartre static int 6216047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd) 6217047ba61eSachartre { 6218edcc0754Sachartre int status, rval; 6219bae9e67eSachartre struct dk_label label; 6220047ba61eSachartre char *device_path = vd->device_path; 6221342440ecSPrasad Singamsetty struct vtoc vtoc; 6222047ba61eSachartre 6223047ba61eSachartre /* Get size of backing device */ 6224047ba61eSachartre if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 6225047ba61eSachartre PRN("ldi_get_size() failed for %s", device_path); 62261ae08745Sheppo return (EIO); 62271ae08745Sheppo } 6228047ba61eSachartre vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 622917cadca8Slm66018 vd->block_size = DEV_BSIZE; 623017cadca8Slm66018 vd->vdisk_block_size = DEV_BSIZE; 623117cadca8Slm66018 vd->vdisk_media = VD_MEDIA_FIXED; 6232047ba61eSachartre 62338fce2fd6Sachartre if (vd->volume) { 6234047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 623578fcd0a1Sachartre } 62360a55fbb7Slm66018 6237047ba61eSachartre /* 6238047ba61eSachartre * We export the slice as a single slice disk even if the "slice" 6239047ba61eSachartre * option was not specified. 6240047ba61eSachartre */ 62411ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 62421ae08745Sheppo vd->nslices = 1; 62431ae08745Sheppo 6244edcc0754Sachartre /* 6245edcc0754Sachartre * When exporting a slice or a device as a single slice disk, we don't 6246edcc0754Sachartre * care about any partitioning exposed by the backend. The goal is just 6247edcc0754Sachartre * to export the backend as a flat storage. We provide a fake partition 6248edcc0754Sachartre * table (either a VTOC or EFI), which presents only one slice, to 6249bae9e67eSachartre * accommodate tools expecting a disk label. The selection of the label 6250bae9e67eSachartre * type (VTOC or EFI) depends on the value of the vd_slice_label 6251bae9e67eSachartre * variable. 6252edcc0754Sachartre */ 6253bae9e67eSachartre if (vd_slice_label == VD_DISK_LABEL_EFI || 6254bae9e67eSachartre vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) { 6255bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 6256bae9e67eSachartre } else { 6257342440ecSPrasad Singamsetty status = ldi_ioctl(vd->ldi_handle[0], DKIOCGEXTVTOC, 6258bae9e67eSachartre (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL), 6259bae9e67eSachartre kcred, &rval); 6260edcc0754Sachartre 6261342440ecSPrasad Singamsetty if (status == ENOTTY) { 6262342440ecSPrasad Singamsetty /* try with the non-extended vtoc ioctl */ 6263342440ecSPrasad Singamsetty status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, 6264342440ecSPrasad Singamsetty (intptr_t)&vtoc, (vd->open_flags | FKIOCTL), 6265342440ecSPrasad Singamsetty kcred, &rval); 6266342440ecSPrasad Singamsetty vtoctoextvtoc(vtoc, vd->vtoc); 6267342440ecSPrasad Singamsetty } 6268342440ecSPrasad Singamsetty 6269edcc0754Sachartre if (status == 0) { 6270bae9e67eSachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 6271bae9e67eSachartre (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), 6272bae9e67eSachartre kcred, &rval); 6273bae9e67eSachartre 6274bae9e67eSachartre if (status != 0) { 6275bae9e67eSachartre PRN("ldi_ioctl(DKIOCGEOM) returned errno %d " 6276bae9e67eSachartre "for %s", status, device_path); 6277bae9e67eSachartre return (status); 6278bae9e67eSachartre } 6279edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 6280bae9e67eSachartre 6281bae9e67eSachartre } else if (vd_slice_label == VD_DISK_LABEL_VTOC) { 6282bae9e67eSachartre 6283bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 6284bae9e67eSachartre vd_build_default_label(vd->vdisk_size * DEV_BSIZE, 6285bae9e67eSachartre &label); 6286bae9e67eSachartre vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 6287bae9e67eSachartre 6288bae9e67eSachartre } else { 6289bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 6290bae9e67eSachartre } 6291bae9e67eSachartre } 6292bae9e67eSachartre 6293bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 6294bae9e67eSachartre /* export with a fake VTOC label */ 629578fcd0a1Sachartre status = vd_setup_partition_vtoc(vd); 6296bae9e67eSachartre 6297edcc0754Sachartre } else { 6298edcc0754Sachartre /* export with a fake EFI label */ 6299edcc0754Sachartre status = vd_setup_partition_efi(vd); 630078fcd0a1Sachartre } 630178fcd0a1Sachartre 63024bac2208Snarayan return (status); 63034bac2208Snarayan } 63041ae08745Sheppo 6305de3a5331SRamesh Chitrothu static int 6306de3a5331SRamesh Chitrothu vd_backend_check_size(vd_t *vd) 6307de3a5331SRamesh Chitrothu { 6308de3a5331SRamesh Chitrothu size_t backend_size, old_size, new_size; 6309de3a5331SRamesh Chitrothu struct dk_minfo minfo; 6310de3a5331SRamesh Chitrothu vattr_t vattr; 6311de3a5331SRamesh Chitrothu int rval, rv; 6312de3a5331SRamesh Chitrothu 6313de3a5331SRamesh Chitrothu if (vd->file) { 6314de3a5331SRamesh Chitrothu 6315de3a5331SRamesh Chitrothu /* file (slice or full disk) */ 6316de3a5331SRamesh Chitrothu vattr.va_mask = AT_SIZE; 6317de3a5331SRamesh Chitrothu rv = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL); 6318de3a5331SRamesh Chitrothu if (rv != 0) { 6319de3a5331SRamesh Chitrothu PR0("VOP_GETATTR(%s) = errno %d", vd->device_path, rv); 6320de3a5331SRamesh Chitrothu return (rv); 6321de3a5331SRamesh Chitrothu } 6322de3a5331SRamesh Chitrothu backend_size = vattr.va_size; 6323de3a5331SRamesh Chitrothu 63241aff8f07SAlexandre Chartre } else if (vd->volume || vd->vdisk_type == VD_DISK_TYPE_SLICE) { 6325de3a5331SRamesh Chitrothu 63261aff8f07SAlexandre Chartre /* physical slice or volume (slice or full disk) */ 6327de3a5331SRamesh Chitrothu rv = ldi_get_size(vd->ldi_handle[0], &backend_size); 6328de3a5331SRamesh Chitrothu if (rv != DDI_SUCCESS) { 6329de3a5331SRamesh Chitrothu PR0("ldi_get_size() failed for %s", vd->device_path); 6330de3a5331SRamesh Chitrothu return (EIO); 6331de3a5331SRamesh Chitrothu } 6332de3a5331SRamesh Chitrothu 6333de3a5331SRamesh Chitrothu } else { 6334de3a5331SRamesh Chitrothu 63351aff8f07SAlexandre Chartre /* physical disk */ 6336de3a5331SRamesh Chitrothu ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 6337de3a5331SRamesh Chitrothu rv = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 6338de3a5331SRamesh Chitrothu (intptr_t)&minfo, (vd->open_flags | FKIOCTL), 6339de3a5331SRamesh Chitrothu kcred, &rval); 6340de3a5331SRamesh Chitrothu if (rv != 0) { 6341de3a5331SRamesh Chitrothu PR0("DKIOCGMEDIAINFO failed for %s (err=%d)", 6342de3a5331SRamesh Chitrothu vd->device_path, rv); 6343de3a5331SRamesh Chitrothu return (rv); 6344de3a5331SRamesh Chitrothu } 6345de3a5331SRamesh Chitrothu backend_size = minfo.dki_capacity * minfo.dki_lbsize; 6346de3a5331SRamesh Chitrothu } 6347de3a5331SRamesh Chitrothu 6348de3a5331SRamesh Chitrothu old_size = vd->vdisk_size; 6349de3a5331SRamesh Chitrothu new_size = backend_size / DEV_BSIZE; 6350de3a5331SRamesh Chitrothu 6351de3a5331SRamesh Chitrothu /* check if size has changed */ 6352de3a5331SRamesh Chitrothu if (old_size != VD_SIZE_UNKNOWN && old_size == new_size) 6353de3a5331SRamesh Chitrothu return (0); 6354de3a5331SRamesh Chitrothu 6355de3a5331SRamesh Chitrothu vd->vdisk_size = new_size; 6356de3a5331SRamesh Chitrothu 63571aff8f07SAlexandre Chartre if (vd->file || vd->volume) 63581aff8f07SAlexandre Chartre vd->dskimg_size = backend_size; 6359de3a5331SRamesh Chitrothu 6360de3a5331SRamesh Chitrothu /* 6361de3a5331SRamesh Chitrothu * If we are exporting a single-slice disk and the size of the backend 6362de3a5331SRamesh Chitrothu * has changed then we regenerate the partition setup so that the 6363de3a5331SRamesh Chitrothu * partitioning matches with the new disk backend size. 6364de3a5331SRamesh Chitrothu */ 6365de3a5331SRamesh Chitrothu 6366de3a5331SRamesh Chitrothu if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 6367de3a5331SRamesh Chitrothu /* slice or file or device exported as a slice */ 6368de3a5331SRamesh Chitrothu if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 6369de3a5331SRamesh Chitrothu rv = vd_setup_partition_vtoc(vd); 6370de3a5331SRamesh Chitrothu if (rv != 0) { 6371de3a5331SRamesh Chitrothu PR0("vd_setup_partition_vtoc() failed for %s " 6372de3a5331SRamesh Chitrothu "(err = %d)", vd->device_path, rv); 6373de3a5331SRamesh Chitrothu return (rv); 6374de3a5331SRamesh Chitrothu } 6375de3a5331SRamesh Chitrothu } else { 6376de3a5331SRamesh Chitrothu rv = vd_setup_partition_efi(vd); 6377de3a5331SRamesh Chitrothu if (rv != 0) { 6378de3a5331SRamesh Chitrothu PR0("vd_setup_partition_efi() failed for %s " 6379de3a5331SRamesh Chitrothu "(err = %d)", vd->device_path, rv); 6380de3a5331SRamesh Chitrothu return (rv); 6381de3a5331SRamesh Chitrothu } 6382de3a5331SRamesh Chitrothu } 6383de3a5331SRamesh Chitrothu 63841aff8f07SAlexandre Chartre } else if (!vd->file && !vd->volume) { 63851aff8f07SAlexandre Chartre /* physical disk */ 6386de3a5331SRamesh Chitrothu ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 6387de3a5331SRamesh Chitrothu vd->block_size = minfo.dki_lbsize; 6388de3a5331SRamesh Chitrothu vd->vdisk_media = 6389de3a5331SRamesh Chitrothu DK_MEDIATYPE2VD_MEDIATYPE(minfo.dki_media_type); 6390de3a5331SRamesh Chitrothu } 6391de3a5331SRamesh Chitrothu 6392de3a5331SRamesh Chitrothu return (0); 6393de3a5331SRamesh Chitrothu } 6394de3a5331SRamesh Chitrothu 63958fce2fd6Sachartre /* 63968fce2fd6Sachartre * Description: 63978fce2fd6Sachartre * Open a device using its device path and identify if this is 63988fce2fd6Sachartre * a disk device or a volume device. 63998fce2fd6Sachartre * 64008fce2fd6Sachartre * Parameters: 64018fce2fd6Sachartre * vd - pointer to structure containing the vDisk info 64028fce2fd6Sachartre * dtype - return the driver type of the device 64038fce2fd6Sachartre * 64048fce2fd6Sachartre * Return Value 64058fce2fd6Sachartre * 0 - success 64068fce2fd6Sachartre * != 0 - some other non-zero return value from ldi(9F) functions 64078fce2fd6Sachartre */ 64088fce2fd6Sachartre static int 64098fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype) 64108fce2fd6Sachartre { 64118fce2fd6Sachartre int status, i; 64128fce2fd6Sachartre char *device_path = vd->device_path; 64138fce2fd6Sachartre char *drv_name; 64148fce2fd6Sachartre int drv_type; 64158fce2fd6Sachartre vds_t *vds = vd->vds; 64168fce2fd6Sachartre 64178fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE); 64188fce2fd6Sachartre if (status != 0) { 64198fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", device_path, status); 64208fce2fd6Sachartre return (status); 64218fce2fd6Sachartre } 64228fce2fd6Sachartre 64238fce2fd6Sachartre /* Get device number of backing device */ 64248fce2fd6Sachartre if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 64258fce2fd6Sachartre PRN("ldi_get_dev() returned errno %d for %s", 64268fce2fd6Sachartre status, device_path); 64278fce2fd6Sachartre return (status); 64288fce2fd6Sachartre } 64298fce2fd6Sachartre 64308fce2fd6Sachartre /* 64318fce2fd6Sachartre * We start by looking if the driver is in the list from vds.conf 64328fce2fd6Sachartre * so that we can override the built-in list using vds.conf. 64338fce2fd6Sachartre */ 64348fce2fd6Sachartre drv_name = ddi_major_to_name(getmajor(vd->dev[0])); 64358fce2fd6Sachartre drv_type = VD_DRIVER_UNKNOWN; 64368fce2fd6Sachartre 64378fce2fd6Sachartre /* check vds.conf list */ 64388fce2fd6Sachartre for (i = 0; i < vds->num_drivers; i++) { 64398fce2fd6Sachartre if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) { 64408fce2fd6Sachartre /* ignore invalid entries */ 64418fce2fd6Sachartre continue; 64428fce2fd6Sachartre } 64438fce2fd6Sachartre if (strcmp(drv_name, vds->driver_types[i].name) == 0) { 64448fce2fd6Sachartre drv_type = vds->driver_types[i].type; 64458fce2fd6Sachartre goto done; 64468fce2fd6Sachartre } 64478fce2fd6Sachartre } 64488fce2fd6Sachartre 64498fce2fd6Sachartre /* check built-in list */ 64508fce2fd6Sachartre for (i = 0; i < VDS_NUM_DRIVERS; i++) { 64518fce2fd6Sachartre if (strcmp(drv_name, vds_driver_types[i].name) == 0) { 64528fce2fd6Sachartre drv_type = vds_driver_types[i].type; 64538fce2fd6Sachartre goto done; 64548fce2fd6Sachartre } 64558fce2fd6Sachartre } 64568fce2fd6Sachartre 64578fce2fd6Sachartre done: 64588fce2fd6Sachartre PR0("driver %s identified as %s", drv_name, 64598fce2fd6Sachartre (drv_type == VD_DRIVER_DISK)? "DISK" : 64608fce2fd6Sachartre (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN"); 64618fce2fd6Sachartre 64621aff8f07SAlexandre Chartre if (strcmp(drv_name, "zfs") == 0) 64631aff8f07SAlexandre Chartre vd->zvol = B_TRUE; 64641aff8f07SAlexandre Chartre 64658fce2fd6Sachartre *dtype = drv_type; 64668fce2fd6Sachartre 64678fce2fd6Sachartre return (0); 64688fce2fd6Sachartre } 64698fce2fd6Sachartre 64701ae08745Sheppo static int 6471047ba61eSachartre vd_setup_vd(vd_t *vd) 6472047ba61eSachartre { 64738fce2fd6Sachartre int status, drv_type, pseudo; 6474047ba61eSachartre dev_info_t *dip; 6475047ba61eSachartre vnode_t *vnp; 6476047ba61eSachartre char *path = vd->device_path; 647783990c4aSAlexandre Chartre char tq_name[TASKQ_NAMELEN]; 6478047ba61eSachartre 6479047ba61eSachartre /* make sure the vdisk backend is valid */ 6480047ba61eSachartre if ((status = lookupname(path, UIO_SYSSPACE, 6481047ba61eSachartre FOLLOW, NULLVPP, &vnp)) != 0) { 6482047ba61eSachartre PR0("Cannot lookup %s errno %d", path, status); 6483047ba61eSachartre goto done; 6484047ba61eSachartre } 6485047ba61eSachartre 6486047ba61eSachartre switch (vnp->v_type) { 6487047ba61eSachartre case VREG: 6488047ba61eSachartre /* 6489047ba61eSachartre * Backend is a file so it is exported as a full disk or as a 6490047ba61eSachartre * single slice disk using the vnode interface. 6491047ba61eSachartre */ 6492047ba61eSachartre VN_RELE(vnp); 64938fce2fd6Sachartre vd->volume = B_FALSE; 6494047ba61eSachartre status = vd_setup_backend_vnode(vd); 6495047ba61eSachartre break; 6496047ba61eSachartre 6497047ba61eSachartre case VBLK: 6498047ba61eSachartre case VCHR: 6499047ba61eSachartre /* 65001aff8f07SAlexandre Chartre * Backend is a device. In that case, it is exported using the 65011aff8f07SAlexandre Chartre * LDI interface, and it is exported either as a single-slice 65021aff8f07SAlexandre Chartre * disk or as a full disk depending on the "slice" option and 65031aff8f07SAlexandre Chartre * on the type of device. 6504047ba61eSachartre * 65051aff8f07SAlexandre Chartre * - A volume device is exported as a single-slice disk if the 65061aff8f07SAlexandre Chartre * "slice" is specified, otherwise it is exported as a full 65071aff8f07SAlexandre Chartre * disk. 6508047ba61eSachartre * 6509047ba61eSachartre * - A disk slice (different from slice 2) is always exported 6510047ba61eSachartre * as a single slice disk using the LDI interface. 6511047ba61eSachartre * 6512047ba61eSachartre * - The slice 2 of a disk is exported as a single slice disk 6513047ba61eSachartre * if the "slice" option is specified, otherwise the entire 65141aff8f07SAlexandre Chartre * disk will be exported. 65151aff8f07SAlexandre Chartre * 65161aff8f07SAlexandre Chartre * - The slice of a CD or DVD is exported as single slice disk 65171aff8f07SAlexandre Chartre * if the "slice" option is specified, otherwise the entire 65181aff8f07SAlexandre Chartre * disk will be exported. 6519047ba61eSachartre */ 6520047ba61eSachartre 6521047ba61eSachartre /* check if this is a pseudo device */ 6522047ba61eSachartre if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev), 6523047ba61eSachartre dev_to_instance(vnp->v_rdev), 0)) == NULL) { 6524047ba61eSachartre PRN("%s is no longer accessible", path); 6525047ba61eSachartre VN_RELE(vnp); 6526047ba61eSachartre status = EIO; 6527047ba61eSachartre break; 6528047ba61eSachartre } 65298fce2fd6Sachartre pseudo = is_pseudo_device(dip); 6530047ba61eSachartre ddi_release_devi(dip); 6531047ba61eSachartre VN_RELE(vnp); 6532047ba61eSachartre 6533*d753835aSZach Kissel if ((status = vd_identify_dev(vd, &drv_type)) != 0) { 6534*d753835aSZach Kissel if (status != ENODEV && status != ENXIO && 6535*d753835aSZach Kissel status != ENOENT && status != EROFS) { 6536*d753835aSZach Kissel PRN("%s identification failed with status %d", 6537*d753835aSZach Kissel path, status); 65388fce2fd6Sachartre status = EIO; 6539*d753835aSZach Kissel } 65408fce2fd6Sachartre break; 65418fce2fd6Sachartre } 65428fce2fd6Sachartre 65438fce2fd6Sachartre /* 65448fce2fd6Sachartre * If the driver hasn't been identified then we consider that 65458fce2fd6Sachartre * pseudo devices are volumes and other devices are disks. 65468fce2fd6Sachartre */ 65478fce2fd6Sachartre if (drv_type == VD_DRIVER_VOLUME || 65488fce2fd6Sachartre (drv_type == VD_DRIVER_UNKNOWN && pseudo)) { 65498fce2fd6Sachartre vd->volume = B_TRUE; 65502f5224aeSachartre } 65512f5224aeSachartre 6552047ba61eSachartre /* 65538fce2fd6Sachartre * If this is a volume device then its usage depends if the 6554047ba61eSachartre * "slice" option is set or not. If the "slice" option is set 65558fce2fd6Sachartre * then the volume device will be exported as a single slice, 6556047ba61eSachartre * otherwise it will be exported as a full disk. 65572f5224aeSachartre * 65582f5224aeSachartre * For backward compatibility, if vd_volume_force_slice is set 65598fce2fd6Sachartre * then we always export volume devices as slices. 6560047ba61eSachartre */ 65611aff8f07SAlexandre Chartre if (vd->volume && vd_volume_force_slice) { 65622f5224aeSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 65632f5224aeSachartre vd->nslices = 1; 65642f5224aeSachartre } 65652f5224aeSachartre 6566047ba61eSachartre status = vd_setup_backend_ldi(vd); 6567047ba61eSachartre break; 6568047ba61eSachartre 6569047ba61eSachartre default: 6570047ba61eSachartre PRN("Unsupported vdisk backend %s", path); 6571047ba61eSachartre VN_RELE(vnp); 6572047ba61eSachartre status = EBADF; 6573047ba61eSachartre } 6574047ba61eSachartre 6575047ba61eSachartre done: 6576047ba61eSachartre if (status != 0) { 6577047ba61eSachartre /* 6578047ba61eSachartre * If the error is retryable print an error message only 6579047ba61eSachartre * during the first try. 6580047ba61eSachartre */ 6581047ba61eSachartre if (status == ENXIO || status == ENODEV || 6582047ba61eSachartre status == ENOENT || status == EROFS) { 6583047ba61eSachartre if (!(vd->initialized & VD_SETUP_ERROR)) { 6584047ba61eSachartre PRN("%s is currently inaccessible (error %d)", 6585047ba61eSachartre path, status); 6586047ba61eSachartre } 6587047ba61eSachartre status = EAGAIN; 6588047ba61eSachartre } else { 6589047ba61eSachartre PRN("%s can not be exported as a virtual disk " 6590047ba61eSachartre "(error %d)", path, status); 6591047ba61eSachartre } 6592047ba61eSachartre vd->initialized |= VD_SETUP_ERROR; 6593047ba61eSachartre 6594047ba61eSachartre } else if (vd->initialized & VD_SETUP_ERROR) { 6595047ba61eSachartre /* print a message only if we previously had an error */ 6596047ba61eSachartre PRN("%s is now online", path); 6597047ba61eSachartre vd->initialized &= ~VD_SETUP_ERROR; 6598047ba61eSachartre } 6599047ba61eSachartre 660083990c4aSAlexandre Chartre /* 660183990c4aSAlexandre Chartre * For file or ZFS volume we also need an I/O queue. 660283990c4aSAlexandre Chartre * 660383990c4aSAlexandre Chartre * The I/O task queue is initialized here and not in vds_do_init_vd() 660483990c4aSAlexandre Chartre * (as the start and completion queues) because vd_setup_vd() will be 660583990c4aSAlexandre Chartre * call again if the backend is not available, and we need to know if 660683990c4aSAlexandre Chartre * the backend is a ZFS volume or a file. 660783990c4aSAlexandre Chartre */ 660883990c4aSAlexandre Chartre if ((vd->file || vd->zvol) && vd->ioq == NULL) { 660983990c4aSAlexandre Chartre (void) snprintf(tq_name, sizeof (tq_name), "vd_ioq%lu", vd->id); 661083990c4aSAlexandre Chartre 661183990c4aSAlexandre Chartre if ((vd->ioq = ddi_taskq_create(vd->vds->dip, tq_name, 661283990c4aSAlexandre Chartre vd_ioq_nthreads, TASKQ_DEFAULTPRI, 0)) == NULL) { 661383990c4aSAlexandre Chartre PRN("Could not create io task queue"); 661483990c4aSAlexandre Chartre return (EIO); 661583990c4aSAlexandre Chartre } 661683990c4aSAlexandre Chartre } 661783990c4aSAlexandre Chartre 6618047ba61eSachartre return (status); 6619047ba61eSachartre } 6620047ba61eSachartre 6621047ba61eSachartre static int 6622047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6623047ba61eSachartre uint64_t ldc_id, vd_t **vdp) 66241ae08745Sheppo { 66251ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 66260a55fbb7Slm66018 int status; 66271ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 66281ae08745Sheppo ldc_attr_t ldc_attr; 66291ae08745Sheppo vd_t *vd; 66301ae08745Sheppo 66311ae08745Sheppo 66321ae08745Sheppo ASSERT(vds != NULL); 6633e1ebb9ecSlm66018 ASSERT(device_path != NULL); 66341ae08745Sheppo ASSERT(vdp != NULL); 6635e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 66361ae08745Sheppo 66371ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 66381ae08745Sheppo PRN("No memory for virtual disk"); 66391ae08745Sheppo return (EAGAIN); 66401ae08745Sheppo } 66411ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 664283990c4aSAlexandre Chartre vd->id = id; 66431ae08745Sheppo vd->vds = vds; 66443c96341aSnarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 66451ae08745Sheppo 6646047ba61eSachartre /* Setup open flags */ 6647047ba61eSachartre vd->open_flags = FREAD; 6648047ba61eSachartre 6649047ba61eSachartre if (!(options & VD_OPT_RDONLY)) 6650047ba61eSachartre vd->open_flags |= FWRITE; 6651047ba61eSachartre 6652047ba61eSachartre if (options & VD_OPT_EXCLUSIVE) 6653047ba61eSachartre vd->open_flags |= FEXCL; 6654047ba61eSachartre 6655047ba61eSachartre /* Setup disk type */ 6656047ba61eSachartre if (options & VD_OPT_SLICE) { 6657047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 6658047ba61eSachartre vd->nslices = 1; 6659047ba61eSachartre } else { 6660047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_DISK; 6661047ba61eSachartre vd->nslices = V_NUMPAR; 6662047ba61eSachartre } 6663047ba61eSachartre 6664047ba61eSachartre /* default disk label */ 6665047ba61eSachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 6666047ba61eSachartre 66670a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 66683c96341aSnarayan if ((status = vd_setup_vd(vd)) == 0) { 66693c96341aSnarayan vd->initialized |= VD_DISK_READY; 66701ae08745Sheppo 66713c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 66728fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 66733c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 66748fce2fd6Sachartre (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"), 66753c96341aSnarayan vd->nslices); 66763c96341aSnarayan } else { 66773c96341aSnarayan if (status != EAGAIN) 66783c96341aSnarayan return (status); 66793c96341aSnarayan } 66801ae08745Sheppo 66811ae08745Sheppo /* Initialize locking */ 66821ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 66831ae08745Sheppo &iblock) != DDI_SUCCESS) { 66841ae08745Sheppo PRN("Could not get iblock cookie."); 66851ae08745Sheppo return (EIO); 66861ae08745Sheppo } 66871ae08745Sheppo 66881ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 66891ae08745Sheppo vd->initialized |= VD_LOCKING; 66901ae08745Sheppo 66911ae08745Sheppo 6692d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 6693d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 66941ae08745Sheppo PR1("tq_name = %s", tq_name); 6695d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 66961ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 66971ae08745Sheppo PRN("Could not create task queue"); 66981ae08745Sheppo return (EIO); 66991ae08745Sheppo } 6700d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 6701d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 6702d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 6703d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 6704d10e4ef2Snarayan PRN("Could not create task queue"); 6705d10e4ef2Snarayan return (EIO); 6706d10e4ef2Snarayan } 67075b98b509Sachartre 67085b98b509Sachartre /* Allocate the staging buffer */ 67095b98b509Sachartre vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 67105b98b509Sachartre vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 67115b98b509Sachartre 6712d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 67131ae08745Sheppo 67141ae08745Sheppo 67151ae08745Sheppo /* Bring up LDC */ 67161ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 67171ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 67181ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 6719e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 67201ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 672117cadca8Slm66018 PRN("Could not initialize LDC channel %lx, " 6722690555a1Sachartre "init failed with error %d", ldc_id, status); 67231ae08745Sheppo return (status); 67241ae08745Sheppo } 67251ae08745Sheppo vd->initialized |= VD_LDC; 67261ae08745Sheppo 67271ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 67281ae08745Sheppo (caddr_t)vd)) != 0) { 6729690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6730690555a1Sachartre "reg_callback failed with error %d", ldc_id, status); 67311ae08745Sheppo return (status); 67321ae08745Sheppo } 67331ae08745Sheppo 67341ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 6735690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6736690555a1Sachartre "open failed with error %d", ldc_id, status); 67371ae08745Sheppo return (status); 67381ae08745Sheppo } 67391ae08745Sheppo 67403af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 674134683adeSsg70180 PR0("ldc_up() returned errno %d", status); 67423af08d82Slm66018 } 67433af08d82Slm66018 67444bac2208Snarayan /* Allocate the inband task memory handle */ 67454bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 67464bac2208Snarayan if (status) { 6747690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6748690555a1Sachartre "alloc_handle failed with error %d", ldc_id, status); 67494bac2208Snarayan return (ENXIO); 67504bac2208Snarayan } 67511ae08745Sheppo 67521ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 67531ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 67541ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 67551ae08745Sheppo return (EIO); 67561ae08745Sheppo } 67571ae08745Sheppo 67583af08d82Slm66018 /* store initial state */ 67593af08d82Slm66018 vd->state = VD_STATE_INIT; 67603af08d82Slm66018 67611ae08745Sheppo return (0); 67621ae08745Sheppo } 67631ae08745Sheppo 67643af08d82Slm66018 static void 67653af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 67663af08d82Slm66018 { 67673af08d82Slm66018 if (vdp->dring_task != NULL) { 67683af08d82Slm66018 ASSERT(vdp->dring_len != 0); 67693af08d82Slm66018 /* Free all dring_task memory handles */ 67703af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 67713af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 67725b7cb889Sha137994 kmem_free(vdp->dring_task[i].request, 67735b7cb889Sha137994 (vdp->descriptor_size - 67745b7cb889Sha137994 sizeof (vio_dring_entry_hdr_t))); 67755b7cb889Sha137994 vdp->dring_task[i].request = NULL; 67763af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 67773af08d82Slm66018 vdp->dring_task[i].msg = NULL; 67783af08d82Slm66018 } 67793af08d82Slm66018 kmem_free(vdp->dring_task, 67803af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 67813af08d82Slm66018 vdp->dring_task = NULL; 67823af08d82Slm66018 } 678383990c4aSAlexandre Chartre 678483990c4aSAlexandre Chartre if (vdp->write_queue != NULL) { 678583990c4aSAlexandre Chartre kmem_free(vdp->write_queue, sizeof (buf_t *) * vdp->dring_len); 678683990c4aSAlexandre Chartre vdp->write_queue = NULL; 678783990c4aSAlexandre Chartre } 67883af08d82Slm66018 } 67893af08d82Slm66018 67901ae08745Sheppo /* 67911ae08745Sheppo * Destroy the state associated with a virtual disk 67921ae08745Sheppo */ 67931ae08745Sheppo static void 67941ae08745Sheppo vds_destroy_vd(void *arg) 67951ae08745Sheppo { 67961ae08745Sheppo vd_t *vd = (vd_t *)arg; 679734683adeSsg70180 int retry = 0, rv; 67981ae08745Sheppo 67991ae08745Sheppo if (vd == NULL) 68001ae08745Sheppo return; 68011ae08745Sheppo 6802d10e4ef2Snarayan PR0("Destroying vdisk state"); 6803d10e4ef2Snarayan 68041ae08745Sheppo /* Disable queuing requests for the vdisk */ 68051ae08745Sheppo if (vd->initialized & VD_LOCKING) { 68061ae08745Sheppo mutex_enter(&vd->lock); 68071ae08745Sheppo vd->enabled = 0; 68081ae08745Sheppo mutex_exit(&vd->lock); 68091ae08745Sheppo } 68101ae08745Sheppo 681183990c4aSAlexandre Chartre /* Drain and destroy start queue (*before* destroying ioq) */ 6812d10e4ef2Snarayan if (vd->startq != NULL) 6813d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 6814d10e4ef2Snarayan 681583990c4aSAlexandre Chartre /* Drain and destroy the I/O queue (*before* destroying completionq) */ 681683990c4aSAlexandre Chartre if (vd->ioq != NULL) 681783990c4aSAlexandre Chartre ddi_taskq_destroy(vd->ioq); 681883990c4aSAlexandre Chartre 6819d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 6820d10e4ef2Snarayan if (vd->completionq != NULL) 6821d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 6822d10e4ef2Snarayan 68233af08d82Slm66018 vd_free_dring_task(vd); 68243af08d82Slm66018 682534683adeSsg70180 /* Free the inband task memory handle */ 682634683adeSsg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 682734683adeSsg70180 682834683adeSsg70180 /* Shut down LDC */ 682934683adeSsg70180 if (vd->initialized & VD_LDC) { 683034683adeSsg70180 /* unmap the dring */ 683134683adeSsg70180 if (vd->initialized & VD_DRING) 683234683adeSsg70180 (void) ldc_mem_dring_unmap(vd->dring_handle); 683334683adeSsg70180 683434683adeSsg70180 /* close LDC channel - retry on EAGAIN */ 683534683adeSsg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 683634683adeSsg70180 if (++retry > vds_ldc_retries) { 683734683adeSsg70180 PR0("Timed out closing channel"); 683834683adeSsg70180 break; 683934683adeSsg70180 } 684034683adeSsg70180 drv_usecwait(vds_ldc_delay); 684134683adeSsg70180 } 684234683adeSsg70180 if (rv == 0) { 684334683adeSsg70180 (void) ldc_unreg_callback(vd->ldc_handle); 684434683adeSsg70180 (void) ldc_fini(vd->ldc_handle); 684534683adeSsg70180 } else { 684634683adeSsg70180 /* 684734683adeSsg70180 * Closing the LDC channel has failed. Ideally we should 684834683adeSsg70180 * fail here but there is no Zeus level infrastructure 684934683adeSsg70180 * to handle this. The MD has already been changed and 685034683adeSsg70180 * we have to do the close. So we try to do as much 685134683adeSsg70180 * clean up as we can. 685234683adeSsg70180 */ 685334683adeSsg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 685434683adeSsg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 685534683adeSsg70180 drv_usecwait(vds_ldc_delay); 685634683adeSsg70180 } 685734683adeSsg70180 } 685834683adeSsg70180 68593af08d82Slm66018 /* Free the staging buffer for msgs */ 68603af08d82Slm66018 if (vd->vio_msgp != NULL) { 68613af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 68623af08d82Slm66018 vd->vio_msgp = NULL; 68633af08d82Slm66018 } 68643af08d82Slm66018 68653af08d82Slm66018 /* Free the inband message buffer */ 68663af08d82Slm66018 if (vd->inband_task.msg != NULL) { 68673af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 68683af08d82Slm66018 vd->inband_task.msg = NULL; 6869d10e4ef2Snarayan } 6870da6c28aaSamw 68713c96341aSnarayan if (vd->file) { 6872690555a1Sachartre /* Close file */ 6873047ba61eSachartre (void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1, 6874da6c28aaSamw 0, kcred, NULL); 68753c96341aSnarayan VN_RELE(vd->file_vnode); 68763c96341aSnarayan } else { 68771ae08745Sheppo /* Close any open backing-device slices */ 6878bae9e67eSachartre for (uint_t slice = 0; slice < V_NUMPAR; slice++) { 68791ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 68801ae08745Sheppo PR0("Closing slice %u", slice); 68811ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 6882047ba61eSachartre vd->open_flags, kcred); 68831ae08745Sheppo } 68841ae08745Sheppo } 68853c96341aSnarayan } 68861ae08745Sheppo 68871aff8f07SAlexandre Chartre /* Free disk image devid */ 68881aff8f07SAlexandre Chartre if (vd->dskimg_devid != NULL) 68891aff8f07SAlexandre Chartre ddi_devid_free(vd->dskimg_devid); 68901aff8f07SAlexandre Chartre 6891bae9e67eSachartre /* Free any fake label */ 6892bae9e67eSachartre if (vd->flabel) { 6893bae9e67eSachartre kmem_free(vd->flabel, vd->flabel_size); 6894bae9e67eSachartre vd->flabel = NULL; 6895bae9e67eSachartre vd->flabel_size = 0; 6896bae9e67eSachartre } 6897bae9e67eSachartre 68981ae08745Sheppo /* Free lock */ 68991ae08745Sheppo if (vd->initialized & VD_LOCKING) 69001ae08745Sheppo mutex_destroy(&vd->lock); 69011ae08745Sheppo 69021ae08745Sheppo /* Finally, free the vdisk structure itself */ 69031ae08745Sheppo kmem_free(vd, sizeof (*vd)); 69041ae08745Sheppo } 69051ae08745Sheppo 69061ae08745Sheppo static int 6907047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6908047ba61eSachartre uint64_t ldc_id) 69091ae08745Sheppo { 69101ae08745Sheppo int status; 69111ae08745Sheppo vd_t *vd = NULL; 69121ae08745Sheppo 69131ae08745Sheppo 6914047ba61eSachartre if ((status = vds_do_init_vd(vds, id, device_path, options, 6915047ba61eSachartre ldc_id, &vd)) != 0) 69161ae08745Sheppo vds_destroy_vd(vd); 69171ae08745Sheppo 69181ae08745Sheppo return (status); 69191ae08745Sheppo } 69201ae08745Sheppo 69211ae08745Sheppo static int 69221ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 69231ae08745Sheppo uint64_t *ldc_id) 69241ae08745Sheppo { 69251ae08745Sheppo int num_channels; 69261ae08745Sheppo 69271ae08745Sheppo 69281ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 69291ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 69301ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 69311ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 69321ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 69331ae08745Sheppo return (-1); 69341ae08745Sheppo } 69351ae08745Sheppo 69361ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 69371ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 69381ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 69391ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 69401ae08745Sheppo return (-1); 69411ae08745Sheppo } 69421ae08745Sheppo 69431ae08745Sheppo if (num_channels > 1) { 69441ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 69451ae08745Sheppo } 69461ae08745Sheppo 69471ae08745Sheppo return (0); 69481ae08745Sheppo } 69491ae08745Sheppo 69501ae08745Sheppo static int 69511ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 69521ae08745Sheppo { 69531ae08745Sheppo int num_nodes, status; 69541ae08745Sheppo size_t size; 69551ae08745Sheppo mde_cookie_t *channel; 69561ae08745Sheppo 69571ae08745Sheppo 69581ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 69591ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 69601ae08745Sheppo return (-1); 69611ae08745Sheppo } 69621ae08745Sheppo size = num_nodes*(sizeof (*channel)); 69631ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 69641ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 69651ae08745Sheppo kmem_free(channel, size); 69661ae08745Sheppo 69671ae08745Sheppo return (status); 69681ae08745Sheppo } 69691ae08745Sheppo 6970047ba61eSachartre /* 6971047ba61eSachartre * Function: 6972047ba61eSachartre * vds_get_options 6973047ba61eSachartre * 6974047ba61eSachartre * Description: 6975047ba61eSachartre * Parse the options of a vds node. Options are defined as an array 6976047ba61eSachartre * of strings in the vds-block-device-opts property of the vds node 6977047ba61eSachartre * in the machine description. Options are returned as a bitmask. The 6978047ba61eSachartre * mapping between the bitmask options and the options strings from the 6979047ba61eSachartre * machine description is defined in the vd_bdev_options[] array. 6980047ba61eSachartre * 6981047ba61eSachartre * The vds-block-device-opts property is optional. If a vds has no such 6982047ba61eSachartre * property then no option is defined. 6983047ba61eSachartre * 6984047ba61eSachartre * Parameters: 6985047ba61eSachartre * md - machine description. 6986047ba61eSachartre * vd_node - vds node in the machine description for which 6987047ba61eSachartre * options have to be parsed. 6988047ba61eSachartre * options - the returned options. 6989047ba61eSachartre * 6990047ba61eSachartre * Return Code: 6991047ba61eSachartre * none. 6992047ba61eSachartre */ 6993047ba61eSachartre static void 6994047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options) 6995047ba61eSachartre { 6996047ba61eSachartre char *optstr, *opt; 6997047ba61eSachartre int len, n, i; 6998047ba61eSachartre 6999047ba61eSachartre *options = 0; 7000047ba61eSachartre 7001047ba61eSachartre if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS, 7002047ba61eSachartre (uint8_t **)&optstr, &len) != 0) { 7003047ba61eSachartre PR0("No options found"); 7004047ba61eSachartre return; 7005047ba61eSachartre } 7006047ba61eSachartre 7007047ba61eSachartre /* parse options */ 7008047ba61eSachartre opt = optstr; 7009047ba61eSachartre n = sizeof (vd_bdev_options) / sizeof (vd_option_t); 7010047ba61eSachartre 7011047ba61eSachartre while (opt < optstr + len) { 7012047ba61eSachartre for (i = 0; i < n; i++) { 7013047ba61eSachartre if (strncmp(vd_bdev_options[i].vdo_name, 7014047ba61eSachartre opt, VD_OPTION_NLEN) == 0) { 7015047ba61eSachartre *options |= vd_bdev_options[i].vdo_value; 7016047ba61eSachartre break; 7017047ba61eSachartre } 7018047ba61eSachartre } 7019047ba61eSachartre 7020047ba61eSachartre if (i < n) { 7021047ba61eSachartre PR0("option: %s", opt); 7022047ba61eSachartre } else { 7023047ba61eSachartre PRN("option %s is unknown or unsupported", opt); 7024047ba61eSachartre } 7025047ba61eSachartre 7026047ba61eSachartre opt += strlen(opt) + 1; 7027047ba61eSachartre } 7028047ba61eSachartre } 7029047ba61eSachartre 70301ae08745Sheppo static void 70318fce2fd6Sachartre vds_driver_types_free(vds_t *vds) 70328fce2fd6Sachartre { 70338fce2fd6Sachartre if (vds->driver_types != NULL) { 70348fce2fd6Sachartre kmem_free(vds->driver_types, sizeof (vd_driver_type_t) * 70358fce2fd6Sachartre vds->num_drivers); 70368fce2fd6Sachartre vds->driver_types = NULL; 70378fce2fd6Sachartre vds->num_drivers = 0; 70388fce2fd6Sachartre } 70398fce2fd6Sachartre } 70408fce2fd6Sachartre 70418fce2fd6Sachartre /* 70428fce2fd6Sachartre * Update the driver type list with information from vds.conf. 70438fce2fd6Sachartre */ 70448fce2fd6Sachartre static void 70458fce2fd6Sachartre vds_driver_types_update(vds_t *vds) 70468fce2fd6Sachartre { 70478fce2fd6Sachartre char **list, *s; 70488fce2fd6Sachartre uint_t i, num, count = 0, len; 70498fce2fd6Sachartre 70508fce2fd6Sachartre if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip, 70518fce2fd6Sachartre DDI_PROP_DONTPASS, "driver-type-list", &list, &num) != 70528fce2fd6Sachartre DDI_PROP_SUCCESS) 70538fce2fd6Sachartre return; 70548fce2fd6Sachartre 70558fce2fd6Sachartre /* 70568fce2fd6Sachartre * We create a driver_types list with as many as entries as there 70578fce2fd6Sachartre * is in the driver-type-list from vds.conf. However only valid 70588fce2fd6Sachartre * entries will be populated (i.e. entries from driver-type-list 70598fce2fd6Sachartre * with a valid syntax). Invalid entries will be left blank so 70608fce2fd6Sachartre * they will have no driver name and the driver type will be 70618fce2fd6Sachartre * VD_DRIVER_UNKNOWN (= 0). 70628fce2fd6Sachartre */ 70638fce2fd6Sachartre vds->num_drivers = num; 70648fce2fd6Sachartre vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num, 70658fce2fd6Sachartre KM_SLEEP); 70668fce2fd6Sachartre 70678fce2fd6Sachartre for (i = 0; i < num; i++) { 70688fce2fd6Sachartre 70698fce2fd6Sachartre s = strchr(list[i], ':'); 70708fce2fd6Sachartre 70718fce2fd6Sachartre if (s == NULL) { 70728fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 70738fce2fd6Sachartre "a colon is expected in the entry", 70748fce2fd6Sachartre i, list[i]); 70758fce2fd6Sachartre continue; 70768fce2fd6Sachartre } 70778fce2fd6Sachartre 70788fce2fd6Sachartre len = (uintptr_t)s - (uintptr_t)list[i]; 70798fce2fd6Sachartre 70808fce2fd6Sachartre if (len == 0) { 70818fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 70828fce2fd6Sachartre "the driver name is empty", 70838fce2fd6Sachartre i, list[i]); 70848fce2fd6Sachartre continue; 70858fce2fd6Sachartre } 70868fce2fd6Sachartre 70878fce2fd6Sachartre if (len >= VD_DRIVER_NAME_LEN) { 70888fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 70898fce2fd6Sachartre "the driver name is too long", 70908fce2fd6Sachartre i, list[i]); 70918fce2fd6Sachartre continue; 70928fce2fd6Sachartre } 70938fce2fd6Sachartre 70948fce2fd6Sachartre if (strcmp(s + 1, "disk") == 0) { 70958fce2fd6Sachartre 70968fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_DISK; 70978fce2fd6Sachartre 70988fce2fd6Sachartre } else if (strcmp(s + 1, "volume") == 0) { 70998fce2fd6Sachartre 71008fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_VOLUME; 71018fce2fd6Sachartre 71028fce2fd6Sachartre } else { 71038fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 71048fce2fd6Sachartre "the driver type is invalid", 71058fce2fd6Sachartre i, list[i]); 71068fce2fd6Sachartre continue; 71078fce2fd6Sachartre } 71088fce2fd6Sachartre 71098fce2fd6Sachartre (void) strncpy(vds->driver_types[i].name, list[i], len); 71108fce2fd6Sachartre 71118fce2fd6Sachartre PR0("driver-type-list, entry %d (%s) added", 71128fce2fd6Sachartre i, list[i]); 71138fce2fd6Sachartre 71148fce2fd6Sachartre count++; 71158fce2fd6Sachartre } 71168fce2fd6Sachartre 71178fce2fd6Sachartre ddi_prop_free(list); 71188fce2fd6Sachartre 71198fce2fd6Sachartre if (count == 0) { 71208fce2fd6Sachartre /* nothing was added, clean up */ 71218fce2fd6Sachartre vds_driver_types_free(vds); 71228fce2fd6Sachartre } 71238fce2fd6Sachartre } 71248fce2fd6Sachartre 71258fce2fd6Sachartre static void 71261ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 71271ae08745Sheppo { 7128e1ebb9ecSlm66018 char *device_path = NULL; 7129047ba61eSachartre uint64_t id = 0, ldc_id = 0, options = 0; 71301ae08745Sheppo 71311ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 71321ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 71331ae08745Sheppo return; 71341ae08745Sheppo } 71351ae08745Sheppo PR0("Adding vdisk ID %lu", id); 71361ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 7137e1ebb9ecSlm66018 &device_path) != 0) { 71381ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 71391ae08745Sheppo return; 71401ae08745Sheppo } 71411ae08745Sheppo 7142047ba61eSachartre vds_get_options(md, vd_node, &options); 7143047ba61eSachartre 71441ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 71451ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 71461ae08745Sheppo return; 71471ae08745Sheppo } 71481ae08745Sheppo 7149047ba61eSachartre if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) { 71501ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 715117cadca8Slm66018 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 715217cadca8Slm66018 PRN("No vDisk entry found for vdisk ID %lu", id); 71531ae08745Sheppo return; 71541ae08745Sheppo } 71551ae08745Sheppo } 71561ae08745Sheppo 71571ae08745Sheppo static void 71581ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 71591ae08745Sheppo { 71601ae08745Sheppo uint64_t id = 0; 71611ae08745Sheppo 71621ae08745Sheppo 71631ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 71641ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 71651ae08745Sheppo VD_ID_PROP); 71661ae08745Sheppo return; 71671ae08745Sheppo } 71681ae08745Sheppo PR0("Removing vdisk ID %lu", id); 71691ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 71701ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 71711ae08745Sheppo } 71721ae08745Sheppo 71731ae08745Sheppo static void 71741ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 71751ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 71761ae08745Sheppo { 71771ae08745Sheppo char *curr_dev, *prev_dev; 7178047ba61eSachartre uint64_t curr_id = 0, curr_ldc_id = 0, curr_options = 0; 7179047ba61eSachartre uint64_t prev_id = 0, prev_ldc_id = 0, prev_options = 0; 71801ae08745Sheppo size_t len; 71811ae08745Sheppo 71821ae08745Sheppo 71831ae08745Sheppo /* Validate that vdisk ID has not changed */ 71841ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 71851ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 71861ae08745Sheppo VD_ID_PROP); 71871ae08745Sheppo return; 71881ae08745Sheppo } 71891ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 71901ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 71911ae08745Sheppo return; 71921ae08745Sheppo } 71931ae08745Sheppo if (curr_id != prev_id) { 71941ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 71951ae08745Sheppo prev_id, curr_id); 71961ae08745Sheppo return; 71971ae08745Sheppo } 71981ae08745Sheppo 71991ae08745Sheppo /* Validate that LDC ID has not changed */ 72001ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 72011ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 72021ae08745Sheppo return; 72031ae08745Sheppo } 72041ae08745Sheppo 72051ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 72061ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 72071ae08745Sheppo return; 72081ae08745Sheppo } 72091ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 72100a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 72111ae08745Sheppo PRN("Not changing vdisk: " 72121ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 72131ae08745Sheppo return; 72141ae08745Sheppo } 72151ae08745Sheppo 72161ae08745Sheppo /* Determine whether device path has changed */ 72171ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 72181ae08745Sheppo &prev_dev) != 0) { 72191ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 72201ae08745Sheppo VD_BLOCK_DEVICE_PROP); 72211ae08745Sheppo return; 72221ae08745Sheppo } 72231ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 72241ae08745Sheppo &curr_dev) != 0) { 72251ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 72261ae08745Sheppo return; 72271ae08745Sheppo } 72281ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 72291ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 72301ae08745Sheppo return; /* no relevant (supported) change */ 72311ae08745Sheppo 7232047ba61eSachartre /* Validate that options have not changed */ 7233047ba61eSachartre vds_get_options(prev_md, prev_vd_node, &prev_options); 7234047ba61eSachartre vds_get_options(curr_md, curr_vd_node, &curr_options); 7235047ba61eSachartre if (prev_options != curr_options) { 7236047ba61eSachartre PRN("Not changing vdisk: options changed from %lx to %lx", 7237047ba61eSachartre prev_options, curr_options); 7238047ba61eSachartre return; 7239047ba61eSachartre } 7240047ba61eSachartre 72411ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 72423af08d82Slm66018 72431ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 72441ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 72451ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 72463af08d82Slm66018 72471ae08745Sheppo /* Re-initialize vdisk with new state */ 7248047ba61eSachartre if (vds_init_vd(vds, curr_id, curr_dev, curr_options, 7249047ba61eSachartre curr_ldc_id) != 0) { 72501ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 72511ae08745Sheppo return; 72521ae08745Sheppo } 72531ae08745Sheppo } 72541ae08745Sheppo 72551ae08745Sheppo static int 72561ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 72571ae08745Sheppo { 72581ae08745Sheppo int i; 72591ae08745Sheppo vds_t *vds = arg; 72601ae08745Sheppo 72611ae08745Sheppo 72621ae08745Sheppo if (md == NULL) 72631ae08745Sheppo return (MDEG_FAILURE); 72641ae08745Sheppo ASSERT(vds != NULL); 72651ae08745Sheppo 72661ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 72671ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 72681ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 72691ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 72701ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 72711ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 72721ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 72731ae08745Sheppo 72741ae08745Sheppo return (MDEG_SUCCESS); 72751ae08745Sheppo } 72761ae08745Sheppo 72773c96341aSnarayan 72781ae08745Sheppo static int 72791ae08745Sheppo vds_do_attach(dev_info_t *dip) 72801ae08745Sheppo { 7281445b4c2eSsb155480 int status, sz; 7282445b4c2eSsb155480 int cfg_handle; 72831ae08745Sheppo minor_t instance = ddi_get_instance(dip); 72841ae08745Sheppo vds_t *vds; 7285445b4c2eSsb155480 mdeg_prop_spec_t *pspecp; 7286445b4c2eSsb155480 mdeg_node_spec_t *ispecp; 72871ae08745Sheppo 72881ae08745Sheppo /* 72891ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 72901ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 72911ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 72921ae08745Sheppo * the "reg" property on the node in the device tree it builds from 72931ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 72941ae08745Sheppo * "reg" property value to uniquely identify this device instance when 72951ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 72961ae08745Sheppo * property cannot be found, the device tree state is presumably so 72971ae08745Sheppo * broken that there is no point in continuing. 72981ae08745Sheppo */ 7299445b4c2eSsb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 7300445b4c2eSsb155480 VD_REG_PROP)) { 7301445b4c2eSsb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 73021ae08745Sheppo return (DDI_FAILURE); 73031ae08745Sheppo } 73041ae08745Sheppo 73051ae08745Sheppo /* Get the MD instance for later MDEG registration */ 73061ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 7307445b4c2eSsb155480 VD_REG_PROP, -1); 73081ae08745Sheppo 73091ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 73101ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 73111ae08745Sheppo return (DDI_FAILURE); 73121ae08745Sheppo } 73131ae08745Sheppo 73141ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 73151ae08745Sheppo PRN("Could not get state for instance %u", instance); 73161ae08745Sheppo ddi_soft_state_free(vds_state, instance); 73171ae08745Sheppo return (DDI_FAILURE); 73181ae08745Sheppo } 73191ae08745Sheppo 73201ae08745Sheppo vds->dip = dip; 73211ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 732287a7269eSachartre vds_destroy_vd, sizeof (void *)); 732387a7269eSachartre 73241ae08745Sheppo ASSERT(vds->vd_table != NULL); 73251ae08745Sheppo 73261ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 73271ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 73281ae08745Sheppo return (DDI_FAILURE); 73291ae08745Sheppo } 73301ae08745Sheppo vds->initialized |= VDS_LDI; 73311ae08745Sheppo 73321ae08745Sheppo /* Register for MD updates */ 7333445b4c2eSsb155480 sz = sizeof (vds_prop_template); 7334445b4c2eSsb155480 pspecp = kmem_alloc(sz, KM_SLEEP); 7335445b4c2eSsb155480 bcopy(vds_prop_template, pspecp, sz); 7336445b4c2eSsb155480 7337445b4c2eSsb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 7338445b4c2eSsb155480 7339445b4c2eSsb155480 /* initialize the complete prop spec structure */ 7340445b4c2eSsb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 7341445b4c2eSsb155480 ispecp->namep = "virtual-device"; 7342445b4c2eSsb155480 ispecp->specp = pspecp; 7343445b4c2eSsb155480 7344445b4c2eSsb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 73451ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 73461ae08745Sheppo PRN("Unable to register for MD updates"); 7347445b4c2eSsb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 7348445b4c2eSsb155480 kmem_free(pspecp, sz); 73491ae08745Sheppo return (DDI_FAILURE); 73501ae08745Sheppo } 7351445b4c2eSsb155480 7352445b4c2eSsb155480 vds->ispecp = ispecp; 73531ae08745Sheppo vds->initialized |= VDS_MDEG; 73541ae08745Sheppo 73550a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 73560a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 73570a55fbb7Slm66018 DDI_PROP_SUCCESS) { 73580a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 73590a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 73600a55fbb7Slm66018 } 73610a55fbb7Slm66018 73628fce2fd6Sachartre /* read any user defined driver types from conf file and update list */ 73638fce2fd6Sachartre vds_driver_types_update(vds); 73648fce2fd6Sachartre 73651ae08745Sheppo ddi_report_dev(dip); 73661ae08745Sheppo return (DDI_SUCCESS); 73671ae08745Sheppo } 73681ae08745Sheppo 73691ae08745Sheppo static int 73701ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 73711ae08745Sheppo { 73721ae08745Sheppo int status; 73731ae08745Sheppo 73741ae08745Sheppo switch (cmd) { 73751ae08745Sheppo case DDI_ATTACH: 7376d10e4ef2Snarayan PR0("Attaching"); 73771ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 73781ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 73791ae08745Sheppo return (status); 73801ae08745Sheppo case DDI_RESUME: 7381d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 73821ae08745Sheppo return (DDI_SUCCESS); 73831ae08745Sheppo default: 73841ae08745Sheppo return (DDI_FAILURE); 73851ae08745Sheppo } 73861ae08745Sheppo } 73871ae08745Sheppo 73881ae08745Sheppo static struct dev_ops vds_ops = { 73891ae08745Sheppo DEVO_REV, /* devo_rev */ 73901ae08745Sheppo 0, /* devo_refcnt */ 73911ae08745Sheppo ddi_no_info, /* devo_getinfo */ 73921ae08745Sheppo nulldev, /* devo_identify */ 73931ae08745Sheppo nulldev, /* devo_probe */ 73941ae08745Sheppo vds_attach, /* devo_attach */ 73951ae08745Sheppo vds_detach, /* devo_detach */ 73961ae08745Sheppo nodev, /* devo_reset */ 73971ae08745Sheppo NULL, /* devo_cb_ops */ 73981ae08745Sheppo NULL, /* devo_bus_ops */ 739919397407SSherry Moore nulldev, /* devo_power */ 740019397407SSherry Moore ddi_quiesce_not_needed, /* devo_quiesce */ 74011ae08745Sheppo }; 74021ae08745Sheppo 74031ae08745Sheppo static struct modldrv modldrv = { 74041ae08745Sheppo &mod_driverops, 7405205eeb1aSlm66018 "virtual disk server", 74061ae08745Sheppo &vds_ops, 74071ae08745Sheppo }; 74081ae08745Sheppo 74091ae08745Sheppo static struct modlinkage modlinkage = { 74101ae08745Sheppo MODREV_1, 74111ae08745Sheppo &modldrv, 74121ae08745Sheppo NULL 74131ae08745Sheppo }; 74141ae08745Sheppo 74151ae08745Sheppo 74161ae08745Sheppo int 74171ae08745Sheppo _init(void) 74181ae08745Sheppo { 741917cadca8Slm66018 int status; 7420d10e4ef2Snarayan 74211ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 74221ae08745Sheppo return (status); 742317cadca8Slm66018 74241ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 74251ae08745Sheppo ddi_soft_state_fini(&vds_state); 74261ae08745Sheppo return (status); 74271ae08745Sheppo } 74281ae08745Sheppo 74291ae08745Sheppo return (0); 74301ae08745Sheppo } 74311ae08745Sheppo 74321ae08745Sheppo int 74331ae08745Sheppo _info(struct modinfo *modinfop) 74341ae08745Sheppo { 74351ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 74361ae08745Sheppo } 74371ae08745Sheppo 74381ae08745Sheppo int 74391ae08745Sheppo _fini(void) 74401ae08745Sheppo { 74411ae08745Sheppo int status; 74421ae08745Sheppo 74431ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 74441ae08745Sheppo return (status); 74451ae08745Sheppo ddi_soft_state_fini(&vds_state); 74461ae08745Sheppo return (0); 74471ae08745Sheppo } 7448