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 /* 23edcc0754Sachartre * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 241ae08745Sheppo * Use is subject to license terms. 251ae08745Sheppo */ 261ae08745Sheppo 271ae08745Sheppo #pragma ident "%Z%%M% %I% %E% SMI" 281ae08745Sheppo 291ae08745Sheppo /* 301ae08745Sheppo * Virtual disk server 311ae08745Sheppo */ 321ae08745Sheppo 331ae08745Sheppo 341ae08745Sheppo #include <sys/types.h> 351ae08745Sheppo #include <sys/conf.h> 364bac2208Snarayan #include <sys/crc32.h> 371ae08745Sheppo #include <sys/ddi.h> 381ae08745Sheppo #include <sys/dkio.h> 391ae08745Sheppo #include <sys/file.h> 4017cadca8Slm66018 #include <sys/fs/hsfs_isospec.h> 411ae08745Sheppo #include <sys/mdeg.h> 422f5224aeSachartre #include <sys/mhd.h> 431ae08745Sheppo #include <sys/modhash.h> 441ae08745Sheppo #include <sys/note.h> 451ae08745Sheppo #include <sys/pathname.h> 46205eeb1aSlm66018 #include <sys/sdt.h> 471ae08745Sheppo #include <sys/sunddi.h> 481ae08745Sheppo #include <sys/sunldi.h> 491ae08745Sheppo #include <sys/sysmacros.h> 501ae08745Sheppo #include <sys/vio_common.h> 5117cadca8Slm66018 #include <sys/vio_util.h> 521ae08745Sheppo #include <sys/vdsk_mailbox.h> 531ae08745Sheppo #include <sys/vdsk_common.h> 541ae08745Sheppo #include <sys/vtoc.h> 553c96341aSnarayan #include <sys/vfs.h> 563c96341aSnarayan #include <sys/stat.h> 5787a7269eSachartre #include <sys/scsi/impl/uscsi.h> 58bbfa0259Sha137994 #include <sys/ontrap.h> 59690555a1Sachartre #include <vm/seg_map.h> 601ae08745Sheppo 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 95eba0cb4eSachartre /* Flags for writing to a vdisk which is a file */ 96eba0cb4eSachartre #define VD_FILE_WRITE_FLAGS SM_ASYNC 97eba0cb4eSachartre 9887a7269eSachartre /* Number of backup labels */ 9987a7269eSachartre #define VD_FILE_NUM_BACKUP 5 10087a7269eSachartre 10187a7269eSachartre /* Timeout for SCSI I/O */ 10287a7269eSachartre #define VD_SCSI_RDWR_TIMEOUT 30 /* 30 secs */ 10387a7269eSachartre 104edcc0754Sachartre /* Maximum number of logical partitions */ 105edcc0754Sachartre #define VD_MAXPART (NDKMAP + 1) 106edcc0754Sachartre 1071ae08745Sheppo /* 1081ae08745Sheppo * By Solaris convention, slice/partition 2 represents the entire disk; 1091ae08745Sheppo * unfortunately, this convention does not appear to be codified. 1101ae08745Sheppo */ 1111ae08745Sheppo #define VD_ENTIRE_DISK_SLICE 2 1121ae08745Sheppo 113bae9e67eSachartre /* Logical block address for EFI */ 114bae9e67eSachartre #define VD_EFI_LBA_GPT 1 /* LBA of the GPT */ 115bae9e67eSachartre #define VD_EFI_LBA_GPE 2 /* LBA of the GPE */ 116bae9e67eSachartre 1178fce2fd6Sachartre /* Driver types */ 1188fce2fd6Sachartre typedef enum vd_driver { 1198fce2fd6Sachartre VD_DRIVER_UNKNOWN = 0, /* driver type unknown */ 1208fce2fd6Sachartre VD_DRIVER_DISK, /* disk driver */ 1218fce2fd6Sachartre VD_DRIVER_VOLUME /* volume driver */ 1228fce2fd6Sachartre } vd_driver_t; 1238fce2fd6Sachartre 1248fce2fd6Sachartre #define VD_DRIVER_NAME_LEN 64 1258fce2fd6Sachartre 1268fce2fd6Sachartre #define VDS_NUM_DRIVERS (sizeof (vds_driver_types) / sizeof (vd_driver_type_t)) 1278fce2fd6Sachartre 1288fce2fd6Sachartre typedef struct vd_driver_type { 1298fce2fd6Sachartre char name[VD_DRIVER_NAME_LEN]; /* driver name */ 1308fce2fd6Sachartre vd_driver_t type; /* driver type (disk or volume) */ 1318fce2fd6Sachartre } vd_driver_type_t; 1328fce2fd6Sachartre 1338fce2fd6Sachartre /* 1348fce2fd6Sachartre * There is no reliable way to determine if a device is representing a disk 1358fce2fd6Sachartre * or a volume, especially with pseudo devices. So we maintain a list of well 1368fce2fd6Sachartre * known drivers and the type of device they represent (either a disk or a 1378fce2fd6Sachartre * volume). 1388fce2fd6Sachartre * 1398fce2fd6Sachartre * The list can be extended by adding a "driver-type-list" entry in vds.conf 1408fce2fd6Sachartre * with the following syntax: 1418fce2fd6Sachartre * 1428fce2fd6Sachartre * driver-type-list="<driver>:<type>", ... ,"<driver>:<type>"; 1438fce2fd6Sachartre * 1448fce2fd6Sachartre * Where: 1458fce2fd6Sachartre * <driver> is the name of a driver (limited to 64 characters) 1468fce2fd6Sachartre * <type> is either the string "disk" or "volume" 1478fce2fd6Sachartre * 1488fce2fd6Sachartre * Invalid entries in "driver-type-list" will be ignored. 1498fce2fd6Sachartre * 1508fce2fd6Sachartre * For example, the following line in vds.conf: 1518fce2fd6Sachartre * 1528fce2fd6Sachartre * driver-type-list="foo:disk","bar:volume"; 1538fce2fd6Sachartre * 1548fce2fd6Sachartre * defines that "foo" is a disk driver, and driver "bar" is a volume driver. 1558fce2fd6Sachartre * 1568fce2fd6Sachartre * When a list is defined in vds.conf, it is checked before the built-in list 1578fce2fd6Sachartre * (vds_driver_types[]) so that any definition from this list can be overriden 1588fce2fd6Sachartre * using vds.conf. 1598fce2fd6Sachartre */ 1608fce2fd6Sachartre vd_driver_type_t vds_driver_types[] = { 1618fce2fd6Sachartre { "dad", VD_DRIVER_DISK }, /* Solaris */ 1628fce2fd6Sachartre { "did", VD_DRIVER_DISK }, /* Sun Cluster */ 1635b98b509Sachartre { "emcp", VD_DRIVER_DISK }, /* EMC Powerpath */ 1648fce2fd6Sachartre { "lofi", VD_DRIVER_VOLUME }, /* Solaris */ 1658fce2fd6Sachartre { "md", VD_DRIVER_VOLUME }, /* Solaris - SVM */ 1668fce2fd6Sachartre { "sd", VD_DRIVER_DISK }, /* Solaris */ 1678fce2fd6Sachartre { "ssd", VD_DRIVER_DISK }, /* Solaris */ 1688fce2fd6Sachartre { "vdc", VD_DRIVER_DISK }, /* Solaris */ 1698fce2fd6Sachartre { "vxdmp", VD_DRIVER_DISK }, /* Veritas */ 1708fce2fd6Sachartre { "vxio", VD_DRIVER_VOLUME }, /* Veritas - VxVM */ 1718fce2fd6Sachartre { "zfs", VD_DRIVER_VOLUME } /* Solaris */ 1728fce2fd6Sachartre }; 1738fce2fd6Sachartre 1741ae08745Sheppo /* Return a cpp token as a string */ 1751ae08745Sheppo #define STRINGIZE(token) #token 1761ae08745Sheppo 1771ae08745Sheppo /* 1781ae08745Sheppo * Print a message prefixed with the current function name to the message log 1791ae08745Sheppo * (and optionally to the console for verbose boots); these macros use cpp's 1801ae08745Sheppo * concatenation of string literals and C99 variable-length-argument-list 1811ae08745Sheppo * macros 1821ae08745Sheppo */ 1831ae08745Sheppo #define PRN(...) _PRN("?%s(): "__VA_ARGS__, "") 1841ae08745Sheppo #define _PRN(format, ...) \ 1851ae08745Sheppo cmn_err(CE_CONT, format"%s", __func__, __VA_ARGS__) 1861ae08745Sheppo 1871ae08745Sheppo /* Return a pointer to the "i"th vdisk dring element */ 1881ae08745Sheppo #define VD_DRING_ELEM(i) ((vd_dring_entry_t *)(void *) \ 1891ae08745Sheppo (vd->dring + (i)*vd->descriptor_size)) 1901ae08745Sheppo 1911ae08745Sheppo /* Return the virtual disk client's type as a string (for use in messages) */ 1921ae08745Sheppo #define VD_CLIENT(vd) \ 1931ae08745Sheppo (((vd)->xfer_mode == VIO_DESC_MODE) ? "in-band client" : \ 194f0ca1d9aSsb155480 (((vd)->xfer_mode == VIO_DRING_MODE_V1_0) ? "dring client" : \ 1951ae08745Sheppo (((vd)->xfer_mode == 0) ? "null client" : \ 1961ae08745Sheppo "unsupported client"))) 1971ae08745Sheppo 198690555a1Sachartre /* Read disk label from a disk on file */ 199690555a1Sachartre #define VD_FILE_LABEL_READ(vd, labelp) \ 20087a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)labelp, \ 201690555a1Sachartre 0, sizeof (struct dk_label)) 202690555a1Sachartre 203690555a1Sachartre /* Write disk label to a disk on file */ 204690555a1Sachartre #define VD_FILE_LABEL_WRITE(vd, labelp) \ 20587a7269eSachartre vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)labelp, \ 206690555a1Sachartre 0, sizeof (struct dk_label)) 207690555a1Sachartre 2082f5224aeSachartre /* Message for disk access rights reset failure */ 2092f5224aeSachartre #define VD_RESET_ACCESS_FAILURE_MSG \ 2102f5224aeSachartre "Fail to reset disk access rights for disk %s" 2112f5224aeSachartre 212445b4c2eSsb155480 /* 213445b4c2eSsb155480 * Specification of an MD node passed to the MDEG to filter any 214445b4c2eSsb155480 * 'vport' nodes that do not belong to the specified node. This 215445b4c2eSsb155480 * template is copied for each vds instance and filled in with 216445b4c2eSsb155480 * the appropriate 'cfg-handle' value before being passed to the MDEG. 217445b4c2eSsb155480 */ 218445b4c2eSsb155480 static mdeg_prop_spec_t vds_prop_template[] = { 219445b4c2eSsb155480 { MDET_PROP_STR, "name", VDS_NAME }, 220445b4c2eSsb155480 { MDET_PROP_VAL, "cfg-handle", NULL }, 221445b4c2eSsb155480 { MDET_LIST_END, NULL, NULL } 222445b4c2eSsb155480 }; 223445b4c2eSsb155480 224445b4c2eSsb155480 #define VDS_SET_MDEG_PROP_INST(specp, val) (specp)[1].ps_val = (val); 225445b4c2eSsb155480 226445b4c2eSsb155480 /* 227445b4c2eSsb155480 * Matching criteria passed to the MDEG to register interest 228445b4c2eSsb155480 * in changes to 'virtual-device-port' nodes identified by their 229445b4c2eSsb155480 * 'id' property. 230445b4c2eSsb155480 */ 231445b4c2eSsb155480 static md_prop_match_t vd_prop_match[] = { 232445b4c2eSsb155480 { MDET_PROP_VAL, VD_ID_PROP }, 233445b4c2eSsb155480 { MDET_LIST_END, NULL } 234445b4c2eSsb155480 }; 235445b4c2eSsb155480 236445b4c2eSsb155480 static mdeg_node_match_t vd_match = {"virtual-device-port", 237445b4c2eSsb155480 vd_prop_match}; 238445b4c2eSsb155480 239047ba61eSachartre /* 240047ba61eSachartre * Options for the VD_BLOCK_DEVICE_OPTS property. 241047ba61eSachartre */ 242047ba61eSachartre #define VD_OPT_RDONLY 0x1 /* read-only */ 243047ba61eSachartre #define VD_OPT_SLICE 0x2 /* single slice */ 244047ba61eSachartre #define VD_OPT_EXCLUSIVE 0x4 /* exclusive access */ 245047ba61eSachartre 246047ba61eSachartre #define VD_OPTION_NLEN 128 247047ba61eSachartre 248047ba61eSachartre typedef struct vd_option { 249047ba61eSachartre char vdo_name[VD_OPTION_NLEN]; 250047ba61eSachartre uint64_t vdo_value; 251047ba61eSachartre } vd_option_t; 252047ba61eSachartre 253047ba61eSachartre vd_option_t vd_bdev_options[] = { 254047ba61eSachartre { "ro", VD_OPT_RDONLY }, 255047ba61eSachartre { "slice", VD_OPT_SLICE }, 256047ba61eSachartre { "excl", VD_OPT_EXCLUSIVE } 257047ba61eSachartre }; 258047ba61eSachartre 2591ae08745Sheppo /* Debugging macros */ 2601ae08745Sheppo #ifdef DEBUG 2613af08d82Slm66018 2623af08d82Slm66018 static int vd_msglevel = 0; 2633af08d82Slm66018 2641ae08745Sheppo #define PR0 if (vd_msglevel > 0) PRN 2651ae08745Sheppo #define PR1 if (vd_msglevel > 1) PRN 2661ae08745Sheppo #define PR2 if (vd_msglevel > 2) PRN 2671ae08745Sheppo 2681ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) \ 2693c96341aSnarayan PR0("dst:%x op:%x st:%u nb:%lx addr:%lx ncook:%u\n", \ 2701ae08745Sheppo elem->hdr.dstate, \ 2711ae08745Sheppo elem->payload.operation, \ 2721ae08745Sheppo elem->payload.status, \ 2731ae08745Sheppo elem->payload.nbytes, \ 2741ae08745Sheppo elem->payload.addr, \ 2751ae08745Sheppo elem->payload.ncookies); 2761ae08745Sheppo 2773af08d82Slm66018 char * 2783af08d82Slm66018 vd_decode_state(int state) 2793af08d82Slm66018 { 2803af08d82Slm66018 char *str; 2813af08d82Slm66018 2823af08d82Slm66018 #define CASE_STATE(_s) case _s: str = #_s; break; 2833af08d82Slm66018 2843af08d82Slm66018 switch (state) { 2853af08d82Slm66018 CASE_STATE(VD_STATE_INIT) 2863af08d82Slm66018 CASE_STATE(VD_STATE_VER) 2873af08d82Slm66018 CASE_STATE(VD_STATE_ATTR) 2883af08d82Slm66018 CASE_STATE(VD_STATE_DRING) 2893af08d82Slm66018 CASE_STATE(VD_STATE_RDX) 2903af08d82Slm66018 CASE_STATE(VD_STATE_DATA) 2913af08d82Slm66018 default: str = "unknown"; break; 2923af08d82Slm66018 } 2933af08d82Slm66018 2943af08d82Slm66018 #undef CASE_STATE 2953af08d82Slm66018 2963af08d82Slm66018 return (str); 2973af08d82Slm66018 } 2983af08d82Slm66018 2993af08d82Slm66018 void 3003af08d82Slm66018 vd_decode_tag(vio_msg_t *msg) 3013af08d82Slm66018 { 3023af08d82Slm66018 char *tstr, *sstr, *estr; 3033af08d82Slm66018 3043af08d82Slm66018 #define CASE_TYPE(_s) case _s: tstr = #_s; break; 3053af08d82Slm66018 3063af08d82Slm66018 switch (msg->tag.vio_msgtype) { 3073af08d82Slm66018 CASE_TYPE(VIO_TYPE_CTRL) 3083af08d82Slm66018 CASE_TYPE(VIO_TYPE_DATA) 3093af08d82Slm66018 CASE_TYPE(VIO_TYPE_ERR) 3103af08d82Slm66018 default: tstr = "unknown"; break; 3113af08d82Slm66018 } 3123af08d82Slm66018 3133af08d82Slm66018 #undef CASE_TYPE 3143af08d82Slm66018 3153af08d82Slm66018 #define CASE_SUBTYPE(_s) case _s: sstr = #_s; break; 3163af08d82Slm66018 3173af08d82Slm66018 switch (msg->tag.vio_subtype) { 3183af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_INFO) 3193af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_ACK) 3203af08d82Slm66018 CASE_SUBTYPE(VIO_SUBTYPE_NACK) 3213af08d82Slm66018 default: sstr = "unknown"; break; 3223af08d82Slm66018 } 3233af08d82Slm66018 3243af08d82Slm66018 #undef CASE_SUBTYPE 3253af08d82Slm66018 3263af08d82Slm66018 #define CASE_ENV(_s) case _s: estr = #_s; break; 3273af08d82Slm66018 3283af08d82Slm66018 switch (msg->tag.vio_subtype_env) { 3293af08d82Slm66018 CASE_ENV(VIO_VER_INFO) 3303af08d82Slm66018 CASE_ENV(VIO_ATTR_INFO) 3313af08d82Slm66018 CASE_ENV(VIO_DRING_REG) 3323af08d82Slm66018 CASE_ENV(VIO_DRING_UNREG) 3333af08d82Slm66018 CASE_ENV(VIO_RDX) 3343af08d82Slm66018 CASE_ENV(VIO_PKT_DATA) 3353af08d82Slm66018 CASE_ENV(VIO_DESC_DATA) 3363af08d82Slm66018 CASE_ENV(VIO_DRING_DATA) 3373af08d82Slm66018 default: estr = "unknown"; break; 3383af08d82Slm66018 } 3393af08d82Slm66018 3403af08d82Slm66018 #undef CASE_ENV 3413af08d82Slm66018 3423af08d82Slm66018 PR1("(%x/%x/%x) message : (%s/%s/%s)", 3433af08d82Slm66018 msg->tag.vio_msgtype, msg->tag.vio_subtype, 3443af08d82Slm66018 msg->tag.vio_subtype_env, tstr, sstr, estr); 3453af08d82Slm66018 } 3463af08d82Slm66018 3471ae08745Sheppo #else /* !DEBUG */ 3483af08d82Slm66018 3491ae08745Sheppo #define PR0(...) 3501ae08745Sheppo #define PR1(...) 3511ae08745Sheppo #define PR2(...) 3521ae08745Sheppo 3531ae08745Sheppo #define VD_DUMP_DRING_ELEM(elem) 3541ae08745Sheppo 3553af08d82Slm66018 #define vd_decode_state(_s) (NULL) 3563af08d82Slm66018 #define vd_decode_tag(_s) (NULL) 3573af08d82Slm66018 3581ae08745Sheppo #endif /* DEBUG */ 3591ae08745Sheppo 3601ae08745Sheppo 361d10e4ef2Snarayan /* 362d10e4ef2Snarayan * Soft state structure for a vds instance 363d10e4ef2Snarayan */ 3641ae08745Sheppo typedef struct vds { 3651ae08745Sheppo uint_t initialized; /* driver inst initialization flags */ 3661ae08745Sheppo dev_info_t *dip; /* driver inst devinfo pointer */ 3671ae08745Sheppo ldi_ident_t ldi_ident; /* driver's identifier for LDI */ 3681ae08745Sheppo mod_hash_t *vd_table; /* table of virtual disks served */ 369445b4c2eSsb155480 mdeg_node_spec_t *ispecp; /* mdeg node specification */ 3701ae08745Sheppo mdeg_handle_t mdeg; /* handle for MDEG operations */ 3718fce2fd6Sachartre vd_driver_type_t *driver_types; /* extra driver types (from vds.conf) */ 3728fce2fd6Sachartre int num_drivers; /* num of extra driver types */ 3731ae08745Sheppo } vds_t; 3741ae08745Sheppo 375d10e4ef2Snarayan /* 376d10e4ef2Snarayan * Types of descriptor-processing tasks 377d10e4ef2Snarayan */ 378d10e4ef2Snarayan typedef enum vd_task_type { 379d10e4ef2Snarayan VD_NONFINAL_RANGE_TASK, /* task for intermediate descriptor in range */ 380d10e4ef2Snarayan VD_FINAL_RANGE_TASK, /* task for last in a range of descriptors */ 381d10e4ef2Snarayan } vd_task_type_t; 382d10e4ef2Snarayan 383d10e4ef2Snarayan /* 384d10e4ef2Snarayan * Structure describing the task for processing a descriptor 385d10e4ef2Snarayan */ 386d10e4ef2Snarayan typedef struct vd_task { 387d10e4ef2Snarayan struct vd *vd; /* vd instance task is for */ 388d10e4ef2Snarayan vd_task_type_t type; /* type of descriptor task */ 389d10e4ef2Snarayan int index; /* dring elem index for task */ 390d10e4ef2Snarayan vio_msg_t *msg; /* VIO message task is for */ 391d10e4ef2Snarayan size_t msglen; /* length of message content */ 392d10e4ef2Snarayan vd_dring_payload_t *request; /* request task will perform */ 393d10e4ef2Snarayan struct buf buf; /* buf(9s) for I/O request */ 3944bac2208Snarayan ldc_mem_handle_t mhdl; /* task memory handle */ 395205eeb1aSlm66018 int status; /* status of processing task */ 396205eeb1aSlm66018 int (*completef)(struct vd_task *task); /* completion func ptr */ 397d10e4ef2Snarayan } vd_task_t; 398d10e4ef2Snarayan 399d10e4ef2Snarayan /* 400d10e4ef2Snarayan * Soft state structure for a virtual disk instance 401d10e4ef2Snarayan */ 4021ae08745Sheppo typedef struct vd { 4031ae08745Sheppo uint_t initialized; /* vdisk initialization flags */ 40417cadca8Slm66018 uint64_t operations; /* bitmask of VD_OPs exported */ 40517cadca8Slm66018 vio_ver_t version; /* ver negotiated with client */ 4061ae08745Sheppo vds_t *vds; /* server for this vdisk */ 407d10e4ef2Snarayan ddi_taskq_t *startq; /* queue for I/O start tasks */ 408d10e4ef2Snarayan ddi_taskq_t *completionq; /* queue for completion tasks */ 4091ae08745Sheppo ldi_handle_t ldi_handle[V_NUMPAR]; /* LDI slice handles */ 4103c96341aSnarayan char device_path[MAXPATHLEN + 1]; /* vdisk device */ 4111ae08745Sheppo dev_t dev[V_NUMPAR]; /* dev numbers for slices */ 412047ba61eSachartre int open_flags; /* open flags */ 413bae9e67eSachartre uint_t nslices; /* number of slices we export */ 4141ae08745Sheppo size_t vdisk_size; /* number of blocks in vdisk */ 41517cadca8Slm66018 size_t vdisk_block_size; /* size of each vdisk block */ 4161ae08745Sheppo vd_disk_type_t vdisk_type; /* slice or entire disk */ 4174bac2208Snarayan vd_disk_label_t vdisk_label; /* EFI or VTOC label */ 41817cadca8Slm66018 vd_media_t vdisk_media; /* media type of backing dev. */ 41917cadca8Slm66018 boolean_t is_atapi_dev; /* Is this an IDE CD-ROM dev? */ 420e1ebb9ecSlm66018 ushort_t max_xfer_sz; /* max xfer size in DEV_BSIZE */ 42117cadca8Slm66018 size_t block_size; /* blk size of actual device */ 4228fce2fd6Sachartre boolean_t volume; /* is vDisk backed by volume */ 42317cadca8Slm66018 boolean_t file; /* is vDisk backed by a file? */ 4242f5224aeSachartre boolean_t scsi; /* is vDisk backed by scsi? */ 4253c96341aSnarayan vnode_t *file_vnode; /* file vnode */ 4263c96341aSnarayan size_t file_size; /* file size */ 42787a7269eSachartre ddi_devid_t file_devid; /* devid for disk image */ 428edcc0754Sachartre int efi_reserved; /* EFI reserved slice */ 429bae9e67eSachartre caddr_t flabel; /* fake label for slice type */ 430bae9e67eSachartre uint_t flabel_size; /* fake label size */ 431bae9e67eSachartre uint_t flabel_limit; /* limit of the fake label */ 4321ae08745Sheppo struct dk_geom dk_geom; /* synthetic for slice type */ 4331ae08745Sheppo struct vtoc vtoc; /* synthetic for slice type */ 434edcc0754Sachartre vd_slice_t slices[VD_MAXPART]; /* logical partitions */ 4352f5224aeSachartre boolean_t ownership; /* disk ownership status */ 4361ae08745Sheppo ldc_status_t ldc_state; /* LDC connection state */ 4371ae08745Sheppo ldc_handle_t ldc_handle; /* handle for LDC comm */ 4381ae08745Sheppo size_t max_msglen; /* largest LDC message len */ 4391ae08745Sheppo vd_state_t state; /* client handshake state */ 4401ae08745Sheppo uint8_t xfer_mode; /* transfer mode with client */ 4411ae08745Sheppo uint32_t sid; /* client's session ID */ 4421ae08745Sheppo uint64_t seq_num; /* message sequence number */ 4431ae08745Sheppo uint64_t dring_ident; /* identifier of dring */ 4441ae08745Sheppo ldc_dring_handle_t dring_handle; /* handle for dring ops */ 4451ae08745Sheppo uint32_t descriptor_size; /* num bytes in desc */ 4461ae08745Sheppo uint32_t dring_len; /* number of dring elements */ 447bbfa0259Sha137994 uint8_t dring_mtype; /* dring mem map type */ 4481ae08745Sheppo caddr_t dring; /* address of dring */ 4493af08d82Slm66018 caddr_t vio_msgp; /* vio msg staging buffer */ 450d10e4ef2Snarayan vd_task_t inband_task; /* task for inband descriptor */ 451d10e4ef2Snarayan vd_task_t *dring_task; /* tasks dring elements */ 452d10e4ef2Snarayan 453d10e4ef2Snarayan kmutex_t lock; /* protects variables below */ 454d10e4ef2Snarayan boolean_t enabled; /* is vdisk enabled? */ 455d10e4ef2Snarayan boolean_t reset_state; /* reset connection state? */ 456d10e4ef2Snarayan boolean_t reset_ldc; /* reset LDC channel? */ 4571ae08745Sheppo } vd_t; 4581ae08745Sheppo 459bae9e67eSachartre /* 460bae9e67eSachartre * Macros to manipulate the fake label (flabel) for single slice disks. 461bae9e67eSachartre * 462bae9e67eSachartre * If we fake a VTOC label then the fake label consists of only one block 463bae9e67eSachartre * containing the VTOC label (struct dk_label). 464bae9e67eSachartre * 465bae9e67eSachartre * If we fake an EFI label then the fake label consists of a blank block 466bae9e67eSachartre * followed by a GPT (efi_gpt_t) and a GPE (efi_gpe_t). 467bae9e67eSachartre * 468bae9e67eSachartre */ 469bae9e67eSachartre #define VD_LABEL_VTOC_SIZE \ 470bae9e67eSachartre P2ROUNDUP(sizeof (struct dk_label), DEV_BSIZE) 471bae9e67eSachartre 472bae9e67eSachartre #define VD_LABEL_EFI_SIZE \ 473bae9e67eSachartre P2ROUNDUP(DEV_BSIZE + sizeof (efi_gpt_t) + \ 474bae9e67eSachartre sizeof (efi_gpe_t) * VD_MAXPART, DEV_BSIZE) 475bae9e67eSachartre 476bae9e67eSachartre #define VD_LABEL_VTOC(vd) \ 477bae9e67eSachartre ((struct dk_label *)((vd)->flabel)) 478bae9e67eSachartre 479bae9e67eSachartre #define VD_LABEL_EFI_GPT(vd) \ 480bae9e67eSachartre ((efi_gpt_t *)((vd)->flabel + DEV_BSIZE)) 481bae9e67eSachartre #define VD_LABEL_EFI_GPE(vd) \ 482bae9e67eSachartre ((efi_gpe_t *)((vd)->flabel + DEV_BSIZE + sizeof (efi_gpt_t))) 483bae9e67eSachartre 484bae9e67eSachartre 4851ae08745Sheppo typedef struct vds_operation { 4863af08d82Slm66018 char *namep; 4871ae08745Sheppo uint8_t operation; 488d10e4ef2Snarayan int (*start)(vd_task_t *task); 489205eeb1aSlm66018 int (*complete)(vd_task_t *task); 4901ae08745Sheppo } vds_operation_t; 4911ae08745Sheppo 4920a55fbb7Slm66018 typedef struct vd_ioctl { 4930a55fbb7Slm66018 uint8_t operation; /* vdisk operation */ 4940a55fbb7Slm66018 const char *operation_name; /* vdisk operation name */ 4950a55fbb7Slm66018 size_t nbytes; /* size of operation buffer */ 4960a55fbb7Slm66018 int cmd; /* corresponding ioctl cmd */ 4970a55fbb7Slm66018 const char *cmd_name; /* ioctl cmd name */ 4980a55fbb7Slm66018 void *arg; /* ioctl cmd argument */ 4990a55fbb7Slm66018 /* convert input vd_buf to output ioctl_arg */ 5002f5224aeSachartre int (*copyin)(void *vd_buf, size_t, void *ioctl_arg); 5010a55fbb7Slm66018 /* convert input ioctl_arg to output vd_buf */ 5020a55fbb7Slm66018 void (*copyout)(void *ioctl_arg, void *vd_buf); 503047ba61eSachartre /* write is true if the operation writes any data to the backend */ 504047ba61eSachartre boolean_t write; 5050a55fbb7Slm66018 } vd_ioctl_t; 5060a55fbb7Slm66018 5070a55fbb7Slm66018 /* Define trivial copyin/copyout conversion function flag */ 5082f5224aeSachartre #define VD_IDENTITY_IN ((int (*)(void *, size_t, void *))-1) 5092f5224aeSachartre #define VD_IDENTITY_OUT ((void (*)(void *, void *))-1) 5101ae08745Sheppo 5111ae08745Sheppo 5123c96341aSnarayan static int vds_ldc_retries = VDS_RETRIES; 5133af08d82Slm66018 static int vds_ldc_delay = VDS_LDC_DELAY; 5143c96341aSnarayan static int vds_dev_retries = VDS_RETRIES; 5153c96341aSnarayan static int vds_dev_delay = VDS_DEV_DELAY; 5161ae08745Sheppo static void *vds_state; 5171ae08745Sheppo 518eba0cb4eSachartre static uint_t vd_file_write_flags = VD_FILE_WRITE_FLAGS; 519eba0cb4eSachartre 52087a7269eSachartre static short vd_scsi_rdwr_timeout = VD_SCSI_RDWR_TIMEOUT; 5212f5224aeSachartre static int vd_scsi_debug = USCSI_SILENT; 5222f5224aeSachartre 5232f5224aeSachartre /* 5242f5224aeSachartre * Tunable to define the behavior of the service domain if the vdisk server 5252f5224aeSachartre * fails to reset disk exclusive access when a LDC channel is reset. When a 5262f5224aeSachartre * LDC channel is reset the vdisk server will try to reset disk exclusive 5272f5224aeSachartre * access by releasing any SCSI-2 reservation or resetting the disk. If these 5282f5224aeSachartre * actions fail then the default behavior (vd_reset_access_failure = 0) is to 5292f5224aeSachartre * print a warning message. This default behavior can be changed by setting 5302f5224aeSachartre * the vd_reset_access_failure variable to A_REBOOT (= 0x1) and that will 5312f5224aeSachartre * cause the service domain to reboot, or A_DUMP (= 0x5) and that will cause 5322f5224aeSachartre * the service domain to panic. In both cases, the reset of the service domain 5332f5224aeSachartre * should trigger a reset SCSI buses and hopefully clear any SCSI-2 reservation. 5342f5224aeSachartre */ 5352f5224aeSachartre static int vd_reset_access_failure = 0; 5362f5224aeSachartre 5372f5224aeSachartre /* 5382f5224aeSachartre * Tunable for backward compatibility. When this variable is set to B_TRUE, 5392f5224aeSachartre * all disk volumes (ZFS, SVM, VxvM volumes) will be exported as single 5402f5224aeSachartre * slice disks whether or not they have the "slice" option set. This is 5412f5224aeSachartre * to provide a simple backward compatibility mechanism when upgrading 5422f5224aeSachartre * the vds driver and using a domain configuration created before the 5432f5224aeSachartre * "slice" option was available. 5442f5224aeSachartre */ 5452f5224aeSachartre static boolean_t vd_volume_force_slice = B_FALSE; 54687a7269eSachartre 5470a55fbb7Slm66018 /* 54866cfcfbeSachartre * The label of disk images created with some earlier versions of the virtual 54966cfcfbeSachartre * disk software is not entirely correct and have an incorrect v_sanity field 55066cfcfbeSachartre * (usually 0) instead of VTOC_SANE. This creates a compatibility problem with 55166cfcfbeSachartre * these images because we are now validating that the disk label (and the 55266cfcfbeSachartre * sanity) is correct when a disk image is opened. 55366cfcfbeSachartre * 55466cfcfbeSachartre * This tunable is set to false to not validate the sanity field and ensure 55566cfcfbeSachartre * compatibility. If the tunable is set to true, we will do a strict checking 55666cfcfbeSachartre * of the sanity but this can create compatibility problems with old disk 55766cfcfbeSachartre * images. 55866cfcfbeSachartre */ 55966cfcfbeSachartre static boolean_t vd_file_validate_sanity = B_FALSE; 56066cfcfbeSachartre 56166cfcfbeSachartre /* 562bbfa0259Sha137994 * Enables the use of LDC_DIRECT_MAP when mapping in imported descriptor rings. 563bbfa0259Sha137994 */ 564bbfa0259Sha137994 static boolean_t vd_direct_mapped_drings = B_TRUE; 565bbfa0259Sha137994 566bbfa0259Sha137994 /* 567bae9e67eSachartre * When a backend is exported as a single-slice disk then we entirely fake 568bae9e67eSachartre * its disk label. So it can be exported either with a VTOC label or with 569bae9e67eSachartre * an EFI label. If vd_slice_label is set to VD_DISK_LABEL_VTOC then all 570bae9e67eSachartre * single-slice disks will be exported with a VTOC label; and if it is set 571bae9e67eSachartre * to VD_DISK_LABEL_EFI then all single-slice disks will be exported with 572bae9e67eSachartre * an EFI label. 573bae9e67eSachartre * 574bae9e67eSachartre * If vd_slice_label is set to VD_DISK_LABEL_UNK and the backend is a disk 575bae9e67eSachartre * or volume device then it will be exported with the same type of label as 576bae9e67eSachartre * defined on the device. Otherwise if the backend is a file then it will 577bae9e67eSachartre * exported with the disk label type set in the vd_file_slice_label variable. 578bae9e67eSachartre * 579bae9e67eSachartre * Note that if the backend size is greater than 1TB then it will always be 580bae9e67eSachartre * exported with an EFI label no matter what the setting is. 581bae9e67eSachartre */ 582bae9e67eSachartre static vd_disk_label_t vd_slice_label = VD_DISK_LABEL_UNK; 583bae9e67eSachartre 584bae9e67eSachartre static vd_disk_label_t vd_file_slice_label = VD_DISK_LABEL_VTOC; 585bae9e67eSachartre 586bae9e67eSachartre /* 587bae9e67eSachartre * Tunable for backward compatibility. If this variable is set to B_TRUE then 588bae9e67eSachartre * single-slice disks are exported as disks with only one slice instead of 589bae9e67eSachartre * faking a complete disk partitioning. 590bae9e67eSachartre */ 591bae9e67eSachartre static boolean_t vd_slice_single_slice = B_FALSE; 592bae9e67eSachartre 593bae9e67eSachartre /* 5940a55fbb7Slm66018 * Supported protocol version pairs, from highest (newest) to lowest (oldest) 5950a55fbb7Slm66018 * 5960a55fbb7Slm66018 * Each supported major version should appear only once, paired with (and only 5970a55fbb7Slm66018 * with) its highest supported minor version number (as the protocol requires 5980a55fbb7Slm66018 * supporting all lower minor version numbers as well) 5990a55fbb7Slm66018 */ 60017cadca8Slm66018 static const vio_ver_t vds_version[] = {{1, 1}}; 6010a55fbb7Slm66018 static const size_t vds_num_versions = 6020a55fbb7Slm66018 sizeof (vds_version)/sizeof (vds_version[0]); 6030a55fbb7Slm66018 6043af08d82Slm66018 static void vd_free_dring_task(vd_t *vdp); 6053c96341aSnarayan static int vd_setup_vd(vd_t *vd); 606047ba61eSachartre static int vd_setup_single_slice_disk(vd_t *vd); 6072f5224aeSachartre static int vd_setup_mediainfo(vd_t *vd); 6083c96341aSnarayan static boolean_t vd_enabled(vd_t *vd); 60978fcd0a1Sachartre static ushort_t vd_lbl2cksum(struct dk_label *label); 61078fcd0a1Sachartre static int vd_file_validate_geometry(vd_t *vd); 61117cadca8Slm66018 static boolean_t vd_file_is_iso_image(vd_t *vd); 61217cadca8Slm66018 static void vd_set_exported_operations(vd_t *vd); 6132f5224aeSachartre static void vd_reset_access(vd_t *vd); 614edcc0754Sachartre static int vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg); 615edcc0754Sachartre static int vds_efi_alloc_and_read(vd_t *, efi_gpt_t **, efi_gpe_t **); 616edcc0754Sachartre static void vds_efi_free(vd_t *, efi_gpt_t *, efi_gpe_t *); 6178fce2fd6Sachartre static void vds_driver_types_free(vds_t *vds); 618bae9e67eSachartre static void vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom, 619bae9e67eSachartre struct dk_label *label); 620bae9e67eSachartre static void vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc, 621bae9e67eSachartre struct dk_geom *geom); 622bae9e67eSachartre static boolean_t vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom); 623bae9e67eSachartre static boolean_t vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc); 624bae9e67eSachartre 625bae9e67eSachartre extern int is_pseudo_device(dev_info_t *); 626bae9e67eSachartre 627bae9e67eSachartre /* 628bae9e67eSachartre * Function: 629bae9e67eSachartre * vd_get_readable_size 630bae9e67eSachartre * 631bae9e67eSachartre * Description: 632bae9e67eSachartre * Convert a given size in bytes to a human readable format in 633bae9e67eSachartre * kilobytes, megabytes, gigabytes or terabytes. 634bae9e67eSachartre * 635bae9e67eSachartre * Parameters: 636bae9e67eSachartre * full_size - the size to convert in bytes. 637bae9e67eSachartre * size - the converted size. 638bae9e67eSachartre * unit - the unit of the converted size: 'K' (kilobyte), 639bae9e67eSachartre * 'M' (Megabyte), 'G' (Gigabyte), 'T' (Terabyte). 640bae9e67eSachartre * 641bae9e67eSachartre * Return Code: 642bae9e67eSachartre * none 643bae9e67eSachartre */ 644bae9e67eSachartre void 645bae9e67eSachartre vd_get_readable_size(size_t full_size, size_t *size, char *unit) 646bae9e67eSachartre { 647bae9e67eSachartre if (full_size < (1ULL << 20)) { 648bae9e67eSachartre *size = full_size >> 10; 649bae9e67eSachartre *unit = 'K'; /* Kilobyte */ 650bae9e67eSachartre } else if (full_size < (1ULL << 30)) { 651bae9e67eSachartre *size = full_size >> 20; 652bae9e67eSachartre *unit = 'M'; /* Megabyte */ 653bae9e67eSachartre } else if (full_size < (1ULL << 40)) { 654bae9e67eSachartre *size = full_size >> 30; 655bae9e67eSachartre *unit = 'G'; /* Gigabyte */ 656bae9e67eSachartre } else { 657bae9e67eSachartre *size = full_size >> 40; 658bae9e67eSachartre *unit = 'T'; /* Terabyte */ 659bae9e67eSachartre } 660bae9e67eSachartre } 661047ba61eSachartre 662690555a1Sachartre /* 663690555a1Sachartre * Function: 664690555a1Sachartre * vd_file_rw 665690555a1Sachartre * 666690555a1Sachartre * Description: 667690555a1Sachartre * Read or write to a disk on file. 668690555a1Sachartre * 669690555a1Sachartre * Parameters: 670690555a1Sachartre * vd - disk on which the operation is performed. 671690555a1Sachartre * slice - slice on which the operation is performed, 67287a7269eSachartre * VD_SLICE_NONE indicates that the operation 67387a7269eSachartre * is done using an absolute disk offset. 674690555a1Sachartre * operation - operation to execute: read (VD_OP_BREAD) or 675690555a1Sachartre * write (VD_OP_BWRITE). 676690555a1Sachartre * data - buffer where data are read to or written from. 677690555a1Sachartre * blk - starting block for the operation. 678690555a1Sachartre * len - number of bytes to read or write. 679690555a1Sachartre * 680690555a1Sachartre * Return Code: 681690555a1Sachartre * n >= 0 - success, n indicates the number of bytes read 682690555a1Sachartre * or written. 683690555a1Sachartre * -1 - error. 684690555a1Sachartre */ 685690555a1Sachartre static ssize_t 686690555a1Sachartre vd_file_rw(vd_t *vd, int slice, int operation, caddr_t data, size_t blk, 687690555a1Sachartre size_t len) 688690555a1Sachartre { 689690555a1Sachartre caddr_t maddr; 690690555a1Sachartre size_t offset, maxlen, moffset, mlen, n; 691690555a1Sachartre uint_t smflags; 692690555a1Sachartre enum seg_rw srw; 693690555a1Sachartre 694690555a1Sachartre ASSERT(vd->file); 695690555a1Sachartre ASSERT(len > 0); 696690555a1Sachartre 697047ba61eSachartre /* 698047ba61eSachartre * If a file is exported as a slice then we don't care about the vtoc. 699047ba61eSachartre * In that case, the vtoc is a fake mainly to make newfs happy and we 700047ba61eSachartre * handle any I/O as a raw disk access so that we can have access to the 701047ba61eSachartre * entire backend. 702047ba61eSachartre */ 703047ba61eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE || slice == VD_SLICE_NONE) { 704690555a1Sachartre /* raw disk access */ 705690555a1Sachartre offset = blk * DEV_BSIZE; 706bae9e67eSachartre if (offset >= vd->file_size) { 707bae9e67eSachartre /* offset past the end of the disk */ 708bae9e67eSachartre PR0("offset (0x%lx) >= fsize (0x%lx)", 709bae9e67eSachartre offset, vd->file_size); 710bae9e67eSachartre return (0); 711bae9e67eSachartre } 712bae9e67eSachartre maxlen = vd->file_size - offset; 713690555a1Sachartre } else { 714690555a1Sachartre ASSERT(slice >= 0 && slice < V_NUMPAR); 71578fcd0a1Sachartre 71617cadca8Slm66018 /* 71717cadca8Slm66018 * v1.0 vDisk clients depended on the server not verifying 71817cadca8Slm66018 * the label of a unformatted disk. This "feature" is 71917cadca8Slm66018 * maintained for backward compatibility but all versions 72017cadca8Slm66018 * from v1.1 onwards must do the right thing. 72117cadca8Slm66018 */ 72278fcd0a1Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK && 723edcc0754Sachartre vio_ver_is_supported(vd->version, 1, 1)) { 724edcc0754Sachartre (void) vd_file_validate_geometry(vd); 725edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 726edcc0754Sachartre PR0("Unknown disk label, can't do I/O " 727edcc0754Sachartre "from slice %d", slice); 72878fcd0a1Sachartre return (-1); 72978fcd0a1Sachartre } 730edcc0754Sachartre } 73178fcd0a1Sachartre 732edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 733edcc0754Sachartre ASSERT(vd->vtoc.v_sectorsz == DEV_BSIZE); 734edcc0754Sachartre } else { 735edcc0754Sachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_EFI); 736edcc0754Sachartre ASSERT(vd->vdisk_block_size == DEV_BSIZE); 737edcc0754Sachartre } 738edcc0754Sachartre 739edcc0754Sachartre if (blk >= vd->slices[slice].nblocks) { 740690555a1Sachartre /* address past the end of the slice */ 741bae9e67eSachartre PR0("req_addr (0x%lx) >= psize (0x%lx)", 742edcc0754Sachartre blk, vd->slices[slice].nblocks); 743690555a1Sachartre return (0); 744690555a1Sachartre } 745690555a1Sachartre 746edcc0754Sachartre offset = (vd->slices[slice].start + blk) * DEV_BSIZE; 747bae9e67eSachartre maxlen = (vd->slices[slice].nblocks - blk) * DEV_BSIZE; 748bae9e67eSachartre } 749690555a1Sachartre 750690555a1Sachartre /* 751690555a1Sachartre * If the requested size is greater than the size 752690555a1Sachartre * of the partition, truncate the read/write. 753690555a1Sachartre */ 754690555a1Sachartre if (len > maxlen) { 755690555a1Sachartre PR0("I/O size truncated to %lu bytes from %lu bytes", 756690555a1Sachartre maxlen, len); 757690555a1Sachartre len = maxlen; 758690555a1Sachartre } 759690555a1Sachartre 760690555a1Sachartre /* 761690555a1Sachartre * We have to ensure that we are reading/writing into the mmap 762690555a1Sachartre * range. If we have a partial disk image (e.g. an image of 763690555a1Sachartre * s0 instead s2) the system can try to access slices that 764690555a1Sachartre * are not included into the disk image. 765690555a1Sachartre */ 766edcc0754Sachartre if ((offset + len) > vd->file_size) { 767edcc0754Sachartre PR0("offset + nbytes (0x%lx + 0x%lx) > " 768690555a1Sachartre "file_size (0x%lx)", offset, len, vd->file_size); 769690555a1Sachartre return (-1); 770690555a1Sachartre } 771690555a1Sachartre 772690555a1Sachartre srw = (operation == VD_OP_BREAD)? S_READ : S_WRITE; 773eba0cb4eSachartre smflags = (operation == VD_OP_BREAD)? 0 : 774eba0cb4eSachartre (SM_WRITE | vd_file_write_flags); 775690555a1Sachartre n = len; 776690555a1Sachartre 777690555a1Sachartre do { 778690555a1Sachartre /* 779690555a1Sachartre * segmap_getmapflt() returns a MAXBSIZE chunk which is 780690555a1Sachartre * MAXBSIZE aligned. 781690555a1Sachartre */ 782690555a1Sachartre moffset = offset & MAXBOFFSET; 783690555a1Sachartre mlen = MIN(MAXBSIZE - moffset, n); 784690555a1Sachartre maddr = segmap_getmapflt(segkmap, vd->file_vnode, offset, 785690555a1Sachartre mlen, 1, srw); 786690555a1Sachartre /* 787690555a1Sachartre * Fault in the pages so we can check for error and ensure 788690555a1Sachartre * that we can safely used the mapped address. 789690555a1Sachartre */ 790690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 791690555a1Sachartre F_SOFTLOCK, srw) != 0) { 792690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 793690555a1Sachartre return (-1); 794690555a1Sachartre } 795690555a1Sachartre 796690555a1Sachartre if (operation == VD_OP_BREAD) 797690555a1Sachartre bcopy(maddr + moffset, data, mlen); 798690555a1Sachartre else 799690555a1Sachartre bcopy(data, maddr + moffset, mlen); 800690555a1Sachartre 801690555a1Sachartre if (segmap_fault(kas.a_hat, segkmap, maddr, mlen, 802690555a1Sachartre F_SOFTUNLOCK, srw) != 0) { 803690555a1Sachartre (void) segmap_release(segkmap, maddr, 0); 804690555a1Sachartre return (-1); 805690555a1Sachartre } 806690555a1Sachartre if (segmap_release(segkmap, maddr, smflags) != 0) 807690555a1Sachartre return (-1); 808690555a1Sachartre n -= mlen; 809690555a1Sachartre offset += mlen; 810690555a1Sachartre data += mlen; 811690555a1Sachartre 812690555a1Sachartre } while (n > 0); 813690555a1Sachartre 814690555a1Sachartre return (len); 815690555a1Sachartre } 816690555a1Sachartre 81787a7269eSachartre /* 81887a7269eSachartre * Function: 819bae9e67eSachartre * vd_build_default_label 82078fcd0a1Sachartre * 82178fcd0a1Sachartre * Description: 822bae9e67eSachartre * Return a default label for a given disk size. This is used when the disk 82378fcd0a1Sachartre * does not have a valid VTOC so that the user can get a valid default 82417cadca8Slm66018 * configuration. The default label has all slice sizes set to 0 (except 82578fcd0a1Sachartre * slice 2 which is the entire disk) to force the user to write a valid 82678fcd0a1Sachartre * label onto the disk image. 82778fcd0a1Sachartre * 82878fcd0a1Sachartre * Parameters: 829bae9e67eSachartre * disk_size - the disk size in bytes 83078fcd0a1Sachartre * label - the returned default label. 83178fcd0a1Sachartre * 83278fcd0a1Sachartre * Return Code: 83378fcd0a1Sachartre * none. 83478fcd0a1Sachartre */ 83578fcd0a1Sachartre static void 836bae9e67eSachartre vd_build_default_label(size_t disk_size, struct dk_label *label) 83778fcd0a1Sachartre { 83878fcd0a1Sachartre size_t size; 839bae9e67eSachartre char unit; 840edcc0754Sachartre 841edcc0754Sachartre bzero(label, sizeof (struct dk_label)); 84278fcd0a1Sachartre 84378fcd0a1Sachartre /* 84478fcd0a1Sachartre * We must have a resonable number of cylinders and sectors so 84578fcd0a1Sachartre * that newfs can run using default values. 84678fcd0a1Sachartre * 84778fcd0a1Sachartre * if (disk_size < 2MB) 84878fcd0a1Sachartre * phys_cylinders = disk_size / 100K 849f745d6a3Sachartre * else if (disk_size >= 18.75GB) 850f745d6a3Sachartre * phys_cylinders = 65535 (maximum number of cylinders) 85178fcd0a1Sachartre * else 85278fcd0a1Sachartre * phys_cylinders = disk_size / 300K 85378fcd0a1Sachartre * 85478fcd0a1Sachartre * phys_cylinders = (phys_cylinders == 0) ? 1 : phys_cylinders 85578fcd0a1Sachartre * alt_cylinders = (phys_cylinders > 2) ? 2 : 0; 85678fcd0a1Sachartre * data_cylinders = phys_cylinders - alt_cylinders 85778fcd0a1Sachartre * 85878fcd0a1Sachartre * sectors = disk_size / (phys_cylinders * blk_size) 85978fcd0a1Sachartre * 860f745d6a3Sachartre * The disk size test is an attempt to not have too few cylinders 861f745d6a3Sachartre * for a small disk image, or so many on a big disk image that you 862f745d6a3Sachartre * waste space for backup superblocks or cylinder group structures. 86378fcd0a1Sachartre */ 864bae9e67eSachartre if (disk_size < (2 * 1024 * 1024)) 865bae9e67eSachartre label->dkl_pcyl = disk_size / (100 * 1024); 866f745d6a3Sachartre else if (disk_size >= (UINT16_MAX * 300 * 1024ULL)) 867f745d6a3Sachartre label->dkl_pcyl = UINT16_MAX; 86878fcd0a1Sachartre else 869bae9e67eSachartre label->dkl_pcyl = disk_size / (300 * 1024); 87078fcd0a1Sachartre 87178fcd0a1Sachartre if (label->dkl_pcyl == 0) 87278fcd0a1Sachartre label->dkl_pcyl = 1; 87378fcd0a1Sachartre 874047ba61eSachartre label->dkl_acyl = 0; 875047ba61eSachartre 87678fcd0a1Sachartre if (label->dkl_pcyl > 2) 87778fcd0a1Sachartre label->dkl_acyl = 2; 87878fcd0a1Sachartre 879bae9e67eSachartre label->dkl_nsect = disk_size / (DEV_BSIZE * label->dkl_pcyl); 88078fcd0a1Sachartre label->dkl_ncyl = label->dkl_pcyl - label->dkl_acyl; 88178fcd0a1Sachartre label->dkl_nhead = 1; 88278fcd0a1Sachartre label->dkl_write_reinstruct = 0; 88378fcd0a1Sachartre label->dkl_read_reinstruct = 0; 88478fcd0a1Sachartre label->dkl_rpm = 7200; 88578fcd0a1Sachartre label->dkl_apc = 0; 88678fcd0a1Sachartre label->dkl_intrlv = 0; 88778fcd0a1Sachartre 888bae9e67eSachartre PR0("requested disk size: %ld bytes\n", disk_size); 88978fcd0a1Sachartre PR0("setup: ncyl=%d nhead=%d nsec=%d\n", label->dkl_pcyl, 89078fcd0a1Sachartre label->dkl_nhead, label->dkl_nsect); 89178fcd0a1Sachartre PR0("provided disk size: %ld bytes\n", (uint64_t) 89278fcd0a1Sachartre (label->dkl_pcyl * label->dkl_nhead * 89378fcd0a1Sachartre label->dkl_nsect * DEV_BSIZE)); 89478fcd0a1Sachartre 895bae9e67eSachartre vd_get_readable_size(disk_size, &size, &unit); 89678fcd0a1Sachartre 89778fcd0a1Sachartre /* 89878fcd0a1Sachartre * We must have a correct label name otherwise format(1m) will 89978fcd0a1Sachartre * not recognized the disk as labeled. 90078fcd0a1Sachartre */ 90178fcd0a1Sachartre (void) snprintf(label->dkl_asciilabel, LEN_DKL_ASCII, 90278fcd0a1Sachartre "SUN-DiskImage-%ld%cB cyl %d alt %d hd %d sec %d", 903bae9e67eSachartre size, unit, 90478fcd0a1Sachartre label->dkl_ncyl, label->dkl_acyl, label->dkl_nhead, 90578fcd0a1Sachartre label->dkl_nsect); 90678fcd0a1Sachartre 90778fcd0a1Sachartre /* default VTOC */ 90878fcd0a1Sachartre label->dkl_vtoc.v_version = V_VERSION; 909edcc0754Sachartre label->dkl_vtoc.v_nparts = V_NUMPAR; 91078fcd0a1Sachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 911edcc0754Sachartre label->dkl_vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 912edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_cylno = 0; 913edcc0754Sachartre label->dkl_map[VD_ENTIRE_DISK_SLICE].dkl_nblk = label->dkl_ncyl * 91478fcd0a1Sachartre label->dkl_nhead * label->dkl_nsect; 915edcc0754Sachartre label->dkl_magic = DKL_MAGIC; 91678fcd0a1Sachartre label->dkl_cksum = vd_lbl2cksum(label); 91778fcd0a1Sachartre } 91878fcd0a1Sachartre 91978fcd0a1Sachartre /* 92078fcd0a1Sachartre * Function: 92187a7269eSachartre * vd_file_set_vtoc 92287a7269eSachartre * 92387a7269eSachartre * Description: 92487a7269eSachartre * Set the vtoc of a disk image by writing the label and backup 92587a7269eSachartre * labels into the disk image backend. 92687a7269eSachartre * 92787a7269eSachartre * Parameters: 92887a7269eSachartre * vd - disk on which the operation is performed. 92987a7269eSachartre * label - the data to be written. 93087a7269eSachartre * 93187a7269eSachartre * Return Code: 93287a7269eSachartre * 0 - success. 93387a7269eSachartre * n > 0 - error, n indicates the errno code. 93487a7269eSachartre */ 93587a7269eSachartre static int 93687a7269eSachartre vd_file_set_vtoc(vd_t *vd, struct dk_label *label) 93787a7269eSachartre { 93887a7269eSachartre int blk, sec, cyl, head, cnt; 93987a7269eSachartre 94087a7269eSachartre ASSERT(vd->file); 94187a7269eSachartre 94287a7269eSachartre if (VD_FILE_LABEL_WRITE(vd, label) < 0) { 94387a7269eSachartre PR0("fail to write disk label"); 94487a7269eSachartre return (EIO); 94587a7269eSachartre } 94687a7269eSachartre 94787a7269eSachartre /* 94887a7269eSachartre * Backup labels are on the last alternate cylinder's 94987a7269eSachartre * first five odd sectors. 95087a7269eSachartre */ 95187a7269eSachartre if (label->dkl_acyl == 0) { 95287a7269eSachartre PR0("no alternate cylinder, can not store backup labels"); 95387a7269eSachartre return (0); 95487a7269eSachartre } 95587a7269eSachartre 95687a7269eSachartre cyl = label->dkl_ncyl + label->dkl_acyl - 1; 95787a7269eSachartre head = label->dkl_nhead - 1; 95887a7269eSachartre 95987a7269eSachartre blk = (cyl * ((label->dkl_nhead * label->dkl_nsect) - label->dkl_apc)) + 96087a7269eSachartre (head * label->dkl_nsect); 96187a7269eSachartre 96287a7269eSachartre /* 96387a7269eSachartre * Write the backup labels. Make sure we don't try to write past 96487a7269eSachartre * the last cylinder. 96587a7269eSachartre */ 96687a7269eSachartre sec = 1; 96787a7269eSachartre 96887a7269eSachartre for (cnt = 0; cnt < VD_FILE_NUM_BACKUP; cnt++) { 96987a7269eSachartre 97087a7269eSachartre if (sec >= label->dkl_nsect) { 97187a7269eSachartre PR0("not enough sector to store all backup labels"); 97287a7269eSachartre return (0); 97387a7269eSachartre } 97487a7269eSachartre 97587a7269eSachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, (caddr_t)label, 97687a7269eSachartre blk + sec, sizeof (struct dk_label)) < 0) { 97787a7269eSachartre PR0("error writing backup label at block %d\n", 97887a7269eSachartre blk + sec); 97987a7269eSachartre return (EIO); 98087a7269eSachartre } 98187a7269eSachartre 98287a7269eSachartre PR1("wrote backup label at block %d\n", blk + sec); 98387a7269eSachartre 98487a7269eSachartre sec += 2; 98587a7269eSachartre } 98687a7269eSachartre 98787a7269eSachartre return (0); 98887a7269eSachartre } 98987a7269eSachartre 99087a7269eSachartre /* 99187a7269eSachartre * Function: 99287a7269eSachartre * vd_file_get_devid_block 99387a7269eSachartre * 99487a7269eSachartre * Description: 99587a7269eSachartre * Return the block number where the device id is stored. 99687a7269eSachartre * 99787a7269eSachartre * Parameters: 99887a7269eSachartre * vd - disk on which the operation is performed. 99987a7269eSachartre * blkp - pointer to the block number 100087a7269eSachartre * 100187a7269eSachartre * Return Code: 100287a7269eSachartre * 0 - success 100387a7269eSachartre * ENOSPC - disk has no space to store a device id 100487a7269eSachartre */ 100587a7269eSachartre static int 100687a7269eSachartre vd_file_get_devid_block(vd_t *vd, size_t *blkp) 100787a7269eSachartre { 100887a7269eSachartre diskaddr_t spc, head, cyl; 100987a7269eSachartre 101087a7269eSachartre ASSERT(vd->file); 1011edcc0754Sachartre 1012edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_UNK) { 1013edcc0754Sachartre /* 1014edcc0754Sachartre * If no label is defined we don't know where to find 1015edcc0754Sachartre * a device id. 1016edcc0754Sachartre */ 1017edcc0754Sachartre return (ENOSPC); 1018edcc0754Sachartre } 1019edcc0754Sachartre 1020edcc0754Sachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1021edcc0754Sachartre /* 1022edcc0754Sachartre * For an EFI disk, the devid is at the beginning of 1023edcc0754Sachartre * the reserved slice 1024edcc0754Sachartre */ 1025edcc0754Sachartre if (vd->efi_reserved == -1) { 1026edcc0754Sachartre PR0("EFI disk has no reserved slice"); 1027edcc0754Sachartre return (ENOSPC); 1028edcc0754Sachartre } 1029edcc0754Sachartre 1030edcc0754Sachartre *blkp = vd->slices[vd->efi_reserved].start; 1031edcc0754Sachartre return (0); 1032edcc0754Sachartre } 1033edcc0754Sachartre 103487a7269eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 103587a7269eSachartre 103687a7269eSachartre /* this geometry doesn't allow us to have a devid */ 103787a7269eSachartre if (vd->dk_geom.dkg_acyl < 2) { 103887a7269eSachartre PR0("not enough alternate cylinder available for devid " 103987a7269eSachartre "(acyl=%u)", vd->dk_geom.dkg_acyl); 104087a7269eSachartre return (ENOSPC); 104187a7269eSachartre } 104287a7269eSachartre 104387a7269eSachartre /* the devid is in on the track next to the last cylinder */ 104487a7269eSachartre cyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl - 2; 104587a7269eSachartre spc = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 104687a7269eSachartre head = vd->dk_geom.dkg_nhead - 1; 104787a7269eSachartre 104887a7269eSachartre *blkp = (cyl * (spc - vd->dk_geom.dkg_apc)) + 104987a7269eSachartre (head * vd->dk_geom.dkg_nsect) + 1; 105087a7269eSachartre 105187a7269eSachartre return (0); 105287a7269eSachartre } 105387a7269eSachartre 105487a7269eSachartre /* 105587a7269eSachartre * Return the checksum of a disk block containing an on-disk devid. 105687a7269eSachartre */ 105787a7269eSachartre static uint_t 105887a7269eSachartre vd_dkdevid2cksum(struct dk_devid *dkdevid) 105987a7269eSachartre { 106087a7269eSachartre uint_t chksum, *ip; 106187a7269eSachartre int i; 106287a7269eSachartre 106387a7269eSachartre chksum = 0; 106487a7269eSachartre ip = (uint_t *)dkdevid; 106587a7269eSachartre for (i = 0; i < ((DEV_BSIZE - sizeof (int)) / sizeof (int)); i++) 106687a7269eSachartre chksum ^= ip[i]; 106787a7269eSachartre 106887a7269eSachartre return (chksum); 106987a7269eSachartre } 107087a7269eSachartre 107187a7269eSachartre /* 107287a7269eSachartre * Function: 107387a7269eSachartre * vd_file_read_devid 107487a7269eSachartre * 107587a7269eSachartre * Description: 107687a7269eSachartre * Read the device id stored on a disk image. 107787a7269eSachartre * 107887a7269eSachartre * Parameters: 107987a7269eSachartre * vd - disk on which the operation is performed. 108087a7269eSachartre * devid - the return address of the device ID. 108187a7269eSachartre * 108287a7269eSachartre * Return Code: 108387a7269eSachartre * 0 - success 108487a7269eSachartre * EIO - I/O error while trying to access the disk image 108587a7269eSachartre * EINVAL - no valid device id was found 108687a7269eSachartre * ENOSPC - disk has no space to store a device id 108787a7269eSachartre */ 108887a7269eSachartre static int 108987a7269eSachartre vd_file_read_devid(vd_t *vd, ddi_devid_t *devid) 109087a7269eSachartre { 109187a7269eSachartre struct dk_devid *dkdevid; 109287a7269eSachartre size_t blk; 109387a7269eSachartre uint_t chksum; 109487a7269eSachartre int status, sz; 109587a7269eSachartre 109687a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 109787a7269eSachartre return (status); 109887a7269eSachartre 109987a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 110087a7269eSachartre 110187a7269eSachartre /* get the devid */ 110287a7269eSachartre if ((vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)dkdevid, blk, 110387a7269eSachartre DEV_BSIZE)) < 0) { 110487a7269eSachartre PR0("error reading devid block at %lu", blk); 110587a7269eSachartre status = EIO; 110687a7269eSachartre goto done; 110787a7269eSachartre } 110887a7269eSachartre 110987a7269eSachartre /* validate the revision */ 111087a7269eSachartre if ((dkdevid->dkd_rev_hi != DK_DEVID_REV_MSB) || 111187a7269eSachartre (dkdevid->dkd_rev_lo != DK_DEVID_REV_LSB)) { 111287a7269eSachartre PR0("invalid devid found at block %lu (bad revision)", blk); 111387a7269eSachartre status = EINVAL; 111487a7269eSachartre goto done; 111587a7269eSachartre } 111687a7269eSachartre 111787a7269eSachartre /* compute checksum */ 111887a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 111987a7269eSachartre 112087a7269eSachartre /* compare the checksums */ 112187a7269eSachartre if (DKD_GETCHKSUM(dkdevid) != chksum) { 112287a7269eSachartre PR0("invalid devid found at block %lu (bad checksum)", blk); 112387a7269eSachartre status = EINVAL; 112487a7269eSachartre goto done; 112587a7269eSachartre } 112687a7269eSachartre 112787a7269eSachartre /* validate the device id */ 112887a7269eSachartre if (ddi_devid_valid((ddi_devid_t)&dkdevid->dkd_devid) != DDI_SUCCESS) { 112987a7269eSachartre PR0("invalid devid found at block %lu", blk); 113087a7269eSachartre status = EINVAL; 113187a7269eSachartre goto done; 113287a7269eSachartre } 113387a7269eSachartre 113487a7269eSachartre PR1("devid read at block %lu", blk); 113587a7269eSachartre 113687a7269eSachartre sz = ddi_devid_sizeof((ddi_devid_t)&dkdevid->dkd_devid); 113787a7269eSachartre *devid = kmem_alloc(sz, KM_SLEEP); 113887a7269eSachartre bcopy(&dkdevid->dkd_devid, *devid, sz); 113987a7269eSachartre 114087a7269eSachartre done: 114187a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 114287a7269eSachartre return (status); 114387a7269eSachartre 114487a7269eSachartre } 114587a7269eSachartre 114687a7269eSachartre /* 114787a7269eSachartre * Function: 114887a7269eSachartre * vd_file_write_devid 114987a7269eSachartre * 115087a7269eSachartre * Description: 115187a7269eSachartre * Write a device id into disk image. 115287a7269eSachartre * 115387a7269eSachartre * Parameters: 115487a7269eSachartre * vd - disk on which the operation is performed. 115587a7269eSachartre * devid - the device ID to store. 115687a7269eSachartre * 115787a7269eSachartre * Return Code: 115887a7269eSachartre * 0 - success 115987a7269eSachartre * EIO - I/O error while trying to access the disk image 116087a7269eSachartre * ENOSPC - disk has no space to store a device id 116187a7269eSachartre */ 116287a7269eSachartre static int 116387a7269eSachartre vd_file_write_devid(vd_t *vd, ddi_devid_t devid) 116487a7269eSachartre { 116587a7269eSachartre struct dk_devid *dkdevid; 116687a7269eSachartre uint_t chksum; 116787a7269eSachartre size_t blk; 116887a7269eSachartre int status; 116987a7269eSachartre 1170edcc0754Sachartre if (devid == NULL) { 1171edcc0754Sachartre /* nothing to write */ 1172edcc0754Sachartre return (0); 1173edcc0754Sachartre } 1174edcc0754Sachartre 117587a7269eSachartre if ((status = vd_file_get_devid_block(vd, &blk)) != 0) 117687a7269eSachartre return (status); 117787a7269eSachartre 117887a7269eSachartre dkdevid = kmem_zalloc(DEV_BSIZE, KM_SLEEP); 117987a7269eSachartre 118087a7269eSachartre /* set revision */ 118187a7269eSachartre dkdevid->dkd_rev_hi = DK_DEVID_REV_MSB; 118287a7269eSachartre dkdevid->dkd_rev_lo = DK_DEVID_REV_LSB; 118387a7269eSachartre 118487a7269eSachartre /* copy devid */ 118587a7269eSachartre bcopy(devid, &dkdevid->dkd_devid, ddi_devid_sizeof(devid)); 118687a7269eSachartre 118787a7269eSachartre /* compute checksum */ 118887a7269eSachartre chksum = vd_dkdevid2cksum(dkdevid); 118987a7269eSachartre 119087a7269eSachartre /* set checksum */ 119187a7269eSachartre DKD_FORMCHKSUM(chksum, dkdevid); 119287a7269eSachartre 119387a7269eSachartre /* store the devid */ 119487a7269eSachartre if ((status = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 119587a7269eSachartre (caddr_t)dkdevid, blk, DEV_BSIZE)) < 0) { 119687a7269eSachartre PR0("Error writing devid block at %lu", blk); 119787a7269eSachartre status = EIO; 119887a7269eSachartre } else { 119987a7269eSachartre PR1("devid written at block %lu", blk); 120087a7269eSachartre status = 0; 120187a7269eSachartre } 120287a7269eSachartre 120387a7269eSachartre kmem_free(dkdevid, DEV_BSIZE); 120487a7269eSachartre return (status); 120587a7269eSachartre } 120687a7269eSachartre 120787a7269eSachartre /* 120887a7269eSachartre * Function: 120917cadca8Slm66018 * vd_do_scsi_rdwr 121087a7269eSachartre * 121187a7269eSachartre * Description: 121287a7269eSachartre * Read or write to a SCSI disk using an absolute disk offset. 121387a7269eSachartre * 121487a7269eSachartre * Parameters: 121587a7269eSachartre * vd - disk on which the operation is performed. 121687a7269eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 121787a7269eSachartre * write (VD_OP_BWRITE). 121887a7269eSachartre * data - buffer where data are read to or written from. 121987a7269eSachartre * blk - starting block for the operation. 122087a7269eSachartre * len - number of bytes to read or write. 122187a7269eSachartre * 122287a7269eSachartre * Return Code: 122387a7269eSachartre * 0 - success 122487a7269eSachartre * n != 0 - error. 122587a7269eSachartre */ 122687a7269eSachartre static int 122717cadca8Slm66018 vd_do_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t blk, size_t len) 122887a7269eSachartre { 122987a7269eSachartre struct uscsi_cmd ucmd; 123087a7269eSachartre union scsi_cdb cdb; 123187a7269eSachartre int nsectors, nblk; 123287a7269eSachartre int max_sectors; 123387a7269eSachartre int status, rval; 123487a7269eSachartre 123587a7269eSachartre ASSERT(!vd->file); 123617cadca8Slm66018 ASSERT(vd->vdisk_block_size > 0); 123787a7269eSachartre 123887a7269eSachartre max_sectors = vd->max_xfer_sz; 123917cadca8Slm66018 nblk = (len / vd->vdisk_block_size); 124087a7269eSachartre 124117cadca8Slm66018 if (len % vd->vdisk_block_size != 0) 124287a7269eSachartre return (EINVAL); 124387a7269eSachartre 124487a7269eSachartre /* 124587a7269eSachartre * Build and execute the uscsi ioctl. We build a group0, group1 124687a7269eSachartre * or group4 command as necessary, since some targets 124787a7269eSachartre * do not support group1 commands. 124887a7269eSachartre */ 124987a7269eSachartre while (nblk) { 125087a7269eSachartre 125187a7269eSachartre bzero(&ucmd, sizeof (ucmd)); 125287a7269eSachartre bzero(&cdb, sizeof (cdb)); 125387a7269eSachartre 125487a7269eSachartre nsectors = (max_sectors < nblk) ? max_sectors : nblk; 125587a7269eSachartre 125617cadca8Slm66018 /* 125717cadca8Slm66018 * Some of the optical drives on sun4v machines are ATAPI 125817cadca8Slm66018 * devices which use Group 1 Read/Write commands so we need 125917cadca8Slm66018 * to explicitly check a flag which is set when a domain 126017cadca8Slm66018 * is bound. 126117cadca8Slm66018 */ 126217cadca8Slm66018 if (blk < (2 << 20) && nsectors <= 0xff && !vd->is_atapi_dev) { 126387a7269eSachartre FORMG0ADDR(&cdb, blk); 126487a7269eSachartre FORMG0COUNT(&cdb, nsectors); 126587a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP0; 126687a7269eSachartre } else if (blk > 0xffffffff) { 126787a7269eSachartre FORMG4LONGADDR(&cdb, blk); 126887a7269eSachartre FORMG4COUNT(&cdb, nsectors); 126987a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP4; 127087a7269eSachartre cdb.scc_cmd |= SCMD_GROUP4; 127187a7269eSachartre } else { 127287a7269eSachartre FORMG1ADDR(&cdb, blk); 127387a7269eSachartre FORMG1COUNT(&cdb, nsectors); 127487a7269eSachartre ucmd.uscsi_cdblen = CDB_GROUP1; 127587a7269eSachartre cdb.scc_cmd |= SCMD_GROUP1; 127687a7269eSachartre } 127787a7269eSachartre ucmd.uscsi_cdb = (caddr_t)&cdb; 127887a7269eSachartre ucmd.uscsi_bufaddr = data; 127917cadca8Slm66018 ucmd.uscsi_buflen = nsectors * vd->block_size; 128087a7269eSachartre ucmd.uscsi_timeout = vd_scsi_rdwr_timeout; 128187a7269eSachartre /* 128287a7269eSachartre * Set flags so that the command is isolated from normal 128387a7269eSachartre * commands and no error message is printed. 128487a7269eSachartre */ 128587a7269eSachartre ucmd.uscsi_flags = USCSI_ISOLATE | USCSI_SILENT; 128687a7269eSachartre 128787a7269eSachartre if (operation == VD_OP_BREAD) { 128887a7269eSachartre cdb.scc_cmd |= SCMD_READ; 128987a7269eSachartre ucmd.uscsi_flags |= USCSI_READ; 129087a7269eSachartre } else { 129187a7269eSachartre cdb.scc_cmd |= SCMD_WRITE; 129287a7269eSachartre } 129387a7269eSachartre 129487a7269eSachartre status = ldi_ioctl(vd->ldi_handle[VD_ENTIRE_DISK_SLICE], 1295047ba61eSachartre USCSICMD, (intptr_t)&ucmd, (vd->open_flags | FKIOCTL), 129687a7269eSachartre kcred, &rval); 129787a7269eSachartre 129887a7269eSachartre if (status == 0) 129987a7269eSachartre status = ucmd.uscsi_status; 130087a7269eSachartre 130187a7269eSachartre if (status != 0) 130287a7269eSachartre break; 130387a7269eSachartre 130487a7269eSachartre /* 130587a7269eSachartre * Check if partial DMA breakup is required. If so, reduce 130687a7269eSachartre * the request size by half and retry the last request. 130787a7269eSachartre */ 130887a7269eSachartre if (ucmd.uscsi_resid == ucmd.uscsi_buflen) { 130987a7269eSachartre max_sectors >>= 1; 131087a7269eSachartre if (max_sectors <= 0) { 131187a7269eSachartre status = EIO; 131287a7269eSachartre break; 131387a7269eSachartre } 131487a7269eSachartre continue; 131587a7269eSachartre } 131687a7269eSachartre 131787a7269eSachartre if (ucmd.uscsi_resid != 0) { 131887a7269eSachartre status = EIO; 131987a7269eSachartre break; 132087a7269eSachartre } 132187a7269eSachartre 132287a7269eSachartre blk += nsectors; 132387a7269eSachartre nblk -= nsectors; 132417cadca8Slm66018 data += nsectors * vd->vdisk_block_size; /* SECSIZE */ 132587a7269eSachartre } 132687a7269eSachartre 132787a7269eSachartre return (status); 132887a7269eSachartre } 132987a7269eSachartre 1330205eeb1aSlm66018 /* 133117cadca8Slm66018 * Function: 133217cadca8Slm66018 * vd_scsi_rdwr 133317cadca8Slm66018 * 133417cadca8Slm66018 * Description: 133517cadca8Slm66018 * Wrapper function to read or write to a SCSI disk using an absolute 133617cadca8Slm66018 * disk offset. It checks the blocksize of the underlying device and, 133717cadca8Slm66018 * if necessary, adjusts the buffers accordingly before calling 133817cadca8Slm66018 * vd_do_scsi_rdwr() to do the actual read or write. 133917cadca8Slm66018 * 134017cadca8Slm66018 * Parameters: 134117cadca8Slm66018 * vd - disk on which the operation is performed. 134217cadca8Slm66018 * operation - operation to execute: read (VD_OP_BREAD) or 134317cadca8Slm66018 * write (VD_OP_BWRITE). 134417cadca8Slm66018 * data - buffer where data are read to or written from. 134517cadca8Slm66018 * blk - starting block for the operation. 134617cadca8Slm66018 * len - number of bytes to read or write. 134717cadca8Slm66018 * 134817cadca8Slm66018 * Return Code: 134917cadca8Slm66018 * 0 - success 135017cadca8Slm66018 * n != 0 - error. 135117cadca8Slm66018 */ 135217cadca8Slm66018 static int 135317cadca8Slm66018 vd_scsi_rdwr(vd_t *vd, int operation, caddr_t data, size_t vblk, size_t vlen) 135417cadca8Slm66018 { 135517cadca8Slm66018 int rv; 135617cadca8Slm66018 135717cadca8Slm66018 size_t pblk; /* physical device block number of data on device */ 135817cadca8Slm66018 size_t delta; /* relative offset between pblk and vblk */ 135917cadca8Slm66018 size_t pnblk; /* number of physical blocks to be read from device */ 136017cadca8Slm66018 size_t plen; /* length of data to be read from physical device */ 136117cadca8Slm66018 char *buf; /* buffer area to fit physical device's block size */ 136217cadca8Slm66018 13632f5224aeSachartre if (vd->block_size == 0) { 13642f5224aeSachartre /* 13652f5224aeSachartre * The block size was not available during the attach, 13662f5224aeSachartre * try to update it now. 13672f5224aeSachartre */ 13682f5224aeSachartre if (vd_setup_mediainfo(vd) != 0) 13692f5224aeSachartre return (EIO); 13702f5224aeSachartre } 13712f5224aeSachartre 137217cadca8Slm66018 /* 137317cadca8Slm66018 * If the vdisk block size and the block size of the underlying device 137417cadca8Slm66018 * match we can skip straight to vd_do_scsi_rdwr(), otherwise we need 137517cadca8Slm66018 * to create a buffer large enough to handle the device's block size 137617cadca8Slm66018 * and adjust the block to be read from and the amount of data to 137717cadca8Slm66018 * read to correspond with the device's block size. 137817cadca8Slm66018 */ 137917cadca8Slm66018 if (vd->vdisk_block_size == vd->block_size) 138017cadca8Slm66018 return (vd_do_scsi_rdwr(vd, operation, data, vblk, vlen)); 138117cadca8Slm66018 138217cadca8Slm66018 if (vd->vdisk_block_size > vd->block_size) 138317cadca8Slm66018 return (EINVAL); 138417cadca8Slm66018 138517cadca8Slm66018 /* 138617cadca8Slm66018 * Writing of physical block sizes larger than the virtual block size 138717cadca8Slm66018 * is not supported. This would be added if/when support for guests 138817cadca8Slm66018 * writing to DVDs is implemented. 138917cadca8Slm66018 */ 139017cadca8Slm66018 if (operation == VD_OP_BWRITE) 139117cadca8Slm66018 return (ENOTSUP); 139217cadca8Slm66018 139317cadca8Slm66018 /* BEGIN CSTYLED */ 139417cadca8Slm66018 /* 139517cadca8Slm66018 * Below is a diagram showing the relationship between the physical 139617cadca8Slm66018 * and virtual blocks. If the virtual blocks marked by 'X' below are 139717cadca8Slm66018 * requested, then the physical blocks denoted by 'Y' are read. 139817cadca8Slm66018 * 139917cadca8Slm66018 * vblk 140017cadca8Slm66018 * | vlen 140117cadca8Slm66018 * |<--------------->| 140217cadca8Slm66018 * v v 140317cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- virtual disk: 140417cadca8Slm66018 * | | | |XX|XX|XX|XX|XX|XX| | | | | | } block size is 140517cadca8Slm66018 * --+--+--+--+--+--+--+--+--+--+--+--+--+--+--+- vd->vdisk_block_size 140617cadca8Slm66018 * : : : : 140717cadca8Slm66018 * >:==:< delta : : 140817cadca8Slm66018 * : : : : 140917cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- physical disk: 141017cadca8Slm66018 * | |YY:YY|YYYYY|YYYYY|YY:YY| | | } block size is 141117cadca8Slm66018 * --+-----+-----+-----+-----+-----+-----+-----+-- vd->block_size 141217cadca8Slm66018 * ^ ^ 141317cadca8Slm66018 * |<--------------------->| 141417cadca8Slm66018 * | plen 141517cadca8Slm66018 * pblk 141617cadca8Slm66018 */ 141717cadca8Slm66018 /* END CSTYLED */ 141817cadca8Slm66018 pblk = (vblk * vd->vdisk_block_size) / vd->block_size; 141917cadca8Slm66018 delta = (vblk * vd->vdisk_block_size) - (pblk * vd->block_size); 142017cadca8Slm66018 pnblk = ((delta + vlen - 1) / vd->block_size) + 1; 142117cadca8Slm66018 plen = pnblk * vd->block_size; 142217cadca8Slm66018 142317cadca8Slm66018 PR2("vblk %lx:pblk %lx: vlen %ld:plen %ld", vblk, pblk, vlen, plen); 142417cadca8Slm66018 142517cadca8Slm66018 buf = kmem_zalloc(sizeof (caddr_t) * plen, KM_SLEEP); 142617cadca8Slm66018 rv = vd_do_scsi_rdwr(vd, operation, (caddr_t)buf, pblk, plen); 142717cadca8Slm66018 bcopy(buf + delta, data, vlen); 142817cadca8Slm66018 142917cadca8Slm66018 kmem_free(buf, sizeof (caddr_t) * plen); 143017cadca8Slm66018 143117cadca8Slm66018 return (rv); 143217cadca8Slm66018 } 143317cadca8Slm66018 143417cadca8Slm66018 /* 1435bae9e67eSachartre * Function: 1436bae9e67eSachartre * vd_slice_flabel_read 1437bae9e67eSachartre * 1438bae9e67eSachartre * Description: 1439bae9e67eSachartre * This function simulates a read operation from the fake label of 1440bae9e67eSachartre * a single-slice disk. 1441bae9e67eSachartre * 1442bae9e67eSachartre * Parameters: 1443bae9e67eSachartre * vd - single-slice disk to read from 1444bae9e67eSachartre * data - buffer where data should be read to 1445bae9e67eSachartre * offset - offset in byte where the read should start 1446bae9e67eSachartre * length - number of bytes to read 1447bae9e67eSachartre * 1448bae9e67eSachartre * Return Code: 1449bae9e67eSachartre * n >= 0 - success, n indicates the number of bytes read 1450bae9e67eSachartre * -1 - error 1451bae9e67eSachartre */ 1452bae9e67eSachartre static ssize_t 1453bae9e67eSachartre vd_slice_flabel_read(vd_t *vd, caddr_t data, size_t offset, size_t length) 1454bae9e67eSachartre { 1455bae9e67eSachartre size_t n = 0; 1456bae9e67eSachartre uint_t limit = vd->flabel_limit * DEV_BSIZE; 1457bae9e67eSachartre 1458bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1459bae9e67eSachartre ASSERT(vd->flabel != NULL); 1460bae9e67eSachartre 1461bae9e67eSachartre /* if offset is past the fake label limit there's nothing to read */ 1462bae9e67eSachartre if (offset >= limit) 1463bae9e67eSachartre return (0); 1464bae9e67eSachartre 1465bae9e67eSachartre /* data with offset 0 to flabel_size are read from flabel */ 1466bae9e67eSachartre if (offset < vd->flabel_size) { 1467bae9e67eSachartre 1468bae9e67eSachartre if (offset + length <= vd->flabel_size) { 1469bae9e67eSachartre bcopy(vd->flabel + offset, data, length); 1470bae9e67eSachartre return (length); 1471bae9e67eSachartre } 1472bae9e67eSachartre 1473bae9e67eSachartre n = vd->flabel_size - offset; 1474bae9e67eSachartre bcopy(vd->flabel + offset, data, n); 1475bae9e67eSachartre data += n; 1476bae9e67eSachartre } 1477bae9e67eSachartre 1478bae9e67eSachartre /* data with offset from flabel_size to flabel_limit are all zeros */ 1479bae9e67eSachartre if (offset + length <= limit) { 1480bae9e67eSachartre bzero(data, length - n); 1481bae9e67eSachartre return (length); 1482bae9e67eSachartre } 1483bae9e67eSachartre 1484bae9e67eSachartre bzero(data, limit - offset - n); 1485bae9e67eSachartre return (limit - offset); 1486bae9e67eSachartre } 1487bae9e67eSachartre 1488bae9e67eSachartre /* 1489bae9e67eSachartre * Function: 1490bae9e67eSachartre * vd_slice_flabel_write 1491bae9e67eSachartre * 1492bae9e67eSachartre * Description: 1493bae9e67eSachartre * This function simulates a write operation to the fake label of 1494bae9e67eSachartre * a single-slice disk. Write operations are actually faked and return 1495bae9e67eSachartre * success although the label is never changed. This is mostly to 1496bae9e67eSachartre * simulate a successful label update. 1497bae9e67eSachartre * 1498bae9e67eSachartre * Parameters: 1499bae9e67eSachartre * vd - single-slice disk to write to 1500bae9e67eSachartre * data - buffer where data should be written from 1501bae9e67eSachartre * offset - offset in byte where the write should start 1502bae9e67eSachartre * length - number of bytes to written 1503bae9e67eSachartre * 1504bae9e67eSachartre * Return Code: 1505bae9e67eSachartre * n >= 0 - success, n indicates the number of bytes written 1506bae9e67eSachartre * -1 - error 1507bae9e67eSachartre */ 1508bae9e67eSachartre static ssize_t 1509bae9e67eSachartre vd_slice_flabel_write(vd_t *vd, caddr_t data, size_t offset, size_t length) 1510bae9e67eSachartre { 1511bae9e67eSachartre uint_t limit = vd->flabel_limit * DEV_BSIZE; 1512bae9e67eSachartre struct dk_label *label; 1513bae9e67eSachartre struct dk_geom geom; 1514bae9e67eSachartre struct vtoc vtoc; 1515bae9e67eSachartre 1516bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1517bae9e67eSachartre ASSERT(vd->flabel != NULL); 1518bae9e67eSachartre 1519bae9e67eSachartre if (offset >= limit) 1520bae9e67eSachartre return (0); 1521bae9e67eSachartre 1522bae9e67eSachartre /* 1523bae9e67eSachartre * If this is a request to overwrite the VTOC disk label, check that 1524bae9e67eSachartre * the new label is similar to the previous one and return that the 1525bae9e67eSachartre * write was successful, but note that nothing is actually overwritten. 1526bae9e67eSachartre */ 1527bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1528bae9e67eSachartre offset == 0 && length == DEV_BSIZE) { 1529bae9e67eSachartre label = (struct dk_label *)data; 1530bae9e67eSachartre 1531bae9e67eSachartre /* check that this is a valid label */ 1532bae9e67eSachartre if (label->dkl_magic != DKL_MAGIC || 1533bae9e67eSachartre label->dkl_cksum != vd_lbl2cksum(label)) 1534bae9e67eSachartre return (-1); 1535bae9e67eSachartre 1536bae9e67eSachartre /* check the vtoc and geometry */ 1537bae9e67eSachartre vd_label_to_vtocgeom(label, &vtoc, &geom); 1538bae9e67eSachartre if (vd_slice_geom_isvalid(vd, &geom) && 1539bae9e67eSachartre vd_slice_vtoc_isvalid(vd, &vtoc)) 1540bae9e67eSachartre return (length); 1541bae9e67eSachartre } 1542bae9e67eSachartre 1543bae9e67eSachartre /* fail any other write */ 1544bae9e67eSachartre return (-1); 1545bae9e67eSachartre } 1546bae9e67eSachartre 1547bae9e67eSachartre /* 1548bae9e67eSachartre * Function: 1549bae9e67eSachartre * vd_slice_fake_rdwr 1550bae9e67eSachartre * 1551bae9e67eSachartre * Description: 1552bae9e67eSachartre * This function simulates a raw read or write operation to a single-slice 1553bae9e67eSachartre * disk. It only handles the faked part of the operation i.e. I/Os to 1554bae9e67eSachartre * blocks which have no mapping with the vdisk backend (I/Os to the 1555bae9e67eSachartre * beginning and to the end of the vdisk). 1556bae9e67eSachartre * 1557bae9e67eSachartre * The function returns 0 is the operation is completed and it has been 1558bae9e67eSachartre * entirely handled as a fake read or write. In that case, lengthp points 1559bae9e67eSachartre * to the number of bytes not read or written. Values returned by datap 1560bae9e67eSachartre * and blkp are undefined. 1561bae9e67eSachartre * 1562bae9e67eSachartre * If the fake operation has succeeded but the read or write is not 1563bae9e67eSachartre * complete (i.e. the read/write operation extends beyond the blocks 1564bae9e67eSachartre * we fake) then the function returns EAGAIN and datap, blkp and lengthp 1565bae9e67eSachartre * pointers points to the parameters for completing the operation. 1566bae9e67eSachartre * 1567bae9e67eSachartre * In case of an error, for example if the slice is empty or parameters 1568bae9e67eSachartre * are invalid, then the function returns a non-zero value different 1569bae9e67eSachartre * from EAGAIN. In that case, the returned values of datap, blkp and 1570bae9e67eSachartre * lengthp are undefined. 1571bae9e67eSachartre * 1572bae9e67eSachartre * Parameters: 1573bae9e67eSachartre * vd - single-slice disk on which the operation is performed 1574bae9e67eSachartre * slice - slice on which the operation is performed, 1575bae9e67eSachartre * VD_SLICE_NONE indicates that the operation 1576bae9e67eSachartre * is done using an absolute disk offset. 1577bae9e67eSachartre * operation - operation to execute: read (VD_OP_BREAD) or 1578bae9e67eSachartre * write (VD_OP_BWRITE). 1579bae9e67eSachartre * datap - pointer to the buffer where data are read to 1580bae9e67eSachartre * or written from. Return the pointer where remaining 1581bae9e67eSachartre * data have to be read to or written from. 1582bae9e67eSachartre * blkp - pointer to the starting block for the operation. 1583bae9e67eSachartre * Return the starting block relative to the vdisk 1584bae9e67eSachartre * backend for the remaining operation. 1585bae9e67eSachartre * lengthp - pointer to the number of bytes to read or write. 1586bae9e67eSachartre * This should be a multiple of DEV_BSIZE. Return the 1587bae9e67eSachartre * remaining number of bytes to read or write. 1588bae9e67eSachartre * 1589bae9e67eSachartre * Return Code: 1590bae9e67eSachartre * 0 - read/write operation is completed 1591bae9e67eSachartre * EAGAIN - read/write operation is not completed 1592bae9e67eSachartre * other values - error 1593bae9e67eSachartre */ 1594bae9e67eSachartre static int 1595bae9e67eSachartre vd_slice_fake_rdwr(vd_t *vd, int slice, int operation, caddr_t *datap, 1596bae9e67eSachartre size_t *blkp, size_t *lengthp) 1597bae9e67eSachartre { 1598bae9e67eSachartre struct dk_label *label; 1599bae9e67eSachartre caddr_t data; 1600bae9e67eSachartre size_t blk, length, csize; 1601bae9e67eSachartre size_t ablk, asize, aoff, alen; 1602bae9e67eSachartre ssize_t n; 1603bae9e67eSachartre int sec, status; 1604bae9e67eSachartre 1605bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 1606bae9e67eSachartre ASSERT(slice != 0); 1607bae9e67eSachartre 1608bae9e67eSachartre data = *datap; 1609bae9e67eSachartre blk = *blkp; 1610bae9e67eSachartre length = *lengthp; 1611bae9e67eSachartre 1612bae9e67eSachartre /* 1613bae9e67eSachartre * If this is not a raw I/O or an I/O from a full disk slice then 1614bae9e67eSachartre * this is an I/O to/from an empty slice. 1615bae9e67eSachartre */ 1616bae9e67eSachartre if (slice != VD_SLICE_NONE && 1617bae9e67eSachartre (slice != VD_ENTIRE_DISK_SLICE || 1618bae9e67eSachartre vd->vdisk_label != VD_DISK_LABEL_VTOC) && 1619bae9e67eSachartre (slice != VD_EFI_WD_SLICE || 1620bae9e67eSachartre vd->vdisk_label != VD_DISK_LABEL_EFI)) { 1621bae9e67eSachartre return (EIO); 1622bae9e67eSachartre } 1623bae9e67eSachartre 1624bae9e67eSachartre if (length % DEV_BSIZE != 0) 1625bae9e67eSachartre return (EINVAL); 1626bae9e67eSachartre 1627bae9e67eSachartre /* handle any I/O with the fake label */ 1628bae9e67eSachartre if (operation == VD_OP_BWRITE) 1629bae9e67eSachartre n = vd_slice_flabel_write(vd, data, blk * DEV_BSIZE, length); 1630bae9e67eSachartre else 1631bae9e67eSachartre n = vd_slice_flabel_read(vd, data, blk * DEV_BSIZE, length); 1632bae9e67eSachartre 1633bae9e67eSachartre if (n == -1) 1634bae9e67eSachartre return (EINVAL); 1635bae9e67eSachartre 1636bae9e67eSachartre ASSERT(n % DEV_BSIZE == 0); 1637bae9e67eSachartre 1638bae9e67eSachartre /* adjust I/O arguments */ 1639bae9e67eSachartre data += n; 1640bae9e67eSachartre blk += n / DEV_BSIZE; 1641bae9e67eSachartre length -= n; 1642bae9e67eSachartre 1643bae9e67eSachartre /* check if there's something else to process */ 1644bae9e67eSachartre if (length == 0) { 1645bae9e67eSachartre status = 0; 1646bae9e67eSachartre goto done; 1647bae9e67eSachartre } 1648bae9e67eSachartre 1649bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC && 1650bae9e67eSachartre slice == VD_ENTIRE_DISK_SLICE) { 1651bae9e67eSachartre status = EAGAIN; 1652bae9e67eSachartre goto done; 1653bae9e67eSachartre } 1654bae9e67eSachartre 1655bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI) { 1656bae9e67eSachartre asize = EFI_MIN_RESV_SIZE + 33; 1657bae9e67eSachartre ablk = vd->vdisk_size - asize; 1658bae9e67eSachartre } else { 1659bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 1660bae9e67eSachartre ASSERT(vd->dk_geom.dkg_apc == 0); 1661bae9e67eSachartre 1662bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 1663bae9e67eSachartre ablk = vd->dk_geom.dkg_ncyl * csize; 1664bae9e67eSachartre asize = vd->dk_geom.dkg_acyl * csize; 1665bae9e67eSachartre } 1666bae9e67eSachartre 1667bae9e67eSachartre alen = length / DEV_BSIZE; 1668bae9e67eSachartre aoff = blk; 1669bae9e67eSachartre 1670bae9e67eSachartre /* if we have reached the last block then the I/O is completed */ 1671bae9e67eSachartre if (aoff == ablk + asize) { 1672bae9e67eSachartre status = 0; 1673bae9e67eSachartre goto done; 1674bae9e67eSachartre } 1675bae9e67eSachartre 1676bae9e67eSachartre /* if we are past the last block then return an error */ 1677bae9e67eSachartre if (aoff > ablk + asize) 1678bae9e67eSachartre return (EIO); 1679bae9e67eSachartre 1680bae9e67eSachartre /* check if there is any I/O to end of the disk */ 1681bae9e67eSachartre if (aoff + alen < ablk) { 1682bae9e67eSachartre status = EAGAIN; 1683bae9e67eSachartre goto done; 1684bae9e67eSachartre } 1685bae9e67eSachartre 1686bae9e67eSachartre /* we don't allow any write to the end of the disk */ 1687bae9e67eSachartre if (operation == VD_OP_BWRITE) 1688bae9e67eSachartre return (EIO); 1689bae9e67eSachartre 1690bae9e67eSachartre if (aoff < ablk) { 1691bae9e67eSachartre alen -= (ablk - aoff); 1692bae9e67eSachartre aoff = ablk; 1693bae9e67eSachartre } 1694bae9e67eSachartre 1695bae9e67eSachartre if (aoff + alen > ablk + asize) { 1696bae9e67eSachartre alen = ablk + asize - aoff; 1697bae9e67eSachartre } 1698bae9e67eSachartre 1699bae9e67eSachartre alen *= DEV_BSIZE; 1700bae9e67eSachartre 1701bae9e67eSachartre if (operation == VD_OP_BREAD) { 1702bae9e67eSachartre bzero(data + (aoff - blk) * DEV_BSIZE, alen); 1703bae9e67eSachartre 1704bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 1705bae9e67eSachartre /* check if we read backup labels */ 1706bae9e67eSachartre label = VD_LABEL_VTOC(vd); 1707bae9e67eSachartre ablk += (label->dkl_acyl - 1) * csize + 1708bae9e67eSachartre (label->dkl_nhead - 1) * label->dkl_nsect; 1709bae9e67eSachartre 1710bae9e67eSachartre for (sec = 1; (sec < 5 * 2 + 1); sec += 2) { 1711bae9e67eSachartre 1712bae9e67eSachartre if (ablk + sec >= blk && 1713bae9e67eSachartre ablk + sec < blk + (length / DEV_BSIZE)) { 1714bae9e67eSachartre bcopy(label, data + 1715bae9e67eSachartre (ablk + sec - blk) * DEV_BSIZE, 1716bae9e67eSachartre sizeof (struct dk_label)); 1717bae9e67eSachartre } 1718bae9e67eSachartre } 1719bae9e67eSachartre } 1720bae9e67eSachartre } 1721bae9e67eSachartre 1722bae9e67eSachartre length -= alen; 1723bae9e67eSachartre 1724bae9e67eSachartre status = (length == 0)? 0: EAGAIN; 1725bae9e67eSachartre 1726bae9e67eSachartre done: 1727bae9e67eSachartre ASSERT(length == 0 || blk >= vd->flabel_limit); 1728bae9e67eSachartre 1729bae9e67eSachartre /* 1730bae9e67eSachartre * Return the parameters for the remaining I/O. The starting block is 1731bae9e67eSachartre * adjusted so that it is relative to the vdisk backend. 1732bae9e67eSachartre */ 1733bae9e67eSachartre *datap = data; 1734bae9e67eSachartre *blkp = blk - vd->flabel_limit; 1735bae9e67eSachartre *lengthp = length; 1736bae9e67eSachartre 1737bae9e67eSachartre return (status); 1738bae9e67eSachartre } 1739bae9e67eSachartre 1740bae9e67eSachartre /* 1741205eeb1aSlm66018 * Return Values 1742205eeb1aSlm66018 * EINPROGRESS - operation was successfully started 1743205eeb1aSlm66018 * EIO - encountered LDC (aka. task error) 1744205eeb1aSlm66018 * 0 - operation completed successfully 1745205eeb1aSlm66018 * 1746205eeb1aSlm66018 * Side Effect 1747205eeb1aSlm66018 * sets request->status = <disk operation status> 1748205eeb1aSlm66018 */ 17491ae08745Sheppo static int 1750d10e4ef2Snarayan vd_start_bio(vd_task_t *task) 17511ae08745Sheppo { 17524bac2208Snarayan int rv, status = 0; 1753d10e4ef2Snarayan vd_t *vd = task->vd; 1754d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 1755d10e4ef2Snarayan struct buf *buf = &task->buf; 17564bac2208Snarayan uint8_t mtype; 17573c96341aSnarayan int slice; 1758047ba61eSachartre char *bufaddr = 0; 1759047ba61eSachartre size_t buflen; 1760bae9e67eSachartre size_t offset, length, nbytes; 1761d10e4ef2Snarayan 1762d10e4ef2Snarayan ASSERT(vd != NULL); 1763d10e4ef2Snarayan ASSERT(request != NULL); 17643c96341aSnarayan 17653c96341aSnarayan slice = request->slice; 17663c96341aSnarayan 176787a7269eSachartre ASSERT(slice == VD_SLICE_NONE || slice < vd->nslices); 1768d10e4ef2Snarayan ASSERT((request->operation == VD_OP_BREAD) || 1769d10e4ef2Snarayan (request->operation == VD_OP_BWRITE)); 1770d10e4ef2Snarayan 1771205eeb1aSlm66018 if (request->nbytes == 0) { 1772205eeb1aSlm66018 /* no service for trivial requests */ 1773205eeb1aSlm66018 request->status = EINVAL; 1774205eeb1aSlm66018 return (0); 1775205eeb1aSlm66018 } 17761ae08745Sheppo 1777d10e4ef2Snarayan PR1("%s %lu bytes at block %lu", 1778d10e4ef2Snarayan (request->operation == VD_OP_BREAD) ? "Read" : "Write", 1779d10e4ef2Snarayan request->nbytes, request->addr); 17801ae08745Sheppo 1781047ba61eSachartre /* 1782047ba61eSachartre * We have to check the open flags because the functions processing 1783047ba61eSachartre * the read/write request will not do it. 1784047ba61eSachartre */ 1785047ba61eSachartre if (request->operation == VD_OP_BWRITE && !(vd->open_flags & FWRITE)) { 1786047ba61eSachartre PR0("write fails because backend is opened read-only"); 1787047ba61eSachartre request->nbytes = 0; 1788047ba61eSachartre request->status = EROFS; 1789047ba61eSachartre return (0); 1790047ba61eSachartre } 1791d10e4ef2Snarayan 17924bac2208Snarayan mtype = (&vd->inband_task == task) ? LDC_SHADOW_MAP : LDC_DIRECT_MAP; 17934bac2208Snarayan 17944bac2208Snarayan /* Map memory exported by client */ 17954bac2208Snarayan status = ldc_mem_map(task->mhdl, request->cookie, request->ncookies, 17964bac2208Snarayan mtype, (request->operation == VD_OP_BREAD) ? LDC_MEM_W : LDC_MEM_R, 1797047ba61eSachartre &bufaddr, NULL); 17984bac2208Snarayan if (status != 0) { 17993af08d82Slm66018 PR0("ldc_mem_map() returned err %d ", status); 1800205eeb1aSlm66018 return (EIO); 1801d10e4ef2Snarayan } 1802d10e4ef2Snarayan 1803bae9e67eSachartre /* 1804bae9e67eSachartre * The buffer size has to be 8-byte aligned, so the client should have 1805bae9e67eSachartre * sent a buffer which size is roundup to the next 8-byte aligned value. 1806bae9e67eSachartre */ 1807bae9e67eSachartre buflen = P2ROUNDUP(request->nbytes, 8); 1808047ba61eSachartre 1809047ba61eSachartre status = ldc_mem_acquire(task->mhdl, 0, buflen); 18104bac2208Snarayan if (status != 0) { 18114bac2208Snarayan (void) ldc_mem_unmap(task->mhdl); 18123af08d82Slm66018 PR0("ldc_mem_acquire() returned err %d ", status); 1813205eeb1aSlm66018 return (EIO); 18144bac2208Snarayan } 18154bac2208Snarayan 1816bae9e67eSachartre offset = request->addr; 1817bae9e67eSachartre nbytes = request->nbytes; 1818bae9e67eSachartre length = nbytes; 1819bae9e67eSachartre 1820bae9e67eSachartre /* default number of byte returned by the I/O */ 1821bae9e67eSachartre request->nbytes = 0; 1822bae9e67eSachartre 1823bae9e67eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 1824bae9e67eSachartre 1825bae9e67eSachartre if (slice != 0) { 1826bae9e67eSachartre /* handle any fake I/O */ 1827bae9e67eSachartre rv = vd_slice_fake_rdwr(vd, slice, request->operation, 1828bae9e67eSachartre &bufaddr, &offset, &length); 1829bae9e67eSachartre 1830bae9e67eSachartre /* record the number of bytes from the fake I/O */ 1831bae9e67eSachartre request->nbytes = nbytes - length; 1832bae9e67eSachartre 1833bae9e67eSachartre if (rv == 0) { 1834bae9e67eSachartre request->status = 0; 1835bae9e67eSachartre goto io_done; 1836bae9e67eSachartre } 1837bae9e67eSachartre 1838bae9e67eSachartre if (rv != EAGAIN) { 18393c96341aSnarayan request->nbytes = 0; 1840205eeb1aSlm66018 request->status = EIO; 1841bae9e67eSachartre goto io_done; 18423c96341aSnarayan } 1843bae9e67eSachartre 1844bae9e67eSachartre /* 1845bae9e67eSachartre * If we return with EAGAIN then this means that there 1846bae9e67eSachartre * are still data to read or write. 1847bae9e67eSachartre */ 1848bae9e67eSachartre ASSERT(length != 0); 1849bae9e67eSachartre 1850bae9e67eSachartre /* 1851bae9e67eSachartre * We need to continue the I/O from the slice backend to 1852bae9e67eSachartre * complete the request. The variables bufaddr, offset 1853bae9e67eSachartre * and length have been adjusted to have the right 1854bae9e67eSachartre * information to do the remaining I/O from the backend. 1855bae9e67eSachartre * The backend is entirely mapped to slice 0 so we just 1856bae9e67eSachartre * have to complete the I/O from that slice. 1857bae9e67eSachartre */ 1858bae9e67eSachartre slice = 0; 1859bae9e67eSachartre } 1860bae9e67eSachartre 1861bae9e67eSachartre } else if ((slice == VD_SLICE_NONE) && (!vd->file)) { 1862bae9e67eSachartre 186387a7269eSachartre /* 186487a7269eSachartre * This is not a disk image so it is a real disk. We 186587a7269eSachartre * assume that the underlying device driver supports 186687a7269eSachartre * USCSICMD ioctls. This is the case of all SCSI devices 186787a7269eSachartre * (sd, ssd...). 186887a7269eSachartre * 186987a7269eSachartre * In the future if we have non-SCSI disks we would need 187087a7269eSachartre * to invoke the appropriate function to do I/O using an 187117cadca8Slm66018 * absolute disk offset (for example using DIOCTL_RWCMD 187287a7269eSachartre * for IDE disks). 187387a7269eSachartre */ 1874bae9e67eSachartre rv = vd_scsi_rdwr(vd, request->operation, bufaddr, offset, 1875bae9e67eSachartre length); 187687a7269eSachartre if (rv != 0) { 1877bae9e67eSachartre request->status = EIO; 1878bae9e67eSachartre } else { 1879bae9e67eSachartre request->nbytes = length; 1880bae9e67eSachartre request->status = 0; 1881bae9e67eSachartre } 1882bae9e67eSachartre goto io_done; 1883bae9e67eSachartre } 1884bae9e67eSachartre 1885bae9e67eSachartre /* Start the block I/O */ 1886bae9e67eSachartre if (vd->file) { 1887bae9e67eSachartre rv = vd_file_rw(vd, slice, request->operation, bufaddr, offset, 1888bae9e67eSachartre length); 1889bae9e67eSachartre if (rv < 0) { 189087a7269eSachartre request->nbytes = 0; 1891205eeb1aSlm66018 request->status = EIO; 189287a7269eSachartre } else { 1893bae9e67eSachartre request->nbytes += rv; 1894205eeb1aSlm66018 request->status = 0; 189587a7269eSachartre } 189687a7269eSachartre } else { 1897047ba61eSachartre bioinit(buf); 1898047ba61eSachartre buf->b_flags = B_BUSY; 1899bae9e67eSachartre buf->b_bcount = length; 1900bae9e67eSachartre buf->b_lblkno = offset; 1901bae9e67eSachartre buf->b_bufsize = buflen; 1902047ba61eSachartre buf->b_edev = vd->dev[slice]; 1903047ba61eSachartre buf->b_un.b_addr = bufaddr; 1904047ba61eSachartre buf->b_flags |= (request->operation == VD_OP_BREAD)? 1905047ba61eSachartre B_READ : B_WRITE; 1906047ba61eSachartre 1907bae9e67eSachartre request->status = ldi_strategy(vd->ldi_handle[slice], buf); 1908205eeb1aSlm66018 1909205eeb1aSlm66018 /* 1910205eeb1aSlm66018 * This is to indicate to the caller that the request 1911205eeb1aSlm66018 * needs to be finished by vd_complete_bio() by calling 1912205eeb1aSlm66018 * biowait() there and waiting for that to return before 1913205eeb1aSlm66018 * triggering the notification of the vDisk client. 1914205eeb1aSlm66018 * 1915205eeb1aSlm66018 * This is necessary when writing to real disks as 1916205eeb1aSlm66018 * otherwise calls to ldi_strategy() would be serialized 1917205eeb1aSlm66018 * behind the calls to biowait() and performance would 1918205eeb1aSlm66018 * suffer. 1919205eeb1aSlm66018 */ 1920205eeb1aSlm66018 if (request->status == 0) 192187a7269eSachartre return (EINPROGRESS); 1922047ba61eSachartre 1923047ba61eSachartre biofini(buf); 192487a7269eSachartre } 19253c96341aSnarayan 1926bae9e67eSachartre io_done: 1927bae9e67eSachartre /* Clean up after error or completion */ 1928047ba61eSachartre rv = ldc_mem_release(task->mhdl, 0, buflen); 19294bac2208Snarayan if (rv) { 19303af08d82Slm66018 PR0("ldc_mem_release() returned err %d ", rv); 1931205eeb1aSlm66018 status = EIO; 19324bac2208Snarayan } 19334bac2208Snarayan rv = ldc_mem_unmap(task->mhdl); 19344bac2208Snarayan if (rv) { 1935205eeb1aSlm66018 PR0("ldc_mem_unmap() returned err %d ", rv); 1936205eeb1aSlm66018 status = EIO; 19374bac2208Snarayan } 19384bac2208Snarayan 1939d10e4ef2Snarayan return (status); 1940d10e4ef2Snarayan } 1941d10e4ef2Snarayan 1942205eeb1aSlm66018 /* 1943205eeb1aSlm66018 * This function should only be called from vd_notify to ensure that requests 1944205eeb1aSlm66018 * are responded to in the order that they are received. 1945205eeb1aSlm66018 */ 1946d10e4ef2Snarayan static int 1947d10e4ef2Snarayan send_msg(ldc_handle_t ldc_handle, void *msg, size_t msglen) 1948d10e4ef2Snarayan { 19493af08d82Slm66018 int status; 1950d10e4ef2Snarayan size_t nbytes; 1951d10e4ef2Snarayan 19523af08d82Slm66018 do { 1953d10e4ef2Snarayan nbytes = msglen; 1954d10e4ef2Snarayan status = ldc_write(ldc_handle, msg, &nbytes); 19553af08d82Slm66018 if (status != EWOULDBLOCK) 19563af08d82Slm66018 break; 19573af08d82Slm66018 drv_usecwait(vds_ldc_delay); 19583af08d82Slm66018 } while (status == EWOULDBLOCK); 1959d10e4ef2Snarayan 1960d10e4ef2Snarayan if (status != 0) { 19613af08d82Slm66018 if (status != ECONNRESET) 19623af08d82Slm66018 PR0("ldc_write() returned errno %d", status); 1963d10e4ef2Snarayan return (status); 1964d10e4ef2Snarayan } else if (nbytes != msglen) { 19653af08d82Slm66018 PR0("ldc_write() performed only partial write"); 1966d10e4ef2Snarayan return (EIO); 1967d10e4ef2Snarayan } 1968d10e4ef2Snarayan 1969d10e4ef2Snarayan PR1("SENT %lu bytes", msglen); 1970d10e4ef2Snarayan return (0); 1971d10e4ef2Snarayan } 1972d10e4ef2Snarayan 1973d10e4ef2Snarayan static void 1974d10e4ef2Snarayan vd_need_reset(vd_t *vd, boolean_t reset_ldc) 1975d10e4ef2Snarayan { 1976d10e4ef2Snarayan mutex_enter(&vd->lock); 1977d10e4ef2Snarayan vd->reset_state = B_TRUE; 1978d10e4ef2Snarayan vd->reset_ldc = reset_ldc; 1979d10e4ef2Snarayan mutex_exit(&vd->lock); 1980d10e4ef2Snarayan } 1981d10e4ef2Snarayan 1982d10e4ef2Snarayan /* 1983d10e4ef2Snarayan * Reset the state of the connection with a client, if needed; reset the LDC 1984d10e4ef2Snarayan * transport as well, if needed. This function should only be called from the 19853af08d82Slm66018 * "vd_recv_msg", as it waits for tasks - otherwise a deadlock can occur. 1986d10e4ef2Snarayan */ 1987d10e4ef2Snarayan static void 1988d10e4ef2Snarayan vd_reset_if_needed(vd_t *vd) 1989d10e4ef2Snarayan { 1990d10e4ef2Snarayan int status = 0; 1991d10e4ef2Snarayan 1992d10e4ef2Snarayan mutex_enter(&vd->lock); 1993d10e4ef2Snarayan if (!vd->reset_state) { 1994d10e4ef2Snarayan ASSERT(!vd->reset_ldc); 1995d10e4ef2Snarayan mutex_exit(&vd->lock); 1996d10e4ef2Snarayan return; 1997d10e4ef2Snarayan } 1998d10e4ef2Snarayan mutex_exit(&vd->lock); 1999d10e4ef2Snarayan 2000d10e4ef2Snarayan PR0("Resetting connection state with %s", VD_CLIENT(vd)); 2001d10e4ef2Snarayan 2002d10e4ef2Snarayan /* 2003d10e4ef2Snarayan * Let any asynchronous I/O complete before possibly pulling the rug 2004d10e4ef2Snarayan * out from under it; defer checking vd->reset_ldc, as one of the 2005d10e4ef2Snarayan * asynchronous tasks might set it 2006d10e4ef2Snarayan */ 2007d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 2008d10e4ef2Snarayan 20093c96341aSnarayan if (vd->file) { 2010da6c28aaSamw status = VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL); 20113c96341aSnarayan if (status) { 20123c96341aSnarayan PR0("VOP_FSYNC returned errno %d", status); 20133c96341aSnarayan } 20143c96341aSnarayan } 20153c96341aSnarayan 2016d10e4ef2Snarayan if ((vd->initialized & VD_DRING) && 2017d10e4ef2Snarayan ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0)) 20183af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 2019d10e4ef2Snarayan 20203af08d82Slm66018 vd_free_dring_task(vd); 20213af08d82Slm66018 20223af08d82Slm66018 /* Free the staging buffer for msgs */ 20233af08d82Slm66018 if (vd->vio_msgp != NULL) { 20243af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 20253af08d82Slm66018 vd->vio_msgp = NULL; 2026d10e4ef2Snarayan } 2027d10e4ef2Snarayan 20283af08d82Slm66018 /* Free the inband message buffer */ 20293af08d82Slm66018 if (vd->inband_task.msg != NULL) { 20303af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 20313af08d82Slm66018 vd->inband_task.msg = NULL; 20323af08d82Slm66018 } 2033d10e4ef2Snarayan 2034d10e4ef2Snarayan mutex_enter(&vd->lock); 20353af08d82Slm66018 20363af08d82Slm66018 if (vd->reset_ldc) 20373af08d82Slm66018 PR0("taking down LDC channel"); 2038e1ebb9ecSlm66018 if (vd->reset_ldc && ((status = ldc_down(vd->ldc_handle)) != 0)) 20393af08d82Slm66018 PR0("ldc_down() returned errno %d", status); 2040d10e4ef2Snarayan 20412f5224aeSachartre /* Reset exclusive access rights */ 20422f5224aeSachartre vd_reset_access(vd); 20432f5224aeSachartre 2044d10e4ef2Snarayan vd->initialized &= ~(VD_SID | VD_SEQ_NUM | VD_DRING); 2045d10e4ef2Snarayan vd->state = VD_STATE_INIT; 2046d10e4ef2Snarayan vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 2047d10e4ef2Snarayan 20483af08d82Slm66018 /* Allocate the staging buffer */ 20493af08d82Slm66018 vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 20503af08d82Slm66018 20513af08d82Slm66018 PR0("calling ldc_up\n"); 20523af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 20533af08d82Slm66018 2054d10e4ef2Snarayan vd->reset_state = B_FALSE; 2055d10e4ef2Snarayan vd->reset_ldc = B_FALSE; 20563af08d82Slm66018 2057d10e4ef2Snarayan mutex_exit(&vd->lock); 2058d10e4ef2Snarayan } 2059d10e4ef2Snarayan 20603af08d82Slm66018 static void vd_recv_msg(void *arg); 20613af08d82Slm66018 20623af08d82Slm66018 static void 20633af08d82Slm66018 vd_mark_in_reset(vd_t *vd) 20643af08d82Slm66018 { 20653af08d82Slm66018 int status; 20663af08d82Slm66018 20673af08d82Slm66018 PR0("vd_mark_in_reset: marking vd in reset\n"); 20683af08d82Slm66018 20693af08d82Slm66018 vd_need_reset(vd, B_FALSE); 20703af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, DDI_SLEEP); 20713af08d82Slm66018 if (status == DDI_FAILURE) { 20723af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 20733af08d82Slm66018 vd_need_reset(vd, B_TRUE); 20743af08d82Slm66018 return; 20753af08d82Slm66018 } 20763af08d82Slm66018 } 20773af08d82Slm66018 2078d10e4ef2Snarayan static int 20793c96341aSnarayan vd_mark_elem_done(vd_t *vd, int idx, int elem_status, int elem_nbytes) 2080d10e4ef2Snarayan { 2081d10e4ef2Snarayan boolean_t accepted; 2082d10e4ef2Snarayan int status; 2083bbfa0259Sha137994 on_trap_data_t otd; 2084d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 2085d10e4ef2Snarayan 20863af08d82Slm66018 if (vd->reset_state) 20873af08d82Slm66018 return (0); 2088d10e4ef2Snarayan 2089d10e4ef2Snarayan /* Acquire the element */ 2090bbfa0259Sha137994 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype, 2091bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 20923af08d82Slm66018 if (status == ECONNRESET) { 20933af08d82Slm66018 vd_mark_in_reset(vd); 20943af08d82Slm66018 return (0); 20953af08d82Slm66018 } else { 2096d10e4ef2Snarayan return (status); 2097d10e4ef2Snarayan } 20983af08d82Slm66018 } 2099d10e4ef2Snarayan 2100d10e4ef2Snarayan /* Set the element's status and mark it done */ 2101d10e4ef2Snarayan accepted = (elem->hdr.dstate == VIO_DESC_ACCEPTED); 2102d10e4ef2Snarayan if (accepted) { 21033c96341aSnarayan elem->payload.nbytes = elem_nbytes; 2104d10e4ef2Snarayan elem->payload.status = elem_status; 2105d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_DONE; 2106d10e4ef2Snarayan } else { 2107d10e4ef2Snarayan /* Perhaps client timed out waiting for I/O... */ 21083af08d82Slm66018 PR0("element %u no longer \"accepted\"", idx); 2109d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 2110d10e4ef2Snarayan } 2111d10e4ef2Snarayan /* Release the element */ 2112bbfa0259Sha137994 if ((status = VIO_DRING_RELEASE(vd->dring_mtype, 2113bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 21143af08d82Slm66018 if (status == ECONNRESET) { 21153af08d82Slm66018 vd_mark_in_reset(vd); 21163af08d82Slm66018 return (0); 21173af08d82Slm66018 } else { 2118bbfa0259Sha137994 PR0("VIO_DRING_RELEASE() returned errno %d", 21193af08d82Slm66018 status); 2120d10e4ef2Snarayan return (status); 2121d10e4ef2Snarayan } 21223af08d82Slm66018 } 2123d10e4ef2Snarayan 2124d10e4ef2Snarayan return (accepted ? 0 : EINVAL); 2125d10e4ef2Snarayan } 2126d10e4ef2Snarayan 2127205eeb1aSlm66018 /* 2128205eeb1aSlm66018 * Return Values 2129205eeb1aSlm66018 * 0 - operation completed successfully 2130205eeb1aSlm66018 * EIO - encountered LDC / task error 2131205eeb1aSlm66018 * 2132205eeb1aSlm66018 * Side Effect 2133205eeb1aSlm66018 * sets request->status = <disk operation status> 2134205eeb1aSlm66018 */ 2135205eeb1aSlm66018 static int 2136205eeb1aSlm66018 vd_complete_bio(vd_task_t *task) 2137d10e4ef2Snarayan { 2138d10e4ef2Snarayan int status = 0; 2139205eeb1aSlm66018 int rv = 0; 2140d10e4ef2Snarayan vd_t *vd = task->vd; 2141d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 2142d10e4ef2Snarayan struct buf *buf = &task->buf; 2143d10e4ef2Snarayan 2144d10e4ef2Snarayan 2145d10e4ef2Snarayan ASSERT(vd != NULL); 2146d10e4ef2Snarayan ASSERT(request != NULL); 2147d10e4ef2Snarayan ASSERT(task->msg != NULL); 2148d10e4ef2Snarayan ASSERT(task->msglen >= sizeof (*task->msg)); 21493c96341aSnarayan ASSERT(!vd->file); 2150bae9e67eSachartre ASSERT(request->slice != VD_SLICE_NONE || (!vd_slice_single_slice && 2151bae9e67eSachartre vd->vdisk_type == VD_DISK_TYPE_SLICE)); 2152d10e4ef2Snarayan 2153205eeb1aSlm66018 /* Wait for the I/O to complete [ call to ldi_strategy(9f) ] */ 2154d10e4ef2Snarayan request->status = biowait(buf); 2155d10e4ef2Snarayan 2156bae9e67eSachartre /* Update the number of bytes read/written */ 2157bae9e67eSachartre request->nbytes += buf->b_bcount - buf->b_resid; 21583c96341aSnarayan 21594bac2208Snarayan /* Release the buffer */ 21603af08d82Slm66018 if (!vd->reset_state) 2161bae9e67eSachartre status = ldc_mem_release(task->mhdl, 0, buf->b_bufsize); 21624bac2208Snarayan if (status) { 21633af08d82Slm66018 PR0("ldc_mem_release() returned errno %d copying to " 21643af08d82Slm66018 "client", status); 21653af08d82Slm66018 if (status == ECONNRESET) { 21663af08d82Slm66018 vd_mark_in_reset(vd); 21673af08d82Slm66018 } 2168205eeb1aSlm66018 rv = EIO; 21691ae08745Sheppo } 21701ae08745Sheppo 21713af08d82Slm66018 /* Unmap the memory, even if in reset */ 21724bac2208Snarayan status = ldc_mem_unmap(task->mhdl); 21734bac2208Snarayan if (status) { 21743af08d82Slm66018 PR0("ldc_mem_unmap() returned errno %d copying to client", 21754bac2208Snarayan status); 21763af08d82Slm66018 if (status == ECONNRESET) { 21773af08d82Slm66018 vd_mark_in_reset(vd); 21783af08d82Slm66018 } 2179205eeb1aSlm66018 rv = EIO; 21804bac2208Snarayan } 21814bac2208Snarayan 2182d10e4ef2Snarayan biofini(buf); 21831ae08745Sheppo 2184205eeb1aSlm66018 return (rv); 2185205eeb1aSlm66018 } 2186205eeb1aSlm66018 2187205eeb1aSlm66018 /* 2188205eeb1aSlm66018 * Description: 2189205eeb1aSlm66018 * This function is called by the two functions called by a taskq 2190205eeb1aSlm66018 * [ vd_complete_notify() and vd_serial_notify()) ] to send the 2191205eeb1aSlm66018 * message to the client. 2192205eeb1aSlm66018 * 2193205eeb1aSlm66018 * Parameters: 2194205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2195205eeb1aSlm66018 * 2196205eeb1aSlm66018 * Return Values 2197205eeb1aSlm66018 * None 2198205eeb1aSlm66018 */ 2199205eeb1aSlm66018 static void 2200205eeb1aSlm66018 vd_notify(vd_task_t *task) 2201205eeb1aSlm66018 { 2202205eeb1aSlm66018 int status; 2203205eeb1aSlm66018 2204205eeb1aSlm66018 ASSERT(task != NULL); 2205205eeb1aSlm66018 ASSERT(task->vd != NULL); 2206205eeb1aSlm66018 2207205eeb1aSlm66018 /* 2208205eeb1aSlm66018 * Send the "ack" or "nack" back to the client; if sending the message 2209205eeb1aSlm66018 * via LDC fails, arrange to reset both the connection state and LDC 2210205eeb1aSlm66018 * itself 2211205eeb1aSlm66018 */ 2212205eeb1aSlm66018 PR2("Sending %s", 2213205eeb1aSlm66018 (task->msg->tag.vio_subtype == VIO_SUBTYPE_ACK) ? "ACK" : "NACK"); 2214205eeb1aSlm66018 2215205eeb1aSlm66018 status = send_msg(task->vd->ldc_handle, task->msg, task->msglen); 2216205eeb1aSlm66018 switch (status) { 2217205eeb1aSlm66018 case 0: 2218205eeb1aSlm66018 break; 2219205eeb1aSlm66018 case ECONNRESET: 2220205eeb1aSlm66018 vd_mark_in_reset(task->vd); 2221205eeb1aSlm66018 break; 2222205eeb1aSlm66018 default: 2223205eeb1aSlm66018 PR0("initiating full reset"); 2224205eeb1aSlm66018 vd_need_reset(task->vd, B_TRUE); 2225205eeb1aSlm66018 break; 2226205eeb1aSlm66018 } 2227205eeb1aSlm66018 2228205eeb1aSlm66018 DTRACE_PROBE1(task__end, vd_task_t *, task); 2229205eeb1aSlm66018 } 2230205eeb1aSlm66018 2231205eeb1aSlm66018 /* 2232205eeb1aSlm66018 * Description: 2233205eeb1aSlm66018 * Mark the Dring entry as Done and (if necessary) send an ACK/NACK to 2234205eeb1aSlm66018 * the vDisk client 2235205eeb1aSlm66018 * 2236205eeb1aSlm66018 * Parameters: 2237205eeb1aSlm66018 * task - structure containing the request sent from client 2238205eeb1aSlm66018 * 2239205eeb1aSlm66018 * Return Values 2240205eeb1aSlm66018 * None 2241205eeb1aSlm66018 */ 2242205eeb1aSlm66018 static void 2243205eeb1aSlm66018 vd_complete_notify(vd_task_t *task) 2244205eeb1aSlm66018 { 2245205eeb1aSlm66018 int status = 0; 2246205eeb1aSlm66018 vd_t *vd = task->vd; 2247205eeb1aSlm66018 vd_dring_payload_t *request = task->request; 2248205eeb1aSlm66018 2249d10e4ef2Snarayan /* Update the dring element for a dring client */ 2250f0ca1d9aSsb155480 if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 22513c96341aSnarayan status = vd_mark_elem_done(vd, task->index, 22523c96341aSnarayan request->status, request->nbytes); 22533af08d82Slm66018 if (status == ECONNRESET) 22543af08d82Slm66018 vd_mark_in_reset(vd); 2255bbfa0259Sha137994 else if (status == EACCES) 2256bbfa0259Sha137994 vd_need_reset(vd, B_TRUE); 22573af08d82Slm66018 } 22581ae08745Sheppo 2259d10e4ef2Snarayan /* 2260205eeb1aSlm66018 * If a transport error occurred while marking the element done or 2261205eeb1aSlm66018 * previously while executing the task, arrange to "nack" the message 2262205eeb1aSlm66018 * when the final task in the descriptor element range completes 2263d10e4ef2Snarayan */ 2264205eeb1aSlm66018 if ((status != 0) || (task->status != 0)) 2265d10e4ef2Snarayan task->msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 22661ae08745Sheppo 2267d10e4ef2Snarayan /* 2268d10e4ef2Snarayan * Only the final task for a range of elements will respond to and 2269d10e4ef2Snarayan * free the message 2270d10e4ef2Snarayan */ 22713af08d82Slm66018 if (task->type == VD_NONFINAL_RANGE_TASK) { 2272d10e4ef2Snarayan return; 22733af08d82Slm66018 } 22741ae08745Sheppo 227527ac699dSzk194757 /* 227627ac699dSzk194757 * We should only send an ACK/NACK here if we are not currently in 227727ac699dSzk194757 * reset as, depending on how we reset, the dring may have been 227827ac699dSzk194757 * blown away and we don't want to ACK/NACK a message that isn't 227927ac699dSzk194757 * there. 228027ac699dSzk194757 */ 228127ac699dSzk194757 if (!vd->reset_state) 2282205eeb1aSlm66018 vd_notify(task); 2283205eeb1aSlm66018 } 2284205eeb1aSlm66018 2285d10e4ef2Snarayan /* 2286205eeb1aSlm66018 * Description: 2287205eeb1aSlm66018 * This is the basic completion function called to handle inband data 2288205eeb1aSlm66018 * requests and handshake messages. All it needs to do is trigger a 2289205eeb1aSlm66018 * message to the client that the request is completed. 2290205eeb1aSlm66018 * 2291205eeb1aSlm66018 * Parameters: 2292205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 2293205eeb1aSlm66018 * 2294205eeb1aSlm66018 * Return Values 2295205eeb1aSlm66018 * None 2296d10e4ef2Snarayan */ 2297205eeb1aSlm66018 static void 2298205eeb1aSlm66018 vd_serial_notify(void *arg) 2299205eeb1aSlm66018 { 2300205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 2301205eeb1aSlm66018 2302205eeb1aSlm66018 ASSERT(task != NULL); 2303205eeb1aSlm66018 vd_notify(task); 23041ae08745Sheppo } 23051ae08745Sheppo 23062f5224aeSachartre /* ARGSUSED */ 23072f5224aeSachartre static int 23082f5224aeSachartre vd_geom2dk_geom(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23090a55fbb7Slm66018 { 23100a55fbb7Slm66018 VD_GEOM2DK_GEOM((vd_geom_t *)vd_buf, (struct dk_geom *)ioctl_arg); 23112f5224aeSachartre return (0); 23120a55fbb7Slm66018 } 23130a55fbb7Slm66018 23142f5224aeSachartre /* ARGSUSED */ 23152f5224aeSachartre static int 23162f5224aeSachartre vd_vtoc2vtoc(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23170a55fbb7Slm66018 { 23180a55fbb7Slm66018 VD_VTOC2VTOC((vd_vtoc_t *)vd_buf, (struct vtoc *)ioctl_arg); 23192f5224aeSachartre return (0); 23200a55fbb7Slm66018 } 23210a55fbb7Slm66018 23220a55fbb7Slm66018 static void 23230a55fbb7Slm66018 dk_geom2vd_geom(void *ioctl_arg, void *vd_buf) 23240a55fbb7Slm66018 { 23250a55fbb7Slm66018 DK_GEOM2VD_GEOM((struct dk_geom *)ioctl_arg, (vd_geom_t *)vd_buf); 23260a55fbb7Slm66018 } 23270a55fbb7Slm66018 23280a55fbb7Slm66018 static void 23290a55fbb7Slm66018 vtoc2vd_vtoc(void *ioctl_arg, void *vd_buf) 23300a55fbb7Slm66018 { 23310a55fbb7Slm66018 VTOC2VD_VTOC((struct vtoc *)ioctl_arg, (vd_vtoc_t *)vd_buf); 23320a55fbb7Slm66018 } 23330a55fbb7Slm66018 23342f5224aeSachartre static int 23352f5224aeSachartre vd_get_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23364bac2208Snarayan { 23374bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23384bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23392f5224aeSachartre size_t data_len; 23402f5224aeSachartre 23412f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 23422f5224aeSachartre if (vd_efi->length > data_len) 23432f5224aeSachartre return (EINVAL); 23444bac2208Snarayan 23454bac2208Snarayan dk_efi->dki_lba = vd_efi->lba; 23464bac2208Snarayan dk_efi->dki_length = vd_efi->length; 23474bac2208Snarayan dk_efi->dki_data = kmem_zalloc(vd_efi->length, KM_SLEEP); 23482f5224aeSachartre return (0); 23494bac2208Snarayan } 23504bac2208Snarayan 23514bac2208Snarayan static void 23524bac2208Snarayan vd_get_efi_out(void *ioctl_arg, void *vd_buf) 23534bac2208Snarayan { 23544bac2208Snarayan int len; 23554bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23564bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23574bac2208Snarayan 23584bac2208Snarayan len = vd_efi->length; 23594bac2208Snarayan DK_EFI2VD_EFI(dk_efi, vd_efi); 23604bac2208Snarayan kmem_free(dk_efi->dki_data, len); 23614bac2208Snarayan } 23624bac2208Snarayan 23632f5224aeSachartre static int 23642f5224aeSachartre vd_set_efi_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23654bac2208Snarayan { 23664bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23674bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23682f5224aeSachartre size_t data_len; 23692f5224aeSachartre 23702f5224aeSachartre data_len = vd_buf_len - (sizeof (vd_efi_t) - sizeof (uint64_t)); 23712f5224aeSachartre if (vd_efi->length > data_len) 23722f5224aeSachartre return (EINVAL); 23734bac2208Snarayan 23744bac2208Snarayan dk_efi->dki_data = kmem_alloc(vd_efi->length, KM_SLEEP); 23754bac2208Snarayan VD_EFI2DK_EFI(vd_efi, dk_efi); 23762f5224aeSachartre return (0); 23774bac2208Snarayan } 23784bac2208Snarayan 23794bac2208Snarayan static void 23804bac2208Snarayan vd_set_efi_out(void *ioctl_arg, void *vd_buf) 23814bac2208Snarayan { 23824bac2208Snarayan vd_efi_t *vd_efi = (vd_efi_t *)vd_buf; 23834bac2208Snarayan dk_efi_t *dk_efi = (dk_efi_t *)ioctl_arg; 23844bac2208Snarayan 23854bac2208Snarayan kmem_free(dk_efi->dki_data, vd_efi->length); 23864bac2208Snarayan } 23874bac2208Snarayan 23882f5224aeSachartre static int 23892f5224aeSachartre vd_scsicmd_in(void *vd_buf, size_t vd_buf_len, void *ioctl_arg) 23902f5224aeSachartre { 23912f5224aeSachartre size_t vd_scsi_len; 23922f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 23932f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 23942f5224aeSachartre 23952f5224aeSachartre /* check buffer size */ 23962f5224aeSachartre vd_scsi_len = VD_SCSI_SIZE; 23972f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->cdb_len, sizeof (uint64_t)); 23982f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->sense_len, sizeof (uint64_t)); 23992f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->datain_len, sizeof (uint64_t)); 24002f5224aeSachartre vd_scsi_len += P2ROUNDUP(vd_scsi->dataout_len, sizeof (uint64_t)); 24012f5224aeSachartre 24022f5224aeSachartre ASSERT(vd_scsi_len % sizeof (uint64_t) == 0); 24032f5224aeSachartre 24042f5224aeSachartre if (vd_buf_len < vd_scsi_len) 24052f5224aeSachartre return (EINVAL); 24062f5224aeSachartre 24072f5224aeSachartre /* set flags */ 24082f5224aeSachartre uscsi->uscsi_flags = vd_scsi_debug; 24092f5224aeSachartre 24102f5224aeSachartre if (vd_scsi->options & VD_SCSI_OPT_NORETRY) { 24112f5224aeSachartre uscsi->uscsi_flags |= USCSI_ISOLATE; 24122f5224aeSachartre uscsi->uscsi_flags |= USCSI_DIAGNOSE; 24132f5224aeSachartre } 24142f5224aeSachartre 24152f5224aeSachartre /* task attribute */ 24162f5224aeSachartre switch (vd_scsi->task_attribute) { 24172f5224aeSachartre case VD_SCSI_TASK_ACA: 24182f5224aeSachartre uscsi->uscsi_flags |= USCSI_HEAD; 24192f5224aeSachartre break; 24202f5224aeSachartre case VD_SCSI_TASK_HQUEUE: 24212f5224aeSachartre uscsi->uscsi_flags |= USCSI_HTAG; 24222f5224aeSachartre break; 24232f5224aeSachartre case VD_SCSI_TASK_ORDERED: 24242f5224aeSachartre uscsi->uscsi_flags |= USCSI_OTAG; 24252f5224aeSachartre break; 24262f5224aeSachartre default: 24272f5224aeSachartre uscsi->uscsi_flags |= USCSI_NOTAG; 24282f5224aeSachartre break; 24292f5224aeSachartre } 24302f5224aeSachartre 24312f5224aeSachartre /* timeout */ 24322f5224aeSachartre uscsi->uscsi_timeout = vd_scsi->timeout; 24332f5224aeSachartre 24342f5224aeSachartre /* cdb data */ 24352f5224aeSachartre uscsi->uscsi_cdb = (caddr_t)VD_SCSI_DATA_CDB(vd_scsi); 24362f5224aeSachartre uscsi->uscsi_cdblen = vd_scsi->cdb_len; 24372f5224aeSachartre 24382f5224aeSachartre /* sense buffer */ 24392f5224aeSachartre if (vd_scsi->sense_len != 0) { 24402f5224aeSachartre uscsi->uscsi_flags |= USCSI_RQENABLE; 24412f5224aeSachartre uscsi->uscsi_rqbuf = (caddr_t)VD_SCSI_DATA_SENSE(vd_scsi); 24422f5224aeSachartre uscsi->uscsi_rqlen = vd_scsi->sense_len; 24432f5224aeSachartre } 24442f5224aeSachartre 24452f5224aeSachartre if (vd_scsi->datain_len != 0 && vd_scsi->dataout_len != 0) { 24462f5224aeSachartre /* uscsi does not support read/write request */ 24472f5224aeSachartre return (EINVAL); 24482f5224aeSachartre } 24492f5224aeSachartre 24502f5224aeSachartre /* request data-in */ 24512f5224aeSachartre if (vd_scsi->datain_len != 0) { 24522f5224aeSachartre uscsi->uscsi_flags |= USCSI_READ; 24532f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->datain_len; 24542f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_IN(vd_scsi); 24552f5224aeSachartre } 24562f5224aeSachartre 24572f5224aeSachartre /* request data-out */ 24582f5224aeSachartre if (vd_scsi->dataout_len != 0) { 24592f5224aeSachartre uscsi->uscsi_buflen = vd_scsi->dataout_len; 24602f5224aeSachartre uscsi->uscsi_bufaddr = (char *)VD_SCSI_DATA_OUT(vd_scsi); 24612f5224aeSachartre } 24622f5224aeSachartre 24632f5224aeSachartre return (0); 24642f5224aeSachartre } 24652f5224aeSachartre 24662f5224aeSachartre static void 24672f5224aeSachartre vd_scsicmd_out(void *ioctl_arg, void *vd_buf) 24682f5224aeSachartre { 24692f5224aeSachartre vd_scsi_t *vd_scsi = (vd_scsi_t *)vd_buf; 24702f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl_arg; 24712f5224aeSachartre 24722f5224aeSachartre /* output fields */ 24732f5224aeSachartre vd_scsi->cmd_status = uscsi->uscsi_status; 24742f5224aeSachartre 24752f5224aeSachartre /* sense data */ 24762f5224aeSachartre if ((uscsi->uscsi_flags & USCSI_RQENABLE) && 24772f5224aeSachartre (uscsi->uscsi_status == STATUS_CHECK || 24782f5224aeSachartre uscsi->uscsi_status == STATUS_TERMINATED)) { 24792f5224aeSachartre vd_scsi->sense_status = uscsi->uscsi_rqstatus; 24802f5224aeSachartre if (uscsi->uscsi_rqstatus == STATUS_GOOD) 248114466a20Szk194757 vd_scsi->sense_len -= uscsi->uscsi_rqresid; 24822f5224aeSachartre else 24832f5224aeSachartre vd_scsi->sense_len = 0; 24842f5224aeSachartre } else { 24852f5224aeSachartre vd_scsi->sense_len = 0; 24862f5224aeSachartre } 24872f5224aeSachartre 24882f5224aeSachartre if (uscsi->uscsi_status != STATUS_GOOD) { 24892f5224aeSachartre vd_scsi->dataout_len = 0; 24902f5224aeSachartre vd_scsi->datain_len = 0; 24912f5224aeSachartre return; 24922f5224aeSachartre } 24932f5224aeSachartre 24942f5224aeSachartre if (uscsi->uscsi_flags & USCSI_READ) { 24952f5224aeSachartre /* request data (read) */ 24962f5224aeSachartre vd_scsi->datain_len -= uscsi->uscsi_resid; 24972f5224aeSachartre vd_scsi->dataout_len = 0; 24982f5224aeSachartre } else { 24992f5224aeSachartre /* request data (write) */ 25002f5224aeSachartre vd_scsi->datain_len = 0; 25012f5224aeSachartre vd_scsi->dataout_len -= uscsi->uscsi_resid; 25022f5224aeSachartre } 25032f5224aeSachartre } 25042f5224aeSachartre 2505690555a1Sachartre static ushort_t 25063c96341aSnarayan vd_lbl2cksum(struct dk_label *label) 25073c96341aSnarayan { 25083c96341aSnarayan int count; 2509690555a1Sachartre ushort_t sum, *sp; 25103c96341aSnarayan 25113c96341aSnarayan count = (sizeof (struct dk_label)) / (sizeof (short)) - 1; 2512690555a1Sachartre sp = (ushort_t *)label; 25133c96341aSnarayan sum = 0; 25143c96341aSnarayan while (count--) { 25153c96341aSnarayan sum ^= *sp++; 25163c96341aSnarayan } 25173c96341aSnarayan 25183c96341aSnarayan return (sum); 25193c96341aSnarayan } 25203c96341aSnarayan 252187a7269eSachartre /* 2522bae9e67eSachartre * Copy information from a vtoc and dk_geom structures to a dk_label structure. 2523bae9e67eSachartre */ 2524bae9e67eSachartre static void 2525bae9e67eSachartre vd_vtocgeom_to_label(struct vtoc *vtoc, struct dk_geom *geom, 2526bae9e67eSachartre struct dk_label *label) 2527bae9e67eSachartre { 2528bae9e67eSachartre int i; 2529bae9e67eSachartre 2530bae9e67eSachartre ASSERT(vtoc->v_nparts == V_NUMPAR); 2531bae9e67eSachartre ASSERT(vtoc->v_sanity == VTOC_SANE); 2532bae9e67eSachartre 2533bae9e67eSachartre bzero(label, sizeof (struct dk_label)); 2534bae9e67eSachartre 2535bae9e67eSachartre label->dkl_ncyl = geom->dkg_ncyl; 2536bae9e67eSachartre label->dkl_acyl = geom->dkg_acyl; 2537bae9e67eSachartre label->dkl_pcyl = geom->dkg_pcyl; 2538bae9e67eSachartre label->dkl_nhead = geom->dkg_nhead; 2539bae9e67eSachartre label->dkl_nsect = geom->dkg_nsect; 2540bae9e67eSachartre label->dkl_intrlv = geom->dkg_intrlv; 2541bae9e67eSachartre label->dkl_apc = geom->dkg_apc; 2542bae9e67eSachartre label->dkl_rpm = geom->dkg_rpm; 2543bae9e67eSachartre label->dkl_write_reinstruct = geom->dkg_write_reinstruct; 2544bae9e67eSachartre label->dkl_read_reinstruct = geom->dkg_read_reinstruct; 2545bae9e67eSachartre 2546bae9e67eSachartre label->dkl_vtoc.v_nparts = V_NUMPAR; 2547bae9e67eSachartre label->dkl_vtoc.v_sanity = VTOC_SANE; 2548bae9e67eSachartre label->dkl_vtoc.v_version = vtoc->v_version; 2549bae9e67eSachartre for (i = 0; i < V_NUMPAR; i++) { 2550bae9e67eSachartre label->dkl_vtoc.v_timestamp[i] = vtoc->timestamp[i]; 2551bae9e67eSachartre label->dkl_vtoc.v_part[i].p_tag = vtoc->v_part[i].p_tag; 2552bae9e67eSachartre label->dkl_vtoc.v_part[i].p_flag = vtoc->v_part[i].p_flag; 2553bae9e67eSachartre label->dkl_map[i].dkl_cylno = vtoc->v_part[i].p_start / 2554bae9e67eSachartre (label->dkl_nhead * label->dkl_nsect); 2555bae9e67eSachartre label->dkl_map[i].dkl_nblk = vtoc->v_part[i].p_size; 2556bae9e67eSachartre } 2557bae9e67eSachartre 2558bae9e67eSachartre /* 2559bae9e67eSachartre * The bootinfo array can not be copied with bcopy() because 2560bae9e67eSachartre * elements are of type long in vtoc (so 64-bit) and of type 2561bae9e67eSachartre * int in dk_vtoc (so 32-bit). 2562bae9e67eSachartre */ 2563bae9e67eSachartre label->dkl_vtoc.v_bootinfo[0] = vtoc->v_bootinfo[0]; 2564bae9e67eSachartre label->dkl_vtoc.v_bootinfo[1] = vtoc->v_bootinfo[1]; 2565bae9e67eSachartre label->dkl_vtoc.v_bootinfo[2] = vtoc->v_bootinfo[2]; 2566bae9e67eSachartre bcopy(vtoc->v_asciilabel, label->dkl_asciilabel, LEN_DKL_ASCII); 2567bae9e67eSachartre bcopy(vtoc->v_volume, label->dkl_vtoc.v_volume, LEN_DKL_VVOL); 2568bae9e67eSachartre 2569bae9e67eSachartre /* re-compute checksum */ 2570bae9e67eSachartre label->dkl_magic = DKL_MAGIC; 2571bae9e67eSachartre label->dkl_cksum = vd_lbl2cksum(label); 2572bae9e67eSachartre } 2573bae9e67eSachartre 2574bae9e67eSachartre /* 2575bae9e67eSachartre * Copy information from a dk_label structure to a vtoc and dk_geom structures. 2576bae9e67eSachartre */ 2577bae9e67eSachartre static void 2578bae9e67eSachartre vd_label_to_vtocgeom(struct dk_label *label, struct vtoc *vtoc, 2579bae9e67eSachartre struct dk_geom *geom) 2580bae9e67eSachartre { 2581bae9e67eSachartre int i; 2582bae9e67eSachartre 2583bae9e67eSachartre bzero(vtoc, sizeof (struct vtoc)); 2584bae9e67eSachartre bzero(geom, sizeof (struct dk_geom)); 2585bae9e67eSachartre 2586bae9e67eSachartre geom->dkg_ncyl = label->dkl_ncyl; 2587bae9e67eSachartre geom->dkg_acyl = label->dkl_acyl; 2588bae9e67eSachartre geom->dkg_nhead = label->dkl_nhead; 2589bae9e67eSachartre geom->dkg_nsect = label->dkl_nsect; 2590bae9e67eSachartre geom->dkg_intrlv = label->dkl_intrlv; 2591bae9e67eSachartre geom->dkg_apc = label->dkl_apc; 2592bae9e67eSachartre geom->dkg_rpm = label->dkl_rpm; 2593bae9e67eSachartre geom->dkg_pcyl = label->dkl_pcyl; 2594bae9e67eSachartre geom->dkg_write_reinstruct = label->dkl_write_reinstruct; 2595bae9e67eSachartre geom->dkg_read_reinstruct = label->dkl_read_reinstruct; 2596bae9e67eSachartre 2597bae9e67eSachartre vtoc->v_sanity = label->dkl_vtoc.v_sanity; 2598bae9e67eSachartre vtoc->v_version = label->dkl_vtoc.v_version; 2599bae9e67eSachartre vtoc->v_sectorsz = DEV_BSIZE; 2600bae9e67eSachartre vtoc->v_nparts = label->dkl_vtoc.v_nparts; 2601bae9e67eSachartre 2602bae9e67eSachartre for (i = 0; i < vtoc->v_nparts; i++) { 2603bae9e67eSachartre vtoc->v_part[i].p_tag = label->dkl_vtoc.v_part[i].p_tag; 2604bae9e67eSachartre vtoc->v_part[i].p_flag = label->dkl_vtoc.v_part[i].p_flag; 2605bae9e67eSachartre vtoc->v_part[i].p_start = label->dkl_map[i].dkl_cylno * 2606bae9e67eSachartre (label->dkl_nhead * label->dkl_nsect); 2607bae9e67eSachartre vtoc->v_part[i].p_size = label->dkl_map[i].dkl_nblk; 2608bae9e67eSachartre vtoc->timestamp[i] = label->dkl_vtoc.v_timestamp[i]; 2609bae9e67eSachartre } 2610bae9e67eSachartre 2611bae9e67eSachartre /* 2612bae9e67eSachartre * The bootinfo array can not be copied with bcopy() because 2613bae9e67eSachartre * elements are of type long in vtoc (so 64-bit) and of type 2614bae9e67eSachartre * int in dk_vtoc (so 32-bit). 2615bae9e67eSachartre */ 2616bae9e67eSachartre vtoc->v_bootinfo[0] = label->dkl_vtoc.v_bootinfo[0]; 2617bae9e67eSachartre vtoc->v_bootinfo[1] = label->dkl_vtoc.v_bootinfo[1]; 2618bae9e67eSachartre vtoc->v_bootinfo[2] = label->dkl_vtoc.v_bootinfo[2]; 2619bae9e67eSachartre bcopy(label->dkl_asciilabel, vtoc->v_asciilabel, LEN_DKL_ASCII); 2620bae9e67eSachartre bcopy(label->dkl_vtoc.v_volume, vtoc->v_volume, LEN_DKL_VVOL); 2621bae9e67eSachartre } 2622bae9e67eSachartre 2623bae9e67eSachartre /* 2624bae9e67eSachartre * Check if a geometry is valid for a single-slice disk. A geometry is 2625bae9e67eSachartre * considered valid if the main attributes of the geometry match with the 2626bae9e67eSachartre * attributes of the fake geometry we have created. 2627bae9e67eSachartre */ 2628bae9e67eSachartre static boolean_t 2629bae9e67eSachartre vd_slice_geom_isvalid(vd_t *vd, struct dk_geom *geom) 2630bae9e67eSachartre { 2631bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2632bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2633bae9e67eSachartre 2634bae9e67eSachartre if (geom->dkg_ncyl != vd->dk_geom.dkg_ncyl || 2635bae9e67eSachartre geom->dkg_acyl != vd->dk_geom.dkg_acyl || 2636bae9e67eSachartre geom->dkg_nsect != vd->dk_geom.dkg_nsect || 2637bae9e67eSachartre geom->dkg_pcyl != vd->dk_geom.dkg_pcyl) 2638bae9e67eSachartre return (B_FALSE); 2639bae9e67eSachartre 2640bae9e67eSachartre return (B_TRUE); 2641bae9e67eSachartre } 2642bae9e67eSachartre 2643bae9e67eSachartre /* 2644bae9e67eSachartre * Check if a vtoc is valid for a single-slice disk. A vtoc is considered 2645bae9e67eSachartre * valid if the main attributes of the vtoc match with the attributes of the 2646bae9e67eSachartre * fake vtoc we have created. 2647bae9e67eSachartre */ 2648bae9e67eSachartre static boolean_t 2649bae9e67eSachartre vd_slice_vtoc_isvalid(vd_t *vd, struct vtoc *vtoc) 2650bae9e67eSachartre { 2651bae9e67eSachartre size_t csize; 2652bae9e67eSachartre int i; 2653bae9e67eSachartre 2654bae9e67eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2655bae9e67eSachartre ASSERT(vd->vdisk_label == VD_DISK_LABEL_VTOC); 2656bae9e67eSachartre 2657bae9e67eSachartre if (vtoc->v_sanity != vd->vtoc.v_sanity || 2658bae9e67eSachartre vtoc->v_version != vd->vtoc.v_version || 2659bae9e67eSachartre vtoc->v_nparts != vd->vtoc.v_nparts || 2660bae9e67eSachartre strcmp(vtoc->v_volume, vd->vtoc.v_volume) != 0 || 2661bae9e67eSachartre strcmp(vtoc->v_asciilabel, vd->vtoc.v_asciilabel) != 0) 2662bae9e67eSachartre return (B_FALSE); 2663bae9e67eSachartre 2664bae9e67eSachartre /* slice 2 should be unchanged */ 2665bae9e67eSachartre if (vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_start != 2666bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start || 2667bae9e67eSachartre vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size != 2668bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size) 2669bae9e67eSachartre return (B_FALSE); 2670bae9e67eSachartre 2671bae9e67eSachartre /* 2672bae9e67eSachartre * Slice 0 should be mostly unchanged and cover most of the disk. 2673bae9e67eSachartre * However we allow some flexibility wrt to the start and the size 2674bae9e67eSachartre * of this slice mainly because we can't exactly know how it will 2675bae9e67eSachartre * be defined by the OS installer. 2676bae9e67eSachartre * 2677bae9e67eSachartre * We allow slice 0 to be defined as starting on any of the first 2678bae9e67eSachartre * 4 cylinders. 2679bae9e67eSachartre */ 2680bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 2681bae9e67eSachartre 2682bae9e67eSachartre if (vtoc->v_part[0].p_start > 4 * csize || 2683bae9e67eSachartre vtoc->v_part[0].p_size > vtoc->v_part[VD_ENTIRE_DISK_SLICE].p_size) 2684bae9e67eSachartre return (B_FALSE); 2685bae9e67eSachartre 2686bae9e67eSachartre if (vd->vtoc.v_part[0].p_size >= 4 * csize && 2687bae9e67eSachartre vtoc->v_part[0].p_size < vd->vtoc.v_part[0].p_size - 4 *csize) 2688bae9e67eSachartre return (B_FALSE); 2689bae9e67eSachartre 2690bae9e67eSachartre /* any other slice should have a size of 0 */ 2691bae9e67eSachartre for (i = 1; i < vtoc->v_nparts; i++) { 2692bae9e67eSachartre if (i != VD_ENTIRE_DISK_SLICE && 2693bae9e67eSachartre vtoc->v_part[i].p_size != 0) 2694bae9e67eSachartre return (B_FALSE); 2695bae9e67eSachartre } 2696bae9e67eSachartre 2697bae9e67eSachartre return (B_TRUE); 2698bae9e67eSachartre } 2699bae9e67eSachartre 2700bae9e67eSachartre /* 270187a7269eSachartre * Handle ioctls to a disk slice. 2702205eeb1aSlm66018 * 2703205eeb1aSlm66018 * Return Values 2704205eeb1aSlm66018 * 0 - Indicates that there are no errors in disk operations 2705205eeb1aSlm66018 * ENOTSUP - Unknown disk label type or unsupported DKIO ioctl 2706205eeb1aSlm66018 * EINVAL - Not enough room to copy the EFI label 2707205eeb1aSlm66018 * 270887a7269eSachartre */ 27091ae08745Sheppo static int 27100a55fbb7Slm66018 vd_do_slice_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 27111ae08745Sheppo { 27124bac2208Snarayan dk_efi_t *dk_ioc; 2713bae9e67eSachartre struct vtoc *vtoc; 2714bae9e67eSachartre struct dk_geom *geom; 2715bae9e67eSachartre size_t len, lba; 2716edcc0754Sachartre int rval; 2717edcc0754Sachartre 2718edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 2719edcc0754Sachartre 2720edcc0754Sachartre if (cmd == DKIOCFLUSHWRITECACHE) { 2721edcc0754Sachartre if (vd->file) { 2722edcc0754Sachartre return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 2723edcc0754Sachartre } else { 2724edcc0754Sachartre return (ldi_ioctl(vd->ldi_handle[0], cmd, 2725edcc0754Sachartre (intptr_t)ioctl_arg, vd->open_flags | FKIOCTL, 2726edcc0754Sachartre kcred, &rval)); 2727edcc0754Sachartre } 2728edcc0754Sachartre } 27294bac2208Snarayan 27304bac2208Snarayan switch (vd->vdisk_label) { 27314bac2208Snarayan 2732edcc0754Sachartre /* ioctls for a single slice disk with a VTOC label */ 27334bac2208Snarayan case VD_DISK_LABEL_VTOC: 27344bac2208Snarayan 27351ae08745Sheppo switch (cmd) { 2736bae9e67eSachartre 27371ae08745Sheppo case DKIOCGGEOM: 27380a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 27390a55fbb7Slm66018 bcopy(&vd->dk_geom, ioctl_arg, sizeof (vd->dk_geom)); 27401ae08745Sheppo return (0); 2741bae9e67eSachartre 27421ae08745Sheppo case DKIOCGVTOC: 27430a55fbb7Slm66018 ASSERT(ioctl_arg != NULL); 27440a55fbb7Slm66018 bcopy(&vd->vtoc, ioctl_arg, sizeof (vd->vtoc)); 27451ae08745Sheppo return (0); 2746bae9e67eSachartre 2747bae9e67eSachartre case DKIOCSGEOM: 2748bae9e67eSachartre ASSERT(ioctl_arg != NULL); 2749bae9e67eSachartre if (vd_slice_single_slice) 2750bae9e67eSachartre return (ENOTSUP); 2751bae9e67eSachartre 2752bae9e67eSachartre /* fake success only if new geometry is valid */ 2753bae9e67eSachartre geom = (struct dk_geom *)ioctl_arg; 2754bae9e67eSachartre if (!vd_slice_geom_isvalid(vd, geom)) 2755bae9e67eSachartre return (EINVAL); 2756bae9e67eSachartre 2757bae9e67eSachartre return (0); 2758bae9e67eSachartre 2759bae9e67eSachartre case DKIOCSVTOC: 2760bae9e67eSachartre ASSERT(ioctl_arg != NULL); 2761bae9e67eSachartre if (vd_slice_single_slice) 2762bae9e67eSachartre return (ENOTSUP); 2763bae9e67eSachartre 2764bae9e67eSachartre /* fake sucess only if the new vtoc is valid */ 2765bae9e67eSachartre vtoc = (struct vtoc *)ioctl_arg; 2766bae9e67eSachartre if (!vd_slice_vtoc_isvalid(vd, vtoc)) 2767bae9e67eSachartre return (EINVAL); 2768bae9e67eSachartre 2769bae9e67eSachartre return (0); 2770bae9e67eSachartre 277187a7269eSachartre default: 27723c96341aSnarayan return (ENOTSUP); 277387a7269eSachartre } 277487a7269eSachartre 2775edcc0754Sachartre /* ioctls for a single slice disk with an EFI label */ 277687a7269eSachartre case VD_DISK_LABEL_EFI: 277787a7269eSachartre 2778bae9e67eSachartre if (cmd != DKIOCGETEFI && cmd != DKIOCSETEFI) 2779bae9e67eSachartre return (ENOTSUP); 2780bae9e67eSachartre 27813c96341aSnarayan ASSERT(ioctl_arg != NULL); 278287a7269eSachartre dk_ioc = (dk_efi_t *)ioctl_arg; 2783edcc0754Sachartre 2784bae9e67eSachartre len = dk_ioc->dki_length; 2785bae9e67eSachartre lba = dk_ioc->dki_lba; 2786edcc0754Sachartre 2787bae9e67eSachartre if ((lba != VD_EFI_LBA_GPT && lba != VD_EFI_LBA_GPE) || 2788bae9e67eSachartre (lba == VD_EFI_LBA_GPT && len < sizeof (efi_gpt_t)) || 2789bae9e67eSachartre (lba == VD_EFI_LBA_GPE && len < sizeof (efi_gpe_t))) 279087a7269eSachartre return (EINVAL); 2791edcc0754Sachartre 2792bae9e67eSachartre switch (cmd) { 2793bae9e67eSachartre case DKIOCGETEFI: 2794bae9e67eSachartre len = vd_slice_flabel_read(vd, 2795bae9e67eSachartre (caddr_t)dk_ioc->dki_data, lba * DEV_BSIZE, len); 2796edcc0754Sachartre 2797bae9e67eSachartre ASSERT(len > 0); 2798edcc0754Sachartre 279987a7269eSachartre return (0); 2800bae9e67eSachartre 2801bae9e67eSachartre case DKIOCSETEFI: 2802bae9e67eSachartre if (vd_slice_single_slice) 280387a7269eSachartre return (ENOTSUP); 2804bae9e67eSachartre 2805bae9e67eSachartre /* we currently don't support writing EFI */ 2806bae9e67eSachartre return (EIO); 280787a7269eSachartre } 280887a7269eSachartre 280987a7269eSachartre default: 2810205eeb1aSlm66018 /* Unknown disk label type */ 281187a7269eSachartre return (ENOTSUP); 281287a7269eSachartre } 281387a7269eSachartre } 281487a7269eSachartre 2815edcc0754Sachartre static int 2816edcc0754Sachartre vds_efi_alloc_and_read(vd_t *vd, efi_gpt_t **gpt, efi_gpe_t **gpe) 2817edcc0754Sachartre { 2818edcc0754Sachartre vd_efi_dev_t edev; 2819edcc0754Sachartre int status; 2820edcc0754Sachartre 2821edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2822edcc0754Sachartre 2823edcc0754Sachartre status = vd_efi_alloc_and_read(&edev, gpt, gpe); 2824edcc0754Sachartre 2825edcc0754Sachartre return (status); 2826edcc0754Sachartre } 2827edcc0754Sachartre 2828edcc0754Sachartre static void 2829edcc0754Sachartre vds_efi_free(vd_t *vd, efi_gpt_t *gpt, efi_gpe_t *gpe) 2830edcc0754Sachartre { 2831edcc0754Sachartre vd_efi_dev_t edev; 2832edcc0754Sachartre 2833edcc0754Sachartre VD_EFI_DEV_SET(edev, vd, (vd_efi_ioctl_func)vd_backend_ioctl); 2834edcc0754Sachartre 2835edcc0754Sachartre vd_efi_free(&edev, gpt, gpe); 2836edcc0754Sachartre } 2837edcc0754Sachartre 2838edcc0754Sachartre static int 2839edcc0754Sachartre vd_file_validate_efi(vd_t *vd) 2840edcc0754Sachartre { 2841edcc0754Sachartre efi_gpt_t *gpt; 2842edcc0754Sachartre efi_gpe_t *gpe; 2843edcc0754Sachartre int i, nparts, status; 2844edcc0754Sachartre struct uuid efi_reserved = EFI_RESERVED; 2845edcc0754Sachartre 2846edcc0754Sachartre if ((status = vds_efi_alloc_and_read(vd, &gpt, &gpe)) != 0) 2847edcc0754Sachartre return (status); 2848edcc0754Sachartre 2849edcc0754Sachartre bzero(&vd->vtoc, sizeof (struct vtoc)); 2850edcc0754Sachartre bzero(&vd->dk_geom, sizeof (struct dk_geom)); 2851edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2852edcc0754Sachartre 2853edcc0754Sachartre vd->efi_reserved = -1; 2854edcc0754Sachartre 2855edcc0754Sachartre nparts = gpt->efi_gpt_NumberOfPartitionEntries; 2856edcc0754Sachartre 2857edcc0754Sachartre for (i = 0; i < nparts && i < VD_MAXPART; i++) { 2858edcc0754Sachartre 2859edcc0754Sachartre if (gpe[i].efi_gpe_StartingLBA == 0 || 2860edcc0754Sachartre gpe[i].efi_gpe_EndingLBA == 0) { 2861edcc0754Sachartre continue; 2862edcc0754Sachartre } 2863edcc0754Sachartre 2864edcc0754Sachartre vd->slices[i].start = gpe[i].efi_gpe_StartingLBA; 2865edcc0754Sachartre vd->slices[i].nblocks = gpe[i].efi_gpe_EndingLBA - 2866edcc0754Sachartre gpe[i].efi_gpe_StartingLBA + 1; 2867edcc0754Sachartre 2868edcc0754Sachartre if (bcmp(&gpe[i].efi_gpe_PartitionTypeGUID, &efi_reserved, 2869edcc0754Sachartre sizeof (struct uuid)) == 0) 2870edcc0754Sachartre vd->efi_reserved = i; 2871edcc0754Sachartre 2872edcc0754Sachartre } 2873edcc0754Sachartre 2874edcc0754Sachartre ASSERT(vd->vdisk_size != 0); 2875edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].start = 0; 2876edcc0754Sachartre vd->slices[VD_EFI_WD_SLICE].nblocks = vd->vdisk_size; 2877edcc0754Sachartre 2878edcc0754Sachartre vds_efi_free(vd, gpt, gpe); 2879edcc0754Sachartre 2880edcc0754Sachartre return (status); 2881edcc0754Sachartre } 2882edcc0754Sachartre 288387a7269eSachartre /* 288478fcd0a1Sachartre * Function: 288578fcd0a1Sachartre * vd_file_validate_geometry 2886205eeb1aSlm66018 * 288778fcd0a1Sachartre * Description: 288878fcd0a1Sachartre * Read the label and validate the geometry of a disk image. The driver 288978fcd0a1Sachartre * label, vtoc and geometry information are updated according to the 289078fcd0a1Sachartre * label read from the disk image. 289178fcd0a1Sachartre * 289278fcd0a1Sachartre * If no valid label is found, the label is set to unknown and the 289378fcd0a1Sachartre * function returns EINVAL, but a default vtoc and geometry are provided 2894edcc0754Sachartre * to the driver. If an EFI label is found, ENOTSUP is returned. 289578fcd0a1Sachartre * 289678fcd0a1Sachartre * Parameters: 289778fcd0a1Sachartre * vd - disk on which the operation is performed. 289878fcd0a1Sachartre * 289978fcd0a1Sachartre * Return Code: 290078fcd0a1Sachartre * 0 - success. 290178fcd0a1Sachartre * EIO - error reading the label from the disk image. 290278fcd0a1Sachartre * EINVAL - unknown disk label. 2903edcc0754Sachartre * ENOTSUP - geometry not applicable (EFI label). 290487a7269eSachartre */ 290587a7269eSachartre static int 290678fcd0a1Sachartre vd_file_validate_geometry(vd_t *vd) 290787a7269eSachartre { 290887a7269eSachartre struct dk_label label; 290978fcd0a1Sachartre struct dk_geom *geom = &vd->dk_geom; 291078fcd0a1Sachartre struct vtoc *vtoc = &vd->vtoc; 291178fcd0a1Sachartre int i; 291278fcd0a1Sachartre int status = 0; 291387a7269eSachartre 291487a7269eSachartre ASSERT(vd->file); 2915edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 291687a7269eSachartre 291787a7269eSachartre if (VD_FILE_LABEL_READ(vd, &label) < 0) 291887a7269eSachartre return (EIO); 291987a7269eSachartre 292087a7269eSachartre if (label.dkl_magic != DKL_MAGIC || 292178fcd0a1Sachartre label.dkl_cksum != vd_lbl2cksum(&label) || 292266cfcfbeSachartre (vd_file_validate_sanity && label.dkl_vtoc.v_sanity != VTOC_SANE) || 292378fcd0a1Sachartre label.dkl_vtoc.v_nparts != V_NUMPAR) { 2924edcc0754Sachartre 2925edcc0754Sachartre if (vd_file_validate_efi(vd) == 0) { 2926edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 2927edcc0754Sachartre return (ENOTSUP); 2928edcc0754Sachartre } 2929edcc0754Sachartre 293078fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 2931bae9e67eSachartre vd_build_default_label(vd->file_size, &label); 293278fcd0a1Sachartre status = EINVAL; 293378fcd0a1Sachartre } else { 293478fcd0a1Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 293578fcd0a1Sachartre } 293687a7269eSachartre 2937bae9e67eSachartre /* Update the driver geometry and vtoc */ 2938bae9e67eSachartre vd_label_to_vtocgeom(&label, vtoc, geom); 293987a7269eSachartre 2940edcc0754Sachartre /* Update logical partitions */ 2941edcc0754Sachartre bzero(vd->slices, sizeof (vd_slice_t) * VD_MAXPART); 2942edcc0754Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 2943edcc0754Sachartre for (i = 0; i < vtoc->v_nparts; i++) { 2944edcc0754Sachartre vd->slices[i].start = vtoc->v_part[i].p_start; 2945edcc0754Sachartre vd->slices[i].nblocks = vtoc->v_part[i].p_size; 2946edcc0754Sachartre } 2947edcc0754Sachartre } 2948edcc0754Sachartre 294978fcd0a1Sachartre return (status); 295078fcd0a1Sachartre } 295178fcd0a1Sachartre 295278fcd0a1Sachartre /* 295378fcd0a1Sachartre * Handle ioctls to a disk image (file-based). 295478fcd0a1Sachartre * 295578fcd0a1Sachartre * Return Values 295678fcd0a1Sachartre * 0 - Indicates that there are no errors 295778fcd0a1Sachartre * != 0 - Disk operation returned an error 295878fcd0a1Sachartre */ 295978fcd0a1Sachartre static int 296078fcd0a1Sachartre vd_do_file_ioctl(vd_t *vd, int cmd, void *ioctl_arg) 296178fcd0a1Sachartre { 296278fcd0a1Sachartre struct dk_label label; 296378fcd0a1Sachartre struct dk_geom *geom; 296478fcd0a1Sachartre struct vtoc *vtoc; 2965edcc0754Sachartre dk_efi_t *efi; 2966bae9e67eSachartre int rc; 296778fcd0a1Sachartre 296878fcd0a1Sachartre ASSERT(vd->file); 2969edcc0754Sachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 297078fcd0a1Sachartre 297178fcd0a1Sachartre switch (cmd) { 297278fcd0a1Sachartre 297378fcd0a1Sachartre case DKIOCGGEOM: 297478fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 297578fcd0a1Sachartre geom = (struct dk_geom *)ioctl_arg; 297678fcd0a1Sachartre 297778fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 2978edcc0754Sachartre if (rc != 0 && rc != EINVAL) 297978fcd0a1Sachartre return (rc); 298078fcd0a1Sachartre bcopy(&vd->dk_geom, geom, sizeof (struct dk_geom)); 298178fcd0a1Sachartre return (0); 298278fcd0a1Sachartre 298378fcd0a1Sachartre case DKIOCGVTOC: 298478fcd0a1Sachartre ASSERT(ioctl_arg != NULL); 298578fcd0a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 298678fcd0a1Sachartre 298778fcd0a1Sachartre rc = vd_file_validate_geometry(vd); 2988edcc0754Sachartre if (rc != 0 && rc != EINVAL) 298978fcd0a1Sachartre return (rc); 299078fcd0a1Sachartre bcopy(&vd->vtoc, vtoc, sizeof (struct vtoc)); 299187a7269eSachartre return (0); 299287a7269eSachartre 299387a7269eSachartre case DKIOCSGEOM: 299487a7269eSachartre ASSERT(ioctl_arg != NULL); 299587a7269eSachartre geom = (struct dk_geom *)ioctl_arg; 299687a7269eSachartre 299787a7269eSachartre if (geom->dkg_nhead == 0 || geom->dkg_nsect == 0) 299887a7269eSachartre return (EINVAL); 299987a7269eSachartre 300087a7269eSachartre /* 300187a7269eSachartre * The current device geometry is not updated, just the driver 300287a7269eSachartre * "notion" of it. The device geometry will be effectively 300387a7269eSachartre * updated when a label is written to the device during a next 300487a7269eSachartre * DKIOCSVTOC. 300587a7269eSachartre */ 300687a7269eSachartre bcopy(ioctl_arg, &vd->dk_geom, sizeof (vd->dk_geom)); 300787a7269eSachartre return (0); 300887a7269eSachartre 300987a7269eSachartre case DKIOCSVTOC: 301087a7269eSachartre ASSERT(ioctl_arg != NULL); 301187a7269eSachartre ASSERT(vd->dk_geom.dkg_nhead != 0 && 301287a7269eSachartre vd->dk_geom.dkg_nsect != 0); 3013690555a1Sachartre vtoc = (struct vtoc *)ioctl_arg; 3014690555a1Sachartre 3015690555a1Sachartre if (vtoc->v_sanity != VTOC_SANE || 3016690555a1Sachartre vtoc->v_sectorsz != DEV_BSIZE || 3017690555a1Sachartre vtoc->v_nparts != V_NUMPAR) 3018690555a1Sachartre return (EINVAL); 3019690555a1Sachartre 3020bae9e67eSachartre vd_vtocgeom_to_label(vtoc, &vd->dk_geom, &label); 3021690555a1Sachartre 302287a7269eSachartre /* write label to the disk image */ 302387a7269eSachartre if ((rc = vd_file_set_vtoc(vd, &label)) != 0) 302487a7269eSachartre return (rc); 3025690555a1Sachartre 3026edcc0754Sachartre break; 3027edcc0754Sachartre 3028edcc0754Sachartre case DKIOCFLUSHWRITECACHE: 3029edcc0754Sachartre return (VOP_FSYNC(vd->file_vnode, FSYNC, kcred, NULL)); 3030edcc0754Sachartre 3031edcc0754Sachartre case DKIOCGETEFI: 3032edcc0754Sachartre ASSERT(ioctl_arg != NULL); 3033edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 3034edcc0754Sachartre 3035edcc0754Sachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, 3036edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3037edcc0754Sachartre return (EIO); 3038edcc0754Sachartre 3039edcc0754Sachartre return (0); 3040edcc0754Sachartre 3041edcc0754Sachartre case DKIOCSETEFI: 3042edcc0754Sachartre ASSERT(ioctl_arg != NULL); 3043edcc0754Sachartre efi = (dk_efi_t *)ioctl_arg; 3044edcc0754Sachartre 3045edcc0754Sachartre if (vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BWRITE, 3046edcc0754Sachartre (caddr_t)efi->dki_data, efi->dki_lba, efi->dki_length) < 0) 3047edcc0754Sachartre return (EIO); 3048edcc0754Sachartre 3049edcc0754Sachartre break; 3050edcc0754Sachartre 3051edcc0754Sachartre 3052edcc0754Sachartre default: 3053edcc0754Sachartre return (ENOTSUP); 3054edcc0754Sachartre } 3055edcc0754Sachartre 3056edcc0754Sachartre ASSERT(cmd == DKIOCSVTOC || cmd == DKIOCSETEFI); 3057edcc0754Sachartre 3058edcc0754Sachartre /* label has changed, revalidate the geometry */ 3059edcc0754Sachartre (void) vd_file_validate_geometry(vd); 30603c96341aSnarayan 306187a7269eSachartre /* 306287a7269eSachartre * The disk geometry may have changed, so we need to write 306387a7269eSachartre * the devid (if there is one) so that it is stored at the 306487a7269eSachartre * right location. 306587a7269eSachartre */ 3066edcc0754Sachartre if (vd_file_write_devid(vd, vd->file_devid) != 0) { 306787a7269eSachartre PR0("Fail to write devid"); 30681ae08745Sheppo } 30694bac2208Snarayan 30704bac2208Snarayan return (0); 30714bac2208Snarayan } 3072edcc0754Sachartre 3073edcc0754Sachartre static int 3074edcc0754Sachartre vd_backend_ioctl(vd_t *vd, int cmd, caddr_t arg) 3075edcc0754Sachartre { 3076edcc0754Sachartre int rval = 0, status; 3077edcc0754Sachartre 3078edcc0754Sachartre /* 3079edcc0754Sachartre * Call the appropriate function to execute the ioctl depending 3080edcc0754Sachartre * on the type of vdisk. 3081edcc0754Sachartre */ 3082edcc0754Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3083edcc0754Sachartre 3084edcc0754Sachartre /* slice, file or volume exported as a single slice disk */ 3085edcc0754Sachartre status = vd_do_slice_ioctl(vd, cmd, arg); 3086edcc0754Sachartre 3087edcc0754Sachartre } else if (vd->file) { 3088edcc0754Sachartre 3089edcc0754Sachartre /* file or volume exported as a full disk */ 3090edcc0754Sachartre status = vd_do_file_ioctl(vd, cmd, arg); 3091edcc0754Sachartre 3092edcc0754Sachartre } else { 3093edcc0754Sachartre 3094edcc0754Sachartre /* disk device exported as a full disk */ 3095edcc0754Sachartre status = ldi_ioctl(vd->ldi_handle[0], cmd, (intptr_t)arg, 3096edcc0754Sachartre vd->open_flags | FKIOCTL, kcred, &rval); 3097edcc0754Sachartre } 3098edcc0754Sachartre 3099edcc0754Sachartre #ifdef DEBUG 3100edcc0754Sachartre if (rval != 0) { 3101edcc0754Sachartre PR0("ioctl %x set rval = %d, which is not being returned" 3102edcc0754Sachartre " to caller", cmd, rval); 3103edcc0754Sachartre } 3104edcc0754Sachartre #endif /* DEBUG */ 3105edcc0754Sachartre 3106edcc0754Sachartre return (status); 31071ae08745Sheppo } 31081ae08745Sheppo 3109205eeb1aSlm66018 /* 3110205eeb1aSlm66018 * Description: 3111205eeb1aSlm66018 * This is the function that processes the ioctl requests (farming it 3112205eeb1aSlm66018 * out to functions that handle slices, files or whole disks) 3113205eeb1aSlm66018 * 3114205eeb1aSlm66018 * Return Values 3115205eeb1aSlm66018 * 0 - ioctl operation completed successfully 3116205eeb1aSlm66018 * != 0 - The LDC error value encountered 3117205eeb1aSlm66018 * (propagated back up the call stack as a task error) 3118205eeb1aSlm66018 * 3119205eeb1aSlm66018 * Side Effect 3120205eeb1aSlm66018 * sets request->status to the return value of the ioctl function. 3121205eeb1aSlm66018 */ 31221ae08745Sheppo static int 31230a55fbb7Slm66018 vd_do_ioctl(vd_t *vd, vd_dring_payload_t *request, void* buf, vd_ioctl_t *ioctl) 31241ae08745Sheppo { 3125edcc0754Sachartre int status = 0; 31261ae08745Sheppo size_t nbytes = request->nbytes; /* modifiable copy */ 31271ae08745Sheppo 31281ae08745Sheppo 31291ae08745Sheppo ASSERT(request->slice < vd->nslices); 31301ae08745Sheppo PR0("Performing %s", ioctl->operation_name); 31311ae08745Sheppo 31320a55fbb7Slm66018 /* Get data from client and convert, if necessary */ 31330a55fbb7Slm66018 if (ioctl->copyin != NULL) { 31341ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 31351ae08745Sheppo PR1("Getting \"arg\" data from client"); 31361ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 31371ae08745Sheppo request->cookie, request->ncookies, 31381ae08745Sheppo LDC_COPY_IN)) != 0) { 31393af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 31401ae08745Sheppo "copying from client", status); 31411ae08745Sheppo return (status); 31421ae08745Sheppo } 31430a55fbb7Slm66018 31440a55fbb7Slm66018 /* Convert client's data, if necessary */ 31452f5224aeSachartre if (ioctl->copyin == VD_IDENTITY_IN) { 31462f5224aeSachartre /* use client buffer */ 31470a55fbb7Slm66018 ioctl->arg = buf; 31482f5224aeSachartre } else { 31492f5224aeSachartre /* convert client vdisk operation data to ioctl data */ 31502f5224aeSachartre status = (ioctl->copyin)(buf, nbytes, 31512f5224aeSachartre (void *)ioctl->arg); 31522f5224aeSachartre if (status != 0) { 31532f5224aeSachartre request->status = status; 31542f5224aeSachartre return (0); 31552f5224aeSachartre } 31562f5224aeSachartre } 31572f5224aeSachartre } 31582f5224aeSachartre 31592f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD) { 31602f5224aeSachartre struct uscsi_cmd *uscsi = (struct uscsi_cmd *)ioctl->arg; 31612f5224aeSachartre 31622f5224aeSachartre /* check write permission */ 31632f5224aeSachartre if (!(vd->open_flags & FWRITE) && 31642f5224aeSachartre !(uscsi->uscsi_flags & USCSI_READ)) { 31652f5224aeSachartre PR0("uscsi fails because backend is opened read-only"); 31662f5224aeSachartre request->status = EROFS; 31672f5224aeSachartre return (0); 31682f5224aeSachartre } 31691ae08745Sheppo } 31701ae08745Sheppo 31711ae08745Sheppo /* 3172edcc0754Sachartre * Send the ioctl to the disk backend. 31731ae08745Sheppo */ 3174edcc0754Sachartre request->status = vd_backend_ioctl(vd, ioctl->cmd, ioctl->arg); 3175205eeb1aSlm66018 3176205eeb1aSlm66018 if (request->status != 0) { 3177205eeb1aSlm66018 PR0("ioctl(%s) = errno %d", ioctl->cmd_name, request->status); 31782f5224aeSachartre if (ioctl->operation == VD_OP_SCSICMD && 31792f5224aeSachartre ((struct uscsi_cmd *)ioctl->arg)->uscsi_status != 0) 31802f5224aeSachartre /* 31812f5224aeSachartre * USCSICMD has reported an error and the uscsi_status 31822f5224aeSachartre * field is not zero. This means that the SCSI command 31832f5224aeSachartre * has completed but it has an error. So we should 31842f5224aeSachartre * mark the VD operation has succesfully completed 31852f5224aeSachartre * and clients can check the SCSI status field for 31862f5224aeSachartre * SCSI errors. 31872f5224aeSachartre */ 31882f5224aeSachartre request->status = 0; 31892f5224aeSachartre else 3190205eeb1aSlm66018 return (0); 3191205eeb1aSlm66018 } 31921ae08745Sheppo 31930a55fbb7Slm66018 /* Convert data and send to client, if necessary */ 31940a55fbb7Slm66018 if (ioctl->copyout != NULL) { 31951ae08745Sheppo ASSERT(nbytes != 0 && buf != NULL); 31961ae08745Sheppo PR1("Sending \"arg\" data to client"); 31970a55fbb7Slm66018 31980a55fbb7Slm66018 /* Convert ioctl data to vdisk operation data, if necessary */ 31992f5224aeSachartre if (ioctl->copyout != VD_IDENTITY_OUT) 32000a55fbb7Slm66018 (ioctl->copyout)((void *)ioctl->arg, buf); 32010a55fbb7Slm66018 32021ae08745Sheppo if ((status = ldc_mem_copy(vd->ldc_handle, buf, 0, &nbytes, 32031ae08745Sheppo request->cookie, request->ncookies, 32041ae08745Sheppo LDC_COPY_OUT)) != 0) { 32053af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d " 32061ae08745Sheppo "copying to client", status); 32071ae08745Sheppo return (status); 32081ae08745Sheppo } 32091ae08745Sheppo } 32101ae08745Sheppo 32111ae08745Sheppo return (status); 32121ae08745Sheppo } 32131ae08745Sheppo 32141ae08745Sheppo #define RNDSIZE(expr) P2ROUNDUP(sizeof (expr), sizeof (uint64_t)) 3215205eeb1aSlm66018 3216205eeb1aSlm66018 /* 3217205eeb1aSlm66018 * Description: 3218205eeb1aSlm66018 * This generic function is called by the task queue to complete 3219205eeb1aSlm66018 * the processing of the tasks. The specific completion function 3220205eeb1aSlm66018 * is passed in as a field in the task pointer. 3221205eeb1aSlm66018 * 3222205eeb1aSlm66018 * Parameters: 3223205eeb1aSlm66018 * arg - opaque pointer to structure containing task to be completed 3224205eeb1aSlm66018 * 3225205eeb1aSlm66018 * Return Values 3226205eeb1aSlm66018 * None 3227205eeb1aSlm66018 */ 3228205eeb1aSlm66018 static void 3229205eeb1aSlm66018 vd_complete(void *arg) 3230205eeb1aSlm66018 { 3231205eeb1aSlm66018 vd_task_t *task = (vd_task_t *)arg; 3232205eeb1aSlm66018 3233205eeb1aSlm66018 ASSERT(task != NULL); 3234205eeb1aSlm66018 ASSERT(task->status == EINPROGRESS); 3235205eeb1aSlm66018 ASSERT(task->completef != NULL); 3236205eeb1aSlm66018 3237205eeb1aSlm66018 task->status = task->completef(task); 3238205eeb1aSlm66018 if (task->status) 3239205eeb1aSlm66018 PR0("%s: Error %d completing task", __func__, task->status); 3240205eeb1aSlm66018 3241205eeb1aSlm66018 /* Now notify the vDisk client */ 3242205eeb1aSlm66018 vd_complete_notify(task); 3243205eeb1aSlm66018 } 3244205eeb1aSlm66018 32451ae08745Sheppo static int 3246d10e4ef2Snarayan vd_ioctl(vd_task_t *task) 32471ae08745Sheppo { 324887a7269eSachartre int i, status; 32491ae08745Sheppo void *buf = NULL; 32500a55fbb7Slm66018 struct dk_geom dk_geom = {0}; 32510a55fbb7Slm66018 struct vtoc vtoc = {0}; 32524bac2208Snarayan struct dk_efi dk_efi = {0}; 32532f5224aeSachartre struct uscsi_cmd uscsi = {0}; 3254d10e4ef2Snarayan vd_t *vd = task->vd; 3255d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 32560a55fbb7Slm66018 vd_ioctl_t ioctl[] = { 32570a55fbb7Slm66018 /* Command (no-copy) operations */ 32580a55fbb7Slm66018 {VD_OP_FLUSH, STRINGIZE(VD_OP_FLUSH), 0, 32590a55fbb7Slm66018 DKIOCFLUSHWRITECACHE, STRINGIZE(DKIOCFLUSHWRITECACHE), 3260047ba61eSachartre NULL, NULL, NULL, B_TRUE}, 32610a55fbb7Slm66018 32620a55fbb7Slm66018 /* "Get" (copy-out) operations */ 32630a55fbb7Slm66018 {VD_OP_GET_WCE, STRINGIZE(VD_OP_GET_WCE), RNDSIZE(int), 32640a55fbb7Slm66018 DKIOCGETWCE, STRINGIZE(DKIOCGETWCE), 32652f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_FALSE}, 32660a55fbb7Slm66018 {VD_OP_GET_DISKGEOM, STRINGIZE(VD_OP_GET_DISKGEOM), 32670a55fbb7Slm66018 RNDSIZE(vd_geom_t), 32680a55fbb7Slm66018 DKIOCGGEOM, STRINGIZE(DKIOCGGEOM), 3269047ba61eSachartre &dk_geom, NULL, dk_geom2vd_geom, B_FALSE}, 32700a55fbb7Slm66018 {VD_OP_GET_VTOC, STRINGIZE(VD_OP_GET_VTOC), RNDSIZE(vd_vtoc_t), 32710a55fbb7Slm66018 DKIOCGVTOC, STRINGIZE(DKIOCGVTOC), 3272047ba61eSachartre &vtoc, NULL, vtoc2vd_vtoc, B_FALSE}, 32734bac2208Snarayan {VD_OP_GET_EFI, STRINGIZE(VD_OP_GET_EFI), RNDSIZE(vd_efi_t), 32744bac2208Snarayan DKIOCGETEFI, STRINGIZE(DKIOCGETEFI), 3275047ba61eSachartre &dk_efi, vd_get_efi_in, vd_get_efi_out, B_FALSE}, 32760a55fbb7Slm66018 32770a55fbb7Slm66018 /* "Set" (copy-in) operations */ 32780a55fbb7Slm66018 {VD_OP_SET_WCE, STRINGIZE(VD_OP_SET_WCE), RNDSIZE(int), 32790a55fbb7Slm66018 DKIOCSETWCE, STRINGIZE(DKIOCSETWCE), 32802f5224aeSachartre NULL, VD_IDENTITY_IN, VD_IDENTITY_OUT, B_TRUE}, 32810a55fbb7Slm66018 {VD_OP_SET_DISKGEOM, STRINGIZE(VD_OP_SET_DISKGEOM), 32820a55fbb7Slm66018 RNDSIZE(vd_geom_t), 32830a55fbb7Slm66018 DKIOCSGEOM, STRINGIZE(DKIOCSGEOM), 3284047ba61eSachartre &dk_geom, vd_geom2dk_geom, NULL, B_TRUE}, 32850a55fbb7Slm66018 {VD_OP_SET_VTOC, STRINGIZE(VD_OP_SET_VTOC), RNDSIZE(vd_vtoc_t), 32860a55fbb7Slm66018 DKIOCSVTOC, STRINGIZE(DKIOCSVTOC), 3287047ba61eSachartre &vtoc, vd_vtoc2vtoc, NULL, B_TRUE}, 32884bac2208Snarayan {VD_OP_SET_EFI, STRINGIZE(VD_OP_SET_EFI), RNDSIZE(vd_efi_t), 32894bac2208Snarayan DKIOCSETEFI, STRINGIZE(DKIOCSETEFI), 3290047ba61eSachartre &dk_efi, vd_set_efi_in, vd_set_efi_out, B_TRUE}, 32912f5224aeSachartre 32922f5224aeSachartre {VD_OP_SCSICMD, STRINGIZE(VD_OP_SCSICMD), RNDSIZE(vd_scsi_t), 32932f5224aeSachartre USCSICMD, STRINGIZE(USCSICMD), 32942f5224aeSachartre &uscsi, vd_scsicmd_in, vd_scsicmd_out, B_FALSE}, 32950a55fbb7Slm66018 }; 32961ae08745Sheppo size_t nioctls = (sizeof (ioctl))/(sizeof (ioctl[0])); 32971ae08745Sheppo 32981ae08745Sheppo 3299d10e4ef2Snarayan ASSERT(vd != NULL); 3300d10e4ef2Snarayan ASSERT(request != NULL); 33011ae08745Sheppo ASSERT(request->slice < vd->nslices); 33021ae08745Sheppo 33031ae08745Sheppo /* 33041ae08745Sheppo * Determine ioctl corresponding to caller's "operation" and 33051ae08745Sheppo * validate caller's "nbytes" 33061ae08745Sheppo */ 33071ae08745Sheppo for (i = 0; i < nioctls; i++) { 33081ae08745Sheppo if (request->operation == ioctl[i].operation) { 33090a55fbb7Slm66018 /* LDC memory operations require 8-byte multiples */ 33100a55fbb7Slm66018 ASSERT(ioctl[i].nbytes % sizeof (uint64_t) == 0); 33110a55fbb7Slm66018 33124bac2208Snarayan if (request->operation == VD_OP_GET_EFI || 33132f5224aeSachartre request->operation == VD_OP_SET_EFI || 33142f5224aeSachartre request->operation == VD_OP_SCSICMD) { 33154bac2208Snarayan if (request->nbytes >= ioctl[i].nbytes) 33164bac2208Snarayan break; 33173af08d82Slm66018 PR0("%s: Expected at least nbytes = %lu, " 33184bac2208Snarayan "got %lu", ioctl[i].operation_name, 33194bac2208Snarayan ioctl[i].nbytes, request->nbytes); 33204bac2208Snarayan return (EINVAL); 33214bac2208Snarayan } 33224bac2208Snarayan 33230a55fbb7Slm66018 if (request->nbytes != ioctl[i].nbytes) { 33243af08d82Slm66018 PR0("%s: Expected nbytes = %lu, got %lu", 33250a55fbb7Slm66018 ioctl[i].operation_name, ioctl[i].nbytes, 33260a55fbb7Slm66018 request->nbytes); 33271ae08745Sheppo return (EINVAL); 33281ae08745Sheppo } 33291ae08745Sheppo 33301ae08745Sheppo break; 33311ae08745Sheppo } 33321ae08745Sheppo } 33331ae08745Sheppo ASSERT(i < nioctls); /* because "operation" already validated */ 33341ae08745Sheppo 3335047ba61eSachartre if (!(vd->open_flags & FWRITE) && ioctl[i].write) { 3336047ba61eSachartre PR0("%s fails because backend is opened read-only", 3337047ba61eSachartre ioctl[i].operation_name); 3338047ba61eSachartre request->status = EROFS; 3339047ba61eSachartre return (0); 3340047ba61eSachartre } 3341047ba61eSachartre 33421ae08745Sheppo if (request->nbytes) 33431ae08745Sheppo buf = kmem_zalloc(request->nbytes, KM_SLEEP); 33441ae08745Sheppo status = vd_do_ioctl(vd, request, buf, &ioctl[i]); 33451ae08745Sheppo if (request->nbytes) 33461ae08745Sheppo kmem_free(buf, request->nbytes); 334787a7269eSachartre 33481ae08745Sheppo return (status); 33491ae08745Sheppo } 33501ae08745Sheppo 33514bac2208Snarayan static int 33524bac2208Snarayan vd_get_devid(vd_task_t *task) 33534bac2208Snarayan { 33544bac2208Snarayan vd_t *vd = task->vd; 33554bac2208Snarayan vd_dring_payload_t *request = task->request; 33564bac2208Snarayan vd_devid_t *vd_devid; 33574bac2208Snarayan impl_devid_t *devid; 335887a7269eSachartre int status, bufid_len, devid_len, len, sz; 33593af08d82Slm66018 int bufbytes; 33604bac2208Snarayan 33613af08d82Slm66018 PR1("Get Device ID, nbytes=%ld", request->nbytes); 33624bac2208Snarayan 3363bae9e67eSachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 3364bae9e67eSachartre /* 3365bae9e67eSachartre * We don't support devid for single-slice disks because we 3366bae9e67eSachartre * have no space to store a fabricated devid and for physical 3367bae9e67eSachartre * disk slices, we can't use the devid of the disk otherwise 3368bae9e67eSachartre * exporting multiple slices from the same disk will produce 3369bae9e67eSachartre * the same devids. 3370bae9e67eSachartre */ 3371bae9e67eSachartre PR2("No Device ID for slices"); 3372bae9e67eSachartre request->status = ENOTSUP; 3373bae9e67eSachartre return (0); 3374bae9e67eSachartre } 3375bae9e67eSachartre 33763c96341aSnarayan if (vd->file) { 337787a7269eSachartre if (vd->file_devid == NULL) { 33783af08d82Slm66018 PR2("No Device ID"); 3379205eeb1aSlm66018 request->status = ENOENT; 3380205eeb1aSlm66018 return (0); 338187a7269eSachartre } else { 338287a7269eSachartre sz = ddi_devid_sizeof(vd->file_devid); 338387a7269eSachartre devid = kmem_alloc(sz, KM_SLEEP); 338487a7269eSachartre bcopy(vd->file_devid, devid, sz); 338587a7269eSachartre } 338687a7269eSachartre } else { 338787a7269eSachartre if (ddi_lyr_get_devid(vd->dev[request->slice], 338887a7269eSachartre (ddi_devid_t *)&devid) != DDI_SUCCESS) { 338987a7269eSachartre PR2("No Device ID"); 3390205eeb1aSlm66018 request->status = ENOENT; 3391205eeb1aSlm66018 return (0); 339287a7269eSachartre } 33934bac2208Snarayan } 33944bac2208Snarayan 33954bac2208Snarayan bufid_len = request->nbytes - sizeof (vd_devid_t) + 1; 33964bac2208Snarayan devid_len = DEVID_GETLEN(devid); 33974bac2208Snarayan 33983af08d82Slm66018 /* 33993af08d82Slm66018 * Save the buffer size here for use in deallocation. 34003af08d82Slm66018 * The actual number of bytes copied is returned in 34013af08d82Slm66018 * the 'nbytes' field of the request structure. 34023af08d82Slm66018 */ 34033af08d82Slm66018 bufbytes = request->nbytes; 34043af08d82Slm66018 34053af08d82Slm66018 vd_devid = kmem_zalloc(bufbytes, KM_SLEEP); 34064bac2208Snarayan vd_devid->length = devid_len; 34074bac2208Snarayan vd_devid->type = DEVID_GETTYPE(devid); 34084bac2208Snarayan 34094bac2208Snarayan len = (devid_len > bufid_len)? bufid_len : devid_len; 34104bac2208Snarayan 34114bac2208Snarayan bcopy(devid->did_id, vd_devid->id, len); 34124bac2208Snarayan 341378fcd0a1Sachartre request->status = 0; 341478fcd0a1Sachartre 34154bac2208Snarayan /* LDC memory operations require 8-byte multiples */ 34164bac2208Snarayan ASSERT(request->nbytes % sizeof (uint64_t) == 0); 34174bac2208Snarayan 34184bac2208Snarayan if ((status = ldc_mem_copy(vd->ldc_handle, (caddr_t)vd_devid, 0, 34194bac2208Snarayan &request->nbytes, request->cookie, request->ncookies, 34204bac2208Snarayan LDC_COPY_OUT)) != 0) { 34213af08d82Slm66018 PR0("ldc_mem_copy() returned errno %d copying to client", 34224bac2208Snarayan status); 34234bac2208Snarayan } 34243af08d82Slm66018 PR1("post mem_copy: nbytes=%ld", request->nbytes); 34254bac2208Snarayan 34263af08d82Slm66018 kmem_free(vd_devid, bufbytes); 34274bac2208Snarayan ddi_devid_free((ddi_devid_t)devid); 34284bac2208Snarayan 34294bac2208Snarayan return (status); 34304bac2208Snarayan } 34314bac2208Snarayan 34322f5224aeSachartre static int 34332f5224aeSachartre vd_scsi_reset(vd_t *vd) 34342f5224aeSachartre { 34352f5224aeSachartre int rval, status; 34362f5224aeSachartre struct uscsi_cmd uscsi = { 0 }; 34372f5224aeSachartre 34382f5224aeSachartre uscsi.uscsi_flags = vd_scsi_debug | USCSI_RESET; 34392f5224aeSachartre uscsi.uscsi_timeout = vd_scsi_rdwr_timeout; 34402f5224aeSachartre 34412f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], USCSICMD, (intptr_t)&uscsi, 34422f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 34432f5224aeSachartre 34442f5224aeSachartre return (status); 34452f5224aeSachartre } 34462f5224aeSachartre 34472f5224aeSachartre static int 34482f5224aeSachartre vd_reset(vd_task_t *task) 34492f5224aeSachartre { 34502f5224aeSachartre vd_t *vd = task->vd; 34512f5224aeSachartre vd_dring_payload_t *request = task->request; 34522f5224aeSachartre 34532f5224aeSachartre ASSERT(request->operation == VD_OP_RESET); 34542f5224aeSachartre ASSERT(vd->scsi); 34552f5224aeSachartre 34562f5224aeSachartre PR0("Performing VD_OP_RESET"); 34572f5224aeSachartre 34582f5224aeSachartre if (request->nbytes != 0) { 34592f5224aeSachartre PR0("VD_OP_RESET: Expected nbytes = 0, got %lu", 34602f5224aeSachartre request->nbytes); 34612f5224aeSachartre return (EINVAL); 34622f5224aeSachartre } 34632f5224aeSachartre 34642f5224aeSachartre request->status = vd_scsi_reset(vd); 34652f5224aeSachartre 34662f5224aeSachartre return (0); 34672f5224aeSachartre } 34682f5224aeSachartre 34692f5224aeSachartre static int 34702f5224aeSachartre vd_get_capacity(vd_task_t *task) 34712f5224aeSachartre { 34722f5224aeSachartre int rv; 34732f5224aeSachartre size_t nbytes; 34742f5224aeSachartre vd_t *vd = task->vd; 34752f5224aeSachartre vd_dring_payload_t *request = task->request; 34762f5224aeSachartre vd_capacity_t vd_cap = { 0 }; 34772f5224aeSachartre 34782f5224aeSachartre ASSERT(request->operation == VD_OP_GET_CAPACITY); 34792f5224aeSachartre ASSERT(vd->scsi); 34802f5224aeSachartre 34812f5224aeSachartre PR0("Performing VD_OP_GET_CAPACITY"); 34822f5224aeSachartre 34832f5224aeSachartre nbytes = request->nbytes; 34842f5224aeSachartre 34852f5224aeSachartre if (nbytes != RNDSIZE(vd_capacity_t)) { 34862f5224aeSachartre PR0("VD_OP_GET_CAPACITY: Expected nbytes = %lu, got %lu", 34872f5224aeSachartre RNDSIZE(vd_capacity_t), nbytes); 34882f5224aeSachartre return (EINVAL); 34892f5224aeSachartre } 34902f5224aeSachartre 34912f5224aeSachartre if (vd->vdisk_size == VD_SIZE_UNKNOWN) { 34922f5224aeSachartre if (vd_setup_mediainfo(vd) != 0) 34932f5224aeSachartre ASSERT(vd->vdisk_size == VD_SIZE_UNKNOWN); 34942f5224aeSachartre } 34952f5224aeSachartre 34962f5224aeSachartre ASSERT(vd->vdisk_size != 0); 34972f5224aeSachartre 34982f5224aeSachartre request->status = 0; 34992f5224aeSachartre 35002f5224aeSachartre vd_cap.vdisk_block_size = vd->vdisk_block_size; 35012f5224aeSachartre vd_cap.vdisk_size = vd->vdisk_size; 35022f5224aeSachartre 35032f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&vd_cap, 0, &nbytes, 35042f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 35052f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 35062f5224aeSachartre return (rv); 35072f5224aeSachartre } 35082f5224aeSachartre 35092f5224aeSachartre return (0); 35102f5224aeSachartre } 35112f5224aeSachartre 35122f5224aeSachartre static int 35132f5224aeSachartre vd_get_access(vd_task_t *task) 35142f5224aeSachartre { 35152f5224aeSachartre uint64_t access; 35162f5224aeSachartre int rv, rval = 0; 35172f5224aeSachartre size_t nbytes; 35182f5224aeSachartre vd_t *vd = task->vd; 35192f5224aeSachartre vd_dring_payload_t *request = task->request; 35202f5224aeSachartre 35212f5224aeSachartre ASSERT(request->operation == VD_OP_GET_ACCESS); 35222f5224aeSachartre ASSERT(vd->scsi); 35232f5224aeSachartre 35242f5224aeSachartre PR0("Performing VD_OP_GET_ACCESS"); 35252f5224aeSachartre 35262f5224aeSachartre nbytes = request->nbytes; 35272f5224aeSachartre 35282f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 35292f5224aeSachartre PR0("VD_OP_GET_ACCESS: Expected nbytes = %lu, got %lu", 35302f5224aeSachartre sizeof (uint64_t), nbytes); 35312f5224aeSachartre return (EINVAL); 35322f5224aeSachartre } 35332f5224aeSachartre 35342f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], MHIOCSTATUS, 35352f5224aeSachartre NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 35362f5224aeSachartre 35372f5224aeSachartre if (request->status != 0) 35382f5224aeSachartre return (0); 35392f5224aeSachartre 35402f5224aeSachartre access = (rval == 0)? VD_ACCESS_ALLOWED : VD_ACCESS_DENIED; 35412f5224aeSachartre 35422f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&access, 0, &nbytes, 35432f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_OUT)) != 0) { 35442f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying to client", rv); 35452f5224aeSachartre return (rv); 35462f5224aeSachartre } 35472f5224aeSachartre 35482f5224aeSachartre return (0); 35492f5224aeSachartre } 35502f5224aeSachartre 35512f5224aeSachartre static int 35522f5224aeSachartre vd_set_access(vd_task_t *task) 35532f5224aeSachartre { 35542f5224aeSachartre uint64_t flags; 35552f5224aeSachartre int rv, rval; 35562f5224aeSachartre size_t nbytes; 35572f5224aeSachartre vd_t *vd = task->vd; 35582f5224aeSachartre vd_dring_payload_t *request = task->request; 35592f5224aeSachartre 35602f5224aeSachartre ASSERT(request->operation == VD_OP_SET_ACCESS); 35612f5224aeSachartre ASSERT(vd->scsi); 35622f5224aeSachartre 35632f5224aeSachartre nbytes = request->nbytes; 35642f5224aeSachartre 35652f5224aeSachartre if (nbytes != sizeof (uint64_t)) { 35662f5224aeSachartre PR0("VD_OP_SET_ACCESS: Expected nbytes = %lu, got %lu", 35672f5224aeSachartre sizeof (uint64_t), nbytes); 35682f5224aeSachartre return (EINVAL); 35692f5224aeSachartre } 35702f5224aeSachartre 35712f5224aeSachartre if ((rv = ldc_mem_copy(vd->ldc_handle, (char *)&flags, 0, &nbytes, 35722f5224aeSachartre request->cookie, request->ncookies, LDC_COPY_IN)) != 0) { 35732f5224aeSachartre PR0("ldc_mem_copy() returned errno %d copying from client", rv); 35742f5224aeSachartre return (rv); 35752f5224aeSachartre } 35762f5224aeSachartre 35772f5224aeSachartre if (flags == VD_ACCESS_SET_CLEAR) { 35782f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (CLEAR)"); 35792f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 35802f5224aeSachartre MHIOCRELEASE, NULL, (vd->open_flags | FKIOCTL), kcred, 35812f5224aeSachartre &rval); 35822f5224aeSachartre if (request->status == 0) 35832f5224aeSachartre vd->ownership = B_FALSE; 35842f5224aeSachartre return (0); 35852f5224aeSachartre } 35862f5224aeSachartre 35872f5224aeSachartre /* 35882f5224aeSachartre * As per the VIO spec, the PREEMPT and PRESERVE flags are only valid 35892f5224aeSachartre * when the EXCLUSIVE flag is set. 35902f5224aeSachartre */ 35912f5224aeSachartre if (!(flags & VD_ACCESS_SET_EXCLUSIVE)) { 35922f5224aeSachartre PR0("Invalid VD_OP_SET_ACCESS flags: 0x%lx", flags); 35932f5224aeSachartre request->status = EINVAL; 35942f5224aeSachartre return (0); 35952f5224aeSachartre } 35962f5224aeSachartre 35972f5224aeSachartre switch (flags & (VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE)) { 35982f5224aeSachartre 35992f5224aeSachartre case VD_ACCESS_SET_PREEMPT | VD_ACCESS_SET_PRESERVE: 36002f5224aeSachartre /* 36012f5224aeSachartre * Flags EXCLUSIVE and PREEMPT and PRESERVE. We have to 36022f5224aeSachartre * acquire exclusive access rights, preserve them and we 36032f5224aeSachartre * can use preemption. So we can use the MHIOCTKNOWN ioctl. 36042f5224aeSachartre */ 36052f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT|PRESERVE)"); 36062f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36072f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 36082f5224aeSachartre break; 36092f5224aeSachartre 36102f5224aeSachartre case VD_ACCESS_SET_PRESERVE: 36112f5224aeSachartre /* 36122f5224aeSachartre * Flags EXCLUSIVE and PRESERVE. We have to acquire exclusive 36132f5224aeSachartre * access rights and preserve them, but not preempt any other 36142f5224aeSachartre * host. So we need to use the MHIOCTKOWN ioctl to enable the 36152f5224aeSachartre * "preserve" feature but we can not called it directly 36162f5224aeSachartre * because it uses preemption. So before that, we use the 36172f5224aeSachartre * MHIOCQRESERVE ioctl to ensure we can get exclusive rights 36182f5224aeSachartre * without preempting anyone. 36192f5224aeSachartre */ 36202f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PRESERVE)"); 36212f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36222f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36232f5224aeSachartre &rval); 36242f5224aeSachartre if (request->status != 0) 36252f5224aeSachartre break; 36262f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36272f5224aeSachartre MHIOCTKOWN, NULL, (vd->open_flags | FKIOCTL), kcred, &rval); 36282f5224aeSachartre break; 36292f5224aeSachartre 36302f5224aeSachartre case VD_ACCESS_SET_PREEMPT: 36312f5224aeSachartre /* 36322f5224aeSachartre * Flags EXCLUSIVE and PREEMPT. We have to acquire exclusive 36332f5224aeSachartre * access rights and we can use preemption. So we try to do 36342f5224aeSachartre * a SCSI reservation, if it fails we reset the disk to clear 36352f5224aeSachartre * any reservation and we try to reserve again. 36362f5224aeSachartre */ 36372f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE|PREEMPT)"); 36382f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36392f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36402f5224aeSachartre &rval); 36412f5224aeSachartre if (request->status == 0) 36422f5224aeSachartre break; 36432f5224aeSachartre 36442f5224aeSachartre /* reset the disk */ 36452f5224aeSachartre (void) vd_scsi_reset(vd); 36462f5224aeSachartre 36472f5224aeSachartre /* try again even if the reset has failed */ 36482f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36492f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36502f5224aeSachartre &rval); 36512f5224aeSachartre break; 36522f5224aeSachartre 36532f5224aeSachartre case 0: 36542f5224aeSachartre /* Flag EXCLUSIVE only. Just issue a SCSI reservation */ 36552f5224aeSachartre PR0("Performing VD_OP_SET_ACCESS (EXCLUSIVE)"); 36562f5224aeSachartre request->status = ldi_ioctl(vd->ldi_handle[request->slice], 36572f5224aeSachartre MHIOCQRESERVE, NULL, (vd->open_flags | FKIOCTL), kcred, 36582f5224aeSachartre &rval); 36592f5224aeSachartre break; 36602f5224aeSachartre } 36612f5224aeSachartre 36622f5224aeSachartre if (request->status == 0) 36632f5224aeSachartre vd->ownership = B_TRUE; 36642f5224aeSachartre else 36652f5224aeSachartre PR0("VD_OP_SET_ACCESS: error %d", request->status); 36662f5224aeSachartre 36672f5224aeSachartre return (0); 36682f5224aeSachartre } 36692f5224aeSachartre 36702f5224aeSachartre static void 36712f5224aeSachartre vd_reset_access(vd_t *vd) 36722f5224aeSachartre { 36732f5224aeSachartre int status, rval; 36742f5224aeSachartre 36752f5224aeSachartre if (vd->file || !vd->ownership) 36762f5224aeSachartre return; 36772f5224aeSachartre 36782f5224aeSachartre PR0("Releasing disk ownership"); 36792f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 36802f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 36812f5224aeSachartre 36822f5224aeSachartre /* 36832f5224aeSachartre * An EACCES failure means that there is a reservation conflict, 36842f5224aeSachartre * so we are not the owner of the disk anymore. 36852f5224aeSachartre */ 36862f5224aeSachartre if (status == 0 || status == EACCES) { 36872f5224aeSachartre vd->ownership = B_FALSE; 36882f5224aeSachartre return; 36892f5224aeSachartre } 36902f5224aeSachartre 36912f5224aeSachartre PR0("Fail to release ownership, error %d", status); 36922f5224aeSachartre 36932f5224aeSachartre /* 36942f5224aeSachartre * We have failed to release the ownership, try to reset the disk 36952f5224aeSachartre * to release reservations. 36962f5224aeSachartre */ 36972f5224aeSachartre PR0("Resetting disk"); 36982f5224aeSachartre status = vd_scsi_reset(vd); 36992f5224aeSachartre 37002f5224aeSachartre if (status != 0) 37012f5224aeSachartre PR0("Fail to reset disk, error %d", status); 37022f5224aeSachartre 37032f5224aeSachartre /* whatever the result of the reset is, we try the release again */ 37042f5224aeSachartre status = ldi_ioctl(vd->ldi_handle[0], MHIOCRELEASE, NULL, 37052f5224aeSachartre (vd->open_flags | FKIOCTL), kcred, &rval); 37062f5224aeSachartre 37072f5224aeSachartre if (status == 0 || status == EACCES) { 37082f5224aeSachartre vd->ownership = B_FALSE; 37092f5224aeSachartre return; 37102f5224aeSachartre } 37112f5224aeSachartre 37122f5224aeSachartre PR0("Fail to release ownership, error %d", status); 37132f5224aeSachartre 37142f5224aeSachartre /* 37152f5224aeSachartre * At this point we have done our best to try to reset the 37162f5224aeSachartre * access rights to the disk and we don't know if we still 37172f5224aeSachartre * own a reservation and if any mechanism to preserve the 37182f5224aeSachartre * ownership is still in place. The ultimate solution would 37192f5224aeSachartre * be to reset the system but this is usually not what we 37202f5224aeSachartre * want to happen. 37212f5224aeSachartre */ 37222f5224aeSachartre 37232f5224aeSachartre if (vd_reset_access_failure == A_REBOOT) { 37242f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG 37252f5224aeSachartre ", rebooting the system", vd->device_path); 37262f5224aeSachartre (void) uadmin(A_SHUTDOWN, AD_BOOT, NULL); 37272f5224aeSachartre } else if (vd_reset_access_failure == A_DUMP) { 37282f5224aeSachartre panic(VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 37292f5224aeSachartre } 37302f5224aeSachartre 37312f5224aeSachartre cmn_err(CE_WARN, VD_RESET_ACCESS_FAILURE_MSG, vd->device_path); 37322f5224aeSachartre } 37332f5224aeSachartre 37341ae08745Sheppo /* 37351ae08745Sheppo * Define the supported operations once the functions for performing them have 37361ae08745Sheppo * been defined 37371ae08745Sheppo */ 37381ae08745Sheppo static const vds_operation_t vds_operation[] = { 37393af08d82Slm66018 #define X(_s) #_s, _s 37403af08d82Slm66018 {X(VD_OP_BREAD), vd_start_bio, vd_complete_bio}, 37413af08d82Slm66018 {X(VD_OP_BWRITE), vd_start_bio, vd_complete_bio}, 37423af08d82Slm66018 {X(VD_OP_FLUSH), vd_ioctl, NULL}, 37433af08d82Slm66018 {X(VD_OP_GET_WCE), vd_ioctl, NULL}, 37443af08d82Slm66018 {X(VD_OP_SET_WCE), vd_ioctl, NULL}, 37453af08d82Slm66018 {X(VD_OP_GET_VTOC), vd_ioctl, NULL}, 37463af08d82Slm66018 {X(VD_OP_SET_VTOC), vd_ioctl, NULL}, 37473af08d82Slm66018 {X(VD_OP_GET_DISKGEOM), vd_ioctl, NULL}, 37483af08d82Slm66018 {X(VD_OP_SET_DISKGEOM), vd_ioctl, NULL}, 37493af08d82Slm66018 {X(VD_OP_GET_EFI), vd_ioctl, NULL}, 37503af08d82Slm66018 {X(VD_OP_SET_EFI), vd_ioctl, NULL}, 37513af08d82Slm66018 {X(VD_OP_GET_DEVID), vd_get_devid, NULL}, 37522f5224aeSachartre {X(VD_OP_SCSICMD), vd_ioctl, NULL}, 37532f5224aeSachartre {X(VD_OP_RESET), vd_reset, NULL}, 37542f5224aeSachartre {X(VD_OP_GET_CAPACITY), vd_get_capacity, NULL}, 37552f5224aeSachartre {X(VD_OP_SET_ACCESS), vd_set_access, NULL}, 37562f5224aeSachartre {X(VD_OP_GET_ACCESS), vd_get_access, NULL}, 37573af08d82Slm66018 #undef X 37581ae08745Sheppo }; 37591ae08745Sheppo 37601ae08745Sheppo static const size_t vds_noperations = 37611ae08745Sheppo (sizeof (vds_operation))/(sizeof (vds_operation[0])); 37621ae08745Sheppo 37631ae08745Sheppo /* 3764d10e4ef2Snarayan * Process a task specifying a client I/O request 3765205eeb1aSlm66018 * 3766205eeb1aSlm66018 * Parameters: 3767205eeb1aSlm66018 * task - structure containing the request sent from client 3768205eeb1aSlm66018 * 3769205eeb1aSlm66018 * Return Value 3770205eeb1aSlm66018 * 0 - success 3771205eeb1aSlm66018 * ENOTSUP - Unknown/Unsupported VD_OP_XXX operation 3772205eeb1aSlm66018 * EINVAL - Invalid disk slice 3773205eeb1aSlm66018 * != 0 - some other non-zero return value from start function 37741ae08745Sheppo */ 37751ae08745Sheppo static int 3776205eeb1aSlm66018 vd_do_process_task(vd_task_t *task) 37771ae08745Sheppo { 3778205eeb1aSlm66018 int i; 3779d10e4ef2Snarayan vd_t *vd = task->vd; 3780d10e4ef2Snarayan vd_dring_payload_t *request = task->request; 37811ae08745Sheppo 3782d10e4ef2Snarayan ASSERT(vd != NULL); 3783d10e4ef2Snarayan ASSERT(request != NULL); 37841ae08745Sheppo 3785d10e4ef2Snarayan /* Find the requested operation */ 3786205eeb1aSlm66018 for (i = 0; i < vds_noperations; i++) { 3787205eeb1aSlm66018 if (request->operation == vds_operation[i].operation) { 3788205eeb1aSlm66018 /* all operations should have a start func */ 3789205eeb1aSlm66018 ASSERT(vds_operation[i].start != NULL); 3790205eeb1aSlm66018 3791205eeb1aSlm66018 task->completef = vds_operation[i].complete; 3792d10e4ef2Snarayan break; 3793205eeb1aSlm66018 } 3794205eeb1aSlm66018 } 379517cadca8Slm66018 379617cadca8Slm66018 /* 379717cadca8Slm66018 * We need to check that the requested operation is permitted 379817cadca8Slm66018 * for the particular client that sent it or that the loop above 379917cadca8Slm66018 * did not complete without finding the operation type (indicating 380017cadca8Slm66018 * that the requested operation is unknown/unimplemented) 380117cadca8Slm66018 */ 380217cadca8Slm66018 if ((VD_OP_SUPPORTED(vd->operations, request->operation) == B_FALSE) || 380317cadca8Slm66018 (i == vds_noperations)) { 38043af08d82Slm66018 PR0("Unsupported operation %u", request->operation); 380517cadca8Slm66018 request->status = ENOTSUP; 380617cadca8Slm66018 return (0); 38071ae08745Sheppo } 38081ae08745Sheppo 38097636cb21Slm66018 /* Range-check slice */ 381087a7269eSachartre if (request->slice >= vd->nslices && 3811bae9e67eSachartre ((vd->vdisk_type != VD_DISK_TYPE_DISK && vd_slice_single_slice) || 381287a7269eSachartre request->slice != VD_SLICE_NONE)) { 38133af08d82Slm66018 PR0("Invalid \"slice\" %u (max %u) for virtual disk", 38147636cb21Slm66018 request->slice, (vd->nslices - 1)); 3815bae9e67eSachartre request->status = EINVAL; 3816bae9e67eSachartre return (0); 38177636cb21Slm66018 } 38187636cb21Slm66018 3819205eeb1aSlm66018 /* 3820205eeb1aSlm66018 * Call the function pointer that starts the operation. 3821205eeb1aSlm66018 */ 3822205eeb1aSlm66018 return (vds_operation[i].start(task)); 38231ae08745Sheppo } 38241ae08745Sheppo 3825205eeb1aSlm66018 /* 3826205eeb1aSlm66018 * Description: 3827205eeb1aSlm66018 * This function is called by both the in-band and descriptor ring 3828205eeb1aSlm66018 * message processing functions paths to actually execute the task 3829205eeb1aSlm66018 * requested by the vDisk client. It in turn calls its worker 3830205eeb1aSlm66018 * function, vd_do_process_task(), to carry our the request. 3831205eeb1aSlm66018 * 3832205eeb1aSlm66018 * Any transport errors (e.g. LDC errors, vDisk protocol errors) are 3833205eeb1aSlm66018 * saved in the 'status' field of the task and are propagated back 3834205eeb1aSlm66018 * up the call stack to trigger a NACK 3835205eeb1aSlm66018 * 3836205eeb1aSlm66018 * Any request errors (e.g. ENOTTY from an ioctl) are saved in 3837205eeb1aSlm66018 * the 'status' field of the request and result in an ACK being sent 3838205eeb1aSlm66018 * by the completion handler. 3839205eeb1aSlm66018 * 3840205eeb1aSlm66018 * Parameters: 3841205eeb1aSlm66018 * task - structure containing the request sent from client 3842205eeb1aSlm66018 * 3843205eeb1aSlm66018 * Return Value 3844205eeb1aSlm66018 * 0 - successful synchronous request. 3845205eeb1aSlm66018 * != 0 - transport error (e.g. LDC errors, vDisk protocol) 3846205eeb1aSlm66018 * EINPROGRESS - task will be finished in a completion handler 3847205eeb1aSlm66018 */ 3848205eeb1aSlm66018 static int 3849205eeb1aSlm66018 vd_process_task(vd_task_t *task) 3850205eeb1aSlm66018 { 3851205eeb1aSlm66018 vd_t *vd = task->vd; 3852205eeb1aSlm66018 int status; 38531ae08745Sheppo 3854205eeb1aSlm66018 DTRACE_PROBE1(task__start, vd_task_t *, task); 38553af08d82Slm66018 3856205eeb1aSlm66018 task->status = vd_do_process_task(task); 3857205eeb1aSlm66018 3858205eeb1aSlm66018 /* 3859205eeb1aSlm66018 * If the task processing function returned EINPROGRESS indicating 3860205eeb1aSlm66018 * that the task needs completing then schedule a taskq entry to 3861205eeb1aSlm66018 * finish it now. 3862205eeb1aSlm66018 * 3863205eeb1aSlm66018 * Otherwise the task processing function returned either zero 3864205eeb1aSlm66018 * indicating that the task was finished in the start function (and we 3865205eeb1aSlm66018 * don't need to wait in a completion function) or the start function 3866205eeb1aSlm66018 * returned an error - in both cases all that needs to happen is the 3867205eeb1aSlm66018 * notification to the vDisk client higher up the call stack. 3868205eeb1aSlm66018 * If the task was using a Descriptor Ring, we need to mark it as done 3869205eeb1aSlm66018 * at this stage. 3870205eeb1aSlm66018 */ 3871205eeb1aSlm66018 if (task->status == EINPROGRESS) { 3872d10e4ef2Snarayan /* Queue a task to complete the operation */ 3873205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_complete, 3874d10e4ef2Snarayan task, DDI_SLEEP); 3875d10e4ef2Snarayan 3876f0ca1d9aSsb155480 } else if (!vd->reset_state && (vd->xfer_mode == VIO_DRING_MODE_V1_0)) { 3877205eeb1aSlm66018 /* Update the dring element if it's a dring client */ 3878205eeb1aSlm66018 status = vd_mark_elem_done(vd, task->index, 3879205eeb1aSlm66018 task->request->status, task->request->nbytes); 3880205eeb1aSlm66018 if (status == ECONNRESET) 3881205eeb1aSlm66018 vd_mark_in_reset(vd); 3882bbfa0259Sha137994 else if (status == EACCES) 3883bbfa0259Sha137994 vd_need_reset(vd, B_TRUE); 3884205eeb1aSlm66018 } 3885205eeb1aSlm66018 3886205eeb1aSlm66018 return (task->status); 38871ae08745Sheppo } 38881ae08745Sheppo 38891ae08745Sheppo /* 38900a55fbb7Slm66018 * Return true if the "type", "subtype", and "env" fields of the "tag" first 38910a55fbb7Slm66018 * argument match the corresponding remaining arguments; otherwise, return false 38921ae08745Sheppo */ 38930a55fbb7Slm66018 boolean_t 38941ae08745Sheppo vd_msgtype(vio_msg_tag_t *tag, int type, int subtype, int env) 38951ae08745Sheppo { 38961ae08745Sheppo return ((tag->vio_msgtype == type) && 38971ae08745Sheppo (tag->vio_subtype == subtype) && 38980a55fbb7Slm66018 (tag->vio_subtype_env == env)) ? B_TRUE : B_FALSE; 38991ae08745Sheppo } 39001ae08745Sheppo 39010a55fbb7Slm66018 /* 39020a55fbb7Slm66018 * Check whether the major/minor version specified in "ver_msg" is supported 39030a55fbb7Slm66018 * by this server. 39040a55fbb7Slm66018 */ 39050a55fbb7Slm66018 static boolean_t 39060a55fbb7Slm66018 vds_supported_version(vio_ver_msg_t *ver_msg) 39070a55fbb7Slm66018 { 39080a55fbb7Slm66018 for (int i = 0; i < vds_num_versions; i++) { 39090a55fbb7Slm66018 ASSERT(vds_version[i].major > 0); 39100a55fbb7Slm66018 ASSERT((i == 0) || 39110a55fbb7Slm66018 (vds_version[i].major < vds_version[i-1].major)); 39120a55fbb7Slm66018 39130a55fbb7Slm66018 /* 39140a55fbb7Slm66018 * If the major versions match, adjust the minor version, if 39150a55fbb7Slm66018 * necessary, down to the highest value supported by this 39160a55fbb7Slm66018 * server and return true so this message will get "ack"ed; 39170a55fbb7Slm66018 * the client should also support all minor versions lower 39180a55fbb7Slm66018 * than the value it sent 39190a55fbb7Slm66018 */ 39200a55fbb7Slm66018 if (ver_msg->ver_major == vds_version[i].major) { 39210a55fbb7Slm66018 if (ver_msg->ver_minor > vds_version[i].minor) { 39220a55fbb7Slm66018 PR0("Adjusting minor version from %u to %u", 39230a55fbb7Slm66018 ver_msg->ver_minor, vds_version[i].minor); 39240a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 39250a55fbb7Slm66018 } 39260a55fbb7Slm66018 return (B_TRUE); 39270a55fbb7Slm66018 } 39280a55fbb7Slm66018 39290a55fbb7Slm66018 /* 39300a55fbb7Slm66018 * If the message contains a higher major version number, set 39310a55fbb7Slm66018 * the message's major/minor versions to the current values 39320a55fbb7Slm66018 * and return false, so this message will get "nack"ed with 39330a55fbb7Slm66018 * these values, and the client will potentially try again 39340a55fbb7Slm66018 * with the same or a lower version 39350a55fbb7Slm66018 */ 39360a55fbb7Slm66018 if (ver_msg->ver_major > vds_version[i].major) { 39370a55fbb7Slm66018 ver_msg->ver_major = vds_version[i].major; 39380a55fbb7Slm66018 ver_msg->ver_minor = vds_version[i].minor; 39390a55fbb7Slm66018 return (B_FALSE); 39400a55fbb7Slm66018 } 39410a55fbb7Slm66018 39420a55fbb7Slm66018 /* 39430a55fbb7Slm66018 * Otherwise, the message's major version is less than the 39440a55fbb7Slm66018 * current major version, so continue the loop to the next 39450a55fbb7Slm66018 * (lower) supported version 39460a55fbb7Slm66018 */ 39470a55fbb7Slm66018 } 39480a55fbb7Slm66018 39490a55fbb7Slm66018 /* 39500a55fbb7Slm66018 * No common version was found; "ground" the version pair in the 39510a55fbb7Slm66018 * message to terminate negotiation 39520a55fbb7Slm66018 */ 39530a55fbb7Slm66018 ver_msg->ver_major = 0; 39540a55fbb7Slm66018 ver_msg->ver_minor = 0; 39550a55fbb7Slm66018 return (B_FALSE); 39560a55fbb7Slm66018 } 39570a55fbb7Slm66018 39580a55fbb7Slm66018 /* 39590a55fbb7Slm66018 * Process a version message from a client. vds expects to receive version 39600a55fbb7Slm66018 * messages from clients seeking service, but never issues version messages 39610a55fbb7Slm66018 * itself; therefore, vds can ACK or NACK client version messages, but does 39620a55fbb7Slm66018 * not expect to receive version-message ACKs or NACKs (and will treat such 39630a55fbb7Slm66018 * messages as invalid). 39640a55fbb7Slm66018 */ 39651ae08745Sheppo static int 39660a55fbb7Slm66018 vd_process_ver_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 39671ae08745Sheppo { 39681ae08745Sheppo vio_ver_msg_t *ver_msg = (vio_ver_msg_t *)msg; 39691ae08745Sheppo 39701ae08745Sheppo 39711ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 39721ae08745Sheppo 39731ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 39741ae08745Sheppo VIO_VER_INFO)) { 39751ae08745Sheppo return (ENOMSG); /* not a version message */ 39761ae08745Sheppo } 39771ae08745Sheppo 39781ae08745Sheppo if (msglen != sizeof (*ver_msg)) { 39793af08d82Slm66018 PR0("Expected %lu-byte version message; " 39801ae08745Sheppo "received %lu bytes", sizeof (*ver_msg), msglen); 39811ae08745Sheppo return (EBADMSG); 39821ae08745Sheppo } 39831ae08745Sheppo 39841ae08745Sheppo if (ver_msg->dev_class != VDEV_DISK) { 39853af08d82Slm66018 PR0("Expected device class %u (disk); received %u", 39861ae08745Sheppo VDEV_DISK, ver_msg->dev_class); 39871ae08745Sheppo return (EBADMSG); 39881ae08745Sheppo } 39891ae08745Sheppo 39900a55fbb7Slm66018 /* 39910a55fbb7Slm66018 * We're talking to the expected kind of client; set our device class 39920a55fbb7Slm66018 * for "ack/nack" back to the client 39930a55fbb7Slm66018 */ 39941ae08745Sheppo ver_msg->dev_class = VDEV_DISK_SERVER; 39950a55fbb7Slm66018 39960a55fbb7Slm66018 /* 39970a55fbb7Slm66018 * Check whether the (valid) version message specifies a version 39980a55fbb7Slm66018 * supported by this server. If the version is not supported, return 39990a55fbb7Slm66018 * EBADMSG so the message will get "nack"ed; vds_supported_version() 40000a55fbb7Slm66018 * will have updated the message with a supported version for the 40010a55fbb7Slm66018 * client to consider 40020a55fbb7Slm66018 */ 40030a55fbb7Slm66018 if (!vds_supported_version(ver_msg)) 40040a55fbb7Slm66018 return (EBADMSG); 40050a55fbb7Slm66018 40060a55fbb7Slm66018 40070a55fbb7Slm66018 /* 40080a55fbb7Slm66018 * A version has been agreed upon; use the client's SID for 40090a55fbb7Slm66018 * communication on this channel now 40100a55fbb7Slm66018 */ 40110a55fbb7Slm66018 ASSERT(!(vd->initialized & VD_SID)); 40120a55fbb7Slm66018 vd->sid = ver_msg->tag.vio_sid; 40130a55fbb7Slm66018 vd->initialized |= VD_SID; 40140a55fbb7Slm66018 40150a55fbb7Slm66018 /* 401617cadca8Slm66018 * Store the negotiated major and minor version values in the "vd" data 401717cadca8Slm66018 * structure so that we can check if certain operations are supported 401817cadca8Slm66018 * by the client. 40190a55fbb7Slm66018 */ 402017cadca8Slm66018 vd->version.major = ver_msg->ver_major; 402117cadca8Slm66018 vd->version.minor = ver_msg->ver_minor; 40220a55fbb7Slm66018 40230a55fbb7Slm66018 PR0("Using major version %u, minor version %u", 40240a55fbb7Slm66018 ver_msg->ver_major, ver_msg->ver_minor); 40251ae08745Sheppo return (0); 40261ae08745Sheppo } 40271ae08745Sheppo 402817cadca8Slm66018 static void 402917cadca8Slm66018 vd_set_exported_operations(vd_t *vd) 403017cadca8Slm66018 { 403117cadca8Slm66018 vd->operations = 0; /* clear field */ 403217cadca8Slm66018 403317cadca8Slm66018 /* 403417cadca8Slm66018 * We need to check from the highest version supported to the 403517cadca8Slm66018 * lowest because versions with a higher minor number implicitly 403617cadca8Slm66018 * support versions with a lower minor number. 403717cadca8Slm66018 */ 403817cadca8Slm66018 if (vio_ver_is_supported(vd->version, 1, 1)) { 403917cadca8Slm66018 ASSERT(vd->open_flags & FREAD); 404017cadca8Slm66018 vd->operations |= VD_OP_MASK_READ; 404117cadca8Slm66018 404217cadca8Slm66018 if (vd->open_flags & FWRITE) 404317cadca8Slm66018 vd->operations |= VD_OP_MASK_WRITE; 404417cadca8Slm66018 40452f5224aeSachartre if (vd->scsi) 40462f5224aeSachartre vd->operations |= VD_OP_MASK_SCSI; 40472f5224aeSachartre 404817cadca8Slm66018 if (vd->file && vd_file_is_iso_image(vd)) { 404917cadca8Slm66018 /* 405017cadca8Slm66018 * can't write to ISO images, make sure that write 405117cadca8Slm66018 * support is not set in case administrator did not 405217cadca8Slm66018 * use "options=ro" when doing an ldm add-vdsdev 405317cadca8Slm66018 */ 405417cadca8Slm66018 vd->operations &= ~VD_OP_MASK_WRITE; 405517cadca8Slm66018 } 405617cadca8Slm66018 } else if (vio_ver_is_supported(vd->version, 1, 0)) { 405717cadca8Slm66018 vd->operations = VD_OP_MASK_READ | VD_OP_MASK_WRITE; 405817cadca8Slm66018 } 405917cadca8Slm66018 406017cadca8Slm66018 /* we should have already agreed on a version */ 406117cadca8Slm66018 ASSERT(vd->operations != 0); 406217cadca8Slm66018 } 406317cadca8Slm66018 40641ae08745Sheppo static int 40651ae08745Sheppo vd_process_attr_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 40661ae08745Sheppo { 40671ae08745Sheppo vd_attr_msg_t *attr_msg = (vd_attr_msg_t *)msg; 40683c96341aSnarayan int status, retry = 0; 40691ae08745Sheppo 40701ae08745Sheppo 40711ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 40721ae08745Sheppo 40731ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 40741ae08745Sheppo VIO_ATTR_INFO)) { 4075d10e4ef2Snarayan PR0("Message is not an attribute message"); 4076d10e4ef2Snarayan return (ENOMSG); 40771ae08745Sheppo } 40781ae08745Sheppo 40791ae08745Sheppo if (msglen != sizeof (*attr_msg)) { 40803af08d82Slm66018 PR0("Expected %lu-byte attribute message; " 40811ae08745Sheppo "received %lu bytes", sizeof (*attr_msg), msglen); 40821ae08745Sheppo return (EBADMSG); 40831ae08745Sheppo } 40841ae08745Sheppo 40851ae08745Sheppo if (attr_msg->max_xfer_sz == 0) { 40863af08d82Slm66018 PR0("Received maximum transfer size of 0 from client"); 40871ae08745Sheppo return (EBADMSG); 40881ae08745Sheppo } 40891ae08745Sheppo 40901ae08745Sheppo if ((attr_msg->xfer_mode != VIO_DESC_MODE) && 4091f0ca1d9aSsb155480 (attr_msg->xfer_mode != VIO_DRING_MODE_V1_0)) { 40923af08d82Slm66018 PR0("Client requested unsupported transfer mode"); 40931ae08745Sheppo return (EBADMSG); 40941ae08745Sheppo } 40951ae08745Sheppo 40963c96341aSnarayan /* 40973c96341aSnarayan * check if the underlying disk is ready, if not try accessing 40983c96341aSnarayan * the device again. Open the vdisk device and extract info 40993c96341aSnarayan * about it, as this is needed to respond to the attr info msg 41003c96341aSnarayan */ 41013c96341aSnarayan if ((vd->initialized & VD_DISK_READY) == 0) { 41023c96341aSnarayan PR0("Retry setting up disk (%s)", vd->device_path); 41033c96341aSnarayan do { 41043c96341aSnarayan status = vd_setup_vd(vd); 41053c96341aSnarayan if (status != EAGAIN || ++retry > vds_dev_retries) 41063c96341aSnarayan break; 41073c96341aSnarayan 41083c96341aSnarayan /* incremental delay */ 41093c96341aSnarayan delay(drv_usectohz(vds_dev_delay)); 41103c96341aSnarayan 41113c96341aSnarayan /* if vdisk is no longer enabled - return error */ 41123c96341aSnarayan if (!vd_enabled(vd)) 41133c96341aSnarayan return (ENXIO); 41143c96341aSnarayan 41153c96341aSnarayan } while (status == EAGAIN); 41163c96341aSnarayan 41173c96341aSnarayan if (status) 41183c96341aSnarayan return (ENXIO); 41193c96341aSnarayan 41203c96341aSnarayan vd->initialized |= VD_DISK_READY; 41213c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 41228fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 41233c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 41248fce2fd6Sachartre (vd->volume ? "yes" : "no"), 41253c96341aSnarayan (vd->file ? "yes" : "no"), 41263c96341aSnarayan vd->nslices); 41273c96341aSnarayan } 41283c96341aSnarayan 41291ae08745Sheppo /* Success: valid message and transfer mode */ 41301ae08745Sheppo vd->xfer_mode = attr_msg->xfer_mode; 41313af08d82Slm66018 41321ae08745Sheppo if (vd->xfer_mode == VIO_DESC_MODE) { 41333af08d82Slm66018 41341ae08745Sheppo /* 41351ae08745Sheppo * The vd_dring_inband_msg_t contains one cookie; need room 41361ae08745Sheppo * for up to n-1 more cookies, where "n" is the number of full 41371ae08745Sheppo * pages plus possibly one partial page required to cover 41381ae08745Sheppo * "max_xfer_sz". Add room for one more cookie if 41391ae08745Sheppo * "max_xfer_sz" isn't an integral multiple of the page size. 41401ae08745Sheppo * Must first get the maximum transfer size in bytes. 41411ae08745Sheppo */ 41421ae08745Sheppo size_t max_xfer_bytes = attr_msg->vdisk_block_size ? 41431ae08745Sheppo attr_msg->vdisk_block_size*attr_msg->max_xfer_sz : 41441ae08745Sheppo attr_msg->max_xfer_sz; 41451ae08745Sheppo size_t max_inband_msglen = 41461ae08745Sheppo sizeof (vd_dring_inband_msg_t) + 41471ae08745Sheppo ((max_xfer_bytes/PAGESIZE + 41481ae08745Sheppo ((max_xfer_bytes % PAGESIZE) ? 1 : 0))* 41491ae08745Sheppo (sizeof (ldc_mem_cookie_t))); 41501ae08745Sheppo 41511ae08745Sheppo /* 41521ae08745Sheppo * Set the maximum expected message length to 41531ae08745Sheppo * accommodate in-band-descriptor messages with all 41541ae08745Sheppo * their cookies 41551ae08745Sheppo */ 41561ae08745Sheppo vd->max_msglen = MAX(vd->max_msglen, max_inband_msglen); 4157d10e4ef2Snarayan 4158d10e4ef2Snarayan /* 4159d10e4ef2Snarayan * Initialize the data structure for processing in-band I/O 4160d10e4ef2Snarayan * request descriptors 4161d10e4ef2Snarayan */ 4162d10e4ef2Snarayan vd->inband_task.vd = vd; 41633af08d82Slm66018 vd->inband_task.msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4164d10e4ef2Snarayan vd->inband_task.index = 0; 4165d10e4ef2Snarayan vd->inband_task.type = VD_FINAL_RANGE_TASK; /* range == 1 */ 41661ae08745Sheppo } 41671ae08745Sheppo 4168e1ebb9ecSlm66018 /* Return the device's block size and max transfer size to the client */ 41692f5224aeSachartre attr_msg->vdisk_block_size = vd->vdisk_block_size; 4170e1ebb9ecSlm66018 attr_msg->max_xfer_sz = vd->max_xfer_sz; 4171e1ebb9ecSlm66018 41721ae08745Sheppo attr_msg->vdisk_size = vd->vdisk_size; 4173bae9e67eSachartre attr_msg->vdisk_type = (vd_slice_single_slice)? vd->vdisk_type : 4174bae9e67eSachartre VD_DISK_TYPE_DISK; 417517cadca8Slm66018 attr_msg->vdisk_media = vd->vdisk_media; 417617cadca8Slm66018 417717cadca8Slm66018 /* Discover and save the list of supported VD_OP_XXX operations */ 417817cadca8Slm66018 vd_set_exported_operations(vd); 417917cadca8Slm66018 attr_msg->operations = vd->operations; 418017cadca8Slm66018 41811ae08745Sheppo PR0("%s", VD_CLIENT(vd)); 41823af08d82Slm66018 41833af08d82Slm66018 ASSERT(vd->dring_task == NULL); 41843af08d82Slm66018 41851ae08745Sheppo return (0); 41861ae08745Sheppo } 41871ae08745Sheppo 41881ae08745Sheppo static int 41891ae08745Sheppo vd_process_dring_reg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 41901ae08745Sheppo { 41911ae08745Sheppo int status; 41921ae08745Sheppo size_t expected; 41931ae08745Sheppo ldc_mem_info_t dring_minfo; 4194bbfa0259Sha137994 uint8_t mtype; 41951ae08745Sheppo vio_dring_reg_msg_t *reg_msg = (vio_dring_reg_msg_t *)msg; 41961ae08745Sheppo 41971ae08745Sheppo 41981ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 41991ae08745Sheppo 42001ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 42011ae08745Sheppo VIO_DRING_REG)) { 4202d10e4ef2Snarayan PR0("Message is not a register-dring message"); 4203d10e4ef2Snarayan return (ENOMSG); 42041ae08745Sheppo } 42051ae08745Sheppo 42061ae08745Sheppo if (msglen < sizeof (*reg_msg)) { 42073af08d82Slm66018 PR0("Expected at least %lu-byte register-dring message; " 42081ae08745Sheppo "received %lu bytes", sizeof (*reg_msg), msglen); 42091ae08745Sheppo return (EBADMSG); 42101ae08745Sheppo } 42111ae08745Sheppo 42121ae08745Sheppo expected = sizeof (*reg_msg) + 42131ae08745Sheppo (reg_msg->ncookies - 1)*(sizeof (reg_msg->cookie[0])); 42141ae08745Sheppo if (msglen != expected) { 42153af08d82Slm66018 PR0("Expected %lu-byte register-dring message; " 42161ae08745Sheppo "received %lu bytes", expected, msglen); 42171ae08745Sheppo return (EBADMSG); 42181ae08745Sheppo } 42191ae08745Sheppo 42201ae08745Sheppo if (vd->initialized & VD_DRING) { 42213af08d82Slm66018 PR0("A dring was previously registered; only support one"); 42221ae08745Sheppo return (EBADMSG); 42231ae08745Sheppo } 42241ae08745Sheppo 4225d10e4ef2Snarayan if (reg_msg->num_descriptors > INT32_MAX) { 42263af08d82Slm66018 PR0("reg_msg->num_descriptors = %u; must be <= %u (%s)", 4227d10e4ef2Snarayan reg_msg->ncookies, INT32_MAX, STRINGIZE(INT32_MAX)); 4228d10e4ef2Snarayan return (EBADMSG); 4229d10e4ef2Snarayan } 4230d10e4ef2Snarayan 42311ae08745Sheppo if (reg_msg->ncookies != 1) { 42321ae08745Sheppo /* 42331ae08745Sheppo * In addition to fixing the assertion in the success case 42341ae08745Sheppo * below, supporting drings which require more than one 42351ae08745Sheppo * "cookie" requires increasing the value of vd->max_msglen 42361ae08745Sheppo * somewhere in the code path prior to receiving the message 42371ae08745Sheppo * which results in calling this function. Note that without 42381ae08745Sheppo * making this change, the larger message size required to 42391ae08745Sheppo * accommodate multiple cookies cannot be successfully 42401ae08745Sheppo * received, so this function will not even get called. 42411ae08745Sheppo * Gracefully accommodating more dring cookies might 42421ae08745Sheppo * reasonably demand exchanging an additional attribute or 42431ae08745Sheppo * making a minor protocol adjustment 42441ae08745Sheppo */ 42453af08d82Slm66018 PR0("reg_msg->ncookies = %u != 1", reg_msg->ncookies); 42461ae08745Sheppo return (EBADMSG); 42471ae08745Sheppo } 42481ae08745Sheppo 4249bbfa0259Sha137994 if (vd_direct_mapped_drings) 4250bbfa0259Sha137994 mtype = LDC_DIRECT_MAP; 4251bbfa0259Sha137994 else 4252bbfa0259Sha137994 mtype = LDC_SHADOW_MAP; 4253bbfa0259Sha137994 42541ae08745Sheppo status = ldc_mem_dring_map(vd->ldc_handle, reg_msg->cookie, 42551ae08745Sheppo reg_msg->ncookies, reg_msg->num_descriptors, 4256bbfa0259Sha137994 reg_msg->descriptor_size, mtype, &vd->dring_handle); 42571ae08745Sheppo if (status != 0) { 42583af08d82Slm66018 PR0("ldc_mem_dring_map() returned errno %d", status); 42591ae08745Sheppo return (status); 42601ae08745Sheppo } 42611ae08745Sheppo 42621ae08745Sheppo /* 42631ae08745Sheppo * To remove the need for this assertion, must call 42641ae08745Sheppo * ldc_mem_dring_nextcookie() successfully ncookies-1 times after a 42651ae08745Sheppo * successful call to ldc_mem_dring_map() 42661ae08745Sheppo */ 42671ae08745Sheppo ASSERT(reg_msg->ncookies == 1); 42681ae08745Sheppo 42691ae08745Sheppo if ((status = 42701ae08745Sheppo ldc_mem_dring_info(vd->dring_handle, &dring_minfo)) != 0) { 42713af08d82Slm66018 PR0("ldc_mem_dring_info() returned errno %d", status); 42721ae08745Sheppo if ((status = ldc_mem_dring_unmap(vd->dring_handle)) != 0) 42733af08d82Slm66018 PR0("ldc_mem_dring_unmap() returned errno %d", status); 42741ae08745Sheppo return (status); 42751ae08745Sheppo } 42761ae08745Sheppo 42771ae08745Sheppo if (dring_minfo.vaddr == NULL) { 42783af08d82Slm66018 PR0("Descriptor ring virtual address is NULL"); 42790a55fbb7Slm66018 return (ENXIO); 42801ae08745Sheppo } 42811ae08745Sheppo 42821ae08745Sheppo 4283d10e4ef2Snarayan /* Initialize for valid message and mapped dring */ 42841ae08745Sheppo vd->initialized |= VD_DRING; 42851ae08745Sheppo vd->dring_ident = 1; /* "There Can Be Only One" */ 42861ae08745Sheppo vd->dring = dring_minfo.vaddr; 42871ae08745Sheppo vd->descriptor_size = reg_msg->descriptor_size; 42881ae08745Sheppo vd->dring_len = reg_msg->num_descriptors; 4289bbfa0259Sha137994 vd->dring_mtype = dring_minfo.mtype; 42901ae08745Sheppo reg_msg->dring_ident = vd->dring_ident; 4291*5b7cb889Sha137994 PR1("descriptor size = %u, dring length = %u", 4292*5b7cb889Sha137994 vd->descriptor_size, vd->dring_len); 4293d10e4ef2Snarayan 4294d10e4ef2Snarayan /* 4295d10e4ef2Snarayan * Allocate and initialize a "shadow" array of data structures for 4296d10e4ef2Snarayan * tasks to process I/O requests in dring elements 4297d10e4ef2Snarayan */ 4298d10e4ef2Snarayan vd->dring_task = 4299d10e4ef2Snarayan kmem_zalloc((sizeof (*vd->dring_task)) * vd->dring_len, KM_SLEEP); 4300d10e4ef2Snarayan for (int i = 0; i < vd->dring_len; i++) { 4301d10e4ef2Snarayan vd->dring_task[i].vd = vd; 4302d10e4ef2Snarayan vd->dring_task[i].index = i; 43034bac2208Snarayan 43044bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, 43054bac2208Snarayan &(vd->dring_task[i].mhdl)); 43064bac2208Snarayan if (status) { 43073af08d82Slm66018 PR0("ldc_mem_alloc_handle() returned err %d ", status); 43084bac2208Snarayan return (ENXIO); 43094bac2208Snarayan } 43103af08d82Slm66018 4311*5b7cb889Sha137994 /* 4312*5b7cb889Sha137994 * The descriptor payload varies in length. Calculate its 4313*5b7cb889Sha137994 * size by subtracting the header size from the total 4314*5b7cb889Sha137994 * descriptor size. 4315*5b7cb889Sha137994 */ 4316*5b7cb889Sha137994 vd->dring_task[i].request = kmem_zalloc((vd->descriptor_size - 4317*5b7cb889Sha137994 sizeof (vio_dring_entry_hdr_t)), KM_SLEEP); 43183af08d82Slm66018 vd->dring_task[i].msg = kmem_alloc(vd->max_msglen, KM_SLEEP); 4319d10e4ef2Snarayan } 4320d10e4ef2Snarayan 43211ae08745Sheppo return (0); 43221ae08745Sheppo } 43231ae08745Sheppo 43241ae08745Sheppo static int 43251ae08745Sheppo vd_process_dring_unreg_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 43261ae08745Sheppo { 43271ae08745Sheppo vio_dring_unreg_msg_t *unreg_msg = (vio_dring_unreg_msg_t *)msg; 43281ae08745Sheppo 43291ae08745Sheppo 43301ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 43311ae08745Sheppo 43321ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, 43331ae08745Sheppo VIO_DRING_UNREG)) { 4334d10e4ef2Snarayan PR0("Message is not an unregister-dring message"); 4335d10e4ef2Snarayan return (ENOMSG); 43361ae08745Sheppo } 43371ae08745Sheppo 43381ae08745Sheppo if (msglen != sizeof (*unreg_msg)) { 43393af08d82Slm66018 PR0("Expected %lu-byte unregister-dring message; " 43401ae08745Sheppo "received %lu bytes", sizeof (*unreg_msg), msglen); 43411ae08745Sheppo return (EBADMSG); 43421ae08745Sheppo } 43431ae08745Sheppo 43441ae08745Sheppo if (unreg_msg->dring_ident != vd->dring_ident) { 43453af08d82Slm66018 PR0("Expected dring ident %lu; received %lu", 43461ae08745Sheppo vd->dring_ident, unreg_msg->dring_ident); 43471ae08745Sheppo return (EBADMSG); 43481ae08745Sheppo } 43491ae08745Sheppo 43501ae08745Sheppo return (0); 43511ae08745Sheppo } 43521ae08745Sheppo 43531ae08745Sheppo static int 43541ae08745Sheppo process_rdx_msg(vio_msg_t *msg, size_t msglen) 43551ae08745Sheppo { 43561ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 43571ae08745Sheppo 4358d10e4ef2Snarayan if (!vd_msgtype(&msg->tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX)) { 4359d10e4ef2Snarayan PR0("Message is not an RDX message"); 4360d10e4ef2Snarayan return (ENOMSG); 4361d10e4ef2Snarayan } 43621ae08745Sheppo 43631ae08745Sheppo if (msglen != sizeof (vio_rdx_msg_t)) { 43643af08d82Slm66018 PR0("Expected %lu-byte RDX message; received %lu bytes", 43651ae08745Sheppo sizeof (vio_rdx_msg_t), msglen); 43661ae08745Sheppo return (EBADMSG); 43671ae08745Sheppo } 43681ae08745Sheppo 4369d10e4ef2Snarayan PR0("Valid RDX message"); 43701ae08745Sheppo return (0); 43711ae08745Sheppo } 43721ae08745Sheppo 43731ae08745Sheppo static int 43741ae08745Sheppo vd_check_seq_num(vd_t *vd, uint64_t seq_num) 43751ae08745Sheppo { 43761ae08745Sheppo if ((vd->initialized & VD_SEQ_NUM) && (seq_num != vd->seq_num + 1)) { 43773af08d82Slm66018 PR0("Received seq_num %lu; expected %lu", 43781ae08745Sheppo seq_num, (vd->seq_num + 1)); 43793af08d82Slm66018 PR0("initiating soft reset"); 4380d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 43811ae08745Sheppo return (1); 43821ae08745Sheppo } 43831ae08745Sheppo 43841ae08745Sheppo vd->seq_num = seq_num; 43851ae08745Sheppo vd->initialized |= VD_SEQ_NUM; /* superfluous after first time... */ 43861ae08745Sheppo return (0); 43871ae08745Sheppo } 43881ae08745Sheppo 43891ae08745Sheppo /* 43901ae08745Sheppo * Return the expected size of an inband-descriptor message with all the 43911ae08745Sheppo * cookies it claims to include 43921ae08745Sheppo */ 43931ae08745Sheppo static size_t 43941ae08745Sheppo expected_inband_size(vd_dring_inband_msg_t *msg) 43951ae08745Sheppo { 43961ae08745Sheppo return ((sizeof (*msg)) + 43971ae08745Sheppo (msg->payload.ncookies - 1)*(sizeof (msg->payload.cookie[0]))); 43981ae08745Sheppo } 43991ae08745Sheppo 44001ae08745Sheppo /* 44011ae08745Sheppo * Process an in-band descriptor message: used with clients like OBP, with 44021ae08745Sheppo * which vds exchanges descriptors within VIO message payloads, rather than 44031ae08745Sheppo * operating on them within a descriptor ring 44041ae08745Sheppo */ 44051ae08745Sheppo static int 44063af08d82Slm66018 vd_process_desc_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 44071ae08745Sheppo { 44081ae08745Sheppo size_t expected; 44091ae08745Sheppo vd_dring_inband_msg_t *desc_msg = (vd_dring_inband_msg_t *)msg; 44101ae08745Sheppo 44111ae08745Sheppo 44121ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 44131ae08745Sheppo 44141ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 4415d10e4ef2Snarayan VIO_DESC_DATA)) { 4416d10e4ef2Snarayan PR1("Message is not an in-band-descriptor message"); 4417d10e4ef2Snarayan return (ENOMSG); 4418d10e4ef2Snarayan } 44191ae08745Sheppo 44201ae08745Sheppo if (msglen < sizeof (*desc_msg)) { 44213af08d82Slm66018 PR0("Expected at least %lu-byte descriptor message; " 44221ae08745Sheppo "received %lu bytes", sizeof (*desc_msg), msglen); 44231ae08745Sheppo return (EBADMSG); 44241ae08745Sheppo } 44251ae08745Sheppo 44261ae08745Sheppo if (msglen != (expected = expected_inband_size(desc_msg))) { 44273af08d82Slm66018 PR0("Expected %lu-byte descriptor message; " 44281ae08745Sheppo "received %lu bytes", expected, msglen); 44291ae08745Sheppo return (EBADMSG); 44301ae08745Sheppo } 44311ae08745Sheppo 4432d10e4ef2Snarayan if (vd_check_seq_num(vd, desc_msg->hdr.seq_num) != 0) 44331ae08745Sheppo return (EBADMSG); 44341ae08745Sheppo 4435d10e4ef2Snarayan /* 4436d10e4ef2Snarayan * Valid message: Set up the in-band descriptor task and process the 4437d10e4ef2Snarayan * request. Arrange to acknowledge the client's message, unless an 4438d10e4ef2Snarayan * error processing the descriptor task results in setting 4439d10e4ef2Snarayan * VIO_SUBTYPE_NACK 4440d10e4ef2Snarayan */ 4441d10e4ef2Snarayan PR1("Valid in-band-descriptor message"); 4442d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 44433af08d82Slm66018 44443af08d82Slm66018 ASSERT(vd->inband_task.msg != NULL); 44453af08d82Slm66018 44463af08d82Slm66018 bcopy(msg, vd->inband_task.msg, msglen); 4447d10e4ef2Snarayan vd->inband_task.msglen = msglen; 44483af08d82Slm66018 44493af08d82Slm66018 /* 44503af08d82Slm66018 * The task request is now the payload of the message 44513af08d82Slm66018 * that was just copied into the body of the task. 44523af08d82Slm66018 */ 44533af08d82Slm66018 desc_msg = (vd_dring_inband_msg_t *)vd->inband_task.msg; 4454d10e4ef2Snarayan vd->inband_task.request = &desc_msg->payload; 44553af08d82Slm66018 4456d10e4ef2Snarayan return (vd_process_task(&vd->inband_task)); 44571ae08745Sheppo } 44581ae08745Sheppo 44591ae08745Sheppo static int 4460d10e4ef2Snarayan vd_process_element(vd_t *vd, vd_task_type_t type, uint32_t idx, 44613af08d82Slm66018 vio_msg_t *msg, size_t msglen) 44621ae08745Sheppo { 44631ae08745Sheppo int status; 4464d10e4ef2Snarayan boolean_t ready; 4465bbfa0259Sha137994 on_trap_data_t otd; 4466d10e4ef2Snarayan vd_dring_entry_t *elem = VD_DRING_ELEM(idx); 44671ae08745Sheppo 4468d10e4ef2Snarayan /* Accept the updated dring element */ 4469bbfa0259Sha137994 if ((status = VIO_DRING_ACQUIRE(&otd, vd->dring_mtype, 4470bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 44711ae08745Sheppo return (status); 44721ae08745Sheppo } 4473d10e4ef2Snarayan ready = (elem->hdr.dstate == VIO_DESC_READY); 4474d10e4ef2Snarayan if (ready) { 4475d10e4ef2Snarayan elem->hdr.dstate = VIO_DESC_ACCEPTED; 4476*5b7cb889Sha137994 bcopy(&elem->payload, vd->dring_task[idx].request, 4477*5b7cb889Sha137994 (vd->descriptor_size - sizeof (vio_dring_entry_hdr_t))); 4478d10e4ef2Snarayan } else { 44793af08d82Slm66018 PR0("descriptor %u not ready", idx); 4480d10e4ef2Snarayan VD_DUMP_DRING_ELEM(elem); 4481d10e4ef2Snarayan } 4482bbfa0259Sha137994 if ((status = VIO_DRING_RELEASE(vd->dring_mtype, 4483bbfa0259Sha137994 vd->dring_handle, idx, idx)) != 0) { 4484bbfa0259Sha137994 PR0("VIO_DRING_RELEASE() returned errno %d", status); 44851ae08745Sheppo return (status); 44861ae08745Sheppo } 4487d10e4ef2Snarayan if (!ready) 4488d10e4ef2Snarayan return (EBUSY); 44891ae08745Sheppo 44901ae08745Sheppo 4491d10e4ef2Snarayan /* Initialize a task and process the accepted element */ 4492d10e4ef2Snarayan PR1("Processing dring element %u", idx); 4493d10e4ef2Snarayan vd->dring_task[idx].type = type; 44943af08d82Slm66018 44953af08d82Slm66018 /* duplicate msg buf for cookies etc. */ 44963af08d82Slm66018 bcopy(msg, vd->dring_task[idx].msg, msglen); 44973af08d82Slm66018 4498d10e4ef2Snarayan vd->dring_task[idx].msglen = msglen; 4499205eeb1aSlm66018 return (vd_process_task(&vd->dring_task[idx])); 45001ae08745Sheppo } 45011ae08745Sheppo 45021ae08745Sheppo static int 4503d10e4ef2Snarayan vd_process_element_range(vd_t *vd, int start, int end, 45043af08d82Slm66018 vio_msg_t *msg, size_t msglen) 4505d10e4ef2Snarayan { 4506d10e4ef2Snarayan int i, n, nelem, status = 0; 4507d10e4ef2Snarayan boolean_t inprogress = B_FALSE; 4508d10e4ef2Snarayan vd_task_type_t type; 4509d10e4ef2Snarayan 4510d10e4ef2Snarayan 4511d10e4ef2Snarayan ASSERT(start >= 0); 4512d10e4ef2Snarayan ASSERT(end >= 0); 4513d10e4ef2Snarayan 4514d10e4ef2Snarayan /* 4515d10e4ef2Snarayan * Arrange to acknowledge the client's message, unless an error 4516d10e4ef2Snarayan * processing one of the dring elements results in setting 4517d10e4ef2Snarayan * VIO_SUBTYPE_NACK 4518d10e4ef2Snarayan */ 4519d10e4ef2Snarayan msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 4520d10e4ef2Snarayan 4521d10e4ef2Snarayan /* 4522d10e4ef2Snarayan * Process the dring elements in the range 4523d10e4ef2Snarayan */ 4524d10e4ef2Snarayan nelem = ((end < start) ? end + vd->dring_len : end) - start + 1; 4525d10e4ef2Snarayan for (i = start, n = nelem; n > 0; i = (i + 1) % vd->dring_len, n--) { 4526d10e4ef2Snarayan ((vio_dring_msg_t *)msg)->end_idx = i; 4527d10e4ef2Snarayan type = (n == 1) ? VD_FINAL_RANGE_TASK : VD_NONFINAL_RANGE_TASK; 45283af08d82Slm66018 status = vd_process_element(vd, type, i, msg, msglen); 4529d10e4ef2Snarayan if (status == EINPROGRESS) 4530d10e4ef2Snarayan inprogress = B_TRUE; 4531d10e4ef2Snarayan else if (status != 0) 4532d10e4ef2Snarayan break; 4533d10e4ef2Snarayan } 4534d10e4ef2Snarayan 4535d10e4ef2Snarayan /* 4536d10e4ef2Snarayan * If some, but not all, operations of a multi-element range are in 4537d10e4ef2Snarayan * progress, wait for other operations to complete before returning 4538d10e4ef2Snarayan * (which will result in "ack" or "nack" of the message). Note that 4539d10e4ef2Snarayan * all outstanding operations will need to complete, not just the ones 4540d10e4ef2Snarayan * corresponding to the current range of dring elements; howevever, as 4541d10e4ef2Snarayan * this situation is an error case, performance is less critical. 4542d10e4ef2Snarayan */ 4543d10e4ef2Snarayan if ((nelem > 1) && (status != EINPROGRESS) && inprogress) 4544d10e4ef2Snarayan ddi_taskq_wait(vd->completionq); 4545d10e4ef2Snarayan 4546d10e4ef2Snarayan return (status); 4547d10e4ef2Snarayan } 4548d10e4ef2Snarayan 4549d10e4ef2Snarayan static int 45503af08d82Slm66018 vd_process_dring_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 45511ae08745Sheppo { 45521ae08745Sheppo vio_dring_msg_t *dring_msg = (vio_dring_msg_t *)msg; 45531ae08745Sheppo 45541ae08745Sheppo 45551ae08745Sheppo ASSERT(msglen >= sizeof (msg->tag)); 45561ae08745Sheppo 45571ae08745Sheppo if (!vd_msgtype(&msg->tag, VIO_TYPE_DATA, VIO_SUBTYPE_INFO, 45581ae08745Sheppo VIO_DRING_DATA)) { 4559d10e4ef2Snarayan PR1("Message is not a dring-data message"); 4560d10e4ef2Snarayan return (ENOMSG); 45611ae08745Sheppo } 45621ae08745Sheppo 45631ae08745Sheppo if (msglen != sizeof (*dring_msg)) { 45643af08d82Slm66018 PR0("Expected %lu-byte dring message; received %lu bytes", 45651ae08745Sheppo sizeof (*dring_msg), msglen); 45661ae08745Sheppo return (EBADMSG); 45671ae08745Sheppo } 45681ae08745Sheppo 4569d10e4ef2Snarayan if (vd_check_seq_num(vd, dring_msg->seq_num) != 0) 45701ae08745Sheppo return (EBADMSG); 45711ae08745Sheppo 45721ae08745Sheppo if (dring_msg->dring_ident != vd->dring_ident) { 45733af08d82Slm66018 PR0("Expected dring ident %lu; received ident %lu", 45741ae08745Sheppo vd->dring_ident, dring_msg->dring_ident); 45751ae08745Sheppo return (EBADMSG); 45761ae08745Sheppo } 45771ae08745Sheppo 4578d10e4ef2Snarayan if (dring_msg->start_idx >= vd->dring_len) { 45793af08d82Slm66018 PR0("\"start_idx\" = %u; must be less than %u", 4580d10e4ef2Snarayan dring_msg->start_idx, vd->dring_len); 4581d10e4ef2Snarayan return (EBADMSG); 4582d10e4ef2Snarayan } 45831ae08745Sheppo 4584d10e4ef2Snarayan if ((dring_msg->end_idx < 0) || 4585d10e4ef2Snarayan (dring_msg->end_idx >= vd->dring_len)) { 45863af08d82Slm66018 PR0("\"end_idx\" = %u; must be >= 0 and less than %u", 4587d10e4ef2Snarayan dring_msg->end_idx, vd->dring_len); 4588d10e4ef2Snarayan return (EBADMSG); 4589d10e4ef2Snarayan } 4590d10e4ef2Snarayan 4591d10e4ef2Snarayan /* Valid message; process range of updated dring elements */ 4592d10e4ef2Snarayan PR1("Processing descriptor range, start = %u, end = %u", 4593d10e4ef2Snarayan dring_msg->start_idx, dring_msg->end_idx); 4594d10e4ef2Snarayan return (vd_process_element_range(vd, dring_msg->start_idx, 45953af08d82Slm66018 dring_msg->end_idx, msg, msglen)); 45961ae08745Sheppo } 45971ae08745Sheppo 45981ae08745Sheppo static int 45991ae08745Sheppo recv_msg(ldc_handle_t ldc_handle, void *msg, size_t *nbytes) 46001ae08745Sheppo { 46011ae08745Sheppo int retry, status; 46021ae08745Sheppo size_t size = *nbytes; 46031ae08745Sheppo 46041ae08745Sheppo 46051ae08745Sheppo for (retry = 0, status = ETIMEDOUT; 46061ae08745Sheppo retry < vds_ldc_retries && status == ETIMEDOUT; 46071ae08745Sheppo retry++) { 46081ae08745Sheppo PR1("ldc_read() attempt %d", (retry + 1)); 46091ae08745Sheppo *nbytes = size; 46101ae08745Sheppo status = ldc_read(ldc_handle, msg, nbytes); 46111ae08745Sheppo } 46121ae08745Sheppo 46133af08d82Slm66018 if (status) { 46143af08d82Slm66018 PR0("ldc_read() returned errno %d", status); 46153af08d82Slm66018 if (status != ECONNRESET) 46163af08d82Slm66018 return (ENOMSG); 46171ae08745Sheppo return (status); 46181ae08745Sheppo } else if (*nbytes == 0) { 46191ae08745Sheppo PR1("ldc_read() returned 0 and no message read"); 46201ae08745Sheppo return (ENOMSG); 46211ae08745Sheppo } 46221ae08745Sheppo 46231ae08745Sheppo PR1("RCVD %lu-byte message", *nbytes); 46241ae08745Sheppo return (0); 46251ae08745Sheppo } 46261ae08745Sheppo 46271ae08745Sheppo static int 46283af08d82Slm66018 vd_do_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 46291ae08745Sheppo { 46301ae08745Sheppo int status; 46311ae08745Sheppo 46321ae08745Sheppo 46331ae08745Sheppo PR1("Processing (%x/%x/%x) message", msg->tag.vio_msgtype, 46341ae08745Sheppo msg->tag.vio_subtype, msg->tag.vio_subtype_env); 46353af08d82Slm66018 #ifdef DEBUG 46363af08d82Slm66018 vd_decode_tag(msg); 46373af08d82Slm66018 #endif 46381ae08745Sheppo 46391ae08745Sheppo /* 46401ae08745Sheppo * Validate session ID up front, since it applies to all messages 46411ae08745Sheppo * once set 46421ae08745Sheppo */ 46431ae08745Sheppo if ((msg->tag.vio_sid != vd->sid) && (vd->initialized & VD_SID)) { 46443af08d82Slm66018 PR0("Expected SID %u, received %u", vd->sid, 46451ae08745Sheppo msg->tag.vio_sid); 46461ae08745Sheppo return (EBADMSG); 46471ae08745Sheppo } 46481ae08745Sheppo 46493af08d82Slm66018 PR1("\tWhile in state %d (%s)", vd->state, vd_decode_state(vd->state)); 46501ae08745Sheppo 46511ae08745Sheppo /* 46521ae08745Sheppo * Process the received message based on connection state 46531ae08745Sheppo */ 46541ae08745Sheppo switch (vd->state) { 46551ae08745Sheppo case VD_STATE_INIT: /* expect version message */ 46560a55fbb7Slm66018 if ((status = vd_process_ver_msg(vd, msg, msglen)) != 0) 46571ae08745Sheppo return (status); 46581ae08745Sheppo 46591ae08745Sheppo /* Version negotiated, move to that state */ 46601ae08745Sheppo vd->state = VD_STATE_VER; 46611ae08745Sheppo return (0); 46621ae08745Sheppo 46631ae08745Sheppo case VD_STATE_VER: /* expect attribute message */ 46641ae08745Sheppo if ((status = vd_process_attr_msg(vd, msg, msglen)) != 0) 46651ae08745Sheppo return (status); 46661ae08745Sheppo 46671ae08745Sheppo /* Attributes exchanged, move to that state */ 46681ae08745Sheppo vd->state = VD_STATE_ATTR; 46691ae08745Sheppo return (0); 46701ae08745Sheppo 46711ae08745Sheppo case VD_STATE_ATTR: 46721ae08745Sheppo switch (vd->xfer_mode) { 46731ae08745Sheppo case VIO_DESC_MODE: /* expect RDX message */ 46741ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) != 0) 46751ae08745Sheppo return (status); 46761ae08745Sheppo 46771ae08745Sheppo /* Ready to receive in-band descriptors */ 46781ae08745Sheppo vd->state = VD_STATE_DATA; 46791ae08745Sheppo return (0); 46801ae08745Sheppo 4681f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect register-dring message */ 46821ae08745Sheppo if ((status = 46831ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != 0) 46841ae08745Sheppo return (status); 46851ae08745Sheppo 46861ae08745Sheppo /* One dring negotiated, move to that state */ 46871ae08745Sheppo vd->state = VD_STATE_DRING; 46881ae08745Sheppo return (0); 46891ae08745Sheppo 46901ae08745Sheppo default: 46911ae08745Sheppo ASSERT("Unsupported transfer mode"); 46923af08d82Slm66018 PR0("Unsupported transfer mode"); 46931ae08745Sheppo return (ENOTSUP); 46941ae08745Sheppo } 46951ae08745Sheppo 46961ae08745Sheppo case VD_STATE_DRING: /* expect RDX, register-dring, or unreg-dring */ 46971ae08745Sheppo if ((status = process_rdx_msg(msg, msglen)) == 0) { 46981ae08745Sheppo /* Ready to receive data */ 46991ae08745Sheppo vd->state = VD_STATE_DATA; 47001ae08745Sheppo return (0); 47011ae08745Sheppo } else if (status != ENOMSG) { 47021ae08745Sheppo return (status); 47031ae08745Sheppo } 47041ae08745Sheppo 47051ae08745Sheppo 47061ae08745Sheppo /* 47071ae08745Sheppo * If another register-dring message is received, stay in 47081ae08745Sheppo * dring state in case the client sends RDX; although the 47091ae08745Sheppo * protocol allows multiple drings, this server does not 47101ae08745Sheppo * support using more than one 47111ae08745Sheppo */ 47121ae08745Sheppo if ((status = 47131ae08745Sheppo vd_process_dring_reg_msg(vd, msg, msglen)) != ENOMSG) 47141ae08745Sheppo return (status); 47151ae08745Sheppo 47161ae08745Sheppo /* 47171ae08745Sheppo * Acknowledge an unregister-dring message, but reset the 47181ae08745Sheppo * connection anyway: Although the protocol allows 47191ae08745Sheppo * unregistering drings, this server cannot serve a vdisk 47201ae08745Sheppo * without its only dring 47211ae08745Sheppo */ 47221ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 47231ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 47241ae08745Sheppo 47251ae08745Sheppo case VD_STATE_DATA: 47261ae08745Sheppo switch (vd->xfer_mode) { 47271ae08745Sheppo case VIO_DESC_MODE: /* expect in-band-descriptor message */ 47283af08d82Slm66018 return (vd_process_desc_msg(vd, msg, msglen)); 47291ae08745Sheppo 4730f0ca1d9aSsb155480 case VIO_DRING_MODE_V1_0: /* expect dring-data or unreg-dring */ 47311ae08745Sheppo /* 47321ae08745Sheppo * Typically expect dring-data messages, so handle 47331ae08745Sheppo * them first 47341ae08745Sheppo */ 47351ae08745Sheppo if ((status = vd_process_dring_msg(vd, msg, 47363af08d82Slm66018 msglen)) != ENOMSG) 47371ae08745Sheppo return (status); 47381ae08745Sheppo 47391ae08745Sheppo /* 47401ae08745Sheppo * Acknowledge an unregister-dring message, but reset 47411ae08745Sheppo * the connection anyway: Although the protocol 47421ae08745Sheppo * allows unregistering drings, this server cannot 47431ae08745Sheppo * serve a vdisk without its only dring 47441ae08745Sheppo */ 47451ae08745Sheppo status = vd_process_dring_unreg_msg(vd, msg, msglen); 47461ae08745Sheppo return ((status == 0) ? ENOTSUP : status); 47471ae08745Sheppo 47481ae08745Sheppo default: 47491ae08745Sheppo ASSERT("Unsupported transfer mode"); 47503af08d82Slm66018 PR0("Unsupported transfer mode"); 47511ae08745Sheppo return (ENOTSUP); 47521ae08745Sheppo } 47531ae08745Sheppo 47541ae08745Sheppo default: 47551ae08745Sheppo ASSERT("Invalid client connection state"); 47563af08d82Slm66018 PR0("Invalid client connection state"); 47571ae08745Sheppo return (ENOTSUP); 47581ae08745Sheppo } 47591ae08745Sheppo } 47601ae08745Sheppo 4761d10e4ef2Snarayan static int 47623af08d82Slm66018 vd_process_msg(vd_t *vd, vio_msg_t *msg, size_t msglen) 47631ae08745Sheppo { 47641ae08745Sheppo int status; 47651ae08745Sheppo boolean_t reset_ldc = B_FALSE; 4766205eeb1aSlm66018 vd_task_t task; 47671ae08745Sheppo 47681ae08745Sheppo /* 47691ae08745Sheppo * Check that the message is at least big enough for a "tag", so that 47701ae08745Sheppo * message processing can proceed based on tag-specified message type 47711ae08745Sheppo */ 47721ae08745Sheppo if (msglen < sizeof (vio_msg_tag_t)) { 47733af08d82Slm66018 PR0("Received short (%lu-byte) message", msglen); 47741ae08745Sheppo /* Can't "nack" short message, so drop the big hammer */ 47753af08d82Slm66018 PR0("initiating full reset"); 4776d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 4777d10e4ef2Snarayan return (EBADMSG); 47781ae08745Sheppo } 47791ae08745Sheppo 47801ae08745Sheppo /* 47811ae08745Sheppo * Process the message 47821ae08745Sheppo */ 47833af08d82Slm66018 switch (status = vd_do_process_msg(vd, msg, msglen)) { 47841ae08745Sheppo case 0: 47851ae08745Sheppo /* "ack" valid, successfully-processed messages */ 47861ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_ACK; 47871ae08745Sheppo break; 47881ae08745Sheppo 4789d10e4ef2Snarayan case EINPROGRESS: 4790d10e4ef2Snarayan /* The completion handler will "ack" or "nack" the message */ 4791d10e4ef2Snarayan return (EINPROGRESS); 47921ae08745Sheppo case ENOMSG: 47933af08d82Slm66018 PR0("Received unexpected message"); 47941ae08745Sheppo _NOTE(FALLTHROUGH); 47951ae08745Sheppo case EBADMSG: 47961ae08745Sheppo case ENOTSUP: 4797205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 47981ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 47991ae08745Sheppo break; 48001ae08745Sheppo 48011ae08745Sheppo default: 4802205eeb1aSlm66018 /* "transport" error will cause NACK of invalid messages */ 48031ae08745Sheppo msg->tag.vio_subtype = VIO_SUBTYPE_NACK; 48041ae08745Sheppo /* An LDC error probably occurred, so try resetting it */ 48051ae08745Sheppo reset_ldc = B_TRUE; 48061ae08745Sheppo break; 48071ae08745Sheppo } 48081ae08745Sheppo 48093af08d82Slm66018 PR1("\tResulting in state %d (%s)", vd->state, 48103af08d82Slm66018 vd_decode_state(vd->state)); 48113af08d82Slm66018 4812205eeb1aSlm66018 /* populate the task so we can dispatch it on the taskq */ 4813205eeb1aSlm66018 task.vd = vd; 4814205eeb1aSlm66018 task.msg = msg; 4815205eeb1aSlm66018 task.msglen = msglen; 4816205eeb1aSlm66018 4817205eeb1aSlm66018 /* 4818205eeb1aSlm66018 * Queue a task to send the notification that the operation completed. 4819205eeb1aSlm66018 * We need to ensure that requests are responded to in the correct 4820205eeb1aSlm66018 * order and since the taskq is processed serially this ordering 4821205eeb1aSlm66018 * is maintained. 4822205eeb1aSlm66018 */ 4823205eeb1aSlm66018 (void) ddi_taskq_dispatch(vd->completionq, vd_serial_notify, 4824205eeb1aSlm66018 &task, DDI_SLEEP); 4825205eeb1aSlm66018 4826205eeb1aSlm66018 /* 4827205eeb1aSlm66018 * To ensure handshake negotiations do not happen out of order, such 4828205eeb1aSlm66018 * requests that come through this path should not be done in parallel 4829205eeb1aSlm66018 * so we need to wait here until the response is sent to the client. 4830205eeb1aSlm66018 */ 4831205eeb1aSlm66018 ddi_taskq_wait(vd->completionq); 48321ae08745Sheppo 4833d10e4ef2Snarayan /* Arrange to reset the connection for nack'ed or failed messages */ 48343af08d82Slm66018 if ((status != 0) || reset_ldc) { 48353af08d82Slm66018 PR0("initiating %s reset", 48363af08d82Slm66018 (reset_ldc) ? "full" : "soft"); 4837d10e4ef2Snarayan vd_need_reset(vd, reset_ldc); 48383af08d82Slm66018 } 4839d10e4ef2Snarayan 4840d10e4ef2Snarayan return (status); 4841d10e4ef2Snarayan } 4842d10e4ef2Snarayan 4843d10e4ef2Snarayan static boolean_t 4844d10e4ef2Snarayan vd_enabled(vd_t *vd) 4845d10e4ef2Snarayan { 4846d10e4ef2Snarayan boolean_t enabled; 4847d10e4ef2Snarayan 4848d10e4ef2Snarayan mutex_enter(&vd->lock); 4849d10e4ef2Snarayan enabled = vd->enabled; 4850d10e4ef2Snarayan mutex_exit(&vd->lock); 4851d10e4ef2Snarayan return (enabled); 48521ae08745Sheppo } 48531ae08745Sheppo 48541ae08745Sheppo static void 48550a55fbb7Slm66018 vd_recv_msg(void *arg) 48561ae08745Sheppo { 48571ae08745Sheppo vd_t *vd = (vd_t *)arg; 48583af08d82Slm66018 int rv = 0, status = 0; 48591ae08745Sheppo 48601ae08745Sheppo ASSERT(vd != NULL); 48613af08d82Slm66018 4862d10e4ef2Snarayan PR2("New task to receive incoming message(s)"); 48633af08d82Slm66018 48643af08d82Slm66018 4865d10e4ef2Snarayan while (vd_enabled(vd) && status == 0) { 4866d10e4ef2Snarayan size_t msglen, msgsize; 48673af08d82Slm66018 ldc_status_t lstatus; 4868d10e4ef2Snarayan 48690a55fbb7Slm66018 /* 4870d10e4ef2Snarayan * Receive and process a message 48710a55fbb7Slm66018 */ 4872d10e4ef2Snarayan vd_reset_if_needed(vd); /* can change vd->max_msglen */ 48733af08d82Slm66018 48743af08d82Slm66018 /* 48753af08d82Slm66018 * check if channel is UP - else break out of loop 48763af08d82Slm66018 */ 48773af08d82Slm66018 status = ldc_status(vd->ldc_handle, &lstatus); 48783af08d82Slm66018 if (lstatus != LDC_UP) { 48793af08d82Slm66018 PR0("channel not up (status=%d), exiting recv loop\n", 48803af08d82Slm66018 lstatus); 48813af08d82Slm66018 break; 48823af08d82Slm66018 } 48833af08d82Slm66018 48843af08d82Slm66018 ASSERT(vd->max_msglen != 0); 48853af08d82Slm66018 4886d10e4ef2Snarayan msgsize = vd->max_msglen; /* stable copy for alloc/free */ 48873af08d82Slm66018 msglen = msgsize; /* actual len after recv_msg() */ 48883af08d82Slm66018 48893af08d82Slm66018 status = recv_msg(vd->ldc_handle, vd->vio_msgp, &msglen); 48903af08d82Slm66018 switch (status) { 48913af08d82Slm66018 case 0: 48923af08d82Slm66018 rv = vd_process_msg(vd, (vio_msg_t *)vd->vio_msgp, 48933af08d82Slm66018 msglen); 48943af08d82Slm66018 /* check if max_msglen changed */ 48953af08d82Slm66018 if (msgsize != vd->max_msglen) { 48963af08d82Slm66018 PR0("max_msglen changed 0x%lx to 0x%lx bytes\n", 48973af08d82Slm66018 msgsize, vd->max_msglen); 48983af08d82Slm66018 kmem_free(vd->vio_msgp, msgsize); 48993af08d82Slm66018 vd->vio_msgp = 49003af08d82Slm66018 kmem_alloc(vd->max_msglen, KM_SLEEP); 49013af08d82Slm66018 } 49023af08d82Slm66018 if (rv == EINPROGRESS) 49033af08d82Slm66018 continue; 49043af08d82Slm66018 break; 49053af08d82Slm66018 49063af08d82Slm66018 case ENOMSG: 49073af08d82Slm66018 break; 49083af08d82Slm66018 49093af08d82Slm66018 case ECONNRESET: 49103af08d82Slm66018 PR0("initiating soft reset (ECONNRESET)\n"); 49113af08d82Slm66018 vd_need_reset(vd, B_FALSE); 49123af08d82Slm66018 status = 0; 49133af08d82Slm66018 break; 49143af08d82Slm66018 49153af08d82Slm66018 default: 4916d10e4ef2Snarayan /* Probably an LDC failure; arrange to reset it */ 49173af08d82Slm66018 PR0("initiating full reset (status=0x%x)", status); 4918d10e4ef2Snarayan vd_need_reset(vd, B_TRUE); 49193af08d82Slm66018 break; 49200a55fbb7Slm66018 } 49211ae08745Sheppo } 49223af08d82Slm66018 4923d10e4ef2Snarayan PR2("Task finished"); 49240a55fbb7Slm66018 } 49250a55fbb7Slm66018 49260a55fbb7Slm66018 static uint_t 49271ae08745Sheppo vd_handle_ldc_events(uint64_t event, caddr_t arg) 49281ae08745Sheppo { 49291ae08745Sheppo vd_t *vd = (vd_t *)(void *)arg; 49303af08d82Slm66018 int status; 49311ae08745Sheppo 49321ae08745Sheppo ASSERT(vd != NULL); 4933d10e4ef2Snarayan 4934d10e4ef2Snarayan if (!vd_enabled(vd)) 4935d10e4ef2Snarayan return (LDC_SUCCESS); 4936d10e4ef2Snarayan 49373af08d82Slm66018 if (event & LDC_EVT_DOWN) { 493834683adeSsg70180 PR0("LDC_EVT_DOWN: LDC channel went down"); 49393af08d82Slm66018 49403af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49413af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 49423af08d82Slm66018 DDI_SLEEP); 49433af08d82Slm66018 if (status == DDI_FAILURE) { 49443af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49453af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49463af08d82Slm66018 } 49473af08d82Slm66018 } 49483af08d82Slm66018 4949d10e4ef2Snarayan if (event & LDC_EVT_RESET) { 49503af08d82Slm66018 PR0("LDC_EVT_RESET: LDC channel was reset"); 49513af08d82Slm66018 49523af08d82Slm66018 if (vd->state != VD_STATE_INIT) { 49533af08d82Slm66018 PR0("scheduling full reset"); 49543af08d82Slm66018 vd_need_reset(vd, B_FALSE); 49553af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 49563af08d82Slm66018 vd, DDI_SLEEP); 49573af08d82Slm66018 if (status == DDI_FAILURE) { 49583af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49593af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49603af08d82Slm66018 } 49613af08d82Slm66018 49623af08d82Slm66018 } else { 49633af08d82Slm66018 PR0("channel already reset, ignoring...\n"); 49643af08d82Slm66018 PR0("doing ldc up...\n"); 49653af08d82Slm66018 (void) ldc_up(vd->ldc_handle); 49663af08d82Slm66018 } 49673af08d82Slm66018 4968d10e4ef2Snarayan return (LDC_SUCCESS); 4969d10e4ef2Snarayan } 4970d10e4ef2Snarayan 4971d10e4ef2Snarayan if (event & LDC_EVT_UP) { 49723af08d82Slm66018 PR0("EVT_UP: LDC is up\nResetting client connection state"); 49733af08d82Slm66018 PR0("initiating soft reset"); 4974d10e4ef2Snarayan vd_need_reset(vd, B_FALSE); 49753af08d82Slm66018 status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, 49763af08d82Slm66018 vd, DDI_SLEEP); 49773af08d82Slm66018 if (status == DDI_FAILURE) { 49783af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49793af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49803af08d82Slm66018 return (LDC_SUCCESS); 49813af08d82Slm66018 } 4982d10e4ef2Snarayan } 4983d10e4ef2Snarayan 4984d10e4ef2Snarayan if (event & LDC_EVT_READ) { 4985d10e4ef2Snarayan int status; 4986d10e4ef2Snarayan 4987d10e4ef2Snarayan PR1("New data available"); 4988d10e4ef2Snarayan /* Queue a task to receive the new data */ 4989d10e4ef2Snarayan status = ddi_taskq_dispatch(vd->startq, vd_recv_msg, vd, 4990d10e4ef2Snarayan DDI_SLEEP); 49913af08d82Slm66018 49923af08d82Slm66018 if (status == DDI_FAILURE) { 49933af08d82Slm66018 PR0("cannot schedule task to recv msg\n"); 49943af08d82Slm66018 vd_need_reset(vd, B_TRUE); 49953af08d82Slm66018 } 4996d10e4ef2Snarayan } 4997d10e4ef2Snarayan 4998d10e4ef2Snarayan return (LDC_SUCCESS); 49991ae08745Sheppo } 50001ae08745Sheppo 50011ae08745Sheppo static uint_t 50021ae08745Sheppo vds_check_for_vd(mod_hash_key_t key, mod_hash_val_t *val, void *arg) 50031ae08745Sheppo { 50041ae08745Sheppo _NOTE(ARGUNUSED(key, val)) 50051ae08745Sheppo (*((uint_t *)arg))++; 50061ae08745Sheppo return (MH_WALK_TERMINATE); 50071ae08745Sheppo } 50081ae08745Sheppo 50091ae08745Sheppo 50101ae08745Sheppo static int 50111ae08745Sheppo vds_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 50121ae08745Sheppo { 50131ae08745Sheppo uint_t vd_present = 0; 50141ae08745Sheppo minor_t instance; 50151ae08745Sheppo vds_t *vds; 50161ae08745Sheppo 50171ae08745Sheppo 50181ae08745Sheppo switch (cmd) { 50191ae08745Sheppo case DDI_DETACH: 50201ae08745Sheppo /* the real work happens below */ 50211ae08745Sheppo break; 50221ae08745Sheppo case DDI_SUSPEND: 5023d10e4ef2Snarayan PR0("No action required for DDI_SUSPEND"); 50241ae08745Sheppo return (DDI_SUCCESS); 50251ae08745Sheppo default: 50263af08d82Slm66018 PR0("Unrecognized \"cmd\""); 50271ae08745Sheppo return (DDI_FAILURE); 50281ae08745Sheppo } 50291ae08745Sheppo 50301ae08745Sheppo ASSERT(cmd == DDI_DETACH); 50311ae08745Sheppo instance = ddi_get_instance(dip); 50321ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 50333af08d82Slm66018 PR0("Could not get state for instance %u", instance); 50341ae08745Sheppo ddi_soft_state_free(vds_state, instance); 50351ae08745Sheppo return (DDI_FAILURE); 50361ae08745Sheppo } 50371ae08745Sheppo 50381ae08745Sheppo /* Do no detach when serving any vdisks */ 50391ae08745Sheppo mod_hash_walk(vds->vd_table, vds_check_for_vd, &vd_present); 50401ae08745Sheppo if (vd_present) { 50411ae08745Sheppo PR0("Not detaching because serving vdisks"); 50421ae08745Sheppo return (DDI_FAILURE); 50431ae08745Sheppo } 50441ae08745Sheppo 50451ae08745Sheppo PR0("Detaching"); 5046445b4c2eSsb155480 if (vds->initialized & VDS_MDEG) { 50471ae08745Sheppo (void) mdeg_unregister(vds->mdeg); 5048445b4c2eSsb155480 kmem_free(vds->ispecp->specp, sizeof (vds_prop_template)); 5049445b4c2eSsb155480 kmem_free(vds->ispecp, sizeof (mdeg_node_spec_t)); 5050445b4c2eSsb155480 vds->ispecp = NULL; 5051445b4c2eSsb155480 vds->mdeg = NULL; 5052445b4c2eSsb155480 } 5053445b4c2eSsb155480 50548fce2fd6Sachartre vds_driver_types_free(vds); 50558fce2fd6Sachartre 50561ae08745Sheppo if (vds->initialized & VDS_LDI) 50571ae08745Sheppo (void) ldi_ident_release(vds->ldi_ident); 50581ae08745Sheppo mod_hash_destroy_hash(vds->vd_table); 50591ae08745Sheppo ddi_soft_state_free(vds_state, instance); 50601ae08745Sheppo return (DDI_SUCCESS); 50611ae08745Sheppo } 50621ae08745Sheppo 506317cadca8Slm66018 /* 506417cadca8Slm66018 * Description: 506517cadca8Slm66018 * This function checks to see if the file being used as a 506617cadca8Slm66018 * virtual disk is an ISO image. An ISO image is a special 506717cadca8Slm66018 * case which can be booted/installed from like a CD/DVD 506817cadca8Slm66018 * 506917cadca8Slm66018 * Parameters: 507017cadca8Slm66018 * vd - disk on which the operation is performed. 507117cadca8Slm66018 * 507217cadca8Slm66018 * Return Code: 507317cadca8Slm66018 * B_TRUE - The file is an ISO 9660 compliant image 507417cadca8Slm66018 * B_FALSE - just a regular disk image file 507517cadca8Slm66018 */ 507617cadca8Slm66018 static boolean_t 507717cadca8Slm66018 vd_file_is_iso_image(vd_t *vd) 507817cadca8Slm66018 { 507917cadca8Slm66018 char iso_buf[ISO_SECTOR_SIZE]; 508017cadca8Slm66018 int i, rv; 508117cadca8Slm66018 uint_t sec; 508217cadca8Slm66018 508317cadca8Slm66018 ASSERT(vd->file); 508417cadca8Slm66018 508517cadca8Slm66018 /* 508617cadca8Slm66018 * If we have already discovered and saved this info we can 508717cadca8Slm66018 * short-circuit the check and avoid reading the file. 508817cadca8Slm66018 */ 508917cadca8Slm66018 if (vd->vdisk_media == VD_MEDIA_DVD || vd->vdisk_media == VD_MEDIA_CD) 509017cadca8Slm66018 return (B_TRUE); 509117cadca8Slm66018 509217cadca8Slm66018 /* 509317cadca8Slm66018 * We wish to read the sector that should contain the 2nd ISO volume 509417cadca8Slm66018 * descriptor. The second field in this descriptor is called the 509517cadca8Slm66018 * Standard Identifier and is set to CD001 for a CD-ROM compliant 509617cadca8Slm66018 * to the ISO 9660 standard. 509717cadca8Slm66018 */ 509817cadca8Slm66018 sec = (ISO_VOLDESC_SEC * ISO_SECTOR_SIZE) / vd->vdisk_block_size; 509917cadca8Slm66018 rv = vd_file_rw(vd, VD_SLICE_NONE, VD_OP_BREAD, (caddr_t)iso_buf, 510017cadca8Slm66018 sec, ISO_SECTOR_SIZE); 510117cadca8Slm66018 510217cadca8Slm66018 if (rv < 0) 510317cadca8Slm66018 return (B_FALSE); 510417cadca8Slm66018 510517cadca8Slm66018 for (i = 0; i < ISO_ID_STRLEN; i++) { 510617cadca8Slm66018 if (ISO_STD_ID(iso_buf)[i] != ISO_ID_STRING[i]) 510717cadca8Slm66018 return (B_FALSE); 510817cadca8Slm66018 } 510917cadca8Slm66018 511017cadca8Slm66018 return (B_TRUE); 511117cadca8Slm66018 } 511217cadca8Slm66018 511317cadca8Slm66018 /* 511417cadca8Slm66018 * Description: 511517cadca8Slm66018 * This function checks to see if the virtual device is an ATAPI 511617cadca8Slm66018 * device. ATAPI devices use Group 1 Read/Write commands, so 511717cadca8Slm66018 * any USCSI calls vds makes need to take this into account. 511817cadca8Slm66018 * 511917cadca8Slm66018 * Parameters: 512017cadca8Slm66018 * vd - disk on which the operation is performed. 512117cadca8Slm66018 * 512217cadca8Slm66018 * Return Code: 512317cadca8Slm66018 * B_TRUE - The virtual disk is backed by an ATAPI device 512417cadca8Slm66018 * B_FALSE - not an ATAPI device (presumably SCSI) 512517cadca8Slm66018 */ 512617cadca8Slm66018 static boolean_t 512717cadca8Slm66018 vd_is_atapi_device(vd_t *vd) 512817cadca8Slm66018 { 512917cadca8Slm66018 boolean_t is_atapi = B_FALSE; 513017cadca8Slm66018 char *variantp; 513117cadca8Slm66018 int rv; 513217cadca8Slm66018 513317cadca8Slm66018 ASSERT(vd->ldi_handle[0] != NULL); 513417cadca8Slm66018 ASSERT(!vd->file); 513517cadca8Slm66018 513617cadca8Slm66018 rv = ldi_prop_lookup_string(vd->ldi_handle[0], 513717cadca8Slm66018 (LDI_DEV_T_ANY | DDI_PROP_DONTPASS), "variant", &variantp); 513817cadca8Slm66018 if (rv == DDI_PROP_SUCCESS) { 513917cadca8Slm66018 PR0("'variant' property exists for %s", vd->device_path); 514017cadca8Slm66018 if (strcmp(variantp, "atapi") == 0) 514117cadca8Slm66018 is_atapi = B_TRUE; 514217cadca8Slm66018 ddi_prop_free(variantp); 514317cadca8Slm66018 } 514417cadca8Slm66018 514517cadca8Slm66018 rv = ldi_prop_exists(vd->ldi_handle[0], LDI_DEV_T_ANY, "atapi"); 514617cadca8Slm66018 if (rv) { 514717cadca8Slm66018 PR0("'atapi' property exists for %s", vd->device_path); 514817cadca8Slm66018 is_atapi = B_TRUE; 514917cadca8Slm66018 } 515017cadca8Slm66018 515117cadca8Slm66018 return (is_atapi); 515217cadca8Slm66018 } 515317cadca8Slm66018 51541ae08745Sheppo static int 51552f5224aeSachartre vd_setup_mediainfo(vd_t *vd) 51560a55fbb7Slm66018 { 51572f5224aeSachartre int status, rval; 51584bac2208Snarayan struct dk_minfo dk_minfo; 51590a55fbb7Slm66018 51602f5224aeSachartre ASSERT(vd->ldi_handle[0] != NULL); 51612f5224aeSachartre ASSERT(vd->vdisk_block_size != 0); 51622f5224aeSachartre 51632f5224aeSachartre if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCGMEDIAINFO, 51642f5224aeSachartre (intptr_t)&dk_minfo, (vd->open_flags | FKIOCTL), 51652f5224aeSachartre kcred, &rval)) != 0) 51662f5224aeSachartre return (status); 51672f5224aeSachartre 51682f5224aeSachartre ASSERT(dk_minfo.dki_lbsize % vd->vdisk_block_size == 0); 51692f5224aeSachartre 51702f5224aeSachartre vd->block_size = dk_minfo.dki_lbsize; 51712f5224aeSachartre vd->vdisk_size = (dk_minfo.dki_capacity * dk_minfo.dki_lbsize) / 51722f5224aeSachartre vd->vdisk_block_size; 51732f5224aeSachartre vd->vdisk_media = DK_MEDIATYPE2VD_MEDIATYPE(dk_minfo.dki_media_type); 51742f5224aeSachartre return (0); 51752f5224aeSachartre } 51762f5224aeSachartre 51772f5224aeSachartre static int 51782f5224aeSachartre vd_setup_full_disk(vd_t *vd) 51792f5224aeSachartre { 51802f5224aeSachartre int status; 51812f5224aeSachartre major_t major = getmajor(vd->dev[0]); 51822f5224aeSachartre minor_t minor = getminor(vd->dev[0]) - VD_ENTIRE_DISK_SLICE; 51832f5224aeSachartre 5184047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_DISK); 5185047ba61eSachartre 51862f5224aeSachartre vd->vdisk_block_size = DEV_BSIZE; 51872f5224aeSachartre 51884bac2208Snarayan /* 51894bac2208Snarayan * At this point, vdisk_size is set to the size of partition 2 but 51904bac2208Snarayan * this does not represent the size of the disk because partition 2 51914bac2208Snarayan * may not cover the entire disk and its size does not include reserved 51922f5224aeSachartre * blocks. So we call vd_get_mediainfo to udpate this information and 51932f5224aeSachartre * set the block size and the media type of the disk. 51944bac2208Snarayan */ 51952f5224aeSachartre status = vd_setup_mediainfo(vd); 51962f5224aeSachartre 51972f5224aeSachartre if (status != 0) { 51982f5224aeSachartre if (!vd->scsi) { 51992f5224aeSachartre /* unexpected failure */ 5200690555a1Sachartre PRN("ldi_ioctl(DKIOCGMEDIAINFO) returned errno %d", 52014bac2208Snarayan status); 52020a55fbb7Slm66018 return (status); 52030a55fbb7Slm66018 } 52042f5224aeSachartre 52052f5224aeSachartre /* 52062f5224aeSachartre * The function can fail for SCSI disks which are present but 52072f5224aeSachartre * reserved by another system. In that case, we don't know the 52082f5224aeSachartre * size of the disk and the block size. 52092f5224aeSachartre */ 52102f5224aeSachartre vd->vdisk_size = VD_SIZE_UNKNOWN; 52112f5224aeSachartre vd->block_size = 0; 52122f5224aeSachartre vd->vdisk_media = VD_MEDIA_FIXED; 52132f5224aeSachartre } 52140a55fbb7Slm66018 52150a55fbb7Slm66018 /* Move dev number and LDI handle to entire-disk-slice array elements */ 52160a55fbb7Slm66018 vd->dev[VD_ENTIRE_DISK_SLICE] = vd->dev[0]; 52170a55fbb7Slm66018 vd->dev[0] = 0; 52180a55fbb7Slm66018 vd->ldi_handle[VD_ENTIRE_DISK_SLICE] = vd->ldi_handle[0]; 52190a55fbb7Slm66018 vd->ldi_handle[0] = NULL; 52200a55fbb7Slm66018 52210a55fbb7Slm66018 /* Initialize device numbers for remaining slices and open them */ 52220a55fbb7Slm66018 for (int slice = 0; slice < vd->nslices; slice++) { 52230a55fbb7Slm66018 /* 52240a55fbb7Slm66018 * Skip the entire-disk slice, as it's already open and its 52250a55fbb7Slm66018 * device known 52260a55fbb7Slm66018 */ 52270a55fbb7Slm66018 if (slice == VD_ENTIRE_DISK_SLICE) 52280a55fbb7Slm66018 continue; 52290a55fbb7Slm66018 ASSERT(vd->dev[slice] == 0); 52300a55fbb7Slm66018 ASSERT(vd->ldi_handle[slice] == NULL); 52310a55fbb7Slm66018 52320a55fbb7Slm66018 /* 52330a55fbb7Slm66018 * Construct the device number for the current slice 52340a55fbb7Slm66018 */ 52350a55fbb7Slm66018 vd->dev[slice] = makedevice(major, (minor + slice)); 52360a55fbb7Slm66018 52370a55fbb7Slm66018 /* 523834683adeSsg70180 * Open all slices of the disk to serve them to the client. 523934683adeSsg70180 * Slices are opened exclusively to prevent other threads or 524034683adeSsg70180 * processes in the service domain from performing I/O to 524134683adeSsg70180 * slices being accessed by a client. Failure to open a slice 524234683adeSsg70180 * results in vds not serving this disk, as the client could 524334683adeSsg70180 * attempt (and should be able) to access any slice immediately. 524434683adeSsg70180 * Any slices successfully opened before a failure will get 524534683adeSsg70180 * closed by vds_destroy_vd() as a result of the error returned 524634683adeSsg70180 * by this function. 524734683adeSsg70180 * 524834683adeSsg70180 * We need to do the open with FNDELAY so that opening an empty 524934683adeSsg70180 * slice does not fail. 52500a55fbb7Slm66018 */ 52510a55fbb7Slm66018 PR0("Opening device major %u, minor %u = slice %u", 52520a55fbb7Slm66018 major, minor, slice); 5253047ba61eSachartre 5254047ba61eSachartre /* 5255047ba61eSachartre * Try to open the device. This can fail for example if we are 5256047ba61eSachartre * opening an empty slice. So in case of a failure, we try the 5257047ba61eSachartre * open again but this time with the FNDELAY flag. 5258047ba61eSachartre */ 5259047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5260047ba61eSachartre vd->open_flags, kcred, &vd->ldi_handle[slice], 5261047ba61eSachartre vd->vds->ldi_ident); 5262047ba61eSachartre 5263047ba61eSachartre if (status != 0) { 5264047ba61eSachartre status = ldi_open_by_dev(&vd->dev[slice], OTYP_BLK, 5265047ba61eSachartre vd->open_flags | FNDELAY, kcred, 5266047ba61eSachartre &vd->ldi_handle[slice], vd->vds->ldi_ident); 5267047ba61eSachartre } 5268047ba61eSachartre 5269047ba61eSachartre if (status != 0) { 5270690555a1Sachartre PRN("ldi_open_by_dev() returned errno %d " 52710a55fbb7Slm66018 "for slice %u", status, slice); 52720a55fbb7Slm66018 /* vds_destroy_vd() will close any open slices */ 5273690555a1Sachartre vd->ldi_handle[slice] = NULL; 52740a55fbb7Slm66018 return (status); 52750a55fbb7Slm66018 } 52760a55fbb7Slm66018 } 52770a55fbb7Slm66018 52780a55fbb7Slm66018 return (0); 52790a55fbb7Slm66018 } 52800a55fbb7Slm66018 5281edcc0754Sachartre /* 5282edcc0754Sachartre * When a slice or a volume is exported as a single-slice disk, we want 5283edcc0754Sachartre * the disk backend (i.e. the slice or volume) to be entirely mapped as 5284edcc0754Sachartre * a slice without the addition of any metadata. 5285edcc0754Sachartre * 5286edcc0754Sachartre * So when exporting the disk as a VTOC disk, we fake a disk with the following 5287edcc0754Sachartre * layout: 5288bae9e67eSachartre * flabel +--- flabel_limit 5289bae9e67eSachartre * <-> V 5290bae9e67eSachartre * 0 1 C D E 5291bae9e67eSachartre * +-+---+--------------------------+--+ 5292bae9e67eSachartre * virtual disk: |L|XXX| slice 0 |AA| 5293bae9e67eSachartre * +-+---+--------------------------+--+ 5294edcc0754Sachartre * ^ : : 5295edcc0754Sachartre * | : : 5296edcc0754Sachartre * VTOC LABEL--+ : : 5297edcc0754Sachartre * +--------------------------+ 5298bae9e67eSachartre * disk backend: | slice/volume/file | 5299edcc0754Sachartre * +--------------------------+ 5300edcc0754Sachartre * 0 N 5301edcc0754Sachartre * 5302bae9e67eSachartre * N is the number of blocks in the slice/volume/file. 5303edcc0754Sachartre * 5304bae9e67eSachartre * We simulate a disk with N+M blocks, where M is the number of blocks 5305bae9e67eSachartre * simluated at the beginning and at the end of the disk (blocks 0-C 5306bae9e67eSachartre * and D-E). 5307edcc0754Sachartre * 5308bae9e67eSachartre * The first blocks (0 to C-1) are emulated and can not be changed. Blocks C 5309bae9e67eSachartre * to D defines slice 0 and are mapped to the backend. Finally we emulate 2 5310bae9e67eSachartre * alternate cylinders at the end of the disk (blocks D-E). In summary we have: 5311edcc0754Sachartre * 5312bae9e67eSachartre * - block 0 (L) returns a fake VTOC label 5313bae9e67eSachartre * - blocks 1 to C-1 (X) are unused and return 0 5314bae9e67eSachartre * - blocks C to D-1 are mapped to the exported slice or volume 5315bae9e67eSachartre * - blocks D and E (A) are blocks defining alternate cylinders (2 cylinders) 5316bae9e67eSachartre * 5317bae9e67eSachartre * Note: because we define a fake disk geometry, it is possible that the length 5318bae9e67eSachartre * of the backend is not a multiple of the size of cylinder, in that case the 5319bae9e67eSachartre * very end of the backend will not map to any block of the virtual disk. 5320edcc0754Sachartre */ 53210a55fbb7Slm66018 static int 532278fcd0a1Sachartre vd_setup_partition_vtoc(vd_t *vd) 532378fcd0a1Sachartre { 532478fcd0a1Sachartre char *device_path = vd->device_path; 5325bae9e67eSachartre char unit; 5326bae9e67eSachartre size_t size, csize; 532778fcd0a1Sachartre 532878fcd0a1Sachartre /* Initialize dk_geom structure for single-slice device */ 532978fcd0a1Sachartre if (vd->dk_geom.dkg_nsect == 0) { 533078fcd0a1Sachartre PRN("%s geometry claims 0 sectors per track", device_path); 533178fcd0a1Sachartre return (EIO); 533278fcd0a1Sachartre } 533378fcd0a1Sachartre if (vd->dk_geom.dkg_nhead == 0) { 533478fcd0a1Sachartre PRN("%s geometry claims 0 heads", device_path); 533578fcd0a1Sachartre return (EIO); 533678fcd0a1Sachartre } 5337bae9e67eSachartre 5338bae9e67eSachartre /* size of a cylinder in block */ 5339bae9e67eSachartre csize = vd->dk_geom.dkg_nhead * vd->dk_geom.dkg_nsect; 5340bae9e67eSachartre 5341bae9e67eSachartre /* 5342bae9e67eSachartre * Add extra cylinders: we emulate the first cylinder (which contains 5343bae9e67eSachartre * the disk label). 5344bae9e67eSachartre */ 5345bae9e67eSachartre vd->dk_geom.dkg_ncyl = vd->vdisk_size / csize + 1; 5346bae9e67eSachartre 5347bae9e67eSachartre /* we emulate 2 alternate cylinders */ 5348bae9e67eSachartre vd->dk_geom.dkg_acyl = 2; 534978fcd0a1Sachartre vd->dk_geom.dkg_pcyl = vd->dk_geom.dkg_ncyl + vd->dk_geom.dkg_acyl; 535078fcd0a1Sachartre 535178fcd0a1Sachartre 535278fcd0a1Sachartre /* Initialize vtoc structure for single-slice device */ 535378fcd0a1Sachartre bzero(vd->vtoc.v_part, sizeof (vd->vtoc.v_part)); 535478fcd0a1Sachartre vd->vtoc.v_part[0].p_tag = V_UNASSIGNED; 535578fcd0a1Sachartre vd->vtoc.v_part[0].p_flag = 0; 5356bae9e67eSachartre /* 5357bae9e67eSachartre * Partition 0 starts on cylinder 1 and its size has to be 5358bae9e67eSachartre * a multiple of a number of cylinder. 5359bae9e67eSachartre */ 5360bae9e67eSachartre vd->vtoc.v_part[0].p_start = csize; /* start on cylinder 1 */ 5361bae9e67eSachartre vd->vtoc.v_part[0].p_size = (vd->vdisk_size / csize) * csize; 536278fcd0a1Sachartre 5363bae9e67eSachartre if (vd_slice_single_slice) { 5364bae9e67eSachartre vd->vtoc.v_nparts = 1; 5365bae9e67eSachartre bcopy(VD_ASCIILABEL, vd->vtoc.v_asciilabel, 5366bae9e67eSachartre MIN(sizeof (VD_ASCIILABEL), 5367bae9e67eSachartre sizeof (vd->vtoc.v_asciilabel))); 5368bae9e67eSachartre bcopy(VD_VOLUME_NAME, vd->vtoc.v_volume, 5369bae9e67eSachartre MIN(sizeof (VD_VOLUME_NAME), sizeof (vd->vtoc.v_volume))); 5370bae9e67eSachartre } else { 5371bae9e67eSachartre /* adjust the number of slices */ 5372bae9e67eSachartre vd->nslices = V_NUMPAR; 5373bae9e67eSachartre vd->vtoc.v_nparts = V_NUMPAR; 5374bae9e67eSachartre 5375bae9e67eSachartre /* define slice 2 representing the entire disk */ 5376bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_tag = V_BACKUP; 5377bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_flag = 0; 5378bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_start = 0; 5379bae9e67eSachartre vd->vtoc.v_part[VD_ENTIRE_DISK_SLICE].p_size = 5380bae9e67eSachartre vd->dk_geom.dkg_ncyl * csize; 5381bae9e67eSachartre 5382bae9e67eSachartre vd_get_readable_size(vd->vdisk_size * vd->vdisk_block_size, 5383bae9e67eSachartre &size, &unit); 5384bae9e67eSachartre 5385bae9e67eSachartre /* 5386bae9e67eSachartre * Set some attributes of the geometry to what format(1m) uses 5387bae9e67eSachartre * so that writing a default label using format(1m) does not 5388bae9e67eSachartre * produce any error. 5389bae9e67eSachartre */ 5390bae9e67eSachartre vd->dk_geom.dkg_bcyl = 0; 5391bae9e67eSachartre vd->dk_geom.dkg_intrlv = 1; 5392bae9e67eSachartre vd->dk_geom.dkg_write_reinstruct = 0; 5393bae9e67eSachartre vd->dk_geom.dkg_read_reinstruct = 0; 5394bae9e67eSachartre 5395bae9e67eSachartre /* 5396bae9e67eSachartre * We must have a correct label name otherwise format(1m) will 5397bae9e67eSachartre * not recognized the disk as labeled. 5398bae9e67eSachartre */ 5399bae9e67eSachartre (void) snprintf(vd->vtoc.v_asciilabel, LEN_DKL_ASCII, 5400bae9e67eSachartre "SUN-DiskSlice-%ld%cB cyl %d alt %d hd %d sec %d", 5401bae9e67eSachartre size, unit, 5402bae9e67eSachartre vd->dk_geom.dkg_ncyl, vd->dk_geom.dkg_acyl, 5403bae9e67eSachartre vd->dk_geom.dkg_nhead, vd->dk_geom.dkg_nsect); 5404bae9e67eSachartre bzero(vd->vtoc.v_volume, sizeof (vd->vtoc.v_volume)); 5405bae9e67eSachartre 5406bae9e67eSachartre /* create a fake label from the vtoc and geometry */ 5407bae9e67eSachartre vd->flabel_limit = csize; 5408bae9e67eSachartre vd->flabel_size = VD_LABEL_VTOC_SIZE; 5409bae9e67eSachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5410bae9e67eSachartre vd_vtocgeom_to_label(&vd->vtoc, &vd->dk_geom, 5411bae9e67eSachartre VD_LABEL_VTOC(vd)); 5412bae9e67eSachartre } 5413bae9e67eSachartre 5414bae9e67eSachartre /* adjust the vdisk_size, we emulate 3 cylinders */ 5415bae9e67eSachartre vd->vdisk_size += csize * 3; 5416edcc0754Sachartre 541778fcd0a1Sachartre return (0); 541878fcd0a1Sachartre } 541978fcd0a1Sachartre 5420edcc0754Sachartre /* 5421edcc0754Sachartre * When a slice, volume or file is exported as a single-slice disk, we want 5422edcc0754Sachartre * the disk backend (i.e. the slice, volume or file) to be entirely mapped 5423edcc0754Sachartre * as a slice without the addition of any metadata. 5424edcc0754Sachartre * 5425edcc0754Sachartre * So when exporting the disk as an EFI disk, we fake a disk with the following 5426edcc0754Sachartre * layout: 5427edcc0754Sachartre * 5428bae9e67eSachartre * flabel +--- flabel_limit 5429bae9e67eSachartre * <------> v 5430bae9e67eSachartre * 0 1 2 L 34 34+N P 5431bae9e67eSachartre * +-+-+--+-------+--------------------------+-------+ 5432bae9e67eSachartre * virtual disk: |X|T|EE|XXXXXXX| slice 0 |RRRRRRR| 5433bae9e67eSachartre * +-+-+--+-------+--------------------------+-------+ 5434edcc0754Sachartre * ^ ^ : : 5435edcc0754Sachartre * | | : : 5436edcc0754Sachartre * GPT-+ +-GPE : : 5437edcc0754Sachartre * +--------------------------+ 5438edcc0754Sachartre * disk backend: | slice/volume/file | 5439edcc0754Sachartre * +--------------------------+ 5440edcc0754Sachartre * 0 N 5441edcc0754Sachartre * 5442edcc0754Sachartre * N is the number of blocks in the slice/volume/file. 5443edcc0754Sachartre * 5444bae9e67eSachartre * We simulate a disk with N+M blocks, where M is the number of blocks 5445bae9e67eSachartre * simluated at the beginning and at the end of the disk (blocks 0-34 5446bae9e67eSachartre * and 34+N-P). 5447edcc0754Sachartre * 5448bae9e67eSachartre * The first 34 blocks (0 to 33) are emulated and can not be changed. Blocks 34 5449bae9e67eSachartre * to 34+N defines slice 0 and are mapped to the exported backend, and we 5450bae9e67eSachartre * emulate some blocks at the end of the disk (blocks 34+N to P) as a the EFI 5451bae9e67eSachartre * reserved partition. 5452bae9e67eSachartre * 5453bae9e67eSachartre * - block 0 (X) is unused and return 0 5454edcc0754Sachartre * - block 1 (T) returns a fake EFI GPT (via DKIOCGETEFI) 5455bae9e67eSachartre * - blocks 2 to L-1 (E) defines a fake EFI GPE (via DKIOCGETEFI) 5456bae9e67eSachartre * - blocks L to 33 (X) are unused and return 0 5457bae9e67eSachartre * - blocks 34 to 34+N are mapped to the exported slice, volume or file 5458bae9e67eSachartre * - blocks 34+N+1 to P define a fake reserved partition and backup label, it 5459bae9e67eSachartre * returns 0 5460edcc0754Sachartre * 5461bae9e67eSachartre * Note: if the backend size is not a multiple of the vdisk block size 5462bae9e67eSachartre * (DEV_BSIZE = 512 byte) then the very end of the backend will not map to 5463bae9e67eSachartre * any block of the virtual disk. 5464edcc0754Sachartre */ 546578fcd0a1Sachartre static int 54664bac2208Snarayan vd_setup_partition_efi(vd_t *vd) 54674bac2208Snarayan { 54684bac2208Snarayan efi_gpt_t *gpt; 54694bac2208Snarayan efi_gpe_t *gpe; 5470edcc0754Sachartre struct uuid uuid = EFI_USR; 5471bae9e67eSachartre struct uuid efi_reserved = EFI_RESERVED; 54724bac2208Snarayan uint32_t crc; 5473bae9e67eSachartre uint64_t s0_start, s0_end; 54744bac2208Snarayan 5475bae9e67eSachartre vd->flabel_limit = 34; 5476bae9e67eSachartre vd->flabel_size = VD_LABEL_EFI_SIZE; 5477bae9e67eSachartre vd->flabel = kmem_zalloc(vd->flabel_size, KM_SLEEP); 5478bae9e67eSachartre gpt = VD_LABEL_EFI_GPT(vd); 5479bae9e67eSachartre gpe = VD_LABEL_EFI_GPE(vd); 5480edcc0754Sachartre 5481edcc0754Sachartre /* adjust the vdisk_size, we emulate the first 34 blocks */ 5482edcc0754Sachartre vd->vdisk_size += 34; 5483bae9e67eSachartre s0_start = 34; 5484bae9e67eSachartre s0_end = vd->vdisk_size - 1; 54854bac2208Snarayan 54864bac2208Snarayan gpt->efi_gpt_Signature = LE_64(EFI_SIGNATURE); 54874bac2208Snarayan gpt->efi_gpt_Revision = LE_32(EFI_VERSION_CURRENT); 54884bac2208Snarayan gpt->efi_gpt_HeaderSize = LE_32(sizeof (efi_gpt_t)); 5489edcc0754Sachartre gpt->efi_gpt_FirstUsableLBA = LE_64(34ULL); 5490edcc0754Sachartre gpt->efi_gpt_PartitionEntryLBA = LE_64(2ULL); 54914bac2208Snarayan gpt->efi_gpt_SizeOfPartitionEntry = LE_32(sizeof (efi_gpe_t)); 54924bac2208Snarayan 5493bae9e67eSachartre UUID_LE_CONVERT(gpe[0].efi_gpe_PartitionTypeGUID, uuid); 5494bae9e67eSachartre gpe[0].efi_gpe_StartingLBA = LE_64(s0_start); 5495bae9e67eSachartre gpe[0].efi_gpe_EndingLBA = LE_64(s0_end); 54964bac2208Snarayan 5497bae9e67eSachartre if (vd_slice_single_slice) { 5498bae9e67eSachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(1); 5499bae9e67eSachartre } else { 5500bae9e67eSachartre /* adjust the number of slices */ 5501bae9e67eSachartre gpt->efi_gpt_NumberOfPartitionEntries = LE_32(VD_MAXPART); 5502bae9e67eSachartre vd->nslices = V_NUMPAR; 5503bae9e67eSachartre 5504bae9e67eSachartre /* define a fake reserved partition */ 5505bae9e67eSachartre UUID_LE_CONVERT(gpe[VD_MAXPART - 1].efi_gpe_PartitionTypeGUID, 5506bae9e67eSachartre efi_reserved); 5507bae9e67eSachartre gpe[VD_MAXPART - 1].efi_gpe_StartingLBA = 5508bae9e67eSachartre LE_64(s0_end + 1); 5509bae9e67eSachartre gpe[VD_MAXPART - 1].efi_gpe_EndingLBA = 5510bae9e67eSachartre LE_64(s0_end + EFI_MIN_RESV_SIZE); 5511bae9e67eSachartre 5512bae9e67eSachartre /* adjust the vdisk_size to include the reserved slice */ 5513bae9e67eSachartre vd->vdisk_size += EFI_MIN_RESV_SIZE; 5514bae9e67eSachartre } 5515bae9e67eSachartre 5516bae9e67eSachartre gpt->efi_gpt_LastUsableLBA = LE_64(vd->vdisk_size - 1); 5517bae9e67eSachartre 5518bae9e67eSachartre /* adjust the vdisk size for the backup GPT and GPE */ 5519bae9e67eSachartre vd->vdisk_size += 33; 5520bae9e67eSachartre 5521bae9e67eSachartre CRC32(crc, gpe, sizeof (efi_gpe_t) * VD_MAXPART, -1U, crc32_table); 55224bac2208Snarayan gpt->efi_gpt_PartitionEntryArrayCRC32 = LE_32(~crc); 55234bac2208Snarayan 55244bac2208Snarayan CRC32(crc, gpt, sizeof (efi_gpt_t), -1U, crc32_table); 55254bac2208Snarayan gpt->efi_gpt_HeaderCRC32 = LE_32(~crc); 55264bac2208Snarayan 55274bac2208Snarayan return (0); 55284bac2208Snarayan } 55294bac2208Snarayan 5530047ba61eSachartre /* 5531047ba61eSachartre * Setup for a virtual disk whose backend is a file (exported as a single slice 55328fce2fd6Sachartre * or as a full disk) or a volume device (for example a ZFS, SVM or VxVM volume) 5533047ba61eSachartre * exported as a full disk. In these cases, the backend is accessed using the 5534047ba61eSachartre * vnode interface. 5535047ba61eSachartre */ 55364bac2208Snarayan static int 5537047ba61eSachartre vd_setup_backend_vnode(vd_t *vd) 55383c96341aSnarayan { 553978fcd0a1Sachartre int rval, status; 55403c96341aSnarayan vattr_t vattr; 55413c96341aSnarayan dev_t dev; 55423c96341aSnarayan char *file_path = vd->device_path; 55433c96341aSnarayan ldi_handle_t lhandle; 55443c96341aSnarayan struct dk_cinfo dk_cinfo; 5545bae9e67eSachartre struct dk_label label; 55463c96341aSnarayan 5547047ba61eSachartre if ((status = vn_open(file_path, UIO_SYSSPACE, vd->open_flags | FOFFMAX, 55483c96341aSnarayan 0, &vd->file_vnode, 0, 0)) != 0) { 5549690555a1Sachartre PRN("vn_open(%s) = errno %d", file_path, status); 55503c96341aSnarayan return (status); 55513c96341aSnarayan } 55523c96341aSnarayan 5553690555a1Sachartre /* 5554690555a1Sachartre * We set vd->file now so that vds_destroy_vd will take care of 5555690555a1Sachartre * closing the file and releasing the vnode in case of an error. 5556690555a1Sachartre */ 5557690555a1Sachartre vd->file = B_TRUE; 5558690555a1Sachartre 55593c96341aSnarayan vattr.va_mask = AT_SIZE; 5560da6c28aaSamw if ((status = VOP_GETATTR(vd->file_vnode, &vattr, 0, kcred, NULL)) 5561da6c28aaSamw != 0) { 5562690555a1Sachartre PRN("VOP_GETATTR(%s) = errno %d", file_path, status); 55633c96341aSnarayan return (EIO); 55643c96341aSnarayan } 55653c96341aSnarayan 55663c96341aSnarayan vd->file_size = vattr.va_size; 55673c96341aSnarayan /* size should be at least sizeof(dk_label) */ 55683c96341aSnarayan if (vd->file_size < sizeof (struct dk_label)) { 55693c96341aSnarayan PRN("Size of file has to be at least %ld bytes", 55703c96341aSnarayan sizeof (struct dk_label)); 55713c96341aSnarayan return (EIO); 55723c96341aSnarayan } 55733c96341aSnarayan 5574690555a1Sachartre if (vd->file_vnode->v_flag & VNOMAP) { 5575690555a1Sachartre PRN("File %s cannot be mapped", file_path); 55763c96341aSnarayan return (EIO); 55773c96341aSnarayan } 55783c96341aSnarayan 55793c96341aSnarayan /* sector size = block size = DEV_BSIZE */ 558017cadca8Slm66018 vd->block_size = DEV_BSIZE; 558117cadca8Slm66018 vd->vdisk_block_size = DEV_BSIZE; 558287a7269eSachartre vd->vdisk_size = vd->file_size / DEV_BSIZE; 55833c96341aSnarayan vd->max_xfer_sz = maxphys / DEV_BSIZE; /* default transfer size */ 55843c96341aSnarayan 5585047ba61eSachartre /* 5586047ba61eSachartre * Get max_xfer_sz from the device where the file is or from the device 55878fce2fd6Sachartre * itself if we have a volume device. 5588047ba61eSachartre */ 55898fce2fd6Sachartre if (vd->volume) { 5590047ba61eSachartre status = ldi_open_by_name(file_path, FREAD, kcred, &lhandle, 5591047ba61eSachartre vd->vds->ldi_ident); 5592047ba61eSachartre } else { 55933c96341aSnarayan dev = vd->file_vnode->v_vfsp->vfs_dev; 5594f745d6a3Sachartre PR0("underlying device of %s = (%d, %d)\n", file_path, 5595f745d6a3Sachartre getmajor(dev), getminor(dev)); 55963c96341aSnarayan 5597047ba61eSachartre status = ldi_open_by_dev(&dev, OTYP_BLK, FREAD, kcred, &lhandle, 5598047ba61eSachartre vd->vds->ldi_ident); 5599047ba61eSachartre } 5600047ba61eSachartre 5601047ba61eSachartre if (status != 0) { 5602f745d6a3Sachartre PR0("ldi_open() returned errno %d for underlying device", 5603f745d6a3Sachartre status); 56043c96341aSnarayan } else { 56053c96341aSnarayan if ((status = ldi_ioctl(lhandle, DKIOCINFO, 5606047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 56073c96341aSnarayan &rval)) != 0) { 5608f745d6a3Sachartre PR0("ldi_ioctl(DKIOCINFO) returned errno %d for " 5609f745d6a3Sachartre "underlying device", status); 56103c96341aSnarayan } else { 56113c96341aSnarayan /* 56123c96341aSnarayan * Store the device's max transfer size for 56133c96341aSnarayan * return to the client 56143c96341aSnarayan */ 56153c96341aSnarayan vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 56163c96341aSnarayan } 56173c96341aSnarayan 5618f745d6a3Sachartre PR0("close the underlying device"); 56193c96341aSnarayan (void) ldi_close(lhandle, FREAD, kcred); 56203c96341aSnarayan } 56213c96341aSnarayan 5622f745d6a3Sachartre if (vd->volume) { 5623f745d6a3Sachartre PR0("using volume %s, max_xfer = %u blks", file_path, 5624f745d6a3Sachartre vd->max_xfer_sz); 5625f745d6a3Sachartre } else { 5626f745d6a3Sachartre PR0("using file %s on device (%d, %d), max_xfer = %u blks", 5627f745d6a3Sachartre file_path, getmajor(dev), getminor(dev), vd->max_xfer_sz); 5628f745d6a3Sachartre } 56293c96341aSnarayan 5630edcc0754Sachartre if (vd->vdisk_type == VD_DISK_TYPE_SLICE) { 56318fce2fd6Sachartre ASSERT(!vd->volume); 5632bae9e67eSachartre vd->vdisk_media = VD_MEDIA_FIXED; 5633bae9e67eSachartre vd->vdisk_label = (vd_slice_label == VD_DISK_LABEL_UNK)? 5634bae9e67eSachartre vd_file_slice_label : vd_slice_label; 5635bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_EFI || 5636bae9e67eSachartre vd->file_size >= ONE_TERABYTE) { 5637edcc0754Sachartre status = vd_setup_partition_efi(vd); 5638bae9e67eSachartre } else { 5639bae9e67eSachartre /* 5640bae9e67eSachartre * We build a default label to get a geometry for 5641bae9e67eSachartre * the vdisk. Then the partition setup function will 5642bae9e67eSachartre * adjust the vtoc so that it defines a single-slice 5643bae9e67eSachartre * disk. 5644bae9e67eSachartre */ 5645bae9e67eSachartre vd_build_default_label(vd->file_size, &label); 5646bae9e67eSachartre vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 5647bae9e67eSachartre status = vd_setup_partition_vtoc(vd); 5648bae9e67eSachartre } 5649bae9e67eSachartre return (status); 5650edcc0754Sachartre } 5651edcc0754Sachartre 5652edcc0754Sachartre /* 5653edcc0754Sachartre * Find and validate the geometry of a disk image. 5654edcc0754Sachartre */ 5655edcc0754Sachartre status = vd_file_validate_geometry(vd); 5656edcc0754Sachartre if (status != 0 && status != EINVAL && status != ENOTSUP) { 5657edcc0754Sachartre PRN("Failed to read label from %s", file_path); 5658edcc0754Sachartre return (EIO); 5659edcc0754Sachartre } 5660edcc0754Sachartre 5661edcc0754Sachartre if (vd_file_is_iso_image(vd)) { 5662edcc0754Sachartre /* 5663edcc0754Sachartre * Indicate whether to call this a CD or DVD from the size 5664edcc0754Sachartre * of the ISO image (images for both drive types are stored 5665edcc0754Sachartre * in the ISO-9600 format). CDs can store up to just under 1Gb 5666edcc0754Sachartre */ 5667edcc0754Sachartre if ((vd->vdisk_size * vd->vdisk_block_size) > 5668edcc0754Sachartre (1024 * 1024 * 1024)) 5669edcc0754Sachartre vd->vdisk_media = VD_MEDIA_DVD; 5670edcc0754Sachartre else 5671edcc0754Sachartre vd->vdisk_media = VD_MEDIA_CD; 5672edcc0754Sachartre } else { 5673edcc0754Sachartre vd->vdisk_media = VD_MEDIA_FIXED; 5674edcc0754Sachartre } 5675edcc0754Sachartre 5676edcc0754Sachartre /* Setup devid for the disk image */ 5677047ba61eSachartre 567878fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 567978fcd0a1Sachartre 568087a7269eSachartre status = vd_file_read_devid(vd, &vd->file_devid); 568187a7269eSachartre 568287a7269eSachartre if (status == 0) { 568387a7269eSachartre /* a valid devid was found */ 568487a7269eSachartre return (0); 568587a7269eSachartre } 568687a7269eSachartre 568787a7269eSachartre if (status != EINVAL) { 568887a7269eSachartre /* 568978fcd0a1Sachartre * There was an error while trying to read the devid. 569078fcd0a1Sachartre * So this disk image may have a devid but we are 569178fcd0a1Sachartre * unable to read it. 569287a7269eSachartre */ 569387a7269eSachartre PR0("can not read devid for %s", file_path); 569487a7269eSachartre vd->file_devid = NULL; 569587a7269eSachartre return (0); 569687a7269eSachartre } 569778fcd0a1Sachartre } 569887a7269eSachartre 569987a7269eSachartre /* 570087a7269eSachartre * No valid device id was found so we create one. Note that a failure 570187a7269eSachartre * to create a device id is not fatal and does not prevent the disk 570287a7269eSachartre * image from being attached. 570387a7269eSachartre */ 570487a7269eSachartre PR1("creating devid for %s", file_path); 570587a7269eSachartre 570687a7269eSachartre if (ddi_devid_init(vd->vds->dip, DEVID_FAB, NULL, 0, 570787a7269eSachartre &vd->file_devid) != DDI_SUCCESS) { 570887a7269eSachartre PR0("fail to create devid for %s", file_path); 570987a7269eSachartre vd->file_devid = NULL; 571087a7269eSachartre return (0); 571187a7269eSachartre } 571287a7269eSachartre 571378fcd0a1Sachartre /* 571478fcd0a1Sachartre * Write devid to the disk image. The devid is stored into the disk 571578fcd0a1Sachartre * image if we have a valid label; otherwise the devid will be stored 571678fcd0a1Sachartre * when the user writes a valid label. 571778fcd0a1Sachartre */ 571878fcd0a1Sachartre if (vd->vdisk_label != VD_DISK_LABEL_UNK) { 571987a7269eSachartre if (vd_file_write_devid(vd, vd->file_devid) != 0) { 572087a7269eSachartre PR0("fail to write devid for %s", file_path); 572187a7269eSachartre ddi_devid_free(vd->file_devid); 572287a7269eSachartre vd->file_devid = NULL; 572387a7269eSachartre } 572478fcd0a1Sachartre } 572587a7269eSachartre 57263c96341aSnarayan return (0); 57273c96341aSnarayan } 57283c96341aSnarayan 572917cadca8Slm66018 573017cadca8Slm66018 /* 573117cadca8Slm66018 * Description: 573217cadca8Slm66018 * Open a device using its device path (supplied by ldm(1m)) 573317cadca8Slm66018 * 573417cadca8Slm66018 * Parameters: 573517cadca8Slm66018 * vd - pointer to structure containing the vDisk info 57368fce2fd6Sachartre * flags - open flags 573717cadca8Slm66018 * 573817cadca8Slm66018 * Return Value 573917cadca8Slm66018 * 0 - success 574017cadca8Slm66018 * != 0 - some other non-zero return value from ldi(9F) functions 574117cadca8Slm66018 */ 574217cadca8Slm66018 static int 57438fce2fd6Sachartre vd_open_using_ldi_by_name(vd_t *vd, int flags) 574417cadca8Slm66018 { 57458fce2fd6Sachartre int status; 574617cadca8Slm66018 char *device_path = vd->device_path; 574717cadca8Slm66018 57488fce2fd6Sachartre /* Attempt to open device */ 57498fce2fd6Sachartre status = ldi_open_by_name(device_path, flags, kcred, 575017cadca8Slm66018 &vd->ldi_handle[0], vd->vds->ldi_ident); 575117cadca8Slm66018 575217cadca8Slm66018 /* 575317cadca8Slm66018 * The open can fail for example if we are opening an empty slice. 575417cadca8Slm66018 * In case of a failure, we try the open again but this time with 575517cadca8Slm66018 * the FNDELAY flag. 575617cadca8Slm66018 */ 575717cadca8Slm66018 if (status != 0) 57588fce2fd6Sachartre status = ldi_open_by_name(device_path, flags | FNDELAY, 575917cadca8Slm66018 kcred, &vd->ldi_handle[0], vd->vds->ldi_ident); 576017cadca8Slm66018 576117cadca8Slm66018 if (status != 0) { 576217cadca8Slm66018 PR0("ldi_open_by_name(%s) = errno %d", device_path, status); 576317cadca8Slm66018 vd->ldi_handle[0] = NULL; 576417cadca8Slm66018 return (status); 576517cadca8Slm66018 } 576617cadca8Slm66018 576717cadca8Slm66018 return (0); 576817cadca8Slm66018 } 576917cadca8Slm66018 5770047ba61eSachartre /* 5771047ba61eSachartre * Setup for a virtual disk which backend is a device (a physical disk, 57728fce2fd6Sachartre * slice or volume device) that is directly exported either as a full disk 57738fce2fd6Sachartre * for a physical disk or as a slice for a volume device or a disk slice. 5774047ba61eSachartre * In these cases, the backend is accessed using the LDI interface. 5775047ba61eSachartre */ 57763c96341aSnarayan static int 5777047ba61eSachartre vd_setup_backend_ldi(vd_t *vd) 57781ae08745Sheppo { 5779e1ebb9ecSlm66018 int rval, status; 57801ae08745Sheppo struct dk_cinfo dk_cinfo; 57813c96341aSnarayan char *device_path = vd->device_path; 57821ae08745Sheppo 57838fce2fd6Sachartre /* device has been opened by vd_identify_dev() */ 57848fce2fd6Sachartre ASSERT(vd->ldi_handle[0] != NULL); 57858fce2fd6Sachartre ASSERT(vd->dev[0] != NULL); 57860a55fbb7Slm66018 57873c96341aSnarayan vd->file = B_FALSE; 57884bac2208Snarayan 578978fcd0a1Sachartre /* Verify backing device supports dk_cinfo */ 5790e1ebb9ecSlm66018 if ((status = ldi_ioctl(vd->ldi_handle[0], DKIOCINFO, 5791047ba61eSachartre (intptr_t)&dk_cinfo, (vd->open_flags | FKIOCTL), kcred, 5792e1ebb9ecSlm66018 &rval)) != 0) { 5793e1ebb9ecSlm66018 PRN("ldi_ioctl(DKIOCINFO) returned errno %d for %s", 5794e1ebb9ecSlm66018 status, device_path); 5795e1ebb9ecSlm66018 return (status); 5796e1ebb9ecSlm66018 } 5797e1ebb9ecSlm66018 if (dk_cinfo.dki_partition >= V_NUMPAR) { 5798e1ebb9ecSlm66018 PRN("slice %u >= maximum slice %u for %s", 5799e1ebb9ecSlm66018 dk_cinfo.dki_partition, V_NUMPAR, device_path); 5800e1ebb9ecSlm66018 return (EIO); 5801e1ebb9ecSlm66018 } 58024bac2208Snarayan 58038fce2fd6Sachartre /* 58048fce2fd6Sachartre * The device has been opened read-only by vd_identify_dev(), re-open 58058fce2fd6Sachartre * it read-write if the write flag is set and we don't have an optical 58068fce2fd6Sachartre * device such as a CD-ROM, which, for now, we do not permit writes to 58078fce2fd6Sachartre * and thus should not export write operations to the client. 58088fce2fd6Sachartre * 58098fce2fd6Sachartre * Future: if/when we implement support for guest domains writing to 58108fce2fd6Sachartre * optical devices we will need to do further checking of the media type 58118fce2fd6Sachartre * to distinguish between read-only and writable discs. 58128fce2fd6Sachartre */ 58138fce2fd6Sachartre if (dk_cinfo.dki_ctype == DKC_CDROM) { 58148fce2fd6Sachartre 58158fce2fd6Sachartre vd->open_flags &= ~FWRITE; 58168fce2fd6Sachartre 58178fce2fd6Sachartre } else if (vd->open_flags & FWRITE) { 58188fce2fd6Sachartre 58198fce2fd6Sachartre (void) ldi_close(vd->ldi_handle[0], vd->open_flags & ~FWRITE, 58208fce2fd6Sachartre kcred); 58218fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags); 58228fce2fd6Sachartre if (status != 0) { 58238fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", 58248fce2fd6Sachartre device_path, status); 58258fce2fd6Sachartre return (status); 58268fce2fd6Sachartre } 58278fce2fd6Sachartre } 58288fce2fd6Sachartre 5829e1ebb9ecSlm66018 /* Store the device's max transfer size for return to the client */ 5830e1ebb9ecSlm66018 vd->max_xfer_sz = dk_cinfo.dki_maxtransfer; 5831e1ebb9ecSlm66018 5832047ba61eSachartre /* 583317cadca8Slm66018 * We need to work out if it's an ATAPI (IDE CD-ROM) or SCSI device so 583417cadca8Slm66018 * that we can use the correct CDB group when sending USCSI commands. 583517cadca8Slm66018 */ 583617cadca8Slm66018 vd->is_atapi_dev = vd_is_atapi_device(vd); 583717cadca8Slm66018 583817cadca8Slm66018 /* 5839047ba61eSachartre * Export a full disk. 5840047ba61eSachartre * 5841047ba61eSachartre * When we use the LDI interface, we export a device as a full disk 5842047ba61eSachartre * if we have an entire disk slice (slice 2) and if this slice is 5843047ba61eSachartre * exported as a full disk and not as a single slice disk. 584417cadca8Slm66018 * Similarly, we want to use LDI if we are accessing a CD or DVD 584517cadca8Slm66018 * device (even if it isn't s2) 5846047ba61eSachartre * 58478fce2fd6Sachartre * Note that volume devices are exported as full disks using the vnode 5848047ba61eSachartre * interface, not the LDI interface. 5849047ba61eSachartre */ 585017cadca8Slm66018 if ((dk_cinfo.dki_partition == VD_ENTIRE_DISK_SLICE && 585117cadca8Slm66018 vd->vdisk_type == VD_DISK_TYPE_DISK) || 585217cadca8Slm66018 dk_cinfo.dki_ctype == DKC_CDROM) { 58538fce2fd6Sachartre ASSERT(!vd->volume); 58542f5224aeSachartre if (dk_cinfo.dki_ctype == DKC_SCSI_CCS) 58552f5224aeSachartre vd->scsi = B_TRUE; 5856047ba61eSachartre return (vd_setup_full_disk(vd)); 5857047ba61eSachartre } 5858047ba61eSachartre 5859047ba61eSachartre /* 5860047ba61eSachartre * Export a single slice disk. 5861047ba61eSachartre * 58628fce2fd6Sachartre * The exported device can be either a volume device or a disk slice. If 5863047ba61eSachartre * it is a disk slice different from slice 2 then it is always exported 5864047ba61eSachartre * as a single slice disk even if the "slice" option is not specified. 58658fce2fd6Sachartre * If it is disk slice 2 or a volume device then it is exported as a 5866047ba61eSachartre * single slice disk only if the "slice" option is specified. 5867047ba61eSachartre */ 5868047ba61eSachartre return (vd_setup_single_slice_disk(vd)); 5869047ba61eSachartre } 5870047ba61eSachartre 5871047ba61eSachartre static int 5872047ba61eSachartre vd_setup_single_slice_disk(vd_t *vd) 5873047ba61eSachartre { 5874edcc0754Sachartre int status, rval; 5875bae9e67eSachartre struct dk_label label; 5876047ba61eSachartre char *device_path = vd->device_path; 5877047ba61eSachartre 5878047ba61eSachartre /* Get size of backing device */ 5879047ba61eSachartre if (ldi_get_size(vd->ldi_handle[0], &vd->vdisk_size) != DDI_SUCCESS) { 5880047ba61eSachartre PRN("ldi_get_size() failed for %s", device_path); 58811ae08745Sheppo return (EIO); 58821ae08745Sheppo } 5883047ba61eSachartre vd->vdisk_size = lbtodb(vd->vdisk_size); /* convert to blocks */ 588417cadca8Slm66018 vd->block_size = DEV_BSIZE; 588517cadca8Slm66018 vd->vdisk_block_size = DEV_BSIZE; 588617cadca8Slm66018 vd->vdisk_media = VD_MEDIA_FIXED; 5887047ba61eSachartre 58888fce2fd6Sachartre if (vd->volume) { 5889047ba61eSachartre ASSERT(vd->vdisk_type == VD_DISK_TYPE_SLICE); 589078fcd0a1Sachartre } 58910a55fbb7Slm66018 5892047ba61eSachartre /* 5893047ba61eSachartre * We export the slice as a single slice disk even if the "slice" 5894047ba61eSachartre * option was not specified. 5895047ba61eSachartre */ 58961ae08745Sheppo vd->vdisk_type = VD_DISK_TYPE_SLICE; 58971ae08745Sheppo vd->nslices = 1; 58981ae08745Sheppo 5899edcc0754Sachartre /* 5900edcc0754Sachartre * When exporting a slice or a device as a single slice disk, we don't 5901edcc0754Sachartre * care about any partitioning exposed by the backend. The goal is just 5902edcc0754Sachartre * to export the backend as a flat storage. We provide a fake partition 5903edcc0754Sachartre * table (either a VTOC or EFI), which presents only one slice, to 5904bae9e67eSachartre * accommodate tools expecting a disk label. The selection of the label 5905bae9e67eSachartre * type (VTOC or EFI) depends on the value of the vd_slice_label 5906bae9e67eSachartre * variable. 5907edcc0754Sachartre */ 5908bae9e67eSachartre if (vd_slice_label == VD_DISK_LABEL_EFI || 5909bae9e67eSachartre vd->vdisk_size >= ONE_TERABYTE / DEV_BSIZE) { 5910bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 5911bae9e67eSachartre } else { 5912bae9e67eSachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGVTOC, 5913bae9e67eSachartre (intptr_t)&vd->vtoc, (vd->open_flags | FKIOCTL), 5914bae9e67eSachartre kcred, &rval); 5915edcc0754Sachartre 5916edcc0754Sachartre if (status == 0) { 5917bae9e67eSachartre status = ldi_ioctl(vd->ldi_handle[0], DKIOCGGEOM, 5918bae9e67eSachartre (intptr_t)&vd->dk_geom, (vd->open_flags | FKIOCTL), 5919bae9e67eSachartre kcred, &rval); 5920bae9e67eSachartre 5921bae9e67eSachartre if (status != 0) { 5922bae9e67eSachartre PRN("ldi_ioctl(DKIOCGEOM) returned errno %d " 5923bae9e67eSachartre "for %s", status, device_path); 5924bae9e67eSachartre return (status); 5925bae9e67eSachartre } 5926edcc0754Sachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 5927bae9e67eSachartre 5928bae9e67eSachartre } else if (vd_slice_label == VD_DISK_LABEL_VTOC) { 5929bae9e67eSachartre 5930bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_VTOC; 5931bae9e67eSachartre vd_build_default_label(vd->vdisk_size * DEV_BSIZE, 5932bae9e67eSachartre &label); 5933bae9e67eSachartre vd_label_to_vtocgeom(&label, &vd->vtoc, &vd->dk_geom); 5934bae9e67eSachartre 5935bae9e67eSachartre } else { 5936bae9e67eSachartre vd->vdisk_label = VD_DISK_LABEL_EFI; 5937bae9e67eSachartre } 5938bae9e67eSachartre } 5939bae9e67eSachartre 5940bae9e67eSachartre if (vd->vdisk_label == VD_DISK_LABEL_VTOC) { 5941bae9e67eSachartre /* export with a fake VTOC label */ 594278fcd0a1Sachartre status = vd_setup_partition_vtoc(vd); 5943bae9e67eSachartre 5944edcc0754Sachartre } else { 5945edcc0754Sachartre /* export with a fake EFI label */ 5946edcc0754Sachartre status = vd_setup_partition_efi(vd); 594778fcd0a1Sachartre } 594878fcd0a1Sachartre 59494bac2208Snarayan return (status); 59504bac2208Snarayan } 59511ae08745Sheppo 59528fce2fd6Sachartre /* 59538fce2fd6Sachartre * Description: 59548fce2fd6Sachartre * Open a device using its device path and identify if this is 59558fce2fd6Sachartre * a disk device or a volume device. 59568fce2fd6Sachartre * 59578fce2fd6Sachartre * Parameters: 59588fce2fd6Sachartre * vd - pointer to structure containing the vDisk info 59598fce2fd6Sachartre * dtype - return the driver type of the device 59608fce2fd6Sachartre * 59618fce2fd6Sachartre * Return Value 59628fce2fd6Sachartre * 0 - success 59638fce2fd6Sachartre * != 0 - some other non-zero return value from ldi(9F) functions 59648fce2fd6Sachartre */ 59658fce2fd6Sachartre static int 59668fce2fd6Sachartre vd_identify_dev(vd_t *vd, int *dtype) 59678fce2fd6Sachartre { 59688fce2fd6Sachartre int status, i; 59698fce2fd6Sachartre char *device_path = vd->device_path; 59708fce2fd6Sachartre char *drv_name; 59718fce2fd6Sachartre int drv_type; 59728fce2fd6Sachartre vds_t *vds = vd->vds; 59738fce2fd6Sachartre 59748fce2fd6Sachartre status = vd_open_using_ldi_by_name(vd, vd->open_flags & ~FWRITE); 59758fce2fd6Sachartre if (status != 0) { 59768fce2fd6Sachartre PR0("Failed to open (%s) = errno %d", device_path, status); 59778fce2fd6Sachartre return (status); 59788fce2fd6Sachartre } 59798fce2fd6Sachartre 59808fce2fd6Sachartre /* Get device number of backing device */ 59818fce2fd6Sachartre if ((status = ldi_get_dev(vd->ldi_handle[0], &vd->dev[0])) != 0) { 59828fce2fd6Sachartre PRN("ldi_get_dev() returned errno %d for %s", 59838fce2fd6Sachartre status, device_path); 59848fce2fd6Sachartre return (status); 59858fce2fd6Sachartre } 59868fce2fd6Sachartre 59878fce2fd6Sachartre /* 59888fce2fd6Sachartre * We start by looking if the driver is in the list from vds.conf 59898fce2fd6Sachartre * so that we can override the built-in list using vds.conf. 59908fce2fd6Sachartre */ 59918fce2fd6Sachartre drv_name = ddi_major_to_name(getmajor(vd->dev[0])); 59928fce2fd6Sachartre drv_type = VD_DRIVER_UNKNOWN; 59938fce2fd6Sachartre 59948fce2fd6Sachartre /* check vds.conf list */ 59958fce2fd6Sachartre for (i = 0; i < vds->num_drivers; i++) { 59968fce2fd6Sachartre if (vds->driver_types[i].type == VD_DRIVER_UNKNOWN) { 59978fce2fd6Sachartre /* ignore invalid entries */ 59988fce2fd6Sachartre continue; 59998fce2fd6Sachartre } 60008fce2fd6Sachartre if (strcmp(drv_name, vds->driver_types[i].name) == 0) { 60018fce2fd6Sachartre drv_type = vds->driver_types[i].type; 60028fce2fd6Sachartre goto done; 60038fce2fd6Sachartre } 60048fce2fd6Sachartre } 60058fce2fd6Sachartre 60068fce2fd6Sachartre /* check built-in list */ 60078fce2fd6Sachartre for (i = 0; i < VDS_NUM_DRIVERS; i++) { 60088fce2fd6Sachartre if (strcmp(drv_name, vds_driver_types[i].name) == 0) { 60098fce2fd6Sachartre drv_type = vds_driver_types[i].type; 60108fce2fd6Sachartre goto done; 60118fce2fd6Sachartre } 60128fce2fd6Sachartre } 60138fce2fd6Sachartre 60148fce2fd6Sachartre done: 60158fce2fd6Sachartre PR0("driver %s identified as %s", drv_name, 60168fce2fd6Sachartre (drv_type == VD_DRIVER_DISK)? "DISK" : 60178fce2fd6Sachartre (drv_type == VD_DRIVER_VOLUME)? "VOLUME" : "UNKNOWN"); 60188fce2fd6Sachartre 60198fce2fd6Sachartre *dtype = drv_type; 60208fce2fd6Sachartre 60218fce2fd6Sachartre return (0); 60228fce2fd6Sachartre } 60238fce2fd6Sachartre 60241ae08745Sheppo static int 6025047ba61eSachartre vd_setup_vd(vd_t *vd) 6026047ba61eSachartre { 60278fce2fd6Sachartre int status, drv_type, pseudo; 6028047ba61eSachartre dev_info_t *dip; 6029047ba61eSachartre vnode_t *vnp; 6030047ba61eSachartre char *path = vd->device_path; 6031047ba61eSachartre 6032047ba61eSachartre /* make sure the vdisk backend is valid */ 6033047ba61eSachartre if ((status = lookupname(path, UIO_SYSSPACE, 6034047ba61eSachartre FOLLOW, NULLVPP, &vnp)) != 0) { 6035047ba61eSachartre PR0("Cannot lookup %s errno %d", path, status); 6036047ba61eSachartre goto done; 6037047ba61eSachartre } 6038047ba61eSachartre 6039047ba61eSachartre switch (vnp->v_type) { 6040047ba61eSachartre case VREG: 6041047ba61eSachartre /* 6042047ba61eSachartre * Backend is a file so it is exported as a full disk or as a 6043047ba61eSachartre * single slice disk using the vnode interface. 6044047ba61eSachartre */ 6045047ba61eSachartre VN_RELE(vnp); 60468fce2fd6Sachartre vd->volume = B_FALSE; 6047047ba61eSachartre status = vd_setup_backend_vnode(vd); 6048047ba61eSachartre break; 6049047ba61eSachartre 6050047ba61eSachartre case VBLK: 6051047ba61eSachartre case VCHR: 6052047ba61eSachartre /* 6053047ba61eSachartre * Backend is a device. The way it is exported depends on the 6054047ba61eSachartre * type of the device. 6055047ba61eSachartre * 60568fce2fd6Sachartre * - A volume device is exported as a full disk using the vnode 6057047ba61eSachartre * interface or as a single slice disk using the LDI 6058047ba61eSachartre * interface. 6059047ba61eSachartre * 6060047ba61eSachartre * - A disk (represented by the slice 2 of that disk) is 6061047ba61eSachartre * exported as a full disk using the LDI interface. 6062047ba61eSachartre * 6063047ba61eSachartre * - A disk slice (different from slice 2) is always exported 6064047ba61eSachartre * as a single slice disk using the LDI interface. 6065047ba61eSachartre * 6066047ba61eSachartre * - The slice 2 of a disk is exported as a single slice disk 6067047ba61eSachartre * if the "slice" option is specified, otherwise the entire 6068047ba61eSachartre * disk will be exported. In any case, the LDI interface is 6069047ba61eSachartre * used. 6070047ba61eSachartre */ 6071047ba61eSachartre 6072047ba61eSachartre /* check if this is a pseudo device */ 6073047ba61eSachartre if ((dip = ddi_hold_devi_by_instance(getmajor(vnp->v_rdev), 6074047ba61eSachartre dev_to_instance(vnp->v_rdev), 0)) == NULL) { 6075047ba61eSachartre PRN("%s is no longer accessible", path); 6076047ba61eSachartre VN_RELE(vnp); 6077047ba61eSachartre status = EIO; 6078047ba61eSachartre break; 6079047ba61eSachartre } 60808fce2fd6Sachartre pseudo = is_pseudo_device(dip); 6081047ba61eSachartre ddi_release_devi(dip); 6082047ba61eSachartre VN_RELE(vnp); 6083047ba61eSachartre 60848fce2fd6Sachartre if (vd_identify_dev(vd, &drv_type) != 0) { 60858fce2fd6Sachartre PRN("%s identification failed", path); 60868fce2fd6Sachartre status = EIO; 60878fce2fd6Sachartre break; 60888fce2fd6Sachartre } 60898fce2fd6Sachartre 60908fce2fd6Sachartre /* 60918fce2fd6Sachartre * If the driver hasn't been identified then we consider that 60928fce2fd6Sachartre * pseudo devices are volumes and other devices are disks. 60938fce2fd6Sachartre */ 60948fce2fd6Sachartre if (drv_type == VD_DRIVER_VOLUME || 60958fce2fd6Sachartre (drv_type == VD_DRIVER_UNKNOWN && pseudo)) { 60968fce2fd6Sachartre vd->volume = B_TRUE; 60978fce2fd6Sachartre } else { 60982f5224aeSachartre status = vd_setup_backend_ldi(vd); 60992f5224aeSachartre break; 61002f5224aeSachartre } 61012f5224aeSachartre 6102047ba61eSachartre /* 61038fce2fd6Sachartre * If this is a volume device then its usage depends if the 6104047ba61eSachartre * "slice" option is set or not. If the "slice" option is set 61058fce2fd6Sachartre * then the volume device will be exported as a single slice, 6106047ba61eSachartre * otherwise it will be exported as a full disk. 61072f5224aeSachartre * 61082f5224aeSachartre * For backward compatibility, if vd_volume_force_slice is set 61098fce2fd6Sachartre * then we always export volume devices as slices. 6110047ba61eSachartre */ 61112f5224aeSachartre if (vd_volume_force_slice) { 61122f5224aeSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 61132f5224aeSachartre vd->nslices = 1; 61142f5224aeSachartre } 61152f5224aeSachartre 61168fce2fd6Sachartre if (vd->vdisk_type == VD_DISK_TYPE_DISK) { 61178fce2fd6Sachartre /* close device opened during identification */ 61188fce2fd6Sachartre (void) ldi_close(vd->ldi_handle[0], 61198fce2fd6Sachartre vd->open_flags & ~FWRITE, kcred); 61208fce2fd6Sachartre vd->ldi_handle[0] = NULL; 61218fce2fd6Sachartre vd->dev[0] = 0; 6122047ba61eSachartre status = vd_setup_backend_vnode(vd); 61238fce2fd6Sachartre } else { 6124047ba61eSachartre status = vd_setup_backend_ldi(vd); 61258fce2fd6Sachartre } 6126047ba61eSachartre break; 6127047ba61eSachartre 6128047ba61eSachartre default: 6129047ba61eSachartre PRN("Unsupported vdisk backend %s", path); 6130047ba61eSachartre VN_RELE(vnp); 6131047ba61eSachartre status = EBADF; 6132047ba61eSachartre } 6133047ba61eSachartre 6134047ba61eSachartre done: 6135047ba61eSachartre if (status != 0) { 6136047ba61eSachartre /* 6137047ba61eSachartre * If the error is retryable print an error message only 6138047ba61eSachartre * during the first try. 6139047ba61eSachartre */ 6140047ba61eSachartre if (status == ENXIO || status == ENODEV || 6141047ba61eSachartre status == ENOENT || status == EROFS) { 6142047ba61eSachartre if (!(vd->initialized & VD_SETUP_ERROR)) { 6143047ba61eSachartre PRN("%s is currently inaccessible (error %d)", 6144047ba61eSachartre path, status); 6145047ba61eSachartre } 6146047ba61eSachartre status = EAGAIN; 6147047ba61eSachartre } else { 6148047ba61eSachartre PRN("%s can not be exported as a virtual disk " 6149047ba61eSachartre "(error %d)", path, status); 6150047ba61eSachartre } 6151047ba61eSachartre vd->initialized |= VD_SETUP_ERROR; 6152047ba61eSachartre 6153047ba61eSachartre } else if (vd->initialized & VD_SETUP_ERROR) { 6154047ba61eSachartre /* print a message only if we previously had an error */ 6155047ba61eSachartre PRN("%s is now online", path); 6156047ba61eSachartre vd->initialized &= ~VD_SETUP_ERROR; 6157047ba61eSachartre } 6158047ba61eSachartre 6159047ba61eSachartre return (status); 6160047ba61eSachartre } 6161047ba61eSachartre 6162047ba61eSachartre static int 6163047ba61eSachartre vds_do_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6164047ba61eSachartre uint64_t ldc_id, vd_t **vdp) 61651ae08745Sheppo { 61661ae08745Sheppo char tq_name[TASKQ_NAMELEN]; 61670a55fbb7Slm66018 int status; 61681ae08745Sheppo ddi_iblock_cookie_t iblock = NULL; 61691ae08745Sheppo ldc_attr_t ldc_attr; 61701ae08745Sheppo vd_t *vd; 61711ae08745Sheppo 61721ae08745Sheppo 61731ae08745Sheppo ASSERT(vds != NULL); 6174e1ebb9ecSlm66018 ASSERT(device_path != NULL); 61751ae08745Sheppo ASSERT(vdp != NULL); 6176e1ebb9ecSlm66018 PR0("Adding vdisk for %s", device_path); 61771ae08745Sheppo 61781ae08745Sheppo if ((vd = kmem_zalloc(sizeof (*vd), KM_NOSLEEP)) == NULL) { 61791ae08745Sheppo PRN("No memory for virtual disk"); 61801ae08745Sheppo return (EAGAIN); 61811ae08745Sheppo } 61821ae08745Sheppo *vdp = vd; /* assign here so vds_destroy_vd() can cleanup later */ 61831ae08745Sheppo vd->vds = vds; 61843c96341aSnarayan (void) strncpy(vd->device_path, device_path, MAXPATHLEN); 61851ae08745Sheppo 6186047ba61eSachartre /* Setup open flags */ 6187047ba61eSachartre vd->open_flags = FREAD; 6188047ba61eSachartre 6189047ba61eSachartre if (!(options & VD_OPT_RDONLY)) 6190047ba61eSachartre vd->open_flags |= FWRITE; 6191047ba61eSachartre 6192047ba61eSachartre if (options & VD_OPT_EXCLUSIVE) 6193047ba61eSachartre vd->open_flags |= FEXCL; 6194047ba61eSachartre 6195047ba61eSachartre /* Setup disk type */ 6196047ba61eSachartre if (options & VD_OPT_SLICE) { 6197047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_SLICE; 6198047ba61eSachartre vd->nslices = 1; 6199047ba61eSachartre } else { 6200047ba61eSachartre vd->vdisk_type = VD_DISK_TYPE_DISK; 6201047ba61eSachartre vd->nslices = V_NUMPAR; 6202047ba61eSachartre } 6203047ba61eSachartre 6204047ba61eSachartre /* default disk label */ 6205047ba61eSachartre vd->vdisk_label = VD_DISK_LABEL_UNK; 6206047ba61eSachartre 62070a55fbb7Slm66018 /* Open vdisk and initialize parameters */ 62083c96341aSnarayan if ((status = vd_setup_vd(vd)) == 0) { 62093c96341aSnarayan vd->initialized |= VD_DISK_READY; 62101ae08745Sheppo 62113c96341aSnarayan ASSERT(vd->nslices > 0 && vd->nslices <= V_NUMPAR); 62128fce2fd6Sachartre PR0("vdisk_type = %s, volume = %s, file = %s, nslices = %u", 62133c96341aSnarayan ((vd->vdisk_type == VD_DISK_TYPE_DISK) ? "disk" : "slice"), 62148fce2fd6Sachartre (vd->volume ? "yes" : "no"), (vd->file ? "yes" : "no"), 62153c96341aSnarayan vd->nslices); 62163c96341aSnarayan } else { 62173c96341aSnarayan if (status != EAGAIN) 62183c96341aSnarayan return (status); 62193c96341aSnarayan } 62201ae08745Sheppo 62211ae08745Sheppo /* Initialize locking */ 62221ae08745Sheppo if (ddi_get_soft_iblock_cookie(vds->dip, DDI_SOFTINT_MED, 62231ae08745Sheppo &iblock) != DDI_SUCCESS) { 62241ae08745Sheppo PRN("Could not get iblock cookie."); 62251ae08745Sheppo return (EIO); 62261ae08745Sheppo } 62271ae08745Sheppo 62281ae08745Sheppo mutex_init(&vd->lock, NULL, MUTEX_DRIVER, iblock); 62291ae08745Sheppo vd->initialized |= VD_LOCKING; 62301ae08745Sheppo 62311ae08745Sheppo 6232d10e4ef2Snarayan /* Create start and completion task queues for the vdisk */ 6233d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_startq%lu", id); 62341ae08745Sheppo PR1("tq_name = %s", tq_name); 6235d10e4ef2Snarayan if ((vd->startq = ddi_taskq_create(vds->dip, tq_name, 1, 62361ae08745Sheppo TASKQ_DEFAULTPRI, 0)) == NULL) { 62371ae08745Sheppo PRN("Could not create task queue"); 62381ae08745Sheppo return (EIO); 62391ae08745Sheppo } 6240d10e4ef2Snarayan (void) snprintf(tq_name, sizeof (tq_name), "vd_completionq%lu", id); 6241d10e4ef2Snarayan PR1("tq_name = %s", tq_name); 6242d10e4ef2Snarayan if ((vd->completionq = ddi_taskq_create(vds->dip, tq_name, 1, 6243d10e4ef2Snarayan TASKQ_DEFAULTPRI, 0)) == NULL) { 6244d10e4ef2Snarayan PRN("Could not create task queue"); 6245d10e4ef2Snarayan return (EIO); 6246d10e4ef2Snarayan } 62475b98b509Sachartre 62485b98b509Sachartre /* Allocate the staging buffer */ 62495b98b509Sachartre vd->max_msglen = sizeof (vio_msg_t); /* baseline vio message size */ 62505b98b509Sachartre vd->vio_msgp = kmem_alloc(vd->max_msglen, KM_SLEEP); 62515b98b509Sachartre 6252d10e4ef2Snarayan vd->enabled = 1; /* before callback can dispatch to startq */ 62531ae08745Sheppo 62541ae08745Sheppo 62551ae08745Sheppo /* Bring up LDC */ 62561ae08745Sheppo ldc_attr.devclass = LDC_DEV_BLK_SVC; 62571ae08745Sheppo ldc_attr.instance = ddi_get_instance(vds->dip); 62581ae08745Sheppo ldc_attr.mode = LDC_MODE_UNRELIABLE; 6259e1ebb9ecSlm66018 ldc_attr.mtu = VD_LDC_MTU; 62601ae08745Sheppo if ((status = ldc_init(ldc_id, &ldc_attr, &vd->ldc_handle)) != 0) { 626117cadca8Slm66018 PRN("Could not initialize LDC channel %lx, " 6262690555a1Sachartre "init failed with error %d", ldc_id, status); 62631ae08745Sheppo return (status); 62641ae08745Sheppo } 62651ae08745Sheppo vd->initialized |= VD_LDC; 62661ae08745Sheppo 62671ae08745Sheppo if ((status = ldc_reg_callback(vd->ldc_handle, vd_handle_ldc_events, 62681ae08745Sheppo (caddr_t)vd)) != 0) { 6269690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6270690555a1Sachartre "reg_callback failed with error %d", ldc_id, status); 62711ae08745Sheppo return (status); 62721ae08745Sheppo } 62731ae08745Sheppo 62741ae08745Sheppo if ((status = ldc_open(vd->ldc_handle)) != 0) { 6275690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6276690555a1Sachartre "open failed with error %d", ldc_id, status); 62771ae08745Sheppo return (status); 62781ae08745Sheppo } 62791ae08745Sheppo 62803af08d82Slm66018 if ((status = ldc_up(vd->ldc_handle)) != 0) { 628134683adeSsg70180 PR0("ldc_up() returned errno %d", status); 62823af08d82Slm66018 } 62833af08d82Slm66018 62844bac2208Snarayan /* Allocate the inband task memory handle */ 62854bac2208Snarayan status = ldc_mem_alloc_handle(vd->ldc_handle, &(vd->inband_task.mhdl)); 62864bac2208Snarayan if (status) { 6287690555a1Sachartre PRN("Could not initialize LDC channel %lu," 6288690555a1Sachartre "alloc_handle failed with error %d", ldc_id, status); 62894bac2208Snarayan return (ENXIO); 62904bac2208Snarayan } 62911ae08745Sheppo 62921ae08745Sheppo /* Add the successfully-initialized vdisk to the server's table */ 62931ae08745Sheppo if (mod_hash_insert(vds->vd_table, (mod_hash_key_t)id, vd) != 0) { 62941ae08745Sheppo PRN("Error adding vdisk ID %lu to table", id); 62951ae08745Sheppo return (EIO); 62961ae08745Sheppo } 62971ae08745Sheppo 62983af08d82Slm66018 /* store initial state */ 62993af08d82Slm66018 vd->state = VD_STATE_INIT; 63003af08d82Slm66018 63011ae08745Sheppo return (0); 63021ae08745Sheppo } 63031ae08745Sheppo 63043af08d82Slm66018 static void 63053af08d82Slm66018 vd_free_dring_task(vd_t *vdp) 63063af08d82Slm66018 { 63073af08d82Slm66018 if (vdp->dring_task != NULL) { 63083af08d82Slm66018 ASSERT(vdp->dring_len != 0); 63093af08d82Slm66018 /* Free all dring_task memory handles */ 63103af08d82Slm66018 for (int i = 0; i < vdp->dring_len; i++) { 63113af08d82Slm66018 (void) ldc_mem_free_handle(vdp->dring_task[i].mhdl); 6312*5b7cb889Sha137994 kmem_free(vdp->dring_task[i].request, 6313*5b7cb889Sha137994 (vdp->descriptor_size - 6314*5b7cb889Sha137994 sizeof (vio_dring_entry_hdr_t))); 6315*5b7cb889Sha137994 vdp->dring_task[i].request = NULL; 63163af08d82Slm66018 kmem_free(vdp->dring_task[i].msg, vdp->max_msglen); 63173af08d82Slm66018 vdp->dring_task[i].msg = NULL; 63183af08d82Slm66018 } 63193af08d82Slm66018 kmem_free(vdp->dring_task, 63203af08d82Slm66018 (sizeof (*vdp->dring_task)) * vdp->dring_len); 63213af08d82Slm66018 vdp->dring_task = NULL; 63223af08d82Slm66018 } 63233af08d82Slm66018 } 63243af08d82Slm66018 63251ae08745Sheppo /* 63261ae08745Sheppo * Destroy the state associated with a virtual disk 63271ae08745Sheppo */ 63281ae08745Sheppo static void 63291ae08745Sheppo vds_destroy_vd(void *arg) 63301ae08745Sheppo { 63311ae08745Sheppo vd_t *vd = (vd_t *)arg; 633234683adeSsg70180 int retry = 0, rv; 63331ae08745Sheppo 63341ae08745Sheppo if (vd == NULL) 63351ae08745Sheppo return; 63361ae08745Sheppo 6337d10e4ef2Snarayan PR0("Destroying vdisk state"); 6338d10e4ef2Snarayan 63391ae08745Sheppo /* Disable queuing requests for the vdisk */ 63401ae08745Sheppo if (vd->initialized & VD_LOCKING) { 63411ae08745Sheppo mutex_enter(&vd->lock); 63421ae08745Sheppo vd->enabled = 0; 63431ae08745Sheppo mutex_exit(&vd->lock); 63441ae08745Sheppo } 63451ae08745Sheppo 6346d10e4ef2Snarayan /* Drain and destroy start queue (*before* destroying completionq) */ 6347d10e4ef2Snarayan if (vd->startq != NULL) 6348d10e4ef2Snarayan ddi_taskq_destroy(vd->startq); /* waits for queued tasks */ 6349d10e4ef2Snarayan 6350d10e4ef2Snarayan /* Drain and destroy completion queue (*before* shutting down LDC) */ 6351d10e4ef2Snarayan if (vd->completionq != NULL) 6352d10e4ef2Snarayan ddi_taskq_destroy(vd->completionq); /* waits for tasks */ 6353d10e4ef2Snarayan 63543af08d82Slm66018 vd_free_dring_task(vd); 63553af08d82Slm66018 635634683adeSsg70180 /* Free the inband task memory handle */ 635734683adeSsg70180 (void) ldc_mem_free_handle(vd->inband_task.mhdl); 635834683adeSsg70180 635934683adeSsg70180 /* Shut down LDC */ 636034683adeSsg70180 if (vd->initialized & VD_LDC) { 636134683adeSsg70180 /* unmap the dring */ 636234683adeSsg70180 if (vd->initialized & VD_DRING) 636334683adeSsg70180 (void) ldc_mem_dring_unmap(vd->dring_handle); 636434683adeSsg70180 636534683adeSsg70180 /* close LDC channel - retry on EAGAIN */ 636634683adeSsg70180 while ((rv = ldc_close(vd->ldc_handle)) == EAGAIN) { 636734683adeSsg70180 if (++retry > vds_ldc_retries) { 636834683adeSsg70180 PR0("Timed out closing channel"); 636934683adeSsg70180 break; 637034683adeSsg70180 } 637134683adeSsg70180 drv_usecwait(vds_ldc_delay); 637234683adeSsg70180 } 637334683adeSsg70180 if (rv == 0) { 637434683adeSsg70180 (void) ldc_unreg_callback(vd->ldc_handle); 637534683adeSsg70180 (void) ldc_fini(vd->ldc_handle); 637634683adeSsg70180 } else { 637734683adeSsg70180 /* 637834683adeSsg70180 * Closing the LDC channel has failed. Ideally we should 637934683adeSsg70180 * fail here but there is no Zeus level infrastructure 638034683adeSsg70180 * to handle this. The MD has already been changed and 638134683adeSsg70180 * we have to do the close. So we try to do as much 638234683adeSsg70180 * clean up as we can. 638334683adeSsg70180 */ 638434683adeSsg70180 (void) ldc_set_cb_mode(vd->ldc_handle, LDC_CB_DISABLE); 638534683adeSsg70180 while (ldc_unreg_callback(vd->ldc_handle) == EAGAIN) 638634683adeSsg70180 drv_usecwait(vds_ldc_delay); 638734683adeSsg70180 } 638834683adeSsg70180 } 638934683adeSsg70180 63903af08d82Slm66018 /* Free the staging buffer for msgs */ 63913af08d82Slm66018 if (vd->vio_msgp != NULL) { 63923af08d82Slm66018 kmem_free(vd->vio_msgp, vd->max_msglen); 63933af08d82Slm66018 vd->vio_msgp = NULL; 63943af08d82Slm66018 } 63953af08d82Slm66018 63963af08d82Slm66018 /* Free the inband message buffer */ 63973af08d82Slm66018 if (vd->inband_task.msg != NULL) { 63983af08d82Slm66018 kmem_free(vd->inband_task.msg, vd->max_msglen); 63993af08d82Slm66018 vd->inband_task.msg = NULL; 6400d10e4ef2Snarayan } 6401da6c28aaSamw 64023c96341aSnarayan if (vd->file) { 6403690555a1Sachartre /* Close file */ 6404047ba61eSachartre (void) VOP_CLOSE(vd->file_vnode, vd->open_flags, 1, 6405da6c28aaSamw 0, kcred, NULL); 64063c96341aSnarayan VN_RELE(vd->file_vnode); 640787a7269eSachartre if (vd->file_devid != NULL) 640887a7269eSachartre ddi_devid_free(vd->file_devid); 64093c96341aSnarayan } else { 64101ae08745Sheppo /* Close any open backing-device slices */ 6411bae9e67eSachartre for (uint_t slice = 0; slice < V_NUMPAR; slice++) { 64121ae08745Sheppo if (vd->ldi_handle[slice] != NULL) { 64131ae08745Sheppo PR0("Closing slice %u", slice); 64141ae08745Sheppo (void) ldi_close(vd->ldi_handle[slice], 6415047ba61eSachartre vd->open_flags, kcred); 64161ae08745Sheppo } 64171ae08745Sheppo } 64183c96341aSnarayan } 64191ae08745Sheppo 6420bae9e67eSachartre /* Free any fake label */ 6421bae9e67eSachartre if (vd->flabel) { 6422bae9e67eSachartre kmem_free(vd->flabel, vd->flabel_size); 6423bae9e67eSachartre vd->flabel = NULL; 6424bae9e67eSachartre vd->flabel_size = 0; 6425bae9e67eSachartre } 6426bae9e67eSachartre 64271ae08745Sheppo /* Free lock */ 64281ae08745Sheppo if (vd->initialized & VD_LOCKING) 64291ae08745Sheppo mutex_destroy(&vd->lock); 64301ae08745Sheppo 64311ae08745Sheppo /* Finally, free the vdisk structure itself */ 64321ae08745Sheppo kmem_free(vd, sizeof (*vd)); 64331ae08745Sheppo } 64341ae08745Sheppo 64351ae08745Sheppo static int 6436047ba61eSachartre vds_init_vd(vds_t *vds, uint64_t id, char *device_path, uint64_t options, 6437047ba61eSachartre uint64_t ldc_id) 64381ae08745Sheppo { 64391ae08745Sheppo int status; 64401ae08745Sheppo vd_t *vd = NULL; 64411ae08745Sheppo 64421ae08745Sheppo 6443047ba61eSachartre if ((status = vds_do_init_vd(vds, id, device_path, options, 6444047ba61eSachartre ldc_id, &vd)) != 0) 64451ae08745Sheppo vds_destroy_vd(vd); 64461ae08745Sheppo 64471ae08745Sheppo return (status); 64481ae08745Sheppo } 64491ae08745Sheppo 64501ae08745Sheppo static int 64511ae08745Sheppo vds_do_get_ldc_id(md_t *md, mde_cookie_t vd_node, mde_cookie_t *channel, 64521ae08745Sheppo uint64_t *ldc_id) 64531ae08745Sheppo { 64541ae08745Sheppo int num_channels; 64551ae08745Sheppo 64561ae08745Sheppo 64571ae08745Sheppo /* Look for channel endpoint child(ren) of the vdisk MD node */ 64581ae08745Sheppo if ((num_channels = md_scan_dag(md, vd_node, 64591ae08745Sheppo md_find_name(md, VD_CHANNEL_ENDPOINT), 64601ae08745Sheppo md_find_name(md, "fwd"), channel)) <= 0) { 64611ae08745Sheppo PRN("No \"%s\" found for virtual disk", VD_CHANNEL_ENDPOINT); 64621ae08745Sheppo return (-1); 64631ae08745Sheppo } 64641ae08745Sheppo 64651ae08745Sheppo /* Get the "id" value for the first channel endpoint node */ 64661ae08745Sheppo if (md_get_prop_val(md, channel[0], VD_ID_PROP, ldc_id) != 0) { 64671ae08745Sheppo PRN("No \"%s\" property found for \"%s\" of vdisk", 64681ae08745Sheppo VD_ID_PROP, VD_CHANNEL_ENDPOINT); 64691ae08745Sheppo return (-1); 64701ae08745Sheppo } 64711ae08745Sheppo 64721ae08745Sheppo if (num_channels > 1) { 64731ae08745Sheppo PRN("Using ID of first of multiple channels for this vdisk"); 64741ae08745Sheppo } 64751ae08745Sheppo 64761ae08745Sheppo return (0); 64771ae08745Sheppo } 64781ae08745Sheppo 64791ae08745Sheppo static int 64801ae08745Sheppo vds_get_ldc_id(md_t *md, mde_cookie_t vd_node, uint64_t *ldc_id) 64811ae08745Sheppo { 64821ae08745Sheppo int num_nodes, status; 64831ae08745Sheppo size_t size; 64841ae08745Sheppo mde_cookie_t *channel; 64851ae08745Sheppo 64861ae08745Sheppo 64871ae08745Sheppo if ((num_nodes = md_node_count(md)) <= 0) { 64881ae08745Sheppo PRN("Invalid node count in Machine Description subtree"); 64891ae08745Sheppo return (-1); 64901ae08745Sheppo } 64911ae08745Sheppo size = num_nodes*(sizeof (*channel)); 64921ae08745Sheppo channel = kmem_zalloc(size, KM_SLEEP); 64931ae08745Sheppo status = vds_do_get_ldc_id(md, vd_node, channel, ldc_id); 64941ae08745Sheppo kmem_free(channel, size); 64951ae08745Sheppo 64961ae08745Sheppo return (status); 64971ae08745Sheppo } 64981ae08745Sheppo 6499047ba61eSachartre /* 6500047ba61eSachartre * Function: 6501047ba61eSachartre * vds_get_options 6502047ba61eSachartre * 6503047ba61eSachartre * Description: 6504047ba61eSachartre * Parse the options of a vds node. Options are defined as an array 6505047ba61eSachartre * of strings in the vds-block-device-opts property of the vds node 6506047ba61eSachartre * in the machine description. Options are returned as a bitmask. The 6507047ba61eSachartre * mapping between the bitmask options and the options strings from the 6508047ba61eSachartre * machine description is defined in the vd_bdev_options[] array. 6509047ba61eSachartre * 6510047ba61eSachartre * The vds-block-device-opts property is optional. If a vds has no such 6511047ba61eSachartre * property then no option is defined. 6512047ba61eSachartre * 6513047ba61eSachartre * Parameters: 6514047ba61eSachartre * md - machine description. 6515047ba61eSachartre * vd_node - vds node in the machine description for which 6516047ba61eSachartre * options have to be parsed. 6517047ba61eSachartre * options - the returned options. 6518047ba61eSachartre * 6519047ba61eSachartre * Return Code: 6520047ba61eSachartre * none. 6521047ba61eSachartre */ 6522047ba61eSachartre static void 6523047ba61eSachartre vds_get_options(md_t *md, mde_cookie_t vd_node, uint64_t *options) 6524047ba61eSachartre { 6525047ba61eSachartre char *optstr, *opt; 6526047ba61eSachartre int len, n, i; 6527047ba61eSachartre 6528047ba61eSachartre *options = 0; 6529047ba61eSachartre 6530047ba61eSachartre if (md_get_prop_data(md, vd_node, VD_BLOCK_DEVICE_OPTS, 6531047ba61eSachartre (uint8_t **)&optstr, &len) != 0) { 6532047ba61eSachartre PR0("No options found"); 6533047ba61eSachartre return; 6534047ba61eSachartre } 6535047ba61eSachartre 6536047ba61eSachartre /* parse options */ 6537047ba61eSachartre opt = optstr; 6538047ba61eSachartre n = sizeof (vd_bdev_options) / sizeof (vd_option_t); 6539047ba61eSachartre 6540047ba61eSachartre while (opt < optstr + len) { 6541047ba61eSachartre for (i = 0; i < n; i++) { 6542047ba61eSachartre if (strncmp(vd_bdev_options[i].vdo_name, 6543047ba61eSachartre opt, VD_OPTION_NLEN) == 0) { 6544047ba61eSachartre *options |= vd_bdev_options[i].vdo_value; 6545047ba61eSachartre break; 6546047ba61eSachartre } 6547047ba61eSachartre } 6548047ba61eSachartre 6549047ba61eSachartre if (i < n) { 6550047ba61eSachartre PR0("option: %s", opt); 6551047ba61eSachartre } else { 6552047ba61eSachartre PRN("option %s is unknown or unsupported", opt); 6553047ba61eSachartre } 6554047ba61eSachartre 6555047ba61eSachartre opt += strlen(opt) + 1; 6556047ba61eSachartre } 6557047ba61eSachartre } 6558047ba61eSachartre 65591ae08745Sheppo static void 65608fce2fd6Sachartre vds_driver_types_free(vds_t *vds) 65618fce2fd6Sachartre { 65628fce2fd6Sachartre if (vds->driver_types != NULL) { 65638fce2fd6Sachartre kmem_free(vds->driver_types, sizeof (vd_driver_type_t) * 65648fce2fd6Sachartre vds->num_drivers); 65658fce2fd6Sachartre vds->driver_types = NULL; 65668fce2fd6Sachartre vds->num_drivers = 0; 65678fce2fd6Sachartre } 65688fce2fd6Sachartre } 65698fce2fd6Sachartre 65708fce2fd6Sachartre /* 65718fce2fd6Sachartre * Update the driver type list with information from vds.conf. 65728fce2fd6Sachartre */ 65738fce2fd6Sachartre static void 65748fce2fd6Sachartre vds_driver_types_update(vds_t *vds) 65758fce2fd6Sachartre { 65768fce2fd6Sachartre char **list, *s; 65778fce2fd6Sachartre uint_t i, num, count = 0, len; 65788fce2fd6Sachartre 65798fce2fd6Sachartre if (ddi_prop_lookup_string_array(DDI_DEV_T_ANY, vds->dip, 65808fce2fd6Sachartre DDI_PROP_DONTPASS, "driver-type-list", &list, &num) != 65818fce2fd6Sachartre DDI_PROP_SUCCESS) 65828fce2fd6Sachartre return; 65838fce2fd6Sachartre 65848fce2fd6Sachartre /* 65858fce2fd6Sachartre * We create a driver_types list with as many as entries as there 65868fce2fd6Sachartre * is in the driver-type-list from vds.conf. However only valid 65878fce2fd6Sachartre * entries will be populated (i.e. entries from driver-type-list 65888fce2fd6Sachartre * with a valid syntax). Invalid entries will be left blank so 65898fce2fd6Sachartre * they will have no driver name and the driver type will be 65908fce2fd6Sachartre * VD_DRIVER_UNKNOWN (= 0). 65918fce2fd6Sachartre */ 65928fce2fd6Sachartre vds->num_drivers = num; 65938fce2fd6Sachartre vds->driver_types = kmem_zalloc(sizeof (vd_driver_type_t) * num, 65948fce2fd6Sachartre KM_SLEEP); 65958fce2fd6Sachartre 65968fce2fd6Sachartre for (i = 0; i < num; i++) { 65978fce2fd6Sachartre 65988fce2fd6Sachartre s = strchr(list[i], ':'); 65998fce2fd6Sachartre 66008fce2fd6Sachartre if (s == NULL) { 66018fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 66028fce2fd6Sachartre "a colon is expected in the entry", 66038fce2fd6Sachartre i, list[i]); 66048fce2fd6Sachartre continue; 66058fce2fd6Sachartre } 66068fce2fd6Sachartre 66078fce2fd6Sachartre len = (uintptr_t)s - (uintptr_t)list[i]; 66088fce2fd6Sachartre 66098fce2fd6Sachartre if (len == 0) { 66108fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 66118fce2fd6Sachartre "the driver name is empty", 66128fce2fd6Sachartre i, list[i]); 66138fce2fd6Sachartre continue; 66148fce2fd6Sachartre } 66158fce2fd6Sachartre 66168fce2fd6Sachartre if (len >= VD_DRIVER_NAME_LEN) { 66178fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 66188fce2fd6Sachartre "the driver name is too long", 66198fce2fd6Sachartre i, list[i]); 66208fce2fd6Sachartre continue; 66218fce2fd6Sachartre } 66228fce2fd6Sachartre 66238fce2fd6Sachartre if (strcmp(s + 1, "disk") == 0) { 66248fce2fd6Sachartre 66258fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_DISK; 66268fce2fd6Sachartre 66278fce2fd6Sachartre } else if (strcmp(s + 1, "volume") == 0) { 66288fce2fd6Sachartre 66298fce2fd6Sachartre vds->driver_types[i].type = VD_DRIVER_VOLUME; 66308fce2fd6Sachartre 66318fce2fd6Sachartre } else { 66328fce2fd6Sachartre PRN("vds.conf: driver-type-list, entry %d (%s): " 66338fce2fd6Sachartre "the driver type is invalid", 66348fce2fd6Sachartre i, list[i]); 66358fce2fd6Sachartre continue; 66368fce2fd6Sachartre } 66378fce2fd6Sachartre 66388fce2fd6Sachartre (void) strncpy(vds->driver_types[i].name, list[i], len); 66398fce2fd6Sachartre 66408fce2fd6Sachartre PR0("driver-type-list, entry %d (%s) added", 66418fce2fd6Sachartre i, list[i]); 66428fce2fd6Sachartre 66438fce2fd6Sachartre count++; 66448fce2fd6Sachartre } 66458fce2fd6Sachartre 66468fce2fd6Sachartre ddi_prop_free(list); 66478fce2fd6Sachartre 66488fce2fd6Sachartre if (count == 0) { 66498fce2fd6Sachartre /* nothing was added, clean up */ 66508fce2fd6Sachartre vds_driver_types_free(vds); 66518fce2fd6Sachartre } 66528fce2fd6Sachartre } 66538fce2fd6Sachartre 66548fce2fd6Sachartre static void 66551ae08745Sheppo vds_add_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 66561ae08745Sheppo { 6657e1ebb9ecSlm66018 char *device_path = NULL; 6658047ba61eSachartre uint64_t id = 0, ldc_id = 0, options = 0; 66591ae08745Sheppo 66601ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 66611ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_ID_PROP); 66621ae08745Sheppo return; 66631ae08745Sheppo } 66641ae08745Sheppo PR0("Adding vdisk ID %lu", id); 66651ae08745Sheppo if (md_get_prop_str(md, vd_node, VD_BLOCK_DEVICE_PROP, 6666e1ebb9ecSlm66018 &device_path) != 0) { 66671ae08745Sheppo PRN("Error getting vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 66681ae08745Sheppo return; 66691ae08745Sheppo } 66701ae08745Sheppo 6671047ba61eSachartre vds_get_options(md, vd_node, &options); 6672047ba61eSachartre 66731ae08745Sheppo if (vds_get_ldc_id(md, vd_node, &ldc_id) != 0) { 66741ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", id); 66751ae08745Sheppo return; 66761ae08745Sheppo } 66771ae08745Sheppo 6678047ba61eSachartre if (vds_init_vd(vds, id, device_path, options, ldc_id) != 0) { 66791ae08745Sheppo PRN("Failed to add vdisk ID %lu", id); 668017cadca8Slm66018 if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 668117cadca8Slm66018 PRN("No vDisk entry found for vdisk ID %lu", id); 66821ae08745Sheppo return; 66831ae08745Sheppo } 66841ae08745Sheppo } 66851ae08745Sheppo 66861ae08745Sheppo static void 66871ae08745Sheppo vds_remove_vd(vds_t *vds, md_t *md, mde_cookie_t vd_node) 66881ae08745Sheppo { 66891ae08745Sheppo uint64_t id = 0; 66901ae08745Sheppo 66911ae08745Sheppo 66921ae08745Sheppo if (md_get_prop_val(md, vd_node, VD_ID_PROP, &id) != 0) { 66931ae08745Sheppo PRN("Unable to get \"%s\" property from vdisk's MD node", 66941ae08745Sheppo VD_ID_PROP); 66951ae08745Sheppo return; 66961ae08745Sheppo } 66971ae08745Sheppo PR0("Removing vdisk ID %lu", id); 66981ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)id) != 0) 66991ae08745Sheppo PRN("No vdisk entry found for vdisk ID %lu", id); 67001ae08745Sheppo } 67011ae08745Sheppo 67021ae08745Sheppo static void 67031ae08745Sheppo vds_change_vd(vds_t *vds, md_t *prev_md, mde_cookie_t prev_vd_node, 67041ae08745Sheppo md_t *curr_md, mde_cookie_t curr_vd_node) 67051ae08745Sheppo { 67061ae08745Sheppo char *curr_dev, *prev_dev; 6707047ba61eSachartre uint64_t curr_id = 0, curr_ldc_id = 0, curr_options = 0; 6708047ba61eSachartre uint64_t prev_id = 0, prev_ldc_id = 0, prev_options = 0; 67091ae08745Sheppo size_t len; 67101ae08745Sheppo 67111ae08745Sheppo 67121ae08745Sheppo /* Validate that vdisk ID has not changed */ 67131ae08745Sheppo if (md_get_prop_val(prev_md, prev_vd_node, VD_ID_PROP, &prev_id) != 0) { 67141ae08745Sheppo PRN("Error getting previous vdisk \"%s\" property", 67151ae08745Sheppo VD_ID_PROP); 67161ae08745Sheppo return; 67171ae08745Sheppo } 67181ae08745Sheppo if (md_get_prop_val(curr_md, curr_vd_node, VD_ID_PROP, &curr_id) != 0) { 67191ae08745Sheppo PRN("Error getting current vdisk \"%s\" property", VD_ID_PROP); 67201ae08745Sheppo return; 67211ae08745Sheppo } 67221ae08745Sheppo if (curr_id != prev_id) { 67231ae08745Sheppo PRN("Not changing vdisk: ID changed from %lu to %lu", 67241ae08745Sheppo prev_id, curr_id); 67251ae08745Sheppo return; 67261ae08745Sheppo } 67271ae08745Sheppo 67281ae08745Sheppo /* Validate that LDC ID has not changed */ 67291ae08745Sheppo if (vds_get_ldc_id(prev_md, prev_vd_node, &prev_ldc_id) != 0) { 67301ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", prev_id); 67311ae08745Sheppo return; 67321ae08745Sheppo } 67331ae08745Sheppo 67341ae08745Sheppo if (vds_get_ldc_id(curr_md, curr_vd_node, &curr_ldc_id) != 0) { 67351ae08745Sheppo PRN("Error getting LDC ID for vdisk %lu", curr_id); 67361ae08745Sheppo return; 67371ae08745Sheppo } 67381ae08745Sheppo if (curr_ldc_id != prev_ldc_id) { 67390a55fbb7Slm66018 _NOTE(NOTREACHED); /* lint is confused */ 67401ae08745Sheppo PRN("Not changing vdisk: " 67411ae08745Sheppo "LDC ID changed from %lu to %lu", prev_ldc_id, curr_ldc_id); 67421ae08745Sheppo return; 67431ae08745Sheppo } 67441ae08745Sheppo 67451ae08745Sheppo /* Determine whether device path has changed */ 67461ae08745Sheppo if (md_get_prop_str(prev_md, prev_vd_node, VD_BLOCK_DEVICE_PROP, 67471ae08745Sheppo &prev_dev) != 0) { 67481ae08745Sheppo PRN("Error getting previous vdisk \"%s\"", 67491ae08745Sheppo VD_BLOCK_DEVICE_PROP); 67501ae08745Sheppo return; 67511ae08745Sheppo } 67521ae08745Sheppo if (md_get_prop_str(curr_md, curr_vd_node, VD_BLOCK_DEVICE_PROP, 67531ae08745Sheppo &curr_dev) != 0) { 67541ae08745Sheppo PRN("Error getting current vdisk \"%s\"", VD_BLOCK_DEVICE_PROP); 67551ae08745Sheppo return; 67561ae08745Sheppo } 67571ae08745Sheppo if (((len = strlen(curr_dev)) == strlen(prev_dev)) && 67581ae08745Sheppo (strncmp(curr_dev, prev_dev, len) == 0)) 67591ae08745Sheppo return; /* no relevant (supported) change */ 67601ae08745Sheppo 6761047ba61eSachartre /* Validate that options have not changed */ 6762047ba61eSachartre vds_get_options(prev_md, prev_vd_node, &prev_options); 6763047ba61eSachartre vds_get_options(curr_md, curr_vd_node, &curr_options); 6764047ba61eSachartre if (prev_options != curr_options) { 6765047ba61eSachartre PRN("Not changing vdisk: options changed from %lx to %lx", 6766047ba61eSachartre prev_options, curr_options); 6767047ba61eSachartre return; 6768047ba61eSachartre } 6769047ba61eSachartre 67701ae08745Sheppo PR0("Changing vdisk ID %lu", prev_id); 67713af08d82Slm66018 67721ae08745Sheppo /* Remove old state, which will close vdisk and reset */ 67731ae08745Sheppo if (mod_hash_destroy(vds->vd_table, (mod_hash_key_t)prev_id) != 0) 67741ae08745Sheppo PRN("No entry found for vdisk ID %lu", prev_id); 67753af08d82Slm66018 67761ae08745Sheppo /* Re-initialize vdisk with new state */ 6777047ba61eSachartre if (vds_init_vd(vds, curr_id, curr_dev, curr_options, 6778047ba61eSachartre curr_ldc_id) != 0) { 67791ae08745Sheppo PRN("Failed to change vdisk ID %lu", curr_id); 67801ae08745Sheppo return; 67811ae08745Sheppo } 67821ae08745Sheppo } 67831ae08745Sheppo 67841ae08745Sheppo static int 67851ae08745Sheppo vds_process_md(void *arg, mdeg_result_t *md) 67861ae08745Sheppo { 67871ae08745Sheppo int i; 67881ae08745Sheppo vds_t *vds = arg; 67891ae08745Sheppo 67901ae08745Sheppo 67911ae08745Sheppo if (md == NULL) 67921ae08745Sheppo return (MDEG_FAILURE); 67931ae08745Sheppo ASSERT(vds != NULL); 67941ae08745Sheppo 67951ae08745Sheppo for (i = 0; i < md->removed.nelem; i++) 67961ae08745Sheppo vds_remove_vd(vds, md->removed.mdp, md->removed.mdep[i]); 67971ae08745Sheppo for (i = 0; i < md->match_curr.nelem; i++) 67981ae08745Sheppo vds_change_vd(vds, md->match_prev.mdp, md->match_prev.mdep[i], 67991ae08745Sheppo md->match_curr.mdp, md->match_curr.mdep[i]); 68001ae08745Sheppo for (i = 0; i < md->added.nelem; i++) 68011ae08745Sheppo vds_add_vd(vds, md->added.mdp, md->added.mdep[i]); 68021ae08745Sheppo 68031ae08745Sheppo return (MDEG_SUCCESS); 68041ae08745Sheppo } 68051ae08745Sheppo 68063c96341aSnarayan 68071ae08745Sheppo static int 68081ae08745Sheppo vds_do_attach(dev_info_t *dip) 68091ae08745Sheppo { 6810445b4c2eSsb155480 int status, sz; 6811445b4c2eSsb155480 int cfg_handle; 68121ae08745Sheppo minor_t instance = ddi_get_instance(dip); 68131ae08745Sheppo vds_t *vds; 6814445b4c2eSsb155480 mdeg_prop_spec_t *pspecp; 6815445b4c2eSsb155480 mdeg_node_spec_t *ispecp; 68161ae08745Sheppo 68171ae08745Sheppo /* 68181ae08745Sheppo * The "cfg-handle" property of a vds node in an MD contains the MD's 68191ae08745Sheppo * notion of "instance", or unique identifier, for that node; OBP 68201ae08745Sheppo * stores the value of the "cfg-handle" MD property as the value of 68211ae08745Sheppo * the "reg" property on the node in the device tree it builds from 68221ae08745Sheppo * the MD and passes to Solaris. Thus, we look up the devinfo node's 68231ae08745Sheppo * "reg" property value to uniquely identify this device instance when 68241ae08745Sheppo * registering with the MD event-generation framework. If the "reg" 68251ae08745Sheppo * property cannot be found, the device tree state is presumably so 68261ae08745Sheppo * broken that there is no point in continuing. 68271ae08745Sheppo */ 6828445b4c2eSsb155480 if (!ddi_prop_exists(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6829445b4c2eSsb155480 VD_REG_PROP)) { 6830445b4c2eSsb155480 PRN("vds \"%s\" property does not exist", VD_REG_PROP); 68311ae08745Sheppo return (DDI_FAILURE); 68321ae08745Sheppo } 68331ae08745Sheppo 68341ae08745Sheppo /* Get the MD instance for later MDEG registration */ 68351ae08745Sheppo cfg_handle = ddi_prop_get_int(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 6836445b4c2eSsb155480 VD_REG_PROP, -1); 68371ae08745Sheppo 68381ae08745Sheppo if (ddi_soft_state_zalloc(vds_state, instance) != DDI_SUCCESS) { 68391ae08745Sheppo PRN("Could not allocate state for instance %u", instance); 68401ae08745Sheppo return (DDI_FAILURE); 68411ae08745Sheppo } 68421ae08745Sheppo 68431ae08745Sheppo if ((vds = ddi_get_soft_state(vds_state, instance)) == NULL) { 68441ae08745Sheppo PRN("Could not get state for instance %u", instance); 68451ae08745Sheppo ddi_soft_state_free(vds_state, instance); 68461ae08745Sheppo return (DDI_FAILURE); 68471ae08745Sheppo } 68481ae08745Sheppo 68491ae08745Sheppo vds->dip = dip; 68501ae08745Sheppo vds->vd_table = mod_hash_create_ptrhash("vds_vd_table", VDS_NCHAINS, 685187a7269eSachartre vds_destroy_vd, sizeof (void *)); 685287a7269eSachartre 68531ae08745Sheppo ASSERT(vds->vd_table != NULL); 68541ae08745Sheppo 68551ae08745Sheppo if ((status = ldi_ident_from_dip(dip, &vds->ldi_ident)) != 0) { 68561ae08745Sheppo PRN("ldi_ident_from_dip() returned errno %d", status); 68571ae08745Sheppo return (DDI_FAILURE); 68581ae08745Sheppo } 68591ae08745Sheppo vds->initialized |= VDS_LDI; 68601ae08745Sheppo 68611ae08745Sheppo /* Register for MD updates */ 6862445b4c2eSsb155480 sz = sizeof (vds_prop_template); 6863445b4c2eSsb155480 pspecp = kmem_alloc(sz, KM_SLEEP); 6864445b4c2eSsb155480 bcopy(vds_prop_template, pspecp, sz); 6865445b4c2eSsb155480 6866445b4c2eSsb155480 VDS_SET_MDEG_PROP_INST(pspecp, cfg_handle); 6867445b4c2eSsb155480 6868445b4c2eSsb155480 /* initialize the complete prop spec structure */ 6869445b4c2eSsb155480 ispecp = kmem_zalloc(sizeof (mdeg_node_spec_t), KM_SLEEP); 6870445b4c2eSsb155480 ispecp->namep = "virtual-device"; 6871445b4c2eSsb155480 ispecp->specp = pspecp; 6872445b4c2eSsb155480 6873445b4c2eSsb155480 if (mdeg_register(ispecp, &vd_match, vds_process_md, vds, 68741ae08745Sheppo &vds->mdeg) != MDEG_SUCCESS) { 68751ae08745Sheppo PRN("Unable to register for MD updates"); 6876445b4c2eSsb155480 kmem_free(ispecp, sizeof (mdeg_node_spec_t)); 6877445b4c2eSsb155480 kmem_free(pspecp, sz); 68781ae08745Sheppo return (DDI_FAILURE); 68791ae08745Sheppo } 6880445b4c2eSsb155480 6881445b4c2eSsb155480 vds->ispecp = ispecp; 68821ae08745Sheppo vds->initialized |= VDS_MDEG; 68831ae08745Sheppo 68840a55fbb7Slm66018 /* Prevent auto-detaching so driver is available whenever MD changes */ 68850a55fbb7Slm66018 if (ddi_prop_update_int(DDI_DEV_T_NONE, dip, DDI_NO_AUTODETACH, 1) != 68860a55fbb7Slm66018 DDI_PROP_SUCCESS) { 68870a55fbb7Slm66018 PRN("failed to set \"%s\" property for instance %u", 68880a55fbb7Slm66018 DDI_NO_AUTODETACH, instance); 68890a55fbb7Slm66018 } 68900a55fbb7Slm66018 68918fce2fd6Sachartre /* read any user defined driver types from conf file and update list */ 68928fce2fd6Sachartre vds_driver_types_update(vds); 68938fce2fd6Sachartre 68941ae08745Sheppo ddi_report_dev(dip); 68951ae08745Sheppo return (DDI_SUCCESS); 68961ae08745Sheppo } 68971ae08745Sheppo 68981ae08745Sheppo static int 68991ae08745Sheppo vds_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 69001ae08745Sheppo { 69011ae08745Sheppo int status; 69021ae08745Sheppo 69031ae08745Sheppo switch (cmd) { 69041ae08745Sheppo case DDI_ATTACH: 6905d10e4ef2Snarayan PR0("Attaching"); 69061ae08745Sheppo if ((status = vds_do_attach(dip)) != DDI_SUCCESS) 69071ae08745Sheppo (void) vds_detach(dip, DDI_DETACH); 69081ae08745Sheppo return (status); 69091ae08745Sheppo case DDI_RESUME: 6910d10e4ef2Snarayan PR0("No action required for DDI_RESUME"); 69111ae08745Sheppo return (DDI_SUCCESS); 69121ae08745Sheppo default: 69131ae08745Sheppo return (DDI_FAILURE); 69141ae08745Sheppo } 69151ae08745Sheppo } 69161ae08745Sheppo 69171ae08745Sheppo static struct dev_ops vds_ops = { 69181ae08745Sheppo DEVO_REV, /* devo_rev */ 69191ae08745Sheppo 0, /* devo_refcnt */ 69201ae08745Sheppo ddi_no_info, /* devo_getinfo */ 69211ae08745Sheppo nulldev, /* devo_identify */ 69221ae08745Sheppo nulldev, /* devo_probe */ 69231ae08745Sheppo vds_attach, /* devo_attach */ 69241ae08745Sheppo vds_detach, /* devo_detach */ 69251ae08745Sheppo nodev, /* devo_reset */ 69261ae08745Sheppo NULL, /* devo_cb_ops */ 69271ae08745Sheppo NULL, /* devo_bus_ops */ 69281ae08745Sheppo nulldev /* devo_power */ 69291ae08745Sheppo }; 69301ae08745Sheppo 69311ae08745Sheppo static struct modldrv modldrv = { 69321ae08745Sheppo &mod_driverops, 6933205eeb1aSlm66018 "virtual disk server", 69341ae08745Sheppo &vds_ops, 69351ae08745Sheppo }; 69361ae08745Sheppo 69371ae08745Sheppo static struct modlinkage modlinkage = { 69381ae08745Sheppo MODREV_1, 69391ae08745Sheppo &modldrv, 69401ae08745Sheppo NULL 69411ae08745Sheppo }; 69421ae08745Sheppo 69431ae08745Sheppo 69441ae08745Sheppo int 69451ae08745Sheppo _init(void) 69461ae08745Sheppo { 694717cadca8Slm66018 int status; 6948d10e4ef2Snarayan 69491ae08745Sheppo if ((status = ddi_soft_state_init(&vds_state, sizeof (vds_t), 1)) != 0) 69501ae08745Sheppo return (status); 695117cadca8Slm66018 69521ae08745Sheppo if ((status = mod_install(&modlinkage)) != 0) { 69531ae08745Sheppo ddi_soft_state_fini(&vds_state); 69541ae08745Sheppo return (status); 69551ae08745Sheppo } 69561ae08745Sheppo 69571ae08745Sheppo return (0); 69581ae08745Sheppo } 69591ae08745Sheppo 69601ae08745Sheppo int 69611ae08745Sheppo _info(struct modinfo *modinfop) 69621ae08745Sheppo { 69631ae08745Sheppo return (mod_info(&modlinkage, modinfop)); 69641ae08745Sheppo } 69651ae08745Sheppo 69661ae08745Sheppo int 69671ae08745Sheppo _fini(void) 69681ae08745Sheppo { 69691ae08745Sheppo int status; 69701ae08745Sheppo 69711ae08745Sheppo if ((status = mod_remove(&modlinkage)) != 0) 69721ae08745Sheppo return (status); 69731ae08745Sheppo ddi_soft_state_fini(&vds_state); 69741ae08745Sheppo return (0); 69751ae08745Sheppo } 6976